Deleted Added
full compact
sctp_output.c (196364) sctp_output.c (197288)
1/*-
2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
32
33#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 196364 2009-08-18 19:58:49Z tuexen $");
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 197288 2009-09-17 15:11:12Z rrs $");
35
36#include <netinet/sctp_os.h>
37#include <sys/proc.h>
38#include <netinet/sctp_var.h>
39#include <netinet/sctp_sysctl.h>
40#include <netinet/sctp_header.h>
41#include <netinet/sctp_pcb.h>
42#include <netinet/sctputil.h>
43#include <netinet/sctp_output.h>
44#include <netinet/sctp_uio.h>
45#include <netinet/sctputil.h>
46#include <netinet/sctp_auth.h>
47#include <netinet/sctp_timer.h>
48#include <netinet/sctp_asconf.h>
49#include <netinet/sctp_indata.h>
50#include <netinet/sctp_bsd_addr.h>
51#include <netinet/sctp_input.h>
52#include <netinet/sctp_crc32.h>
53#include <netinet/udp.h>
54#include <machine/in_cksum.h>
55
56
57
58#define SCTP_MAX_GAPS_INARRAY 4
59struct sack_track {
60 uint8_t right_edge; /* mergable on the right edge */
61 uint8_t left_edge; /* mergable on the left edge */
62 uint8_t num_entries;
63 uint8_t spare;
64 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65};
66
67struct sack_track sack_array[256] = {
68 {0, 0, 0, 0, /* 0x00 */
69 {{0, 0},
70 {0, 0},
71 {0, 0},
72 {0, 0}
73 }
74 },
75 {1, 0, 1, 0, /* 0x01 */
76 {{0, 0},
77 {0, 0},
78 {0, 0},
79 {0, 0}
80 }
81 },
82 {0, 0, 1, 0, /* 0x02 */
83 {{1, 1},
84 {0, 0},
85 {0, 0},
86 {0, 0}
87 }
88 },
89 {1, 0, 1, 0, /* 0x03 */
90 {{0, 1},
91 {0, 0},
92 {0, 0},
93 {0, 0}
94 }
95 },
96 {0, 0, 1, 0, /* 0x04 */
97 {{2, 2},
98 {0, 0},
99 {0, 0},
100 {0, 0}
101 }
102 },
103 {1, 0, 2, 0, /* 0x05 */
104 {{0, 0},
105 {2, 2},
106 {0, 0},
107 {0, 0}
108 }
109 },
110 {0, 0, 1, 0, /* 0x06 */
111 {{1, 2},
112 {0, 0},
113 {0, 0},
114 {0, 0}
115 }
116 },
117 {1, 0, 1, 0, /* 0x07 */
118 {{0, 2},
119 {0, 0},
120 {0, 0},
121 {0, 0}
122 }
123 },
124 {0, 0, 1, 0, /* 0x08 */
125 {{3, 3},
126 {0, 0},
127 {0, 0},
128 {0, 0}
129 }
130 },
131 {1, 0, 2, 0, /* 0x09 */
132 {{0, 0},
133 {3, 3},
134 {0, 0},
135 {0, 0}
136 }
137 },
138 {0, 0, 2, 0, /* 0x0a */
139 {{1, 1},
140 {3, 3},
141 {0, 0},
142 {0, 0}
143 }
144 },
145 {1, 0, 2, 0, /* 0x0b */
146 {{0, 1},
147 {3, 3},
148 {0, 0},
149 {0, 0}
150 }
151 },
152 {0, 0, 1, 0, /* 0x0c */
153 {{2, 3},
154 {0, 0},
155 {0, 0},
156 {0, 0}
157 }
158 },
159 {1, 0, 2, 0, /* 0x0d */
160 {{0, 0},
161 {2, 3},
162 {0, 0},
163 {0, 0}
164 }
165 },
166 {0, 0, 1, 0, /* 0x0e */
167 {{1, 3},
168 {0, 0},
169 {0, 0},
170 {0, 0}
171 }
172 },
173 {1, 0, 1, 0, /* 0x0f */
174 {{0, 3},
175 {0, 0},
176 {0, 0},
177 {0, 0}
178 }
179 },
180 {0, 0, 1, 0, /* 0x10 */
181 {{4, 4},
182 {0, 0},
183 {0, 0},
184 {0, 0}
185 }
186 },
187 {1, 0, 2, 0, /* 0x11 */
188 {{0, 0},
189 {4, 4},
190 {0, 0},
191 {0, 0}
192 }
193 },
194 {0, 0, 2, 0, /* 0x12 */
195 {{1, 1},
196 {4, 4},
197 {0, 0},
198 {0, 0}
199 }
200 },
201 {1, 0, 2, 0, /* 0x13 */
202 {{0, 1},
203 {4, 4},
204 {0, 0},
205 {0, 0}
206 }
207 },
208 {0, 0, 2, 0, /* 0x14 */
209 {{2, 2},
210 {4, 4},
211 {0, 0},
212 {0, 0}
213 }
214 },
215 {1, 0, 3, 0, /* 0x15 */
216 {{0, 0},
217 {2, 2},
218 {4, 4},
219 {0, 0}
220 }
221 },
222 {0, 0, 2, 0, /* 0x16 */
223 {{1, 2},
224 {4, 4},
225 {0, 0},
226 {0, 0}
227 }
228 },
229 {1, 0, 2, 0, /* 0x17 */
230 {{0, 2},
231 {4, 4},
232 {0, 0},
233 {0, 0}
234 }
235 },
236 {0, 0, 1, 0, /* 0x18 */
237 {{3, 4},
238 {0, 0},
239 {0, 0},
240 {0, 0}
241 }
242 },
243 {1, 0, 2, 0, /* 0x19 */
244 {{0, 0},
245 {3, 4},
246 {0, 0},
247 {0, 0}
248 }
249 },
250 {0, 0, 2, 0, /* 0x1a */
251 {{1, 1},
252 {3, 4},
253 {0, 0},
254 {0, 0}
255 }
256 },
257 {1, 0, 2, 0, /* 0x1b */
258 {{0, 1},
259 {3, 4},
260 {0, 0},
261 {0, 0}
262 }
263 },
264 {0, 0, 1, 0, /* 0x1c */
265 {{2, 4},
266 {0, 0},
267 {0, 0},
268 {0, 0}
269 }
270 },
271 {1, 0, 2, 0, /* 0x1d */
272 {{0, 0},
273 {2, 4},
274 {0, 0},
275 {0, 0}
276 }
277 },
278 {0, 0, 1, 0, /* 0x1e */
279 {{1, 4},
280 {0, 0},
281 {0, 0},
282 {0, 0}
283 }
284 },
285 {1, 0, 1, 0, /* 0x1f */
286 {{0, 4},
287 {0, 0},
288 {0, 0},
289 {0, 0}
290 }
291 },
292 {0, 0, 1, 0, /* 0x20 */
293 {{5, 5},
294 {0, 0},
295 {0, 0},
296 {0, 0}
297 }
298 },
299 {1, 0, 2, 0, /* 0x21 */
300 {{0, 0},
301 {5, 5},
302 {0, 0},
303 {0, 0}
304 }
305 },
306 {0, 0, 2, 0, /* 0x22 */
307 {{1, 1},
308 {5, 5},
309 {0, 0},
310 {0, 0}
311 }
312 },
313 {1, 0, 2, 0, /* 0x23 */
314 {{0, 1},
315 {5, 5},
316 {0, 0},
317 {0, 0}
318 }
319 },
320 {0, 0, 2, 0, /* 0x24 */
321 {{2, 2},
322 {5, 5},
323 {0, 0},
324 {0, 0}
325 }
326 },
327 {1, 0, 3, 0, /* 0x25 */
328 {{0, 0},
329 {2, 2},
330 {5, 5},
331 {0, 0}
332 }
333 },
334 {0, 0, 2, 0, /* 0x26 */
335 {{1, 2},
336 {5, 5},
337 {0, 0},
338 {0, 0}
339 }
340 },
341 {1, 0, 2, 0, /* 0x27 */
342 {{0, 2},
343 {5, 5},
344 {0, 0},
345 {0, 0}
346 }
347 },
348 {0, 0, 2, 0, /* 0x28 */
349 {{3, 3},
350 {5, 5},
351 {0, 0},
352 {0, 0}
353 }
354 },
355 {1, 0, 3, 0, /* 0x29 */
356 {{0, 0},
357 {3, 3},
358 {5, 5},
359 {0, 0}
360 }
361 },
362 {0, 0, 3, 0, /* 0x2a */
363 {{1, 1},
364 {3, 3},
365 {5, 5},
366 {0, 0}
367 }
368 },
369 {1, 0, 3, 0, /* 0x2b */
370 {{0, 1},
371 {3, 3},
372 {5, 5},
373 {0, 0}
374 }
375 },
376 {0, 0, 2, 0, /* 0x2c */
377 {{2, 3},
378 {5, 5},
379 {0, 0},
380 {0, 0}
381 }
382 },
383 {1, 0, 3, 0, /* 0x2d */
384 {{0, 0},
385 {2, 3},
386 {5, 5},
387 {0, 0}
388 }
389 },
390 {0, 0, 2, 0, /* 0x2e */
391 {{1, 3},
392 {5, 5},
393 {0, 0},
394 {0, 0}
395 }
396 },
397 {1, 0, 2, 0, /* 0x2f */
398 {{0, 3},
399 {5, 5},
400 {0, 0},
401 {0, 0}
402 }
403 },
404 {0, 0, 1, 0, /* 0x30 */
405 {{4, 5},
406 {0, 0},
407 {0, 0},
408 {0, 0}
409 }
410 },
411 {1, 0, 2, 0, /* 0x31 */
412 {{0, 0},
413 {4, 5},
414 {0, 0},
415 {0, 0}
416 }
417 },
418 {0, 0, 2, 0, /* 0x32 */
419 {{1, 1},
420 {4, 5},
421 {0, 0},
422 {0, 0}
423 }
424 },
425 {1, 0, 2, 0, /* 0x33 */
426 {{0, 1},
427 {4, 5},
428 {0, 0},
429 {0, 0}
430 }
431 },
432 {0, 0, 2, 0, /* 0x34 */
433 {{2, 2},
434 {4, 5},
435 {0, 0},
436 {0, 0}
437 }
438 },
439 {1, 0, 3, 0, /* 0x35 */
440 {{0, 0},
441 {2, 2},
442 {4, 5},
443 {0, 0}
444 }
445 },
446 {0, 0, 2, 0, /* 0x36 */
447 {{1, 2},
448 {4, 5},
449 {0, 0},
450 {0, 0}
451 }
452 },
453 {1, 0, 2, 0, /* 0x37 */
454 {{0, 2},
455 {4, 5},
456 {0, 0},
457 {0, 0}
458 }
459 },
460 {0, 0, 1, 0, /* 0x38 */
461 {{3, 5},
462 {0, 0},
463 {0, 0},
464 {0, 0}
465 }
466 },
467 {1, 0, 2, 0, /* 0x39 */
468 {{0, 0},
469 {3, 5},
470 {0, 0},
471 {0, 0}
472 }
473 },
474 {0, 0, 2, 0, /* 0x3a */
475 {{1, 1},
476 {3, 5},
477 {0, 0},
478 {0, 0}
479 }
480 },
481 {1, 0, 2, 0, /* 0x3b */
482 {{0, 1},
483 {3, 5},
484 {0, 0},
485 {0, 0}
486 }
487 },
488 {0, 0, 1, 0, /* 0x3c */
489 {{2, 5},
490 {0, 0},
491 {0, 0},
492 {0, 0}
493 }
494 },
495 {1, 0, 2, 0, /* 0x3d */
496 {{0, 0},
497 {2, 5},
498 {0, 0},
499 {0, 0}
500 }
501 },
502 {0, 0, 1, 0, /* 0x3e */
503 {{1, 5},
504 {0, 0},
505 {0, 0},
506 {0, 0}
507 }
508 },
509 {1, 0, 1, 0, /* 0x3f */
510 {{0, 5},
511 {0, 0},
512 {0, 0},
513 {0, 0}
514 }
515 },
516 {0, 0, 1, 0, /* 0x40 */
517 {{6, 6},
518 {0, 0},
519 {0, 0},
520 {0, 0}
521 }
522 },
523 {1, 0, 2, 0, /* 0x41 */
524 {{0, 0},
525 {6, 6},
526 {0, 0},
527 {0, 0}
528 }
529 },
530 {0, 0, 2, 0, /* 0x42 */
531 {{1, 1},
532 {6, 6},
533 {0, 0},
534 {0, 0}
535 }
536 },
537 {1, 0, 2, 0, /* 0x43 */
538 {{0, 1},
539 {6, 6},
540 {0, 0},
541 {0, 0}
542 }
543 },
544 {0, 0, 2, 0, /* 0x44 */
545 {{2, 2},
546 {6, 6},
547 {0, 0},
548 {0, 0}
549 }
550 },
551 {1, 0, 3, 0, /* 0x45 */
552 {{0, 0},
553 {2, 2},
554 {6, 6},
555 {0, 0}
556 }
557 },
558 {0, 0, 2, 0, /* 0x46 */
559 {{1, 2},
560 {6, 6},
561 {0, 0},
562 {0, 0}
563 }
564 },
565 {1, 0, 2, 0, /* 0x47 */
566 {{0, 2},
567 {6, 6},
568 {0, 0},
569 {0, 0}
570 }
571 },
572 {0, 0, 2, 0, /* 0x48 */
573 {{3, 3},
574 {6, 6},
575 {0, 0},
576 {0, 0}
577 }
578 },
579 {1, 0, 3, 0, /* 0x49 */
580 {{0, 0},
581 {3, 3},
582 {6, 6},
583 {0, 0}
584 }
585 },
586 {0, 0, 3, 0, /* 0x4a */
587 {{1, 1},
588 {3, 3},
589 {6, 6},
590 {0, 0}
591 }
592 },
593 {1, 0, 3, 0, /* 0x4b */
594 {{0, 1},
595 {3, 3},
596 {6, 6},
597 {0, 0}
598 }
599 },
600 {0, 0, 2, 0, /* 0x4c */
601 {{2, 3},
602 {6, 6},
603 {0, 0},
604 {0, 0}
605 }
606 },
607 {1, 0, 3, 0, /* 0x4d */
608 {{0, 0},
609 {2, 3},
610 {6, 6},
611 {0, 0}
612 }
613 },
614 {0, 0, 2, 0, /* 0x4e */
615 {{1, 3},
616 {6, 6},
617 {0, 0},
618 {0, 0}
619 }
620 },
621 {1, 0, 2, 0, /* 0x4f */
622 {{0, 3},
623 {6, 6},
624 {0, 0},
625 {0, 0}
626 }
627 },
628 {0, 0, 2, 0, /* 0x50 */
629 {{4, 4},
630 {6, 6},
631 {0, 0},
632 {0, 0}
633 }
634 },
635 {1, 0, 3, 0, /* 0x51 */
636 {{0, 0},
637 {4, 4},
638 {6, 6},
639 {0, 0}
640 }
641 },
642 {0, 0, 3, 0, /* 0x52 */
643 {{1, 1},
644 {4, 4},
645 {6, 6},
646 {0, 0}
647 }
648 },
649 {1, 0, 3, 0, /* 0x53 */
650 {{0, 1},
651 {4, 4},
652 {6, 6},
653 {0, 0}
654 }
655 },
656 {0, 0, 3, 0, /* 0x54 */
657 {{2, 2},
658 {4, 4},
659 {6, 6},
660 {0, 0}
661 }
662 },
663 {1, 0, 4, 0, /* 0x55 */
664 {{0, 0},
665 {2, 2},
666 {4, 4},
667 {6, 6}
668 }
669 },
670 {0, 0, 3, 0, /* 0x56 */
671 {{1, 2},
672 {4, 4},
673 {6, 6},
674 {0, 0}
675 }
676 },
677 {1, 0, 3, 0, /* 0x57 */
678 {{0, 2},
679 {4, 4},
680 {6, 6},
681 {0, 0}
682 }
683 },
684 {0, 0, 2, 0, /* 0x58 */
685 {{3, 4},
686 {6, 6},
687 {0, 0},
688 {0, 0}
689 }
690 },
691 {1, 0, 3, 0, /* 0x59 */
692 {{0, 0},
693 {3, 4},
694 {6, 6},
695 {0, 0}
696 }
697 },
698 {0, 0, 3, 0, /* 0x5a */
699 {{1, 1},
700 {3, 4},
701 {6, 6},
702 {0, 0}
703 }
704 },
705 {1, 0, 3, 0, /* 0x5b */
706 {{0, 1},
707 {3, 4},
708 {6, 6},
709 {0, 0}
710 }
711 },
712 {0, 0, 2, 0, /* 0x5c */
713 {{2, 4},
714 {6, 6},
715 {0, 0},
716 {0, 0}
717 }
718 },
719 {1, 0, 3, 0, /* 0x5d */
720 {{0, 0},
721 {2, 4},
722 {6, 6},
723 {0, 0}
724 }
725 },
726 {0, 0, 2, 0, /* 0x5e */
727 {{1, 4},
728 {6, 6},
729 {0, 0},
730 {0, 0}
731 }
732 },
733 {1, 0, 2, 0, /* 0x5f */
734 {{0, 4},
735 {6, 6},
736 {0, 0},
737 {0, 0}
738 }
739 },
740 {0, 0, 1, 0, /* 0x60 */
741 {{5, 6},
742 {0, 0},
743 {0, 0},
744 {0, 0}
745 }
746 },
747 {1, 0, 2, 0, /* 0x61 */
748 {{0, 0},
749 {5, 6},
750 {0, 0},
751 {0, 0}
752 }
753 },
754 {0, 0, 2, 0, /* 0x62 */
755 {{1, 1},
756 {5, 6},
757 {0, 0},
758 {0, 0}
759 }
760 },
761 {1, 0, 2, 0, /* 0x63 */
762 {{0, 1},
763 {5, 6},
764 {0, 0},
765 {0, 0}
766 }
767 },
768 {0, 0, 2, 0, /* 0x64 */
769 {{2, 2},
770 {5, 6},
771 {0, 0},
772 {0, 0}
773 }
774 },
775 {1, 0, 3, 0, /* 0x65 */
776 {{0, 0},
777 {2, 2},
778 {5, 6},
779 {0, 0}
780 }
781 },
782 {0, 0, 2, 0, /* 0x66 */
783 {{1, 2},
784 {5, 6},
785 {0, 0},
786 {0, 0}
787 }
788 },
789 {1, 0, 2, 0, /* 0x67 */
790 {{0, 2},
791 {5, 6},
792 {0, 0},
793 {0, 0}
794 }
795 },
796 {0, 0, 2, 0, /* 0x68 */
797 {{3, 3},
798 {5, 6},
799 {0, 0},
800 {0, 0}
801 }
802 },
803 {1, 0, 3, 0, /* 0x69 */
804 {{0, 0},
805 {3, 3},
806 {5, 6},
807 {0, 0}
808 }
809 },
810 {0, 0, 3, 0, /* 0x6a */
811 {{1, 1},
812 {3, 3},
813 {5, 6},
814 {0, 0}
815 }
816 },
817 {1, 0, 3, 0, /* 0x6b */
818 {{0, 1},
819 {3, 3},
820 {5, 6},
821 {0, 0}
822 }
823 },
824 {0, 0, 2, 0, /* 0x6c */
825 {{2, 3},
826 {5, 6},
827 {0, 0},
828 {0, 0}
829 }
830 },
831 {1, 0, 3, 0, /* 0x6d */
832 {{0, 0},
833 {2, 3},
834 {5, 6},
835 {0, 0}
836 }
837 },
838 {0, 0, 2, 0, /* 0x6e */
839 {{1, 3},
840 {5, 6},
841 {0, 0},
842 {0, 0}
843 }
844 },
845 {1, 0, 2, 0, /* 0x6f */
846 {{0, 3},
847 {5, 6},
848 {0, 0},
849 {0, 0}
850 }
851 },
852 {0, 0, 1, 0, /* 0x70 */
853 {{4, 6},
854 {0, 0},
855 {0, 0},
856 {0, 0}
857 }
858 },
859 {1, 0, 2, 0, /* 0x71 */
860 {{0, 0},
861 {4, 6},
862 {0, 0},
863 {0, 0}
864 }
865 },
866 {0, 0, 2, 0, /* 0x72 */
867 {{1, 1},
868 {4, 6},
869 {0, 0},
870 {0, 0}
871 }
872 },
873 {1, 0, 2, 0, /* 0x73 */
874 {{0, 1},
875 {4, 6},
876 {0, 0},
877 {0, 0}
878 }
879 },
880 {0, 0, 2, 0, /* 0x74 */
881 {{2, 2},
882 {4, 6},
883 {0, 0},
884 {0, 0}
885 }
886 },
887 {1, 0, 3, 0, /* 0x75 */
888 {{0, 0},
889 {2, 2},
890 {4, 6},
891 {0, 0}
892 }
893 },
894 {0, 0, 2, 0, /* 0x76 */
895 {{1, 2},
896 {4, 6},
897 {0, 0},
898 {0, 0}
899 }
900 },
901 {1, 0, 2, 0, /* 0x77 */
902 {{0, 2},
903 {4, 6},
904 {0, 0},
905 {0, 0}
906 }
907 },
908 {0, 0, 1, 0, /* 0x78 */
909 {{3, 6},
910 {0, 0},
911 {0, 0},
912 {0, 0}
913 }
914 },
915 {1, 0, 2, 0, /* 0x79 */
916 {{0, 0},
917 {3, 6},
918 {0, 0},
919 {0, 0}
920 }
921 },
922 {0, 0, 2, 0, /* 0x7a */
923 {{1, 1},
924 {3, 6},
925 {0, 0},
926 {0, 0}
927 }
928 },
929 {1, 0, 2, 0, /* 0x7b */
930 {{0, 1},
931 {3, 6},
932 {0, 0},
933 {0, 0}
934 }
935 },
936 {0, 0, 1, 0, /* 0x7c */
937 {{2, 6},
938 {0, 0},
939 {0, 0},
940 {0, 0}
941 }
942 },
943 {1, 0, 2, 0, /* 0x7d */
944 {{0, 0},
945 {2, 6},
946 {0, 0},
947 {0, 0}
948 }
949 },
950 {0, 0, 1, 0, /* 0x7e */
951 {{1, 6},
952 {0, 0},
953 {0, 0},
954 {0, 0}
955 }
956 },
957 {1, 0, 1, 0, /* 0x7f */
958 {{0, 6},
959 {0, 0},
960 {0, 0},
961 {0, 0}
962 }
963 },
964 {0, 1, 1, 0, /* 0x80 */
965 {{7, 7},
966 {0, 0},
967 {0, 0},
968 {0, 0}
969 }
970 },
971 {1, 1, 2, 0, /* 0x81 */
972 {{0, 0},
973 {7, 7},
974 {0, 0},
975 {0, 0}
976 }
977 },
978 {0, 1, 2, 0, /* 0x82 */
979 {{1, 1},
980 {7, 7},
981 {0, 0},
982 {0, 0}
983 }
984 },
985 {1, 1, 2, 0, /* 0x83 */
986 {{0, 1},
987 {7, 7},
988 {0, 0},
989 {0, 0}
990 }
991 },
992 {0, 1, 2, 0, /* 0x84 */
993 {{2, 2},
994 {7, 7},
995 {0, 0},
996 {0, 0}
997 }
998 },
999 {1, 1, 3, 0, /* 0x85 */
1000 {{0, 0},
1001 {2, 2},
1002 {7, 7},
1003 {0, 0}
1004 }
1005 },
1006 {0, 1, 2, 0, /* 0x86 */
1007 {{1, 2},
1008 {7, 7},
1009 {0, 0},
1010 {0, 0}
1011 }
1012 },
1013 {1, 1, 2, 0, /* 0x87 */
1014 {{0, 2},
1015 {7, 7},
1016 {0, 0},
1017 {0, 0}
1018 }
1019 },
1020 {0, 1, 2, 0, /* 0x88 */
1021 {{3, 3},
1022 {7, 7},
1023 {0, 0},
1024 {0, 0}
1025 }
1026 },
1027 {1, 1, 3, 0, /* 0x89 */
1028 {{0, 0},
1029 {3, 3},
1030 {7, 7},
1031 {0, 0}
1032 }
1033 },
1034 {0, 1, 3, 0, /* 0x8a */
1035 {{1, 1},
1036 {3, 3},
1037 {7, 7},
1038 {0, 0}
1039 }
1040 },
1041 {1, 1, 3, 0, /* 0x8b */
1042 {{0, 1},
1043 {3, 3},
1044 {7, 7},
1045 {0, 0}
1046 }
1047 },
1048 {0, 1, 2, 0, /* 0x8c */
1049 {{2, 3},
1050 {7, 7},
1051 {0, 0},
1052 {0, 0}
1053 }
1054 },
1055 {1, 1, 3, 0, /* 0x8d */
1056 {{0, 0},
1057 {2, 3},
1058 {7, 7},
1059 {0, 0}
1060 }
1061 },
1062 {0, 1, 2, 0, /* 0x8e */
1063 {{1, 3},
1064 {7, 7},
1065 {0, 0},
1066 {0, 0}
1067 }
1068 },
1069 {1, 1, 2, 0, /* 0x8f */
1070 {{0, 3},
1071 {7, 7},
1072 {0, 0},
1073 {0, 0}
1074 }
1075 },
1076 {0, 1, 2, 0, /* 0x90 */
1077 {{4, 4},
1078 {7, 7},
1079 {0, 0},
1080 {0, 0}
1081 }
1082 },
1083 {1, 1, 3, 0, /* 0x91 */
1084 {{0, 0},
1085 {4, 4},
1086 {7, 7},
1087 {0, 0}
1088 }
1089 },
1090 {0, 1, 3, 0, /* 0x92 */
1091 {{1, 1},
1092 {4, 4},
1093 {7, 7},
1094 {0, 0}
1095 }
1096 },
1097 {1, 1, 3, 0, /* 0x93 */
1098 {{0, 1},
1099 {4, 4},
1100 {7, 7},
1101 {0, 0}
1102 }
1103 },
1104 {0, 1, 3, 0, /* 0x94 */
1105 {{2, 2},
1106 {4, 4},
1107 {7, 7},
1108 {0, 0}
1109 }
1110 },
1111 {1, 1, 4, 0, /* 0x95 */
1112 {{0, 0},
1113 {2, 2},
1114 {4, 4},
1115 {7, 7}
1116 }
1117 },
1118 {0, 1, 3, 0, /* 0x96 */
1119 {{1, 2},
1120 {4, 4},
1121 {7, 7},
1122 {0, 0}
1123 }
1124 },
1125 {1, 1, 3, 0, /* 0x97 */
1126 {{0, 2},
1127 {4, 4},
1128 {7, 7},
1129 {0, 0}
1130 }
1131 },
1132 {0, 1, 2, 0, /* 0x98 */
1133 {{3, 4},
1134 {7, 7},
1135 {0, 0},
1136 {0, 0}
1137 }
1138 },
1139 {1, 1, 3, 0, /* 0x99 */
1140 {{0, 0},
1141 {3, 4},
1142 {7, 7},
1143 {0, 0}
1144 }
1145 },
1146 {0, 1, 3, 0, /* 0x9a */
1147 {{1, 1},
1148 {3, 4},
1149 {7, 7},
1150 {0, 0}
1151 }
1152 },
1153 {1, 1, 3, 0, /* 0x9b */
1154 {{0, 1},
1155 {3, 4},
1156 {7, 7},
1157 {0, 0}
1158 }
1159 },
1160 {0, 1, 2, 0, /* 0x9c */
1161 {{2, 4},
1162 {7, 7},
1163 {0, 0},
1164 {0, 0}
1165 }
1166 },
1167 {1, 1, 3, 0, /* 0x9d */
1168 {{0, 0},
1169 {2, 4},
1170 {7, 7},
1171 {0, 0}
1172 }
1173 },
1174 {0, 1, 2, 0, /* 0x9e */
1175 {{1, 4},
1176 {7, 7},
1177 {0, 0},
1178 {0, 0}
1179 }
1180 },
1181 {1, 1, 2, 0, /* 0x9f */
1182 {{0, 4},
1183 {7, 7},
1184 {0, 0},
1185 {0, 0}
1186 }
1187 },
1188 {0, 1, 2, 0, /* 0xa0 */
1189 {{5, 5},
1190 {7, 7},
1191 {0, 0},
1192 {0, 0}
1193 }
1194 },
1195 {1, 1, 3, 0, /* 0xa1 */
1196 {{0, 0},
1197 {5, 5},
1198 {7, 7},
1199 {0, 0}
1200 }
1201 },
1202 {0, 1, 3, 0, /* 0xa2 */
1203 {{1, 1},
1204 {5, 5},
1205 {7, 7},
1206 {0, 0}
1207 }
1208 },
1209 {1, 1, 3, 0, /* 0xa3 */
1210 {{0, 1},
1211 {5, 5},
1212 {7, 7},
1213 {0, 0}
1214 }
1215 },
1216 {0, 1, 3, 0, /* 0xa4 */
1217 {{2, 2},
1218 {5, 5},
1219 {7, 7},
1220 {0, 0}
1221 }
1222 },
1223 {1, 1, 4, 0, /* 0xa5 */
1224 {{0, 0},
1225 {2, 2},
1226 {5, 5},
1227 {7, 7}
1228 }
1229 },
1230 {0, 1, 3, 0, /* 0xa6 */
1231 {{1, 2},
1232 {5, 5},
1233 {7, 7},
1234 {0, 0}
1235 }
1236 },
1237 {1, 1, 3, 0, /* 0xa7 */
1238 {{0, 2},
1239 {5, 5},
1240 {7, 7},
1241 {0, 0}
1242 }
1243 },
1244 {0, 1, 3, 0, /* 0xa8 */
1245 {{3, 3},
1246 {5, 5},
1247 {7, 7},
1248 {0, 0}
1249 }
1250 },
1251 {1, 1, 4, 0, /* 0xa9 */
1252 {{0, 0},
1253 {3, 3},
1254 {5, 5},
1255 {7, 7}
1256 }
1257 },
1258 {0, 1, 4, 0, /* 0xaa */
1259 {{1, 1},
1260 {3, 3},
1261 {5, 5},
1262 {7, 7}
1263 }
1264 },
1265 {1, 1, 4, 0, /* 0xab */
1266 {{0, 1},
1267 {3, 3},
1268 {5, 5},
1269 {7, 7}
1270 }
1271 },
1272 {0, 1, 3, 0, /* 0xac */
1273 {{2, 3},
1274 {5, 5},
1275 {7, 7},
1276 {0, 0}
1277 }
1278 },
1279 {1, 1, 4, 0, /* 0xad */
1280 {{0, 0},
1281 {2, 3},
1282 {5, 5},
1283 {7, 7}
1284 }
1285 },
1286 {0, 1, 3, 0, /* 0xae */
1287 {{1, 3},
1288 {5, 5},
1289 {7, 7},
1290 {0, 0}
1291 }
1292 },
1293 {1, 1, 3, 0, /* 0xaf */
1294 {{0, 3},
1295 {5, 5},
1296 {7, 7},
1297 {0, 0}
1298 }
1299 },
1300 {0, 1, 2, 0, /* 0xb0 */
1301 {{4, 5},
1302 {7, 7},
1303 {0, 0},
1304 {0, 0}
1305 }
1306 },
1307 {1, 1, 3, 0, /* 0xb1 */
1308 {{0, 0},
1309 {4, 5},
1310 {7, 7},
1311 {0, 0}
1312 }
1313 },
1314 {0, 1, 3, 0, /* 0xb2 */
1315 {{1, 1},
1316 {4, 5},
1317 {7, 7},
1318 {0, 0}
1319 }
1320 },
1321 {1, 1, 3, 0, /* 0xb3 */
1322 {{0, 1},
1323 {4, 5},
1324 {7, 7},
1325 {0, 0}
1326 }
1327 },
1328 {0, 1, 3, 0, /* 0xb4 */
1329 {{2, 2},
1330 {4, 5},
1331 {7, 7},
1332 {0, 0}
1333 }
1334 },
1335 {1, 1, 4, 0, /* 0xb5 */
1336 {{0, 0},
1337 {2, 2},
1338 {4, 5},
1339 {7, 7}
1340 }
1341 },
1342 {0, 1, 3, 0, /* 0xb6 */
1343 {{1, 2},
1344 {4, 5},
1345 {7, 7},
1346 {0, 0}
1347 }
1348 },
1349 {1, 1, 3, 0, /* 0xb7 */
1350 {{0, 2},
1351 {4, 5},
1352 {7, 7},
1353 {0, 0}
1354 }
1355 },
1356 {0, 1, 2, 0, /* 0xb8 */
1357 {{3, 5},
1358 {7, 7},
1359 {0, 0},
1360 {0, 0}
1361 }
1362 },
1363 {1, 1, 3, 0, /* 0xb9 */
1364 {{0, 0},
1365 {3, 5},
1366 {7, 7},
1367 {0, 0}
1368 }
1369 },
1370 {0, 1, 3, 0, /* 0xba */
1371 {{1, 1},
1372 {3, 5},
1373 {7, 7},
1374 {0, 0}
1375 }
1376 },
1377 {1, 1, 3, 0, /* 0xbb */
1378 {{0, 1},
1379 {3, 5},
1380 {7, 7},
1381 {0, 0}
1382 }
1383 },
1384 {0, 1, 2, 0, /* 0xbc */
1385 {{2, 5},
1386 {7, 7},
1387 {0, 0},
1388 {0, 0}
1389 }
1390 },
1391 {1, 1, 3, 0, /* 0xbd */
1392 {{0, 0},
1393 {2, 5},
1394 {7, 7},
1395 {0, 0}
1396 }
1397 },
1398 {0, 1, 2, 0, /* 0xbe */
1399 {{1, 5},
1400 {7, 7},
1401 {0, 0},
1402 {0, 0}
1403 }
1404 },
1405 {1, 1, 2, 0, /* 0xbf */
1406 {{0, 5},
1407 {7, 7},
1408 {0, 0},
1409 {0, 0}
1410 }
1411 },
1412 {0, 1, 1, 0, /* 0xc0 */
1413 {{6, 7},
1414 {0, 0},
1415 {0, 0},
1416 {0, 0}
1417 }
1418 },
1419 {1, 1, 2, 0, /* 0xc1 */
1420 {{0, 0},
1421 {6, 7},
1422 {0, 0},
1423 {0, 0}
1424 }
1425 },
1426 {0, 1, 2, 0, /* 0xc2 */
1427 {{1, 1},
1428 {6, 7},
1429 {0, 0},
1430 {0, 0}
1431 }
1432 },
1433 {1, 1, 2, 0, /* 0xc3 */
1434 {{0, 1},
1435 {6, 7},
1436 {0, 0},
1437 {0, 0}
1438 }
1439 },
1440 {0, 1, 2, 0, /* 0xc4 */
1441 {{2, 2},
1442 {6, 7},
1443 {0, 0},
1444 {0, 0}
1445 }
1446 },
1447 {1, 1, 3, 0, /* 0xc5 */
1448 {{0, 0},
1449 {2, 2},
1450 {6, 7},
1451 {0, 0}
1452 }
1453 },
1454 {0, 1, 2, 0, /* 0xc6 */
1455 {{1, 2},
1456 {6, 7},
1457 {0, 0},
1458 {0, 0}
1459 }
1460 },
1461 {1, 1, 2, 0, /* 0xc7 */
1462 {{0, 2},
1463 {6, 7},
1464 {0, 0},
1465 {0, 0}
1466 }
1467 },
1468 {0, 1, 2, 0, /* 0xc8 */
1469 {{3, 3},
1470 {6, 7},
1471 {0, 0},
1472 {0, 0}
1473 }
1474 },
1475 {1, 1, 3, 0, /* 0xc9 */
1476 {{0, 0},
1477 {3, 3},
1478 {6, 7},
1479 {0, 0}
1480 }
1481 },
1482 {0, 1, 3, 0, /* 0xca */
1483 {{1, 1},
1484 {3, 3},
1485 {6, 7},
1486 {0, 0}
1487 }
1488 },
1489 {1, 1, 3, 0, /* 0xcb */
1490 {{0, 1},
1491 {3, 3},
1492 {6, 7},
1493 {0, 0}
1494 }
1495 },
1496 {0, 1, 2, 0, /* 0xcc */
1497 {{2, 3},
1498 {6, 7},
1499 {0, 0},
1500 {0, 0}
1501 }
1502 },
1503 {1, 1, 3, 0, /* 0xcd */
1504 {{0, 0},
1505 {2, 3},
1506 {6, 7},
1507 {0, 0}
1508 }
1509 },
1510 {0, 1, 2, 0, /* 0xce */
1511 {{1, 3},
1512 {6, 7},
1513 {0, 0},
1514 {0, 0}
1515 }
1516 },
1517 {1, 1, 2, 0, /* 0xcf */
1518 {{0, 3},
1519 {6, 7},
1520 {0, 0},
1521 {0, 0}
1522 }
1523 },
1524 {0, 1, 2, 0, /* 0xd0 */
1525 {{4, 4},
1526 {6, 7},
1527 {0, 0},
1528 {0, 0}
1529 }
1530 },
1531 {1, 1, 3, 0, /* 0xd1 */
1532 {{0, 0},
1533 {4, 4},
1534 {6, 7},
1535 {0, 0}
1536 }
1537 },
1538 {0, 1, 3, 0, /* 0xd2 */
1539 {{1, 1},
1540 {4, 4},
1541 {6, 7},
1542 {0, 0}
1543 }
1544 },
1545 {1, 1, 3, 0, /* 0xd3 */
1546 {{0, 1},
1547 {4, 4},
1548 {6, 7},
1549 {0, 0}
1550 }
1551 },
1552 {0, 1, 3, 0, /* 0xd4 */
1553 {{2, 2},
1554 {4, 4},
1555 {6, 7},
1556 {0, 0}
1557 }
1558 },
1559 {1, 1, 4, 0, /* 0xd5 */
1560 {{0, 0},
1561 {2, 2},
1562 {4, 4},
1563 {6, 7}
1564 }
1565 },
1566 {0, 1, 3, 0, /* 0xd6 */
1567 {{1, 2},
1568 {4, 4},
1569 {6, 7},
1570 {0, 0}
1571 }
1572 },
1573 {1, 1, 3, 0, /* 0xd7 */
1574 {{0, 2},
1575 {4, 4},
1576 {6, 7},
1577 {0, 0}
1578 }
1579 },
1580 {0, 1, 2, 0, /* 0xd8 */
1581 {{3, 4},
1582 {6, 7},
1583 {0, 0},
1584 {0, 0}
1585 }
1586 },
1587 {1, 1, 3, 0, /* 0xd9 */
1588 {{0, 0},
1589 {3, 4},
1590 {6, 7},
1591 {0, 0}
1592 }
1593 },
1594 {0, 1, 3, 0, /* 0xda */
1595 {{1, 1},
1596 {3, 4},
1597 {6, 7},
1598 {0, 0}
1599 }
1600 },
1601 {1, 1, 3, 0, /* 0xdb */
1602 {{0, 1},
1603 {3, 4},
1604 {6, 7},
1605 {0, 0}
1606 }
1607 },
1608 {0, 1, 2, 0, /* 0xdc */
1609 {{2, 4},
1610 {6, 7},
1611 {0, 0},
1612 {0, 0}
1613 }
1614 },
1615 {1, 1, 3, 0, /* 0xdd */
1616 {{0, 0},
1617 {2, 4},
1618 {6, 7},
1619 {0, 0}
1620 }
1621 },
1622 {0, 1, 2, 0, /* 0xde */
1623 {{1, 4},
1624 {6, 7},
1625 {0, 0},
1626 {0, 0}
1627 }
1628 },
1629 {1, 1, 2, 0, /* 0xdf */
1630 {{0, 4},
1631 {6, 7},
1632 {0, 0},
1633 {0, 0}
1634 }
1635 },
1636 {0, 1, 1, 0, /* 0xe0 */
1637 {{5, 7},
1638 {0, 0},
1639 {0, 0},
1640 {0, 0}
1641 }
1642 },
1643 {1, 1, 2, 0, /* 0xe1 */
1644 {{0, 0},
1645 {5, 7},
1646 {0, 0},
1647 {0, 0}
1648 }
1649 },
1650 {0, 1, 2, 0, /* 0xe2 */
1651 {{1, 1},
1652 {5, 7},
1653 {0, 0},
1654 {0, 0}
1655 }
1656 },
1657 {1, 1, 2, 0, /* 0xe3 */
1658 {{0, 1},
1659 {5, 7},
1660 {0, 0},
1661 {0, 0}
1662 }
1663 },
1664 {0, 1, 2, 0, /* 0xe4 */
1665 {{2, 2},
1666 {5, 7},
1667 {0, 0},
1668 {0, 0}
1669 }
1670 },
1671 {1, 1, 3, 0, /* 0xe5 */
1672 {{0, 0},
1673 {2, 2},
1674 {5, 7},
1675 {0, 0}
1676 }
1677 },
1678 {0, 1, 2, 0, /* 0xe6 */
1679 {{1, 2},
1680 {5, 7},
1681 {0, 0},
1682 {0, 0}
1683 }
1684 },
1685 {1, 1, 2, 0, /* 0xe7 */
1686 {{0, 2},
1687 {5, 7},
1688 {0, 0},
1689 {0, 0}
1690 }
1691 },
1692 {0, 1, 2, 0, /* 0xe8 */
1693 {{3, 3},
1694 {5, 7},
1695 {0, 0},
1696 {0, 0}
1697 }
1698 },
1699 {1, 1, 3, 0, /* 0xe9 */
1700 {{0, 0},
1701 {3, 3},
1702 {5, 7},
1703 {0, 0}
1704 }
1705 },
1706 {0, 1, 3, 0, /* 0xea */
1707 {{1, 1},
1708 {3, 3},
1709 {5, 7},
1710 {0, 0}
1711 }
1712 },
1713 {1, 1, 3, 0, /* 0xeb */
1714 {{0, 1},
1715 {3, 3},
1716 {5, 7},
1717 {0, 0}
1718 }
1719 },
1720 {0, 1, 2, 0, /* 0xec */
1721 {{2, 3},
1722 {5, 7},
1723 {0, 0},
1724 {0, 0}
1725 }
1726 },
1727 {1, 1, 3, 0, /* 0xed */
1728 {{0, 0},
1729 {2, 3},
1730 {5, 7},
1731 {0, 0}
1732 }
1733 },
1734 {0, 1, 2, 0, /* 0xee */
1735 {{1, 3},
1736 {5, 7},
1737 {0, 0},
1738 {0, 0}
1739 }
1740 },
1741 {1, 1, 2, 0, /* 0xef */
1742 {{0, 3},
1743 {5, 7},
1744 {0, 0},
1745 {0, 0}
1746 }
1747 },
1748 {0, 1, 1, 0, /* 0xf0 */
1749 {{4, 7},
1750 {0, 0},
1751 {0, 0},
1752 {0, 0}
1753 }
1754 },
1755 {1, 1, 2, 0, /* 0xf1 */
1756 {{0, 0},
1757 {4, 7},
1758 {0, 0},
1759 {0, 0}
1760 }
1761 },
1762 {0, 1, 2, 0, /* 0xf2 */
1763 {{1, 1},
1764 {4, 7},
1765 {0, 0},
1766 {0, 0}
1767 }
1768 },
1769 {1, 1, 2, 0, /* 0xf3 */
1770 {{0, 1},
1771 {4, 7},
1772 {0, 0},
1773 {0, 0}
1774 }
1775 },
1776 {0, 1, 2, 0, /* 0xf4 */
1777 {{2, 2},
1778 {4, 7},
1779 {0, 0},
1780 {0, 0}
1781 }
1782 },
1783 {1, 1, 3, 0, /* 0xf5 */
1784 {{0, 0},
1785 {2, 2},
1786 {4, 7},
1787 {0, 0}
1788 }
1789 },
1790 {0, 1, 2, 0, /* 0xf6 */
1791 {{1, 2},
1792 {4, 7},
1793 {0, 0},
1794 {0, 0}
1795 }
1796 },
1797 {1, 1, 2, 0, /* 0xf7 */
1798 {{0, 2},
1799 {4, 7},
1800 {0, 0},
1801 {0, 0}
1802 }
1803 },
1804 {0, 1, 1, 0, /* 0xf8 */
1805 {{3, 7},
1806 {0, 0},
1807 {0, 0},
1808 {0, 0}
1809 }
1810 },
1811 {1, 1, 2, 0, /* 0xf9 */
1812 {{0, 0},
1813 {3, 7},
1814 {0, 0},
1815 {0, 0}
1816 }
1817 },
1818 {0, 1, 2, 0, /* 0xfa */
1819 {{1, 1},
1820 {3, 7},
1821 {0, 0},
1822 {0, 0}
1823 }
1824 },
1825 {1, 1, 2, 0, /* 0xfb */
1826 {{0, 1},
1827 {3, 7},
1828 {0, 0},
1829 {0, 0}
1830 }
1831 },
1832 {0, 1, 1, 0, /* 0xfc */
1833 {{2, 7},
1834 {0, 0},
1835 {0, 0},
1836 {0, 0}
1837 }
1838 },
1839 {1, 1, 2, 0, /* 0xfd */
1840 {{0, 0},
1841 {2, 7},
1842 {0, 0},
1843 {0, 0}
1844 }
1845 },
1846 {0, 1, 1, 0, /* 0xfe */
1847 {{1, 7},
1848 {0, 0},
1849 {0, 0},
1850 {0, 0}
1851 }
1852 },
1853 {1, 1, 1, 0, /* 0xff */
1854 {{0, 7},
1855 {0, 0},
1856 {0, 0},
1857 {0, 0}
1858 }
1859 }
1860};
1861
1862
1863int
1864sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865 int ipv4_addr_legal,
1866 int ipv6_addr_legal,
1867 int loopback_scope,
1868 int ipv4_local_scope,
1869 int local_scope,
1870 int site_scope,
1871 int do_update)
1872{
1873 if ((loopback_scope == 0) &&
1874 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 /*
1876 * skip loopback if not in scope *
1877 */
1878 return (0);
1879 }
1880 switch (ifa->address.sa.sa_family) {
1881 case AF_INET:
1882 if (ipv4_addr_legal) {
1883 struct sockaddr_in *sin;
1884
1885 sin = (struct sockaddr_in *)&ifa->address.sin;
1886 if (sin->sin_addr.s_addr == 0) {
1887 /* not in scope , unspecified */
1888 return (0);
1889 }
1890 if ((ipv4_local_scope == 0) &&
1891 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 /* private address not in scope */
1893 return (0);
1894 }
1895 } else {
1896 return (0);
1897 }
1898 break;
1899#ifdef INET6
1900 case AF_INET6:
1901 if (ipv6_addr_legal) {
1902 struct sockaddr_in6 *sin6;
1903
1904 /*
1905 * Must update the flags, bummer, which means any
1906 * IFA locks must now be applied HERE <->
1907 */
1908 if (do_update) {
1909 sctp_gather_internal_ifa_flags(ifa);
1910 }
1911 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 return (0);
1913 }
1914 /* ok to use deprecated addresses? */
1915 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 /* skip unspecifed addresses */
1918 return (0);
1919 }
1920 if ( /* (local_scope == 0) && */
1921 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 return (0);
1923 }
1924 if ((site_scope == 0) &&
1925 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926 return (0);
1927 }
1928 } else {
1929 return (0);
1930 }
1931 break;
1932#endif
1933 default:
1934 return (0);
1935 }
1936 return (1);
1937}
1938
1939static struct mbuf *
1940sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941{
1942 struct sctp_paramhdr *parmh;
1943 struct mbuf *mret;
1944 int len;
1945
1946 if (ifa->address.sa.sa_family == AF_INET) {
1947 len = sizeof(struct sctp_ipv4addr_param);
1948 } else if (ifa->address.sa.sa_family == AF_INET6) {
1949 len = sizeof(struct sctp_ipv6addr_param);
1950 } else {
1951 /* unknown type */
1952 return (m);
1953 }
1954 if (M_TRAILINGSPACE(m) >= len) {
1955 /* easy side we just drop it on the end */
1956 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957 mret = m;
1958 } else {
1959 /* Need more space */
1960 mret = m;
1961 while (SCTP_BUF_NEXT(mret) != NULL) {
1962 mret = SCTP_BUF_NEXT(mret);
1963 }
1964 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 if (SCTP_BUF_NEXT(mret) == NULL) {
1966 /* We are hosed, can't add more addresses */
1967 return (m);
1968 }
1969 mret = SCTP_BUF_NEXT(mret);
1970 parmh = mtod(mret, struct sctp_paramhdr *);
1971 }
1972 /* now add the parameter */
1973 switch (ifa->address.sa.sa_family) {
1974 case AF_INET:
1975 {
1976 struct sctp_ipv4addr_param *ipv4p;
1977 struct sockaddr_in *sin;
1978
1979 sin = (struct sockaddr_in *)&ifa->address.sin;
1980 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 parmh->param_length = htons(len);
1983 ipv4p->addr = sin->sin_addr.s_addr;
1984 SCTP_BUF_LEN(mret) += len;
1985 break;
1986 }
1987#ifdef INET6
1988 case AF_INET6:
1989 {
1990 struct sctp_ipv6addr_param *ipv6p;
1991 struct sockaddr_in6 *sin6;
1992
1993 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 parmh->param_length = htons(len);
1997 memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 sizeof(ipv6p->addr));
1999 /* clear embedded scope in the address */
2000 in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 SCTP_BUF_LEN(mret) += len;
2002 break;
2003 }
2004#endif
2005 default:
2006 return (m);
2007 }
2008 return (mret);
2009}
2010
2011
2012struct mbuf *
2013sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014 struct mbuf *m_at, int cnt_inits_to)
2015{
2016 struct sctp_vrf *vrf = NULL;
2017 int cnt, limit_out = 0, total_count;
2018 uint32_t vrf_id;
2019
2020 vrf_id = inp->def_vrf_id;
2021 SCTP_IPI_ADDR_RLOCK();
2022 vrf = sctp_find_vrf(vrf_id);
2023 if (vrf == NULL) {
2024 SCTP_IPI_ADDR_RUNLOCK();
2025 return (m_at);
2026 }
2027 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 struct sctp_ifa *sctp_ifap;
2029 struct sctp_ifn *sctp_ifnp;
2030
2031 cnt = cnt_inits_to;
2032 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 limit_out = 1;
2034 cnt = SCTP_ADDRESS_LIMIT;
2035 goto skip_count;
2036 }
2037 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 if ((scope->loopback_scope == 0) &&
2039 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 /*
2041 * Skip loopback devices if loopback_scope
2042 * not set
2043 */
2044 continue;
2045 }
2046 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 if (sctp_is_address_in_scope(sctp_ifap,
2048 scope->ipv4_addr_legal,
2049 scope->ipv6_addr_legal,
2050 scope->loopback_scope,
2051 scope->ipv4_local_scope,
2052 scope->local_scope,
2053 scope->site_scope, 1) == 0) {
2054 continue;
2055 }
2056 cnt++;
2057 if (cnt > SCTP_ADDRESS_LIMIT) {
2058 break;
2059 }
2060 }
2061 if (cnt > SCTP_ADDRESS_LIMIT) {
2062 break;
2063 }
2064 }
2065skip_count:
2066 if (cnt > 1) {
2067 total_count = 0;
2068 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 cnt = 0;
2070 if ((scope->loopback_scope == 0) &&
2071 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 /*
2073 * Skip loopback devices if
2074 * loopback_scope not set
2075 */
2076 continue;
2077 }
2078 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 if (sctp_is_address_in_scope(sctp_ifap,
2080 scope->ipv4_addr_legal,
2081 scope->ipv6_addr_legal,
2082 scope->loopback_scope,
2083 scope->ipv4_local_scope,
2084 scope->local_scope,
2085 scope->site_scope, 0) == 0) {
2086 continue;
2087 }
2088 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089 if (limit_out) {
2090 cnt++;
2091 total_count++;
2092 if (cnt >= 2) {
2093 /*
2094 * two from each
2095 * address
2096 */
2097 break;
2098 }
2099 if (total_count > SCTP_ADDRESS_LIMIT) {
2100 /* No more addresses */
2101 break;
2102 }
2103 }
2104 }
2105 }
2106 }
2107 } else {
2108 struct sctp_laddr *laddr;
2109
2110 cnt = cnt_inits_to;
2111 /* First, how many ? */
2112 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 if (laddr->ifa == NULL) {
2114 continue;
2115 }
2116 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 /*
2118 * Address being deleted by the system, dont
2119 * list.
2120 */
2121 continue;
2122 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 /*
2124 * Address being deleted on this ep don't
2125 * list.
2126 */
2127 continue;
2128 }
2129 if (sctp_is_address_in_scope(laddr->ifa,
2130 scope->ipv4_addr_legal,
2131 scope->ipv6_addr_legal,
2132 scope->loopback_scope,
2133 scope->ipv4_local_scope,
2134 scope->local_scope,
2135 scope->site_scope, 1) == 0) {
2136 continue;
2137 }
2138 cnt++;
2139 }
2140 if (cnt > SCTP_ADDRESS_LIMIT) {
2141 limit_out = 1;
2142 }
2143 /*
2144 * To get through a NAT we only list addresses if we have
2145 * more than one. That way if you just bind a single address
2146 * we let the source of the init dictate our address.
2147 */
2148 if (cnt > 1) {
2149 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 cnt = 0;
2151 if (laddr->ifa == NULL) {
2152 continue;
2153 }
2154 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155 continue;
2156
2157 if (sctp_is_address_in_scope(laddr->ifa,
2158 scope->ipv4_addr_legal,
2159 scope->ipv6_addr_legal,
2160 scope->loopback_scope,
2161 scope->ipv4_local_scope,
2162 scope->local_scope,
2163 scope->site_scope, 0) == 0) {
2164 continue;
2165 }
2166 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 cnt++;
2168 if (cnt >= SCTP_ADDRESS_LIMIT) {
2169 break;
2170 }
2171 }
2172 }
2173 }
2174 SCTP_IPI_ADDR_RUNLOCK();
2175 return (m_at);
2176}
2177
2178static struct sctp_ifa *
2179sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180 uint8_t dest_is_loop,
2181 uint8_t dest_is_priv,
2182 sa_family_t fam)
2183{
2184 uint8_t dest_is_global = 0;
2185
2186 /* dest_is_priv is true if destination is a private address */
2187 /* dest_is_loop is true if destination is a loopback addresses */
2188
2189 /*
2190 * Here we determine if its a preferred address. A preferred address
2191 * means it is the same scope or higher scope then the destination.
2192 * L = loopback, P = private, G = global
2193 * ----------------------------------------- src | dest | result
2194 * ---------------------------------------- L | L | yes
2195 * ----------------------------------------- P | L |
2196 * yes-v4 no-v6 ----------------------------------------- G |
2197 * L | yes-v4 no-v6 ----------------------------------------- L
2198 * | P | no ----------------------------------------- P |
2199 * P | yes ----------------------------------------- G |
2200 * P | no ----------------------------------------- L | G
2201 * | no ----------------------------------------- P | G |
2202 * no ----------------------------------------- G | G |
2203 * yes -----------------------------------------
2204 */
2205
2206 if (ifa->address.sa.sa_family != fam) {
2207 /* forget mis-matched family */
2208 return (NULL);
2209 }
2210 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2211 dest_is_global = 1;
2212 }
2213 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2214 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2215 /* Ok the address may be ok */
2216 if (fam == AF_INET6) {
2217 /* ok to use deprecated addresses? no lets not! */
2218 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2219 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2220 return (NULL);
2221 }
2222 if (ifa->src_is_priv && !ifa->src_is_loop) {
2223 if (dest_is_loop) {
2224 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2225 return (NULL);
2226 }
2227 }
2228 if (ifa->src_is_glob) {
2229 if (dest_is_loop) {
2230 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2231 return (NULL);
2232 }
2233 }
2234 }
2235 /*
2236 * Now that we know what is what, implement or table this could in
2237 * theory be done slicker (it used to be), but this is
2238 * straightforward and easier to validate :-)
2239 */
2240 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2241 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2242 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2243 dest_is_loop, dest_is_priv, dest_is_global);
2244
2245 if ((ifa->src_is_loop) && (dest_is_priv)) {
2246 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2247 return (NULL);
2248 }
2249 if ((ifa->src_is_glob) && (dest_is_priv)) {
2250 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2251 return (NULL);
2252 }
2253 if ((ifa->src_is_loop) && (dest_is_global)) {
2254 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2255 return (NULL);
2256 }
2257 if ((ifa->src_is_priv) && (dest_is_global)) {
2258 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2259 return (NULL);
2260 }
2261 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2262 /* its a preferred address */
2263 return (ifa);
2264}
2265
2266static struct sctp_ifa *
2267sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2268 uint8_t dest_is_loop,
2269 uint8_t dest_is_priv,
2270 sa_family_t fam)
2271{
2272 uint8_t dest_is_global = 0;
2273
2274
2275 /*
2276 * Here we determine if its a acceptable address. A acceptable
2277 * address means it is the same scope or higher scope but we can
2278 * allow for NAT which means its ok to have a global dest and a
2279 * private src.
2280 *
2281 * L = loopback, P = private, G = global
2282 * ----------------------------------------- src | dest | result
2283 * ----------------------------------------- L | L | yes
2284 * ----------------------------------------- P | L |
2285 * yes-v4 no-v6 ----------------------------------------- G |
2286 * L | yes ----------------------------------------- L |
2287 * P | no ----------------------------------------- P | P
2288 * | yes ----------------------------------------- G | P
2289 * | yes - May not work -----------------------------------------
2290 * L | G | no ----------------------------------------- P
2291 * | G | yes - May not work
2292 * ----------------------------------------- G | G | yes
2293 * -----------------------------------------
2294 */
2295
2296 if (ifa->address.sa.sa_family != fam) {
2297 /* forget non matching family */
2298 return (NULL);
2299 }
2300 /* Ok the address may be ok */
2301 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2302 dest_is_global = 1;
2303 }
2304 if (fam == AF_INET6) {
2305 /* ok to use deprecated addresses? */
2306 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2307 return (NULL);
2308 }
2309 if (ifa->src_is_priv) {
2310 /* Special case, linklocal to loop */
2311 if (dest_is_loop)
2312 return (NULL);
2313 }
2314 }
2315 /*
2316 * Now that we know what is what, implement our table. This could in
2317 * theory be done slicker (it used to be), but this is
2318 * straightforward and easier to validate :-)
2319 */
2320 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2321 return (NULL);
2322 }
2323 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2324 return (NULL);
2325 }
2326 /* its an acceptable address */
2327 return (ifa);
2328}
2329
2330int
2331sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2332{
2333 struct sctp_laddr *laddr;
2334
2335 if (stcb == NULL) {
2336 /* There are no restrictions, no TCB :-) */
2337 return (0);
2338 }
2339 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2340 if (laddr->ifa == NULL) {
2341 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2342 __FUNCTION__);
2343 continue;
2344 }
2345 if (laddr->ifa == ifa) {
2346 /* Yes it is on the list */
2347 return (1);
2348 }
2349 }
2350 return (0);
2351}
2352
2353
2354int
2355sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2356{
2357 struct sctp_laddr *laddr;
2358
2359 if (ifa == NULL)
2360 return (0);
2361 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2362 if (laddr->ifa == NULL) {
2363 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2364 __FUNCTION__);
2365 continue;
2366 }
2367 if ((laddr->ifa == ifa) && laddr->action == 0)
2368 /* same pointer */
2369 return (1);
2370 }
2371 return (0);
2372}
2373
2374
2375
2376static struct sctp_ifa *
2377sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2378 sctp_route_t * ro,
2379 uint32_t vrf_id,
2380 int non_asoc_addr_ok,
2381 uint8_t dest_is_priv,
2382 uint8_t dest_is_loop,
2383 sa_family_t fam)
2384{
2385 struct sctp_laddr *laddr, *starting_point;
2386 void *ifn;
2387 int resettotop = 0;
2388 struct sctp_ifn *sctp_ifn;
2389 struct sctp_ifa *sctp_ifa, *sifa;
2390 struct sctp_vrf *vrf;
2391 uint32_t ifn_index;
2392
2393 vrf = sctp_find_vrf(vrf_id);
2394 if (vrf == NULL)
2395 return (NULL);
2396
2397 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2398 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2399 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2400 /*
2401 * first question, is the ifn we will emit on in our list, if so, we
2402 * want such an address. Note that we first looked for a preferred
2403 * address.
2404 */
2405 if (sctp_ifn) {
2406 /* is a preferred one on the interface we route out? */
2407 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2408 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2409 (non_asoc_addr_ok == 0))
2410 continue;
2411 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2412 dest_is_loop,
2413 dest_is_priv, fam);
2414 if (sifa == NULL)
2415 continue;
2416 if (sctp_is_addr_in_ep(inp, sifa)) {
2417 atomic_add_int(&sifa->refcount, 1);
2418 return (sifa);
2419 }
2420 }
2421 }
2422 /*
2423 * ok, now we now need to find one on the list of the addresses. We
2424 * can't get one on the emitting interface so let's find first a
2425 * preferred one. If not that an acceptable one otherwise... we
2426 * return NULL.
2427 */
2428 starting_point = inp->next_addr_touse;
2429once_again:
2430 if (inp->next_addr_touse == NULL) {
2431 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2432 resettotop = 1;
2433 }
2434 for (laddr = inp->next_addr_touse; laddr;
2435 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2436 if (laddr->ifa == NULL) {
2437 /* address has been removed */
2438 continue;
2439 }
2440 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2441 /* address is being deleted */
2442 continue;
2443 }
2444 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2445 dest_is_priv, fam);
2446 if (sifa == NULL)
2447 continue;
2448 atomic_add_int(&sifa->refcount, 1);
2449 return (sifa);
2450 }
2451 if (resettotop == 0) {
2452 inp->next_addr_touse = NULL;
2453 goto once_again;
2454 }
2455 inp->next_addr_touse = starting_point;
2456 resettotop = 0;
2457once_again_too:
2458 if (inp->next_addr_touse == NULL) {
2459 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2460 resettotop = 1;
2461 }
2462 /* ok, what about an acceptable address in the inp */
2463 for (laddr = inp->next_addr_touse; laddr;
2464 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2465 if (laddr->ifa == NULL) {
2466 /* address has been removed */
2467 continue;
2468 }
2469 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2470 /* address is being deleted */
2471 continue;
2472 }
2473 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2474 dest_is_priv, fam);
2475 if (sifa == NULL)
2476 continue;
2477 atomic_add_int(&sifa->refcount, 1);
2478 return (sifa);
2479 }
2480 if (resettotop == 0) {
2481 inp->next_addr_touse = NULL;
2482 goto once_again_too;
2483 }
2484 /*
2485 * no address bound can be a source for the destination we are in
2486 * trouble
2487 */
2488 return (NULL);
2489}
2490
2491
2492
2493static struct sctp_ifa *
2494sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2495 struct sctp_tcb *stcb,
2496 struct sctp_nets *net,
2497 sctp_route_t * ro,
2498 uint32_t vrf_id,
2499 uint8_t dest_is_priv,
2500 uint8_t dest_is_loop,
2501 int non_asoc_addr_ok,
2502 sa_family_t fam)
2503{
2504 struct sctp_laddr *laddr, *starting_point;
2505 void *ifn;
2506 struct sctp_ifn *sctp_ifn;
2507 struct sctp_ifa *sctp_ifa, *sifa;
2508 uint8_t start_at_beginning = 0;
2509 struct sctp_vrf *vrf;
2510 uint32_t ifn_index;
2511
2512 /*
2513 * first question, is the ifn we will emit on in our list, if so, we
2514 * want that one.
2515 */
2516 vrf = sctp_find_vrf(vrf_id);
2517 if (vrf == NULL)
2518 return (NULL);
2519
2520 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2521 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2522 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2523
2524 /*
2525 * first question, is the ifn we will emit on in our list? If so,
2526 * we want that one. First we look for a preferred. Second, we go
2527 * for an acceptable.
2528 */
2529 if (sctp_ifn) {
2530 /* first try for a preferred address on the ep */
2531 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2532 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2533 continue;
2534 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2535 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2536 if (sifa == NULL)
2537 continue;
2538 if (((non_asoc_addr_ok == 0) &&
2539 (sctp_is_addr_restricted(stcb, sifa))) ||
2540 (non_asoc_addr_ok &&
2541 (sctp_is_addr_restricted(stcb, sifa)) &&
2542 (!sctp_is_addr_pending(stcb, sifa)))) {
2543 /* on the no-no list */
2544 continue;
2545 }
2546 atomic_add_int(&sifa->refcount, 1);
2547 return (sifa);
2548 }
2549 }
2550 /* next try for an acceptable address on the ep */
2551 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2552 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2553 continue;
2554 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2555 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2556 if (sifa == NULL)
2557 continue;
2558 if (((non_asoc_addr_ok == 0) &&
2559 (sctp_is_addr_restricted(stcb, sifa))) ||
2560 (non_asoc_addr_ok &&
2561 (sctp_is_addr_restricted(stcb, sifa)) &&
2562 (!sctp_is_addr_pending(stcb, sifa)))) {
2563 /* on the no-no list */
2564 continue;
2565 }
2566 atomic_add_int(&sifa->refcount, 1);
2567 return (sifa);
2568 }
2569 }
2570
2571 }
2572 /*
2573 * if we can't find one like that then we must look at all addresses
2574 * bound to pick one at first preferable then secondly acceptable.
2575 */
2576 starting_point = stcb->asoc.last_used_address;
2577sctp_from_the_top:
2578 if (stcb->asoc.last_used_address == NULL) {
2579 start_at_beginning = 1;
2580 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2581 }
2582 /* search beginning with the last used address */
2583 for (laddr = stcb->asoc.last_used_address; laddr;
2584 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2585 if (laddr->ifa == NULL) {
2586 /* address has been removed */
2587 continue;
2588 }
2589 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2590 /* address is being deleted */
2591 continue;
2592 }
2593 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2594 if (sifa == NULL)
2595 continue;
2596 if (((non_asoc_addr_ok == 0) &&
2597 (sctp_is_addr_restricted(stcb, sifa))) ||
2598 (non_asoc_addr_ok &&
2599 (sctp_is_addr_restricted(stcb, sifa)) &&
2600 (!sctp_is_addr_pending(stcb, sifa)))) {
2601 /* on the no-no list */
2602 continue;
2603 }
2604 stcb->asoc.last_used_address = laddr;
2605 atomic_add_int(&sifa->refcount, 1);
2606 return (sifa);
2607 }
2608 if (start_at_beginning == 0) {
2609 stcb->asoc.last_used_address = NULL;
2610 goto sctp_from_the_top;
2611 }
2612 /* now try for any higher scope than the destination */
2613 stcb->asoc.last_used_address = starting_point;
2614 start_at_beginning = 0;
2615sctp_from_the_top2:
2616 if (stcb->asoc.last_used_address == NULL) {
2617 start_at_beginning = 1;
2618 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2619 }
2620 /* search beginning with the last used address */
2621 for (laddr = stcb->asoc.last_used_address; laddr;
2622 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2623 if (laddr->ifa == NULL) {
2624 /* address has been removed */
2625 continue;
2626 }
2627 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2628 /* address is being deleted */
2629 continue;
2630 }
2631 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2632 dest_is_priv, fam);
2633 if (sifa == NULL)
2634 continue;
2635 if (((non_asoc_addr_ok == 0) &&
2636 (sctp_is_addr_restricted(stcb, sifa))) ||
2637 (non_asoc_addr_ok &&
2638 (sctp_is_addr_restricted(stcb, sifa)) &&
2639 (!sctp_is_addr_pending(stcb, sifa)))) {
2640 /* on the no-no list */
2641 continue;
2642 }
2643 stcb->asoc.last_used_address = laddr;
2644 atomic_add_int(&sifa->refcount, 1);
2645 return (sifa);
2646 }
2647 if (start_at_beginning == 0) {
2648 stcb->asoc.last_used_address = NULL;
2649 goto sctp_from_the_top2;
2650 }
2651 return (NULL);
2652}
2653
2654static struct sctp_ifa *
2655sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2656 struct sctp_tcb *stcb,
2657 int non_asoc_addr_ok,
2658 uint8_t dest_is_loop,
2659 uint8_t dest_is_priv,
2660 int addr_wanted,
2661 sa_family_t fam,
2662 sctp_route_t * ro
2663)
2664{
2665 struct sctp_ifa *ifa, *sifa;
2666 int num_eligible_addr = 0;
2667
2668#ifdef INET6
2669 struct sockaddr_in6 sin6, lsa6;
2670
2671 if (fam == AF_INET6) {
2672 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2673 (void)sa6_recoverscope(&sin6);
2674 }
2675#endif /* INET6 */
2676 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2677 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2678 (non_asoc_addr_ok == 0))
2679 continue;
2680 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2681 dest_is_priv, fam);
2682 if (sifa == NULL)
2683 continue;
2684#ifdef INET6
2685 if (fam == AF_INET6 &&
2686 dest_is_loop &&
2687 sifa->src_is_loop && sifa->src_is_priv) {
2688 /*
2689 * don't allow fe80::1 to be a src on loop ::1, we
2690 * don't list it to the peer so we will get an
2691 * abort.
2692 */
2693 continue;
2694 }
2695 if (fam == AF_INET6 &&
2696 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2697 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2698 /*
2699 * link-local <-> link-local must belong to the same
2700 * scope.
2701 */
2702 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2703 (void)sa6_recoverscope(&lsa6);
2704 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2705 continue;
2706 }
2707 }
2708#endif /* INET6 */
2709
2710 /*
2711 * Check if the IPv6 address matches to next-hop. In the
2712 * mobile case, old IPv6 address may be not deleted from the
2713 * interface. Then, the interface has previous and new
2714 * addresses. We should use one corresponding to the
2715 * next-hop. (by micchie)
2716 */
2717#ifdef INET6
2718 if (stcb && fam == AF_INET6 &&
2719 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2720 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2721 == 0) {
2722 continue;
2723 }
2724 }
2725#endif
2726 /* Avoid topologically incorrect IPv4 address */
2727 if (stcb && fam == AF_INET &&
2728 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2730 continue;
2731 }
2732 }
2733 if (stcb) {
2734 if (((non_asoc_addr_ok == 0) &&
2735 (sctp_is_addr_restricted(stcb, sifa))) ||
2736 (non_asoc_addr_ok &&
2737 (sctp_is_addr_restricted(stcb, sifa)) &&
2738 (!sctp_is_addr_pending(stcb, sifa)))) {
2739 /*
2740 * It is restricted for some reason..
2741 * probably not yet added.
2742 */
2743 continue;
2744 }
2745 }
2746 if (num_eligible_addr >= addr_wanted) {
2747 return (sifa);
2748 }
2749 num_eligible_addr++;
2750 }
2751 return (NULL);
2752}
2753
2754
2755static int
2756sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2757 struct sctp_tcb *stcb,
2758 int non_asoc_addr_ok,
2759 uint8_t dest_is_loop,
2760 uint8_t dest_is_priv,
2761 sa_family_t fam)
2762{
2763 struct sctp_ifa *ifa, *sifa;
2764 int num_eligible_addr = 0;
2765
2766 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2767 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2768 (non_asoc_addr_ok == 0)) {
2769 continue;
2770 }
2771 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2772 dest_is_priv, fam);
2773 if (sifa == NULL) {
2774 continue;
2775 }
2776 if (stcb) {
2777 if (((non_asoc_addr_ok == 0) &&
2778 (sctp_is_addr_restricted(stcb, sifa))) ||
2779 (non_asoc_addr_ok &&
2780 (sctp_is_addr_restricted(stcb, sifa)) &&
2781 (!sctp_is_addr_pending(stcb, sifa)))) {
2782 /*
2783 * It is restricted for some reason..
2784 * probably not yet added.
2785 */
2786 continue;
2787 }
2788 }
2789 num_eligible_addr++;
2790 }
2791 return (num_eligible_addr);
2792}
2793
2794static struct sctp_ifa *
2795sctp_choose_boundall(struct sctp_inpcb *inp,
2796 struct sctp_tcb *stcb,
2797 struct sctp_nets *net,
2798 sctp_route_t * ro,
2799 uint32_t vrf_id,
2800 uint8_t dest_is_priv,
2801 uint8_t dest_is_loop,
2802 int non_asoc_addr_ok,
2803 sa_family_t fam)
2804{
2805 int cur_addr_num = 0, num_preferred = 0;
2806 void *ifn;
2807 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2808 struct sctp_ifa *sctp_ifa, *sifa;
2809 uint32_t ifn_index;
2810 struct sctp_vrf *vrf;
2811
2812 /*-
2813 * For boundall we can use any address in the association.
2814 * If non_asoc_addr_ok is set we can use any address (at least in
2815 * theory). So we look for preferred addresses first. If we find one,
2816 * we use it. Otherwise we next try to get an address on the
2817 * interface, which we should be able to do (unless non_asoc_addr_ok
2818 * is false and we are routed out that way). In these cases where we
2819 * can't use the address of the interface we go through all the
2820 * ifn's looking for an address we can use and fill that in. Punting
2821 * means we send back address 0, which will probably cause problems
2822 * actually since then IP will fill in the address of the route ifn,
2823 * which means we probably already rejected it.. i.e. here comes an
2824 * abort :-<.
2825 */
2826 vrf = sctp_find_vrf(vrf_id);
2827 if (vrf == NULL)
2828 return (NULL);
2829
2830 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2831 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2832 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2833 if (sctp_ifn == NULL) {
2834 /* ?? We don't have this guy ?? */
2835 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2836 goto bound_all_plan_b;
2837 }
2838 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2839 ifn_index, sctp_ifn->ifn_name);
2840
2841 if (net) {
2842 cur_addr_num = net->indx_of_eligible_next_to_use;
2843 }
2844 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2845 stcb,
2846 non_asoc_addr_ok,
2847 dest_is_loop,
2848 dest_is_priv, fam);
2849 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2850 num_preferred, sctp_ifn->ifn_name);
2851 if (num_preferred == 0) {
2852 /*
2853 * no eligible addresses, we must use some other interface
2854 * address if we can find one.
2855 */
2856 goto bound_all_plan_b;
2857 }
2858 /*
2859 * Ok we have num_eligible_addr set with how many we can use, this
2860 * may vary from call to call due to addresses being deprecated
2861 * etc..
2862 */
2863 if (cur_addr_num >= num_preferred) {
2864 cur_addr_num = 0;
2865 }
2866 /*
2867 * select the nth address from the list (where cur_addr_num is the
2868 * nth) and 0 is the first one, 1 is the second one etc...
2869 */
2870 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2871
2872 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2873 dest_is_priv, cur_addr_num, fam, ro);
2874
2875 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2876 if (sctp_ifa) {
2877 atomic_add_int(&sctp_ifa->refcount, 1);
2878 if (net) {
2879 /* save off where the next one we will want */
2880 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2881 }
2882 return (sctp_ifa);
2883 }
2884 /*
2885 * plan_b: Look at all interfaces and find a preferred address. If
2886 * no preferred fall through to plan_c.
2887 */
2888bound_all_plan_b:
2889 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2890 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2891 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2892 sctp_ifn->ifn_name);
2893 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2894 /* wrong base scope */
2895 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2896 continue;
2897 }
2898 if ((sctp_ifn == looked_at) && looked_at) {
2899 /* already looked at this guy */
2900 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2901 continue;
2902 }
2903 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2904 dest_is_loop, dest_is_priv, fam);
2905 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2906 "Found ifn:%p %d preferred source addresses\n",
2907 ifn, num_preferred);
2908 if (num_preferred == 0) {
2909 /* None on this interface. */
2910 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2911 continue;
2912 }
2913 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2914 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2915 num_preferred, sctp_ifn, cur_addr_num);
2916
2917 /*
2918 * Ok we have num_eligible_addr set with how many we can
2919 * use, this may vary from call to call due to addresses
2920 * being deprecated etc..
2921 */
2922 if (cur_addr_num >= num_preferred) {
2923 cur_addr_num = 0;
2924 }
2925 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2926 dest_is_priv, cur_addr_num, fam, ro);
2927 if (sifa == NULL)
2928 continue;
2929 if (net) {
2930 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2931 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2932 cur_addr_num);
2933 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2934 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2935 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2936 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2937 }
2938 atomic_add_int(&sifa->refcount, 1);
2939 return (sifa);
2940
2941 }
2942
2943 /* plan_c: do we have an acceptable address on the emit interface */
2944 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2945 if (emit_ifn == NULL) {
2946 goto plan_d;
2947 }
2948 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2949 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2950 (non_asoc_addr_ok == 0))
2951 continue;
2952 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2953 dest_is_priv, fam);
2954 if (sifa == NULL)
2955 continue;
2956 if (stcb) {
2957 if (((non_asoc_addr_ok == 0) &&
2958 (sctp_is_addr_restricted(stcb, sifa))) ||
2959 (non_asoc_addr_ok &&
2960 (sctp_is_addr_restricted(stcb, sifa)) &&
2961 (!sctp_is_addr_pending(stcb, sifa)))) {
2962 /*
2963 * It is restricted for some reason..
2964 * probably not yet added.
2965 */
2966 continue;
2967 }
2968 }
2969 atomic_add_int(&sifa->refcount, 1);
2970 return (sifa);
2971 }
2972plan_d:
2973 /*
2974 * plan_d: We are in trouble. No preferred address on the emit
2975 * interface. And not even a preferred address on all interfaces. Go
2976 * out and see if we can find an acceptable address somewhere
2977 * amongst all interfaces.
2978 */
2979 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2980 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2981 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2982 /* wrong base scope */
2983 continue;
2984 }
2985 if ((sctp_ifn == looked_at) && looked_at)
2986 /* already looked at this guy */
2987 continue;
2988
2989 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2990 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2991 (non_asoc_addr_ok == 0))
2992 continue;
2993 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2994 dest_is_loop,
2995 dest_is_priv, fam);
2996 if (sifa == NULL)
2997 continue;
2998 if (stcb) {
2999 if (((non_asoc_addr_ok == 0) &&
3000 (sctp_is_addr_restricted(stcb, sifa))) ||
3001 (non_asoc_addr_ok &&
3002 (sctp_is_addr_restricted(stcb, sifa)) &&
3003 (!sctp_is_addr_pending(stcb, sifa)))) {
3004 /*
3005 * It is restricted for some
3006 * reason.. probably not yet added.
3007 */
3008 continue;
3009 }
3010 }
3011 atomic_add_int(&sifa->refcount, 1);
3012 return (sifa);
3013 }
3014 }
3015 /*
3016 * Ok we can find NO address to source from that is not on our
3017 * restricted list and non_asoc_address is NOT ok, or it is on our
3018 * restricted list. We can't source to it :-(
3019 */
3020 return (NULL);
3021}
3022
3023
3024
3025/* tcb may be NULL */
3026struct sctp_ifa *
3027sctp_source_address_selection(struct sctp_inpcb *inp,
3028 struct sctp_tcb *stcb,
3029 sctp_route_t * ro,
3030 struct sctp_nets *net,
3031 int non_asoc_addr_ok, uint32_t vrf_id)
3032{
3033 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3034
3035#ifdef INET6
3036 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3037
3038#endif
3039 struct sctp_ifa *answer;
3040 uint8_t dest_is_priv, dest_is_loop;
3041 sa_family_t fam;
3042
3043 /*-
3044 * Rules: - Find the route if needed, cache if I can. - Look at
3045 * interface address in route, Is it in the bound list. If so we
3046 * have the best source. - If not we must rotate amongst the
3047 * addresses.
3048 *
3049 * Cavets and issues
3050 *
3051 * Do we need to pay attention to scope. We can have a private address
3052 * or a global address we are sourcing or sending to. So if we draw
3053 * it out
3054 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3055 * For V4
3056 *------------------------------------------
3057 * source * dest * result
3058 * -----------------------------------------
3059 * <a> Private * Global * NAT
3060 * -----------------------------------------
3061 * <b> Private * Private * No problem
3062 * -----------------------------------------
3063 * <c> Global * Private * Huh, How will this work?
3064 * -----------------------------------------
3065 * <d> Global * Global * No Problem
3066 *------------------------------------------
3067 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3068 * For V6
3069 *------------------------------------------
3070 * source * dest * result
3071 * -----------------------------------------
3072 * <a> Linklocal * Global *
3073 * -----------------------------------------
3074 * <b> Linklocal * Linklocal * No problem
3075 * -----------------------------------------
3076 * <c> Global * Linklocal * Huh, How will this work?
3077 * -----------------------------------------
3078 * <d> Global * Global * No Problem
3079 *------------------------------------------
3080 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3081 *
3082 * And then we add to that what happens if there are multiple addresses
3083 * assigned to an interface. Remember the ifa on a ifn is a linked
3084 * list of addresses. So one interface can have more than one IP
3085 * address. What happens if we have both a private and a global
3086 * address? Do we then use context of destination to sort out which
3087 * one is best? And what about NAT's sending P->G may get you a NAT
3088 * translation, or should you select the G thats on the interface in
3089 * preference.
3090 *
3091 * Decisions:
3092 *
3093 * - count the number of addresses on the interface.
3094 * - if it is one, no problem except case <c>.
3095 * For <a> we will assume a NAT out there.
3096 * - if there are more than one, then we need to worry about scope P
3097 * or G. We should prefer G -> G and P -> P if possible.
3098 * Then as a secondary fall back to mixed types G->P being a last
3099 * ditch one.
3100 * - The above all works for bound all, but bound specific we need to
3101 * use the same concept but instead only consider the bound
3102 * addresses. If the bound set is NOT assigned to the interface then
3103 * we must use rotation amongst the bound addresses..
3104 */
3105 if (ro->ro_rt == NULL) {
3106 /*
3107 * Need a route to cache.
3108 */
3109 SCTP_RTALLOC(ro, vrf_id);
3110 }
3111 if (ro->ro_rt == NULL) {
3112 return (NULL);
3113 }
3114 fam = to->sin_family;
3115 dest_is_priv = dest_is_loop = 0;
3116 /* Setup our scopes for the destination */
3117 switch (fam) {
3118 case AF_INET:
3119 /* Scope based on outbound address */
3120 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3121 dest_is_loop = 1;
3122 if (net != NULL) {
3123 /* mark it as local */
3124 net->addr_is_local = 1;
3125 }
3126 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3127 dest_is_priv = 1;
3128 }
3129 break;
3130#ifdef INET6
3131 case AF_INET6:
3132 /* Scope based on outbound address */
3133 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3134 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3135 /*
3136 * If the address is a loopback address, which
3137 * consists of "::1" OR "fe80::1%lo0", we are
3138 * loopback scope. But we don't use dest_is_priv
3139 * (link local addresses).
3140 */
3141 dest_is_loop = 1;
3142 if (net != NULL) {
3143 /* mark it as local */
3144 net->addr_is_local = 1;
3145 }
3146 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3147 dest_is_priv = 1;
3148 }
3149 break;
3150#endif
3151 }
3152 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3153 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3154 SCTP_IPI_ADDR_RLOCK();
3155 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3156 /*
3157 * Bound all case
3158 */
3159 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3160 dest_is_priv, dest_is_loop,
3161 non_asoc_addr_ok, fam);
3162 SCTP_IPI_ADDR_RUNLOCK();
3163 return (answer);
3164 }
3165 /*
3166 * Subset bound case
3167 */
3168 if (stcb) {
3169 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3170 vrf_id, dest_is_priv,
3171 dest_is_loop,
3172 non_asoc_addr_ok, fam);
3173 } else {
3174 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3175 non_asoc_addr_ok,
3176 dest_is_priv,
3177 dest_is_loop, fam);
3178 }
3179 SCTP_IPI_ADDR_RUNLOCK();
3180 return (answer);
3181}
3182
3183static int
3184sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3185{
3186 struct cmsghdr cmh;
3187 int tlen, at;
3188
3189 tlen = SCTP_BUF_LEN(control);
3190 at = 0;
3191 /*
3192 * Independent of how many mbufs, find the c_type inside the control
3193 * structure and copy out the data.
3194 */
3195 while (at < tlen) {
3196 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3197 /* not enough room for one more we are done. */
3198 return (0);
3199 }
3200 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3201 if (((int)cmh.cmsg_len + at) > tlen) {
3202 /*
3203 * this is real messed up since there is not enough
3204 * data here to cover the cmsg header. We are done.
3205 */
3206 return (0);
3207 }
3208 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3209 (c_type == cmh.cmsg_type)) {
3210 /* found the one we want, copy it out */
3211 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3212 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3213 /*
3214 * space of cmsg_len after header not big
3215 * enough
3216 */
3217 return (0);
3218 }
3219 m_copydata(control, at, cpsize, data);
3220 return (1);
3221 } else {
3222 at += CMSG_ALIGN(cmh.cmsg_len);
3223 if (cmh.cmsg_len == 0) {
3224 break;
3225 }
3226 }
3227 }
3228 /* not found */
3229 return (0);
3230}
3231
3232static struct mbuf *
3233sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3234 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3235{
3236 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3237 struct sctp_state_cookie *stc;
3238 struct sctp_paramhdr *ph;
3239 uint8_t *foo;
3240 int sig_offset;
3241 uint16_t cookie_sz;
3242
3243 mret = NULL;
3244 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3245 sizeof(struct sctp_paramhdr)), 0,
3246 M_DONTWAIT, 1, MT_DATA);
3247 if (mret == NULL) {
3248 return (NULL);
3249 }
3250 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3251 if (copy_init == NULL) {
3252 sctp_m_freem(mret);
3253 return (NULL);
3254 }
3255#ifdef SCTP_MBUF_LOGGING
3256 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3257 struct mbuf *mat;
3258
3259 mat = copy_init;
3260 while (mat) {
3261 if (SCTP_BUF_IS_EXTENDED(mat)) {
3262 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3263 }
3264 mat = SCTP_BUF_NEXT(mat);
3265 }
3266 }
3267#endif
3268 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3269 M_DONTWAIT);
3270 if (copy_initack == NULL) {
3271 sctp_m_freem(mret);
3272 sctp_m_freem(copy_init);
3273 return (NULL);
3274 }
3275#ifdef SCTP_MBUF_LOGGING
3276 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3277 struct mbuf *mat;
3278
3279 mat = copy_initack;
3280 while (mat) {
3281 if (SCTP_BUF_IS_EXTENDED(mat)) {
3282 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3283 }
3284 mat = SCTP_BUF_NEXT(mat);
3285 }
3286 }
3287#endif
3288 /* easy side we just drop it on the end */
3289 ph = mtod(mret, struct sctp_paramhdr *);
3290 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3291 sizeof(struct sctp_paramhdr);
3292 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3293 sizeof(struct sctp_paramhdr));
3294 ph->param_type = htons(SCTP_STATE_COOKIE);
3295 ph->param_length = 0; /* fill in at the end */
3296 /* Fill in the stc cookie data */
3297 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3298
3299 /* tack the INIT and then the INIT-ACK onto the chain */
3300 cookie_sz = 0;
3301 m_at = mret;
3302 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3303 cookie_sz += SCTP_BUF_LEN(m_at);
3304 if (SCTP_BUF_NEXT(m_at) == NULL) {
3305 SCTP_BUF_NEXT(m_at) = copy_init;
3306 break;
3307 }
3308 }
3309
3310 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3311 cookie_sz += SCTP_BUF_LEN(m_at);
3312 if (SCTP_BUF_NEXT(m_at) == NULL) {
3313 SCTP_BUF_NEXT(m_at) = copy_initack;
3314 break;
3315 }
3316 }
3317
3318 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3319 cookie_sz += SCTP_BUF_LEN(m_at);
3320 if (SCTP_BUF_NEXT(m_at) == NULL) {
3321 break;
3322 }
3323 }
3324 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3325 if (sig == NULL) {
3326 /* no space, so free the entire chain */
3327 sctp_m_freem(mret);
3328 return (NULL);
3329 }
3330 SCTP_BUF_LEN(sig) = 0;
3331 SCTP_BUF_NEXT(m_at) = sig;
3332 sig_offset = 0;
3333 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3334 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3335 *signature = foo;
3336 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3337 cookie_sz += SCTP_SIGNATURE_SIZE;
3338 ph->param_length = htons(cookie_sz);
3339 return (mret);
3340}
3341
3342
3343static uint8_t
3344sctp_get_ect(struct sctp_tcb *stcb,
3345 struct sctp_tmit_chunk *chk)
3346{
3347 uint8_t this_random;
3348
3349 /* Huh? */
3350 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3351 return (0);
3352
3353 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3354 /* no nonce, always return ECT0 */
3355 return (SCTP_ECT0_BIT);
3356
3357 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3358 /* Peer does NOT support it, so we send a ECT0 only */
3359 return (SCTP_ECT0_BIT);
3360 }
3361 if (chk == NULL)
3362 return (SCTP_ECT0_BIT);
3363
3364 if ((stcb->asoc.hb_random_idx > 3) ||
3365 ((stcb->asoc.hb_random_idx == 3) &&
3366 (stcb->asoc.hb_ect_randombit > 7))) {
3367 uint32_t rndval;
3368
3369warp_drive_sa:
3370 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3371 memcpy(stcb->asoc.hb_random_values, &rndval,
3372 sizeof(stcb->asoc.hb_random_values));
3373 this_random = stcb->asoc.hb_random_values[0];
3374 stcb->asoc.hb_random_idx = 0;
3375 stcb->asoc.hb_ect_randombit = 0;
3376 } else {
3377 if (stcb->asoc.hb_ect_randombit > 7) {
3378 stcb->asoc.hb_ect_randombit = 0;
3379 stcb->asoc.hb_random_idx++;
3380 if (stcb->asoc.hb_random_idx > 3) {
3381 goto warp_drive_sa;
3382 }
3383 }
3384 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3385 }
3386 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3387 if (chk != NULL)
3388 /* ECN Nonce stuff */
3389 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3390 stcb->asoc.hb_ect_randombit++;
3391 return (SCTP_ECT1_BIT);
3392 } else {
3393 stcb->asoc.hb_ect_randombit++;
3394 return (SCTP_ECT0_BIT);
3395 }
3396}
3397
3398static int
3399sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3400 struct sctp_tcb *stcb, /* may be NULL */
3401 struct sctp_nets *net,
3402 struct sockaddr *to,
3403 struct mbuf *m,
3404 uint32_t auth_offset,
3405 struct sctp_auth_chunk *auth,
3406 uint16_t auth_keyid,
3407 int nofragment_flag,
3408 int ecn_ok,
3409 struct sctp_tmit_chunk *chk,
3410 int out_of_asoc_ok,
3411 uint16_t src_port,
3412 uint16_t dest_port,
3413 uint32_t v_tag,
3414 uint16_t port,
3415 int so_locked,
3416#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3417 SCTP_UNUSED
3418#endif
3419 union sctp_sockstore *over_addr
3420)
3421/* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3422{
3423 /*
3424 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3425 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3426 * structure: - fill in the HMAC digest of any AUTH chunk in the
3427 * packet. - calculate and fill in the SCTP checksum. - prepend an
3428 * IP address header. - if boundall use INADDR_ANY. - if
3429 * boundspecific do source address selection. - set fragmentation
3430 * option for ipV4. - On return from IP output, check/adjust mtu
3431 * size of output interface and smallest_mtu size as well.
3432 */
3433 /* Will need ifdefs around this */
3434 struct mbuf *o_pak;
3435 struct mbuf *newm;
3436 struct sctphdr *sctphdr;
3437 int packet_length;
3438 int ret;
3439 uint32_t vrf_id;
3440 sctp_route_t *ro = NULL;
3441 struct udphdr *udp = NULL;
3442
3443#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3444 struct socket *so = NULL;
3445
3446#endif
3447
3448 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3449 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3450 sctp_m_freem(m);
3451 return (EFAULT);
3452 }
3453 if (stcb) {
3454 vrf_id = stcb->asoc.vrf_id;
3455 } else {
3456 vrf_id = inp->def_vrf_id;
3457 }
3458
3459 /* fill in the HMAC digest for any AUTH chunk in the packet */
3460 if ((auth != NULL) && (stcb != NULL)) {
3461 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3462 }
3463 if (to->sa_family == AF_INET) {
3464 struct ip *ip = NULL;
3465 sctp_route_t iproute;
3466 uint8_t tos_value;
3467 int len;
3468
3469 len = sizeof(struct ip) + sizeof(struct sctphdr);
3470 if (port) {
3471 len += sizeof(struct udphdr);
3472 }
3473 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3474 if (newm == NULL) {
3475 sctp_m_freem(m);
3476 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3477 return (ENOMEM);
3478 }
3479 SCTP_ALIGN_TO_END(newm, len);
3480 SCTP_BUF_LEN(newm) = len;
3481 SCTP_BUF_NEXT(newm) = m;
3482 m = newm;
3483 packet_length = sctp_calculate_len(m);
3484 ip = mtod(m, struct ip *);
3485 ip->ip_v = IPVERSION;
3486 ip->ip_hl = (sizeof(struct ip) >> 2);
3487 if (net) {
3488 tos_value = net->tos_flowlabel & 0x000000ff;
3489 } else {
3490 tos_value = inp->ip_inp.inp.inp_ip_tos;
3491 }
3492 if ((nofragment_flag) && (port == 0)) {
3493#if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3494 ip->ip_off = IP_DF;
3495#else
3496 ip->ip_off = htons(IP_DF);
3497#endif
3498 } else
3499 ip->ip_off = 0;
3500
3501 /* FreeBSD has a function for ip_id's */
3502 ip->ip_id = ip_newid();
3503
3504 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3505 ip->ip_len = packet_length;
3506 if (stcb) {
3507 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3508 /* Enable ECN */
3509 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3510 } else {
3511 /* No ECN */
3512 ip->ip_tos = (u_char)(tos_value & 0xfc);
3513 }
3514 } else {
3515 /* no association at all */
3516 ip->ip_tos = (tos_value & 0xfc);
3517 }
3518 if (port) {
3519 ip->ip_p = IPPROTO_UDP;
3520 } else {
3521 ip->ip_p = IPPROTO_SCTP;
3522 }
3523 ip->ip_sum = 0;
3524 if (net == NULL) {
3525 ro = &iproute;
3526 memset(&iproute, 0, sizeof(iproute));
3527 memcpy(&ro->ro_dst, to, to->sa_len);
3528 } else {
3529 ro = (sctp_route_t *) & net->ro;
3530 }
3531 /* Now the address selection part */
3532 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3533
3534 /* call the routine to select the src address */
3535 if (net && out_of_asoc_ok == 0) {
3536 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3537 sctp_free_ifa(net->ro._s_addr);
3538 net->ro._s_addr = NULL;
3539 net->src_addr_selected = 0;
3540 if (ro->ro_rt) {
3541 RTFREE(ro->ro_rt);
3542 ro->ro_rt = NULL;
3543 }
3544 }
3545 if (net->src_addr_selected == 0) {
3546 /* Cache the source address */
3547 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3548 ro, net, 0,
3549 vrf_id);
3550 net->src_addr_selected = 1;
3551 }
3552 if (net->ro._s_addr == NULL) {
3553 /* No route to host */
3554 net->src_addr_selected = 0;
3555 goto no_route;
3556 }
3557 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3558 } else {
3559 if (over_addr == NULL) {
3560 struct sctp_ifa *_lsrc;
3561
3562 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3563 net,
3564 out_of_asoc_ok,
3565 vrf_id);
3566 if (_lsrc == NULL) {
3567 goto no_route;
3568 }
3569 ip->ip_src = _lsrc->address.sin.sin_addr;
3570 sctp_free_ifa(_lsrc);
3571 } else {
3572 ip->ip_src = over_addr->sin.sin_addr;
3573 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3574 }
3575 }
3576 if (port) {
3577 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3578 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3579 udp->uh_dport = port;
3580 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3581 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3582 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3583 } else {
3584 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3585 }
3586
3587 sctphdr->src_port = src_port;
3588 sctphdr->dest_port = dest_port;
3589 sctphdr->v_tag = v_tag;
3590 sctphdr->checksum = 0;
3591
3592 /*
3593 * If source address selection fails and we find no route
3594 * then the ip_output should fail as well with a
3595 * NO_ROUTE_TO_HOST type error. We probably should catch
3596 * that somewhere and abort the association right away
3597 * (assuming this is an INIT being sent).
3598 */
3599 if ((ro->ro_rt == NULL)) {
3600 /*
3601 * src addr selection failed to find a route (or
3602 * valid source addr), so we can't get there from
3603 * here (yet)!
3604 */
3605 no_route:
3606 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3607 "%s: dropped packet - no valid source addr\n",
3608 __FUNCTION__);
3609 if (net) {
3610 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3611 "Destination was ");
3612 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3613 &net->ro._l_addr.sa);
3614 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3615 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3616 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3617 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3618 stcb,
3619 SCTP_FAILED_THRESHOLD,
3620 (void *)net,
3621 so_locked);
3622 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3623 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3624 /*
3625 * JRS 5/14/07 - If a
3626 * destination is
3627 * unreachable, the PF bit
3628 * is turned off. This
3629 * allows an unambiguous use
3630 * of the PF bit for
3631 * destinations that are
3632 * reachable but potentially
3633 * failed. If the
3634 * destination is set to the
3635 * unreachable state, also
3636 * set the destination to
3637 * the PF state.
3638 */
3639 /*
3640 * Add debug message here if
3641 * destination is not in PF
3642 * state.
3643 */
3644 /*
3645 * Stop any running T3
3646 * timers here?
3647 */
3648 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3649 net->dest_state &= ~SCTP_ADDR_PF;
3650 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3651 net);
3652 }
3653 }
3654 }
3655 if (stcb) {
3656 if (net == stcb->asoc.primary_destination) {
3657 /* need a new primary */
3658 struct sctp_nets *alt;
3659
3660 alt = sctp_find_alternate_net(stcb, net, 0);
3661 if (alt != net) {
3662 if (sctp_set_primary_addr(stcb,
3663 (struct sockaddr *)NULL,
3664 alt) == 0) {
3665 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3666 if (net->ro._s_addr) {
3667 sctp_free_ifa(net->ro._s_addr);
3668 net->ro._s_addr = NULL;
3669 }
3670 net->src_addr_selected = 0;
3671 }
3672 }
3673 }
3674 }
3675 }
3676 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3677 sctp_m_freem(m);
3678 return (EHOSTUNREACH);
3679 }
3680 if (ro != &iproute) {
3681 memcpy(&iproute, ro, sizeof(*ro));
3682 }
3683 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3684 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3685 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3686 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3687 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3688 ro->ro_rt);
3689
3690 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3691 /* failed to prepend data, give up */
3692 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3693 sctp_m_freem(m);
3694 return (ENOMEM);
3695 }
3696#ifdef SCTP_PACKET_LOGGING
3697 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3698 sctp_packet_log(m, packet_length);
3699#endif
3700 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3701 if (port) {
3702 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3703 (stcb) &&
3704 (stcb->asoc.loopback_scope))) {
3705 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3706 SCTP_STAT_INCR(sctps_sendswcrc);
3707 } else {
3708 SCTP_STAT_INCR(sctps_sendnocrc);
3709 }
3710 SCTP_ENABLE_UDP_CSUM(o_pak);
3711 } else {
3712 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3713 (stcb) &&
3714 (stcb->asoc.loopback_scope))) {
3715 m->m_pkthdr.csum_flags = CSUM_SCTP;
3716 m->m_pkthdr.csum_data = 0; /* FIXME MT */
3717 SCTP_STAT_INCR(sctps_sendhwcrc);
3718 } else {
3719 SCTP_STAT_INCR(sctps_sendnocrc);
3720 }
3721 }
3722 /* send it out. table id is taken from stcb */
3723#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 so = SCTP_INP_SO(inp);
3726 SCTP_SOCKET_UNLOCK(so, 0);
3727 }
3728#endif
3729 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3730#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3731 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3732 atomic_add_int(&stcb->asoc.refcnt, 1);
3733 SCTP_TCB_UNLOCK(stcb);
3734 SCTP_SOCKET_LOCK(so, 0);
3735 SCTP_TCB_LOCK(stcb);
3736 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3737 }
3738#endif
3739 SCTP_STAT_INCR(sctps_sendpackets);
3740 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3741 if (ret)
3742 SCTP_STAT_INCR(sctps_senderrors);
3743
3744 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3745 if (net == NULL) {
3746 /* free tempy routes */
3747 if (ro->ro_rt) {
3748 RTFREE(ro->ro_rt);
3749 ro->ro_rt = NULL;
3750 }
3751 } else {
3752 /* PMTU check versus smallest asoc MTU goes here */
3753 if ((ro->ro_rt != NULL) &&
3754 (net->ro._s_addr)) {
3755 uint32_t mtu;
3756
3757 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3758 if (net->port) {
3759 mtu -= sizeof(struct udphdr);
3760 }
3761 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3762#ifdef SCTP_PRINT_FOR_B_AND_M
3763 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3764#endif
3765 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3766 net->mtu = mtu;
3767 }
3768 } else if (ro->ro_rt == NULL) {
3769 /* route was freed */
3770 if (net->ro._s_addr &&
3771 net->src_addr_selected) {
3772 sctp_free_ifa(net->ro._s_addr);
3773 net->ro._s_addr = NULL;
3774 }
3775 net->src_addr_selected = 0;
3776 }
3777 }
3778 return (ret);
3779 }
3780#ifdef INET6
3781 else if (to->sa_family == AF_INET6) {
3782 uint32_t flowlabel;
3783 struct ip6_hdr *ip6h;
3784 struct route_in6 ip6route;
3785 struct ifnet *ifp;
3786 u_char flowTop;
3787 uint16_t flowBottom;
3788 u_char tosBottom, tosTop;
3789 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3790 int prev_scope = 0;
3791 struct sockaddr_in6 lsa6_storage;
3792 int error;
3793 u_short prev_port = 0;
3794 int len;
3795
3796 if (net != NULL) {
3797 flowlabel = net->tos_flowlabel;
3798 } else {
3799 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3800 }
3801
3802 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3803 if (port) {
3804 len += sizeof(struct udphdr);
3805 }
3806 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3807 if (newm == NULL) {
3808 sctp_m_freem(m);
3809 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3810 return (ENOMEM);
3811 }
3812 SCTP_ALIGN_TO_END(newm, len);
3813 SCTP_BUF_LEN(newm) = len;
3814 SCTP_BUF_NEXT(newm) = m;
3815 m = newm;
3816 packet_length = sctp_calculate_len(m);
3817
3818 ip6h = mtod(m, struct ip6_hdr *);
3819 /*
3820 * We assume here that inp_flow is in host byte order within
3821 * the TCB!
3822 */
3823 flowBottom = flowlabel & 0x0000ffff;
3824 flowTop = ((flowlabel & 0x000f0000) >> 16);
3825 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3826 /* protect *sin6 from overwrite */
3827 sin6 = (struct sockaddr_in6 *)to;
3828 tmp = *sin6;
3829 sin6 = &tmp;
3830
3831 /* KAME hack: embed scopeid */
35
36#include <netinet/sctp_os.h>
37#include <sys/proc.h>
38#include <netinet/sctp_var.h>
39#include <netinet/sctp_sysctl.h>
40#include <netinet/sctp_header.h>
41#include <netinet/sctp_pcb.h>
42#include <netinet/sctputil.h>
43#include <netinet/sctp_output.h>
44#include <netinet/sctp_uio.h>
45#include <netinet/sctputil.h>
46#include <netinet/sctp_auth.h>
47#include <netinet/sctp_timer.h>
48#include <netinet/sctp_asconf.h>
49#include <netinet/sctp_indata.h>
50#include <netinet/sctp_bsd_addr.h>
51#include <netinet/sctp_input.h>
52#include <netinet/sctp_crc32.h>
53#include <netinet/udp.h>
54#include <machine/in_cksum.h>
55
56
57
58#define SCTP_MAX_GAPS_INARRAY 4
59struct sack_track {
60 uint8_t right_edge; /* mergable on the right edge */
61 uint8_t left_edge; /* mergable on the left edge */
62 uint8_t num_entries;
63 uint8_t spare;
64 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65};
66
67struct sack_track sack_array[256] = {
68 {0, 0, 0, 0, /* 0x00 */
69 {{0, 0},
70 {0, 0},
71 {0, 0},
72 {0, 0}
73 }
74 },
75 {1, 0, 1, 0, /* 0x01 */
76 {{0, 0},
77 {0, 0},
78 {0, 0},
79 {0, 0}
80 }
81 },
82 {0, 0, 1, 0, /* 0x02 */
83 {{1, 1},
84 {0, 0},
85 {0, 0},
86 {0, 0}
87 }
88 },
89 {1, 0, 1, 0, /* 0x03 */
90 {{0, 1},
91 {0, 0},
92 {0, 0},
93 {0, 0}
94 }
95 },
96 {0, 0, 1, 0, /* 0x04 */
97 {{2, 2},
98 {0, 0},
99 {0, 0},
100 {0, 0}
101 }
102 },
103 {1, 0, 2, 0, /* 0x05 */
104 {{0, 0},
105 {2, 2},
106 {0, 0},
107 {0, 0}
108 }
109 },
110 {0, 0, 1, 0, /* 0x06 */
111 {{1, 2},
112 {0, 0},
113 {0, 0},
114 {0, 0}
115 }
116 },
117 {1, 0, 1, 0, /* 0x07 */
118 {{0, 2},
119 {0, 0},
120 {0, 0},
121 {0, 0}
122 }
123 },
124 {0, 0, 1, 0, /* 0x08 */
125 {{3, 3},
126 {0, 0},
127 {0, 0},
128 {0, 0}
129 }
130 },
131 {1, 0, 2, 0, /* 0x09 */
132 {{0, 0},
133 {3, 3},
134 {0, 0},
135 {0, 0}
136 }
137 },
138 {0, 0, 2, 0, /* 0x0a */
139 {{1, 1},
140 {3, 3},
141 {0, 0},
142 {0, 0}
143 }
144 },
145 {1, 0, 2, 0, /* 0x0b */
146 {{0, 1},
147 {3, 3},
148 {0, 0},
149 {0, 0}
150 }
151 },
152 {0, 0, 1, 0, /* 0x0c */
153 {{2, 3},
154 {0, 0},
155 {0, 0},
156 {0, 0}
157 }
158 },
159 {1, 0, 2, 0, /* 0x0d */
160 {{0, 0},
161 {2, 3},
162 {0, 0},
163 {0, 0}
164 }
165 },
166 {0, 0, 1, 0, /* 0x0e */
167 {{1, 3},
168 {0, 0},
169 {0, 0},
170 {0, 0}
171 }
172 },
173 {1, 0, 1, 0, /* 0x0f */
174 {{0, 3},
175 {0, 0},
176 {0, 0},
177 {0, 0}
178 }
179 },
180 {0, 0, 1, 0, /* 0x10 */
181 {{4, 4},
182 {0, 0},
183 {0, 0},
184 {0, 0}
185 }
186 },
187 {1, 0, 2, 0, /* 0x11 */
188 {{0, 0},
189 {4, 4},
190 {0, 0},
191 {0, 0}
192 }
193 },
194 {0, 0, 2, 0, /* 0x12 */
195 {{1, 1},
196 {4, 4},
197 {0, 0},
198 {0, 0}
199 }
200 },
201 {1, 0, 2, 0, /* 0x13 */
202 {{0, 1},
203 {4, 4},
204 {0, 0},
205 {0, 0}
206 }
207 },
208 {0, 0, 2, 0, /* 0x14 */
209 {{2, 2},
210 {4, 4},
211 {0, 0},
212 {0, 0}
213 }
214 },
215 {1, 0, 3, 0, /* 0x15 */
216 {{0, 0},
217 {2, 2},
218 {4, 4},
219 {0, 0}
220 }
221 },
222 {0, 0, 2, 0, /* 0x16 */
223 {{1, 2},
224 {4, 4},
225 {0, 0},
226 {0, 0}
227 }
228 },
229 {1, 0, 2, 0, /* 0x17 */
230 {{0, 2},
231 {4, 4},
232 {0, 0},
233 {0, 0}
234 }
235 },
236 {0, 0, 1, 0, /* 0x18 */
237 {{3, 4},
238 {0, 0},
239 {0, 0},
240 {0, 0}
241 }
242 },
243 {1, 0, 2, 0, /* 0x19 */
244 {{0, 0},
245 {3, 4},
246 {0, 0},
247 {0, 0}
248 }
249 },
250 {0, 0, 2, 0, /* 0x1a */
251 {{1, 1},
252 {3, 4},
253 {0, 0},
254 {0, 0}
255 }
256 },
257 {1, 0, 2, 0, /* 0x1b */
258 {{0, 1},
259 {3, 4},
260 {0, 0},
261 {0, 0}
262 }
263 },
264 {0, 0, 1, 0, /* 0x1c */
265 {{2, 4},
266 {0, 0},
267 {0, 0},
268 {0, 0}
269 }
270 },
271 {1, 0, 2, 0, /* 0x1d */
272 {{0, 0},
273 {2, 4},
274 {0, 0},
275 {0, 0}
276 }
277 },
278 {0, 0, 1, 0, /* 0x1e */
279 {{1, 4},
280 {0, 0},
281 {0, 0},
282 {0, 0}
283 }
284 },
285 {1, 0, 1, 0, /* 0x1f */
286 {{0, 4},
287 {0, 0},
288 {0, 0},
289 {0, 0}
290 }
291 },
292 {0, 0, 1, 0, /* 0x20 */
293 {{5, 5},
294 {0, 0},
295 {0, 0},
296 {0, 0}
297 }
298 },
299 {1, 0, 2, 0, /* 0x21 */
300 {{0, 0},
301 {5, 5},
302 {0, 0},
303 {0, 0}
304 }
305 },
306 {0, 0, 2, 0, /* 0x22 */
307 {{1, 1},
308 {5, 5},
309 {0, 0},
310 {0, 0}
311 }
312 },
313 {1, 0, 2, 0, /* 0x23 */
314 {{0, 1},
315 {5, 5},
316 {0, 0},
317 {0, 0}
318 }
319 },
320 {0, 0, 2, 0, /* 0x24 */
321 {{2, 2},
322 {5, 5},
323 {0, 0},
324 {0, 0}
325 }
326 },
327 {1, 0, 3, 0, /* 0x25 */
328 {{0, 0},
329 {2, 2},
330 {5, 5},
331 {0, 0}
332 }
333 },
334 {0, 0, 2, 0, /* 0x26 */
335 {{1, 2},
336 {5, 5},
337 {0, 0},
338 {0, 0}
339 }
340 },
341 {1, 0, 2, 0, /* 0x27 */
342 {{0, 2},
343 {5, 5},
344 {0, 0},
345 {0, 0}
346 }
347 },
348 {0, 0, 2, 0, /* 0x28 */
349 {{3, 3},
350 {5, 5},
351 {0, 0},
352 {0, 0}
353 }
354 },
355 {1, 0, 3, 0, /* 0x29 */
356 {{0, 0},
357 {3, 3},
358 {5, 5},
359 {0, 0}
360 }
361 },
362 {0, 0, 3, 0, /* 0x2a */
363 {{1, 1},
364 {3, 3},
365 {5, 5},
366 {0, 0}
367 }
368 },
369 {1, 0, 3, 0, /* 0x2b */
370 {{0, 1},
371 {3, 3},
372 {5, 5},
373 {0, 0}
374 }
375 },
376 {0, 0, 2, 0, /* 0x2c */
377 {{2, 3},
378 {5, 5},
379 {0, 0},
380 {0, 0}
381 }
382 },
383 {1, 0, 3, 0, /* 0x2d */
384 {{0, 0},
385 {2, 3},
386 {5, 5},
387 {0, 0}
388 }
389 },
390 {0, 0, 2, 0, /* 0x2e */
391 {{1, 3},
392 {5, 5},
393 {0, 0},
394 {0, 0}
395 }
396 },
397 {1, 0, 2, 0, /* 0x2f */
398 {{0, 3},
399 {5, 5},
400 {0, 0},
401 {0, 0}
402 }
403 },
404 {0, 0, 1, 0, /* 0x30 */
405 {{4, 5},
406 {0, 0},
407 {0, 0},
408 {0, 0}
409 }
410 },
411 {1, 0, 2, 0, /* 0x31 */
412 {{0, 0},
413 {4, 5},
414 {0, 0},
415 {0, 0}
416 }
417 },
418 {0, 0, 2, 0, /* 0x32 */
419 {{1, 1},
420 {4, 5},
421 {0, 0},
422 {0, 0}
423 }
424 },
425 {1, 0, 2, 0, /* 0x33 */
426 {{0, 1},
427 {4, 5},
428 {0, 0},
429 {0, 0}
430 }
431 },
432 {0, 0, 2, 0, /* 0x34 */
433 {{2, 2},
434 {4, 5},
435 {0, 0},
436 {0, 0}
437 }
438 },
439 {1, 0, 3, 0, /* 0x35 */
440 {{0, 0},
441 {2, 2},
442 {4, 5},
443 {0, 0}
444 }
445 },
446 {0, 0, 2, 0, /* 0x36 */
447 {{1, 2},
448 {4, 5},
449 {0, 0},
450 {0, 0}
451 }
452 },
453 {1, 0, 2, 0, /* 0x37 */
454 {{0, 2},
455 {4, 5},
456 {0, 0},
457 {0, 0}
458 }
459 },
460 {0, 0, 1, 0, /* 0x38 */
461 {{3, 5},
462 {0, 0},
463 {0, 0},
464 {0, 0}
465 }
466 },
467 {1, 0, 2, 0, /* 0x39 */
468 {{0, 0},
469 {3, 5},
470 {0, 0},
471 {0, 0}
472 }
473 },
474 {0, 0, 2, 0, /* 0x3a */
475 {{1, 1},
476 {3, 5},
477 {0, 0},
478 {0, 0}
479 }
480 },
481 {1, 0, 2, 0, /* 0x3b */
482 {{0, 1},
483 {3, 5},
484 {0, 0},
485 {0, 0}
486 }
487 },
488 {0, 0, 1, 0, /* 0x3c */
489 {{2, 5},
490 {0, 0},
491 {0, 0},
492 {0, 0}
493 }
494 },
495 {1, 0, 2, 0, /* 0x3d */
496 {{0, 0},
497 {2, 5},
498 {0, 0},
499 {0, 0}
500 }
501 },
502 {0, 0, 1, 0, /* 0x3e */
503 {{1, 5},
504 {0, 0},
505 {0, 0},
506 {0, 0}
507 }
508 },
509 {1, 0, 1, 0, /* 0x3f */
510 {{0, 5},
511 {0, 0},
512 {0, 0},
513 {0, 0}
514 }
515 },
516 {0, 0, 1, 0, /* 0x40 */
517 {{6, 6},
518 {0, 0},
519 {0, 0},
520 {0, 0}
521 }
522 },
523 {1, 0, 2, 0, /* 0x41 */
524 {{0, 0},
525 {6, 6},
526 {0, 0},
527 {0, 0}
528 }
529 },
530 {0, 0, 2, 0, /* 0x42 */
531 {{1, 1},
532 {6, 6},
533 {0, 0},
534 {0, 0}
535 }
536 },
537 {1, 0, 2, 0, /* 0x43 */
538 {{0, 1},
539 {6, 6},
540 {0, 0},
541 {0, 0}
542 }
543 },
544 {0, 0, 2, 0, /* 0x44 */
545 {{2, 2},
546 {6, 6},
547 {0, 0},
548 {0, 0}
549 }
550 },
551 {1, 0, 3, 0, /* 0x45 */
552 {{0, 0},
553 {2, 2},
554 {6, 6},
555 {0, 0}
556 }
557 },
558 {0, 0, 2, 0, /* 0x46 */
559 {{1, 2},
560 {6, 6},
561 {0, 0},
562 {0, 0}
563 }
564 },
565 {1, 0, 2, 0, /* 0x47 */
566 {{0, 2},
567 {6, 6},
568 {0, 0},
569 {0, 0}
570 }
571 },
572 {0, 0, 2, 0, /* 0x48 */
573 {{3, 3},
574 {6, 6},
575 {0, 0},
576 {0, 0}
577 }
578 },
579 {1, 0, 3, 0, /* 0x49 */
580 {{0, 0},
581 {3, 3},
582 {6, 6},
583 {0, 0}
584 }
585 },
586 {0, 0, 3, 0, /* 0x4a */
587 {{1, 1},
588 {3, 3},
589 {6, 6},
590 {0, 0}
591 }
592 },
593 {1, 0, 3, 0, /* 0x4b */
594 {{0, 1},
595 {3, 3},
596 {6, 6},
597 {0, 0}
598 }
599 },
600 {0, 0, 2, 0, /* 0x4c */
601 {{2, 3},
602 {6, 6},
603 {0, 0},
604 {0, 0}
605 }
606 },
607 {1, 0, 3, 0, /* 0x4d */
608 {{0, 0},
609 {2, 3},
610 {6, 6},
611 {0, 0}
612 }
613 },
614 {0, 0, 2, 0, /* 0x4e */
615 {{1, 3},
616 {6, 6},
617 {0, 0},
618 {0, 0}
619 }
620 },
621 {1, 0, 2, 0, /* 0x4f */
622 {{0, 3},
623 {6, 6},
624 {0, 0},
625 {0, 0}
626 }
627 },
628 {0, 0, 2, 0, /* 0x50 */
629 {{4, 4},
630 {6, 6},
631 {0, 0},
632 {0, 0}
633 }
634 },
635 {1, 0, 3, 0, /* 0x51 */
636 {{0, 0},
637 {4, 4},
638 {6, 6},
639 {0, 0}
640 }
641 },
642 {0, 0, 3, 0, /* 0x52 */
643 {{1, 1},
644 {4, 4},
645 {6, 6},
646 {0, 0}
647 }
648 },
649 {1, 0, 3, 0, /* 0x53 */
650 {{0, 1},
651 {4, 4},
652 {6, 6},
653 {0, 0}
654 }
655 },
656 {0, 0, 3, 0, /* 0x54 */
657 {{2, 2},
658 {4, 4},
659 {6, 6},
660 {0, 0}
661 }
662 },
663 {1, 0, 4, 0, /* 0x55 */
664 {{0, 0},
665 {2, 2},
666 {4, 4},
667 {6, 6}
668 }
669 },
670 {0, 0, 3, 0, /* 0x56 */
671 {{1, 2},
672 {4, 4},
673 {6, 6},
674 {0, 0}
675 }
676 },
677 {1, 0, 3, 0, /* 0x57 */
678 {{0, 2},
679 {4, 4},
680 {6, 6},
681 {0, 0}
682 }
683 },
684 {0, 0, 2, 0, /* 0x58 */
685 {{3, 4},
686 {6, 6},
687 {0, 0},
688 {0, 0}
689 }
690 },
691 {1, 0, 3, 0, /* 0x59 */
692 {{0, 0},
693 {3, 4},
694 {6, 6},
695 {0, 0}
696 }
697 },
698 {0, 0, 3, 0, /* 0x5a */
699 {{1, 1},
700 {3, 4},
701 {6, 6},
702 {0, 0}
703 }
704 },
705 {1, 0, 3, 0, /* 0x5b */
706 {{0, 1},
707 {3, 4},
708 {6, 6},
709 {0, 0}
710 }
711 },
712 {0, 0, 2, 0, /* 0x5c */
713 {{2, 4},
714 {6, 6},
715 {0, 0},
716 {0, 0}
717 }
718 },
719 {1, 0, 3, 0, /* 0x5d */
720 {{0, 0},
721 {2, 4},
722 {6, 6},
723 {0, 0}
724 }
725 },
726 {0, 0, 2, 0, /* 0x5e */
727 {{1, 4},
728 {6, 6},
729 {0, 0},
730 {0, 0}
731 }
732 },
733 {1, 0, 2, 0, /* 0x5f */
734 {{0, 4},
735 {6, 6},
736 {0, 0},
737 {0, 0}
738 }
739 },
740 {0, 0, 1, 0, /* 0x60 */
741 {{5, 6},
742 {0, 0},
743 {0, 0},
744 {0, 0}
745 }
746 },
747 {1, 0, 2, 0, /* 0x61 */
748 {{0, 0},
749 {5, 6},
750 {0, 0},
751 {0, 0}
752 }
753 },
754 {0, 0, 2, 0, /* 0x62 */
755 {{1, 1},
756 {5, 6},
757 {0, 0},
758 {0, 0}
759 }
760 },
761 {1, 0, 2, 0, /* 0x63 */
762 {{0, 1},
763 {5, 6},
764 {0, 0},
765 {0, 0}
766 }
767 },
768 {0, 0, 2, 0, /* 0x64 */
769 {{2, 2},
770 {5, 6},
771 {0, 0},
772 {0, 0}
773 }
774 },
775 {1, 0, 3, 0, /* 0x65 */
776 {{0, 0},
777 {2, 2},
778 {5, 6},
779 {0, 0}
780 }
781 },
782 {0, 0, 2, 0, /* 0x66 */
783 {{1, 2},
784 {5, 6},
785 {0, 0},
786 {0, 0}
787 }
788 },
789 {1, 0, 2, 0, /* 0x67 */
790 {{0, 2},
791 {5, 6},
792 {0, 0},
793 {0, 0}
794 }
795 },
796 {0, 0, 2, 0, /* 0x68 */
797 {{3, 3},
798 {5, 6},
799 {0, 0},
800 {0, 0}
801 }
802 },
803 {1, 0, 3, 0, /* 0x69 */
804 {{0, 0},
805 {3, 3},
806 {5, 6},
807 {0, 0}
808 }
809 },
810 {0, 0, 3, 0, /* 0x6a */
811 {{1, 1},
812 {3, 3},
813 {5, 6},
814 {0, 0}
815 }
816 },
817 {1, 0, 3, 0, /* 0x6b */
818 {{0, 1},
819 {3, 3},
820 {5, 6},
821 {0, 0}
822 }
823 },
824 {0, 0, 2, 0, /* 0x6c */
825 {{2, 3},
826 {5, 6},
827 {0, 0},
828 {0, 0}
829 }
830 },
831 {1, 0, 3, 0, /* 0x6d */
832 {{0, 0},
833 {2, 3},
834 {5, 6},
835 {0, 0}
836 }
837 },
838 {0, 0, 2, 0, /* 0x6e */
839 {{1, 3},
840 {5, 6},
841 {0, 0},
842 {0, 0}
843 }
844 },
845 {1, 0, 2, 0, /* 0x6f */
846 {{0, 3},
847 {5, 6},
848 {0, 0},
849 {0, 0}
850 }
851 },
852 {0, 0, 1, 0, /* 0x70 */
853 {{4, 6},
854 {0, 0},
855 {0, 0},
856 {0, 0}
857 }
858 },
859 {1, 0, 2, 0, /* 0x71 */
860 {{0, 0},
861 {4, 6},
862 {0, 0},
863 {0, 0}
864 }
865 },
866 {0, 0, 2, 0, /* 0x72 */
867 {{1, 1},
868 {4, 6},
869 {0, 0},
870 {0, 0}
871 }
872 },
873 {1, 0, 2, 0, /* 0x73 */
874 {{0, 1},
875 {4, 6},
876 {0, 0},
877 {0, 0}
878 }
879 },
880 {0, 0, 2, 0, /* 0x74 */
881 {{2, 2},
882 {4, 6},
883 {0, 0},
884 {0, 0}
885 }
886 },
887 {1, 0, 3, 0, /* 0x75 */
888 {{0, 0},
889 {2, 2},
890 {4, 6},
891 {0, 0}
892 }
893 },
894 {0, 0, 2, 0, /* 0x76 */
895 {{1, 2},
896 {4, 6},
897 {0, 0},
898 {0, 0}
899 }
900 },
901 {1, 0, 2, 0, /* 0x77 */
902 {{0, 2},
903 {4, 6},
904 {0, 0},
905 {0, 0}
906 }
907 },
908 {0, 0, 1, 0, /* 0x78 */
909 {{3, 6},
910 {0, 0},
911 {0, 0},
912 {0, 0}
913 }
914 },
915 {1, 0, 2, 0, /* 0x79 */
916 {{0, 0},
917 {3, 6},
918 {0, 0},
919 {0, 0}
920 }
921 },
922 {0, 0, 2, 0, /* 0x7a */
923 {{1, 1},
924 {3, 6},
925 {0, 0},
926 {0, 0}
927 }
928 },
929 {1, 0, 2, 0, /* 0x7b */
930 {{0, 1},
931 {3, 6},
932 {0, 0},
933 {0, 0}
934 }
935 },
936 {0, 0, 1, 0, /* 0x7c */
937 {{2, 6},
938 {0, 0},
939 {0, 0},
940 {0, 0}
941 }
942 },
943 {1, 0, 2, 0, /* 0x7d */
944 {{0, 0},
945 {2, 6},
946 {0, 0},
947 {0, 0}
948 }
949 },
950 {0, 0, 1, 0, /* 0x7e */
951 {{1, 6},
952 {0, 0},
953 {0, 0},
954 {0, 0}
955 }
956 },
957 {1, 0, 1, 0, /* 0x7f */
958 {{0, 6},
959 {0, 0},
960 {0, 0},
961 {0, 0}
962 }
963 },
964 {0, 1, 1, 0, /* 0x80 */
965 {{7, 7},
966 {0, 0},
967 {0, 0},
968 {0, 0}
969 }
970 },
971 {1, 1, 2, 0, /* 0x81 */
972 {{0, 0},
973 {7, 7},
974 {0, 0},
975 {0, 0}
976 }
977 },
978 {0, 1, 2, 0, /* 0x82 */
979 {{1, 1},
980 {7, 7},
981 {0, 0},
982 {0, 0}
983 }
984 },
985 {1, 1, 2, 0, /* 0x83 */
986 {{0, 1},
987 {7, 7},
988 {0, 0},
989 {0, 0}
990 }
991 },
992 {0, 1, 2, 0, /* 0x84 */
993 {{2, 2},
994 {7, 7},
995 {0, 0},
996 {0, 0}
997 }
998 },
999 {1, 1, 3, 0, /* 0x85 */
1000 {{0, 0},
1001 {2, 2},
1002 {7, 7},
1003 {0, 0}
1004 }
1005 },
1006 {0, 1, 2, 0, /* 0x86 */
1007 {{1, 2},
1008 {7, 7},
1009 {0, 0},
1010 {0, 0}
1011 }
1012 },
1013 {1, 1, 2, 0, /* 0x87 */
1014 {{0, 2},
1015 {7, 7},
1016 {0, 0},
1017 {0, 0}
1018 }
1019 },
1020 {0, 1, 2, 0, /* 0x88 */
1021 {{3, 3},
1022 {7, 7},
1023 {0, 0},
1024 {0, 0}
1025 }
1026 },
1027 {1, 1, 3, 0, /* 0x89 */
1028 {{0, 0},
1029 {3, 3},
1030 {7, 7},
1031 {0, 0}
1032 }
1033 },
1034 {0, 1, 3, 0, /* 0x8a */
1035 {{1, 1},
1036 {3, 3},
1037 {7, 7},
1038 {0, 0}
1039 }
1040 },
1041 {1, 1, 3, 0, /* 0x8b */
1042 {{0, 1},
1043 {3, 3},
1044 {7, 7},
1045 {0, 0}
1046 }
1047 },
1048 {0, 1, 2, 0, /* 0x8c */
1049 {{2, 3},
1050 {7, 7},
1051 {0, 0},
1052 {0, 0}
1053 }
1054 },
1055 {1, 1, 3, 0, /* 0x8d */
1056 {{0, 0},
1057 {2, 3},
1058 {7, 7},
1059 {0, 0}
1060 }
1061 },
1062 {0, 1, 2, 0, /* 0x8e */
1063 {{1, 3},
1064 {7, 7},
1065 {0, 0},
1066 {0, 0}
1067 }
1068 },
1069 {1, 1, 2, 0, /* 0x8f */
1070 {{0, 3},
1071 {7, 7},
1072 {0, 0},
1073 {0, 0}
1074 }
1075 },
1076 {0, 1, 2, 0, /* 0x90 */
1077 {{4, 4},
1078 {7, 7},
1079 {0, 0},
1080 {0, 0}
1081 }
1082 },
1083 {1, 1, 3, 0, /* 0x91 */
1084 {{0, 0},
1085 {4, 4},
1086 {7, 7},
1087 {0, 0}
1088 }
1089 },
1090 {0, 1, 3, 0, /* 0x92 */
1091 {{1, 1},
1092 {4, 4},
1093 {7, 7},
1094 {0, 0}
1095 }
1096 },
1097 {1, 1, 3, 0, /* 0x93 */
1098 {{0, 1},
1099 {4, 4},
1100 {7, 7},
1101 {0, 0}
1102 }
1103 },
1104 {0, 1, 3, 0, /* 0x94 */
1105 {{2, 2},
1106 {4, 4},
1107 {7, 7},
1108 {0, 0}
1109 }
1110 },
1111 {1, 1, 4, 0, /* 0x95 */
1112 {{0, 0},
1113 {2, 2},
1114 {4, 4},
1115 {7, 7}
1116 }
1117 },
1118 {0, 1, 3, 0, /* 0x96 */
1119 {{1, 2},
1120 {4, 4},
1121 {7, 7},
1122 {0, 0}
1123 }
1124 },
1125 {1, 1, 3, 0, /* 0x97 */
1126 {{0, 2},
1127 {4, 4},
1128 {7, 7},
1129 {0, 0}
1130 }
1131 },
1132 {0, 1, 2, 0, /* 0x98 */
1133 {{3, 4},
1134 {7, 7},
1135 {0, 0},
1136 {0, 0}
1137 }
1138 },
1139 {1, 1, 3, 0, /* 0x99 */
1140 {{0, 0},
1141 {3, 4},
1142 {7, 7},
1143 {0, 0}
1144 }
1145 },
1146 {0, 1, 3, 0, /* 0x9a */
1147 {{1, 1},
1148 {3, 4},
1149 {7, 7},
1150 {0, 0}
1151 }
1152 },
1153 {1, 1, 3, 0, /* 0x9b */
1154 {{0, 1},
1155 {3, 4},
1156 {7, 7},
1157 {0, 0}
1158 }
1159 },
1160 {0, 1, 2, 0, /* 0x9c */
1161 {{2, 4},
1162 {7, 7},
1163 {0, 0},
1164 {0, 0}
1165 }
1166 },
1167 {1, 1, 3, 0, /* 0x9d */
1168 {{0, 0},
1169 {2, 4},
1170 {7, 7},
1171 {0, 0}
1172 }
1173 },
1174 {0, 1, 2, 0, /* 0x9e */
1175 {{1, 4},
1176 {7, 7},
1177 {0, 0},
1178 {0, 0}
1179 }
1180 },
1181 {1, 1, 2, 0, /* 0x9f */
1182 {{0, 4},
1183 {7, 7},
1184 {0, 0},
1185 {0, 0}
1186 }
1187 },
1188 {0, 1, 2, 0, /* 0xa0 */
1189 {{5, 5},
1190 {7, 7},
1191 {0, 0},
1192 {0, 0}
1193 }
1194 },
1195 {1, 1, 3, 0, /* 0xa1 */
1196 {{0, 0},
1197 {5, 5},
1198 {7, 7},
1199 {0, 0}
1200 }
1201 },
1202 {0, 1, 3, 0, /* 0xa2 */
1203 {{1, 1},
1204 {5, 5},
1205 {7, 7},
1206 {0, 0}
1207 }
1208 },
1209 {1, 1, 3, 0, /* 0xa3 */
1210 {{0, 1},
1211 {5, 5},
1212 {7, 7},
1213 {0, 0}
1214 }
1215 },
1216 {0, 1, 3, 0, /* 0xa4 */
1217 {{2, 2},
1218 {5, 5},
1219 {7, 7},
1220 {0, 0}
1221 }
1222 },
1223 {1, 1, 4, 0, /* 0xa5 */
1224 {{0, 0},
1225 {2, 2},
1226 {5, 5},
1227 {7, 7}
1228 }
1229 },
1230 {0, 1, 3, 0, /* 0xa6 */
1231 {{1, 2},
1232 {5, 5},
1233 {7, 7},
1234 {0, 0}
1235 }
1236 },
1237 {1, 1, 3, 0, /* 0xa7 */
1238 {{0, 2},
1239 {5, 5},
1240 {7, 7},
1241 {0, 0}
1242 }
1243 },
1244 {0, 1, 3, 0, /* 0xa8 */
1245 {{3, 3},
1246 {5, 5},
1247 {7, 7},
1248 {0, 0}
1249 }
1250 },
1251 {1, 1, 4, 0, /* 0xa9 */
1252 {{0, 0},
1253 {3, 3},
1254 {5, 5},
1255 {7, 7}
1256 }
1257 },
1258 {0, 1, 4, 0, /* 0xaa */
1259 {{1, 1},
1260 {3, 3},
1261 {5, 5},
1262 {7, 7}
1263 }
1264 },
1265 {1, 1, 4, 0, /* 0xab */
1266 {{0, 1},
1267 {3, 3},
1268 {5, 5},
1269 {7, 7}
1270 }
1271 },
1272 {0, 1, 3, 0, /* 0xac */
1273 {{2, 3},
1274 {5, 5},
1275 {7, 7},
1276 {0, 0}
1277 }
1278 },
1279 {1, 1, 4, 0, /* 0xad */
1280 {{0, 0},
1281 {2, 3},
1282 {5, 5},
1283 {7, 7}
1284 }
1285 },
1286 {0, 1, 3, 0, /* 0xae */
1287 {{1, 3},
1288 {5, 5},
1289 {7, 7},
1290 {0, 0}
1291 }
1292 },
1293 {1, 1, 3, 0, /* 0xaf */
1294 {{0, 3},
1295 {5, 5},
1296 {7, 7},
1297 {0, 0}
1298 }
1299 },
1300 {0, 1, 2, 0, /* 0xb0 */
1301 {{4, 5},
1302 {7, 7},
1303 {0, 0},
1304 {0, 0}
1305 }
1306 },
1307 {1, 1, 3, 0, /* 0xb1 */
1308 {{0, 0},
1309 {4, 5},
1310 {7, 7},
1311 {0, 0}
1312 }
1313 },
1314 {0, 1, 3, 0, /* 0xb2 */
1315 {{1, 1},
1316 {4, 5},
1317 {7, 7},
1318 {0, 0}
1319 }
1320 },
1321 {1, 1, 3, 0, /* 0xb3 */
1322 {{0, 1},
1323 {4, 5},
1324 {7, 7},
1325 {0, 0}
1326 }
1327 },
1328 {0, 1, 3, 0, /* 0xb4 */
1329 {{2, 2},
1330 {4, 5},
1331 {7, 7},
1332 {0, 0}
1333 }
1334 },
1335 {1, 1, 4, 0, /* 0xb5 */
1336 {{0, 0},
1337 {2, 2},
1338 {4, 5},
1339 {7, 7}
1340 }
1341 },
1342 {0, 1, 3, 0, /* 0xb6 */
1343 {{1, 2},
1344 {4, 5},
1345 {7, 7},
1346 {0, 0}
1347 }
1348 },
1349 {1, 1, 3, 0, /* 0xb7 */
1350 {{0, 2},
1351 {4, 5},
1352 {7, 7},
1353 {0, 0}
1354 }
1355 },
1356 {0, 1, 2, 0, /* 0xb8 */
1357 {{3, 5},
1358 {7, 7},
1359 {0, 0},
1360 {0, 0}
1361 }
1362 },
1363 {1, 1, 3, 0, /* 0xb9 */
1364 {{0, 0},
1365 {3, 5},
1366 {7, 7},
1367 {0, 0}
1368 }
1369 },
1370 {0, 1, 3, 0, /* 0xba */
1371 {{1, 1},
1372 {3, 5},
1373 {7, 7},
1374 {0, 0}
1375 }
1376 },
1377 {1, 1, 3, 0, /* 0xbb */
1378 {{0, 1},
1379 {3, 5},
1380 {7, 7},
1381 {0, 0}
1382 }
1383 },
1384 {0, 1, 2, 0, /* 0xbc */
1385 {{2, 5},
1386 {7, 7},
1387 {0, 0},
1388 {0, 0}
1389 }
1390 },
1391 {1, 1, 3, 0, /* 0xbd */
1392 {{0, 0},
1393 {2, 5},
1394 {7, 7},
1395 {0, 0}
1396 }
1397 },
1398 {0, 1, 2, 0, /* 0xbe */
1399 {{1, 5},
1400 {7, 7},
1401 {0, 0},
1402 {0, 0}
1403 }
1404 },
1405 {1, 1, 2, 0, /* 0xbf */
1406 {{0, 5},
1407 {7, 7},
1408 {0, 0},
1409 {0, 0}
1410 }
1411 },
1412 {0, 1, 1, 0, /* 0xc0 */
1413 {{6, 7},
1414 {0, 0},
1415 {0, 0},
1416 {0, 0}
1417 }
1418 },
1419 {1, 1, 2, 0, /* 0xc1 */
1420 {{0, 0},
1421 {6, 7},
1422 {0, 0},
1423 {0, 0}
1424 }
1425 },
1426 {0, 1, 2, 0, /* 0xc2 */
1427 {{1, 1},
1428 {6, 7},
1429 {0, 0},
1430 {0, 0}
1431 }
1432 },
1433 {1, 1, 2, 0, /* 0xc3 */
1434 {{0, 1},
1435 {6, 7},
1436 {0, 0},
1437 {0, 0}
1438 }
1439 },
1440 {0, 1, 2, 0, /* 0xc4 */
1441 {{2, 2},
1442 {6, 7},
1443 {0, 0},
1444 {0, 0}
1445 }
1446 },
1447 {1, 1, 3, 0, /* 0xc5 */
1448 {{0, 0},
1449 {2, 2},
1450 {6, 7},
1451 {0, 0}
1452 }
1453 },
1454 {0, 1, 2, 0, /* 0xc6 */
1455 {{1, 2},
1456 {6, 7},
1457 {0, 0},
1458 {0, 0}
1459 }
1460 },
1461 {1, 1, 2, 0, /* 0xc7 */
1462 {{0, 2},
1463 {6, 7},
1464 {0, 0},
1465 {0, 0}
1466 }
1467 },
1468 {0, 1, 2, 0, /* 0xc8 */
1469 {{3, 3},
1470 {6, 7},
1471 {0, 0},
1472 {0, 0}
1473 }
1474 },
1475 {1, 1, 3, 0, /* 0xc9 */
1476 {{0, 0},
1477 {3, 3},
1478 {6, 7},
1479 {0, 0}
1480 }
1481 },
1482 {0, 1, 3, 0, /* 0xca */
1483 {{1, 1},
1484 {3, 3},
1485 {6, 7},
1486 {0, 0}
1487 }
1488 },
1489 {1, 1, 3, 0, /* 0xcb */
1490 {{0, 1},
1491 {3, 3},
1492 {6, 7},
1493 {0, 0}
1494 }
1495 },
1496 {0, 1, 2, 0, /* 0xcc */
1497 {{2, 3},
1498 {6, 7},
1499 {0, 0},
1500 {0, 0}
1501 }
1502 },
1503 {1, 1, 3, 0, /* 0xcd */
1504 {{0, 0},
1505 {2, 3},
1506 {6, 7},
1507 {0, 0}
1508 }
1509 },
1510 {0, 1, 2, 0, /* 0xce */
1511 {{1, 3},
1512 {6, 7},
1513 {0, 0},
1514 {0, 0}
1515 }
1516 },
1517 {1, 1, 2, 0, /* 0xcf */
1518 {{0, 3},
1519 {6, 7},
1520 {0, 0},
1521 {0, 0}
1522 }
1523 },
1524 {0, 1, 2, 0, /* 0xd0 */
1525 {{4, 4},
1526 {6, 7},
1527 {0, 0},
1528 {0, 0}
1529 }
1530 },
1531 {1, 1, 3, 0, /* 0xd1 */
1532 {{0, 0},
1533 {4, 4},
1534 {6, 7},
1535 {0, 0}
1536 }
1537 },
1538 {0, 1, 3, 0, /* 0xd2 */
1539 {{1, 1},
1540 {4, 4},
1541 {6, 7},
1542 {0, 0}
1543 }
1544 },
1545 {1, 1, 3, 0, /* 0xd3 */
1546 {{0, 1},
1547 {4, 4},
1548 {6, 7},
1549 {0, 0}
1550 }
1551 },
1552 {0, 1, 3, 0, /* 0xd4 */
1553 {{2, 2},
1554 {4, 4},
1555 {6, 7},
1556 {0, 0}
1557 }
1558 },
1559 {1, 1, 4, 0, /* 0xd5 */
1560 {{0, 0},
1561 {2, 2},
1562 {4, 4},
1563 {6, 7}
1564 }
1565 },
1566 {0, 1, 3, 0, /* 0xd6 */
1567 {{1, 2},
1568 {4, 4},
1569 {6, 7},
1570 {0, 0}
1571 }
1572 },
1573 {1, 1, 3, 0, /* 0xd7 */
1574 {{0, 2},
1575 {4, 4},
1576 {6, 7},
1577 {0, 0}
1578 }
1579 },
1580 {0, 1, 2, 0, /* 0xd8 */
1581 {{3, 4},
1582 {6, 7},
1583 {0, 0},
1584 {0, 0}
1585 }
1586 },
1587 {1, 1, 3, 0, /* 0xd9 */
1588 {{0, 0},
1589 {3, 4},
1590 {6, 7},
1591 {0, 0}
1592 }
1593 },
1594 {0, 1, 3, 0, /* 0xda */
1595 {{1, 1},
1596 {3, 4},
1597 {6, 7},
1598 {0, 0}
1599 }
1600 },
1601 {1, 1, 3, 0, /* 0xdb */
1602 {{0, 1},
1603 {3, 4},
1604 {6, 7},
1605 {0, 0}
1606 }
1607 },
1608 {0, 1, 2, 0, /* 0xdc */
1609 {{2, 4},
1610 {6, 7},
1611 {0, 0},
1612 {0, 0}
1613 }
1614 },
1615 {1, 1, 3, 0, /* 0xdd */
1616 {{0, 0},
1617 {2, 4},
1618 {6, 7},
1619 {0, 0}
1620 }
1621 },
1622 {0, 1, 2, 0, /* 0xde */
1623 {{1, 4},
1624 {6, 7},
1625 {0, 0},
1626 {0, 0}
1627 }
1628 },
1629 {1, 1, 2, 0, /* 0xdf */
1630 {{0, 4},
1631 {6, 7},
1632 {0, 0},
1633 {0, 0}
1634 }
1635 },
1636 {0, 1, 1, 0, /* 0xe0 */
1637 {{5, 7},
1638 {0, 0},
1639 {0, 0},
1640 {0, 0}
1641 }
1642 },
1643 {1, 1, 2, 0, /* 0xe1 */
1644 {{0, 0},
1645 {5, 7},
1646 {0, 0},
1647 {0, 0}
1648 }
1649 },
1650 {0, 1, 2, 0, /* 0xe2 */
1651 {{1, 1},
1652 {5, 7},
1653 {0, 0},
1654 {0, 0}
1655 }
1656 },
1657 {1, 1, 2, 0, /* 0xe3 */
1658 {{0, 1},
1659 {5, 7},
1660 {0, 0},
1661 {0, 0}
1662 }
1663 },
1664 {0, 1, 2, 0, /* 0xe4 */
1665 {{2, 2},
1666 {5, 7},
1667 {0, 0},
1668 {0, 0}
1669 }
1670 },
1671 {1, 1, 3, 0, /* 0xe5 */
1672 {{0, 0},
1673 {2, 2},
1674 {5, 7},
1675 {0, 0}
1676 }
1677 },
1678 {0, 1, 2, 0, /* 0xe6 */
1679 {{1, 2},
1680 {5, 7},
1681 {0, 0},
1682 {0, 0}
1683 }
1684 },
1685 {1, 1, 2, 0, /* 0xe7 */
1686 {{0, 2},
1687 {5, 7},
1688 {0, 0},
1689 {0, 0}
1690 }
1691 },
1692 {0, 1, 2, 0, /* 0xe8 */
1693 {{3, 3},
1694 {5, 7},
1695 {0, 0},
1696 {0, 0}
1697 }
1698 },
1699 {1, 1, 3, 0, /* 0xe9 */
1700 {{0, 0},
1701 {3, 3},
1702 {5, 7},
1703 {0, 0}
1704 }
1705 },
1706 {0, 1, 3, 0, /* 0xea */
1707 {{1, 1},
1708 {3, 3},
1709 {5, 7},
1710 {0, 0}
1711 }
1712 },
1713 {1, 1, 3, 0, /* 0xeb */
1714 {{0, 1},
1715 {3, 3},
1716 {5, 7},
1717 {0, 0}
1718 }
1719 },
1720 {0, 1, 2, 0, /* 0xec */
1721 {{2, 3},
1722 {5, 7},
1723 {0, 0},
1724 {0, 0}
1725 }
1726 },
1727 {1, 1, 3, 0, /* 0xed */
1728 {{0, 0},
1729 {2, 3},
1730 {5, 7},
1731 {0, 0}
1732 }
1733 },
1734 {0, 1, 2, 0, /* 0xee */
1735 {{1, 3},
1736 {5, 7},
1737 {0, 0},
1738 {0, 0}
1739 }
1740 },
1741 {1, 1, 2, 0, /* 0xef */
1742 {{0, 3},
1743 {5, 7},
1744 {0, 0},
1745 {0, 0}
1746 }
1747 },
1748 {0, 1, 1, 0, /* 0xf0 */
1749 {{4, 7},
1750 {0, 0},
1751 {0, 0},
1752 {0, 0}
1753 }
1754 },
1755 {1, 1, 2, 0, /* 0xf1 */
1756 {{0, 0},
1757 {4, 7},
1758 {0, 0},
1759 {0, 0}
1760 }
1761 },
1762 {0, 1, 2, 0, /* 0xf2 */
1763 {{1, 1},
1764 {4, 7},
1765 {0, 0},
1766 {0, 0}
1767 }
1768 },
1769 {1, 1, 2, 0, /* 0xf3 */
1770 {{0, 1},
1771 {4, 7},
1772 {0, 0},
1773 {0, 0}
1774 }
1775 },
1776 {0, 1, 2, 0, /* 0xf4 */
1777 {{2, 2},
1778 {4, 7},
1779 {0, 0},
1780 {0, 0}
1781 }
1782 },
1783 {1, 1, 3, 0, /* 0xf5 */
1784 {{0, 0},
1785 {2, 2},
1786 {4, 7},
1787 {0, 0}
1788 }
1789 },
1790 {0, 1, 2, 0, /* 0xf6 */
1791 {{1, 2},
1792 {4, 7},
1793 {0, 0},
1794 {0, 0}
1795 }
1796 },
1797 {1, 1, 2, 0, /* 0xf7 */
1798 {{0, 2},
1799 {4, 7},
1800 {0, 0},
1801 {0, 0}
1802 }
1803 },
1804 {0, 1, 1, 0, /* 0xf8 */
1805 {{3, 7},
1806 {0, 0},
1807 {0, 0},
1808 {0, 0}
1809 }
1810 },
1811 {1, 1, 2, 0, /* 0xf9 */
1812 {{0, 0},
1813 {3, 7},
1814 {0, 0},
1815 {0, 0}
1816 }
1817 },
1818 {0, 1, 2, 0, /* 0xfa */
1819 {{1, 1},
1820 {3, 7},
1821 {0, 0},
1822 {0, 0}
1823 }
1824 },
1825 {1, 1, 2, 0, /* 0xfb */
1826 {{0, 1},
1827 {3, 7},
1828 {0, 0},
1829 {0, 0}
1830 }
1831 },
1832 {0, 1, 1, 0, /* 0xfc */
1833 {{2, 7},
1834 {0, 0},
1835 {0, 0},
1836 {0, 0}
1837 }
1838 },
1839 {1, 1, 2, 0, /* 0xfd */
1840 {{0, 0},
1841 {2, 7},
1842 {0, 0},
1843 {0, 0}
1844 }
1845 },
1846 {0, 1, 1, 0, /* 0xfe */
1847 {{1, 7},
1848 {0, 0},
1849 {0, 0},
1850 {0, 0}
1851 }
1852 },
1853 {1, 1, 1, 0, /* 0xff */
1854 {{0, 7},
1855 {0, 0},
1856 {0, 0},
1857 {0, 0}
1858 }
1859 }
1860};
1861
1862
1863int
1864sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865 int ipv4_addr_legal,
1866 int ipv6_addr_legal,
1867 int loopback_scope,
1868 int ipv4_local_scope,
1869 int local_scope,
1870 int site_scope,
1871 int do_update)
1872{
1873 if ((loopback_scope == 0) &&
1874 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 /*
1876 * skip loopback if not in scope *
1877 */
1878 return (0);
1879 }
1880 switch (ifa->address.sa.sa_family) {
1881 case AF_INET:
1882 if (ipv4_addr_legal) {
1883 struct sockaddr_in *sin;
1884
1885 sin = (struct sockaddr_in *)&ifa->address.sin;
1886 if (sin->sin_addr.s_addr == 0) {
1887 /* not in scope , unspecified */
1888 return (0);
1889 }
1890 if ((ipv4_local_scope == 0) &&
1891 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 /* private address not in scope */
1893 return (0);
1894 }
1895 } else {
1896 return (0);
1897 }
1898 break;
1899#ifdef INET6
1900 case AF_INET6:
1901 if (ipv6_addr_legal) {
1902 struct sockaddr_in6 *sin6;
1903
1904 /*
1905 * Must update the flags, bummer, which means any
1906 * IFA locks must now be applied HERE <->
1907 */
1908 if (do_update) {
1909 sctp_gather_internal_ifa_flags(ifa);
1910 }
1911 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 return (0);
1913 }
1914 /* ok to use deprecated addresses? */
1915 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 /* skip unspecifed addresses */
1918 return (0);
1919 }
1920 if ( /* (local_scope == 0) && */
1921 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 return (0);
1923 }
1924 if ((site_scope == 0) &&
1925 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926 return (0);
1927 }
1928 } else {
1929 return (0);
1930 }
1931 break;
1932#endif
1933 default:
1934 return (0);
1935 }
1936 return (1);
1937}
1938
1939static struct mbuf *
1940sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941{
1942 struct sctp_paramhdr *parmh;
1943 struct mbuf *mret;
1944 int len;
1945
1946 if (ifa->address.sa.sa_family == AF_INET) {
1947 len = sizeof(struct sctp_ipv4addr_param);
1948 } else if (ifa->address.sa.sa_family == AF_INET6) {
1949 len = sizeof(struct sctp_ipv6addr_param);
1950 } else {
1951 /* unknown type */
1952 return (m);
1953 }
1954 if (M_TRAILINGSPACE(m) >= len) {
1955 /* easy side we just drop it on the end */
1956 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957 mret = m;
1958 } else {
1959 /* Need more space */
1960 mret = m;
1961 while (SCTP_BUF_NEXT(mret) != NULL) {
1962 mret = SCTP_BUF_NEXT(mret);
1963 }
1964 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 if (SCTP_BUF_NEXT(mret) == NULL) {
1966 /* We are hosed, can't add more addresses */
1967 return (m);
1968 }
1969 mret = SCTP_BUF_NEXT(mret);
1970 parmh = mtod(mret, struct sctp_paramhdr *);
1971 }
1972 /* now add the parameter */
1973 switch (ifa->address.sa.sa_family) {
1974 case AF_INET:
1975 {
1976 struct sctp_ipv4addr_param *ipv4p;
1977 struct sockaddr_in *sin;
1978
1979 sin = (struct sockaddr_in *)&ifa->address.sin;
1980 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 parmh->param_length = htons(len);
1983 ipv4p->addr = sin->sin_addr.s_addr;
1984 SCTP_BUF_LEN(mret) += len;
1985 break;
1986 }
1987#ifdef INET6
1988 case AF_INET6:
1989 {
1990 struct sctp_ipv6addr_param *ipv6p;
1991 struct sockaddr_in6 *sin6;
1992
1993 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 parmh->param_length = htons(len);
1997 memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 sizeof(ipv6p->addr));
1999 /* clear embedded scope in the address */
2000 in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 SCTP_BUF_LEN(mret) += len;
2002 break;
2003 }
2004#endif
2005 default:
2006 return (m);
2007 }
2008 return (mret);
2009}
2010
2011
2012struct mbuf *
2013sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014 struct mbuf *m_at, int cnt_inits_to)
2015{
2016 struct sctp_vrf *vrf = NULL;
2017 int cnt, limit_out = 0, total_count;
2018 uint32_t vrf_id;
2019
2020 vrf_id = inp->def_vrf_id;
2021 SCTP_IPI_ADDR_RLOCK();
2022 vrf = sctp_find_vrf(vrf_id);
2023 if (vrf == NULL) {
2024 SCTP_IPI_ADDR_RUNLOCK();
2025 return (m_at);
2026 }
2027 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 struct sctp_ifa *sctp_ifap;
2029 struct sctp_ifn *sctp_ifnp;
2030
2031 cnt = cnt_inits_to;
2032 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 limit_out = 1;
2034 cnt = SCTP_ADDRESS_LIMIT;
2035 goto skip_count;
2036 }
2037 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 if ((scope->loopback_scope == 0) &&
2039 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 /*
2041 * Skip loopback devices if loopback_scope
2042 * not set
2043 */
2044 continue;
2045 }
2046 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 if (sctp_is_address_in_scope(sctp_ifap,
2048 scope->ipv4_addr_legal,
2049 scope->ipv6_addr_legal,
2050 scope->loopback_scope,
2051 scope->ipv4_local_scope,
2052 scope->local_scope,
2053 scope->site_scope, 1) == 0) {
2054 continue;
2055 }
2056 cnt++;
2057 if (cnt > SCTP_ADDRESS_LIMIT) {
2058 break;
2059 }
2060 }
2061 if (cnt > SCTP_ADDRESS_LIMIT) {
2062 break;
2063 }
2064 }
2065skip_count:
2066 if (cnt > 1) {
2067 total_count = 0;
2068 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 cnt = 0;
2070 if ((scope->loopback_scope == 0) &&
2071 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 /*
2073 * Skip loopback devices if
2074 * loopback_scope not set
2075 */
2076 continue;
2077 }
2078 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 if (sctp_is_address_in_scope(sctp_ifap,
2080 scope->ipv4_addr_legal,
2081 scope->ipv6_addr_legal,
2082 scope->loopback_scope,
2083 scope->ipv4_local_scope,
2084 scope->local_scope,
2085 scope->site_scope, 0) == 0) {
2086 continue;
2087 }
2088 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089 if (limit_out) {
2090 cnt++;
2091 total_count++;
2092 if (cnt >= 2) {
2093 /*
2094 * two from each
2095 * address
2096 */
2097 break;
2098 }
2099 if (total_count > SCTP_ADDRESS_LIMIT) {
2100 /* No more addresses */
2101 break;
2102 }
2103 }
2104 }
2105 }
2106 }
2107 } else {
2108 struct sctp_laddr *laddr;
2109
2110 cnt = cnt_inits_to;
2111 /* First, how many ? */
2112 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 if (laddr->ifa == NULL) {
2114 continue;
2115 }
2116 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 /*
2118 * Address being deleted by the system, dont
2119 * list.
2120 */
2121 continue;
2122 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 /*
2124 * Address being deleted on this ep don't
2125 * list.
2126 */
2127 continue;
2128 }
2129 if (sctp_is_address_in_scope(laddr->ifa,
2130 scope->ipv4_addr_legal,
2131 scope->ipv6_addr_legal,
2132 scope->loopback_scope,
2133 scope->ipv4_local_scope,
2134 scope->local_scope,
2135 scope->site_scope, 1) == 0) {
2136 continue;
2137 }
2138 cnt++;
2139 }
2140 if (cnt > SCTP_ADDRESS_LIMIT) {
2141 limit_out = 1;
2142 }
2143 /*
2144 * To get through a NAT we only list addresses if we have
2145 * more than one. That way if you just bind a single address
2146 * we let the source of the init dictate our address.
2147 */
2148 if (cnt > 1) {
2149 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 cnt = 0;
2151 if (laddr->ifa == NULL) {
2152 continue;
2153 }
2154 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155 continue;
2156
2157 if (sctp_is_address_in_scope(laddr->ifa,
2158 scope->ipv4_addr_legal,
2159 scope->ipv6_addr_legal,
2160 scope->loopback_scope,
2161 scope->ipv4_local_scope,
2162 scope->local_scope,
2163 scope->site_scope, 0) == 0) {
2164 continue;
2165 }
2166 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 cnt++;
2168 if (cnt >= SCTP_ADDRESS_LIMIT) {
2169 break;
2170 }
2171 }
2172 }
2173 }
2174 SCTP_IPI_ADDR_RUNLOCK();
2175 return (m_at);
2176}
2177
2178static struct sctp_ifa *
2179sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180 uint8_t dest_is_loop,
2181 uint8_t dest_is_priv,
2182 sa_family_t fam)
2183{
2184 uint8_t dest_is_global = 0;
2185
2186 /* dest_is_priv is true if destination is a private address */
2187 /* dest_is_loop is true if destination is a loopback addresses */
2188
2189 /*
2190 * Here we determine if its a preferred address. A preferred address
2191 * means it is the same scope or higher scope then the destination.
2192 * L = loopback, P = private, G = global
2193 * ----------------------------------------- src | dest | result
2194 * ---------------------------------------- L | L | yes
2195 * ----------------------------------------- P | L |
2196 * yes-v4 no-v6 ----------------------------------------- G |
2197 * L | yes-v4 no-v6 ----------------------------------------- L
2198 * | P | no ----------------------------------------- P |
2199 * P | yes ----------------------------------------- G |
2200 * P | no ----------------------------------------- L | G
2201 * | no ----------------------------------------- P | G |
2202 * no ----------------------------------------- G | G |
2203 * yes -----------------------------------------
2204 */
2205
2206 if (ifa->address.sa.sa_family != fam) {
2207 /* forget mis-matched family */
2208 return (NULL);
2209 }
2210 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2211 dest_is_global = 1;
2212 }
2213 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2214 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2215 /* Ok the address may be ok */
2216 if (fam == AF_INET6) {
2217 /* ok to use deprecated addresses? no lets not! */
2218 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2219 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2220 return (NULL);
2221 }
2222 if (ifa->src_is_priv && !ifa->src_is_loop) {
2223 if (dest_is_loop) {
2224 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2225 return (NULL);
2226 }
2227 }
2228 if (ifa->src_is_glob) {
2229 if (dest_is_loop) {
2230 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2231 return (NULL);
2232 }
2233 }
2234 }
2235 /*
2236 * Now that we know what is what, implement or table this could in
2237 * theory be done slicker (it used to be), but this is
2238 * straightforward and easier to validate :-)
2239 */
2240 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2241 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2242 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2243 dest_is_loop, dest_is_priv, dest_is_global);
2244
2245 if ((ifa->src_is_loop) && (dest_is_priv)) {
2246 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2247 return (NULL);
2248 }
2249 if ((ifa->src_is_glob) && (dest_is_priv)) {
2250 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2251 return (NULL);
2252 }
2253 if ((ifa->src_is_loop) && (dest_is_global)) {
2254 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2255 return (NULL);
2256 }
2257 if ((ifa->src_is_priv) && (dest_is_global)) {
2258 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2259 return (NULL);
2260 }
2261 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2262 /* its a preferred address */
2263 return (ifa);
2264}
2265
2266static struct sctp_ifa *
2267sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2268 uint8_t dest_is_loop,
2269 uint8_t dest_is_priv,
2270 sa_family_t fam)
2271{
2272 uint8_t dest_is_global = 0;
2273
2274
2275 /*
2276 * Here we determine if its a acceptable address. A acceptable
2277 * address means it is the same scope or higher scope but we can
2278 * allow for NAT which means its ok to have a global dest and a
2279 * private src.
2280 *
2281 * L = loopback, P = private, G = global
2282 * ----------------------------------------- src | dest | result
2283 * ----------------------------------------- L | L | yes
2284 * ----------------------------------------- P | L |
2285 * yes-v4 no-v6 ----------------------------------------- G |
2286 * L | yes ----------------------------------------- L |
2287 * P | no ----------------------------------------- P | P
2288 * | yes ----------------------------------------- G | P
2289 * | yes - May not work -----------------------------------------
2290 * L | G | no ----------------------------------------- P
2291 * | G | yes - May not work
2292 * ----------------------------------------- G | G | yes
2293 * -----------------------------------------
2294 */
2295
2296 if (ifa->address.sa.sa_family != fam) {
2297 /* forget non matching family */
2298 return (NULL);
2299 }
2300 /* Ok the address may be ok */
2301 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2302 dest_is_global = 1;
2303 }
2304 if (fam == AF_INET6) {
2305 /* ok to use deprecated addresses? */
2306 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2307 return (NULL);
2308 }
2309 if (ifa->src_is_priv) {
2310 /* Special case, linklocal to loop */
2311 if (dest_is_loop)
2312 return (NULL);
2313 }
2314 }
2315 /*
2316 * Now that we know what is what, implement our table. This could in
2317 * theory be done slicker (it used to be), but this is
2318 * straightforward and easier to validate :-)
2319 */
2320 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2321 return (NULL);
2322 }
2323 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2324 return (NULL);
2325 }
2326 /* its an acceptable address */
2327 return (ifa);
2328}
2329
2330int
2331sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2332{
2333 struct sctp_laddr *laddr;
2334
2335 if (stcb == NULL) {
2336 /* There are no restrictions, no TCB :-) */
2337 return (0);
2338 }
2339 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2340 if (laddr->ifa == NULL) {
2341 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2342 __FUNCTION__);
2343 continue;
2344 }
2345 if (laddr->ifa == ifa) {
2346 /* Yes it is on the list */
2347 return (1);
2348 }
2349 }
2350 return (0);
2351}
2352
2353
2354int
2355sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2356{
2357 struct sctp_laddr *laddr;
2358
2359 if (ifa == NULL)
2360 return (0);
2361 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2362 if (laddr->ifa == NULL) {
2363 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2364 __FUNCTION__);
2365 continue;
2366 }
2367 if ((laddr->ifa == ifa) && laddr->action == 0)
2368 /* same pointer */
2369 return (1);
2370 }
2371 return (0);
2372}
2373
2374
2375
2376static struct sctp_ifa *
2377sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2378 sctp_route_t * ro,
2379 uint32_t vrf_id,
2380 int non_asoc_addr_ok,
2381 uint8_t dest_is_priv,
2382 uint8_t dest_is_loop,
2383 sa_family_t fam)
2384{
2385 struct sctp_laddr *laddr, *starting_point;
2386 void *ifn;
2387 int resettotop = 0;
2388 struct sctp_ifn *sctp_ifn;
2389 struct sctp_ifa *sctp_ifa, *sifa;
2390 struct sctp_vrf *vrf;
2391 uint32_t ifn_index;
2392
2393 vrf = sctp_find_vrf(vrf_id);
2394 if (vrf == NULL)
2395 return (NULL);
2396
2397 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2398 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2399 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2400 /*
2401 * first question, is the ifn we will emit on in our list, if so, we
2402 * want such an address. Note that we first looked for a preferred
2403 * address.
2404 */
2405 if (sctp_ifn) {
2406 /* is a preferred one on the interface we route out? */
2407 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2408 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2409 (non_asoc_addr_ok == 0))
2410 continue;
2411 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2412 dest_is_loop,
2413 dest_is_priv, fam);
2414 if (sifa == NULL)
2415 continue;
2416 if (sctp_is_addr_in_ep(inp, sifa)) {
2417 atomic_add_int(&sifa->refcount, 1);
2418 return (sifa);
2419 }
2420 }
2421 }
2422 /*
2423 * ok, now we now need to find one on the list of the addresses. We
2424 * can't get one on the emitting interface so let's find first a
2425 * preferred one. If not that an acceptable one otherwise... we
2426 * return NULL.
2427 */
2428 starting_point = inp->next_addr_touse;
2429once_again:
2430 if (inp->next_addr_touse == NULL) {
2431 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2432 resettotop = 1;
2433 }
2434 for (laddr = inp->next_addr_touse; laddr;
2435 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2436 if (laddr->ifa == NULL) {
2437 /* address has been removed */
2438 continue;
2439 }
2440 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2441 /* address is being deleted */
2442 continue;
2443 }
2444 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2445 dest_is_priv, fam);
2446 if (sifa == NULL)
2447 continue;
2448 atomic_add_int(&sifa->refcount, 1);
2449 return (sifa);
2450 }
2451 if (resettotop == 0) {
2452 inp->next_addr_touse = NULL;
2453 goto once_again;
2454 }
2455 inp->next_addr_touse = starting_point;
2456 resettotop = 0;
2457once_again_too:
2458 if (inp->next_addr_touse == NULL) {
2459 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2460 resettotop = 1;
2461 }
2462 /* ok, what about an acceptable address in the inp */
2463 for (laddr = inp->next_addr_touse; laddr;
2464 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2465 if (laddr->ifa == NULL) {
2466 /* address has been removed */
2467 continue;
2468 }
2469 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2470 /* address is being deleted */
2471 continue;
2472 }
2473 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2474 dest_is_priv, fam);
2475 if (sifa == NULL)
2476 continue;
2477 atomic_add_int(&sifa->refcount, 1);
2478 return (sifa);
2479 }
2480 if (resettotop == 0) {
2481 inp->next_addr_touse = NULL;
2482 goto once_again_too;
2483 }
2484 /*
2485 * no address bound can be a source for the destination we are in
2486 * trouble
2487 */
2488 return (NULL);
2489}
2490
2491
2492
2493static struct sctp_ifa *
2494sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2495 struct sctp_tcb *stcb,
2496 struct sctp_nets *net,
2497 sctp_route_t * ro,
2498 uint32_t vrf_id,
2499 uint8_t dest_is_priv,
2500 uint8_t dest_is_loop,
2501 int non_asoc_addr_ok,
2502 sa_family_t fam)
2503{
2504 struct sctp_laddr *laddr, *starting_point;
2505 void *ifn;
2506 struct sctp_ifn *sctp_ifn;
2507 struct sctp_ifa *sctp_ifa, *sifa;
2508 uint8_t start_at_beginning = 0;
2509 struct sctp_vrf *vrf;
2510 uint32_t ifn_index;
2511
2512 /*
2513 * first question, is the ifn we will emit on in our list, if so, we
2514 * want that one.
2515 */
2516 vrf = sctp_find_vrf(vrf_id);
2517 if (vrf == NULL)
2518 return (NULL);
2519
2520 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2521 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2522 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2523
2524 /*
2525 * first question, is the ifn we will emit on in our list? If so,
2526 * we want that one. First we look for a preferred. Second, we go
2527 * for an acceptable.
2528 */
2529 if (sctp_ifn) {
2530 /* first try for a preferred address on the ep */
2531 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2532 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2533 continue;
2534 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2535 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2536 if (sifa == NULL)
2537 continue;
2538 if (((non_asoc_addr_ok == 0) &&
2539 (sctp_is_addr_restricted(stcb, sifa))) ||
2540 (non_asoc_addr_ok &&
2541 (sctp_is_addr_restricted(stcb, sifa)) &&
2542 (!sctp_is_addr_pending(stcb, sifa)))) {
2543 /* on the no-no list */
2544 continue;
2545 }
2546 atomic_add_int(&sifa->refcount, 1);
2547 return (sifa);
2548 }
2549 }
2550 /* next try for an acceptable address on the ep */
2551 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2552 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2553 continue;
2554 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2555 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2556 if (sifa == NULL)
2557 continue;
2558 if (((non_asoc_addr_ok == 0) &&
2559 (sctp_is_addr_restricted(stcb, sifa))) ||
2560 (non_asoc_addr_ok &&
2561 (sctp_is_addr_restricted(stcb, sifa)) &&
2562 (!sctp_is_addr_pending(stcb, sifa)))) {
2563 /* on the no-no list */
2564 continue;
2565 }
2566 atomic_add_int(&sifa->refcount, 1);
2567 return (sifa);
2568 }
2569 }
2570
2571 }
2572 /*
2573 * if we can't find one like that then we must look at all addresses
2574 * bound to pick one at first preferable then secondly acceptable.
2575 */
2576 starting_point = stcb->asoc.last_used_address;
2577sctp_from_the_top:
2578 if (stcb->asoc.last_used_address == NULL) {
2579 start_at_beginning = 1;
2580 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2581 }
2582 /* search beginning with the last used address */
2583 for (laddr = stcb->asoc.last_used_address; laddr;
2584 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2585 if (laddr->ifa == NULL) {
2586 /* address has been removed */
2587 continue;
2588 }
2589 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2590 /* address is being deleted */
2591 continue;
2592 }
2593 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2594 if (sifa == NULL)
2595 continue;
2596 if (((non_asoc_addr_ok == 0) &&
2597 (sctp_is_addr_restricted(stcb, sifa))) ||
2598 (non_asoc_addr_ok &&
2599 (sctp_is_addr_restricted(stcb, sifa)) &&
2600 (!sctp_is_addr_pending(stcb, sifa)))) {
2601 /* on the no-no list */
2602 continue;
2603 }
2604 stcb->asoc.last_used_address = laddr;
2605 atomic_add_int(&sifa->refcount, 1);
2606 return (sifa);
2607 }
2608 if (start_at_beginning == 0) {
2609 stcb->asoc.last_used_address = NULL;
2610 goto sctp_from_the_top;
2611 }
2612 /* now try for any higher scope than the destination */
2613 stcb->asoc.last_used_address = starting_point;
2614 start_at_beginning = 0;
2615sctp_from_the_top2:
2616 if (stcb->asoc.last_used_address == NULL) {
2617 start_at_beginning = 1;
2618 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2619 }
2620 /* search beginning with the last used address */
2621 for (laddr = stcb->asoc.last_used_address; laddr;
2622 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2623 if (laddr->ifa == NULL) {
2624 /* address has been removed */
2625 continue;
2626 }
2627 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2628 /* address is being deleted */
2629 continue;
2630 }
2631 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2632 dest_is_priv, fam);
2633 if (sifa == NULL)
2634 continue;
2635 if (((non_asoc_addr_ok == 0) &&
2636 (sctp_is_addr_restricted(stcb, sifa))) ||
2637 (non_asoc_addr_ok &&
2638 (sctp_is_addr_restricted(stcb, sifa)) &&
2639 (!sctp_is_addr_pending(stcb, sifa)))) {
2640 /* on the no-no list */
2641 continue;
2642 }
2643 stcb->asoc.last_used_address = laddr;
2644 atomic_add_int(&sifa->refcount, 1);
2645 return (sifa);
2646 }
2647 if (start_at_beginning == 0) {
2648 stcb->asoc.last_used_address = NULL;
2649 goto sctp_from_the_top2;
2650 }
2651 return (NULL);
2652}
2653
2654static struct sctp_ifa *
2655sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2656 struct sctp_tcb *stcb,
2657 int non_asoc_addr_ok,
2658 uint8_t dest_is_loop,
2659 uint8_t dest_is_priv,
2660 int addr_wanted,
2661 sa_family_t fam,
2662 sctp_route_t * ro
2663)
2664{
2665 struct sctp_ifa *ifa, *sifa;
2666 int num_eligible_addr = 0;
2667
2668#ifdef INET6
2669 struct sockaddr_in6 sin6, lsa6;
2670
2671 if (fam == AF_INET6) {
2672 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2673 (void)sa6_recoverscope(&sin6);
2674 }
2675#endif /* INET6 */
2676 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2677 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2678 (non_asoc_addr_ok == 0))
2679 continue;
2680 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2681 dest_is_priv, fam);
2682 if (sifa == NULL)
2683 continue;
2684#ifdef INET6
2685 if (fam == AF_INET6 &&
2686 dest_is_loop &&
2687 sifa->src_is_loop && sifa->src_is_priv) {
2688 /*
2689 * don't allow fe80::1 to be a src on loop ::1, we
2690 * don't list it to the peer so we will get an
2691 * abort.
2692 */
2693 continue;
2694 }
2695 if (fam == AF_INET6 &&
2696 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2697 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2698 /*
2699 * link-local <-> link-local must belong to the same
2700 * scope.
2701 */
2702 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2703 (void)sa6_recoverscope(&lsa6);
2704 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2705 continue;
2706 }
2707 }
2708#endif /* INET6 */
2709
2710 /*
2711 * Check if the IPv6 address matches to next-hop. In the
2712 * mobile case, old IPv6 address may be not deleted from the
2713 * interface. Then, the interface has previous and new
2714 * addresses. We should use one corresponding to the
2715 * next-hop. (by micchie)
2716 */
2717#ifdef INET6
2718 if (stcb && fam == AF_INET6 &&
2719 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2720 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2721 == 0) {
2722 continue;
2723 }
2724 }
2725#endif
2726 /* Avoid topologically incorrect IPv4 address */
2727 if (stcb && fam == AF_INET &&
2728 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2730 continue;
2731 }
2732 }
2733 if (stcb) {
2734 if (((non_asoc_addr_ok == 0) &&
2735 (sctp_is_addr_restricted(stcb, sifa))) ||
2736 (non_asoc_addr_ok &&
2737 (sctp_is_addr_restricted(stcb, sifa)) &&
2738 (!sctp_is_addr_pending(stcb, sifa)))) {
2739 /*
2740 * It is restricted for some reason..
2741 * probably not yet added.
2742 */
2743 continue;
2744 }
2745 }
2746 if (num_eligible_addr >= addr_wanted) {
2747 return (sifa);
2748 }
2749 num_eligible_addr++;
2750 }
2751 return (NULL);
2752}
2753
2754
2755static int
2756sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2757 struct sctp_tcb *stcb,
2758 int non_asoc_addr_ok,
2759 uint8_t dest_is_loop,
2760 uint8_t dest_is_priv,
2761 sa_family_t fam)
2762{
2763 struct sctp_ifa *ifa, *sifa;
2764 int num_eligible_addr = 0;
2765
2766 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2767 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2768 (non_asoc_addr_ok == 0)) {
2769 continue;
2770 }
2771 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2772 dest_is_priv, fam);
2773 if (sifa == NULL) {
2774 continue;
2775 }
2776 if (stcb) {
2777 if (((non_asoc_addr_ok == 0) &&
2778 (sctp_is_addr_restricted(stcb, sifa))) ||
2779 (non_asoc_addr_ok &&
2780 (sctp_is_addr_restricted(stcb, sifa)) &&
2781 (!sctp_is_addr_pending(stcb, sifa)))) {
2782 /*
2783 * It is restricted for some reason..
2784 * probably not yet added.
2785 */
2786 continue;
2787 }
2788 }
2789 num_eligible_addr++;
2790 }
2791 return (num_eligible_addr);
2792}
2793
2794static struct sctp_ifa *
2795sctp_choose_boundall(struct sctp_inpcb *inp,
2796 struct sctp_tcb *stcb,
2797 struct sctp_nets *net,
2798 sctp_route_t * ro,
2799 uint32_t vrf_id,
2800 uint8_t dest_is_priv,
2801 uint8_t dest_is_loop,
2802 int non_asoc_addr_ok,
2803 sa_family_t fam)
2804{
2805 int cur_addr_num = 0, num_preferred = 0;
2806 void *ifn;
2807 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2808 struct sctp_ifa *sctp_ifa, *sifa;
2809 uint32_t ifn_index;
2810 struct sctp_vrf *vrf;
2811
2812 /*-
2813 * For boundall we can use any address in the association.
2814 * If non_asoc_addr_ok is set we can use any address (at least in
2815 * theory). So we look for preferred addresses first. If we find one,
2816 * we use it. Otherwise we next try to get an address on the
2817 * interface, which we should be able to do (unless non_asoc_addr_ok
2818 * is false and we are routed out that way). In these cases where we
2819 * can't use the address of the interface we go through all the
2820 * ifn's looking for an address we can use and fill that in. Punting
2821 * means we send back address 0, which will probably cause problems
2822 * actually since then IP will fill in the address of the route ifn,
2823 * which means we probably already rejected it.. i.e. here comes an
2824 * abort :-<.
2825 */
2826 vrf = sctp_find_vrf(vrf_id);
2827 if (vrf == NULL)
2828 return (NULL);
2829
2830 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2831 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2832 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2833 if (sctp_ifn == NULL) {
2834 /* ?? We don't have this guy ?? */
2835 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2836 goto bound_all_plan_b;
2837 }
2838 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2839 ifn_index, sctp_ifn->ifn_name);
2840
2841 if (net) {
2842 cur_addr_num = net->indx_of_eligible_next_to_use;
2843 }
2844 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2845 stcb,
2846 non_asoc_addr_ok,
2847 dest_is_loop,
2848 dest_is_priv, fam);
2849 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2850 num_preferred, sctp_ifn->ifn_name);
2851 if (num_preferred == 0) {
2852 /*
2853 * no eligible addresses, we must use some other interface
2854 * address if we can find one.
2855 */
2856 goto bound_all_plan_b;
2857 }
2858 /*
2859 * Ok we have num_eligible_addr set with how many we can use, this
2860 * may vary from call to call due to addresses being deprecated
2861 * etc..
2862 */
2863 if (cur_addr_num >= num_preferred) {
2864 cur_addr_num = 0;
2865 }
2866 /*
2867 * select the nth address from the list (where cur_addr_num is the
2868 * nth) and 0 is the first one, 1 is the second one etc...
2869 */
2870 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2871
2872 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2873 dest_is_priv, cur_addr_num, fam, ro);
2874
2875 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2876 if (sctp_ifa) {
2877 atomic_add_int(&sctp_ifa->refcount, 1);
2878 if (net) {
2879 /* save off where the next one we will want */
2880 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2881 }
2882 return (sctp_ifa);
2883 }
2884 /*
2885 * plan_b: Look at all interfaces and find a preferred address. If
2886 * no preferred fall through to plan_c.
2887 */
2888bound_all_plan_b:
2889 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2890 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2891 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2892 sctp_ifn->ifn_name);
2893 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2894 /* wrong base scope */
2895 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2896 continue;
2897 }
2898 if ((sctp_ifn == looked_at) && looked_at) {
2899 /* already looked at this guy */
2900 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2901 continue;
2902 }
2903 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2904 dest_is_loop, dest_is_priv, fam);
2905 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2906 "Found ifn:%p %d preferred source addresses\n",
2907 ifn, num_preferred);
2908 if (num_preferred == 0) {
2909 /* None on this interface. */
2910 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2911 continue;
2912 }
2913 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2914 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2915 num_preferred, sctp_ifn, cur_addr_num);
2916
2917 /*
2918 * Ok we have num_eligible_addr set with how many we can
2919 * use, this may vary from call to call due to addresses
2920 * being deprecated etc..
2921 */
2922 if (cur_addr_num >= num_preferred) {
2923 cur_addr_num = 0;
2924 }
2925 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2926 dest_is_priv, cur_addr_num, fam, ro);
2927 if (sifa == NULL)
2928 continue;
2929 if (net) {
2930 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2931 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2932 cur_addr_num);
2933 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2934 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2935 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2936 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2937 }
2938 atomic_add_int(&sifa->refcount, 1);
2939 return (sifa);
2940
2941 }
2942
2943 /* plan_c: do we have an acceptable address on the emit interface */
2944 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2945 if (emit_ifn == NULL) {
2946 goto plan_d;
2947 }
2948 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2949 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2950 (non_asoc_addr_ok == 0))
2951 continue;
2952 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2953 dest_is_priv, fam);
2954 if (sifa == NULL)
2955 continue;
2956 if (stcb) {
2957 if (((non_asoc_addr_ok == 0) &&
2958 (sctp_is_addr_restricted(stcb, sifa))) ||
2959 (non_asoc_addr_ok &&
2960 (sctp_is_addr_restricted(stcb, sifa)) &&
2961 (!sctp_is_addr_pending(stcb, sifa)))) {
2962 /*
2963 * It is restricted for some reason..
2964 * probably not yet added.
2965 */
2966 continue;
2967 }
2968 }
2969 atomic_add_int(&sifa->refcount, 1);
2970 return (sifa);
2971 }
2972plan_d:
2973 /*
2974 * plan_d: We are in trouble. No preferred address on the emit
2975 * interface. And not even a preferred address on all interfaces. Go
2976 * out and see if we can find an acceptable address somewhere
2977 * amongst all interfaces.
2978 */
2979 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2980 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2981 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2982 /* wrong base scope */
2983 continue;
2984 }
2985 if ((sctp_ifn == looked_at) && looked_at)
2986 /* already looked at this guy */
2987 continue;
2988
2989 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2990 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2991 (non_asoc_addr_ok == 0))
2992 continue;
2993 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2994 dest_is_loop,
2995 dest_is_priv, fam);
2996 if (sifa == NULL)
2997 continue;
2998 if (stcb) {
2999 if (((non_asoc_addr_ok == 0) &&
3000 (sctp_is_addr_restricted(stcb, sifa))) ||
3001 (non_asoc_addr_ok &&
3002 (sctp_is_addr_restricted(stcb, sifa)) &&
3003 (!sctp_is_addr_pending(stcb, sifa)))) {
3004 /*
3005 * It is restricted for some
3006 * reason.. probably not yet added.
3007 */
3008 continue;
3009 }
3010 }
3011 atomic_add_int(&sifa->refcount, 1);
3012 return (sifa);
3013 }
3014 }
3015 /*
3016 * Ok we can find NO address to source from that is not on our
3017 * restricted list and non_asoc_address is NOT ok, or it is on our
3018 * restricted list. We can't source to it :-(
3019 */
3020 return (NULL);
3021}
3022
3023
3024
3025/* tcb may be NULL */
3026struct sctp_ifa *
3027sctp_source_address_selection(struct sctp_inpcb *inp,
3028 struct sctp_tcb *stcb,
3029 sctp_route_t * ro,
3030 struct sctp_nets *net,
3031 int non_asoc_addr_ok, uint32_t vrf_id)
3032{
3033 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3034
3035#ifdef INET6
3036 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3037
3038#endif
3039 struct sctp_ifa *answer;
3040 uint8_t dest_is_priv, dest_is_loop;
3041 sa_family_t fam;
3042
3043 /*-
3044 * Rules: - Find the route if needed, cache if I can. - Look at
3045 * interface address in route, Is it in the bound list. If so we
3046 * have the best source. - If not we must rotate amongst the
3047 * addresses.
3048 *
3049 * Cavets and issues
3050 *
3051 * Do we need to pay attention to scope. We can have a private address
3052 * or a global address we are sourcing or sending to. So if we draw
3053 * it out
3054 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3055 * For V4
3056 *------------------------------------------
3057 * source * dest * result
3058 * -----------------------------------------
3059 * <a> Private * Global * NAT
3060 * -----------------------------------------
3061 * <b> Private * Private * No problem
3062 * -----------------------------------------
3063 * <c> Global * Private * Huh, How will this work?
3064 * -----------------------------------------
3065 * <d> Global * Global * No Problem
3066 *------------------------------------------
3067 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3068 * For V6
3069 *------------------------------------------
3070 * source * dest * result
3071 * -----------------------------------------
3072 * <a> Linklocal * Global *
3073 * -----------------------------------------
3074 * <b> Linklocal * Linklocal * No problem
3075 * -----------------------------------------
3076 * <c> Global * Linklocal * Huh, How will this work?
3077 * -----------------------------------------
3078 * <d> Global * Global * No Problem
3079 *------------------------------------------
3080 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3081 *
3082 * And then we add to that what happens if there are multiple addresses
3083 * assigned to an interface. Remember the ifa on a ifn is a linked
3084 * list of addresses. So one interface can have more than one IP
3085 * address. What happens if we have both a private and a global
3086 * address? Do we then use context of destination to sort out which
3087 * one is best? And what about NAT's sending P->G may get you a NAT
3088 * translation, or should you select the G thats on the interface in
3089 * preference.
3090 *
3091 * Decisions:
3092 *
3093 * - count the number of addresses on the interface.
3094 * - if it is one, no problem except case <c>.
3095 * For <a> we will assume a NAT out there.
3096 * - if there are more than one, then we need to worry about scope P
3097 * or G. We should prefer G -> G and P -> P if possible.
3098 * Then as a secondary fall back to mixed types G->P being a last
3099 * ditch one.
3100 * - The above all works for bound all, but bound specific we need to
3101 * use the same concept but instead only consider the bound
3102 * addresses. If the bound set is NOT assigned to the interface then
3103 * we must use rotation amongst the bound addresses..
3104 */
3105 if (ro->ro_rt == NULL) {
3106 /*
3107 * Need a route to cache.
3108 */
3109 SCTP_RTALLOC(ro, vrf_id);
3110 }
3111 if (ro->ro_rt == NULL) {
3112 return (NULL);
3113 }
3114 fam = to->sin_family;
3115 dest_is_priv = dest_is_loop = 0;
3116 /* Setup our scopes for the destination */
3117 switch (fam) {
3118 case AF_INET:
3119 /* Scope based on outbound address */
3120 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3121 dest_is_loop = 1;
3122 if (net != NULL) {
3123 /* mark it as local */
3124 net->addr_is_local = 1;
3125 }
3126 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3127 dest_is_priv = 1;
3128 }
3129 break;
3130#ifdef INET6
3131 case AF_INET6:
3132 /* Scope based on outbound address */
3133 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3134 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3135 /*
3136 * If the address is a loopback address, which
3137 * consists of "::1" OR "fe80::1%lo0", we are
3138 * loopback scope. But we don't use dest_is_priv
3139 * (link local addresses).
3140 */
3141 dest_is_loop = 1;
3142 if (net != NULL) {
3143 /* mark it as local */
3144 net->addr_is_local = 1;
3145 }
3146 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3147 dest_is_priv = 1;
3148 }
3149 break;
3150#endif
3151 }
3152 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3153 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3154 SCTP_IPI_ADDR_RLOCK();
3155 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3156 /*
3157 * Bound all case
3158 */
3159 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3160 dest_is_priv, dest_is_loop,
3161 non_asoc_addr_ok, fam);
3162 SCTP_IPI_ADDR_RUNLOCK();
3163 return (answer);
3164 }
3165 /*
3166 * Subset bound case
3167 */
3168 if (stcb) {
3169 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3170 vrf_id, dest_is_priv,
3171 dest_is_loop,
3172 non_asoc_addr_ok, fam);
3173 } else {
3174 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3175 non_asoc_addr_ok,
3176 dest_is_priv,
3177 dest_is_loop, fam);
3178 }
3179 SCTP_IPI_ADDR_RUNLOCK();
3180 return (answer);
3181}
3182
3183static int
3184sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3185{
3186 struct cmsghdr cmh;
3187 int tlen, at;
3188
3189 tlen = SCTP_BUF_LEN(control);
3190 at = 0;
3191 /*
3192 * Independent of how many mbufs, find the c_type inside the control
3193 * structure and copy out the data.
3194 */
3195 while (at < tlen) {
3196 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3197 /* not enough room for one more we are done. */
3198 return (0);
3199 }
3200 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3201 if (((int)cmh.cmsg_len + at) > tlen) {
3202 /*
3203 * this is real messed up since there is not enough
3204 * data here to cover the cmsg header. We are done.
3205 */
3206 return (0);
3207 }
3208 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3209 (c_type == cmh.cmsg_type)) {
3210 /* found the one we want, copy it out */
3211 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3212 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3213 /*
3214 * space of cmsg_len after header not big
3215 * enough
3216 */
3217 return (0);
3218 }
3219 m_copydata(control, at, cpsize, data);
3220 return (1);
3221 } else {
3222 at += CMSG_ALIGN(cmh.cmsg_len);
3223 if (cmh.cmsg_len == 0) {
3224 break;
3225 }
3226 }
3227 }
3228 /* not found */
3229 return (0);
3230}
3231
3232static struct mbuf *
3233sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3234 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3235{
3236 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3237 struct sctp_state_cookie *stc;
3238 struct sctp_paramhdr *ph;
3239 uint8_t *foo;
3240 int sig_offset;
3241 uint16_t cookie_sz;
3242
3243 mret = NULL;
3244 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3245 sizeof(struct sctp_paramhdr)), 0,
3246 M_DONTWAIT, 1, MT_DATA);
3247 if (mret == NULL) {
3248 return (NULL);
3249 }
3250 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3251 if (copy_init == NULL) {
3252 sctp_m_freem(mret);
3253 return (NULL);
3254 }
3255#ifdef SCTP_MBUF_LOGGING
3256 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3257 struct mbuf *mat;
3258
3259 mat = copy_init;
3260 while (mat) {
3261 if (SCTP_BUF_IS_EXTENDED(mat)) {
3262 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3263 }
3264 mat = SCTP_BUF_NEXT(mat);
3265 }
3266 }
3267#endif
3268 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3269 M_DONTWAIT);
3270 if (copy_initack == NULL) {
3271 sctp_m_freem(mret);
3272 sctp_m_freem(copy_init);
3273 return (NULL);
3274 }
3275#ifdef SCTP_MBUF_LOGGING
3276 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3277 struct mbuf *mat;
3278
3279 mat = copy_initack;
3280 while (mat) {
3281 if (SCTP_BUF_IS_EXTENDED(mat)) {
3282 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3283 }
3284 mat = SCTP_BUF_NEXT(mat);
3285 }
3286 }
3287#endif
3288 /* easy side we just drop it on the end */
3289 ph = mtod(mret, struct sctp_paramhdr *);
3290 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3291 sizeof(struct sctp_paramhdr);
3292 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3293 sizeof(struct sctp_paramhdr));
3294 ph->param_type = htons(SCTP_STATE_COOKIE);
3295 ph->param_length = 0; /* fill in at the end */
3296 /* Fill in the stc cookie data */
3297 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3298
3299 /* tack the INIT and then the INIT-ACK onto the chain */
3300 cookie_sz = 0;
3301 m_at = mret;
3302 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3303 cookie_sz += SCTP_BUF_LEN(m_at);
3304 if (SCTP_BUF_NEXT(m_at) == NULL) {
3305 SCTP_BUF_NEXT(m_at) = copy_init;
3306 break;
3307 }
3308 }
3309
3310 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3311 cookie_sz += SCTP_BUF_LEN(m_at);
3312 if (SCTP_BUF_NEXT(m_at) == NULL) {
3313 SCTP_BUF_NEXT(m_at) = copy_initack;
3314 break;
3315 }
3316 }
3317
3318 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3319 cookie_sz += SCTP_BUF_LEN(m_at);
3320 if (SCTP_BUF_NEXT(m_at) == NULL) {
3321 break;
3322 }
3323 }
3324 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3325 if (sig == NULL) {
3326 /* no space, so free the entire chain */
3327 sctp_m_freem(mret);
3328 return (NULL);
3329 }
3330 SCTP_BUF_LEN(sig) = 0;
3331 SCTP_BUF_NEXT(m_at) = sig;
3332 sig_offset = 0;
3333 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3334 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3335 *signature = foo;
3336 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3337 cookie_sz += SCTP_SIGNATURE_SIZE;
3338 ph->param_length = htons(cookie_sz);
3339 return (mret);
3340}
3341
3342
3343static uint8_t
3344sctp_get_ect(struct sctp_tcb *stcb,
3345 struct sctp_tmit_chunk *chk)
3346{
3347 uint8_t this_random;
3348
3349 /* Huh? */
3350 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3351 return (0);
3352
3353 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3354 /* no nonce, always return ECT0 */
3355 return (SCTP_ECT0_BIT);
3356
3357 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3358 /* Peer does NOT support it, so we send a ECT0 only */
3359 return (SCTP_ECT0_BIT);
3360 }
3361 if (chk == NULL)
3362 return (SCTP_ECT0_BIT);
3363
3364 if ((stcb->asoc.hb_random_idx > 3) ||
3365 ((stcb->asoc.hb_random_idx == 3) &&
3366 (stcb->asoc.hb_ect_randombit > 7))) {
3367 uint32_t rndval;
3368
3369warp_drive_sa:
3370 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3371 memcpy(stcb->asoc.hb_random_values, &rndval,
3372 sizeof(stcb->asoc.hb_random_values));
3373 this_random = stcb->asoc.hb_random_values[0];
3374 stcb->asoc.hb_random_idx = 0;
3375 stcb->asoc.hb_ect_randombit = 0;
3376 } else {
3377 if (stcb->asoc.hb_ect_randombit > 7) {
3378 stcb->asoc.hb_ect_randombit = 0;
3379 stcb->asoc.hb_random_idx++;
3380 if (stcb->asoc.hb_random_idx > 3) {
3381 goto warp_drive_sa;
3382 }
3383 }
3384 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3385 }
3386 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3387 if (chk != NULL)
3388 /* ECN Nonce stuff */
3389 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3390 stcb->asoc.hb_ect_randombit++;
3391 return (SCTP_ECT1_BIT);
3392 } else {
3393 stcb->asoc.hb_ect_randombit++;
3394 return (SCTP_ECT0_BIT);
3395 }
3396}
3397
3398static int
3399sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3400 struct sctp_tcb *stcb, /* may be NULL */
3401 struct sctp_nets *net,
3402 struct sockaddr *to,
3403 struct mbuf *m,
3404 uint32_t auth_offset,
3405 struct sctp_auth_chunk *auth,
3406 uint16_t auth_keyid,
3407 int nofragment_flag,
3408 int ecn_ok,
3409 struct sctp_tmit_chunk *chk,
3410 int out_of_asoc_ok,
3411 uint16_t src_port,
3412 uint16_t dest_port,
3413 uint32_t v_tag,
3414 uint16_t port,
3415 int so_locked,
3416#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3417 SCTP_UNUSED
3418#endif
3419 union sctp_sockstore *over_addr
3420)
3421/* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3422{
3423 /*
3424 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3425 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3426 * structure: - fill in the HMAC digest of any AUTH chunk in the
3427 * packet. - calculate and fill in the SCTP checksum. - prepend an
3428 * IP address header. - if boundall use INADDR_ANY. - if
3429 * boundspecific do source address selection. - set fragmentation
3430 * option for ipV4. - On return from IP output, check/adjust mtu
3431 * size of output interface and smallest_mtu size as well.
3432 */
3433 /* Will need ifdefs around this */
3434 struct mbuf *o_pak;
3435 struct mbuf *newm;
3436 struct sctphdr *sctphdr;
3437 int packet_length;
3438 int ret;
3439 uint32_t vrf_id;
3440 sctp_route_t *ro = NULL;
3441 struct udphdr *udp = NULL;
3442
3443#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3444 struct socket *so = NULL;
3445
3446#endif
3447
3448 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3449 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3450 sctp_m_freem(m);
3451 return (EFAULT);
3452 }
3453 if (stcb) {
3454 vrf_id = stcb->asoc.vrf_id;
3455 } else {
3456 vrf_id = inp->def_vrf_id;
3457 }
3458
3459 /* fill in the HMAC digest for any AUTH chunk in the packet */
3460 if ((auth != NULL) && (stcb != NULL)) {
3461 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3462 }
3463 if (to->sa_family == AF_INET) {
3464 struct ip *ip = NULL;
3465 sctp_route_t iproute;
3466 uint8_t tos_value;
3467 int len;
3468
3469 len = sizeof(struct ip) + sizeof(struct sctphdr);
3470 if (port) {
3471 len += sizeof(struct udphdr);
3472 }
3473 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3474 if (newm == NULL) {
3475 sctp_m_freem(m);
3476 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3477 return (ENOMEM);
3478 }
3479 SCTP_ALIGN_TO_END(newm, len);
3480 SCTP_BUF_LEN(newm) = len;
3481 SCTP_BUF_NEXT(newm) = m;
3482 m = newm;
3483 packet_length = sctp_calculate_len(m);
3484 ip = mtod(m, struct ip *);
3485 ip->ip_v = IPVERSION;
3486 ip->ip_hl = (sizeof(struct ip) >> 2);
3487 if (net) {
3488 tos_value = net->tos_flowlabel & 0x000000ff;
3489 } else {
3490 tos_value = inp->ip_inp.inp.inp_ip_tos;
3491 }
3492 if ((nofragment_flag) && (port == 0)) {
3493#if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3494 ip->ip_off = IP_DF;
3495#else
3496 ip->ip_off = htons(IP_DF);
3497#endif
3498 } else
3499 ip->ip_off = 0;
3500
3501 /* FreeBSD has a function for ip_id's */
3502 ip->ip_id = ip_newid();
3503
3504 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3505 ip->ip_len = packet_length;
3506 if (stcb) {
3507 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3508 /* Enable ECN */
3509 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3510 } else {
3511 /* No ECN */
3512 ip->ip_tos = (u_char)(tos_value & 0xfc);
3513 }
3514 } else {
3515 /* no association at all */
3516 ip->ip_tos = (tos_value & 0xfc);
3517 }
3518 if (port) {
3519 ip->ip_p = IPPROTO_UDP;
3520 } else {
3521 ip->ip_p = IPPROTO_SCTP;
3522 }
3523 ip->ip_sum = 0;
3524 if (net == NULL) {
3525 ro = &iproute;
3526 memset(&iproute, 0, sizeof(iproute));
3527 memcpy(&ro->ro_dst, to, to->sa_len);
3528 } else {
3529 ro = (sctp_route_t *) & net->ro;
3530 }
3531 /* Now the address selection part */
3532 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3533
3534 /* call the routine to select the src address */
3535 if (net && out_of_asoc_ok == 0) {
3536 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3537 sctp_free_ifa(net->ro._s_addr);
3538 net->ro._s_addr = NULL;
3539 net->src_addr_selected = 0;
3540 if (ro->ro_rt) {
3541 RTFREE(ro->ro_rt);
3542 ro->ro_rt = NULL;
3543 }
3544 }
3545 if (net->src_addr_selected == 0) {
3546 /* Cache the source address */
3547 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3548 ro, net, 0,
3549 vrf_id);
3550 net->src_addr_selected = 1;
3551 }
3552 if (net->ro._s_addr == NULL) {
3553 /* No route to host */
3554 net->src_addr_selected = 0;
3555 goto no_route;
3556 }
3557 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3558 } else {
3559 if (over_addr == NULL) {
3560 struct sctp_ifa *_lsrc;
3561
3562 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3563 net,
3564 out_of_asoc_ok,
3565 vrf_id);
3566 if (_lsrc == NULL) {
3567 goto no_route;
3568 }
3569 ip->ip_src = _lsrc->address.sin.sin_addr;
3570 sctp_free_ifa(_lsrc);
3571 } else {
3572 ip->ip_src = over_addr->sin.sin_addr;
3573 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3574 }
3575 }
3576 if (port) {
3577 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3578 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3579 udp->uh_dport = port;
3580 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3581 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3582 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3583 } else {
3584 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3585 }
3586
3587 sctphdr->src_port = src_port;
3588 sctphdr->dest_port = dest_port;
3589 sctphdr->v_tag = v_tag;
3590 sctphdr->checksum = 0;
3591
3592 /*
3593 * If source address selection fails and we find no route
3594 * then the ip_output should fail as well with a
3595 * NO_ROUTE_TO_HOST type error. We probably should catch
3596 * that somewhere and abort the association right away
3597 * (assuming this is an INIT being sent).
3598 */
3599 if ((ro->ro_rt == NULL)) {
3600 /*
3601 * src addr selection failed to find a route (or
3602 * valid source addr), so we can't get there from
3603 * here (yet)!
3604 */
3605 no_route:
3606 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3607 "%s: dropped packet - no valid source addr\n",
3608 __FUNCTION__);
3609 if (net) {
3610 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3611 "Destination was ");
3612 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3613 &net->ro._l_addr.sa);
3614 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3615 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3616 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3617 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3618 stcb,
3619 SCTP_FAILED_THRESHOLD,
3620 (void *)net,
3621 so_locked);
3622 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3623 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3624 /*
3625 * JRS 5/14/07 - If a
3626 * destination is
3627 * unreachable, the PF bit
3628 * is turned off. This
3629 * allows an unambiguous use
3630 * of the PF bit for
3631 * destinations that are
3632 * reachable but potentially
3633 * failed. If the
3634 * destination is set to the
3635 * unreachable state, also
3636 * set the destination to
3637 * the PF state.
3638 */
3639 /*
3640 * Add debug message here if
3641 * destination is not in PF
3642 * state.
3643 */
3644 /*
3645 * Stop any running T3
3646 * timers here?
3647 */
3648 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3649 net->dest_state &= ~SCTP_ADDR_PF;
3650 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3651 net);
3652 }
3653 }
3654 }
3655 if (stcb) {
3656 if (net == stcb->asoc.primary_destination) {
3657 /* need a new primary */
3658 struct sctp_nets *alt;
3659
3660 alt = sctp_find_alternate_net(stcb, net, 0);
3661 if (alt != net) {
3662 if (sctp_set_primary_addr(stcb,
3663 (struct sockaddr *)NULL,
3664 alt) == 0) {
3665 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3666 if (net->ro._s_addr) {
3667 sctp_free_ifa(net->ro._s_addr);
3668 net->ro._s_addr = NULL;
3669 }
3670 net->src_addr_selected = 0;
3671 }
3672 }
3673 }
3674 }
3675 }
3676 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3677 sctp_m_freem(m);
3678 return (EHOSTUNREACH);
3679 }
3680 if (ro != &iproute) {
3681 memcpy(&iproute, ro, sizeof(*ro));
3682 }
3683 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3684 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3685 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3686 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3687 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3688 ro->ro_rt);
3689
3690 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3691 /* failed to prepend data, give up */
3692 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3693 sctp_m_freem(m);
3694 return (ENOMEM);
3695 }
3696#ifdef SCTP_PACKET_LOGGING
3697 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3698 sctp_packet_log(m, packet_length);
3699#endif
3700 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3701 if (port) {
3702 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3703 (stcb) &&
3704 (stcb->asoc.loopback_scope))) {
3705 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3706 SCTP_STAT_INCR(sctps_sendswcrc);
3707 } else {
3708 SCTP_STAT_INCR(sctps_sendnocrc);
3709 }
3710 SCTP_ENABLE_UDP_CSUM(o_pak);
3711 } else {
3712 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3713 (stcb) &&
3714 (stcb->asoc.loopback_scope))) {
3715 m->m_pkthdr.csum_flags = CSUM_SCTP;
3716 m->m_pkthdr.csum_data = 0; /* FIXME MT */
3717 SCTP_STAT_INCR(sctps_sendhwcrc);
3718 } else {
3719 SCTP_STAT_INCR(sctps_sendnocrc);
3720 }
3721 }
3722 /* send it out. table id is taken from stcb */
3723#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 so = SCTP_INP_SO(inp);
3726 SCTP_SOCKET_UNLOCK(so, 0);
3727 }
3728#endif
3729 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3730#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3731 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3732 atomic_add_int(&stcb->asoc.refcnt, 1);
3733 SCTP_TCB_UNLOCK(stcb);
3734 SCTP_SOCKET_LOCK(so, 0);
3735 SCTP_TCB_LOCK(stcb);
3736 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3737 }
3738#endif
3739 SCTP_STAT_INCR(sctps_sendpackets);
3740 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3741 if (ret)
3742 SCTP_STAT_INCR(sctps_senderrors);
3743
3744 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3745 if (net == NULL) {
3746 /* free tempy routes */
3747 if (ro->ro_rt) {
3748 RTFREE(ro->ro_rt);
3749 ro->ro_rt = NULL;
3750 }
3751 } else {
3752 /* PMTU check versus smallest asoc MTU goes here */
3753 if ((ro->ro_rt != NULL) &&
3754 (net->ro._s_addr)) {
3755 uint32_t mtu;
3756
3757 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3758 if (net->port) {
3759 mtu -= sizeof(struct udphdr);
3760 }
3761 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3762#ifdef SCTP_PRINT_FOR_B_AND_M
3763 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3764#endif
3765 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3766 net->mtu = mtu;
3767 }
3768 } else if (ro->ro_rt == NULL) {
3769 /* route was freed */
3770 if (net->ro._s_addr &&
3771 net->src_addr_selected) {
3772 sctp_free_ifa(net->ro._s_addr);
3773 net->ro._s_addr = NULL;
3774 }
3775 net->src_addr_selected = 0;
3776 }
3777 }
3778 return (ret);
3779 }
3780#ifdef INET6
3781 else if (to->sa_family == AF_INET6) {
3782 uint32_t flowlabel;
3783 struct ip6_hdr *ip6h;
3784 struct route_in6 ip6route;
3785 struct ifnet *ifp;
3786 u_char flowTop;
3787 uint16_t flowBottom;
3788 u_char tosBottom, tosTop;
3789 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3790 int prev_scope = 0;
3791 struct sockaddr_in6 lsa6_storage;
3792 int error;
3793 u_short prev_port = 0;
3794 int len;
3795
3796 if (net != NULL) {
3797 flowlabel = net->tos_flowlabel;
3798 } else {
3799 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3800 }
3801
3802 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3803 if (port) {
3804 len += sizeof(struct udphdr);
3805 }
3806 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3807 if (newm == NULL) {
3808 sctp_m_freem(m);
3809 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3810 return (ENOMEM);
3811 }
3812 SCTP_ALIGN_TO_END(newm, len);
3813 SCTP_BUF_LEN(newm) = len;
3814 SCTP_BUF_NEXT(newm) = m;
3815 m = newm;
3816 packet_length = sctp_calculate_len(m);
3817
3818 ip6h = mtod(m, struct ip6_hdr *);
3819 /*
3820 * We assume here that inp_flow is in host byte order within
3821 * the TCB!
3822 */
3823 flowBottom = flowlabel & 0x0000ffff;
3824 flowTop = ((flowlabel & 0x000f0000) >> 16);
3825 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3826 /* protect *sin6 from overwrite */
3827 sin6 = (struct sockaddr_in6 *)to;
3828 tmp = *sin6;
3829 sin6 = &tmp;
3830
3831 /* KAME hack: embed scopeid */
3832 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3832 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3833 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3834 return (EINVAL);
3835 }
3836 if (net == NULL) {
3837 memset(&ip6route, 0, sizeof(ip6route));
3838 ro = (sctp_route_t *) & ip6route;
3839 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3840 } else {
3841 ro = (sctp_route_t *) & net->ro;
3842 }
3843 if (stcb != NULL) {
3844 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3845 /* Enable ECN */
3846 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3847 } else {
3848 /* No ECN */
3849 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3850 }
3851 } else {
3852 /* we could get no asoc if it is a O-O-T-B packet */
3853 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3854 }
3855 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3856 if (port) {
3857 ip6h->ip6_nxt = IPPROTO_UDP;
3858 } else {
3859 ip6h->ip6_nxt = IPPROTO_SCTP;
3860 }
3861 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3862 ip6h->ip6_dst = sin6->sin6_addr;
3863
3864 /*
3865 * Add SRC address selection here: we can only reuse to a
3866 * limited degree the kame src-addr-sel, since we can try
3867 * their selection but it may not be bound.
3868 */
3869 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3870 lsa6_tmp.sin6_family = AF_INET6;
3871 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3872 lsa6 = &lsa6_tmp;
3873 if (net && out_of_asoc_ok == 0) {
3874 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3875 sctp_free_ifa(net->ro._s_addr);
3876 net->ro._s_addr = NULL;
3877 net->src_addr_selected = 0;
3878 if (ro->ro_rt) {
3879 RTFREE(ro->ro_rt);
3880 ro->ro_rt = NULL;
3881 }
3882 }
3883 if (net->src_addr_selected == 0) {
3884 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3885 /* KAME hack: embed scopeid */
3833 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3834 return (EINVAL);
3835 }
3836 if (net == NULL) {
3837 memset(&ip6route, 0, sizeof(ip6route));
3838 ro = (sctp_route_t *) & ip6route;
3839 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3840 } else {
3841 ro = (sctp_route_t *) & net->ro;
3842 }
3843 if (stcb != NULL) {
3844 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3845 /* Enable ECN */
3846 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3847 } else {
3848 /* No ECN */
3849 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3850 }
3851 } else {
3852 /* we could get no asoc if it is a O-O-T-B packet */
3853 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3854 }
3855 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3856 if (port) {
3857 ip6h->ip6_nxt = IPPROTO_UDP;
3858 } else {
3859 ip6h->ip6_nxt = IPPROTO_SCTP;
3860 }
3861 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3862 ip6h->ip6_dst = sin6->sin6_addr;
3863
3864 /*
3865 * Add SRC address selection here: we can only reuse to a
3866 * limited degree the kame src-addr-sel, since we can try
3867 * their selection but it may not be bound.
3868 */
3869 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3870 lsa6_tmp.sin6_family = AF_INET6;
3871 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3872 lsa6 = &lsa6_tmp;
3873 if (net && out_of_asoc_ok == 0) {
3874 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3875 sctp_free_ifa(net->ro._s_addr);
3876 net->ro._s_addr = NULL;
3877 net->src_addr_selected = 0;
3878 if (ro->ro_rt) {
3879 RTFREE(ro->ro_rt);
3880 ro->ro_rt = NULL;
3881 }
3882 }
3883 if (net->src_addr_selected == 0) {
3884 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3885 /* KAME hack: embed scopeid */
3886 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3886 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3887 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3888 return (EINVAL);
3889 }
3890 /* Cache the source address */
3891 net->ro._s_addr = sctp_source_address_selection(inp,
3892 stcb,
3893 ro,
3894 net,
3895 0,
3896 vrf_id);
3897 (void)sa6_recoverscope(sin6);
3898 net->src_addr_selected = 1;
3899 }
3900 if (net->ro._s_addr == NULL) {
3901 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3902 net->src_addr_selected = 0;
3903 goto no_route;
3904 }
3905 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3906 } else {
3907 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3908 /* KAME hack: embed scopeid */
3887 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3888 return (EINVAL);
3889 }
3890 /* Cache the source address */
3891 net->ro._s_addr = sctp_source_address_selection(inp,
3892 stcb,
3893 ro,
3894 net,
3895 0,
3896 vrf_id);
3897 (void)sa6_recoverscope(sin6);
3898 net->src_addr_selected = 1;
3899 }
3900 if (net->ro._s_addr == NULL) {
3901 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3902 net->src_addr_selected = 0;
3903 goto no_route;
3904 }
3905 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3906 } else {
3907 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3908 /* KAME hack: embed scopeid */
3909 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3909 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3910 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3911 return (EINVAL);
3912 }
3913 if (over_addr == NULL) {
3914 struct sctp_ifa *_lsrc;
3915
3916 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3917 net,
3918 out_of_asoc_ok,
3919 vrf_id);
3920 if (_lsrc == NULL) {
3921 goto no_route;
3922 }
3923 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3924 sctp_free_ifa(_lsrc);
3925 } else {
3926 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3927 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3928 }
3929 (void)sa6_recoverscope(sin6);
3930 }
3931 lsa6->sin6_port = inp->sctp_lport;
3932
3933 if (ro->ro_rt == NULL) {
3934 /*
3935 * src addr selection failed to find a route (or
3936 * valid source addr), so we can't get there from
3937 * here!
3938 */
3939 goto no_route;
3940 }
3941 /*
3942 * XXX: sa6 may not have a valid sin6_scope_id in the
3943 * non-SCOPEDROUTING case.
3944 */
3945 bzero(&lsa6_storage, sizeof(lsa6_storage));
3946 lsa6_storage.sin6_family = AF_INET6;
3947 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3948 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3949 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3950 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3951 sctp_m_freem(m);
3952 return (error);
3953 }
3954 /* XXX */
3955 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3956 lsa6_storage.sin6_port = inp->sctp_lport;
3957 lsa6 = &lsa6_storage;
3958 ip6h->ip6_src = lsa6->sin6_addr;
3959
3960 if (port) {
3961 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3962 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3963 udp->uh_dport = port;
3964 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3965 udp->uh_sum = 0;
3966 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3967 } else {
3968 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3969 }
3970
3971 sctphdr->src_port = src_port;
3972 sctphdr->dest_port = dest_port;
3973 sctphdr->v_tag = v_tag;
3974 sctphdr->checksum = 0;
3975
3976 /*
3977 * We set the hop limit now since there is a good chance
3978 * that our ro pointer is now filled
3979 */
3980 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3981 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3982
3983#ifdef SCTP_DEBUG
3984 /* Copy to be sure something bad is not happening */
3985 sin6->sin6_addr = ip6h->ip6_dst;
3986 lsa6->sin6_addr = ip6h->ip6_src;
3987#endif
3988
3989 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3990 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3991 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3992 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3993 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3994 if (net) {
3995 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3996 /* preserve the port and scope for link local send */
3997 prev_scope = sin6->sin6_scope_id;
3998 prev_port = sin6->sin6_port;
3999 }
4000 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4001 /* failed to prepend data, give up */
4002 sctp_m_freem(m);
4003 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4004 return (ENOMEM);
4005 }
4006#ifdef SCTP_PACKET_LOGGING
4007 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4008 sctp_packet_log(m, packet_length);
4009#endif
4010 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4011 if (port) {
4012 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4013 (stcb) &&
4014 (stcb->asoc.loopback_scope))) {
4015 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4016 SCTP_STAT_INCR(sctps_sendswcrc);
4017 } else {
4018 SCTP_STAT_INCR(sctps_sendnocrc);
4019 }
4020 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4021 udp->uh_sum = 0xffff;
4022 }
4023 } else {
4024 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4025 (stcb) &&
4026 (stcb->asoc.loopback_scope))) {
4027 m->m_pkthdr.csum_flags = CSUM_SCTP;
4028 m->m_pkthdr.csum_data = 0; /* FIXME MT */
4029 SCTP_STAT_INCR(sctps_sendhwcrc);
4030 } else {
4031 SCTP_STAT_INCR(sctps_sendnocrc);
4032 }
4033 }
4034 /* send it out. table id is taken from stcb */
4035#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4036 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4037 so = SCTP_INP_SO(inp);
4038 SCTP_SOCKET_UNLOCK(so, 0);
4039 }
4040#endif
4041 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4042#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4043 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4044 atomic_add_int(&stcb->asoc.refcnt, 1);
4045 SCTP_TCB_UNLOCK(stcb);
4046 SCTP_SOCKET_LOCK(so, 0);
4047 SCTP_TCB_LOCK(stcb);
4048 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4049 }
4050#endif
4051 if (net) {
4052 /* for link local this must be done */
4053 sin6->sin6_scope_id = prev_scope;
4054 sin6->sin6_port = prev_port;
4055 }
4056 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4057 SCTP_STAT_INCR(sctps_sendpackets);
4058 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4059 if (ret) {
4060 SCTP_STAT_INCR(sctps_senderrors);
4061 }
4062 if (net == NULL) {
4063 /* Now if we had a temp route free it */
4064 if (ro->ro_rt) {
4065 RTFREE(ro->ro_rt);
4066 }
4067 } else {
4068 /* PMTU check versus smallest asoc MTU goes here */
4069 if (ro->ro_rt == NULL) {
4070 /* Route was freed */
4071 if (net->ro._s_addr &&
4072 net->src_addr_selected) {
4073 sctp_free_ifa(net->ro._s_addr);
4074 net->ro._s_addr = NULL;
4075 }
4076 net->src_addr_selected = 0;
4077 }
4078 if ((ro->ro_rt != NULL) &&
4079 (net->ro._s_addr)) {
4080 uint32_t mtu;
4081
4082 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4083 if (mtu &&
4084 (stcb->asoc.smallest_mtu > mtu)) {
4085#ifdef SCTP_PRINT_FOR_B_AND_M
4086 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4087 mtu);
4088#endif
4089 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4090 net->mtu = mtu;
4091 if (net->port) {
4092 net->mtu -= sizeof(struct udphdr);
4093 }
4094 }
4095 } else if (ifp) {
4096 if (ND_IFINFO(ifp)->linkmtu &&
4097 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4098#ifdef SCTP_PRINT_FOR_B_AND_M
4099 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4100 ND_IFINFO(ifp)->linkmtu);
4101#endif
4102 sctp_mtu_size_reset(inp,
4103 &stcb->asoc,
4104 ND_IFINFO(ifp)->linkmtu);
4105 }
4106 }
4107 }
4108 return (ret);
4109 }
4110#endif
4111 else {
4112 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4113 ((struct sockaddr *)to)->sa_family);
4114 sctp_m_freem(m);
4115 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4116 return (EFAULT);
4117 }
4118}
4119
4120
4121void
4122sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4123#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4124 SCTP_UNUSED
4125#endif
4126)
4127{
4128 struct mbuf *m, *m_at, *mp_last;
4129 struct sctp_nets *net;
4130 struct sctp_init_chunk *init;
4131 struct sctp_supported_addr_param *sup_addr;
4132 struct sctp_adaptation_layer_indication *ali;
4133 struct sctp_ecn_supported_param *ecn;
4134 struct sctp_prsctp_supported_param *prsctp;
4135 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4136 struct sctp_supported_chunk_types_param *pr_supported;
4137 int cnt_inits_to = 0;
4138 int padval, ret;
4139 int num_ext;
4140 int p_len;
4141
4142 /* INIT's always go to the primary (and usually ONLY address) */
4143 mp_last = NULL;
4144 net = stcb->asoc.primary_destination;
4145 if (net == NULL) {
4146 net = TAILQ_FIRST(&stcb->asoc.nets);
4147 if (net == NULL) {
4148 /* TSNH */
4149 return;
4150 }
4151 /* we confirm any address we send an INIT to */
4152 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4153 (void)sctp_set_primary_addr(stcb, NULL, net);
4154 } else {
4155 /* we confirm any address we send an INIT to */
4156 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4157 }
4158 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4159#ifdef INET6
4160 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4161 /*
4162 * special hook, if we are sending to link local it will not
4163 * show up in our private address count.
4164 */
4165 struct sockaddr_in6 *sin6l;
4166
4167 sin6l = &net->ro._l_addr.sin6;
4168 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4169 cnt_inits_to = 1;
4170 }
4171#endif
4172 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4173 /* This case should not happen */
4174 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4175 return;
4176 }
4177 /* start the INIT timer */
4178 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4179
4180 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4181 if (m == NULL) {
4182 /* No memory, INIT timer will re-attempt. */
4183 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4184 return;
4185 }
4186 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4187 /*
4188 * assume peer supports asconf in order to be able to queue local
4189 * address changes while an INIT is in flight and before the assoc
4190 * is established.
4191 */
4192 stcb->asoc.peer_supports_asconf = 1;
4193 /* Now lets put the SCTP header in place */
4194 init = mtod(m, struct sctp_init_chunk *);
4195 /* now the chunk header */
4196 init->ch.chunk_type = SCTP_INITIATION;
4197 init->ch.chunk_flags = 0;
4198 /* fill in later from mbuf we build */
4199 init->ch.chunk_length = 0;
4200 /* place in my tag */
4201 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4202 /* set up some of the credits. */
4203 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4204 SCTP_MINIMAL_RWND));
4205
4206 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4207 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4208 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4209 /* now the address restriction */
4210 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4211 sizeof(*init));
4212 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4213#ifdef INET6
4214 /* we support 2 types: IPv6/IPv4 */
4215 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4216 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4217 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4218#else
4219 /* we support 1 type: IPv4 */
4220 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4221 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4222 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4223#endif
4224 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4225 /* adaptation layer indication parameter */
4226 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4227 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4228 ali->ph.param_length = htons(sizeof(*ali));
4229 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4230 SCTP_BUF_LEN(m) += sizeof(*ali);
4231 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4232
4233 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4234 /* Add NAT friendly parameter */
4235 struct sctp_paramhdr *ph;
4236
4237 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4238 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4239 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4240 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4241 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4242 }
4243 /* now any cookie time extensions */
4244 if (stcb->asoc.cookie_preserve_req) {
4245 struct sctp_cookie_perserve_param *cookie_preserve;
4246
4247 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4248 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4249 cookie_preserve->ph.param_length = htons(
4250 sizeof(*cookie_preserve));
4251 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4252 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4253 ecn = (struct sctp_ecn_supported_param *)(
4254 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4255 stcb->asoc.cookie_preserve_req = 0;
4256 }
4257 /* ECN parameter */
4258 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4259 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4260 ecn->ph.param_length = htons(sizeof(*ecn));
4261 SCTP_BUF_LEN(m) += sizeof(*ecn);
4262 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4263 sizeof(*ecn));
4264 } else {
4265 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4266 }
4267 /* And now tell the peer we do pr-sctp */
4268 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4269 prsctp->ph.param_length = htons(sizeof(*prsctp));
4270 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4271
4272 /* And now tell the peer we do all the extensions */
4273 pr_supported = (struct sctp_supported_chunk_types_param *)
4274 ((caddr_t)prsctp + sizeof(*prsctp));
4275 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4276 num_ext = 0;
4277 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4278 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4279 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4280 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4281 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4282 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4283 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4284 }
4285 /*
4286 * EY if the initiator supports nr_sacks, need to report that to
4287 * responder in INIT chunk
4288 */
4289 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4290 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4291 }
4292 p_len = sizeof(*pr_supported) + num_ext;
4293 pr_supported->ph.param_length = htons(p_len);
4294 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4295 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4296
4297
4298 /* ECN nonce: And now tell the peer we support ECN nonce */
4299 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4300 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4301 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4302 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4303 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4304 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4305 }
4306 /* add authentication parameters */
4307 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4308 struct sctp_auth_random *randp;
4309 struct sctp_auth_hmac_algo *hmacs;
4310 struct sctp_auth_chunk_list *chunks;
4311
4312 /* attach RANDOM parameter, if available */
4313 if (stcb->asoc.authinfo.random != NULL) {
4314 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4315 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4316#ifdef SCTP_AUTH_DRAFT_04
4317 randp->ph.param_type = htons(SCTP_RANDOM);
4318 randp->ph.param_length = htons(p_len);
4319 bcopy(stcb->asoc.authinfo.random->key,
4320 randp->random_data,
4321 stcb->asoc.authinfo.random_len);
4322#else
4323 /* random key already contains the header */
4324 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4325#endif
4326 /* zero out any padding required */
4327 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4328 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4329 }
4330 /* add HMAC_ALGO parameter */
4331 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4332 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4333 (uint8_t *) hmacs->hmac_ids);
4334 if (p_len > 0) {
4335 p_len += sizeof(*hmacs);
4336 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4337 hmacs->ph.param_length = htons(p_len);
4338 /* zero out any padding required */
4339 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4340 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4341 }
4342 /* add CHUNKS parameter */
4343 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4344 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4345 chunks->chunk_types);
4346 if (p_len > 0) {
4347 p_len += sizeof(*chunks);
4348 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4349 chunks->ph.param_length = htons(p_len);
4350 /* zero out any padding required */
4351 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4352 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4353 }
4354 }
4355 m_at = m;
4356 /* now the addresses */
4357 {
4358 struct sctp_scoping scp;
4359
4360 /*
4361 * To optimize this we could put the scoping stuff into a
4362 * structure and remove the individual uint8's from the
4363 * assoc structure. Then we could just sifa in the address
4364 * within the stcb.. but for now this is a quick hack to get
4365 * the address stuff teased apart.
4366 */
4367 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4368 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4369 scp.loopback_scope = stcb->asoc.loopback_scope;
4370 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4371 scp.local_scope = stcb->asoc.local_scope;
4372 scp.site_scope = stcb->asoc.site_scope;
4373
4374 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4375 }
4376
4377 /* calulate the size and update pkt header and chunk header */
4378 p_len = 0;
4379 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4380 if (SCTP_BUF_NEXT(m_at) == NULL)
4381 mp_last = m_at;
4382 p_len += SCTP_BUF_LEN(m_at);
4383 }
4384 init->ch.chunk_length = htons(p_len);
4385 /*
4386 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4387 * here since the timer will drive a retranmission.
4388 */
4389
4390 /* I don't expect this to execute but we will be safe here */
4391 padval = p_len % 4;
4392 if ((padval) && (mp_last)) {
4393 /*
4394 * The compiler worries that mp_last may not be set even
4395 * though I think it is impossible :-> however we add
4396 * mp_last here just in case.
4397 */
4398 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4399 if (ret) {
4400 /* Houston we have a problem, no space */
4401 sctp_m_freem(m);
4402 return;
4403 }
4404 p_len += padval;
4405 }
4406 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4407 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4408 (struct sockaddr *)&net->ro._l_addr,
4409 m, 0, NULL, 0, 0, 0, NULL, 0,
4410 inp->sctp_lport, stcb->rport, htonl(0),
4411 net->port, so_locked, NULL);
4412 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4413 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4414 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4415}
4416
4417struct mbuf *
4418sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4419 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4420{
4421 /*
4422 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4423 * being equal to the beginning of the params i.e. (iphlen +
4424 * sizeof(struct sctp_init_msg) parse through the parameters to the
4425 * end of the mbuf verifying that all parameters are known.
4426 *
4427 * For unknown parameters build and return a mbuf with
4428 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4429 * processing this chunk stop, and set *abort_processing to 1.
4430 *
4431 * By having param_offset be pre-set to where parameters begin it is
4432 * hoped that this routine may be reused in the future by new
4433 * features.
4434 */
4435 struct sctp_paramhdr *phdr, params;
4436
4437 struct mbuf *mat, *op_err;
4438 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4439 int at, limit, pad_needed;
4440 uint16_t ptype, plen, padded_size;
4441 int err_at;
4442
4443 *abort_processing = 0;
4444 mat = in_initpkt;
4445 err_at = 0;
4446 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4447 at = param_offset;
4448 op_err = NULL;
4449 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4450 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4451 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4452 ptype = ntohs(phdr->param_type);
4453 plen = ntohs(phdr->param_length);
4454 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4455 /* wacked parameter */
4456 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4457 goto invalid_size;
4458 }
4459 limit -= SCTP_SIZE32(plen);
4460 /*-
4461 * All parameters for all chunks that we know/understand are
4462 * listed here. We process them other places and make
4463 * appropriate stop actions per the upper bits. However this
4464 * is the generic routine processor's can call to get back
4465 * an operr.. to either incorporate (init-ack) or send.
4466 */
4467 padded_size = SCTP_SIZE32(plen);
4468 switch (ptype) {
4469 /* Param's with variable size */
4470 case SCTP_HEARTBEAT_INFO:
4471 case SCTP_STATE_COOKIE:
4472 case SCTP_UNRECOG_PARAM:
4473 case SCTP_ERROR_CAUSE_IND:
4474 /* ok skip fwd */
4475 at += padded_size;
4476 break;
4477 /* Param's with variable size within a range */
4478 case SCTP_CHUNK_LIST:
4479 case SCTP_SUPPORTED_CHUNK_EXT:
4480 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4481 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4482 goto invalid_size;
4483 }
4484 at += padded_size;
4485 break;
4486 case SCTP_SUPPORTED_ADDRTYPE:
4487 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4488 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4489 goto invalid_size;
4490 }
4491 at += padded_size;
4492 break;
4493 case SCTP_RANDOM:
4494 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4495 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4496 goto invalid_size;
4497 }
4498 at += padded_size;
4499 break;
4500 case SCTP_SET_PRIM_ADDR:
4501 case SCTP_DEL_IP_ADDRESS:
4502 case SCTP_ADD_IP_ADDRESS:
4503 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4504 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4505 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4506 goto invalid_size;
4507 }
4508 at += padded_size;
4509 break;
4510 /* Param's with a fixed size */
4511 case SCTP_IPV4_ADDRESS:
4512 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4513 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4514 goto invalid_size;
4515 }
4516 at += padded_size;
4517 break;
4518 case SCTP_IPV6_ADDRESS:
4519 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4520 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4521 goto invalid_size;
4522 }
4523 at += padded_size;
4524 break;
4525 case SCTP_COOKIE_PRESERVE:
4526 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4527 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4528 goto invalid_size;
4529 }
4530 at += padded_size;
4531 break;
4532 case SCTP_HAS_NAT_SUPPORT:
4533 *nat_friendly = 1;
4534 /* fall through */
4535 case SCTP_ECN_NONCE_SUPPORTED:
4536 case SCTP_PRSCTP_SUPPORTED:
4537
4538 if (padded_size != sizeof(struct sctp_paramhdr)) {
4539 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4540 goto invalid_size;
4541 }
4542 at += padded_size;
4543 break;
4544 case SCTP_ECN_CAPABLE:
4545 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4546 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4547 goto invalid_size;
4548 }
4549 at += padded_size;
4550 break;
4551 case SCTP_ULP_ADAPTATION:
4552 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4553 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4554 goto invalid_size;
4555 }
4556 at += padded_size;
4557 break;
4558 case SCTP_SUCCESS_REPORT:
4559 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4560 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4561 goto invalid_size;
4562 }
4563 at += padded_size;
4564 break;
4565 case SCTP_HOSTNAME_ADDRESS:
4566 {
4567 /* We can NOT handle HOST NAME addresses!! */
4568 int l_len;
4569
4570 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4571 *abort_processing = 1;
4572 if (op_err == NULL) {
4573 /* Ok need to try to get a mbuf */
4574#ifdef INET6
4575 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4576#else
4577 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4578#endif
4579 l_len += plen;
4580 l_len += sizeof(struct sctp_paramhdr);
4581 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4582 if (op_err) {
4583 SCTP_BUF_LEN(op_err) = 0;
4584 /*
4585 * pre-reserve space for ip
4586 * and sctp header and
4587 * chunk hdr
4588 */
4589#ifdef INET6
4590 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4591#else
4592 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4593#endif
4594 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4595 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4596 }
4597 }
4598 if (op_err) {
4599 /* If we have space */
4600 struct sctp_paramhdr s;
4601
4602 if (err_at % 4) {
4603 uint32_t cpthis = 0;
4604
4605 pad_needed = 4 - (err_at % 4);
4606 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4607 err_at += pad_needed;
4608 }
4609 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4610 s.param_length = htons(sizeof(s) + plen);
4611 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4612 err_at += sizeof(s);
4613 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4614 if (phdr == NULL) {
4615 sctp_m_freem(op_err);
4616 /*
4617 * we are out of memory but
4618 * we still need to have a
4619 * look at what to do (the
4620 * system is in trouble
4621 * though).
4622 */
4623 return (NULL);
4624 }
4625 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4626 err_at += plen;
4627 }
4628 return (op_err);
4629 break;
4630 }
4631 default:
4632 /*
4633 * we do not recognize the parameter figure out what
4634 * we do.
4635 */
4636 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4637 if ((ptype & 0x4000) == 0x4000) {
4638 /* Report bit is set?? */
4639 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4640 if (op_err == NULL) {
4641 int l_len;
4642
4643 /* Ok need to try to get an mbuf */
4644#ifdef INET6
4645 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4646#else
4647 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4648#endif
4649 l_len += plen;
4650 l_len += sizeof(struct sctp_paramhdr);
4651 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4652 if (op_err) {
4653 SCTP_BUF_LEN(op_err) = 0;
4654#ifdef INET6
4655 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4656#else
4657 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4658#endif
4659 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4660 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4661 }
4662 }
4663 if (op_err) {
4664 /* If we have space */
4665 struct sctp_paramhdr s;
4666
4667 if (err_at % 4) {
4668 uint32_t cpthis = 0;
4669
4670 pad_needed = 4 - (err_at % 4);
4671 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4672 err_at += pad_needed;
4673 }
4674 s.param_type = htons(SCTP_UNRECOG_PARAM);
4675 s.param_length = htons(sizeof(s) + plen);
4676 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4677 err_at += sizeof(s);
4678 if (plen > sizeof(tempbuf)) {
4679 plen = sizeof(tempbuf);
4680 }
4681 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4682 if (phdr == NULL) {
4683 sctp_m_freem(op_err);
4684 /*
4685 * we are out of memory but
4686 * we still need to have a
4687 * look at what to do (the
4688 * system is in trouble
4689 * though).
4690 */
4691 op_err = NULL;
4692 goto more_processing;
4693 }
4694 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4695 err_at += plen;
4696 }
4697 }
4698 more_processing:
4699 if ((ptype & 0x8000) == 0x0000) {
4700 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4701 return (op_err);
4702 } else {
4703 /* skip this chunk and continue processing */
4704 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4705 at += SCTP_SIZE32(plen);
4706 }
4707 break;
4708
4709 }
4710 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4711 }
4712 return (op_err);
4713invalid_size:
4714 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4715 *abort_processing = 1;
4716 if ((op_err == NULL) && phdr) {
4717 int l_len;
4718
4719#ifdef INET6
4720 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4721#else
4722 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4723#endif
4724 l_len += (2 * sizeof(struct sctp_paramhdr));
4725 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4726 if (op_err) {
4727 SCTP_BUF_LEN(op_err) = 0;
4728#ifdef INET6
4729 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4730#else
4731 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4732#endif
4733 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4734 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4735 }
4736 }
4737 if ((op_err) && phdr) {
4738 struct sctp_paramhdr s;
4739
4740 if (err_at % 4) {
4741 uint32_t cpthis = 0;
4742
4743 pad_needed = 4 - (err_at % 4);
4744 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4745 err_at += pad_needed;
4746 }
4747 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4748 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4749 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4750 err_at += sizeof(s);
4751 /* Only copy back the p-hdr that caused the issue */
4752 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4753 }
4754 return (op_err);
4755}
4756
4757static int
4758sctp_are_there_new_addresses(struct sctp_association *asoc,
4759 struct mbuf *in_initpkt, int iphlen, int offset)
4760{
4761 /*
4762 * Given a INIT packet, look through the packet to verify that there
4763 * are NO new addresses. As we go through the parameters add reports
4764 * of any un-understood parameters that require an error. Also we
4765 * must return (1) to drop the packet if we see a un-understood
4766 * parameter that tells us to drop the chunk.
4767 */
4768 struct sockaddr_in sin4, *sa4;
4769
4770#ifdef INET6
4771 struct sockaddr_in6 sin6, *sa6;
4772
4773#endif
4774 struct sockaddr *sa_touse;
4775 struct sockaddr *sa;
4776 struct sctp_paramhdr *phdr, params;
4777 struct ip *iph;
4778
4779#ifdef INET6
4780 struct ip6_hdr *ip6h;
4781
4782#endif
4783 struct mbuf *mat;
4784 uint16_t ptype, plen;
4785 int err_at;
4786 uint8_t fnd;
4787 struct sctp_nets *net;
4788
4789 memset(&sin4, 0, sizeof(sin4));
4790#ifdef INET6
4791 memset(&sin6, 0, sizeof(sin6));
4792#endif
4793 sin4.sin_family = AF_INET;
4794 sin4.sin_len = sizeof(sin4);
4795#ifdef INET6
4796 sin6.sin6_family = AF_INET6;
4797 sin6.sin6_len = sizeof(sin6);
4798#endif
4799 sa_touse = NULL;
4800 /* First what about the src address of the pkt ? */
4801 iph = mtod(in_initpkt, struct ip *);
4802 switch (iph->ip_v) {
4803 case IPVERSION:
4804 /* source addr is IPv4 */
4805 sin4.sin_addr = iph->ip_src;
4806 sa_touse = (struct sockaddr *)&sin4;
4807 break;
4808#ifdef INET6
4809 case IPV6_VERSION >> 4:
4810 /* source addr is IPv6 */
4811 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4812 sin6.sin6_addr = ip6h->ip6_src;
4813 sa_touse = (struct sockaddr *)&sin6;
4814 break;
4815#endif
4816 default:
4817 return (1);
4818 }
4819
4820 fnd = 0;
4821 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4822 sa = (struct sockaddr *)&net->ro._l_addr;
4823 if (sa->sa_family == sa_touse->sa_family) {
4824 if (sa->sa_family == AF_INET) {
4825 sa4 = (struct sockaddr_in *)sa;
4826 if (sa4->sin_addr.s_addr ==
4827 sin4.sin_addr.s_addr) {
4828 fnd = 1;
4829 break;
4830 }
4831 }
4832#ifdef INET6
4833 if (sa->sa_family == AF_INET6) {
4834 sa6 = (struct sockaddr_in6 *)sa;
4835 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4836 &sin6)) {
4837 fnd = 1;
4838 break;
4839 }
4840 }
4841#endif
4842 }
4843 }
4844 if (fnd == 0) {
4845 /* New address added! no need to look futher. */
4846 return (1);
4847 }
4848 /* Ok so far lets munge through the rest of the packet */
4849 mat = in_initpkt;
4850 err_at = 0;
4851 sa_touse = NULL;
4852 offset += sizeof(struct sctp_init_chunk);
4853 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4854 while (phdr) {
4855 ptype = ntohs(phdr->param_type);
4856 plen = ntohs(phdr->param_length);
4857 if (ptype == SCTP_IPV4_ADDRESS) {
4858 struct sctp_ipv4addr_param *p4, p4_buf;
4859
4860 phdr = sctp_get_next_param(mat, offset,
4861 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4862 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4863 phdr == NULL) {
4864 return (1);
4865 }
4866 p4 = (struct sctp_ipv4addr_param *)phdr;
4867 sin4.sin_addr.s_addr = p4->addr;
4868 sa_touse = (struct sockaddr *)&sin4;
4869 } else if (ptype == SCTP_IPV6_ADDRESS) {
4870 struct sctp_ipv6addr_param *p6, p6_buf;
4871
4872 phdr = sctp_get_next_param(mat, offset,
4873 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4874 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4875 phdr == NULL) {
4876 return (1);
4877 }
4878 p6 = (struct sctp_ipv6addr_param *)phdr;
4879#ifdef INET6
4880 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4881 sizeof(p6->addr));
4882#endif
4883 sa_touse = (struct sockaddr *)&sin4;
4884 }
4885 if (sa_touse) {
4886 /* ok, sa_touse points to one to check */
4887 fnd = 0;
4888 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4889 sa = (struct sockaddr *)&net->ro._l_addr;
4890 if (sa->sa_family != sa_touse->sa_family) {
4891 continue;
4892 }
4893 if (sa->sa_family == AF_INET) {
4894 sa4 = (struct sockaddr_in *)sa;
4895 if (sa4->sin_addr.s_addr ==
4896 sin4.sin_addr.s_addr) {
4897 fnd = 1;
4898 break;
4899 }
4900 }
4901#ifdef INET6
4902 if (sa->sa_family == AF_INET6) {
4903 sa6 = (struct sockaddr_in6 *)sa;
4904 if (SCTP6_ARE_ADDR_EQUAL(
4905 sa6, &sin6)) {
4906 fnd = 1;
4907 break;
4908 }
4909 }
4910#endif
4911 }
4912 if (!fnd) {
4913 /* New addr added! no need to look further */
4914 return (1);
4915 }
4916 }
4917 offset += SCTP_SIZE32(plen);
4918 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4919 }
4920 return (0);
4921}
4922
4923/*
4924 * Given a MBUF chain that was sent into us containing an INIT. Build a
4925 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4926 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4927 * message (i.e. the struct sctp_init_msg).
4928 */
4929void
4930sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4931 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4932 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4933{
4934 struct sctp_association *asoc;
4935 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4936 struct sctp_init_ack_chunk *initack;
4937 struct sctp_adaptation_layer_indication *ali;
4938 struct sctp_ecn_supported_param *ecn;
4939 struct sctp_prsctp_supported_param *prsctp;
4940 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4941 struct sctp_supported_chunk_types_param *pr_supported;
4942 union sctp_sockstore store, store1, *over_addr;
4943 struct sockaddr_in *sin, *to_sin;
4944
4945#ifdef INET6
4946 struct sockaddr_in6 *sin6, *to_sin6;
4947
4948#endif
4949 struct ip *iph;
4950
4951#ifdef INET6
4952 struct ip6_hdr *ip6;
4953
4954#endif
4955 struct sockaddr *to;
4956 struct sctp_state_cookie stc;
4957 struct sctp_nets *net = NULL;
4958 uint8_t *signature = NULL;
4959 int cnt_inits_to = 0;
4960 uint16_t his_limit, i_want;
4961 int abort_flag, padval;
4962 int num_ext;
4963 int p_len;
4964 int nat_friendly = 0;
4965 struct socket *so;
4966
4967 if (stcb)
4968 asoc = &stcb->asoc;
4969 else
4970 asoc = NULL;
4971 mp_last = NULL;
4972 if ((asoc != NULL) &&
4973 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4974 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4975 /* new addresses, out of here in non-cookie-wait states */
4976 /*
4977 * Send a ABORT, we don't add the new address error clause
4978 * though we even set the T bit and copy in the 0 tag.. this
4979 * looks no different than if no listener was present.
4980 */
4981 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4982 return;
4983 }
4984 abort_flag = 0;
4985 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4986 (offset + sizeof(struct sctp_init_chunk)),
4987 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4988 if (abort_flag) {
4989do_a_abort:
4990 sctp_send_abort(init_pkt, iphlen, sh,
4991 init_chk->init.initiate_tag, op_err, vrf_id, port);
4992 return;
4993 }
4994 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4995 if (m == NULL) {
4996 /* No memory, INIT timer will re-attempt. */
4997 if (op_err)
4998 sctp_m_freem(op_err);
4999 return;
5000 }
5001 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5002
5003 /* the time I built cookie */
5004 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5005
5006 /* populate any tie tags */
5007 if (asoc != NULL) {
5008 /* unlock before tag selections */
5009 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5010 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5011 stc.cookie_life = asoc->cookie_life;
5012 net = asoc->primary_destination;
5013 } else {
5014 stc.tie_tag_my_vtag = 0;
5015 stc.tie_tag_peer_vtag = 0;
5016 /* life I will award this cookie */
5017 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5018 }
5019
5020 /* copy in the ports for later check */
5021 stc.myport = sh->dest_port;
5022 stc.peerport = sh->src_port;
5023
5024 /*
5025 * If we wanted to honor cookie life extentions, we would add to
5026 * stc.cookie_life. For now we should NOT honor any extension
5027 */
5028 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5029 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5030 struct inpcb *in_inp;
5031
5032 /* Its a V6 socket */
5033 in_inp = (struct inpcb *)inp;
5034 stc.ipv6_addr_legal = 1;
5035 /* Now look at the binding flag to see if V4 will be legal */
5036 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5037 stc.ipv4_addr_legal = 1;
5038 } else {
5039 /* V4 addresses are NOT legal on the association */
5040 stc.ipv4_addr_legal = 0;
5041 }
5042 } else {
5043 /* Its a V4 socket, no - V6 */
5044 stc.ipv4_addr_legal = 1;
5045 stc.ipv6_addr_legal = 0;
5046 }
5047
5048#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5049 stc.ipv4_scope = 1;
5050#else
5051 stc.ipv4_scope = 0;
5052#endif
5053 /* now for scope setup */
5054 memset((caddr_t)&store, 0, sizeof(store));
5055 memset((caddr_t)&store1, 0, sizeof(store1));
5056 sin = &store.sin;
5057 to_sin = &store1.sin;
5058#ifdef INET6
5059 sin6 = &store.sin6;
5060 to_sin6 = &store1.sin6;
5061#endif
5062 iph = mtod(init_pkt, struct ip *);
5063 /* establish the to_addr's */
5064 switch (iph->ip_v) {
5065 case IPVERSION:
5066 to_sin->sin_port = sh->dest_port;
5067 to_sin->sin_family = AF_INET;
5068 to_sin->sin_len = sizeof(struct sockaddr_in);
5069 to_sin->sin_addr = iph->ip_dst;
5070 break;
5071#ifdef INET6
5072 case IPV6_VERSION >> 4:
5073 ip6 = mtod(init_pkt, struct ip6_hdr *);
5074 to_sin6->sin6_addr = ip6->ip6_dst;
5075 to_sin6->sin6_scope_id = 0;
5076 to_sin6->sin6_port = sh->dest_port;
5077 to_sin6->sin6_family = AF_INET6;
5078 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5079 break;
5080#endif
5081 default:
5082 goto do_a_abort;
5083 break;
5084 };
5085
5086 if (net == NULL) {
5087 to = (struct sockaddr *)&store;
5088 switch (iph->ip_v) {
5089 case IPVERSION:
5090 {
5091 sin->sin_family = AF_INET;
5092 sin->sin_len = sizeof(struct sockaddr_in);
5093 sin->sin_port = sh->src_port;
5094 sin->sin_addr = iph->ip_src;
5095 /* lookup address */
5096 stc.address[0] = sin->sin_addr.s_addr;
5097 stc.address[1] = 0;
5098 stc.address[2] = 0;
5099 stc.address[3] = 0;
5100 stc.addr_type = SCTP_IPV4_ADDRESS;
5101 /* local from address */
5102 stc.laddress[0] = to_sin->sin_addr.s_addr;
5103 stc.laddress[1] = 0;
5104 stc.laddress[2] = 0;
5105 stc.laddress[3] = 0;
5106 stc.laddr_type = SCTP_IPV4_ADDRESS;
5107 /* scope_id is only for v6 */
5108 stc.scope_id = 0;
5109#ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5110 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5111 stc.ipv4_scope = 1;
5112 }
5113#else
5114 stc.ipv4_scope = 1;
5115#endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5116 /* Must use the address in this case */
5117 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5118 stc.loopback_scope = 1;
5119 stc.ipv4_scope = 1;
5120 stc.site_scope = 1;
5121 stc.local_scope = 0;
5122 }
5123 break;
5124 }
5125#ifdef INET6
5126 case IPV6_VERSION >> 4:
5127 {
5128 ip6 = mtod(init_pkt, struct ip6_hdr *);
5129 sin6->sin6_family = AF_INET6;
5130 sin6->sin6_len = sizeof(struct sockaddr_in6);
5131 sin6->sin6_port = sh->src_port;
5132 sin6->sin6_addr = ip6->ip6_src;
5133 /* lookup address */
5134 memcpy(&stc.address, &sin6->sin6_addr,
5135 sizeof(struct in6_addr));
5136 sin6->sin6_scope_id = 0;
5137 stc.addr_type = SCTP_IPV6_ADDRESS;
5138 stc.scope_id = 0;
5139 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5140 /*
5141 * FIX ME: does this have scope from
5142 * rcvif?
5143 */
5144 (void)sa6_recoverscope(sin6);
5145 stc.scope_id = sin6->sin6_scope_id;
3910 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3911 return (EINVAL);
3912 }
3913 if (over_addr == NULL) {
3914 struct sctp_ifa *_lsrc;
3915
3916 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3917 net,
3918 out_of_asoc_ok,
3919 vrf_id);
3920 if (_lsrc == NULL) {
3921 goto no_route;
3922 }
3923 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3924 sctp_free_ifa(_lsrc);
3925 } else {
3926 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3927 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3928 }
3929 (void)sa6_recoverscope(sin6);
3930 }
3931 lsa6->sin6_port = inp->sctp_lport;
3932
3933 if (ro->ro_rt == NULL) {
3934 /*
3935 * src addr selection failed to find a route (or
3936 * valid source addr), so we can't get there from
3937 * here!
3938 */
3939 goto no_route;
3940 }
3941 /*
3942 * XXX: sa6 may not have a valid sin6_scope_id in the
3943 * non-SCOPEDROUTING case.
3944 */
3945 bzero(&lsa6_storage, sizeof(lsa6_storage));
3946 lsa6_storage.sin6_family = AF_INET6;
3947 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3948 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3949 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3950 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3951 sctp_m_freem(m);
3952 return (error);
3953 }
3954 /* XXX */
3955 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3956 lsa6_storage.sin6_port = inp->sctp_lport;
3957 lsa6 = &lsa6_storage;
3958 ip6h->ip6_src = lsa6->sin6_addr;
3959
3960 if (port) {
3961 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3962 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3963 udp->uh_dport = port;
3964 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3965 udp->uh_sum = 0;
3966 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3967 } else {
3968 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3969 }
3970
3971 sctphdr->src_port = src_port;
3972 sctphdr->dest_port = dest_port;
3973 sctphdr->v_tag = v_tag;
3974 sctphdr->checksum = 0;
3975
3976 /*
3977 * We set the hop limit now since there is a good chance
3978 * that our ro pointer is now filled
3979 */
3980 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3981 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3982
3983#ifdef SCTP_DEBUG
3984 /* Copy to be sure something bad is not happening */
3985 sin6->sin6_addr = ip6h->ip6_dst;
3986 lsa6->sin6_addr = ip6h->ip6_src;
3987#endif
3988
3989 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3990 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3991 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3992 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3993 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3994 if (net) {
3995 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3996 /* preserve the port and scope for link local send */
3997 prev_scope = sin6->sin6_scope_id;
3998 prev_port = sin6->sin6_port;
3999 }
4000 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4001 /* failed to prepend data, give up */
4002 sctp_m_freem(m);
4003 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4004 return (ENOMEM);
4005 }
4006#ifdef SCTP_PACKET_LOGGING
4007 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4008 sctp_packet_log(m, packet_length);
4009#endif
4010 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4011 if (port) {
4012 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4013 (stcb) &&
4014 (stcb->asoc.loopback_scope))) {
4015 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4016 SCTP_STAT_INCR(sctps_sendswcrc);
4017 } else {
4018 SCTP_STAT_INCR(sctps_sendnocrc);
4019 }
4020 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4021 udp->uh_sum = 0xffff;
4022 }
4023 } else {
4024 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4025 (stcb) &&
4026 (stcb->asoc.loopback_scope))) {
4027 m->m_pkthdr.csum_flags = CSUM_SCTP;
4028 m->m_pkthdr.csum_data = 0; /* FIXME MT */
4029 SCTP_STAT_INCR(sctps_sendhwcrc);
4030 } else {
4031 SCTP_STAT_INCR(sctps_sendnocrc);
4032 }
4033 }
4034 /* send it out. table id is taken from stcb */
4035#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4036 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4037 so = SCTP_INP_SO(inp);
4038 SCTP_SOCKET_UNLOCK(so, 0);
4039 }
4040#endif
4041 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4042#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4043 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4044 atomic_add_int(&stcb->asoc.refcnt, 1);
4045 SCTP_TCB_UNLOCK(stcb);
4046 SCTP_SOCKET_LOCK(so, 0);
4047 SCTP_TCB_LOCK(stcb);
4048 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4049 }
4050#endif
4051 if (net) {
4052 /* for link local this must be done */
4053 sin6->sin6_scope_id = prev_scope;
4054 sin6->sin6_port = prev_port;
4055 }
4056 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4057 SCTP_STAT_INCR(sctps_sendpackets);
4058 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4059 if (ret) {
4060 SCTP_STAT_INCR(sctps_senderrors);
4061 }
4062 if (net == NULL) {
4063 /* Now if we had a temp route free it */
4064 if (ro->ro_rt) {
4065 RTFREE(ro->ro_rt);
4066 }
4067 } else {
4068 /* PMTU check versus smallest asoc MTU goes here */
4069 if (ro->ro_rt == NULL) {
4070 /* Route was freed */
4071 if (net->ro._s_addr &&
4072 net->src_addr_selected) {
4073 sctp_free_ifa(net->ro._s_addr);
4074 net->ro._s_addr = NULL;
4075 }
4076 net->src_addr_selected = 0;
4077 }
4078 if ((ro->ro_rt != NULL) &&
4079 (net->ro._s_addr)) {
4080 uint32_t mtu;
4081
4082 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4083 if (mtu &&
4084 (stcb->asoc.smallest_mtu > mtu)) {
4085#ifdef SCTP_PRINT_FOR_B_AND_M
4086 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4087 mtu);
4088#endif
4089 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4090 net->mtu = mtu;
4091 if (net->port) {
4092 net->mtu -= sizeof(struct udphdr);
4093 }
4094 }
4095 } else if (ifp) {
4096 if (ND_IFINFO(ifp)->linkmtu &&
4097 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4098#ifdef SCTP_PRINT_FOR_B_AND_M
4099 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4100 ND_IFINFO(ifp)->linkmtu);
4101#endif
4102 sctp_mtu_size_reset(inp,
4103 &stcb->asoc,
4104 ND_IFINFO(ifp)->linkmtu);
4105 }
4106 }
4107 }
4108 return (ret);
4109 }
4110#endif
4111 else {
4112 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4113 ((struct sockaddr *)to)->sa_family);
4114 sctp_m_freem(m);
4115 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4116 return (EFAULT);
4117 }
4118}
4119
4120
4121void
4122sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4123#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4124 SCTP_UNUSED
4125#endif
4126)
4127{
4128 struct mbuf *m, *m_at, *mp_last;
4129 struct sctp_nets *net;
4130 struct sctp_init_chunk *init;
4131 struct sctp_supported_addr_param *sup_addr;
4132 struct sctp_adaptation_layer_indication *ali;
4133 struct sctp_ecn_supported_param *ecn;
4134 struct sctp_prsctp_supported_param *prsctp;
4135 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4136 struct sctp_supported_chunk_types_param *pr_supported;
4137 int cnt_inits_to = 0;
4138 int padval, ret;
4139 int num_ext;
4140 int p_len;
4141
4142 /* INIT's always go to the primary (and usually ONLY address) */
4143 mp_last = NULL;
4144 net = stcb->asoc.primary_destination;
4145 if (net == NULL) {
4146 net = TAILQ_FIRST(&stcb->asoc.nets);
4147 if (net == NULL) {
4148 /* TSNH */
4149 return;
4150 }
4151 /* we confirm any address we send an INIT to */
4152 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4153 (void)sctp_set_primary_addr(stcb, NULL, net);
4154 } else {
4155 /* we confirm any address we send an INIT to */
4156 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4157 }
4158 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4159#ifdef INET6
4160 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4161 /*
4162 * special hook, if we are sending to link local it will not
4163 * show up in our private address count.
4164 */
4165 struct sockaddr_in6 *sin6l;
4166
4167 sin6l = &net->ro._l_addr.sin6;
4168 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4169 cnt_inits_to = 1;
4170 }
4171#endif
4172 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4173 /* This case should not happen */
4174 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4175 return;
4176 }
4177 /* start the INIT timer */
4178 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4179
4180 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4181 if (m == NULL) {
4182 /* No memory, INIT timer will re-attempt. */
4183 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4184 return;
4185 }
4186 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4187 /*
4188 * assume peer supports asconf in order to be able to queue local
4189 * address changes while an INIT is in flight and before the assoc
4190 * is established.
4191 */
4192 stcb->asoc.peer_supports_asconf = 1;
4193 /* Now lets put the SCTP header in place */
4194 init = mtod(m, struct sctp_init_chunk *);
4195 /* now the chunk header */
4196 init->ch.chunk_type = SCTP_INITIATION;
4197 init->ch.chunk_flags = 0;
4198 /* fill in later from mbuf we build */
4199 init->ch.chunk_length = 0;
4200 /* place in my tag */
4201 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4202 /* set up some of the credits. */
4203 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4204 SCTP_MINIMAL_RWND));
4205
4206 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4207 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4208 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4209 /* now the address restriction */
4210 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4211 sizeof(*init));
4212 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4213#ifdef INET6
4214 /* we support 2 types: IPv6/IPv4 */
4215 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4216 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4217 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4218#else
4219 /* we support 1 type: IPv4 */
4220 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4221 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4222 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4223#endif
4224 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4225 /* adaptation layer indication parameter */
4226 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4227 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4228 ali->ph.param_length = htons(sizeof(*ali));
4229 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4230 SCTP_BUF_LEN(m) += sizeof(*ali);
4231 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4232
4233 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4234 /* Add NAT friendly parameter */
4235 struct sctp_paramhdr *ph;
4236
4237 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4238 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4239 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4240 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4241 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4242 }
4243 /* now any cookie time extensions */
4244 if (stcb->asoc.cookie_preserve_req) {
4245 struct sctp_cookie_perserve_param *cookie_preserve;
4246
4247 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4248 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4249 cookie_preserve->ph.param_length = htons(
4250 sizeof(*cookie_preserve));
4251 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4252 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4253 ecn = (struct sctp_ecn_supported_param *)(
4254 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4255 stcb->asoc.cookie_preserve_req = 0;
4256 }
4257 /* ECN parameter */
4258 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4259 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4260 ecn->ph.param_length = htons(sizeof(*ecn));
4261 SCTP_BUF_LEN(m) += sizeof(*ecn);
4262 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4263 sizeof(*ecn));
4264 } else {
4265 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4266 }
4267 /* And now tell the peer we do pr-sctp */
4268 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4269 prsctp->ph.param_length = htons(sizeof(*prsctp));
4270 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4271
4272 /* And now tell the peer we do all the extensions */
4273 pr_supported = (struct sctp_supported_chunk_types_param *)
4274 ((caddr_t)prsctp + sizeof(*prsctp));
4275 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4276 num_ext = 0;
4277 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4278 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4279 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4280 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4281 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4282 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4283 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4284 }
4285 /*
4286 * EY if the initiator supports nr_sacks, need to report that to
4287 * responder in INIT chunk
4288 */
4289 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4290 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4291 }
4292 p_len = sizeof(*pr_supported) + num_ext;
4293 pr_supported->ph.param_length = htons(p_len);
4294 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4295 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4296
4297
4298 /* ECN nonce: And now tell the peer we support ECN nonce */
4299 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4300 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4301 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4302 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4303 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4304 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4305 }
4306 /* add authentication parameters */
4307 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4308 struct sctp_auth_random *randp;
4309 struct sctp_auth_hmac_algo *hmacs;
4310 struct sctp_auth_chunk_list *chunks;
4311
4312 /* attach RANDOM parameter, if available */
4313 if (stcb->asoc.authinfo.random != NULL) {
4314 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4315 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4316#ifdef SCTP_AUTH_DRAFT_04
4317 randp->ph.param_type = htons(SCTP_RANDOM);
4318 randp->ph.param_length = htons(p_len);
4319 bcopy(stcb->asoc.authinfo.random->key,
4320 randp->random_data,
4321 stcb->asoc.authinfo.random_len);
4322#else
4323 /* random key already contains the header */
4324 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4325#endif
4326 /* zero out any padding required */
4327 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4328 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4329 }
4330 /* add HMAC_ALGO parameter */
4331 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4332 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4333 (uint8_t *) hmacs->hmac_ids);
4334 if (p_len > 0) {
4335 p_len += sizeof(*hmacs);
4336 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4337 hmacs->ph.param_length = htons(p_len);
4338 /* zero out any padding required */
4339 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4340 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4341 }
4342 /* add CHUNKS parameter */
4343 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4344 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4345 chunks->chunk_types);
4346 if (p_len > 0) {
4347 p_len += sizeof(*chunks);
4348 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4349 chunks->ph.param_length = htons(p_len);
4350 /* zero out any padding required */
4351 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4352 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4353 }
4354 }
4355 m_at = m;
4356 /* now the addresses */
4357 {
4358 struct sctp_scoping scp;
4359
4360 /*
4361 * To optimize this we could put the scoping stuff into a
4362 * structure and remove the individual uint8's from the
4363 * assoc structure. Then we could just sifa in the address
4364 * within the stcb.. but for now this is a quick hack to get
4365 * the address stuff teased apart.
4366 */
4367 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4368 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4369 scp.loopback_scope = stcb->asoc.loopback_scope;
4370 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4371 scp.local_scope = stcb->asoc.local_scope;
4372 scp.site_scope = stcb->asoc.site_scope;
4373
4374 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4375 }
4376
4377 /* calulate the size and update pkt header and chunk header */
4378 p_len = 0;
4379 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4380 if (SCTP_BUF_NEXT(m_at) == NULL)
4381 mp_last = m_at;
4382 p_len += SCTP_BUF_LEN(m_at);
4383 }
4384 init->ch.chunk_length = htons(p_len);
4385 /*
4386 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4387 * here since the timer will drive a retranmission.
4388 */
4389
4390 /* I don't expect this to execute but we will be safe here */
4391 padval = p_len % 4;
4392 if ((padval) && (mp_last)) {
4393 /*
4394 * The compiler worries that mp_last may not be set even
4395 * though I think it is impossible :-> however we add
4396 * mp_last here just in case.
4397 */
4398 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4399 if (ret) {
4400 /* Houston we have a problem, no space */
4401 sctp_m_freem(m);
4402 return;
4403 }
4404 p_len += padval;
4405 }
4406 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4407 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4408 (struct sockaddr *)&net->ro._l_addr,
4409 m, 0, NULL, 0, 0, 0, NULL, 0,
4410 inp->sctp_lport, stcb->rport, htonl(0),
4411 net->port, so_locked, NULL);
4412 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4413 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4414 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4415}
4416
4417struct mbuf *
4418sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4419 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4420{
4421 /*
4422 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4423 * being equal to the beginning of the params i.e. (iphlen +
4424 * sizeof(struct sctp_init_msg) parse through the parameters to the
4425 * end of the mbuf verifying that all parameters are known.
4426 *
4427 * For unknown parameters build and return a mbuf with
4428 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4429 * processing this chunk stop, and set *abort_processing to 1.
4430 *
4431 * By having param_offset be pre-set to where parameters begin it is
4432 * hoped that this routine may be reused in the future by new
4433 * features.
4434 */
4435 struct sctp_paramhdr *phdr, params;
4436
4437 struct mbuf *mat, *op_err;
4438 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4439 int at, limit, pad_needed;
4440 uint16_t ptype, plen, padded_size;
4441 int err_at;
4442
4443 *abort_processing = 0;
4444 mat = in_initpkt;
4445 err_at = 0;
4446 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4447 at = param_offset;
4448 op_err = NULL;
4449 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4450 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4451 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4452 ptype = ntohs(phdr->param_type);
4453 plen = ntohs(phdr->param_length);
4454 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4455 /* wacked parameter */
4456 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4457 goto invalid_size;
4458 }
4459 limit -= SCTP_SIZE32(plen);
4460 /*-
4461 * All parameters for all chunks that we know/understand are
4462 * listed here. We process them other places and make
4463 * appropriate stop actions per the upper bits. However this
4464 * is the generic routine processor's can call to get back
4465 * an operr.. to either incorporate (init-ack) or send.
4466 */
4467 padded_size = SCTP_SIZE32(plen);
4468 switch (ptype) {
4469 /* Param's with variable size */
4470 case SCTP_HEARTBEAT_INFO:
4471 case SCTP_STATE_COOKIE:
4472 case SCTP_UNRECOG_PARAM:
4473 case SCTP_ERROR_CAUSE_IND:
4474 /* ok skip fwd */
4475 at += padded_size;
4476 break;
4477 /* Param's with variable size within a range */
4478 case SCTP_CHUNK_LIST:
4479 case SCTP_SUPPORTED_CHUNK_EXT:
4480 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4481 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4482 goto invalid_size;
4483 }
4484 at += padded_size;
4485 break;
4486 case SCTP_SUPPORTED_ADDRTYPE:
4487 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4488 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4489 goto invalid_size;
4490 }
4491 at += padded_size;
4492 break;
4493 case SCTP_RANDOM:
4494 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4495 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4496 goto invalid_size;
4497 }
4498 at += padded_size;
4499 break;
4500 case SCTP_SET_PRIM_ADDR:
4501 case SCTP_DEL_IP_ADDRESS:
4502 case SCTP_ADD_IP_ADDRESS:
4503 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4504 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4505 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4506 goto invalid_size;
4507 }
4508 at += padded_size;
4509 break;
4510 /* Param's with a fixed size */
4511 case SCTP_IPV4_ADDRESS:
4512 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4513 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4514 goto invalid_size;
4515 }
4516 at += padded_size;
4517 break;
4518 case SCTP_IPV6_ADDRESS:
4519 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4520 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4521 goto invalid_size;
4522 }
4523 at += padded_size;
4524 break;
4525 case SCTP_COOKIE_PRESERVE:
4526 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4527 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4528 goto invalid_size;
4529 }
4530 at += padded_size;
4531 break;
4532 case SCTP_HAS_NAT_SUPPORT:
4533 *nat_friendly = 1;
4534 /* fall through */
4535 case SCTP_ECN_NONCE_SUPPORTED:
4536 case SCTP_PRSCTP_SUPPORTED:
4537
4538 if (padded_size != sizeof(struct sctp_paramhdr)) {
4539 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4540 goto invalid_size;
4541 }
4542 at += padded_size;
4543 break;
4544 case SCTP_ECN_CAPABLE:
4545 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4546 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4547 goto invalid_size;
4548 }
4549 at += padded_size;
4550 break;
4551 case SCTP_ULP_ADAPTATION:
4552 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4553 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4554 goto invalid_size;
4555 }
4556 at += padded_size;
4557 break;
4558 case SCTP_SUCCESS_REPORT:
4559 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4560 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4561 goto invalid_size;
4562 }
4563 at += padded_size;
4564 break;
4565 case SCTP_HOSTNAME_ADDRESS:
4566 {
4567 /* We can NOT handle HOST NAME addresses!! */
4568 int l_len;
4569
4570 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4571 *abort_processing = 1;
4572 if (op_err == NULL) {
4573 /* Ok need to try to get a mbuf */
4574#ifdef INET6
4575 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4576#else
4577 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4578#endif
4579 l_len += plen;
4580 l_len += sizeof(struct sctp_paramhdr);
4581 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4582 if (op_err) {
4583 SCTP_BUF_LEN(op_err) = 0;
4584 /*
4585 * pre-reserve space for ip
4586 * and sctp header and
4587 * chunk hdr
4588 */
4589#ifdef INET6
4590 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4591#else
4592 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4593#endif
4594 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4595 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4596 }
4597 }
4598 if (op_err) {
4599 /* If we have space */
4600 struct sctp_paramhdr s;
4601
4602 if (err_at % 4) {
4603 uint32_t cpthis = 0;
4604
4605 pad_needed = 4 - (err_at % 4);
4606 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4607 err_at += pad_needed;
4608 }
4609 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4610 s.param_length = htons(sizeof(s) + plen);
4611 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4612 err_at += sizeof(s);
4613 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4614 if (phdr == NULL) {
4615 sctp_m_freem(op_err);
4616 /*
4617 * we are out of memory but
4618 * we still need to have a
4619 * look at what to do (the
4620 * system is in trouble
4621 * though).
4622 */
4623 return (NULL);
4624 }
4625 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4626 err_at += plen;
4627 }
4628 return (op_err);
4629 break;
4630 }
4631 default:
4632 /*
4633 * we do not recognize the parameter figure out what
4634 * we do.
4635 */
4636 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4637 if ((ptype & 0x4000) == 0x4000) {
4638 /* Report bit is set?? */
4639 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4640 if (op_err == NULL) {
4641 int l_len;
4642
4643 /* Ok need to try to get an mbuf */
4644#ifdef INET6
4645 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4646#else
4647 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4648#endif
4649 l_len += plen;
4650 l_len += sizeof(struct sctp_paramhdr);
4651 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4652 if (op_err) {
4653 SCTP_BUF_LEN(op_err) = 0;
4654#ifdef INET6
4655 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4656#else
4657 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4658#endif
4659 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4660 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4661 }
4662 }
4663 if (op_err) {
4664 /* If we have space */
4665 struct sctp_paramhdr s;
4666
4667 if (err_at % 4) {
4668 uint32_t cpthis = 0;
4669
4670 pad_needed = 4 - (err_at % 4);
4671 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4672 err_at += pad_needed;
4673 }
4674 s.param_type = htons(SCTP_UNRECOG_PARAM);
4675 s.param_length = htons(sizeof(s) + plen);
4676 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4677 err_at += sizeof(s);
4678 if (plen > sizeof(tempbuf)) {
4679 plen = sizeof(tempbuf);
4680 }
4681 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4682 if (phdr == NULL) {
4683 sctp_m_freem(op_err);
4684 /*
4685 * we are out of memory but
4686 * we still need to have a
4687 * look at what to do (the
4688 * system is in trouble
4689 * though).
4690 */
4691 op_err = NULL;
4692 goto more_processing;
4693 }
4694 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4695 err_at += plen;
4696 }
4697 }
4698 more_processing:
4699 if ((ptype & 0x8000) == 0x0000) {
4700 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4701 return (op_err);
4702 } else {
4703 /* skip this chunk and continue processing */
4704 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4705 at += SCTP_SIZE32(plen);
4706 }
4707 break;
4708
4709 }
4710 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4711 }
4712 return (op_err);
4713invalid_size:
4714 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4715 *abort_processing = 1;
4716 if ((op_err == NULL) && phdr) {
4717 int l_len;
4718
4719#ifdef INET6
4720 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4721#else
4722 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4723#endif
4724 l_len += (2 * sizeof(struct sctp_paramhdr));
4725 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4726 if (op_err) {
4727 SCTP_BUF_LEN(op_err) = 0;
4728#ifdef INET6
4729 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4730#else
4731 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4732#endif
4733 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4734 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4735 }
4736 }
4737 if ((op_err) && phdr) {
4738 struct sctp_paramhdr s;
4739
4740 if (err_at % 4) {
4741 uint32_t cpthis = 0;
4742
4743 pad_needed = 4 - (err_at % 4);
4744 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4745 err_at += pad_needed;
4746 }
4747 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4748 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4749 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4750 err_at += sizeof(s);
4751 /* Only copy back the p-hdr that caused the issue */
4752 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4753 }
4754 return (op_err);
4755}
4756
4757static int
4758sctp_are_there_new_addresses(struct sctp_association *asoc,
4759 struct mbuf *in_initpkt, int iphlen, int offset)
4760{
4761 /*
4762 * Given a INIT packet, look through the packet to verify that there
4763 * are NO new addresses. As we go through the parameters add reports
4764 * of any un-understood parameters that require an error. Also we
4765 * must return (1) to drop the packet if we see a un-understood
4766 * parameter that tells us to drop the chunk.
4767 */
4768 struct sockaddr_in sin4, *sa4;
4769
4770#ifdef INET6
4771 struct sockaddr_in6 sin6, *sa6;
4772
4773#endif
4774 struct sockaddr *sa_touse;
4775 struct sockaddr *sa;
4776 struct sctp_paramhdr *phdr, params;
4777 struct ip *iph;
4778
4779#ifdef INET6
4780 struct ip6_hdr *ip6h;
4781
4782#endif
4783 struct mbuf *mat;
4784 uint16_t ptype, plen;
4785 int err_at;
4786 uint8_t fnd;
4787 struct sctp_nets *net;
4788
4789 memset(&sin4, 0, sizeof(sin4));
4790#ifdef INET6
4791 memset(&sin6, 0, sizeof(sin6));
4792#endif
4793 sin4.sin_family = AF_INET;
4794 sin4.sin_len = sizeof(sin4);
4795#ifdef INET6
4796 sin6.sin6_family = AF_INET6;
4797 sin6.sin6_len = sizeof(sin6);
4798#endif
4799 sa_touse = NULL;
4800 /* First what about the src address of the pkt ? */
4801 iph = mtod(in_initpkt, struct ip *);
4802 switch (iph->ip_v) {
4803 case IPVERSION:
4804 /* source addr is IPv4 */
4805 sin4.sin_addr = iph->ip_src;
4806 sa_touse = (struct sockaddr *)&sin4;
4807 break;
4808#ifdef INET6
4809 case IPV6_VERSION >> 4:
4810 /* source addr is IPv6 */
4811 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4812 sin6.sin6_addr = ip6h->ip6_src;
4813 sa_touse = (struct sockaddr *)&sin6;
4814 break;
4815#endif
4816 default:
4817 return (1);
4818 }
4819
4820 fnd = 0;
4821 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4822 sa = (struct sockaddr *)&net->ro._l_addr;
4823 if (sa->sa_family == sa_touse->sa_family) {
4824 if (sa->sa_family == AF_INET) {
4825 sa4 = (struct sockaddr_in *)sa;
4826 if (sa4->sin_addr.s_addr ==
4827 sin4.sin_addr.s_addr) {
4828 fnd = 1;
4829 break;
4830 }
4831 }
4832#ifdef INET6
4833 if (sa->sa_family == AF_INET6) {
4834 sa6 = (struct sockaddr_in6 *)sa;
4835 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4836 &sin6)) {
4837 fnd = 1;
4838 break;
4839 }
4840 }
4841#endif
4842 }
4843 }
4844 if (fnd == 0) {
4845 /* New address added! no need to look futher. */
4846 return (1);
4847 }
4848 /* Ok so far lets munge through the rest of the packet */
4849 mat = in_initpkt;
4850 err_at = 0;
4851 sa_touse = NULL;
4852 offset += sizeof(struct sctp_init_chunk);
4853 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4854 while (phdr) {
4855 ptype = ntohs(phdr->param_type);
4856 plen = ntohs(phdr->param_length);
4857 if (ptype == SCTP_IPV4_ADDRESS) {
4858 struct sctp_ipv4addr_param *p4, p4_buf;
4859
4860 phdr = sctp_get_next_param(mat, offset,
4861 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4862 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4863 phdr == NULL) {
4864 return (1);
4865 }
4866 p4 = (struct sctp_ipv4addr_param *)phdr;
4867 sin4.sin_addr.s_addr = p4->addr;
4868 sa_touse = (struct sockaddr *)&sin4;
4869 } else if (ptype == SCTP_IPV6_ADDRESS) {
4870 struct sctp_ipv6addr_param *p6, p6_buf;
4871
4872 phdr = sctp_get_next_param(mat, offset,
4873 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4874 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4875 phdr == NULL) {
4876 return (1);
4877 }
4878 p6 = (struct sctp_ipv6addr_param *)phdr;
4879#ifdef INET6
4880 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4881 sizeof(p6->addr));
4882#endif
4883 sa_touse = (struct sockaddr *)&sin4;
4884 }
4885 if (sa_touse) {
4886 /* ok, sa_touse points to one to check */
4887 fnd = 0;
4888 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4889 sa = (struct sockaddr *)&net->ro._l_addr;
4890 if (sa->sa_family != sa_touse->sa_family) {
4891 continue;
4892 }
4893 if (sa->sa_family == AF_INET) {
4894 sa4 = (struct sockaddr_in *)sa;
4895 if (sa4->sin_addr.s_addr ==
4896 sin4.sin_addr.s_addr) {
4897 fnd = 1;
4898 break;
4899 }
4900 }
4901#ifdef INET6
4902 if (sa->sa_family == AF_INET6) {
4903 sa6 = (struct sockaddr_in6 *)sa;
4904 if (SCTP6_ARE_ADDR_EQUAL(
4905 sa6, &sin6)) {
4906 fnd = 1;
4907 break;
4908 }
4909 }
4910#endif
4911 }
4912 if (!fnd) {
4913 /* New addr added! no need to look further */
4914 return (1);
4915 }
4916 }
4917 offset += SCTP_SIZE32(plen);
4918 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4919 }
4920 return (0);
4921}
4922
4923/*
4924 * Given a MBUF chain that was sent into us containing an INIT. Build a
4925 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4926 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4927 * message (i.e. the struct sctp_init_msg).
4928 */
4929void
4930sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4931 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4932 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4933{
4934 struct sctp_association *asoc;
4935 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4936 struct sctp_init_ack_chunk *initack;
4937 struct sctp_adaptation_layer_indication *ali;
4938 struct sctp_ecn_supported_param *ecn;
4939 struct sctp_prsctp_supported_param *prsctp;
4940 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4941 struct sctp_supported_chunk_types_param *pr_supported;
4942 union sctp_sockstore store, store1, *over_addr;
4943 struct sockaddr_in *sin, *to_sin;
4944
4945#ifdef INET6
4946 struct sockaddr_in6 *sin6, *to_sin6;
4947
4948#endif
4949 struct ip *iph;
4950
4951#ifdef INET6
4952 struct ip6_hdr *ip6;
4953
4954#endif
4955 struct sockaddr *to;
4956 struct sctp_state_cookie stc;
4957 struct sctp_nets *net = NULL;
4958 uint8_t *signature = NULL;
4959 int cnt_inits_to = 0;
4960 uint16_t his_limit, i_want;
4961 int abort_flag, padval;
4962 int num_ext;
4963 int p_len;
4964 int nat_friendly = 0;
4965 struct socket *so;
4966
4967 if (stcb)
4968 asoc = &stcb->asoc;
4969 else
4970 asoc = NULL;
4971 mp_last = NULL;
4972 if ((asoc != NULL) &&
4973 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4974 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4975 /* new addresses, out of here in non-cookie-wait states */
4976 /*
4977 * Send a ABORT, we don't add the new address error clause
4978 * though we even set the T bit and copy in the 0 tag.. this
4979 * looks no different than if no listener was present.
4980 */
4981 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4982 return;
4983 }
4984 abort_flag = 0;
4985 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4986 (offset + sizeof(struct sctp_init_chunk)),
4987 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4988 if (abort_flag) {
4989do_a_abort:
4990 sctp_send_abort(init_pkt, iphlen, sh,
4991 init_chk->init.initiate_tag, op_err, vrf_id, port);
4992 return;
4993 }
4994 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4995 if (m == NULL) {
4996 /* No memory, INIT timer will re-attempt. */
4997 if (op_err)
4998 sctp_m_freem(op_err);
4999 return;
5000 }
5001 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5002
5003 /* the time I built cookie */
5004 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5005
5006 /* populate any tie tags */
5007 if (asoc != NULL) {
5008 /* unlock before tag selections */
5009 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5010 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5011 stc.cookie_life = asoc->cookie_life;
5012 net = asoc->primary_destination;
5013 } else {
5014 stc.tie_tag_my_vtag = 0;
5015 stc.tie_tag_peer_vtag = 0;
5016 /* life I will award this cookie */
5017 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5018 }
5019
5020 /* copy in the ports for later check */
5021 stc.myport = sh->dest_port;
5022 stc.peerport = sh->src_port;
5023
5024 /*
5025 * If we wanted to honor cookie life extentions, we would add to
5026 * stc.cookie_life. For now we should NOT honor any extension
5027 */
5028 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5029 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5030 struct inpcb *in_inp;
5031
5032 /* Its a V6 socket */
5033 in_inp = (struct inpcb *)inp;
5034 stc.ipv6_addr_legal = 1;
5035 /* Now look at the binding flag to see if V4 will be legal */
5036 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5037 stc.ipv4_addr_legal = 1;
5038 } else {
5039 /* V4 addresses are NOT legal on the association */
5040 stc.ipv4_addr_legal = 0;
5041 }
5042 } else {
5043 /* Its a V4 socket, no - V6 */
5044 stc.ipv4_addr_legal = 1;
5045 stc.ipv6_addr_legal = 0;
5046 }
5047
5048#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5049 stc.ipv4_scope = 1;
5050#else
5051 stc.ipv4_scope = 0;
5052#endif
5053 /* now for scope setup */
5054 memset((caddr_t)&store, 0, sizeof(store));
5055 memset((caddr_t)&store1, 0, sizeof(store1));
5056 sin = &store.sin;
5057 to_sin = &store1.sin;
5058#ifdef INET6
5059 sin6 = &store.sin6;
5060 to_sin6 = &store1.sin6;
5061#endif
5062 iph = mtod(init_pkt, struct ip *);
5063 /* establish the to_addr's */
5064 switch (iph->ip_v) {
5065 case IPVERSION:
5066 to_sin->sin_port = sh->dest_port;
5067 to_sin->sin_family = AF_INET;
5068 to_sin->sin_len = sizeof(struct sockaddr_in);
5069 to_sin->sin_addr = iph->ip_dst;
5070 break;
5071#ifdef INET6
5072 case IPV6_VERSION >> 4:
5073 ip6 = mtod(init_pkt, struct ip6_hdr *);
5074 to_sin6->sin6_addr = ip6->ip6_dst;
5075 to_sin6->sin6_scope_id = 0;
5076 to_sin6->sin6_port = sh->dest_port;
5077 to_sin6->sin6_family = AF_INET6;
5078 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5079 break;
5080#endif
5081 default:
5082 goto do_a_abort;
5083 break;
5084 };
5085
5086 if (net == NULL) {
5087 to = (struct sockaddr *)&store;
5088 switch (iph->ip_v) {
5089 case IPVERSION:
5090 {
5091 sin->sin_family = AF_INET;
5092 sin->sin_len = sizeof(struct sockaddr_in);
5093 sin->sin_port = sh->src_port;
5094 sin->sin_addr = iph->ip_src;
5095 /* lookup address */
5096 stc.address[0] = sin->sin_addr.s_addr;
5097 stc.address[1] = 0;
5098 stc.address[2] = 0;
5099 stc.address[3] = 0;
5100 stc.addr_type = SCTP_IPV4_ADDRESS;
5101 /* local from address */
5102 stc.laddress[0] = to_sin->sin_addr.s_addr;
5103 stc.laddress[1] = 0;
5104 stc.laddress[2] = 0;
5105 stc.laddress[3] = 0;
5106 stc.laddr_type = SCTP_IPV4_ADDRESS;
5107 /* scope_id is only for v6 */
5108 stc.scope_id = 0;
5109#ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5110 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5111 stc.ipv4_scope = 1;
5112 }
5113#else
5114 stc.ipv4_scope = 1;
5115#endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5116 /* Must use the address in this case */
5117 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5118 stc.loopback_scope = 1;
5119 stc.ipv4_scope = 1;
5120 stc.site_scope = 1;
5121 stc.local_scope = 0;
5122 }
5123 break;
5124 }
5125#ifdef INET6
5126 case IPV6_VERSION >> 4:
5127 {
5128 ip6 = mtod(init_pkt, struct ip6_hdr *);
5129 sin6->sin6_family = AF_INET6;
5130 sin6->sin6_len = sizeof(struct sockaddr_in6);
5131 sin6->sin6_port = sh->src_port;
5132 sin6->sin6_addr = ip6->ip6_src;
5133 /* lookup address */
5134 memcpy(&stc.address, &sin6->sin6_addr,
5135 sizeof(struct in6_addr));
5136 sin6->sin6_scope_id = 0;
5137 stc.addr_type = SCTP_IPV6_ADDRESS;
5138 stc.scope_id = 0;
5139 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5140 /*
5141 * FIX ME: does this have scope from
5142 * rcvif?
5143 */
5144 (void)sa6_recoverscope(sin6);
5145 stc.scope_id = sin6->sin6_scope_id;
5146 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5146 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5147 stc.loopback_scope = 1;
5148 stc.local_scope = 0;
5149 stc.site_scope = 1;
5150 stc.ipv4_scope = 1;
5151 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5152 /*
5153 * If the new destination is a
5154 * LINK_LOCAL we must have common
5155 * both site and local scope. Don't
5156 * set local scope though since we
5157 * must depend on the source to be
5158 * added implicitly. We cannot
5159 * assure just because we share one
5160 * link that all links are common.
5161 */
5162 stc.local_scope = 0;
5163 stc.site_scope = 1;
5164 stc.ipv4_scope = 1;
5165 /*
5166 * we start counting for the private
5167 * address stuff at 1. since the
5168 * link local we source from won't
5169 * show up in our scoped count.
5170 */
5171 cnt_inits_to = 1;
5172 /*
5173 * pull out the scope_id from
5174 * incoming pkt
5175 */
5176 /*
5177 * FIX ME: does this have scope from
5178 * rcvif?
5179 */
5180 (void)sa6_recoverscope(sin6);
5181 stc.scope_id = sin6->sin6_scope_id;
5147 stc.loopback_scope = 1;
5148 stc.local_scope = 0;
5149 stc.site_scope = 1;
5150 stc.ipv4_scope = 1;
5151 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5152 /*
5153 * If the new destination is a
5154 * LINK_LOCAL we must have common
5155 * both site and local scope. Don't
5156 * set local scope though since we
5157 * must depend on the source to be
5158 * added implicitly. We cannot
5159 * assure just because we share one
5160 * link that all links are common.
5161 */
5162 stc.local_scope = 0;
5163 stc.site_scope = 1;
5164 stc.ipv4_scope = 1;
5165 /*
5166 * we start counting for the private
5167 * address stuff at 1. since the
5168 * link local we source from won't
5169 * show up in our scoped count.
5170 */
5171 cnt_inits_to = 1;
5172 /*
5173 * pull out the scope_id from
5174 * incoming pkt
5175 */
5176 /*
5177 * FIX ME: does this have scope from
5178 * rcvif?
5179 */
5180 (void)sa6_recoverscope(sin6);
5181 stc.scope_id = sin6->sin6_scope_id;
5182 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5182 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5183 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5184 /*
5185 * If the new destination is
5186 * SITE_LOCAL then we must have site
5187 * scope in common.
5188 */
5189 stc.site_scope = 1;
5190 }
5191 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5192 stc.laddr_type = SCTP_IPV6_ADDRESS;
5193 break;
5194 }
5195#endif
5196 default:
5197 /* TSNH */
5198 goto do_a_abort;
5199 break;
5200 }
5201 } else {
5202 /* set the scope per the existing tcb */
5203
5204#ifdef INET6
5205 struct sctp_nets *lnet;
5206
5207#endif
5208
5209 stc.loopback_scope = asoc->loopback_scope;
5210 stc.ipv4_scope = asoc->ipv4_local_scope;
5211 stc.site_scope = asoc->site_scope;
5212 stc.local_scope = asoc->local_scope;
5213#ifdef INET6
5214 /* Why do we not consider IPv4 LL addresses? */
5215 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5216 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5217 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5218 /*
5219 * if we have a LL address, start
5220 * counting at 1.
5221 */
5222 cnt_inits_to = 1;
5223 }
5224 }
5225 }
5226#endif
5227 /* use the net pointer */
5228 to = (struct sockaddr *)&net->ro._l_addr;
5229 switch (to->sa_family) {
5230 case AF_INET:
5231 sin = (struct sockaddr_in *)to;
5232 stc.address[0] = sin->sin_addr.s_addr;
5233 stc.address[1] = 0;
5234 stc.address[2] = 0;
5235 stc.address[3] = 0;
5236 stc.addr_type = SCTP_IPV4_ADDRESS;
5237 if (net->src_addr_selected == 0) {
5238 /*
5239 * strange case here, the INIT should have
5240 * did the selection.
5241 */
5242 net->ro._s_addr = sctp_source_address_selection(inp,
5243 stcb, (sctp_route_t *) & net->ro,
5244 net, 0, vrf_id);
5245 if (net->ro._s_addr == NULL)
5246 return;
5247
5248 net->src_addr_selected = 1;
5249
5250 }
5251 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5252 stc.laddress[1] = 0;
5253 stc.laddress[2] = 0;
5254 stc.laddress[3] = 0;
5255 stc.laddr_type = SCTP_IPV4_ADDRESS;
5256 break;
5257#ifdef INET6
5258 case AF_INET6:
5259 sin6 = (struct sockaddr_in6 *)to;
5260 memcpy(&stc.address, &sin6->sin6_addr,
5261 sizeof(struct in6_addr));
5262 stc.addr_type = SCTP_IPV6_ADDRESS;
5263 if (net->src_addr_selected == 0) {
5264 /*
5265 * strange case here, the INIT should have
5266 * did the selection.
5267 */
5268 net->ro._s_addr = sctp_source_address_selection(inp,
5269 stcb, (sctp_route_t *) & net->ro,
5270 net, 0, vrf_id);
5271 if (net->ro._s_addr == NULL)
5272 return;
5273
5274 net->src_addr_selected = 1;
5275 }
5276 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5277 sizeof(struct in6_addr));
5278 stc.laddr_type = SCTP_IPV6_ADDRESS;
5279 break;
5280#endif
5281 }
5282 }
5283 /* Now lets put the SCTP header in place */
5284 initack = mtod(m, struct sctp_init_ack_chunk *);
5285 /* Save it off for quick ref */
5286 stc.peers_vtag = init_chk->init.initiate_tag;
5287 /* who are we */
5288 memcpy(stc.identification, SCTP_VERSION_STRING,
5289 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5290 /* now the chunk header */
5291 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5292 initack->ch.chunk_flags = 0;
5293 /* fill in later from mbuf we build */
5294 initack->ch.chunk_length = 0;
5295 /* place in my tag */
5296 if ((asoc != NULL) &&
5297 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5298 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5299 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5300 /* re-use the v-tags and init-seq here */
5301 initack->init.initiate_tag = htonl(asoc->my_vtag);
5302 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5303 } else {
5304 uint32_t vtag, itsn;
5305
5306 if (hold_inp_lock) {
5307 SCTP_INP_INCR_REF(inp);
5308 SCTP_INP_RUNLOCK(inp);
5309 }
5310 if (asoc) {
5311 atomic_add_int(&asoc->refcnt, 1);
5312 SCTP_TCB_UNLOCK(stcb);
5313 new_tag:
5314 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5315 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5316 /*
5317 * Got a duplicate vtag on some guy behind a
5318 * nat make sure we don't use it.
5319 */
5320 goto new_tag;
5321 }
5322 initack->init.initiate_tag = htonl(vtag);
5323 /* get a TSN to use too */
5324 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5325 initack->init.initial_tsn = htonl(itsn);
5326 SCTP_TCB_LOCK(stcb);
5327 atomic_add_int(&asoc->refcnt, -1);
5328 } else {
5329 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5330 initack->init.initiate_tag = htonl(vtag);
5331 /* get a TSN to use too */
5332 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5333 }
5334 if (hold_inp_lock) {
5335 SCTP_INP_RLOCK(inp);
5336 SCTP_INP_DECR_REF(inp);
5337 }
5338 }
5339 /* save away my tag to */
5340 stc.my_vtag = initack->init.initiate_tag;
5341
5342 /* set up some of the credits. */
5343 so = inp->sctp_socket;
5344 if (so == NULL) {
5345 /* memory problem */
5346 sctp_m_freem(m);
5347 return;
5348 } else {
5349 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5350 }
5351 /* set what I want */
5352 his_limit = ntohs(init_chk->init.num_inbound_streams);
5353 /* choose what I want */
5354 if (asoc != NULL) {
5355 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5356 i_want = asoc->streamoutcnt;
5357 } else {
5358 i_want = inp->sctp_ep.pre_open_stream_count;
5359 }
5360 } else {
5361 i_want = inp->sctp_ep.pre_open_stream_count;
5362 }
5363 if (his_limit < i_want) {
5364 /* I Want more :< */
5365 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5366 } else {
5367 /* I can have what I want :> */
5368 initack->init.num_outbound_streams = htons(i_want);
5369 }
5370 /* tell him his limt. */
5371 initack->init.num_inbound_streams =
5372 htons(inp->sctp_ep.max_open_streams_intome);
5373
5374 /* adaptation layer indication parameter */
5375 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5376 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5377 ali->ph.param_length = htons(sizeof(*ali));
5378 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5379 SCTP_BUF_LEN(m) += sizeof(*ali);
5380 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5381
5382 /* ECN parameter */
5383 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5384 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5385 ecn->ph.param_length = htons(sizeof(*ecn));
5386 SCTP_BUF_LEN(m) += sizeof(*ecn);
5387
5388 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5389 sizeof(*ecn));
5390 } else {
5391 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5392 }
5393 /* And now tell the peer we do pr-sctp */
5394 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5395 prsctp->ph.param_length = htons(sizeof(*prsctp));
5396 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5397 if (nat_friendly) {
5398 /* Add NAT friendly parameter */
5399 struct sctp_paramhdr *ph;
5400
5401 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5402 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5403 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5404 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5405 }
5406 /* And now tell the peer we do all the extensions */
5407 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5408 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5409 num_ext = 0;
5410 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5411 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5412 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5413 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5414 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5415 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5416 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5417 /*
5418 * EY if the sysctl variable is set, tell the assoc. initiator that
5419 * we do nr_sack
5420 */
5421 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5422 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5423 p_len = sizeof(*pr_supported) + num_ext;
5424 pr_supported->ph.param_length = htons(p_len);
5425 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5426 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5427
5428 /* ECN nonce: And now tell the peer we support ECN nonce */
5429 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5430 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5431 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5432 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5433 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5434 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5435 }
5436 /* add authentication parameters */
5437 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5438 struct sctp_auth_random *randp;
5439 struct sctp_auth_hmac_algo *hmacs;
5440 struct sctp_auth_chunk_list *chunks;
5441 uint16_t random_len;
5442
5443 /* generate and add RANDOM parameter */
5444 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5445 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5446 randp->ph.param_type = htons(SCTP_RANDOM);
5447 p_len = sizeof(*randp) + random_len;
5448 randp->ph.param_length = htons(p_len);
5449 SCTP_READ_RANDOM(randp->random_data, random_len);
5450 /* zero out any padding required */
5451 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5452 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5453
5454 /* add HMAC_ALGO parameter */
5455 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5456 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5457 (uint8_t *) hmacs->hmac_ids);
5458 if (p_len > 0) {
5459 p_len += sizeof(*hmacs);
5460 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5461 hmacs->ph.param_length = htons(p_len);
5462 /* zero out any padding required */
5463 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5464 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5465 }
5466 /* add CHUNKS parameter */
5467 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5468 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5469 chunks->chunk_types);
5470 if (p_len > 0) {
5471 p_len += sizeof(*chunks);
5472 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5473 chunks->ph.param_length = htons(p_len);
5474 /* zero out any padding required */
5475 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5476 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5477 }
5478 }
5479 m_at = m;
5480 /* now the addresses */
5481 {
5482 struct sctp_scoping scp;
5483
5484 /*
5485 * To optimize this we could put the scoping stuff into a
5486 * structure and remove the individual uint8's from the stc
5487 * structure. Then we could just sifa in the address within
5488 * the stc.. but for now this is a quick hack to get the
5489 * address stuff teased apart.
5490 */
5491 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5492 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5493 scp.loopback_scope = stc.loopback_scope;
5494 scp.ipv4_local_scope = stc.ipv4_scope;
5495 scp.local_scope = stc.local_scope;
5496 scp.site_scope = stc.site_scope;
5497 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5498 }
5499
5500 /* tack on the operational error if present */
5501 if (op_err) {
5502 struct mbuf *ol;
5503 int llen;
5504
5505 llen = 0;
5506 ol = op_err;
5507 while (ol) {
5508 llen += SCTP_BUF_LEN(ol);
5509 ol = SCTP_BUF_NEXT(ol);
5510 }
5511 if (llen % 4) {
5512 /* must add a pad to the param */
5513 uint32_t cpthis = 0;
5514 int padlen;
5515
5516 padlen = 4 - (llen % 4);
5517 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5518 }
5519 while (SCTP_BUF_NEXT(m_at) != NULL) {
5520 m_at = SCTP_BUF_NEXT(m_at);
5521 }
5522 SCTP_BUF_NEXT(m_at) = op_err;
5523 while (SCTP_BUF_NEXT(m_at) != NULL) {
5524 m_at = SCTP_BUF_NEXT(m_at);
5525 }
5526 }
5527 /* pre-calulate the size and update pkt header and chunk header */
5528 p_len = 0;
5529 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5530 p_len += SCTP_BUF_LEN(m_tmp);
5531 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5532 /* m_tmp should now point to last one */
5533 break;
5534 }
5535 }
5536
5537 /* Now we must build a cookie */
5538 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5539 if (m_cookie == NULL) {
5540 /* memory problem */
5541 sctp_m_freem(m);
5542 return;
5543 }
5544 /* Now append the cookie to the end and update the space/size */
5545 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5546
5547 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5548 p_len += SCTP_BUF_LEN(m_tmp);
5549 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5550 /* m_tmp should now point to last one */
5551 mp_last = m_tmp;
5552 break;
5553 }
5554 }
5555 /*
5556 * Place in the size, but we don't include the last pad (if any) in
5557 * the INIT-ACK.
5558 */
5559 initack->ch.chunk_length = htons(p_len);
5560
5561 /*
5562 * Time to sign the cookie, we don't sign over the cookie signature
5563 * though thus we set trailer.
5564 */
5565 (void)sctp_hmac_m(SCTP_HMAC,
5566 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5567 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5568 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5569 /*
5570 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5571 * here since the timer will drive a retranmission.
5572 */
5573 padval = p_len % 4;
5574 if ((padval) && (mp_last)) {
5575 /* see my previous comments on mp_last */
5576 int ret;
5577
5578 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5579 if (ret) {
5580 /* Houston we have a problem, no space */
5581 sctp_m_freem(m);
5582 return;
5583 }
5584 p_len += padval;
5585 }
5586 if (stc.loopback_scope) {
5587 over_addr = &store1;
5588 } else {
5589 over_addr = NULL;
5590 }
5591
5592 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5593 0, NULL, 0,
5594 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5595 port, SCTP_SO_NOT_LOCKED, over_addr);
5596 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5597}
5598
5599
5600void
5601sctp_insert_on_wheel(struct sctp_tcb *stcb,
5602 struct sctp_association *asoc,
5603 struct sctp_stream_out *strq, int holds_lock)
5604{
5605 struct sctp_stream_out *stre, *strn;
5606
5607 if (holds_lock == 0) {
5608 SCTP_TCB_SEND_LOCK(stcb);
5609 }
5610 if ((strq->next_spoke.tqe_next) ||
5611 (strq->next_spoke.tqe_prev)) {
5612 /* already on wheel */
5613 goto outof_here;
5614 }
5615 stre = TAILQ_FIRST(&asoc->out_wheel);
5616 if (stre == NULL) {
5617 /* only one on wheel */
5618 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5619 goto outof_here;
5620 }
5621 for (; stre; stre = strn) {
5622 strn = TAILQ_NEXT(stre, next_spoke);
5623 if (stre->stream_no > strq->stream_no) {
5624 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5625 goto outof_here;
5626 } else if (stre->stream_no == strq->stream_no) {
5627 /* huh, should not happen */
5628 goto outof_here;
5629 } else if (strn == NULL) {
5630 /* next one is null */
5631 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5632 next_spoke);
5633 }
5634 }
5635outof_here:
5636 if (holds_lock == 0) {
5637 SCTP_TCB_SEND_UNLOCK(stcb);
5638 }
5639}
5640
5641void
5642sctp_remove_from_wheel(struct sctp_tcb *stcb,
5643 struct sctp_association *asoc,
5644 struct sctp_stream_out *strq,
5645 int holds_lock)
5646{
5647 /* take off and then setup so we know it is not on the wheel */
5648 if (holds_lock == 0)
5649 SCTP_TCB_SEND_LOCK(stcb);
5650 if (TAILQ_FIRST(&strq->outqueue)) {
5651 /* more was added */
5652 if (holds_lock == 0)
5653 SCTP_TCB_SEND_UNLOCK(stcb);
5654 return;
5655 }
5656 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5657 strq->next_spoke.tqe_next = NULL;
5658 strq->next_spoke.tqe_prev = NULL;
5659 if (holds_lock == 0)
5660 SCTP_TCB_SEND_UNLOCK(stcb);
5661}
5662
5663static void
5664sctp_prune_prsctp(struct sctp_tcb *stcb,
5665 struct sctp_association *asoc,
5666 struct sctp_sndrcvinfo *srcv,
5667 int dataout)
5668{
5669 int freed_spc = 0;
5670 struct sctp_tmit_chunk *chk, *nchk;
5671
5672 SCTP_TCB_LOCK_ASSERT(stcb);
5673 if ((asoc->peer_supports_prsctp) &&
5674 (asoc->sent_queue_cnt_removeable > 0)) {
5675 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5676 /*
5677 * Look for chunks marked with the PR_SCTP flag AND
5678 * the buffer space flag. If the one being sent is
5679 * equal or greater priority then purge the old one
5680 * and free some space.
5681 */
5682 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5683 /*
5684 * This one is PR-SCTP AND buffer space
5685 * limited type
5686 */
5687 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5688 /*
5689 * Lower numbers equates to higher
5690 * priority so if the one we are
5691 * looking at has a larger or equal
5692 * priority we want to drop the data
5693 * and NOT retransmit it.
5694 */
5695 if (chk->data) {
5696 /*
5697 * We release the book_size
5698 * if the mbuf is here
5699 */
5700 int ret_spc;
5701 int cause;
5702
5703 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5704 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5705 else
5706 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5707 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5708 cause,
5709 SCTP_SO_LOCKED);
5710 freed_spc += ret_spc;
5711 if (freed_spc >= dataout) {
5712 return;
5713 }
5714 } /* if chunk was present */
5715 } /* if of sufficent priority */
5716 } /* if chunk has enabled */
5717 } /* tailqforeach */
5718
5719 chk = TAILQ_FIRST(&asoc->send_queue);
5720 while (chk) {
5721 nchk = TAILQ_NEXT(chk, sctp_next);
5722 /* Here we must move to the sent queue and mark */
5723 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5724 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5725 if (chk->data) {
5726 /*
5727 * We release the book_size
5728 * if the mbuf is here
5729 */
5730 int ret_spc;
5731
5732 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5733 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5734 SCTP_SO_LOCKED);
5735
5736 freed_spc += ret_spc;
5737 if (freed_spc >= dataout) {
5738 return;
5739 }
5740 } /* end if chk->data */
5741 } /* end if right class */
5742 } /* end if chk pr-sctp */
5743 chk = nchk;
5744 } /* end while (chk) */
5745 } /* if enabled in asoc */
5746}
5747
5748int
5749sctp_get_frag_point(struct sctp_tcb *stcb,
5750 struct sctp_association *asoc)
5751{
5752 int siz, ovh;
5753
5754 /*
5755 * For endpoints that have both v6 and v4 addresses we must reserve
5756 * room for the ipv6 header, for those that are only dealing with V4
5757 * we use a larger frag point.
5758 */
5759 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5760 ovh = SCTP_MED_OVERHEAD;
5761 } else {
5762 ovh = SCTP_MED_V4_OVERHEAD;
5763 }
5764
5765 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5766 siz = asoc->smallest_mtu - ovh;
5767 else
5768 siz = (stcb->asoc.sctp_frag_point - ovh);
5769 /*
5770 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5771 */
5772 /* A data chunk MUST fit in a cluster */
5773 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5774 /* } */
5775
5776 /* adjust for an AUTH chunk if DATA requires auth */
5777 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5778 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5779
5780 if (siz % 4) {
5781 /* make it an even word boundary please */
5782 siz -= (siz % 4);
5783 }
5784 return (siz);
5785}
5786
5787static void
5788sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5789{
5790 sp->pr_sctp_on = 0;
5791 /*
5792 * We assume that the user wants PR_SCTP_TTL if the user provides a
5793 * positive lifetime but does not specify any PR_SCTP policy. This
5794 * is a BAD assumption and causes problems at least with the
5795 * U-Vancovers MPI folks. I will change this to be no policy means
5796 * NO PR-SCTP.
5797 */
5798 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5799 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5800 sp->pr_sctp_on = 1;
5801 } else {
5802 return;
5803 }
5804 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5805 case CHUNK_FLAGS_PR_SCTP_BUF:
5806 /*
5807 * Time to live is a priority stored in tv_sec when doing
5808 * the buffer drop thing.
5809 */
5810 sp->ts.tv_sec = sp->timetolive;
5811 sp->ts.tv_usec = 0;
5812 break;
5813 case CHUNK_FLAGS_PR_SCTP_TTL:
5814 {
5815 struct timeval tv;
5816
5817 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5818 tv.tv_sec = sp->timetolive / 1000;
5819 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5820 /*
5821 * TODO sctp_constants.h needs alternative time
5822 * macros when _KERNEL is undefined.
5823 */
5824 timevaladd(&sp->ts, &tv);
5825 }
5826 break;
5827 case CHUNK_FLAGS_PR_SCTP_RTX:
5828 /*
5829 * Time to live is a the number or retransmissions stored in
5830 * tv_sec.
5831 */
5832 sp->ts.tv_sec = sp->timetolive;
5833 sp->ts.tv_usec = 0;
5834 break;
5835 default:
5836 SCTPDBG(SCTP_DEBUG_USRREQ1,
5837 "Unknown PR_SCTP policy %u.\n",
5838 PR_SCTP_POLICY(sp->sinfo_flags));
5839 break;
5840 }
5841}
5842
5843static int
5844sctp_msg_append(struct sctp_tcb *stcb,
5845 struct sctp_nets *net,
5846 struct mbuf *m,
5847 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5848{
5849 int error = 0, holds_lock;
5850 struct mbuf *at;
5851 struct sctp_stream_queue_pending *sp = NULL;
5852 struct sctp_stream_out *strm;
5853
5854 /*
5855 * Given an mbuf chain, put it into the association send queue and
5856 * place it on the wheel
5857 */
5858 holds_lock = hold_stcb_lock;
5859 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5860 /* Invalid stream number */
5861 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5862 error = EINVAL;
5863 goto out_now;
5864 }
5865 if ((stcb->asoc.stream_locked) &&
5866 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5867 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5868 error = EINVAL;
5869 goto out_now;
5870 }
5871 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5872 /* Now can we send this? */
5873 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5874 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5875 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5876 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5877 /* got data while shutting down */
5878 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5879 error = ECONNRESET;
5880 goto out_now;
5881 }
5882 sctp_alloc_a_strmoq(stcb, sp);
5883 if (sp == NULL) {
5884 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5885 error = ENOMEM;
5886 goto out_now;
5887 }
5888 sp->sinfo_flags = srcv->sinfo_flags;
5889 sp->timetolive = srcv->sinfo_timetolive;
5890 sp->ppid = srcv->sinfo_ppid;
5891 sp->context = srcv->sinfo_context;
5892 sp->strseq = 0;
5893 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5894 sp->net = net;
5895 sp->addr_over = 1;
5896 } else {
5897 sp->net = stcb->asoc.primary_destination;
5898 sp->addr_over = 0;
5899 }
5900 atomic_add_int(&sp->net->ref_count, 1);
5901 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5902 sp->stream = srcv->sinfo_stream;
5903 sp->msg_is_complete = 1;
5904 sp->sender_all_done = 1;
5905 sp->some_taken = 0;
5906 sp->data = m;
5907 sp->tail_mbuf = NULL;
5908 sp->length = 0;
5909 at = m;
5910 sctp_set_prsctp_policy(sp);
5911 /*
5912 * We could in theory (for sendall) sifa the length in, but we would
5913 * still have to hunt through the chain since we need to setup the
5914 * tail_mbuf
5915 */
5916 while (at) {
5917 if (SCTP_BUF_NEXT(at) == NULL)
5918 sp->tail_mbuf = at;
5919 sp->length += SCTP_BUF_LEN(at);
5920 at = SCTP_BUF_NEXT(at);
5921 }
5922 SCTP_TCB_SEND_LOCK(stcb);
5923 sctp_snd_sb_alloc(stcb, sp->length);
5924 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5925 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5926 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5927 sp->strseq = strm->next_sequence_sent;
5928 strm->next_sequence_sent++;
5929 }
5930 if ((strm->next_spoke.tqe_next == NULL) &&
5931 (strm->next_spoke.tqe_prev == NULL)) {
5932 /* Not on wheel, insert */
5933 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5934 }
5935 m = NULL;
5936 SCTP_TCB_SEND_UNLOCK(stcb);
5937out_now:
5938 if (m) {
5939 sctp_m_freem(m);
5940 }
5941 return (error);
5942}
5943
5944
5945static struct mbuf *
5946sctp_copy_mbufchain(struct mbuf *clonechain,
5947 struct mbuf *outchain,
5948 struct mbuf **endofchain,
5949 int can_take_mbuf,
5950 int sizeofcpy,
5951 uint8_t copy_by_ref)
5952{
5953 struct mbuf *m;
5954 struct mbuf *appendchain;
5955 caddr_t cp;
5956 int len;
5957
5958 if (endofchain == NULL) {
5959 /* error */
5960error_out:
5961 if (outchain)
5962 sctp_m_freem(outchain);
5963 return (NULL);
5964 }
5965 if (can_take_mbuf) {
5966 appendchain = clonechain;
5967 } else {
5968 if (!copy_by_ref &&
5969 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5970 ) {
5971 /* Its not in a cluster */
5972 if (*endofchain == NULL) {
5973 /* lets get a mbuf cluster */
5974 if (outchain == NULL) {
5975 /* This is the general case */
5976 new_mbuf:
5977 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5978 if (outchain == NULL) {
5979 goto error_out;
5980 }
5981 SCTP_BUF_LEN(outchain) = 0;
5982 *endofchain = outchain;
5983 /* get the prepend space */
5984 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5985 } else {
5986 /*
5987 * We really should not get a NULL
5988 * in endofchain
5989 */
5990 /* find end */
5991 m = outchain;
5992 while (m) {
5993 if (SCTP_BUF_NEXT(m) == NULL) {
5994 *endofchain = m;
5995 break;
5996 }
5997 m = SCTP_BUF_NEXT(m);
5998 }
5999 /* sanity */
6000 if (*endofchain == NULL) {
6001 /*
6002 * huh, TSNH XXX maybe we
6003 * should panic
6004 */
6005 sctp_m_freem(outchain);
6006 goto new_mbuf;
6007 }
6008 }
6009 /* get the new end of length */
6010 len = M_TRAILINGSPACE(*endofchain);
6011 } else {
6012 /* how much is left at the end? */
6013 len = M_TRAILINGSPACE(*endofchain);
6014 }
6015 /* Find the end of the data, for appending */
6016 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6017
6018 /* Now lets copy it out */
6019 if (len >= sizeofcpy) {
6020 /* It all fits, copy it in */
6021 m_copydata(clonechain, 0, sizeofcpy, cp);
6022 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6023 } else {
6024 /* fill up the end of the chain */
6025 if (len > 0) {
6026 m_copydata(clonechain, 0, len, cp);
6027 SCTP_BUF_LEN((*endofchain)) += len;
6028 /* now we need another one */
6029 sizeofcpy -= len;
6030 }
6031 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6032 if (m == NULL) {
6033 /* We failed */
6034 goto error_out;
6035 }
6036 SCTP_BUF_NEXT((*endofchain)) = m;
6037 *endofchain = m;
6038 cp = mtod((*endofchain), caddr_t);
6039 m_copydata(clonechain, len, sizeofcpy, cp);
6040 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6041 }
6042 return (outchain);
6043 } else {
6044 /* copy the old fashion way */
6045 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6046#ifdef SCTP_MBUF_LOGGING
6047 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6048 struct mbuf *mat;
6049
6050 mat = appendchain;
6051 while (mat) {
6052 if (SCTP_BUF_IS_EXTENDED(mat)) {
6053 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6054 }
6055 mat = SCTP_BUF_NEXT(mat);
6056 }
6057 }
6058#endif
6059 }
6060 }
6061 if (appendchain == NULL) {
6062 /* error */
6063 if (outchain)
6064 sctp_m_freem(outchain);
6065 return (NULL);
6066 }
6067 if (outchain) {
6068 /* tack on to the end */
6069 if (*endofchain != NULL) {
6070 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6071 } else {
6072 m = outchain;
6073 while (m) {
6074 if (SCTP_BUF_NEXT(m) == NULL) {
6075 SCTP_BUF_NEXT(m) = appendchain;
6076 break;
6077 }
6078 m = SCTP_BUF_NEXT(m);
6079 }
6080 }
6081 /*
6082 * save off the end and update the end-chain postion
6083 */
6084 m = appendchain;
6085 while (m) {
6086 if (SCTP_BUF_NEXT(m) == NULL) {
6087 *endofchain = m;
6088 break;
6089 }
6090 m = SCTP_BUF_NEXT(m);
6091 }
6092 return (outchain);
6093 } else {
6094 /* save off the end and update the end-chain postion */
6095 m = appendchain;
6096 while (m) {
6097 if (SCTP_BUF_NEXT(m) == NULL) {
6098 *endofchain = m;
6099 break;
6100 }
6101 m = SCTP_BUF_NEXT(m);
6102 }
6103 return (appendchain);
6104 }
6105}
6106
6107int
6108sctp_med_chunk_output(struct sctp_inpcb *inp,
6109 struct sctp_tcb *stcb,
6110 struct sctp_association *asoc,
6111 int *num_out,
6112 int *reason_code,
6113 int control_only, int from_where,
6114 struct timeval *now, int *now_filled, int frag_point, int so_locked
6115#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6116 SCTP_UNUSED
6117#endif
6118);
6119
6120static void
6121sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6122 uint32_t val)
6123{
6124 struct sctp_copy_all *ca;
6125 struct mbuf *m;
6126 int ret = 0;
6127 int added_control = 0;
6128 int un_sent, do_chunk_output = 1;
6129 struct sctp_association *asoc;
6130
6131 ca = (struct sctp_copy_all *)ptr;
6132 if (ca->m == NULL) {
6133 return;
6134 }
6135 if (ca->inp != inp) {
6136 /* TSNH */
6137 return;
6138 }
6139 if ((ca->m) && ca->sndlen) {
6140 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6141 if (m == NULL) {
6142 /* can't copy so we are done */
6143 ca->cnt_failed++;
6144 return;
6145 }
6146#ifdef SCTP_MBUF_LOGGING
6147 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6148 struct mbuf *mat;
6149
6150 mat = m;
6151 while (mat) {
6152 if (SCTP_BUF_IS_EXTENDED(mat)) {
6153 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6154 }
6155 mat = SCTP_BUF_NEXT(mat);
6156 }
6157 }
6158#endif
6159 } else {
6160 m = NULL;
6161 }
6162 SCTP_TCB_LOCK_ASSERT(stcb);
6163 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6164 /* Abort this assoc with m as the user defined reason */
6165 if (m) {
6166 struct sctp_paramhdr *ph;
6167
6168 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6169 if (m) {
6170 ph = mtod(m, struct sctp_paramhdr *);
6171 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6172 ph->param_length = htons(ca->sndlen);
6173 }
6174 /*
6175 * We add one here to keep the assoc from
6176 * dis-appearing on us.
6177 */
6178 atomic_add_int(&stcb->asoc.refcnt, 1);
6179 sctp_abort_an_association(inp, stcb,
6180 SCTP_RESPONSE_TO_USER_REQ,
6181 m, SCTP_SO_NOT_LOCKED);
6182 /*
6183 * sctp_abort_an_association calls sctp_free_asoc()
6184 * free association will NOT free it since we
6185 * incremented the refcnt .. we do this to prevent
6186 * it being freed and things getting tricky since we
6187 * could end up (from free_asoc) calling inpcb_free
6188 * which would get a recursive lock call to the
6189 * iterator lock.. But as a consequence of that the
6190 * stcb will return to us un-locked.. since
6191 * free_asoc returns with either no TCB or the TCB
6192 * unlocked, we must relock.. to unlock in the
6193 * iterator timer :-0
6194 */
6195 SCTP_TCB_LOCK(stcb);
6196 atomic_add_int(&stcb->asoc.refcnt, -1);
6197 goto no_chunk_output;
6198 }
6199 } else {
6200 if (m) {
6201 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6202 &ca->sndrcv, 1);
6203 }
6204 asoc = &stcb->asoc;
6205 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6206 /* shutdown this assoc */
6207 int cnt;
6208
6209 cnt = sctp_is_there_unsent_data(stcb);
6210
6211 if (TAILQ_EMPTY(&asoc->send_queue) &&
6212 TAILQ_EMPTY(&asoc->sent_queue) &&
6213 (cnt == 0)) {
6214 if (asoc->locked_on_sending) {
6215 goto abort_anyway;
6216 }
6217 /*
6218 * there is nothing queued to send, so I'm
6219 * done...
6220 */
6221 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6222 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6223 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6224 /*
6225 * only send SHUTDOWN the first time
6226 * through
6227 */
6228 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6229 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6230 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6231 }
6232 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6233 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6234 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6235 asoc->primary_destination);
6236 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6237 asoc->primary_destination);
6238 added_control = 1;
6239 do_chunk_output = 0;
6240 }
6241 } else {
6242 /*
6243 * we still got (or just got) data to send,
6244 * so set SHUTDOWN_PENDING
6245 */
6246 /*
6247 * XXX sockets draft says that SCTP_EOF
6248 * should be sent with no data. currently,
6249 * we will allow user data to be sent first
6250 * and move to SHUTDOWN-PENDING
6251 */
6252 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6253 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6254 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6255 if (asoc->locked_on_sending) {
6256 /*
6257 * Locked to send out the
6258 * data
6259 */
6260 struct sctp_stream_queue_pending *sp;
6261
6262 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6263 if (sp) {
6264 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6265 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6266 }
6267 }
6268 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6269 if (TAILQ_EMPTY(&asoc->send_queue) &&
6270 TAILQ_EMPTY(&asoc->sent_queue) &&
6271 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6272 abort_anyway:
6273 atomic_add_int(&stcb->asoc.refcnt, 1);
6274 sctp_abort_an_association(stcb->sctp_ep, stcb,
6275 SCTP_RESPONSE_TO_USER_REQ,
6276 NULL, SCTP_SO_NOT_LOCKED);
6277 atomic_add_int(&stcb->asoc.refcnt, -1);
6278 goto no_chunk_output;
6279 }
6280 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6281 asoc->primary_destination);
6282 }
6283 }
6284
6285 }
6286 }
6287 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6288 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6289
6290 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6291 (stcb->asoc.total_flight > 0) &&
6292 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6293 ) {
6294 do_chunk_output = 0;
6295 }
6296 if (do_chunk_output)
6297 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6298 else if (added_control) {
6299 int num_out = 0, reason = 0, now_filled = 0;
6300 struct timeval now;
6301 int frag_point;
6302
6303 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6304 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6305 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6306 }
6307no_chunk_output:
6308 if (ret) {
6309 ca->cnt_failed++;
6310 } else {
6311 ca->cnt_sent++;
6312 }
6313}
6314
6315static void
6316sctp_sendall_completes(void *ptr, uint32_t val)
6317{
6318 struct sctp_copy_all *ca;
6319
6320 ca = (struct sctp_copy_all *)ptr;
6321 /*
6322 * Do a notify here? Kacheong suggests that the notify be done at
6323 * the send time.. so you would push up a notification if any send
6324 * failed. Don't know if this is feasable since the only failures we
6325 * have is "memory" related and if you cannot get an mbuf to send
6326 * the data you surely can't get an mbuf to send up to notify the
6327 * user you can't send the data :->
6328 */
6329
6330 /* now free everything */
6331 sctp_m_freem(ca->m);
6332 SCTP_FREE(ca, SCTP_M_COPYAL);
6333}
6334
6335
6336#define MC_ALIGN(m, len) do { \
6337 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6338} while (0)
6339
6340
6341
6342static struct mbuf *
6343sctp_copy_out_all(struct uio *uio, int len)
6344{
6345 struct mbuf *ret, *at;
6346 int left, willcpy, cancpy, error;
6347
6348 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6349 if (ret == NULL) {
6350 /* TSNH */
6351 return (NULL);
6352 }
6353 left = len;
6354 SCTP_BUF_LEN(ret) = 0;
6355 /* save space for the data chunk header */
6356 cancpy = M_TRAILINGSPACE(ret);
6357 willcpy = min(cancpy, left);
6358 at = ret;
6359 while (left > 0) {
6360 /* Align data to the end */
6361 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6362 if (error) {
6363 err_out_now:
6364 sctp_m_freem(at);
6365 return (NULL);
6366 }
6367 SCTP_BUF_LEN(at) = willcpy;
6368 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6369 left -= willcpy;
6370 if (left > 0) {
6371 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6372 if (SCTP_BUF_NEXT(at) == NULL) {
6373 goto err_out_now;
6374 }
6375 at = SCTP_BUF_NEXT(at);
6376 SCTP_BUF_LEN(at) = 0;
6377 cancpy = M_TRAILINGSPACE(at);
6378 willcpy = min(cancpy, left);
6379 }
6380 }
6381 return (ret);
6382}
6383
6384static int
6385sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6386 struct sctp_sndrcvinfo *srcv)
6387{
6388 int ret;
6389 struct sctp_copy_all *ca;
6390
6391 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6392 SCTP_M_COPYAL);
6393 if (ca == NULL) {
6394 sctp_m_freem(m);
6395 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6396 return (ENOMEM);
6397 }
6398 memset(ca, 0, sizeof(struct sctp_copy_all));
6399
6400 ca->inp = inp;
6401 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6402 /*
6403 * take off the sendall flag, it would be bad if we failed to do
6404 * this :-0
6405 */
6406 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6407 /* get length and mbuf chain */
6408 if (uio) {
6409 ca->sndlen = uio->uio_resid;
6410 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6411 if (ca->m == NULL) {
6412 SCTP_FREE(ca, SCTP_M_COPYAL);
6413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6414 return (ENOMEM);
6415 }
6416 } else {
6417 /* Gather the length of the send */
6418 struct mbuf *mat;
6419
6420 mat = m;
6421 ca->sndlen = 0;
6422 while (m) {
6423 ca->sndlen += SCTP_BUF_LEN(m);
6424 m = SCTP_BUF_NEXT(m);
6425 }
6426 ca->m = mat;
6427 }
6428 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6429 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6430 SCTP_ASOC_ANY_STATE,
6431 (void *)ca, 0,
6432 sctp_sendall_completes, inp, 1);
6433 if (ret) {
6434 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6435 SCTP_FREE(ca, SCTP_M_COPYAL);
6436 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6437 return (EFAULT);
6438 }
6439 return (0);
6440}
6441
6442
6443void
6444sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6445{
6446 struct sctp_tmit_chunk *chk, *nchk;
6447
6448 chk = TAILQ_FIRST(&asoc->control_send_queue);
6449 while (chk) {
6450 nchk = TAILQ_NEXT(chk, sctp_next);
6451 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6452 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6453 if (chk->data) {
6454 sctp_m_freem(chk->data);
6455 chk->data = NULL;
6456 }
6457 asoc->ctrl_queue_cnt--;
6458 sctp_free_a_chunk(stcb, chk);
6459 }
6460 chk = nchk;
6461 }
6462}
6463
6464void
6465sctp_toss_old_asconf(struct sctp_tcb *stcb)
6466{
6467 struct sctp_association *asoc;
6468 struct sctp_tmit_chunk *chk, *chk_tmp;
6469 struct sctp_asconf_chunk *acp;
6470
6471 asoc = &stcb->asoc;
6472 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6473 chk = chk_tmp) {
6474 /* get next chk */
6475 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6476 /* find SCTP_ASCONF chunk in queue */
6477 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6478 if (chk->data) {
6479 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6480 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6481 /* Not Acked yet */
6482 break;
6483 }
6484 }
6485 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6486 if (chk->data) {
6487 sctp_m_freem(chk->data);
6488 chk->data = NULL;
6489 }
6490 asoc->ctrl_queue_cnt--;
6491 sctp_free_a_chunk(stcb, chk);
6492 }
6493 }
6494}
6495
6496
6497static void
6498sctp_clean_up_datalist(struct sctp_tcb *stcb,
6499
6500 struct sctp_association *asoc,
6501 struct sctp_tmit_chunk **data_list,
6502 int bundle_at,
6503 struct sctp_nets *net)
6504{
6505 int i;
6506 struct sctp_tmit_chunk *tp1;
6507
6508 for (i = 0; i < bundle_at; i++) {
6509 /* off of the send queue */
6510 if (i) {
6511 /*
6512 * Any chunk NOT 0 you zap the time chunk 0 gets
6513 * zapped or set based on if a RTO measurment is
6514 * needed.
6515 */
6516 data_list[i]->do_rtt = 0;
6517 }
6518 /* record time */
6519 data_list[i]->sent_rcv_time = net->last_sent_time;
6520 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6521 TAILQ_REMOVE(&asoc->send_queue,
6522 data_list[i],
6523 sctp_next);
6524 /* on to the sent queue */
6525 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6526 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6527 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6528 struct sctp_tmit_chunk *tpp;
6529
6530 /* need to move back */
6531 back_up_more:
6532 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6533 if (tpp == NULL) {
6534 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6535 goto all_done;
6536 }
6537 tp1 = tpp;
6538 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6539 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6540 goto back_up_more;
6541 }
6542 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6543 } else {
6544 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6545 data_list[i],
6546 sctp_next);
6547 }
6548all_done:
6549 /* This does not lower until the cum-ack passes it */
6550 asoc->sent_queue_cnt++;
6551 asoc->send_queue_cnt--;
6552 if ((asoc->peers_rwnd <= 0) &&
6553 (asoc->total_flight == 0) &&
6554 (bundle_at == 1)) {
6555 /* Mark the chunk as being a window probe */
6556 SCTP_STAT_INCR(sctps_windowprobed);
6557 }
6558#ifdef SCTP_AUDITING_ENABLED
6559 sctp_audit_log(0xC2, 3);
6560#endif
6561 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6562 data_list[i]->snd_count = 1;
6563 data_list[i]->rec.data.chunk_was_revoked = 0;
6564 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6565 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6566 data_list[i]->whoTo->flight_size,
6567 data_list[i]->book_size,
6568 (uintptr_t) data_list[i]->whoTo,
6569 data_list[i]->rec.data.TSN_seq);
6570 }
6571 sctp_flight_size_increase(data_list[i]);
6572 sctp_total_flight_increase(stcb, data_list[i]);
6573 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6574 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6575 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6576 }
6577 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6578 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6579 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6580 /* SWS sender side engages */
6581 asoc->peers_rwnd = 0;
6582 }
6583 }
6584}
6585
6586static void
6587sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6588{
6589 struct sctp_tmit_chunk *chk, *nchk;
6590
6591 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6592 chk; chk = nchk) {
6593 nchk = TAILQ_NEXT(chk, sctp_next);
6594 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6595 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
6596 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6597 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6598 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6599 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6600 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6601 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6602 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6603 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6604 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6605 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6606 /* Stray chunks must be cleaned up */
6607 clean_up_anyway:
6608 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6609 if (chk->data) {
6610 sctp_m_freem(chk->data);
6611 chk->data = NULL;
6612 }
6613 asoc->ctrl_queue_cnt--;
6614 sctp_free_a_chunk(stcb, chk);
6615 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6616 /* special handling, we must look into the param */
6617 if (chk != asoc->str_reset) {
6618 goto clean_up_anyway;
6619 }
6620 }
6621 }
6622}
6623
6624
6625static int
6626sctp_can_we_split_this(struct sctp_tcb *stcb,
6627 uint32_t length,
6628 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6629{
6630 /*
6631 * Make a decision on if I should split a msg into multiple parts.
6632 * This is only asked of incomplete messages.
6633 */
6634 if (eeor_on) {
6635 /*
6636 * If we are doing EEOR we need to always send it if its the
6637 * entire thing, since it might be all the guy is putting in
6638 * the hopper.
6639 */
6640 if (goal_mtu >= length) {
6641 /*-
6642 * If we have data outstanding,
6643 * we get another chance when the sack
6644 * arrives to transmit - wait for more data
6645 */
6646 if (stcb->asoc.total_flight == 0) {
6647 /*
6648 * If nothing is in flight, we zero the
6649 * packet counter.
6650 */
6651 return (length);
6652 }
6653 return (0);
6654
6655 } else {
6656 /* You can fill the rest */
6657 return (goal_mtu);
6658 }
6659 }
6660 /*-
6661 * For those strange folk that make the send buffer
6662 * smaller than our fragmentation point, we can't
6663 * get a full msg in so we have to allow splitting.
6664 */
6665 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6666 return (length);
6667 }
6668 if ((length <= goal_mtu) ||
6669 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6670 /* Sub-optimial residual don't split in non-eeor mode. */
6671 return (0);
6672 }
6673 /*
6674 * If we reach here length is larger than the goal_mtu. Do we wish
6675 * to split it for the sake of packet putting together?
6676 */
6677 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6678 /* Its ok to split it */
6679 return (min(goal_mtu, frag_point));
6680 }
6681 /* Nope, can't split */
6682 return (0);
6683
6684}
6685
6686static uint32_t
6687sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6688 struct sctp_stream_out *strq,
6689 uint32_t goal_mtu,
6690 uint32_t frag_point,
6691 int *locked,
6692 int *giveup,
6693 int eeor_mode,
6694 int *bail)
6695{
6696 /* Move from the stream to the send_queue keeping track of the total */
6697 struct sctp_association *asoc;
6698 struct sctp_stream_queue_pending *sp;
6699 struct sctp_tmit_chunk *chk;
6700 struct sctp_data_chunk *dchkh;
6701 uint32_t to_move, length;
6702 uint8_t rcv_flags = 0;
6703 uint8_t some_taken;
6704 uint8_t send_lock_up = 0;
6705
6706 SCTP_TCB_LOCK_ASSERT(stcb);
6707 asoc = &stcb->asoc;
6708one_more_time:
6709 /* sa_ignore FREED_MEMORY */
6710 sp = TAILQ_FIRST(&strq->outqueue);
6711 if (sp == NULL) {
6712 *locked = 0;
6713 if (send_lock_up == 0) {
6714 SCTP_TCB_SEND_LOCK(stcb);
6715 send_lock_up = 1;
6716 }
6717 sp = TAILQ_FIRST(&strq->outqueue);
6718 if (sp) {
6719 goto one_more_time;
6720 }
6721 if (strq->last_msg_incomplete) {
6722 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6723 strq->stream_no,
6724 strq->last_msg_incomplete);
6725 strq->last_msg_incomplete = 0;
6726 }
6727 to_move = 0;
6728 if (send_lock_up) {
6729 SCTP_TCB_SEND_UNLOCK(stcb);
6730 send_lock_up = 0;
6731 }
6732 goto out_of;
6733 }
6734 if ((sp->msg_is_complete) && (sp->length == 0)) {
6735 if (sp->sender_all_done) {
6736 /*
6737 * We are doing differed cleanup. Last time through
6738 * when we took all the data the sender_all_done was
6739 * not set.
6740 */
6741 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6742 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6743 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6744 sp->sender_all_done,
6745 sp->length,
6746 sp->msg_is_complete,
6747 sp->put_last_out,
6748 send_lock_up);
6749 }
6750 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6751 SCTP_TCB_SEND_LOCK(stcb);
6752 send_lock_up = 1;
6753 }
6754 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6755 TAILQ_REMOVE(&strq->outqueue, sp, next);
6756 sctp_free_remote_addr(sp->net);
6757 if (sp->data) {
6758 sctp_m_freem(sp->data);
6759 sp->data = NULL;
6760 }
6761 sctp_free_a_strmoq(stcb, sp);
6762 /* we can't be locked to it */
6763 *locked = 0;
6764 stcb->asoc.locked_on_sending = NULL;
6765 if (send_lock_up) {
6766 SCTP_TCB_SEND_UNLOCK(stcb);
6767 send_lock_up = 0;
6768 }
6769 /* back to get the next msg */
6770 goto one_more_time;
6771 } else {
6772 /*
6773 * sender just finished this but still holds a
6774 * reference
6775 */
6776 *locked = 1;
6777 *giveup = 1;
6778 to_move = 0;
6779 goto out_of;
6780 }
6781 } else {
6782 /* is there some to get */
6783 if (sp->length == 0) {
6784 /* no */
6785 *locked = 1;
6786 *giveup = 1;
6787 to_move = 0;
6788 goto out_of;
6789 } else if (sp->discard_rest) {
6790 if (send_lock_up == 0) {
6791 SCTP_TCB_SEND_LOCK(stcb);
6792 send_lock_up = 1;
6793 }
6794 /* Whack down the size */
6795 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6796 if ((stcb->sctp_socket != NULL) && \
6797 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6798 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6799 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6800 }
6801 if (sp->data) {
6802 sctp_m_freem(sp->data);
6803 sp->data = NULL;
6804 sp->tail_mbuf = NULL;
6805 }
6806 sp->length = 0;
6807 sp->some_taken = 1;
6808 *locked = 1;
6809 *giveup = 1;
6810 to_move = 0;
6811 goto out_of;
6812 }
6813 }
6814 some_taken = sp->some_taken;
6815 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6816 sp->msg_is_complete = 1;
6817 }
6818re_look:
6819 length = sp->length;
6820 if (sp->msg_is_complete) {
6821 /* The message is complete */
6822 to_move = min(length, frag_point);
6823 if (to_move == length) {
6824 /* All of it fits in the MTU */
6825 if (sp->some_taken) {
6826 rcv_flags |= SCTP_DATA_LAST_FRAG;
6827 sp->put_last_out = 1;
6828 } else {
6829 rcv_flags |= SCTP_DATA_NOT_FRAG;
6830 sp->put_last_out = 1;
6831 }
6832 } else {
6833 /* Not all of it fits, we fragment */
6834 if (sp->some_taken == 0) {
6835 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6836 }
6837 sp->some_taken = 1;
6838 }
6839 } else {
6840 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6841 if (to_move) {
6842 /*-
6843 * We use a snapshot of length in case it
6844 * is expanding during the compare.
6845 */
6846 uint32_t llen;
6847
6848 llen = length;
6849 if (to_move >= llen) {
6850 to_move = llen;
6851 if (send_lock_up == 0) {
6852 /*-
6853 * We are taking all of an incomplete msg
6854 * thus we need a send lock.
6855 */
6856 SCTP_TCB_SEND_LOCK(stcb);
6857 send_lock_up = 1;
6858 if (sp->msg_is_complete) {
6859 /*
6860 * the sender finished the
6861 * msg
6862 */
6863 goto re_look;
6864 }
6865 }
6866 }
6867 if (sp->some_taken == 0) {
6868 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6869 sp->some_taken = 1;
6870 }
6871 } else {
6872 /* Nothing to take. */
6873 if (sp->some_taken) {
6874 *locked = 1;
6875 }
6876 *giveup = 1;
6877 to_move = 0;
6878 goto out_of;
6879 }
6880 }
6881
6882 /* If we reach here, we can copy out a chunk */
6883 sctp_alloc_a_chunk(stcb, chk);
6884 if (chk == NULL) {
6885 /* No chunk memory */
6886 *giveup = 1;
6887 to_move = 0;
6888 goto out_of;
6889 }
6890 /*
6891 * Setup for unordered if needed by looking at the user sent info
6892 * flags.
6893 */
6894 if (sp->sinfo_flags & SCTP_UNORDERED) {
6895 rcv_flags |= SCTP_DATA_UNORDERED;
6896 }
6897 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6898 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6899 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6900 }
6901 /* clear out the chunk before setting up */
6902 memset(chk, 0, sizeof(*chk));
6903 chk->rec.data.rcv_flags = rcv_flags;
6904
6905 if (to_move >= length) {
6906 /* we think we can steal the whole thing */
6907 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6908 SCTP_TCB_SEND_LOCK(stcb);
6909 send_lock_up = 1;
6910 }
6911 if (to_move < sp->length) {
6912 /* bail, it changed */
6913 goto dont_do_it;
6914 }
6915 chk->data = sp->data;
6916 chk->last_mbuf = sp->tail_mbuf;
6917 /* register the stealing */
6918 sp->data = sp->tail_mbuf = NULL;
6919 } else {
6920 struct mbuf *m;
6921
6922dont_do_it:
6923 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6924 chk->last_mbuf = NULL;
6925 if (chk->data == NULL) {
6926 sp->some_taken = some_taken;
6927 sctp_free_a_chunk(stcb, chk);
6928 *bail = 1;
6929 to_move = 0;
6930 goto out_of;
6931 }
6932#ifdef SCTP_MBUF_LOGGING
6933 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6934 struct mbuf *mat;
6935
6936 mat = chk->data;
6937 while (mat) {
6938 if (SCTP_BUF_IS_EXTENDED(mat)) {
6939 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6940 }
6941 mat = SCTP_BUF_NEXT(mat);
6942 }
6943 }
6944#endif
6945 /* Pull off the data */
6946 m_adj(sp->data, to_move);
6947 /* Now lets work our way down and compact it */
6948 m = sp->data;
6949 while (m && (SCTP_BUF_LEN(m) == 0)) {
6950 sp->data = SCTP_BUF_NEXT(m);
6951 SCTP_BUF_NEXT(m) = NULL;
6952 if (sp->tail_mbuf == m) {
6953 /*-
6954 * Freeing tail? TSNH since
6955 * we supposedly were taking less
6956 * than the sp->length.
6957 */
6958#ifdef INVARIANTS
6959 panic("Huh, freing tail? - TSNH");
6960#else
6961 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6962 sp->tail_mbuf = sp->data = NULL;
6963 sp->length = 0;
6964#endif
6965
6966 }
6967 sctp_m_free(m);
6968 m = sp->data;
6969 }
6970 }
6971 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6972 chk->copy_by_ref = 1;
6973 } else {
6974 chk->copy_by_ref = 0;
6975 }
6976 /*
6977 * get last_mbuf and counts of mb useage This is ugly but hopefully
6978 * its only one mbuf.
6979 */
6980 if (chk->last_mbuf == NULL) {
6981 chk->last_mbuf = chk->data;
6982 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6983 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6984 }
6985 }
6986 if (to_move > length) {
6987 /*- This should not happen either
6988 * since we always lower to_move to the size
6989 * of sp->length if its larger.
6990 */
6991#ifdef INVARIANTS
6992 panic("Huh, how can to_move be larger?");
6993#else
6994 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6995 sp->length = 0;
6996#endif
6997 } else {
6998 atomic_subtract_int(&sp->length, to_move);
6999 }
7000 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7001 /* Not enough room for a chunk header, get some */
7002 struct mbuf *m;
7003
7004 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7005 if (m == NULL) {
7006 /*
7007 * we're in trouble here. _PREPEND below will free
7008 * all the data if there is no leading space, so we
7009 * must put the data back and restore.
7010 */
7011 if (send_lock_up == 0) {
7012 SCTP_TCB_SEND_LOCK(stcb);
7013 send_lock_up = 1;
7014 }
7015 if (chk->data == NULL) {
7016 /* unsteal the data */
7017 sp->data = chk->data;
7018 sp->tail_mbuf = chk->last_mbuf;
7019 } else {
7020 struct mbuf *m_tmp;
7021
7022 /* reassemble the data */
7023 m_tmp = sp->data;
7024 sp->data = chk->data;
7025 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7026 }
7027 sp->some_taken = some_taken;
7028 atomic_add_int(&sp->length, to_move);
7029 chk->data = NULL;
7030 *bail = 1;
7031 sctp_free_a_chunk(stcb, chk);
7032 to_move = 0;
7033 goto out_of;
7034 } else {
7035 SCTP_BUF_LEN(m) = 0;
7036 SCTP_BUF_NEXT(m) = chk->data;
7037 chk->data = m;
7038 M_ALIGN(chk->data, 4);
7039 }
7040 }
7041 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7042 if (chk->data == NULL) {
7043 /* HELP, TSNH since we assured it would not above? */
7044#ifdef INVARIANTS
7045 panic("prepend failes HELP?");
7046#else
7047 SCTP_PRINTF("prepend fails HELP?\n");
7048 sctp_free_a_chunk(stcb, chk);
7049#endif
7050 *bail = 1;
7051 to_move = 0;
7052 goto out_of;
7053 }
7054 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7055 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7056 chk->book_size_scale = 0;
7057 chk->sent = SCTP_DATAGRAM_UNSENT;
7058
7059 chk->flags = 0;
7060 chk->asoc = &stcb->asoc;
7061 chk->pad_inplace = 0;
7062 chk->no_fr_allowed = 0;
7063 chk->rec.data.stream_seq = sp->strseq;
7064 chk->rec.data.stream_number = sp->stream;
7065 chk->rec.data.payloadtype = sp->ppid;
7066 chk->rec.data.context = sp->context;
7067 chk->rec.data.doing_fast_retransmit = 0;
7068 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
7069
7070 chk->rec.data.timetodrop = sp->ts;
7071 chk->flags = sp->act_flags;
7072 chk->addr_over = sp->addr_over;
7073
7074 chk->whoTo = net;
7075 atomic_add_int(&chk->whoTo->ref_count, 1);
7076
7077 if (sp->holds_key_ref) {
7078 chk->auth_keyid = sp->auth_keyid;
7079 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7080 chk->holds_key_ref = 1;
7081 }
7082 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7083 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7084 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7085 (uintptr_t) stcb, sp->length,
7086 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7087 chk->rec.data.TSN_seq);
7088 }
7089 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7090 /*
7091 * Put the rest of the things in place now. Size was done earlier in
7092 * previous loop prior to padding.
7093 */
7094
7095#ifdef SCTP_ASOCLOG_OF_TSNS
7096 SCTP_TCB_LOCK_ASSERT(stcb);
7097 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7098 asoc->tsn_out_at = 0;
7099 asoc->tsn_out_wrapped = 1;
7100 }
7101 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7102 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7103 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7104 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7105 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7106 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7107 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7108 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7109 asoc->tsn_out_at++;
7110#endif
7111
7112 dchkh->ch.chunk_type = SCTP_DATA;
7113 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7114 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7115 dchkh->dp.stream_id = htons(strq->stream_no);
7116 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7117 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7118 dchkh->ch.chunk_length = htons(chk->send_size);
7119 /* Now advance the chk->send_size by the actual pad needed. */
7120 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7121 /* need a pad */
7122 struct mbuf *lm;
7123 int pads;
7124
7125 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7126 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7127 chk->pad_inplace = 1;
7128 }
7129 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7130 /* pad added an mbuf */
7131 chk->last_mbuf = lm;
7132 }
7133 chk->send_size += pads;
7134 }
7135 /* We only re-set the policy if it is on */
7136 if (sp->pr_sctp_on) {
7137 sctp_set_prsctp_policy(sp);
7138 asoc->pr_sctp_cnt++;
7139 chk->pr_sctp_on = 1;
7140 } else {
7141 chk->pr_sctp_on = 0;
7142 }
7143 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7144 /* All done pull and kill the message */
7145 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7146 if (sp->put_last_out == 0) {
7147 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7148 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7149 sp->sender_all_done,
7150 sp->length,
7151 sp->msg_is_complete,
7152 sp->put_last_out,
7153 send_lock_up);
7154 }
7155 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7156 SCTP_TCB_SEND_LOCK(stcb);
7157 send_lock_up = 1;
7158 }
7159 TAILQ_REMOVE(&strq->outqueue, sp, next);
7160 sctp_free_remote_addr(sp->net);
7161 if (sp->data) {
7162 sctp_m_freem(sp->data);
7163 sp->data = NULL;
7164 }
7165 sctp_free_a_strmoq(stcb, sp);
7166
7167 /* we can't be locked to it */
7168 *locked = 0;
7169 stcb->asoc.locked_on_sending = NULL;
7170 } else {
7171 /* more to go, we are locked */
7172 *locked = 1;
7173 }
7174 asoc->chunks_on_out_queue++;
7175 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7176 asoc->send_queue_cnt++;
7177out_of:
7178 if (send_lock_up) {
7179 SCTP_TCB_SEND_UNLOCK(stcb);
7180 send_lock_up = 0;
7181 }
7182 return (to_move);
7183}
7184
7185
7186static struct sctp_stream_out *
7187sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7188{
7189 struct sctp_stream_out *strq;
7190
7191 /* Find the next stream to use */
7192 if (asoc->last_out_stream == NULL) {
7193 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7194 if (asoc->last_out_stream == NULL) {
7195 /* huh nothing on the wheel, TSNH */
7196 return (NULL);
7197 }
7198 goto done_it;
7199 }
7200 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7201done_it:
7202 if (strq == NULL) {
7203 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7204 }
7205 /* Save off the last stream */
7206 asoc->last_out_stream = strq;
7207 return (strq);
7208
7209}
7210
7211
7212static void
7213sctp_fill_outqueue(struct sctp_tcb *stcb,
7214 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7215{
7216 struct sctp_association *asoc;
7217 struct sctp_stream_out *strq, *strqn, *strqt;
7218 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7219 int locked, giveup;
7220 struct sctp_stream_queue_pending *sp;
7221
7222 SCTP_TCB_LOCK_ASSERT(stcb);
7223 asoc = &stcb->asoc;
7224#ifdef INET6
7225 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7226 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7227 } else {
7228 /* ?? not sure what else to do */
7229 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7230 }
7231#else
7232 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7233#endif
7234 /* Need an allowance for the data chunk header too */
7235 goal_mtu -= sizeof(struct sctp_data_chunk);
7236
7237 /* must make even word boundary */
7238 goal_mtu &= 0xfffffffc;
7239 if (asoc->locked_on_sending) {
7240 /* We are stuck on one stream until the message completes. */
7241 strqn = strq = asoc->locked_on_sending;
7242 locked = 1;
7243 } else {
7244 strqn = strq = sctp_select_a_stream(stcb, asoc);
7245 locked = 0;
7246 }
7247
7248 while ((goal_mtu > 0) && strq) {
7249 sp = TAILQ_FIRST(&strq->outqueue);
7250 /*
7251 * If CMT is off, we must validate that the stream in
7252 * question has the first item pointed towards are network
7253 * destionation requested by the caller. Note that if we
7254 * turn out to be locked to a stream (assigning TSN's then
7255 * we must stop, since we cannot look for another stream
7256 * with data to send to that destination). In CMT's case, by
7257 * skipping this check, we will send one data packet towards
7258 * the requested net.
7259 */
7260 if (sp == NULL) {
7261 break;
7262 }
7263 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7264 /* none for this network */
7265 if (locked) {
7266 break;
7267 } else {
7268 strq = sctp_select_a_stream(stcb, asoc);
7269 if (strq == NULL)
7270 /* none left */
7271 break;
7272 if (strqn == strq) {
7273 /* I have circled */
7274 break;
7275 }
7276 continue;
7277 }
7278 }
7279 giveup = 0;
7280 bail = 0;
7281 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7282 &giveup, eeor_mode, &bail);
7283 asoc->last_out_stream = strq;
7284 if (locked) {
7285 asoc->locked_on_sending = strq;
7286 if ((moved_how_much == 0) || (giveup) || bail)
7287 /* no more to move for now */
7288 break;
7289 } else {
7290 asoc->locked_on_sending = NULL;
7291 strqt = sctp_select_a_stream(stcb, asoc);
7292 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7293 if (strq == strqn) {
7294 /* Must move start to next one */
7295 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7296 if (strqn == NULL) {
7297 strqn = TAILQ_FIRST(&asoc->out_wheel);
7298 if (strqn == NULL) {
7299 break;
7300 }
7301 }
7302 }
7303 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7304 }
7305 if ((giveup) || bail) {
7306 break;
7307 }
7308 strq = strqt;
7309 if (strq == NULL) {
7310 break;
7311 }
7312 }
7313 total_moved += moved_how_much;
7314 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7315 goal_mtu &= 0xfffffffc;
7316 }
7317 if (bail)
7318 *quit_now = 1;
7319
7320 if (total_moved == 0) {
7321 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7322 (net == stcb->asoc.primary_destination)) {
7323 /* ran dry for primary network net */
7324 SCTP_STAT_INCR(sctps_primary_randry);
7325 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7326 /* ran dry with CMT on */
7327 SCTP_STAT_INCR(sctps_cmt_randry);
7328 }
7329 }
7330}
7331
7332void
7333sctp_fix_ecn_echo(struct sctp_association *asoc)
7334{
7335 struct sctp_tmit_chunk *chk;
7336
7337 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7338 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7339 chk->sent = SCTP_DATAGRAM_UNSENT;
7340 }
7341 }
7342}
7343
7344static void
7345sctp_move_to_an_alt(struct sctp_tcb *stcb,
7346 struct sctp_association *asoc,
7347 struct sctp_nets *net)
7348{
7349 struct sctp_tmit_chunk *chk;
7350 struct sctp_nets *a_net;
7351
7352 SCTP_TCB_LOCK_ASSERT(stcb);
7353 /*
7354 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7355 * destination using the PF algorithm for finding alternate
7356 * destinations.
7357 */
7358 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7359 a_net = sctp_find_alternate_net(stcb, net, 2);
7360 } else {
7361 a_net = sctp_find_alternate_net(stcb, net, 0);
7362 }
7363 if ((a_net != net) &&
7364 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7365 /*
7366 * We only proceed if a valid alternate is found that is not
7367 * this one and is reachable. Here we must move all chunks
7368 * queued in the send queue off of the destination address
7369 * to our alternate.
7370 */
7371 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7372 if (chk->whoTo == net) {
7373 /* Move the chunk to our alternate */
7374 sctp_free_remote_addr(chk->whoTo);
7375 chk->whoTo = a_net;
7376 atomic_add_int(&a_net->ref_count, 1);
7377 }
7378 }
7379 }
7380}
7381
7382int
7383sctp_med_chunk_output(struct sctp_inpcb *inp,
7384 struct sctp_tcb *stcb,
7385 struct sctp_association *asoc,
7386 int *num_out,
7387 int *reason_code,
7388 int control_only, int from_where,
7389 struct timeval *now, int *now_filled, int frag_point, int so_locked
7390#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7391 SCTP_UNUSED
7392#endif
7393)
7394{
7395 /*
7396 * Ok this is the generic chunk service queue. we must do the
7397 * following: - Service the stream queue that is next, moving any
7398 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7399 * LAST to the out queue in one pass) and assigning TSN's - Check to
7400 * see if the cwnd/rwnd allows any output, if so we go ahead and
7401 * fomulate and send the low level chunks. Making sure to combine
7402 * any control in the control chunk queue also.
7403 */
7404 struct sctp_nets *net, *start_at, *old_start_at = NULL;
7405 struct mbuf *outchain, *endoutchain;
7406 struct sctp_tmit_chunk *chk, *nchk;
7407
7408 /* temp arrays for unlinking */
7409 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7410 int no_fragmentflg, error;
7411 unsigned int max_rwnd_per_dest;
7412 int one_chunk, hbflag, skip_data_for_this_net;
7413 int asconf, cookie, no_out_cnt;
7414 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7415 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7416 int tsns_sent = 0;
7417 uint32_t auth_offset = 0;
7418 struct sctp_auth_chunk *auth = NULL;
7419 uint16_t auth_keyid;
7420 int override_ok = 1;
7421 int data_auth_reqd = 0;
7422
7423 /*
7424 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7425 * destination.
7426 */
7427 int pf_hbflag = 0;
7428 int quit_now = 0;
7429
7430 *num_out = 0;
7431 auth_keyid = stcb->asoc.authinfo.active_keyid;
7432
7433 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7434 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7435 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7436 eeor_mode = 1;
7437 } else {
7438 eeor_mode = 0;
7439 }
7440 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7441 /*
7442 * First lets prime the pump. For each destination, if there is room
7443 * in the flight size, attempt to pull an MTU's worth out of the
7444 * stream queues into the general send_queue
7445 */
7446#ifdef SCTP_AUDITING_ENABLED
7447 sctp_audit_log(0xC2, 2);
7448#endif
7449 SCTP_TCB_LOCK_ASSERT(stcb);
7450 hbflag = 0;
7451 if ((control_only) || (asoc->stream_reset_outstanding))
7452 no_data_chunks = 1;
7453 else
7454 no_data_chunks = 0;
7455
7456 /* Nothing to possible to send? */
7457 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7458 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7459 TAILQ_EMPTY(&asoc->send_queue) &&
7460 TAILQ_EMPTY(&asoc->out_wheel)) {
7461 *reason_code = 9;
7462 return (0);
7463 }
7464 if (asoc->peers_rwnd == 0) {
7465 /* No room in peers rwnd */
7466 *reason_code = 1;
7467 if (asoc->total_flight > 0) {
7468 /* we are allowed one chunk in flight */
7469 no_data_chunks = 1;
7470 }
7471 }
7472 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7473 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7474 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7475 /*
7476 * This for loop we are in takes in each net, if
7477 * its's got space in cwnd and has data sent to it
7478 * (when CMT is off) then it calls
7479 * sctp_fill_outqueue for the net. This gets data on
7480 * the send queue for that network.
7481 *
7482 * In sctp_fill_outqueue TSN's are assigned and data is
7483 * copied out of the stream buffers. Note mostly
7484 * copy by reference (we hope).
7485 */
7486 net->window_probe = 0;
7487 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7488 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7489 sctp_log_cwnd(stcb, net, 1,
7490 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7491 }
7492 continue;
7493 }
7494 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7495 /* nothing can be in queue for this guy */
7496 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7497 sctp_log_cwnd(stcb, net, 2,
7498 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7499 }
7500 continue;
7501 }
7502 if (net->flight_size >= net->cwnd) {
7503 /* skip this network, no room - can't fill */
7504 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7505 sctp_log_cwnd(stcb, net, 3,
7506 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7507 }
7508 continue;
7509 }
7510 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7511 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7512 }
7513 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7514 if (quit_now) {
7515 /* memory alloc failure */
7516 no_data_chunks = 1;
7517 break;
7518 }
7519 }
7520 }
7521 /* now service each destination and send out what we can for it */
7522 /* Nothing to send? */
7523 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7524 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7525 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7526 *reason_code = 8;
7527 return (0);
7528 }
7529 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7530 /* get the last start point */
7531 start_at = asoc->last_net_cmt_send_started;
7532 if (start_at == NULL) {
7533 /* null so to beginning */
7534 start_at = TAILQ_FIRST(&asoc->nets);
7535 } else {
7536 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7537 if (start_at == NULL) {
7538 start_at = TAILQ_FIRST(&asoc->nets);
7539 }
7540 }
7541 asoc->last_net_cmt_send_started = start_at;
7542 } else {
7543 start_at = TAILQ_FIRST(&asoc->nets);
7544 }
7545 old_start_at = NULL;
7546again_one_more_time:
7547 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7548 /* how much can we send? */
7549 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7550 if (old_start_at && (old_start_at == net)) {
7551 /* through list ocmpletely. */
7552 break;
7553 }
7554 tsns_sent = 0xa;
7555 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7556 /*
7557 * Ref-count of 1 so we cannot have data or control
7558 * queued to this address. Skip it (non-CMT).
7559 */
7560 continue;
7561 }
7562 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7563 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7564 (net->flight_size >= net->cwnd)) {
7565 /*
7566 * Nothing on control or asconf and flight is full,
7567 * we can skip even in the CMT case.
7568 */
7569 continue;
7570 }
7571 ctl_cnt = bundle_at = 0;
7572 endoutchain = outchain = NULL;
7573 no_fragmentflg = 1;
7574 one_chunk = 0;
7575 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7576 skip_data_for_this_net = 1;
7577 } else {
7578 skip_data_for_this_net = 0;
7579 }
7580 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7581 /*
7582 * if we have a route and an ifp check to see if we
7583 * have room to send to this guy
7584 */
7585 struct ifnet *ifp;
7586
7587 ifp = net->ro.ro_rt->rt_ifp;
7588 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7589 SCTP_STAT_INCR(sctps_ifnomemqueued);
7590 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7591 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7592 }
7593 continue;
7594 }
7595 }
7596 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7597 case AF_INET:
7598 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7599 break;
7600#ifdef INET6
7601 case AF_INET6:
7602 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7603 break;
7604#endif
7605 default:
7606 /* TSNH */
7607 mtu = net->mtu;
7608 break;
7609 }
7610 mx_mtu = mtu;
7611 to_out = 0;
7612 if (mtu > asoc->peers_rwnd) {
7613 if (asoc->total_flight > 0) {
7614 /* We have a packet in flight somewhere */
7615 r_mtu = asoc->peers_rwnd;
7616 } else {
7617 /* We are always allowed to send one MTU out */
7618 one_chunk = 1;
7619 r_mtu = mtu;
7620 }
7621 } else {
7622 r_mtu = mtu;
7623 }
7624 /************************/
7625 /* ASCONF transmission */
7626 /************************/
7627 /* Now first lets go through the asconf queue */
7628 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7629 chk; chk = nchk) {
7630 nchk = TAILQ_NEXT(chk, sctp_next);
7631 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7632 continue;
7633 }
7634 if (chk->whoTo != net) {
7635 /*
7636 * No, not sent to the network we are
7637 * looking at
7638 */
7639 break;
7640 }
7641 if (chk->data == NULL) {
7642 break;
7643 }
7644 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7645 chk->sent != SCTP_DATAGRAM_RESEND) {
7646 break;
7647 }
7648 /*
7649 * if no AUTH is yet included and this chunk
7650 * requires it, make sure to account for it. We
7651 * don't apply the size until the AUTH chunk is
7652 * actually added below in case there is no room for
7653 * this chunk. NOTE: we overload the use of "omtu"
7654 * here
7655 */
7656 if ((auth == NULL) &&
7657 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7658 stcb->asoc.peer_auth_chunks)) {
7659 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7660 } else
7661 omtu = 0;
7662 /* Here we do NOT factor the r_mtu */
7663 if ((chk->send_size < (int)(mtu - omtu)) ||
7664 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7665 /*
7666 * We probably should glom the mbuf chain
7667 * from the chk->data for control but the
7668 * problem is it becomes yet one more level
7669 * of tracking to do if for some reason
7670 * output fails. Then I have got to
7671 * reconstruct the merged control chain.. el
7672 * yucko.. for now we take the easy way and
7673 * do the copy
7674 */
7675 /*
7676 * Add an AUTH chunk, if chunk requires it
7677 * save the offset into the chain for AUTH
7678 */
7679 if ((auth == NULL) &&
7680 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7681 stcb->asoc.peer_auth_chunks))) {
7682 outchain = sctp_add_auth_chunk(outchain,
7683 &endoutchain,
7684 &auth,
7685 &auth_offset,
7686 stcb,
7687 chk->rec.chunk_id.id);
7688 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7689 }
7690 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7691 (int)chk->rec.chunk_id.can_take_data,
7692 chk->send_size, chk->copy_by_ref);
7693 if (outchain == NULL) {
7694 *reason_code = 8;
7695 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7696 return (ENOMEM);
7697 }
7698 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7699 /* update our MTU size */
7700 if (mtu > (chk->send_size + omtu))
7701 mtu -= (chk->send_size + omtu);
7702 else
7703 mtu = 0;
7704 to_out += (chk->send_size + omtu);
7705 /* Do clear IP_DF ? */
7706 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7707 no_fragmentflg = 0;
7708 }
7709 if (chk->rec.chunk_id.can_take_data)
7710 chk->data = NULL;
7711 /*
7712 * set hb flag since we can use these for
7713 * RTO
7714 */
7715 hbflag = 1;
7716 asconf = 1;
7717 /*
7718 * should sysctl this: don't bundle data
7719 * with ASCONF since it requires AUTH
7720 */
7721 no_data_chunks = 1;
7722 chk->sent = SCTP_DATAGRAM_SENT;
7723 chk->snd_count++;
7724 if (mtu == 0) {
7725 /*
7726 * Ok we are out of room but we can
7727 * output without effecting the
7728 * flight size since this little guy
7729 * is a control only packet.
7730 */
7731 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7732 /*
7733 * do NOT clear the asconf flag as
7734 * it is used to do appropriate
7735 * source address selection.
7736 */
7737 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7738 (struct sockaddr *)&net->ro._l_addr,
7739 outchain, auth_offset, auth,
7740 stcb->asoc.authinfo.active_keyid,
7741 no_fragmentflg, 0, NULL, asconf,
7742 inp->sctp_lport, stcb->rport,
7743 htonl(stcb->asoc.peer_vtag),
7744 net->port, so_locked, NULL))) {
7745 if (error == ENOBUFS) {
7746 asoc->ifp_had_enobuf = 1;
7747 SCTP_STAT_INCR(sctps_lowlevelerr);
7748 }
7749 if (from_where == 0) {
7750 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7751 }
7752 if (*now_filled == 0) {
7753 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7754 *now_filled = 1;
7755 *now = net->last_sent_time;
7756 } else {
7757 net->last_sent_time = *now;
7758 }
7759 hbflag = 0;
7760 /* error, could not output */
7761 if (error == EHOSTUNREACH) {
7762 /*
7763 * Destination went
7764 * unreachable
7765 * during this send
7766 */
7767 sctp_move_to_an_alt(stcb, asoc, net);
7768 }
7769 *reason_code = 7;
7770 continue;
7771 } else
7772 asoc->ifp_had_enobuf = 0;
7773 if (*now_filled == 0) {
7774 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7775 *now_filled = 1;
7776 *now = net->last_sent_time;
7777 } else {
7778 net->last_sent_time = *now;
7779 }
7780 hbflag = 0;
7781 /*
7782 * increase the number we sent, if a
7783 * cookie is sent we don't tell them
7784 * any was sent out.
7785 */
7786 outchain = endoutchain = NULL;
7787 auth = NULL;
7788 auth_offset = 0;
7789 if (!no_out_cnt)
7790 *num_out += ctl_cnt;
7791 /* recalc a clean slate and setup */
7792 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7793 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7794 } else {
7795 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7796 }
7797 to_out = 0;
7798 no_fragmentflg = 1;
7799 }
7800 }
7801 }
7802 /************************/
7803 /* Control transmission */
7804 /************************/
7805 /* Now first lets go through the control queue */
7806 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7807 chk; chk = nchk) {
7808 nchk = TAILQ_NEXT(chk, sctp_next);
7809 if (chk->whoTo != net) {
7810 /*
7811 * No, not sent to the network we are
7812 * looking at
7813 */
7814 continue;
7815 }
7816 if (chk->data == NULL) {
7817 continue;
7818 }
7819 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7820 /*
7821 * It must be unsent. Cookies and ASCONF's
7822 * hang around but there timers will force
7823 * when marked for resend.
7824 */
7825 continue;
7826 }
7827 /*
7828 * if no AUTH is yet included and this chunk
7829 * requires it, make sure to account for it. We
7830 * don't apply the size until the AUTH chunk is
7831 * actually added below in case there is no room for
7832 * this chunk. NOTE: we overload the use of "omtu"
7833 * here
7834 */
7835 if ((auth == NULL) &&
7836 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7837 stcb->asoc.peer_auth_chunks)) {
7838 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7839 } else
7840 omtu = 0;
7841 /* Here we do NOT factor the r_mtu */
7842 if ((chk->send_size < (int)(mtu - omtu)) ||
7843 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7844 /*
7845 * We probably should glom the mbuf chain
7846 * from the chk->data for control but the
7847 * problem is it becomes yet one more level
7848 * of tracking to do if for some reason
7849 * output fails. Then I have got to
7850 * reconstruct the merged control chain.. el
7851 * yucko.. for now we take the easy way and
7852 * do the copy
7853 */
7854 /*
7855 * Add an AUTH chunk, if chunk requires it
7856 * save the offset into the chain for AUTH
7857 */
7858 if ((auth == NULL) &&
7859 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7860 stcb->asoc.peer_auth_chunks))) {
7861 outchain = sctp_add_auth_chunk(outchain,
7862 &endoutchain,
7863 &auth,
7864 &auth_offset,
7865 stcb,
7866 chk->rec.chunk_id.id);
7867 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7868 }
7869 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7870 (int)chk->rec.chunk_id.can_take_data,
7871 chk->send_size, chk->copy_by_ref);
7872 if (outchain == NULL) {
7873 *reason_code = 8;
7874 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7875 return (ENOMEM);
7876 }
7877 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7878 /* update our MTU size */
7879 if (mtu > (chk->send_size + omtu))
7880 mtu -= (chk->send_size + omtu);
7881 else
7882 mtu = 0;
7883 to_out += (chk->send_size + omtu);
7884 /* Do clear IP_DF ? */
7885 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7886 no_fragmentflg = 0;
7887 }
7888 if (chk->rec.chunk_id.can_take_data)
7889 chk->data = NULL;
7890 /* Mark things to be removed, if needed */
7891 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7892 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7893 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7894 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7895 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7896 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7897 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7898 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7899 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7900 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7901 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7902
7903 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7904 hbflag = 1;
7905 /*
7906 * JRS 5/14/07 - Set the
7907 * flag to say a heartbeat
7908 * is being sent.
7909 */
7910 pf_hbflag = 1;
7911 }
7912 /* remove these chunks at the end */
7913 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7914 /* turn off the timer */
7915 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7916 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7917 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7918 }
7919 }
7920 /*
7921 * EY -Nr-sack version of the above
7922 * if statement
7923 */
7924 if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
7925 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { /* EY !?! */
7926 /* turn off the timer */
7927 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7928 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7929 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7930 }
7931 }
7932 ctl_cnt++;
7933 } else {
7934 /*
7935 * Other chunks, since they have
7936 * timers running (i.e. COOKIE) we
7937 * just "trust" that it gets sent or
7938 * retransmitted.
7939 */
7940 ctl_cnt++;
7941 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7942 cookie = 1;
7943 no_out_cnt = 1;
7944 }
7945 chk->sent = SCTP_DATAGRAM_SENT;
7946 chk->snd_count++;
7947 }
7948 if (mtu == 0) {
7949 /*
7950 * Ok we are out of room but we can
7951 * output without effecting the
7952 * flight size since this little guy
7953 * is a control only packet.
7954 */
7955 if (asconf) {
7956 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7957 /*
7958 * do NOT clear the asconf
7959 * flag as it is used to do
7960 * appropriate source
7961 * address selection.
7962 */
7963 }
7964 if (cookie) {
7965 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7966 cookie = 0;
7967 }
7968 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7969 (struct sockaddr *)&net->ro._l_addr,
7970 outchain,
7971 auth_offset, auth,
7972 stcb->asoc.authinfo.active_keyid,
7973 no_fragmentflg, 0, NULL, asconf,
7974 inp->sctp_lport, stcb->rport,
7975 htonl(stcb->asoc.peer_vtag),
7976 net->port, so_locked, NULL))) {
7977 if (error == ENOBUFS) {
7978 asoc->ifp_had_enobuf = 1;
7979 SCTP_STAT_INCR(sctps_lowlevelerr);
7980 }
7981 if (from_where == 0) {
7982 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7983 }
7984 /* error, could not output */
7985 if (hbflag) {
7986 if (*now_filled == 0) {
7987 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7988 *now_filled = 1;
7989 *now = net->last_sent_time;
7990 } else {
7991 net->last_sent_time = *now;
7992 }
7993 hbflag = 0;
7994 }
7995 if (error == EHOSTUNREACH) {
7996 /*
7997 * Destination went
7998 * unreachable
7999 * during this send
8000 */
8001 sctp_move_to_an_alt(stcb, asoc, net);
8002 }
8003 *reason_code = 7;
8004 continue;
8005 } else
8006 asoc->ifp_had_enobuf = 0;
8007 /* Only HB or ASCONF advances time */
8008 if (hbflag) {
8009 if (*now_filled == 0) {
8010 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8011 *now_filled = 1;
8012 *now = net->last_sent_time;
8013 } else {
8014 net->last_sent_time = *now;
8015 }
8016 hbflag = 0;
8017 }
8018 /*
8019 * increase the number we sent, if a
8020 * cookie is sent we don't tell them
8021 * any was sent out.
8022 */
8023 outchain = endoutchain = NULL;
8024 auth = NULL;
8025 auth_offset = 0;
8026 if (!no_out_cnt)
8027 *num_out += ctl_cnt;
8028 /* recalc a clean slate and setup */
8029 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8030 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8031 } else {
8032 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8033 }
8034 to_out = 0;
8035 no_fragmentflg = 1;
8036 }
8037 }
8038 }
8039 /* JRI: if dest is in PF state, do not send data to it */
8040 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8041 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8042 (net->dest_state & SCTP_ADDR_PF)) {
8043 goto no_data_fill;
8044 }
8045 if (net->flight_size >= net->cwnd) {
8046 goto no_data_fill;
8047 }
8048 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
8049 (net->flight_size > max_rwnd_per_dest)) {
8050 goto no_data_fill;
8051 }
8052 /*********************/
8053 /* Data transmission */
8054 /*********************/
8055 /*
8056 * if AUTH for DATA is required and no AUTH has been added
8057 * yet, account for this in the mtu now... if no data can be
8058 * bundled, this adjustment won't matter anyways since the
8059 * packet will be going out...
8060 */
8061 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8062 stcb->asoc.peer_auth_chunks);
8063 if (data_auth_reqd && (auth == NULL)) {
8064 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8065 }
8066 /* now lets add any data within the MTU constraints */
8067 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8068 case AF_INET:
8069 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8070 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8071 else
8072 omtu = 0;
8073 break;
8074#ifdef INET6
8075 case AF_INET6:
8076 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8077 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8078 else
8079 omtu = 0;
8080 break;
8081#endif
8082 default:
8083 /* TSNH */
8084 omtu = 0;
8085 break;
8086 }
8087 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8088 (skip_data_for_this_net == 0)) ||
8089 (cookie)) {
8090 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8091 if (no_data_chunks) {
8092 /* let only control go out */
8093 *reason_code = 1;
8094 break;
8095 }
8096 if (net->flight_size >= net->cwnd) {
8097 /* skip this net, no room for data */
8098 *reason_code = 2;
8099 break;
8100 }
8101 nchk = TAILQ_NEXT(chk, sctp_next);
8102 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8103 if (chk->whoTo != net) {
8104 /*
8105 * For CMT, steal the data
8106 * to this network if its
8107 * not set here.
8108 */
8109 sctp_free_remote_addr(chk->whoTo);
8110 chk->whoTo = net;
8111 atomic_add_int(&chk->whoTo->ref_count, 1);
8112 }
8113 } else if (chk->whoTo != net) {
8114 /* No, not sent to this net */
8115 continue;
8116 }
8117 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8118 /*-
8119 * strange, we have a chunk that is
8120 * to big for its destination and
8121 * yet no fragment ok flag.
8122 * Something went wrong when the
8123 * PMTU changed...we did not mark
8124 * this chunk for some reason?? I
8125 * will fix it here by letting IP
8126 * fragment it for now and printing
8127 * a warning. This really should not
8128 * happen ...
8129 */
8130 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8131 chk->send_size, mtu);
8132 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8133 }
8134 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8135 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8136 struct sctp_data_chunk *dchkh;
8137
8138 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8139 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8140 }
8141 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8142 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8143 /* ok we will add this one */
8144
8145 /*
8146 * Add an AUTH chunk, if chunk
8147 * requires it, save the offset into
8148 * the chain for AUTH
8149 */
8150 if (data_auth_reqd) {
8151 if (auth == NULL) {
8152 outchain = sctp_add_auth_chunk(outchain,
8153 &endoutchain,
8154 &auth,
8155 &auth_offset,
8156 stcb,
8157 SCTP_DATA);
8158 auth_keyid = chk->auth_keyid;
8159 override_ok = 0;
8160 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8161 } else if (override_ok) {
8162 /*
8163 * use this data's
8164 * keyid
8165 */
8166 auth_keyid = chk->auth_keyid;
8167 override_ok = 0;
8168 } else if (auth_keyid != chk->auth_keyid) {
8169 /*
8170 * different keyid,
8171 * so done bundling
8172 */
8173 break;
8174 }
8175 }
8176 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8177 chk->send_size, chk->copy_by_ref);
8178 if (outchain == NULL) {
8179 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8180 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8181 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8182 }
8183 *reason_code = 3;
8184 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8185 return (ENOMEM);
8186 }
8187 /* upate our MTU size */
8188 /* Do clear IP_DF ? */
8189 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8190 no_fragmentflg = 0;
8191 }
8192 /* unsigned subtraction of mtu */
8193 if (mtu > chk->send_size)
8194 mtu -= chk->send_size;
8195 else
8196 mtu = 0;
8197 /* unsigned subtraction of r_mtu */
8198 if (r_mtu > chk->send_size)
8199 r_mtu -= chk->send_size;
8200 else
8201 r_mtu = 0;
8202
8203 to_out += chk->send_size;
8204 if ((to_out > mx_mtu) && no_fragmentflg) {
8205#ifdef INVARIANTS
8206 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8207#else
8208 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8209 mx_mtu, to_out);
8210#endif
8211 }
8212 chk->window_probe = 0;
8213 data_list[bundle_at++] = chk;
8214 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8215 mtu = 0;
8216 break;
8217 }
8218 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8219 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8220 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8221 } else {
8222 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8223 }
8224 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8225 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8226 /*
8227 * Count number of
8228 * user msg's that
8229 * were fragmented
8230 * we do this by
8231 * counting when we
8232 * see a LAST
8233 * fragment only.
8234 */
8235 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8236 }
8237 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8238 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8239 data_list[0]->window_probe = 1;
8240 net->window_probe = 1;
8241 }
8242 break;
8243 }
8244 } else {
8245 /*
8246 * Must be sent in order of the
8247 * TSN's (on a network)
8248 */
8249 break;
8250 }
8251 } /* for (chunk gather loop for this net) */
8252 } /* if asoc.state OPEN */
8253no_data_fill:
8254 /* Is there something to send for this destination? */
8255 if (outchain) {
8256 /* We may need to start a control timer or two */
8257 if (asconf) {
8258 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8259 stcb, net);
8260 /*
8261 * do NOT clear the asconf flag as it is
8262 * used to do appropriate source address
8263 * selection.
8264 */
8265 }
8266 if (cookie) {
8267 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8268 cookie = 0;
8269 }
8270 /* must start a send timer if data is being sent */
8271 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8272 /*
8273 * no timer running on this destination
8274 * restart it.
8275 */
8276 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8277 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8278 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8279 pf_hbflag &&
8280 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8281 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8282 /*
8283 * JRS 5/14/07 - If a HB has been sent to a
8284 * PF destination and no T3 timer is
8285 * currently running, start the T3 timer to
8286 * track the HBs that were sent.
8287 */
8288 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8289 }
8290 /* Now send it, if there is anything to send :> */
8291 if ((error = sctp_lowlevel_chunk_output(inp,
8292 stcb,
8293 net,
8294 (struct sockaddr *)&net->ro._l_addr,
8295 outchain,
8296 auth_offset,
8297 auth,
8298 auth_keyid,
8299 no_fragmentflg,
8300 bundle_at,
8301 data_list[0],
8302 asconf,
8303 inp->sctp_lport, stcb->rport,
8304 htonl(stcb->asoc.peer_vtag),
8305 net->port, so_locked, NULL))) {
8306 /* error, we could not output */
8307 if (error == ENOBUFS) {
8308 SCTP_STAT_INCR(sctps_lowlevelerr);
8309 asoc->ifp_had_enobuf = 1;
8310 }
8311 if (from_where == 0) {
8312 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8313 }
8314 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8315 if (hbflag) {
8316 if (*now_filled == 0) {
8317 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8318 *now_filled = 1;
8319 *now = net->last_sent_time;
8320 } else {
8321 net->last_sent_time = *now;
8322 }
8323 hbflag = 0;
8324 }
8325 if (error == EHOSTUNREACH) {
8326 /*
8327 * Destination went unreachable
8328 * during this send
8329 */
8330 sctp_move_to_an_alt(stcb, asoc, net);
8331 }
8332 *reason_code = 6;
8333 /*-
8334 * I add this line to be paranoid. As far as
8335 * I can tell the continue, takes us back to
8336 * the top of the for, but just to make sure
8337 * I will reset these again here.
8338 */
8339 ctl_cnt = bundle_at = 0;
8340 continue; /* This takes us back to the
8341 * for() for the nets. */
8342 } else {
8343 asoc->ifp_had_enobuf = 0;
8344 }
8345 outchain = endoutchain = NULL;
8346 auth = NULL;
8347 auth_offset = 0;
8348 if (bundle_at || hbflag) {
8349 /* For data/asconf and hb set time */
8350 if (*now_filled == 0) {
8351 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8352 *now_filled = 1;
8353 *now = net->last_sent_time;
8354 } else {
8355 net->last_sent_time = *now;
8356 }
8357 }
8358 if (!no_out_cnt) {
8359 *num_out += (ctl_cnt + bundle_at);
8360 }
8361 if (bundle_at) {
8362 /* setup for a RTO measurement */
8363 tsns_sent = data_list[0]->rec.data.TSN_seq;
8364 /* fill time if not already filled */
8365 if (*now_filled == 0) {
8366 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8367 *now_filled = 1;
8368 *now = asoc->time_last_sent;
8369 } else {
8370 asoc->time_last_sent = *now;
8371 }
8372 data_list[0]->do_rtt = 1;
8373 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8374 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8375 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8376 if (net->flight_size < net->cwnd) {
8377 /* start or restart it */
8378 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8379 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8380 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8381 }
8382 SCTP_STAT_INCR(sctps_earlyfrstrout);
8383 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8384 } else {
8385 /* stop it if its running */
8386 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8387 SCTP_STAT_INCR(sctps_earlyfrstpout);
8388 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8389 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8390 }
8391 }
8392 }
8393 }
8394 if (one_chunk) {
8395 break;
8396 }
8397 }
8398 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8399 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8400 }
8401 }
8402 if (old_start_at == NULL) {
8403 old_start_at = start_at;
8404 start_at = TAILQ_FIRST(&asoc->nets);
8405 if (old_start_at)
8406 goto again_one_more_time;
8407 }
8408 /*
8409 * At the end there should be no NON timed chunks hanging on this
8410 * queue.
8411 */
8412 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8413 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8414 }
8415 if ((*num_out == 0) && (*reason_code == 0)) {
8416 *reason_code = 4;
8417 } else {
8418 *reason_code = 5;
8419 }
8420 sctp_clean_up_ctl(stcb, asoc);
8421 return (0);
8422}
8423
8424void
8425sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8426{
8427 /*-
8428 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8429 * the control chunk queue.
8430 */
8431 struct sctp_chunkhdr *hdr;
8432 struct sctp_tmit_chunk *chk;
8433 struct mbuf *mat;
8434
8435 SCTP_TCB_LOCK_ASSERT(stcb);
8436 sctp_alloc_a_chunk(stcb, chk);
8437 if (chk == NULL) {
8438 /* no memory */
8439 sctp_m_freem(op_err);
8440 return;
8441 }
8442 chk->copy_by_ref = 0;
8443 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8444 if (op_err == NULL) {
8445 sctp_free_a_chunk(stcb, chk);
8446 return;
8447 }
8448 chk->send_size = 0;
8449 mat = op_err;
8450 while (mat != NULL) {
8451 chk->send_size += SCTP_BUF_LEN(mat);
8452 mat = SCTP_BUF_NEXT(mat);
8453 }
8454 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8455 chk->rec.chunk_id.can_take_data = 1;
8456 chk->sent = SCTP_DATAGRAM_UNSENT;
8457 chk->snd_count = 0;
8458 chk->flags = 0;
8459 chk->asoc = &stcb->asoc;
8460 chk->data = op_err;
8461 chk->whoTo = chk->asoc->primary_destination;
8462 atomic_add_int(&chk->whoTo->ref_count, 1);
8463 hdr = mtod(op_err, struct sctp_chunkhdr *);
8464 hdr->chunk_type = SCTP_OPERATION_ERROR;
8465 hdr->chunk_flags = 0;
8466 hdr->chunk_length = htons(chk->send_size);
8467 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8468 chk,
8469 sctp_next);
8470 chk->asoc->ctrl_queue_cnt++;
8471}
8472
8473int
8474sctp_send_cookie_echo(struct mbuf *m,
8475 int offset,
8476 struct sctp_tcb *stcb,
8477 struct sctp_nets *net)
8478{
8479 /*-
8480 * pull out the cookie and put it at the front of the control chunk
8481 * queue.
8482 */
8483 int at;
8484 struct mbuf *cookie;
8485 struct sctp_paramhdr parm, *phdr;
8486 struct sctp_chunkhdr *hdr;
8487 struct sctp_tmit_chunk *chk;
8488 uint16_t ptype, plen;
8489
8490 /* First find the cookie in the param area */
8491 cookie = NULL;
8492 at = offset + sizeof(struct sctp_init_chunk);
8493
8494 SCTP_TCB_LOCK_ASSERT(stcb);
8495 do {
8496 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8497 if (phdr == NULL) {
8498 return (-3);
8499 }
8500 ptype = ntohs(phdr->param_type);
8501 plen = ntohs(phdr->param_length);
8502 if (ptype == SCTP_STATE_COOKIE) {
8503 int pad;
8504
8505 /* found the cookie */
8506 if ((pad = (plen % 4))) {
8507 plen += 4 - pad;
8508 }
8509 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8510 if (cookie == NULL) {
8511 /* No memory */
8512 return (-2);
8513 }
8514#ifdef SCTP_MBUF_LOGGING
8515 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8516 struct mbuf *mat;
8517
8518 mat = cookie;
8519 while (mat) {
8520 if (SCTP_BUF_IS_EXTENDED(mat)) {
8521 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8522 }
8523 mat = SCTP_BUF_NEXT(mat);
8524 }
8525 }
8526#endif
8527 break;
8528 }
8529 at += SCTP_SIZE32(plen);
8530 } while (phdr);
8531 if (cookie == NULL) {
8532 /* Did not find the cookie */
8533 return (-3);
8534 }
8535 /* ok, we got the cookie lets change it into a cookie echo chunk */
8536
8537 /* first the change from param to cookie */
8538 hdr = mtod(cookie, struct sctp_chunkhdr *);
8539 hdr->chunk_type = SCTP_COOKIE_ECHO;
8540 hdr->chunk_flags = 0;
8541 /* get the chunk stuff now and place it in the FRONT of the queue */
8542 sctp_alloc_a_chunk(stcb, chk);
8543 if (chk == NULL) {
8544 /* no memory */
8545 sctp_m_freem(cookie);
8546 return (-5);
8547 }
8548 chk->copy_by_ref = 0;
8549 chk->send_size = plen;
8550 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8551 chk->rec.chunk_id.can_take_data = 0;
8552 chk->sent = SCTP_DATAGRAM_UNSENT;
8553 chk->snd_count = 0;
8554 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8555 chk->asoc = &stcb->asoc;
8556 chk->data = cookie;
8557 chk->whoTo = chk->asoc->primary_destination;
8558 atomic_add_int(&chk->whoTo->ref_count, 1);
8559 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8560 chk->asoc->ctrl_queue_cnt++;
8561 return (0);
8562}
8563
8564void
8565sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8566 struct mbuf *m,
8567 int offset,
8568 int chk_length,
8569 struct sctp_nets *net)
8570{
8571 /*
8572 * take a HB request and make it into a HB ack and send it.
8573 */
8574 struct mbuf *outchain;
8575 struct sctp_chunkhdr *chdr;
8576 struct sctp_tmit_chunk *chk;
8577
8578
8579 if (net == NULL)
8580 /* must have a net pointer */
8581 return;
8582
8583 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8584 if (outchain == NULL) {
8585 /* gak out of memory */
8586 return;
8587 }
8588#ifdef SCTP_MBUF_LOGGING
8589 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8590 struct mbuf *mat;
8591
8592 mat = outchain;
8593 while (mat) {
8594 if (SCTP_BUF_IS_EXTENDED(mat)) {
8595 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8596 }
8597 mat = SCTP_BUF_NEXT(mat);
8598 }
8599 }
8600#endif
8601 chdr = mtod(outchain, struct sctp_chunkhdr *);
8602 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8603 chdr->chunk_flags = 0;
8604 if (chk_length % 4) {
8605 /* need pad */
8606 uint32_t cpthis = 0;
8607 int padlen;
8608
8609 padlen = 4 - (chk_length % 4);
8610 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8611 }
8612 sctp_alloc_a_chunk(stcb, chk);
8613 if (chk == NULL) {
8614 /* no memory */
8615 sctp_m_freem(outchain);
8616 return;
8617 }
8618 chk->copy_by_ref = 0;
8619 chk->send_size = chk_length;
8620 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8621 chk->rec.chunk_id.can_take_data = 1;
8622 chk->sent = SCTP_DATAGRAM_UNSENT;
8623 chk->snd_count = 0;
8624 chk->flags = 0;
8625 chk->asoc = &stcb->asoc;
8626 chk->data = outchain;
8627 chk->whoTo = net;
8628 atomic_add_int(&chk->whoTo->ref_count, 1);
8629 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8630 chk->asoc->ctrl_queue_cnt++;
8631}
8632
8633void
8634sctp_send_cookie_ack(struct sctp_tcb *stcb)
8635{
8636 /* formulate and queue a cookie-ack back to sender */
8637 struct mbuf *cookie_ack;
8638 struct sctp_chunkhdr *hdr;
8639 struct sctp_tmit_chunk *chk;
8640
8641 cookie_ack = NULL;
8642 SCTP_TCB_LOCK_ASSERT(stcb);
8643
8644 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8645 if (cookie_ack == NULL) {
8646 /* no mbuf's */
8647 return;
8648 }
8649 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8650 sctp_alloc_a_chunk(stcb, chk);
8651 if (chk == NULL) {
8652 /* no memory */
8653 sctp_m_freem(cookie_ack);
8654 return;
8655 }
8656 chk->copy_by_ref = 0;
8657 chk->send_size = sizeof(struct sctp_chunkhdr);
8658 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8659 chk->rec.chunk_id.can_take_data = 1;
8660 chk->sent = SCTP_DATAGRAM_UNSENT;
8661 chk->snd_count = 0;
8662 chk->flags = 0;
8663 chk->asoc = &stcb->asoc;
8664 chk->data = cookie_ack;
8665 if (chk->asoc->last_control_chunk_from != NULL) {
8666 chk->whoTo = chk->asoc->last_control_chunk_from;
8667 } else {
8668 chk->whoTo = chk->asoc->primary_destination;
8669 }
8670 atomic_add_int(&chk->whoTo->ref_count, 1);
8671 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8672 hdr->chunk_type = SCTP_COOKIE_ACK;
8673 hdr->chunk_flags = 0;
8674 hdr->chunk_length = htons(chk->send_size);
8675 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8676 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8677 chk->asoc->ctrl_queue_cnt++;
8678 return;
8679}
8680
8681
8682void
8683sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8684{
8685 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8686 struct mbuf *m_shutdown_ack;
8687 struct sctp_shutdown_ack_chunk *ack_cp;
8688 struct sctp_tmit_chunk *chk;
8689
8690 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8691 if (m_shutdown_ack == NULL) {
8692 /* no mbuf's */
8693 return;
8694 }
8695 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8696 sctp_alloc_a_chunk(stcb, chk);
8697 if (chk == NULL) {
8698 /* no memory */
8699 sctp_m_freem(m_shutdown_ack);
8700 return;
8701 }
8702 chk->copy_by_ref = 0;
8703 chk->send_size = sizeof(struct sctp_chunkhdr);
8704 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8705 chk->rec.chunk_id.can_take_data = 1;
8706 chk->sent = SCTP_DATAGRAM_UNSENT;
8707 chk->snd_count = 0;
8708 chk->flags = 0;
8709 chk->asoc = &stcb->asoc;
8710 chk->data = m_shutdown_ack;
8711 chk->whoTo = net;
8712 atomic_add_int(&net->ref_count, 1);
8713
8714 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8715 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8716 ack_cp->ch.chunk_flags = 0;
8717 ack_cp->ch.chunk_length = htons(chk->send_size);
8718 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8719 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8720 chk->asoc->ctrl_queue_cnt++;
8721 return;
8722}
8723
8724void
8725sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8726{
8727 /* formulate and queue a SHUTDOWN to the sender */
8728 struct mbuf *m_shutdown;
8729 struct sctp_shutdown_chunk *shutdown_cp;
8730 struct sctp_tmit_chunk *chk;
8731
8732 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8733 if (m_shutdown == NULL) {
8734 /* no mbuf's */
8735 return;
8736 }
8737 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8738 sctp_alloc_a_chunk(stcb, chk);
8739 if (chk == NULL) {
8740 /* no memory */
8741 sctp_m_freem(m_shutdown);
8742 return;
8743 }
8744 chk->copy_by_ref = 0;
8745 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8746 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8747 chk->rec.chunk_id.can_take_data = 1;
8748 chk->sent = SCTP_DATAGRAM_UNSENT;
8749 chk->snd_count = 0;
8750 chk->flags = 0;
8751 chk->asoc = &stcb->asoc;
8752 chk->data = m_shutdown;
8753 chk->whoTo = net;
8754 atomic_add_int(&net->ref_count, 1);
8755
8756 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8757 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8758 shutdown_cp->ch.chunk_flags = 0;
8759 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8760 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8761 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8762 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8763 chk->asoc->ctrl_queue_cnt++;
8764 return;
8765}
8766
8767void
8768sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8769{
8770 /*
8771 * formulate and queue an ASCONF to the peer. ASCONF parameters
8772 * should be queued on the assoc queue.
8773 */
8774 struct sctp_tmit_chunk *chk;
8775 struct mbuf *m_asconf;
8776 int len;
8777
8778 SCTP_TCB_LOCK_ASSERT(stcb);
8779
8780 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8781 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8782 /* can't send a new one if there is one in flight already */
8783 return;
8784 }
8785 /* compose an ASCONF chunk, maximum length is PMTU */
8786 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8787 if (m_asconf == NULL) {
8788 return;
8789 }
8790 sctp_alloc_a_chunk(stcb, chk);
8791 if (chk == NULL) {
8792 /* no memory */
8793 sctp_m_freem(m_asconf);
8794 return;
8795 }
8796 chk->copy_by_ref = 0;
8797 chk->data = m_asconf;
8798 chk->send_size = len;
8799 chk->rec.chunk_id.id = SCTP_ASCONF;
8800 chk->rec.chunk_id.can_take_data = 0;
8801 chk->sent = SCTP_DATAGRAM_UNSENT;
8802 chk->snd_count = 0;
8803 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8804 chk->asoc = &stcb->asoc;
8805 chk->whoTo = net;
8806 atomic_add_int(&chk->whoTo->ref_count, 1);
8807 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8808 chk->asoc->ctrl_queue_cnt++;
8809 return;
8810}
8811
8812void
8813sctp_send_asconf_ack(struct sctp_tcb *stcb)
8814{
8815 /*
8816 * formulate and queue a asconf-ack back to sender. the asconf-ack
8817 * must be stored in the tcb.
8818 */
8819 struct sctp_tmit_chunk *chk;
8820 struct sctp_asconf_ack *ack, *latest_ack;
8821 struct mbuf *m_ack, *m;
8822 struct sctp_nets *net = NULL;
8823
8824 SCTP_TCB_LOCK_ASSERT(stcb);
8825 /* Get the latest ASCONF-ACK */
8826 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8827 if (latest_ack == NULL) {
8828 return;
8829 }
8830 if (latest_ack->last_sent_to != NULL &&
8831 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8832 /* we're doing a retransmission */
8833 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8834 if (net == NULL) {
8835 /* no alternate */
8836 if (stcb->asoc.last_control_chunk_from == NULL)
8837 net = stcb->asoc.primary_destination;
8838 else
8839 net = stcb->asoc.last_control_chunk_from;
8840 }
8841 } else {
8842 /* normal case */
8843 if (stcb->asoc.last_control_chunk_from == NULL)
8844 net = stcb->asoc.primary_destination;
8845 else
8846 net = stcb->asoc.last_control_chunk_from;
8847 }
8848 latest_ack->last_sent_to = net;
8849
8850 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8851 if (ack->data == NULL) {
8852 continue;
8853 }
8854 /* copy the asconf_ack */
8855 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8856 if (m_ack == NULL) {
8857 /* couldn't copy it */
8858 return;
8859 }
8860#ifdef SCTP_MBUF_LOGGING
8861 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8862 struct mbuf *mat;
8863
8864 mat = m_ack;
8865 while (mat) {
8866 if (SCTP_BUF_IS_EXTENDED(mat)) {
8867 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8868 }
8869 mat = SCTP_BUF_NEXT(mat);
8870 }
8871 }
8872#endif
8873
8874 sctp_alloc_a_chunk(stcb, chk);
8875 if (chk == NULL) {
8876 /* no memory */
8877 if (m_ack)
8878 sctp_m_freem(m_ack);
8879 return;
8880 }
8881 chk->copy_by_ref = 0;
8882
8883 chk->whoTo = net;
8884 chk->data = m_ack;
8885 chk->send_size = 0;
8886 /* Get size */
8887 m = m_ack;
8888 chk->send_size = ack->len;
8889 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8890 chk->rec.chunk_id.can_take_data = 1;
8891 chk->sent = SCTP_DATAGRAM_UNSENT;
8892 chk->snd_count = 0;
8893 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8894 chk->asoc = &stcb->asoc;
8895 atomic_add_int(&chk->whoTo->ref_count, 1);
8896
8897 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8898 chk->asoc->ctrl_queue_cnt++;
8899 }
8900 return;
8901}
8902
8903
8904static int
8905sctp_chunk_retransmission(struct sctp_inpcb *inp,
8906 struct sctp_tcb *stcb,
8907 struct sctp_association *asoc,
8908 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8909#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8910 SCTP_UNUSED
8911#endif
8912)
8913{
8914 /*-
8915 * send out one MTU of retransmission. If fast_retransmit is
8916 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8917 * rwnd. For a Cookie or Asconf in the control chunk queue we
8918 * retransmit them by themselves.
8919 *
8920 * For data chunks we will pick out the lowest TSN's in the sent_queue
8921 * marked for resend and bundle them all together (up to a MTU of
8922 * destination). The address to send to should have been
8923 * selected/changed where the retransmission was marked (i.e. in FR
8924 * or t3-timeout routines).
8925 */
8926 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8927 struct sctp_tmit_chunk *chk, *fwd;
8928 struct mbuf *m, *endofchain;
8929 struct sctp_nets *net = NULL;
8930 uint32_t tsns_sent = 0;
8931 int no_fragmentflg, bundle_at, cnt_thru;
8932 unsigned int mtu;
8933 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8934 struct sctp_auth_chunk *auth = NULL;
8935 uint32_t auth_offset = 0;
8936 uint16_t auth_keyid;
8937 int override_ok = 1;
8938 int data_auth_reqd = 0;
8939 uint32_t dmtu = 0;
8940
8941 SCTP_TCB_LOCK_ASSERT(stcb);
8942 tmr_started = ctl_cnt = bundle_at = error = 0;
8943 no_fragmentflg = 1;
8944 fwd_tsn = 0;
8945 *cnt_out = 0;
8946 fwd = NULL;
8947 endofchain = m = NULL;
8948 auth_keyid = stcb->asoc.authinfo.active_keyid;
8949#ifdef SCTP_AUDITING_ENABLED
8950 sctp_audit_log(0xC3, 1);
8951#endif
8952 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8953 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8954 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8955 asoc->sent_queue_retran_cnt);
8956 asoc->sent_queue_cnt = 0;
8957 asoc->sent_queue_cnt_removeable = 0;
8958 /* send back 0/0 so we enter normal transmission */
8959 *cnt_out = 0;
8960 return (0);
8961 }
8962 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8963 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8964 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8965 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8966 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8967 if (chk != asoc->str_reset) {
8968 /*
8969 * not eligible for retran if its
8970 * not ours
8971 */
8972 continue;
8973 }
8974 }
8975 ctl_cnt++;
8976 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8977 fwd_tsn = 1;
8978 fwd = chk;
8979 }
8980 /*
8981 * Add an AUTH chunk, if chunk requires it save the
8982 * offset into the chain for AUTH
8983 */
8984 if ((auth == NULL) &&
8985 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8986 stcb->asoc.peer_auth_chunks))) {
8987 m = sctp_add_auth_chunk(m, &endofchain,
8988 &auth, &auth_offset,
8989 stcb,
8990 chk->rec.chunk_id.id);
8991 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8992 }
8993 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8994 break;
8995 }
8996 }
8997 one_chunk = 0;
8998 cnt_thru = 0;
8999 /* do we have control chunks to retransmit? */
9000 if (m != NULL) {
9001 /* Start a timer no matter if we suceed or fail */
9002 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9003 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9004 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9005 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9006 chk->snd_count++; /* update our count */
9007 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9008 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9009 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9010 no_fragmentflg, 0, NULL, 0,
9011 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9012 chk->whoTo->port, so_locked, NULL))) {
9013 SCTP_STAT_INCR(sctps_lowlevelerr);
9014 return (error);
9015 }
9016 m = endofchain = NULL;
9017 auth = NULL;
9018 auth_offset = 0;
9019 /*
9020 * We don't want to mark the net->sent time here since this
9021 * we use this for HB and retrans cannot measure RTT
9022 */
9023 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9024 *cnt_out += 1;
9025 chk->sent = SCTP_DATAGRAM_SENT;
9026 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9027 if (fwd_tsn == 0) {
9028 return (0);
9029 } else {
9030 /* Clean up the fwd-tsn list */
9031 sctp_clean_up_ctl(stcb, asoc);
9032 return (0);
9033 }
9034 }
9035 /*
9036 * Ok, it is just data retransmission we need to do or that and a
9037 * fwd-tsn with it all.
9038 */
9039 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9040 return (SCTP_RETRAN_DONE);
9041 }
9042 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9043 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9044 /* not yet open, resend the cookie and that is it */
9045 return (1);
9046 }
9047#ifdef SCTP_AUDITING_ENABLED
9048 sctp_auditing(20, inp, stcb, NULL);
9049#endif
9050 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9051 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9052 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9053 /* No, not sent to this net or not ready for rtx */
9054 continue;
9055 }
9056 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9057 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9058 /* Gak, we have exceeded max unlucky retran, abort! */
9059 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9060 chk->snd_count,
9061 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9062 atomic_add_int(&stcb->asoc.refcnt, 1);
9063 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9064 SCTP_TCB_LOCK(stcb);
9065 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9066 return (SCTP_RETRAN_EXIT);
9067 }
9068 /* pick up the net */
9069 net = chk->whoTo;
9070 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9071 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9072 } else {
9073 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9074 }
9075
9076 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9077 /* No room in peers rwnd */
9078 uint32_t tsn;
9079
9080 tsn = asoc->last_acked_seq + 1;
9081 if (tsn == chk->rec.data.TSN_seq) {
9082 /*
9083 * we make a special exception for this
9084 * case. The peer has no rwnd but is missing
9085 * the lowest chunk.. which is probably what
9086 * is holding up the rwnd.
9087 */
9088 goto one_chunk_around;
9089 }
9090 return (1);
9091 }
9092one_chunk_around:
9093 if (asoc->peers_rwnd < mtu) {
9094 one_chunk = 1;
9095 if ((asoc->peers_rwnd == 0) &&
9096 (asoc->total_flight == 0)) {
9097 chk->window_probe = 1;
9098 chk->whoTo->window_probe = 1;
9099 }
9100 }
9101#ifdef SCTP_AUDITING_ENABLED
9102 sctp_audit_log(0xC3, 2);
9103#endif
9104 bundle_at = 0;
9105 m = NULL;
9106 net->fast_retran_ip = 0;
9107 if (chk->rec.data.doing_fast_retransmit == 0) {
9108 /*
9109 * if no FR in progress skip destination that have
9110 * flight_size > cwnd.
9111 */
9112 if (net->flight_size >= net->cwnd) {
9113 continue;
9114 }
9115 } else {
9116 /*
9117 * Mark the destination net to have FR recovery
9118 * limits put on it.
9119 */
9120 *fr_done = 1;
9121 net->fast_retran_ip = 1;
9122 }
9123
9124 /*
9125 * if no AUTH is yet included and this chunk requires it,
9126 * make sure to account for it. We don't apply the size
9127 * until the AUTH chunk is actually added below in case
9128 * there is no room for this chunk.
9129 */
9130 if (data_auth_reqd && (auth == NULL)) {
9131 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9132 } else
9133 dmtu = 0;
9134
9135 if ((chk->send_size <= (mtu - dmtu)) ||
9136 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9137 /* ok we will add this one */
9138 if (data_auth_reqd) {
9139 if (auth == NULL) {
9140 m = sctp_add_auth_chunk(m,
9141 &endofchain,
9142 &auth,
9143 &auth_offset,
9144 stcb,
9145 SCTP_DATA);
9146 auth_keyid = chk->auth_keyid;
9147 override_ok = 0;
9148 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9149 } else if (override_ok) {
9150 auth_keyid = chk->auth_keyid;
9151 override_ok = 0;
9152 } else if (chk->auth_keyid != auth_keyid) {
9153 /* different keyid, so done bundling */
9154 break;
9155 }
9156 }
9157 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9158 if (m == NULL) {
9159 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9160 return (ENOMEM);
9161 }
9162 /* Do clear IP_DF ? */
9163 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9164 no_fragmentflg = 0;
9165 }
9166 /* upate our MTU size */
9167 if (mtu > (chk->send_size + dmtu))
9168 mtu -= (chk->send_size + dmtu);
9169 else
9170 mtu = 0;
9171 data_list[bundle_at++] = chk;
9172 if (one_chunk && (asoc->total_flight <= 0)) {
9173 SCTP_STAT_INCR(sctps_windowprobed);
9174 }
9175 }
9176 if (one_chunk == 0) {
9177 /*
9178 * now are there anymore forward from chk to pick
9179 * up?
9180 */
9181 fwd = TAILQ_NEXT(chk, sctp_next);
9182 while (fwd) {
9183 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9184 /* Nope, not for retran */
9185 fwd = TAILQ_NEXT(fwd, sctp_next);
9186 continue;
9187 }
9188 if (fwd->whoTo != net) {
9189 /* Nope, not the net in question */
9190 fwd = TAILQ_NEXT(fwd, sctp_next);
9191 continue;
9192 }
9193 if (data_auth_reqd && (auth == NULL)) {
9194 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9195 } else
9196 dmtu = 0;
9197 if (fwd->send_size <= (mtu - dmtu)) {
9198 if (data_auth_reqd) {
9199 if (auth == NULL) {
9200 m = sctp_add_auth_chunk(m,
9201 &endofchain,
9202 &auth,
9203 &auth_offset,
9204 stcb,
9205 SCTP_DATA);
9206 auth_keyid = fwd->auth_keyid;
9207 override_ok = 0;
9208 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9209 } else if (override_ok) {
9210 auth_keyid = fwd->auth_keyid;
9211 override_ok = 0;
9212 } else if (fwd->auth_keyid != auth_keyid) {
9213 /*
9214 * different keyid,
9215 * so done bundling
9216 */
9217 break;
9218 }
9219 }
9220 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9221 if (m == NULL) {
9222 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9223 return (ENOMEM);
9224 }
9225 /* Do clear IP_DF ? */
9226 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9227 no_fragmentflg = 0;
9228 }
9229 /* upate our MTU size */
9230 if (mtu > (fwd->send_size + dmtu))
9231 mtu -= (fwd->send_size + dmtu);
9232 else
9233 mtu = 0;
9234 data_list[bundle_at++] = fwd;
9235 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9236 break;
9237 }
9238 fwd = TAILQ_NEXT(fwd, sctp_next);
9239 } else {
9240 /* can't fit so we are done */
9241 break;
9242 }
9243 }
9244 }
9245 /* Is there something to send for this destination? */
9246 if (m) {
9247 /*
9248 * No matter if we fail/or suceed we should start a
9249 * timer. A failure is like a lost IP packet :-)
9250 */
9251 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9252 /*
9253 * no timer running on this destination
9254 * restart it.
9255 */
9256 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9257 tmr_started = 1;
9258 }
9259 /* Now lets send it, if there is anything to send :> */
9260 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9261 (struct sockaddr *)&net->ro._l_addr, m,
9262 auth_offset, auth, auth_keyid,
9263 no_fragmentflg, 0, NULL, 0,
9264 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9265 net->port, so_locked, NULL))) {
9266 /* error, we could not output */
9267 SCTP_STAT_INCR(sctps_lowlevelerr);
9268 return (error);
9269 }
9270 m = endofchain = NULL;
9271 auth = NULL;
9272 auth_offset = 0;
9273 /* For HB's */
9274 /*
9275 * We don't want to mark the net->sent time here
9276 * since this we use this for HB and retrans cannot
9277 * measure RTT
9278 */
9279 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9280
9281 /* For auto-close */
9282 cnt_thru++;
9283 if (*now_filled == 0) {
9284 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9285 *now = asoc->time_last_sent;
9286 *now_filled = 1;
9287 } else {
9288 asoc->time_last_sent = *now;
9289 }
9290 *cnt_out += bundle_at;
9291#ifdef SCTP_AUDITING_ENABLED
9292 sctp_audit_log(0xC4, bundle_at);
9293#endif
9294 if (bundle_at) {
9295 tsns_sent = data_list[0]->rec.data.TSN_seq;
9296 }
9297 for (i = 0; i < bundle_at; i++) {
9298 SCTP_STAT_INCR(sctps_sendretransdata);
9299 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9300 /*
9301 * When we have a revoked data, and we
9302 * retransmit it, then we clear the revoked
9303 * flag since this flag dictates if we
9304 * subtracted from the fs
9305 */
9306 if (data_list[i]->rec.data.chunk_was_revoked) {
9307 /* Deflate the cwnd */
9308 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9309 data_list[i]->rec.data.chunk_was_revoked = 0;
9310 }
9311 data_list[i]->snd_count++;
9312 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9313 /* record the time */
9314 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9315 if (data_list[i]->book_size_scale) {
9316 /*
9317 * need to double the book size on
9318 * this one
9319 */
9320 data_list[i]->book_size_scale = 0;
9321 /*
9322 * Since we double the booksize, we
9323 * must also double the output queue
9324 * size, since this get shrunk when
9325 * we free by this amount.
9326 */
9327 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9328 data_list[i]->book_size *= 2;
9329
9330
9331 } else {
9332 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9333 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9334 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9335 }
9336 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9337 (uint32_t) (data_list[i]->send_size +
9338 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9339 }
9340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9341 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9342 data_list[i]->whoTo->flight_size,
9343 data_list[i]->book_size,
9344 (uintptr_t) data_list[i]->whoTo,
9345 data_list[i]->rec.data.TSN_seq);
9346 }
9347 sctp_flight_size_increase(data_list[i]);
9348 sctp_total_flight_increase(stcb, data_list[i]);
9349 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9350 /* SWS sender side engages */
9351 asoc->peers_rwnd = 0;
9352 }
9353 if ((i == 0) &&
9354 (data_list[i]->rec.data.doing_fast_retransmit)) {
9355 SCTP_STAT_INCR(sctps_sendfastretrans);
9356 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9357 (tmr_started == 0)) {
9358 /*-
9359 * ok we just fast-retrans'd
9360 * the lowest TSN, i.e the
9361 * first on the list. In
9362 * this case we want to give
9363 * some more time to get a
9364 * SACK back without a
9365 * t3-expiring.
9366 */
9367 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9368 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9369 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9370 }
9371 }
9372 }
9373 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9374 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9375 }
9376#ifdef SCTP_AUDITING_ENABLED
9377 sctp_auditing(21, inp, stcb, NULL);
9378#endif
9379 } else {
9380 /* None will fit */
9381 return (1);
9382 }
9383 if (asoc->sent_queue_retran_cnt <= 0) {
9384 /* all done we have no more to retran */
9385 asoc->sent_queue_retran_cnt = 0;
9386 break;
9387 }
9388 if (one_chunk) {
9389 /* No more room in rwnd */
9390 return (1);
9391 }
9392 /* stop the for loop here. we sent out a packet */
9393 break;
9394 }
9395 return (0);
9396}
9397
9398
9399static int
9400sctp_timer_validation(struct sctp_inpcb *inp,
9401 struct sctp_tcb *stcb,
9402 struct sctp_association *asoc,
9403 int ret)
9404{
9405 struct sctp_nets *net;
9406
9407 /* Validate that a timer is running somewhere */
9408 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9409 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9410 /* Here is a timer */
9411 return (ret);
9412 }
9413 }
9414 SCTP_TCB_LOCK_ASSERT(stcb);
9415 /* Gak, we did not have a timer somewhere */
9416 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9417 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9418 return (ret);
9419}
9420
9421void
9422sctp_chunk_output(struct sctp_inpcb *inp,
9423 struct sctp_tcb *stcb,
9424 int from_where,
9425 int so_locked
9426#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9427 SCTP_UNUSED
9428#endif
9429)
9430{
9431 /*-
9432 * Ok this is the generic chunk service queue. we must do the
9433 * following:
9434 * - See if there are retransmits pending, if so we must
9435 * do these first.
9436 * - Service the stream queue that is next, moving any
9437 * message (note I must get a complete message i.e.
9438 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9439 * TSN's
9440 * - Check to see if the cwnd/rwnd allows any output, if so we
9441 * go ahead and fomulate and send the low level chunks. Making sure
9442 * to combine any control in the control chunk queue also.
9443 */
9444 struct sctp_association *asoc;
9445 struct sctp_nets *net;
9446 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9447 burst_cnt = 0, burst_limit = 0;
9448 struct timeval now;
9449 int now_filled = 0;
9450 int nagle_on = 0;
9451 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9452 int un_sent = 0;
9453 int fr_done, tot_frs = 0;
9454
9455 asoc = &stcb->asoc;
9456 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9457 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9458 nagle_on = 0;
9459 } else {
9460 nagle_on = 1;
9461 }
9462 }
9463 SCTP_TCB_LOCK_ASSERT(stcb);
9464
9465 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9466
9467 if ((un_sent <= 0) &&
9468 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9469 (asoc->sent_queue_retran_cnt == 0)) {
9470 /* Nothing to do unless there is something to be sent left */
9471 return;
9472 }
9473 /*
9474 * Do we have something to send, data or control AND a sack timer
9475 * running, if so piggy-back the sack.
9476 */
9477 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9478 /*
9479 * EY if nr_sacks used then send an nr-sack , a sack
9480 * otherwise
9481 */
9482 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
9483 sctp_send_nr_sack(stcb);
9484 else
9485 sctp_send_sack(stcb);
9486 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9487 }
9488 while (asoc->sent_queue_retran_cnt) {
9489 /*-
9490 * Ok, it is retransmission time only, we send out only ONE
9491 * packet with a single call off to the retran code.
9492 */
9493 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9494 /*-
9495 * Special hook for handling cookiess discarded
9496 * by peer that carried data. Send cookie-ack only
9497 * and then the next call with get the retran's.
9498 */
9499 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9500 from_where,
9501 &now, &now_filled, frag_point, so_locked);
9502 return;
9503 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9504 /* if its not from a HB then do it */
9505 fr_done = 0;
9506 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9507 if (fr_done) {
9508 tot_frs++;
9509 }
9510 } else {
9511 /*
9512 * its from any other place, we don't allow retran
9513 * output (only control)
9514 */
9515 ret = 1;
9516 }
9517 if (ret > 0) {
9518 /* Can't send anymore */
9519 /*-
9520 * now lets push out control by calling med-level
9521 * output once. this assures that we WILL send HB's
9522 * if queued too.
9523 */
9524 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9525 from_where,
9526 &now, &now_filled, frag_point, so_locked);
9527#ifdef SCTP_AUDITING_ENABLED
9528 sctp_auditing(8, inp, stcb, NULL);
9529#endif
9530 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9531 return;
9532 }
9533 if (ret < 0) {
9534 /*-
9535 * The count was off.. retran is not happening so do
9536 * the normal retransmission.
9537 */
9538#ifdef SCTP_AUDITING_ENABLED
9539 sctp_auditing(9, inp, stcb, NULL);
9540#endif
9541 if (ret == SCTP_RETRAN_EXIT) {
9542 return;
9543 }
9544 break;
9545 }
9546 if (from_where == SCTP_OUTPUT_FROM_T3) {
9547 /* Only one transmission allowed out of a timeout */
9548#ifdef SCTP_AUDITING_ENABLED
9549 sctp_auditing(10, inp, stcb, NULL);
9550#endif
9551 /* Push out any control */
9552 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9553 &now, &now_filled, frag_point, so_locked);
9554 return;
9555 }
9556 if (tot_frs > asoc->max_burst) {
9557 /* Hit FR burst limit */
9558 return;
9559 }
9560 if ((num_out == 0) && (ret == 0)) {
9561
9562 /* No more retrans to send */
9563 break;
9564 }
9565 }
9566#ifdef SCTP_AUDITING_ENABLED
9567 sctp_auditing(12, inp, stcb, NULL);
9568#endif
9569 /* Check for bad destinations, if they exist move chunks around. */
9570 burst_limit = asoc->max_burst;
9571 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9572 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9573 SCTP_ADDR_NOT_REACHABLE) {
9574 /*-
9575 * if possible move things off of this address we
9576 * still may send below due to the dormant state but
9577 * we try to find an alternate address to send to
9578 * and if we have one we move all queued data on the
9579 * out wheel to this alternate address.
9580 */
9581 if (net->ref_count > 1)
9582 sctp_move_to_an_alt(stcb, asoc, net);
9583 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9584 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9585 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9586 /*
9587 * JRS 5/14/07 - If CMT PF is on and the current
9588 * destination is in PF state, move all queued data
9589 * to an alternate desination.
9590 */
9591 if (net->ref_count > 1)
9592 sctp_move_to_an_alt(stcb, asoc, net);
9593 } else {
9594 /*-
9595 * if ((asoc->sat_network) || (net->addr_is_local))
9596 * { burst_limit = asoc->max_burst *
9597 * SCTP_SAT_NETWORK_BURST_INCR; }
9598 */
9599 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9600 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9601 /*
9602 * JRS - Use the congestion control
9603 * given in the congestion control
9604 * module
9605 */
9606 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9607 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9608 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9609 }
9610 SCTP_STAT_INCR(sctps_maxburstqueued);
9611 }
9612 net->fast_retran_ip = 0;
9613 } else {
9614 if (net->flight_size == 0) {
9615 /* Should be decaying the cwnd here */
9616 ;
9617 }
9618 }
9619 }
9620
9621 }
9622 burst_cnt = 0;
9623 do {
9624 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9625 &reason_code, 0, from_where,
9626 &now, &now_filled, frag_point, so_locked);
9627 if (error) {
9628 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9629 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9630 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9631 }
9632 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9633 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9634 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9635 }
9636 break;
9637 }
9638 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9639
9640 tot_out += num_out;
9641 burst_cnt++;
9642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9643 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9644 if (num_out == 0) {
9645 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9646 }
9647 }
9648 if (nagle_on) {
9649 /*-
9650 * When nagle is on, we look at how much is un_sent, then
9651 * if its smaller than an MTU and we have data in
9652 * flight we stop.
9653 */
9654 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9655 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9656 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9657 (stcb->asoc.total_flight > 0)) {
9658 break;
9659 }
9660 }
9661 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9662 TAILQ_EMPTY(&asoc->send_queue) &&
9663 TAILQ_EMPTY(&asoc->out_wheel)) {
9664 /* Nothing left to send */
9665 break;
9666 }
9667 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9668 /* Nothing left to send */
9669 break;
9670 }
9671 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9672 (burst_cnt < burst_limit)));
9673
9674 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9675 if (burst_cnt >= burst_limit) {
9676 SCTP_STAT_INCR(sctps_maxburstqueued);
9677 asoc->burst_limit_applied = 1;
9678 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9679 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9680 }
9681 } else {
9682 asoc->burst_limit_applied = 0;
9683 }
9684 }
9685 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9686 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9687 }
9688 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9689 tot_out);
9690
9691 /*-
9692 * Now we need to clean up the control chunk chain if a ECNE is on
9693 * it. It must be marked as UNSENT again so next call will continue
9694 * to send it until such time that we get a CWR, to remove it.
9695 */
9696 if (stcb->asoc.ecn_echo_cnt_onq)
9697 sctp_fix_ecn_echo(asoc);
9698 return;
9699}
9700
9701
9702int
9703sctp_output(inp, m, addr, control, p, flags)
9704 struct sctp_inpcb *inp;
9705 struct mbuf *m;
9706 struct sockaddr *addr;
9707 struct mbuf *control;
9708 struct thread *p;
9709 int flags;
9710{
9711 if (inp == NULL) {
9712 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9713 return (EINVAL);
9714 }
9715 if (inp->sctp_socket == NULL) {
9716 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9717 return (EINVAL);
9718 }
9719 return (sctp_sosend(inp->sctp_socket,
9720 addr,
9721 (struct uio *)NULL,
9722 m,
9723 control,
9724 flags, p
9725 ));
9726}
9727
9728void
9729send_forward_tsn(struct sctp_tcb *stcb,
9730 struct sctp_association *asoc)
9731{
9732 struct sctp_tmit_chunk *chk;
9733 struct sctp_forward_tsn_chunk *fwdtsn;
9734 uint32_t advance_peer_ack_point;
9735
9736 SCTP_TCB_LOCK_ASSERT(stcb);
9737 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9738 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9739 /* mark it to unsent */
9740 chk->sent = SCTP_DATAGRAM_UNSENT;
9741 chk->snd_count = 0;
9742 /* Do we correct its output location? */
9743 if (chk->whoTo != asoc->primary_destination) {
9744 sctp_free_remote_addr(chk->whoTo);
9745 chk->whoTo = asoc->primary_destination;
9746 atomic_add_int(&chk->whoTo->ref_count, 1);
9747 }
9748 goto sctp_fill_in_rest;
9749 }
9750 }
9751 /* Ok if we reach here we must build one */
9752 sctp_alloc_a_chunk(stcb, chk);
9753 if (chk == NULL) {
9754 return;
9755 }
9756 chk->copy_by_ref = 0;
9757 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9758 chk->rec.chunk_id.can_take_data = 0;
9759 chk->asoc = asoc;
9760 chk->whoTo = NULL;
9761
9762 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9763 if (chk->data == NULL) {
9764 sctp_free_a_chunk(stcb, chk);
9765 return;
9766 }
9767 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9768 chk->sent = SCTP_DATAGRAM_UNSENT;
9769 chk->snd_count = 0;
9770 chk->whoTo = asoc->primary_destination;
9771 atomic_add_int(&chk->whoTo->ref_count, 1);
9772 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9773 asoc->ctrl_queue_cnt++;
9774sctp_fill_in_rest:
9775 /*-
9776 * Here we go through and fill out the part that deals with
9777 * stream/seq of the ones we skip.
9778 */
9779 SCTP_BUF_LEN(chk->data) = 0;
9780 {
9781 struct sctp_tmit_chunk *at, *tp1, *last;
9782 struct sctp_strseq *strseq;
9783 unsigned int cnt_of_space, i, ovh;
9784 unsigned int space_needed;
9785 unsigned int cnt_of_skipped = 0;
9786
9787 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9788 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9789 /* no more to look at */
9790 break;
9791 }
9792 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9793 /* We don't report these */
9794 continue;
9795 }
9796 cnt_of_skipped++;
9797 }
9798 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9799 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9800
9801 cnt_of_space = M_TRAILINGSPACE(chk->data);
9802
9803 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9804 ovh = SCTP_MIN_OVERHEAD;
9805 } else {
9806 ovh = SCTP_MIN_V4_OVERHEAD;
9807 }
9808 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9809 /* trim to a mtu size */
9810 cnt_of_space = asoc->smallest_mtu - ovh;
9811 }
9812 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9813 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9814 0xff, 0, cnt_of_skipped,
9815 asoc->advanced_peer_ack_point);
9816
9817 }
9818 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9819 if (cnt_of_space < space_needed) {
9820 /*-
9821 * ok we must trim down the chunk by lowering the
9822 * advance peer ack point.
9823 */
9824 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9825 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9826 0xff, 0xff, cnt_of_space,
9827 space_needed);
9828 }
9829 cnt_of_skipped = (cnt_of_space -
9830 ((sizeof(struct sctp_forward_tsn_chunk)) /
9831 sizeof(struct sctp_strseq)));
9832 /*-
9833 * Go through and find the TSN that will be the one
9834 * we report.
9835 */
9836 at = TAILQ_FIRST(&asoc->sent_queue);
9837 for (i = 0; i < cnt_of_skipped; i++) {
9838 tp1 = TAILQ_NEXT(at, sctp_next);
9839 at = tp1;
9840 }
9841 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9842 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9843 0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9844 asoc->advanced_peer_ack_point);
9845 }
9846 last = at;
9847 /*-
9848 * last now points to last one I can report, update
9849 * peer ack point
9850 */
9851 advance_peer_ack_point = last->rec.data.TSN_seq;
9852 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9853 }
9854 chk->send_size = space_needed;
9855 /* Setup the chunk */
9856 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9857 fwdtsn->ch.chunk_length = htons(chk->send_size);
9858 fwdtsn->ch.chunk_flags = 0;
9859 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9860 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9861 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9862 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9863 SCTP_BUF_LEN(chk->data) = chk->send_size;
9864 fwdtsn++;
9865 /*-
9866 * Move pointer to after the fwdtsn and transfer to the
9867 * strseq pointer.
9868 */
9869 strseq = (struct sctp_strseq *)fwdtsn;
9870 /*-
9871 * Now populate the strseq list. This is done blindly
9872 * without pulling out duplicate stream info. This is
9873 * inefficent but won't harm the process since the peer will
9874 * look at these in sequence and will thus release anything.
9875 * It could mean we exceed the PMTU and chop off some that
9876 * we could have included.. but this is unlikely (aka 1432/4
9877 * would mean 300+ stream seq's would have to be reported in
9878 * one FWD-TSN. With a bit of work we can later FIX this to
9879 * optimize and pull out duplcates.. but it does add more
9880 * overhead. So for now... not!
9881 */
9882 at = TAILQ_FIRST(&asoc->sent_queue);
9883 for (i = 0; i < cnt_of_skipped; i++) {
9884 tp1 = TAILQ_NEXT(at, sctp_next);
9885 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9886 /* We don't report these */
9887 i--;
9888 at = tp1;
9889 continue;
9890 }
9891 if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9892 at->rec.data.fwd_tsn_cnt = 0;
9893 }
9894 strseq->stream = ntohs(at->rec.data.stream_number);
9895 strseq->sequence = ntohs(at->rec.data.stream_seq);
9896 strseq++;
9897 at = tp1;
9898 }
9899 }
9900 return;
9901
9902}
9903
9904void
9905sctp_send_sack(struct sctp_tcb *stcb)
9906{
9907 /*-
9908 * Queue up a SACK in the control queue. We must first check to see
9909 * if a SACK is somehow on the control queue. If so, we will take
9910 * and and remove the old one.
9911 */
9912 struct sctp_association *asoc;
9913 struct sctp_tmit_chunk *chk, *a_chk;
9914 struct sctp_sack_chunk *sack;
9915 struct sctp_gap_ack_block *gap_descriptor;
9916 struct sack_track *selector;
9917 int mergeable = 0;
9918 int offset;
9919 caddr_t limit;
9920 uint32_t *dup;
9921 int limit_reached = 0;
9922 unsigned int i, jstart, siz, j;
9923 unsigned int num_gap_blocks = 0, space;
9924 int num_dups = 0;
9925 int space_req;
9926
9927 a_chk = NULL;
9928 asoc = &stcb->asoc;
9929 SCTP_TCB_LOCK_ASSERT(stcb);
9930 if (asoc->last_data_chunk_from == NULL) {
9931 /* Hmm we never received anything */
9932 return;
9933 }
9934 sctp_set_rwnd(stcb, asoc);
9935 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9936 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9937 /* Hmm, found a sack already on queue, remove it */
9938 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9939 asoc->ctrl_queue_cnt++;
9940 a_chk = chk;
9941 if (a_chk->data) {
9942 sctp_m_freem(a_chk->data);
9943 a_chk->data = NULL;
9944 }
9945 sctp_free_remote_addr(a_chk->whoTo);
9946 a_chk->whoTo = NULL;
9947 break;
9948 }
9949 }
9950 if (a_chk == NULL) {
9951 sctp_alloc_a_chunk(stcb, a_chk);
9952 if (a_chk == NULL) {
9953 /* No memory so we drop the idea, and set a timer */
9954 if (stcb->asoc.delayed_ack) {
9955 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9956 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9957 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9958 stcb->sctp_ep, stcb, NULL);
9959 } else {
9960 stcb->asoc.send_sack = 1;
9961 }
9962 return;
9963 }
9964 a_chk->copy_by_ref = 0;
9965 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9966 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9967 a_chk->rec.chunk_id.can_take_data = 1;
9968 }
9969 /* Clear our pkt counts */
9970 asoc->data_pkts_seen = 0;
9971
9972 a_chk->asoc = asoc;
9973 a_chk->snd_count = 0;
9974 a_chk->send_size = 0; /* fill in later */
9975 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9976 a_chk->whoTo = NULL;
9977
9978 if ((asoc->numduptsns) ||
9979 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9980 ) {
9981 /*-
9982 * Ok, we have some duplicates or the destination for the
9983 * sack is unreachable, lets see if we can select an
9984 * alternate than asoc->last_data_chunk_from
9985 */
9986 if ((!(asoc->last_data_chunk_from->dest_state &
9987 SCTP_ADDR_NOT_REACHABLE)) &&
9988 (asoc->used_alt_onsack > asoc->numnets)) {
9989 /* We used an alt last time, don't this time */
9990 a_chk->whoTo = NULL;
9991 } else {
9992 asoc->used_alt_onsack++;
9993 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9994 }
9995 if (a_chk->whoTo == NULL) {
9996 /* Nope, no alternate */
9997 a_chk->whoTo = asoc->last_data_chunk_from;
9998 asoc->used_alt_onsack = 0;
9999 }
10000 } else {
10001 /*
10002 * No duplicates so we use the last place we received data
10003 * from.
10004 */
10005 asoc->used_alt_onsack = 0;
10006 a_chk->whoTo = asoc->last_data_chunk_from;
10007 }
10008 if (a_chk->whoTo) {
10009 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10010 }
10011 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
10012 /* no gaps */
10013 space_req = sizeof(struct sctp_sack_chunk);
10014 } else {
10015 /* gaps get a cluster */
10016 space_req = MCLBYTES;
10017 }
10018 /* Ok now lets formulate a MBUF with our sack */
10019 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10020 if ((a_chk->data == NULL) ||
10021 (a_chk->whoTo == NULL)) {
10022 /* rats, no mbuf memory */
10023 if (a_chk->data) {
10024 /* was a problem with the destination */
10025 sctp_m_freem(a_chk->data);
10026 a_chk->data = NULL;
10027 }
10028 sctp_free_a_chunk(stcb, a_chk);
10029 /* sa_ignore NO_NULL_CHK */
10030 if (stcb->asoc.delayed_ack) {
10031 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10032 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10033 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10034 stcb->sctp_ep, stcb, NULL);
10035 } else {
10036 stcb->asoc.send_sack = 1;
10037 }
10038 return;
10039 }
10040 /* ok, lets go through and fill it in */
10041 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10042 space = M_TRAILINGSPACE(a_chk->data);
10043 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10044 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10045 }
10046 limit = mtod(a_chk->data, caddr_t);
10047 limit += space;
10048
10049 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10050 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
10051 /* 0x01 is used by nonce for ecn */
10052 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10053 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10054 (asoc->peer_supports_ecn_nonce))
10055 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10056 else
10057 sack->ch.chunk_flags = 0;
10058
10059 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10060 /*-
10061 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10062 * received, then set high bit to 1, else 0. Reset
10063 * pkts_rcvd.
10064 */
10065 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10066 asoc->cmt_dac_pkts_rcvd = 0;
10067 }
10068#ifdef SCTP_ASOCLOG_OF_TSNS
10069 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10070 stcb->asoc.cumack_log_atsnt++;
10071 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10072 stcb->asoc.cumack_log_atsnt = 0;
10073 }
10074#endif
10075 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10076 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10077 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10078
10079 /* reset the readers interpretation */
10080 stcb->freed_by_sorcv_sincelast = 0;
10081
10082 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10083
10084 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10085 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10086 else
10087 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10088
10089 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10090 offset = 1;
10091 /*-
10092 * cum-ack behind the mapping array, so we start and use all
10093 * entries.
10094 */
10095 jstart = 0;
10096 } else {
10097 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10098 /*-
10099 * we skip the first one when the cum-ack is at or above the
10100 * mapping array base. Note this only works if
10101 */
10102 jstart = 1;
10103 }
10104 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10105 /* we have a gap .. maybe */
10106 for (i = 0; i < siz; i++) {
10107 selector = &sack_array[asoc->mapping_array[i]];
10108 if (mergeable && selector->right_edge) {
10109 /*
10110 * Backup, left and right edges were ok to
10111 * merge.
10112 */
10113 num_gap_blocks--;
10114 gap_descriptor--;
10115 }
10116 if (selector->num_entries == 0)
10117 mergeable = 0;
10118 else {
10119 for (j = jstart; j < selector->num_entries; j++) {
10120 if (mergeable && selector->right_edge) {
10121 /*
10122 * do a merge by NOT setting
10123 * the left side
10124 */
10125 mergeable = 0;
10126 } else {
10127 /*
10128 * no merge, set the left
10129 * side
10130 */
10131 mergeable = 0;
10132 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10133 }
10134 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10135 num_gap_blocks++;
10136 gap_descriptor++;
10137 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10138 /* no more room */
10139 limit_reached = 1;
10140 break;
10141 }
10142 }
10143 if (selector->left_edge) {
10144 mergeable = 1;
10145 }
10146 }
10147 if (limit_reached) {
10148 /* Reached the limit stop */
10149 break;
10150 }
10151 jstart = 0;
10152 offset += 8;
10153 }
10154 if (num_gap_blocks == 0) {
10155 /*
10156 * slide not yet happened, and somehow we got called
10157 * to send a sack. Cumack needs to move up.
10158 */
10159 int abort_flag = 0;
10160
10161 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10162 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10163 sctp_sack_check(stcb, 0, 0, &abort_flag);
10164 }
10165 }
10166 /* now we must add any dups we are going to report. */
10167 if ((limit_reached == 0) && (asoc->numduptsns)) {
10168 dup = (uint32_t *) gap_descriptor;
10169 for (i = 0; i < asoc->numduptsns; i++) {
10170 *dup = htonl(asoc->dup_tsns[i]);
10171 dup++;
10172 num_dups++;
10173 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10174 /* no more room */
10175 break;
10176 }
10177 }
10178 asoc->numduptsns = 0;
10179 }
10180 /*
10181 * now that the chunk is prepared queue it to the control chunk
10182 * queue.
10183 */
10184 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10185 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10186 (num_dups * sizeof(int32_t)));
10187 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10188 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10189 sack->sack.num_dup_tsns = htons(num_dups);
10190 sack->ch.chunk_length = htons(a_chk->send_size);
10191 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10192 asoc->ctrl_queue_cnt++;
10193 asoc->send_sack = 0;
10194 SCTP_STAT_INCR(sctps_sendsacks);
10195 return;
10196}
10197
10198/* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
10199void
10200sctp_send_nr_sack(struct sctp_tcb *stcb)
10201{
10202 /*-
10203 * Queue up an NR-SACK in the control queue. We must first check to see
10204 * if an NR-SACK is somehow on the control queue. If so, we will take
10205 * and and remove the old one.
10206 */
10207 struct sctp_association *asoc;
10208 struct sctp_tmit_chunk *chk, *a_chk;
10209
10210 struct sctp_nr_sack_chunk *nr_sack;
10211
10212 struct sctp_gap_ack_block *gap_descriptor;
10213 struct sctp_nr_gap_ack_block *nr_gap_descriptor;
10214
10215 struct sack_track *selector;
10216 struct sack_track *nr_selector;
10217
10218 /* EY do we need nr_mergeable, NO */
10219 int mergeable = 0;
10220 int offset;
10221 caddr_t limit;
10222 uint32_t *dup;
10223 int limit_reached = 0;
10224 unsigned int i, jstart, siz, j;
10225 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10226 int num_dups = 0;
10227 int space_req;
10228 unsigned int reserved = 0;
10229
10230 a_chk = NULL;
10231 asoc = &stcb->asoc;
10232 SCTP_TCB_LOCK_ASSERT(stcb);
10233 if (asoc->last_data_chunk_from == NULL) {
10234 /* Hmm we never received anything */
10235 return;
10236 }
10237 sctp_set_rwnd(stcb, asoc);
10238 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10239 if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
10240 /* Hmm, found a sack already on queue, remove it */
10241 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10242 asoc->ctrl_queue_cnt++;
10243 a_chk = chk;
10244 if (a_chk->data) {
10245 sctp_m_freem(a_chk->data);
10246 a_chk->data = NULL;
10247 }
10248 sctp_free_remote_addr(a_chk->whoTo);
10249 a_chk->whoTo = NULL;
10250 break;
10251 }
10252 }
10253 if (a_chk == NULL) {
10254 sctp_alloc_a_chunk(stcb, a_chk);
10255 if (a_chk == NULL) {
10256 /* No memory so we drop the idea, and set a timer */
10257 if (stcb->asoc.delayed_ack) {
10258 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10259 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10260 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10261 stcb->sctp_ep, stcb, NULL);
10262 } else {
10263 stcb->asoc.send_sack = 1;
10264 }
10265 return;
10266 }
10267 a_chk->copy_by_ref = 0;
10268 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
10269 a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
10270 a_chk->rec.chunk_id.can_take_data = 1;
10271 }
10272 /* Clear our pkt counts */
10273 asoc->data_pkts_seen = 0;
10274
10275 a_chk->asoc = asoc;
10276 a_chk->snd_count = 0;
10277 a_chk->send_size = 0; /* fill in later */
10278 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10279 a_chk->whoTo = NULL;
10280
10281 if ((asoc->numduptsns) ||
10282 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
10283 ) {
10284 /*-
10285 * Ok, we have some duplicates or the destination for the
10286 * sack is unreachable, lets see if we can select an
10287 * alternate than asoc->last_data_chunk_from
10288 */
10289 if ((!(asoc->last_data_chunk_from->dest_state &
10290 SCTP_ADDR_NOT_REACHABLE)) &&
10291 (asoc->used_alt_onsack > asoc->numnets)) {
10292 /* We used an alt last time, don't this time */
10293 a_chk->whoTo = NULL;
10294 } else {
10295 asoc->used_alt_onsack++;
10296 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10297 }
10298 if (a_chk->whoTo == NULL) {
10299 /* Nope, no alternate */
10300 a_chk->whoTo = asoc->last_data_chunk_from;
10301 asoc->used_alt_onsack = 0;
10302 }
10303 } else {
10304 /*
10305 * No duplicates so we use the last place we received data
10306 * from.
10307 */
10308 asoc->used_alt_onsack = 0;
10309 a_chk->whoTo = asoc->last_data_chunk_from;
10310 }
10311 if (a_chk->whoTo) {
10312 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10313 }
10314 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
10315 /* no gaps */
10316 space_req = sizeof(struct sctp_nr_sack_chunk);
10317 } else {
10318 /* EY - what is this about? */
10319 /* gaps get a cluster */
10320 space_req = MCLBYTES;
10321 }
10322 /* Ok now lets formulate a MBUF with our sack */
10323 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10324 if ((a_chk->data == NULL) ||
10325 (a_chk->whoTo == NULL)) {
10326 /* rats, no mbuf memory */
10327 if (a_chk->data) {
10328 /* was a problem with the destination */
10329 sctp_m_freem(a_chk->data);
10330 a_chk->data = NULL;
10331 }
10332 sctp_free_a_chunk(stcb, a_chk);
10333 /* sa_ignore NO_NULL_CHK */
10334 if (stcb->asoc.delayed_ack) {
10335 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10336 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10337 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10338 stcb->sctp_ep, stcb, NULL);
10339 } else {
10340 stcb->asoc.send_sack = 1;
10341 }
10342 return;
10343 }
10344 /* ok, lets go through and fill it in */
10345 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10346 space = M_TRAILINGSPACE(a_chk->data);
10347 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10348 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10349 }
10350 limit = mtod(a_chk->data, caddr_t);
10351 limit += space;
10352
10353 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10354 nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
10355 /* EYJ */
10356 /* 0x01 is used by nonce for ecn */
10357 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10358 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10359 (asoc->peer_supports_ecn_nonce))
10360 nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10361 else
10362 nr_sack->ch.chunk_flags = 0;
10363
10364 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10365 /*-
10366 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10367 * received, then set high bit to 1, else 0. Reset
10368 * pkts_rcvd.
10369 */
10370 /* EY - TODO: which chunk flag is used in here? -The LSB */
10371 nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10372 asoc->cmt_dac_pkts_rcvd = 0;
10373 }
10374#ifdef SCTP_ASOCLOG_OF_TSNS
10375 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10376 stcb->asoc.cumack_log_atsnt++;
10377 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10378 stcb->asoc.cumack_log_atsnt = 0;
10379 }
10380#endif
10381 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10382 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10383 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10384
10385 /* reset the readers interpretation */
10386 stcb->freed_by_sorcv_sincelast = 0;
10387
10388 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10389
10390 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10391 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10392 else
10393 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10394
10395 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10396 offset = 1;
10397 /*-
10398 * cum-ack behind the mapping array, so we start and use all
10399 * entries.
10400 */
10401 jstart = 0;
10402 } else {
10403 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10404 /*-
10405 * we skip the first one when the cum-ack is at or above the
10406 * mapping array base. Note this only works if
10407 */
10408 jstart = 1;
10409 }
10410 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10411 /* we have a gap .. maybe */
10412 for (i = 0; i < siz; i++) {
10413 selector = &sack_array[asoc->mapping_array[i]];
10414 if (mergeable && selector->right_edge) {
10415 /*
10416 * Backup, left and right edges were ok to
10417 * merge.
10418 */
10419 num_gap_blocks--;
10420 gap_descriptor--;
10421 }
10422 if (selector->num_entries == 0)
10423 mergeable = 0;
10424 else {
10425 for (j = jstart; j < selector->num_entries; j++) {
10426 if (mergeable && selector->right_edge) {
10427 /*
10428 * do a merge by NOT setting
10429 * the left side
10430 */
10431 mergeable = 0;
10432 } else {
10433 /*
10434 * no merge, set the left
10435 * side
10436 */
10437 mergeable = 0;
10438 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10439 }
10440 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10441 num_gap_blocks++;
10442 gap_descriptor++;
10443 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10444 /* no more room */
10445 limit_reached = 1;
10446 break;
10447 }
10448 }
10449 if (selector->left_edge) {
10450 mergeable = 1;
10451 }
10452 }
10453 if (limit_reached) {
10454 /* Reached the limit stop */
10455 break;
10456 }
10457 jstart = 0;
10458 offset += 8;
10459 }
10460 if (num_gap_blocks == 0) {
10461 /*
10462 * slide not yet happened, and somehow we got called
10463 * to send a sack. Cumack needs to move up.
10464 */
10465 int abort_flag = 0;
10466
10467 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10468 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10469 sctp_sack_check(stcb, 0, 0, &abort_flag);
10470 }
10471 }
10472 /*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
10473
10474 nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
10475
10476 /* EY - there will be gaps + nr_gaps if draining is possible */
10477 if ((SCTP_BASE_SYSCTL(sctp_do_drain)) && (limit_reached == 0)) {
10478
10479 mergeable = 0;
10480
10481 if (asoc->highest_tsn_inside_nr_map > asoc->nr_mapping_array_base_tsn)
10482 siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
10483 else
10484 siz = (((MAX_TSN - asoc->nr_mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10485
10486 if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10487 offset = 1;
10488 /*-
10489 * cum-ack behind the mapping array, so we start and use all
10490 * entries.
10491 */
10492 jstart = 0;
10493 } else {
10494 offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
10495 /*-
10496 * we skip the first one when the cum-ack is at or above the
10497 * mapping array base. Note this only works if
10498 */
10499 jstart = 1;
10500 }
10501 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10502 /* we have a gap .. maybe */
10503 for (i = 0; i < siz; i++) {
10504 nr_selector = &sack_array[asoc->nr_mapping_array[i]];
10505 if (mergeable && nr_selector->right_edge) {
10506 /*
10507 * Backup, left and right edges were
10508 * ok to merge.
10509 */
10510 num_nr_gap_blocks--;
10511 nr_gap_descriptor--;
10512 }
10513 if (nr_selector->num_entries == 0)
10514 mergeable = 0;
10515 else {
10516 for (j = jstart; j < nr_selector->num_entries; j++) {
10517 if (mergeable && nr_selector->right_edge) {
10518 /*
10519 * do a merge by NOT
10520 * setting the left
10521 * side
10522 */
10523 mergeable = 0;
10524 } else {
10525 /*
10526 * no merge, set the
10527 * left side
10528 */
10529 mergeable = 0;
10530 nr_gap_descriptor->start = htons((nr_selector->gaps[j].start + offset));
10531 }
10532 nr_gap_descriptor->end = htons((nr_selector->gaps[j].end + offset));
10533 num_nr_gap_blocks++;
10534 nr_gap_descriptor++;
10535 if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
10536 /* no more room */
10537 limit_reached = 1;
10538 break;
10539 }
10540 }
10541 if (nr_selector->left_edge) {
10542 mergeable = 1;
10543 }
10544 }
10545 if (limit_reached) {
10546 /* Reached the limit stop */
10547 break;
10548 }
10549 jstart = 0;
10550 offset += 8;
10551 }
10552 }
10553 }
10554 /*---------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
10555
10556 /* now we must add any dups we are going to report. */
10557 if ((limit_reached == 0) && (asoc->numduptsns)) {
10558 dup = (uint32_t *) nr_gap_descriptor;
10559 for (i = 0; i < asoc->numduptsns; i++) {
10560 *dup = htonl(asoc->dup_tsns[i]);
10561 dup++;
10562 num_dups++;
10563 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10564 /* no more room */
10565 break;
10566 }
10567 }
10568 asoc->numduptsns = 0;
10569 }
10570 /*
10571 * now that the chunk is prepared queue it to the control chunk
10572 * queue.
10573 */
10574 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
10575 num_nr_gap_blocks = num_gap_blocks;
10576 num_gap_blocks = 0;
10577 }
10578 a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
10579 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10580 (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
10581 (num_dups * sizeof(int32_t)));
10582
10583 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10584 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10585 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10586 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10587 nr_sack->nr_sack.reserved = htons(reserved);
10588 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10589 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10590 asoc->ctrl_queue_cnt++;
10591 asoc->send_sack = 0;
10592 SCTP_STAT_INCR(sctps_sendsacks);
10593 return;
10594}
10595
10596void
10597sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10598#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10599 SCTP_UNUSED
10600#endif
10601)
10602{
10603 struct mbuf *m_abort;
10604 struct mbuf *m_out = NULL, *m_end = NULL;
10605 struct sctp_abort_chunk *abort = NULL;
10606 int sz;
10607 uint32_t auth_offset = 0;
10608 struct sctp_auth_chunk *auth = NULL;
10609
10610 /*-
10611 * Add an AUTH chunk, if chunk requires it and save the offset into
10612 * the chain for AUTH
10613 */
10614 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10615 stcb->asoc.peer_auth_chunks)) {
10616 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10617 stcb, SCTP_ABORT_ASSOCIATION);
10618 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10619 }
10620 SCTP_TCB_LOCK_ASSERT(stcb);
10621 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10622 if (m_abort == NULL) {
10623 /* no mbuf's */
10624 if (m_out)
10625 sctp_m_freem(m_out);
10626 return;
10627 }
10628 /* link in any error */
10629 SCTP_BUF_NEXT(m_abort) = operr;
10630 sz = 0;
10631 if (operr) {
10632 struct mbuf *n;
10633
10634 n = operr;
10635 while (n) {
10636 sz += SCTP_BUF_LEN(n);
10637 n = SCTP_BUF_NEXT(n);
10638 }
10639 }
10640 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10641 if (m_out == NULL) {
10642 /* NO Auth chunk prepended, so reserve space in front */
10643 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10644 m_out = m_abort;
10645 } else {
10646 /* Put AUTH chunk at the front of the chain */
10647 SCTP_BUF_NEXT(m_end) = m_abort;
10648 }
10649
10650 /* fill in the ABORT chunk */
10651 abort = mtod(m_abort, struct sctp_abort_chunk *);
10652 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10653 abort->ch.chunk_flags = 0;
10654 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10655
10656 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10657 stcb->asoc.primary_destination,
10658 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10659 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10660 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10661 stcb->asoc.primary_destination->port, so_locked, NULL);
10662 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10663}
10664
10665void
10666sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10667 struct sctp_nets *net)
10668{
10669 /* formulate and SEND a SHUTDOWN-COMPLETE */
10670 struct mbuf *m_shutdown_comp;
10671 struct sctp_shutdown_complete_chunk *shutdown_complete;
10672
10673 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10674 if (m_shutdown_comp == NULL) {
10675 /* no mbuf's */
10676 return;
10677 }
10678 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10679 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10680 shutdown_complete->ch.chunk_flags = 0;
10681 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10682 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10683 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10684 (struct sockaddr *)&net->ro._l_addr,
10685 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10686 stcb->sctp_ep->sctp_lport, stcb->rport,
10687 htonl(stcb->asoc.peer_vtag),
10688 net->port, SCTP_SO_NOT_LOCKED, NULL);
10689 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10690 return;
10691}
10692
10693void
10694sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10695 uint32_t vrf_id, uint16_t port)
10696{
10697 /* formulate and SEND a SHUTDOWN-COMPLETE */
10698 struct mbuf *o_pak;
10699 struct mbuf *mout;
10700 struct ip *iph, *iph_out;
10701 struct udphdr *udp = NULL;
10702
10703#ifdef INET6
10704 struct ip6_hdr *ip6, *ip6_out;
10705
10706#endif
10707 int offset_out, len, mlen;
10708 struct sctp_shutdown_complete_msg *comp_cp;
10709
10710 iph = mtod(m, struct ip *);
10711 switch (iph->ip_v) {
10712 case IPVERSION:
10713 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10714 break;
10715#ifdef INET6
10716 case IPV6_VERSION >> 4:
10717 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10718 break;
10719#endif
10720 default:
10721 return;
10722 }
10723 if (port) {
10724 len += sizeof(struct udphdr);
10725 }
10726 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10727 if (mout == NULL) {
10728 return;
10729 }
10730 SCTP_BUF_RESV_UF(mout, max_linkhdr);
10731 SCTP_BUF_LEN(mout) = len;
10732 SCTP_BUF_NEXT(mout) = NULL;
10733 iph_out = NULL;
10734#ifdef INET6
10735 ip6_out = NULL;
10736#endif
10737 offset_out = 0;
10738
10739 switch (iph->ip_v) {
10740 case IPVERSION:
10741 iph_out = mtod(mout, struct ip *);
10742
10743 /* Fill in the IP header for the ABORT */
10744 iph_out->ip_v = IPVERSION;
10745 iph_out->ip_hl = (sizeof(struct ip) / 4);
10746 iph_out->ip_tos = (u_char)0;
10747 iph_out->ip_id = 0;
10748 iph_out->ip_off = 0;
10749 iph_out->ip_ttl = MAXTTL;
10750 if (port) {
10751 iph_out->ip_p = IPPROTO_UDP;
10752 } else {
10753 iph_out->ip_p = IPPROTO_SCTP;
10754 }
10755 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10756 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10757
10758 /* let IP layer calculate this */
10759 iph_out->ip_sum = 0;
10760 offset_out += sizeof(*iph_out);
10761 comp_cp = (struct sctp_shutdown_complete_msg *)(
10762 (caddr_t)iph_out + offset_out);
10763 break;
10764#ifdef INET6
10765 case IPV6_VERSION >> 4:
10766 ip6 = (struct ip6_hdr *)iph;
10767 ip6_out = mtod(mout, struct ip6_hdr *);
10768
10769 /* Fill in the IPv6 header for the ABORT */
10770 ip6_out->ip6_flow = ip6->ip6_flow;
5183 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5184 /*
5185 * If the new destination is
5186 * SITE_LOCAL then we must have site
5187 * scope in common.
5188 */
5189 stc.site_scope = 1;
5190 }
5191 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5192 stc.laddr_type = SCTP_IPV6_ADDRESS;
5193 break;
5194 }
5195#endif
5196 default:
5197 /* TSNH */
5198 goto do_a_abort;
5199 break;
5200 }
5201 } else {
5202 /* set the scope per the existing tcb */
5203
5204#ifdef INET6
5205 struct sctp_nets *lnet;
5206
5207#endif
5208
5209 stc.loopback_scope = asoc->loopback_scope;
5210 stc.ipv4_scope = asoc->ipv4_local_scope;
5211 stc.site_scope = asoc->site_scope;
5212 stc.local_scope = asoc->local_scope;
5213#ifdef INET6
5214 /* Why do we not consider IPv4 LL addresses? */
5215 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5216 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5217 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5218 /*
5219 * if we have a LL address, start
5220 * counting at 1.
5221 */
5222 cnt_inits_to = 1;
5223 }
5224 }
5225 }
5226#endif
5227 /* use the net pointer */
5228 to = (struct sockaddr *)&net->ro._l_addr;
5229 switch (to->sa_family) {
5230 case AF_INET:
5231 sin = (struct sockaddr_in *)to;
5232 stc.address[0] = sin->sin_addr.s_addr;
5233 stc.address[1] = 0;
5234 stc.address[2] = 0;
5235 stc.address[3] = 0;
5236 stc.addr_type = SCTP_IPV4_ADDRESS;
5237 if (net->src_addr_selected == 0) {
5238 /*
5239 * strange case here, the INIT should have
5240 * did the selection.
5241 */
5242 net->ro._s_addr = sctp_source_address_selection(inp,
5243 stcb, (sctp_route_t *) & net->ro,
5244 net, 0, vrf_id);
5245 if (net->ro._s_addr == NULL)
5246 return;
5247
5248 net->src_addr_selected = 1;
5249
5250 }
5251 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5252 stc.laddress[1] = 0;
5253 stc.laddress[2] = 0;
5254 stc.laddress[3] = 0;
5255 stc.laddr_type = SCTP_IPV4_ADDRESS;
5256 break;
5257#ifdef INET6
5258 case AF_INET6:
5259 sin6 = (struct sockaddr_in6 *)to;
5260 memcpy(&stc.address, &sin6->sin6_addr,
5261 sizeof(struct in6_addr));
5262 stc.addr_type = SCTP_IPV6_ADDRESS;
5263 if (net->src_addr_selected == 0) {
5264 /*
5265 * strange case here, the INIT should have
5266 * did the selection.
5267 */
5268 net->ro._s_addr = sctp_source_address_selection(inp,
5269 stcb, (sctp_route_t *) & net->ro,
5270 net, 0, vrf_id);
5271 if (net->ro._s_addr == NULL)
5272 return;
5273
5274 net->src_addr_selected = 1;
5275 }
5276 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5277 sizeof(struct in6_addr));
5278 stc.laddr_type = SCTP_IPV6_ADDRESS;
5279 break;
5280#endif
5281 }
5282 }
5283 /* Now lets put the SCTP header in place */
5284 initack = mtod(m, struct sctp_init_ack_chunk *);
5285 /* Save it off for quick ref */
5286 stc.peers_vtag = init_chk->init.initiate_tag;
5287 /* who are we */
5288 memcpy(stc.identification, SCTP_VERSION_STRING,
5289 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5290 /* now the chunk header */
5291 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5292 initack->ch.chunk_flags = 0;
5293 /* fill in later from mbuf we build */
5294 initack->ch.chunk_length = 0;
5295 /* place in my tag */
5296 if ((asoc != NULL) &&
5297 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5298 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5299 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5300 /* re-use the v-tags and init-seq here */
5301 initack->init.initiate_tag = htonl(asoc->my_vtag);
5302 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5303 } else {
5304 uint32_t vtag, itsn;
5305
5306 if (hold_inp_lock) {
5307 SCTP_INP_INCR_REF(inp);
5308 SCTP_INP_RUNLOCK(inp);
5309 }
5310 if (asoc) {
5311 atomic_add_int(&asoc->refcnt, 1);
5312 SCTP_TCB_UNLOCK(stcb);
5313 new_tag:
5314 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5315 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5316 /*
5317 * Got a duplicate vtag on some guy behind a
5318 * nat make sure we don't use it.
5319 */
5320 goto new_tag;
5321 }
5322 initack->init.initiate_tag = htonl(vtag);
5323 /* get a TSN to use too */
5324 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5325 initack->init.initial_tsn = htonl(itsn);
5326 SCTP_TCB_LOCK(stcb);
5327 atomic_add_int(&asoc->refcnt, -1);
5328 } else {
5329 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5330 initack->init.initiate_tag = htonl(vtag);
5331 /* get a TSN to use too */
5332 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5333 }
5334 if (hold_inp_lock) {
5335 SCTP_INP_RLOCK(inp);
5336 SCTP_INP_DECR_REF(inp);
5337 }
5338 }
5339 /* save away my tag to */
5340 stc.my_vtag = initack->init.initiate_tag;
5341
5342 /* set up some of the credits. */
5343 so = inp->sctp_socket;
5344 if (so == NULL) {
5345 /* memory problem */
5346 sctp_m_freem(m);
5347 return;
5348 } else {
5349 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5350 }
5351 /* set what I want */
5352 his_limit = ntohs(init_chk->init.num_inbound_streams);
5353 /* choose what I want */
5354 if (asoc != NULL) {
5355 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5356 i_want = asoc->streamoutcnt;
5357 } else {
5358 i_want = inp->sctp_ep.pre_open_stream_count;
5359 }
5360 } else {
5361 i_want = inp->sctp_ep.pre_open_stream_count;
5362 }
5363 if (his_limit < i_want) {
5364 /* I Want more :< */
5365 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5366 } else {
5367 /* I can have what I want :> */
5368 initack->init.num_outbound_streams = htons(i_want);
5369 }
5370 /* tell him his limt. */
5371 initack->init.num_inbound_streams =
5372 htons(inp->sctp_ep.max_open_streams_intome);
5373
5374 /* adaptation layer indication parameter */
5375 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5376 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5377 ali->ph.param_length = htons(sizeof(*ali));
5378 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5379 SCTP_BUF_LEN(m) += sizeof(*ali);
5380 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5381
5382 /* ECN parameter */
5383 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5384 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5385 ecn->ph.param_length = htons(sizeof(*ecn));
5386 SCTP_BUF_LEN(m) += sizeof(*ecn);
5387
5388 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5389 sizeof(*ecn));
5390 } else {
5391 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5392 }
5393 /* And now tell the peer we do pr-sctp */
5394 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5395 prsctp->ph.param_length = htons(sizeof(*prsctp));
5396 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5397 if (nat_friendly) {
5398 /* Add NAT friendly parameter */
5399 struct sctp_paramhdr *ph;
5400
5401 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5402 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5403 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5404 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5405 }
5406 /* And now tell the peer we do all the extensions */
5407 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5408 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5409 num_ext = 0;
5410 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5411 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5412 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5413 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5414 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5415 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5416 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5417 /*
5418 * EY if the sysctl variable is set, tell the assoc. initiator that
5419 * we do nr_sack
5420 */
5421 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5422 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5423 p_len = sizeof(*pr_supported) + num_ext;
5424 pr_supported->ph.param_length = htons(p_len);
5425 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5426 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5427
5428 /* ECN nonce: And now tell the peer we support ECN nonce */
5429 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5430 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5431 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5432 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5433 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5434 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5435 }
5436 /* add authentication parameters */
5437 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5438 struct sctp_auth_random *randp;
5439 struct sctp_auth_hmac_algo *hmacs;
5440 struct sctp_auth_chunk_list *chunks;
5441 uint16_t random_len;
5442
5443 /* generate and add RANDOM parameter */
5444 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5445 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5446 randp->ph.param_type = htons(SCTP_RANDOM);
5447 p_len = sizeof(*randp) + random_len;
5448 randp->ph.param_length = htons(p_len);
5449 SCTP_READ_RANDOM(randp->random_data, random_len);
5450 /* zero out any padding required */
5451 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5452 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5453
5454 /* add HMAC_ALGO parameter */
5455 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5456 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5457 (uint8_t *) hmacs->hmac_ids);
5458 if (p_len > 0) {
5459 p_len += sizeof(*hmacs);
5460 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5461 hmacs->ph.param_length = htons(p_len);
5462 /* zero out any padding required */
5463 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5464 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5465 }
5466 /* add CHUNKS parameter */
5467 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5468 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5469 chunks->chunk_types);
5470 if (p_len > 0) {
5471 p_len += sizeof(*chunks);
5472 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5473 chunks->ph.param_length = htons(p_len);
5474 /* zero out any padding required */
5475 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5476 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5477 }
5478 }
5479 m_at = m;
5480 /* now the addresses */
5481 {
5482 struct sctp_scoping scp;
5483
5484 /*
5485 * To optimize this we could put the scoping stuff into a
5486 * structure and remove the individual uint8's from the stc
5487 * structure. Then we could just sifa in the address within
5488 * the stc.. but for now this is a quick hack to get the
5489 * address stuff teased apart.
5490 */
5491 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5492 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5493 scp.loopback_scope = stc.loopback_scope;
5494 scp.ipv4_local_scope = stc.ipv4_scope;
5495 scp.local_scope = stc.local_scope;
5496 scp.site_scope = stc.site_scope;
5497 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5498 }
5499
5500 /* tack on the operational error if present */
5501 if (op_err) {
5502 struct mbuf *ol;
5503 int llen;
5504
5505 llen = 0;
5506 ol = op_err;
5507 while (ol) {
5508 llen += SCTP_BUF_LEN(ol);
5509 ol = SCTP_BUF_NEXT(ol);
5510 }
5511 if (llen % 4) {
5512 /* must add a pad to the param */
5513 uint32_t cpthis = 0;
5514 int padlen;
5515
5516 padlen = 4 - (llen % 4);
5517 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5518 }
5519 while (SCTP_BUF_NEXT(m_at) != NULL) {
5520 m_at = SCTP_BUF_NEXT(m_at);
5521 }
5522 SCTP_BUF_NEXT(m_at) = op_err;
5523 while (SCTP_BUF_NEXT(m_at) != NULL) {
5524 m_at = SCTP_BUF_NEXT(m_at);
5525 }
5526 }
5527 /* pre-calulate the size and update pkt header and chunk header */
5528 p_len = 0;
5529 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5530 p_len += SCTP_BUF_LEN(m_tmp);
5531 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5532 /* m_tmp should now point to last one */
5533 break;
5534 }
5535 }
5536
5537 /* Now we must build a cookie */
5538 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5539 if (m_cookie == NULL) {
5540 /* memory problem */
5541 sctp_m_freem(m);
5542 return;
5543 }
5544 /* Now append the cookie to the end and update the space/size */
5545 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5546
5547 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5548 p_len += SCTP_BUF_LEN(m_tmp);
5549 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5550 /* m_tmp should now point to last one */
5551 mp_last = m_tmp;
5552 break;
5553 }
5554 }
5555 /*
5556 * Place in the size, but we don't include the last pad (if any) in
5557 * the INIT-ACK.
5558 */
5559 initack->ch.chunk_length = htons(p_len);
5560
5561 /*
5562 * Time to sign the cookie, we don't sign over the cookie signature
5563 * though thus we set trailer.
5564 */
5565 (void)sctp_hmac_m(SCTP_HMAC,
5566 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5567 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5568 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5569 /*
5570 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5571 * here since the timer will drive a retranmission.
5572 */
5573 padval = p_len % 4;
5574 if ((padval) && (mp_last)) {
5575 /* see my previous comments on mp_last */
5576 int ret;
5577
5578 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5579 if (ret) {
5580 /* Houston we have a problem, no space */
5581 sctp_m_freem(m);
5582 return;
5583 }
5584 p_len += padval;
5585 }
5586 if (stc.loopback_scope) {
5587 over_addr = &store1;
5588 } else {
5589 over_addr = NULL;
5590 }
5591
5592 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5593 0, NULL, 0,
5594 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5595 port, SCTP_SO_NOT_LOCKED, over_addr);
5596 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5597}
5598
5599
5600void
5601sctp_insert_on_wheel(struct sctp_tcb *stcb,
5602 struct sctp_association *asoc,
5603 struct sctp_stream_out *strq, int holds_lock)
5604{
5605 struct sctp_stream_out *stre, *strn;
5606
5607 if (holds_lock == 0) {
5608 SCTP_TCB_SEND_LOCK(stcb);
5609 }
5610 if ((strq->next_spoke.tqe_next) ||
5611 (strq->next_spoke.tqe_prev)) {
5612 /* already on wheel */
5613 goto outof_here;
5614 }
5615 stre = TAILQ_FIRST(&asoc->out_wheel);
5616 if (stre == NULL) {
5617 /* only one on wheel */
5618 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5619 goto outof_here;
5620 }
5621 for (; stre; stre = strn) {
5622 strn = TAILQ_NEXT(stre, next_spoke);
5623 if (stre->stream_no > strq->stream_no) {
5624 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5625 goto outof_here;
5626 } else if (stre->stream_no == strq->stream_no) {
5627 /* huh, should not happen */
5628 goto outof_here;
5629 } else if (strn == NULL) {
5630 /* next one is null */
5631 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5632 next_spoke);
5633 }
5634 }
5635outof_here:
5636 if (holds_lock == 0) {
5637 SCTP_TCB_SEND_UNLOCK(stcb);
5638 }
5639}
5640
5641void
5642sctp_remove_from_wheel(struct sctp_tcb *stcb,
5643 struct sctp_association *asoc,
5644 struct sctp_stream_out *strq,
5645 int holds_lock)
5646{
5647 /* take off and then setup so we know it is not on the wheel */
5648 if (holds_lock == 0)
5649 SCTP_TCB_SEND_LOCK(stcb);
5650 if (TAILQ_FIRST(&strq->outqueue)) {
5651 /* more was added */
5652 if (holds_lock == 0)
5653 SCTP_TCB_SEND_UNLOCK(stcb);
5654 return;
5655 }
5656 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5657 strq->next_spoke.tqe_next = NULL;
5658 strq->next_spoke.tqe_prev = NULL;
5659 if (holds_lock == 0)
5660 SCTP_TCB_SEND_UNLOCK(stcb);
5661}
5662
5663static void
5664sctp_prune_prsctp(struct sctp_tcb *stcb,
5665 struct sctp_association *asoc,
5666 struct sctp_sndrcvinfo *srcv,
5667 int dataout)
5668{
5669 int freed_spc = 0;
5670 struct sctp_tmit_chunk *chk, *nchk;
5671
5672 SCTP_TCB_LOCK_ASSERT(stcb);
5673 if ((asoc->peer_supports_prsctp) &&
5674 (asoc->sent_queue_cnt_removeable > 0)) {
5675 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5676 /*
5677 * Look for chunks marked with the PR_SCTP flag AND
5678 * the buffer space flag. If the one being sent is
5679 * equal or greater priority then purge the old one
5680 * and free some space.
5681 */
5682 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5683 /*
5684 * This one is PR-SCTP AND buffer space
5685 * limited type
5686 */
5687 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5688 /*
5689 * Lower numbers equates to higher
5690 * priority so if the one we are
5691 * looking at has a larger or equal
5692 * priority we want to drop the data
5693 * and NOT retransmit it.
5694 */
5695 if (chk->data) {
5696 /*
5697 * We release the book_size
5698 * if the mbuf is here
5699 */
5700 int ret_spc;
5701 int cause;
5702
5703 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5704 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5705 else
5706 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5707 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5708 cause,
5709 SCTP_SO_LOCKED);
5710 freed_spc += ret_spc;
5711 if (freed_spc >= dataout) {
5712 return;
5713 }
5714 } /* if chunk was present */
5715 } /* if of sufficent priority */
5716 } /* if chunk has enabled */
5717 } /* tailqforeach */
5718
5719 chk = TAILQ_FIRST(&asoc->send_queue);
5720 while (chk) {
5721 nchk = TAILQ_NEXT(chk, sctp_next);
5722 /* Here we must move to the sent queue and mark */
5723 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5724 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5725 if (chk->data) {
5726 /*
5727 * We release the book_size
5728 * if the mbuf is here
5729 */
5730 int ret_spc;
5731
5732 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5733 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5734 SCTP_SO_LOCKED);
5735
5736 freed_spc += ret_spc;
5737 if (freed_spc >= dataout) {
5738 return;
5739 }
5740 } /* end if chk->data */
5741 } /* end if right class */
5742 } /* end if chk pr-sctp */
5743 chk = nchk;
5744 } /* end while (chk) */
5745 } /* if enabled in asoc */
5746}
5747
5748int
5749sctp_get_frag_point(struct sctp_tcb *stcb,
5750 struct sctp_association *asoc)
5751{
5752 int siz, ovh;
5753
5754 /*
5755 * For endpoints that have both v6 and v4 addresses we must reserve
5756 * room for the ipv6 header, for those that are only dealing with V4
5757 * we use a larger frag point.
5758 */
5759 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5760 ovh = SCTP_MED_OVERHEAD;
5761 } else {
5762 ovh = SCTP_MED_V4_OVERHEAD;
5763 }
5764
5765 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5766 siz = asoc->smallest_mtu - ovh;
5767 else
5768 siz = (stcb->asoc.sctp_frag_point - ovh);
5769 /*
5770 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5771 */
5772 /* A data chunk MUST fit in a cluster */
5773 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5774 /* } */
5775
5776 /* adjust for an AUTH chunk if DATA requires auth */
5777 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5778 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5779
5780 if (siz % 4) {
5781 /* make it an even word boundary please */
5782 siz -= (siz % 4);
5783 }
5784 return (siz);
5785}
5786
5787static void
5788sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5789{
5790 sp->pr_sctp_on = 0;
5791 /*
5792 * We assume that the user wants PR_SCTP_TTL if the user provides a
5793 * positive lifetime but does not specify any PR_SCTP policy. This
5794 * is a BAD assumption and causes problems at least with the
5795 * U-Vancovers MPI folks. I will change this to be no policy means
5796 * NO PR-SCTP.
5797 */
5798 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5799 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5800 sp->pr_sctp_on = 1;
5801 } else {
5802 return;
5803 }
5804 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5805 case CHUNK_FLAGS_PR_SCTP_BUF:
5806 /*
5807 * Time to live is a priority stored in tv_sec when doing
5808 * the buffer drop thing.
5809 */
5810 sp->ts.tv_sec = sp->timetolive;
5811 sp->ts.tv_usec = 0;
5812 break;
5813 case CHUNK_FLAGS_PR_SCTP_TTL:
5814 {
5815 struct timeval tv;
5816
5817 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5818 tv.tv_sec = sp->timetolive / 1000;
5819 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5820 /*
5821 * TODO sctp_constants.h needs alternative time
5822 * macros when _KERNEL is undefined.
5823 */
5824 timevaladd(&sp->ts, &tv);
5825 }
5826 break;
5827 case CHUNK_FLAGS_PR_SCTP_RTX:
5828 /*
5829 * Time to live is a the number or retransmissions stored in
5830 * tv_sec.
5831 */
5832 sp->ts.tv_sec = sp->timetolive;
5833 sp->ts.tv_usec = 0;
5834 break;
5835 default:
5836 SCTPDBG(SCTP_DEBUG_USRREQ1,
5837 "Unknown PR_SCTP policy %u.\n",
5838 PR_SCTP_POLICY(sp->sinfo_flags));
5839 break;
5840 }
5841}
5842
5843static int
5844sctp_msg_append(struct sctp_tcb *stcb,
5845 struct sctp_nets *net,
5846 struct mbuf *m,
5847 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5848{
5849 int error = 0, holds_lock;
5850 struct mbuf *at;
5851 struct sctp_stream_queue_pending *sp = NULL;
5852 struct sctp_stream_out *strm;
5853
5854 /*
5855 * Given an mbuf chain, put it into the association send queue and
5856 * place it on the wheel
5857 */
5858 holds_lock = hold_stcb_lock;
5859 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5860 /* Invalid stream number */
5861 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5862 error = EINVAL;
5863 goto out_now;
5864 }
5865 if ((stcb->asoc.stream_locked) &&
5866 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5867 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5868 error = EINVAL;
5869 goto out_now;
5870 }
5871 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5872 /* Now can we send this? */
5873 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5874 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5875 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5876 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5877 /* got data while shutting down */
5878 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5879 error = ECONNRESET;
5880 goto out_now;
5881 }
5882 sctp_alloc_a_strmoq(stcb, sp);
5883 if (sp == NULL) {
5884 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5885 error = ENOMEM;
5886 goto out_now;
5887 }
5888 sp->sinfo_flags = srcv->sinfo_flags;
5889 sp->timetolive = srcv->sinfo_timetolive;
5890 sp->ppid = srcv->sinfo_ppid;
5891 sp->context = srcv->sinfo_context;
5892 sp->strseq = 0;
5893 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5894 sp->net = net;
5895 sp->addr_over = 1;
5896 } else {
5897 sp->net = stcb->asoc.primary_destination;
5898 sp->addr_over = 0;
5899 }
5900 atomic_add_int(&sp->net->ref_count, 1);
5901 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5902 sp->stream = srcv->sinfo_stream;
5903 sp->msg_is_complete = 1;
5904 sp->sender_all_done = 1;
5905 sp->some_taken = 0;
5906 sp->data = m;
5907 sp->tail_mbuf = NULL;
5908 sp->length = 0;
5909 at = m;
5910 sctp_set_prsctp_policy(sp);
5911 /*
5912 * We could in theory (for sendall) sifa the length in, but we would
5913 * still have to hunt through the chain since we need to setup the
5914 * tail_mbuf
5915 */
5916 while (at) {
5917 if (SCTP_BUF_NEXT(at) == NULL)
5918 sp->tail_mbuf = at;
5919 sp->length += SCTP_BUF_LEN(at);
5920 at = SCTP_BUF_NEXT(at);
5921 }
5922 SCTP_TCB_SEND_LOCK(stcb);
5923 sctp_snd_sb_alloc(stcb, sp->length);
5924 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5925 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5926 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5927 sp->strseq = strm->next_sequence_sent;
5928 strm->next_sequence_sent++;
5929 }
5930 if ((strm->next_spoke.tqe_next == NULL) &&
5931 (strm->next_spoke.tqe_prev == NULL)) {
5932 /* Not on wheel, insert */
5933 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5934 }
5935 m = NULL;
5936 SCTP_TCB_SEND_UNLOCK(stcb);
5937out_now:
5938 if (m) {
5939 sctp_m_freem(m);
5940 }
5941 return (error);
5942}
5943
5944
5945static struct mbuf *
5946sctp_copy_mbufchain(struct mbuf *clonechain,
5947 struct mbuf *outchain,
5948 struct mbuf **endofchain,
5949 int can_take_mbuf,
5950 int sizeofcpy,
5951 uint8_t copy_by_ref)
5952{
5953 struct mbuf *m;
5954 struct mbuf *appendchain;
5955 caddr_t cp;
5956 int len;
5957
5958 if (endofchain == NULL) {
5959 /* error */
5960error_out:
5961 if (outchain)
5962 sctp_m_freem(outchain);
5963 return (NULL);
5964 }
5965 if (can_take_mbuf) {
5966 appendchain = clonechain;
5967 } else {
5968 if (!copy_by_ref &&
5969 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5970 ) {
5971 /* Its not in a cluster */
5972 if (*endofchain == NULL) {
5973 /* lets get a mbuf cluster */
5974 if (outchain == NULL) {
5975 /* This is the general case */
5976 new_mbuf:
5977 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5978 if (outchain == NULL) {
5979 goto error_out;
5980 }
5981 SCTP_BUF_LEN(outchain) = 0;
5982 *endofchain = outchain;
5983 /* get the prepend space */
5984 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5985 } else {
5986 /*
5987 * We really should not get a NULL
5988 * in endofchain
5989 */
5990 /* find end */
5991 m = outchain;
5992 while (m) {
5993 if (SCTP_BUF_NEXT(m) == NULL) {
5994 *endofchain = m;
5995 break;
5996 }
5997 m = SCTP_BUF_NEXT(m);
5998 }
5999 /* sanity */
6000 if (*endofchain == NULL) {
6001 /*
6002 * huh, TSNH XXX maybe we
6003 * should panic
6004 */
6005 sctp_m_freem(outchain);
6006 goto new_mbuf;
6007 }
6008 }
6009 /* get the new end of length */
6010 len = M_TRAILINGSPACE(*endofchain);
6011 } else {
6012 /* how much is left at the end? */
6013 len = M_TRAILINGSPACE(*endofchain);
6014 }
6015 /* Find the end of the data, for appending */
6016 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6017
6018 /* Now lets copy it out */
6019 if (len >= sizeofcpy) {
6020 /* It all fits, copy it in */
6021 m_copydata(clonechain, 0, sizeofcpy, cp);
6022 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6023 } else {
6024 /* fill up the end of the chain */
6025 if (len > 0) {
6026 m_copydata(clonechain, 0, len, cp);
6027 SCTP_BUF_LEN((*endofchain)) += len;
6028 /* now we need another one */
6029 sizeofcpy -= len;
6030 }
6031 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6032 if (m == NULL) {
6033 /* We failed */
6034 goto error_out;
6035 }
6036 SCTP_BUF_NEXT((*endofchain)) = m;
6037 *endofchain = m;
6038 cp = mtod((*endofchain), caddr_t);
6039 m_copydata(clonechain, len, sizeofcpy, cp);
6040 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6041 }
6042 return (outchain);
6043 } else {
6044 /* copy the old fashion way */
6045 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6046#ifdef SCTP_MBUF_LOGGING
6047 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6048 struct mbuf *mat;
6049
6050 mat = appendchain;
6051 while (mat) {
6052 if (SCTP_BUF_IS_EXTENDED(mat)) {
6053 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6054 }
6055 mat = SCTP_BUF_NEXT(mat);
6056 }
6057 }
6058#endif
6059 }
6060 }
6061 if (appendchain == NULL) {
6062 /* error */
6063 if (outchain)
6064 sctp_m_freem(outchain);
6065 return (NULL);
6066 }
6067 if (outchain) {
6068 /* tack on to the end */
6069 if (*endofchain != NULL) {
6070 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6071 } else {
6072 m = outchain;
6073 while (m) {
6074 if (SCTP_BUF_NEXT(m) == NULL) {
6075 SCTP_BUF_NEXT(m) = appendchain;
6076 break;
6077 }
6078 m = SCTP_BUF_NEXT(m);
6079 }
6080 }
6081 /*
6082 * save off the end and update the end-chain postion
6083 */
6084 m = appendchain;
6085 while (m) {
6086 if (SCTP_BUF_NEXT(m) == NULL) {
6087 *endofchain = m;
6088 break;
6089 }
6090 m = SCTP_BUF_NEXT(m);
6091 }
6092 return (outchain);
6093 } else {
6094 /* save off the end and update the end-chain postion */
6095 m = appendchain;
6096 while (m) {
6097 if (SCTP_BUF_NEXT(m) == NULL) {
6098 *endofchain = m;
6099 break;
6100 }
6101 m = SCTP_BUF_NEXT(m);
6102 }
6103 return (appendchain);
6104 }
6105}
6106
6107int
6108sctp_med_chunk_output(struct sctp_inpcb *inp,
6109 struct sctp_tcb *stcb,
6110 struct sctp_association *asoc,
6111 int *num_out,
6112 int *reason_code,
6113 int control_only, int from_where,
6114 struct timeval *now, int *now_filled, int frag_point, int so_locked
6115#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6116 SCTP_UNUSED
6117#endif
6118);
6119
6120static void
6121sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6122 uint32_t val)
6123{
6124 struct sctp_copy_all *ca;
6125 struct mbuf *m;
6126 int ret = 0;
6127 int added_control = 0;
6128 int un_sent, do_chunk_output = 1;
6129 struct sctp_association *asoc;
6130
6131 ca = (struct sctp_copy_all *)ptr;
6132 if (ca->m == NULL) {
6133 return;
6134 }
6135 if (ca->inp != inp) {
6136 /* TSNH */
6137 return;
6138 }
6139 if ((ca->m) && ca->sndlen) {
6140 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6141 if (m == NULL) {
6142 /* can't copy so we are done */
6143 ca->cnt_failed++;
6144 return;
6145 }
6146#ifdef SCTP_MBUF_LOGGING
6147 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6148 struct mbuf *mat;
6149
6150 mat = m;
6151 while (mat) {
6152 if (SCTP_BUF_IS_EXTENDED(mat)) {
6153 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6154 }
6155 mat = SCTP_BUF_NEXT(mat);
6156 }
6157 }
6158#endif
6159 } else {
6160 m = NULL;
6161 }
6162 SCTP_TCB_LOCK_ASSERT(stcb);
6163 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6164 /* Abort this assoc with m as the user defined reason */
6165 if (m) {
6166 struct sctp_paramhdr *ph;
6167
6168 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6169 if (m) {
6170 ph = mtod(m, struct sctp_paramhdr *);
6171 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6172 ph->param_length = htons(ca->sndlen);
6173 }
6174 /*
6175 * We add one here to keep the assoc from
6176 * dis-appearing on us.
6177 */
6178 atomic_add_int(&stcb->asoc.refcnt, 1);
6179 sctp_abort_an_association(inp, stcb,
6180 SCTP_RESPONSE_TO_USER_REQ,
6181 m, SCTP_SO_NOT_LOCKED);
6182 /*
6183 * sctp_abort_an_association calls sctp_free_asoc()
6184 * free association will NOT free it since we
6185 * incremented the refcnt .. we do this to prevent
6186 * it being freed and things getting tricky since we
6187 * could end up (from free_asoc) calling inpcb_free
6188 * which would get a recursive lock call to the
6189 * iterator lock.. But as a consequence of that the
6190 * stcb will return to us un-locked.. since
6191 * free_asoc returns with either no TCB or the TCB
6192 * unlocked, we must relock.. to unlock in the
6193 * iterator timer :-0
6194 */
6195 SCTP_TCB_LOCK(stcb);
6196 atomic_add_int(&stcb->asoc.refcnt, -1);
6197 goto no_chunk_output;
6198 }
6199 } else {
6200 if (m) {
6201 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6202 &ca->sndrcv, 1);
6203 }
6204 asoc = &stcb->asoc;
6205 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6206 /* shutdown this assoc */
6207 int cnt;
6208
6209 cnt = sctp_is_there_unsent_data(stcb);
6210
6211 if (TAILQ_EMPTY(&asoc->send_queue) &&
6212 TAILQ_EMPTY(&asoc->sent_queue) &&
6213 (cnt == 0)) {
6214 if (asoc->locked_on_sending) {
6215 goto abort_anyway;
6216 }
6217 /*
6218 * there is nothing queued to send, so I'm
6219 * done...
6220 */
6221 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6222 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6223 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6224 /*
6225 * only send SHUTDOWN the first time
6226 * through
6227 */
6228 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6229 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6230 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6231 }
6232 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6233 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6234 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6235 asoc->primary_destination);
6236 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6237 asoc->primary_destination);
6238 added_control = 1;
6239 do_chunk_output = 0;
6240 }
6241 } else {
6242 /*
6243 * we still got (or just got) data to send,
6244 * so set SHUTDOWN_PENDING
6245 */
6246 /*
6247 * XXX sockets draft says that SCTP_EOF
6248 * should be sent with no data. currently,
6249 * we will allow user data to be sent first
6250 * and move to SHUTDOWN-PENDING
6251 */
6252 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6253 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6254 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6255 if (asoc->locked_on_sending) {
6256 /*
6257 * Locked to send out the
6258 * data
6259 */
6260 struct sctp_stream_queue_pending *sp;
6261
6262 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6263 if (sp) {
6264 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6265 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6266 }
6267 }
6268 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6269 if (TAILQ_EMPTY(&asoc->send_queue) &&
6270 TAILQ_EMPTY(&asoc->sent_queue) &&
6271 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6272 abort_anyway:
6273 atomic_add_int(&stcb->asoc.refcnt, 1);
6274 sctp_abort_an_association(stcb->sctp_ep, stcb,
6275 SCTP_RESPONSE_TO_USER_REQ,
6276 NULL, SCTP_SO_NOT_LOCKED);
6277 atomic_add_int(&stcb->asoc.refcnt, -1);
6278 goto no_chunk_output;
6279 }
6280 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6281 asoc->primary_destination);
6282 }
6283 }
6284
6285 }
6286 }
6287 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6288 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6289
6290 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6291 (stcb->asoc.total_flight > 0) &&
6292 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6293 ) {
6294 do_chunk_output = 0;
6295 }
6296 if (do_chunk_output)
6297 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6298 else if (added_control) {
6299 int num_out = 0, reason = 0, now_filled = 0;
6300 struct timeval now;
6301 int frag_point;
6302
6303 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6304 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6305 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6306 }
6307no_chunk_output:
6308 if (ret) {
6309 ca->cnt_failed++;
6310 } else {
6311 ca->cnt_sent++;
6312 }
6313}
6314
6315static void
6316sctp_sendall_completes(void *ptr, uint32_t val)
6317{
6318 struct sctp_copy_all *ca;
6319
6320 ca = (struct sctp_copy_all *)ptr;
6321 /*
6322 * Do a notify here? Kacheong suggests that the notify be done at
6323 * the send time.. so you would push up a notification if any send
6324 * failed. Don't know if this is feasable since the only failures we
6325 * have is "memory" related and if you cannot get an mbuf to send
6326 * the data you surely can't get an mbuf to send up to notify the
6327 * user you can't send the data :->
6328 */
6329
6330 /* now free everything */
6331 sctp_m_freem(ca->m);
6332 SCTP_FREE(ca, SCTP_M_COPYAL);
6333}
6334
6335
6336#define MC_ALIGN(m, len) do { \
6337 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6338} while (0)
6339
6340
6341
6342static struct mbuf *
6343sctp_copy_out_all(struct uio *uio, int len)
6344{
6345 struct mbuf *ret, *at;
6346 int left, willcpy, cancpy, error;
6347
6348 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6349 if (ret == NULL) {
6350 /* TSNH */
6351 return (NULL);
6352 }
6353 left = len;
6354 SCTP_BUF_LEN(ret) = 0;
6355 /* save space for the data chunk header */
6356 cancpy = M_TRAILINGSPACE(ret);
6357 willcpy = min(cancpy, left);
6358 at = ret;
6359 while (left > 0) {
6360 /* Align data to the end */
6361 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6362 if (error) {
6363 err_out_now:
6364 sctp_m_freem(at);
6365 return (NULL);
6366 }
6367 SCTP_BUF_LEN(at) = willcpy;
6368 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6369 left -= willcpy;
6370 if (left > 0) {
6371 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6372 if (SCTP_BUF_NEXT(at) == NULL) {
6373 goto err_out_now;
6374 }
6375 at = SCTP_BUF_NEXT(at);
6376 SCTP_BUF_LEN(at) = 0;
6377 cancpy = M_TRAILINGSPACE(at);
6378 willcpy = min(cancpy, left);
6379 }
6380 }
6381 return (ret);
6382}
6383
6384static int
6385sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6386 struct sctp_sndrcvinfo *srcv)
6387{
6388 int ret;
6389 struct sctp_copy_all *ca;
6390
6391 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6392 SCTP_M_COPYAL);
6393 if (ca == NULL) {
6394 sctp_m_freem(m);
6395 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6396 return (ENOMEM);
6397 }
6398 memset(ca, 0, sizeof(struct sctp_copy_all));
6399
6400 ca->inp = inp;
6401 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6402 /*
6403 * take off the sendall flag, it would be bad if we failed to do
6404 * this :-0
6405 */
6406 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6407 /* get length and mbuf chain */
6408 if (uio) {
6409 ca->sndlen = uio->uio_resid;
6410 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6411 if (ca->m == NULL) {
6412 SCTP_FREE(ca, SCTP_M_COPYAL);
6413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6414 return (ENOMEM);
6415 }
6416 } else {
6417 /* Gather the length of the send */
6418 struct mbuf *mat;
6419
6420 mat = m;
6421 ca->sndlen = 0;
6422 while (m) {
6423 ca->sndlen += SCTP_BUF_LEN(m);
6424 m = SCTP_BUF_NEXT(m);
6425 }
6426 ca->m = mat;
6427 }
6428 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6429 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6430 SCTP_ASOC_ANY_STATE,
6431 (void *)ca, 0,
6432 sctp_sendall_completes, inp, 1);
6433 if (ret) {
6434 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6435 SCTP_FREE(ca, SCTP_M_COPYAL);
6436 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6437 return (EFAULT);
6438 }
6439 return (0);
6440}
6441
6442
6443void
6444sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6445{
6446 struct sctp_tmit_chunk *chk, *nchk;
6447
6448 chk = TAILQ_FIRST(&asoc->control_send_queue);
6449 while (chk) {
6450 nchk = TAILQ_NEXT(chk, sctp_next);
6451 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6452 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6453 if (chk->data) {
6454 sctp_m_freem(chk->data);
6455 chk->data = NULL;
6456 }
6457 asoc->ctrl_queue_cnt--;
6458 sctp_free_a_chunk(stcb, chk);
6459 }
6460 chk = nchk;
6461 }
6462}
6463
6464void
6465sctp_toss_old_asconf(struct sctp_tcb *stcb)
6466{
6467 struct sctp_association *asoc;
6468 struct sctp_tmit_chunk *chk, *chk_tmp;
6469 struct sctp_asconf_chunk *acp;
6470
6471 asoc = &stcb->asoc;
6472 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6473 chk = chk_tmp) {
6474 /* get next chk */
6475 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6476 /* find SCTP_ASCONF chunk in queue */
6477 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6478 if (chk->data) {
6479 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6480 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6481 /* Not Acked yet */
6482 break;
6483 }
6484 }
6485 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6486 if (chk->data) {
6487 sctp_m_freem(chk->data);
6488 chk->data = NULL;
6489 }
6490 asoc->ctrl_queue_cnt--;
6491 sctp_free_a_chunk(stcb, chk);
6492 }
6493 }
6494}
6495
6496
6497static void
6498sctp_clean_up_datalist(struct sctp_tcb *stcb,
6499
6500 struct sctp_association *asoc,
6501 struct sctp_tmit_chunk **data_list,
6502 int bundle_at,
6503 struct sctp_nets *net)
6504{
6505 int i;
6506 struct sctp_tmit_chunk *tp1;
6507
6508 for (i = 0; i < bundle_at; i++) {
6509 /* off of the send queue */
6510 if (i) {
6511 /*
6512 * Any chunk NOT 0 you zap the time chunk 0 gets
6513 * zapped or set based on if a RTO measurment is
6514 * needed.
6515 */
6516 data_list[i]->do_rtt = 0;
6517 }
6518 /* record time */
6519 data_list[i]->sent_rcv_time = net->last_sent_time;
6520 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6521 TAILQ_REMOVE(&asoc->send_queue,
6522 data_list[i],
6523 sctp_next);
6524 /* on to the sent queue */
6525 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6526 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6527 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6528 struct sctp_tmit_chunk *tpp;
6529
6530 /* need to move back */
6531 back_up_more:
6532 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6533 if (tpp == NULL) {
6534 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6535 goto all_done;
6536 }
6537 tp1 = tpp;
6538 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6539 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6540 goto back_up_more;
6541 }
6542 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6543 } else {
6544 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6545 data_list[i],
6546 sctp_next);
6547 }
6548all_done:
6549 /* This does not lower until the cum-ack passes it */
6550 asoc->sent_queue_cnt++;
6551 asoc->send_queue_cnt--;
6552 if ((asoc->peers_rwnd <= 0) &&
6553 (asoc->total_flight == 0) &&
6554 (bundle_at == 1)) {
6555 /* Mark the chunk as being a window probe */
6556 SCTP_STAT_INCR(sctps_windowprobed);
6557 }
6558#ifdef SCTP_AUDITING_ENABLED
6559 sctp_audit_log(0xC2, 3);
6560#endif
6561 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6562 data_list[i]->snd_count = 1;
6563 data_list[i]->rec.data.chunk_was_revoked = 0;
6564 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6565 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6566 data_list[i]->whoTo->flight_size,
6567 data_list[i]->book_size,
6568 (uintptr_t) data_list[i]->whoTo,
6569 data_list[i]->rec.data.TSN_seq);
6570 }
6571 sctp_flight_size_increase(data_list[i]);
6572 sctp_total_flight_increase(stcb, data_list[i]);
6573 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6574 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6575 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6576 }
6577 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6578 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6579 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6580 /* SWS sender side engages */
6581 asoc->peers_rwnd = 0;
6582 }
6583 }
6584}
6585
6586static void
6587sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6588{
6589 struct sctp_tmit_chunk *chk, *nchk;
6590
6591 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6592 chk; chk = nchk) {
6593 nchk = TAILQ_NEXT(chk, sctp_next);
6594 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6595 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
6596 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6597 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6598 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6599 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6600 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6601 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6602 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6603 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6604 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6605 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6606 /* Stray chunks must be cleaned up */
6607 clean_up_anyway:
6608 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6609 if (chk->data) {
6610 sctp_m_freem(chk->data);
6611 chk->data = NULL;
6612 }
6613 asoc->ctrl_queue_cnt--;
6614 sctp_free_a_chunk(stcb, chk);
6615 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6616 /* special handling, we must look into the param */
6617 if (chk != asoc->str_reset) {
6618 goto clean_up_anyway;
6619 }
6620 }
6621 }
6622}
6623
6624
6625static int
6626sctp_can_we_split_this(struct sctp_tcb *stcb,
6627 uint32_t length,
6628 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6629{
6630 /*
6631 * Make a decision on if I should split a msg into multiple parts.
6632 * This is only asked of incomplete messages.
6633 */
6634 if (eeor_on) {
6635 /*
6636 * If we are doing EEOR we need to always send it if its the
6637 * entire thing, since it might be all the guy is putting in
6638 * the hopper.
6639 */
6640 if (goal_mtu >= length) {
6641 /*-
6642 * If we have data outstanding,
6643 * we get another chance when the sack
6644 * arrives to transmit - wait for more data
6645 */
6646 if (stcb->asoc.total_flight == 0) {
6647 /*
6648 * If nothing is in flight, we zero the
6649 * packet counter.
6650 */
6651 return (length);
6652 }
6653 return (0);
6654
6655 } else {
6656 /* You can fill the rest */
6657 return (goal_mtu);
6658 }
6659 }
6660 /*-
6661 * For those strange folk that make the send buffer
6662 * smaller than our fragmentation point, we can't
6663 * get a full msg in so we have to allow splitting.
6664 */
6665 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6666 return (length);
6667 }
6668 if ((length <= goal_mtu) ||
6669 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6670 /* Sub-optimial residual don't split in non-eeor mode. */
6671 return (0);
6672 }
6673 /*
6674 * If we reach here length is larger than the goal_mtu. Do we wish
6675 * to split it for the sake of packet putting together?
6676 */
6677 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6678 /* Its ok to split it */
6679 return (min(goal_mtu, frag_point));
6680 }
6681 /* Nope, can't split */
6682 return (0);
6683
6684}
6685
6686static uint32_t
6687sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6688 struct sctp_stream_out *strq,
6689 uint32_t goal_mtu,
6690 uint32_t frag_point,
6691 int *locked,
6692 int *giveup,
6693 int eeor_mode,
6694 int *bail)
6695{
6696 /* Move from the stream to the send_queue keeping track of the total */
6697 struct sctp_association *asoc;
6698 struct sctp_stream_queue_pending *sp;
6699 struct sctp_tmit_chunk *chk;
6700 struct sctp_data_chunk *dchkh;
6701 uint32_t to_move, length;
6702 uint8_t rcv_flags = 0;
6703 uint8_t some_taken;
6704 uint8_t send_lock_up = 0;
6705
6706 SCTP_TCB_LOCK_ASSERT(stcb);
6707 asoc = &stcb->asoc;
6708one_more_time:
6709 /* sa_ignore FREED_MEMORY */
6710 sp = TAILQ_FIRST(&strq->outqueue);
6711 if (sp == NULL) {
6712 *locked = 0;
6713 if (send_lock_up == 0) {
6714 SCTP_TCB_SEND_LOCK(stcb);
6715 send_lock_up = 1;
6716 }
6717 sp = TAILQ_FIRST(&strq->outqueue);
6718 if (sp) {
6719 goto one_more_time;
6720 }
6721 if (strq->last_msg_incomplete) {
6722 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6723 strq->stream_no,
6724 strq->last_msg_incomplete);
6725 strq->last_msg_incomplete = 0;
6726 }
6727 to_move = 0;
6728 if (send_lock_up) {
6729 SCTP_TCB_SEND_UNLOCK(stcb);
6730 send_lock_up = 0;
6731 }
6732 goto out_of;
6733 }
6734 if ((sp->msg_is_complete) && (sp->length == 0)) {
6735 if (sp->sender_all_done) {
6736 /*
6737 * We are doing differed cleanup. Last time through
6738 * when we took all the data the sender_all_done was
6739 * not set.
6740 */
6741 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6742 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6743 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6744 sp->sender_all_done,
6745 sp->length,
6746 sp->msg_is_complete,
6747 sp->put_last_out,
6748 send_lock_up);
6749 }
6750 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6751 SCTP_TCB_SEND_LOCK(stcb);
6752 send_lock_up = 1;
6753 }
6754 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6755 TAILQ_REMOVE(&strq->outqueue, sp, next);
6756 sctp_free_remote_addr(sp->net);
6757 if (sp->data) {
6758 sctp_m_freem(sp->data);
6759 sp->data = NULL;
6760 }
6761 sctp_free_a_strmoq(stcb, sp);
6762 /* we can't be locked to it */
6763 *locked = 0;
6764 stcb->asoc.locked_on_sending = NULL;
6765 if (send_lock_up) {
6766 SCTP_TCB_SEND_UNLOCK(stcb);
6767 send_lock_up = 0;
6768 }
6769 /* back to get the next msg */
6770 goto one_more_time;
6771 } else {
6772 /*
6773 * sender just finished this but still holds a
6774 * reference
6775 */
6776 *locked = 1;
6777 *giveup = 1;
6778 to_move = 0;
6779 goto out_of;
6780 }
6781 } else {
6782 /* is there some to get */
6783 if (sp->length == 0) {
6784 /* no */
6785 *locked = 1;
6786 *giveup = 1;
6787 to_move = 0;
6788 goto out_of;
6789 } else if (sp->discard_rest) {
6790 if (send_lock_up == 0) {
6791 SCTP_TCB_SEND_LOCK(stcb);
6792 send_lock_up = 1;
6793 }
6794 /* Whack down the size */
6795 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6796 if ((stcb->sctp_socket != NULL) && \
6797 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6798 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6799 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6800 }
6801 if (sp->data) {
6802 sctp_m_freem(sp->data);
6803 sp->data = NULL;
6804 sp->tail_mbuf = NULL;
6805 }
6806 sp->length = 0;
6807 sp->some_taken = 1;
6808 *locked = 1;
6809 *giveup = 1;
6810 to_move = 0;
6811 goto out_of;
6812 }
6813 }
6814 some_taken = sp->some_taken;
6815 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6816 sp->msg_is_complete = 1;
6817 }
6818re_look:
6819 length = sp->length;
6820 if (sp->msg_is_complete) {
6821 /* The message is complete */
6822 to_move = min(length, frag_point);
6823 if (to_move == length) {
6824 /* All of it fits in the MTU */
6825 if (sp->some_taken) {
6826 rcv_flags |= SCTP_DATA_LAST_FRAG;
6827 sp->put_last_out = 1;
6828 } else {
6829 rcv_flags |= SCTP_DATA_NOT_FRAG;
6830 sp->put_last_out = 1;
6831 }
6832 } else {
6833 /* Not all of it fits, we fragment */
6834 if (sp->some_taken == 0) {
6835 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6836 }
6837 sp->some_taken = 1;
6838 }
6839 } else {
6840 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6841 if (to_move) {
6842 /*-
6843 * We use a snapshot of length in case it
6844 * is expanding during the compare.
6845 */
6846 uint32_t llen;
6847
6848 llen = length;
6849 if (to_move >= llen) {
6850 to_move = llen;
6851 if (send_lock_up == 0) {
6852 /*-
6853 * We are taking all of an incomplete msg
6854 * thus we need a send lock.
6855 */
6856 SCTP_TCB_SEND_LOCK(stcb);
6857 send_lock_up = 1;
6858 if (sp->msg_is_complete) {
6859 /*
6860 * the sender finished the
6861 * msg
6862 */
6863 goto re_look;
6864 }
6865 }
6866 }
6867 if (sp->some_taken == 0) {
6868 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6869 sp->some_taken = 1;
6870 }
6871 } else {
6872 /* Nothing to take. */
6873 if (sp->some_taken) {
6874 *locked = 1;
6875 }
6876 *giveup = 1;
6877 to_move = 0;
6878 goto out_of;
6879 }
6880 }
6881
6882 /* If we reach here, we can copy out a chunk */
6883 sctp_alloc_a_chunk(stcb, chk);
6884 if (chk == NULL) {
6885 /* No chunk memory */
6886 *giveup = 1;
6887 to_move = 0;
6888 goto out_of;
6889 }
6890 /*
6891 * Setup for unordered if needed by looking at the user sent info
6892 * flags.
6893 */
6894 if (sp->sinfo_flags & SCTP_UNORDERED) {
6895 rcv_flags |= SCTP_DATA_UNORDERED;
6896 }
6897 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6898 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6899 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6900 }
6901 /* clear out the chunk before setting up */
6902 memset(chk, 0, sizeof(*chk));
6903 chk->rec.data.rcv_flags = rcv_flags;
6904
6905 if (to_move >= length) {
6906 /* we think we can steal the whole thing */
6907 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6908 SCTP_TCB_SEND_LOCK(stcb);
6909 send_lock_up = 1;
6910 }
6911 if (to_move < sp->length) {
6912 /* bail, it changed */
6913 goto dont_do_it;
6914 }
6915 chk->data = sp->data;
6916 chk->last_mbuf = sp->tail_mbuf;
6917 /* register the stealing */
6918 sp->data = sp->tail_mbuf = NULL;
6919 } else {
6920 struct mbuf *m;
6921
6922dont_do_it:
6923 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6924 chk->last_mbuf = NULL;
6925 if (chk->data == NULL) {
6926 sp->some_taken = some_taken;
6927 sctp_free_a_chunk(stcb, chk);
6928 *bail = 1;
6929 to_move = 0;
6930 goto out_of;
6931 }
6932#ifdef SCTP_MBUF_LOGGING
6933 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6934 struct mbuf *mat;
6935
6936 mat = chk->data;
6937 while (mat) {
6938 if (SCTP_BUF_IS_EXTENDED(mat)) {
6939 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6940 }
6941 mat = SCTP_BUF_NEXT(mat);
6942 }
6943 }
6944#endif
6945 /* Pull off the data */
6946 m_adj(sp->data, to_move);
6947 /* Now lets work our way down and compact it */
6948 m = sp->data;
6949 while (m && (SCTP_BUF_LEN(m) == 0)) {
6950 sp->data = SCTP_BUF_NEXT(m);
6951 SCTP_BUF_NEXT(m) = NULL;
6952 if (sp->tail_mbuf == m) {
6953 /*-
6954 * Freeing tail? TSNH since
6955 * we supposedly were taking less
6956 * than the sp->length.
6957 */
6958#ifdef INVARIANTS
6959 panic("Huh, freing tail? - TSNH");
6960#else
6961 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6962 sp->tail_mbuf = sp->data = NULL;
6963 sp->length = 0;
6964#endif
6965
6966 }
6967 sctp_m_free(m);
6968 m = sp->data;
6969 }
6970 }
6971 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6972 chk->copy_by_ref = 1;
6973 } else {
6974 chk->copy_by_ref = 0;
6975 }
6976 /*
6977 * get last_mbuf and counts of mb useage This is ugly but hopefully
6978 * its only one mbuf.
6979 */
6980 if (chk->last_mbuf == NULL) {
6981 chk->last_mbuf = chk->data;
6982 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6983 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6984 }
6985 }
6986 if (to_move > length) {
6987 /*- This should not happen either
6988 * since we always lower to_move to the size
6989 * of sp->length if its larger.
6990 */
6991#ifdef INVARIANTS
6992 panic("Huh, how can to_move be larger?");
6993#else
6994 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6995 sp->length = 0;
6996#endif
6997 } else {
6998 atomic_subtract_int(&sp->length, to_move);
6999 }
7000 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7001 /* Not enough room for a chunk header, get some */
7002 struct mbuf *m;
7003
7004 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7005 if (m == NULL) {
7006 /*
7007 * we're in trouble here. _PREPEND below will free
7008 * all the data if there is no leading space, so we
7009 * must put the data back and restore.
7010 */
7011 if (send_lock_up == 0) {
7012 SCTP_TCB_SEND_LOCK(stcb);
7013 send_lock_up = 1;
7014 }
7015 if (chk->data == NULL) {
7016 /* unsteal the data */
7017 sp->data = chk->data;
7018 sp->tail_mbuf = chk->last_mbuf;
7019 } else {
7020 struct mbuf *m_tmp;
7021
7022 /* reassemble the data */
7023 m_tmp = sp->data;
7024 sp->data = chk->data;
7025 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7026 }
7027 sp->some_taken = some_taken;
7028 atomic_add_int(&sp->length, to_move);
7029 chk->data = NULL;
7030 *bail = 1;
7031 sctp_free_a_chunk(stcb, chk);
7032 to_move = 0;
7033 goto out_of;
7034 } else {
7035 SCTP_BUF_LEN(m) = 0;
7036 SCTP_BUF_NEXT(m) = chk->data;
7037 chk->data = m;
7038 M_ALIGN(chk->data, 4);
7039 }
7040 }
7041 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7042 if (chk->data == NULL) {
7043 /* HELP, TSNH since we assured it would not above? */
7044#ifdef INVARIANTS
7045 panic("prepend failes HELP?");
7046#else
7047 SCTP_PRINTF("prepend fails HELP?\n");
7048 sctp_free_a_chunk(stcb, chk);
7049#endif
7050 *bail = 1;
7051 to_move = 0;
7052 goto out_of;
7053 }
7054 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7055 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7056 chk->book_size_scale = 0;
7057 chk->sent = SCTP_DATAGRAM_UNSENT;
7058
7059 chk->flags = 0;
7060 chk->asoc = &stcb->asoc;
7061 chk->pad_inplace = 0;
7062 chk->no_fr_allowed = 0;
7063 chk->rec.data.stream_seq = sp->strseq;
7064 chk->rec.data.stream_number = sp->stream;
7065 chk->rec.data.payloadtype = sp->ppid;
7066 chk->rec.data.context = sp->context;
7067 chk->rec.data.doing_fast_retransmit = 0;
7068 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
7069
7070 chk->rec.data.timetodrop = sp->ts;
7071 chk->flags = sp->act_flags;
7072 chk->addr_over = sp->addr_over;
7073
7074 chk->whoTo = net;
7075 atomic_add_int(&chk->whoTo->ref_count, 1);
7076
7077 if (sp->holds_key_ref) {
7078 chk->auth_keyid = sp->auth_keyid;
7079 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7080 chk->holds_key_ref = 1;
7081 }
7082 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7083 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7084 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7085 (uintptr_t) stcb, sp->length,
7086 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7087 chk->rec.data.TSN_seq);
7088 }
7089 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7090 /*
7091 * Put the rest of the things in place now. Size was done earlier in
7092 * previous loop prior to padding.
7093 */
7094
7095#ifdef SCTP_ASOCLOG_OF_TSNS
7096 SCTP_TCB_LOCK_ASSERT(stcb);
7097 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7098 asoc->tsn_out_at = 0;
7099 asoc->tsn_out_wrapped = 1;
7100 }
7101 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7102 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7103 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7104 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7105 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7106 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7107 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7108 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7109 asoc->tsn_out_at++;
7110#endif
7111
7112 dchkh->ch.chunk_type = SCTP_DATA;
7113 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7114 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7115 dchkh->dp.stream_id = htons(strq->stream_no);
7116 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7117 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7118 dchkh->ch.chunk_length = htons(chk->send_size);
7119 /* Now advance the chk->send_size by the actual pad needed. */
7120 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7121 /* need a pad */
7122 struct mbuf *lm;
7123 int pads;
7124
7125 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7126 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7127 chk->pad_inplace = 1;
7128 }
7129 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7130 /* pad added an mbuf */
7131 chk->last_mbuf = lm;
7132 }
7133 chk->send_size += pads;
7134 }
7135 /* We only re-set the policy if it is on */
7136 if (sp->pr_sctp_on) {
7137 sctp_set_prsctp_policy(sp);
7138 asoc->pr_sctp_cnt++;
7139 chk->pr_sctp_on = 1;
7140 } else {
7141 chk->pr_sctp_on = 0;
7142 }
7143 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7144 /* All done pull and kill the message */
7145 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7146 if (sp->put_last_out == 0) {
7147 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7148 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7149 sp->sender_all_done,
7150 sp->length,
7151 sp->msg_is_complete,
7152 sp->put_last_out,
7153 send_lock_up);
7154 }
7155 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7156 SCTP_TCB_SEND_LOCK(stcb);
7157 send_lock_up = 1;
7158 }
7159 TAILQ_REMOVE(&strq->outqueue, sp, next);
7160 sctp_free_remote_addr(sp->net);
7161 if (sp->data) {
7162 sctp_m_freem(sp->data);
7163 sp->data = NULL;
7164 }
7165 sctp_free_a_strmoq(stcb, sp);
7166
7167 /* we can't be locked to it */
7168 *locked = 0;
7169 stcb->asoc.locked_on_sending = NULL;
7170 } else {
7171 /* more to go, we are locked */
7172 *locked = 1;
7173 }
7174 asoc->chunks_on_out_queue++;
7175 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7176 asoc->send_queue_cnt++;
7177out_of:
7178 if (send_lock_up) {
7179 SCTP_TCB_SEND_UNLOCK(stcb);
7180 send_lock_up = 0;
7181 }
7182 return (to_move);
7183}
7184
7185
7186static struct sctp_stream_out *
7187sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7188{
7189 struct sctp_stream_out *strq;
7190
7191 /* Find the next stream to use */
7192 if (asoc->last_out_stream == NULL) {
7193 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7194 if (asoc->last_out_stream == NULL) {
7195 /* huh nothing on the wheel, TSNH */
7196 return (NULL);
7197 }
7198 goto done_it;
7199 }
7200 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7201done_it:
7202 if (strq == NULL) {
7203 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7204 }
7205 /* Save off the last stream */
7206 asoc->last_out_stream = strq;
7207 return (strq);
7208
7209}
7210
7211
7212static void
7213sctp_fill_outqueue(struct sctp_tcb *stcb,
7214 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7215{
7216 struct sctp_association *asoc;
7217 struct sctp_stream_out *strq, *strqn, *strqt;
7218 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7219 int locked, giveup;
7220 struct sctp_stream_queue_pending *sp;
7221
7222 SCTP_TCB_LOCK_ASSERT(stcb);
7223 asoc = &stcb->asoc;
7224#ifdef INET6
7225 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7226 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7227 } else {
7228 /* ?? not sure what else to do */
7229 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7230 }
7231#else
7232 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7233#endif
7234 /* Need an allowance for the data chunk header too */
7235 goal_mtu -= sizeof(struct sctp_data_chunk);
7236
7237 /* must make even word boundary */
7238 goal_mtu &= 0xfffffffc;
7239 if (asoc->locked_on_sending) {
7240 /* We are stuck on one stream until the message completes. */
7241 strqn = strq = asoc->locked_on_sending;
7242 locked = 1;
7243 } else {
7244 strqn = strq = sctp_select_a_stream(stcb, asoc);
7245 locked = 0;
7246 }
7247
7248 while ((goal_mtu > 0) && strq) {
7249 sp = TAILQ_FIRST(&strq->outqueue);
7250 /*
7251 * If CMT is off, we must validate that the stream in
7252 * question has the first item pointed towards are network
7253 * destionation requested by the caller. Note that if we
7254 * turn out to be locked to a stream (assigning TSN's then
7255 * we must stop, since we cannot look for another stream
7256 * with data to send to that destination). In CMT's case, by
7257 * skipping this check, we will send one data packet towards
7258 * the requested net.
7259 */
7260 if (sp == NULL) {
7261 break;
7262 }
7263 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7264 /* none for this network */
7265 if (locked) {
7266 break;
7267 } else {
7268 strq = sctp_select_a_stream(stcb, asoc);
7269 if (strq == NULL)
7270 /* none left */
7271 break;
7272 if (strqn == strq) {
7273 /* I have circled */
7274 break;
7275 }
7276 continue;
7277 }
7278 }
7279 giveup = 0;
7280 bail = 0;
7281 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7282 &giveup, eeor_mode, &bail);
7283 asoc->last_out_stream = strq;
7284 if (locked) {
7285 asoc->locked_on_sending = strq;
7286 if ((moved_how_much == 0) || (giveup) || bail)
7287 /* no more to move for now */
7288 break;
7289 } else {
7290 asoc->locked_on_sending = NULL;
7291 strqt = sctp_select_a_stream(stcb, asoc);
7292 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7293 if (strq == strqn) {
7294 /* Must move start to next one */
7295 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7296 if (strqn == NULL) {
7297 strqn = TAILQ_FIRST(&asoc->out_wheel);
7298 if (strqn == NULL) {
7299 break;
7300 }
7301 }
7302 }
7303 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7304 }
7305 if ((giveup) || bail) {
7306 break;
7307 }
7308 strq = strqt;
7309 if (strq == NULL) {
7310 break;
7311 }
7312 }
7313 total_moved += moved_how_much;
7314 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7315 goal_mtu &= 0xfffffffc;
7316 }
7317 if (bail)
7318 *quit_now = 1;
7319
7320 if (total_moved == 0) {
7321 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7322 (net == stcb->asoc.primary_destination)) {
7323 /* ran dry for primary network net */
7324 SCTP_STAT_INCR(sctps_primary_randry);
7325 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7326 /* ran dry with CMT on */
7327 SCTP_STAT_INCR(sctps_cmt_randry);
7328 }
7329 }
7330}
7331
7332void
7333sctp_fix_ecn_echo(struct sctp_association *asoc)
7334{
7335 struct sctp_tmit_chunk *chk;
7336
7337 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7338 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7339 chk->sent = SCTP_DATAGRAM_UNSENT;
7340 }
7341 }
7342}
7343
7344static void
7345sctp_move_to_an_alt(struct sctp_tcb *stcb,
7346 struct sctp_association *asoc,
7347 struct sctp_nets *net)
7348{
7349 struct sctp_tmit_chunk *chk;
7350 struct sctp_nets *a_net;
7351
7352 SCTP_TCB_LOCK_ASSERT(stcb);
7353 /*
7354 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7355 * destination using the PF algorithm for finding alternate
7356 * destinations.
7357 */
7358 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7359 a_net = sctp_find_alternate_net(stcb, net, 2);
7360 } else {
7361 a_net = sctp_find_alternate_net(stcb, net, 0);
7362 }
7363 if ((a_net != net) &&
7364 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7365 /*
7366 * We only proceed if a valid alternate is found that is not
7367 * this one and is reachable. Here we must move all chunks
7368 * queued in the send queue off of the destination address
7369 * to our alternate.
7370 */
7371 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7372 if (chk->whoTo == net) {
7373 /* Move the chunk to our alternate */
7374 sctp_free_remote_addr(chk->whoTo);
7375 chk->whoTo = a_net;
7376 atomic_add_int(&a_net->ref_count, 1);
7377 }
7378 }
7379 }
7380}
7381
7382int
7383sctp_med_chunk_output(struct sctp_inpcb *inp,
7384 struct sctp_tcb *stcb,
7385 struct sctp_association *asoc,
7386 int *num_out,
7387 int *reason_code,
7388 int control_only, int from_where,
7389 struct timeval *now, int *now_filled, int frag_point, int so_locked
7390#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7391 SCTP_UNUSED
7392#endif
7393)
7394{
7395 /*
7396 * Ok this is the generic chunk service queue. we must do the
7397 * following: - Service the stream queue that is next, moving any
7398 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7399 * LAST to the out queue in one pass) and assigning TSN's - Check to
7400 * see if the cwnd/rwnd allows any output, if so we go ahead and
7401 * fomulate and send the low level chunks. Making sure to combine
7402 * any control in the control chunk queue also.
7403 */
7404 struct sctp_nets *net, *start_at, *old_start_at = NULL;
7405 struct mbuf *outchain, *endoutchain;
7406 struct sctp_tmit_chunk *chk, *nchk;
7407
7408 /* temp arrays for unlinking */
7409 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7410 int no_fragmentflg, error;
7411 unsigned int max_rwnd_per_dest;
7412 int one_chunk, hbflag, skip_data_for_this_net;
7413 int asconf, cookie, no_out_cnt;
7414 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7415 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7416 int tsns_sent = 0;
7417 uint32_t auth_offset = 0;
7418 struct sctp_auth_chunk *auth = NULL;
7419 uint16_t auth_keyid;
7420 int override_ok = 1;
7421 int data_auth_reqd = 0;
7422
7423 /*
7424 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7425 * destination.
7426 */
7427 int pf_hbflag = 0;
7428 int quit_now = 0;
7429
7430 *num_out = 0;
7431 auth_keyid = stcb->asoc.authinfo.active_keyid;
7432
7433 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7434 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7435 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7436 eeor_mode = 1;
7437 } else {
7438 eeor_mode = 0;
7439 }
7440 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7441 /*
7442 * First lets prime the pump. For each destination, if there is room
7443 * in the flight size, attempt to pull an MTU's worth out of the
7444 * stream queues into the general send_queue
7445 */
7446#ifdef SCTP_AUDITING_ENABLED
7447 sctp_audit_log(0xC2, 2);
7448#endif
7449 SCTP_TCB_LOCK_ASSERT(stcb);
7450 hbflag = 0;
7451 if ((control_only) || (asoc->stream_reset_outstanding))
7452 no_data_chunks = 1;
7453 else
7454 no_data_chunks = 0;
7455
7456 /* Nothing to possible to send? */
7457 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7458 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7459 TAILQ_EMPTY(&asoc->send_queue) &&
7460 TAILQ_EMPTY(&asoc->out_wheel)) {
7461 *reason_code = 9;
7462 return (0);
7463 }
7464 if (asoc->peers_rwnd == 0) {
7465 /* No room in peers rwnd */
7466 *reason_code = 1;
7467 if (asoc->total_flight > 0) {
7468 /* we are allowed one chunk in flight */
7469 no_data_chunks = 1;
7470 }
7471 }
7472 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7473 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7474 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7475 /*
7476 * This for loop we are in takes in each net, if
7477 * its's got space in cwnd and has data sent to it
7478 * (when CMT is off) then it calls
7479 * sctp_fill_outqueue for the net. This gets data on
7480 * the send queue for that network.
7481 *
7482 * In sctp_fill_outqueue TSN's are assigned and data is
7483 * copied out of the stream buffers. Note mostly
7484 * copy by reference (we hope).
7485 */
7486 net->window_probe = 0;
7487 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7488 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7489 sctp_log_cwnd(stcb, net, 1,
7490 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7491 }
7492 continue;
7493 }
7494 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7495 /* nothing can be in queue for this guy */
7496 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7497 sctp_log_cwnd(stcb, net, 2,
7498 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7499 }
7500 continue;
7501 }
7502 if (net->flight_size >= net->cwnd) {
7503 /* skip this network, no room - can't fill */
7504 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7505 sctp_log_cwnd(stcb, net, 3,
7506 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7507 }
7508 continue;
7509 }
7510 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7511 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7512 }
7513 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7514 if (quit_now) {
7515 /* memory alloc failure */
7516 no_data_chunks = 1;
7517 break;
7518 }
7519 }
7520 }
7521 /* now service each destination and send out what we can for it */
7522 /* Nothing to send? */
7523 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7524 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7525 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7526 *reason_code = 8;
7527 return (0);
7528 }
7529 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7530 /* get the last start point */
7531 start_at = asoc->last_net_cmt_send_started;
7532 if (start_at == NULL) {
7533 /* null so to beginning */
7534 start_at = TAILQ_FIRST(&asoc->nets);
7535 } else {
7536 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7537 if (start_at == NULL) {
7538 start_at = TAILQ_FIRST(&asoc->nets);
7539 }
7540 }
7541 asoc->last_net_cmt_send_started = start_at;
7542 } else {
7543 start_at = TAILQ_FIRST(&asoc->nets);
7544 }
7545 old_start_at = NULL;
7546again_one_more_time:
7547 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7548 /* how much can we send? */
7549 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7550 if (old_start_at && (old_start_at == net)) {
7551 /* through list ocmpletely. */
7552 break;
7553 }
7554 tsns_sent = 0xa;
7555 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7556 /*
7557 * Ref-count of 1 so we cannot have data or control
7558 * queued to this address. Skip it (non-CMT).
7559 */
7560 continue;
7561 }
7562 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7563 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7564 (net->flight_size >= net->cwnd)) {
7565 /*
7566 * Nothing on control or asconf and flight is full,
7567 * we can skip even in the CMT case.
7568 */
7569 continue;
7570 }
7571 ctl_cnt = bundle_at = 0;
7572 endoutchain = outchain = NULL;
7573 no_fragmentflg = 1;
7574 one_chunk = 0;
7575 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7576 skip_data_for_this_net = 1;
7577 } else {
7578 skip_data_for_this_net = 0;
7579 }
7580 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7581 /*
7582 * if we have a route and an ifp check to see if we
7583 * have room to send to this guy
7584 */
7585 struct ifnet *ifp;
7586
7587 ifp = net->ro.ro_rt->rt_ifp;
7588 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7589 SCTP_STAT_INCR(sctps_ifnomemqueued);
7590 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7591 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7592 }
7593 continue;
7594 }
7595 }
7596 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7597 case AF_INET:
7598 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7599 break;
7600#ifdef INET6
7601 case AF_INET6:
7602 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7603 break;
7604#endif
7605 default:
7606 /* TSNH */
7607 mtu = net->mtu;
7608 break;
7609 }
7610 mx_mtu = mtu;
7611 to_out = 0;
7612 if (mtu > asoc->peers_rwnd) {
7613 if (asoc->total_flight > 0) {
7614 /* We have a packet in flight somewhere */
7615 r_mtu = asoc->peers_rwnd;
7616 } else {
7617 /* We are always allowed to send one MTU out */
7618 one_chunk = 1;
7619 r_mtu = mtu;
7620 }
7621 } else {
7622 r_mtu = mtu;
7623 }
7624 /************************/
7625 /* ASCONF transmission */
7626 /************************/
7627 /* Now first lets go through the asconf queue */
7628 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7629 chk; chk = nchk) {
7630 nchk = TAILQ_NEXT(chk, sctp_next);
7631 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7632 continue;
7633 }
7634 if (chk->whoTo != net) {
7635 /*
7636 * No, not sent to the network we are
7637 * looking at
7638 */
7639 break;
7640 }
7641 if (chk->data == NULL) {
7642 break;
7643 }
7644 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7645 chk->sent != SCTP_DATAGRAM_RESEND) {
7646 break;
7647 }
7648 /*
7649 * if no AUTH is yet included and this chunk
7650 * requires it, make sure to account for it. We
7651 * don't apply the size until the AUTH chunk is
7652 * actually added below in case there is no room for
7653 * this chunk. NOTE: we overload the use of "omtu"
7654 * here
7655 */
7656 if ((auth == NULL) &&
7657 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7658 stcb->asoc.peer_auth_chunks)) {
7659 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7660 } else
7661 omtu = 0;
7662 /* Here we do NOT factor the r_mtu */
7663 if ((chk->send_size < (int)(mtu - omtu)) ||
7664 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7665 /*
7666 * We probably should glom the mbuf chain
7667 * from the chk->data for control but the
7668 * problem is it becomes yet one more level
7669 * of tracking to do if for some reason
7670 * output fails. Then I have got to
7671 * reconstruct the merged control chain.. el
7672 * yucko.. for now we take the easy way and
7673 * do the copy
7674 */
7675 /*
7676 * Add an AUTH chunk, if chunk requires it
7677 * save the offset into the chain for AUTH
7678 */
7679 if ((auth == NULL) &&
7680 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7681 stcb->asoc.peer_auth_chunks))) {
7682 outchain = sctp_add_auth_chunk(outchain,
7683 &endoutchain,
7684 &auth,
7685 &auth_offset,
7686 stcb,
7687 chk->rec.chunk_id.id);
7688 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7689 }
7690 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7691 (int)chk->rec.chunk_id.can_take_data,
7692 chk->send_size, chk->copy_by_ref);
7693 if (outchain == NULL) {
7694 *reason_code = 8;
7695 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7696 return (ENOMEM);
7697 }
7698 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7699 /* update our MTU size */
7700 if (mtu > (chk->send_size + omtu))
7701 mtu -= (chk->send_size + omtu);
7702 else
7703 mtu = 0;
7704 to_out += (chk->send_size + omtu);
7705 /* Do clear IP_DF ? */
7706 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7707 no_fragmentflg = 0;
7708 }
7709 if (chk->rec.chunk_id.can_take_data)
7710 chk->data = NULL;
7711 /*
7712 * set hb flag since we can use these for
7713 * RTO
7714 */
7715 hbflag = 1;
7716 asconf = 1;
7717 /*
7718 * should sysctl this: don't bundle data
7719 * with ASCONF since it requires AUTH
7720 */
7721 no_data_chunks = 1;
7722 chk->sent = SCTP_DATAGRAM_SENT;
7723 chk->snd_count++;
7724 if (mtu == 0) {
7725 /*
7726 * Ok we are out of room but we can
7727 * output without effecting the
7728 * flight size since this little guy
7729 * is a control only packet.
7730 */
7731 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7732 /*
7733 * do NOT clear the asconf flag as
7734 * it is used to do appropriate
7735 * source address selection.
7736 */
7737 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7738 (struct sockaddr *)&net->ro._l_addr,
7739 outchain, auth_offset, auth,
7740 stcb->asoc.authinfo.active_keyid,
7741 no_fragmentflg, 0, NULL, asconf,
7742 inp->sctp_lport, stcb->rport,
7743 htonl(stcb->asoc.peer_vtag),
7744 net->port, so_locked, NULL))) {
7745 if (error == ENOBUFS) {
7746 asoc->ifp_had_enobuf = 1;
7747 SCTP_STAT_INCR(sctps_lowlevelerr);
7748 }
7749 if (from_where == 0) {
7750 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7751 }
7752 if (*now_filled == 0) {
7753 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7754 *now_filled = 1;
7755 *now = net->last_sent_time;
7756 } else {
7757 net->last_sent_time = *now;
7758 }
7759 hbflag = 0;
7760 /* error, could not output */
7761 if (error == EHOSTUNREACH) {
7762 /*
7763 * Destination went
7764 * unreachable
7765 * during this send
7766 */
7767 sctp_move_to_an_alt(stcb, asoc, net);
7768 }
7769 *reason_code = 7;
7770 continue;
7771 } else
7772 asoc->ifp_had_enobuf = 0;
7773 if (*now_filled == 0) {
7774 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7775 *now_filled = 1;
7776 *now = net->last_sent_time;
7777 } else {
7778 net->last_sent_time = *now;
7779 }
7780 hbflag = 0;
7781 /*
7782 * increase the number we sent, if a
7783 * cookie is sent we don't tell them
7784 * any was sent out.
7785 */
7786 outchain = endoutchain = NULL;
7787 auth = NULL;
7788 auth_offset = 0;
7789 if (!no_out_cnt)
7790 *num_out += ctl_cnt;
7791 /* recalc a clean slate and setup */
7792 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7793 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7794 } else {
7795 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7796 }
7797 to_out = 0;
7798 no_fragmentflg = 1;
7799 }
7800 }
7801 }
7802 /************************/
7803 /* Control transmission */
7804 /************************/
7805 /* Now first lets go through the control queue */
7806 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7807 chk; chk = nchk) {
7808 nchk = TAILQ_NEXT(chk, sctp_next);
7809 if (chk->whoTo != net) {
7810 /*
7811 * No, not sent to the network we are
7812 * looking at
7813 */
7814 continue;
7815 }
7816 if (chk->data == NULL) {
7817 continue;
7818 }
7819 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7820 /*
7821 * It must be unsent. Cookies and ASCONF's
7822 * hang around but there timers will force
7823 * when marked for resend.
7824 */
7825 continue;
7826 }
7827 /*
7828 * if no AUTH is yet included and this chunk
7829 * requires it, make sure to account for it. We
7830 * don't apply the size until the AUTH chunk is
7831 * actually added below in case there is no room for
7832 * this chunk. NOTE: we overload the use of "omtu"
7833 * here
7834 */
7835 if ((auth == NULL) &&
7836 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7837 stcb->asoc.peer_auth_chunks)) {
7838 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7839 } else
7840 omtu = 0;
7841 /* Here we do NOT factor the r_mtu */
7842 if ((chk->send_size < (int)(mtu - omtu)) ||
7843 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7844 /*
7845 * We probably should glom the mbuf chain
7846 * from the chk->data for control but the
7847 * problem is it becomes yet one more level
7848 * of tracking to do if for some reason
7849 * output fails. Then I have got to
7850 * reconstruct the merged control chain.. el
7851 * yucko.. for now we take the easy way and
7852 * do the copy
7853 */
7854 /*
7855 * Add an AUTH chunk, if chunk requires it
7856 * save the offset into the chain for AUTH
7857 */
7858 if ((auth == NULL) &&
7859 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7860 stcb->asoc.peer_auth_chunks))) {
7861 outchain = sctp_add_auth_chunk(outchain,
7862 &endoutchain,
7863 &auth,
7864 &auth_offset,
7865 stcb,
7866 chk->rec.chunk_id.id);
7867 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7868 }
7869 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7870 (int)chk->rec.chunk_id.can_take_data,
7871 chk->send_size, chk->copy_by_ref);
7872 if (outchain == NULL) {
7873 *reason_code = 8;
7874 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7875 return (ENOMEM);
7876 }
7877 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7878 /* update our MTU size */
7879 if (mtu > (chk->send_size + omtu))
7880 mtu -= (chk->send_size + omtu);
7881 else
7882 mtu = 0;
7883 to_out += (chk->send_size + omtu);
7884 /* Do clear IP_DF ? */
7885 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7886 no_fragmentflg = 0;
7887 }
7888 if (chk->rec.chunk_id.can_take_data)
7889 chk->data = NULL;
7890 /* Mark things to be removed, if needed */
7891 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7892 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7893 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7894 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7895 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7896 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7897 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7898 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7899 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7900 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7901 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7902
7903 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7904 hbflag = 1;
7905 /*
7906 * JRS 5/14/07 - Set the
7907 * flag to say a heartbeat
7908 * is being sent.
7909 */
7910 pf_hbflag = 1;
7911 }
7912 /* remove these chunks at the end */
7913 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7914 /* turn off the timer */
7915 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7916 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7917 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7918 }
7919 }
7920 /*
7921 * EY -Nr-sack version of the above
7922 * if statement
7923 */
7924 if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
7925 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { /* EY !?! */
7926 /* turn off the timer */
7927 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7928 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7929 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7930 }
7931 }
7932 ctl_cnt++;
7933 } else {
7934 /*
7935 * Other chunks, since they have
7936 * timers running (i.e. COOKIE) we
7937 * just "trust" that it gets sent or
7938 * retransmitted.
7939 */
7940 ctl_cnt++;
7941 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7942 cookie = 1;
7943 no_out_cnt = 1;
7944 }
7945 chk->sent = SCTP_DATAGRAM_SENT;
7946 chk->snd_count++;
7947 }
7948 if (mtu == 0) {
7949 /*
7950 * Ok we are out of room but we can
7951 * output without effecting the
7952 * flight size since this little guy
7953 * is a control only packet.
7954 */
7955 if (asconf) {
7956 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7957 /*
7958 * do NOT clear the asconf
7959 * flag as it is used to do
7960 * appropriate source
7961 * address selection.
7962 */
7963 }
7964 if (cookie) {
7965 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7966 cookie = 0;
7967 }
7968 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7969 (struct sockaddr *)&net->ro._l_addr,
7970 outchain,
7971 auth_offset, auth,
7972 stcb->asoc.authinfo.active_keyid,
7973 no_fragmentflg, 0, NULL, asconf,
7974 inp->sctp_lport, stcb->rport,
7975 htonl(stcb->asoc.peer_vtag),
7976 net->port, so_locked, NULL))) {
7977 if (error == ENOBUFS) {
7978 asoc->ifp_had_enobuf = 1;
7979 SCTP_STAT_INCR(sctps_lowlevelerr);
7980 }
7981 if (from_where == 0) {
7982 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7983 }
7984 /* error, could not output */
7985 if (hbflag) {
7986 if (*now_filled == 0) {
7987 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7988 *now_filled = 1;
7989 *now = net->last_sent_time;
7990 } else {
7991 net->last_sent_time = *now;
7992 }
7993 hbflag = 0;
7994 }
7995 if (error == EHOSTUNREACH) {
7996 /*
7997 * Destination went
7998 * unreachable
7999 * during this send
8000 */
8001 sctp_move_to_an_alt(stcb, asoc, net);
8002 }
8003 *reason_code = 7;
8004 continue;
8005 } else
8006 asoc->ifp_had_enobuf = 0;
8007 /* Only HB or ASCONF advances time */
8008 if (hbflag) {
8009 if (*now_filled == 0) {
8010 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8011 *now_filled = 1;
8012 *now = net->last_sent_time;
8013 } else {
8014 net->last_sent_time = *now;
8015 }
8016 hbflag = 0;
8017 }
8018 /*
8019 * increase the number we sent, if a
8020 * cookie is sent we don't tell them
8021 * any was sent out.
8022 */
8023 outchain = endoutchain = NULL;
8024 auth = NULL;
8025 auth_offset = 0;
8026 if (!no_out_cnt)
8027 *num_out += ctl_cnt;
8028 /* recalc a clean slate and setup */
8029 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8030 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8031 } else {
8032 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8033 }
8034 to_out = 0;
8035 no_fragmentflg = 1;
8036 }
8037 }
8038 }
8039 /* JRI: if dest is in PF state, do not send data to it */
8040 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8041 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8042 (net->dest_state & SCTP_ADDR_PF)) {
8043 goto no_data_fill;
8044 }
8045 if (net->flight_size >= net->cwnd) {
8046 goto no_data_fill;
8047 }
8048 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
8049 (net->flight_size > max_rwnd_per_dest)) {
8050 goto no_data_fill;
8051 }
8052 /*********************/
8053 /* Data transmission */
8054 /*********************/
8055 /*
8056 * if AUTH for DATA is required and no AUTH has been added
8057 * yet, account for this in the mtu now... if no data can be
8058 * bundled, this adjustment won't matter anyways since the
8059 * packet will be going out...
8060 */
8061 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8062 stcb->asoc.peer_auth_chunks);
8063 if (data_auth_reqd && (auth == NULL)) {
8064 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8065 }
8066 /* now lets add any data within the MTU constraints */
8067 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8068 case AF_INET:
8069 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8070 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8071 else
8072 omtu = 0;
8073 break;
8074#ifdef INET6
8075 case AF_INET6:
8076 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8077 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8078 else
8079 omtu = 0;
8080 break;
8081#endif
8082 default:
8083 /* TSNH */
8084 omtu = 0;
8085 break;
8086 }
8087 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8088 (skip_data_for_this_net == 0)) ||
8089 (cookie)) {
8090 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8091 if (no_data_chunks) {
8092 /* let only control go out */
8093 *reason_code = 1;
8094 break;
8095 }
8096 if (net->flight_size >= net->cwnd) {
8097 /* skip this net, no room for data */
8098 *reason_code = 2;
8099 break;
8100 }
8101 nchk = TAILQ_NEXT(chk, sctp_next);
8102 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8103 if (chk->whoTo != net) {
8104 /*
8105 * For CMT, steal the data
8106 * to this network if its
8107 * not set here.
8108 */
8109 sctp_free_remote_addr(chk->whoTo);
8110 chk->whoTo = net;
8111 atomic_add_int(&chk->whoTo->ref_count, 1);
8112 }
8113 } else if (chk->whoTo != net) {
8114 /* No, not sent to this net */
8115 continue;
8116 }
8117 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8118 /*-
8119 * strange, we have a chunk that is
8120 * to big for its destination and
8121 * yet no fragment ok flag.
8122 * Something went wrong when the
8123 * PMTU changed...we did not mark
8124 * this chunk for some reason?? I
8125 * will fix it here by letting IP
8126 * fragment it for now and printing
8127 * a warning. This really should not
8128 * happen ...
8129 */
8130 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8131 chk->send_size, mtu);
8132 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8133 }
8134 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8135 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8136 struct sctp_data_chunk *dchkh;
8137
8138 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8139 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8140 }
8141 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8142 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8143 /* ok we will add this one */
8144
8145 /*
8146 * Add an AUTH chunk, if chunk
8147 * requires it, save the offset into
8148 * the chain for AUTH
8149 */
8150 if (data_auth_reqd) {
8151 if (auth == NULL) {
8152 outchain = sctp_add_auth_chunk(outchain,
8153 &endoutchain,
8154 &auth,
8155 &auth_offset,
8156 stcb,
8157 SCTP_DATA);
8158 auth_keyid = chk->auth_keyid;
8159 override_ok = 0;
8160 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8161 } else if (override_ok) {
8162 /*
8163 * use this data's
8164 * keyid
8165 */
8166 auth_keyid = chk->auth_keyid;
8167 override_ok = 0;
8168 } else if (auth_keyid != chk->auth_keyid) {
8169 /*
8170 * different keyid,
8171 * so done bundling
8172 */
8173 break;
8174 }
8175 }
8176 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8177 chk->send_size, chk->copy_by_ref);
8178 if (outchain == NULL) {
8179 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8180 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8181 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8182 }
8183 *reason_code = 3;
8184 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8185 return (ENOMEM);
8186 }
8187 /* upate our MTU size */
8188 /* Do clear IP_DF ? */
8189 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8190 no_fragmentflg = 0;
8191 }
8192 /* unsigned subtraction of mtu */
8193 if (mtu > chk->send_size)
8194 mtu -= chk->send_size;
8195 else
8196 mtu = 0;
8197 /* unsigned subtraction of r_mtu */
8198 if (r_mtu > chk->send_size)
8199 r_mtu -= chk->send_size;
8200 else
8201 r_mtu = 0;
8202
8203 to_out += chk->send_size;
8204 if ((to_out > mx_mtu) && no_fragmentflg) {
8205#ifdef INVARIANTS
8206 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8207#else
8208 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8209 mx_mtu, to_out);
8210#endif
8211 }
8212 chk->window_probe = 0;
8213 data_list[bundle_at++] = chk;
8214 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8215 mtu = 0;
8216 break;
8217 }
8218 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8219 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8220 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8221 } else {
8222 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8223 }
8224 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8225 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8226 /*
8227 * Count number of
8228 * user msg's that
8229 * were fragmented
8230 * we do this by
8231 * counting when we
8232 * see a LAST
8233 * fragment only.
8234 */
8235 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8236 }
8237 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8238 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8239 data_list[0]->window_probe = 1;
8240 net->window_probe = 1;
8241 }
8242 break;
8243 }
8244 } else {
8245 /*
8246 * Must be sent in order of the
8247 * TSN's (on a network)
8248 */
8249 break;
8250 }
8251 } /* for (chunk gather loop for this net) */
8252 } /* if asoc.state OPEN */
8253no_data_fill:
8254 /* Is there something to send for this destination? */
8255 if (outchain) {
8256 /* We may need to start a control timer or two */
8257 if (asconf) {
8258 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8259 stcb, net);
8260 /*
8261 * do NOT clear the asconf flag as it is
8262 * used to do appropriate source address
8263 * selection.
8264 */
8265 }
8266 if (cookie) {
8267 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8268 cookie = 0;
8269 }
8270 /* must start a send timer if data is being sent */
8271 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8272 /*
8273 * no timer running on this destination
8274 * restart it.
8275 */
8276 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8277 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8278 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8279 pf_hbflag &&
8280 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8281 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8282 /*
8283 * JRS 5/14/07 - If a HB has been sent to a
8284 * PF destination and no T3 timer is
8285 * currently running, start the T3 timer to
8286 * track the HBs that were sent.
8287 */
8288 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8289 }
8290 /* Now send it, if there is anything to send :> */
8291 if ((error = sctp_lowlevel_chunk_output(inp,
8292 stcb,
8293 net,
8294 (struct sockaddr *)&net->ro._l_addr,
8295 outchain,
8296 auth_offset,
8297 auth,
8298 auth_keyid,
8299 no_fragmentflg,
8300 bundle_at,
8301 data_list[0],
8302 asconf,
8303 inp->sctp_lport, stcb->rport,
8304 htonl(stcb->asoc.peer_vtag),
8305 net->port, so_locked, NULL))) {
8306 /* error, we could not output */
8307 if (error == ENOBUFS) {
8308 SCTP_STAT_INCR(sctps_lowlevelerr);
8309 asoc->ifp_had_enobuf = 1;
8310 }
8311 if (from_where == 0) {
8312 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8313 }
8314 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8315 if (hbflag) {
8316 if (*now_filled == 0) {
8317 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8318 *now_filled = 1;
8319 *now = net->last_sent_time;
8320 } else {
8321 net->last_sent_time = *now;
8322 }
8323 hbflag = 0;
8324 }
8325 if (error == EHOSTUNREACH) {
8326 /*
8327 * Destination went unreachable
8328 * during this send
8329 */
8330 sctp_move_to_an_alt(stcb, asoc, net);
8331 }
8332 *reason_code = 6;
8333 /*-
8334 * I add this line to be paranoid. As far as
8335 * I can tell the continue, takes us back to
8336 * the top of the for, but just to make sure
8337 * I will reset these again here.
8338 */
8339 ctl_cnt = bundle_at = 0;
8340 continue; /* This takes us back to the
8341 * for() for the nets. */
8342 } else {
8343 asoc->ifp_had_enobuf = 0;
8344 }
8345 outchain = endoutchain = NULL;
8346 auth = NULL;
8347 auth_offset = 0;
8348 if (bundle_at || hbflag) {
8349 /* For data/asconf and hb set time */
8350 if (*now_filled == 0) {
8351 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8352 *now_filled = 1;
8353 *now = net->last_sent_time;
8354 } else {
8355 net->last_sent_time = *now;
8356 }
8357 }
8358 if (!no_out_cnt) {
8359 *num_out += (ctl_cnt + bundle_at);
8360 }
8361 if (bundle_at) {
8362 /* setup for a RTO measurement */
8363 tsns_sent = data_list[0]->rec.data.TSN_seq;
8364 /* fill time if not already filled */
8365 if (*now_filled == 0) {
8366 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8367 *now_filled = 1;
8368 *now = asoc->time_last_sent;
8369 } else {
8370 asoc->time_last_sent = *now;
8371 }
8372 data_list[0]->do_rtt = 1;
8373 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8374 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8375 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8376 if (net->flight_size < net->cwnd) {
8377 /* start or restart it */
8378 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8379 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8380 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8381 }
8382 SCTP_STAT_INCR(sctps_earlyfrstrout);
8383 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8384 } else {
8385 /* stop it if its running */
8386 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8387 SCTP_STAT_INCR(sctps_earlyfrstpout);
8388 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8389 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8390 }
8391 }
8392 }
8393 }
8394 if (one_chunk) {
8395 break;
8396 }
8397 }
8398 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8399 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8400 }
8401 }
8402 if (old_start_at == NULL) {
8403 old_start_at = start_at;
8404 start_at = TAILQ_FIRST(&asoc->nets);
8405 if (old_start_at)
8406 goto again_one_more_time;
8407 }
8408 /*
8409 * At the end there should be no NON timed chunks hanging on this
8410 * queue.
8411 */
8412 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8413 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8414 }
8415 if ((*num_out == 0) && (*reason_code == 0)) {
8416 *reason_code = 4;
8417 } else {
8418 *reason_code = 5;
8419 }
8420 sctp_clean_up_ctl(stcb, asoc);
8421 return (0);
8422}
8423
8424void
8425sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8426{
8427 /*-
8428 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8429 * the control chunk queue.
8430 */
8431 struct sctp_chunkhdr *hdr;
8432 struct sctp_tmit_chunk *chk;
8433 struct mbuf *mat;
8434
8435 SCTP_TCB_LOCK_ASSERT(stcb);
8436 sctp_alloc_a_chunk(stcb, chk);
8437 if (chk == NULL) {
8438 /* no memory */
8439 sctp_m_freem(op_err);
8440 return;
8441 }
8442 chk->copy_by_ref = 0;
8443 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8444 if (op_err == NULL) {
8445 sctp_free_a_chunk(stcb, chk);
8446 return;
8447 }
8448 chk->send_size = 0;
8449 mat = op_err;
8450 while (mat != NULL) {
8451 chk->send_size += SCTP_BUF_LEN(mat);
8452 mat = SCTP_BUF_NEXT(mat);
8453 }
8454 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8455 chk->rec.chunk_id.can_take_data = 1;
8456 chk->sent = SCTP_DATAGRAM_UNSENT;
8457 chk->snd_count = 0;
8458 chk->flags = 0;
8459 chk->asoc = &stcb->asoc;
8460 chk->data = op_err;
8461 chk->whoTo = chk->asoc->primary_destination;
8462 atomic_add_int(&chk->whoTo->ref_count, 1);
8463 hdr = mtod(op_err, struct sctp_chunkhdr *);
8464 hdr->chunk_type = SCTP_OPERATION_ERROR;
8465 hdr->chunk_flags = 0;
8466 hdr->chunk_length = htons(chk->send_size);
8467 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8468 chk,
8469 sctp_next);
8470 chk->asoc->ctrl_queue_cnt++;
8471}
8472
8473int
8474sctp_send_cookie_echo(struct mbuf *m,
8475 int offset,
8476 struct sctp_tcb *stcb,
8477 struct sctp_nets *net)
8478{
8479 /*-
8480 * pull out the cookie and put it at the front of the control chunk
8481 * queue.
8482 */
8483 int at;
8484 struct mbuf *cookie;
8485 struct sctp_paramhdr parm, *phdr;
8486 struct sctp_chunkhdr *hdr;
8487 struct sctp_tmit_chunk *chk;
8488 uint16_t ptype, plen;
8489
8490 /* First find the cookie in the param area */
8491 cookie = NULL;
8492 at = offset + sizeof(struct sctp_init_chunk);
8493
8494 SCTP_TCB_LOCK_ASSERT(stcb);
8495 do {
8496 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8497 if (phdr == NULL) {
8498 return (-3);
8499 }
8500 ptype = ntohs(phdr->param_type);
8501 plen = ntohs(phdr->param_length);
8502 if (ptype == SCTP_STATE_COOKIE) {
8503 int pad;
8504
8505 /* found the cookie */
8506 if ((pad = (plen % 4))) {
8507 plen += 4 - pad;
8508 }
8509 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8510 if (cookie == NULL) {
8511 /* No memory */
8512 return (-2);
8513 }
8514#ifdef SCTP_MBUF_LOGGING
8515 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8516 struct mbuf *mat;
8517
8518 mat = cookie;
8519 while (mat) {
8520 if (SCTP_BUF_IS_EXTENDED(mat)) {
8521 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8522 }
8523 mat = SCTP_BUF_NEXT(mat);
8524 }
8525 }
8526#endif
8527 break;
8528 }
8529 at += SCTP_SIZE32(plen);
8530 } while (phdr);
8531 if (cookie == NULL) {
8532 /* Did not find the cookie */
8533 return (-3);
8534 }
8535 /* ok, we got the cookie lets change it into a cookie echo chunk */
8536
8537 /* first the change from param to cookie */
8538 hdr = mtod(cookie, struct sctp_chunkhdr *);
8539 hdr->chunk_type = SCTP_COOKIE_ECHO;
8540 hdr->chunk_flags = 0;
8541 /* get the chunk stuff now and place it in the FRONT of the queue */
8542 sctp_alloc_a_chunk(stcb, chk);
8543 if (chk == NULL) {
8544 /* no memory */
8545 sctp_m_freem(cookie);
8546 return (-5);
8547 }
8548 chk->copy_by_ref = 0;
8549 chk->send_size = plen;
8550 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8551 chk->rec.chunk_id.can_take_data = 0;
8552 chk->sent = SCTP_DATAGRAM_UNSENT;
8553 chk->snd_count = 0;
8554 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8555 chk->asoc = &stcb->asoc;
8556 chk->data = cookie;
8557 chk->whoTo = chk->asoc->primary_destination;
8558 atomic_add_int(&chk->whoTo->ref_count, 1);
8559 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8560 chk->asoc->ctrl_queue_cnt++;
8561 return (0);
8562}
8563
8564void
8565sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8566 struct mbuf *m,
8567 int offset,
8568 int chk_length,
8569 struct sctp_nets *net)
8570{
8571 /*
8572 * take a HB request and make it into a HB ack and send it.
8573 */
8574 struct mbuf *outchain;
8575 struct sctp_chunkhdr *chdr;
8576 struct sctp_tmit_chunk *chk;
8577
8578
8579 if (net == NULL)
8580 /* must have a net pointer */
8581 return;
8582
8583 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8584 if (outchain == NULL) {
8585 /* gak out of memory */
8586 return;
8587 }
8588#ifdef SCTP_MBUF_LOGGING
8589 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8590 struct mbuf *mat;
8591
8592 mat = outchain;
8593 while (mat) {
8594 if (SCTP_BUF_IS_EXTENDED(mat)) {
8595 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8596 }
8597 mat = SCTP_BUF_NEXT(mat);
8598 }
8599 }
8600#endif
8601 chdr = mtod(outchain, struct sctp_chunkhdr *);
8602 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8603 chdr->chunk_flags = 0;
8604 if (chk_length % 4) {
8605 /* need pad */
8606 uint32_t cpthis = 0;
8607 int padlen;
8608
8609 padlen = 4 - (chk_length % 4);
8610 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8611 }
8612 sctp_alloc_a_chunk(stcb, chk);
8613 if (chk == NULL) {
8614 /* no memory */
8615 sctp_m_freem(outchain);
8616 return;
8617 }
8618 chk->copy_by_ref = 0;
8619 chk->send_size = chk_length;
8620 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8621 chk->rec.chunk_id.can_take_data = 1;
8622 chk->sent = SCTP_DATAGRAM_UNSENT;
8623 chk->snd_count = 0;
8624 chk->flags = 0;
8625 chk->asoc = &stcb->asoc;
8626 chk->data = outchain;
8627 chk->whoTo = net;
8628 atomic_add_int(&chk->whoTo->ref_count, 1);
8629 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8630 chk->asoc->ctrl_queue_cnt++;
8631}
8632
8633void
8634sctp_send_cookie_ack(struct sctp_tcb *stcb)
8635{
8636 /* formulate and queue a cookie-ack back to sender */
8637 struct mbuf *cookie_ack;
8638 struct sctp_chunkhdr *hdr;
8639 struct sctp_tmit_chunk *chk;
8640
8641 cookie_ack = NULL;
8642 SCTP_TCB_LOCK_ASSERT(stcb);
8643
8644 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8645 if (cookie_ack == NULL) {
8646 /* no mbuf's */
8647 return;
8648 }
8649 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8650 sctp_alloc_a_chunk(stcb, chk);
8651 if (chk == NULL) {
8652 /* no memory */
8653 sctp_m_freem(cookie_ack);
8654 return;
8655 }
8656 chk->copy_by_ref = 0;
8657 chk->send_size = sizeof(struct sctp_chunkhdr);
8658 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8659 chk->rec.chunk_id.can_take_data = 1;
8660 chk->sent = SCTP_DATAGRAM_UNSENT;
8661 chk->snd_count = 0;
8662 chk->flags = 0;
8663 chk->asoc = &stcb->asoc;
8664 chk->data = cookie_ack;
8665 if (chk->asoc->last_control_chunk_from != NULL) {
8666 chk->whoTo = chk->asoc->last_control_chunk_from;
8667 } else {
8668 chk->whoTo = chk->asoc->primary_destination;
8669 }
8670 atomic_add_int(&chk->whoTo->ref_count, 1);
8671 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8672 hdr->chunk_type = SCTP_COOKIE_ACK;
8673 hdr->chunk_flags = 0;
8674 hdr->chunk_length = htons(chk->send_size);
8675 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8676 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8677 chk->asoc->ctrl_queue_cnt++;
8678 return;
8679}
8680
8681
8682void
8683sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8684{
8685 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8686 struct mbuf *m_shutdown_ack;
8687 struct sctp_shutdown_ack_chunk *ack_cp;
8688 struct sctp_tmit_chunk *chk;
8689
8690 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8691 if (m_shutdown_ack == NULL) {
8692 /* no mbuf's */
8693 return;
8694 }
8695 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8696 sctp_alloc_a_chunk(stcb, chk);
8697 if (chk == NULL) {
8698 /* no memory */
8699 sctp_m_freem(m_shutdown_ack);
8700 return;
8701 }
8702 chk->copy_by_ref = 0;
8703 chk->send_size = sizeof(struct sctp_chunkhdr);
8704 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8705 chk->rec.chunk_id.can_take_data = 1;
8706 chk->sent = SCTP_DATAGRAM_UNSENT;
8707 chk->snd_count = 0;
8708 chk->flags = 0;
8709 chk->asoc = &stcb->asoc;
8710 chk->data = m_shutdown_ack;
8711 chk->whoTo = net;
8712 atomic_add_int(&net->ref_count, 1);
8713
8714 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8715 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8716 ack_cp->ch.chunk_flags = 0;
8717 ack_cp->ch.chunk_length = htons(chk->send_size);
8718 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8719 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8720 chk->asoc->ctrl_queue_cnt++;
8721 return;
8722}
8723
8724void
8725sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8726{
8727 /* formulate and queue a SHUTDOWN to the sender */
8728 struct mbuf *m_shutdown;
8729 struct sctp_shutdown_chunk *shutdown_cp;
8730 struct sctp_tmit_chunk *chk;
8731
8732 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8733 if (m_shutdown == NULL) {
8734 /* no mbuf's */
8735 return;
8736 }
8737 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8738 sctp_alloc_a_chunk(stcb, chk);
8739 if (chk == NULL) {
8740 /* no memory */
8741 sctp_m_freem(m_shutdown);
8742 return;
8743 }
8744 chk->copy_by_ref = 0;
8745 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8746 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8747 chk->rec.chunk_id.can_take_data = 1;
8748 chk->sent = SCTP_DATAGRAM_UNSENT;
8749 chk->snd_count = 0;
8750 chk->flags = 0;
8751 chk->asoc = &stcb->asoc;
8752 chk->data = m_shutdown;
8753 chk->whoTo = net;
8754 atomic_add_int(&net->ref_count, 1);
8755
8756 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8757 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8758 shutdown_cp->ch.chunk_flags = 0;
8759 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8760 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8761 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8762 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8763 chk->asoc->ctrl_queue_cnt++;
8764 return;
8765}
8766
8767void
8768sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8769{
8770 /*
8771 * formulate and queue an ASCONF to the peer. ASCONF parameters
8772 * should be queued on the assoc queue.
8773 */
8774 struct sctp_tmit_chunk *chk;
8775 struct mbuf *m_asconf;
8776 int len;
8777
8778 SCTP_TCB_LOCK_ASSERT(stcb);
8779
8780 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8781 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8782 /* can't send a new one if there is one in flight already */
8783 return;
8784 }
8785 /* compose an ASCONF chunk, maximum length is PMTU */
8786 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8787 if (m_asconf == NULL) {
8788 return;
8789 }
8790 sctp_alloc_a_chunk(stcb, chk);
8791 if (chk == NULL) {
8792 /* no memory */
8793 sctp_m_freem(m_asconf);
8794 return;
8795 }
8796 chk->copy_by_ref = 0;
8797 chk->data = m_asconf;
8798 chk->send_size = len;
8799 chk->rec.chunk_id.id = SCTP_ASCONF;
8800 chk->rec.chunk_id.can_take_data = 0;
8801 chk->sent = SCTP_DATAGRAM_UNSENT;
8802 chk->snd_count = 0;
8803 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8804 chk->asoc = &stcb->asoc;
8805 chk->whoTo = net;
8806 atomic_add_int(&chk->whoTo->ref_count, 1);
8807 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8808 chk->asoc->ctrl_queue_cnt++;
8809 return;
8810}
8811
8812void
8813sctp_send_asconf_ack(struct sctp_tcb *stcb)
8814{
8815 /*
8816 * formulate and queue a asconf-ack back to sender. the asconf-ack
8817 * must be stored in the tcb.
8818 */
8819 struct sctp_tmit_chunk *chk;
8820 struct sctp_asconf_ack *ack, *latest_ack;
8821 struct mbuf *m_ack, *m;
8822 struct sctp_nets *net = NULL;
8823
8824 SCTP_TCB_LOCK_ASSERT(stcb);
8825 /* Get the latest ASCONF-ACK */
8826 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8827 if (latest_ack == NULL) {
8828 return;
8829 }
8830 if (latest_ack->last_sent_to != NULL &&
8831 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8832 /* we're doing a retransmission */
8833 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8834 if (net == NULL) {
8835 /* no alternate */
8836 if (stcb->asoc.last_control_chunk_from == NULL)
8837 net = stcb->asoc.primary_destination;
8838 else
8839 net = stcb->asoc.last_control_chunk_from;
8840 }
8841 } else {
8842 /* normal case */
8843 if (stcb->asoc.last_control_chunk_from == NULL)
8844 net = stcb->asoc.primary_destination;
8845 else
8846 net = stcb->asoc.last_control_chunk_from;
8847 }
8848 latest_ack->last_sent_to = net;
8849
8850 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8851 if (ack->data == NULL) {
8852 continue;
8853 }
8854 /* copy the asconf_ack */
8855 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8856 if (m_ack == NULL) {
8857 /* couldn't copy it */
8858 return;
8859 }
8860#ifdef SCTP_MBUF_LOGGING
8861 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8862 struct mbuf *mat;
8863
8864 mat = m_ack;
8865 while (mat) {
8866 if (SCTP_BUF_IS_EXTENDED(mat)) {
8867 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8868 }
8869 mat = SCTP_BUF_NEXT(mat);
8870 }
8871 }
8872#endif
8873
8874 sctp_alloc_a_chunk(stcb, chk);
8875 if (chk == NULL) {
8876 /* no memory */
8877 if (m_ack)
8878 sctp_m_freem(m_ack);
8879 return;
8880 }
8881 chk->copy_by_ref = 0;
8882
8883 chk->whoTo = net;
8884 chk->data = m_ack;
8885 chk->send_size = 0;
8886 /* Get size */
8887 m = m_ack;
8888 chk->send_size = ack->len;
8889 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8890 chk->rec.chunk_id.can_take_data = 1;
8891 chk->sent = SCTP_DATAGRAM_UNSENT;
8892 chk->snd_count = 0;
8893 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8894 chk->asoc = &stcb->asoc;
8895 atomic_add_int(&chk->whoTo->ref_count, 1);
8896
8897 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8898 chk->asoc->ctrl_queue_cnt++;
8899 }
8900 return;
8901}
8902
8903
8904static int
8905sctp_chunk_retransmission(struct sctp_inpcb *inp,
8906 struct sctp_tcb *stcb,
8907 struct sctp_association *asoc,
8908 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8909#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8910 SCTP_UNUSED
8911#endif
8912)
8913{
8914 /*-
8915 * send out one MTU of retransmission. If fast_retransmit is
8916 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8917 * rwnd. For a Cookie or Asconf in the control chunk queue we
8918 * retransmit them by themselves.
8919 *
8920 * For data chunks we will pick out the lowest TSN's in the sent_queue
8921 * marked for resend and bundle them all together (up to a MTU of
8922 * destination). The address to send to should have been
8923 * selected/changed where the retransmission was marked (i.e. in FR
8924 * or t3-timeout routines).
8925 */
8926 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8927 struct sctp_tmit_chunk *chk, *fwd;
8928 struct mbuf *m, *endofchain;
8929 struct sctp_nets *net = NULL;
8930 uint32_t tsns_sent = 0;
8931 int no_fragmentflg, bundle_at, cnt_thru;
8932 unsigned int mtu;
8933 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8934 struct sctp_auth_chunk *auth = NULL;
8935 uint32_t auth_offset = 0;
8936 uint16_t auth_keyid;
8937 int override_ok = 1;
8938 int data_auth_reqd = 0;
8939 uint32_t dmtu = 0;
8940
8941 SCTP_TCB_LOCK_ASSERT(stcb);
8942 tmr_started = ctl_cnt = bundle_at = error = 0;
8943 no_fragmentflg = 1;
8944 fwd_tsn = 0;
8945 *cnt_out = 0;
8946 fwd = NULL;
8947 endofchain = m = NULL;
8948 auth_keyid = stcb->asoc.authinfo.active_keyid;
8949#ifdef SCTP_AUDITING_ENABLED
8950 sctp_audit_log(0xC3, 1);
8951#endif
8952 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8953 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8954 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8955 asoc->sent_queue_retran_cnt);
8956 asoc->sent_queue_cnt = 0;
8957 asoc->sent_queue_cnt_removeable = 0;
8958 /* send back 0/0 so we enter normal transmission */
8959 *cnt_out = 0;
8960 return (0);
8961 }
8962 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8963 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8964 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8965 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8966 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8967 if (chk != asoc->str_reset) {
8968 /*
8969 * not eligible for retran if its
8970 * not ours
8971 */
8972 continue;
8973 }
8974 }
8975 ctl_cnt++;
8976 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8977 fwd_tsn = 1;
8978 fwd = chk;
8979 }
8980 /*
8981 * Add an AUTH chunk, if chunk requires it save the
8982 * offset into the chain for AUTH
8983 */
8984 if ((auth == NULL) &&
8985 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8986 stcb->asoc.peer_auth_chunks))) {
8987 m = sctp_add_auth_chunk(m, &endofchain,
8988 &auth, &auth_offset,
8989 stcb,
8990 chk->rec.chunk_id.id);
8991 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8992 }
8993 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8994 break;
8995 }
8996 }
8997 one_chunk = 0;
8998 cnt_thru = 0;
8999 /* do we have control chunks to retransmit? */
9000 if (m != NULL) {
9001 /* Start a timer no matter if we suceed or fail */
9002 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9003 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9004 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9005 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9006 chk->snd_count++; /* update our count */
9007 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9008 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9009 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9010 no_fragmentflg, 0, NULL, 0,
9011 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9012 chk->whoTo->port, so_locked, NULL))) {
9013 SCTP_STAT_INCR(sctps_lowlevelerr);
9014 return (error);
9015 }
9016 m = endofchain = NULL;
9017 auth = NULL;
9018 auth_offset = 0;
9019 /*
9020 * We don't want to mark the net->sent time here since this
9021 * we use this for HB and retrans cannot measure RTT
9022 */
9023 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9024 *cnt_out += 1;
9025 chk->sent = SCTP_DATAGRAM_SENT;
9026 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9027 if (fwd_tsn == 0) {
9028 return (0);
9029 } else {
9030 /* Clean up the fwd-tsn list */
9031 sctp_clean_up_ctl(stcb, asoc);
9032 return (0);
9033 }
9034 }
9035 /*
9036 * Ok, it is just data retransmission we need to do or that and a
9037 * fwd-tsn with it all.
9038 */
9039 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9040 return (SCTP_RETRAN_DONE);
9041 }
9042 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9043 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9044 /* not yet open, resend the cookie and that is it */
9045 return (1);
9046 }
9047#ifdef SCTP_AUDITING_ENABLED
9048 sctp_auditing(20, inp, stcb, NULL);
9049#endif
9050 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9051 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9052 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9053 /* No, not sent to this net or not ready for rtx */
9054 continue;
9055 }
9056 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9057 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9058 /* Gak, we have exceeded max unlucky retran, abort! */
9059 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9060 chk->snd_count,
9061 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9062 atomic_add_int(&stcb->asoc.refcnt, 1);
9063 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9064 SCTP_TCB_LOCK(stcb);
9065 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9066 return (SCTP_RETRAN_EXIT);
9067 }
9068 /* pick up the net */
9069 net = chk->whoTo;
9070 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9071 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9072 } else {
9073 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9074 }
9075
9076 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9077 /* No room in peers rwnd */
9078 uint32_t tsn;
9079
9080 tsn = asoc->last_acked_seq + 1;
9081 if (tsn == chk->rec.data.TSN_seq) {
9082 /*
9083 * we make a special exception for this
9084 * case. The peer has no rwnd but is missing
9085 * the lowest chunk.. which is probably what
9086 * is holding up the rwnd.
9087 */
9088 goto one_chunk_around;
9089 }
9090 return (1);
9091 }
9092one_chunk_around:
9093 if (asoc->peers_rwnd < mtu) {
9094 one_chunk = 1;
9095 if ((asoc->peers_rwnd == 0) &&
9096 (asoc->total_flight == 0)) {
9097 chk->window_probe = 1;
9098 chk->whoTo->window_probe = 1;
9099 }
9100 }
9101#ifdef SCTP_AUDITING_ENABLED
9102 sctp_audit_log(0xC3, 2);
9103#endif
9104 bundle_at = 0;
9105 m = NULL;
9106 net->fast_retran_ip = 0;
9107 if (chk->rec.data.doing_fast_retransmit == 0) {
9108 /*
9109 * if no FR in progress skip destination that have
9110 * flight_size > cwnd.
9111 */
9112 if (net->flight_size >= net->cwnd) {
9113 continue;
9114 }
9115 } else {
9116 /*
9117 * Mark the destination net to have FR recovery
9118 * limits put on it.
9119 */
9120 *fr_done = 1;
9121 net->fast_retran_ip = 1;
9122 }
9123
9124 /*
9125 * if no AUTH is yet included and this chunk requires it,
9126 * make sure to account for it. We don't apply the size
9127 * until the AUTH chunk is actually added below in case
9128 * there is no room for this chunk.
9129 */
9130 if (data_auth_reqd && (auth == NULL)) {
9131 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9132 } else
9133 dmtu = 0;
9134
9135 if ((chk->send_size <= (mtu - dmtu)) ||
9136 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9137 /* ok we will add this one */
9138 if (data_auth_reqd) {
9139 if (auth == NULL) {
9140 m = sctp_add_auth_chunk(m,
9141 &endofchain,
9142 &auth,
9143 &auth_offset,
9144 stcb,
9145 SCTP_DATA);
9146 auth_keyid = chk->auth_keyid;
9147 override_ok = 0;
9148 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9149 } else if (override_ok) {
9150 auth_keyid = chk->auth_keyid;
9151 override_ok = 0;
9152 } else if (chk->auth_keyid != auth_keyid) {
9153 /* different keyid, so done bundling */
9154 break;
9155 }
9156 }
9157 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9158 if (m == NULL) {
9159 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9160 return (ENOMEM);
9161 }
9162 /* Do clear IP_DF ? */
9163 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9164 no_fragmentflg = 0;
9165 }
9166 /* upate our MTU size */
9167 if (mtu > (chk->send_size + dmtu))
9168 mtu -= (chk->send_size + dmtu);
9169 else
9170 mtu = 0;
9171 data_list[bundle_at++] = chk;
9172 if (one_chunk && (asoc->total_flight <= 0)) {
9173 SCTP_STAT_INCR(sctps_windowprobed);
9174 }
9175 }
9176 if (one_chunk == 0) {
9177 /*
9178 * now are there anymore forward from chk to pick
9179 * up?
9180 */
9181 fwd = TAILQ_NEXT(chk, sctp_next);
9182 while (fwd) {
9183 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9184 /* Nope, not for retran */
9185 fwd = TAILQ_NEXT(fwd, sctp_next);
9186 continue;
9187 }
9188 if (fwd->whoTo != net) {
9189 /* Nope, not the net in question */
9190 fwd = TAILQ_NEXT(fwd, sctp_next);
9191 continue;
9192 }
9193 if (data_auth_reqd && (auth == NULL)) {
9194 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9195 } else
9196 dmtu = 0;
9197 if (fwd->send_size <= (mtu - dmtu)) {
9198 if (data_auth_reqd) {
9199 if (auth == NULL) {
9200 m = sctp_add_auth_chunk(m,
9201 &endofchain,
9202 &auth,
9203 &auth_offset,
9204 stcb,
9205 SCTP_DATA);
9206 auth_keyid = fwd->auth_keyid;
9207 override_ok = 0;
9208 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9209 } else if (override_ok) {
9210 auth_keyid = fwd->auth_keyid;
9211 override_ok = 0;
9212 } else if (fwd->auth_keyid != auth_keyid) {
9213 /*
9214 * different keyid,
9215 * so done bundling
9216 */
9217 break;
9218 }
9219 }
9220 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9221 if (m == NULL) {
9222 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9223 return (ENOMEM);
9224 }
9225 /* Do clear IP_DF ? */
9226 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9227 no_fragmentflg = 0;
9228 }
9229 /* upate our MTU size */
9230 if (mtu > (fwd->send_size + dmtu))
9231 mtu -= (fwd->send_size + dmtu);
9232 else
9233 mtu = 0;
9234 data_list[bundle_at++] = fwd;
9235 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9236 break;
9237 }
9238 fwd = TAILQ_NEXT(fwd, sctp_next);
9239 } else {
9240 /* can't fit so we are done */
9241 break;
9242 }
9243 }
9244 }
9245 /* Is there something to send for this destination? */
9246 if (m) {
9247 /*
9248 * No matter if we fail/or suceed we should start a
9249 * timer. A failure is like a lost IP packet :-)
9250 */
9251 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9252 /*
9253 * no timer running on this destination
9254 * restart it.
9255 */
9256 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9257 tmr_started = 1;
9258 }
9259 /* Now lets send it, if there is anything to send :> */
9260 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9261 (struct sockaddr *)&net->ro._l_addr, m,
9262 auth_offset, auth, auth_keyid,
9263 no_fragmentflg, 0, NULL, 0,
9264 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9265 net->port, so_locked, NULL))) {
9266 /* error, we could not output */
9267 SCTP_STAT_INCR(sctps_lowlevelerr);
9268 return (error);
9269 }
9270 m = endofchain = NULL;
9271 auth = NULL;
9272 auth_offset = 0;
9273 /* For HB's */
9274 /*
9275 * We don't want to mark the net->sent time here
9276 * since this we use this for HB and retrans cannot
9277 * measure RTT
9278 */
9279 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9280
9281 /* For auto-close */
9282 cnt_thru++;
9283 if (*now_filled == 0) {
9284 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9285 *now = asoc->time_last_sent;
9286 *now_filled = 1;
9287 } else {
9288 asoc->time_last_sent = *now;
9289 }
9290 *cnt_out += bundle_at;
9291#ifdef SCTP_AUDITING_ENABLED
9292 sctp_audit_log(0xC4, bundle_at);
9293#endif
9294 if (bundle_at) {
9295 tsns_sent = data_list[0]->rec.data.TSN_seq;
9296 }
9297 for (i = 0; i < bundle_at; i++) {
9298 SCTP_STAT_INCR(sctps_sendretransdata);
9299 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9300 /*
9301 * When we have a revoked data, and we
9302 * retransmit it, then we clear the revoked
9303 * flag since this flag dictates if we
9304 * subtracted from the fs
9305 */
9306 if (data_list[i]->rec.data.chunk_was_revoked) {
9307 /* Deflate the cwnd */
9308 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9309 data_list[i]->rec.data.chunk_was_revoked = 0;
9310 }
9311 data_list[i]->snd_count++;
9312 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9313 /* record the time */
9314 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9315 if (data_list[i]->book_size_scale) {
9316 /*
9317 * need to double the book size on
9318 * this one
9319 */
9320 data_list[i]->book_size_scale = 0;
9321 /*
9322 * Since we double the booksize, we
9323 * must also double the output queue
9324 * size, since this get shrunk when
9325 * we free by this amount.
9326 */
9327 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9328 data_list[i]->book_size *= 2;
9329
9330
9331 } else {
9332 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9333 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9334 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9335 }
9336 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9337 (uint32_t) (data_list[i]->send_size +
9338 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9339 }
9340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9341 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9342 data_list[i]->whoTo->flight_size,
9343 data_list[i]->book_size,
9344 (uintptr_t) data_list[i]->whoTo,
9345 data_list[i]->rec.data.TSN_seq);
9346 }
9347 sctp_flight_size_increase(data_list[i]);
9348 sctp_total_flight_increase(stcb, data_list[i]);
9349 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9350 /* SWS sender side engages */
9351 asoc->peers_rwnd = 0;
9352 }
9353 if ((i == 0) &&
9354 (data_list[i]->rec.data.doing_fast_retransmit)) {
9355 SCTP_STAT_INCR(sctps_sendfastretrans);
9356 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9357 (tmr_started == 0)) {
9358 /*-
9359 * ok we just fast-retrans'd
9360 * the lowest TSN, i.e the
9361 * first on the list. In
9362 * this case we want to give
9363 * some more time to get a
9364 * SACK back without a
9365 * t3-expiring.
9366 */
9367 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9368 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9369 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9370 }
9371 }
9372 }
9373 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9374 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9375 }
9376#ifdef SCTP_AUDITING_ENABLED
9377 sctp_auditing(21, inp, stcb, NULL);
9378#endif
9379 } else {
9380 /* None will fit */
9381 return (1);
9382 }
9383 if (asoc->sent_queue_retran_cnt <= 0) {
9384 /* all done we have no more to retran */
9385 asoc->sent_queue_retran_cnt = 0;
9386 break;
9387 }
9388 if (one_chunk) {
9389 /* No more room in rwnd */
9390 return (1);
9391 }
9392 /* stop the for loop here. we sent out a packet */
9393 break;
9394 }
9395 return (0);
9396}
9397
9398
9399static int
9400sctp_timer_validation(struct sctp_inpcb *inp,
9401 struct sctp_tcb *stcb,
9402 struct sctp_association *asoc,
9403 int ret)
9404{
9405 struct sctp_nets *net;
9406
9407 /* Validate that a timer is running somewhere */
9408 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9409 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9410 /* Here is a timer */
9411 return (ret);
9412 }
9413 }
9414 SCTP_TCB_LOCK_ASSERT(stcb);
9415 /* Gak, we did not have a timer somewhere */
9416 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9417 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9418 return (ret);
9419}
9420
9421void
9422sctp_chunk_output(struct sctp_inpcb *inp,
9423 struct sctp_tcb *stcb,
9424 int from_where,
9425 int so_locked
9426#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9427 SCTP_UNUSED
9428#endif
9429)
9430{
9431 /*-
9432 * Ok this is the generic chunk service queue. we must do the
9433 * following:
9434 * - See if there are retransmits pending, if so we must
9435 * do these first.
9436 * - Service the stream queue that is next, moving any
9437 * message (note I must get a complete message i.e.
9438 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9439 * TSN's
9440 * - Check to see if the cwnd/rwnd allows any output, if so we
9441 * go ahead and fomulate and send the low level chunks. Making sure
9442 * to combine any control in the control chunk queue also.
9443 */
9444 struct sctp_association *asoc;
9445 struct sctp_nets *net;
9446 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9447 burst_cnt = 0, burst_limit = 0;
9448 struct timeval now;
9449 int now_filled = 0;
9450 int nagle_on = 0;
9451 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9452 int un_sent = 0;
9453 int fr_done, tot_frs = 0;
9454
9455 asoc = &stcb->asoc;
9456 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9457 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9458 nagle_on = 0;
9459 } else {
9460 nagle_on = 1;
9461 }
9462 }
9463 SCTP_TCB_LOCK_ASSERT(stcb);
9464
9465 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9466
9467 if ((un_sent <= 0) &&
9468 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9469 (asoc->sent_queue_retran_cnt == 0)) {
9470 /* Nothing to do unless there is something to be sent left */
9471 return;
9472 }
9473 /*
9474 * Do we have something to send, data or control AND a sack timer
9475 * running, if so piggy-back the sack.
9476 */
9477 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9478 /*
9479 * EY if nr_sacks used then send an nr-sack , a sack
9480 * otherwise
9481 */
9482 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
9483 sctp_send_nr_sack(stcb);
9484 else
9485 sctp_send_sack(stcb);
9486 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9487 }
9488 while (asoc->sent_queue_retran_cnt) {
9489 /*-
9490 * Ok, it is retransmission time only, we send out only ONE
9491 * packet with a single call off to the retran code.
9492 */
9493 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9494 /*-
9495 * Special hook for handling cookiess discarded
9496 * by peer that carried data. Send cookie-ack only
9497 * and then the next call with get the retran's.
9498 */
9499 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9500 from_where,
9501 &now, &now_filled, frag_point, so_locked);
9502 return;
9503 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9504 /* if its not from a HB then do it */
9505 fr_done = 0;
9506 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9507 if (fr_done) {
9508 tot_frs++;
9509 }
9510 } else {
9511 /*
9512 * its from any other place, we don't allow retran
9513 * output (only control)
9514 */
9515 ret = 1;
9516 }
9517 if (ret > 0) {
9518 /* Can't send anymore */
9519 /*-
9520 * now lets push out control by calling med-level
9521 * output once. this assures that we WILL send HB's
9522 * if queued too.
9523 */
9524 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9525 from_where,
9526 &now, &now_filled, frag_point, so_locked);
9527#ifdef SCTP_AUDITING_ENABLED
9528 sctp_auditing(8, inp, stcb, NULL);
9529#endif
9530 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9531 return;
9532 }
9533 if (ret < 0) {
9534 /*-
9535 * The count was off.. retran is not happening so do
9536 * the normal retransmission.
9537 */
9538#ifdef SCTP_AUDITING_ENABLED
9539 sctp_auditing(9, inp, stcb, NULL);
9540#endif
9541 if (ret == SCTP_RETRAN_EXIT) {
9542 return;
9543 }
9544 break;
9545 }
9546 if (from_where == SCTP_OUTPUT_FROM_T3) {
9547 /* Only one transmission allowed out of a timeout */
9548#ifdef SCTP_AUDITING_ENABLED
9549 sctp_auditing(10, inp, stcb, NULL);
9550#endif
9551 /* Push out any control */
9552 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9553 &now, &now_filled, frag_point, so_locked);
9554 return;
9555 }
9556 if (tot_frs > asoc->max_burst) {
9557 /* Hit FR burst limit */
9558 return;
9559 }
9560 if ((num_out == 0) && (ret == 0)) {
9561
9562 /* No more retrans to send */
9563 break;
9564 }
9565 }
9566#ifdef SCTP_AUDITING_ENABLED
9567 sctp_auditing(12, inp, stcb, NULL);
9568#endif
9569 /* Check for bad destinations, if they exist move chunks around. */
9570 burst_limit = asoc->max_burst;
9571 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9572 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9573 SCTP_ADDR_NOT_REACHABLE) {
9574 /*-
9575 * if possible move things off of this address we
9576 * still may send below due to the dormant state but
9577 * we try to find an alternate address to send to
9578 * and if we have one we move all queued data on the
9579 * out wheel to this alternate address.
9580 */
9581 if (net->ref_count > 1)
9582 sctp_move_to_an_alt(stcb, asoc, net);
9583 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9584 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9585 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9586 /*
9587 * JRS 5/14/07 - If CMT PF is on and the current
9588 * destination is in PF state, move all queued data
9589 * to an alternate desination.
9590 */
9591 if (net->ref_count > 1)
9592 sctp_move_to_an_alt(stcb, asoc, net);
9593 } else {
9594 /*-
9595 * if ((asoc->sat_network) || (net->addr_is_local))
9596 * { burst_limit = asoc->max_burst *
9597 * SCTP_SAT_NETWORK_BURST_INCR; }
9598 */
9599 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9600 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9601 /*
9602 * JRS - Use the congestion control
9603 * given in the congestion control
9604 * module
9605 */
9606 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9607 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9608 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9609 }
9610 SCTP_STAT_INCR(sctps_maxburstqueued);
9611 }
9612 net->fast_retran_ip = 0;
9613 } else {
9614 if (net->flight_size == 0) {
9615 /* Should be decaying the cwnd here */
9616 ;
9617 }
9618 }
9619 }
9620
9621 }
9622 burst_cnt = 0;
9623 do {
9624 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9625 &reason_code, 0, from_where,
9626 &now, &now_filled, frag_point, so_locked);
9627 if (error) {
9628 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9629 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9630 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9631 }
9632 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9633 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9634 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9635 }
9636 break;
9637 }
9638 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9639
9640 tot_out += num_out;
9641 burst_cnt++;
9642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9643 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9644 if (num_out == 0) {
9645 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9646 }
9647 }
9648 if (nagle_on) {
9649 /*-
9650 * When nagle is on, we look at how much is un_sent, then
9651 * if its smaller than an MTU and we have data in
9652 * flight we stop.
9653 */
9654 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9655 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9656 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9657 (stcb->asoc.total_flight > 0)) {
9658 break;
9659 }
9660 }
9661 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9662 TAILQ_EMPTY(&asoc->send_queue) &&
9663 TAILQ_EMPTY(&asoc->out_wheel)) {
9664 /* Nothing left to send */
9665 break;
9666 }
9667 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9668 /* Nothing left to send */
9669 break;
9670 }
9671 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9672 (burst_cnt < burst_limit)));
9673
9674 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9675 if (burst_cnt >= burst_limit) {
9676 SCTP_STAT_INCR(sctps_maxburstqueued);
9677 asoc->burst_limit_applied = 1;
9678 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9679 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9680 }
9681 } else {
9682 asoc->burst_limit_applied = 0;
9683 }
9684 }
9685 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9686 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9687 }
9688 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9689 tot_out);
9690
9691 /*-
9692 * Now we need to clean up the control chunk chain if a ECNE is on
9693 * it. It must be marked as UNSENT again so next call will continue
9694 * to send it until such time that we get a CWR, to remove it.
9695 */
9696 if (stcb->asoc.ecn_echo_cnt_onq)
9697 sctp_fix_ecn_echo(asoc);
9698 return;
9699}
9700
9701
9702int
9703sctp_output(inp, m, addr, control, p, flags)
9704 struct sctp_inpcb *inp;
9705 struct mbuf *m;
9706 struct sockaddr *addr;
9707 struct mbuf *control;
9708 struct thread *p;
9709 int flags;
9710{
9711 if (inp == NULL) {
9712 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9713 return (EINVAL);
9714 }
9715 if (inp->sctp_socket == NULL) {
9716 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9717 return (EINVAL);
9718 }
9719 return (sctp_sosend(inp->sctp_socket,
9720 addr,
9721 (struct uio *)NULL,
9722 m,
9723 control,
9724 flags, p
9725 ));
9726}
9727
9728void
9729send_forward_tsn(struct sctp_tcb *stcb,
9730 struct sctp_association *asoc)
9731{
9732 struct sctp_tmit_chunk *chk;
9733 struct sctp_forward_tsn_chunk *fwdtsn;
9734 uint32_t advance_peer_ack_point;
9735
9736 SCTP_TCB_LOCK_ASSERT(stcb);
9737 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9738 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9739 /* mark it to unsent */
9740 chk->sent = SCTP_DATAGRAM_UNSENT;
9741 chk->snd_count = 0;
9742 /* Do we correct its output location? */
9743 if (chk->whoTo != asoc->primary_destination) {
9744 sctp_free_remote_addr(chk->whoTo);
9745 chk->whoTo = asoc->primary_destination;
9746 atomic_add_int(&chk->whoTo->ref_count, 1);
9747 }
9748 goto sctp_fill_in_rest;
9749 }
9750 }
9751 /* Ok if we reach here we must build one */
9752 sctp_alloc_a_chunk(stcb, chk);
9753 if (chk == NULL) {
9754 return;
9755 }
9756 chk->copy_by_ref = 0;
9757 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9758 chk->rec.chunk_id.can_take_data = 0;
9759 chk->asoc = asoc;
9760 chk->whoTo = NULL;
9761
9762 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9763 if (chk->data == NULL) {
9764 sctp_free_a_chunk(stcb, chk);
9765 return;
9766 }
9767 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9768 chk->sent = SCTP_DATAGRAM_UNSENT;
9769 chk->snd_count = 0;
9770 chk->whoTo = asoc->primary_destination;
9771 atomic_add_int(&chk->whoTo->ref_count, 1);
9772 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9773 asoc->ctrl_queue_cnt++;
9774sctp_fill_in_rest:
9775 /*-
9776 * Here we go through and fill out the part that deals with
9777 * stream/seq of the ones we skip.
9778 */
9779 SCTP_BUF_LEN(chk->data) = 0;
9780 {
9781 struct sctp_tmit_chunk *at, *tp1, *last;
9782 struct sctp_strseq *strseq;
9783 unsigned int cnt_of_space, i, ovh;
9784 unsigned int space_needed;
9785 unsigned int cnt_of_skipped = 0;
9786
9787 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9788 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9789 /* no more to look at */
9790 break;
9791 }
9792 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9793 /* We don't report these */
9794 continue;
9795 }
9796 cnt_of_skipped++;
9797 }
9798 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9799 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9800
9801 cnt_of_space = M_TRAILINGSPACE(chk->data);
9802
9803 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9804 ovh = SCTP_MIN_OVERHEAD;
9805 } else {
9806 ovh = SCTP_MIN_V4_OVERHEAD;
9807 }
9808 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9809 /* trim to a mtu size */
9810 cnt_of_space = asoc->smallest_mtu - ovh;
9811 }
9812 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9813 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9814 0xff, 0, cnt_of_skipped,
9815 asoc->advanced_peer_ack_point);
9816
9817 }
9818 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9819 if (cnt_of_space < space_needed) {
9820 /*-
9821 * ok we must trim down the chunk by lowering the
9822 * advance peer ack point.
9823 */
9824 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9825 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9826 0xff, 0xff, cnt_of_space,
9827 space_needed);
9828 }
9829 cnt_of_skipped = (cnt_of_space -
9830 ((sizeof(struct sctp_forward_tsn_chunk)) /
9831 sizeof(struct sctp_strseq)));
9832 /*-
9833 * Go through and find the TSN that will be the one
9834 * we report.
9835 */
9836 at = TAILQ_FIRST(&asoc->sent_queue);
9837 for (i = 0; i < cnt_of_skipped; i++) {
9838 tp1 = TAILQ_NEXT(at, sctp_next);
9839 at = tp1;
9840 }
9841 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9842 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9843 0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9844 asoc->advanced_peer_ack_point);
9845 }
9846 last = at;
9847 /*-
9848 * last now points to last one I can report, update
9849 * peer ack point
9850 */
9851 advance_peer_ack_point = last->rec.data.TSN_seq;
9852 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9853 }
9854 chk->send_size = space_needed;
9855 /* Setup the chunk */
9856 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9857 fwdtsn->ch.chunk_length = htons(chk->send_size);
9858 fwdtsn->ch.chunk_flags = 0;
9859 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9860 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9861 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9862 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9863 SCTP_BUF_LEN(chk->data) = chk->send_size;
9864 fwdtsn++;
9865 /*-
9866 * Move pointer to after the fwdtsn and transfer to the
9867 * strseq pointer.
9868 */
9869 strseq = (struct sctp_strseq *)fwdtsn;
9870 /*-
9871 * Now populate the strseq list. This is done blindly
9872 * without pulling out duplicate stream info. This is
9873 * inefficent but won't harm the process since the peer will
9874 * look at these in sequence and will thus release anything.
9875 * It could mean we exceed the PMTU and chop off some that
9876 * we could have included.. but this is unlikely (aka 1432/4
9877 * would mean 300+ stream seq's would have to be reported in
9878 * one FWD-TSN. With a bit of work we can later FIX this to
9879 * optimize and pull out duplcates.. but it does add more
9880 * overhead. So for now... not!
9881 */
9882 at = TAILQ_FIRST(&asoc->sent_queue);
9883 for (i = 0; i < cnt_of_skipped; i++) {
9884 tp1 = TAILQ_NEXT(at, sctp_next);
9885 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9886 /* We don't report these */
9887 i--;
9888 at = tp1;
9889 continue;
9890 }
9891 if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9892 at->rec.data.fwd_tsn_cnt = 0;
9893 }
9894 strseq->stream = ntohs(at->rec.data.stream_number);
9895 strseq->sequence = ntohs(at->rec.data.stream_seq);
9896 strseq++;
9897 at = tp1;
9898 }
9899 }
9900 return;
9901
9902}
9903
9904void
9905sctp_send_sack(struct sctp_tcb *stcb)
9906{
9907 /*-
9908 * Queue up a SACK in the control queue. We must first check to see
9909 * if a SACK is somehow on the control queue. If so, we will take
9910 * and and remove the old one.
9911 */
9912 struct sctp_association *asoc;
9913 struct sctp_tmit_chunk *chk, *a_chk;
9914 struct sctp_sack_chunk *sack;
9915 struct sctp_gap_ack_block *gap_descriptor;
9916 struct sack_track *selector;
9917 int mergeable = 0;
9918 int offset;
9919 caddr_t limit;
9920 uint32_t *dup;
9921 int limit_reached = 0;
9922 unsigned int i, jstart, siz, j;
9923 unsigned int num_gap_blocks = 0, space;
9924 int num_dups = 0;
9925 int space_req;
9926
9927 a_chk = NULL;
9928 asoc = &stcb->asoc;
9929 SCTP_TCB_LOCK_ASSERT(stcb);
9930 if (asoc->last_data_chunk_from == NULL) {
9931 /* Hmm we never received anything */
9932 return;
9933 }
9934 sctp_set_rwnd(stcb, asoc);
9935 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9936 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9937 /* Hmm, found a sack already on queue, remove it */
9938 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9939 asoc->ctrl_queue_cnt++;
9940 a_chk = chk;
9941 if (a_chk->data) {
9942 sctp_m_freem(a_chk->data);
9943 a_chk->data = NULL;
9944 }
9945 sctp_free_remote_addr(a_chk->whoTo);
9946 a_chk->whoTo = NULL;
9947 break;
9948 }
9949 }
9950 if (a_chk == NULL) {
9951 sctp_alloc_a_chunk(stcb, a_chk);
9952 if (a_chk == NULL) {
9953 /* No memory so we drop the idea, and set a timer */
9954 if (stcb->asoc.delayed_ack) {
9955 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9956 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9957 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9958 stcb->sctp_ep, stcb, NULL);
9959 } else {
9960 stcb->asoc.send_sack = 1;
9961 }
9962 return;
9963 }
9964 a_chk->copy_by_ref = 0;
9965 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9966 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9967 a_chk->rec.chunk_id.can_take_data = 1;
9968 }
9969 /* Clear our pkt counts */
9970 asoc->data_pkts_seen = 0;
9971
9972 a_chk->asoc = asoc;
9973 a_chk->snd_count = 0;
9974 a_chk->send_size = 0; /* fill in later */
9975 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9976 a_chk->whoTo = NULL;
9977
9978 if ((asoc->numduptsns) ||
9979 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9980 ) {
9981 /*-
9982 * Ok, we have some duplicates or the destination for the
9983 * sack is unreachable, lets see if we can select an
9984 * alternate than asoc->last_data_chunk_from
9985 */
9986 if ((!(asoc->last_data_chunk_from->dest_state &
9987 SCTP_ADDR_NOT_REACHABLE)) &&
9988 (asoc->used_alt_onsack > asoc->numnets)) {
9989 /* We used an alt last time, don't this time */
9990 a_chk->whoTo = NULL;
9991 } else {
9992 asoc->used_alt_onsack++;
9993 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9994 }
9995 if (a_chk->whoTo == NULL) {
9996 /* Nope, no alternate */
9997 a_chk->whoTo = asoc->last_data_chunk_from;
9998 asoc->used_alt_onsack = 0;
9999 }
10000 } else {
10001 /*
10002 * No duplicates so we use the last place we received data
10003 * from.
10004 */
10005 asoc->used_alt_onsack = 0;
10006 a_chk->whoTo = asoc->last_data_chunk_from;
10007 }
10008 if (a_chk->whoTo) {
10009 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10010 }
10011 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
10012 /* no gaps */
10013 space_req = sizeof(struct sctp_sack_chunk);
10014 } else {
10015 /* gaps get a cluster */
10016 space_req = MCLBYTES;
10017 }
10018 /* Ok now lets formulate a MBUF with our sack */
10019 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10020 if ((a_chk->data == NULL) ||
10021 (a_chk->whoTo == NULL)) {
10022 /* rats, no mbuf memory */
10023 if (a_chk->data) {
10024 /* was a problem with the destination */
10025 sctp_m_freem(a_chk->data);
10026 a_chk->data = NULL;
10027 }
10028 sctp_free_a_chunk(stcb, a_chk);
10029 /* sa_ignore NO_NULL_CHK */
10030 if (stcb->asoc.delayed_ack) {
10031 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10032 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10033 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10034 stcb->sctp_ep, stcb, NULL);
10035 } else {
10036 stcb->asoc.send_sack = 1;
10037 }
10038 return;
10039 }
10040 /* ok, lets go through and fill it in */
10041 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10042 space = M_TRAILINGSPACE(a_chk->data);
10043 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10044 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10045 }
10046 limit = mtod(a_chk->data, caddr_t);
10047 limit += space;
10048
10049 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10050 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
10051 /* 0x01 is used by nonce for ecn */
10052 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10053 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10054 (asoc->peer_supports_ecn_nonce))
10055 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10056 else
10057 sack->ch.chunk_flags = 0;
10058
10059 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10060 /*-
10061 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10062 * received, then set high bit to 1, else 0. Reset
10063 * pkts_rcvd.
10064 */
10065 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10066 asoc->cmt_dac_pkts_rcvd = 0;
10067 }
10068#ifdef SCTP_ASOCLOG_OF_TSNS
10069 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10070 stcb->asoc.cumack_log_atsnt++;
10071 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10072 stcb->asoc.cumack_log_atsnt = 0;
10073 }
10074#endif
10075 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10076 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10077 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10078
10079 /* reset the readers interpretation */
10080 stcb->freed_by_sorcv_sincelast = 0;
10081
10082 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10083
10084 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10085 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10086 else
10087 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10088
10089 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10090 offset = 1;
10091 /*-
10092 * cum-ack behind the mapping array, so we start and use all
10093 * entries.
10094 */
10095 jstart = 0;
10096 } else {
10097 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10098 /*-
10099 * we skip the first one when the cum-ack is at or above the
10100 * mapping array base. Note this only works if
10101 */
10102 jstart = 1;
10103 }
10104 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10105 /* we have a gap .. maybe */
10106 for (i = 0; i < siz; i++) {
10107 selector = &sack_array[asoc->mapping_array[i]];
10108 if (mergeable && selector->right_edge) {
10109 /*
10110 * Backup, left and right edges were ok to
10111 * merge.
10112 */
10113 num_gap_blocks--;
10114 gap_descriptor--;
10115 }
10116 if (selector->num_entries == 0)
10117 mergeable = 0;
10118 else {
10119 for (j = jstart; j < selector->num_entries; j++) {
10120 if (mergeable && selector->right_edge) {
10121 /*
10122 * do a merge by NOT setting
10123 * the left side
10124 */
10125 mergeable = 0;
10126 } else {
10127 /*
10128 * no merge, set the left
10129 * side
10130 */
10131 mergeable = 0;
10132 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10133 }
10134 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10135 num_gap_blocks++;
10136 gap_descriptor++;
10137 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10138 /* no more room */
10139 limit_reached = 1;
10140 break;
10141 }
10142 }
10143 if (selector->left_edge) {
10144 mergeable = 1;
10145 }
10146 }
10147 if (limit_reached) {
10148 /* Reached the limit stop */
10149 break;
10150 }
10151 jstart = 0;
10152 offset += 8;
10153 }
10154 if (num_gap_blocks == 0) {
10155 /*
10156 * slide not yet happened, and somehow we got called
10157 * to send a sack. Cumack needs to move up.
10158 */
10159 int abort_flag = 0;
10160
10161 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10162 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10163 sctp_sack_check(stcb, 0, 0, &abort_flag);
10164 }
10165 }
10166 /* now we must add any dups we are going to report. */
10167 if ((limit_reached == 0) && (asoc->numduptsns)) {
10168 dup = (uint32_t *) gap_descriptor;
10169 for (i = 0; i < asoc->numduptsns; i++) {
10170 *dup = htonl(asoc->dup_tsns[i]);
10171 dup++;
10172 num_dups++;
10173 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10174 /* no more room */
10175 break;
10176 }
10177 }
10178 asoc->numduptsns = 0;
10179 }
10180 /*
10181 * now that the chunk is prepared queue it to the control chunk
10182 * queue.
10183 */
10184 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10185 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10186 (num_dups * sizeof(int32_t)));
10187 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10188 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10189 sack->sack.num_dup_tsns = htons(num_dups);
10190 sack->ch.chunk_length = htons(a_chk->send_size);
10191 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10192 asoc->ctrl_queue_cnt++;
10193 asoc->send_sack = 0;
10194 SCTP_STAT_INCR(sctps_sendsacks);
10195 return;
10196}
10197
10198/* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
10199void
10200sctp_send_nr_sack(struct sctp_tcb *stcb)
10201{
10202 /*-
10203 * Queue up an NR-SACK in the control queue. We must first check to see
10204 * if an NR-SACK is somehow on the control queue. If so, we will take
10205 * and and remove the old one.
10206 */
10207 struct sctp_association *asoc;
10208 struct sctp_tmit_chunk *chk, *a_chk;
10209
10210 struct sctp_nr_sack_chunk *nr_sack;
10211
10212 struct sctp_gap_ack_block *gap_descriptor;
10213 struct sctp_nr_gap_ack_block *nr_gap_descriptor;
10214
10215 struct sack_track *selector;
10216 struct sack_track *nr_selector;
10217
10218 /* EY do we need nr_mergeable, NO */
10219 int mergeable = 0;
10220 int offset;
10221 caddr_t limit;
10222 uint32_t *dup;
10223 int limit_reached = 0;
10224 unsigned int i, jstart, siz, j;
10225 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10226 int num_dups = 0;
10227 int space_req;
10228 unsigned int reserved = 0;
10229
10230 a_chk = NULL;
10231 asoc = &stcb->asoc;
10232 SCTP_TCB_LOCK_ASSERT(stcb);
10233 if (asoc->last_data_chunk_from == NULL) {
10234 /* Hmm we never received anything */
10235 return;
10236 }
10237 sctp_set_rwnd(stcb, asoc);
10238 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10239 if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
10240 /* Hmm, found a sack already on queue, remove it */
10241 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10242 asoc->ctrl_queue_cnt++;
10243 a_chk = chk;
10244 if (a_chk->data) {
10245 sctp_m_freem(a_chk->data);
10246 a_chk->data = NULL;
10247 }
10248 sctp_free_remote_addr(a_chk->whoTo);
10249 a_chk->whoTo = NULL;
10250 break;
10251 }
10252 }
10253 if (a_chk == NULL) {
10254 sctp_alloc_a_chunk(stcb, a_chk);
10255 if (a_chk == NULL) {
10256 /* No memory so we drop the idea, and set a timer */
10257 if (stcb->asoc.delayed_ack) {
10258 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10259 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10260 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10261 stcb->sctp_ep, stcb, NULL);
10262 } else {
10263 stcb->asoc.send_sack = 1;
10264 }
10265 return;
10266 }
10267 a_chk->copy_by_ref = 0;
10268 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
10269 a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
10270 a_chk->rec.chunk_id.can_take_data = 1;
10271 }
10272 /* Clear our pkt counts */
10273 asoc->data_pkts_seen = 0;
10274
10275 a_chk->asoc = asoc;
10276 a_chk->snd_count = 0;
10277 a_chk->send_size = 0; /* fill in later */
10278 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10279 a_chk->whoTo = NULL;
10280
10281 if ((asoc->numduptsns) ||
10282 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
10283 ) {
10284 /*-
10285 * Ok, we have some duplicates or the destination for the
10286 * sack is unreachable, lets see if we can select an
10287 * alternate than asoc->last_data_chunk_from
10288 */
10289 if ((!(asoc->last_data_chunk_from->dest_state &
10290 SCTP_ADDR_NOT_REACHABLE)) &&
10291 (asoc->used_alt_onsack > asoc->numnets)) {
10292 /* We used an alt last time, don't this time */
10293 a_chk->whoTo = NULL;
10294 } else {
10295 asoc->used_alt_onsack++;
10296 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10297 }
10298 if (a_chk->whoTo == NULL) {
10299 /* Nope, no alternate */
10300 a_chk->whoTo = asoc->last_data_chunk_from;
10301 asoc->used_alt_onsack = 0;
10302 }
10303 } else {
10304 /*
10305 * No duplicates so we use the last place we received data
10306 * from.
10307 */
10308 asoc->used_alt_onsack = 0;
10309 a_chk->whoTo = asoc->last_data_chunk_from;
10310 }
10311 if (a_chk->whoTo) {
10312 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10313 }
10314 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
10315 /* no gaps */
10316 space_req = sizeof(struct sctp_nr_sack_chunk);
10317 } else {
10318 /* EY - what is this about? */
10319 /* gaps get a cluster */
10320 space_req = MCLBYTES;
10321 }
10322 /* Ok now lets formulate a MBUF with our sack */
10323 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10324 if ((a_chk->data == NULL) ||
10325 (a_chk->whoTo == NULL)) {
10326 /* rats, no mbuf memory */
10327 if (a_chk->data) {
10328 /* was a problem with the destination */
10329 sctp_m_freem(a_chk->data);
10330 a_chk->data = NULL;
10331 }
10332 sctp_free_a_chunk(stcb, a_chk);
10333 /* sa_ignore NO_NULL_CHK */
10334 if (stcb->asoc.delayed_ack) {
10335 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10336 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10337 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10338 stcb->sctp_ep, stcb, NULL);
10339 } else {
10340 stcb->asoc.send_sack = 1;
10341 }
10342 return;
10343 }
10344 /* ok, lets go through and fill it in */
10345 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10346 space = M_TRAILINGSPACE(a_chk->data);
10347 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10348 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10349 }
10350 limit = mtod(a_chk->data, caddr_t);
10351 limit += space;
10352
10353 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10354 nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
10355 /* EYJ */
10356 /* 0x01 is used by nonce for ecn */
10357 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10358 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10359 (asoc->peer_supports_ecn_nonce))
10360 nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10361 else
10362 nr_sack->ch.chunk_flags = 0;
10363
10364 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10365 /*-
10366 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10367 * received, then set high bit to 1, else 0. Reset
10368 * pkts_rcvd.
10369 */
10370 /* EY - TODO: which chunk flag is used in here? -The LSB */
10371 nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10372 asoc->cmt_dac_pkts_rcvd = 0;
10373 }
10374#ifdef SCTP_ASOCLOG_OF_TSNS
10375 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10376 stcb->asoc.cumack_log_atsnt++;
10377 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10378 stcb->asoc.cumack_log_atsnt = 0;
10379 }
10380#endif
10381 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10382 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10383 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10384
10385 /* reset the readers interpretation */
10386 stcb->freed_by_sorcv_sincelast = 0;
10387
10388 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10389
10390 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10391 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10392 else
10393 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10394
10395 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10396 offset = 1;
10397 /*-
10398 * cum-ack behind the mapping array, so we start and use all
10399 * entries.
10400 */
10401 jstart = 0;
10402 } else {
10403 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10404 /*-
10405 * we skip the first one when the cum-ack is at or above the
10406 * mapping array base. Note this only works if
10407 */
10408 jstart = 1;
10409 }
10410 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10411 /* we have a gap .. maybe */
10412 for (i = 0; i < siz; i++) {
10413 selector = &sack_array[asoc->mapping_array[i]];
10414 if (mergeable && selector->right_edge) {
10415 /*
10416 * Backup, left and right edges were ok to
10417 * merge.
10418 */
10419 num_gap_blocks--;
10420 gap_descriptor--;
10421 }
10422 if (selector->num_entries == 0)
10423 mergeable = 0;
10424 else {
10425 for (j = jstart; j < selector->num_entries; j++) {
10426 if (mergeable && selector->right_edge) {
10427 /*
10428 * do a merge by NOT setting
10429 * the left side
10430 */
10431 mergeable = 0;
10432 } else {
10433 /*
10434 * no merge, set the left
10435 * side
10436 */
10437 mergeable = 0;
10438 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10439 }
10440 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10441 num_gap_blocks++;
10442 gap_descriptor++;
10443 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10444 /* no more room */
10445 limit_reached = 1;
10446 break;
10447 }
10448 }
10449 if (selector->left_edge) {
10450 mergeable = 1;
10451 }
10452 }
10453 if (limit_reached) {
10454 /* Reached the limit stop */
10455 break;
10456 }
10457 jstart = 0;
10458 offset += 8;
10459 }
10460 if (num_gap_blocks == 0) {
10461 /*
10462 * slide not yet happened, and somehow we got called
10463 * to send a sack. Cumack needs to move up.
10464 */
10465 int abort_flag = 0;
10466
10467 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10468 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10469 sctp_sack_check(stcb, 0, 0, &abort_flag);
10470 }
10471 }
10472 /*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
10473
10474 nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
10475
10476 /* EY - there will be gaps + nr_gaps if draining is possible */
10477 if ((SCTP_BASE_SYSCTL(sctp_do_drain)) && (limit_reached == 0)) {
10478
10479 mergeable = 0;
10480
10481 if (asoc->highest_tsn_inside_nr_map > asoc->nr_mapping_array_base_tsn)
10482 siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
10483 else
10484 siz = (((MAX_TSN - asoc->nr_mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10485
10486 if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10487 offset = 1;
10488 /*-
10489 * cum-ack behind the mapping array, so we start and use all
10490 * entries.
10491 */
10492 jstart = 0;
10493 } else {
10494 offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
10495 /*-
10496 * we skip the first one when the cum-ack is at or above the
10497 * mapping array base. Note this only works if
10498 */
10499 jstart = 1;
10500 }
10501 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10502 /* we have a gap .. maybe */
10503 for (i = 0; i < siz; i++) {
10504 nr_selector = &sack_array[asoc->nr_mapping_array[i]];
10505 if (mergeable && nr_selector->right_edge) {
10506 /*
10507 * Backup, left and right edges were
10508 * ok to merge.
10509 */
10510 num_nr_gap_blocks--;
10511 nr_gap_descriptor--;
10512 }
10513 if (nr_selector->num_entries == 0)
10514 mergeable = 0;
10515 else {
10516 for (j = jstart; j < nr_selector->num_entries; j++) {
10517 if (mergeable && nr_selector->right_edge) {
10518 /*
10519 * do a merge by NOT
10520 * setting the left
10521 * side
10522 */
10523 mergeable = 0;
10524 } else {
10525 /*
10526 * no merge, set the
10527 * left side
10528 */
10529 mergeable = 0;
10530 nr_gap_descriptor->start = htons((nr_selector->gaps[j].start + offset));
10531 }
10532 nr_gap_descriptor->end = htons((nr_selector->gaps[j].end + offset));
10533 num_nr_gap_blocks++;
10534 nr_gap_descriptor++;
10535 if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
10536 /* no more room */
10537 limit_reached = 1;
10538 break;
10539 }
10540 }
10541 if (nr_selector->left_edge) {
10542 mergeable = 1;
10543 }
10544 }
10545 if (limit_reached) {
10546 /* Reached the limit stop */
10547 break;
10548 }
10549 jstart = 0;
10550 offset += 8;
10551 }
10552 }
10553 }
10554 /*---------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
10555
10556 /* now we must add any dups we are going to report. */
10557 if ((limit_reached == 0) && (asoc->numduptsns)) {
10558 dup = (uint32_t *) nr_gap_descriptor;
10559 for (i = 0; i < asoc->numduptsns; i++) {
10560 *dup = htonl(asoc->dup_tsns[i]);
10561 dup++;
10562 num_dups++;
10563 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10564 /* no more room */
10565 break;
10566 }
10567 }
10568 asoc->numduptsns = 0;
10569 }
10570 /*
10571 * now that the chunk is prepared queue it to the control chunk
10572 * queue.
10573 */
10574 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
10575 num_nr_gap_blocks = num_gap_blocks;
10576 num_gap_blocks = 0;
10577 }
10578 a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
10579 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10580 (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
10581 (num_dups * sizeof(int32_t)));
10582
10583 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10584 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10585 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10586 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10587 nr_sack->nr_sack.reserved = htons(reserved);
10588 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10589 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10590 asoc->ctrl_queue_cnt++;
10591 asoc->send_sack = 0;
10592 SCTP_STAT_INCR(sctps_sendsacks);
10593 return;
10594}
10595
10596void
10597sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10598#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10599 SCTP_UNUSED
10600#endif
10601)
10602{
10603 struct mbuf *m_abort;
10604 struct mbuf *m_out = NULL, *m_end = NULL;
10605 struct sctp_abort_chunk *abort = NULL;
10606 int sz;
10607 uint32_t auth_offset = 0;
10608 struct sctp_auth_chunk *auth = NULL;
10609
10610 /*-
10611 * Add an AUTH chunk, if chunk requires it and save the offset into
10612 * the chain for AUTH
10613 */
10614 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10615 stcb->asoc.peer_auth_chunks)) {
10616 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10617 stcb, SCTP_ABORT_ASSOCIATION);
10618 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10619 }
10620 SCTP_TCB_LOCK_ASSERT(stcb);
10621 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10622 if (m_abort == NULL) {
10623 /* no mbuf's */
10624 if (m_out)
10625 sctp_m_freem(m_out);
10626 return;
10627 }
10628 /* link in any error */
10629 SCTP_BUF_NEXT(m_abort) = operr;
10630 sz = 0;
10631 if (operr) {
10632 struct mbuf *n;
10633
10634 n = operr;
10635 while (n) {
10636 sz += SCTP_BUF_LEN(n);
10637 n = SCTP_BUF_NEXT(n);
10638 }
10639 }
10640 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10641 if (m_out == NULL) {
10642 /* NO Auth chunk prepended, so reserve space in front */
10643 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10644 m_out = m_abort;
10645 } else {
10646 /* Put AUTH chunk at the front of the chain */
10647 SCTP_BUF_NEXT(m_end) = m_abort;
10648 }
10649
10650 /* fill in the ABORT chunk */
10651 abort = mtod(m_abort, struct sctp_abort_chunk *);
10652 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10653 abort->ch.chunk_flags = 0;
10654 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10655
10656 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10657 stcb->asoc.primary_destination,
10658 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10659 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10660 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10661 stcb->asoc.primary_destination->port, so_locked, NULL);
10662 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10663}
10664
10665void
10666sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10667 struct sctp_nets *net)
10668{
10669 /* formulate and SEND a SHUTDOWN-COMPLETE */
10670 struct mbuf *m_shutdown_comp;
10671 struct sctp_shutdown_complete_chunk *shutdown_complete;
10672
10673 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10674 if (m_shutdown_comp == NULL) {
10675 /* no mbuf's */
10676 return;
10677 }
10678 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10679 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10680 shutdown_complete->ch.chunk_flags = 0;
10681 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10682 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10683 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10684 (struct sockaddr *)&net->ro._l_addr,
10685 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10686 stcb->sctp_ep->sctp_lport, stcb->rport,
10687 htonl(stcb->asoc.peer_vtag),
10688 net->port, SCTP_SO_NOT_LOCKED, NULL);
10689 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10690 return;
10691}
10692
10693void
10694sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10695 uint32_t vrf_id, uint16_t port)
10696{
10697 /* formulate and SEND a SHUTDOWN-COMPLETE */
10698 struct mbuf *o_pak;
10699 struct mbuf *mout;
10700 struct ip *iph, *iph_out;
10701 struct udphdr *udp = NULL;
10702
10703#ifdef INET6
10704 struct ip6_hdr *ip6, *ip6_out;
10705
10706#endif
10707 int offset_out, len, mlen;
10708 struct sctp_shutdown_complete_msg *comp_cp;
10709
10710 iph = mtod(m, struct ip *);
10711 switch (iph->ip_v) {
10712 case IPVERSION:
10713 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10714 break;
10715#ifdef INET6
10716 case IPV6_VERSION >> 4:
10717 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10718 break;
10719#endif
10720 default:
10721 return;
10722 }
10723 if (port) {
10724 len += sizeof(struct udphdr);
10725 }
10726 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10727 if (mout == NULL) {
10728 return;
10729 }
10730 SCTP_BUF_RESV_UF(mout, max_linkhdr);
10731 SCTP_BUF_LEN(mout) = len;
10732 SCTP_BUF_NEXT(mout) = NULL;
10733 iph_out = NULL;
10734#ifdef INET6
10735 ip6_out = NULL;
10736#endif
10737 offset_out = 0;
10738
10739 switch (iph->ip_v) {
10740 case IPVERSION:
10741 iph_out = mtod(mout, struct ip *);
10742
10743 /* Fill in the IP header for the ABORT */
10744 iph_out->ip_v = IPVERSION;
10745 iph_out->ip_hl = (sizeof(struct ip) / 4);
10746 iph_out->ip_tos = (u_char)0;
10747 iph_out->ip_id = 0;
10748 iph_out->ip_off = 0;
10749 iph_out->ip_ttl = MAXTTL;
10750 if (port) {
10751 iph_out->ip_p = IPPROTO_UDP;
10752 } else {
10753 iph_out->ip_p = IPPROTO_SCTP;
10754 }
10755 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10756 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10757
10758 /* let IP layer calculate this */
10759 iph_out->ip_sum = 0;
10760 offset_out += sizeof(*iph_out);
10761 comp_cp = (struct sctp_shutdown_complete_msg *)(
10762 (caddr_t)iph_out + offset_out);
10763 break;
10764#ifdef INET6
10765 case IPV6_VERSION >> 4:
10766 ip6 = (struct ip6_hdr *)iph;
10767 ip6_out = mtod(mout, struct ip6_hdr *);
10768
10769 /* Fill in the IPv6 header for the ABORT */
10770 ip6_out->ip6_flow = ip6->ip6_flow;
10771 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
10771 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10772 if (port) {
10773 ip6_out->ip6_nxt = IPPROTO_UDP;
10774 } else {
10775 ip6_out->ip6_nxt = IPPROTO_SCTP;
10776 }
10777 ip6_out->ip6_src = ip6->ip6_dst;
10778 ip6_out->ip6_dst = ip6->ip6_src;
10779 /*
10780 * ?? The old code had both the iph len + payload, I think
10781 * this is wrong and would never have worked
10782 */
10783 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10784 offset_out += sizeof(*ip6_out);
10785 comp_cp = (struct sctp_shutdown_complete_msg *)(
10786 (caddr_t)ip6_out + offset_out);
10787 break;
10788#endif /* INET6 */
10789 default:
10790 /* Currently not supported. */
10791 sctp_m_freem(mout);
10792 return;
10793 }
10794 if (port) {
10795 udp = (struct udphdr *)comp_cp;
10796 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10797 udp->uh_dport = port;
10798 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10799 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10800 offset_out += sizeof(struct udphdr);
10801 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10802 }
10803 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10804 /* no mbuf's */
10805 sctp_m_freem(mout);
10806 return;
10807 }
10808 /* Now copy in and fill in the ABORT tags etc. */
10809 comp_cp->sh.src_port = sh->dest_port;
10810 comp_cp->sh.dest_port = sh->src_port;
10811 comp_cp->sh.checksum = 0;
10812 comp_cp->sh.v_tag = sh->v_tag;
10813 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10814 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10815 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10816
10817 if (iph_out != NULL) {
10818 sctp_route_t ro;
10819 int ret;
10820 struct sctp_tcb *stcb = NULL;
10821
10822 mlen = SCTP_BUF_LEN(mout);
10823 bzero(&ro, sizeof ro);
10824 /* set IPv4 length */
10825 iph_out->ip_len = mlen;
10826#ifdef SCTP_PACKET_LOGGING
10827 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10828 sctp_packet_log(mout, mlen);
10829#endif
10830 if (port) {
10831 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10832 SCTP_STAT_INCR(sctps_sendswcrc);
10833 SCTP_ENABLE_UDP_CSUM(mout);
10834 } else {
10835 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10836 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
10837 SCTP_STAT_INCR(sctps_sendhwcrc);
10838 }
10839 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10840 /* out it goes */
10841 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10842
10843 /* Free the route if we got one back */
10844 if (ro.ro_rt)
10845 RTFREE(ro.ro_rt);
10846 }
10847#ifdef INET6
10848 if (ip6_out != NULL) {
10849 struct route_in6 ro;
10850 int ret;
10851 struct sctp_tcb *stcb = NULL;
10852 struct ifnet *ifp = NULL;
10853
10854 bzero(&ro, sizeof(ro));
10855 mlen = SCTP_BUF_LEN(mout);
10856#ifdef SCTP_PACKET_LOGGING
10857 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10858 sctp_packet_log(mout, mlen);
10859#endif
10860 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10861 SCTP_STAT_INCR(sctps_sendswcrc);
10862 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10863 if (port) {
10864 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10865 sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10866 udp->uh_sum = 0xffff;
10867 }
10868 }
10869 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10870
10871 /* Free the route if we got one back */
10872 if (ro.ro_rt)
10873 RTFREE(ro.ro_rt);
10874 }
10875#endif
10876 SCTP_STAT_INCR(sctps_sendpackets);
10877 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10878 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10879 return;
10880
10881}
10882
10883static struct sctp_nets *
10884sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10885{
10886 struct sctp_nets *net, *hnet;
10887 int ms_goneby, highest_ms, state_overide = 0;
10888
10889 (void)SCTP_GETTIME_TIMEVAL(now);
10890 highest_ms = 0;
10891 hnet = NULL;
10892 SCTP_TCB_LOCK_ASSERT(stcb);
10893 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10894 if (
10895 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10896 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10897 ) {
10898 /*
10899 * Skip this guy from consideration if HB is off AND
10900 * its confirmed
10901 */
10902 continue;
10903 }
10904 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10905 /* skip this dest net from consideration */
10906 continue;
10907 }
10908 if (net->last_sent_time.tv_sec) {
10909 /* Sent to so we subtract */
10910 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10911 } else
10912 /* Never been sent to */
10913 ms_goneby = 0x7fffffff;
10914 /*-
10915 * When the address state is unconfirmed but still
10916 * considered reachable, we HB at a higher rate. Once it
10917 * goes confirmed OR reaches the "unreachable" state, thenw
10918 * we cut it back to HB at a more normal pace.
10919 */
10920 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10921 state_overide = 1;
10922 } else {
10923 state_overide = 0;
10924 }
10925
10926 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10927 (ms_goneby > highest_ms)) {
10928 highest_ms = ms_goneby;
10929 hnet = net;
10930 }
10931 }
10932 if (hnet &&
10933 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10934 state_overide = 1;
10935 } else {
10936 state_overide = 0;
10937 }
10938
10939 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10940 /*-
10941 * Found the one with longest delay bounds OR it is
10942 * unconfirmed and still not marked unreachable.
10943 */
10944 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10945#ifdef SCTP_DEBUG
10946 if (hnet) {
10947 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10948 (struct sockaddr *)&hnet->ro._l_addr);
10949 } else {
10950 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10951 }
10952#endif
10953 /* update the timer now */
10954 hnet->last_sent_time = *now;
10955 return (hnet);
10956 }
10957 /* Nothing to HB */
10958 return (NULL);
10959}
10960
10961int
10962sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10963{
10964 struct sctp_tmit_chunk *chk;
10965 struct sctp_nets *net;
10966 struct sctp_heartbeat_chunk *hb;
10967 struct timeval now;
10968 struct sockaddr_in *sin;
10969 struct sockaddr_in6 *sin6;
10970
10971 SCTP_TCB_LOCK_ASSERT(stcb);
10972 if (user_req == 0) {
10973 net = sctp_select_hb_destination(stcb, &now);
10974 if (net == NULL) {
10975 /*-
10976 * All our busy none to send to, just start the
10977 * timer again.
10978 */
10979 if (stcb->asoc.state == 0) {
10980 return (0);
10981 }
10982 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10983 stcb->sctp_ep,
10984 stcb,
10985 net);
10986 return (0);
10987 }
10988 } else {
10989 net = u_net;
10990 if (net == NULL) {
10991 return (0);
10992 }
10993 (void)SCTP_GETTIME_TIMEVAL(&now);
10994 }
10995 sin = (struct sockaddr_in *)&net->ro._l_addr;
10996 if (sin->sin_family != AF_INET) {
10997 if (sin->sin_family != AF_INET6) {
10998 /* huh */
10999 return (0);
11000 }
11001 }
11002 sctp_alloc_a_chunk(stcb, chk);
11003 if (chk == NULL) {
11004 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11005 return (0);
11006 }
11007 chk->copy_by_ref = 0;
11008 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11009 chk->rec.chunk_id.can_take_data = 1;
11010 chk->asoc = &stcb->asoc;
11011 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11012
11013 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11014 if (chk->data == NULL) {
11015 sctp_free_a_chunk(stcb, chk);
11016 return (0);
11017 }
11018 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11019 SCTP_BUF_LEN(chk->data) = chk->send_size;
11020 chk->sent = SCTP_DATAGRAM_UNSENT;
11021 chk->snd_count = 0;
11022 chk->whoTo = net;
11023 atomic_add_int(&chk->whoTo->ref_count, 1);
11024 /* Now we have a mbuf that we can fill in with the details */
11025 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11026 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11027 /* fill out chunk header */
11028 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11029 hb->ch.chunk_flags = 0;
11030 hb->ch.chunk_length = htons(chk->send_size);
11031 /* Fill out hb parameter */
11032 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11033 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11034 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11035 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11036 /* Did our user request this one, put it in */
11037 hb->heartbeat.hb_info.user_req = user_req;
11038 hb->heartbeat.hb_info.addr_family = sin->sin_family;
11039 hb->heartbeat.hb_info.addr_len = sin->sin_len;
11040 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11041 /*
11042 * we only take from the entropy pool if the address is not
11043 * confirmed.
11044 */
11045 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11046 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11047 } else {
11048 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11049 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11050 }
11051 if (sin->sin_family == AF_INET) {
11052 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
11053 } else if (sin->sin_family == AF_INET6) {
11054 /* We leave the scope the way it is in our lookup table. */
11055 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
11056 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
11057 } else {
11058 /* huh compiler bug */
11059 return (0);
11060 }
11061
11062 /*
11063 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
11064 * PF-heartbeats. Because of this, threshold management is done by
11065 * the t3 timer handler, and does not need to be done upon the send
11066 * of a PF-heartbeat. If CMT PF is on and the destination to which a
11067 * heartbeat is being sent is in PF state, do NOT do threshold
11068 * management.
11069 */
11070 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
11071 /* ok we have a destination that needs a beat */
11072 /* lets do the theshold management Qiaobing style */
11073 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
11074 stcb->asoc.max_send_times)) {
11075 /*-
11076 * we have lost the association, in a way this is
11077 * quite bad since we really are one less time since
11078 * we really did not send yet. This is the down side
11079 * to the Q's style as defined in the RFC and not my
11080 * alternate style defined in the RFC.
11081 */
11082 if (chk->data != NULL) {
11083 sctp_m_freem(chk->data);
11084 chk->data = NULL;
11085 }
11086 /*
11087 * Here we do NOT use the macro since the
11088 * association is now gone.
11089 */
11090 if (chk->whoTo) {
11091 sctp_free_remote_addr(chk->whoTo);
11092 chk->whoTo = NULL;
11093 }
11094 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
11095 return (-1);
11096 }
11097 }
11098 net->hb_responded = 0;
11099 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11100 stcb->asoc.ctrl_queue_cnt++;
11101 SCTP_STAT_INCR(sctps_sendheartbeat);
11102 /*-
11103 * Call directly med level routine to put out the chunk. It will
11104 * always tumble out control chunks aka HB but it may even tumble
11105 * out data too.
11106 */
11107 return (1);
11108}
11109
11110void
11111sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11112 uint32_t high_tsn)
11113{
11114 struct sctp_association *asoc;
11115 struct sctp_ecne_chunk *ecne;
11116 struct sctp_tmit_chunk *chk;
11117
11118 asoc = &stcb->asoc;
11119 SCTP_TCB_LOCK_ASSERT(stcb);
11120 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11121 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
11122 /* found a previous ECN_ECHO update it if needed */
11123 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11124 ecne->tsn = htonl(high_tsn);
11125 return;
11126 }
11127 }
11128 /* nope could not find one to update so we must build one */
11129 sctp_alloc_a_chunk(stcb, chk);
11130 if (chk == NULL) {
11131 return;
11132 }
11133 chk->copy_by_ref = 0;
11134 SCTP_STAT_INCR(sctps_sendecne);
11135 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11136 chk->rec.chunk_id.can_take_data = 0;
11137 chk->asoc = &stcb->asoc;
11138 chk->send_size = sizeof(struct sctp_ecne_chunk);
11139 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11140 if (chk->data == NULL) {
11141 sctp_free_a_chunk(stcb, chk);
11142 return;
11143 }
11144 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11145 SCTP_BUF_LEN(chk->data) = chk->send_size;
11146 chk->sent = SCTP_DATAGRAM_UNSENT;
11147 chk->snd_count = 0;
11148 chk->whoTo = net;
11149 atomic_add_int(&chk->whoTo->ref_count, 1);
11150 stcb->asoc.ecn_echo_cnt_onq++;
11151 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11152 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11153 ecne->ch.chunk_flags = 0;
11154 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11155 ecne->tsn = htonl(high_tsn);
11156 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11157 asoc->ctrl_queue_cnt++;
11158}
11159
11160void
11161sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11162 struct mbuf *m, int iphlen, int bad_crc)
11163{
11164 struct sctp_association *asoc;
11165 struct sctp_pktdrop_chunk *drp;
11166 struct sctp_tmit_chunk *chk;
11167 uint8_t *datap;
11168 int len;
11169 int was_trunc = 0;
11170 struct ip *iph;
11171
11172#ifdef INET6
11173 struct ip6_hdr *ip6h;
11174
11175#endif
11176 int fullsz = 0, extra = 0;
11177 long spc;
11178 int offset;
11179 struct sctp_chunkhdr *ch, chunk_buf;
11180 unsigned int chk_length;
11181
11182 if (!stcb) {
11183 return;
11184 }
11185 asoc = &stcb->asoc;
11186 SCTP_TCB_LOCK_ASSERT(stcb);
11187 if (asoc->peer_supports_pktdrop == 0) {
11188 /*-
11189 * peer must declare support before I send one.
11190 */
11191 return;
11192 }
11193 if (stcb->sctp_socket == NULL) {
11194 return;
11195 }
11196 sctp_alloc_a_chunk(stcb, chk);
11197 if (chk == NULL) {
11198 return;
11199 }
11200 chk->copy_by_ref = 0;
11201 iph = mtod(m, struct ip *);
11202 if (iph == NULL) {
11203 sctp_free_a_chunk(stcb, chk);
11204 return;
11205 }
11206 switch (iph->ip_v) {
11207 case IPVERSION:
11208 /* IPv4 */
11209 len = chk->send_size = iph->ip_len;
11210 break;
11211#ifdef INET6
11212 case IPV6_VERSION >> 4:
11213 /* IPv6 */
11214 ip6h = mtod(m, struct ip6_hdr *);
11215 len = chk->send_size = htons(ip6h->ip6_plen);
11216 break;
11217#endif
11218 default:
11219 return;
11220 }
11221 /* Validate that we do not have an ABORT in here. */
11222 offset = iphlen + sizeof(struct sctphdr);
11223 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11224 sizeof(*ch), (uint8_t *) & chunk_buf);
11225 while (ch != NULL) {
11226 chk_length = ntohs(ch->chunk_length);
11227 if (chk_length < sizeof(*ch)) {
11228 /* break to abort land */
11229 break;
11230 }
11231 switch (ch->chunk_type) {
11232 case SCTP_PACKET_DROPPED:
11233 case SCTP_ABORT_ASSOCIATION:
11234 /*-
11235 * we don't respond with an PKT-DROP to an ABORT
11236 * or PKT-DROP
11237 */
11238 sctp_free_a_chunk(stcb, chk);
11239 return;
11240 default:
11241 break;
11242 }
11243 offset += SCTP_SIZE32(chk_length);
11244 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11245 sizeof(*ch), (uint8_t *) & chunk_buf);
11246 }
11247
11248 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11249 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11250 /*
11251 * only send 1 mtu worth, trim off the excess on the end.
11252 */
11253 fullsz = len - extra;
11254 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11255 was_trunc = 1;
11256 }
11257 chk->asoc = &stcb->asoc;
11258 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11259 if (chk->data == NULL) {
11260jump_out:
11261 sctp_free_a_chunk(stcb, chk);
11262 return;
11263 }
11264 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11265 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11266 if (drp == NULL) {
11267 sctp_m_freem(chk->data);
11268 chk->data = NULL;
11269 goto jump_out;
11270 }
11271 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11272 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11273 chk->book_size_scale = 0;
11274 if (was_trunc) {
11275 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11276 drp->trunc_len = htons(fullsz);
11277 /*
11278 * Len is already adjusted to size minus overhead above take
11279 * out the pkt_drop chunk itself from it.
11280 */
11281 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11282 len = chk->send_size;
11283 } else {
11284 /* no truncation needed */
11285 drp->ch.chunk_flags = 0;
11286 drp->trunc_len = htons(0);
11287 }
11288 if (bad_crc) {
11289 drp->ch.chunk_flags |= SCTP_BADCRC;
11290 }
11291 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11292 SCTP_BUF_LEN(chk->data) = chk->send_size;
11293 chk->sent = SCTP_DATAGRAM_UNSENT;
11294 chk->snd_count = 0;
11295 if (net) {
11296 /* we should hit here */
11297 chk->whoTo = net;
11298 } else {
11299 chk->whoTo = asoc->primary_destination;
11300 }
11301 atomic_add_int(&chk->whoTo->ref_count, 1);
11302 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11303 chk->rec.chunk_id.can_take_data = 1;
11304 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11305 drp->ch.chunk_length = htons(chk->send_size);
11306 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11307 if (spc < 0) {
11308 spc = 0;
11309 }
11310 drp->bottle_bw = htonl(spc);
11311 if (asoc->my_rwnd) {
11312 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11313 asoc->size_on_all_streams +
11314 asoc->my_rwnd_control_len +
11315 stcb->sctp_socket->so_rcv.sb_cc);
11316 } else {
11317 /*-
11318 * If my rwnd is 0, possibly from mbuf depletion as well as
11319 * space used, tell the peer there is NO space aka onq == bw
11320 */
11321 drp->current_onq = htonl(spc);
11322 }
11323 drp->reserved = 0;
11324 datap = drp->data;
11325 m_copydata(m, iphlen, len, (caddr_t)datap);
11326 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11327 asoc->ctrl_queue_cnt++;
11328}
11329
11330void
11331sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11332{
11333 struct sctp_association *asoc;
11334 struct sctp_cwr_chunk *cwr;
11335 struct sctp_tmit_chunk *chk;
11336
11337 asoc = &stcb->asoc;
11338 SCTP_TCB_LOCK_ASSERT(stcb);
11339 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11340 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11341 /* found a previous ECN_CWR update it if needed */
11342 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11343 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11344 MAX_TSN)) {
11345 cwr->tsn = htonl(high_tsn);
11346 }
11347 return;
11348 }
11349 }
11350 /* nope could not find one to update so we must build one */
11351 sctp_alloc_a_chunk(stcb, chk);
11352 if (chk == NULL) {
11353 return;
11354 }
11355 chk->copy_by_ref = 0;
11356 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11357 chk->rec.chunk_id.can_take_data = 1;
11358 chk->asoc = &stcb->asoc;
11359 chk->send_size = sizeof(struct sctp_cwr_chunk);
11360 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11361 if (chk->data == NULL) {
11362 sctp_free_a_chunk(stcb, chk);
11363 return;
11364 }
11365 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11366 SCTP_BUF_LEN(chk->data) = chk->send_size;
11367 chk->sent = SCTP_DATAGRAM_UNSENT;
11368 chk->snd_count = 0;
11369 chk->whoTo = net;
11370 atomic_add_int(&chk->whoTo->ref_count, 1);
11371 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11372 cwr->ch.chunk_type = SCTP_ECN_CWR;
11373 cwr->ch.chunk_flags = 0;
11374 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11375 cwr->tsn = htonl(high_tsn);
11376 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11377 asoc->ctrl_queue_cnt++;
11378}
11379
11380void
11381sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11382 int number_entries, uint16_t * list,
11383 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11384{
11385 int len, old_len, i;
11386 struct sctp_stream_reset_out_request *req_out;
11387 struct sctp_chunkhdr *ch;
11388
11389 ch = mtod(chk->data, struct sctp_chunkhdr *);
11390
11391
11392 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11393
11394 /* get to new offset for the param. */
11395 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11396 /* now how long will this param be? */
11397 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11398 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11399 req_out->ph.param_length = htons(len);
11400 req_out->request_seq = htonl(seq);
11401 req_out->response_seq = htonl(resp_seq);
11402 req_out->send_reset_at_tsn = htonl(last_sent);
11403 if (number_entries) {
11404 for (i = 0; i < number_entries; i++) {
11405 req_out->list_of_streams[i] = htons(list[i]);
11406 }
11407 }
11408 if (SCTP_SIZE32(len) > len) {
11409 /*-
11410 * Need to worry about the pad we may end up adding to the
11411 * end. This is easy since the struct is either aligned to 4
11412 * bytes or 2 bytes off.
11413 */
11414 req_out->list_of_streams[number_entries] = 0;
11415 }
11416 /* now fix the chunk length */
11417 ch->chunk_length = htons(len + old_len);
11418 chk->book_size = len + old_len;
11419 chk->book_size_scale = 0;
11420 chk->send_size = SCTP_SIZE32(chk->book_size);
11421 SCTP_BUF_LEN(chk->data) = chk->send_size;
11422 return;
11423}
11424
11425
11426void
11427sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11428 int number_entries, uint16_t * list,
11429 uint32_t seq)
11430{
11431 int len, old_len, i;
11432 struct sctp_stream_reset_in_request *req_in;
11433 struct sctp_chunkhdr *ch;
11434
11435 ch = mtod(chk->data, struct sctp_chunkhdr *);
11436
11437
11438 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11439
11440 /* get to new offset for the param. */
11441 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11442 /* now how long will this param be? */
11443 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11444 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11445 req_in->ph.param_length = htons(len);
11446 req_in->request_seq = htonl(seq);
11447 if (number_entries) {
11448 for (i = 0; i < number_entries; i++) {
11449 req_in->list_of_streams[i] = htons(list[i]);
11450 }
11451 }
11452 if (SCTP_SIZE32(len) > len) {
11453 /*-
11454 * Need to worry about the pad we may end up adding to the
11455 * end. This is easy since the struct is either aligned to 4
11456 * bytes or 2 bytes off.
11457 */
11458 req_in->list_of_streams[number_entries] = 0;
11459 }
11460 /* now fix the chunk length */
11461 ch->chunk_length = htons(len + old_len);
11462 chk->book_size = len + old_len;
11463 chk->book_size_scale = 0;
11464 chk->send_size = SCTP_SIZE32(chk->book_size);
11465 SCTP_BUF_LEN(chk->data) = chk->send_size;
11466 return;
11467}
11468
11469
11470void
11471sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11472 uint32_t seq)
11473{
11474 int len, old_len;
11475 struct sctp_stream_reset_tsn_request *req_tsn;
11476 struct sctp_chunkhdr *ch;
11477
11478 ch = mtod(chk->data, struct sctp_chunkhdr *);
11479
11480
11481 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11482
11483 /* get to new offset for the param. */
11484 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11485 /* now how long will this param be? */
11486 len = sizeof(struct sctp_stream_reset_tsn_request);
11487 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11488 req_tsn->ph.param_length = htons(len);
11489 req_tsn->request_seq = htonl(seq);
11490
11491 /* now fix the chunk length */
11492 ch->chunk_length = htons(len + old_len);
11493 chk->send_size = len + old_len;
11494 chk->book_size = SCTP_SIZE32(chk->send_size);
11495 chk->book_size_scale = 0;
11496 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11497 return;
11498}
11499
11500void
11501sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11502 uint32_t resp_seq, uint32_t result)
11503{
11504 int len, old_len;
11505 struct sctp_stream_reset_response *resp;
11506 struct sctp_chunkhdr *ch;
11507
11508 ch = mtod(chk->data, struct sctp_chunkhdr *);
11509
11510
11511 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11512
11513 /* get to new offset for the param. */
11514 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11515 /* now how long will this param be? */
11516 len = sizeof(struct sctp_stream_reset_response);
11517 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11518 resp->ph.param_length = htons(len);
11519 resp->response_seq = htonl(resp_seq);
11520 resp->result = ntohl(result);
11521
11522 /* now fix the chunk length */
11523 ch->chunk_length = htons(len + old_len);
11524 chk->book_size = len + old_len;
11525 chk->book_size_scale = 0;
11526 chk->send_size = SCTP_SIZE32(chk->book_size);
11527 SCTP_BUF_LEN(chk->data) = chk->send_size;
11528 return;
11529
11530}
11531
11532
11533void
11534sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11535 uint32_t resp_seq, uint32_t result,
11536 uint32_t send_una, uint32_t recv_next)
11537{
11538 int len, old_len;
11539 struct sctp_stream_reset_response_tsn *resp;
11540 struct sctp_chunkhdr *ch;
11541
11542 ch = mtod(chk->data, struct sctp_chunkhdr *);
11543
11544
11545 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11546
11547 /* get to new offset for the param. */
11548 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11549 /* now how long will this param be? */
11550 len = sizeof(struct sctp_stream_reset_response_tsn);
11551 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11552 resp->ph.param_length = htons(len);
11553 resp->response_seq = htonl(resp_seq);
11554 resp->result = htonl(result);
11555 resp->senders_next_tsn = htonl(send_una);
11556 resp->receivers_next_tsn = htonl(recv_next);
11557
11558 /* now fix the chunk length */
11559 ch->chunk_length = htons(len + old_len);
11560 chk->book_size = len + old_len;
11561 chk->send_size = SCTP_SIZE32(chk->book_size);
11562 chk->book_size_scale = 0;
11563 SCTP_BUF_LEN(chk->data) = chk->send_size;
11564 return;
11565}
11566
11567static void
11568sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11569 uint32_t seq,
11570 uint16_t adding)
11571{
11572 int len, old_len;
11573 struct sctp_chunkhdr *ch;
11574 struct sctp_stream_reset_add_strm *addstr;
11575
11576 ch = mtod(chk->data, struct sctp_chunkhdr *);
11577 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11578
11579 /* get to new offset for the param. */
11580 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11581 /* now how long will this param be? */
11582 len = sizeof(struct sctp_stream_reset_add_strm);
11583
11584 /* Fill it out. */
11585 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11586 addstr->ph.param_length = htons(len);
11587 addstr->request_seq = htonl(seq);
11588 addstr->number_of_streams = htons(adding);
11589 addstr->reserved = 0;
11590
11591 /* now fix the chunk length */
11592 ch->chunk_length = htons(len + old_len);
11593 chk->send_size = len + old_len;
11594 chk->book_size = SCTP_SIZE32(chk->send_size);
11595 chk->book_size_scale = 0;
11596 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11597 return;
11598}
11599
11600int
11601sctp_send_str_reset_req(struct sctp_tcb *stcb,
11602 int number_entries, uint16_t * list,
11603 uint8_t send_out_req,
11604 uint32_t resp_seq,
11605 uint8_t send_in_req,
11606 uint8_t send_tsn_req,
11607 uint8_t add_stream,
11608 uint16_t adding
11609)
11610{
11611
11612 struct sctp_association *asoc;
11613 struct sctp_tmit_chunk *chk;
11614 struct sctp_chunkhdr *ch;
11615 uint32_t seq;
11616
11617 asoc = &stcb->asoc;
11618 if (asoc->stream_reset_outstanding) {
11619 /*-
11620 * Already one pending, must get ACK back to clear the flag.
11621 */
11622 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11623 return (EBUSY);
11624 }
11625 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11626 (add_stream == 0)) {
11627 /* nothing to do */
11628 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11629 return (EINVAL);
11630 }
11631 if (send_tsn_req && (send_out_req || send_in_req)) {
11632 /* error, can't do that */
11633 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11634 return (EINVAL);
11635 }
11636 sctp_alloc_a_chunk(stcb, chk);
11637 if (chk == NULL) {
11638 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11639 return (ENOMEM);
11640 }
11641 chk->copy_by_ref = 0;
11642 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11643 chk->rec.chunk_id.can_take_data = 0;
11644 chk->asoc = &stcb->asoc;
11645 chk->book_size = sizeof(struct sctp_chunkhdr);
11646 chk->send_size = SCTP_SIZE32(chk->book_size);
11647 chk->book_size_scale = 0;
11648
11649 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11650 if (chk->data == NULL) {
11651 sctp_free_a_chunk(stcb, chk);
11652 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11653 return (ENOMEM);
11654 }
11655 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11656
11657 /* setup chunk parameters */
11658 chk->sent = SCTP_DATAGRAM_UNSENT;
11659 chk->snd_count = 0;
11660 chk->whoTo = asoc->primary_destination;
11661 atomic_add_int(&chk->whoTo->ref_count, 1);
11662
11663 ch = mtod(chk->data, struct sctp_chunkhdr *);
11664 ch->chunk_type = SCTP_STREAM_RESET;
11665 ch->chunk_flags = 0;
11666 ch->chunk_length = htons(chk->book_size);
11667 SCTP_BUF_LEN(chk->data) = chk->send_size;
11668
11669 seq = stcb->asoc.str_reset_seq_out;
11670 if (send_out_req) {
11671 sctp_add_stream_reset_out(chk, number_entries, list,
11672 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11673 asoc->stream_reset_out_is_outstanding = 1;
11674 seq++;
11675 asoc->stream_reset_outstanding++;
11676 }
11677 if (add_stream) {
11678 sctp_add_a_stream(chk, seq, adding);
11679 seq++;
11680 asoc->stream_reset_outstanding++;
11681 }
11682 if (send_in_req) {
11683 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11684 asoc->stream_reset_outstanding++;
11685 }
11686 if (send_tsn_req) {
11687 sctp_add_stream_reset_tsn(chk, seq);
11688 asoc->stream_reset_outstanding++;
11689 }
11690 asoc->str_reset = chk;
11691
11692 /* insert the chunk for sending */
11693 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11694 chk,
11695 sctp_next);
11696 asoc->ctrl_queue_cnt++;
11697 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11698 return (0);
11699}
11700
11701void
11702sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11703 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11704{
11705 /*-
11706 * Formulate the abort message, and send it back down.
11707 */
11708 struct mbuf *o_pak;
11709 struct mbuf *mout;
11710 struct sctp_abort_msg *abm;
11711 struct ip *iph, *iph_out;
11712 struct udphdr *udp;
11713
11714#ifdef INET6
11715 struct ip6_hdr *ip6, *ip6_out;
11716
11717#endif
11718 int iphlen_out, len;
11719
11720 /* don't respond to ABORT with ABORT */
11721 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11722 if (err_cause)
11723 sctp_m_freem(err_cause);
11724 return;
11725 }
11726 iph = mtod(m, struct ip *);
11727 switch (iph->ip_v) {
11728 case IPVERSION:
11729 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11730 break;
11731#ifdef INET6
11732 case IPV6_VERSION >> 4:
11733 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11734 break;
11735#endif
11736 default:
11737 if (err_cause) {
11738 sctp_m_freem(err_cause);
11739 }
11740 return;
11741 }
11742 if (port) {
11743 len += sizeof(struct udphdr);
11744 }
11745 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11746 if (mout == NULL) {
11747 if (err_cause) {
11748 sctp_m_freem(err_cause);
11749 }
11750 return;
11751 }
11752 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11753 SCTP_BUF_LEN(mout) = len;
11754 SCTP_BUF_NEXT(mout) = err_cause;
11755 iph_out = NULL;
11756#ifdef INET6
11757 ip6_out = NULL;
11758#endif
11759 switch (iph->ip_v) {
11760 case IPVERSION:
11761 iph_out = mtod(mout, struct ip *);
11762
11763 /* Fill in the IP header for the ABORT */
11764 iph_out->ip_v = IPVERSION;
11765 iph_out->ip_hl = (sizeof(struct ip) / 4);
11766 iph_out->ip_tos = (u_char)0;
11767 iph_out->ip_id = 0;
11768 iph_out->ip_off = 0;
11769 iph_out->ip_ttl = MAXTTL;
11770 if (port) {
11771 iph_out->ip_p = IPPROTO_UDP;
11772 } else {
11773 iph_out->ip_p = IPPROTO_SCTP;
11774 }
11775 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11776 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11777 /* let IP layer calculate this */
11778 iph_out->ip_sum = 0;
11779
11780 iphlen_out = sizeof(*iph_out);
11781 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11782 break;
11783#ifdef INET6
11784 case IPV6_VERSION >> 4:
11785 ip6 = (struct ip6_hdr *)iph;
11786 ip6_out = mtod(mout, struct ip6_hdr *);
11787
11788 /* Fill in the IP6 header for the ABORT */
11789 ip6_out->ip6_flow = ip6->ip6_flow;
10772 if (port) {
10773 ip6_out->ip6_nxt = IPPROTO_UDP;
10774 } else {
10775 ip6_out->ip6_nxt = IPPROTO_SCTP;
10776 }
10777 ip6_out->ip6_src = ip6->ip6_dst;
10778 ip6_out->ip6_dst = ip6->ip6_src;
10779 /*
10780 * ?? The old code had both the iph len + payload, I think
10781 * this is wrong and would never have worked
10782 */
10783 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10784 offset_out += sizeof(*ip6_out);
10785 comp_cp = (struct sctp_shutdown_complete_msg *)(
10786 (caddr_t)ip6_out + offset_out);
10787 break;
10788#endif /* INET6 */
10789 default:
10790 /* Currently not supported. */
10791 sctp_m_freem(mout);
10792 return;
10793 }
10794 if (port) {
10795 udp = (struct udphdr *)comp_cp;
10796 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10797 udp->uh_dport = port;
10798 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10799 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10800 offset_out += sizeof(struct udphdr);
10801 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10802 }
10803 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10804 /* no mbuf's */
10805 sctp_m_freem(mout);
10806 return;
10807 }
10808 /* Now copy in and fill in the ABORT tags etc. */
10809 comp_cp->sh.src_port = sh->dest_port;
10810 comp_cp->sh.dest_port = sh->src_port;
10811 comp_cp->sh.checksum = 0;
10812 comp_cp->sh.v_tag = sh->v_tag;
10813 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10814 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10815 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10816
10817 if (iph_out != NULL) {
10818 sctp_route_t ro;
10819 int ret;
10820 struct sctp_tcb *stcb = NULL;
10821
10822 mlen = SCTP_BUF_LEN(mout);
10823 bzero(&ro, sizeof ro);
10824 /* set IPv4 length */
10825 iph_out->ip_len = mlen;
10826#ifdef SCTP_PACKET_LOGGING
10827 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10828 sctp_packet_log(mout, mlen);
10829#endif
10830 if (port) {
10831 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10832 SCTP_STAT_INCR(sctps_sendswcrc);
10833 SCTP_ENABLE_UDP_CSUM(mout);
10834 } else {
10835 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10836 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
10837 SCTP_STAT_INCR(sctps_sendhwcrc);
10838 }
10839 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10840 /* out it goes */
10841 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10842
10843 /* Free the route if we got one back */
10844 if (ro.ro_rt)
10845 RTFREE(ro.ro_rt);
10846 }
10847#ifdef INET6
10848 if (ip6_out != NULL) {
10849 struct route_in6 ro;
10850 int ret;
10851 struct sctp_tcb *stcb = NULL;
10852 struct ifnet *ifp = NULL;
10853
10854 bzero(&ro, sizeof(ro));
10855 mlen = SCTP_BUF_LEN(mout);
10856#ifdef SCTP_PACKET_LOGGING
10857 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10858 sctp_packet_log(mout, mlen);
10859#endif
10860 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10861 SCTP_STAT_INCR(sctps_sendswcrc);
10862 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10863 if (port) {
10864 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10865 sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10866 udp->uh_sum = 0xffff;
10867 }
10868 }
10869 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10870
10871 /* Free the route if we got one back */
10872 if (ro.ro_rt)
10873 RTFREE(ro.ro_rt);
10874 }
10875#endif
10876 SCTP_STAT_INCR(sctps_sendpackets);
10877 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10878 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10879 return;
10880
10881}
10882
10883static struct sctp_nets *
10884sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10885{
10886 struct sctp_nets *net, *hnet;
10887 int ms_goneby, highest_ms, state_overide = 0;
10888
10889 (void)SCTP_GETTIME_TIMEVAL(now);
10890 highest_ms = 0;
10891 hnet = NULL;
10892 SCTP_TCB_LOCK_ASSERT(stcb);
10893 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10894 if (
10895 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10896 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10897 ) {
10898 /*
10899 * Skip this guy from consideration if HB is off AND
10900 * its confirmed
10901 */
10902 continue;
10903 }
10904 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10905 /* skip this dest net from consideration */
10906 continue;
10907 }
10908 if (net->last_sent_time.tv_sec) {
10909 /* Sent to so we subtract */
10910 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10911 } else
10912 /* Never been sent to */
10913 ms_goneby = 0x7fffffff;
10914 /*-
10915 * When the address state is unconfirmed but still
10916 * considered reachable, we HB at a higher rate. Once it
10917 * goes confirmed OR reaches the "unreachable" state, thenw
10918 * we cut it back to HB at a more normal pace.
10919 */
10920 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10921 state_overide = 1;
10922 } else {
10923 state_overide = 0;
10924 }
10925
10926 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10927 (ms_goneby > highest_ms)) {
10928 highest_ms = ms_goneby;
10929 hnet = net;
10930 }
10931 }
10932 if (hnet &&
10933 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10934 state_overide = 1;
10935 } else {
10936 state_overide = 0;
10937 }
10938
10939 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10940 /*-
10941 * Found the one with longest delay bounds OR it is
10942 * unconfirmed and still not marked unreachable.
10943 */
10944 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10945#ifdef SCTP_DEBUG
10946 if (hnet) {
10947 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10948 (struct sockaddr *)&hnet->ro._l_addr);
10949 } else {
10950 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10951 }
10952#endif
10953 /* update the timer now */
10954 hnet->last_sent_time = *now;
10955 return (hnet);
10956 }
10957 /* Nothing to HB */
10958 return (NULL);
10959}
10960
10961int
10962sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10963{
10964 struct sctp_tmit_chunk *chk;
10965 struct sctp_nets *net;
10966 struct sctp_heartbeat_chunk *hb;
10967 struct timeval now;
10968 struct sockaddr_in *sin;
10969 struct sockaddr_in6 *sin6;
10970
10971 SCTP_TCB_LOCK_ASSERT(stcb);
10972 if (user_req == 0) {
10973 net = sctp_select_hb_destination(stcb, &now);
10974 if (net == NULL) {
10975 /*-
10976 * All our busy none to send to, just start the
10977 * timer again.
10978 */
10979 if (stcb->asoc.state == 0) {
10980 return (0);
10981 }
10982 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10983 stcb->sctp_ep,
10984 stcb,
10985 net);
10986 return (0);
10987 }
10988 } else {
10989 net = u_net;
10990 if (net == NULL) {
10991 return (0);
10992 }
10993 (void)SCTP_GETTIME_TIMEVAL(&now);
10994 }
10995 sin = (struct sockaddr_in *)&net->ro._l_addr;
10996 if (sin->sin_family != AF_INET) {
10997 if (sin->sin_family != AF_INET6) {
10998 /* huh */
10999 return (0);
11000 }
11001 }
11002 sctp_alloc_a_chunk(stcb, chk);
11003 if (chk == NULL) {
11004 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11005 return (0);
11006 }
11007 chk->copy_by_ref = 0;
11008 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11009 chk->rec.chunk_id.can_take_data = 1;
11010 chk->asoc = &stcb->asoc;
11011 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11012
11013 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11014 if (chk->data == NULL) {
11015 sctp_free_a_chunk(stcb, chk);
11016 return (0);
11017 }
11018 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11019 SCTP_BUF_LEN(chk->data) = chk->send_size;
11020 chk->sent = SCTP_DATAGRAM_UNSENT;
11021 chk->snd_count = 0;
11022 chk->whoTo = net;
11023 atomic_add_int(&chk->whoTo->ref_count, 1);
11024 /* Now we have a mbuf that we can fill in with the details */
11025 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11026 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11027 /* fill out chunk header */
11028 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11029 hb->ch.chunk_flags = 0;
11030 hb->ch.chunk_length = htons(chk->send_size);
11031 /* Fill out hb parameter */
11032 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11033 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11034 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11035 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11036 /* Did our user request this one, put it in */
11037 hb->heartbeat.hb_info.user_req = user_req;
11038 hb->heartbeat.hb_info.addr_family = sin->sin_family;
11039 hb->heartbeat.hb_info.addr_len = sin->sin_len;
11040 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11041 /*
11042 * we only take from the entropy pool if the address is not
11043 * confirmed.
11044 */
11045 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11046 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11047 } else {
11048 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11049 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11050 }
11051 if (sin->sin_family == AF_INET) {
11052 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
11053 } else if (sin->sin_family == AF_INET6) {
11054 /* We leave the scope the way it is in our lookup table. */
11055 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
11056 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
11057 } else {
11058 /* huh compiler bug */
11059 return (0);
11060 }
11061
11062 /*
11063 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
11064 * PF-heartbeats. Because of this, threshold management is done by
11065 * the t3 timer handler, and does not need to be done upon the send
11066 * of a PF-heartbeat. If CMT PF is on and the destination to which a
11067 * heartbeat is being sent is in PF state, do NOT do threshold
11068 * management.
11069 */
11070 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
11071 /* ok we have a destination that needs a beat */
11072 /* lets do the theshold management Qiaobing style */
11073 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
11074 stcb->asoc.max_send_times)) {
11075 /*-
11076 * we have lost the association, in a way this is
11077 * quite bad since we really are one less time since
11078 * we really did not send yet. This is the down side
11079 * to the Q's style as defined in the RFC and not my
11080 * alternate style defined in the RFC.
11081 */
11082 if (chk->data != NULL) {
11083 sctp_m_freem(chk->data);
11084 chk->data = NULL;
11085 }
11086 /*
11087 * Here we do NOT use the macro since the
11088 * association is now gone.
11089 */
11090 if (chk->whoTo) {
11091 sctp_free_remote_addr(chk->whoTo);
11092 chk->whoTo = NULL;
11093 }
11094 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
11095 return (-1);
11096 }
11097 }
11098 net->hb_responded = 0;
11099 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11100 stcb->asoc.ctrl_queue_cnt++;
11101 SCTP_STAT_INCR(sctps_sendheartbeat);
11102 /*-
11103 * Call directly med level routine to put out the chunk. It will
11104 * always tumble out control chunks aka HB but it may even tumble
11105 * out data too.
11106 */
11107 return (1);
11108}
11109
11110void
11111sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11112 uint32_t high_tsn)
11113{
11114 struct sctp_association *asoc;
11115 struct sctp_ecne_chunk *ecne;
11116 struct sctp_tmit_chunk *chk;
11117
11118 asoc = &stcb->asoc;
11119 SCTP_TCB_LOCK_ASSERT(stcb);
11120 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11121 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
11122 /* found a previous ECN_ECHO update it if needed */
11123 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11124 ecne->tsn = htonl(high_tsn);
11125 return;
11126 }
11127 }
11128 /* nope could not find one to update so we must build one */
11129 sctp_alloc_a_chunk(stcb, chk);
11130 if (chk == NULL) {
11131 return;
11132 }
11133 chk->copy_by_ref = 0;
11134 SCTP_STAT_INCR(sctps_sendecne);
11135 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11136 chk->rec.chunk_id.can_take_data = 0;
11137 chk->asoc = &stcb->asoc;
11138 chk->send_size = sizeof(struct sctp_ecne_chunk);
11139 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11140 if (chk->data == NULL) {
11141 sctp_free_a_chunk(stcb, chk);
11142 return;
11143 }
11144 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11145 SCTP_BUF_LEN(chk->data) = chk->send_size;
11146 chk->sent = SCTP_DATAGRAM_UNSENT;
11147 chk->snd_count = 0;
11148 chk->whoTo = net;
11149 atomic_add_int(&chk->whoTo->ref_count, 1);
11150 stcb->asoc.ecn_echo_cnt_onq++;
11151 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11152 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11153 ecne->ch.chunk_flags = 0;
11154 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11155 ecne->tsn = htonl(high_tsn);
11156 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11157 asoc->ctrl_queue_cnt++;
11158}
11159
11160void
11161sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11162 struct mbuf *m, int iphlen, int bad_crc)
11163{
11164 struct sctp_association *asoc;
11165 struct sctp_pktdrop_chunk *drp;
11166 struct sctp_tmit_chunk *chk;
11167 uint8_t *datap;
11168 int len;
11169 int was_trunc = 0;
11170 struct ip *iph;
11171
11172#ifdef INET6
11173 struct ip6_hdr *ip6h;
11174
11175#endif
11176 int fullsz = 0, extra = 0;
11177 long spc;
11178 int offset;
11179 struct sctp_chunkhdr *ch, chunk_buf;
11180 unsigned int chk_length;
11181
11182 if (!stcb) {
11183 return;
11184 }
11185 asoc = &stcb->asoc;
11186 SCTP_TCB_LOCK_ASSERT(stcb);
11187 if (asoc->peer_supports_pktdrop == 0) {
11188 /*-
11189 * peer must declare support before I send one.
11190 */
11191 return;
11192 }
11193 if (stcb->sctp_socket == NULL) {
11194 return;
11195 }
11196 sctp_alloc_a_chunk(stcb, chk);
11197 if (chk == NULL) {
11198 return;
11199 }
11200 chk->copy_by_ref = 0;
11201 iph = mtod(m, struct ip *);
11202 if (iph == NULL) {
11203 sctp_free_a_chunk(stcb, chk);
11204 return;
11205 }
11206 switch (iph->ip_v) {
11207 case IPVERSION:
11208 /* IPv4 */
11209 len = chk->send_size = iph->ip_len;
11210 break;
11211#ifdef INET6
11212 case IPV6_VERSION >> 4:
11213 /* IPv6 */
11214 ip6h = mtod(m, struct ip6_hdr *);
11215 len = chk->send_size = htons(ip6h->ip6_plen);
11216 break;
11217#endif
11218 default:
11219 return;
11220 }
11221 /* Validate that we do not have an ABORT in here. */
11222 offset = iphlen + sizeof(struct sctphdr);
11223 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11224 sizeof(*ch), (uint8_t *) & chunk_buf);
11225 while (ch != NULL) {
11226 chk_length = ntohs(ch->chunk_length);
11227 if (chk_length < sizeof(*ch)) {
11228 /* break to abort land */
11229 break;
11230 }
11231 switch (ch->chunk_type) {
11232 case SCTP_PACKET_DROPPED:
11233 case SCTP_ABORT_ASSOCIATION:
11234 /*-
11235 * we don't respond with an PKT-DROP to an ABORT
11236 * or PKT-DROP
11237 */
11238 sctp_free_a_chunk(stcb, chk);
11239 return;
11240 default:
11241 break;
11242 }
11243 offset += SCTP_SIZE32(chk_length);
11244 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11245 sizeof(*ch), (uint8_t *) & chunk_buf);
11246 }
11247
11248 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11249 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11250 /*
11251 * only send 1 mtu worth, trim off the excess on the end.
11252 */
11253 fullsz = len - extra;
11254 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11255 was_trunc = 1;
11256 }
11257 chk->asoc = &stcb->asoc;
11258 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11259 if (chk->data == NULL) {
11260jump_out:
11261 sctp_free_a_chunk(stcb, chk);
11262 return;
11263 }
11264 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11265 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11266 if (drp == NULL) {
11267 sctp_m_freem(chk->data);
11268 chk->data = NULL;
11269 goto jump_out;
11270 }
11271 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11272 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11273 chk->book_size_scale = 0;
11274 if (was_trunc) {
11275 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11276 drp->trunc_len = htons(fullsz);
11277 /*
11278 * Len is already adjusted to size minus overhead above take
11279 * out the pkt_drop chunk itself from it.
11280 */
11281 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11282 len = chk->send_size;
11283 } else {
11284 /* no truncation needed */
11285 drp->ch.chunk_flags = 0;
11286 drp->trunc_len = htons(0);
11287 }
11288 if (bad_crc) {
11289 drp->ch.chunk_flags |= SCTP_BADCRC;
11290 }
11291 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11292 SCTP_BUF_LEN(chk->data) = chk->send_size;
11293 chk->sent = SCTP_DATAGRAM_UNSENT;
11294 chk->snd_count = 0;
11295 if (net) {
11296 /* we should hit here */
11297 chk->whoTo = net;
11298 } else {
11299 chk->whoTo = asoc->primary_destination;
11300 }
11301 atomic_add_int(&chk->whoTo->ref_count, 1);
11302 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11303 chk->rec.chunk_id.can_take_data = 1;
11304 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11305 drp->ch.chunk_length = htons(chk->send_size);
11306 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11307 if (spc < 0) {
11308 spc = 0;
11309 }
11310 drp->bottle_bw = htonl(spc);
11311 if (asoc->my_rwnd) {
11312 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11313 asoc->size_on_all_streams +
11314 asoc->my_rwnd_control_len +
11315 stcb->sctp_socket->so_rcv.sb_cc);
11316 } else {
11317 /*-
11318 * If my rwnd is 0, possibly from mbuf depletion as well as
11319 * space used, tell the peer there is NO space aka onq == bw
11320 */
11321 drp->current_onq = htonl(spc);
11322 }
11323 drp->reserved = 0;
11324 datap = drp->data;
11325 m_copydata(m, iphlen, len, (caddr_t)datap);
11326 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11327 asoc->ctrl_queue_cnt++;
11328}
11329
11330void
11331sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11332{
11333 struct sctp_association *asoc;
11334 struct sctp_cwr_chunk *cwr;
11335 struct sctp_tmit_chunk *chk;
11336
11337 asoc = &stcb->asoc;
11338 SCTP_TCB_LOCK_ASSERT(stcb);
11339 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11340 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11341 /* found a previous ECN_CWR update it if needed */
11342 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11343 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11344 MAX_TSN)) {
11345 cwr->tsn = htonl(high_tsn);
11346 }
11347 return;
11348 }
11349 }
11350 /* nope could not find one to update so we must build one */
11351 sctp_alloc_a_chunk(stcb, chk);
11352 if (chk == NULL) {
11353 return;
11354 }
11355 chk->copy_by_ref = 0;
11356 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11357 chk->rec.chunk_id.can_take_data = 1;
11358 chk->asoc = &stcb->asoc;
11359 chk->send_size = sizeof(struct sctp_cwr_chunk);
11360 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11361 if (chk->data == NULL) {
11362 sctp_free_a_chunk(stcb, chk);
11363 return;
11364 }
11365 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11366 SCTP_BUF_LEN(chk->data) = chk->send_size;
11367 chk->sent = SCTP_DATAGRAM_UNSENT;
11368 chk->snd_count = 0;
11369 chk->whoTo = net;
11370 atomic_add_int(&chk->whoTo->ref_count, 1);
11371 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11372 cwr->ch.chunk_type = SCTP_ECN_CWR;
11373 cwr->ch.chunk_flags = 0;
11374 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11375 cwr->tsn = htonl(high_tsn);
11376 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11377 asoc->ctrl_queue_cnt++;
11378}
11379
11380void
11381sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11382 int number_entries, uint16_t * list,
11383 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11384{
11385 int len, old_len, i;
11386 struct sctp_stream_reset_out_request *req_out;
11387 struct sctp_chunkhdr *ch;
11388
11389 ch = mtod(chk->data, struct sctp_chunkhdr *);
11390
11391
11392 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11393
11394 /* get to new offset for the param. */
11395 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11396 /* now how long will this param be? */
11397 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11398 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11399 req_out->ph.param_length = htons(len);
11400 req_out->request_seq = htonl(seq);
11401 req_out->response_seq = htonl(resp_seq);
11402 req_out->send_reset_at_tsn = htonl(last_sent);
11403 if (number_entries) {
11404 for (i = 0; i < number_entries; i++) {
11405 req_out->list_of_streams[i] = htons(list[i]);
11406 }
11407 }
11408 if (SCTP_SIZE32(len) > len) {
11409 /*-
11410 * Need to worry about the pad we may end up adding to the
11411 * end. This is easy since the struct is either aligned to 4
11412 * bytes or 2 bytes off.
11413 */
11414 req_out->list_of_streams[number_entries] = 0;
11415 }
11416 /* now fix the chunk length */
11417 ch->chunk_length = htons(len + old_len);
11418 chk->book_size = len + old_len;
11419 chk->book_size_scale = 0;
11420 chk->send_size = SCTP_SIZE32(chk->book_size);
11421 SCTP_BUF_LEN(chk->data) = chk->send_size;
11422 return;
11423}
11424
11425
11426void
11427sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11428 int number_entries, uint16_t * list,
11429 uint32_t seq)
11430{
11431 int len, old_len, i;
11432 struct sctp_stream_reset_in_request *req_in;
11433 struct sctp_chunkhdr *ch;
11434
11435 ch = mtod(chk->data, struct sctp_chunkhdr *);
11436
11437
11438 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11439
11440 /* get to new offset for the param. */
11441 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11442 /* now how long will this param be? */
11443 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11444 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11445 req_in->ph.param_length = htons(len);
11446 req_in->request_seq = htonl(seq);
11447 if (number_entries) {
11448 for (i = 0; i < number_entries; i++) {
11449 req_in->list_of_streams[i] = htons(list[i]);
11450 }
11451 }
11452 if (SCTP_SIZE32(len) > len) {
11453 /*-
11454 * Need to worry about the pad we may end up adding to the
11455 * end. This is easy since the struct is either aligned to 4
11456 * bytes or 2 bytes off.
11457 */
11458 req_in->list_of_streams[number_entries] = 0;
11459 }
11460 /* now fix the chunk length */
11461 ch->chunk_length = htons(len + old_len);
11462 chk->book_size = len + old_len;
11463 chk->book_size_scale = 0;
11464 chk->send_size = SCTP_SIZE32(chk->book_size);
11465 SCTP_BUF_LEN(chk->data) = chk->send_size;
11466 return;
11467}
11468
11469
11470void
11471sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11472 uint32_t seq)
11473{
11474 int len, old_len;
11475 struct sctp_stream_reset_tsn_request *req_tsn;
11476 struct sctp_chunkhdr *ch;
11477
11478 ch = mtod(chk->data, struct sctp_chunkhdr *);
11479
11480
11481 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11482
11483 /* get to new offset for the param. */
11484 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11485 /* now how long will this param be? */
11486 len = sizeof(struct sctp_stream_reset_tsn_request);
11487 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11488 req_tsn->ph.param_length = htons(len);
11489 req_tsn->request_seq = htonl(seq);
11490
11491 /* now fix the chunk length */
11492 ch->chunk_length = htons(len + old_len);
11493 chk->send_size = len + old_len;
11494 chk->book_size = SCTP_SIZE32(chk->send_size);
11495 chk->book_size_scale = 0;
11496 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11497 return;
11498}
11499
11500void
11501sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11502 uint32_t resp_seq, uint32_t result)
11503{
11504 int len, old_len;
11505 struct sctp_stream_reset_response *resp;
11506 struct sctp_chunkhdr *ch;
11507
11508 ch = mtod(chk->data, struct sctp_chunkhdr *);
11509
11510
11511 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11512
11513 /* get to new offset for the param. */
11514 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11515 /* now how long will this param be? */
11516 len = sizeof(struct sctp_stream_reset_response);
11517 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11518 resp->ph.param_length = htons(len);
11519 resp->response_seq = htonl(resp_seq);
11520 resp->result = ntohl(result);
11521
11522 /* now fix the chunk length */
11523 ch->chunk_length = htons(len + old_len);
11524 chk->book_size = len + old_len;
11525 chk->book_size_scale = 0;
11526 chk->send_size = SCTP_SIZE32(chk->book_size);
11527 SCTP_BUF_LEN(chk->data) = chk->send_size;
11528 return;
11529
11530}
11531
11532
11533void
11534sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11535 uint32_t resp_seq, uint32_t result,
11536 uint32_t send_una, uint32_t recv_next)
11537{
11538 int len, old_len;
11539 struct sctp_stream_reset_response_tsn *resp;
11540 struct sctp_chunkhdr *ch;
11541
11542 ch = mtod(chk->data, struct sctp_chunkhdr *);
11543
11544
11545 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11546
11547 /* get to new offset for the param. */
11548 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11549 /* now how long will this param be? */
11550 len = sizeof(struct sctp_stream_reset_response_tsn);
11551 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11552 resp->ph.param_length = htons(len);
11553 resp->response_seq = htonl(resp_seq);
11554 resp->result = htonl(result);
11555 resp->senders_next_tsn = htonl(send_una);
11556 resp->receivers_next_tsn = htonl(recv_next);
11557
11558 /* now fix the chunk length */
11559 ch->chunk_length = htons(len + old_len);
11560 chk->book_size = len + old_len;
11561 chk->send_size = SCTP_SIZE32(chk->book_size);
11562 chk->book_size_scale = 0;
11563 SCTP_BUF_LEN(chk->data) = chk->send_size;
11564 return;
11565}
11566
11567static void
11568sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11569 uint32_t seq,
11570 uint16_t adding)
11571{
11572 int len, old_len;
11573 struct sctp_chunkhdr *ch;
11574 struct sctp_stream_reset_add_strm *addstr;
11575
11576 ch = mtod(chk->data, struct sctp_chunkhdr *);
11577 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11578
11579 /* get to new offset for the param. */
11580 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11581 /* now how long will this param be? */
11582 len = sizeof(struct sctp_stream_reset_add_strm);
11583
11584 /* Fill it out. */
11585 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11586 addstr->ph.param_length = htons(len);
11587 addstr->request_seq = htonl(seq);
11588 addstr->number_of_streams = htons(adding);
11589 addstr->reserved = 0;
11590
11591 /* now fix the chunk length */
11592 ch->chunk_length = htons(len + old_len);
11593 chk->send_size = len + old_len;
11594 chk->book_size = SCTP_SIZE32(chk->send_size);
11595 chk->book_size_scale = 0;
11596 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11597 return;
11598}
11599
11600int
11601sctp_send_str_reset_req(struct sctp_tcb *stcb,
11602 int number_entries, uint16_t * list,
11603 uint8_t send_out_req,
11604 uint32_t resp_seq,
11605 uint8_t send_in_req,
11606 uint8_t send_tsn_req,
11607 uint8_t add_stream,
11608 uint16_t adding
11609)
11610{
11611
11612 struct sctp_association *asoc;
11613 struct sctp_tmit_chunk *chk;
11614 struct sctp_chunkhdr *ch;
11615 uint32_t seq;
11616
11617 asoc = &stcb->asoc;
11618 if (asoc->stream_reset_outstanding) {
11619 /*-
11620 * Already one pending, must get ACK back to clear the flag.
11621 */
11622 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11623 return (EBUSY);
11624 }
11625 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11626 (add_stream == 0)) {
11627 /* nothing to do */
11628 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11629 return (EINVAL);
11630 }
11631 if (send_tsn_req && (send_out_req || send_in_req)) {
11632 /* error, can't do that */
11633 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11634 return (EINVAL);
11635 }
11636 sctp_alloc_a_chunk(stcb, chk);
11637 if (chk == NULL) {
11638 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11639 return (ENOMEM);
11640 }
11641 chk->copy_by_ref = 0;
11642 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11643 chk->rec.chunk_id.can_take_data = 0;
11644 chk->asoc = &stcb->asoc;
11645 chk->book_size = sizeof(struct sctp_chunkhdr);
11646 chk->send_size = SCTP_SIZE32(chk->book_size);
11647 chk->book_size_scale = 0;
11648
11649 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11650 if (chk->data == NULL) {
11651 sctp_free_a_chunk(stcb, chk);
11652 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11653 return (ENOMEM);
11654 }
11655 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11656
11657 /* setup chunk parameters */
11658 chk->sent = SCTP_DATAGRAM_UNSENT;
11659 chk->snd_count = 0;
11660 chk->whoTo = asoc->primary_destination;
11661 atomic_add_int(&chk->whoTo->ref_count, 1);
11662
11663 ch = mtod(chk->data, struct sctp_chunkhdr *);
11664 ch->chunk_type = SCTP_STREAM_RESET;
11665 ch->chunk_flags = 0;
11666 ch->chunk_length = htons(chk->book_size);
11667 SCTP_BUF_LEN(chk->data) = chk->send_size;
11668
11669 seq = stcb->asoc.str_reset_seq_out;
11670 if (send_out_req) {
11671 sctp_add_stream_reset_out(chk, number_entries, list,
11672 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11673 asoc->stream_reset_out_is_outstanding = 1;
11674 seq++;
11675 asoc->stream_reset_outstanding++;
11676 }
11677 if (add_stream) {
11678 sctp_add_a_stream(chk, seq, adding);
11679 seq++;
11680 asoc->stream_reset_outstanding++;
11681 }
11682 if (send_in_req) {
11683 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11684 asoc->stream_reset_outstanding++;
11685 }
11686 if (send_tsn_req) {
11687 sctp_add_stream_reset_tsn(chk, seq);
11688 asoc->stream_reset_outstanding++;
11689 }
11690 asoc->str_reset = chk;
11691
11692 /* insert the chunk for sending */
11693 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11694 chk,
11695 sctp_next);
11696 asoc->ctrl_queue_cnt++;
11697 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11698 return (0);
11699}
11700
11701void
11702sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11703 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11704{
11705 /*-
11706 * Formulate the abort message, and send it back down.
11707 */
11708 struct mbuf *o_pak;
11709 struct mbuf *mout;
11710 struct sctp_abort_msg *abm;
11711 struct ip *iph, *iph_out;
11712 struct udphdr *udp;
11713
11714#ifdef INET6
11715 struct ip6_hdr *ip6, *ip6_out;
11716
11717#endif
11718 int iphlen_out, len;
11719
11720 /* don't respond to ABORT with ABORT */
11721 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11722 if (err_cause)
11723 sctp_m_freem(err_cause);
11724 return;
11725 }
11726 iph = mtod(m, struct ip *);
11727 switch (iph->ip_v) {
11728 case IPVERSION:
11729 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11730 break;
11731#ifdef INET6
11732 case IPV6_VERSION >> 4:
11733 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11734 break;
11735#endif
11736 default:
11737 if (err_cause) {
11738 sctp_m_freem(err_cause);
11739 }
11740 return;
11741 }
11742 if (port) {
11743 len += sizeof(struct udphdr);
11744 }
11745 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11746 if (mout == NULL) {
11747 if (err_cause) {
11748 sctp_m_freem(err_cause);
11749 }
11750 return;
11751 }
11752 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11753 SCTP_BUF_LEN(mout) = len;
11754 SCTP_BUF_NEXT(mout) = err_cause;
11755 iph_out = NULL;
11756#ifdef INET6
11757 ip6_out = NULL;
11758#endif
11759 switch (iph->ip_v) {
11760 case IPVERSION:
11761 iph_out = mtod(mout, struct ip *);
11762
11763 /* Fill in the IP header for the ABORT */
11764 iph_out->ip_v = IPVERSION;
11765 iph_out->ip_hl = (sizeof(struct ip) / 4);
11766 iph_out->ip_tos = (u_char)0;
11767 iph_out->ip_id = 0;
11768 iph_out->ip_off = 0;
11769 iph_out->ip_ttl = MAXTTL;
11770 if (port) {
11771 iph_out->ip_p = IPPROTO_UDP;
11772 } else {
11773 iph_out->ip_p = IPPROTO_SCTP;
11774 }
11775 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11776 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11777 /* let IP layer calculate this */
11778 iph_out->ip_sum = 0;
11779
11780 iphlen_out = sizeof(*iph_out);
11781 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11782 break;
11783#ifdef INET6
11784 case IPV6_VERSION >> 4:
11785 ip6 = (struct ip6_hdr *)iph;
11786 ip6_out = mtod(mout, struct ip6_hdr *);
11787
11788 /* Fill in the IP6 header for the ABORT */
11789 ip6_out->ip6_flow = ip6->ip6_flow;
11790 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11790 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11791 if (port) {
11792 ip6_out->ip6_nxt = IPPROTO_UDP;
11793 } else {
11794 ip6_out->ip6_nxt = IPPROTO_SCTP;
11795 }
11796 ip6_out->ip6_src = ip6->ip6_dst;
11797 ip6_out->ip6_dst = ip6->ip6_src;
11798
11799 iphlen_out = sizeof(*ip6_out);
11800 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11801 break;
11802#endif /* INET6 */
11803 default:
11804 /* Currently not supported */
11805 sctp_m_freem(mout);
11806 return;
11807 }
11808
11809 udp = (struct udphdr *)abm;
11810 if (port) {
11811 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11812 udp->uh_dport = port;
11813 /* set udp->uh_ulen later */
11814 udp->uh_sum = 0;
11815 iphlen_out += sizeof(struct udphdr);
11816 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11817 }
11818 abm->sh.src_port = sh->dest_port;
11819 abm->sh.dest_port = sh->src_port;
11820 abm->sh.checksum = 0;
11821 if (vtag == 0) {
11822 abm->sh.v_tag = sh->v_tag;
11823 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11824 } else {
11825 abm->sh.v_tag = htonl(vtag);
11826 abm->msg.ch.chunk_flags = 0;
11827 }
11828 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11829
11830 if (err_cause) {
11831 struct mbuf *m_tmp = err_cause;
11832 int err_len = 0;
11833
11834 /* get length of the err_cause chain */
11835 while (m_tmp != NULL) {
11836 err_len += SCTP_BUF_LEN(m_tmp);
11837 m_tmp = SCTP_BUF_NEXT(m_tmp);
11838 }
11839 len = SCTP_BUF_LEN(mout) + err_len;
11840 if (err_len % 4) {
11841 /* need pad at end of chunk */
11842 uint32_t cpthis = 0;
11843 int padlen;
11844
11845 padlen = 4 - (len % 4);
11846 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11847 len += padlen;
11848 }
11849 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11850 } else {
11851 len = SCTP_BUF_LEN(mout);
11852 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11853 }
11854
11855 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11856 /* no mbuf's */
11857 sctp_m_freem(mout);
11858 return;
11859 }
11860 if (iph_out != NULL) {
11861 sctp_route_t ro;
11862 struct sctp_tcb *stcb = NULL;
11863 int ret;
11864
11865 /* zap the stack pointer to the route */
11866 bzero(&ro, sizeof ro);
11867 if (port) {
11868 udp->uh_ulen = htons(len - sizeof(struct ip));
11869 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11870 }
11871 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11872 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11873 /* set IPv4 length */
11874 iph_out->ip_len = len;
11875 /* out it goes */
11876#ifdef SCTP_PACKET_LOGGING
11877 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11878 sctp_packet_log(mout, len);
11879#endif
11880 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11881 if (port) {
11882 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11883 SCTP_STAT_INCR(sctps_sendswcrc);
11884 SCTP_ENABLE_UDP_CSUM(o_pak);
11885 } else {
11886 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11887 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
11888 SCTP_STAT_INCR(sctps_sendhwcrc);
11889 }
11890 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11891
11892 /* Free the route if we got one back */
11893 if (ro.ro_rt)
11894 RTFREE(ro.ro_rt);
11895 }
11896#ifdef INET6
11897 if (ip6_out != NULL) {
11898 struct route_in6 ro;
11899 int ret;
11900 struct sctp_tcb *stcb = NULL;
11901 struct ifnet *ifp = NULL;
11902
11903 /* zap the stack pointer to the route */
11904 bzero(&ro, sizeof(ro));
11905 if (port) {
11906 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11907 }
11908 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11909 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11910 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11911#ifdef SCTP_PACKET_LOGGING
11912 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11913 sctp_packet_log(mout, len);
11914#endif
11915 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11916 SCTP_STAT_INCR(sctps_sendswcrc);
11917 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11918 if (port) {
11919 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11920 udp->uh_sum = 0xffff;
11921 }
11922 }
11923 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11924
11925 /* Free the route if we got one back */
11926 if (ro.ro_rt)
11927 RTFREE(ro.ro_rt);
11928 }
11929#endif
11930 SCTP_STAT_INCR(sctps_sendpackets);
11931 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11932 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11933}
11934
11935void
11936sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11937 uint32_t vrf_id, uint16_t port)
11938{
11939 struct mbuf *o_pak;
11940 struct sctphdr *sh, *sh_out;
11941 struct sctp_chunkhdr *ch;
11942 struct ip *iph, *iph_out;
11943 struct udphdr *udp = NULL;
11944 struct mbuf *mout;
11945
11946#ifdef INET6
11947 struct ip6_hdr *ip6, *ip6_out;
11948
11949#endif
11950 int iphlen_out, len;
11951
11952 iph = mtod(m, struct ip *);
11953 sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11954 switch (iph->ip_v) {
11955 case IPVERSION:
11956 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11957 break;
11958#ifdef INET6
11959 case IPV6_VERSION >> 4:
11960 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11961 break;
11962#endif
11963 default:
11964 if (scm) {
11965 sctp_m_freem(scm);
11966 }
11967 return;
11968 }
11969 if (port) {
11970 len += sizeof(struct udphdr);
11971 }
11972 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11973 if (mout == NULL) {
11974 if (scm) {
11975 sctp_m_freem(scm);
11976 }
11977 return;
11978 }
11979 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11980 SCTP_BUF_LEN(mout) = len;
11981 SCTP_BUF_NEXT(mout) = scm;
11982 iph_out = NULL;
11983#ifdef INET6
11984 ip6_out = NULL;
11985#endif
11986 switch (iph->ip_v) {
11987 case IPVERSION:
11988 iph_out = mtod(mout, struct ip *);
11989
11990 /* Fill in the IP header for the ABORT */
11991 iph_out->ip_v = IPVERSION;
11992 iph_out->ip_hl = (sizeof(struct ip) / 4);
11993 iph_out->ip_tos = (u_char)0;
11994 iph_out->ip_id = 0;
11995 iph_out->ip_off = 0;
11996 iph_out->ip_ttl = MAXTTL;
11997 if (port) {
11998 iph_out->ip_p = IPPROTO_UDP;
11999 } else {
12000 iph_out->ip_p = IPPROTO_SCTP;
12001 }
12002 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12003 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12004 /* let IP layer calculate this */
12005 iph_out->ip_sum = 0;
12006
12007 iphlen_out = sizeof(struct ip);
12008 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12009 break;
12010#ifdef INET6
12011 case IPV6_VERSION >> 4:
12012 ip6 = (struct ip6_hdr *)iph;
12013 ip6_out = mtod(mout, struct ip6_hdr *);
12014
12015 /* Fill in the IP6 header for the ABORT */
12016 ip6_out->ip6_flow = ip6->ip6_flow;
11791 if (port) {
11792 ip6_out->ip6_nxt = IPPROTO_UDP;
11793 } else {
11794 ip6_out->ip6_nxt = IPPROTO_SCTP;
11795 }
11796 ip6_out->ip6_src = ip6->ip6_dst;
11797 ip6_out->ip6_dst = ip6->ip6_src;
11798
11799 iphlen_out = sizeof(*ip6_out);
11800 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11801 break;
11802#endif /* INET6 */
11803 default:
11804 /* Currently not supported */
11805 sctp_m_freem(mout);
11806 return;
11807 }
11808
11809 udp = (struct udphdr *)abm;
11810 if (port) {
11811 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11812 udp->uh_dport = port;
11813 /* set udp->uh_ulen later */
11814 udp->uh_sum = 0;
11815 iphlen_out += sizeof(struct udphdr);
11816 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11817 }
11818 abm->sh.src_port = sh->dest_port;
11819 abm->sh.dest_port = sh->src_port;
11820 abm->sh.checksum = 0;
11821 if (vtag == 0) {
11822 abm->sh.v_tag = sh->v_tag;
11823 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11824 } else {
11825 abm->sh.v_tag = htonl(vtag);
11826 abm->msg.ch.chunk_flags = 0;
11827 }
11828 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11829
11830 if (err_cause) {
11831 struct mbuf *m_tmp = err_cause;
11832 int err_len = 0;
11833
11834 /* get length of the err_cause chain */
11835 while (m_tmp != NULL) {
11836 err_len += SCTP_BUF_LEN(m_tmp);
11837 m_tmp = SCTP_BUF_NEXT(m_tmp);
11838 }
11839 len = SCTP_BUF_LEN(mout) + err_len;
11840 if (err_len % 4) {
11841 /* need pad at end of chunk */
11842 uint32_t cpthis = 0;
11843 int padlen;
11844
11845 padlen = 4 - (len % 4);
11846 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11847 len += padlen;
11848 }
11849 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11850 } else {
11851 len = SCTP_BUF_LEN(mout);
11852 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11853 }
11854
11855 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11856 /* no mbuf's */
11857 sctp_m_freem(mout);
11858 return;
11859 }
11860 if (iph_out != NULL) {
11861 sctp_route_t ro;
11862 struct sctp_tcb *stcb = NULL;
11863 int ret;
11864
11865 /* zap the stack pointer to the route */
11866 bzero(&ro, sizeof ro);
11867 if (port) {
11868 udp->uh_ulen = htons(len - sizeof(struct ip));
11869 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11870 }
11871 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11872 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11873 /* set IPv4 length */
11874 iph_out->ip_len = len;
11875 /* out it goes */
11876#ifdef SCTP_PACKET_LOGGING
11877 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11878 sctp_packet_log(mout, len);
11879#endif
11880 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11881 if (port) {
11882 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11883 SCTP_STAT_INCR(sctps_sendswcrc);
11884 SCTP_ENABLE_UDP_CSUM(o_pak);
11885 } else {
11886 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11887 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
11888 SCTP_STAT_INCR(sctps_sendhwcrc);
11889 }
11890 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11891
11892 /* Free the route if we got one back */
11893 if (ro.ro_rt)
11894 RTFREE(ro.ro_rt);
11895 }
11896#ifdef INET6
11897 if (ip6_out != NULL) {
11898 struct route_in6 ro;
11899 int ret;
11900 struct sctp_tcb *stcb = NULL;
11901 struct ifnet *ifp = NULL;
11902
11903 /* zap the stack pointer to the route */
11904 bzero(&ro, sizeof(ro));
11905 if (port) {
11906 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11907 }
11908 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11909 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11910 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11911#ifdef SCTP_PACKET_LOGGING
11912 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11913 sctp_packet_log(mout, len);
11914#endif
11915 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11916 SCTP_STAT_INCR(sctps_sendswcrc);
11917 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11918 if (port) {
11919 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11920 udp->uh_sum = 0xffff;
11921 }
11922 }
11923 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11924
11925 /* Free the route if we got one back */
11926 if (ro.ro_rt)
11927 RTFREE(ro.ro_rt);
11928 }
11929#endif
11930 SCTP_STAT_INCR(sctps_sendpackets);
11931 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11932 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11933}
11934
11935void
11936sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11937 uint32_t vrf_id, uint16_t port)
11938{
11939 struct mbuf *o_pak;
11940 struct sctphdr *sh, *sh_out;
11941 struct sctp_chunkhdr *ch;
11942 struct ip *iph, *iph_out;
11943 struct udphdr *udp = NULL;
11944 struct mbuf *mout;
11945
11946#ifdef INET6
11947 struct ip6_hdr *ip6, *ip6_out;
11948
11949#endif
11950 int iphlen_out, len;
11951
11952 iph = mtod(m, struct ip *);
11953 sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11954 switch (iph->ip_v) {
11955 case IPVERSION:
11956 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11957 break;
11958#ifdef INET6
11959 case IPV6_VERSION >> 4:
11960 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11961 break;
11962#endif
11963 default:
11964 if (scm) {
11965 sctp_m_freem(scm);
11966 }
11967 return;
11968 }
11969 if (port) {
11970 len += sizeof(struct udphdr);
11971 }
11972 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11973 if (mout == NULL) {
11974 if (scm) {
11975 sctp_m_freem(scm);
11976 }
11977 return;
11978 }
11979 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11980 SCTP_BUF_LEN(mout) = len;
11981 SCTP_BUF_NEXT(mout) = scm;
11982 iph_out = NULL;
11983#ifdef INET6
11984 ip6_out = NULL;
11985#endif
11986 switch (iph->ip_v) {
11987 case IPVERSION:
11988 iph_out = mtod(mout, struct ip *);
11989
11990 /* Fill in the IP header for the ABORT */
11991 iph_out->ip_v = IPVERSION;
11992 iph_out->ip_hl = (sizeof(struct ip) / 4);
11993 iph_out->ip_tos = (u_char)0;
11994 iph_out->ip_id = 0;
11995 iph_out->ip_off = 0;
11996 iph_out->ip_ttl = MAXTTL;
11997 if (port) {
11998 iph_out->ip_p = IPPROTO_UDP;
11999 } else {
12000 iph_out->ip_p = IPPROTO_SCTP;
12001 }
12002 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12003 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12004 /* let IP layer calculate this */
12005 iph_out->ip_sum = 0;
12006
12007 iphlen_out = sizeof(struct ip);
12008 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12009 break;
12010#ifdef INET6
12011 case IPV6_VERSION >> 4:
12012 ip6 = (struct ip6_hdr *)iph;
12013 ip6_out = mtod(mout, struct ip6_hdr *);
12014
12015 /* Fill in the IP6 header for the ABORT */
12016 ip6_out->ip6_flow = ip6->ip6_flow;
12017 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
12017 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12018 if (port) {
12019 ip6_out->ip6_nxt = IPPROTO_UDP;
12020 } else {
12021 ip6_out->ip6_nxt = IPPROTO_SCTP;
12022 }
12023 ip6_out->ip6_src = ip6->ip6_dst;
12024 ip6_out->ip6_dst = ip6->ip6_src;
12025
12026 iphlen_out = sizeof(struct ip6_hdr);
12027 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12028 break;
12029#endif /* INET6 */
12030 default:
12031 /* Currently not supported */
12032 sctp_m_freem(mout);
12033 return;
12034 }
12035
12036 udp = (struct udphdr *)sh_out;
12037 if (port) {
12038 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12039 udp->uh_dport = port;
12040 /* set udp->uh_ulen later */
12041 udp->uh_sum = 0;
12042 iphlen_out += sizeof(struct udphdr);
12043 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12044 }
12045 sh_out->src_port = sh->dest_port;
12046 sh_out->dest_port = sh->src_port;
12047 sh_out->v_tag = vtag;
12048 sh_out->checksum = 0;
12049
12050 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12051 ch->chunk_type = SCTP_OPERATION_ERROR;
12052 ch->chunk_flags = 0;
12053
12054 if (scm) {
12055 struct mbuf *m_tmp = scm;
12056 int cause_len = 0;
12057
12058 /* get length of the err_cause chain */
12059 while (m_tmp != NULL) {
12060 cause_len += SCTP_BUF_LEN(m_tmp);
12061 m_tmp = SCTP_BUF_NEXT(m_tmp);
12062 }
12063 len = SCTP_BUF_LEN(mout) + cause_len;
12064 if (cause_len % 4) {
12065 /* need pad at end of chunk */
12066 uint32_t cpthis = 0;
12067 int padlen;
12068
12069 padlen = 4 - (len % 4);
12070 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12071 len += padlen;
12072 }
12073 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12074 } else {
12075 len = SCTP_BUF_LEN(mout);
12076 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12077 }
12078
12079 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12080 /* no mbuf's */
12081 sctp_m_freem(mout);
12082 return;
12083 }
12084 if (iph_out != NULL) {
12085 sctp_route_t ro;
12086 struct sctp_tcb *stcb = NULL;
12087 int ret;
12088
12089 /* zap the stack pointer to the route */
12090 bzero(&ro, sizeof ro);
12091 if (port) {
12092 udp->uh_ulen = htons(len - sizeof(struct ip));
12093 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12094 }
12095 /* set IPv4 length */
12096 iph_out->ip_len = len;
12097 /* out it goes */
12098#ifdef SCTP_PACKET_LOGGING
12099 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12100 sctp_packet_log(mout, len);
12101#endif
12102 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12103 if (port) {
12104 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12105 SCTP_STAT_INCR(sctps_sendswcrc);
12106 SCTP_ENABLE_UDP_CSUM(o_pak);
12107 } else {
12108 mout->m_pkthdr.csum_flags = CSUM_SCTP;
12109 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
12110 SCTP_STAT_INCR(sctps_sendhwcrc);
12111 }
12112 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12113
12114 /* Free the route if we got one back */
12115 if (ro.ro_rt)
12116 RTFREE(ro.ro_rt);
12117 }
12118#ifdef INET6
12119 if (ip6_out != NULL) {
12120 struct route_in6 ro;
12121 int ret;
12122 struct sctp_tcb *stcb = NULL;
12123 struct ifnet *ifp = NULL;
12124
12125 /* zap the stack pointer to the route */
12126 bzero(&ro, sizeof(ro));
12127 if (port) {
12128 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12129 }
12130 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12131#ifdef SCTP_PACKET_LOGGING
12132 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12133 sctp_packet_log(mout, len);
12134#endif
12135 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12136 SCTP_STAT_INCR(sctps_sendswcrc);
12137 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12138 if (port) {
12139 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12140 udp->uh_sum = 0xffff;
12141 }
12142 }
12143 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12144
12145 /* Free the route if we got one back */
12146 if (ro.ro_rt)
12147 RTFREE(ro.ro_rt);
12148 }
12149#endif
12150 SCTP_STAT_INCR(sctps_sendpackets);
12151 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12152 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12153}
12154
12155static struct mbuf *
12156sctp_copy_resume(struct sctp_stream_queue_pending *sp,
12157 struct uio *uio,
12158 struct sctp_sndrcvinfo *srcv,
12159 int max_send_len,
12160 int user_marks_eor,
12161 int *error,
12162 uint32_t * sndout,
12163 struct mbuf **new_tail)
12164{
12165 struct mbuf *m;
12166
12167 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12168 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12169 if (m == NULL) {
12170 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12171 *error = ENOMEM;
12172 } else {
12173 *sndout = m_length(m, NULL);
12174 *new_tail = m_last(m);
12175 }
12176 return (m);
12177}
12178
12179static int
12180sctp_copy_one(struct sctp_stream_queue_pending *sp,
12181 struct uio *uio,
12182 int resv_upfront)
12183{
12184 int left;
12185
12186 left = sp->length;
12187 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12188 resv_upfront, 0);
12189 if (sp->data == NULL) {
12190 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12191 return (ENOMEM);
12192 }
12193 sp->tail_mbuf = m_last(sp->data);
12194 return (0);
12195}
12196
12197
12198
12199static struct sctp_stream_queue_pending *
12200sctp_copy_it_in(struct sctp_tcb *stcb,
12201 struct sctp_association *asoc,
12202 struct sctp_sndrcvinfo *srcv,
12203 struct uio *uio,
12204 struct sctp_nets *net,
12205 int max_send_len,
12206 int user_marks_eor,
12207 int *error,
12208 int non_blocking)
12209{
12210 /*-
12211 * This routine must be very careful in its work. Protocol
12212 * processing is up and running so care must be taken to spl...()
12213 * when you need to do something that may effect the stcb/asoc. The
12214 * sb is locked however. When data is copied the protocol processing
12215 * should be enabled since this is a slower operation...
12216 */
12217 struct sctp_stream_queue_pending *sp = NULL;
12218 int resv_in_first;
12219
12220 *error = 0;
12221 /* Now can we send this? */
12222 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12223 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12224 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12225 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12226 /* got data while shutting down */
12227 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12228 *error = ECONNRESET;
12229 goto out_now;
12230 }
12231 sctp_alloc_a_strmoq(stcb, sp);
12232 if (sp == NULL) {
12233 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12234 *error = ENOMEM;
12235 goto out_now;
12236 }
12237 sp->act_flags = 0;
12238 sp->sender_all_done = 0;
12239 sp->sinfo_flags = srcv->sinfo_flags;
12240 sp->timetolive = srcv->sinfo_timetolive;
12241 sp->ppid = srcv->sinfo_ppid;
12242 sp->context = srcv->sinfo_context;
12243 sp->strseq = 0;
12244 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12245
12246 sp->stream = srcv->sinfo_stream;
12247 sp->length = min(uio->uio_resid, max_send_len);
12248 if ((sp->length == (uint32_t) uio->uio_resid) &&
12249 ((user_marks_eor == 0) ||
12250 (srcv->sinfo_flags & SCTP_EOF) ||
12251 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12252 sp->msg_is_complete = 1;
12253 } else {
12254 sp->msg_is_complete = 0;
12255 }
12256 sp->sender_all_done = 0;
12257 sp->some_taken = 0;
12258 sp->put_last_out = 0;
12259 resv_in_first = sizeof(struct sctp_data_chunk);
12260 sp->data = sp->tail_mbuf = NULL;
12261 if (sp->length == 0) {
12262 *error = 0;
12263 goto skip_copy;
12264 }
12265 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12266 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12267 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12268 sp->holds_key_ref = 1;
12269 }
12270 *error = sctp_copy_one(sp, uio, resv_in_first);
12271skip_copy:
12272 if (*error) {
12273 sctp_free_a_strmoq(stcb, sp);
12274 sp = NULL;
12275 } else {
12276 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12277 sp->net = net;
12278 sp->addr_over = 1;
12279 } else {
12280 sp->net = asoc->primary_destination;
12281 sp->addr_over = 0;
12282 }
12283 atomic_add_int(&sp->net->ref_count, 1);
12284 sctp_set_prsctp_policy(sp);
12285 }
12286out_now:
12287 return (sp);
12288}
12289
12290
12291int
12292sctp_sosend(struct socket *so,
12293 struct sockaddr *addr,
12294 struct uio *uio,
12295 struct mbuf *top,
12296 struct mbuf *control,
12297 int flags,
12298 struct thread *p
12299)
12300{
12301 struct sctp_inpcb *inp;
12302 int error, use_rcvinfo = 0;
12303 struct sctp_sndrcvinfo srcv;
12304 struct sockaddr *addr_to_use;
12305
12306#ifdef INET6
12307 struct sockaddr_in sin;
12308
12309#endif
12310
12311 inp = (struct sctp_inpcb *)so->so_pcb;
12312 if (control) {
12313 /* process cmsg snd/rcv info (maybe a assoc-id) */
12314 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12315 sizeof(srcv))) {
12316 /* got one */
12317 use_rcvinfo = 1;
12318 }
12319 }
12320 addr_to_use = addr;
12321#if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
12322 if ((addr) && (addr->sa_family == AF_INET6)) {
12323 struct sockaddr_in6 *sin6;
12324
12325 sin6 = (struct sockaddr_in6 *)addr;
12326 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12327 in6_sin6_2_sin(&sin, sin6);
12328 addr_to_use = (struct sockaddr *)&sin;
12329 }
12330 }
12331#endif
12332 error = sctp_lower_sosend(so, addr_to_use, uio, top,
12333 control,
12334 flags,
12335 use_rcvinfo, &srcv
12336 ,p
12337 );
12338 return (error);
12339}
12340
12341
12342int
12343sctp_lower_sosend(struct socket *so,
12344 struct sockaddr *addr,
12345 struct uio *uio,
12346 struct mbuf *i_pak,
12347 struct mbuf *control,
12348 int flags,
12349 int use_rcvinfo,
12350 struct sctp_sndrcvinfo *srcv
12351 ,
12352 struct thread *p
12353)
12354{
12355 unsigned int sndlen = 0, max_len;
12356 int error, len;
12357 struct mbuf *top = NULL;
12358 int queue_only = 0, queue_only_for_init = 0;
12359 int free_cnt_applied = 0;
12360 int un_sent = 0;
12361 int now_filled = 0;
12362 unsigned int inqueue_bytes = 0;
12363 struct sctp_block_entry be;
12364 struct sctp_inpcb *inp;
12365 struct sctp_tcb *stcb = NULL;
12366 struct timeval now;
12367 struct sctp_nets *net;
12368 struct sctp_association *asoc;
12369 struct sctp_inpcb *t_inp;
12370 int user_marks_eor;
12371 int create_lock_applied = 0;
12372 int nagle_applies = 0;
12373 int some_on_control = 0;
12374 int got_all_of_the_send = 0;
12375 int hold_tcblock = 0;
12376 int non_blocking = 0;
12377 int temp_flags = 0;
12378 uint32_t local_add_more, local_soresv = 0;
12379
12380 error = 0;
12381 net = NULL;
12382 stcb = NULL;
12383 asoc = NULL;
12384
12385 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12386 if (inp == NULL) {
12387 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12388 error = EFAULT;
12389 if (i_pak) {
12390 SCTP_RELEASE_PKT(i_pak);
12391 }
12392 return (error);
12393 }
12394 if ((uio == NULL) && (i_pak == NULL)) {
12395 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12396 return (EINVAL);
12397 }
12398 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12399 atomic_add_int(&inp->total_sends, 1);
12400 if (uio) {
12401 if (uio->uio_resid < 0) {
12402 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12403 return (EINVAL);
12404 }
12405 sndlen = uio->uio_resid;
12406 } else {
12407 top = SCTP_HEADER_TO_CHAIN(i_pak);
12408 sndlen = SCTP_HEADER_LEN(i_pak);
12409 }
12410 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12411 addr,
12412 sndlen);
12413 /*-
12414 * Pre-screen address, if one is given the sin-len
12415 * must be set correctly!
12416 */
12417 if (addr) {
12418 if ((addr->sa_family == AF_INET) &&
12419 (addr->sa_len != sizeof(struct sockaddr_in))) {
12420 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12421 error = EINVAL;
12422 goto out_unlocked;
12423 } else if ((addr->sa_family == AF_INET6) &&
12424 (addr->sa_len != sizeof(struct sockaddr_in6))) {
12425 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12426 error = EINVAL;
12427 goto out_unlocked;
12428 }
12429 }
12430 hold_tcblock = 0;
12431
12432 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12433 (inp->sctp_socket->so_qlimit)) {
12434 /* The listener can NOT send */
12435 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12436 error = EFAULT;
12437 goto out_unlocked;
12438 }
12439 if ((use_rcvinfo) && srcv) {
12440 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12441 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12442 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12443 error = EINVAL;
12444 goto out_unlocked;
12445 }
12446 if (srcv->sinfo_flags)
12447 SCTP_STAT_INCR(sctps_sends_with_flags);
12448
12449 if (srcv->sinfo_flags & SCTP_SENDALL) {
12450 /* its a sendall */
12451 error = sctp_sendall(inp, uio, top, srcv);
12452 top = NULL;
12453 goto out_unlocked;
12454 }
12455 }
12456 /* now we must find the assoc */
12457 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12458 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12459 SCTP_INP_RLOCK(inp);
12460 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12461 if (stcb == NULL) {
12462 SCTP_INP_RUNLOCK(inp);
12463 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12464 error = ENOTCONN;
12465 goto out_unlocked;
12466 }
12467 SCTP_TCB_LOCK(stcb);
12468 hold_tcblock = 1;
12469 SCTP_INP_RUNLOCK(inp);
12470 if (addr) {
12471 /* Must locate the net structure if addr given */
12472 net = sctp_findnet(stcb, addr);
12473 if (net) {
12474 /* validate port was 0 or correct */
12475 struct sockaddr_in *sin;
12476
12477 sin = (struct sockaddr_in *)addr;
12478 if ((sin->sin_port != 0) &&
12479 (sin->sin_port != stcb->rport)) {
12480 net = NULL;
12481 }
12482 }
12483 temp_flags |= SCTP_ADDR_OVER;
12484 } else
12485 net = stcb->asoc.primary_destination;
12486 if (addr && (net == NULL)) {
12487 /* Could not find address, was it legal */
12488 if (addr->sa_family == AF_INET) {
12489 struct sockaddr_in *sin;
12490
12491 sin = (struct sockaddr_in *)addr;
12492 if (sin->sin_addr.s_addr == 0) {
12493 if ((sin->sin_port == 0) ||
12494 (sin->sin_port == stcb->rport)) {
12495 net = stcb->asoc.primary_destination;
12496 }
12497 }
12498 } else {
12499 struct sockaddr_in6 *sin6;
12500
12501 sin6 = (struct sockaddr_in6 *)addr;
12502 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12503 if ((sin6->sin6_port == 0) ||
12504 (sin6->sin6_port == stcb->rport)) {
12505 net = stcb->asoc.primary_destination;
12506 }
12507 }
12508 }
12509 }
12510 if (net == NULL) {
12511 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12512 error = EINVAL;
12513 goto out_unlocked;
12514 }
12515 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12516 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12517 if (stcb) {
12518 if (addr)
12519 /*
12520 * Must locate the net structure if addr
12521 * given
12522 */
12523 net = sctp_findnet(stcb, addr);
12524 else
12525 net = stcb->asoc.primary_destination;
12526 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12527 ((net == NULL) || (addr == NULL))) {
12528 struct sockaddr_in *sin;
12529
12530 if (addr == NULL) {
12531 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12532 error = EINVAL;
12533 goto out_unlocked;
12534 }
12535 sin = (struct sockaddr_in *)addr;
12536 /* Validate port is 0 or correct */
12537 if ((sin->sin_port != 0) &&
12538 (sin->sin_port != stcb->rport)) {
12539 net = NULL;
12540 }
12541 }
12542 }
12543 hold_tcblock = 0;
12544 } else if (addr) {
12545 /*-
12546 * Since we did not use findep we must
12547 * increment it, and if we don't find a tcb
12548 * decrement it.
12549 */
12550 SCTP_INP_WLOCK(inp);
12551 SCTP_INP_INCR_REF(inp);
12552 SCTP_INP_WUNLOCK(inp);
12553 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12554 if (stcb == NULL) {
12555 SCTP_INP_WLOCK(inp);
12556 SCTP_INP_DECR_REF(inp);
12557 SCTP_INP_WUNLOCK(inp);
12558 } else {
12559 hold_tcblock = 1;
12560 }
12561 }
12562 if ((stcb == NULL) && (addr)) {
12563 /* Possible implicit send? */
12564 SCTP_ASOC_CREATE_LOCK(inp);
12565 create_lock_applied = 1;
12566 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12567 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12568 /* Should I really unlock ? */
12569 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12570 error = EFAULT;
12571 goto out_unlocked;
12572
12573 }
12574 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12575 (addr->sa_family == AF_INET6)) {
12576 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12577 error = EINVAL;
12578 goto out_unlocked;
12579 }
12580 SCTP_INP_WLOCK(inp);
12581 SCTP_INP_INCR_REF(inp);
12582 SCTP_INP_WUNLOCK(inp);
12583 /* With the lock applied look again */
12584 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12585 if (stcb == NULL) {
12586 SCTP_INP_WLOCK(inp);
12587 SCTP_INP_DECR_REF(inp);
12588 SCTP_INP_WUNLOCK(inp);
12589 } else {
12590 hold_tcblock = 1;
12591 }
12592 if (t_inp != inp) {
12593 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12594 error = ENOTCONN;
12595 goto out_unlocked;
12596 }
12597 }
12598 if (stcb == NULL) {
12599 if (addr == NULL) {
12600 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12601 error = ENOENT;
12602 goto out_unlocked;
12603 } else {
12604 /*
12605 * UDP style, we must go ahead and start the INIT
12606 * process
12607 */
12608 uint32_t vrf_id;
12609
12610 if ((use_rcvinfo) && (srcv) &&
12611 ((srcv->sinfo_flags & SCTP_ABORT) ||
12612 ((srcv->sinfo_flags & SCTP_EOF) &&
12613 (sndlen == 0)))) {
12614 /*-
12615 * User asks to abort a non-existant assoc,
12616 * or EOF a non-existant assoc with no data
12617 */
12618 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12619 error = ENOENT;
12620 goto out_unlocked;
12621 }
12622 /* get an asoc/stcb struct */
12623 vrf_id = inp->def_vrf_id;
12624#ifdef INVARIANTS
12625 if (create_lock_applied == 0) {
12626 panic("Error, should hold create lock and I don't?");
12627 }
12628#endif
12629 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12630 p
12631 );
12632 if (stcb == NULL) {
12633 /* Error is setup for us in the call */
12634 goto out_unlocked;
12635 }
12636 if (create_lock_applied) {
12637 SCTP_ASOC_CREATE_UNLOCK(inp);
12638 create_lock_applied = 0;
12639 } else {
12640 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12641 }
12642 /*
12643 * Turn on queue only flag to prevent data from
12644 * being sent
12645 */
12646 queue_only = 1;
12647 asoc = &stcb->asoc;
12648 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12649 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12650
12651 /* initialize authentication params for the assoc */
12652 sctp_initialize_auth_params(inp, stcb);
12653
12654 if (control) {
12655 /*
12656 * see if a init structure exists in cmsg
12657 * headers
12658 */
12659 struct sctp_initmsg initm;
12660 int i;
12661
12662 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12663 sizeof(initm))) {
12664 /*
12665 * we have an INIT override of the
12666 * default
12667 */
12668 if (initm.sinit_max_attempts)
12669 asoc->max_init_times = initm.sinit_max_attempts;
12670 if (initm.sinit_num_ostreams)
12671 asoc->pre_open_streams = initm.sinit_num_ostreams;
12672 if (initm.sinit_max_instreams)
12673 asoc->max_inbound_streams = initm.sinit_max_instreams;
12674 if (initm.sinit_max_init_timeo)
12675 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12676 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12677 struct sctp_stream_out *tmp_str;
12678 int had_lock = 0;
12679
12680 /* Default is NOT correct */
12681 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12682 asoc->streamoutcnt, asoc->pre_open_streams);
12683 /*
12684 * What happens if this
12685 * fails? we panic ...
12686 */
12687
12688 if (hold_tcblock) {
12689 had_lock = 1;
12690 SCTP_TCB_UNLOCK(stcb);
12691 }
12692 SCTP_MALLOC(tmp_str,
12693 struct sctp_stream_out *,
12694 (asoc->pre_open_streams *
12695 sizeof(struct sctp_stream_out)),
12696 SCTP_M_STRMO);
12697 if (had_lock) {
12698 SCTP_TCB_LOCK(stcb);
12699 }
12700 if (tmp_str != NULL) {
12701 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12702 asoc->strmout = tmp_str;
12703 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12704 } else {
12705 asoc->pre_open_streams = asoc->streamoutcnt;
12706 }
12707 for (i = 0; i < asoc->streamoutcnt; i++) {
12708 /*-
12709 * inbound side must be set
12710 * to 0xffff, also NOTE when
12711 * we get the INIT-ACK back
12712 * (for INIT sender) we MUST
12713 * reduce the count
12714 * (streamoutcnt) but first
12715 * check if we sent to any
12716 * of the upper streams that
12717 * were dropped (if some
12718 * were). Those that were
12719 * dropped must be notified
12720 * to the upper layer as
12721 * failed to send.
12722 */
12723 asoc->strmout[i].next_sequence_sent = 0x0;
12724 TAILQ_INIT(&asoc->strmout[i].outqueue);
12725 asoc->strmout[i].stream_no = i;
12726 asoc->strmout[i].last_msg_incomplete = 0;
12727 asoc->strmout[i].next_spoke.tqe_next = 0;
12728 asoc->strmout[i].next_spoke.tqe_prev = 0;
12729 }
12730 }
12731 }
12732 }
12733 hold_tcblock = 1;
12734 /* out with the INIT */
12735 queue_only_for_init = 1;
12736 /*-
12737 * we may want to dig in after this call and adjust the MTU
12738 * value. It defaulted to 1500 (constant) but the ro
12739 * structure may now have an update and thus we may need to
12740 * change it BEFORE we append the message.
12741 */
12742 net = stcb->asoc.primary_destination;
12743 asoc = &stcb->asoc;
12744 }
12745 }
12746 if ((SCTP_SO_IS_NBIO(so)
12747 || (flags & MSG_NBIO)
12748 )) {
12749 non_blocking = 1;
12750 }
12751 asoc = &stcb->asoc;
12752 atomic_add_int(&stcb->total_sends, 1);
12753
12754 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12755 if (sndlen > asoc->smallest_mtu) {
12756 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12757 error = EMSGSIZE;
12758 goto out_unlocked;
12759 }
12760 }
12761 /* would we block? */
12762 if (non_blocking) {
12763 if (hold_tcblock == 0) {
12764 SCTP_TCB_LOCK(stcb);
12765 hold_tcblock = 1;
12766 }
12767 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12768 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12769 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12770 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12771 if (sndlen > SCTP_SB_LIMIT_SND(so))
12772 error = EMSGSIZE;
12773 else
12774 error = EWOULDBLOCK;
12775 goto out_unlocked;
12776 }
12777 stcb->asoc.sb_send_resv += sndlen;
12778 SCTP_TCB_UNLOCK(stcb);
12779 hold_tcblock = 0;
12780 } else {
12781 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12782 }
12783 local_soresv = sndlen;
12784 /* Keep the stcb from being freed under our feet */
12785 if (free_cnt_applied) {
12786#ifdef INVARIANTS
12787 panic("refcnt already incremented");
12788#else
12789 printf("refcnt:1 already incremented?\n");
12790#endif
12791 } else {
12792 atomic_add_int(&stcb->asoc.refcnt, 1);
12793 free_cnt_applied = 1;
12794 }
12795 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12796 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12797 error = ECONNRESET;
12798 goto out_unlocked;
12799 }
12800 if (create_lock_applied) {
12801 SCTP_ASOC_CREATE_UNLOCK(inp);
12802 create_lock_applied = 0;
12803 }
12804 if (asoc->stream_reset_outstanding) {
12805 /*
12806 * Can't queue any data while stream reset is underway.
12807 */
12808 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12809 error = EAGAIN;
12810 goto out_unlocked;
12811 }
12812 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12813 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12814 queue_only = 1;
12815 }
12816 if ((use_rcvinfo == 0) || (srcv == NULL)) {
12817 /* Grab the default stuff from the asoc */
12818 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12819 }
12820 /* we are now done with all control */
12821 if (control) {
12822 sctp_m_freem(control);
12823 control = NULL;
12824 }
12825 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12826 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12827 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12828 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12829 if ((use_rcvinfo) &&
12830 (srcv->sinfo_flags & SCTP_ABORT)) {
12831 ;
12832 } else {
12833 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12834 error = ECONNRESET;
12835 goto out_unlocked;
12836 }
12837 }
12838 /* Ok, we will attempt a msgsnd :> */
12839 if (p) {
12840 p->td_ru.ru_msgsnd++;
12841 }
12842 if (stcb) {
12843 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12844 net = stcb->asoc.primary_destination;
12845 }
12846 }
12847 if (net == NULL) {
12848 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12849 error = EINVAL;
12850 goto out_unlocked;
12851 }
12852 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12853 /*-
12854 * CMT: Added check for CMT above. net above is the primary
12855 * dest. If CMT is ON, sender should always attempt to send
12856 * with the output routine sctp_fill_outqueue() that loops
12857 * through all destination addresses. Therefore, if CMT is
12858 * ON, queue_only is NOT set to 1 here, so that
12859 * sctp_chunk_output() can be called below.
12860 */
12861 queue_only = 1;
12862 } else if (asoc->ifp_had_enobuf) {
12863 SCTP_STAT_INCR(sctps_ifnomemqueued);
12864 if (net->flight_size > (net->mtu * 2))
12865 queue_only = 1;
12866 asoc->ifp_had_enobuf = 0;
12867 } else {
12868 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12869 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12870 }
12871 /* Are we aborting? */
12872 if (srcv->sinfo_flags & SCTP_ABORT) {
12873 struct mbuf *mm;
12874 int tot_demand, tot_out = 0, max_out;
12875
12876 SCTP_STAT_INCR(sctps_sends_with_abort);
12877 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12878 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12879 /* It has to be up before we abort */
12880 /* how big is the user initiated abort? */
12881 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12882 error = EINVAL;
12883 goto out;
12884 }
12885 if (hold_tcblock) {
12886 SCTP_TCB_UNLOCK(stcb);
12887 hold_tcblock = 0;
12888 }
12889 if (top) {
12890 struct mbuf *cntm = NULL;
12891
12892 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12893 if (sndlen != 0) {
12894 cntm = top;
12895 while (cntm) {
12896 tot_out += SCTP_BUF_LEN(cntm);
12897 cntm = SCTP_BUF_NEXT(cntm);
12898 }
12899 }
12900 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12901 } else {
12902 /* Must fit in a MTU */
12903 tot_out = sndlen;
12904 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12905 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12906 /* To big */
12907 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12908 error = EMSGSIZE;
12909 goto out;
12910 }
12911 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12912 }
12913 if (mm == NULL) {
12914 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12915 error = ENOMEM;
12916 goto out;
12917 }
12918 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12919 max_out -= sizeof(struct sctp_abort_msg);
12920 if (tot_out > max_out) {
12921 tot_out = max_out;
12922 }
12923 if (mm) {
12924 struct sctp_paramhdr *ph;
12925
12926 /* now move forward the data pointer */
12927 ph = mtod(mm, struct sctp_paramhdr *);
12928 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12929 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12930 ph++;
12931 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12932 if (top == NULL) {
12933 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12934 if (error) {
12935 /*-
12936 * Here if we can't get his data we
12937 * still abort we just don't get to
12938 * send the users note :-0
12939 */
12940 sctp_m_freem(mm);
12941 mm = NULL;
12942 }
12943 } else {
12944 if (sndlen != 0) {
12945 SCTP_BUF_NEXT(mm) = top;
12946 }
12947 }
12948 }
12949 if (hold_tcblock == 0) {
12950 SCTP_TCB_LOCK(stcb);
12951 hold_tcblock = 1;
12952 }
12953 atomic_add_int(&stcb->asoc.refcnt, -1);
12954 free_cnt_applied = 0;
12955 /* release this lock, otherwise we hang on ourselves */
12956 sctp_abort_an_association(stcb->sctp_ep, stcb,
12957 SCTP_RESPONSE_TO_USER_REQ,
12958 mm, SCTP_SO_LOCKED);
12959 /* now relock the stcb so everything is sane */
12960 hold_tcblock = 0;
12961 stcb = NULL;
12962 /*
12963 * In this case top is already chained to mm avoid double
12964 * free, since we free it below if top != NULL and driver
12965 * would free it after sending the packet out
12966 */
12967 if (sndlen != 0) {
12968 top = NULL;
12969 }
12970 goto out_unlocked;
12971 }
12972 /* Calculate the maximum we can send */
12973 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12974 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12975 if (non_blocking) {
12976 /* we already checked for non-blocking above. */
12977 max_len = sndlen;
12978 } else {
12979 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12980 }
12981 } else {
12982 max_len = 0;
12983 }
12984 if (hold_tcblock) {
12985 SCTP_TCB_UNLOCK(stcb);
12986 hold_tcblock = 0;
12987 }
12988 /* Is the stream no. valid? */
12989 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12990 /* Invalid stream number */
12991 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12992 error = EINVAL;
12993 goto out_unlocked;
12994 }
12995 if (asoc->strmout == NULL) {
12996 /* huh? software error */
12997 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12998 error = EFAULT;
12999 goto out_unlocked;
13000 }
13001 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13002 if ((user_marks_eor == 0) &&
13003 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13004 /* It will NEVER fit */
13005 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13006 error = EMSGSIZE;
13007 goto out_unlocked;
13008 }
13009 if ((uio == NULL) && user_marks_eor) {
13010 /*-
13011 * We do not support eeor mode for
13012 * sending with mbuf chains (like sendfile).
13013 */
13014 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13015 error = EINVAL;
13016 goto out_unlocked;
13017 }
13018 if (user_marks_eor) {
13019 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13020 } else {
13021 /*-
13022 * For non-eeor the whole message must fit in
13023 * the socket send buffer.
13024 */
13025 local_add_more = sndlen;
13026 }
13027 len = 0;
13028 if (non_blocking) {
13029 goto skip_preblock;
13030 }
13031 if (((max_len <= local_add_more) &&
13032 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13033 (max_len == 0) ||
13034 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13035 /* No room right now ! */
13036 SOCKBUF_LOCK(&so->so_snd);
13037 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13038 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13039 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13040 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13041 (unsigned int)SCTP_SB_LIMIT_SND(so),
13042 inqueue_bytes,
13043 local_add_more,
13044 stcb->asoc.stream_queue_cnt,
13045 stcb->asoc.chunks_on_out_queue,
13046 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13047 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13048 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
13049 }
13050 be.error = 0;
13051 stcb->block_entry = &be;
13052 error = sbwait(&so->so_snd);
13053 stcb->block_entry = NULL;
13054 if (error || so->so_error || be.error) {
13055 if (error == 0) {
13056 if (so->so_error)
13057 error = so->so_error;
13058 if (be.error) {
13059 error = be.error;
13060 }
13061 }
13062 SOCKBUF_UNLOCK(&so->so_snd);
13063 goto out_unlocked;
13064 }
13065 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13066 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13067 so, asoc, stcb->asoc.total_output_queue_size);
13068 }
13069 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13070 goto out_unlocked;
13071 }
13072 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13073 }
13074 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13075 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13076 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13077 } else {
13078 max_len = 0;
13079 }
13080 SOCKBUF_UNLOCK(&so->so_snd);
13081 }
13082skip_preblock:
13083 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13084 goto out_unlocked;
13085 }
13086 /*
13087 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13088 * case NOTE: uio will be null when top/mbuf is passed
13089 */
13090 if (sndlen == 0) {
13091 if (srcv->sinfo_flags & SCTP_EOF) {
13092 got_all_of_the_send = 1;
13093 goto dataless_eof;
13094 } else {
13095 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13096 error = EINVAL;
13097 goto out;
13098 }
13099 }
13100 if (top == NULL) {
13101 struct sctp_stream_queue_pending *sp;
13102 struct sctp_stream_out *strm;
13103 uint32_t sndout, initial_out;
13104
13105 initial_out = uio->uio_resid;
13106
13107 SCTP_TCB_SEND_LOCK(stcb);
13108 if ((asoc->stream_locked) &&
13109 (asoc->stream_locked_on != srcv->sinfo_stream)) {
13110 SCTP_TCB_SEND_UNLOCK(stcb);
13111 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13112 error = EINVAL;
13113 goto out;
13114 }
13115 SCTP_TCB_SEND_UNLOCK(stcb);
13116
13117 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13118 if (strm->last_msg_incomplete == 0) {
13119 do_a_copy_in:
13120 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
13121 if ((sp == NULL) || (error)) {
13122 goto out;
13123 }
13124 SCTP_TCB_SEND_LOCK(stcb);
13125 if (sp->msg_is_complete) {
13126 strm->last_msg_incomplete = 0;
13127 asoc->stream_locked = 0;
13128 } else {
13129 /*
13130 * Just got locked to this guy in case of an
13131 * interrupt.
13132 */
13133 strm->last_msg_incomplete = 1;
13134 asoc->stream_locked = 1;
13135 asoc->stream_locked_on = srcv->sinfo_stream;
13136 sp->sender_all_done = 0;
13137 }
13138 sctp_snd_sb_alloc(stcb, sp->length);
13139 atomic_add_int(&asoc->stream_queue_cnt, 1);
13140 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13141 sp->strseq = strm->next_sequence_sent;
13142 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13143 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13144 (uintptr_t) stcb, sp->length,
13145 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13146 }
13147 strm->next_sequence_sent++;
13148 } else {
13149 SCTP_STAT_INCR(sctps_sends_with_unord);
13150 }
13151 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13152 if ((strm->next_spoke.tqe_next == NULL) &&
13153 (strm->next_spoke.tqe_prev == NULL)) {
13154 /* Not on wheel, insert */
13155 sctp_insert_on_wheel(stcb, asoc, strm, 1);
13156 }
13157 SCTP_TCB_SEND_UNLOCK(stcb);
13158 } else {
13159 SCTP_TCB_SEND_LOCK(stcb);
13160 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13161 SCTP_TCB_SEND_UNLOCK(stcb);
13162 if (sp == NULL) {
13163 /* ???? Huh ??? last msg is gone */
13164#ifdef INVARIANTS
13165 panic("Warning: Last msg marked incomplete, yet nothing left?");
13166#else
13167 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13168 strm->last_msg_incomplete = 0;
13169#endif
13170 goto do_a_copy_in;
13171
13172 }
13173 }
13174 while (uio->uio_resid > 0) {
13175 /* How much room do we have? */
13176 struct mbuf *new_tail, *mm;
13177
13178 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13179 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13180 else
13181 max_len = 0;
13182
13183 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13184 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13185 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13186 sndout = 0;
13187 new_tail = NULL;
13188 if (hold_tcblock) {
13189 SCTP_TCB_UNLOCK(stcb);
13190 hold_tcblock = 0;
13191 }
13192 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13193 if ((mm == NULL) || error) {
13194 if (mm) {
13195 sctp_m_freem(mm);
13196 }
13197 goto out;
13198 }
13199 /* Update the mbuf and count */
13200 SCTP_TCB_SEND_LOCK(stcb);
13201 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13202 /*
13203 * we need to get out. Peer probably
13204 * aborted.
13205 */
13206 sctp_m_freem(mm);
13207 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13208 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13209 error = ECONNRESET;
13210 }
13211 SCTP_TCB_SEND_UNLOCK(stcb);
13212 goto out;
13213 }
13214 if (sp->tail_mbuf) {
13215 /* tack it to the end */
13216 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13217 sp->tail_mbuf = new_tail;
13218 } else {
13219 /* A stolen mbuf */
13220 sp->data = mm;
13221 sp->tail_mbuf = new_tail;
13222 }
13223 sctp_snd_sb_alloc(stcb, sndout);
13224 atomic_add_int(&sp->length, sndout);
13225 len += sndout;
13226
13227 /* Did we reach EOR? */
13228 if ((uio->uio_resid == 0) &&
13229 ((user_marks_eor == 0) ||
13230 (srcv->sinfo_flags & SCTP_EOF) ||
13231 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13232 sp->msg_is_complete = 1;
13233 } else {
13234 sp->msg_is_complete = 0;
13235 }
13236 SCTP_TCB_SEND_UNLOCK(stcb);
13237 }
13238 if (uio->uio_resid == 0) {
13239 /* got it all? */
13240 continue;
13241 }
13242 /* PR-SCTP? */
13243 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13244 /*
13245 * This is ugly but we must assure locking
13246 * order
13247 */
13248 if (hold_tcblock == 0) {
13249 SCTP_TCB_LOCK(stcb);
13250 hold_tcblock = 1;
13251 }
13252 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13253 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13254 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13255 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13256 else
13257 max_len = 0;
13258 if (max_len > 0) {
13259 continue;
13260 }
13261 SCTP_TCB_UNLOCK(stcb);
13262 hold_tcblock = 0;
13263 }
13264 /* wait for space now */
13265 if (non_blocking) {
13266 /* Non-blocking io in place out */
13267 goto skip_out_eof;
13268 }
13269 if ((net->flight_size > net->cwnd) &&
13270 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13271 queue_only = 1;
13272 } else if (asoc->ifp_had_enobuf) {
13273 SCTP_STAT_INCR(sctps_ifnomemqueued);
13274 if (net->flight_size > (net->mtu * 2)) {
13275 queue_only = 1;
13276 } else {
13277 queue_only = 0;
13278 }
13279 asoc->ifp_had_enobuf = 0;
13280 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13281 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13282 } else {
13283 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13284 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13285 if (net->flight_size > net->cwnd) {
13286 queue_only = 1;
13287 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13288 } else {
13289 queue_only = 0;
13290 }
13291 }
13292 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13293 (stcb->asoc.total_flight > 0) &&
13294 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13295 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13296
13297 /*-
13298 * Ok, Nagle is set on and we have data outstanding.
13299 * Don't send anything and let SACKs drive out the
13300 * data unless wen have a "full" segment to send.
13301 */
13302 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13303 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13304 }
13305 SCTP_STAT_INCR(sctps_naglequeued);
13306 nagle_applies = 1;
13307 } else {
13308 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13309 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13310 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13311 }
13312 SCTP_STAT_INCR(sctps_naglesent);
13313 nagle_applies = 0;
13314 }
13315 /* What about the INIT, send it maybe */
13316 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13317
13318 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13319 nagle_applies, un_sent);
13320 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13321 stcb->asoc.total_flight,
13322 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13323 }
13324 if (queue_only_for_init) {
13325 if (hold_tcblock == 0) {
13326 SCTP_TCB_LOCK(stcb);
13327 hold_tcblock = 1;
13328 }
13329 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13330 /* a collision took us forward? */
13331 queue_only_for_init = 0;
13332 queue_only = 0;
13333 } else {
13334 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13335 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13336 queue_only_for_init = 0;
13337 queue_only = 1;
13338 }
13339 }
13340 if ((queue_only == 0) && (nagle_applies == 0)) {
13341 /*-
13342 * need to start chunk output
13343 * before blocking.. note that if
13344 * a lock is already applied, then
13345 * the input via the net is happening
13346 * and I don't need to start output :-D
13347 */
13348 if (hold_tcblock == 0) {
13349 if (SCTP_TCB_TRYLOCK(stcb)) {
13350 hold_tcblock = 1;
13351 sctp_chunk_output(inp,
13352 stcb,
13353 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13354 }
13355 } else {
13356 sctp_chunk_output(inp,
13357 stcb,
13358 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13359 }
13360 if (hold_tcblock == 1) {
13361 SCTP_TCB_UNLOCK(stcb);
13362 hold_tcblock = 0;
13363 }
13364 }
13365 SOCKBUF_LOCK(&so->so_snd);
13366 /*-
13367 * This is a bit strange, but I think it will
13368 * work. The total_output_queue_size is locked and
13369 * protected by the TCB_LOCK, which we just released.
13370 * There is a race that can occur between releasing it
13371 * above, and me getting the socket lock, where sacks
13372 * come in but we have not put the SB_WAIT on the
13373 * so_snd buffer to get the wakeup. After the LOCK
13374 * is applied the sack_processing will also need to
13375 * LOCK the so->so_snd to do the actual sowwakeup(). So
13376 * once we have the socket buffer lock if we recheck the
13377 * size we KNOW we will get to sleep safely with the
13378 * wakeup flag in place.
13379 */
13380 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13381 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13382 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13383 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13384 so, asoc, uio->uio_resid);
13385 }
13386 be.error = 0;
13387 stcb->block_entry = &be;
13388 error = sbwait(&so->so_snd);
13389 stcb->block_entry = NULL;
13390
13391 if (error || so->so_error || be.error) {
13392 if (error == 0) {
13393 if (so->so_error)
13394 error = so->so_error;
13395 if (be.error) {
13396 error = be.error;
13397 }
13398 }
13399 SOCKBUF_UNLOCK(&so->so_snd);
13400 goto out_unlocked;
13401 }
13402 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13403 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13404 so, asoc, stcb->asoc.total_output_queue_size);
13405 }
13406 }
13407 SOCKBUF_UNLOCK(&so->so_snd);
13408 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13409 goto out_unlocked;
13410 }
13411 }
13412 SCTP_TCB_SEND_LOCK(stcb);
13413 if (sp) {
13414 if (sp->msg_is_complete == 0) {
13415 strm->last_msg_incomplete = 1;
13416 asoc->stream_locked = 1;
13417 asoc->stream_locked_on = srcv->sinfo_stream;
13418 } else {
13419 sp->sender_all_done = 1;
13420 strm->last_msg_incomplete = 0;
13421 asoc->stream_locked = 0;
13422 }
13423 } else {
13424 SCTP_PRINTF("Huh no sp TSNH?\n");
13425 strm->last_msg_incomplete = 0;
13426 asoc->stream_locked = 0;
13427 }
13428 SCTP_TCB_SEND_UNLOCK(stcb);
13429 if (uio->uio_resid == 0) {
13430 got_all_of_the_send = 1;
13431 }
13432 } else if (top) {
13433 /* We send in a 0, since we do NOT have any locks */
13434 error = sctp_msg_append(stcb, net, top, srcv, 0);
13435 top = NULL;
13436 if (srcv->sinfo_flags & SCTP_EOF) {
13437 /*
13438 * This should only happen for Panda for the mbuf
13439 * send case, which does NOT yet support EEOR mode.
13440 * Thus, we can just set this flag to do the proper
13441 * EOF handling.
13442 */
13443 got_all_of_the_send = 1;
13444 }
13445 }
13446 if (error) {
13447 goto out;
13448 }
13449dataless_eof:
13450 /* EOF thing ? */
13451 if ((srcv->sinfo_flags & SCTP_EOF) &&
13452 (got_all_of_the_send == 1) &&
13453 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13454 int cnt;
13455
13456 SCTP_STAT_INCR(sctps_sends_with_eof);
13457 error = 0;
13458 if (hold_tcblock == 0) {
13459 SCTP_TCB_LOCK(stcb);
13460 hold_tcblock = 1;
13461 }
13462 cnt = sctp_is_there_unsent_data(stcb);
13463 if (TAILQ_EMPTY(&asoc->send_queue) &&
13464 TAILQ_EMPTY(&asoc->sent_queue) &&
13465 (cnt == 0)) {
13466 if (asoc->locked_on_sending) {
13467 goto abort_anyway;
13468 }
13469 /* there is nothing queued to send, so I'm done... */
13470 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13471 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13472 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13473 /* only send SHUTDOWN the first time through */
13474 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13475 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13476 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13477 }
13478 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13479 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13480 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13481 asoc->primary_destination);
13482 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13483 asoc->primary_destination);
13484 }
13485 } else {
13486 /*-
13487 * we still got (or just got) data to send, so set
13488 * SHUTDOWN_PENDING
13489 */
13490 /*-
13491 * XXX sockets draft says that SCTP_EOF should be
13492 * sent with no data. currently, we will allow user
13493 * data to be sent first and move to
13494 * SHUTDOWN-PENDING
13495 */
13496 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13497 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13498 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13499 if (hold_tcblock == 0) {
13500 SCTP_TCB_LOCK(stcb);
13501 hold_tcblock = 1;
13502 }
13503 if (asoc->locked_on_sending) {
13504 /* Locked to send out the data */
13505 struct sctp_stream_queue_pending *sp;
13506
13507 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13508 if (sp) {
13509 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13510 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13511 }
13512 }
13513 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13514 if (TAILQ_EMPTY(&asoc->send_queue) &&
13515 TAILQ_EMPTY(&asoc->sent_queue) &&
13516 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13517 abort_anyway:
13518 if (free_cnt_applied) {
13519 atomic_add_int(&stcb->asoc.refcnt, -1);
13520 free_cnt_applied = 0;
13521 }
13522 sctp_abort_an_association(stcb->sctp_ep, stcb,
13523 SCTP_RESPONSE_TO_USER_REQ,
13524 NULL, SCTP_SO_LOCKED);
13525 /*
13526 * now relock the stcb so everything
13527 * is sane
13528 */
13529 hold_tcblock = 0;
13530 stcb = NULL;
13531 goto out;
13532 }
13533 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13534 asoc->primary_destination);
13535 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13536 }
13537 }
13538 }
13539skip_out_eof:
13540 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13541 some_on_control = 1;
13542 }
13543 if ((net->flight_size > net->cwnd) &&
13544 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13545 queue_only = 1;
13546 } else if (asoc->ifp_had_enobuf) {
13547 SCTP_STAT_INCR(sctps_ifnomemqueued);
13548 if (net->flight_size > (net->mtu * 2)) {
13549 queue_only = 1;
13550 } else {
13551 queue_only = 0;
13552 }
13553 asoc->ifp_had_enobuf = 0;
13554 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13555 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13556 } else {
13557 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13558 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13559 if (net->flight_size > net->cwnd) {
13560 queue_only = 1;
13561 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13562 } else {
13563 queue_only = 0;
13564 }
13565 }
13566 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13567 (stcb->asoc.total_flight > 0) &&
13568 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13569 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13570 /*-
13571 * Ok, Nagle is set on and we have data outstanding.
13572 * Don't send anything and let SACKs drive out the
13573 * data unless wen have a "full" segment to send.
13574 */
13575 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13576 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13577 }
13578 SCTP_STAT_INCR(sctps_naglequeued);
13579 nagle_applies = 1;
13580 } else {
13581 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13582 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13583 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13584 }
13585 SCTP_STAT_INCR(sctps_naglesent);
13586 nagle_applies = 0;
13587 }
13588 if (queue_only_for_init) {
13589 if (hold_tcblock == 0) {
13590 SCTP_TCB_LOCK(stcb);
13591 hold_tcblock = 1;
13592 }
13593 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13594 /* a collision took us forward? */
13595 queue_only_for_init = 0;
13596 queue_only = 0;
13597 } else {
13598 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13599 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13600 queue_only_for_init = 0;
13601 queue_only = 1;
13602 }
13603 }
13604 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13605 /* we can attempt to send too. */
13606 if (hold_tcblock == 0) {
13607 /*
13608 * If there is activity recv'ing sacks no need to
13609 * send
13610 */
13611 if (SCTP_TCB_TRYLOCK(stcb)) {
13612 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13613 hold_tcblock = 1;
13614 }
13615 } else {
13616 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13617 }
13618 } else if ((queue_only == 0) &&
13619 (stcb->asoc.peers_rwnd == 0) &&
13620 (stcb->asoc.total_flight == 0)) {
13621 /* We get to have a probe outstanding */
13622 if (hold_tcblock == 0) {
13623 hold_tcblock = 1;
13624 SCTP_TCB_LOCK(stcb);
13625 }
13626 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13627 } else if (some_on_control) {
13628 int num_out, reason, frag_point;
13629
13630 /* Here we do control only */
13631 if (hold_tcblock == 0) {
13632 hold_tcblock = 1;
13633 SCTP_TCB_LOCK(stcb);
13634 }
13635 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13636 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13637 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13638 }
13639 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13640 queue_only, stcb->asoc.peers_rwnd, un_sent,
13641 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13642 stcb->asoc.total_output_queue_size, error);
13643
13644out:
13645out_unlocked:
13646
13647 if (local_soresv && stcb) {
13648 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13649 local_soresv = 0;
13650 }
13651 if (create_lock_applied) {
13652 SCTP_ASOC_CREATE_UNLOCK(inp);
13653 create_lock_applied = 0;
13654 }
13655 if ((stcb) && hold_tcblock) {
13656 SCTP_TCB_UNLOCK(stcb);
13657 }
13658 if (stcb && free_cnt_applied) {
13659 atomic_add_int(&stcb->asoc.refcnt, -1);
13660 }
13661#ifdef INVARIANTS
13662 if (stcb) {
13663 if (mtx_owned(&stcb->tcb_mtx)) {
13664 panic("Leaving with tcb mtx owned?");
13665 }
13666 if (mtx_owned(&stcb->tcb_send_mtx)) {
13667 panic("Leaving with tcb send mtx owned?");
13668 }
13669 }
13670#endif
13671 if (top) {
13672 sctp_m_freem(top);
13673 }
13674 if (control) {
13675 sctp_m_freem(control);
13676 }
13677 return (error);
13678}
13679
13680
13681/*
13682 * generate an AUTHentication chunk, if required
13683 */
13684struct mbuf *
13685sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13686 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13687 struct sctp_tcb *stcb, uint8_t chunk)
13688{
13689 struct mbuf *m_auth;
13690 struct sctp_auth_chunk *auth;
13691 int chunk_len;
13692
13693 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13694 (stcb == NULL))
13695 return (m);
13696
13697 /* sysctl disabled auth? */
13698 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13699 return (m);
13700
13701 /* peer doesn't do auth... */
13702 if (!stcb->asoc.peer_supports_auth) {
13703 return (m);
13704 }
13705 /* does the requested chunk require auth? */
13706 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13707 return (m);
13708 }
13709 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13710 if (m_auth == NULL) {
13711 /* no mbuf's */
13712 return (m);
13713 }
13714 /* reserve some space if this will be the first mbuf */
13715 if (m == NULL)
13716 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13717 /* fill in the AUTH chunk details */
13718 auth = mtod(m_auth, struct sctp_auth_chunk *);
13719 bzero(auth, sizeof(*auth));
13720 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13721 auth->ch.chunk_flags = 0;
13722 chunk_len = sizeof(*auth) +
13723 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13724 auth->ch.chunk_length = htons(chunk_len);
13725 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13726 /* key id and hmac digest will be computed and filled in upon send */
13727
13728 /* save the offset where the auth was inserted into the chain */
13729 if (m != NULL) {
13730 struct mbuf *cn;
13731
13732 *offset = 0;
13733 cn = m;
13734 while (cn) {
13735 *offset += SCTP_BUF_LEN(cn);
13736 cn = SCTP_BUF_NEXT(cn);
13737 }
13738 } else
13739 *offset = 0;
13740
13741 /* update length and return pointer to the auth chunk */
13742 SCTP_BUF_LEN(m_auth) = chunk_len;
13743 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13744 if (auth_ret != NULL)
13745 *auth_ret = auth;
13746
13747 return (m);
13748}
13749
13750#ifdef INET6
13751int
13752sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13753{
13754 struct nd_prefix *pfx = NULL;
13755 struct nd_pfxrouter *pfxrtr = NULL;
13756 struct sockaddr_in6 gw6;
13757
13758 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13759 return (0);
13760
13761 /* get prefix entry of address */
12018 if (port) {
12019 ip6_out->ip6_nxt = IPPROTO_UDP;
12020 } else {
12021 ip6_out->ip6_nxt = IPPROTO_SCTP;
12022 }
12023 ip6_out->ip6_src = ip6->ip6_dst;
12024 ip6_out->ip6_dst = ip6->ip6_src;
12025
12026 iphlen_out = sizeof(struct ip6_hdr);
12027 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12028 break;
12029#endif /* INET6 */
12030 default:
12031 /* Currently not supported */
12032 sctp_m_freem(mout);
12033 return;
12034 }
12035
12036 udp = (struct udphdr *)sh_out;
12037 if (port) {
12038 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12039 udp->uh_dport = port;
12040 /* set udp->uh_ulen later */
12041 udp->uh_sum = 0;
12042 iphlen_out += sizeof(struct udphdr);
12043 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12044 }
12045 sh_out->src_port = sh->dest_port;
12046 sh_out->dest_port = sh->src_port;
12047 sh_out->v_tag = vtag;
12048 sh_out->checksum = 0;
12049
12050 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12051 ch->chunk_type = SCTP_OPERATION_ERROR;
12052 ch->chunk_flags = 0;
12053
12054 if (scm) {
12055 struct mbuf *m_tmp = scm;
12056 int cause_len = 0;
12057
12058 /* get length of the err_cause chain */
12059 while (m_tmp != NULL) {
12060 cause_len += SCTP_BUF_LEN(m_tmp);
12061 m_tmp = SCTP_BUF_NEXT(m_tmp);
12062 }
12063 len = SCTP_BUF_LEN(mout) + cause_len;
12064 if (cause_len % 4) {
12065 /* need pad at end of chunk */
12066 uint32_t cpthis = 0;
12067 int padlen;
12068
12069 padlen = 4 - (len % 4);
12070 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12071 len += padlen;
12072 }
12073 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12074 } else {
12075 len = SCTP_BUF_LEN(mout);
12076 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12077 }
12078
12079 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12080 /* no mbuf's */
12081 sctp_m_freem(mout);
12082 return;
12083 }
12084 if (iph_out != NULL) {
12085 sctp_route_t ro;
12086 struct sctp_tcb *stcb = NULL;
12087 int ret;
12088
12089 /* zap the stack pointer to the route */
12090 bzero(&ro, sizeof ro);
12091 if (port) {
12092 udp->uh_ulen = htons(len - sizeof(struct ip));
12093 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12094 }
12095 /* set IPv4 length */
12096 iph_out->ip_len = len;
12097 /* out it goes */
12098#ifdef SCTP_PACKET_LOGGING
12099 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12100 sctp_packet_log(mout, len);
12101#endif
12102 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12103 if (port) {
12104 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12105 SCTP_STAT_INCR(sctps_sendswcrc);
12106 SCTP_ENABLE_UDP_CSUM(o_pak);
12107 } else {
12108 mout->m_pkthdr.csum_flags = CSUM_SCTP;
12109 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
12110 SCTP_STAT_INCR(sctps_sendhwcrc);
12111 }
12112 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12113
12114 /* Free the route if we got one back */
12115 if (ro.ro_rt)
12116 RTFREE(ro.ro_rt);
12117 }
12118#ifdef INET6
12119 if (ip6_out != NULL) {
12120 struct route_in6 ro;
12121 int ret;
12122 struct sctp_tcb *stcb = NULL;
12123 struct ifnet *ifp = NULL;
12124
12125 /* zap the stack pointer to the route */
12126 bzero(&ro, sizeof(ro));
12127 if (port) {
12128 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12129 }
12130 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12131#ifdef SCTP_PACKET_LOGGING
12132 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12133 sctp_packet_log(mout, len);
12134#endif
12135 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12136 SCTP_STAT_INCR(sctps_sendswcrc);
12137 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12138 if (port) {
12139 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12140 udp->uh_sum = 0xffff;
12141 }
12142 }
12143 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12144
12145 /* Free the route if we got one back */
12146 if (ro.ro_rt)
12147 RTFREE(ro.ro_rt);
12148 }
12149#endif
12150 SCTP_STAT_INCR(sctps_sendpackets);
12151 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12152 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12153}
12154
12155static struct mbuf *
12156sctp_copy_resume(struct sctp_stream_queue_pending *sp,
12157 struct uio *uio,
12158 struct sctp_sndrcvinfo *srcv,
12159 int max_send_len,
12160 int user_marks_eor,
12161 int *error,
12162 uint32_t * sndout,
12163 struct mbuf **new_tail)
12164{
12165 struct mbuf *m;
12166
12167 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12168 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12169 if (m == NULL) {
12170 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12171 *error = ENOMEM;
12172 } else {
12173 *sndout = m_length(m, NULL);
12174 *new_tail = m_last(m);
12175 }
12176 return (m);
12177}
12178
12179static int
12180sctp_copy_one(struct sctp_stream_queue_pending *sp,
12181 struct uio *uio,
12182 int resv_upfront)
12183{
12184 int left;
12185
12186 left = sp->length;
12187 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12188 resv_upfront, 0);
12189 if (sp->data == NULL) {
12190 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12191 return (ENOMEM);
12192 }
12193 sp->tail_mbuf = m_last(sp->data);
12194 return (0);
12195}
12196
12197
12198
12199static struct sctp_stream_queue_pending *
12200sctp_copy_it_in(struct sctp_tcb *stcb,
12201 struct sctp_association *asoc,
12202 struct sctp_sndrcvinfo *srcv,
12203 struct uio *uio,
12204 struct sctp_nets *net,
12205 int max_send_len,
12206 int user_marks_eor,
12207 int *error,
12208 int non_blocking)
12209{
12210 /*-
12211 * This routine must be very careful in its work. Protocol
12212 * processing is up and running so care must be taken to spl...()
12213 * when you need to do something that may effect the stcb/asoc. The
12214 * sb is locked however. When data is copied the protocol processing
12215 * should be enabled since this is a slower operation...
12216 */
12217 struct sctp_stream_queue_pending *sp = NULL;
12218 int resv_in_first;
12219
12220 *error = 0;
12221 /* Now can we send this? */
12222 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12223 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12224 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12225 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12226 /* got data while shutting down */
12227 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12228 *error = ECONNRESET;
12229 goto out_now;
12230 }
12231 sctp_alloc_a_strmoq(stcb, sp);
12232 if (sp == NULL) {
12233 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12234 *error = ENOMEM;
12235 goto out_now;
12236 }
12237 sp->act_flags = 0;
12238 sp->sender_all_done = 0;
12239 sp->sinfo_flags = srcv->sinfo_flags;
12240 sp->timetolive = srcv->sinfo_timetolive;
12241 sp->ppid = srcv->sinfo_ppid;
12242 sp->context = srcv->sinfo_context;
12243 sp->strseq = 0;
12244 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12245
12246 sp->stream = srcv->sinfo_stream;
12247 sp->length = min(uio->uio_resid, max_send_len);
12248 if ((sp->length == (uint32_t) uio->uio_resid) &&
12249 ((user_marks_eor == 0) ||
12250 (srcv->sinfo_flags & SCTP_EOF) ||
12251 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12252 sp->msg_is_complete = 1;
12253 } else {
12254 sp->msg_is_complete = 0;
12255 }
12256 sp->sender_all_done = 0;
12257 sp->some_taken = 0;
12258 sp->put_last_out = 0;
12259 resv_in_first = sizeof(struct sctp_data_chunk);
12260 sp->data = sp->tail_mbuf = NULL;
12261 if (sp->length == 0) {
12262 *error = 0;
12263 goto skip_copy;
12264 }
12265 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12266 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12267 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12268 sp->holds_key_ref = 1;
12269 }
12270 *error = sctp_copy_one(sp, uio, resv_in_first);
12271skip_copy:
12272 if (*error) {
12273 sctp_free_a_strmoq(stcb, sp);
12274 sp = NULL;
12275 } else {
12276 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12277 sp->net = net;
12278 sp->addr_over = 1;
12279 } else {
12280 sp->net = asoc->primary_destination;
12281 sp->addr_over = 0;
12282 }
12283 atomic_add_int(&sp->net->ref_count, 1);
12284 sctp_set_prsctp_policy(sp);
12285 }
12286out_now:
12287 return (sp);
12288}
12289
12290
12291int
12292sctp_sosend(struct socket *so,
12293 struct sockaddr *addr,
12294 struct uio *uio,
12295 struct mbuf *top,
12296 struct mbuf *control,
12297 int flags,
12298 struct thread *p
12299)
12300{
12301 struct sctp_inpcb *inp;
12302 int error, use_rcvinfo = 0;
12303 struct sctp_sndrcvinfo srcv;
12304 struct sockaddr *addr_to_use;
12305
12306#ifdef INET6
12307 struct sockaddr_in sin;
12308
12309#endif
12310
12311 inp = (struct sctp_inpcb *)so->so_pcb;
12312 if (control) {
12313 /* process cmsg snd/rcv info (maybe a assoc-id) */
12314 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12315 sizeof(srcv))) {
12316 /* got one */
12317 use_rcvinfo = 1;
12318 }
12319 }
12320 addr_to_use = addr;
12321#if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
12322 if ((addr) && (addr->sa_family == AF_INET6)) {
12323 struct sockaddr_in6 *sin6;
12324
12325 sin6 = (struct sockaddr_in6 *)addr;
12326 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12327 in6_sin6_2_sin(&sin, sin6);
12328 addr_to_use = (struct sockaddr *)&sin;
12329 }
12330 }
12331#endif
12332 error = sctp_lower_sosend(so, addr_to_use, uio, top,
12333 control,
12334 flags,
12335 use_rcvinfo, &srcv
12336 ,p
12337 );
12338 return (error);
12339}
12340
12341
12342int
12343sctp_lower_sosend(struct socket *so,
12344 struct sockaddr *addr,
12345 struct uio *uio,
12346 struct mbuf *i_pak,
12347 struct mbuf *control,
12348 int flags,
12349 int use_rcvinfo,
12350 struct sctp_sndrcvinfo *srcv
12351 ,
12352 struct thread *p
12353)
12354{
12355 unsigned int sndlen = 0, max_len;
12356 int error, len;
12357 struct mbuf *top = NULL;
12358 int queue_only = 0, queue_only_for_init = 0;
12359 int free_cnt_applied = 0;
12360 int un_sent = 0;
12361 int now_filled = 0;
12362 unsigned int inqueue_bytes = 0;
12363 struct sctp_block_entry be;
12364 struct sctp_inpcb *inp;
12365 struct sctp_tcb *stcb = NULL;
12366 struct timeval now;
12367 struct sctp_nets *net;
12368 struct sctp_association *asoc;
12369 struct sctp_inpcb *t_inp;
12370 int user_marks_eor;
12371 int create_lock_applied = 0;
12372 int nagle_applies = 0;
12373 int some_on_control = 0;
12374 int got_all_of_the_send = 0;
12375 int hold_tcblock = 0;
12376 int non_blocking = 0;
12377 int temp_flags = 0;
12378 uint32_t local_add_more, local_soresv = 0;
12379
12380 error = 0;
12381 net = NULL;
12382 stcb = NULL;
12383 asoc = NULL;
12384
12385 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12386 if (inp == NULL) {
12387 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12388 error = EFAULT;
12389 if (i_pak) {
12390 SCTP_RELEASE_PKT(i_pak);
12391 }
12392 return (error);
12393 }
12394 if ((uio == NULL) && (i_pak == NULL)) {
12395 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12396 return (EINVAL);
12397 }
12398 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12399 atomic_add_int(&inp->total_sends, 1);
12400 if (uio) {
12401 if (uio->uio_resid < 0) {
12402 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12403 return (EINVAL);
12404 }
12405 sndlen = uio->uio_resid;
12406 } else {
12407 top = SCTP_HEADER_TO_CHAIN(i_pak);
12408 sndlen = SCTP_HEADER_LEN(i_pak);
12409 }
12410 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12411 addr,
12412 sndlen);
12413 /*-
12414 * Pre-screen address, if one is given the sin-len
12415 * must be set correctly!
12416 */
12417 if (addr) {
12418 if ((addr->sa_family == AF_INET) &&
12419 (addr->sa_len != sizeof(struct sockaddr_in))) {
12420 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12421 error = EINVAL;
12422 goto out_unlocked;
12423 } else if ((addr->sa_family == AF_INET6) &&
12424 (addr->sa_len != sizeof(struct sockaddr_in6))) {
12425 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12426 error = EINVAL;
12427 goto out_unlocked;
12428 }
12429 }
12430 hold_tcblock = 0;
12431
12432 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12433 (inp->sctp_socket->so_qlimit)) {
12434 /* The listener can NOT send */
12435 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12436 error = EFAULT;
12437 goto out_unlocked;
12438 }
12439 if ((use_rcvinfo) && srcv) {
12440 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12441 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12442 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12443 error = EINVAL;
12444 goto out_unlocked;
12445 }
12446 if (srcv->sinfo_flags)
12447 SCTP_STAT_INCR(sctps_sends_with_flags);
12448
12449 if (srcv->sinfo_flags & SCTP_SENDALL) {
12450 /* its a sendall */
12451 error = sctp_sendall(inp, uio, top, srcv);
12452 top = NULL;
12453 goto out_unlocked;
12454 }
12455 }
12456 /* now we must find the assoc */
12457 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12458 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12459 SCTP_INP_RLOCK(inp);
12460 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12461 if (stcb == NULL) {
12462 SCTP_INP_RUNLOCK(inp);
12463 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12464 error = ENOTCONN;
12465 goto out_unlocked;
12466 }
12467 SCTP_TCB_LOCK(stcb);
12468 hold_tcblock = 1;
12469 SCTP_INP_RUNLOCK(inp);
12470 if (addr) {
12471 /* Must locate the net structure if addr given */
12472 net = sctp_findnet(stcb, addr);
12473 if (net) {
12474 /* validate port was 0 or correct */
12475 struct sockaddr_in *sin;
12476
12477 sin = (struct sockaddr_in *)addr;
12478 if ((sin->sin_port != 0) &&
12479 (sin->sin_port != stcb->rport)) {
12480 net = NULL;
12481 }
12482 }
12483 temp_flags |= SCTP_ADDR_OVER;
12484 } else
12485 net = stcb->asoc.primary_destination;
12486 if (addr && (net == NULL)) {
12487 /* Could not find address, was it legal */
12488 if (addr->sa_family == AF_INET) {
12489 struct sockaddr_in *sin;
12490
12491 sin = (struct sockaddr_in *)addr;
12492 if (sin->sin_addr.s_addr == 0) {
12493 if ((sin->sin_port == 0) ||
12494 (sin->sin_port == stcb->rport)) {
12495 net = stcb->asoc.primary_destination;
12496 }
12497 }
12498 } else {
12499 struct sockaddr_in6 *sin6;
12500
12501 sin6 = (struct sockaddr_in6 *)addr;
12502 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12503 if ((sin6->sin6_port == 0) ||
12504 (sin6->sin6_port == stcb->rport)) {
12505 net = stcb->asoc.primary_destination;
12506 }
12507 }
12508 }
12509 }
12510 if (net == NULL) {
12511 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12512 error = EINVAL;
12513 goto out_unlocked;
12514 }
12515 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12516 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12517 if (stcb) {
12518 if (addr)
12519 /*
12520 * Must locate the net structure if addr
12521 * given
12522 */
12523 net = sctp_findnet(stcb, addr);
12524 else
12525 net = stcb->asoc.primary_destination;
12526 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12527 ((net == NULL) || (addr == NULL))) {
12528 struct sockaddr_in *sin;
12529
12530 if (addr == NULL) {
12531 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12532 error = EINVAL;
12533 goto out_unlocked;
12534 }
12535 sin = (struct sockaddr_in *)addr;
12536 /* Validate port is 0 or correct */
12537 if ((sin->sin_port != 0) &&
12538 (sin->sin_port != stcb->rport)) {
12539 net = NULL;
12540 }
12541 }
12542 }
12543 hold_tcblock = 0;
12544 } else if (addr) {
12545 /*-
12546 * Since we did not use findep we must
12547 * increment it, and if we don't find a tcb
12548 * decrement it.
12549 */
12550 SCTP_INP_WLOCK(inp);
12551 SCTP_INP_INCR_REF(inp);
12552 SCTP_INP_WUNLOCK(inp);
12553 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12554 if (stcb == NULL) {
12555 SCTP_INP_WLOCK(inp);
12556 SCTP_INP_DECR_REF(inp);
12557 SCTP_INP_WUNLOCK(inp);
12558 } else {
12559 hold_tcblock = 1;
12560 }
12561 }
12562 if ((stcb == NULL) && (addr)) {
12563 /* Possible implicit send? */
12564 SCTP_ASOC_CREATE_LOCK(inp);
12565 create_lock_applied = 1;
12566 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12567 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12568 /* Should I really unlock ? */
12569 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12570 error = EFAULT;
12571 goto out_unlocked;
12572
12573 }
12574 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12575 (addr->sa_family == AF_INET6)) {
12576 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12577 error = EINVAL;
12578 goto out_unlocked;
12579 }
12580 SCTP_INP_WLOCK(inp);
12581 SCTP_INP_INCR_REF(inp);
12582 SCTP_INP_WUNLOCK(inp);
12583 /* With the lock applied look again */
12584 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12585 if (stcb == NULL) {
12586 SCTP_INP_WLOCK(inp);
12587 SCTP_INP_DECR_REF(inp);
12588 SCTP_INP_WUNLOCK(inp);
12589 } else {
12590 hold_tcblock = 1;
12591 }
12592 if (t_inp != inp) {
12593 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12594 error = ENOTCONN;
12595 goto out_unlocked;
12596 }
12597 }
12598 if (stcb == NULL) {
12599 if (addr == NULL) {
12600 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12601 error = ENOENT;
12602 goto out_unlocked;
12603 } else {
12604 /*
12605 * UDP style, we must go ahead and start the INIT
12606 * process
12607 */
12608 uint32_t vrf_id;
12609
12610 if ((use_rcvinfo) && (srcv) &&
12611 ((srcv->sinfo_flags & SCTP_ABORT) ||
12612 ((srcv->sinfo_flags & SCTP_EOF) &&
12613 (sndlen == 0)))) {
12614 /*-
12615 * User asks to abort a non-existant assoc,
12616 * or EOF a non-existant assoc with no data
12617 */
12618 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12619 error = ENOENT;
12620 goto out_unlocked;
12621 }
12622 /* get an asoc/stcb struct */
12623 vrf_id = inp->def_vrf_id;
12624#ifdef INVARIANTS
12625 if (create_lock_applied == 0) {
12626 panic("Error, should hold create lock and I don't?");
12627 }
12628#endif
12629 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12630 p
12631 );
12632 if (stcb == NULL) {
12633 /* Error is setup for us in the call */
12634 goto out_unlocked;
12635 }
12636 if (create_lock_applied) {
12637 SCTP_ASOC_CREATE_UNLOCK(inp);
12638 create_lock_applied = 0;
12639 } else {
12640 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12641 }
12642 /*
12643 * Turn on queue only flag to prevent data from
12644 * being sent
12645 */
12646 queue_only = 1;
12647 asoc = &stcb->asoc;
12648 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12649 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12650
12651 /* initialize authentication params for the assoc */
12652 sctp_initialize_auth_params(inp, stcb);
12653
12654 if (control) {
12655 /*
12656 * see if a init structure exists in cmsg
12657 * headers
12658 */
12659 struct sctp_initmsg initm;
12660 int i;
12661
12662 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12663 sizeof(initm))) {
12664 /*
12665 * we have an INIT override of the
12666 * default
12667 */
12668 if (initm.sinit_max_attempts)
12669 asoc->max_init_times = initm.sinit_max_attempts;
12670 if (initm.sinit_num_ostreams)
12671 asoc->pre_open_streams = initm.sinit_num_ostreams;
12672 if (initm.sinit_max_instreams)
12673 asoc->max_inbound_streams = initm.sinit_max_instreams;
12674 if (initm.sinit_max_init_timeo)
12675 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12676 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12677 struct sctp_stream_out *tmp_str;
12678 int had_lock = 0;
12679
12680 /* Default is NOT correct */
12681 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12682 asoc->streamoutcnt, asoc->pre_open_streams);
12683 /*
12684 * What happens if this
12685 * fails? we panic ...
12686 */
12687
12688 if (hold_tcblock) {
12689 had_lock = 1;
12690 SCTP_TCB_UNLOCK(stcb);
12691 }
12692 SCTP_MALLOC(tmp_str,
12693 struct sctp_stream_out *,
12694 (asoc->pre_open_streams *
12695 sizeof(struct sctp_stream_out)),
12696 SCTP_M_STRMO);
12697 if (had_lock) {
12698 SCTP_TCB_LOCK(stcb);
12699 }
12700 if (tmp_str != NULL) {
12701 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12702 asoc->strmout = tmp_str;
12703 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12704 } else {
12705 asoc->pre_open_streams = asoc->streamoutcnt;
12706 }
12707 for (i = 0; i < asoc->streamoutcnt; i++) {
12708 /*-
12709 * inbound side must be set
12710 * to 0xffff, also NOTE when
12711 * we get the INIT-ACK back
12712 * (for INIT sender) we MUST
12713 * reduce the count
12714 * (streamoutcnt) but first
12715 * check if we sent to any
12716 * of the upper streams that
12717 * were dropped (if some
12718 * were). Those that were
12719 * dropped must be notified
12720 * to the upper layer as
12721 * failed to send.
12722 */
12723 asoc->strmout[i].next_sequence_sent = 0x0;
12724 TAILQ_INIT(&asoc->strmout[i].outqueue);
12725 asoc->strmout[i].stream_no = i;
12726 asoc->strmout[i].last_msg_incomplete = 0;
12727 asoc->strmout[i].next_spoke.tqe_next = 0;
12728 asoc->strmout[i].next_spoke.tqe_prev = 0;
12729 }
12730 }
12731 }
12732 }
12733 hold_tcblock = 1;
12734 /* out with the INIT */
12735 queue_only_for_init = 1;
12736 /*-
12737 * we may want to dig in after this call and adjust the MTU
12738 * value. It defaulted to 1500 (constant) but the ro
12739 * structure may now have an update and thus we may need to
12740 * change it BEFORE we append the message.
12741 */
12742 net = stcb->asoc.primary_destination;
12743 asoc = &stcb->asoc;
12744 }
12745 }
12746 if ((SCTP_SO_IS_NBIO(so)
12747 || (flags & MSG_NBIO)
12748 )) {
12749 non_blocking = 1;
12750 }
12751 asoc = &stcb->asoc;
12752 atomic_add_int(&stcb->total_sends, 1);
12753
12754 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12755 if (sndlen > asoc->smallest_mtu) {
12756 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12757 error = EMSGSIZE;
12758 goto out_unlocked;
12759 }
12760 }
12761 /* would we block? */
12762 if (non_blocking) {
12763 if (hold_tcblock == 0) {
12764 SCTP_TCB_LOCK(stcb);
12765 hold_tcblock = 1;
12766 }
12767 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12768 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12769 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12770 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12771 if (sndlen > SCTP_SB_LIMIT_SND(so))
12772 error = EMSGSIZE;
12773 else
12774 error = EWOULDBLOCK;
12775 goto out_unlocked;
12776 }
12777 stcb->asoc.sb_send_resv += sndlen;
12778 SCTP_TCB_UNLOCK(stcb);
12779 hold_tcblock = 0;
12780 } else {
12781 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12782 }
12783 local_soresv = sndlen;
12784 /* Keep the stcb from being freed under our feet */
12785 if (free_cnt_applied) {
12786#ifdef INVARIANTS
12787 panic("refcnt already incremented");
12788#else
12789 printf("refcnt:1 already incremented?\n");
12790#endif
12791 } else {
12792 atomic_add_int(&stcb->asoc.refcnt, 1);
12793 free_cnt_applied = 1;
12794 }
12795 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12796 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12797 error = ECONNRESET;
12798 goto out_unlocked;
12799 }
12800 if (create_lock_applied) {
12801 SCTP_ASOC_CREATE_UNLOCK(inp);
12802 create_lock_applied = 0;
12803 }
12804 if (asoc->stream_reset_outstanding) {
12805 /*
12806 * Can't queue any data while stream reset is underway.
12807 */
12808 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12809 error = EAGAIN;
12810 goto out_unlocked;
12811 }
12812 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12813 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12814 queue_only = 1;
12815 }
12816 if ((use_rcvinfo == 0) || (srcv == NULL)) {
12817 /* Grab the default stuff from the asoc */
12818 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12819 }
12820 /* we are now done with all control */
12821 if (control) {
12822 sctp_m_freem(control);
12823 control = NULL;
12824 }
12825 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12826 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12827 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12828 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12829 if ((use_rcvinfo) &&
12830 (srcv->sinfo_flags & SCTP_ABORT)) {
12831 ;
12832 } else {
12833 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12834 error = ECONNRESET;
12835 goto out_unlocked;
12836 }
12837 }
12838 /* Ok, we will attempt a msgsnd :> */
12839 if (p) {
12840 p->td_ru.ru_msgsnd++;
12841 }
12842 if (stcb) {
12843 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12844 net = stcb->asoc.primary_destination;
12845 }
12846 }
12847 if (net == NULL) {
12848 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12849 error = EINVAL;
12850 goto out_unlocked;
12851 }
12852 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12853 /*-
12854 * CMT: Added check for CMT above. net above is the primary
12855 * dest. If CMT is ON, sender should always attempt to send
12856 * with the output routine sctp_fill_outqueue() that loops
12857 * through all destination addresses. Therefore, if CMT is
12858 * ON, queue_only is NOT set to 1 here, so that
12859 * sctp_chunk_output() can be called below.
12860 */
12861 queue_only = 1;
12862 } else if (asoc->ifp_had_enobuf) {
12863 SCTP_STAT_INCR(sctps_ifnomemqueued);
12864 if (net->flight_size > (net->mtu * 2))
12865 queue_only = 1;
12866 asoc->ifp_had_enobuf = 0;
12867 } else {
12868 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12869 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12870 }
12871 /* Are we aborting? */
12872 if (srcv->sinfo_flags & SCTP_ABORT) {
12873 struct mbuf *mm;
12874 int tot_demand, tot_out = 0, max_out;
12875
12876 SCTP_STAT_INCR(sctps_sends_with_abort);
12877 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12878 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12879 /* It has to be up before we abort */
12880 /* how big is the user initiated abort? */
12881 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12882 error = EINVAL;
12883 goto out;
12884 }
12885 if (hold_tcblock) {
12886 SCTP_TCB_UNLOCK(stcb);
12887 hold_tcblock = 0;
12888 }
12889 if (top) {
12890 struct mbuf *cntm = NULL;
12891
12892 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12893 if (sndlen != 0) {
12894 cntm = top;
12895 while (cntm) {
12896 tot_out += SCTP_BUF_LEN(cntm);
12897 cntm = SCTP_BUF_NEXT(cntm);
12898 }
12899 }
12900 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12901 } else {
12902 /* Must fit in a MTU */
12903 tot_out = sndlen;
12904 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12905 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12906 /* To big */
12907 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12908 error = EMSGSIZE;
12909 goto out;
12910 }
12911 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12912 }
12913 if (mm == NULL) {
12914 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12915 error = ENOMEM;
12916 goto out;
12917 }
12918 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12919 max_out -= sizeof(struct sctp_abort_msg);
12920 if (tot_out > max_out) {
12921 tot_out = max_out;
12922 }
12923 if (mm) {
12924 struct sctp_paramhdr *ph;
12925
12926 /* now move forward the data pointer */
12927 ph = mtod(mm, struct sctp_paramhdr *);
12928 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12929 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12930 ph++;
12931 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12932 if (top == NULL) {
12933 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12934 if (error) {
12935 /*-
12936 * Here if we can't get his data we
12937 * still abort we just don't get to
12938 * send the users note :-0
12939 */
12940 sctp_m_freem(mm);
12941 mm = NULL;
12942 }
12943 } else {
12944 if (sndlen != 0) {
12945 SCTP_BUF_NEXT(mm) = top;
12946 }
12947 }
12948 }
12949 if (hold_tcblock == 0) {
12950 SCTP_TCB_LOCK(stcb);
12951 hold_tcblock = 1;
12952 }
12953 atomic_add_int(&stcb->asoc.refcnt, -1);
12954 free_cnt_applied = 0;
12955 /* release this lock, otherwise we hang on ourselves */
12956 sctp_abort_an_association(stcb->sctp_ep, stcb,
12957 SCTP_RESPONSE_TO_USER_REQ,
12958 mm, SCTP_SO_LOCKED);
12959 /* now relock the stcb so everything is sane */
12960 hold_tcblock = 0;
12961 stcb = NULL;
12962 /*
12963 * In this case top is already chained to mm avoid double
12964 * free, since we free it below if top != NULL and driver
12965 * would free it after sending the packet out
12966 */
12967 if (sndlen != 0) {
12968 top = NULL;
12969 }
12970 goto out_unlocked;
12971 }
12972 /* Calculate the maximum we can send */
12973 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12974 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12975 if (non_blocking) {
12976 /* we already checked for non-blocking above. */
12977 max_len = sndlen;
12978 } else {
12979 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12980 }
12981 } else {
12982 max_len = 0;
12983 }
12984 if (hold_tcblock) {
12985 SCTP_TCB_UNLOCK(stcb);
12986 hold_tcblock = 0;
12987 }
12988 /* Is the stream no. valid? */
12989 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12990 /* Invalid stream number */
12991 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12992 error = EINVAL;
12993 goto out_unlocked;
12994 }
12995 if (asoc->strmout == NULL) {
12996 /* huh? software error */
12997 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12998 error = EFAULT;
12999 goto out_unlocked;
13000 }
13001 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13002 if ((user_marks_eor == 0) &&
13003 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13004 /* It will NEVER fit */
13005 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13006 error = EMSGSIZE;
13007 goto out_unlocked;
13008 }
13009 if ((uio == NULL) && user_marks_eor) {
13010 /*-
13011 * We do not support eeor mode for
13012 * sending with mbuf chains (like sendfile).
13013 */
13014 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13015 error = EINVAL;
13016 goto out_unlocked;
13017 }
13018 if (user_marks_eor) {
13019 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13020 } else {
13021 /*-
13022 * For non-eeor the whole message must fit in
13023 * the socket send buffer.
13024 */
13025 local_add_more = sndlen;
13026 }
13027 len = 0;
13028 if (non_blocking) {
13029 goto skip_preblock;
13030 }
13031 if (((max_len <= local_add_more) &&
13032 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13033 (max_len == 0) ||
13034 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13035 /* No room right now ! */
13036 SOCKBUF_LOCK(&so->so_snd);
13037 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13038 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13039 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13040 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13041 (unsigned int)SCTP_SB_LIMIT_SND(so),
13042 inqueue_bytes,
13043 local_add_more,
13044 stcb->asoc.stream_queue_cnt,
13045 stcb->asoc.chunks_on_out_queue,
13046 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13047 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13048 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
13049 }
13050 be.error = 0;
13051 stcb->block_entry = &be;
13052 error = sbwait(&so->so_snd);
13053 stcb->block_entry = NULL;
13054 if (error || so->so_error || be.error) {
13055 if (error == 0) {
13056 if (so->so_error)
13057 error = so->so_error;
13058 if (be.error) {
13059 error = be.error;
13060 }
13061 }
13062 SOCKBUF_UNLOCK(&so->so_snd);
13063 goto out_unlocked;
13064 }
13065 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13066 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13067 so, asoc, stcb->asoc.total_output_queue_size);
13068 }
13069 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13070 goto out_unlocked;
13071 }
13072 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13073 }
13074 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13075 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13076 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13077 } else {
13078 max_len = 0;
13079 }
13080 SOCKBUF_UNLOCK(&so->so_snd);
13081 }
13082skip_preblock:
13083 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13084 goto out_unlocked;
13085 }
13086 /*
13087 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13088 * case NOTE: uio will be null when top/mbuf is passed
13089 */
13090 if (sndlen == 0) {
13091 if (srcv->sinfo_flags & SCTP_EOF) {
13092 got_all_of_the_send = 1;
13093 goto dataless_eof;
13094 } else {
13095 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13096 error = EINVAL;
13097 goto out;
13098 }
13099 }
13100 if (top == NULL) {
13101 struct sctp_stream_queue_pending *sp;
13102 struct sctp_stream_out *strm;
13103 uint32_t sndout, initial_out;
13104
13105 initial_out = uio->uio_resid;
13106
13107 SCTP_TCB_SEND_LOCK(stcb);
13108 if ((asoc->stream_locked) &&
13109 (asoc->stream_locked_on != srcv->sinfo_stream)) {
13110 SCTP_TCB_SEND_UNLOCK(stcb);
13111 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13112 error = EINVAL;
13113 goto out;
13114 }
13115 SCTP_TCB_SEND_UNLOCK(stcb);
13116
13117 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13118 if (strm->last_msg_incomplete == 0) {
13119 do_a_copy_in:
13120 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
13121 if ((sp == NULL) || (error)) {
13122 goto out;
13123 }
13124 SCTP_TCB_SEND_LOCK(stcb);
13125 if (sp->msg_is_complete) {
13126 strm->last_msg_incomplete = 0;
13127 asoc->stream_locked = 0;
13128 } else {
13129 /*
13130 * Just got locked to this guy in case of an
13131 * interrupt.
13132 */
13133 strm->last_msg_incomplete = 1;
13134 asoc->stream_locked = 1;
13135 asoc->stream_locked_on = srcv->sinfo_stream;
13136 sp->sender_all_done = 0;
13137 }
13138 sctp_snd_sb_alloc(stcb, sp->length);
13139 atomic_add_int(&asoc->stream_queue_cnt, 1);
13140 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13141 sp->strseq = strm->next_sequence_sent;
13142 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13143 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13144 (uintptr_t) stcb, sp->length,
13145 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13146 }
13147 strm->next_sequence_sent++;
13148 } else {
13149 SCTP_STAT_INCR(sctps_sends_with_unord);
13150 }
13151 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13152 if ((strm->next_spoke.tqe_next == NULL) &&
13153 (strm->next_spoke.tqe_prev == NULL)) {
13154 /* Not on wheel, insert */
13155 sctp_insert_on_wheel(stcb, asoc, strm, 1);
13156 }
13157 SCTP_TCB_SEND_UNLOCK(stcb);
13158 } else {
13159 SCTP_TCB_SEND_LOCK(stcb);
13160 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13161 SCTP_TCB_SEND_UNLOCK(stcb);
13162 if (sp == NULL) {
13163 /* ???? Huh ??? last msg is gone */
13164#ifdef INVARIANTS
13165 panic("Warning: Last msg marked incomplete, yet nothing left?");
13166#else
13167 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13168 strm->last_msg_incomplete = 0;
13169#endif
13170 goto do_a_copy_in;
13171
13172 }
13173 }
13174 while (uio->uio_resid > 0) {
13175 /* How much room do we have? */
13176 struct mbuf *new_tail, *mm;
13177
13178 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13179 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13180 else
13181 max_len = 0;
13182
13183 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13184 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13185 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13186 sndout = 0;
13187 new_tail = NULL;
13188 if (hold_tcblock) {
13189 SCTP_TCB_UNLOCK(stcb);
13190 hold_tcblock = 0;
13191 }
13192 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13193 if ((mm == NULL) || error) {
13194 if (mm) {
13195 sctp_m_freem(mm);
13196 }
13197 goto out;
13198 }
13199 /* Update the mbuf and count */
13200 SCTP_TCB_SEND_LOCK(stcb);
13201 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13202 /*
13203 * we need to get out. Peer probably
13204 * aborted.
13205 */
13206 sctp_m_freem(mm);
13207 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13208 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13209 error = ECONNRESET;
13210 }
13211 SCTP_TCB_SEND_UNLOCK(stcb);
13212 goto out;
13213 }
13214 if (sp->tail_mbuf) {
13215 /* tack it to the end */
13216 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13217 sp->tail_mbuf = new_tail;
13218 } else {
13219 /* A stolen mbuf */
13220 sp->data = mm;
13221 sp->tail_mbuf = new_tail;
13222 }
13223 sctp_snd_sb_alloc(stcb, sndout);
13224 atomic_add_int(&sp->length, sndout);
13225 len += sndout;
13226
13227 /* Did we reach EOR? */
13228 if ((uio->uio_resid == 0) &&
13229 ((user_marks_eor == 0) ||
13230 (srcv->sinfo_flags & SCTP_EOF) ||
13231 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13232 sp->msg_is_complete = 1;
13233 } else {
13234 sp->msg_is_complete = 0;
13235 }
13236 SCTP_TCB_SEND_UNLOCK(stcb);
13237 }
13238 if (uio->uio_resid == 0) {
13239 /* got it all? */
13240 continue;
13241 }
13242 /* PR-SCTP? */
13243 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13244 /*
13245 * This is ugly but we must assure locking
13246 * order
13247 */
13248 if (hold_tcblock == 0) {
13249 SCTP_TCB_LOCK(stcb);
13250 hold_tcblock = 1;
13251 }
13252 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13253 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13254 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13255 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13256 else
13257 max_len = 0;
13258 if (max_len > 0) {
13259 continue;
13260 }
13261 SCTP_TCB_UNLOCK(stcb);
13262 hold_tcblock = 0;
13263 }
13264 /* wait for space now */
13265 if (non_blocking) {
13266 /* Non-blocking io in place out */
13267 goto skip_out_eof;
13268 }
13269 if ((net->flight_size > net->cwnd) &&
13270 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13271 queue_only = 1;
13272 } else if (asoc->ifp_had_enobuf) {
13273 SCTP_STAT_INCR(sctps_ifnomemqueued);
13274 if (net->flight_size > (net->mtu * 2)) {
13275 queue_only = 1;
13276 } else {
13277 queue_only = 0;
13278 }
13279 asoc->ifp_had_enobuf = 0;
13280 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13281 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13282 } else {
13283 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13284 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13285 if (net->flight_size > net->cwnd) {
13286 queue_only = 1;
13287 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13288 } else {
13289 queue_only = 0;
13290 }
13291 }
13292 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13293 (stcb->asoc.total_flight > 0) &&
13294 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13295 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13296
13297 /*-
13298 * Ok, Nagle is set on and we have data outstanding.
13299 * Don't send anything and let SACKs drive out the
13300 * data unless wen have a "full" segment to send.
13301 */
13302 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13303 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13304 }
13305 SCTP_STAT_INCR(sctps_naglequeued);
13306 nagle_applies = 1;
13307 } else {
13308 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13309 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13310 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13311 }
13312 SCTP_STAT_INCR(sctps_naglesent);
13313 nagle_applies = 0;
13314 }
13315 /* What about the INIT, send it maybe */
13316 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13317
13318 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13319 nagle_applies, un_sent);
13320 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13321 stcb->asoc.total_flight,
13322 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13323 }
13324 if (queue_only_for_init) {
13325 if (hold_tcblock == 0) {
13326 SCTP_TCB_LOCK(stcb);
13327 hold_tcblock = 1;
13328 }
13329 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13330 /* a collision took us forward? */
13331 queue_only_for_init = 0;
13332 queue_only = 0;
13333 } else {
13334 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13335 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13336 queue_only_for_init = 0;
13337 queue_only = 1;
13338 }
13339 }
13340 if ((queue_only == 0) && (nagle_applies == 0)) {
13341 /*-
13342 * need to start chunk output
13343 * before blocking.. note that if
13344 * a lock is already applied, then
13345 * the input via the net is happening
13346 * and I don't need to start output :-D
13347 */
13348 if (hold_tcblock == 0) {
13349 if (SCTP_TCB_TRYLOCK(stcb)) {
13350 hold_tcblock = 1;
13351 sctp_chunk_output(inp,
13352 stcb,
13353 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13354 }
13355 } else {
13356 sctp_chunk_output(inp,
13357 stcb,
13358 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13359 }
13360 if (hold_tcblock == 1) {
13361 SCTP_TCB_UNLOCK(stcb);
13362 hold_tcblock = 0;
13363 }
13364 }
13365 SOCKBUF_LOCK(&so->so_snd);
13366 /*-
13367 * This is a bit strange, but I think it will
13368 * work. The total_output_queue_size is locked and
13369 * protected by the TCB_LOCK, which we just released.
13370 * There is a race that can occur between releasing it
13371 * above, and me getting the socket lock, where sacks
13372 * come in but we have not put the SB_WAIT on the
13373 * so_snd buffer to get the wakeup. After the LOCK
13374 * is applied the sack_processing will also need to
13375 * LOCK the so->so_snd to do the actual sowwakeup(). So
13376 * once we have the socket buffer lock if we recheck the
13377 * size we KNOW we will get to sleep safely with the
13378 * wakeup flag in place.
13379 */
13380 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13381 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13382 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13383 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13384 so, asoc, uio->uio_resid);
13385 }
13386 be.error = 0;
13387 stcb->block_entry = &be;
13388 error = sbwait(&so->so_snd);
13389 stcb->block_entry = NULL;
13390
13391 if (error || so->so_error || be.error) {
13392 if (error == 0) {
13393 if (so->so_error)
13394 error = so->so_error;
13395 if (be.error) {
13396 error = be.error;
13397 }
13398 }
13399 SOCKBUF_UNLOCK(&so->so_snd);
13400 goto out_unlocked;
13401 }
13402 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13403 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13404 so, asoc, stcb->asoc.total_output_queue_size);
13405 }
13406 }
13407 SOCKBUF_UNLOCK(&so->so_snd);
13408 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13409 goto out_unlocked;
13410 }
13411 }
13412 SCTP_TCB_SEND_LOCK(stcb);
13413 if (sp) {
13414 if (sp->msg_is_complete == 0) {
13415 strm->last_msg_incomplete = 1;
13416 asoc->stream_locked = 1;
13417 asoc->stream_locked_on = srcv->sinfo_stream;
13418 } else {
13419 sp->sender_all_done = 1;
13420 strm->last_msg_incomplete = 0;
13421 asoc->stream_locked = 0;
13422 }
13423 } else {
13424 SCTP_PRINTF("Huh no sp TSNH?\n");
13425 strm->last_msg_incomplete = 0;
13426 asoc->stream_locked = 0;
13427 }
13428 SCTP_TCB_SEND_UNLOCK(stcb);
13429 if (uio->uio_resid == 0) {
13430 got_all_of_the_send = 1;
13431 }
13432 } else if (top) {
13433 /* We send in a 0, since we do NOT have any locks */
13434 error = sctp_msg_append(stcb, net, top, srcv, 0);
13435 top = NULL;
13436 if (srcv->sinfo_flags & SCTP_EOF) {
13437 /*
13438 * This should only happen for Panda for the mbuf
13439 * send case, which does NOT yet support EEOR mode.
13440 * Thus, we can just set this flag to do the proper
13441 * EOF handling.
13442 */
13443 got_all_of_the_send = 1;
13444 }
13445 }
13446 if (error) {
13447 goto out;
13448 }
13449dataless_eof:
13450 /* EOF thing ? */
13451 if ((srcv->sinfo_flags & SCTP_EOF) &&
13452 (got_all_of_the_send == 1) &&
13453 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13454 int cnt;
13455
13456 SCTP_STAT_INCR(sctps_sends_with_eof);
13457 error = 0;
13458 if (hold_tcblock == 0) {
13459 SCTP_TCB_LOCK(stcb);
13460 hold_tcblock = 1;
13461 }
13462 cnt = sctp_is_there_unsent_data(stcb);
13463 if (TAILQ_EMPTY(&asoc->send_queue) &&
13464 TAILQ_EMPTY(&asoc->sent_queue) &&
13465 (cnt == 0)) {
13466 if (asoc->locked_on_sending) {
13467 goto abort_anyway;
13468 }
13469 /* there is nothing queued to send, so I'm done... */
13470 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13471 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13472 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13473 /* only send SHUTDOWN the first time through */
13474 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13475 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13476 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13477 }
13478 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13479 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13480 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13481 asoc->primary_destination);
13482 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13483 asoc->primary_destination);
13484 }
13485 } else {
13486 /*-
13487 * we still got (or just got) data to send, so set
13488 * SHUTDOWN_PENDING
13489 */
13490 /*-
13491 * XXX sockets draft says that SCTP_EOF should be
13492 * sent with no data. currently, we will allow user
13493 * data to be sent first and move to
13494 * SHUTDOWN-PENDING
13495 */
13496 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13497 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13498 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13499 if (hold_tcblock == 0) {
13500 SCTP_TCB_LOCK(stcb);
13501 hold_tcblock = 1;
13502 }
13503 if (asoc->locked_on_sending) {
13504 /* Locked to send out the data */
13505 struct sctp_stream_queue_pending *sp;
13506
13507 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13508 if (sp) {
13509 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13510 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13511 }
13512 }
13513 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13514 if (TAILQ_EMPTY(&asoc->send_queue) &&
13515 TAILQ_EMPTY(&asoc->sent_queue) &&
13516 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13517 abort_anyway:
13518 if (free_cnt_applied) {
13519 atomic_add_int(&stcb->asoc.refcnt, -1);
13520 free_cnt_applied = 0;
13521 }
13522 sctp_abort_an_association(stcb->sctp_ep, stcb,
13523 SCTP_RESPONSE_TO_USER_REQ,
13524 NULL, SCTP_SO_LOCKED);
13525 /*
13526 * now relock the stcb so everything
13527 * is sane
13528 */
13529 hold_tcblock = 0;
13530 stcb = NULL;
13531 goto out;
13532 }
13533 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13534 asoc->primary_destination);
13535 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13536 }
13537 }
13538 }
13539skip_out_eof:
13540 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13541 some_on_control = 1;
13542 }
13543 if ((net->flight_size > net->cwnd) &&
13544 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13545 queue_only = 1;
13546 } else if (asoc->ifp_had_enobuf) {
13547 SCTP_STAT_INCR(sctps_ifnomemqueued);
13548 if (net->flight_size > (net->mtu * 2)) {
13549 queue_only = 1;
13550 } else {
13551 queue_only = 0;
13552 }
13553 asoc->ifp_had_enobuf = 0;
13554 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13555 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13556 } else {
13557 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13558 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13559 if (net->flight_size > net->cwnd) {
13560 queue_only = 1;
13561 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13562 } else {
13563 queue_only = 0;
13564 }
13565 }
13566 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13567 (stcb->asoc.total_flight > 0) &&
13568 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13569 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13570 /*-
13571 * Ok, Nagle is set on and we have data outstanding.
13572 * Don't send anything and let SACKs drive out the
13573 * data unless wen have a "full" segment to send.
13574 */
13575 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13576 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13577 }
13578 SCTP_STAT_INCR(sctps_naglequeued);
13579 nagle_applies = 1;
13580 } else {
13581 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13582 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13583 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13584 }
13585 SCTP_STAT_INCR(sctps_naglesent);
13586 nagle_applies = 0;
13587 }
13588 if (queue_only_for_init) {
13589 if (hold_tcblock == 0) {
13590 SCTP_TCB_LOCK(stcb);
13591 hold_tcblock = 1;
13592 }
13593 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13594 /* a collision took us forward? */
13595 queue_only_for_init = 0;
13596 queue_only = 0;
13597 } else {
13598 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13599 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13600 queue_only_for_init = 0;
13601 queue_only = 1;
13602 }
13603 }
13604 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13605 /* we can attempt to send too. */
13606 if (hold_tcblock == 0) {
13607 /*
13608 * If there is activity recv'ing sacks no need to
13609 * send
13610 */
13611 if (SCTP_TCB_TRYLOCK(stcb)) {
13612 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13613 hold_tcblock = 1;
13614 }
13615 } else {
13616 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13617 }
13618 } else if ((queue_only == 0) &&
13619 (stcb->asoc.peers_rwnd == 0) &&
13620 (stcb->asoc.total_flight == 0)) {
13621 /* We get to have a probe outstanding */
13622 if (hold_tcblock == 0) {
13623 hold_tcblock = 1;
13624 SCTP_TCB_LOCK(stcb);
13625 }
13626 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13627 } else if (some_on_control) {
13628 int num_out, reason, frag_point;
13629
13630 /* Here we do control only */
13631 if (hold_tcblock == 0) {
13632 hold_tcblock = 1;
13633 SCTP_TCB_LOCK(stcb);
13634 }
13635 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13636 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13637 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13638 }
13639 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13640 queue_only, stcb->asoc.peers_rwnd, un_sent,
13641 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13642 stcb->asoc.total_output_queue_size, error);
13643
13644out:
13645out_unlocked:
13646
13647 if (local_soresv && stcb) {
13648 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13649 local_soresv = 0;
13650 }
13651 if (create_lock_applied) {
13652 SCTP_ASOC_CREATE_UNLOCK(inp);
13653 create_lock_applied = 0;
13654 }
13655 if ((stcb) && hold_tcblock) {
13656 SCTP_TCB_UNLOCK(stcb);
13657 }
13658 if (stcb && free_cnt_applied) {
13659 atomic_add_int(&stcb->asoc.refcnt, -1);
13660 }
13661#ifdef INVARIANTS
13662 if (stcb) {
13663 if (mtx_owned(&stcb->tcb_mtx)) {
13664 panic("Leaving with tcb mtx owned?");
13665 }
13666 if (mtx_owned(&stcb->tcb_send_mtx)) {
13667 panic("Leaving with tcb send mtx owned?");
13668 }
13669 }
13670#endif
13671 if (top) {
13672 sctp_m_freem(top);
13673 }
13674 if (control) {
13675 sctp_m_freem(control);
13676 }
13677 return (error);
13678}
13679
13680
13681/*
13682 * generate an AUTHentication chunk, if required
13683 */
13684struct mbuf *
13685sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13686 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13687 struct sctp_tcb *stcb, uint8_t chunk)
13688{
13689 struct mbuf *m_auth;
13690 struct sctp_auth_chunk *auth;
13691 int chunk_len;
13692
13693 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13694 (stcb == NULL))
13695 return (m);
13696
13697 /* sysctl disabled auth? */
13698 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13699 return (m);
13700
13701 /* peer doesn't do auth... */
13702 if (!stcb->asoc.peer_supports_auth) {
13703 return (m);
13704 }
13705 /* does the requested chunk require auth? */
13706 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13707 return (m);
13708 }
13709 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13710 if (m_auth == NULL) {
13711 /* no mbuf's */
13712 return (m);
13713 }
13714 /* reserve some space if this will be the first mbuf */
13715 if (m == NULL)
13716 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13717 /* fill in the AUTH chunk details */
13718 auth = mtod(m_auth, struct sctp_auth_chunk *);
13719 bzero(auth, sizeof(*auth));
13720 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13721 auth->ch.chunk_flags = 0;
13722 chunk_len = sizeof(*auth) +
13723 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13724 auth->ch.chunk_length = htons(chunk_len);
13725 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13726 /* key id and hmac digest will be computed and filled in upon send */
13727
13728 /* save the offset where the auth was inserted into the chain */
13729 if (m != NULL) {
13730 struct mbuf *cn;
13731
13732 *offset = 0;
13733 cn = m;
13734 while (cn) {
13735 *offset += SCTP_BUF_LEN(cn);
13736 cn = SCTP_BUF_NEXT(cn);
13737 }
13738 } else
13739 *offset = 0;
13740
13741 /* update length and return pointer to the auth chunk */
13742 SCTP_BUF_LEN(m_auth) = chunk_len;
13743 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13744 if (auth_ret != NULL)
13745 *auth_ret = auth;
13746
13747 return (m);
13748}
13749
13750#ifdef INET6
13751int
13752sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13753{
13754 struct nd_prefix *pfx = NULL;
13755 struct nd_pfxrouter *pfxrtr = NULL;
13756 struct sockaddr_in6 gw6;
13757
13758 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13759 return (0);
13760
13761 /* get prefix entry of address */
13762 LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
13762 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13763 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13764 continue;
13765 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13766 &src6->sin6_addr, &pfx->ndpr_mask))
13767 break;
13768 }
13769 /* no prefix entry in the prefix list */
13770 if (pfx == NULL) {
13771 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13772 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13773 return (0);
13774 }
13775 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13776 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13777
13778 /* search installed gateway from prefix entry */
13779 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13780 pfxrtr->pfr_next) {
13781 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13782 gw6.sin6_family = AF_INET6;
13783 gw6.sin6_len = sizeof(struct sockaddr_in6);
13784 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13785 sizeof(struct in6_addr));
13786 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13787 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13788 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13789 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13790 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13791 ro->ro_rt->rt_gateway)) {
13792 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13793 return (1);
13794 }
13795 }
13796 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13797 return (0);
13798}
13799
13800#endif
13801
13802int
13803sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13804{
13805 struct sockaddr_in *sin, *mask;
13806 struct ifaddr *ifa;
13807 struct in_addr srcnetaddr, gwnetaddr;
13808
13809 if (ro == NULL || ro->ro_rt == NULL ||
13810 sifa->address.sa.sa_family != AF_INET) {
13811 return (0);
13812 }
13813 ifa = (struct ifaddr *)sifa->ifa;
13814 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13815 sin = (struct sockaddr_in *)&sifa->address.sin;
13816 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13817 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13818 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13819 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13820
13821 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13822 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13823 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13824 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13825 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13826 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13827 return (1);
13828 }
13829 return (0);
13830}
13763 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13764 continue;
13765 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13766 &src6->sin6_addr, &pfx->ndpr_mask))
13767 break;
13768 }
13769 /* no prefix entry in the prefix list */
13770 if (pfx == NULL) {
13771 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13772 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13773 return (0);
13774 }
13775 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13776 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13777
13778 /* search installed gateway from prefix entry */
13779 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13780 pfxrtr->pfr_next) {
13781 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13782 gw6.sin6_family = AF_INET6;
13783 gw6.sin6_len = sizeof(struct sockaddr_in6);
13784 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13785 sizeof(struct in6_addr));
13786 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13787 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13788 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13789 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13790 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13791 ro->ro_rt->rt_gateway)) {
13792 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13793 return (1);
13794 }
13795 }
13796 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13797 return (0);
13798}
13799
13800#endif
13801
13802int
13803sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13804{
13805 struct sockaddr_in *sin, *mask;
13806 struct ifaddr *ifa;
13807 struct in_addr srcnetaddr, gwnetaddr;
13808
13809 if (ro == NULL || ro->ro_rt == NULL ||
13810 sifa->address.sa.sa_family != AF_INET) {
13811 return (0);
13812 }
13813 ifa = (struct ifaddr *)sifa->ifa;
13814 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13815 sin = (struct sockaddr_in *)&sifa->address.sin;
13816 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13817 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13818 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13819 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13820
13821 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13822 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13823 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13824 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13825 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13826 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13827 return (1);
13828 }
13829 return (0);
13830}