2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/sctp_kdtrace.h>
54 #if defined(INET) || defined(INET6)
55 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
58 #include <machine/in_cksum.h>
60 #define SCTP_MAX_GAPS_INARRAY 4
62 uint8_t right_edge; /* mergable on the right edge */
63 uint8_t left_edge; /* mergable on the left edge */
66 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
69 const struct sack_track sack_array[256] = {
70 {0, 0, 0, 0, /* 0x00 */
77 {1, 0, 1, 0, /* 0x01 */
84 {0, 0, 1, 0, /* 0x02 */
91 {1, 0, 1, 0, /* 0x03 */
98 {0, 0, 1, 0, /* 0x04 */
105 {1, 0, 2, 0, /* 0x05 */
112 {0, 0, 1, 0, /* 0x06 */
119 {1, 0, 1, 0, /* 0x07 */
126 {0, 0, 1, 0, /* 0x08 */
133 {1, 0, 2, 0, /* 0x09 */
140 {0, 0, 2, 0, /* 0x0a */
147 {1, 0, 2, 0, /* 0x0b */
154 {0, 0, 1, 0, /* 0x0c */
161 {1, 0, 2, 0, /* 0x0d */
168 {0, 0, 1, 0, /* 0x0e */
175 {1, 0, 1, 0, /* 0x0f */
182 {0, 0, 1, 0, /* 0x10 */
189 {1, 0, 2, 0, /* 0x11 */
196 {0, 0, 2, 0, /* 0x12 */
203 {1, 0, 2, 0, /* 0x13 */
210 {0, 0, 2, 0, /* 0x14 */
217 {1, 0, 3, 0, /* 0x15 */
224 {0, 0, 2, 0, /* 0x16 */
231 {1, 0, 2, 0, /* 0x17 */
238 {0, 0, 1, 0, /* 0x18 */
245 {1, 0, 2, 0, /* 0x19 */
252 {0, 0, 2, 0, /* 0x1a */
259 {1, 0, 2, 0, /* 0x1b */
266 {0, 0, 1, 0, /* 0x1c */
273 {1, 0, 2, 0, /* 0x1d */
280 {0, 0, 1, 0, /* 0x1e */
287 {1, 0, 1, 0, /* 0x1f */
294 {0, 0, 1, 0, /* 0x20 */
301 {1, 0, 2, 0, /* 0x21 */
308 {0, 0, 2, 0, /* 0x22 */
315 {1, 0, 2, 0, /* 0x23 */
322 {0, 0, 2, 0, /* 0x24 */
329 {1, 0, 3, 0, /* 0x25 */
336 {0, 0, 2, 0, /* 0x26 */
343 {1, 0, 2, 0, /* 0x27 */
350 {0, 0, 2, 0, /* 0x28 */
357 {1, 0, 3, 0, /* 0x29 */
364 {0, 0, 3, 0, /* 0x2a */
371 {1, 0, 3, 0, /* 0x2b */
378 {0, 0, 2, 0, /* 0x2c */
385 {1, 0, 3, 0, /* 0x2d */
392 {0, 0, 2, 0, /* 0x2e */
399 {1, 0, 2, 0, /* 0x2f */
406 {0, 0, 1, 0, /* 0x30 */
413 {1, 0, 2, 0, /* 0x31 */
420 {0, 0, 2, 0, /* 0x32 */
427 {1, 0, 2, 0, /* 0x33 */
434 {0, 0, 2, 0, /* 0x34 */
441 {1, 0, 3, 0, /* 0x35 */
448 {0, 0, 2, 0, /* 0x36 */
455 {1, 0, 2, 0, /* 0x37 */
462 {0, 0, 1, 0, /* 0x38 */
469 {1, 0, 2, 0, /* 0x39 */
476 {0, 0, 2, 0, /* 0x3a */
483 {1, 0, 2, 0, /* 0x3b */
490 {0, 0, 1, 0, /* 0x3c */
497 {1, 0, 2, 0, /* 0x3d */
504 {0, 0, 1, 0, /* 0x3e */
511 {1, 0, 1, 0, /* 0x3f */
518 {0, 0, 1, 0, /* 0x40 */
525 {1, 0, 2, 0, /* 0x41 */
532 {0, 0, 2, 0, /* 0x42 */
539 {1, 0, 2, 0, /* 0x43 */
546 {0, 0, 2, 0, /* 0x44 */
553 {1, 0, 3, 0, /* 0x45 */
560 {0, 0, 2, 0, /* 0x46 */
567 {1, 0, 2, 0, /* 0x47 */
574 {0, 0, 2, 0, /* 0x48 */
581 {1, 0, 3, 0, /* 0x49 */
588 {0, 0, 3, 0, /* 0x4a */
595 {1, 0, 3, 0, /* 0x4b */
602 {0, 0, 2, 0, /* 0x4c */
609 {1, 0, 3, 0, /* 0x4d */
616 {0, 0, 2, 0, /* 0x4e */
623 {1, 0, 2, 0, /* 0x4f */
630 {0, 0, 2, 0, /* 0x50 */
637 {1, 0, 3, 0, /* 0x51 */
644 {0, 0, 3, 0, /* 0x52 */
651 {1, 0, 3, 0, /* 0x53 */
658 {0, 0, 3, 0, /* 0x54 */
665 {1, 0, 4, 0, /* 0x55 */
672 {0, 0, 3, 0, /* 0x56 */
679 {1, 0, 3, 0, /* 0x57 */
686 {0, 0, 2, 0, /* 0x58 */
693 {1, 0, 3, 0, /* 0x59 */
700 {0, 0, 3, 0, /* 0x5a */
707 {1, 0, 3, 0, /* 0x5b */
714 {0, 0, 2, 0, /* 0x5c */
721 {1, 0, 3, 0, /* 0x5d */
728 {0, 0, 2, 0, /* 0x5e */
735 {1, 0, 2, 0, /* 0x5f */
742 {0, 0, 1, 0, /* 0x60 */
749 {1, 0, 2, 0, /* 0x61 */
756 {0, 0, 2, 0, /* 0x62 */
763 {1, 0, 2, 0, /* 0x63 */
770 {0, 0, 2, 0, /* 0x64 */
777 {1, 0, 3, 0, /* 0x65 */
784 {0, 0, 2, 0, /* 0x66 */
791 {1, 0, 2, 0, /* 0x67 */
798 {0, 0, 2, 0, /* 0x68 */
805 {1, 0, 3, 0, /* 0x69 */
812 {0, 0, 3, 0, /* 0x6a */
819 {1, 0, 3, 0, /* 0x6b */
826 {0, 0, 2, 0, /* 0x6c */
833 {1, 0, 3, 0, /* 0x6d */
840 {0, 0, 2, 0, /* 0x6e */
847 {1, 0, 2, 0, /* 0x6f */
854 {0, 0, 1, 0, /* 0x70 */
861 {1, 0, 2, 0, /* 0x71 */
868 {0, 0, 2, 0, /* 0x72 */
875 {1, 0, 2, 0, /* 0x73 */
882 {0, 0, 2, 0, /* 0x74 */
889 {1, 0, 3, 0, /* 0x75 */
896 {0, 0, 2, 0, /* 0x76 */
903 {1, 0, 2, 0, /* 0x77 */
910 {0, 0, 1, 0, /* 0x78 */
917 {1, 0, 2, 0, /* 0x79 */
924 {0, 0, 2, 0, /* 0x7a */
931 {1, 0, 2, 0, /* 0x7b */
938 {0, 0, 1, 0, /* 0x7c */
945 {1, 0, 2, 0, /* 0x7d */
952 {0, 0, 1, 0, /* 0x7e */
959 {1, 0, 1, 0, /* 0x7f */
966 {0, 1, 1, 0, /* 0x80 */
973 {1, 1, 2, 0, /* 0x81 */
980 {0, 1, 2, 0, /* 0x82 */
987 {1, 1, 2, 0, /* 0x83 */
994 {0, 1, 2, 0, /* 0x84 */
1001 {1, 1, 3, 0, /* 0x85 */
1008 {0, 1, 2, 0, /* 0x86 */
1015 {1, 1, 2, 0, /* 0x87 */
1022 {0, 1, 2, 0, /* 0x88 */
1029 {1, 1, 3, 0, /* 0x89 */
1036 {0, 1, 3, 0, /* 0x8a */
1043 {1, 1, 3, 0, /* 0x8b */
1050 {0, 1, 2, 0, /* 0x8c */
1057 {1, 1, 3, 0, /* 0x8d */
1064 {0, 1, 2, 0, /* 0x8e */
1071 {1, 1, 2, 0, /* 0x8f */
1078 {0, 1, 2, 0, /* 0x90 */
1085 {1, 1, 3, 0, /* 0x91 */
1092 {0, 1, 3, 0, /* 0x92 */
1099 {1, 1, 3, 0, /* 0x93 */
1106 {0, 1, 3, 0, /* 0x94 */
1113 {1, 1, 4, 0, /* 0x95 */
1120 {0, 1, 3, 0, /* 0x96 */
1127 {1, 1, 3, 0, /* 0x97 */
1134 {0, 1, 2, 0, /* 0x98 */
1141 {1, 1, 3, 0, /* 0x99 */
1148 {0, 1, 3, 0, /* 0x9a */
1155 {1, 1, 3, 0, /* 0x9b */
1162 {0, 1, 2, 0, /* 0x9c */
1169 {1, 1, 3, 0, /* 0x9d */
1176 {0, 1, 2, 0, /* 0x9e */
1183 {1, 1, 2, 0, /* 0x9f */
1190 {0, 1, 2, 0, /* 0xa0 */
1197 {1, 1, 3, 0, /* 0xa1 */
1204 {0, 1, 3, 0, /* 0xa2 */
1211 {1, 1, 3, 0, /* 0xa3 */
1218 {0, 1, 3, 0, /* 0xa4 */
1225 {1, 1, 4, 0, /* 0xa5 */
1232 {0, 1, 3, 0, /* 0xa6 */
1239 {1, 1, 3, 0, /* 0xa7 */
1246 {0, 1, 3, 0, /* 0xa8 */
1253 {1, 1, 4, 0, /* 0xa9 */
1260 {0, 1, 4, 0, /* 0xaa */
1267 {1, 1, 4, 0, /* 0xab */
1274 {0, 1, 3, 0, /* 0xac */
1281 {1, 1, 4, 0, /* 0xad */
1288 {0, 1, 3, 0, /* 0xae */
1295 {1, 1, 3, 0, /* 0xaf */
1302 {0, 1, 2, 0, /* 0xb0 */
1309 {1, 1, 3, 0, /* 0xb1 */
1316 {0, 1, 3, 0, /* 0xb2 */
1323 {1, 1, 3, 0, /* 0xb3 */
1330 {0, 1, 3, 0, /* 0xb4 */
1337 {1, 1, 4, 0, /* 0xb5 */
1344 {0, 1, 3, 0, /* 0xb6 */
1351 {1, 1, 3, 0, /* 0xb7 */
1358 {0, 1, 2, 0, /* 0xb8 */
1365 {1, 1, 3, 0, /* 0xb9 */
1372 {0, 1, 3, 0, /* 0xba */
1379 {1, 1, 3, 0, /* 0xbb */
1386 {0, 1, 2, 0, /* 0xbc */
1393 {1, 1, 3, 0, /* 0xbd */
1400 {0, 1, 2, 0, /* 0xbe */
1407 {1, 1, 2, 0, /* 0xbf */
1414 {0, 1, 1, 0, /* 0xc0 */
1421 {1, 1, 2, 0, /* 0xc1 */
1428 {0, 1, 2, 0, /* 0xc2 */
1435 {1, 1, 2, 0, /* 0xc3 */
1442 {0, 1, 2, 0, /* 0xc4 */
1449 {1, 1, 3, 0, /* 0xc5 */
1456 {0, 1, 2, 0, /* 0xc6 */
1463 {1, 1, 2, 0, /* 0xc7 */
1470 {0, 1, 2, 0, /* 0xc8 */
1477 {1, 1, 3, 0, /* 0xc9 */
1484 {0, 1, 3, 0, /* 0xca */
1491 {1, 1, 3, 0, /* 0xcb */
1498 {0, 1, 2, 0, /* 0xcc */
1505 {1, 1, 3, 0, /* 0xcd */
1512 {0, 1, 2, 0, /* 0xce */
1519 {1, 1, 2, 0, /* 0xcf */
1526 {0, 1, 2, 0, /* 0xd0 */
1533 {1, 1, 3, 0, /* 0xd1 */
1540 {0, 1, 3, 0, /* 0xd2 */
1547 {1, 1, 3, 0, /* 0xd3 */
1554 {0, 1, 3, 0, /* 0xd4 */
1561 {1, 1, 4, 0, /* 0xd5 */
1568 {0, 1, 3, 0, /* 0xd6 */
1575 {1, 1, 3, 0, /* 0xd7 */
1582 {0, 1, 2, 0, /* 0xd8 */
1589 {1, 1, 3, 0, /* 0xd9 */
1596 {0, 1, 3, 0, /* 0xda */
1603 {1, 1, 3, 0, /* 0xdb */
1610 {0, 1, 2, 0, /* 0xdc */
1617 {1, 1, 3, 0, /* 0xdd */
1624 {0, 1, 2, 0, /* 0xde */
1631 {1, 1, 2, 0, /* 0xdf */
1638 {0, 1, 1, 0, /* 0xe0 */
1645 {1, 1, 2, 0, /* 0xe1 */
1652 {0, 1, 2, 0, /* 0xe2 */
1659 {1, 1, 2, 0, /* 0xe3 */
1666 {0, 1, 2, 0, /* 0xe4 */
1673 {1, 1, 3, 0, /* 0xe5 */
1680 {0, 1, 2, 0, /* 0xe6 */
1687 {1, 1, 2, 0, /* 0xe7 */
1694 {0, 1, 2, 0, /* 0xe8 */
1701 {1, 1, 3, 0, /* 0xe9 */
1708 {0, 1, 3, 0, /* 0xea */
1715 {1, 1, 3, 0, /* 0xeb */
1722 {0, 1, 2, 0, /* 0xec */
1729 {1, 1, 3, 0, /* 0xed */
1736 {0, 1, 2, 0, /* 0xee */
1743 {1, 1, 2, 0, /* 0xef */
1750 {0, 1, 1, 0, /* 0xf0 */
1757 {1, 1, 2, 0, /* 0xf1 */
1764 {0, 1, 2, 0, /* 0xf2 */
1771 {1, 1, 2, 0, /* 0xf3 */
1778 {0, 1, 2, 0, /* 0xf4 */
1785 {1, 1, 3, 0, /* 0xf5 */
1792 {0, 1, 2, 0, /* 0xf6 */
1799 {1, 1, 2, 0, /* 0xf7 */
1806 {0, 1, 1, 0, /* 0xf8 */
1813 {1, 1, 2, 0, /* 0xf9 */
1820 {0, 1, 2, 0, /* 0xfa */
1827 {1, 1, 2, 0, /* 0xfb */
1834 {0, 1, 1, 0, /* 0xfc */
1841 {1, 1, 2, 0, /* 0xfd */
1848 {0, 1, 1, 0, /* 0xfe */
1855 {1, 1, 1, 0, /* 0xff */
1865 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1866 struct sctp_scoping *scope,
1869 if ((scope->loopback_scope == 0) &&
1870 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1872 * skip loopback if not in scope *
1876 switch (ifa->address.sa.sa_family) {
1879 if (scope->ipv4_addr_legal) {
1880 struct sockaddr_in *sin;
1882 sin = &ifa->address.sin;
1883 if (sin->sin_addr.s_addr == 0) {
1884 /* not in scope , unspecified */
1887 if ((scope->ipv4_local_scope == 0) &&
1888 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1889 /* private address not in scope */
1899 if (scope->ipv6_addr_legal) {
1900 struct sockaddr_in6 *sin6;
1903 * Must update the flags, bummer, which means any
1904 * IFA locks must now be applied HERE <->
1907 sctp_gather_internal_ifa_flags(ifa);
1909 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 /* ok to use deprecated addresses? */
1913 sin6 = &ifa->address.sin6;
1914 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1915 /* skip unspecified addresses */
1918 if ( /* (local_scope == 0) && */
1919 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 if ((scope->site_scope == 0) &&
1923 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1937 static struct mbuf *
1938 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1940 #if defined(INET) || defined(INET6)
1941 struct sctp_paramhdr *paramh;
1946 switch (ifa->address.sa.sa_family) {
1949 plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1954 plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1960 #if defined(INET) || defined(INET6)
1961 if (M_TRAILINGSPACE(m) >= plen) {
1962 /* easy side we just drop it on the end */
1963 paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1966 /* Need more space */
1968 while (SCTP_BUF_NEXT(mret) != NULL) {
1969 mret = SCTP_BUF_NEXT(mret);
1971 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
1972 if (SCTP_BUF_NEXT(mret) == NULL) {
1973 /* We are hosed, can't add more addresses */
1976 mret = SCTP_BUF_NEXT(mret);
1977 paramh = mtod(mret, struct sctp_paramhdr *);
1979 /* now add the parameter */
1980 switch (ifa->address.sa.sa_family) {
1984 struct sctp_ipv4addr_param *ipv4p;
1985 struct sockaddr_in *sin;
1987 sin = &ifa->address.sin;
1988 ipv4p = (struct sctp_ipv4addr_param *)paramh;
1989 paramh->param_type = htons(SCTP_IPV4_ADDRESS);
1990 paramh->param_length = htons(plen);
1991 ipv4p->addr = sin->sin_addr.s_addr;
1992 SCTP_BUF_LEN(mret) += plen;
1999 struct sctp_ipv6addr_param *ipv6p;
2000 struct sockaddr_in6 *sin6;
2002 sin6 = &ifa->address.sin6;
2003 ipv6p = (struct sctp_ipv6addr_param *)paramh;
2004 paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2005 paramh->param_length = htons(plen);
2006 memcpy(ipv6p->addr, &sin6->sin6_addr,
2007 sizeof(ipv6p->addr));
2008 /* clear embedded scope in the address */
2009 in6_clearscope((struct in6_addr *)ipv6p->addr);
2010 SCTP_BUF_LEN(mret) += plen;
2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2026 struct sctp_scoping *scope,
2027 struct mbuf *m_at, int cnt_inits_to,
2028 uint16_t *padding_len, uint16_t *chunk_len)
2030 struct sctp_vrf *vrf = NULL;
2031 int cnt, limit_out = 0, total_count;
2034 vrf_id = inp->def_vrf_id;
2035 SCTP_IPI_ADDR_RLOCK();
2036 vrf = sctp_find_vrf(vrf_id);
2038 SCTP_IPI_ADDR_RUNLOCK();
2041 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2042 struct sctp_ifa *sctp_ifap;
2043 struct sctp_ifn *sctp_ifnp;
2046 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2048 cnt = SCTP_ADDRESS_LIMIT;
2051 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2052 if ((scope->loopback_scope == 0) &&
2053 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2055 * Skip loopback devices if loopback_scope
2060 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2062 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2063 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2064 &sctp_ifap->address.sin.sin_addr) != 0)) {
2069 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2070 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2071 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2075 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2078 if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2082 if (cnt > SCTP_ADDRESS_LIMIT) {
2086 if (cnt > SCTP_ADDRESS_LIMIT) {
2093 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2095 if ((scope->loopback_scope == 0) &&
2096 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2098 * Skip loopback devices if
2099 * loopback_scope not set
2103 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2105 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2106 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2107 &sctp_ifap->address.sin.sin_addr) != 0)) {
2112 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2113 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2114 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2118 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2121 if (sctp_is_address_in_scope(sctp_ifap,
2125 if ((chunk_len != NULL) &&
2126 (padding_len != NULL) &&
2127 (*padding_len > 0)) {
2128 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2129 SCTP_BUF_LEN(m_at) += *padding_len;
2130 *chunk_len += *padding_len;
2133 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2144 if (total_count > SCTP_ADDRESS_LIMIT) {
2145 /* No more addresses */
2153 struct sctp_laddr *laddr;
2156 /* First, how many ? */
2157 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2158 if (laddr->ifa == NULL) {
2161 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2163 * Address being deleted by the system, dont
2167 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2169 * Address being deleted on this ep don't
2174 if (sctp_is_address_in_scope(laddr->ifa,
2181 * To get through a NAT we only list addresses if we have
2182 * more than one. That way if you just bind a single address
2183 * we let the source of the init dictate our address.
2187 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2188 if (laddr->ifa == NULL) {
2191 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2194 if (sctp_is_address_in_scope(laddr->ifa,
2198 if ((chunk_len != NULL) &&
2199 (padding_len != NULL) &&
2200 (*padding_len > 0)) {
2201 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2202 SCTP_BUF_LEN(m_at) += *padding_len;
2203 *chunk_len += *padding_len;
2206 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2208 if (cnt >= SCTP_ADDRESS_LIMIT) {
2214 SCTP_IPI_ADDR_RUNLOCK();
2218 static struct sctp_ifa *
2219 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2220 uint8_t dest_is_loop,
2221 uint8_t dest_is_priv,
2224 uint8_t dest_is_global = 0;
2226 /* dest_is_priv is true if destination is a private address */
2227 /* dest_is_loop is true if destination is a loopback addresses */
2230 * Here we determine if its a preferred address. A preferred address
2231 * means it is the same scope or higher scope then the destination.
2232 * L = loopback, P = private, G = global
2233 * -----------------------------------------
2234 * src | dest | result
2235 * ----------------------------------------
2237 * -----------------------------------------
2238 * P | L | yes-v4 no-v6
2239 * -----------------------------------------
2240 * G | L | yes-v4 no-v6
2241 * -----------------------------------------
2243 * -----------------------------------------
2245 * -----------------------------------------
2247 * -----------------------------------------
2249 * -----------------------------------------
2251 * -----------------------------------------
2253 * -----------------------------------------
2256 if (ifa->address.sa.sa_family != fam) {
2257 /* forget mis-matched family */
2260 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2263 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2264 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2265 /* Ok the address may be ok */
2267 if (fam == AF_INET6) {
2268 /* ok to use deprecated addresses? no lets not! */
2269 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2270 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2273 if (ifa->src_is_priv && !ifa->src_is_loop) {
2275 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2279 if (ifa->src_is_glob) {
2281 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2288 * Now that we know what is what, implement or table this could in
2289 * theory be done slicker (it used to be), but this is
2290 * straightforward and easier to validate :-)
2292 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2293 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2294 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2295 dest_is_loop, dest_is_priv, dest_is_global);
2297 if ((ifa->src_is_loop) && (dest_is_priv)) {
2298 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2301 if ((ifa->src_is_glob) && (dest_is_priv)) {
2302 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2305 if ((ifa->src_is_loop) && (dest_is_global)) {
2306 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2309 if ((ifa->src_is_priv) && (dest_is_global)) {
2310 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2313 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2314 /* its a preferred address */
2318 static struct sctp_ifa *
2319 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2320 uint8_t dest_is_loop,
2321 uint8_t dest_is_priv,
2324 uint8_t dest_is_global = 0;
2327 * Here we determine if its a acceptable address. A acceptable
2328 * address means it is the same scope or higher scope but we can
2329 * allow for NAT which means its ok to have a global dest and a
2332 * L = loopback, P = private, G = global
2333 * -----------------------------------------
2334 * src | dest | result
2335 * -----------------------------------------
2337 * -----------------------------------------
2338 * P | L | yes-v4 no-v6
2339 * -----------------------------------------
2341 * -----------------------------------------
2343 * -----------------------------------------
2345 * -----------------------------------------
2346 * G | P | yes - May not work
2347 * -----------------------------------------
2349 * -----------------------------------------
2350 * P | G | yes - May not work
2351 * -----------------------------------------
2353 * -----------------------------------------
2356 if (ifa->address.sa.sa_family != fam) {
2357 /* forget non matching family */
2358 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2359 ifa->address.sa.sa_family, fam);
2362 /* Ok the address may be ok */
2363 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2364 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2365 dest_is_loop, dest_is_priv);
2366 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2370 if (fam == AF_INET6) {
2371 /* ok to use deprecated addresses? */
2372 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2375 if (ifa->src_is_priv) {
2376 /* Special case, linklocal to loop */
2383 * Now that we know what is what, implement our table. This could in
2384 * theory be done slicker (it used to be), but this is
2385 * straightforward and easier to validate :-)
2387 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2390 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2393 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2396 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2399 SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2400 /* its an acceptable address */
2405 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2407 struct sctp_laddr *laddr;
2410 /* There are no restrictions, no TCB :-) */
2413 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2414 if (laddr->ifa == NULL) {
2415 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2419 if (laddr->ifa == ifa) {
2420 /* Yes it is on the list */
2428 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2430 struct sctp_laddr *laddr;
2434 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2435 if (laddr->ifa == NULL) {
2436 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2440 if ((laddr->ifa == ifa) && laddr->action == 0)
2447 static struct sctp_ifa *
2448 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2451 int non_asoc_addr_ok,
2452 uint8_t dest_is_priv,
2453 uint8_t dest_is_loop,
2456 struct sctp_laddr *laddr, *starting_point;
2459 struct sctp_ifn *sctp_ifn;
2460 struct sctp_ifa *sctp_ifa, *sifa;
2461 struct sctp_vrf *vrf;
2464 vrf = sctp_find_vrf(vrf_id);
2468 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2469 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2470 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2472 * first question, is the ifn we will emit on in our list, if so, we
2473 * want such an address. Note that we first looked for a preferred
2477 /* is a preferred one on the interface we route out? */
2478 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2480 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2481 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2482 &sctp_ifa->address.sin.sin_addr) != 0)) {
2487 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2488 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2489 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2493 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2494 (non_asoc_addr_ok == 0))
2496 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2501 if (sctp_is_addr_in_ep(inp, sifa)) {
2502 atomic_add_int(&sifa->refcount, 1);
2508 * ok, now we now need to find one on the list of the addresses. We
2509 * can't get one on the emitting interface so let's find first a
2510 * preferred one. If not that an acceptable one otherwise... we
2513 starting_point = inp->next_addr_touse;
2515 if (inp->next_addr_touse == NULL) {
2516 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2519 for (laddr = inp->next_addr_touse; laddr;
2520 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2521 if (laddr->ifa == NULL) {
2522 /* address has been removed */
2525 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2526 /* address is being deleted */
2529 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2533 atomic_add_int(&sifa->refcount, 1);
2536 if (resettotop == 0) {
2537 inp->next_addr_touse = NULL;
2541 inp->next_addr_touse = starting_point;
2544 if (inp->next_addr_touse == NULL) {
2545 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2549 /* ok, what about an acceptable address in the inp */
2550 for (laddr = inp->next_addr_touse; laddr;
2551 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2552 if (laddr->ifa == NULL) {
2553 /* address has been removed */
2556 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2557 /* address is being deleted */
2560 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2564 atomic_add_int(&sifa->refcount, 1);
2567 if (resettotop == 0) {
2568 inp->next_addr_touse = NULL;
2569 goto once_again_too;
2573 * no address bound can be a source for the destination we are in
2579 static struct sctp_ifa *
2580 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2581 struct sctp_tcb *stcb,
2584 uint8_t dest_is_priv,
2585 uint8_t dest_is_loop,
2586 int non_asoc_addr_ok,
2589 struct sctp_laddr *laddr, *starting_point;
2591 struct sctp_ifn *sctp_ifn;
2592 struct sctp_ifa *sctp_ifa, *sifa;
2593 uint8_t start_at_beginning = 0;
2594 struct sctp_vrf *vrf;
2598 * first question, is the ifn we will emit on in our list, if so, we
2601 vrf = sctp_find_vrf(vrf_id);
2605 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2606 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2607 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2610 * first question, is the ifn we will emit on in our list? If so,
2611 * we want that one. First we look for a preferred. Second, we go
2612 * for an acceptable.
2615 /* first try for a preferred address on the ep */
2616 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2618 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2619 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2620 &sctp_ifa->address.sin.sin_addr) != 0)) {
2625 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2626 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2627 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2631 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2633 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2634 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2637 if (((non_asoc_addr_ok == 0) &&
2638 (sctp_is_addr_restricted(stcb, sifa))) ||
2639 (non_asoc_addr_ok &&
2640 (sctp_is_addr_restricted(stcb, sifa)) &&
2641 (!sctp_is_addr_pending(stcb, sifa)))) {
2642 /* on the no-no list */
2645 atomic_add_int(&sifa->refcount, 1);
2649 /* next try for an acceptable address on the ep */
2650 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2652 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2653 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2654 &sctp_ifa->address.sin.sin_addr) != 0)) {
2659 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2660 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2661 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2665 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2667 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2668 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2671 if (((non_asoc_addr_ok == 0) &&
2672 (sctp_is_addr_restricted(stcb, sifa))) ||
2673 (non_asoc_addr_ok &&
2674 (sctp_is_addr_restricted(stcb, sifa)) &&
2675 (!sctp_is_addr_pending(stcb, sifa)))) {
2676 /* on the no-no list */
2679 atomic_add_int(&sifa->refcount, 1);
2685 * if we can't find one like that then we must look at all addresses
2686 * bound to pick one at first preferable then secondly acceptable.
2688 starting_point = stcb->asoc.last_used_address;
2690 if (stcb->asoc.last_used_address == NULL) {
2691 start_at_beginning = 1;
2692 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2694 /* search beginning with the last used address */
2695 for (laddr = stcb->asoc.last_used_address; laddr;
2696 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2697 if (laddr->ifa == NULL) {
2698 /* address has been removed */
2701 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2702 /* address is being deleted */
2705 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2708 if (((non_asoc_addr_ok == 0) &&
2709 (sctp_is_addr_restricted(stcb, sifa))) ||
2710 (non_asoc_addr_ok &&
2711 (sctp_is_addr_restricted(stcb, sifa)) &&
2712 (!sctp_is_addr_pending(stcb, sifa)))) {
2713 /* on the no-no list */
2716 stcb->asoc.last_used_address = laddr;
2717 atomic_add_int(&sifa->refcount, 1);
2720 if (start_at_beginning == 0) {
2721 stcb->asoc.last_used_address = NULL;
2722 goto sctp_from_the_top;
2724 /* now try for any higher scope than the destination */
2725 stcb->asoc.last_used_address = starting_point;
2726 start_at_beginning = 0;
2728 if (stcb->asoc.last_used_address == NULL) {
2729 start_at_beginning = 1;
2730 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2732 /* search beginning with the last used address */
2733 for (laddr = stcb->asoc.last_used_address; laddr;
2734 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2735 if (laddr->ifa == NULL) {
2736 /* address has been removed */
2739 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2740 /* address is being deleted */
2743 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2747 if (((non_asoc_addr_ok == 0) &&
2748 (sctp_is_addr_restricted(stcb, sifa))) ||
2749 (non_asoc_addr_ok &&
2750 (sctp_is_addr_restricted(stcb, sifa)) &&
2751 (!sctp_is_addr_pending(stcb, sifa)))) {
2752 /* on the no-no list */
2755 stcb->asoc.last_used_address = laddr;
2756 atomic_add_int(&sifa->refcount, 1);
2759 if (start_at_beginning == 0) {
2760 stcb->asoc.last_used_address = NULL;
2761 goto sctp_from_the_top2;
2766 static struct sctp_ifa *
2767 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2768 struct sctp_inpcb *inp,
2769 struct sctp_tcb *stcb,
2770 int non_asoc_addr_ok,
2771 uint8_t dest_is_loop,
2772 uint8_t dest_is_priv,
2777 struct sctp_ifa *ifa, *sifa;
2778 int num_eligible_addr = 0;
2780 struct sockaddr_in6 sin6, lsa6;
2782 if (fam == AF_INET6) {
2783 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2784 (void)sa6_recoverscope(&sin6);
2787 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2789 if ((ifa->address.sa.sa_family == AF_INET) &&
2790 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2791 &ifa->address.sin.sin_addr) != 0)) {
2796 if ((ifa->address.sa.sa_family == AF_INET6) &&
2797 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2798 &ifa->address.sin6.sin6_addr) != 0)) {
2802 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2803 (non_asoc_addr_ok == 0))
2805 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2810 if (fam == AF_INET6 &&
2812 sifa->src_is_loop && sifa->src_is_priv) {
2814 * don't allow fe80::1 to be a src on loop ::1, we
2815 * don't list it to the peer so we will get an
2820 if (fam == AF_INET6 &&
2821 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2822 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2824 * link-local <-> link-local must belong to the same
2827 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2828 (void)sa6_recoverscope(&lsa6);
2829 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2836 * Check if the IPv6 address matches to next-hop. In the
2837 * mobile case, old IPv6 address may be not deleted from the
2838 * interface. Then, the interface has previous and new
2839 * addresses. We should use one corresponding to the
2840 * next-hop. (by micchie)
2843 if (stcb && fam == AF_INET6 &&
2844 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2845 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2851 /* Avoid topologically incorrect IPv4 address */
2852 if (stcb && fam == AF_INET &&
2853 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2854 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2860 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2863 if (((non_asoc_addr_ok == 0) &&
2864 (sctp_is_addr_restricted(stcb, sifa))) ||
2865 (non_asoc_addr_ok &&
2866 (sctp_is_addr_restricted(stcb, sifa)) &&
2867 (!sctp_is_addr_pending(stcb, sifa)))) {
2869 * It is restricted for some reason..
2870 * probably not yet added.
2875 if (num_eligible_addr >= addr_wanted) {
2878 num_eligible_addr++;
2884 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2885 struct sctp_inpcb *inp,
2886 struct sctp_tcb *stcb,
2887 int non_asoc_addr_ok,
2888 uint8_t dest_is_loop,
2889 uint8_t dest_is_priv,
2892 struct sctp_ifa *ifa, *sifa;
2893 int num_eligible_addr = 0;
2895 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2897 if ((ifa->address.sa.sa_family == AF_INET) &&
2898 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2899 &ifa->address.sin.sin_addr) != 0)) {
2904 if ((ifa->address.sa.sa_family == AF_INET6) &&
2906 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2907 &ifa->address.sin6.sin6_addr) != 0)) {
2911 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2912 (non_asoc_addr_ok == 0)) {
2915 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2921 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2924 if (((non_asoc_addr_ok == 0) &&
2925 (sctp_is_addr_restricted(stcb, sifa))) ||
2926 (non_asoc_addr_ok &&
2927 (sctp_is_addr_restricted(stcb, sifa)) &&
2928 (!sctp_is_addr_pending(stcb, sifa)))) {
2930 * It is restricted for some reason..
2931 * probably not yet added.
2936 num_eligible_addr++;
2938 return (num_eligible_addr);
2941 static struct sctp_ifa *
2942 sctp_choose_boundall(struct sctp_inpcb *inp,
2943 struct sctp_tcb *stcb,
2944 struct sctp_nets *net,
2947 uint8_t dest_is_priv,
2948 uint8_t dest_is_loop,
2949 int non_asoc_addr_ok,
2952 int cur_addr_num = 0, num_preferred = 0;
2954 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2955 struct sctp_ifa *sctp_ifa, *sifa;
2957 struct sctp_vrf *vrf;
2963 * For boundall we can use any address in the association.
2964 * If non_asoc_addr_ok is set we can use any address (at least in
2965 * theory). So we look for preferred addresses first. If we find one,
2966 * we use it. Otherwise we next try to get an address on the
2967 * interface, which we should be able to do (unless non_asoc_addr_ok
2968 * is false and we are routed out that way). In these cases where we
2969 * can't use the address of the interface we go through all the
2970 * ifn's looking for an address we can use and fill that in. Punting
2971 * means we send back address 0, which will probably cause problems
2972 * actually since then IP will fill in the address of the route ifn,
2973 * which means we probably already rejected it.. i.e. here comes an
2976 vrf = sctp_find_vrf(vrf_id);
2980 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2981 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2982 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2983 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2984 if (sctp_ifn == NULL) {
2985 /* ?? We don't have this guy ?? */
2986 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2987 goto bound_all_plan_b;
2989 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2990 ifn_index, sctp_ifn->ifn_name);
2993 cur_addr_num = net->indx_of_eligible_next_to_use;
2995 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
3000 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3001 num_preferred, sctp_ifn->ifn_name);
3002 if (num_preferred == 0) {
3004 * no eligible addresses, we must use some other interface
3005 * address if we can find one.
3007 goto bound_all_plan_b;
3010 * Ok we have num_eligible_addr set with how many we can use, this
3011 * may vary from call to call due to addresses being deprecated
3014 if (cur_addr_num >= num_preferred) {
3018 * select the nth address from the list (where cur_addr_num is the
3019 * nth) and 0 is the first one, 1 is the second one etc...
3021 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3023 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3024 dest_is_priv, cur_addr_num, fam, ro);
3026 /* if sctp_ifa is NULL something changed??, fall to plan b. */
3028 atomic_add_int(&sctp_ifa->refcount, 1);
3030 /* save off where the next one we will want */
3031 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3036 * plan_b: Look at all interfaces and find a preferred address. If
3037 * no preferred fall through to plan_c.
3040 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3041 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3042 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3043 sctp_ifn->ifn_name);
3044 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3045 /* wrong base scope */
3046 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3049 if ((sctp_ifn == looked_at) && looked_at) {
3050 /* already looked at this guy */
3051 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3054 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3055 dest_is_loop, dest_is_priv, fam);
3056 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3057 "Found ifn:%p %d preferred source addresses\n",
3058 ifn, num_preferred);
3059 if (num_preferred == 0) {
3060 /* None on this interface. */
3061 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3064 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3065 "num preferred:%d on interface:%p cur_addr_num:%d\n",
3066 num_preferred, (void *)sctp_ifn, cur_addr_num);
3069 * Ok we have num_eligible_addr set with how many we can
3070 * use, this may vary from call to call due to addresses
3071 * being deprecated etc..
3073 if (cur_addr_num >= num_preferred) {
3076 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3077 dest_is_priv, cur_addr_num, fam, ro);
3081 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3082 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3084 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3085 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3086 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3087 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3089 atomic_add_int(&sifa->refcount, 1);
3093 again_with_private_addresses_allowed:
3095 /* plan_c: do we have an acceptable address on the emit interface */
3097 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3098 if (emit_ifn == NULL) {
3099 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3102 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3103 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3105 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3106 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3107 &sctp_ifa->address.sin.sin_addr) != 0)) {
3108 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3113 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3114 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3115 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3116 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3120 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3121 (non_asoc_addr_ok == 0)) {
3122 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3125 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3128 SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3132 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3133 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3137 if (((non_asoc_addr_ok == 0) &&
3138 (sctp_is_addr_restricted(stcb, sifa))) ||
3139 (non_asoc_addr_ok &&
3140 (sctp_is_addr_restricted(stcb, sifa)) &&
3141 (!sctp_is_addr_pending(stcb, sifa)))) {
3143 * It is restricted for some reason..
3144 * probably not yet added.
3146 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3151 atomic_add_int(&sifa->refcount, 1);
3156 * plan_d: We are in trouble. No preferred address on the emit
3157 * interface. And not even a preferred address on all interfaces. Go
3158 * out and see if we can find an acceptable address somewhere
3159 * amongst all interfaces.
3161 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3162 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3163 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3164 /* wrong base scope */
3167 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3169 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3170 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3171 &sctp_ifa->address.sin.sin_addr) != 0)) {
3176 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3177 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3178 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3182 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3183 (non_asoc_addr_ok == 0))
3185 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3191 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3195 if (((non_asoc_addr_ok == 0) &&
3196 (sctp_is_addr_restricted(stcb, sifa))) ||
3197 (non_asoc_addr_ok &&
3198 (sctp_is_addr_restricted(stcb, sifa)) &&
3199 (!sctp_is_addr_pending(stcb, sifa)))) {
3201 * It is restricted for some
3202 * reason.. probably not yet added.
3213 if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3214 stcb->asoc.scope.ipv4_local_scope = 1;
3216 goto again_with_private_addresses_allowed;
3217 } else if (retried == 1) {
3218 stcb->asoc.scope.ipv4_local_scope = 0;
3226 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3227 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3228 /* wrong base scope */
3231 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3232 struct sctp_ifa *tmp_sifa;
3235 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3236 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3237 &sctp_ifa->address.sin.sin_addr) != 0)) {
3242 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3243 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3244 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3248 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3249 (non_asoc_addr_ok == 0))
3251 tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3254 if (tmp_sifa == NULL) {
3257 if (tmp_sifa == sifa) {
3261 if (sctp_is_address_in_scope(tmp_sifa,
3262 &stcb->asoc.scope, 0) == 0) {
3265 if (((non_asoc_addr_ok == 0) &&
3266 (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3267 (non_asoc_addr_ok &&
3268 (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3269 (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3279 if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3280 (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3281 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3286 atomic_add_int(&sifa->refcount, 1);
3292 /* tcb may be NULL */
3294 sctp_source_address_selection(struct sctp_inpcb *inp,
3295 struct sctp_tcb *stcb,
3297 struct sctp_nets *net,
3298 int non_asoc_addr_ok, uint32_t vrf_id)
3300 struct sctp_ifa *answer;
3301 uint8_t dest_is_priv, dest_is_loop;
3304 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3307 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3312 * - Find the route if needed, cache if I can.
3313 * - Look at interface address in route, Is it in the bound list. If so we
3314 * have the best source.
3315 * - If not we must rotate amongst the addresses.
3317 * Caveats and issues
3319 * Do we need to pay attention to scope. We can have a private address
3320 * or a global address we are sourcing or sending to. So if we draw
3322 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3324 * ------------------------------------------
3325 * source * dest * result
3326 * -----------------------------------------
3327 * <a> Private * Global * NAT
3328 * -----------------------------------------
3329 * <b> Private * Private * No problem
3330 * -----------------------------------------
3331 * <c> Global * Private * Huh, How will this work?
3332 * -----------------------------------------
3333 * <d> Global * Global * No Problem
3334 *------------------------------------------
3335 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3337 *------------------------------------------
3338 * source * dest * result
3339 * -----------------------------------------
3340 * <a> Linklocal * Global *
3341 * -----------------------------------------
3342 * <b> Linklocal * Linklocal * No problem
3343 * -----------------------------------------
3344 * <c> Global * Linklocal * Huh, How will this work?
3345 * -----------------------------------------
3346 * <d> Global * Global * No Problem
3347 *------------------------------------------
3348 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3350 * And then we add to that what happens if there are multiple addresses
3351 * assigned to an interface. Remember the ifa on a ifn is a linked
3352 * list of addresses. So one interface can have more than one IP
3353 * address. What happens if we have both a private and a global
3354 * address? Do we then use context of destination to sort out which
3355 * one is best? And what about NAT's sending P->G may get you a NAT
3356 * translation, or should you select the G thats on the interface in
3361 * - count the number of addresses on the interface.
3362 * - if it is one, no problem except case <c>.
3363 * For <a> we will assume a NAT out there.
3364 * - if there are more than one, then we need to worry about scope P
3365 * or G. We should prefer G -> G and P -> P if possible.
3366 * Then as a secondary fall back to mixed types G->P being a last
3368 * - The above all works for bound all, but bound specific we need to
3369 * use the same concept but instead only consider the bound
3370 * addresses. If the bound set is NOT assigned to the interface then
3371 * we must use rotation amongst the bound addresses..
3373 if (ro->ro_nh == NULL) {
3375 * Need a route to cache.
3377 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3379 if (ro->ro_nh == NULL) {
3382 fam = ro->ro_dst.sa_family;
3383 dest_is_priv = dest_is_loop = 0;
3384 /* Setup our scopes for the destination */
3388 /* Scope based on outbound address */
3389 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3392 /* mark it as local */
3393 net->addr_is_local = 1;
3395 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3402 /* Scope based on outbound address */
3403 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3404 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3406 * If the address is a loopback address, which
3407 * consists of "::1" OR "fe80::1%lo0", we are
3408 * loopback scope. But we don't use dest_is_priv
3409 * (link local addresses).
3413 /* mark it as local */
3414 net->addr_is_local = 1;
3416 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3422 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3423 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3424 SCTP_IPI_ADDR_RLOCK();
3425 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3429 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3430 dest_is_priv, dest_is_loop,
3431 non_asoc_addr_ok, fam);
3432 SCTP_IPI_ADDR_RUNLOCK();
3439 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3440 vrf_id, dest_is_priv,
3442 non_asoc_addr_ok, fam);
3444 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3449 SCTP_IPI_ADDR_RUNLOCK();
3454 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3457 struct sctp_sndinfo sndinfo;
3458 struct sctp_prinfo prinfo;
3459 struct sctp_authinfo authinfo;
3460 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3464 * Independent of how many mbufs, find the c_type inside the control
3465 * structure and copy out the data.
3468 tot_len = SCTP_BUF_LEN(control);
3469 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3470 rem_len = tot_len - off;
3471 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3472 /* There is not enough room for one more. */
3475 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3476 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3477 /* We dont't have a complete CMSG header. */
3480 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3481 /* We don't have the complete CMSG. */
3484 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3485 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3486 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3487 ((c_type == cmh.cmsg_type) ||
3488 ((c_type == SCTP_SNDRCV) &&
3489 ((cmh.cmsg_type == SCTP_SNDINFO) ||
3490 (cmh.cmsg_type == SCTP_PRINFO) ||
3491 (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3492 if (c_type == cmh.cmsg_type) {
3493 if (cpsize > INT_MAX) {
3496 if (cmsg_data_len < (int)cpsize) {
3499 /* It is exactly what we want. Copy it out. */
3500 m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3503 struct sctp_sndrcvinfo *sndrcvinfo;
3505 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3507 if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3510 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3512 switch (cmh.cmsg_type) {
3514 if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3517 m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3518 sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3519 sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3520 sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3521 sndrcvinfo->sinfo_context = sndinfo.snd_context;
3522 sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3525 if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3528 m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3529 if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3530 sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3532 sndrcvinfo->sinfo_timetolive = 0;
3534 sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3537 if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3540 m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3541 sndrcvinfo->sinfo_keynumber_valid = 1;
3542 sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3555 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3558 struct sctp_initmsg initmsg;
3560 struct sockaddr_in sin;
3563 struct sockaddr_in6 sin6;
3565 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3567 tot_len = SCTP_BUF_LEN(control);
3568 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3569 rem_len = tot_len - off;
3570 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3571 /* There is not enough room for one more. */
3575 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3576 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3577 /* We dont't have a complete CMSG header. */
3581 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3582 /* We don't have the complete CMSG. */
3586 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3587 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3588 if (cmh.cmsg_level == IPPROTO_SCTP) {
3589 switch (cmh.cmsg_type) {
3591 if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3595 m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3596 if (initmsg.sinit_max_attempts)
3597 stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3598 if (initmsg.sinit_num_ostreams)
3599 stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3600 if (initmsg.sinit_max_instreams)
3601 stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3602 if (initmsg.sinit_max_init_timeo)
3603 stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3604 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3605 struct sctp_stream_out *tmp_str;
3607 #if defined(SCTP_DETAILED_STR_STATS)
3611 /* Default is NOT correct */
3612 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3613 stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3614 SCTP_TCB_UNLOCK(stcb);
3615 SCTP_MALLOC(tmp_str,
3616 struct sctp_stream_out *,
3617 (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3619 SCTP_TCB_LOCK(stcb);
3620 if (tmp_str != NULL) {
3621 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3622 stcb->asoc.strmout = tmp_str;
3623 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3625 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3627 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3628 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3629 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3630 stcb->asoc.strmout[i].chunks_on_queues = 0;
3631 #if defined(SCTP_DETAILED_STR_STATS)
3632 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3633 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3634 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3637 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3638 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3640 stcb->asoc.strmout[i].next_mid_ordered = 0;
3641 stcb->asoc.strmout[i].next_mid_unordered = 0;
3642 stcb->asoc.strmout[i].sid = i;
3643 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3644 stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3649 case SCTP_DSTADDRV4:
3650 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3654 memset(&sin, 0, sizeof(struct sockaddr_in));
3655 sin.sin_family = AF_INET;
3656 sin.sin_len = sizeof(struct sockaddr_in);
3657 sin.sin_port = stcb->rport;
3658 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3659 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3660 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3661 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3665 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3666 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3673 case SCTP_DSTADDRV6:
3674 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3678 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3679 sin6.sin6_family = AF_INET6;
3680 sin6.sin6_len = sizeof(struct sockaddr_in6);
3681 sin6.sin6_port = stcb->rport;
3682 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3683 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3684 IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3689 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3690 in6_sin6_2_sin(&sin, &sin6);
3691 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3692 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3693 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3697 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3698 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3704 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3705 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3719 #if defined(INET) || defined(INET6)
3720 static struct sctp_tcb *
3721 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3723 struct mbuf *control,
3724 struct sctp_nets **net_p,
3728 struct sctp_tcb *stcb;
3729 struct sockaddr *addr;
3731 struct sockaddr_in sin;
3734 struct sockaddr_in6 sin6;
3736 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3738 tot_len = SCTP_BUF_LEN(control);
3739 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3740 rem_len = tot_len - off;
3741 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3742 /* There is not enough room for one more. */
3746 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3747 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3748 /* We dont't have a complete CMSG header. */
3752 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3753 /* We don't have the complete CMSG. */
3757 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3758 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3759 if (cmh.cmsg_level == IPPROTO_SCTP) {
3760 switch (cmh.cmsg_type) {
3762 case SCTP_DSTADDRV4:
3763 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3767 memset(&sin, 0, sizeof(struct sockaddr_in));
3768 sin.sin_family = AF_INET;
3769 sin.sin_len = sizeof(struct sockaddr_in);
3770 sin.sin_port = port;
3771 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3772 addr = (struct sockaddr *)&sin;
3776 case SCTP_DSTADDRV6:
3777 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3781 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3782 sin6.sin6_family = AF_INET6;
3783 sin6.sin6_len = sizeof(struct sockaddr_in6);
3784 sin6.sin6_port = port;
3785 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3787 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3788 in6_sin6_2_sin(&sin, &sin6);
3789 addr = (struct sockaddr *)&sin;
3792 addr = (struct sockaddr *)&sin6;
3800 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3811 static struct mbuf *
3812 sctp_add_cookie(struct mbuf *init, int init_offset,
3813 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3815 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3816 struct sctp_state_cookie *stc;
3817 struct sctp_paramhdr *ph;
3820 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3821 sizeof(struct sctp_paramhdr)), 0,
3822 M_NOWAIT, 1, MT_DATA);
3826 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3827 if (copy_init == NULL) {
3831 #ifdef SCTP_MBUF_LOGGING
3832 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3833 sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3836 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3838 if (copy_initack == NULL) {
3840 sctp_m_freem(copy_init);
3843 #ifdef SCTP_MBUF_LOGGING
3844 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3845 sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3848 /* easy side we just drop it on the end */
3849 ph = mtod(mret, struct sctp_paramhdr *);
3850 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3851 sizeof(struct sctp_paramhdr);
3852 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3853 sizeof(struct sctp_paramhdr));
3854 ph->param_type = htons(SCTP_STATE_COOKIE);
3855 ph->param_length = 0; /* fill in at the end */
3856 /* Fill in the stc cookie data */
3857 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3859 /* tack the INIT and then the INIT-ACK onto the chain */
3861 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3862 cookie_sz += SCTP_BUF_LEN(m_at);
3863 if (SCTP_BUF_NEXT(m_at) == NULL) {
3864 SCTP_BUF_NEXT(m_at) = copy_init;
3868 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3869 cookie_sz += SCTP_BUF_LEN(m_at);
3870 if (SCTP_BUF_NEXT(m_at) == NULL) {
3871 SCTP_BUF_NEXT(m_at) = copy_initack;
3875 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3876 cookie_sz += SCTP_BUF_LEN(m_at);
3877 if (SCTP_BUF_NEXT(m_at) == NULL) {
3881 sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3883 /* no space, so free the entire chain */
3887 SCTP_BUF_NEXT(m_at) = sig;
3888 SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
3889 cookie_sz += SCTP_SIGNATURE_SIZE;
3890 ph->param_length = htons(cookie_sz);
3891 *signature = (uint8_t *)mtod(sig, caddr_t);
3892 memset(*signature, 0, SCTP_SIGNATURE_SIZE);
3897 sctp_get_ect(struct sctp_tcb *stcb)
3899 if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
3900 return (SCTP_ECT0_BIT);
3906 #if defined(INET) || defined(INET6)
3908 sctp_handle_no_route(struct sctp_tcb *stcb,
3909 struct sctp_nets *net,
3912 SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3915 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3916 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3917 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3918 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3919 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
3920 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3924 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3925 net->dest_state &= ~SCTP_ADDR_PF;
3929 if (net == stcb->asoc.primary_destination) {
3930 /* need a new primary */
3931 struct sctp_nets *alt;
3933 alt = sctp_find_alternate_net(stcb, net, 0);
3935 if (stcb->asoc.alternate) {
3936 sctp_free_remote_addr(stcb->asoc.alternate);
3938 stcb->asoc.alternate = alt;
3939 atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3940 if (net->ro._s_addr) {
3941 sctp_free_ifa(net->ro._s_addr);
3942 net->ro._s_addr = NULL;
3944 net->src_addr_selected = 0;
3953 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3954 struct sctp_tcb *stcb, /* may be NULL */
3955 struct sctp_nets *net,
3956 struct sockaddr *to,
3958 uint32_t auth_offset,
3959 struct sctp_auth_chunk *auth,
3960 uint16_t auth_keyid,
3961 int nofragment_flag,
3968 union sctp_sockstore *over_addr,
3969 uint8_t mflowtype, uint32_t mflowid,
3972 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3974 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3975 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3976 * - fill in the HMAC digest of any AUTH chunk in the packet.
3977 * - calculate and fill in the SCTP checksum.
3978 * - prepend an IP address header.
3979 * - if boundall use INADDR_ANY.
3980 * - if boundspecific do source address selection.
3981 * - set fragmentation option for ipV4.
3982 * - On return from IP output, check/adjust mtu size of output
3983 * interface and smallest_mtu size as well.
3985 /* Will need ifdefs around this */
3987 struct sctphdr *sctphdr;
3990 #if defined(INET) || defined(INET6)
3993 #if defined(INET) || defined(INET6)
3995 sctp_route_t *ro = NULL;
3996 struct udphdr *udp = NULL;
4000 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
4001 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4005 #if defined(INET) || defined(INET6)
4007 vrf_id = stcb->asoc.vrf_id;
4009 vrf_id = inp->def_vrf_id;
4012 /* fill in the HMAC digest for any AUTH chunk in the packet */
4013 if ((auth != NULL) && (stcb != NULL)) {
4014 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
4018 tos_value = net->dscp;
4020 tos_value = stcb->asoc.default_dscp;
4022 tos_value = inp->sctp_ep.default_dscp;
4025 switch (to->sa_family) {
4029 struct ip *ip = NULL;
4030 sctp_route_t iproute;
4033 len = SCTP_MIN_V4_OVERHEAD;
4035 len += sizeof(struct udphdr);
4037 newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4040 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4043 SCTP_ALIGN_TO_END(newm, len);
4044 SCTP_BUF_LEN(newm) = len;
4045 SCTP_BUF_NEXT(newm) = m;
4048 m->m_pkthdr.flowid = net->flowid;
4049 M_HASHTYPE_SET(m, net->flowtype);
4051 m->m_pkthdr.flowid = mflowid;
4052 M_HASHTYPE_SET(m, mflowtype);
4054 packet_length = sctp_calculate_len(m);
4055 ip = mtod(m, struct ip *);
4056 ip->ip_v = IPVERSION;
4057 ip->ip_hl = (sizeof(struct ip) >> 2);
4058 if (tos_value == 0) {
4060 * This means especially, that it is not set
4061 * at the SCTP layer. So use the value from
4064 tos_value = inp->ip_inp.inp.inp_ip_tos;
4068 tos_value |= sctp_get_ect(stcb);
4070 if ((nofragment_flag) && (port == 0)) {
4071 ip->ip_off = htons(IP_DF);
4073 ip->ip_off = htons(0);
4075 /* FreeBSD has a function for ip_id's */
4078 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
4079 ip->ip_len = htons(packet_length);
4080 ip->ip_tos = tos_value;
4082 ip->ip_p = IPPROTO_UDP;
4084 ip->ip_p = IPPROTO_SCTP;
4089 memset(&iproute, 0, sizeof(iproute));
4090 memcpy(&ro->ro_dst, to, to->sa_len);
4092 ro = (sctp_route_t *)&net->ro;
4094 /* Now the address selection part */
4095 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4097 /* call the routine to select the src address */
4098 if (net && out_of_asoc_ok == 0) {
4099 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4100 sctp_free_ifa(net->ro._s_addr);
4101 net->ro._s_addr = NULL;
4102 net->src_addr_selected = 0;
4105 if (net->src_addr_selected == 0) {
4106 /* Cache the source address */
4107 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4110 net->src_addr_selected = 1;
4112 if (net->ro._s_addr == NULL) {
4113 /* No route to host */
4114 net->src_addr_selected = 0;
4115 sctp_handle_no_route(stcb, net, so_locked);
4116 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4118 return (EHOSTUNREACH);
4120 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4122 if (over_addr == NULL) {
4123 struct sctp_ifa *_lsrc;
4125 _lsrc = sctp_source_address_selection(inp, stcb, ro,
4129 if (_lsrc == NULL) {
4130 sctp_handle_no_route(stcb, net, so_locked);
4131 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4133 return (EHOSTUNREACH);
4135 ip->ip_src = _lsrc->address.sin.sin_addr;
4136 sctp_free_ifa(_lsrc);
4138 ip->ip_src = over_addr->sin.sin_addr;
4139 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4143 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4144 sctp_handle_no_route(stcb, net, so_locked);
4145 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4147 return (EHOSTUNREACH);
4149 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4150 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4151 udp->uh_dport = port;
4152 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
4154 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4158 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4160 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4163 sctphdr->src_port = src_port;
4164 sctphdr->dest_port = dest_port;
4165 sctphdr->v_tag = v_tag;
4166 sctphdr->checksum = 0;
4169 * If source address selection fails and we find no
4170 * route then the ip_output should fail as well with
4171 * a NO_ROUTE_TO_HOST type error. We probably should
4172 * catch that somewhere and abort the association
4173 * right away (assuming this is an INIT being sent).
4175 if (ro->ro_nh == NULL) {
4177 * src addr selection failed to find a route
4178 * (or valid source addr), so we can't get
4179 * there from here (yet)!
4181 sctp_handle_no_route(stcb, net, so_locked);
4182 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4184 return (EHOSTUNREACH);
4186 if (ro != &iproute) {
4187 memcpy(&iproute, ro, sizeof(*ro));
4189 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4190 (uint32_t)(ntohl(ip->ip_src.s_addr)));
4191 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4192 (uint32_t)(ntohl(ip->ip_dst.s_addr)));
4193 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4196 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4197 /* failed to prepend data, give up */
4198 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4202 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4204 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4205 SCTP_STAT_INCR(sctps_sendswcrc);
4207 SCTP_ENABLE_UDP_CSUM(o_pak);
4210 m->m_pkthdr.csum_flags = CSUM_SCTP;
4211 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4212 SCTP_STAT_INCR(sctps_sendhwcrc);
4214 #ifdef SCTP_PACKET_LOGGING
4215 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4216 sctp_packet_log(o_pak);
4218 /* send it out. table id is taken from stcb */
4219 SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4220 SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4222 UDPSTAT_INC(udps_opackets);
4224 SCTP_STAT_INCR(sctps_sendpackets);
4225 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4227 SCTP_STAT_INCR(sctps_senderrors);
4229 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4231 /* free tempy routes */
4234 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4235 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4238 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4241 mtu -= sizeof(struct udphdr);
4243 if (mtu < net->mtu) {
4245 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4246 sctp_pathmtu_adjustment(stcb, mtu, true);
4250 } else if (ro->ro_nh == NULL) {
4251 /* route was freed */
4252 if (net->ro._s_addr &&
4253 net->src_addr_selected) {
4254 sctp_free_ifa(net->ro._s_addr);
4255 net->ro._s_addr = NULL;
4257 net->src_addr_selected = 0;
4266 uint32_t flowlabel, flowinfo;
4267 struct ip6_hdr *ip6h;
4268 struct route_in6 ip6route;
4270 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4272 struct sockaddr_in6 lsa6_storage;
4274 u_short prev_port = 0;
4278 flowlabel = net->flowlabel;
4280 flowlabel = stcb->asoc.default_flowlabel;
4282 flowlabel = inp->sctp_ep.default_flowlabel;
4284 if (flowlabel == 0) {
4286 * This means especially, that it is not set
4287 * at the SCTP layer. So use the value from
4290 flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4292 flowlabel &= 0x000fffff;
4293 len = SCTP_MIN_OVERHEAD;
4295 len += sizeof(struct udphdr);
4297 newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4300 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4303 SCTP_ALIGN_TO_END(newm, len);
4304 SCTP_BUF_LEN(newm) = len;
4305 SCTP_BUF_NEXT(newm) = m;
4308 m->m_pkthdr.flowid = net->flowid;
4309 M_HASHTYPE_SET(m, net->flowtype);
4311 m->m_pkthdr.flowid = mflowid;
4312 M_HASHTYPE_SET(m, mflowtype);
4314 packet_length = sctp_calculate_len(m);
4316 ip6h = mtod(m, struct ip6_hdr *);
4317 /* protect *sin6 from overwrite */
4318 sin6 = (struct sockaddr_in6 *)to;
4322 /* KAME hack: embed scopeid */
4323 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4324 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4329 memset(&ip6route, 0, sizeof(ip6route));
4330 ro = (sctp_route_t *)&ip6route;
4331 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4333 ro = (sctp_route_t *)&net->ro;
4336 * We assume here that inp_flow is in host byte
4337 * order within the TCB!
4339 if (tos_value == 0) {
4341 * This means especially, that it is not set
4342 * at the SCTP layer. So use the value from
4345 tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4349 tos_value |= sctp_get_ect(stcb);
4353 flowinfo |= tos_value;
4355 flowinfo |= flowlabel;
4356 ip6h->ip6_flow = htonl(flowinfo);
4358 ip6h->ip6_nxt = IPPROTO_UDP;
4360 ip6h->ip6_nxt = IPPROTO_SCTP;
4362 ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4363 ip6h->ip6_dst = sin6->sin6_addr;
4366 * Add SRC address selection here: we can only reuse
4367 * to a limited degree the kame src-addr-sel, since
4368 * we can try their selection but it may not be
4371 memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4372 lsa6_tmp.sin6_family = AF_INET6;
4373 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4375 if (net && out_of_asoc_ok == 0) {
4376 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4377 sctp_free_ifa(net->ro._s_addr);
4378 net->ro._s_addr = NULL;
4379 net->src_addr_selected = 0;
4382 if (net->src_addr_selected == 0) {
4383 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4384 /* KAME hack: embed scopeid */
4385 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4386 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4390 /* Cache the source address */
4391 net->ro._s_addr = sctp_source_address_selection(inp,
4397 (void)sa6_recoverscope(sin6);
4398 net->src_addr_selected = 1;
4400 if (net->ro._s_addr == NULL) {
4401 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4402 net->src_addr_selected = 0;
4403 sctp_handle_no_route(stcb, net, so_locked);
4404 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4406 return (EHOSTUNREACH);
4408 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4410 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4411 /* KAME hack: embed scopeid */
4412 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4413 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4417 if (over_addr == NULL) {
4418 struct sctp_ifa *_lsrc;
4420 _lsrc = sctp_source_address_selection(inp, stcb, ro,
4424 if (_lsrc == NULL) {
4425 sctp_handle_no_route(stcb, net, so_locked);
4426 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4428 return (EHOSTUNREACH);
4430 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4431 sctp_free_ifa(_lsrc);
4433 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4434 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4436 (void)sa6_recoverscope(sin6);
4438 lsa6->sin6_port = inp->sctp_lport;
4440 if (ro->ro_nh == NULL) {
4442 * src addr selection failed to find a route
4443 * (or valid source addr), so we can't get
4446 sctp_handle_no_route(stcb, net, so_locked);
4447 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4449 return (EHOSTUNREACH);
4452 * XXX: sa6 may not have a valid sin6_scope_id in
4453 * the non-SCOPEDROUTING case.
4455 memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4456 lsa6_storage.sin6_family = AF_INET6;
4457 lsa6_storage.sin6_len = sizeof(lsa6_storage);
4458 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4459 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4460 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4465 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4466 lsa6_storage.sin6_port = inp->sctp_lport;
4467 lsa6 = &lsa6_storage;
4468 ip6h->ip6_src = lsa6->sin6_addr;
4471 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4472 sctp_handle_no_route(stcb, net, so_locked);
4473 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4475 return (EHOSTUNREACH);
4477 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4478 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4479 udp->uh_dport = port;
4480 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4482 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4484 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4487 sctphdr->src_port = src_port;
4488 sctphdr->dest_port = dest_port;
4489 sctphdr->v_tag = v_tag;
4490 sctphdr->checksum = 0;
4493 * We set the hop limit now since there is a good
4494 * chance that our ro pointer is now filled
4496 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4497 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4500 /* Copy to be sure something bad is not happening */
4501 sin6->sin6_addr = ip6h->ip6_dst;
4502 lsa6->sin6_addr = ip6h->ip6_src;
4505 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4506 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4507 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4508 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4509 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4511 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4513 * preserve the port and scope for link
4516 prev_scope = sin6->sin6_scope_id;
4517 prev_port = sin6->sin6_port;
4520 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4521 /* failed to prepend data, give up */
4523 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4526 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4528 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4529 SCTP_STAT_INCR(sctps_sendswcrc);
4530 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4531 udp->uh_sum = 0xffff;
4534 m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4535 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4536 SCTP_STAT_INCR(sctps_sendhwcrc);
4538 /* send it out. table id is taken from stcb */
4539 #ifdef SCTP_PACKET_LOGGING
4540 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4541 sctp_packet_log(o_pak);
4543 SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4544 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4546 /* for link local this must be done */
4547 sin6->sin6_scope_id = prev_scope;
4548 sin6->sin6_port = prev_port;
4550 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4552 UDPSTAT_INC(udps_opackets);
4554 SCTP_STAT_INCR(sctps_sendpackets);
4555 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4557 SCTP_STAT_INCR(sctps_senderrors);
4560 /* Now if we had a temp route free it */
4564 * PMTU check versus smallest asoc MTU goes
4567 if (ro->ro_nh == NULL) {
4568 /* Route was freed */
4569 if (net->ro._s_addr &&
4570 net->src_addr_selected) {
4571 sctp_free_ifa(net->ro._s_addr);
4572 net->ro._s_addr = NULL;
4574 net->src_addr_selected = 0;
4576 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4577 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4580 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4583 mtu -= sizeof(struct udphdr);
4585 if (mtu < net->mtu) {
4587 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4588 sctp_pathmtu_adjustment(stcb, mtu, false);
4592 } else if (ifp != NULL) {
4593 if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4594 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4595 sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4603 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4604 ((struct sockaddr *)to)->sa_family);
4606 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4612 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
4614 struct mbuf *m, *m_last;
4615 struct sctp_nets *net;
4616 struct sctp_init_chunk *init;
4617 struct sctp_supported_addr_param *sup_addr;
4618 struct sctp_adaptation_layer_indication *ali;
4619 struct sctp_supported_chunk_types_param *pr_supported;
4620 struct sctp_paramhdr *ph;
4621 int cnt_inits_to = 0;
4623 uint16_t num_ext, chunk_len, padding_len, parameter_len;
4625 /* INIT's always go to the primary (and usually ONLY address) */
4626 net = stcb->asoc.primary_destination;
4628 net = TAILQ_FIRST(&stcb->asoc.nets);
4633 /* we confirm any address we send an INIT to */
4634 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4635 (void)sctp_set_primary_addr(stcb, NULL, net);
4637 /* we confirm any address we send an INIT to */
4638 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4640 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4642 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4644 * special hook, if we are sending to link local it will not
4645 * show up in our private address count.
4647 if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
4651 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4652 /* This case should not happen */
4653 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4656 /* start the INIT timer */
4657 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4659 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
4661 /* No memory, INIT timer will re-attempt. */
4662 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4665 chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
4667 /* Now lets put the chunk header in place */
4668 init = mtod(m, struct sctp_init_chunk *);
4669 /* now the chunk header */
4670 init->ch.chunk_type = SCTP_INITIATION;
4671 init->ch.chunk_flags = 0;
4672 /* fill in later from mbuf we build */
4673 init->ch.chunk_length = 0;
4674 /* place in my tag */
4675 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4676 /* set up some of the credits. */
4677 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4678 SCTP_MINIMAL_RWND));
4679 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4680 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4681 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4683 /* Adaptation layer indication parameter */
4684 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
4685 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
4686 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
4687 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4688 ali->ph.param_length = htons(parameter_len);
4689 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
4690 chunk_len += parameter_len;
4694 if (stcb->asoc.ecn_supported == 1) {
4695 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4696 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4697 ph->param_type = htons(SCTP_ECN_CAPABLE);
4698 ph->param_length = htons(parameter_len);
4699 chunk_len += parameter_len;
4702 /* PR-SCTP supported parameter */
4703 if (stcb->asoc.prsctp_supported == 1) {
4704 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4705 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4706 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
4707 ph->param_length = htons(parameter_len);
4708 chunk_len += parameter_len;
4711 /* Add NAT friendly parameter. */
4712 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4713 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4714 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4715 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4716 ph->param_length = htons(parameter_len);
4717 chunk_len += parameter_len;
4720 /* And now tell the peer which extensions we support */
4722 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
4723 if (stcb->asoc.prsctp_supported == 1) {
4724 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4725 if (stcb->asoc.idata_supported) {
4726 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
4729 if (stcb->asoc.auth_supported == 1) {
4730 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4732 if (stcb->asoc.asconf_supported == 1) {
4733 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4734 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4736 if (stcb->asoc.reconfig_supported == 1) {
4737 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4739 if (stcb->asoc.idata_supported) {
4740 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
4742 if (stcb->asoc.nrsack_supported == 1) {
4743 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4745 if (stcb->asoc.pktdrop_supported == 1) {
4746 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4749 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
4750 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4751 pr_supported->ph.param_length = htons(parameter_len);
4752 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4753 chunk_len += parameter_len;
4755 /* add authentication parameters */
4756 if (stcb->asoc.auth_supported) {
4757 /* attach RANDOM parameter, if available */
4758 if (stcb->asoc.authinfo.random != NULL) {
4759 struct sctp_auth_random *randp;
4761 if (padding_len > 0) {
4762 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4763 chunk_len += padding_len;
4766 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
4767 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
4768 /* random key already contains the header */
4769 memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
4770 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4771 chunk_len += parameter_len;
4773 /* add HMAC_ALGO parameter */
4774 if (stcb->asoc.local_hmacs != NULL) {
4775 struct sctp_auth_hmac_algo *hmacs;
4777 if (padding_len > 0) {
4778 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4779 chunk_len += padding_len;
4782 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
4783 parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
4784 stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
4785 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4786 hmacs->ph.param_length = htons(parameter_len);
4787 sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
4788 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4789 chunk_len += parameter_len;
4791 /* add CHUNKS parameter */
4792 if (stcb->asoc.local_auth_chunks != NULL) {
4793 struct sctp_auth_chunk_list *chunks;
4795 if (padding_len > 0) {
4796 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4797 chunk_len += padding_len;
4800 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
4801 parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
4802 sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
4803 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4804 chunks->ph.param_length = htons(parameter_len);
4805 sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
4806 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4807 chunk_len += parameter_len;
4811 /* now any cookie time extensions */
4812 if (stcb->asoc.cookie_preserve_req > 0) {
4813 struct sctp_cookie_perserve_param *cookie_preserve;
4815 if (padding_len > 0) {
4816 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4817 chunk_len += padding_len;
4820 parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
4821 cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t)+chunk_len);
4822 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4823 cookie_preserve->ph.param_length = htons(parameter_len);
4824 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4825 stcb->asoc.cookie_preserve_req = 0;
4826 chunk_len += parameter_len;
4829 if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
4832 if (padding_len > 0) {
4833 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4834 chunk_len += padding_len;
4837 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4838 if (stcb->asoc.scope.ipv4_addr_legal) {
4839 parameter_len += (uint16_t)sizeof(uint16_t);
4841 if (stcb->asoc.scope.ipv6_addr_legal) {
4842 parameter_len += (uint16_t)sizeof(uint16_t);
4844 sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t)+chunk_len);
4845 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4846 sup_addr->ph.param_length = htons(parameter_len);
4848 if (stcb->asoc.scope.ipv4_addr_legal) {
4849 sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
4851 if (stcb->asoc.scope.ipv6_addr_legal) {
4852 sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
4854 padding_len = 4 - 2 * i;
4855 chunk_len += parameter_len;
4858 SCTP_BUF_LEN(m) = chunk_len;
4859 /* now the addresses */
4861 * To optimize this we could put the scoping stuff into a structure
4862 * and remove the individual uint8's from the assoc structure. Then
4863 * we could just sifa in the address within the stcb. But for now
4864 * this is a quick hack to get the address stuff teased apart.
4866 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
4868 &padding_len, &chunk_len);
4870 init->ch.chunk_length = htons(chunk_len);
4871 if (padding_len > 0) {
4872 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
4877 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4878 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
4879 (struct sockaddr *)&net->ro._l_addr,
4880 m, 0, NULL, 0, 0, 0, 0,
4881 inp->sctp_lport, stcb->rport, htonl(0),
4885 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
4886 if (error == ENOBUFS) {
4887 stcb->asoc.ifp_had_enobuf = 1;
4888 SCTP_STAT_INCR(sctps_lowlevelerr);
4891 stcb->asoc.ifp_had_enobuf = 0;
4893 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4894 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4898 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4899 int param_offset, int *abort_processing,
4900 struct sctp_chunkhdr *cp,
4905 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4906 * being equal to the beginning of the params i.e. (iphlen +
4907 * sizeof(struct sctp_init_msg) parse through the parameters to the
4908 * end of the mbuf verifying that all parameters are known.
4910 * For unknown parameters build and return a mbuf with
4911 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4912 * processing this chunk stop, and set *abort_processing to 1.
4914 * By having param_offset be pre-set to where parameters begin it is
4915 * hoped that this routine may be reused in the future by new
4918 struct sctp_paramhdr *phdr, params;
4920 struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
4921 int at, limit, pad_needed;
4922 uint16_t ptype, plen, padded_size;
4924 *abort_processing = 0;
4925 if (cookie_found != NULL) {
4929 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4934 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4935 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4936 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4937 ptype = ntohs(phdr->param_type);
4938 plen = ntohs(phdr->param_length);
4939 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4940 /* wacked parameter */
4941 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4944 limit -= SCTP_SIZE32(plen);
4946 * All parameters for all chunks that we know/understand are
4947 * listed here. We process them other places and make
4948 * appropriate stop actions per the upper bits. However this
4949 * is the generic routine processor's can call to get back
4950 * an operr.. to either incorporate (init-ack) or send.
4952 padded_size = SCTP_SIZE32(plen);
4954 /* Param's with variable size */
4955 case SCTP_HEARTBEAT_INFO:
4956 case SCTP_UNRECOG_PARAM:
4957 case SCTP_ERROR_CAUSE_IND:
4961 case SCTP_STATE_COOKIE:
4962 if (cookie_found != NULL) {
4967 /* Param's with variable size within a range */
4968 case SCTP_CHUNK_LIST:
4969 case SCTP_SUPPORTED_CHUNK_EXT:
4970 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4971 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4976 case SCTP_SUPPORTED_ADDRTYPE:
4977 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4978 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4984 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4985 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4990 case SCTP_SET_PRIM_ADDR:
4991 case SCTP_DEL_IP_ADDRESS:
4992 case SCTP_ADD_IP_ADDRESS:
4993 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4994 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4995 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
5000 /* Param's with a fixed size */
5001 case SCTP_IPV4_ADDRESS:
5002 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5003 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5008 case SCTP_IPV6_ADDRESS:
5009 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5010 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5015 case SCTP_COOKIE_PRESERVE:
5016 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5017 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5022 case SCTP_HAS_NAT_SUPPORT:
5025 case SCTP_PRSCTP_SUPPORTED:
5026 if (padded_size != sizeof(struct sctp_paramhdr)) {
5027 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
5032 case SCTP_ECN_CAPABLE:
5033 if (padded_size != sizeof(struct sctp_paramhdr)) {
5034 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5039 case SCTP_ULP_ADAPTATION:
5040 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5041 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5046 case SCTP_SUCCESS_REPORT:
5047 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5048 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5053 case SCTP_HOSTNAME_ADDRESS:
5055 /* Hostname parameters are deprecated. */
5056 struct sctp_gen_error_cause *cause;
5059 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5060 *abort_processing = 1;
5061 sctp_m_freem(op_err);
5065 l_len = SCTP_MIN_OVERHEAD;
5067 l_len = SCTP_MIN_V4_OVERHEAD;
5069 l_len += sizeof(struct sctp_chunkhdr);
5070 l_len += sizeof(struct sctp_gen_error_cause);
5071 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5072 if (op_err != NULL) {
5074 * Pre-reserve space for IP, SCTP,
5078 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5080 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5082 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5083 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5084 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5085 cause = mtod(op_err, struct sctp_gen_error_cause *);
5086 cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5087 cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5088 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5089 if (SCTP_BUF_NEXT(op_err) == NULL) {
5090 sctp_m_freem(op_err);
5099 * we do not recognize the parameter figure out what
5102 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5103 if ((ptype & 0x4000) == 0x4000) {
5104 /* Report bit is set?? */
5105 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5106 if (op_err == NULL) {
5109 /* Ok need to try to get an mbuf */
5111 l_len = SCTP_MIN_OVERHEAD;
5113 l_len = SCTP_MIN_V4_OVERHEAD;
5115 l_len += sizeof(struct sctp_chunkhdr);
5116 l_len += sizeof(struct sctp_paramhdr);
5117 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5119 SCTP_BUF_LEN(op_err) = 0;
5121 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5123 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5125 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5126 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5127 op_err_last = op_err;
5130 if (op_err != NULL) {
5131 /* If we have space */
5132 struct sctp_paramhdr *param;
5134 if (pad_needed > 0) {
5135 op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5137 if (op_err_last == NULL) {
5138 sctp_m_freem(op_err);
5141 goto more_processing;
5143 if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5144 m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5145 if (m_tmp == NULL) {
5146 sctp_m_freem(op_err);
5149 goto more_processing;
5151 SCTP_BUF_LEN(m_tmp) = 0;
5152 SCTP_BUF_NEXT(m_tmp) = NULL;
5153 SCTP_BUF_NEXT(op_err_last) = m_tmp;
5154 op_err_last = m_tmp;
5156 param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t)+SCTP_BUF_LEN(op_err_last));
5157 param->param_type = htons(SCTP_UNRECOG_PARAM);
5158 param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5159 SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5160 SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5161 if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5162 sctp_m_freem(op_err);
5165 goto more_processing;
5167 while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5168 op_err_last = SCTP_BUF_NEXT(op_err_last);
5171 if (plen % 4 != 0) {
5172 pad_needed = 4 - (plen % 4);
5179 if ((ptype & 0x8000) == 0x0000) {
5180 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5183 /* skip this chunk and continue processing */
5184 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5185 at += SCTP_SIZE32(plen);
5189 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
5193 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5194 *abort_processing = 1;
5195 sctp_m_freem(op_err);
5199 struct sctp_paramhdr *param;
5202 l_len = SCTP_MIN_OVERHEAD;
5204 l_len = SCTP_MIN_V4_OVERHEAD;
5206 l_len += sizeof(struct sctp_chunkhdr);
5207 l_len += (2 * sizeof(struct sctp_paramhdr));
5208 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5210 SCTP_BUF_LEN(op_err) = 0;
5212 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5214 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5216 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5217 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5218 SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5219 param = mtod(op_err, struct sctp_paramhdr *);
5220 param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5221 param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5223 param->param_type = htons(ptype);
5224 param->param_length = htons(plen);
5231 * Given a INIT chunk, look through the parameters to verify that there
5232 * are no new addresses.
5233 * Return true, if there is a new address or there is a problem parsing
5234 the parameters. Provide an optional error cause used when sending an ABORT.
5235 * Return false, if there are no new addresses and there is no problem in
5236 parameter processing.
5239 sctp_are_there_new_addresses(struct sctp_association *asoc,
5240 struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5241 struct mbuf **op_err)
5243 struct sockaddr *sa_touse;
5244 struct sockaddr *sa;
5245 struct sctp_paramhdr *phdr, params;
5246 struct sctp_nets *net;
5248 struct sockaddr_in sin4, *sa4;
5251 struct sockaddr_in6 sin6, *sa6;
5253 uint16_t ptype, plen;
5254 bool fnd, check_src;
5258 memset(&sin4, 0, sizeof(sin4));
5259 sin4.sin_family = AF_INET;
5260 sin4.sin_len = sizeof(sin4);
5263 memset(&sin6, 0, sizeof(sin6));
5264 sin6.sin6_family = AF_INET6;
5265 sin6.sin6_len = sizeof(sin6);
5267 /* First what about the src address of the pkt ? */
5269 switch (src->sa_family) {
5272 if (asoc->scope.ipv4_addr_legal) {
5279 if (asoc->scope.ipv6_addr_legal) {
5290 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5291 sa = (struct sockaddr *)&net->ro._l_addr;
5292 if (sa->sa_family == src->sa_family) {
5294 if (sa->sa_family == AF_INET) {
5295 struct sockaddr_in *src4;
5297 sa4 = (struct sockaddr_in *)sa;
5298 src4 = (struct sockaddr_in *)src;
5299 if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5306 if (sa->sa_family == AF_INET6) {
5307 struct sockaddr_in6 *src6;
5309 sa6 = (struct sockaddr_in6 *)sa;
5310 src6 = (struct sockaddr_in6 *)src;
5311 if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5321 * If sending an ABORT in case of an additional
5322 * address, don't use the new address error cause.
5323 * This looks no different than if no listener was
5326 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5330 /* Ok so far lets munge through the rest of the packet */
5331 offset += sizeof(struct sctp_init_chunk);
5332 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5335 ptype = ntohs(phdr->param_type);
5336 plen = ntohs(phdr->param_length);
5337 if (offset + plen > limit) {
5338 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5341 if (plen < sizeof(struct sctp_paramhdr)) {
5342 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5347 case SCTP_IPV4_ADDRESS:
5349 struct sctp_ipv4addr_param *p4, p4_buf;
5351 if (plen != sizeof(struct sctp_ipv4addr_param)) {
5352 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5355 phdr = sctp_get_next_param(in_initpkt, offset,
5356 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5358 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5361 if (asoc->scope.ipv4_addr_legal) {
5362 p4 = (struct sctp_ipv4addr_param *)phdr;
5363 sin4.sin_addr.s_addr = p4->addr;
5364 sa_touse = (struct sockaddr *)&sin4;
5370 case SCTP_IPV6_ADDRESS:
5372 struct sctp_ipv6addr_param *p6, p6_buf;
5374 if (plen != sizeof(struct sctp_ipv6addr_param)) {
5375 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5378 phdr = sctp_get_next_param(in_initpkt, offset,
5379 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5381 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5384 if (asoc->scope.ipv6_addr_legal) {
5385 p6 = (struct sctp_ipv6addr_param *)phdr;
5386 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5388 sa_touse = (struct sockaddr *)&sin6;
5398 /* ok, sa_touse points to one to check */
5400 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5401 sa = (struct sockaddr *)&net->ro._l_addr;
5402 if (sa->sa_family != sa_touse->sa_family) {
5406 if (sa->sa_family == AF_INET) {
5407 sa4 = (struct sockaddr_in *)sa;
5408 if (sa4->sin_addr.s_addr ==
5409 sin4.sin_addr.s_addr) {
5416 if (sa->sa_family == AF_INET6) {
5417 sa6 = (struct sockaddr_in6 *)sa;
5418 if (SCTP6_ARE_ADDR_EQUAL(
5428 * If sending an ABORT in case of an
5429 * additional address, don't use the new
5430 * address error cause. This looks no
5431 * different than if no listener was
5434 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5438 offset += SCTP_SIZE32(plen);
5439 if (offset >= limit) {
5442 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5448 * Given a MBUF chain that was sent into us containing an INIT. Build a
5449 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5450 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5451 * message (i.e. the struct sctp_init_msg).
5454 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5455 struct sctp_nets *src_net, struct mbuf *init_pkt,
5456 int iphlen, int offset,
5457 struct sockaddr *src, struct sockaddr *dst,
5458 struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5459 uint8_t mflowtype, uint32_t mflowid,
5460 uint32_t vrf_id, uint16_t port)
5462 struct sctp_association *asoc;
5463 struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5464 struct sctp_init_ack_chunk *initack;
5465 struct sctp_adaptation_layer_indication *ali;
5466 struct sctp_supported_chunk_types_param *pr_supported;
5467 struct sctp_paramhdr *ph;
5468 union sctp_sockstore *over_addr;
5469 struct sctp_scoping scp;
5472 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5473 struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5474 struct sockaddr_in *sin;
5477 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
5478 struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
5479 struct sockaddr_in6 *sin6;
5481 struct sockaddr *to;
5482 struct sctp_state_cookie stc;
5483 struct sctp_nets *net = NULL;
5484 uint8_t *signature = NULL;
5485 int cnt_inits_to = 0;
5486 uint16_t his_limit, i_want;
5488 int nat_friendly = 0;
5491 uint16_t num_ext, chunk_len, padding_len, parameter_len;
5498 if ((asoc != NULL) &&
5499 (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
5500 if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
5502 * new addresses, out of here in non-cookie-wait
5505 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5506 mflowtype, mflowid, inp->fibnum,
5510 if (src_net != NULL && (src_net->port != port)) {
5512 * change of remote encapsulation port, out of here
5513 * in non-cookie-wait states
5515 * Send an ABORT, without an specific error cause.
5516 * This looks no different than if no listener was
5519 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5520 "Remote encapsulation port changed");
5521 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5522 mflowtype, mflowid, inp->fibnum,
5528 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5529 (offset + sizeof(struct sctp_init_chunk)),
5531 (struct sctp_chunkhdr *)init_chk,
5532 &nat_friendly, NULL);
5535 if (op_err == NULL) {
5536 char msg[SCTP_DIAG_INFO_LEN];
5538 SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
5539 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5542 sctp_send_abort(init_pkt, iphlen, src, dst, sh,
5543 init_chk->init.initiate_tag, op_err,
5544 mflowtype, mflowid, inp->fibnum,
5548 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
5550 /* No memory, INIT timer will re-attempt. */
5551 sctp_m_freem(op_err);
5554 chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
5558 * We might not overwrite the identification[] completely and on
5559 * some platforms time_entered will contain some padding. Therefore
5560 * zero out the cookie to avoid putting uninitialized memory on the
5563 memset(&stc, 0, sizeof(struct sctp_state_cookie));
5565 /* the time I built cookie */
5566 (void)SCTP_GETTIME_TIMEVAL(&now);
5567 stc.time_entered.tv_sec = now.tv_sec;
5568 stc.time_entered.tv_usec = now.tv_usec;
5570 /* populate any tie tags */
5572 /* unlock before tag selections */
5573 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5574 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5575 stc.cookie_life = asoc->cookie_life;
5576 net = asoc->primary_destination;
5578 stc.tie_tag_my_vtag = 0;
5579 stc.tie_tag_peer_vtag = 0;
5580 /* life I will award this cookie */
5581 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5584 /* copy in the ports for later check */
5585 stc.myport = sh->dest_port;
5586 stc.peerport = sh->src_port;
5589 * If we wanted to honor cookie life extensions, we would add to
5590 * stc.cookie_life. For now we should NOT honor any extension
5592 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5593 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5594 stc.ipv6_addr_legal = 1;
5595 if (SCTP_IPV6_V6ONLY(inp)) {
5596 stc.ipv4_addr_legal = 0;
5598 stc.ipv4_addr_legal = 1;
5601 stc.ipv6_addr_legal = 0;
5602 stc.ipv4_addr_legal = 1;
5607 switch (dst->sa_family) {
5611 /* lookup address */
5612 stc.address[0] = src4->sin_addr.s_addr;
5616 stc.addr_type = SCTP_IPV4_ADDRESS;
5617 /* local from address */
5618 stc.laddress[0] = dst4->sin_addr.s_addr;
5619 stc.laddress[1] = 0;
5620 stc.laddress[2] = 0;
5621 stc.laddress[3] = 0;
5622 stc.laddr_type = SCTP_IPV4_ADDRESS;
5623 /* scope_id is only for v6 */
5625 if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
5626 (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
5629 /* Must use the address in this case */
5630 if (sctp_is_address_on_local_host(src, vrf_id)) {
5631 stc.loopback_scope = 1;
5634 stc.local_scope = 0;
5642 stc.addr_type = SCTP_IPV6_ADDRESS;
5643 memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
5644 stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
5645 if (sctp_is_address_on_local_host(src, vrf_id)) {
5646 stc.loopback_scope = 1;
5647 stc.local_scope = 0;
5650 } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
5651 IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
5653 * If the new destination or source
5654 * is a LINK_LOCAL we must have
5655 * common both site and local scope.
5656 * Don't set local scope though
5657 * since we must depend on the
5658 * source to be added implicitly. We
5659 * cannot assure just because we
5660 * share one link that all links are
5663 stc.local_scope = 0;
5667 * we start counting for the private
5668 * address stuff at 1. since the
5669 * link local we source from won't
5670 * show up in our scoped count.
5674 * pull out the scope_id from
5677 } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
5678 IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
5680 * If the new destination or source
5681 * is SITE_LOCAL then we must have
5682 * site scope in common.
5686 memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
5687 stc.laddr_type = SCTP_IPV6_ADDRESS;
5697 /* set the scope per the existing tcb */
5700 struct sctp_nets *lnet;
5703 stc.loopback_scope = asoc->scope.loopback_scope;
5704 stc.ipv4_scope = asoc->scope.ipv4_local_scope;
5705 stc.site_scope = asoc->scope.site_scope;
5706 stc.local_scope = asoc->scope.local_scope;
5708 /* Why do we not consider IPv4 LL addresses? */
5709 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5710 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5711 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5713 * if we have a LL address, start
5721 /* use the net pointer */
5722 to = (struct sockaddr *)&net->ro._l_addr;
5723 switch (to->sa_family) {
5726 sin = (struct sockaddr_in *)to;
5727 stc.address[0] = sin->sin_addr.s_addr;
5731 stc.addr_type = SCTP_IPV4_ADDRESS;
5732 if (net->src_addr_selected == 0) {
5734 * strange case here, the INIT should have
5735 * did the selection.
5737 net->ro._s_addr = sctp_source_address_selection(inp,
5738 stcb, (sctp_route_t *)&net->ro,
5740 if (net->ro._s_addr == NULL) {
5741 sctp_m_freem(op_err);
5746 net->src_addr_selected = 1;
5748 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5749 stc.laddress[1] = 0;
5750 stc.laddress[2] = 0;
5751 stc.laddress[3] = 0;
5752 stc.laddr_type = SCTP_IPV4_ADDRESS;
5753 /* scope_id is only for v6 */
5759 sin6 = (struct sockaddr_in6 *)to;
5760 memcpy(&stc.address, &sin6->sin6_addr,
5761 sizeof(struct in6_addr));
5762 stc.addr_type = SCTP_IPV6_ADDRESS;
5763 stc.scope_id = sin6->sin6_scope_id;
5764 if (net->src_addr_selected == 0) {
5766 * strange case here, the INIT should have
5767 * done the selection.
5769 net->ro._s_addr = sctp_source_address_selection(inp,
5770 stcb, (sctp_route_t *)&net->ro,
5772 if (net->ro._s_addr == NULL) {
5773 sctp_m_freem(op_err);
5778 net->src_addr_selected = 1;
5780 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5781 sizeof(struct in6_addr));
5782 stc.laddr_type = SCTP_IPV6_ADDRESS;
5787 /* Now lets put the SCTP header in place */
5788 initack = mtod(m, struct sctp_init_ack_chunk *);
5789 /* Save it off for quick ref */
5790 stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
5792 memcpy(stc.identification, SCTP_VERSION_STRING,
5793 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5794 memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5795 /* now the chunk header */
5796 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5797 initack->ch.chunk_flags = 0;
5798 /* fill in later from mbuf we build */
5799 initack->ch.chunk_length = 0;
5800 /* place in my tag */
5801 if ((asoc != NULL) &&
5802 ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
5803 (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
5804 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
5805 /* re-use the v-tags and init-seq here */
5806 initack->init.initiate_tag = htonl(asoc->my_vtag);
5807 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5809 uint32_t vtag, itsn;
5812 atomic_add_int(&asoc->refcnt, 1);
5813 SCTP_TCB_UNLOCK(stcb);
5815 SCTP_INP_INFO_RLOCK();
5816 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5817 SCTP_INP_INFO_RUNLOCK();
5818 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5820 * Got a duplicate vtag on some guy behind a
5821 * nat make sure we don't use it.
5825 initack->init.initiate_tag = htonl(vtag);
5826 /* get a TSN to use too */
5827 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5828 initack->init.initial_tsn = htonl(itsn);
5829 SCTP_TCB_LOCK(stcb);
5830 atomic_subtract_int(&asoc->refcnt, 1);
5832 SCTP_INP_INCR_REF(inp);
5833 SCTP_INP_RUNLOCK(inp);
5834 SCTP_INP_INFO_RLOCK();
5835 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5836 SCTP_INP_INFO_RUNLOCK();
5837 initack->init.initiate_tag = htonl(vtag);
5838 /* get a TSN to use too */
5839 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5840 SCTP_INP_RLOCK(inp);
5841 SCTP_INP_DECR_REF(inp);
5844 /* save away my tag to */
5845 stc.my_vtag = initack->init.initiate_tag;
5847 /* set up some of the credits. */
5848 so = inp->sctp_socket;
5850 /* memory problem */
5851 sctp_m_freem(op_err);
5855 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5857 /* set what I want */
5858 his_limit = ntohs(init_chk->init.num_inbound_streams);
5859 /* choose what I want */
5861 if (asoc->streamoutcnt > asoc->pre_open_streams) {
5862 i_want = asoc->streamoutcnt;
5864 i_want = asoc->pre_open_streams;
5867 i_want = inp->sctp_ep.pre_open_stream_count;
5869 if (his_limit < i_want) {
5870 /* I Want more :< */
5871 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5873 /* I can have what I want :> */
5874 initack->init.num_outbound_streams = htons(i_want);
5876 /* tell him his limit. */
5877 initack->init.num_inbound_streams =
5878 htons(inp->sctp_ep.max_open_streams_intome);
5880 /* adaptation layer indication parameter */
5881 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5882 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5883 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
5884 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5885 ali->ph.param_length = htons(parameter_len);
5886 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5887 chunk_len += parameter_len;
5891 if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
5892 ((asoc == NULL) && (inp->ecn_supported == 1))) {
5893 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5894 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5895 ph->param_type = htons(SCTP_ECN_CAPABLE);
5896 ph->param_length = htons(parameter_len);
5897 chunk_len += parameter_len;
5900 /* PR-SCTP supported parameter */
5901 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5902 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5903 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5904 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5905 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5906 ph->param_length = htons(parameter_len);
5907 chunk_len += parameter_len;
5910 /* Add NAT friendly parameter */
5912 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5913 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5914 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5915 ph->param_length = htons(parameter_len);
5916 chunk_len += parameter_len;
5919 /* And now tell the peer which extensions we support */
5921 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
5922 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5923 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5924 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5925 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5926 ((asoc == NULL) && (inp->idata_supported == 1))) {
5927 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5930 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5931 ((asoc == NULL) && (inp->auth_supported == 1))) {
5932 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5934 if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
5935 ((asoc == NULL) && (inp->asconf_supported == 1))) {
5936 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5937 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5939 if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
5940 ((asoc == NULL) && (inp->reconfig_supported == 1))) {
5941 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5943 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5944 ((asoc == NULL) && (inp->idata_supported == 1))) {
5945 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5947 if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
5948 ((asoc == NULL) && (inp->nrsack_supported == 1))) {
5949 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5951 if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
5952 ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
5953 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5956 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5957 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5958 pr_supported->ph.param_length = htons(parameter_len);
5959 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5960 chunk_len += parameter_len;
5963 /* add authentication parameters */
5964 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5965 ((asoc == NULL) && (inp->auth_supported == 1))) {
5966 struct sctp_auth_random *randp;
5967 struct sctp_auth_hmac_algo *hmacs;
5968 struct sctp_auth_chunk_list *chunks;
5970 if (padding_len > 0) {
5971 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5972 chunk_len += padding_len;
5975 /* generate and add RANDOM parameter */
5976 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
5977 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
5978 SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5979 randp->ph.param_type = htons(SCTP_RANDOM);
5980 randp->ph.param_length = htons(parameter_len);
5981 SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
5982 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5983 chunk_len += parameter_len;
5985 if (padding_len > 0) {
5986 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5987 chunk_len += padding_len;
5990 /* add HMAC_ALGO parameter */
5991 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
5992 parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
5993 sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5994 (uint8_t *)hmacs->hmac_ids);
5995 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5996 hmacs->ph.param_length = htons(parameter_len);
5997 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5998 chunk_len += parameter_len;
6000 if (padding_len > 0) {
6001 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6002 chunk_len += padding_len;
6005 /* add CHUNKS parameter */
6006 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
6007 parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6008 sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6009 chunks->chunk_types);
6010 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6011 chunks->ph.param_length = htons(parameter_len);
6012 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6013 chunk_len += parameter_len;
6015 SCTP_BUF_LEN(m) = chunk_len;
6017 /* now the addresses */
6019 * To optimize this we could put the scoping stuff into a structure
6020 * and remove the individual uint8's from the stc structure. Then we
6021 * could just sifa in the address within the stc.. but for now this
6022 * is a quick hack to get the address stuff teased apart.
6024 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6025 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6026 scp.loopback_scope = stc.loopback_scope;
6027 scp.ipv4_local_scope = stc.ipv4_scope;
6028 scp.local_scope = stc.local_scope;
6029 scp.site_scope = stc.site_scope;
6030 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6032 &padding_len, &chunk_len);
6033 /* padding_len can only be positive, if no addresses have been added */
6034 if (padding_len > 0) {
6035 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6036 chunk_len += padding_len;
6037 SCTP_BUF_LEN(m) += padding_len;
6041 /* tack on the operational error if present */
6044 for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6045 parameter_len += SCTP_BUF_LEN(m_tmp);
6047 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6048 SCTP_BUF_NEXT(m_last) = op_err;
6049 while (SCTP_BUF_NEXT(m_last) != NULL) {
6050 m_last = SCTP_BUF_NEXT(m_last);
6052 chunk_len += parameter_len;
6054 if (padding_len > 0) {
6055 m_last = sctp_add_pad_tombuf(m_last, padding_len);
6056 if (m_last == NULL) {
6057 /* Houston we have a problem, no space */
6061 chunk_len += padding_len;
6064 /* Now we must build a cookie */
6065 m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6066 if (m_cookie == NULL) {
6067 /* memory problem */
6071 /* Now append the cookie to the end and update the space/size */
6072 SCTP_BUF_NEXT(m_last) = m_cookie;
6074 for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6075 parameter_len += SCTP_BUF_LEN(m_tmp);
6076 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6080 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6081 chunk_len += parameter_len;
6084 * Place in the size, but we don't include the last pad (if any) in
6087 initack->ch.chunk_length = htons(chunk_len);
6090 * Time to sign the cookie, we don't sign over the cookie signature
6091 * though thus we set trailer.
6093 (void)sctp_hmac_m(SCTP_HMAC,
6094 (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6095 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6096 (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6098 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6099 * here since the timer will drive a retranmission.
6101 if (padding_len > 0) {
6102 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6107 if (stc.loopback_scope) {
6108 over_addr = (union sctp_sockstore *)dst;
6113 if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6115 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6118 SCTP_SO_NOT_LOCKED))) {
6119 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6120 if (error == ENOBUFS) {
6122 asoc->ifp_had_enobuf = 1;
6124 SCTP_STAT_INCR(sctps_lowlevelerr);
6128 asoc->ifp_had_enobuf = 0;
6131 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6135 sctp_prune_prsctp(struct sctp_tcb *stcb,
6136 struct sctp_association *asoc,
6137 struct sctp_nonpad_sndrcvinfo *srcv,
6141 struct sctp_tmit_chunk *chk, *nchk;
6143 SCTP_TCB_LOCK_ASSERT(stcb);
6144 if ((asoc->prsctp_supported) &&
6145 (asoc->sent_queue_cnt_removeable > 0)) {
6146 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6148 * Look for chunks marked with the PR_SCTP flag AND
6149 * the buffer space flag. If the one being sent is
6150 * equal or greater priority then purge the old one
6151 * and free some space.
6153 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6155 * This one is PR-SCTP AND buffer space
6158 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6160 * Lower numbers equates to higher
6161 * priority. So if the one we are
6162 * looking at has a larger priority,
6163 * we want to drop the data and NOT
6168 * We release the book_size
6169 * if the mbuf is here
6174 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6178 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6181 freed_spc += ret_spc;
6182 if (freed_spc >= dataout) {
6185 } /* if chunk was present */
6186 } /* if of sufficient priority */
6187 } /* if chunk has enabled */
6188 } /* tailqforeach */
6190 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6191 /* Here we must move to the sent queue and mark */
6192 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6193 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6196 * We release the book_size
6197 * if the mbuf is here
6201 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6204 freed_spc += ret_spc;
6205 if (freed_spc >= dataout) {
6208 } /* end if chk->data */
6209 } /* end if right class */
6210 } /* end if chk pr-sctp */
6211 } /* tailqforeachsafe (chk) */
6212 } /* if enabled in asoc */
6216 sctp_get_frag_point(struct sctp_tcb *stcb)
6218 struct sctp_association *asoc;
6219 uint32_t frag_point, overhead;
6222 /* Consider IP header and SCTP common header. */
6223 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6224 overhead = SCTP_MIN_OVERHEAD;
6226 overhead = SCTP_MIN_V4_OVERHEAD;
6228 /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6229 if (asoc->idata_supported) {
6230 overhead += sizeof(struct sctp_idata_chunk);
6231 if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6232 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6235 overhead += sizeof(struct sctp_data_chunk);
6236 if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6237 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6240 KASSERT(overhead % 4 == 0,
6241 ("overhead (%u) not a multiple of 4", overhead));
6242 /* Consider padding. */
6243 if (asoc->smallest_mtu % 4 > 0) {
6244 overhead += (asoc->smallest_mtu % 4);
6246 KASSERT(asoc->smallest_mtu > overhead,
6247 ("Association MTU (%u) too small for overhead (%u)",
6248 asoc->smallest_mtu, overhead));
6249 frag_point = asoc->smallest_mtu - overhead;
6250 KASSERT(frag_point % 4 == 0,
6251 ("frag_point (%u) not a multiple of 4", frag_point));
6252 /* Honor MAXSEG socket option. */
6253 if ((asoc->sctp_frag_point > 0) &&
6254 (asoc->sctp_frag_point < frag_point)) {
6255 frag_point = asoc->sctp_frag_point;
6257 return (frag_point);
6261 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6264 * We assume that the user wants PR_SCTP_TTL if the user provides a
6265 * positive lifetime but does not specify any PR_SCTP policy.
6267 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6268 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6269 } else if (sp->timetolive > 0) {
6270 sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6271 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6275 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6276 case CHUNK_FLAGS_PR_SCTP_BUF:
6278 * Time to live is a priority stored in tv_sec when doing
6279 * the buffer drop thing.
6281 sp->ts.tv_sec = sp->timetolive;
6284 case CHUNK_FLAGS_PR_SCTP_TTL:
6288 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6289 tv.tv_sec = sp->timetolive / 1000;
6290 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6292 * TODO sctp_constants.h needs alternative time
6293 * macros when _KERNEL is undefined.
6295 timevaladd(&sp->ts, &tv);
6298 case CHUNK_FLAGS_PR_SCTP_RTX:
6300 * Time to live is a the number or retransmissions stored in
6303 sp->ts.tv_sec = sp->timetolive;
6307 SCTPDBG(SCTP_DEBUG_USRREQ1,
6308 "Unknown PR_SCTP policy %u.\n",
6309 PR_SCTP_POLICY(sp->sinfo_flags));
6315 sctp_msg_append(struct sctp_tcb *stcb,
6316 struct sctp_nets *net,
6318 struct sctp_nonpad_sndrcvinfo *srcv)
6322 struct sctp_stream_queue_pending *sp = NULL;
6323 struct sctp_stream_out *strm;
6325 SCTP_TCB_LOCK_ASSERT(stcb);
6328 * Given an mbuf chain, put it into the association send queue and
6329 * place it on the wheel
6331 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6332 /* Invalid stream number */
6333 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6337 if ((stcb->asoc.stream_locked) &&
6338 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6339 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6343 if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6344 (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6346 * Can't queue any data while stream reset is underway.
6348 if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
6355 /* Now can we send this? */
6356 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
6357 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6358 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6359 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6360 /* got data while shutting down */
6361 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
6365 sctp_alloc_a_strmoq(stcb, sp);
6367 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6371 sp->sinfo_flags = srcv->sinfo_flags;
6372 sp->timetolive = srcv->sinfo_timetolive;
6373 sp->ppid = srcv->sinfo_ppid;
6374 sp->context = srcv->sinfo_context;
6376 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6378 atomic_add_int(&sp->net->ref_count, 1);
6382 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6383 sp->sid = srcv->sinfo_stream;
6384 sp->msg_is_complete = 1;
6385 sp->sender_all_done = 1;
6388 sp->tail_mbuf = NULL;
6389 sctp_set_prsctp_policy(sp);
6391 * We could in theory (for sendall) sifa the length in, but we would
6392 * still have to hunt through the chain since we need to setup the
6396 for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6397 if (SCTP_BUF_NEXT(at) == NULL)
6399 sp->length += SCTP_BUF_LEN(at);
6401 if (srcv->sinfo_keynumber_valid) {
6402 sp->auth_keyid = srcv->sinfo_keynumber;
6404 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6406 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6407 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6408 sp->holds_key_ref = 1;
6410 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6411 sctp_snd_sb_alloc(stcb, sp->length);
6412 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6413 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6414 stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
6423 static struct mbuf *
6424 sctp_copy_mbufchain(struct mbuf *clonechain,
6425 struct mbuf *outchain,
6426 struct mbuf **endofchain,
6429 uint8_t copy_by_ref)
6432 struct mbuf *appendchain;
6436 if (endofchain == NULL) {
6440 sctp_m_freem(outchain);
6443 if (can_take_mbuf) {
6444 appendchain = clonechain;
6447 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
6448 /* Its not in a cluster */
6449 if (*endofchain == NULL) {
6450 /* lets get a mbuf cluster */
6451 if (outchain == NULL) {
6452 /* This is the general case */
6454 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6455 if (outchain == NULL) {
6458 SCTP_BUF_LEN(outchain) = 0;
6459 *endofchain = outchain;
6460 /* get the prepend space */
6461 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6464 * We really should not get a NULL
6470 if (SCTP_BUF_NEXT(m) == NULL) {
6474 m = SCTP_BUF_NEXT(m);
6477 if (*endofchain == NULL) {
6479 * huh, TSNH XXX maybe we
6482 sctp_m_freem(outchain);
6486 /* get the new end of length */
6487 len = (int)M_TRAILINGSPACE(*endofchain);
6489 /* how much is left at the end? */
6490 len = (int)M_TRAILINGSPACE(*endofchain);
6492 /* Find the end of the data, for appending */
6493 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6495 /* Now lets copy it out */
6496 if (len >= sizeofcpy) {
6497 /* It all fits, copy it in */
6498 m_copydata(clonechain, 0, sizeofcpy, cp);
6499 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6501 /* fill up the end of the chain */
6503 m_copydata(clonechain, 0, len, cp);
6504 SCTP_BUF_LEN((*endofchain)) += len;
6505 /* now we need another one */
6508 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6513 SCTP_BUF_NEXT((*endofchain)) = m;
6515 cp = mtod((*endofchain), caddr_t);
6516 m_copydata(clonechain, len, sizeofcpy, cp);
6517 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6521 /* copy the old fashion way */
6522 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
6523 #ifdef SCTP_MBUF_LOGGING
6524 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6525 sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
6530 if (appendchain == NULL) {
6533 sctp_m_freem(outchain);
6537 /* tack on to the end */
6538 if (*endofchain != NULL) {
6539 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6543 if (SCTP_BUF_NEXT(m) == NULL) {
6544 SCTP_BUF_NEXT(m) = appendchain;
6547 m = SCTP_BUF_NEXT(m);
6551 * save off the end and update the end-chain position
6555 if (SCTP_BUF_NEXT(m) == NULL) {
6559 m = SCTP_BUF_NEXT(m);
6563 /* save off the end and update the end-chain position */
6566 if (SCTP_BUF_NEXT(m) == NULL) {
6570 m = SCTP_BUF_NEXT(m);
6572 return (appendchain);
6577 sctp_med_chunk_output(struct sctp_inpcb *inp,
6578 struct sctp_tcb *stcb,
6579 struct sctp_association *asoc,
6582 int control_only, int from_where,
6583 struct timeval *now, int *now_filled,
6584 uint32_t frag_point, int so_locked);
6587 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6588 uint32_t val SCTP_UNUSED)
6590 struct sctp_copy_all *ca;
6593 int added_control = 0;
6594 int un_sent, do_chunk_output = 1;
6595 struct sctp_association *asoc;
6596 struct sctp_nets *net;
6598 ca = (struct sctp_copy_all *)ptr;
6599 if (ca->m == NULL) {
6602 if (ca->inp != inp) {
6606 if (ca->sndlen > 0) {
6607 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
6609 /* can't copy so we are done */
6613 #ifdef SCTP_MBUF_LOGGING
6614 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6615 sctp_log_mbc(m, SCTP_MBUF_ICOPY);
6621 SCTP_TCB_LOCK_ASSERT(stcb);
6622 if (stcb->asoc.alternate) {
6623 net = stcb->asoc.alternate;
6625 net = stcb->asoc.primary_destination;
6627 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6628 /* Abort this assoc with m as the user defined reason */
6630 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
6632 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
6633 0, M_NOWAIT, 1, MT_DATA);
6634 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
6637 struct sctp_paramhdr *ph;
6639 ph = mtod(m, struct sctp_paramhdr *);
6640 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6641 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
6644 * We add one here to keep the assoc from dis-appearing on
6647 atomic_add_int(&stcb->asoc.refcnt, 1);
6648 sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
6650 * sctp_abort_an_association calls sctp_free_asoc() free
6651 * association will NOT free it since we incremented the
6652 * refcnt .. we do this to prevent it being freed and things
6653 * getting tricky since we could end up (from free_asoc)
6654 * calling inpcb_free which would get a recursive lock call
6655 * to the iterator lock.. But as a consequence of that the
6656 * stcb will return to us un-locked.. since free_asoc
6657 * returns with either no TCB or the TCB unlocked, we must
6658 * relock.. to unlock in the iterator timer :-0
6660 SCTP_TCB_LOCK(stcb);
6661 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6662 goto no_chunk_output;
6665 ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
6668 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6669 /* shutdown this assoc */
6670 if (TAILQ_EMPTY(&asoc->send_queue) &&
6671 TAILQ_EMPTY(&asoc->sent_queue) &&
6672 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
6673 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6677 * there is nothing queued to send, so I'm
6680 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6681 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6682 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6684 * only send SHUTDOWN the first time
6687 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
6688 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6690 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
6691 sctp_stop_timers_for_shutdown(stcb);
6692 sctp_send_shutdown(stcb, net);
6693 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6695 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6698 do_chunk_output = 0;
6702 * we still got (or just got) data to send,
6703 * so set SHUTDOWN_PENDING
6706 * XXX sockets draft says that SCTP_EOF
6707 * should be sent with no data. currently,
6708 * we will allow user data to be sent first
6709 * and move to SHUTDOWN-PENDING
6711 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6712 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6713 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6714 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6715 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
6717 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
6718 if (TAILQ_EMPTY(&asoc->send_queue) &&
6719 TAILQ_EMPTY(&asoc->sent_queue) &&
6720 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6721 struct mbuf *op_err;
6722 char msg[SCTP_DIAG_INFO_LEN];
6725 SCTP_SNPRINTF(msg, sizeof(msg),
6726 "%s:%d at %s", __FILE__, __LINE__, __func__);
6727 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6729 atomic_add_int(&stcb->asoc.refcnt, 1);
6730 sctp_abort_an_association(stcb->sctp_ep, stcb,
6731 op_err, false, SCTP_SO_NOT_LOCKED);
6732 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6733 goto no_chunk_output;
6739 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6740 (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
6742 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6743 (stcb->asoc.total_flight > 0) &&
6744 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
6745 do_chunk_output = 0;
6747 if (do_chunk_output)
6748 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6749 else if (added_control) {
6751 int num_out, reason, now_filled = 0;
6753 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6754 &reason, 1, 1, &now, &now_filled,
6755 sctp_get_frag_point(stcb),
6756 SCTP_SO_NOT_LOCKED);
6767 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6769 struct sctp_copy_all *ca;
6771 ca = (struct sctp_copy_all *)ptr;
6773 * Do a notify here? Kacheong suggests that the notify be done at
6774 * the send time.. so you would push up a notification if any send
6775 * failed. Don't know if this is feasible since the only failures we
6776 * have is "memory" related and if you cannot get an mbuf to send
6777 * the data you surely can't get an mbuf to send up to notify the
6778 * user you can't send the data :->
6781 /* now free everything */
6783 /* Lets clear the flag to allow others to run. */
6784 SCTP_INP_WLOCK(ca->inp);
6785 ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6786 SCTP_INP_WUNLOCK(ca->inp);
6788 sctp_m_freem(ca->m);
6789 SCTP_FREE(ca, SCTP_M_COPYAL);
6792 static struct mbuf *
6793 sctp_copy_out_all(struct uio *uio, ssize_t len)
6795 struct mbuf *ret, *at;
6796 ssize_t left, willcpy, cancpy, error;
6798 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
6804 SCTP_BUF_LEN(ret) = 0;
6805 /* save space for the data chunk header */
6806 cancpy = (int)M_TRAILINGSPACE(ret);
6807 willcpy = min(cancpy, left);
6810 /* Align data to the end */
6811 error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
6817 SCTP_BUF_LEN(at) = (int)willcpy;
6818 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6821 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
6822 if (SCTP_BUF_NEXT(at) == NULL) {
6825 at = SCTP_BUF_NEXT(at);
6826 SCTP_BUF_LEN(at) = 0;
6827 cancpy = (int)M_TRAILINGSPACE(at);
6828 willcpy = min(cancpy, left);
6835 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6836 struct sctp_nonpad_sndrcvinfo *srcv)
6839 struct sctp_copy_all *ca;
6841 if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
6842 /* You must not be larger than the limit! */
6845 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6849 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6852 memset(ca, 0, sizeof(struct sctp_copy_all));
6856 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6860 SCTP_INP_WLOCK(inp);
6861 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
6862 SCTP_INP_WUNLOCK(inp);
6864 SCTP_FREE(ca, SCTP_M_COPYAL);
6867 inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6868 SCTP_INP_WUNLOCK(inp);
6871 * take off the sendall flag, it would be bad if we failed to do
6874 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6875 /* get length and mbuf chain */
6877 ca->sndlen = uio->uio_resid;
6878 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6879 if (ca->m == NULL) {
6880 SCTP_FREE(ca, SCTP_M_COPYAL);
6882 SCTP_INP_WLOCK(inp);
6883 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6884 SCTP_INP_WUNLOCK(inp);
6885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6889 /* Gather the length of the send */
6893 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6894 ca->sndlen += SCTP_BUF_LEN(mat);
6897 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6898 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6899 SCTP_ASOC_ANY_STATE,
6901 sctp_sendall_completes, inp, 1);
6903 SCTP_INP_WLOCK(inp);
6904 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6905 SCTP_INP_WUNLOCK(inp);
6906 SCTP_FREE(ca, SCTP_M_COPYAL);
6907 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6914 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6916 struct sctp_tmit_chunk *chk, *nchk;
6918 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6919 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6920 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6921 asoc->ctrl_queue_cnt--;
6923 sctp_m_freem(chk->data);
6926 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6932 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6934 struct sctp_association *asoc;
6935 struct sctp_tmit_chunk *chk, *nchk;
6936 struct sctp_asconf_chunk *acp;
6939 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6940 /* find SCTP_ASCONF chunk in queue */
6941 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6943 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6944 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6949 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6950 asoc->ctrl_queue_cnt--;
6952 sctp_m_freem(chk->data);
6955 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6961 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6962 struct sctp_association *asoc,
6963 struct sctp_tmit_chunk **data_list,
6965 struct sctp_nets *net)
6968 struct sctp_tmit_chunk *tp1;
6970 for (i = 0; i < bundle_at; i++) {
6971 /* off of the send queue */
6972 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6973 asoc->send_queue_cnt--;
6976 * Any chunk NOT 0 you zap the time chunk 0 gets
6977 * zapped or set based on if a RTO measurement is
6980 data_list[i]->do_rtt = 0;
6983 data_list[i]->sent_rcv_time = net->last_sent_time;
6984 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6985 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
6986 if (data_list[i]->whoTo == NULL) {
6987 data_list[i]->whoTo = net;
6988 atomic_add_int(&net->ref_count, 1);
6990 /* on to the sent queue */
6991 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6992 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
6993 struct sctp_tmit_chunk *tpp;
6995 /* need to move back */
6997 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6999 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7003 if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7006 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7008 TAILQ_INSERT_TAIL(&asoc->sent_queue,
7013 /* This does not lower until the cum-ack passes it */
7014 asoc->sent_queue_cnt++;
7015 if ((asoc->peers_rwnd <= 0) &&
7016 (asoc->total_flight == 0) &&
7018 /* Mark the chunk as being a window probe */
7019 SCTP_STAT_INCR(sctps_windowprobed);
7021 #ifdef SCTP_AUDITING_ENABLED
7022 sctp_audit_log(0xC2, 3);
7024 data_list[i]->sent = SCTP_DATAGRAM_SENT;
7025 data_list[i]->snd_count = 1;
7026 data_list[i]->rec.data.chunk_was_revoked = 0;
7027 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7028 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7029 data_list[i]->whoTo->flight_size,
7030 data_list[i]->book_size,
7031 (uint32_t)(uintptr_t)data_list[i]->whoTo,
7032 data_list[i]->rec.data.tsn);
7034 sctp_flight_size_increase(data_list[i]);
7035 sctp_total_flight_increase(stcb, data_list[i]);
7036 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7037 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7038 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7040 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7041 (uint32_t)(data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7042 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7043 /* SWS sender side engages */
7044 asoc->peers_rwnd = 0;
7047 if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7048 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
7053 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7055 struct sctp_tmit_chunk *chk, *nchk;
7057 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7058 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7059 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7060 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7061 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7062 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7063 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7064 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7065 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7066 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7067 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7068 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7069 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7070 /* Stray chunks must be cleaned up */
7072 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7073 asoc->ctrl_queue_cnt--;
7075 sctp_m_freem(chk->data);
7078 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7079 asoc->fwd_tsn_cnt--;
7081 sctp_free_a_chunk(stcb, chk, so_locked);
7082 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7083 /* special handling, we must look into the param */
7084 if (chk != asoc->str_reset) {
7085 goto clean_up_anyway;
7092 sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7093 uint32_t space_left, uint32_t frag_point, int eeor_on)
7096 * Make a decision on if I should split a msg into multiple parts.
7097 * This is only asked of incomplete messages.
7101 * If we are doing EEOR we need to always send it if its the
7102 * entire thing, since it might be all the guy is putting in
7105 if (space_left >= length) {
7107 * If we have data outstanding,
7108 * we get another chance when the sack
7109 * arrives to transmit - wait for more data
7111 if (stcb->asoc.total_flight == 0) {
7113 * If nothing is in flight, we zero the
7121 /* You can fill the rest */
7122 return (space_left);
7126 * For those strange folk that make the send buffer
7127 * smaller than our fragmentation point, we can't
7128 * get a full msg in so we have to allow splitting.
7130 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7133 if ((length <= space_left) ||
7134 ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7135 /* Sub-optimal residual don't split in non-eeor mode. */
7139 * If we reach here length is larger than the space_left. Do we wish
7140 * to split it for the sake of packet putting together?
7142 if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7143 /* Its ok to split it */
7144 return (min(space_left, frag_point));
7146 /* Nope, can't split */
7151 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7152 struct sctp_nets *net,
7153 struct sctp_stream_out *strq,
7154 uint32_t space_left,
7155 uint32_t frag_point,
7161 /* Move from the stream to the send_queue keeping track of the total */
7162 struct sctp_association *asoc;
7163 struct sctp_stream_queue_pending *sp;
7164 struct sctp_tmit_chunk *chk;
7165 struct sctp_data_chunk *dchkh = NULL;
7166 struct sctp_idata_chunk *ndchkh = NULL;
7167 uint32_t to_move, length;
7169 uint8_t rcv_flags = 0;
7172 SCTP_TCB_LOCK_ASSERT(stcb);
7175 /* sa_ignore FREED_MEMORY */
7176 sp = TAILQ_FIRST(&strq->outqueue);
7178 sp = TAILQ_FIRST(&strq->outqueue);
7182 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7183 (stcb->asoc.idata_supported == 0) &&
7184 (strq->last_msg_incomplete)) {
7185 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7187 strq->last_msg_incomplete);
7188 strq->last_msg_incomplete = 0;
7193 if ((sp->msg_is_complete) && (sp->length == 0)) {
7194 if (sp->sender_all_done) {
7196 * We are doing deferred cleanup. Last time through
7197 * when we took all the data the sender_all_done was
7200 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7201 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7202 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7203 sp->sender_all_done,
7205 sp->msg_is_complete,
7208 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7209 TAILQ_REMOVE(&strq->outqueue, sp, next);
7210 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7211 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7212 (strq->chunks_on_queues == 0) &&
7213 TAILQ_EMPTY(&strq->outqueue)) {
7214 stcb->asoc.trigger_reset = 1;
7217 sctp_free_remote_addr(sp->net);
7221 sctp_m_freem(sp->data);
7224 sctp_free_a_strmoq(stcb, sp, so_locked);
7225 /* back to get the next msg */
7229 * sender just finished this but still holds a
7237 /* is there some to get */
7238 if (sp->length == 0) {
7243 } else if (sp->discard_rest) {
7244 /* Whack down the size */
7245 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7246 if ((stcb->sctp_socket != NULL) &&
7247 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7248 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7249 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7252 sctp_m_freem(sp->data);
7254 sp->tail_mbuf = NULL;
7263 some_taken = sp->some_taken;
7264 length = sp->length;
7265 if (sp->msg_is_complete) {
7266 /* The message is complete */
7267 to_move = min(length, frag_point);
7268 if (to_move == length) {
7269 /* All of it fits in the MTU */
7270 if (sp->some_taken) {
7271 rcv_flags |= SCTP_DATA_LAST_FRAG;
7273 rcv_flags |= SCTP_DATA_NOT_FRAG;
7275 sp->put_last_out = 1;
7276 if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7277 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7280 /* Not all of it fits, we fragment */
7281 if (sp->some_taken == 0) {
7282 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7287 to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7289 if (to_move >= length) {
7292 if (sp->some_taken == 0) {
7293 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7297 /* Nothing to take. */
7304 /* If we reach here, we can copy out a chunk */
7305 sctp_alloc_a_chunk(stcb, chk);
7307 /* No chunk memory */
7313 * Setup for unordered if needed by looking at the user sent info
7316 if (sp->sinfo_flags & SCTP_UNORDERED) {
7317 rcv_flags |= SCTP_DATA_UNORDERED;
7319 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7320 (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7321 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7323 /* clear out the chunk before setting up */
7324 memset(chk, 0, sizeof(*chk));
7325 chk->rec.data.rcv_flags = rcv_flags;
7327 if (to_move >= length) {
7328 /* we think we can steal the whole thing */
7329 if (to_move < sp->length) {
7330 /* bail, it changed */
7333 chk->data = sp->data;
7334 chk->last_mbuf = sp->tail_mbuf;
7335 /* register the stealing */
7336 sp->data = sp->tail_mbuf = NULL;
7341 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7342 chk->last_mbuf = NULL;
7343 if (chk->data == NULL) {
7344 sp->some_taken = some_taken;
7345 sctp_free_a_chunk(stcb, chk, so_locked);
7350 #ifdef SCTP_MBUF_LOGGING
7351 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7352 sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
7355 /* Pull off the data */
7356 m_adj(sp->data, to_move);
7357 /* Now lets work our way down and compact it */
7359 while (m && (SCTP_BUF_LEN(m) == 0)) {
7360 sp->data = SCTP_BUF_NEXT(m);
7361 SCTP_BUF_NEXT(m) = NULL;
7362 if (sp->tail_mbuf == m) {
7364 * Freeing tail? TSNH since
7365 * we supposedly were taking less
7366 * than the sp->length.
7369 panic("Huh, freeing tail? - TSNH");
7371 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7372 sp->tail_mbuf = sp->data = NULL;
7380 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7381 chk->copy_by_ref = 1;
7383 chk->copy_by_ref = 0;
7386 * get last_mbuf and counts of mb usage This is ugly but hopefully
7387 * its only one mbuf.
7389 if (chk->last_mbuf == NULL) {
7390 chk->last_mbuf = chk->data;
7391 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7392 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7396 if (to_move > length) {
7397 /*- This should not happen either
7398 * since we always lower to_move to the size
7399 * of sp->length if its larger.
7402 panic("Huh, how can to_move be larger?");
7404 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7408 atomic_subtract_int(&sp->length, to_move);
7410 leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
7411 if (M_LEADINGSPACE(chk->data) < leading) {
7412 /* Not enough room for a chunk header, get some */
7415 m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
7418 * we're in trouble here. _PREPEND below will free
7419 * all the data if there is no leading space, so we
7420 * must put the data back and restore.
7422 if (sp->data == NULL) {
7423 /* unsteal the data */
7424 sp->data = chk->data;
7425 sp->tail_mbuf = chk->last_mbuf;
7429 /* reassemble the data */
7431 sp->data = chk->data;
7432 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7434 sp->some_taken = some_taken;
7435 atomic_add_int(&sp->length, to_move);
7438 sctp_free_a_chunk(stcb, chk, so_locked);
7442 SCTP_BUF_LEN(m) = 0;
7443 SCTP_BUF_NEXT(m) = chk->data;
7445 M_ALIGN(chk->data, 4);
7448 SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
7449 if (chk->data == NULL) {
7450 /* HELP, TSNH since we assured it would not above? */
7452 panic("prepend fails HELP?");
7454 SCTP_PRINTF("prepend fails HELP?\n");
7455 sctp_free_a_chunk(stcb, chk, so_locked);
7461 sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
7462 chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
7463 chk->book_size_scale = 0;
7464 chk->sent = SCTP_DATAGRAM_UNSENT;
7467 chk->asoc = &stcb->asoc;
7468 chk->pad_inplace = 0;
7469 chk->no_fr_allowed = 0;
7470 if (stcb->asoc.idata_supported == 0) {
7471 if (rcv_flags & SCTP_DATA_UNORDERED) {
7472 /* Just use 0. The receiver ignores the values. */
7473 chk->rec.data.mid = 0;
7475 chk->rec.data.mid = strq->next_mid_ordered;
7476 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7477 strq->next_mid_ordered++;
7481 if (rcv_flags & SCTP_DATA_UNORDERED) {
7482 chk->rec.data.mid = strq->next_mid_unordered;
7483 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7484 strq->next_mid_unordered++;
7487 chk->rec.data.mid = strq->next_mid_ordered;
7488 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7489 strq->next_mid_ordered++;
7493 chk->rec.data.sid = sp->sid;
7494 chk->rec.data.ppid = sp->ppid;
7495 chk->rec.data.context = sp->context;
7496 chk->rec.data.doing_fast_retransmit = 0;
7498 chk->rec.data.timetodrop = sp->ts;
7499 chk->flags = sp->act_flags;
7502 chk->whoTo = sp->net;
7503 atomic_add_int(&chk->whoTo->ref_count, 1);
7507 if (sp->holds_key_ref) {
7508 chk->auth_keyid = sp->auth_keyid;
7509 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7510 chk->holds_key_ref = 1;
7512 stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
7513 chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
7514 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7515 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7516 (uint32_t)(uintptr_t)stcb, sp->length,
7517 (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
7520 if (stcb->asoc.idata_supported == 0) {
7521 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7523 ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
7526 * Put the rest of the things in place now. Size was done earlier in
7527 * previous loop prior to padding.
7530 SCTP_TCB_LOCK_ASSERT(stcb);
7531 #ifdef SCTP_ASOCLOG_OF_TSNS
7532 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7533 asoc->tsn_out_at = 0;
7534 asoc->tsn_out_wrapped = 1;
7536 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
7537 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
7538 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
7539 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7540 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7541 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7542 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7543 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7546 if (stcb->asoc.idata_supported == 0) {
7547 dchkh->ch.chunk_type = SCTP_DATA;
7548 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7549 dchkh->dp.tsn = htonl(chk->rec.data.tsn);
7550 dchkh->dp.sid = htons(strq->sid);
7551 dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
7552 dchkh->dp.ppid = chk->rec.data.ppid;
7553 dchkh->ch.chunk_length = htons(chk->send_size);
7555 ndchkh->ch.chunk_type = SCTP_IDATA;
7556 ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7557 ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
7558 ndchkh->dp.sid = htons(strq->sid);
7559 ndchkh->dp.reserved = htons(0);
7560 ndchkh->dp.mid = htonl(chk->rec.data.mid);
7562 ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
7564 ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
7566 ndchkh->ch.chunk_length = htons(chk->send_size);
7568 /* Now advance the chk->send_size by the actual pad needed. */
7569 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7574 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7575 lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
7577 chk->last_mbuf = lm;
7578 chk->pad_inplace = 1;
7580 chk->send_size += pads;
7582 if (PR_SCTP_ENABLED(chk->flags)) {
7583 asoc->pr_sctp_cnt++;
7585 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7586 /* All done pull and kill the message */
7587 if (sp->put_last_out == 0) {
7588 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7589 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7590 sp->sender_all_done,
7592 sp->msg_is_complete,
7595 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7596 TAILQ_REMOVE(&strq->outqueue, sp, next);
7597 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7598 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7599 (strq->chunks_on_queues == 0) &&
7600 TAILQ_EMPTY(&strq->outqueue)) {
7601 stcb->asoc.trigger_reset = 1;
7604 sctp_free_remote_addr(sp->net);
7608 sctp_m_freem(sp->data);
7611 sctp_free_a_strmoq(stcb, sp, so_locked);
7613 asoc->chunks_on_out_queue++;
7614 strq->chunks_on_queues++;
7615 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7616 asoc->send_queue_cnt++;
7622 sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
7623 uint32_t frag_point, int eeor_mode, int *quit_now,
7626 struct sctp_association *asoc;
7627 struct sctp_stream_out *strq;
7628 uint32_t space_left, moved, total_moved;
7631 SCTP_TCB_LOCK_ASSERT(stcb);
7634 switch (net->ro._l_addr.sa.sa_family) {
7637 space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
7642 space_left = net->mtu - SCTP_MIN_OVERHEAD;
7647 space_left = net->mtu;
7650 /* Need an allowance for the data chunk header too */
7651 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7653 /* must make even word boundary */
7654 space_left &= 0xfffffffc;
7655 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7658 while ((space_left > 0) && (strq != NULL)) {
7659 moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
7660 frag_point, &giveup, eeor_mode,
7662 if ((giveup != 0) || (bail != 0)) {
7665 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7666 total_moved += moved;
7667 if (space_left >= moved) {
7668 space_left -= moved;
7672 if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
7673 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7677 space_left &= 0xfffffffc;
7682 stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7684 if (total_moved == 0) {
7685 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7686 (net == stcb->asoc.primary_destination)) {
7687 /* ran dry for primary network net */
7688 SCTP_STAT_INCR(sctps_primary_randry);
7689 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7690 /* ran dry with CMT on */
7691 SCTP_STAT_INCR(sctps_cmt_randry);
7697 sctp_fix_ecn_echo(struct sctp_association *asoc)
7699 struct sctp_tmit_chunk *chk;
7701 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7702 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7703 chk->sent = SCTP_DATAGRAM_UNSENT;
7709 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7711 struct sctp_association *asoc;
7712 struct sctp_tmit_chunk *chk;
7713 struct sctp_stream_queue_pending *sp;
7720 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7721 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7722 if (sp->net == net) {
7723 sctp_free_remote_addr(sp->net);
7728 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7729 if (chk->whoTo == net) {
7730 sctp_free_remote_addr(chk->whoTo);
7737 sctp_med_chunk_output(struct sctp_inpcb *inp,
7738 struct sctp_tcb *stcb,
7739 struct sctp_association *asoc,
7742 int control_only, int from_where,
7743 struct timeval *now, int *now_filled,
7744 uint32_t frag_point, int so_locked)
7747 * Ok this is the generic chunk service queue. we must do the
7749 * - Service the stream queue that is next, moving any
7750 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7751 * LAST to the out queue in one pass) and assigning TSN's. This
7752 * only applies though if the peer does not support NDATA. For NDATA
7753 * chunks its ok to not send the entire message ;-)
7754 * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
7755 * formulate and send the low level chunks. Making sure to combine
7756 * any control in the control chunk queue also.
7758 struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7759 struct mbuf *outchain, *endoutchain;
7760 struct sctp_tmit_chunk *chk, *nchk;
7762 /* temp arrays for unlinking */
7763 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7764 int no_fragmentflg, error;
7765 unsigned int max_rwnd_per_dest, max_send_per_dest;
7766 int one_chunk, hbflag, skip_data_for_this_net;
7767 int asconf, cookie, no_out_cnt;
7768 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7769 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7771 uint32_t auth_offset;
7772 struct sctp_auth_chunk *auth;
7773 uint16_t auth_keyid;
7774 int override_ok = 1;
7775 int skip_fill_up = 0;
7776 int data_auth_reqd = 0;
7779 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7786 auth_keyid = stcb->asoc.authinfo.active_keyid;
7787 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7788 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7789 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7794 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7796 * First lets prime the pump. For each destination, if there is room
7797 * in the flight size, attempt to pull an MTU's worth out of the
7798 * stream queues into the general send_queue
7800 #ifdef SCTP_AUDITING_ENABLED
7801 sctp_audit_log(0xC2, 2);
7803 SCTP_TCB_LOCK_ASSERT(stcb);
7810 /* Nothing to possible to send? */
7811 if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7812 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7813 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7814 TAILQ_EMPTY(&asoc->send_queue) &&
7815 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
7820 if (asoc->peers_rwnd == 0) {
7821 /* No room in peers rwnd */
7823 if (asoc->total_flight > 0) {
7824 /* we are allowed one chunk in flight */
7828 if (stcb->asoc.ecn_echo_cnt_onq) {
7829 /* Record where a sack goes, if any */
7830 if (no_data_chunks &&
7831 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7832 /* Nothing but ECNe to send - we don't do that */
7833 goto nothing_to_send;
7835 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7836 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7837 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7838 sack_goes_to = chk->whoTo;
7843 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7844 if (stcb->sctp_socket)
7845 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7847 max_send_per_dest = 0;
7848 if (no_data_chunks == 0) {
7849 /* How many non-directed chunks are there? */
7850 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7851 if (chk->whoTo == NULL) {
7853 * We already have non-directed chunks on
7854 * the queue, no need to do a fill-up.
7861 if ((no_data_chunks == 0) &&
7862 (skip_fill_up == 0) &&
7863 (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7864 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7866 * This for loop we are in takes in each net, if
7867 * its's got space in cwnd and has data sent to it
7868 * (when CMT is off) then it calls
7869 * sctp_fill_outqueue for the net. This gets data on
7870 * the send queue for that network.
7872 * In sctp_fill_outqueue TSN's are assigned and data
7873 * is copied out of the stream buffers. Note mostly
7874 * copy by reference (we hope).
7876 net->window_probe = 0;
7877 if ((net != stcb->asoc.alternate) &&
7878 ((net->dest_state & SCTP_ADDR_PF) ||
7879 ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
7880 (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7881 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7882 sctp_log_cwnd(stcb, net, 1,
7883 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7887 if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7888 (net->flight_size == 0)) {
7889 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7891 if (net->flight_size >= net->cwnd) {
7892 /* skip this network, no room - can't fill */
7893 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7894 sctp_log_cwnd(stcb, net, 3,
7895 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7899 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7900 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7902 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7904 /* memory alloc failure */
7910 /* now service each destination and send out what we can for it */
7911 /* Nothing to send? */
7912 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7913 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7914 TAILQ_EMPTY(&asoc->send_queue)) {
7919 if (asoc->sctp_cmt_on_off > 0) {
7920 /* get the last start point */
7921 start_at = asoc->last_net_cmt_send_started;
7922 if (start_at == NULL) {
7923 /* null so to beginning */
7924 start_at = TAILQ_FIRST(&asoc->nets);
7926 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7927 if (start_at == NULL) {
7928 start_at = TAILQ_FIRST(&asoc->nets);
7931 asoc->last_net_cmt_send_started = start_at;
7933 start_at = TAILQ_FIRST(&asoc->nets);
7935 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7936 if (chk->whoTo == NULL) {
7937 if (asoc->alternate) {
7938 chk->whoTo = asoc->alternate;
7940 chk->whoTo = asoc->primary_destination;
7942 atomic_add_int(&chk->whoTo->ref_count, 1);
7945 old_start_at = NULL;
7946 again_one_more_time:
7947 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7948 /* how much can we send? */
7949 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7950 if (old_start_at && (old_start_at == net)) {
7951 /* through list completely. */
7955 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7956 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7957 (net->flight_size >= net->cwnd)) {
7959 * Nothing on control or asconf and flight is full,
7960 * we can skip even in the CMT case.
7965 endoutchain = outchain = NULL;
7970 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7971 skip_data_for_this_net = 1;
7973 skip_data_for_this_net = 0;
7975 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7978 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7983 mtu = net->mtu - SCTP_MIN_OVERHEAD;
7993 if (mtu > asoc->peers_rwnd) {
7994 if (asoc->total_flight > 0) {
7995 /* We have a packet in flight somewhere */
7996 r_mtu = asoc->peers_rwnd;
7998 /* We are always allowed to send one MTU out */
8006 /************************/
8007 /* ASCONF transmission */
8008 /************************/
8009 /* Now first lets go through the asconf queue */
8010 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8011 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8014 if (chk->whoTo == NULL) {
8015 if (asoc->alternate == NULL) {
8016 if (asoc->primary_destination != net) {
8020 if (asoc->alternate != net) {
8025 if (chk->whoTo != net) {
8029 if (chk->data == NULL) {
8032 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8033 chk->sent != SCTP_DATAGRAM_RESEND) {
8037 * if no AUTH is yet included and this chunk
8038 * requires it, make sure to account for it. We
8039 * don't apply the size until the AUTH chunk is
8040 * actually added below in case there is no room for
8041 * this chunk. NOTE: we overload the use of "omtu"
8044 if ((auth == NULL) &&
8045 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8046 stcb->asoc.peer_auth_chunks)) {
8047 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8050 /* Here we do NOT factor the r_mtu */
8051 if ((chk->send_size < (int)(mtu - omtu)) ||
8052 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8054 * We probably should glom the mbuf chain
8055 * from the chk->data for control but the
8056 * problem is it becomes yet one more level
8057 * of tracking to do if for some reason
8058 * output fails. Then I have got to
8059 * reconstruct the merged control chain.. el
8060 * yucko.. for now we take the easy way and
8064 * Add an AUTH chunk, if chunk requires it
8065 * save the offset into the chain for AUTH
8067 if ((auth == NULL) &&
8068 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8069 stcb->asoc.peer_auth_chunks))) {
8070 outchain = sctp_add_auth_chunk(outchain,
8075 chk->rec.chunk_id.id);
8076 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8078 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8079 (int)chk->rec.chunk_id.can_take_data,
8080 chk->send_size, chk->copy_by_ref);
8081 if (outchain == NULL) {
8083 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8086 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8087 /* update our MTU size */
8088 if (mtu > (chk->send_size + omtu))
8089 mtu -= (chk->send_size + omtu);
8092 to_out += (chk->send_size + omtu);
8093 /* Do clear IP_DF ? */
8094 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8097 if (chk->rec.chunk_id.can_take_data)
8100 * set hb flag since we can use these for
8106 * should sysctl this: don't bundle data
8107 * with ASCONF since it requires AUTH
8110 chk->sent = SCTP_DATAGRAM_SENT;
8111 if (chk->whoTo == NULL) {
8113 atomic_add_int(&net->ref_count, 1);
8118 * Ok we are out of room but we can
8119 * output without effecting the
8120 * flight size since this little guy
8121 * is a control only packet.
8123 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8125 * do NOT clear the asconf flag as
8126 * it is used to do appropriate
8127 * source address selection.
8129 if (*now_filled == 0) {
8130 (void)SCTP_GETTIME_TIMEVAL(now);
8133 net->last_sent_time = *now;
8135 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8136 (struct sockaddr *)&net->ro._l_addr,
8137 outchain, auth_offset, auth,
8138 stcb->asoc.authinfo.active_keyid,
8139 no_fragmentflg, 0, asconf,
8140 inp->sctp_lport, stcb->rport,
8141 htonl(stcb->asoc.peer_vtag),
8146 * error, we could not
8149 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8150 if (from_where == 0) {
8151 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8153 if (error == ENOBUFS) {
8154 asoc->ifp_had_enobuf = 1;
8155 SCTP_STAT_INCR(sctps_lowlevelerr);
8157 /* error, could not output */
8158 if (error == EHOSTUNREACH) {
8164 sctp_move_chunks_from_net(stcb, net);
8169 asoc->ifp_had_enobuf = 0;
8172 * increase the number we sent, if a
8173 * cookie is sent we don't tell them
8176 outchain = endoutchain = NULL;
8180 *num_out += ctl_cnt;
8181 /* recalc a clean slate and setup */
8182 switch (net->ro._l_addr.sa.sa_family) {
8185 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8190 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8207 /************************/
8208 /* Control transmission */
8209 /************************/
8210 /* Now first lets go through the control queue */
8211 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8212 if ((sack_goes_to) &&
8213 (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8214 (chk->whoTo != sack_goes_to)) {
8216 * if we have a sack in queue, and we are
8217 * looking at an ecn echo that is NOT queued
8218 * to where the sack is going..
8220 if (chk->whoTo == net) {
8222 * Don't transmit it to where its
8223 * going (current net)
8226 } else if (sack_goes_to == net) {
8228 * But do transmit it to this
8231 goto skip_net_check;
8234 if (chk->whoTo == NULL) {
8235 if (asoc->alternate == NULL) {
8236 if (asoc->primary_destination != net) {
8240 if (asoc->alternate != net) {
8245 if (chk->whoTo != net) {
8250 if (chk->data == NULL) {
8253 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8255 * It must be unsent. Cookies and ASCONF's
8256 * hang around but there timers will force
8257 * when marked for resend.
8262 * if no AUTH is yet included and this chunk
8263 * requires it, make sure to account for it. We
8264 * don't apply the size until the AUTH chunk is
8265 * actually added below in case there is no room for
8266 * this chunk. NOTE: we overload the use of "omtu"
8269 if ((auth == NULL) &&
8270 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8271 stcb->asoc.peer_auth_chunks)) {
8272 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8275 /* Here we do NOT factor the r_mtu */
8276 if ((chk->send_size <= (int)(mtu - omtu)) ||
8277 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8279 * We probably should glom the mbuf chain
8280 * from the chk->data for control but the
8281 * problem is it becomes yet one more level
8282 * of tracking to do if for some reason
8283 * output fails. Then I have got to
8284 * reconstruct the merged control chain.. el
8285 * yucko.. for now we take the easy way and
8289 * Add an AUTH chunk, if chunk requires it
8290 * save the offset into the chain for AUTH
8292 if ((auth == NULL) &&
8293 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8294 stcb->asoc.peer_auth_chunks))) {
8295 outchain = sctp_add_auth_chunk(outchain,
8300 chk->rec.chunk_id.id);
8301 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8303 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8304 (int)chk->rec.chunk_id.can_take_data,
8305 chk->send_size, chk->copy_by_ref);
8306 if (outchain == NULL) {
8308 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8311 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8312 /* update our MTU size */
8313 if (mtu > (chk->send_size + omtu))
8314 mtu -= (chk->send_size + omtu);
8317 to_out += (chk->send_size + omtu);
8318 /* Do clear IP_DF ? */
8319 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8322 if (chk->rec.chunk_id.can_take_data)
8324 /* Mark things to be removed, if needed */
8325 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8326 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
8327 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8328 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8329 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8330 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8331 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8332 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8333 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8334 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8335 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8336 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8339 /* remove these chunks at the end */
8340 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8341 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8342 /* turn off the timer */
8343 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8344 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8346 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8352 * Other chunks, since they have
8353 * timers running (i.e. COOKIE) we
8354 * just "trust" that it gets sent or
8358 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8361 } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8363 * Increment ecne send count
8364 * here this means we may be
8365 * over-zealous in our
8366 * counting if the send
8367 * fails, but its the best
8368 * place to do it (we used
8369 * to do it in the queue of
8370 * the chunk, but that did
8371 * not tell how many times
8374 SCTP_STAT_INCR(sctps_sendecne);
8376 chk->sent = SCTP_DATAGRAM_SENT;
8377 if (chk->whoTo == NULL) {
8379 atomic_add_int(&net->ref_count, 1);
8385 * Ok we are out of room but we can
8386 * output without effecting the
8387 * flight size since this little guy
8388 * is a control only packet.
8391 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8393 * do NOT clear the asconf
8394 * flag as it is used to do
8395 * appropriate source
8396 * address selection.
8400 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8403 /* Only HB or ASCONF advances time */
8405 if (*now_filled == 0) {
8406 (void)SCTP_GETTIME_TIMEVAL(now);
8409 net->last_sent_time = *now;
8412 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8413 (struct sockaddr *)&net->ro._l_addr,
8416 stcb->asoc.authinfo.active_keyid,
8417 no_fragmentflg, 0, asconf,
8418 inp->sctp_lport, stcb->rport,
8419 htonl(stcb->asoc.peer_vtag),
8424 * error, we could not
8427 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8428 if (from_where == 0) {
8429 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8431 if (error == ENOBUFS) {
8432 asoc->ifp_had_enobuf = 1;
8433 SCTP_STAT_INCR(sctps_lowlevelerr);
8435 if (error == EHOSTUNREACH) {
8441 sctp_move_chunks_from_net(stcb, net);
8446 asoc->ifp_had_enobuf = 0;
8449 * increase the number we sent, if a
8450 * cookie is sent we don't tell them
8453 outchain = endoutchain = NULL;
8457 *num_out += ctl_cnt;
8458 /* recalc a clean slate and setup */
8459 switch (net->ro._l_addr.sa.sa_family) {
8462 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8467 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8484 /* JRI: if dest is in PF state, do not send data to it */
8485 if ((asoc->sctp_cmt_on_off > 0) &&
8486 (net != stcb->asoc.alternate) &&
8487 (net->dest_state & SCTP_ADDR_PF)) {
8490 if (net->flight_size >= net->cwnd) {
8493 if ((asoc->sctp_cmt_on_off > 0) &&
8494 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8495 (net->flight_size > max_rwnd_per_dest)) {
8499 * We need a specific accounting for the usage of the send
8500 * buffer. We also need to check the number of messages per
8501 * net. For now, this is better than nothing and it disabled
8504 if ((asoc->sctp_cmt_on_off > 0) &&
8505 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8506 (max_send_per_dest > 0) &&
8507 (net->flight_size > max_send_per_dest)) {
8510 /*********************/
8511 /* Data transmission */
8512 /*********************/
8514 * if AUTH for DATA is required and no AUTH has been added
8515 * yet, account for this in the mtu now... if no data can be
8516 * bundled, this adjustment won't matter anyways since the
8517 * packet will be going out...
8519 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8520 stcb->asoc.peer_auth_chunks);
8521 if (data_auth_reqd && (auth == NULL)) {
8522 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8524 /* now lets add any data within the MTU constraints */
8525 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8528 if (net->mtu > SCTP_MIN_V4_OVERHEAD)
8529 omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8536 if (net->mtu > SCTP_MIN_OVERHEAD)
8537 omtu = net->mtu - SCTP_MIN_OVERHEAD;
8547 if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
8548 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8549 (skip_data_for_this_net == 0)) ||
8551 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8552 if (no_data_chunks) {
8553 /* let only control go out */
8557 if (net->flight_size >= net->cwnd) {
8558 /* skip this net, no room for data */
8562 if ((chk->whoTo != NULL) &&
8563 (chk->whoTo != net)) {
8564 /* Don't send the chunk on this net */
8568 if (asoc->sctp_cmt_on_off == 0) {
8569 if ((asoc->alternate) &&
8570 (asoc->alternate != net) &&
8571 (chk->whoTo == NULL)) {
8573 } else if ((net != asoc->primary_destination) &&
8574 (asoc->alternate == NULL) &&
8575 (chk->whoTo == NULL)) {
8579 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8581 * strange, we have a chunk that is
8582 * to big for its destination and
8583 * yet no fragment ok flag.
8584 * Something went wrong when the
8585 * PMTU changed...we did not mark
8586 * this chunk for some reason?? I
8587 * will fix it here by letting IP
8588 * fragment it for now and printing
8589 * a warning. This really should not
8592 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8593 chk->send_size, mtu);
8594 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8596 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8597 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
8598 struct sctp_data_chunk *dchkh;
8600 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8601 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8603 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8604 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8605 /* ok we will add this one */
8608 * Add an AUTH chunk, if chunk
8609 * requires it, save the offset into
8610 * the chain for AUTH
8612 if (data_auth_reqd) {
8614 outchain = sctp_add_auth_chunk(outchain,
8620 auth_keyid = chk->auth_keyid;
8622 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8623 } else if (override_ok) {
8628 auth_keyid = chk->auth_keyid;
8630 } else if (auth_keyid != chk->auth_keyid) {
8638 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8639 chk->send_size, chk->copy_by_ref);
8640 if (outchain == NULL) {
8641 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8642 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8643 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8646 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8649 /* update our MTU size */
8650 /* Do clear IP_DF ? */
8651 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8654 /* unsigned subtraction of mtu */
8655 if (mtu > chk->send_size)
8656 mtu -= chk->send_size;
8659 /* unsigned subtraction of r_mtu */
8660 if (r_mtu > chk->send_size)
8661 r_mtu -= chk->send_size;
8665 to_out += chk->send_size;
8666 if ((to_out > mx_mtu) && no_fragmentflg) {
8668 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8670 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8674 chk->window_probe = 0;
8675 data_list[bundle_at++] = chk;
8676 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8679 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8680 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8681 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8683 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8685 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8686 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8696 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8698 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8699 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8700 data_list[0]->window_probe = 1;
8701 net->window_probe = 1;
8707 * Must be sent in order of the
8708 * TSN's (on a network)
8712 } /* for (chunk gather loop for this net) */
8713 } /* if asoc.state OPEN */
8715 /* Is there something to send for this destination? */
8717 /* We may need to start a control timer or two */
8719 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8722 * do NOT clear the asconf flag as it is
8723 * used to do appropriate source address
8728 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8731 /* must start a send timer if data is being sent */
8732 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8734 * no timer running on this destination
8737 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8739 if (bundle_at || hbflag) {
8740 /* For data/asconf and hb set time */
8741 if (*now_filled == 0) {
8742 (void)SCTP_GETTIME_TIMEVAL(now);
8745 net->last_sent_time = *now;
8747 /* Now send it, if there is anything to send :> */
8748 if ((error = sctp_lowlevel_chunk_output(inp,
8751 (struct sockaddr *)&net->ro._l_addr,
8759 inp->sctp_lport, stcb->rport,
8760 htonl(stcb->asoc.peer_vtag),
8764 /* error, we could not output */
8765 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8766 if (from_where == 0) {
8767 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8769 if (error == ENOBUFS) {
8770 asoc->ifp_had_enobuf = 1;
8771 SCTP_STAT_INCR(sctps_lowlevelerr);
8773 if (error == EHOSTUNREACH) {
8775 * Destination went unreachable
8778 sctp_move_chunks_from_net(stcb, net);
8782 * I add this line to be paranoid. As far as
8783 * I can tell the continue, takes us back to
8784 * the top of the for, but just to make sure
8785 * I will reset these again here.
8788 continue; /* This takes us back to the
8789 * for() for the nets. */
8791 asoc->ifp_had_enobuf = 0;
8797 *num_out += (ctl_cnt + bundle_at);
8800 /* setup for a RTO measurement */
8801 tsns_sent = data_list[0]->rec.data.tsn;
8802 /* fill time if not already filled */
8803 if (*now_filled == 0) {
8804 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8806 *now = asoc->time_last_sent;
8808 asoc->time_last_sent = *now;
8810 if (net->rto_needed) {
8811 data_list[0]->do_rtt = 1;
8812 net->rto_needed = 0;
8814 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8815 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8821 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8822 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8825 if (old_start_at == NULL) {
8826 old_start_at = start_at;
8827 start_at = TAILQ_FIRST(&asoc->nets);
8829 goto again_one_more_time;
8833 * At the end there should be no NON timed chunks hanging on this
8836 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8837 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8839 if ((*num_out == 0) && (*reason_code == 0)) {
8844 sctp_clean_up_ctl(stcb, asoc, so_locked);
8849 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8852 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8853 * the control chunk queue.
8855 struct sctp_chunkhdr *hdr;
8856 struct sctp_tmit_chunk *chk;
8857 struct mbuf *mat, *last_mbuf;
8858 uint32_t chunk_length;
8859 uint16_t padding_length;
8861 SCTP_TCB_LOCK_ASSERT(stcb);
8862 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
8863 if (op_err == NULL) {
8868 for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
8869 chunk_length += SCTP_BUF_LEN(mat);
8870 if (SCTP_BUF_NEXT(mat) == NULL) {
8874 if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
8875 sctp_m_freem(op_err);
8878 padding_length = chunk_length % 4;
8879 if (padding_length != 0) {
8880 padding_length = 4 - padding_length;
8882 if (padding_length != 0) {
8883 if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
8884 sctp_m_freem(op_err);
8888 sctp_alloc_a_chunk(stcb, chk);
8891 sctp_m_freem(op_err);
8894 chk->copy_by_ref = 0;
8895 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8896 chk->rec.chunk_id.can_take_data = 0;
8898 chk->send_size = (uint16_t)chunk_length;
8899 chk->sent = SCTP_DATAGRAM_UNSENT;
8901 chk->asoc = &stcb->asoc;
8904 hdr = mtod(op_err, struct sctp_chunkhdr *);
8905 hdr->chunk_type = SCTP_OPERATION_ERROR;
8906 hdr->chunk_flags = 0;
8907 hdr->chunk_length = htons(chk->send_size);
8908 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8909 chk->asoc->ctrl_queue_cnt++;
8913 sctp_send_cookie_echo(struct mbuf *m,
8914 int offset, int limit,
8915 struct sctp_tcb *stcb,
8916 struct sctp_nets *net)
8919 * pull out the cookie and put it at the front of the control chunk
8923 struct mbuf *cookie;
8924 struct sctp_paramhdr param, *phdr;
8925 struct sctp_chunkhdr *hdr;
8926 struct sctp_tmit_chunk *chk;
8927 uint16_t ptype, plen;
8929 SCTP_TCB_LOCK_ASSERT(stcb);
8930 /* First find the cookie in the param area */
8932 at = offset + sizeof(struct sctp_init_chunk);
8934 phdr = sctp_get_next_param(m, at, ¶m, sizeof(param));
8938 ptype = ntohs(phdr->param_type);
8939 plen = ntohs(phdr->param_length);
8940 if (plen < sizeof(struct sctp_paramhdr)) {
8943 if (ptype == SCTP_STATE_COOKIE) {
8946 /* found the cookie */
8947 if (at + plen > limit) {
8950 cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
8951 if (cookie == NULL) {
8955 if ((pad = (plen % 4)) > 0) {
8959 if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
8963 #ifdef SCTP_MBUF_LOGGING
8964 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8965 sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
8970 at += SCTP_SIZE32(plen);
8972 /* ok, we got the cookie lets change it into a cookie echo chunk */
8973 /* first the change from param to cookie */
8974 hdr = mtod(cookie, struct sctp_chunkhdr *);
8975 hdr->chunk_type = SCTP_COOKIE_ECHO;
8976 hdr->chunk_flags = 0;
8977 /* get the chunk stuff now and place it in the FRONT of the queue */
8978 sctp_alloc_a_chunk(stcb, chk);
8981 sctp_m_freem(cookie);
8984 chk->copy_by_ref = 0;
8985 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8986 chk->rec.chunk_id.can_take_data = 0;
8987 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8988 chk->send_size = SCTP_SIZE32(plen);
8989 chk->sent = SCTP_DATAGRAM_UNSENT;
8991 chk->asoc = &stcb->asoc;
8994 atomic_add_int(&chk->whoTo->ref_count, 1);
8995 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8996 chk->asoc->ctrl_queue_cnt++;
9001 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9005 struct sctp_nets *net)
9008 * take a HB request and make it into a HB ack and send it.
9010 struct mbuf *outchain;
9011 struct sctp_chunkhdr *chdr;
9012 struct sctp_tmit_chunk *chk;
9015 /* must have a net pointer */
9018 outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9019 if (outchain == NULL) {
9020 /* gak out of memory */
9023 #ifdef SCTP_MBUF_LOGGING
9024 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9025 sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9028 chdr = mtod(outchain, struct sctp_chunkhdr *);
9029 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9030 chdr->chunk_flags = 0;
9031 if (chk_length % 4 != 0) {
9032 sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9034 sctp_alloc_a_chunk(stcb, chk);
9037 sctp_m_freem(outchain);
9040 chk->copy_by_ref = 0;
9041 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9042 chk->rec.chunk_id.can_take_data = 1;
9044 chk->send_size = chk_length;
9045 chk->sent = SCTP_DATAGRAM_UNSENT;
9047 chk->asoc = &stcb->asoc;
9048 chk->data = outchain;
9050 atomic_add_int(&chk->whoTo->ref_count, 1);
9051 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9052 chk->asoc->ctrl_queue_cnt++;
9056 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9058 /* formulate and queue a cookie-ack back to sender */
9059 struct mbuf *cookie_ack;
9060 struct sctp_chunkhdr *hdr;
9061 struct sctp_tmit_chunk *chk;
9063 SCTP_TCB_LOCK_ASSERT(stcb);
9065 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9066 if (cookie_ack == NULL) {
9070 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9071 sctp_alloc_a_chunk(stcb, chk);
9074 sctp_m_freem(cookie_ack);
9077 chk->copy_by_ref = 0;
9078 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9079 chk->rec.chunk_id.can_take_data = 1;
9081 chk->send_size = sizeof(struct sctp_chunkhdr);
9082 chk->sent = SCTP_DATAGRAM_UNSENT;
9084 chk->asoc = &stcb->asoc;
9085 chk->data = cookie_ack;
9086 if (chk->asoc->last_control_chunk_from != NULL) {
9087 chk->whoTo = chk->asoc->last_control_chunk_from;
9088 atomic_add_int(&chk->whoTo->ref_count, 1);
9092 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9093 hdr->chunk_type = SCTP_COOKIE_ACK;
9094 hdr->chunk_flags = 0;
9095 hdr->chunk_length = htons(chk->send_size);
9096 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9097 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9098 chk->asoc->ctrl_queue_cnt++;
9103 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9105 /* formulate and queue a SHUTDOWN-ACK back to the sender */
9106 struct mbuf *m_shutdown_ack;
9107 struct sctp_shutdown_ack_chunk *ack_cp;
9108 struct sctp_tmit_chunk *chk;
9110 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9111 if (m_shutdown_ack == NULL) {
9115 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9116 sctp_alloc_a_chunk(stcb, chk);
9119 sctp_m_freem(m_shutdown_ack);
9122 chk->copy_by_ref = 0;
9123 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9124 chk->rec.chunk_id.can_take_data = 1;
9126 chk->send_size = sizeof(struct sctp_chunkhdr);
9127 chk->sent = SCTP_DATAGRAM_UNSENT;
9129 chk->asoc = &stcb->asoc;
9130 chk->data = m_shutdown_ack;
9133 atomic_add_int(&chk->whoTo->ref_count, 1);
9135 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9136 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9137 ack_cp->ch.chunk_flags = 0;
9138 ack_cp->ch.chunk_length = htons(chk->send_size);
9139 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9140 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9141 chk->asoc->ctrl_queue_cnt++;
9146 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9148 /* formulate and queue a SHUTDOWN to the sender */
9149 struct mbuf *m_shutdown;
9150 struct sctp_shutdown_chunk *shutdown_cp;
9151 struct sctp_tmit_chunk *chk;
9153 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9154 if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9155 /* We already have a SHUTDOWN queued. Reuse it. */
9157 sctp_free_remote_addr(chk->whoTo);
9164 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9165 if (m_shutdown == NULL) {
9169 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9170 sctp_alloc_a_chunk(stcb, chk);
9173 sctp_m_freem(m_shutdown);
9176 chk->copy_by_ref = 0;
9177 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9178 chk->rec.chunk_id.can_take_data = 1;
9180 chk->send_size = sizeof(struct sctp_shutdown_chunk);
9181 chk->sent = SCTP_DATAGRAM_UNSENT;
9183 chk->asoc = &stcb->asoc;
9184 chk->data = m_shutdown;
9187 atomic_add_int(&chk->whoTo->ref_count, 1);
9189 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9190 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9191 shutdown_cp->ch.chunk_flags = 0;
9192 shutdown_cp->ch.chunk_length = htons(chk->send_size);
9193 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9194 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9195 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9196 chk->asoc->ctrl_queue_cnt++;
9198 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9201 atomic_add_int(&chk->whoTo->ref_count, 1);
9203 shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9204 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9205 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9211 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9214 * formulate and queue an ASCONF to the peer. ASCONF parameters
9215 * should be queued on the assoc queue.
9217 struct sctp_tmit_chunk *chk;
9218 struct mbuf *m_asconf;
9221 SCTP_TCB_LOCK_ASSERT(stcb);
9223 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9224 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9225 /* can't send a new one if there is one in flight already */
9229 /* compose an ASCONF chunk, maximum length is PMTU */
9230 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9231 if (m_asconf == NULL) {
9235 sctp_alloc_a_chunk(stcb, chk);
9238 sctp_m_freem(m_asconf);
9242 chk->copy_by_ref = 0;
9243 chk->rec.chunk_id.id = SCTP_ASCONF;
9244 chk->rec.chunk_id.can_take_data = 0;
9245 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9246 chk->data = m_asconf;
9247 chk->send_size = len;
9248 chk->sent = SCTP_DATAGRAM_UNSENT;
9250 chk->asoc = &stcb->asoc;
9253 atomic_add_int(&chk->whoTo->ref_count, 1);
9255 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9256 chk->asoc->ctrl_queue_cnt++;
9261 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9264 * formulate and queue a asconf-ack back to sender. the asconf-ack
9265 * must be stored in the tcb.
9267 struct sctp_tmit_chunk *chk;
9268 struct sctp_asconf_ack *ack, *latest_ack;
9270 struct sctp_nets *net = NULL;
9272 SCTP_TCB_LOCK_ASSERT(stcb);
9273 /* Get the latest ASCONF-ACK */
9274 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9275 if (latest_ack == NULL) {
9278 if (latest_ack->last_sent_to != NULL &&
9279 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9280 /* we're doing a retransmission */
9281 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9284 if (stcb->asoc.last_control_chunk_from == NULL) {
9285 if (stcb->asoc.alternate) {
9286 net = stcb->asoc.alternate;
9288 net = stcb->asoc.primary_destination;
9291 net = stcb->asoc.last_control_chunk_from;
9296 if (stcb->asoc.last_control_chunk_from == NULL) {
9297 if (stcb->asoc.alternate) {
9298 net = stcb->asoc.alternate;
9300 net = stcb->asoc.primary_destination;
9303 net = stcb->asoc.last_control_chunk_from;
9306 latest_ack->last_sent_to = net;
9308 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9309 if (ack->data == NULL) {
9313 /* copy the asconf_ack */
9314 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
9315 if (m_ack == NULL) {
9316 /* couldn't copy it */
9319 #ifdef SCTP_MBUF_LOGGING
9320 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9321 sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
9325 sctp_alloc_a_chunk(stcb, chk);
9329 sctp_m_freem(m_ack);
9332 chk->copy_by_ref = 0;
9333 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9334 chk->rec.chunk_id.can_take_data = 1;
9335 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9338 atomic_add_int(&chk->whoTo->ref_count, 1);
9341 chk->send_size = ack->len;
9342 chk->sent = SCTP_DATAGRAM_UNSENT;
9344 chk->asoc = &stcb->asoc;
9346 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9347 chk->asoc->ctrl_queue_cnt++;
9353 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9354 struct sctp_tcb *stcb,
9355 struct sctp_association *asoc,
9356 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
9359 * send out one MTU of retransmission. If fast_retransmit is
9360 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9361 * rwnd. For a Cookie or Asconf in the control chunk queue we
9362 * retransmit them by themselves.
9364 * For data chunks we will pick out the lowest TSN's in the sent_queue
9365 * marked for resend and bundle them all together (up to a MTU of
9366 * destination). The address to send to should have been
9367 * selected/changed where the retransmission was marked (i.e. in FR
9368 * or t3-timeout routines).
9370 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9371 struct sctp_tmit_chunk *chk, *fwd;
9372 struct mbuf *m, *endofchain;
9373 struct sctp_nets *net = NULL;
9374 uint32_t tsns_sent = 0;
9375 int no_fragmentflg, bundle_at;
9377 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9378 struct sctp_auth_chunk *auth = NULL;
9379 uint32_t auth_offset = 0;
9380 uint16_t auth_keyid;
9381 int override_ok = 1;
9382 int data_auth_reqd = 0;
9385 SCTP_TCB_LOCK_ASSERT(stcb);
9386 tmr_started = ctl_cnt = 0;
9391 endofchain = m = NULL;
9392 auth_keyid = stcb->asoc.authinfo.active_keyid;
9393 #ifdef SCTP_AUDITING_ENABLED
9394 sctp_audit_log(0xC3, 1);
9396 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9397 (TAILQ_EMPTY(&asoc->control_send_queue))) {
9398 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9399 asoc->sent_queue_retran_cnt);
9400 asoc->sent_queue_cnt = 0;
9401 asoc->sent_queue_cnt_removeable = 0;
9402 /* send back 0/0 so we enter normal transmission */
9406 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9407 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9408 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9409 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9410 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9413 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9414 if (chk != asoc->str_reset) {
9416 * not eligible for retran if its
9423 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9427 * Add an AUTH chunk, if chunk requires it save the
9428 * offset into the chain for AUTH
9430 if ((auth == NULL) &&
9431 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9432 stcb->asoc.peer_auth_chunks))) {
9433 m = sctp_add_auth_chunk(m, &endofchain,
9434 &auth, &auth_offset,
9436 chk->rec.chunk_id.id);
9437 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9439 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9444 /* do we have control chunks to retransmit? */
9446 /* Start a timer no matter if we succeed or fail */
9447 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9448 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9449 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9450 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9451 chk->snd_count++; /* update our count */
9452 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9453 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9454 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9455 no_fragmentflg, 0, 0,
9456 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9457 chk->whoTo->port, NULL,
9460 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9461 if (error == ENOBUFS) {
9462 asoc->ifp_had_enobuf = 1;
9463 SCTP_STAT_INCR(sctps_lowlevelerr);
9467 asoc->ifp_had_enobuf = 0;
9473 * We don't want to mark the net->sent time here since this
9474 * we use this for HB and retrans cannot measure RTT
9476 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9478 chk->sent = SCTP_DATAGRAM_SENT;
9479 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9483 /* Clean up the fwd-tsn list */
9484 sctp_clean_up_ctl(stcb, asoc, so_locked);
9489 * Ok, it is just data retransmission we need to do or that and a
9490 * fwd-tsn with it all.
9492 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9493 return (SCTP_RETRAN_DONE);
9495 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
9496 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
9497 /* not yet open, resend the cookie and that is it */
9500 #ifdef SCTP_AUDITING_ENABLED
9501 sctp_auditing(20, inp, stcb, NULL);
9503 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9504 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9505 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9506 /* No, not sent to this net or not ready for rtx */
9509 if (chk->data == NULL) {
9510 SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9511 chk->rec.data.tsn, chk->snd_count, chk->sent);
9514 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9515 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9516 struct mbuf *op_err;
9517 char msg[SCTP_DIAG_INFO_LEN];
9519 SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
9520 chk->rec.data.tsn, chk->snd_count);
9521 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
9523 atomic_add_int(&stcb->asoc.refcnt, 1);
9524 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
9526 SCTP_TCB_LOCK(stcb);
9527 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9528 return (SCTP_RETRAN_EXIT);
9530 /* pick up the net */
9532 switch (net->ro._l_addr.sa.sa_family) {
9535 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9540 mtu = net->mtu - SCTP_MIN_OVERHEAD;
9549 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9550 /* No room in peers rwnd */
9553 tsn = asoc->last_acked_seq + 1;
9554 if (tsn == chk->rec.data.tsn) {
9556 * we make a special exception for this
9557 * case. The peer has no rwnd but is missing
9558 * the lowest chunk.. which is probably what
9559 * is holding up the rwnd.
9561 goto one_chunk_around;
9566 if (asoc->peers_rwnd < mtu) {
9568 if ((asoc->peers_rwnd == 0) &&
9569 (asoc->total_flight == 0)) {
9570 chk->window_probe = 1;
9571 chk->whoTo->window_probe = 1;
9574 #ifdef SCTP_AUDITING_ENABLED
9575 sctp_audit_log(0xC3, 2);
9579 net->fast_retran_ip = 0;
9580 if (chk->rec.data.doing_fast_retransmit == 0) {
9582 * if no FR in progress skip destination that have
9583 * flight_size > cwnd.
9585 if (net->flight_size >= net->cwnd) {
9590 * Mark the destination net to have FR recovery
9594 net->fast_retran_ip = 1;
9598 * if no AUTH is yet included and this chunk requires it,
9599 * make sure to account for it. We don't apply the size
9600 * until the AUTH chunk is actually added below in case
9601 * there is no room for this chunk.
9603 if (data_auth_reqd && (auth == NULL)) {
9604 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9608 if ((chk->send_size <= (mtu - dmtu)) ||
9609 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9610 /* ok we will add this one */
9611 if (data_auth_reqd) {
9613 m = sctp_add_auth_chunk(m,
9619 auth_keyid = chk->auth_keyid;
9621 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9622 } else if (override_ok) {
9623 auth_keyid = chk->auth_keyid;
9625 } else if (chk->auth_keyid != auth_keyid) {
9626 /* different keyid, so done bundling */
9630 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9632 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9635 /* Do clear IP_DF ? */
9636 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9639 /* update our MTU size */
9640 if (mtu > (chk->send_size + dmtu))
9641 mtu -= (chk->send_size + dmtu);
9644 data_list[bundle_at++] = chk;
9645 if (one_chunk && (asoc->total_flight <= 0)) {
9646 SCTP_STAT_INCR(sctps_windowprobed);
9649 if (one_chunk == 0) {
9651 * now are there anymore forward from chk to pick
9654 for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9655 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9656 /* Nope, not for retran */
9659 if (fwd->whoTo != net) {
9660 /* Nope, not the net in question */
9663 if (data_auth_reqd && (auth == NULL)) {
9664 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9667 if (fwd->send_size <= (mtu - dmtu)) {
9668 if (data_auth_reqd) {
9670 m = sctp_add_auth_chunk(m,
9676 auth_keyid = fwd->auth_keyid;
9678 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9679 } else if (override_ok) {
9680 auth_keyid = fwd->auth_keyid;
9682 } else if (fwd->auth_keyid != auth_keyid) {
9690 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9692 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9695 /* Do clear IP_DF ? */
9696 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9699 /* update our MTU size */
9700 if (mtu > (fwd->send_size + dmtu))
9701 mtu -= (fwd->send_size + dmtu);
9704 data_list[bundle_at++] = fwd;
9705 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9709 /* can't fit so we are done */
9714 /* Is there something to send for this destination? */
9717 * No matter if we fail/or succeed we should start a
9718 * timer. A failure is like a lost IP packet :-)
9720 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9722 * no timer running on this destination
9725 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9728 /* Now lets send it, if there is anything to send :> */
9729 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9730 (struct sockaddr *)&net->ro._l_addr, m,
9731 auth_offset, auth, auth_keyid,
9732 no_fragmentflg, 0, 0,
9733 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9737 /* error, we could not output */
9738 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9739 if (error == ENOBUFS) {
9740 asoc->ifp_had_enobuf = 1;
9741 SCTP_STAT_INCR(sctps_lowlevelerr);
9745 asoc->ifp_had_enobuf = 0;
9752 * We don't want to mark the net->sent time here
9753 * since this we use this for HB and retrans cannot
9756 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9758 /* For auto-close */
9759 if (*now_filled == 0) {
9760 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9761 *now = asoc->time_last_sent;
9764 asoc->time_last_sent = *now;
9766 *cnt_out += bundle_at;
9767 #ifdef SCTP_AUDITING_ENABLED
9768 sctp_audit_log(0xC4, bundle_at);
9771 tsns_sent = data_list[0]->rec.data.tsn;
9773 for (i = 0; i < bundle_at; i++) {
9774 SCTP_STAT_INCR(sctps_sendretransdata);
9775 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9777 * When we have a revoked data, and we
9778 * retransmit it, then we clear the revoked
9779 * flag since this flag dictates if we
9780 * subtracted from the fs
9782 if (data_list[i]->rec.data.chunk_was_revoked) {
9783 /* Deflate the cwnd */
9784 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9785 data_list[i]->rec.data.chunk_was_revoked = 0;
9787 data_list[i]->snd_count++;
9788 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9789 /* record the time */
9790 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9791 if (data_list[i]->book_size_scale) {
9793 * need to double the book size on
9796 data_list[i]->book_size_scale = 0;
9798 * Since we double the booksize, we
9799 * must also double the output queue
9800 * size, since this get shrunk when
9801 * we free by this amount.
9803 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9804 data_list[i]->book_size *= 2;
9806 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9807 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9808 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9810 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9811 (uint32_t)(data_list[i]->send_size +
9812 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9814 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9815 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9816 data_list[i]->whoTo->flight_size,
9817 data_list[i]->book_size,
9818 (uint32_t)(uintptr_t)data_list[i]->whoTo,
9819 data_list[i]->rec.data.tsn);
9821 sctp_flight_size_increase(data_list[i]);
9822 sctp_total_flight_increase(stcb, data_list[i]);
9823 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9824 /* SWS sender side engages */
9825 asoc->peers_rwnd = 0;
9828 (data_list[i]->rec.data.doing_fast_retransmit)) {
9829 SCTP_STAT_INCR(sctps_sendfastretrans);
9830 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9831 (tmr_started == 0)) {
9833 * ok we just fast-retrans'd
9834 * the lowest TSN, i.e the
9835 * first on the list. In
9836 * this case we want to give
9837 * some more time to get a
9838 * SACK back without a
9841 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9842 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
9843 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9847 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9848 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9850 #ifdef SCTP_AUDITING_ENABLED
9851 sctp_auditing(21, inp, stcb, NULL);
9857 if (asoc->sent_queue_retran_cnt <= 0) {
9858 /* all done we have no more to retran */
9859 asoc->sent_queue_retran_cnt = 0;
9863 /* No more room in rwnd */
9866 /* stop the for loop here. we sent out a packet */
9873 sctp_timer_validation(struct sctp_inpcb *inp,
9874 struct sctp_tcb *stcb,
9875 struct sctp_association *asoc)
9877 struct sctp_nets *net;
9879 /* Validate that a timer is running somewhere */
9880 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9881 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9882 /* Here is a timer */
9886 SCTP_TCB_LOCK_ASSERT(stcb);
9887 /* Gak, we did not have a timer somewhere */
9888 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9889 if (asoc->alternate) {
9890 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9892 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9898 sctp_chunk_output(struct sctp_inpcb *inp,
9899 struct sctp_tcb *stcb,
9904 * Ok this is the generic chunk service queue. we must do the
9906 * - See if there are retransmits pending, if so we must
9908 * - Service the stream queue that is next, moving any
9909 * message (note I must get a complete message i.e.
9910 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9912 * - Check to see if the cwnd/rwnd allows any output, if so we
9913 * go ahead and formulate and send the low level chunks. Making sure
9914 * to combine any control in the control chunk queue also.
9916 struct sctp_association *asoc;
9917 struct sctp_nets *net;
9918 int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
9919 unsigned int burst_cnt = 0;
9923 uint32_t frag_point = sctp_get_frag_point(stcb);
9926 unsigned int tot_frs = 0;
9930 /* The Nagle algorithm is only applied when handling a send call. */
9931 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9932 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9940 SCTP_TCB_LOCK_ASSERT(stcb);
9942 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9944 if ((un_sent <= 0) &&
9945 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9946 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9947 (asoc->sent_queue_retran_cnt == 0) &&
9948 (asoc->trigger_reset == 0)) {
9949 /* Nothing to do unless there is something to be sent left */
9953 * Do we have something to send, data or control AND a sack timer
9954 * running, if so piggy-back the sack.
9956 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9957 sctp_send_sack(stcb, so_locked);
9958 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
9959 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
9961 while (asoc->sent_queue_retran_cnt) {
9963 * Ok, it is retransmission time only, we send out only ONE
9964 * packet with a single call off to the retran code.
9966 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9968 * Special hook for handling cookies discarded
9969 * by peer that carried data. Send cookie-ack only
9970 * and then the next call with get the retran's.
9972 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9974 &now, &now_filled, frag_point, so_locked);
9976 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9977 /* if its not from a HB then do it */
9979 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9985 * its from any other place, we don't allow retran
9986 * output (only control)
9991 /* Can't send anymore */
9993 * now lets push out control by calling med-level
9994 * output once. this assures that we WILL send HB's
9997 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9999 &now, &now_filled, frag_point, so_locked);
10000 #ifdef SCTP_AUDITING_ENABLED
10001 sctp_auditing(8, inp, stcb, NULL);
10003 sctp_timer_validation(inp, stcb, asoc);
10008 * The count was off.. retran is not happening so do
10009 * the normal retransmission.
10011 #ifdef SCTP_AUDITING_ENABLED
10012 sctp_auditing(9, inp, stcb, NULL);
10014 if (ret == SCTP_RETRAN_EXIT) {
10019 if (from_where == SCTP_OUTPUT_FROM_T3) {
10020 /* Only one transmission allowed out of a timeout */
10021 #ifdef SCTP_AUDITING_ENABLED
10022 sctp_auditing(10, inp, stcb, NULL);
10024 /* Push out any control */
10025 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10026 &now, &now_filled, frag_point, so_locked);
10029 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10030 /* Hit FR burst limit */
10033 if ((num_out == 0) && (ret == 0)) {
10034 /* No more retrans to send */
10038 #ifdef SCTP_AUDITING_ENABLED
10039 sctp_auditing(12, inp, stcb, NULL);
10041 /* Check for bad destinations, if they exist move chunks around. */
10042 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10043 if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10045 * if possible move things off of this address we
10046 * still may send below due to the dormant state but
10047 * we try to find an alternate address to send to
10048 * and if we have one we move all queued data on the
10049 * out wheel to this alternate address.
10051 if (net->ref_count > 1)
10052 sctp_move_chunks_from_net(stcb, net);
10055 * if ((asoc->sat_network) || (net->addr_is_local))
10056 * { burst_limit = asoc->max_burst *
10057 * SCTP_SAT_NETWORK_BURST_INCR; }
10059 if (asoc->max_burst > 0) {
10060 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10061 if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10063 * JRS - Use the congestion
10064 * control given in the
10065 * congestion control module
10067 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10068 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10069 sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10071 SCTP_STAT_INCR(sctps_maxburstqueued);
10073 net->fast_retran_ip = 0;
10075 if (net->flight_size == 0) {
10077 * Should be decaying the
10088 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10089 &reason_code, 0, from_where,
10090 &now, &now_filled, frag_point, so_locked);
10092 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10093 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10094 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10096 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10097 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10098 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10102 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10104 tot_out += num_out;
10106 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10107 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10108 if (num_out == 0) {
10109 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10114 * When the Nagle algorithm is used, look at how
10115 * much is unsent, then if its smaller than an MTU
10116 * and we have data in flight we stop, except if we
10117 * are handling a fragmented user message.
10119 un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10120 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10121 (stcb->asoc.total_flight > 0)) {
10122 /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10126 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10127 TAILQ_EMPTY(&asoc->send_queue) &&
10128 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10129 /* Nothing left to send */
10132 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10133 /* Nothing left to send */
10136 } while (num_out &&
10137 ((asoc->max_burst == 0) ||
10138 SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10139 (burst_cnt < asoc->max_burst)));
10141 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10142 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10143 SCTP_STAT_INCR(sctps_maxburstqueued);
10144 asoc->burst_limit_applied = 1;
10145 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10146 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10149 asoc->burst_limit_applied = 0;
10152 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10153 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10155 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10159 * Now we need to clean up the control chunk chain if a ECNE is on
10160 * it. It must be marked as UNSENT again so next call will continue
10161 * to send it until such time that we get a CWR, to remove it.
10163 if (stcb->asoc.ecn_echo_cnt_onq)
10164 sctp_fix_ecn_echo(asoc);
10166 if (stcb->asoc.trigger_reset) {
10167 if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10176 struct sctp_inpcb *inp,
10178 struct sockaddr *addr,
10179 struct mbuf *control,
10184 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10188 if (inp->sctp_socket == NULL) {
10189 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10192 return (sctp_sosend(inp->sctp_socket,
10194 (struct uio *)NULL,
10202 send_forward_tsn(struct sctp_tcb *stcb,
10203 struct sctp_association *asoc)
10205 struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10206 struct sctp_forward_tsn_chunk *fwdtsn;
10207 struct sctp_strseq *strseq;
10208 struct sctp_strseq_mid *strseq_m;
10209 uint32_t advance_peer_ack_point;
10210 unsigned int cnt_of_space, i, ovh;
10211 unsigned int space_needed;
10212 unsigned int cnt_of_skipped = 0;
10214 SCTP_TCB_LOCK_ASSERT(stcb);
10215 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10216 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10217 /* mark it to unsent */
10218 chk->sent = SCTP_DATAGRAM_UNSENT;
10219 chk->snd_count = 0;
10220 /* Do we correct its output location? */
10222 sctp_free_remote_addr(chk->whoTo);
10225 goto sctp_fill_in_rest;
10228 /* Ok if we reach here we must build one */
10229 sctp_alloc_a_chunk(stcb, chk);
10233 asoc->fwd_tsn_cnt++;
10234 chk->copy_by_ref = 0;
10236 * We don't do the old thing here since this is used not for on-wire
10237 * but to tell if we are sending a fwd-tsn by the stack during
10238 * output. And if its a IFORWARD or a FORWARD it is a fwd-tsn.
10240 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10241 chk->rec.chunk_id.can_take_data = 0;
10245 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10246 if (chk->data == NULL) {
10247 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10250 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10251 chk->sent = SCTP_DATAGRAM_UNSENT;
10252 chk->snd_count = 0;
10253 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10254 asoc->ctrl_queue_cnt++;
10257 * Here we go through and fill out the part that deals with
10258 * stream/seq of the ones we skip.
10260 SCTP_BUF_LEN(chk->data) = 0;
10261 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10262 if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
10263 (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
10264 /* no more to look at */
10267 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10268 /* We don't report these */
10273 if (asoc->idata_supported) {
10274 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10275 (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
10277 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10278 (cnt_of_skipped * sizeof(struct sctp_strseq)));
10280 cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
10282 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10283 ovh = SCTP_MIN_OVERHEAD;
10285 ovh = SCTP_MIN_V4_OVERHEAD;
10287 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10288 /* trim to a mtu size */
10289 cnt_of_space = asoc->smallest_mtu - ovh;
10291 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10292 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10293 0xff, 0, cnt_of_skipped,
10294 asoc->advanced_peer_ack_point);
10296 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10297 if (cnt_of_space < space_needed) {
10299 * ok we must trim down the chunk by lowering the
10300 * advance peer ack point.
10302 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10303 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10304 0xff, 0xff, cnt_of_space,
10307 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10308 if (asoc->idata_supported) {
10309 cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
10311 cnt_of_skipped /= sizeof(struct sctp_strseq);
10314 * Go through and find the TSN that will be the one
10317 at = TAILQ_FIRST(&asoc->sent_queue);
10319 for (i = 0; i < cnt_of_skipped; i++) {
10320 tp1 = TAILQ_NEXT(at, sctp_next);
10327 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10328 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10329 0xff, cnt_of_skipped, at->rec.data.tsn,
10330 asoc->advanced_peer_ack_point);
10334 * last now points to last one I can report, update
10338 advance_peer_ack_point = last->rec.data.tsn;
10340 if (asoc->idata_supported) {
10341 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10342 cnt_of_skipped * sizeof(struct sctp_strseq_mid);
10344 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10345 cnt_of_skipped * sizeof(struct sctp_strseq);
10348 chk->send_size = space_needed;
10349 /* Setup the chunk */
10350 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10351 fwdtsn->ch.chunk_length = htons(chk->send_size);
10352 fwdtsn->ch.chunk_flags = 0;
10353 if (asoc->idata_supported) {
10354 fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
10356 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10358 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10359 SCTP_BUF_LEN(chk->data) = chk->send_size;
10362 * Move pointer to after the fwdtsn and transfer to the
10365 if (asoc->idata_supported) {
10366 strseq_m = (struct sctp_strseq_mid *)fwdtsn;
10369 strseq = (struct sctp_strseq *)fwdtsn;
10373 * Now populate the strseq list. This is done blindly
10374 * without pulling out duplicate stream info. This is
10375 * inefficient but won't harm the process since the peer will
10376 * look at these in sequence and will thus release anything.
10377 * It could mean we exceed the PMTU and chop off some that
10378 * we could have included.. but this is unlikely (aka 1432/4
10379 * would mean 300+ stream seq's would have to be reported in
10380 * one FWD-TSN. With a bit of work we can later FIX this to
10381 * optimize and pull out duplicates.. but it does add more
10382 * overhead. So for now... not!
10385 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10386 if (i >= cnt_of_skipped) {
10389 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10390 /* We don't report these */
10393 if (at->rec.data.tsn == advance_peer_ack_point) {
10394 at->rec.data.fwd_tsn_cnt = 0;
10396 if (asoc->idata_supported) {
10397 strseq_m->sid = htons(at->rec.data.sid);
10398 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10399 strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
10401 strseq_m->flags = 0;
10403 strseq_m->mid = htonl(at->rec.data.mid);
10406 strseq->sid = htons(at->rec.data.sid);
10407 strseq->ssn = htons((uint16_t)at->rec.data.mid);
10416 sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
10419 * Queue up a SACK or NR-SACK in the control queue.
10420 * We must first check to see if a SACK or NR-SACK is
10421 * somehow on the control queue.
10422 * If so, we will take and and remove the old one.
10424 struct sctp_association *asoc;
10425 struct sctp_tmit_chunk *chk, *a_chk;
10426 struct sctp_sack_chunk *sack;
10427 struct sctp_nr_sack_chunk *nr_sack;
10428 struct sctp_gap_ack_block *gap_descriptor;
10429 const struct sack_track *selector;
10434 int limit_reached = 0;
10435 unsigned int i, siz, j;
10436 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10439 uint32_t highest_tsn;
10444 if (stcb->asoc.nrsack_supported == 1) {
10445 type = SCTP_NR_SELECTIVE_ACK;
10447 type = SCTP_SELECTIVE_ACK;
10450 asoc = &stcb->asoc;
10451 SCTP_TCB_LOCK_ASSERT(stcb);
10452 if (asoc->last_data_chunk_from == NULL) {
10453 /* Hmm we never received anything */
10456 sctp_slide_mapping_arrays(stcb);
10457 sctp_set_rwnd(stcb, asoc);
10458 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10459 if (chk->rec.chunk_id.id == type) {
10460 /* Hmm, found a sack already on queue, remove it */
10461 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10462 asoc->ctrl_queue_cnt--;
10465 sctp_m_freem(a_chk->data);
10466 a_chk->data = NULL;
10468 if (a_chk->whoTo) {
10469 sctp_free_remote_addr(a_chk->whoTo);
10470 a_chk->whoTo = NULL;
10475 if (a_chk == NULL) {
10476 sctp_alloc_a_chunk(stcb, a_chk);
10477 if (a_chk == NULL) {
10478 /* No memory so we drop the idea, and set a timer */
10479 if (stcb->asoc.delayed_ack) {
10480 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10481 stcb->sctp_ep, stcb, NULL,
10482 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
10483 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10484 stcb->sctp_ep, stcb, NULL);
10486 stcb->asoc.send_sack = 1;
10490 a_chk->copy_by_ref = 0;
10491 a_chk->rec.chunk_id.id = type;
10492 a_chk->rec.chunk_id.can_take_data = 1;
10494 /* Clear our pkt counts */
10495 asoc->data_pkts_seen = 0;
10498 a_chk->asoc = asoc;
10499 a_chk->snd_count = 0;
10500 a_chk->send_size = 0; /* fill in later */
10501 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10502 a_chk->whoTo = NULL;
10504 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10506 * Ok, the destination for the SACK is unreachable, lets see if
10507 * we can select an alternate to asoc->last_data_chunk_from
10509 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10510 if (a_chk->whoTo == NULL) {
10511 /* Nope, no alternate */
10512 a_chk->whoTo = asoc->last_data_chunk_from;
10515 a_chk->whoTo = asoc->last_data_chunk_from;
10517 if (a_chk->whoTo) {
10518 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10520 if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10521 highest_tsn = asoc->highest_tsn_inside_map;
10523 highest_tsn = asoc->highest_tsn_inside_nr_map;
10525 if (highest_tsn == asoc->cumulative_tsn) {
10527 if (type == SCTP_SELECTIVE_ACK) {
10528 space_req = sizeof(struct sctp_sack_chunk);
10530 space_req = sizeof(struct sctp_nr_sack_chunk);
10533 /* gaps get a cluster */
10534 space_req = MCLBYTES;
10536 /* Ok now lets formulate a MBUF with our sack */
10537 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
10538 if ((a_chk->data == NULL) ||
10539 (a_chk->whoTo == NULL)) {
10540 /* rats, no mbuf memory */
10542 /* was a problem with the destination */
10543 sctp_m_freem(a_chk->data);
10544 a_chk->data = NULL;
10546 sctp_free_a_chunk(stcb, a_chk, so_locked);
10547 /* sa_ignore NO_NULL_CHK */
10548 if (stcb->asoc.delayed_ack) {
10549 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10550 stcb->sctp_ep, stcb, NULL,
10551 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10552 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10553 stcb->sctp_ep, stcb, NULL);
10555 stcb->asoc.send_sack = 1;
10559 /* ok, lets go through and fill it in */
10560 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10561 space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
10562 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10563 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10565 limit = mtod(a_chk->data, caddr_t);
10570 if ((asoc->sctp_cmt_on_off > 0) &&
10571 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10573 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10574 * received, then set high bit to 1, else 0. Reset
10577 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10578 asoc->cmt_dac_pkts_rcvd = 0;
10580 #ifdef SCTP_ASOCLOG_OF_TSNS
10581 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10582 stcb->asoc.cumack_log_atsnt++;
10583 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10584 stcb->asoc.cumack_log_atsnt = 0;
10587 /* reset the readers interpretation */
10588 stcb->freed_by_sorcv_sincelast = 0;
10590 if (type == SCTP_SELECTIVE_ACK) {
10591 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10593 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10594 if (highest_tsn > asoc->mapping_array_base_tsn) {
10595 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10597 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
10601 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10602 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10603 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10604 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10606 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10610 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10613 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10615 if (((type == SCTP_SELECTIVE_ACK) &&
10616 SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10617 ((type == SCTP_NR_SELECTIVE_ACK) &&
10618 SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10619 /* we have a gap .. maybe */
10620 for (i = 0; i < siz; i++) {
10621 tsn_map = asoc->mapping_array[i];
10622 if (type == SCTP_SELECTIVE_ACK) {
10623 tsn_map |= asoc->nr_mapping_array[i];
10627 * Clear all bits corresponding to TSNs
10628 * smaller or equal to the cumulative TSN.
10630 tsn_map &= (~0U << (1 - offset));
10632 selector = &sack_array[tsn_map];
10633 if (mergeable && selector->right_edge) {
10635 * Backup, left and right edges were ok to
10641 if (selector->num_entries == 0)
10644 for (j = 0; j < selector->num_entries; j++) {
10645 if (mergeable && selector->right_edge) {
10647 * do a merge by NOT setting
10653 * no merge, set the left
10657 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10659 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10662 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10668 if (selector->left_edge) {
10672 if (limit_reached) {
10673 /* Reached the limit stop */
10679 if ((type == SCTP_NR_SELECTIVE_ACK) &&
10680 (limit_reached == 0)) {
10683 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10684 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10686 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10689 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10692 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10694 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10695 /* we have a gap .. maybe */
10696 for (i = 0; i < siz; i++) {
10697 tsn_map = asoc->nr_mapping_array[i];
10700 * Clear all bits corresponding to
10701 * TSNs smaller or equal to the
10704 tsn_map &= (~0U << (1 - offset));
10706 selector = &sack_array[tsn_map];
10707 if (mergeable && selector->right_edge) {
10709 * Backup, left and right edges were
10712 num_nr_gap_blocks--;
10715 if (selector->num_entries == 0)
10718 for (j = 0; j < selector->num_entries; j++) {
10719 if (mergeable && selector->right_edge) {
10721 * do a merge by NOT
10728 * no merge, set the
10732 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10734 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10735 num_nr_gap_blocks++;
10737 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10743 if (selector->left_edge) {
10747 if (limit_reached) {
10748 /* Reached the limit stop */
10755 /* now we must add any dups we are going to report. */
10756 if ((limit_reached == 0) && (asoc->numduptsns)) {
10757 dup = (uint32_t *)gap_descriptor;
10758 for (i = 0; i < asoc->numduptsns; i++) {
10759 *dup = htonl(asoc->dup_tsns[i]);
10762 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10767 asoc->numduptsns = 0;
10770 * now that the chunk is prepared queue it to the control chunk
10773 if (type == SCTP_SELECTIVE_ACK) {
10774 a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
10775 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10776 num_dups * sizeof(int32_t));
10777 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10778 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10779 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10780 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10781 sack->sack.num_dup_tsns = htons(num_dups);
10782 sack->ch.chunk_type = type;
10783 sack->ch.chunk_flags = flags;
10784 sack->ch.chunk_length = htons(a_chk->send_size);
10786 a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
10787 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10788 num_dups * sizeof(int32_t));
10789 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10790 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10791 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10792 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10793 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10794 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10795 nr_sack->nr_sack.reserved = 0;
10796 nr_sack->ch.chunk_type = type;
10797 nr_sack->ch.chunk_flags = flags;
10798 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10800 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10801 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10802 asoc->ctrl_queue_cnt++;
10803 asoc->send_sack = 0;
10804 SCTP_STAT_INCR(sctps_sendsacks);
10809 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
10811 struct mbuf *m_abort, *m, *m_last;
10812 struct mbuf *m_out, *m_end = NULL;
10813 struct sctp_abort_chunk *abort;
10814 struct sctp_auth_chunk *auth = NULL;
10815 struct sctp_nets *net;
10817 uint32_t auth_offset = 0;
10819 uint16_t cause_len, chunk_len, padding_len;
10821 SCTP_TCB_LOCK_ASSERT(stcb);
10823 * Add an AUTH chunk, if chunk requires it and save the offset into
10824 * the chain for AUTH
10826 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10827 stcb->asoc.peer_auth_chunks)) {
10828 m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
10829 stcb, SCTP_ABORT_ASSOCIATION);
10830 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10834 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
10835 if (m_abort == NULL) {
10837 sctp_m_freem(m_out);
10840 sctp_m_freem(operr);
10844 /* link in any error */
10845 SCTP_BUF_NEXT(m_abort) = operr;
10848 for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
10849 cause_len += (uint16_t)SCTP_BUF_LEN(m);
10850 if (SCTP_BUF_NEXT(m) == NULL) {
10854 SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
10855 chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
10856 padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
10857 if (m_out == NULL) {
10858 /* NO Auth chunk prepended, so reserve space in front */
10859 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10862 /* Put AUTH chunk at the front of the chain */
10863 SCTP_BUF_NEXT(m_end) = m_abort;
10865 if (stcb->asoc.alternate) {
10866 net = stcb->asoc.alternate;
10868 net = stcb->asoc.primary_destination;
10870 /* Fill in the ABORT chunk header. */
10871 abort = mtod(m_abort, struct sctp_abort_chunk *);
10872 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10873 if (stcb->asoc.peer_vtag == 0) {
10874 /* This happens iff the assoc is in COOKIE-WAIT state. */
10875 vtag = stcb->asoc.my_vtag;
10876 abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
10878 vtag = stcb->asoc.peer_vtag;
10879 abort->ch.chunk_flags = 0;
10881 abort->ch.chunk_length = htons(chunk_len);
10882 /* Add padding, if necessary. */
10883 if (padding_len > 0) {
10884 if ((m_last == NULL) ||
10885 (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
10886 sctp_m_freem(m_out);
10890 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10891 (struct sockaddr *)&net->ro._l_addr,
10892 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10893 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
10894 stcb->asoc.primary_destination->port, NULL,
10897 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10898 if (error == ENOBUFS) {
10899 stcb->asoc.ifp_had_enobuf = 1;
10900 SCTP_STAT_INCR(sctps_lowlevelerr);
10903 stcb->asoc.ifp_had_enobuf = 0;
10905 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10909 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10910 struct sctp_nets *net,
10913 /* formulate and SEND a SHUTDOWN-COMPLETE */
10914 struct mbuf *m_shutdown_comp;
10915 struct sctp_shutdown_complete_chunk *shutdown_complete;
10920 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
10921 if (m_shutdown_comp == NULL) {
10925 if (reflect_vtag) {
10926 flags = SCTP_HAD_NO_TCB;
10927 vtag = stcb->asoc.my_vtag;
10930 vtag = stcb->asoc.peer_vtag;
10932 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10933 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10934 shutdown_complete->ch.chunk_flags = flags;
10935 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10936 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10937 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10938 (struct sockaddr *)&net->ro._l_addr,
10939 m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10940 stcb->sctp_ep->sctp_lport, stcb->rport,
10944 SCTP_SO_NOT_LOCKED))) {
10945 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10946 if (error == ENOBUFS) {
10947 stcb->asoc.ifp_had_enobuf = 1;
10948 SCTP_STAT_INCR(sctps_lowlevelerr);
10951 stcb->asoc.ifp_had_enobuf = 0;
10953 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10958 sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
10959 struct sctphdr *sh, uint32_t vtag,
10960 uint8_t type, struct mbuf *cause,
10961 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
10962 uint32_t vrf_id, uint16_t port)
10964 struct mbuf *o_pak;
10966 struct sctphdr *shout;
10967 struct sctp_chunkhdr *ch;
10968 #if defined(INET) || defined(INET6)
10969 struct udphdr *udp;
10971 int ret, len, cause_len, padding_len;
10973 struct sockaddr_in *src_sin, *dst_sin;
10977 struct sockaddr_in6 *src_sin6, *dst_sin6;
10978 struct ip6_hdr *ip6;
10981 /* Compute the length of the cause and add final padding. */
10983 if (cause != NULL) {
10984 struct mbuf *m_at, *m_last = NULL;
10986 for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
10987 if (SCTP_BUF_NEXT(m_at) == NULL)
10989 cause_len += SCTP_BUF_LEN(m_at);
10991 padding_len = cause_len % 4;
10992 if (padding_len != 0) {
10993 padding_len = 4 - padding_len;
10995 if (padding_len != 0) {
10996 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
10997 sctp_m_freem(cause);
11004 /* Get an mbuf for the header. */
11005 len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11006 switch (dst->sa_family) {
11009 len += sizeof(struct ip);
11014 len += sizeof(struct ip6_hdr);
11020 #if defined(INET) || defined(INET6)
11022 len += sizeof(struct udphdr);
11025 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11026 if (mout == NULL) {
11028 sctp_m_freem(cause);
11032 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11033 SCTP_BUF_LEN(mout) = len;
11034 SCTP_BUF_NEXT(mout) = cause;
11035 M_SETFIB(mout, fibnum);
11036 mout->m_pkthdr.flowid = mflowid;
11037 M_HASHTYPE_SET(mout, mflowtype);
11044 switch (dst->sa_family) {
11047 src_sin = (struct sockaddr_in *)src;
11048 dst_sin = (struct sockaddr_in *)dst;
11049 ip = mtod(mout, struct ip *);
11050 ip->ip_v = IPVERSION;
11051 ip->ip_hl = (sizeof(struct ip) >> 2);
11053 ip->ip_off = htons(IP_DF);
11055 ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11057 ip->ip_p = IPPROTO_UDP;
11059 ip->ip_p = IPPROTO_SCTP;
11061 ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11062 ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11064 len = sizeof(struct ip);
11065 shout = (struct sctphdr *)((caddr_t)ip + len);
11070 src_sin6 = (struct sockaddr_in6 *)src;
11071 dst_sin6 = (struct sockaddr_in6 *)dst;
11072 ip6 = mtod(mout, struct ip6_hdr *);
11073 ip6->ip6_flow = htonl(0x60000000);
11074 if (V_ip6_auto_flowlabel) {
11075 ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11077 ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11079 ip6->ip6_nxt = IPPROTO_UDP;
11081 ip6->ip6_nxt = IPPROTO_SCTP;
11083 ip6->ip6_src = dst_sin6->sin6_addr;
11084 ip6->ip6_dst = src_sin6->sin6_addr;
11085 len = sizeof(struct ip6_hdr);
11086 shout = (struct sctphdr *)((caddr_t)ip6 + len);
11091 shout = mtod(mout, struct sctphdr *);
11094 #if defined(INET) || defined(INET6)
11096 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11097 sctp_m_freem(mout);
11100 udp = (struct udphdr *)shout;
11101 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11102 udp->uh_dport = port;
11104 udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11105 sizeof(struct sctphdr) +
11106 sizeof(struct sctp_chunkhdr) +
11107 cause_len + padding_len));
11108 len += sizeof(struct udphdr);
11109 shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11114 shout->src_port = sh->dest_port;
11115 shout->dest_port = sh->src_port;
11116 shout->checksum = 0;
11118 shout->v_tag = htonl(vtag);
11120 shout->v_tag = sh->v_tag;
11122 len += sizeof(struct sctphdr);
11123 ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11124 ch->chunk_type = type;
11126 ch->chunk_flags = 0;
11128 ch->chunk_flags = SCTP_HAD_NO_TCB;
11130 ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11131 len += sizeof(struct sctp_chunkhdr);
11132 len += cause_len + padding_len;
11134 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11135 sctp_m_freem(mout);
11138 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11139 switch (dst->sa_family) {
11144 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11149 ip->ip_len = htons(len);
11151 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11152 SCTP_STAT_INCR(sctps_sendswcrc);
11154 SCTP_ENABLE_UDP_CSUM(o_pak);
11157 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11158 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11159 SCTP_STAT_INCR(sctps_sendhwcrc);
11161 #ifdef SCTP_PACKET_LOGGING
11162 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11163 sctp_packet_log(o_pak);
11166 SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
11167 SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
11172 ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
11174 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11175 SCTP_STAT_INCR(sctps_sendswcrc);
11176 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11177 udp->uh_sum = 0xffff;
11180 mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
11181 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11182 SCTP_STAT_INCR(sctps_sendhwcrc);
11184 #ifdef SCTP_PACKET_LOGGING
11185 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11186 sctp_packet_log(o_pak);
11189 SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
11190 SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
11194 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
11196 sctp_m_freem(mout);
11197 SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11200 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
11202 UDPSTAT_INC(udps_opackets);
11204 SCTP_STAT_INCR(sctps_sendpackets);
11205 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11206 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11208 SCTP_STAT_INCR(sctps_senderrors);
11214 sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
11215 struct sctphdr *sh,
11216 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11217 uint32_t vrf_id, uint16_t port)
11219 sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
11220 mflowtype, mflowid, fibnum,
11225 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked)
11227 struct sctp_tmit_chunk *chk;
11228 struct sctp_heartbeat_chunk *hb;
11229 struct timeval now;
11231 SCTP_TCB_LOCK_ASSERT(stcb);
11235 (void)SCTP_GETTIME_TIMEVAL(&now);
11236 switch (net->ro._l_addr.sa.sa_family) {
11248 sctp_alloc_a_chunk(stcb, chk);
11250 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11254 chk->copy_by_ref = 0;
11255 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11256 chk->rec.chunk_id.can_take_data = 1;
11258 chk->asoc = &stcb->asoc;
11259 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11261 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11262 if (chk->data == NULL) {
11263 sctp_free_a_chunk(stcb, chk, so_locked);
11266 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11267 SCTP_BUF_LEN(chk->data) = chk->send_size;
11268 chk->sent = SCTP_DATAGRAM_UNSENT;
11269 chk->snd_count = 0;
11271 atomic_add_int(&chk->whoTo->ref_count, 1);
11272 /* Now we have a mbuf that we can fill in with the details */
11273 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11274 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11275 /* fill out chunk header */
11276 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11277 hb->ch.chunk_flags = 0;
11278 hb->ch.chunk_length = htons(chk->send_size);
11279 /* Fill out hb parameter */
11280 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11281 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11282 hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec;
11283 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11284 /* Did our user request this one, put it in */
11285 hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
11286 hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11287 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11289 * we only take from the entropy pool if the address is not
11292 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11293 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11295 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11296 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11298 switch (net->ro._l_addr.sa.sa_family) {
11301 memcpy(hb->heartbeat.hb_info.address,
11302 &net->ro._l_addr.sin.sin_addr,
11303 sizeof(net->ro._l_addr.sin.sin_addr));
11308 memcpy(hb->heartbeat.hb_info.address,
11309 &net->ro._l_addr.sin6.sin6_addr,
11310 sizeof(net->ro._l_addr.sin6.sin6_addr));
11315 sctp_m_freem(chk->data);
11318 sctp_free_a_chunk(stcb, chk, so_locked);
11322 net->hb_responded = 0;
11323 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11324 stcb->asoc.ctrl_queue_cnt++;
11325 SCTP_STAT_INCR(sctps_sendheartbeat);
11330 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11333 struct sctp_association *asoc;
11334 struct sctp_ecne_chunk *ecne;
11335 struct sctp_tmit_chunk *chk;
11340 asoc = &stcb->asoc;
11341 SCTP_TCB_LOCK_ASSERT(stcb);
11342 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11343 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11344 /* found a previous ECN_ECHO update it if needed */
11345 uint32_t cnt, ctsn;
11347 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11348 ctsn = ntohl(ecne->tsn);
11349 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11350 ecne->tsn = htonl(high_tsn);
11351 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11353 cnt = ntohl(ecne->num_pkts_since_cwr);
11355 ecne->num_pkts_since_cwr = htonl(cnt);
11359 /* nope could not find one to update so we must build one */
11360 sctp_alloc_a_chunk(stcb, chk);
11364 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11365 chk->copy_by_ref = 0;
11366 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11367 chk->rec.chunk_id.can_take_data = 0;
11369 chk->asoc = &stcb->asoc;
11370 chk->send_size = sizeof(struct sctp_ecne_chunk);
11371 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11372 if (chk->data == NULL) {
11373 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11376 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11377 SCTP_BUF_LEN(chk->data) = chk->send_size;
11378 chk->sent = SCTP_DATAGRAM_UNSENT;
11379 chk->snd_count = 0;
11381 atomic_add_int(&chk->whoTo->ref_count, 1);
11383 stcb->asoc.ecn_echo_cnt_onq++;
11384 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11385 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11386 ecne->ch.chunk_flags = 0;
11387 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11388 ecne->tsn = htonl(high_tsn);
11389 ecne->num_pkts_since_cwr = htonl(1);
11390 TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11391 asoc->ctrl_queue_cnt++;
11395 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11396 struct mbuf *m, int len, int iphlen, int bad_crc)
11398 struct sctp_association *asoc;
11399 struct sctp_pktdrop_chunk *drp;
11400 struct sctp_tmit_chunk *chk;
11406 struct sctp_chunkhdr *ch, chunk_buf;
11407 unsigned int chk_length;
11412 asoc = &stcb->asoc;
11413 SCTP_TCB_LOCK_ASSERT(stcb);
11414 if (asoc->pktdrop_supported == 0) {
11416 * peer must declare support before I send one.
11420 if (stcb->sctp_socket == NULL) {
11423 sctp_alloc_a_chunk(stcb, chk);
11427 chk->copy_by_ref = 0;
11428 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11429 chk->rec.chunk_id.can_take_data = 1;
11432 chk->send_size = len;
11433 /* Validate that we do not have an ABORT in here. */
11434 offset = iphlen + sizeof(struct sctphdr);
11435 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11436 sizeof(*ch), (uint8_t *)&chunk_buf);
11437 while (ch != NULL) {
11438 chk_length = ntohs(ch->chunk_length);
11439 if (chk_length < sizeof(*ch)) {
11440 /* break to abort land */
11443 switch (ch->chunk_type) {
11444 case SCTP_PACKET_DROPPED:
11445 case SCTP_ABORT_ASSOCIATION:
11446 case SCTP_INITIATION_ACK:
11448 * We don't respond with an PKT-DROP to an ABORT
11449 * or PKT-DROP. We also do not respond to an
11450 * INIT-ACK, because we can't know if the initiation
11451 * tag is correct or not.
11453 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11458 offset += SCTP_SIZE32(chk_length);
11459 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11460 sizeof(*ch), (uint8_t *)&chunk_buf);
11463 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11464 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11466 * only send 1 mtu worth, trim off the excess on the end.
11469 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11472 chk->asoc = &stcb->asoc;
11473 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11474 if (chk->data == NULL) {
11476 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11479 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11480 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11482 sctp_m_freem(chk->data);
11486 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11487 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11488 chk->book_size_scale = 0;
11490 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11491 drp->trunc_len = htons(fullsz);
11493 * Len is already adjusted to size minus overhead above take
11494 * out the pkt_drop chunk itself from it.
11496 chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
11497 len = chk->send_size;
11499 /* no truncation needed */
11500 drp->ch.chunk_flags = 0;
11501 drp->trunc_len = htons(0);
11504 drp->ch.chunk_flags |= SCTP_BADCRC;
11506 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11507 SCTP_BUF_LEN(chk->data) = chk->send_size;
11508 chk->sent = SCTP_DATAGRAM_UNSENT;
11509 chk->snd_count = 0;
11511 /* we should hit here */
11513 atomic_add_int(&chk->whoTo->ref_count, 1);
11517 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11518 drp->ch.chunk_length = htons(chk->send_size);
11519 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11523 drp->bottle_bw = htonl(spc);
11524 if (asoc->my_rwnd) {
11525 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11526 asoc->size_on_all_streams +
11527 asoc->my_rwnd_control_len +
11528 SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
11531 * If my rwnd is 0, possibly from mbuf depletion as well as
11532 * space used, tell the peer there is NO space aka onq == bw
11534 drp->current_onq = htonl(spc);
11538 m_copydata(m, iphlen, len, (caddr_t)datap);
11539 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11540 asoc->ctrl_queue_cnt++;
11544 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11546 struct sctp_association *asoc;
11547 struct sctp_cwr_chunk *cwr;
11548 struct sctp_tmit_chunk *chk;
11550 SCTP_TCB_LOCK_ASSERT(stcb);
11554 asoc = &stcb->asoc;
11555 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11556 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11558 * found a previous CWR queued to same destination
11559 * update it if needed
11563 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11564 ctsn = ntohl(cwr->tsn);
11565 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11566 cwr->tsn = htonl(high_tsn);
11568 if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11569 /* Make sure override is carried */
11570 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11575 sctp_alloc_a_chunk(stcb, chk);
11579 chk->copy_by_ref = 0;
11580 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11581 chk->rec.chunk_id.can_take_data = 1;
11584 chk->send_size = sizeof(struct sctp_cwr_chunk);
11585 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11586 if (chk->data == NULL) {
11587 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11590 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11591 SCTP_BUF_LEN(chk->data) = chk->send_size;
11592 chk->sent = SCTP_DATAGRAM_UNSENT;
11593 chk->snd_count = 0;
11595 atomic_add_int(&chk->whoTo->ref_count, 1);
11596 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11597 cwr->ch.chunk_type = SCTP_ECN_CWR;
11598 cwr->ch.chunk_flags = override;
11599 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11600 cwr->tsn = htonl(high_tsn);
11601 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11602 asoc->ctrl_queue_cnt++;
11606 sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
11607 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11609 uint16_t len, old_len, i;
11610 struct sctp_stream_reset_out_request *req_out;
11611 struct sctp_chunkhdr *ch;
11613 int number_entries = 0;
11615 ch = mtod(chk->data, struct sctp_chunkhdr *);
11616 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11617 /* get to new offset for the param. */
11618 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11619 /* now how long will this param be? */
11620 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11621 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11622 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11623 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11627 if (number_entries == 0) {
11630 if (number_entries == stcb->asoc.streamoutcnt) {
11631 number_entries = 0;
11633 if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
11634 number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
11636 len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11637 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11638 req_out->ph.param_length = htons(len);
11639 req_out->request_seq = htonl(seq);
11640 req_out->response_seq = htonl(resp_seq);
11641 req_out->send_reset_at_tsn = htonl(last_sent);
11643 if (number_entries) {
11644 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11645 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11646 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11647 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11648 req_out->list_of_streams[at] = htons(i);
11650 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11651 if (at >= number_entries) {
11657 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11658 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11661 if (SCTP_SIZE32(len) > len) {
11663 * Need to worry about the pad we may end up adding to the
11664 * end. This is easy since the struct is either aligned to 4
11665 * bytes or 2 bytes off.
11667 req_out->list_of_streams[number_entries] = 0;
11669 /* now fix the chunk length */
11670 ch->chunk_length = htons(len + old_len);
11671 chk->book_size = len + old_len;
11672 chk->book_size_scale = 0;
11673 chk->send_size = SCTP_SIZE32(chk->book_size);
11674 SCTP_BUF_LEN(chk->data) = chk->send_size;
11679 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11680 int number_entries, uint16_t *list,
11683 uint16_t len, old_len, i;
11684 struct sctp_stream_reset_in_request *req_in;
11685 struct sctp_chunkhdr *ch;
11687 ch = mtod(chk->data, struct sctp_chunkhdr *);
11688 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11690 /* get to new offset for the param. */
11691 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11692 /* now how long will this param be? */
11693 len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11694 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11695 req_in->ph.param_length = htons(len);
11696 req_in->request_seq = htonl(seq);
11697 if (number_entries) {
11698 for (i = 0; i < number_entries; i++) {
11699 req_in->list_of_streams[i] = htons(list[i]);
11702 if (SCTP_SIZE32(len) > len) {
11704 * Need to worry about the pad we may end up adding to the
11705 * end. This is easy since the struct is either aligned to 4
11706 * bytes or 2 bytes off.
11708 req_in->list_of_streams[number_entries] = 0;
11710 /* now fix the chunk length */
11711 ch->chunk_length = htons(len + old_len);
11712 chk->book_size = len + old_len;
11713 chk->book_size_scale = 0;
11714 chk->send_size = SCTP_SIZE32(chk->book_size);
11715 SCTP_BUF_LEN(chk->data) = chk->send_size;
11720 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11723 uint16_t len, old_len;
11724 struct sctp_stream_reset_tsn_request *req_tsn;
11725 struct sctp_chunkhdr *ch;
11727 ch = mtod(chk->data, struct sctp_chunkhdr *);
11728 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11730 /* get to new offset for the param. */
11731 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11732 /* now how long will this param be? */
11733 len = sizeof(struct sctp_stream_reset_tsn_request);
11734 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11735 req_tsn->ph.param_length = htons(len);
11736 req_tsn->request_seq = htonl(seq);
11738 /* now fix the chunk length */
11739 ch->chunk_length = htons(len + old_len);
11740 chk->send_size = len + old_len;
11741 chk->book_size = SCTP_SIZE32(chk->send_size);
11742 chk->book_size_scale = 0;
11743 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11748 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11749 uint32_t resp_seq, uint32_t result)
11751 uint16_t len, old_len;
11752 struct sctp_stream_reset_response *resp;
11753 struct sctp_chunkhdr *ch;
11755 ch = mtod(chk->data, struct sctp_chunkhdr *);
11756 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11758 /* get to new offset for the param. */
11759 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11760 /* now how long will this param be? */
11761 len = sizeof(struct sctp_stream_reset_response);
11762 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11763 resp->ph.param_length = htons(len);
11764 resp->response_seq = htonl(resp_seq);
11765 resp->result = ntohl(result);
11767 /* now fix the chunk length */
11768 ch->chunk_length = htons(len + old_len);
11769 chk->book_size = len + old_len;
11770 chk->book_size_scale = 0;
11771 chk->send_size = SCTP_SIZE32(chk->book_size);
11772 SCTP_BUF_LEN(chk->data) = chk->send_size;
11777 sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
11778 struct sctp_stream_reset_list *ent,
11781 struct sctp_association *asoc;
11782 struct sctp_tmit_chunk *chk;
11783 struct sctp_chunkhdr *ch;
11785 asoc = &stcb->asoc;
11788 * Reset our last reset action to the new one IP -> response
11789 * (PERFORMED probably). This assures that if we fail to send, a
11790 * retran from the peer will get the new response.
11792 asoc->last_reset_action[0] = response;
11793 if (asoc->stream_reset_outstanding) {
11796 sctp_alloc_a_chunk(stcb, chk);
11798 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11801 chk->copy_by_ref = 0;
11802 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11803 chk->rec.chunk_id.can_take_data = 0;
11805 chk->asoc = &stcb->asoc;
11806 chk->book_size = sizeof(struct sctp_chunkhdr);
11807 chk->send_size = SCTP_SIZE32(chk->book_size);
11808 chk->book_size_scale = 0;
11809 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11810 if (chk->data == NULL) {
11811 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11812 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11815 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11816 /* setup chunk parameters */
11817 chk->sent = SCTP_DATAGRAM_UNSENT;
11818 chk->snd_count = 0;
11819 if (stcb->asoc.alternate) {
11820 chk->whoTo = stcb->asoc.alternate;
11822 chk->whoTo = stcb->asoc.primary_destination;
11824 ch = mtod(chk->data, struct sctp_chunkhdr *);
11825 ch->chunk_type = SCTP_STREAM_RESET;
11826 ch->chunk_flags = 0;
11827 ch->chunk_length = htons(chk->book_size);
11828 atomic_add_int(&chk->whoTo->ref_count, 1);
11829 SCTP_BUF_LEN(chk->data) = chk->send_size;
11830 sctp_add_stream_reset_result(chk, ent->seq, response);
11831 /* insert the chunk for sending */
11832 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11835 asoc->ctrl_queue_cnt++;
11839 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11840 uint32_t resp_seq, uint32_t result,
11841 uint32_t send_una, uint32_t recv_next)
11843 uint16_t len, old_len;
11844 struct sctp_stream_reset_response_tsn *resp;
11845 struct sctp_chunkhdr *ch;
11847 ch = mtod(chk->data, struct sctp_chunkhdr *);
11848 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11850 /* get to new offset for the param. */
11851 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11852 /* now how long will this param be? */
11853 len = sizeof(struct sctp_stream_reset_response_tsn);
11854 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11855 resp->ph.param_length = htons(len);
11856 resp->response_seq = htonl(resp_seq);
11857 resp->result = htonl(result);
11858 resp->senders_next_tsn = htonl(send_una);
11859 resp->receivers_next_tsn = htonl(recv_next);
11861 /* now fix the chunk length */
11862 ch->chunk_length = htons(len + old_len);
11863 chk->book_size = len + old_len;
11864 chk->send_size = SCTP_SIZE32(chk->book_size);
11865 chk->book_size_scale = 0;
11866 SCTP_BUF_LEN(chk->data) = chk->send_size;
11871 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11875 uint16_t len, old_len;
11876 struct sctp_chunkhdr *ch;
11877 struct sctp_stream_reset_add_strm *addstr;
11879 ch = mtod(chk->data, struct sctp_chunkhdr *);
11880 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11882 /* get to new offset for the param. */
11883 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11884 /* now how long will this param be? */
11885 len = sizeof(struct sctp_stream_reset_add_strm);
11888 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11889 addstr->ph.param_length = htons(len);
11890 addstr->request_seq = htonl(seq);
11891 addstr->number_of_streams = htons(adding);
11892 addstr->reserved = 0;
11894 /* now fix the chunk length */
11895 ch->chunk_length = htons(len + old_len);
11896 chk->send_size = len + old_len;
11897 chk->book_size = SCTP_SIZE32(chk->send_size);
11898 chk->book_size_scale = 0;
11899 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11904 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11908 uint16_t len, old_len;
11909 struct sctp_chunkhdr *ch;
11910 struct sctp_stream_reset_add_strm *addstr;
11912 ch = mtod(chk->data, struct sctp_chunkhdr *);
11913 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11915 /* get to new offset for the param. */
11916 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11917 /* now how long will this param be? */
11918 len = sizeof(struct sctp_stream_reset_add_strm);
11920 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11921 addstr->ph.param_length = htons(len);
11922 addstr->request_seq = htonl(seq);
11923 addstr->number_of_streams = htons(adding);
11924 addstr->reserved = 0;
11926 /* now fix the chunk length */
11927 ch->chunk_length = htons(len + old_len);
11928 chk->send_size = len + old_len;
11929 chk->book_size = SCTP_SIZE32(chk->send_size);
11930 chk->book_size_scale = 0;
11931 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11936 sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
11938 struct sctp_association *asoc;
11939 struct sctp_tmit_chunk *chk;
11940 struct sctp_chunkhdr *ch;
11943 asoc = &stcb->asoc;
11944 asoc->trigger_reset = 0;
11945 if (asoc->stream_reset_outstanding) {
11948 sctp_alloc_a_chunk(stcb, chk);
11950 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11953 chk->copy_by_ref = 0;
11954 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11955 chk->rec.chunk_id.can_take_data = 0;
11957 chk->asoc = &stcb->asoc;
11958 chk->book_size = sizeof(struct sctp_chunkhdr);
11959 chk->send_size = SCTP_SIZE32(chk->book_size);
11960 chk->book_size_scale = 0;
11961 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11962 if (chk->data == NULL) {
11963 sctp_free_a_chunk(stcb, chk, so_locked);
11964 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11967 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11969 /* setup chunk parameters */
11970 chk->sent = SCTP_DATAGRAM_UNSENT;
11971 chk->snd_count = 0;
11972 if (stcb->asoc.alternate) {
11973 chk->whoTo = stcb->asoc.alternate;
11975 chk->whoTo = stcb->asoc.primary_destination;
11977 ch = mtod(chk->data, struct sctp_chunkhdr *);
11978 ch->chunk_type = SCTP_STREAM_RESET;
11979 ch->chunk_flags = 0;
11980 ch->chunk_length = htons(chk->book_size);
11981 atomic_add_int(&chk->whoTo->ref_count, 1);
11982 SCTP_BUF_LEN(chk->data) = chk->send_size;
11983 seq = stcb->asoc.str_reset_seq_out;
11984 if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
11986 asoc->stream_reset_outstanding++;
11988 m_freem(chk->data);
11990 sctp_free_a_chunk(stcb, chk, so_locked);
11993 asoc->str_reset = chk;
11994 /* insert the chunk for sending */
11995 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11998 asoc->ctrl_queue_cnt++;
12000 if (stcb->asoc.send_sack) {
12001 sctp_send_sack(stcb, so_locked);
12003 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12008 sctp_send_str_reset_req(struct sctp_tcb *stcb,
12009 uint16_t number_entries, uint16_t *list,
12010 uint8_t send_in_req,
12011 uint8_t send_tsn_req,
12012 uint8_t add_stream,
12014 uint16_t adding_i, uint8_t peer_asked)
12016 struct sctp_association *asoc;
12017 struct sctp_tmit_chunk *chk;
12018 struct sctp_chunkhdr *ch;
12019 int can_send_out_req = 0;
12022 SCTP_TCB_LOCK_ASSERT(stcb);
12024 asoc = &stcb->asoc;
12025 if (asoc->stream_reset_outstanding) {
12027 * Already one pending, must get ACK back to clear the flag.
12029 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12032 if ((send_in_req == 0) && (send_tsn_req == 0) &&
12033 (add_stream == 0)) {
12034 /* nothing to do */
12035 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12038 if (send_tsn_req && send_in_req) {
12039 /* error, can't do that */
12040 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12042 } else if (send_in_req) {
12043 can_send_out_req = 1;
12045 if (number_entries > (MCLBYTES -
12046 SCTP_MIN_OVERHEAD -
12047 sizeof(struct sctp_chunkhdr) -
12048 sizeof(struct sctp_stream_reset_out_request)) /
12049 sizeof(uint16_t)) {
12050 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12053 sctp_alloc_a_chunk(stcb, chk);
12055 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12058 chk->copy_by_ref = 0;
12059 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12060 chk->rec.chunk_id.can_take_data = 0;
12062 chk->asoc = &stcb->asoc;
12063 chk->book_size = sizeof(struct sctp_chunkhdr);
12064 chk->send_size = SCTP_SIZE32(chk->book_size);
12065 chk->book_size_scale = 0;
12066 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12067 if (chk->data == NULL) {
12068 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12069 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12072 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12074 /* setup chunk parameters */
12075 chk->sent = SCTP_DATAGRAM_UNSENT;
12076 chk->snd_count = 0;
12077 if (stcb->asoc.alternate) {
12078 chk->whoTo = stcb->asoc.alternate;
12080 chk->whoTo = stcb->asoc.primary_destination;
12082 atomic_add_int(&chk->whoTo->ref_count, 1);
12083 ch = mtod(chk->data, struct sctp_chunkhdr *);
12084 ch->chunk_type = SCTP_STREAM_RESET;
12085 ch->chunk_flags = 0;
12086 ch->chunk_length = htons(chk->book_size);
12087 SCTP_BUF_LEN(chk->data) = chk->send_size;
12089 seq = stcb->asoc.str_reset_seq_out;
12090 if (can_send_out_req) {
12093 ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
12096 asoc->stream_reset_outstanding++;
12099 if ((add_stream & 1) &&
12100 ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
12101 /* Need to allocate more */
12102 struct sctp_stream_out *oldstream;
12103 struct sctp_stream_queue_pending *sp, *nsp;
12105 #if defined(SCTP_DETAILED_STR_STATS)
12109 oldstream = stcb->asoc.strmout;
12110 /* get some more */
12111 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
12112 (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
12114 if (stcb->asoc.strmout == NULL) {
12117 stcb->asoc.strmout = oldstream;
12118 /* Turn off the bit */
12119 x = add_stream & 0xfe;
12124 * Ok now we proceed with copying the old out stuff and
12125 * initializing the new stuff.
12127 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
12128 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12129 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12130 /* FIX ME FIX ME */
12132 * This should be a SS_COPY operation FIX ME STREAM
12135 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
12136 stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
12137 #if defined(SCTP_DETAILED_STR_STATS)
12138 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12139 stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
12140 stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
12143 stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
12144 stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
12146 stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
12147 stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
12148 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
12149 stcb->asoc.strmout[i].sid = i;
12150 stcb->asoc.strmout[i].state = oldstream[i].state;
12151 /* now anything on those queues? */
12152 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
12153 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
12154 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
12157 /* now the new streams */
12158 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
12159 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
12160 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12161 stcb->asoc.strmout[i].chunks_on_queues = 0;
12162 #if defined(SCTP_DETAILED_STR_STATS)
12163 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12164 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
12165 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
12168 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
12169 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
12171 stcb->asoc.strmout[i].next_mid_ordered = 0;
12172 stcb->asoc.strmout[i].next_mid_unordered = 0;
12173 stcb->asoc.strmout[i].sid = i;
12174 stcb->asoc.strmout[i].last_msg_incomplete = 0;
12175 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
12176 stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
12178 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
12179 SCTP_FREE(oldstream, SCTP_M_STRMO);
12182 if ((add_stream & 1) && (adding_o > 0)) {
12183 asoc->strm_pending_add_size = adding_o;
12184 asoc->peer_req_out = peer_asked;
12185 sctp_add_an_out_stream(chk, seq, adding_o);
12187 asoc->stream_reset_outstanding++;
12189 if ((add_stream & 2) && (adding_i > 0)) {
12190 sctp_add_an_in_stream(chk, seq, adding_i);
12192 asoc->stream_reset_outstanding++;
12195 sctp_add_stream_reset_in(chk, number_entries, list, seq);
12197 asoc->stream_reset_outstanding++;
12199 if (send_tsn_req) {
12200 sctp_add_stream_reset_tsn(chk, seq);
12201 asoc->stream_reset_outstanding++;
12203 asoc->str_reset = chk;
12204 /* insert the chunk for sending */
12205 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12208 asoc->ctrl_queue_cnt++;
12209 if (stcb->asoc.send_sack) {
12210 sctp_send_sack(stcb, SCTP_SO_LOCKED);
12212 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12217 sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
12218 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12219 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12220 uint32_t vrf_id, uint16_t port)
12222 /* Don't respond to an ABORT with an ABORT. */
12223 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
12225 sctp_m_freem(cause);
12228 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
12229 mflowtype, mflowid, fibnum,
12235 sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
12236 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12237 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12238 uint32_t vrf_id, uint16_t port)
12240 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
12241 mflowtype, mflowid, fibnum,
12246 static struct mbuf *
12247 sctp_copy_resume(struct uio *uio,
12249 int user_marks_eor,
12252 struct mbuf **new_tail)
12256 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12257 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12259 /* The only possible error is EFAULT. */
12260 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12263 *sndout = m_length(m, NULL);
12264 *new_tail = m_last(m);
12270 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12274 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
12275 if (sp->data == NULL) {
12276 /* The only possible error is EFAULT. */
12277 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12280 sp->tail_mbuf = m_last(sp->data);
12284 static struct sctp_stream_queue_pending *
12285 sctp_copy_it_in(struct sctp_tcb *stcb,
12286 struct sctp_association *asoc,
12287 struct sctp_nonpad_sndrcvinfo *srcv,
12289 struct sctp_nets *net,
12290 ssize_t max_send_len,
12291 int user_marks_eor,
12296 * This routine must be very careful in its work. Protocol
12297 * processing is up and running so care must be taken to spl...()
12298 * when you need to do something that may effect the stcb/asoc. The
12299 * sb is locked however. When data is copied the protocol processing
12300 * should be enabled since this is a slower operation...
12302 struct sctp_stream_queue_pending *sp;
12306 sctp_alloc_a_strmoq(stcb, sp);
12308 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12313 sp->sender_all_done = 0;
12314 sp->sinfo_flags = srcv->sinfo_flags;
12315 sp->timetolive = srcv->sinfo_timetolive;
12316 sp->ppid = srcv->sinfo_ppid;
12317 sp->context = srcv->sinfo_context;
12319 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12320 sp->sid = srcv->sinfo_stream;
12321 sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
12322 if ((sp->length == (uint32_t)uio->uio_resid) &&
12323 ((user_marks_eor == 0) ||
12324 (srcv->sinfo_flags & SCTP_EOF) ||
12325 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12326 sp->msg_is_complete = 1;
12328 sp->msg_is_complete = 0;
12330 sp->sender_all_done = 0;
12331 sp->some_taken = 0;
12332 sp->put_last_out = 0;
12333 resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
12334 sp->data = sp->tail_mbuf = NULL;
12335 if (sp->length == 0) {
12338 if (srcv->sinfo_keynumber_valid) {
12339 sp->auth_keyid = srcv->sinfo_keynumber;
12341 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12343 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12344 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12345 sp->holds_key_ref = 1;
12347 *error = sctp_copy_one(sp, uio, resv_in_first);
12350 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12353 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12355 atomic_add_int(&sp->net->ref_count, 1);
12359 sctp_set_prsctp_policy(sp);
12366 sctp_sosend(struct socket *so,
12367 struct sockaddr *addr,
12370 struct mbuf *control,
12374 struct sctp_sndrcvinfo sndrcvninfo;
12375 #if defined(INET) && defined(INET6)
12376 struct sockaddr_in sin;
12378 struct sockaddr *addr_to_use;
12382 if (control != NULL) {
12383 /* process cmsg snd/rcv info (maybe a assoc-id) */
12384 use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
12386 use_sndinfo = false;
12388 #if defined(INET) && defined(INET6)
12389 if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
12390 struct sockaddr_in6 *sin6;
12392 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12393 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12396 sin6 = (struct sockaddr_in6 *)addr;
12397 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12398 in6_sin6_2_sin(&sin, sin6);
12399 addr_to_use = (struct sockaddr *)&sin;
12401 addr_to_use = addr;
12404 addr_to_use = addr;
12407 addr_to_use = addr;
12409 error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
12410 use_sndinfo ? &sndrcvninfo : NULL, p);
12415 sctp_lower_sosend(struct socket *so,
12416 struct sockaddr *addr,
12419 struct mbuf *control,
12421 struct sctp_sndrcvinfo *srcv,
12424 struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
12425 struct epoch_tracker et;
12426 struct timeval now;
12427 struct sctp_block_entry be;
12428 struct sctp_inpcb *inp;
12429 struct sctp_tcb *stcb = NULL;
12430 struct sctp_nets *net;
12431 struct sctp_association *asoc;
12432 struct sctp_inpcb *t_inp;
12433 struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
12434 ssize_t sndlen = 0, max_len, local_add_more;
12435 ssize_t local_soresv = 0;
12436 sctp_assoc_t sinfo_assoc_id;
12437 int user_marks_eor;
12438 int nagle_applies = 0;
12440 int queue_only = 0, queue_only_for_init = 0;
12442 int now_filled = 0;
12443 unsigned int inqueue_bytes = 0;
12445 uint16_t sinfo_flags;
12446 uint16_t sinfo_stream;
12447 bool create_lock_applied = false;
12448 bool free_cnt_applied = false;
12449 bool some_on_control;
12450 bool got_all_of_the_send = false;
12451 bool non_blocking = false;
12457 if ((uio == NULL) && (top == NULL)) {
12461 if (addr != NULL) {
12462 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12464 switch (raddr->sa.sa_family) {
12467 if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12471 port = raddr->sin.sin_port;
12476 if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12480 port = raddr->sin6.sin6_port;
12484 error = EAFNOSUPPORT;
12491 if (uio->uio_resid < 0) {
12495 sndlen = uio->uio_resid;
12497 sndlen = SCTP_HEADER_LEN(top);
12499 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
12500 (void *)addr, sndlen);
12502 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12507 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12508 if ((uio == NULL) && (user_marks_eor != 0)) {
12510 * We do not support eeor mode for
12511 * sending with mbuf chains (like sendfile).
12516 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12517 SCTP_IS_LISTENING(inp)) {
12518 /* The listener can NOT send. */
12522 atomic_add_int(&inp->total_sends, 1);
12524 if (srcv != NULL) {
12525 sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
12526 sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
12527 sinfo_flags = sndrcvninfo->sinfo_flags;
12528 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12529 PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12533 if (sinfo_flags != 0) {
12534 SCTP_STAT_INCR(sctps_sends_with_flags);
12537 sndrcvninfo = NULL;
12538 sinfo_flags = inp->def_send.sinfo_flags;
12539 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12541 if (flags & MSG_EOR) {
12542 sinfo_flags |= SCTP_EOR;
12544 if (flags & MSG_EOF) {
12545 sinfo_flags |= SCTP_EOF;
12547 if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12551 SCTP_INP_RLOCK(inp);
12552 if ((sinfo_flags & SCTP_SENDALL) &&
12553 (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
12554 SCTP_INP_RUNLOCK(inp);
12555 error = sctp_sendall(inp, uio, top, sndrcvninfo);
12559 /* Now we must find the association. */
12560 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12561 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12562 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12563 if (stcb != NULL) {
12564 SCTP_TCB_LOCK(stcb);
12566 SCTP_INP_RUNLOCK(inp);
12567 } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
12568 stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
12569 SCTP_INP_RUNLOCK(inp);
12570 if (stcb != NULL) {
12571 SCTP_TCB_LOCK_ASSERT(stcb);
12573 } else if (addr != NULL) {
12575 * Since we did not use findep we must
12576 * increment it, and if we don't find a tcb
12579 SCTP_INP_INCR_REF(inp);
12580 SCTP_INP_RUNLOCK(inp);
12581 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12582 if (stcb == NULL) {
12583 SCTP_INP_WLOCK(inp);
12584 SCTP_INP_DECR_REF(inp);
12585 SCTP_INP_WUNLOCK(inp);
12587 SCTP_TCB_LOCK_ASSERT(stcb);
12590 SCTP_INP_RUNLOCK(inp);
12594 if (stcb != NULL) {
12595 SCTP_TCB_LOCK_ASSERT(stcb);
12599 if ((stcb == NULL) && (addr != NULL)) {
12600 /* Possible implicit send? */
12601 SCTP_ASOC_CREATE_LOCK(inp);
12602 create_lock_applied = true;
12603 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12604 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12608 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12609 (addr->sa_family == AF_INET6)) {
12613 SCTP_INP_WLOCK(inp);
12614 SCTP_INP_INCR_REF(inp);
12615 SCTP_INP_WUNLOCK(inp);
12616 /* With the lock applied look again */
12617 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12618 #if defined(INET) || defined(INET6)
12619 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12620 stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12623 if (stcb == NULL) {
12624 SCTP_INP_WLOCK(inp);
12625 SCTP_INP_DECR_REF(inp);
12626 SCTP_INP_WUNLOCK(inp);
12628 SCTP_TCB_LOCK_ASSERT(stcb);
12629 SCTP_ASOC_CREATE_UNLOCK(inp);
12630 create_lock_applied = false;
12635 if (t_inp != inp) {
12640 if (stcb == NULL) {
12641 if (addr == NULL) {
12645 /* We must go ahead and start the INIT process */
12648 if ((sinfo_flags & SCTP_ABORT) ||
12649 ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12651 * User asks to abort a non-existent assoc,
12652 * or EOF a non-existent assoc with no data
12657 /* get an asoc/stcb struct */
12658 vrf_id = inp->def_vrf_id;
12659 KASSERT(create_lock_applied, ("create_lock_applied is false"));
12660 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
12661 inp->sctp_ep.pre_open_stream_count,
12664 SCTP_INITIALIZE_AUTH_PARAMS);
12665 if (stcb == NULL) {
12666 /* error is setup for us in the call. */
12667 KASSERT(error != 0, ("error is 0 although stcb is NULL"));
12670 SCTP_TCB_LOCK_ASSERT(stcb);
12671 SCTP_ASOC_CREATE_UNLOCK(inp);
12672 create_lock_applied = false;
12674 * Turn on queue only flag to prevent data from
12678 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
12679 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
12680 if (control != NULL) {
12681 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12682 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
12683 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12685 KASSERT(error != 0,
12686 ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
12690 /* out with the INIT */
12691 queue_only_for_init = 1;
12693 * we may want to dig in after this call and adjust the MTU
12694 * value. It defaulted to 1500 (constant) but the ro
12695 * structure may now have an update and thus we may need to
12696 * change it BEFORE we append the message.
12701 KASSERT(!create_lock_applied, ("create_lock_applied is true"));
12702 KASSERT(stcb != NULL, ("stcb is NULL"));
12703 SCTP_TCB_LOCK_ASSERT(stcb);
12705 asoc = &stcb->asoc;
12706 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12707 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12708 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12709 /* XXX: Could also be ECONNABORTED, not enough info. */
12710 error = ECONNRESET;
12716 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12717 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12720 /* Keep the stcb from being freed under our feet. */
12721 atomic_add_int(&asoc->refcnt, 1);
12722 free_cnt_applied = true;
12723 if (sndrcvninfo == NULL) {
12724 /* Use a local copy to have a consistent view. */
12725 sndrcvninfo_buf = asoc->def_send;
12726 sndrcvninfo = &sndrcvninfo_buf;
12727 sinfo_flags = sndrcvninfo->sinfo_flags;
12728 if (flags & MSG_EOR) {
12729 sinfo_flags |= SCTP_EOR;
12731 if (flags & MSG_EOF) {
12732 sinfo_flags |= SCTP_EOF;
12735 /* Are we aborting? */
12736 if (sinfo_flags & SCTP_ABORT) {
12738 struct sctp_paramhdr *ph;
12739 ssize_t tot_demand, tot_out = 0, max_out;
12741 SCTP_STAT_INCR(sctps_sends_with_abort);
12742 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12743 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12744 /* It has to be up before we abort. */
12748 /* How big is the user initiated abort? */
12753 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12754 tot_out += SCTP_BUF_LEN(cntm);
12757 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
12759 /* Must fit in a MTU */
12761 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12762 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12766 mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
12772 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12773 max_out -= sizeof(struct sctp_abort_msg);
12774 if (tot_out > max_out) {
12777 ph = mtod(mm, struct sctp_paramhdr *);
12778 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12779 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
12781 SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
12783 SCTP_TCB_UNLOCK(stcb);
12784 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12785 SCTP_TCB_LOCK(stcb);
12786 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12787 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12789 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12791 * XXX: Could also be ECONNABORTED,
12794 error = ECONNRESET;
12802 * Here if we can't get his data we
12803 * still abort we just don't get to
12804 * send the users note :-0
12812 SCTP_BUF_NEXT(mm) = top;
12815 atomic_subtract_int(&asoc->refcnt, 1);
12816 free_cnt_applied = false;
12817 /* release this lock, otherwise we hang on ourselves */
12818 NET_EPOCH_ENTER(et);
12819 sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
12820 NET_EPOCH_EXIT(et);
12823 * In this case top is already chained to mm avoid double
12824 * free, since we free it below if top != NULL and driver
12825 * would free it after sending the packet out
12833 KASSERT(stcb != NULL, ("stcb is NULL"));
12834 SCTP_TCB_LOCK_ASSERT(stcb);
12835 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12836 ("Association about to be freed"));
12837 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12838 ("Association was aborted"));
12840 if (sinfo_flags & SCTP_ADDR_OVER) {
12841 if (addr != NULL) {
12842 net = sctp_findnet(stcb, addr);
12846 if ((net == NULL) ||
12847 ((port != 0) && (port != stcb->rport))) {
12852 if (asoc->alternate != NULL) {
12853 net = asoc->alternate;
12855 net = asoc->primary_destination;
12859 if (sinfo_flags & SCTP_EOF) {
12860 got_all_of_the_send = true;
12867 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12868 if (sndlen > (ssize_t)asoc->smallest_mtu) {
12873 sinfo_stream = sndrcvninfo->sinfo_stream;
12874 /* Is the stream no. valid? */
12875 if (sinfo_stream >= asoc->streamoutcnt) {
12876 /* Invalid stream number */
12880 if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
12881 (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
12883 * Can't queue any data while stream reset is underway.
12885 if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
12892 atomic_add_int(&stcb->total_sends, 1);
12893 if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
12894 non_blocking = true;
12896 if (non_blocking) {
12899 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12900 if (user_marks_eor == 0) {
12905 if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
12906 (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12907 if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
12908 (user_marks_eor == 0)) {
12911 error = EWOULDBLOCK;
12916 atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
12917 local_soresv = sndlen;
12919 KASSERT(stcb != NULL, ("stcb is NULL"));
12920 SCTP_TCB_LOCK_ASSERT(stcb);
12921 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12922 ("Association about to be freed"));
12923 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12924 ("Association was aborted"));
12926 /* Ok, we will attempt a msgsnd :> */
12928 p->td_ru.ru_msgsnd++;
12930 /* Calculate the maximum we can send */
12931 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12932 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12933 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12937 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12938 if ((user_marks_eor == 0) &&
12939 (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12940 /* It will NEVER fit. */
12944 if (user_marks_eor != 0) {
12945 local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12948 * For non-eeor the whole message must fit in
12949 * the socket send buffer.
12951 local_add_more = sndlen;
12953 if (non_blocking) {
12954 goto skip_preblock;
12956 if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12958 ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12959 /* No room right now! */
12960 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12961 SOCKBUF_LOCK(&so->so_snd);
12962 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12963 ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12964 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
12965 (unsigned int)SCTP_SB_LIMIT_SND(so),
12968 asoc->stream_queue_cnt,
12969 asoc->chunks_on_out_queue,
12970 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12971 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12972 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
12975 stcb->block_entry = &be;
12976 SCTP_TCB_UNLOCK(stcb);
12977 error = sbwait(&so->so_snd);
12979 if (so->so_error != 0) {
12980 error = so->so_error;
12982 if (be.error != 0) {
12986 SOCKBUF_UNLOCK(&so->so_snd);
12987 SCTP_TCB_LOCK(stcb);
12988 stcb->block_entry = NULL;
12992 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12993 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12994 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12996 * XXX: Could also be ECONNABORTED,
12999 error = ECONNRESET;
13005 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13006 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13007 asoc, asoc->total_output_queue_size);
13009 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13010 SOCKBUF_LOCK(&so->so_snd);
13012 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13013 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13017 SOCKBUF_UNLOCK(&so->so_snd);
13021 KASSERT(stcb != NULL, ("stcb is NULL"));
13022 SCTP_TCB_LOCK_ASSERT(stcb);
13023 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13024 ("Association about to be freed"));
13025 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13026 ("Association was aborted"));
13029 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13030 * case NOTE: uio will be null when top/mbuf is passed
13033 struct sctp_stream_queue_pending *sp;
13034 struct sctp_stream_out *strm;
13037 if ((asoc->stream_locked) &&
13038 (asoc->stream_locked_on != sinfo_stream)) {
13042 strm = &asoc->strmout[sinfo_stream];
13043 if (strm->last_msg_incomplete == 0) {
13045 SCTP_TCB_UNLOCK(stcb);
13046 sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
13047 SCTP_TCB_LOCK(stcb);
13048 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13049 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13050 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13052 * XXX: Could also be ECONNABORTED,
13055 error = ECONNRESET;
13065 * Reject the sending of a new user message, if the
13066 * association is about to be shut down.
13068 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
13069 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13070 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13071 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13072 if (sp->data != 0) {
13073 sctp_m_freem(sp->data);
13075 sp->tail_mbuf = NULL;
13078 if (sp->net != NULL) {
13079 sctp_free_remote_addr(sp->net);
13082 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13086 /* The out streams might be reallocated. */
13087 strm = &asoc->strmout[sinfo_stream];
13088 if (sp->msg_is_complete) {
13089 strm->last_msg_incomplete = 0;
13090 asoc->stream_locked = 0;
13093 * Just got locked to this guy in case of an
13096 strm->last_msg_incomplete = 1;
13097 if (asoc->idata_supported == 0) {
13098 asoc->stream_locked = 1;
13099 asoc->stream_locked_on = sinfo_stream;
13101 sp->sender_all_done = 0;
13103 sctp_snd_sb_alloc(stcb, sp->length);
13104 atomic_add_int(&asoc->stream_queue_cnt, 1);
13105 if (sinfo_flags & SCTP_UNORDERED) {
13106 SCTP_STAT_INCR(sctps_sends_with_unord);
13108 sp->processing = 1;
13109 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13110 asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
13112 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13114 /* ???? Huh ??? last msg is gone */
13116 panic("Warning: Last msg marked incomplete, yet nothing left?");
13118 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13119 strm->last_msg_incomplete = 0;
13123 if (sp->processing != 0) {
13127 sp->processing = 1;
13131 KASSERT(stcb != NULL, ("stcb is NULL"));
13132 SCTP_TCB_LOCK_ASSERT(stcb);
13133 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13134 ("Association about to be freed"));
13135 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13136 ("Association was aborted"));
13138 while (uio->uio_resid > 0) {
13139 /* How much room do we have? */
13140 struct mbuf *new_tail, *mm;
13142 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13143 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13144 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13148 if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13149 ((max_len > 0) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13150 (uio->uio_resid <= max_len)) {
13151 SCTP_TCB_UNLOCK(stcb);
13154 mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
13155 SCTP_TCB_LOCK(stcb);
13156 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13157 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13159 * We need to get out. Peer probably
13163 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13165 * XXX: Could also be
13166 * ECONNABORTED, not enough
13169 error = ECONNRESET;
13175 if ((mm == NULL) || (error != 0)) {
13180 sp->processing = 0;
13184 /* Update the mbuf and count */
13185 if (sp->tail_mbuf != NULL) {
13186 /* Tack it to the end. */
13187 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13189 /* A stolen mbuf. */
13192 sp->tail_mbuf = new_tail;
13193 sctp_snd_sb_alloc(stcb, sndout);
13194 atomic_add_int(&sp->length, sndout);
13195 if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
13196 sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
13199 /* Did we reach EOR? */
13200 if ((uio->uio_resid == 0) &&
13201 ((user_marks_eor == 0) ||
13202 (sinfo_flags & SCTP_EOF) ||
13203 (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
13204 sp->msg_is_complete = 1;
13206 sp->msg_is_complete = 0;
13210 KASSERT(stcb != NULL, ("stcb is NULL"));
13211 SCTP_TCB_LOCK_ASSERT(stcb);
13212 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13213 ("Association about to be freed"));
13214 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13215 ("Association was aborted"));
13217 if (uio->uio_resid == 0) {
13222 if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
13224 * This is ugly but we must assure locking
13227 sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
13228 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13229 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
13230 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13237 /* wait for space now */
13238 if (non_blocking) {
13239 /* Non-blocking io in place out */
13241 sp->processing = 0;
13245 /* What about the INIT, send it maybe */
13246 if (queue_only_for_init) {
13247 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13248 /* a collision took us forward? */
13251 NET_EPOCH_ENTER(et);
13252 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13253 NET_EPOCH_EXIT(et);
13254 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13258 if ((net->flight_size > net->cwnd) &&
13259 (asoc->sctp_cmt_on_off == 0)) {
13260 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13262 } else if (asoc->ifp_had_enobuf) {
13263 SCTP_STAT_INCR(sctps_ifnomemqueued);
13264 if (net->flight_size > (2 * net->mtu)) {
13267 asoc->ifp_had_enobuf = 0;
13269 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13270 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13271 (asoc->total_flight > 0) &&
13272 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13273 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13275 * Ok, Nagle is set on and we have data outstanding.
13276 * Don't send anything and let SACKs drive out the
13277 * data unless we have a "full" segment to send.
13279 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13280 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13282 SCTP_STAT_INCR(sctps_naglequeued);
13285 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13286 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13287 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13289 SCTP_STAT_INCR(sctps_naglesent);
13292 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13293 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13294 nagle_applies, un_sent);
13295 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13296 asoc->total_flight,
13297 asoc->chunks_on_out_queue, asoc->total_flight_count);
13299 if (queue_only_for_init) {
13300 queue_only_for_init = 0;
13302 if ((queue_only == 0) && (nagle_applies == 0)) {
13304 * need to start chunk output
13305 * before blocking.. note that if
13306 * a lock is already applied, then
13307 * the input via the net is happening
13308 * and I don't need to start output :-D
13310 NET_EPOCH_ENTER(et);
13311 sctp_chunk_output(inp, stcb,
13312 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13313 NET_EPOCH_EXIT(et);
13316 * This is a bit strange, but I think it will
13317 * work. The total_output_queue_size is locked and
13318 * protected by the TCB_LOCK, which we just released.
13319 * There is a race that can occur between releasing it
13320 * above, and me getting the socket lock, where sacks
13321 * come in but we have not put the SB_WAIT on the
13322 * so_snd buffer to get the wakeup. After the LOCK
13323 * is applied the sack_processing will also need to
13324 * LOCK the so->so_snd to do the actual sowwakeup(). So
13325 * once we have the socket buffer lock if we recheck the
13326 * size we KNOW we will get to sleep safely with the
13327 * wakeup flag in place.
13329 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13330 SOCKBUF_LOCK(&so->so_snd);
13331 if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
13332 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13333 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13334 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13335 asoc, uio->uio_resid);
13338 stcb->block_entry = &be;
13339 SCTP_TCB_UNLOCK(stcb);
13340 error = sbwait(&so->so_snd);
13342 if (so->so_error != 0)
13343 error = so->so_error;
13344 if (be.error != 0) {
13348 SOCKBUF_UNLOCK(&so->so_snd);
13349 SCTP_TCB_LOCK(stcb);
13350 stcb->block_entry = NULL;
13351 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13352 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13353 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13355 * XXX: Could also be
13356 * ECONNABORTED, not enough
13359 error = ECONNRESET;
13367 sp->processing = 0;
13372 SOCKBUF_UNLOCK(&so->so_snd);
13374 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13375 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13376 asoc, asoc->total_output_queue_size);
13380 KASSERT(stcb != NULL, ("stcb is NULL"));
13381 SCTP_TCB_LOCK_ASSERT(stcb);
13382 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13383 ("Association about to be freed"));
13384 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13385 ("Association was aborted"));
13387 /* The out streams might be reallocated. */
13388 strm = &asoc->strmout[sinfo_stream];
13390 if (sp->msg_is_complete == 0) {
13391 strm->last_msg_incomplete = 1;
13392 if (asoc->idata_supported == 0) {
13393 asoc->stream_locked = 1;
13394 asoc->stream_locked_on = sinfo_stream;
13397 sp->sender_all_done = 1;
13398 strm->last_msg_incomplete = 0;
13399 asoc->stream_locked = 0;
13401 sp->processing = 0;
13403 SCTP_PRINTF("Huh no sp TSNH?\n");
13404 strm->last_msg_incomplete = 0;
13405 asoc->stream_locked = 0;
13407 if (uio->uio_resid == 0) {
13408 got_all_of_the_send = true;
13411 error = sctp_msg_append(stcb, net, top, sndrcvninfo);
13413 if ((sinfo_flags & SCTP_EOF) != 0) {
13414 got_all_of_the_send = true;
13422 KASSERT(stcb != NULL, ("stcb is NULL"));
13423 SCTP_TCB_LOCK_ASSERT(stcb);
13424 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13425 ("Association about to be freed"));
13426 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13427 ("Association was aborted"));
13430 if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
13431 SCTP_STAT_INCR(sctps_sends_with_eof);
13433 if (TAILQ_EMPTY(&asoc->send_queue) &&
13434 TAILQ_EMPTY(&asoc->sent_queue) &&
13435 sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
13436 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13439 /* there is nothing queued to send, so I'm done... */
13440 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13441 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13442 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13443 struct sctp_nets *netp;
13445 /* only send SHUTDOWN the first time through */
13446 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13447 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13449 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
13450 sctp_stop_timers_for_shutdown(stcb);
13451 if (asoc->alternate != NULL) {
13452 netp = asoc->alternate;
13454 netp = asoc->primary_destination;
13456 sctp_send_shutdown(stcb, netp);
13457 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13459 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13464 * we still got (or just got) data to send, so set
13468 * XXX sockets draft says that SCTP_EOF should be
13469 * sent with no data. currently, we will allow user
13470 * data to be sent first and move to
13473 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13474 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13475 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13476 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13477 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
13479 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
13480 if (TAILQ_EMPTY(&asoc->send_queue) &&
13481 TAILQ_EMPTY(&asoc->sent_queue) &&
13482 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13483 struct mbuf *op_err;
13484 char msg[SCTP_DIAG_INFO_LEN];
13487 if (free_cnt_applied) {
13488 atomic_subtract_int(&asoc->refcnt, 1);
13489 free_cnt_applied = false;
13491 SCTP_SNPRINTF(msg, sizeof(msg),
13492 "%s:%d at %s", __FILE__, __LINE__, __func__);
13493 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
13495 NET_EPOCH_ENTER(et);
13496 sctp_abort_an_association(stcb->sctp_ep, stcb,
13497 op_err, false, SCTP_SO_LOCKED);
13498 NET_EPOCH_EXIT(et);
13500 error = ECONNABORTED;
13503 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13509 KASSERT(stcb != NULL, ("stcb is NULL"));
13510 SCTP_TCB_LOCK_ASSERT(stcb);
13511 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13512 ("Association about to be freed"));
13513 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13514 ("Association was aborted"));
13516 some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
13517 if (queue_only_for_init) {
13518 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13519 /* a collision took us forward? */
13522 NET_EPOCH_ENTER(et);
13523 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13524 NET_EPOCH_EXIT(et);
13525 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13530 KASSERT(stcb != NULL, ("stcb is NULL"));
13531 SCTP_TCB_LOCK_ASSERT(stcb);
13532 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13533 ("Association about to be freed"));
13534 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13535 ("Association was aborted"));
13537 if ((net->flight_size > net->cwnd) &&
13538 (asoc->sctp_cmt_on_off == 0)) {
13539 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13541 } else if (asoc->ifp_had_enobuf) {
13542 SCTP_STAT_INCR(sctps_ifnomemqueued);
13543 if (net->flight_size > (2 * net->mtu)) {
13546 asoc->ifp_had_enobuf = 0;
13548 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13549 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13550 (asoc->total_flight > 0) &&
13551 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13552 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13554 * Ok, Nagle is set on and we have data outstanding.
13555 * Don't send anything and let SACKs drive out the
13556 * data unless wen have a "full" segment to send.
13558 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13559 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13561 SCTP_STAT_INCR(sctps_naglequeued);
13564 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13565 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13566 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13568 SCTP_STAT_INCR(sctps_naglesent);
13571 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13572 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13573 nagle_applies, un_sent);
13574 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13575 asoc->total_flight,
13576 asoc->chunks_on_out_queue, asoc->total_flight_count);
13579 KASSERT(stcb != NULL, ("stcb is NULL"));
13580 SCTP_TCB_LOCK_ASSERT(stcb);
13581 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13582 ("Association about to be freed"));
13583 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13584 ("Association was aborted"));
13586 NET_EPOCH_ENTER(et);
13587 if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
13588 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13589 } else if ((queue_only == 0) &&
13590 (asoc->peers_rwnd == 0) &&
13591 (asoc->total_flight == 0)) {
13592 /* We get to have a probe outstanding */
13593 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13594 } else if (some_on_control) {
13595 int num_out, reason;
13597 /* Here we do control only */
13598 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
13599 &reason, 1, 1, &now, &now_filled,
13600 sctp_get_frag_point(stcb),
13603 NET_EPOCH_EXIT(et);
13604 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13605 queue_only, asoc->peers_rwnd, un_sent,
13606 asoc->total_flight, asoc->chunks_on_out_queue,
13607 asoc->total_output_queue_size, error);
13609 KASSERT(stcb != NULL, ("stcb is NULL"));
13610 SCTP_TCB_LOCK_ASSERT(stcb);
13611 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13612 ("Association about to be freed"));
13613 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13614 ("Association was aborted"));
13618 if (create_lock_applied) {
13619 SCTP_ASOC_CREATE_UNLOCK(inp);
13621 if (stcb != NULL) {
13622 if (local_soresv) {
13623 atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
13625 if (free_cnt_applied) {
13626 atomic_subtract_int(&asoc->refcnt, 1);
13628 SCTP_TCB_UNLOCK(stcb);
13633 if (control != NULL) {
13634 sctp_m_freem(control);
13636 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
13641 * generate an AUTHentication chunk, if required
13644 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13645 struct sctp_auth_chunk **auth_ret, uint32_t *offset,
13646 struct sctp_tcb *stcb, uint8_t chunk)
13648 struct mbuf *m_auth;
13649 struct sctp_auth_chunk *auth;
13653 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13657 if (stcb->asoc.auth_supported == 0) {
13660 /* does the requested chunk require auth? */
13661 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13664 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
13665 if (m_auth == NULL) {
13669 /* reserve some space if this will be the first mbuf */
13671 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13672 /* fill in the AUTH chunk details */
13673 auth = mtod(m_auth, struct sctp_auth_chunk *);
13674 memset(auth, 0, sizeof(*auth));
13675 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13676 auth->ch.chunk_flags = 0;
13677 chunk_len = sizeof(*auth) +
13678 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13679 auth->ch.chunk_length = htons(chunk_len);
13680 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13681 /* key id and hmac digest will be computed and filled in upon send */
13683 /* save the offset where the auth was inserted into the chain */
13685 for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13686 *offset += SCTP_BUF_LEN(cn);
13689 /* update length and return pointer to the auth chunk */
13690 SCTP_BUF_LEN(m_auth) = chunk_len;
13691 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13692 if (auth_ret != NULL)
13700 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
13702 struct nd_prefix *pfx = NULL;
13703 struct nd_pfxrouter *pfxrtr = NULL;
13704 struct sockaddr_in6 gw6;
13706 if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
13709 /* get prefix entry of address */
13711 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13712 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13714 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13715 &src6->sin6_addr, &pfx->ndpr_mask))
13718 /* no prefix entry in the prefix list */
13721 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13722 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13726 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13727 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13729 /* search installed gateway from prefix entry */
13730 LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13731 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13732 gw6.sin6_family = AF_INET6;
13733 gw6.sin6_len = sizeof(struct sockaddr_in6);
13734 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13735 sizeof(struct in6_addr));
13736 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13737 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13738 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13739 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13740 if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
13742 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13747 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13753 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
13756 struct sockaddr_in *sin, *mask;
13757 struct ifaddr *ifa;
13758 struct in_addr srcnetaddr, gwnetaddr;
13760 if (ro == NULL || ro->ro_nh == NULL ||
13761 sifa->address.sa.sa_family != AF_INET) {
13764 ifa = (struct ifaddr *)sifa->ifa;
13765 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13766 sin = &sifa->address.sin;
13767 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13768 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13769 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13770 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13772 sin = &ro->ro_nh->gw4_sa;
13773 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13774 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13775 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13776 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13777 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {