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_LOCK();
1997 vrf = sctp_find_vrf(vrf_id);
1999 SCTP_IPI_ADDR_UNLOCK();
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_UNLOCK();
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 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2419 atomic_add_int(&sifa->refcount, 1);
2422 if (resettotop == 0) {
2423 inp->next_addr_touse = NULL;
2426 inp->next_addr_touse = starting_point;
2429 if (inp->next_addr_touse == NULL) {
2430 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2433 /* ok, what about an acceptable address in the inp */
2434 for (laddr = inp->next_addr_touse; laddr;
2435 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2436 if (laddr->ifa == NULL) {
2437 /* address has been removed */
2440 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2444 atomic_add_int(&sifa->refcount, 1);
2447 if (resettotop == 0) {
2448 inp->next_addr_touse = NULL;
2449 goto once_again_too;
2452 * no address bound can be a source for the destination we are in
2460 static struct sctp_ifa *
2461 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2462 struct sctp_tcb *stcb,
2463 struct sctp_nets *net,
2466 uint8_t dest_is_priv,
2467 uint8_t dest_is_loop,
2468 int non_asoc_addr_ok,
2471 struct sctp_laddr *laddr, *starting_point;
2473 struct sctp_ifn *sctp_ifn;
2474 struct sctp_ifa *sctp_ifa, *sifa;
2475 uint8_t start_at_beginning = 0;
2476 struct sctp_vrf *vrf;
2480 * first question, is the ifn we will emit on in our list, if so, we
2483 vrf = sctp_find_vrf(vrf_id);
2487 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2488 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2489 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2492 * first question, is the ifn we will emit on in our list? If so,
2493 * we want that one. First we look for a preferred. Second, we go
2494 * for an acceptable.
2497 /* first try for a preferred address on the ep */
2498 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2499 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2501 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2502 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2505 if ((non_asoc_addr_ok == 0) &&
2506 (sctp_is_addr_restricted(stcb, sifa))) {
2507 /* on the no-no list */
2510 atomic_add_int(&sifa->refcount, 1);
2514 /* next try for an acceptable address on the ep */
2515 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2516 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2518 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2519 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2522 if ((non_asoc_addr_ok == 0) &&
2523 (sctp_is_addr_restricted(stcb, sifa))) {
2524 /* on the no-no list */
2527 atomic_add_int(&sifa->refcount, 1);
2534 * if we can't find one like that then we must look at all addresses
2535 * bound to pick one at first preferable then secondly acceptable.
2537 starting_point = stcb->asoc.last_used_address;
2539 if (stcb->asoc.last_used_address == NULL) {
2540 start_at_beginning = 1;
2541 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2543 /* search beginning with the last used address */
2544 for (laddr = stcb->asoc.last_used_address; laddr;
2545 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2546 if (laddr->ifa == NULL) {
2547 /* address has been removed */
2550 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2553 if ((non_asoc_addr_ok == 0) &&
2554 (sctp_is_addr_restricted(stcb, sifa))) {
2555 /* on the no-no list */
2558 stcb->asoc.last_used_address = laddr;
2559 atomic_add_int(&sifa->refcount, 1);
2563 if (start_at_beginning == 0) {
2564 stcb->asoc.last_used_address = NULL;
2565 goto sctp_from_the_top;
2567 /* now try for any higher scope than the destination */
2568 stcb->asoc.last_used_address = starting_point;
2569 start_at_beginning = 0;
2571 if (stcb->asoc.last_used_address == NULL) {
2572 start_at_beginning = 1;
2573 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2575 /* search beginning with the last used address */
2576 for (laddr = stcb->asoc.last_used_address; laddr;
2577 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2578 if (laddr->ifa == NULL) {
2579 /* address has been removed */
2582 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2586 if ((non_asoc_addr_ok == 0) &&
2587 (sctp_is_addr_restricted(stcb, sifa))) {
2588 /* on the no-no list */
2591 stcb->asoc.last_used_address = laddr;
2592 atomic_add_int(&sifa->refcount, 1);
2595 if (start_at_beginning == 0) {
2596 stcb->asoc.last_used_address = NULL;
2597 goto sctp_from_the_top2;
2602 static struct sctp_ifa *
2603 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2604 struct sctp_tcb *stcb,
2605 int non_asoc_addr_ok,
2606 uint8_t dest_is_loop,
2607 uint8_t dest_is_priv,
2611 struct sctp_ifa *ifa, *sifa;
2612 int num_eligible_addr = 0;
2614 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2615 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2616 (non_asoc_addr_ok == 0))
2618 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2623 if ((non_asoc_addr_ok == 0) &&
2624 sctp_is_addr_restricted(stcb, sifa)) {
2626 * It is restricted for some reason..
2627 * probably not yet added.
2632 if (num_eligible_addr >= addr_wanted) {
2635 num_eligible_addr++;
2642 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2643 struct sctp_tcb *stcb,
2644 int non_asoc_addr_ok,
2645 uint8_t dest_is_loop,
2646 uint8_t dest_is_priv,
2649 struct sctp_ifa *ifa, *sifa;
2650 int num_eligible_addr = 0;
2652 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2653 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2654 (non_asoc_addr_ok == 0)) {
2657 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2663 if ((non_asoc_addr_ok == 0) &&
2664 sctp_is_addr_restricted(stcb, sifa)) {
2666 * It is restricted for some reason..
2667 * probably not yet added.
2672 num_eligible_addr++;
2674 return (num_eligible_addr);
2677 static struct sctp_ifa *
2678 sctp_choose_boundall(struct sctp_inpcb *inp,
2679 struct sctp_tcb *stcb,
2680 struct sctp_nets *net,
2683 uint8_t dest_is_priv,
2684 uint8_t dest_is_loop,
2685 int non_asoc_addr_ok,
2688 int cur_addr_num = 0, num_preferred = 0;
2690 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2691 struct sctp_ifa *sctp_ifa, *sifa;
2693 struct sctp_vrf *vrf;
2696 * For boundall we can use any address in the association. If
2697 * non_asoc_addr_ok is set we can use any address (at least in
2698 * theory). So we look for preferred addresses first. If we find
2699 * one, we use it. Otherwise we next try to get an address on the
2700 * interface, which we should be able to do (unless non_asoc_addr_ok
2701 * is false and we are routed out that way). In these cases where we
2702 * can't use the address of the interface we go through all the
2703 * ifn's looking for an address we can use and fill that in. Punting
2704 * means we send back address 0, which will probably cause problems
2705 * actually since then IP will fill in the address of the route ifn,
2706 * which means we probably already rejected it.. i.e. here comes an
2709 vrf = sctp_find_vrf(vrf_id);
2713 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2714 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2716 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2717 if (sctp_ifn == NULL) {
2718 /* ?? We don't have this guy ?? */
2719 goto bound_all_plan_b;
2722 cur_addr_num = net->indx_of_eligible_next_to_use;
2724 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2729 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses\n",
2731 if (num_preferred == 0) {
2733 * no eligible addresses, we must use some other interface
2734 * address if we can find one.
2736 goto bound_all_plan_b;
2739 * Ok we have num_eligible_addr set with how many we can use, this
2740 * may vary from call to call due to addresses being deprecated
2743 if (cur_addr_num >= num_preferred) {
2747 * select the nth address from the list (where cur_addr_num is the
2748 * nth) and 0 is the first one, 1 is the second one etc...
2750 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2752 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2753 dest_is_priv, cur_addr_num, fam);
2755 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2757 atomic_add_int(&sctp_ifa->refcount, 1);
2759 /* save off where the next one we will want */
2760 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2765 * plan_b: Look at all interfaces and find a preferred address. If
2766 * no preferred fall through to plan_c.
2769 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2770 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2771 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2772 /* wrong base scope */
2775 if ((sctp_ifn == looked_at) && looked_at)
2776 /* already looked at this guy */
2778 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2779 dest_is_loop, dest_is_priv, fam);
2780 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2781 "Found ifn:%p %d preferred source addresses\n",
2782 ifn, num_preferred);
2783 if (num_preferred == 0) {
2784 /* None on this interface. */
2787 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2788 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2789 num_preferred, sctp_ifn, cur_addr_num);
2792 * Ok we have num_eligible_addr set with how many we can
2793 * use, this may vary from call to call due to addresses
2794 * being deprecated etc..
2796 if (cur_addr_num >= num_preferred) {
2799 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2800 dest_is_priv, cur_addr_num, fam);
2804 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2805 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2807 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2808 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2809 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2810 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2812 atomic_add_int(&sifa->refcount, 1);
2817 /* plan_c: do we have an acceptable address on the emit interface */
2818 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2819 if (emit_ifn == NULL) {
2822 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2823 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2824 (non_asoc_addr_ok == 0))
2826 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2831 if ((non_asoc_addr_ok == 0) &&
2832 sctp_is_addr_restricted(stcb, sifa)) {
2834 * It is restricted for some reason..
2835 * probably not yet added.
2840 atomic_add_int(&sifa->refcount, 1);
2845 * plan_d: We are in trouble. No preferred address on the emit
2846 * interface. And not even a preferred address on all interfaces. Go
2847 * out and see if we can find an acceptable address somewhere
2848 * amongst all interfaces.
2850 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2851 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2852 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2853 /* wrong base scope */
2856 if ((sctp_ifn == looked_at) && looked_at)
2857 /* already looked at this guy */
2860 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2861 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2862 (non_asoc_addr_ok == 0))
2864 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2870 if ((non_asoc_addr_ok == 0) &&
2871 sctp_is_addr_restricted(stcb, sifa)) {
2873 * It is restricted for some
2874 * reason.. probably not yet added.
2879 atomic_add_int(&sifa->refcount, 1);
2884 * Ok we can find NO address to source from that is not on our
2885 * restricted list and non_asoc_address is NOT ok, or it is on our
2886 * restricted list. We can't source to it :-(
2893 /* tcb may be NULL */
2895 sctp_source_address_selection(struct sctp_inpcb *inp,
2896 struct sctp_tcb *stcb,
2898 struct sctp_nets *net,
2899 int non_asoc_addr_ok, uint32_t vrf_id)
2902 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
2903 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
2904 struct sctp_ifa *answer;
2905 uint8_t dest_is_priv, dest_is_loop;
2909 * Rules: - Find the route if needed, cache if I can. - Look at
2910 * interface address in route, Is it in the bound list. If so we
2911 * have the best source. - If not we must rotate amongst the
2916 * Do we need to pay attention to scope. We can have a private address
2917 * or a global address we are sourcing or sending to. So if we draw
2918 * it out zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2919 * For V4 ------------------------------------------ source *
2920 * dest * result ----------------------------------------- <a>
2921 * Private * Global * NAT
2922 * ----------------------------------------- <b> Private *
2923 * Private * No problem -----------------------------------------
2924 * <c> Global * Private * Huh, How will this work?
2925 * ----------------------------------------- <d> Global *
2926 * Global * No Problem ------------------------------------------
2927 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz For V6
2928 * ------------------------------------------ source * dest *
2929 * result ----------------------------------------- <a> Linklocal *
2930 * Global * ----------------------------------------- <b>
2931 * Linklocal * Linklocal * No problem
2932 * ----------------------------------------- <c> Global *
2933 * Linklocal * Huh, How will this work?
2934 * ----------------------------------------- <d> Global *
2935 * Global * No Problem ------------------------------------------
2936 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2938 * And then we add to that what happens if there are multiple addresses
2939 * assigned to an interface. Remember the ifa on a ifn is a linked
2940 * list of addresses. So one interface can have more than one IP
2941 * address. What happens if we have both a private and a global
2942 * address? Do we then use context of destination to sort out which
2943 * one is best? And what about NAT's sending P->G may get you a NAT
2944 * translation, or should you select the G thats on the interface in
2949 * - count the number of addresses on the interface. - if it is one, no
2950 * problem except case <c>. For <a> we will assume a NAT out there.
2951 * - if there are more than one, then we need to worry about scope P
2952 * or G. We should prefer G -> G and P -> P if possible. Then as a
2953 * secondary fall back to mixed types G->P being a last ditch one. -
2954 * The above all works for bound all, but bound specific we need to
2955 * use the same concept but instead only consider the bound
2956 * addresses. If the bound set is NOT assigned to the interface then
2957 * we must use rotation amongst the bound addresses..
2959 if (ro->ro_rt == NULL) {
2961 * Need a route to cache.
2963 SCTP_RTALLOC(ro, vrf_id);
2965 if (ro->ro_rt == NULL) {
2968 fam = to->sin_family;
2969 dest_is_priv = dest_is_loop = 0;
2970 /* Setup our scopes for the destination */
2971 if (fam == AF_INET) {
2972 /* Scope based on outbound address */
2973 if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
2975 } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
2978 /* mark it as local */
2979 net->addr_is_local = 1;
2982 } else if (fam == AF_INET6) {
2983 /* Scope based on outbound address */
2984 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
2986 * If the route goes to the loopback address OR the
2987 * address is a loopback address, we are loopback
2988 * scope. But we don't use dest_is_priv (link local
2993 /* mark it as local */
2994 net->addr_is_local = 1;
2996 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3000 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3001 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3002 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3006 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3007 dest_is_priv, dest_is_loop,
3008 non_asoc_addr_ok, fam);
3015 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3016 vrf_id, dest_is_priv,
3018 non_asoc_addr_ok, fam);
3020 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3029 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3034 tlen = SCTP_BUF_LEN(control);
3037 * Independent of how many mbufs, find the c_type inside the control
3038 * structure and copy out the data.
3041 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3042 /* not enough room for one more we are done. */
3045 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3046 if (((int)cmh.cmsg_len + at) > tlen) {
3048 * this is real messed up since there is not enough
3049 * data here to cover the cmsg header. We are done.
3053 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3054 (c_type == cmh.cmsg_type)) {
3055 /* found the one we want, copy it out */
3056 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3057 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3059 * space of cmsg_len after header not big
3064 m_copydata(control, at, cpsize, data);
3067 at += CMSG_ALIGN(cmh.cmsg_len);
3068 if (cmh.cmsg_len == 0) {
3077 static struct mbuf *
3078 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3079 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3081 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3082 struct sctp_state_cookie *stc;
3083 struct sctp_paramhdr *ph;
3089 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3090 sizeof(struct sctp_paramhdr)), 0,
3091 M_DONTWAIT, 1, MT_DATA);
3095 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3096 if (copy_init == NULL) {
3100 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3102 if (copy_initack == NULL) {
3104 sctp_m_freem(copy_init);
3107 /* easy side we just drop it on the end */
3108 ph = mtod(mret, struct sctp_paramhdr *);
3109 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3110 sizeof(struct sctp_paramhdr);
3111 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3112 sizeof(struct sctp_paramhdr));
3113 ph->param_type = htons(SCTP_STATE_COOKIE);
3114 ph->param_length = 0; /* fill in at the end */
3115 /* Fill in the stc cookie data */
3116 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3118 /* tack the INIT and then the INIT-ACK onto the chain */
3121 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3122 cookie_sz += SCTP_BUF_LEN(m_at);
3123 if (SCTP_BUF_NEXT(m_at) == NULL) {
3124 SCTP_BUF_NEXT(m_at) = copy_init;
3129 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3130 cookie_sz += SCTP_BUF_LEN(m_at);
3131 if (SCTP_BUF_NEXT(m_at) == NULL) {
3132 SCTP_BUF_NEXT(m_at) = copy_initack;
3137 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3138 cookie_sz += SCTP_BUF_LEN(m_at);
3139 if (SCTP_BUF_NEXT(m_at) == NULL) {
3143 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3145 /* no space, so free the entire chain */
3149 SCTP_BUF_LEN(sig) = 0;
3150 SCTP_BUF_NEXT(m_at) = sig;
3152 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3153 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3155 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3156 cookie_sz += SCTP_SIGNATURE_SIZE;
3157 ph->param_length = htons(cookie_sz);
3163 sctp_get_ect(struct sctp_tcb *stcb,
3164 struct sctp_tmit_chunk *chk)
3166 uint8_t this_random;
3169 if (sctp_ecn_enable == 0)
3172 if (sctp_ecn_nonce == 0)
3173 /* no nonce, always return ECT0 */
3174 return (SCTP_ECT0_BIT);
3176 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3177 /* Peer does NOT support it, so we send a ECT0 only */
3178 return (SCTP_ECT0_BIT);
3181 return (SCTP_ECT0_BIT);
3183 if ((stcb->asoc.hb_random_idx > 3) ||
3184 ((stcb->asoc.hb_random_idx == 3) &&
3185 (stcb->asoc.hb_ect_randombit > 7))) {
3189 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3190 memcpy(stcb->asoc.hb_random_values, &rndval,
3191 sizeof(stcb->asoc.hb_random_values));
3192 this_random = stcb->asoc.hb_random_values[0];
3193 stcb->asoc.hb_random_idx = 0;
3194 stcb->asoc.hb_ect_randombit = 0;
3196 if (stcb->asoc.hb_ect_randombit > 7) {
3197 stcb->asoc.hb_ect_randombit = 0;
3198 stcb->asoc.hb_random_idx++;
3199 if (stcb->asoc.hb_random_idx > 3) {
3203 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3205 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3207 /* ECN Nonce stuff */
3208 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3209 stcb->asoc.hb_ect_randombit++;
3210 return (SCTP_ECT1_BIT);
3212 stcb->asoc.hb_ect_randombit++;
3213 return (SCTP_ECT0_BIT);
3218 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3219 struct sctp_tcb *stcb, /* may be NULL */
3220 struct sctp_nets *net,
3221 struct sockaddr *to,
3223 uint32_t auth_offset,
3224 struct sctp_auth_chunk *auth,
3225 int nofragment_flag,
3227 struct sctp_tmit_chunk *chk,
3229 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3232 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3233 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3234 * structure: - fill in the HMAC digest of any AUTH chunk in the
3235 * packet. - calculate and fill in the SCTP checksum. - prepend an
3236 * IP address header. - if boundall use INADDR_ANY. - if
3237 * boundspecific do source address selection. - set fragmentation
3238 * option for ipV4. - On return from IP output, check/adjust mtu
3239 * size of output interface and smallest_mtu size as well.
3241 /* Will need ifdefs around this */
3244 struct sctphdr *sctphdr;
3249 sctp_route_t *ro = NULL;
3251 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3256 vrf_id = stcb->asoc.vrf_id;
3258 vrf_id = inp->def_vrf_id;
3261 /* fill in the HMAC digest for any AUTH chunk in the packet */
3262 if ((auth != NULL) && (stcb != NULL)) {
3263 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3265 /* Calculate the csum and fill in the length of the packet */
3266 sctphdr = mtod(m, struct sctphdr *);
3267 if (sctp_no_csum_on_loopback &&
3269 (stcb->asoc.loopback_scope)) {
3270 sctphdr->checksum = 0;
3272 * This can probably now be taken out since my audit shows
3273 * no more bad pktlen's coming in. But we will wait a while
3276 packet_length = sctp_calculate_len(m);
3278 sctphdr->checksum = 0;
3279 csum = sctp_calculate_sum(m, &packet_length, 0);
3280 sctphdr->checksum = csum;
3283 if (to->sa_family == AF_INET) {
3284 struct ip *ip = NULL;
3285 sctp_route_t iproute;
3288 newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3291 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
3294 SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3295 SCTP_BUF_LEN(newm) = sizeof(struct ip);
3296 packet_length += sizeof(struct ip);
3297 SCTP_BUF_NEXT(newm) = m;
3299 ip = mtod(m, struct ip *);
3300 ip->ip_v = IPVERSION;
3301 ip->ip_hl = (sizeof(struct ip) >> 2);
3303 tos_value = net->tos_flowlabel & 0x000000ff;
3305 tos_value = inp->ip_inp.inp.inp_ip_tos;
3307 if (nofragment_flag) {
3308 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__)
3311 ip->ip_off = htons(IP_DF);
3316 /* FreeBSD has a function for ip_id's */
3317 ip->ip_id = ip_newid();
3319 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3320 ip->ip_len = packet_length;
3322 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3324 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3327 ip->ip_tos = (u_char)(tos_value & 0xfc);
3330 /* no association at all */
3331 ip->ip_tos = (tos_value & 0xfc);
3333 ip->ip_p = IPPROTO_SCTP;
3337 memset(&iproute, 0, sizeof(iproute));
3338 memcpy(&ro->ro_dst, to, to->sa_len);
3340 ro = (sctp_route_t *) & net->ro;
3342 /* Now the address selection part */
3343 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3345 /* call the routine to select the src address */
3347 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
3348 sctp_free_ifa(net->ro._s_addr);
3349 net->ro._s_addr = NULL;
3350 net->src_addr_selected = 0;
3352 if (net->src_addr_selected == 0) {
3353 if (out_of_asoc_ok) {
3357 /* Cache the source address */
3358 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3359 ro, net, out_of_asoc_ok,
3361 net->src_addr_selected = 1;
3363 if (net->ro._s_addr == NULL) {
3364 /* No route to host */
3365 net->src_addr_selected = 0;
3368 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3370 struct sctp_ifa *_lsrc;
3373 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3377 if (_lsrc == NULL) {
3380 ip->ip_src = _lsrc->address.sin.sin_addr;
3381 sctp_free_ifa(_lsrc);
3385 * If source address selection fails and we find no route
3386 * then the ip_output should fail as well with a
3387 * NO_ROUTE_TO_HOST type error. We probably should catch
3388 * that somewhere and abort the association right away
3389 * (assuming this is an INIT being sent).
3391 if ((ro->ro_rt == NULL)) {
3393 * src addr selection failed to find a route (or
3394 * valid source addr), so we can't get there from
3398 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3399 "%s: dropped packet - no valid source addr\n",
3402 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3403 "Destination was ");
3404 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3405 &net->ro._l_addr.sa);
3406 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3407 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3408 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3409 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3411 SCTP_FAILED_THRESHOLD,
3413 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3414 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3416 * JRS 5/14/07 - If a
3418 * unreachable, the PF bit
3419 * is turned off. This
3420 * allows an unambiguous use
3422 * destinations that are
3423 * reachable but potentially
3425 * destination is set to the
3426 * unreachable state, also
3427 * set the destination to
3431 * Add debug message here if
3432 * destination is not in PF
3436 * Stop any running T3
3439 if (sctp_cmt_on_off && sctp_cmt_pf) {
3440 net->dest_state &= ~SCTP_ADDR_PF;
3441 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3447 if (net == stcb->asoc.primary_destination) {
3448 /* need a new primary */
3449 struct sctp_nets *alt;
3451 alt = sctp_find_alternate_net(stcb, net, 0);
3453 if (sctp_set_primary_addr(stcb,
3454 (struct sockaddr *)NULL,
3456 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3457 if (net->ro._s_addr) {
3458 sctp_free_ifa(net->ro._s_addr);
3459 net->ro._s_addr = NULL;
3461 net->src_addr_selected = 0;
3468 return (EHOSTUNREACH);
3470 if (ro != &iproute) {
3471 memcpy(&iproute, ro, sizeof(*ro));
3473 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3474 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3475 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3476 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3477 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3480 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3481 /* failed to prepend data, give up */
3483 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
3486 #ifdef SCTP_PACKET_LOGGING
3487 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3488 sctp_packet_log(m, packet_length);
3490 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3492 /* send it out. table id is taken from stcb */
3493 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3495 SCTP_STAT_INCR(sctps_sendpackets);
3496 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3498 SCTP_STAT_INCR(sctps_senderrors);
3500 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3502 /* free tempy routes */
3508 /* PMTU check versus smallest asoc MTU goes here */
3509 if (ro->ro_rt != NULL) {
3512 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3514 (stcb->asoc.smallest_mtu > mtu)) {
3515 #ifdef SCTP_PRINT_FOR_B_AND_M
3516 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3519 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3523 /* route was freed */
3524 if (net->ro._s_addr &&
3525 net->src_addr_selected) {
3526 sctp_free_ifa(net->ro._s_addr);
3527 net->ro._s_addr = NULL;
3529 net->src_addr_selected = 0;
3535 else if (to->sa_family == AF_INET6) {
3537 struct ip6_hdr *ip6h;
3538 struct route_in6 ip6route;
3541 uint16_t flowBottom;
3542 u_char tosBottom, tosTop;
3543 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3545 struct sockaddr_in6 lsa6_storage;
3547 u_short prev_port = 0;
3550 flowlabel = net->tos_flowlabel;
3552 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3555 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3558 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
3561 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3562 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3563 packet_length += sizeof(struct ip6_hdr);
3564 SCTP_BUF_NEXT(newm) = m;
3567 ip6h = mtod(m, struct ip6_hdr *);
3569 * We assume here that inp_flow is in host byte order within
3572 flowBottom = flowlabel & 0x0000ffff;
3573 flowTop = ((flowlabel & 0x000f0000) >> 16);
3574 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3575 /* protect *sin6 from overwrite */
3576 sin6 = (struct sockaddr_in6 *)to;
3580 /* KAME hack: embed scopeid */
3581 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
3584 memset(&ip6route, 0, sizeof(ip6route));
3585 ro = (sctp_route_t *) & ip6route;
3586 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3588 ro = (sctp_route_t *) & net->ro;
3591 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3593 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3596 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3599 /* we could get no asoc if it is a O-O-T-B packet */
3600 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3602 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3603 ip6h->ip6_nxt = IPPROTO_SCTP;
3604 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3605 ip6h->ip6_dst = sin6->sin6_addr;
3608 * Add SRC address selection here: we can only reuse to a
3609 * limited degree the kame src-addr-sel, since we can try
3610 * their selection but it may not be bound.
3612 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3613 lsa6_tmp.sin6_family = AF_INET6;
3614 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3617 if (net->ro._s_addr && net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED) {
3618 sctp_free_ifa(net->ro._s_addr);
3619 net->ro._s_addr = NULL;
3620 net->src_addr_selected = 0;
3622 if (net->src_addr_selected == 0) {
3623 if (out_of_asoc_ok) {
3627 /* Cache the source address */
3628 net->ro._s_addr = sctp_source_address_selection(inp,
3634 net->src_addr_selected = 1;
3636 if (net->ro._s_addr == NULL) {
3637 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3638 net->src_addr_selected = 0;
3641 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3643 struct sctp_ifa *_lsrc;
3646 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3650 if (_lsrc == NULL) {
3653 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3654 sctp_free_ifa(_lsrc);
3656 lsa6->sin6_port = inp->sctp_lport;
3658 if ((ro->ro_rt == NULL)) {
3660 * src addr selection failed to find a route (or
3661 * valid source addr), so we can't get there from
3667 * XXX: sa6 may not have a valid sin6_scope_id in the
3668 * non-SCOPEDROUTING case.
3670 bzero(&lsa6_storage, sizeof(lsa6_storage));
3671 lsa6_storage.sin6_family = AF_INET6;
3672 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3673 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3678 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3679 lsa6_storage.sin6_port = inp->sctp_lport;
3680 lsa6 = &lsa6_storage;
3681 ip6h->ip6_src = lsa6->sin6_addr;
3684 * We set the hop limit now since there is a good chance
3685 * that our ro pointer is now filled
3687 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3688 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3691 /* Copy to be sure something bad is not happening */
3692 sin6->sin6_addr = ip6h->ip6_dst;
3693 lsa6->sin6_addr = ip6h->ip6_src;
3696 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3697 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3698 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3699 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3700 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3702 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3703 /* preserve the port and scope for link local send */
3704 prev_scope = sin6->sin6_scope_id;
3705 prev_port = sin6->sin6_port;
3707 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3708 /* failed to prepend data, give up */
3710 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
3713 #ifdef SCTP_PACKET_LOGGING
3714 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
3715 sctp_packet_log(m, packet_length);
3717 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3719 /* send it out. table id is taken from stcb */
3720 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp,
3724 /* for link local this must be done */
3725 sin6->sin6_scope_id = prev_scope;
3726 sin6->sin6_port = prev_port;
3728 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
3729 SCTP_STAT_INCR(sctps_sendpackets);
3730 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3732 SCTP_STAT_INCR(sctps_senderrors);
3735 /* Now if we had a temp route free it */
3740 /* PMTU check versus smallest asoc MTU goes here */
3741 if (ro->ro_rt == NULL) {
3742 /* Route was freed */
3743 if (net->ro._s_addr &&
3744 net->src_addr_selected) {
3745 sctp_free_ifa(net->ro._s_addr);
3746 net->ro._s_addr = NULL;
3748 net->src_addr_selected = 0;
3750 if (ro->ro_rt != NULL) {
3753 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3755 (stcb->asoc.smallest_mtu > mtu)) {
3756 #ifdef SCTP_PRINT_FOR_B_AND_M
3757 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
3760 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3764 if (ND_IFINFO(ifp)->linkmtu &&
3765 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
3766 #ifdef SCTP_PRINT_FOR_B_AND_M
3767 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
3768 ND_IFINFO(ifp)->linkmtu);
3770 sctp_mtu_size_reset(inp,
3772 ND_IFINFO(ifp)->linkmtu);
3780 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
3781 ((struct sockaddr *)to)->sa_family);
3789 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3791 struct mbuf *m, *m_at, *mp_last;
3792 struct sctp_nets *net;
3793 struct sctp_init_msg *initm;
3794 struct sctp_supported_addr_param *sup_addr;
3795 struct sctp_ecn_supported_param *ecn;
3796 struct sctp_prsctp_supported_param *prsctp;
3797 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3798 struct sctp_supported_chunk_types_param *pr_supported;
3799 int cnt_inits_to = 0;
3804 /* INIT's always go to the primary (and usually ONLY address) */
3806 net = stcb->asoc.primary_destination;
3808 net = TAILQ_FIRST(&stcb->asoc.nets);
3813 /* we confirm any address we send an INIT to */
3814 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3815 (void)sctp_set_primary_addr(stcb, NULL, net);
3817 /* we confirm any address we send an INIT to */
3818 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
3820 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
3821 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
3823 * special hook, if we are sending to link local it will not
3824 * show up in our private address count.
3826 struct sockaddr_in6 *sin6l;
3828 sin6l = &net->ro._l_addr.sin6;
3829 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
3832 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3833 /* This case should not happen */
3836 /* start the INIT timer */
3837 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
3839 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3841 /* No memory, INIT timer will re-attempt. */
3844 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
3846 * assume peer supports asconf in order to be able to queue local
3847 * address changes while an INIT is in flight and before the assoc
3850 stcb->asoc.peer_supports_asconf = 1;
3851 /* Now lets put the SCTP header in place */
3852 initm = mtod(m, struct sctp_init_msg *);
3853 initm->sh.src_port = inp->sctp_lport;
3854 initm->sh.dest_port = stcb->rport;
3855 initm->sh.v_tag = 0;
3856 initm->sh.checksum = 0; /* calculate later */
3857 /* now the chunk header */
3858 initm->msg.ch.chunk_type = SCTP_INITIATION;
3859 initm->msg.ch.chunk_flags = 0;
3860 /* fill in later from mbuf we build */
3861 initm->msg.ch.chunk_length = 0;
3862 /* place in my tag */
3863 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
3864 /* set up some of the credits. */
3865 initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
3866 SCTP_MINIMAL_RWND));
3868 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
3869 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
3870 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
3871 /* now the address restriction */
3872 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
3874 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
3875 /* we support 2 types IPv6/IPv4 */
3876 sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
3878 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
3879 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
3880 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
3882 if (inp->sctp_ep.adaptation_layer_indicator) {
3883 struct sctp_adaptation_layer_indication *ali;
3885 ali = (struct sctp_adaptation_layer_indication *)(
3886 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
3887 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
3888 ali->ph.param_length = htons(sizeof(*ali));
3889 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
3890 SCTP_BUF_LEN(m) += sizeof(*ali);
3891 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3894 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
3895 sizeof(*sup_addr) + sizeof(uint16_t));
3898 /* now any cookie time extensions */
3899 if (stcb->asoc.cookie_preserve_req) {
3900 struct sctp_cookie_perserve_param *cookie_preserve;
3902 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
3903 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
3904 cookie_preserve->ph.param_length = htons(
3905 sizeof(*cookie_preserve));
3906 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
3907 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
3908 ecn = (struct sctp_ecn_supported_param *)(
3909 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
3910 stcb->asoc.cookie_preserve_req = 0;
3913 if (sctp_ecn_enable == 1) {
3914 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3915 ecn->ph.param_length = htons(sizeof(*ecn));
3916 SCTP_BUF_LEN(m) += sizeof(*ecn);
3917 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3920 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3922 /* And now tell the peer we do pr-sctp */
3923 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3924 prsctp->ph.param_length = htons(sizeof(*prsctp));
3925 SCTP_BUF_LEN(m) += sizeof(*prsctp);
3927 /* And now tell the peer we do all the extensions */
3928 pr_supported = (struct sctp_supported_chunk_types_param *)
3929 ((caddr_t)prsctp + sizeof(*prsctp));
3930 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3932 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
3933 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
3934 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
3935 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
3936 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
3937 if (!sctp_auth_disable)
3938 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
3939 p_len = sizeof(*pr_supported) + num_ext;
3940 pr_supported->ph.param_length = htons(p_len);
3941 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
3942 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
3944 /* ECN nonce: And now tell the peer we support ECN nonce */
3945 if (sctp_ecn_nonce) {
3946 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
3947 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
3948 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3949 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3950 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
3952 /* add authentication parameters */
3953 if (!sctp_auth_disable) {
3954 struct sctp_auth_random *randp;
3955 struct sctp_auth_hmac_algo *hmacs;
3956 struct sctp_auth_chunk_list *chunks;
3958 /* attach RANDOM parameter, if available */
3959 if (stcb->asoc.authinfo.random != NULL) {
3960 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
3961 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
3962 #ifdef SCTP_AUTH_DRAFT_04
3963 randp->ph.param_type = htons(SCTP_RANDOM);
3964 randp->ph.param_length = htons(p_len);
3965 bcopy(stcb->asoc.authinfo.random->key,
3967 stcb->asoc.authinfo.random_len);
3969 /* random key already contains the header */
3970 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
3972 /* zero out any padding required */
3973 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
3974 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
3976 /* add HMAC_ALGO parameter */
3977 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
3978 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
3979 (uint8_t *) hmacs->hmac_ids);
3981 p_len += sizeof(*hmacs);
3982 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
3983 hmacs->ph.param_length = htons(p_len);
3984 /* zero out any padding required */
3985 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
3986 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
3988 /* add CHUNKS parameter */
3989 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
3990 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
3991 chunks->chunk_types);
3993 p_len += sizeof(*chunks);
3994 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
3995 chunks->ph.param_length = htons(p_len);
3996 /* zero out any padding required */
3997 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
3998 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4002 /* now the addresses */
4004 struct sctp_scoping scp;
4007 * To optimize this we could put the scoping stuff into a
4008 * structure and remove the individual uint8's from the
4009 * assoc structure. Then we could just sifa in the address
4010 * within the stcb.. but for now this is a quick hack to get
4011 * the address stuff teased apart.
4013 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4014 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4015 scp.loopback_scope = stcb->asoc.loopback_scope;
4016 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4017 scp.local_scope = stcb->asoc.local_scope;
4018 scp.site_scope = stcb->asoc.site_scope;
4020 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4023 /* calulate the size and update pkt header and chunk header */
4025 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4026 if (SCTP_BUF_NEXT(m_at) == NULL)
4028 p_len += SCTP_BUF_LEN(m_at);
4030 initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4032 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4033 * here since the timer will drive a retranmission.
4036 /* I don't expect this to execute but we will be safe here */
4038 if ((padval) && (mp_last)) {
4040 * The compiler worries that mp_last may not be set even
4041 * though I think it is impossible :-> however we add
4042 * mp_last here just in case.
4044 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4046 /* Houston we have a problem, no space */
4052 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4053 (struct sockaddr *)&net->ro._l_addr,
4054 m, 0, NULL, 0, 0, NULL, 0);
4055 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4056 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4057 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4061 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4062 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4065 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4066 * being equal to the beginning of the params i.e. (iphlen +
4067 * sizeof(struct sctp_init_msg) parse through the parameters to the
4068 * end of the mbuf verifying that all parameters are known.
4070 * For unknown parameters build and return a mbuf with
4071 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4072 * processing this chunk stop, and set *abort_processing to 1.
4074 * By having param_offset be pre-set to where parameters begin it is
4075 * hoped that this routine may be reused in the future by new
4078 struct sctp_paramhdr *phdr, params;
4080 struct mbuf *mat, *op_err;
4081 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4082 int at, limit, pad_needed;
4083 uint16_t ptype, plen, padded_size;
4086 *abort_processing = 0;
4089 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4092 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4093 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4094 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4095 ptype = ntohs(phdr->param_type);
4096 plen = ntohs(phdr->param_length);
4097 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4098 /* wacked parameter */
4099 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4102 limit -= SCTP_SIZE32(plen);
4104 * All parameters for all chunks that we know/understand are
4105 * listed here. We process them other places and make
4106 * appropriate stop actions per the upper bits. However this
4107 * is the generic routine processor's can call to get back
4108 * an operr.. to either incorporate (init-ack) or send.
4110 padded_size = SCTP_SIZE32(plen);
4112 /* Param's with variable size */
4113 case SCTP_HEARTBEAT_INFO:
4114 case SCTP_STATE_COOKIE:
4115 case SCTP_UNRECOG_PARAM:
4116 case SCTP_ERROR_CAUSE_IND:
4120 /* Param's with variable size within a range */
4121 case SCTP_CHUNK_LIST:
4122 case SCTP_SUPPORTED_CHUNK_EXT:
4123 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4124 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4129 case SCTP_SUPPORTED_ADDRTYPE:
4130 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4131 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4137 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4138 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4143 case SCTP_SET_PRIM_ADDR:
4144 case SCTP_DEL_IP_ADDRESS:
4145 case SCTP_ADD_IP_ADDRESS:
4146 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4147 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4148 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4153 /* Param's with a fixed size */
4154 case SCTP_IPV4_ADDRESS:
4155 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4156 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4161 case SCTP_IPV6_ADDRESS:
4162 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4163 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4168 case SCTP_COOKIE_PRESERVE:
4169 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4170 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4175 case SCTP_ECN_NONCE_SUPPORTED:
4176 case SCTP_PRSCTP_SUPPORTED:
4177 if (padded_size != sizeof(struct sctp_paramhdr)) {
4178 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4183 case SCTP_ECN_CAPABLE:
4184 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4185 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4190 case SCTP_ULP_ADAPTATION:
4191 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4192 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4197 case SCTP_SUCCESS_REPORT:
4198 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4199 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4204 case SCTP_HOSTNAME_ADDRESS:
4206 /* We can NOT handle HOST NAME addresses!! */
4209 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4210 *abort_processing = 1;
4211 if (op_err == NULL) {
4212 /* Ok need to try to get a mbuf */
4213 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4215 l_len += sizeof(struct sctp_paramhdr);
4216 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4218 SCTP_BUF_LEN(op_err) = 0;
4220 * pre-reserve space for ip
4221 * and sctp header and
4224 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4225 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4226 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4230 /* If we have space */
4231 struct sctp_paramhdr s;
4234 uint32_t cpthis = 0;
4236 pad_needed = 4 - (err_at % 4);
4237 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4238 err_at += pad_needed;
4240 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4241 s.param_length = htons(sizeof(s) + plen);
4242 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4243 err_at += sizeof(s);
4244 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4246 sctp_m_freem(op_err);
4248 * we are out of memory but
4249 * we still need to have a
4250 * look at what to do (the
4251 * system is in trouble
4256 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4264 * we do not recognize the parameter figure out what
4267 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4268 if ((ptype & 0x4000) == 0x4000) {
4269 /* Report bit is set?? */
4270 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4271 if (op_err == NULL) {
4274 /* Ok need to try to get an mbuf */
4275 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4277 l_len += sizeof(struct sctp_paramhdr);
4278 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4280 SCTP_BUF_LEN(op_err) = 0;
4281 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4282 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4283 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4287 /* If we have space */
4288 struct sctp_paramhdr s;
4291 uint32_t cpthis = 0;
4293 pad_needed = 4 - (err_at % 4);
4294 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4295 err_at += pad_needed;
4297 s.param_type = htons(SCTP_UNRECOG_PARAM);
4298 s.param_length = htons(sizeof(s) + plen);
4299 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4300 err_at += sizeof(s);
4301 if (plen > sizeof(tempbuf)) {
4302 plen = sizeof(tempbuf);
4304 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4306 sctp_m_freem(op_err);
4308 * we are out of memory but
4309 * we still need to have a
4310 * look at what to do (the
4311 * system is in trouble
4315 goto more_processing;
4317 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4322 if ((ptype & 0x8000) == 0x0000) {
4323 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4326 /* skip this chunk and continue processing */
4327 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4328 at += SCTP_SIZE32(plen);
4333 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4337 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4338 *abort_processing = 1;
4339 if ((op_err == NULL) && phdr) {
4342 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4343 l_len += (2 * sizeof(struct sctp_paramhdr));
4344 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4346 SCTP_BUF_LEN(op_err) = 0;
4347 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4348 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4349 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4352 if ((op_err) && phdr) {
4353 struct sctp_paramhdr s;
4356 uint32_t cpthis = 0;
4358 pad_needed = 4 - (err_at % 4);
4359 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4360 err_at += pad_needed;
4362 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4363 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4364 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4365 err_at += sizeof(s);
4366 /* Only copy back the p-hdr that caused the issue */
4367 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4373 sctp_are_there_new_addresses(struct sctp_association *asoc,
4374 struct mbuf *in_initpkt, int iphlen, int offset)
4377 * Given a INIT packet, look through the packet to verify that there
4378 * are NO new addresses. As we go through the parameters add reports
4379 * of any un-understood parameters that require an error. Also we
4380 * must return (1) to drop the packet if we see a un-understood
4381 * parameter that tells us to drop the chunk.
4383 struct sockaddr_in sin4, *sa4;
4384 struct sockaddr_in6 sin6, *sa6;
4385 struct sockaddr *sa_touse;
4386 struct sockaddr *sa;
4387 struct sctp_paramhdr *phdr, params;
4390 uint16_t ptype, plen;
4393 struct sctp_nets *net;
4395 memset(&sin4, 0, sizeof(sin4));
4396 memset(&sin6, 0, sizeof(sin6));
4397 sin4.sin_family = AF_INET;
4398 sin4.sin_len = sizeof(sin4);
4399 sin6.sin6_family = AF_INET6;
4400 sin6.sin6_len = sizeof(sin6);
4403 /* First what about the src address of the pkt ? */
4404 iph = mtod(in_initpkt, struct ip *);
4405 if (iph->ip_v == IPVERSION) {
4406 /* source addr is IPv4 */
4407 sin4.sin_addr = iph->ip_src;
4408 sa_touse = (struct sockaddr *)&sin4;
4409 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4410 /* source addr is IPv6 */
4411 struct ip6_hdr *ip6h;
4413 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4414 sin6.sin6_addr = ip6h->ip6_src;
4415 sa_touse = (struct sockaddr *)&sin6;
4421 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4422 sa = (struct sockaddr *)&net->ro._l_addr;
4423 if (sa->sa_family == sa_touse->sa_family) {
4424 if (sa->sa_family == AF_INET) {
4425 sa4 = (struct sockaddr_in *)sa;
4426 if (sa4->sin_addr.s_addr ==
4427 sin4.sin_addr.s_addr) {
4431 } else if (sa->sa_family == AF_INET6) {
4432 sa6 = (struct sockaddr_in6 *)sa;
4433 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
4442 /* New address added! no need to look futher. */
4445 /* Ok so far lets munge through the rest of the packet */
4449 offset += sizeof(struct sctp_init_chunk);
4450 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4452 ptype = ntohs(phdr->param_type);
4453 plen = ntohs(phdr->param_length);
4454 if (ptype == SCTP_IPV4_ADDRESS) {
4455 struct sctp_ipv4addr_param *p4, p4_buf;
4457 phdr = sctp_get_next_param(mat, offset,
4458 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4459 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4463 p4 = (struct sctp_ipv4addr_param *)phdr;
4464 sin4.sin_addr.s_addr = p4->addr;
4465 sa_touse = (struct sockaddr *)&sin4;
4466 } else if (ptype == SCTP_IPV6_ADDRESS) {
4467 struct sctp_ipv6addr_param *p6, p6_buf;
4469 phdr = sctp_get_next_param(mat, offset,
4470 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4471 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4475 p6 = (struct sctp_ipv6addr_param *)phdr;
4476 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4478 sa_touse = (struct sockaddr *)&sin4;
4481 /* ok, sa_touse points to one to check */
4483 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4484 sa = (struct sockaddr *)&net->ro._l_addr;
4485 if (sa->sa_family != sa_touse->sa_family) {
4488 if (sa->sa_family == AF_INET) {
4489 sa4 = (struct sockaddr_in *)sa;
4490 if (sa4->sin_addr.s_addr ==
4491 sin4.sin_addr.s_addr) {
4495 } else if (sa->sa_family == AF_INET6) {
4496 sa6 = (struct sockaddr_in6 *)sa;
4497 if (SCTP6_ARE_ADDR_EQUAL(
4498 &sa6->sin6_addr, &sin6.sin6_addr)) {
4505 /* New addr added! no need to look further */
4509 offset += SCTP_SIZE32(plen);
4510 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4516 * Given a MBUF chain that was sent into us containing an INIT. Build a
4517 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4518 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4519 * message (i.e. the struct sctp_init_msg).
4522 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4523 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4524 struct sctp_init_chunk *init_chk, uint32_t vrf_id)
4526 struct sctp_association *asoc;
4527 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4528 struct sctp_init_msg *initackm_out;
4529 struct sctp_ecn_supported_param *ecn;
4530 struct sctp_prsctp_supported_param *prsctp;
4531 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4532 struct sctp_supported_chunk_types_param *pr_supported;
4533 struct sockaddr_storage store;
4534 struct sockaddr_in *sin;
4535 struct sockaddr_in6 *sin6;
4538 struct ip6_hdr *ip6;
4539 struct sockaddr *to;
4540 struct sctp_state_cookie stc;
4541 struct sctp_nets *net = NULL;
4542 uint8_t *signature = NULL;
4543 int cnt_inits_to = 0;
4544 uint16_t his_limit, i_want;
4545 int abort_flag, padval;
4554 if ((asoc != NULL) &&
4555 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4556 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4557 /* new addresses, out of here in non-cookie-wait states */
4559 * Send a ABORT, we don't add the new address error clause
4560 * though we even set the T bit and copy in the 0 tag.. this
4561 * looks no different than if no listener was present.
4563 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id);
4567 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4568 (offset + sizeof(struct sctp_init_chunk)),
4569 &abort_flag, (struct sctp_chunkhdr *)init_chk);
4571 sctp_send_abort(init_pkt, iphlen, sh,
4572 init_chk->init.initiate_tag, op_err, vrf_id);
4575 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4577 /* No memory, INIT timer will re-attempt. */
4579 sctp_m_freem(op_err);
4582 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4584 /* the time I built cookie */
4585 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4587 /* populate any tie tags */
4589 /* unlock before tag selections */
4590 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4591 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4592 stc.cookie_life = asoc->cookie_life;
4593 net = asoc->primary_destination;
4595 stc.tie_tag_my_vtag = 0;
4596 stc.tie_tag_peer_vtag = 0;
4597 /* life I will award this cookie */
4598 stc.cookie_life = inp->sctp_ep.def_cookie_life;
4601 /* copy in the ports for later check */
4602 stc.myport = sh->dest_port;
4603 stc.peerport = sh->src_port;
4606 * If we wanted to honor cookie life extentions, we would add to
4607 * stc.cookie_life. For now we should NOT honor any extension
4609 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4610 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4611 struct inpcb *in_inp;
4613 /* Its a V6 socket */
4614 in_inp = (struct inpcb *)inp;
4615 stc.ipv6_addr_legal = 1;
4616 /* Now look at the binding flag to see if V4 will be legal */
4617 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4618 stc.ipv4_addr_legal = 1;
4620 /* V4 addresses are NOT legal on the association */
4621 stc.ipv4_addr_legal = 0;
4624 /* Its a V4 socket, no - V6 */
4625 stc.ipv4_addr_legal = 1;
4626 stc.ipv6_addr_legal = 0;
4629 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4634 /* now for scope setup */
4635 memset((caddr_t)&store, 0, sizeof(store));
4636 sin = (struct sockaddr_in *)&store;
4637 sin6 = (struct sockaddr_in6 *)&store;
4639 to = (struct sockaddr *)&store;
4640 iph = mtod(init_pkt, struct ip *);
4641 if (iph->ip_v == IPVERSION) {
4642 struct sctp_ifa *addr;
4643 sctp_route_t iproute;
4645 sin->sin_family = AF_INET;
4646 sin->sin_len = sizeof(struct sockaddr_in);
4647 sin->sin_port = sh->src_port;
4648 sin->sin_addr = iph->ip_src;
4649 /* lookup address */
4650 stc.address[0] = sin->sin_addr.s_addr;
4654 stc.addr_type = SCTP_IPV4_ADDRESS;
4655 /* local from address */
4656 memset(&iproute, 0, sizeof(iproute));
4658 memcpy(&ro->ro_dst, sin, sizeof(*sin));
4659 addr = sctp_source_address_selection(inp, NULL,
4669 stc.laddress[0] = addr->address.sin.sin_addr.s_addr;
4670 stc.laddress[1] = 0;
4671 stc.laddress[2] = 0;
4672 stc.laddress[3] = 0;
4673 stc.laddr_type = SCTP_IPV4_ADDRESS;
4674 /* scope_id is only for v6 */
4676 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
4677 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4682 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
4683 /* Must use the address in this case */
4684 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
4685 stc.loopback_scope = 1;
4688 stc.local_scope = 0;
4690 sctp_free_ifa(addr);
4691 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4692 struct sctp_ifa *addr;
4693 struct route_in6 iproute6;
4695 ip6 = mtod(init_pkt, struct ip6_hdr *);
4696 sin6->sin6_family = AF_INET6;
4697 sin6->sin6_len = sizeof(struct sockaddr_in6);
4698 sin6->sin6_port = sh->src_port;
4699 sin6->sin6_addr = ip6->ip6_src;
4700 /* lookup address */
4701 memcpy(&stc.address, &sin6->sin6_addr,
4702 sizeof(struct in6_addr));
4703 sin6->sin6_scope_id = 0;
4704 stc.addr_type = SCTP_IPV6_ADDRESS;
4706 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
4707 stc.loopback_scope = 1;
4708 stc.local_scope = 0;
4711 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4713 * If the new destination is a LINK_LOCAL we
4714 * must have common both site and local
4715 * scope. Don't set local scope though since
4716 * we must depend on the source to be added
4717 * implicitly. We cannot assure just because
4718 * we share one link that all links are
4721 stc.local_scope = 0;
4725 * we start counting for the private address
4726 * stuff at 1. since the link local we
4727 * source from won't show up in our scoped
4731 /* pull out the scope_id from incoming pkt */
4732 /* FIX ME: does this have scope from rcvif? */
4733 (void)sa6_recoverscope(sin6);
4735 sa6_embedscope(sin6, ip6_use_defzone);
4736 stc.scope_id = sin6->sin6_scope_id;
4737 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4739 * If the new destination is SITE_LOCAL then
4740 * we must have site scope in common.
4744 /* local from address */
4745 memset(&iproute6, 0, sizeof(iproute6));
4746 ro = (sctp_route_t *) & iproute6;
4747 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
4748 addr = sctp_source_address_selection(inp, NULL,
4749 ro, NULL, 0, vrf_id);
4757 memcpy(&stc.laddress, &addr->address.sin6.sin6_addr, sizeof(struct in6_addr));
4758 stc.laddr_type = SCTP_IPV6_ADDRESS;
4759 sctp_free_ifa(addr);
4762 /* set the scope per the existing tcb */
4763 struct sctp_nets *lnet;
4765 stc.loopback_scope = asoc->loopback_scope;
4766 stc.ipv4_scope = asoc->ipv4_local_scope;
4767 stc.site_scope = asoc->site_scope;
4768 stc.local_scope = asoc->local_scope;
4769 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
4770 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
4771 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
4773 * if we have a LL address, start
4781 /* use the net pointer */
4782 to = (struct sockaddr *)&net->ro._l_addr;
4783 if (to->sa_family == AF_INET) {
4784 sin = (struct sockaddr_in *)to;
4785 stc.address[0] = sin->sin_addr.s_addr;
4789 stc.addr_type = SCTP_IPV4_ADDRESS;
4790 if (net->src_addr_selected == 0) {
4792 * strange case here, the INIT should have
4793 * did the selection.
4795 net->ro._s_addr = sctp_source_address_selection(inp,
4796 stcb, (sctp_route_t *) & net->ro,
4798 if (net->ro._s_addr == NULL)
4801 net->src_addr_selected = 1;
4804 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
4805 stc.laddress[1] = 0;
4806 stc.laddress[2] = 0;
4807 stc.laddress[3] = 0;
4808 stc.laddr_type = SCTP_IPV4_ADDRESS;
4809 } else if (to->sa_family == AF_INET6) {
4810 sin6 = (struct sockaddr_in6 *)to;
4811 memcpy(&stc.address, &sin6->sin6_addr,
4812 sizeof(struct in6_addr));
4813 stc.addr_type = SCTP_IPV6_ADDRESS;
4814 if (net->src_addr_selected == 0) {
4816 * strange case here, the INIT should have
4817 * did the selection.
4819 net->ro._s_addr = sctp_source_address_selection(inp,
4820 stcb, (sctp_route_t *) & net->ro,
4822 if (net->ro._s_addr == NULL)
4825 net->src_addr_selected = 1;
4827 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
4828 sizeof(struct in6_addr));
4829 stc.laddr_type = SCTP_IPV6_ADDRESS;
4832 /* Now lets put the SCTP header in place */
4833 initackm_out = mtod(m, struct sctp_init_msg *);
4834 initackm_out->sh.src_port = inp->sctp_lport;
4835 initackm_out->sh.dest_port = sh->src_port;
4836 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
4837 /* Save it off for quick ref */
4838 stc.peers_vtag = init_chk->init.initiate_tag;
4839 initackm_out->sh.checksum = 0; /* calculate later */
4841 memcpy(stc.identification, SCTP_VERSION_STRING,
4842 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
4843 /* now the chunk header */
4844 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
4845 initackm_out->msg.ch.chunk_flags = 0;
4846 /* fill in later from mbuf we build */
4847 initackm_out->msg.ch.chunk_length = 0;
4848 /* place in my tag */
4849 if ((asoc != NULL) &&
4850 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
4851 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
4852 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
4853 /* re-use the v-tags and init-seq here */
4854 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
4855 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
4860 atomic_add_int(&asoc->refcnt, 1);
4861 SCTP_TCB_UNLOCK(stcb);
4862 vtag = sctp_select_a_tag(inp);
4863 initackm_out->msg.init.initiate_tag = htonl(vtag);
4864 /* get a TSN to use too */
4865 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
4866 SCTP_TCB_LOCK(stcb);
4867 atomic_add_int(&asoc->refcnt, -1);
4869 vtag = sctp_select_a_tag(inp);
4870 initackm_out->msg.init.initiate_tag = htonl(vtag);
4871 /* get a TSN to use too */
4872 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
4875 /* save away my tag to */
4876 stc.my_vtag = initackm_out->msg.init.initiate_tag;
4878 /* set up some of the credits. */
4879 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND));
4880 /* set what I want */
4881 his_limit = ntohs(init_chk->init.num_inbound_streams);
4882 /* choose what I want */
4884 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
4885 i_want = asoc->streamoutcnt;
4887 i_want = inp->sctp_ep.pre_open_stream_count;
4890 i_want = inp->sctp_ep.pre_open_stream_count;
4892 if (his_limit < i_want) {
4893 /* I Want more :< */
4894 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
4896 /* I can have what I want :> */
4897 initackm_out->msg.init.num_outbound_streams = htons(i_want);
4899 /* tell him his limt. */
4900 initackm_out->msg.init.num_inbound_streams =
4901 htons(inp->sctp_ep.max_open_streams_intome);
4902 /* setup the ECN pointer */
4904 if (inp->sctp_ep.adaptation_layer_indicator) {
4905 struct sctp_adaptation_layer_indication *ali;
4907 ali = (struct sctp_adaptation_layer_indication *)(
4908 (caddr_t)initackm_out + sizeof(*initackm_out));
4909 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4910 ali->ph.param_length = htons(sizeof(*ali));
4911 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4912 SCTP_BUF_LEN(m) += sizeof(*ali);
4913 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
4916 ecn = (struct sctp_ecn_supported_param *)(
4917 (caddr_t)initackm_out + sizeof(*initackm_out));
4921 if (sctp_ecn_enable == 1) {
4922 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4923 ecn->ph.param_length = htons(sizeof(*ecn));
4924 SCTP_BUF_LEN(m) += sizeof(*ecn);
4926 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4929 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4931 /* And now tell the peer we do pr-sctp */
4932 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4933 prsctp->ph.param_length = htons(sizeof(*prsctp));
4934 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4936 /* And now tell the peer we do all the extensions */
4937 pr_supported = (struct sctp_supported_chunk_types_param *)
4938 ((caddr_t)prsctp + sizeof(*prsctp));
4940 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4942 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4943 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4944 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4945 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4946 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4947 if (!sctp_auth_disable)
4948 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4949 p_len = sizeof(*pr_supported) + num_ext;
4950 pr_supported->ph.param_length = htons(p_len);
4951 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4952 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4954 /* ECN nonce: And now tell the peer we support ECN nonce */
4955 if (sctp_ecn_nonce) {
4956 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4957 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4958 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4959 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4960 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4962 /* add authentication parameters */
4963 if (!sctp_auth_disable) {
4964 struct sctp_auth_random *randp;
4965 struct sctp_auth_hmac_algo *hmacs;
4966 struct sctp_auth_chunk_list *chunks;
4967 uint16_t random_len;
4969 /* generate and add RANDOM parameter */
4970 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
4971 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4972 randp->ph.param_type = htons(SCTP_RANDOM);
4973 p_len = sizeof(*randp) + random_len;
4974 randp->ph.param_length = htons(p_len);
4975 SCTP_READ_RANDOM(randp->random_data, random_len);
4976 /* zero out any padding required */
4977 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4978 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4980 /* add HMAC_ALGO parameter */
4981 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4982 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
4983 (uint8_t *) hmacs->hmac_ids);
4985 p_len += sizeof(*hmacs);
4986 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4987 hmacs->ph.param_length = htons(p_len);
4988 /* zero out any padding required */
4989 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4990 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4992 /* add CHUNKS parameter */
4993 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4994 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
4995 chunks->chunk_types);
4997 p_len += sizeof(*chunks);
4998 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4999 chunks->ph.param_length = htons(p_len);
5000 /* zero out any padding required */
5001 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5002 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5006 /* now the addresses */
5008 struct sctp_scoping scp;
5011 * To optimize this we could put the scoping stuff into a
5012 * structure and remove the individual uint8's from the stc
5013 * structure. Then we could just sifa in the address within
5014 * the stc.. but for now this is a quick hack to get the
5015 * address stuff teased apart.
5017 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5018 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5019 scp.loopback_scope = stc.loopback_scope;
5020 scp.ipv4_local_scope = stc.ipv4_scope;
5021 scp.local_scope = stc.local_scope;
5022 scp.site_scope = stc.site_scope;
5023 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5026 /* tack on the operational error if present */
5034 llen += SCTP_BUF_LEN(ol);
5035 ol = SCTP_BUF_NEXT(ol);
5038 /* must add a pad to the param */
5039 uint32_t cpthis = 0;
5042 padlen = 4 - (llen % 4);
5043 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5045 while (SCTP_BUF_NEXT(m_at) != NULL) {
5046 m_at = SCTP_BUF_NEXT(m_at);
5048 SCTP_BUF_NEXT(m_at) = op_err;
5049 while (SCTP_BUF_NEXT(m_at) != NULL) {
5050 m_at = SCTP_BUF_NEXT(m_at);
5053 /* pre-calulate the size and update pkt header and chunk header */
5055 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5056 p_len += SCTP_BUF_LEN(m_tmp);
5057 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5058 /* m_tmp should now point to last one */
5063 /* Now we must build a cookie */
5064 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5065 sizeof(struct sctphdr), &stc, &signature);
5066 if (m_cookie == NULL) {
5067 /* memory problem */
5071 /* Now append the cookie to the end and update the space/size */
5072 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5074 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5075 p_len += SCTP_BUF_LEN(m_tmp);
5076 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5077 /* m_tmp should now point to last one */
5083 * Place in the size, but we don't include the last pad (if any) in
5086 initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5089 * Time to sign the cookie, we don't sign over the cookie signature
5090 * though thus we set trailer.
5092 (void)sctp_hmac_m(SCTP_HMAC,
5093 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5094 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5095 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5097 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5098 * here since the timer will drive a retranmission.
5101 if ((padval) && (mp_last)) {
5102 /* see my previous comments on mp_last */
5105 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5107 /* Houston we have a problem, no space */
5113 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5115 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5120 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5121 struct sctp_association *asoc,
5122 struct sctp_stream_out *strq, int holds_lock)
5124 struct sctp_stream_out *stre, *strn;
5126 if (holds_lock == 0) {
5127 SCTP_TCB_SEND_LOCK(stcb);
5129 if ((strq->next_spoke.tqe_next) ||
5130 (strq->next_spoke.tqe_prev)) {
5131 /* already on wheel */
5134 stre = TAILQ_FIRST(&asoc->out_wheel);
5136 /* only one on wheel */
5137 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5140 for (; stre; stre = strn) {
5141 strn = TAILQ_NEXT(stre, next_spoke);
5142 if (stre->stream_no > strq->stream_no) {
5143 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5145 } else if (stre->stream_no == strq->stream_no) {
5146 /* huh, should not happen */
5148 } else if (strn == NULL) {
5149 /* next one is null */
5150 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5155 if (holds_lock == 0) {
5156 SCTP_TCB_SEND_UNLOCK(stcb);
5161 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5162 struct sctp_association *asoc,
5163 struct sctp_stream_out *strq)
5165 /* take off and then setup so we know it is not on the wheel */
5166 SCTP_TCB_SEND_LOCK(stcb);
5167 if (TAILQ_FIRST(&strq->outqueue)) {
5168 /* more was added */
5169 SCTP_TCB_SEND_UNLOCK(stcb);
5172 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5173 strq->next_spoke.tqe_next = NULL;
5174 strq->next_spoke.tqe_prev = NULL;
5175 SCTP_TCB_SEND_UNLOCK(stcb);
5179 sctp_prune_prsctp(struct sctp_tcb *stcb,
5180 struct sctp_association *asoc,
5181 struct sctp_sndrcvinfo *srcv,
5185 struct sctp_tmit_chunk *chk, *nchk;
5187 SCTP_TCB_LOCK_ASSERT(stcb);
5188 if ((asoc->peer_supports_prsctp) &&
5189 (asoc->sent_queue_cnt_removeable > 0)) {
5190 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5192 * Look for chunks marked with the PR_SCTP flag AND
5193 * the buffer space flag. If the one being sent is
5194 * equal or greater priority then purge the old one
5195 * and free some space.
5197 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5199 * This one is PR-SCTP AND buffer space
5202 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5204 * Lower numbers equates to higher
5205 * priority so if the one we are
5206 * looking at has a larger or equal
5207 * priority we want to drop the data
5208 * and NOT retransmit it.
5212 * We release the book_size
5213 * if the mbuf is here
5218 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5219 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5221 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5222 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5225 freed_spc += ret_spc;
5226 if (freed_spc >= dataout) {
5229 } /* if chunk was present */
5230 } /* if of sufficent priority */
5231 } /* if chunk has enabled */
5232 } /* tailqforeach */
5234 chk = TAILQ_FIRST(&asoc->send_queue);
5236 nchk = TAILQ_NEXT(chk, sctp_next);
5237 /* Here we must move to the sent queue and mark */
5238 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5239 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5242 * We release the book_size
5243 * if the mbuf is here
5247 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5248 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5251 freed_spc += ret_spc;
5252 if (freed_spc >= dataout) {
5255 } /* end if chk->data */
5256 } /* end if right class */
5257 } /* end if chk pr-sctp */
5259 } /* end while (chk) */
5260 } /* if enabled in asoc */
5264 sctp_get_frag_point(struct sctp_tcb *stcb,
5265 struct sctp_association *asoc)
5270 * For endpoints that have both v6 and v4 addresses we must reserve
5271 * room for the ipv6 header, for those that are only dealing with V4
5272 * we use a larger frag point.
5274 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5275 ovh = SCTP_MED_OVERHEAD;
5277 ovh = SCTP_MED_V4_OVERHEAD;
5280 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5281 siz = asoc->smallest_mtu - ovh;
5283 siz = (stcb->asoc.sctp_frag_point - ovh);
5285 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5287 /* A data chunk MUST fit in a cluster */
5288 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5291 /* adjust for an AUTH chunk if DATA requires auth */
5292 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5293 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5296 /* make it an even word boundary please */
5303 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5304 struct sctp_stream_queue_pending *sp)
5307 if (stcb->asoc.peer_supports_prsctp) {
5309 * We assume that the user wants PR_SCTP_TTL if the user
5310 * provides a positive lifetime but does not specify any
5311 * PR_SCTP policy. This is a BAD assumption and causes
5312 * problems at least with the U-Vancovers MPI folks. I will
5313 * change this to be no policy means NO PR-SCTP.
5315 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5316 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5321 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5322 case CHUNK_FLAGS_PR_SCTP_BUF:
5324 * Time to live is a priority stored in tv_sec when
5325 * doing the buffer drop thing.
5327 sp->ts.tv_sec = sp->timetolive;
5330 case CHUNK_FLAGS_PR_SCTP_TTL:
5334 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5335 tv.tv_sec = sp->timetolive / 1000;
5336 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5337 timevaladd(&sp->ts, &tv);
5340 case CHUNK_FLAGS_PR_SCTP_RTX:
5342 * Time to live is a the number or retransmissions
5345 sp->ts.tv_sec = sp->timetolive;
5349 SCTPDBG(SCTP_DEBUG_USRREQ1,
5350 "Unknown PR_SCTP policy %u.\n",
5351 PR_SCTP_POLICY(sp->sinfo_flags));
5358 sctp_msg_append(struct sctp_tcb *stcb,
5359 struct sctp_nets *net,
5361 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5363 int error = 0, holds_lock;
5365 struct sctp_stream_queue_pending *sp = NULL;
5366 struct sctp_stream_out *strm;
5369 * Given an mbuf chain, put it into the association send queue and
5370 * place it on the wheel
5372 holds_lock = hold_stcb_lock;
5373 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5374 /* Invalid stream number */
5378 if ((stcb->asoc.stream_locked) &&
5379 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5383 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5384 /* Now can we send this? */
5385 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5386 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5387 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5388 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5389 /* got data while shutting down */
5393 sctp_alloc_a_strmoq(stcb, sp);
5395 SCTP_LTRACE_ERR(stcb->sctp_ep, stcb, ENOMEM, 0);
5399 sp->sinfo_flags = srcv->sinfo_flags;
5400 sp->timetolive = srcv->sinfo_timetolive;
5401 sp->ppid = srcv->sinfo_ppid;
5402 sp->context = srcv->sinfo_context;
5404 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5408 sp->net = stcb->asoc.primary_destination;
5411 atomic_add_int(&sp->net->ref_count, 1);
5412 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5413 sp->stream = srcv->sinfo_stream;
5414 sp->msg_is_complete = 1;
5415 sp->sender_all_done = 1;
5418 sp->tail_mbuf = NULL;
5421 sctp_set_prsctp_policy(stcb, sp);
5423 * We could in theory (for sendall) sifa the length in, but we would
5424 * still have to hunt through the chain since we need to setup the
5428 if (SCTP_BUF_NEXT(at) == NULL)
5430 sp->length += SCTP_BUF_LEN(at);
5431 at = SCTP_BUF_NEXT(at);
5433 SCTP_TCB_SEND_LOCK(stcb);
5434 sctp_snd_sb_alloc(stcb, sp->length);
5435 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5436 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5437 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5438 sp->strseq = strm->next_sequence_sent;
5439 strm->next_sequence_sent++;
5441 if ((strm->next_spoke.tqe_next == NULL) &&
5442 (strm->next_spoke.tqe_prev == NULL)) {
5443 /* Not on wheel, insert */
5444 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5447 SCTP_TCB_SEND_UNLOCK(stcb);
5456 static struct mbuf *
5457 sctp_copy_mbufchain(struct mbuf *clonechain,
5458 struct mbuf *outchain,
5459 struct mbuf **endofchain,
5462 uint8_t copy_by_ref)
5465 struct mbuf *appendchain;
5469 if (endofchain == NULL) {
5473 sctp_m_freem(outchain);
5476 if (can_take_mbuf) {
5477 appendchain = clonechain;
5480 (sizeofcpy <= (int)((((sctp_mbuf_threshold_count - 1) * MLEN) + MHLEN)))
5482 /* Its not in a cluster */
5483 if (*endofchain == NULL) {
5484 /* lets get a mbuf cluster */
5485 if (outchain == NULL) {
5486 /* This is the general case */
5488 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5489 if (outchain == NULL) {
5492 SCTP_BUF_LEN(outchain) = 0;
5493 *endofchain = outchain;
5494 /* get the prepend space */
5495 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5498 * We really should not get a NULL
5504 if (SCTP_BUF_NEXT(m) == NULL) {
5508 m = SCTP_BUF_NEXT(m);
5511 if (*endofchain == NULL) {
5513 * huh, TSNH XXX maybe we
5516 sctp_m_freem(outchain);
5520 /* get the new end of length */
5521 len = M_TRAILINGSPACE(*endofchain);
5523 /* how much is left at the end? */
5524 len = M_TRAILINGSPACE(*endofchain);
5526 /* Find the end of the data, for appending */
5527 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5529 /* Now lets copy it out */
5530 if (len >= sizeofcpy) {
5531 /* It all fits, copy it in */
5532 m_copydata(clonechain, 0, sizeofcpy, cp);
5533 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5535 /* fill up the end of the chain */
5537 m_copydata(clonechain, 0, len, cp);
5538 SCTP_BUF_LEN((*endofchain)) += len;
5539 /* now we need another one */
5542 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5547 SCTP_BUF_NEXT((*endofchain)) = m;
5549 cp = mtod((*endofchain), caddr_t);
5550 m_copydata(clonechain, len, sizeofcpy, cp);
5551 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5555 /* copy the old fashion way */
5556 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5559 if (appendchain == NULL) {
5562 sctp_m_freem(outchain);
5566 /* tack on to the end */
5567 if (*endofchain != NULL) {
5568 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
5572 if (SCTP_BUF_NEXT(m) == NULL) {
5573 SCTP_BUF_NEXT(m) = appendchain;
5576 m = SCTP_BUF_NEXT(m);
5580 * save off the end and update the end-chain postion
5584 if (SCTP_BUF_NEXT(m) == NULL) {
5588 m = SCTP_BUF_NEXT(m);
5592 /* save off the end and update the end-chain postion */
5595 if (SCTP_BUF_NEXT(m) == NULL) {
5599 m = SCTP_BUF_NEXT(m);
5601 return (appendchain);
5606 sctp_med_chunk_output(struct sctp_inpcb *inp,
5607 struct sctp_tcb *stcb,
5608 struct sctp_association *asoc,
5611 int control_only, int *cwnd_full, int from_where,
5612 struct timeval *now, int *now_filled, int frag_point);
5615 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
5618 struct sctp_copy_all *ca;
5621 int added_control = 0;
5622 int un_sent, do_chunk_output = 1;
5623 struct sctp_association *asoc;
5625 ca = (struct sctp_copy_all *)ptr;
5626 if (ca->m == NULL) {
5629 if (ca->inp != inp) {
5633 if ((ca->m) && ca->sndlen) {
5634 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
5636 /* can't copy so we are done */
5643 SCTP_TCB_LOCK_ASSERT(stcb);
5644 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
5645 /* Abort this assoc with m as the user defined reason */
5647 struct sctp_paramhdr *ph;
5649 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
5651 ph = mtod(m, struct sctp_paramhdr *);
5652 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5653 ph->param_length = htons(ca->sndlen);
5656 * We add one here to keep the assoc from
5657 * dis-appearing on us.
5659 atomic_add_int(&stcb->asoc.refcnt, 1);
5660 sctp_abort_an_association(inp, stcb,
5661 SCTP_RESPONSE_TO_USER_REQ,
5664 * sctp_abort_an_association calls sctp_free_asoc()
5665 * free association will NOT free it since we
5666 * incremented the refcnt .. we do this to prevent
5667 * it being freed and things getting tricky since we
5668 * could end up (from free_asoc) calling inpcb_free
5669 * which would get a recursive lock call to the
5670 * iterator lock.. But as a consequence of that the
5671 * stcb will return to us un-locked.. since
5672 * free_asoc returns with either no TCB or the TCB
5673 * unlocked, we must relock.. to unlock in the
5674 * iterator timer :-0
5676 SCTP_TCB_LOCK(stcb);
5677 atomic_add_int(&stcb->asoc.refcnt, -1);
5678 goto no_chunk_output;
5682 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
5686 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
5687 /* shutdown this assoc */
5690 cnt = sctp_is_there_unsent_data(stcb);
5692 if (TAILQ_EMPTY(&asoc->send_queue) &&
5693 TAILQ_EMPTY(&asoc->sent_queue) &&
5695 if (asoc->locked_on_sending) {
5699 * there is nothing queued to send, so I'm
5702 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
5703 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
5704 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5706 * only send SHUTDOWN the first time
5709 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
5710 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
5711 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5713 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
5714 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
5715 asoc->primary_destination);
5716 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
5717 asoc->primary_destination);
5719 do_chunk_output = 0;
5723 * we still got (or just got) data to send,
5724 * so set SHUTDOWN_PENDING
5727 * XXX sockets draft says that SCTP_EOF
5728 * should be sent with no data. currently,
5729 * we will allow user data to be sent first
5730 * and move to SHUTDOWN-PENDING
5732 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
5733 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
5734 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5735 if (asoc->locked_on_sending) {
5737 * Locked to send out the
5740 struct sctp_stream_queue_pending *sp;
5742 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
5744 if ((sp->length == 0) && (sp->msg_is_complete == 0))
5745 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5748 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
5749 if (TAILQ_EMPTY(&asoc->send_queue) &&
5750 TAILQ_EMPTY(&asoc->sent_queue) &&
5751 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
5753 atomic_add_int(&stcb->asoc.refcnt, 1);
5754 sctp_abort_an_association(stcb->sctp_ep, stcb,
5755 SCTP_RESPONSE_TO_USER_REQ,
5757 atomic_add_int(&stcb->asoc.refcnt, -1);
5758 goto no_chunk_output;
5760 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
5761 asoc->primary_destination);
5767 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
5768 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
5770 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
5771 (stcb->asoc.total_flight > 0) &&
5772 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
5774 do_chunk_output = 0;
5776 if (do_chunk_output)
5777 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
5778 else if (added_control) {
5779 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
5783 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
5784 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
5785 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
5796 sctp_sendall_completes(void *ptr, uint32_t val)
5798 struct sctp_copy_all *ca;
5800 ca = (struct sctp_copy_all *)ptr;
5802 * Do a notify here? Kacheong suggests that the notify be done at
5803 * the send time.. so you would push up a notification if any send
5804 * failed. Don't know if this is feasable since the only failures we
5805 * have is "memory" related and if you cannot get an mbuf to send
5806 * the data you surely can't get an mbuf to send up to notify the
5807 * user you can't send the data :->
5810 /* now free everything */
5811 sctp_m_freem(ca->m);
5812 SCTP_FREE(ca, SCTP_M_COPYAL);
5816 #define MC_ALIGN(m, len) do { \
5817 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
5822 static struct mbuf *
5823 sctp_copy_out_all(struct uio *uio, int len)
5825 struct mbuf *ret, *at;
5826 int left, willcpy, cancpy, error;
5828 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
5834 SCTP_BUF_LEN(ret) = 0;
5835 /* save space for the data chunk header */
5836 cancpy = M_TRAILINGSPACE(ret);
5837 willcpy = min(cancpy, left);
5840 /* Align data to the end */
5841 error = uiomove(mtod(at, caddr_t), willcpy, uio);
5847 SCTP_BUF_LEN(at) = willcpy;
5848 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
5851 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
5852 if (SCTP_BUF_NEXT(at) == NULL) {
5855 at = SCTP_BUF_NEXT(at);
5856 SCTP_BUF_LEN(at) = 0;
5857 cancpy = M_TRAILINGSPACE(at);
5858 willcpy = min(cancpy, left);
5865 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
5866 struct sctp_sndrcvinfo *srcv)
5869 struct sctp_copy_all *ca;
5871 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
5875 SCTP_LTRACE_ERR(inp, NULL, ENOMEM, 0);
5878 memset(ca, 0, sizeof(struct sctp_copy_all));
5881 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
5883 * take off the sendall flag, it would be bad if we failed to do
5886 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
5887 /* get length and mbuf chain */
5889 ca->sndlen = uio->uio_resid;
5890 ca->m = sctp_copy_out_all(uio, ca->sndlen);
5891 if (ca->m == NULL) {
5892 SCTP_FREE(ca, SCTP_M_COPYAL);
5893 SCTP_LTRACE_ERR(inp, NULL, ENOMEM, 0);
5897 /* Gather the length of the send */
5903 ca->sndlen += SCTP_BUF_LEN(m);
5904 m = SCTP_BUF_NEXT(m);
5908 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
5909 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
5910 SCTP_ASOC_ANY_STATE,
5912 sctp_sendall_completes, inp, 1);
5914 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
5915 SCTP_FREE(ca, SCTP_M_COPYAL);
5923 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
5925 struct sctp_tmit_chunk *chk, *nchk;
5927 chk = TAILQ_FIRST(&asoc->control_send_queue);
5929 nchk = TAILQ_NEXT(chk, sctp_next);
5930 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
5931 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5933 sctp_m_freem(chk->data);
5936 asoc->ctrl_queue_cnt--;
5937 sctp_free_a_chunk(stcb, chk);
5944 sctp_toss_old_asconf(struct sctp_tcb *stcb)
5946 struct sctp_association *asoc;
5947 struct sctp_tmit_chunk *chk, *chk_tmp;
5950 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
5953 chk_tmp = TAILQ_NEXT(chk, sctp_next);
5954 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
5955 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
5956 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5958 sctp_m_freem(chk->data);
5961 asoc->ctrl_queue_cnt--;
5962 sctp_free_a_chunk(stcb, chk);
5969 sctp_clean_up_datalist(struct sctp_tcb *stcb,
5971 struct sctp_association *asoc,
5972 struct sctp_tmit_chunk **data_list,
5974 struct sctp_nets *net)
5977 struct sctp_tmit_chunk *tp1;
5979 for (i = 0; i < bundle_at; i++) {
5980 /* off of the send queue */
5983 * Any chunk NOT 0 you zap the time chunk 0 gets
5984 * zapped or set based on if a RTO measurment is
5987 data_list[i]->do_rtt = 0;
5990 data_list[i]->sent_rcv_time = net->last_sent_time;
5991 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
5992 TAILQ_REMOVE(&asoc->send_queue,
5995 /* on to the sent queue */
5996 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
5997 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
5998 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
5999 struct sctp_tmit_chunk *tpp;
6001 /* need to move back */
6003 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6005 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6009 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6010 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6013 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6015 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6020 /* This does not lower until the cum-ack passes it */
6021 asoc->sent_queue_cnt++;
6022 asoc->send_queue_cnt--;
6023 if ((asoc->peers_rwnd <= 0) &&
6024 (asoc->total_flight == 0) &&
6026 /* Mark the chunk as being a window probe */
6027 SCTP_STAT_INCR(sctps_windowprobed);
6029 #ifdef SCTP_AUDITING_ENABLED
6030 sctp_audit_log(0xC2, 3);
6032 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6033 data_list[i]->snd_count = 1;
6034 data_list[i]->rec.data.chunk_was_revoked = 0;
6035 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
6036 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6037 data_list[i]->whoTo->flight_size,
6038 data_list[i]->book_size,
6039 (uintptr_t) data_list[i]->whoTo,
6040 data_list[i]->rec.data.TSN_seq);
6042 sctp_flight_size_increase(data_list[i]);
6043 sctp_total_flight_increase(stcb, data_list[i]);
6044 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
6045 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6046 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
6048 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6049 (uint32_t) (data_list[i]->send_size + sctp_peer_chunk_oh));
6050 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6051 /* SWS sender side engages */
6052 asoc->peers_rwnd = 0;
6058 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6060 struct sctp_tmit_chunk *chk, *nchk;
6062 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6064 nchk = TAILQ_NEXT(chk, sctp_next);
6065 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6066 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6067 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6068 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6069 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6070 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6071 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6072 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6073 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6074 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6075 /* Stray chunks must be cleaned up */
6077 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6079 sctp_m_freem(chk->data);
6082 asoc->ctrl_queue_cnt--;
6083 sctp_free_a_chunk(stcb, chk);
6084 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6085 /* special handling, we must look into the param */
6086 if (chk != asoc->str_reset) {
6087 goto clean_up_anyway;
6095 sctp_can_we_split_this(struct sctp_tcb *stcb,
6096 struct sctp_stream_queue_pending *sp,
6097 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6100 * Make a decision on if I should split a msg into multiple parts.
6101 * This is only asked of incomplete messages.
6105 * If we are doing EEOR we need to always send it if its the
6106 * entire thing, since it might be all the guy is putting in
6109 if (goal_mtu >= sp->length) {
6111 * If we have data outstanding,
6112 * we get another chance when the sack
6113 * arrives to transmit - wait for more data
6115 if (stcb->asoc.total_flight == 0) {
6117 * If nothing is in flight, we zero the
6120 return (sp->length);
6125 /* You can fill the rest */
6129 if ((sp->length <= goal_mtu) ||
6130 ((sp->length - goal_mtu) < sctp_min_residual)) {
6131 /* Sub-optimial residual don't split in non-eeor mode. */
6135 * If we reach here sp->length is larger than the goal_mtu. Do we
6136 * wish to split it for the sake of packet putting together?
6138 if (goal_mtu >= min(sctp_min_split_point, frag_point)) {
6139 /* Its ok to split it */
6140 return (min(goal_mtu, frag_point));
6142 /* Nope, can't split */
6148 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6149 struct sctp_stream_out *strq,
6151 uint32_t frag_point,
6157 /* Move from the stream to the send_queue keeping track of the total */
6158 struct sctp_association *asoc;
6159 struct sctp_stream_queue_pending *sp;
6160 struct sctp_tmit_chunk *chk;
6161 struct sctp_data_chunk *dchkh;
6163 uint8_t rcv_flags = 0;
6165 uint8_t send_lock_up = 0;
6167 SCTP_TCB_LOCK_ASSERT(stcb);
6170 /* sa_ignore FREED_MEMORY */
6171 sp = TAILQ_FIRST(&strq->outqueue);
6174 SCTP_TCB_SEND_LOCK(stcb);
6175 sp = TAILQ_FIRST(&strq->outqueue);
6177 SCTP_TCB_SEND_UNLOCK(stcb);
6180 if (strq->last_msg_incomplete) {
6181 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6183 strq->last_msg_incomplete);
6184 strq->last_msg_incomplete = 0;
6186 SCTP_TCB_SEND_UNLOCK(stcb);
6189 if (sp->msg_is_complete) {
6190 if (sp->length == 0) {
6191 if (sp->sender_all_done) {
6193 * We are doing differed cleanup. Last time
6194 * through when we took all the data the
6195 * sender_all_done was not set.
6197 if (sp->put_last_out == 0) {
6198 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6199 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6200 sp->sender_all_done,
6202 sp->msg_is_complete,
6206 if (TAILQ_NEXT(sp, next) == NULL) {
6207 SCTP_TCB_SEND_LOCK(stcb);
6210 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6211 TAILQ_REMOVE(&strq->outqueue, sp, next);
6212 sctp_free_remote_addr(sp->net);
6214 sctp_m_freem(sp->data);
6217 sctp_free_a_strmoq(stcb, sp);
6219 /* we can't be locked to it */
6221 stcb->asoc.locked_on_sending = NULL;
6223 SCTP_TCB_SEND_UNLOCK(stcb);
6226 /* back to get the next msg */
6230 * sender just finished this but still holds
6239 /* is there some to get */
6240 if (sp->length == 0) {
6247 some_taken = sp->some_taken;
6248 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6249 sp->msg_is_complete = 1;
6252 if (sp->msg_is_complete) {
6253 /* The message is complete */
6254 to_move = min(sp->length, frag_point);
6255 if (to_move == sp->length) {
6256 /* All of it fits in the MTU */
6257 if (sp->some_taken) {
6258 rcv_flags |= SCTP_DATA_LAST_FRAG;
6259 sp->put_last_out = 1;
6261 rcv_flags |= SCTP_DATA_NOT_FRAG;
6262 sp->put_last_out = 1;
6265 /* Not all of it fits, we fragment */
6266 if (sp->some_taken == 0) {
6267 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6272 to_move = sctp_can_we_split_this(stcb, sp, goal_mtu,
6273 frag_point, eeor_mode);
6276 * We use a snapshot of length in case it
6277 * is expanding during the compare.
6282 if (to_move >= llen) {
6284 if (send_lock_up == 0) {
6286 * We are taking all of an incomplete msg
6287 * thus we need a send lock.
6289 SCTP_TCB_SEND_LOCK(stcb);
6291 if (sp->msg_is_complete) {
6293 * the sender finished the
6300 if (sp->some_taken == 0) {
6301 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6305 /* Nothing to take. */
6306 if (sp->some_taken) {
6314 /* If we reach here, we can copy out a chunk */
6315 sctp_alloc_a_chunk(stcb, chk);
6317 /* No chunk memory */
6320 SCTP_TCB_SEND_UNLOCK(stcb);
6327 * Setup for unordered if needed by looking at the user sent info
6330 if (sp->sinfo_flags & SCTP_UNORDERED) {
6331 rcv_flags |= SCTP_DATA_UNORDERED;
6333 /* clear out the chunk before setting up */
6334 memset(chk, sizeof(*chk), 0);
6335 chk->rec.data.rcv_flags = rcv_flags;
6336 if (SCTP_BUF_IS_EXTENDED(sp->data)) {
6337 chk->copy_by_ref = 1;
6339 chk->copy_by_ref = 0;
6341 if (to_move >= sp->length) {
6342 /* we can steal the whole thing */
6343 chk->data = sp->data;
6344 chk->last_mbuf = sp->tail_mbuf;
6345 /* register the stealing */
6346 sp->data = sp->tail_mbuf = NULL;
6350 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6351 chk->last_mbuf = NULL;
6352 if (chk->data == NULL) {
6353 sp->some_taken = some_taken;
6354 sctp_free_a_chunk(stcb, chk);
6358 /* Pull off the data */
6359 m_adj(sp->data, to_move);
6360 /* Now lets work our way down and compact it */
6362 while (m && (SCTP_BUF_LEN(m) == 0)) {
6363 sp->data = SCTP_BUF_NEXT(m);
6364 SCTP_BUF_NEXT(m) = NULL;
6365 if (sp->tail_mbuf == m) {
6367 * Freeing tail? TSNH since
6368 * we supposedly were taking less
6369 * than the sp->length.
6372 panic("Huh, freing tail? - TSNH");
6374 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6375 sp->tail_mbuf = sp->data = NULL;
6384 if (to_move > sp->length) {
6385 /*- This should not happen either
6386 * since we always lower to_move to the size
6387 * of sp->length if its larger.
6390 panic("Huh, how can to_move be larger?");
6392 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6396 atomic_subtract_int(&sp->length, to_move);
6398 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6399 /* Not enough room for a chunk header, get some */
6402 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6405 * we're in trouble here. _PREPEND below will free
6406 * all the data if there is no leading space, so we
6407 * must put the data back and restore.
6409 if (send_lock_up == 0) {
6410 SCTP_TCB_SEND_LOCK(stcb);
6413 if (chk->data == NULL) {
6414 /* unsteal the data */
6415 sp->data = chk->data;
6416 sp->tail_mbuf = chk->last_mbuf;
6420 /* reassemble the data */
6422 sp->data = chk->data;
6423 SCTP_BUF_NEXT(sp->data) = m_tmp;
6425 sp->some_taken = some_taken;
6426 atomic_add_int(&sp->length, to_move);
6429 sctp_free_a_chunk(stcb, chk);
6432 SCTP_BUF_LEN(m) = 0;
6433 SCTP_BUF_NEXT(m) = chk->data;
6435 M_ALIGN(chk->data, 4);
6438 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6439 if (chk->data == NULL) {
6440 /* HELP, TSNH since we assured it would not above? */
6442 panic("prepend failes HELP?");
6444 SCTP_PRINTF("prepend fails HELP?\n");
6445 sctp_free_a_chunk(stcb, chk);
6450 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6451 chk->book_size = chk->send_size = (to_move +
6452 sizeof(struct sctp_data_chunk));
6453 chk->book_size_scale = 0;
6454 chk->sent = SCTP_DATAGRAM_UNSENT;
6457 * get last_mbuf and counts of mb useage This is ugly but hopefully
6458 * its only one mbuf.
6460 if (chk->last_mbuf == NULL) {
6461 chk->last_mbuf = chk->data;
6462 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6463 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6467 chk->asoc = &stcb->asoc;
6468 chk->pad_inplace = 0;
6469 chk->no_fr_allowed = 0;
6470 chk->rec.data.stream_seq = sp->strseq;
6471 chk->rec.data.stream_number = sp->stream;
6472 chk->rec.data.payloadtype = sp->ppid;
6473 chk->rec.data.context = sp->context;
6474 chk->rec.data.doing_fast_retransmit = 0;
6475 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
6477 chk->rec.data.timetodrop = sp->ts;
6478 chk->flags = sp->act_flags;
6479 chk->addr_over = sp->addr_over;
6482 atomic_add_int(&chk->whoTo->ref_count, 1);
6484 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6485 #ifdef SCTP_LOG_SENDING_STR
6486 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6487 (uintptr_t) stcb, (uintptr_t) sp,
6488 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6489 chk->rec.data.TSN_seq);
6492 dchkh = mtod(chk->data, struct sctp_data_chunk *);
6494 * Put the rest of the things in place now. Size was done earlier in
6495 * previous loop prior to padding.
6498 #ifdef SCTP_ASOCLOG_OF_TSNS
6499 SCTP_TCB_LOCK_ASSERT(stcb);
6500 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
6501 asoc->tsn_out_at = 0;
6502 asoc->tsn_out_wrapped = 1;
6504 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
6505 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
6506 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
6507 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
6508 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
6509 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
6510 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
6511 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
6515 dchkh->ch.chunk_type = SCTP_DATA;
6516 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
6517 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
6518 dchkh->dp.stream_id = htons(strq->stream_no);
6519 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
6520 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
6521 dchkh->ch.chunk_length = htons(chk->send_size);
6522 /* Now advance the chk->send_size by the actual pad needed. */
6523 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
6528 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
6529 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
6530 chk->pad_inplace = 1;
6532 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
6533 /* pad added an mbuf */
6534 chk->last_mbuf = lm;
6536 chk->send_size += pads;
6538 /* We only re-set the policy if it is on */
6539 if (sp->pr_sctp_on) {
6540 sctp_set_prsctp_policy(stcb, sp);
6541 asoc->pr_sctp_cnt++;
6542 chk->pr_sctp_on = 1;
6544 chk->pr_sctp_on = 0;
6546 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
6547 /* All done pull and kill the message */
6548 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6549 if (sp->put_last_out == 0) {
6550 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
6551 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6552 sp->sender_all_done,
6554 sp->msg_is_complete,
6558 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
6559 SCTP_TCB_SEND_LOCK(stcb);
6562 TAILQ_REMOVE(&strq->outqueue, sp, next);
6563 sctp_free_remote_addr(sp->net);
6565 sctp_m_freem(sp->data);
6568 sctp_free_a_strmoq(stcb, sp);
6570 /* we can't be locked to it */
6572 stcb->asoc.locked_on_sending = NULL;
6574 /* more to go, we are locked */
6577 asoc->chunks_on_out_queue++;
6578 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
6579 asoc->send_queue_cnt++;
6581 SCTP_TCB_SEND_UNLOCK(stcb);
6588 static struct sctp_stream_out *
6589 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
6591 struct sctp_stream_out *strq;
6593 /* Find the next stream to use */
6594 if (asoc->last_out_stream == NULL) {
6595 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6596 if (asoc->last_out_stream == NULL) {
6597 /* huh nothing on the wheel, TSNH */
6602 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
6605 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
6607 /* Save off the last stream */
6608 asoc->last_out_stream = strq;
6614 sctp_fill_outqueue(struct sctp_tcb *stcb,
6615 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
6617 struct sctp_association *asoc;
6618 struct sctp_stream_out *strq, *strqn, *strqt;
6619 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
6621 struct sctp_stream_queue_pending *sp;
6623 SCTP_TCB_LOCK_ASSERT(stcb);
6626 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
6627 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6629 /* ?? not sure what else to do */
6630 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
6633 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
6636 /* Need an allowance for the data chunk header too */
6637 goal_mtu -= sizeof(struct sctp_data_chunk);
6639 /* must make even word boundary */
6640 goal_mtu &= 0xfffffffc;
6641 if (asoc->locked_on_sending) {
6642 /* We are stuck on one stream until the message completes. */
6643 strqn = strq = asoc->locked_on_sending;
6646 strqn = strq = sctp_select_a_stream(stcb, asoc);
6650 while ((goal_mtu > 0) && strq) {
6651 sp = TAILQ_FIRST(&strq->outqueue);
6653 * If CMT is off, we must validate that the stream in
6654 * question has the first item pointed towards are network
6655 * destionation requested by the caller. Note that if we
6656 * turn out to be locked to a stream (assigning TSN's then
6657 * we must stop, since we cannot look for another stream
6658 * with data to send to that destination). In CMT's case, by
6659 * skipping this check, we will send one data packet towards
6660 * the requested net.
6665 if ((sp->net != net) && (sctp_cmt_on_off == 0)) {
6666 /* none for this network */
6670 strq = sctp_select_a_stream(stcb, asoc);
6674 if (strqn == strq) {
6675 /* I have circled */
6683 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
6684 &giveup, eeor_mode, &bail);
6685 asoc->last_out_stream = strq;
6687 asoc->locked_on_sending = strq;
6688 if ((moved_how_much == 0) || (giveup) || bail)
6689 /* no more to move for now */
6692 asoc->locked_on_sending = NULL;
6693 strqt = sctp_select_a_stream(stcb, asoc);
6694 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
6695 if (strq == strqn) {
6696 /* Must move start to next one */
6697 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
6698 if (strqn == NULL) {
6699 strqn = TAILQ_FIRST(&asoc->out_wheel);
6700 if (strqn == NULL) {
6705 sctp_remove_from_wheel(stcb, asoc, strq);
6707 if ((giveup) || bail) {
6715 total_moved += moved_how_much;
6716 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
6717 goal_mtu &= 0xfffffffc;
6722 if (total_moved == 0) {
6723 if ((sctp_cmt_on_off == 0) &&
6724 (net == stcb->asoc.primary_destination)) {
6725 /* ran dry for primary network net */
6726 SCTP_STAT_INCR(sctps_primary_randry);
6727 } else if (sctp_cmt_on_off) {
6728 /* ran dry with CMT on */
6729 SCTP_STAT_INCR(sctps_cmt_randry);
6735 sctp_fix_ecn_echo(struct sctp_association *asoc)
6737 struct sctp_tmit_chunk *chk;
6739 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6740 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
6741 chk->sent = SCTP_DATAGRAM_UNSENT;
6747 sctp_move_to_an_alt(struct sctp_tcb *stcb,
6748 struct sctp_association *asoc,
6749 struct sctp_nets *net)
6751 struct sctp_tmit_chunk *chk;
6752 struct sctp_nets *a_net;
6754 SCTP_TCB_LOCK_ASSERT(stcb);
6756 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
6757 * destination using the PF algorithm for finding alternate
6760 if (sctp_cmt_on_off && sctp_cmt_pf) {
6761 a_net = sctp_find_alternate_net(stcb, net, 2);
6763 a_net = sctp_find_alternate_net(stcb, net, 0);
6765 if ((a_net != net) &&
6766 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
6768 * We only proceed if a valid alternate is found that is not
6769 * this one and is reachable. Here we must move all chunks
6770 * queued in the send queue off of the destination address
6773 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
6774 if (chk->whoTo == net) {
6775 /* Move the chunk to our alternate */
6776 sctp_free_remote_addr(chk->whoTo);
6778 atomic_add_int(&a_net->ref_count, 1);
6785 sctp_med_chunk_output(struct sctp_inpcb *inp,
6786 struct sctp_tcb *stcb,
6787 struct sctp_association *asoc,
6790 int control_only, int *cwnd_full, int from_where,
6791 struct timeval *now, int *now_filled, int frag_point)
6794 * Ok this is the generic chunk service queue. we must do the
6795 * following: - Service the stream queue that is next, moving any
6796 * message (note I must get a complete message i.e. FIRST/MIDDLE and
6797 * LAST to the out queue in one pass) and assigning TSN's - Check to
6798 * see if the cwnd/rwnd allows any output, if so we go ahead and
6799 * fomulate and send the low level chunks. Making sure to combine
6800 * any control in the control chunk queue also.
6802 struct sctp_nets *net;
6803 struct mbuf *outchain, *endoutchain;
6804 struct sctp_tmit_chunk *chk, *nchk;
6805 struct sctphdr *shdr;
6807 /* temp arrays for unlinking */
6808 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6809 int no_fragmentflg, error;
6810 int one_chunk, hbflag, skip_data_for_this_net;
6811 int asconf, cookie, no_out_cnt;
6812 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
6813 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
6814 struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
6816 uint32_t auth_offset = 0;
6817 struct sctp_auth_chunk *auth = NULL;
6820 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
6829 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6830 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
6831 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
6836 ctl_cnt = no_out_cnt = asconf = cookie = 0;
6838 * First lets prime the pump. For each destination, if there is room
6839 * in the flight size, attempt to pull an MTU's worth out of the
6840 * stream queues into the general send_queue
6842 #ifdef SCTP_AUDITING_ENABLED
6843 sctp_audit_log(0xC2, 2);
6845 SCTP_TCB_LOCK_ASSERT(stcb);
6847 if ((control_only) || (asoc->stream_reset_outstanding))
6852 /* Nothing to possible to send? */
6853 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
6854 TAILQ_EMPTY(&asoc->send_queue) &&
6855 TAILQ_EMPTY(&asoc->out_wheel)) {
6859 if (asoc->peers_rwnd == 0) {
6860 /* No room in peers rwnd */
6863 if (asoc->total_flight > 0) {
6864 /* we are allowed one chunk in flight */
6868 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
6869 if (sctp_cmt_on_off) {
6871 * for CMT we start at the next one past the one we
6872 * last added data to.
6874 if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
6875 goto skip_the_fill_from_streams;
6877 if (asoc->last_net_data_came_from) {
6878 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
6880 net = TAILQ_FIRST(&asoc->nets);
6884 net = TAILQ_FIRST(&asoc->nets);
6888 net = asoc->primary_destination;
6891 net = TAILQ_FIRST(&asoc->nets);
6896 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
6897 net->window_probe = 0;
6898 if (old_startat && (old_startat == net)) {
6901 if ((sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
6902 /* nothing can be in queue for this guy */
6905 if (net->flight_size >= net->cwnd) {
6906 /* skip this network, no room */
6911 * @@@ JRI : this for loop we are in takes in each
6912 * net, if its's got space in cwnd and has data sent
6913 * to it (when CMT is off) then it calls
6914 * sctp_fill_outqueue for the net. This gets data on
6915 * the send queue for that network.
6917 * In sctp_fill_outqueue TSN's are assigned and data is
6918 * copied out of the stream buffers. Note mostly
6919 * copy by reference (we hope).
6921 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
6922 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
6924 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
6926 /* memory alloc failure */
6928 goto skip_the_fill_from_streams;
6931 if (start_at != TAILQ_FIRST(&asoc->nets)) {
6932 /* got to pick up the beginning stuff. */
6933 old_startat = start_at;
6934 start_at = net = TAILQ_FIRST(&asoc->nets);
6939 skip_the_fill_from_streams:
6940 *cwnd_full = cwnd_full_ind;
6941 /* now service each destination and send out what we can for it */
6942 /* Nothing to send? */
6943 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
6944 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
6948 if (no_data_chunks) {
6949 chk = TAILQ_FIRST(&asoc->control_send_queue);
6951 chk = TAILQ_FIRST(&asoc->send_queue);
6954 send_start_at = chk->whoTo;
6956 send_start_at = TAILQ_FIRST(&asoc->nets);
6959 again_one_more_time:
6960 for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
6961 /* how much can we send? */
6962 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
6963 if (old_startat && (old_startat == net)) {
6964 /* through list ocmpletely. */
6968 if (net->ref_count < 2) {
6970 * Ref-count of 1 so we cannot have data or control
6971 * queued to this address. Skip it.
6975 ctl_cnt = bundle_at = 0;
6976 endoutchain = outchain = NULL;
6979 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6980 skip_data_for_this_net = 1;
6982 skip_data_for_this_net = 0;
6984 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
6986 * if we have a route and an ifp check to see if we
6987 * have room to send to this guy
6991 ifp = net->ro.ro_rt->rt_ifp;
6992 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
6993 SCTP_STAT_INCR(sctps_ifnomemqueued);
6994 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
6995 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7000 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
7001 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7003 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7007 if (mtu > asoc->peers_rwnd) {
7008 if (asoc->total_flight > 0) {
7009 /* We have a packet in flight somewhere */
7010 r_mtu = asoc->peers_rwnd;
7012 /* We are always allowed to send one MTU out */
7019 /************************/
7020 /* Control transmission */
7021 /************************/
7022 /* Now first lets go through the control queue */
7023 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7025 nchk = TAILQ_NEXT(chk, sctp_next);
7026 if (chk->whoTo != net) {
7028 * No, not sent to the network we are
7033 if (chk->data == NULL) {
7036 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7038 * It must be unsent. Cookies and ASCONF's
7039 * hang around but there timers will force
7040 * when marked for resend.
7045 * if no AUTH is yet included and this chunk
7046 * requires it, make sure to account for it. We
7047 * don't apply the size until the AUTH chunk is
7048 * actually added below in case there is no room for
7049 * this chunk. NOTE: we overload the use of "omtu"
7052 if ((auth == NULL) &&
7053 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7054 stcb->asoc.peer_auth_chunks)) {
7055 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7058 /* Here we do NOT factor the r_mtu */
7059 if ((chk->send_size < (int)(mtu - omtu)) ||
7060 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7062 * We probably should glom the mbuf chain
7063 * from the chk->data for control but the
7064 * problem is it becomes yet one more level
7065 * of tracking to do if for some reason
7066 * output fails. Then I have got to
7067 * reconstruct the merged control chain.. el
7068 * yucko.. for now we take the easy way and
7072 * Add an AUTH chunk, if chunk requires it
7073 * save the offset into the chain for AUTH
7075 if ((auth == NULL) &&
7076 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7077 stcb->asoc.peer_auth_chunks))) {
7078 outchain = sctp_add_auth_chunk(outchain,
7083 chk->rec.chunk_id.id);
7084 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7086 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7087 (int)chk->rec.chunk_id.can_take_data,
7088 chk->send_size, chk->copy_by_ref);
7089 if (outchain == NULL) {
7091 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
7094 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7095 /* update our MTU size */
7096 if (mtu > (chk->send_size + omtu))
7097 mtu -= (chk->send_size + omtu);
7100 to_out += (chk->send_size + omtu);
7101 /* Do clear IP_DF ? */
7102 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7105 if (chk->rec.chunk_id.can_take_data)
7107 /* Mark things to be removed, if needed */
7108 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7109 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7110 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7111 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7112 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7113 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7114 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7115 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7116 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7117 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7119 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7122 * JRS 5/14/07 - Set the
7123 * flag to say a heartbeat
7128 /* remove these chunks at the end */
7129 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7130 /* turn off the timer */
7131 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7132 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7133 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7139 * Other chunks, since they have
7140 * timers running (i.e. COOKIE or
7141 * ASCONF) we just "trust" that it
7142 * gets sent or retransmitted.
7145 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7148 } else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
7150 * set hb flag since we can
7156 * should sysctl this: don't
7157 * bundle data with ASCONF
7158 * since it requires AUTH
7162 chk->sent = SCTP_DATAGRAM_SENT;
7167 * Ok we are out of room but we can
7168 * output without effecting the
7169 * flight size since this little guy
7170 * is a control only packet.
7173 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7175 * do NOT clear the asconf
7176 * flag as it is used to do
7177 * appropriate source
7178 * address selection.
7182 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7185 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7186 if (outchain == NULL) {
7189 goto error_out_again;
7191 shdr = mtod(outchain, struct sctphdr *);
7192 shdr->src_port = inp->sctp_lport;
7193 shdr->dest_port = stcb->rport;
7194 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7196 auth_offset += sizeof(struct sctphdr);
7197 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7198 (struct sockaddr *)&net->ro._l_addr,
7199 outchain, auth_offset, auth,
7200 no_fragmentflg, 0, NULL, asconf))) {
7201 if (error == ENOBUFS) {
7202 asoc->ifp_had_enobuf = 1;
7203 SCTP_STAT_INCR(sctps_lowlevelerr);
7205 if (from_where == 0) {
7206 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7209 /* error, could not output */
7211 if (*now_filled == 0) {
7212 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7214 *now = net->last_sent_time;
7216 net->last_sent_time = *now;
7220 if (error == EHOSTUNREACH) {
7226 sctp_move_to_an_alt(stcb, asoc, net);
7231 asoc->ifp_had_enobuf = 0;
7232 /* Only HB or ASCONF advances time */
7234 if (*now_filled == 0) {
7235 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7237 *now = net->last_sent_time;
7239 net->last_sent_time = *now;
7244 * increase the number we sent, if a
7245 * cookie is sent we don't tell them
7248 outchain = endoutchain = NULL;
7252 *num_out += ctl_cnt;
7253 /* recalc a clean slate and setup */
7254 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7255 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7257 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7264 /*********************/
7265 /* Data transmission */
7266 /*********************/
7268 * if AUTH for DATA is required and no AUTH has been added
7269 * yet, account for this in the mtu now... if no data can be
7270 * bundled, this adjustment won't matter anyways since the
7271 * packet will be going out...
7273 if ((auth == NULL) &&
7274 sctp_auth_is_required_chunk(SCTP_DATA,
7275 stcb->asoc.peer_auth_chunks)) {
7276 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7278 /* now lets add any data within the MTU constraints */
7279 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
7280 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
7281 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7285 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
7286 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7290 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
7292 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
7293 if (no_data_chunks) {
7294 /* let only control go out */
7298 if (net->flight_size >= net->cwnd) {
7299 /* skip this net, no room for data */
7303 nchk = TAILQ_NEXT(chk, sctp_next);
7304 if (chk->whoTo != net) {
7305 /* No, not sent to this net */
7308 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
7310 * strange, we have a chunk that is
7311 * to big for its destination and
7312 * yet no fragment ok flag.
7313 * Something went wrong when the
7314 * PMTU changed...we did not mark
7315 * this chunk for some reason?? I
7316 * will fix it here by letting IP
7317 * fragment it for now and printing
7318 * a warning. This really should not
7321 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
7322 chk->send_size, mtu);
7323 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
7325 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
7326 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
7327 /* ok we will add this one */
7330 * Add an AUTH chunk, if chunk
7331 * requires it, save the offset into
7332 * the chain for AUTH
7334 if ((auth == NULL) &&
7335 (sctp_auth_is_required_chunk(SCTP_DATA,
7336 stcb->asoc.peer_auth_chunks))) {
7338 outchain = sctp_add_auth_chunk(outchain,
7344 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7346 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
7347 chk->send_size, chk->copy_by_ref);
7348 if (outchain == NULL) {
7349 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
7350 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
7351 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7354 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
7357 /* upate our MTU size */
7358 /* Do clear IP_DF ? */
7359 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7362 /* unsigned subtraction of mtu */
7363 if (mtu > chk->send_size)
7364 mtu -= chk->send_size;
7367 /* unsigned subtraction of r_mtu */
7368 if (r_mtu > chk->send_size)
7369 r_mtu -= chk->send_size;
7373 to_out += chk->send_size;
7374 if ((to_out > mx_mtu) && no_fragmentflg) {
7376 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
7378 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
7382 chk->window_probe = 0;
7383 data_list[bundle_at++] = chk;
7384 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
7388 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
7389 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
7390 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
7392 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
7394 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
7395 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
7405 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
7407 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
7409 data_list[0]->window_probe = 1;
7410 net->window_probe = 1;
7416 * Must be sent in order of the
7417 * TSN's (on a network)
7421 } /* for (chunk gather loop for this net) */
7422 } /* if asoc.state OPEN */
7423 /* Is there something to send for this destination? */
7425 /* We may need to start a control timer or two */
7427 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
7430 * do NOT clear the asconf flag as it is
7431 * used to do appropriate source address
7436 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7439 /* must start a send timer if data is being sent */
7440 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7442 * no timer running on this destination
7445 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7446 } else if (sctp_cmt_on_off && sctp_cmt_pf && pf_hbflag && ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)
7447 && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
7449 * JRS 5/14/07 - If a HB has been sent to a
7450 * PF destination and no T3 timer is
7451 * currently running, start the T3 timer to
7452 * track the HBs that were sent.
7454 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7456 /* Now send it, if there is anything to send :> */
7457 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7458 if (outchain == NULL) {
7463 shdr = mtod(outchain, struct sctphdr *);
7464 shdr->src_port = inp->sctp_lport;
7465 shdr->dest_port = stcb->rport;
7466 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7468 auth_offset += sizeof(struct sctphdr);
7469 if ((error = sctp_lowlevel_chunk_output(inp,
7472 (struct sockaddr *)&net->ro._l_addr,
7480 /* error, we could not output */
7481 if (error == ENOBUFS) {
7482 SCTP_STAT_INCR(sctps_lowlevelerr);
7483 asoc->ifp_had_enobuf = 1;
7485 if (from_where == 0) {
7486 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7489 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
7491 if (*now_filled == 0) {
7492 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7494 *now = net->last_sent_time;
7496 net->last_sent_time = *now;
7500 if (error == EHOSTUNREACH) {
7502 * Destination went unreachable
7505 sctp_move_to_an_alt(stcb, asoc, net);
7509 * I add this line to be paranoid. As far as
7510 * I can tell the continue, takes us back to
7511 * the top of the for, but just to make sure
7512 * I will reset these again here.
7514 ctl_cnt = bundle_at = 0;
7515 continue; /* This takes us back to the
7516 * for() for the nets. */
7518 asoc->ifp_had_enobuf = 0;
7520 outchain = endoutchain = NULL;
7523 if (bundle_at || hbflag) {
7524 /* For data/asconf and hb set time */
7525 if (*now_filled == 0) {
7526 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7528 *now = net->last_sent_time;
7530 net->last_sent_time = *now;
7534 *num_out += (ctl_cnt + bundle_at);
7537 /* setup for a RTO measurement */
7538 tsns_sent = data_list[0]->rec.data.TSN_seq;
7539 /* fill time if not already filled */
7540 if (*now_filled == 0) {
7541 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
7543 *now = asoc->time_last_sent;
7545 asoc->time_last_sent = *now;
7547 data_list[0]->do_rtt = 1;
7548 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
7549 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
7550 if (sctp_early_fr) {
7551 if (net->flight_size < net->cwnd) {
7552 /* start or restart it */
7553 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7554 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7555 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
7557 SCTP_STAT_INCR(sctps_earlyfrstrout);
7558 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
7560 /* stop it if its running */
7561 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7562 SCTP_STAT_INCR(sctps_earlyfrstpout);
7563 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
7564 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
7573 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7574 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
7577 if (old_startat == NULL) {
7578 old_startat = send_start_at;
7579 send_start_at = TAILQ_FIRST(&asoc->nets);
7581 goto again_one_more_time;
7584 * At the end there should be no NON timed chunks hanging on this
7587 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
7588 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
7590 if ((*num_out == 0) && (*reason_code == 0)) {
7595 sctp_clean_up_ctl(stcb, asoc);
7600 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
7603 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
7604 * the control chunk queue.
7606 struct sctp_chunkhdr *hdr;
7607 struct sctp_tmit_chunk *chk;
7610 SCTP_TCB_LOCK_ASSERT(stcb);
7611 sctp_alloc_a_chunk(stcb, chk);
7614 sctp_m_freem(op_err);
7617 chk->copy_by_ref = 0;
7618 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
7619 if (op_err == NULL) {
7620 sctp_free_a_chunk(stcb, chk);
7625 while (mat != NULL) {
7626 chk->send_size += SCTP_BUF_LEN(mat);
7627 mat = SCTP_BUF_NEXT(mat);
7629 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
7630 chk->rec.chunk_id.can_take_data = 1;
7631 chk->sent = SCTP_DATAGRAM_UNSENT;
7634 chk->asoc = &stcb->asoc;
7636 chk->whoTo = chk->asoc->primary_destination;
7637 atomic_add_int(&chk->whoTo->ref_count, 1);
7638 hdr = mtod(op_err, struct sctp_chunkhdr *);
7639 hdr->chunk_type = SCTP_OPERATION_ERROR;
7640 hdr->chunk_flags = 0;
7641 hdr->chunk_length = htons(chk->send_size);
7642 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
7645 chk->asoc->ctrl_queue_cnt++;
7649 sctp_send_cookie_echo(struct mbuf *m,
7651 struct sctp_tcb *stcb,
7652 struct sctp_nets *net)
7655 * pull out the cookie and put it at the front of the control chunk
7659 struct mbuf *cookie;
7660 struct sctp_paramhdr parm, *phdr;
7661 struct sctp_chunkhdr *hdr;
7662 struct sctp_tmit_chunk *chk;
7663 uint16_t ptype, plen;
7665 /* First find the cookie in the param area */
7667 at = offset + sizeof(struct sctp_init_chunk);
7669 SCTP_TCB_LOCK_ASSERT(stcb);
7671 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
7675 ptype = ntohs(phdr->param_type);
7676 plen = ntohs(phdr->param_length);
7677 if (ptype == SCTP_STATE_COOKIE) {
7680 /* found the cookie */
7681 if ((pad = (plen % 4))) {
7684 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
7685 if (cookie == NULL) {
7691 at += SCTP_SIZE32(plen);
7693 if (cookie == NULL) {
7694 /* Did not find the cookie */
7697 /* ok, we got the cookie lets change it into a cookie echo chunk */
7699 /* first the change from param to cookie */
7700 hdr = mtod(cookie, struct sctp_chunkhdr *);
7701 hdr->chunk_type = SCTP_COOKIE_ECHO;
7702 hdr->chunk_flags = 0;
7703 /* get the chunk stuff now and place it in the FRONT of the queue */
7704 sctp_alloc_a_chunk(stcb, chk);
7707 sctp_m_freem(cookie);
7710 chk->copy_by_ref = 0;
7711 chk->send_size = plen;
7712 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
7713 chk->rec.chunk_id.can_take_data = 0;
7714 chk->sent = SCTP_DATAGRAM_UNSENT;
7717 chk->asoc = &stcb->asoc;
7719 chk->whoTo = chk->asoc->primary_destination;
7720 atomic_add_int(&chk->whoTo->ref_count, 1);
7721 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
7722 chk->asoc->ctrl_queue_cnt++;
7727 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
7731 struct sctp_nets *net)
7734 * take a HB request and make it into a HB ack and send it.
7736 struct mbuf *outchain;
7737 struct sctp_chunkhdr *chdr;
7738 struct sctp_tmit_chunk *chk;
7742 /* must have a net pointer */
7745 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
7746 if (outchain == NULL) {
7747 /* gak out of memory */
7750 chdr = mtod(outchain, struct sctp_chunkhdr *);
7751 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
7752 chdr->chunk_flags = 0;
7753 if (chk_length % 4) {
7755 uint32_t cpthis = 0;
7758 padlen = 4 - (chk_length % 4);
7759 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
7761 sctp_alloc_a_chunk(stcb, chk);
7764 sctp_m_freem(outchain);
7767 chk->copy_by_ref = 0;
7768 chk->send_size = chk_length;
7769 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
7770 chk->rec.chunk_id.can_take_data = 1;
7771 chk->sent = SCTP_DATAGRAM_UNSENT;
7774 chk->asoc = &stcb->asoc;
7775 chk->data = outchain;
7777 atomic_add_int(&chk->whoTo->ref_count, 1);
7778 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7779 chk->asoc->ctrl_queue_cnt++;
7783 sctp_send_cookie_ack(struct sctp_tcb *stcb)
7785 /* formulate and queue a cookie-ack back to sender */
7786 struct mbuf *cookie_ack;
7787 struct sctp_chunkhdr *hdr;
7788 struct sctp_tmit_chunk *chk;
7791 SCTP_TCB_LOCK_ASSERT(stcb);
7793 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
7794 if (cookie_ack == NULL) {
7798 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
7799 sctp_alloc_a_chunk(stcb, chk);
7802 sctp_m_freem(cookie_ack);
7805 chk->copy_by_ref = 0;
7806 chk->send_size = sizeof(struct sctp_chunkhdr);
7807 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
7808 chk->rec.chunk_id.can_take_data = 1;
7809 chk->sent = SCTP_DATAGRAM_UNSENT;
7812 chk->asoc = &stcb->asoc;
7813 chk->data = cookie_ack;
7814 if (chk->asoc->last_control_chunk_from != NULL) {
7815 chk->whoTo = chk->asoc->last_control_chunk_from;
7817 chk->whoTo = chk->asoc->primary_destination;
7819 atomic_add_int(&chk->whoTo->ref_count, 1);
7820 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
7821 hdr->chunk_type = SCTP_COOKIE_ACK;
7822 hdr->chunk_flags = 0;
7823 hdr->chunk_length = htons(chk->send_size);
7824 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
7825 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7826 chk->asoc->ctrl_queue_cnt++;
7832 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
7834 /* formulate and queue a SHUTDOWN-ACK back to the sender */
7835 struct mbuf *m_shutdown_ack;
7836 struct sctp_shutdown_ack_chunk *ack_cp;
7837 struct sctp_tmit_chunk *chk;
7839 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
7840 if (m_shutdown_ack == NULL) {
7844 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
7845 sctp_alloc_a_chunk(stcb, chk);
7848 sctp_m_freem(m_shutdown_ack);
7851 chk->copy_by_ref = 0;
7853 chk->send_size = sizeof(struct sctp_chunkhdr);
7854 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
7855 chk->rec.chunk_id.can_take_data = 1;
7856 chk->sent = SCTP_DATAGRAM_UNSENT;
7859 chk->asoc = &stcb->asoc;
7860 chk->data = m_shutdown_ack;
7862 atomic_add_int(&net->ref_count, 1);
7864 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
7865 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
7866 ack_cp->ch.chunk_flags = 0;
7867 ack_cp->ch.chunk_length = htons(chk->send_size);
7868 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
7869 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7870 chk->asoc->ctrl_queue_cnt++;
7875 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
7877 /* formulate and queue a SHUTDOWN to the sender */
7878 struct mbuf *m_shutdown;
7879 struct sctp_shutdown_chunk *shutdown_cp;
7880 struct sctp_tmit_chunk *chk;
7882 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
7883 if (m_shutdown == NULL) {
7887 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
7888 sctp_alloc_a_chunk(stcb, chk);
7891 sctp_m_freem(m_shutdown);
7894 chk->copy_by_ref = 0;
7895 chk->send_size = sizeof(struct sctp_shutdown_chunk);
7896 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
7897 chk->rec.chunk_id.can_take_data = 1;
7898 chk->sent = SCTP_DATAGRAM_UNSENT;
7901 chk->asoc = &stcb->asoc;
7902 chk->data = m_shutdown;
7904 atomic_add_int(&net->ref_count, 1);
7906 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
7907 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
7908 shutdown_cp->ch.chunk_flags = 0;
7909 shutdown_cp->ch.chunk_length = htons(chk->send_size);
7910 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
7911 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
7912 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7913 chk->asoc->ctrl_queue_cnt++;
7918 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
7921 * formulate and queue an ASCONF to the peer. ASCONF parameters
7922 * should be queued on the assoc queue.
7924 struct sctp_tmit_chunk *chk;
7925 struct mbuf *m_asconf;
7926 struct sctp_asconf_chunk *acp;
7929 SCTP_TCB_LOCK_ASSERT(stcb);
7930 /* compose an ASCONF chunk, maximum length is PMTU */
7931 m_asconf = sctp_compose_asconf(stcb, &len);
7932 if (m_asconf == NULL) {
7935 acp = mtod(m_asconf, struct sctp_asconf_chunk *);
7936 sctp_alloc_a_chunk(stcb, chk);
7939 sctp_m_freem(m_asconf);
7942 chk->copy_by_ref = 0;
7943 chk->data = m_asconf;
7944 chk->send_size = len;
7945 chk->rec.chunk_id.id = SCTP_ASCONF;
7946 chk->rec.chunk_id.can_take_data = 0;
7947 chk->sent = SCTP_DATAGRAM_UNSENT;
7950 chk->asoc = &stcb->asoc;
7951 chk->whoTo = chk->asoc->primary_destination;
7952 atomic_add_int(&chk->whoTo->ref_count, 1);
7953 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
7954 chk->asoc->ctrl_queue_cnt++;
7959 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
7962 * formulate and queue a asconf-ack back to sender. the asconf-ack
7963 * must be stored in the tcb.
7965 struct sctp_tmit_chunk *chk;
7966 struct mbuf *m_ack, *m;
7968 SCTP_TCB_LOCK_ASSERT(stcb);
7969 /* is there a asconf-ack mbuf chain to send? */
7970 if (stcb->asoc.last_asconf_ack_sent == NULL) {
7973 /* copy the asconf_ack */
7974 m_ack = SCTP_M_COPYM(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL,
7976 if (m_ack == NULL) {
7977 /* couldn't copy it */
7980 sctp_alloc_a_chunk(stcb, chk);
7984 sctp_m_freem(m_ack);
7987 chk->copy_by_ref = 0;
7988 /* figure out where it goes to */
7990 /* we're doing a retransmission */
7991 if (stcb->asoc.used_alt_asconfack > 2) {
7992 /* tried alternate nets already, go back */
7995 /* need to try and alternate net */
7996 chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
7997 stcb->asoc.used_alt_asconfack++;
7999 if (chk->whoTo == NULL) {
8001 if (stcb->asoc.last_control_chunk_from == NULL)
8002 chk->whoTo = stcb->asoc.primary_destination;
8004 chk->whoTo = stcb->asoc.last_control_chunk_from;
8005 stcb->asoc.used_alt_asconfack = 0;
8009 if (stcb->asoc.last_control_chunk_from == NULL)
8010 chk->whoTo = stcb->asoc.primary_destination;
8012 chk->whoTo = stcb->asoc.last_control_chunk_from;
8013 stcb->asoc.used_alt_asconfack = 0;
8020 chk->send_size += SCTP_BUF_LEN(m);
8021 m = SCTP_BUF_NEXT(m);
8023 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8024 chk->rec.chunk_id.can_take_data = 1;
8025 chk->sent = SCTP_DATAGRAM_UNSENT;
8028 chk->asoc = &stcb->asoc;
8029 atomic_add_int(&chk->whoTo->ref_count, 1);
8030 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8031 chk->asoc->ctrl_queue_cnt++;
8037 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8038 struct sctp_tcb *stcb,
8039 struct sctp_association *asoc,
8040 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done)
8043 * send out one MTU of retransmission. If fast_retransmit is
8044 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8045 * rwnd. For a Cookie or Asconf in the control chunk queue we
8046 * retransmit them by themselves.
8048 * For data chunks we will pick out the lowest TSN's in the sent_queue
8049 * marked for resend and bundle them all together (up to a MTU of
8050 * destination). The address to send to should have been
8051 * selected/changed where the retransmission was marked (i.e. in FR
8052 * or t3-timeout routines).
8054 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8055 struct sctp_tmit_chunk *chk, *fwd;
8056 struct mbuf *m, *endofchain;
8057 struct sctphdr *shdr;
8059 struct sctp_nets *net;
8060 uint32_t tsns_sent = 0;
8061 int no_fragmentflg, bundle_at, cnt_thru;
8063 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8064 struct sctp_auth_chunk *auth = NULL;
8065 uint32_t auth_offset = 0;
8068 SCTP_TCB_LOCK_ASSERT(stcb);
8069 tmr_started = ctl_cnt = bundle_at = error = 0;
8075 endofchain = m = NULL;
8076 #ifdef SCTP_AUDITING_ENABLED
8077 sctp_audit_log(0xC3, 1);
8079 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8080 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8081 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8082 asoc->sent_queue_retran_cnt);
8083 asoc->sent_queue_cnt = 0;
8084 asoc->sent_queue_cnt_removeable = 0;
8085 /* send back 0/0 so we enter normal transmission */
8089 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8090 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8091 (chk->rec.chunk_id.id == SCTP_ASCONF) ||
8092 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8093 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8094 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8095 if (chk != asoc->str_reset) {
8097 * not eligible for retran if its
8104 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8108 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8113 * Add an AUTH chunk, if chunk requires it save the
8114 * offset into the chain for AUTH
8116 if ((auth == NULL) &&
8117 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8118 stcb->asoc.peer_auth_chunks))) {
8119 m = sctp_add_auth_chunk(m, &endofchain,
8120 &auth, &auth_offset,
8122 chk->rec.chunk_id.id);
8124 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8130 /* do we have control chunks to retransmit? */
8132 /* Start a timer no matter if we suceed or fail */
8133 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8134 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8135 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8136 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8138 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8142 shdr = mtod(m, struct sctphdr *);
8143 shdr->src_port = inp->sctp_lport;
8144 shdr->dest_port = stcb->rport;
8145 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8147 auth_offset += sizeof(struct sctphdr);
8148 chk->snd_count++; /* update our count */
8150 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8151 (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8152 auth, no_fragmentflg, 0, NULL, asconf))) {
8153 SCTP_STAT_INCR(sctps_lowlevelerr);
8156 m = endofchain = NULL;
8160 * We don't want to mark the net->sent time here since this
8161 * we use this for HB and retrans cannot measure RTT
8163 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8165 chk->sent = SCTP_DATAGRAM_SENT;
8166 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8170 /* Clean up the fwd-tsn list */
8171 sctp_clean_up_ctl(stcb, asoc);
8176 * Ok, it is just data retransmission we need to do or that and a
8177 * fwd-tsn with it all.
8179 if (TAILQ_EMPTY(&asoc->sent_queue)) {
8180 return (SCTP_RETRAN_DONE);
8182 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8183 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8184 /* not yet open, resend the cookie and that is it */
8187 #ifdef SCTP_AUDITING_ENABLED
8188 sctp_auditing(20, inp, stcb, NULL);
8190 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8191 if (chk->sent != SCTP_DATAGRAM_RESEND) {
8192 /* No, not sent to this net or not ready for rtx */
8195 if ((sctp_max_retran_chunk) && (chk->snd_count >= sctp_max_retran_chunk)) {
8196 /* Gak, we have exceeded max unlucky retran, abort! */
8197 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8199 sctp_max_retran_chunk);
8200 sctp_send_abort_tcb(stcb, NULL);
8201 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
8202 return (SCTP_RETRAN_EXIT);
8204 /* pick up the net */
8206 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8207 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8209 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8212 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8213 /* No room in peers rwnd */
8216 tsn = asoc->last_acked_seq + 1;
8217 if (tsn == chk->rec.data.TSN_seq) {
8219 * we make a special exception for this
8220 * case. The peer has no rwnd but is missing
8221 * the lowest chunk.. which is probably what
8222 * is holding up the rwnd.
8224 goto one_chunk_around;
8229 if (asoc->peers_rwnd < mtu) {
8231 if ((asoc->peers_rwnd == 0) &&
8232 (asoc->total_flight == 0)) {
8233 chk->window_probe = 1;
8234 chk->whoTo->window_probe = 1;
8237 #ifdef SCTP_AUDITING_ENABLED
8238 sctp_audit_log(0xC3, 2);
8242 net->fast_retran_ip = 0;
8243 if (chk->rec.data.doing_fast_retransmit == 0) {
8245 * if no FR in progress skip destination that have
8246 * flight_size > cwnd.
8248 if (net->flight_size >= net->cwnd) {
8253 * Mark the destination net to have FR recovery
8257 net->fast_retran_ip = 1;
8261 * if no AUTH is yet included and this chunk requires it,
8262 * make sure to account for it. We don't apply the size
8263 * until the AUTH chunk is actually added below in case
8264 * there is no room for this chunk.
8266 if ((auth == NULL) &&
8267 sctp_auth_is_required_chunk(SCTP_DATA,
8268 stcb->asoc.peer_auth_chunks)) {
8269 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8273 if ((chk->send_size <= (mtu - dmtu)) ||
8274 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8275 /* ok we will add this one */
8276 if ((auth == NULL) &&
8277 (sctp_auth_is_required_chunk(SCTP_DATA,
8278 stcb->asoc.peer_auth_chunks))) {
8279 m = sctp_add_auth_chunk(m, &endofchain,
8280 &auth, &auth_offset,
8283 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8285 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
8288 /* Do clear IP_DF ? */
8289 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8292 /* upate our MTU size */
8293 if (mtu > (chk->send_size + dmtu))
8294 mtu -= (chk->send_size + dmtu);
8297 data_list[bundle_at++] = chk;
8298 if (one_chunk && (asoc->total_flight <= 0)) {
8299 SCTP_STAT_INCR(sctps_windowprobed);
8302 if (one_chunk == 0) {
8304 * now are there anymore forward from chk to pick
8307 fwd = TAILQ_NEXT(chk, sctp_next);
8309 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
8310 /* Nope, not for retran */
8311 fwd = TAILQ_NEXT(fwd, sctp_next);
8314 if (fwd->whoTo != net) {
8315 /* Nope, not the net in question */
8316 fwd = TAILQ_NEXT(fwd, sctp_next);
8319 if ((auth == NULL) &&
8320 sctp_auth_is_required_chunk(SCTP_DATA,
8321 stcb->asoc.peer_auth_chunks)) {
8322 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8325 if (fwd->send_size <= (mtu - dmtu)) {
8326 if ((auth == NULL) &&
8327 (sctp_auth_is_required_chunk(SCTP_DATA,
8328 stcb->asoc.peer_auth_chunks))) {
8329 m = sctp_add_auth_chunk(m,
8331 &auth, &auth_offset,
8335 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
8337 SCTP_LTRACE_ERR(inp, stcb, ENOMEM, 0);
8340 /* Do clear IP_DF ? */
8341 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8344 /* upate our MTU size */
8345 if (mtu > (fwd->send_size + dmtu))
8346 mtu -= (fwd->send_size + dmtu);
8349 data_list[bundle_at++] = fwd;
8350 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8353 fwd = TAILQ_NEXT(fwd, sctp_next);
8355 /* can't fit so we are done */
8360 /* Is there something to send for this destination? */
8363 * No matter if we fail/or suceed we should start a
8364 * timer. A failure is like a lost IP packet :-)
8366 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8368 * no timer running on this destination
8371 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8374 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8378 shdr = mtod(m, struct sctphdr *);
8379 shdr->src_port = inp->sctp_lport;
8380 shdr->dest_port = stcb->rport;
8381 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8383 auth_offset += sizeof(struct sctphdr);
8384 /* Now lets send it, if there is anything to send :> */
8385 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8386 (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
8387 auth, no_fragmentflg, 0, NULL, asconf))) {
8388 /* error, we could not output */
8389 SCTP_STAT_INCR(sctps_lowlevelerr);
8392 m = endofchain = NULL;
8397 * We don't want to mark the net->sent time here
8398 * since this we use this for HB and retrans cannot
8401 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
8403 /* For auto-close */
8405 if (*now_filled == 0) {
8406 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8407 *now = asoc->time_last_sent;
8410 asoc->time_last_sent = *now;
8412 *cnt_out += bundle_at;
8413 #ifdef SCTP_AUDITING_ENABLED
8414 sctp_audit_log(0xC4, bundle_at);
8417 tsns_sent = data_list[0]->rec.data.TSN_seq;
8419 for (i = 0; i < bundle_at; i++) {
8420 SCTP_STAT_INCR(sctps_sendretransdata);
8421 data_list[i]->sent = SCTP_DATAGRAM_SENT;
8423 * When we have a revoked data, and we
8424 * retransmit it, then we clear the revoked
8425 * flag since this flag dictates if we
8426 * subtracted from the fs
8428 if (data_list[i]->rec.data.chunk_was_revoked) {
8429 /* Deflate the cwnd */
8430 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
8431 data_list[i]->rec.data.chunk_was_revoked = 0;
8433 data_list[i]->snd_count++;
8434 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8435 /* record the time */
8436 data_list[i]->sent_rcv_time = asoc->time_last_sent;
8437 if (data_list[i]->book_size_scale) {
8439 * need to double the book size on
8442 data_list[i]->book_size_scale = 0;
8444 * Since we double the booksize, we
8445 * must also double the output queue
8446 * size, since this get shrunk when
8447 * we free by this amount.
8449 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
8450 data_list[i]->book_size *= 2;
8454 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
8455 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8456 asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
8458 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8459 (uint32_t) (data_list[i]->send_size +
8460 sctp_peer_chunk_oh));
8462 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
8463 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
8464 data_list[i]->whoTo->flight_size,
8465 data_list[i]->book_size,
8466 (uintptr_t) data_list[i]->whoTo,
8467 data_list[i]->rec.data.TSN_seq);
8469 sctp_flight_size_increase(data_list[i]);
8470 sctp_total_flight_increase(stcb, data_list[i]);
8471 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8472 /* SWS sender side engages */
8473 asoc->peers_rwnd = 0;
8476 (data_list[i]->rec.data.doing_fast_retransmit)) {
8477 SCTP_STAT_INCR(sctps_sendfastretrans);
8478 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
8479 (tmr_started == 0)) {
8481 * ok we just fast-retrans'd
8482 * the lowest TSN, i.e the
8483 * first on the list. In
8484 * this case we want to give
8485 * some more time to get a
8486 * SACK back without a
8489 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
8490 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
8491 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8495 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8496 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
8498 #ifdef SCTP_AUDITING_ENABLED
8499 sctp_auditing(21, inp, stcb, NULL);
8505 if (asoc->sent_queue_retran_cnt <= 0) {
8506 /* all done we have no more to retran */
8507 asoc->sent_queue_retran_cnt = 0;
8511 /* No more room in rwnd */
8514 /* stop the for loop here. we sent out a packet */
8522 sctp_timer_validation(struct sctp_inpcb *inp,
8523 struct sctp_tcb *stcb,
8524 struct sctp_association *asoc,
8527 struct sctp_nets *net;
8529 /* Validate that a timer is running somewhere */
8530 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8531 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8532 /* Here is a timer */
8536 SCTP_TCB_LOCK_ASSERT(stcb);
8537 /* Gak, we did not have a timer somewhere */
8538 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
8539 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
8544 sctp_chunk_output(struct sctp_inpcb *inp,
8545 struct sctp_tcb *stcb,
8549 * Ok this is the generic chunk service queue. we must do the
8551 * - See if there are retransmits pending, if so we must
8553 * - Service the stream queue that is next, moving any
8554 * message (note I must get a complete message i.e.
8555 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
8557 * - Check to see if the cwnd/rwnd allows any output, if so we
8558 * go ahead and fomulate and send the low level chunks. Making sure
8559 * to combine any control in the control chunk queue also.
8561 struct sctp_association *asoc;
8562 struct sctp_nets *net;
8563 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
8564 burst_cnt = 0, burst_limit = 0;
8569 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8571 int fr_done, tot_frs = 0;
8574 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
8575 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
8581 SCTP_TCB_LOCK_ASSERT(stcb);
8583 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
8585 if ((un_sent <= 0) &&
8586 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
8587 (asoc->sent_queue_retran_cnt == 0)) {
8588 /* Nothing to do unless there is something to be sent left */
8592 * Do we have something to send, data or control AND a sack timer
8593 * running, if so piggy-back the sack.
8595 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8596 sctp_send_sack(stcb);
8597 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
8599 while (asoc->sent_queue_retran_cnt) {
8601 * Ok, it is retransmission time only, we send out only ONE
8602 * packet with a single call off to the retran code.
8604 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
8606 * Special hook for handling cookiess discarded
8607 * by peer that carried data. Send cookie-ack only
8608 * and then the next call with get the retran's.
8610 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
8611 &cwnd_full, from_where,
8612 &now, &now_filled, frag_point);
8614 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
8615 /* if its not from a HB then do it */
8617 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done);
8623 * its from any other place, we don't allow retran
8624 * output (only control)
8629 /* Can't send anymore */
8631 * now lets push out control by calling med-level
8632 * output once. this assures that we WILL send HB's
8635 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
8636 &cwnd_full, from_where,
8637 &now, &now_filled, frag_point);
8638 #ifdef SCTP_AUDITING_ENABLED
8639 sctp_auditing(8, inp, stcb, NULL);
8641 (void)sctp_timer_validation(inp, stcb, asoc, ret);
8646 * The count was off.. retran is not happening so do
8647 * the normal retransmission.
8649 #ifdef SCTP_AUDITING_ENABLED
8650 sctp_auditing(9, inp, stcb, NULL);
8652 if (ret == SCTP_RETRAN_EXIT) {
8657 if (from_where == SCTP_OUTPUT_FROM_T3) {
8658 /* Only one transmission allowed out of a timeout */
8659 #ifdef SCTP_AUDITING_ENABLED
8660 sctp_auditing(10, inp, stcb, NULL);
8662 /* Push out any control */
8663 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
8664 &now, &now_filled, frag_point);
8667 if (tot_frs > asoc->max_burst) {
8668 /* Hit FR burst limit */
8671 if ((num_out == 0) && (ret == 0)) {
8673 /* No more retrans to send */
8677 #ifdef SCTP_AUDITING_ENABLED
8678 sctp_auditing(12, inp, stcb, NULL);
8680 /* Check for bad destinations, if they exist move chunks around. */
8681 burst_limit = asoc->max_burst;
8682 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8683 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
8684 SCTP_ADDR_NOT_REACHABLE) {
8686 * if possible move things off of this address we
8687 * still may send below due to the dormant state but
8688 * we try to find an alternate address to send to
8689 * and if we have one we move all queued data on the
8690 * out wheel to this alternate address.
8692 if (net->ref_count > 1)
8693 sctp_move_to_an_alt(stcb, asoc, net);
8694 } else if (sctp_cmt_on_off && sctp_cmt_pf && ((net->dest_state & SCTP_ADDR_PF) ==
8697 * JRS 5/14/07 - If CMT PF is on and the current
8698 * destination is in PF state, move all queued data
8699 * to an alternate desination.
8701 if (net->ref_count > 1)
8702 sctp_move_to_an_alt(stcb, asoc, net);
8705 * if ((asoc->sat_network) || (net->addr_is_local))
8706 * { burst_limit = asoc->max_burst *
8707 * SCTP_SAT_NETWORK_BURST_INCR; }
8709 if (sctp_use_cwnd_based_maxburst) {
8710 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
8712 * JRS - Use the congestion control
8713 * given in the congestion control
8716 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
8717 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8718 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
8720 SCTP_STAT_INCR(sctps_maxburstqueued);
8722 net->fast_retran_ip = 0;
8724 if (net->flight_size == 0) {
8725 /* Should be decaying the cwnd here */
8735 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
8736 &reason_code, 0, &cwnd_full, from_where,
8737 &now, &now_filled, frag_point);
8739 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
8740 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8741 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
8743 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8744 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
8745 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
8749 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
8753 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8754 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
8756 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
8761 * When nagle is on, we look at how much is un_sent, then
8762 * if its smaller than an MTU and we have data in
8765 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8766 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count)
8767 * sizeof(struct sctp_data_chunk)));
8768 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
8769 (stcb->asoc.total_flight > 0)) {
8773 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
8774 TAILQ_EMPTY(&asoc->send_queue) &&
8775 TAILQ_EMPTY(&asoc->out_wheel)) {
8776 /* Nothing left to send */
8779 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
8780 /* Nothing left to send */
8783 } while (num_out && (sctp_use_cwnd_based_maxburst ||
8784 (burst_cnt < burst_limit)));
8786 if (sctp_use_cwnd_based_maxburst == 0) {
8787 if (burst_cnt >= burst_limit) {
8788 SCTP_STAT_INCR(sctps_maxburstqueued);
8789 asoc->burst_limit_applied = 1;
8790 if (sctp_logging_level & SCTP_LOG_MAXBURST_ENABLE) {
8791 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
8794 asoc->burst_limit_applied = 0;
8797 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
8798 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
8800 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
8804 * Now we need to clean up the control chunk chain if a ECNE is on
8805 * it. It must be marked as UNSENT again so next call will continue
8806 * to send it until such time that we get a CWR, to remove it.
8808 if (stcb->asoc.ecn_echo_cnt_onq)
8809 sctp_fix_ecn_echo(asoc);
8815 sctp_output(inp, m, addr, control, p, flags)
8816 struct sctp_inpcb *inp;
8818 struct sockaddr *addr;
8819 struct mbuf *control;
8826 if (inp->sctp_socket == NULL) {
8829 return (sctp_sosend(inp->sctp_socket,
8839 send_forward_tsn(struct sctp_tcb *stcb,
8840 struct sctp_association *asoc)
8842 struct sctp_tmit_chunk *chk;
8843 struct sctp_forward_tsn_chunk *fwdtsn;
8845 SCTP_TCB_LOCK_ASSERT(stcb);
8846 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8847 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8848 /* mark it to unsent */
8849 chk->sent = SCTP_DATAGRAM_UNSENT;
8851 /* Do we correct its output location? */
8852 if (chk->whoTo != asoc->primary_destination) {
8853 sctp_free_remote_addr(chk->whoTo);
8854 chk->whoTo = asoc->primary_destination;
8855 atomic_add_int(&chk->whoTo->ref_count, 1);
8857 goto sctp_fill_in_rest;
8860 /* Ok if we reach here we must build one */
8861 sctp_alloc_a_chunk(stcb, chk);
8865 chk->copy_by_ref = 0;
8866 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
8867 chk->rec.chunk_id.can_take_data = 0;
8871 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
8872 if (chk->data == NULL) {
8873 sctp_free_a_chunk(stcb, chk);
8876 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
8877 chk->sent = SCTP_DATAGRAM_UNSENT;
8879 chk->whoTo = asoc->primary_destination;
8880 atomic_add_int(&chk->whoTo->ref_count, 1);
8881 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
8882 asoc->ctrl_queue_cnt++;
8885 * Here we go through and fill out the part that deals with
8886 * stream/seq of the ones we skip.
8888 SCTP_BUF_LEN(chk->data) = 0;
8890 struct sctp_tmit_chunk *at, *tp1, *last;
8891 struct sctp_strseq *strseq;
8892 unsigned int cnt_of_space, i, ovh;
8893 unsigned int space_needed;
8894 unsigned int cnt_of_skipped = 0;
8896 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
8897 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
8898 /* no more to look at */
8901 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
8902 /* We don't report these */
8907 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
8908 (cnt_of_skipped * sizeof(struct sctp_strseq)));
8910 cnt_of_space = M_TRAILINGSPACE(chk->data);
8912 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8913 ovh = SCTP_MIN_OVERHEAD;
8915 ovh = SCTP_MIN_V4_OVERHEAD;
8917 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
8918 /* trim to a mtu size */
8919 cnt_of_space = asoc->smallest_mtu - ovh;
8921 if (cnt_of_space < space_needed) {
8923 * ok we must trim down the chunk by lowering the
8924 * advance peer ack point.
8926 cnt_of_skipped = (cnt_of_space -
8927 ((sizeof(struct sctp_forward_tsn_chunk)) /
8928 sizeof(struct sctp_strseq)));
8930 * Go through and find the TSN that will be the one
8933 at = TAILQ_FIRST(&asoc->sent_queue);
8934 for (i = 0; i < cnt_of_skipped; i++) {
8935 tp1 = TAILQ_NEXT(at, sctp_next);
8940 * last now points to last one I can report, update
8943 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
8944 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
8946 chk->send_size = space_needed;
8947 /* Setup the chunk */
8948 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
8949 fwdtsn->ch.chunk_length = htons(chk->send_size);
8950 fwdtsn->ch.chunk_flags = 0;
8951 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
8952 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
8953 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
8954 (cnt_of_skipped * sizeof(struct sctp_strseq)));
8955 SCTP_BUF_LEN(chk->data) = chk->send_size;
8958 * Move pointer to after the fwdtsn and transfer to the
8961 strseq = (struct sctp_strseq *)fwdtsn;
8963 * Now populate the strseq list. This is done blindly
8964 * without pulling out duplicate stream info. This is
8965 * inefficent but won't harm the process since the peer will
8966 * look at these in sequence and will thus release anything.
8967 * It could mean we exceed the PMTU and chop off some that
8968 * we could have included.. but this is unlikely (aka 1432/4
8969 * would mean 300+ stream seq's would have to be reported in
8970 * one FWD-TSN. With a bit of work we can later FIX this to
8971 * optimize and pull out duplcates.. but it does add more
8972 * overhead. So for now... not!
8974 at = TAILQ_FIRST(&asoc->sent_queue);
8975 for (i = 0; i < cnt_of_skipped; i++) {
8976 tp1 = TAILQ_NEXT(at, sctp_next);
8977 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
8978 /* We don't report these */
8983 strseq->stream = ntohs(at->rec.data.stream_number);
8984 strseq->sequence = ntohs(at->rec.data.stream_seq);
8994 sctp_send_sack(struct sctp_tcb *stcb)
8997 * Queue up a SACK in the control queue. We must first check to see
8998 * if a SACK is somehow on the control queue. If so, we will take
8999 * and and remove the old one.
9001 struct sctp_association *asoc;
9002 struct sctp_tmit_chunk *chk, *a_chk;
9003 struct sctp_sack_chunk *sack;
9004 struct sctp_gap_ack_block *gap_descriptor;
9005 struct sack_track *selector;
9010 int limit_reached = 0;
9011 unsigned int i, jstart, siz, j;
9012 unsigned int num_gap_blocks = 0, space;
9019 SCTP_TCB_LOCK_ASSERT(stcb);
9020 if (asoc->last_data_chunk_from == NULL) {
9021 /* Hmm we never received anything */
9024 sctp_set_rwnd(stcb, asoc);
9025 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9026 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9027 /* Hmm, found a sack already on queue, remove it */
9028 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9029 asoc->ctrl_queue_cnt++;
9032 sctp_m_freem(a_chk->data);
9035 sctp_free_remote_addr(a_chk->whoTo);
9036 a_chk->whoTo = NULL;
9040 if (a_chk == NULL) {
9041 sctp_alloc_a_chunk(stcb, a_chk);
9042 if (a_chk == NULL) {
9043 /* No memory so we drop the idea, and set a timer */
9044 if (stcb->asoc.delayed_ack) {
9045 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9046 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9047 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9048 stcb->sctp_ep, stcb, NULL);
9050 stcb->asoc.send_sack = 1;
9054 a_chk->copy_by_ref = 0;
9055 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9056 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9057 a_chk->rec.chunk_id.can_take_data = 1;
9059 /* Clear our pkt counts */
9060 asoc->data_pkts_seen = 0;
9063 a_chk->snd_count = 0;
9064 a_chk->send_size = 0; /* fill in later */
9065 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9066 a_chk->whoTo = NULL;
9068 if ((asoc->numduptsns) ||
9069 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9072 * Ok, we have some duplicates or the destination for the
9073 * sack is unreachable, lets see if we can select an
9074 * alternate than asoc->last_data_chunk_from
9076 if ((!(asoc->last_data_chunk_from->dest_state &
9077 SCTP_ADDR_NOT_REACHABLE)) &&
9078 (asoc->used_alt_onsack > asoc->numnets)) {
9079 /* We used an alt last time, don't this time */
9080 a_chk->whoTo = NULL;
9082 asoc->used_alt_onsack++;
9083 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9085 if (a_chk->whoTo == NULL) {
9086 /* Nope, no alternate */
9087 a_chk->whoTo = asoc->last_data_chunk_from;
9088 asoc->used_alt_onsack = 0;
9092 * No duplicates so we use the last place we received data
9095 asoc->used_alt_onsack = 0;
9096 a_chk->whoTo = asoc->last_data_chunk_from;
9099 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9101 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9103 space_req = sizeof(struct sctp_sack_chunk);
9105 /* gaps get a cluster */
9106 space_req = MCLBYTES;
9108 /* Ok now lets formulate a MBUF with our sack */
9109 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9110 if ((a_chk->data == NULL) ||
9111 (a_chk->whoTo == NULL)) {
9112 /* rats, no mbuf memory */
9114 /* was a problem with the destination */
9115 sctp_m_freem(a_chk->data);
9118 sctp_free_a_chunk(stcb, a_chk);
9119 if (stcb->asoc.delayed_ack) {
9120 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9121 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9122 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9123 stcb->sctp_ep, stcb, NULL);
9125 stcb->asoc.send_sack = 1;
9129 /* ok, lets go through and fill it in */
9130 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9131 space = M_TRAILINGSPACE(a_chk->data);
9132 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9133 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9135 limit = mtod(a_chk->data, caddr_t);
9138 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9139 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9140 /* 0x01 is used by nonce for ecn */
9141 if ((sctp_ecn_enable) &&
9143 (asoc->peer_supports_ecn_nonce))
9144 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9146 sack->ch.chunk_flags = 0;
9148 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
9150 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9151 * received, then set high bit to 1, else 0. Reset
9154 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9155 asoc->cmt_dac_pkts_rcvd = 0;
9157 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9158 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9159 asoc->my_last_reported_rwnd = asoc->my_rwnd;
9161 /* reset the readers interpretation */
9162 stcb->freed_by_sorcv_sincelast = 0;
9164 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9166 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9167 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9170 * cum-ack behind the mapping array, so we start and use all
9175 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9177 * we skip the first one when the cum-ack is at or above the
9178 * mapping array base. Note this only works if
9182 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9183 /* we have a gap .. maybe */
9184 for (i = 0; i < siz; i++) {
9185 selector = &sack_array[asoc->mapping_array[i]];
9186 if (mergeable && selector->right_edge) {
9188 * Backup, left and right edges were ok to
9194 if (selector->num_entries == 0)
9197 for (j = jstart; j < selector->num_entries; j++) {
9198 if (mergeable && selector->right_edge) {
9200 * do a merge by NOT setting
9206 * no merge, set the left
9210 gap_descriptor->start = htons((selector->gaps[j].start + offset));
9212 gap_descriptor->end = htons((selector->gaps[j].end + offset));
9215 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
9221 if (selector->left_edge) {
9225 if (limit_reached) {
9226 /* Reached the limit stop */
9232 if (num_gap_blocks == 0) {
9233 /* reneged all chunks */
9234 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
9237 /* now we must add any dups we are going to report. */
9238 if ((limit_reached == 0) && (asoc->numduptsns)) {
9239 dup = (uint32_t *) gap_descriptor;
9240 for (i = 0; i < asoc->numduptsns; i++) {
9241 *dup = htonl(asoc->dup_tsns[i]);
9244 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
9249 asoc->numduptsns = 0;
9252 * now that the chunk is prepared queue it to the control chunk
9255 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
9256 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
9257 (num_dups * sizeof(int32_t)));
9258 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
9259 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
9260 sack->sack.num_dup_tsns = htons(num_dups);
9261 sack->ch.chunk_length = htons(a_chk->send_size);
9262 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
9263 asoc->ctrl_queue_cnt++;
9264 asoc->send_sack = 0;
9265 SCTP_STAT_INCR(sctps_sendsacks);
9271 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
9273 struct mbuf *m_abort;
9274 struct mbuf *m_out = NULL, *m_end = NULL;
9275 struct sctp_abort_chunk *abort = NULL;
9277 uint32_t auth_offset = 0;
9278 struct sctp_auth_chunk *auth = NULL;
9279 struct sctphdr *shdr;
9282 * Add an AUTH chunk, if chunk requires it and save the offset into
9283 * the chain for AUTH
9285 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
9286 stcb->asoc.peer_auth_chunks)) {
9287 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
9288 stcb, SCTP_ABORT_ASSOCIATION);
9290 SCTP_TCB_LOCK_ASSERT(stcb);
9291 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9292 if (m_abort == NULL) {
9295 sctp_m_freem(m_out);
9298 /* link in any error */
9299 SCTP_BUF_NEXT(m_abort) = operr;
9306 sz += SCTP_BUF_LEN(n);
9307 n = SCTP_BUF_NEXT(n);
9310 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
9311 if (m_out == NULL) {
9312 /* NO Auth chunk prepended, so reserve space in front */
9313 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
9316 /* Put AUTH chunk at the front of the chain */
9317 SCTP_BUF_NEXT(m_end) = m_abort;
9320 /* fill in the ABORT chunk */
9321 abort = mtod(m_abort, struct sctp_abort_chunk *);
9322 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9323 abort->ch.chunk_flags = 0;
9324 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
9326 /* prepend and fill in the SCTP header */
9327 SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
9328 if (m_out == NULL) {
9329 /* TSNH: no memory */
9332 shdr = mtod(m_out, struct sctphdr *);
9333 shdr->src_port = stcb->sctp_ep->sctp_lport;
9334 shdr->dest_port = stcb->rport;
9335 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9337 auth_offset += sizeof(struct sctphdr);
9339 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
9340 stcb->asoc.primary_destination,
9341 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
9342 m_out, auth_offset, auth, 1, 0, NULL, 0);
9343 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9347 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
9348 struct sctp_nets *net)
9350 /* formulate and SEND a SHUTDOWN-COMPLETE */
9351 struct mbuf *m_shutdown_comp;
9352 struct sctp_shutdown_complete_msg *comp_cp;
9354 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
9355 if (m_shutdown_comp == NULL) {
9359 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
9360 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9361 comp_cp->shut_cmp.ch.chunk_flags = 0;
9362 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9363 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
9364 comp_cp->sh.dest_port = stcb->rport;
9365 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
9366 comp_cp->sh.checksum = 0;
9368 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
9369 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
9370 (struct sockaddr *)&net->ro._l_addr,
9371 m_shutdown_comp, 0, NULL, 1, 0, NULL, 0);
9372 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9377 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
9380 /* formulate and SEND a SHUTDOWN-COMPLETE */
9383 struct ip *iph, *iph_out;
9384 struct ip6_hdr *ip6, *ip6_out;
9385 int offset_out, len, mlen;
9386 struct sctp_shutdown_complete_msg *comp_cp;
9388 /* Get room for the largest message */
9389 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
9390 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
9394 SCTP_BUF_LEN(mout) = len;
9395 iph = mtod(m, struct ip *);
9399 if (iph->ip_v == IPVERSION) {
9400 SCTP_BUF_LEN(mout) = sizeof(struct ip) +
9401 sizeof(struct sctp_shutdown_complete_msg);
9402 SCTP_BUF_NEXT(mout) = NULL;
9403 iph_out = mtod(mout, struct ip *);
9405 /* Fill in the IP header for the ABORT */
9406 iph_out->ip_v = IPVERSION;
9407 iph_out->ip_hl = (sizeof(struct ip) / 4);
9408 iph_out->ip_tos = (u_char)0;
9410 iph_out->ip_off = 0;
9411 iph_out->ip_ttl = MAXTTL;
9412 iph_out->ip_p = IPPROTO_SCTP;
9413 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9414 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9416 /* let IP layer calculate this */
9417 iph_out->ip_sum = 0;
9418 offset_out += sizeof(*iph_out);
9419 comp_cp = (struct sctp_shutdown_complete_msg *)(
9420 (caddr_t)iph_out + offset_out);
9421 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9422 ip6 = (struct ip6_hdr *)iph;
9423 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr) +
9424 sizeof(struct sctp_shutdown_complete_msg);
9425 SCTP_BUF_NEXT(mout) = NULL;
9426 ip6_out = mtod(mout, struct ip6_hdr *);
9428 /* Fill in the IPv6 header for the ABORT */
9429 ip6_out->ip6_flow = ip6->ip6_flow;
9430 ip6_out->ip6_hlim = ip6_defhlim;
9431 ip6_out->ip6_nxt = IPPROTO_SCTP;
9432 ip6_out->ip6_src = ip6->ip6_dst;
9433 ip6_out->ip6_dst = ip6->ip6_src;
9435 * ?? The old code had both the iph len + payload, I think
9436 * this is wrong and would never have worked
9438 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
9439 offset_out += sizeof(*ip6_out);
9440 comp_cp = (struct sctp_shutdown_complete_msg *)(
9441 (caddr_t)ip6_out + offset_out);
9443 /* Currently not supported. */
9446 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
9451 /* Now copy in and fill in the ABORT tags etc. */
9452 comp_cp->sh.src_port = sh->dest_port;
9453 comp_cp->sh.dest_port = sh->src_port;
9454 comp_cp->sh.checksum = 0;
9455 comp_cp->sh.v_tag = sh->v_tag;
9456 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
9457 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
9458 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
9461 if ((sctp_no_csum_on_loopback) && SCTP_IS_IT_LOOPBACK(mout)) {
9462 comp_cp->sh.checksum = 0;
9464 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
9466 if (iph_out != NULL) {
9469 struct sctp_tcb *stcb = NULL;
9471 mlen = SCTP_BUF_LEN(mout);
9472 bzero(&ro, sizeof ro);
9473 /* set IPv4 length */
9474 iph_out->ip_len = mlen;
9475 #ifdef SCTP_PACKET_LOGGING
9476 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9477 sctp_packet_log(mout, mlen);
9479 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9482 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
9484 /* Free the route if we got one back */
9487 } else if (ip6_out != NULL) {
9488 struct route_in6 ro;
9490 struct sctp_tcb *stcb = NULL;
9491 struct ifnet *ifp = NULL;
9493 bzero(&ro, sizeof(ro));
9494 mlen = SCTP_BUF_LEN(mout);
9495 #ifdef SCTP_PACKET_LOGGING
9496 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
9497 sctp_packet_log(mout, mlen);
9499 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
9500 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
9502 /* Free the route if we got one back */
9506 SCTP_STAT_INCR(sctps_sendpackets);
9507 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9508 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9513 static struct sctp_nets *
9514 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
9516 struct sctp_nets *net, *hnet;
9517 int ms_goneby, highest_ms, state_overide = 0;
9519 (void)SCTP_GETTIME_TIMEVAL(now);
9522 SCTP_TCB_LOCK_ASSERT(stcb);
9523 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9525 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
9526 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
9529 * Skip this guy from consideration if HB is off AND
9534 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
9535 /* skip this dest net from consideration */
9538 if (net->last_sent_time.tv_sec) {
9539 /* Sent to so we subtract */
9540 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
9542 /* Never been sent to */
9543 ms_goneby = 0x7fffffff;
9545 * When the address state is unconfirmed but still
9546 * considered reachable, we HB at a higher rate. Once it
9547 * goes confirmed OR reaches the "unreachable" state, thenw
9548 * we cut it back to HB at a more normal pace.
9550 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
9556 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
9557 (ms_goneby > highest_ms)) {
9558 highest_ms = ms_goneby;
9563 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
9569 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
9571 * Found the one with longest delay bounds OR it is
9572 * unconfirmed and still not marked unreachable.
9574 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
9577 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
9578 (struct sockaddr *)&hnet->ro._l_addr);
9580 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
9583 /* update the timer now */
9584 hnet->last_sent_time = *now;
9592 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
9594 struct sctp_tmit_chunk *chk;
9595 struct sctp_nets *net;
9596 struct sctp_heartbeat_chunk *hb;
9598 struct sockaddr_in *sin;
9599 struct sockaddr_in6 *sin6;
9601 SCTP_TCB_LOCK_ASSERT(stcb);
9602 if (user_req == 0) {
9603 net = sctp_select_hb_destination(stcb, &now);
9606 * All our busy none to send to, just start the
9609 if (stcb->asoc.state == 0) {
9612 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
9623 (void)SCTP_GETTIME_TIMEVAL(&now);
9625 sin = (struct sockaddr_in *)&net->ro._l_addr;
9626 if (sin->sin_family != AF_INET) {
9627 if (sin->sin_family != AF_INET6) {
9632 sctp_alloc_a_chunk(stcb, chk);
9634 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
9637 chk->copy_by_ref = 0;
9638 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
9639 chk->rec.chunk_id.can_take_data = 1;
9640 chk->asoc = &stcb->asoc;
9641 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
9643 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
9644 if (chk->data == NULL) {
9645 sctp_free_a_chunk(stcb, chk);
9648 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9649 SCTP_BUF_LEN(chk->data) = chk->send_size;
9650 chk->sent = SCTP_DATAGRAM_UNSENT;
9653 atomic_add_int(&chk->whoTo->ref_count, 1);
9654 /* Now we have a mbuf that we can fill in with the details */
9655 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
9656 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
9657 /* fill out chunk header */
9658 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
9659 hb->ch.chunk_flags = 0;
9660 hb->ch.chunk_length = htons(chk->send_size);
9661 /* Fill out hb parameter */
9662 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
9663 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
9664 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
9665 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
9666 /* Did our user request this one, put it in */
9667 hb->heartbeat.hb_info.user_req = user_req;
9668 hb->heartbeat.hb_info.addr_family = sin->sin_family;
9669 hb->heartbeat.hb_info.addr_len = sin->sin_len;
9670 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9672 * we only take from the entropy pool if the address is not
9675 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
9676 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
9678 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
9679 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
9681 if (sin->sin_family == AF_INET) {
9682 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
9683 } else if (sin->sin_family == AF_INET6) {
9684 /* We leave the scope the way it is in our lookup table. */
9685 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
9686 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
9688 /* huh compiler bug */
9693 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
9694 * PF-heartbeats. Because of this, threshold management is done by
9695 * the t3 timer handler, and does not need to be done upon the send
9696 * of a PF-heartbeat. If CMT PF is on and the destination to which a
9697 * heartbeat is being sent is in PF state, do NOT do threshold
9700 if ((sctp_cmt_pf == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
9701 /* ok we have a destination that needs a beat */
9702 /* lets do the theshold management Qiaobing style */
9703 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
9704 stcb->asoc.max_send_times)) {
9706 * we have lost the association, in a way this is
9707 * quite bad since we really are one less time since
9708 * we really did not send yet. This is the down side
9709 * to the Q's style as defined in the RFC and not my
9710 * alternate style defined in the RFC.
9712 if (chk->data != NULL) {
9713 sctp_m_freem(chk->data);
9717 * Here we do NOT use the macro since the
9718 * association is now gone.
9721 sctp_free_remote_addr(chk->whoTo);
9724 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9725 SCTP_DECR_CHK_COUNT();
9729 net->hb_responded = 0;
9730 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9731 stcb->asoc.ctrl_queue_cnt++;
9732 SCTP_STAT_INCR(sctps_sendheartbeat);
9734 * Call directly med level routine to put out the chunk. It will
9735 * always tumble out control chunks aka HB but it may even tumble
9742 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
9745 struct sctp_association *asoc;
9746 struct sctp_ecne_chunk *ecne;
9747 struct sctp_tmit_chunk *chk;
9750 SCTP_TCB_LOCK_ASSERT(stcb);
9751 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9752 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9753 /* found a previous ECN_ECHO update it if needed */
9754 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
9755 ecne->tsn = htonl(high_tsn);
9759 /* nope could not find one to update so we must build one */
9760 sctp_alloc_a_chunk(stcb, chk);
9764 chk->copy_by_ref = 0;
9765 SCTP_STAT_INCR(sctps_sendecne);
9766 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
9767 chk->rec.chunk_id.can_take_data = 0;
9768 chk->asoc = &stcb->asoc;
9769 chk->send_size = sizeof(struct sctp_ecne_chunk);
9770 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
9771 if (chk->data == NULL) {
9772 sctp_free_a_chunk(stcb, chk);
9775 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9776 SCTP_BUF_LEN(chk->data) = chk->send_size;
9777 chk->sent = SCTP_DATAGRAM_UNSENT;
9780 atomic_add_int(&chk->whoTo->ref_count, 1);
9781 stcb->asoc.ecn_echo_cnt_onq++;
9782 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
9783 ecne->ch.chunk_type = SCTP_ECN_ECHO;
9784 ecne->ch.chunk_flags = 0;
9785 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
9786 ecne->tsn = htonl(high_tsn);
9787 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9788 asoc->ctrl_queue_cnt++;
9792 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
9793 struct mbuf *m, int iphlen, int bad_crc)
9795 struct sctp_association *asoc;
9796 struct sctp_pktdrop_chunk *drp;
9797 struct sctp_tmit_chunk *chk;
9802 int fullsz = 0, extra = 0;
9806 SCTP_TCB_LOCK_ASSERT(stcb);
9807 if (asoc->peer_supports_pktdrop == 0) {
9809 * peer must declare support before I send one.
9813 if (stcb->sctp_socket == NULL) {
9816 sctp_alloc_a_chunk(stcb, chk);
9820 chk->copy_by_ref = 0;
9821 iph = mtod(m, struct ip *);
9823 sctp_free_a_chunk(stcb, chk);
9826 if (iph->ip_v == IPVERSION) {
9828 len = chk->send_size = iph->ip_len;
9830 struct ip6_hdr *ip6h;
9833 ip6h = mtod(m, struct ip6_hdr *);
9834 len = chk->send_size = htons(ip6h->ip6_plen);
9836 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
9837 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
9839 * only send 1 mtu worth, trim off the excess on the end.
9841 fullsz = len - extra;
9842 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
9845 chk->asoc = &stcb->asoc;
9846 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9847 if (chk->data == NULL) {
9849 sctp_free_a_chunk(stcb, chk);
9852 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9853 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
9855 sctp_m_freem(chk->data);
9859 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
9860 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
9861 chk->book_size_scale = 0;
9863 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
9864 drp->trunc_len = htons(fullsz);
9866 * Len is already adjusted to size minus overhead above take
9867 * out the pkt_drop chunk itself from it.
9869 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
9870 len = chk->send_size;
9872 /* no truncation needed */
9873 drp->ch.chunk_flags = 0;
9874 drp->trunc_len = htons(0);
9877 drp->ch.chunk_flags |= SCTP_BADCRC;
9879 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
9880 SCTP_BUF_LEN(chk->data) = chk->send_size;
9881 chk->sent = SCTP_DATAGRAM_UNSENT;
9884 /* we should hit here */
9887 chk->whoTo = asoc->primary_destination;
9889 atomic_add_int(&chk->whoTo->ref_count, 1);
9890 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
9891 chk->rec.chunk_id.can_take_data = 1;
9892 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
9893 drp->ch.chunk_length = htons(chk->send_size);
9894 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
9898 drp->bottle_bw = htonl(spc);
9899 if (asoc->my_rwnd) {
9900 drp->current_onq = htonl(asoc->size_on_reasm_queue +
9901 asoc->size_on_all_streams +
9902 asoc->my_rwnd_control_len +
9903 stcb->sctp_socket->so_rcv.sb_cc);
9906 * If my rwnd is 0, possibly from mbuf depletion as well as
9907 * space used, tell the peer there is NO space aka onq == bw
9909 drp->current_onq = htonl(spc);
9913 m_copydata(m, iphlen, len, (caddr_t)datap);
9914 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9915 asoc->ctrl_queue_cnt++;
9919 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
9921 struct sctp_association *asoc;
9922 struct sctp_cwr_chunk *cwr;
9923 struct sctp_tmit_chunk *chk;
9926 SCTP_TCB_LOCK_ASSERT(stcb);
9927 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9928 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
9929 /* found a previous ECN_CWR update it if needed */
9930 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
9931 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
9933 cwr->tsn = htonl(high_tsn);
9938 /* nope could not find one to update so we must build one */
9939 sctp_alloc_a_chunk(stcb, chk);
9943 chk->copy_by_ref = 0;
9944 chk->rec.chunk_id.id = SCTP_ECN_CWR;
9945 chk->rec.chunk_id.can_take_data = 1;
9946 chk->asoc = &stcb->asoc;
9947 chk->send_size = sizeof(struct sctp_cwr_chunk);
9948 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
9949 if (chk->data == NULL) {
9950 sctp_free_a_chunk(stcb, chk);
9953 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9954 SCTP_BUF_LEN(chk->data) = chk->send_size;
9955 chk->sent = SCTP_DATAGRAM_UNSENT;
9958 atomic_add_int(&chk->whoTo->ref_count, 1);
9959 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
9960 cwr->ch.chunk_type = SCTP_ECN_CWR;
9961 cwr->ch.chunk_flags = 0;
9962 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
9963 cwr->tsn = htonl(high_tsn);
9964 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9965 asoc->ctrl_queue_cnt++;
9969 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
9970 int number_entries, uint16_t * list,
9971 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
9973 int len, old_len, i;
9974 struct sctp_stream_reset_out_request *req_out;
9975 struct sctp_chunkhdr *ch;
9977 ch = mtod(chk->data, struct sctp_chunkhdr *);
9980 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
9982 /* get to new offset for the param. */
9983 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
9984 /* now how long will this param be? */
9985 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
9986 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
9987 req_out->ph.param_length = htons(len);
9988 req_out->request_seq = htonl(seq);
9989 req_out->response_seq = htonl(resp_seq);
9990 req_out->send_reset_at_tsn = htonl(last_sent);
9991 if (number_entries) {
9992 for (i = 0; i < number_entries; i++) {
9993 req_out->list_of_streams[i] = htons(list[i]);
9996 if (SCTP_SIZE32(len) > len) {
9998 * Need to worry about the pad we may end up adding to the
9999 * end. This is easy since the struct is either aligned to 4
10000 * bytes or 2 bytes off.
10002 req_out->list_of_streams[number_entries] = 0;
10004 /* now fix the chunk length */
10005 ch->chunk_length = htons(len + old_len);
10006 chk->book_size = len + old_len;
10007 chk->book_size_scale = 0;
10008 chk->send_size = SCTP_SIZE32(chk->book_size);
10009 SCTP_BUF_LEN(chk->data) = chk->send_size;
10015 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10016 int number_entries, uint16_t * list,
10019 int len, old_len, i;
10020 struct sctp_stream_reset_in_request *req_in;
10021 struct sctp_chunkhdr *ch;
10023 ch = mtod(chk->data, struct sctp_chunkhdr *);
10026 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10028 /* get to new offset for the param. */
10029 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10030 /* now how long will this param be? */
10031 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10032 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10033 req_in->ph.param_length = htons(len);
10034 req_in->request_seq = htonl(seq);
10035 if (number_entries) {
10036 for (i = 0; i < number_entries; i++) {
10037 req_in->list_of_streams[i] = htons(list[i]);
10040 if (SCTP_SIZE32(len) > len) {
10042 * Need to worry about the pad we may end up adding to the
10043 * end. This is easy since the struct is either aligned to 4
10044 * bytes or 2 bytes off.
10046 req_in->list_of_streams[number_entries] = 0;
10048 /* now fix the chunk length */
10049 ch->chunk_length = htons(len + old_len);
10050 chk->book_size = len + old_len;
10051 chk->book_size_scale = 0;
10052 chk->send_size = SCTP_SIZE32(chk->book_size);
10053 SCTP_BUF_LEN(chk->data) = chk->send_size;
10059 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10063 struct sctp_stream_reset_tsn_request *req_tsn;
10064 struct sctp_chunkhdr *ch;
10066 ch = mtod(chk->data, struct sctp_chunkhdr *);
10069 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10071 /* get to new offset for the param. */
10072 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10073 /* now how long will this param be? */
10074 len = sizeof(struct sctp_stream_reset_tsn_request);
10075 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10076 req_tsn->ph.param_length = htons(len);
10077 req_tsn->request_seq = htonl(seq);
10079 /* now fix the chunk length */
10080 ch->chunk_length = htons(len + old_len);
10081 chk->send_size = len + old_len;
10082 chk->book_size = SCTP_SIZE32(chk->send_size);
10083 chk->book_size_scale = 0;
10084 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10089 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10090 uint32_t resp_seq, uint32_t result)
10093 struct sctp_stream_reset_response *resp;
10094 struct sctp_chunkhdr *ch;
10096 ch = mtod(chk->data, struct sctp_chunkhdr *);
10099 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10101 /* get to new offset for the param. */
10102 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
10103 /* now how long will this param be? */
10104 len = sizeof(struct sctp_stream_reset_response);
10105 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10106 resp->ph.param_length = htons(len);
10107 resp->response_seq = htonl(resp_seq);
10108 resp->result = ntohl(result);
10110 /* now fix the chunk length */
10111 ch->chunk_length = htons(len + old_len);
10112 chk->book_size = len + old_len;
10113 chk->book_size_scale = 0;
10114 chk->send_size = SCTP_SIZE32(chk->book_size);
10115 SCTP_BUF_LEN(chk->data) = chk->send_size;
10122 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
10123 uint32_t resp_seq, uint32_t result,
10124 uint32_t send_una, uint32_t recv_next)
10127 struct sctp_stream_reset_response_tsn *resp;
10128 struct sctp_chunkhdr *ch;
10130 ch = mtod(chk->data, struct sctp_chunkhdr *);
10133 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10135 /* get to new offset for the param. */
10136 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
10137 /* now how long will this param be? */
10138 len = sizeof(struct sctp_stream_reset_response_tsn);
10139 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
10140 resp->ph.param_length = htons(len);
10141 resp->response_seq = htonl(resp_seq);
10142 resp->result = htonl(result);
10143 resp->senders_next_tsn = htonl(send_una);
10144 resp->receivers_next_tsn = htonl(recv_next);
10146 /* now fix the chunk length */
10147 ch->chunk_length = htons(len + old_len);
10148 chk->book_size = len + old_len;
10149 chk->send_size = SCTP_SIZE32(chk->book_size);
10150 chk->book_size_scale = 0;
10151 SCTP_BUF_LEN(chk->data) = chk->send_size;
10157 sctp_send_str_reset_req(struct sctp_tcb *stcb,
10158 int number_entries, uint16_t * list,
10159 uint8_t send_out_req, uint32_t resp_seq,
10160 uint8_t send_in_req,
10161 uint8_t send_tsn_req)
10164 struct sctp_association *asoc;
10165 struct sctp_tmit_chunk *chk;
10166 struct sctp_chunkhdr *ch;
10169 asoc = &stcb->asoc;
10170 if (asoc->stream_reset_outstanding) {
10172 * Already one pending, must get ACK back to clear the flag.
10176 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
10177 /* nothing to do */
10180 if (send_tsn_req && (send_out_req || send_in_req)) {
10181 /* error, can't do that */
10184 sctp_alloc_a_chunk(stcb, chk);
10186 SCTP_LTRACE_ERR(stcb->sctp_ep, stcb, ENOMEM, 0);
10189 chk->copy_by_ref = 0;
10190 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
10191 chk->rec.chunk_id.can_take_data = 0;
10192 chk->asoc = &stcb->asoc;
10193 chk->book_size = sizeof(struct sctp_chunkhdr);
10194 chk->send_size = SCTP_SIZE32(chk->book_size);
10195 chk->book_size_scale = 0;
10197 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10198 if (chk->data == NULL) {
10199 sctp_free_a_chunk(stcb, chk);
10200 SCTP_LTRACE_ERR(stcb->sctp_ep, stcb, ENOMEM, 0);
10203 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10205 /* setup chunk parameters */
10206 chk->sent = SCTP_DATAGRAM_UNSENT;
10207 chk->snd_count = 0;
10208 chk->whoTo = asoc->primary_destination;
10209 atomic_add_int(&chk->whoTo->ref_count, 1);
10211 ch = mtod(chk->data, struct sctp_chunkhdr *);
10212 ch->chunk_type = SCTP_STREAM_RESET;
10213 ch->chunk_flags = 0;
10214 ch->chunk_length = htons(chk->book_size);
10215 SCTP_BUF_LEN(chk->data) = chk->send_size;
10217 seq = stcb->asoc.str_reset_seq_out;
10218 if (send_out_req) {
10219 sctp_add_stream_reset_out(chk, number_entries, list,
10220 seq, resp_seq, (stcb->asoc.sending_seq - 1));
10221 asoc->stream_reset_out_is_outstanding = 1;
10223 asoc->stream_reset_outstanding++;
10226 sctp_add_stream_reset_in(chk, number_entries, list, seq);
10227 asoc->stream_reset_outstanding++;
10229 if (send_tsn_req) {
10230 sctp_add_stream_reset_tsn(chk, seq);
10231 asoc->stream_reset_outstanding++;
10233 asoc->str_reset = chk;
10235 /* insert the chunk for sending */
10236 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
10239 asoc->ctrl_queue_cnt++;
10240 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
10245 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
10246 struct mbuf *err_cause, uint32_t vrf_id)
10249 * Formulate the abort message, and send it back down.
10251 struct mbuf *o_pak;
10253 struct sctp_abort_msg *abm;
10254 struct ip *iph, *iph_out;
10255 struct ip6_hdr *ip6, *ip6_out;
10256 int iphlen_out, len;
10258 /* don't respond to ABORT with ABORT */
10259 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
10261 sctp_m_freem(err_cause);
10264 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
10266 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10267 if (mout == NULL) {
10269 sctp_m_freem(err_cause);
10272 iph = mtod(m, struct ip *);
10275 if (iph->ip_v == IPVERSION) {
10276 iph_out = mtod(mout, struct ip *);
10277 SCTP_BUF_LEN(mout) = sizeof(*iph_out) + sizeof(*abm);
10278 SCTP_BUF_NEXT(mout) = err_cause;
10280 /* Fill in the IP header for the ABORT */
10281 iph_out->ip_v = IPVERSION;
10282 iph_out->ip_hl = (sizeof(struct ip) / 4);
10283 iph_out->ip_tos = (u_char)0;
10284 iph_out->ip_id = 0;
10285 iph_out->ip_off = 0;
10286 iph_out->ip_ttl = MAXTTL;
10287 iph_out->ip_p = IPPROTO_SCTP;
10288 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10289 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10290 /* let IP layer calculate this */
10291 iph_out->ip_sum = 0;
10293 iphlen_out = sizeof(*iph_out);
10294 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
10295 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
10296 ip6 = (struct ip6_hdr *)iph;
10297 ip6_out = mtod(mout, struct ip6_hdr *);
10298 SCTP_BUF_LEN(mout) = sizeof(*ip6_out) + sizeof(*abm);
10299 SCTP_BUF_NEXT(mout) = err_cause;
10301 /* Fill in the IP6 header for the ABORT */
10302 ip6_out->ip6_flow = ip6->ip6_flow;
10303 ip6_out->ip6_hlim = ip6_defhlim;
10304 ip6_out->ip6_nxt = IPPROTO_SCTP;
10305 ip6_out->ip6_src = ip6->ip6_dst;
10306 ip6_out->ip6_dst = ip6->ip6_src;
10308 iphlen_out = sizeof(*ip6_out);
10309 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
10311 /* Currently not supported */
10315 abm->sh.src_port = sh->dest_port;
10316 abm->sh.dest_port = sh->src_port;
10317 abm->sh.checksum = 0;
10319 abm->sh.v_tag = sh->v_tag;
10320 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
10322 abm->sh.v_tag = htonl(vtag);
10323 abm->msg.ch.chunk_flags = 0;
10325 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10328 struct mbuf *m_tmp = err_cause;
10331 /* get length of the err_cause chain */
10332 while (m_tmp != NULL) {
10333 err_len += SCTP_BUF_LEN(m_tmp);
10334 m_tmp = SCTP_BUF_NEXT(m_tmp);
10336 len = SCTP_BUF_LEN(mout) + err_len;
10338 /* need pad at end of chunk */
10339 uint32_t cpthis = 0;
10342 padlen = 4 - (len % 4);
10343 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
10346 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
10348 len = SCTP_BUF_LEN(mout);
10349 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
10353 if ((sctp_no_csum_on_loopback) && SCTP_IS_IT_LOOPBACK(m)) {
10354 abm->sh.checksum = 0;
10356 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
10358 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10360 sctp_m_freem(mout);
10363 if (iph_out != NULL) {
10365 struct sctp_tcb *stcb = NULL;
10368 /* zap the stack pointer to the route */
10369 bzero(&ro, sizeof ro);
10370 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
10371 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
10372 /* set IPv4 length */
10373 iph_out->ip_len = len;
10375 #ifdef SCTP_PACKET_LOGGING
10376 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10377 sctp_packet_log(mout, len);
10379 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10380 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10382 /* Free the route if we got one back */
10385 } else if (ip6_out != NULL) {
10386 struct route_in6 ro;
10388 struct sctp_tcb *stcb = NULL;
10389 struct ifnet *ifp = NULL;
10391 /* zap the stack pointer to the route */
10392 bzero(&ro, sizeof(ro));
10393 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
10394 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
10395 ip6_out->ip6_plen = len - sizeof(*ip6_out);
10396 #ifdef SCTP_PACKET_LOGGING
10397 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10398 sctp_packet_log(mout, len);
10400 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10401 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10403 /* Free the route if we got one back */
10407 SCTP_STAT_INCR(sctps_sendpackets);
10408 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10412 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
10415 struct mbuf *o_pak;
10416 struct sctphdr *ihdr;
10418 struct sctphdr *ohdr;
10419 struct sctp_chunkhdr *ophdr;
10424 struct sockaddr_in6 lsa6, fsa6;
10431 iph = mtod(m, struct ip *);
10432 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
10434 SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
10436 /* can't send because we can't add a mbuf */
10439 ohdr = mtod(scm, struct sctphdr *);
10440 ohdr->src_port = ihdr->dest_port;
10441 ohdr->dest_port = ihdr->src_port;
10442 ohdr->v_tag = vtag;
10443 ohdr->checksum = 0;
10444 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
10445 ophdr->chunk_type = SCTP_OPERATION_ERROR;
10446 ophdr->chunk_flags = 0;
10450 len += SCTP_BUF_LEN(at);
10451 at = SCTP_BUF_NEXT(at);
10453 ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
10456 uint32_t cpthis = 0;
10459 padlen = 4 - (len % 4);
10460 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
10463 if ((sctp_no_csum_on_loopback) && SCTP_IS_IT_LOOPBACK(m)) {
10466 val = sctp_calculate_sum(scm, NULL, 0);
10468 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
10469 if (mout == NULL) {
10473 SCTP_BUF_NEXT(mout) = scm;
10474 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10475 sctp_m_freem(mout);
10478 ohdr->checksum = val;
10479 if (iph->ip_v == IPVERSION) {
10483 struct sctp_tcb *stcb = NULL;
10485 SCTP_BUF_LEN(mout) = sizeof(struct ip);
10486 len += sizeof(struct ip);
10488 bzero(&ro, sizeof ro);
10489 out = mtod(mout, struct ip *);
10490 out->ip_v = iph->ip_v;
10491 out->ip_hl = (sizeof(struct ip) / 4);
10492 out->ip_tos = iph->ip_tos;
10493 out->ip_id = iph->ip_id;
10495 out->ip_ttl = MAXTTL;
10496 out->ip_p = IPPROTO_SCTP;
10498 out->ip_src = iph->ip_dst;
10499 out->ip_dst = iph->ip_src;
10501 #ifdef SCTP_PACKET_LOGGING
10502 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10503 sctp_packet_log(mout, len);
10505 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10507 SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
10509 SCTP_STAT_INCR(sctps_sendpackets);
10510 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10511 /* Free the route if we got one back */
10516 struct route_in6 ro;
10518 struct sctp_tcb *stcb = NULL;
10519 struct ifnet *ifp = NULL;
10520 struct ip6_hdr *out6, *in6;
10522 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
10523 len += sizeof(struct ip6_hdr);
10524 bzero(&ro, sizeof ro);
10525 in6 = mtod(m, struct ip6_hdr *);
10526 out6 = mtod(mout, struct ip6_hdr *);
10527 out6->ip6_flow = in6->ip6_flow;
10528 out6->ip6_hlim = ip6_defhlim;
10529 out6->ip6_nxt = IPPROTO_SCTP;
10530 out6->ip6_src = in6->ip6_dst;
10531 out6->ip6_dst = in6->ip6_src;
10532 out6->ip6_plen = len - sizeof(struct ip6_hdr);
10535 bzero(&lsa6, sizeof(lsa6));
10536 lsa6.sin6_len = sizeof(lsa6);
10537 lsa6.sin6_family = AF_INET6;
10538 lsa6.sin6_addr = out6->ip6_src;
10539 bzero(&fsa6, sizeof(fsa6));
10540 fsa6.sin6_len = sizeof(fsa6);
10541 fsa6.sin6_family = AF_INET6;
10542 fsa6.sin6_addr = out6->ip6_dst;
10544 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
10545 SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
10546 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
10547 SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
10548 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
10550 #ifdef SCTP_PACKET_LOGGING
10551 if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
10552 sctp_packet_log(mout, len);
10554 SCTP_ATTACH_CHAIN(o_pak, mout, len);
10555 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10557 SCTP_STAT_INCR(sctps_sendpackets);
10558 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10559 /* Free the route if we got one back */
10565 static struct mbuf *
10566 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
10568 struct sctp_sndrcvinfo *srcv,
10570 int user_marks_eor,
10573 struct mbuf **new_tail)
10577 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
10578 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
10582 *sndout = m_length(m, NULL);
10583 *new_tail = m_last(m);
10589 sctp_copy_one(struct sctp_stream_queue_pending *sp,
10596 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
10598 if (sp->data == NULL)
10601 sp->tail_mbuf = m_last(sp->data);
10607 static struct sctp_stream_queue_pending *
10608 sctp_copy_it_in(struct sctp_tcb *stcb,
10609 struct sctp_association *asoc,
10610 struct sctp_sndrcvinfo *srcv,
10612 struct sctp_nets *net,
10614 int user_marks_eor,
10619 * This routine must be very careful in its work. Protocol
10620 * processing is up and running so care must be taken to spl...()
10621 * when you need to do something that may effect the stcb/asoc. The
10622 * sb is locked however. When data is copied the protocol processing
10623 * should be enabled since this is a slower operation...
10625 struct sctp_stream_queue_pending *sp = NULL;
10629 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
10630 if (((user_marks_eor == 0) && non_blocking) &&
10631 (uio->uio_resid > (int)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
10632 /* It will NEVER fit */
10636 /* Now can we send this? */
10637 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10638 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10639 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10640 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10641 /* got data while shutting down */
10642 *error = ECONNRESET;
10645 sctp_alloc_a_strmoq(stcb, sp);
10651 sp->sender_all_done = 0;
10652 sp->sinfo_flags = srcv->sinfo_flags;
10653 sp->timetolive = srcv->sinfo_timetolive;
10654 sp->ppid = srcv->sinfo_ppid;
10655 sp->context = srcv->sinfo_context;
10657 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
10659 sp->stream = srcv->sinfo_stream;
10660 sp->length = min(uio->uio_resid, max_send_len);
10661 if ((sp->length == (uint32_t) uio->uio_resid) &&
10662 ((user_marks_eor == 0) ||
10663 (srcv->sinfo_flags & SCTP_EOF) ||
10664 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
10665 sp->msg_is_complete = 1;
10667 sp->msg_is_complete = 0;
10669 sp->sender_all_done = 0;
10670 sp->some_taken = 0;
10671 sp->put_last_out = 0;
10672 resv_in_first = sizeof(struct sctp_data_chunk);
10673 sp->data = sp->tail_mbuf = NULL;
10674 *error = sctp_copy_one(sp, uio, resv_in_first);
10676 sctp_free_a_strmoq(stcb, sp);
10679 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
10683 sp->net = asoc->primary_destination;
10686 atomic_add_int(&sp->net->ref_count, 1);
10687 sctp_set_prsctp_policy(stcb, sp);
10695 sctp_sosend(struct socket *so,
10696 struct sockaddr *addr,
10699 struct mbuf *control,
10705 struct sctp_inpcb *inp;
10706 int error, use_rcvinfo = 0;
10707 struct sctp_sndrcvinfo srcv;
10709 inp = (struct sctp_inpcb *)so->so_pcb;
10711 /* process cmsg snd/rcv info (maybe a assoc-id) */
10712 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
10718 error = sctp_lower_sosend(so, addr, uio, top,
10729 sctp_lower_sosend(struct socket *so,
10730 struct sockaddr *addr,
10732 struct mbuf *i_pak,
10733 struct mbuf *control,
10736 struct sctp_sndrcvinfo *srcv
10741 unsigned int sndlen = 0, max_len;
10743 struct mbuf *top = NULL;
10745 #if defined(__NetBSD__) || defined(__OpenBSD_)
10749 int queue_only = 0, queue_only_for_init = 0;
10750 int free_cnt_applied = 0;
10752 int now_filled = 0;
10753 struct sctp_block_entry be;
10754 struct sctp_inpcb *inp;
10755 struct sctp_tcb *stcb = NULL;
10756 struct timeval now;
10757 struct sctp_nets *net;
10758 struct sctp_association *asoc;
10759 struct sctp_inpcb *t_inp;
10760 int create_lock_applied = 0;
10761 int nagle_applies = 0;
10762 int some_on_control = 0;
10763 int got_all_of_the_send = 0;
10764 int hold_tcblock = 0;
10765 int non_blocking = 0;
10766 int temp_flags = 0;
10772 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
10777 if ((uio == NULL) && (i_pak == NULL)) {
10780 atomic_add_int(&inp->total_sends, 1);
10782 sndlen = uio->uio_resid;
10784 sndlen = SCTP_HEADER_LEN(i_pak);
10785 top = SCTP_HEADER_TO_CHAIN(i_pak);
10788 * Pre-screen address, if one is given the sin-len must be set
10792 if ((addr->sa_family == AF_INET) &&
10793 (addr->sa_len != sizeof(struct sockaddr_in))) {
10796 } else if ((addr->sa_family == AF_INET6) &&
10797 (addr->sa_len != sizeof(struct sockaddr_in6))) {
10804 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
10805 (inp->sctp_socket->so_qlimit)) {
10806 /* The listener can NOT send */
10810 if ((use_rcvinfo) && srcv) {
10811 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
10812 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
10816 if (srcv->sinfo_flags)
10817 SCTP_STAT_INCR(sctps_sends_with_flags);
10819 if (srcv->sinfo_flags & SCTP_SENDALL) {
10820 /* its a sendall */
10821 error = sctp_sendall(inp, uio, top, srcv);
10826 /* now we must find the assoc */
10827 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
10828 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
10829 SCTP_INP_RLOCK(inp);
10830 stcb = LIST_FIRST(&inp->sctp_asoc_list);
10831 if (stcb == NULL) {
10832 SCTP_INP_RUNLOCK(inp);
10837 SCTP_INP_RUNLOCK(inp);
10839 /* Must locate the net structure if addr given */
10840 net = sctp_findnet(stcb, addr);
10842 /* validate port was 0 or correct */
10843 struct sockaddr_in *sin;
10845 sin = (struct sockaddr_in *)addr;
10846 if ((sin->sin_port != 0) &&
10847 (sin->sin_port != stcb->rport)) {
10851 temp_flags |= SCTP_ADDR_OVER;
10853 net = stcb->asoc.primary_destination;
10854 if (addr && (net == NULL)) {
10855 /* Could not find address, was it legal */
10856 if (addr->sa_family == AF_INET) {
10857 struct sockaddr_in *sin;
10859 sin = (struct sockaddr_in *)addr;
10860 if (sin->sin_addr.s_addr == 0) {
10861 if ((sin->sin_port == 0) ||
10862 (sin->sin_port == stcb->rport)) {
10863 net = stcb->asoc.primary_destination;
10867 struct sockaddr_in6 *sin6;
10869 sin6 = (struct sockaddr_in6 *)addr;
10870 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
10871 if ((sin6->sin6_port == 0) ||
10872 (sin6->sin6_port == stcb->rport)) {
10873 net = stcb->asoc.primary_destination;
10882 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
10883 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
10887 * Must locate the net structure if addr
10890 net = sctp_findnet(stcb, addr);
10892 net = stcb->asoc.primary_destination;
10893 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
10894 ((net == NULL) || (addr == NULL))) {
10895 struct sockaddr_in *sin;
10897 if (addr == NULL) {
10901 sin = (struct sockaddr_in *)addr;
10902 /* Validate port is 0 or correct */
10903 if ((sin->sin_port != 0) &&
10904 (sin->sin_port != stcb->rport)) {
10912 * Since we did not use findep we must
10913 * increment it, and if we don't find a tcb
10916 SCTP_INP_WLOCK(inp);
10917 SCTP_INP_INCR_REF(inp);
10918 SCTP_INP_WUNLOCK(inp);
10919 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
10920 if (stcb == NULL) {
10921 SCTP_INP_WLOCK(inp);
10922 SCTP_INP_DECR_REF(inp);
10923 SCTP_INP_WUNLOCK(inp);
10928 if ((stcb == NULL) && (addr)) {
10929 /* Possible implicit send? */
10930 SCTP_ASOC_CREATE_LOCK(inp);
10931 create_lock_applied = 1;
10932 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
10933 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
10934 /* Should I really unlock ? */
10939 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
10940 (addr->sa_family == AF_INET6)) {
10944 SCTP_INP_WLOCK(inp);
10945 SCTP_INP_INCR_REF(inp);
10946 SCTP_INP_WUNLOCK(inp);
10947 /* With the lock applied look again */
10948 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
10949 if (stcb == NULL) {
10950 SCTP_INP_WLOCK(inp);
10951 SCTP_INP_DECR_REF(inp);
10952 SCTP_INP_WUNLOCK(inp);
10956 if (t_inp != inp) {
10961 if (stcb == NULL) {
10962 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
10963 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
10966 } else if (addr == NULL) {
10971 * UDP style, we must go ahead and start the INIT
10976 if ((use_rcvinfo) && (srcv) &&
10977 ((srcv->sinfo_flags & SCTP_ABORT) ||
10978 ((srcv->sinfo_flags & SCTP_EOF) &&
10980 (uio->uio_resid == 0)))) {
10982 * User asks to abort a non-existant assoc,
10983 * or EOF a non-existant assoc with no data
10988 /* get an asoc/stcb struct */
10989 vrf_id = inp->def_vrf_id;
10990 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
10993 if (stcb == NULL) {
10994 /* Error is setup for us in the call */
10997 if (create_lock_applied) {
10998 SCTP_ASOC_CREATE_UNLOCK(inp);
10999 create_lock_applied = 0;
11001 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
11004 * Turn on queue only flag to prevent data from
11008 asoc = &stcb->asoc;
11009 asoc->state = SCTP_STATE_COOKIE_WAIT;
11010 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
11012 /* initialize authentication params for the assoc */
11013 sctp_initialize_auth_params(inp, stcb);
11017 * see if a init structure exists in cmsg
11020 struct sctp_initmsg initm;
11023 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
11026 * we have an INIT override of the
11029 if (initm.sinit_max_attempts)
11030 asoc->max_init_times = initm.sinit_max_attempts;
11031 if (initm.sinit_num_ostreams)
11032 asoc->pre_open_streams = initm.sinit_num_ostreams;
11033 if (initm.sinit_max_instreams)
11034 asoc->max_inbound_streams = initm.sinit_max_instreams;
11035 if (initm.sinit_max_init_timeo)
11036 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
11037 if (asoc->streamoutcnt < asoc->pre_open_streams) {
11038 /* Default is NOT correct */
11039 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
11040 asoc->streamoutcnt, asoc->pre_open_streams);
11042 * What happens if this
11043 * fails? we panic ...
11046 struct sctp_stream_out *tmp_str;
11049 if (hold_tcblock) {
11051 SCTP_TCB_UNLOCK(stcb);
11053 SCTP_MALLOC(tmp_str,
11054 struct sctp_stream_out *,
11055 (asoc->pre_open_streams *
11056 sizeof(struct sctp_stream_out)),
11059 SCTP_TCB_LOCK(stcb);
11061 if (tmp_str != NULL) {
11062 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
11063 asoc->strmout = tmp_str;
11064 asoc->streamoutcnt = asoc->pre_open_streams;
11066 asoc->pre_open_streams = asoc->streamoutcnt;
11069 for (i = 0; i < asoc->streamoutcnt; i++) {
11071 * inbound side must be set
11072 * to 0xffff, also NOTE when
11073 * we get the INIT-ACK back
11074 * (for INIT sender) we MUST
11076 * (streamoutcnt) but first
11077 * check if we sent to any
11078 * of the upper streams that
11079 * were dropped (if some
11080 * were). Those that were
11081 * dropped must be notified
11082 * to the upper layer as
11085 asoc->strmout[i].next_sequence_sent = 0x0;
11086 TAILQ_INIT(&asoc->strmout[i].outqueue);
11087 asoc->strmout[i].stream_no = i;
11088 asoc->strmout[i].last_msg_incomplete = 0;
11089 asoc->strmout[i].next_spoke.tqe_next = 0;
11090 asoc->strmout[i].next_spoke.tqe_prev = 0;
11096 /* out with the INIT */
11097 queue_only_for_init = 1;
11099 * we may want to dig in after this call and adjust the MTU
11100 * value. It defaulted to 1500 (constant) but the ro
11101 * structure may now have an update and thus we may need to
11102 * change it BEFORE we append the message.
11104 net = stcb->asoc.primary_destination;
11105 asoc = &stcb->asoc;
11108 if ((SCTP_SO_IS_NBIO(so)
11109 || (flags & MSG_NBIO)
11113 asoc = &stcb->asoc;
11115 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
11116 if (sndlen > asoc->smallest_mtu) {
11121 /* would we block? */
11122 if (non_blocking) {
11123 if ((SCTP_SB_LIMIT_SND(so) <
11124 (sndlen + stcb->asoc.total_output_queue_size)) ||
11125 (stcb->asoc.chunks_on_out_queue >
11126 sctp_max_chunks_on_queue)) {
11127 error = EWOULDBLOCK;
11128 atomic_add_int(&stcb->sctp_ep->total_nospaces, 1);
11132 /* Keep the stcb from being freed under our feet */
11133 if (free_cnt_applied)
11134 panic("refcnt already incremented");
11135 atomic_add_int(&stcb->asoc.refcnt, 1);
11136 free_cnt_applied = 1;
11138 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11139 error = ECONNRESET;
11142 if (create_lock_applied) {
11143 SCTP_ASOC_CREATE_UNLOCK(inp);
11144 create_lock_applied = 0;
11146 if (asoc->stream_reset_outstanding) {
11148 * Can't queue any data while stream reset is underway.
11153 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11154 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11157 if ((use_rcvinfo == 0) || (srcv == NULL)) {
11158 /* Grab the default stuff from the asoc */
11159 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
11161 /* we are now done with all control */
11163 sctp_m_freem(control);
11166 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11167 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11168 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11169 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11170 if ((use_rcvinfo) &&
11171 (srcv->sinfo_flags & SCTP_ABORT)) {
11174 error = ECONNRESET;
11178 /* Ok, we will attempt a msgsnd :> */
11180 p->td_ru.ru_msgsnd++;
11183 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
11184 net = stcb->asoc.primary_destination;
11191 if ((net->flight_size > net->cwnd) && (sctp_cmt_on_off == 0)) {
11193 * CMT: Added check for CMT above. net above is the primary
11194 * dest. If CMT is ON, sender should always attempt to send
11195 * with the output routine sctp_fill_outqueue() that loops
11196 * through all destination addresses. Therefore, if CMT is
11197 * ON, queue_only is NOT set to 1 here, so that
11198 * sctp_chunk_output() can be called below.
11202 } else if (asoc->ifp_had_enobuf) {
11203 SCTP_STAT_INCR(sctps_ifnomemqueued);
11204 if (net->flight_size > (net->mtu * 2))
11206 asoc->ifp_had_enobuf = 0;
11208 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11209 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
11211 /* Are we aborting? */
11212 if (srcv->sinfo_flags & SCTP_ABORT) {
11214 int tot_demand, tot_out = 0, max_out;
11216 SCTP_STAT_INCR(sctps_sends_with_abort);
11217 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
11218 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
11219 /* It has to be up before we abort */
11220 /* how big is the user initiated abort? */
11224 if (hold_tcblock) {
11225 SCTP_TCB_UNLOCK(stcb);
11231 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
11235 tot_out += SCTP_BUF_LEN(cntm);
11236 cntm = SCTP_BUF_NEXT(cntm);
11238 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11240 /* Must fit in a MTU */
11242 tot_out = uio->uio_resid;
11243 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
11244 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
11250 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
11251 max_out -= sizeof(struct sctp_abort_msg);
11252 if (tot_out > max_out) {
11256 struct sctp_paramhdr *ph;
11258 /* now move forward the data pointer */
11259 ph = mtod(mm, struct sctp_paramhdr *);
11260 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
11261 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
11263 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
11265 error = uiomove((caddr_t)ph, (int)tot_out, uio);
11268 * Here if we can't get his data we
11269 * still abort we just don't get to
11270 * send the users note :-0
11276 SCTP_BUF_NEXT(mm) = top;
11279 if (hold_tcblock == 0) {
11280 SCTP_TCB_LOCK(stcb);
11283 atomic_add_int(&stcb->asoc.refcnt, -1);
11284 free_cnt_applied = 0;
11285 /* release this lock, otherwise we hang on ourselves */
11286 sctp_abort_an_association(stcb->sctp_ep, stcb,
11287 SCTP_RESPONSE_TO_USER_REQ,
11289 /* now relock the stcb so everything is sane */
11294 /* Calculate the maximum we can send */
11295 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) {
11296 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11300 if (hold_tcblock) {
11301 SCTP_TCB_UNLOCK(stcb);
11304 /* Is the stream no. valid? */
11305 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
11306 /* Invalid stream number */
11310 if (asoc->strmout == NULL) {
11311 /* huh? software error */
11316 if (max_len < sctp_add_more_threshold) {
11317 /* No room right no ! */
11318 SOCKBUF_LOCK(&so->so_snd);
11319 while (SCTP_SB_LIMIT_SND(so) < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
11320 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11321 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
11322 so, asoc, uio->uio_resid);
11325 stcb->block_entry = &be;
11326 error = sbwait(&so->so_snd);
11327 stcb->block_entry = NULL;
11328 if (error || so->so_error || be.error) {
11331 error = so->so_error;
11336 SOCKBUF_UNLOCK(&so->so_snd);
11339 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11340 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
11341 so, asoc, stcb->asoc.total_output_queue_size);
11343 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11347 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) {
11348 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11352 SOCKBUF_UNLOCK(&so->so_snd);
11354 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11357 atomic_add_int(&stcb->total_sends, 1);
11359 struct sctp_stream_queue_pending *sp;
11360 struct sctp_stream_out *strm;
11361 uint32_t sndout, initial_out;
11362 int user_marks_eor;
11364 if (uio->uio_resid == 0) {
11365 if (srcv->sinfo_flags & SCTP_EOF) {
11366 got_all_of_the_send = 1;
11373 initial_out = uio->uio_resid;
11375 SCTP_TCB_SEND_LOCK(stcb);
11376 if ((asoc->stream_locked) &&
11377 (asoc->stream_locked_on != srcv->sinfo_stream)) {
11378 SCTP_TCB_SEND_UNLOCK(stcb);
11382 SCTP_TCB_SEND_UNLOCK(stcb);
11384 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
11385 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11386 if (strm->last_msg_incomplete == 0) {
11388 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
11389 if ((sp == NULL) || (error)) {
11392 SCTP_TCB_SEND_LOCK(stcb);
11393 if (sp->msg_is_complete) {
11394 strm->last_msg_incomplete = 0;
11395 asoc->stream_locked = 0;
11398 * Just got locked to this guy in case of an
11401 strm->last_msg_incomplete = 1;
11402 asoc->stream_locked = 1;
11403 asoc->stream_locked_on = srcv->sinfo_stream;
11404 sp->sender_all_done = 0;
11406 sctp_snd_sb_alloc(stcb, sp->length);
11407 atomic_add_int(&asoc->stream_queue_cnt, 1);
11408 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
11409 sp->strseq = strm->next_sequence_sent;
11410 #ifdef SCTP_LOG_SENDING_STR
11411 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
11412 (uintptr_t) stcb, (uintptr_t) sp,
11413 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
11415 strm->next_sequence_sent++;
11417 SCTP_STAT_INCR(sctps_sends_with_unord);
11419 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
11420 if ((strm->next_spoke.tqe_next == NULL) &&
11421 (strm->next_spoke.tqe_prev == NULL)) {
11422 /* Not on wheel, insert */
11423 sctp_insert_on_wheel(stcb, asoc, strm, 1);
11425 SCTP_TCB_SEND_UNLOCK(stcb);
11427 SCTP_TCB_SEND_LOCK(stcb);
11428 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
11429 SCTP_TCB_SEND_UNLOCK(stcb);
11431 /* ???? Huh ??? last msg is gone */
11433 panic("Warning: Last msg marked incomplete, yet nothing left?");
11435 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
11436 strm->last_msg_incomplete = 0;
11442 while (uio->uio_resid > 0) {
11443 /* How much room do we have? */
11444 struct mbuf *new_tail, *mm;
11446 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
11447 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11451 if ((max_len > sctp_add_more_threshold) ||
11453 (uio->uio_resid < (int)max_len))) {
11456 if (hold_tcblock) {
11457 SCTP_TCB_UNLOCK(stcb);
11460 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
11461 if ((mm == NULL) || error) {
11467 /* Update the mbuf and count */
11468 SCTP_TCB_SEND_LOCK(stcb);
11469 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11471 * we need to get out. Peer probably
11475 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED)
11476 error = ECONNRESET;
11477 SCTP_TCB_SEND_UNLOCK(stcb);
11480 if (sp->tail_mbuf) {
11481 /* tack it to the end */
11482 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
11483 sp->tail_mbuf = new_tail;
11485 /* A stolen mbuf */
11487 sp->tail_mbuf = new_tail;
11489 sctp_snd_sb_alloc(stcb, sndout);
11490 atomic_add_int(&sp->length, sndout);
11493 /* Did we reach EOR? */
11494 if ((uio->uio_resid == 0) &&
11495 ((user_marks_eor == 0) ||
11496 (srcv->sinfo_flags & SCTP_EOF) ||
11497 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
11499 sp->msg_is_complete = 1;
11501 sp->msg_is_complete = 0;
11503 SCTP_TCB_SEND_UNLOCK(stcb);
11505 if (uio->uio_resid == 0) {
11510 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
11512 * This is ugly but we must assure locking
11515 if (hold_tcblock == 0) {
11516 SCTP_TCB_LOCK(stcb);
11519 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
11520 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
11521 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
11527 SCTP_TCB_UNLOCK(stcb);
11530 /* wait for space now */
11531 if (non_blocking) {
11532 /* Non-blocking io in place out */
11535 if ((net->flight_size > net->cwnd) &&
11536 (sctp_cmt_on_off == 0)) {
11539 } else if (asoc->ifp_had_enobuf) {
11540 SCTP_STAT_INCR(sctps_ifnomemqueued);
11541 if (net->flight_size > (net->mtu * 2)) {
11546 asoc->ifp_had_enobuf = 0;
11547 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11548 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11549 sizeof(struct sctp_data_chunk)));
11551 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11552 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11553 sizeof(struct sctp_data_chunk)));
11554 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
11556 SCTP_STAT_INCR(sctps_send_burst_avoid);
11557 } else if (net->flight_size > net->cwnd) {
11559 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
11564 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
11565 (stcb->asoc.total_flight > 0) &&
11566 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
11570 * Ok, Nagle is set on and we have data outstanding.
11571 * Don't send anything and let SACKs drive out the
11572 * data unless wen have a "full" segment to send.
11574 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11575 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
11577 SCTP_STAT_INCR(sctps_naglequeued);
11580 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11581 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
11582 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
11584 SCTP_STAT_INCR(sctps_naglesent);
11587 /* What about the INIT, send it maybe */
11588 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11590 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
11591 nagle_applies, un_sent);
11592 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
11593 stcb->asoc.total_flight,
11594 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
11596 if (queue_only_for_init) {
11597 if (hold_tcblock == 0) {
11598 SCTP_TCB_LOCK(stcb);
11601 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
11602 /* a collision took us forward? */
11603 queue_only_for_init = 0;
11606 sctp_send_initiate(inp, stcb);
11607 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
11608 queue_only_for_init = 0;
11612 if ((queue_only == 0) && (nagle_applies == 0)
11615 * need to start chunk output
11616 * before blocking.. note that if
11617 * a lock is already applied, then
11618 * the input via the net is happening
11619 * and I don't need to start output :-D
11621 if (hold_tcblock == 0) {
11622 if (SCTP_TCB_TRYLOCK(stcb)) {
11624 sctp_chunk_output(inp,
11626 SCTP_OUTPUT_FROM_USR_SEND);
11630 sctp_chunk_output(inp,
11632 SCTP_OUTPUT_FROM_USR_SEND);
11634 if (hold_tcblock == 1) {
11635 SCTP_TCB_UNLOCK(stcb);
11639 SOCKBUF_LOCK(&so->so_snd);
11641 * This is a bit strange, but I think it will
11642 * work. The total_output_queue_size is locked and
11643 * protected by the TCB_LOCK, which we just released.
11644 * There is a race that can occur between releasing it
11645 * above, and me getting the socket lock, where sacks
11646 * come in but we have not put the SB_WAIT on the
11647 * so_snd buffer to get the wakeup. After the LOCK
11648 * is applied the sack_processing will also need to
11649 * LOCK the so->so_snd to do the actual sowwakeup(). So
11650 * once we have the socket buffer lock if we recheck the
11651 * size we KNOW we will get to sleep safely with the
11652 * wakeup flag in place.
11654 if (SCTP_SB_LIMIT_SND(so) < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
11655 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11656 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
11657 so, asoc, uio->uio_resid);
11660 stcb->block_entry = &be;
11661 error = sbwait(&so->so_snd);
11662 stcb->block_entry = NULL;
11664 if (error || so->so_error || be.error) {
11667 error = so->so_error;
11672 SOCKBUF_UNLOCK(&so->so_snd);
11675 if (sctp_logging_level & SCTP_BLK_LOGGING_ENABLE) {
11676 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
11677 so, asoc, stcb->asoc.total_output_queue_size);
11680 SOCKBUF_UNLOCK(&so->so_snd);
11681 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
11685 SCTP_TCB_SEND_LOCK(stcb);
11687 if (sp->msg_is_complete == 0) {
11688 strm->last_msg_incomplete = 1;
11689 asoc->stream_locked = 1;
11690 asoc->stream_locked_on = srcv->sinfo_stream;
11692 sp->sender_all_done = 1;
11693 strm->last_msg_incomplete = 0;
11694 asoc->stream_locked = 0;
11697 SCTP_PRINTF("Huh no sp TSNH?\n");
11698 strm->last_msg_incomplete = 0;
11699 asoc->stream_locked = 0;
11701 SCTP_TCB_SEND_UNLOCK(stcb);
11702 if (uio->uio_resid == 0) {
11703 got_all_of_the_send = 1;
11706 /* We send in a 0, since we do NOT have any locks */
11707 error = sctp_msg_append(stcb, net, top, srcv, 0);
11715 if ((srcv->sinfo_flags & SCTP_EOF) &&
11716 (got_all_of_the_send == 1) &&
11717 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
11721 SCTP_STAT_INCR(sctps_sends_with_eof);
11723 if (hold_tcblock == 0) {
11724 SCTP_TCB_LOCK(stcb);
11727 cnt = sctp_is_there_unsent_data(stcb);
11728 if (TAILQ_EMPTY(&asoc->send_queue) &&
11729 TAILQ_EMPTY(&asoc->sent_queue) &&
11731 if (asoc->locked_on_sending) {
11734 /* there is nothing queued to send, so I'm done... */
11735 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
11736 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
11737 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
11738 /* only send SHUTDOWN the first time through */
11739 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
11740 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
11741 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
11743 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
11744 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
11745 asoc->primary_destination);
11746 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
11747 asoc->primary_destination);
11751 * we still got (or just got) data to send, so set
11755 * XXX sockets draft says that SCTP_EOF should be
11756 * sent with no data. currently, we will allow user
11757 * data to be sent first and move to
11760 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
11761 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
11762 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
11763 if (hold_tcblock == 0) {
11764 SCTP_TCB_LOCK(stcb);
11767 if (asoc->locked_on_sending) {
11768 /* Locked to send out the data */
11769 struct sctp_stream_queue_pending *sp;
11771 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
11773 if ((sp->length == 0) && (sp->msg_is_complete == 0))
11774 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
11777 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
11778 if (TAILQ_EMPTY(&asoc->send_queue) &&
11779 TAILQ_EMPTY(&asoc->sent_queue) &&
11780 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
11782 if (free_cnt_applied) {
11783 atomic_add_int(&stcb->asoc.refcnt, -1);
11784 free_cnt_applied = 0;
11786 sctp_abort_an_association(stcb->sctp_ep, stcb,
11787 SCTP_RESPONSE_TO_USER_REQ,
11790 * now relock the stcb so everything
11797 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
11798 asoc->primary_destination);
11803 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
11804 some_on_control = 1;
11806 if ((net->flight_size > net->cwnd) &&
11807 (sctp_cmt_on_off == 0)) {
11809 } else if (asoc->ifp_had_enobuf) {
11810 SCTP_STAT_INCR(sctps_ifnomemqueued);
11811 if (net->flight_size > (net->mtu * 2)) {
11816 asoc->ifp_had_enobuf = 0;
11817 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11818 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11819 sizeof(struct sctp_data_chunk)));
11821 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11822 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
11823 sizeof(struct sctp_data_chunk)));
11824 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
11826 SCTP_STAT_INCR(sctps_send_burst_avoid);
11827 } else if (net->flight_size > net->cwnd) {
11829 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
11834 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
11835 (stcb->asoc.total_flight > 0) &&
11836 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
11840 * Ok, Nagle is set on and we have data outstanding.
11841 * Don't send anything and let SACKs drive out the
11842 * data unless wen have a "full" segment to send.
11844 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11845 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
11847 SCTP_STAT_INCR(sctps_naglequeued);
11850 if (sctp_logging_level & SCTP_NAGLE_LOGGING_ENABLE) {
11851 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
11852 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
11854 SCTP_STAT_INCR(sctps_naglesent);
11857 if (queue_only_for_init) {
11858 if (hold_tcblock == 0) {
11859 SCTP_TCB_LOCK(stcb);
11862 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
11863 /* a collision took us forward? */
11864 queue_only_for_init = 0;
11867 sctp_send_initiate(inp, stcb);
11868 if (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)
11869 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT |
11870 SCTP_STATE_SHUTDOWN_PENDING;
11872 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
11873 queue_only_for_init = 0;
11877 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
11878 /* we can attempt to send too. */
11879 if (hold_tcblock == 0) {
11881 * If there is activity recv'ing sacks no need to
11884 if (SCTP_TCB_TRYLOCK(stcb)) {
11885 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
11889 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
11891 } else if ((queue_only == 0) &&
11892 (stcb->asoc.peers_rwnd == 0) &&
11893 (stcb->asoc.total_flight == 0)) {
11894 /* We get to have a probe outstanding */
11895 if (hold_tcblock == 0) {
11897 SCTP_TCB_LOCK(stcb);
11899 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
11900 } else if (some_on_control) {
11901 int num_out, reason, cwnd_full, frag_point;
11903 /* Here we do control only */
11904 if (hold_tcblock == 0) {
11906 SCTP_TCB_LOCK(stcb);
11908 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
11909 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
11910 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
11912 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
11913 queue_only, stcb->asoc.peers_rwnd, un_sent,
11914 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
11915 stcb->asoc.total_output_queue_size);
11921 if (create_lock_applied) {
11922 SCTP_ASOC_CREATE_UNLOCK(inp);
11923 create_lock_applied = 0;
11925 if ((stcb) && hold_tcblock) {
11926 SCTP_TCB_UNLOCK(stcb);
11928 if (stcb && free_cnt_applied) {
11929 atomic_add_int(&stcb->asoc.refcnt, -1);
11933 if (mtx_owned(&stcb->tcb_mtx)) {
11934 panic("Leaving with tcb mtx owned?");
11936 if (mtx_owned(&stcb->tcb_send_mtx)) {
11937 panic("Leaving with tcb send mtx owned?");
11945 sctp_m_freem(control);
11952 * generate an AUTHentication chunk, if required
11955 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
11956 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
11957 struct sctp_tcb *stcb, uint8_t chunk)
11959 struct mbuf *m_auth;
11960 struct sctp_auth_chunk *auth;
11963 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
11967 /* sysctl disabled auth? */
11968 if (sctp_auth_disable)
11971 /* peer doesn't do auth... */
11972 if (!stcb->asoc.peer_supports_auth) {
11975 /* does the requested chunk require auth? */
11976 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
11979 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
11980 if (m_auth == NULL) {
11984 /* reserve some space if this will be the first mbuf */
11986 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
11987 /* fill in the AUTH chunk details */
11988 auth = mtod(m_auth, struct sctp_auth_chunk *);
11989 bzero(auth, sizeof(*auth));
11990 auth->ch.chunk_type = SCTP_AUTHENTICATION;
11991 auth->ch.chunk_flags = 0;
11992 chunk_len = sizeof(*auth) +
11993 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
11994 auth->ch.chunk_length = htons(chunk_len);
11995 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
11996 /* key id and hmac digest will be computed and filled in upon send */
11998 /* save the offset where the auth was inserted into the chain */
12005 *offset += SCTP_BUF_LEN(cn);
12006 cn = SCTP_BUF_NEXT(cn);
12011 /* update length and return pointer to the auth chunk */
12012 SCTP_BUF_LEN(m_auth) = chunk_len;
12013 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
12014 if (auth_ret != NULL)