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 <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_uio.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_auth.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_input.h>
51 #include <netinet/sctp_crc32.h>
52 #include <netinet/sctp_kdtrace.h>
53 #if defined(INET) || defined(INET6)
54 #include <netinet/udp.h>
56 #include <netinet/udp_var.h>
57 #include <machine/in_cksum.h>
59 #define SCTP_MAX_GAPS_INARRAY 4
61 uint8_t right_edge; /* mergable on the right edge */
62 uint8_t left_edge; /* mergable on the left edge */
65 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
68 const struct sack_track sack_array[256] = {
69 {0, 0, 0, 0, /* 0x00 */
76 {1, 0, 1, 0, /* 0x01 */
83 {0, 0, 1, 0, /* 0x02 */
90 {1, 0, 1, 0, /* 0x03 */
97 {0, 0, 1, 0, /* 0x04 */
104 {1, 0, 2, 0, /* 0x05 */
111 {0, 0, 1, 0, /* 0x06 */
118 {1, 0, 1, 0, /* 0x07 */
125 {0, 0, 1, 0, /* 0x08 */
132 {1, 0, 2, 0, /* 0x09 */
139 {0, 0, 2, 0, /* 0x0a */
146 {1, 0, 2, 0, /* 0x0b */
153 {0, 0, 1, 0, /* 0x0c */
160 {1, 0, 2, 0, /* 0x0d */
167 {0, 0, 1, 0, /* 0x0e */
174 {1, 0, 1, 0, /* 0x0f */
181 {0, 0, 1, 0, /* 0x10 */
188 {1, 0, 2, 0, /* 0x11 */
195 {0, 0, 2, 0, /* 0x12 */
202 {1, 0, 2, 0, /* 0x13 */
209 {0, 0, 2, 0, /* 0x14 */
216 {1, 0, 3, 0, /* 0x15 */
223 {0, 0, 2, 0, /* 0x16 */
230 {1, 0, 2, 0, /* 0x17 */
237 {0, 0, 1, 0, /* 0x18 */
244 {1, 0, 2, 0, /* 0x19 */
251 {0, 0, 2, 0, /* 0x1a */
258 {1, 0, 2, 0, /* 0x1b */
265 {0, 0, 1, 0, /* 0x1c */
272 {1, 0, 2, 0, /* 0x1d */
279 {0, 0, 1, 0, /* 0x1e */
286 {1, 0, 1, 0, /* 0x1f */
293 {0, 0, 1, 0, /* 0x20 */
300 {1, 0, 2, 0, /* 0x21 */
307 {0, 0, 2, 0, /* 0x22 */
314 {1, 0, 2, 0, /* 0x23 */
321 {0, 0, 2, 0, /* 0x24 */
328 {1, 0, 3, 0, /* 0x25 */
335 {0, 0, 2, 0, /* 0x26 */
342 {1, 0, 2, 0, /* 0x27 */
349 {0, 0, 2, 0, /* 0x28 */
356 {1, 0, 3, 0, /* 0x29 */
363 {0, 0, 3, 0, /* 0x2a */
370 {1, 0, 3, 0, /* 0x2b */
377 {0, 0, 2, 0, /* 0x2c */
384 {1, 0, 3, 0, /* 0x2d */
391 {0, 0, 2, 0, /* 0x2e */
398 {1, 0, 2, 0, /* 0x2f */
405 {0, 0, 1, 0, /* 0x30 */
412 {1, 0, 2, 0, /* 0x31 */
419 {0, 0, 2, 0, /* 0x32 */
426 {1, 0, 2, 0, /* 0x33 */
433 {0, 0, 2, 0, /* 0x34 */
440 {1, 0, 3, 0, /* 0x35 */
447 {0, 0, 2, 0, /* 0x36 */
454 {1, 0, 2, 0, /* 0x37 */
461 {0, 0, 1, 0, /* 0x38 */
468 {1, 0, 2, 0, /* 0x39 */
475 {0, 0, 2, 0, /* 0x3a */
482 {1, 0, 2, 0, /* 0x3b */
489 {0, 0, 1, 0, /* 0x3c */
496 {1, 0, 2, 0, /* 0x3d */
503 {0, 0, 1, 0, /* 0x3e */
510 {1, 0, 1, 0, /* 0x3f */
517 {0, 0, 1, 0, /* 0x40 */
524 {1, 0, 2, 0, /* 0x41 */
531 {0, 0, 2, 0, /* 0x42 */
538 {1, 0, 2, 0, /* 0x43 */
545 {0, 0, 2, 0, /* 0x44 */
552 {1, 0, 3, 0, /* 0x45 */
559 {0, 0, 2, 0, /* 0x46 */
566 {1, 0, 2, 0, /* 0x47 */
573 {0, 0, 2, 0, /* 0x48 */
580 {1, 0, 3, 0, /* 0x49 */
587 {0, 0, 3, 0, /* 0x4a */
594 {1, 0, 3, 0, /* 0x4b */
601 {0, 0, 2, 0, /* 0x4c */
608 {1, 0, 3, 0, /* 0x4d */
615 {0, 0, 2, 0, /* 0x4e */
622 {1, 0, 2, 0, /* 0x4f */
629 {0, 0, 2, 0, /* 0x50 */
636 {1, 0, 3, 0, /* 0x51 */
643 {0, 0, 3, 0, /* 0x52 */
650 {1, 0, 3, 0, /* 0x53 */
657 {0, 0, 3, 0, /* 0x54 */
664 {1, 0, 4, 0, /* 0x55 */
671 {0, 0, 3, 0, /* 0x56 */
678 {1, 0, 3, 0, /* 0x57 */
685 {0, 0, 2, 0, /* 0x58 */
692 {1, 0, 3, 0, /* 0x59 */
699 {0, 0, 3, 0, /* 0x5a */
706 {1, 0, 3, 0, /* 0x5b */
713 {0, 0, 2, 0, /* 0x5c */
720 {1, 0, 3, 0, /* 0x5d */
727 {0, 0, 2, 0, /* 0x5e */
734 {1, 0, 2, 0, /* 0x5f */
741 {0, 0, 1, 0, /* 0x60 */
748 {1, 0, 2, 0, /* 0x61 */
755 {0, 0, 2, 0, /* 0x62 */
762 {1, 0, 2, 0, /* 0x63 */
769 {0, 0, 2, 0, /* 0x64 */
776 {1, 0, 3, 0, /* 0x65 */
783 {0, 0, 2, 0, /* 0x66 */
790 {1, 0, 2, 0, /* 0x67 */
797 {0, 0, 2, 0, /* 0x68 */
804 {1, 0, 3, 0, /* 0x69 */
811 {0, 0, 3, 0, /* 0x6a */
818 {1, 0, 3, 0, /* 0x6b */
825 {0, 0, 2, 0, /* 0x6c */
832 {1, 0, 3, 0, /* 0x6d */
839 {0, 0, 2, 0, /* 0x6e */
846 {1, 0, 2, 0, /* 0x6f */
853 {0, 0, 1, 0, /* 0x70 */
860 {1, 0, 2, 0, /* 0x71 */
867 {0, 0, 2, 0, /* 0x72 */
874 {1, 0, 2, 0, /* 0x73 */
881 {0, 0, 2, 0, /* 0x74 */
888 {1, 0, 3, 0, /* 0x75 */
895 {0, 0, 2, 0, /* 0x76 */
902 {1, 0, 2, 0, /* 0x77 */
909 {0, 0, 1, 0, /* 0x78 */
916 {1, 0, 2, 0, /* 0x79 */
923 {0, 0, 2, 0, /* 0x7a */
930 {1, 0, 2, 0, /* 0x7b */
937 {0, 0, 1, 0, /* 0x7c */
944 {1, 0, 2, 0, /* 0x7d */
951 {0, 0, 1, 0, /* 0x7e */
958 {1, 0, 1, 0, /* 0x7f */
965 {0, 1, 1, 0, /* 0x80 */
972 {1, 1, 2, 0, /* 0x81 */
979 {0, 1, 2, 0, /* 0x82 */
986 {1, 1, 2, 0, /* 0x83 */
993 {0, 1, 2, 0, /* 0x84 */
1000 {1, 1, 3, 0, /* 0x85 */
1007 {0, 1, 2, 0, /* 0x86 */
1014 {1, 1, 2, 0, /* 0x87 */
1021 {0, 1, 2, 0, /* 0x88 */
1028 {1, 1, 3, 0, /* 0x89 */
1035 {0, 1, 3, 0, /* 0x8a */
1042 {1, 1, 3, 0, /* 0x8b */
1049 {0, 1, 2, 0, /* 0x8c */
1056 {1, 1, 3, 0, /* 0x8d */
1063 {0, 1, 2, 0, /* 0x8e */
1070 {1, 1, 2, 0, /* 0x8f */
1077 {0, 1, 2, 0, /* 0x90 */
1084 {1, 1, 3, 0, /* 0x91 */
1091 {0, 1, 3, 0, /* 0x92 */
1098 {1, 1, 3, 0, /* 0x93 */
1105 {0, 1, 3, 0, /* 0x94 */
1112 {1, 1, 4, 0, /* 0x95 */
1119 {0, 1, 3, 0, /* 0x96 */
1126 {1, 1, 3, 0, /* 0x97 */
1133 {0, 1, 2, 0, /* 0x98 */
1140 {1, 1, 3, 0, /* 0x99 */
1147 {0, 1, 3, 0, /* 0x9a */
1154 {1, 1, 3, 0, /* 0x9b */
1161 {0, 1, 2, 0, /* 0x9c */
1168 {1, 1, 3, 0, /* 0x9d */
1175 {0, 1, 2, 0, /* 0x9e */
1182 {1, 1, 2, 0, /* 0x9f */
1189 {0, 1, 2, 0, /* 0xa0 */
1196 {1, 1, 3, 0, /* 0xa1 */
1203 {0, 1, 3, 0, /* 0xa2 */
1210 {1, 1, 3, 0, /* 0xa3 */
1217 {0, 1, 3, 0, /* 0xa4 */
1224 {1, 1, 4, 0, /* 0xa5 */
1231 {0, 1, 3, 0, /* 0xa6 */
1238 {1, 1, 3, 0, /* 0xa7 */
1245 {0, 1, 3, 0, /* 0xa8 */
1252 {1, 1, 4, 0, /* 0xa9 */
1259 {0, 1, 4, 0, /* 0xaa */
1266 {1, 1, 4, 0, /* 0xab */
1273 {0, 1, 3, 0, /* 0xac */
1280 {1, 1, 4, 0, /* 0xad */
1287 {0, 1, 3, 0, /* 0xae */
1294 {1, 1, 3, 0, /* 0xaf */
1301 {0, 1, 2, 0, /* 0xb0 */
1308 {1, 1, 3, 0, /* 0xb1 */
1315 {0, 1, 3, 0, /* 0xb2 */
1322 {1, 1, 3, 0, /* 0xb3 */
1329 {0, 1, 3, 0, /* 0xb4 */
1336 {1, 1, 4, 0, /* 0xb5 */
1343 {0, 1, 3, 0, /* 0xb6 */
1350 {1, 1, 3, 0, /* 0xb7 */
1357 {0, 1, 2, 0, /* 0xb8 */
1364 {1, 1, 3, 0, /* 0xb9 */
1371 {0, 1, 3, 0, /* 0xba */
1378 {1, 1, 3, 0, /* 0xbb */
1385 {0, 1, 2, 0, /* 0xbc */
1392 {1, 1, 3, 0, /* 0xbd */
1399 {0, 1, 2, 0, /* 0xbe */
1406 {1, 1, 2, 0, /* 0xbf */
1413 {0, 1, 1, 0, /* 0xc0 */
1420 {1, 1, 2, 0, /* 0xc1 */
1427 {0, 1, 2, 0, /* 0xc2 */
1434 {1, 1, 2, 0, /* 0xc3 */
1441 {0, 1, 2, 0, /* 0xc4 */
1448 {1, 1, 3, 0, /* 0xc5 */
1455 {0, 1, 2, 0, /* 0xc6 */
1462 {1, 1, 2, 0, /* 0xc7 */
1469 {0, 1, 2, 0, /* 0xc8 */
1476 {1, 1, 3, 0, /* 0xc9 */
1483 {0, 1, 3, 0, /* 0xca */
1490 {1, 1, 3, 0, /* 0xcb */
1497 {0, 1, 2, 0, /* 0xcc */
1504 {1, 1, 3, 0, /* 0xcd */
1511 {0, 1, 2, 0, /* 0xce */
1518 {1, 1, 2, 0, /* 0xcf */
1525 {0, 1, 2, 0, /* 0xd0 */
1532 {1, 1, 3, 0, /* 0xd1 */
1539 {0, 1, 3, 0, /* 0xd2 */
1546 {1, 1, 3, 0, /* 0xd3 */
1553 {0, 1, 3, 0, /* 0xd4 */
1560 {1, 1, 4, 0, /* 0xd5 */
1567 {0, 1, 3, 0, /* 0xd6 */
1574 {1, 1, 3, 0, /* 0xd7 */
1581 {0, 1, 2, 0, /* 0xd8 */
1588 {1, 1, 3, 0, /* 0xd9 */
1595 {0, 1, 3, 0, /* 0xda */
1602 {1, 1, 3, 0, /* 0xdb */
1609 {0, 1, 2, 0, /* 0xdc */
1616 {1, 1, 3, 0, /* 0xdd */
1623 {0, 1, 2, 0, /* 0xde */
1630 {1, 1, 2, 0, /* 0xdf */
1637 {0, 1, 1, 0, /* 0xe0 */
1644 {1, 1, 2, 0, /* 0xe1 */
1651 {0, 1, 2, 0, /* 0xe2 */
1658 {1, 1, 2, 0, /* 0xe3 */
1665 {0, 1, 2, 0, /* 0xe4 */
1672 {1, 1, 3, 0, /* 0xe5 */
1679 {0, 1, 2, 0, /* 0xe6 */
1686 {1, 1, 2, 0, /* 0xe7 */
1693 {0, 1, 2, 0, /* 0xe8 */
1700 {1, 1, 3, 0, /* 0xe9 */
1707 {0, 1, 3, 0, /* 0xea */
1714 {1, 1, 3, 0, /* 0xeb */
1721 {0, 1, 2, 0, /* 0xec */
1728 {1, 1, 3, 0, /* 0xed */
1735 {0, 1, 2, 0, /* 0xee */
1742 {1, 1, 2, 0, /* 0xef */
1749 {0, 1, 1, 0, /* 0xf0 */
1756 {1, 1, 2, 0, /* 0xf1 */
1763 {0, 1, 2, 0, /* 0xf2 */
1770 {1, 1, 2, 0, /* 0xf3 */
1777 {0, 1, 2, 0, /* 0xf4 */
1784 {1, 1, 3, 0, /* 0xf5 */
1791 {0, 1, 2, 0, /* 0xf6 */
1798 {1, 1, 2, 0, /* 0xf7 */
1805 {0, 1, 1, 0, /* 0xf8 */
1812 {1, 1, 2, 0, /* 0xf9 */
1819 {0, 1, 2, 0, /* 0xfa */
1826 {1, 1, 2, 0, /* 0xfb */
1833 {0, 1, 1, 0, /* 0xfc */
1840 {1, 1, 2, 0, /* 0xfd */
1847 {0, 1, 1, 0, /* 0xfe */
1854 {1, 1, 1, 0, /* 0xff */
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865 struct sctp_scoping *scope,
1868 if ((scope->loopback_scope == 0) &&
1869 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1871 * skip loopback if not in scope *
1875 switch (ifa->address.sa.sa_family) {
1878 if (scope->ipv4_addr_legal) {
1879 struct sockaddr_in *sin;
1881 sin = &ifa->address.sin;
1882 if (sin->sin_addr.s_addr == 0) {
1883 /* not in scope , unspecified */
1886 if ((scope->ipv4_local_scope == 0) &&
1887 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1888 /* private address not in scope */
1898 if (scope->ipv6_addr_legal) {
1899 struct sockaddr_in6 *sin6;
1902 * Must update the flags, bummer, which means any
1903 * IFA locks must now be applied HERE <->
1906 sctp_gather_internal_ifa_flags(ifa);
1908 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1911 /* ok to use deprecated addresses? */
1912 sin6 = &ifa->address.sin6;
1913 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1914 /* skip unspecified addresses */
1917 if ( /* (local_scope == 0) && */
1918 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1921 if ((scope->site_scope == 0) &&
1922 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1936 static struct mbuf *
1937 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1939 #if defined(INET) || defined(INET6)
1940 struct sctp_paramhdr *paramh;
1945 switch (ifa->address.sa.sa_family) {
1948 plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1953 plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1959 #if defined(INET) || defined(INET6)
1960 if (M_TRAILINGSPACE(m) >= plen) {
1961 /* easy side we just drop it on the end */
1962 paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1965 /* Need more space */
1967 while (SCTP_BUF_NEXT(mret) != NULL) {
1968 mret = SCTP_BUF_NEXT(mret);
1970 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
1971 if (SCTP_BUF_NEXT(mret) == NULL) {
1972 /* We are hosed, can't add more addresses */
1975 mret = SCTP_BUF_NEXT(mret);
1976 paramh = mtod(mret, struct sctp_paramhdr *);
1978 /* now add the parameter */
1979 switch (ifa->address.sa.sa_family) {
1983 struct sctp_ipv4addr_param *ipv4p;
1984 struct sockaddr_in *sin;
1986 sin = &ifa->address.sin;
1987 ipv4p = (struct sctp_ipv4addr_param *)paramh;
1988 paramh->param_type = htons(SCTP_IPV4_ADDRESS);
1989 paramh->param_length = htons(plen);
1990 ipv4p->addr = sin->sin_addr.s_addr;
1991 SCTP_BUF_LEN(mret) += plen;
1998 struct sctp_ipv6addr_param *ipv6p;
1999 struct sockaddr_in6 *sin6;
2001 sin6 = &ifa->address.sin6;
2002 ipv6p = (struct sctp_ipv6addr_param *)paramh;
2003 paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2004 paramh->param_length = htons(plen);
2005 memcpy(ipv6p->addr, &sin6->sin6_addr,
2006 sizeof(ipv6p->addr));
2007 /* clear embedded scope in the address */
2008 in6_clearscope((struct in6_addr *)ipv6p->addr);
2009 SCTP_BUF_LEN(mret) += plen;
2024 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2025 struct sctp_scoping *scope,
2026 struct mbuf *m_at, int cnt_inits_to,
2027 uint16_t *padding_len, uint16_t *chunk_len)
2029 struct sctp_vrf *vrf = NULL;
2030 int cnt, limit_out = 0, total_count;
2033 vrf_id = inp->def_vrf_id;
2034 SCTP_IPI_ADDR_RLOCK();
2035 vrf = sctp_find_vrf(vrf_id);
2037 SCTP_IPI_ADDR_RUNLOCK();
2040 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2041 struct sctp_ifa *sctp_ifap;
2042 struct sctp_ifn *sctp_ifnp;
2045 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2047 cnt = SCTP_ADDRESS_LIMIT;
2050 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2051 if ((scope->loopback_scope == 0) &&
2052 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2054 * Skip loopback devices if loopback_scope
2059 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2061 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2062 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2063 &sctp_ifap->address.sin.sin_addr) != 0)) {
2068 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2069 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2070 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2074 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2077 if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2081 if (cnt > SCTP_ADDRESS_LIMIT) {
2085 if (cnt > SCTP_ADDRESS_LIMIT) {
2092 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2094 if ((scope->loopback_scope == 0) &&
2095 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2097 * Skip loopback devices if
2098 * loopback_scope not set
2102 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2104 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2105 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2106 &sctp_ifap->address.sin.sin_addr) != 0)) {
2111 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2112 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2113 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2117 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2120 if (sctp_is_address_in_scope(sctp_ifap,
2124 if ((chunk_len != NULL) &&
2125 (padding_len != NULL) &&
2126 (*padding_len > 0)) {
2127 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2128 SCTP_BUF_LEN(m_at) += *padding_len;
2129 *chunk_len += *padding_len;
2132 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2143 if (total_count > SCTP_ADDRESS_LIMIT) {
2144 /* No more addresses */
2152 struct sctp_laddr *laddr;
2155 /* First, how many ? */
2156 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2157 if (laddr->ifa == NULL) {
2160 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2162 * Address being deleted by the system, dont
2166 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2168 * Address being deleted on this ep don't
2173 if (sctp_is_address_in_scope(laddr->ifa,
2180 * To get through a NAT we only list addresses if we have
2181 * more than one. That way if you just bind a single address
2182 * we let the source of the init dictate our address.
2186 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2187 if (laddr->ifa == NULL) {
2190 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2193 if (sctp_is_address_in_scope(laddr->ifa,
2197 if ((chunk_len != NULL) &&
2198 (padding_len != NULL) &&
2199 (*padding_len > 0)) {
2200 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2201 SCTP_BUF_LEN(m_at) += *padding_len;
2202 *chunk_len += *padding_len;
2205 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2207 if (cnt >= SCTP_ADDRESS_LIMIT) {
2213 SCTP_IPI_ADDR_RUNLOCK();
2217 static struct sctp_ifa *
2218 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2219 uint8_t dest_is_loop,
2220 uint8_t dest_is_priv,
2223 uint8_t dest_is_global = 0;
2225 /* dest_is_priv is true if destination is a private address */
2226 /* dest_is_loop is true if destination is a loopback addresses */
2229 * Here we determine if its a preferred address. A preferred address
2230 * means it is the same scope or higher scope then the destination.
2231 * L = loopback, P = private, G = global
2232 * -----------------------------------------
2233 * src | dest | result
2234 * ----------------------------------------
2236 * -----------------------------------------
2237 * P | L | yes-v4 no-v6
2238 * -----------------------------------------
2239 * G | L | yes-v4 no-v6
2240 * -----------------------------------------
2242 * -----------------------------------------
2244 * -----------------------------------------
2246 * -----------------------------------------
2248 * -----------------------------------------
2250 * -----------------------------------------
2252 * -----------------------------------------
2255 if (ifa->address.sa.sa_family != fam) {
2256 /* forget mis-matched family */
2259 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2262 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2263 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2264 /* Ok the address may be ok */
2266 if (fam == AF_INET6) {
2267 /* ok to use deprecated addresses? no lets not! */
2268 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2269 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2272 if (ifa->src_is_priv && !ifa->src_is_loop) {
2274 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2278 if (ifa->src_is_glob) {
2280 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2287 * Now that we know what is what, implement or table this could in
2288 * theory be done slicker (it used to be), but this is
2289 * straightforward and easier to validate :-)
2291 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2292 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2293 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2294 dest_is_loop, dest_is_priv, dest_is_global);
2296 if ((ifa->src_is_loop) && (dest_is_priv)) {
2297 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2300 if ((ifa->src_is_glob) && (dest_is_priv)) {
2301 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2304 if ((ifa->src_is_loop) && (dest_is_global)) {
2305 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2308 if ((ifa->src_is_priv) && (dest_is_global)) {
2309 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2312 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2313 /* its a preferred address */
2317 static struct sctp_ifa *
2318 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2319 uint8_t dest_is_loop,
2320 uint8_t dest_is_priv,
2323 uint8_t dest_is_global = 0;
2326 * Here we determine if its a acceptable address. A acceptable
2327 * address means it is the same scope or higher scope but we can
2328 * allow for NAT which means its ok to have a global dest and a
2331 * L = loopback, P = private, G = global
2332 * -----------------------------------------
2333 * src | dest | result
2334 * -----------------------------------------
2336 * -----------------------------------------
2337 * P | L | yes-v4 no-v6
2338 * -----------------------------------------
2340 * -----------------------------------------
2342 * -----------------------------------------
2344 * -----------------------------------------
2345 * G | P | yes - May not work
2346 * -----------------------------------------
2348 * -----------------------------------------
2349 * P | G | yes - May not work
2350 * -----------------------------------------
2352 * -----------------------------------------
2355 if (ifa->address.sa.sa_family != fam) {
2356 /* forget non matching family */
2357 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2358 ifa->address.sa.sa_family, fam);
2361 /* Ok the address may be ok */
2362 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2363 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2364 dest_is_loop, dest_is_priv);
2365 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2369 if (fam == AF_INET6) {
2370 /* ok to use deprecated addresses? */
2371 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2374 if (ifa->src_is_priv) {
2375 /* Special case, linklocal to loop */
2382 * Now that we know what is what, implement our table. This could in
2383 * theory be done slicker (it used to be), but this is
2384 * straightforward and easier to validate :-)
2386 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2389 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2392 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2395 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2398 SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2399 /* its an acceptable address */
2404 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2406 struct sctp_laddr *laddr;
2409 /* There are no restrictions, no TCB :-) */
2412 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2413 if (laddr->ifa == NULL) {
2414 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2418 if (laddr->ifa == ifa) {
2419 /* Yes it is on the list */
2427 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2429 struct sctp_laddr *laddr;
2433 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2434 if (laddr->ifa == NULL) {
2435 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2439 if ((laddr->ifa == ifa) && laddr->action == 0)
2446 static struct sctp_ifa *
2447 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2450 int non_asoc_addr_ok,
2451 uint8_t dest_is_priv,
2452 uint8_t dest_is_loop,
2455 struct sctp_laddr *laddr, *starting_point;
2458 struct sctp_ifn *sctp_ifn;
2459 struct sctp_ifa *sctp_ifa, *sifa;
2460 struct sctp_vrf *vrf;
2463 vrf = sctp_find_vrf(vrf_id);
2467 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2468 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2469 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2471 * first question, is the ifn we will emit on in our list, if so, we
2472 * want such an address. Note that we first looked for a preferred
2476 /* is a preferred one on the interface we route out? */
2477 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2479 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2480 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2481 &sctp_ifa->address.sin.sin_addr) != 0)) {
2486 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2487 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2488 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2492 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2493 (non_asoc_addr_ok == 0))
2495 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2500 if (sctp_is_addr_in_ep(inp, sifa)) {
2501 atomic_add_int(&sifa->refcount, 1);
2507 * ok, now we now need to find one on the list of the addresses. We
2508 * can't get one on the emitting interface so let's find first a
2509 * preferred one. If not that an acceptable one otherwise... we
2512 starting_point = inp->next_addr_touse;
2514 if (inp->next_addr_touse == NULL) {
2515 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2518 for (laddr = inp->next_addr_touse; laddr;
2519 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2520 if (laddr->ifa == NULL) {
2521 /* address has been removed */
2524 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2525 /* address is being deleted */
2528 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2532 atomic_add_int(&sifa->refcount, 1);
2535 if (resettotop == 0) {
2536 inp->next_addr_touse = NULL;
2540 inp->next_addr_touse = starting_point;
2543 if (inp->next_addr_touse == NULL) {
2544 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2548 /* ok, what about an acceptable address in the inp */
2549 for (laddr = inp->next_addr_touse; laddr;
2550 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2551 if (laddr->ifa == NULL) {
2552 /* address has been removed */
2555 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2556 /* address is being deleted */
2559 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2563 atomic_add_int(&sifa->refcount, 1);
2566 if (resettotop == 0) {
2567 inp->next_addr_touse = NULL;
2568 goto once_again_too;
2572 * no address bound can be a source for the destination we are in
2578 static struct sctp_ifa *
2579 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2580 struct sctp_tcb *stcb,
2583 uint8_t dest_is_priv,
2584 uint8_t dest_is_loop,
2585 int non_asoc_addr_ok,
2588 struct sctp_laddr *laddr, *starting_point;
2590 struct sctp_ifn *sctp_ifn;
2591 struct sctp_ifa *sctp_ifa, *sifa;
2592 uint8_t start_at_beginning = 0;
2593 struct sctp_vrf *vrf;
2597 * first question, is the ifn we will emit on in our list, if so, we
2600 vrf = sctp_find_vrf(vrf_id);
2604 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2605 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2606 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2609 * first question, is the ifn we will emit on in our list? If so,
2610 * we want that one. First we look for a preferred. Second, we go
2611 * for an acceptable.
2614 /* first try for a preferred address on the ep */
2615 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2617 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2618 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2619 &sctp_ifa->address.sin.sin_addr) != 0)) {
2624 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2625 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2626 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2630 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2632 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2633 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2636 if (((non_asoc_addr_ok == 0) &&
2637 (sctp_is_addr_restricted(stcb, sifa))) ||
2638 (non_asoc_addr_ok &&
2639 (sctp_is_addr_restricted(stcb, sifa)) &&
2640 (!sctp_is_addr_pending(stcb, sifa)))) {
2641 /* on the no-no list */
2644 atomic_add_int(&sifa->refcount, 1);
2648 /* next try for an acceptable address on the ep */
2649 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2651 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2652 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2653 &sctp_ifa->address.sin.sin_addr) != 0)) {
2658 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2659 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2660 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2664 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2666 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2667 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2670 if (((non_asoc_addr_ok == 0) &&
2671 (sctp_is_addr_restricted(stcb, sifa))) ||
2672 (non_asoc_addr_ok &&
2673 (sctp_is_addr_restricted(stcb, sifa)) &&
2674 (!sctp_is_addr_pending(stcb, sifa)))) {
2675 /* on the no-no list */
2678 atomic_add_int(&sifa->refcount, 1);
2684 * if we can't find one like that then we must look at all addresses
2685 * bound to pick one at first preferable then secondly acceptable.
2687 starting_point = stcb->asoc.last_used_address;
2689 if (stcb->asoc.last_used_address == NULL) {
2690 start_at_beginning = 1;
2691 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2693 /* search beginning with the last used address */
2694 for (laddr = stcb->asoc.last_used_address; laddr;
2695 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2696 if (laddr->ifa == NULL) {
2697 /* address has been removed */
2700 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2701 /* address is being deleted */
2704 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2707 if (((non_asoc_addr_ok == 0) &&
2708 (sctp_is_addr_restricted(stcb, sifa))) ||
2709 (non_asoc_addr_ok &&
2710 (sctp_is_addr_restricted(stcb, sifa)) &&
2711 (!sctp_is_addr_pending(stcb, sifa)))) {
2712 /* on the no-no list */
2715 stcb->asoc.last_used_address = laddr;
2716 atomic_add_int(&sifa->refcount, 1);
2719 if (start_at_beginning == 0) {
2720 stcb->asoc.last_used_address = NULL;
2721 goto sctp_from_the_top;
2723 /* now try for any higher scope than the destination */
2724 stcb->asoc.last_used_address = starting_point;
2725 start_at_beginning = 0;
2727 if (stcb->asoc.last_used_address == NULL) {
2728 start_at_beginning = 1;
2729 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2731 /* search beginning with the last used address */
2732 for (laddr = stcb->asoc.last_used_address; laddr;
2733 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2734 if (laddr->ifa == NULL) {
2735 /* address has been removed */
2738 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2739 /* address is being deleted */
2742 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2746 if (((non_asoc_addr_ok == 0) &&
2747 (sctp_is_addr_restricted(stcb, sifa))) ||
2748 (non_asoc_addr_ok &&
2749 (sctp_is_addr_restricted(stcb, sifa)) &&
2750 (!sctp_is_addr_pending(stcb, sifa)))) {
2751 /* on the no-no list */
2754 stcb->asoc.last_used_address = laddr;
2755 atomic_add_int(&sifa->refcount, 1);
2758 if (start_at_beginning == 0) {
2759 stcb->asoc.last_used_address = NULL;
2760 goto sctp_from_the_top2;
2765 static struct sctp_ifa *
2766 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2767 struct sctp_inpcb *inp,
2768 struct sctp_tcb *stcb,
2769 int non_asoc_addr_ok,
2770 uint8_t dest_is_loop,
2771 uint8_t dest_is_priv,
2776 struct sctp_ifa *ifa, *sifa;
2777 int num_eligible_addr = 0;
2779 struct sockaddr_in6 sin6, lsa6;
2781 if (fam == AF_INET6) {
2782 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2783 (void)sa6_recoverscope(&sin6);
2786 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2788 if ((ifa->address.sa.sa_family == AF_INET) &&
2789 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2790 &ifa->address.sin.sin_addr) != 0)) {
2795 if ((ifa->address.sa.sa_family == AF_INET6) &&
2796 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2797 &ifa->address.sin6.sin6_addr) != 0)) {
2801 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2802 (non_asoc_addr_ok == 0))
2804 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2809 if (fam == AF_INET6 &&
2811 sifa->src_is_loop && sifa->src_is_priv) {
2813 * don't allow fe80::1 to be a src on loop ::1, we
2814 * don't list it to the peer so we will get an
2819 if (fam == AF_INET6 &&
2820 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2821 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2823 * link-local <-> link-local must belong to the same
2826 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2827 (void)sa6_recoverscope(&lsa6);
2828 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2835 * Check if the IPv6 address matches to next-hop. In the
2836 * mobile case, old IPv6 address may be not deleted from the
2837 * interface. Then, the interface has previous and new
2838 * addresses. We should use one corresponding to the
2839 * next-hop. (by micchie)
2842 if (stcb && fam == AF_INET6 &&
2843 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2844 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2850 /* Avoid topologically incorrect IPv4 address */
2851 if (stcb && fam == AF_INET &&
2852 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2853 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2859 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2862 if (((non_asoc_addr_ok == 0) &&
2863 (sctp_is_addr_restricted(stcb, sifa))) ||
2864 (non_asoc_addr_ok &&
2865 (sctp_is_addr_restricted(stcb, sifa)) &&
2866 (!sctp_is_addr_pending(stcb, sifa)))) {
2868 * It is restricted for some reason..
2869 * probably not yet added.
2874 if (num_eligible_addr >= addr_wanted) {
2877 num_eligible_addr++;
2883 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2884 struct sctp_inpcb *inp,
2885 struct sctp_tcb *stcb,
2886 int non_asoc_addr_ok,
2887 uint8_t dest_is_loop,
2888 uint8_t dest_is_priv,
2891 struct sctp_ifa *ifa, *sifa;
2892 int num_eligible_addr = 0;
2894 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2896 if ((ifa->address.sa.sa_family == AF_INET) &&
2897 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2898 &ifa->address.sin.sin_addr) != 0)) {
2903 if ((ifa->address.sa.sa_family == AF_INET6) &&
2905 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2906 &ifa->address.sin6.sin6_addr) != 0)) {
2910 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2911 (non_asoc_addr_ok == 0)) {
2914 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2920 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2923 if (((non_asoc_addr_ok == 0) &&
2924 (sctp_is_addr_restricted(stcb, sifa))) ||
2925 (non_asoc_addr_ok &&
2926 (sctp_is_addr_restricted(stcb, sifa)) &&
2927 (!sctp_is_addr_pending(stcb, sifa)))) {
2929 * It is restricted for some reason..
2930 * probably not yet added.
2935 num_eligible_addr++;
2937 return (num_eligible_addr);
2940 static struct sctp_ifa *
2941 sctp_choose_boundall(struct sctp_inpcb *inp,
2942 struct sctp_tcb *stcb,
2943 struct sctp_nets *net,
2946 uint8_t dest_is_priv,
2947 uint8_t dest_is_loop,
2948 int non_asoc_addr_ok,
2951 int cur_addr_num = 0, num_preferred = 0;
2953 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2954 struct sctp_ifa *sctp_ifa, *sifa;
2956 struct sctp_vrf *vrf;
2962 * For boundall we can use any address in the association.
2963 * If non_asoc_addr_ok is set we can use any address (at least in
2964 * theory). So we look for preferred addresses first. If we find one,
2965 * we use it. Otherwise we next try to get an address on the
2966 * interface, which we should be able to do (unless non_asoc_addr_ok
2967 * is false and we are routed out that way). In these cases where we
2968 * can't use the address of the interface we go through all the
2969 * ifn's looking for an address we can use and fill that in. Punting
2970 * means we send back address 0, which will probably cause problems
2971 * actually since then IP will fill in the address of the route ifn,
2972 * which means we probably already rejected it.. i.e. here comes an
2975 vrf = sctp_find_vrf(vrf_id);
2979 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2980 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2981 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2982 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2983 if (sctp_ifn == NULL) {
2984 /* ?? We don't have this guy ?? */
2985 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2986 goto bound_all_plan_b;
2988 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2989 ifn_index, sctp_ifn->ifn_name);
2992 cur_addr_num = net->indx_of_eligible_next_to_use;
2994 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2999 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3000 num_preferred, sctp_ifn->ifn_name);
3001 if (num_preferred == 0) {
3003 * no eligible addresses, we must use some other interface
3004 * address if we can find one.
3006 goto bound_all_plan_b;
3009 * Ok we have num_eligible_addr set with how many we can use, this
3010 * may vary from call to call due to addresses being deprecated
3013 if (cur_addr_num >= num_preferred) {
3017 * select the nth address from the list (where cur_addr_num is the
3018 * nth) and 0 is the first one, 1 is the second one etc...
3020 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3022 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3023 dest_is_priv, cur_addr_num, fam, ro);
3025 /* if sctp_ifa is NULL something changed??, fall to plan b. */
3027 atomic_add_int(&sctp_ifa->refcount, 1);
3029 /* save off where the next one we will want */
3030 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3035 * plan_b: Look at all interfaces and find a preferred address. If
3036 * no preferred fall through to plan_c.
3039 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3040 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3041 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3042 sctp_ifn->ifn_name);
3043 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3044 /* wrong base scope */
3045 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3048 if ((sctp_ifn == looked_at) && looked_at) {
3049 /* already looked at this guy */
3050 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3053 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3054 dest_is_loop, dest_is_priv, fam);
3055 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3056 "Found ifn:%p %d preferred source addresses\n",
3057 ifn, num_preferred);
3058 if (num_preferred == 0) {
3059 /* None on this interface. */
3060 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3063 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3064 "num preferred:%d on interface:%p cur_addr_num:%d\n",
3065 num_preferred, (void *)sctp_ifn, cur_addr_num);
3068 * Ok we have num_eligible_addr set with how many we can
3069 * use, this may vary from call to call due to addresses
3070 * being deprecated etc..
3072 if (cur_addr_num >= num_preferred) {
3075 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3076 dest_is_priv, cur_addr_num, fam, ro);
3080 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3081 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3083 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3084 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3085 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3086 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3088 atomic_add_int(&sifa->refcount, 1);
3092 again_with_private_addresses_allowed:
3094 /* plan_c: do we have an acceptable address on the emit interface */
3096 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3097 if (emit_ifn == NULL) {
3098 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3101 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3102 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3104 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3105 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3106 &sctp_ifa->address.sin.sin_addr) != 0)) {
3107 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3112 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3113 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3114 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3115 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3119 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3120 (non_asoc_addr_ok == 0)) {
3121 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3124 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3127 SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3131 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3132 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3136 if (((non_asoc_addr_ok == 0) &&
3137 (sctp_is_addr_restricted(stcb, sifa))) ||
3138 (non_asoc_addr_ok &&
3139 (sctp_is_addr_restricted(stcb, sifa)) &&
3140 (!sctp_is_addr_pending(stcb, sifa)))) {
3142 * It is restricted for some reason..
3143 * probably not yet added.
3145 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3150 atomic_add_int(&sifa->refcount, 1);
3155 * plan_d: We are in trouble. No preferred address on the emit
3156 * interface. And not even a preferred address on all interfaces. Go
3157 * out and see if we can find an acceptable address somewhere
3158 * amongst all interfaces.
3160 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3161 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3162 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3163 /* wrong base scope */
3166 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3168 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3169 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3170 &sctp_ifa->address.sin.sin_addr) != 0)) {
3175 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3176 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3177 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3181 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3182 (non_asoc_addr_ok == 0))
3184 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3190 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3194 if (((non_asoc_addr_ok == 0) &&
3195 (sctp_is_addr_restricted(stcb, sifa))) ||
3196 (non_asoc_addr_ok &&
3197 (sctp_is_addr_restricted(stcb, sifa)) &&
3198 (!sctp_is_addr_pending(stcb, sifa)))) {
3200 * It is restricted for some
3201 * reason.. probably not yet added.
3212 if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3213 stcb->asoc.scope.ipv4_local_scope = 1;
3215 goto again_with_private_addresses_allowed;
3216 } else if (retried == 1) {
3217 stcb->asoc.scope.ipv4_local_scope = 0;
3225 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3226 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3227 /* wrong base scope */
3230 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3231 struct sctp_ifa *tmp_sifa;
3234 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3235 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3236 &sctp_ifa->address.sin.sin_addr) != 0)) {
3241 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3242 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3243 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3247 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3248 (non_asoc_addr_ok == 0))
3250 tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3253 if (tmp_sifa == NULL) {
3256 if (tmp_sifa == sifa) {
3260 if (sctp_is_address_in_scope(tmp_sifa,
3261 &stcb->asoc.scope, 0) == 0) {
3264 if (((non_asoc_addr_ok == 0) &&
3265 (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3266 (non_asoc_addr_ok &&
3267 (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3268 (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3278 if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3279 (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3280 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3285 atomic_add_int(&sifa->refcount, 1);
3291 /* tcb may be NULL */
3293 sctp_source_address_selection(struct sctp_inpcb *inp,
3294 struct sctp_tcb *stcb,
3296 struct sctp_nets *net,
3297 int non_asoc_addr_ok, uint32_t vrf_id)
3299 struct sctp_ifa *answer;
3300 uint8_t dest_is_priv, dest_is_loop;
3303 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3306 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3311 * - Find the route if needed, cache if I can.
3312 * - Look at interface address in route, Is it in the bound list. If so we
3313 * have the best source.
3314 * - If not we must rotate amongst the addresses.
3316 * Caveats and issues
3318 * Do we need to pay attention to scope. We can have a private address
3319 * or a global address we are sourcing or sending to. So if we draw
3321 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3323 * ------------------------------------------
3324 * source * dest * result
3325 * -----------------------------------------
3326 * <a> Private * Global * NAT
3327 * -----------------------------------------
3328 * <b> Private * Private * No problem
3329 * -----------------------------------------
3330 * <c> Global * Private * Huh, How will this work?
3331 * -----------------------------------------
3332 * <d> Global * Global * No Problem
3333 *------------------------------------------
3334 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3336 *------------------------------------------
3337 * source * dest * result
3338 * -----------------------------------------
3339 * <a> Linklocal * Global *
3340 * -----------------------------------------
3341 * <b> Linklocal * Linklocal * No problem
3342 * -----------------------------------------
3343 * <c> Global * Linklocal * Huh, How will this work?
3344 * -----------------------------------------
3345 * <d> Global * Global * No Problem
3346 *------------------------------------------
3347 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3349 * And then we add to that what happens if there are multiple addresses
3350 * assigned to an interface. Remember the ifa on a ifn is a linked
3351 * list of addresses. So one interface can have more than one IP
3352 * address. What happens if we have both a private and a global
3353 * address? Do we then use context of destination to sort out which
3354 * one is best? And what about NAT's sending P->G may get you a NAT
3355 * translation, or should you select the G thats on the interface in
3360 * - count the number of addresses on the interface.
3361 * - if it is one, no problem except case <c>.
3362 * For <a> we will assume a NAT out there.
3363 * - if there are more than one, then we need to worry about scope P
3364 * or G. We should prefer G -> G and P -> P if possible.
3365 * Then as a secondary fall back to mixed types G->P being a last
3367 * - The above all works for bound all, but bound specific we need to
3368 * use the same concept but instead only consider the bound
3369 * addresses. If the bound set is NOT assigned to the interface then
3370 * we must use rotation amongst the bound addresses..
3372 if (ro->ro_nh == NULL) {
3374 * Need a route to cache.
3376 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3378 if (ro->ro_nh == NULL) {
3381 fam = ro->ro_dst.sa_family;
3382 dest_is_priv = dest_is_loop = 0;
3383 /* Setup our scopes for the destination */
3387 /* Scope based on outbound address */
3388 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3391 /* mark it as local */
3392 net->addr_is_local = 1;
3394 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3401 /* Scope based on outbound address */
3402 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3403 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3405 * If the address is a loopback address, which
3406 * consists of "::1" OR "fe80::1%lo0", we are
3407 * loopback scope. But we don't use dest_is_priv
3408 * (link local addresses).
3412 /* mark it as local */
3413 net->addr_is_local = 1;
3415 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3421 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3422 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3423 SCTP_IPI_ADDR_RLOCK();
3424 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3428 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3429 dest_is_priv, dest_is_loop,
3430 non_asoc_addr_ok, fam);
3431 SCTP_IPI_ADDR_RUNLOCK();
3438 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3439 vrf_id, dest_is_priv,
3441 non_asoc_addr_ok, fam);
3443 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3448 SCTP_IPI_ADDR_RUNLOCK();
3453 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3456 struct sctp_sndinfo sndinfo;
3457 struct sctp_prinfo prinfo;
3458 struct sctp_authinfo authinfo;
3459 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3463 * Independent of how many mbufs, find the c_type inside the control
3464 * structure and copy out the data.
3467 tot_len = SCTP_BUF_LEN(control);
3468 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3469 rem_len = tot_len - off;
3470 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3471 /* There is not enough room for one more. */
3474 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3475 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3476 /* We dont't have a complete CMSG header. */
3479 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3480 /* We don't have the complete CMSG. */
3483 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3484 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3485 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3486 ((c_type == cmh.cmsg_type) ||
3487 ((c_type == SCTP_SNDRCV) &&
3488 ((cmh.cmsg_type == SCTP_SNDINFO) ||
3489 (cmh.cmsg_type == SCTP_PRINFO) ||
3490 (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3491 if (c_type == cmh.cmsg_type) {
3492 if (cpsize > INT_MAX) {
3495 if (cmsg_data_len < (int)cpsize) {
3498 /* It is exactly what we want. Copy it out. */
3499 m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3502 struct sctp_sndrcvinfo *sndrcvinfo;
3504 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3506 if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3509 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3511 switch (cmh.cmsg_type) {
3513 if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3516 m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3517 sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3518 sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3519 sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3520 sndrcvinfo->sinfo_context = sndinfo.snd_context;
3521 sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3524 if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3527 m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3528 if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3529 sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3531 sndrcvinfo->sinfo_timetolive = 0;
3533 sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3536 if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3539 m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3540 sndrcvinfo->sinfo_keynumber_valid = 1;
3541 sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3554 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3557 struct sctp_initmsg initmsg;
3559 struct sockaddr_in sin;
3562 struct sockaddr_in6 sin6;
3564 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3566 tot_len = SCTP_BUF_LEN(control);
3567 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3568 rem_len = tot_len - off;
3569 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3570 /* There is not enough room for one more. */
3574 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3575 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3576 /* We dont't have a complete CMSG header. */
3580 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3581 /* We don't have the complete CMSG. */
3585 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3586 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3587 if (cmh.cmsg_level == IPPROTO_SCTP) {
3588 switch (cmh.cmsg_type) {
3590 if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3594 m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3595 if (initmsg.sinit_max_attempts)
3596 stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3597 if (initmsg.sinit_num_ostreams)
3598 stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3599 if (initmsg.sinit_max_instreams)
3600 stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3601 if (initmsg.sinit_max_init_timeo)
3602 stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3603 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3604 struct sctp_stream_out *tmp_str;
3606 #if defined(SCTP_DETAILED_STR_STATS)
3610 /* Default is NOT correct */
3611 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3612 stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3613 SCTP_TCB_UNLOCK(stcb);
3614 SCTP_MALLOC(tmp_str,
3615 struct sctp_stream_out *,
3616 (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3618 SCTP_TCB_LOCK(stcb);
3619 if (tmp_str != NULL) {
3620 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3621 stcb->asoc.strmout = tmp_str;
3622 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3624 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3626 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3627 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3628 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3629 stcb->asoc.strmout[i].chunks_on_queues = 0;
3630 #if defined(SCTP_DETAILED_STR_STATS)
3631 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3632 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3633 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3636 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3637 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3639 stcb->asoc.strmout[i].next_mid_ordered = 0;
3640 stcb->asoc.strmout[i].next_mid_unordered = 0;
3641 stcb->asoc.strmout[i].sid = i;
3642 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3643 stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3648 case SCTP_DSTADDRV4:
3649 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3653 memset(&sin, 0, sizeof(struct sockaddr_in));
3654 sin.sin_family = AF_INET;
3655 sin.sin_len = sizeof(struct sockaddr_in);
3656 sin.sin_port = stcb->rport;
3657 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3658 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3659 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3660 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3664 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3665 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3672 case SCTP_DSTADDRV6:
3673 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3677 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3678 sin6.sin6_family = AF_INET6;
3679 sin6.sin6_len = sizeof(struct sockaddr_in6);
3680 sin6.sin6_port = stcb->rport;
3681 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3682 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3683 IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3688 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3689 in6_sin6_2_sin(&sin, &sin6);
3690 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3691 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3692 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3696 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3697 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3703 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3704 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3718 #if defined(INET) || defined(INET6)
3719 static struct sctp_tcb *
3720 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3722 struct mbuf *control,
3723 struct sctp_nets **net_p,
3727 struct sctp_tcb *stcb;
3728 struct sockaddr *addr;
3730 struct sockaddr_in sin;
3733 struct sockaddr_in6 sin6;
3735 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3737 tot_len = SCTP_BUF_LEN(control);
3738 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3739 rem_len = tot_len - off;
3740 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3741 /* There is not enough room for one more. */
3745 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3746 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3747 /* We dont't have a complete CMSG header. */
3751 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3752 /* We don't have the complete CMSG. */
3756 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3757 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3758 if (cmh.cmsg_level == IPPROTO_SCTP) {
3759 switch (cmh.cmsg_type) {
3761 case SCTP_DSTADDRV4:
3762 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3766 memset(&sin, 0, sizeof(struct sockaddr_in));
3767 sin.sin_family = AF_INET;
3768 sin.sin_len = sizeof(struct sockaddr_in);
3769 sin.sin_port = port;
3770 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3771 addr = (struct sockaddr *)&sin;
3775 case SCTP_DSTADDRV6:
3776 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3780 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3781 sin6.sin6_family = AF_INET6;
3782 sin6.sin6_len = sizeof(struct sockaddr_in6);
3783 sin6.sin6_port = port;
3784 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3786 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3787 in6_sin6_2_sin(&sin, &sin6);
3788 addr = (struct sockaddr *)&sin;
3791 addr = (struct sockaddr *)&sin6;
3799 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3810 static struct mbuf *
3811 sctp_add_cookie(struct mbuf *init, int init_offset,
3812 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3814 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3815 struct sctp_state_cookie *stc;
3816 struct sctp_paramhdr *ph;
3819 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3820 sizeof(struct sctp_paramhdr)), 0,
3821 M_NOWAIT, 1, MT_DATA);
3825 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3826 if (copy_init == NULL) {
3830 #ifdef SCTP_MBUF_LOGGING
3831 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3832 sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3835 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3837 if (copy_initack == NULL) {
3839 sctp_m_freem(copy_init);
3842 #ifdef SCTP_MBUF_LOGGING
3843 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3844 sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3847 /* easy side we just drop it on the end */
3848 ph = mtod(mret, struct sctp_paramhdr *);
3849 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3850 sizeof(struct sctp_paramhdr);
3851 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3852 sizeof(struct sctp_paramhdr));
3853 ph->param_type = htons(SCTP_STATE_COOKIE);
3854 ph->param_length = 0; /* fill in at the end */
3855 /* Fill in the stc cookie data */
3856 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3858 /* tack the INIT and then the INIT-ACK onto the chain */
3860 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3861 cookie_sz += SCTP_BUF_LEN(m_at);
3862 if (SCTP_BUF_NEXT(m_at) == NULL) {
3863 SCTP_BUF_NEXT(m_at) = copy_init;
3867 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3868 cookie_sz += SCTP_BUF_LEN(m_at);
3869 if (SCTP_BUF_NEXT(m_at) == NULL) {
3870 SCTP_BUF_NEXT(m_at) = copy_initack;
3874 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3875 cookie_sz += SCTP_BUF_LEN(m_at);
3876 if (SCTP_BUF_NEXT(m_at) == NULL) {
3880 sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3882 /* no space, so free the entire chain */
3886 SCTP_BUF_NEXT(m_at) = sig;
3887 SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
3888 cookie_sz += SCTP_SIGNATURE_SIZE;
3889 ph->param_length = htons(cookie_sz);
3890 *signature = (uint8_t *)mtod(sig, caddr_t);
3891 memset(*signature, 0, SCTP_SIGNATURE_SIZE);
3896 sctp_get_ect(struct sctp_tcb *stcb)
3898 if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
3899 return (SCTP_ECT0_BIT);
3905 #if defined(INET) || defined(INET6)
3907 sctp_handle_no_route(struct sctp_tcb *stcb,
3908 struct sctp_nets *net,
3911 SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3914 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3915 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3916 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3917 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3918 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
3919 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3923 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3924 net->dest_state &= ~SCTP_ADDR_PF;
3928 if (net == stcb->asoc.primary_destination) {
3929 /* need a new primary */
3930 struct sctp_nets *alt;
3932 alt = sctp_find_alternate_net(stcb, net, 0);
3934 if (stcb->asoc.alternate) {
3935 sctp_free_remote_addr(stcb->asoc.alternate);
3937 stcb->asoc.alternate = alt;
3938 atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3939 if (net->ro._s_addr) {
3940 sctp_free_ifa(net->ro._s_addr);
3941 net->ro._s_addr = NULL;
3943 net->src_addr_selected = 0;
3952 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3953 struct sctp_tcb *stcb, /* may be NULL */
3954 struct sctp_nets *net,
3955 struct sockaddr *to,
3957 uint32_t auth_offset,
3958 struct sctp_auth_chunk *auth,
3959 uint16_t auth_keyid,
3960 int nofragment_flag,
3967 union sctp_sockstore *over_addr,
3968 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);
4205 SCTP_STAT_INCR(sctps_sendzerocrc);
4207 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4208 SCTP_STAT_INCR(sctps_sendswcrc);
4211 SCTP_ENABLE_UDP_CSUM(o_pak);
4215 SCTP_STAT_INCR(sctps_sendzerocrc);
4217 m->m_pkthdr.csum_flags = CSUM_SCTP;
4218 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4219 SCTP_STAT_INCR(sctps_sendhwcrc);
4222 #ifdef SCTP_PACKET_LOGGING
4223 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4224 sctp_packet_log(o_pak);
4226 /* send it out. table id is taken from stcb */
4227 SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4228 SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4230 UDPSTAT_INC(udps_opackets);
4232 SCTP_STAT_INCR(sctps_sendpackets);
4233 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4235 SCTP_STAT_INCR(sctps_senderrors);
4237 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4239 /* free tempy routes */
4242 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4243 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4246 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4249 mtu -= sizeof(struct udphdr);
4251 if (mtu < net->mtu) {
4253 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4254 sctp_pathmtu_adjustment(stcb, mtu, true);
4258 } else if (ro->ro_nh == NULL) {
4259 /* route was freed */
4260 if (net->ro._s_addr &&
4261 net->src_addr_selected) {
4262 sctp_free_ifa(net->ro._s_addr);
4263 net->ro._s_addr = NULL;
4265 net->src_addr_selected = 0;
4274 uint32_t flowlabel, flowinfo;
4275 struct ip6_hdr *ip6h;
4276 struct route_in6 ip6route;
4278 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4280 struct sockaddr_in6 lsa6_storage;
4282 u_short prev_port = 0;
4286 flowlabel = net->flowlabel;
4288 flowlabel = stcb->asoc.default_flowlabel;
4290 flowlabel = inp->sctp_ep.default_flowlabel;
4292 if (flowlabel == 0) {
4294 * This means especially, that it is not set
4295 * at the SCTP layer. So use the value from
4298 flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4300 flowlabel &= 0x000fffff;
4301 len = SCTP_MIN_OVERHEAD;
4303 len += sizeof(struct udphdr);
4305 newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4308 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4311 SCTP_ALIGN_TO_END(newm, len);
4312 SCTP_BUF_LEN(newm) = len;
4313 SCTP_BUF_NEXT(newm) = m;
4316 m->m_pkthdr.flowid = net->flowid;
4317 M_HASHTYPE_SET(m, net->flowtype);
4319 m->m_pkthdr.flowid = mflowid;
4320 M_HASHTYPE_SET(m, mflowtype);
4322 packet_length = sctp_calculate_len(m);
4324 ip6h = mtod(m, struct ip6_hdr *);
4325 /* protect *sin6 from overwrite */
4326 sin6 = (struct sockaddr_in6 *)to;
4330 /* KAME hack: embed scopeid */
4331 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4332 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4337 memset(&ip6route, 0, sizeof(ip6route));
4338 ro = (sctp_route_t *)&ip6route;
4339 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4341 ro = (sctp_route_t *)&net->ro;
4344 * We assume here that inp_flow is in host byte
4345 * order within the TCB!
4347 if (tos_value == 0) {
4349 * This means especially, that it is not set
4350 * at the SCTP layer. So use the value from
4353 tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4357 tos_value |= sctp_get_ect(stcb);
4361 flowinfo |= tos_value;
4363 flowinfo |= flowlabel;
4364 ip6h->ip6_flow = htonl(flowinfo);
4366 ip6h->ip6_nxt = IPPROTO_UDP;
4368 ip6h->ip6_nxt = IPPROTO_SCTP;
4370 ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4371 ip6h->ip6_dst = sin6->sin6_addr;
4374 * Add SRC address selection here: we can only reuse
4375 * to a limited degree the kame src-addr-sel, since
4376 * we can try their selection but it may not be
4379 memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4380 lsa6_tmp.sin6_family = AF_INET6;
4381 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4383 if (net && out_of_asoc_ok == 0) {
4384 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4385 sctp_free_ifa(net->ro._s_addr);
4386 net->ro._s_addr = NULL;
4387 net->src_addr_selected = 0;
4390 if (net->src_addr_selected == 0) {
4391 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4392 /* KAME hack: embed scopeid */
4393 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4394 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4398 /* Cache the source address */
4399 net->ro._s_addr = sctp_source_address_selection(inp,
4405 (void)sa6_recoverscope(sin6);
4406 net->src_addr_selected = 1;
4408 if (net->ro._s_addr == NULL) {
4409 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4410 net->src_addr_selected = 0;
4411 sctp_handle_no_route(stcb, net, so_locked);
4412 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4414 return (EHOSTUNREACH);
4416 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4418 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4419 /* KAME hack: embed scopeid */
4420 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4421 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4425 if (over_addr == NULL) {
4426 struct sctp_ifa *_lsrc;
4428 _lsrc = sctp_source_address_selection(inp, stcb, ro,
4432 if (_lsrc == NULL) {
4433 sctp_handle_no_route(stcb, net, so_locked);
4434 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4436 return (EHOSTUNREACH);
4438 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4439 sctp_free_ifa(_lsrc);
4441 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4442 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4444 (void)sa6_recoverscope(sin6);
4446 lsa6->sin6_port = inp->sctp_lport;
4448 if (ro->ro_nh == NULL) {
4450 * src addr selection failed to find a route
4451 * (or valid source addr), so we can't get
4454 sctp_handle_no_route(stcb, net, so_locked);
4455 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4457 return (EHOSTUNREACH);
4460 * XXX: sa6 may not have a valid sin6_scope_id in
4461 * the non-SCOPEDROUTING case.
4463 memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4464 lsa6_storage.sin6_family = AF_INET6;
4465 lsa6_storage.sin6_len = sizeof(lsa6_storage);
4466 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4467 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4468 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4473 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4474 lsa6_storage.sin6_port = inp->sctp_lport;
4475 lsa6 = &lsa6_storage;
4476 ip6h->ip6_src = lsa6->sin6_addr;
4479 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4480 sctp_handle_no_route(stcb, net, so_locked);
4481 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4483 return (EHOSTUNREACH);
4485 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4486 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4487 udp->uh_dport = port;
4488 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4490 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4492 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4495 sctphdr->src_port = src_port;
4496 sctphdr->dest_port = dest_port;
4497 sctphdr->v_tag = v_tag;
4498 sctphdr->checksum = 0;
4501 * We set the hop limit now since there is a good
4502 * chance that our ro pointer is now filled
4504 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4505 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4508 /* Copy to be sure something bad is not happening */
4509 sin6->sin6_addr = ip6h->ip6_dst;
4510 lsa6->sin6_addr = ip6h->ip6_src;
4513 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4514 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4515 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4516 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4517 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4519 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4521 * preserve the port and scope for link
4524 prev_scope = sin6->sin6_scope_id;
4525 prev_port = sin6->sin6_port;
4528 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4529 /* failed to prepend data, give up */
4531 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4534 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4536 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4537 SCTP_STAT_INCR(sctps_sendswcrc);
4538 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4539 udp->uh_sum = 0xffff;
4542 m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4543 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4544 SCTP_STAT_INCR(sctps_sendhwcrc);
4546 /* send it out. table id is taken from stcb */
4547 #ifdef SCTP_PACKET_LOGGING
4548 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4549 sctp_packet_log(o_pak);
4551 SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4552 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4554 /* for link local this must be done */
4555 sin6->sin6_scope_id = prev_scope;
4556 sin6->sin6_port = prev_port;
4558 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4560 UDPSTAT_INC(udps_opackets);
4562 SCTP_STAT_INCR(sctps_sendpackets);
4563 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4565 SCTP_STAT_INCR(sctps_senderrors);
4568 /* Now if we had a temp route free it */
4572 * PMTU check versus smallest asoc MTU goes
4575 if (ro->ro_nh == NULL) {
4576 /* Route was freed */
4577 if (net->ro._s_addr &&
4578 net->src_addr_selected) {
4579 sctp_free_ifa(net->ro._s_addr);
4580 net->ro._s_addr = NULL;
4582 net->src_addr_selected = 0;
4584 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4585 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4588 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4591 mtu -= sizeof(struct udphdr);
4593 if (mtu < net->mtu) {
4595 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4596 sctp_pathmtu_adjustment(stcb, mtu, false);
4600 } else if (ifp != NULL) {
4601 if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4602 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4603 sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4611 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4612 ((struct sockaddr *)to)->sa_family);
4614 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4620 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
4622 struct mbuf *m, *m_last;
4623 struct sctp_nets *net;
4624 struct sctp_init_chunk *init;
4625 struct sctp_supported_addr_param *sup_addr;
4626 struct sctp_adaptation_layer_indication *ali;
4627 struct sctp_zero_checksum_acceptable *zero_chksum;
4628 struct sctp_supported_chunk_types_param *pr_supported;
4629 struct sctp_paramhdr *ph;
4630 int cnt_inits_to = 0;
4632 uint16_t num_ext, chunk_len, padding_len, parameter_len;
4634 /* INIT's always go to the primary (and usually ONLY address) */
4635 net = stcb->asoc.primary_destination;
4637 net = TAILQ_FIRST(&stcb->asoc.nets);
4642 /* we confirm any address we send an INIT to */
4643 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4644 (void)sctp_set_primary_addr(stcb, NULL, net);
4646 /* we confirm any address we send an INIT to */
4647 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4649 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4651 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4653 * special hook, if we are sending to link local it will not
4654 * show up in our private address count.
4656 if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
4660 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4661 /* This case should not happen */
4662 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4665 /* start the INIT timer */
4666 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4668 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
4670 /* No memory, INIT timer will re-attempt. */
4671 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4674 chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
4676 /* Now lets put the chunk header in place */
4677 init = mtod(m, struct sctp_init_chunk *);
4678 /* now the chunk header */
4679 init->ch.chunk_type = SCTP_INITIATION;
4680 init->ch.chunk_flags = 0;
4681 /* fill in later from mbuf we build */
4682 init->ch.chunk_length = 0;
4683 /* place in my tag */
4684 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4685 /* set up some of the credits. */
4686 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4687 SCTP_MINIMAL_RWND));
4688 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4689 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4690 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4692 /* Adaptation layer indication parameter */
4693 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
4694 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
4695 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
4696 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4697 ali->ph.param_length = htons(parameter_len);
4698 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
4699 chunk_len += parameter_len;
4703 if (stcb->asoc.ecn_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_ECN_CAPABLE);
4707 ph->param_length = htons(parameter_len);
4708 chunk_len += parameter_len;
4711 /* PR-SCTP supported parameter */
4712 if (stcb->asoc.prsctp_supported == 1) {
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_PRSCTP_SUPPORTED);
4716 ph->param_length = htons(parameter_len);
4717 chunk_len += parameter_len;
4720 /* Zero checksum acceptable parameter */
4721 if (stcb->asoc.rcv_edmid != SCTP_EDMID_NONE) {
4722 parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
4723 zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
4724 zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
4725 zero_chksum->ph.param_length = htons(parameter_len);
4726 zero_chksum->edmid = htonl(stcb->asoc.rcv_edmid);
4727 chunk_len += parameter_len;
4730 /* Add NAT friendly parameter. */
4731 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4732 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4733 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4734 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4735 ph->param_length = htons(parameter_len);
4736 chunk_len += parameter_len;
4739 /* And now tell the peer which extensions we support */
4741 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
4742 if (stcb->asoc.prsctp_supported == 1) {
4743 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4744 if (stcb->asoc.idata_supported) {
4745 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
4748 if (stcb->asoc.auth_supported == 1) {
4749 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4751 if (stcb->asoc.asconf_supported == 1) {
4752 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4753 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4755 if (stcb->asoc.reconfig_supported == 1) {
4756 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4758 if (stcb->asoc.idata_supported) {
4759 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
4761 if (stcb->asoc.nrsack_supported == 1) {
4762 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4764 if (stcb->asoc.pktdrop_supported == 1) {
4765 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4768 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
4769 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4770 pr_supported->ph.param_length = htons(parameter_len);
4771 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4772 chunk_len += parameter_len;
4774 /* add authentication parameters */
4775 if (stcb->asoc.auth_supported) {
4776 /* attach RANDOM parameter, if available */
4777 if (stcb->asoc.authinfo.random != NULL) {
4778 struct sctp_auth_random *randp;
4780 if (padding_len > 0) {
4781 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4782 chunk_len += padding_len;
4785 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
4786 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
4787 /* random key already contains the header */
4788 memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
4789 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4790 chunk_len += parameter_len;
4792 /* add HMAC_ALGO parameter */
4793 if (stcb->asoc.local_hmacs != NULL) {
4794 struct sctp_auth_hmac_algo *hmacs;
4796 if (padding_len > 0) {
4797 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4798 chunk_len += padding_len;
4801 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
4802 parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
4803 stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
4804 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4805 hmacs->ph.param_length = htons(parameter_len);
4806 sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
4807 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4808 chunk_len += parameter_len;
4810 /* add CHUNKS parameter */
4811 if (stcb->asoc.local_auth_chunks != NULL) {
4812 struct sctp_auth_chunk_list *chunks;
4814 if (padding_len > 0) {
4815 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4816 chunk_len += padding_len;
4819 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
4820 parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
4821 sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
4822 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4823 chunks->ph.param_length = htons(parameter_len);
4824 sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
4825 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4826 chunk_len += parameter_len;
4830 /* now any cookie time extensions */
4831 if (stcb->asoc.cookie_preserve_req > 0) {
4832 struct sctp_cookie_perserve_param *cookie_preserve;
4834 if (padding_len > 0) {
4835 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4836 chunk_len += padding_len;
4839 parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
4840 cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t)+chunk_len);
4841 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4842 cookie_preserve->ph.param_length = htons(parameter_len);
4843 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4844 stcb->asoc.cookie_preserve_req = 0;
4845 chunk_len += parameter_len;
4848 if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
4851 if (padding_len > 0) {
4852 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4853 chunk_len += padding_len;
4856 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4857 if (stcb->asoc.scope.ipv4_addr_legal) {
4858 parameter_len += (uint16_t)sizeof(uint16_t);
4860 if (stcb->asoc.scope.ipv6_addr_legal) {
4861 parameter_len += (uint16_t)sizeof(uint16_t);
4863 sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t)+chunk_len);
4864 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4865 sup_addr->ph.param_length = htons(parameter_len);
4867 if (stcb->asoc.scope.ipv4_addr_legal) {
4868 sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
4870 if (stcb->asoc.scope.ipv6_addr_legal) {
4871 sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
4873 padding_len = 4 - 2 * i;
4874 chunk_len += parameter_len;
4877 SCTP_BUF_LEN(m) = chunk_len;
4878 /* now the addresses */
4880 * To optimize this we could put the scoping stuff into a structure
4881 * and remove the individual uint8's from the assoc structure. Then
4882 * we could just sifa in the address within the stcb. But for now
4883 * this is a quick hack to get the address stuff teased apart.
4885 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
4887 &padding_len, &chunk_len);
4889 init->ch.chunk_length = htons(chunk_len);
4890 if (padding_len > 0) {
4891 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
4896 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4897 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
4898 (struct sockaddr *)&net->ro._l_addr,
4899 m, 0, NULL, 0, 0, 0, 0,
4900 inp->sctp_lport, stcb->rport, htonl(0),
4903 false, so_locked))) {
4904 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
4905 if (error == ENOBUFS) {
4906 stcb->asoc.ifp_had_enobuf = 1;
4907 SCTP_STAT_INCR(sctps_lowlevelerr);
4910 stcb->asoc.ifp_had_enobuf = 0;
4912 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4913 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4917 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4918 int param_offset, int *abort_processing,
4919 struct sctp_chunkhdr *cp,
4924 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4925 * being equal to the beginning of the params i.e. (iphlen +
4926 * sizeof(struct sctp_init_msg) parse through the parameters to the
4927 * end of the mbuf verifying that all parameters are known.
4929 * For unknown parameters build and return a mbuf with
4930 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4931 * processing this chunk stop, and set *abort_processing to 1.
4933 * By having param_offset be pre-set to where parameters begin it is
4934 * hoped that this routine may be reused in the future by new
4937 struct sctp_paramhdr *phdr, params;
4939 struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
4940 int at, limit, pad_needed;
4941 uint16_t ptype, plen, padded_size;
4943 *abort_processing = 0;
4944 if (cookie_found != NULL) {
4948 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4953 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4954 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4955 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4956 ptype = ntohs(phdr->param_type);
4957 plen = ntohs(phdr->param_length);
4958 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4959 /* wacked parameter */
4960 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4963 limit -= SCTP_SIZE32(plen);
4965 * All parameters for all chunks that we know/understand are
4966 * listed here. We process them other places and make
4967 * appropriate stop actions per the upper bits. However this
4968 * is the generic routine processor's can call to get back
4969 * an operr.. to either incorporate (init-ack) or send.
4971 padded_size = SCTP_SIZE32(plen);
4973 /* Param's with variable size */
4974 case SCTP_HEARTBEAT_INFO:
4975 case SCTP_UNRECOG_PARAM:
4976 case SCTP_ERROR_CAUSE_IND:
4980 case SCTP_STATE_COOKIE:
4981 if (cookie_found != NULL) {
4986 /* Param's with variable size within a range */
4987 case SCTP_CHUNK_LIST:
4988 case SCTP_SUPPORTED_CHUNK_EXT:
4989 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4990 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4995 case SCTP_SUPPORTED_ADDRTYPE:
4996 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4997 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
5003 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
5004 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
5009 case SCTP_SET_PRIM_ADDR:
5010 case SCTP_DEL_IP_ADDRESS:
5011 case SCTP_ADD_IP_ADDRESS:
5012 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
5013 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
5014 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
5019 /* Param's with a fixed size */
5020 case SCTP_IPV4_ADDRESS:
5021 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5022 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5027 case SCTP_IPV6_ADDRESS:
5028 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5029 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5034 case SCTP_COOKIE_PRESERVE:
5035 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5036 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5041 case SCTP_HAS_NAT_SUPPORT:
5044 case SCTP_PRSCTP_SUPPORTED:
5045 if (padded_size != sizeof(struct sctp_paramhdr)) {
5046 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
5051 case SCTP_ECN_CAPABLE:
5052 if (padded_size != sizeof(struct sctp_paramhdr)) {
5053 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5058 case SCTP_ULP_ADAPTATION:
5059 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5060 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5065 case SCTP_SUCCESS_REPORT:
5066 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5067 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5072 case SCTP_HOSTNAME_ADDRESS:
5074 /* Hostname parameters are deprecated. */
5075 struct sctp_gen_error_cause *cause;
5078 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5079 *abort_processing = 1;
5080 sctp_m_freem(op_err);
5084 l_len = SCTP_MIN_OVERHEAD;
5086 l_len = SCTP_MIN_V4_OVERHEAD;
5088 l_len += sizeof(struct sctp_chunkhdr);
5089 l_len += sizeof(struct sctp_gen_error_cause);
5090 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5091 if (op_err != NULL) {
5093 * Pre-reserve space for IP, SCTP,
5097 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5099 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5101 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5102 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5103 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5104 cause = mtod(op_err, struct sctp_gen_error_cause *);
5105 cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5106 cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5107 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5108 if (SCTP_BUF_NEXT(op_err) == NULL) {
5109 sctp_m_freem(op_err);
5118 * we do not recognize the parameter figure out what
5121 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5122 if ((ptype & 0x4000) == 0x4000) {
5123 /* Report bit is set?? */
5124 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5125 if (op_err == NULL) {
5128 /* Ok need to try to get an mbuf */
5130 l_len = SCTP_MIN_OVERHEAD;
5132 l_len = SCTP_MIN_V4_OVERHEAD;
5134 l_len += sizeof(struct sctp_chunkhdr);
5135 l_len += sizeof(struct sctp_paramhdr);
5136 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5138 SCTP_BUF_LEN(op_err) = 0;
5140 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5142 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5144 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5145 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5146 op_err_last = op_err;
5149 if (op_err != NULL) {
5150 /* If we have space */
5151 struct sctp_paramhdr *param;
5153 if (pad_needed > 0) {
5154 op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5156 if (op_err_last == NULL) {
5157 sctp_m_freem(op_err);
5160 goto more_processing;
5162 if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5163 m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5164 if (m_tmp == NULL) {
5165 sctp_m_freem(op_err);
5168 goto more_processing;
5170 SCTP_BUF_LEN(m_tmp) = 0;
5171 SCTP_BUF_NEXT(m_tmp) = NULL;
5172 SCTP_BUF_NEXT(op_err_last) = m_tmp;
5173 op_err_last = m_tmp;
5175 param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t)+SCTP_BUF_LEN(op_err_last));
5176 param->param_type = htons(SCTP_UNRECOG_PARAM);
5177 param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5178 SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5179 SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5180 if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5181 sctp_m_freem(op_err);
5184 goto more_processing;
5186 while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5187 op_err_last = SCTP_BUF_NEXT(op_err_last);
5190 if (plen % 4 != 0) {
5191 pad_needed = 4 - (plen % 4);
5198 if ((ptype & 0x8000) == 0x0000) {
5199 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5202 /* skip this chunk and continue processing */
5203 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5204 at += SCTP_SIZE32(plen);
5208 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
5212 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5213 *abort_processing = 1;
5214 sctp_m_freem(op_err);
5218 struct sctp_paramhdr *param;
5221 l_len = SCTP_MIN_OVERHEAD;
5223 l_len = SCTP_MIN_V4_OVERHEAD;
5225 l_len += sizeof(struct sctp_chunkhdr);
5226 l_len += (2 * sizeof(struct sctp_paramhdr));
5227 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5229 SCTP_BUF_LEN(op_err) = 0;
5231 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5233 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5235 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5236 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5237 SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5238 param = mtod(op_err, struct sctp_paramhdr *);
5239 param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5240 param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5242 param->param_type = htons(ptype);
5243 param->param_length = htons(plen);
5250 * Given a INIT chunk, look through the parameters to verify that there
5251 * are no new addresses.
5252 * Return true, if there is a new address or there is a problem parsing
5253 the parameters. Provide an optional error cause used when sending an ABORT.
5254 * Return false, if there are no new addresses and there is no problem in
5255 parameter processing.
5258 sctp_are_there_new_addresses(struct sctp_association *asoc,
5259 struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5260 struct mbuf **op_err)
5262 struct sockaddr *sa_touse;
5263 struct sockaddr *sa;
5264 struct sctp_paramhdr *phdr, params;
5265 struct sctp_nets *net;
5267 struct sockaddr_in sin4, *sa4;
5270 struct sockaddr_in6 sin6, *sa6;
5272 uint16_t ptype, plen;
5273 bool fnd, check_src;
5277 memset(&sin4, 0, sizeof(sin4));
5278 sin4.sin_family = AF_INET;
5279 sin4.sin_len = sizeof(sin4);
5282 memset(&sin6, 0, sizeof(sin6));
5283 sin6.sin6_family = AF_INET6;
5284 sin6.sin6_len = sizeof(sin6);
5286 /* First what about the src address of the pkt ? */
5288 switch (src->sa_family) {
5291 if (asoc->scope.ipv4_addr_legal) {
5298 if (asoc->scope.ipv6_addr_legal) {
5309 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5310 sa = (struct sockaddr *)&net->ro._l_addr;
5311 if (sa->sa_family == src->sa_family) {
5313 if (sa->sa_family == AF_INET) {
5314 struct sockaddr_in *src4;
5316 sa4 = (struct sockaddr_in *)sa;
5317 src4 = (struct sockaddr_in *)src;
5318 if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5325 if (sa->sa_family == AF_INET6) {
5326 struct sockaddr_in6 *src6;
5328 sa6 = (struct sockaddr_in6 *)sa;
5329 src6 = (struct sockaddr_in6 *)src;
5330 if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5340 * If sending an ABORT in case of an additional
5341 * address, don't use the new address error cause.
5342 * This looks no different than if no listener was
5345 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5349 /* Ok so far lets munge through the rest of the packet */
5350 offset += sizeof(struct sctp_init_chunk);
5351 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5354 ptype = ntohs(phdr->param_type);
5355 plen = ntohs(phdr->param_length);
5356 if (offset + plen > limit) {
5357 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5360 if (plen < sizeof(struct sctp_paramhdr)) {
5361 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5366 case SCTP_IPV4_ADDRESS:
5368 struct sctp_ipv4addr_param *p4, p4_buf;
5370 if (plen != sizeof(struct sctp_ipv4addr_param)) {
5371 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5374 phdr = sctp_get_next_param(in_initpkt, offset,
5375 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5377 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5380 if (asoc->scope.ipv4_addr_legal) {
5381 p4 = (struct sctp_ipv4addr_param *)phdr;
5382 sin4.sin_addr.s_addr = p4->addr;
5383 sa_touse = (struct sockaddr *)&sin4;
5389 case SCTP_IPV6_ADDRESS:
5391 struct sctp_ipv6addr_param *p6, p6_buf;
5393 if (plen != sizeof(struct sctp_ipv6addr_param)) {
5394 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5397 phdr = sctp_get_next_param(in_initpkt, offset,
5398 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5400 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5403 if (asoc->scope.ipv6_addr_legal) {
5404 p6 = (struct sctp_ipv6addr_param *)phdr;
5405 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5407 sa_touse = (struct sockaddr *)&sin6;
5417 /* ok, sa_touse points to one to check */
5419 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5420 sa = (struct sockaddr *)&net->ro._l_addr;
5421 if (sa->sa_family != sa_touse->sa_family) {
5425 if (sa->sa_family == AF_INET) {
5426 sa4 = (struct sockaddr_in *)sa;
5427 if (sa4->sin_addr.s_addr ==
5428 sin4.sin_addr.s_addr) {
5435 if (sa->sa_family == AF_INET6) {
5436 sa6 = (struct sockaddr_in6 *)sa;
5437 if (SCTP6_ARE_ADDR_EQUAL(
5447 * If sending an ABORT in case of an
5448 * additional address, don't use the new
5449 * address error cause. This looks no
5450 * different than if no listener was
5453 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5457 offset += SCTP_SIZE32(plen);
5458 if (offset >= limit) {
5461 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5467 * Given a MBUF chain that was sent into us containing an INIT. Build a
5468 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5469 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5470 * message (i.e. the struct sctp_init_msg).
5473 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5474 struct sctp_nets *src_net, struct mbuf *init_pkt,
5475 int iphlen, int offset,
5476 struct sockaddr *src, struct sockaddr *dst,
5477 struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5478 uint8_t mflowtype, uint32_t mflowid,
5479 uint32_t vrf_id, uint16_t port)
5481 struct sctp_association *asoc;
5482 struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5483 struct sctp_init_ack_chunk *initack;
5484 struct sctp_adaptation_layer_indication *ali;
5485 struct sctp_zero_checksum_acceptable *zero_chksum;
5486 struct sctp_supported_chunk_types_param *pr_supported;
5487 struct sctp_paramhdr *ph;
5488 union sctp_sockstore *over_addr;
5489 struct sctp_scoping scp;
5492 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5493 struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5494 struct sockaddr_in *sin;
5497 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
5498 struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
5499 struct sockaddr_in6 *sin6;
5501 struct sockaddr *to;
5502 struct sctp_state_cookie stc;
5503 struct sctp_nets *net = NULL;
5504 uint8_t *signature = NULL;
5505 int cnt_inits_to = 0;
5506 uint16_t his_limit, i_want;
5508 int nat_friendly = 0;
5511 uint16_t num_ext, chunk_len, padding_len, parameter_len;
5518 if ((asoc != NULL) &&
5519 (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
5520 if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
5522 * new addresses, out of here in non-cookie-wait
5525 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5526 mflowtype, mflowid, inp->fibnum,
5530 if (src_net != NULL && (src_net->port != port)) {
5532 * change of remote encapsulation port, out of here
5533 * in non-cookie-wait states
5535 * Send an ABORT, without an specific error cause.
5536 * This looks no different than if no listener was
5539 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5540 "Remote encapsulation port changed");
5541 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5542 mflowtype, mflowid, inp->fibnum,
5548 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5549 (offset + sizeof(struct sctp_init_chunk)),
5551 (struct sctp_chunkhdr *)init_chk,
5552 &nat_friendly, NULL);
5555 if (op_err == NULL) {
5556 char msg[SCTP_DIAG_INFO_LEN];
5558 SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
5559 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5562 sctp_send_abort(init_pkt, iphlen, src, dst, sh,
5563 init_chk->init.initiate_tag, op_err,
5564 mflowtype, mflowid, inp->fibnum,
5568 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
5570 /* No memory, INIT timer will re-attempt. */
5571 sctp_m_freem(op_err);
5574 chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
5578 * We might not overwrite the identification[] completely and on
5579 * some platforms time_entered will contain some padding. Therefore
5580 * zero out the cookie to avoid putting uninitialized memory on the
5583 memset(&stc, 0, sizeof(struct sctp_state_cookie));
5585 /* the time I built cookie */
5586 (void)SCTP_GETTIME_TIMEVAL(&now);
5587 stc.time_entered.tv_sec = now.tv_sec;
5588 stc.time_entered.tv_usec = now.tv_usec;
5590 /* populate any tie tags */
5592 /* unlock before tag selections */
5593 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5594 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5595 stc.cookie_life = asoc->cookie_life;
5596 net = asoc->primary_destination;
5598 stc.tie_tag_my_vtag = 0;
5599 stc.tie_tag_peer_vtag = 0;
5600 /* life I will award this cookie */
5601 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5604 /* copy in the ports for later check */
5605 stc.myport = sh->dest_port;
5606 stc.peerport = sh->src_port;
5609 * If we wanted to honor cookie life extensions, we would add to
5610 * stc.cookie_life. For now we should NOT honor any extension
5612 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5613 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5614 stc.ipv6_addr_legal = 1;
5615 if (SCTP_IPV6_V6ONLY(inp)) {
5616 stc.ipv4_addr_legal = 0;
5618 stc.ipv4_addr_legal = 1;
5621 stc.ipv6_addr_legal = 0;
5622 stc.ipv4_addr_legal = 1;
5627 switch (dst->sa_family) {
5631 /* lookup address */
5632 stc.address[0] = src4->sin_addr.s_addr;
5636 stc.addr_type = SCTP_IPV4_ADDRESS;
5637 /* local from address */
5638 stc.laddress[0] = dst4->sin_addr.s_addr;
5639 stc.laddress[1] = 0;
5640 stc.laddress[2] = 0;
5641 stc.laddress[3] = 0;
5642 stc.laddr_type = SCTP_IPV4_ADDRESS;
5643 /* scope_id is only for v6 */
5645 if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
5646 (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
5649 /* Must use the address in this case */
5650 if (sctp_is_address_on_local_host(src, vrf_id)) {
5651 stc.loopback_scope = 1;
5654 stc.local_scope = 0;
5662 stc.addr_type = SCTP_IPV6_ADDRESS;
5663 memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
5664 stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
5665 if (sctp_is_address_on_local_host(src, vrf_id)) {
5666 stc.loopback_scope = 1;
5667 stc.local_scope = 0;
5670 } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
5671 IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
5673 * If the new destination or source
5674 * is a LINK_LOCAL we must have
5675 * common both site and local scope.
5676 * Don't set local scope though
5677 * since we must depend on the
5678 * source to be added implicitly. We
5679 * cannot assure just because we
5680 * share one link that all links are
5683 stc.local_scope = 0;
5687 * we start counting for the private
5688 * address stuff at 1. since the
5689 * link local we source from won't
5690 * show up in our scoped count.
5694 * pull out the scope_id from
5697 } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
5698 IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
5700 * If the new destination or source
5701 * is SITE_LOCAL then we must have
5702 * site scope in common.
5706 memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
5707 stc.laddr_type = SCTP_IPV6_ADDRESS;
5717 /* set the scope per the existing tcb */
5720 struct sctp_nets *lnet;
5723 stc.loopback_scope = asoc->scope.loopback_scope;
5724 stc.ipv4_scope = asoc->scope.ipv4_local_scope;
5725 stc.site_scope = asoc->scope.site_scope;
5726 stc.local_scope = asoc->scope.local_scope;
5728 /* Why do we not consider IPv4 LL addresses? */
5729 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5730 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5731 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5733 * if we have a LL address, start
5741 /* use the net pointer */
5742 to = (struct sockaddr *)&net->ro._l_addr;
5743 switch (to->sa_family) {
5746 sin = (struct sockaddr_in *)to;
5747 stc.address[0] = sin->sin_addr.s_addr;
5751 stc.addr_type = SCTP_IPV4_ADDRESS;
5752 if (net->src_addr_selected == 0) {
5754 * strange case here, the INIT should have
5755 * did the selection.
5757 net->ro._s_addr = sctp_source_address_selection(inp,
5758 stcb, (sctp_route_t *)&net->ro,
5760 if (net->ro._s_addr == NULL) {
5761 sctp_m_freem(op_err);
5766 net->src_addr_selected = 1;
5768 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5769 stc.laddress[1] = 0;
5770 stc.laddress[2] = 0;
5771 stc.laddress[3] = 0;
5772 stc.laddr_type = SCTP_IPV4_ADDRESS;
5773 /* scope_id is only for v6 */
5779 sin6 = (struct sockaddr_in6 *)to;
5780 memcpy(&stc.address, &sin6->sin6_addr,
5781 sizeof(struct in6_addr));
5782 stc.addr_type = SCTP_IPV6_ADDRESS;
5783 stc.scope_id = sin6->sin6_scope_id;
5784 if (net->src_addr_selected == 0) {
5786 * strange case here, the INIT should have
5787 * done the selection.
5789 net->ro._s_addr = sctp_source_address_selection(inp,
5790 stcb, (sctp_route_t *)&net->ro,
5792 if (net->ro._s_addr == NULL) {
5793 sctp_m_freem(op_err);
5798 net->src_addr_selected = 1;
5800 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5801 sizeof(struct in6_addr));
5802 stc.laddr_type = SCTP_IPV6_ADDRESS;
5808 stc.rcv_edmid = asoc->rcv_edmid;
5810 stc.rcv_edmid = inp->rcv_edmid;
5812 /* Now lets put the SCTP header in place */
5813 initack = mtod(m, struct sctp_init_ack_chunk *);
5814 /* Save it off for quick ref */
5815 stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
5817 memcpy(stc.identification, SCTP_VERSION_STRING,
5818 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5819 memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5820 /* now the chunk header */
5821 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5822 initack->ch.chunk_flags = 0;
5823 /* fill in later from mbuf we build */
5824 initack->ch.chunk_length = 0;
5825 /* place in my tag */
5826 if ((asoc != NULL) &&
5827 ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
5828 (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
5829 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
5830 /* re-use the v-tags and init-seq here */
5831 initack->init.initiate_tag = htonl(asoc->my_vtag);
5832 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5834 uint32_t vtag, itsn;
5837 atomic_add_int(&asoc->refcnt, 1);
5838 SCTP_TCB_UNLOCK(stcb);
5840 SCTP_INP_INFO_RLOCK();
5841 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5842 SCTP_INP_INFO_RUNLOCK();
5843 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5845 * Got a duplicate vtag on some guy behind a
5846 * nat make sure we don't use it.
5850 initack->init.initiate_tag = htonl(vtag);
5851 /* get a TSN to use too */
5852 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5853 initack->init.initial_tsn = htonl(itsn);
5854 SCTP_TCB_LOCK(stcb);
5855 atomic_subtract_int(&asoc->refcnt, 1);
5857 SCTP_INP_INCR_REF(inp);
5858 SCTP_INP_RUNLOCK(inp);
5859 SCTP_INP_INFO_RLOCK();
5860 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5861 SCTP_INP_INFO_RUNLOCK();
5862 initack->init.initiate_tag = htonl(vtag);
5863 /* get a TSN to use too */
5864 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5865 SCTP_INP_RLOCK(inp);
5866 SCTP_INP_DECR_REF(inp);
5869 /* save away my tag to */
5870 stc.my_vtag = initack->init.initiate_tag;
5872 /* set up some of the credits. */
5873 so = inp->sctp_socket;
5875 /* memory problem */
5876 sctp_m_freem(op_err);
5880 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5882 /* set what I want */
5883 his_limit = ntohs(init_chk->init.num_inbound_streams);
5884 /* choose what I want */
5886 if (asoc->streamoutcnt > asoc->pre_open_streams) {
5887 i_want = asoc->streamoutcnt;
5889 i_want = asoc->pre_open_streams;
5892 i_want = inp->sctp_ep.pre_open_stream_count;
5894 if (his_limit < i_want) {
5895 /* I Want more :< */
5896 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5898 /* I can have what I want :> */
5899 initack->init.num_outbound_streams = htons(i_want);
5901 /* tell him his limit. */
5902 initack->init.num_inbound_streams =
5903 htons(inp->sctp_ep.max_open_streams_intome);
5905 /* adaptation layer indication parameter */
5906 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5907 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5908 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
5909 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5910 ali->ph.param_length = htons(parameter_len);
5911 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5912 chunk_len += parameter_len;
5916 if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
5917 ((asoc == NULL) && (inp->ecn_supported == 1))) {
5918 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5919 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5920 ph->param_type = htons(SCTP_ECN_CAPABLE);
5921 ph->param_length = htons(parameter_len);
5922 chunk_len += parameter_len;
5925 /* PR-SCTP supported parameter */
5926 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5927 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5928 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5929 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5930 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5931 ph->param_length = htons(parameter_len);
5932 chunk_len += parameter_len;
5935 /* Zero checksum acceptable parameter */
5936 if (((asoc != NULL) && (asoc->rcv_edmid != SCTP_EDMID_NONE)) ||
5937 ((asoc == NULL) && (inp->rcv_edmid != SCTP_EDMID_NONE))) {
5938 parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
5939 zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
5940 zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
5941 zero_chksum->ph.param_length = htons(parameter_len);
5943 zero_chksum->edmid = htonl(asoc->rcv_edmid);
5945 zero_chksum->edmid = htonl(inp->rcv_edmid);
5947 chunk_len += parameter_len;
5950 /* Add NAT friendly parameter */
5952 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5953 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5954 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5955 ph->param_length = htons(parameter_len);
5956 chunk_len += parameter_len;
5959 /* And now tell the peer which extensions we support */
5961 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
5962 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5963 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5964 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5965 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5966 ((asoc == NULL) && (inp->idata_supported == 1))) {
5967 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5970 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5971 ((asoc == NULL) && (inp->auth_supported == 1))) {
5972 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5974 if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
5975 ((asoc == NULL) && (inp->asconf_supported == 1))) {
5976 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5977 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5979 if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
5980 ((asoc == NULL) && (inp->reconfig_supported == 1))) {
5981 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5983 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5984 ((asoc == NULL) && (inp->idata_supported == 1))) {
5985 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5987 if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
5988 ((asoc == NULL) && (inp->nrsack_supported == 1))) {
5989 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5991 if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
5992 ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
5993 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5996 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5997 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5998 pr_supported->ph.param_length = htons(parameter_len);
5999 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6000 chunk_len += parameter_len;
6003 /* add authentication parameters */
6004 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
6005 ((asoc == NULL) && (inp->auth_supported == 1))) {
6006 struct sctp_auth_random *randp;
6007 struct sctp_auth_hmac_algo *hmacs;
6008 struct sctp_auth_chunk_list *chunks;
6010 if (padding_len > 0) {
6011 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6012 chunk_len += padding_len;
6015 /* generate and add RANDOM parameter */
6016 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
6017 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
6018 SCTP_AUTH_RANDOM_SIZE_DEFAULT;
6019 randp->ph.param_type = htons(SCTP_RANDOM);
6020 randp->ph.param_length = htons(parameter_len);
6021 SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
6022 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6023 chunk_len += parameter_len;
6025 if (padding_len > 0) {
6026 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6027 chunk_len += padding_len;
6030 /* add HMAC_ALGO parameter */
6031 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
6032 parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
6033 sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
6034 (uint8_t *)hmacs->hmac_ids);
6035 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6036 hmacs->ph.param_length = htons(parameter_len);
6037 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6038 chunk_len += parameter_len;
6040 if (padding_len > 0) {
6041 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6042 chunk_len += padding_len;
6045 /* add CHUNKS parameter */
6046 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
6047 parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6048 sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6049 chunks->chunk_types);
6050 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6051 chunks->ph.param_length = htons(parameter_len);
6052 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6053 chunk_len += parameter_len;
6055 SCTP_BUF_LEN(m) = chunk_len;
6057 /* now the addresses */
6059 * To optimize this we could put the scoping stuff into a structure
6060 * and remove the individual uint8's from the stc structure. Then we
6061 * could just sifa in the address within the stc.. but for now this
6062 * is a quick hack to get the address stuff teased apart.
6064 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6065 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6066 scp.loopback_scope = stc.loopback_scope;
6067 scp.ipv4_local_scope = stc.ipv4_scope;
6068 scp.local_scope = stc.local_scope;
6069 scp.site_scope = stc.site_scope;
6070 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6072 &padding_len, &chunk_len);
6073 /* padding_len can only be positive, if no addresses have been added */
6074 if (padding_len > 0) {
6075 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6076 chunk_len += padding_len;
6077 SCTP_BUF_LEN(m) += padding_len;
6081 /* tack on the operational error if present */
6084 for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6085 parameter_len += SCTP_BUF_LEN(m_tmp);
6087 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6088 SCTP_BUF_NEXT(m_last) = op_err;
6089 while (SCTP_BUF_NEXT(m_last) != NULL) {
6090 m_last = SCTP_BUF_NEXT(m_last);
6092 chunk_len += parameter_len;
6094 if (padding_len > 0) {
6095 m_last = sctp_add_pad_tombuf(m_last, padding_len);
6096 if (m_last == NULL) {
6097 /* Houston we have a problem, no space */
6101 chunk_len += padding_len;
6104 /* Now we must build a cookie */
6105 m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6106 if (m_cookie == NULL) {
6107 /* memory problem */
6111 /* Now append the cookie to the end and update the space/size */
6112 SCTP_BUF_NEXT(m_last) = m_cookie;
6114 for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6115 parameter_len += SCTP_BUF_LEN(m_tmp);
6116 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6120 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6121 chunk_len += parameter_len;
6124 * Place in the size, but we don't include the last pad (if any) in
6127 initack->ch.chunk_length = htons(chunk_len);
6130 * Time to sign the cookie, we don't sign over the cookie signature
6131 * though thus we set trailer.
6133 (void)sctp_hmac_m(SCTP_HMAC,
6134 (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6135 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6136 (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6138 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6139 * here since the timer will drive a retranmission.
6141 if (padding_len > 0) {
6142 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6147 if (stc.loopback_scope) {
6148 over_addr = (union sctp_sockstore *)dst;
6153 if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6155 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6158 false, /* XXXMT: Improve this! */
6159 SCTP_SO_NOT_LOCKED))) {
6160 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6161 if (error == ENOBUFS) {
6163 asoc->ifp_had_enobuf = 1;
6165 SCTP_STAT_INCR(sctps_lowlevelerr);
6169 asoc->ifp_had_enobuf = 0;
6172 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6176 sctp_prune_prsctp(struct sctp_tcb *stcb,
6177 struct sctp_association *asoc,
6178 struct sctp_nonpad_sndrcvinfo *srcv,
6182 struct sctp_tmit_chunk *chk, *nchk;
6184 SCTP_TCB_LOCK_ASSERT(stcb);
6185 if ((asoc->prsctp_supported) &&
6186 (asoc->sent_queue_cnt_removeable > 0)) {
6187 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6189 * Look for chunks marked with the PR_SCTP flag AND
6190 * the buffer space flag. If the one being sent is
6191 * equal or greater priority then purge the old one
6192 * and free some space.
6194 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6196 * This one is PR-SCTP AND buffer space
6199 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6201 * Lower numbers equates to higher
6202 * priority. So if the one we are
6203 * looking at has a larger priority,
6204 * we want to drop the data and NOT
6209 * We release the book_size
6210 * if the mbuf is here
6215 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6219 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6222 freed_spc += ret_spc;
6223 if (freed_spc >= dataout) {
6226 } /* if chunk was present */
6227 } /* if of sufficient priority */
6228 } /* if chunk has enabled */
6229 } /* tailqforeach */
6231 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6232 /* Here we must move to the sent queue and mark */
6233 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6234 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6237 * We release the book_size
6238 * if the mbuf is here
6242 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6245 freed_spc += ret_spc;
6246 if (freed_spc >= dataout) {
6249 } /* end if chk->data */
6250 } /* end if right class */
6251 } /* end if chk pr-sctp */
6252 } /* tailqforeachsafe (chk) */
6253 } /* if enabled in asoc */
6257 sctp_get_frag_point(struct sctp_tcb *stcb)
6259 struct sctp_association *asoc;
6260 uint32_t frag_point, overhead;
6263 /* Consider IP header and SCTP common header. */
6264 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6265 overhead = SCTP_MIN_OVERHEAD;
6267 overhead = SCTP_MIN_V4_OVERHEAD;
6269 /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6270 if (asoc->idata_supported) {
6271 overhead += sizeof(struct sctp_idata_chunk);
6272 if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6273 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6276 overhead += sizeof(struct sctp_data_chunk);
6277 if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6278 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6281 KASSERT(overhead % 4 == 0,
6282 ("overhead (%u) not a multiple of 4", overhead));
6283 /* Consider padding. */
6284 if (asoc->smallest_mtu % 4 > 0) {
6285 overhead += (asoc->smallest_mtu % 4);
6287 KASSERT(asoc->smallest_mtu > overhead,
6288 ("Association MTU (%u) too small for overhead (%u)",
6289 asoc->smallest_mtu, overhead));
6290 frag_point = asoc->smallest_mtu - overhead;
6291 KASSERT(frag_point % 4 == 0,
6292 ("frag_point (%u) not a multiple of 4", frag_point));
6293 /* Honor MAXSEG socket option. */
6294 if ((asoc->sctp_frag_point > 0) &&
6295 (asoc->sctp_frag_point < frag_point)) {
6296 frag_point = asoc->sctp_frag_point;
6298 return (frag_point);
6302 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6305 * We assume that the user wants PR_SCTP_TTL if the user provides a
6306 * positive lifetime but does not specify any PR_SCTP policy.
6308 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6309 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6310 } else if (sp->timetolive > 0) {
6311 sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6312 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6316 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6317 case CHUNK_FLAGS_PR_SCTP_BUF:
6319 * Time to live is a priority stored in tv_sec when doing
6320 * the buffer drop thing.
6322 sp->ts.tv_sec = sp->timetolive;
6325 case CHUNK_FLAGS_PR_SCTP_TTL:
6329 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6330 tv.tv_sec = sp->timetolive / 1000;
6331 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6333 * TODO sctp_constants.h needs alternative time
6334 * macros when _KERNEL is undefined.
6336 timevaladd(&sp->ts, &tv);
6339 case CHUNK_FLAGS_PR_SCTP_RTX:
6341 * Time to live is a the number or retransmissions stored in
6344 sp->ts.tv_sec = sp->timetolive;
6348 SCTPDBG(SCTP_DEBUG_USRREQ1,
6349 "Unknown PR_SCTP policy %u.\n",
6350 PR_SCTP_POLICY(sp->sinfo_flags));
6356 sctp_msg_append(struct sctp_tcb *stcb,
6357 struct sctp_nets *net,
6359 struct sctp_nonpad_sndrcvinfo *srcv)
6363 struct sctp_stream_queue_pending *sp = NULL;
6364 struct sctp_stream_out *strm;
6366 SCTP_TCB_LOCK_ASSERT(stcb);
6369 * Given an mbuf chain, put it into the association send queue and
6370 * place it on the wheel
6372 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6373 /* Invalid stream number */
6374 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6378 if ((stcb->asoc.stream_locked) &&
6379 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6380 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6384 if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6385 (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6387 * Can't queue any data while stream reset is underway.
6389 if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
6396 /* Now can we send this? */
6397 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
6398 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6399 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6400 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6401 /* got data while shutting down */
6402 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
6406 sctp_alloc_a_strmoq(stcb, sp);
6408 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6412 sp->sinfo_flags = srcv->sinfo_flags;
6413 sp->timetolive = srcv->sinfo_timetolive;
6414 sp->ppid = srcv->sinfo_ppid;
6415 sp->context = srcv->sinfo_context;
6417 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6419 atomic_add_int(&sp->net->ref_count, 1);
6423 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6424 sp->sid = srcv->sinfo_stream;
6425 sp->msg_is_complete = 1;
6426 sp->sender_all_done = 1;
6429 sp->tail_mbuf = NULL;
6430 sctp_set_prsctp_policy(sp);
6432 * We could in theory (for sendall) sifa the length in, but we would
6433 * still have to hunt through the chain since we need to setup the
6437 for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6438 if (SCTP_BUF_NEXT(at) == NULL)
6440 sp->length += SCTP_BUF_LEN(at);
6442 if (srcv->sinfo_keynumber_valid) {
6443 sp->auth_keyid = srcv->sinfo_keynumber;
6445 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6447 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6448 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6449 sp->holds_key_ref = 1;
6451 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6452 sctp_snd_sb_alloc(stcb, sp->length);
6453 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6454 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6455 stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
6464 static struct mbuf *
6465 sctp_copy_mbufchain(struct mbuf *clonechain,
6466 struct mbuf *outchain,
6467 struct mbuf **endofchain,
6470 uint8_t copy_by_ref)
6473 struct mbuf *appendchain;
6477 if (endofchain == NULL) {
6481 sctp_m_freem(outchain);
6484 if (can_take_mbuf) {
6485 appendchain = clonechain;
6488 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
6489 /* Its not in a cluster */
6490 if (*endofchain == NULL) {
6491 /* lets get a mbuf cluster */
6492 if (outchain == NULL) {
6493 /* This is the general case */
6495 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6496 if (outchain == NULL) {
6499 SCTP_BUF_LEN(outchain) = 0;
6500 *endofchain = outchain;
6501 /* get the prepend space */
6502 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6505 * We really should not get a NULL
6511 if (SCTP_BUF_NEXT(m) == NULL) {
6515 m = SCTP_BUF_NEXT(m);
6518 if (*endofchain == NULL) {
6520 * huh, TSNH XXX maybe we
6523 sctp_m_freem(outchain);
6527 /* get the new end of length */
6528 len = (int)M_TRAILINGSPACE(*endofchain);
6530 /* how much is left at the end? */
6531 len = (int)M_TRAILINGSPACE(*endofchain);
6533 /* Find the end of the data, for appending */
6534 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6536 /* Now lets copy it out */
6537 if (len >= sizeofcpy) {
6538 /* It all fits, copy it in */
6539 m_copydata(clonechain, 0, sizeofcpy, cp);
6540 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6542 /* fill up the end of the chain */
6544 m_copydata(clonechain, 0, len, cp);
6545 SCTP_BUF_LEN((*endofchain)) += len;
6546 /* now we need another one */
6549 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6554 SCTP_BUF_NEXT((*endofchain)) = m;
6556 cp = mtod((*endofchain), caddr_t);
6557 m_copydata(clonechain, len, sizeofcpy, cp);
6558 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6562 /* copy the old fashion way */
6563 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
6564 #ifdef SCTP_MBUF_LOGGING
6565 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6566 sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
6571 if (appendchain == NULL) {
6574 sctp_m_freem(outchain);
6578 /* tack on to the end */
6579 if (*endofchain != NULL) {
6580 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6584 if (SCTP_BUF_NEXT(m) == NULL) {
6585 SCTP_BUF_NEXT(m) = appendchain;
6588 m = SCTP_BUF_NEXT(m);
6592 * save off the end and update the end-chain position
6596 if (SCTP_BUF_NEXT(m) == NULL) {
6600 m = SCTP_BUF_NEXT(m);
6604 /* save off the end and update the end-chain position */
6607 if (SCTP_BUF_NEXT(m) == NULL) {
6611 m = SCTP_BUF_NEXT(m);
6613 return (appendchain);
6618 sctp_med_chunk_output(struct sctp_inpcb *inp,
6619 struct sctp_tcb *stcb,
6620 struct sctp_association *asoc,
6623 int control_only, int from_where,
6624 struct timeval *now, int *now_filled,
6625 uint32_t frag_point, int so_locked);
6628 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6629 uint32_t val SCTP_UNUSED)
6631 struct sctp_copy_all *ca;
6634 int added_control = 0;
6635 int un_sent, do_chunk_output = 1;
6636 struct sctp_association *asoc;
6637 struct sctp_nets *net;
6639 ca = (struct sctp_copy_all *)ptr;
6640 if (ca->m == NULL) {
6643 if (ca->inp != inp) {
6647 if (ca->sndlen > 0) {
6648 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
6650 /* can't copy so we are done */
6654 #ifdef SCTP_MBUF_LOGGING
6655 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6656 sctp_log_mbc(m, SCTP_MBUF_ICOPY);
6662 SCTP_TCB_LOCK_ASSERT(stcb);
6663 if (stcb->asoc.alternate) {
6664 net = stcb->asoc.alternate;
6666 net = stcb->asoc.primary_destination;
6668 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6669 /* Abort this assoc with m as the user defined reason */
6671 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
6673 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
6674 0, M_NOWAIT, 1, MT_DATA);
6675 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
6678 struct sctp_paramhdr *ph;
6680 ph = mtod(m, struct sctp_paramhdr *);
6681 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6682 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
6685 * We add one here to keep the assoc from dis-appearing on
6688 atomic_add_int(&stcb->asoc.refcnt, 1);
6689 sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
6691 * sctp_abort_an_association calls sctp_free_asoc() free
6692 * association will NOT free it since we incremented the
6693 * refcnt .. we do this to prevent it being freed and things
6694 * getting tricky since we could end up (from free_asoc)
6695 * calling inpcb_free which would get a recursive lock call
6696 * to the iterator lock.. But as a consequence of that the
6697 * stcb will return to us un-locked.. since free_asoc
6698 * returns with either no TCB or the TCB unlocked, we must
6699 * relock.. to unlock in the iterator timer :-0
6701 SCTP_TCB_LOCK(stcb);
6702 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6703 goto no_chunk_output;
6706 ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
6709 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6710 /* shutdown this assoc */
6711 if (TAILQ_EMPTY(&asoc->send_queue) &&
6712 TAILQ_EMPTY(&asoc->sent_queue) &&
6713 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
6714 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6718 * there is nothing queued to send, so I'm
6721 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6722 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6723 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6725 * only send SHUTDOWN the first time
6728 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
6729 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6731 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
6732 sctp_stop_timers_for_shutdown(stcb);
6733 sctp_send_shutdown(stcb, net);
6734 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6736 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6739 do_chunk_output = 0;
6743 * we still got (or just got) data to send,
6744 * so set SHUTDOWN_PENDING
6747 * XXX sockets draft says that SCTP_EOF
6748 * should be sent with no data. currently,
6749 * we will allow user data to be sent first
6750 * and move to SHUTDOWN-PENDING
6752 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6753 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6754 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6755 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6756 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
6758 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
6759 if (TAILQ_EMPTY(&asoc->send_queue) &&
6760 TAILQ_EMPTY(&asoc->sent_queue) &&
6761 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6762 struct mbuf *op_err;
6763 char msg[SCTP_DIAG_INFO_LEN];
6766 SCTP_SNPRINTF(msg, sizeof(msg),
6767 "%s:%d at %s", __FILE__, __LINE__, __func__);
6768 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6770 atomic_add_int(&stcb->asoc.refcnt, 1);
6771 sctp_abort_an_association(stcb->sctp_ep, stcb,
6772 op_err, false, SCTP_SO_NOT_LOCKED);
6773 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6774 goto no_chunk_output;
6780 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6781 (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
6783 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6784 (stcb->asoc.total_flight > 0) &&
6785 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
6786 do_chunk_output = 0;
6788 if (do_chunk_output)
6789 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6790 else if (added_control) {
6792 int num_out, reason, now_filled = 0;
6794 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6795 &reason, 1, 1, &now, &now_filled,
6796 sctp_get_frag_point(stcb),
6797 SCTP_SO_NOT_LOCKED);
6808 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6810 struct sctp_copy_all *ca;
6812 ca = (struct sctp_copy_all *)ptr;
6814 * Do a notify here? Kacheong suggests that the notify be done at
6815 * the send time.. so you would push up a notification if any send
6816 * failed. Don't know if this is feasible since the only failures we
6817 * have is "memory" related and if you cannot get an mbuf to send
6818 * the data you surely can't get an mbuf to send up to notify the
6819 * user you can't send the data :->
6822 /* now free everything */
6824 /* Lets clear the flag to allow others to run. */
6825 SCTP_INP_WLOCK(ca->inp);
6826 ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6827 SCTP_INP_WUNLOCK(ca->inp);
6829 sctp_m_freem(ca->m);
6830 SCTP_FREE(ca, SCTP_M_COPYAL);
6833 static struct mbuf *
6834 sctp_copy_out_all(struct uio *uio, ssize_t len)
6836 struct mbuf *ret, *at;
6837 ssize_t left, willcpy, cancpy, error;
6839 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
6845 SCTP_BUF_LEN(ret) = 0;
6846 /* save space for the data chunk header */
6847 cancpy = (int)M_TRAILINGSPACE(ret);
6848 willcpy = min(cancpy, left);
6851 /* Align data to the end */
6852 error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
6858 SCTP_BUF_LEN(at) = (int)willcpy;
6859 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6862 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
6863 if (SCTP_BUF_NEXT(at) == NULL) {
6866 at = SCTP_BUF_NEXT(at);
6867 SCTP_BUF_LEN(at) = 0;
6868 cancpy = (int)M_TRAILINGSPACE(at);
6869 willcpy = min(cancpy, left);
6876 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6877 struct sctp_nonpad_sndrcvinfo *srcv)
6880 struct sctp_copy_all *ca;
6882 if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
6883 /* You must not be larger than the limit! */
6886 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6890 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6893 memset(ca, 0, sizeof(struct sctp_copy_all));
6897 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6901 SCTP_INP_WLOCK(inp);
6902 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
6903 SCTP_INP_WUNLOCK(inp);
6905 SCTP_FREE(ca, SCTP_M_COPYAL);
6908 inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6909 SCTP_INP_WUNLOCK(inp);
6912 * take off the sendall flag, it would be bad if we failed to do
6915 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6916 /* get length and mbuf chain */
6918 ca->sndlen = uio->uio_resid;
6919 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6920 if (ca->m == NULL) {
6921 SCTP_FREE(ca, SCTP_M_COPYAL);
6923 SCTP_INP_WLOCK(inp);
6924 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6925 SCTP_INP_WUNLOCK(inp);
6926 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6930 /* Gather the length of the send */
6934 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6935 ca->sndlen += SCTP_BUF_LEN(mat);
6938 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6939 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6940 SCTP_ASOC_ANY_STATE,
6942 sctp_sendall_completes, inp, 1);
6944 SCTP_INP_WLOCK(inp);
6945 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6946 SCTP_INP_WUNLOCK(inp);
6947 SCTP_FREE(ca, SCTP_M_COPYAL);
6948 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6955 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6957 struct sctp_tmit_chunk *chk, *nchk;
6959 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6960 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6961 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6962 asoc->ctrl_queue_cnt--;
6964 sctp_m_freem(chk->data);
6967 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6973 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6975 struct sctp_association *asoc;
6976 struct sctp_tmit_chunk *chk, *nchk;
6977 struct sctp_asconf_chunk *acp;
6980 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6981 /* find SCTP_ASCONF chunk in queue */
6982 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6984 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6985 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6990 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6991 asoc->ctrl_queue_cnt--;
6993 sctp_m_freem(chk->data);
6996 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7002 sctp_clean_up_datalist(struct sctp_tcb *stcb,
7003 struct sctp_association *asoc,
7004 struct sctp_tmit_chunk **data_list,
7006 struct sctp_nets *net)
7009 struct sctp_tmit_chunk *tp1;
7011 for (i = 0; i < bundle_at; i++) {
7012 /* off of the send queue */
7013 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
7014 asoc->send_queue_cnt--;
7017 * Any chunk NOT 0 you zap the time chunk 0 gets
7018 * zapped or set based on if a RTO measurement is
7021 data_list[i]->do_rtt = 0;
7024 data_list[i]->sent_rcv_time = net->last_sent_time;
7025 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
7026 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
7027 if (data_list[i]->whoTo == NULL) {
7028 data_list[i]->whoTo = net;
7029 atomic_add_int(&net->ref_count, 1);
7031 /* on to the sent queue */
7032 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
7033 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7034 struct sctp_tmit_chunk *tpp;
7036 /* need to move back */
7038 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
7040 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7044 if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7047 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7049 TAILQ_INSERT_TAIL(&asoc->sent_queue,
7054 /* This does not lower until the cum-ack passes it */
7055 asoc->sent_queue_cnt++;
7056 if ((asoc->peers_rwnd <= 0) &&
7057 (asoc->total_flight == 0) &&
7059 /* Mark the chunk as being a window probe */
7060 SCTP_STAT_INCR(sctps_windowprobed);
7062 #ifdef SCTP_AUDITING_ENABLED
7063 sctp_audit_log(0xC2, 3);
7065 data_list[i]->sent = SCTP_DATAGRAM_SENT;
7066 data_list[i]->snd_count = 1;
7067 data_list[i]->rec.data.chunk_was_revoked = 0;
7068 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7069 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7070 data_list[i]->whoTo->flight_size,
7071 data_list[i]->book_size,
7072 (uint32_t)(uintptr_t)data_list[i]->whoTo,
7073 data_list[i]->rec.data.tsn);
7075 sctp_flight_size_increase(data_list[i]);
7076 sctp_total_flight_increase(stcb, data_list[i]);
7077 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7078 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7079 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7081 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7082 (uint32_t)(data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7083 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7084 /* SWS sender side engages */
7085 asoc->peers_rwnd = 0;
7088 if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7089 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
7094 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7096 struct sctp_tmit_chunk *chk, *nchk;
7098 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7099 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7100 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7101 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7102 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7103 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7104 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7105 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7106 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7107 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7108 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7109 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7110 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7111 /* Stray chunks must be cleaned up */
7113 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7114 asoc->ctrl_queue_cnt--;
7116 sctp_m_freem(chk->data);
7119 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7120 asoc->fwd_tsn_cnt--;
7122 sctp_free_a_chunk(stcb, chk, so_locked);
7123 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7124 /* special handling, we must look into the param */
7125 if (chk != asoc->str_reset) {
7126 goto clean_up_anyway;
7133 sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7134 uint32_t space_left, uint32_t frag_point, int eeor_on)
7137 * Make a decision on if I should split a msg into multiple parts.
7138 * This is only asked of incomplete messages.
7142 * If we are doing EEOR we need to always send it if its the
7143 * entire thing, since it might be all the guy is putting in
7146 if (space_left >= length) {
7148 * If we have data outstanding,
7149 * we get another chance when the sack
7150 * arrives to transmit - wait for more data
7152 if (stcb->asoc.total_flight == 0) {
7154 * If nothing is in flight, we zero the
7162 /* You can fill the rest */
7163 return (space_left);
7167 * For those strange folk that make the send buffer
7168 * smaller than our fragmentation point, we can't
7169 * get a full msg in so we have to allow splitting.
7171 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7174 if ((length <= space_left) ||
7175 ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7176 /* Sub-optimal residual don't split in non-eeor mode. */
7180 * If we reach here length is larger than the space_left. Do we wish
7181 * to split it for the sake of packet putting together?
7183 if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7184 /* Its ok to split it */
7185 return (min(space_left, frag_point));
7187 /* Nope, can't split */
7192 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7193 struct sctp_nets *net,
7194 struct sctp_stream_out *strq,
7195 uint32_t space_left,
7196 uint32_t frag_point,
7202 /* Move from the stream to the send_queue keeping track of the total */
7203 struct sctp_association *asoc;
7204 struct sctp_stream_queue_pending *sp;
7205 struct sctp_tmit_chunk *chk;
7206 struct sctp_data_chunk *dchkh = NULL;
7207 struct sctp_idata_chunk *ndchkh = NULL;
7208 uint32_t to_move, length;
7210 uint8_t rcv_flags = 0;
7213 SCTP_TCB_LOCK_ASSERT(stcb);
7216 /* sa_ignore FREED_MEMORY */
7217 sp = TAILQ_FIRST(&strq->outqueue);
7219 sp = TAILQ_FIRST(&strq->outqueue);
7223 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7224 (stcb->asoc.idata_supported == 0) &&
7225 (strq->last_msg_incomplete)) {
7226 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7228 strq->last_msg_incomplete);
7229 strq->last_msg_incomplete = 0;
7234 if ((sp->msg_is_complete) && (sp->length == 0)) {
7235 if (sp->sender_all_done) {
7237 * We are doing deferred cleanup. Last time through
7238 * when we took all the data the sender_all_done was
7241 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7242 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7243 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7244 sp->sender_all_done,
7246 sp->msg_is_complete,
7249 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7250 TAILQ_REMOVE(&strq->outqueue, sp, next);
7251 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7252 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7253 (strq->chunks_on_queues == 0) &&
7254 TAILQ_EMPTY(&strq->outqueue)) {
7255 stcb->asoc.trigger_reset = 1;
7258 sctp_free_remote_addr(sp->net);
7262 sctp_m_freem(sp->data);
7265 sctp_free_a_strmoq(stcb, sp, so_locked);
7266 /* back to get the next msg */
7270 * sender just finished this but still holds a
7278 /* is there some to get */
7279 if (sp->length == 0) {
7284 } else if (sp->discard_rest) {
7285 /* Whack down the size */
7286 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7287 if ((stcb->sctp_socket != NULL) &&
7288 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7289 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7290 SCTP_SB_DECR(&stcb->sctp_socket->so_snd, sp->length);
7293 sctp_m_freem(sp->data);
7295 sp->tail_mbuf = NULL;
7304 some_taken = sp->some_taken;
7305 length = sp->length;
7306 if (sp->msg_is_complete) {
7307 /* The message is complete */
7308 to_move = min(length, frag_point);
7309 if (to_move == length) {
7310 /* All of it fits in the MTU */
7311 if (sp->some_taken) {
7312 rcv_flags |= SCTP_DATA_LAST_FRAG;
7314 rcv_flags |= SCTP_DATA_NOT_FRAG;
7316 sp->put_last_out = 1;
7317 if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7318 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7321 /* Not all of it fits, we fragment */
7322 if (sp->some_taken == 0) {
7323 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7328 to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7330 if (to_move >= length) {
7333 if (sp->some_taken == 0) {
7334 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7338 /* Nothing to take. */
7345 /* If we reach here, we can copy out a chunk */
7346 sctp_alloc_a_chunk(stcb, chk);
7348 /* No chunk memory */
7354 * Setup for unordered if needed by looking at the user sent info
7357 if (sp->sinfo_flags & SCTP_UNORDERED) {
7358 rcv_flags |= SCTP_DATA_UNORDERED;
7360 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7361 (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7362 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7364 /* clear out the chunk before setting up */
7365 memset(chk, 0, sizeof(*chk));
7366 chk->rec.data.rcv_flags = rcv_flags;
7368 if (to_move >= length) {
7369 /* we think we can steal the whole thing */
7370 if (to_move < sp->length) {
7371 /* bail, it changed */
7374 chk->data = sp->data;
7375 chk->last_mbuf = sp->tail_mbuf;
7376 /* register the stealing */
7377 sp->data = sp->tail_mbuf = NULL;
7382 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7383 chk->last_mbuf = NULL;
7384 if (chk->data == NULL) {
7385 sp->some_taken = some_taken;
7386 sctp_free_a_chunk(stcb, chk, so_locked);
7391 #ifdef SCTP_MBUF_LOGGING
7392 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7393 sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
7396 /* Pull off the data */
7397 m_adj(sp->data, to_move);
7398 /* Now lets work our way down and compact it */
7400 while (m && (SCTP_BUF_LEN(m) == 0)) {
7401 sp->data = SCTP_BUF_NEXT(m);
7402 SCTP_BUF_NEXT(m) = NULL;
7403 if (sp->tail_mbuf == m) {
7405 * Freeing tail? TSNH since
7406 * we supposedly were taking less
7407 * than the sp->length.
7410 panic("Huh, freeing tail? - TSNH");
7412 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7413 sp->tail_mbuf = sp->data = NULL;
7421 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7422 chk->copy_by_ref = 1;
7424 chk->copy_by_ref = 0;
7427 * get last_mbuf and counts of mb usage This is ugly but hopefully
7428 * its only one mbuf.
7430 if (chk->last_mbuf == NULL) {
7431 chk->last_mbuf = chk->data;
7432 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7433 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7437 if (to_move > length) {
7438 /*- This should not happen either
7439 * since we always lower to_move to the size
7440 * of sp->length if its larger.
7443 panic("Huh, how can to_move be larger?");
7445 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7449 atomic_subtract_int(&sp->length, to_move);
7451 leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
7452 if (M_LEADINGSPACE(chk->data) < leading) {
7453 /* Not enough room for a chunk header, get some */
7456 m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
7459 * we're in trouble here. _PREPEND below will free
7460 * all the data if there is no leading space, so we
7461 * must put the data back and restore.
7463 if (sp->data == NULL) {
7464 /* unsteal the data */
7465 sp->data = chk->data;
7466 sp->tail_mbuf = chk->last_mbuf;
7470 /* reassemble the data */
7472 sp->data = chk->data;
7473 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7475 sp->some_taken = some_taken;
7476 atomic_add_int(&sp->length, to_move);
7479 sctp_free_a_chunk(stcb, chk, so_locked);
7483 SCTP_BUF_LEN(m) = 0;
7484 SCTP_BUF_NEXT(m) = chk->data;
7486 M_ALIGN(chk->data, 4);
7489 SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
7490 if (chk->data == NULL) {
7491 /* HELP, TSNH since we assured it would not above? */
7493 panic("prepend fails HELP?");
7495 SCTP_PRINTF("prepend fails HELP?\n");
7496 sctp_free_a_chunk(stcb, chk, so_locked);
7502 sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
7503 chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
7504 chk->book_size_scale = 0;
7505 chk->sent = SCTP_DATAGRAM_UNSENT;
7508 chk->asoc = &stcb->asoc;
7509 chk->pad_inplace = 0;
7510 chk->no_fr_allowed = 0;
7511 if (stcb->asoc.idata_supported == 0) {
7512 if (rcv_flags & SCTP_DATA_UNORDERED) {
7513 /* Just use 0. The receiver ignores the values. */
7514 chk->rec.data.mid = 0;
7516 chk->rec.data.mid = strq->next_mid_ordered;
7517 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7518 strq->next_mid_ordered++;
7522 if (rcv_flags & SCTP_DATA_UNORDERED) {
7523 chk->rec.data.mid = strq->next_mid_unordered;
7524 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7525 strq->next_mid_unordered++;
7528 chk->rec.data.mid = strq->next_mid_ordered;
7529 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7530 strq->next_mid_ordered++;
7534 chk->rec.data.sid = sp->sid;
7535 chk->rec.data.ppid = sp->ppid;
7536 chk->rec.data.context = sp->context;
7537 chk->rec.data.doing_fast_retransmit = 0;
7539 chk->rec.data.timetodrop = sp->ts;
7540 chk->flags = sp->act_flags;
7543 chk->whoTo = sp->net;
7544 atomic_add_int(&chk->whoTo->ref_count, 1);
7548 if (sp->holds_key_ref) {
7549 chk->auth_keyid = sp->auth_keyid;
7550 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7551 chk->holds_key_ref = 1;
7553 stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
7554 chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
7555 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7556 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7557 (uint32_t)(uintptr_t)stcb, sp->length,
7558 (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
7561 if (stcb->asoc.idata_supported == 0) {
7562 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7564 ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
7567 * Put the rest of the things in place now. Size was done earlier in
7568 * previous loop prior to padding.
7571 SCTP_TCB_LOCK_ASSERT(stcb);
7572 #ifdef SCTP_ASOCLOG_OF_TSNS
7573 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7574 asoc->tsn_out_at = 0;
7575 asoc->tsn_out_wrapped = 1;
7577 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
7578 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
7579 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
7580 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7581 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7582 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7583 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7584 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7587 if (stcb->asoc.idata_supported == 0) {
7588 dchkh->ch.chunk_type = SCTP_DATA;
7589 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7590 dchkh->dp.tsn = htonl(chk->rec.data.tsn);
7591 dchkh->dp.sid = htons(strq->sid);
7592 dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
7593 dchkh->dp.ppid = chk->rec.data.ppid;
7594 dchkh->ch.chunk_length = htons(chk->send_size);
7596 ndchkh->ch.chunk_type = SCTP_IDATA;
7597 ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7598 ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
7599 ndchkh->dp.sid = htons(strq->sid);
7600 ndchkh->dp.reserved = htons(0);
7601 ndchkh->dp.mid = htonl(chk->rec.data.mid);
7603 ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
7605 ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
7607 ndchkh->ch.chunk_length = htons(chk->send_size);
7609 /* Now advance the chk->send_size by the actual pad needed. */
7610 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7615 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7616 lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
7618 chk->last_mbuf = lm;
7619 chk->pad_inplace = 1;
7621 chk->send_size += pads;
7623 if (PR_SCTP_ENABLED(chk->flags)) {
7624 asoc->pr_sctp_cnt++;
7626 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7627 /* All done pull and kill the message */
7628 if (sp->put_last_out == 0) {
7629 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7630 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7631 sp->sender_all_done,
7633 sp->msg_is_complete,
7636 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7637 TAILQ_REMOVE(&strq->outqueue, sp, next);
7638 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7639 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7640 (strq->chunks_on_queues == 0) &&
7641 TAILQ_EMPTY(&strq->outqueue)) {
7642 stcb->asoc.trigger_reset = 1;
7645 sctp_free_remote_addr(sp->net);
7649 sctp_m_freem(sp->data);
7652 sctp_free_a_strmoq(stcb, sp, so_locked);
7654 asoc->chunks_on_out_queue++;
7655 strq->chunks_on_queues++;
7656 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7657 asoc->send_queue_cnt++;
7663 sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
7664 uint32_t frag_point, int eeor_mode, int *quit_now,
7667 struct sctp_association *asoc;
7668 struct sctp_stream_out *strq;
7669 uint32_t space_left, moved, total_moved;
7672 SCTP_TCB_LOCK_ASSERT(stcb);
7675 switch (net->ro._l_addr.sa.sa_family) {
7678 space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
7683 space_left = net->mtu - SCTP_MIN_OVERHEAD;
7688 space_left = net->mtu;
7691 /* Need an allowance for the data chunk header too */
7692 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7694 /* must make even word boundary */
7695 space_left &= 0xfffffffc;
7696 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7699 while ((space_left > 0) && (strq != NULL)) {
7700 moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
7701 frag_point, &giveup, eeor_mode,
7703 if ((giveup != 0) || (bail != 0)) {
7706 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7707 total_moved += moved;
7708 if (space_left >= moved) {
7709 space_left -= moved;
7713 if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
7714 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7718 space_left &= 0xfffffffc;
7723 stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7725 if (total_moved == 0) {
7726 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7727 (net == stcb->asoc.primary_destination)) {
7728 /* ran dry for primary network net */
7729 SCTP_STAT_INCR(sctps_primary_randry);
7730 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7731 /* ran dry with CMT on */
7732 SCTP_STAT_INCR(sctps_cmt_randry);
7738 sctp_fix_ecn_echo(struct sctp_association *asoc)
7740 struct sctp_tmit_chunk *chk;
7742 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7743 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7744 chk->sent = SCTP_DATAGRAM_UNSENT;
7750 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7752 struct sctp_association *asoc;
7753 struct sctp_tmit_chunk *chk;
7754 struct sctp_stream_queue_pending *sp;
7761 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7762 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7763 if (sp->net == net) {
7764 sctp_free_remote_addr(sp->net);
7769 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7770 if (chk->whoTo == net) {
7771 sctp_free_remote_addr(chk->whoTo);
7778 sctp_med_chunk_output(struct sctp_inpcb *inp,
7779 struct sctp_tcb *stcb,
7780 struct sctp_association *asoc,
7783 int control_only, int from_where,
7784 struct timeval *now, int *now_filled,
7785 uint32_t frag_point, int so_locked)
7788 * Ok this is the generic chunk service queue. we must do the
7790 * - Service the stream queue that is next, moving any
7791 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7792 * LAST to the out queue in one pass) and assigning TSN's. This
7793 * only applies though if the peer does not support NDATA. For NDATA
7794 * chunks its ok to not send the entire message ;-)
7795 * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
7796 * formulate and send the low level chunks. Making sure to combine
7797 * any control in the control chunk queue also.
7799 struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7800 struct mbuf *outchain, *endoutchain;
7801 struct sctp_tmit_chunk *chk, *nchk;
7803 /* temp arrays for unlinking */
7804 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7805 int no_fragmentflg, error;
7806 unsigned int max_rwnd_per_dest, max_send_per_dest;
7807 int one_chunk, hbflag, skip_data_for_this_net;
7808 int asconf, cookie, no_out_cnt;
7809 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7810 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7812 uint32_t auth_offset;
7813 struct sctp_auth_chunk *auth;
7814 uint16_t auth_keyid;
7815 int override_ok = 1;
7816 int skip_fill_up = 0;
7817 int data_auth_reqd = 0;
7820 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7828 auth_keyid = stcb->asoc.authinfo.active_keyid;
7829 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7830 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7831 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7836 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7838 * First lets prime the pump. For each destination, if there is room
7839 * in the flight size, attempt to pull an MTU's worth out of the
7840 * stream queues into the general send_queue
7842 #ifdef SCTP_AUDITING_ENABLED
7843 sctp_audit_log(0xC2, 2);
7845 SCTP_TCB_LOCK_ASSERT(stcb);
7852 /* Nothing to possible to send? */
7853 if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7854 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7855 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7856 TAILQ_EMPTY(&asoc->send_queue) &&
7857 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
7862 if (asoc->peers_rwnd == 0) {
7863 /* No room in peers rwnd */
7865 if (asoc->total_flight > 0) {
7866 /* we are allowed one chunk in flight */
7870 if (stcb->asoc.ecn_echo_cnt_onq) {
7871 /* Record where a sack goes, if any */
7872 if (no_data_chunks &&
7873 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7874 /* Nothing but ECNe to send - we don't do that */
7875 goto nothing_to_send;
7877 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7878 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7879 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7880 sack_goes_to = chk->whoTo;
7885 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7886 if (stcb->sctp_socket)
7887 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7889 max_send_per_dest = 0;
7890 if (no_data_chunks == 0) {
7891 /* How many non-directed chunks are there? */
7892 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7893 if (chk->whoTo == NULL) {
7895 * We already have non-directed chunks on
7896 * the queue, no need to do a fill-up.
7903 if ((no_data_chunks == 0) &&
7904 (skip_fill_up == 0) &&
7905 (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7906 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7908 * This for loop we are in takes in each net, if
7909 * its's got space in cwnd and has data sent to it
7910 * (when CMT is off) then it calls
7911 * sctp_fill_outqueue for the net. This gets data on
7912 * the send queue for that network.
7914 * In sctp_fill_outqueue TSN's are assigned and data
7915 * is copied out of the stream buffers. Note mostly
7916 * copy by reference (we hope).
7918 net->window_probe = 0;
7919 if ((net != stcb->asoc.alternate) &&
7920 ((net->dest_state & SCTP_ADDR_PF) ||
7921 ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
7922 (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7923 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7924 sctp_log_cwnd(stcb, net, 1,
7925 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7929 if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7930 (net->flight_size == 0)) {
7931 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7933 if (net->flight_size >= net->cwnd) {
7934 /* skip this network, no room - can't fill */
7935 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7936 sctp_log_cwnd(stcb, net, 3,
7937 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7941 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7942 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7944 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7946 /* memory alloc failure */
7952 /* now service each destination and send out what we can for it */
7953 /* Nothing to send? */
7954 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7955 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7956 TAILQ_EMPTY(&asoc->send_queue)) {
7961 if (asoc->sctp_cmt_on_off > 0) {
7962 /* get the last start point */
7963 start_at = asoc->last_net_cmt_send_started;
7964 if (start_at == NULL) {
7965 /* null so to beginning */
7966 start_at = TAILQ_FIRST(&asoc->nets);
7968 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7969 if (start_at == NULL) {
7970 start_at = TAILQ_FIRST(&asoc->nets);
7973 asoc->last_net_cmt_send_started = start_at;
7975 start_at = TAILQ_FIRST(&asoc->nets);
7977 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7978 if (chk->whoTo == NULL) {
7979 if (asoc->alternate) {
7980 chk->whoTo = asoc->alternate;
7982 chk->whoTo = asoc->primary_destination;
7984 atomic_add_int(&chk->whoTo->ref_count, 1);
7987 old_start_at = NULL;
7988 again_one_more_time:
7989 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7990 /* how much can we send? */
7991 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7992 if (old_start_at && (old_start_at == net)) {
7993 /* through list completely. */
7997 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7998 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7999 (net->flight_size >= net->cwnd)) {
8001 * Nothing on control or asconf and flight is full,
8002 * we can skip even in the CMT case.
8007 endoutchain = outchain = NULL;
8012 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8013 skip_data_for_this_net = 1;
8015 skip_data_for_this_net = 0;
8017 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8020 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8025 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8035 if (mtu > asoc->peers_rwnd) {
8036 if (asoc->total_flight > 0) {
8037 /* We have a packet in flight somewhere */
8038 r_mtu = asoc->peers_rwnd;
8040 /* We are always allowed to send one MTU out */
8048 /************************/
8049 /* ASCONF transmission */
8050 /************************/
8051 /* Now first lets go through the asconf queue */
8052 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8053 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8056 if (chk->whoTo == NULL) {
8057 if (asoc->alternate == NULL) {
8058 if (asoc->primary_destination != net) {
8062 if (asoc->alternate != net) {
8067 if (chk->whoTo != net) {
8071 if (chk->data == NULL) {
8074 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8075 chk->sent != SCTP_DATAGRAM_RESEND) {
8079 * if no AUTH is yet included and this chunk
8080 * requires it, make sure to account for it. We
8081 * don't apply the size until the AUTH chunk is
8082 * actually added below in case there is no room for
8083 * this chunk. NOTE: we overload the use of "omtu"
8086 if ((auth == NULL) &&
8087 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8088 stcb->asoc.peer_auth_chunks)) {
8089 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8092 /* Here we do NOT factor the r_mtu */
8093 if ((chk->send_size < (int)(mtu - omtu)) ||
8094 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8096 * We probably should glom the mbuf chain
8097 * from the chk->data for control but the
8098 * problem is it becomes yet one more level
8099 * of tracking to do if for some reason
8100 * output fails. Then I have got to
8101 * reconstruct the merged control chain.. el
8102 * yucko.. for now we take the easy way and
8106 * Add an AUTH chunk, if chunk requires it
8107 * save the offset into the chain for AUTH
8109 if ((auth == NULL) &&
8110 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8111 stcb->asoc.peer_auth_chunks))) {
8112 outchain = sctp_add_auth_chunk(outchain,
8117 chk->rec.chunk_id.id);
8118 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8120 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8121 (int)chk->rec.chunk_id.can_take_data,
8122 chk->send_size, chk->copy_by_ref);
8123 if (outchain == NULL) {
8125 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8128 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8129 /* update our MTU size */
8130 if (mtu > (chk->send_size + omtu))
8131 mtu -= (chk->send_size + omtu);
8134 to_out += (chk->send_size + omtu);
8135 /* Do clear IP_DF ? */
8136 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8139 if (chk->rec.chunk_id.can_take_data)
8142 * set hb flag since we can use these for
8148 * should sysctl this: don't bundle data
8149 * with ASCONF since it requires AUTH
8152 chk->sent = SCTP_DATAGRAM_SENT;
8153 if (chk->whoTo == NULL) {
8155 atomic_add_int(&net->ref_count, 1);
8160 * Ok we are out of room but we can
8161 * output without effecting the
8162 * flight size since this little guy
8163 * is a control only packet.
8165 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8167 * do NOT clear the asconf flag as
8168 * it is used to do appropriate
8169 * source address selection.
8171 if (*now_filled == 0) {
8172 (void)SCTP_GETTIME_TIMEVAL(now);
8175 net->last_sent_time = *now;
8177 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8178 (struct sockaddr *)&net->ro._l_addr,
8179 outchain, auth_offset, auth,
8180 stcb->asoc.authinfo.active_keyid,
8181 no_fragmentflg, 0, asconf,
8182 inp->sctp_lport, stcb->rport,
8183 htonl(stcb->asoc.peer_vtag),
8186 false, so_locked))) {
8188 * error, we could not
8191 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8192 if (from_where == 0) {
8193 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8195 if (error == ENOBUFS) {
8196 asoc->ifp_had_enobuf = 1;
8197 SCTP_STAT_INCR(sctps_lowlevelerr);
8199 /* error, could not output */
8200 if (error == EHOSTUNREACH) {
8206 sctp_move_chunks_from_net(stcb, net);
8212 asoc->ifp_had_enobuf = 0;
8215 * increase the number we sent, if a
8216 * cookie is sent we don't tell them
8219 outchain = endoutchain = NULL;
8224 *num_out += ctl_cnt;
8225 /* recalc a clean slate and setup */
8226 switch (net->ro._l_addr.sa.sa_family) {
8229 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8234 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8251 /************************/
8252 /* Control transmission */
8253 /************************/
8254 /* Now first lets go through the control queue */
8255 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8256 if ((sack_goes_to) &&
8257 (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8258 (chk->whoTo != sack_goes_to)) {
8260 * if we have a sack in queue, and we are
8261 * looking at an ecn echo that is NOT queued
8262 * to where the sack is going..
8264 if (chk->whoTo == net) {
8266 * Don't transmit it to where its
8267 * going (current net)
8270 } else if (sack_goes_to == net) {
8272 * But do transmit it to this
8275 goto skip_net_check;
8278 if (chk->whoTo == NULL) {
8279 if (asoc->alternate == NULL) {
8280 if (asoc->primary_destination != net) {
8284 if (asoc->alternate != net) {
8289 if (chk->whoTo != net) {
8294 if (chk->data == NULL) {
8297 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8299 * It must be unsent. Cookies and ASCONF's
8300 * hang around but there timers will force
8301 * when marked for resend.
8306 * if no AUTH is yet included and this chunk
8307 * requires it, make sure to account for it. We
8308 * don't apply the size until the AUTH chunk is
8309 * actually added below in case there is no room for
8310 * this chunk. NOTE: we overload the use of "omtu"
8313 if ((auth == NULL) &&
8314 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8315 stcb->asoc.peer_auth_chunks)) {
8316 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8319 /* Here we do NOT factor the r_mtu */
8320 if ((chk->send_size <= (int)(mtu - omtu)) ||
8321 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8323 * We probably should glom the mbuf chain
8324 * from the chk->data for control but the
8325 * problem is it becomes yet one more level
8326 * of tracking to do if for some reason
8327 * output fails. Then I have got to
8328 * reconstruct the merged control chain.. el
8329 * yucko.. for now we take the easy way and
8333 * Add an AUTH chunk, if chunk requires it
8334 * save the offset into the chain for AUTH
8336 if ((auth == NULL) &&
8337 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8338 stcb->asoc.peer_auth_chunks))) {
8339 outchain = sctp_add_auth_chunk(outchain,
8344 chk->rec.chunk_id.id);
8345 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8347 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8348 (int)chk->rec.chunk_id.can_take_data,
8349 chk->send_size, chk->copy_by_ref);
8350 if (outchain == NULL) {
8352 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8355 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8356 /* update our MTU size */
8357 if (mtu > (chk->send_size + omtu))
8358 mtu -= (chk->send_size + omtu);
8361 to_out += (chk->send_size + omtu);
8362 /* Do clear IP_DF ? */
8363 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8366 if (chk->rec.chunk_id.can_take_data)
8368 /* Mark things to be removed, if needed */
8369 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8370 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
8371 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8372 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8373 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8374 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8375 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8376 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8377 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8378 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8379 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8380 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8383 /* remove these chunks at the end */
8384 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8385 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8386 /* turn off the timer */
8387 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8388 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8390 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8396 * Other chunks, since they have
8397 * timers running (i.e. COOKIE) we
8398 * just "trust" that it gets sent or
8402 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8405 } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8407 * Increment ecne send count
8408 * here this means we may be
8409 * over-zealous in our
8410 * counting if the send
8411 * fails, but its the best
8412 * place to do it (we used
8413 * to do it in the queue of
8414 * the chunk, but that did
8415 * not tell how many times
8418 SCTP_STAT_INCR(sctps_sendecne);
8420 chk->sent = SCTP_DATAGRAM_SENT;
8421 if (chk->whoTo == NULL) {
8423 atomic_add_int(&net->ref_count, 1);
8429 * Ok we are out of room but we can
8430 * output without effecting the
8431 * flight size since this little guy
8432 * is a control only packet.
8434 switch (asoc->snd_edmid) {
8435 case SCTP_EDMID_LOWER_LAYER_DTLS:
8436 use_zero_crc = true;
8439 use_zero_crc = false;
8443 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8444 use_zero_crc = false;
8446 * do NOT clear the asconf
8447 * flag as it is used to do
8448 * appropriate source
8449 * address selection.
8453 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8454 use_zero_crc = false;
8457 /* Only HB or ASCONF advances time */
8459 if (*now_filled == 0) {
8460 (void)SCTP_GETTIME_TIMEVAL(now);
8463 net->last_sent_time = *now;
8466 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8467 (struct sockaddr *)&net->ro._l_addr,
8470 stcb->asoc.authinfo.active_keyid,
8471 no_fragmentflg, 0, asconf,
8472 inp->sctp_lport, stcb->rport,
8473 htonl(stcb->asoc.peer_vtag),
8476 use_zero_crc, so_locked))) {
8478 * error, we could not
8481 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8482 if (from_where == 0) {
8483 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8485 if (error == ENOBUFS) {
8486 asoc->ifp_had_enobuf = 1;
8487 SCTP_STAT_INCR(sctps_lowlevelerr);
8489 if (error == EHOSTUNREACH) {
8495 sctp_move_chunks_from_net(stcb, net);
8501 asoc->ifp_had_enobuf = 0;
8504 * increase the number we sent, if a
8505 * cookie is sent we don't tell them
8508 outchain = endoutchain = NULL;
8513 *num_out += ctl_cnt;
8514 /* recalc a clean slate and setup */
8515 switch (net->ro._l_addr.sa.sa_family) {
8518 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8523 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8540 /* JRI: if dest is in PF state, do not send data to it */
8541 if ((asoc->sctp_cmt_on_off > 0) &&
8542 (net != stcb->asoc.alternate) &&
8543 (net->dest_state & SCTP_ADDR_PF)) {
8546 if (net->flight_size >= net->cwnd) {
8549 if ((asoc->sctp_cmt_on_off > 0) &&
8550 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8551 (net->flight_size > max_rwnd_per_dest)) {
8555 * We need a specific accounting for the usage of the send
8556 * buffer. We also need to check the number of messages per
8557 * net. For now, this is better than nothing and it disabled
8560 if ((asoc->sctp_cmt_on_off > 0) &&
8561 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8562 (max_send_per_dest > 0) &&
8563 (net->flight_size > max_send_per_dest)) {
8566 /*********************/
8567 /* Data transmission */
8568 /*********************/
8570 * if AUTH for DATA is required and no AUTH has been added
8571 * yet, account for this in the mtu now... if no data can be
8572 * bundled, this adjustment won't matter anyways since the
8573 * packet will be going out...
8575 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8576 stcb->asoc.peer_auth_chunks);
8577 if (data_auth_reqd && (auth == NULL)) {
8578 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8580 /* now lets add any data within the MTU constraints */
8581 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8584 if (net->mtu > SCTP_MIN_V4_OVERHEAD)
8585 omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8592 if (net->mtu > SCTP_MIN_OVERHEAD)
8593 omtu = net->mtu - SCTP_MIN_OVERHEAD;
8603 if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
8604 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8605 (skip_data_for_this_net == 0)) ||
8607 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8608 if (no_data_chunks) {
8609 /* let only control go out */
8613 if (net->flight_size >= net->cwnd) {
8614 /* skip this net, no room for data */
8618 if ((chk->whoTo != NULL) &&
8619 (chk->whoTo != net)) {
8620 /* Don't send the chunk on this net */
8624 if (asoc->sctp_cmt_on_off == 0) {
8625 if ((asoc->alternate) &&
8626 (asoc->alternate != net) &&
8627 (chk->whoTo == NULL)) {
8629 } else if ((net != asoc->primary_destination) &&
8630 (asoc->alternate == NULL) &&
8631 (chk->whoTo == NULL)) {
8635 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8637 * strange, we have a chunk that is
8638 * to big for its destination and
8639 * yet no fragment ok flag.
8640 * Something went wrong when the
8641 * PMTU changed...we did not mark
8642 * this chunk for some reason?? I
8643 * will fix it here by letting IP
8644 * fragment it for now and printing
8645 * a warning. This really should not
8648 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8649 chk->send_size, mtu);
8650 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8652 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8653 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
8654 struct sctp_data_chunk *dchkh;
8656 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8657 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8659 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8660 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8661 /* ok we will add this one */
8664 * Add an AUTH chunk, if chunk
8665 * requires it, save the offset into
8666 * the chain for AUTH
8668 if (data_auth_reqd) {
8670 outchain = sctp_add_auth_chunk(outchain,
8676 auth_keyid = chk->auth_keyid;
8678 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8679 } else if (override_ok) {
8684 auth_keyid = chk->auth_keyid;
8686 } else if (auth_keyid != chk->auth_keyid) {
8694 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8695 chk->send_size, chk->copy_by_ref);
8696 if (outchain == NULL) {
8697 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8698 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8699 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8702 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8705 /* update our MTU size */
8706 /* Do clear IP_DF ? */
8707 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8710 /* unsigned subtraction of mtu */
8711 if (mtu > chk->send_size)
8712 mtu -= chk->send_size;
8715 /* unsigned subtraction of r_mtu */
8716 if (r_mtu > chk->send_size)
8717 r_mtu -= chk->send_size;
8721 to_out += chk->send_size;
8722 if ((to_out > mx_mtu) && no_fragmentflg) {
8724 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8726 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8730 chk->window_probe = 0;
8731 data_list[bundle_at++] = chk;
8732 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8735 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8736 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8737 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8739 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8741 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8742 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8752 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8754 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8755 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8756 data_list[0]->window_probe = 1;
8757 net->window_probe = 1;
8763 * Must be sent in order of the
8764 * TSN's (on a network)
8768 } /* for (chunk gather loop for this net) */
8769 } /* if asoc.state OPEN */
8771 /* Is there something to send for this destination? */
8773 switch (asoc->snd_edmid) {
8774 case SCTP_EDMID_LOWER_LAYER_DTLS:
8775 use_zero_crc = true;
8778 use_zero_crc = false;
8781 /* We may need to start a control timer or two */
8783 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8785 use_zero_crc = false;
8787 * do NOT clear the asconf flag as it is
8788 * used to do appropriate source address
8793 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8794 use_zero_crc = false;
8797 /* must start a send timer if data is being sent */
8798 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8800 * no timer running on this destination
8803 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8805 if (bundle_at || hbflag) {
8806 /* For data/asconf and hb set time */
8807 if (*now_filled == 0) {
8808 (void)SCTP_GETTIME_TIMEVAL(now);
8811 net->last_sent_time = *now;
8813 /* Now send it, if there is anything to send :> */
8814 if ((error = sctp_lowlevel_chunk_output(inp,
8817 (struct sockaddr *)&net->ro._l_addr,
8825 inp->sctp_lport, stcb->rport,
8826 htonl(stcb->asoc.peer_vtag),
8831 /* error, we could not output */
8832 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8833 if (from_where == 0) {
8834 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8836 if (error == ENOBUFS) {
8837 asoc->ifp_had_enobuf = 1;
8838 SCTP_STAT_INCR(sctps_lowlevelerr);
8840 if (error == EHOSTUNREACH) {
8842 * Destination went unreachable
8845 sctp_move_chunks_from_net(stcb, net);
8850 * I add this line to be paranoid. As far as
8851 * I can tell the continue, takes us back to
8852 * the top of the for, but just to make sure
8853 * I will reset these again here.
8856 continue; /* This takes us back to the
8857 * for() for the nets. */
8859 asoc->ifp_had_enobuf = 0;
8866 *num_out += (ctl_cnt + bundle_at);
8869 /* setup for a RTO measurement */
8870 tsns_sent = data_list[0]->rec.data.tsn;
8871 /* fill time if not already filled */
8872 if (*now_filled == 0) {
8873 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8875 *now = asoc->time_last_sent;
8877 asoc->time_last_sent = *now;
8879 if (net->rto_needed) {
8880 data_list[0]->do_rtt = 1;
8881 net->rto_needed = 0;
8883 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8884 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8890 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8891 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8894 if (old_start_at == NULL) {
8895 old_start_at = start_at;
8896 start_at = TAILQ_FIRST(&asoc->nets);
8898 goto again_one_more_time;
8902 * At the end there should be no NON timed chunks hanging on this
8905 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8906 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8908 if ((*num_out == 0) && (*reason_code == 0)) {
8913 sctp_clean_up_ctl(stcb, asoc, so_locked);
8918 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8921 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8922 * the control chunk queue.
8924 struct sctp_chunkhdr *hdr;
8925 struct sctp_tmit_chunk *chk;
8926 struct mbuf *mat, *last_mbuf;
8927 uint32_t chunk_length;
8928 uint16_t padding_length;
8930 SCTP_TCB_LOCK_ASSERT(stcb);
8931 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
8932 if (op_err == NULL) {
8937 for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
8938 chunk_length += SCTP_BUF_LEN(mat);
8939 if (SCTP_BUF_NEXT(mat) == NULL) {
8943 if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
8944 sctp_m_freem(op_err);
8947 padding_length = chunk_length % 4;
8948 if (padding_length != 0) {
8949 padding_length = 4 - padding_length;
8951 if (padding_length != 0) {
8952 if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
8953 sctp_m_freem(op_err);
8957 sctp_alloc_a_chunk(stcb, chk);
8960 sctp_m_freem(op_err);
8963 chk->copy_by_ref = 0;
8964 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8965 chk->rec.chunk_id.can_take_data = 0;
8967 chk->send_size = (uint16_t)chunk_length;
8968 chk->sent = SCTP_DATAGRAM_UNSENT;
8970 chk->asoc = &stcb->asoc;
8973 hdr = mtod(op_err, struct sctp_chunkhdr *);
8974 hdr->chunk_type = SCTP_OPERATION_ERROR;
8975 hdr->chunk_flags = 0;
8976 hdr->chunk_length = htons(chk->send_size);
8977 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8978 chk->asoc->ctrl_queue_cnt++;
8982 sctp_send_cookie_echo(struct mbuf *m,
8983 int offset, int limit,
8984 struct sctp_tcb *stcb,
8985 struct sctp_nets *net)
8988 * pull out the cookie and put it at the front of the control chunk
8992 struct mbuf *cookie;
8993 struct sctp_paramhdr param, *phdr;
8994 struct sctp_chunkhdr *hdr;
8995 struct sctp_tmit_chunk *chk;
8996 uint16_t ptype, plen;
8998 SCTP_TCB_LOCK_ASSERT(stcb);
8999 /* First find the cookie in the param area */
9001 at = offset + sizeof(struct sctp_init_chunk);
9003 phdr = sctp_get_next_param(m, at, ¶m, sizeof(param));
9007 ptype = ntohs(phdr->param_type);
9008 plen = ntohs(phdr->param_length);
9009 if (plen < sizeof(struct sctp_paramhdr)) {
9012 if (ptype == SCTP_STATE_COOKIE) {
9015 /* found the cookie */
9016 if (at + plen > limit) {
9019 cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
9020 if (cookie == NULL) {
9024 if ((pad = (plen % 4)) > 0) {
9028 if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
9032 #ifdef SCTP_MBUF_LOGGING
9033 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9034 sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
9039 at += SCTP_SIZE32(plen);
9041 /* ok, we got the cookie lets change it into a cookie echo chunk */
9042 /* first the change from param to cookie */
9043 hdr = mtod(cookie, struct sctp_chunkhdr *);
9044 hdr->chunk_type = SCTP_COOKIE_ECHO;
9045 hdr->chunk_flags = 0;
9046 /* get the chunk stuff now and place it in the FRONT of the queue */
9047 sctp_alloc_a_chunk(stcb, chk);
9050 sctp_m_freem(cookie);
9053 chk->copy_by_ref = 0;
9054 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
9055 chk->rec.chunk_id.can_take_data = 0;
9056 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9057 chk->send_size = SCTP_SIZE32(plen);
9058 chk->sent = SCTP_DATAGRAM_UNSENT;
9060 chk->asoc = &stcb->asoc;
9063 atomic_add_int(&chk->whoTo->ref_count, 1);
9064 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
9065 chk->asoc->ctrl_queue_cnt++;
9070 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9074 struct sctp_nets *net)
9077 * take a HB request and make it into a HB ack and send it.
9079 struct mbuf *outchain;
9080 struct sctp_chunkhdr *chdr;
9081 struct sctp_tmit_chunk *chk;
9084 /* must have a net pointer */
9087 outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9088 if (outchain == NULL) {
9089 /* gak out of memory */
9092 #ifdef SCTP_MBUF_LOGGING
9093 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9094 sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9097 chdr = mtod(outchain, struct sctp_chunkhdr *);
9098 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9099 chdr->chunk_flags = 0;
9100 if (chk_length % 4 != 0) {
9101 sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9103 sctp_alloc_a_chunk(stcb, chk);
9106 sctp_m_freem(outchain);
9109 chk->copy_by_ref = 0;
9110 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9111 chk->rec.chunk_id.can_take_data = 1;
9113 chk->send_size = chk_length;
9114 chk->sent = SCTP_DATAGRAM_UNSENT;
9116 chk->asoc = &stcb->asoc;
9117 chk->data = outchain;
9119 atomic_add_int(&chk->whoTo->ref_count, 1);
9120 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9121 chk->asoc->ctrl_queue_cnt++;
9125 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9127 /* formulate and queue a cookie-ack back to sender */
9128 struct mbuf *cookie_ack;
9129 struct sctp_chunkhdr *hdr;
9130 struct sctp_tmit_chunk *chk;
9132 SCTP_TCB_LOCK_ASSERT(stcb);
9134 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9135 if (cookie_ack == NULL) {
9139 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9140 sctp_alloc_a_chunk(stcb, chk);
9143 sctp_m_freem(cookie_ack);
9146 chk->copy_by_ref = 0;
9147 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9148 chk->rec.chunk_id.can_take_data = 1;
9150 chk->send_size = sizeof(struct sctp_chunkhdr);
9151 chk->sent = SCTP_DATAGRAM_UNSENT;
9153 chk->asoc = &stcb->asoc;
9154 chk->data = cookie_ack;
9155 if (chk->asoc->last_control_chunk_from != NULL) {
9156 chk->whoTo = chk->asoc->last_control_chunk_from;
9157 atomic_add_int(&chk->whoTo->ref_count, 1);
9161 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9162 hdr->chunk_type = SCTP_COOKIE_ACK;
9163 hdr->chunk_flags = 0;
9164 hdr->chunk_length = htons(chk->send_size);
9165 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9166 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9167 chk->asoc->ctrl_queue_cnt++;
9172 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9174 /* formulate and queue a SHUTDOWN-ACK back to the sender */
9175 struct mbuf *m_shutdown_ack;
9176 struct sctp_shutdown_ack_chunk *ack_cp;
9177 struct sctp_tmit_chunk *chk;
9179 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9180 if (m_shutdown_ack == NULL) {
9184 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9185 sctp_alloc_a_chunk(stcb, chk);
9188 sctp_m_freem(m_shutdown_ack);
9191 chk->copy_by_ref = 0;
9192 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9193 chk->rec.chunk_id.can_take_data = 1;
9195 chk->send_size = sizeof(struct sctp_chunkhdr);
9196 chk->sent = SCTP_DATAGRAM_UNSENT;
9198 chk->asoc = &stcb->asoc;
9199 chk->data = m_shutdown_ack;
9202 atomic_add_int(&chk->whoTo->ref_count, 1);
9204 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9205 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9206 ack_cp->ch.chunk_flags = 0;
9207 ack_cp->ch.chunk_length = htons(chk->send_size);
9208 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9209 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9210 chk->asoc->ctrl_queue_cnt++;
9215 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9217 /* formulate and queue a SHUTDOWN to the sender */
9218 struct mbuf *m_shutdown;
9219 struct sctp_shutdown_chunk *shutdown_cp;
9220 struct sctp_tmit_chunk *chk;
9222 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9223 if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9224 /* We already have a SHUTDOWN queued. Reuse it. */
9226 sctp_free_remote_addr(chk->whoTo);
9233 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9234 if (m_shutdown == NULL) {
9238 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9239 sctp_alloc_a_chunk(stcb, chk);
9242 sctp_m_freem(m_shutdown);
9245 chk->copy_by_ref = 0;
9246 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9247 chk->rec.chunk_id.can_take_data = 1;
9249 chk->send_size = sizeof(struct sctp_shutdown_chunk);
9250 chk->sent = SCTP_DATAGRAM_UNSENT;
9252 chk->asoc = &stcb->asoc;
9253 chk->data = m_shutdown;
9256 atomic_add_int(&chk->whoTo->ref_count, 1);
9258 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9259 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9260 shutdown_cp->ch.chunk_flags = 0;
9261 shutdown_cp->ch.chunk_length = htons(chk->send_size);
9262 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9263 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9264 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9265 chk->asoc->ctrl_queue_cnt++;
9267 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9270 atomic_add_int(&chk->whoTo->ref_count, 1);
9272 shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9273 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9274 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9280 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9283 * formulate and queue an ASCONF to the peer. ASCONF parameters
9284 * should be queued on the assoc queue.
9286 struct sctp_tmit_chunk *chk;
9287 struct mbuf *m_asconf;
9290 SCTP_TCB_LOCK_ASSERT(stcb);
9292 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9293 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9294 /* can't send a new one if there is one in flight already */
9298 /* compose an ASCONF chunk, maximum length is PMTU */
9299 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9300 if (m_asconf == NULL) {
9304 sctp_alloc_a_chunk(stcb, chk);
9307 sctp_m_freem(m_asconf);
9311 chk->copy_by_ref = 0;
9312 chk->rec.chunk_id.id = SCTP_ASCONF;
9313 chk->rec.chunk_id.can_take_data = 0;
9314 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9315 chk->data = m_asconf;
9316 chk->send_size = len;
9317 chk->sent = SCTP_DATAGRAM_UNSENT;
9319 chk->asoc = &stcb->asoc;
9322 atomic_add_int(&chk->whoTo->ref_count, 1);
9324 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9325 chk->asoc->ctrl_queue_cnt++;
9330 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9333 * formulate and queue a asconf-ack back to sender. the asconf-ack
9334 * must be stored in the tcb.
9336 struct sctp_tmit_chunk *chk;
9337 struct sctp_asconf_ack *ack, *latest_ack;
9339 struct sctp_nets *net = NULL;
9341 SCTP_TCB_LOCK_ASSERT(stcb);
9342 /* Get the latest ASCONF-ACK */
9343 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9344 if (latest_ack == NULL) {
9347 if (latest_ack->last_sent_to != NULL &&
9348 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9349 /* we're doing a retransmission */
9350 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9353 if (stcb->asoc.last_control_chunk_from == NULL) {
9354 if (stcb->asoc.alternate) {
9355 net = stcb->asoc.alternate;
9357 net = stcb->asoc.primary_destination;
9360 net = stcb->asoc.last_control_chunk_from;
9365 if (stcb->asoc.last_control_chunk_from == NULL) {
9366 if (stcb->asoc.alternate) {
9367 net = stcb->asoc.alternate;
9369 net = stcb->asoc.primary_destination;
9372 net = stcb->asoc.last_control_chunk_from;
9375 latest_ack->last_sent_to = net;
9377 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9378 if (ack->data == NULL) {
9382 /* copy the asconf_ack */
9383 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
9384 if (m_ack == NULL) {
9385 /* couldn't copy it */
9388 #ifdef SCTP_MBUF_LOGGING
9389 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9390 sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
9394 sctp_alloc_a_chunk(stcb, chk);
9398 sctp_m_freem(m_ack);
9401 chk->copy_by_ref = 0;
9402 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9403 chk->rec.chunk_id.can_take_data = 1;
9404 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9407 atomic_add_int(&chk->whoTo->ref_count, 1);
9410 chk->send_size = ack->len;
9411 chk->sent = SCTP_DATAGRAM_UNSENT;
9413 chk->asoc = &stcb->asoc;
9415 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9416 chk->asoc->ctrl_queue_cnt++;
9422 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9423 struct sctp_tcb *stcb,
9424 struct sctp_association *asoc,
9425 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
9428 * send out one MTU of retransmission. If fast_retransmit is
9429 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9430 * rwnd. For a Cookie or Asconf in the control chunk queue we
9431 * retransmit them by themselves.
9433 * For data chunks we will pick out the lowest TSN's in the sent_queue
9434 * marked for resend and bundle them all together (up to a MTU of
9435 * destination). The address to send to should have been
9436 * selected/changed where the retransmission was marked (i.e. in FR
9437 * or t3-timeout routines).
9439 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9440 struct sctp_tmit_chunk *chk, *fwd;
9441 struct mbuf *m, *endofchain;
9442 struct sctp_nets *net = NULL;
9443 uint32_t tsns_sent = 0;
9444 int no_fragmentflg, bundle_at;
9446 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9447 struct sctp_auth_chunk *auth = NULL;
9448 uint32_t auth_offset = 0;
9449 uint16_t auth_keyid;
9450 int override_ok = 1;
9451 int data_auth_reqd = 0;
9455 SCTP_TCB_LOCK_ASSERT(stcb);
9456 tmr_started = ctl_cnt = 0;
9461 endofchain = m = NULL;
9462 auth_keyid = stcb->asoc.authinfo.active_keyid;
9463 #ifdef SCTP_AUDITING_ENABLED
9464 sctp_audit_log(0xC3, 1);
9466 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9467 (TAILQ_EMPTY(&asoc->control_send_queue))) {
9468 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9469 asoc->sent_queue_retran_cnt);
9470 asoc->sent_queue_cnt = 0;
9471 asoc->sent_queue_cnt_removeable = 0;
9472 /* send back 0/0 so we enter normal transmission */
9476 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9477 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9478 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9479 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9480 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9483 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9484 if (chk != asoc->str_reset) {
9486 * not eligible for retran if its
9493 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9497 * Add an AUTH chunk, if chunk requires it save the
9498 * offset into the chain for AUTH
9500 if ((auth == NULL) &&
9501 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9502 stcb->asoc.peer_auth_chunks))) {
9503 m = sctp_add_auth_chunk(m, &endofchain,
9504 &auth, &auth_offset,
9506 chk->rec.chunk_id.id);
9507 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9509 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9514 /* do we have control chunks to retransmit? */
9516 /* Start a timer no matter if we succeed or fail */
9517 switch (asoc->snd_edmid) {
9518 case SCTP_EDMID_LOWER_LAYER_DTLS:
9519 use_zero_crc = true;
9522 use_zero_crc = false;
9525 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9526 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9527 use_zero_crc = false;
9528 } else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
9529 /* XXXMT: Can this happen? */
9530 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9531 use_zero_crc = false;
9533 chk->snd_count++; /* update our count */
9534 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9535 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9536 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9537 no_fragmentflg, 0, 0,
9538 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9539 chk->whoTo->port, NULL,
9543 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9544 if (error == ENOBUFS) {
9545 asoc->ifp_had_enobuf = 1;
9546 SCTP_STAT_INCR(sctps_lowlevelerr);
9550 asoc->ifp_had_enobuf = 0;
9556 * We don't want to mark the net->sent time here since this
9557 * we use this for HB and retrans cannot measure RTT
9559 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9561 chk->sent = SCTP_DATAGRAM_SENT;
9562 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9566 /* Clean up the fwd-tsn list */
9567 sctp_clean_up_ctl(stcb, asoc, so_locked);
9572 * Ok, it is just data retransmission we need to do or that and a
9573 * fwd-tsn with it all.
9575 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9576 return (SCTP_RETRAN_DONE);
9578 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
9579 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
9580 /* not yet open, resend the cookie and that is it */
9583 #ifdef SCTP_AUDITING_ENABLED
9584 sctp_auditing(20, inp, stcb, NULL);
9586 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9587 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9588 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9589 /* No, not sent to this net or not ready for rtx */
9592 if (chk->data == NULL) {
9593 SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9594 chk->rec.data.tsn, chk->snd_count, chk->sent);
9597 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9598 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9599 struct mbuf *op_err;
9600 char msg[SCTP_DIAG_INFO_LEN];
9602 SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
9603 chk->rec.data.tsn, chk->snd_count);
9604 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
9606 atomic_add_int(&stcb->asoc.refcnt, 1);
9607 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
9609 SCTP_TCB_LOCK(stcb);
9610 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9611 return (SCTP_RETRAN_EXIT);
9613 /* pick up the net */
9615 switch (net->ro._l_addr.sa.sa_family) {
9618 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9623 mtu = net->mtu - SCTP_MIN_OVERHEAD;
9632 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9633 /* No room in peers rwnd */
9636 tsn = asoc->last_acked_seq + 1;
9637 if (tsn == chk->rec.data.tsn) {
9639 * we make a special exception for this
9640 * case. The peer has no rwnd but is missing
9641 * the lowest chunk.. which is probably what
9642 * is holding up the rwnd.
9644 goto one_chunk_around;
9649 if (asoc->peers_rwnd < mtu) {
9651 if ((asoc->peers_rwnd == 0) &&
9652 (asoc->total_flight == 0)) {
9653 chk->window_probe = 1;
9654 chk->whoTo->window_probe = 1;
9657 #ifdef SCTP_AUDITING_ENABLED
9658 sctp_audit_log(0xC3, 2);
9662 net->fast_retran_ip = 0;
9663 if (chk->rec.data.doing_fast_retransmit == 0) {
9665 * if no FR in progress skip destination that have
9666 * flight_size > cwnd.
9668 if (net->flight_size >= net->cwnd) {
9673 * Mark the destination net to have FR recovery
9677 net->fast_retran_ip = 1;
9681 * if no AUTH is yet included and this chunk requires it,
9682 * make sure to account for it. We don't apply the size
9683 * until the AUTH chunk is actually added below in case
9684 * there is no room for this chunk.
9686 if (data_auth_reqd && (auth == NULL)) {
9687 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9691 if ((chk->send_size <= (mtu - dmtu)) ||
9692 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9693 /* ok we will add this one */
9694 if (data_auth_reqd) {
9696 m = sctp_add_auth_chunk(m,
9702 auth_keyid = chk->auth_keyid;
9704 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9705 } else if (override_ok) {
9706 auth_keyid = chk->auth_keyid;
9708 } else if (chk->auth_keyid != auth_keyid) {
9709 /* different keyid, so done bundling */
9713 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9715 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9718 /* Do clear IP_DF ? */
9719 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9722 /* update our MTU size */
9723 if (mtu > (chk->send_size + dmtu))
9724 mtu -= (chk->send_size + dmtu);
9727 data_list[bundle_at++] = chk;
9728 if (one_chunk && (asoc->total_flight <= 0)) {
9729 SCTP_STAT_INCR(sctps_windowprobed);
9732 if (one_chunk == 0) {
9734 * now are there anymore forward from chk to pick
9737 for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9738 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9739 /* Nope, not for retran */
9742 if (fwd->whoTo != net) {
9743 /* Nope, not the net in question */
9746 if (data_auth_reqd && (auth == NULL)) {
9747 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9750 if (fwd->send_size <= (mtu - dmtu)) {
9751 if (data_auth_reqd) {
9753 m = sctp_add_auth_chunk(m,
9759 auth_keyid = fwd->auth_keyid;
9761 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9762 } else if (override_ok) {
9763 auth_keyid = fwd->auth_keyid;
9765 } else if (fwd->auth_keyid != auth_keyid) {
9773 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9775 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9778 /* Do clear IP_DF ? */
9779 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9782 /* update our MTU size */
9783 if (mtu > (fwd->send_size + dmtu))
9784 mtu -= (fwd->send_size + dmtu);
9787 data_list[bundle_at++] = fwd;
9788 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9792 /* can't fit so we are done */
9797 /* Is there something to send for this destination? */
9800 * No matter if we fail/or succeed we should start a
9801 * timer. A failure is like a lost IP packet :-)
9803 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9805 * no timer running on this destination
9808 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9811 switch (asoc->snd_edmid) {
9812 case SCTP_EDMID_LOWER_LAYER_DTLS:
9813 use_zero_crc = true;
9816 use_zero_crc = false;
9819 /* Now lets send it, if there is anything to send :> */
9820 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9821 (struct sockaddr *)&net->ro._l_addr, m,
9822 auth_offset, auth, auth_keyid,
9823 no_fragmentflg, 0, 0,
9824 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9829 /* error, we could not output */
9830 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9831 if (error == ENOBUFS) {
9832 asoc->ifp_had_enobuf = 1;
9833 SCTP_STAT_INCR(sctps_lowlevelerr);
9837 asoc->ifp_had_enobuf = 0;
9844 * We don't want to mark the net->sent time here
9845 * since this we use this for HB and retrans cannot
9848 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9850 /* For auto-close */
9851 if (*now_filled == 0) {
9852 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9853 *now = asoc->time_last_sent;
9856 asoc->time_last_sent = *now;
9858 *cnt_out += bundle_at;
9859 #ifdef SCTP_AUDITING_ENABLED
9860 sctp_audit_log(0xC4, bundle_at);
9863 tsns_sent = data_list[0]->rec.data.tsn;
9865 for (i = 0; i < bundle_at; i++) {
9866 SCTP_STAT_INCR(sctps_sendretransdata);
9867 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9869 * When we have a revoked data, and we
9870 * retransmit it, then we clear the revoked
9871 * flag since this flag dictates if we
9872 * subtracted from the fs
9874 if (data_list[i]->rec.data.chunk_was_revoked) {
9875 /* Deflate the cwnd */
9876 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9877 data_list[i]->rec.data.chunk_was_revoked = 0;
9879 data_list[i]->snd_count++;
9880 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9881 /* record the time */
9882 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9883 if (data_list[i]->book_size_scale) {
9885 * need to double the book size on
9888 data_list[i]->book_size_scale = 0;
9890 * Since we double the booksize, we
9891 * must also double the output queue
9892 * size, since this get shrunk when
9893 * we free by this amount.
9895 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9896 data_list[i]->book_size *= 2;
9898 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9899 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9900 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9902 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9903 (uint32_t)(data_list[i]->send_size +
9904 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9906 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9907 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9908 data_list[i]->whoTo->flight_size,
9909 data_list[i]->book_size,
9910 (uint32_t)(uintptr_t)data_list[i]->whoTo,
9911 data_list[i]->rec.data.tsn);
9913 sctp_flight_size_increase(data_list[i]);
9914 sctp_total_flight_increase(stcb, data_list[i]);
9915 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9916 /* SWS sender side engages */
9917 asoc->peers_rwnd = 0;
9920 (data_list[i]->rec.data.doing_fast_retransmit)) {
9921 SCTP_STAT_INCR(sctps_sendfastretrans);
9922 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9923 (tmr_started == 0)) {
9925 * ok we just fast-retrans'd
9926 * the lowest TSN, i.e the
9927 * first on the list. In
9928 * this case we want to give
9929 * some more time to get a
9930 * SACK back without a
9933 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9934 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
9935 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9939 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9940 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9942 #ifdef SCTP_AUDITING_ENABLED
9943 sctp_auditing(21, inp, stcb, NULL);
9949 if (asoc->sent_queue_retran_cnt <= 0) {
9950 /* all done we have no more to retran */
9951 asoc->sent_queue_retran_cnt = 0;
9955 /* No more room in rwnd */
9958 /* stop the for loop here. we sent out a packet */
9965 sctp_timer_validation(struct sctp_inpcb *inp,
9966 struct sctp_tcb *stcb,
9967 struct sctp_association *asoc)
9969 struct sctp_nets *net;
9971 /* Validate that a timer is running somewhere */
9972 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9973 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9974 /* Here is a timer */
9978 SCTP_TCB_LOCK_ASSERT(stcb);
9979 /* Gak, we did not have a timer somewhere */
9980 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9981 if (asoc->alternate) {
9982 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9984 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9990 sctp_chunk_output(struct sctp_inpcb *inp,
9991 struct sctp_tcb *stcb,
9996 * Ok this is the generic chunk service queue. we must do the
9998 * - See if there are retransmits pending, if so we must
10000 * - Service the stream queue that is next, moving any
10001 * message (note I must get a complete message i.e.
10002 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
10004 * - Check to see if the cwnd/rwnd allows any output, if so we
10005 * go ahead and formulate and send the low level chunks. Making sure
10006 * to combine any control in the control chunk queue also.
10008 struct sctp_association *asoc;
10009 struct sctp_nets *net;
10010 int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
10011 unsigned int burst_cnt = 0;
10012 struct timeval now;
10013 int now_filled = 0;
10015 uint32_t frag_point = sctp_get_frag_point(stcb);
10018 unsigned int tot_frs = 0;
10020 asoc = &stcb->asoc;
10022 /* The Nagle algorithm is only applied when handling a send call. */
10023 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
10024 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
10032 SCTP_TCB_LOCK_ASSERT(stcb);
10034 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
10036 if ((un_sent <= 0) &&
10037 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
10038 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
10039 (asoc->sent_queue_retran_cnt == 0) &&
10040 (asoc->trigger_reset == 0)) {
10041 /* Nothing to do unless there is something to be sent left */
10045 * Do we have something to send, data or control AND a sack timer
10046 * running, if so piggy-back the sack.
10048 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
10049 sctp_send_sack(stcb, so_locked);
10050 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
10051 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10053 while (asoc->sent_queue_retran_cnt) {
10055 * Ok, it is retransmission time only, we send out only ONE
10056 * packet with a single call off to the retran code.
10058 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
10060 * Special hook for handling cookies discarded
10061 * by peer that carried data. Send cookie-ack only
10062 * and then the next call with get the retran's.
10064 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10066 &now, &now_filled, frag_point, so_locked);
10068 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
10069 /* if its not from a HB then do it */
10071 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
10077 * its from any other place, we don't allow retran
10078 * output (only control)
10083 /* Can't send anymore */
10085 * now lets push out control by calling med-level
10086 * output once. this assures that we WILL send HB's
10089 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10091 &now, &now_filled, frag_point, so_locked);
10092 #ifdef SCTP_AUDITING_ENABLED
10093 sctp_auditing(8, inp, stcb, NULL);
10095 sctp_timer_validation(inp, stcb, asoc);
10100 * The count was off.. retran is not happening so do
10101 * the normal retransmission.
10103 #ifdef SCTP_AUDITING_ENABLED
10104 sctp_auditing(9, inp, stcb, NULL);
10106 if (ret == SCTP_RETRAN_EXIT) {
10111 if (from_where == SCTP_OUTPUT_FROM_T3) {
10112 /* Only one transmission allowed out of a timeout */
10113 #ifdef SCTP_AUDITING_ENABLED
10114 sctp_auditing(10, inp, stcb, NULL);
10116 /* Push out any control */
10117 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10118 &now, &now_filled, frag_point, so_locked);
10121 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10122 /* Hit FR burst limit */
10125 if ((num_out == 0) && (ret == 0)) {
10126 /* No more retrans to send */
10130 #ifdef SCTP_AUDITING_ENABLED
10131 sctp_auditing(12, inp, stcb, NULL);
10133 /* Check for bad destinations, if they exist move chunks around. */
10134 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10135 if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10137 * if possible move things off of this address we
10138 * still may send below due to the dormant state but
10139 * we try to find an alternate address to send to
10140 * and if we have one we move all queued data on the
10141 * out wheel to this alternate address.
10143 if (net->ref_count > 1)
10144 sctp_move_chunks_from_net(stcb, net);
10147 * if ((asoc->sat_network) || (net->addr_is_local))
10148 * { burst_limit = asoc->max_burst *
10149 * SCTP_SAT_NETWORK_BURST_INCR; }
10151 if (asoc->max_burst > 0) {
10152 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10153 if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10155 * JRS - Use the congestion
10156 * control given in the
10157 * congestion control module
10159 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10160 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10161 sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10163 SCTP_STAT_INCR(sctps_maxburstqueued);
10165 net->fast_retran_ip = 0;
10167 if (net->flight_size == 0) {
10169 * Should be decaying the
10180 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10181 &reason_code, 0, from_where,
10182 &now, &now_filled, frag_point, so_locked);
10184 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10185 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10186 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10188 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10189 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10190 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10194 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10196 tot_out += num_out;
10198 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10199 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10200 if (num_out == 0) {
10201 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10206 * When the Nagle algorithm is used, look at how
10207 * much is unsent, then if its smaller than an MTU
10208 * and we have data in flight we stop, except if we
10209 * are handling a fragmented user message.
10211 un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10212 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10213 (stcb->asoc.total_flight > 0)) {
10214 /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10218 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10219 TAILQ_EMPTY(&asoc->send_queue) &&
10220 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10221 /* Nothing left to send */
10224 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10225 /* Nothing left to send */
10228 } while (num_out &&
10229 ((asoc->max_burst == 0) ||
10230 SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10231 (burst_cnt < asoc->max_burst)));
10233 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10234 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10235 SCTP_STAT_INCR(sctps_maxburstqueued);
10236 asoc->burst_limit_applied = 1;
10237 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10238 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10241 asoc->burst_limit_applied = 0;
10244 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10245 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10247 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10251 * Now we need to clean up the control chunk chain if a ECNE is on
10252 * it. It must be marked as UNSENT again so next call will continue
10253 * to send it until such time that we get a CWR, to remove it.
10255 if (stcb->asoc.ecn_echo_cnt_onq)
10256 sctp_fix_ecn_echo(asoc);
10258 if (stcb->asoc.trigger_reset) {
10259 if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10268 struct sctp_inpcb *inp,
10270 struct sockaddr *addr,
10271 struct mbuf *control,
10276 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10280 if (inp->sctp_socket == NULL) {
10281 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10284 return (sctp_sosend(inp->sctp_socket,
10286 (struct uio *)NULL,
10294 send_forward_tsn(struct sctp_tcb *stcb,
10295 struct sctp_association *asoc)
10297 struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10298 struct sctp_forward_tsn_chunk *fwdtsn;
10299 struct sctp_strseq *strseq;
10300 struct sctp_strseq_mid *strseq_m;
10301 uint32_t advance_peer_ack_point;
10302 unsigned int cnt_of_space, i, ovh;
10303 unsigned int space_needed;
10304 unsigned int cnt_of_skipped = 0;
10306 SCTP_TCB_LOCK_ASSERT(stcb);
10307 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10308 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10309 /* mark it to unsent */
10310 chk->sent = SCTP_DATAGRAM_UNSENT;
10311 chk->snd_count = 0;
10312 /* Do we correct its output location? */
10314 sctp_free_remote_addr(chk->whoTo);
10317 goto sctp_fill_in_rest;
10320 /* Ok if we reach here we must build one */
10321 sctp_alloc_a_chunk(stcb, chk);
10325 asoc->fwd_tsn_cnt++;
10326 chk->copy_by_ref = 0;
10328 * We don't do the old thing here since this is used not for on-wire
10329 * but to tell if we are sending a fwd-tsn by the stack during
10330 * output. And if its a IFORWARD or a FORWARD it is a fwd-tsn.
10332 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10333 chk->rec.chunk_id.can_take_data = 0;
10337 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10338 if (chk->data == NULL) {
10339 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10342 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10343 chk->sent = SCTP_DATAGRAM_UNSENT;
10344 chk->snd_count = 0;
10345 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10346 asoc->ctrl_queue_cnt++;
10349 * Here we go through and fill out the part that deals with
10350 * stream/seq of the ones we skip.
10352 SCTP_BUF_LEN(chk->data) = 0;
10353 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10354 if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
10355 (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
10356 /* no more to look at */
10359 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10360 /* We don't report these */
10365 if (asoc->idata_supported) {
10366 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10367 (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
10369 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10370 (cnt_of_skipped * sizeof(struct sctp_strseq)));
10372 cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
10374 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10375 ovh = SCTP_MIN_OVERHEAD;
10377 ovh = SCTP_MIN_V4_OVERHEAD;
10379 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10380 /* trim to a mtu size */
10381 cnt_of_space = asoc->smallest_mtu - ovh;
10383 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10384 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10385 0xff, 0, cnt_of_skipped,
10386 asoc->advanced_peer_ack_point);
10388 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10389 if (cnt_of_space < space_needed) {
10391 * ok we must trim down the chunk by lowering the
10392 * advance peer ack point.
10394 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10395 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10396 0xff, 0xff, cnt_of_space,
10399 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10400 if (asoc->idata_supported) {
10401 cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
10403 cnt_of_skipped /= sizeof(struct sctp_strseq);
10406 * Go through and find the TSN that will be the one
10409 at = TAILQ_FIRST(&asoc->sent_queue);
10411 for (i = 0; i < cnt_of_skipped; i++) {
10412 tp1 = TAILQ_NEXT(at, sctp_next);
10419 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10420 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10421 0xff, cnt_of_skipped, at->rec.data.tsn,
10422 asoc->advanced_peer_ack_point);
10426 * last now points to last one I can report, update
10430 advance_peer_ack_point = last->rec.data.tsn;
10432 if (asoc->idata_supported) {
10433 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10434 cnt_of_skipped * sizeof(struct sctp_strseq_mid);
10436 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10437 cnt_of_skipped * sizeof(struct sctp_strseq);
10440 chk->send_size = space_needed;
10441 /* Setup the chunk */
10442 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10443 fwdtsn->ch.chunk_length = htons(chk->send_size);
10444 fwdtsn->ch.chunk_flags = 0;
10445 if (asoc->idata_supported) {
10446 fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
10448 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10450 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10451 SCTP_BUF_LEN(chk->data) = chk->send_size;
10454 * Move pointer to after the fwdtsn and transfer to the
10457 if (asoc->idata_supported) {
10458 strseq_m = (struct sctp_strseq_mid *)fwdtsn;
10461 strseq = (struct sctp_strseq *)fwdtsn;
10465 * Now populate the strseq list. This is done blindly
10466 * without pulling out duplicate stream info. This is
10467 * inefficient but won't harm the process since the peer will
10468 * look at these in sequence and will thus release anything.
10469 * It could mean we exceed the PMTU and chop off some that
10470 * we could have included.. but this is unlikely (aka 1432/4
10471 * would mean 300+ stream seq's would have to be reported in
10472 * one FWD-TSN. With a bit of work we can later FIX this to
10473 * optimize and pull out duplicates.. but it does add more
10474 * overhead. So for now... not!
10477 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10478 if (i >= cnt_of_skipped) {
10481 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10482 /* We don't report these */
10485 if (at->rec.data.tsn == advance_peer_ack_point) {
10486 at->rec.data.fwd_tsn_cnt = 0;
10488 if (asoc->idata_supported) {
10489 strseq_m->sid = htons(at->rec.data.sid);
10490 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10491 strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
10493 strseq_m->flags = 0;
10495 strseq_m->mid = htonl(at->rec.data.mid);
10498 strseq->sid = htons(at->rec.data.sid);
10499 strseq->ssn = htons((uint16_t)at->rec.data.mid);
10508 sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
10511 * Queue up a SACK or NR-SACK in the control queue.
10512 * We must first check to see if a SACK or NR-SACK is
10513 * somehow on the control queue.
10514 * If so, we will take and and remove the old one.
10516 struct sctp_association *asoc;
10517 struct sctp_tmit_chunk *chk, *a_chk;
10518 struct sctp_sack_chunk *sack;
10519 struct sctp_nr_sack_chunk *nr_sack;
10520 struct sctp_gap_ack_block *gap_descriptor;
10521 const struct sack_track *selector;
10526 int limit_reached = 0;
10527 unsigned int i, siz, j;
10528 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10531 uint32_t highest_tsn;
10536 if (stcb->asoc.nrsack_supported == 1) {
10537 type = SCTP_NR_SELECTIVE_ACK;
10539 type = SCTP_SELECTIVE_ACK;
10542 asoc = &stcb->asoc;
10543 SCTP_TCB_LOCK_ASSERT(stcb);
10544 if (asoc->last_data_chunk_from == NULL) {
10545 /* Hmm we never received anything */
10548 sctp_slide_mapping_arrays(stcb);
10549 sctp_set_rwnd(stcb, asoc);
10550 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10551 if (chk->rec.chunk_id.id == type) {
10552 /* Hmm, found a sack already on queue, remove it */
10553 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10554 asoc->ctrl_queue_cnt--;
10557 sctp_m_freem(a_chk->data);
10558 a_chk->data = NULL;
10560 if (a_chk->whoTo) {
10561 sctp_free_remote_addr(a_chk->whoTo);
10562 a_chk->whoTo = NULL;
10567 if (a_chk == NULL) {
10568 sctp_alloc_a_chunk(stcb, a_chk);
10569 if (a_chk == NULL) {
10570 /* No memory so we drop the idea, and set a timer */
10571 if (stcb->asoc.delayed_ack) {
10572 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10573 stcb->sctp_ep, stcb, NULL,
10574 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
10575 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10576 stcb->sctp_ep, stcb, NULL);
10578 stcb->asoc.send_sack = 1;
10582 a_chk->copy_by_ref = 0;
10583 a_chk->rec.chunk_id.id = type;
10584 a_chk->rec.chunk_id.can_take_data = 1;
10586 /* Clear our pkt counts */
10587 asoc->data_pkts_seen = 0;
10590 a_chk->asoc = asoc;
10591 a_chk->snd_count = 0;
10592 a_chk->send_size = 0; /* fill in later */
10593 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10594 a_chk->whoTo = NULL;
10596 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10598 * Ok, the destination for the SACK is unreachable, lets see if
10599 * we can select an alternate to asoc->last_data_chunk_from
10601 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10602 if (a_chk->whoTo == NULL) {
10603 /* Nope, no alternate */
10604 a_chk->whoTo = asoc->last_data_chunk_from;
10607 a_chk->whoTo = asoc->last_data_chunk_from;
10609 if (a_chk->whoTo) {
10610 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10612 if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10613 highest_tsn = asoc->highest_tsn_inside_map;
10615 highest_tsn = asoc->highest_tsn_inside_nr_map;
10617 if (highest_tsn == asoc->cumulative_tsn) {
10619 if (type == SCTP_SELECTIVE_ACK) {
10620 space_req = sizeof(struct sctp_sack_chunk);
10622 space_req = sizeof(struct sctp_nr_sack_chunk);
10625 /* gaps get a cluster */
10626 space_req = MCLBYTES;
10628 /* Ok now lets formulate a MBUF with our sack */
10629 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
10630 if ((a_chk->data == NULL) ||
10631 (a_chk->whoTo == NULL)) {
10632 /* rats, no mbuf memory */
10634 /* was a problem with the destination */
10635 sctp_m_freem(a_chk->data);
10636 a_chk->data = NULL;
10638 sctp_free_a_chunk(stcb, a_chk, so_locked);
10639 /* sa_ignore NO_NULL_CHK */
10640 if (stcb->asoc.delayed_ack) {
10641 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10642 stcb->sctp_ep, stcb, NULL,
10643 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10644 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10645 stcb->sctp_ep, stcb, NULL);
10647 stcb->asoc.send_sack = 1;
10651 /* ok, lets go through and fill it in */
10652 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10653 space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
10654 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10655 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10657 limit = mtod(a_chk->data, caddr_t);
10662 if ((asoc->sctp_cmt_on_off > 0) &&
10663 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10665 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10666 * received, then set high bit to 1, else 0. Reset
10669 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10670 asoc->cmt_dac_pkts_rcvd = 0;
10672 #ifdef SCTP_ASOCLOG_OF_TSNS
10673 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10674 stcb->asoc.cumack_log_atsnt++;
10675 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10676 stcb->asoc.cumack_log_atsnt = 0;
10679 /* reset the readers interpretation */
10680 stcb->freed_by_sorcv_sincelast = 0;
10682 if (type == SCTP_SELECTIVE_ACK) {
10683 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10685 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10686 if (highest_tsn > asoc->mapping_array_base_tsn) {
10687 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10689 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
10693 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10694 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10695 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10696 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10698 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10702 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10705 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10707 if (((type == SCTP_SELECTIVE_ACK) &&
10708 SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10709 ((type == SCTP_NR_SELECTIVE_ACK) &&
10710 SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10711 /* we have a gap .. maybe */
10712 for (i = 0; i < siz; i++) {
10713 tsn_map = asoc->mapping_array[i];
10714 if (type == SCTP_SELECTIVE_ACK) {
10715 tsn_map |= asoc->nr_mapping_array[i];
10719 * Clear all bits corresponding to TSNs
10720 * smaller or equal to the cumulative TSN.
10722 tsn_map &= (~0U << (1 - offset));
10724 selector = &sack_array[tsn_map];
10725 if (mergeable && selector->right_edge) {
10727 * Backup, left and right edges were ok to
10733 if (selector->num_entries == 0)
10736 for (j = 0; j < selector->num_entries; j++) {
10737 if (mergeable && selector->right_edge) {
10739 * do a merge by NOT setting
10745 * no merge, set the left
10749 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10751 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10754 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10760 if (selector->left_edge) {
10764 if (limit_reached) {
10765 /* Reached the limit stop */
10771 if ((type == SCTP_NR_SELECTIVE_ACK) &&
10772 (limit_reached == 0)) {
10775 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10776 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10778 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10781 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10784 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10786 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10787 /* we have a gap .. maybe */
10788 for (i = 0; i < siz; i++) {
10789 tsn_map = asoc->nr_mapping_array[i];
10792 * Clear all bits corresponding to
10793 * TSNs smaller or equal to the
10796 tsn_map &= (~0U << (1 - offset));
10798 selector = &sack_array[tsn_map];
10799 if (mergeable && selector->right_edge) {
10801 * Backup, left and right edges were
10804 num_nr_gap_blocks--;
10807 if (selector->num_entries == 0)
10810 for (j = 0; j < selector->num_entries; j++) {
10811 if (mergeable && selector->right_edge) {
10813 * do a merge by NOT
10820 * no merge, set the
10824 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10826 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10827 num_nr_gap_blocks++;
10829 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10835 if (selector->left_edge) {
10839 if (limit_reached) {
10840 /* Reached the limit stop */
10847 /* now we must add any dups we are going to report. */
10848 if ((limit_reached == 0) && (asoc->numduptsns)) {
10849 dup = (uint32_t *)gap_descriptor;
10850 for (i = 0; i < asoc->numduptsns; i++) {
10851 *dup = htonl(asoc->dup_tsns[i]);
10854 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10859 asoc->numduptsns = 0;
10862 * now that the chunk is prepared queue it to the control chunk
10865 if (type == SCTP_SELECTIVE_ACK) {
10866 a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
10867 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10868 num_dups * sizeof(int32_t));
10869 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10870 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10871 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10872 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10873 sack->sack.num_dup_tsns = htons(num_dups);
10874 sack->ch.chunk_type = type;
10875 sack->ch.chunk_flags = flags;
10876 sack->ch.chunk_length = htons(a_chk->send_size);
10878 a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
10879 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10880 num_dups * sizeof(int32_t));
10881 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10882 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10883 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10884 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10885 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10886 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10887 nr_sack->nr_sack.reserved = 0;
10888 nr_sack->ch.chunk_type = type;
10889 nr_sack->ch.chunk_flags = flags;
10890 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10892 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10893 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10894 asoc->ctrl_queue_cnt++;
10895 asoc->send_sack = 0;
10896 SCTP_STAT_INCR(sctps_sendsacks);
10901 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
10903 struct mbuf *m_abort, *m, *m_last;
10904 struct mbuf *m_out, *m_end = NULL;
10905 struct sctp_abort_chunk *abort;
10906 struct sctp_auth_chunk *auth = NULL;
10907 struct sctp_nets *net;
10909 uint32_t auth_offset = 0;
10911 uint16_t cause_len, chunk_len, padding_len;
10914 SCTP_TCB_LOCK_ASSERT(stcb);
10916 * Add an AUTH chunk, if chunk requires it and save the offset into
10917 * the chain for AUTH
10919 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10920 stcb->asoc.peer_auth_chunks)) {
10921 m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
10922 stcb, SCTP_ABORT_ASSOCIATION);
10923 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10927 switch (stcb->asoc.snd_edmid) {
10928 case SCTP_EDMID_LOWER_LAYER_DTLS:
10929 use_zero_crc = true;
10932 use_zero_crc = false;
10935 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
10936 if (m_abort == NULL) {
10938 sctp_m_freem(m_out);
10941 sctp_m_freem(operr);
10945 /* link in any error */
10946 SCTP_BUF_NEXT(m_abort) = operr;
10949 for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
10950 cause_len += (uint16_t)SCTP_BUF_LEN(m);
10951 if (SCTP_BUF_NEXT(m) == NULL) {
10955 SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
10956 chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
10957 padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
10958 if (m_out == NULL) {
10959 /* NO Auth chunk prepended, so reserve space in front */
10960 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10963 /* Put AUTH chunk at the front of the chain */
10964 SCTP_BUF_NEXT(m_end) = m_abort;
10966 if (stcb->asoc.alternate) {
10967 net = stcb->asoc.alternate;
10969 net = stcb->asoc.primary_destination;
10971 /* Fill in the ABORT chunk header. */
10972 abort = mtod(m_abort, struct sctp_abort_chunk *);
10973 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10974 if (stcb->asoc.peer_vtag == 0) {
10975 /* This happens iff the assoc is in COOKIE-WAIT state. */
10976 vtag = stcb->asoc.my_vtag;
10977 abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
10979 vtag = stcb->asoc.peer_vtag;
10980 abort->ch.chunk_flags = 0;
10982 abort->ch.chunk_length = htons(chunk_len);
10983 /* Add padding, if necessary. */
10984 if (padding_len > 0) {
10985 if ((m_last == NULL) ||
10986 (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
10987 sctp_m_freem(m_out);
10991 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10992 (struct sockaddr *)&net->ro._l_addr,
10993 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10994 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
10995 stcb->asoc.primary_destination->port, NULL,
10999 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11000 if (error == ENOBUFS) {
11001 stcb->asoc.ifp_had_enobuf = 1;
11002 SCTP_STAT_INCR(sctps_lowlevelerr);
11005 stcb->asoc.ifp_had_enobuf = 0;
11007 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11011 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
11012 struct sctp_nets *net,
11015 /* formulate and SEND a SHUTDOWN-COMPLETE */
11016 struct mbuf *m_shutdown_comp;
11017 struct sctp_shutdown_complete_chunk *shutdown_complete;
11023 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
11024 if (m_shutdown_comp == NULL) {
11028 if (reflect_vtag) {
11029 flags = SCTP_HAD_NO_TCB;
11030 vtag = stcb->asoc.my_vtag;
11033 vtag = stcb->asoc.peer_vtag;
11035 switch (stcb->asoc.snd_edmid) {
11036 case SCTP_EDMID_LOWER_LAYER_DTLS:
11037 use_zero_crc = true;
11040 use_zero_crc = false;
11043 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
11044 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11045 shutdown_complete->ch.chunk_flags = flags;
11046 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11047 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
11048 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
11049 (struct sockaddr *)&net->ro._l_addr,
11050 m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
11051 stcb->sctp_ep->sctp_lport, stcb->rport,
11056 SCTP_SO_NOT_LOCKED))) {
11057 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11058 if (error == ENOBUFS) {
11059 stcb->asoc.ifp_had_enobuf = 1;
11060 SCTP_STAT_INCR(sctps_lowlevelerr);
11063 stcb->asoc.ifp_had_enobuf = 0;
11065 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11070 sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
11071 struct sctphdr *sh, uint32_t vtag,
11072 uint8_t type, struct mbuf *cause,
11073 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11074 uint32_t vrf_id, uint16_t port)
11076 struct mbuf *o_pak;
11078 struct sctphdr *shout;
11079 struct sctp_chunkhdr *ch;
11080 #if defined(INET) || defined(INET6)
11081 struct udphdr *udp;
11083 int ret, len, cause_len, padding_len;
11085 struct sockaddr_in *src_sin, *dst_sin;
11089 struct sockaddr_in6 *src_sin6, *dst_sin6;
11090 struct ip6_hdr *ip6;
11093 /* Compute the length of the cause and add final padding. */
11095 if (cause != NULL) {
11096 struct mbuf *m_at, *m_last = NULL;
11098 for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
11099 if (SCTP_BUF_NEXT(m_at) == NULL)
11101 cause_len += SCTP_BUF_LEN(m_at);
11103 padding_len = cause_len % 4;
11104 if (padding_len != 0) {
11105 padding_len = 4 - padding_len;
11107 if (padding_len != 0) {
11108 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
11109 sctp_m_freem(cause);
11116 /* Get an mbuf for the header. */
11117 len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11118 switch (dst->sa_family) {
11121 len += sizeof(struct ip);
11126 len += sizeof(struct ip6_hdr);
11132 #if defined(INET) || defined(INET6)
11134 len += sizeof(struct udphdr);
11137 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11138 if (mout == NULL) {
11140 sctp_m_freem(cause);
11144 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11145 SCTP_BUF_LEN(mout) = len;
11146 SCTP_BUF_NEXT(mout) = cause;
11147 M_SETFIB(mout, fibnum);
11148 mout->m_pkthdr.flowid = mflowid;
11149 M_HASHTYPE_SET(mout, mflowtype);
11156 switch (dst->sa_family) {
11159 src_sin = (struct sockaddr_in *)src;
11160 dst_sin = (struct sockaddr_in *)dst;
11161 ip = mtod(mout, struct ip *);
11162 ip->ip_v = IPVERSION;
11163 ip->ip_hl = (sizeof(struct ip) >> 2);
11165 ip->ip_off = htons(IP_DF);
11167 ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11169 ip->ip_p = IPPROTO_UDP;
11171 ip->ip_p = IPPROTO_SCTP;
11173 ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11174 ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11176 len = sizeof(struct ip);
11177 shout = (struct sctphdr *)((caddr_t)ip + len);
11182 src_sin6 = (struct sockaddr_in6 *)src;
11183 dst_sin6 = (struct sockaddr_in6 *)dst;
11184 ip6 = mtod(mout, struct ip6_hdr *);
11185 ip6->ip6_flow = htonl(0x60000000);
11186 if (V_ip6_auto_flowlabel) {
11187 ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11189 ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11191 ip6->ip6_nxt = IPPROTO_UDP;
11193 ip6->ip6_nxt = IPPROTO_SCTP;
11195 ip6->ip6_src = dst_sin6->sin6_addr;
11196 ip6->ip6_dst = src_sin6->sin6_addr;
11197 len = sizeof(struct ip6_hdr);
11198 shout = (struct sctphdr *)((caddr_t)ip6 + len);
11203 shout = mtod(mout, struct sctphdr *);
11206 #if defined(INET) || defined(INET6)
11208 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11209 sctp_m_freem(mout);
11212 udp = (struct udphdr *)shout;
11213 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11214 udp->uh_dport = port;
11216 udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11217 sizeof(struct sctphdr) +
11218 sizeof(struct sctp_chunkhdr) +
11219 cause_len + padding_len));
11220 len += sizeof(struct udphdr);
11221 shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11226 shout->src_port = sh->dest_port;
11227 shout->dest_port = sh->src_port;
11228 shout->checksum = 0;
11230 shout->v_tag = htonl(vtag);
11232 shout->v_tag = sh->v_tag;
11234 len += sizeof(struct sctphdr);
11235 ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11236 ch->chunk_type = type;
11238 ch->chunk_flags = 0;
11240 ch->chunk_flags = SCTP_HAD_NO_TCB;
11242 ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11243 len += sizeof(struct sctp_chunkhdr);
11244 len += cause_len + padding_len;
11246 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11247 sctp_m_freem(mout);
11250 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11251 switch (dst->sa_family) {
11256 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11261 ip->ip_len = htons(len);
11263 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11264 SCTP_STAT_INCR(sctps_sendswcrc);
11266 SCTP_ENABLE_UDP_CSUM(o_pak);
11269 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11270 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11271 SCTP_STAT_INCR(sctps_sendhwcrc);
11273 #ifdef SCTP_PACKET_LOGGING
11274 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11275 sctp_packet_log(o_pak);
11278 SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
11279 SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
11284 ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
11286 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11287 SCTP_STAT_INCR(sctps_sendswcrc);
11288 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11289 udp->uh_sum = 0xffff;
11292 mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
11293 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11294 SCTP_STAT_INCR(sctps_sendhwcrc);
11296 #ifdef SCTP_PACKET_LOGGING
11297 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11298 sctp_packet_log(o_pak);
11301 SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
11302 SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
11306 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
11308 sctp_m_freem(mout);
11309 SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11312 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
11314 UDPSTAT_INC(udps_opackets);
11316 SCTP_STAT_INCR(sctps_sendpackets);
11317 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11318 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11320 SCTP_STAT_INCR(sctps_senderrors);
11326 sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
11327 struct sctphdr *sh,
11328 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11329 uint32_t vrf_id, uint16_t port)
11331 sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
11332 mflowtype, mflowid, fibnum,
11337 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked)
11339 struct sctp_tmit_chunk *chk;
11340 struct sctp_heartbeat_chunk *hb;
11341 struct timeval now;
11343 SCTP_TCB_LOCK_ASSERT(stcb);
11347 (void)SCTP_GETTIME_TIMEVAL(&now);
11348 switch (net->ro._l_addr.sa.sa_family) {
11360 sctp_alloc_a_chunk(stcb, chk);
11362 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11366 chk->copy_by_ref = 0;
11367 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11368 chk->rec.chunk_id.can_take_data = 1;
11370 chk->asoc = &stcb->asoc;
11371 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11373 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11374 if (chk->data == NULL) {
11375 sctp_free_a_chunk(stcb, chk, so_locked);
11378 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11379 SCTP_BUF_LEN(chk->data) = chk->send_size;
11380 chk->sent = SCTP_DATAGRAM_UNSENT;
11381 chk->snd_count = 0;
11383 atomic_add_int(&chk->whoTo->ref_count, 1);
11384 /* Now we have a mbuf that we can fill in with the details */
11385 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11386 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11387 /* fill out chunk header */
11388 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11389 hb->ch.chunk_flags = 0;
11390 hb->ch.chunk_length = htons(chk->send_size);
11391 /* Fill out hb parameter */
11392 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11393 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11394 hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec;
11395 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11396 /* Did our user request this one, put it in */
11397 hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
11398 hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11399 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11401 * we only take from the entropy pool if the address is not
11404 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11405 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11407 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11408 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11410 switch (net->ro._l_addr.sa.sa_family) {
11413 memcpy(hb->heartbeat.hb_info.address,
11414 &net->ro._l_addr.sin.sin_addr,
11415 sizeof(net->ro._l_addr.sin.sin_addr));
11420 memcpy(hb->heartbeat.hb_info.address,
11421 &net->ro._l_addr.sin6.sin6_addr,
11422 sizeof(net->ro._l_addr.sin6.sin6_addr));
11427 sctp_m_freem(chk->data);
11430 sctp_free_a_chunk(stcb, chk, so_locked);
11434 net->hb_responded = 0;
11435 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11436 stcb->asoc.ctrl_queue_cnt++;
11437 SCTP_STAT_INCR(sctps_sendheartbeat);
11442 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11445 struct sctp_association *asoc;
11446 struct sctp_ecne_chunk *ecne;
11447 struct sctp_tmit_chunk *chk;
11452 asoc = &stcb->asoc;
11453 SCTP_TCB_LOCK_ASSERT(stcb);
11454 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11455 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11456 /* found a previous ECN_ECHO update it if needed */
11457 uint32_t cnt, ctsn;
11459 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11460 ctsn = ntohl(ecne->tsn);
11461 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11462 ecne->tsn = htonl(high_tsn);
11463 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11465 cnt = ntohl(ecne->num_pkts_since_cwr);
11467 ecne->num_pkts_since_cwr = htonl(cnt);
11471 /* nope could not find one to update so we must build one */
11472 sctp_alloc_a_chunk(stcb, chk);
11476 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11477 chk->copy_by_ref = 0;
11478 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11479 chk->rec.chunk_id.can_take_data = 0;
11481 chk->asoc = &stcb->asoc;
11482 chk->send_size = sizeof(struct sctp_ecne_chunk);
11483 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11484 if (chk->data == NULL) {
11485 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11488 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11489 SCTP_BUF_LEN(chk->data) = chk->send_size;
11490 chk->sent = SCTP_DATAGRAM_UNSENT;
11491 chk->snd_count = 0;
11493 atomic_add_int(&chk->whoTo->ref_count, 1);
11495 stcb->asoc.ecn_echo_cnt_onq++;
11496 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11497 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11498 ecne->ch.chunk_flags = 0;
11499 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11500 ecne->tsn = htonl(high_tsn);
11501 ecne->num_pkts_since_cwr = htonl(1);
11502 TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11503 asoc->ctrl_queue_cnt++;
11507 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11508 struct mbuf *m, int len, int iphlen, int bad_crc)
11510 struct sctp_association *asoc;
11511 struct sctp_pktdrop_chunk *drp;
11512 struct sctp_tmit_chunk *chk;
11518 struct sctp_chunkhdr *ch, chunk_buf;
11519 unsigned int chk_length;
11524 asoc = &stcb->asoc;
11525 SCTP_TCB_LOCK_ASSERT(stcb);
11526 if (asoc->pktdrop_supported == 0) {
11528 * peer must declare support before I send one.
11532 if (stcb->sctp_socket == NULL) {
11535 sctp_alloc_a_chunk(stcb, chk);
11539 chk->copy_by_ref = 0;
11540 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11541 chk->rec.chunk_id.can_take_data = 1;
11544 chk->send_size = len;
11545 /* Validate that we do not have an ABORT in here. */
11546 offset = iphlen + sizeof(struct sctphdr);
11547 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11548 sizeof(*ch), (uint8_t *)&chunk_buf);
11549 while (ch != NULL) {
11550 chk_length = ntohs(ch->chunk_length);
11551 if (chk_length < sizeof(*ch)) {
11552 /* break to abort land */
11555 switch (ch->chunk_type) {
11556 case SCTP_PACKET_DROPPED:
11557 case SCTP_ABORT_ASSOCIATION:
11558 case SCTP_INITIATION_ACK:
11560 * We don't respond with an PKT-DROP to an ABORT
11561 * or PKT-DROP. We also do not respond to an
11562 * INIT-ACK, because we can't know if the initiation
11563 * tag is correct or not.
11565 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11570 offset += SCTP_SIZE32(chk_length);
11571 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11572 sizeof(*ch), (uint8_t *)&chunk_buf);
11575 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11576 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11578 * only send 1 mtu worth, trim off the excess on the end.
11581 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11584 chk->asoc = &stcb->asoc;
11585 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11586 if (chk->data == NULL) {
11588 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11591 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11592 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11594 sctp_m_freem(chk->data);
11598 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11599 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11600 chk->book_size_scale = 0;
11602 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11603 drp->trunc_len = htons(fullsz);
11605 * Len is already adjusted to size minus overhead above take
11606 * out the pkt_drop chunk itself from it.
11608 chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
11609 len = chk->send_size;
11611 /* no truncation needed */
11612 drp->ch.chunk_flags = 0;
11613 drp->trunc_len = htons(0);
11616 drp->ch.chunk_flags |= SCTP_BADCRC;
11618 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11619 SCTP_BUF_LEN(chk->data) = chk->send_size;
11620 chk->sent = SCTP_DATAGRAM_UNSENT;
11621 chk->snd_count = 0;
11623 /* we should hit here */
11625 atomic_add_int(&chk->whoTo->ref_count, 1);
11629 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11630 drp->ch.chunk_length = htons(chk->send_size);
11631 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11635 drp->bottle_bw = htonl(spc);
11636 if (asoc->my_rwnd) {
11637 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11638 asoc->size_on_all_streams +
11639 asoc->my_rwnd_control_len +
11640 SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
11643 * If my rwnd is 0, possibly from mbuf depletion as well as
11644 * space used, tell the peer there is NO space aka onq == bw
11646 drp->current_onq = htonl(spc);
11650 m_copydata(m, iphlen, len, (caddr_t)datap);
11651 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11652 asoc->ctrl_queue_cnt++;
11656 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11658 struct sctp_association *asoc;
11659 struct sctp_cwr_chunk *cwr;
11660 struct sctp_tmit_chunk *chk;
11662 SCTP_TCB_LOCK_ASSERT(stcb);
11666 asoc = &stcb->asoc;
11667 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11668 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11670 * found a previous CWR queued to same destination
11671 * update it if needed
11675 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11676 ctsn = ntohl(cwr->tsn);
11677 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11678 cwr->tsn = htonl(high_tsn);
11680 if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11681 /* Make sure override is carried */
11682 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11687 sctp_alloc_a_chunk(stcb, chk);
11691 chk->copy_by_ref = 0;
11692 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11693 chk->rec.chunk_id.can_take_data = 1;
11696 chk->send_size = sizeof(struct sctp_cwr_chunk);
11697 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11698 if (chk->data == NULL) {
11699 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11702 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11703 SCTP_BUF_LEN(chk->data) = chk->send_size;
11704 chk->sent = SCTP_DATAGRAM_UNSENT;
11705 chk->snd_count = 0;
11707 atomic_add_int(&chk->whoTo->ref_count, 1);
11708 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11709 cwr->ch.chunk_type = SCTP_ECN_CWR;
11710 cwr->ch.chunk_flags = override;
11711 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11712 cwr->tsn = htonl(high_tsn);
11713 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11714 asoc->ctrl_queue_cnt++;
11718 sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
11719 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11721 uint16_t len, old_len, i;
11722 struct sctp_stream_reset_out_request *req_out;
11723 struct sctp_chunkhdr *ch;
11725 int number_entries = 0;
11727 ch = mtod(chk->data, struct sctp_chunkhdr *);
11728 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11729 /* get to new offset for the param. */
11730 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11731 /* now how long will this param be? */
11732 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11733 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11734 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11735 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11739 if (number_entries == 0) {
11742 if (number_entries == stcb->asoc.streamoutcnt) {
11743 number_entries = 0;
11745 if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
11746 number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
11748 len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11749 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11750 req_out->ph.param_length = htons(len);
11751 req_out->request_seq = htonl(seq);
11752 req_out->response_seq = htonl(resp_seq);
11753 req_out->send_reset_at_tsn = htonl(last_sent);
11755 if (number_entries) {
11756 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11757 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11758 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11759 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11760 req_out->list_of_streams[at] = htons(i);
11762 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11763 if (at >= number_entries) {
11769 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11770 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11773 if (SCTP_SIZE32(len) > len) {
11775 * Need to worry about the pad we may end up adding to the
11776 * end. This is easy since the struct is either aligned to 4
11777 * bytes or 2 bytes off.
11779 req_out->list_of_streams[number_entries] = 0;
11781 /* now fix the chunk length */
11782 ch->chunk_length = htons(len + old_len);
11783 chk->book_size = len + old_len;
11784 chk->book_size_scale = 0;
11785 chk->send_size = SCTP_SIZE32(chk->book_size);
11786 SCTP_BUF_LEN(chk->data) = chk->send_size;
11791 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11792 int number_entries, uint16_t *list,
11795 uint16_t len, old_len, i;
11796 struct sctp_stream_reset_in_request *req_in;
11797 struct sctp_chunkhdr *ch;
11799 ch = mtod(chk->data, struct sctp_chunkhdr *);
11800 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11802 /* get to new offset for the param. */
11803 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11804 /* now how long will this param be? */
11805 len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11806 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11807 req_in->ph.param_length = htons(len);
11808 req_in->request_seq = htonl(seq);
11809 if (number_entries) {
11810 for (i = 0; i < number_entries; i++) {
11811 req_in->list_of_streams[i] = htons(list[i]);
11814 if (SCTP_SIZE32(len) > len) {
11816 * Need to worry about the pad we may end up adding to the
11817 * end. This is easy since the struct is either aligned to 4
11818 * bytes or 2 bytes off.
11820 req_in->list_of_streams[number_entries] = 0;
11822 /* now fix the chunk length */
11823 ch->chunk_length = htons(len + old_len);
11824 chk->book_size = len + old_len;
11825 chk->book_size_scale = 0;
11826 chk->send_size = SCTP_SIZE32(chk->book_size);
11827 SCTP_BUF_LEN(chk->data) = chk->send_size;
11832 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11835 uint16_t len, old_len;
11836 struct sctp_stream_reset_tsn_request *req_tsn;
11837 struct sctp_chunkhdr *ch;
11839 ch = mtod(chk->data, struct sctp_chunkhdr *);
11840 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11842 /* get to new offset for the param. */
11843 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11844 /* now how long will this param be? */
11845 len = sizeof(struct sctp_stream_reset_tsn_request);
11846 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11847 req_tsn->ph.param_length = htons(len);
11848 req_tsn->request_seq = htonl(seq);
11850 /* now fix the chunk length */
11851 ch->chunk_length = htons(len + old_len);
11852 chk->send_size = len + old_len;
11853 chk->book_size = SCTP_SIZE32(chk->send_size);
11854 chk->book_size_scale = 0;
11855 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11860 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11861 uint32_t resp_seq, uint32_t result)
11863 uint16_t len, old_len;
11864 struct sctp_stream_reset_response *resp;
11865 struct sctp_chunkhdr *ch;
11867 ch = mtod(chk->data, struct sctp_chunkhdr *);
11868 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11870 /* get to new offset for the param. */
11871 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11872 /* now how long will this param be? */
11873 len = sizeof(struct sctp_stream_reset_response);
11874 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11875 resp->ph.param_length = htons(len);
11876 resp->response_seq = htonl(resp_seq);
11877 resp->result = ntohl(result);
11879 /* now fix the chunk length */
11880 ch->chunk_length = htons(len + old_len);
11881 chk->book_size = len + old_len;
11882 chk->book_size_scale = 0;
11883 chk->send_size = SCTP_SIZE32(chk->book_size);
11884 SCTP_BUF_LEN(chk->data) = chk->send_size;
11889 sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
11890 struct sctp_stream_reset_list *ent,
11893 struct sctp_association *asoc;
11894 struct sctp_tmit_chunk *chk;
11895 struct sctp_chunkhdr *ch;
11897 asoc = &stcb->asoc;
11900 * Reset our last reset action to the new one IP -> response
11901 * (PERFORMED probably). This assures that if we fail to send, a
11902 * retran from the peer will get the new response.
11904 asoc->last_reset_action[0] = response;
11905 if (asoc->stream_reset_outstanding) {
11908 sctp_alloc_a_chunk(stcb, chk);
11910 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11913 chk->copy_by_ref = 0;
11914 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11915 chk->rec.chunk_id.can_take_data = 0;
11917 chk->asoc = &stcb->asoc;
11918 chk->book_size = sizeof(struct sctp_chunkhdr);
11919 chk->send_size = SCTP_SIZE32(chk->book_size);
11920 chk->book_size_scale = 0;
11921 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11922 if (chk->data == NULL) {
11923 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11924 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11927 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11928 /* setup chunk parameters */
11929 chk->sent = SCTP_DATAGRAM_UNSENT;
11930 chk->snd_count = 0;
11931 if (stcb->asoc.alternate) {
11932 chk->whoTo = stcb->asoc.alternate;
11934 chk->whoTo = stcb->asoc.primary_destination;
11936 ch = mtod(chk->data, struct sctp_chunkhdr *);
11937 ch->chunk_type = SCTP_STREAM_RESET;
11938 ch->chunk_flags = 0;
11939 ch->chunk_length = htons(chk->book_size);
11940 atomic_add_int(&chk->whoTo->ref_count, 1);
11941 SCTP_BUF_LEN(chk->data) = chk->send_size;
11942 sctp_add_stream_reset_result(chk, ent->seq, response);
11943 /* insert the chunk for sending */
11944 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11947 asoc->ctrl_queue_cnt++;
11951 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11952 uint32_t resp_seq, uint32_t result,
11953 uint32_t send_una, uint32_t recv_next)
11955 uint16_t len, old_len;
11956 struct sctp_stream_reset_response_tsn *resp;
11957 struct sctp_chunkhdr *ch;
11959 ch = mtod(chk->data, struct sctp_chunkhdr *);
11960 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11962 /* get to new offset for the param. */
11963 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11964 /* now how long will this param be? */
11965 len = sizeof(struct sctp_stream_reset_response_tsn);
11966 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11967 resp->ph.param_length = htons(len);
11968 resp->response_seq = htonl(resp_seq);
11969 resp->result = htonl(result);
11970 resp->senders_next_tsn = htonl(send_una);
11971 resp->receivers_next_tsn = htonl(recv_next);
11973 /* now fix the chunk length */
11974 ch->chunk_length = htons(len + old_len);
11975 chk->book_size = len + old_len;
11976 chk->send_size = SCTP_SIZE32(chk->book_size);
11977 chk->book_size_scale = 0;
11978 SCTP_BUF_LEN(chk->data) = chk->send_size;
11983 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11987 uint16_t len, old_len;
11988 struct sctp_chunkhdr *ch;
11989 struct sctp_stream_reset_add_strm *addstr;
11991 ch = mtod(chk->data, struct sctp_chunkhdr *);
11992 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11994 /* get to new offset for the param. */
11995 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11996 /* now how long will this param be? */
11997 len = sizeof(struct sctp_stream_reset_add_strm);
12000 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
12001 addstr->ph.param_length = htons(len);
12002 addstr->request_seq = htonl(seq);
12003 addstr->number_of_streams = htons(adding);
12004 addstr->reserved = 0;
12006 /* now fix the chunk length */
12007 ch->chunk_length = htons(len + old_len);
12008 chk->send_size = len + old_len;
12009 chk->book_size = SCTP_SIZE32(chk->send_size);
12010 chk->book_size_scale = 0;
12011 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12016 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
12020 uint16_t len, old_len;
12021 struct sctp_chunkhdr *ch;
12022 struct sctp_stream_reset_add_strm *addstr;
12024 ch = mtod(chk->data, struct sctp_chunkhdr *);
12025 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12027 /* get to new offset for the param. */
12028 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
12029 /* now how long will this param be? */
12030 len = sizeof(struct sctp_stream_reset_add_strm);
12032 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
12033 addstr->ph.param_length = htons(len);
12034 addstr->request_seq = htonl(seq);
12035 addstr->number_of_streams = htons(adding);
12036 addstr->reserved = 0;
12038 /* now fix the chunk length */
12039 ch->chunk_length = htons(len + old_len);
12040 chk->send_size = len + old_len;
12041 chk->book_size = SCTP_SIZE32(chk->send_size);
12042 chk->book_size_scale = 0;
12043 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12048 sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
12050 struct sctp_association *asoc;
12051 struct sctp_tmit_chunk *chk;
12052 struct sctp_chunkhdr *ch;
12055 asoc = &stcb->asoc;
12056 asoc->trigger_reset = 0;
12057 if (asoc->stream_reset_outstanding) {
12060 sctp_alloc_a_chunk(stcb, chk);
12062 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12065 chk->copy_by_ref = 0;
12066 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12067 chk->rec.chunk_id.can_take_data = 0;
12069 chk->asoc = &stcb->asoc;
12070 chk->book_size = sizeof(struct sctp_chunkhdr);
12071 chk->send_size = SCTP_SIZE32(chk->book_size);
12072 chk->book_size_scale = 0;
12073 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12074 if (chk->data == NULL) {
12075 sctp_free_a_chunk(stcb, chk, so_locked);
12076 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12079 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12081 /* setup chunk parameters */
12082 chk->sent = SCTP_DATAGRAM_UNSENT;
12083 chk->snd_count = 0;
12084 if (stcb->asoc.alternate) {
12085 chk->whoTo = stcb->asoc.alternate;
12087 chk->whoTo = stcb->asoc.primary_destination;
12089 ch = mtod(chk->data, struct sctp_chunkhdr *);
12090 ch->chunk_type = SCTP_STREAM_RESET;
12091 ch->chunk_flags = 0;
12092 ch->chunk_length = htons(chk->book_size);
12093 atomic_add_int(&chk->whoTo->ref_count, 1);
12094 SCTP_BUF_LEN(chk->data) = chk->send_size;
12095 seq = stcb->asoc.str_reset_seq_out;
12096 if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
12098 asoc->stream_reset_outstanding++;
12100 m_freem(chk->data);
12102 sctp_free_a_chunk(stcb, chk, so_locked);
12105 asoc->str_reset = chk;
12106 /* insert the chunk for sending */
12107 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12110 asoc->ctrl_queue_cnt++;
12112 if (stcb->asoc.send_sack) {
12113 sctp_send_sack(stcb, so_locked);
12115 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12120 sctp_send_str_reset_req(struct sctp_tcb *stcb,
12121 uint16_t number_entries, uint16_t *list,
12122 uint8_t send_in_req,
12123 uint8_t send_tsn_req,
12124 uint8_t add_stream,
12126 uint16_t adding_i, uint8_t peer_asked)
12128 struct sctp_association *asoc;
12129 struct sctp_tmit_chunk *chk;
12130 struct sctp_chunkhdr *ch;
12131 int can_send_out_req = 0;
12134 SCTP_TCB_LOCK_ASSERT(stcb);
12136 asoc = &stcb->asoc;
12137 if (asoc->stream_reset_outstanding) {
12139 * Already one pending, must get ACK back to clear the flag.
12141 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12144 if ((send_in_req == 0) && (send_tsn_req == 0) &&
12145 (add_stream == 0)) {
12146 /* nothing to do */
12147 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12150 if (send_tsn_req && send_in_req) {
12151 /* error, can't do that */
12152 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12154 } else if (send_in_req) {
12155 can_send_out_req = 1;
12157 if (number_entries > (MCLBYTES -
12158 SCTP_MIN_OVERHEAD -
12159 sizeof(struct sctp_chunkhdr) -
12160 sizeof(struct sctp_stream_reset_out_request)) /
12161 sizeof(uint16_t)) {
12162 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12165 sctp_alloc_a_chunk(stcb, chk);
12167 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12170 chk->copy_by_ref = 0;
12171 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12172 chk->rec.chunk_id.can_take_data = 0;
12174 chk->asoc = &stcb->asoc;
12175 chk->book_size = sizeof(struct sctp_chunkhdr);
12176 chk->send_size = SCTP_SIZE32(chk->book_size);
12177 chk->book_size_scale = 0;
12178 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12179 if (chk->data == NULL) {
12180 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12181 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12184 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12186 /* setup chunk parameters */
12187 chk->sent = SCTP_DATAGRAM_UNSENT;
12188 chk->snd_count = 0;
12189 if (stcb->asoc.alternate) {
12190 chk->whoTo = stcb->asoc.alternate;
12192 chk->whoTo = stcb->asoc.primary_destination;
12194 atomic_add_int(&chk->whoTo->ref_count, 1);
12195 ch = mtod(chk->data, struct sctp_chunkhdr *);
12196 ch->chunk_type = SCTP_STREAM_RESET;
12197 ch->chunk_flags = 0;
12198 ch->chunk_length = htons(chk->book_size);
12199 SCTP_BUF_LEN(chk->data) = chk->send_size;
12201 seq = stcb->asoc.str_reset_seq_out;
12202 if (can_send_out_req) {
12205 ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
12208 asoc->stream_reset_outstanding++;
12211 if ((add_stream & 1) &&
12212 ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
12213 /* Need to allocate more */
12214 struct sctp_stream_out *oldstream;
12215 struct sctp_stream_queue_pending *sp, *nsp;
12217 #if defined(SCTP_DETAILED_STR_STATS)
12221 oldstream = stcb->asoc.strmout;
12222 /* get some more */
12223 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
12224 (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
12226 if (stcb->asoc.strmout == NULL) {
12229 stcb->asoc.strmout = oldstream;
12230 /* Turn off the bit */
12231 x = add_stream & 0xfe;
12236 * Ok now we proceed with copying the old out stuff and
12237 * initializing the new stuff.
12239 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
12240 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12241 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12242 /* FIX ME FIX ME */
12244 * This should be a SS_COPY operation FIX ME STREAM
12247 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
12248 stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
12249 #if defined(SCTP_DETAILED_STR_STATS)
12250 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12251 stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
12252 stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
12255 stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
12256 stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
12258 stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
12259 stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
12260 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
12261 stcb->asoc.strmout[i].sid = i;
12262 stcb->asoc.strmout[i].state = oldstream[i].state;
12263 /* now anything on those queues? */
12264 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
12265 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
12266 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
12269 /* now the new streams */
12270 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
12271 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
12272 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12273 stcb->asoc.strmout[i].chunks_on_queues = 0;
12274 #if defined(SCTP_DETAILED_STR_STATS)
12275 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12276 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
12277 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
12280 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
12281 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
12283 stcb->asoc.strmout[i].next_mid_ordered = 0;
12284 stcb->asoc.strmout[i].next_mid_unordered = 0;
12285 stcb->asoc.strmout[i].sid = i;
12286 stcb->asoc.strmout[i].last_msg_incomplete = 0;
12287 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
12288 stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
12290 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
12291 SCTP_FREE(oldstream, SCTP_M_STRMO);
12294 if ((add_stream & 1) && (adding_o > 0)) {
12295 asoc->strm_pending_add_size = adding_o;
12296 asoc->peer_req_out = peer_asked;
12297 sctp_add_an_out_stream(chk, seq, adding_o);
12299 asoc->stream_reset_outstanding++;
12301 if ((add_stream & 2) && (adding_i > 0)) {
12302 sctp_add_an_in_stream(chk, seq, adding_i);
12304 asoc->stream_reset_outstanding++;
12307 sctp_add_stream_reset_in(chk, number_entries, list, seq);
12309 asoc->stream_reset_outstanding++;
12311 if (send_tsn_req) {
12312 sctp_add_stream_reset_tsn(chk, seq);
12313 asoc->stream_reset_outstanding++;
12315 asoc->str_reset = chk;
12316 /* insert the chunk for sending */
12317 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12320 asoc->ctrl_queue_cnt++;
12321 if (stcb->asoc.send_sack) {
12322 sctp_send_sack(stcb, SCTP_SO_LOCKED);
12324 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12329 sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
12330 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12331 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12332 uint32_t vrf_id, uint16_t port)
12334 /* Don't respond to an ABORT with an ABORT. */
12335 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
12337 sctp_m_freem(cause);
12340 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
12341 mflowtype, mflowid, fibnum,
12347 sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
12348 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12349 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12350 uint32_t vrf_id, uint16_t port)
12352 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
12353 mflowtype, mflowid, fibnum,
12358 static struct mbuf *
12359 sctp_copy_resume(struct uio *uio,
12361 int user_marks_eor,
12364 struct mbuf **new_tail)
12368 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12369 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12371 /* The only possible error is EFAULT. */
12372 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12375 *sndout = m_length(m, NULL);
12376 *new_tail = m_last(m);
12382 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12386 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
12387 if (sp->data == NULL) {
12388 /* The only possible error is EFAULT. */
12389 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12392 sp->tail_mbuf = m_last(sp->data);
12396 static struct sctp_stream_queue_pending *
12397 sctp_copy_it_in(struct sctp_tcb *stcb,
12398 struct sctp_association *asoc,
12399 struct sctp_nonpad_sndrcvinfo *srcv,
12401 struct sctp_nets *net,
12402 ssize_t max_send_len,
12403 int user_marks_eor,
12408 * This routine must be very careful in its work. Protocol
12409 * processing is up and running so care must be taken to spl...()
12410 * when you need to do something that may effect the stcb/asoc. The
12411 * sb is locked however. When data is copied the protocol processing
12412 * should be enabled since this is a slower operation...
12414 struct sctp_stream_queue_pending *sp;
12418 sctp_alloc_a_strmoq(stcb, sp);
12420 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12425 sp->sender_all_done = 0;
12426 sp->sinfo_flags = srcv->sinfo_flags;
12427 sp->timetolive = srcv->sinfo_timetolive;
12428 sp->ppid = srcv->sinfo_ppid;
12429 sp->context = srcv->sinfo_context;
12431 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12432 sp->sid = srcv->sinfo_stream;
12433 sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
12434 if ((sp->length == (uint32_t)uio->uio_resid) &&
12435 ((user_marks_eor == 0) ||
12436 (srcv->sinfo_flags & SCTP_EOF) ||
12437 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12438 sp->msg_is_complete = 1;
12440 sp->msg_is_complete = 0;
12442 sp->sender_all_done = 0;
12443 sp->some_taken = 0;
12444 sp->put_last_out = 0;
12445 resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
12446 sp->data = sp->tail_mbuf = NULL;
12447 if (sp->length == 0) {
12450 if (srcv->sinfo_keynumber_valid) {
12451 sp->auth_keyid = srcv->sinfo_keynumber;
12453 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12455 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12456 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12457 sp->holds_key_ref = 1;
12459 *error = sctp_copy_one(sp, uio, resv_in_first);
12462 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12465 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12467 atomic_add_int(&sp->net->ref_count, 1);
12471 sctp_set_prsctp_policy(sp);
12478 sctp_sosend(struct socket *so,
12479 struct sockaddr *addr,
12482 struct mbuf *control,
12486 struct sctp_sndrcvinfo sndrcvninfo;
12487 #if defined(INET) && defined(INET6)
12488 struct sockaddr_in sin;
12490 struct sockaddr *addr_to_use;
12494 if (control != NULL) {
12495 /* process cmsg snd/rcv info (maybe a assoc-id) */
12496 use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
12498 use_sndinfo = false;
12500 #if defined(INET) && defined(INET6)
12501 if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
12502 struct sockaddr_in6 *sin6;
12504 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12505 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12508 sin6 = (struct sockaddr_in6 *)addr;
12509 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12510 in6_sin6_2_sin(&sin, sin6);
12511 addr_to_use = (struct sockaddr *)&sin;
12513 addr_to_use = addr;
12516 addr_to_use = addr;
12519 addr_to_use = addr;
12521 error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
12522 use_sndinfo ? &sndrcvninfo : NULL, p);
12527 sctp_lower_sosend(struct socket *so,
12528 struct sockaddr *addr,
12531 struct mbuf *control,
12533 struct sctp_sndrcvinfo *srcv,
12536 struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
12537 struct epoch_tracker et;
12538 struct timeval now;
12539 struct sctp_block_entry be;
12540 struct sctp_inpcb *inp;
12541 struct sctp_tcb *stcb = NULL;
12542 struct sctp_nets *net;
12543 struct sctp_association *asoc;
12544 struct sctp_inpcb *t_inp;
12545 struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
12546 ssize_t sndlen = 0, max_len, local_add_more;
12547 ssize_t local_soresv = 0;
12548 sctp_assoc_t sinfo_assoc_id;
12549 int user_marks_eor;
12550 int nagle_applies = 0;
12552 int queue_only = 0, queue_only_for_init = 0;
12554 int now_filled = 0;
12555 unsigned int inqueue_bytes = 0;
12557 uint16_t sinfo_flags;
12558 uint16_t sinfo_stream;
12559 bool create_lock_applied = false;
12560 bool free_cnt_applied = false;
12561 bool some_on_control;
12562 bool got_all_of_the_send = false;
12563 bool non_blocking = false;
12569 if ((uio == NULL) && (top == NULL)) {
12573 if (addr != NULL) {
12574 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12576 switch (raddr->sa.sa_family) {
12579 if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12583 port = raddr->sin.sin_port;
12588 if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12592 port = raddr->sin6.sin6_port;
12596 error = EAFNOSUPPORT;
12603 if (uio->uio_resid < 0) {
12607 sndlen = uio->uio_resid;
12609 sndlen = SCTP_HEADER_LEN(top);
12611 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
12612 (void *)addr, sndlen);
12614 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12619 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12620 if ((uio == NULL) && (user_marks_eor != 0)) {
12622 * We do not support eeor mode for
12623 * sending with mbuf chains (like sendfile).
12628 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12629 SCTP_IS_LISTENING(inp)) {
12630 /* The listener can NOT send. */
12634 atomic_add_int(&inp->total_sends, 1);
12636 if (srcv != NULL) {
12637 sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
12638 sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
12639 sinfo_flags = sndrcvninfo->sinfo_flags;
12640 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12641 PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12645 if (sinfo_flags != 0) {
12646 SCTP_STAT_INCR(sctps_sends_with_flags);
12649 sndrcvninfo = NULL;
12650 sinfo_flags = inp->def_send.sinfo_flags;
12651 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12653 if (flags & MSG_EOR) {
12654 sinfo_flags |= SCTP_EOR;
12656 if (flags & MSG_EOF) {
12657 sinfo_flags |= SCTP_EOF;
12659 if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12663 SCTP_INP_RLOCK(inp);
12664 if ((sinfo_flags & SCTP_SENDALL) &&
12665 (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
12666 SCTP_INP_RUNLOCK(inp);
12667 error = sctp_sendall(inp, uio, top, sndrcvninfo);
12671 /* Now we must find the association. */
12672 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12673 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12674 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12675 if (stcb != NULL) {
12676 SCTP_TCB_LOCK(stcb);
12678 SCTP_INP_RUNLOCK(inp);
12679 } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
12680 stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
12681 SCTP_INP_RUNLOCK(inp);
12682 if (stcb != NULL) {
12683 SCTP_TCB_LOCK_ASSERT(stcb);
12685 } else if (addr != NULL) {
12687 * Since we did not use findep we must
12688 * increment it, and if we don't find a tcb
12691 SCTP_INP_INCR_REF(inp);
12692 SCTP_INP_RUNLOCK(inp);
12693 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12694 if (stcb == NULL) {
12695 SCTP_INP_WLOCK(inp);
12696 SCTP_INP_DECR_REF(inp);
12697 SCTP_INP_WUNLOCK(inp);
12699 SCTP_TCB_LOCK_ASSERT(stcb);
12702 SCTP_INP_RUNLOCK(inp);
12706 if (stcb != NULL) {
12707 SCTP_TCB_LOCK_ASSERT(stcb);
12711 if ((stcb == NULL) && (addr != NULL)) {
12712 /* Possible implicit send? */
12713 SCTP_ASOC_CREATE_LOCK(inp);
12714 create_lock_applied = true;
12715 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12716 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12720 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12721 (addr->sa_family == AF_INET6)) {
12725 SCTP_INP_WLOCK(inp);
12726 SCTP_INP_INCR_REF(inp);
12727 SCTP_INP_WUNLOCK(inp);
12728 /* With the lock applied look again */
12729 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12730 #if defined(INET) || defined(INET6)
12731 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12732 stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12735 if (stcb == NULL) {
12736 SCTP_INP_WLOCK(inp);
12737 SCTP_INP_DECR_REF(inp);
12738 SCTP_INP_WUNLOCK(inp);
12740 SCTP_TCB_LOCK_ASSERT(stcb);
12741 SCTP_ASOC_CREATE_UNLOCK(inp);
12742 create_lock_applied = false;
12747 if (t_inp != inp) {
12752 if (stcb == NULL) {
12753 if (addr == NULL) {
12757 /* We must go ahead and start the INIT process */
12760 if ((sinfo_flags & SCTP_ABORT) ||
12761 ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12763 * User asks to abort a non-existent assoc,
12764 * or EOF a non-existent assoc with no data
12769 /* get an asoc/stcb struct */
12770 vrf_id = inp->def_vrf_id;
12771 KASSERT(create_lock_applied, ("create_lock_applied is false"));
12772 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
12773 inp->sctp_ep.pre_open_stream_count,
12776 SCTP_INITIALIZE_AUTH_PARAMS);
12777 if (stcb == NULL) {
12778 /* error is setup for us in the call. */
12779 KASSERT(error != 0, ("error is 0 although stcb is NULL"));
12782 SCTP_TCB_LOCK_ASSERT(stcb);
12783 SCTP_ASOC_CREATE_UNLOCK(inp);
12784 create_lock_applied = false;
12786 * Turn on queue only flag to prevent data from
12790 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
12791 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
12792 if (control != NULL) {
12793 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12794 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
12795 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12797 KASSERT(error != 0,
12798 ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
12802 /* out with the INIT */
12803 queue_only_for_init = 1;
12805 * we may want to dig in after this call and adjust the MTU
12806 * value. It defaulted to 1500 (constant) but the ro
12807 * structure may now have an update and thus we may need to
12808 * change it BEFORE we append the message.
12813 KASSERT(!create_lock_applied, ("create_lock_applied is true"));
12814 KASSERT(stcb != NULL, ("stcb is NULL"));
12815 SCTP_TCB_LOCK_ASSERT(stcb);
12817 asoc = &stcb->asoc;
12818 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12819 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12820 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12821 /* XXX: Could also be ECONNABORTED, not enough info. */
12822 error = ECONNRESET;
12828 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12829 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12832 /* Keep the stcb from being freed under our feet. */
12833 atomic_add_int(&asoc->refcnt, 1);
12834 free_cnt_applied = true;
12835 if (sndrcvninfo == NULL) {
12836 /* Use a local copy to have a consistent view. */
12837 sndrcvninfo_buf = asoc->def_send;
12838 sndrcvninfo = &sndrcvninfo_buf;
12839 sinfo_flags = sndrcvninfo->sinfo_flags;
12840 if (flags & MSG_EOR) {
12841 sinfo_flags |= SCTP_EOR;
12843 if (flags & MSG_EOF) {
12844 sinfo_flags |= SCTP_EOF;
12847 /* Are we aborting? */
12848 if (sinfo_flags & SCTP_ABORT) {
12850 struct sctp_paramhdr *ph;
12851 ssize_t tot_demand, tot_out = 0, max_out;
12853 SCTP_STAT_INCR(sctps_sends_with_abort);
12854 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12855 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12856 /* It has to be up before we abort. */
12860 /* How big is the user initiated abort? */
12865 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12866 tot_out += SCTP_BUF_LEN(cntm);
12869 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
12871 /* Must fit in a MTU */
12873 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12874 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12878 mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
12884 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12885 max_out -= sizeof(struct sctp_abort_msg);
12886 if (tot_out > max_out) {
12889 ph = mtod(mm, struct sctp_paramhdr *);
12890 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12891 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
12893 SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
12895 SCTP_TCB_UNLOCK(stcb);
12896 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12897 SCTP_TCB_LOCK(stcb);
12898 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12899 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12901 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12903 * XXX: Could also be ECONNABORTED,
12906 error = ECONNRESET;
12914 * Here if we can't get his data we
12915 * still abort we just don't get to
12916 * send the users note :-0
12924 SCTP_BUF_NEXT(mm) = top;
12927 atomic_subtract_int(&asoc->refcnt, 1);
12928 free_cnt_applied = false;
12929 /* release this lock, otherwise we hang on ourselves */
12930 NET_EPOCH_ENTER(et);
12931 sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
12932 NET_EPOCH_EXIT(et);
12935 * In this case top is already chained to mm avoid double
12936 * free, since we free it below if top != NULL and driver
12937 * would free it after sending the packet out
12945 KASSERT(stcb != NULL, ("stcb is NULL"));
12946 SCTP_TCB_LOCK_ASSERT(stcb);
12947 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12948 ("Association about to be freed"));
12949 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12950 ("Association was aborted"));
12952 if (sinfo_flags & SCTP_ADDR_OVER) {
12953 if (addr != NULL) {
12954 net = sctp_findnet(stcb, addr);
12958 if ((net == NULL) ||
12959 ((port != 0) && (port != stcb->rport))) {
12964 if (asoc->alternate != NULL) {
12965 net = asoc->alternate;
12967 net = asoc->primary_destination;
12971 if (sinfo_flags & SCTP_EOF) {
12972 got_all_of_the_send = true;
12979 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12980 if (sndlen > (ssize_t)asoc->smallest_mtu) {
12985 sinfo_stream = sndrcvninfo->sinfo_stream;
12986 /* Is the stream no. valid? */
12987 if (sinfo_stream >= asoc->streamoutcnt) {
12988 /* Invalid stream number */
12992 if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
12993 (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
12995 * Can't queue any data while stream reset is underway.
12997 if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
13004 atomic_add_int(&stcb->total_sends, 1);
13005 if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
13006 non_blocking = true;
13008 if (non_blocking) {
13011 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13012 if (user_marks_eor == 0) {
13017 if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
13018 (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13019 if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
13020 (user_marks_eor == 0)) {
13023 error = EWOULDBLOCK;
13028 atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
13029 local_soresv = sndlen;
13031 KASSERT(stcb != NULL, ("stcb is NULL"));
13032 SCTP_TCB_LOCK_ASSERT(stcb);
13033 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13034 ("Association about to be freed"));
13035 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13036 ("Association was aborted"));
13038 /* Ok, we will attempt a msgsnd :> */
13040 p->td_ru.ru_msgsnd++;
13042 /* Calculate the maximum we can send */
13043 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13044 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13045 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13049 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13050 if ((user_marks_eor == 0) &&
13051 (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13052 /* It will NEVER fit. */
13056 if (user_marks_eor != 0) {
13057 local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13060 * For non-eeor the whole message must fit in
13061 * the socket send buffer.
13063 local_add_more = sndlen;
13065 if (non_blocking) {
13066 goto skip_preblock;
13068 if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13070 ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13071 /* No room right now! */
13072 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13073 SOCKBUF_LOCK(&so->so_snd);
13074 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13075 ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13076 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
13077 (unsigned int)SCTP_SB_LIMIT_SND(so),
13080 asoc->stream_queue_cnt,
13081 asoc->chunks_on_out_queue,
13082 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13083 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13084 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13087 stcb->block_entry = &be;
13088 SCTP_TCB_UNLOCK(stcb);
13089 error = sbwait(so, SO_SND);
13091 if (so->so_error != 0) {
13092 error = so->so_error;
13094 if (be.error != 0) {
13098 SOCKBUF_UNLOCK(&so->so_snd);
13099 SCTP_TCB_LOCK(stcb);
13100 stcb->block_entry = NULL;
13104 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13105 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13106 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13108 * XXX: Could also be ECONNABORTED,
13111 error = ECONNRESET;
13117 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13118 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13119 asoc, asoc->total_output_queue_size);
13121 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13122 SOCKBUF_LOCK(&so->so_snd);
13124 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13125 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13129 SOCKBUF_UNLOCK(&so->so_snd);
13133 KASSERT(stcb != NULL, ("stcb is NULL"));
13134 SCTP_TCB_LOCK_ASSERT(stcb);
13135 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13136 ("Association about to be freed"));
13137 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13138 ("Association was aborted"));
13141 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13142 * case NOTE: uio will be null when top/mbuf is passed
13145 struct sctp_stream_queue_pending *sp;
13146 struct sctp_stream_out *strm;
13149 if ((asoc->stream_locked) &&
13150 (asoc->stream_locked_on != sinfo_stream)) {
13154 strm = &asoc->strmout[sinfo_stream];
13155 if (strm->last_msg_incomplete == 0) {
13157 SCTP_TCB_UNLOCK(stcb);
13158 sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
13159 SCTP_TCB_LOCK(stcb);
13160 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13161 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13162 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13164 * XXX: Could also be ECONNABORTED,
13167 error = ECONNRESET;
13177 * Reject the sending of a new user message, if the
13178 * association is about to be shut down.
13180 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
13181 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13182 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13183 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13184 if (sp->data != 0) {
13185 sctp_m_freem(sp->data);
13187 sp->tail_mbuf = NULL;
13190 if (sp->net != NULL) {
13191 sctp_free_remote_addr(sp->net);
13194 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13198 /* The out streams might be reallocated. */
13199 strm = &asoc->strmout[sinfo_stream];
13200 if (sp->msg_is_complete) {
13201 strm->last_msg_incomplete = 0;
13202 asoc->stream_locked = 0;
13205 * Just got locked to this guy in case of an
13208 strm->last_msg_incomplete = 1;
13209 if (asoc->idata_supported == 0) {
13210 asoc->stream_locked = 1;
13211 asoc->stream_locked_on = sinfo_stream;
13213 sp->sender_all_done = 0;
13215 sctp_snd_sb_alloc(stcb, sp->length);
13216 atomic_add_int(&asoc->stream_queue_cnt, 1);
13217 if (sinfo_flags & SCTP_UNORDERED) {
13218 SCTP_STAT_INCR(sctps_sends_with_unord);
13220 sp->processing = 1;
13221 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13222 asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
13224 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13226 /* ???? Huh ??? last msg is gone */
13228 panic("Warning: Last msg marked incomplete, yet nothing left?");
13230 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13231 strm->last_msg_incomplete = 0;
13235 if (sp->processing != 0) {
13239 sp->processing = 1;
13243 KASSERT(stcb != NULL, ("stcb is NULL"));
13244 SCTP_TCB_LOCK_ASSERT(stcb);
13245 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13246 ("Association about to be freed"));
13247 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13248 ("Association was aborted"));
13250 while (uio->uio_resid > 0) {
13251 /* How much room do we have? */
13252 struct mbuf *new_tail, *mm;
13254 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13255 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13256 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13260 if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13261 ((max_len > 0) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13262 (uio->uio_resid <= max_len)) {
13263 SCTP_TCB_UNLOCK(stcb);
13266 mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
13267 SCTP_TCB_LOCK(stcb);
13268 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13269 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13271 * We need to get out. Peer probably
13275 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13277 * XXX: Could also be
13278 * ECONNABORTED, not enough
13281 error = ECONNRESET;
13287 if ((mm == NULL) || (error != 0)) {
13292 sp->processing = 0;
13296 /* Update the mbuf and count */
13297 if (sp->tail_mbuf != NULL) {
13298 /* Tack it to the end. */
13299 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13301 /* A stolen mbuf. */
13304 sp->tail_mbuf = new_tail;
13305 sctp_snd_sb_alloc(stcb, sndout);
13306 atomic_add_int(&sp->length, sndout);
13307 if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
13308 sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
13311 /* Did we reach EOR? */
13312 if ((uio->uio_resid == 0) &&
13313 ((user_marks_eor == 0) ||
13314 (sinfo_flags & SCTP_EOF) ||
13315 (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
13316 sp->msg_is_complete = 1;
13318 sp->msg_is_complete = 0;
13322 KASSERT(stcb != NULL, ("stcb is NULL"));
13323 SCTP_TCB_LOCK_ASSERT(stcb);
13324 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13325 ("Association about to be freed"));
13326 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13327 ("Association was aborted"));
13329 if (uio->uio_resid == 0) {
13334 if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
13336 * This is ugly but we must assure locking
13339 sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
13340 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13341 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
13342 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13349 /* wait for space now */
13350 if (non_blocking) {
13351 /* Non-blocking io in place out */
13353 sp->processing = 0;
13357 /* What about the INIT, send it maybe */
13358 if (queue_only_for_init) {
13359 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13360 /* a collision took us forward? */
13363 NET_EPOCH_ENTER(et);
13364 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13365 NET_EPOCH_EXIT(et);
13366 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13370 if ((net->flight_size > net->cwnd) &&
13371 (asoc->sctp_cmt_on_off == 0)) {
13372 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13374 } else if (asoc->ifp_had_enobuf) {
13375 SCTP_STAT_INCR(sctps_ifnomemqueued);
13376 if (net->flight_size > (2 * net->mtu)) {
13379 asoc->ifp_had_enobuf = 0;
13381 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13382 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13383 (asoc->total_flight > 0) &&
13384 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13385 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13387 * Ok, Nagle is set on and we have data outstanding.
13388 * Don't send anything and let SACKs drive out the
13389 * data unless we have a "full" segment to send.
13391 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13392 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13394 SCTP_STAT_INCR(sctps_naglequeued);
13397 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13398 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13399 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13401 SCTP_STAT_INCR(sctps_naglesent);
13404 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13405 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13406 nagle_applies, un_sent);
13407 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13408 asoc->total_flight,
13409 asoc->chunks_on_out_queue, asoc->total_flight_count);
13411 if (queue_only_for_init) {
13412 queue_only_for_init = 0;
13414 if ((queue_only == 0) && (nagle_applies == 0)) {
13416 * need to start chunk output
13417 * before blocking.. note that if
13418 * a lock is already applied, then
13419 * the input via the net is happening
13420 * and I don't need to start output :-D
13422 NET_EPOCH_ENTER(et);
13423 sctp_chunk_output(inp, stcb,
13424 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13425 NET_EPOCH_EXIT(et);
13428 * This is a bit strange, but I think it will
13429 * work. The total_output_queue_size is locked and
13430 * protected by the TCB_LOCK, which we just released.
13431 * There is a race that can occur between releasing it
13432 * above, and me getting the socket lock, where sacks
13433 * come in but we have not put the SB_WAIT on the
13434 * so_snd buffer to get the wakeup. After the LOCK
13435 * is applied the sack_processing will also need to
13436 * LOCK the so->so_snd to do the actual sowwakeup(). So
13437 * once we have the socket buffer lock if we recheck the
13438 * size we KNOW we will get to sleep safely with the
13439 * wakeup flag in place.
13441 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13442 SOCKBUF_LOCK(&so->so_snd);
13443 if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
13444 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13445 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13446 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13447 asoc, uio->uio_resid);
13450 stcb->block_entry = &be;
13451 SCTP_TCB_UNLOCK(stcb);
13452 error = sbwait(so, SO_SND);
13454 if (so->so_error != 0)
13455 error = so->so_error;
13456 if (be.error != 0) {
13460 SOCKBUF_UNLOCK(&so->so_snd);
13461 SCTP_TCB_LOCK(stcb);
13462 stcb->block_entry = NULL;
13463 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13464 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13465 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13467 * XXX: Could also be
13468 * ECONNABORTED, not enough
13471 error = ECONNRESET;
13479 sp->processing = 0;
13484 SOCKBUF_UNLOCK(&so->so_snd);
13486 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13487 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13488 asoc, asoc->total_output_queue_size);
13492 KASSERT(stcb != NULL, ("stcb is NULL"));
13493 SCTP_TCB_LOCK_ASSERT(stcb);
13494 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13495 ("Association about to be freed"));
13496 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13497 ("Association was aborted"));
13499 /* The out streams might be reallocated. */
13500 strm = &asoc->strmout[sinfo_stream];
13502 if (sp->msg_is_complete == 0) {
13503 strm->last_msg_incomplete = 1;
13504 if (asoc->idata_supported == 0) {
13505 asoc->stream_locked = 1;
13506 asoc->stream_locked_on = sinfo_stream;
13509 sp->sender_all_done = 1;
13510 strm->last_msg_incomplete = 0;
13511 asoc->stream_locked = 0;
13513 sp->processing = 0;
13515 SCTP_PRINTF("Huh no sp TSNH?\n");
13516 strm->last_msg_incomplete = 0;
13517 asoc->stream_locked = 0;
13519 if (uio->uio_resid == 0) {
13520 got_all_of_the_send = true;
13523 error = sctp_msg_append(stcb, net, top, sndrcvninfo);
13525 if ((sinfo_flags & SCTP_EOF) != 0) {
13526 got_all_of_the_send = true;
13534 KASSERT(stcb != NULL, ("stcb is NULL"));
13535 SCTP_TCB_LOCK_ASSERT(stcb);
13536 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13537 ("Association about to be freed"));
13538 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13539 ("Association was aborted"));
13542 if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
13543 SCTP_STAT_INCR(sctps_sends_with_eof);
13545 if (TAILQ_EMPTY(&asoc->send_queue) &&
13546 TAILQ_EMPTY(&asoc->sent_queue) &&
13547 sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
13548 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13551 /* there is nothing queued to send, so I'm done... */
13552 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13553 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13554 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13555 struct sctp_nets *netp;
13557 /* only send SHUTDOWN the first time through */
13558 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13559 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13561 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
13562 sctp_stop_timers_for_shutdown(stcb);
13563 if (asoc->alternate != NULL) {
13564 netp = asoc->alternate;
13566 netp = asoc->primary_destination;
13568 sctp_send_shutdown(stcb, netp);
13569 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13571 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13576 * we still got (or just got) data to send, so set
13580 * XXX sockets draft says that SCTP_EOF should be
13581 * sent with no data. currently, we will allow user
13582 * data to be sent first and move to
13585 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13586 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13587 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13588 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13589 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
13591 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
13592 if (TAILQ_EMPTY(&asoc->send_queue) &&
13593 TAILQ_EMPTY(&asoc->sent_queue) &&
13594 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13595 struct mbuf *op_err;
13596 char msg[SCTP_DIAG_INFO_LEN];
13599 if (free_cnt_applied) {
13600 atomic_subtract_int(&asoc->refcnt, 1);
13601 free_cnt_applied = false;
13603 SCTP_SNPRINTF(msg, sizeof(msg),
13604 "%s:%d at %s", __FILE__, __LINE__, __func__);
13605 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
13607 NET_EPOCH_ENTER(et);
13608 sctp_abort_an_association(stcb->sctp_ep, stcb,
13609 op_err, false, SCTP_SO_LOCKED);
13610 NET_EPOCH_EXIT(et);
13612 error = ECONNABORTED;
13615 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13621 KASSERT(stcb != NULL, ("stcb is NULL"));
13622 SCTP_TCB_LOCK_ASSERT(stcb);
13623 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13624 ("Association about to be freed"));
13625 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13626 ("Association was aborted"));
13628 some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
13629 if (queue_only_for_init) {
13630 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13631 /* a collision took us forward? */
13634 NET_EPOCH_ENTER(et);
13635 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13636 NET_EPOCH_EXIT(et);
13637 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13642 KASSERT(stcb != NULL, ("stcb is NULL"));
13643 SCTP_TCB_LOCK_ASSERT(stcb);
13644 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13645 ("Association about to be freed"));
13646 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13647 ("Association was aborted"));
13649 if ((net->flight_size > net->cwnd) &&
13650 (asoc->sctp_cmt_on_off == 0)) {
13651 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13653 } else if (asoc->ifp_had_enobuf) {
13654 SCTP_STAT_INCR(sctps_ifnomemqueued);
13655 if (net->flight_size > (2 * net->mtu)) {
13658 asoc->ifp_had_enobuf = 0;
13660 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13661 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13662 (asoc->total_flight > 0) &&
13663 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13664 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13666 * Ok, Nagle is set on and we have data outstanding.
13667 * Don't send anything and let SACKs drive out the
13668 * data unless wen have a "full" segment to send.
13670 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13671 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13673 SCTP_STAT_INCR(sctps_naglequeued);
13676 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13677 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13678 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13680 SCTP_STAT_INCR(sctps_naglesent);
13683 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13684 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13685 nagle_applies, un_sent);
13686 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13687 asoc->total_flight,
13688 asoc->chunks_on_out_queue, asoc->total_flight_count);
13691 KASSERT(stcb != NULL, ("stcb is NULL"));
13692 SCTP_TCB_LOCK_ASSERT(stcb);
13693 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13694 ("Association about to be freed"));
13695 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13696 ("Association was aborted"));
13698 NET_EPOCH_ENTER(et);
13699 if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
13700 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13701 } else if ((queue_only == 0) &&
13702 (asoc->peers_rwnd == 0) &&
13703 (asoc->total_flight == 0)) {
13704 /* We get to have a probe outstanding */
13705 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13706 } else if (some_on_control) {
13707 int num_out, reason;
13709 /* Here we do control only */
13710 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
13711 &reason, 1, 1, &now, &now_filled,
13712 sctp_get_frag_point(stcb),
13715 NET_EPOCH_EXIT(et);
13716 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13717 queue_only, asoc->peers_rwnd, un_sent,
13718 asoc->total_flight, asoc->chunks_on_out_queue,
13719 asoc->total_output_queue_size, error);
13721 KASSERT(stcb != NULL, ("stcb is NULL"));
13722 SCTP_TCB_LOCK_ASSERT(stcb);
13723 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13724 ("Association about to be freed"));
13725 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13726 ("Association was aborted"));
13730 if (create_lock_applied) {
13731 SCTP_ASOC_CREATE_UNLOCK(inp);
13733 if (stcb != NULL) {
13734 if (local_soresv) {
13735 atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
13737 if (free_cnt_applied) {
13738 atomic_subtract_int(&asoc->refcnt, 1);
13740 SCTP_TCB_UNLOCK(stcb);
13745 if (control != NULL) {
13746 sctp_m_freem(control);
13748 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
13753 * generate an AUTHentication chunk, if required
13756 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13757 struct sctp_auth_chunk **auth_ret, uint32_t *offset,
13758 struct sctp_tcb *stcb, uint8_t chunk)
13760 struct mbuf *m_auth;
13761 struct sctp_auth_chunk *auth;
13765 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13769 if (stcb->asoc.auth_supported == 0) {
13772 /* does the requested chunk require auth? */
13773 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13776 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
13777 if (m_auth == NULL) {
13781 /* reserve some space if this will be the first mbuf */
13783 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13784 /* fill in the AUTH chunk details */
13785 auth = mtod(m_auth, struct sctp_auth_chunk *);
13786 memset(auth, 0, sizeof(*auth));
13787 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13788 auth->ch.chunk_flags = 0;
13789 chunk_len = sizeof(*auth) +
13790 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13791 auth->ch.chunk_length = htons(chunk_len);
13792 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13793 /* key id and hmac digest will be computed and filled in upon send */
13795 /* save the offset where the auth was inserted into the chain */
13797 for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13798 *offset += SCTP_BUF_LEN(cn);
13801 /* update length and return pointer to the auth chunk */
13802 SCTP_BUF_LEN(m_auth) = chunk_len;
13803 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13804 if (auth_ret != NULL)
13812 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
13814 struct nd_prefix *pfx = NULL;
13815 struct nd_pfxrouter *pfxrtr = NULL;
13816 struct sockaddr_in6 gw6;
13818 if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
13821 /* get prefix entry of address */
13823 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13824 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13826 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13827 &src6->sin6_addr, &pfx->ndpr_mask))
13830 /* no prefix entry in the prefix list */
13833 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13834 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13838 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13839 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13841 /* search installed gateway from prefix entry */
13842 LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13843 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13844 gw6.sin6_family = AF_INET6;
13845 gw6.sin6_len = sizeof(struct sockaddr_in6);
13846 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13847 sizeof(struct in6_addr));
13848 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13849 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13850 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13851 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13852 if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
13854 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13859 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13865 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
13868 struct sockaddr_in *sin, *mask;
13869 struct ifaddr *ifa;
13870 struct in_addr srcnetaddr, gwnetaddr;
13872 if (ro == NULL || ro->ro_nh == NULL ||
13873 sifa->address.sa.sa_family != AF_INET) {
13876 ifa = (struct ifaddr *)sifa->ifa;
13877 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13878 sin = &sifa->address.sin;
13879 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13880 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13881 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13882 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13884 sin = &ro->ro_nh->gw4_sa;
13885 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13886 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13887 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13888 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13889 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {