2 * Copyright (c) 2001-2007, 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>
55 #define SCTP_MAX_GAPS_INARRAY 4
57 uint8_t right_edge; /* mergable on the right edge */
58 uint8_t left_edge; /* mergable on the left edge */
61 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
64 struct sack_track sack_array[256] = {
65 {0, 0, 0, 0, /* 0x00 */
72 {1, 0, 1, 0, /* 0x01 */
79 {0, 0, 1, 0, /* 0x02 */
86 {1, 0, 1, 0, /* 0x03 */
93 {0, 0, 1, 0, /* 0x04 */
100 {1, 0, 2, 0, /* 0x05 */
107 {0, 0, 1, 0, /* 0x06 */
114 {1, 0, 1, 0, /* 0x07 */
121 {0, 0, 1, 0, /* 0x08 */
128 {1, 0, 2, 0, /* 0x09 */
135 {0, 0, 2, 0, /* 0x0a */
142 {1, 0, 2, 0, /* 0x0b */
149 {0, 0, 1, 0, /* 0x0c */
156 {1, 0, 2, 0, /* 0x0d */
163 {0, 0, 1, 0, /* 0x0e */
170 {1, 0, 1, 0, /* 0x0f */
177 {0, 0, 1, 0, /* 0x10 */
184 {1, 0, 2, 0, /* 0x11 */
191 {0, 0, 2, 0, /* 0x12 */
198 {1, 0, 2, 0, /* 0x13 */
205 {0, 0, 2, 0, /* 0x14 */
212 {1, 0, 3, 0, /* 0x15 */
219 {0, 0, 2, 0, /* 0x16 */
226 {1, 0, 2, 0, /* 0x17 */
233 {0, 0, 1, 0, /* 0x18 */
240 {1, 0, 2, 0, /* 0x19 */
247 {0, 0, 2, 0, /* 0x1a */
254 {1, 0, 2, 0, /* 0x1b */
261 {0, 0, 1, 0, /* 0x1c */
268 {1, 0, 2, 0, /* 0x1d */
275 {0, 0, 1, 0, /* 0x1e */
282 {1, 0, 1, 0, /* 0x1f */
289 {0, 0, 1, 0, /* 0x20 */
296 {1, 0, 2, 0, /* 0x21 */
303 {0, 0, 2, 0, /* 0x22 */
310 {1, 0, 2, 0, /* 0x23 */
317 {0, 0, 2, 0, /* 0x24 */
324 {1, 0, 3, 0, /* 0x25 */
331 {0, 0, 2, 0, /* 0x26 */
338 {1, 0, 2, 0, /* 0x27 */
345 {0, 0, 2, 0, /* 0x28 */
352 {1, 0, 3, 0, /* 0x29 */
359 {0, 0, 3, 0, /* 0x2a */
366 {1, 0, 3, 0, /* 0x2b */
373 {0, 0, 2, 0, /* 0x2c */
380 {1, 0, 3, 0, /* 0x2d */
387 {0, 0, 2, 0, /* 0x2e */
394 {1, 0, 2, 0, /* 0x2f */
401 {0, 0, 1, 0, /* 0x30 */
408 {1, 0, 2, 0, /* 0x31 */
415 {0, 0, 2, 0, /* 0x32 */
422 {1, 0, 2, 0, /* 0x33 */
429 {0, 0, 2, 0, /* 0x34 */
436 {1, 0, 3, 0, /* 0x35 */
443 {0, 0, 2, 0, /* 0x36 */
450 {1, 0, 2, 0, /* 0x37 */
457 {0, 0, 1, 0, /* 0x38 */
464 {1, 0, 2, 0, /* 0x39 */
471 {0, 0, 2, 0, /* 0x3a */
478 {1, 0, 2, 0, /* 0x3b */
485 {0, 0, 1, 0, /* 0x3c */
492 {1, 0, 2, 0, /* 0x3d */
499 {0, 0, 1, 0, /* 0x3e */
506 {1, 0, 1, 0, /* 0x3f */
513 {0, 0, 1, 0, /* 0x40 */
520 {1, 0, 2, 0, /* 0x41 */
527 {0, 0, 2, 0, /* 0x42 */
534 {1, 0, 2, 0, /* 0x43 */
541 {0, 0, 2, 0, /* 0x44 */
548 {1, 0, 3, 0, /* 0x45 */
555 {0, 0, 2, 0, /* 0x46 */
562 {1, 0, 2, 0, /* 0x47 */
569 {0, 0, 2, 0, /* 0x48 */
576 {1, 0, 3, 0, /* 0x49 */
583 {0, 0, 3, 0, /* 0x4a */
590 {1, 0, 3, 0, /* 0x4b */
597 {0, 0, 2, 0, /* 0x4c */
604 {1, 0, 3, 0, /* 0x4d */
611 {0, 0, 2, 0, /* 0x4e */
618 {1, 0, 2, 0, /* 0x4f */
625 {0, 0, 2, 0, /* 0x50 */
632 {1, 0, 3, 0, /* 0x51 */
639 {0, 0, 3, 0, /* 0x52 */
646 {1, 0, 3, 0, /* 0x53 */
653 {0, 0, 3, 0, /* 0x54 */
660 {1, 0, 4, 0, /* 0x55 */
667 {0, 0, 3, 0, /* 0x56 */
674 {1, 0, 3, 0, /* 0x57 */
681 {0, 0, 2, 0, /* 0x58 */
688 {1, 0, 3, 0, /* 0x59 */
695 {0, 0, 3, 0, /* 0x5a */
702 {1, 0, 3, 0, /* 0x5b */
709 {0, 0, 2, 0, /* 0x5c */
716 {1, 0, 3, 0, /* 0x5d */
723 {0, 0, 2, 0, /* 0x5e */
730 {1, 0, 2, 0, /* 0x5f */
737 {0, 0, 1, 0, /* 0x60 */
744 {1, 0, 2, 0, /* 0x61 */
751 {0, 0, 2, 0, /* 0x62 */
758 {1, 0, 2, 0, /* 0x63 */
765 {0, 0, 2, 0, /* 0x64 */
772 {1, 0, 3, 0, /* 0x65 */
779 {0, 0, 2, 0, /* 0x66 */
786 {1, 0, 2, 0, /* 0x67 */
793 {0, 0, 2, 0, /* 0x68 */
800 {1, 0, 3, 0, /* 0x69 */
807 {0, 0, 3, 0, /* 0x6a */
814 {1, 0, 3, 0, /* 0x6b */
821 {0, 0, 2, 0, /* 0x6c */
828 {1, 0, 3, 0, /* 0x6d */
835 {0, 0, 2, 0, /* 0x6e */
842 {1, 0, 2, 0, /* 0x6f */
849 {0, 0, 1, 0, /* 0x70 */
856 {1, 0, 2, 0, /* 0x71 */
863 {0, 0, 2, 0, /* 0x72 */
870 {1, 0, 2, 0, /* 0x73 */
877 {0, 0, 2, 0, /* 0x74 */
884 {1, 0, 3, 0, /* 0x75 */
891 {0, 0, 2, 0, /* 0x76 */
898 {1, 0, 2, 0, /* 0x77 */
905 {0, 0, 1, 0, /* 0x78 */
912 {1, 0, 2, 0, /* 0x79 */
919 {0, 0, 2, 0, /* 0x7a */
926 {1, 0, 2, 0, /* 0x7b */
933 {0, 0, 1, 0, /* 0x7c */
940 {1, 0, 2, 0, /* 0x7d */
947 {0, 0, 1, 0, /* 0x7e */
954 {1, 0, 1, 0, /* 0x7f */
961 {0, 1, 1, 0, /* 0x80 */
968 {1, 1, 2, 0, /* 0x81 */
975 {0, 1, 2, 0, /* 0x82 */
982 {1, 1, 2, 0, /* 0x83 */
989 {0, 1, 2, 0, /* 0x84 */
996 {1, 1, 3, 0, /* 0x85 */
1003 {0, 1, 2, 0, /* 0x86 */
1010 {1, 1, 2, 0, /* 0x87 */
1017 {0, 1, 2, 0, /* 0x88 */
1024 {1, 1, 3, 0, /* 0x89 */
1031 {0, 1, 3, 0, /* 0x8a */
1038 {1, 1, 3, 0, /* 0x8b */
1045 {0, 1, 2, 0, /* 0x8c */
1052 {1, 1, 3, 0, /* 0x8d */
1059 {0, 1, 2, 0, /* 0x8e */
1066 {1, 1, 2, 0, /* 0x8f */
1073 {0, 1, 2, 0, /* 0x90 */
1080 {1, 1, 3, 0, /* 0x91 */
1087 {0, 1, 3, 0, /* 0x92 */
1094 {1, 1, 3, 0, /* 0x93 */
1101 {0, 1, 3, 0, /* 0x94 */
1108 {1, 1, 4, 0, /* 0x95 */
1115 {0, 1, 3, 0, /* 0x96 */
1122 {1, 1, 3, 0, /* 0x97 */
1129 {0, 1, 2, 0, /* 0x98 */
1136 {1, 1, 3, 0, /* 0x99 */
1143 {0, 1, 3, 0, /* 0x9a */
1150 {1, 1, 3, 0, /* 0x9b */
1157 {0, 1, 2, 0, /* 0x9c */
1164 {1, 1, 3, 0, /* 0x9d */
1171 {0, 1, 2, 0, /* 0x9e */
1178 {1, 1, 2, 0, /* 0x9f */
1185 {0, 1, 2, 0, /* 0xa0 */
1192 {1, 1, 3, 0, /* 0xa1 */
1199 {0, 1, 3, 0, /* 0xa2 */
1206 {1, 1, 3, 0, /* 0xa3 */
1213 {0, 1, 3, 0, /* 0xa4 */
1220 {1, 1, 4, 0, /* 0xa5 */
1227 {0, 1, 3, 0, /* 0xa6 */
1234 {1, 1, 3, 0, /* 0xa7 */
1241 {0, 1, 3, 0, /* 0xa8 */
1248 {1, 1, 4, 0, /* 0xa9 */
1255 {0, 1, 4, 0, /* 0xaa */
1262 {1, 1, 4, 0, /* 0xab */
1269 {0, 1, 3, 0, /* 0xac */
1276 {1, 1, 4, 0, /* 0xad */
1283 {0, 1, 3, 0, /* 0xae */
1290 {1, 1, 3, 0, /* 0xaf */
1297 {0, 1, 2, 0, /* 0xb0 */
1304 {1, 1, 3, 0, /* 0xb1 */
1311 {0, 1, 3, 0, /* 0xb2 */
1318 {1, 1, 3, 0, /* 0xb3 */
1325 {0, 1, 3, 0, /* 0xb4 */
1332 {1, 1, 4, 0, /* 0xb5 */
1339 {0, 1, 3, 0, /* 0xb6 */
1346 {1, 1, 3, 0, /* 0xb7 */
1353 {0, 1, 2, 0, /* 0xb8 */
1360 {1, 1, 3, 0, /* 0xb9 */
1367 {0, 1, 3, 0, /* 0xba */
1374 {1, 1, 3, 0, /* 0xbb */
1381 {0, 1, 2, 0, /* 0xbc */
1388 {1, 1, 3, 0, /* 0xbd */
1395 {0, 1, 2, 0, /* 0xbe */
1402 {1, 1, 2, 0, /* 0xbf */
1409 {0, 1, 1, 0, /* 0xc0 */
1416 {1, 1, 2, 0, /* 0xc1 */
1423 {0, 1, 2, 0, /* 0xc2 */
1430 {1, 1, 2, 0, /* 0xc3 */
1437 {0, 1, 2, 0, /* 0xc4 */
1444 {1, 1, 3, 0, /* 0xc5 */
1451 {0, 1, 2, 0, /* 0xc6 */
1458 {1, 1, 2, 0, /* 0xc7 */
1465 {0, 1, 2, 0, /* 0xc8 */
1472 {1, 1, 3, 0, /* 0xc9 */
1479 {0, 1, 3, 0, /* 0xca */
1486 {1, 1, 3, 0, /* 0xcb */
1493 {0, 1, 2, 0, /* 0xcc */
1500 {1, 1, 3, 0, /* 0xcd */
1507 {0, 1, 2, 0, /* 0xce */
1514 {1, 1, 2, 0, /* 0xcf */
1521 {0, 1, 2, 0, /* 0xd0 */
1528 {1, 1, 3, 0, /* 0xd1 */
1535 {0, 1, 3, 0, /* 0xd2 */
1542 {1, 1, 3, 0, /* 0xd3 */
1549 {0, 1, 3, 0, /* 0xd4 */
1556 {1, 1, 4, 0, /* 0xd5 */
1563 {0, 1, 3, 0, /* 0xd6 */
1570 {1, 1, 3, 0, /* 0xd7 */
1577 {0, 1, 2, 0, /* 0xd8 */
1584 {1, 1, 3, 0, /* 0xd9 */
1591 {0, 1, 3, 0, /* 0xda */
1598 {1, 1, 3, 0, /* 0xdb */
1605 {0, 1, 2, 0, /* 0xdc */
1612 {1, 1, 3, 0, /* 0xdd */
1619 {0, 1, 2, 0, /* 0xde */
1626 {1, 1, 2, 0, /* 0xdf */
1633 {0, 1, 1, 0, /* 0xe0 */
1640 {1, 1, 2, 0, /* 0xe1 */
1647 {0, 1, 2, 0, /* 0xe2 */
1654 {1, 1, 2, 0, /* 0xe3 */
1661 {0, 1, 2, 0, /* 0xe4 */
1668 {1, 1, 3, 0, /* 0xe5 */
1675 {0, 1, 2, 0, /* 0xe6 */
1682 {1, 1, 2, 0, /* 0xe7 */
1689 {0, 1, 2, 0, /* 0xe8 */
1696 {1, 1, 3, 0, /* 0xe9 */
1703 {0, 1, 3, 0, /* 0xea */
1710 {1, 1, 3, 0, /* 0xeb */
1717 {0, 1, 2, 0, /* 0xec */
1724 {1, 1, 3, 0, /* 0xed */
1731 {0, 1, 2, 0, /* 0xee */
1738 {1, 1, 2, 0, /* 0xef */
1745 {0, 1, 1, 0, /* 0xf0 */
1752 {1, 1, 2, 0, /* 0xf1 */
1759 {0, 1, 2, 0, /* 0xf2 */
1766 {1, 1, 2, 0, /* 0xf3 */
1773 {0, 1, 2, 0, /* 0xf4 */
1780 {1, 1, 3, 0, /* 0xf5 */
1787 {0, 1, 2, 0, /* 0xf6 */
1794 {1, 1, 2, 0, /* 0xf7 */
1801 {0, 1, 1, 0, /* 0xf8 */
1808 {1, 1, 2, 0, /* 0xf9 */
1815 {0, 1, 2, 0, /* 0xfa */
1822 {1, 1, 2, 0, /* 0xfb */
1829 {0, 1, 1, 0, /* 0xfc */
1836 {1, 1, 2, 0, /* 0xfd */
1843 {0, 1, 1, 0, /* 0xfe */
1850 {1, 1, 1, 0, /* 0xff */
1861 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1862 int ipv4_addr_legal,
1863 int ipv6_addr_legal,
1865 int ipv4_local_scope,
1870 if ((loopback_scope == 0) &&
1871 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1873 * skip loopback if not in scope *
1877 switch (ifa->address.sa.sa_family) {
1879 if (ipv4_addr_legal) {
1880 struct sockaddr_in *sin;
1882 sin = (struct sockaddr_in *)&ifa->address.sin;
1883 if (sin->sin_addr.s_addr == 0) {
1884 /* not in scope , unspecified */
1887 if ((ipv4_local_scope == 0) &&
1888 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1889 /* private address not in scope */
1898 if (ipv6_addr_legal) {
1899 struct sockaddr_in6 *sin6;
1902 * Must update the flags, bummer, which means any
1903 * IFA locks must now be applied HERE <->
1906 sctp_gather_internal_ifa_flags(ifa);
1908 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1911 /* ok to use deprecated addresses? */
1912 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1913 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1914 /* skip unspecifed addresses */
1917 if ( /* (local_scope == 0) && */
1918 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1921 if ((site_scope == 0) &&
1922 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1936 static struct mbuf *
1937 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1939 struct sctp_paramhdr *parmh;
1943 if (ifa->address.sa.sa_family == AF_INET) {
1944 len = sizeof(struct sctp_ipv4addr_param);
1945 } else if (ifa->address.sa.sa_family == AF_INET6) {
1946 len = sizeof(struct sctp_ipv6addr_param);
1951 if (M_TRAILINGSPACE(m) >= len) {
1952 /* easy side we just drop it on the end */
1953 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1956 /* Need more space */
1958 while (SCTP_BUF_NEXT(mret) != NULL) {
1959 mret = SCTP_BUF_NEXT(mret);
1961 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1962 if (SCTP_BUF_NEXT(mret) == NULL) {
1963 /* We are hosed, can't add more addresses */
1966 mret = SCTP_BUF_NEXT(mret);
1967 parmh = mtod(mret, struct sctp_paramhdr *);
1969 /* now add the parameter */
1970 switch (ifa->address.sa.sa_family) {
1973 struct sctp_ipv4addr_param *ipv4p;
1974 struct sockaddr_in *sin;
1976 sin = (struct sockaddr_in *)&ifa->address.sin;
1977 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1978 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1979 parmh->param_length = htons(len);
1980 ipv4p->addr = sin->sin_addr.s_addr;
1981 SCTP_BUF_LEN(mret) += len;
1987 struct sctp_ipv6addr_param *ipv6p;
1988 struct sockaddr_in6 *sin6;
1990 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1991 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1992 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1993 parmh->param_length = htons(len);
1994 memcpy(ipv6p->addr, &sin6->sin6_addr,
1995 sizeof(ipv6p->addr));
1996 /* clear embedded scope in the address */
1997 in6_clearscope((struct in6_addr *)ipv6p->addr);
1998 SCTP_BUF_LEN(mret) += len;
2010 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2011 struct mbuf *m_at, int cnt_inits_to)
2013 struct sctp_vrf *vrf = NULL;
2014 int cnt, limit_out = 0, total_count;
2017 vrf_id = inp->def_vrf_id;
2018 SCTP_IPI_ADDR_RLOCK();
2019 vrf = sctp_find_vrf(vrf_id);
2021 SCTP_IPI_ADDR_RUNLOCK();
2024 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2025 struct sctp_ifa *sctp_ifap;
2026 struct sctp_ifn *sctp_ifnp;
2029 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2031 cnt = SCTP_ADDRESS_LIMIT;
2034 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2035 if ((scope->loopback_scope == 0) &&
2036 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2038 * Skip loopback devices if loopback_scope
2043 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2044 if (sctp_is_address_in_scope(sctp_ifap,
2045 scope->ipv4_addr_legal,
2046 scope->ipv6_addr_legal,
2047 scope->loopback_scope,
2048 scope->ipv4_local_scope,
2050 scope->site_scope, 1) == 0) {
2054 if (cnt > SCTP_ADDRESS_LIMIT) {
2058 if (cnt > SCTP_ADDRESS_LIMIT) {
2065 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2067 if ((scope->loopback_scope == 0) &&
2068 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2070 * Skip loopback devices if
2071 * loopback_scope not set
2075 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2076 if (sctp_is_address_in_scope(sctp_ifap,
2077 scope->ipv4_addr_legal,
2078 scope->ipv6_addr_legal,
2079 scope->loopback_scope,
2080 scope->ipv4_local_scope,
2082 scope->site_scope, 0) == 0) {
2085 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2096 if (total_count > SCTP_ADDRESS_LIMIT) {
2097 /* No more addresses */
2105 struct sctp_laddr *laddr;
2108 /* First, how many ? */
2109 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2110 if (laddr->ifa == NULL) {
2113 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2115 * Address being deleted by the system, dont
2119 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2121 * Address being deleted on this ep don't
2126 if (sctp_is_address_in_scope(laddr->ifa,
2127 scope->ipv4_addr_legal,
2128 scope->ipv6_addr_legal,
2129 scope->loopback_scope,
2130 scope->ipv4_local_scope,
2132 scope->site_scope, 1) == 0) {
2137 if (cnt > SCTP_ADDRESS_LIMIT) {
2141 * To get through a NAT we only list addresses if we have
2142 * more than one. That way if you just bind a single address
2143 * we let the source of the init dictate our address.
2146 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2148 if (laddr->ifa == NULL) {
2151 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2154 if (sctp_is_address_in_scope(laddr->ifa,
2155 scope->ipv4_addr_legal,
2156 scope->ipv6_addr_legal,
2157 scope->loopback_scope,
2158 scope->ipv4_local_scope,
2160 scope->site_scope, 0) == 0) {
2163 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2165 if (cnt >= SCTP_ADDRESS_LIMIT) {
2171 SCTP_IPI_ADDR_RUNLOCK();
2175 static struct sctp_ifa *
2176 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2177 uint8_t dest_is_loop,
2178 uint8_t dest_is_priv,
2181 uint8_t dest_is_global = 0;
2183 /* dest_is_priv is true if destination is a private address */
2184 /* dest_is_loop is true if destination is a loopback addresses */
2187 * Here we determine if its a preferred address. A preferred address
2188 * means it is the same scope or higher scope then the destination.
2189 * L = loopback, P = private, G = global
2190 * ----------------------------------------- src | dest | result
2191 * ---------------------------------------- L | L | yes
2192 * ----------------------------------------- P | L |
2193 * yes-v4 no-v6 ----------------------------------------- G |
2194 * L | yes-v4 no-v6 ----------------------------------------- L
2195 * | P | no ----------------------------------------- P |
2196 * P | yes ----------------------------------------- G |
2197 * P | no ----------------------------------------- L | G
2198 * | no ----------------------------------------- P | G |
2199 * no ----------------------------------------- G | G |
2200 * yes -----------------------------------------
2203 if (ifa->address.sa.sa_family != fam) {
2204 /* forget mis-matched family */
2207 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2210 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2211 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2212 /* Ok the address may be ok */
2213 if (fam == AF_INET6) {
2214 /* ok to use deprecated addresses? */
2215 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2216 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2219 if (ifa->src_is_priv) {
2221 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2225 if (ifa->src_is_glob) {
2227 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2233 * Now that we know what is what, implement or table this could in
2234 * theory be done slicker (it used to be), but this is
2235 * straightforward and easier to validate :-)
2237 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2238 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2239 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2240 dest_is_loop, dest_is_priv, dest_is_global);
2242 if ((ifa->src_is_loop) && (dest_is_priv)) {
2243 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2246 if ((ifa->src_is_glob) && (dest_is_priv)) {
2247 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2250 if ((ifa->src_is_loop) && (dest_is_global)) {
2251 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2254 if ((ifa->src_is_priv) && (dest_is_global)) {
2255 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2258 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2259 /* its a preferred address */
2263 static struct sctp_ifa *
2264 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2265 uint8_t dest_is_loop,
2266 uint8_t dest_is_priv,
2269 uint8_t dest_is_global = 0;
2273 * Here we determine if its a acceptable address. A acceptable
2274 * address means it is the same scope or higher scope but we can
2275 * allow for NAT which means its ok to have a global dest and a
2278 * L = loopback, P = private, G = global
2279 * ----------------------------------------- src | dest | result
2280 * ----------------------------------------- L | L | yes
2281 * ----------------------------------------- P | L |
2282 * yes-v4 no-v6 ----------------------------------------- G |
2283 * L | yes ----------------------------------------- L |
2284 * P | no ----------------------------------------- P | P
2285 * | yes ----------------------------------------- G | P
2286 * | yes - May not work -----------------------------------------
2287 * L | G | no ----------------------------------------- P
2288 * | G | yes - May not work
2289 * ----------------------------------------- G | G | yes
2290 * -----------------------------------------
2293 if (ifa->address.sa.sa_family != fam) {
2294 /* forget non matching family */
2297 /* Ok the address may be ok */
2298 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2301 if (fam == AF_INET6) {
2302 /* ok to use deprecated addresses? */
2303 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2306 if (ifa->src_is_priv) {
2307 /* Special case, linklocal to loop */
2313 * Now that we know what is what, implement our table. This could in
2314 * theory be done slicker (it used to be), but this is
2315 * straightforward and easier to validate :-)
2317 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2320 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2323 /* its an acceptable address */
2328 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2330 struct sctp_laddr *laddr;
2333 /* There are no restrictions, no TCB :-) */
2336 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2337 if (laddr->ifa == NULL) {
2338 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2342 if (laddr->ifa == ifa) {
2343 /* Yes it is on the list */
2352 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2354 struct sctp_laddr *laddr;
2358 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2359 if (laddr->ifa == NULL) {
2360 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2364 if ((laddr->ifa == ifa) && laddr->action == 0)
2373 static struct sctp_ifa *
2374 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2377 int non_asoc_addr_ok,
2378 uint8_t dest_is_priv,
2379 uint8_t dest_is_loop,
2382 struct sctp_laddr *laddr, *starting_point;
2385 struct sctp_ifn *sctp_ifn;
2386 struct sctp_ifa *sctp_ifa, *sifa;
2387 struct sctp_vrf *vrf;
2390 vrf = sctp_find_vrf(vrf_id);
2394 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2395 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2396 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2398 * first question, is the ifn we will emit on in our list, if so, we
2399 * want such an address. Note that we first looked for a preferred
2403 /* is a preferred one on the interface we route out? */
2404 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2405 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2406 (non_asoc_addr_ok == 0))
2408 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2413 if (sctp_is_addr_in_ep(inp, sifa)) {
2414 atomic_add_int(&sifa->refcount, 1);
2420 * ok, now we now need to find one on the list of the addresses. We
2421 * can't get one on the emitting interface so let's find first a
2422 * preferred one. If not that an acceptable one otherwise... we
2425 starting_point = inp->next_addr_touse;
2427 if (inp->next_addr_touse == NULL) {
2428 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2431 for (laddr = inp->next_addr_touse; laddr;
2432 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2433 if (laddr->ifa == NULL) {
2434 /* address has been removed */
2437 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2438 /* address is being deleted */
2441 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2445 atomic_add_int(&sifa->refcount, 1);
2448 if (resettotop == 0) {
2449 inp->next_addr_touse = NULL;
2452 inp->next_addr_touse = starting_point;
2455 if (inp->next_addr_touse == NULL) {
2456 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2459 /* ok, what about an acceptable address in the inp */
2460 for (laddr = inp->next_addr_touse; laddr;
2461 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2462 if (laddr->ifa == NULL) {
2463 /* address has been removed */
2466 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2467 /* address is being deleted */
2470 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2474 atomic_add_int(&sifa->refcount, 1);
2477 if (resettotop == 0) {
2478 inp->next_addr_touse = NULL;
2479 goto once_again_too;
2482 * no address bound can be a source for the destination we are in
2490 static struct sctp_ifa *
2491 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2492 struct sctp_tcb *stcb,
2493 struct sctp_nets *net,
2496 uint8_t dest_is_priv,
2497 uint8_t dest_is_loop,
2498 int non_asoc_addr_ok,
2501 struct sctp_laddr *laddr, *starting_point;
2503 struct sctp_ifn *sctp_ifn;
2504 struct sctp_ifa *sctp_ifa, *sifa;
2505 uint8_t start_at_beginning = 0;
2506 struct sctp_vrf *vrf;
2510 * first question, is the ifn we will emit on in our list, if so, we
2513 vrf = sctp_find_vrf(vrf_id);
2517 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2518 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2519 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2522 * first question, is the ifn we will emit on in our list? If so,
2523 * we want that one. First we look for a preferred. Second, we go
2524 * for an acceptable.
2527 /* first try for a preferred address on the ep */
2528 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2529 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2531 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2532 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2535 if ((non_asoc_addr_ok == 0) &&
2536 (sctp_is_addr_restricted(stcb, sifa))) {
2537 /* on the no-no list */
2540 atomic_add_int(&sifa->refcount, 1);
2544 /* next try for an acceptable address on the ep */
2545 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2546 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2548 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2549 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2552 if ((non_asoc_addr_ok == 0) &&
2553 (sctp_is_addr_restricted(stcb, sifa))) {
2554 /* on the no-no list */
2557 atomic_add_int(&sifa->refcount, 1);
2564 * if we can't find one like that then we must look at all addresses
2565 * bound to pick one at first preferable then secondly acceptable.
2567 starting_point = stcb->asoc.last_used_address;
2569 if (stcb->asoc.last_used_address == NULL) {
2570 start_at_beginning = 1;
2571 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2573 /* search beginning with the last used address */
2574 for (laddr = stcb->asoc.last_used_address; laddr;
2575 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2576 if (laddr->ifa == NULL) {
2577 /* address has been removed */
2580 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2581 /* address is being deleted */
2584 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2587 if ((non_asoc_addr_ok == 0) &&
2588 (sctp_is_addr_restricted(stcb, sifa))) {
2589 /* on the no-no list */
2592 stcb->asoc.last_used_address = laddr;
2593 atomic_add_int(&sifa->refcount, 1);
2596 if (start_at_beginning == 0) {
2597 stcb->asoc.last_used_address = NULL;
2598 goto sctp_from_the_top;
2600 /* now try for any higher scope than the destination */
2601 stcb->asoc.last_used_address = starting_point;
2602 start_at_beginning = 0;
2604 if (stcb->asoc.last_used_address == NULL) {
2605 start_at_beginning = 1;
2606 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2608 /* search beginning with the last used address */
2609 for (laddr = stcb->asoc.last_used_address; laddr;
2610 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2611 if (laddr->ifa == NULL) {
2612 /* address has been removed */
2615 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2616 /* address is being deleted */
2619 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2623 if ((non_asoc_addr_ok == 0) &&
2624 (sctp_is_addr_restricted(stcb, sifa))) {
2625 /* on the no-no list */
2628 stcb->asoc.last_used_address = laddr;
2629 atomic_add_int(&sifa->refcount, 1);
2632 if (start_at_beginning == 0) {
2633 stcb->asoc.last_used_address = NULL;
2634 goto sctp_from_the_top2;
2639 static struct sctp_ifa *
2640 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2641 struct sctp_tcb *stcb,
2642 int non_asoc_addr_ok,
2643 uint8_t dest_is_loop,
2644 uint8_t dest_is_priv,
2650 struct sctp_ifa *ifa, *sifa;
2651 int num_eligible_addr = 0;
2653 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2654 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2655 (non_asoc_addr_ok == 0))
2657 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2662 * Check if the IPv6 address matches to next-hop. In the
2663 * mobile case, old IPv6 address may be not deleted from the
2664 * interface. Then, the interface has previous and new
2665 * addresses. We should use one corresponding to the
2666 * next-hop. (by micchie)
2669 if (stcb && fam == AF_INET6 &&
2670 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2671 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2677 /* Avoid topologically incorrect IPv4 address */
2678 if (stcb && fam == AF_INET &&
2679 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2680 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2685 if ((non_asoc_addr_ok == 0) &&
2686 sctp_is_addr_restricted(stcb, sifa)) {
2688 * It is restricted for some reason..
2689 * probably not yet added.
2694 if (num_eligible_addr >= addr_wanted) {
2697 num_eligible_addr++;
2704 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2705 struct sctp_tcb *stcb,
2706 int non_asoc_addr_ok,
2707 uint8_t dest_is_loop,
2708 uint8_t dest_is_priv,
2711 struct sctp_ifa *ifa, *sifa;
2712 int num_eligible_addr = 0;
2714 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2715 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2716 (non_asoc_addr_ok == 0)) {
2719 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2725 if ((non_asoc_addr_ok == 0) &&
2726 sctp_is_addr_restricted(stcb, sifa)) {
2728 * It is restricted for some reason..
2729 * probably not yet added.
2734 num_eligible_addr++;
2736 return (num_eligible_addr);
2739 static struct sctp_ifa *
2740 sctp_choose_boundall(struct sctp_inpcb *inp,
2741 struct sctp_tcb *stcb,
2742 struct sctp_nets *net,
2745 uint8_t dest_is_priv,
2746 uint8_t dest_is_loop,
2747 int non_asoc_addr_ok,
2750 int cur_addr_num = 0, num_preferred = 0;
2752 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2753 struct sctp_ifa *sctp_ifa, *sifa;
2755 struct sctp_vrf *vrf;
2758 * For boundall we can use any address in the association.
2759 * If non_asoc_addr_ok is set we can use any address (at least in
2760 * theory). So we look for preferred addresses first. If we find one,
2761 * we use it. Otherwise we next try to get an address on the
2762 * interface, which we should be able to do (unless non_asoc_addr_ok
2763 * is false and we are routed out that way). In these cases where we
2764 * can't use the address of the interface we go through all the
2765 * ifn's looking for an address we can use and fill that in. Punting
2766 * means we send back address 0, which will probably cause problems
2767 * actually since then IP will fill in the address of the route ifn,
2768 * which means we probably already rejected it.. i.e. here comes an
2771 vrf = sctp_find_vrf(vrf_id);
2775 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2776 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2777 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2778 if (sctp_ifn == NULL) {
2779 /* ?? We don't have this guy ?? */
2780 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2781 goto bound_all_plan_b;
2783 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2784 ifn_index, sctp_ifn->ifn_name);
2787 cur_addr_num = net->indx_of_eligible_next_to_use;
2789 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2794 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2795 num_preferred, sctp_ifn->ifn_name);
2796 if (num_preferred == 0) {
2798 * no eligible addresses, we must use some other interface
2799 * address if we can find one.
2801 goto bound_all_plan_b;
2804 * Ok we have num_eligible_addr set with how many we can use, this
2805 * may vary from call to call due to addresses being deprecated
2808 if (cur_addr_num >= num_preferred) {
2812 * select the nth address from the list (where cur_addr_num is the
2813 * nth) and 0 is the first one, 1 is the second one etc...
2815 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2817 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2818 dest_is_priv, cur_addr_num, fam, ro);
2820 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2822 atomic_add_int(&sctp_ifa->refcount, 1);
2824 /* save off where the next one we will want */
2825 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2830 * plan_b: Look at all interfaces and find a preferred address. If
2831 * no preferred fall through to plan_c.
2834 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2835 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2836 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2837 sctp_ifn->ifn_name);
2838 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2839 /* wrong base scope */
2840 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2843 if ((sctp_ifn == looked_at) && looked_at) {
2844 /* already looked at this guy */
2845 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2848 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2849 dest_is_loop, dest_is_priv, fam);
2850 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2851 "Found ifn:%p %d preferred source addresses\n",
2852 ifn, num_preferred);
2853 if (num_preferred == 0) {
2854 /* None on this interface. */
2855 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2858 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2859 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2860 num_preferred, sctp_ifn, cur_addr_num);
2863 * Ok we have num_eligible_addr set with how many we can
2864 * use, this may vary from call to call due to addresses
2865 * being deprecated etc..
2867 if (cur_addr_num >= num_preferred) {
2870 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2871 dest_is_priv, cur_addr_num, fam, ro);
2875 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2876 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2878 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2879 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2880 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2881 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2883 atomic_add_int(&sifa->refcount, 1);
2888 /* plan_c: do we have an acceptable address on the emit interface */
2889 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2890 if (emit_ifn == NULL) {
2893 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2894 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2895 (non_asoc_addr_ok == 0))
2897 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2902 if ((non_asoc_addr_ok == 0) &&
2903 sctp_is_addr_restricted(stcb, sifa)) {
2905 * It is restricted for some reason..
2906 * probably not yet added.
2911 atomic_add_int(&sifa->refcount, 1);
2916 * plan_d: We are in trouble. No preferred address on the emit
2917 * interface. And not even a preferred address on all interfaces. Go
2918 * out and see if we can find an acceptable address somewhere
2919 * amongst all interfaces.
2921 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2922 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2923 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2924 /* wrong base scope */
2927 if ((sctp_ifn == looked_at) && looked_at)
2928 /* already looked at this guy */
2931 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2932 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2933 (non_asoc_addr_ok == 0))
2935 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2941 if ((non_asoc_addr_ok == 0) &&
2942 sctp_is_addr_restricted(stcb, sifa)) {
2944 * It is restricted for some
2945 * reason.. probably not yet added.
2950 atomic_add_int(&sifa->refcount, 1);
2955 * Ok we can find NO address to source from that is not on our
2956 * restricted list and non_asoc_address is NOT ok, or it is on our
2957 * restricted list. We can't source to it :-(
2964 /* tcb may be NULL */
2966 sctp_source_address_selection(struct sctp_inpcb *inp,
2967 struct sctp_tcb *stcb,
2969 struct sctp_nets *net,
2970 int non_asoc_addr_ok, uint32_t vrf_id)
2972 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
2975 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
2978 struct sctp_ifa *answer;
2979 uint8_t dest_is_priv, dest_is_loop;
2983 * Rules: - Find the route if needed, cache if I can. - Look at
2984 * interface address in route, Is it in the bound list. If so we
2985 * have the best source. - If not we must rotate amongst the
2990 * Do we need to pay attention to scope. We can have a private address
2991 * or a global address we are sourcing or sending to. So if we draw
2993 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2995 *------------------------------------------
2996 * source * dest * result
2997 * -----------------------------------------
2998 * <a> Private * Global * NAT
2999 * -----------------------------------------
3000 * <b> Private * Private * No problem
3001 * -----------------------------------------
3002 * <c> Global * Private * Huh, How will this work?
3003 * -----------------------------------------
3004 * <d> Global * Global * No Problem
3005 *------------------------------------------
3006 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3008 *------------------------------------------
3009 * source * dest * result
3010 * -----------------------------------------
3011 * <a> Linklocal * Global *
3012 * -----------------------------------------
3013 * <b> Linklocal * Linklocal * No problem
3014 * -----------------------------------------
3015 * <c> Global * Linklocal * Huh, How will this work?
3016 * -----------------------------------------
3017 * <d> Global * Global * No Problem
3018 *------------------------------------------
3019 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3021 * And then we add to that what happens if there are multiple addresses
3022 * assigned to an interface. Remember the ifa on a ifn is a linked
3023 * list of addresses. So one interface can have more than one IP
3024 * address. What happens if we have both a private and a global
3025 * address? Do we then use context of destination to sort out which
3026 * one is best? And what about NAT's sending P->G may get you a NAT
3027 * translation, or should you select the G thats on the interface in
3032 * - count the number of addresses on the interface.
3033 * - if it is one, no problem except case <c>.
3034 * For <a> we will assume a NAT out there.
3035 * - if there are more than one, then we need to worry about scope P
3036 * or G. We should prefer G -> G and P -> P if possible.
3037 * Then as a secondary fall back to mixed types G->P being a last
3039 * - The above all works for bound all, but bound specific we need to
3040 * use the same concept but instead only consider the bound
3041 * addresses. If the bound set is NOT assigned to the interface then
3042 * we must use rotation amongst the bound addresses..
3044 if (ro->ro_rt == NULL) {
3046 * Need a route to cache.
3048 SCTP_RTALLOC(ro, vrf_id);
3050 if (ro->ro_rt == NULL) {
3053 fam = to->sin_family;
3054 dest_is_priv = dest_is_loop = 0;
3055 /* Setup our scopes for the destination */
3058 /* Scope based on outbound address */
3059 if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3061 } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3064 /* mark it as local */
3065 net->addr_is_local = 1;
3071 /* Scope based on outbound address */
3072 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3074 * If the route goes to the loopback address OR the
3075 * address is a loopback address, we are loopback
3076 * scope. But we don't use dest_is_priv (link local
3081 /* mark it as local */
3082 net->addr_is_local = 1;
3084 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3090 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3091 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3092 SCTP_IPI_ADDR_RLOCK();
3093 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3097 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3098 dest_is_priv, dest_is_loop,
3099 non_asoc_addr_ok, fam);
3100 SCTP_IPI_ADDR_RUNLOCK();
3107 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3108 vrf_id, dest_is_priv,
3110 non_asoc_addr_ok, fam);
3112 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3117 SCTP_IPI_ADDR_RUNLOCK();
3122 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3127 tlen = SCTP_BUF_LEN(control);
3130 * Independent of how many mbufs, find the c_type inside the control
3131 * structure and copy out the data.
3134 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3135 /* not enough room for one more we are done. */
3138 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3139 if (((int)cmh.cmsg_len + at) > tlen) {
3141 * this is real messed up since there is not enough
3142 * data here to cover the cmsg header. We are done.
3146 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3147 (c_type == cmh.cmsg_type)) {
3148 /* found the one we want, copy it out */
3149 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3150 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3152 * space of cmsg_len after header not big
3157 m_copydata(control, at, cpsize, data);
3160 at += CMSG_ALIGN(cmh.cmsg_len);
3161 if (cmh.cmsg_len == 0) {
3170 static struct mbuf *
3171 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3172 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3174 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3175 struct sctp_state_cookie *stc;
3176 struct sctp_paramhdr *ph;
3182 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3183 sizeof(struct sctp_paramhdr)), 0,
3184 M_DONTWAIT, 1, MT_DATA);
3188 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3189 if (copy_init == NULL) {
3193 #ifdef SCTP_MBUF_LOGGING
3194 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
3199 if (SCTP_BUF_IS_EXTENDED(mat)) {
3200 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3202 mat = SCTP_BUF_NEXT(mat);
3206 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3208 if (copy_initack == NULL) {
3210 sctp_m_freem(copy_init);
3213 #ifdef SCTP_MBUF_LOGGING
3214 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
3219 if (SCTP_BUF_IS_EXTENDED(mat)) {
3220 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3222 mat = SCTP_BUF_NEXT(mat);
3226 /* easy side we just drop it on the end */
3227 ph = mtod(mret, struct sctp_paramhdr *);
3228 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3229 sizeof(struct sctp_paramhdr);
3230 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3231 sizeof(struct sctp_paramhdr));
3232 ph->param_type = htons(SCTP_STATE_COOKIE);
3233 ph->param_length = 0; /* fill in at the end */
3234 /* Fill in the stc cookie data */
3235 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3237 /* tack the INIT and then the INIT-ACK onto the chain */
3240 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3241 cookie_sz += SCTP_BUF_LEN(m_at);
3242 if (SCTP_BUF_NEXT(m_at) == NULL) {
3243 SCTP_BUF_NEXT(m_at) = copy_init;
3248 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3249 cookie_sz += SCTP_BUF_LEN(m_at);
3250 if (SCTP_BUF_NEXT(m_at) == NULL) {
3251 SCTP_BUF_NEXT(m_at) = copy_initack;
3256 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3257 cookie_sz += SCTP_BUF_LEN(m_at);
3258 if (SCTP_BUF_NEXT(m_at) == NULL) {
3262 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3264 /* no space, so free the entire chain */
3268 SCTP_BUF_LEN(sig) = 0;
3269 SCTP_BUF_NEXT(m_at) = sig;
3271 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3272 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3274 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3275 cookie_sz += SCTP_SIGNATURE_SIZE;
3276 ph->param_length = htons(cookie_sz);
3282 sctp_get_ect(struct sctp_tcb *stcb,
3283 struct sctp_tmit_chunk *chk)
3285 uint8_t this_random;
3288 if (sctp_ecn_enable == 0)
3291 if (sctp_ecn_nonce == 0)
3292 /* no nonce, always return ECT0 */
3293 return (SCTP_ECT0_BIT);
3295 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3296 /* Peer does NOT support it, so we send a ECT0 only */
3297 return (SCTP_ECT0_BIT);
3300 return (SCTP_ECT0_BIT);
3302 if ((stcb->asoc.hb_random_idx > 3) ||
3303 ((stcb->asoc.hb_random_idx == 3) &&
3304 (stcb->asoc.hb_ect_randombit > 7))) {
3308 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3309 memcpy(stcb->asoc.hb_random_values, &rndval,
3310 sizeof(stcb->asoc.hb_random_values));
3311 this_random = stcb->asoc.hb_random_values[0];
3312 stcb->asoc.hb_random_idx = 0;
3313 stcb->asoc.hb_ect_randombit = 0;
3315 if (stcb->asoc.hb_ect_randombit > 7) {
3316 stcb->asoc.hb_ect_randombit = 0;
3317 stcb->asoc.hb_random_idx++;
3318 if (stcb->asoc.hb_random_idx > 3) {
3322 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3324 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3326 /* ECN Nonce stuff */
3327 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3328 stcb->asoc.hb_ect_randombit++;
3329 return (SCTP_ECT1_BIT);
3331 stcb->asoc.hb_ect_randombit++;
3332 return (SCTP_ECT0_BIT);
3337 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3338 struct sctp_tcb *stcb, /* may be NULL */
3339 struct sctp_nets *net,
3340 struct sockaddr *to,
3342 uint32_t auth_offset,
3343 struct sctp_auth_chunk *auth,
3344 int nofragment_flag,
3346 struct sctp_tmit_chunk *chk,
3349 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3353 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3356 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3357 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3358 * structure: - fill in the HMAC digest of any AUTH chunk in the
3359 * packet. - calculate and fill in the SCTP checksum. - prepend an
3360 * IP address header. - if boundall use INADDR_ANY. - if
3361 * boundspecific do source address selection. - set fragmentation
3362 * option for ipV4. - On return from IP output, check/adjust mtu
3363 * size of output interface and smallest_mtu size as well.
3365 /* Will need ifdefs around this */
3368 struct sctphdr *sctphdr;
3373 sctp_route_t *ro = NULL;
3375 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3376 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3381 vrf_id = stcb->asoc.vrf_id;
3383 vrf_id = inp->def_vrf_id;
3386 /* fill in the HMAC digest for any AUTH chunk in the packet */
3387 if ((auth != NULL) && (stcb != NULL)) {
3388 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3390 /* Calculate the csum and fill in the length of the packet */
3391 sctphdr = mtod(m, struct sctphdr *);
3392 if (sctp_no_csum_on_loopback &&
3394 (to->sa_family == AF_INET) &&
3395 (stcb->asoc.loopback_scope)) {
3396 sctphdr->checksum = 0;
3398 * This can probably now be taken out since my audit shows
3399 * no more bad pktlen's coming in. But we will wait a while
3402 packet_length = sctp_calculate_len(m);
3404 sctphdr->checksum = 0;
3405 csum = sctp_calculate_sum(m, &packet_length, 0);
3406 sctphdr->checksum = csum;
3409 if (to->sa_family == AF_INET) {
3410 struct ip *ip = NULL;
3411 sctp_route_t iproute;
3414 newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3417 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3420 SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3421 SCTP_BUF_LEN(newm) = sizeof(struct ip);
3422 packet_length += sizeof(struct ip);
3423 SCTP_BUF_NEXT(newm) = m;
3425 ip = mtod(m, struct ip *);
3426 ip->ip_v = IPVERSION;
3427 ip->ip_hl = (sizeof(struct ip) >> 2);
3429 tos_value = net->tos_flowlabel & 0x000000ff;
3431 tos_value = inp->ip_inp.inp.inp_ip_tos;
3433 if (nofragment_flag) {
3434 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__)
3437 ip->ip_off = htons(IP_DF);
3442 /* FreeBSD has a function for ip_id's */
3443 ip->ip_id = ip_newid();
3445 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3446 ip->ip_len = packet_length;
3448 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3450 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3453 ip->ip_tos = (u_char)(tos_value & 0xfc);
3456 /* no association at all */
3457 ip->ip_tos = (tos_value & 0xfc);
3459 ip->ip_p = IPPROTO_SCTP;
3463 memset(&iproute, 0, sizeof(iproute));
3464 memcpy(&ro->ro_dst, to, to->sa_len);
3466 ro = (sctp_route_t *) & net->ro;
3468 /* Now the address selection part */
3469 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3471 /* call the routine to select the src address */
3473 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3474 sctp_free_ifa(net->ro._s_addr);
3475 net->ro._s_addr = NULL;
3476 net->src_addr_selected = 0;
3482 if (net->src_addr_selected == 0) {
3483 if (out_of_asoc_ok) {
3487 /* Cache the source address */
3488 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3489 ro, net, out_of_asoc_ok,
3491 net->src_addr_selected = 1;
3493 if (net->ro._s_addr == NULL) {
3494 /* No route to host */
3495 net->src_addr_selected = 0;
3498 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3500 struct sctp_ifa *_lsrc;
3503 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3507 if (_lsrc == NULL) {
3510 ip->ip_src = _lsrc->address.sin.sin_addr;
3511 sctp_free_ifa(_lsrc);
3515 * If source address selection fails and we find no route
3516 * then the ip_output should fail as well with a
3517 * NO_ROUTE_TO_HOST type error. We probably should catch
3518 * that somewhere and abort the association right away
3519 * (assuming this is an INIT being sent).
3521 if ((ro->ro_rt == NULL)) {
3523 * src addr selection failed to find a route (or
3524 * valid source addr), so we can't get there from
3528 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3529 "%s: dropped packet - no valid source addr\n",
3532 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3533 "Destination was ");
3534 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3535 &net->ro._l_addr.sa);
3536 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3537 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3538 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3539 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3541 SCTP_FAILED_THRESHOLD,
3544 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3545 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3547 * JRS 5/14/07 - If a
3549 * unreachable, the PF bit
3550 * is turned off. This
3551 * allows an unambiguous use
3553 * destinations that are
3554 * reachable but potentially
3556 * destination is set to the
3557 * unreachable state, also
3558 * set the destination to
3562 * Add debug message here if
3563 * destination is not in PF
3567 * Stop any running T3
3570 if (sctp_cmt_on_off && sctp_cmt_pf) {
3571 net->dest_state &= ~SCTP_ADDR_PF;
3572 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3578 if (net == stcb->asoc.primary_destination) {
3579 /* need a new primary */
3580 struct sctp_nets *alt;
3582 alt = sctp_find_alternate_net(stcb, net, 0);
3584 if (sctp_set_primary_addr(stcb,
3585 (struct sockaddr *)NULL,
3587 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3588 if (net->ro._s_addr) {
3589 sctp_free_ifa(net->ro._s_addr);
3590 net->ro._s_addr = NULL;
3592 net->src_addr_selected = 0;
3598 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3600 return (EHOSTUNREACH);
3602 if (ro != &iproute) {
3603 memcpy(&iproute, ro, sizeof(*ro));
3605 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3606 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3607 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3608 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3609 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3612 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3613 /* failed to prepend data, give up */
3614 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3618 #ifdef SCTP_PACKET_LOGGING
3619 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3620 sctp_packet_log(m, packet_length);
3622 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3624 /* send it out. table id is taken from stcb */
3625 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3627 SCTP_STAT_INCR(sctps_sendpackets);
3628 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3630 SCTP_STAT_INCR(sctps_senderrors);
3632 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3634 /* free tempy routes */
3640 /* PMTU check versus smallest asoc MTU goes here */
3641 if ((ro->ro_rt != NULL) &&
3642 (net->ro._s_addr)) {
3645 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3647 (stcb->asoc.smallest_mtu > mtu)) {
3648 #ifdef SCTP_PRINT_FOR_B_AND_M
3649 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3652 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3655 } else if (ro->ro_rt == NULL) {
3656 /* route was freed */
3657 if (net->ro._s_addr &&
3658 net->src_addr_selected) {
3659 sctp_free_ifa(net->ro._s_addr);
3660 net->ro._s_addr = NULL;
3662 net->src_addr_selected = 0;
3668 else if (to->sa_family == AF_INET6) {
3670 struct ip6_hdr *ip6h;
3671 struct route_in6 ip6route;
3674 uint16_t flowBottom;
3675 u_char tosBottom, tosTop;
3676 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3678 struct sockaddr_in6 lsa6_storage;
3680 u_short prev_port = 0;
3683 flowlabel = net->tos_flowlabel;
3685 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3688 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3691 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3694 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3695 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3696 packet_length += sizeof(struct ip6_hdr);
3697 SCTP_BUF_NEXT(newm) = m;
3700 ip6h = mtod(m, struct ip6_hdr *);
3702 * We assume here that inp_flow is in host byte order within
3705 flowBottom = flowlabel & 0x0000ffff;
3706 flowTop = ((flowlabel & 0x000f0000) >> 16);
3707 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3708 /* protect *sin6 from overwrite */
3709 sin6 = (struct sockaddr_in6 *)to;
3713 /* KAME hack: embed scopeid */
3714 if (sa6_embedscope(sin6, ip6_use_defzone) != 0) {
3715 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3719 memset(&ip6route, 0, sizeof(ip6route));
3720 ro = (sctp_route_t *) & ip6route;
3721 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3723 ro = (sctp_route_t *) & net->ro;
3726 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3728 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3731 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3734 /* we could get no asoc if it is a O-O-T-B packet */
3735 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3737 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3738 ip6h->ip6_nxt = IPPROTO_SCTP;
3739 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3740 ip6h->ip6_dst = sin6->sin6_addr;
3743 * Add SRC address selection here: we can only reuse to a
3744 * limited degree the kame src-addr-sel, since we can try
3745 * their selection but it may not be bound.
3747 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3748 lsa6_tmp.sin6_family = AF_INET6;
3749 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3752 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3753 sctp_free_ifa(net->ro._s_addr);
3754 net->ro._s_addr = NULL;
3755 net->src_addr_selected = 0;
3761 if (net->src_addr_selected == 0) {
3762 if (out_of_asoc_ok) {
3766 /* Cache the source address */
3767 net->ro._s_addr = sctp_source_address_selection(inp,
3773 net->src_addr_selected = 1;
3775 if (net->ro._s_addr == NULL) {
3776 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3777 net->src_addr_selected = 0;
3780 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3782 struct sctp_ifa *_lsrc;
3785 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3789 if (_lsrc == NULL) {
3792 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3793 sctp_free_ifa(_lsrc);
3795 lsa6->sin6_port = inp->sctp_lport;
3797 if ((ro->ro_rt == NULL)) {
3799 * src addr selection failed to find a route (or
3800 * valid source addr), so we can't get there from
3806 * XXX: sa6 may not have a valid sin6_scope_id in the
3807 * non-SCOPEDROUTING case.
3809 bzero(&lsa6_storage, sizeof(lsa6_storage));
3810 lsa6_storage.sin6_family = AF_INET6;
3811 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3812 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3813 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3818 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3819 lsa6_storage.sin6_port = inp->sctp_lport;
3820 lsa6 = &lsa6_storage;
3821 ip6h->ip6_src = lsa6->sin6_addr;
3824 * We set the hop limit now since there is a good chance
3825 * that our ro pointer is now filled
3827 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3828 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3831 /* Copy to be sure something bad is not happening */
3832 sin6->sin6_addr = ip6h->ip6_dst;
3833 lsa6->sin6_addr = ip6h->ip6_src;
3836 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3837 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3838 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3839 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3840 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3842 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3843 /* preserve the port and scope for link local send */
3844 prev_scope = sin6->sin6_scope_id;
3845 prev_port = sin6->sin6_port;
3847 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3848 /* failed to prepend data, give up */
3850 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3853 #ifdef SCTP_PACKET_LOGGING
3854 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3855 sctp_packet_log(m, packet_length);
3857 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3859 /* send it out. table id is taken from stcb */
3860 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp,
3864 /* for link local this must be done */
3865 sin6->sin6_scope_id = prev_scope;
3866 sin6->sin6_port = prev_port;
3868 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
3869 SCTP_STAT_INCR(sctps_sendpackets);
3870 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3872 SCTP_STAT_INCR(sctps_senderrors);
3875 /* Now if we had a temp route free it */
3880 /* PMTU check versus smallest asoc MTU goes here */
3881 if (ro->ro_rt == NULL) {
3882 /* Route was freed */
3883 if (net->ro._s_addr &&
3884 net->src_addr_selected) {
3885 sctp_free_ifa(net->ro._s_addr);
3886 net->ro._s_addr = NULL;
3888 net->src_addr_selected = 0;
3890 if ((ro->ro_rt != NULL) &&
3891 (net->ro._s_addr)) {
3894 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3896 (stcb->asoc.smallest_mtu > mtu)) {
3897 #ifdef SCTP_PRINT_FOR_B_AND_M
3898 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
3901 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3905 if (ND_IFINFO(ifp)->linkmtu &&
3906 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
3907 #ifdef SCTP_PRINT_FOR_B_AND_M
3908 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
3909 ND_IFINFO(ifp)->linkmtu);
3911 sctp_mtu_size_reset(inp,
3913 ND_IFINFO(ifp)->linkmtu);
3921 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
3922 ((struct sockaddr *)to)->sa_family);
3924 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3931 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
3932 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3937 struct mbuf *m, *m_at, *mp_last;
3938 struct sctp_nets *net;
3939 struct sctp_init_msg *initm;
3940 struct sctp_supported_addr_param *sup_addr;
3941 struct sctp_ecn_supported_param *ecn;
3942 struct sctp_prsctp_supported_param *prsctp;
3943 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3944 struct sctp_supported_chunk_types_param *pr_supported;
3945 int cnt_inits_to = 0;
3950 /* INIT's always go to the primary (and usually ONLY address) */
3952 net = stcb->asoc.primary_destination;
3954 net = TAILQ_FIRST(&stcb->asoc.nets);
3959 /* we confirm any address we send an INIT to */
3960 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3961 (void)sctp_set_primary_addr(stcb, NULL, net);
3963 /* we confirm any address we send an INIT to */
3964 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3966 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
3968 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
3970 * special hook, if we are sending to link local it will not
3971 * show up in our private address count.
3973 struct sockaddr_in6 *sin6l;
3975 sin6l = &net->ro._l_addr.sin6;
3976 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
3980 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3981 /* This case should not happen */
3982 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
3985 /* start the INIT timer */
3986 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
3988 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3990 /* No memory, INIT timer will re-attempt. */
3991 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
3994 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
3996 * assume peer supports asconf in order to be able to queue local
3997 * address changes while an INIT is in flight and before the assoc
4000 stcb->asoc.peer_supports_asconf = 1;
4001 /* Now lets put the SCTP header in place */
4002 initm = mtod(m, struct sctp_init_msg *);
4003 initm->sh.src_port = inp->sctp_lport;
4004 initm->sh.dest_port = stcb->rport;
4005 initm->sh.v_tag = 0;
4006 initm->sh.checksum = 0; /* calculate later */
4007 /* now the chunk header */
4008 initm->msg.ch.chunk_type = SCTP_INITIATION;
4009 initm->msg.ch.chunk_flags = 0;
4010 /* fill in later from mbuf we build */
4011 initm->msg.ch.chunk_length = 0;
4012 /* place in my tag */
4013 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
4014 /* set up some of the credits. */
4015 initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
4016 SCTP_MINIMAL_RWND));
4018 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4019 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4020 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4021 /* now the address restriction */
4022 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
4024 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4026 /* we support 2 types: IPv6/IPv4 */
4027 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4028 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4029 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4031 /* we support 1 type: IPv4 */
4032 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4033 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4034 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4036 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4038 if (inp->sctp_ep.adaptation_layer_indicator) {
4039 struct sctp_adaptation_layer_indication *ali;
4041 ali = (struct sctp_adaptation_layer_indication *)(
4042 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4043 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4044 ali->ph.param_length = htons(sizeof(*ali));
4045 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4046 SCTP_BUF_LEN(m) += sizeof(*ali);
4047 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
4050 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
4051 sizeof(*sup_addr) + sizeof(uint16_t));
4054 /* now any cookie time extensions */
4055 if (stcb->asoc.cookie_preserve_req) {
4056 struct sctp_cookie_perserve_param *cookie_preserve;
4058 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4059 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4060 cookie_preserve->ph.param_length = htons(
4061 sizeof(*cookie_preserve));
4062 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4063 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4064 ecn = (struct sctp_ecn_supported_param *)(
4065 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4066 stcb->asoc.cookie_preserve_req = 0;
4069 if (sctp_ecn_enable == 1) {
4070 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4071 ecn->ph.param_length = htons(sizeof(*ecn));
4072 SCTP_BUF_LEN(m) += sizeof(*ecn);
4073 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4076 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4078 /* And now tell the peer we do pr-sctp */
4079 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4080 prsctp->ph.param_length = htons(sizeof(*prsctp));
4081 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4083 /* And now tell the peer we do all the extensions */
4084 pr_supported = (struct sctp_supported_chunk_types_param *)
4085 ((caddr_t)prsctp + sizeof(*prsctp));
4086 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4088 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4089 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4090 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4091 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4092 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4093 if (!sctp_auth_disable)
4094 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4095 p_len = sizeof(*pr_supported) + num_ext;
4096 pr_supported->ph.param_length = htons(p_len);
4097 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4098 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4100 /* ECN nonce: And now tell the peer we support ECN nonce */
4101 if (sctp_ecn_nonce) {
4102 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4103 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4104 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4105 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4106 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4108 /* add authentication parameters */
4109 if (!sctp_auth_disable) {
4110 struct sctp_auth_random *randp;
4111 struct sctp_auth_hmac_algo *hmacs;
4112 struct sctp_auth_chunk_list *chunks;
4114 /* attach RANDOM parameter, if available */
4115 if (stcb->asoc.authinfo.random != NULL) {
4116 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4117 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4118 #ifdef SCTP_AUTH_DRAFT_04
4119 randp->ph.param_type = htons(SCTP_RANDOM);
4120 randp->ph.param_length = htons(p_len);
4121 bcopy(stcb->asoc.authinfo.random->key,
4123 stcb->asoc.authinfo.random_len);
4125 /* random key already contains the header */
4126 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4128 /* zero out any padding required */
4129 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4130 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4132 /* add HMAC_ALGO parameter */
4133 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4134 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4135 (uint8_t *) hmacs->hmac_ids);
4137 p_len += sizeof(*hmacs);
4138 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4139 hmacs->ph.param_length = htons(p_len);
4140 /* zero out any padding required */
4141 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4142 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4144 /* add CHUNKS parameter */
4145 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4146 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4147 chunks->chunk_types);
4149 p_len += sizeof(*chunks);
4150 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4151 chunks->ph.param_length = htons(p_len);
4152 /* zero out any padding required */
4153 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4154 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4158 /* now the addresses */
4160 struct sctp_scoping scp;
4163 * To optimize this we could put the scoping stuff into a
4164 * structure and remove the individual uint8's from the
4165 * assoc structure. Then we could just sifa in the address
4166 * within the stcb.. but for now this is a quick hack to get
4167 * the address stuff teased apart.
4169 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4170 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4171 scp.loopback_scope = stcb->asoc.loopback_scope;
4172 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4173 scp.local_scope = stcb->asoc.local_scope;
4174 scp.site_scope = stcb->asoc.site_scope;
4176 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4179 /* calulate the size and update pkt header and chunk header */
4181 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4182 if (SCTP_BUF_NEXT(m_at) == NULL)
4184 p_len += SCTP_BUF_LEN(m_at);
4186 initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4188 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4189 * here since the timer will drive a retranmission.
4192 /* I don't expect this to execute but we will be safe here */
4194 if ((padval) && (mp_last)) {
4196 * The compiler worries that mp_last may not be set even
4197 * though I think it is impossible :-> however we add
4198 * mp_last here just in case.
4200 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4202 /* Houston we have a problem, no space */
4208 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4209 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4210 (struct sockaddr *)&net->ro._l_addr,
4211 m, 0, NULL, 0, 0, NULL, 0, so_locked);
4212 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4213 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4214 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4215 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4219 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4220 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4223 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4224 * being equal to the beginning of the params i.e. (iphlen +
4225 * sizeof(struct sctp_init_msg) parse through the parameters to the
4226 * end of the mbuf verifying that all parameters are known.
4228 * For unknown parameters build and return a mbuf with
4229 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4230 * processing this chunk stop, and set *abort_processing to 1.
4232 * By having param_offset be pre-set to where parameters begin it is
4233 * hoped that this routine may be reused in the future by new
4236 struct sctp_paramhdr *phdr, params;
4238 struct mbuf *mat, *op_err;
4239 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4240 int at, limit, pad_needed;
4241 uint16_t ptype, plen, padded_size;
4244 *abort_processing = 0;
4247 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4250 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4251 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4252 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4253 ptype = ntohs(phdr->param_type);
4254 plen = ntohs(phdr->param_length);
4255 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4256 /* wacked parameter */
4257 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4260 limit -= SCTP_SIZE32(plen);
4262 * All parameters for all chunks that we know/understand are
4263 * listed here. We process them other places and make
4264 * appropriate stop actions per the upper bits. However this
4265 * is the generic routine processor's can call to get back
4266 * an operr.. to either incorporate (init-ack) or send.
4268 padded_size = SCTP_SIZE32(plen);
4270 /* Param's with variable size */
4271 case SCTP_HEARTBEAT_INFO:
4272 case SCTP_STATE_COOKIE:
4273 case SCTP_UNRECOG_PARAM:
4274 case SCTP_ERROR_CAUSE_IND:
4278 /* Param's with variable size within a range */
4279 case SCTP_CHUNK_LIST:
4280 case SCTP_SUPPORTED_CHUNK_EXT:
4281 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4282 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4287 case SCTP_SUPPORTED_ADDRTYPE:
4288 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4289 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4295 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4296 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4301 case SCTP_SET_PRIM_ADDR:
4302 case SCTP_DEL_IP_ADDRESS:
4303 case SCTP_ADD_IP_ADDRESS:
4304 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4305 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4306 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4311 /* Param's with a fixed size */
4312 case SCTP_IPV4_ADDRESS:
4313 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4314 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4319 case SCTP_IPV6_ADDRESS:
4320 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4321 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4326 case SCTP_COOKIE_PRESERVE:
4327 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4328 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4333 case SCTP_ECN_NONCE_SUPPORTED:
4334 case SCTP_PRSCTP_SUPPORTED:
4335 if (padded_size != sizeof(struct sctp_paramhdr)) {
4336 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4341 case SCTP_ECN_CAPABLE:
4342 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4343 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4348 case SCTP_ULP_ADAPTATION:
4349 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4350 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4355 case SCTP_SUCCESS_REPORT:
4356 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4357 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4362 case SCTP_HOSTNAME_ADDRESS:
4364 /* We can NOT handle HOST NAME addresses!! */
4367 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4368 *abort_processing = 1;
4369 if (op_err == NULL) {
4370 /* Ok need to try to get a mbuf */
4372 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4374 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4377 l_len += sizeof(struct sctp_paramhdr);
4378 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4380 SCTP_BUF_LEN(op_err) = 0;
4382 * pre-reserve space for ip
4383 * and sctp header and
4387 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4389 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4391 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4392 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4396 /* If we have space */
4397 struct sctp_paramhdr s;
4400 uint32_t cpthis = 0;
4402 pad_needed = 4 - (err_at % 4);
4403 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4404 err_at += pad_needed;
4406 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4407 s.param_length = htons(sizeof(s) + plen);
4408 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4409 err_at += sizeof(s);
4410 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4412 sctp_m_freem(op_err);
4414 * we are out of memory but
4415 * we still need to have a
4416 * look at what to do (the
4417 * system is in trouble
4422 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4430 * we do not recognize the parameter figure out what
4433 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4434 if ((ptype & 0x4000) == 0x4000) {
4435 /* Report bit is set?? */
4436 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4437 if (op_err == NULL) {
4440 /* Ok need to try to get an mbuf */
4442 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4444 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4447 l_len += sizeof(struct sctp_paramhdr);
4448 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4450 SCTP_BUF_LEN(op_err) = 0;
4452 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4454 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4456 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4457 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4461 /* If we have space */
4462 struct sctp_paramhdr s;
4465 uint32_t cpthis = 0;
4467 pad_needed = 4 - (err_at % 4);
4468 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4469 err_at += pad_needed;
4471 s.param_type = htons(SCTP_UNRECOG_PARAM);
4472 s.param_length = htons(sizeof(s) + plen);
4473 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4474 err_at += sizeof(s);
4475 if (plen > sizeof(tempbuf)) {
4476 plen = sizeof(tempbuf);
4478 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4480 sctp_m_freem(op_err);
4482 * we are out of memory but
4483 * we still need to have a
4484 * look at what to do (the
4485 * system is in trouble
4489 goto more_processing;
4491 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4496 if ((ptype & 0x8000) == 0x0000) {
4497 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4500 /* skip this chunk and continue processing */
4501 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4502 at += SCTP_SIZE32(plen);
4507 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4511 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4512 *abort_processing = 1;
4513 if ((op_err == NULL) && phdr) {
4517 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4519 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4521 l_len += (2 * sizeof(struct sctp_paramhdr));
4522 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4524 SCTP_BUF_LEN(op_err) = 0;
4526 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4528 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4530 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4531 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4534 if ((op_err) && phdr) {
4535 struct sctp_paramhdr s;
4538 uint32_t cpthis = 0;
4540 pad_needed = 4 - (err_at % 4);
4541 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4542 err_at += pad_needed;
4544 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4545 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4546 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4547 err_at += sizeof(s);
4548 /* Only copy back the p-hdr that caused the issue */
4549 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4555 sctp_are_there_new_addresses(struct sctp_association *asoc,
4556 struct mbuf *in_initpkt, int iphlen, int offset)
4559 * Given a INIT packet, look through the packet to verify that there
4560 * are NO new addresses. As we go through the parameters add reports
4561 * of any un-understood parameters that require an error. Also we
4562 * must return (1) to drop the packet if we see a un-understood
4563 * parameter that tells us to drop the chunk.
4565 struct sockaddr_in sin4, *sa4;
4568 struct sockaddr_in6 sin6, *sa6;
4571 struct sockaddr *sa_touse;
4572 struct sockaddr *sa;
4573 struct sctp_paramhdr *phdr, params;
4577 struct ip6_hdr *ip6h;
4581 uint16_t ptype, plen;
4584 struct sctp_nets *net;
4586 memset(&sin4, 0, sizeof(sin4));
4588 memset(&sin6, 0, sizeof(sin6));
4590 sin4.sin_family = AF_INET;
4591 sin4.sin_len = sizeof(sin4);
4593 sin6.sin6_family = AF_INET6;
4594 sin6.sin6_len = sizeof(sin6);
4597 /* First what about the src address of the pkt ? */
4598 iph = mtod(in_initpkt, struct ip *);
4599 switch (iph->ip_v) {
4601 /* source addr is IPv4 */
4602 sin4.sin_addr = iph->ip_src;
4603 sa_touse = (struct sockaddr *)&sin4;
4606 case IPV6_VERSION >> 4:
4607 /* source addr is IPv6 */
4608 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4609 sin6.sin6_addr = ip6h->ip6_src;
4610 sa_touse = (struct sockaddr *)&sin6;
4618 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4619 sa = (struct sockaddr *)&net->ro._l_addr;
4620 if (sa->sa_family == sa_touse->sa_family) {
4621 if (sa->sa_family == AF_INET) {
4622 sa4 = (struct sockaddr_in *)sa;
4623 if (sa4->sin_addr.s_addr ==
4624 sin4.sin_addr.s_addr) {
4630 if (sa->sa_family == AF_INET6) {
4631 sa6 = (struct sockaddr_in6 *)sa;
4632 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
4642 /* New address added! no need to look futher. */
4645 /* Ok so far lets munge through the rest of the packet */
4649 offset += sizeof(struct sctp_init_chunk);
4650 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4652 ptype = ntohs(phdr->param_type);
4653 plen = ntohs(phdr->param_length);
4654 if (ptype == SCTP_IPV4_ADDRESS) {
4655 struct sctp_ipv4addr_param *p4, p4_buf;
4657 phdr = sctp_get_next_param(mat, offset,
4658 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4659 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4663 p4 = (struct sctp_ipv4addr_param *)phdr;
4664 sin4.sin_addr.s_addr = p4->addr;
4665 sa_touse = (struct sockaddr *)&sin4;
4666 } else if (ptype == SCTP_IPV6_ADDRESS) {
4667 struct sctp_ipv6addr_param *p6, p6_buf;
4669 phdr = sctp_get_next_param(mat, offset,
4670 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4671 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4675 p6 = (struct sctp_ipv6addr_param *)phdr;
4677 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4680 sa_touse = (struct sockaddr *)&sin4;
4683 /* ok, sa_touse points to one to check */
4685 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4686 sa = (struct sockaddr *)&net->ro._l_addr;
4687 if (sa->sa_family != sa_touse->sa_family) {
4690 if (sa->sa_family == AF_INET) {
4691 sa4 = (struct sockaddr_in *)sa;
4692 if (sa4->sin_addr.s_addr ==
4693 sin4.sin_addr.s_addr) {
4699 if (sa->sa_family == AF_INET6) {
4700 sa6 = (struct sockaddr_in6 *)sa;
4701 if (SCTP6_ARE_ADDR_EQUAL(
4702 &sa6->sin6_addr, &sin6.sin6_addr)) {
4710 /* New addr added! no need to look further */
4714 offset += SCTP_SIZE32(plen);
4715 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4721 * Given a MBUF chain that was sent into us containing an INIT. Build a
4722 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4723 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4724 * message (i.e. the struct sctp_init_msg).
4727 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4728 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4729 struct sctp_init_chunk *init_chk, uint32_t vrf_id, int hold_inp_lock)
4731 struct sctp_association *asoc;
4732 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4733 struct sctp_init_msg *initackm_out;
4734 struct sctp_ecn_supported_param *ecn;
4735 struct sctp_prsctp_supported_param *prsctp;
4736 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4737 struct sctp_supported_chunk_types_param *pr_supported;
4738 struct sockaddr_storage store;
4739 struct sockaddr_in *sin;
4742 struct sockaddr_in6 *sin6;
4749 struct ip6_hdr *ip6;
4752 struct sockaddr *to;
4753 struct sctp_state_cookie stc;
4754 struct sctp_nets *net = NULL;
4755 uint8_t *signature = NULL;
4756 int cnt_inits_to = 0;
4757 uint16_t his_limit, i_want;
4758 int abort_flag, padval;
4768 if ((asoc != NULL) &&
4769 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4770 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4771 /* new addresses, out of here in non-cookie-wait states */
4773 * Send a ABORT, we don't add the new address error clause
4774 * though we even set the T bit and copy in the 0 tag.. this
4775 * looks no different than if no listener was present.
4777 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id);
4781 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4782 (offset + sizeof(struct sctp_init_chunk)),
4783 &abort_flag, (struct sctp_chunkhdr *)init_chk);
4785 sctp_send_abort(init_pkt, iphlen, sh,
4786 init_chk->init.initiate_tag, op_err, vrf_id);
4789 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4791 /* No memory, INIT timer will re-attempt. */
4793 sctp_m_freem(op_err);
4796 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4798 /* the time I built cookie */
4799 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4801 /* populate any tie tags */
4803 /* unlock before tag selections */
4804 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4805 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4806 stc.cookie_life = asoc->cookie_life;
4807 net = asoc->primary_destination;
4809 stc.tie_tag_my_vtag = 0;
4810 stc.tie_tag_peer_vtag = 0;
4811 /* life I will award this cookie */
4812 stc.cookie_life = inp->sctp_ep.def_cookie_life;
4815 /* copy in the ports for later check */
4816 stc.myport = sh->dest_port;
4817 stc.peerport = sh->src_port;
4820 * If we wanted to honor cookie life extentions, we would add to
4821 * stc.cookie_life. For now we should NOT honor any extension
4823 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4824 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4825 struct inpcb *in_inp;
4827 /* Its a V6 socket */
4828 in_inp = (struct inpcb *)inp;
4829 stc.ipv6_addr_legal = 1;
4830 /* Now look at the binding flag to see if V4 will be legal */
4831 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4832 stc.ipv4_addr_legal = 1;
4834 /* V4 addresses are NOT legal on the association */
4835 stc.ipv4_addr_legal = 0;
4838 /* Its a V4 socket, no - V6 */
4839 stc.ipv4_addr_legal = 1;
4840 stc.ipv6_addr_legal = 0;
4843 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4848 /* now for scope setup */
4849 memset((caddr_t)&store, 0, sizeof(store));
4850 sin = (struct sockaddr_in *)&store;
4852 sin6 = (struct sockaddr_in6 *)&store;
4855 to = (struct sockaddr *)&store;
4856 iph = mtod(init_pkt, struct ip *);
4857 switch (iph->ip_v) {
4860 struct sctp_ifa *addr;
4861 sctp_route_t iproute;
4863 sin->sin_family = AF_INET;
4864 sin->sin_len = sizeof(struct sockaddr_in);
4865 sin->sin_port = sh->src_port;
4866 sin->sin_addr = iph->ip_src;
4867 /* lookup address */
4868 stc.address[0] = sin->sin_addr.s_addr;
4872 stc.addr_type = SCTP_IPV4_ADDRESS;
4873 /* local from address */
4874 memset(&iproute, 0, sizeof(iproute));
4876 memcpy(&ro->ro_dst, sin, sizeof(*sin));
4877 addr = sctp_source_address_selection(inp, NULL,
4887 stc.laddress[0] = addr->address.sin.sin_addr.s_addr;
4888 stc.laddress[1] = 0;
4889 stc.laddress[2] = 0;
4890 stc.laddress[3] = 0;
4891 stc.laddr_type = SCTP_IPV4_ADDRESS;
4892 /* scope_id is only for v6 */
4894 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
4895 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4900 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
4901 /* Must use the address in this case */
4902 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
4903 stc.loopback_scope = 1;
4906 stc.local_scope = 0;
4908 sctp_free_ifa(addr);
4912 case IPV6_VERSION >> 4:
4914 struct sctp_ifa *addr;
4915 struct route_in6 iproute6;
4917 ip6 = mtod(init_pkt, struct ip6_hdr *);
4918 sin6->sin6_family = AF_INET6;
4919 sin6->sin6_len = sizeof(struct sockaddr_in6);
4920 sin6->sin6_port = sh->src_port;
4921 sin6->sin6_addr = ip6->ip6_src;
4922 /* lookup address */
4923 memcpy(&stc.address, &sin6->sin6_addr,
4924 sizeof(struct in6_addr));
4925 sin6->sin6_scope_id = 0;
4926 stc.addr_type = SCTP_IPV6_ADDRESS;
4928 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
4929 stc.loopback_scope = 1;
4930 stc.local_scope = 0;
4933 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4935 * If the new destination is a
4936 * LINK_LOCAL we must have common
4937 * both site and local scope. Don't
4938 * set local scope though since we
4939 * must depend on the source to be
4940 * added implicitly. We cannot
4941 * assure just because we share one
4942 * link that all links are common.
4944 stc.local_scope = 0;
4948 * we start counting for the private
4949 * address stuff at 1. since the
4950 * link local we source from won't
4951 * show up in our scoped count.
4955 * pull out the scope_id from
4959 * FIX ME: does this have scope from
4962 (void)sa6_recoverscope(sin6);
4964 sa6_embedscope(sin6, ip6_use_defzone);
4965 stc.scope_id = sin6->sin6_scope_id;
4966 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4968 * If the new destination is
4969 * SITE_LOCAL then we must have site
4974 /* local from address */
4975 memset(&iproute6, 0, sizeof(iproute6));
4976 ro = (sctp_route_t *) & iproute6;
4977 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
4978 addr = sctp_source_address_selection(inp, NULL,
4979 ro, NULL, 0, vrf_id);
4987 memcpy(&stc.laddress, &addr->address.sin6.sin6_addr, sizeof(struct in6_addr));
4988 stc.laddr_type = SCTP_IPV6_ADDRESS;
4989 sctp_free_ifa(addr);
4998 /* set the scope per the existing tcb */
5001 struct sctp_nets *lnet;
5005 stc.loopback_scope = asoc->loopback_scope;
5006 stc.ipv4_scope = asoc->ipv4_local_scope;
5007 stc.site_scope = asoc->site_scope;
5008 stc.local_scope = asoc->local_scope;
5010 /* Why do we not consider IPv4 LL addresses? */
5011 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5012 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5013 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5015 * if we have a LL address, start
5023 /* use the net pointer */
5024 to = (struct sockaddr *)&net->ro._l_addr;
5025 switch (to->sa_family) {
5027 sin = (struct sockaddr_in *)to;
5028 stc.address[0] = sin->sin_addr.s_addr;
5032 stc.addr_type = SCTP_IPV4_ADDRESS;
5033 if (net->src_addr_selected == 0) {
5035 * strange case here, the INIT should have
5036 * did the selection.
5038 net->ro._s_addr = sctp_source_address_selection(inp,
5039 stcb, (sctp_route_t *) & net->ro,
5041 if (net->ro._s_addr == NULL)
5044 net->src_addr_selected = 1;
5047 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5048 stc.laddress[1] = 0;
5049 stc.laddress[2] = 0;
5050 stc.laddress[3] = 0;
5051 stc.laddr_type = SCTP_IPV4_ADDRESS;
5055 sin6 = (struct sockaddr_in6 *)to;
5056 memcpy(&stc.address, &sin6->sin6_addr,
5057 sizeof(struct in6_addr));
5058 stc.addr_type = SCTP_IPV6_ADDRESS;
5059 if (net->src_addr_selected == 0) {
5061 * strange case here, the INIT should have
5062 * did the selection.
5064 net->ro._s_addr = sctp_source_address_selection(inp,
5065 stcb, (sctp_route_t *) & net->ro,
5067 if (net->ro._s_addr == NULL)
5070 net->src_addr_selected = 1;
5072 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5073 sizeof(struct in6_addr));
5074 stc.laddr_type = SCTP_IPV6_ADDRESS;
5079 /* Now lets put the SCTP header in place */
5080 initackm_out = mtod(m, struct sctp_init_msg *);
5081 initackm_out->sh.src_port = inp->sctp_lport;
5082 initackm_out->sh.dest_port = sh->src_port;
5083 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
5084 /* Save it off for quick ref */
5085 stc.peers_vtag = init_chk->init.initiate_tag;
5086 initackm_out->sh.checksum = 0; /* calculate later */
5088 memcpy(stc.identification, SCTP_VERSION_STRING,
5089 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5090 /* now the chunk header */
5091 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
5092 initackm_out->msg.ch.chunk_flags = 0;
5093 /* fill in later from mbuf we build */
5094 initackm_out->msg.ch.chunk_length = 0;
5095 /* place in my tag */
5096 if ((asoc != NULL) &&
5097 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5098 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5099 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5100 /* re-use the v-tags and init-seq here */
5101 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
5102 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
5104 uint32_t vtag, itsn;
5106 if (hold_inp_lock) {
5107 SCTP_INP_INCR_REF(inp);
5108 SCTP_INP_RUNLOCK(inp);
5111 atomic_add_int(&asoc->refcnt, 1);
5112 SCTP_TCB_UNLOCK(stcb);
5113 vtag = sctp_select_a_tag(inp, 1);
5114 initackm_out->msg.init.initiate_tag = htonl(vtag);
5115 /* get a TSN to use too */
5116 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5117 initackm_out->msg.init.initial_tsn = htonl(itsn);
5118 SCTP_TCB_LOCK(stcb);
5119 atomic_add_int(&asoc->refcnt, -1);
5121 vtag = sctp_select_a_tag(inp, 1);
5122 initackm_out->msg.init.initiate_tag = htonl(vtag);
5123 /* get a TSN to use too */
5124 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5126 if (hold_inp_lock) {
5127 SCTP_INP_RLOCK(inp);
5128 SCTP_INP_DECR_REF(inp);
5131 /* save away my tag to */
5132 stc.my_vtag = initackm_out->msg.init.initiate_tag;
5134 /* set up some of the credits. */
5135 so = inp->sctp_socket;
5137 /* memory problem */
5141 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5143 /* set what I want */
5144 his_limit = ntohs(init_chk->init.num_inbound_streams);
5145 /* choose what I want */
5147 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5148 i_want = asoc->streamoutcnt;
5150 i_want = inp->sctp_ep.pre_open_stream_count;
5153 i_want = inp->sctp_ep.pre_open_stream_count;
5155 if (his_limit < i_want) {
5156 /* I Want more :< */
5157 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
5159 /* I can have what I want :> */
5160 initackm_out->msg.init.num_outbound_streams = htons(i_want);
5162 /* tell him his limt. */
5163 initackm_out->msg.init.num_inbound_streams =
5164 htons(inp->sctp_ep.max_open_streams_intome);
5165 /* setup the ECN pointer */
5167 if (inp->sctp_ep.adaptation_layer_indicator) {
5168 struct sctp_adaptation_layer_indication *ali;
5170 ali = (struct sctp_adaptation_layer_indication *)(
5171 (caddr_t)initackm_out + sizeof(*initackm_out));
5172 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5173 ali->ph.param_length = htons(sizeof(*ali));
5174 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5175 SCTP_BUF_LEN(m) += sizeof(*ali);
5176 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
5179 ecn = (struct sctp_ecn_supported_param *)(
5180 (caddr_t)initackm_out + sizeof(*initackm_out));
5184 if (sctp_ecn_enable == 1) {
5185 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5186 ecn->ph.param_length = htons(sizeof(*ecn));
5187 SCTP_BUF_LEN(m) += sizeof(*ecn);
5189 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5192 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5194 /* And now tell the peer we do pr-sctp */
5195 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5196 prsctp->ph.param_length = htons(sizeof(*prsctp));
5197 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5199 /* And now tell the peer we do all the extensions */
5200 pr_supported = (struct sctp_supported_chunk_types_param *)
5201 ((caddr_t)prsctp + sizeof(*prsctp));
5203 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5205 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5206 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5207 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5208 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5209 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5210 if (!sctp_auth_disable)
5211 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5212 p_len = sizeof(*pr_supported) + num_ext;
5213 pr_supported->ph.param_length = htons(p_len);
5214 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5215 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5217 /* ECN nonce: And now tell the peer we support ECN nonce */
5218 if (sctp_ecn_nonce) {
5219 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5220 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5221 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5222 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5223 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5225 /* add authentication parameters */
5226 if (!sctp_auth_disable) {
5227 struct sctp_auth_random *randp;
5228 struct sctp_auth_hmac_algo *hmacs;
5229 struct sctp_auth_chunk_list *chunks;
5230 uint16_t random_len;
5232 /* generate and add RANDOM parameter */
5233 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5234 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5235 randp->ph.param_type = htons(SCTP_RANDOM);
5236 p_len = sizeof(*randp) + random_len;
5237 randp->ph.param_length = htons(p_len);
5238 SCTP_READ_RANDOM(randp->random_data, random_len);
5239 /* zero out any padding required */
5240 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5241 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5243 /* add HMAC_ALGO parameter */
5244 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5245 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5246 (uint8_t *) hmacs->hmac_ids);
5248 p_len += sizeof(*hmacs);
5249 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5250 hmacs->ph.param_length = htons(p_len);
5251 /* zero out any padding required */
5252 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5253 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5255 /* add CHUNKS parameter */
5256 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5257 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5258 chunks->chunk_types);
5260 p_len += sizeof(*chunks);
5261 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5262 chunks->ph.param_length = htons(p_len);
5263 /* zero out any padding required */
5264 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5265 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5269 /* now the addresses */
5271 struct sctp_scoping scp;
5274 * To optimize this we could put the scoping stuff into a
5275 * structure and remove the individual uint8's from the stc
5276 * structure. Then we could just sifa in the address within
5277 * the stc.. but for now this is a quick hack to get the
5278 * address stuff teased apart.
5280 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5281 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5282 scp.loopback_scope = stc.loopback_scope;
5283 scp.ipv4_local_scope = stc.ipv4_scope;
5284 scp.local_scope = stc.local_scope;
5285 scp.site_scope = stc.site_scope;
5286 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5289 /* tack on the operational error if present */
5297 llen += SCTP_BUF_LEN(ol);
5298 ol = SCTP_BUF_NEXT(ol);
5301 /* must add a pad to the param */
5302 uint32_t cpthis = 0;
5305 padlen = 4 - (llen % 4);
5306 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5308 while (SCTP_BUF_NEXT(m_at) != NULL) {
5309 m_at = SCTP_BUF_NEXT(m_at);
5311 SCTP_BUF_NEXT(m_at) = op_err;
5312 while (SCTP_BUF_NEXT(m_at) != NULL) {
5313 m_at = SCTP_BUF_NEXT(m_at);
5316 /* pre-calulate the size and update pkt header and chunk header */
5318 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5319 p_len += SCTP_BUF_LEN(m_tmp);
5320 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5321 /* m_tmp should now point to last one */
5326 /* Now we must build a cookie */
5327 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5328 sizeof(struct sctphdr), &stc, &signature);
5329 if (m_cookie == NULL) {
5330 /* memory problem */
5334 /* Now append the cookie to the end and update the space/size */
5335 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5337 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5338 p_len += SCTP_BUF_LEN(m_tmp);
5339 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5340 /* m_tmp should now point to last one */
5346 * Place in the size, but we don't include the last pad (if any) in
5349 initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5352 * Time to sign the cookie, we don't sign over the cookie signature
5353 * though thus we set trailer.
5355 (void)sctp_hmac_m(SCTP_HMAC,
5356 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5357 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5358 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5360 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5361 * here since the timer will drive a retranmission.
5364 if ((padval) && (mp_last)) {
5365 /* see my previous comments on mp_last */
5368 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5370 /* Houston we have a problem, no space */
5376 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5377 NULL, 0, SCTP_SO_NOT_LOCKED);
5378 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5383 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5384 struct sctp_association *asoc,
5385 struct sctp_stream_out *strq, int holds_lock)
5387 struct sctp_stream_out *stre, *strn;
5389 if (holds_lock == 0) {
5390 SCTP_TCB_SEND_LOCK(stcb);
5392 if ((strq->next_spoke.tqe_next) ||
5393 (strq->next_spoke.tqe_prev)) {
5394 /* already on wheel */
5397 stre = TAILQ_FIRST(&asoc->out_wheel);
5399 /* only one on wheel */
5400 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5403 for (; stre; stre = strn) {
5404 strn = TAILQ_NEXT(stre, next_spoke);
5405 if (stre->stream_no > strq->stream_no) {
5406 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5408 } else if (stre->stream_no == strq->stream_no) {
5409 /* huh, should not happen */
5411 } else if (strn == NULL) {
5412 /* next one is null */
5413 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5418 if (holds_lock == 0) {
5419 SCTP_TCB_SEND_UNLOCK(stcb);
5424 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5425 struct sctp_association *asoc,
5426 struct sctp_stream_out *strq)
5428 /* take off and then setup so we know it is not on the wheel */
5429 SCTP_TCB_SEND_LOCK(stcb);
5430 if (TAILQ_FIRST(&strq->outqueue)) {
5431 /* more was added */
5432 SCTP_TCB_SEND_UNLOCK(stcb);
5435 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5436 strq->next_spoke.tqe_next = NULL;
5437 strq->next_spoke.tqe_prev = NULL;
5438 SCTP_TCB_SEND_UNLOCK(stcb);
5442 sctp_prune_prsctp(struct sctp_tcb *stcb,
5443 struct sctp_association *asoc,
5444 struct sctp_sndrcvinfo *srcv,
5448 struct sctp_tmit_chunk *chk, *nchk;
5450 SCTP_TCB_LOCK_ASSERT(stcb);
5451 if ((asoc->peer_supports_prsctp) &&
5452 (asoc->sent_queue_cnt_removeable > 0)) {
5453 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5455 * Look for chunks marked with the PR_SCTP flag AND
5456 * the buffer space flag. If the one being sent is
5457 * equal or greater priority then purge the old one
5458 * and free some space.
5460 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5462 * This one is PR-SCTP AND buffer space
5465 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5467 * Lower numbers equates to higher
5468 * priority so if the one we are
5469 * looking at has a larger or equal
5470 * priority we want to drop the data
5471 * and NOT retransmit it.
5475 * We release the book_size
5476 * if the mbuf is here
5481 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5482 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5484 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5485 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5487 &asoc->sent_queue, SCTP_SO_LOCKED);
5488 freed_spc += ret_spc;
5489 if (freed_spc >= dataout) {
5492 } /* if chunk was present */
5493 } /* if of sufficent priority */
5494 } /* if chunk has enabled */
5495 } /* tailqforeach */
5497 chk = TAILQ_FIRST(&asoc->send_queue);
5499 nchk = TAILQ_NEXT(chk, sctp_next);
5500 /* Here we must move to the sent queue and mark */
5501 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5502 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5505 * We release the book_size
5506 * if the mbuf is here
5510 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5511 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5512 &asoc->send_queue, SCTP_SO_LOCKED);
5514 freed_spc += ret_spc;
5515 if (freed_spc >= dataout) {
5518 } /* end if chk->data */
5519 } /* end if right class */
5520 } /* end if chk pr-sctp */
5522 } /* end while (chk) */
5523 } /* if enabled in asoc */
5527 sctp_get_frag_point(struct sctp_tcb *stcb,
5528 struct sctp_association *asoc)
5533 * For endpoints that have both v6 and v4 addresses we must reserve
5534 * room for the ipv6 header, for those that are only dealing with V4
5535 * we use a larger frag point.
5537 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5538 ovh = SCTP_MED_OVERHEAD;
5540 ovh = SCTP_MED_V4_OVERHEAD;
5543 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5544 siz = asoc->smallest_mtu - ovh;
5546 siz = (stcb->asoc.sctp_frag_point - ovh);
5548 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5550 /* A data chunk MUST fit in a cluster */
5551 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5554 /* adjust for an AUTH chunk if DATA requires auth */
5555 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5556 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5559 /* make it an even word boundary please */
5566 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5567 struct sctp_stream_queue_pending *sp)
5570 if (stcb->asoc.peer_supports_prsctp) {
5572 * We assume that the user wants PR_SCTP_TTL if the user
5573 * provides a positive lifetime but does not specify any
5574 * PR_SCTP policy. This is a BAD assumption and causes
5575 * problems at least with the U-Vancovers MPI folks. I will
5576 * change this to be no policy means NO PR-SCTP.
5578 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5579 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5584 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5585 case CHUNK_FLAGS_PR_SCTP_BUF:
5587 * Time to live is a priority stored in tv_sec when
5588 * doing the buffer drop thing.
5590 sp->ts.tv_sec = sp->timetolive;
5593 case CHUNK_FLAGS_PR_SCTP_TTL:
5597 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5598 tv.tv_sec = sp->timetolive / 1000;
5599 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5600 timevaladd(&sp->ts, &tv);
5603 case CHUNK_FLAGS_PR_SCTP_RTX:
5605 * Time to live is a the number or retransmissions
5608 sp->ts.tv_sec = sp->timetolive;
5612 SCTPDBG(SCTP_DEBUG_USRREQ1,
5613 "Unknown PR_SCTP policy %u.\n",
5614 PR_SCTP_POLICY(sp->sinfo_flags));
5621 sctp_msg_append(struct sctp_tcb *stcb,
5622 struct sctp_nets *net,
5624 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5626 int error = 0, holds_lock;
5628 struct sctp_stream_queue_pending *sp = NULL;
5629 struct sctp_stream_out *strm;
5632 * Given an mbuf chain, put it into the association send queue and
5633 * place it on the wheel
5635 holds_lock = hold_stcb_lock;
5636 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5637 /* Invalid stream number */
5638 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5642 if ((stcb->asoc.stream_locked) &&
5643 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5644 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5648 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5649 /* Now can we send this? */
5650 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5651 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5652 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5653 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5654 /* got data while shutting down */
5655 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5659 sctp_alloc_a_strmoq(stcb, sp);
5661 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5665 sp->sinfo_flags = srcv->sinfo_flags;
5666 sp->timetolive = srcv->sinfo_timetolive;
5667 sp->ppid = srcv->sinfo_ppid;
5668 sp->context = srcv->sinfo_context;
5670 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5674 sp->net = stcb->asoc.primary_destination;
5677 atomic_add_int(&sp->net->ref_count, 1);
5678 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5679 sp->stream = srcv->sinfo_stream;
5680 sp->msg_is_complete = 1;
5681 sp->sender_all_done = 1;
5684 sp->tail_mbuf = NULL;
5687 sctp_set_prsctp_policy(stcb, sp);
5689 * We could in theory (for sendall) sifa the length in, but we would
5690 * still have to hunt through the chain since we need to setup the
5694 if (SCTP_BUF_NEXT(at) == NULL)
5696 sp->length += SCTP_BUF_LEN(at);
5697 at = SCTP_BUF_NEXT(at);
5699 SCTP_TCB_SEND_LOCK(stcb);
5700 sctp_snd_sb_alloc(stcb, sp->length);
5701 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5702 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5703 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5704 sp->strseq = strm->next_sequence_sent;
5705 strm->next_sequence_sent++;
5707 if ((strm->next_spoke.tqe_next == NULL) &&
5708 (strm->next_spoke.tqe_prev == NULL)) {
5709 /* Not on wheel, insert */
5710 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5713 SCTP_TCB_SEND_UNLOCK(stcb);
5722 static struct mbuf *
5723 sctp_copy_mbufchain(struct mbuf *clonechain,
5724 struct mbuf *outchain,
5725 struct mbuf **endofchain,
5728 uint8_t copy_by_ref)
5731 struct mbuf *appendchain;
5735 if (endofchain == NULL) {
5739 sctp_m_freem(outchain);
5742 if (can_take_mbuf) {
5743 appendchain = clonechain;
5746 (sizeofcpy <= (int)((((sctp_mbuf_threshold_count - 1) * MLEN) + MHLEN)))
5748 /* Its not in a cluster */
5749 if (*endofchain == NULL) {
5750 /* lets get a mbuf cluster */
5751 if (outchain == NULL) {
5752 /* This is the general case */
5754 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5755 if (outchain == NULL) {
5758 SCTP_BUF_LEN(outchain) = 0;
5759 *endofchain = outchain;
5760 /* get the prepend space */
5761 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5764 * We really should not get a NULL
5770 if (SCTP_BUF_NEXT(m) == NULL) {
5774 m = SCTP_BUF_NEXT(m);
5777 if (*endofchain == NULL) {
5779 * huh, TSNH XXX maybe we
5782 sctp_m_freem(outchain);
5786 /* get the new end of length */
5787 len = M_TRAILINGSPACE(*endofchain);
5789 /* how much is left at the end? */
5790 len = M_TRAILINGSPACE(*endofchain);
5792 /* Find the end of the data, for appending */
5793 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5795 /* Now lets copy it out */
5796 if (len >= sizeofcpy) {
5797 /* It all fits, copy it in */
5798 m_copydata(clonechain, 0, sizeofcpy, cp);
5799 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5801 /* fill up the end of the chain */
5803 m_copydata(clonechain, 0, len, cp);
5804 SCTP_BUF_LEN((*endofchain)) += len;
5805 /* now we need another one */
5808 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5813 SCTP_BUF_NEXT((*endofchain)) = m;
5815 cp = mtod((*endofchain), caddr_t);
5816 m_copydata(clonechain, len, sizeofcpy, cp);
5817 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5821 /* copy the old fashion way */
5822 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5823 #ifdef SCTP_MBUF_LOGGING
5824 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
5829 if (SCTP_BUF_IS_EXTENDED(mat)) {
5830 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5832 mat = SCTP_BUF_NEXT(mat);
5838 if (appendchain == NULL) {
5841 sctp_m_freem(outchain);
5845 /* tack on to the end */
5846 if (*endofchain != NULL) {
5847 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
5851 if (SCTP_BUF_NEXT(m) == NULL) {
5852 SCTP_BUF_NEXT(m) = appendchain;
5855 m = SCTP_BUF_NEXT(m);
5859 * save off the end and update the end-chain postion
5863 if (SCTP_BUF_NEXT(m) == NULL) {
5867 m = SCTP_BUF_NEXT(m);
5871 /* save off the end and update the end-chain postion */
5874 if (SCTP_BUF_NEXT(m) == NULL) {
5878 m = SCTP_BUF_NEXT(m);
5880 return (appendchain);
5885 sctp_med_chunk_output(struct sctp_inpcb *inp,
5886 struct sctp_tcb *stcb,
5887 struct sctp_association *asoc,
5890 int control_only, int *cwnd_full, int from_where,
5891 struct timeval *now, int *now_filled, int frag_point, int so_locked
5892 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5898 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
5901 struct sctp_copy_all *ca;
5904 int added_control = 0;
5905 int un_sent, do_chunk_output = 1;
5906 struct sctp_association *asoc;
5908 ca = (struct sctp_copy_all *)ptr;
5909 if (ca->m == NULL) {
5912 if (ca->inp != inp) {
5916 if ((ca->m) && ca->sndlen) {
5917 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
5919 /* can't copy so we are done */
5923 #ifdef SCTP_MBUF_LOGGING
5924 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
5929 if (SCTP_BUF_IS_EXTENDED(mat)) {
5930 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5932 mat = SCTP_BUF_NEXT(mat);
5939 SCTP_TCB_LOCK_ASSERT(stcb);
5940 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
5941 /* Abort this assoc with m as the user defined reason */
5943 struct sctp_paramhdr *ph;
5945 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
5947 ph = mtod(m, struct sctp_paramhdr *);
5948 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5949 ph->param_length = htons(ca->sndlen);
5952 * We add one here to keep the assoc from
5953 * dis-appearing on us.
5955 atomic_add_int(&stcb->asoc.refcnt, 1);
5956 sctp_abort_an_association(inp, stcb,
5957 SCTP_RESPONSE_TO_USER_REQ,
5958 m, SCTP_SO_NOT_LOCKED);
5960 * sctp_abort_an_association calls sctp_free_asoc()
5961 * free association will NOT free it since we
5962 * incremented the refcnt .. we do this to prevent
5963 * it being freed and things getting tricky since we
5964 * could end up (from free_asoc) calling inpcb_free
5965 * which would get a recursive lock call to the
5966 * iterator lock.. But as a consequence of that the
5967 * stcb will return to us un-locked.. since
5968 * free_asoc returns with either no TCB or the TCB
5969 * unlocked, we must relock.. to unlock in the
5970 * iterator timer :-0
5972 SCTP_TCB_LOCK(stcb);
5973 atomic_add_int(&stcb->asoc.refcnt, -1);
5974 goto no_chunk_output;
5978 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
5982 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
5983 /* shutdown this assoc */
5986 cnt = sctp_is_there_unsent_data(stcb);
5988 if (TAILQ_EMPTY(&asoc->send_queue) &&
5989 TAILQ_EMPTY(&asoc->sent_queue) &&
5991 if (asoc->locked_on_sending) {
5995 * there is nothing queued to send, so I'm
5998 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
5999 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6000 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6002 * only send SHUTDOWN the first time
6005 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6006 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6007 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6009 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6010 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6011 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6012 asoc->primary_destination);
6013 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6014 asoc->primary_destination);
6016 do_chunk_output = 0;
6020 * we still got (or just got) data to send,
6021 * so set SHUTDOWN_PENDING
6024 * XXX sockets draft says that SCTP_EOF
6025 * should be sent with no data. currently,
6026 * we will allow user data to be sent first
6027 * and move to SHUTDOWN-PENDING
6029 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6030 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6031 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6032 if (asoc->locked_on_sending) {
6034 * Locked to send out the
6037 struct sctp_stream_queue_pending *sp;
6039 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6041 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6042 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6045 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6046 if (TAILQ_EMPTY(&asoc->send_queue) &&
6047 TAILQ_EMPTY(&asoc->sent_queue) &&
6048 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6050 atomic_add_int(&stcb->asoc.refcnt, 1);
6051 sctp_abort_an_association(stcb->sctp_ep, stcb,
6052 SCTP_RESPONSE_TO_USER_REQ,
6053 NULL, SCTP_SO_NOT_LOCKED);
6054 atomic_add_int(&stcb->asoc.refcnt, -1);
6055 goto no_chunk_output;
6057 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6058 asoc->primary_destination);
6064 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6065 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
6067 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6068 (stcb->asoc.total_flight > 0) &&
6069 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6071 do_chunk_output = 0;
6073 if (do_chunk_output)
6074 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6075 else if (added_control) {
6076 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
6080 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6081 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6082 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6093 sctp_sendall_completes(void *ptr, uint32_t val)
6095 struct sctp_copy_all *ca;
6097 ca = (struct sctp_copy_all *)ptr;
6099 * Do a notify here? Kacheong suggests that the notify be done at
6100 * the send time.. so you would push up a notification if any send
6101 * failed. Don't know if this is feasable since the only failures we
6102 * have is "memory" related and if you cannot get an mbuf to send
6103 * the data you surely can't get an mbuf to send up to notify the
6104 * user you can't send the data :->
6107 /* now free everything */
6108 sctp_m_freem(ca->m);
6109 SCTP_FREE(ca, SCTP_M_COPYAL);
6113 #define MC_ALIGN(m, len) do { \
6114 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6119 static struct mbuf *
6120 sctp_copy_out_all(struct uio *uio, int len)
6122 struct mbuf *ret, *at;
6123 int left, willcpy, cancpy, error;
6125 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6131 SCTP_BUF_LEN(ret) = 0;
6132 /* save space for the data chunk header */
6133 cancpy = M_TRAILINGSPACE(ret);
6134 willcpy = min(cancpy, left);
6137 /* Align data to the end */
6138 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6144 SCTP_BUF_LEN(at) = willcpy;
6145 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6148 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6149 if (SCTP_BUF_NEXT(at) == NULL) {
6152 at = SCTP_BUF_NEXT(at);
6153 SCTP_BUF_LEN(at) = 0;
6154 cancpy = M_TRAILINGSPACE(at);
6155 willcpy = min(cancpy, left);
6162 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6163 struct sctp_sndrcvinfo *srcv)
6166 struct sctp_copy_all *ca;
6168 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6175 memset(ca, 0, sizeof(struct sctp_copy_all));
6178 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6180 * take off the sendall flag, it would be bad if we failed to do
6183 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6184 /* get length and mbuf chain */
6186 ca->sndlen = uio->uio_resid;
6187 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6188 if (ca->m == NULL) {
6189 SCTP_FREE(ca, SCTP_M_COPYAL);
6190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6194 /* Gather the length of the send */
6200 ca->sndlen += SCTP_BUF_LEN(m);
6201 m = SCTP_BUF_NEXT(m);
6205 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6206 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6207 SCTP_ASOC_ANY_STATE,
6209 sctp_sendall_completes, inp, 1);
6211 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6212 SCTP_FREE(ca, SCTP_M_COPYAL);
6213 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6221 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6223 struct sctp_tmit_chunk *chk, *nchk;
6225 chk = TAILQ_FIRST(&asoc->control_send_queue);
6227 nchk = TAILQ_NEXT(chk, sctp_next);
6228 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6229 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6231 sctp_m_freem(chk->data);
6234 asoc->ctrl_queue_cnt--;
6235 sctp_free_a_chunk(stcb, chk);
6242 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6244 struct sctp_association *asoc;
6245 struct sctp_tmit_chunk *chk, *chk_tmp;
6248 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
6251 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6252 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
6253 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6254 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6256 sctp_m_freem(chk->data);
6259 asoc->ctrl_queue_cnt--;
6260 sctp_free_a_chunk(stcb, chk);
6267 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6269 struct sctp_association *asoc,
6270 struct sctp_tmit_chunk **data_list,
6272 struct sctp_nets *net)
6275 struct sctp_tmit_chunk *tp1;
6277 for (i = 0; i < bundle_at; i++) {
6278 /* off of the send queue */
6281 * Any chunk NOT 0 you zap the time chunk 0 gets
6282 * zapped or set based on if a RTO measurment is
6285 data_list[i]->do_rtt = 0;
6288 data_list[i]->sent_rcv_time = net->last_sent_time;
6289 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6290 TAILQ_REMOVE(&asoc->send_queue,
6293 /* on to the sent queue */
6294 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6295 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6296 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6297 struct sctp_tmit_chunk *tpp;
6299 /* need to move back */
6301 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6303 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6307 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6308 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6311 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6313 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6318 /* This does not lower until the cum-ack passes it */
6319 asoc->sent_queue_cnt++;
6320 asoc->send_queue_cnt--;
6321 if ((asoc->peers_rwnd <= 0) &&
6322 (asoc->total_flight == 0) &&
6324 /* Mark the chunk as being a window probe */
6325 SCTP_STAT_INCR(sctps_windowprobed);
6327 #ifdef SCTP_AUDITING_ENABLED
6328 sctp_audit_log(0xC2, 3);
6330 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6331 data_list[i]->snd_count = 1;
6332 data_list[i]->rec.data.chunk_was_revoked = 0;
6333 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
6334 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6335 data_list[i]->whoTo->flight_size,
6336 data_list[i]->book_size,
6337 (uintptr_t) data_list[i]->whoTo,
6338 data_list[i]->rec.data.TSN_seq);
6340 sctp_flight_size_increase(data_list[i]);
6341 sctp_total_flight_increase(stcb, data_list[i]);
6342 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
6343 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6344 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
6346 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6347 (uint32_t) (data_list[i]->send_size + sctp_peer_chunk_oh));
6348 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6349 /* SWS sender side engages */
6350 asoc->peers_rwnd = 0;
6356 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6358 struct sctp_tmit_chunk *chk, *nchk;
6360 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6362 nchk = TAILQ_NEXT(chk, sctp_next);
6363 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6364 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6365 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6366 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6367 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6368 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6369 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6370 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6371 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6372 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6373 /* Stray chunks must be cleaned up */
6375 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6377 sctp_m_freem(chk->data);
6380 asoc->ctrl_queue_cnt--;
6381 sctp_free_a_chunk(stcb, chk);
6382 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6383 /* special handling, we must look into the param */
6384 if (chk != asoc->str_reset) {
6385 goto clean_up_anyway;
6393 sctp_can_we_split_this(struct sctp_tcb *stcb,
6395 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6398 * Make a decision on if I should split a msg into multiple parts.
6399 * This is only asked of incomplete messages.
6403 * If we are doing EEOR we need to always send it if its the
6404 * entire thing, since it might be all the guy is putting in
6407 if (goal_mtu >= length) {
6409 * If we have data outstanding,
6410 * we get another chance when the sack
6411 * arrives to transmit - wait for more data
6413 if (stcb->asoc.total_flight == 0) {
6415 * If nothing is in flight, we zero the
6423 /* You can fill the rest */
6428 * For those strange folk that make the send buffer
6429 * smaller than our fragmentation point, we can't
6430 * get a full msg in so we have to allow splitting.
6432 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6435 if ((length <= goal_mtu) ||
6436 ((length - goal_mtu) < sctp_min_residual)) {
6437 /* Sub-optimial residual don't split in non-eeor mode. */
6441 * If we reach here length is larger than the goal_mtu. Do we wish
6442 * to split it for the sake of packet putting together?
6444 if (goal_mtu >= min(sctp_min_split_point, frag_point)) {
6445 /* Its ok to split it */
6446 return (min(goal_mtu, frag_point));
6448 /* Nope, can't split */
6454 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6455 struct sctp_stream_out *strq,
6457 uint32_t frag_point,
6463 /* Move from the stream to the send_queue keeping track of the total */
6464 struct sctp_association *asoc;
6465 struct sctp_stream_queue_pending *sp;
6466 struct sctp_tmit_chunk *chk;
6467 struct sctp_data_chunk *dchkh;
6468 uint32_t to_move, length;
6469 uint8_t rcv_flags = 0;
6471 uint8_t send_lock_up = 0;
6473 SCTP_TCB_LOCK_ASSERT(stcb);
6476 /* sa_ignore FREED_MEMORY */
6477 sp = TAILQ_FIRST(&strq->outqueue);
6480 if (send_lock_up == 0) {
6481 SCTP_TCB_SEND_LOCK(stcb);
6484 sp = TAILQ_FIRST(&strq->outqueue);
6488 if (strq->last_msg_incomplete) {
6489 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6491 strq->last_msg_incomplete);
6492 strq->last_msg_incomplete = 0;
6496 SCTP_TCB_SEND_UNLOCK(stcb);
6501 if ((sp->msg_is_complete) && (sp->length == 0)) {
6502 if (sp->sender_all_done) {
6504 * We are doing differed cleanup. Last time through
6505 * when we took all the data the sender_all_done was
6508 if (sp->put_last_out == 0) {
6509 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6510 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6511 sp->sender_all_done,
6513 sp->msg_is_complete,
6517 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6518 SCTP_TCB_SEND_LOCK(stcb);
6521 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6522 TAILQ_REMOVE(&strq->outqueue, sp, next);
6523 sctp_free_remote_addr(sp->net);
6525 sctp_m_freem(sp->data);
6528 sctp_free_a_strmoq(stcb, sp);
6530 /* we can't be locked to it */
6532 stcb->asoc.locked_on_sending = NULL;
6534 SCTP_TCB_SEND_UNLOCK(stcb);
6537 /* back to get the next msg */
6541 * sender just finished this but still holds a
6550 /* is there some to get */
6551 if (sp->length == 0) {
6559 some_taken = sp->some_taken;
6560 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6561 sp->msg_is_complete = 1;
6564 length = sp->length;
6565 if (sp->msg_is_complete) {
6566 /* The message is complete */
6567 to_move = min(length, frag_point);
6568 if (to_move == length) {
6569 /* All of it fits in the MTU */
6570 if (sp->some_taken) {
6571 rcv_flags |= SCTP_DATA_LAST_FRAG;
6572 sp->put_last_out = 1;
6574 rcv_flags |= SCTP_DATA_NOT_FRAG;
6575 sp->put_last_out = 1;
6578 /* Not all of it fits, we fragment */
6579 if (sp->some_taken == 0) {
6580 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6585 to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6586 frag_point, eeor_mode);
6589 * We use a snapshot of length in case it
6590 * is expanding during the compare.
6595 if (to_move >= llen) {
6597 if (send_lock_up == 0) {
6599 * We are taking all of an incomplete msg
6600 * thus we need a send lock.
6602 SCTP_TCB_SEND_LOCK(stcb);
6604 if (sp->msg_is_complete) {
6606 * the sender finished the
6613 if (sp->some_taken == 0) {
6614 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6618 /* Nothing to take. */
6619 if (sp->some_taken) {
6628 /* If we reach here, we can copy out a chunk */
6629 sctp_alloc_a_chunk(stcb, chk);
6631 /* No chunk memory */
6637 * Setup for unordered if needed by looking at the user sent info
6640 if (sp->sinfo_flags & SCTP_UNORDERED) {
6641 rcv_flags |= SCTP_DATA_UNORDERED;
6643 /* clear out the chunk before setting up */
6644 memset(chk, 0, sizeof(*chk));
6645 chk->rec.data.rcv_flags = rcv_flags;
6647 if (to_move >= length) {
6648 /* we think we can steal the whole thing */
6649 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6650 SCTP_TCB_SEND_LOCK(stcb);
6653 if (to_move < sp->length) {
6654 /* bail, it changed */
6657 chk->data = sp->data;
6658 chk->last_mbuf = sp->tail_mbuf;
6659 /* register the stealing */
6660 sp->data = sp->tail_mbuf = NULL;
6665 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6666 chk->last_mbuf = NULL;
6667 if (chk->data == NULL) {
6668 sp->some_taken = some_taken;
6669 sctp_free_a_chunk(stcb, chk);
6674 #ifdef SCTP_MBUF_LOGGING
6675 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
6680 if (SCTP_BUF_IS_EXTENDED(mat)) {
6681 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6683 mat = SCTP_BUF_NEXT(mat);
6687 /* Pull off the data */
6688 m_adj(sp->data, to_move);
6689 /* Now lets work our way down and compact it */
6691 while (m && (SCTP_BUF_LEN(m) == 0)) {
6692 sp->data = SCTP_BUF_NEXT(m);
6693 SCTP_BUF_NEXT(m) = NULL;
6694 if (sp->tail_mbuf == m) {
6696 * Freeing tail? TSNH since
6697 * we supposedly were taking less
6698 * than the sp->length.
6701 panic("Huh, freing tail? - TSNH");
6703 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6704 sp->tail_mbuf = sp->data = NULL;
6713 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6714 chk->copy_by_ref = 1;
6716 chk->copy_by_ref = 0;
6719 * get last_mbuf and counts of mb useage This is ugly but hopefully
6720 * its only one mbuf.
6722 if (chk->last_mbuf == NULL) {
6723 chk->last_mbuf = chk->data;
6724 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6725 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6728 if (to_move > length) {
6729 /*- This should not happen either
6730 * since we always lower to_move to the size
6731 * of sp->length if its larger.
6734 panic("Huh, how can to_move be larger?");
6736 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6740 atomic_subtract_int(&sp->length, to_move);
6742 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6743 /* Not enough room for a chunk header, get some */
6746 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6749 * we're in trouble here. _PREPEND below will free
6750 * all the data if there is no leading space, so we
6751 * must put the data back and restore.
6753 if (send_lock_up == 0) {
6754 SCTP_TCB_SEND_LOCK(stcb);
6757 if (chk->data == NULL) {
6758 /* unsteal the data */
6759 sp->data = chk->data;
6760 sp->tail_mbuf = chk->last_mbuf;
6764 /* reassemble the data */
6766 sp->data = chk->data;
6767 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6769 sp->some_taken = some_taken;
6770 atomic_add_int(&sp->length, to_move);
6773 sctp_free_a_chunk(stcb, chk);
6777 SCTP_BUF_LEN(m) = 0;
6778 SCTP_BUF_NEXT(m) = chk->data;
6780 M_ALIGN(chk->data, 4);
6783 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6784 if (chk->data == NULL) {
6785 /* HELP, TSNH since we assured it would not above? */
6787 panic("prepend failes HELP?");
6789 SCTP_PRINTF("prepend fails HELP?\n");
6790 sctp_free_a_chunk(stcb, chk);
6796 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6797 chk->book_size = chk->send_size = (to_move +
6798 sizeof(struct sctp_data_chunk));
6799 chk->book_size_scale = 0;
6800 chk->sent = SCTP_DATAGRAM_UNSENT;
6803 chk->asoc = &stcb->asoc;
6804 chk->pad_inplace = 0;
6805 chk->no_fr_allowed = 0;
6806 chk->rec.data.stream_seq = sp->strseq;
6807 chk->rec.data.stream_number = sp->stream;
6808 chk->rec.data.payloadtype = sp->ppid;
6809 chk->rec.data.context = sp->context;
6810 chk->rec.data.doing_fast_retransmit = 0;
6811 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
6813 chk->rec.data.timetodrop = sp->ts;
6814 chk->flags = sp->act_flags;
6815 chk->addr_over = sp->addr_over;
6818 atomic_add_int(&chk->whoTo->ref_count, 1);
6820 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6821 if (sctp_logging_level & SCTP_LOG_AT_SEND_2_OUTQ) {
6822 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6823 (uintptr_t) stcb, sp->length,
6824 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6825 chk->rec.data.TSN_seq);
6827 dchkh = mtod(chk->data, struct sctp_data_chunk *);
6829 * Put the rest of the things in place now. Size was done earlier in
6830 * previous loop prior to padding.
6833 #ifdef SCTP_ASOCLOG_OF_TSNS
6834 SCTP_TCB_LOCK_ASSERT(stcb);
6835 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
6836 asoc->tsn_out_at = 0;
6837 asoc->tsn_out_wrapped = 1;
6839 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
6840 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
6841 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
6842 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
6843 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
6844 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
6845 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
6846 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
6850 dchkh->ch.chunk_type = SCTP_DATA;
6851 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
6852 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
6853 dchkh->dp.stream_id = htons(strq->stream_no);
6854 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
6855 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
6856 dchkh->ch.chunk_length = htons(chk->send_size);
6857 /* Now advance the chk->send_size by the actual pad needed. */
6858 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
6863 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
6864 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
6865 chk->pad_inplace = 1;
6867 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
6868 /* pad added an mbuf */
6869 chk->last_mbuf = lm;
6871 chk->send_size += pads;
6873 /* We only re-set the policy if it is on */
6874 if (sp->pr_sctp_on) {
6875 sctp_set_prsctp_policy(stcb, sp);
6876 asoc->pr_sctp_cnt++;
6877 chk->pr_sctp_on = 1;
6879 chk->pr_sctp_on = 0;
6881 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
6882 /* All done pull and kill the message */
6883 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6884 if (sp->put_last_out == 0) {
6885 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
6886 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6887 sp->sender_all_done,
6889 sp->msg_is_complete,
6893 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
6894 SCTP_TCB_SEND_LOCK(stcb);
6897 TAILQ_REMOVE(&strq->outqueue, sp, next);
6898 sctp_free_remote_addr(sp->net);
6900 sctp_m_freem(sp->data);
6903 sctp_free_a_strmoq(stcb, sp);
6905 /* we can't be locked to it */
6907 stcb->asoc.locked_on_sending = NULL;
6909 /* more to go, we are locked */
6912 asoc->chunks_on_out_queue++;
6913 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
6914 asoc->send_queue_cnt++;
6917 SCTP_TCB_SEND_UNLOCK(stcb);
6924 static struct sctp_stream_out *
6925 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
6927 struct sctp_stream_out *strq;
6929 /* Find the next stream to use */
6930 if (asoc->last_out_stream == NULL) {
6931 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6932 if (asoc->last_out_stream == NULL) {
6933 /* huh nothing on the wheel, TSNH */
6938 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
6941 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6943 /* Save off the last stream */
6944 asoc->last_out_stream = strq;
6951 sctp_fill_outqueue(struct sctp_tcb *stcb,
6952 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
6954 struct sctp_association *asoc;
6955 struct sctp_stream_out *strq, *strqn, *strqt;
6956 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
6958 struct sctp_stream_queue_pending *sp;
6960 SCTP_TCB_LOCK_ASSERT(stcb);
6963 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
6964 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6966 /* ?? not sure what else to do */
6967 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
6970 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6972 /* Need an allowance for the data chunk header too */
6973 goal_mtu -= sizeof(struct sctp_data_chunk);
6975 /* must make even word boundary */
6976 goal_mtu &= 0xfffffffc;
6977 if (asoc->locked_on_sending) {
6978 /* We are stuck on one stream until the message completes. */
6979 strqn = strq = asoc->locked_on_sending;
6982 strqn = strq = sctp_select_a_stream(stcb, asoc);
6986 while ((goal_mtu > 0) && strq) {
6987 sp = TAILQ_FIRST(&strq->outqueue);
6989 * If CMT is off, we must validate that the stream in
6990 * question has the first item pointed towards are network
6991 * destionation requested by the caller. Note that if we
6992 * turn out to be locked to a stream (assigning TSN's then
6993 * we must stop, since we cannot look for another stream
6994 * with data to send to that destination). In CMT's case, by
6995 * skipping this check, we will send one data packet towards
6996 * the requested net.
7001 if ((sp->net != net) && (sctp_cmt_on_off == 0)) {
7002 /* none for this network */
7006 strq = sctp_select_a_stream(stcb, asoc);
7010 if (strqn == strq) {
7011 /* I have circled */
7019 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7020 &giveup, eeor_mode, &bail);
7021 asoc->last_out_stream = strq;
7023 asoc->locked_on_sending = strq;
7024 if ((moved_how_much == 0) || (giveup) || bail)
7025 /* no more to move for now */
7028 asoc->locked_on_sending = NULL;
7029 strqt = sctp_select_a_stream(stcb, asoc);
7030 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7031 if (strq == strqn) {
7032 /* Must move start to next one */
7033 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7034 if (strqn == NULL) {
7035 strqn = TAILQ_FIRST(&asoc->out_wheel);
7036 if (strqn == NULL) {
7041 sctp_remove_from_wheel(stcb, asoc, strq);
7043 if ((giveup) || bail) {
7051 total_moved += moved_how_much;
7052 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7053 goal_mtu &= 0xfffffffc;
7058 if (total_moved == 0) {
7059 if ((sctp_cmt_on_off == 0) &&
7060 (net == stcb->asoc.primary_destination)) {
7061 /* ran dry for primary network net */
7062 SCTP_STAT_INCR(sctps_primary_randry);
7063 } else if (sctp_cmt_on_off) {
7064 /* ran dry with CMT on */
7065 SCTP_STAT_INCR(sctps_cmt_randry);
7071 sctp_fix_ecn_echo(struct sctp_association *asoc)
7073 struct sctp_tmit_chunk *chk;
7075 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7076 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7077 chk->sent = SCTP_DATAGRAM_UNSENT;
7083 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7084 struct sctp_association *asoc,
7085 struct sctp_nets *net)
7087 struct sctp_tmit_chunk *chk;
7088 struct sctp_nets *a_net;
7090 SCTP_TCB_LOCK_ASSERT(stcb);
7092 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7093 * destination using the PF algorithm for finding alternate
7096 if (sctp_cmt_on_off && sctp_cmt_pf) {
7097 a_net = sctp_find_alternate_net(stcb, net, 2);
7099 a_net = sctp_find_alternate_net(stcb, net, 0);
7101 if ((a_net != net) &&
7102 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7104 * We only proceed if a valid alternate is found that is not
7105 * this one and is reachable. Here we must move all chunks
7106 * queued in the send queue off of the destination address
7109 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7110 if (chk->whoTo == net) {
7111 /* Move the chunk to our alternate */
7112 sctp_free_remote_addr(chk->whoTo);
7114 atomic_add_int(&a_net->ref_count, 1);
7121 sctp_med_chunk_output(struct sctp_inpcb *inp,
7122 struct sctp_tcb *stcb,
7123 struct sctp_association *asoc,
7126 int control_only, int *cwnd_full, int from_where,
7127 struct timeval *now, int *now_filled, int frag_point, int so_locked
7128 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7134 * Ok this is the generic chunk service queue. we must do the
7135 * following: - Service the stream queue that is next, moving any
7136 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7137 * LAST to the out queue in one pass) and assigning TSN's - Check to
7138 * see if the cwnd/rwnd allows any output, if so we go ahead and
7139 * fomulate and send the low level chunks. Making sure to combine
7140 * any control in the control chunk queue also.
7142 struct sctp_nets *net;
7143 struct mbuf *outchain, *endoutchain;
7144 struct sctp_tmit_chunk *chk, *nchk;
7145 struct sctphdr *shdr;
7147 /* temp arrays for unlinking */
7148 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7149 int no_fragmentflg, error;
7150 int one_chunk, hbflag, skip_data_for_this_net;
7151 int asconf, cookie, no_out_cnt;
7152 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
7153 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7154 struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
7156 uint32_t auth_offset = 0;
7157 struct sctp_auth_chunk *auth = NULL;
7160 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7169 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7170 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7171 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7176 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7178 * First lets prime the pump. For each destination, if there is room
7179 * in the flight size, attempt to pull an MTU's worth out of the
7180 * stream queues into the general send_queue
7182 #ifdef SCTP_AUDITING_ENABLED
7183 sctp_audit_log(0xC2, 2);
7185 SCTP_TCB_LOCK_ASSERT(stcb);
7187 if ((control_only) || (asoc->stream_reset_outstanding))
7192 /* Nothing to possible to send? */
7193 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7194 TAILQ_EMPTY(&asoc->send_queue) &&
7195 TAILQ_EMPTY(&asoc->out_wheel)) {
7199 if (asoc->peers_rwnd == 0) {
7200 /* No room in peers rwnd */
7203 if (asoc->total_flight > 0) {
7204 /* we are allowed one chunk in flight */
7208 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7209 if (sctp_cmt_on_off) {
7211 * for CMT we start at the next one past the one we
7212 * last added data to.
7214 if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7215 goto skip_the_fill_from_streams;
7217 if (asoc->last_net_data_came_from) {
7218 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7220 net = TAILQ_FIRST(&asoc->nets);
7224 net = TAILQ_FIRST(&asoc->nets);
7228 * JRI-TODO: CMT-MPI. Simply set the first
7229 * destination (net) to be optimized for the next
7230 * message to be pulled out of the outwheel. 1. peek
7231 * at outwheel 2. If large message, set net =
7232 * highest_cwnd 3. If small message, set net =
7236 net = asoc->primary_destination;
7239 net = TAILQ_FIRST(&asoc->nets);
7245 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7246 net->window_probe = 0;
7247 if (old_startat && (old_startat == net)) {
7251 * JRI: if dest is unreachable or unconfirmed, do
7252 * not send data to it
7254 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7258 * JRI: if dest is in PF state, do not send data to
7261 if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF)) {
7264 if ((sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
7265 /* nothing can be in queue for this guy */
7268 if (net->flight_size >= net->cwnd) {
7269 /* skip this network, no room */
7274 * JRI : this for loop we are in takes in each net,
7275 * if its's got space in cwnd and has data sent to
7276 * it (when CMT is off) then it calls
7277 * sctp_fill_outqueue for the net. This gets data on
7278 * the send queue for that network.
7280 * In sctp_fill_outqueue TSN's are assigned and data is
7281 * copied out of the stream buffers. Note mostly
7282 * copy by reference (we hope).
7284 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7285 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7287 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7289 /* memory alloc failure */
7291 goto skip_the_fill_from_streams;
7294 if (start_at != TAILQ_FIRST(&asoc->nets)) {
7295 /* got to pick up the beginning stuff. */
7296 old_startat = start_at;
7297 start_at = net = TAILQ_FIRST(&asoc->nets);
7302 skip_the_fill_from_streams:
7303 *cwnd_full = cwnd_full_ind;
7305 /* now service each destination and send out what we can for it */
7306 /* Nothing to send? */
7307 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7308 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7312 if (no_data_chunks) {
7313 chk = TAILQ_FIRST(&asoc->control_send_queue);
7315 chk = TAILQ_FIRST(&asoc->send_queue);
7318 send_start_at = chk->whoTo;
7320 send_start_at = TAILQ_FIRST(&asoc->nets);
7323 again_one_more_time:
7324 for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7325 /* how much can we send? */
7326 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7327 if (old_startat && (old_startat == net)) {
7328 /* through list ocmpletely. */
7332 if (net->ref_count < 2) {
7334 * Ref-count of 1 so we cannot have data or control
7335 * queued to this address. Skip it.
7339 ctl_cnt = bundle_at = 0;
7340 endoutchain = outchain = NULL;
7343 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7344 skip_data_for_this_net = 1;
7346 skip_data_for_this_net = 0;
7348 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7350 * if we have a route and an ifp check to see if we
7351 * have room to send to this guy
7355 ifp = net->ro.ro_rt->rt_ifp;
7356 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7357 SCTP_STAT_INCR(sctps_ifnomemqueued);
7358 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
7359 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7364 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7366 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7370 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7380 if (mtu > asoc->peers_rwnd) {
7381 if (asoc->total_flight > 0) {
7382 /* We have a packet in flight somewhere */
7383 r_mtu = asoc->peers_rwnd;
7385 /* We are always allowed to send one MTU out */
7392 /************************/
7393 /* Control transmission */
7394 /************************/
7395 /* Now first lets go through the control queue */
7396 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7398 nchk = TAILQ_NEXT(chk, sctp_next);
7399 if (chk->whoTo != net) {
7401 * No, not sent to the network we are
7406 if (chk->data == NULL) {
7409 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7411 * It must be unsent. Cookies and ASCONF's
7412 * hang around but there timers will force
7413 * when marked for resend.
7418 * if no AUTH is yet included and this chunk
7419 * requires it, make sure to account for it. We
7420 * don't apply the size until the AUTH chunk is
7421 * actually added below in case there is no room for
7422 * this chunk. NOTE: we overload the use of "omtu"
7425 if ((auth == NULL) &&
7426 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7427 stcb->asoc.peer_auth_chunks)) {
7428 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7431 /* Here we do NOT factor the r_mtu */
7432 if ((chk->send_size < (int)(mtu - omtu)) ||
7433 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7435 * We probably should glom the mbuf chain
7436 * from the chk->data for control but the
7437 * problem is it becomes yet one more level
7438 * of tracking to do if for some reason
7439 * output fails. Then I have got to
7440 * reconstruct the merged control chain.. el
7441 * yucko.. for now we take the easy way and
7445 * Add an AUTH chunk, if chunk requires it
7446 * save the offset into the chain for AUTH
7448 if ((auth == NULL) &&
7449 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7450 stcb->asoc.peer_auth_chunks))) {
7451 outchain = sctp_add_auth_chunk(outchain,
7456 chk->rec.chunk_id.id);
7457 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7459 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7460 (int)chk->rec.chunk_id.can_take_data,
7461 chk->send_size, chk->copy_by_ref);
7462 if (outchain == NULL) {
7464 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7467 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7468 /* update our MTU size */
7469 if (mtu > (chk->send_size + omtu))
7470 mtu -= (chk->send_size + omtu);
7473 to_out += (chk->send_size + omtu);
7474 /* Do clear IP_DF ? */
7475 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7478 if (chk->rec.chunk_id.can_take_data)
7480 /* Mark things to be removed, if needed */
7481 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7482 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7483 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7484 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7485 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7486 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7487 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7488 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7489 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7490 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7492 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7495 * JRS 5/14/07 - Set the
7496 * flag to say a heartbeat
7501 /* remove these chunks at the end */
7502 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7503 /* turn off the timer */
7504 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7505 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7506 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7512 * Other chunks, since they have
7513 * timers running (i.e. COOKIE or
7514 * ASCONF) we just "trust" that it
7515 * gets sent or retransmitted.
7518 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7521 } else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
7523 * set hb flag since we can
7529 * should sysctl this: don't
7530 * bundle data with ASCONF
7531 * since it requires AUTH
7535 chk->sent = SCTP_DATAGRAM_SENT;
7540 * Ok we are out of room but we can
7541 * output without effecting the
7542 * flight size since this little guy
7543 * is a control only packet.
7546 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7548 * do NOT clear the asconf
7549 * flag as it is used to do
7550 * appropriate source
7551 * address selection.
7555 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7558 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7559 if (outchain == NULL) {
7561 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7563 goto error_out_again;
7565 shdr = mtod(outchain, struct sctphdr *);
7566 shdr->src_port = inp->sctp_lport;
7567 shdr->dest_port = stcb->rport;
7568 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7570 auth_offset += sizeof(struct sctphdr);
7571 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7572 (struct sockaddr *)&net->ro._l_addr,
7573 outchain, auth_offset, auth,
7574 no_fragmentflg, 0, NULL, asconf, so_locked))) {
7575 if (error == ENOBUFS) {
7576 asoc->ifp_had_enobuf = 1;
7577 SCTP_STAT_INCR(sctps_lowlevelerr);
7579 if (from_where == 0) {
7580 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7583 /* error, could not output */
7585 if (*now_filled == 0) {
7586 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7588 *now = net->last_sent_time;
7590 net->last_sent_time = *now;
7594 if (error == EHOSTUNREACH) {
7600 sctp_move_to_an_alt(stcb, asoc, net);
7605 asoc->ifp_had_enobuf = 0;
7606 /* Only HB or ASCONF advances time */
7608 if (*now_filled == 0) {
7609 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7611 *now = net->last_sent_time;
7613 net->last_sent_time = *now;
7618 * increase the number we sent, if a
7619 * cookie is sent we don't tell them
7622 outchain = endoutchain = NULL;
7626 *num_out += ctl_cnt;
7627 /* recalc a clean slate and setup */
7628 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7629 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7631 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7638 /*********************/
7639 /* Data transmission */
7640 /*********************/
7642 * if AUTH for DATA is required and no AUTH has been added
7643 * yet, account for this in the mtu now... if no data can be
7644 * bundled, this adjustment won't matter anyways since the
7645 * packet will be going out...
7647 if ((auth == NULL) &&
7648 sctp_auth_is_required_chunk(SCTP_DATA,
7649 stcb->asoc.peer_auth_chunks)) {
7650 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7652 /* now lets add any data within the MTU constraints */
7653 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7655 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
7656 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7662 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
7663 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7673 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
7675 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
7676 if (no_data_chunks) {
7677 /* let only control go out */
7681 if (net->flight_size >= net->cwnd) {
7682 /* skip this net, no room for data */
7686 nchk = TAILQ_NEXT(chk, sctp_next);
7687 if (chk->whoTo != net) {
7688 /* No, not sent to this net */
7691 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
7693 * strange, we have a chunk that is
7694 * to big for its destination and
7695 * yet no fragment ok flag.
7696 * Something went wrong when the
7697 * PMTU changed...we did not mark
7698 * this chunk for some reason?? I
7699 * will fix it here by letting IP
7700 * fragment it for now and printing
7701 * a warning. This really should not
7704 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
7705 chk->send_size, mtu);
7706 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
7708 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
7709 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
7710 /* ok we will add this one */
7713 * Add an AUTH chunk, if chunk
7714 * requires it, save the offset into
7715 * the chain for AUTH
7717 if ((auth == NULL) &&
7718 (sctp_auth_is_required_chunk(SCTP_DATA,
7719 stcb->asoc.peer_auth_chunks))) {
7721 outchain = sctp_add_auth_chunk(outchain,
7727 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7729 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
7730 chk->send_size, chk->copy_by_ref);
7731 if (outchain == NULL) {
7732 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
7733 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
7734 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7737 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7740 /* upate our MTU size */
7741 /* Do clear IP_DF ? */
7742 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7745 /* unsigned subtraction of mtu */
7746 if (mtu > chk->send_size)
7747 mtu -= chk->send_size;
7750 /* unsigned subtraction of r_mtu */
7751 if (r_mtu > chk->send_size)
7752 r_mtu -= chk->send_size;
7756 to_out += chk->send_size;
7757 if ((to_out > mx_mtu) && no_fragmentflg) {
7759 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
7761 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
7765 chk->window_probe = 0;
7766 data_list[bundle_at++] = chk;
7767 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
7771 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
7772 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
7773 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
7775 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
7777 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
7778 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
7788 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
7790 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
7792 data_list[0]->window_probe = 1;
7793 net->window_probe = 1;
7799 * Must be sent in order of the
7800 * TSN's (on a network)
7804 } /* for (chunk gather loop for this net) */
7805 } /* if asoc.state OPEN */
7806 /* Is there something to send for this destination? */
7808 /* We may need to start a control timer or two */
7810 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
7813 * do NOT clear the asconf flag as it is
7814 * used to do appropriate source address
7819 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7822 /* must start a send timer if data is being sent */
7823 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7825 * no timer running on this destination
7828 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7829 } else if (sctp_cmt_on_off && sctp_cmt_pf && pf_hbflag && ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)
7830 && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7832 * JRS 5/14/07 - If a HB has been sent to a
7833 * PF destination and no T3 timer is
7834 * currently running, start the T3 timer to
7835 * track the HBs that were sent.
7837 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7839 /* Now send it, if there is anything to send :> */
7840 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7841 if (outchain == NULL) {
7843 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7847 shdr = mtod(outchain, struct sctphdr *);
7848 shdr->src_port = inp->sctp_lport;
7849 shdr->dest_port = stcb->rport;
7850 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7852 auth_offset += sizeof(struct sctphdr);
7853 if ((error = sctp_lowlevel_chunk_output(inp,
7856 (struct sockaddr *)&net->ro._l_addr,
7863 asconf, so_locked))) {
7864 /* error, we could not output */
7865 if (error == ENOBUFS) {
7866 SCTP_STAT_INCR(sctps_lowlevelerr);
7867 asoc->ifp_had_enobuf = 1;
7869 if (from_where == 0) {
7870 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7873 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
7875 if (*now_filled == 0) {
7876 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7878 *now = net->last_sent_time;
7880 net->last_sent_time = *now;
7884 if (error == EHOSTUNREACH) {
7886 * Destination went unreachable
7889 sctp_move_to_an_alt(stcb, asoc, net);
7893 * I add this line to be paranoid. As far as
7894 * I can tell the continue, takes us back to
7895 * the top of the for, but just to make sure
7896 * I will reset these again here.
7898 ctl_cnt = bundle_at = 0;
7899 continue; /* This takes us back to the
7900 * for() for the nets. */
7902 asoc->ifp_had_enobuf = 0;
7904 outchain = endoutchain = NULL;
7907 if (bundle_at || hbflag) {
7908 /* For data/asconf and hb set time */
7909 if (*now_filled == 0) {
7910 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7912 *now = net->last_sent_time;
7914 net->last_sent_time = *now;
7918 *num_out += (ctl_cnt + bundle_at);
7921 /* setup for a RTO measurement */
7922 tsns_sent = data_list[0]->rec.data.TSN_seq;
7923 /* fill time if not already filled */
7924 if (*now_filled == 0) {
7925 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
7927 *now = asoc->time_last_sent;
7929 asoc->time_last_sent = *now;
7931 data_list[0]->do_rtt = 1;
7932 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
7933 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
7934 if (sctp_early_fr) {
7935 if (net->flight_size < net->cwnd) {
7936 /* start or restart it */
7937 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7938 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7939 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
7941 SCTP_STAT_INCR(sctps_earlyfrstrout);
7942 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
7944 /* stop it if its running */
7945 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7946 SCTP_STAT_INCR(sctps_earlyfrstpout);
7947 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7948 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
7957 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7958 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
7961 if (old_startat == NULL) {
7962 old_startat = send_start_at;
7963 send_start_at = TAILQ_FIRST(&asoc->nets);
7965 goto again_one_more_time;
7968 * At the end there should be no NON timed chunks hanging on this
7971 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7972 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
7974 if ((*num_out == 0) && (*reason_code == 0)) {
7979 sctp_clean_up_ctl(stcb, asoc);
7984 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
7987 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
7988 * the control chunk queue.
7990 struct sctp_chunkhdr *hdr;
7991 struct sctp_tmit_chunk *chk;
7994 SCTP_TCB_LOCK_ASSERT(stcb);
7995 sctp_alloc_a_chunk(stcb, chk);
7998 sctp_m_freem(op_err);
8001 chk->copy_by_ref = 0;
8002 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8003 if (op_err == NULL) {
8004 sctp_free_a_chunk(stcb, chk);
8009 while (mat != NULL) {
8010 chk->send_size += SCTP_BUF_LEN(mat);
8011 mat = SCTP_BUF_NEXT(mat);
8013 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8014 chk->rec.chunk_id.can_take_data = 1;
8015 chk->sent = SCTP_DATAGRAM_UNSENT;
8018 chk->asoc = &stcb->asoc;
8020 chk->whoTo = chk->asoc->primary_destination;
8021 atomic_add_int(&chk->whoTo->ref_count, 1);
8022 hdr = mtod(op_err, struct sctp_chunkhdr *);
8023 hdr->chunk_type = SCTP_OPERATION_ERROR;
8024 hdr->chunk_flags = 0;
8025 hdr->chunk_length = htons(chk->send_size);
8026 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8029 chk->asoc->ctrl_queue_cnt++;
8033 sctp_send_cookie_echo(struct mbuf *m,
8035 struct sctp_tcb *stcb,
8036 struct sctp_nets *net)
8039 * pull out the cookie and put it at the front of the control chunk
8043 struct mbuf *cookie;
8044 struct sctp_paramhdr parm, *phdr;
8045 struct sctp_chunkhdr *hdr;
8046 struct sctp_tmit_chunk *chk;
8047 uint16_t ptype, plen;
8049 /* First find the cookie in the param area */
8051 at = offset + sizeof(struct sctp_init_chunk);
8053 SCTP_TCB_LOCK_ASSERT(stcb);
8055 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8059 ptype = ntohs(phdr->param_type);
8060 plen = ntohs(phdr->param_length);
8061 if (ptype == SCTP_STATE_COOKIE) {
8064 /* found the cookie */
8065 if ((pad = (plen % 4))) {
8068 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8069 if (cookie == NULL) {
8073 #ifdef SCTP_MBUF_LOGGING
8074 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
8079 if (SCTP_BUF_IS_EXTENDED(mat)) {
8080 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8082 mat = SCTP_BUF_NEXT(mat);
8088 at += SCTP_SIZE32(plen);
8090 if (cookie == NULL) {
8091 /* Did not find the cookie */
8094 /* ok, we got the cookie lets change it into a cookie echo chunk */
8096 /* first the change from param to cookie */
8097 hdr = mtod(cookie, struct sctp_chunkhdr *);
8098 hdr->chunk_type = SCTP_COOKIE_ECHO;
8099 hdr->chunk_flags = 0;
8100 /* get the chunk stuff now and place it in the FRONT of the queue */
8101 sctp_alloc_a_chunk(stcb, chk);
8104 sctp_m_freem(cookie);
8107 chk->copy_by_ref = 0;
8108 chk->send_size = plen;
8109 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8110 chk->rec.chunk_id.can_take_data = 0;
8111 chk->sent = SCTP_DATAGRAM_UNSENT;
8113 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8114 chk->asoc = &stcb->asoc;
8116 chk->whoTo = chk->asoc->primary_destination;
8117 atomic_add_int(&chk->whoTo->ref_count, 1);
8118 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8119 chk->asoc->ctrl_queue_cnt++;
8124 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8128 struct sctp_nets *net)
8131 * take a HB request and make it into a HB ack and send it.
8133 struct mbuf *outchain;
8134 struct sctp_chunkhdr *chdr;
8135 struct sctp_tmit_chunk *chk;
8139 /* must have a net pointer */
8142 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8143 if (outchain == NULL) {
8144 /* gak out of memory */
8147 #ifdef SCTP_MBUF_LOGGING
8148 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
8153 if (SCTP_BUF_IS_EXTENDED(mat)) {
8154 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8156 mat = SCTP_BUF_NEXT(mat);
8160 chdr = mtod(outchain, struct sctp_chunkhdr *);
8161 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8162 chdr->chunk_flags = 0;
8163 if (chk_length % 4) {
8165 uint32_t cpthis = 0;
8168 padlen = 4 - (chk_length % 4);
8169 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8171 sctp_alloc_a_chunk(stcb, chk);
8174 sctp_m_freem(outchain);
8177 chk->copy_by_ref = 0;
8178 chk->send_size = chk_length;
8179 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8180 chk->rec.chunk_id.can_take_data = 1;
8181 chk->sent = SCTP_DATAGRAM_UNSENT;
8184 chk->asoc = &stcb->asoc;
8185 chk->data = outchain;
8187 atomic_add_int(&chk->whoTo->ref_count, 1);
8188 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8189 chk->asoc->ctrl_queue_cnt++;
8193 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8195 /* formulate and queue a cookie-ack back to sender */
8196 struct mbuf *cookie_ack;
8197 struct sctp_chunkhdr *hdr;
8198 struct sctp_tmit_chunk *chk;
8201 SCTP_TCB_LOCK_ASSERT(stcb);
8203 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8204 if (cookie_ack == NULL) {
8208 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8209 sctp_alloc_a_chunk(stcb, chk);
8212 sctp_m_freem(cookie_ack);
8215 chk->copy_by_ref = 0;
8216 chk->send_size = sizeof(struct sctp_chunkhdr);
8217 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8218 chk->rec.chunk_id.can_take_data = 1;
8219 chk->sent = SCTP_DATAGRAM_UNSENT;
8222 chk->asoc = &stcb->asoc;
8223 chk->data = cookie_ack;
8224 if (chk->asoc->last_control_chunk_from != NULL) {
8225 chk->whoTo = chk->asoc->last_control_chunk_from;
8227 chk->whoTo = chk->asoc->primary_destination;
8229 atomic_add_int(&chk->whoTo->ref_count, 1);
8230 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8231 hdr->chunk_type = SCTP_COOKIE_ACK;
8232 hdr->chunk_flags = 0;
8233 hdr->chunk_length = htons(chk->send_size);
8234 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8235 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8236 chk->asoc->ctrl_queue_cnt++;
8242 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8244 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8245 struct mbuf *m_shutdown_ack;
8246 struct sctp_shutdown_ack_chunk *ack_cp;
8247 struct sctp_tmit_chunk *chk;
8249 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8250 if (m_shutdown_ack == NULL) {
8254 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8255 sctp_alloc_a_chunk(stcb, chk);
8258 sctp_m_freem(m_shutdown_ack);
8261 chk->copy_by_ref = 0;
8262 chk->send_size = sizeof(struct sctp_chunkhdr);
8263 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8264 chk->rec.chunk_id.can_take_data = 1;
8265 chk->sent = SCTP_DATAGRAM_UNSENT;
8268 chk->asoc = &stcb->asoc;
8269 chk->data = m_shutdown_ack;
8271 atomic_add_int(&net->ref_count, 1);
8273 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8274 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8275 ack_cp->ch.chunk_flags = 0;
8276 ack_cp->ch.chunk_length = htons(chk->send_size);
8277 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8278 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8279 chk->asoc->ctrl_queue_cnt++;
8284 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8286 /* formulate and queue a SHUTDOWN to the sender */
8287 struct mbuf *m_shutdown;
8288 struct sctp_shutdown_chunk *shutdown_cp;
8289 struct sctp_tmit_chunk *chk;
8291 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8292 if (m_shutdown == NULL) {
8296 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8297 sctp_alloc_a_chunk(stcb, chk);
8300 sctp_m_freem(m_shutdown);
8303 chk->copy_by_ref = 0;
8304 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8305 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8306 chk->rec.chunk_id.can_take_data = 1;
8307 chk->sent = SCTP_DATAGRAM_UNSENT;
8310 chk->asoc = &stcb->asoc;
8311 chk->data = m_shutdown;
8313 atomic_add_int(&net->ref_count, 1);
8315 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8316 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8317 shutdown_cp->ch.chunk_flags = 0;
8318 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8319 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8320 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8321 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8322 chk->asoc->ctrl_queue_cnt++;
8327 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8330 * formulate and queue an ASCONF to the peer. ASCONF parameters
8331 * should be queued on the assoc queue.
8333 struct sctp_tmit_chunk *chk;
8334 struct mbuf *m_asconf;
8335 struct sctp_asconf_chunk *acp;
8338 SCTP_TCB_LOCK_ASSERT(stcb);
8339 /* compose an ASCONF chunk, maximum length is PMTU */
8340 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8341 if (m_asconf == NULL) {
8344 acp = mtod(m_asconf, struct sctp_asconf_chunk *);
8345 sctp_alloc_a_chunk(stcb, chk);
8348 sctp_m_freem(m_asconf);
8351 chk->copy_by_ref = 0;
8352 chk->data = m_asconf;
8353 chk->send_size = len;
8354 chk->rec.chunk_id.id = SCTP_ASCONF;
8355 chk->rec.chunk_id.can_take_data = 0;
8356 chk->sent = SCTP_DATAGRAM_UNSENT;
8359 chk->asoc = &stcb->asoc;
8360 chk->whoTo = chk->asoc->primary_destination;
8361 atomic_add_int(&chk->whoTo->ref_count, 1);
8362 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8363 chk->asoc->ctrl_queue_cnt++;
8368 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8371 * formulate and queue a asconf-ack back to sender. the asconf-ack
8372 * must be stored in the tcb.
8374 struct sctp_tmit_chunk *chk;
8375 struct sctp_asconf_ack *ack, *latest_ack;
8376 struct mbuf *m_ack, *m;
8377 struct sctp_nets *net = NULL;
8379 SCTP_TCB_LOCK_ASSERT(stcb);
8380 /* Get the latest ASCONF-ACK */
8381 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8382 if (latest_ack == NULL) {
8385 if (latest_ack->last_sent_to != NULL &&
8386 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8387 /* we're doing a retransmission */
8388 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8391 if (stcb->asoc.last_control_chunk_from == NULL)
8392 net = stcb->asoc.primary_destination;
8394 net = stcb->asoc.last_control_chunk_from;
8398 if (stcb->asoc.last_control_chunk_from == NULL)
8399 net = stcb->asoc.primary_destination;
8401 net = stcb->asoc.last_control_chunk_from;
8403 latest_ack->last_sent_to = net;
8405 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8406 if (ack->data == NULL) {
8409 /* copy the asconf_ack */
8410 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8411 if (m_ack == NULL) {
8412 /* couldn't copy it */
8415 #ifdef SCTP_MBUF_LOGGING
8416 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
8421 if (SCTP_BUF_IS_EXTENDED(mat)) {
8422 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8424 mat = SCTP_BUF_NEXT(mat);
8429 sctp_alloc_a_chunk(stcb, chk);
8433 sctp_m_freem(m_ack);
8436 chk->copy_by_ref = 0;
8443 chk->send_size = ack->len;
8444 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8445 chk->rec.chunk_id.can_take_data = 1;
8446 chk->sent = SCTP_DATAGRAM_UNSENT;
8448 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8449 chk->asoc = &stcb->asoc;
8450 atomic_add_int(&chk->whoTo->ref_count, 1);
8452 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8453 chk->asoc->ctrl_queue_cnt++;
8460 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8461 struct sctp_tcb *stcb,
8462 struct sctp_association *asoc,
8463 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8464 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8470 * send out one MTU of retransmission. If fast_retransmit is
8471 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8472 * rwnd. For a Cookie or Asconf in the control chunk queue we
8473 * retransmit them by themselves.
8475 * For data chunks we will pick out the lowest TSN's in the sent_queue
8476 * marked for resend and bundle them all together (up to a MTU of
8477 * destination). The address to send to should have been
8478 * selected/changed where the retransmission was marked (i.e. in FR
8479 * or t3-timeout routines).
8481 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8482 struct sctp_tmit_chunk *chk, *fwd;
8483 struct mbuf *m, *endofchain;
8484 struct sctphdr *shdr;
8486 struct sctp_nets *net = NULL;
8487 uint32_t tsns_sent = 0;
8488 int no_fragmentflg, bundle_at, cnt_thru;
8490 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8491 struct sctp_auth_chunk *auth = NULL;
8492 uint32_t auth_offset = 0;
8495 SCTP_TCB_LOCK_ASSERT(stcb);
8496 tmr_started = ctl_cnt = bundle_at = error = 0;
8502 endofchain = m = NULL;
8503 #ifdef SCTP_AUDITING_ENABLED
8504 sctp_audit_log(0xC3, 1);
8506 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8507 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8508 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8509 asoc->sent_queue_retran_cnt);
8510 asoc->sent_queue_cnt = 0;
8511 asoc->sent_queue_cnt_removeable = 0;
8512 /* send back 0/0 so we enter normal transmission */
8516 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8517 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8518 (chk->rec.chunk_id.id == SCTP_ASCONF) ||
8519 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8520 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8521 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8522 if (chk != asoc->str_reset) {
8524 * not eligible for retran if its
8531 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8535 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8540 * Add an AUTH chunk, if chunk requires it save the
8541 * offset into the chain for AUTH
8543 if ((auth == NULL) &&
8544 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8545 stcb->asoc.peer_auth_chunks))) {
8546 m = sctp_add_auth_chunk(m, &endofchain,
8547 &auth, &auth_offset,
8549 chk->rec.chunk_id.id);
8551 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8557 /* do we have control chunks to retransmit? */
8559 /* Start a timer no matter if we suceed or fail */
8560 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8561 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8562 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8563 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8565 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8567 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8570 shdr = mtod(m, struct sctphdr *);
8571 shdr->src_port = inp->sctp_lport;
8572 shdr->dest_port = stcb->rport;
8573 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8575 auth_offset += sizeof(struct sctphdr);
8576 chk->snd_count++; /* update our count */
8578 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8579 (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8580 auth, no_fragmentflg, 0, NULL, asconf, so_locked))) {
8581 SCTP_STAT_INCR(sctps_lowlevelerr);
8584 m = endofchain = NULL;
8588 * We don't want to mark the net->sent time here since this
8589 * we use this for HB and retrans cannot measure RTT
8591 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8593 chk->sent = SCTP_DATAGRAM_SENT;
8594 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8598 /* Clean up the fwd-tsn list */
8599 sctp_clean_up_ctl(stcb, asoc);
8604 * Ok, it is just data retransmission we need to do or that and a
8605 * fwd-tsn with it all.
8607 if (TAILQ_EMPTY(&asoc->sent_queue)) {
8608 return (SCTP_RETRAN_DONE);
8610 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8611 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8612 /* not yet open, resend the cookie and that is it */
8615 #ifdef SCTP_AUDITING_ENABLED
8616 sctp_auditing(20, inp, stcb, NULL);
8618 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8619 if (chk->sent != SCTP_DATAGRAM_RESEND) {
8620 /* No, not sent to this net or not ready for rtx */
8623 if ((sctp_max_retran_chunk) && (chk->snd_count >= sctp_max_retran_chunk)) {
8624 /* Gak, we have exceeded max unlucky retran, abort! */
8625 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8627 sctp_max_retran_chunk);
8628 atomic_add_int(&stcb->asoc.refcnt, 1);
8629 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8630 SCTP_TCB_LOCK(stcb);
8631 atomic_subtract_int(&stcb->asoc.refcnt, 1);
8632 return (SCTP_RETRAN_EXIT);
8634 /* pick up the net */
8636 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8637 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8639 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8642 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8643 /* No room in peers rwnd */
8646 tsn = asoc->last_acked_seq + 1;
8647 if (tsn == chk->rec.data.TSN_seq) {
8649 * we make a special exception for this
8650 * case. The peer has no rwnd but is missing
8651 * the lowest chunk.. which is probably what
8652 * is holding up the rwnd.
8654 goto one_chunk_around;
8659 if (asoc->peers_rwnd < mtu) {
8661 if ((asoc->peers_rwnd == 0) &&
8662 (asoc->total_flight == 0)) {
8663 chk->window_probe = 1;
8664 chk->whoTo->window_probe = 1;
8667 #ifdef SCTP_AUDITING_ENABLED
8668 sctp_audit_log(0xC3, 2);
8672 net->fast_retran_ip = 0;
8673 if (chk->rec.data.doing_fast_retransmit == 0) {
8675 * if no FR in progress skip destination that have
8676 * flight_size > cwnd.
8678 if (net->flight_size >= net->cwnd) {
8683 * Mark the destination net to have FR recovery
8687 net->fast_retran_ip = 1;
8691 * if no AUTH is yet included and this chunk requires it,
8692 * make sure to account for it. We don't apply the size
8693 * until the AUTH chunk is actually added below in case
8694 * there is no room for this chunk.
8696 if ((auth == NULL) &&
8697 sctp_auth_is_required_chunk(SCTP_DATA,
8698 stcb->asoc.peer_auth_chunks)) {
8699 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8703 if ((chk->send_size <= (mtu - dmtu)) ||
8704 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8705 /* ok we will add this one */
8706 if ((auth == NULL) &&
8707 (sctp_auth_is_required_chunk(SCTP_DATA,
8708 stcb->asoc.peer_auth_chunks))) {
8709 m = sctp_add_auth_chunk(m, &endofchain,
8710 &auth, &auth_offset,
8713 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8715 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8718 /* Do clear IP_DF ? */
8719 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8722 /* upate our MTU size */
8723 if (mtu > (chk->send_size + dmtu))
8724 mtu -= (chk->send_size + dmtu);
8727 data_list[bundle_at++] = chk;
8728 if (one_chunk && (asoc->total_flight <= 0)) {
8729 SCTP_STAT_INCR(sctps_windowprobed);
8732 if (one_chunk == 0) {
8734 * now are there anymore forward from chk to pick
8737 fwd = TAILQ_NEXT(chk, sctp_next);
8739 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
8740 /* Nope, not for retran */
8741 fwd = TAILQ_NEXT(fwd, sctp_next);
8744 if (fwd->whoTo != net) {
8745 /* Nope, not the net in question */
8746 fwd = TAILQ_NEXT(fwd, sctp_next);
8749 if ((auth == NULL) &&
8750 sctp_auth_is_required_chunk(SCTP_DATA,
8751 stcb->asoc.peer_auth_chunks)) {
8752 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8755 if (fwd->send_size <= (mtu - dmtu)) {
8756 if ((auth == NULL) &&
8757 (sctp_auth_is_required_chunk(SCTP_DATA,
8758 stcb->asoc.peer_auth_chunks))) {
8759 m = sctp_add_auth_chunk(m,
8761 &auth, &auth_offset,
8765 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
8767 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8770 /* Do clear IP_DF ? */
8771 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8774 /* upate our MTU size */
8775 if (mtu > (fwd->send_size + dmtu))
8776 mtu -= (fwd->send_size + dmtu);
8779 data_list[bundle_at++] = fwd;
8780 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8783 fwd = TAILQ_NEXT(fwd, sctp_next);
8785 /* can't fit so we are done */
8790 /* Is there something to send for this destination? */
8793 * No matter if we fail/or suceed we should start a
8794 * timer. A failure is like a lost IP packet :-)
8796 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8798 * no timer running on this destination
8801 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8804 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8806 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8809 shdr = mtod(m, struct sctphdr *);
8810 shdr->src_port = inp->sctp_lport;
8811 shdr->dest_port = stcb->rport;
8812 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8814 auth_offset += sizeof(struct sctphdr);
8815 /* Now lets send it, if there is anything to send :> */
8816 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8817 (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
8818 auth, no_fragmentflg, 0, NULL, asconf, so_locked))) {
8819 /* error, we could not output */
8820 SCTP_STAT_INCR(sctps_lowlevelerr);
8823 m = endofchain = NULL;
8828 * We don't want to mark the net->sent time here
8829 * since this we use this for HB and retrans cannot
8832 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
8834 /* For auto-close */
8836 if (*now_filled == 0) {
8837 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8838 *now = asoc->time_last_sent;
8841 asoc->time_last_sent = *now;
8843 *cnt_out += bundle_at;
8844 #ifdef SCTP_AUDITING_ENABLED
8845 sctp_audit_log(0xC4, bundle_at);
8848 tsns_sent = data_list[0]->rec.data.TSN_seq;
8850 for (i = 0; i < bundle_at; i++) {
8851 SCTP_STAT_INCR(sctps_sendretransdata);
8852 data_list[i]->sent = SCTP_DATAGRAM_SENT;
8854 * When we have a revoked data, and we
8855 * retransmit it, then we clear the revoked
8856 * flag since this flag dictates if we
8857 * subtracted from the fs
8859 if (data_list[i]->rec.data.chunk_was_revoked) {
8860 /* Deflate the cwnd */
8861 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
8862 data_list[i]->rec.data.chunk_was_revoked = 0;
8864 data_list[i]->snd_count++;
8865 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8866 /* record the time */
8867 data_list[i]->sent_rcv_time = asoc->time_last_sent;
8868 if (data_list[i]->book_size_scale) {
8870 * need to double the book size on
8873 data_list[i]->book_size_scale = 0;
8875 * Since we double the booksize, we
8876 * must also double the output queue
8877 * size, since this get shrunk when
8878 * we free by this amount.
8880 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
8881 data_list[i]->book_size *= 2;
8885 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
8886 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8887 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
8889 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8890 (uint32_t) (data_list[i]->send_size +
8891 sctp_peer_chunk_oh));
8893 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
8894 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
8895 data_list[i]->whoTo->flight_size,
8896 data_list[i]->book_size,
8897 (uintptr_t) data_list[i]->whoTo,
8898 data_list[i]->rec.data.TSN_seq);
8900 sctp_flight_size_increase(data_list[i]);
8901 sctp_total_flight_increase(stcb, data_list[i]);
8902 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8903 /* SWS sender side engages */
8904 asoc->peers_rwnd = 0;
8907 (data_list[i]->rec.data.doing_fast_retransmit)) {
8908 SCTP_STAT_INCR(sctps_sendfastretrans);
8909 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
8910 (tmr_started == 0)) {
8912 * ok we just fast-retrans'd
8913 * the lowest TSN, i.e the
8914 * first on the list. In
8915 * this case we want to give
8916 * some more time to get a
8917 * SACK back without a
8920 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
8921 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
8922 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8926 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8927 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
8929 #ifdef SCTP_AUDITING_ENABLED
8930 sctp_auditing(21, inp, stcb, NULL);
8936 if (asoc->sent_queue_retran_cnt <= 0) {
8937 /* all done we have no more to retran */
8938 asoc->sent_queue_retran_cnt = 0;
8942 /* No more room in rwnd */
8945 /* stop the for loop here. we sent out a packet */
8953 sctp_timer_validation(struct sctp_inpcb *inp,
8954 struct sctp_tcb *stcb,
8955 struct sctp_association *asoc,
8958 struct sctp_nets *net;
8960 /* Validate that a timer is running somewhere */
8961 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8962 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8963 /* Here is a timer */
8967 SCTP_TCB_LOCK_ASSERT(stcb);
8968 /* Gak, we did not have a timer somewhere */
8969 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
8970 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
8975 sctp_chunk_output(struct sctp_inpcb *inp,
8976 struct sctp_tcb *stcb,
8979 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8985 * Ok this is the generic chunk service queue. we must do the
8987 * - See if there are retransmits pending, if so we must
8989 * - Service the stream queue that is next, moving any
8990 * message (note I must get a complete message i.e.
8991 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
8993 * - Check to see if the cwnd/rwnd allows any output, if so we
8994 * go ahead and fomulate and send the low level chunks. Making sure
8995 * to combine any control in the control chunk queue also.
8997 struct sctp_association *asoc;
8998 struct sctp_nets *net;
8999 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9000 burst_cnt = 0, burst_limit = 0;
9005 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9007 int fr_done, tot_frs = 0;
9010 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9011 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9017 SCTP_TCB_LOCK_ASSERT(stcb);
9019 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9021 if ((un_sent <= 0) &&
9022 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9023 (asoc->sent_queue_retran_cnt == 0)) {
9024 /* Nothing to do unless there is something to be sent left */
9028 * Do we have something to send, data or control AND a sack timer
9029 * running, if so piggy-back the sack.
9031 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9032 sctp_send_sack(stcb);
9033 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9035 while (asoc->sent_queue_retran_cnt) {
9037 * Ok, it is retransmission time only, we send out only ONE
9038 * packet with a single call off to the retran code.
9040 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9042 * Special hook for handling cookiess discarded
9043 * by peer that carried data. Send cookie-ack only
9044 * and then the next call with get the retran's.
9046 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9047 &cwnd_full, from_where,
9048 &now, &now_filled, frag_point, so_locked);
9050 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9051 /* if its not from a HB then do it */
9053 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9059 * its from any other place, we don't allow retran
9060 * output (only control)
9065 /* Can't send anymore */
9067 * now lets push out control by calling med-level
9068 * output once. this assures that we WILL send HB's
9071 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9072 &cwnd_full, from_where,
9073 &now, &now_filled, frag_point, so_locked);
9074 #ifdef SCTP_AUDITING_ENABLED
9075 sctp_auditing(8, inp, stcb, NULL);
9077 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9082 * The count was off.. retran is not happening so do
9083 * the normal retransmission.
9085 #ifdef SCTP_AUDITING_ENABLED
9086 sctp_auditing(9, inp, stcb, NULL);
9088 if (ret == SCTP_RETRAN_EXIT) {
9093 if (from_where == SCTP_OUTPUT_FROM_T3) {
9094 /* Only one transmission allowed out of a timeout */
9095 #ifdef SCTP_AUDITING_ENABLED
9096 sctp_auditing(10, inp, stcb, NULL);
9098 /* Push out any control */
9099 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
9100 &now, &now_filled, frag_point, so_locked);
9103 if (tot_frs > asoc->max_burst) {
9104 /* Hit FR burst limit */
9107 if ((num_out == 0) && (ret == 0)) {
9109 /* No more retrans to send */
9113 #ifdef SCTP_AUDITING_ENABLED
9114 sctp_auditing(12, inp, stcb, NULL);
9116 /* Check for bad destinations, if they exist move chunks around. */
9117 burst_limit = asoc->max_burst;
9118 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9119 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9120 SCTP_ADDR_NOT_REACHABLE) {
9122 * if possible move things off of this address we
9123 * still may send below due to the dormant state but
9124 * we try to find an alternate address to send to
9125 * and if we have one we move all queued data on the
9126 * out wheel to this alternate address.
9128 if (net->ref_count > 1)
9129 sctp_move_to_an_alt(stcb, asoc, net);
9130 } else if (sctp_cmt_on_off && sctp_cmt_pf && ((net->dest_state & SCTP_ADDR_PF) ==
9133 * JRS 5/14/07 - If CMT PF is on and the current
9134 * destination is in PF state, move all queued data
9135 * to an alternate desination.
9137 if (net->ref_count > 1)
9138 sctp_move_to_an_alt(stcb, asoc, net);
9141 * if ((asoc->sat_network) || (net->addr_is_local))
9142 * { burst_limit = asoc->max_burst *
9143 * SCTP_SAT_NETWORK_BURST_INCR; }
9145 if (sctp_use_cwnd_based_maxburst) {
9146 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9148 * JRS - Use the congestion control
9149 * given in the congestion control
9152 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9153 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
9154 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9156 SCTP_STAT_INCR(sctps_maxburstqueued);
9158 net->fast_retran_ip = 0;
9160 if (net->flight_size == 0) {
9161 /* Should be decaying the cwnd here */
9171 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9172 &reason_code, 0, &cwnd_full, from_where,
9173 &now, &now_filled, frag_point, so_locked);
9175 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9176 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
9177 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9179 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
9180 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9181 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9185 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9189 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
9190 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9192 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9197 * When nagle is on, we look at how much is un_sent, then
9198 * if its smaller than an MTU and we have data in
9201 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9202 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count)
9203 * sizeof(struct sctp_data_chunk)));
9204 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9205 (stcb->asoc.total_flight > 0)) {
9209 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9210 TAILQ_EMPTY(&asoc->send_queue) &&
9211 TAILQ_EMPTY(&asoc->out_wheel)) {
9212 /* Nothing left to send */
9215 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9216 /* Nothing left to send */
9219 } while (num_out && (sctp_use_cwnd_based_maxburst ||
9220 (burst_cnt < burst_limit)));
9222 if (sctp_use_cwnd_based_maxburst == 0) {
9223 if (burst_cnt >= burst_limit) {
9224 SCTP_STAT_INCR(sctps_maxburstqueued);
9225 asoc->burst_limit_applied = 1;
9226 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
9227 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9230 asoc->burst_limit_applied = 0;
9233 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
9234 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9236 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9240 * Now we need to clean up the control chunk chain if a ECNE is on
9241 * it. It must be marked as UNSENT again so next call will continue
9242 * to send it until such time that we get a CWR, to remove it.
9244 if (stcb->asoc.ecn_echo_cnt_onq)
9245 sctp_fix_ecn_echo(asoc);
9251 sctp_output(inp, m, addr, control, p, flags)
9252 struct sctp_inpcb *inp;
9254 struct sockaddr *addr;
9255 struct mbuf *control;
9260 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9263 if (inp->sctp_socket == NULL) {
9264 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9267 return (sctp_sosend(inp->sctp_socket,
9277 send_forward_tsn(struct sctp_tcb *stcb,
9278 struct sctp_association *asoc)
9280 struct sctp_tmit_chunk *chk;
9281 struct sctp_forward_tsn_chunk *fwdtsn;
9283 SCTP_TCB_LOCK_ASSERT(stcb);
9284 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9285 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9286 /* mark it to unsent */
9287 chk->sent = SCTP_DATAGRAM_UNSENT;
9289 /* Do we correct its output location? */
9290 if (chk->whoTo != asoc->primary_destination) {
9291 sctp_free_remote_addr(chk->whoTo);
9292 chk->whoTo = asoc->primary_destination;
9293 atomic_add_int(&chk->whoTo->ref_count, 1);
9295 goto sctp_fill_in_rest;
9298 /* Ok if we reach here we must build one */
9299 sctp_alloc_a_chunk(stcb, chk);
9303 chk->copy_by_ref = 0;
9304 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9305 chk->rec.chunk_id.can_take_data = 0;
9309 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9310 if (chk->data == NULL) {
9311 sctp_free_a_chunk(stcb, chk);
9314 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9315 chk->sent = SCTP_DATAGRAM_UNSENT;
9317 chk->whoTo = asoc->primary_destination;
9318 atomic_add_int(&chk->whoTo->ref_count, 1);
9319 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9320 asoc->ctrl_queue_cnt++;
9323 * Here we go through and fill out the part that deals with
9324 * stream/seq of the ones we skip.
9326 SCTP_BUF_LEN(chk->data) = 0;
9328 struct sctp_tmit_chunk *at, *tp1, *last;
9329 struct sctp_strseq *strseq;
9330 unsigned int cnt_of_space, i, ovh;
9331 unsigned int space_needed;
9332 unsigned int cnt_of_skipped = 0;
9334 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9335 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9336 /* no more to look at */
9339 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9340 /* We don't report these */
9345 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9346 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9348 cnt_of_space = M_TRAILINGSPACE(chk->data);
9350 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9351 ovh = SCTP_MIN_OVERHEAD;
9353 ovh = SCTP_MIN_V4_OVERHEAD;
9355 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9356 /* trim to a mtu size */
9357 cnt_of_space = asoc->smallest_mtu - ovh;
9359 if (cnt_of_space < space_needed) {
9361 * ok we must trim down the chunk by lowering the
9362 * advance peer ack point.
9364 cnt_of_skipped = (cnt_of_space -
9365 ((sizeof(struct sctp_forward_tsn_chunk)) /
9366 sizeof(struct sctp_strseq)));
9368 * Go through and find the TSN that will be the one
9371 at = TAILQ_FIRST(&asoc->sent_queue);
9372 for (i = 0; i < cnt_of_skipped; i++) {
9373 tp1 = TAILQ_NEXT(at, sctp_next);
9378 * last now points to last one I can report, update
9381 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9382 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9384 chk->send_size = space_needed;
9385 /* Setup the chunk */
9386 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9387 fwdtsn->ch.chunk_length = htons(chk->send_size);
9388 fwdtsn->ch.chunk_flags = 0;
9389 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9390 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9391 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9392 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9393 SCTP_BUF_LEN(chk->data) = chk->send_size;
9396 * Move pointer to after the fwdtsn and transfer to the
9399 strseq = (struct sctp_strseq *)fwdtsn;
9401 * Now populate the strseq list. This is done blindly
9402 * without pulling out duplicate stream info. This is
9403 * inefficent but won't harm the process since the peer will
9404 * look at these in sequence and will thus release anything.
9405 * It could mean we exceed the PMTU and chop off some that
9406 * we could have included.. but this is unlikely (aka 1432/4
9407 * would mean 300+ stream seq's would have to be reported in
9408 * one FWD-TSN. With a bit of work we can later FIX this to
9409 * optimize and pull out duplcates.. but it does add more
9410 * overhead. So for now... not!
9412 at = TAILQ_FIRST(&asoc->sent_queue);
9413 for (i = 0; i < cnt_of_skipped; i++) {
9414 tp1 = TAILQ_NEXT(at, sctp_next);
9415 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9416 /* We don't report these */
9421 strseq->stream = ntohs(at->rec.data.stream_number);
9422 strseq->sequence = ntohs(at->rec.data.stream_seq);
9432 sctp_send_sack(struct sctp_tcb *stcb)
9435 * Queue up a SACK in the control queue. We must first check to see
9436 * if a SACK is somehow on the control queue. If so, we will take
9437 * and and remove the old one.
9439 struct sctp_association *asoc;
9440 struct sctp_tmit_chunk *chk, *a_chk;
9441 struct sctp_sack_chunk *sack;
9442 struct sctp_gap_ack_block *gap_descriptor;
9443 struct sack_track *selector;
9448 int limit_reached = 0;
9449 unsigned int i, jstart, siz, j;
9450 unsigned int num_gap_blocks = 0, space;
9456 SCTP_TCB_LOCK_ASSERT(stcb);
9457 if (asoc->last_data_chunk_from == NULL) {
9458 /* Hmm we never received anything */
9461 sctp_set_rwnd(stcb, asoc);
9462 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9463 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9464 /* Hmm, found a sack already on queue, remove it */
9465 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9466 asoc->ctrl_queue_cnt++;
9469 sctp_m_freem(a_chk->data);
9472 sctp_free_remote_addr(a_chk->whoTo);
9473 a_chk->whoTo = NULL;
9477 if (a_chk == NULL) {
9478 sctp_alloc_a_chunk(stcb, a_chk);
9479 if (a_chk == NULL) {
9480 /* No memory so we drop the idea, and set a timer */
9481 if (stcb->asoc.delayed_ack) {
9482 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9483 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9484 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9485 stcb->sctp_ep, stcb, NULL);
9487 stcb->asoc.send_sack = 1;
9491 a_chk->copy_by_ref = 0;
9492 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9493 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9494 a_chk->rec.chunk_id.can_take_data = 1;
9496 /* Clear our pkt counts */
9497 asoc->data_pkts_seen = 0;
9500 a_chk->snd_count = 0;
9501 a_chk->send_size = 0; /* fill in later */
9502 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9503 a_chk->whoTo = NULL;
9505 if ((asoc->numduptsns) ||
9506 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9509 * Ok, we have some duplicates or the destination for the
9510 * sack is unreachable, lets see if we can select an
9511 * alternate than asoc->last_data_chunk_from
9513 if ((!(asoc->last_data_chunk_from->dest_state &
9514 SCTP_ADDR_NOT_REACHABLE)) &&
9515 (asoc->used_alt_onsack > asoc->numnets)) {
9516 /* We used an alt last time, don't this time */
9517 a_chk->whoTo = NULL;
9519 asoc->used_alt_onsack++;
9520 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9522 if (a_chk->whoTo == NULL) {
9523 /* Nope, no alternate */
9524 a_chk->whoTo = asoc->last_data_chunk_from;
9525 asoc->used_alt_onsack = 0;
9529 * No duplicates so we use the last place we received data
9532 asoc->used_alt_onsack = 0;
9533 a_chk->whoTo = asoc->last_data_chunk_from;
9536 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9538 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9540 space_req = sizeof(struct sctp_sack_chunk);
9542 /* gaps get a cluster */
9543 space_req = MCLBYTES;
9545 /* Ok now lets formulate a MBUF with our sack */
9546 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9547 if ((a_chk->data == NULL) ||
9548 (a_chk->whoTo == NULL)) {
9549 /* rats, no mbuf memory */
9551 /* was a problem with the destination */
9552 sctp_m_freem(a_chk->data);
9555 sctp_free_a_chunk(stcb, a_chk);
9556 /* sa_ignore NO_NULL_CHK */
9557 if (stcb->asoc.delayed_ack) {
9558 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9559 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9560 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9561 stcb->sctp_ep, stcb, NULL);
9563 stcb->asoc.send_sack = 1;
9567 /* ok, lets go through and fill it in */
9568 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9569 space = M_TRAILINGSPACE(a_chk->data);
9570 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9571 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9573 limit = mtod(a_chk->data, caddr_t);
9576 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9577 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9578 /* 0x01 is used by nonce for ecn */
9579 if ((sctp_ecn_enable) &&
9581 (asoc->peer_supports_ecn_nonce))
9582 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9584 sack->ch.chunk_flags = 0;
9586 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
9588 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9589 * received, then set high bit to 1, else 0. Reset
9592 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9593 asoc->cmt_dac_pkts_rcvd = 0;
9595 #ifdef SCTP_ASOCLOG_OF_TSNS
9596 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9597 stcb->asoc.cumack_log_atsnt++;
9598 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9599 stcb->asoc.cumack_log_atsnt = 0;
9602 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9603 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9604 asoc->my_last_reported_rwnd = asoc->my_rwnd;
9606 /* reset the readers interpretation */
9607 stcb->freed_by_sorcv_sincelast = 0;
9609 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9611 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9612 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9615 * cum-ack behind the mapping array, so we start and use all
9620 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9622 * we skip the first one when the cum-ack is at or above the
9623 * mapping array base. Note this only works if
9627 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9628 /* we have a gap .. maybe */
9629 for (i = 0; i < siz; i++) {
9630 selector = &sack_array[asoc->mapping_array[i]];
9631 if (mergeable && selector->right_edge) {
9633 * Backup, left and right edges were ok to
9639 if (selector->num_entries == 0)
9642 for (j = jstart; j < selector->num_entries; j++) {
9643 if (mergeable && selector->right_edge) {
9645 * do a merge by NOT setting
9651 * no merge, set the left
9655 gap_descriptor->start = htons((selector->gaps[j].start + offset));
9657 gap_descriptor->end = htons((selector->gaps[j].end + offset));
9660 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
9666 if (selector->left_edge) {
9670 if (limit_reached) {
9671 /* Reached the limit stop */
9677 if (num_gap_blocks == 0) {
9679 * slide not yet happened, and somehow we got called
9680 * to send a sack. Cumack needs to move up.
9684 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
9685 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9686 sctp_sack_check(stcb, 0, 0, &abort_flag);
9689 /* now we must add any dups we are going to report. */
9690 if ((limit_reached == 0) && (asoc->numduptsns)) {
9691 dup = (uint32_t *) gap_descriptor;
9692 for (i = 0; i < asoc->numduptsns; i++) {
9693 *dup = htonl(asoc->dup_tsns[i]);
9696 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
9701 asoc->numduptsns = 0;
9704 * now that the chunk is prepared queue it to the control chunk
9707 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
9708 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
9709 (num_dups * sizeof(int32_t)));
9710 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
9711 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
9712 sack->sack.num_dup_tsns = htons(num_dups);
9713 sack->ch.chunk_length = htons(a_chk->send_size);
9714 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
9715 asoc->ctrl_queue_cnt++;
9716 asoc->send_sack = 0;
9717 SCTP_STAT_INCR(sctps_sendsacks);
9723 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
9724 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9729 struct mbuf *m_abort;
9730 struct mbuf *m_out = NULL, *m_end = NULL;
9731 struct sctp_abort_chunk *abort = NULL;
9733 uint32_t auth_offset = 0;
9734 struct sctp_auth_chunk *auth = NULL;
9735 struct sctphdr *shdr;
9738 * Add an AUTH chunk, if chunk requires it and save the offset into
9739 * the chain for AUTH
9741 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
9742 stcb->asoc.peer_auth_chunks)) {
9743 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
9744 stcb, SCTP_ABORT_ASSOCIATION);
9746 SCTP_TCB_LOCK_ASSERT(stcb);
9747 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9748 if (m_abort == NULL) {
9751 sctp_m_freem(m_out);
9754 /* link in any error */
9755 SCTP_BUF_NEXT(m_abort) = operr;
9762 sz += SCTP_BUF_LEN(n);
9763 n = SCTP_BUF_NEXT(n);
9766 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
9767 if (m_out == NULL) {
9768 /* NO Auth chunk prepended, so reserve space in front */
9769 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
9772 /* Put AUTH chunk at the front of the chain */
9773 SCTP_BUF_NEXT(m_end) = m_abort;
9776 /* fill in the ABORT chunk */
9777 abort = mtod(m_abort, struct sctp_abort_chunk *);
9778 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9779 abort->ch.chunk_flags = 0;
9780 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
9782 /* prepend and fill in the SCTP header */
9783 SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
9784 if (m_out == NULL) {
9785 /* TSNH: no memory */
9788 shdr = mtod(m_out, struct sctphdr *);
9789 shdr->src_port = stcb->sctp_ep->sctp_lport;
9790 shdr->dest_port = stcb->rport;
9791 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9793 auth_offset += sizeof(struct sctphdr);
9795 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
9796 stcb->asoc.primary_destination,
9797 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
9798 m_out, auth_offset, auth, 1, 0, NULL, 0, so_locked);
9799 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9803 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
9804 struct sctp_nets *net)
9806 /* formulate and SEND a SHUTDOWN-COMPLETE */
9807 struct mbuf *m_shutdown_comp;
9808 struct sctp_shutdown_complete_msg *comp_cp;
9810 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
9811 if (m_shutdown_comp == NULL) {
9815 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
9816 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9817 comp_cp->shut_cmp.ch.chunk_flags = 0;
9818 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9819 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
9820 comp_cp->sh.dest_port = stcb->rport;
9821 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
9822 comp_cp->sh.checksum = 0;
9824 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
9825 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
9826 (struct sockaddr *)&net->ro._l_addr,
9827 m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, SCTP_SO_NOT_LOCKED);
9828 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9833 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
9836 /* formulate and SEND a SHUTDOWN-COMPLETE */
9839 struct ip *iph, *iph_out;
9842 struct ip6_hdr *ip6, *ip6_out;
9845 int offset_out, len, mlen;
9846 struct sctp_shutdown_complete_msg *comp_cp;
9848 /* Get room for the largest message */
9850 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
9852 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
9854 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
9858 SCTP_BUF_LEN(mout) = len;
9859 iph = mtod(m, struct ip *);
9865 switch (iph->ip_v) {
9867 SCTP_BUF_LEN(mout) = sizeof(struct ip) +
9868 sizeof(struct sctp_shutdown_complete_msg);
9869 SCTP_BUF_NEXT(mout) = NULL;
9870 iph_out = mtod(mout, struct ip *);
9872 /* Fill in the IP header for the ABORT */
9873 iph_out->ip_v = IPVERSION;
9874 iph_out->ip_hl = (sizeof(struct ip) / 4);
9875 iph_out->ip_tos = (u_char)0;
9877 iph_out->ip_off = 0;
9878 iph_out->ip_ttl = MAXTTL;
9879 iph_out->ip_p = IPPROTO_SCTP;
9880 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9881 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9883 /* let IP layer calculate this */
9884 iph_out->ip_sum = 0;
9885 offset_out += sizeof(*iph_out);
9886 comp_cp = (struct sctp_shutdown_complete_msg *)(
9887 (caddr_t)iph_out + offset_out);
9890 case IPV6_VERSION >> 4:
9891 ip6 = (struct ip6_hdr *)iph;
9892 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr) +
9893 sizeof(struct sctp_shutdown_complete_msg);
9894 SCTP_BUF_NEXT(mout) = NULL;
9895 ip6_out = mtod(mout, struct ip6_hdr *);
9897 /* Fill in the IPv6 header for the ABORT */
9898 ip6_out->ip6_flow = ip6->ip6_flow;
9899 ip6_out->ip6_hlim = ip6_defhlim;
9900 ip6_out->ip6_nxt = IPPROTO_SCTP;
9901 ip6_out->ip6_src = ip6->ip6_dst;
9902 ip6_out->ip6_dst = ip6->ip6_src;
9904 * ?? The old code had both the iph len + payload, I think
9905 * this is wrong and would never have worked
9907 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
9908 offset_out += sizeof(*ip6_out);
9909 comp_cp = (struct sctp_shutdown_complete_msg *)(
9910 (caddr_t)ip6_out + offset_out);
9914 /* Currently not supported. */
9917 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
9922 /* Now copy in and fill in the ABORT tags etc. */
9923 comp_cp->sh.src_port = sh->dest_port;
9924 comp_cp->sh.dest_port = sh->src_port;
9925 comp_cp->sh.checksum = 0;
9926 comp_cp->sh.v_tag = sh->v_tag;
9927 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
9928 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9929 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9932 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
9933 if (iph_out != NULL) {
9936 struct sctp_tcb *stcb = NULL;
9938 mlen = SCTP_BUF_LEN(mout);
9939 bzero(&ro, sizeof ro);
9940 /* set IPv4 length */
9941 iph_out->ip_len = mlen;
9942 #ifdef SCTP_PACKET_LOGGING
9943 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9944 sctp_packet_log(mout, mlen);
9946 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9949 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
9951 /* Free the route if we got one back */
9956 if (ip6_out != NULL) {
9957 struct route_in6 ro;
9959 struct sctp_tcb *stcb = NULL;
9960 struct ifnet *ifp = NULL;
9962 bzero(&ro, sizeof(ro));
9963 mlen = SCTP_BUF_LEN(mout);
9964 #ifdef SCTP_PACKET_LOGGING
9965 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9966 sctp_packet_log(mout, mlen);
9968 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9969 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
9971 /* Free the route if we got one back */
9976 SCTP_STAT_INCR(sctps_sendpackets);
9977 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9978 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9983 static struct sctp_nets *
9984 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
9986 struct sctp_nets *net, *hnet;
9987 int ms_goneby, highest_ms, state_overide = 0;
9989 (void)SCTP_GETTIME_TIMEVAL(now);
9992 SCTP_TCB_LOCK_ASSERT(stcb);
9993 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9995 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
9996 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
9999 * Skip this guy from consideration if HB is off AND
10004 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10005 /* skip this dest net from consideration */
10008 if (net->last_sent_time.tv_sec) {
10009 /* Sent to so we subtract */
10010 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10012 /* Never been sent to */
10013 ms_goneby = 0x7fffffff;
10015 * When the address state is unconfirmed but still
10016 * considered reachable, we HB at a higher rate. Once it
10017 * goes confirmed OR reaches the "unreachable" state, thenw
10018 * we cut it back to HB at a more normal pace.
10020 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10026 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10027 (ms_goneby > highest_ms)) {
10028 highest_ms = ms_goneby;
10033 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10039 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10041 * Found the one with longest delay bounds OR it is
10042 * unconfirmed and still not marked unreachable.
10044 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10047 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10048 (struct sockaddr *)&hnet->ro._l_addr);
10050 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10053 /* update the timer now */
10054 hnet->last_sent_time = *now;
10057 /* Nothing to HB */
10062 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10064 struct sctp_tmit_chunk *chk;
10065 struct sctp_nets *net;
10066 struct sctp_heartbeat_chunk *hb;
10067 struct timeval now;
10068 struct sockaddr_in *sin;
10069 struct sockaddr_in6 *sin6;
10071 SCTP_TCB_LOCK_ASSERT(stcb);
10072 if (user_req == 0) {
10073 net = sctp_select_hb_destination(stcb, &now);
10076 * All our busy none to send to, just start the
10079 if (stcb->asoc.state == 0) {
10082 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10093 (void)SCTP_GETTIME_TIMEVAL(&now);
10095 sin = (struct sockaddr_in *)&net->ro._l_addr;
10096 if (sin->sin_family != AF_INET) {
10097 if (sin->sin_family != AF_INET6) {
10102 sctp_alloc_a_chunk(stcb, chk);
10104 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10107 chk->copy_by_ref = 0;
10108 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10109 chk->rec.chunk_id.can_take_data = 1;
10110 chk->asoc = &stcb->asoc;
10111 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10113 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10114 if (chk->data == NULL) {
10115 sctp_free_a_chunk(stcb, chk);
10118 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10119 SCTP_BUF_LEN(chk->data) = chk->send_size;
10120 chk->sent = SCTP_DATAGRAM_UNSENT;
10121 chk->snd_count = 0;
10123 atomic_add_int(&chk->whoTo->ref_count, 1);
10124 /* Now we have a mbuf that we can fill in with the details */
10125 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10126 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10127 /* fill out chunk header */
10128 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10129 hb->ch.chunk_flags = 0;
10130 hb->ch.chunk_length = htons(chk->send_size);
10131 /* Fill out hb parameter */
10132 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10133 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10134 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10135 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10136 /* Did our user request this one, put it in */
10137 hb->heartbeat.hb_info.user_req = user_req;
10138 hb->heartbeat.hb_info.addr_family = sin->sin_family;
10139 hb->heartbeat.hb_info.addr_len = sin->sin_len;
10140 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10142 * we only take from the entropy pool if the address is not
10145 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10146 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10148 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10149 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10151 if (sin->sin_family == AF_INET) {
10152 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10153 } else if (sin->sin_family == AF_INET6) {
10154 /* We leave the scope the way it is in our lookup table. */
10155 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10156 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10158 /* huh compiler bug */
10163 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10164 * PF-heartbeats. Because of this, threshold management is done by
10165 * the t3 timer handler, and does not need to be done upon the send
10166 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10167 * heartbeat is being sent is in PF state, do NOT do threshold
10170 if ((sctp_cmt_pf == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10171 /* ok we have a destination that needs a beat */
10172 /* lets do the theshold management Qiaobing style */
10173 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10174 stcb->asoc.max_send_times)) {
10176 * we have lost the association, in a way this is
10177 * quite bad since we really are one less time since
10178 * we really did not send yet. This is the down side
10179 * to the Q's style as defined in the RFC and not my
10180 * alternate style defined in the RFC.
10182 if (chk->data != NULL) {
10183 sctp_m_freem(chk->data);
10187 * Here we do NOT use the macro since the
10188 * association is now gone.
10191 sctp_free_remote_addr(chk->whoTo);
10194 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10198 net->hb_responded = 0;
10199 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10200 stcb->asoc.ctrl_queue_cnt++;
10201 SCTP_STAT_INCR(sctps_sendheartbeat);
10203 * Call directly med level routine to put out the chunk. It will
10204 * always tumble out control chunks aka HB but it may even tumble
10211 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10214 struct sctp_association *asoc;
10215 struct sctp_ecne_chunk *ecne;
10216 struct sctp_tmit_chunk *chk;
10218 asoc = &stcb->asoc;
10219 SCTP_TCB_LOCK_ASSERT(stcb);
10220 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10221 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10222 /* found a previous ECN_ECHO update it if needed */
10223 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10224 ecne->tsn = htonl(high_tsn);
10228 /* nope could not find one to update so we must build one */
10229 sctp_alloc_a_chunk(stcb, chk);
10233 chk->copy_by_ref = 0;
10234 SCTP_STAT_INCR(sctps_sendecne);
10235 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10236 chk->rec.chunk_id.can_take_data = 0;
10237 chk->asoc = &stcb->asoc;
10238 chk->send_size = sizeof(struct sctp_ecne_chunk);
10239 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10240 if (chk->data == NULL) {
10241 sctp_free_a_chunk(stcb, chk);
10244 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10245 SCTP_BUF_LEN(chk->data) = chk->send_size;
10246 chk->sent = SCTP_DATAGRAM_UNSENT;
10247 chk->snd_count = 0;
10249 atomic_add_int(&chk->whoTo->ref_count, 1);
10250 stcb->asoc.ecn_echo_cnt_onq++;
10251 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10252 ecne->ch.chunk_type = SCTP_ECN_ECHO;
10253 ecne->ch.chunk_flags = 0;
10254 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10255 ecne->tsn = htonl(high_tsn);
10256 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10257 asoc->ctrl_queue_cnt++;
10261 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10262 struct mbuf *m, int iphlen, int bad_crc)
10264 struct sctp_association *asoc;
10265 struct sctp_pktdrop_chunk *drp;
10266 struct sctp_tmit_chunk *chk;
10273 struct ip6_hdr *ip6h;
10276 int fullsz = 0, extra = 0;
10279 struct sctp_chunkhdr *ch, chunk_buf;
10280 unsigned int chk_length;
10285 asoc = &stcb->asoc;
10286 SCTP_TCB_LOCK_ASSERT(stcb);
10287 if (asoc->peer_supports_pktdrop == 0) {
10289 * peer must declare support before I send one.
10293 if (stcb->sctp_socket == NULL) {
10296 sctp_alloc_a_chunk(stcb, chk);
10300 chk->copy_by_ref = 0;
10301 iph = mtod(m, struct ip *);
10303 sctp_free_a_chunk(stcb, chk);
10306 switch (iph->ip_v) {
10309 len = chk->send_size = iph->ip_len;
10312 case IPV6_VERSION >> 4:
10314 ip6h = mtod(m, struct ip6_hdr *);
10315 len = chk->send_size = htons(ip6h->ip6_plen);
10321 /* Validate that we do not have an ABORT in here. */
10322 offset = iphlen + sizeof(struct sctphdr);
10323 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10324 sizeof(*ch), (uint8_t *) & chunk_buf);
10325 while (ch != NULL) {
10326 chk_length = ntohs(ch->chunk_length);
10327 if (chk_length < sizeof(*ch)) {
10328 /* break to abort land */
10331 switch (ch->chunk_type) {
10332 case SCTP_PACKET_DROPPED:
10333 case SCTP_ABORT_ASSOCIATION:
10335 * we don't respond with an PKT-DROP to an ABORT
10338 sctp_free_a_chunk(stcb, chk);
10343 offset += SCTP_SIZE32(chk_length);
10344 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10345 sizeof(*ch), (uint8_t *) & chunk_buf);
10348 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10349 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10351 * only send 1 mtu worth, trim off the excess on the end.
10353 fullsz = len - extra;
10354 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10357 chk->asoc = &stcb->asoc;
10358 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10359 if (chk->data == NULL) {
10361 sctp_free_a_chunk(stcb, chk);
10364 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10365 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10367 sctp_m_freem(chk->data);
10371 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10372 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10373 chk->book_size_scale = 0;
10375 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10376 drp->trunc_len = htons(fullsz);
10378 * Len is already adjusted to size minus overhead above take
10379 * out the pkt_drop chunk itself from it.
10381 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10382 len = chk->send_size;
10384 /* no truncation needed */
10385 drp->ch.chunk_flags = 0;
10386 drp->trunc_len = htons(0);
10389 drp->ch.chunk_flags |= SCTP_BADCRC;
10391 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10392 SCTP_BUF_LEN(chk->data) = chk->send_size;
10393 chk->sent = SCTP_DATAGRAM_UNSENT;
10394 chk->snd_count = 0;
10396 /* we should hit here */
10399 chk->whoTo = asoc->primary_destination;
10401 atomic_add_int(&chk->whoTo->ref_count, 1);
10402 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10403 chk->rec.chunk_id.can_take_data = 1;
10404 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10405 drp->ch.chunk_length = htons(chk->send_size);
10406 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10410 drp->bottle_bw = htonl(spc);
10411 if (asoc->my_rwnd) {
10412 drp->current_onq = htonl(asoc->size_on_reasm_queue +
10413 asoc->size_on_all_streams +
10414 asoc->my_rwnd_control_len +
10415 stcb->sctp_socket->so_rcv.sb_cc);
10418 * If my rwnd is 0, possibly from mbuf depletion as well as
10419 * space used, tell the peer there is NO space aka onq == bw
10421 drp->current_onq = htonl(spc);
10425 m_copydata(m, iphlen, len, (caddr_t)datap);
10426 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10427 asoc->ctrl_queue_cnt++;
10431 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10433 struct sctp_association *asoc;
10434 struct sctp_cwr_chunk *cwr;
10435 struct sctp_tmit_chunk *chk;
10437 asoc = &stcb->asoc;
10438 SCTP_TCB_LOCK_ASSERT(stcb);
10439 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10440 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10441 /* found a previous ECN_CWR update it if needed */
10442 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10443 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10445 cwr->tsn = htonl(high_tsn);
10450 /* nope could not find one to update so we must build one */
10451 sctp_alloc_a_chunk(stcb, chk);
10455 chk->copy_by_ref = 0;
10456 chk->rec.chunk_id.id = SCTP_ECN_CWR;
10457 chk->rec.chunk_id.can_take_data = 1;
10458 chk->asoc = &stcb->asoc;
10459 chk->send_size = sizeof(struct sctp_cwr_chunk);
10460 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10461 if (chk->data == NULL) {
10462 sctp_free_a_chunk(stcb, chk);
10465 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10466 SCTP_BUF_LEN(chk->data) = chk->send_size;
10467 chk->sent = SCTP_DATAGRAM_UNSENT;
10468 chk->snd_count = 0;
10470 atomic_add_int(&chk->whoTo->ref_count, 1);
10471 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10472 cwr->ch.chunk_type = SCTP_ECN_CWR;
10473 cwr->ch.chunk_flags = 0;
10474 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10475 cwr->tsn = htonl(high_tsn);
10476 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10477 asoc->ctrl_queue_cnt++;
10481 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10482 int number_entries, uint16_t * list,
10483 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10485 int len, old_len, i;
10486 struct sctp_stream_reset_out_request *req_out;
10487 struct sctp_chunkhdr *ch;
10489 ch = mtod(chk->data, struct sctp_chunkhdr *);
10492 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10494 /* get to new offset for the param. */
10495 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10496 /* now how long will this param be? */
10497 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10498 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10499 req_out->ph.param_length = htons(len);
10500 req_out->request_seq = htonl(seq);
10501 req_out->response_seq = htonl(resp_seq);
10502 req_out->send_reset_at_tsn = htonl(last_sent);
10503 if (number_entries) {
10504 for (i = 0; i < number_entries; i++) {
10505 req_out->list_of_streams[i] = htons(list[i]);
10508 if (SCTP_SIZE32(len) > len) {
10510 * Need to worry about the pad we may end up adding to the
10511 * end. This is easy since the struct is either aligned to 4
10512 * bytes or 2 bytes off.
10514 req_out->list_of_streams[number_entries] = 0;
10516 /* now fix the chunk length */
10517 ch->chunk_length = htons(len + old_len);
10518 chk->book_size = len + old_len;
10519 chk->book_size_scale = 0;
10520 chk->send_size = SCTP_SIZE32(chk->book_size);
10521 SCTP_BUF_LEN(chk->data) = chk->send_size;
10527 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10528 int number_entries, uint16_t * list,
10531 int len, old_len, i;
10532 struct sctp_stream_reset_in_request *req_in;
10533 struct sctp_chunkhdr *ch;
10535 ch = mtod(chk->data, struct sctp_chunkhdr *);
10538 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10540 /* get to new offset for the param. */
10541 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10542 /* now how long will this param be? */
10543 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10544 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10545 req_in->ph.param_length = htons(len);
10546 req_in->request_seq = htonl(seq);
10547 if (number_entries) {
10548 for (i = 0; i < number_entries; i++) {
10549 req_in->list_of_streams[i] = htons(list[i]);
10552 if (SCTP_SIZE32(len) > len) {
10554 * Need to worry about the pad we may end up adding to the
10555 * end. This is easy since the struct is either aligned to 4
10556 * bytes or 2 bytes off.
10558 req_in->list_of_streams[number_entries] = 0;
10560 /* now fix the chunk length */
10561 ch->chunk_length = htons(len + old_len);
10562 chk->book_size = len + old_len;
10563 chk->book_size_scale = 0;
10564 chk->send_size = SCTP_SIZE32(chk->book_size);
10565 SCTP_BUF_LEN(chk->data) = chk->send_size;
10571 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10575 struct sctp_stream_reset_tsn_request *req_tsn;
10576 struct sctp_chunkhdr *ch;
10578 ch = mtod(chk->data, struct sctp_chunkhdr *);
10581 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10583 /* get to new offset for the param. */
10584 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10585 /* now how long will this param be? */
10586 len = sizeof(struct sctp_stream_reset_tsn_request);
10587 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10588 req_tsn->ph.param_length = htons(len);
10589 req_tsn->request_seq = htonl(seq);
10591 /* now fix the chunk length */
10592 ch->chunk_length = htons(len + old_len);
10593 chk->send_size = len + old_len;
10594 chk->book_size = SCTP_SIZE32(chk->send_size);
10595 chk->book_size_scale = 0;
10596 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10601 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10602 uint32_t resp_seq, uint32_t result)
10605 struct sctp_stream_reset_response *resp;
10606 struct sctp_chunkhdr *ch;
10608 ch = mtod(chk->data, struct sctp_chunkhdr *);
10611 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10613 /* get to new offset for the param. */
10614 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
10615 /* now how long will this param be? */
10616 len = sizeof(struct sctp_stream_reset_response);
10617 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10618 resp->ph.param_length = htons(len);
10619 resp->response_seq = htonl(resp_seq);
10620 resp->result = ntohl(result);
10622 /* now fix the chunk length */
10623 ch->chunk_length = htons(len + old_len);
10624 chk->book_size = len + old_len;
10625 chk->book_size_scale = 0;
10626 chk->send_size = SCTP_SIZE32(chk->book_size);
10627 SCTP_BUF_LEN(chk->data) = chk->send_size;
10634 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
10635 uint32_t resp_seq, uint32_t result,
10636 uint32_t send_una, uint32_t recv_next)
10639 struct sctp_stream_reset_response_tsn *resp;
10640 struct sctp_chunkhdr *ch;
10642 ch = mtod(chk->data, struct sctp_chunkhdr *);
10645 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10647 /* get to new offset for the param. */
10648 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
10649 /* now how long will this param be? */
10650 len = sizeof(struct sctp_stream_reset_response_tsn);
10651 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10652 resp->ph.param_length = htons(len);
10653 resp->response_seq = htonl(resp_seq);
10654 resp->result = htonl(result);
10655 resp->senders_next_tsn = htonl(send_una);
10656 resp->receivers_next_tsn = htonl(recv_next);
10658 /* now fix the chunk length */
10659 ch->chunk_length = htons(len + old_len);
10660 chk->book_size = len + old_len;
10661 chk->send_size = SCTP_SIZE32(chk->book_size);
10662 chk->book_size_scale = 0;
10663 SCTP_BUF_LEN(chk->data) = chk->send_size;
10669 sctp_send_str_reset_req(struct sctp_tcb *stcb,
10670 int number_entries, uint16_t * list,
10671 uint8_t send_out_req, uint32_t resp_seq,
10672 uint8_t send_in_req,
10673 uint8_t send_tsn_req)
10676 struct sctp_association *asoc;
10677 struct sctp_tmit_chunk *chk;
10678 struct sctp_chunkhdr *ch;
10681 asoc = &stcb->asoc;
10682 if (asoc->stream_reset_outstanding) {
10684 * Already one pending, must get ACK back to clear the flag.
10686 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
10689 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
10690 /* nothing to do */
10691 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10694 if (send_tsn_req && (send_out_req || send_in_req)) {
10695 /* error, can't do that */
10696 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10699 sctp_alloc_a_chunk(stcb, chk);
10701 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10704 chk->copy_by_ref = 0;
10705 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
10706 chk->rec.chunk_id.can_take_data = 0;
10707 chk->asoc = &stcb->asoc;
10708 chk->book_size = sizeof(struct sctp_chunkhdr);
10709 chk->send_size = SCTP_SIZE32(chk->book_size);
10710 chk->book_size_scale = 0;
10712 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10713 if (chk->data == NULL) {
10714 sctp_free_a_chunk(stcb, chk);
10715 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10718 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10720 /* setup chunk parameters */
10721 chk->sent = SCTP_DATAGRAM_UNSENT;
10722 chk->snd_count = 0;
10723 chk->whoTo = asoc->primary_destination;
10724 atomic_add_int(&chk->whoTo->ref_count, 1);
10726 ch = mtod(chk->data, struct sctp_chunkhdr *);
10727 ch->chunk_type = SCTP_STREAM_RESET;
10728 ch->chunk_flags = 0;
10729 ch->chunk_length = htons(chk->book_size);
10730 SCTP_BUF_LEN(chk->data) = chk->send_size;
10732 seq = stcb->asoc.str_reset_seq_out;
10733 if (send_out_req) {
10734 sctp_add_stream_reset_out(chk, number_entries, list,
10735 seq, resp_seq, (stcb->asoc.sending_seq - 1));
10736 asoc->stream_reset_out_is_outstanding = 1;
10738 asoc->stream_reset_outstanding++;
10741 sctp_add_stream_reset_in(chk, number_entries, list, seq);
10742 asoc->stream_reset_outstanding++;
10744 if (send_tsn_req) {
10745 sctp_add_stream_reset_tsn(chk, seq);
10746 asoc->stream_reset_outstanding++;
10748 asoc->str_reset = chk;
10750 /* insert the chunk for sending */
10751 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
10754 asoc->ctrl_queue_cnt++;
10755 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
10760 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
10761 struct mbuf *err_cause, uint32_t vrf_id)
10764 * Formulate the abort message, and send it back down.
10766 struct mbuf *o_pak;
10768 struct sctp_abort_msg *abm;
10769 struct ip *iph, *iph_out;
10772 struct ip6_hdr *ip6, *ip6_out;
10775 int iphlen_out, len;
10777 /* don't respond to ABORT with ABORT */
10778 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
10780 sctp_m_freem(err_cause);
10784 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
10786 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
10788 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10789 if (mout == NULL) {
10791 sctp_m_freem(err_cause);
10794 iph = mtod(m, struct ip *);
10799 switch (iph->ip_v) {
10801 iph_out = mtod(mout, struct ip *);
10802 SCTP_BUF_LEN(mout) = sizeof(*iph_out) + sizeof(*abm);
10803 SCTP_BUF_NEXT(mout) = err_cause;
10805 /* Fill in the IP header for the ABORT */
10806 iph_out->ip_v = IPVERSION;
10807 iph_out->ip_hl = (sizeof(struct ip) / 4);
10808 iph_out->ip_tos = (u_char)0;
10809 iph_out->ip_id = 0;
10810 iph_out->ip_off = 0;
10811 iph_out->ip_ttl = MAXTTL;
10812 iph_out->ip_p = IPPROTO_SCTP;
10813 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10814 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10815 /* let IP layer calculate this */
10816 iph_out->ip_sum = 0;
10818 iphlen_out = sizeof(*iph_out);
10819 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
10822 case IPV6_VERSION >> 4:
10823 ip6 = (struct ip6_hdr *)iph;
10824 ip6_out = mtod(mout, struct ip6_hdr *);
10825 SCTP_BUF_LEN(mout) = sizeof(*ip6_out) + sizeof(*abm);
10826 SCTP_BUF_NEXT(mout) = err_cause;
10828 /* Fill in the IP6 header for the ABORT */
10829 ip6_out->ip6_flow = ip6->ip6_flow;
10830 ip6_out->ip6_hlim = ip6_defhlim;
10831 ip6_out->ip6_nxt = IPPROTO_SCTP;
10832 ip6_out->ip6_src = ip6->ip6_dst;
10833 ip6_out->ip6_dst = ip6->ip6_src;
10835 iphlen_out = sizeof(*ip6_out);
10836 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
10840 /* Currently not supported */
10842 sctp_m_freem(err_cause);
10843 sctp_m_freem(mout);
10847 abm->sh.src_port = sh->dest_port;
10848 abm->sh.dest_port = sh->src_port;
10849 abm->sh.checksum = 0;
10851 abm->sh.v_tag = sh->v_tag;
10852 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
10854 abm->sh.v_tag = htonl(vtag);
10855 abm->msg.ch.chunk_flags = 0;
10857 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10860 struct mbuf *m_tmp = err_cause;
10863 /* get length of the err_cause chain */
10864 while (m_tmp != NULL) {
10865 err_len += SCTP_BUF_LEN(m_tmp);
10866 m_tmp = SCTP_BUF_NEXT(m_tmp);
10868 len = SCTP_BUF_LEN(mout) + err_len;
10870 /* need pad at end of chunk */
10871 uint32_t cpthis = 0;
10874 padlen = 4 - (len % 4);
10875 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
10878 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
10880 len = SCTP_BUF_LEN(mout);
10881 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
10885 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
10886 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10888 sctp_m_freem(mout);
10891 if (iph_out != NULL) {
10893 struct sctp_tcb *stcb = NULL;
10896 /* zap the stack pointer to the route */
10897 bzero(&ro, sizeof ro);
10898 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
10899 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
10900 /* set IPv4 length */
10901 iph_out->ip_len = len;
10903 #ifdef SCTP_PACKET_LOGGING
10904 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10905 sctp_packet_log(mout, len);
10907 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10908 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10910 /* Free the route if we got one back */
10915 if (ip6_out != NULL) {
10916 struct route_in6 ro;
10918 struct sctp_tcb *stcb = NULL;
10919 struct ifnet *ifp = NULL;
10921 /* zap the stack pointer to the route */
10922 bzero(&ro, sizeof(ro));
10923 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
10924 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
10925 ip6_out->ip6_plen = len - sizeof(*ip6_out);
10926 #ifdef SCTP_PACKET_LOGGING
10927 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10928 sctp_packet_log(mout, len);
10930 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10931 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10933 /* Free the route if we got one back */
10938 SCTP_STAT_INCR(sctps_sendpackets);
10939 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10943 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
10946 struct mbuf *o_pak;
10947 struct sctphdr *ihdr;
10949 struct sctphdr *ohdr;
10950 struct sctp_chunkhdr *ophdr;
10955 struct sockaddr_in6 lsa6, fsa6;
10962 iph = mtod(m, struct ip *);
10963 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
10965 SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
10967 /* can't send because we can't add a mbuf */
10970 ohdr = mtod(scm, struct sctphdr *);
10971 ohdr->src_port = ihdr->dest_port;
10972 ohdr->dest_port = ihdr->src_port;
10973 ohdr->v_tag = vtag;
10974 ohdr->checksum = 0;
10975 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
10976 ophdr->chunk_type = SCTP_OPERATION_ERROR;
10977 ophdr->chunk_flags = 0;
10981 len += SCTP_BUF_LEN(at);
10982 at = SCTP_BUF_NEXT(at);
10984 ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
10987 uint32_t cpthis = 0;
10990 padlen = 4 - (len % 4);
10991 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
10994 val = sctp_calculate_sum(scm, NULL, 0);
10996 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
10998 mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
11000 if (mout == NULL) {
11004 SCTP_BUF_NEXT(mout) = scm;
11005 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11006 sctp_m_freem(mout);
11009 ohdr->checksum = val;
11010 switch (iph->ip_v) {
11016 struct sctp_tcb *stcb = NULL;
11018 SCTP_BUF_LEN(mout) = sizeof(struct ip);
11019 len += sizeof(struct ip);
11021 bzero(&ro, sizeof ro);
11022 out = mtod(mout, struct ip *);
11023 out->ip_v = iph->ip_v;
11024 out->ip_hl = (sizeof(struct ip) / 4);
11025 out->ip_tos = iph->ip_tos;
11026 out->ip_id = iph->ip_id;
11028 out->ip_ttl = MAXTTL;
11029 out->ip_p = IPPROTO_SCTP;
11031 out->ip_src = iph->ip_dst;
11032 out->ip_dst = iph->ip_src;
11034 #ifdef SCTP_PACKET_LOGGING
11035 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
11036 sctp_packet_log(mout, len);
11038 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11040 SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
11042 SCTP_STAT_INCR(sctps_sendpackets);
11043 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11044 /* Free the route if we got one back */
11050 case IPV6_VERSION >> 4:
11053 struct route_in6 ro;
11055 struct sctp_tcb *stcb = NULL;
11056 struct ifnet *ifp = NULL;
11057 struct ip6_hdr *out6, *in6;
11059 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
11060 len += sizeof(struct ip6_hdr);
11061 bzero(&ro, sizeof ro);
11062 in6 = mtod(m, struct ip6_hdr *);
11063 out6 = mtod(mout, struct ip6_hdr *);
11064 out6->ip6_flow = in6->ip6_flow;
11065 out6->ip6_hlim = ip6_defhlim;
11066 out6->ip6_nxt = IPPROTO_SCTP;
11067 out6->ip6_src = in6->ip6_dst;
11068 out6->ip6_dst = in6->ip6_src;
11069 out6->ip6_plen = len - sizeof(struct ip6_hdr);
11072 bzero(&lsa6, sizeof(lsa6));
11073 lsa6.sin6_len = sizeof(lsa6);
11074 lsa6.sin6_family = AF_INET6;
11075 lsa6.sin6_addr = out6->ip6_src;
11076 bzero(&fsa6, sizeof(fsa6));
11077 fsa6.sin6_len = sizeof(fsa6);
11078 fsa6.sin6_family = AF_INET6;
11079 fsa6.sin6_addr = out6->ip6_dst;
11081 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
11082 SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
11083 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
11084 SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
11085 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
11087 #ifdef SCTP_PACKET_LOGGING
11088 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
11089 sctp_packet_log(mout, len);
11091 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11092 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11094 SCTP_STAT_INCR(sctps_sendpackets);
11095 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11096 /* Free the route if we got one back */
11108 static struct mbuf *
11109 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11111 struct sctp_sndrcvinfo *srcv,
11113 int user_marks_eor,
11116 struct mbuf **new_tail)
11120 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11121 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11123 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11126 *sndout = m_length(m, NULL);
11127 *new_tail = m_last(m);
11133 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11140 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11142 if (sp->data == NULL) {
11143 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11146 sp->tail_mbuf = m_last(sp->data);
11152 static struct sctp_stream_queue_pending *
11153 sctp_copy_it_in(struct sctp_tcb *stcb,
11154 struct sctp_association *asoc,
11155 struct sctp_sndrcvinfo *srcv,
11157 struct sctp_nets *net,
11159 int user_marks_eor,
11164 * This routine must be very careful in its work. Protocol
11165 * processing is up and running so care must be taken to spl...()
11166 * when you need to do something that may effect the stcb/asoc. The
11167 * sb is locked however. When data is copied the protocol processing
11168 * should be enabled since this is a slower operation...
11170 struct sctp_stream_queue_pending *sp = NULL;
11174 /* Now can we send this? */
11175 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11176 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11177 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11178 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11179 /* got data while shutting down */
11180 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11181 *error = ECONNRESET;
11184 sctp_alloc_a_strmoq(stcb, sp);
11186 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11191 sp->sender_all_done = 0;
11192 sp->sinfo_flags = srcv->sinfo_flags;
11193 sp->timetolive = srcv->sinfo_timetolive;
11194 sp->ppid = srcv->sinfo_ppid;
11195 sp->context = srcv->sinfo_context;
11197 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11199 sp->stream = srcv->sinfo_stream;
11200 sp->length = min(uio->uio_resid, max_send_len);
11201 if ((sp->length == (uint32_t) uio->uio_resid) &&
11202 ((user_marks_eor == 0) ||
11203 (srcv->sinfo_flags & SCTP_EOF) ||
11204 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11205 sp->msg_is_complete = 1;
11207 sp->msg_is_complete = 0;
11209 sp->sender_all_done = 0;
11210 sp->some_taken = 0;
11211 sp->put_last_out = 0;
11212 resv_in_first = sizeof(struct sctp_data_chunk);
11213 sp->data = sp->tail_mbuf = NULL;
11214 *error = sctp_copy_one(sp, uio, resv_in_first);
11216 sctp_free_a_strmoq(stcb, sp);
11219 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11223 sp->net = asoc->primary_destination;
11226 atomic_add_int(&sp->net->ref_count, 1);
11227 sctp_set_prsctp_policy(stcb, sp);
11235 sctp_sosend(struct socket *so,
11236 struct sockaddr *addr,
11239 struct mbuf *control,
11244 struct sctp_inpcb *inp;
11245 int error, use_rcvinfo = 0;
11246 struct sctp_sndrcvinfo srcv;
11248 inp = (struct sctp_inpcb *)so->so_pcb;
11250 /* process cmsg snd/rcv info (maybe a assoc-id) */
11251 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11257 error = sctp_lower_sosend(so, addr, uio, top,
11268 sctp_lower_sosend(struct socket *so,
11269 struct sockaddr *addr,
11271 struct mbuf *i_pak,
11272 struct mbuf *control,
11275 struct sctp_sndrcvinfo *srcv
11280 unsigned int sndlen = 0, max_len;
11282 struct mbuf *top = NULL;
11283 int queue_only = 0, queue_only_for_init = 0;
11284 int free_cnt_applied = 0;
11286 int now_filled = 0;
11287 unsigned int inqueue_bytes = 0;
11288 struct sctp_block_entry be;
11289 struct sctp_inpcb *inp;
11290 struct sctp_tcb *stcb = NULL;
11291 struct timeval now;
11292 struct sctp_nets *net;
11293 struct sctp_association *asoc;
11294 struct sctp_inpcb *t_inp;
11295 int user_marks_eor;
11296 int create_lock_applied = 0;
11297 int nagle_applies = 0;
11298 int some_on_control = 0;
11299 int got_all_of_the_send = 0;
11300 int hold_tcblock = 0;
11301 int non_blocking = 0;
11302 int temp_flags = 0;
11303 uint32_t local_add_more, local_soresv = 0;
11310 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11312 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11315 SCTP_RELEASE_PKT(i_pak);
11319 if ((uio == NULL) && (i_pak == NULL)) {
11320 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11323 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11324 atomic_add_int(&inp->total_sends, 1);
11326 if (uio->uio_resid < 0) {
11327 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11330 sndlen = uio->uio_resid;
11332 top = SCTP_HEADER_TO_CHAIN(i_pak);
11333 sndlen = SCTP_HEADER_LEN(i_pak);
11335 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11339 * Pre-screen address, if one is given the sin-len
11340 * must be set correctly!
11343 if ((addr->sa_family == AF_INET) &&
11344 (addr->sa_len != sizeof(struct sockaddr_in))) {
11345 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11348 } else if ((addr->sa_family == AF_INET6) &&
11349 (addr->sa_len != sizeof(struct sockaddr_in6))) {
11350 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11357 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11358 (inp->sctp_socket->so_qlimit)) {
11359 /* The listener can NOT send */
11360 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11364 if ((use_rcvinfo) && srcv) {
11365 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11366 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11367 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11371 if (srcv->sinfo_flags)
11372 SCTP_STAT_INCR(sctps_sends_with_flags);
11374 if (srcv->sinfo_flags & SCTP_SENDALL) {
11375 /* its a sendall */
11376 error = sctp_sendall(inp, uio, top, srcv);
11381 /* now we must find the assoc */
11382 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11383 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11384 SCTP_INP_RLOCK(inp);
11385 stcb = LIST_FIRST(&inp->sctp_asoc_list);
11386 if (stcb == NULL) {
11387 SCTP_INP_RUNLOCK(inp);
11388 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11393 SCTP_INP_RUNLOCK(inp);
11395 /* Must locate the net structure if addr given */
11396 net = sctp_findnet(stcb, addr);
11398 /* validate port was 0 or correct */
11399 struct sockaddr_in *sin;
11401 sin = (struct sockaddr_in *)addr;
11402 if ((sin->sin_port != 0) &&
11403 (sin->sin_port != stcb->rport)) {
11407 temp_flags |= SCTP_ADDR_OVER;
11409 net = stcb->asoc.primary_destination;
11410 if (addr && (net == NULL)) {
11411 /* Could not find address, was it legal */
11412 if (addr->sa_family == AF_INET) {
11413 struct sockaddr_in *sin;
11415 sin = (struct sockaddr_in *)addr;
11416 if (sin->sin_addr.s_addr == 0) {
11417 if ((sin->sin_port == 0) ||
11418 (sin->sin_port == stcb->rport)) {
11419 net = stcb->asoc.primary_destination;
11423 struct sockaddr_in6 *sin6;
11425 sin6 = (struct sockaddr_in6 *)addr;
11426 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11427 if ((sin6->sin6_port == 0) ||
11428 (sin6->sin6_port == stcb->rport)) {
11429 net = stcb->asoc.primary_destination;
11435 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11439 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11440 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11444 * Must locate the net structure if addr
11447 net = sctp_findnet(stcb, addr);
11449 net = stcb->asoc.primary_destination;
11450 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11451 ((net == NULL) || (addr == NULL))) {
11452 struct sockaddr_in *sin;
11454 if (addr == NULL) {
11455 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11459 sin = (struct sockaddr_in *)addr;
11460 /* Validate port is 0 or correct */
11461 if ((sin->sin_port != 0) &&
11462 (sin->sin_port != stcb->rport)) {
11470 * Since we did not use findep we must
11471 * increment it, and if we don't find a tcb
11474 SCTP_INP_WLOCK(inp);
11475 SCTP_INP_INCR_REF(inp);
11476 SCTP_INP_WUNLOCK(inp);
11477 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11478 if (stcb == NULL) {
11479 SCTP_INP_WLOCK(inp);
11480 SCTP_INP_DECR_REF(inp);
11481 SCTP_INP_WUNLOCK(inp);
11486 if ((stcb == NULL) && (addr)) {
11487 /* Possible implicit send? */
11488 SCTP_ASOC_CREATE_LOCK(inp);
11489 create_lock_applied = 1;
11490 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11491 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11492 /* Should I really unlock ? */
11493 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11498 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11499 (addr->sa_family == AF_INET6)) {
11500 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11504 SCTP_INP_WLOCK(inp);
11505 SCTP_INP_INCR_REF(inp);
11506 SCTP_INP_WUNLOCK(inp);
11507 /* With the lock applied look again */
11508 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11509 if (stcb == NULL) {
11510 SCTP_INP_WLOCK(inp);
11511 SCTP_INP_DECR_REF(inp);
11512 SCTP_INP_WUNLOCK(inp);
11516 if (t_inp != inp) {
11517 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11522 if (stcb == NULL) {
11523 if (addr == NULL) {
11524 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
11529 * UDP style, we must go ahead and start the INIT
11534 if ((use_rcvinfo) && (srcv) &&
11535 ((srcv->sinfo_flags & SCTP_ABORT) ||
11536 ((srcv->sinfo_flags & SCTP_EOF) &&
11539 * User asks to abort a non-existant assoc,
11540 * or EOF a non-existant assoc with no data
11542 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
11546 /* get an asoc/stcb struct */
11547 vrf_id = inp->def_vrf_id;
11549 if (create_lock_applied == 0) {
11550 panic("Error, should hold create lock and I don't?");
11553 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
11556 if (stcb == NULL) {
11557 /* Error is setup for us in the call */
11560 if (create_lock_applied) {
11561 SCTP_ASOC_CREATE_UNLOCK(inp);
11562 create_lock_applied = 0;
11564 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
11567 * Turn on queue only flag to prevent data from
11571 asoc = &stcb->asoc;
11572 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
11573 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
11575 /* initialize authentication params for the assoc */
11576 sctp_initialize_auth_params(inp, stcb);
11580 * see if a init structure exists in cmsg
11583 struct sctp_initmsg initm;
11586 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
11589 * we have an INIT override of the
11592 if (initm.sinit_max_attempts)
11593 asoc->max_init_times = initm.sinit_max_attempts;
11594 if (initm.sinit_num_ostreams)
11595 asoc->pre_open_streams = initm.sinit_num_ostreams;
11596 if (initm.sinit_max_instreams)
11597 asoc->max_inbound_streams = initm.sinit_max_instreams;
11598 if (initm.sinit_max_init_timeo)
11599 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
11600 if (asoc->streamoutcnt < asoc->pre_open_streams) {
11601 /* Default is NOT correct */
11602 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
11603 asoc->streamoutcnt, asoc->pre_open_streams);
11605 * What happens if this
11606 * fails? we panic ...
11609 struct sctp_stream_out *tmp_str;
11612 if (hold_tcblock) {
11614 SCTP_TCB_UNLOCK(stcb);
11616 SCTP_MALLOC(tmp_str,
11617 struct sctp_stream_out *,
11618 (asoc->pre_open_streams *
11619 sizeof(struct sctp_stream_out)),
11622 SCTP_TCB_LOCK(stcb);
11624 if (tmp_str != NULL) {
11625 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
11626 asoc->strmout = tmp_str;
11627 asoc->streamoutcnt = asoc->pre_open_streams;
11629 asoc->pre_open_streams = asoc->streamoutcnt;
11632 for (i = 0; i < asoc->streamoutcnt; i++) {
11634 * inbound side must be set
11635 * to 0xffff, also NOTE when
11636 * we get the INIT-ACK back
11637 * (for INIT sender) we MUST
11639 * (streamoutcnt) but first
11640 * check if we sent to any
11641 * of the upper streams that
11642 * were dropped (if some
11643 * were). Those that were
11644 * dropped must be notified
11645 * to the upper layer as
11648 asoc->strmout[i].next_sequence_sent = 0x0;
11649 TAILQ_INIT(&asoc->strmout[i].outqueue);
11650 asoc->strmout[i].stream_no = i;
11651 asoc->strmout[i].last_msg_incomplete = 0;
11652 asoc->strmout[i].next_spoke.tqe_next = 0;
11653 asoc->strmout[i].next_spoke.tqe_prev = 0;
11659 /* out with the INIT */
11660 queue_only_for_init = 1;
11662 * we may want to dig in after this call and adjust the MTU
11663 * value. It defaulted to 1500 (constant) but the ro
11664 * structure may now have an update and thus we may need to
11665 * change it BEFORE we append the message.
11667 net = stcb->asoc.primary_destination;
11668 asoc = &stcb->asoc;
11671 if ((SCTP_SO_IS_NBIO(so)
11672 || (flags & MSG_NBIO)
11676 asoc = &stcb->asoc;
11678 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
11679 if (sndlen > asoc->smallest_mtu) {
11680 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11685 /* would we block? */
11686 if (non_blocking) {
11687 if (hold_tcblock == 0) {
11688 SCTP_TCB_LOCK(stcb);
11691 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11692 if ((SCTP_SB_LIMIT_SND(so) <
11693 (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
11694 (stcb->asoc.chunks_on_out_queue >
11695 sctp_max_chunks_on_queue)) {
11696 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
11697 if (sndlen > SCTP_SB_LIMIT_SND(so))
11700 error = EWOULDBLOCK;
11703 stcb->asoc.sb_send_resv += sndlen;
11704 SCTP_TCB_UNLOCK(stcb);
11707 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
11709 local_soresv = sndlen;
11710 /* Keep the stcb from being freed under our feet */
11711 if (free_cnt_applied) {
11713 panic("refcnt already incremented");
11715 printf("refcnt:1 already incremented?\n");
11718 atomic_add_int(&stcb->asoc.refcnt, 1);
11719 free_cnt_applied = 1;
11721 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11722 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11723 error = ECONNRESET;
11726 if (create_lock_applied) {
11727 SCTP_ASOC_CREATE_UNLOCK(inp);
11728 create_lock_applied = 0;
11730 if (asoc->stream_reset_outstanding) {
11732 * Can't queue any data while stream reset is underway.
11734 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
11738 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11739 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11742 if ((use_rcvinfo == 0) || (srcv == NULL)) {
11743 /* Grab the default stuff from the asoc */
11744 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
11746 /* we are now done with all control */
11748 sctp_m_freem(control);
11751 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11752 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11753 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11754 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11755 if ((use_rcvinfo) &&
11756 (srcv->sinfo_flags & SCTP_ABORT)) {
11759 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11760 error = ECONNRESET;
11764 /* Ok, we will attempt a msgsnd :> */
11766 p->td_ru.ru_msgsnd++;
11769 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
11770 net = stcb->asoc.primary_destination;
11774 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11778 if ((net->flight_size > net->cwnd) && (sctp_cmt_on_off == 0)) {
11780 * CMT: Added check for CMT above. net above is the primary
11781 * dest. If CMT is ON, sender should always attempt to send
11782 * with the output routine sctp_fill_outqueue() that loops
11783 * through all destination addresses. Therefore, if CMT is
11784 * ON, queue_only is NOT set to 1 here, so that
11785 * sctp_chunk_output() can be called below.
11789 } else if (asoc->ifp_had_enobuf) {
11790 SCTP_STAT_INCR(sctps_ifnomemqueued);
11791 if (net->flight_size > (net->mtu * 2))
11793 asoc->ifp_had_enobuf = 0;
11795 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11796 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
11798 /* Are we aborting? */
11799 if (srcv->sinfo_flags & SCTP_ABORT) {
11801 int tot_demand, tot_out = 0, max_out;
11803 SCTP_STAT_INCR(sctps_sends_with_abort);
11804 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11805 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11806 /* It has to be up before we abort */
11807 /* how big is the user initiated abort? */
11808 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11812 if (hold_tcblock) {
11813 SCTP_TCB_UNLOCK(stcb);
11817 struct mbuf *cntm = NULL;
11819 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
11823 tot_out += SCTP_BUF_LEN(cntm);
11824 cntm = SCTP_BUF_NEXT(cntm);
11827 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11829 /* Must fit in a MTU */
11831 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11832 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
11834 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11838 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
11841 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11845 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
11846 max_out -= sizeof(struct sctp_abort_msg);
11847 if (tot_out > max_out) {
11851 struct sctp_paramhdr *ph;
11853 /* now move forward the data pointer */
11854 ph = mtod(mm, struct sctp_paramhdr *);
11855 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
11856 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
11858 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
11860 error = uiomove((caddr_t)ph, (int)tot_out, uio);
11863 * Here if we can't get his data we
11864 * still abort we just don't get to
11865 * send the users note :-0
11872 SCTP_BUF_NEXT(mm) = top;
11876 if (hold_tcblock == 0) {
11877 SCTP_TCB_LOCK(stcb);
11880 atomic_add_int(&stcb->asoc.refcnt, -1);
11881 free_cnt_applied = 0;
11882 /* release this lock, otherwise we hang on ourselves */
11883 sctp_abort_an_association(stcb->sctp_ep, stcb,
11884 SCTP_RESPONSE_TO_USER_REQ,
11885 mm, SCTP_SO_LOCKED);
11886 /* now relock the stcb so everything is sane */
11890 * In this case top is already chained to mm avoid double
11891 * free, since we free it below if top != NULL and driver
11892 * would free it after sending the packet out
11899 /* Calculate the maximum we can send */
11900 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11901 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
11902 if (non_blocking) {
11903 /* we already checked for non-blocking above. */
11906 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
11911 if (hold_tcblock) {
11912 SCTP_TCB_UNLOCK(stcb);
11915 /* Is the stream no. valid? */
11916 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
11917 /* Invalid stream number */
11918 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11922 if (asoc->strmout == NULL) {
11923 /* huh? software error */
11924 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11928 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
11929 if ((user_marks_eor == 0) &&
11930 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
11931 /* It will NEVER fit */
11932 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11936 if ((uio == NULL) && user_marks_eor) {
11938 * We do not support eeor mode for
11939 * sending with mbuf chains (like sendfile).
11941 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11945 if (user_marks_eor) {
11946 local_add_more = sctp_add_more_threshold;
11949 * For non-eeor the whole message must fit in
11950 * the socket send buffer.
11952 local_add_more = sndlen;
11955 if (non_blocking) {
11956 goto skip_preblock;
11958 if (((max_len <= local_add_more) &&
11959 (SCTP_SB_LIMIT_SND(so) > local_add_more)) ||
11960 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) > sctp_max_chunks_on_queue)) { /* if */
11961 /* No room right no ! */
11962 SOCKBUF_LOCK(&so->so_snd);
11963 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11964 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + sctp_add_more_threshold)) ||
11965 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) > sctp_max_chunks_on_queue /* while */ )) {
11967 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11968 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
11972 stcb->block_entry = &be;
11973 error = sbwait(&so->so_snd);
11974 stcb->block_entry = NULL;
11975 if (error || so->so_error || be.error) {
11978 error = so->so_error;
11983 SOCKBUF_UNLOCK(&so->so_snd);
11986 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11987 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
11988 so, asoc, stcb->asoc.total_output_queue_size);
11990 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11993 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11995 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11996 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
11997 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12001 SOCKBUF_UNLOCK(&so->so_snd);
12004 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12007 atomic_add_int(&stcb->total_sends, 1);
12009 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12010 * case NOTE: uio will be null when top/mbuf is passed
12013 if (srcv->sinfo_flags & SCTP_EOF) {
12014 got_all_of_the_send = 1;
12017 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12023 struct sctp_stream_queue_pending *sp;
12024 struct sctp_stream_out *strm;
12025 uint32_t sndout, initial_out;
12027 initial_out = uio->uio_resid;
12029 SCTP_TCB_SEND_LOCK(stcb);
12030 if ((asoc->stream_locked) &&
12031 (asoc->stream_locked_on != srcv->sinfo_stream)) {
12032 SCTP_TCB_SEND_UNLOCK(stcb);
12033 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12037 SCTP_TCB_SEND_UNLOCK(stcb);
12039 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12040 if (strm->last_msg_incomplete == 0) {
12042 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12043 if ((sp == NULL) || (error)) {
12046 SCTP_TCB_SEND_LOCK(stcb);
12047 if (sp->msg_is_complete) {
12048 strm->last_msg_incomplete = 0;
12049 asoc->stream_locked = 0;
12052 * Just got locked to this guy in case of an
12055 strm->last_msg_incomplete = 1;
12056 asoc->stream_locked = 1;
12057 asoc->stream_locked_on = srcv->sinfo_stream;
12058 sp->sender_all_done = 0;
12060 sctp_snd_sb_alloc(stcb, sp->length);
12061 atomic_add_int(&asoc->stream_queue_cnt, 1);
12062 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12063 sp->strseq = strm->next_sequence_sent;
12064 if (sctp_logging_level & SCTP_LOG_AT_SEND_2_SCTP) {
12065 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12066 (uintptr_t) stcb, sp->length,
12067 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12069 strm->next_sequence_sent++;
12071 SCTP_STAT_INCR(sctps_sends_with_unord);
12073 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12074 if ((strm->next_spoke.tqe_next == NULL) &&
12075 (strm->next_spoke.tqe_prev == NULL)) {
12076 /* Not on wheel, insert */
12077 sctp_insert_on_wheel(stcb, asoc, strm, 1);
12079 SCTP_TCB_SEND_UNLOCK(stcb);
12081 SCTP_TCB_SEND_LOCK(stcb);
12082 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12083 SCTP_TCB_SEND_UNLOCK(stcb);
12085 /* ???? Huh ??? last msg is gone */
12087 panic("Warning: Last msg marked incomplete, yet nothing left?");
12089 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12090 strm->last_msg_incomplete = 0;
12096 while (uio->uio_resid > 0) {
12097 /* How much room do we have? */
12098 struct mbuf *new_tail, *mm;
12100 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12101 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12105 if ((max_len > sctp_add_more_threshold) ||
12106 (max_len && (SCTP_SB_LIMIT_SND(so) < sctp_add_more_threshold)) ||
12108 (uio->uio_resid <= (int)max_len))) {
12111 if (hold_tcblock) {
12112 SCTP_TCB_UNLOCK(stcb);
12115 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12116 if ((mm == NULL) || error) {
12122 /* Update the mbuf and count */
12123 SCTP_TCB_SEND_LOCK(stcb);
12124 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12126 * we need to get out. Peer probably
12130 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12131 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12132 error = ECONNRESET;
12134 SCTP_TCB_SEND_UNLOCK(stcb);
12137 if (sp->tail_mbuf) {
12138 /* tack it to the end */
12139 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12140 sp->tail_mbuf = new_tail;
12142 /* A stolen mbuf */
12144 sp->tail_mbuf = new_tail;
12146 sctp_snd_sb_alloc(stcb, sndout);
12147 atomic_add_int(&sp->length, sndout);
12150 /* Did we reach EOR? */
12151 if ((uio->uio_resid == 0) &&
12152 ((user_marks_eor == 0) ||
12153 (srcv->sinfo_flags & SCTP_EOF) ||
12154 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
12156 sp->msg_is_complete = 1;
12158 sp->msg_is_complete = 0;
12160 SCTP_TCB_SEND_UNLOCK(stcb);
12162 if (uio->uio_resid == 0) {
12167 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12169 * This is ugly but we must assure locking
12172 if (hold_tcblock == 0) {
12173 SCTP_TCB_LOCK(stcb);
12176 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12177 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12178 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12179 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12185 SCTP_TCB_UNLOCK(stcb);
12188 /* wait for space now */
12189 if (non_blocking) {
12190 /* Non-blocking io in place out */
12193 if ((net->flight_size > net->cwnd) &&
12194 (sctp_cmt_on_off == 0)) {
12196 } else if (asoc->ifp_had_enobuf) {
12197 SCTP_STAT_INCR(sctps_ifnomemqueued);
12198 if (net->flight_size > (net->mtu * 2)) {
12203 asoc->ifp_had_enobuf = 0;
12204 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12205 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12206 sizeof(struct sctp_data_chunk)));
12208 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12209 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12210 sizeof(struct sctp_data_chunk)));
12211 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12213 SCTP_STAT_INCR(sctps_send_burst_avoid);
12214 } else if (net->flight_size > net->cwnd) {
12216 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12221 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12222 (stcb->asoc.total_flight > 0) &&
12223 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12224 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12228 * Ok, Nagle is set on and we have data outstanding.
12229 * Don't send anything and let SACKs drive out the
12230 * data unless wen have a "full" segment to send.
12232 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12233 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12235 SCTP_STAT_INCR(sctps_naglequeued);
12238 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12239 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12240 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12242 SCTP_STAT_INCR(sctps_naglesent);
12245 /* What about the INIT, send it maybe */
12246 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
12248 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12249 nagle_applies, un_sent);
12250 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12251 stcb->asoc.total_flight,
12252 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12254 if (queue_only_for_init) {
12255 if (hold_tcblock == 0) {
12256 SCTP_TCB_LOCK(stcb);
12259 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12260 /* a collision took us forward? */
12261 queue_only_for_init = 0;
12264 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12265 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12266 queue_only_for_init = 0;
12270 if ((queue_only == 0) && (nagle_applies == 0)
12273 * need to start chunk output
12274 * before blocking.. note that if
12275 * a lock is already applied, then
12276 * the input via the net is happening
12277 * and I don't need to start output :-D
12279 if (hold_tcblock == 0) {
12280 if (SCTP_TCB_TRYLOCK(stcb)) {
12282 sctp_chunk_output(inp,
12284 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12287 sctp_chunk_output(inp,
12289 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12291 if (hold_tcblock == 1) {
12292 SCTP_TCB_UNLOCK(stcb);
12296 SOCKBUF_LOCK(&so->so_snd);
12298 * This is a bit strange, but I think it will
12299 * work. The total_output_queue_size is locked and
12300 * protected by the TCB_LOCK, which we just released.
12301 * There is a race that can occur between releasing it
12302 * above, and me getting the socket lock, where sacks
12303 * come in but we have not put the SB_WAIT on the
12304 * so_snd buffer to get the wakeup. After the LOCK
12305 * is applied the sack_processing will also need to
12306 * LOCK the so->so_snd to do the actual sowwakeup(). So
12307 * once we have the socket buffer lock if we recheck the
12308 * size we KNOW we will get to sleep safely with the
12309 * wakeup flag in place.
12311 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12312 min(sctp_add_more_threshold, SCTP_SB_LIMIT_SND(so)))
12314 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
12315 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12316 so, asoc, uio->uio_resid);
12319 stcb->block_entry = &be;
12320 error = sbwait(&so->so_snd);
12321 stcb->block_entry = NULL;
12323 if (error || so->so_error || be.error) {
12326 error = so->so_error;
12331 SOCKBUF_UNLOCK(&so->so_snd);
12334 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
12335 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12336 so, asoc, stcb->asoc.total_output_queue_size);
12339 SOCKBUF_UNLOCK(&so->so_snd);
12340 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12344 SCTP_TCB_SEND_LOCK(stcb);
12346 if (sp->msg_is_complete == 0) {
12347 strm->last_msg_incomplete = 1;
12348 asoc->stream_locked = 1;
12349 asoc->stream_locked_on = srcv->sinfo_stream;
12351 sp->sender_all_done = 1;
12352 strm->last_msg_incomplete = 0;
12353 asoc->stream_locked = 0;
12356 SCTP_PRINTF("Huh no sp TSNH?\n");
12357 strm->last_msg_incomplete = 0;
12358 asoc->stream_locked = 0;
12360 SCTP_TCB_SEND_UNLOCK(stcb);
12361 if (uio->uio_resid == 0) {
12362 got_all_of_the_send = 1;
12365 /* We send in a 0, since we do NOT have any locks */
12366 error = sctp_msg_append(stcb, net, top, srcv, 0);
12368 if (srcv->sinfo_flags & SCTP_EOF) {
12370 * This should only happen for Panda for the mbuf
12371 * send case, which does NOT yet support EEOR mode.
12372 * Thus, we can just set this flag to do the proper
12375 got_all_of_the_send = 1;
12383 if ((srcv->sinfo_flags & SCTP_EOF) &&
12384 (got_all_of_the_send == 1) &&
12385 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12389 SCTP_STAT_INCR(sctps_sends_with_eof);
12391 if (hold_tcblock == 0) {
12392 SCTP_TCB_LOCK(stcb);
12395 cnt = sctp_is_there_unsent_data(stcb);
12396 if (TAILQ_EMPTY(&asoc->send_queue) &&
12397 TAILQ_EMPTY(&asoc->sent_queue) &&
12399 if (asoc->locked_on_sending) {
12402 /* there is nothing queued to send, so I'm done... */
12403 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12404 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12405 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12406 /* only send SHUTDOWN the first time through */
12407 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12408 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12409 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12411 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12412 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12413 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12414 asoc->primary_destination);
12415 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12416 asoc->primary_destination);
12420 * we still got (or just got) data to send, so set
12424 * XXX sockets draft says that SCTP_EOF should be
12425 * sent with no data. currently, we will allow user
12426 * data to be sent first and move to
12429 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12430 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12431 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12432 if (hold_tcblock == 0) {
12433 SCTP_TCB_LOCK(stcb);
12436 if (asoc->locked_on_sending) {
12437 /* Locked to send out the data */
12438 struct sctp_stream_queue_pending *sp;
12440 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12442 if ((sp->length == 0) && (sp->msg_is_complete == 0))
12443 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12446 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12447 if (TAILQ_EMPTY(&asoc->send_queue) &&
12448 TAILQ_EMPTY(&asoc->sent_queue) &&
12449 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12451 if (free_cnt_applied) {
12452 atomic_add_int(&stcb->asoc.refcnt, -1);
12453 free_cnt_applied = 0;
12455 sctp_abort_an_association(stcb->sctp_ep, stcb,
12456 SCTP_RESPONSE_TO_USER_REQ,
12457 NULL, SCTP_SO_LOCKED);
12459 * now relock the stcb so everything
12466 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12467 asoc->primary_destination);
12468 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12473 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12474 some_on_control = 1;
12476 if ((net->flight_size > net->cwnd) &&
12477 (sctp_cmt_on_off == 0)) {
12479 } else if (asoc->ifp_had_enobuf) {
12480 SCTP_STAT_INCR(sctps_ifnomemqueued);
12481 if (net->flight_size > (net->mtu * 2)) {
12486 asoc->ifp_had_enobuf = 0;
12487 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12488 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12489 sizeof(struct sctp_data_chunk)));
12491 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12492 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12493 sizeof(struct sctp_data_chunk)));
12494 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12496 SCTP_STAT_INCR(sctps_send_burst_avoid);
12497 } else if (net->flight_size > net->cwnd) {
12499 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12504 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12505 (stcb->asoc.total_flight > 0) &&
12506 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12507 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12510 * Ok, Nagle is set on and we have data outstanding.
12511 * Don't send anything and let SACKs drive out the
12512 * data unless wen have a "full" segment to send.
12514 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12515 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12517 SCTP_STAT_INCR(sctps_naglequeued);
12520 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12521 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12522 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12524 SCTP_STAT_INCR(sctps_naglesent);
12527 if (queue_only_for_init) {
12528 if (hold_tcblock == 0) {
12529 SCTP_TCB_LOCK(stcb);
12532 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12533 /* a collision took us forward? */
12534 queue_only_for_init = 0;
12537 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12538 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
12539 queue_only_for_init = 0;
12543 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
12544 /* we can attempt to send too. */
12545 if (hold_tcblock == 0) {
12547 * If there is activity recv'ing sacks no need to
12550 if (SCTP_TCB_TRYLOCK(stcb)) {
12551 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12555 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12557 } else if ((queue_only == 0) &&
12558 (stcb->asoc.peers_rwnd == 0) &&
12559 (stcb->asoc.total_flight == 0)) {
12560 /* We get to have a probe outstanding */
12561 if (hold_tcblock == 0) {
12563 SCTP_TCB_LOCK(stcb);
12565 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12566 } else if (some_on_control) {
12567 int num_out, reason, cwnd_full, frag_point;
12569 /* Here we do control only */
12570 if (hold_tcblock == 0) {
12572 SCTP_TCB_LOCK(stcb);
12574 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
12575 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
12576 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
12578 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d",
12579 queue_only, stcb->asoc.peers_rwnd, un_sent,
12580 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
12581 stcb->asoc.total_output_queue_size, error);
12586 if (local_soresv && stcb) {
12587 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
12590 if (create_lock_applied) {
12591 SCTP_ASOC_CREATE_UNLOCK(inp);
12592 create_lock_applied = 0;
12594 if ((stcb) && hold_tcblock) {
12595 SCTP_TCB_UNLOCK(stcb);
12597 if (stcb && free_cnt_applied) {
12598 atomic_add_int(&stcb->asoc.refcnt, -1);
12602 if (mtx_owned(&stcb->tcb_mtx)) {
12603 panic("Leaving with tcb mtx owned?");
12605 if (mtx_owned(&stcb->tcb_send_mtx)) {
12606 panic("Leaving with tcb send mtx owned?");
12614 sctp_m_freem(control);
12621 * generate an AUTHentication chunk, if required
12624 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
12625 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
12626 struct sctp_tcb *stcb, uint8_t chunk)
12628 struct mbuf *m_auth;
12629 struct sctp_auth_chunk *auth;
12632 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
12636 /* sysctl disabled auth? */
12637 if (sctp_auth_disable)
12640 /* peer doesn't do auth... */
12641 if (!stcb->asoc.peer_supports_auth) {
12644 /* does the requested chunk require auth? */
12645 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
12648 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
12649 if (m_auth == NULL) {
12653 /* reserve some space if this will be the first mbuf */
12655 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
12656 /* fill in the AUTH chunk details */
12657 auth = mtod(m_auth, struct sctp_auth_chunk *);
12658 bzero(auth, sizeof(*auth));
12659 auth->ch.chunk_type = SCTP_AUTHENTICATION;
12660 auth->ch.chunk_flags = 0;
12661 chunk_len = sizeof(*auth) +
12662 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
12663 auth->ch.chunk_length = htons(chunk_len);
12664 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
12665 /* key id and hmac digest will be computed and filled in upon send */
12667 /* save the offset where the auth was inserted into the chain */
12674 *offset += SCTP_BUF_LEN(cn);
12675 cn = SCTP_BUF_NEXT(cn);
12680 /* update length and return pointer to the auth chunk */
12681 SCTP_BUF_LEN(m_auth) = chunk_len;
12682 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
12683 if (auth_ret != NULL)
12691 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
12693 struct nd_prefix *pfx = NULL;
12694 struct nd_pfxrouter *pfxrtr = NULL;
12695 struct sockaddr_in6 gw6;
12697 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
12700 /* get prefix entry of address */
12701 LIST_FOREACH(pfx, &nd_prefix, ndpr_entry) {
12702 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
12704 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
12705 &src6->sin6_addr, &pfx->ndpr_mask))
12708 /* no prefix entry in the prefix list */
12710 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
12711 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
12714 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
12715 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
12717 /* search installed gateway from prefix entry */
12718 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
12719 pfxrtr->pfr_next) {
12720 memset(&gw6, 0, sizeof(struct sockaddr_in6));
12721 gw6.sin6_family = AF_INET6;
12722 gw6.sin6_len = sizeof(struct sockaddr_in6);
12723 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
12724 sizeof(struct in6_addr));
12725 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
12726 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
12727 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
12728 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
12729 if (sctp_cmpaddr((struct sockaddr *)&gw6,
12730 ro->ro_rt->rt_gateway)) {
12731 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
12735 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
12742 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
12744 struct sockaddr_in *sin, *mask;
12745 struct ifaddr *ifa;
12746 struct in_addr srcnetaddr, gwnetaddr;
12748 if (ro == NULL || ro->ro_rt == NULL ||
12749 sifa->address.sa.sa_family != AF_INET) {
12752 ifa = (struct ifaddr *)sifa->ifa;
12753 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
12754 sin = (struct sockaddr_in *)&sifa->address.sin;
12755 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
12756 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
12757 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
12758 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
12760 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
12761 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
12762 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
12763 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
12764 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
12765 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {