2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
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.
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.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/udp.h>
53 #include <machine/in_cksum.h>
57 #define SCTP_MAX_GAPS_INARRAY 4
59 uint8_t right_edge; /* mergable on the right edge */
60 uint8_t left_edge; /* mergable on the left edge */
63 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
66 struct sack_track sack_array[256] = {
67 {0, 0, 0, 0, /* 0x00 */
74 {1, 0, 1, 0, /* 0x01 */
81 {0, 0, 1, 0, /* 0x02 */
88 {1, 0, 1, 0, /* 0x03 */
95 {0, 0, 1, 0, /* 0x04 */
102 {1, 0, 2, 0, /* 0x05 */
109 {0, 0, 1, 0, /* 0x06 */
116 {1, 0, 1, 0, /* 0x07 */
123 {0, 0, 1, 0, /* 0x08 */
130 {1, 0, 2, 0, /* 0x09 */
137 {0, 0, 2, 0, /* 0x0a */
144 {1, 0, 2, 0, /* 0x0b */
151 {0, 0, 1, 0, /* 0x0c */
158 {1, 0, 2, 0, /* 0x0d */
165 {0, 0, 1, 0, /* 0x0e */
172 {1, 0, 1, 0, /* 0x0f */
179 {0, 0, 1, 0, /* 0x10 */
186 {1, 0, 2, 0, /* 0x11 */
193 {0, 0, 2, 0, /* 0x12 */
200 {1, 0, 2, 0, /* 0x13 */
207 {0, 0, 2, 0, /* 0x14 */
214 {1, 0, 3, 0, /* 0x15 */
221 {0, 0, 2, 0, /* 0x16 */
228 {1, 0, 2, 0, /* 0x17 */
235 {0, 0, 1, 0, /* 0x18 */
242 {1, 0, 2, 0, /* 0x19 */
249 {0, 0, 2, 0, /* 0x1a */
256 {1, 0, 2, 0, /* 0x1b */
263 {0, 0, 1, 0, /* 0x1c */
270 {1, 0, 2, 0, /* 0x1d */
277 {0, 0, 1, 0, /* 0x1e */
284 {1, 0, 1, 0, /* 0x1f */
291 {0, 0, 1, 0, /* 0x20 */
298 {1, 0, 2, 0, /* 0x21 */
305 {0, 0, 2, 0, /* 0x22 */
312 {1, 0, 2, 0, /* 0x23 */
319 {0, 0, 2, 0, /* 0x24 */
326 {1, 0, 3, 0, /* 0x25 */
333 {0, 0, 2, 0, /* 0x26 */
340 {1, 0, 2, 0, /* 0x27 */
347 {0, 0, 2, 0, /* 0x28 */
354 {1, 0, 3, 0, /* 0x29 */
361 {0, 0, 3, 0, /* 0x2a */
368 {1, 0, 3, 0, /* 0x2b */
375 {0, 0, 2, 0, /* 0x2c */
382 {1, 0, 3, 0, /* 0x2d */
389 {0, 0, 2, 0, /* 0x2e */
396 {1, 0, 2, 0, /* 0x2f */
403 {0, 0, 1, 0, /* 0x30 */
410 {1, 0, 2, 0, /* 0x31 */
417 {0, 0, 2, 0, /* 0x32 */
424 {1, 0, 2, 0, /* 0x33 */
431 {0, 0, 2, 0, /* 0x34 */
438 {1, 0, 3, 0, /* 0x35 */
445 {0, 0, 2, 0, /* 0x36 */
452 {1, 0, 2, 0, /* 0x37 */
459 {0, 0, 1, 0, /* 0x38 */
466 {1, 0, 2, 0, /* 0x39 */
473 {0, 0, 2, 0, /* 0x3a */
480 {1, 0, 2, 0, /* 0x3b */
487 {0, 0, 1, 0, /* 0x3c */
494 {1, 0, 2, 0, /* 0x3d */
501 {0, 0, 1, 0, /* 0x3e */
508 {1, 0, 1, 0, /* 0x3f */
515 {0, 0, 1, 0, /* 0x40 */
522 {1, 0, 2, 0, /* 0x41 */
529 {0, 0, 2, 0, /* 0x42 */
536 {1, 0, 2, 0, /* 0x43 */
543 {0, 0, 2, 0, /* 0x44 */
550 {1, 0, 3, 0, /* 0x45 */
557 {0, 0, 2, 0, /* 0x46 */
564 {1, 0, 2, 0, /* 0x47 */
571 {0, 0, 2, 0, /* 0x48 */
578 {1, 0, 3, 0, /* 0x49 */
585 {0, 0, 3, 0, /* 0x4a */
592 {1, 0, 3, 0, /* 0x4b */
599 {0, 0, 2, 0, /* 0x4c */
606 {1, 0, 3, 0, /* 0x4d */
613 {0, 0, 2, 0, /* 0x4e */
620 {1, 0, 2, 0, /* 0x4f */
627 {0, 0, 2, 0, /* 0x50 */
634 {1, 0, 3, 0, /* 0x51 */
641 {0, 0, 3, 0, /* 0x52 */
648 {1, 0, 3, 0, /* 0x53 */
655 {0, 0, 3, 0, /* 0x54 */
662 {1, 0, 4, 0, /* 0x55 */
669 {0, 0, 3, 0, /* 0x56 */
676 {1, 0, 3, 0, /* 0x57 */
683 {0, 0, 2, 0, /* 0x58 */
690 {1, 0, 3, 0, /* 0x59 */
697 {0, 0, 3, 0, /* 0x5a */
704 {1, 0, 3, 0, /* 0x5b */
711 {0, 0, 2, 0, /* 0x5c */
718 {1, 0, 3, 0, /* 0x5d */
725 {0, 0, 2, 0, /* 0x5e */
732 {1, 0, 2, 0, /* 0x5f */
739 {0, 0, 1, 0, /* 0x60 */
746 {1, 0, 2, 0, /* 0x61 */
753 {0, 0, 2, 0, /* 0x62 */
760 {1, 0, 2, 0, /* 0x63 */
767 {0, 0, 2, 0, /* 0x64 */
774 {1, 0, 3, 0, /* 0x65 */
781 {0, 0, 2, 0, /* 0x66 */
788 {1, 0, 2, 0, /* 0x67 */
795 {0, 0, 2, 0, /* 0x68 */
802 {1, 0, 3, 0, /* 0x69 */
809 {0, 0, 3, 0, /* 0x6a */
816 {1, 0, 3, 0, /* 0x6b */
823 {0, 0, 2, 0, /* 0x6c */
830 {1, 0, 3, 0, /* 0x6d */
837 {0, 0, 2, 0, /* 0x6e */
844 {1, 0, 2, 0, /* 0x6f */
851 {0, 0, 1, 0, /* 0x70 */
858 {1, 0, 2, 0, /* 0x71 */
865 {0, 0, 2, 0, /* 0x72 */
872 {1, 0, 2, 0, /* 0x73 */
879 {0, 0, 2, 0, /* 0x74 */
886 {1, 0, 3, 0, /* 0x75 */
893 {0, 0, 2, 0, /* 0x76 */
900 {1, 0, 2, 0, /* 0x77 */
907 {0, 0, 1, 0, /* 0x78 */
914 {1, 0, 2, 0, /* 0x79 */
921 {0, 0, 2, 0, /* 0x7a */
928 {1, 0, 2, 0, /* 0x7b */
935 {0, 0, 1, 0, /* 0x7c */
942 {1, 0, 2, 0, /* 0x7d */
949 {0, 0, 1, 0, /* 0x7e */
956 {1, 0, 1, 0, /* 0x7f */
963 {0, 1, 1, 0, /* 0x80 */
970 {1, 1, 2, 0, /* 0x81 */
977 {0, 1, 2, 0, /* 0x82 */
984 {1, 1, 2, 0, /* 0x83 */
991 {0, 1, 2, 0, /* 0x84 */
998 {1, 1, 3, 0, /* 0x85 */
1005 {0, 1, 2, 0, /* 0x86 */
1012 {1, 1, 2, 0, /* 0x87 */
1019 {0, 1, 2, 0, /* 0x88 */
1026 {1, 1, 3, 0, /* 0x89 */
1033 {0, 1, 3, 0, /* 0x8a */
1040 {1, 1, 3, 0, /* 0x8b */
1047 {0, 1, 2, 0, /* 0x8c */
1054 {1, 1, 3, 0, /* 0x8d */
1061 {0, 1, 2, 0, /* 0x8e */
1068 {1, 1, 2, 0, /* 0x8f */
1075 {0, 1, 2, 0, /* 0x90 */
1082 {1, 1, 3, 0, /* 0x91 */
1089 {0, 1, 3, 0, /* 0x92 */
1096 {1, 1, 3, 0, /* 0x93 */
1103 {0, 1, 3, 0, /* 0x94 */
1110 {1, 1, 4, 0, /* 0x95 */
1117 {0, 1, 3, 0, /* 0x96 */
1124 {1, 1, 3, 0, /* 0x97 */
1131 {0, 1, 2, 0, /* 0x98 */
1138 {1, 1, 3, 0, /* 0x99 */
1145 {0, 1, 3, 0, /* 0x9a */
1152 {1, 1, 3, 0, /* 0x9b */
1159 {0, 1, 2, 0, /* 0x9c */
1166 {1, 1, 3, 0, /* 0x9d */
1173 {0, 1, 2, 0, /* 0x9e */
1180 {1, 1, 2, 0, /* 0x9f */
1187 {0, 1, 2, 0, /* 0xa0 */
1194 {1, 1, 3, 0, /* 0xa1 */
1201 {0, 1, 3, 0, /* 0xa2 */
1208 {1, 1, 3, 0, /* 0xa3 */
1215 {0, 1, 3, 0, /* 0xa4 */
1222 {1, 1, 4, 0, /* 0xa5 */
1229 {0, 1, 3, 0, /* 0xa6 */
1236 {1, 1, 3, 0, /* 0xa7 */
1243 {0, 1, 3, 0, /* 0xa8 */
1250 {1, 1, 4, 0, /* 0xa9 */
1257 {0, 1, 4, 0, /* 0xaa */
1264 {1, 1, 4, 0, /* 0xab */
1271 {0, 1, 3, 0, /* 0xac */
1278 {1, 1, 4, 0, /* 0xad */
1285 {0, 1, 3, 0, /* 0xae */
1292 {1, 1, 3, 0, /* 0xaf */
1299 {0, 1, 2, 0, /* 0xb0 */
1306 {1, 1, 3, 0, /* 0xb1 */
1313 {0, 1, 3, 0, /* 0xb2 */
1320 {1, 1, 3, 0, /* 0xb3 */
1327 {0, 1, 3, 0, /* 0xb4 */
1334 {1, 1, 4, 0, /* 0xb5 */
1341 {0, 1, 3, 0, /* 0xb6 */
1348 {1, 1, 3, 0, /* 0xb7 */
1355 {0, 1, 2, 0, /* 0xb8 */
1362 {1, 1, 3, 0, /* 0xb9 */
1369 {0, 1, 3, 0, /* 0xba */
1376 {1, 1, 3, 0, /* 0xbb */
1383 {0, 1, 2, 0, /* 0xbc */
1390 {1, 1, 3, 0, /* 0xbd */
1397 {0, 1, 2, 0, /* 0xbe */
1404 {1, 1, 2, 0, /* 0xbf */
1411 {0, 1, 1, 0, /* 0xc0 */
1418 {1, 1, 2, 0, /* 0xc1 */
1425 {0, 1, 2, 0, /* 0xc2 */
1432 {1, 1, 2, 0, /* 0xc3 */
1439 {0, 1, 2, 0, /* 0xc4 */
1446 {1, 1, 3, 0, /* 0xc5 */
1453 {0, 1, 2, 0, /* 0xc6 */
1460 {1, 1, 2, 0, /* 0xc7 */
1467 {0, 1, 2, 0, /* 0xc8 */
1474 {1, 1, 3, 0, /* 0xc9 */
1481 {0, 1, 3, 0, /* 0xca */
1488 {1, 1, 3, 0, /* 0xcb */
1495 {0, 1, 2, 0, /* 0xcc */
1502 {1, 1, 3, 0, /* 0xcd */
1509 {0, 1, 2, 0, /* 0xce */
1516 {1, 1, 2, 0, /* 0xcf */
1523 {0, 1, 2, 0, /* 0xd0 */
1530 {1, 1, 3, 0, /* 0xd1 */
1537 {0, 1, 3, 0, /* 0xd2 */
1544 {1, 1, 3, 0, /* 0xd3 */
1551 {0, 1, 3, 0, /* 0xd4 */
1558 {1, 1, 4, 0, /* 0xd5 */
1565 {0, 1, 3, 0, /* 0xd6 */
1572 {1, 1, 3, 0, /* 0xd7 */
1579 {0, 1, 2, 0, /* 0xd8 */
1586 {1, 1, 3, 0, /* 0xd9 */
1593 {0, 1, 3, 0, /* 0xda */
1600 {1, 1, 3, 0, /* 0xdb */
1607 {0, 1, 2, 0, /* 0xdc */
1614 {1, 1, 3, 0, /* 0xdd */
1621 {0, 1, 2, 0, /* 0xde */
1628 {1, 1, 2, 0, /* 0xdf */
1635 {0, 1, 1, 0, /* 0xe0 */
1642 {1, 1, 2, 0, /* 0xe1 */
1649 {0, 1, 2, 0, /* 0xe2 */
1656 {1, 1, 2, 0, /* 0xe3 */
1663 {0, 1, 2, 0, /* 0xe4 */
1670 {1, 1, 3, 0, /* 0xe5 */
1677 {0, 1, 2, 0, /* 0xe6 */
1684 {1, 1, 2, 0, /* 0xe7 */
1691 {0, 1, 2, 0, /* 0xe8 */
1698 {1, 1, 3, 0, /* 0xe9 */
1705 {0, 1, 3, 0, /* 0xea */
1712 {1, 1, 3, 0, /* 0xeb */
1719 {0, 1, 2, 0, /* 0xec */
1726 {1, 1, 3, 0, /* 0xed */
1733 {0, 1, 2, 0, /* 0xee */
1740 {1, 1, 2, 0, /* 0xef */
1747 {0, 1, 1, 0, /* 0xf0 */
1754 {1, 1, 2, 0, /* 0xf1 */
1761 {0, 1, 2, 0, /* 0xf2 */
1768 {1, 1, 2, 0, /* 0xf3 */
1775 {0, 1, 2, 0, /* 0xf4 */
1782 {1, 1, 3, 0, /* 0xf5 */
1789 {0, 1, 2, 0, /* 0xf6 */
1796 {1, 1, 2, 0, /* 0xf7 */
1803 {0, 1, 1, 0, /* 0xf8 */
1810 {1, 1, 2, 0, /* 0xf9 */
1817 {0, 1, 2, 0, /* 0xfa */
1824 {1, 1, 2, 0, /* 0xfb */
1831 {0, 1, 1, 0, /* 0xfc */
1838 {1, 1, 2, 0, /* 0xfd */
1845 {0, 1, 1, 0, /* 0xfe */
1852 {1, 1, 1, 0, /* 0xff */
1861 /* EY below are nr_sacks version of the preceeding two data structures, identical except their names */
1862 #define SCTP_MAX_NR_GAPS_INARRAY 4
1863 struct nr_sack_track {
1864 uint8_t right_edge; /* mergable on the right edge */
1865 uint8_t left_edge; /* mergable on the left edge */
1866 uint8_t num_entries;
1868 struct sctp_nr_gap_ack_block nr_gaps[SCTP_MAX_NR_GAPS_INARRAY];
1871 struct nr_sack_track nr_sack_array[256] = {
1872 {0, 0, 0, 0, /* 0x00 */
1879 {1, 0, 1, 0, /* 0x01 */
1886 {0, 0, 1, 0, /* 0x02 */
1893 {1, 0, 1, 0, /* 0x03 */
1900 {0, 0, 1, 0, /* 0x04 */
1907 {1, 0, 2, 0, /* 0x05 */
1914 {0, 0, 1, 0, /* 0x06 */
1921 {1, 0, 1, 0, /* 0x07 */
1928 {0, 0, 1, 0, /* 0x08 */
1935 {1, 0, 2, 0, /* 0x09 */
1942 {0, 0, 2, 0, /* 0x0a */
1949 {1, 0, 2, 0, /* 0x0b */
1956 {0, 0, 1, 0, /* 0x0c */
1963 {1, 0, 2, 0, /* 0x0d */
1970 {0, 0, 1, 0, /* 0x0e */
1977 {1, 0, 1, 0, /* 0x0f */
1984 {0, 0, 1, 0, /* 0x10 */
1991 {1, 0, 2, 0, /* 0x11 */
1998 {0, 0, 2, 0, /* 0x12 */
2005 {1, 0, 2, 0, /* 0x13 */
2012 {0, 0, 2, 0, /* 0x14 */
2019 {1, 0, 3, 0, /* 0x15 */
2026 {0, 0, 2, 0, /* 0x16 */
2033 {1, 0, 2, 0, /* 0x17 */
2040 {0, 0, 1, 0, /* 0x18 */
2047 {1, 0, 2, 0, /* 0x19 */
2054 {0, 0, 2, 0, /* 0x1a */
2061 {1, 0, 2, 0, /* 0x1b */
2068 {0, 0, 1, 0, /* 0x1c */
2075 {1, 0, 2, 0, /* 0x1d */
2082 {0, 0, 1, 0, /* 0x1e */
2089 {1, 0, 1, 0, /* 0x1f */
2096 {0, 0, 1, 0, /* 0x20 */
2103 {1, 0, 2, 0, /* 0x21 */
2110 {0, 0, 2, 0, /* 0x22 */
2117 {1, 0, 2, 0, /* 0x23 */
2124 {0, 0, 2, 0, /* 0x24 */
2131 {1, 0, 3, 0, /* 0x25 */
2138 {0, 0, 2, 0, /* 0x26 */
2145 {1, 0, 2, 0, /* 0x27 */
2152 {0, 0, 2, 0, /* 0x28 */
2159 {1, 0, 3, 0, /* 0x29 */
2166 {0, 0, 3, 0, /* 0x2a */
2173 {1, 0, 3, 0, /* 0x2b */
2180 {0, 0, 2, 0, /* 0x2c */
2187 {1, 0, 3, 0, /* 0x2d */
2194 {0, 0, 2, 0, /* 0x2e */
2201 {1, 0, 2, 0, /* 0x2f */
2208 {0, 0, 1, 0, /* 0x30 */
2215 {1, 0, 2, 0, /* 0x31 */
2222 {0, 0, 2, 0, /* 0x32 */
2229 {1, 0, 2, 0, /* 0x33 */
2236 {0, 0, 2, 0, /* 0x34 */
2243 {1, 0, 3, 0, /* 0x35 */
2250 {0, 0, 2, 0, /* 0x36 */
2257 {1, 0, 2, 0, /* 0x37 */
2264 {0, 0, 1, 0, /* 0x38 */
2271 {1, 0, 2, 0, /* 0x39 */
2278 {0, 0, 2, 0, /* 0x3a */
2285 {1, 0, 2, 0, /* 0x3b */
2292 {0, 0, 1, 0, /* 0x3c */
2299 {1, 0, 2, 0, /* 0x3d */
2306 {0, 0, 1, 0, /* 0x3e */
2313 {1, 0, 1, 0, /* 0x3f */
2320 {0, 0, 1, 0, /* 0x40 */
2327 {1, 0, 2, 0, /* 0x41 */
2334 {0, 0, 2, 0, /* 0x42 */
2341 {1, 0, 2, 0, /* 0x43 */
2348 {0, 0, 2, 0, /* 0x44 */
2355 {1, 0, 3, 0, /* 0x45 */
2362 {0, 0, 2, 0, /* 0x46 */
2369 {1, 0, 2, 0, /* 0x47 */
2376 {0, 0, 2, 0, /* 0x48 */
2383 {1, 0, 3, 0, /* 0x49 */
2390 {0, 0, 3, 0, /* 0x4a */
2397 {1, 0, 3, 0, /* 0x4b */
2404 {0, 0, 2, 0, /* 0x4c */
2411 {1, 0, 3, 0, /* 0x4d */
2418 {0, 0, 2, 0, /* 0x4e */
2425 {1, 0, 2, 0, /* 0x4f */
2432 {0, 0, 2, 0, /* 0x50 */
2439 {1, 0, 3, 0, /* 0x51 */
2446 {0, 0, 3, 0, /* 0x52 */
2453 {1, 0, 3, 0, /* 0x53 */
2460 {0, 0, 3, 0, /* 0x54 */
2467 {1, 0, 4, 0, /* 0x55 */
2474 {0, 0, 3, 0, /* 0x56 */
2481 {1, 0, 3, 0, /* 0x57 */
2488 {0, 0, 2, 0, /* 0x58 */
2495 {1, 0, 3, 0, /* 0x59 */
2502 {0, 0, 3, 0, /* 0x5a */
2509 {1, 0, 3, 0, /* 0x5b */
2516 {0, 0, 2, 0, /* 0x5c */
2523 {1, 0, 3, 0, /* 0x5d */
2530 {0, 0, 2, 0, /* 0x5e */
2537 {1, 0, 2, 0, /* 0x5f */
2544 {0, 0, 1, 0, /* 0x60 */
2551 {1, 0, 2, 0, /* 0x61 */
2558 {0, 0, 2, 0, /* 0x62 */
2565 {1, 0, 2, 0, /* 0x63 */
2572 {0, 0, 2, 0, /* 0x64 */
2579 {1, 0, 3, 0, /* 0x65 */
2586 {0, 0, 2, 0, /* 0x66 */
2593 {1, 0, 2, 0, /* 0x67 */
2600 {0, 0, 2, 0, /* 0x68 */
2607 {1, 0, 3, 0, /* 0x69 */
2614 {0, 0, 3, 0, /* 0x6a */
2621 {1, 0, 3, 0, /* 0x6b */
2628 {0, 0, 2, 0, /* 0x6c */
2635 {1, 0, 3, 0, /* 0x6d */
2642 {0, 0, 2, 0, /* 0x6e */
2649 {1, 0, 2, 0, /* 0x6f */
2656 {0, 0, 1, 0, /* 0x70 */
2663 {1, 0, 2, 0, /* 0x71 */
2670 {0, 0, 2, 0, /* 0x72 */
2677 {1, 0, 2, 0, /* 0x73 */
2684 {0, 0, 2, 0, /* 0x74 */
2691 {1, 0, 3, 0, /* 0x75 */
2698 {0, 0, 2, 0, /* 0x76 */
2705 {1, 0, 2, 0, /* 0x77 */
2712 {0, 0, 1, 0, /* 0x78 */
2719 {1, 0, 2, 0, /* 0x79 */
2726 {0, 0, 2, 0, /* 0x7a */
2733 {1, 0, 2, 0, /* 0x7b */
2740 {0, 0, 1, 0, /* 0x7c */
2747 {1, 0, 2, 0, /* 0x7d */
2754 {0, 0, 1, 0, /* 0x7e */
2761 {1, 0, 1, 0, /* 0x7f */
2768 {0, 1, 1, 0, /* 0x80 */
2775 {1, 1, 2, 0, /* 0x81 */
2782 {0, 1, 2, 0, /* 0x82 */
2789 {1, 1, 2, 0, /* 0x83 */
2796 {0, 1, 2, 0, /* 0x84 */
2803 {1, 1, 3, 0, /* 0x85 */
2810 {0, 1, 2, 0, /* 0x86 */
2817 {1, 1, 2, 0, /* 0x87 */
2824 {0, 1, 2, 0, /* 0x88 */
2831 {1, 1, 3, 0, /* 0x89 */
2838 {0, 1, 3, 0, /* 0x8a */
2845 {1, 1, 3, 0, /* 0x8b */
2852 {0, 1, 2, 0, /* 0x8c */
2859 {1, 1, 3, 0, /* 0x8d */
2866 {0, 1, 2, 0, /* 0x8e */
2873 {1, 1, 2, 0, /* 0x8f */
2880 {0, 1, 2, 0, /* 0x90 */
2887 {1, 1, 3, 0, /* 0x91 */
2894 {0, 1, 3, 0, /* 0x92 */
2901 {1, 1, 3, 0, /* 0x93 */
2908 {0, 1, 3, 0, /* 0x94 */
2915 {1, 1, 4, 0, /* 0x95 */
2922 {0, 1, 3, 0, /* 0x96 */
2929 {1, 1, 3, 0, /* 0x97 */
2936 {0, 1, 2, 0, /* 0x98 */
2943 {1, 1, 3, 0, /* 0x99 */
2950 {0, 1, 3, 0, /* 0x9a */
2957 {1, 1, 3, 0, /* 0x9b */
2964 {0, 1, 2, 0, /* 0x9c */
2971 {1, 1, 3, 0, /* 0x9d */
2978 {0, 1, 2, 0, /* 0x9e */
2985 {1, 1, 2, 0, /* 0x9f */
2992 {0, 1, 2, 0, /* 0xa0 */
2999 {1, 1, 3, 0, /* 0xa1 */
3006 {0, 1, 3, 0, /* 0xa2 */
3013 {1, 1, 3, 0, /* 0xa3 */
3020 {0, 1, 3, 0, /* 0xa4 */
3027 {1, 1, 4, 0, /* 0xa5 */
3034 {0, 1, 3, 0, /* 0xa6 */
3041 {1, 1, 3, 0, /* 0xa7 */
3048 {0, 1, 3, 0, /* 0xa8 */
3055 {1, 1, 4, 0, /* 0xa9 */
3062 {0, 1, 4, 0, /* 0xaa */
3069 {1, 1, 4, 0, /* 0xab */
3076 {0, 1, 3, 0, /* 0xac */
3083 {1, 1, 4, 0, /* 0xad */
3090 {0, 1, 3, 0, /* 0xae */
3097 {1, 1, 3, 0, /* 0xaf */
3104 {0, 1, 2, 0, /* 0xb0 */
3111 {1, 1, 3, 0, /* 0xb1 */
3118 {0, 1, 3, 0, /* 0xb2 */
3125 {1, 1, 3, 0, /* 0xb3 */
3132 {0, 1, 3, 0, /* 0xb4 */
3139 {1, 1, 4, 0, /* 0xb5 */
3146 {0, 1, 3, 0, /* 0xb6 */
3153 {1, 1, 3, 0, /* 0xb7 */
3160 {0, 1, 2, 0, /* 0xb8 */
3167 {1, 1, 3, 0, /* 0xb9 */
3174 {0, 1, 3, 0, /* 0xba */
3181 {1, 1, 3, 0, /* 0xbb */
3188 {0, 1, 2, 0, /* 0xbc */
3195 {1, 1, 3, 0, /* 0xbd */
3202 {0, 1, 2, 0, /* 0xbe */
3209 {1, 1, 2, 0, /* 0xbf */
3216 {0, 1, 1, 0, /* 0xc0 */
3223 {1, 1, 2, 0, /* 0xc1 */
3230 {0, 1, 2, 0, /* 0xc2 */
3237 {1, 1, 2, 0, /* 0xc3 */
3244 {0, 1, 2, 0, /* 0xc4 */
3251 {1, 1, 3, 0, /* 0xc5 */
3258 {0, 1, 2, 0, /* 0xc6 */
3265 {1, 1, 2, 0, /* 0xc7 */
3272 {0, 1, 2, 0, /* 0xc8 */
3279 {1, 1, 3, 0, /* 0xc9 */
3286 {0, 1, 3, 0, /* 0xca */
3293 {1, 1, 3, 0, /* 0xcb */
3300 {0, 1, 2, 0, /* 0xcc */
3307 {1, 1, 3, 0, /* 0xcd */
3314 {0, 1, 2, 0, /* 0xce */
3321 {1, 1, 2, 0, /* 0xcf */
3328 {0, 1, 2, 0, /* 0xd0 */
3335 {1, 1, 3, 0, /* 0xd1 */
3342 {0, 1, 3, 0, /* 0xd2 */
3349 {1, 1, 3, 0, /* 0xd3 */
3356 {0, 1, 3, 0, /* 0xd4 */
3363 {1, 1, 4, 0, /* 0xd5 */
3370 {0, 1, 3, 0, /* 0xd6 */
3377 {1, 1, 3, 0, /* 0xd7 */
3384 {0, 1, 2, 0, /* 0xd8 */
3391 {1, 1, 3, 0, /* 0xd9 */
3398 {0, 1, 3, 0, /* 0xda */
3405 {1, 1, 3, 0, /* 0xdb */
3412 {0, 1, 2, 0, /* 0xdc */
3419 {1, 1, 3, 0, /* 0xdd */
3426 {0, 1, 2, 0, /* 0xde */
3433 {1, 1, 2, 0, /* 0xdf */
3440 {0, 1, 1, 0, /* 0xe0 */
3447 {1, 1, 2, 0, /* 0xe1 */
3454 {0, 1, 2, 0, /* 0xe2 */
3461 {1, 1, 2, 0, /* 0xe3 */
3468 {0, 1, 2, 0, /* 0xe4 */
3475 {1, 1, 3, 0, /* 0xe5 */
3482 {0, 1, 2, 0, /* 0xe6 */
3489 {1, 1, 2, 0, /* 0xe7 */
3496 {0, 1, 2, 0, /* 0xe8 */
3503 {1, 1, 3, 0, /* 0xe9 */
3510 {0, 1, 3, 0, /* 0xea */
3517 {1, 1, 3, 0, /* 0xeb */
3524 {0, 1, 2, 0, /* 0xec */
3531 {1, 1, 3, 0, /* 0xed */
3538 {0, 1, 2, 0, /* 0xee */
3545 {1, 1, 2, 0, /* 0xef */
3552 {0, 1, 1, 0, /* 0xf0 */
3559 {1, 1, 2, 0, /* 0xf1 */
3566 {0, 1, 2, 0, /* 0xf2 */
3573 {1, 1, 2, 0, /* 0xf3 */
3580 {0, 1, 2, 0, /* 0xf4 */
3587 {1, 1, 3, 0, /* 0xf5 */
3594 {0, 1, 2, 0, /* 0xf6 */
3601 {1, 1, 2, 0, /* 0xf7 */
3608 {0, 1, 1, 0, /* 0xf8 */
3615 {1, 1, 2, 0, /* 0xf9 */
3622 {0, 1, 2, 0, /* 0xfa */
3629 {1, 1, 2, 0, /* 0xfb */
3636 {0, 1, 1, 0, /* 0xfc */
3643 {1, 1, 2, 0, /* 0xfd */
3650 {0, 1, 1, 0, /* 0xfe */
3657 {1, 1, 1, 0, /* 0xff */
3669 sctp_is_address_in_scope(struct sctp_ifa *ifa,
3670 int ipv4_addr_legal,
3671 int ipv6_addr_legal,
3673 int ipv4_local_scope,
3678 if ((loopback_scope == 0) &&
3679 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
3681 * skip loopback if not in scope *
3685 switch (ifa->address.sa.sa_family) {
3687 if (ipv4_addr_legal) {
3688 struct sockaddr_in *sin;
3690 sin = (struct sockaddr_in *)&ifa->address.sin;
3691 if (sin->sin_addr.s_addr == 0) {
3692 /* not in scope , unspecified */
3695 if ((ipv4_local_scope == 0) &&
3696 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3697 /* private address not in scope */
3706 if (ipv6_addr_legal) {
3707 struct sockaddr_in6 *sin6;
3710 * Must update the flags, bummer, which means any
3711 * IFA locks must now be applied HERE <->
3714 sctp_gather_internal_ifa_flags(ifa);
3716 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3719 /* ok to use deprecated addresses? */
3720 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3721 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3722 /* skip unspecifed addresses */
3725 if ( /* (local_scope == 0) && */
3726 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3729 if ((site_scope == 0) &&
3730 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3744 static struct mbuf *
3745 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
3747 struct sctp_paramhdr *parmh;
3751 if (ifa->address.sa.sa_family == AF_INET) {
3752 len = sizeof(struct sctp_ipv4addr_param);
3753 } else if (ifa->address.sa.sa_family == AF_INET6) {
3754 len = sizeof(struct sctp_ipv6addr_param);
3759 if (M_TRAILINGSPACE(m) >= len) {
3760 /* easy side we just drop it on the end */
3761 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
3764 /* Need more space */
3766 while (SCTP_BUF_NEXT(mret) != NULL) {
3767 mret = SCTP_BUF_NEXT(mret);
3769 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
3770 if (SCTP_BUF_NEXT(mret) == NULL) {
3771 /* We are hosed, can't add more addresses */
3774 mret = SCTP_BUF_NEXT(mret);
3775 parmh = mtod(mret, struct sctp_paramhdr *);
3777 /* now add the parameter */
3778 switch (ifa->address.sa.sa_family) {
3781 struct sctp_ipv4addr_param *ipv4p;
3782 struct sockaddr_in *sin;
3784 sin = (struct sockaddr_in *)&ifa->address.sin;
3785 ipv4p = (struct sctp_ipv4addr_param *)parmh;
3786 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
3787 parmh->param_length = htons(len);
3788 ipv4p->addr = sin->sin_addr.s_addr;
3789 SCTP_BUF_LEN(mret) += len;
3795 struct sctp_ipv6addr_param *ipv6p;
3796 struct sockaddr_in6 *sin6;
3798 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3799 ipv6p = (struct sctp_ipv6addr_param *)parmh;
3800 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
3801 parmh->param_length = htons(len);
3802 memcpy(ipv6p->addr, &sin6->sin6_addr,
3803 sizeof(ipv6p->addr));
3804 /* clear embedded scope in the address */
3805 in6_clearscope((struct in6_addr *)ipv6p->addr);
3806 SCTP_BUF_LEN(mret) += len;
3818 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
3819 struct mbuf *m_at, int cnt_inits_to)
3821 struct sctp_vrf *vrf = NULL;
3822 int cnt, limit_out = 0, total_count;
3825 vrf_id = inp->def_vrf_id;
3826 SCTP_IPI_ADDR_RLOCK();
3827 vrf = sctp_find_vrf(vrf_id);
3829 SCTP_IPI_ADDR_RUNLOCK();
3832 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3833 struct sctp_ifa *sctp_ifap;
3834 struct sctp_ifn *sctp_ifnp;
3837 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
3839 cnt = SCTP_ADDRESS_LIMIT;
3842 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3843 if ((scope->loopback_scope == 0) &&
3844 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3846 * Skip loopback devices if loopback_scope
3851 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3852 if (sctp_is_address_in_scope(sctp_ifap,
3853 scope->ipv4_addr_legal,
3854 scope->ipv6_addr_legal,
3855 scope->loopback_scope,
3856 scope->ipv4_local_scope,
3858 scope->site_scope, 1) == 0) {
3862 if (cnt > SCTP_ADDRESS_LIMIT) {
3866 if (cnt > SCTP_ADDRESS_LIMIT) {
3873 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3875 if ((scope->loopback_scope == 0) &&
3876 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3878 * Skip loopback devices if
3879 * loopback_scope not set
3883 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3884 if (sctp_is_address_in_scope(sctp_ifap,
3885 scope->ipv4_addr_legal,
3886 scope->ipv6_addr_legal,
3887 scope->loopback_scope,
3888 scope->ipv4_local_scope,
3890 scope->site_scope, 0) == 0) {
3893 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
3904 if (total_count > SCTP_ADDRESS_LIMIT) {
3905 /* No more addresses */
3913 struct sctp_laddr *laddr;
3916 /* First, how many ? */
3917 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3918 if (laddr->ifa == NULL) {
3921 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3923 * Address being deleted by the system, dont
3927 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3929 * Address being deleted on this ep don't
3934 if (sctp_is_address_in_scope(laddr->ifa,
3935 scope->ipv4_addr_legal,
3936 scope->ipv6_addr_legal,
3937 scope->loopback_scope,
3938 scope->ipv4_local_scope,
3940 scope->site_scope, 1) == 0) {
3945 if (cnt > SCTP_ADDRESS_LIMIT) {
3949 * To get through a NAT we only list addresses if we have
3950 * more than one. That way if you just bind a single address
3951 * we let the source of the init dictate our address.
3954 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3956 if (laddr->ifa == NULL) {
3959 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3962 if (sctp_is_address_in_scope(laddr->ifa,
3963 scope->ipv4_addr_legal,
3964 scope->ipv6_addr_legal,
3965 scope->loopback_scope,
3966 scope->ipv4_local_scope,
3968 scope->site_scope, 0) == 0) {
3971 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3973 if (cnt >= SCTP_ADDRESS_LIMIT) {
3979 SCTP_IPI_ADDR_RUNLOCK();
3983 static struct sctp_ifa *
3984 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
3985 uint8_t dest_is_loop,
3986 uint8_t dest_is_priv,
3989 uint8_t dest_is_global = 0;
3991 /* dest_is_priv is true if destination is a private address */
3992 /* dest_is_loop is true if destination is a loopback addresses */
3995 * Here we determine if its a preferred address. A preferred address
3996 * means it is the same scope or higher scope then the destination.
3997 * L = loopback, P = private, G = global
3998 * ----------------------------------------- src | dest | result
3999 * ---------------------------------------- L | L | yes
4000 * ----------------------------------------- P | L |
4001 * yes-v4 no-v6 ----------------------------------------- G |
4002 * L | yes-v4 no-v6 ----------------------------------------- L
4003 * | P | no ----------------------------------------- P |
4004 * P | yes ----------------------------------------- G |
4005 * P | no ----------------------------------------- L | G
4006 * | no ----------------------------------------- P | G |
4007 * no ----------------------------------------- G | G |
4008 * yes -----------------------------------------
4011 if (ifa->address.sa.sa_family != fam) {
4012 /* forget mis-matched family */
4015 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
4018 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
4019 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
4020 /* Ok the address may be ok */
4021 if (fam == AF_INET6) {
4022 /* ok to use deprecated addresses? no lets not! */
4023 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4024 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
4027 if (ifa->src_is_priv && !ifa->src_is_loop) {
4029 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
4033 if (ifa->src_is_glob) {
4035 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
4041 * Now that we know what is what, implement or table this could in
4042 * theory be done slicker (it used to be), but this is
4043 * straightforward and easier to validate :-)
4045 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
4046 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
4047 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
4048 dest_is_loop, dest_is_priv, dest_is_global);
4050 if ((ifa->src_is_loop) && (dest_is_priv)) {
4051 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
4054 if ((ifa->src_is_glob) && (dest_is_priv)) {
4055 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
4058 if ((ifa->src_is_loop) && (dest_is_global)) {
4059 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
4062 if ((ifa->src_is_priv) && (dest_is_global)) {
4063 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
4066 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
4067 /* its a preferred address */
4071 static struct sctp_ifa *
4072 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
4073 uint8_t dest_is_loop,
4074 uint8_t dest_is_priv,
4077 uint8_t dest_is_global = 0;
4081 * Here we determine if its a acceptable address. A acceptable
4082 * address means it is the same scope or higher scope but we can
4083 * allow for NAT which means its ok to have a global dest and a
4086 * L = loopback, P = private, G = global
4087 * ----------------------------------------- src | dest | result
4088 * ----------------------------------------- L | L | yes
4089 * ----------------------------------------- P | L |
4090 * yes-v4 no-v6 ----------------------------------------- G |
4091 * L | yes ----------------------------------------- L |
4092 * P | no ----------------------------------------- P | P
4093 * | yes ----------------------------------------- G | P
4094 * | yes - May not work -----------------------------------------
4095 * L | G | no ----------------------------------------- P
4096 * | G | yes - May not work
4097 * ----------------------------------------- G | G | yes
4098 * -----------------------------------------
4101 if (ifa->address.sa.sa_family != fam) {
4102 /* forget non matching family */
4105 /* Ok the address may be ok */
4106 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
4109 if (fam == AF_INET6) {
4110 /* ok to use deprecated addresses? */
4111 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4114 if (ifa->src_is_priv) {
4115 /* Special case, linklocal to loop */
4121 * Now that we know what is what, implement our table. This could in
4122 * theory be done slicker (it used to be), but this is
4123 * straightforward and easier to validate :-)
4125 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
4128 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
4131 /* its an acceptable address */
4136 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4138 struct sctp_laddr *laddr;
4141 /* There are no restrictions, no TCB :-) */
4144 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4145 if (laddr->ifa == NULL) {
4146 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4150 if (laddr->ifa == ifa) {
4151 /* Yes it is on the list */
4160 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4162 struct sctp_laddr *laddr;
4166 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4167 if (laddr->ifa == NULL) {
4168 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4172 if ((laddr->ifa == ifa) && laddr->action == 0)
4181 static struct sctp_ifa *
4182 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
4185 int non_asoc_addr_ok,
4186 uint8_t dest_is_priv,
4187 uint8_t dest_is_loop,
4190 struct sctp_laddr *laddr, *starting_point;
4193 struct sctp_ifn *sctp_ifn;
4194 struct sctp_ifa *sctp_ifa, *sifa;
4195 struct sctp_vrf *vrf;
4198 vrf = sctp_find_vrf(vrf_id);
4202 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4203 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4204 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4206 * first question, is the ifn we will emit on in our list, if so, we
4207 * want such an address. Note that we first looked for a preferred
4211 /* is a preferred one on the interface we route out? */
4212 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4213 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4214 (non_asoc_addr_ok == 0))
4216 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
4221 if (sctp_is_addr_in_ep(inp, sifa)) {
4222 atomic_add_int(&sifa->refcount, 1);
4228 * ok, now we now need to find one on the list of the addresses. We
4229 * can't get one on the emitting interface so let's find first a
4230 * preferred one. If not that an acceptable one otherwise... we
4233 starting_point = inp->next_addr_touse;
4235 if (inp->next_addr_touse == NULL) {
4236 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4239 for (laddr = inp->next_addr_touse; laddr;
4240 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4241 if (laddr->ifa == NULL) {
4242 /* address has been removed */
4245 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4246 /* address is being deleted */
4249 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
4253 atomic_add_int(&sifa->refcount, 1);
4256 if (resettotop == 0) {
4257 inp->next_addr_touse = NULL;
4260 inp->next_addr_touse = starting_point;
4263 if (inp->next_addr_touse == NULL) {
4264 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4267 /* ok, what about an acceptable address in the inp */
4268 for (laddr = inp->next_addr_touse; laddr;
4269 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4270 if (laddr->ifa == NULL) {
4271 /* address has been removed */
4274 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4275 /* address is being deleted */
4278 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4282 atomic_add_int(&sifa->refcount, 1);
4285 if (resettotop == 0) {
4286 inp->next_addr_touse = NULL;
4287 goto once_again_too;
4290 * no address bound can be a source for the destination we are in
4298 static struct sctp_ifa *
4299 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
4300 struct sctp_tcb *stcb,
4301 struct sctp_nets *net,
4304 uint8_t dest_is_priv,
4305 uint8_t dest_is_loop,
4306 int non_asoc_addr_ok,
4309 struct sctp_laddr *laddr, *starting_point;
4311 struct sctp_ifn *sctp_ifn;
4312 struct sctp_ifa *sctp_ifa, *sifa;
4313 uint8_t start_at_beginning = 0;
4314 struct sctp_vrf *vrf;
4318 * first question, is the ifn we will emit on in our list, if so, we
4321 vrf = sctp_find_vrf(vrf_id);
4325 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4326 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4327 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4330 * first question, is the ifn we will emit on in our list? If so,
4331 * we want that one. First we look for a preferred. Second, we go
4332 * for an acceptable.
4335 /* first try for a preferred address on the ep */
4336 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4337 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4339 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4340 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4343 if (((non_asoc_addr_ok == 0) &&
4344 (sctp_is_addr_restricted(stcb, sifa))) ||
4345 (non_asoc_addr_ok &&
4346 (sctp_is_addr_restricted(stcb, sifa)) &&
4347 (!sctp_is_addr_pending(stcb, sifa)))) {
4348 /* on the no-no list */
4351 atomic_add_int(&sifa->refcount, 1);
4355 /* next try for an acceptable address on the ep */
4356 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4357 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4359 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4360 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4363 if (((non_asoc_addr_ok == 0) &&
4364 (sctp_is_addr_restricted(stcb, sifa))) ||
4365 (non_asoc_addr_ok &&
4366 (sctp_is_addr_restricted(stcb, sifa)) &&
4367 (!sctp_is_addr_pending(stcb, sifa)))) {
4368 /* on the no-no list */
4371 atomic_add_int(&sifa->refcount, 1);
4378 * if we can't find one like that then we must look at all addresses
4379 * bound to pick one at first preferable then secondly acceptable.
4381 starting_point = stcb->asoc.last_used_address;
4383 if (stcb->asoc.last_used_address == NULL) {
4384 start_at_beginning = 1;
4385 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4387 /* search beginning with the last used address */
4388 for (laddr = stcb->asoc.last_used_address; laddr;
4389 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4390 if (laddr->ifa == NULL) {
4391 /* address has been removed */
4394 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4395 /* address is being deleted */
4398 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
4401 if (((non_asoc_addr_ok == 0) &&
4402 (sctp_is_addr_restricted(stcb, sifa))) ||
4403 (non_asoc_addr_ok &&
4404 (sctp_is_addr_restricted(stcb, sifa)) &&
4405 (!sctp_is_addr_pending(stcb, sifa)))) {
4406 /* on the no-no list */
4409 stcb->asoc.last_used_address = laddr;
4410 atomic_add_int(&sifa->refcount, 1);
4413 if (start_at_beginning == 0) {
4414 stcb->asoc.last_used_address = NULL;
4415 goto sctp_from_the_top;
4417 /* now try for any higher scope than the destination */
4418 stcb->asoc.last_used_address = starting_point;
4419 start_at_beginning = 0;
4421 if (stcb->asoc.last_used_address == NULL) {
4422 start_at_beginning = 1;
4423 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4425 /* search beginning with the last used address */
4426 for (laddr = stcb->asoc.last_used_address; laddr;
4427 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4428 if (laddr->ifa == NULL) {
4429 /* address has been removed */
4432 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4433 /* address is being deleted */
4436 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4440 if (((non_asoc_addr_ok == 0) &&
4441 (sctp_is_addr_restricted(stcb, sifa))) ||
4442 (non_asoc_addr_ok &&
4443 (sctp_is_addr_restricted(stcb, sifa)) &&
4444 (!sctp_is_addr_pending(stcb, sifa)))) {
4445 /* on the no-no list */
4448 stcb->asoc.last_used_address = laddr;
4449 atomic_add_int(&sifa->refcount, 1);
4452 if (start_at_beginning == 0) {
4453 stcb->asoc.last_used_address = NULL;
4454 goto sctp_from_the_top2;
4459 static struct sctp_ifa *
4460 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
4461 struct sctp_tcb *stcb,
4462 int non_asoc_addr_ok,
4463 uint8_t dest_is_loop,
4464 uint8_t dest_is_priv,
4470 struct sctp_ifa *ifa, *sifa;
4471 int num_eligible_addr = 0;
4474 struct sockaddr_in6 sin6, lsa6;
4476 if (fam == AF_INET6) {
4477 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
4478 (void)sa6_recoverscope(&sin6);
4481 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4482 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4483 (non_asoc_addr_ok == 0))
4485 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4490 if (fam == AF_INET6 &&
4492 sifa->src_is_loop && sifa->src_is_priv) {
4494 * don't allow fe80::1 to be a src on loop ::1, we
4495 * don't list it to the peer so we will get an
4500 if (fam == AF_INET6 &&
4501 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
4502 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
4504 * link-local <-> link-local must belong to the same
4507 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
4508 (void)sa6_recoverscope(&lsa6);
4509 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
4516 * Check if the IPv6 address matches to next-hop. In the
4517 * mobile case, old IPv6 address may be not deleted from the
4518 * interface. Then, the interface has previous and new
4519 * addresses. We should use one corresponding to the
4520 * next-hop. (by micchie)
4523 if (stcb && fam == AF_INET6 &&
4524 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4525 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
4531 /* Avoid topologically incorrect IPv4 address */
4532 if (stcb && fam == AF_INET &&
4533 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4534 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
4539 if (((non_asoc_addr_ok == 0) &&
4540 (sctp_is_addr_restricted(stcb, sifa))) ||
4541 (non_asoc_addr_ok &&
4542 (sctp_is_addr_restricted(stcb, sifa)) &&
4543 (!sctp_is_addr_pending(stcb, sifa)))) {
4545 * It is restricted for some reason..
4546 * probably not yet added.
4551 if (num_eligible_addr >= addr_wanted) {
4554 num_eligible_addr++;
4561 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
4562 struct sctp_tcb *stcb,
4563 int non_asoc_addr_ok,
4564 uint8_t dest_is_loop,
4565 uint8_t dest_is_priv,
4568 struct sctp_ifa *ifa, *sifa;
4569 int num_eligible_addr = 0;
4571 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4572 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4573 (non_asoc_addr_ok == 0)) {
4576 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4582 if (((non_asoc_addr_ok == 0) &&
4583 (sctp_is_addr_restricted(stcb, sifa))) ||
4584 (non_asoc_addr_ok &&
4585 (sctp_is_addr_restricted(stcb, sifa)) &&
4586 (!sctp_is_addr_pending(stcb, sifa)))) {
4588 * It is restricted for some reason..
4589 * probably not yet added.
4594 num_eligible_addr++;
4596 return (num_eligible_addr);
4599 static struct sctp_ifa *
4600 sctp_choose_boundall(struct sctp_inpcb *inp,
4601 struct sctp_tcb *stcb,
4602 struct sctp_nets *net,
4605 uint8_t dest_is_priv,
4606 uint8_t dest_is_loop,
4607 int non_asoc_addr_ok,
4610 int cur_addr_num = 0, num_preferred = 0;
4612 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
4613 struct sctp_ifa *sctp_ifa, *sifa;
4615 struct sctp_vrf *vrf;
4618 * For boundall we can use any address in the association.
4619 * If non_asoc_addr_ok is set we can use any address (at least in
4620 * theory). So we look for preferred addresses first. If we find one,
4621 * we use it. Otherwise we next try to get an address on the
4622 * interface, which we should be able to do (unless non_asoc_addr_ok
4623 * is false and we are routed out that way). In these cases where we
4624 * can't use the address of the interface we go through all the
4625 * ifn's looking for an address we can use and fill that in. Punting
4626 * means we send back address 0, which will probably cause problems
4627 * actually since then IP will fill in the address of the route ifn,
4628 * which means we probably already rejected it.. i.e. here comes an
4631 vrf = sctp_find_vrf(vrf_id);
4635 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4636 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4637 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4638 if (sctp_ifn == NULL) {
4639 /* ?? We don't have this guy ?? */
4640 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
4641 goto bound_all_plan_b;
4643 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
4644 ifn_index, sctp_ifn->ifn_name);
4647 cur_addr_num = net->indx_of_eligible_next_to_use;
4649 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
4654 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
4655 num_preferred, sctp_ifn->ifn_name);
4656 if (num_preferred == 0) {
4658 * no eligible addresses, we must use some other interface
4659 * address if we can find one.
4661 goto bound_all_plan_b;
4664 * Ok we have num_eligible_addr set with how many we can use, this
4665 * may vary from call to call due to addresses being deprecated
4668 if (cur_addr_num >= num_preferred) {
4672 * select the nth address from the list (where cur_addr_num is the
4673 * nth) and 0 is the first one, 1 is the second one etc...
4675 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
4677 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4678 dest_is_priv, cur_addr_num, fam, ro);
4680 /* if sctp_ifa is NULL something changed??, fall to plan b. */
4682 atomic_add_int(&sctp_ifa->refcount, 1);
4684 /* save off where the next one we will want */
4685 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4690 * plan_b: Look at all interfaces and find a preferred address. If
4691 * no preferred fall through to plan_c.
4694 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
4695 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4696 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
4697 sctp_ifn->ifn_name);
4698 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4699 /* wrong base scope */
4700 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
4703 if ((sctp_ifn == looked_at) && looked_at) {
4704 /* already looked at this guy */
4705 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
4708 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
4709 dest_is_loop, dest_is_priv, fam);
4710 SCTPDBG(SCTP_DEBUG_OUTPUT2,
4711 "Found ifn:%p %d preferred source addresses\n",
4712 ifn, num_preferred);
4713 if (num_preferred == 0) {
4714 /* None on this interface. */
4715 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
4718 SCTPDBG(SCTP_DEBUG_OUTPUT2,
4719 "num preferred:%d on interface:%p cur_addr_num:%d\n",
4720 num_preferred, sctp_ifn, cur_addr_num);
4723 * Ok we have num_eligible_addr set with how many we can
4724 * use, this may vary from call to call due to addresses
4725 * being deprecated etc..
4727 if (cur_addr_num >= num_preferred) {
4730 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4731 dest_is_priv, cur_addr_num, fam, ro);
4735 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4736 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
4738 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
4739 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
4740 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
4741 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
4743 atomic_add_int(&sifa->refcount, 1);
4748 /* plan_c: do we have an acceptable address on the emit interface */
4749 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
4750 if (emit_ifn == NULL) {
4753 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
4754 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4755 (non_asoc_addr_ok == 0))
4757 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
4762 if (((non_asoc_addr_ok == 0) &&
4763 (sctp_is_addr_restricted(stcb, sifa))) ||
4764 (non_asoc_addr_ok &&
4765 (sctp_is_addr_restricted(stcb, sifa)) &&
4766 (!sctp_is_addr_pending(stcb, sifa)))) {
4768 * It is restricted for some reason..
4769 * probably not yet added.
4774 atomic_add_int(&sifa->refcount, 1);
4779 * plan_d: We are in trouble. No preferred address on the emit
4780 * interface. And not even a preferred address on all interfaces. Go
4781 * out and see if we can find an acceptable address somewhere
4782 * amongst all interfaces.
4784 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
4785 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4786 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4787 /* wrong base scope */
4790 if ((sctp_ifn == looked_at) && looked_at)
4791 /* already looked at this guy */
4794 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4795 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4796 (non_asoc_addr_ok == 0))
4798 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
4804 if (((non_asoc_addr_ok == 0) &&
4805 (sctp_is_addr_restricted(stcb, sifa))) ||
4806 (non_asoc_addr_ok &&
4807 (sctp_is_addr_restricted(stcb, sifa)) &&
4808 (!sctp_is_addr_pending(stcb, sifa)))) {
4810 * It is restricted for some
4811 * reason.. probably not yet added.
4816 atomic_add_int(&sifa->refcount, 1);
4821 * Ok we can find NO address to source from that is not on our
4822 * restricted list and non_asoc_address is NOT ok, or it is on our
4823 * restricted list. We can't source to it :-(
4830 /* tcb may be NULL */
4832 sctp_source_address_selection(struct sctp_inpcb *inp,
4833 struct sctp_tcb *stcb,
4835 struct sctp_nets *net,
4836 int non_asoc_addr_ok, uint32_t vrf_id)
4838 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
4841 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
4844 struct sctp_ifa *answer;
4845 uint8_t dest_is_priv, dest_is_loop;
4849 * Rules: - Find the route if needed, cache if I can. - Look at
4850 * interface address in route, Is it in the bound list. If so we
4851 * have the best source. - If not we must rotate amongst the
4856 * Do we need to pay attention to scope. We can have a private address
4857 * or a global address we are sourcing or sending to. So if we draw
4859 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4861 *------------------------------------------
4862 * source * dest * result
4863 * -----------------------------------------
4864 * <a> Private * Global * NAT
4865 * -----------------------------------------
4866 * <b> Private * Private * No problem
4867 * -----------------------------------------
4868 * <c> Global * Private * Huh, How will this work?
4869 * -----------------------------------------
4870 * <d> Global * Global * No Problem
4871 *------------------------------------------
4872 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4874 *------------------------------------------
4875 * source * dest * result
4876 * -----------------------------------------
4877 * <a> Linklocal * Global *
4878 * -----------------------------------------
4879 * <b> Linklocal * Linklocal * No problem
4880 * -----------------------------------------
4881 * <c> Global * Linklocal * Huh, How will this work?
4882 * -----------------------------------------
4883 * <d> Global * Global * No Problem
4884 *------------------------------------------
4885 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4887 * And then we add to that what happens if there are multiple addresses
4888 * assigned to an interface. Remember the ifa on a ifn is a linked
4889 * list of addresses. So one interface can have more than one IP
4890 * address. What happens if we have both a private and a global
4891 * address? Do we then use context of destination to sort out which
4892 * one is best? And what about NAT's sending P->G may get you a NAT
4893 * translation, or should you select the G thats on the interface in
4898 * - count the number of addresses on the interface.
4899 * - if it is one, no problem except case <c>.
4900 * For <a> we will assume a NAT out there.
4901 * - if there are more than one, then we need to worry about scope P
4902 * or G. We should prefer G -> G and P -> P if possible.
4903 * Then as a secondary fall back to mixed types G->P being a last
4905 * - The above all works for bound all, but bound specific we need to
4906 * use the same concept but instead only consider the bound
4907 * addresses. If the bound set is NOT assigned to the interface then
4908 * we must use rotation amongst the bound addresses..
4910 if (ro->ro_rt == NULL) {
4912 * Need a route to cache.
4914 SCTP_RTALLOC(ro, vrf_id);
4916 if (ro->ro_rt == NULL) {
4919 fam = to->sin_family;
4920 dest_is_priv = dest_is_loop = 0;
4921 /* Setup our scopes for the destination */
4924 /* Scope based on outbound address */
4925 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
4928 /* mark it as local */
4929 net->addr_is_local = 1;
4931 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
4937 /* Scope based on outbound address */
4938 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
4939 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
4941 * If the address is a loopback address, which
4942 * consists of "::1" OR "fe80::1%lo0", we are
4943 * loopback scope. But we don't use dest_is_priv
4944 * (link local addresses).
4948 /* mark it as local */
4949 net->addr_is_local = 1;
4951 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
4957 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
4958 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
4959 SCTP_IPI_ADDR_RLOCK();
4960 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4964 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
4965 dest_is_priv, dest_is_loop,
4966 non_asoc_addr_ok, fam);
4967 SCTP_IPI_ADDR_RUNLOCK();
4974 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
4975 vrf_id, dest_is_priv,
4977 non_asoc_addr_ok, fam);
4979 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
4984 SCTP_IPI_ADDR_RUNLOCK();
4989 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
4994 tlen = SCTP_BUF_LEN(control);
4997 * Independent of how many mbufs, find the c_type inside the control
4998 * structure and copy out the data.
5001 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
5002 /* not enough room for one more we are done. */
5005 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
5006 if (((int)cmh.cmsg_len + at) > tlen) {
5008 * this is real messed up since there is not enough
5009 * data here to cover the cmsg header. We are done.
5013 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
5014 (c_type == cmh.cmsg_type)) {
5015 /* found the one we want, copy it out */
5016 at += CMSG_ALIGN(sizeof(struct cmsghdr));
5017 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
5019 * space of cmsg_len after header not big
5024 m_copydata(control, at, cpsize, data);
5027 at += CMSG_ALIGN(cmh.cmsg_len);
5028 if (cmh.cmsg_len == 0) {
5037 static struct mbuf *
5038 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
5039 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
5041 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
5042 struct sctp_state_cookie *stc;
5043 struct sctp_paramhdr *ph;
5049 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
5050 sizeof(struct sctp_paramhdr)), 0,
5051 M_DONTWAIT, 1, MT_DATA);
5055 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
5056 if (copy_init == NULL) {
5060 #ifdef SCTP_MBUF_LOGGING
5061 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5066 if (SCTP_BUF_IS_EXTENDED(mat)) {
5067 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5069 mat = SCTP_BUF_NEXT(mat);
5073 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
5075 if (copy_initack == NULL) {
5077 sctp_m_freem(copy_init);
5080 #ifdef SCTP_MBUF_LOGGING
5081 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5086 if (SCTP_BUF_IS_EXTENDED(mat)) {
5087 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5089 mat = SCTP_BUF_NEXT(mat);
5093 /* easy side we just drop it on the end */
5094 ph = mtod(mret, struct sctp_paramhdr *);
5095 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
5096 sizeof(struct sctp_paramhdr);
5097 stc = (struct sctp_state_cookie *)((caddr_t)ph +
5098 sizeof(struct sctp_paramhdr));
5099 ph->param_type = htons(SCTP_STATE_COOKIE);
5100 ph->param_length = 0; /* fill in at the end */
5101 /* Fill in the stc cookie data */
5102 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
5104 /* tack the INIT and then the INIT-ACK onto the chain */
5107 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5108 cookie_sz += SCTP_BUF_LEN(m_at);
5109 if (SCTP_BUF_NEXT(m_at) == NULL) {
5110 SCTP_BUF_NEXT(m_at) = copy_init;
5115 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5116 cookie_sz += SCTP_BUF_LEN(m_at);
5117 if (SCTP_BUF_NEXT(m_at) == NULL) {
5118 SCTP_BUF_NEXT(m_at) = copy_initack;
5123 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5124 cookie_sz += SCTP_BUF_LEN(m_at);
5125 if (SCTP_BUF_NEXT(m_at) == NULL) {
5129 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
5131 /* no space, so free the entire chain */
5135 SCTP_BUF_LEN(sig) = 0;
5136 SCTP_BUF_NEXT(m_at) = sig;
5138 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
5139 memset(foo, 0, SCTP_SIGNATURE_SIZE);
5141 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
5142 cookie_sz += SCTP_SIGNATURE_SIZE;
5143 ph->param_length = htons(cookie_sz);
5149 sctp_get_ect(struct sctp_tcb *stcb,
5150 struct sctp_tmit_chunk *chk)
5152 uint8_t this_random;
5155 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
5158 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
5159 /* no nonce, always return ECT0 */
5160 return (SCTP_ECT0_BIT);
5162 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
5163 /* Peer does NOT support it, so we send a ECT0 only */
5164 return (SCTP_ECT0_BIT);
5167 return (SCTP_ECT0_BIT);
5169 if ((stcb->asoc.hb_random_idx > 3) ||
5170 ((stcb->asoc.hb_random_idx == 3) &&
5171 (stcb->asoc.hb_ect_randombit > 7))) {
5175 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
5176 memcpy(stcb->asoc.hb_random_values, &rndval,
5177 sizeof(stcb->asoc.hb_random_values));
5178 this_random = stcb->asoc.hb_random_values[0];
5179 stcb->asoc.hb_random_idx = 0;
5180 stcb->asoc.hb_ect_randombit = 0;
5182 if (stcb->asoc.hb_ect_randombit > 7) {
5183 stcb->asoc.hb_ect_randombit = 0;
5184 stcb->asoc.hb_random_idx++;
5185 if (stcb->asoc.hb_random_idx > 3) {
5189 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
5191 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
5193 /* ECN Nonce stuff */
5194 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
5195 stcb->asoc.hb_ect_randombit++;
5196 return (SCTP_ECT1_BIT);
5198 stcb->asoc.hb_ect_randombit++;
5199 return (SCTP_ECT0_BIT);
5204 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
5205 struct sctp_tcb *stcb, /* may be NULL */
5206 struct sctp_nets *net,
5207 struct sockaddr *to,
5209 uint32_t auth_offset,
5210 struct sctp_auth_chunk *auth,
5211 uint16_t auth_keyid,
5212 int nofragment_flag,
5214 struct sctp_tmit_chunk *chk,
5218 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5221 union sctp_sockstore *over_addr
5223 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
5226 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
5227 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
5228 * structure: - fill in the HMAC digest of any AUTH chunk in the
5229 * packet. - calculate and fill in the SCTP checksum. - prepend an
5230 * IP address header. - if boundall use INADDR_ANY. - if
5231 * boundspecific do source address selection. - set fragmentation
5232 * option for ipV4. - On return from IP output, check/adjust mtu
5233 * size of output interface and smallest_mtu size as well.
5235 /* Will need ifdefs around this */
5238 struct sctphdr *sctphdr;
5243 sctp_route_t *ro = NULL;
5244 struct udphdr *udp = NULL;
5246 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5247 struct socket *so = NULL;
5251 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
5252 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5257 vrf_id = stcb->asoc.vrf_id;
5259 vrf_id = inp->def_vrf_id;
5262 /* fill in the HMAC digest for any AUTH chunk in the packet */
5263 if ((auth != NULL) && (stcb != NULL)) {
5264 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
5266 /* Calculate the csum and fill in the length of the packet */
5267 sctphdr = mtod(m, struct sctphdr *);
5268 if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5270 (to->sa_family == AF_INET) &&
5271 (stcb->asoc.loopback_scope)) {
5272 sctphdr->checksum = 0;
5274 * This can probably now be taken out since my audit shows
5275 * no more bad pktlen's coming in. But we will wait a while
5278 packet_length = sctp_calculate_len(m);
5280 sctphdr->checksum = 0;
5281 csum = sctp_calculate_sum(m, &packet_length, 0);
5282 sctphdr->checksum = csum;
5285 if (to->sa_family == AF_INET) {
5286 struct ip *ip = NULL;
5287 sctp_route_t iproute;
5291 newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
5293 newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
5297 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5301 SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
5302 SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
5303 packet_length += sizeof(struct ip) + sizeof(struct udphdr);
5305 SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
5306 SCTP_BUF_LEN(newm) = sizeof(struct ip);
5307 packet_length += sizeof(struct ip);
5309 SCTP_BUF_NEXT(newm) = m;
5311 ip = mtod(m, struct ip *);
5312 ip->ip_v = IPVERSION;
5313 ip->ip_hl = (sizeof(struct ip) >> 2);
5315 tos_value = net->tos_flowlabel & 0x000000ff;
5317 tos_value = inp->ip_inp.inp.inp_ip_tos;
5319 if ((nofragment_flag) && (port == 0)) {
5320 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
5323 ip->ip_off = htons(IP_DF);
5328 /* FreeBSD has a function for ip_id's */
5329 ip->ip_id = ip_newid();
5331 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
5332 ip->ip_len = packet_length;
5334 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5336 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
5339 ip->ip_tos = (u_char)(tos_value & 0xfc);
5342 /* no association at all */
5343 ip->ip_tos = (tos_value & 0xfc);
5346 ip->ip_p = IPPROTO_UDP;
5348 ip->ip_p = IPPROTO_SCTP;
5353 memset(&iproute, 0, sizeof(iproute));
5354 memcpy(&ro->ro_dst, to, to->sa_len);
5356 ro = (sctp_route_t *) & net->ro;
5358 /* Now the address selection part */
5359 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
5361 /* call the routine to select the src address */
5362 if (net && out_of_asoc_ok == 0) {
5363 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5364 sctp_free_ifa(net->ro._s_addr);
5365 net->ro._s_addr = NULL;
5366 net->src_addr_selected = 0;
5372 if (net->src_addr_selected == 0) {
5373 /* Cache the source address */
5374 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
5377 net->src_addr_selected = 1;
5379 if (net->ro._s_addr == NULL) {
5380 /* No route to host */
5381 net->src_addr_selected = 0;
5384 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
5386 if (over_addr == NULL) {
5387 struct sctp_ifa *_lsrc;
5389 _lsrc = sctp_source_address_selection(inp, stcb, ro,
5393 if (_lsrc == NULL) {
5396 ip->ip_src = _lsrc->address.sin.sin_addr;
5397 sctp_free_ifa(_lsrc);
5399 ip->ip_src = over_addr->sin.sin_addr;
5400 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5404 udp = (struct udphdr *)(ip + 1);
5405 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5406 udp->uh_dport = port;
5407 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
5408 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
5411 * If source address selection fails and we find no route
5412 * then the ip_output should fail as well with a
5413 * NO_ROUTE_TO_HOST type error. We probably should catch
5414 * that somewhere and abort the association right away
5415 * (assuming this is an INIT being sent).
5417 if ((ro->ro_rt == NULL)) {
5419 * src addr selection failed to find a route (or
5420 * valid source addr), so we can't get there from
5424 SCTPDBG(SCTP_DEBUG_OUTPUT1,
5425 "%s: dropped packet - no valid source addr\n",
5428 SCTPDBG(SCTP_DEBUG_OUTPUT1,
5429 "Destination was ");
5430 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
5431 &net->ro._l_addr.sa);
5432 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
5433 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
5434 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
5435 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
5437 SCTP_FAILED_THRESHOLD,
5440 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5441 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
5443 * JRS 5/14/07 - If a
5445 * unreachable, the PF bit
5446 * is turned off. This
5447 * allows an unambiguous use
5449 * destinations that are
5450 * reachable but potentially
5452 * destination is set to the
5453 * unreachable state, also
5454 * set the destination to
5458 * Add debug message here if
5459 * destination is not in PF
5463 * Stop any running T3
5466 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
5467 net->dest_state &= ~SCTP_ADDR_PF;
5468 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
5474 if (net == stcb->asoc.primary_destination) {
5475 /* need a new primary */
5476 struct sctp_nets *alt;
5478 alt = sctp_find_alternate_net(stcb, net, 0);
5480 if (sctp_set_primary_addr(stcb,
5481 (struct sockaddr *)NULL,
5483 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
5484 if (net->ro._s_addr) {
5485 sctp_free_ifa(net->ro._s_addr);
5486 net->ro._s_addr = NULL;
5488 net->src_addr_selected = 0;
5494 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
5496 return (EHOSTUNREACH);
5498 if (ro != &iproute) {
5499 memcpy(&iproute, ro, sizeof(*ro));
5501 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
5502 (uint32_t) (ntohl(ip->ip_src.s_addr)));
5503 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
5504 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
5505 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
5508 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5509 /* failed to prepend data, give up */
5510 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5514 #ifdef SCTP_PACKET_LOGGING
5515 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5516 sctp_packet_log(m, packet_length);
5518 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5520 SCTP_ENABLE_UDP_CSUM(o_pak);
5522 /* send it out. table id is taken from stcb */
5523 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5524 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5525 so = SCTP_INP_SO(inp);
5526 SCTP_SOCKET_UNLOCK(so, 0);
5529 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
5530 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5531 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5532 atomic_add_int(&stcb->asoc.refcnt, 1);
5533 SCTP_TCB_UNLOCK(stcb);
5534 SCTP_SOCKET_LOCK(so, 0);
5535 SCTP_TCB_LOCK(stcb);
5536 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5539 SCTP_STAT_INCR(sctps_sendpackets);
5540 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5542 SCTP_STAT_INCR(sctps_senderrors);
5544 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
5546 /* free tempy routes */
5552 /* PMTU check versus smallest asoc MTU goes here */
5553 if ((ro->ro_rt != NULL) &&
5554 (net->ro._s_addr)) {
5557 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5559 mtu -= sizeof(struct udphdr);
5561 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
5562 #ifdef SCTP_PRINT_FOR_B_AND_M
5563 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
5565 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5568 } else if (ro->ro_rt == NULL) {
5569 /* route was freed */
5570 if (net->ro._s_addr &&
5571 net->src_addr_selected) {
5572 sctp_free_ifa(net->ro._s_addr);
5573 net->ro._s_addr = NULL;
5575 net->src_addr_selected = 0;
5581 else if (to->sa_family == AF_INET6) {
5583 struct ip6_hdr *ip6h;
5584 struct route_in6 ip6route;
5587 uint16_t flowBottom;
5588 u_char tosBottom, tosTop;
5589 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
5591 struct sockaddr_in6 lsa6_storage;
5593 u_short prev_port = 0;
5596 flowlabel = net->tos_flowlabel;
5598 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
5602 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
5604 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
5608 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5612 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
5613 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
5614 packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
5616 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
5617 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
5618 packet_length += sizeof(struct ip6_hdr);
5620 SCTP_BUF_NEXT(newm) = m;
5623 ip6h = mtod(m, struct ip6_hdr *);
5625 * We assume here that inp_flow is in host byte order within
5628 flowBottom = flowlabel & 0x0000ffff;
5629 flowTop = ((flowlabel & 0x000f0000) >> 16);
5630 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
5631 /* protect *sin6 from overwrite */
5632 sin6 = (struct sockaddr_in6 *)to;
5636 /* KAME hack: embed scopeid */
5637 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5638 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5642 memset(&ip6route, 0, sizeof(ip6route));
5643 ro = (sctp_route_t *) & ip6route;
5644 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
5646 ro = (sctp_route_t *) & net->ro;
5649 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5651 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
5654 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5657 /* we could get no asoc if it is a O-O-T-B packet */
5658 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5660 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
5662 ip6h->ip6_nxt = IPPROTO_UDP;
5664 ip6h->ip6_nxt = IPPROTO_SCTP;
5666 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
5667 ip6h->ip6_dst = sin6->sin6_addr;
5670 * Add SRC address selection here: we can only reuse to a
5671 * limited degree the kame src-addr-sel, since we can try
5672 * their selection but it may not be bound.
5674 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
5675 lsa6_tmp.sin6_family = AF_INET6;
5676 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
5678 if (net && out_of_asoc_ok == 0) {
5679 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5680 sctp_free_ifa(net->ro._s_addr);
5681 net->ro._s_addr = NULL;
5682 net->src_addr_selected = 0;
5688 if (net->src_addr_selected == 0) {
5689 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5690 /* KAME hack: embed scopeid */
5691 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5692 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5695 /* Cache the source address */
5696 net->ro._s_addr = sctp_source_address_selection(inp,
5702 (void)sa6_recoverscope(sin6);
5703 net->src_addr_selected = 1;
5705 if (net->ro._s_addr == NULL) {
5706 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
5707 net->src_addr_selected = 0;
5710 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
5712 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
5713 /* KAME hack: embed scopeid */
5714 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5715 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5718 if (over_addr == NULL) {
5719 struct sctp_ifa *_lsrc;
5721 _lsrc = sctp_source_address_selection(inp, stcb, ro,
5725 if (_lsrc == NULL) {
5728 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
5729 sctp_free_ifa(_lsrc);
5731 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
5732 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5734 (void)sa6_recoverscope(sin6);
5736 lsa6->sin6_port = inp->sctp_lport;
5738 if (ro->ro_rt == NULL) {
5740 * src addr selection failed to find a route (or
5741 * valid source addr), so we can't get there from
5747 * XXX: sa6 may not have a valid sin6_scope_id in the
5748 * non-SCOPEDROUTING case.
5750 bzero(&lsa6_storage, sizeof(lsa6_storage));
5751 lsa6_storage.sin6_family = AF_INET6;
5752 lsa6_storage.sin6_len = sizeof(lsa6_storage);
5753 lsa6_storage.sin6_addr = lsa6->sin6_addr;
5754 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
5755 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
5760 lsa6_storage.sin6_addr = lsa6->sin6_addr;
5761 lsa6_storage.sin6_port = inp->sctp_lport;
5762 lsa6 = &lsa6_storage;
5763 ip6h->ip6_src = lsa6->sin6_addr;
5766 udp = (struct udphdr *)(ip6h + 1);
5767 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5768 udp->uh_dport = port;
5769 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
5773 * We set the hop limit now since there is a good chance
5774 * that our ro pointer is now filled
5776 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
5777 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
5780 /* Copy to be sure something bad is not happening */
5781 sin6->sin6_addr = ip6h->ip6_dst;
5782 lsa6->sin6_addr = ip6h->ip6_src;
5785 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
5786 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
5787 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
5788 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
5789 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
5791 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5792 /* preserve the port and scope for link local send */
5793 prev_scope = sin6->sin6_scope_id;
5794 prev_port = sin6->sin6_port;
5796 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5797 /* failed to prepend data, give up */
5799 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5802 #ifdef SCTP_PACKET_LOGGING
5803 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5804 sctp_packet_log(m, packet_length);
5806 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5808 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
5809 udp->uh_sum = 0xffff;
5812 /* send it out. table id is taken from stcb */
5813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5814 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5815 so = SCTP_INP_SO(inp);
5816 SCTP_SOCKET_UNLOCK(so, 0);
5819 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
5820 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5821 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5822 atomic_add_int(&stcb->asoc.refcnt, 1);
5823 SCTP_TCB_UNLOCK(stcb);
5824 SCTP_SOCKET_LOCK(so, 0);
5825 SCTP_TCB_LOCK(stcb);
5826 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5830 /* for link local this must be done */
5831 sin6->sin6_scope_id = prev_scope;
5832 sin6->sin6_port = prev_port;
5834 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
5835 SCTP_STAT_INCR(sctps_sendpackets);
5836 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5838 SCTP_STAT_INCR(sctps_senderrors);
5841 /* Now if we had a temp route free it */
5846 /* PMTU check versus smallest asoc MTU goes here */
5847 if (ro->ro_rt == NULL) {
5848 /* Route was freed */
5849 if (net->ro._s_addr &&
5850 net->src_addr_selected) {
5851 sctp_free_ifa(net->ro._s_addr);
5852 net->ro._s_addr = NULL;
5854 net->src_addr_selected = 0;
5856 if ((ro->ro_rt != NULL) &&
5857 (net->ro._s_addr)) {
5860 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5862 (stcb->asoc.smallest_mtu > mtu)) {
5863 #ifdef SCTP_PRINT_FOR_B_AND_M
5864 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
5867 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5870 net->mtu -= sizeof(struct udphdr);
5874 if (ND_IFINFO(ifp)->linkmtu &&
5875 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
5876 #ifdef SCTP_PRINT_FOR_B_AND_M
5877 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
5878 ND_IFINFO(ifp)->linkmtu);
5880 sctp_mtu_size_reset(inp,
5882 ND_IFINFO(ifp)->linkmtu);
5890 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
5891 ((struct sockaddr *)to)->sa_family);
5893 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5900 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
5901 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5906 struct mbuf *m, *m_at, *mp_last;
5907 struct sctp_nets *net;
5908 struct sctp_init_msg *initm;
5909 struct sctp_supported_addr_param *sup_addr;
5910 struct sctp_ecn_supported_param *ecn;
5911 struct sctp_prsctp_supported_param *prsctp;
5912 struct sctp_ecn_nonce_supported_param *ecn_nonce;
5913 struct sctp_supported_chunk_types_param *pr_supported;
5914 int cnt_inits_to = 0;
5919 /* INIT's always go to the primary (and usually ONLY address) */
5921 net = stcb->asoc.primary_destination;
5923 net = TAILQ_FIRST(&stcb->asoc.nets);
5928 /* we confirm any address we send an INIT to */
5929 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5930 (void)sctp_set_primary_addr(stcb, NULL, net);
5932 /* we confirm any address we send an INIT to */
5933 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5935 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
5937 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
5939 * special hook, if we are sending to link local it will not
5940 * show up in our private address count.
5942 struct sockaddr_in6 *sin6l;
5944 sin6l = &net->ro._l_addr.sin6;
5945 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
5949 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5950 /* This case should not happen */
5951 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
5954 /* start the INIT timer */
5955 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
5957 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
5959 /* No memory, INIT timer will re-attempt. */
5960 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
5963 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
5965 * assume peer supports asconf in order to be able to queue local
5966 * address changes while an INIT is in flight and before the assoc
5969 stcb->asoc.peer_supports_asconf = 1;
5970 /* Now lets put the SCTP header in place */
5971 initm = mtod(m, struct sctp_init_msg *);
5972 initm->sh.src_port = inp->sctp_lport;
5973 initm->sh.dest_port = stcb->rport;
5974 initm->sh.v_tag = 0;
5975 initm->sh.checksum = 0; /* calculate later */
5976 /* now the chunk header */
5977 initm->msg.ch.chunk_type = SCTP_INITIATION;
5978 initm->msg.ch.chunk_flags = 0;
5979 /* fill in later from mbuf we build */
5980 initm->msg.ch.chunk_length = 0;
5981 /* place in my tag */
5982 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
5983 /* set up some of the credits. */
5984 initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
5985 SCTP_MINIMAL_RWND));
5987 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
5988 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
5989 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
5990 /* now the address restriction */
5991 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
5993 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
5995 /* we support 2 types: IPv6/IPv4 */
5996 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
5997 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
5998 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
6000 /* we support 1 type: IPv4 */
6001 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
6002 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
6003 sup_addr->addr_type[1] = htons(0); /* this is the padding */
6005 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
6007 if (inp->sctp_ep.adaptation_layer_indicator) {
6008 struct sctp_adaptation_layer_indication *ali;
6010 ali = (struct sctp_adaptation_layer_indication *)(
6011 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
6012 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
6013 ali->ph.param_length = htons(sizeof(*ali));
6014 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
6015 SCTP_BUF_LEN(m) += sizeof(*ali);
6016 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
6019 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
6020 sizeof(*sup_addr) + sizeof(uint16_t));
6023 /* now any cookie time extensions */
6024 if (stcb->asoc.cookie_preserve_req) {
6025 struct sctp_cookie_perserve_param *cookie_preserve;
6027 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
6028 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
6029 cookie_preserve->ph.param_length = htons(
6030 sizeof(*cookie_preserve));
6031 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
6032 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
6033 ecn = (struct sctp_ecn_supported_param *)(
6034 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
6035 stcb->asoc.cookie_preserve_req = 0;
6038 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
6039 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
6040 ecn->ph.param_length = htons(sizeof(*ecn));
6041 SCTP_BUF_LEN(m) += sizeof(*ecn);
6042 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
6045 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
6047 /* And now tell the peer we do pr-sctp */
6048 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
6049 prsctp->ph.param_length = htons(sizeof(*prsctp));
6050 SCTP_BUF_LEN(m) += sizeof(*prsctp);
6052 /* And now tell the peer we do all the extensions */
6053 pr_supported = (struct sctp_supported_chunk_types_param *)
6054 ((caddr_t)prsctp + sizeof(*prsctp));
6055 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
6057 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
6058 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
6059 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
6060 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
6061 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
6062 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
6063 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
6065 * EY if the initiator supports nr_sacks, need to report that to
6066 * responder in INIT chunk
6068 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
6069 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
6070 p_len = sizeof(*pr_supported) + num_ext;
6071 pr_supported->ph.param_length = htons(p_len);
6072 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
6073 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6075 /* ECN nonce: And now tell the peer we support ECN nonce */
6076 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
6077 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
6078 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
6079 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
6080 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
6081 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
6083 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
6084 /* Add NAT friendly parameter */
6085 struct sctp_paramhdr *ph;
6087 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6088 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
6089 ph->param_length = htons(sizeof(struct sctp_paramhdr));
6090 SCTP_BUF_LEN(m) += sizeof(sizeof(struct sctp_paramhdr));
6092 /* add authentication parameters */
6093 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
6094 struct sctp_auth_random *randp;
6095 struct sctp_auth_hmac_algo *hmacs;
6096 struct sctp_auth_chunk_list *chunks;
6098 /* attach RANDOM parameter, if available */
6099 if (stcb->asoc.authinfo.random != NULL) {
6100 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6101 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
6102 #ifdef SCTP_AUTH_DRAFT_04
6103 randp->ph.param_type = htons(SCTP_RANDOM);
6104 randp->ph.param_length = htons(p_len);
6105 bcopy(stcb->asoc.authinfo.random->key,
6107 stcb->asoc.authinfo.random_len);
6109 /* random key already contains the header */
6110 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
6112 /* zero out any padding required */
6113 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
6114 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6116 /* add HMAC_ALGO parameter */
6117 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6118 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
6119 (uint8_t *) hmacs->hmac_ids);
6121 p_len += sizeof(*hmacs);
6122 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6123 hmacs->ph.param_length = htons(p_len);
6124 /* zero out any padding required */
6125 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
6126 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6128 /* add CHUNKS parameter */
6129 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6130 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
6131 chunks->chunk_types);
6133 p_len += sizeof(*chunks);
6134 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6135 chunks->ph.param_length = htons(p_len);
6136 /* zero out any padding required */
6137 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
6138 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6142 /* now the addresses */
6144 struct sctp_scoping scp;
6147 * To optimize this we could put the scoping stuff into a
6148 * structure and remove the individual uint8's from the
6149 * assoc structure. Then we could just sifa in the address
6150 * within the stcb.. but for now this is a quick hack to get
6151 * the address stuff teased apart.
6153 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
6154 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
6155 scp.loopback_scope = stcb->asoc.loopback_scope;
6156 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6157 scp.local_scope = stcb->asoc.local_scope;
6158 scp.site_scope = stcb->asoc.site_scope;
6160 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
6163 /* calulate the size and update pkt header and chunk header */
6165 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
6166 if (SCTP_BUF_NEXT(m_at) == NULL)
6168 p_len += SCTP_BUF_LEN(m_at);
6170 initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
6172 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6173 * here since the timer will drive a retranmission.
6176 /* I don't expect this to execute but we will be safe here */
6178 if ((padval) && (mp_last)) {
6180 * The compiler worries that mp_last may not be set even
6181 * though I think it is impossible :-> however we add
6182 * mp_last here just in case.
6184 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
6186 /* Houston we have a problem, no space */
6192 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
6193 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
6194 (struct sockaddr *)&net->ro._l_addr,
6195 m, 0, NULL, 0, 0, 0, NULL, 0, net->port, so_locked, NULL);
6196 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
6197 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6198 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
6199 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6203 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
6204 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
6207 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
6208 * being equal to the beginning of the params i.e. (iphlen +
6209 * sizeof(struct sctp_init_msg) parse through the parameters to the
6210 * end of the mbuf verifying that all parameters are known.
6212 * For unknown parameters build and return a mbuf with
6213 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
6214 * processing this chunk stop, and set *abort_processing to 1.
6216 * By having param_offset be pre-set to where parameters begin it is
6217 * hoped that this routine may be reused in the future by new
6220 struct sctp_paramhdr *phdr, params;
6222 struct mbuf *mat, *op_err;
6223 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
6224 int at, limit, pad_needed;
6225 uint16_t ptype, plen, padded_size;
6228 *abort_processing = 0;
6231 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
6234 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
6235 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
6236 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
6237 ptype = ntohs(phdr->param_type);
6238 plen = ntohs(phdr->param_length);
6239 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
6240 /* wacked parameter */
6241 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
6244 limit -= SCTP_SIZE32(plen);
6246 * All parameters for all chunks that we know/understand are
6247 * listed here. We process them other places and make
6248 * appropriate stop actions per the upper bits. However this
6249 * is the generic routine processor's can call to get back
6250 * an operr.. to either incorporate (init-ack) or send.
6252 padded_size = SCTP_SIZE32(plen);
6254 /* Param's with variable size */
6255 case SCTP_HEARTBEAT_INFO:
6256 case SCTP_STATE_COOKIE:
6257 case SCTP_UNRECOG_PARAM:
6258 case SCTP_ERROR_CAUSE_IND:
6262 /* Param's with variable size within a range */
6263 case SCTP_CHUNK_LIST:
6264 case SCTP_SUPPORTED_CHUNK_EXT:
6265 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
6266 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
6271 case SCTP_SUPPORTED_ADDRTYPE:
6272 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
6273 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
6279 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
6280 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
6285 case SCTP_SET_PRIM_ADDR:
6286 case SCTP_DEL_IP_ADDRESS:
6287 case SCTP_ADD_IP_ADDRESS:
6288 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
6289 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
6290 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
6295 /* Param's with a fixed size */
6296 case SCTP_IPV4_ADDRESS:
6297 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
6298 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
6303 case SCTP_IPV6_ADDRESS:
6304 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
6305 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
6310 case SCTP_COOKIE_PRESERVE:
6311 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
6312 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
6317 case SCTP_HAS_NAT_SUPPORT:
6320 case SCTP_ECN_NONCE_SUPPORTED:
6321 case SCTP_PRSCTP_SUPPORTED:
6323 if (padded_size != sizeof(struct sctp_paramhdr)) {
6324 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
6329 case SCTP_ECN_CAPABLE:
6330 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
6331 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
6336 case SCTP_ULP_ADAPTATION:
6337 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
6338 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
6343 case SCTP_SUCCESS_REPORT:
6344 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
6345 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
6350 case SCTP_HOSTNAME_ADDRESS:
6352 /* We can NOT handle HOST NAME addresses!! */
6355 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
6356 *abort_processing = 1;
6357 if (op_err == NULL) {
6358 /* Ok need to try to get a mbuf */
6360 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6362 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6365 l_len += sizeof(struct sctp_paramhdr);
6366 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6368 SCTP_BUF_LEN(op_err) = 0;
6370 * pre-reserve space for ip
6371 * and sctp header and
6375 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6377 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6379 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6380 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6384 /* If we have space */
6385 struct sctp_paramhdr s;
6388 uint32_t cpthis = 0;
6390 pad_needed = 4 - (err_at % 4);
6391 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6392 err_at += pad_needed;
6394 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
6395 s.param_length = htons(sizeof(s) + plen);
6396 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6397 err_at += sizeof(s);
6398 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6400 sctp_m_freem(op_err);
6402 * we are out of memory but
6403 * we still need to have a
6404 * look at what to do (the
6405 * system is in trouble
6410 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6418 * we do not recognize the parameter figure out what
6421 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
6422 if ((ptype & 0x4000) == 0x4000) {
6423 /* Report bit is set?? */
6424 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
6425 if (op_err == NULL) {
6428 /* Ok need to try to get an mbuf */
6430 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6432 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6435 l_len += sizeof(struct sctp_paramhdr);
6436 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6438 SCTP_BUF_LEN(op_err) = 0;
6440 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6442 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6444 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6445 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6449 /* If we have space */
6450 struct sctp_paramhdr s;
6453 uint32_t cpthis = 0;
6455 pad_needed = 4 - (err_at % 4);
6456 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6457 err_at += pad_needed;
6459 s.param_type = htons(SCTP_UNRECOG_PARAM);
6460 s.param_length = htons(sizeof(s) + plen);
6461 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6462 err_at += sizeof(s);
6463 if (plen > sizeof(tempbuf)) {
6464 plen = sizeof(tempbuf);
6466 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6468 sctp_m_freem(op_err);
6470 * we are out of memory but
6471 * we still need to have a
6472 * look at what to do (the
6473 * system is in trouble
6477 goto more_processing;
6479 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6484 if ((ptype & 0x8000) == 0x0000) {
6485 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
6488 /* skip this chunk and continue processing */
6489 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
6490 at += SCTP_SIZE32(plen);
6495 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
6499 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
6500 *abort_processing = 1;
6501 if ((op_err == NULL) && phdr) {
6505 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6507 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6509 l_len += (2 * sizeof(struct sctp_paramhdr));
6510 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6512 SCTP_BUF_LEN(op_err) = 0;
6514 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6516 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6518 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6519 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6522 if ((op_err) && phdr) {
6523 struct sctp_paramhdr s;
6526 uint32_t cpthis = 0;
6528 pad_needed = 4 - (err_at % 4);
6529 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6530 err_at += pad_needed;
6532 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6533 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
6534 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6535 err_at += sizeof(s);
6536 /* Only copy back the p-hdr that caused the issue */
6537 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
6543 sctp_are_there_new_addresses(struct sctp_association *asoc,
6544 struct mbuf *in_initpkt, int iphlen, int offset)
6547 * Given a INIT packet, look through the packet to verify that there
6548 * are NO new addresses. As we go through the parameters add reports
6549 * of any un-understood parameters that require an error. Also we
6550 * must return (1) to drop the packet if we see a un-understood
6551 * parameter that tells us to drop the chunk.
6553 struct sockaddr_in sin4, *sa4;
6556 struct sockaddr_in6 sin6, *sa6;
6559 struct sockaddr *sa_touse;
6560 struct sockaddr *sa;
6561 struct sctp_paramhdr *phdr, params;
6565 struct ip6_hdr *ip6h;
6569 uint16_t ptype, plen;
6572 struct sctp_nets *net;
6574 memset(&sin4, 0, sizeof(sin4));
6576 memset(&sin6, 0, sizeof(sin6));
6578 sin4.sin_family = AF_INET;
6579 sin4.sin_len = sizeof(sin4);
6581 sin6.sin6_family = AF_INET6;
6582 sin6.sin6_len = sizeof(sin6);
6585 /* First what about the src address of the pkt ? */
6586 iph = mtod(in_initpkt, struct ip *);
6587 switch (iph->ip_v) {
6589 /* source addr is IPv4 */
6590 sin4.sin_addr = iph->ip_src;
6591 sa_touse = (struct sockaddr *)&sin4;
6594 case IPV6_VERSION >> 4:
6595 /* source addr is IPv6 */
6596 ip6h = mtod(in_initpkt, struct ip6_hdr *);
6597 sin6.sin6_addr = ip6h->ip6_src;
6598 sa_touse = (struct sockaddr *)&sin6;
6606 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6607 sa = (struct sockaddr *)&net->ro._l_addr;
6608 if (sa->sa_family == sa_touse->sa_family) {
6609 if (sa->sa_family == AF_INET) {
6610 sa4 = (struct sockaddr_in *)sa;
6611 if (sa4->sin_addr.s_addr ==
6612 sin4.sin_addr.s_addr) {
6618 if (sa->sa_family == AF_INET6) {
6619 sa6 = (struct sockaddr_in6 *)sa;
6620 if (SCTP6_ARE_ADDR_EQUAL(sa6,
6630 /* New address added! no need to look futher. */
6633 /* Ok so far lets munge through the rest of the packet */
6637 offset += sizeof(struct sctp_init_chunk);
6638 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
6640 ptype = ntohs(phdr->param_type);
6641 plen = ntohs(phdr->param_length);
6642 if (ptype == SCTP_IPV4_ADDRESS) {
6643 struct sctp_ipv4addr_param *p4, p4_buf;
6645 phdr = sctp_get_next_param(mat, offset,
6646 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
6647 if (plen != sizeof(struct sctp_ipv4addr_param) ||
6651 p4 = (struct sctp_ipv4addr_param *)phdr;
6652 sin4.sin_addr.s_addr = p4->addr;
6653 sa_touse = (struct sockaddr *)&sin4;
6654 } else if (ptype == SCTP_IPV6_ADDRESS) {
6655 struct sctp_ipv6addr_param *p6, p6_buf;
6657 phdr = sctp_get_next_param(mat, offset,
6658 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
6659 if (plen != sizeof(struct sctp_ipv6addr_param) ||
6663 p6 = (struct sctp_ipv6addr_param *)phdr;
6665 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6668 sa_touse = (struct sockaddr *)&sin4;
6671 /* ok, sa_touse points to one to check */
6673 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6674 sa = (struct sockaddr *)&net->ro._l_addr;
6675 if (sa->sa_family != sa_touse->sa_family) {
6678 if (sa->sa_family == AF_INET) {
6679 sa4 = (struct sockaddr_in *)sa;
6680 if (sa4->sin_addr.s_addr ==
6681 sin4.sin_addr.s_addr) {
6687 if (sa->sa_family == AF_INET6) {
6688 sa6 = (struct sockaddr_in6 *)sa;
6689 if (SCTP6_ARE_ADDR_EQUAL(
6698 /* New addr added! no need to look further */
6702 offset += SCTP_SIZE32(plen);
6703 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
6709 * Given a MBUF chain that was sent into us containing an INIT. Build a
6710 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
6711 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
6712 * message (i.e. the struct sctp_init_msg).
6715 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
6716 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
6717 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
6719 struct sctp_association *asoc;
6720 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
6721 struct sctp_init_msg *initackm_out;
6722 struct sctp_ecn_supported_param *ecn;
6723 struct sctp_prsctp_supported_param *prsctp;
6724 struct sctp_ecn_nonce_supported_param *ecn_nonce;
6725 struct sctp_supported_chunk_types_param *pr_supported;
6726 union sctp_sockstore store, store1, *over_addr;
6727 struct sockaddr_in *sin, *to_sin;
6730 struct sockaddr_in6 *sin6, *to_sin6;
6736 struct ip6_hdr *ip6;
6739 struct sockaddr *to;
6740 struct sctp_state_cookie stc;
6741 struct sctp_nets *net = NULL;
6742 uint8_t *signature = NULL;
6743 int cnt_inits_to = 0;
6744 uint16_t his_limit, i_want;
6745 int abort_flag, padval;
6748 int nat_friendly = 0;
6756 if ((asoc != NULL) &&
6757 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
6758 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
6759 /* new addresses, out of here in non-cookie-wait states */
6761 * Send a ABORT, we don't add the new address error clause
6762 * though we even set the T bit and copy in the 0 tag.. this
6763 * looks no different than if no listener was present.
6765 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
6769 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
6770 (offset + sizeof(struct sctp_init_chunk)),
6771 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
6774 sctp_send_abort(init_pkt, iphlen, sh,
6775 init_chk->init.initiate_tag, op_err, vrf_id, port);
6778 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
6780 /* No memory, INIT timer will re-attempt. */
6782 sctp_m_freem(op_err);
6785 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
6787 /* the time I built cookie */
6788 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
6790 /* populate any tie tags */
6792 /* unlock before tag selections */
6793 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
6794 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
6795 stc.cookie_life = asoc->cookie_life;
6796 net = asoc->primary_destination;
6798 stc.tie_tag_my_vtag = 0;
6799 stc.tie_tag_peer_vtag = 0;
6800 /* life I will award this cookie */
6801 stc.cookie_life = inp->sctp_ep.def_cookie_life;
6804 /* copy in the ports for later check */
6805 stc.myport = sh->dest_port;
6806 stc.peerport = sh->src_port;
6809 * If we wanted to honor cookie life extentions, we would add to
6810 * stc.cookie_life. For now we should NOT honor any extension
6812 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
6813 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6814 struct inpcb *in_inp;
6816 /* Its a V6 socket */
6817 in_inp = (struct inpcb *)inp;
6818 stc.ipv6_addr_legal = 1;
6819 /* Now look at the binding flag to see if V4 will be legal */
6820 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
6821 stc.ipv4_addr_legal = 1;
6823 /* V4 addresses are NOT legal on the association */
6824 stc.ipv4_addr_legal = 0;
6827 /* Its a V4 socket, no - V6 */
6828 stc.ipv4_addr_legal = 1;
6829 stc.ipv6_addr_legal = 0;
6832 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
6837 /* now for scope setup */
6838 memset((caddr_t)&store, 0, sizeof(store));
6839 memset((caddr_t)&store1, 0, sizeof(store1));
6841 to_sin = &store1.sin;
6844 to_sin6 = &store1.sin6;
6846 iph = mtod(init_pkt, struct ip *);
6847 /* establish the to_addr's */
6848 switch (iph->ip_v) {
6850 to_sin->sin_port = sh->dest_port;
6851 to_sin->sin_family = AF_INET;
6852 to_sin->sin_len = sizeof(struct sockaddr_in);
6853 to_sin->sin_addr = iph->ip_dst;
6856 case IPV6_VERSION >> 4:
6857 ip6 = mtod(init_pkt, struct ip6_hdr *);
6858 to_sin6->sin6_addr = ip6->ip6_dst;
6859 to_sin6->sin6_scope_id = 0;
6860 to_sin6->sin6_port = sh->dest_port;
6861 to_sin6->sin6_family = AF_INET6;
6862 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
6871 to = (struct sockaddr *)&store;
6872 switch (iph->ip_v) {
6875 sin->sin_family = AF_INET;
6876 sin->sin_len = sizeof(struct sockaddr_in);
6877 sin->sin_port = sh->src_port;
6878 sin->sin_addr = iph->ip_src;
6879 /* lookup address */
6880 stc.address[0] = sin->sin_addr.s_addr;
6884 stc.addr_type = SCTP_IPV4_ADDRESS;
6885 /* local from address */
6886 stc.laddress[0] = to_sin->sin_addr.s_addr;
6887 stc.laddress[1] = 0;
6888 stc.laddress[2] = 0;
6889 stc.laddress[3] = 0;
6890 stc.laddr_type = SCTP_IPV4_ADDRESS;
6891 /* scope_id is only for v6 */
6893 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
6894 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
6899 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
6900 /* Must use the address in this case */
6901 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
6902 stc.loopback_scope = 1;
6905 stc.local_scope = 0;
6910 case IPV6_VERSION >> 4:
6912 ip6 = mtod(init_pkt, struct ip6_hdr *);
6913 sin6->sin6_family = AF_INET6;
6914 sin6->sin6_len = sizeof(struct sockaddr_in6);
6915 sin6->sin6_port = sh->src_port;
6916 sin6->sin6_addr = ip6->ip6_src;
6917 /* lookup address */
6918 memcpy(&stc.address, &sin6->sin6_addr,
6919 sizeof(struct in6_addr));
6920 sin6->sin6_scope_id = 0;
6921 stc.addr_type = SCTP_IPV6_ADDRESS;
6923 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
6925 * FIX ME: does this have scope from
6928 (void)sa6_recoverscope(sin6);
6929 stc.scope_id = sin6->sin6_scope_id;
6930 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6931 stc.loopback_scope = 1;
6932 stc.local_scope = 0;
6935 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6937 * If the new destination is a
6938 * LINK_LOCAL we must have common
6939 * both site and local scope. Don't
6940 * set local scope though since we
6941 * must depend on the source to be
6942 * added implicitly. We cannot
6943 * assure just because we share one
6944 * link that all links are common.
6946 stc.local_scope = 0;
6950 * we start counting for the private
6951 * address stuff at 1. since the
6952 * link local we source from won't
6953 * show up in our scoped count.
6957 * pull out the scope_id from
6961 * FIX ME: does this have scope from
6964 (void)sa6_recoverscope(sin6);
6965 stc.scope_id = sin6->sin6_scope_id;
6966 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6967 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
6969 * If the new destination is
6970 * SITE_LOCAL then we must have site
6975 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
6976 stc.laddr_type = SCTP_IPV6_ADDRESS;
6986 /* set the scope per the existing tcb */
6989 struct sctp_nets *lnet;
6993 stc.loopback_scope = asoc->loopback_scope;
6994 stc.ipv4_scope = asoc->ipv4_local_scope;
6995 stc.site_scope = asoc->site_scope;
6996 stc.local_scope = asoc->local_scope;
6998 /* Why do we not consider IPv4 LL addresses? */
6999 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
7000 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
7001 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
7003 * if we have a LL address, start
7011 /* use the net pointer */
7012 to = (struct sockaddr *)&net->ro._l_addr;
7013 switch (to->sa_family) {
7015 sin = (struct sockaddr_in *)to;
7016 stc.address[0] = sin->sin_addr.s_addr;
7020 stc.addr_type = SCTP_IPV4_ADDRESS;
7021 if (net->src_addr_selected == 0) {
7023 * strange case here, the INIT should have
7024 * did the selection.
7026 net->ro._s_addr = sctp_source_address_selection(inp,
7027 stcb, (sctp_route_t *) & net->ro,
7029 if (net->ro._s_addr == NULL)
7032 net->src_addr_selected = 1;
7035 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
7036 stc.laddress[1] = 0;
7037 stc.laddress[2] = 0;
7038 stc.laddress[3] = 0;
7039 stc.laddr_type = SCTP_IPV4_ADDRESS;
7043 sin6 = (struct sockaddr_in6 *)to;
7044 memcpy(&stc.address, &sin6->sin6_addr,
7045 sizeof(struct in6_addr));
7046 stc.addr_type = SCTP_IPV6_ADDRESS;
7047 if (net->src_addr_selected == 0) {
7049 * strange case here, the INIT should have
7050 * did the selection.
7052 net->ro._s_addr = sctp_source_address_selection(inp,
7053 stcb, (sctp_route_t *) & net->ro,
7055 if (net->ro._s_addr == NULL)
7058 net->src_addr_selected = 1;
7060 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
7061 sizeof(struct in6_addr));
7062 stc.laddr_type = SCTP_IPV6_ADDRESS;
7067 /* Now lets put the SCTP header in place */
7068 initackm_out = mtod(m, struct sctp_init_msg *);
7069 initackm_out->sh.src_port = inp->sctp_lport;
7070 initackm_out->sh.dest_port = sh->src_port;
7071 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
7072 /* Save it off for quick ref */
7073 stc.peers_vtag = init_chk->init.initiate_tag;
7074 initackm_out->sh.checksum = 0; /* calculate later */
7076 memcpy(stc.identification, SCTP_VERSION_STRING,
7077 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
7078 /* now the chunk header */
7079 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
7080 initackm_out->msg.ch.chunk_flags = 0;
7081 /* fill in later from mbuf we build */
7082 initackm_out->msg.ch.chunk_length = 0;
7083 /* place in my tag */
7084 if ((asoc != NULL) &&
7085 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7086 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
7087 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
7088 /* re-use the v-tags and init-seq here */
7089 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
7090 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
7092 uint32_t vtag, itsn;
7094 if (hold_inp_lock) {
7095 SCTP_INP_INCR_REF(inp);
7096 SCTP_INP_RUNLOCK(inp);
7099 atomic_add_int(&asoc->refcnt, 1);
7100 SCTP_TCB_UNLOCK(stcb);
7102 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7103 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
7105 * Got a duplicate vtag on some guy behind a
7106 * nat make sure we don't use it.
7110 initackm_out->msg.init.initiate_tag = htonl(vtag);
7111 /* get a TSN to use too */
7112 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
7113 initackm_out->msg.init.initial_tsn = htonl(itsn);
7114 SCTP_TCB_LOCK(stcb);
7115 atomic_add_int(&asoc->refcnt, -1);
7117 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7118 initackm_out->msg.init.initiate_tag = htonl(vtag);
7119 /* get a TSN to use too */
7120 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
7122 if (hold_inp_lock) {
7123 SCTP_INP_RLOCK(inp);
7124 SCTP_INP_DECR_REF(inp);
7127 /* save away my tag to */
7128 stc.my_vtag = initackm_out->msg.init.initiate_tag;
7130 /* set up some of the credits. */
7131 so = inp->sctp_socket;
7133 /* memory problem */
7137 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
7139 /* set what I want */
7140 his_limit = ntohs(init_chk->init.num_inbound_streams);
7141 /* choose what I want */
7143 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
7144 i_want = asoc->streamoutcnt;
7146 i_want = inp->sctp_ep.pre_open_stream_count;
7149 i_want = inp->sctp_ep.pre_open_stream_count;
7151 if (his_limit < i_want) {
7152 /* I Want more :< */
7153 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
7155 /* I can have what I want :> */
7156 initackm_out->msg.init.num_outbound_streams = htons(i_want);
7158 /* tell him his limt. */
7159 initackm_out->msg.init.num_inbound_streams =
7160 htons(inp->sctp_ep.max_open_streams_intome);
7161 /* setup the ECN pointer */
7163 if (inp->sctp_ep.adaptation_layer_indicator) {
7164 struct sctp_adaptation_layer_indication *ali;
7166 ali = (struct sctp_adaptation_layer_indication *)(
7167 (caddr_t)initackm_out + sizeof(*initackm_out));
7168 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
7169 ali->ph.param_length = htons(sizeof(*ali));
7170 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
7171 SCTP_BUF_LEN(m) += sizeof(*ali);
7172 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
7175 ecn = (struct sctp_ecn_supported_param *)(
7176 (caddr_t)initackm_out + sizeof(*initackm_out));
7180 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
7181 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
7182 ecn->ph.param_length = htons(sizeof(*ecn));
7183 SCTP_BUF_LEN(m) += sizeof(*ecn);
7185 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
7188 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
7190 /* And now tell the peer we do pr-sctp */
7191 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
7192 prsctp->ph.param_length = htons(sizeof(*prsctp));
7193 SCTP_BUF_LEN(m) += sizeof(*prsctp);
7195 /* Add NAT friendly parameter */
7196 struct sctp_paramhdr *ph;
7198 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7199 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
7200 ph->param_length = htons(sizeof(struct sctp_paramhdr));
7201 SCTP_BUF_LEN(m) += sizeof(sizeof(struct sctp_paramhdr));
7203 /* And now tell the peer we do all the extensions */
7204 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7205 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
7207 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
7208 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
7209 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
7210 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
7211 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
7212 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
7213 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
7215 * EY if the sysctl variable is set, tell the assoc. initiator that
7218 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
7219 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
7220 p_len = sizeof(*pr_supported) + num_ext;
7221 pr_supported->ph.param_length = htons(p_len);
7222 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
7223 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7225 /* ECN nonce: And now tell the peer we support ECN nonce */
7226 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
7227 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
7228 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
7229 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
7230 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
7231 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
7233 /* add authentication parameters */
7234 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
7235 struct sctp_auth_random *randp;
7236 struct sctp_auth_hmac_algo *hmacs;
7237 struct sctp_auth_chunk_list *chunks;
7238 uint16_t random_len;
7240 /* generate and add RANDOM parameter */
7241 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
7242 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7243 randp->ph.param_type = htons(SCTP_RANDOM);
7244 p_len = sizeof(*randp) + random_len;
7245 randp->ph.param_length = htons(p_len);
7246 SCTP_READ_RANDOM(randp->random_data, random_len);
7247 /* zero out any padding required */
7248 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
7249 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7251 /* add HMAC_ALGO parameter */
7252 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7253 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
7254 (uint8_t *) hmacs->hmac_ids);
7256 p_len += sizeof(*hmacs);
7257 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
7258 hmacs->ph.param_length = htons(p_len);
7259 /* zero out any padding required */
7260 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
7261 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7263 /* add CHUNKS parameter */
7264 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7265 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
7266 chunks->chunk_types);
7268 p_len += sizeof(*chunks);
7269 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
7270 chunks->ph.param_length = htons(p_len);
7271 /* zero out any padding required */
7272 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
7273 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7277 /* now the addresses */
7279 struct sctp_scoping scp;
7282 * To optimize this we could put the scoping stuff into a
7283 * structure and remove the individual uint8's from the stc
7284 * structure. Then we could just sifa in the address within
7285 * the stc.. but for now this is a quick hack to get the
7286 * address stuff teased apart.
7288 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
7289 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
7290 scp.loopback_scope = stc.loopback_scope;
7291 scp.ipv4_local_scope = stc.ipv4_scope;
7292 scp.local_scope = stc.local_scope;
7293 scp.site_scope = stc.site_scope;
7294 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
7297 /* tack on the operational error if present */
7305 llen += SCTP_BUF_LEN(ol);
7306 ol = SCTP_BUF_NEXT(ol);
7309 /* must add a pad to the param */
7310 uint32_t cpthis = 0;
7313 padlen = 4 - (llen % 4);
7314 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
7316 while (SCTP_BUF_NEXT(m_at) != NULL) {
7317 m_at = SCTP_BUF_NEXT(m_at);
7319 SCTP_BUF_NEXT(m_at) = op_err;
7320 while (SCTP_BUF_NEXT(m_at) != NULL) {
7321 m_at = SCTP_BUF_NEXT(m_at);
7324 /* pre-calulate the size and update pkt header and chunk header */
7326 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7327 p_len += SCTP_BUF_LEN(m_tmp);
7328 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7329 /* m_tmp should now point to last one */
7334 /* Now we must build a cookie */
7335 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
7336 sizeof(struct sctphdr), &stc, &signature);
7337 if (m_cookie == NULL) {
7338 /* memory problem */
7342 /* Now append the cookie to the end and update the space/size */
7343 SCTP_BUF_NEXT(m_tmp) = m_cookie;
7345 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7346 p_len += SCTP_BUF_LEN(m_tmp);
7347 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7348 /* m_tmp should now point to last one */
7354 * Place in the size, but we don't include the last pad (if any) in
7357 initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
7360 * Time to sign the cookie, we don't sign over the cookie signature
7361 * though thus we set trailer.
7363 (void)sctp_hmac_m(SCTP_HMAC,
7364 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
7365 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
7366 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
7368 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
7369 * here since the timer will drive a retranmission.
7372 if ((padval) && (mp_last)) {
7373 /* see my previous comments on mp_last */
7376 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
7378 /* Houston we have a problem, no space */
7384 if (stc.loopback_scope) {
7385 over_addr = &store1;
7391 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
7392 0, NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
7393 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7398 sctp_insert_on_wheel(struct sctp_tcb *stcb,
7399 struct sctp_association *asoc,
7400 struct sctp_stream_out *strq, int holds_lock)
7402 struct sctp_stream_out *stre, *strn;
7404 if (holds_lock == 0) {
7405 SCTP_TCB_SEND_LOCK(stcb);
7407 if ((strq->next_spoke.tqe_next) ||
7408 (strq->next_spoke.tqe_prev)) {
7409 /* already on wheel */
7412 stre = TAILQ_FIRST(&asoc->out_wheel);
7414 /* only one on wheel */
7415 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
7418 for (; stre; stre = strn) {
7419 strn = TAILQ_NEXT(stre, next_spoke);
7420 if (stre->stream_no > strq->stream_no) {
7421 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
7423 } else if (stre->stream_no == strq->stream_no) {
7424 /* huh, should not happen */
7426 } else if (strn == NULL) {
7427 /* next one is null */
7428 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
7433 if (holds_lock == 0) {
7434 SCTP_TCB_SEND_UNLOCK(stcb);
7439 sctp_remove_from_wheel(struct sctp_tcb *stcb,
7440 struct sctp_association *asoc,
7441 struct sctp_stream_out *strq)
7443 /* take off and then setup so we know it is not on the wheel */
7444 SCTP_TCB_SEND_LOCK(stcb);
7445 if (TAILQ_FIRST(&strq->outqueue)) {
7446 /* more was added */
7447 SCTP_TCB_SEND_UNLOCK(stcb);
7450 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
7451 strq->next_spoke.tqe_next = NULL;
7452 strq->next_spoke.tqe_prev = NULL;
7453 SCTP_TCB_SEND_UNLOCK(stcb);
7457 sctp_prune_prsctp(struct sctp_tcb *stcb,
7458 struct sctp_association *asoc,
7459 struct sctp_sndrcvinfo *srcv,
7463 struct sctp_tmit_chunk *chk, *nchk;
7465 SCTP_TCB_LOCK_ASSERT(stcb);
7466 if ((asoc->peer_supports_prsctp) &&
7467 (asoc->sent_queue_cnt_removeable > 0)) {
7468 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
7470 * Look for chunks marked with the PR_SCTP flag AND
7471 * the buffer space flag. If the one being sent is
7472 * equal or greater priority then purge the old one
7473 * and free some space.
7475 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
7477 * This one is PR-SCTP AND buffer space
7480 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7482 * Lower numbers equates to higher
7483 * priority so if the one we are
7484 * looking at has a larger or equal
7485 * priority we want to drop the data
7486 * and NOT retransmit it.
7490 * We release the book_size
7491 * if the mbuf is here
7496 if (chk->sent > SCTP_DATAGRAM_UNSENT)
7497 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
7499 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
7500 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7502 &asoc->sent_queue, SCTP_SO_LOCKED);
7503 freed_spc += ret_spc;
7504 if (freed_spc >= dataout) {
7507 } /* if chunk was present */
7508 } /* if of sufficent priority */
7509 } /* if chunk has enabled */
7510 } /* tailqforeach */
7512 chk = TAILQ_FIRST(&asoc->send_queue);
7514 nchk = TAILQ_NEXT(chk, sctp_next);
7515 /* Here we must move to the sent queue and mark */
7516 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
7517 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7520 * We release the book_size
7521 * if the mbuf is here
7525 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7526 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
7527 &asoc->send_queue, SCTP_SO_LOCKED);
7529 freed_spc += ret_spc;
7530 if (freed_spc >= dataout) {
7533 } /* end if chk->data */
7534 } /* end if right class */
7535 } /* end if chk pr-sctp */
7537 } /* end while (chk) */
7538 } /* if enabled in asoc */
7542 sctp_get_frag_point(struct sctp_tcb *stcb,
7543 struct sctp_association *asoc)
7548 * For endpoints that have both v6 and v4 addresses we must reserve
7549 * room for the ipv6 header, for those that are only dealing with V4
7550 * we use a larger frag point.
7552 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7553 ovh = SCTP_MED_OVERHEAD;
7555 ovh = SCTP_MED_V4_OVERHEAD;
7558 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
7559 siz = asoc->smallest_mtu - ovh;
7561 siz = (stcb->asoc.sctp_frag_point - ovh);
7563 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
7565 /* A data chunk MUST fit in a cluster */
7566 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
7569 /* adjust for an AUTH chunk if DATA requires auth */
7570 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
7571 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7574 /* make it an even word boundary please */
7581 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
7582 struct sctp_stream_queue_pending *sp)
7585 if (stcb->asoc.peer_supports_prsctp) {
7587 * We assume that the user wants PR_SCTP_TTL if the user
7588 * provides a positive lifetime but does not specify any
7589 * PR_SCTP policy. This is a BAD assumption and causes
7590 * problems at least with the U-Vancovers MPI folks. I will
7591 * change this to be no policy means NO PR-SCTP.
7593 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
7594 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
7599 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
7600 case CHUNK_FLAGS_PR_SCTP_BUF:
7602 * Time to live is a priority stored in tv_sec when
7603 * doing the buffer drop thing.
7605 sp->ts.tv_sec = sp->timetolive;
7608 case CHUNK_FLAGS_PR_SCTP_TTL:
7612 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7613 tv.tv_sec = sp->timetolive / 1000;
7614 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
7616 * TODO sctp_constants.h needs alternative
7617 * time macros when _KERNEL is undefined.
7619 timevaladd(&sp->ts, &tv);
7622 case CHUNK_FLAGS_PR_SCTP_RTX:
7624 * Time to live is a the number or retransmissions
7627 sp->ts.tv_sec = sp->timetolive;
7631 SCTPDBG(SCTP_DEBUG_USRREQ1,
7632 "Unknown PR_SCTP policy %u.\n",
7633 PR_SCTP_POLICY(sp->sinfo_flags));
7640 sctp_msg_append(struct sctp_tcb *stcb,
7641 struct sctp_nets *net,
7643 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
7645 int error = 0, holds_lock;
7647 struct sctp_stream_queue_pending *sp = NULL;
7648 struct sctp_stream_out *strm;
7651 * Given an mbuf chain, put it into the association send queue and
7652 * place it on the wheel
7654 holds_lock = hold_stcb_lock;
7655 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
7656 /* Invalid stream number */
7657 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7661 if ((stcb->asoc.stream_locked) &&
7662 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
7663 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7667 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
7668 /* Now can we send this? */
7669 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7670 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7671 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7672 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
7673 /* got data while shutting down */
7674 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
7678 sctp_alloc_a_strmoq(stcb, sp);
7680 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7684 sp->sinfo_flags = srcv->sinfo_flags;
7685 sp->timetolive = srcv->sinfo_timetolive;
7686 sp->ppid = srcv->sinfo_ppid;
7687 sp->context = srcv->sinfo_context;
7689 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
7693 sp->net = stcb->asoc.primary_destination;
7696 atomic_add_int(&sp->net->ref_count, 1);
7697 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7698 sp->stream = srcv->sinfo_stream;
7699 sp->msg_is_complete = 1;
7700 sp->sender_all_done = 1;
7703 sp->tail_mbuf = NULL;
7706 sctp_set_prsctp_policy(stcb, sp);
7708 * We could in theory (for sendall) sifa the length in, but we would
7709 * still have to hunt through the chain since we need to setup the
7713 if (SCTP_BUF_NEXT(at) == NULL)
7715 sp->length += SCTP_BUF_LEN(at);
7716 at = SCTP_BUF_NEXT(at);
7718 SCTP_TCB_SEND_LOCK(stcb);
7719 sctp_snd_sb_alloc(stcb, sp->length);
7720 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
7721 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
7722 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
7723 sp->strseq = strm->next_sequence_sent;
7724 strm->next_sequence_sent++;
7726 if ((strm->next_spoke.tqe_next == NULL) &&
7727 (strm->next_spoke.tqe_prev == NULL)) {
7728 /* Not on wheel, insert */
7729 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
7732 SCTP_TCB_SEND_UNLOCK(stcb);
7741 static struct mbuf *
7742 sctp_copy_mbufchain(struct mbuf *clonechain,
7743 struct mbuf *outchain,
7744 struct mbuf **endofchain,
7747 uint8_t copy_by_ref)
7750 struct mbuf *appendchain;
7754 if (endofchain == NULL) {
7758 sctp_m_freem(outchain);
7761 if (can_take_mbuf) {
7762 appendchain = clonechain;
7765 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
7767 /* Its not in a cluster */
7768 if (*endofchain == NULL) {
7769 /* lets get a mbuf cluster */
7770 if (outchain == NULL) {
7771 /* This is the general case */
7773 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7774 if (outchain == NULL) {
7777 SCTP_BUF_LEN(outchain) = 0;
7778 *endofchain = outchain;
7779 /* get the prepend space */
7780 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
7783 * We really should not get a NULL
7789 if (SCTP_BUF_NEXT(m) == NULL) {
7793 m = SCTP_BUF_NEXT(m);
7796 if (*endofchain == NULL) {
7798 * huh, TSNH XXX maybe we
7801 sctp_m_freem(outchain);
7805 /* get the new end of length */
7806 len = M_TRAILINGSPACE(*endofchain);
7808 /* how much is left at the end? */
7809 len = M_TRAILINGSPACE(*endofchain);
7811 /* Find the end of the data, for appending */
7812 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
7814 /* Now lets copy it out */
7815 if (len >= sizeofcpy) {
7816 /* It all fits, copy it in */
7817 m_copydata(clonechain, 0, sizeofcpy, cp);
7818 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7820 /* fill up the end of the chain */
7822 m_copydata(clonechain, 0, len, cp);
7823 SCTP_BUF_LEN((*endofchain)) += len;
7824 /* now we need another one */
7827 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7832 SCTP_BUF_NEXT((*endofchain)) = m;
7834 cp = mtod((*endofchain), caddr_t);
7835 m_copydata(clonechain, len, sizeofcpy, cp);
7836 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7840 /* copy the old fashion way */
7841 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
7842 #ifdef SCTP_MBUF_LOGGING
7843 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7848 if (SCTP_BUF_IS_EXTENDED(mat)) {
7849 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7851 mat = SCTP_BUF_NEXT(mat);
7857 if (appendchain == NULL) {
7860 sctp_m_freem(outchain);
7864 /* tack on to the end */
7865 if (*endofchain != NULL) {
7866 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
7870 if (SCTP_BUF_NEXT(m) == NULL) {
7871 SCTP_BUF_NEXT(m) = appendchain;
7874 m = SCTP_BUF_NEXT(m);
7878 * save off the end and update the end-chain postion
7882 if (SCTP_BUF_NEXT(m) == NULL) {
7886 m = SCTP_BUF_NEXT(m);
7890 /* save off the end and update the end-chain postion */
7893 if (SCTP_BUF_NEXT(m) == NULL) {
7897 m = SCTP_BUF_NEXT(m);
7899 return (appendchain);
7904 sctp_med_chunk_output(struct sctp_inpcb *inp,
7905 struct sctp_tcb *stcb,
7906 struct sctp_association *asoc,
7909 int control_only, int *cwnd_full, int from_where,
7910 struct timeval *now, int *now_filled, int frag_point, int so_locked
7911 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7917 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
7920 struct sctp_copy_all *ca;
7923 int added_control = 0;
7924 int un_sent, do_chunk_output = 1;
7925 struct sctp_association *asoc;
7927 ca = (struct sctp_copy_all *)ptr;
7928 if (ca->m == NULL) {
7931 if (ca->inp != inp) {
7935 if ((ca->m) && ca->sndlen) {
7936 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
7938 /* can't copy so we are done */
7942 #ifdef SCTP_MBUF_LOGGING
7943 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7948 if (SCTP_BUF_IS_EXTENDED(mat)) {
7949 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7951 mat = SCTP_BUF_NEXT(mat);
7958 SCTP_TCB_LOCK_ASSERT(stcb);
7959 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
7960 /* Abort this assoc with m as the user defined reason */
7962 struct sctp_paramhdr *ph;
7964 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
7966 ph = mtod(m, struct sctp_paramhdr *);
7967 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7968 ph->param_length = htons(ca->sndlen);
7971 * We add one here to keep the assoc from
7972 * dis-appearing on us.
7974 atomic_add_int(&stcb->asoc.refcnt, 1);
7975 sctp_abort_an_association(inp, stcb,
7976 SCTP_RESPONSE_TO_USER_REQ,
7977 m, SCTP_SO_NOT_LOCKED);
7979 * sctp_abort_an_association calls sctp_free_asoc()
7980 * free association will NOT free it since we
7981 * incremented the refcnt .. we do this to prevent
7982 * it being freed and things getting tricky since we
7983 * could end up (from free_asoc) calling inpcb_free
7984 * which would get a recursive lock call to the
7985 * iterator lock.. But as a consequence of that the
7986 * stcb will return to us un-locked.. since
7987 * free_asoc returns with either no TCB or the TCB
7988 * unlocked, we must relock.. to unlock in the
7989 * iterator timer :-0
7991 SCTP_TCB_LOCK(stcb);
7992 atomic_add_int(&stcb->asoc.refcnt, -1);
7993 goto no_chunk_output;
7997 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
8001 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
8002 /* shutdown this assoc */
8005 cnt = sctp_is_there_unsent_data(stcb);
8007 if (TAILQ_EMPTY(&asoc->send_queue) &&
8008 TAILQ_EMPTY(&asoc->sent_queue) &&
8010 if (asoc->locked_on_sending) {
8014 * there is nothing queued to send, so I'm
8017 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8018 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8019 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8021 * only send SHUTDOWN the first time
8024 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
8025 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
8026 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8028 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8029 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8030 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
8031 asoc->primary_destination);
8032 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8033 asoc->primary_destination);
8035 do_chunk_output = 0;
8039 * we still got (or just got) data to send,
8040 * so set SHUTDOWN_PENDING
8043 * XXX sockets draft says that SCTP_EOF
8044 * should be sent with no data. currently,
8045 * we will allow user data to be sent first
8046 * and move to SHUTDOWN-PENDING
8048 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8049 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8050 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8051 if (asoc->locked_on_sending) {
8053 * Locked to send out the
8056 struct sctp_stream_queue_pending *sp;
8058 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
8060 if ((sp->length == 0) && (sp->msg_is_complete == 0))
8061 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8064 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
8065 if (TAILQ_EMPTY(&asoc->send_queue) &&
8066 TAILQ_EMPTY(&asoc->sent_queue) &&
8067 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
8069 atomic_add_int(&stcb->asoc.refcnt, 1);
8070 sctp_abort_an_association(stcb->sctp_ep, stcb,
8071 SCTP_RESPONSE_TO_USER_REQ,
8072 NULL, SCTP_SO_NOT_LOCKED);
8073 atomic_add_int(&stcb->asoc.refcnt, -1);
8074 goto no_chunk_output;
8076 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8077 asoc->primary_destination);
8083 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8084 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
8086 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
8087 (stcb->asoc.total_flight > 0) &&
8088 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
8090 do_chunk_output = 0;
8092 if (do_chunk_output)
8093 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
8094 else if (added_control) {
8095 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
8099 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8100 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
8101 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
8112 sctp_sendall_completes(void *ptr, uint32_t val)
8114 struct sctp_copy_all *ca;
8116 ca = (struct sctp_copy_all *)ptr;
8118 * Do a notify here? Kacheong suggests that the notify be done at
8119 * the send time.. so you would push up a notification if any send
8120 * failed. Don't know if this is feasable since the only failures we
8121 * have is "memory" related and if you cannot get an mbuf to send
8122 * the data you surely can't get an mbuf to send up to notify the
8123 * user you can't send the data :->
8126 /* now free everything */
8127 sctp_m_freem(ca->m);
8128 SCTP_FREE(ca, SCTP_M_COPYAL);
8132 #define MC_ALIGN(m, len) do { \
8133 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
8138 static struct mbuf *
8139 sctp_copy_out_all(struct uio *uio, int len)
8141 struct mbuf *ret, *at;
8142 int left, willcpy, cancpy, error;
8144 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
8150 SCTP_BUF_LEN(ret) = 0;
8151 /* save space for the data chunk header */
8152 cancpy = M_TRAILINGSPACE(ret);
8153 willcpy = min(cancpy, left);
8156 /* Align data to the end */
8157 error = uiomove(mtod(at, caddr_t), willcpy, uio);
8163 SCTP_BUF_LEN(at) = willcpy;
8164 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
8167 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
8168 if (SCTP_BUF_NEXT(at) == NULL) {
8171 at = SCTP_BUF_NEXT(at);
8172 SCTP_BUF_LEN(at) = 0;
8173 cancpy = M_TRAILINGSPACE(at);
8174 willcpy = min(cancpy, left);
8181 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
8182 struct sctp_sndrcvinfo *srcv)
8185 struct sctp_copy_all *ca;
8187 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
8191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8194 memset(ca, 0, sizeof(struct sctp_copy_all));
8197 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
8199 * take off the sendall flag, it would be bad if we failed to do
8202 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
8203 /* get length and mbuf chain */
8205 ca->sndlen = uio->uio_resid;
8206 ca->m = sctp_copy_out_all(uio, ca->sndlen);
8207 if (ca->m == NULL) {
8208 SCTP_FREE(ca, SCTP_M_COPYAL);
8209 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8213 /* Gather the length of the send */
8219 ca->sndlen += SCTP_BUF_LEN(m);
8220 m = SCTP_BUF_NEXT(m);
8224 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
8225 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
8226 SCTP_ASOC_ANY_STATE,
8228 sctp_sendall_completes, inp, 1);
8230 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
8231 SCTP_FREE(ca, SCTP_M_COPYAL);
8232 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
8240 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
8242 struct sctp_tmit_chunk *chk, *nchk;
8244 chk = TAILQ_FIRST(&asoc->control_send_queue);
8246 nchk = TAILQ_NEXT(chk, sctp_next);
8247 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8248 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8250 sctp_m_freem(chk->data);
8253 asoc->ctrl_queue_cnt--;
8254 sctp_free_a_chunk(stcb, chk);
8261 sctp_toss_old_asconf(struct sctp_tcb *stcb)
8263 struct sctp_association *asoc;
8264 struct sctp_tmit_chunk *chk, *chk_tmp;
8265 struct sctp_asconf_chunk *acp;
8268 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
8271 chk_tmp = TAILQ_NEXT(chk, sctp_next);
8272 /* find SCTP_ASCONF chunk in queue */
8273 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8275 acp = mtod(chk->data, struct sctp_asconf_chunk *);
8276 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
8281 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
8283 sctp_m_freem(chk->data);
8286 asoc->ctrl_queue_cnt--;
8287 sctp_free_a_chunk(stcb, chk);
8294 sctp_clean_up_datalist(struct sctp_tcb *stcb,
8296 struct sctp_association *asoc,
8297 struct sctp_tmit_chunk **data_list,
8299 struct sctp_nets *net)
8302 struct sctp_tmit_chunk *tp1;
8304 for (i = 0; i < bundle_at; i++) {
8305 /* off of the send queue */
8308 * Any chunk NOT 0 you zap the time chunk 0 gets
8309 * zapped or set based on if a RTO measurment is
8312 data_list[i]->do_rtt = 0;
8315 data_list[i]->sent_rcv_time = net->last_sent_time;
8316 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
8317 TAILQ_REMOVE(&asoc->send_queue,
8320 /* on to the sent queue */
8321 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
8322 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
8323 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
8324 struct sctp_tmit_chunk *tpp;
8326 /* need to move back */
8328 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
8330 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
8334 if (compare_with_wrap(tp1->rec.data.TSN_seq,
8335 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
8338 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
8340 TAILQ_INSERT_TAIL(&asoc->sent_queue,
8345 /* This does not lower until the cum-ack passes it */
8346 asoc->sent_queue_cnt++;
8347 asoc->send_queue_cnt--;
8348 if ((asoc->peers_rwnd <= 0) &&
8349 (asoc->total_flight == 0) &&
8351 /* Mark the chunk as being a window probe */
8352 SCTP_STAT_INCR(sctps_windowprobed);
8354 #ifdef SCTP_AUDITING_ENABLED
8355 sctp_audit_log(0xC2, 3);
8357 data_list[i]->sent = SCTP_DATAGRAM_SENT;
8358 data_list[i]->snd_count = 1;
8359 data_list[i]->rec.data.chunk_was_revoked = 0;
8360 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
8361 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
8362 data_list[i]->whoTo->flight_size,
8363 data_list[i]->book_size,
8364 (uintptr_t) data_list[i]->whoTo,
8365 data_list[i]->rec.data.TSN_seq);
8367 sctp_flight_size_increase(data_list[i]);
8368 sctp_total_flight_increase(stcb, data_list[i]);
8369 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8370 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8371 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
8373 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8374 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
8375 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8376 /* SWS sender side engages */
8377 asoc->peers_rwnd = 0;
8383 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
8385 struct sctp_tmit_chunk *chk, *nchk;
8387 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
8389 nchk = TAILQ_NEXT(chk, sctp_next);
8390 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8391 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
8392 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8393 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8394 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8395 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8396 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8397 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8398 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8399 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8400 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8401 /* Stray chunks must be cleaned up */
8403 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8405 sctp_m_freem(chk->data);
8408 asoc->ctrl_queue_cnt--;
8409 sctp_free_a_chunk(stcb, chk);
8410 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8411 /* special handling, we must look into the param */
8412 if (chk != asoc->str_reset) {
8413 goto clean_up_anyway;
8421 sctp_can_we_split_this(struct sctp_tcb *stcb,
8423 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
8426 * Make a decision on if I should split a msg into multiple parts.
8427 * This is only asked of incomplete messages.
8431 * If we are doing EEOR we need to always send it if its the
8432 * entire thing, since it might be all the guy is putting in
8435 if (goal_mtu >= length) {
8437 * If we have data outstanding,
8438 * we get another chance when the sack
8439 * arrives to transmit - wait for more data
8441 if (stcb->asoc.total_flight == 0) {
8443 * If nothing is in flight, we zero the
8451 /* You can fill the rest */
8456 * For those strange folk that make the send buffer
8457 * smaller than our fragmentation point, we can't
8458 * get a full msg in so we have to allow splitting.
8460 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
8463 if ((length <= goal_mtu) ||
8464 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
8465 /* Sub-optimial residual don't split in non-eeor mode. */
8469 * If we reach here length is larger than the goal_mtu. Do we wish
8470 * to split it for the sake of packet putting together?
8472 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
8473 /* Its ok to split it */
8474 return (min(goal_mtu, frag_point));
8476 /* Nope, can't split */
8482 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
8483 struct sctp_stream_out *strq,
8485 uint32_t frag_point,
8491 /* Move from the stream to the send_queue keeping track of the total */
8492 struct sctp_association *asoc;
8493 struct sctp_stream_queue_pending *sp;
8494 struct sctp_tmit_chunk *chk;
8495 struct sctp_data_chunk *dchkh;
8496 uint32_t to_move, length;
8497 uint8_t rcv_flags = 0;
8499 uint8_t send_lock_up = 0;
8501 SCTP_TCB_LOCK_ASSERT(stcb);
8504 /* sa_ignore FREED_MEMORY */
8505 sp = TAILQ_FIRST(&strq->outqueue);
8508 if (send_lock_up == 0) {
8509 SCTP_TCB_SEND_LOCK(stcb);
8512 sp = TAILQ_FIRST(&strq->outqueue);
8516 if (strq->last_msg_incomplete) {
8517 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
8519 strq->last_msg_incomplete);
8520 strq->last_msg_incomplete = 0;
8524 SCTP_TCB_SEND_UNLOCK(stcb);
8529 if ((sp->msg_is_complete) && (sp->length == 0)) {
8530 if (sp->sender_all_done) {
8532 * We are doing differed cleanup. Last time through
8533 * when we took all the data the sender_all_done was
8536 if (sp->put_last_out == 0) {
8537 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
8538 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8539 sp->sender_all_done,
8541 sp->msg_is_complete,
8545 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
8546 SCTP_TCB_SEND_LOCK(stcb);
8549 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8550 TAILQ_REMOVE(&strq->outqueue, sp, next);
8551 sctp_free_remote_addr(sp->net);
8553 sctp_m_freem(sp->data);
8556 sctp_free_a_strmoq(stcb, sp);
8558 /* we can't be locked to it */
8560 stcb->asoc.locked_on_sending = NULL;
8562 SCTP_TCB_SEND_UNLOCK(stcb);
8565 /* back to get the next msg */
8569 * sender just finished this but still holds a
8578 /* is there some to get */
8579 if (sp->length == 0) {
8587 some_taken = sp->some_taken;
8588 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
8589 sp->msg_is_complete = 1;
8592 length = sp->length;
8593 if (sp->msg_is_complete) {
8594 /* The message is complete */
8595 to_move = min(length, frag_point);
8596 if (to_move == length) {
8597 /* All of it fits in the MTU */
8598 if (sp->some_taken) {
8599 rcv_flags |= SCTP_DATA_LAST_FRAG;
8600 sp->put_last_out = 1;
8602 rcv_flags |= SCTP_DATA_NOT_FRAG;
8603 sp->put_last_out = 1;
8606 /* Not all of it fits, we fragment */
8607 if (sp->some_taken == 0) {
8608 rcv_flags |= SCTP_DATA_FIRST_FRAG;
8613 to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
8614 frag_point, eeor_mode);
8617 * We use a snapshot of length in case it
8618 * is expanding during the compare.
8623 if (to_move >= llen) {
8625 if (send_lock_up == 0) {
8627 * We are taking all of an incomplete msg
8628 * thus we need a send lock.
8630 SCTP_TCB_SEND_LOCK(stcb);
8632 if (sp->msg_is_complete) {
8634 * the sender finished the
8641 if (sp->some_taken == 0) {
8642 rcv_flags |= SCTP_DATA_FIRST_FRAG;
8646 /* Nothing to take. */
8647 if (sp->some_taken) {
8656 /* If we reach here, we can copy out a chunk */
8657 sctp_alloc_a_chunk(stcb, chk);
8659 /* No chunk memory */
8665 * Setup for unordered if needed by looking at the user sent info
8668 if (sp->sinfo_flags & SCTP_UNORDERED) {
8669 rcv_flags |= SCTP_DATA_UNORDERED;
8671 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
8672 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
8673 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8675 /* clear out the chunk before setting up */
8676 memset(chk, 0, sizeof(*chk));
8677 chk->rec.data.rcv_flags = rcv_flags;
8679 if (to_move >= length) {
8680 /* we think we can steal the whole thing */
8681 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
8682 SCTP_TCB_SEND_LOCK(stcb);
8685 if (to_move < sp->length) {
8686 /* bail, it changed */
8689 chk->data = sp->data;
8690 chk->last_mbuf = sp->tail_mbuf;
8691 /* register the stealing */
8692 sp->data = sp->tail_mbuf = NULL;
8697 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
8698 chk->last_mbuf = NULL;
8699 if (chk->data == NULL) {
8700 sp->some_taken = some_taken;
8701 sctp_free_a_chunk(stcb, chk);
8706 #ifdef SCTP_MBUF_LOGGING
8707 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8712 if (SCTP_BUF_IS_EXTENDED(mat)) {
8713 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8715 mat = SCTP_BUF_NEXT(mat);
8719 /* Pull off the data */
8720 m_adj(sp->data, to_move);
8721 /* Now lets work our way down and compact it */
8723 while (m && (SCTP_BUF_LEN(m) == 0)) {
8724 sp->data = SCTP_BUF_NEXT(m);
8725 SCTP_BUF_NEXT(m) = NULL;
8726 if (sp->tail_mbuf == m) {
8728 * Freeing tail? TSNH since
8729 * we supposedly were taking less
8730 * than the sp->length.
8733 panic("Huh, freing tail? - TSNH");
8735 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
8736 sp->tail_mbuf = sp->data = NULL;
8745 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
8746 chk->copy_by_ref = 1;
8748 chk->copy_by_ref = 0;
8751 * get last_mbuf and counts of mb useage This is ugly but hopefully
8752 * its only one mbuf.
8754 if (chk->last_mbuf == NULL) {
8755 chk->last_mbuf = chk->data;
8756 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
8757 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
8760 if (to_move > length) {
8761 /*- This should not happen either
8762 * since we always lower to_move to the size
8763 * of sp->length if its larger.
8766 panic("Huh, how can to_move be larger?");
8768 SCTP_PRINTF("Huh, how can to_move be larger?\n");
8772 atomic_subtract_int(&sp->length, to_move);
8774 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
8775 /* Not enough room for a chunk header, get some */
8778 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
8781 * we're in trouble here. _PREPEND below will free
8782 * all the data if there is no leading space, so we
8783 * must put the data back and restore.
8785 if (send_lock_up == 0) {
8786 SCTP_TCB_SEND_LOCK(stcb);
8789 if (chk->data == NULL) {
8790 /* unsteal the data */
8791 sp->data = chk->data;
8792 sp->tail_mbuf = chk->last_mbuf;
8796 /* reassemble the data */
8798 sp->data = chk->data;
8799 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
8801 sp->some_taken = some_taken;
8802 atomic_add_int(&sp->length, to_move);
8805 sctp_free_a_chunk(stcb, chk);
8809 SCTP_BUF_LEN(m) = 0;
8810 SCTP_BUF_NEXT(m) = chk->data;
8812 M_ALIGN(chk->data, 4);
8815 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
8816 if (chk->data == NULL) {
8817 /* HELP, TSNH since we assured it would not above? */
8819 panic("prepend failes HELP?");
8821 SCTP_PRINTF("prepend fails HELP?\n");
8822 sctp_free_a_chunk(stcb, chk);
8828 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
8829 chk->book_size = chk->send_size = (to_move +
8830 sizeof(struct sctp_data_chunk));
8831 chk->book_size_scale = 0;
8832 chk->sent = SCTP_DATAGRAM_UNSENT;
8835 chk->asoc = &stcb->asoc;
8836 chk->pad_inplace = 0;
8837 chk->no_fr_allowed = 0;
8838 chk->rec.data.stream_seq = sp->strseq;
8839 chk->rec.data.stream_number = sp->stream;
8840 chk->rec.data.payloadtype = sp->ppid;
8841 chk->rec.data.context = sp->context;
8842 chk->rec.data.doing_fast_retransmit = 0;
8843 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
8845 chk->rec.data.timetodrop = sp->ts;
8846 chk->flags = sp->act_flags;
8847 chk->addr_over = sp->addr_over;
8850 atomic_add_int(&chk->whoTo->ref_count, 1);
8852 if (sp->holds_key_ref) {
8853 chk->auth_keyid = sp->auth_keyid;
8854 sctp_auth_key_acquire(stcb, chk->auth_keyid);
8855 chk->holds_key_ref = 1;
8857 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
8858 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
8859 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
8860 (uintptr_t) stcb, sp->length,
8861 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
8862 chk->rec.data.TSN_seq);
8864 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8866 * Put the rest of the things in place now. Size was done earlier in
8867 * previous loop prior to padding.
8870 #ifdef SCTP_ASOCLOG_OF_TSNS
8871 SCTP_TCB_LOCK_ASSERT(stcb);
8872 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
8873 asoc->tsn_out_at = 0;
8874 asoc->tsn_out_wrapped = 1;
8876 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
8877 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
8878 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
8879 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
8880 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
8881 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
8882 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
8883 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
8887 dchkh->ch.chunk_type = SCTP_DATA;
8888 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8889 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
8890 dchkh->dp.stream_id = htons(strq->stream_no);
8891 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
8892 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
8893 dchkh->ch.chunk_length = htons(chk->send_size);
8894 /* Now advance the chk->send_size by the actual pad needed. */
8895 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
8900 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
8901 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
8902 chk->pad_inplace = 1;
8904 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
8905 /* pad added an mbuf */
8906 chk->last_mbuf = lm;
8908 chk->send_size += pads;
8910 /* We only re-set the policy if it is on */
8911 if (sp->pr_sctp_on) {
8912 sctp_set_prsctp_policy(stcb, sp);
8913 asoc->pr_sctp_cnt++;
8914 chk->pr_sctp_on = 1;
8916 chk->pr_sctp_on = 0;
8918 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
8919 /* All done pull and kill the message */
8920 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8921 if (sp->put_last_out == 0) {
8922 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
8923 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8924 sp->sender_all_done,
8926 sp->msg_is_complete,
8930 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
8931 SCTP_TCB_SEND_LOCK(stcb);
8934 TAILQ_REMOVE(&strq->outqueue, sp, next);
8935 sctp_free_remote_addr(sp->net);
8937 sctp_m_freem(sp->data);
8940 sctp_free_a_strmoq(stcb, sp);
8942 /* we can't be locked to it */
8944 stcb->asoc.locked_on_sending = NULL;
8946 /* more to go, we are locked */
8949 asoc->chunks_on_out_queue++;
8950 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
8951 asoc->send_queue_cnt++;
8954 SCTP_TCB_SEND_UNLOCK(stcb);
8961 static struct sctp_stream_out *
8962 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
8964 struct sctp_stream_out *strq;
8966 /* Find the next stream to use */
8967 if (asoc->last_out_stream == NULL) {
8968 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8969 if (asoc->last_out_stream == NULL) {
8970 /* huh nothing on the wheel, TSNH */
8975 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
8978 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8980 /* Save off the last stream */
8981 asoc->last_out_stream = strq;
8988 sctp_fill_outqueue(struct sctp_tcb *stcb,
8989 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
8991 struct sctp_association *asoc;
8992 struct sctp_stream_out *strq, *strqn, *strqt;
8993 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
8995 struct sctp_stream_queue_pending *sp;
8997 SCTP_TCB_LOCK_ASSERT(stcb);
9000 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
9001 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9003 /* ?? not sure what else to do */
9004 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9007 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9009 /* Need an allowance for the data chunk header too */
9010 goal_mtu -= sizeof(struct sctp_data_chunk);
9012 /* must make even word boundary */
9013 goal_mtu &= 0xfffffffc;
9014 if (asoc->locked_on_sending) {
9015 /* We are stuck on one stream until the message completes. */
9016 strqn = strq = asoc->locked_on_sending;
9019 strqn = strq = sctp_select_a_stream(stcb, asoc);
9023 while ((goal_mtu > 0) && strq) {
9024 sp = TAILQ_FIRST(&strq->outqueue);
9026 * If CMT is off, we must validate that the stream in
9027 * question has the first item pointed towards are network
9028 * destionation requested by the caller. Note that if we
9029 * turn out to be locked to a stream (assigning TSN's then
9030 * we must stop, since we cannot look for another stream
9031 * with data to send to that destination). In CMT's case, by
9032 * skipping this check, we will send one data packet towards
9033 * the requested net.
9038 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
9039 /* none for this network */
9043 strq = sctp_select_a_stream(stcb, asoc);
9047 if (strqn == strq) {
9048 /* I have circled */
9056 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
9057 &giveup, eeor_mode, &bail);
9058 asoc->last_out_stream = strq;
9060 asoc->locked_on_sending = strq;
9061 if ((moved_how_much == 0) || (giveup) || bail)
9062 /* no more to move for now */
9065 asoc->locked_on_sending = NULL;
9066 strqt = sctp_select_a_stream(stcb, asoc);
9067 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
9068 if (strq == strqn) {
9069 /* Must move start to next one */
9070 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
9071 if (strqn == NULL) {
9072 strqn = TAILQ_FIRST(&asoc->out_wheel);
9073 if (strqn == NULL) {
9078 sctp_remove_from_wheel(stcb, asoc, strq);
9080 if ((giveup) || bail) {
9088 total_moved += moved_how_much;
9089 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
9090 goal_mtu &= 0xfffffffc;
9095 if (total_moved == 0) {
9096 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
9097 (net == stcb->asoc.primary_destination)) {
9098 /* ran dry for primary network net */
9099 SCTP_STAT_INCR(sctps_primary_randry);
9100 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9101 /* ran dry with CMT on */
9102 SCTP_STAT_INCR(sctps_cmt_randry);
9108 sctp_fix_ecn_echo(struct sctp_association *asoc)
9110 struct sctp_tmit_chunk *chk;
9112 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9113 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9114 chk->sent = SCTP_DATAGRAM_UNSENT;
9120 sctp_move_to_an_alt(struct sctp_tcb *stcb,
9121 struct sctp_association *asoc,
9122 struct sctp_nets *net)
9124 struct sctp_tmit_chunk *chk;
9125 struct sctp_nets *a_net;
9127 SCTP_TCB_LOCK_ASSERT(stcb);
9129 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
9130 * destination using the PF algorithm for finding alternate
9133 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
9134 a_net = sctp_find_alternate_net(stcb, net, 2);
9136 a_net = sctp_find_alternate_net(stcb, net, 0);
9138 if ((a_net != net) &&
9139 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
9141 * We only proceed if a valid alternate is found that is not
9142 * this one and is reachable. Here we must move all chunks
9143 * queued in the send queue off of the destination address
9146 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
9147 if (chk->whoTo == net) {
9148 /* Move the chunk to our alternate */
9149 sctp_free_remote_addr(chk->whoTo);
9151 atomic_add_int(&a_net->ref_count, 1);
9158 sctp_med_chunk_output(struct sctp_inpcb *inp,
9159 struct sctp_tcb *stcb,
9160 struct sctp_association *asoc,
9163 int control_only, int *cwnd_full, int from_where,
9164 struct timeval *now, int *now_filled, int frag_point, int so_locked
9165 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9171 * Ok this is the generic chunk service queue. we must do the
9172 * following: - Service the stream queue that is next, moving any
9173 * message (note I must get a complete message i.e. FIRST/MIDDLE and
9174 * LAST to the out queue in one pass) and assigning TSN's - Check to
9175 * see if the cwnd/rwnd allows any output, if so we go ahead and
9176 * fomulate and send the low level chunks. Making sure to combine
9177 * any control in the control chunk queue also.
9179 struct sctp_nets *net;
9180 struct mbuf *outchain, *endoutchain;
9181 struct sctp_tmit_chunk *chk, *nchk;
9182 struct sctphdr *shdr;
9184 /* temp arrays for unlinking */
9185 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9186 int no_fragmentflg, error;
9187 int one_chunk, hbflag, skip_data_for_this_net;
9188 int asconf, cookie, no_out_cnt;
9189 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
9190 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
9191 struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
9193 uint32_t auth_offset = 0;
9194 struct sctp_auth_chunk *auth = NULL;
9195 uint16_t auth_keyid = 0;
9196 int data_auth_reqd = 0;
9199 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
9208 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
9209 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
9210 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
9215 ctl_cnt = no_out_cnt = asconf = cookie = 0;
9217 * First lets prime the pump. For each destination, if there is room
9218 * in the flight size, attempt to pull an MTU's worth out of the
9219 * stream queues into the general send_queue
9221 #ifdef SCTP_AUDITING_ENABLED
9222 sctp_audit_log(0xC2, 2);
9224 SCTP_TCB_LOCK_ASSERT(stcb);
9226 if ((control_only) || (asoc->stream_reset_outstanding))
9231 /* Nothing to possible to send? */
9232 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9233 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
9234 TAILQ_EMPTY(&asoc->send_queue) &&
9235 TAILQ_EMPTY(&asoc->out_wheel)) {
9239 if (asoc->peers_rwnd == 0) {
9240 /* No room in peers rwnd */
9243 if (asoc->total_flight > 0) {
9244 /* we are allowed one chunk in flight */
9248 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
9249 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9251 * for CMT we start at the next one past the one we
9252 * last added data to.
9254 if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
9255 goto skip_the_fill_from_streams;
9257 if (asoc->last_net_data_came_from) {
9258 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
9260 net = TAILQ_FIRST(&asoc->nets);
9264 net = TAILQ_FIRST(&asoc->nets);
9268 * JRI-TODO: CMT-MPI. Simply set the first
9269 * destination (net) to be optimized for the next
9270 * message to be pulled out of the outwheel. 1. peek
9271 * at outwheel 2. If large message, set net =
9272 * highest_cwnd 3. If small message, set net =
9276 net = asoc->primary_destination;
9279 net = TAILQ_FIRST(&asoc->nets);
9285 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9286 net->window_probe = 0;
9287 if (old_startat && (old_startat == net)) {
9291 * JRI: if dest is unreachable or unconfirmed, do
9292 * not send data to it
9294 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
9298 * JRI: if dest is in PF state, do not send data to
9301 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9302 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9303 (net->dest_state & SCTP_ADDR_PF)) {
9306 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
9307 /* nothing can be in queue for this guy */
9310 if (net->flight_size >= net->cwnd) {
9311 /* skip this network, no room */
9316 * JRI : this for loop we are in takes in each net,
9317 * if its's got space in cwnd and has data sent to
9318 * it (when CMT is off) then it calls
9319 * sctp_fill_outqueue for the net. This gets data on
9320 * the send queue for that network.
9322 * In sctp_fill_outqueue TSN's are assigned and data is
9323 * copied out of the stream buffers. Note mostly
9324 * copy by reference (we hope).
9326 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9327 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
9329 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
9331 /* memory alloc failure */
9333 goto skip_the_fill_from_streams;
9336 if (start_at != TAILQ_FIRST(&asoc->nets)) {
9337 /* got to pick up the beginning stuff. */
9338 old_startat = start_at;
9339 start_at = net = TAILQ_FIRST(&asoc->nets);
9344 skip_the_fill_from_streams:
9345 *cwnd_full = cwnd_full_ind;
9347 /* now service each destination and send out what we can for it */
9348 /* Nothing to send? */
9349 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
9350 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
9351 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
9355 if (no_data_chunks) {
9356 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9358 chk = TAILQ_FIRST(&asoc->control_send_queue);
9360 chk = TAILQ_FIRST(&asoc->send_queue);
9363 send_start_at = chk->whoTo;
9365 send_start_at = TAILQ_FIRST(&asoc->nets);
9368 again_one_more_time:
9369 for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9370 /* how much can we send? */
9371 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
9372 if (old_startat && (old_startat == net)) {
9373 /* through list ocmpletely. */
9377 if (net->ref_count < 2) {
9379 * Ref-count of 1 so we cannot have data or control
9380 * queued to this address. Skip it.
9384 ctl_cnt = bundle_at = 0;
9385 endoutchain = outchain = NULL;
9388 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9389 skip_data_for_this_net = 1;
9391 skip_data_for_this_net = 0;
9393 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
9395 * if we have a route and an ifp check to see if we
9396 * have room to send to this guy
9400 ifp = net->ro.ro_rt->rt_ifp;
9401 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
9402 SCTP_STAT_INCR(sctps_ifnomemqueued);
9403 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9404 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
9409 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9411 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9415 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9425 if (mtu > asoc->peers_rwnd) {
9426 if (asoc->total_flight > 0) {
9427 /* We have a packet in flight somewhere */
9428 r_mtu = asoc->peers_rwnd;
9430 /* We are always allowed to send one MTU out */
9437 /************************/
9438 /* ASCONF transmission */
9439 /************************/
9440 /* Now first lets go through the asconf queue */
9441 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9443 nchk = TAILQ_NEXT(chk, sctp_next);
9444 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
9447 if (chk->whoTo != net) {
9449 * No, not sent to the network we are
9454 if (chk->data == NULL) {
9457 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
9458 chk->sent != SCTP_DATAGRAM_RESEND) {
9462 * if no AUTH is yet included and this chunk
9463 * requires it, make sure to account for it. We
9464 * don't apply the size until the AUTH chunk is
9465 * actually added below in case there is no room for
9466 * this chunk. NOTE: we overload the use of "omtu"
9469 if ((auth == NULL) &&
9470 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9471 stcb->asoc.peer_auth_chunks)) {
9472 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9475 /* Here we do NOT factor the r_mtu */
9476 if ((chk->send_size < (int)(mtu - omtu)) ||
9477 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9479 * We probably should glom the mbuf chain
9480 * from the chk->data for control but the
9481 * problem is it becomes yet one more level
9482 * of tracking to do if for some reason
9483 * output fails. Then I have got to
9484 * reconstruct the merged control chain.. el
9485 * yucko.. for now we take the easy way and
9489 * Add an AUTH chunk, if chunk requires it
9490 * save the offset into the chain for AUTH
9492 if ((auth == NULL) &&
9493 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9494 stcb->asoc.peer_auth_chunks))) {
9495 outchain = sctp_add_auth_chunk(outchain,
9500 chk->rec.chunk_id.id);
9501 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9503 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9504 (int)chk->rec.chunk_id.can_take_data,
9505 chk->send_size, chk->copy_by_ref);
9506 if (outchain == NULL) {
9508 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9511 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9512 /* update our MTU size */
9513 if (mtu > (chk->send_size + omtu))
9514 mtu -= (chk->send_size + omtu);
9517 to_out += (chk->send_size + omtu);
9518 /* Do clear IP_DF ? */
9519 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9522 if (chk->rec.chunk_id.can_take_data)
9525 * set hb flag since we can use these for
9531 * should sysctl this: don't bundle data
9532 * with ASCONF since it requires AUTH
9535 chk->sent = SCTP_DATAGRAM_SENT;
9539 * Ok we are out of room but we can
9540 * output without effecting the
9541 * flight size since this little guy
9542 * is a control only packet.
9544 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9546 * do NOT clear the asconf flag as
9547 * it is used to do appropriate
9548 * source address selection.
9550 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
9551 if (outchain == NULL) {
9553 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9558 shdr = mtod(outchain, struct sctphdr *);
9559 shdr->src_port = inp->sctp_lport;
9560 shdr->dest_port = stcb->rport;
9561 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9563 auth_offset += sizeof(struct sctphdr);
9564 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9565 (struct sockaddr *)&net->ro._l_addr,
9566 outchain, auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9567 no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
9568 if (error == ENOBUFS) {
9569 asoc->ifp_had_enobuf = 1;
9570 SCTP_STAT_INCR(sctps_lowlevelerr);
9572 if (from_where == 0) {
9573 SCTP_STAT_INCR(sctps_lowlevelerrusr);
9575 if (*now_filled == 0) {
9576 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9578 *now = net->last_sent_time;
9580 net->last_sent_time = *now;
9583 /* error, could not output */
9584 if (error == EHOSTUNREACH) {
9590 sctp_move_to_an_alt(stcb, asoc, net);
9595 asoc->ifp_had_enobuf = 0;
9596 if (*now_filled == 0) {
9597 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9599 *now = net->last_sent_time;
9601 net->last_sent_time = *now;
9605 * increase the number we sent, if a
9606 * cookie is sent we don't tell them
9609 outchain = endoutchain = NULL;
9613 *num_out += ctl_cnt;
9614 /* recalc a clean slate and setup */
9615 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9616 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9618 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9625 /************************/
9626 /* Control transmission */
9627 /************************/
9628 /* Now first lets go through the control queue */
9629 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
9631 nchk = TAILQ_NEXT(chk, sctp_next);
9632 if (chk->whoTo != net) {
9634 * No, not sent to the network we are
9639 if (chk->data == NULL) {
9642 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
9644 * It must be unsent. Cookies and ASCONF's
9645 * hang around but there timers will force
9646 * when marked for resend.
9651 * if no AUTH is yet included and this chunk
9652 * requires it, make sure to account for it. We
9653 * don't apply the size until the AUTH chunk is
9654 * actually added below in case there is no room for
9655 * this chunk. NOTE: we overload the use of "omtu"
9658 if ((auth == NULL) &&
9659 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9660 stcb->asoc.peer_auth_chunks)) {
9661 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9664 /* Here we do NOT factor the r_mtu */
9665 if ((chk->send_size < (int)(mtu - omtu)) ||
9666 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9668 * We probably should glom the mbuf chain
9669 * from the chk->data for control but the
9670 * problem is it becomes yet one more level
9671 * of tracking to do if for some reason
9672 * output fails. Then I have got to
9673 * reconstruct the merged control chain.. el
9674 * yucko.. for now we take the easy way and
9678 * Add an AUTH chunk, if chunk requires it
9679 * save the offset into the chain for AUTH
9681 if ((auth == NULL) &&
9682 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9683 stcb->asoc.peer_auth_chunks))) {
9684 outchain = sctp_add_auth_chunk(outchain,
9689 chk->rec.chunk_id.id);
9690 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9692 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9693 (int)chk->rec.chunk_id.can_take_data,
9694 chk->send_size, chk->copy_by_ref);
9695 if (outchain == NULL) {
9697 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9700 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9701 /* update our MTU size */
9702 if (mtu > (chk->send_size + omtu))
9703 mtu -= (chk->send_size + omtu);
9706 to_out += (chk->send_size + omtu);
9707 /* Do clear IP_DF ? */
9708 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9711 if (chk->rec.chunk_id.can_take_data)
9713 /* Mark things to be removed, if needed */
9714 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
9715 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
9716 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
9717 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
9718 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
9719 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
9720 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
9721 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
9722 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
9723 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
9724 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
9726 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
9729 * JRS 5/14/07 - Set the
9730 * flag to say a heartbeat
9735 /* remove these chunks at the end */
9736 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9737 /* turn off the timer */
9738 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9739 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9740 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9744 * EY -Nr-sack version of the above
9747 if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
9748 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { /* EY !?! */
9749 /* turn off the timer */
9750 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9751 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9752 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9758 * Other chunks, since they have
9759 * timers running (i.e. COOKIE) we
9760 * just "trust" that it gets sent or
9764 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9768 chk->sent = SCTP_DATAGRAM_SENT;
9773 * Ok we are out of room but we can
9774 * output without effecting the
9775 * flight size since this little guy
9776 * is a control only packet.
9779 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9781 * do NOT clear the asconf
9782 * flag as it is used to do
9783 * appropriate source
9784 * address selection.
9788 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9791 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
9792 if (outchain == NULL) {
9794 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9796 goto error_out_again;
9798 shdr = mtod(outchain, struct sctphdr *);
9799 shdr->src_port = inp->sctp_lport;
9800 shdr->dest_port = stcb->rport;
9801 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9803 auth_offset += sizeof(struct sctphdr);
9804 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9805 (struct sockaddr *)&net->ro._l_addr,
9807 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9808 no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
9809 if (error == ENOBUFS) {
9810 asoc->ifp_had_enobuf = 1;
9811 SCTP_STAT_INCR(sctps_lowlevelerr);
9813 if (from_where == 0) {
9814 SCTP_STAT_INCR(sctps_lowlevelerrusr);
9817 /* error, could not output */
9819 if (*now_filled == 0) {
9820 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9822 *now = net->last_sent_time;
9824 net->last_sent_time = *now;
9828 if (error == EHOSTUNREACH) {
9834 sctp_move_to_an_alt(stcb, asoc, net);
9839 asoc->ifp_had_enobuf = 0;
9840 /* Only HB or ASCONF advances time */
9842 if (*now_filled == 0) {
9843 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9845 *now = net->last_sent_time;
9847 net->last_sent_time = *now;
9852 * increase the number we sent, if a
9853 * cookie is sent we don't tell them
9856 outchain = endoutchain = NULL;
9860 *num_out += ctl_cnt;
9861 /* recalc a clean slate and setup */
9862 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9863 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9865 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9872 /*********************/
9873 /* Data transmission */
9874 /*********************/
9876 * if AUTH for DATA is required and no AUTH has been added
9877 * yet, account for this in the mtu now... if no data can be
9878 * bundled, this adjustment won't matter anyways since the
9879 * packet will be going out...
9881 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
9882 stcb->asoc.peer_auth_chunks);
9883 if (data_auth_reqd && (auth == NULL)) {
9884 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9886 /* now lets add any data within the MTU constraints */
9887 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9889 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
9890 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9896 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
9897 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9907 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
9909 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
9910 if (no_data_chunks) {
9911 /* let only control go out */
9915 if (net->flight_size >= net->cwnd) {
9916 /* skip this net, no room for data */
9920 nchk = TAILQ_NEXT(chk, sctp_next);
9921 if (chk->whoTo != net) {
9922 /* No, not sent to this net */
9925 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
9927 * strange, we have a chunk that is
9928 * to big for its destination and
9929 * yet no fragment ok flag.
9930 * Something went wrong when the
9931 * PMTU changed...we did not mark
9932 * this chunk for some reason?? I
9933 * will fix it here by letting IP
9934 * fragment it for now and printing
9935 * a warning. This really should not
9938 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
9939 chk->send_size, mtu);
9940 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
9942 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
9943 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
9944 /* ok we will add this one */
9947 * Add an AUTH chunk, if chunk
9948 * requires it, save the offset into
9949 * the chain for AUTH
9951 if (data_auth_reqd) {
9953 outchain = sctp_add_auth_chunk(outchain,
9959 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9960 auth_keyid = chk->auth_keyid;
9961 } else if (auth_keyid != chk->auth_keyid) {
9969 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
9970 chk->send_size, chk->copy_by_ref);
9971 if (outchain == NULL) {
9972 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
9973 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9974 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9977 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9980 /* upate our MTU size */
9981 /* Do clear IP_DF ? */
9982 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9985 /* unsigned subtraction of mtu */
9986 if (mtu > chk->send_size)
9987 mtu -= chk->send_size;
9990 /* unsigned subtraction of r_mtu */
9991 if (r_mtu > chk->send_size)
9992 r_mtu -= chk->send_size;
9996 to_out += chk->send_size;
9997 if ((to_out > mx_mtu) && no_fragmentflg) {
9999 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
10001 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
10005 chk->window_probe = 0;
10006 data_list[bundle_at++] = chk;
10007 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
10011 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
10012 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
10013 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
10015 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
10017 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
10018 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
10028 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
10030 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
10032 data_list[0]->window_probe = 1;
10033 net->window_probe = 1;
10039 * Must be sent in order of the
10040 * TSN's (on a network)
10044 } /* for (chunk gather loop for this net) */
10045 } /* if asoc.state OPEN */
10046 /* Is there something to send for this destination? */
10048 /* We may need to start a control timer or two */
10050 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
10053 * do NOT clear the asconf flag as it is
10054 * used to do appropriate source address
10059 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
10062 /* must start a send timer if data is being sent */
10063 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10065 * no timer running on this destination
10068 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10069 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
10070 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
10072 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
10073 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10075 * JRS 5/14/07 - If a HB has been sent to a
10076 * PF destination and no T3 timer is
10077 * currently running, start the T3 timer to
10078 * track the HBs that were sent.
10080 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10082 /* Now send it, if there is anything to send :> */
10083 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
10084 if (outchain == NULL) {
10086 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
10090 shdr = mtod(outchain, struct sctphdr *);
10091 shdr->src_port = inp->sctp_lport;
10092 shdr->dest_port = stcb->rport;
10093 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10094 shdr->checksum = 0;
10095 auth_offset += sizeof(struct sctphdr);
10097 * if data auth isn't needed, use the assoc active
10100 if (!data_auth_reqd)
10101 auth_keyid = stcb->asoc.authinfo.active_keyid;
10102 if ((error = sctp_lowlevel_chunk_output(inp,
10105 (struct sockaddr *)&net->ro._l_addr,
10113 asconf, net->port, so_locked, NULL))) {
10114 /* error, we could not output */
10115 if (error == ENOBUFS) {
10116 SCTP_STAT_INCR(sctps_lowlevelerr);
10117 asoc->ifp_had_enobuf = 1;
10119 if (from_where == 0) {
10120 SCTP_STAT_INCR(sctps_lowlevelerrusr);
10123 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10125 if (*now_filled == 0) {
10126 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10128 *now = net->last_sent_time;
10130 net->last_sent_time = *now;
10134 if (error == EHOSTUNREACH) {
10136 * Destination went unreachable
10139 sctp_move_to_an_alt(stcb, asoc, net);
10143 * I add this line to be paranoid. As far as
10144 * I can tell the continue, takes us back to
10145 * the top of the for, but just to make sure
10146 * I will reset these again here.
10148 ctl_cnt = bundle_at = 0;
10149 continue; /* This takes us back to the
10150 * for() for the nets. */
10152 asoc->ifp_had_enobuf = 0;
10154 outchain = endoutchain = NULL;
10157 if (bundle_at || hbflag) {
10158 /* For data/asconf and hb set time */
10159 if (*now_filled == 0) {
10160 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10162 *now = net->last_sent_time;
10164 net->last_sent_time = *now;
10168 *num_out += (ctl_cnt + bundle_at);
10171 /* setup for a RTO measurement */
10172 tsns_sent = data_list[0]->rec.data.TSN_seq;
10173 /* fill time if not already filled */
10174 if (*now_filled == 0) {
10175 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
10177 *now = asoc->time_last_sent;
10179 asoc->time_last_sent = *now;
10181 data_list[0]->do_rtt = 1;
10182 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
10183 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
10184 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
10185 if (net->flight_size < net->cwnd) {
10186 /* start or restart it */
10187 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10188 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10189 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
10191 SCTP_STAT_INCR(sctps_earlyfrstrout);
10192 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
10194 /* stop it if its running */
10195 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10196 SCTP_STAT_INCR(sctps_earlyfrstpout);
10197 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10198 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10207 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10208 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
10211 if (old_startat == NULL) {
10212 old_startat = send_start_at;
10213 send_start_at = TAILQ_FIRST(&asoc->nets);
10215 goto again_one_more_time;
10218 * At the end there should be no NON timed chunks hanging on this
10221 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10222 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
10224 if ((*num_out == 0) && (*reason_code == 0)) {
10229 sctp_clean_up_ctl(stcb, asoc);
10234 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
10237 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
10238 * the control chunk queue.
10240 struct sctp_chunkhdr *hdr;
10241 struct sctp_tmit_chunk *chk;
10244 SCTP_TCB_LOCK_ASSERT(stcb);
10245 sctp_alloc_a_chunk(stcb, chk);
10248 sctp_m_freem(op_err);
10251 chk->copy_by_ref = 0;
10252 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
10253 if (op_err == NULL) {
10254 sctp_free_a_chunk(stcb, chk);
10257 chk->send_size = 0;
10259 while (mat != NULL) {
10260 chk->send_size += SCTP_BUF_LEN(mat);
10261 mat = SCTP_BUF_NEXT(mat);
10263 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
10264 chk->rec.chunk_id.can_take_data = 1;
10265 chk->sent = SCTP_DATAGRAM_UNSENT;
10266 chk->snd_count = 0;
10268 chk->asoc = &stcb->asoc;
10269 chk->data = op_err;
10270 chk->whoTo = chk->asoc->primary_destination;
10271 atomic_add_int(&chk->whoTo->ref_count, 1);
10272 hdr = mtod(op_err, struct sctp_chunkhdr *);
10273 hdr->chunk_type = SCTP_OPERATION_ERROR;
10274 hdr->chunk_flags = 0;
10275 hdr->chunk_length = htons(chk->send_size);
10276 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
10279 chk->asoc->ctrl_queue_cnt++;
10283 sctp_send_cookie_echo(struct mbuf *m,
10285 struct sctp_tcb *stcb,
10286 struct sctp_nets *net)
10289 * pull out the cookie and put it at the front of the control chunk
10293 struct mbuf *cookie;
10294 struct sctp_paramhdr parm, *phdr;
10295 struct sctp_chunkhdr *hdr;
10296 struct sctp_tmit_chunk *chk;
10297 uint16_t ptype, plen;
10299 /* First find the cookie in the param area */
10301 at = offset + sizeof(struct sctp_init_chunk);
10303 SCTP_TCB_LOCK_ASSERT(stcb);
10305 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
10306 if (phdr == NULL) {
10309 ptype = ntohs(phdr->param_type);
10310 plen = ntohs(phdr->param_length);
10311 if (ptype == SCTP_STATE_COOKIE) {
10314 /* found the cookie */
10315 if ((pad = (plen % 4))) {
10318 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
10319 if (cookie == NULL) {
10323 #ifdef SCTP_MBUF_LOGGING
10324 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10329 if (SCTP_BUF_IS_EXTENDED(mat)) {
10330 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10332 mat = SCTP_BUF_NEXT(mat);
10338 at += SCTP_SIZE32(plen);
10340 if (cookie == NULL) {
10341 /* Did not find the cookie */
10344 /* ok, we got the cookie lets change it into a cookie echo chunk */
10346 /* first the change from param to cookie */
10347 hdr = mtod(cookie, struct sctp_chunkhdr *);
10348 hdr->chunk_type = SCTP_COOKIE_ECHO;
10349 hdr->chunk_flags = 0;
10350 /* get the chunk stuff now and place it in the FRONT of the queue */
10351 sctp_alloc_a_chunk(stcb, chk);
10354 sctp_m_freem(cookie);
10357 chk->copy_by_ref = 0;
10358 chk->send_size = plen;
10359 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
10360 chk->rec.chunk_id.can_take_data = 0;
10361 chk->sent = SCTP_DATAGRAM_UNSENT;
10362 chk->snd_count = 0;
10363 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10364 chk->asoc = &stcb->asoc;
10365 chk->data = cookie;
10366 chk->whoTo = chk->asoc->primary_destination;
10367 atomic_add_int(&chk->whoTo->ref_count, 1);
10368 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
10369 chk->asoc->ctrl_queue_cnt++;
10374 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
10378 struct sctp_nets *net)
10381 * take a HB request and make it into a HB ack and send it.
10383 struct mbuf *outchain;
10384 struct sctp_chunkhdr *chdr;
10385 struct sctp_tmit_chunk *chk;
10389 /* must have a net pointer */
10392 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
10393 if (outchain == NULL) {
10394 /* gak out of memory */
10397 #ifdef SCTP_MBUF_LOGGING
10398 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10403 if (SCTP_BUF_IS_EXTENDED(mat)) {
10404 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10406 mat = SCTP_BUF_NEXT(mat);
10410 chdr = mtod(outchain, struct sctp_chunkhdr *);
10411 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
10412 chdr->chunk_flags = 0;
10413 if (chk_length % 4) {
10415 uint32_t cpthis = 0;
10418 padlen = 4 - (chk_length % 4);
10419 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
10421 sctp_alloc_a_chunk(stcb, chk);
10424 sctp_m_freem(outchain);
10427 chk->copy_by_ref = 0;
10428 chk->send_size = chk_length;
10429 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
10430 chk->rec.chunk_id.can_take_data = 1;
10431 chk->sent = SCTP_DATAGRAM_UNSENT;
10432 chk->snd_count = 0;
10434 chk->asoc = &stcb->asoc;
10435 chk->data = outchain;
10437 atomic_add_int(&chk->whoTo->ref_count, 1);
10438 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10439 chk->asoc->ctrl_queue_cnt++;
10443 sctp_send_cookie_ack(struct sctp_tcb *stcb)
10445 /* formulate and queue a cookie-ack back to sender */
10446 struct mbuf *cookie_ack;
10447 struct sctp_chunkhdr *hdr;
10448 struct sctp_tmit_chunk *chk;
10451 SCTP_TCB_LOCK_ASSERT(stcb);
10453 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10454 if (cookie_ack == NULL) {
10458 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
10459 sctp_alloc_a_chunk(stcb, chk);
10462 sctp_m_freem(cookie_ack);
10465 chk->copy_by_ref = 0;
10466 chk->send_size = sizeof(struct sctp_chunkhdr);
10467 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
10468 chk->rec.chunk_id.can_take_data = 1;
10469 chk->sent = SCTP_DATAGRAM_UNSENT;
10470 chk->snd_count = 0;
10472 chk->asoc = &stcb->asoc;
10473 chk->data = cookie_ack;
10474 if (chk->asoc->last_control_chunk_from != NULL) {
10475 chk->whoTo = chk->asoc->last_control_chunk_from;
10477 chk->whoTo = chk->asoc->primary_destination;
10479 atomic_add_int(&chk->whoTo->ref_count, 1);
10480 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
10481 hdr->chunk_type = SCTP_COOKIE_ACK;
10482 hdr->chunk_flags = 0;
10483 hdr->chunk_length = htons(chk->send_size);
10484 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
10485 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10486 chk->asoc->ctrl_queue_cnt++;
10492 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
10494 /* formulate and queue a SHUTDOWN-ACK back to the sender */
10495 struct mbuf *m_shutdown_ack;
10496 struct sctp_shutdown_ack_chunk *ack_cp;
10497 struct sctp_tmit_chunk *chk;
10499 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10500 if (m_shutdown_ack == NULL) {
10504 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
10505 sctp_alloc_a_chunk(stcb, chk);
10508 sctp_m_freem(m_shutdown_ack);
10511 chk->copy_by_ref = 0;
10512 chk->send_size = sizeof(struct sctp_chunkhdr);
10513 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
10514 chk->rec.chunk_id.can_take_data = 1;
10515 chk->sent = SCTP_DATAGRAM_UNSENT;
10516 chk->snd_count = 0;
10518 chk->asoc = &stcb->asoc;
10519 chk->data = m_shutdown_ack;
10521 atomic_add_int(&net->ref_count, 1);
10523 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
10524 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
10525 ack_cp->ch.chunk_flags = 0;
10526 ack_cp->ch.chunk_length = htons(chk->send_size);
10527 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
10528 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10529 chk->asoc->ctrl_queue_cnt++;
10534 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
10536 /* formulate and queue a SHUTDOWN to the sender */
10537 struct mbuf *m_shutdown;
10538 struct sctp_shutdown_chunk *shutdown_cp;
10539 struct sctp_tmit_chunk *chk;
10541 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10542 if (m_shutdown == NULL) {
10546 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
10547 sctp_alloc_a_chunk(stcb, chk);
10550 sctp_m_freem(m_shutdown);
10553 chk->copy_by_ref = 0;
10554 chk->send_size = sizeof(struct sctp_shutdown_chunk);
10555 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
10556 chk->rec.chunk_id.can_take_data = 1;
10557 chk->sent = SCTP_DATAGRAM_UNSENT;
10558 chk->snd_count = 0;
10560 chk->asoc = &stcb->asoc;
10561 chk->data = m_shutdown;
10563 atomic_add_int(&net->ref_count, 1);
10565 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
10566 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
10567 shutdown_cp->ch.chunk_flags = 0;
10568 shutdown_cp->ch.chunk_length = htons(chk->send_size);
10569 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
10570 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
10571 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10572 chk->asoc->ctrl_queue_cnt++;
10577 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
10580 * formulate and queue an ASCONF to the peer. ASCONF parameters
10581 * should be queued on the assoc queue.
10583 struct sctp_tmit_chunk *chk;
10584 struct mbuf *m_asconf;
10587 SCTP_TCB_LOCK_ASSERT(stcb);
10589 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
10590 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
10591 /* can't send a new one if there is one in flight already */
10594 /* compose an ASCONF chunk, maximum length is PMTU */
10595 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
10596 if (m_asconf == NULL) {
10599 sctp_alloc_a_chunk(stcb, chk);
10602 sctp_m_freem(m_asconf);
10605 chk->copy_by_ref = 0;
10606 chk->data = m_asconf;
10607 chk->send_size = len;
10608 chk->rec.chunk_id.id = SCTP_ASCONF;
10609 chk->rec.chunk_id.can_take_data = 0;
10610 chk->sent = SCTP_DATAGRAM_UNSENT;
10611 chk->snd_count = 0;
10612 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10613 chk->asoc = &stcb->asoc;
10615 atomic_add_int(&chk->whoTo->ref_count, 1);
10616 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
10617 chk->asoc->ctrl_queue_cnt++;
10622 sctp_send_asconf_ack(struct sctp_tcb *stcb)
10625 * formulate and queue a asconf-ack back to sender. the asconf-ack
10626 * must be stored in the tcb.
10628 struct sctp_tmit_chunk *chk;
10629 struct sctp_asconf_ack *ack, *latest_ack;
10630 struct mbuf *m_ack, *m;
10631 struct sctp_nets *net = NULL;
10633 SCTP_TCB_LOCK_ASSERT(stcb);
10634 /* Get the latest ASCONF-ACK */
10635 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
10636 if (latest_ack == NULL) {
10639 if (latest_ack->last_sent_to != NULL &&
10640 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
10641 /* we're doing a retransmission */
10642 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
10645 if (stcb->asoc.last_control_chunk_from == NULL)
10646 net = stcb->asoc.primary_destination;
10648 net = stcb->asoc.last_control_chunk_from;
10652 if (stcb->asoc.last_control_chunk_from == NULL)
10653 net = stcb->asoc.primary_destination;
10655 net = stcb->asoc.last_control_chunk_from;
10657 latest_ack->last_sent_to = net;
10659 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
10660 if (ack->data == NULL) {
10663 /* copy the asconf_ack */
10664 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
10665 if (m_ack == NULL) {
10666 /* couldn't copy it */
10669 #ifdef SCTP_MBUF_LOGGING
10670 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10675 if (SCTP_BUF_IS_EXTENDED(mat)) {
10676 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10678 mat = SCTP_BUF_NEXT(mat);
10683 sctp_alloc_a_chunk(stcb, chk);
10687 sctp_m_freem(m_ack);
10690 chk->copy_by_ref = 0;
10694 chk->send_size = 0;
10697 chk->send_size = ack->len;
10698 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
10699 chk->rec.chunk_id.can_take_data = 1;
10700 chk->sent = SCTP_DATAGRAM_UNSENT;
10701 chk->snd_count = 0;
10702 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
10703 chk->asoc = &stcb->asoc;
10704 atomic_add_int(&chk->whoTo->ref_count, 1);
10706 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10707 chk->asoc->ctrl_queue_cnt++;
10714 sctp_chunk_retransmission(struct sctp_inpcb *inp,
10715 struct sctp_tcb *stcb,
10716 struct sctp_association *asoc,
10717 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
10718 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10724 * send out one MTU of retransmission. If fast_retransmit is
10725 * happening we ignore the cwnd. Otherwise we obey the cwnd and
10726 * rwnd. For a Cookie or Asconf in the control chunk queue we
10727 * retransmit them by themselves.
10729 * For data chunks we will pick out the lowest TSN's in the sent_queue
10730 * marked for resend and bundle them all together (up to a MTU of
10731 * destination). The address to send to should have been
10732 * selected/changed where the retransmission was marked (i.e. in FR
10733 * or t3-timeout routines).
10735 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
10736 struct sctp_tmit_chunk *chk, *fwd;
10737 struct mbuf *m, *endofchain;
10738 struct sctphdr *shdr;
10739 struct sctp_nets *net = NULL;
10740 uint32_t tsns_sent = 0;
10741 int no_fragmentflg, bundle_at, cnt_thru;
10743 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
10744 struct sctp_auth_chunk *auth = NULL;
10745 uint32_t auth_offset = 0;
10746 uint16_t auth_keyid = 0;
10747 int data_auth_reqd = 0;
10750 SCTP_TCB_LOCK_ASSERT(stcb);
10751 tmr_started = ctl_cnt = bundle_at = error = 0;
10752 no_fragmentflg = 1;
10756 endofchain = m = NULL;
10757 #ifdef SCTP_AUDITING_ENABLED
10758 sctp_audit_log(0xC3, 1);
10760 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
10761 (TAILQ_EMPTY(&asoc->control_send_queue))) {
10762 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
10763 asoc->sent_queue_retran_cnt);
10764 asoc->sent_queue_cnt = 0;
10765 asoc->sent_queue_cnt_removeable = 0;
10766 /* send back 0/0 so we enter normal transmission */
10770 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10771 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
10772 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
10773 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
10774 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
10775 if (chk != asoc->str_reset) {
10777 * not eligible for retran if its
10784 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10789 * Add an AUTH chunk, if chunk requires it save the
10790 * offset into the chain for AUTH
10792 if ((auth == NULL) &&
10793 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
10794 stcb->asoc.peer_auth_chunks))) {
10795 m = sctp_add_auth_chunk(m, &endofchain,
10796 &auth, &auth_offset,
10798 chk->rec.chunk_id.id);
10799 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10801 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10807 /* do we have control chunks to retransmit? */
10809 /* Start a timer no matter if we suceed or fail */
10810 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
10811 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
10812 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
10813 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
10815 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
10817 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
10820 shdr = mtod(m, struct sctphdr *);
10821 shdr->src_port = inp->sctp_lport;
10822 shdr->dest_port = stcb->rport;
10823 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10824 shdr->checksum = 0;
10825 auth_offset += sizeof(struct sctphdr);
10826 chk->snd_count++; /* update our count */
10828 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
10829 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
10830 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
10831 no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
10832 SCTP_STAT_INCR(sctps_lowlevelerr);
10835 m = endofchain = NULL;
10839 * We don't want to mark the net->sent time here since this
10840 * we use this for HB and retrans cannot measure RTT
10842 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
10844 chk->sent = SCTP_DATAGRAM_SENT;
10845 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
10846 if (fwd_tsn == 0) {
10849 /* Clean up the fwd-tsn list */
10850 sctp_clean_up_ctl(stcb, asoc);
10855 * Ok, it is just data retransmission we need to do or that and a
10856 * fwd-tsn with it all.
10858 if (TAILQ_EMPTY(&asoc->sent_queue)) {
10859 return (SCTP_RETRAN_DONE);
10861 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
10862 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
10863 /* not yet open, resend the cookie and that is it */
10866 #ifdef SCTP_AUDITING_ENABLED
10867 sctp_auditing(20, inp, stcb, NULL);
10869 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
10870 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
10871 if (chk->sent != SCTP_DATAGRAM_RESEND) {
10872 /* No, not sent to this net or not ready for rtx */
10875 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
10876 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
10877 /* Gak, we have exceeded max unlucky retran, abort! */
10878 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
10880 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
10881 atomic_add_int(&stcb->asoc.refcnt, 1);
10882 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
10883 SCTP_TCB_LOCK(stcb);
10884 atomic_subtract_int(&stcb->asoc.refcnt, 1);
10885 return (SCTP_RETRAN_EXIT);
10887 /* pick up the net */
10889 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10890 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
10892 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
10895 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
10896 /* No room in peers rwnd */
10899 tsn = asoc->last_acked_seq + 1;
10900 if (tsn == chk->rec.data.TSN_seq) {
10902 * we make a special exception for this
10903 * case. The peer has no rwnd but is missing
10904 * the lowest chunk.. which is probably what
10905 * is holding up the rwnd.
10907 goto one_chunk_around;
10912 if (asoc->peers_rwnd < mtu) {
10914 if ((asoc->peers_rwnd == 0) &&
10915 (asoc->total_flight == 0)) {
10916 chk->window_probe = 1;
10917 chk->whoTo->window_probe = 1;
10920 #ifdef SCTP_AUDITING_ENABLED
10921 sctp_audit_log(0xC3, 2);
10925 net->fast_retran_ip = 0;
10926 if (chk->rec.data.doing_fast_retransmit == 0) {
10928 * if no FR in progress skip destination that have
10929 * flight_size > cwnd.
10931 if (net->flight_size >= net->cwnd) {
10936 * Mark the destination net to have FR recovery
10937 * limits put on it.
10940 net->fast_retran_ip = 1;
10944 * if no AUTH is yet included and this chunk requires it,
10945 * make sure to account for it. We don't apply the size
10946 * until the AUTH chunk is actually added below in case
10947 * there is no room for this chunk.
10949 if (data_auth_reqd && (auth == NULL)) {
10950 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10954 if ((chk->send_size <= (mtu - dmtu)) ||
10955 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
10956 /* ok we will add this one */
10957 if (data_auth_reqd) {
10958 if (auth == NULL) {
10959 m = sctp_add_auth_chunk(m,
10965 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10966 auth_keyid = chk->auth_keyid;
10967 } else if (chk->auth_keyid != auth_keyid) {
10968 /* different keyid, so done bundling */
10972 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10974 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10977 /* Do clear IP_DF ? */
10978 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10979 no_fragmentflg = 0;
10981 /* upate our MTU size */
10982 if (mtu > (chk->send_size + dmtu))
10983 mtu -= (chk->send_size + dmtu);
10986 data_list[bundle_at++] = chk;
10987 if (one_chunk && (asoc->total_flight <= 0)) {
10988 SCTP_STAT_INCR(sctps_windowprobed);
10991 if (one_chunk == 0) {
10993 * now are there anymore forward from chk to pick
10996 fwd = TAILQ_NEXT(chk, sctp_next);
10998 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
10999 /* Nope, not for retran */
11000 fwd = TAILQ_NEXT(fwd, sctp_next);
11003 if (fwd->whoTo != net) {
11004 /* Nope, not the net in question */
11005 fwd = TAILQ_NEXT(fwd, sctp_next);
11008 if (data_auth_reqd && (auth == NULL)) {
11009 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
11012 if (fwd->send_size <= (mtu - dmtu)) {
11013 if (data_auth_reqd) {
11014 if (auth == NULL) {
11015 m = sctp_add_auth_chunk(m,
11021 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11022 auth_keyid = fwd->auth_keyid;
11023 } else if (fwd->auth_keyid != auth_keyid) {
11031 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
11033 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11036 /* Do clear IP_DF ? */
11037 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
11038 no_fragmentflg = 0;
11040 /* upate our MTU size */
11041 if (mtu > (fwd->send_size + dmtu))
11042 mtu -= (fwd->send_size + dmtu);
11045 data_list[bundle_at++] = fwd;
11046 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
11049 fwd = TAILQ_NEXT(fwd, sctp_next);
11051 /* can't fit so we are done */
11056 /* Is there something to send for this destination? */
11059 * No matter if we fail/or suceed we should start a
11060 * timer. A failure is like a lost IP packet :-)
11062 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11064 * no timer running on this destination
11067 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11070 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
11072 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
11075 shdr = mtod(m, struct sctphdr *);
11076 shdr->src_port = inp->sctp_lport;
11077 shdr->dest_port = stcb->rport;
11078 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
11079 shdr->checksum = 0;
11080 auth_offset += sizeof(struct sctphdr);
11082 * if doing DATA auth, use the data chunk(s) key id,
11083 * otherwise use the assoc's active key id
11085 if (!data_auth_reqd)
11086 auth_keyid = stcb->asoc.authinfo.active_keyid;
11087 /* Now lets send it, if there is anything to send :> */
11088 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
11089 (struct sockaddr *)&net->ro._l_addr, m,
11090 auth_offset, auth, auth_keyid,
11091 no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
11092 /* error, we could not output */
11093 SCTP_STAT_INCR(sctps_lowlevelerr);
11096 m = endofchain = NULL;
11101 * We don't want to mark the net->sent time here
11102 * since this we use this for HB and retrans cannot
11105 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
11107 /* For auto-close */
11109 if (*now_filled == 0) {
11110 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
11111 *now = asoc->time_last_sent;
11114 asoc->time_last_sent = *now;
11116 *cnt_out += bundle_at;
11117 #ifdef SCTP_AUDITING_ENABLED
11118 sctp_audit_log(0xC4, bundle_at);
11121 tsns_sent = data_list[0]->rec.data.TSN_seq;
11123 for (i = 0; i < bundle_at; i++) {
11124 SCTP_STAT_INCR(sctps_sendretransdata);
11125 data_list[i]->sent = SCTP_DATAGRAM_SENT;
11127 * When we have a revoked data, and we
11128 * retransmit it, then we clear the revoked
11129 * flag since this flag dictates if we
11130 * subtracted from the fs
11132 if (data_list[i]->rec.data.chunk_was_revoked) {
11133 /* Deflate the cwnd */
11134 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
11135 data_list[i]->rec.data.chunk_was_revoked = 0;
11137 data_list[i]->snd_count++;
11138 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
11139 /* record the time */
11140 data_list[i]->sent_rcv_time = asoc->time_last_sent;
11141 if (data_list[i]->book_size_scale) {
11143 * need to double the book size on
11146 data_list[i]->book_size_scale = 0;
11148 * Since we double the booksize, we
11149 * must also double the output queue
11150 * size, since this get shrunk when
11151 * we free by this amount.
11153 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
11154 data_list[i]->book_size *= 2;
11158 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
11159 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
11160 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
11162 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
11163 (uint32_t) (data_list[i]->send_size +
11164 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
11166 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
11167 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
11168 data_list[i]->whoTo->flight_size,
11169 data_list[i]->book_size,
11170 (uintptr_t) data_list[i]->whoTo,
11171 data_list[i]->rec.data.TSN_seq);
11173 sctp_flight_size_increase(data_list[i]);
11174 sctp_total_flight_increase(stcb, data_list[i]);
11175 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
11176 /* SWS sender side engages */
11177 asoc->peers_rwnd = 0;
11180 (data_list[i]->rec.data.doing_fast_retransmit)) {
11181 SCTP_STAT_INCR(sctps_sendfastretrans);
11182 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
11183 (tmr_started == 0)) {
11185 * ok we just fast-retrans'd
11186 * the lowest TSN, i.e the
11187 * first on the list. In
11188 * this case we want to give
11189 * some more time to get a
11190 * SACK back without a
11193 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
11194 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
11195 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11199 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11200 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
11202 #ifdef SCTP_AUDITING_ENABLED
11203 sctp_auditing(21, inp, stcb, NULL);
11206 /* None will fit */
11209 if (asoc->sent_queue_retran_cnt <= 0) {
11210 /* all done we have no more to retran */
11211 asoc->sent_queue_retran_cnt = 0;
11215 /* No more room in rwnd */
11218 /* stop the for loop here. we sent out a packet */
11226 sctp_timer_validation(struct sctp_inpcb *inp,
11227 struct sctp_tcb *stcb,
11228 struct sctp_association *asoc,
11231 struct sctp_nets *net;
11233 /* Validate that a timer is running somewhere */
11234 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11235 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11236 /* Here is a timer */
11240 SCTP_TCB_LOCK_ASSERT(stcb);
11241 /* Gak, we did not have a timer somewhere */
11242 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
11243 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
11248 sctp_chunk_output(struct sctp_inpcb *inp,
11249 struct sctp_tcb *stcb,
11252 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11258 * Ok this is the generic chunk service queue. we must do the
11260 * - See if there are retransmits pending, if so we must
11262 * - Service the stream queue that is next, moving any
11263 * message (note I must get a complete message i.e.
11264 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
11266 * - Check to see if the cwnd/rwnd allows any output, if so we
11267 * go ahead and fomulate and send the low level chunks. Making sure
11268 * to combine any control in the control chunk queue also.
11270 struct sctp_association *asoc;
11271 struct sctp_nets *net;
11272 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
11273 burst_cnt = 0, burst_limit = 0;
11274 struct timeval now;
11275 int now_filled = 0;
11278 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
11280 int fr_done, tot_frs = 0;
11282 asoc = &stcb->asoc;
11283 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
11284 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
11290 SCTP_TCB_LOCK_ASSERT(stcb);
11292 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
11294 if ((un_sent <= 0) &&
11295 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
11296 (asoc->sent_queue_retran_cnt == 0)) {
11297 /* Nothing to do unless there is something to be sent left */
11301 * Do we have something to send, data or control AND a sack timer
11302 * running, if so piggy-back the sack.
11304 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
11306 * EY if nr_sacks used then send an nr-sack , a sack
11309 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
11310 sctp_send_nr_sack(stcb);
11312 sctp_send_sack(stcb);
11313 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
11315 while (asoc->sent_queue_retran_cnt) {
11317 * Ok, it is retransmission time only, we send out only ONE
11318 * packet with a single call off to the retran code.
11320 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
11322 * Special hook for handling cookiess discarded
11323 * by peer that carried data. Send cookie-ack only
11324 * and then the next call with get the retran's.
11326 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11327 &cwnd_full, from_where,
11328 &now, &now_filled, frag_point, so_locked);
11330 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
11331 /* if its not from a HB then do it */
11333 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
11339 * its from any other place, we don't allow retran
11340 * output (only control)
11345 /* Can't send anymore */
11347 * now lets push out control by calling med-level
11348 * output once. this assures that we WILL send HB's
11351 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11352 &cwnd_full, from_where,
11353 &now, &now_filled, frag_point, so_locked);
11354 #ifdef SCTP_AUDITING_ENABLED
11355 sctp_auditing(8, inp, stcb, NULL);
11357 (void)sctp_timer_validation(inp, stcb, asoc, ret);
11362 * The count was off.. retran is not happening so do
11363 * the normal retransmission.
11365 #ifdef SCTP_AUDITING_ENABLED
11366 sctp_auditing(9, inp, stcb, NULL);
11368 if (ret == SCTP_RETRAN_EXIT) {
11373 if (from_where == SCTP_OUTPUT_FROM_T3) {
11374 /* Only one transmission allowed out of a timeout */
11375 #ifdef SCTP_AUDITING_ENABLED
11376 sctp_auditing(10, inp, stcb, NULL);
11378 /* Push out any control */
11379 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
11380 &now, &now_filled, frag_point, so_locked);
11383 if (tot_frs > asoc->max_burst) {
11384 /* Hit FR burst limit */
11387 if ((num_out == 0) && (ret == 0)) {
11389 /* No more retrans to send */
11393 #ifdef SCTP_AUDITING_ENABLED
11394 sctp_auditing(12, inp, stcb, NULL);
11396 /* Check for bad destinations, if they exist move chunks around. */
11397 burst_limit = asoc->max_burst;
11398 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11399 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
11400 SCTP_ADDR_NOT_REACHABLE) {
11402 * if possible move things off of this address we
11403 * still may send below due to the dormant state but
11404 * we try to find an alternate address to send to
11405 * and if we have one we move all queued data on the
11406 * out wheel to this alternate address.
11408 if (net->ref_count > 1)
11409 sctp_move_to_an_alt(stcb, asoc, net);
11410 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
11411 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
11412 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
11414 * JRS 5/14/07 - If CMT PF is on and the current
11415 * destination is in PF state, move all queued data
11416 * to an alternate desination.
11418 if (net->ref_count > 1)
11419 sctp_move_to_an_alt(stcb, asoc, net);
11422 * if ((asoc->sat_network) || (net->addr_is_local))
11423 * { burst_limit = asoc->max_burst *
11424 * SCTP_SAT_NETWORK_BURST_INCR; }
11426 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
11427 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
11429 * JRS - Use the congestion control
11430 * given in the congestion control
11433 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
11434 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11435 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
11437 SCTP_STAT_INCR(sctps_maxburstqueued);
11439 net->fast_retran_ip = 0;
11441 if (net->flight_size == 0) {
11442 /* Should be decaying the cwnd here */
11452 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
11453 &reason_code, 0, &cwnd_full, from_where,
11454 &now, &now_filled, frag_point, so_locked);
11456 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
11457 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11458 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
11460 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11461 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
11462 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
11466 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
11468 tot_out += num_out;
11470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11471 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
11472 if (num_out == 0) {
11473 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
11478 * When nagle is on, we look at how much is un_sent, then
11479 * if its smaller than an MTU and we have data in
11482 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11483 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
11484 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
11485 (stcb->asoc.total_flight > 0)) {
11489 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
11490 TAILQ_EMPTY(&asoc->send_queue) &&
11491 TAILQ_EMPTY(&asoc->out_wheel)) {
11492 /* Nothing left to send */
11495 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
11496 /* Nothing left to send */
11499 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
11500 (burst_cnt < burst_limit)));
11502 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
11503 if (burst_cnt >= burst_limit) {
11504 SCTP_STAT_INCR(sctps_maxburstqueued);
11505 asoc->burst_limit_applied = 1;
11506 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11507 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
11510 asoc->burst_limit_applied = 0;
11513 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11514 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
11516 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
11520 * Now we need to clean up the control chunk chain if a ECNE is on
11521 * it. It must be marked as UNSENT again so next call will continue
11522 * to send it until such time that we get a CWR, to remove it.
11524 if (stcb->asoc.ecn_echo_cnt_onq)
11525 sctp_fix_ecn_echo(asoc);
11531 sctp_output(inp, m, addr, control, p, flags)
11532 struct sctp_inpcb *inp;
11534 struct sockaddr *addr;
11535 struct mbuf *control;
11540 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11543 if (inp->sctp_socket == NULL) {
11544 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11547 return (sctp_sosend(inp->sctp_socket,
11549 (struct uio *)NULL,
11557 send_forward_tsn(struct sctp_tcb *stcb,
11558 struct sctp_association *asoc)
11560 struct sctp_tmit_chunk *chk;
11561 struct sctp_forward_tsn_chunk *fwdtsn;
11563 SCTP_TCB_LOCK_ASSERT(stcb);
11564 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11565 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
11566 /* mark it to unsent */
11567 chk->sent = SCTP_DATAGRAM_UNSENT;
11568 chk->snd_count = 0;
11569 /* Do we correct its output location? */
11570 if (chk->whoTo != asoc->primary_destination) {
11571 sctp_free_remote_addr(chk->whoTo);
11572 chk->whoTo = asoc->primary_destination;
11573 atomic_add_int(&chk->whoTo->ref_count, 1);
11575 goto sctp_fill_in_rest;
11578 /* Ok if we reach here we must build one */
11579 sctp_alloc_a_chunk(stcb, chk);
11583 chk->copy_by_ref = 0;
11584 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
11585 chk->rec.chunk_id.can_take_data = 0;
11589 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11590 if (chk->data == NULL) {
11591 sctp_free_a_chunk(stcb, chk);
11594 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11595 chk->sent = SCTP_DATAGRAM_UNSENT;
11596 chk->snd_count = 0;
11597 chk->whoTo = asoc->primary_destination;
11598 atomic_add_int(&chk->whoTo->ref_count, 1);
11599 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11600 asoc->ctrl_queue_cnt++;
11603 * Here we go through and fill out the part that deals with
11604 * stream/seq of the ones we skip.
11606 SCTP_BUF_LEN(chk->data) = 0;
11608 struct sctp_tmit_chunk *at, *tp1, *last;
11609 struct sctp_strseq *strseq;
11610 unsigned int cnt_of_space, i, ovh;
11611 unsigned int space_needed;
11612 unsigned int cnt_of_skipped = 0;
11614 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11615 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
11616 /* no more to look at */
11619 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11620 /* We don't report these */
11625 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11626 (cnt_of_skipped * sizeof(struct sctp_strseq)));
11628 cnt_of_space = M_TRAILINGSPACE(chk->data);
11630 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
11631 ovh = SCTP_MIN_OVERHEAD;
11633 ovh = SCTP_MIN_V4_OVERHEAD;
11635 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
11636 /* trim to a mtu size */
11637 cnt_of_space = asoc->smallest_mtu - ovh;
11639 if (cnt_of_space < space_needed) {
11641 * ok we must trim down the chunk by lowering the
11642 * advance peer ack point.
11644 cnt_of_skipped = (cnt_of_space -
11645 ((sizeof(struct sctp_forward_tsn_chunk)) /
11646 sizeof(struct sctp_strseq)));
11648 * Go through and find the TSN that will be the one
11651 at = TAILQ_FIRST(&asoc->sent_queue);
11652 for (i = 0; i < cnt_of_skipped; i++) {
11653 tp1 = TAILQ_NEXT(at, sctp_next);
11658 * last now points to last one I can report, update
11661 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
11662 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
11664 chk->send_size = space_needed;
11665 /* Setup the chunk */
11666 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
11667 fwdtsn->ch.chunk_length = htons(chk->send_size);
11668 fwdtsn->ch.chunk_flags = 0;
11669 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
11670 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
11671 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
11672 (cnt_of_skipped * sizeof(struct sctp_strseq)));
11673 SCTP_BUF_LEN(chk->data) = chk->send_size;
11676 * Move pointer to after the fwdtsn and transfer to the
11679 strseq = (struct sctp_strseq *)fwdtsn;
11681 * Now populate the strseq list. This is done blindly
11682 * without pulling out duplicate stream info. This is
11683 * inefficent but won't harm the process since the peer will
11684 * look at these in sequence and will thus release anything.
11685 * It could mean we exceed the PMTU and chop off some that
11686 * we could have included.. but this is unlikely (aka 1432/4
11687 * would mean 300+ stream seq's would have to be reported in
11688 * one FWD-TSN. With a bit of work we can later FIX this to
11689 * optimize and pull out duplcates.. but it does add more
11690 * overhead. So for now... not!
11692 at = TAILQ_FIRST(&asoc->sent_queue);
11693 for (i = 0; i < cnt_of_skipped; i++) {
11694 tp1 = TAILQ_NEXT(at, sctp_next);
11695 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11696 /* We don't report these */
11701 strseq->stream = ntohs(at->rec.data.stream_number);
11702 strseq->sequence = ntohs(at->rec.data.stream_seq);
11712 sctp_send_sack(struct sctp_tcb *stcb)
11715 * Queue up a SACK in the control queue. We must first check to see
11716 * if a SACK is somehow on the control queue. If so, we will take
11717 * and and remove the old one.
11719 struct sctp_association *asoc;
11720 struct sctp_tmit_chunk *chk, *a_chk;
11721 struct sctp_sack_chunk *sack;
11722 struct sctp_gap_ack_block *gap_descriptor;
11723 struct sack_track *selector;
11728 int limit_reached = 0;
11729 unsigned int i, jstart, siz, j;
11730 unsigned int num_gap_blocks = 0, space;
11735 asoc = &stcb->asoc;
11736 SCTP_TCB_LOCK_ASSERT(stcb);
11737 if (asoc->last_data_chunk_from == NULL) {
11738 /* Hmm we never received anything */
11741 sctp_set_rwnd(stcb, asoc);
11742 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11743 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
11744 /* Hmm, found a sack already on queue, remove it */
11745 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
11746 asoc->ctrl_queue_cnt++;
11749 sctp_m_freem(a_chk->data);
11750 a_chk->data = NULL;
11752 sctp_free_remote_addr(a_chk->whoTo);
11753 a_chk->whoTo = NULL;
11757 if (a_chk == NULL) {
11758 sctp_alloc_a_chunk(stcb, a_chk);
11759 if (a_chk == NULL) {
11760 /* No memory so we drop the idea, and set a timer */
11761 if (stcb->asoc.delayed_ack) {
11762 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11763 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
11764 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11765 stcb->sctp_ep, stcb, NULL);
11767 stcb->asoc.send_sack = 1;
11771 a_chk->copy_by_ref = 0;
11772 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
11773 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
11774 a_chk->rec.chunk_id.can_take_data = 1;
11776 /* Clear our pkt counts */
11777 asoc->data_pkts_seen = 0;
11779 a_chk->asoc = asoc;
11780 a_chk->snd_count = 0;
11781 a_chk->send_size = 0; /* fill in later */
11782 a_chk->sent = SCTP_DATAGRAM_UNSENT;
11783 a_chk->whoTo = NULL;
11785 if ((asoc->numduptsns) ||
11786 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
11789 * Ok, we have some duplicates or the destination for the
11790 * sack is unreachable, lets see if we can select an
11791 * alternate than asoc->last_data_chunk_from
11793 if ((!(asoc->last_data_chunk_from->dest_state &
11794 SCTP_ADDR_NOT_REACHABLE)) &&
11795 (asoc->used_alt_onsack > asoc->numnets)) {
11796 /* We used an alt last time, don't this time */
11797 a_chk->whoTo = NULL;
11799 asoc->used_alt_onsack++;
11800 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
11802 if (a_chk->whoTo == NULL) {
11803 /* Nope, no alternate */
11804 a_chk->whoTo = asoc->last_data_chunk_from;
11805 asoc->used_alt_onsack = 0;
11809 * No duplicates so we use the last place we received data
11812 asoc->used_alt_onsack = 0;
11813 a_chk->whoTo = asoc->last_data_chunk_from;
11815 if (a_chk->whoTo) {
11816 atomic_add_int(&a_chk->whoTo->ref_count, 1);
11818 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
11820 space_req = sizeof(struct sctp_sack_chunk);
11822 /* gaps get a cluster */
11823 space_req = MCLBYTES;
11825 /* Ok now lets formulate a MBUF with our sack */
11826 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
11827 if ((a_chk->data == NULL) ||
11828 (a_chk->whoTo == NULL)) {
11829 /* rats, no mbuf memory */
11831 /* was a problem with the destination */
11832 sctp_m_freem(a_chk->data);
11833 a_chk->data = NULL;
11835 sctp_free_a_chunk(stcb, a_chk);
11836 /* sa_ignore NO_NULL_CHK */
11837 if (stcb->asoc.delayed_ack) {
11838 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11839 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
11840 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11841 stcb->sctp_ep, stcb, NULL);
11843 stcb->asoc.send_sack = 1;
11847 /* ok, lets go through and fill it in */
11848 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
11849 space = M_TRAILINGSPACE(a_chk->data);
11850 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
11851 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
11853 limit = mtod(a_chk->data, caddr_t);
11856 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
11857 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
11858 /* 0x01 is used by nonce for ecn */
11859 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
11860 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
11861 (asoc->peer_supports_ecn_nonce))
11862 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
11864 sack->ch.chunk_flags = 0;
11866 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
11868 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
11869 * received, then set high bit to 1, else 0. Reset
11872 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
11873 asoc->cmt_dac_pkts_rcvd = 0;
11875 #ifdef SCTP_ASOCLOG_OF_TSNS
11876 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
11877 stcb->asoc.cumack_log_atsnt++;
11878 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
11879 stcb->asoc.cumack_log_atsnt = 0;
11882 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11883 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
11884 asoc->my_last_reported_rwnd = asoc->my_rwnd;
11886 /* reset the readers interpretation */
11887 stcb->freed_by_sorcv_sincelast = 0;
11889 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
11891 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11892 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
11895 * cum-ack behind the mapping array, so we start and use all
11900 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11902 * we skip the first one when the cum-ack is at or above the
11903 * mapping array base. Note this only works if
11907 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
11908 /* we have a gap .. maybe */
11909 for (i = 0; i < siz; i++) {
11910 selector = &sack_array[asoc->mapping_array[i]];
11911 if (mergeable && selector->right_edge) {
11913 * Backup, left and right edges were ok to
11919 if (selector->num_entries == 0)
11922 for (j = jstart; j < selector->num_entries; j++) {
11923 if (mergeable && selector->right_edge) {
11925 * do a merge by NOT setting
11931 * no merge, set the left
11935 gap_descriptor->start = htons((selector->gaps[j].start + offset));
11937 gap_descriptor->end = htons((selector->gaps[j].end + offset));
11940 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11946 if (selector->left_edge) {
11950 if (limit_reached) {
11951 /* Reached the limit stop */
11957 if (num_gap_blocks == 0) {
11959 * slide not yet happened, and somehow we got called
11960 * to send a sack. Cumack needs to move up.
11962 int abort_flag = 0;
11964 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
11965 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11966 sctp_sack_check(stcb, 0, 0, &abort_flag);
11969 /* now we must add any dups we are going to report. */
11970 if ((limit_reached == 0) && (asoc->numduptsns)) {
11971 dup = (uint32_t *) gap_descriptor;
11972 for (i = 0; i < asoc->numduptsns; i++) {
11973 *dup = htonl(asoc->dup_tsns[i]);
11976 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
11981 asoc->numduptsns = 0;
11984 * now that the chunk is prepared queue it to the control chunk
11987 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
11988 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
11989 (num_dups * sizeof(int32_t)));
11990 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11991 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
11992 sack->sack.num_dup_tsns = htons(num_dups);
11993 sack->ch.chunk_length = htons(a_chk->send_size);
11994 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
11995 asoc->ctrl_queue_cnt++;
11996 asoc->send_sack = 0;
11997 SCTP_STAT_INCR(sctps_sendsacks);
12001 /* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
12003 sctp_send_nr_sack(struct sctp_tcb *stcb)
12006 * Queue up an NR-SACK in the control queue. We must first check to see
12007 * if an NR-SACK is somehow on the control queue. If so, we will take
12008 * and and remove the old one.
12010 struct sctp_association *asoc;
12011 struct sctp_tmit_chunk *chk, *a_chk;
12013 struct sctp_nr_sack_chunk *nr_sack;
12015 struct sctp_gap_ack_block *gap_descriptor;
12016 struct sctp_nr_gap_ack_block *nr_gap_descriptor;
12018 struct sack_track *selector;
12019 struct nr_sack_track *nr_selector;
12021 /* EY do we need nr_mergeable, NO */
12026 int limit_reached = 0;
12027 unsigned int i, jstart, siz, j;
12028 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
12031 unsigned int reserved = 0;
12034 asoc = &stcb->asoc;
12035 SCTP_TCB_LOCK_ASSERT(stcb);
12036 if (asoc->last_data_chunk_from == NULL) {
12037 /* Hmm we never received anything */
12040 sctp_set_rwnd(stcb, asoc);
12041 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12042 if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
12043 /* Hmm, found a sack already on queue, remove it */
12044 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
12045 asoc->ctrl_queue_cnt++;
12048 sctp_m_freem(a_chk->data);
12049 a_chk->data = NULL;
12051 sctp_free_remote_addr(a_chk->whoTo);
12052 a_chk->whoTo = NULL;
12056 if (a_chk == NULL) {
12057 sctp_alloc_a_chunk(stcb, a_chk);
12058 if (a_chk == NULL) {
12059 /* No memory so we drop the idea, and set a timer */
12060 if (stcb->asoc.delayed_ack) {
12061 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12062 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
12063 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12064 stcb->sctp_ep, stcb, NULL);
12066 stcb->asoc.send_sack = 1;
12070 a_chk->copy_by_ref = 0;
12071 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
12072 a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
12073 a_chk->rec.chunk_id.can_take_data = 1;
12075 /* Clear our pkt counts */
12076 asoc->data_pkts_seen = 0;
12078 a_chk->asoc = asoc;
12079 a_chk->snd_count = 0;
12080 a_chk->send_size = 0; /* fill in later */
12081 a_chk->sent = SCTP_DATAGRAM_UNSENT;
12082 a_chk->whoTo = NULL;
12084 if ((asoc->numduptsns) ||
12085 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
12088 * Ok, we have some duplicates or the destination for the
12089 * sack is unreachable, lets see if we can select an
12090 * alternate than asoc->last_data_chunk_from
12092 if ((!(asoc->last_data_chunk_from->dest_state &
12093 SCTP_ADDR_NOT_REACHABLE)) &&
12094 (asoc->used_alt_onsack > asoc->numnets)) {
12095 /* We used an alt last time, don't this time */
12096 a_chk->whoTo = NULL;
12098 asoc->used_alt_onsack++;
12099 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
12101 if (a_chk->whoTo == NULL) {
12102 /* Nope, no alternate */
12103 a_chk->whoTo = asoc->last_data_chunk_from;
12104 asoc->used_alt_onsack = 0;
12108 * No duplicates so we use the last place we received data
12111 asoc->used_alt_onsack = 0;
12112 a_chk->whoTo = asoc->last_data_chunk_from;
12114 if (a_chk->whoTo) {
12115 atomic_add_int(&a_chk->whoTo->ref_count, 1);
12117 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
12119 space_req = sizeof(struct sctp_nr_sack_chunk);
12121 /* EY - what is this about? */
12122 /* gaps get a cluster */
12123 space_req = MCLBYTES;
12125 /* Ok now lets formulate a MBUF with our sack */
12126 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
12127 if ((a_chk->data == NULL) ||
12128 (a_chk->whoTo == NULL)) {
12129 /* rats, no mbuf memory */
12131 /* was a problem with the destination */
12132 sctp_m_freem(a_chk->data);
12133 a_chk->data = NULL;
12135 sctp_free_a_chunk(stcb, a_chk);
12136 /* sa_ignore NO_NULL_CHK */
12137 if (stcb->asoc.delayed_ack) {
12138 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12139 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12140 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12141 stcb->sctp_ep, stcb, NULL);
12143 stcb->asoc.send_sack = 1;
12147 /* ok, lets go through and fill it in */
12148 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
12149 space = M_TRAILINGSPACE(a_chk->data);
12150 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
12151 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
12153 limit = mtod(a_chk->data, caddr_t);
12156 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
12157 nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
12159 /* 0x01 is used by nonce for ecn */
12160 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
12161 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
12162 (asoc->peer_supports_ecn_nonce))
12163 nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
12165 nr_sack->ch.chunk_flags = 0;
12167 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
12169 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
12170 * received, then set high bit to 1, else 0. Reset
12173 /* EY - TODO: which chunk flag is used in here? -The LSB */
12174 nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
12175 asoc->cmt_dac_pkts_rcvd = 0;
12178 * EY - this is a never reneging receiver, that makes all gaps are
12179 * nr-gaps, set the All bit
12181 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12182 nr_sack->ch.chunk_flags |= SCTP_NR_SACK_ALL_BIT;
12184 #ifdef SCTP_ASOCLOG_OF_TSNS
12185 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
12186 stcb->asoc.cumack_log_atsnt++;
12187 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
12188 stcb->asoc.cumack_log_atsnt = 0;
12191 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12192 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
12193 asoc->my_last_reported_rwnd = asoc->my_rwnd;
12195 /* reset the readers interpretation */
12196 stcb->freed_by_sorcv_sincelast = 0;
12198 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12199 nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12201 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
12202 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12205 * cum-ack behind the mapping array, so we start and use all
12210 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
12212 * we skip the first one when the cum-ack is at or above the
12213 * mapping array base. Note this only works if
12217 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
12218 /* we have a gap .. maybe */
12219 for (i = 0; i < siz; i++) {
12220 selector = &sack_array[asoc->mapping_array[i]];
12221 if (mergeable && selector->right_edge) {
12223 * Backup, left and right edges were ok to
12229 if (selector->num_entries == 0)
12232 for (j = jstart; j < selector->num_entries; j++) {
12233 if (mergeable && selector->right_edge) {
12235 * do a merge by NOT setting
12241 * no merge, set the left
12245 gap_descriptor->start = htons((selector->gaps[j].start + offset));
12247 gap_descriptor->end = htons((selector->gaps[j].end + offset));
12250 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
12256 if (selector->left_edge) {
12260 if (limit_reached) {
12261 /* Reached the limit stop */
12267 if (num_gap_blocks == 0) {
12269 * slide not yet happened, and somehow we got called
12270 * to send a sack. Cumack needs to move up.
12272 int abort_flag = 0;
12274 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
12275 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12276 sctp_sack_check(stcb, 0, 0, &abort_flag);
12279 /*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
12281 nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
12283 /* EY - there will be gaps + nr_gaps if draining is possible */
12284 if (SCTP_BASE_SYSCTL(sctp_do_drain)) {
12288 siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
12289 if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12292 * cum-ack behind the mapping array, so we start and use all
12297 offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
12299 * we skip the first one when the cum-ack is at or above the
12300 * mapping array base. Note this only works if
12304 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
12305 /* we have a gap .. maybe */
12306 for (i = 0; i < siz; i++) {
12307 nr_selector = &nr_sack_array[asoc->nr_mapping_array[i]];
12308 if (mergeable && nr_selector->right_edge) {
12310 * Backup, left and right edges were
12313 num_nr_gap_blocks--;
12314 nr_gap_descriptor--;
12316 if (nr_selector->num_entries == 0)
12319 for (j = jstart; j < nr_selector->num_entries; j++) {
12320 if (mergeable && nr_selector->right_edge) {
12322 * do a merge by NOT
12329 * no merge, set the
12333 nr_gap_descriptor->start = htons((nr_selector->nr_gaps[j].start + offset));
12335 nr_gap_descriptor->end = htons((nr_selector->nr_gaps[j].end + offset));
12336 num_nr_gap_blocks++;
12337 nr_gap_descriptor++;
12338 if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
12344 if (nr_selector->left_edge) {
12348 if (limit_reached) {
12349 /* Reached the limit stop */
12357 /*---------------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
12359 /* now we must add any dups we are going to report. */
12360 if ((limit_reached == 0) && (asoc->numduptsns)) {
12361 dup = (uint32_t *) nr_gap_descriptor;
12362 for (i = 0; i < asoc->numduptsns; i++) {
12363 *dup = htonl(asoc->dup_tsns[i]);
12366 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
12371 asoc->numduptsns = 0;
12374 * now that the chunk is prepared queue it to the control chunk
12377 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12378 num_nr_gap_blocks = num_gap_blocks;
12379 num_gap_blocks = 0;
12381 a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
12382 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
12383 (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
12384 (num_dups * sizeof(int32_t)));
12386 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
12387 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
12388 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
12389 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
12390 nr_sack->nr_sack.reserved = htons(reserved);
12391 nr_sack->ch.chunk_length = htons(a_chk->send_size);
12392 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
12393 asoc->ctrl_queue_cnt++;
12394 asoc->send_sack = 0;
12395 SCTP_STAT_INCR(sctps_sendsacks);
12400 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
12401 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
12406 struct mbuf *m_abort;
12407 struct mbuf *m_out = NULL, *m_end = NULL;
12408 struct sctp_abort_chunk *abort = NULL;
12410 uint32_t auth_offset = 0;
12411 struct sctp_auth_chunk *auth = NULL;
12412 struct sctphdr *shdr;
12415 * Add an AUTH chunk, if chunk requires it and save the offset into
12416 * the chain for AUTH
12418 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
12419 stcb->asoc.peer_auth_chunks)) {
12420 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
12421 stcb, SCTP_ABORT_ASSOCIATION);
12422 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12424 SCTP_TCB_LOCK_ASSERT(stcb);
12425 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
12426 if (m_abort == NULL) {
12429 sctp_m_freem(m_out);
12432 /* link in any error */
12433 SCTP_BUF_NEXT(m_abort) = operr;
12440 sz += SCTP_BUF_LEN(n);
12441 n = SCTP_BUF_NEXT(n);
12444 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
12445 if (m_out == NULL) {
12446 /* NO Auth chunk prepended, so reserve space in front */
12447 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
12450 /* Put AUTH chunk at the front of the chain */
12451 SCTP_BUF_NEXT(m_end) = m_abort;
12454 /* fill in the ABORT chunk */
12455 abort = mtod(m_abort, struct sctp_abort_chunk *);
12456 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12457 abort->ch.chunk_flags = 0;
12458 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
12460 /* prepend and fill in the SCTP header */
12461 SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
12462 if (m_out == NULL) {
12463 /* TSNH: no memory */
12466 shdr = mtod(m_out, struct sctphdr *);
12467 shdr->src_port = stcb->sctp_ep->sctp_lport;
12468 shdr->dest_port = stcb->rport;
12469 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
12470 shdr->checksum = 0;
12471 auth_offset += sizeof(struct sctphdr);
12473 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
12474 stcb->asoc.primary_destination,
12475 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
12476 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
12477 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12481 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
12482 struct sctp_nets *net)
12484 /* formulate and SEND a SHUTDOWN-COMPLETE */
12485 struct mbuf *m_shutdown_comp;
12486 struct sctp_shutdown_complete_msg *comp_cp;
12488 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
12489 if (m_shutdown_comp == NULL) {
12493 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
12494 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12495 comp_cp->shut_cmp.ch.chunk_flags = 0;
12496 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12497 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
12498 comp_cp->sh.dest_port = stcb->rport;
12499 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
12500 comp_cp->sh.checksum = 0;
12502 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
12503 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
12504 (struct sockaddr *)&net->ro._l_addr,
12505 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
12506 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12511 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
12512 uint32_t vrf_id, uint16_t port)
12514 /* formulate and SEND a SHUTDOWN-COMPLETE */
12515 struct mbuf *o_pak;
12517 struct ip *iph, *iph_out;
12518 struct udphdr *udp = NULL;
12521 struct ip6_hdr *ip6, *ip6_out;
12524 int offset_out, len, mlen;
12525 struct sctp_shutdown_complete_msg *comp_cp;
12527 iph = mtod(m, struct ip *);
12528 switch (iph->ip_v) {
12530 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
12533 case IPV6_VERSION >> 4:
12534 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
12541 len += sizeof(struct udphdr);
12543 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
12544 if (mout == NULL) {
12547 SCTP_BUF_LEN(mout) = len;
12548 SCTP_BUF_NEXT(mout) = NULL;
12555 switch (iph->ip_v) {
12557 iph_out = mtod(mout, struct ip *);
12559 /* Fill in the IP header for the ABORT */
12560 iph_out->ip_v = IPVERSION;
12561 iph_out->ip_hl = (sizeof(struct ip) / 4);
12562 iph_out->ip_tos = (u_char)0;
12563 iph_out->ip_id = 0;
12564 iph_out->ip_off = 0;
12565 iph_out->ip_ttl = MAXTTL;
12567 iph_out->ip_p = IPPROTO_UDP;
12569 iph_out->ip_p = IPPROTO_SCTP;
12571 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12572 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12574 /* let IP layer calculate this */
12575 iph_out->ip_sum = 0;
12576 offset_out += sizeof(*iph_out);
12577 comp_cp = (struct sctp_shutdown_complete_msg *)(
12578 (caddr_t)iph_out + offset_out);
12581 case IPV6_VERSION >> 4:
12582 ip6 = (struct ip6_hdr *)iph;
12583 ip6_out = mtod(mout, struct ip6_hdr *);
12585 /* Fill in the IPv6 header for the ABORT */
12586 ip6_out->ip6_flow = ip6->ip6_flow;
12587 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
12589 ip6_out->ip6_nxt = IPPROTO_UDP;
12591 ip6_out->ip6_nxt = IPPROTO_SCTP;
12593 ip6_out->ip6_src = ip6->ip6_dst;
12594 ip6_out->ip6_dst = ip6->ip6_src;
12596 * ?? The old code had both the iph len + payload, I think
12597 * this is wrong and would never have worked
12599 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
12600 offset_out += sizeof(*ip6_out);
12601 comp_cp = (struct sctp_shutdown_complete_msg *)(
12602 (caddr_t)ip6_out + offset_out);
12606 /* Currently not supported. */
12610 udp = (struct udphdr *)comp_cp;
12611 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12612 udp->uh_dport = port;
12613 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
12614 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12615 offset_out += sizeof(struct udphdr);
12616 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
12618 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12620 sctp_m_freem(mout);
12623 /* Now copy in and fill in the ABORT tags etc. */
12624 comp_cp->sh.src_port = sh->dest_port;
12625 comp_cp->sh.dest_port = sh->src_port;
12626 comp_cp->sh.checksum = 0;
12627 comp_cp->sh.v_tag = sh->v_tag;
12628 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
12629 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12630 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12633 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
12634 if (iph_out != NULL) {
12637 struct sctp_tcb *stcb = NULL;
12639 mlen = SCTP_BUF_LEN(mout);
12640 bzero(&ro, sizeof ro);
12641 /* set IPv4 length */
12642 iph_out->ip_len = mlen;
12643 #ifdef SCTP_PACKET_LOGGING
12644 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12645 sctp_packet_log(mout, mlen);
12647 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12649 SCTP_ENABLE_UDP_CSUM(o_pak);
12652 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12654 /* Free the route if we got one back */
12659 if (ip6_out != NULL) {
12660 struct route_in6 ro;
12662 struct sctp_tcb *stcb = NULL;
12663 struct ifnet *ifp = NULL;
12665 bzero(&ro, sizeof(ro));
12666 mlen = SCTP_BUF_LEN(mout);
12667 #ifdef SCTP_PACKET_LOGGING
12668 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12669 sctp_packet_log(mout, mlen);
12671 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12673 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
12674 sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
12675 udp->uh_sum = 0xffff;
12678 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12680 /* Free the route if we got one back */
12685 SCTP_STAT_INCR(sctps_sendpackets);
12686 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12687 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12692 static struct sctp_nets *
12693 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
12695 struct sctp_nets *net, *hnet;
12696 int ms_goneby, highest_ms, state_overide = 0;
12698 (void)SCTP_GETTIME_TIMEVAL(now);
12701 SCTP_TCB_LOCK_ASSERT(stcb);
12702 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
12704 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
12705 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
12708 * Skip this guy from consideration if HB is off AND
12713 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
12714 /* skip this dest net from consideration */
12717 if (net->last_sent_time.tv_sec) {
12718 /* Sent to so we subtract */
12719 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
12721 /* Never been sent to */
12722 ms_goneby = 0x7fffffff;
12724 * When the address state is unconfirmed but still
12725 * considered reachable, we HB at a higher rate. Once it
12726 * goes confirmed OR reaches the "unreachable" state, thenw
12727 * we cut it back to HB at a more normal pace.
12729 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
12735 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
12736 (ms_goneby > highest_ms)) {
12737 highest_ms = ms_goneby;
12742 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
12748 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
12750 * Found the one with longest delay bounds OR it is
12751 * unconfirmed and still not marked unreachable.
12753 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
12756 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
12757 (struct sockaddr *)&hnet->ro._l_addr);
12759 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
12762 /* update the timer now */
12763 hnet->last_sent_time = *now;
12766 /* Nothing to HB */
12771 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
12773 struct sctp_tmit_chunk *chk;
12774 struct sctp_nets *net;
12775 struct sctp_heartbeat_chunk *hb;
12776 struct timeval now;
12777 struct sockaddr_in *sin;
12778 struct sockaddr_in6 *sin6;
12780 SCTP_TCB_LOCK_ASSERT(stcb);
12781 if (user_req == 0) {
12782 net = sctp_select_hb_destination(stcb, &now);
12785 * All our busy none to send to, just start the
12788 if (stcb->asoc.state == 0) {
12791 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
12802 (void)SCTP_GETTIME_TIMEVAL(&now);
12804 sin = (struct sockaddr_in *)&net->ro._l_addr;
12805 if (sin->sin_family != AF_INET) {
12806 if (sin->sin_family != AF_INET6) {
12811 sctp_alloc_a_chunk(stcb, chk);
12813 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
12816 chk->copy_by_ref = 0;
12817 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
12818 chk->rec.chunk_id.can_take_data = 1;
12819 chk->asoc = &stcb->asoc;
12820 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
12822 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12823 if (chk->data == NULL) {
12824 sctp_free_a_chunk(stcb, chk);
12827 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12828 SCTP_BUF_LEN(chk->data) = chk->send_size;
12829 chk->sent = SCTP_DATAGRAM_UNSENT;
12830 chk->snd_count = 0;
12832 atomic_add_int(&chk->whoTo->ref_count, 1);
12833 /* Now we have a mbuf that we can fill in with the details */
12834 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
12835 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
12836 /* fill out chunk header */
12837 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
12838 hb->ch.chunk_flags = 0;
12839 hb->ch.chunk_length = htons(chk->send_size);
12840 /* Fill out hb parameter */
12841 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
12842 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
12843 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
12844 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
12845 /* Did our user request this one, put it in */
12846 hb->heartbeat.hb_info.user_req = user_req;
12847 hb->heartbeat.hb_info.addr_family = sin->sin_family;
12848 hb->heartbeat.hb_info.addr_len = sin->sin_len;
12849 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
12851 * we only take from the entropy pool if the address is not
12854 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12855 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12857 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
12858 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
12860 if (sin->sin_family == AF_INET) {
12861 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
12862 } else if (sin->sin_family == AF_INET6) {
12863 /* We leave the scope the way it is in our lookup table. */
12864 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
12865 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
12867 /* huh compiler bug */
12872 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
12873 * PF-heartbeats. Because of this, threshold management is done by
12874 * the t3 timer handler, and does not need to be done upon the send
12875 * of a PF-heartbeat. If CMT PF is on and the destination to which a
12876 * heartbeat is being sent is in PF state, do NOT do threshold
12879 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
12880 /* ok we have a destination that needs a beat */
12881 /* lets do the theshold management Qiaobing style */
12882 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
12883 stcb->asoc.max_send_times)) {
12885 * we have lost the association, in a way this is
12886 * quite bad since we really are one less time since
12887 * we really did not send yet. This is the down side
12888 * to the Q's style as defined in the RFC and not my
12889 * alternate style defined in the RFC.
12891 if (chk->data != NULL) {
12892 sctp_m_freem(chk->data);
12896 * Here we do NOT use the macro since the
12897 * association is now gone.
12900 sctp_free_remote_addr(chk->whoTo);
12903 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
12907 net->hb_responded = 0;
12908 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12909 stcb->asoc.ctrl_queue_cnt++;
12910 SCTP_STAT_INCR(sctps_sendheartbeat);
12912 * Call directly med level routine to put out the chunk. It will
12913 * always tumble out control chunks aka HB but it may even tumble
12920 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
12923 struct sctp_association *asoc;
12924 struct sctp_ecne_chunk *ecne;
12925 struct sctp_tmit_chunk *chk;
12927 asoc = &stcb->asoc;
12928 SCTP_TCB_LOCK_ASSERT(stcb);
12929 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12930 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
12931 /* found a previous ECN_ECHO update it if needed */
12932 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12933 ecne->tsn = htonl(high_tsn);
12937 /* nope could not find one to update so we must build one */
12938 sctp_alloc_a_chunk(stcb, chk);
12942 chk->copy_by_ref = 0;
12943 SCTP_STAT_INCR(sctps_sendecne);
12944 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
12945 chk->rec.chunk_id.can_take_data = 0;
12946 chk->asoc = &stcb->asoc;
12947 chk->send_size = sizeof(struct sctp_ecne_chunk);
12948 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12949 if (chk->data == NULL) {
12950 sctp_free_a_chunk(stcb, chk);
12953 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12954 SCTP_BUF_LEN(chk->data) = chk->send_size;
12955 chk->sent = SCTP_DATAGRAM_UNSENT;
12956 chk->snd_count = 0;
12958 atomic_add_int(&chk->whoTo->ref_count, 1);
12959 stcb->asoc.ecn_echo_cnt_onq++;
12960 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12961 ecne->ch.chunk_type = SCTP_ECN_ECHO;
12962 ecne->ch.chunk_flags = 0;
12963 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
12964 ecne->tsn = htonl(high_tsn);
12965 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12966 asoc->ctrl_queue_cnt++;
12970 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
12971 struct mbuf *m, int iphlen, int bad_crc)
12973 struct sctp_association *asoc;
12974 struct sctp_pktdrop_chunk *drp;
12975 struct sctp_tmit_chunk *chk;
12982 struct ip6_hdr *ip6h;
12985 int fullsz = 0, extra = 0;
12988 struct sctp_chunkhdr *ch, chunk_buf;
12989 unsigned int chk_length;
12994 asoc = &stcb->asoc;
12995 SCTP_TCB_LOCK_ASSERT(stcb);
12996 if (asoc->peer_supports_pktdrop == 0) {
12998 * peer must declare support before I send one.
13002 if (stcb->sctp_socket == NULL) {
13005 sctp_alloc_a_chunk(stcb, chk);
13009 chk->copy_by_ref = 0;
13010 iph = mtod(m, struct ip *);
13012 sctp_free_a_chunk(stcb, chk);
13015 switch (iph->ip_v) {
13018 len = chk->send_size = iph->ip_len;
13021 case IPV6_VERSION >> 4:
13023 ip6h = mtod(m, struct ip6_hdr *);
13024 len = chk->send_size = htons(ip6h->ip6_plen);
13030 /* Validate that we do not have an ABORT in here. */
13031 offset = iphlen + sizeof(struct sctphdr);
13032 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13033 sizeof(*ch), (uint8_t *) & chunk_buf);
13034 while (ch != NULL) {
13035 chk_length = ntohs(ch->chunk_length);
13036 if (chk_length < sizeof(*ch)) {
13037 /* break to abort land */
13040 switch (ch->chunk_type) {
13041 case SCTP_PACKET_DROPPED:
13042 case SCTP_ABORT_ASSOCIATION:
13044 * we don't respond with an PKT-DROP to an ABORT
13047 sctp_free_a_chunk(stcb, chk);
13052 offset += SCTP_SIZE32(chk_length);
13053 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13054 sizeof(*ch), (uint8_t *) & chunk_buf);
13057 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
13058 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
13060 * only send 1 mtu worth, trim off the excess on the end.
13062 fullsz = len - extra;
13063 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
13066 chk->asoc = &stcb->asoc;
13067 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13068 if (chk->data == NULL) {
13070 sctp_free_a_chunk(stcb, chk);
13073 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13074 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
13076 sctp_m_freem(chk->data);
13080 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
13081 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
13082 chk->book_size_scale = 0;
13084 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
13085 drp->trunc_len = htons(fullsz);
13087 * Len is already adjusted to size minus overhead above take
13088 * out the pkt_drop chunk itself from it.
13090 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
13091 len = chk->send_size;
13093 /* no truncation needed */
13094 drp->ch.chunk_flags = 0;
13095 drp->trunc_len = htons(0);
13098 drp->ch.chunk_flags |= SCTP_BADCRC;
13100 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
13101 SCTP_BUF_LEN(chk->data) = chk->send_size;
13102 chk->sent = SCTP_DATAGRAM_UNSENT;
13103 chk->snd_count = 0;
13105 /* we should hit here */
13108 chk->whoTo = asoc->primary_destination;
13110 atomic_add_int(&chk->whoTo->ref_count, 1);
13111 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
13112 chk->rec.chunk_id.can_take_data = 1;
13113 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
13114 drp->ch.chunk_length = htons(chk->send_size);
13115 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
13119 drp->bottle_bw = htonl(spc);
13120 if (asoc->my_rwnd) {
13121 drp->current_onq = htonl(asoc->size_on_reasm_queue +
13122 asoc->size_on_all_streams +
13123 asoc->my_rwnd_control_len +
13124 stcb->sctp_socket->so_rcv.sb_cc);
13127 * If my rwnd is 0, possibly from mbuf depletion as well as
13128 * space used, tell the peer there is NO space aka onq == bw
13130 drp->current_onq = htonl(spc);
13134 m_copydata(m, iphlen, len, (caddr_t)datap);
13135 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13136 asoc->ctrl_queue_cnt++;
13140 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
13142 struct sctp_association *asoc;
13143 struct sctp_cwr_chunk *cwr;
13144 struct sctp_tmit_chunk *chk;
13146 asoc = &stcb->asoc;
13147 SCTP_TCB_LOCK_ASSERT(stcb);
13148 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
13149 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
13150 /* found a previous ECN_CWR update it if needed */
13151 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13152 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
13154 cwr->tsn = htonl(high_tsn);
13159 /* nope could not find one to update so we must build one */
13160 sctp_alloc_a_chunk(stcb, chk);
13164 chk->copy_by_ref = 0;
13165 chk->rec.chunk_id.id = SCTP_ECN_CWR;
13166 chk->rec.chunk_id.can_take_data = 1;
13167 chk->asoc = &stcb->asoc;
13168 chk->send_size = sizeof(struct sctp_cwr_chunk);
13169 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
13170 if (chk->data == NULL) {
13171 sctp_free_a_chunk(stcb, chk);
13174 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13175 SCTP_BUF_LEN(chk->data) = chk->send_size;
13176 chk->sent = SCTP_DATAGRAM_UNSENT;
13177 chk->snd_count = 0;
13179 atomic_add_int(&chk->whoTo->ref_count, 1);
13180 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13181 cwr->ch.chunk_type = SCTP_ECN_CWR;
13182 cwr->ch.chunk_flags = 0;
13183 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
13184 cwr->tsn = htonl(high_tsn);
13185 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13186 asoc->ctrl_queue_cnt++;
13190 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
13191 int number_entries, uint16_t * list,
13192 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
13194 int len, old_len, i;
13195 struct sctp_stream_reset_out_request *req_out;
13196 struct sctp_chunkhdr *ch;
13198 ch = mtod(chk->data, struct sctp_chunkhdr *);
13201 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13203 /* get to new offset for the param. */
13204 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
13205 /* now how long will this param be? */
13206 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
13207 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
13208 req_out->ph.param_length = htons(len);
13209 req_out->request_seq = htonl(seq);
13210 req_out->response_seq = htonl(resp_seq);
13211 req_out->send_reset_at_tsn = htonl(last_sent);
13212 if (number_entries) {
13213 for (i = 0; i < number_entries; i++) {
13214 req_out->list_of_streams[i] = htons(list[i]);
13217 if (SCTP_SIZE32(len) > len) {
13219 * Need to worry about the pad we may end up adding to the
13220 * end. This is easy since the struct is either aligned to 4
13221 * bytes or 2 bytes off.
13223 req_out->list_of_streams[number_entries] = 0;
13225 /* now fix the chunk length */
13226 ch->chunk_length = htons(len + old_len);
13227 chk->book_size = len + old_len;
13228 chk->book_size_scale = 0;
13229 chk->send_size = SCTP_SIZE32(chk->book_size);
13230 SCTP_BUF_LEN(chk->data) = chk->send_size;
13236 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
13237 int number_entries, uint16_t * list,
13240 int len, old_len, i;
13241 struct sctp_stream_reset_in_request *req_in;
13242 struct sctp_chunkhdr *ch;
13244 ch = mtod(chk->data, struct sctp_chunkhdr *);
13247 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13249 /* get to new offset for the param. */
13250 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
13251 /* now how long will this param be? */
13252 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
13253 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
13254 req_in->ph.param_length = htons(len);
13255 req_in->request_seq = htonl(seq);
13256 if (number_entries) {
13257 for (i = 0; i < number_entries; i++) {
13258 req_in->list_of_streams[i] = htons(list[i]);
13261 if (SCTP_SIZE32(len) > len) {
13263 * Need to worry about the pad we may end up adding to the
13264 * end. This is easy since the struct is either aligned to 4
13265 * bytes or 2 bytes off.
13267 req_in->list_of_streams[number_entries] = 0;
13269 /* now fix the chunk length */
13270 ch->chunk_length = htons(len + old_len);
13271 chk->book_size = len + old_len;
13272 chk->book_size_scale = 0;
13273 chk->send_size = SCTP_SIZE32(chk->book_size);
13274 SCTP_BUF_LEN(chk->data) = chk->send_size;
13280 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
13284 struct sctp_stream_reset_tsn_request *req_tsn;
13285 struct sctp_chunkhdr *ch;
13287 ch = mtod(chk->data, struct sctp_chunkhdr *);
13290 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13292 /* get to new offset for the param. */
13293 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
13294 /* now how long will this param be? */
13295 len = sizeof(struct sctp_stream_reset_tsn_request);
13296 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
13297 req_tsn->ph.param_length = htons(len);
13298 req_tsn->request_seq = htonl(seq);
13300 /* now fix the chunk length */
13301 ch->chunk_length = htons(len + old_len);
13302 chk->send_size = len + old_len;
13303 chk->book_size = SCTP_SIZE32(chk->send_size);
13304 chk->book_size_scale = 0;
13305 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13310 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
13311 uint32_t resp_seq, uint32_t result)
13314 struct sctp_stream_reset_response *resp;
13315 struct sctp_chunkhdr *ch;
13317 ch = mtod(chk->data, struct sctp_chunkhdr *);
13320 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13322 /* get to new offset for the param. */
13323 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
13324 /* now how long will this param be? */
13325 len = sizeof(struct sctp_stream_reset_response);
13326 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13327 resp->ph.param_length = htons(len);
13328 resp->response_seq = htonl(resp_seq);
13329 resp->result = ntohl(result);
13331 /* now fix the chunk length */
13332 ch->chunk_length = htons(len + old_len);
13333 chk->book_size = len + old_len;
13334 chk->book_size_scale = 0;
13335 chk->send_size = SCTP_SIZE32(chk->book_size);
13336 SCTP_BUF_LEN(chk->data) = chk->send_size;
13343 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
13344 uint32_t resp_seq, uint32_t result,
13345 uint32_t send_una, uint32_t recv_next)
13348 struct sctp_stream_reset_response_tsn *resp;
13349 struct sctp_chunkhdr *ch;
13351 ch = mtod(chk->data, struct sctp_chunkhdr *);
13354 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13356 /* get to new offset for the param. */
13357 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
13358 /* now how long will this param be? */
13359 len = sizeof(struct sctp_stream_reset_response_tsn);
13360 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13361 resp->ph.param_length = htons(len);
13362 resp->response_seq = htonl(resp_seq);
13363 resp->result = htonl(result);
13364 resp->senders_next_tsn = htonl(send_una);
13365 resp->receivers_next_tsn = htonl(recv_next);
13367 /* now fix the chunk length */
13368 ch->chunk_length = htons(len + old_len);
13369 chk->book_size = len + old_len;
13370 chk->send_size = SCTP_SIZE32(chk->book_size);
13371 chk->book_size_scale = 0;
13372 SCTP_BUF_LEN(chk->data) = chk->send_size;
13378 sctp_send_str_reset_req(struct sctp_tcb *stcb,
13379 int number_entries, uint16_t * list,
13380 uint8_t send_out_req, uint32_t resp_seq,
13381 uint8_t send_in_req,
13382 uint8_t send_tsn_req)
13385 struct sctp_association *asoc;
13386 struct sctp_tmit_chunk *chk;
13387 struct sctp_chunkhdr *ch;
13390 asoc = &stcb->asoc;
13391 if (asoc->stream_reset_outstanding) {
13393 * Already one pending, must get ACK back to clear the flag.
13395 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
13398 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
13399 /* nothing to do */
13400 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13403 if (send_tsn_req && (send_out_req || send_in_req)) {
13404 /* error, can't do that */
13405 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13408 sctp_alloc_a_chunk(stcb, chk);
13410 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13413 chk->copy_by_ref = 0;
13414 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
13415 chk->rec.chunk_id.can_take_data = 0;
13416 chk->asoc = &stcb->asoc;
13417 chk->book_size = sizeof(struct sctp_chunkhdr);
13418 chk->send_size = SCTP_SIZE32(chk->book_size);
13419 chk->book_size_scale = 0;
13421 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13422 if (chk->data == NULL) {
13423 sctp_free_a_chunk(stcb, chk);
13424 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13427 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13429 /* setup chunk parameters */
13430 chk->sent = SCTP_DATAGRAM_UNSENT;
13431 chk->snd_count = 0;
13432 chk->whoTo = asoc->primary_destination;
13433 atomic_add_int(&chk->whoTo->ref_count, 1);
13435 ch = mtod(chk->data, struct sctp_chunkhdr *);
13436 ch->chunk_type = SCTP_STREAM_RESET;
13437 ch->chunk_flags = 0;
13438 ch->chunk_length = htons(chk->book_size);
13439 SCTP_BUF_LEN(chk->data) = chk->send_size;
13441 seq = stcb->asoc.str_reset_seq_out;
13442 if (send_out_req) {
13443 sctp_add_stream_reset_out(chk, number_entries, list,
13444 seq, resp_seq, (stcb->asoc.sending_seq - 1));
13445 asoc->stream_reset_out_is_outstanding = 1;
13447 asoc->stream_reset_outstanding++;
13450 sctp_add_stream_reset_in(chk, number_entries, list, seq);
13451 asoc->stream_reset_outstanding++;
13453 if (send_tsn_req) {
13454 sctp_add_stream_reset_tsn(chk, seq);
13455 asoc->stream_reset_outstanding++;
13457 asoc->str_reset = chk;
13459 /* insert the chunk for sending */
13460 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
13463 asoc->ctrl_queue_cnt++;
13464 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
13469 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
13470 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
13473 * Formulate the abort message, and send it back down.
13475 struct mbuf *o_pak;
13477 struct sctp_abort_msg *abm;
13478 struct ip *iph, *iph_out;
13479 struct udphdr *udp;
13482 struct ip6_hdr *ip6, *ip6_out;
13485 int iphlen_out, len;
13487 /* don't respond to ABORT with ABORT */
13488 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
13490 sctp_m_freem(err_cause);
13493 iph = mtod(m, struct ip *);
13494 switch (iph->ip_v) {
13496 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
13499 case IPV6_VERSION >> 4:
13500 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
13507 len += sizeof(struct udphdr);
13509 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
13510 if (mout == NULL) {
13512 sctp_m_freem(err_cause);
13515 SCTP_BUF_LEN(mout) = len;
13516 SCTP_BUF_NEXT(mout) = err_cause;
13521 switch (iph->ip_v) {
13523 iph_out = mtod(mout, struct ip *);
13525 /* Fill in the IP header for the ABORT */
13526 iph_out->ip_v = IPVERSION;
13527 iph_out->ip_hl = (sizeof(struct ip) / 4);
13528 iph_out->ip_tos = (u_char)0;
13529 iph_out->ip_id = 0;
13530 iph_out->ip_off = 0;
13531 iph_out->ip_ttl = MAXTTL;
13533 iph_out->ip_p = IPPROTO_UDP;
13535 iph_out->ip_p = IPPROTO_SCTP;
13537 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13538 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13539 /* let IP layer calculate this */
13540 iph_out->ip_sum = 0;
13542 iphlen_out = sizeof(*iph_out);
13543 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
13546 case IPV6_VERSION >> 4:
13547 ip6 = (struct ip6_hdr *)iph;
13548 ip6_out = mtod(mout, struct ip6_hdr *);
13550 /* Fill in the IP6 header for the ABORT */
13551 ip6_out->ip6_flow = ip6->ip6_flow;
13552 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13554 ip6_out->ip6_nxt = IPPROTO_UDP;
13556 ip6_out->ip6_nxt = IPPROTO_SCTP;
13558 ip6_out->ip6_src = ip6->ip6_dst;
13559 ip6_out->ip6_dst = ip6->ip6_src;
13561 iphlen_out = sizeof(*ip6_out);
13562 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
13566 /* Currently not supported */
13568 sctp_m_freem(err_cause);
13569 sctp_m_freem(mout);
13573 udp = (struct udphdr *)abm;
13575 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13576 udp->uh_dport = port;
13577 /* set udp->uh_ulen later */
13579 iphlen_out += sizeof(struct udphdr);
13580 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
13582 abm->sh.src_port = sh->dest_port;
13583 abm->sh.dest_port = sh->src_port;
13584 abm->sh.checksum = 0;
13586 abm->sh.v_tag = sh->v_tag;
13587 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
13589 abm->sh.v_tag = htonl(vtag);
13590 abm->msg.ch.chunk_flags = 0;
13592 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
13595 struct mbuf *m_tmp = err_cause;
13598 /* get length of the err_cause chain */
13599 while (m_tmp != NULL) {
13600 err_len += SCTP_BUF_LEN(m_tmp);
13601 m_tmp = SCTP_BUF_NEXT(m_tmp);
13603 len = SCTP_BUF_LEN(mout) + err_len;
13605 /* need pad at end of chunk */
13606 uint32_t cpthis = 0;
13609 padlen = 4 - (len % 4);
13610 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13613 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
13615 len = SCTP_BUF_LEN(mout);
13616 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
13620 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
13621 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13623 sctp_m_freem(mout);
13626 if (iph_out != NULL) {
13628 struct sctp_tcb *stcb = NULL;
13631 /* zap the stack pointer to the route */
13632 bzero(&ro, sizeof ro);
13634 udp->uh_ulen = htons(len - sizeof(struct ip));
13635 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13637 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
13638 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
13639 /* set IPv4 length */
13640 iph_out->ip_len = len;
13642 #ifdef SCTP_PACKET_LOGGING
13643 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13644 sctp_packet_log(mout, len);
13646 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13648 SCTP_ENABLE_UDP_CSUM(o_pak);
13650 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13652 /* Free the route if we got one back */
13657 if (ip6_out != NULL) {
13658 struct route_in6 ro;
13660 struct sctp_tcb *stcb = NULL;
13661 struct ifnet *ifp = NULL;
13663 /* zap the stack pointer to the route */
13664 bzero(&ro, sizeof(ro));
13666 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13668 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
13669 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
13670 ip6_out->ip6_plen = len - sizeof(*ip6_out);
13671 #ifdef SCTP_PACKET_LOGGING
13672 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13673 sctp_packet_log(mout, len);
13675 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13677 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13678 udp->uh_sum = 0xffff;
13681 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13683 /* Free the route if we got one back */
13688 SCTP_STAT_INCR(sctps_sendpackets);
13689 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13693 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
13694 uint32_t vrf_id, uint16_t port)
13696 struct mbuf *o_pak;
13697 struct sctphdr *ihdr;
13699 struct sctphdr *ohdr;
13700 struct sctp_chunkhdr *ophdr;
13702 struct udphdr *udp = NULL;
13707 struct sockaddr_in6 lsa6, fsa6;
13715 iph = mtod(m, struct ip *);
13716 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
13718 SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
13720 /* can't send because we can't add a mbuf */
13723 ohdr = mtod(scm, struct sctphdr *);
13724 ohdr->src_port = ihdr->dest_port;
13725 ohdr->dest_port = ihdr->src_port;
13726 ohdr->v_tag = vtag;
13727 ohdr->checksum = 0;
13728 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
13729 ophdr->chunk_type = SCTP_OPERATION_ERROR;
13730 ophdr->chunk_flags = 0;
13734 len += SCTP_BUF_LEN(at);
13735 at = SCTP_BUF_NEXT(at);
13737 ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
13740 uint32_t cpthis = 0;
13743 padlen = 4 - (len % 4);
13744 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
13747 val = sctp_calculate_sum(scm, NULL, 0);
13750 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
13752 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
13756 mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
13758 mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
13761 if (mout == NULL) {
13765 SCTP_BUF_NEXT(mout) = scm;
13766 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13767 sctp_m_freem(mout);
13770 ohdr->checksum = val;
13771 switch (iph->ip_v) {
13777 struct sctp_tcb *stcb = NULL;
13779 SCTP_BUF_LEN(mout) = sizeof(struct ip);
13780 len += sizeof(struct ip);
13782 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
13783 len += sizeof(struct udphdr);
13785 bzero(&ro, sizeof ro);
13786 out = mtod(mout, struct ip *);
13787 out->ip_v = iph->ip_v;
13788 out->ip_hl = (sizeof(struct ip) / 4);
13789 out->ip_tos = iph->ip_tos;
13790 out->ip_id = iph->ip_id;
13792 out->ip_ttl = MAXTTL;
13794 out->ip_p = IPPROTO_UDP;
13796 out->ip_p = IPPROTO_SCTP;
13799 out->ip_src = iph->ip_dst;
13800 out->ip_dst = iph->ip_src;
13803 udp = (struct udphdr *)(out + 1);
13804 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13805 udp->uh_dport = port;
13806 udp->uh_ulen = htons(len - sizeof(struct ip));
13807 udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13809 #ifdef SCTP_PACKET_LOGGING
13810 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13811 sctp_packet_log(mout, len);
13813 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13815 SCTP_ENABLE_UDP_CSUM(o_pak);
13817 SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
13819 SCTP_STAT_INCR(sctps_sendpackets);
13820 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13821 /* Free the route if we got one back */
13827 case IPV6_VERSION >> 4:
13830 struct route_in6 ro;
13832 struct sctp_tcb *stcb = NULL;
13833 struct ifnet *ifp = NULL;
13834 struct ip6_hdr *out6, *in6;
13836 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
13837 len += sizeof(struct ip6_hdr);
13838 bzero(&ro, sizeof ro);
13840 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
13841 len += sizeof(struct udphdr);
13843 in6 = mtod(m, struct ip6_hdr *);
13844 out6 = mtod(mout, struct ip6_hdr *);
13845 out6->ip6_flow = in6->ip6_flow;
13846 out6->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13848 out6->ip6_nxt = IPPROTO_UDP;
13850 out6->ip6_nxt = IPPROTO_SCTP;
13852 out6->ip6_src = in6->ip6_dst;
13853 out6->ip6_dst = in6->ip6_src;
13854 out6->ip6_plen = len - sizeof(struct ip6_hdr);
13856 udp = (struct udphdr *)(out6 + 1);
13857 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13858 udp->uh_dport = port;
13859 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13863 bzero(&lsa6, sizeof(lsa6));
13864 lsa6.sin6_len = sizeof(lsa6);
13865 lsa6.sin6_family = AF_INET6;
13866 lsa6.sin6_addr = out6->ip6_src;
13867 bzero(&fsa6, sizeof(fsa6));
13868 fsa6.sin6_len = sizeof(fsa6);
13869 fsa6.sin6_family = AF_INET6;
13870 fsa6.sin6_addr = out6->ip6_dst;
13872 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
13873 SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
13874 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
13875 SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
13876 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
13878 #ifdef SCTP_PACKET_LOGGING
13879 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13880 sctp_packet_log(mout, len);
13882 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13884 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13885 udp->uh_sum = 0xffff;
13888 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13890 SCTP_STAT_INCR(sctps_sendpackets);
13891 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13892 /* Free the route if we got one back */
13904 static struct mbuf *
13905 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
13907 struct sctp_sndrcvinfo *srcv,
13909 int user_marks_eor,
13912 struct mbuf **new_tail)
13916 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
13917 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
13919 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13922 *sndout = m_length(m, NULL);
13923 *new_tail = m_last(m);
13929 sctp_copy_one(struct sctp_stream_queue_pending *sp,
13936 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
13938 if (sp->data == NULL) {
13939 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13942 sp->tail_mbuf = m_last(sp->data);
13948 static struct sctp_stream_queue_pending *
13949 sctp_copy_it_in(struct sctp_tcb *stcb,
13950 struct sctp_association *asoc,
13951 struct sctp_sndrcvinfo *srcv,
13953 struct sctp_nets *net,
13955 int user_marks_eor,
13960 * This routine must be very careful in its work. Protocol
13961 * processing is up and running so care must be taken to spl...()
13962 * when you need to do something that may effect the stcb/asoc. The
13963 * sb is locked however. When data is copied the protocol processing
13964 * should be enabled since this is a slower operation...
13966 struct sctp_stream_queue_pending *sp = NULL;
13970 /* Now can we send this? */
13971 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13972 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13973 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13974 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13975 /* got data while shutting down */
13976 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13977 *error = ECONNRESET;
13980 sctp_alloc_a_strmoq(stcb, sp);
13982 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13987 sp->sender_all_done = 0;
13988 sp->sinfo_flags = srcv->sinfo_flags;
13989 sp->timetolive = srcv->sinfo_timetolive;
13990 sp->ppid = srcv->sinfo_ppid;
13991 sp->context = srcv->sinfo_context;
13993 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
13995 sp->stream = srcv->sinfo_stream;
13996 sp->length = min(uio->uio_resid, max_send_len);
13997 if ((sp->length == (uint32_t) uio->uio_resid) &&
13998 ((user_marks_eor == 0) ||
13999 (srcv->sinfo_flags & SCTP_EOF) ||
14000 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14001 sp->msg_is_complete = 1;
14003 sp->msg_is_complete = 0;
14005 sp->sender_all_done = 0;
14006 sp->some_taken = 0;
14007 sp->put_last_out = 0;
14008 resv_in_first = sizeof(struct sctp_data_chunk);
14009 sp->data = sp->tail_mbuf = NULL;
14010 if (sp->length == 0) {
14014 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
14015 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
14016 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
14017 sp->holds_key_ref = 1;
14019 *error = sctp_copy_one(sp, uio, resv_in_first);
14022 sctp_free_a_strmoq(stcb, sp);
14025 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
14029 sp->net = asoc->primary_destination;
14032 atomic_add_int(&sp->net->ref_count, 1);
14033 sctp_set_prsctp_policy(stcb, sp);
14041 sctp_sosend(struct socket *so,
14042 struct sockaddr *addr,
14045 struct mbuf *control,
14050 struct sctp_inpcb *inp;
14051 int error, use_rcvinfo = 0;
14052 struct sctp_sndrcvinfo srcv;
14053 struct sockaddr *addr_to_use;
14056 struct sockaddr_in sin;
14060 inp = (struct sctp_inpcb *)so->so_pcb;
14062 /* process cmsg snd/rcv info (maybe a assoc-id) */
14063 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
14069 addr_to_use = addr;
14071 if ((addr) && (addr->sa_family == AF_INET6)) {
14072 struct sockaddr_in6 *sin6;
14074 sin6 = (struct sockaddr_in6 *)addr;
14075 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
14076 in6_sin6_2_sin(&sin, sin6);
14077 addr_to_use = (struct sockaddr *)&sin;
14081 error = sctp_lower_sosend(so, addr_to_use, uio, top,
14092 sctp_lower_sosend(struct socket *so,
14093 struct sockaddr *addr,
14095 struct mbuf *i_pak,
14096 struct mbuf *control,
14099 struct sctp_sndrcvinfo *srcv
14104 unsigned int sndlen = 0, max_len;
14106 struct mbuf *top = NULL;
14108 int queue_only = 0, queue_only_for_init = 0;
14109 int free_cnt_applied = 0;
14111 int now_filled = 0;
14112 unsigned int inqueue_bytes = 0;
14113 struct sctp_block_entry be;
14114 struct sctp_inpcb *inp;
14115 struct sctp_tcb *stcb = NULL;
14116 struct timeval now;
14117 struct sctp_nets *net;
14118 struct sctp_association *asoc;
14119 struct sctp_inpcb *t_inp;
14120 int user_marks_eor;
14121 int create_lock_applied = 0;
14122 int nagle_applies = 0;
14123 int some_on_control = 0;
14124 int got_all_of_the_send = 0;
14125 int hold_tcblock = 0;
14126 int non_blocking = 0;
14127 int temp_flags = 0;
14128 uint32_t local_add_more, local_soresv = 0;
14135 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
14137 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14140 SCTP_RELEASE_PKT(i_pak);
14144 if ((uio == NULL) && (i_pak == NULL)) {
14145 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14148 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
14149 atomic_add_int(&inp->total_sends, 1);
14151 if (uio->uio_resid < 0) {
14152 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14155 sndlen = uio->uio_resid;
14157 top = SCTP_HEADER_TO_CHAIN(i_pak);
14158 sndlen = SCTP_HEADER_LEN(i_pak);
14160 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
14164 * Pre-screen address, if one is given the sin-len
14165 * must be set correctly!
14168 if ((addr->sa_family == AF_INET) &&
14169 (addr->sa_len != sizeof(struct sockaddr_in))) {
14170 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14173 } else if ((addr->sa_family == AF_INET6) &&
14174 (addr->sa_len != sizeof(struct sockaddr_in6))) {
14175 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14182 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
14183 (inp->sctp_socket->so_qlimit)) {
14184 /* The listener can NOT send */
14185 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14189 if ((use_rcvinfo) && srcv) {
14190 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
14191 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
14192 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14196 if (srcv->sinfo_flags)
14197 SCTP_STAT_INCR(sctps_sends_with_flags);
14199 if (srcv->sinfo_flags & SCTP_SENDALL) {
14200 /* its a sendall */
14201 error = sctp_sendall(inp, uio, top, srcv);
14206 /* now we must find the assoc */
14207 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
14208 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
14209 SCTP_INP_RLOCK(inp);
14210 stcb = LIST_FIRST(&inp->sctp_asoc_list);
14211 if (stcb == NULL) {
14212 SCTP_INP_RUNLOCK(inp);
14213 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14218 SCTP_INP_RUNLOCK(inp);
14220 /* Must locate the net structure if addr given */
14221 net = sctp_findnet(stcb, addr);
14223 /* validate port was 0 or correct */
14224 struct sockaddr_in *sin;
14226 sin = (struct sockaddr_in *)addr;
14227 if ((sin->sin_port != 0) &&
14228 (sin->sin_port != stcb->rport)) {
14232 temp_flags |= SCTP_ADDR_OVER;
14234 net = stcb->asoc.primary_destination;
14235 if (addr && (net == NULL)) {
14236 /* Could not find address, was it legal */
14237 if (addr->sa_family == AF_INET) {
14238 struct sockaddr_in *sin;
14240 sin = (struct sockaddr_in *)addr;
14241 if (sin->sin_addr.s_addr == 0) {
14242 if ((sin->sin_port == 0) ||
14243 (sin->sin_port == stcb->rport)) {
14244 net = stcb->asoc.primary_destination;
14248 struct sockaddr_in6 *sin6;
14250 sin6 = (struct sockaddr_in6 *)addr;
14251 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
14252 if ((sin6->sin6_port == 0) ||
14253 (sin6->sin6_port == stcb->rport)) {
14254 net = stcb->asoc.primary_destination;
14260 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14264 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
14265 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
14269 * Must locate the net structure if addr
14272 net = sctp_findnet(stcb, addr);
14274 net = stcb->asoc.primary_destination;
14275 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
14276 ((net == NULL) || (addr == NULL))) {
14277 struct sockaddr_in *sin;
14279 if (addr == NULL) {
14280 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14284 sin = (struct sockaddr_in *)addr;
14285 /* Validate port is 0 or correct */
14286 if ((sin->sin_port != 0) &&
14287 (sin->sin_port != stcb->rport)) {
14295 * Since we did not use findep we must
14296 * increment it, and if we don't find a tcb
14299 SCTP_INP_WLOCK(inp);
14300 SCTP_INP_INCR_REF(inp);
14301 SCTP_INP_WUNLOCK(inp);
14302 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14303 if (stcb == NULL) {
14304 SCTP_INP_WLOCK(inp);
14305 SCTP_INP_DECR_REF(inp);
14306 SCTP_INP_WUNLOCK(inp);
14311 if ((stcb == NULL) && (addr)) {
14312 /* Possible implicit send? */
14313 SCTP_ASOC_CREATE_LOCK(inp);
14314 create_lock_applied = 1;
14315 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
14316 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
14317 /* Should I really unlock ? */
14318 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14323 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
14324 (addr->sa_family == AF_INET6)) {
14325 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14329 SCTP_INP_WLOCK(inp);
14330 SCTP_INP_INCR_REF(inp);
14331 SCTP_INP_WUNLOCK(inp);
14332 /* With the lock applied look again */
14333 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14334 if (stcb == NULL) {
14335 SCTP_INP_WLOCK(inp);
14336 SCTP_INP_DECR_REF(inp);
14337 SCTP_INP_WUNLOCK(inp);
14341 if (t_inp != inp) {
14342 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14347 if (stcb == NULL) {
14348 if (addr == NULL) {
14349 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14354 * UDP style, we must go ahead and start the INIT
14359 if ((use_rcvinfo) && (srcv) &&
14360 ((srcv->sinfo_flags & SCTP_ABORT) ||
14361 ((srcv->sinfo_flags & SCTP_EOF) &&
14364 * User asks to abort a non-existant assoc,
14365 * or EOF a non-existant assoc with no data
14367 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14371 /* get an asoc/stcb struct */
14372 vrf_id = inp->def_vrf_id;
14374 if (create_lock_applied == 0) {
14375 panic("Error, should hold create lock and I don't?");
14378 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
14381 if (stcb == NULL) {
14382 /* Error is setup for us in the call */
14385 if (create_lock_applied) {
14386 SCTP_ASOC_CREATE_UNLOCK(inp);
14387 create_lock_applied = 0;
14389 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
14392 * Turn on queue only flag to prevent data from
14396 asoc = &stcb->asoc;
14397 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
14398 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
14400 /* initialize authentication params for the assoc */
14401 sctp_initialize_auth_params(inp, stcb);
14405 * see if a init structure exists in cmsg
14408 struct sctp_initmsg initm;
14411 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
14414 * we have an INIT override of the
14417 if (initm.sinit_max_attempts)
14418 asoc->max_init_times = initm.sinit_max_attempts;
14419 if (initm.sinit_num_ostreams)
14420 asoc->pre_open_streams = initm.sinit_num_ostreams;
14421 if (initm.sinit_max_instreams)
14422 asoc->max_inbound_streams = initm.sinit_max_instreams;
14423 if (initm.sinit_max_init_timeo)
14424 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
14425 if (asoc->streamoutcnt < asoc->pre_open_streams) {
14426 struct sctp_stream_out *tmp_str;
14429 /* Default is NOT correct */
14430 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
14431 asoc->streamoutcnt, asoc->pre_open_streams);
14433 * What happens if this
14434 * fails? we panic ...
14437 if (hold_tcblock) {
14439 SCTP_TCB_UNLOCK(stcb);
14441 SCTP_MALLOC(tmp_str,
14442 struct sctp_stream_out *,
14443 (asoc->pre_open_streams *
14444 sizeof(struct sctp_stream_out)),
14447 SCTP_TCB_LOCK(stcb);
14449 if (tmp_str != NULL) {
14450 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
14451 asoc->strmout = tmp_str;
14452 asoc->streamoutcnt = asoc->pre_open_streams;
14454 asoc->pre_open_streams = asoc->streamoutcnt;
14456 for (i = 0; i < asoc->streamoutcnt; i++) {
14458 * inbound side must be set
14459 * to 0xffff, also NOTE when
14460 * we get the INIT-ACK back
14461 * (for INIT sender) we MUST
14463 * (streamoutcnt) but first
14464 * check if we sent to any
14465 * of the upper streams that
14466 * were dropped (if some
14467 * were). Those that were
14468 * dropped must be notified
14469 * to the upper layer as
14472 asoc->strmout[i].next_sequence_sent = 0x0;
14473 TAILQ_INIT(&asoc->strmout[i].outqueue);
14474 asoc->strmout[i].stream_no = i;
14475 asoc->strmout[i].last_msg_incomplete = 0;
14476 asoc->strmout[i].next_spoke.tqe_next = 0;
14477 asoc->strmout[i].next_spoke.tqe_prev = 0;
14483 /* out with the INIT */
14484 queue_only_for_init = 1;
14486 * we may want to dig in after this call and adjust the MTU
14487 * value. It defaulted to 1500 (constant) but the ro
14488 * structure may now have an update and thus we may need to
14489 * change it BEFORE we append the message.
14491 net = stcb->asoc.primary_destination;
14492 asoc = &stcb->asoc;
14495 if ((SCTP_SO_IS_NBIO(so)
14496 || (flags & MSG_NBIO)
14500 asoc = &stcb->asoc;
14501 atomic_add_int(&stcb->total_sends, 1);
14503 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
14504 if (sndlen > asoc->smallest_mtu) {
14505 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14510 /* would we block? */
14511 if (non_blocking) {
14512 if (hold_tcblock == 0) {
14513 SCTP_TCB_LOCK(stcb);
14516 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14517 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
14518 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14519 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
14520 if (sndlen > SCTP_SB_LIMIT_SND(so))
14523 error = EWOULDBLOCK;
14526 stcb->asoc.sb_send_resv += sndlen;
14527 SCTP_TCB_UNLOCK(stcb);
14530 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
14532 local_soresv = sndlen;
14533 /* Keep the stcb from being freed under our feet */
14534 if (free_cnt_applied) {
14536 panic("refcnt already incremented");
14538 printf("refcnt:1 already incremented?\n");
14541 atomic_add_int(&stcb->asoc.refcnt, 1);
14542 free_cnt_applied = 1;
14544 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14545 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14546 error = ECONNRESET;
14549 if (create_lock_applied) {
14550 SCTP_ASOC_CREATE_UNLOCK(inp);
14551 create_lock_applied = 0;
14553 if (asoc->stream_reset_outstanding) {
14555 * Can't queue any data while stream reset is underway.
14557 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
14561 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14562 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14565 if ((use_rcvinfo == 0) || (srcv == NULL)) {
14566 /* Grab the default stuff from the asoc */
14567 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
14569 /* we are now done with all control */
14571 sctp_m_freem(control);
14574 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
14575 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14576 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14577 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14578 if ((use_rcvinfo) &&
14579 (srcv->sinfo_flags & SCTP_ABORT)) {
14582 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14583 error = ECONNRESET;
14587 /* Ok, we will attempt a msgsnd :> */
14589 p->td_ru.ru_msgsnd++;
14592 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
14593 net = stcb->asoc.primary_destination;
14597 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14601 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
14603 * CMT: Added check for CMT above. net above is the primary
14604 * dest. If CMT is ON, sender should always attempt to send
14605 * with the output routine sctp_fill_outqueue() that loops
14606 * through all destination addresses. Therefore, if CMT is
14607 * ON, queue_only is NOT set to 1 here, so that
14608 * sctp_chunk_output() can be called below.
14611 } else if (asoc->ifp_had_enobuf) {
14612 SCTP_STAT_INCR(sctps_ifnomemqueued);
14613 if (net->flight_size > (net->mtu * 2))
14615 asoc->ifp_had_enobuf = 0;
14617 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
14618 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
14620 /* Are we aborting? */
14621 if (srcv->sinfo_flags & SCTP_ABORT) {
14623 int tot_demand, tot_out = 0, max_out;
14625 SCTP_STAT_INCR(sctps_sends_with_abort);
14626 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14627 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14628 /* It has to be up before we abort */
14629 /* how big is the user initiated abort? */
14630 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14634 if (hold_tcblock) {
14635 SCTP_TCB_UNLOCK(stcb);
14639 struct mbuf *cntm = NULL;
14641 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
14645 tot_out += SCTP_BUF_LEN(cntm);
14646 cntm = SCTP_BUF_NEXT(cntm);
14649 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14651 /* Must fit in a MTU */
14653 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14654 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
14656 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14660 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
14663 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14667 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
14668 max_out -= sizeof(struct sctp_abort_msg);
14669 if (tot_out > max_out) {
14673 struct sctp_paramhdr *ph;
14675 /* now move forward the data pointer */
14676 ph = mtod(mm, struct sctp_paramhdr *);
14677 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
14678 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
14680 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
14682 error = uiomove((caddr_t)ph, (int)tot_out, uio);
14685 * Here if we can't get his data we
14686 * still abort we just don't get to
14687 * send the users note :-0
14694 SCTP_BUF_NEXT(mm) = top;
14698 if (hold_tcblock == 0) {
14699 SCTP_TCB_LOCK(stcb);
14702 atomic_add_int(&stcb->asoc.refcnt, -1);
14703 free_cnt_applied = 0;
14704 /* release this lock, otherwise we hang on ourselves */
14705 sctp_abort_an_association(stcb->sctp_ep, stcb,
14706 SCTP_RESPONSE_TO_USER_REQ,
14707 mm, SCTP_SO_LOCKED);
14708 /* now relock the stcb so everything is sane */
14712 * In this case top is already chained to mm avoid double
14713 * free, since we free it below if top != NULL and driver
14714 * would free it after sending the packet out
14721 /* Calculate the maximum we can send */
14722 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14723 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14724 if (non_blocking) {
14725 /* we already checked for non-blocking above. */
14728 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14733 if (hold_tcblock) {
14734 SCTP_TCB_UNLOCK(stcb);
14737 /* Is the stream no. valid? */
14738 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
14739 /* Invalid stream number */
14740 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14744 if (asoc->strmout == NULL) {
14745 /* huh? software error */
14746 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14750 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
14751 if ((user_marks_eor == 0) &&
14752 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
14753 /* It will NEVER fit */
14754 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14758 if ((uio == NULL) && user_marks_eor) {
14760 * We do not support eeor mode for
14761 * sending with mbuf chains (like sendfile).
14763 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14767 if (user_marks_eor) {
14768 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
14771 * For non-eeor the whole message must fit in
14772 * the socket send buffer.
14774 local_add_more = sndlen;
14777 if (non_blocking) {
14778 goto skip_preblock;
14780 if (((max_len <= local_add_more) &&
14781 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
14783 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14784 /* No room right now ! */
14785 SOCKBUF_LOCK(&so->so_snd);
14786 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14787 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
14788 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14789 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
14790 (unsigned int)SCTP_SB_LIMIT_SND(so),
14793 stcb->asoc.stream_queue_cnt,
14794 stcb->asoc.chunks_on_out_queue,
14795 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
14796 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14797 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
14800 stcb->block_entry = &be;
14801 error = sbwait(&so->so_snd);
14802 stcb->block_entry = NULL;
14803 if (error || so->so_error || be.error) {
14806 error = so->so_error;
14811 SOCKBUF_UNLOCK(&so->so_snd);
14814 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14815 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14816 so, asoc, stcb->asoc.total_output_queue_size);
14818 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14821 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14823 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14824 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14825 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14829 SOCKBUF_UNLOCK(&so->so_snd);
14832 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14836 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
14837 * case NOTE: uio will be null when top/mbuf is passed
14840 if (srcv->sinfo_flags & SCTP_EOF) {
14841 got_all_of_the_send = 1;
14844 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14850 struct sctp_stream_queue_pending *sp;
14851 struct sctp_stream_out *strm;
14852 uint32_t sndout, initial_out;
14854 initial_out = uio->uio_resid;
14856 SCTP_TCB_SEND_LOCK(stcb);
14857 if ((asoc->stream_locked) &&
14858 (asoc->stream_locked_on != srcv->sinfo_stream)) {
14859 SCTP_TCB_SEND_UNLOCK(stcb);
14860 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14864 SCTP_TCB_SEND_UNLOCK(stcb);
14866 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
14867 if (strm->last_msg_incomplete == 0) {
14869 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
14870 if ((sp == NULL) || (error)) {
14873 SCTP_TCB_SEND_LOCK(stcb);
14874 if (sp->msg_is_complete) {
14875 strm->last_msg_incomplete = 0;
14876 asoc->stream_locked = 0;
14879 * Just got locked to this guy in case of an
14882 strm->last_msg_incomplete = 1;
14883 asoc->stream_locked = 1;
14884 asoc->stream_locked_on = srcv->sinfo_stream;
14885 sp->sender_all_done = 0;
14887 sctp_snd_sb_alloc(stcb, sp->length);
14888 atomic_add_int(&asoc->stream_queue_cnt, 1);
14889 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
14890 sp->strseq = strm->next_sequence_sent;
14891 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
14892 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
14893 (uintptr_t) stcb, sp->length,
14894 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
14896 strm->next_sequence_sent++;
14898 SCTP_STAT_INCR(sctps_sends_with_unord);
14900 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
14901 if ((strm->next_spoke.tqe_next == NULL) &&
14902 (strm->next_spoke.tqe_prev == NULL)) {
14903 /* Not on wheel, insert */
14904 sctp_insert_on_wheel(stcb, asoc, strm, 1);
14906 SCTP_TCB_SEND_UNLOCK(stcb);
14908 SCTP_TCB_SEND_LOCK(stcb);
14909 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
14910 SCTP_TCB_SEND_UNLOCK(stcb);
14912 /* ???? Huh ??? last msg is gone */
14914 panic("Warning: Last msg marked incomplete, yet nothing left?");
14916 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
14917 strm->last_msg_incomplete = 0;
14923 while (uio->uio_resid > 0) {
14924 /* How much room do we have? */
14925 struct mbuf *new_tail, *mm;
14927 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
14928 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
14932 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
14933 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
14934 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
14937 if (hold_tcblock) {
14938 SCTP_TCB_UNLOCK(stcb);
14941 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
14942 if ((mm == NULL) || error) {
14948 /* Update the mbuf and count */
14949 SCTP_TCB_SEND_LOCK(stcb);
14950 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14952 * we need to get out. Peer probably
14956 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
14957 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14958 error = ECONNRESET;
14960 SCTP_TCB_SEND_UNLOCK(stcb);
14963 if (sp->tail_mbuf) {
14964 /* tack it to the end */
14965 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
14966 sp->tail_mbuf = new_tail;
14968 /* A stolen mbuf */
14970 sp->tail_mbuf = new_tail;
14972 sctp_snd_sb_alloc(stcb, sndout);
14973 atomic_add_int(&sp->length, sndout);
14976 /* Did we reach EOR? */
14977 if ((uio->uio_resid == 0) &&
14978 ((user_marks_eor == 0) ||
14979 (srcv->sinfo_flags & SCTP_EOF) ||
14980 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14981 sp->msg_is_complete = 1;
14983 sp->msg_is_complete = 0;
14985 SCTP_TCB_SEND_UNLOCK(stcb);
14987 if (uio->uio_resid == 0) {
14992 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
14994 * This is ugly but we must assure locking
14997 if (hold_tcblock == 0) {
14998 SCTP_TCB_LOCK(stcb);
15001 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
15002 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
15003 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
15004 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
15010 SCTP_TCB_UNLOCK(stcb);
15013 /* wait for space now */
15014 if (non_blocking) {
15015 /* Non-blocking io in place out */
15018 if ((net->flight_size > net->cwnd) &&
15019 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15021 } else if (asoc->ifp_had_enobuf) {
15022 SCTP_STAT_INCR(sctps_ifnomemqueued);
15023 if (net->flight_size > (net->mtu * 2)) {
15028 asoc->ifp_had_enobuf = 0;
15029 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15030 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15032 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15033 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15034 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
15036 SCTP_STAT_INCR(sctps_send_burst_avoid);
15037 } else if (net->flight_size > net->cwnd) {
15039 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15044 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15045 (stcb->asoc.total_flight > 0) &&
15046 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15047 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15050 * Ok, Nagle is set on and we have data outstanding.
15051 * Don't send anything and let SACKs drive out the
15052 * data unless wen have a "full" segment to send.
15054 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15055 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15057 SCTP_STAT_INCR(sctps_naglequeued);
15060 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15061 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15062 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15064 SCTP_STAT_INCR(sctps_naglesent);
15067 /* What about the INIT, send it maybe */
15068 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15070 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
15071 nagle_applies, un_sent);
15072 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
15073 stcb->asoc.total_flight,
15074 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
15076 if (queue_only_for_init) {
15077 if (hold_tcblock == 0) {
15078 SCTP_TCB_LOCK(stcb);
15081 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15082 /* a collision took us forward? */
15083 queue_only_for_init = 0;
15086 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15087 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
15088 queue_only_for_init = 0;
15092 if ((queue_only == 0) && (nagle_applies == 0)) {
15094 * need to start chunk output
15095 * before blocking.. note that if
15096 * a lock is already applied, then
15097 * the input via the net is happening
15098 * and I don't need to start output :-D
15100 if (hold_tcblock == 0) {
15101 if (SCTP_TCB_TRYLOCK(stcb)) {
15103 sctp_chunk_output(inp,
15105 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15108 sctp_chunk_output(inp,
15110 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15112 if (hold_tcblock == 1) {
15113 SCTP_TCB_UNLOCK(stcb);
15117 SOCKBUF_LOCK(&so->so_snd);
15119 * This is a bit strange, but I think it will
15120 * work. The total_output_queue_size is locked and
15121 * protected by the TCB_LOCK, which we just released.
15122 * There is a race that can occur between releasing it
15123 * above, and me getting the socket lock, where sacks
15124 * come in but we have not put the SB_WAIT on the
15125 * so_snd buffer to get the wakeup. After the LOCK
15126 * is applied the sack_processing will also need to
15127 * LOCK the so->so_snd to do the actual sowwakeup(). So
15128 * once we have the socket buffer lock if we recheck the
15129 * size we KNOW we will get to sleep safely with the
15130 * wakeup flag in place.
15132 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
15133 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
15134 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15135 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
15136 so, asoc, uio->uio_resid);
15139 stcb->block_entry = &be;
15140 error = sbwait(&so->so_snd);
15141 stcb->block_entry = NULL;
15143 if (error || so->so_error || be.error) {
15146 error = so->so_error;
15151 SOCKBUF_UNLOCK(&so->so_snd);
15154 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15155 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
15156 so, asoc, stcb->asoc.total_output_queue_size);
15159 SOCKBUF_UNLOCK(&so->so_snd);
15160 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
15164 SCTP_TCB_SEND_LOCK(stcb);
15166 if (sp->msg_is_complete == 0) {
15167 strm->last_msg_incomplete = 1;
15168 asoc->stream_locked = 1;
15169 asoc->stream_locked_on = srcv->sinfo_stream;
15171 sp->sender_all_done = 1;
15172 strm->last_msg_incomplete = 0;
15173 asoc->stream_locked = 0;
15176 SCTP_PRINTF("Huh no sp TSNH?\n");
15177 strm->last_msg_incomplete = 0;
15178 asoc->stream_locked = 0;
15180 SCTP_TCB_SEND_UNLOCK(stcb);
15181 if (uio->uio_resid == 0) {
15182 got_all_of_the_send = 1;
15185 /* We send in a 0, since we do NOT have any locks */
15186 error = sctp_msg_append(stcb, net, top, srcv, 0);
15188 if (srcv->sinfo_flags & SCTP_EOF) {
15190 * This should only happen for Panda for the mbuf
15191 * send case, which does NOT yet support EEOR mode.
15192 * Thus, we can just set this flag to do the proper
15195 got_all_of_the_send = 1;
15203 if ((srcv->sinfo_flags & SCTP_EOF) &&
15204 (got_all_of_the_send == 1) &&
15205 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
15208 SCTP_STAT_INCR(sctps_sends_with_eof);
15210 if (hold_tcblock == 0) {
15211 SCTP_TCB_LOCK(stcb);
15214 cnt = sctp_is_there_unsent_data(stcb);
15215 if (TAILQ_EMPTY(&asoc->send_queue) &&
15216 TAILQ_EMPTY(&asoc->sent_queue) &&
15218 if (asoc->locked_on_sending) {
15221 /* there is nothing queued to send, so I'm done... */
15222 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15223 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15224 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15225 /* only send SHUTDOWN the first time through */
15226 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
15227 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
15228 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
15230 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
15231 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
15232 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
15233 asoc->primary_destination);
15234 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15235 asoc->primary_destination);
15239 * we still got (or just got) data to send, so set
15243 * XXX sockets draft says that SCTP_EOF should be
15244 * sent with no data. currently, we will allow user
15245 * data to be sent first and move to
15248 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15249 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15250 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15251 if (hold_tcblock == 0) {
15252 SCTP_TCB_LOCK(stcb);
15255 if (asoc->locked_on_sending) {
15256 /* Locked to send out the data */
15257 struct sctp_stream_queue_pending *sp;
15259 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
15261 if ((sp->length == 0) && (sp->msg_is_complete == 0))
15262 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
15265 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
15266 if (TAILQ_EMPTY(&asoc->send_queue) &&
15267 TAILQ_EMPTY(&asoc->sent_queue) &&
15268 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
15270 if (free_cnt_applied) {
15271 atomic_add_int(&stcb->asoc.refcnt, -1);
15272 free_cnt_applied = 0;
15274 sctp_abort_an_association(stcb->sctp_ep, stcb,
15275 SCTP_RESPONSE_TO_USER_REQ,
15276 NULL, SCTP_SO_LOCKED);
15278 * now relock the stcb so everything
15285 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15286 asoc->primary_destination);
15287 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
15292 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
15293 some_on_control = 1;
15295 if ((net->flight_size > net->cwnd) &&
15296 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15298 } else if (asoc->ifp_had_enobuf) {
15299 SCTP_STAT_INCR(sctps_ifnomemqueued);
15300 if (net->flight_size > (net->mtu * 2)) {
15305 asoc->ifp_had_enobuf = 0;
15306 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15307 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15309 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15310 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15311 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
15313 SCTP_STAT_INCR(sctps_send_burst_avoid);
15314 } else if (net->flight_size > net->cwnd) {
15316 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15321 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15322 (stcb->asoc.total_flight > 0) &&
15323 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15324 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15326 * Ok, Nagle is set on and we have data outstanding.
15327 * Don't send anything and let SACKs drive out the
15328 * data unless wen have a "full" segment to send.
15330 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15331 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15333 SCTP_STAT_INCR(sctps_naglequeued);
15336 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15337 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15338 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15340 SCTP_STAT_INCR(sctps_naglesent);
15343 if (queue_only_for_init) {
15344 if (hold_tcblock == 0) {
15345 SCTP_TCB_LOCK(stcb);
15348 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15349 /* a collision took us forward? */
15350 queue_only_for_init = 0;
15353 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15354 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
15355 queue_only_for_init = 0;
15359 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
15360 /* we can attempt to send too. */
15361 if (hold_tcblock == 0) {
15363 * If there is activity recv'ing sacks no need to
15366 if (SCTP_TCB_TRYLOCK(stcb)) {
15367 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15371 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15373 } else if ((queue_only == 0) &&
15374 (stcb->asoc.peers_rwnd == 0) &&
15375 (stcb->asoc.total_flight == 0)) {
15376 /* We get to have a probe outstanding */
15377 if (hold_tcblock == 0) {
15379 SCTP_TCB_LOCK(stcb);
15381 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15382 } else if (some_on_control) {
15383 int num_out, reason, cwnd_full, frag_point;
15385 /* Here we do control only */
15386 if (hold_tcblock == 0) {
15388 SCTP_TCB_LOCK(stcb);
15390 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
15391 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
15392 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
15394 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
15395 queue_only, stcb->asoc.peers_rwnd, un_sent,
15396 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
15397 stcb->asoc.total_output_queue_size, error);
15402 if (local_soresv && stcb) {
15403 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
15406 if (create_lock_applied) {
15407 SCTP_ASOC_CREATE_UNLOCK(inp);
15408 create_lock_applied = 0;
15410 if ((stcb) && hold_tcblock) {
15411 SCTP_TCB_UNLOCK(stcb);
15413 if (stcb && free_cnt_applied) {
15414 atomic_add_int(&stcb->asoc.refcnt, -1);
15418 if (mtx_owned(&stcb->tcb_mtx)) {
15419 panic("Leaving with tcb mtx owned?");
15421 if (mtx_owned(&stcb->tcb_send_mtx)) {
15422 panic("Leaving with tcb send mtx owned?");
15430 sctp_m_freem(control);
15437 * generate an AUTHentication chunk, if required
15440 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
15441 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
15442 struct sctp_tcb *stcb, uint8_t chunk)
15444 struct mbuf *m_auth;
15445 struct sctp_auth_chunk *auth;
15448 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
15452 /* sysctl disabled auth? */
15453 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
15456 /* peer doesn't do auth... */
15457 if (!stcb->asoc.peer_supports_auth) {
15460 /* does the requested chunk require auth? */
15461 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
15464 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
15465 if (m_auth == NULL) {
15469 /* reserve some space if this will be the first mbuf */
15471 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
15472 /* fill in the AUTH chunk details */
15473 auth = mtod(m_auth, struct sctp_auth_chunk *);
15474 bzero(auth, sizeof(*auth));
15475 auth->ch.chunk_type = SCTP_AUTHENTICATION;
15476 auth->ch.chunk_flags = 0;
15477 chunk_len = sizeof(*auth) +
15478 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
15479 auth->ch.chunk_length = htons(chunk_len);
15480 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
15481 /* key id and hmac digest will be computed and filled in upon send */
15483 /* save the offset where the auth was inserted into the chain */
15490 *offset += SCTP_BUF_LEN(cn);
15491 cn = SCTP_BUF_NEXT(cn);
15496 /* update length and return pointer to the auth chunk */
15497 SCTP_BUF_LEN(m_auth) = chunk_len;
15498 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
15499 if (auth_ret != NULL)
15507 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
15509 struct nd_prefix *pfx = NULL;
15510 struct nd_pfxrouter *pfxrtr = NULL;
15511 struct sockaddr_in6 gw6;
15513 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
15516 /* get prefix entry of address */
15517 LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
15518 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
15520 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
15521 &src6->sin6_addr, &pfx->ndpr_mask))
15524 /* no prefix entry in the prefix list */
15526 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
15527 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15530 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
15531 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15533 /* search installed gateway from prefix entry */
15534 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
15535 pfxrtr->pfr_next) {
15536 memset(&gw6, 0, sizeof(struct sockaddr_in6));
15537 gw6.sin6_family = AF_INET6;
15538 gw6.sin6_len = sizeof(struct sockaddr_in6);
15539 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
15540 sizeof(struct in6_addr));
15541 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
15542 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
15543 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
15544 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15545 if (sctp_cmpaddr((struct sockaddr *)&gw6,
15546 ro->ro_rt->rt_gateway)) {
15547 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
15551 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
15558 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
15560 struct sockaddr_in *sin, *mask;
15561 struct ifaddr *ifa;
15562 struct in_addr srcnetaddr, gwnetaddr;
15564 if (ro == NULL || ro->ro_rt == NULL ||
15565 sifa->address.sa.sa_family != AF_INET) {
15568 ifa = (struct ifaddr *)sifa->ifa;
15569 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
15570 sin = (struct sockaddr_in *)&sifa->address.sin;
15571 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15572 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
15573 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
15574 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
15576 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
15577 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15578 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
15579 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15580 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
15581 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {