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 if ((ifa->address.sa.sa_family == AF_INET) && ipv4_addr_legal) {
1878 struct sockaddr_in *sin;
1880 sin = (struct sockaddr_in *)&ifa->address.sin;
1881 if (sin->sin_addr.s_addr == 0) {
1882 /* not in scope , unspecified */
1885 if ((ipv4_local_scope == 0) &&
1886 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1887 /* private address not in scope */
1890 } else if ((ifa->address.sa.sa_family == AF_INET6) && ipv6_addr_legal) {
1891 struct sockaddr_in6 *sin6;
1894 * Must update the flags, bummer, which means any IFA locks
1895 * must now be applied HERE <->
1898 sctp_gather_internal_ifa_flags(ifa);
1900 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1903 /* ok to use deprecated addresses? */
1904 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1905 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1906 /* skip unspecifed addresses */
1909 if ( /* (local_scope == 0) && */
1910 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1913 if ((site_scope == 0) &&
1914 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1923 static struct mbuf *
1924 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1926 struct sctp_paramhdr *parmh;
1930 if (ifa->address.sa.sa_family == AF_INET) {
1931 len = sizeof(struct sctp_ipv4addr_param);
1932 } else if (ifa->address.sa.sa_family == AF_INET6) {
1933 len = sizeof(struct sctp_ipv6addr_param);
1938 if (M_TRAILINGSPACE(m) >= len) {
1939 /* easy side we just drop it on the end */
1940 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1943 /* Need more space */
1945 while (SCTP_BUF_NEXT(mret) != NULL) {
1946 mret = SCTP_BUF_NEXT(mret);
1948 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1949 if (SCTP_BUF_NEXT(mret) == NULL) {
1950 /* We are hosed, can't add more addresses */
1953 mret = SCTP_BUF_NEXT(mret);
1954 parmh = mtod(mret, struct sctp_paramhdr *);
1956 /* now add the parameter */
1957 if (ifa->address.sa.sa_family == AF_INET) {
1958 struct sctp_ipv4addr_param *ipv4p;
1959 struct sockaddr_in *sin;
1961 sin = (struct sockaddr_in *)&ifa->address.sin;
1962 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1963 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1964 parmh->param_length = htons(len);
1965 ipv4p->addr = sin->sin_addr.s_addr;
1966 SCTP_BUF_LEN(mret) += len;
1967 } else if (ifa->address.sa.sa_family == AF_INET6) {
1968 struct sctp_ipv6addr_param *ipv6p;
1969 struct sockaddr_in6 *sin6;
1971 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1972 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1973 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1974 parmh->param_length = htons(len);
1975 memcpy(ipv6p->addr, &sin6->sin6_addr,
1976 sizeof(ipv6p->addr));
1977 /* clear embedded scope in the address */
1978 in6_clearscope((struct in6_addr *)ipv6p->addr);
1979 SCTP_BUF_LEN(mret) += len;
1988 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
1989 struct mbuf *m_at, int cnt_inits_to)
1991 struct sctp_vrf *vrf = NULL;
1992 int cnt, limit_out = 0, total_count;
1995 vrf_id = inp->def_vrf_id;
1996 SCTP_IPI_ADDR_RLOCK();
1997 vrf = sctp_find_vrf(vrf_id);
1999 SCTP_IPI_ADDR_RUNLOCK();
2002 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2003 struct sctp_ifa *sctp_ifap;
2004 struct sctp_ifn *sctp_ifnp;
2007 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2009 cnt = SCTP_ADDRESS_LIMIT;
2012 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2013 if ((scope->loopback_scope == 0) &&
2014 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2016 * Skip loopback devices if loopback_scope
2021 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2022 if (sctp_is_address_in_scope(sctp_ifap,
2023 scope->ipv4_addr_legal,
2024 scope->ipv6_addr_legal,
2025 scope->loopback_scope,
2026 scope->ipv4_local_scope,
2028 scope->site_scope, 1) == 0) {
2032 if (cnt > SCTP_ADDRESS_LIMIT) {
2036 if (cnt > SCTP_ADDRESS_LIMIT) {
2043 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2045 if ((scope->loopback_scope == 0) &&
2046 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2048 * Skip loopback devices if
2049 * loopback_scope not set
2053 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2054 if (sctp_is_address_in_scope(sctp_ifap,
2055 scope->ipv4_addr_legal,
2056 scope->ipv6_addr_legal,
2057 scope->loopback_scope,
2058 scope->ipv4_local_scope,
2060 scope->site_scope, 0) == 0) {
2063 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2074 if (total_count > SCTP_ADDRESS_LIMIT) {
2075 /* No more addresses */
2083 struct sctp_laddr *laddr;
2086 /* First, how many ? */
2087 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2088 if (laddr->ifa == NULL) {
2091 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2093 * Address being deleted by the system, dont
2097 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2099 * Address being deleted on this ep don't
2104 if (sctp_is_address_in_scope(laddr->ifa,
2105 scope->ipv4_addr_legal,
2106 scope->ipv6_addr_legal,
2107 scope->loopback_scope,
2108 scope->ipv4_local_scope,
2110 scope->site_scope, 1) == 0) {
2115 if (cnt > SCTP_ADDRESS_LIMIT) {
2119 * To get through a NAT we only list addresses if we have
2120 * more than one. That way if you just bind a single address
2121 * we let the source of the init dictate our address.
2124 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2126 if (laddr->ifa == NULL) {
2129 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2132 if (sctp_is_address_in_scope(laddr->ifa,
2133 scope->ipv4_addr_legal,
2134 scope->ipv6_addr_legal,
2135 scope->loopback_scope,
2136 scope->ipv4_local_scope,
2138 scope->site_scope, 0) == 0) {
2141 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2143 if (cnt >= SCTP_ADDRESS_LIMIT) {
2149 SCTP_IPI_ADDR_RUNLOCK();
2153 static struct sctp_ifa *
2154 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2155 uint8_t dest_is_loop,
2156 uint8_t dest_is_priv,
2159 uint8_t dest_is_global = 0;
2161 /* dest_is_priv is true if destination is a private address */
2162 /* dest_is_loop is true if destination is a loopback addresses */
2165 * Here we determine if its a preferred address. A preferred address
2166 * means it is the same scope or higher scope then the destination.
2167 * L = loopback, P = private, G = global
2168 * ----------------------------------------- src | dest | result
2169 * ---------------------------------------- L | L | yes
2170 * ----------------------------------------- P | L |
2171 * yes-v4 no-v6 ----------------------------------------- G |
2172 * L | yes-v4 no-v6 ----------------------------------------- L
2173 * | P | no ----------------------------------------- P |
2174 * P | yes ----------------------------------------- G |
2175 * P | no ----------------------------------------- L | G
2176 * | no ----------------------------------------- P | G |
2177 * no ----------------------------------------- G | G |
2178 * yes -----------------------------------------
2181 if (ifa->address.sa.sa_family != fam) {
2182 /* forget mis-matched family */
2185 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2188 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2189 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2190 /* Ok the address may be ok */
2191 if (fam == AF_INET6) {
2192 /* ok to use deprecated addresses? */
2193 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2194 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2197 if (ifa->src_is_priv) {
2199 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2203 if (ifa->src_is_glob) {
2205 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2211 * Now that we know what is what, implement or table this could in
2212 * theory be done slicker (it used to be), but this is
2213 * straightforward and easier to validate :-)
2215 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2216 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2217 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2218 dest_is_loop, dest_is_priv, dest_is_global);
2220 if ((ifa->src_is_loop) && (dest_is_priv)) {
2221 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2224 if ((ifa->src_is_glob) && (dest_is_priv)) {
2225 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2228 if ((ifa->src_is_loop) && (dest_is_global)) {
2229 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2232 if ((ifa->src_is_priv) && (dest_is_global)) {
2233 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2236 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2237 /* its a preferred address */
2241 static struct sctp_ifa *
2242 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2243 uint8_t dest_is_loop,
2244 uint8_t dest_is_priv,
2247 uint8_t dest_is_global = 0;
2251 * Here we determine if its a acceptable address. A acceptable
2252 * address means it is the same scope or higher scope but we can
2253 * allow for NAT which means its ok to have a global dest and a
2256 * L = loopback, P = private, G = global
2257 * ----------------------------------------- src | dest | result
2258 * ----------------------------------------- L | L | yes
2259 * ----------------------------------------- P | L |
2260 * yes-v4 no-v6 ----------------------------------------- G |
2261 * L | yes ----------------------------------------- L |
2262 * P | no ----------------------------------------- P | P
2263 * | yes ----------------------------------------- G | P
2264 * | yes - May not work -----------------------------------------
2265 * L | G | no ----------------------------------------- P
2266 * | G | yes - May not work
2267 * ----------------------------------------- G | G | yes
2268 * -----------------------------------------
2271 if (ifa->address.sa.sa_family != fam) {
2272 /* forget non matching family */
2275 /* Ok the address may be ok */
2276 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2279 if (fam == AF_INET6) {
2280 /* ok to use deprecated addresses? */
2281 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2284 if (ifa->src_is_priv) {
2285 /* Special case, linklocal to loop */
2291 * Now that we know what is what, implement our table. This could in
2292 * theory be done slicker (it used to be), but this is
2293 * straightforward and easier to validate :-)
2295 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2298 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2301 /* its an acceptable address */
2306 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2308 struct sctp_laddr *laddr;
2311 /* There are no restrictions, no TCB :-) */
2314 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2315 if (laddr->ifa == NULL) {
2316 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2320 if (laddr->ifa == ifa) {
2321 /* Yes it is on the list */
2330 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2332 struct sctp_laddr *laddr;
2336 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2337 if (laddr->ifa == NULL) {
2338 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2342 if ((laddr->ifa == ifa) && laddr->action == 0)
2351 static struct sctp_ifa *
2352 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2355 int non_asoc_addr_ok,
2356 uint8_t dest_is_priv,
2357 uint8_t dest_is_loop,
2360 struct sctp_laddr *laddr, *starting_point;
2363 struct sctp_ifn *sctp_ifn;
2364 struct sctp_ifa *sctp_ifa, *sifa;
2365 struct sctp_vrf *vrf;
2368 vrf = sctp_find_vrf(vrf_id);
2372 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2373 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2374 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2376 * first question, is the ifn we will emit on in our list, if so, we
2377 * want such an address. Note that we first looked for a preferred
2381 /* is a preferred one on the interface we route out? */
2382 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2383 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2384 (non_asoc_addr_ok == 0))
2386 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2391 if (sctp_is_addr_in_ep(inp, sifa)) {
2392 atomic_add_int(&sifa->refcount, 1);
2398 * ok, now we now need to find one on the list of the addresses. We
2399 * can't get one on the emitting interface so let's find first a
2400 * preferred one. If not that an acceptable one otherwise... we
2403 starting_point = inp->next_addr_touse;
2405 if (inp->next_addr_touse == NULL) {
2406 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2409 for (laddr = inp->next_addr_touse; laddr;
2410 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2411 if (laddr->ifa == NULL) {
2412 /* address has been removed */
2415 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2416 /* address is being deleted */
2419 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2423 atomic_add_int(&sifa->refcount, 1);
2426 if (resettotop == 0) {
2427 inp->next_addr_touse = NULL;
2430 inp->next_addr_touse = starting_point;
2433 if (inp->next_addr_touse == NULL) {
2434 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2437 /* ok, what about an acceptable address in the inp */
2438 for (laddr = inp->next_addr_touse; laddr;
2439 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2440 if (laddr->ifa == NULL) {
2441 /* address has been removed */
2444 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2445 /* address is being deleted */
2448 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2452 atomic_add_int(&sifa->refcount, 1);
2455 if (resettotop == 0) {
2456 inp->next_addr_touse = NULL;
2457 goto once_again_too;
2460 * no address bound can be a source for the destination we are in
2468 static struct sctp_ifa *
2469 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2470 struct sctp_tcb *stcb,
2471 struct sctp_nets *net,
2474 uint8_t dest_is_priv,
2475 uint8_t dest_is_loop,
2476 int non_asoc_addr_ok,
2479 struct sctp_laddr *laddr, *starting_point;
2481 struct sctp_ifn *sctp_ifn;
2482 struct sctp_ifa *sctp_ifa, *sifa;
2483 uint8_t start_at_beginning = 0;
2484 struct sctp_vrf *vrf;
2488 * first question, is the ifn we will emit on in our list, if so, we
2491 vrf = sctp_find_vrf(vrf_id);
2495 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2496 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2497 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2500 * first question, is the ifn we will emit on in our list? If so,
2501 * we want that one. First we look for a preferred. Second, we go
2502 * for an acceptable.
2505 /* first try for a preferred address on the ep */
2506 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2507 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2509 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2510 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2513 if ((non_asoc_addr_ok == 0) &&
2514 (sctp_is_addr_restricted(stcb, sifa))) {
2515 /* on the no-no list */
2518 atomic_add_int(&sifa->refcount, 1);
2522 /* next try for an acceptable address on the ep */
2523 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2524 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2526 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2527 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2530 if ((non_asoc_addr_ok == 0) &&
2531 (sctp_is_addr_restricted(stcb, sifa))) {
2532 /* on the no-no list */
2535 atomic_add_int(&sifa->refcount, 1);
2542 * if we can't find one like that then we must look at all addresses
2543 * bound to pick one at first preferable then secondly acceptable.
2545 starting_point = stcb->asoc.last_used_address;
2547 if (stcb->asoc.last_used_address == NULL) {
2548 start_at_beginning = 1;
2549 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2551 /* search beginning with the last used address */
2552 for (laddr = stcb->asoc.last_used_address; laddr;
2553 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2554 if (laddr->ifa == NULL) {
2555 /* address has been removed */
2558 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2559 /* address is being deleted */
2562 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2565 if ((non_asoc_addr_ok == 0) &&
2566 (sctp_is_addr_restricted(stcb, sifa))) {
2567 /* on the no-no list */
2570 stcb->asoc.last_used_address = laddr;
2571 atomic_add_int(&sifa->refcount, 1);
2574 if (start_at_beginning == 0) {
2575 stcb->asoc.last_used_address = NULL;
2576 goto sctp_from_the_top;
2578 /* now try for any higher scope than the destination */
2579 stcb->asoc.last_used_address = starting_point;
2580 start_at_beginning = 0;
2582 if (stcb->asoc.last_used_address == NULL) {
2583 start_at_beginning = 1;
2584 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2586 /* search beginning with the last used address */
2587 for (laddr = stcb->asoc.last_used_address; laddr;
2588 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2589 if (laddr->ifa == NULL) {
2590 /* address has been removed */
2593 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2594 /* address is being deleted */
2597 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2601 if ((non_asoc_addr_ok == 0) &&
2602 (sctp_is_addr_restricted(stcb, sifa))) {
2603 /* on the no-no list */
2606 stcb->asoc.last_used_address = laddr;
2607 atomic_add_int(&sifa->refcount, 1);
2610 if (start_at_beginning == 0) {
2611 stcb->asoc.last_used_address = NULL;
2612 goto sctp_from_the_top2;
2617 static struct sctp_ifa *
2618 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2619 struct sctp_tcb *stcb,
2620 int non_asoc_addr_ok,
2621 uint8_t dest_is_loop,
2622 uint8_t dest_is_priv,
2628 struct sctp_ifa *ifa, *sifa;
2629 int num_eligible_addr = 0;
2631 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2632 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2633 (non_asoc_addr_ok == 0))
2635 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2640 * Check if the IPv6 address matches to next-hop. In the
2641 * mobile case, old IPv6 address may be not deleted from the
2642 * interface. Then, the interface has previous and new
2643 * addresses. We should use one corresponding to the
2644 * next-hop. (by micchie)
2646 if (stcb && fam == AF_INET6 &&
2647 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2648 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2653 /* Avoid topologically incorrect IPv4 address */
2654 if (stcb && fam == AF_INET &&
2655 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2656 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2661 if ((non_asoc_addr_ok == 0) &&
2662 sctp_is_addr_restricted(stcb, sifa)) {
2664 * It is restricted for some reason..
2665 * probably not yet added.
2670 if (num_eligible_addr >= addr_wanted) {
2673 num_eligible_addr++;
2680 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2681 struct sctp_tcb *stcb,
2682 int non_asoc_addr_ok,
2683 uint8_t dest_is_loop,
2684 uint8_t dest_is_priv,
2687 struct sctp_ifa *ifa, *sifa;
2688 int num_eligible_addr = 0;
2690 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2691 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2692 (non_asoc_addr_ok == 0)) {
2695 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2701 if ((non_asoc_addr_ok == 0) &&
2702 sctp_is_addr_restricted(stcb, sifa)) {
2704 * It is restricted for some reason..
2705 * probably not yet added.
2710 num_eligible_addr++;
2712 return (num_eligible_addr);
2715 static struct sctp_ifa *
2716 sctp_choose_boundall(struct sctp_inpcb *inp,
2717 struct sctp_tcb *stcb,
2718 struct sctp_nets *net,
2721 uint8_t dest_is_priv,
2722 uint8_t dest_is_loop,
2723 int non_asoc_addr_ok,
2726 int cur_addr_num = 0, num_preferred = 0;
2728 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2729 struct sctp_ifa *sctp_ifa, *sifa;
2731 struct sctp_vrf *vrf;
2734 * For boundall we can use any address in the association.
2735 * If non_asoc_addr_ok is set we can use any address (at least in
2736 * theory). So we look for preferred addresses first. If we find one,
2737 * we use it. Otherwise we next try to get an address on the
2738 * interface, which we should be able to do (unless non_asoc_addr_ok
2739 * is false and we are routed out that way). In these cases where we
2740 * can't use the address of the interface we go through all the
2741 * ifn's looking for an address we can use and fill that in. Punting
2742 * means we send back address 0, which will probably cause problems
2743 * actually since then IP will fill in the address of the route ifn,
2744 * which means we probably already rejected it.. i.e. here comes an
2747 vrf = sctp_find_vrf(vrf_id);
2751 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2752 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2753 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2754 if (sctp_ifn == NULL) {
2755 /* ?? We don't have this guy ?? */
2756 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2757 goto bound_all_plan_b;
2759 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2760 ifn_index, sctp_ifn->ifn_name);
2763 cur_addr_num = net->indx_of_eligible_next_to_use;
2765 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2770 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2771 num_preferred, sctp_ifn->ifn_name);
2772 if (num_preferred == 0) {
2774 * no eligible addresses, we must use some other interface
2775 * address if we can find one.
2777 goto bound_all_plan_b;
2780 * Ok we have num_eligible_addr set with how many we can use, this
2781 * may vary from call to call due to addresses being deprecated
2784 if (cur_addr_num >= num_preferred) {
2788 * select the nth address from the list (where cur_addr_num is the
2789 * nth) and 0 is the first one, 1 is the second one etc...
2791 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2793 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2794 dest_is_priv, cur_addr_num, fam, ro);
2796 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2798 atomic_add_int(&sctp_ifa->refcount, 1);
2800 /* save off where the next one we will want */
2801 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2806 * plan_b: Look at all interfaces and find a preferred address. If
2807 * no preferred fall through to plan_c.
2810 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2811 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2812 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2813 sctp_ifn->ifn_name);
2814 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2815 /* wrong base scope */
2816 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2819 if ((sctp_ifn == looked_at) && looked_at) {
2820 /* already looked at this guy */
2821 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2824 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2825 dest_is_loop, dest_is_priv, fam);
2826 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2827 "Found ifn:%p %d preferred source addresses\n",
2828 ifn, num_preferred);
2829 if (num_preferred == 0) {
2830 /* None on this interface. */
2831 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2834 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2835 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2836 num_preferred, sctp_ifn, cur_addr_num);
2839 * Ok we have num_eligible_addr set with how many we can
2840 * use, this may vary from call to call due to addresses
2841 * being deprecated etc..
2843 if (cur_addr_num >= num_preferred) {
2846 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2847 dest_is_priv, cur_addr_num, fam, ro);
2851 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2852 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2854 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2855 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2856 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2857 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2859 atomic_add_int(&sifa->refcount, 1);
2864 /* plan_c: do we have an acceptable address on the emit interface */
2865 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2866 if (emit_ifn == NULL) {
2869 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2870 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2871 (non_asoc_addr_ok == 0))
2873 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2878 if ((non_asoc_addr_ok == 0) &&
2879 sctp_is_addr_restricted(stcb, sifa)) {
2881 * It is restricted for some reason..
2882 * probably not yet added.
2887 atomic_add_int(&sifa->refcount, 1);
2892 * plan_d: We are in trouble. No preferred address on the emit
2893 * interface. And not even a preferred address on all interfaces. Go
2894 * out and see if we can find an acceptable address somewhere
2895 * amongst all interfaces.
2897 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2898 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2899 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2900 /* wrong base scope */
2903 if ((sctp_ifn == looked_at) && looked_at)
2904 /* already looked at this guy */
2907 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2908 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2909 (non_asoc_addr_ok == 0))
2911 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2917 if ((non_asoc_addr_ok == 0) &&
2918 sctp_is_addr_restricted(stcb, sifa)) {
2920 * It is restricted for some
2921 * reason.. probably not yet added.
2926 atomic_add_int(&sifa->refcount, 1);
2931 * Ok we can find NO address to source from that is not on our
2932 * restricted list and non_asoc_address is NOT ok, or it is on our
2933 * restricted list. We can't source to it :-(
2940 /* tcb may be NULL */
2942 sctp_source_address_selection(struct sctp_inpcb *inp,
2943 struct sctp_tcb *stcb,
2945 struct sctp_nets *net,
2946 int non_asoc_addr_ok, uint32_t vrf_id)
2948 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
2949 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
2950 struct sctp_ifa *answer;
2951 uint8_t dest_is_priv, dest_is_loop;
2955 * Rules: - Find the route if needed, cache if I can. - Look at
2956 * interface address in route, Is it in the bound list. If so we
2957 * have the best source. - If not we must rotate amongst the
2962 * Do we need to pay attention to scope. We can have a private address
2963 * or a global address we are sourcing or sending to. So if we draw
2965 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2967 *------------------------------------------
2968 * source * dest * result
2969 * -----------------------------------------
2970 * <a> Private * Global * NAT
2971 * -----------------------------------------
2972 * <b> Private * Private * No problem
2973 * -----------------------------------------
2974 * <c> Global * Private * Huh, How will this work?
2975 * -----------------------------------------
2976 * <d> Global * Global * No Problem
2977 *------------------------------------------
2978 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2980 *------------------------------------------
2981 * source * dest * result
2982 * -----------------------------------------
2983 * <a> Linklocal * Global *
2984 * -----------------------------------------
2985 * <b> Linklocal * Linklocal * No problem
2986 * -----------------------------------------
2987 * <c> Global * Linklocal * Huh, How will this work?
2988 * -----------------------------------------
2989 * <d> Global * Global * No Problem
2990 *------------------------------------------
2991 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2993 * And then we add to that what happens if there are multiple addresses
2994 * assigned to an interface. Remember the ifa on a ifn is a linked
2995 * list of addresses. So one interface can have more than one IP
2996 * address. What happens if we have both a private and a global
2997 * address? Do we then use context of destination to sort out which
2998 * one is best? And what about NAT's sending P->G may get you a NAT
2999 * translation, or should you select the G thats on the interface in
3004 * - count the number of addresses on the interface.
3005 * - if it is one, no problem except case <c>.
3006 * For <a> we will assume a NAT out there.
3007 * - if there are more than one, then we need to worry about scope P
3008 * or G. We should prefer G -> G and P -> P if possible.
3009 * Then as a secondary fall back to mixed types G->P being a last
3011 * - The above all works for bound all, but bound specific we need to
3012 * use the same concept but instead only consider the bound
3013 * addresses. If the bound set is NOT assigned to the interface then
3014 * we must use rotation amongst the bound addresses..
3016 if (ro->ro_rt == NULL) {
3018 * Need a route to cache.
3020 SCTP_RTALLOC(ro, vrf_id);
3022 if (ro->ro_rt == NULL) {
3025 fam = to->sin_family;
3026 dest_is_priv = dest_is_loop = 0;
3027 /* Setup our scopes for the destination */
3028 if (fam == AF_INET) {
3029 /* Scope based on outbound address */
3030 if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3032 } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3035 /* mark it as local */
3036 net->addr_is_local = 1;
3039 } else if (fam == AF_INET6) {
3040 /* Scope based on outbound address */
3041 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3043 * If the route goes to the loopback address OR the
3044 * address is a loopback address, we are loopback
3045 * scope. But we don't use dest_is_priv (link local
3050 /* mark it as local */
3051 net->addr_is_local = 1;
3053 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3057 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3058 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3059 SCTP_IPI_ADDR_RLOCK();
3060 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3064 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3065 dest_is_priv, dest_is_loop,
3066 non_asoc_addr_ok, fam);
3067 SCTP_IPI_ADDR_RUNLOCK();
3074 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3075 vrf_id, dest_is_priv,
3077 non_asoc_addr_ok, fam);
3079 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3084 SCTP_IPI_ADDR_RUNLOCK();
3089 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3094 tlen = SCTP_BUF_LEN(control);
3097 * Independent of how many mbufs, find the c_type inside the control
3098 * structure and copy out the data.
3101 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3102 /* not enough room for one more we are done. */
3105 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3106 if (((int)cmh.cmsg_len + at) > tlen) {
3108 * this is real messed up since there is not enough
3109 * data here to cover the cmsg header. We are done.
3113 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3114 (c_type == cmh.cmsg_type)) {
3115 /* found the one we want, copy it out */
3116 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3117 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3119 * space of cmsg_len after header not big
3124 m_copydata(control, at, cpsize, data);
3127 at += CMSG_ALIGN(cmh.cmsg_len);
3128 if (cmh.cmsg_len == 0) {
3137 static struct mbuf *
3138 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3139 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3141 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3142 struct sctp_state_cookie *stc;
3143 struct sctp_paramhdr *ph;
3149 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3150 sizeof(struct sctp_paramhdr)), 0,
3151 M_DONTWAIT, 1, MT_DATA);
3155 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3156 if (copy_init == NULL) {
3160 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3162 if (copy_initack == NULL) {
3164 sctp_m_freem(copy_init);
3167 /* easy side we just drop it on the end */
3168 ph = mtod(mret, struct sctp_paramhdr *);
3169 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3170 sizeof(struct sctp_paramhdr);
3171 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3172 sizeof(struct sctp_paramhdr));
3173 ph->param_type = htons(SCTP_STATE_COOKIE);
3174 ph->param_length = 0; /* fill in at the end */
3175 /* Fill in the stc cookie data */
3176 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3178 /* tack the INIT and then the INIT-ACK onto the chain */
3181 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3182 cookie_sz += SCTP_BUF_LEN(m_at);
3183 if (SCTP_BUF_NEXT(m_at) == NULL) {
3184 SCTP_BUF_NEXT(m_at) = copy_init;
3189 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3190 cookie_sz += SCTP_BUF_LEN(m_at);
3191 if (SCTP_BUF_NEXT(m_at) == NULL) {
3192 SCTP_BUF_NEXT(m_at) = copy_initack;
3197 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3198 cookie_sz += SCTP_BUF_LEN(m_at);
3199 if (SCTP_BUF_NEXT(m_at) == NULL) {
3203 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3205 /* no space, so free the entire chain */
3209 SCTP_BUF_LEN(sig) = 0;
3210 SCTP_BUF_NEXT(m_at) = sig;
3212 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3213 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3215 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3216 cookie_sz += SCTP_SIGNATURE_SIZE;
3217 ph->param_length = htons(cookie_sz);
3223 sctp_get_ect(struct sctp_tcb *stcb,
3224 struct sctp_tmit_chunk *chk)
3226 uint8_t this_random;
3229 if (sctp_ecn_enable == 0)
3232 if (sctp_ecn_nonce == 0)
3233 /* no nonce, always return ECT0 */
3234 return (SCTP_ECT0_BIT);
3236 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3237 /* Peer does NOT support it, so we send a ECT0 only */
3238 return (SCTP_ECT0_BIT);
3241 return (SCTP_ECT0_BIT);
3243 if ((stcb->asoc.hb_random_idx > 3) ||
3244 ((stcb->asoc.hb_random_idx == 3) &&
3245 (stcb->asoc.hb_ect_randombit > 7))) {
3249 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3250 memcpy(stcb->asoc.hb_random_values, &rndval,
3251 sizeof(stcb->asoc.hb_random_values));
3252 this_random = stcb->asoc.hb_random_values[0];
3253 stcb->asoc.hb_random_idx = 0;
3254 stcb->asoc.hb_ect_randombit = 0;
3256 if (stcb->asoc.hb_ect_randombit > 7) {
3257 stcb->asoc.hb_ect_randombit = 0;
3258 stcb->asoc.hb_random_idx++;
3259 if (stcb->asoc.hb_random_idx > 3) {
3263 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3265 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3267 /* ECN Nonce stuff */
3268 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3269 stcb->asoc.hb_ect_randombit++;
3270 return (SCTP_ECT1_BIT);
3272 stcb->asoc.hb_ect_randombit++;
3273 return (SCTP_ECT0_BIT);
3278 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3279 struct sctp_tcb *stcb, /* may be NULL */
3280 struct sctp_nets *net,
3281 struct sockaddr *to,
3283 uint32_t auth_offset,
3284 struct sctp_auth_chunk *auth,
3285 int nofragment_flag,
3287 struct sctp_tmit_chunk *chk,
3290 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3294 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3297 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3298 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3299 * structure: - fill in the HMAC digest of any AUTH chunk in the
3300 * packet. - calculate and fill in the SCTP checksum. - prepend an
3301 * IP address header. - if boundall use INADDR_ANY. - if
3302 * boundspecific do source address selection. - set fragmentation
3303 * option for ipV4. - On return from IP output, check/adjust mtu
3304 * size of output interface and smallest_mtu size as well.
3306 /* Will need ifdefs around this */
3309 struct sctphdr *sctphdr;
3314 sctp_route_t *ro = NULL;
3316 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3317 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3322 vrf_id = stcb->asoc.vrf_id;
3324 vrf_id = inp->def_vrf_id;
3327 /* fill in the HMAC digest for any AUTH chunk in the packet */
3328 if ((auth != NULL) && (stcb != NULL)) {
3329 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3331 /* Calculate the csum and fill in the length of the packet */
3332 sctphdr = mtod(m, struct sctphdr *);
3333 if (sctp_no_csum_on_loopback &&
3335 (to->sa_family == AF_INET) &&
3336 (stcb->asoc.loopback_scope)) {
3337 sctphdr->checksum = 0;
3339 * This can probably now be taken out since my audit shows
3340 * no more bad pktlen's coming in. But we will wait a while
3343 packet_length = sctp_calculate_len(m);
3345 sctphdr->checksum = 0;
3346 csum = sctp_calculate_sum(m, &packet_length, 0);
3347 sctphdr->checksum = csum;
3350 if (to->sa_family == AF_INET) {
3351 struct ip *ip = NULL;
3352 sctp_route_t iproute;
3355 newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3358 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3361 SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3362 SCTP_BUF_LEN(newm) = sizeof(struct ip);
3363 packet_length += sizeof(struct ip);
3364 SCTP_BUF_NEXT(newm) = m;
3366 ip = mtod(m, struct ip *);
3367 ip->ip_v = IPVERSION;
3368 ip->ip_hl = (sizeof(struct ip) >> 2);
3370 tos_value = net->tos_flowlabel & 0x000000ff;
3372 tos_value = inp->ip_inp.inp.inp_ip_tos;
3374 if (nofragment_flag) {
3375 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__)
3378 ip->ip_off = htons(IP_DF);
3383 /* FreeBSD has a function for ip_id's */
3384 ip->ip_id = ip_newid();
3386 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3387 ip->ip_len = packet_length;
3389 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3391 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3394 ip->ip_tos = (u_char)(tos_value & 0xfc);
3397 /* no association at all */
3398 ip->ip_tos = (tos_value & 0xfc);
3400 ip->ip_p = IPPROTO_SCTP;
3404 memset(&iproute, 0, sizeof(iproute));
3405 memcpy(&ro->ro_dst, to, to->sa_len);
3407 ro = (sctp_route_t *) & net->ro;
3409 /* Now the address selection part */
3410 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3412 /* call the routine to select the src address */
3414 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3415 sctp_free_ifa(net->ro._s_addr);
3416 net->ro._s_addr = NULL;
3417 net->src_addr_selected = 0;
3423 if (net->src_addr_selected == 0) {
3424 if (out_of_asoc_ok) {
3428 /* Cache the source address */
3429 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3430 ro, net, out_of_asoc_ok,
3432 net->src_addr_selected = 1;
3434 if (net->ro._s_addr == NULL) {
3435 /* No route to host */
3436 net->src_addr_selected = 0;
3439 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3441 struct sctp_ifa *_lsrc;
3444 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3448 if (_lsrc == NULL) {
3451 ip->ip_src = _lsrc->address.sin.sin_addr;
3452 sctp_free_ifa(_lsrc);
3456 * If source address selection fails and we find no route
3457 * then the ip_output should fail as well with a
3458 * NO_ROUTE_TO_HOST type error. We probably should catch
3459 * that somewhere and abort the association right away
3460 * (assuming this is an INIT being sent).
3462 if ((ro->ro_rt == NULL)) {
3464 * src addr selection failed to find a route (or
3465 * valid source addr), so we can't get there from
3469 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3470 "%s: dropped packet - no valid source addr\n",
3473 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3474 "Destination was ");
3475 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3476 &net->ro._l_addr.sa);
3477 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3478 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3479 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3480 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3482 SCTP_FAILED_THRESHOLD,
3485 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3486 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3488 * JRS 5/14/07 - If a
3490 * unreachable, the PF bit
3491 * is turned off. This
3492 * allows an unambiguous use
3494 * destinations that are
3495 * reachable but potentially
3497 * destination is set to the
3498 * unreachable state, also
3499 * set the destination to
3503 * Add debug message here if
3504 * destination is not in PF
3508 * Stop any running T3
3511 if (sctp_cmt_on_off && sctp_cmt_pf) {
3512 net->dest_state &= ~SCTP_ADDR_PF;
3513 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3519 if (net == stcb->asoc.primary_destination) {
3520 /* need a new primary */
3521 struct sctp_nets *alt;
3523 alt = sctp_find_alternate_net(stcb, net, 0);
3525 if (sctp_set_primary_addr(stcb,
3526 (struct sockaddr *)NULL,
3528 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3529 if (net->ro._s_addr) {
3530 sctp_free_ifa(net->ro._s_addr);
3531 net->ro._s_addr = NULL;
3533 net->src_addr_selected = 0;
3539 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3541 return (EHOSTUNREACH);
3543 if (ro != &iproute) {
3544 memcpy(&iproute, ro, sizeof(*ro));
3546 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3547 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3548 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3549 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3550 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3553 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3554 /* failed to prepend data, give up */
3555 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3559 #ifdef SCTP_PACKET_LOGGING
3560 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3561 sctp_packet_log(m, packet_length);
3563 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3565 /* send it out. table id is taken from stcb */
3566 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3568 SCTP_STAT_INCR(sctps_sendpackets);
3569 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3571 SCTP_STAT_INCR(sctps_senderrors);
3573 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3575 /* free tempy routes */
3581 /* PMTU check versus smallest asoc MTU goes here */
3582 if ((ro->ro_rt != NULL) &&
3583 (net->ro._s_addr)) {
3586 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3588 (stcb->asoc.smallest_mtu > mtu)) {
3589 #ifdef SCTP_PRINT_FOR_B_AND_M
3590 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3593 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3596 } else if (ro->ro_rt == NULL) {
3597 /* route was freed */
3598 if (net->ro._s_addr &&
3599 net->src_addr_selected) {
3600 sctp_free_ifa(net->ro._s_addr);
3601 net->ro._s_addr = NULL;
3603 net->src_addr_selected = 0;
3609 else if (to->sa_family == AF_INET6) {
3611 struct ip6_hdr *ip6h;
3612 struct route_in6 ip6route;
3615 uint16_t flowBottom;
3616 u_char tosBottom, tosTop;
3617 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3619 struct sockaddr_in6 lsa6_storage;
3621 u_short prev_port = 0;
3624 flowlabel = net->tos_flowlabel;
3626 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3629 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3632 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3635 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3636 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3637 packet_length += sizeof(struct ip6_hdr);
3638 SCTP_BUF_NEXT(newm) = m;
3641 ip6h = mtod(m, struct ip6_hdr *);
3643 * We assume here that inp_flow is in host byte order within
3646 flowBottom = flowlabel & 0x0000ffff;
3647 flowTop = ((flowlabel & 0x000f0000) >> 16);
3648 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3649 /* protect *sin6 from overwrite */
3650 sin6 = (struct sockaddr_in6 *)to;
3654 /* KAME hack: embed scopeid */
3655 if (sa6_embedscope(sin6, ip6_use_defzone) != 0) {
3656 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3660 memset(&ip6route, 0, sizeof(ip6route));
3661 ro = (sctp_route_t *) & ip6route;
3662 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3664 ro = (sctp_route_t *) & net->ro;
3667 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3669 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3672 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3675 /* we could get no asoc if it is a O-O-T-B packet */
3676 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3678 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3679 ip6h->ip6_nxt = IPPROTO_SCTP;
3680 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3681 ip6h->ip6_dst = sin6->sin6_addr;
3684 * Add SRC address selection here: we can only reuse to a
3685 * limited degree the kame src-addr-sel, since we can try
3686 * their selection but it may not be bound.
3688 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3689 lsa6_tmp.sin6_family = AF_INET6;
3690 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3693 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3694 sctp_free_ifa(net->ro._s_addr);
3695 net->ro._s_addr = NULL;
3696 net->src_addr_selected = 0;
3702 if (net->src_addr_selected == 0) {
3703 if (out_of_asoc_ok) {
3707 /* Cache the source address */
3708 net->ro._s_addr = sctp_source_address_selection(inp,
3714 net->src_addr_selected = 1;
3716 if (net->ro._s_addr == NULL) {
3717 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3718 net->src_addr_selected = 0;
3721 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3723 struct sctp_ifa *_lsrc;
3726 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3730 if (_lsrc == NULL) {
3733 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3734 sctp_free_ifa(_lsrc);
3736 lsa6->sin6_port = inp->sctp_lport;
3738 if ((ro->ro_rt == NULL)) {
3740 * src addr selection failed to find a route (or
3741 * valid source addr), so we can't get there from
3747 * XXX: sa6 may not have a valid sin6_scope_id in the
3748 * non-SCOPEDROUTING case.
3750 bzero(&lsa6_storage, sizeof(lsa6_storage));
3751 lsa6_storage.sin6_family = AF_INET6;
3752 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3753 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3754 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3759 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3760 lsa6_storage.sin6_port = inp->sctp_lport;
3761 lsa6 = &lsa6_storage;
3762 ip6h->ip6_src = lsa6->sin6_addr;
3765 * We set the hop limit now since there is a good chance
3766 * that our ro pointer is now filled
3768 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3769 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3772 /* Copy to be sure something bad is not happening */
3773 sin6->sin6_addr = ip6h->ip6_dst;
3774 lsa6->sin6_addr = ip6h->ip6_src;
3777 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3778 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3779 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3780 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3781 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3783 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3784 /* preserve the port and scope for link local send */
3785 prev_scope = sin6->sin6_scope_id;
3786 prev_port = sin6->sin6_port;
3788 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3789 /* failed to prepend data, give up */
3791 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3794 #ifdef SCTP_PACKET_LOGGING
3795 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3796 sctp_packet_log(m, packet_length);
3798 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3800 /* send it out. table id is taken from stcb */
3801 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp,
3805 /* for link local this must be done */
3806 sin6->sin6_scope_id = prev_scope;
3807 sin6->sin6_port = prev_port;
3809 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
3810 SCTP_STAT_INCR(sctps_sendpackets);
3811 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3813 SCTP_STAT_INCR(sctps_senderrors);
3816 /* Now if we had a temp route free it */
3821 /* PMTU check versus smallest asoc MTU goes here */
3822 if (ro->ro_rt == NULL) {
3823 /* Route was freed */
3824 if (net->ro._s_addr &&
3825 net->src_addr_selected) {
3826 sctp_free_ifa(net->ro._s_addr);
3827 net->ro._s_addr = NULL;
3829 net->src_addr_selected = 0;
3831 if ((ro->ro_rt != NULL) &&
3832 (net->ro._s_addr)) {
3835 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3837 (stcb->asoc.smallest_mtu > mtu)) {
3838 #ifdef SCTP_PRINT_FOR_B_AND_M
3839 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
3842 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3846 if (ND_IFINFO(ifp)->linkmtu &&
3847 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
3848 #ifdef SCTP_PRINT_FOR_B_AND_M
3849 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
3850 ND_IFINFO(ifp)->linkmtu);
3852 sctp_mtu_size_reset(inp,
3854 ND_IFINFO(ifp)->linkmtu);
3862 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
3863 ((struct sockaddr *)to)->sa_family);
3865 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3872 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
3873 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3878 struct mbuf *m, *m_at, *mp_last;
3879 struct sctp_nets *net;
3880 struct sctp_init_msg *initm;
3881 struct sctp_supported_addr_param *sup_addr;
3882 struct sctp_ecn_supported_param *ecn;
3883 struct sctp_prsctp_supported_param *prsctp;
3884 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3885 struct sctp_supported_chunk_types_param *pr_supported;
3886 int cnt_inits_to = 0;
3891 /* INIT's always go to the primary (and usually ONLY address) */
3893 net = stcb->asoc.primary_destination;
3895 net = TAILQ_FIRST(&stcb->asoc.nets);
3900 /* we confirm any address we send an INIT to */
3901 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3902 (void)sctp_set_primary_addr(stcb, NULL, net);
3904 /* we confirm any address we send an INIT to */
3905 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3907 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
3908 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
3910 * special hook, if we are sending to link local it will not
3911 * show up in our private address count.
3913 struct sockaddr_in6 *sin6l;
3915 sin6l = &net->ro._l_addr.sin6;
3916 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
3919 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3920 /* This case should not happen */
3921 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
3924 /* start the INIT timer */
3925 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
3927 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3929 /* No memory, INIT timer will re-attempt. */
3930 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
3933 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
3935 * assume peer supports asconf in order to be able to queue local
3936 * address changes while an INIT is in flight and before the assoc
3939 stcb->asoc.peer_supports_asconf = 1;
3940 /* Now lets put the SCTP header in place */
3941 initm = mtod(m, struct sctp_init_msg *);
3942 initm->sh.src_port = inp->sctp_lport;
3943 initm->sh.dest_port = stcb->rport;
3944 initm->sh.v_tag = 0;
3945 initm->sh.checksum = 0; /* calculate later */
3946 /* now the chunk header */
3947 initm->msg.ch.chunk_type = SCTP_INITIATION;
3948 initm->msg.ch.chunk_flags = 0;
3949 /* fill in later from mbuf we build */
3950 initm->msg.ch.chunk_length = 0;
3951 /* place in my tag */
3952 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
3953 /* set up some of the credits. */
3954 initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
3955 SCTP_MINIMAL_RWND));
3957 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
3958 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
3959 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
3960 /* now the address restriction */
3961 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
3963 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
3964 /* we support 2 types IPv6/IPv4 */
3965 sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
3967 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
3968 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
3969 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
3971 if (inp->sctp_ep.adaptation_layer_indicator) {
3972 struct sctp_adaptation_layer_indication *ali;
3974 ali = (struct sctp_adaptation_layer_indication *)(
3975 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
3976 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
3977 ali->ph.param_length = htons(sizeof(*ali));
3978 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
3979 SCTP_BUF_LEN(m) += sizeof(*ali);
3980 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3983 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
3984 sizeof(*sup_addr) + sizeof(uint16_t));
3987 /* now any cookie time extensions */
3988 if (stcb->asoc.cookie_preserve_req) {
3989 struct sctp_cookie_perserve_param *cookie_preserve;
3991 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
3992 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
3993 cookie_preserve->ph.param_length = htons(
3994 sizeof(*cookie_preserve));
3995 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
3996 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
3997 ecn = (struct sctp_ecn_supported_param *)(
3998 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
3999 stcb->asoc.cookie_preserve_req = 0;
4002 if (sctp_ecn_enable == 1) {
4003 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4004 ecn->ph.param_length = htons(sizeof(*ecn));
4005 SCTP_BUF_LEN(m) += sizeof(*ecn);
4006 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4009 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4011 /* And now tell the peer we do pr-sctp */
4012 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4013 prsctp->ph.param_length = htons(sizeof(*prsctp));
4014 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4016 /* And now tell the peer we do all the extensions */
4017 pr_supported = (struct sctp_supported_chunk_types_param *)
4018 ((caddr_t)prsctp + sizeof(*prsctp));
4019 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4021 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4022 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4023 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4024 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4025 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4026 if (!sctp_auth_disable)
4027 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4028 p_len = sizeof(*pr_supported) + num_ext;
4029 pr_supported->ph.param_length = htons(p_len);
4030 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4031 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4033 /* ECN nonce: And now tell the peer we support ECN nonce */
4034 if (sctp_ecn_nonce) {
4035 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4036 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4037 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4038 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4039 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4041 /* add authentication parameters */
4042 if (!sctp_auth_disable) {
4043 struct sctp_auth_random *randp;
4044 struct sctp_auth_hmac_algo *hmacs;
4045 struct sctp_auth_chunk_list *chunks;
4047 /* attach RANDOM parameter, if available */
4048 if (stcb->asoc.authinfo.random != NULL) {
4049 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4050 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4051 #ifdef SCTP_AUTH_DRAFT_04
4052 randp->ph.param_type = htons(SCTP_RANDOM);
4053 randp->ph.param_length = htons(p_len);
4054 bcopy(stcb->asoc.authinfo.random->key,
4056 stcb->asoc.authinfo.random_len);
4058 /* random key already contains the header */
4059 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4061 /* zero out any padding required */
4062 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4063 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4065 /* add HMAC_ALGO parameter */
4066 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4067 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4068 (uint8_t *) hmacs->hmac_ids);
4070 p_len += sizeof(*hmacs);
4071 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4072 hmacs->ph.param_length = htons(p_len);
4073 /* zero out any padding required */
4074 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4075 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4077 /* add CHUNKS parameter */
4078 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4079 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4080 chunks->chunk_types);
4082 p_len += sizeof(*chunks);
4083 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4084 chunks->ph.param_length = htons(p_len);
4085 /* zero out any padding required */
4086 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4087 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4091 /* now the addresses */
4093 struct sctp_scoping scp;
4096 * To optimize this we could put the scoping stuff into a
4097 * structure and remove the individual uint8's from the
4098 * assoc structure. Then we could just sifa in the address
4099 * within the stcb.. but for now this is a quick hack to get
4100 * the address stuff teased apart.
4102 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4103 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4104 scp.loopback_scope = stcb->asoc.loopback_scope;
4105 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4106 scp.local_scope = stcb->asoc.local_scope;
4107 scp.site_scope = stcb->asoc.site_scope;
4109 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4112 /* calulate the size and update pkt header and chunk header */
4114 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4115 if (SCTP_BUF_NEXT(m_at) == NULL)
4117 p_len += SCTP_BUF_LEN(m_at);
4119 initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4121 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4122 * here since the timer will drive a retranmission.
4125 /* I don't expect this to execute but we will be safe here */
4127 if ((padval) && (mp_last)) {
4129 * The compiler worries that mp_last may not be set even
4130 * though I think it is impossible :-> however we add
4131 * mp_last here just in case.
4133 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4135 /* Houston we have a problem, no space */
4141 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4142 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4143 (struct sockaddr *)&net->ro._l_addr,
4144 m, 0, NULL, 0, 0, NULL, 0, so_locked);
4145 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4146 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4147 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4148 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4152 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4153 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4156 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4157 * being equal to the beginning of the params i.e. (iphlen +
4158 * sizeof(struct sctp_init_msg) parse through the parameters to the
4159 * end of the mbuf verifying that all parameters are known.
4161 * For unknown parameters build and return a mbuf with
4162 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4163 * processing this chunk stop, and set *abort_processing to 1.
4165 * By having param_offset be pre-set to where parameters begin it is
4166 * hoped that this routine may be reused in the future by new
4169 struct sctp_paramhdr *phdr, params;
4171 struct mbuf *mat, *op_err;
4172 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4173 int at, limit, pad_needed;
4174 uint16_t ptype, plen, padded_size;
4177 *abort_processing = 0;
4180 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4183 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4184 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4185 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4186 ptype = ntohs(phdr->param_type);
4187 plen = ntohs(phdr->param_length);
4188 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4189 /* wacked parameter */
4190 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4193 limit -= SCTP_SIZE32(plen);
4195 * All parameters for all chunks that we know/understand are
4196 * listed here. We process them other places and make
4197 * appropriate stop actions per the upper bits. However this
4198 * is the generic routine processor's can call to get back
4199 * an operr.. to either incorporate (init-ack) or send.
4201 padded_size = SCTP_SIZE32(plen);
4203 /* Param's with variable size */
4204 case SCTP_HEARTBEAT_INFO:
4205 case SCTP_STATE_COOKIE:
4206 case SCTP_UNRECOG_PARAM:
4207 case SCTP_ERROR_CAUSE_IND:
4211 /* Param's with variable size within a range */
4212 case SCTP_CHUNK_LIST:
4213 case SCTP_SUPPORTED_CHUNK_EXT:
4214 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4215 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4220 case SCTP_SUPPORTED_ADDRTYPE:
4221 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4222 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4228 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4229 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4234 case SCTP_SET_PRIM_ADDR:
4235 case SCTP_DEL_IP_ADDRESS:
4236 case SCTP_ADD_IP_ADDRESS:
4237 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4238 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4239 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4244 /* Param's with a fixed size */
4245 case SCTP_IPV4_ADDRESS:
4246 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4247 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4252 case SCTP_IPV6_ADDRESS:
4253 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4254 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4259 case SCTP_COOKIE_PRESERVE:
4260 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4261 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4266 case SCTP_ECN_NONCE_SUPPORTED:
4267 case SCTP_PRSCTP_SUPPORTED:
4268 if (padded_size != sizeof(struct sctp_paramhdr)) {
4269 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4274 case SCTP_ECN_CAPABLE:
4275 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4276 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4281 case SCTP_ULP_ADAPTATION:
4282 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4283 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4288 case SCTP_SUCCESS_REPORT:
4289 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4290 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4295 case SCTP_HOSTNAME_ADDRESS:
4297 /* We can NOT handle HOST NAME addresses!! */
4300 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4301 *abort_processing = 1;
4302 if (op_err == NULL) {
4303 /* Ok need to try to get a mbuf */
4304 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4306 l_len += sizeof(struct sctp_paramhdr);
4307 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4309 SCTP_BUF_LEN(op_err) = 0;
4311 * pre-reserve space for ip
4312 * and sctp header and
4315 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4316 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4317 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4321 /* If we have space */
4322 struct sctp_paramhdr s;
4325 uint32_t cpthis = 0;
4327 pad_needed = 4 - (err_at % 4);
4328 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4329 err_at += pad_needed;
4331 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4332 s.param_length = htons(sizeof(s) + plen);
4333 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4334 err_at += sizeof(s);
4335 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4337 sctp_m_freem(op_err);
4339 * we are out of memory but
4340 * we still need to have a
4341 * look at what to do (the
4342 * system is in trouble
4347 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4355 * we do not recognize the parameter figure out what
4358 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4359 if ((ptype & 0x4000) == 0x4000) {
4360 /* Report bit is set?? */
4361 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4362 if (op_err == NULL) {
4365 /* Ok need to try to get an mbuf */
4366 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4368 l_len += sizeof(struct sctp_paramhdr);
4369 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4371 SCTP_BUF_LEN(op_err) = 0;
4372 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4373 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4374 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4378 /* If we have space */
4379 struct sctp_paramhdr s;
4382 uint32_t cpthis = 0;
4384 pad_needed = 4 - (err_at % 4);
4385 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4386 err_at += pad_needed;
4388 s.param_type = htons(SCTP_UNRECOG_PARAM);
4389 s.param_length = htons(sizeof(s) + plen);
4390 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4391 err_at += sizeof(s);
4392 if (plen > sizeof(tempbuf)) {
4393 plen = sizeof(tempbuf);
4395 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4397 sctp_m_freem(op_err);
4399 * we are out of memory but
4400 * we still need to have a
4401 * look at what to do (the
4402 * system is in trouble
4406 goto more_processing;
4408 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4413 if ((ptype & 0x8000) == 0x0000) {
4414 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4417 /* skip this chunk and continue processing */
4418 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4419 at += SCTP_SIZE32(plen);
4424 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4428 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4429 *abort_processing = 1;
4430 if ((op_err == NULL) && phdr) {
4433 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4434 l_len += (2 * sizeof(struct sctp_paramhdr));
4435 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4437 SCTP_BUF_LEN(op_err) = 0;
4438 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4439 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4440 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4443 if ((op_err) && phdr) {
4444 struct sctp_paramhdr s;
4447 uint32_t cpthis = 0;
4449 pad_needed = 4 - (err_at % 4);
4450 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4451 err_at += pad_needed;
4453 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4454 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4455 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4456 err_at += sizeof(s);
4457 /* Only copy back the p-hdr that caused the issue */
4458 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4464 sctp_are_there_new_addresses(struct sctp_association *asoc,
4465 struct mbuf *in_initpkt, int iphlen, int offset)
4468 * Given a INIT packet, look through the packet to verify that there
4469 * are NO new addresses. As we go through the parameters add reports
4470 * of any un-understood parameters that require an error. Also we
4471 * must return (1) to drop the packet if we see a un-understood
4472 * parameter that tells us to drop the chunk.
4474 struct sockaddr_in sin4, *sa4;
4475 struct sockaddr_in6 sin6, *sa6;
4476 struct sockaddr *sa_touse;
4477 struct sockaddr *sa;
4478 struct sctp_paramhdr *phdr, params;
4481 uint16_t ptype, plen;
4484 struct sctp_nets *net;
4486 memset(&sin4, 0, sizeof(sin4));
4487 memset(&sin6, 0, sizeof(sin6));
4488 sin4.sin_family = AF_INET;
4489 sin4.sin_len = sizeof(sin4);
4490 sin6.sin6_family = AF_INET6;
4491 sin6.sin6_len = sizeof(sin6);
4494 /* First what about the src address of the pkt ? */
4495 iph = mtod(in_initpkt, struct ip *);
4496 if (iph->ip_v == IPVERSION) {
4497 /* source addr is IPv4 */
4498 sin4.sin_addr = iph->ip_src;
4499 sa_touse = (struct sockaddr *)&sin4;
4500 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4501 /* source addr is IPv6 */
4502 struct ip6_hdr *ip6h;
4504 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4505 sin6.sin6_addr = ip6h->ip6_src;
4506 sa_touse = (struct sockaddr *)&sin6;
4512 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4513 sa = (struct sockaddr *)&net->ro._l_addr;
4514 if (sa->sa_family == sa_touse->sa_family) {
4515 if (sa->sa_family == AF_INET) {
4516 sa4 = (struct sockaddr_in *)sa;
4517 if (sa4->sin_addr.s_addr ==
4518 sin4.sin_addr.s_addr) {
4522 } else if (sa->sa_family == AF_INET6) {
4523 sa6 = (struct sockaddr_in6 *)sa;
4524 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
4533 /* New address added! no need to look futher. */
4536 /* Ok so far lets munge through the rest of the packet */
4540 offset += sizeof(struct sctp_init_chunk);
4541 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4543 ptype = ntohs(phdr->param_type);
4544 plen = ntohs(phdr->param_length);
4545 if (ptype == SCTP_IPV4_ADDRESS) {
4546 struct sctp_ipv4addr_param *p4, p4_buf;
4548 phdr = sctp_get_next_param(mat, offset,
4549 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4550 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4554 p4 = (struct sctp_ipv4addr_param *)phdr;
4555 sin4.sin_addr.s_addr = p4->addr;
4556 sa_touse = (struct sockaddr *)&sin4;
4557 } else if (ptype == SCTP_IPV6_ADDRESS) {
4558 struct sctp_ipv6addr_param *p6, p6_buf;
4560 phdr = sctp_get_next_param(mat, offset,
4561 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4562 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4566 p6 = (struct sctp_ipv6addr_param *)phdr;
4567 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4569 sa_touse = (struct sockaddr *)&sin4;
4572 /* ok, sa_touse points to one to check */
4574 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4575 sa = (struct sockaddr *)&net->ro._l_addr;
4576 if (sa->sa_family != sa_touse->sa_family) {
4579 if (sa->sa_family == AF_INET) {
4580 sa4 = (struct sockaddr_in *)sa;
4581 if (sa4->sin_addr.s_addr ==
4582 sin4.sin_addr.s_addr) {
4586 } else if (sa->sa_family == AF_INET6) {
4587 sa6 = (struct sockaddr_in6 *)sa;
4588 if (SCTP6_ARE_ADDR_EQUAL(
4589 &sa6->sin6_addr, &sin6.sin6_addr)) {
4596 /* New addr added! no need to look further */
4600 offset += SCTP_SIZE32(plen);
4601 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4607 * Given a MBUF chain that was sent into us containing an INIT. Build a
4608 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4609 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4610 * message (i.e. the struct sctp_init_msg).
4613 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4614 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4615 struct sctp_init_chunk *init_chk, uint32_t vrf_id, int hold_inp_lock)
4617 struct sctp_association *asoc;
4618 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4619 struct sctp_init_msg *initackm_out;
4620 struct sctp_ecn_supported_param *ecn;
4621 struct sctp_prsctp_supported_param *prsctp;
4622 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4623 struct sctp_supported_chunk_types_param *pr_supported;
4624 struct sockaddr_storage store;
4625 struct sockaddr_in *sin;
4626 struct sockaddr_in6 *sin6;
4629 struct ip6_hdr *ip6;
4630 struct sockaddr *to;
4631 struct sctp_state_cookie stc;
4632 struct sctp_nets *net = NULL;
4633 uint8_t *signature = NULL;
4634 int cnt_inits_to = 0;
4635 uint16_t his_limit, i_want;
4636 int abort_flag, padval;
4646 if ((asoc != NULL) &&
4647 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4648 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4649 /* new addresses, out of here in non-cookie-wait states */
4651 * Send a ABORT, we don't add the new address error clause
4652 * though we even set the T bit and copy in the 0 tag.. this
4653 * looks no different than if no listener was present.
4655 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id);
4659 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4660 (offset + sizeof(struct sctp_init_chunk)),
4661 &abort_flag, (struct sctp_chunkhdr *)init_chk);
4663 sctp_send_abort(init_pkt, iphlen, sh,
4664 init_chk->init.initiate_tag, op_err, vrf_id);
4667 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4669 /* No memory, INIT timer will re-attempt. */
4671 sctp_m_freem(op_err);
4674 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4676 /* the time I built cookie */
4677 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4679 /* populate any tie tags */
4681 /* unlock before tag selections */
4682 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4683 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4684 stc.cookie_life = asoc->cookie_life;
4685 net = asoc->primary_destination;
4687 stc.tie_tag_my_vtag = 0;
4688 stc.tie_tag_peer_vtag = 0;
4689 /* life I will award this cookie */
4690 stc.cookie_life = inp->sctp_ep.def_cookie_life;
4693 /* copy in the ports for later check */
4694 stc.myport = sh->dest_port;
4695 stc.peerport = sh->src_port;
4698 * If we wanted to honor cookie life extentions, we would add to
4699 * stc.cookie_life. For now we should NOT honor any extension
4701 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4702 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4703 struct inpcb *in_inp;
4705 /* Its a V6 socket */
4706 in_inp = (struct inpcb *)inp;
4707 stc.ipv6_addr_legal = 1;
4708 /* Now look at the binding flag to see if V4 will be legal */
4709 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4710 stc.ipv4_addr_legal = 1;
4712 /* V4 addresses are NOT legal on the association */
4713 stc.ipv4_addr_legal = 0;
4716 /* Its a V4 socket, no - V6 */
4717 stc.ipv4_addr_legal = 1;
4718 stc.ipv6_addr_legal = 0;
4721 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4726 /* now for scope setup */
4727 memset((caddr_t)&store, 0, sizeof(store));
4728 sin = (struct sockaddr_in *)&store;
4729 sin6 = (struct sockaddr_in6 *)&store;
4731 to = (struct sockaddr *)&store;
4732 iph = mtod(init_pkt, struct ip *);
4733 if (iph->ip_v == IPVERSION) {
4734 struct sctp_ifa *addr;
4735 sctp_route_t iproute;
4737 sin->sin_family = AF_INET;
4738 sin->sin_len = sizeof(struct sockaddr_in);
4739 sin->sin_port = sh->src_port;
4740 sin->sin_addr = iph->ip_src;
4741 /* lookup address */
4742 stc.address[0] = sin->sin_addr.s_addr;
4746 stc.addr_type = SCTP_IPV4_ADDRESS;
4747 /* local from address */
4748 memset(&iproute, 0, sizeof(iproute));
4750 memcpy(&ro->ro_dst, sin, sizeof(*sin));
4751 addr = sctp_source_address_selection(inp, NULL,
4761 stc.laddress[0] = addr->address.sin.sin_addr.s_addr;
4762 stc.laddress[1] = 0;
4763 stc.laddress[2] = 0;
4764 stc.laddress[3] = 0;
4765 stc.laddr_type = SCTP_IPV4_ADDRESS;
4766 /* scope_id is only for v6 */
4768 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
4769 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4774 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
4775 /* Must use the address in this case */
4776 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
4777 stc.loopback_scope = 1;
4780 stc.local_scope = 0;
4782 sctp_free_ifa(addr);
4783 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4784 struct sctp_ifa *addr;
4785 struct route_in6 iproute6;
4787 ip6 = mtod(init_pkt, struct ip6_hdr *);
4788 sin6->sin6_family = AF_INET6;
4789 sin6->sin6_len = sizeof(struct sockaddr_in6);
4790 sin6->sin6_port = sh->src_port;
4791 sin6->sin6_addr = ip6->ip6_src;
4792 /* lookup address */
4793 memcpy(&stc.address, &sin6->sin6_addr,
4794 sizeof(struct in6_addr));
4795 sin6->sin6_scope_id = 0;
4796 stc.addr_type = SCTP_IPV6_ADDRESS;
4798 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
4799 stc.loopback_scope = 1;
4800 stc.local_scope = 0;
4803 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4805 * If the new destination is a LINK_LOCAL we
4806 * must have common both site and local
4807 * scope. Don't set local scope though since
4808 * we must depend on the source to be added
4809 * implicitly. We cannot assure just because
4810 * we share one link that all links are
4813 stc.local_scope = 0;
4817 * we start counting for the private address
4818 * stuff at 1. since the link local we
4819 * source from won't show up in our scoped
4823 /* pull out the scope_id from incoming pkt */
4824 /* FIX ME: does this have scope from rcvif? */
4825 (void)sa6_recoverscope(sin6);
4827 sa6_embedscope(sin6, ip6_use_defzone);
4828 stc.scope_id = sin6->sin6_scope_id;
4829 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4831 * If the new destination is SITE_LOCAL then
4832 * we must have site scope in common.
4836 /* local from address */
4837 memset(&iproute6, 0, sizeof(iproute6));
4838 ro = (sctp_route_t *) & iproute6;
4839 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
4840 addr = sctp_source_address_selection(inp, NULL,
4841 ro, NULL, 0, vrf_id);
4849 memcpy(&stc.laddress, &addr->address.sin6.sin6_addr, sizeof(struct in6_addr));
4850 stc.laddr_type = SCTP_IPV6_ADDRESS;
4851 sctp_free_ifa(addr);
4854 /* set the scope per the existing tcb */
4855 struct sctp_nets *lnet;
4857 stc.loopback_scope = asoc->loopback_scope;
4858 stc.ipv4_scope = asoc->ipv4_local_scope;
4859 stc.site_scope = asoc->site_scope;
4860 stc.local_scope = asoc->local_scope;
4861 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
4862 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
4863 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
4865 * if we have a LL address, start
4873 /* use the net pointer */
4874 to = (struct sockaddr *)&net->ro._l_addr;
4875 if (to->sa_family == AF_INET) {
4876 sin = (struct sockaddr_in *)to;
4877 stc.address[0] = sin->sin_addr.s_addr;
4881 stc.addr_type = SCTP_IPV4_ADDRESS;
4882 if (net->src_addr_selected == 0) {
4884 * strange case here, the INIT should have
4885 * did the selection.
4887 net->ro._s_addr = sctp_source_address_selection(inp,
4888 stcb, (sctp_route_t *) & net->ro,
4890 if (net->ro._s_addr == NULL)
4893 net->src_addr_selected = 1;
4896 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
4897 stc.laddress[1] = 0;
4898 stc.laddress[2] = 0;
4899 stc.laddress[3] = 0;
4900 stc.laddr_type = SCTP_IPV4_ADDRESS;
4901 } else if (to->sa_family == AF_INET6) {
4902 sin6 = (struct sockaddr_in6 *)to;
4903 memcpy(&stc.address, &sin6->sin6_addr,
4904 sizeof(struct in6_addr));
4905 stc.addr_type = SCTP_IPV6_ADDRESS;
4906 if (net->src_addr_selected == 0) {
4908 * strange case here, the INIT should have
4909 * did the selection.
4911 net->ro._s_addr = sctp_source_address_selection(inp,
4912 stcb, (sctp_route_t *) & net->ro,
4914 if (net->ro._s_addr == NULL)
4917 net->src_addr_selected = 1;
4919 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
4920 sizeof(struct in6_addr));
4921 stc.laddr_type = SCTP_IPV6_ADDRESS;
4924 /* Now lets put the SCTP header in place */
4925 initackm_out = mtod(m, struct sctp_init_msg *);
4926 initackm_out->sh.src_port = inp->sctp_lport;
4927 initackm_out->sh.dest_port = sh->src_port;
4928 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
4929 /* Save it off for quick ref */
4930 stc.peers_vtag = init_chk->init.initiate_tag;
4931 initackm_out->sh.checksum = 0; /* calculate later */
4933 memcpy(stc.identification, SCTP_VERSION_STRING,
4934 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
4935 /* now the chunk header */
4936 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
4937 initackm_out->msg.ch.chunk_flags = 0;
4938 /* fill in later from mbuf we build */
4939 initackm_out->msg.ch.chunk_length = 0;
4940 /* place in my tag */
4941 if ((asoc != NULL) &&
4942 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
4943 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
4944 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
4945 /* re-use the v-tags and init-seq here */
4946 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
4947 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
4949 uint32_t vtag, itsn;
4951 if (hold_inp_lock) {
4952 SCTP_INP_INCR_REF(inp);
4953 SCTP_INP_RUNLOCK(inp);
4956 atomic_add_int(&asoc->refcnt, 1);
4957 SCTP_TCB_UNLOCK(stcb);
4958 vtag = sctp_select_a_tag(inp, 1);
4959 initackm_out->msg.init.initiate_tag = htonl(vtag);
4960 /* get a TSN to use too */
4961 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
4962 initackm_out->msg.init.initial_tsn = htonl(itsn);
4963 SCTP_TCB_LOCK(stcb);
4964 atomic_add_int(&asoc->refcnt, -1);
4966 vtag = sctp_select_a_tag(inp, 1);
4967 initackm_out->msg.init.initiate_tag = htonl(vtag);
4968 /* get a TSN to use too */
4969 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
4971 if (hold_inp_lock) {
4972 SCTP_INP_RLOCK(inp);
4973 SCTP_INP_DECR_REF(inp);
4976 /* save away my tag to */
4977 stc.my_vtag = initackm_out->msg.init.initiate_tag;
4979 /* set up some of the credits. */
4980 so = inp->sctp_socket;
4982 /* memory problem */
4986 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
4988 /* set what I want */
4989 his_limit = ntohs(init_chk->init.num_inbound_streams);
4990 /* choose what I want */
4992 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
4993 i_want = asoc->streamoutcnt;
4995 i_want = inp->sctp_ep.pre_open_stream_count;
4998 i_want = inp->sctp_ep.pre_open_stream_count;
5000 if (his_limit < i_want) {
5001 /* I Want more :< */
5002 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
5004 /* I can have what I want :> */
5005 initackm_out->msg.init.num_outbound_streams = htons(i_want);
5007 /* tell him his limt. */
5008 initackm_out->msg.init.num_inbound_streams =
5009 htons(inp->sctp_ep.max_open_streams_intome);
5010 /* setup the ECN pointer */
5012 if (inp->sctp_ep.adaptation_layer_indicator) {
5013 struct sctp_adaptation_layer_indication *ali;
5015 ali = (struct sctp_adaptation_layer_indication *)(
5016 (caddr_t)initackm_out + sizeof(*initackm_out));
5017 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5018 ali->ph.param_length = htons(sizeof(*ali));
5019 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5020 SCTP_BUF_LEN(m) += sizeof(*ali);
5021 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
5024 ecn = (struct sctp_ecn_supported_param *)(
5025 (caddr_t)initackm_out + sizeof(*initackm_out));
5029 if (sctp_ecn_enable == 1) {
5030 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5031 ecn->ph.param_length = htons(sizeof(*ecn));
5032 SCTP_BUF_LEN(m) += sizeof(*ecn);
5034 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5037 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5039 /* And now tell the peer we do pr-sctp */
5040 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5041 prsctp->ph.param_length = htons(sizeof(*prsctp));
5042 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5044 /* And now tell the peer we do all the extensions */
5045 pr_supported = (struct sctp_supported_chunk_types_param *)
5046 ((caddr_t)prsctp + sizeof(*prsctp));
5048 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5050 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5051 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5052 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5053 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5054 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5055 if (!sctp_auth_disable)
5056 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5057 p_len = sizeof(*pr_supported) + num_ext;
5058 pr_supported->ph.param_length = htons(p_len);
5059 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5060 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5062 /* ECN nonce: And now tell the peer we support ECN nonce */
5063 if (sctp_ecn_nonce) {
5064 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5065 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5066 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5067 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5068 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5070 /* add authentication parameters */
5071 if (!sctp_auth_disable) {
5072 struct sctp_auth_random *randp;
5073 struct sctp_auth_hmac_algo *hmacs;
5074 struct sctp_auth_chunk_list *chunks;
5075 uint16_t random_len;
5077 /* generate and add RANDOM parameter */
5078 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5079 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5080 randp->ph.param_type = htons(SCTP_RANDOM);
5081 p_len = sizeof(*randp) + random_len;
5082 randp->ph.param_length = htons(p_len);
5083 SCTP_READ_RANDOM(randp->random_data, random_len);
5084 /* zero out any padding required */
5085 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5086 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5088 /* add HMAC_ALGO parameter */
5089 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5090 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5091 (uint8_t *) hmacs->hmac_ids);
5093 p_len += sizeof(*hmacs);
5094 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5095 hmacs->ph.param_length = htons(p_len);
5096 /* zero out any padding required */
5097 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5098 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5100 /* add CHUNKS parameter */
5101 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5102 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5103 chunks->chunk_types);
5105 p_len += sizeof(*chunks);
5106 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5107 chunks->ph.param_length = htons(p_len);
5108 /* zero out any padding required */
5109 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5110 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5114 /* now the addresses */
5116 struct sctp_scoping scp;
5119 * To optimize this we could put the scoping stuff into a
5120 * structure and remove the individual uint8's from the stc
5121 * structure. Then we could just sifa in the address within
5122 * the stc.. but for now this is a quick hack to get the
5123 * address stuff teased apart.
5125 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5126 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5127 scp.loopback_scope = stc.loopback_scope;
5128 scp.ipv4_local_scope = stc.ipv4_scope;
5129 scp.local_scope = stc.local_scope;
5130 scp.site_scope = stc.site_scope;
5131 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5134 /* tack on the operational error if present */
5142 llen += SCTP_BUF_LEN(ol);
5143 ol = SCTP_BUF_NEXT(ol);
5146 /* must add a pad to the param */
5147 uint32_t cpthis = 0;
5150 padlen = 4 - (llen % 4);
5151 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5153 while (SCTP_BUF_NEXT(m_at) != NULL) {
5154 m_at = SCTP_BUF_NEXT(m_at);
5156 SCTP_BUF_NEXT(m_at) = op_err;
5157 while (SCTP_BUF_NEXT(m_at) != NULL) {
5158 m_at = SCTP_BUF_NEXT(m_at);
5161 /* pre-calulate the size and update pkt header and chunk header */
5163 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5164 p_len += SCTP_BUF_LEN(m_tmp);
5165 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5166 /* m_tmp should now point to last one */
5171 /* Now we must build a cookie */
5172 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5173 sizeof(struct sctphdr), &stc, &signature);
5174 if (m_cookie == NULL) {
5175 /* memory problem */
5179 /* Now append the cookie to the end and update the space/size */
5180 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5182 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5183 p_len += SCTP_BUF_LEN(m_tmp);
5184 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5185 /* m_tmp should now point to last one */
5191 * Place in the size, but we don't include the last pad (if any) in
5194 initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5197 * Time to sign the cookie, we don't sign over the cookie signature
5198 * though thus we set trailer.
5200 (void)sctp_hmac_m(SCTP_HMAC,
5201 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5202 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5203 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5205 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5206 * here since the timer will drive a retranmission.
5209 if ((padval) && (mp_last)) {
5210 /* see my previous comments on mp_last */
5213 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5215 /* Houston we have a problem, no space */
5221 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5222 NULL, 0, SCTP_SO_NOT_LOCKED);
5223 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5228 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5229 struct sctp_association *asoc,
5230 struct sctp_stream_out *strq, int holds_lock)
5232 struct sctp_stream_out *stre, *strn;
5234 if (holds_lock == 0) {
5235 SCTP_TCB_SEND_LOCK(stcb);
5237 if ((strq->next_spoke.tqe_next) ||
5238 (strq->next_spoke.tqe_prev)) {
5239 /* already on wheel */
5242 stre = TAILQ_FIRST(&asoc->out_wheel);
5244 /* only one on wheel */
5245 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5248 for (; stre; stre = strn) {
5249 strn = TAILQ_NEXT(stre, next_spoke);
5250 if (stre->stream_no > strq->stream_no) {
5251 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5253 } else if (stre->stream_no == strq->stream_no) {
5254 /* huh, should not happen */
5256 } else if (strn == NULL) {
5257 /* next one is null */
5258 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5263 if (holds_lock == 0) {
5264 SCTP_TCB_SEND_UNLOCK(stcb);
5269 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5270 struct sctp_association *asoc,
5271 struct sctp_stream_out *strq)
5273 /* take off and then setup so we know it is not on the wheel */
5274 SCTP_TCB_SEND_LOCK(stcb);
5275 if (TAILQ_FIRST(&strq->outqueue)) {
5276 /* more was added */
5277 SCTP_TCB_SEND_UNLOCK(stcb);
5280 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5281 strq->next_spoke.tqe_next = NULL;
5282 strq->next_spoke.tqe_prev = NULL;
5283 SCTP_TCB_SEND_UNLOCK(stcb);
5287 sctp_prune_prsctp(struct sctp_tcb *stcb,
5288 struct sctp_association *asoc,
5289 struct sctp_sndrcvinfo *srcv,
5293 struct sctp_tmit_chunk *chk, *nchk;
5295 SCTP_TCB_LOCK_ASSERT(stcb);
5296 if ((asoc->peer_supports_prsctp) &&
5297 (asoc->sent_queue_cnt_removeable > 0)) {
5298 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5300 * Look for chunks marked with the PR_SCTP flag AND
5301 * the buffer space flag. If the one being sent is
5302 * equal or greater priority then purge the old one
5303 * and free some space.
5305 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5307 * This one is PR-SCTP AND buffer space
5310 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5312 * Lower numbers equates to higher
5313 * priority so if the one we are
5314 * looking at has a larger or equal
5315 * priority we want to drop the data
5316 * and NOT retransmit it.
5320 * We release the book_size
5321 * if the mbuf is here
5326 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5327 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5329 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5330 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5332 &asoc->sent_queue, SCTP_SO_LOCKED);
5333 freed_spc += ret_spc;
5334 if (freed_spc >= dataout) {
5337 } /* if chunk was present */
5338 } /* if of sufficent priority */
5339 } /* if chunk has enabled */
5340 } /* tailqforeach */
5342 chk = TAILQ_FIRST(&asoc->send_queue);
5344 nchk = TAILQ_NEXT(chk, sctp_next);
5345 /* Here we must move to the sent queue and mark */
5346 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5347 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5350 * We release the book_size
5351 * if the mbuf is here
5355 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5356 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5357 &asoc->send_queue, SCTP_SO_LOCKED);
5359 freed_spc += ret_spc;
5360 if (freed_spc >= dataout) {
5363 } /* end if chk->data */
5364 } /* end if right class */
5365 } /* end if chk pr-sctp */
5367 } /* end while (chk) */
5368 } /* if enabled in asoc */
5372 sctp_get_frag_point(struct sctp_tcb *stcb,
5373 struct sctp_association *asoc)
5378 * For endpoints that have both v6 and v4 addresses we must reserve
5379 * room for the ipv6 header, for those that are only dealing with V4
5380 * we use a larger frag point.
5382 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5383 ovh = SCTP_MED_OVERHEAD;
5385 ovh = SCTP_MED_V4_OVERHEAD;
5388 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5389 siz = asoc->smallest_mtu - ovh;
5391 siz = (stcb->asoc.sctp_frag_point - ovh);
5393 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5395 /* A data chunk MUST fit in a cluster */
5396 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5399 /* adjust for an AUTH chunk if DATA requires auth */
5400 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5401 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5404 /* make it an even word boundary please */
5411 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5412 struct sctp_stream_queue_pending *sp)
5415 if (stcb->asoc.peer_supports_prsctp) {
5417 * We assume that the user wants PR_SCTP_TTL if the user
5418 * provides a positive lifetime but does not specify any
5419 * PR_SCTP policy. This is a BAD assumption and causes
5420 * problems at least with the U-Vancovers MPI folks. I will
5421 * change this to be no policy means NO PR-SCTP.
5423 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5424 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5429 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5430 case CHUNK_FLAGS_PR_SCTP_BUF:
5432 * Time to live is a priority stored in tv_sec when
5433 * doing the buffer drop thing.
5435 sp->ts.tv_sec = sp->timetolive;
5438 case CHUNK_FLAGS_PR_SCTP_TTL:
5442 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5443 tv.tv_sec = sp->timetolive / 1000;
5444 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5445 timevaladd(&sp->ts, &tv);
5448 case CHUNK_FLAGS_PR_SCTP_RTX:
5450 * Time to live is a the number or retransmissions
5453 sp->ts.tv_sec = sp->timetolive;
5457 SCTPDBG(SCTP_DEBUG_USRREQ1,
5458 "Unknown PR_SCTP policy %u.\n",
5459 PR_SCTP_POLICY(sp->sinfo_flags));
5466 sctp_msg_append(struct sctp_tcb *stcb,
5467 struct sctp_nets *net,
5469 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5471 int error = 0, holds_lock;
5473 struct sctp_stream_queue_pending *sp = NULL;
5474 struct sctp_stream_out *strm;
5477 * Given an mbuf chain, put it into the association send queue and
5478 * place it on the wheel
5480 holds_lock = hold_stcb_lock;
5481 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5482 /* Invalid stream number */
5483 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5487 if ((stcb->asoc.stream_locked) &&
5488 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5489 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5493 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5494 /* Now can we send this? */
5495 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5496 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5497 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5498 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5499 /* got data while shutting down */
5500 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5504 sctp_alloc_a_strmoq(stcb, sp);
5506 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5510 sp->sinfo_flags = srcv->sinfo_flags;
5511 sp->timetolive = srcv->sinfo_timetolive;
5512 sp->ppid = srcv->sinfo_ppid;
5513 sp->context = srcv->sinfo_context;
5515 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5519 sp->net = stcb->asoc.primary_destination;
5522 atomic_add_int(&sp->net->ref_count, 1);
5523 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5524 sp->stream = srcv->sinfo_stream;
5525 sp->msg_is_complete = 1;
5526 sp->sender_all_done = 1;
5529 sp->tail_mbuf = NULL;
5532 sctp_set_prsctp_policy(stcb, sp);
5534 * We could in theory (for sendall) sifa the length in, but we would
5535 * still have to hunt through the chain since we need to setup the
5539 if (SCTP_BUF_NEXT(at) == NULL)
5541 sp->length += SCTP_BUF_LEN(at);
5542 at = SCTP_BUF_NEXT(at);
5544 SCTP_TCB_SEND_LOCK(stcb);
5545 sctp_snd_sb_alloc(stcb, sp->length);
5546 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5547 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5548 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5549 sp->strseq = strm->next_sequence_sent;
5550 strm->next_sequence_sent++;
5552 if ((strm->next_spoke.tqe_next == NULL) &&
5553 (strm->next_spoke.tqe_prev == NULL)) {
5554 /* Not on wheel, insert */
5555 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5558 SCTP_TCB_SEND_UNLOCK(stcb);
5567 static struct mbuf *
5568 sctp_copy_mbufchain(struct mbuf *clonechain,
5569 struct mbuf *outchain,
5570 struct mbuf **endofchain,
5573 uint8_t copy_by_ref)
5576 struct mbuf *appendchain;
5580 if (endofchain == NULL) {
5584 sctp_m_freem(outchain);
5587 if (can_take_mbuf) {
5588 appendchain = clonechain;
5591 (sizeofcpy <= (int)((((sctp_mbuf_threshold_count - 1) * MLEN) + MHLEN)))
5593 /* Its not in a cluster */
5594 if (*endofchain == NULL) {
5595 /* lets get a mbuf cluster */
5596 if (outchain == NULL) {
5597 /* This is the general case */
5599 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5600 if (outchain == NULL) {
5603 SCTP_BUF_LEN(outchain) = 0;
5604 *endofchain = outchain;
5605 /* get the prepend space */
5606 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5609 * We really should not get a NULL
5615 if (SCTP_BUF_NEXT(m) == NULL) {
5619 m = SCTP_BUF_NEXT(m);
5622 if (*endofchain == NULL) {
5624 * huh, TSNH XXX maybe we
5627 sctp_m_freem(outchain);
5631 /* get the new end of length */
5632 len = M_TRAILINGSPACE(*endofchain);
5634 /* how much is left at the end? */
5635 len = M_TRAILINGSPACE(*endofchain);
5637 /* Find the end of the data, for appending */
5638 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5640 /* Now lets copy it out */
5641 if (len >= sizeofcpy) {
5642 /* It all fits, copy it in */
5643 m_copydata(clonechain, 0, sizeofcpy, cp);
5644 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5646 /* fill up the end of the chain */
5648 m_copydata(clonechain, 0, len, cp);
5649 SCTP_BUF_LEN((*endofchain)) += len;
5650 /* now we need another one */
5653 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5658 SCTP_BUF_NEXT((*endofchain)) = m;
5660 cp = mtod((*endofchain), caddr_t);
5661 m_copydata(clonechain, len, sizeofcpy, cp);
5662 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5666 /* copy the old fashion way */
5667 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5670 if (appendchain == NULL) {
5673 sctp_m_freem(outchain);
5677 /* tack on to the end */
5678 if (*endofchain != NULL) {
5679 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
5683 if (SCTP_BUF_NEXT(m) == NULL) {
5684 SCTP_BUF_NEXT(m) = appendchain;
5687 m = SCTP_BUF_NEXT(m);
5691 * save off the end and update the end-chain postion
5695 if (SCTP_BUF_NEXT(m) == NULL) {
5699 m = SCTP_BUF_NEXT(m);
5703 /* save off the end and update the end-chain postion */
5706 if (SCTP_BUF_NEXT(m) == NULL) {
5710 m = SCTP_BUF_NEXT(m);
5712 return (appendchain);
5717 sctp_med_chunk_output(struct sctp_inpcb *inp,
5718 struct sctp_tcb *stcb,
5719 struct sctp_association *asoc,
5722 int control_only, int *cwnd_full, int from_where,
5723 struct timeval *now, int *now_filled, int frag_point, int so_locked
5724 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5730 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
5733 struct sctp_copy_all *ca;
5736 int added_control = 0;
5737 int un_sent, do_chunk_output = 1;
5738 struct sctp_association *asoc;
5740 ca = (struct sctp_copy_all *)ptr;
5741 if (ca->m == NULL) {
5744 if (ca->inp != inp) {
5748 if ((ca->m) && ca->sndlen) {
5749 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
5751 /* can't copy so we are done */
5758 SCTP_TCB_LOCK_ASSERT(stcb);
5759 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
5760 /* Abort this assoc with m as the user defined reason */
5762 struct sctp_paramhdr *ph;
5764 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
5766 ph = mtod(m, struct sctp_paramhdr *);
5767 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5768 ph->param_length = htons(ca->sndlen);
5771 * We add one here to keep the assoc from
5772 * dis-appearing on us.
5774 atomic_add_int(&stcb->asoc.refcnt, 1);
5775 sctp_abort_an_association(inp, stcb,
5776 SCTP_RESPONSE_TO_USER_REQ,
5777 m, SCTP_SO_NOT_LOCKED);
5779 * sctp_abort_an_association calls sctp_free_asoc()
5780 * free association will NOT free it since we
5781 * incremented the refcnt .. we do this to prevent
5782 * it being freed and things getting tricky since we
5783 * could end up (from free_asoc) calling inpcb_free
5784 * which would get a recursive lock call to the
5785 * iterator lock.. But as a consequence of that the
5786 * stcb will return to us un-locked.. since
5787 * free_asoc returns with either no TCB or the TCB
5788 * unlocked, we must relock.. to unlock in the
5789 * iterator timer :-0
5791 SCTP_TCB_LOCK(stcb);
5792 atomic_add_int(&stcb->asoc.refcnt, -1);
5793 goto no_chunk_output;
5797 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
5801 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
5802 /* shutdown this assoc */
5805 cnt = sctp_is_there_unsent_data(stcb);
5807 if (TAILQ_EMPTY(&asoc->send_queue) &&
5808 TAILQ_EMPTY(&asoc->sent_queue) &&
5810 if (asoc->locked_on_sending) {
5814 * there is nothing queued to send, so I'm
5817 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
5818 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
5819 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5821 * only send SHUTDOWN the first time
5824 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
5825 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
5826 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5828 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5829 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5830 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
5831 asoc->primary_destination);
5832 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
5833 asoc->primary_destination);
5835 do_chunk_output = 0;
5839 * we still got (or just got) data to send,
5840 * so set SHUTDOWN_PENDING
5843 * XXX sockets draft says that SCTP_EOF
5844 * should be sent with no data. currently,
5845 * we will allow user data to be sent first
5846 * and move to SHUTDOWN-PENDING
5848 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
5849 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
5850 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5851 if (asoc->locked_on_sending) {
5853 * Locked to send out the
5856 struct sctp_stream_queue_pending *sp;
5858 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
5860 if ((sp->length == 0) && (sp->msg_is_complete == 0))
5861 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5864 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
5865 if (TAILQ_EMPTY(&asoc->send_queue) &&
5866 TAILQ_EMPTY(&asoc->sent_queue) &&
5867 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
5869 atomic_add_int(&stcb->asoc.refcnt, 1);
5870 sctp_abort_an_association(stcb->sctp_ep, stcb,
5871 SCTP_RESPONSE_TO_USER_REQ,
5872 NULL, SCTP_SO_NOT_LOCKED);
5873 atomic_add_int(&stcb->asoc.refcnt, -1);
5874 goto no_chunk_output;
5876 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
5877 asoc->primary_destination);
5883 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
5884 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
5886 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
5887 (stcb->asoc.total_flight > 0) &&
5888 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
5890 do_chunk_output = 0;
5892 if (do_chunk_output)
5893 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
5894 else if (added_control) {
5895 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
5899 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
5900 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
5901 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
5912 sctp_sendall_completes(void *ptr, uint32_t val)
5914 struct sctp_copy_all *ca;
5916 ca = (struct sctp_copy_all *)ptr;
5918 * Do a notify here? Kacheong suggests that the notify be done at
5919 * the send time.. so you would push up a notification if any send
5920 * failed. Don't know if this is feasable since the only failures we
5921 * have is "memory" related and if you cannot get an mbuf to send
5922 * the data you surely can't get an mbuf to send up to notify the
5923 * user you can't send the data :->
5926 /* now free everything */
5927 sctp_m_freem(ca->m);
5928 SCTP_FREE(ca, SCTP_M_COPYAL);
5932 #define MC_ALIGN(m, len) do { \
5933 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
5938 static struct mbuf *
5939 sctp_copy_out_all(struct uio *uio, int len)
5941 struct mbuf *ret, *at;
5942 int left, willcpy, cancpy, error;
5944 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
5950 SCTP_BUF_LEN(ret) = 0;
5951 /* save space for the data chunk header */
5952 cancpy = M_TRAILINGSPACE(ret);
5953 willcpy = min(cancpy, left);
5956 /* Align data to the end */
5957 error = uiomove(mtod(at, caddr_t), willcpy, uio);
5963 SCTP_BUF_LEN(at) = willcpy;
5964 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
5967 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
5968 if (SCTP_BUF_NEXT(at) == NULL) {
5971 at = SCTP_BUF_NEXT(at);
5972 SCTP_BUF_LEN(at) = 0;
5973 cancpy = M_TRAILINGSPACE(at);
5974 willcpy = min(cancpy, left);
5981 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
5982 struct sctp_sndrcvinfo *srcv)
5985 struct sctp_copy_all *ca;
5987 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
5991 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5994 memset(ca, 0, sizeof(struct sctp_copy_all));
5997 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
5999 * take off the sendall flag, it would be bad if we failed to do
6002 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6003 /* get length and mbuf chain */
6005 ca->sndlen = uio->uio_resid;
6006 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6007 if (ca->m == NULL) {
6008 SCTP_FREE(ca, SCTP_M_COPYAL);
6009 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6013 /* Gather the length of the send */
6019 ca->sndlen += SCTP_BUF_LEN(m);
6020 m = SCTP_BUF_NEXT(m);
6024 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6025 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6026 SCTP_ASOC_ANY_STATE,
6028 sctp_sendall_completes, inp, 1);
6030 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6031 SCTP_FREE(ca, SCTP_M_COPYAL);
6032 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6040 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6042 struct sctp_tmit_chunk *chk, *nchk;
6044 chk = TAILQ_FIRST(&asoc->control_send_queue);
6046 nchk = TAILQ_NEXT(chk, sctp_next);
6047 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6048 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6050 sctp_m_freem(chk->data);
6053 asoc->ctrl_queue_cnt--;
6054 sctp_free_a_chunk(stcb, chk);
6061 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6063 struct sctp_association *asoc;
6064 struct sctp_tmit_chunk *chk, *chk_tmp;
6067 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
6070 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6071 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
6072 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6073 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6075 sctp_m_freem(chk->data);
6078 asoc->ctrl_queue_cnt--;
6079 sctp_free_a_chunk(stcb, chk);
6086 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6088 struct sctp_association *asoc,
6089 struct sctp_tmit_chunk **data_list,
6091 struct sctp_nets *net)
6094 struct sctp_tmit_chunk *tp1;
6096 for (i = 0; i < bundle_at; i++) {
6097 /* off of the send queue */
6100 * Any chunk NOT 0 you zap the time chunk 0 gets
6101 * zapped or set based on if a RTO measurment is
6104 data_list[i]->do_rtt = 0;
6107 data_list[i]->sent_rcv_time = net->last_sent_time;
6108 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6109 TAILQ_REMOVE(&asoc->send_queue,
6112 /* on to the sent queue */
6113 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6114 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6115 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6116 struct sctp_tmit_chunk *tpp;
6118 /* need to move back */
6120 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6122 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6126 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6127 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6130 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6132 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6137 /* This does not lower until the cum-ack passes it */
6138 asoc->sent_queue_cnt++;
6139 asoc->send_queue_cnt--;
6140 if ((asoc->peers_rwnd <= 0) &&
6141 (asoc->total_flight == 0) &&
6143 /* Mark the chunk as being a window probe */
6144 SCTP_STAT_INCR(sctps_windowprobed);
6146 #ifdef SCTP_AUDITING_ENABLED
6147 sctp_audit_log(0xC2, 3);
6149 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6150 data_list[i]->snd_count = 1;
6151 data_list[i]->rec.data.chunk_was_revoked = 0;
6152 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
6153 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6154 data_list[i]->whoTo->flight_size,
6155 data_list[i]->book_size,
6156 (uintptr_t) data_list[i]->whoTo,
6157 data_list[i]->rec.data.TSN_seq);
6159 sctp_flight_size_increase(data_list[i]);
6160 sctp_total_flight_increase(stcb, data_list[i]);
6161 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
6162 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6163 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
6165 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6166 (uint32_t) (data_list[i]->send_size + sctp_peer_chunk_oh));
6167 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6168 /* SWS sender side engages */
6169 asoc->peers_rwnd = 0;
6175 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6177 struct sctp_tmit_chunk *chk, *nchk;
6179 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6181 nchk = TAILQ_NEXT(chk, sctp_next);
6182 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6183 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6184 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6185 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6186 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6187 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6188 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6189 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6190 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6191 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6192 /* Stray chunks must be cleaned up */
6194 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6196 sctp_m_freem(chk->data);
6199 asoc->ctrl_queue_cnt--;
6200 sctp_free_a_chunk(stcb, chk);
6201 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6202 /* special handling, we must look into the param */
6203 if (chk != asoc->str_reset) {
6204 goto clean_up_anyway;
6212 sctp_can_we_split_this(struct sctp_tcb *stcb,
6214 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6217 * Make a decision on if I should split a msg into multiple parts.
6218 * This is only asked of incomplete messages.
6222 * If we are doing EEOR we need to always send it if its the
6223 * entire thing, since it might be all the guy is putting in
6226 if (goal_mtu >= length) {
6228 * If we have data outstanding,
6229 * we get another chance when the sack
6230 * arrives to transmit - wait for more data
6232 if (stcb->asoc.total_flight == 0) {
6234 * If nothing is in flight, we zero the
6242 /* You can fill the rest */
6247 * For those strange folk that make the send buffer
6248 * smaller than our fragmentation point, we can't
6249 * get a full msg in so we have to allow splitting.
6251 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6254 if ((length <= goal_mtu) ||
6255 ((length - goal_mtu) < sctp_min_residual)) {
6256 /* Sub-optimial residual don't split in non-eeor mode. */
6260 * If we reach here length is larger than the goal_mtu. Do we wish
6261 * to split it for the sake of packet putting together?
6263 if (goal_mtu >= min(sctp_min_split_point, frag_point)) {
6264 /* Its ok to split it */
6265 return (min(goal_mtu, frag_point));
6267 /* Nope, can't split */
6273 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6274 struct sctp_stream_out *strq,
6276 uint32_t frag_point,
6282 /* Move from the stream to the send_queue keeping track of the total */
6283 struct sctp_association *asoc;
6284 struct sctp_stream_queue_pending *sp;
6285 struct sctp_tmit_chunk *chk;
6286 struct sctp_data_chunk *dchkh;
6287 uint32_t to_move, length;
6288 uint8_t rcv_flags = 0;
6290 uint8_t send_lock_up = 0;
6292 SCTP_TCB_LOCK_ASSERT(stcb);
6295 /* sa_ignore FREED_MEMORY */
6296 sp = TAILQ_FIRST(&strq->outqueue);
6299 if (send_lock_up == 0) {
6300 SCTP_TCB_SEND_LOCK(stcb);
6303 sp = TAILQ_FIRST(&strq->outqueue);
6307 if (strq->last_msg_incomplete) {
6308 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6310 strq->last_msg_incomplete);
6311 strq->last_msg_incomplete = 0;
6315 SCTP_TCB_SEND_UNLOCK(stcb);
6320 if ((sp->msg_is_complete) && (sp->length == 0)) {
6321 if (sp->sender_all_done) {
6323 * We are doing differed cleanup. Last time through
6324 * when we took all the data the sender_all_done was
6327 if (sp->put_last_out == 0) {
6328 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6329 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6330 sp->sender_all_done,
6332 sp->msg_is_complete,
6336 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6337 SCTP_TCB_SEND_LOCK(stcb);
6340 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6341 TAILQ_REMOVE(&strq->outqueue, sp, next);
6342 sctp_free_remote_addr(sp->net);
6344 sctp_m_freem(sp->data);
6347 sctp_free_a_strmoq(stcb, sp);
6349 /* we can't be locked to it */
6351 stcb->asoc.locked_on_sending = NULL;
6353 SCTP_TCB_SEND_UNLOCK(stcb);
6356 /* back to get the next msg */
6360 * sender just finished this but still holds a
6369 /* is there some to get */
6370 if (sp->length == 0) {
6378 some_taken = sp->some_taken;
6379 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6380 sp->msg_is_complete = 1;
6383 length = sp->length;
6384 if (sp->msg_is_complete) {
6385 /* The message is complete */
6386 to_move = min(length, frag_point);
6387 if (to_move == length) {
6388 /* All of it fits in the MTU */
6389 if (sp->some_taken) {
6390 rcv_flags |= SCTP_DATA_LAST_FRAG;
6391 sp->put_last_out = 1;
6393 rcv_flags |= SCTP_DATA_NOT_FRAG;
6394 sp->put_last_out = 1;
6397 /* Not all of it fits, we fragment */
6398 if (sp->some_taken == 0) {
6399 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6404 to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6405 frag_point, eeor_mode);
6408 * We use a snapshot of length in case it
6409 * is expanding during the compare.
6414 if (to_move >= llen) {
6416 if (send_lock_up == 0) {
6418 * We are taking all of an incomplete msg
6419 * thus we need a send lock.
6421 SCTP_TCB_SEND_LOCK(stcb);
6423 if (sp->msg_is_complete) {
6425 * the sender finished the
6432 if (sp->some_taken == 0) {
6433 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6437 /* Nothing to take. */
6438 if (sp->some_taken) {
6447 /* If we reach here, we can copy out a chunk */
6448 sctp_alloc_a_chunk(stcb, chk);
6450 /* No chunk memory */
6456 * Setup for unordered if needed by looking at the user sent info
6459 if (sp->sinfo_flags & SCTP_UNORDERED) {
6460 rcv_flags |= SCTP_DATA_UNORDERED;
6462 /* clear out the chunk before setting up */
6463 memset(chk, 0, sizeof(*chk));
6464 chk->rec.data.rcv_flags = rcv_flags;
6466 if (to_move >= length) {
6467 /* we think we can steal the whole thing */
6468 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6469 SCTP_TCB_SEND_LOCK(stcb);
6472 if (to_move < sp->length) {
6473 /* bail, it changed */
6476 chk->data = sp->data;
6477 chk->last_mbuf = sp->tail_mbuf;
6478 /* register the stealing */
6479 sp->data = sp->tail_mbuf = NULL;
6484 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6485 chk->last_mbuf = NULL;
6486 if (chk->data == NULL) {
6487 sp->some_taken = some_taken;
6488 sctp_free_a_chunk(stcb, chk);
6493 /* Pull off the data */
6494 m_adj(sp->data, to_move);
6495 /* Now lets work our way down and compact it */
6497 while (m && (SCTP_BUF_LEN(m) == 0)) {
6498 sp->data = SCTP_BUF_NEXT(m);
6499 SCTP_BUF_NEXT(m) = NULL;
6500 if (sp->tail_mbuf == m) {
6502 * Freeing tail? TSNH since
6503 * we supposedly were taking less
6504 * than the sp->length.
6507 panic("Huh, freing tail? - TSNH");
6509 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6510 sp->tail_mbuf = sp->data = NULL;
6519 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6520 chk->copy_by_ref = 1;
6522 chk->copy_by_ref = 0;
6525 * get last_mbuf and counts of mb useage This is ugly but hopefully
6526 * its only one mbuf.
6528 if (chk->last_mbuf == NULL) {
6529 chk->last_mbuf = chk->data;
6530 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6531 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6534 if (to_move > length) {
6535 /*- This should not happen either
6536 * since we always lower to_move to the size
6537 * of sp->length if its larger.
6540 panic("Huh, how can to_move be larger?");
6542 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6546 atomic_subtract_int(&sp->length, to_move);
6548 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6549 /* Not enough room for a chunk header, get some */
6552 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6555 * we're in trouble here. _PREPEND below will free
6556 * all the data if there is no leading space, so we
6557 * must put the data back and restore.
6559 if (send_lock_up == 0) {
6560 SCTP_TCB_SEND_LOCK(stcb);
6563 if (chk->data == NULL) {
6564 /* unsteal the data */
6565 sp->data = chk->data;
6566 sp->tail_mbuf = chk->last_mbuf;
6570 /* reassemble the data */
6572 sp->data = chk->data;
6573 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6575 sp->some_taken = some_taken;
6576 atomic_add_int(&sp->length, to_move);
6579 sctp_free_a_chunk(stcb, chk);
6583 SCTP_BUF_LEN(m) = 0;
6584 SCTP_BUF_NEXT(m) = chk->data;
6586 M_ALIGN(chk->data, 4);
6589 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6590 if (chk->data == NULL) {
6591 /* HELP, TSNH since we assured it would not above? */
6593 panic("prepend failes HELP?");
6595 SCTP_PRINTF("prepend fails HELP?\n");
6596 sctp_free_a_chunk(stcb, chk);
6602 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6603 chk->book_size = chk->send_size = (to_move +
6604 sizeof(struct sctp_data_chunk));
6605 chk->book_size_scale = 0;
6606 chk->sent = SCTP_DATAGRAM_UNSENT;
6609 chk->asoc = &stcb->asoc;
6610 chk->pad_inplace = 0;
6611 chk->no_fr_allowed = 0;
6612 chk->rec.data.stream_seq = sp->strseq;
6613 chk->rec.data.stream_number = sp->stream;
6614 chk->rec.data.payloadtype = sp->ppid;
6615 chk->rec.data.context = sp->context;
6616 chk->rec.data.doing_fast_retransmit = 0;
6617 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
6619 chk->rec.data.timetodrop = sp->ts;
6620 chk->flags = sp->act_flags;
6621 chk->addr_over = sp->addr_over;
6624 atomic_add_int(&chk->whoTo->ref_count, 1);
6626 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6627 if (sctp_logging_level & SCTP_LOG_AT_SEND_2_OUTQ) {
6628 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6629 (uintptr_t) stcb, sp->length,
6630 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6631 chk->rec.data.TSN_seq);
6633 dchkh = mtod(chk->data, struct sctp_data_chunk *);
6635 * Put the rest of the things in place now. Size was done earlier in
6636 * previous loop prior to padding.
6639 #ifdef SCTP_ASOCLOG_OF_TSNS
6640 SCTP_TCB_LOCK_ASSERT(stcb);
6641 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
6642 asoc->tsn_out_at = 0;
6643 asoc->tsn_out_wrapped = 1;
6645 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
6646 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
6647 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
6648 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
6649 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
6650 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
6651 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
6652 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
6656 dchkh->ch.chunk_type = SCTP_DATA;
6657 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
6658 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
6659 dchkh->dp.stream_id = htons(strq->stream_no);
6660 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
6661 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
6662 dchkh->ch.chunk_length = htons(chk->send_size);
6663 /* Now advance the chk->send_size by the actual pad needed. */
6664 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
6669 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
6670 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
6671 chk->pad_inplace = 1;
6673 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
6674 /* pad added an mbuf */
6675 chk->last_mbuf = lm;
6677 chk->send_size += pads;
6679 /* We only re-set the policy if it is on */
6680 if (sp->pr_sctp_on) {
6681 sctp_set_prsctp_policy(stcb, sp);
6682 asoc->pr_sctp_cnt++;
6683 chk->pr_sctp_on = 1;
6685 chk->pr_sctp_on = 0;
6687 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
6688 /* All done pull and kill the message */
6689 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6690 if (sp->put_last_out == 0) {
6691 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
6692 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6693 sp->sender_all_done,
6695 sp->msg_is_complete,
6699 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
6700 SCTP_TCB_SEND_LOCK(stcb);
6703 TAILQ_REMOVE(&strq->outqueue, sp, next);
6704 sctp_free_remote_addr(sp->net);
6706 sctp_m_freem(sp->data);
6709 sctp_free_a_strmoq(stcb, sp);
6711 /* we can't be locked to it */
6713 stcb->asoc.locked_on_sending = NULL;
6715 /* more to go, we are locked */
6718 asoc->chunks_on_out_queue++;
6719 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
6720 asoc->send_queue_cnt++;
6723 SCTP_TCB_SEND_UNLOCK(stcb);
6730 static struct sctp_stream_out *
6731 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
6733 struct sctp_stream_out *strq;
6735 /* Find the next stream to use */
6736 if (asoc->last_out_stream == NULL) {
6737 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6738 if (asoc->last_out_stream == NULL) {
6739 /* huh nothing on the wheel, TSNH */
6744 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
6747 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6749 /* Save off the last stream */
6750 asoc->last_out_stream = strq;
6757 sctp_fill_outqueue(struct sctp_tcb *stcb,
6758 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
6760 struct sctp_association *asoc;
6761 struct sctp_stream_out *strq, *strqn, *strqt;
6762 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
6764 struct sctp_stream_queue_pending *sp;
6766 SCTP_TCB_LOCK_ASSERT(stcb);
6769 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
6770 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6772 /* ?? not sure what else to do */
6773 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
6776 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6779 /* Need an allowance for the data chunk header too */
6780 goal_mtu -= sizeof(struct sctp_data_chunk);
6782 /* must make even word boundary */
6783 goal_mtu &= 0xfffffffc;
6784 if (asoc->locked_on_sending) {
6785 /* We are stuck on one stream until the message completes. */
6786 strqn = strq = asoc->locked_on_sending;
6789 strqn = strq = sctp_select_a_stream(stcb, asoc);
6793 while ((goal_mtu > 0) && strq) {
6794 sp = TAILQ_FIRST(&strq->outqueue);
6796 * If CMT is off, we must validate that the stream in
6797 * question has the first item pointed towards are network
6798 * destionation requested by the caller. Note that if we
6799 * turn out to be locked to a stream (assigning TSN's then
6800 * we must stop, since we cannot look for another stream
6801 * with data to send to that destination). In CMT's case, by
6802 * skipping this check, we will send one data packet towards
6803 * the requested net.
6808 if ((sp->net != net) && (sctp_cmt_on_off == 0)) {
6809 /* none for this network */
6813 strq = sctp_select_a_stream(stcb, asoc);
6817 if (strqn == strq) {
6818 /* I have circled */
6826 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
6827 &giveup, eeor_mode, &bail);
6828 asoc->last_out_stream = strq;
6830 asoc->locked_on_sending = strq;
6831 if ((moved_how_much == 0) || (giveup) || bail)
6832 /* no more to move for now */
6835 asoc->locked_on_sending = NULL;
6836 strqt = sctp_select_a_stream(stcb, asoc);
6837 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
6838 if (strq == strqn) {
6839 /* Must move start to next one */
6840 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
6841 if (strqn == NULL) {
6842 strqn = TAILQ_FIRST(&asoc->out_wheel);
6843 if (strqn == NULL) {
6848 sctp_remove_from_wheel(stcb, asoc, strq);
6850 if ((giveup) || bail) {
6858 total_moved += moved_how_much;
6859 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
6860 goal_mtu &= 0xfffffffc;
6865 if (total_moved == 0) {
6866 if ((sctp_cmt_on_off == 0) &&
6867 (net == stcb->asoc.primary_destination)) {
6868 /* ran dry for primary network net */
6869 SCTP_STAT_INCR(sctps_primary_randry);
6870 } else if (sctp_cmt_on_off) {
6871 /* ran dry with CMT on */
6872 SCTP_STAT_INCR(sctps_cmt_randry);
6878 sctp_fix_ecn_echo(struct sctp_association *asoc)
6880 struct sctp_tmit_chunk *chk;
6882 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6883 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
6884 chk->sent = SCTP_DATAGRAM_UNSENT;
6890 sctp_move_to_an_alt(struct sctp_tcb *stcb,
6891 struct sctp_association *asoc,
6892 struct sctp_nets *net)
6894 struct sctp_tmit_chunk *chk;
6895 struct sctp_nets *a_net;
6897 SCTP_TCB_LOCK_ASSERT(stcb);
6899 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
6900 * destination using the PF algorithm for finding alternate
6903 if (sctp_cmt_on_off && sctp_cmt_pf) {
6904 a_net = sctp_find_alternate_net(stcb, net, 2);
6906 a_net = sctp_find_alternate_net(stcb, net, 0);
6908 if ((a_net != net) &&
6909 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
6911 * We only proceed if a valid alternate is found that is not
6912 * this one and is reachable. Here we must move all chunks
6913 * queued in the send queue off of the destination address
6916 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
6917 if (chk->whoTo == net) {
6918 /* Move the chunk to our alternate */
6919 sctp_free_remote_addr(chk->whoTo);
6921 atomic_add_int(&a_net->ref_count, 1);
6928 sctp_med_chunk_output(struct sctp_inpcb *inp,
6929 struct sctp_tcb *stcb,
6930 struct sctp_association *asoc,
6933 int control_only, int *cwnd_full, int from_where,
6934 struct timeval *now, int *now_filled, int frag_point, int so_locked
6935 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6941 * Ok this is the generic chunk service queue. we must do the
6942 * following: - Service the stream queue that is next, moving any
6943 * message (note I must get a complete message i.e. FIRST/MIDDLE and
6944 * LAST to the out queue in one pass) and assigning TSN's - Check to
6945 * see if the cwnd/rwnd allows any output, if so we go ahead and
6946 * fomulate and send the low level chunks. Making sure to combine
6947 * any control in the control chunk queue also.
6949 struct sctp_nets *net;
6950 struct mbuf *outchain, *endoutchain;
6951 struct sctp_tmit_chunk *chk, *nchk;
6952 struct sctphdr *shdr;
6954 /* temp arrays for unlinking */
6955 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6956 int no_fragmentflg, error;
6957 int one_chunk, hbflag, skip_data_for_this_net;
6958 int asconf, cookie, no_out_cnt;
6959 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
6960 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
6961 struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
6963 uint32_t auth_offset = 0;
6964 struct sctp_auth_chunk *auth = NULL;
6967 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
6976 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6977 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
6978 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
6983 ctl_cnt = no_out_cnt = asconf = cookie = 0;
6985 * First lets prime the pump. For each destination, if there is room
6986 * in the flight size, attempt to pull an MTU's worth out of the
6987 * stream queues into the general send_queue
6989 #ifdef SCTP_AUDITING_ENABLED
6990 sctp_audit_log(0xC2, 2);
6992 SCTP_TCB_LOCK_ASSERT(stcb);
6994 if ((control_only) || (asoc->stream_reset_outstanding))
6999 /* Nothing to possible to send? */
7000 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7001 TAILQ_EMPTY(&asoc->send_queue) &&
7002 TAILQ_EMPTY(&asoc->out_wheel)) {
7006 if (asoc->peers_rwnd == 0) {
7007 /* No room in peers rwnd */
7010 if (asoc->total_flight > 0) {
7011 /* we are allowed one chunk in flight */
7015 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7016 if (sctp_cmt_on_off) {
7018 * for CMT we start at the next one past the one we
7019 * last added data to.
7021 if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7022 goto skip_the_fill_from_streams;
7024 if (asoc->last_net_data_came_from) {
7025 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7027 net = TAILQ_FIRST(&asoc->nets);
7031 net = TAILQ_FIRST(&asoc->nets);
7035 * JRI-TODO: CMT-MPI. Simply set the first
7036 * destination (net) to be optimized for the next
7037 * message to be pulled out of the outwheel. 1. peek
7038 * at outwheel 2. If large message, set net =
7039 * highest_cwnd 3. If small message, set net =
7043 net = asoc->primary_destination;
7046 net = TAILQ_FIRST(&asoc->nets);
7052 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7053 net->window_probe = 0;
7054 if (old_startat && (old_startat == net)) {
7058 * JRI: if dest is unreachable or unconfirmed, do
7059 * not send data to it
7061 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7065 * JRI: if dest is in PF state, do not send data to
7068 if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF)) {
7071 if ((sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
7072 /* nothing can be in queue for this guy */
7075 if (net->flight_size >= net->cwnd) {
7076 /* skip this network, no room */
7081 * JRI : this for loop we are in takes in each net,
7082 * if its's got space in cwnd and has data sent to
7083 * it (when CMT is off) then it calls
7084 * sctp_fill_outqueue for the net. This gets data on
7085 * the send queue for that network.
7087 * In sctp_fill_outqueue TSN's are assigned and data is
7088 * copied out of the stream buffers. Note mostly
7089 * copy by reference (we hope).
7091 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7092 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7094 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7096 /* memory alloc failure */
7098 goto skip_the_fill_from_streams;
7101 if (start_at != TAILQ_FIRST(&asoc->nets)) {
7102 /* got to pick up the beginning stuff. */
7103 old_startat = start_at;
7104 start_at = net = TAILQ_FIRST(&asoc->nets);
7109 skip_the_fill_from_streams:
7110 *cwnd_full = cwnd_full_ind;
7112 /* now service each destination and send out what we can for it */
7113 /* Nothing to send? */
7114 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7115 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7119 if (no_data_chunks) {
7120 chk = TAILQ_FIRST(&asoc->control_send_queue);
7122 chk = TAILQ_FIRST(&asoc->send_queue);
7125 send_start_at = chk->whoTo;
7127 send_start_at = TAILQ_FIRST(&asoc->nets);
7130 again_one_more_time:
7131 for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7132 /* how much can we send? */
7133 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7134 if (old_startat && (old_startat == net)) {
7135 /* through list ocmpletely. */
7139 if (net->ref_count < 2) {
7141 * Ref-count of 1 so we cannot have data or control
7142 * queued to this address. Skip it.
7146 ctl_cnt = bundle_at = 0;
7147 endoutchain = outchain = NULL;
7150 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7151 skip_data_for_this_net = 1;
7153 skip_data_for_this_net = 0;
7155 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7157 * if we have a route and an ifp check to see if we
7158 * have room to send to this guy
7162 ifp = net->ro.ro_rt->rt_ifp;
7163 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7164 SCTP_STAT_INCR(sctps_ifnomemqueued);
7165 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
7166 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7171 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
7172 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7174 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7178 if (mtu > asoc->peers_rwnd) {
7179 if (asoc->total_flight > 0) {
7180 /* We have a packet in flight somewhere */
7181 r_mtu = asoc->peers_rwnd;
7183 /* We are always allowed to send one MTU out */
7190 /************************/
7191 /* Control transmission */
7192 /************************/
7193 /* Now first lets go through the control queue */
7194 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7196 nchk = TAILQ_NEXT(chk, sctp_next);
7197 if (chk->whoTo != net) {
7199 * No, not sent to the network we are
7204 if (chk->data == NULL) {
7207 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7209 * It must be unsent. Cookies and ASCONF's
7210 * hang around but there timers will force
7211 * when marked for resend.
7216 * if no AUTH is yet included and this chunk
7217 * requires it, make sure to account for it. We
7218 * don't apply the size until the AUTH chunk is
7219 * actually added below in case there is no room for
7220 * this chunk. NOTE: we overload the use of "omtu"
7223 if ((auth == NULL) &&
7224 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7225 stcb->asoc.peer_auth_chunks)) {
7226 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7229 /* Here we do NOT factor the r_mtu */
7230 if ((chk->send_size < (int)(mtu - omtu)) ||
7231 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7233 * We probably should glom the mbuf chain
7234 * from the chk->data for control but the
7235 * problem is it becomes yet one more level
7236 * of tracking to do if for some reason
7237 * output fails. Then I have got to
7238 * reconstruct the merged control chain.. el
7239 * yucko.. for now we take the easy way and
7243 * Add an AUTH chunk, if chunk requires it
7244 * save the offset into the chain for AUTH
7246 if ((auth == NULL) &&
7247 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7248 stcb->asoc.peer_auth_chunks))) {
7249 outchain = sctp_add_auth_chunk(outchain,
7254 chk->rec.chunk_id.id);
7255 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7257 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7258 (int)chk->rec.chunk_id.can_take_data,
7259 chk->send_size, chk->copy_by_ref);
7260 if (outchain == NULL) {
7262 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7265 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7266 /* update our MTU size */
7267 if (mtu > (chk->send_size + omtu))
7268 mtu -= (chk->send_size + omtu);
7271 to_out += (chk->send_size + omtu);
7272 /* Do clear IP_DF ? */
7273 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7276 if (chk->rec.chunk_id.can_take_data)
7278 /* Mark things to be removed, if needed */
7279 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7280 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7281 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7282 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7283 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7284 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7285 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7286 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7287 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7288 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7290 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7293 * JRS 5/14/07 - Set the
7294 * flag to say a heartbeat
7299 /* remove these chunks at the end */
7300 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7301 /* turn off the timer */
7302 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7303 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7304 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7310 * Other chunks, since they have
7311 * timers running (i.e. COOKIE or
7312 * ASCONF) we just "trust" that it
7313 * gets sent or retransmitted.
7316 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7319 } else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
7321 * set hb flag since we can
7327 * should sysctl this: don't
7328 * bundle data with ASCONF
7329 * since it requires AUTH
7333 chk->sent = SCTP_DATAGRAM_SENT;
7338 * Ok we are out of room but we can
7339 * output without effecting the
7340 * flight size since this little guy
7341 * is a control only packet.
7344 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7346 * do NOT clear the asconf
7347 * flag as it is used to do
7348 * appropriate source
7349 * address selection.
7353 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7356 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7357 if (outchain == NULL) {
7359 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7361 goto error_out_again;
7363 shdr = mtod(outchain, struct sctphdr *);
7364 shdr->src_port = inp->sctp_lport;
7365 shdr->dest_port = stcb->rport;
7366 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7368 auth_offset += sizeof(struct sctphdr);
7369 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7370 (struct sockaddr *)&net->ro._l_addr,
7371 outchain, auth_offset, auth,
7372 no_fragmentflg, 0, NULL, asconf, so_locked))) {
7373 if (error == ENOBUFS) {
7374 asoc->ifp_had_enobuf = 1;
7375 SCTP_STAT_INCR(sctps_lowlevelerr);
7377 if (from_where == 0) {
7378 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7381 /* error, could not output */
7383 if (*now_filled == 0) {
7384 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7386 *now = net->last_sent_time;
7388 net->last_sent_time = *now;
7392 if (error == EHOSTUNREACH) {
7398 sctp_move_to_an_alt(stcb, asoc, net);
7403 asoc->ifp_had_enobuf = 0;
7404 /* Only HB or ASCONF advances time */
7406 if (*now_filled == 0) {
7407 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7409 *now = net->last_sent_time;
7411 net->last_sent_time = *now;
7416 * increase the number we sent, if a
7417 * cookie is sent we don't tell them
7420 outchain = endoutchain = NULL;
7424 *num_out += ctl_cnt;
7425 /* recalc a clean slate and setup */
7426 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7427 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7429 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7436 /*********************/
7437 /* Data transmission */
7438 /*********************/
7440 * if AUTH for DATA is required and no AUTH has been added
7441 * yet, account for this in the mtu now... if no data can be
7442 * bundled, this adjustment won't matter anyways since the
7443 * packet will be going out...
7445 if ((auth == NULL) &&
7446 sctp_auth_is_required_chunk(SCTP_DATA,
7447 stcb->asoc.peer_auth_chunks)) {
7448 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7450 /* now lets add any data within the MTU constraints */
7451 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
7452 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
7453 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7457 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
7458 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7462 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
7464 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
7465 if (no_data_chunks) {
7466 /* let only control go out */
7470 if (net->flight_size >= net->cwnd) {
7471 /* skip this net, no room for data */
7475 nchk = TAILQ_NEXT(chk, sctp_next);
7476 if (chk->whoTo != net) {
7477 /* No, not sent to this net */
7480 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
7482 * strange, we have a chunk that is
7483 * to big for its destination and
7484 * yet no fragment ok flag.
7485 * Something went wrong when the
7486 * PMTU changed...we did not mark
7487 * this chunk for some reason?? I
7488 * will fix it here by letting IP
7489 * fragment it for now and printing
7490 * a warning. This really should not
7493 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
7494 chk->send_size, mtu);
7495 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
7497 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
7498 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
7499 /* ok we will add this one */
7502 * Add an AUTH chunk, if chunk
7503 * requires it, save the offset into
7504 * the chain for AUTH
7506 if ((auth == NULL) &&
7507 (sctp_auth_is_required_chunk(SCTP_DATA,
7508 stcb->asoc.peer_auth_chunks))) {
7510 outchain = sctp_add_auth_chunk(outchain,
7516 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7518 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
7519 chk->send_size, chk->copy_by_ref);
7520 if (outchain == NULL) {
7521 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
7522 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
7523 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7526 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7529 /* upate our MTU size */
7530 /* Do clear IP_DF ? */
7531 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7534 /* unsigned subtraction of mtu */
7535 if (mtu > chk->send_size)
7536 mtu -= chk->send_size;
7539 /* unsigned subtraction of r_mtu */
7540 if (r_mtu > chk->send_size)
7541 r_mtu -= chk->send_size;
7545 to_out += chk->send_size;
7546 if ((to_out > mx_mtu) && no_fragmentflg) {
7548 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
7550 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
7554 chk->window_probe = 0;
7555 data_list[bundle_at++] = chk;
7556 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
7560 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
7561 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
7562 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
7564 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
7566 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
7567 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
7577 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
7579 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
7581 data_list[0]->window_probe = 1;
7582 net->window_probe = 1;
7588 * Must be sent in order of the
7589 * TSN's (on a network)
7593 } /* for (chunk gather loop for this net) */
7594 } /* if asoc.state OPEN */
7595 /* Is there something to send for this destination? */
7597 /* We may need to start a control timer or two */
7599 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
7602 * do NOT clear the asconf flag as it is
7603 * used to do appropriate source address
7608 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7611 /* must start a send timer if data is being sent */
7612 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7614 * no timer running on this destination
7617 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7618 } else if (sctp_cmt_on_off && sctp_cmt_pf && pf_hbflag && ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)
7619 && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7621 * JRS 5/14/07 - If a HB has been sent to a
7622 * PF destination and no T3 timer is
7623 * currently running, start the T3 timer to
7624 * track the HBs that were sent.
7626 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7628 /* Now send it, if there is anything to send :> */
7629 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7630 if (outchain == NULL) {
7632 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7636 shdr = mtod(outchain, struct sctphdr *);
7637 shdr->src_port = inp->sctp_lport;
7638 shdr->dest_port = stcb->rport;
7639 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7641 auth_offset += sizeof(struct sctphdr);
7642 if ((error = sctp_lowlevel_chunk_output(inp,
7645 (struct sockaddr *)&net->ro._l_addr,
7652 asconf, so_locked))) {
7653 /* error, we could not output */
7654 if (error == ENOBUFS) {
7655 SCTP_STAT_INCR(sctps_lowlevelerr);
7656 asoc->ifp_had_enobuf = 1;
7658 if (from_where == 0) {
7659 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7662 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
7664 if (*now_filled == 0) {
7665 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7667 *now = net->last_sent_time;
7669 net->last_sent_time = *now;
7673 if (error == EHOSTUNREACH) {
7675 * Destination went unreachable
7678 sctp_move_to_an_alt(stcb, asoc, net);
7682 * I add this line to be paranoid. As far as
7683 * I can tell the continue, takes us back to
7684 * the top of the for, but just to make sure
7685 * I will reset these again here.
7687 ctl_cnt = bundle_at = 0;
7688 continue; /* This takes us back to the
7689 * for() for the nets. */
7691 asoc->ifp_had_enobuf = 0;
7693 outchain = endoutchain = NULL;
7696 if (bundle_at || hbflag) {
7697 /* For data/asconf and hb set time */
7698 if (*now_filled == 0) {
7699 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7701 *now = net->last_sent_time;
7703 net->last_sent_time = *now;
7707 *num_out += (ctl_cnt + bundle_at);
7710 /* setup for a RTO measurement */
7711 tsns_sent = data_list[0]->rec.data.TSN_seq;
7712 /* fill time if not already filled */
7713 if (*now_filled == 0) {
7714 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
7716 *now = asoc->time_last_sent;
7718 asoc->time_last_sent = *now;
7720 data_list[0]->do_rtt = 1;
7721 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
7722 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
7723 if (sctp_early_fr) {
7724 if (net->flight_size < net->cwnd) {
7725 /* start or restart it */
7726 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7727 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7728 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
7730 SCTP_STAT_INCR(sctps_earlyfrstrout);
7731 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
7733 /* stop it if its running */
7734 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7735 SCTP_STAT_INCR(sctps_earlyfrstpout);
7736 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7737 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
7746 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7747 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
7750 if (old_startat == NULL) {
7751 old_startat = send_start_at;
7752 send_start_at = TAILQ_FIRST(&asoc->nets);
7754 goto again_one_more_time;
7757 * At the end there should be no NON timed chunks hanging on this
7760 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7761 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
7763 if ((*num_out == 0) && (*reason_code == 0)) {
7768 sctp_clean_up_ctl(stcb, asoc);
7773 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
7776 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
7777 * the control chunk queue.
7779 struct sctp_chunkhdr *hdr;
7780 struct sctp_tmit_chunk *chk;
7783 SCTP_TCB_LOCK_ASSERT(stcb);
7784 sctp_alloc_a_chunk(stcb, chk);
7787 sctp_m_freem(op_err);
7790 chk->copy_by_ref = 0;
7791 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
7792 if (op_err == NULL) {
7793 sctp_free_a_chunk(stcb, chk);
7798 while (mat != NULL) {
7799 chk->send_size += SCTP_BUF_LEN(mat);
7800 mat = SCTP_BUF_NEXT(mat);
7802 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
7803 chk->rec.chunk_id.can_take_data = 1;
7804 chk->sent = SCTP_DATAGRAM_UNSENT;
7807 chk->asoc = &stcb->asoc;
7809 chk->whoTo = chk->asoc->primary_destination;
7810 atomic_add_int(&chk->whoTo->ref_count, 1);
7811 hdr = mtod(op_err, struct sctp_chunkhdr *);
7812 hdr->chunk_type = SCTP_OPERATION_ERROR;
7813 hdr->chunk_flags = 0;
7814 hdr->chunk_length = htons(chk->send_size);
7815 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
7818 chk->asoc->ctrl_queue_cnt++;
7822 sctp_send_cookie_echo(struct mbuf *m,
7824 struct sctp_tcb *stcb,
7825 struct sctp_nets *net)
7828 * pull out the cookie and put it at the front of the control chunk
7832 struct mbuf *cookie;
7833 struct sctp_paramhdr parm, *phdr;
7834 struct sctp_chunkhdr *hdr;
7835 struct sctp_tmit_chunk *chk;
7836 uint16_t ptype, plen;
7838 /* First find the cookie in the param area */
7840 at = offset + sizeof(struct sctp_init_chunk);
7842 SCTP_TCB_LOCK_ASSERT(stcb);
7844 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
7848 ptype = ntohs(phdr->param_type);
7849 plen = ntohs(phdr->param_length);
7850 if (ptype == SCTP_STATE_COOKIE) {
7853 /* found the cookie */
7854 if ((pad = (plen % 4))) {
7857 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
7858 if (cookie == NULL) {
7864 at += SCTP_SIZE32(plen);
7866 if (cookie == NULL) {
7867 /* Did not find the cookie */
7870 /* ok, we got the cookie lets change it into a cookie echo chunk */
7872 /* first the change from param to cookie */
7873 hdr = mtod(cookie, struct sctp_chunkhdr *);
7874 hdr->chunk_type = SCTP_COOKIE_ECHO;
7875 hdr->chunk_flags = 0;
7876 /* get the chunk stuff now and place it in the FRONT of the queue */
7877 sctp_alloc_a_chunk(stcb, chk);
7880 sctp_m_freem(cookie);
7883 chk->copy_by_ref = 0;
7884 chk->send_size = plen;
7885 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
7886 chk->rec.chunk_id.can_take_data = 0;
7887 chk->sent = SCTP_DATAGRAM_UNSENT;
7889 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
7890 chk->asoc = &stcb->asoc;
7892 chk->whoTo = chk->asoc->primary_destination;
7893 atomic_add_int(&chk->whoTo->ref_count, 1);
7894 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
7895 chk->asoc->ctrl_queue_cnt++;
7900 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
7904 struct sctp_nets *net)
7907 * take a HB request and make it into a HB ack and send it.
7909 struct mbuf *outchain;
7910 struct sctp_chunkhdr *chdr;
7911 struct sctp_tmit_chunk *chk;
7915 /* must have a net pointer */
7918 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
7919 if (outchain == NULL) {
7920 /* gak out of memory */
7923 chdr = mtod(outchain, struct sctp_chunkhdr *);
7924 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
7925 chdr->chunk_flags = 0;
7926 if (chk_length % 4) {
7928 uint32_t cpthis = 0;
7931 padlen = 4 - (chk_length % 4);
7932 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
7934 sctp_alloc_a_chunk(stcb, chk);
7937 sctp_m_freem(outchain);
7940 chk->copy_by_ref = 0;
7941 chk->send_size = chk_length;
7942 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
7943 chk->rec.chunk_id.can_take_data = 1;
7944 chk->sent = SCTP_DATAGRAM_UNSENT;
7947 chk->asoc = &stcb->asoc;
7948 chk->data = outchain;
7950 atomic_add_int(&chk->whoTo->ref_count, 1);
7951 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7952 chk->asoc->ctrl_queue_cnt++;
7956 sctp_send_cookie_ack(struct sctp_tcb *stcb)
7958 /* formulate and queue a cookie-ack back to sender */
7959 struct mbuf *cookie_ack;
7960 struct sctp_chunkhdr *hdr;
7961 struct sctp_tmit_chunk *chk;
7964 SCTP_TCB_LOCK_ASSERT(stcb);
7966 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
7967 if (cookie_ack == NULL) {
7971 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
7972 sctp_alloc_a_chunk(stcb, chk);
7975 sctp_m_freem(cookie_ack);
7978 chk->copy_by_ref = 0;
7979 chk->send_size = sizeof(struct sctp_chunkhdr);
7980 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
7981 chk->rec.chunk_id.can_take_data = 1;
7982 chk->sent = SCTP_DATAGRAM_UNSENT;
7985 chk->asoc = &stcb->asoc;
7986 chk->data = cookie_ack;
7987 if (chk->asoc->last_control_chunk_from != NULL) {
7988 chk->whoTo = chk->asoc->last_control_chunk_from;
7990 chk->whoTo = chk->asoc->primary_destination;
7992 atomic_add_int(&chk->whoTo->ref_count, 1);
7993 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
7994 hdr->chunk_type = SCTP_COOKIE_ACK;
7995 hdr->chunk_flags = 0;
7996 hdr->chunk_length = htons(chk->send_size);
7997 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
7998 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7999 chk->asoc->ctrl_queue_cnt++;
8005 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8007 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8008 struct mbuf *m_shutdown_ack;
8009 struct sctp_shutdown_ack_chunk *ack_cp;
8010 struct sctp_tmit_chunk *chk;
8012 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8013 if (m_shutdown_ack == NULL) {
8017 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8018 sctp_alloc_a_chunk(stcb, chk);
8021 sctp_m_freem(m_shutdown_ack);
8024 chk->copy_by_ref = 0;
8025 chk->send_size = sizeof(struct sctp_chunkhdr);
8026 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8027 chk->rec.chunk_id.can_take_data = 1;
8028 chk->sent = SCTP_DATAGRAM_UNSENT;
8031 chk->asoc = &stcb->asoc;
8032 chk->data = m_shutdown_ack;
8034 atomic_add_int(&net->ref_count, 1);
8036 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8037 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8038 ack_cp->ch.chunk_flags = 0;
8039 ack_cp->ch.chunk_length = htons(chk->send_size);
8040 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8041 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8042 chk->asoc->ctrl_queue_cnt++;
8047 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8049 /* formulate and queue a SHUTDOWN to the sender */
8050 struct mbuf *m_shutdown;
8051 struct sctp_shutdown_chunk *shutdown_cp;
8052 struct sctp_tmit_chunk *chk;
8054 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8055 if (m_shutdown == NULL) {
8059 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8060 sctp_alloc_a_chunk(stcb, chk);
8063 sctp_m_freem(m_shutdown);
8066 chk->copy_by_ref = 0;
8067 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8068 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8069 chk->rec.chunk_id.can_take_data = 1;
8070 chk->sent = SCTP_DATAGRAM_UNSENT;
8073 chk->asoc = &stcb->asoc;
8074 chk->data = m_shutdown;
8076 atomic_add_int(&net->ref_count, 1);
8078 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8079 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8080 shutdown_cp->ch.chunk_flags = 0;
8081 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8082 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8083 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8084 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8085 chk->asoc->ctrl_queue_cnt++;
8090 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8093 * formulate and queue an ASCONF to the peer. ASCONF parameters
8094 * should be queued on the assoc queue.
8096 struct sctp_tmit_chunk *chk;
8097 struct mbuf *m_asconf;
8098 struct sctp_asconf_chunk *acp;
8101 SCTP_TCB_LOCK_ASSERT(stcb);
8102 /* compose an ASCONF chunk, maximum length is PMTU */
8103 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8104 if (m_asconf == NULL) {
8107 acp = mtod(m_asconf, struct sctp_asconf_chunk *);
8108 sctp_alloc_a_chunk(stcb, chk);
8111 sctp_m_freem(m_asconf);
8114 chk->copy_by_ref = 0;
8115 chk->data = m_asconf;
8116 chk->send_size = len;
8117 chk->rec.chunk_id.id = SCTP_ASCONF;
8118 chk->rec.chunk_id.can_take_data = 0;
8119 chk->sent = SCTP_DATAGRAM_UNSENT;
8122 chk->asoc = &stcb->asoc;
8123 chk->whoTo = chk->asoc->primary_destination;
8124 atomic_add_int(&chk->whoTo->ref_count, 1);
8125 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8126 chk->asoc->ctrl_queue_cnt++;
8131 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8134 * formulate and queue a asconf-ack back to sender. the asconf-ack
8135 * must be stored in the tcb.
8137 struct sctp_tmit_chunk *chk;
8138 struct sctp_asconf_ack *ack, *latest_ack;
8139 struct mbuf *m_ack, *m;
8140 struct sctp_nets *net = NULL;
8142 SCTP_TCB_LOCK_ASSERT(stcb);
8143 /* Get the latest ASCONF-ACK */
8144 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8145 if (latest_ack == NULL) {
8148 if (latest_ack->last_sent_to != NULL &&
8149 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8150 /* we're doing a retransmission */
8151 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8154 if (stcb->asoc.last_control_chunk_from == NULL)
8155 net = stcb->asoc.primary_destination;
8157 net = stcb->asoc.last_control_chunk_from;
8161 if (stcb->asoc.last_control_chunk_from == NULL)
8162 net = stcb->asoc.primary_destination;
8164 net = stcb->asoc.last_control_chunk_from;
8166 latest_ack->last_sent_to = net;
8168 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8169 if (ack->data == NULL) {
8172 /* copy the asconf_ack */
8173 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8174 if (m_ack == NULL) {
8175 /* couldn't copy it */
8178 sctp_alloc_a_chunk(stcb, chk);
8182 sctp_m_freem(m_ack);
8185 chk->copy_by_ref = 0;
8192 chk->send_size = ack->len;
8193 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8194 chk->rec.chunk_id.can_take_data = 1;
8195 chk->sent = SCTP_DATAGRAM_UNSENT;
8197 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8198 chk->asoc = &stcb->asoc;
8199 atomic_add_int(&chk->whoTo->ref_count, 1);
8201 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8202 chk->asoc->ctrl_queue_cnt++;
8209 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8210 struct sctp_tcb *stcb,
8211 struct sctp_association *asoc,
8212 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8213 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8219 * send out one MTU of retransmission. If fast_retransmit is
8220 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8221 * rwnd. For a Cookie or Asconf in the control chunk queue we
8222 * retransmit them by themselves.
8224 * For data chunks we will pick out the lowest TSN's in the sent_queue
8225 * marked for resend and bundle them all together (up to a MTU of
8226 * destination). The address to send to should have been
8227 * selected/changed where the retransmission was marked (i.e. in FR
8228 * or t3-timeout routines).
8230 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8231 struct sctp_tmit_chunk *chk, *fwd;
8232 struct mbuf *m, *endofchain;
8233 struct sctphdr *shdr;
8235 struct sctp_nets *net = NULL;
8236 uint32_t tsns_sent = 0;
8237 int no_fragmentflg, bundle_at, cnt_thru;
8239 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8240 struct sctp_auth_chunk *auth = NULL;
8241 uint32_t auth_offset = 0;
8244 SCTP_TCB_LOCK_ASSERT(stcb);
8245 tmr_started = ctl_cnt = bundle_at = error = 0;
8251 endofchain = m = NULL;
8252 #ifdef SCTP_AUDITING_ENABLED
8253 sctp_audit_log(0xC3, 1);
8255 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8256 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8257 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8258 asoc->sent_queue_retran_cnt);
8259 asoc->sent_queue_cnt = 0;
8260 asoc->sent_queue_cnt_removeable = 0;
8261 /* send back 0/0 so we enter normal transmission */
8265 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8266 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8267 (chk->rec.chunk_id.id == SCTP_ASCONF) ||
8268 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8269 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8270 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8271 if (chk != asoc->str_reset) {
8273 * not eligible for retran if its
8280 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8284 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8289 * Add an AUTH chunk, if chunk requires it save the
8290 * offset into the chain for AUTH
8292 if ((auth == NULL) &&
8293 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8294 stcb->asoc.peer_auth_chunks))) {
8295 m = sctp_add_auth_chunk(m, &endofchain,
8296 &auth, &auth_offset,
8298 chk->rec.chunk_id.id);
8300 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8306 /* do we have control chunks to retransmit? */
8308 /* Start a timer no matter if we suceed or fail */
8309 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8310 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8311 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8312 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8314 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8316 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8319 shdr = mtod(m, struct sctphdr *);
8320 shdr->src_port = inp->sctp_lport;
8321 shdr->dest_port = stcb->rport;
8322 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8324 auth_offset += sizeof(struct sctphdr);
8325 chk->snd_count++; /* update our count */
8327 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8328 (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8329 auth, no_fragmentflg, 0, NULL, asconf, so_locked))) {
8330 SCTP_STAT_INCR(sctps_lowlevelerr);
8333 m = endofchain = NULL;
8337 * We don't want to mark the net->sent time here since this
8338 * we use this for HB and retrans cannot measure RTT
8340 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8342 chk->sent = SCTP_DATAGRAM_SENT;
8343 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8347 /* Clean up the fwd-tsn list */
8348 sctp_clean_up_ctl(stcb, asoc);
8353 * Ok, it is just data retransmission we need to do or that and a
8354 * fwd-tsn with it all.
8356 if (TAILQ_EMPTY(&asoc->sent_queue)) {
8357 return (SCTP_RETRAN_DONE);
8359 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8360 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8361 /* not yet open, resend the cookie and that is it */
8364 #ifdef SCTP_AUDITING_ENABLED
8365 sctp_auditing(20, inp, stcb, NULL);
8367 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8368 if (chk->sent != SCTP_DATAGRAM_RESEND) {
8369 /* No, not sent to this net or not ready for rtx */
8372 if ((sctp_max_retran_chunk) && (chk->snd_count >= sctp_max_retran_chunk)) {
8373 /* Gak, we have exceeded max unlucky retran, abort! */
8374 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8376 sctp_max_retran_chunk);
8377 atomic_add_int(&stcb->asoc.refcnt, 1);
8378 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8379 SCTP_TCB_LOCK(stcb);
8380 atomic_subtract_int(&stcb->asoc.refcnt, 1);
8381 return (SCTP_RETRAN_EXIT);
8383 /* pick up the net */
8385 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8386 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8388 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8391 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8392 /* No room in peers rwnd */
8395 tsn = asoc->last_acked_seq + 1;
8396 if (tsn == chk->rec.data.TSN_seq) {
8398 * we make a special exception for this
8399 * case. The peer has no rwnd but is missing
8400 * the lowest chunk.. which is probably what
8401 * is holding up the rwnd.
8403 goto one_chunk_around;
8408 if (asoc->peers_rwnd < mtu) {
8410 if ((asoc->peers_rwnd == 0) &&
8411 (asoc->total_flight == 0)) {
8412 chk->window_probe = 1;
8413 chk->whoTo->window_probe = 1;
8416 #ifdef SCTP_AUDITING_ENABLED
8417 sctp_audit_log(0xC3, 2);
8421 net->fast_retran_ip = 0;
8422 if (chk->rec.data.doing_fast_retransmit == 0) {
8424 * if no FR in progress skip destination that have
8425 * flight_size > cwnd.
8427 if (net->flight_size >= net->cwnd) {
8432 * Mark the destination net to have FR recovery
8436 net->fast_retran_ip = 1;
8440 * if no AUTH is yet included and this chunk requires it,
8441 * make sure to account for it. We don't apply the size
8442 * until the AUTH chunk is actually added below in case
8443 * there is no room for this chunk.
8445 if ((auth == NULL) &&
8446 sctp_auth_is_required_chunk(SCTP_DATA,
8447 stcb->asoc.peer_auth_chunks)) {
8448 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8452 if ((chk->send_size <= (mtu - dmtu)) ||
8453 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8454 /* ok we will add this one */
8455 if ((auth == NULL) &&
8456 (sctp_auth_is_required_chunk(SCTP_DATA,
8457 stcb->asoc.peer_auth_chunks))) {
8458 m = sctp_add_auth_chunk(m, &endofchain,
8459 &auth, &auth_offset,
8462 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8464 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8467 /* Do clear IP_DF ? */
8468 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8471 /* upate our MTU size */
8472 if (mtu > (chk->send_size + dmtu))
8473 mtu -= (chk->send_size + dmtu);
8476 data_list[bundle_at++] = chk;
8477 if (one_chunk && (asoc->total_flight <= 0)) {
8478 SCTP_STAT_INCR(sctps_windowprobed);
8481 if (one_chunk == 0) {
8483 * now are there anymore forward from chk to pick
8486 fwd = TAILQ_NEXT(chk, sctp_next);
8488 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
8489 /* Nope, not for retran */
8490 fwd = TAILQ_NEXT(fwd, sctp_next);
8493 if (fwd->whoTo != net) {
8494 /* Nope, not the net in question */
8495 fwd = TAILQ_NEXT(fwd, sctp_next);
8498 if ((auth == NULL) &&
8499 sctp_auth_is_required_chunk(SCTP_DATA,
8500 stcb->asoc.peer_auth_chunks)) {
8501 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8504 if (fwd->send_size <= (mtu - dmtu)) {
8505 if ((auth == NULL) &&
8506 (sctp_auth_is_required_chunk(SCTP_DATA,
8507 stcb->asoc.peer_auth_chunks))) {
8508 m = sctp_add_auth_chunk(m,
8510 &auth, &auth_offset,
8514 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
8516 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8519 /* Do clear IP_DF ? */
8520 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8523 /* upate our MTU size */
8524 if (mtu > (fwd->send_size + dmtu))
8525 mtu -= (fwd->send_size + dmtu);
8528 data_list[bundle_at++] = fwd;
8529 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8532 fwd = TAILQ_NEXT(fwd, sctp_next);
8534 /* can't fit so we are done */
8539 /* Is there something to send for this destination? */
8542 * No matter if we fail/or suceed we should start a
8543 * timer. A failure is like a lost IP packet :-)
8545 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8547 * no timer running on this destination
8550 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8553 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8555 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8558 shdr = mtod(m, struct sctphdr *);
8559 shdr->src_port = inp->sctp_lport;
8560 shdr->dest_port = stcb->rport;
8561 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8563 auth_offset += sizeof(struct sctphdr);
8564 /* Now lets send it, if there is anything to send :> */
8565 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8566 (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
8567 auth, no_fragmentflg, 0, NULL, asconf, so_locked))) {
8568 /* error, we could not output */
8569 SCTP_STAT_INCR(sctps_lowlevelerr);
8572 m = endofchain = NULL;
8577 * We don't want to mark the net->sent time here
8578 * since this we use this for HB and retrans cannot
8581 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
8583 /* For auto-close */
8585 if (*now_filled == 0) {
8586 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8587 *now = asoc->time_last_sent;
8590 asoc->time_last_sent = *now;
8592 *cnt_out += bundle_at;
8593 #ifdef SCTP_AUDITING_ENABLED
8594 sctp_audit_log(0xC4, bundle_at);
8597 tsns_sent = data_list[0]->rec.data.TSN_seq;
8599 for (i = 0; i < bundle_at; i++) {
8600 SCTP_STAT_INCR(sctps_sendretransdata);
8601 data_list[i]->sent = SCTP_DATAGRAM_SENT;
8603 * When we have a revoked data, and we
8604 * retransmit it, then we clear the revoked
8605 * flag since this flag dictates if we
8606 * subtracted from the fs
8608 if (data_list[i]->rec.data.chunk_was_revoked) {
8609 /* Deflate the cwnd */
8610 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
8611 data_list[i]->rec.data.chunk_was_revoked = 0;
8613 data_list[i]->snd_count++;
8614 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8615 /* record the time */
8616 data_list[i]->sent_rcv_time = asoc->time_last_sent;
8617 if (data_list[i]->book_size_scale) {
8619 * need to double the book size on
8622 data_list[i]->book_size_scale = 0;
8624 * Since we double the booksize, we
8625 * must also double the output queue
8626 * size, since this get shrunk when
8627 * we free by this amount.
8629 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
8630 data_list[i]->book_size *= 2;
8634 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
8635 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8636 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
8638 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8639 (uint32_t) (data_list[i]->send_size +
8640 sctp_peer_chunk_oh));
8642 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
8643 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
8644 data_list[i]->whoTo->flight_size,
8645 data_list[i]->book_size,
8646 (uintptr_t) data_list[i]->whoTo,
8647 data_list[i]->rec.data.TSN_seq);
8649 sctp_flight_size_increase(data_list[i]);
8650 sctp_total_flight_increase(stcb, data_list[i]);
8651 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8652 /* SWS sender side engages */
8653 asoc->peers_rwnd = 0;
8656 (data_list[i]->rec.data.doing_fast_retransmit)) {
8657 SCTP_STAT_INCR(sctps_sendfastretrans);
8658 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
8659 (tmr_started == 0)) {
8661 * ok we just fast-retrans'd
8662 * the lowest TSN, i.e the
8663 * first on the list. In
8664 * this case we want to give
8665 * some more time to get a
8666 * SACK back without a
8669 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
8670 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
8671 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8675 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8676 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
8678 #ifdef SCTP_AUDITING_ENABLED
8679 sctp_auditing(21, inp, stcb, NULL);
8685 if (asoc->sent_queue_retran_cnt <= 0) {
8686 /* all done we have no more to retran */
8687 asoc->sent_queue_retran_cnt = 0;
8691 /* No more room in rwnd */
8694 /* stop the for loop here. we sent out a packet */
8702 sctp_timer_validation(struct sctp_inpcb *inp,
8703 struct sctp_tcb *stcb,
8704 struct sctp_association *asoc,
8707 struct sctp_nets *net;
8709 /* Validate that a timer is running somewhere */
8710 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8711 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8712 /* Here is a timer */
8716 SCTP_TCB_LOCK_ASSERT(stcb);
8717 /* Gak, we did not have a timer somewhere */
8718 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
8719 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
8724 sctp_chunk_output(struct sctp_inpcb *inp,
8725 struct sctp_tcb *stcb,
8728 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8734 * Ok this is the generic chunk service queue. we must do the
8736 * - See if there are retransmits pending, if so we must
8738 * - Service the stream queue that is next, moving any
8739 * message (note I must get a complete message i.e.
8740 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
8742 * - Check to see if the cwnd/rwnd allows any output, if so we
8743 * go ahead and fomulate and send the low level chunks. Making sure
8744 * to combine any control in the control chunk queue also.
8746 struct sctp_association *asoc;
8747 struct sctp_nets *net;
8748 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
8749 burst_cnt = 0, burst_limit = 0;
8754 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8756 int fr_done, tot_frs = 0;
8759 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
8760 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
8766 SCTP_TCB_LOCK_ASSERT(stcb);
8768 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
8770 if ((un_sent <= 0) &&
8771 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
8772 (asoc->sent_queue_retran_cnt == 0)) {
8773 /* Nothing to do unless there is something to be sent left */
8777 * Do we have something to send, data or control AND a sack timer
8778 * running, if so piggy-back the sack.
8780 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8781 sctp_send_sack(stcb);
8782 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
8784 while (asoc->sent_queue_retran_cnt) {
8786 * Ok, it is retransmission time only, we send out only ONE
8787 * packet with a single call off to the retran code.
8789 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
8791 * Special hook for handling cookiess discarded
8792 * by peer that carried data. Send cookie-ack only
8793 * and then the next call with get the retran's.
8795 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
8796 &cwnd_full, from_where,
8797 &now, &now_filled, frag_point, so_locked);
8799 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
8800 /* if its not from a HB then do it */
8802 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
8808 * its from any other place, we don't allow retran
8809 * output (only control)
8814 /* Can't send anymore */
8816 * now lets push out control by calling med-level
8817 * output once. this assures that we WILL send HB's
8820 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
8821 &cwnd_full, from_where,
8822 &now, &now_filled, frag_point, so_locked);
8823 #ifdef SCTP_AUDITING_ENABLED
8824 sctp_auditing(8, inp, stcb, NULL);
8826 (void)sctp_timer_validation(inp, stcb, asoc, ret);
8831 * The count was off.. retran is not happening so do
8832 * the normal retransmission.
8834 #ifdef SCTP_AUDITING_ENABLED
8835 sctp_auditing(9, inp, stcb, NULL);
8837 if (ret == SCTP_RETRAN_EXIT) {
8842 if (from_where == SCTP_OUTPUT_FROM_T3) {
8843 /* Only one transmission allowed out of a timeout */
8844 #ifdef SCTP_AUDITING_ENABLED
8845 sctp_auditing(10, inp, stcb, NULL);
8847 /* Push out any control */
8848 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
8849 &now, &now_filled, frag_point, so_locked);
8852 if (tot_frs > asoc->max_burst) {
8853 /* Hit FR burst limit */
8856 if ((num_out == 0) && (ret == 0)) {
8858 /* No more retrans to send */
8862 #ifdef SCTP_AUDITING_ENABLED
8863 sctp_auditing(12, inp, stcb, NULL);
8865 /* Check for bad destinations, if they exist move chunks around. */
8866 burst_limit = asoc->max_burst;
8867 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8868 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
8869 SCTP_ADDR_NOT_REACHABLE) {
8871 * if possible move things off of this address we
8872 * still may send below due to the dormant state but
8873 * we try to find an alternate address to send to
8874 * and if we have one we move all queued data on the
8875 * out wheel to this alternate address.
8877 if (net->ref_count > 1)
8878 sctp_move_to_an_alt(stcb, asoc, net);
8879 } else if (sctp_cmt_on_off && sctp_cmt_pf && ((net->dest_state & SCTP_ADDR_PF) ==
8882 * JRS 5/14/07 - If CMT PF is on and the current
8883 * destination is in PF state, move all queued data
8884 * to an alternate desination.
8886 if (net->ref_count > 1)
8887 sctp_move_to_an_alt(stcb, asoc, net);
8890 * if ((asoc->sat_network) || (net->addr_is_local))
8891 * { burst_limit = asoc->max_burst *
8892 * SCTP_SAT_NETWORK_BURST_INCR; }
8894 if (sctp_use_cwnd_based_maxburst) {
8895 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
8897 * JRS - Use the congestion control
8898 * given in the congestion control
8901 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
8902 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8903 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
8905 SCTP_STAT_INCR(sctps_maxburstqueued);
8907 net->fast_retran_ip = 0;
8909 if (net->flight_size == 0) {
8910 /* Should be decaying the cwnd here */
8920 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
8921 &reason_code, 0, &cwnd_full, from_where,
8922 &now, &now_filled, frag_point, so_locked);
8924 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
8925 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8926 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
8928 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8929 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
8930 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
8934 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
8938 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8939 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
8941 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
8946 * When nagle is on, we look at how much is un_sent, then
8947 * if its smaller than an MTU and we have data in
8950 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8951 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count)
8952 * sizeof(struct sctp_data_chunk)));
8953 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
8954 (stcb->asoc.total_flight > 0)) {
8958 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
8959 TAILQ_EMPTY(&asoc->send_queue) &&
8960 TAILQ_EMPTY(&asoc->out_wheel)) {
8961 /* Nothing left to send */
8964 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
8965 /* Nothing left to send */
8968 } while (num_out && (sctp_use_cwnd_based_maxburst ||
8969 (burst_cnt < burst_limit)));
8971 if (sctp_use_cwnd_based_maxburst == 0) {
8972 if (burst_cnt >= burst_limit) {
8973 SCTP_STAT_INCR(sctps_maxburstqueued);
8974 asoc->burst_limit_applied = 1;
8975 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8976 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
8979 asoc->burst_limit_applied = 0;
8982 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8983 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
8985 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
8989 * Now we need to clean up the control chunk chain if a ECNE is on
8990 * it. It must be marked as UNSENT again so next call will continue
8991 * to send it until such time that we get a CWR, to remove it.
8993 if (stcb->asoc.ecn_echo_cnt_onq)
8994 sctp_fix_ecn_echo(asoc);
9000 sctp_output(inp, m, addr, control, p, flags)
9001 struct sctp_inpcb *inp;
9003 struct sockaddr *addr;
9004 struct mbuf *control;
9009 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9012 if (inp->sctp_socket == NULL) {
9013 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9016 return (sctp_sosend(inp->sctp_socket,
9026 send_forward_tsn(struct sctp_tcb *stcb,
9027 struct sctp_association *asoc)
9029 struct sctp_tmit_chunk *chk;
9030 struct sctp_forward_tsn_chunk *fwdtsn;
9032 SCTP_TCB_LOCK_ASSERT(stcb);
9033 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9034 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9035 /* mark it to unsent */
9036 chk->sent = SCTP_DATAGRAM_UNSENT;
9038 /* Do we correct its output location? */
9039 if (chk->whoTo != asoc->primary_destination) {
9040 sctp_free_remote_addr(chk->whoTo);
9041 chk->whoTo = asoc->primary_destination;
9042 atomic_add_int(&chk->whoTo->ref_count, 1);
9044 goto sctp_fill_in_rest;
9047 /* Ok if we reach here we must build one */
9048 sctp_alloc_a_chunk(stcb, chk);
9052 chk->copy_by_ref = 0;
9053 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9054 chk->rec.chunk_id.can_take_data = 0;
9058 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9059 if (chk->data == NULL) {
9060 sctp_free_a_chunk(stcb, chk);
9063 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9064 chk->sent = SCTP_DATAGRAM_UNSENT;
9066 chk->whoTo = asoc->primary_destination;
9067 atomic_add_int(&chk->whoTo->ref_count, 1);
9068 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9069 asoc->ctrl_queue_cnt++;
9072 * Here we go through and fill out the part that deals with
9073 * stream/seq of the ones we skip.
9075 SCTP_BUF_LEN(chk->data) = 0;
9077 struct sctp_tmit_chunk *at, *tp1, *last;
9078 struct sctp_strseq *strseq;
9079 unsigned int cnt_of_space, i, ovh;
9080 unsigned int space_needed;
9081 unsigned int cnt_of_skipped = 0;
9083 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9084 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9085 /* no more to look at */
9088 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9089 /* We don't report these */
9094 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9095 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9097 cnt_of_space = M_TRAILINGSPACE(chk->data);
9099 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9100 ovh = SCTP_MIN_OVERHEAD;
9102 ovh = SCTP_MIN_V4_OVERHEAD;
9104 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9105 /* trim to a mtu size */
9106 cnt_of_space = asoc->smallest_mtu - ovh;
9108 if (cnt_of_space < space_needed) {
9110 * ok we must trim down the chunk by lowering the
9111 * advance peer ack point.
9113 cnt_of_skipped = (cnt_of_space -
9114 ((sizeof(struct sctp_forward_tsn_chunk)) /
9115 sizeof(struct sctp_strseq)));
9117 * Go through and find the TSN that will be the one
9120 at = TAILQ_FIRST(&asoc->sent_queue);
9121 for (i = 0; i < cnt_of_skipped; i++) {
9122 tp1 = TAILQ_NEXT(at, sctp_next);
9127 * last now points to last one I can report, update
9130 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9131 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9133 chk->send_size = space_needed;
9134 /* Setup the chunk */
9135 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9136 fwdtsn->ch.chunk_length = htons(chk->send_size);
9137 fwdtsn->ch.chunk_flags = 0;
9138 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9139 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9140 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9141 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9142 SCTP_BUF_LEN(chk->data) = chk->send_size;
9145 * Move pointer to after the fwdtsn and transfer to the
9148 strseq = (struct sctp_strseq *)fwdtsn;
9150 * Now populate the strseq list. This is done blindly
9151 * without pulling out duplicate stream info. This is
9152 * inefficent but won't harm the process since the peer will
9153 * look at these in sequence and will thus release anything.
9154 * It could mean we exceed the PMTU and chop off some that
9155 * we could have included.. but this is unlikely (aka 1432/4
9156 * would mean 300+ stream seq's would have to be reported in
9157 * one FWD-TSN. With a bit of work we can later FIX this to
9158 * optimize and pull out duplcates.. but it does add more
9159 * overhead. So for now... not!
9161 at = TAILQ_FIRST(&asoc->sent_queue);
9162 for (i = 0; i < cnt_of_skipped; i++) {
9163 tp1 = TAILQ_NEXT(at, sctp_next);
9164 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9165 /* We don't report these */
9170 strseq->stream = ntohs(at->rec.data.stream_number);
9171 strseq->sequence = ntohs(at->rec.data.stream_seq);
9181 sctp_send_sack(struct sctp_tcb *stcb)
9184 * Queue up a SACK in the control queue. We must first check to see
9185 * if a SACK is somehow on the control queue. If so, we will take
9186 * and and remove the old one.
9188 struct sctp_association *asoc;
9189 struct sctp_tmit_chunk *chk, *a_chk;
9190 struct sctp_sack_chunk *sack;
9191 struct sctp_gap_ack_block *gap_descriptor;
9192 struct sack_track *selector;
9197 int limit_reached = 0;
9198 unsigned int i, jstart, siz, j;
9199 unsigned int num_gap_blocks = 0, space;
9205 SCTP_TCB_LOCK_ASSERT(stcb);
9206 if (asoc->last_data_chunk_from == NULL) {
9207 /* Hmm we never received anything */
9210 sctp_set_rwnd(stcb, asoc);
9211 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9212 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9213 /* Hmm, found a sack already on queue, remove it */
9214 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9215 asoc->ctrl_queue_cnt++;
9218 sctp_m_freem(a_chk->data);
9221 sctp_free_remote_addr(a_chk->whoTo);
9222 a_chk->whoTo = NULL;
9226 if (a_chk == NULL) {
9227 sctp_alloc_a_chunk(stcb, a_chk);
9228 if (a_chk == NULL) {
9229 /* No memory so we drop the idea, and set a timer */
9230 if (stcb->asoc.delayed_ack) {
9231 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9232 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9233 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9234 stcb->sctp_ep, stcb, NULL);
9236 stcb->asoc.send_sack = 1;
9240 a_chk->copy_by_ref = 0;
9241 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9242 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9243 a_chk->rec.chunk_id.can_take_data = 1;
9245 /* Clear our pkt counts */
9246 asoc->data_pkts_seen = 0;
9249 a_chk->snd_count = 0;
9250 a_chk->send_size = 0; /* fill in later */
9251 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9252 a_chk->whoTo = NULL;
9254 if ((asoc->numduptsns) ||
9255 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9258 * Ok, we have some duplicates or the destination for the
9259 * sack is unreachable, lets see if we can select an
9260 * alternate than asoc->last_data_chunk_from
9262 if ((!(asoc->last_data_chunk_from->dest_state &
9263 SCTP_ADDR_NOT_REACHABLE)) &&
9264 (asoc->used_alt_onsack > asoc->numnets)) {
9265 /* We used an alt last time, don't this time */
9266 a_chk->whoTo = NULL;
9268 asoc->used_alt_onsack++;
9269 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9271 if (a_chk->whoTo == NULL) {
9272 /* Nope, no alternate */
9273 a_chk->whoTo = asoc->last_data_chunk_from;
9274 asoc->used_alt_onsack = 0;
9278 * No duplicates so we use the last place we received data
9281 asoc->used_alt_onsack = 0;
9282 a_chk->whoTo = asoc->last_data_chunk_from;
9285 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9287 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9289 space_req = sizeof(struct sctp_sack_chunk);
9291 /* gaps get a cluster */
9292 space_req = MCLBYTES;
9294 /* Ok now lets formulate a MBUF with our sack */
9295 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9296 if ((a_chk->data == NULL) ||
9297 (a_chk->whoTo == NULL)) {
9298 /* rats, no mbuf memory */
9300 /* was a problem with the destination */
9301 sctp_m_freem(a_chk->data);
9304 sctp_free_a_chunk(stcb, a_chk);
9305 /* sa_ignore NO_NULL_CHK */
9306 if (stcb->asoc.delayed_ack) {
9307 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9308 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9309 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9310 stcb->sctp_ep, stcb, NULL);
9312 stcb->asoc.send_sack = 1;
9316 /* ok, lets go through and fill it in */
9317 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9318 space = M_TRAILINGSPACE(a_chk->data);
9319 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9320 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9322 limit = mtod(a_chk->data, caddr_t);
9325 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9326 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9327 /* 0x01 is used by nonce for ecn */
9328 if ((sctp_ecn_enable) &&
9330 (asoc->peer_supports_ecn_nonce))
9331 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9333 sack->ch.chunk_flags = 0;
9335 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
9337 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9338 * received, then set high bit to 1, else 0. Reset
9341 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9342 asoc->cmt_dac_pkts_rcvd = 0;
9344 #ifdef SCTP_ASOCLOG_OF_TSNS
9345 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9346 stcb->asoc.cumack_log_atsnt++;
9347 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9348 stcb->asoc.cumack_log_atsnt = 0;
9351 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9352 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9353 asoc->my_last_reported_rwnd = asoc->my_rwnd;
9355 /* reset the readers interpretation */
9356 stcb->freed_by_sorcv_sincelast = 0;
9358 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9360 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9361 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9364 * cum-ack behind the mapping array, so we start and use all
9369 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9371 * we skip the first one when the cum-ack is at or above the
9372 * mapping array base. Note this only works if
9376 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9377 /* we have a gap .. maybe */
9378 for (i = 0; i < siz; i++) {
9379 selector = &sack_array[asoc->mapping_array[i]];
9380 if (mergeable && selector->right_edge) {
9382 * Backup, left and right edges were ok to
9388 if (selector->num_entries == 0)
9391 for (j = jstart; j < selector->num_entries; j++) {
9392 if (mergeable && selector->right_edge) {
9394 * do a merge by NOT setting
9400 * no merge, set the left
9404 gap_descriptor->start = htons((selector->gaps[j].start + offset));
9406 gap_descriptor->end = htons((selector->gaps[j].end + offset));
9409 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
9415 if (selector->left_edge) {
9419 if (limit_reached) {
9420 /* Reached the limit stop */
9426 if (num_gap_blocks == 0) {
9428 * slide not yet happened, and somehow we got called
9429 * to send a sack. Cumack needs to move up.
9433 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
9434 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9435 sctp_sack_check(stcb, 0, 0, &abort_flag);
9438 /* now we must add any dups we are going to report. */
9439 if ((limit_reached == 0) && (asoc->numduptsns)) {
9440 dup = (uint32_t *) gap_descriptor;
9441 for (i = 0; i < asoc->numduptsns; i++) {
9442 *dup = htonl(asoc->dup_tsns[i]);
9445 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
9450 asoc->numduptsns = 0;
9453 * now that the chunk is prepared queue it to the control chunk
9456 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
9457 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
9458 (num_dups * sizeof(int32_t)));
9459 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
9460 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
9461 sack->sack.num_dup_tsns = htons(num_dups);
9462 sack->ch.chunk_length = htons(a_chk->send_size);
9463 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
9464 asoc->ctrl_queue_cnt++;
9465 asoc->send_sack = 0;
9466 SCTP_STAT_INCR(sctps_sendsacks);
9472 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
9473 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9478 struct mbuf *m_abort;
9479 struct mbuf *m_out = NULL, *m_end = NULL;
9480 struct sctp_abort_chunk *abort = NULL;
9482 uint32_t auth_offset = 0;
9483 struct sctp_auth_chunk *auth = NULL;
9484 struct sctphdr *shdr;
9487 * Add an AUTH chunk, if chunk requires it and save the offset into
9488 * the chain for AUTH
9490 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
9491 stcb->asoc.peer_auth_chunks)) {
9492 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
9493 stcb, SCTP_ABORT_ASSOCIATION);
9495 SCTP_TCB_LOCK_ASSERT(stcb);
9496 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9497 if (m_abort == NULL) {
9500 sctp_m_freem(m_out);
9503 /* link in any error */
9504 SCTP_BUF_NEXT(m_abort) = operr;
9511 sz += SCTP_BUF_LEN(n);
9512 n = SCTP_BUF_NEXT(n);
9515 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
9516 if (m_out == NULL) {
9517 /* NO Auth chunk prepended, so reserve space in front */
9518 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
9521 /* Put AUTH chunk at the front of the chain */
9522 SCTP_BUF_NEXT(m_end) = m_abort;
9525 /* fill in the ABORT chunk */
9526 abort = mtod(m_abort, struct sctp_abort_chunk *);
9527 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9528 abort->ch.chunk_flags = 0;
9529 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
9531 /* prepend and fill in the SCTP header */
9532 SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
9533 if (m_out == NULL) {
9534 /* TSNH: no memory */
9537 shdr = mtod(m_out, struct sctphdr *);
9538 shdr->src_port = stcb->sctp_ep->sctp_lport;
9539 shdr->dest_port = stcb->rport;
9540 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9542 auth_offset += sizeof(struct sctphdr);
9544 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
9545 stcb->asoc.primary_destination,
9546 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
9547 m_out, auth_offset, auth, 1, 0, NULL, 0, so_locked);
9548 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9552 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
9553 struct sctp_nets *net)
9555 /* formulate and SEND a SHUTDOWN-COMPLETE */
9556 struct mbuf *m_shutdown_comp;
9557 struct sctp_shutdown_complete_msg *comp_cp;
9559 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
9560 if (m_shutdown_comp == NULL) {
9564 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
9565 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9566 comp_cp->shut_cmp.ch.chunk_flags = 0;
9567 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9568 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
9569 comp_cp->sh.dest_port = stcb->rport;
9570 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
9571 comp_cp->sh.checksum = 0;
9573 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
9574 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
9575 (struct sockaddr *)&net->ro._l_addr,
9576 m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, SCTP_SO_NOT_LOCKED);
9577 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9582 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
9585 /* formulate and SEND a SHUTDOWN-COMPLETE */
9588 struct ip *iph, *iph_out;
9589 struct ip6_hdr *ip6, *ip6_out;
9590 int offset_out, len, mlen;
9591 struct sctp_shutdown_complete_msg *comp_cp;
9593 /* Get room for the largest message */
9594 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
9595 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
9599 SCTP_BUF_LEN(mout) = len;
9600 iph = mtod(m, struct ip *);
9604 if (iph->ip_v == IPVERSION) {
9605 SCTP_BUF_LEN(mout) = sizeof(struct ip) +
9606 sizeof(struct sctp_shutdown_complete_msg);
9607 SCTP_BUF_NEXT(mout) = NULL;
9608 iph_out = mtod(mout, struct ip *);
9610 /* Fill in the IP header for the ABORT */
9611 iph_out->ip_v = IPVERSION;
9612 iph_out->ip_hl = (sizeof(struct ip) / 4);
9613 iph_out->ip_tos = (u_char)0;
9615 iph_out->ip_off = 0;
9616 iph_out->ip_ttl = MAXTTL;
9617 iph_out->ip_p = IPPROTO_SCTP;
9618 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9619 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9621 /* let IP layer calculate this */
9622 iph_out->ip_sum = 0;
9623 offset_out += sizeof(*iph_out);
9624 comp_cp = (struct sctp_shutdown_complete_msg *)(
9625 (caddr_t)iph_out + offset_out);
9626 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9627 ip6 = (struct ip6_hdr *)iph;
9628 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr) +
9629 sizeof(struct sctp_shutdown_complete_msg);
9630 SCTP_BUF_NEXT(mout) = NULL;
9631 ip6_out = mtod(mout, struct ip6_hdr *);
9633 /* Fill in the IPv6 header for the ABORT */
9634 ip6_out->ip6_flow = ip6->ip6_flow;
9635 ip6_out->ip6_hlim = ip6_defhlim;
9636 ip6_out->ip6_nxt = IPPROTO_SCTP;
9637 ip6_out->ip6_src = ip6->ip6_dst;
9638 ip6_out->ip6_dst = ip6->ip6_src;
9640 * ?? The old code had both the iph len + payload, I think
9641 * this is wrong and would never have worked
9643 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
9644 offset_out += sizeof(*ip6_out);
9645 comp_cp = (struct sctp_shutdown_complete_msg *)(
9646 (caddr_t)ip6_out + offset_out);
9648 /* Currently not supported. */
9651 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
9656 /* Now copy in and fill in the ABORT tags etc. */
9657 comp_cp->sh.src_port = sh->dest_port;
9658 comp_cp->sh.dest_port = sh->src_port;
9659 comp_cp->sh.checksum = 0;
9660 comp_cp->sh.v_tag = sh->v_tag;
9661 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
9662 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9663 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9666 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
9667 if (iph_out != NULL) {
9670 struct sctp_tcb *stcb = NULL;
9672 mlen = SCTP_BUF_LEN(mout);
9673 bzero(&ro, sizeof ro);
9674 /* set IPv4 length */
9675 iph_out->ip_len = mlen;
9676 #ifdef SCTP_PACKET_LOGGING
9677 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9678 sctp_packet_log(mout, mlen);
9680 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9683 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
9685 /* Free the route if we got one back */
9688 } else if (ip6_out != NULL) {
9689 struct route_in6 ro;
9691 struct sctp_tcb *stcb = NULL;
9692 struct ifnet *ifp = NULL;
9694 bzero(&ro, sizeof(ro));
9695 mlen = SCTP_BUF_LEN(mout);
9696 #ifdef SCTP_PACKET_LOGGING
9697 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9698 sctp_packet_log(mout, mlen);
9700 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9701 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
9703 /* Free the route if we got one back */
9707 SCTP_STAT_INCR(sctps_sendpackets);
9708 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9709 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9714 static struct sctp_nets *
9715 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
9717 struct sctp_nets *net, *hnet;
9718 int ms_goneby, highest_ms, state_overide = 0;
9720 (void)SCTP_GETTIME_TIMEVAL(now);
9723 SCTP_TCB_LOCK_ASSERT(stcb);
9724 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9726 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
9727 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
9730 * Skip this guy from consideration if HB is off AND
9735 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
9736 /* skip this dest net from consideration */
9739 if (net->last_sent_time.tv_sec) {
9740 /* Sent to so we subtract */
9741 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
9743 /* Never been sent to */
9744 ms_goneby = 0x7fffffff;
9746 * When the address state is unconfirmed but still
9747 * considered reachable, we HB at a higher rate. Once it
9748 * goes confirmed OR reaches the "unreachable" state, thenw
9749 * we cut it back to HB at a more normal pace.
9751 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
9757 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
9758 (ms_goneby > highest_ms)) {
9759 highest_ms = ms_goneby;
9764 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
9770 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
9772 * Found the one with longest delay bounds OR it is
9773 * unconfirmed and still not marked unreachable.
9775 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
9778 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
9779 (struct sockaddr *)&hnet->ro._l_addr);
9781 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
9784 /* update the timer now */
9785 hnet->last_sent_time = *now;
9793 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
9795 struct sctp_tmit_chunk *chk;
9796 struct sctp_nets *net;
9797 struct sctp_heartbeat_chunk *hb;
9799 struct sockaddr_in *sin;
9800 struct sockaddr_in6 *sin6;
9802 SCTP_TCB_LOCK_ASSERT(stcb);
9803 if (user_req == 0) {
9804 net = sctp_select_hb_destination(stcb, &now);
9807 * All our busy none to send to, just start the
9810 if (stcb->asoc.state == 0) {
9813 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
9824 (void)SCTP_GETTIME_TIMEVAL(&now);
9826 sin = (struct sockaddr_in *)&net->ro._l_addr;
9827 if (sin->sin_family != AF_INET) {
9828 if (sin->sin_family != AF_INET6) {
9833 sctp_alloc_a_chunk(stcb, chk);
9835 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
9838 chk->copy_by_ref = 0;
9839 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
9840 chk->rec.chunk_id.can_take_data = 1;
9841 chk->asoc = &stcb->asoc;
9842 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
9844 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
9845 if (chk->data == NULL) {
9846 sctp_free_a_chunk(stcb, chk);
9849 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9850 SCTP_BUF_LEN(chk->data) = chk->send_size;
9851 chk->sent = SCTP_DATAGRAM_UNSENT;
9854 atomic_add_int(&chk->whoTo->ref_count, 1);
9855 /* Now we have a mbuf that we can fill in with the details */
9856 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
9857 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
9858 /* fill out chunk header */
9859 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
9860 hb->ch.chunk_flags = 0;
9861 hb->ch.chunk_length = htons(chk->send_size);
9862 /* Fill out hb parameter */
9863 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
9864 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
9865 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
9866 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
9867 /* Did our user request this one, put it in */
9868 hb->heartbeat.hb_info.user_req = user_req;
9869 hb->heartbeat.hb_info.addr_family = sin->sin_family;
9870 hb->heartbeat.hb_info.addr_len = sin->sin_len;
9871 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9873 * we only take from the entropy pool if the address is not
9876 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
9877 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
9879 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
9880 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
9882 if (sin->sin_family == AF_INET) {
9883 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
9884 } else if (sin->sin_family == AF_INET6) {
9885 /* We leave the scope the way it is in our lookup table. */
9886 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
9887 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
9889 /* huh compiler bug */
9894 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
9895 * PF-heartbeats. Because of this, threshold management is done by
9896 * the t3 timer handler, and does not need to be done upon the send
9897 * of a PF-heartbeat. If CMT PF is on and the destination to which a
9898 * heartbeat is being sent is in PF state, do NOT do threshold
9901 if ((sctp_cmt_pf == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
9902 /* ok we have a destination that needs a beat */
9903 /* lets do the theshold management Qiaobing style */
9904 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
9905 stcb->asoc.max_send_times)) {
9907 * we have lost the association, in a way this is
9908 * quite bad since we really are one less time since
9909 * we really did not send yet. This is the down side
9910 * to the Q's style as defined in the RFC and not my
9911 * alternate style defined in the RFC.
9913 if (chk->data != NULL) {
9914 sctp_m_freem(chk->data);
9918 * Here we do NOT use the macro since the
9919 * association is now gone.
9922 sctp_free_remote_addr(chk->whoTo);
9925 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
9929 net->hb_responded = 0;
9930 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9931 stcb->asoc.ctrl_queue_cnt++;
9932 SCTP_STAT_INCR(sctps_sendheartbeat);
9934 * Call directly med level routine to put out the chunk. It will
9935 * always tumble out control chunks aka HB but it may even tumble
9942 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
9945 struct sctp_association *asoc;
9946 struct sctp_ecne_chunk *ecne;
9947 struct sctp_tmit_chunk *chk;
9950 SCTP_TCB_LOCK_ASSERT(stcb);
9951 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9952 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9953 /* found a previous ECN_ECHO update it if needed */
9954 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
9955 ecne->tsn = htonl(high_tsn);
9959 /* nope could not find one to update so we must build one */
9960 sctp_alloc_a_chunk(stcb, chk);
9964 chk->copy_by_ref = 0;
9965 SCTP_STAT_INCR(sctps_sendecne);
9966 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
9967 chk->rec.chunk_id.can_take_data = 0;
9968 chk->asoc = &stcb->asoc;
9969 chk->send_size = sizeof(struct sctp_ecne_chunk);
9970 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
9971 if (chk->data == NULL) {
9972 sctp_free_a_chunk(stcb, chk);
9975 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9976 SCTP_BUF_LEN(chk->data) = chk->send_size;
9977 chk->sent = SCTP_DATAGRAM_UNSENT;
9980 atomic_add_int(&chk->whoTo->ref_count, 1);
9981 stcb->asoc.ecn_echo_cnt_onq++;
9982 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
9983 ecne->ch.chunk_type = SCTP_ECN_ECHO;
9984 ecne->ch.chunk_flags = 0;
9985 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
9986 ecne->tsn = htonl(high_tsn);
9987 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9988 asoc->ctrl_queue_cnt++;
9992 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
9993 struct mbuf *m, int iphlen, int bad_crc)
9995 struct sctp_association *asoc;
9996 struct sctp_pktdrop_chunk *drp;
9997 struct sctp_tmit_chunk *chk;
10002 int fullsz = 0, extra = 0;
10005 struct sctp_chunkhdr *ch, chunk_buf;
10006 unsigned int chk_length;
10011 asoc = &stcb->asoc;
10012 SCTP_TCB_LOCK_ASSERT(stcb);
10013 if (asoc->peer_supports_pktdrop == 0) {
10015 * peer must declare support before I send one.
10019 if (stcb->sctp_socket == NULL) {
10022 sctp_alloc_a_chunk(stcb, chk);
10026 chk->copy_by_ref = 0;
10027 iph = mtod(m, struct ip *);
10029 sctp_free_a_chunk(stcb, chk);
10032 if (iph->ip_v == IPVERSION) {
10034 len = chk->send_size = iph->ip_len;
10036 struct ip6_hdr *ip6h;
10039 ip6h = mtod(m, struct ip6_hdr *);
10040 len = chk->send_size = htons(ip6h->ip6_plen);
10042 /* Validate that we do not have an ABORT in here. */
10043 offset = iphlen + sizeof(struct sctphdr);
10044 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10045 sizeof(*ch), (uint8_t *) & chunk_buf);
10046 while (ch != NULL) {
10047 chk_length = ntohs(ch->chunk_length);
10048 if (chk_length < sizeof(*ch)) {
10049 /* break to abort land */
10052 switch (ch->chunk_type) {
10053 case SCTP_PACKET_DROPPED:
10054 case SCTP_ABORT_ASSOCIATION:
10056 * we don't respond with an PKT-DROP to an ABORT
10059 sctp_free_a_chunk(stcb, chk);
10064 offset += SCTP_SIZE32(chk_length);
10065 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10066 sizeof(*ch), (uint8_t *) & chunk_buf);
10069 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10070 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10072 * only send 1 mtu worth, trim off the excess on the end.
10074 fullsz = len - extra;
10075 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10078 chk->asoc = &stcb->asoc;
10079 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10080 if (chk->data == NULL) {
10082 sctp_free_a_chunk(stcb, chk);
10085 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10086 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10088 sctp_m_freem(chk->data);
10092 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10093 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10094 chk->book_size_scale = 0;
10096 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10097 drp->trunc_len = htons(fullsz);
10099 * Len is already adjusted to size minus overhead above take
10100 * out the pkt_drop chunk itself from it.
10102 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10103 len = chk->send_size;
10105 /* no truncation needed */
10106 drp->ch.chunk_flags = 0;
10107 drp->trunc_len = htons(0);
10110 drp->ch.chunk_flags |= SCTP_BADCRC;
10112 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10113 SCTP_BUF_LEN(chk->data) = chk->send_size;
10114 chk->sent = SCTP_DATAGRAM_UNSENT;
10115 chk->snd_count = 0;
10117 /* we should hit here */
10120 chk->whoTo = asoc->primary_destination;
10122 atomic_add_int(&chk->whoTo->ref_count, 1);
10123 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10124 chk->rec.chunk_id.can_take_data = 1;
10125 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10126 drp->ch.chunk_length = htons(chk->send_size);
10127 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10131 drp->bottle_bw = htonl(spc);
10132 if (asoc->my_rwnd) {
10133 drp->current_onq = htonl(asoc->size_on_reasm_queue +
10134 asoc->size_on_all_streams +
10135 asoc->my_rwnd_control_len +
10136 stcb->sctp_socket->so_rcv.sb_cc);
10139 * If my rwnd is 0, possibly from mbuf depletion as well as
10140 * space used, tell the peer there is NO space aka onq == bw
10142 drp->current_onq = htonl(spc);
10146 m_copydata(m, iphlen, len, (caddr_t)datap);
10147 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10148 asoc->ctrl_queue_cnt++;
10152 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10154 struct sctp_association *asoc;
10155 struct sctp_cwr_chunk *cwr;
10156 struct sctp_tmit_chunk *chk;
10158 asoc = &stcb->asoc;
10159 SCTP_TCB_LOCK_ASSERT(stcb);
10160 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10161 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10162 /* found a previous ECN_CWR update it if needed */
10163 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10164 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10166 cwr->tsn = htonl(high_tsn);
10171 /* nope could not find one to update so we must build one */
10172 sctp_alloc_a_chunk(stcb, chk);
10176 chk->copy_by_ref = 0;
10177 chk->rec.chunk_id.id = SCTP_ECN_CWR;
10178 chk->rec.chunk_id.can_take_data = 1;
10179 chk->asoc = &stcb->asoc;
10180 chk->send_size = sizeof(struct sctp_cwr_chunk);
10181 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10182 if (chk->data == NULL) {
10183 sctp_free_a_chunk(stcb, chk);
10186 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10187 SCTP_BUF_LEN(chk->data) = chk->send_size;
10188 chk->sent = SCTP_DATAGRAM_UNSENT;
10189 chk->snd_count = 0;
10191 atomic_add_int(&chk->whoTo->ref_count, 1);
10192 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10193 cwr->ch.chunk_type = SCTP_ECN_CWR;
10194 cwr->ch.chunk_flags = 0;
10195 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10196 cwr->tsn = htonl(high_tsn);
10197 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10198 asoc->ctrl_queue_cnt++;
10202 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10203 int number_entries, uint16_t * list,
10204 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10206 int len, old_len, i;
10207 struct sctp_stream_reset_out_request *req_out;
10208 struct sctp_chunkhdr *ch;
10210 ch = mtod(chk->data, struct sctp_chunkhdr *);
10213 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10215 /* get to new offset for the param. */
10216 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10217 /* now how long will this param be? */
10218 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10219 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10220 req_out->ph.param_length = htons(len);
10221 req_out->request_seq = htonl(seq);
10222 req_out->response_seq = htonl(resp_seq);
10223 req_out->send_reset_at_tsn = htonl(last_sent);
10224 if (number_entries) {
10225 for (i = 0; i < number_entries; i++) {
10226 req_out->list_of_streams[i] = htons(list[i]);
10229 if (SCTP_SIZE32(len) > len) {
10231 * Need to worry about the pad we may end up adding to the
10232 * end. This is easy since the struct is either aligned to 4
10233 * bytes or 2 bytes off.
10235 req_out->list_of_streams[number_entries] = 0;
10237 /* now fix the chunk length */
10238 ch->chunk_length = htons(len + old_len);
10239 chk->book_size = len + old_len;
10240 chk->book_size_scale = 0;
10241 chk->send_size = SCTP_SIZE32(chk->book_size);
10242 SCTP_BUF_LEN(chk->data) = chk->send_size;
10248 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10249 int number_entries, uint16_t * list,
10252 int len, old_len, i;
10253 struct sctp_stream_reset_in_request *req_in;
10254 struct sctp_chunkhdr *ch;
10256 ch = mtod(chk->data, struct sctp_chunkhdr *);
10259 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10261 /* get to new offset for the param. */
10262 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10263 /* now how long will this param be? */
10264 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10265 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10266 req_in->ph.param_length = htons(len);
10267 req_in->request_seq = htonl(seq);
10268 if (number_entries) {
10269 for (i = 0; i < number_entries; i++) {
10270 req_in->list_of_streams[i] = htons(list[i]);
10273 if (SCTP_SIZE32(len) > len) {
10275 * Need to worry about the pad we may end up adding to the
10276 * end. This is easy since the struct is either aligned to 4
10277 * bytes or 2 bytes off.
10279 req_in->list_of_streams[number_entries] = 0;
10281 /* now fix the chunk length */
10282 ch->chunk_length = htons(len + old_len);
10283 chk->book_size = len + old_len;
10284 chk->book_size_scale = 0;
10285 chk->send_size = SCTP_SIZE32(chk->book_size);
10286 SCTP_BUF_LEN(chk->data) = chk->send_size;
10292 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10296 struct sctp_stream_reset_tsn_request *req_tsn;
10297 struct sctp_chunkhdr *ch;
10299 ch = mtod(chk->data, struct sctp_chunkhdr *);
10302 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10304 /* get to new offset for the param. */
10305 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10306 /* now how long will this param be? */
10307 len = sizeof(struct sctp_stream_reset_tsn_request);
10308 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10309 req_tsn->ph.param_length = htons(len);
10310 req_tsn->request_seq = htonl(seq);
10312 /* now fix the chunk length */
10313 ch->chunk_length = htons(len + old_len);
10314 chk->send_size = len + old_len;
10315 chk->book_size = SCTP_SIZE32(chk->send_size);
10316 chk->book_size_scale = 0;
10317 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10322 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10323 uint32_t resp_seq, uint32_t result)
10326 struct sctp_stream_reset_response *resp;
10327 struct sctp_chunkhdr *ch;
10329 ch = mtod(chk->data, struct sctp_chunkhdr *);
10332 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10334 /* get to new offset for the param. */
10335 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
10336 /* now how long will this param be? */
10337 len = sizeof(struct sctp_stream_reset_response);
10338 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10339 resp->ph.param_length = htons(len);
10340 resp->response_seq = htonl(resp_seq);
10341 resp->result = ntohl(result);
10343 /* now fix the chunk length */
10344 ch->chunk_length = htons(len + old_len);
10345 chk->book_size = len + old_len;
10346 chk->book_size_scale = 0;
10347 chk->send_size = SCTP_SIZE32(chk->book_size);
10348 SCTP_BUF_LEN(chk->data) = chk->send_size;
10355 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
10356 uint32_t resp_seq, uint32_t result,
10357 uint32_t send_una, uint32_t recv_next)
10360 struct sctp_stream_reset_response_tsn *resp;
10361 struct sctp_chunkhdr *ch;
10363 ch = mtod(chk->data, struct sctp_chunkhdr *);
10366 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10368 /* get to new offset for the param. */
10369 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
10370 /* now how long will this param be? */
10371 len = sizeof(struct sctp_stream_reset_response_tsn);
10372 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10373 resp->ph.param_length = htons(len);
10374 resp->response_seq = htonl(resp_seq);
10375 resp->result = htonl(result);
10376 resp->senders_next_tsn = htonl(send_una);
10377 resp->receivers_next_tsn = htonl(recv_next);
10379 /* now fix the chunk length */
10380 ch->chunk_length = htons(len + old_len);
10381 chk->book_size = len + old_len;
10382 chk->send_size = SCTP_SIZE32(chk->book_size);
10383 chk->book_size_scale = 0;
10384 SCTP_BUF_LEN(chk->data) = chk->send_size;
10390 sctp_send_str_reset_req(struct sctp_tcb *stcb,
10391 int number_entries, uint16_t * list,
10392 uint8_t send_out_req, uint32_t resp_seq,
10393 uint8_t send_in_req,
10394 uint8_t send_tsn_req)
10397 struct sctp_association *asoc;
10398 struct sctp_tmit_chunk *chk;
10399 struct sctp_chunkhdr *ch;
10402 asoc = &stcb->asoc;
10403 if (asoc->stream_reset_outstanding) {
10405 * Already one pending, must get ACK back to clear the flag.
10407 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
10410 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
10411 /* nothing to do */
10412 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10415 if (send_tsn_req && (send_out_req || send_in_req)) {
10416 /* error, can't do that */
10417 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10420 sctp_alloc_a_chunk(stcb, chk);
10422 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10425 chk->copy_by_ref = 0;
10426 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
10427 chk->rec.chunk_id.can_take_data = 0;
10428 chk->asoc = &stcb->asoc;
10429 chk->book_size = sizeof(struct sctp_chunkhdr);
10430 chk->send_size = SCTP_SIZE32(chk->book_size);
10431 chk->book_size_scale = 0;
10433 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10434 if (chk->data == NULL) {
10435 sctp_free_a_chunk(stcb, chk);
10436 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10439 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10441 /* setup chunk parameters */
10442 chk->sent = SCTP_DATAGRAM_UNSENT;
10443 chk->snd_count = 0;
10444 chk->whoTo = asoc->primary_destination;
10445 atomic_add_int(&chk->whoTo->ref_count, 1);
10447 ch = mtod(chk->data, struct sctp_chunkhdr *);
10448 ch->chunk_type = SCTP_STREAM_RESET;
10449 ch->chunk_flags = 0;
10450 ch->chunk_length = htons(chk->book_size);
10451 SCTP_BUF_LEN(chk->data) = chk->send_size;
10453 seq = stcb->asoc.str_reset_seq_out;
10454 if (send_out_req) {
10455 sctp_add_stream_reset_out(chk, number_entries, list,
10456 seq, resp_seq, (stcb->asoc.sending_seq - 1));
10457 asoc->stream_reset_out_is_outstanding = 1;
10459 asoc->stream_reset_outstanding++;
10462 sctp_add_stream_reset_in(chk, number_entries, list, seq);
10463 asoc->stream_reset_outstanding++;
10465 if (send_tsn_req) {
10466 sctp_add_stream_reset_tsn(chk, seq);
10467 asoc->stream_reset_outstanding++;
10469 asoc->str_reset = chk;
10471 /* insert the chunk for sending */
10472 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
10475 asoc->ctrl_queue_cnt++;
10476 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
10481 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
10482 struct mbuf *err_cause, uint32_t vrf_id)
10485 * Formulate the abort message, and send it back down.
10487 struct mbuf *o_pak;
10489 struct sctp_abort_msg *abm;
10490 struct ip *iph, *iph_out;
10491 struct ip6_hdr *ip6, *ip6_out;
10492 int iphlen_out, len;
10494 /* don't respond to ABORT with ABORT */
10495 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
10497 sctp_m_freem(err_cause);
10500 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
10502 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10503 if (mout == NULL) {
10505 sctp_m_freem(err_cause);
10508 iph = mtod(m, struct ip *);
10511 if (iph->ip_v == IPVERSION) {
10512 iph_out = mtod(mout, struct ip *);
10513 SCTP_BUF_LEN(mout) = sizeof(*iph_out) + sizeof(*abm);
10514 SCTP_BUF_NEXT(mout) = err_cause;
10516 /* Fill in the IP header for the ABORT */
10517 iph_out->ip_v = IPVERSION;
10518 iph_out->ip_hl = (sizeof(struct ip) / 4);
10519 iph_out->ip_tos = (u_char)0;
10520 iph_out->ip_id = 0;
10521 iph_out->ip_off = 0;
10522 iph_out->ip_ttl = MAXTTL;
10523 iph_out->ip_p = IPPROTO_SCTP;
10524 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10525 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10526 /* let IP layer calculate this */
10527 iph_out->ip_sum = 0;
10529 iphlen_out = sizeof(*iph_out);
10530 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
10531 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
10532 ip6 = (struct ip6_hdr *)iph;
10533 ip6_out = mtod(mout, struct ip6_hdr *);
10534 SCTP_BUF_LEN(mout) = sizeof(*ip6_out) + sizeof(*abm);
10535 SCTP_BUF_NEXT(mout) = err_cause;
10537 /* Fill in the IP6 header for the ABORT */
10538 ip6_out->ip6_flow = ip6->ip6_flow;
10539 ip6_out->ip6_hlim = ip6_defhlim;
10540 ip6_out->ip6_nxt = IPPROTO_SCTP;
10541 ip6_out->ip6_src = ip6->ip6_dst;
10542 ip6_out->ip6_dst = ip6->ip6_src;
10544 iphlen_out = sizeof(*ip6_out);
10545 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
10547 /* Currently not supported */
10549 sctp_m_freem(err_cause);
10550 sctp_m_freem(mout);
10554 abm->sh.src_port = sh->dest_port;
10555 abm->sh.dest_port = sh->src_port;
10556 abm->sh.checksum = 0;
10558 abm->sh.v_tag = sh->v_tag;
10559 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
10561 abm->sh.v_tag = htonl(vtag);
10562 abm->msg.ch.chunk_flags = 0;
10564 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10567 struct mbuf *m_tmp = err_cause;
10570 /* get length of the err_cause chain */
10571 while (m_tmp != NULL) {
10572 err_len += SCTP_BUF_LEN(m_tmp);
10573 m_tmp = SCTP_BUF_NEXT(m_tmp);
10575 len = SCTP_BUF_LEN(mout) + err_len;
10577 /* need pad at end of chunk */
10578 uint32_t cpthis = 0;
10581 padlen = 4 - (len % 4);
10582 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
10585 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
10587 len = SCTP_BUF_LEN(mout);
10588 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
10592 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
10593 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10595 sctp_m_freem(mout);
10598 if (iph_out != NULL) {
10600 struct sctp_tcb *stcb = NULL;
10603 /* zap the stack pointer to the route */
10604 bzero(&ro, sizeof ro);
10605 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
10606 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
10607 /* set IPv4 length */
10608 iph_out->ip_len = len;
10610 #ifdef SCTP_PACKET_LOGGING
10611 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10612 sctp_packet_log(mout, len);
10614 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10615 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10617 /* Free the route if we got one back */
10620 } else if (ip6_out != NULL) {
10621 struct route_in6 ro;
10623 struct sctp_tcb *stcb = NULL;
10624 struct ifnet *ifp = NULL;
10626 /* zap the stack pointer to the route */
10627 bzero(&ro, sizeof(ro));
10628 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
10629 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
10630 ip6_out->ip6_plen = len - sizeof(*ip6_out);
10631 #ifdef SCTP_PACKET_LOGGING
10632 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10633 sctp_packet_log(mout, len);
10635 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10636 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10638 /* Free the route if we got one back */
10642 SCTP_STAT_INCR(sctps_sendpackets);
10643 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10647 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
10650 struct mbuf *o_pak;
10651 struct sctphdr *ihdr;
10653 struct sctphdr *ohdr;
10654 struct sctp_chunkhdr *ophdr;
10659 struct sockaddr_in6 lsa6, fsa6;
10666 iph = mtod(m, struct ip *);
10667 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
10669 SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
10671 /* can't send because we can't add a mbuf */
10674 ohdr = mtod(scm, struct sctphdr *);
10675 ohdr->src_port = ihdr->dest_port;
10676 ohdr->dest_port = ihdr->src_port;
10677 ohdr->v_tag = vtag;
10678 ohdr->checksum = 0;
10679 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
10680 ophdr->chunk_type = SCTP_OPERATION_ERROR;
10681 ophdr->chunk_flags = 0;
10685 len += SCTP_BUF_LEN(at);
10686 at = SCTP_BUF_NEXT(at);
10688 ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
10691 uint32_t cpthis = 0;
10694 padlen = 4 - (len % 4);
10695 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
10698 val = sctp_calculate_sum(scm, NULL, 0);
10699 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
10700 if (mout == NULL) {
10704 SCTP_BUF_NEXT(mout) = scm;
10705 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10706 sctp_m_freem(mout);
10709 ohdr->checksum = val;
10710 if (iph->ip_v == IPVERSION) {
10714 struct sctp_tcb *stcb = NULL;
10716 SCTP_BUF_LEN(mout) = sizeof(struct ip);
10717 len += sizeof(struct ip);
10719 bzero(&ro, sizeof ro);
10720 out = mtod(mout, struct ip *);
10721 out->ip_v = iph->ip_v;
10722 out->ip_hl = (sizeof(struct ip) / 4);
10723 out->ip_tos = iph->ip_tos;
10724 out->ip_id = iph->ip_id;
10726 out->ip_ttl = MAXTTL;
10727 out->ip_p = IPPROTO_SCTP;
10729 out->ip_src = iph->ip_dst;
10730 out->ip_dst = iph->ip_src;
10732 #ifdef SCTP_PACKET_LOGGING
10733 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10734 sctp_packet_log(mout, len);
10736 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10738 SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
10740 SCTP_STAT_INCR(sctps_sendpackets);
10741 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10742 /* Free the route if we got one back */
10747 struct route_in6 ro;
10749 struct sctp_tcb *stcb = NULL;
10750 struct ifnet *ifp = NULL;
10751 struct ip6_hdr *out6, *in6;
10753 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
10754 len += sizeof(struct ip6_hdr);
10755 bzero(&ro, sizeof ro);
10756 in6 = mtod(m, struct ip6_hdr *);
10757 out6 = mtod(mout, struct ip6_hdr *);
10758 out6->ip6_flow = in6->ip6_flow;
10759 out6->ip6_hlim = ip6_defhlim;
10760 out6->ip6_nxt = IPPROTO_SCTP;
10761 out6->ip6_src = in6->ip6_dst;
10762 out6->ip6_dst = in6->ip6_src;
10763 out6->ip6_plen = len - sizeof(struct ip6_hdr);
10766 bzero(&lsa6, sizeof(lsa6));
10767 lsa6.sin6_len = sizeof(lsa6);
10768 lsa6.sin6_family = AF_INET6;
10769 lsa6.sin6_addr = out6->ip6_src;
10770 bzero(&fsa6, sizeof(fsa6));
10771 fsa6.sin6_len = sizeof(fsa6);
10772 fsa6.sin6_family = AF_INET6;
10773 fsa6.sin6_addr = out6->ip6_dst;
10775 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
10776 SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
10777 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
10778 SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
10779 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
10781 #ifdef SCTP_PACKET_LOGGING
10782 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10783 sctp_packet_log(mout, len);
10785 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10786 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10788 SCTP_STAT_INCR(sctps_sendpackets);
10789 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10790 /* Free the route if we got one back */
10796 static struct mbuf *
10797 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
10799 struct sctp_sndrcvinfo *srcv,
10801 int user_marks_eor,
10804 struct mbuf **new_tail)
10808 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
10809 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
10811 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10814 *sndout = m_length(m, NULL);
10815 *new_tail = m_last(m);
10821 sctp_copy_one(struct sctp_stream_queue_pending *sp,
10828 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
10830 if (sp->data == NULL) {
10831 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10834 sp->tail_mbuf = m_last(sp->data);
10840 static struct sctp_stream_queue_pending *
10841 sctp_copy_it_in(struct sctp_tcb *stcb,
10842 struct sctp_association *asoc,
10843 struct sctp_sndrcvinfo *srcv,
10845 struct sctp_nets *net,
10847 int user_marks_eor,
10852 * This routine must be very careful in its work. Protocol
10853 * processing is up and running so care must be taken to spl...()
10854 * when you need to do something that may effect the stcb/asoc. The
10855 * sb is locked however. When data is copied the protocol processing
10856 * should be enabled since this is a slower operation...
10858 struct sctp_stream_queue_pending *sp = NULL;
10862 /* Now can we send this? */
10863 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10864 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10865 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10866 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10867 /* got data while shutting down */
10868 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
10869 *error = ECONNRESET;
10872 sctp_alloc_a_strmoq(stcb, sp);
10874 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10879 sp->sender_all_done = 0;
10880 sp->sinfo_flags = srcv->sinfo_flags;
10881 sp->timetolive = srcv->sinfo_timetolive;
10882 sp->ppid = srcv->sinfo_ppid;
10883 sp->context = srcv->sinfo_context;
10885 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
10887 sp->stream = srcv->sinfo_stream;
10888 sp->length = min(uio->uio_resid, max_send_len);
10889 if ((sp->length == (uint32_t) uio->uio_resid) &&
10890 ((user_marks_eor == 0) ||
10891 (srcv->sinfo_flags & SCTP_EOF) ||
10892 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
10893 sp->msg_is_complete = 1;
10895 sp->msg_is_complete = 0;
10897 sp->sender_all_done = 0;
10898 sp->some_taken = 0;
10899 sp->put_last_out = 0;
10900 resv_in_first = sizeof(struct sctp_data_chunk);
10901 sp->data = sp->tail_mbuf = NULL;
10902 *error = sctp_copy_one(sp, uio, resv_in_first);
10904 sctp_free_a_strmoq(stcb, sp);
10907 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
10911 sp->net = asoc->primary_destination;
10914 atomic_add_int(&sp->net->ref_count, 1);
10915 sctp_set_prsctp_policy(stcb, sp);
10923 sctp_sosend(struct socket *so,
10924 struct sockaddr *addr,
10927 struct mbuf *control,
10932 struct sctp_inpcb *inp;
10933 int error, use_rcvinfo = 0;
10934 struct sctp_sndrcvinfo srcv;
10936 inp = (struct sctp_inpcb *)so->so_pcb;
10938 /* process cmsg snd/rcv info (maybe a assoc-id) */
10939 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
10945 error = sctp_lower_sosend(so, addr, uio, top,
10956 sctp_lower_sosend(struct socket *so,
10957 struct sockaddr *addr,
10959 struct mbuf *i_pak,
10960 struct mbuf *control,
10963 struct sctp_sndrcvinfo *srcv
10968 unsigned int sndlen = 0, max_len;
10970 struct mbuf *top = NULL;
10971 int queue_only = 0, queue_only_for_init = 0;
10972 int free_cnt_applied = 0;
10974 int now_filled = 0;
10975 unsigned int inqueue_bytes = 0;
10976 struct sctp_block_entry be;
10977 struct sctp_inpcb *inp;
10978 struct sctp_tcb *stcb = NULL;
10979 struct timeval now;
10980 struct sctp_nets *net;
10981 struct sctp_association *asoc;
10982 struct sctp_inpcb *t_inp;
10983 int user_marks_eor;
10984 int create_lock_applied = 0;
10985 int nagle_applies = 0;
10986 int some_on_control = 0;
10987 int got_all_of_the_send = 0;
10988 int hold_tcblock = 0;
10989 int non_blocking = 0;
10990 int temp_flags = 0;
10991 uint32_t local_add_more, local_soresv = 0;
10998 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11000 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11003 SCTP_RELEASE_PKT(i_pak);
11007 if ((uio == NULL) && (i_pak == NULL)) {
11008 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11011 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11012 atomic_add_int(&inp->total_sends, 1);
11014 if (uio->uio_resid < 0) {
11015 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11018 sndlen = uio->uio_resid;
11020 top = SCTP_HEADER_TO_CHAIN(i_pak);
11021 sndlen = SCTP_HEADER_LEN(i_pak);
11023 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11027 * Pre-screen address, if one is given the sin-len
11028 * must be set correctly!
11031 if ((addr->sa_family == AF_INET) &&
11032 (addr->sa_len != sizeof(struct sockaddr_in))) {
11033 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11036 } else if ((addr->sa_family == AF_INET6) &&
11037 (addr->sa_len != sizeof(struct sockaddr_in6))) {
11038 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11045 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11046 (inp->sctp_socket->so_qlimit)) {
11047 /* The listener can NOT send */
11048 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11052 if ((use_rcvinfo) && srcv) {
11053 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11054 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11055 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11059 if (srcv->sinfo_flags)
11060 SCTP_STAT_INCR(sctps_sends_with_flags);
11062 if (srcv->sinfo_flags & SCTP_SENDALL) {
11063 /* its a sendall */
11064 error = sctp_sendall(inp, uio, top, srcv);
11069 /* now we must find the assoc */
11070 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11071 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11072 SCTP_INP_RLOCK(inp);
11073 stcb = LIST_FIRST(&inp->sctp_asoc_list);
11074 if (stcb == NULL) {
11075 SCTP_INP_RUNLOCK(inp);
11076 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11081 SCTP_INP_RUNLOCK(inp);
11083 /* Must locate the net structure if addr given */
11084 net = sctp_findnet(stcb, addr);
11086 /* validate port was 0 or correct */
11087 struct sockaddr_in *sin;
11089 sin = (struct sockaddr_in *)addr;
11090 if ((sin->sin_port != 0) &&
11091 (sin->sin_port != stcb->rport)) {
11095 temp_flags |= SCTP_ADDR_OVER;
11097 net = stcb->asoc.primary_destination;
11098 if (addr && (net == NULL)) {
11099 /* Could not find address, was it legal */
11100 if (addr->sa_family == AF_INET) {
11101 struct sockaddr_in *sin;
11103 sin = (struct sockaddr_in *)addr;
11104 if (sin->sin_addr.s_addr == 0) {
11105 if ((sin->sin_port == 0) ||
11106 (sin->sin_port == stcb->rport)) {
11107 net = stcb->asoc.primary_destination;
11111 struct sockaddr_in6 *sin6;
11113 sin6 = (struct sockaddr_in6 *)addr;
11114 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11115 if ((sin6->sin6_port == 0) ||
11116 (sin6->sin6_port == stcb->rport)) {
11117 net = stcb->asoc.primary_destination;
11123 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11127 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11128 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11132 * Must locate the net structure if addr
11135 net = sctp_findnet(stcb, addr);
11137 net = stcb->asoc.primary_destination;
11138 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11139 ((net == NULL) || (addr == NULL))) {
11140 struct sockaddr_in *sin;
11142 if (addr == NULL) {
11143 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11147 sin = (struct sockaddr_in *)addr;
11148 /* Validate port is 0 or correct */
11149 if ((sin->sin_port != 0) &&
11150 (sin->sin_port != stcb->rport)) {
11158 * Since we did not use findep we must
11159 * increment it, and if we don't find a tcb
11162 SCTP_INP_WLOCK(inp);
11163 SCTP_INP_INCR_REF(inp);
11164 SCTP_INP_WUNLOCK(inp);
11165 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11166 if (stcb == NULL) {
11167 SCTP_INP_WLOCK(inp);
11168 SCTP_INP_DECR_REF(inp);
11169 SCTP_INP_WUNLOCK(inp);
11174 if ((stcb == NULL) && (addr)) {
11175 /* Possible implicit send? */
11176 SCTP_ASOC_CREATE_LOCK(inp);
11177 create_lock_applied = 1;
11178 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11179 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11180 /* Should I really unlock ? */
11181 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11186 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11187 (addr->sa_family == AF_INET6)) {
11188 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11192 SCTP_INP_WLOCK(inp);
11193 SCTP_INP_INCR_REF(inp);
11194 SCTP_INP_WUNLOCK(inp);
11195 /* With the lock applied look again */
11196 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11197 if (stcb == NULL) {
11198 SCTP_INP_WLOCK(inp);
11199 SCTP_INP_DECR_REF(inp);
11200 SCTP_INP_WUNLOCK(inp);
11204 if (t_inp != inp) {
11205 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11210 if (stcb == NULL) {
11211 if (addr == NULL) {
11212 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
11217 * UDP style, we must go ahead and start the INIT
11222 if ((use_rcvinfo) && (srcv) &&
11223 ((srcv->sinfo_flags & SCTP_ABORT) ||
11224 ((srcv->sinfo_flags & SCTP_EOF) &&
11227 * User asks to abort a non-existant assoc,
11228 * or EOF a non-existant assoc with no data
11230 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
11234 /* get an asoc/stcb struct */
11235 vrf_id = inp->def_vrf_id;
11237 if (create_lock_applied == 0) {
11238 panic("Error, should hold create lock and I don't?");
11241 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
11244 if (stcb == NULL) {
11245 /* Error is setup for us in the call */
11248 if (create_lock_applied) {
11249 SCTP_ASOC_CREATE_UNLOCK(inp);
11250 create_lock_applied = 0;
11252 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
11255 * Turn on queue only flag to prevent data from
11259 asoc = &stcb->asoc;
11260 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
11261 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
11263 /* initialize authentication params for the assoc */
11264 sctp_initialize_auth_params(inp, stcb);
11268 * see if a init structure exists in cmsg
11271 struct sctp_initmsg initm;
11274 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
11277 * we have an INIT override of the
11280 if (initm.sinit_max_attempts)
11281 asoc->max_init_times = initm.sinit_max_attempts;
11282 if (initm.sinit_num_ostreams)
11283 asoc->pre_open_streams = initm.sinit_num_ostreams;
11284 if (initm.sinit_max_instreams)
11285 asoc->max_inbound_streams = initm.sinit_max_instreams;
11286 if (initm.sinit_max_init_timeo)
11287 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
11288 if (asoc->streamoutcnt < asoc->pre_open_streams) {
11289 /* Default is NOT correct */
11290 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
11291 asoc->streamoutcnt, asoc->pre_open_streams);
11293 * What happens if this
11294 * fails? we panic ...
11297 struct sctp_stream_out *tmp_str;
11300 if (hold_tcblock) {
11302 SCTP_TCB_UNLOCK(stcb);
11304 SCTP_MALLOC(tmp_str,
11305 struct sctp_stream_out *,
11306 (asoc->pre_open_streams *
11307 sizeof(struct sctp_stream_out)),
11310 SCTP_TCB_LOCK(stcb);
11312 if (tmp_str != NULL) {
11313 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
11314 asoc->strmout = tmp_str;
11315 asoc->streamoutcnt = asoc->pre_open_streams;
11317 asoc->pre_open_streams = asoc->streamoutcnt;
11320 for (i = 0; i < asoc->streamoutcnt; i++) {
11322 * inbound side must be set
11323 * to 0xffff, also NOTE when
11324 * we get the INIT-ACK back
11325 * (for INIT sender) we MUST
11327 * (streamoutcnt) but first
11328 * check if we sent to any
11329 * of the upper streams that
11330 * were dropped (if some
11331 * were). Those that were
11332 * dropped must be notified
11333 * to the upper layer as
11336 asoc->strmout[i].next_sequence_sent = 0x0;
11337 TAILQ_INIT(&asoc->strmout[i].outqueue);
11338 asoc->strmout[i].stream_no = i;
11339 asoc->strmout[i].last_msg_incomplete = 0;
11340 asoc->strmout[i].next_spoke.tqe_next = 0;
11341 asoc->strmout[i].next_spoke.tqe_prev = 0;
11347 /* out with the INIT */
11348 queue_only_for_init = 1;
11350 * we may want to dig in after this call and adjust the MTU
11351 * value. It defaulted to 1500 (constant) but the ro
11352 * structure may now have an update and thus we may need to
11353 * change it BEFORE we append the message.
11355 net = stcb->asoc.primary_destination;
11356 asoc = &stcb->asoc;
11359 if ((SCTP_SO_IS_NBIO(so)
11360 || (flags & MSG_NBIO)
11364 asoc = &stcb->asoc;
11366 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
11367 if (sndlen > asoc->smallest_mtu) {
11368 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11373 /* would we block? */
11374 if (non_blocking) {
11375 if (hold_tcblock == 0) {
11376 SCTP_TCB_LOCK(stcb);
11379 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11380 if ((SCTP_SB_LIMIT_SND(so) <
11381 (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
11382 (stcb->asoc.chunks_on_out_queue >
11383 sctp_max_chunks_on_queue)) {
11384 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
11385 if (sndlen > SCTP_SB_LIMIT_SND(so))
11388 error = EWOULDBLOCK;
11391 stcb->asoc.sb_send_resv += sndlen;
11392 SCTP_TCB_UNLOCK(stcb);
11395 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
11397 local_soresv = sndlen;
11398 /* Keep the stcb from being freed under our feet */
11399 if (free_cnt_applied) {
11401 panic("refcnt already incremented");
11403 printf("refcnt:1 already incremented?\n");
11406 atomic_add_int(&stcb->asoc.refcnt, 1);
11407 free_cnt_applied = 1;
11409 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11410 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11411 error = ECONNRESET;
11414 if (create_lock_applied) {
11415 SCTP_ASOC_CREATE_UNLOCK(inp);
11416 create_lock_applied = 0;
11418 if (asoc->stream_reset_outstanding) {
11420 * Can't queue any data while stream reset is underway.
11422 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
11426 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11427 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11430 if ((use_rcvinfo == 0) || (srcv == NULL)) {
11431 /* Grab the default stuff from the asoc */
11432 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
11434 /* we are now done with all control */
11436 sctp_m_freem(control);
11439 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11440 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11441 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11442 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11443 if ((use_rcvinfo) &&
11444 (srcv->sinfo_flags & SCTP_ABORT)) {
11447 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11448 error = ECONNRESET;
11452 /* Ok, we will attempt a msgsnd :> */
11454 p->td_ru.ru_msgsnd++;
11457 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
11458 net = stcb->asoc.primary_destination;
11462 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11466 if ((net->flight_size > net->cwnd) && (sctp_cmt_on_off == 0)) {
11468 * CMT: Added check for CMT above. net above is the primary
11469 * dest. If CMT is ON, sender should always attempt to send
11470 * with the output routine sctp_fill_outqueue() that loops
11471 * through all destination addresses. Therefore, if CMT is
11472 * ON, queue_only is NOT set to 1 here, so that
11473 * sctp_chunk_output() can be called below.
11477 } else if (asoc->ifp_had_enobuf) {
11478 SCTP_STAT_INCR(sctps_ifnomemqueued);
11479 if (net->flight_size > (net->mtu * 2))
11481 asoc->ifp_had_enobuf = 0;
11483 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11484 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
11486 /* Are we aborting? */
11487 if (srcv->sinfo_flags & SCTP_ABORT) {
11489 int tot_demand, tot_out = 0, max_out;
11491 SCTP_STAT_INCR(sctps_sends_with_abort);
11492 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11493 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11494 /* It has to be up before we abort */
11495 /* how big is the user initiated abort? */
11496 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11500 if (hold_tcblock) {
11501 SCTP_TCB_UNLOCK(stcb);
11505 struct mbuf *cntm = NULL;
11507 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
11511 tot_out += SCTP_BUF_LEN(cntm);
11512 cntm = SCTP_BUF_NEXT(cntm);
11515 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11517 /* Must fit in a MTU */
11519 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11520 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
11522 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11526 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
11529 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11533 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
11534 max_out -= sizeof(struct sctp_abort_msg);
11535 if (tot_out > max_out) {
11539 struct sctp_paramhdr *ph;
11541 /* now move forward the data pointer */
11542 ph = mtod(mm, struct sctp_paramhdr *);
11543 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
11544 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
11546 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
11548 error = uiomove((caddr_t)ph, (int)tot_out, uio);
11551 * Here if we can't get his data we
11552 * still abort we just don't get to
11553 * send the users note :-0
11560 SCTP_BUF_NEXT(mm) = top;
11564 if (hold_tcblock == 0) {
11565 SCTP_TCB_LOCK(stcb);
11568 atomic_add_int(&stcb->asoc.refcnt, -1);
11569 free_cnt_applied = 0;
11570 /* release this lock, otherwise we hang on ourselves */
11571 sctp_abort_an_association(stcb->sctp_ep, stcb,
11572 SCTP_RESPONSE_TO_USER_REQ,
11573 mm, SCTP_SO_LOCKED);
11574 /* now relock the stcb so everything is sane */
11578 * In this case top is already chained to mm avoid double
11579 * free, since we free it below if top != NULL and driver
11580 * would free it after sending the packet out
11587 /* Calculate the maximum we can send */
11588 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11589 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
11590 if (non_blocking) {
11591 /* we already checked for non-blocking above. */
11594 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11599 if (hold_tcblock) {
11600 SCTP_TCB_UNLOCK(stcb);
11603 /* Is the stream no. valid? */
11604 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
11605 /* Invalid stream number */
11606 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11610 if (asoc->strmout == NULL) {
11611 /* huh? software error */
11612 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11616 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
11617 if ((user_marks_eor == 0) &&
11618 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
11619 /* It will NEVER fit */
11620 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
11624 if ((uio == NULL) && user_marks_eor) {
11626 * We do not support eeor mode for
11627 * sending with mbuf chains (like sendfile).
11629 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11633 if (user_marks_eor) {
11634 local_add_more = sctp_add_more_threshold;
11637 * For non-eeor the whole message must fit in
11638 * the socket send buffer.
11640 local_add_more = sndlen;
11643 if (non_blocking) {
11644 goto skip_preblock;
11646 if (((max_len <= local_add_more) &&
11647 (SCTP_SB_LIMIT_SND(so) > local_add_more)) ||
11648 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) > sctp_max_chunks_on_queue)) { /* if */
11649 /* No room right no ! */
11650 SOCKBUF_LOCK(&so->so_snd);
11651 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11652 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + sctp_add_more_threshold)) ||
11653 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) > sctp_max_chunks_on_queue /* while */ )) {
11655 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11656 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
11660 stcb->block_entry = &be;
11661 error = sbwait(&so->so_snd);
11662 stcb->block_entry = NULL;
11663 if (error || so->so_error || be.error) {
11666 error = so->so_error;
11671 SOCKBUF_UNLOCK(&so->so_snd);
11674 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11675 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
11676 so, asoc, stcb->asoc.total_output_queue_size);
11678 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11681 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11683 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11684 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
11685 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
11689 SOCKBUF_UNLOCK(&so->so_snd);
11692 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11695 atomic_add_int(&stcb->total_sends, 1);
11697 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
11698 * case NOTE: uio will be null when top/mbuf is passed
11701 if (srcv->sinfo_flags & SCTP_EOF) {
11702 got_all_of_the_send = 1;
11705 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11711 struct sctp_stream_queue_pending *sp;
11712 struct sctp_stream_out *strm;
11713 uint32_t sndout, initial_out;
11715 initial_out = uio->uio_resid;
11717 SCTP_TCB_SEND_LOCK(stcb);
11718 if ((asoc->stream_locked) &&
11719 (asoc->stream_locked_on != srcv->sinfo_stream)) {
11720 SCTP_TCB_SEND_UNLOCK(stcb);
11721 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11725 SCTP_TCB_SEND_UNLOCK(stcb);
11727 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
11728 if (strm->last_msg_incomplete == 0) {
11730 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
11731 if ((sp == NULL) || (error)) {
11734 SCTP_TCB_SEND_LOCK(stcb);
11735 if (sp->msg_is_complete) {
11736 strm->last_msg_incomplete = 0;
11737 asoc->stream_locked = 0;
11740 * Just got locked to this guy in case of an
11743 strm->last_msg_incomplete = 1;
11744 asoc->stream_locked = 1;
11745 asoc->stream_locked_on = srcv->sinfo_stream;
11746 sp->sender_all_done = 0;
11748 sctp_snd_sb_alloc(stcb, sp->length);
11749 atomic_add_int(&asoc->stream_queue_cnt, 1);
11750 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
11751 sp->strseq = strm->next_sequence_sent;
11752 if (sctp_logging_level & SCTP_LOG_AT_SEND_2_SCTP) {
11753 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
11754 (uintptr_t) stcb, sp->length,
11755 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
11757 strm->next_sequence_sent++;
11759 SCTP_STAT_INCR(sctps_sends_with_unord);
11761 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
11762 if ((strm->next_spoke.tqe_next == NULL) &&
11763 (strm->next_spoke.tqe_prev == NULL)) {
11764 /* Not on wheel, insert */
11765 sctp_insert_on_wheel(stcb, asoc, strm, 1);
11767 SCTP_TCB_SEND_UNLOCK(stcb);
11769 SCTP_TCB_SEND_LOCK(stcb);
11770 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
11771 SCTP_TCB_SEND_UNLOCK(stcb);
11773 /* ???? Huh ??? last msg is gone */
11775 panic("Warning: Last msg marked incomplete, yet nothing left?");
11777 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
11778 strm->last_msg_incomplete = 0;
11784 while (uio->uio_resid > 0) {
11785 /* How much room do we have? */
11786 struct mbuf *new_tail, *mm;
11788 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
11789 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11793 if ((max_len > sctp_add_more_threshold) ||
11794 (max_len && (SCTP_SB_LIMIT_SND(so) < sctp_add_more_threshold)) ||
11796 (uio->uio_resid <= (int)max_len))) {
11799 if (hold_tcblock) {
11800 SCTP_TCB_UNLOCK(stcb);
11803 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
11804 if ((mm == NULL) || error) {
11810 /* Update the mbuf and count */
11811 SCTP_TCB_SEND_LOCK(stcb);
11812 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11814 * we need to get out. Peer probably
11818 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
11819 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11820 error = ECONNRESET;
11822 SCTP_TCB_SEND_UNLOCK(stcb);
11825 if (sp->tail_mbuf) {
11826 /* tack it to the end */
11827 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
11828 sp->tail_mbuf = new_tail;
11830 /* A stolen mbuf */
11832 sp->tail_mbuf = new_tail;
11834 sctp_snd_sb_alloc(stcb, sndout);
11835 atomic_add_int(&sp->length, sndout);
11838 /* Did we reach EOR? */
11839 if ((uio->uio_resid == 0) &&
11840 ((user_marks_eor == 0) ||
11841 (srcv->sinfo_flags & SCTP_EOF) ||
11842 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
11844 sp->msg_is_complete = 1;
11846 sp->msg_is_complete = 0;
11848 SCTP_TCB_SEND_UNLOCK(stcb);
11850 if (uio->uio_resid == 0) {
11855 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
11857 * This is ugly but we must assure locking
11860 if (hold_tcblock == 0) {
11861 SCTP_TCB_LOCK(stcb);
11864 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
11865 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
11866 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
11867 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
11873 SCTP_TCB_UNLOCK(stcb);
11876 /* wait for space now */
11877 if (non_blocking) {
11878 /* Non-blocking io in place out */
11881 if ((net->flight_size > net->cwnd) &&
11882 (sctp_cmt_on_off == 0)) {
11884 } else if (asoc->ifp_had_enobuf) {
11885 SCTP_STAT_INCR(sctps_ifnomemqueued);
11886 if (net->flight_size > (net->mtu * 2)) {
11891 asoc->ifp_had_enobuf = 0;
11892 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11893 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11894 sizeof(struct sctp_data_chunk)));
11896 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11897 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11898 sizeof(struct sctp_data_chunk)));
11899 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
11901 SCTP_STAT_INCR(sctps_send_burst_avoid);
11902 } else if (net->flight_size > net->cwnd) {
11904 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
11909 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
11910 (stcb->asoc.total_flight > 0) &&
11911 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
11912 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
11916 * Ok, Nagle is set on and we have data outstanding.
11917 * Don't send anything and let SACKs drive out the
11918 * data unless wen have a "full" segment to send.
11920 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11921 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
11923 SCTP_STAT_INCR(sctps_naglequeued);
11926 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11927 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
11928 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
11930 SCTP_STAT_INCR(sctps_naglesent);
11933 /* What about the INIT, send it maybe */
11934 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11936 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
11937 nagle_applies, un_sent);
11938 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
11939 stcb->asoc.total_flight,
11940 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
11942 if (queue_only_for_init) {
11943 if (hold_tcblock == 0) {
11944 SCTP_TCB_LOCK(stcb);
11947 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
11948 /* a collision took us forward? */
11949 queue_only_for_init = 0;
11952 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
11953 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
11954 queue_only_for_init = 0;
11958 if ((queue_only == 0) && (nagle_applies == 0)
11961 * need to start chunk output
11962 * before blocking.. note that if
11963 * a lock is already applied, then
11964 * the input via the net is happening
11965 * and I don't need to start output :-D
11967 if (hold_tcblock == 0) {
11968 if (SCTP_TCB_TRYLOCK(stcb)) {
11970 sctp_chunk_output(inp,
11972 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
11975 sctp_chunk_output(inp,
11977 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
11979 if (hold_tcblock == 1) {
11980 SCTP_TCB_UNLOCK(stcb);
11984 SOCKBUF_LOCK(&so->so_snd);
11986 * This is a bit strange, but I think it will
11987 * work. The total_output_queue_size is locked and
11988 * protected by the TCB_LOCK, which we just released.
11989 * There is a race that can occur between releasing it
11990 * above, and me getting the socket lock, where sacks
11991 * come in but we have not put the SB_WAIT on the
11992 * so_snd buffer to get the wakeup. After the LOCK
11993 * is applied the sack_processing will also need to
11994 * LOCK the so->so_snd to do the actual sowwakeup(). So
11995 * once we have the socket buffer lock if we recheck the
11996 * size we KNOW we will get to sleep safely with the
11997 * wakeup flag in place.
11999 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12000 min(sctp_add_more_threshold, SCTP_SB_LIMIT_SND(so)))
12002 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
12003 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12004 so, asoc, uio->uio_resid);
12007 stcb->block_entry = &be;
12008 error = sbwait(&so->so_snd);
12009 stcb->block_entry = NULL;
12011 if (error || so->so_error || be.error) {
12014 error = so->so_error;
12019 SOCKBUF_UNLOCK(&so->so_snd);
12022 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
12023 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12024 so, asoc, stcb->asoc.total_output_queue_size);
12027 SOCKBUF_UNLOCK(&so->so_snd);
12028 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12032 SCTP_TCB_SEND_LOCK(stcb);
12034 if (sp->msg_is_complete == 0) {
12035 strm->last_msg_incomplete = 1;
12036 asoc->stream_locked = 1;
12037 asoc->stream_locked_on = srcv->sinfo_stream;
12039 sp->sender_all_done = 1;
12040 strm->last_msg_incomplete = 0;
12041 asoc->stream_locked = 0;
12044 SCTP_PRINTF("Huh no sp TSNH?\n");
12045 strm->last_msg_incomplete = 0;
12046 asoc->stream_locked = 0;
12048 SCTP_TCB_SEND_UNLOCK(stcb);
12049 if (uio->uio_resid == 0) {
12050 got_all_of_the_send = 1;
12053 /* We send in a 0, since we do NOT have any locks */
12054 error = sctp_msg_append(stcb, net, top, srcv, 0);
12056 if (srcv->sinfo_flags & SCTP_EOF) {
12058 * This should only happen for Panda for the mbuf
12059 * send case, which does NOT yet support EEOR mode.
12060 * Thus, we can just set this flag to do the proper
12063 got_all_of_the_send = 1;
12071 if ((srcv->sinfo_flags & SCTP_EOF) &&
12072 (got_all_of_the_send == 1) &&
12073 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12077 SCTP_STAT_INCR(sctps_sends_with_eof);
12079 if (hold_tcblock == 0) {
12080 SCTP_TCB_LOCK(stcb);
12083 cnt = sctp_is_there_unsent_data(stcb);
12084 if (TAILQ_EMPTY(&asoc->send_queue) &&
12085 TAILQ_EMPTY(&asoc->sent_queue) &&
12087 if (asoc->locked_on_sending) {
12090 /* there is nothing queued to send, so I'm done... */
12091 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12092 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12093 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12094 /* only send SHUTDOWN the first time through */
12095 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12096 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12097 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12099 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12100 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12101 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12102 asoc->primary_destination);
12103 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12104 asoc->primary_destination);
12108 * we still got (or just got) data to send, so set
12112 * XXX sockets draft says that SCTP_EOF should be
12113 * sent with no data. currently, we will allow user
12114 * data to be sent first and move to
12117 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12118 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12119 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12120 if (hold_tcblock == 0) {
12121 SCTP_TCB_LOCK(stcb);
12124 if (asoc->locked_on_sending) {
12125 /* Locked to send out the data */
12126 struct sctp_stream_queue_pending *sp;
12128 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12130 if ((sp->length == 0) && (sp->msg_is_complete == 0))
12131 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12134 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12135 if (TAILQ_EMPTY(&asoc->send_queue) &&
12136 TAILQ_EMPTY(&asoc->sent_queue) &&
12137 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12139 if (free_cnt_applied) {
12140 atomic_add_int(&stcb->asoc.refcnt, -1);
12141 free_cnt_applied = 0;
12143 sctp_abort_an_association(stcb->sctp_ep, stcb,
12144 SCTP_RESPONSE_TO_USER_REQ,
12145 NULL, SCTP_SO_LOCKED);
12147 * now relock the stcb so everything
12154 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12155 asoc->primary_destination);
12156 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12161 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12162 some_on_control = 1;
12164 if ((net->flight_size > net->cwnd) &&
12165 (sctp_cmt_on_off == 0)) {
12167 } else if (asoc->ifp_had_enobuf) {
12168 SCTP_STAT_INCR(sctps_ifnomemqueued);
12169 if (net->flight_size > (net->mtu * 2)) {
12174 asoc->ifp_had_enobuf = 0;
12175 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12176 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12177 sizeof(struct sctp_data_chunk)));
12179 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12180 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
12181 sizeof(struct sctp_data_chunk)));
12182 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12184 SCTP_STAT_INCR(sctps_send_burst_avoid);
12185 } else if (net->flight_size > net->cwnd) {
12187 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12192 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12193 (stcb->asoc.total_flight > 0) &&
12194 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12195 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12198 * Ok, Nagle is set on and we have data outstanding.
12199 * Don't send anything and let SACKs drive out the
12200 * data unless wen have a "full" segment to send.
12202 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12203 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12205 SCTP_STAT_INCR(sctps_naglequeued);
12208 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
12209 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12210 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12212 SCTP_STAT_INCR(sctps_naglesent);
12215 if (queue_only_for_init) {
12216 if (hold_tcblock == 0) {
12217 SCTP_TCB_LOCK(stcb);
12220 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12221 /* a collision took us forward? */
12222 queue_only_for_init = 0;
12225 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12226 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
12227 queue_only_for_init = 0;
12231 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
12232 /* we can attempt to send too. */
12233 if (hold_tcblock == 0) {
12235 * If there is activity recv'ing sacks no need to
12238 if (SCTP_TCB_TRYLOCK(stcb)) {
12239 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12243 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12245 } else if ((queue_only == 0) &&
12246 (stcb->asoc.peers_rwnd == 0) &&
12247 (stcb->asoc.total_flight == 0)) {
12248 /* We get to have a probe outstanding */
12249 if (hold_tcblock == 0) {
12251 SCTP_TCB_LOCK(stcb);
12253 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12254 } else if (some_on_control) {
12255 int num_out, reason, cwnd_full, frag_point;
12257 /* Here we do control only */
12258 if (hold_tcblock == 0) {
12260 SCTP_TCB_LOCK(stcb);
12262 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
12263 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
12264 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
12266 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d",
12267 queue_only, stcb->asoc.peers_rwnd, un_sent,
12268 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
12269 stcb->asoc.total_output_queue_size, error);
12274 if (local_soresv && stcb) {
12275 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
12278 if (create_lock_applied) {
12279 SCTP_ASOC_CREATE_UNLOCK(inp);
12280 create_lock_applied = 0;
12282 if ((stcb) && hold_tcblock) {
12283 SCTP_TCB_UNLOCK(stcb);
12285 if (stcb && free_cnt_applied) {
12286 atomic_add_int(&stcb->asoc.refcnt, -1);
12290 if (mtx_owned(&stcb->tcb_mtx)) {
12291 panic("Leaving with tcb mtx owned?");
12293 if (mtx_owned(&stcb->tcb_send_mtx)) {
12294 panic("Leaving with tcb send mtx owned?");
12302 sctp_m_freem(control);
12309 * generate an AUTHentication chunk, if required
12312 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
12313 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
12314 struct sctp_tcb *stcb, uint8_t chunk)
12316 struct mbuf *m_auth;
12317 struct sctp_auth_chunk *auth;
12320 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
12324 /* sysctl disabled auth? */
12325 if (sctp_auth_disable)
12328 /* peer doesn't do auth... */
12329 if (!stcb->asoc.peer_supports_auth) {
12332 /* does the requested chunk require auth? */
12333 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
12336 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
12337 if (m_auth == NULL) {
12341 /* reserve some space if this will be the first mbuf */
12343 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
12344 /* fill in the AUTH chunk details */
12345 auth = mtod(m_auth, struct sctp_auth_chunk *);
12346 bzero(auth, sizeof(*auth));
12347 auth->ch.chunk_type = SCTP_AUTHENTICATION;
12348 auth->ch.chunk_flags = 0;
12349 chunk_len = sizeof(*auth) +
12350 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
12351 auth->ch.chunk_length = htons(chunk_len);
12352 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
12353 /* key id and hmac digest will be computed and filled in upon send */
12355 /* save the offset where the auth was inserted into the chain */
12362 *offset += SCTP_BUF_LEN(cn);
12363 cn = SCTP_BUF_NEXT(cn);
12368 /* update length and return pointer to the auth chunk */
12369 SCTP_BUF_LEN(m_auth) = chunk_len;
12370 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
12371 if (auth_ret != NULL)
12378 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
12380 struct nd_prefix *pfx = NULL;
12381 struct nd_pfxrouter *pfxrtr = NULL;
12382 struct sockaddr_in6 gw6;
12384 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
12387 /* get prefix entry of address */
12388 LIST_FOREACH(pfx, &nd_prefix, ndpr_entry) {
12389 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
12391 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
12392 &src6->sin6_addr, &pfx->ndpr_mask))
12395 /* no prefix entry in the prefix list */
12397 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
12398 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
12401 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
12402 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
12404 /* search installed gateway from prefix entry */
12405 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
12406 pfxrtr->pfr_next) {
12407 memset(&gw6, 0, sizeof(struct sockaddr_in6));
12408 gw6.sin6_family = AF_INET6;
12409 gw6.sin6_len = sizeof(struct sockaddr_in6);
12410 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
12411 sizeof(struct in6_addr));
12412 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
12413 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
12414 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
12415 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
12416 if (sctp_cmpaddr((struct sockaddr *)&gw6,
12417 ro->ro_rt->rt_gateway)) {
12418 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
12422 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
12426 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
12428 struct sockaddr_in *sin, *mask;
12429 struct ifaddr *ifa;
12430 struct in_addr srcnetaddr, gwnetaddr;
12432 if (ro == NULL || ro->ro_rt == NULL ||
12433 sifa->address.sa.sa_family != AF_INET) {
12436 ifa = (struct ifaddr *)sifa->ifa;
12437 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
12438 sin = (struct sockaddr_in *)&sifa->address.sin;
12439 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
12440 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
12441 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
12442 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
12444 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
12445 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
12446 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
12447 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
12448 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
12449 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {