2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/udp.h>
54 #include <machine/in_cksum.h>
58 #define SCTP_MAX_GAPS_INARRAY 4
60 uint8_t right_edge; /* mergable on the right edge */
61 uint8_t left_edge; /* mergable on the left edge */
64 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
67 struct sack_track sack_array[256] = {
68 {0, 0, 0, 0, /* 0x00 */
75 {1, 0, 1, 0, /* 0x01 */
82 {0, 0, 1, 0, /* 0x02 */
89 {1, 0, 1, 0, /* 0x03 */
96 {0, 0, 1, 0, /* 0x04 */
103 {1, 0, 2, 0, /* 0x05 */
110 {0, 0, 1, 0, /* 0x06 */
117 {1, 0, 1, 0, /* 0x07 */
124 {0, 0, 1, 0, /* 0x08 */
131 {1, 0, 2, 0, /* 0x09 */
138 {0, 0, 2, 0, /* 0x0a */
145 {1, 0, 2, 0, /* 0x0b */
152 {0, 0, 1, 0, /* 0x0c */
159 {1, 0, 2, 0, /* 0x0d */
166 {0, 0, 1, 0, /* 0x0e */
173 {1, 0, 1, 0, /* 0x0f */
180 {0, 0, 1, 0, /* 0x10 */
187 {1, 0, 2, 0, /* 0x11 */
194 {0, 0, 2, 0, /* 0x12 */
201 {1, 0, 2, 0, /* 0x13 */
208 {0, 0, 2, 0, /* 0x14 */
215 {1, 0, 3, 0, /* 0x15 */
222 {0, 0, 2, 0, /* 0x16 */
229 {1, 0, 2, 0, /* 0x17 */
236 {0, 0, 1, 0, /* 0x18 */
243 {1, 0, 2, 0, /* 0x19 */
250 {0, 0, 2, 0, /* 0x1a */
257 {1, 0, 2, 0, /* 0x1b */
264 {0, 0, 1, 0, /* 0x1c */
271 {1, 0, 2, 0, /* 0x1d */
278 {0, 0, 1, 0, /* 0x1e */
285 {1, 0, 1, 0, /* 0x1f */
292 {0, 0, 1, 0, /* 0x20 */
299 {1, 0, 2, 0, /* 0x21 */
306 {0, 0, 2, 0, /* 0x22 */
313 {1, 0, 2, 0, /* 0x23 */
320 {0, 0, 2, 0, /* 0x24 */
327 {1, 0, 3, 0, /* 0x25 */
334 {0, 0, 2, 0, /* 0x26 */
341 {1, 0, 2, 0, /* 0x27 */
348 {0, 0, 2, 0, /* 0x28 */
355 {1, 0, 3, 0, /* 0x29 */
362 {0, 0, 3, 0, /* 0x2a */
369 {1, 0, 3, 0, /* 0x2b */
376 {0, 0, 2, 0, /* 0x2c */
383 {1, 0, 3, 0, /* 0x2d */
390 {0, 0, 2, 0, /* 0x2e */
397 {1, 0, 2, 0, /* 0x2f */
404 {0, 0, 1, 0, /* 0x30 */
411 {1, 0, 2, 0, /* 0x31 */
418 {0, 0, 2, 0, /* 0x32 */
425 {1, 0, 2, 0, /* 0x33 */
432 {0, 0, 2, 0, /* 0x34 */
439 {1, 0, 3, 0, /* 0x35 */
446 {0, 0, 2, 0, /* 0x36 */
453 {1, 0, 2, 0, /* 0x37 */
460 {0, 0, 1, 0, /* 0x38 */
467 {1, 0, 2, 0, /* 0x39 */
474 {0, 0, 2, 0, /* 0x3a */
481 {1, 0, 2, 0, /* 0x3b */
488 {0, 0, 1, 0, /* 0x3c */
495 {1, 0, 2, 0, /* 0x3d */
502 {0, 0, 1, 0, /* 0x3e */
509 {1, 0, 1, 0, /* 0x3f */
516 {0, 0, 1, 0, /* 0x40 */
523 {1, 0, 2, 0, /* 0x41 */
530 {0, 0, 2, 0, /* 0x42 */
537 {1, 0, 2, 0, /* 0x43 */
544 {0, 0, 2, 0, /* 0x44 */
551 {1, 0, 3, 0, /* 0x45 */
558 {0, 0, 2, 0, /* 0x46 */
565 {1, 0, 2, 0, /* 0x47 */
572 {0, 0, 2, 0, /* 0x48 */
579 {1, 0, 3, 0, /* 0x49 */
586 {0, 0, 3, 0, /* 0x4a */
593 {1, 0, 3, 0, /* 0x4b */
600 {0, 0, 2, 0, /* 0x4c */
607 {1, 0, 3, 0, /* 0x4d */
614 {0, 0, 2, 0, /* 0x4e */
621 {1, 0, 2, 0, /* 0x4f */
628 {0, 0, 2, 0, /* 0x50 */
635 {1, 0, 3, 0, /* 0x51 */
642 {0, 0, 3, 0, /* 0x52 */
649 {1, 0, 3, 0, /* 0x53 */
656 {0, 0, 3, 0, /* 0x54 */
663 {1, 0, 4, 0, /* 0x55 */
670 {0, 0, 3, 0, /* 0x56 */
677 {1, 0, 3, 0, /* 0x57 */
684 {0, 0, 2, 0, /* 0x58 */
691 {1, 0, 3, 0, /* 0x59 */
698 {0, 0, 3, 0, /* 0x5a */
705 {1, 0, 3, 0, /* 0x5b */
712 {0, 0, 2, 0, /* 0x5c */
719 {1, 0, 3, 0, /* 0x5d */
726 {0, 0, 2, 0, /* 0x5e */
733 {1, 0, 2, 0, /* 0x5f */
740 {0, 0, 1, 0, /* 0x60 */
747 {1, 0, 2, 0, /* 0x61 */
754 {0, 0, 2, 0, /* 0x62 */
761 {1, 0, 2, 0, /* 0x63 */
768 {0, 0, 2, 0, /* 0x64 */
775 {1, 0, 3, 0, /* 0x65 */
782 {0, 0, 2, 0, /* 0x66 */
789 {1, 0, 2, 0, /* 0x67 */
796 {0, 0, 2, 0, /* 0x68 */
803 {1, 0, 3, 0, /* 0x69 */
810 {0, 0, 3, 0, /* 0x6a */
817 {1, 0, 3, 0, /* 0x6b */
824 {0, 0, 2, 0, /* 0x6c */
831 {1, 0, 3, 0, /* 0x6d */
838 {0, 0, 2, 0, /* 0x6e */
845 {1, 0, 2, 0, /* 0x6f */
852 {0, 0, 1, 0, /* 0x70 */
859 {1, 0, 2, 0, /* 0x71 */
866 {0, 0, 2, 0, /* 0x72 */
873 {1, 0, 2, 0, /* 0x73 */
880 {0, 0, 2, 0, /* 0x74 */
887 {1, 0, 3, 0, /* 0x75 */
894 {0, 0, 2, 0, /* 0x76 */
901 {1, 0, 2, 0, /* 0x77 */
908 {0, 0, 1, 0, /* 0x78 */
915 {1, 0, 2, 0, /* 0x79 */
922 {0, 0, 2, 0, /* 0x7a */
929 {1, 0, 2, 0, /* 0x7b */
936 {0, 0, 1, 0, /* 0x7c */
943 {1, 0, 2, 0, /* 0x7d */
950 {0, 0, 1, 0, /* 0x7e */
957 {1, 0, 1, 0, /* 0x7f */
964 {0, 1, 1, 0, /* 0x80 */
971 {1, 1, 2, 0, /* 0x81 */
978 {0, 1, 2, 0, /* 0x82 */
985 {1, 1, 2, 0, /* 0x83 */
992 {0, 1, 2, 0, /* 0x84 */
999 {1, 1, 3, 0, /* 0x85 */
1006 {0, 1, 2, 0, /* 0x86 */
1013 {1, 1, 2, 0, /* 0x87 */
1020 {0, 1, 2, 0, /* 0x88 */
1027 {1, 1, 3, 0, /* 0x89 */
1034 {0, 1, 3, 0, /* 0x8a */
1041 {1, 1, 3, 0, /* 0x8b */
1048 {0, 1, 2, 0, /* 0x8c */
1055 {1, 1, 3, 0, /* 0x8d */
1062 {0, 1, 2, 0, /* 0x8e */
1069 {1, 1, 2, 0, /* 0x8f */
1076 {0, 1, 2, 0, /* 0x90 */
1083 {1, 1, 3, 0, /* 0x91 */
1090 {0, 1, 3, 0, /* 0x92 */
1097 {1, 1, 3, 0, /* 0x93 */
1104 {0, 1, 3, 0, /* 0x94 */
1111 {1, 1, 4, 0, /* 0x95 */
1118 {0, 1, 3, 0, /* 0x96 */
1125 {1, 1, 3, 0, /* 0x97 */
1132 {0, 1, 2, 0, /* 0x98 */
1139 {1, 1, 3, 0, /* 0x99 */
1146 {0, 1, 3, 0, /* 0x9a */
1153 {1, 1, 3, 0, /* 0x9b */
1160 {0, 1, 2, 0, /* 0x9c */
1167 {1, 1, 3, 0, /* 0x9d */
1174 {0, 1, 2, 0, /* 0x9e */
1181 {1, 1, 2, 0, /* 0x9f */
1188 {0, 1, 2, 0, /* 0xa0 */
1195 {1, 1, 3, 0, /* 0xa1 */
1202 {0, 1, 3, 0, /* 0xa2 */
1209 {1, 1, 3, 0, /* 0xa3 */
1216 {0, 1, 3, 0, /* 0xa4 */
1223 {1, 1, 4, 0, /* 0xa5 */
1230 {0, 1, 3, 0, /* 0xa6 */
1237 {1, 1, 3, 0, /* 0xa7 */
1244 {0, 1, 3, 0, /* 0xa8 */
1251 {1, 1, 4, 0, /* 0xa9 */
1258 {0, 1, 4, 0, /* 0xaa */
1265 {1, 1, 4, 0, /* 0xab */
1272 {0, 1, 3, 0, /* 0xac */
1279 {1, 1, 4, 0, /* 0xad */
1286 {0, 1, 3, 0, /* 0xae */
1293 {1, 1, 3, 0, /* 0xaf */
1300 {0, 1, 2, 0, /* 0xb0 */
1307 {1, 1, 3, 0, /* 0xb1 */
1314 {0, 1, 3, 0, /* 0xb2 */
1321 {1, 1, 3, 0, /* 0xb3 */
1328 {0, 1, 3, 0, /* 0xb4 */
1335 {1, 1, 4, 0, /* 0xb5 */
1342 {0, 1, 3, 0, /* 0xb6 */
1349 {1, 1, 3, 0, /* 0xb7 */
1356 {0, 1, 2, 0, /* 0xb8 */
1363 {1, 1, 3, 0, /* 0xb9 */
1370 {0, 1, 3, 0, /* 0xba */
1377 {1, 1, 3, 0, /* 0xbb */
1384 {0, 1, 2, 0, /* 0xbc */
1391 {1, 1, 3, 0, /* 0xbd */
1398 {0, 1, 2, 0, /* 0xbe */
1405 {1, 1, 2, 0, /* 0xbf */
1412 {0, 1, 1, 0, /* 0xc0 */
1419 {1, 1, 2, 0, /* 0xc1 */
1426 {0, 1, 2, 0, /* 0xc2 */
1433 {1, 1, 2, 0, /* 0xc3 */
1440 {0, 1, 2, 0, /* 0xc4 */
1447 {1, 1, 3, 0, /* 0xc5 */
1454 {0, 1, 2, 0, /* 0xc6 */
1461 {1, 1, 2, 0, /* 0xc7 */
1468 {0, 1, 2, 0, /* 0xc8 */
1475 {1, 1, 3, 0, /* 0xc9 */
1482 {0, 1, 3, 0, /* 0xca */
1489 {1, 1, 3, 0, /* 0xcb */
1496 {0, 1, 2, 0, /* 0xcc */
1503 {1, 1, 3, 0, /* 0xcd */
1510 {0, 1, 2, 0, /* 0xce */
1517 {1, 1, 2, 0, /* 0xcf */
1524 {0, 1, 2, 0, /* 0xd0 */
1531 {1, 1, 3, 0, /* 0xd1 */
1538 {0, 1, 3, 0, /* 0xd2 */
1545 {1, 1, 3, 0, /* 0xd3 */
1552 {0, 1, 3, 0, /* 0xd4 */
1559 {1, 1, 4, 0, /* 0xd5 */
1566 {0, 1, 3, 0, /* 0xd6 */
1573 {1, 1, 3, 0, /* 0xd7 */
1580 {0, 1, 2, 0, /* 0xd8 */
1587 {1, 1, 3, 0, /* 0xd9 */
1594 {0, 1, 3, 0, /* 0xda */
1601 {1, 1, 3, 0, /* 0xdb */
1608 {0, 1, 2, 0, /* 0xdc */
1615 {1, 1, 3, 0, /* 0xdd */
1622 {0, 1, 2, 0, /* 0xde */
1629 {1, 1, 2, 0, /* 0xdf */
1636 {0, 1, 1, 0, /* 0xe0 */
1643 {1, 1, 2, 0, /* 0xe1 */
1650 {0, 1, 2, 0, /* 0xe2 */
1657 {1, 1, 2, 0, /* 0xe3 */
1664 {0, 1, 2, 0, /* 0xe4 */
1671 {1, 1, 3, 0, /* 0xe5 */
1678 {0, 1, 2, 0, /* 0xe6 */
1685 {1, 1, 2, 0, /* 0xe7 */
1692 {0, 1, 2, 0, /* 0xe8 */
1699 {1, 1, 3, 0, /* 0xe9 */
1706 {0, 1, 3, 0, /* 0xea */
1713 {1, 1, 3, 0, /* 0xeb */
1720 {0, 1, 2, 0, /* 0xec */
1727 {1, 1, 3, 0, /* 0xed */
1734 {0, 1, 2, 0, /* 0xee */
1741 {1, 1, 2, 0, /* 0xef */
1748 {0, 1, 1, 0, /* 0xf0 */
1755 {1, 1, 2, 0, /* 0xf1 */
1762 {0, 1, 2, 0, /* 0xf2 */
1769 {1, 1, 2, 0, /* 0xf3 */
1776 {0, 1, 2, 0, /* 0xf4 */
1783 {1, 1, 3, 0, /* 0xf5 */
1790 {0, 1, 2, 0, /* 0xf6 */
1797 {1, 1, 2, 0, /* 0xf7 */
1804 {0, 1, 1, 0, /* 0xf8 */
1811 {1, 1, 2, 0, /* 0xf9 */
1818 {0, 1, 2, 0, /* 0xfa */
1825 {1, 1, 2, 0, /* 0xfb */
1832 {0, 1, 1, 0, /* 0xfc */
1839 {1, 1, 2, 0, /* 0xfd */
1846 {0, 1, 1, 0, /* 0xfe */
1853 {1, 1, 1, 0, /* 0xff */
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865 int ipv4_addr_legal,
1866 int ipv6_addr_legal,
1868 int ipv4_local_scope,
1873 if ((loopback_scope == 0) &&
1874 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1876 * skip loopback if not in scope *
1880 switch (ifa->address.sa.sa_family) {
1882 if (ipv4_addr_legal) {
1883 struct sockaddr_in *sin;
1885 sin = (struct sockaddr_in *)&ifa->address.sin;
1886 if (sin->sin_addr.s_addr == 0) {
1887 /* not in scope , unspecified */
1890 if ((ipv4_local_scope == 0) &&
1891 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 /* private address not in scope */
1901 if (ipv6_addr_legal) {
1902 struct sockaddr_in6 *sin6;
1905 * Must update the flags, bummer, which means any
1906 * IFA locks must now be applied HERE <->
1909 sctp_gather_internal_ifa_flags(ifa);
1911 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1914 /* ok to use deprecated addresses? */
1915 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 /* skip unspecifed addresses */
1920 if ( /* (local_scope == 0) && */
1921 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1924 if ((site_scope == 0) &&
1925 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1939 static struct mbuf *
1940 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1942 struct sctp_paramhdr *parmh;
1946 if (ifa->address.sa.sa_family == AF_INET) {
1947 len = sizeof(struct sctp_ipv4addr_param);
1948 } else if (ifa->address.sa.sa_family == AF_INET6) {
1949 len = sizeof(struct sctp_ipv6addr_param);
1954 if (M_TRAILINGSPACE(m) >= len) {
1955 /* easy side we just drop it on the end */
1956 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1959 /* Need more space */
1961 while (SCTP_BUF_NEXT(mret) != NULL) {
1962 mret = SCTP_BUF_NEXT(mret);
1964 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 if (SCTP_BUF_NEXT(mret) == NULL) {
1966 /* We are hosed, can't add more addresses */
1969 mret = SCTP_BUF_NEXT(mret);
1970 parmh = mtod(mret, struct sctp_paramhdr *);
1972 /* now add the parameter */
1973 switch (ifa->address.sa.sa_family) {
1976 struct sctp_ipv4addr_param *ipv4p;
1977 struct sockaddr_in *sin;
1979 sin = (struct sockaddr_in *)&ifa->address.sin;
1980 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 parmh->param_length = htons(len);
1983 ipv4p->addr = sin->sin_addr.s_addr;
1984 SCTP_BUF_LEN(mret) += len;
1990 struct sctp_ipv6addr_param *ipv6p;
1991 struct sockaddr_in6 *sin6;
1993 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 parmh->param_length = htons(len);
1997 memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 sizeof(ipv6p->addr));
1999 /* clear embedded scope in the address */
2000 in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 SCTP_BUF_LEN(mret) += len;
2013 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014 struct mbuf *m_at, int cnt_inits_to)
2016 struct sctp_vrf *vrf = NULL;
2017 int cnt, limit_out = 0, total_count;
2020 vrf_id = inp->def_vrf_id;
2021 SCTP_IPI_ADDR_RLOCK();
2022 vrf = sctp_find_vrf(vrf_id);
2024 SCTP_IPI_ADDR_RUNLOCK();
2027 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 struct sctp_ifa *sctp_ifap;
2029 struct sctp_ifn *sctp_ifnp;
2032 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2034 cnt = SCTP_ADDRESS_LIMIT;
2037 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 if ((scope->loopback_scope == 0) &&
2039 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2041 * Skip loopback devices if loopback_scope
2046 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 if (sctp_is_address_in_scope(sctp_ifap,
2048 scope->ipv4_addr_legal,
2049 scope->ipv6_addr_legal,
2050 scope->loopback_scope,
2051 scope->ipv4_local_scope,
2053 scope->site_scope, 1) == 0) {
2057 if (cnt > SCTP_ADDRESS_LIMIT) {
2061 if (cnt > SCTP_ADDRESS_LIMIT) {
2068 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2070 if ((scope->loopback_scope == 0) &&
2071 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2073 * Skip loopback devices if
2074 * loopback_scope not set
2078 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 if (sctp_is_address_in_scope(sctp_ifap,
2080 scope->ipv4_addr_legal,
2081 scope->ipv6_addr_legal,
2082 scope->loopback_scope,
2083 scope->ipv4_local_scope,
2085 scope->site_scope, 0) == 0) {
2088 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2099 if (total_count > SCTP_ADDRESS_LIMIT) {
2100 /* No more addresses */
2108 struct sctp_laddr *laddr;
2111 /* First, how many ? */
2112 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 if (laddr->ifa == NULL) {
2116 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2118 * Address being deleted by the system, dont
2122 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2124 * Address being deleted on this ep don't
2129 if (sctp_is_address_in_scope(laddr->ifa,
2130 scope->ipv4_addr_legal,
2131 scope->ipv6_addr_legal,
2132 scope->loopback_scope,
2133 scope->ipv4_local_scope,
2135 scope->site_scope, 1) == 0) {
2140 if (cnt > SCTP_ADDRESS_LIMIT) {
2144 * To get through a NAT we only list addresses if we have
2145 * more than one. That way if you just bind a single address
2146 * we let the source of the init dictate our address.
2149 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2151 if (laddr->ifa == NULL) {
2154 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2157 if (sctp_is_address_in_scope(laddr->ifa,
2158 scope->ipv4_addr_legal,
2159 scope->ipv6_addr_legal,
2160 scope->loopback_scope,
2161 scope->ipv4_local_scope,
2163 scope->site_scope, 0) == 0) {
2166 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2168 if (cnt >= SCTP_ADDRESS_LIMIT) {
2174 SCTP_IPI_ADDR_RUNLOCK();
2178 static struct sctp_ifa *
2179 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180 uint8_t dest_is_loop,
2181 uint8_t dest_is_priv,
2184 uint8_t dest_is_global = 0;
2186 /* dest_is_priv is true if destination is a private address */
2187 /* dest_is_loop is true if destination is a loopback addresses */
2190 * Here we determine if its a preferred address. A preferred address
2191 * means it is the same scope or higher scope then the destination.
2192 * L = loopback, P = private, G = global
2193 * -----------------------------------------
2194 * src | dest | result
2195 * ----------------------------------------
2197 * -----------------------------------------
2198 * P | L | yes-v4 no-v6
2199 * -----------------------------------------
2200 * G | L | yes-v4 no-v6
2201 * -----------------------------------------
2203 * -----------------------------------------
2205 * -----------------------------------------
2207 * -----------------------------------------
2209 * -----------------------------------------
2211 * -----------------------------------------
2213 * -----------------------------------------
2216 if (ifa->address.sa.sa_family != fam) {
2217 /* forget mis-matched family */
2220 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2223 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2224 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2225 /* Ok the address may be ok */
2226 if (fam == AF_INET6) {
2227 /* ok to use deprecated addresses? no lets not! */
2228 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2229 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2232 if (ifa->src_is_priv && !ifa->src_is_loop) {
2234 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2238 if (ifa->src_is_glob) {
2240 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2246 * Now that we know what is what, implement or table this could in
2247 * theory be done slicker (it used to be), but this is
2248 * straightforward and easier to validate :-)
2250 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2251 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2252 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2253 dest_is_loop, dest_is_priv, dest_is_global);
2255 if ((ifa->src_is_loop) && (dest_is_priv)) {
2256 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2259 if ((ifa->src_is_glob) && (dest_is_priv)) {
2260 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2263 if ((ifa->src_is_loop) && (dest_is_global)) {
2264 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2267 if ((ifa->src_is_priv) && (dest_is_global)) {
2268 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2271 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2272 /* its a preferred address */
2276 static struct sctp_ifa *
2277 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2278 uint8_t dest_is_loop,
2279 uint8_t dest_is_priv,
2282 uint8_t dest_is_global = 0;
2285 * Here we determine if its a acceptable address. A acceptable
2286 * address means it is the same scope or higher scope but we can
2287 * allow for NAT which means its ok to have a global dest and a
2290 * L = loopback, P = private, G = global
2291 * ----------------------------------------- src | dest | result
2292 * ----------------------------------------- L | L | yes
2293 * ----------------------------------------- P | L |
2294 * yes-v4 no-v6 ----------------------------------------- G |
2295 * L | yes ----------------------------------------- L |
2296 * P | no ----------------------------------------- P | P
2297 * | yes ----------------------------------------- G | P
2298 * | yes - May not work -----------------------------------------
2299 * L | G | no ----------------------------------------- P
2300 * | G | yes - May not work
2301 * ----------------------------------------- G | G | yes
2302 * -----------------------------------------
2305 if (ifa->address.sa.sa_family != fam) {
2306 /* forget non matching family */
2309 /* Ok the address may be ok */
2310 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2313 if (fam == AF_INET6) {
2314 /* ok to use deprecated addresses? */
2315 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2318 if (ifa->src_is_priv) {
2319 /* Special case, linklocal to loop */
2325 * Now that we know what is what, implement our table. This could in
2326 * theory be done slicker (it used to be), but this is
2327 * straightforward and easier to validate :-)
2329 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2332 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2335 /* its an acceptable address */
2340 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2342 struct sctp_laddr *laddr;
2345 /* There are no restrictions, no TCB :-) */
2348 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2349 if (laddr->ifa == NULL) {
2350 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2354 if (laddr->ifa == ifa) {
2355 /* Yes it is on the list */
2364 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2366 struct sctp_laddr *laddr;
2370 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2371 if (laddr->ifa == NULL) {
2372 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2376 if ((laddr->ifa == ifa) && laddr->action == 0)
2385 static struct sctp_ifa *
2386 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2389 int non_asoc_addr_ok,
2390 uint8_t dest_is_priv,
2391 uint8_t dest_is_loop,
2394 struct sctp_laddr *laddr, *starting_point;
2397 struct sctp_ifn *sctp_ifn;
2398 struct sctp_ifa *sctp_ifa, *sifa;
2399 struct sctp_vrf *vrf;
2402 vrf = sctp_find_vrf(vrf_id);
2406 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2407 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2408 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2410 * first question, is the ifn we will emit on in our list, if so, we
2411 * want such an address. Note that we first looked for a preferred
2415 /* is a preferred one on the interface we route out? */
2416 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2417 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2418 (non_asoc_addr_ok == 0))
2420 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2425 if (sctp_is_addr_in_ep(inp, sifa)) {
2426 atomic_add_int(&sifa->refcount, 1);
2432 * ok, now we now need to find one on the list of the addresses. We
2433 * can't get one on the emitting interface so let's find first a
2434 * preferred one. If not that an acceptable one otherwise... we
2437 starting_point = inp->next_addr_touse;
2439 if (inp->next_addr_touse == NULL) {
2440 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2443 for (laddr = inp->next_addr_touse; laddr;
2444 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2445 if (laddr->ifa == NULL) {
2446 /* address has been removed */
2449 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2450 /* address is being deleted */
2453 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2457 atomic_add_int(&sifa->refcount, 1);
2460 if (resettotop == 0) {
2461 inp->next_addr_touse = NULL;
2464 inp->next_addr_touse = starting_point;
2467 if (inp->next_addr_touse == NULL) {
2468 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2471 /* ok, what about an acceptable address in the inp */
2472 for (laddr = inp->next_addr_touse; laddr;
2473 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2474 if (laddr->ifa == NULL) {
2475 /* address has been removed */
2478 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2479 /* address is being deleted */
2482 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2486 atomic_add_int(&sifa->refcount, 1);
2489 if (resettotop == 0) {
2490 inp->next_addr_touse = NULL;
2491 goto once_again_too;
2494 * no address bound can be a source for the destination we are in
2502 static struct sctp_ifa *
2503 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2504 struct sctp_tcb *stcb,
2505 struct sctp_nets *net,
2508 uint8_t dest_is_priv,
2509 uint8_t dest_is_loop,
2510 int non_asoc_addr_ok,
2513 struct sctp_laddr *laddr, *starting_point;
2515 struct sctp_ifn *sctp_ifn;
2516 struct sctp_ifa *sctp_ifa, *sifa;
2517 uint8_t start_at_beginning = 0;
2518 struct sctp_vrf *vrf;
2522 * first question, is the ifn we will emit on in our list, if so, we
2525 vrf = sctp_find_vrf(vrf_id);
2529 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2530 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2531 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2534 * first question, is the ifn we will emit on in our list? If so,
2535 * we want that one. First we look for a preferred. Second, we go
2536 * for an acceptable.
2539 /* first try for a preferred address on the ep */
2540 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2541 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2543 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2544 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2547 if (((non_asoc_addr_ok == 0) &&
2548 (sctp_is_addr_restricted(stcb, sifa))) ||
2549 (non_asoc_addr_ok &&
2550 (sctp_is_addr_restricted(stcb, sifa)) &&
2551 (!sctp_is_addr_pending(stcb, sifa)))) {
2552 /* on the no-no list */
2555 atomic_add_int(&sifa->refcount, 1);
2559 /* next try for an acceptable address on the ep */
2560 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2561 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2563 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2564 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2567 if (((non_asoc_addr_ok == 0) &&
2568 (sctp_is_addr_restricted(stcb, sifa))) ||
2569 (non_asoc_addr_ok &&
2570 (sctp_is_addr_restricted(stcb, sifa)) &&
2571 (!sctp_is_addr_pending(stcb, sifa)))) {
2572 /* on the no-no list */
2575 atomic_add_int(&sifa->refcount, 1);
2582 * if we can't find one like that then we must look at all addresses
2583 * bound to pick one at first preferable then secondly acceptable.
2585 starting_point = stcb->asoc.last_used_address;
2587 if (stcb->asoc.last_used_address == NULL) {
2588 start_at_beginning = 1;
2589 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2591 /* search beginning with the last used address */
2592 for (laddr = stcb->asoc.last_used_address; laddr;
2593 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2594 if (laddr->ifa == NULL) {
2595 /* address has been removed */
2598 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2599 /* address is being deleted */
2602 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2605 if (((non_asoc_addr_ok == 0) &&
2606 (sctp_is_addr_restricted(stcb, sifa))) ||
2607 (non_asoc_addr_ok &&
2608 (sctp_is_addr_restricted(stcb, sifa)) &&
2609 (!sctp_is_addr_pending(stcb, sifa)))) {
2610 /* on the no-no list */
2613 stcb->asoc.last_used_address = laddr;
2614 atomic_add_int(&sifa->refcount, 1);
2617 if (start_at_beginning == 0) {
2618 stcb->asoc.last_used_address = NULL;
2619 goto sctp_from_the_top;
2621 /* now try for any higher scope than the destination */
2622 stcb->asoc.last_used_address = starting_point;
2623 start_at_beginning = 0;
2625 if (stcb->asoc.last_used_address == NULL) {
2626 start_at_beginning = 1;
2627 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2629 /* search beginning with the last used address */
2630 for (laddr = stcb->asoc.last_used_address; laddr;
2631 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2632 if (laddr->ifa == NULL) {
2633 /* address has been removed */
2636 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2637 /* address is being deleted */
2640 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2644 if (((non_asoc_addr_ok == 0) &&
2645 (sctp_is_addr_restricted(stcb, sifa))) ||
2646 (non_asoc_addr_ok &&
2647 (sctp_is_addr_restricted(stcb, sifa)) &&
2648 (!sctp_is_addr_pending(stcb, sifa)))) {
2649 /* on the no-no list */
2652 stcb->asoc.last_used_address = laddr;
2653 atomic_add_int(&sifa->refcount, 1);
2656 if (start_at_beginning == 0) {
2657 stcb->asoc.last_used_address = NULL;
2658 goto sctp_from_the_top2;
2663 static struct sctp_ifa *
2664 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2665 struct sctp_tcb *stcb,
2666 int non_asoc_addr_ok,
2667 uint8_t dest_is_loop,
2668 uint8_t dest_is_priv,
2674 struct sctp_ifa *ifa, *sifa;
2675 int num_eligible_addr = 0;
2678 struct sockaddr_in6 sin6, lsa6;
2680 if (fam == AF_INET6) {
2681 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2682 (void)sa6_recoverscope(&sin6);
2685 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2686 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2687 (non_asoc_addr_ok == 0))
2689 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2694 if (fam == AF_INET6 &&
2696 sifa->src_is_loop && sifa->src_is_priv) {
2698 * don't allow fe80::1 to be a src on loop ::1, we
2699 * don't list it to the peer so we will get an
2704 if (fam == AF_INET6 &&
2705 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2706 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2708 * link-local <-> link-local must belong to the same
2711 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2712 (void)sa6_recoverscope(&lsa6);
2713 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2720 * Check if the IPv6 address matches to next-hop. In the
2721 * mobile case, old IPv6 address may be not deleted from the
2722 * interface. Then, the interface has previous and new
2723 * addresses. We should use one corresponding to the
2724 * next-hop. (by micchie)
2727 if (stcb && fam == AF_INET6 &&
2728 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2735 /* Avoid topologically incorrect IPv4 address */
2736 if (stcb && fam == AF_INET &&
2737 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2738 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2743 if (sctp_is_address_in_scope(ifa,
2744 stcb->asoc.ipv4_addr_legal,
2745 stcb->asoc.ipv6_addr_legal,
2746 stcb->asoc.loopback_scope,
2747 stcb->asoc.ipv4_local_scope,
2748 stcb->asoc.local_scope,
2749 stcb->asoc.site_scope, 0) == 0) {
2752 if (((non_asoc_addr_ok == 0) &&
2753 (sctp_is_addr_restricted(stcb, sifa))) ||
2754 (non_asoc_addr_ok &&
2755 (sctp_is_addr_restricted(stcb, sifa)) &&
2756 (!sctp_is_addr_pending(stcb, sifa)))) {
2758 * It is restricted for some reason..
2759 * probably not yet added.
2764 if (num_eligible_addr >= addr_wanted) {
2767 num_eligible_addr++;
2774 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2775 struct sctp_tcb *stcb,
2776 int non_asoc_addr_ok,
2777 uint8_t dest_is_loop,
2778 uint8_t dest_is_priv,
2781 struct sctp_ifa *ifa, *sifa;
2782 int num_eligible_addr = 0;
2784 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2785 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2786 (non_asoc_addr_ok == 0)) {
2789 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2795 if (sctp_is_address_in_scope(ifa,
2796 stcb->asoc.ipv4_addr_legal,
2797 stcb->asoc.ipv6_addr_legal,
2798 stcb->asoc.loopback_scope,
2799 stcb->asoc.ipv4_local_scope,
2800 stcb->asoc.local_scope,
2801 stcb->asoc.site_scope, 0) == 0) {
2804 if (((non_asoc_addr_ok == 0) &&
2805 (sctp_is_addr_restricted(stcb, sifa))) ||
2806 (non_asoc_addr_ok &&
2807 (sctp_is_addr_restricted(stcb, sifa)) &&
2808 (!sctp_is_addr_pending(stcb, sifa)))) {
2810 * It is restricted for some reason..
2811 * probably not yet added.
2816 num_eligible_addr++;
2818 return (num_eligible_addr);
2821 static struct sctp_ifa *
2822 sctp_choose_boundall(struct sctp_inpcb *inp,
2823 struct sctp_tcb *stcb,
2824 struct sctp_nets *net,
2827 uint8_t dest_is_priv,
2828 uint8_t dest_is_loop,
2829 int non_asoc_addr_ok,
2832 int cur_addr_num = 0, num_preferred = 0;
2834 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2835 struct sctp_ifa *sctp_ifa, *sifa;
2837 struct sctp_vrf *vrf;
2840 * For boundall we can use any address in the association.
2841 * If non_asoc_addr_ok is set we can use any address (at least in
2842 * theory). So we look for preferred addresses first. If we find one,
2843 * we use it. Otherwise we next try to get an address on the
2844 * interface, which we should be able to do (unless non_asoc_addr_ok
2845 * is false and we are routed out that way). In these cases where we
2846 * can't use the address of the interface we go through all the
2847 * ifn's looking for an address we can use and fill that in. Punting
2848 * means we send back address 0, which will probably cause problems
2849 * actually since then IP will fill in the address of the route ifn,
2850 * which means we probably already rejected it.. i.e. here comes an
2853 vrf = sctp_find_vrf(vrf_id);
2857 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2858 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2859 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2860 if (sctp_ifn == NULL) {
2861 /* ?? We don't have this guy ?? */
2862 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2863 goto bound_all_plan_b;
2865 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2866 ifn_index, sctp_ifn->ifn_name);
2869 cur_addr_num = net->indx_of_eligible_next_to_use;
2871 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2876 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2877 num_preferred, sctp_ifn->ifn_name);
2878 if (num_preferred == 0) {
2880 * no eligible addresses, we must use some other interface
2881 * address if we can find one.
2883 goto bound_all_plan_b;
2886 * Ok we have num_eligible_addr set with how many we can use, this
2887 * may vary from call to call due to addresses being deprecated
2890 if (cur_addr_num >= num_preferred) {
2894 * select the nth address from the list (where cur_addr_num is the
2895 * nth) and 0 is the first one, 1 is the second one etc...
2897 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2899 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2900 dest_is_priv, cur_addr_num, fam, ro);
2902 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2904 atomic_add_int(&sctp_ifa->refcount, 1);
2906 /* save off where the next one we will want */
2907 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2912 * plan_b: Look at all interfaces and find a preferred address. If
2913 * no preferred fall through to plan_c.
2916 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2917 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2918 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2919 sctp_ifn->ifn_name);
2920 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2921 /* wrong base scope */
2922 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2925 if ((sctp_ifn == looked_at) && looked_at) {
2926 /* already looked at this guy */
2927 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2930 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2931 dest_is_loop, dest_is_priv, fam);
2932 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2933 "Found ifn:%p %d preferred source addresses\n",
2934 ifn, num_preferred);
2935 if (num_preferred == 0) {
2936 /* None on this interface. */
2937 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2940 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2941 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2942 num_preferred, sctp_ifn, cur_addr_num);
2945 * Ok we have num_eligible_addr set with how many we can
2946 * use, this may vary from call to call due to addresses
2947 * being deprecated etc..
2949 if (cur_addr_num >= num_preferred) {
2952 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2953 dest_is_priv, cur_addr_num, fam, ro);
2957 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2958 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2960 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2961 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2962 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2963 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2965 atomic_add_int(&sifa->refcount, 1);
2970 /* plan_c: do we have an acceptable address on the emit interface */
2971 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2972 if (emit_ifn == NULL) {
2975 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2976 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2977 (non_asoc_addr_ok == 0))
2979 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2984 if (sctp_is_address_in_scope(sifa,
2985 stcb->asoc.ipv4_addr_legal,
2986 stcb->asoc.ipv6_addr_legal,
2987 stcb->asoc.loopback_scope,
2988 stcb->asoc.ipv4_local_scope,
2989 stcb->asoc.local_scope,
2990 stcb->asoc.site_scope, 0) == 0) {
2993 if (((non_asoc_addr_ok == 0) &&
2994 (sctp_is_addr_restricted(stcb, sifa))) ||
2995 (non_asoc_addr_ok &&
2996 (sctp_is_addr_restricted(stcb, sifa)) &&
2997 (!sctp_is_addr_pending(stcb, sifa)))) {
2999 * It is restricted for some reason..
3000 * probably not yet added.
3005 atomic_add_int(&sifa->refcount, 1);
3010 * plan_d: We are in trouble. No preferred address on the emit
3011 * interface. And not even a preferred address on all interfaces. Go
3012 * out and see if we can find an acceptable address somewhere
3013 * amongst all interfaces.
3015 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
3016 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3017 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3018 /* wrong base scope */
3021 if ((sctp_ifn == looked_at) && looked_at)
3022 /* already looked at this guy */
3025 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3026 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3027 (non_asoc_addr_ok == 0))
3029 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3035 if (sctp_is_address_in_scope(sifa,
3036 stcb->asoc.ipv4_addr_legal,
3037 stcb->asoc.ipv6_addr_legal,
3038 stcb->asoc.loopback_scope,
3039 stcb->asoc.ipv4_local_scope,
3040 stcb->asoc.local_scope,
3041 stcb->asoc.site_scope, 0) == 0) {
3044 if (((non_asoc_addr_ok == 0) &&
3045 (sctp_is_addr_restricted(stcb, sifa))) ||
3046 (non_asoc_addr_ok &&
3047 (sctp_is_addr_restricted(stcb, sifa)) &&
3048 (!sctp_is_addr_pending(stcb, sifa)))) {
3050 * It is restricted for some
3051 * reason.. probably not yet added.
3056 atomic_add_int(&sifa->refcount, 1);
3061 * Ok we can find NO address to source from that is not on our
3062 * restricted list and non_asoc_address is NOT ok, or it is on our
3063 * restricted list. We can't source to it :-(
3070 /* tcb may be NULL */
3072 sctp_source_address_selection(struct sctp_inpcb *inp,
3073 struct sctp_tcb *stcb,
3075 struct sctp_nets *net,
3076 int non_asoc_addr_ok, uint32_t vrf_id)
3078 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3081 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3084 struct sctp_ifa *answer;
3085 uint8_t dest_is_priv, dest_is_loop;
3089 * Rules: - Find the route if needed, cache if I can. - Look at
3090 * interface address in route, Is it in the bound list. If so we
3091 * have the best source. - If not we must rotate amongst the
3096 * Do we need to pay attention to scope. We can have a private address
3097 * or a global address we are sourcing or sending to. So if we draw
3099 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3101 * ------------------------------------------
3102 * source * dest * result
3103 * -----------------------------------------
3104 * <a> Private * Global * NAT
3105 * -----------------------------------------
3106 * <b> Private * Private * No problem
3107 * -----------------------------------------
3108 * <c> Global * Private * Huh, How will this work?
3109 * -----------------------------------------
3110 * <d> Global * Global * No Problem
3111 *------------------------------------------
3112 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3114 *------------------------------------------
3115 * source * dest * result
3116 * -----------------------------------------
3117 * <a> Linklocal * Global *
3118 * -----------------------------------------
3119 * <b> Linklocal * Linklocal * No problem
3120 * -----------------------------------------
3121 * <c> Global * Linklocal * Huh, How will this work?
3122 * -----------------------------------------
3123 * <d> Global * Global * No Problem
3124 *------------------------------------------
3125 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3127 * And then we add to that what happens if there are multiple addresses
3128 * assigned to an interface. Remember the ifa on a ifn is a linked
3129 * list of addresses. So one interface can have more than one IP
3130 * address. What happens if we have both a private and a global
3131 * address? Do we then use context of destination to sort out which
3132 * one is best? And what about NAT's sending P->G may get you a NAT
3133 * translation, or should you select the G thats on the interface in
3138 * - count the number of addresses on the interface.
3139 * - if it is one, no problem except case <c>.
3140 * For <a> we will assume a NAT out there.
3141 * - if there are more than one, then we need to worry about scope P
3142 * or G. We should prefer G -> G and P -> P if possible.
3143 * Then as a secondary fall back to mixed types G->P being a last
3145 * - The above all works for bound all, but bound specific we need to
3146 * use the same concept but instead only consider the bound
3147 * addresses. If the bound set is NOT assigned to the interface then
3148 * we must use rotation amongst the bound addresses..
3150 if (ro->ro_rt == NULL) {
3152 * Need a route to cache.
3154 SCTP_RTALLOC(ro, vrf_id);
3156 if (ro->ro_rt == NULL) {
3159 fam = to->sin_family;
3160 dest_is_priv = dest_is_loop = 0;
3161 /* Setup our scopes for the destination */
3164 /* Scope based on outbound address */
3165 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3168 /* mark it as local */
3169 net->addr_is_local = 1;
3171 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3177 /* Scope based on outbound address */
3178 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3179 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3181 * If the address is a loopback address, which
3182 * consists of "::1" OR "fe80::1%lo0", we are
3183 * loopback scope. But we don't use dest_is_priv
3184 * (link local addresses).
3188 /* mark it as local */
3189 net->addr_is_local = 1;
3191 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3197 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3198 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3199 SCTP_IPI_ADDR_RLOCK();
3200 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3204 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3205 dest_is_priv, dest_is_loop,
3206 non_asoc_addr_ok, fam);
3207 SCTP_IPI_ADDR_RUNLOCK();
3214 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3215 vrf_id, dest_is_priv,
3217 non_asoc_addr_ok, fam);
3219 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3224 SCTP_IPI_ADDR_RUNLOCK();
3229 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3234 tlen = SCTP_BUF_LEN(control);
3237 * Independent of how many mbufs, find the c_type inside the control
3238 * structure and copy out the data.
3241 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3242 /* not enough room for one more we are done. */
3245 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3246 if (((int)cmh.cmsg_len + at) > tlen) {
3248 * this is real messed up since there is not enough
3249 * data here to cover the cmsg header. We are done.
3253 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3254 (c_type == cmh.cmsg_type)) {
3255 /* found the one we want, copy it out */
3256 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3257 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3259 * space of cmsg_len after header not big
3264 m_copydata(control, at, cpsize, data);
3267 at += CMSG_ALIGN(cmh.cmsg_len);
3268 if (cmh.cmsg_len == 0) {
3277 static struct mbuf *
3278 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3279 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3281 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3282 struct sctp_state_cookie *stc;
3283 struct sctp_paramhdr *ph;
3289 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3290 sizeof(struct sctp_paramhdr)), 0,
3291 M_DONTWAIT, 1, MT_DATA);
3295 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3296 if (copy_init == NULL) {
3300 #ifdef SCTP_MBUF_LOGGING
3301 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3306 if (SCTP_BUF_IS_EXTENDED(mat)) {
3307 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3309 mat = SCTP_BUF_NEXT(mat);
3313 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3315 if (copy_initack == NULL) {
3317 sctp_m_freem(copy_init);
3320 #ifdef SCTP_MBUF_LOGGING
3321 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3326 if (SCTP_BUF_IS_EXTENDED(mat)) {
3327 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3329 mat = SCTP_BUF_NEXT(mat);
3333 /* easy side we just drop it on the end */
3334 ph = mtod(mret, struct sctp_paramhdr *);
3335 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3336 sizeof(struct sctp_paramhdr);
3337 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3338 sizeof(struct sctp_paramhdr));
3339 ph->param_type = htons(SCTP_STATE_COOKIE);
3340 ph->param_length = 0; /* fill in at the end */
3341 /* Fill in the stc cookie data */
3342 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3344 /* tack the INIT and then the INIT-ACK onto the chain */
3347 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3348 cookie_sz += SCTP_BUF_LEN(m_at);
3349 if (SCTP_BUF_NEXT(m_at) == NULL) {
3350 SCTP_BUF_NEXT(m_at) = copy_init;
3355 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3356 cookie_sz += SCTP_BUF_LEN(m_at);
3357 if (SCTP_BUF_NEXT(m_at) == NULL) {
3358 SCTP_BUF_NEXT(m_at) = copy_initack;
3363 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3364 cookie_sz += SCTP_BUF_LEN(m_at);
3365 if (SCTP_BUF_NEXT(m_at) == NULL) {
3369 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3371 /* no space, so free the entire chain */
3375 SCTP_BUF_LEN(sig) = 0;
3376 SCTP_BUF_NEXT(m_at) = sig;
3378 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3379 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3381 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3382 cookie_sz += SCTP_SIGNATURE_SIZE;
3383 ph->param_length = htons(cookie_sz);
3389 sctp_get_ect(struct sctp_tcb *stcb,
3390 struct sctp_tmit_chunk *chk)
3392 uint8_t this_random;
3395 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3398 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3399 /* no nonce, always return ECT0 */
3400 return (SCTP_ECT0_BIT);
3402 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3403 /* Peer does NOT support it, so we send a ECT0 only */
3404 return (SCTP_ECT0_BIT);
3407 return (SCTP_ECT0_BIT);
3409 if ((stcb->asoc.hb_random_idx > 3) ||
3410 ((stcb->asoc.hb_random_idx == 3) &&
3411 (stcb->asoc.hb_ect_randombit > 7))) {
3415 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3416 memcpy(stcb->asoc.hb_random_values, &rndval,
3417 sizeof(stcb->asoc.hb_random_values));
3418 this_random = stcb->asoc.hb_random_values[0];
3419 stcb->asoc.hb_random_idx = 0;
3420 stcb->asoc.hb_ect_randombit = 0;
3422 if (stcb->asoc.hb_ect_randombit > 7) {
3423 stcb->asoc.hb_ect_randombit = 0;
3424 stcb->asoc.hb_random_idx++;
3425 if (stcb->asoc.hb_random_idx > 3) {
3429 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3431 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3433 /* ECN Nonce stuff */
3434 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3435 stcb->asoc.hb_ect_randombit++;
3436 return (SCTP_ECT1_BIT);
3438 stcb->asoc.hb_ect_randombit++;
3439 return (SCTP_ECT0_BIT);
3444 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3445 struct sctp_tcb *stcb, /* may be NULL */
3446 struct sctp_nets *net,
3447 struct sockaddr *to,
3449 uint32_t auth_offset,
3450 struct sctp_auth_chunk *auth,
3451 uint16_t auth_keyid,
3452 int nofragment_flag,
3454 struct sctp_tmit_chunk *chk,
3461 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3464 union sctp_sockstore *over_addr
3466 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3469 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3470 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3471 * structure: - fill in the HMAC digest of any AUTH chunk in the
3472 * packet. - calculate and fill in the SCTP checksum. - prepend an
3473 * IP address header. - if boundall use INADDR_ANY. - if
3474 * boundspecific do source address selection. - set fragmentation
3475 * option for ipV4. - On return from IP output, check/adjust mtu
3476 * size of output interface and smallest_mtu size as well.
3478 /* Will need ifdefs around this */
3481 struct sctphdr *sctphdr;
3485 sctp_route_t *ro = NULL;
3486 struct udphdr *udp = NULL;
3488 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3489 struct socket *so = NULL;
3493 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3494 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3499 vrf_id = stcb->asoc.vrf_id;
3501 vrf_id = inp->def_vrf_id;
3504 /* fill in the HMAC digest for any AUTH chunk in the packet */
3505 if ((auth != NULL) && (stcb != NULL)) {
3506 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3508 if (to->sa_family == AF_INET) {
3509 struct ip *ip = NULL;
3510 sctp_route_t iproute;
3514 len = sizeof(struct ip) + sizeof(struct sctphdr);
3516 len += sizeof(struct udphdr);
3518 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3521 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3524 SCTP_ALIGN_TO_END(newm, len);
3525 SCTP_BUF_LEN(newm) = len;
3526 SCTP_BUF_NEXT(newm) = m;
3528 packet_length = sctp_calculate_len(m);
3529 ip = mtod(m, struct ip *);
3530 ip->ip_v = IPVERSION;
3531 ip->ip_hl = (sizeof(struct ip) >> 2);
3533 tos_value = net->tos_flowlabel & 0x000000ff;
3535 tos_value = inp->ip_inp.inp.inp_ip_tos;
3537 if ((nofragment_flag) && (port == 0)) {
3542 /* FreeBSD has a function for ip_id's */
3543 ip->ip_id = ip_newid();
3545 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3546 ip->ip_len = packet_length;
3548 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3550 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3553 ip->ip_tos = (u_char)(tos_value & 0xfc);
3556 /* no association at all */
3557 ip->ip_tos = (tos_value & 0xfc);
3560 ip->ip_p = IPPROTO_UDP;
3562 ip->ip_p = IPPROTO_SCTP;
3567 memset(&iproute, 0, sizeof(iproute));
3568 memcpy(&ro->ro_dst, to, to->sa_len);
3570 ro = (sctp_route_t *) & net->ro;
3572 /* Now the address selection part */
3573 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3575 /* call the routine to select the src address */
3576 if (net && out_of_asoc_ok == 0) {
3577 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3578 sctp_free_ifa(net->ro._s_addr);
3579 net->ro._s_addr = NULL;
3580 net->src_addr_selected = 0;
3586 if (net->src_addr_selected == 0) {
3587 /* Cache the source address */
3588 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3591 net->src_addr_selected = 1;
3593 if (net->ro._s_addr == NULL) {
3594 /* No route to host */
3595 net->src_addr_selected = 0;
3598 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3600 if (over_addr == NULL) {
3601 struct sctp_ifa *_lsrc;
3603 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3607 if (_lsrc == NULL) {
3610 ip->ip_src = _lsrc->address.sin.sin_addr;
3611 sctp_free_ifa(_lsrc);
3613 ip->ip_src = over_addr->sin.sin_addr;
3614 SCTP_RTALLOC(ro, vrf_id);
3618 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3619 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3620 udp->uh_dport = port;
3621 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3622 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3623 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3625 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3628 sctphdr->src_port = src_port;
3629 sctphdr->dest_port = dest_port;
3630 sctphdr->v_tag = v_tag;
3631 sctphdr->checksum = 0;
3634 * If source address selection fails and we find no route
3635 * then the ip_output should fail as well with a
3636 * NO_ROUTE_TO_HOST type error. We probably should catch
3637 * that somewhere and abort the association right away
3638 * (assuming this is an INIT being sent).
3640 if ((ro->ro_rt == NULL)) {
3642 * src addr selection failed to find a route (or
3643 * valid source addr), so we can't get there from
3647 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3648 "%s: dropped packet - no valid source addr\n",
3651 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3652 "Destination was ");
3653 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3654 &net->ro._l_addr.sa);
3655 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3656 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3657 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3658 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3660 SCTP_FAILED_THRESHOLD,
3663 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3664 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3666 * JRS 5/14/07 - If a
3668 * unreachable, the PF bit
3669 * is turned off. This
3670 * allows an unambiguous use
3672 * destinations that are
3673 * reachable but potentially
3675 * destination is set to the
3676 * unreachable state, also
3677 * set the destination to
3681 * Add debug message here if
3682 * destination is not in PF
3686 * Stop any running T3
3689 if ((stcb->asoc.sctp_cmt_on_off == 1) &&
3690 (stcb->asoc.sctp_cmt_pf > 0)) {
3691 net->dest_state &= ~SCTP_ADDR_PF;
3692 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3698 if (net == stcb->asoc.primary_destination) {
3699 /* need a new primary */
3700 struct sctp_nets *alt;
3702 alt = sctp_find_alternate_net(stcb, net, 0);
3704 if (sctp_set_primary_addr(stcb,
3705 (struct sockaddr *)NULL,
3707 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3708 if (net->ro._s_addr) {
3709 sctp_free_ifa(net->ro._s_addr);
3710 net->ro._s_addr = NULL;
3712 net->src_addr_selected = 0;
3718 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3720 return (EHOSTUNREACH);
3722 if (ro != &iproute) {
3723 memcpy(&iproute, ro, sizeof(*ro));
3725 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3726 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3727 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3728 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3729 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3732 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3733 /* failed to prepend data, give up */
3734 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3738 #ifdef SCTP_PACKET_LOGGING
3739 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3740 sctp_packet_log(m, packet_length);
3742 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3744 #if defined(SCTP_WITH_NO_CSUM)
3745 SCTP_STAT_INCR(sctps_sendnocrc);
3747 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3749 (stcb->asoc.loopback_scope))) {
3750 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3751 SCTP_STAT_INCR(sctps_sendswcrc);
3753 SCTP_STAT_INCR(sctps_sendnocrc);
3756 SCTP_ENABLE_UDP_CSUM(o_pak);
3758 #if defined(SCTP_WITH_NO_CSUM)
3759 SCTP_STAT_INCR(sctps_sendnocrc);
3761 m->m_pkthdr.csum_flags = CSUM_SCTP;
3762 m->m_pkthdr.csum_data = 0;
3763 SCTP_STAT_INCR(sctps_sendhwcrc);
3766 /* send it out. table id is taken from stcb */
3767 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3768 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3769 so = SCTP_INP_SO(inp);
3770 SCTP_SOCKET_UNLOCK(so, 0);
3773 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3774 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3775 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3776 atomic_add_int(&stcb->asoc.refcnt, 1);
3777 SCTP_TCB_UNLOCK(stcb);
3778 SCTP_SOCKET_LOCK(so, 0);
3779 SCTP_TCB_LOCK(stcb);
3780 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3783 SCTP_STAT_INCR(sctps_sendpackets);
3784 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3786 SCTP_STAT_INCR(sctps_senderrors);
3788 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3790 /* free tempy routes */
3796 /* PMTU check versus smallest asoc MTU goes here */
3797 if ((ro->ro_rt != NULL) &&
3798 (net->ro._s_addr)) {
3801 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3803 mtu -= sizeof(struct udphdr);
3805 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3806 #ifdef SCTP_PRINT_FOR_B_AND_M
3807 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3809 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3812 } else if (ro->ro_rt == NULL) {
3813 /* route was freed */
3814 if (net->ro._s_addr &&
3815 net->src_addr_selected) {
3816 sctp_free_ifa(net->ro._s_addr);
3817 net->ro._s_addr = NULL;
3819 net->src_addr_selected = 0;
3825 else if (to->sa_family == AF_INET6) {
3827 struct ip6_hdr *ip6h;
3828 struct route_in6 ip6route;
3831 uint16_t flowBottom;
3832 u_char tosBottom, tosTop;
3833 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3835 struct sockaddr_in6 lsa6_storage;
3837 u_short prev_port = 0;
3841 flowlabel = net->tos_flowlabel;
3843 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3846 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3848 len += sizeof(struct udphdr);
3850 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3853 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3856 SCTP_ALIGN_TO_END(newm, len);
3857 SCTP_BUF_LEN(newm) = len;
3858 SCTP_BUF_NEXT(newm) = m;
3860 packet_length = sctp_calculate_len(m);
3862 ip6h = mtod(m, struct ip6_hdr *);
3864 * We assume here that inp_flow is in host byte order within
3867 flowBottom = flowlabel & 0x0000ffff;
3868 flowTop = ((flowlabel & 0x000f0000) >> 16);
3869 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3870 /* protect *sin6 from overwrite */
3871 sin6 = (struct sockaddr_in6 *)to;
3875 /* KAME hack: embed scopeid */
3876 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3877 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3881 memset(&ip6route, 0, sizeof(ip6route));
3882 ro = (sctp_route_t *) & ip6route;
3883 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3885 ro = (sctp_route_t *) & net->ro;
3888 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3890 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3893 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3896 /* we could get no asoc if it is a O-O-T-B packet */
3897 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3899 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3901 ip6h->ip6_nxt = IPPROTO_UDP;
3903 ip6h->ip6_nxt = IPPROTO_SCTP;
3905 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3906 ip6h->ip6_dst = sin6->sin6_addr;
3909 * Add SRC address selection here: we can only reuse to a
3910 * limited degree the kame src-addr-sel, since we can try
3911 * their selection but it may not be bound.
3913 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3914 lsa6_tmp.sin6_family = AF_INET6;
3915 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3917 if (net && out_of_asoc_ok == 0) {
3918 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3919 sctp_free_ifa(net->ro._s_addr);
3920 net->ro._s_addr = NULL;
3921 net->src_addr_selected = 0;
3927 if (net->src_addr_selected == 0) {
3928 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3929 /* KAME hack: embed scopeid */
3930 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3931 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3934 /* Cache the source address */
3935 net->ro._s_addr = sctp_source_address_selection(inp,
3941 (void)sa6_recoverscope(sin6);
3942 net->src_addr_selected = 1;
3944 if (net->ro._s_addr == NULL) {
3945 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3946 net->src_addr_selected = 0;
3949 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3951 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3952 /* KAME hack: embed scopeid */
3953 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3954 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3957 if (over_addr == NULL) {
3958 struct sctp_ifa *_lsrc;
3960 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3964 if (_lsrc == NULL) {
3967 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3968 sctp_free_ifa(_lsrc);
3970 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3971 SCTP_RTALLOC(ro, vrf_id);
3973 (void)sa6_recoverscope(sin6);
3975 lsa6->sin6_port = inp->sctp_lport;
3977 if (ro->ro_rt == NULL) {
3979 * src addr selection failed to find a route (or
3980 * valid source addr), so we can't get there from
3986 * XXX: sa6 may not have a valid sin6_scope_id in the
3987 * non-SCOPEDROUTING case.
3989 bzero(&lsa6_storage, sizeof(lsa6_storage));
3990 lsa6_storage.sin6_family = AF_INET6;
3991 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3992 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3993 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3994 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3999 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4000 lsa6_storage.sin6_port = inp->sctp_lport;
4001 lsa6 = &lsa6_storage;
4002 ip6h->ip6_src = lsa6->sin6_addr;
4005 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4006 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4007 udp->uh_dport = port;
4008 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4010 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4012 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4015 sctphdr->src_port = src_port;
4016 sctphdr->dest_port = dest_port;
4017 sctphdr->v_tag = v_tag;
4018 sctphdr->checksum = 0;
4021 * We set the hop limit now since there is a good chance
4022 * that our ro pointer is now filled
4024 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4025 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4028 /* Copy to be sure something bad is not happening */
4029 sin6->sin6_addr = ip6h->ip6_dst;
4030 lsa6->sin6_addr = ip6h->ip6_src;
4033 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4034 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4035 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4036 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4037 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4039 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4040 /* preserve the port and scope for link local send */
4041 prev_scope = sin6->sin6_scope_id;
4042 prev_port = sin6->sin6_port;
4044 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4045 /* failed to prepend data, give up */
4047 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4050 #ifdef SCTP_PACKET_LOGGING
4051 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4052 sctp_packet_log(m, packet_length);
4054 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4056 #if defined(SCTP_WITH_NO_CSUM)
4057 SCTP_STAT_INCR(sctps_sendnocrc);
4059 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4061 (stcb->asoc.loopback_scope))) {
4062 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4063 SCTP_STAT_INCR(sctps_sendswcrc);
4065 SCTP_STAT_INCR(sctps_sendnocrc);
4068 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4069 udp->uh_sum = 0xffff;
4072 #if defined(SCTP_WITH_NO_CSUM)
4073 SCTP_STAT_INCR(sctps_sendnocrc);
4075 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4077 (stcb->asoc.loopback_scope))) {
4078 m->m_pkthdr.csum_flags = CSUM_SCTP;
4079 m->m_pkthdr.csum_data = 0;
4080 SCTP_STAT_INCR(sctps_sendhwcrc);
4082 SCTP_STAT_INCR(sctps_sendnocrc);
4086 /* send it out. table id is taken from stcb */
4087 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4088 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4089 so = SCTP_INP_SO(inp);
4090 SCTP_SOCKET_UNLOCK(so, 0);
4093 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4094 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4095 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4096 atomic_add_int(&stcb->asoc.refcnt, 1);
4097 SCTP_TCB_UNLOCK(stcb);
4098 SCTP_SOCKET_LOCK(so, 0);
4099 SCTP_TCB_LOCK(stcb);
4100 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4104 /* for link local this must be done */
4105 sin6->sin6_scope_id = prev_scope;
4106 sin6->sin6_port = prev_port;
4108 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4109 SCTP_STAT_INCR(sctps_sendpackets);
4110 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4112 SCTP_STAT_INCR(sctps_senderrors);
4115 /* Now if we had a temp route free it */
4120 /* PMTU check versus smallest asoc MTU goes here */
4121 if (ro->ro_rt == NULL) {
4122 /* Route was freed */
4123 if (net->ro._s_addr &&
4124 net->src_addr_selected) {
4125 sctp_free_ifa(net->ro._s_addr);
4126 net->ro._s_addr = NULL;
4128 net->src_addr_selected = 0;
4130 if ((ro->ro_rt != NULL) &&
4131 (net->ro._s_addr)) {
4134 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4136 (stcb->asoc.smallest_mtu > mtu)) {
4137 #ifdef SCTP_PRINT_FOR_B_AND_M
4138 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4141 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4144 net->mtu -= sizeof(struct udphdr);
4148 if (ND_IFINFO(ifp)->linkmtu &&
4149 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4150 #ifdef SCTP_PRINT_FOR_B_AND_M
4151 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4152 ND_IFINFO(ifp)->linkmtu);
4154 sctp_mtu_size_reset(inp,
4156 ND_IFINFO(ifp)->linkmtu);
4164 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4165 ((struct sockaddr *)to)->sa_family);
4167 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4174 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4175 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4180 struct mbuf *m, *m_at, *mp_last;
4181 struct sctp_nets *net;
4182 struct sctp_init_chunk *init;
4183 struct sctp_supported_addr_param *sup_addr;
4184 struct sctp_adaptation_layer_indication *ali;
4185 struct sctp_ecn_supported_param *ecn;
4186 struct sctp_prsctp_supported_param *prsctp;
4187 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4188 struct sctp_supported_chunk_types_param *pr_supported;
4189 int cnt_inits_to = 0;
4194 /* INIT's always go to the primary (and usually ONLY address) */
4196 net = stcb->asoc.primary_destination;
4198 net = TAILQ_FIRST(&stcb->asoc.nets);
4203 /* we confirm any address we send an INIT to */
4204 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4205 (void)sctp_set_primary_addr(stcb, NULL, net);
4207 /* we confirm any address we send an INIT to */
4208 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4210 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4212 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4214 * special hook, if we are sending to link local it will not
4215 * show up in our private address count.
4217 struct sockaddr_in6 *sin6l;
4219 sin6l = &net->ro._l_addr.sin6;
4220 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4224 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4225 /* This case should not happen */
4226 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4229 /* start the INIT timer */
4230 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4232 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4234 /* No memory, INIT timer will re-attempt. */
4235 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4238 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4240 * assume peer supports asconf in order to be able to queue local
4241 * address changes while an INIT is in flight and before the assoc
4244 stcb->asoc.peer_supports_asconf = 1;
4245 /* Now lets put the SCTP header in place */
4246 init = mtod(m, struct sctp_init_chunk *);
4247 /* now the chunk header */
4248 init->ch.chunk_type = SCTP_INITIATION;
4249 init->ch.chunk_flags = 0;
4250 /* fill in later from mbuf we build */
4251 init->ch.chunk_length = 0;
4252 /* place in my tag */
4253 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4254 /* set up some of the credits. */
4255 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4256 SCTP_MINIMAL_RWND));
4258 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4259 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4260 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4261 /* now the address restriction */
4262 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4264 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4266 /* we support 2 types: IPv6/IPv4 */
4267 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4268 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4269 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4271 /* we support 1 type: IPv4 */
4272 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4273 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4274 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4276 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4277 /* adaptation layer indication parameter */
4278 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4279 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4280 ali->ph.param_length = htons(sizeof(*ali));
4281 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4282 SCTP_BUF_LEN(m) += sizeof(*ali);
4283 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4285 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4286 /* Add NAT friendly parameter */
4287 struct sctp_paramhdr *ph;
4289 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4290 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4291 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4292 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4293 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4295 /* now any cookie time extensions */
4296 if (stcb->asoc.cookie_preserve_req) {
4297 struct sctp_cookie_perserve_param *cookie_preserve;
4299 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4300 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4301 cookie_preserve->ph.param_length = htons(
4302 sizeof(*cookie_preserve));
4303 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4304 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4305 ecn = (struct sctp_ecn_supported_param *)(
4306 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4307 stcb->asoc.cookie_preserve_req = 0;
4310 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4311 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4312 ecn->ph.param_length = htons(sizeof(*ecn));
4313 SCTP_BUF_LEN(m) += sizeof(*ecn);
4314 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4317 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4319 /* And now tell the peer we do pr-sctp */
4320 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4321 prsctp->ph.param_length = htons(sizeof(*prsctp));
4322 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4324 /* And now tell the peer we do all the extensions */
4325 pr_supported = (struct sctp_supported_chunk_types_param *)
4326 ((caddr_t)prsctp + sizeof(*prsctp));
4327 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4329 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4330 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4331 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4332 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4333 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4334 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4335 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4337 if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4338 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4340 p_len = sizeof(*pr_supported) + num_ext;
4341 pr_supported->ph.param_length = htons(p_len);
4342 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4343 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4346 /* ECN nonce: And now tell the peer we support ECN nonce */
4347 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4348 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4349 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4350 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4351 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4352 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4354 /* add authentication parameters */
4355 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4356 struct sctp_auth_random *randp;
4357 struct sctp_auth_hmac_algo *hmacs;
4358 struct sctp_auth_chunk_list *chunks;
4360 /* attach RANDOM parameter, if available */
4361 if (stcb->asoc.authinfo.random != NULL) {
4362 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4363 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4364 /* random key already contains the header */
4365 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4366 /* zero out any padding required */
4367 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4368 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4370 /* add HMAC_ALGO parameter */
4371 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4372 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4373 (uint8_t *) hmacs->hmac_ids);
4375 p_len += sizeof(*hmacs);
4376 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4377 hmacs->ph.param_length = htons(p_len);
4378 /* zero out any padding required */
4379 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4380 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4382 /* add CHUNKS parameter */
4383 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4384 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4385 chunks->chunk_types);
4387 p_len += sizeof(*chunks);
4388 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4389 chunks->ph.param_length = htons(p_len);
4390 /* zero out any padding required */
4391 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4392 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4396 /* now the addresses */
4398 struct sctp_scoping scp;
4401 * To optimize this we could put the scoping stuff into a
4402 * structure and remove the individual uint8's from the
4403 * assoc structure. Then we could just sifa in the address
4404 * within the stcb.. but for now this is a quick hack to get
4405 * the address stuff teased apart.
4407 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4408 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4409 scp.loopback_scope = stcb->asoc.loopback_scope;
4410 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4411 scp.local_scope = stcb->asoc.local_scope;
4412 scp.site_scope = stcb->asoc.site_scope;
4414 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4417 /* calulate the size and update pkt header and chunk header */
4419 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4420 if (SCTP_BUF_NEXT(m_at) == NULL)
4422 p_len += SCTP_BUF_LEN(m_at);
4424 init->ch.chunk_length = htons(p_len);
4426 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4427 * here since the timer will drive a retranmission.
4430 /* I don't expect this to execute but we will be safe here */
4432 if ((padval) && (mp_last)) {
4434 * The compiler worries that mp_last may not be set even
4435 * though I think it is impossible :-> however we add
4436 * mp_last here just in case.
4438 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4440 /* Houston we have a problem, no space */
4446 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4447 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4448 (struct sockaddr *)&net->ro._l_addr,
4449 m, 0, NULL, 0, 0, 0, NULL, 0,
4450 inp->sctp_lport, stcb->rport, htonl(0),
4451 net->port, so_locked, NULL);
4452 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4453 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4454 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4458 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4459 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4462 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4463 * being equal to the beginning of the params i.e. (iphlen +
4464 * sizeof(struct sctp_init_msg) parse through the parameters to the
4465 * end of the mbuf verifying that all parameters are known.
4467 * For unknown parameters build and return a mbuf with
4468 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4469 * processing this chunk stop, and set *abort_processing to 1.
4471 * By having param_offset be pre-set to where parameters begin it is
4472 * hoped that this routine may be reused in the future by new
4475 struct sctp_paramhdr *phdr, params;
4477 struct mbuf *mat, *op_err;
4478 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4479 int at, limit, pad_needed;
4480 uint16_t ptype, plen, padded_size;
4483 *abort_processing = 0;
4486 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4489 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4490 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4491 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4492 ptype = ntohs(phdr->param_type);
4493 plen = ntohs(phdr->param_length);
4494 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4495 /* wacked parameter */
4496 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4499 limit -= SCTP_SIZE32(plen);
4501 * All parameters for all chunks that we know/understand are
4502 * listed here. We process them other places and make
4503 * appropriate stop actions per the upper bits. However this
4504 * is the generic routine processor's can call to get back
4505 * an operr.. to either incorporate (init-ack) or send.
4507 padded_size = SCTP_SIZE32(plen);
4509 /* Param's with variable size */
4510 case SCTP_HEARTBEAT_INFO:
4511 case SCTP_STATE_COOKIE:
4512 case SCTP_UNRECOG_PARAM:
4513 case SCTP_ERROR_CAUSE_IND:
4517 /* Param's with variable size within a range */
4518 case SCTP_CHUNK_LIST:
4519 case SCTP_SUPPORTED_CHUNK_EXT:
4520 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4521 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4526 case SCTP_SUPPORTED_ADDRTYPE:
4527 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4528 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4534 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4535 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4540 case SCTP_SET_PRIM_ADDR:
4541 case SCTP_DEL_IP_ADDRESS:
4542 case SCTP_ADD_IP_ADDRESS:
4543 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4544 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4545 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4550 /* Param's with a fixed size */
4551 case SCTP_IPV4_ADDRESS:
4552 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4553 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4558 case SCTP_IPV6_ADDRESS:
4559 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4560 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4565 case SCTP_COOKIE_PRESERVE:
4566 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4567 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4572 case SCTP_HAS_NAT_SUPPORT:
4575 case SCTP_ECN_NONCE_SUPPORTED:
4576 case SCTP_PRSCTP_SUPPORTED:
4578 if (padded_size != sizeof(struct sctp_paramhdr)) {
4579 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4584 case SCTP_ECN_CAPABLE:
4585 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4586 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4591 case SCTP_ULP_ADAPTATION:
4592 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4593 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4598 case SCTP_SUCCESS_REPORT:
4599 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4600 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4605 case SCTP_HOSTNAME_ADDRESS:
4607 /* We can NOT handle HOST NAME addresses!! */
4610 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4611 *abort_processing = 1;
4612 if (op_err == NULL) {
4613 /* Ok need to try to get a mbuf */
4615 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4617 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4620 l_len += sizeof(struct sctp_paramhdr);
4621 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4623 SCTP_BUF_LEN(op_err) = 0;
4625 * pre-reserve space for ip
4626 * and sctp header and
4630 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4632 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4634 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4635 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4639 /* If we have space */
4640 struct sctp_paramhdr s;
4643 uint32_t cpthis = 0;
4645 pad_needed = 4 - (err_at % 4);
4646 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4647 err_at += pad_needed;
4649 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4650 s.param_length = htons(sizeof(s) + plen);
4651 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4652 err_at += sizeof(s);
4653 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4655 sctp_m_freem(op_err);
4657 * we are out of memory but
4658 * we still need to have a
4659 * look at what to do (the
4660 * system is in trouble
4665 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4673 * we do not recognize the parameter figure out what
4676 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4677 if ((ptype & 0x4000) == 0x4000) {
4678 /* Report bit is set?? */
4679 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4680 if (op_err == NULL) {
4683 /* Ok need to try to get an mbuf */
4685 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4687 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4690 l_len += sizeof(struct sctp_paramhdr);
4691 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4693 SCTP_BUF_LEN(op_err) = 0;
4695 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4697 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4699 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4700 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4704 /* If we have space */
4705 struct sctp_paramhdr s;
4708 uint32_t cpthis = 0;
4710 pad_needed = 4 - (err_at % 4);
4711 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4712 err_at += pad_needed;
4714 s.param_type = htons(SCTP_UNRECOG_PARAM);
4715 s.param_length = htons(sizeof(s) + plen);
4716 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4717 err_at += sizeof(s);
4718 if (plen > sizeof(tempbuf)) {
4719 plen = sizeof(tempbuf);
4721 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4723 sctp_m_freem(op_err);
4725 * we are out of memory but
4726 * we still need to have a
4727 * look at what to do (the
4728 * system is in trouble
4732 goto more_processing;
4734 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4739 if ((ptype & 0x8000) == 0x0000) {
4740 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4743 /* skip this chunk and continue processing */
4744 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4745 at += SCTP_SIZE32(plen);
4750 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4754 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4755 *abort_processing = 1;
4756 if ((op_err == NULL) && phdr) {
4760 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4762 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4764 l_len += (2 * sizeof(struct sctp_paramhdr));
4765 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4767 SCTP_BUF_LEN(op_err) = 0;
4769 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4771 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4773 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4774 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4777 if ((op_err) && phdr) {
4778 struct sctp_paramhdr s;
4781 uint32_t cpthis = 0;
4783 pad_needed = 4 - (err_at % 4);
4784 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4785 err_at += pad_needed;
4787 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4788 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4789 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4790 err_at += sizeof(s);
4791 /* Only copy back the p-hdr that caused the issue */
4792 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4798 sctp_are_there_new_addresses(struct sctp_association *asoc,
4799 struct mbuf *in_initpkt, int iphlen, int offset)
4802 * Given a INIT packet, look through the packet to verify that there
4803 * are NO new addresses. As we go through the parameters add reports
4804 * of any un-understood parameters that require an error. Also we
4805 * must return (1) to drop the packet if we see a un-understood
4806 * parameter that tells us to drop the chunk.
4808 struct sockaddr_in sin4, *sa4;
4811 struct sockaddr_in6 sin6, *sa6;
4814 struct sockaddr *sa_touse;
4815 struct sockaddr *sa;
4816 struct sctp_paramhdr *phdr, params;
4820 struct ip6_hdr *ip6h;
4824 uint16_t ptype, plen;
4827 struct sctp_nets *net;
4829 memset(&sin4, 0, sizeof(sin4));
4831 memset(&sin6, 0, sizeof(sin6));
4833 sin4.sin_family = AF_INET;
4834 sin4.sin_len = sizeof(sin4);
4836 sin6.sin6_family = AF_INET6;
4837 sin6.sin6_len = sizeof(sin6);
4840 /* First what about the src address of the pkt ? */
4841 iph = mtod(in_initpkt, struct ip *);
4842 switch (iph->ip_v) {
4844 /* source addr is IPv4 */
4845 sin4.sin_addr = iph->ip_src;
4846 sa_touse = (struct sockaddr *)&sin4;
4849 case IPV6_VERSION >> 4:
4850 /* source addr is IPv6 */
4851 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4852 sin6.sin6_addr = ip6h->ip6_src;
4853 sa_touse = (struct sockaddr *)&sin6;
4861 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4862 sa = (struct sockaddr *)&net->ro._l_addr;
4863 if (sa->sa_family == sa_touse->sa_family) {
4864 if (sa->sa_family == AF_INET) {
4865 sa4 = (struct sockaddr_in *)sa;
4866 if (sa4->sin_addr.s_addr ==
4867 sin4.sin_addr.s_addr) {
4873 if (sa->sa_family == AF_INET6) {
4874 sa6 = (struct sockaddr_in6 *)sa;
4875 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4885 /* New address added! no need to look futher. */
4888 /* Ok so far lets munge through the rest of the packet */
4892 offset += sizeof(struct sctp_init_chunk);
4893 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4895 ptype = ntohs(phdr->param_type);
4896 plen = ntohs(phdr->param_length);
4897 if (ptype == SCTP_IPV4_ADDRESS) {
4898 struct sctp_ipv4addr_param *p4, p4_buf;
4900 phdr = sctp_get_next_param(mat, offset,
4901 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4902 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4906 p4 = (struct sctp_ipv4addr_param *)phdr;
4907 sin4.sin_addr.s_addr = p4->addr;
4908 sa_touse = (struct sockaddr *)&sin4;
4909 } else if (ptype == SCTP_IPV6_ADDRESS) {
4910 struct sctp_ipv6addr_param *p6, p6_buf;
4912 phdr = sctp_get_next_param(mat, offset,
4913 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4914 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4918 p6 = (struct sctp_ipv6addr_param *)phdr;
4920 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4923 sa_touse = (struct sockaddr *)&sin4;
4926 /* ok, sa_touse points to one to check */
4928 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4929 sa = (struct sockaddr *)&net->ro._l_addr;
4930 if (sa->sa_family != sa_touse->sa_family) {
4933 if (sa->sa_family == AF_INET) {
4934 sa4 = (struct sockaddr_in *)sa;
4935 if (sa4->sin_addr.s_addr ==
4936 sin4.sin_addr.s_addr) {
4942 if (sa->sa_family == AF_INET6) {
4943 sa6 = (struct sockaddr_in6 *)sa;
4944 if (SCTP6_ARE_ADDR_EQUAL(
4953 /* New addr added! no need to look further */
4957 offset += SCTP_SIZE32(plen);
4958 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4964 * Given a MBUF chain that was sent into us containing an INIT. Build a
4965 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4966 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4967 * message (i.e. the struct sctp_init_msg).
4970 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4971 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4972 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4974 struct sctp_association *asoc;
4975 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4976 struct sctp_init_ack_chunk *initack;
4977 struct sctp_adaptation_layer_indication *ali;
4978 struct sctp_ecn_supported_param *ecn;
4979 struct sctp_prsctp_supported_param *prsctp;
4980 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4981 struct sctp_supported_chunk_types_param *pr_supported;
4982 union sctp_sockstore store, store1, *over_addr;
4983 struct sockaddr_in *sin, *to_sin;
4986 struct sockaddr_in6 *sin6, *to_sin6;
4992 struct ip6_hdr *ip6;
4995 struct sockaddr *to;
4996 struct sctp_state_cookie stc;
4997 struct sctp_nets *net = NULL;
4998 uint8_t *signature = NULL;
4999 int cnt_inits_to = 0;
5000 uint16_t his_limit, i_want;
5001 int abort_flag, padval;
5004 int nat_friendly = 0;
5012 if ((asoc != NULL) &&
5013 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5014 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
5015 /* new addresses, out of here in non-cookie-wait states */
5017 * Send a ABORT, we don't add the new address error clause
5018 * though we even set the T bit and copy in the 0 tag.. this
5019 * looks no different than if no listener was present.
5021 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5025 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5026 (offset + sizeof(struct sctp_init_chunk)),
5027 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5030 sctp_send_abort(init_pkt, iphlen, sh,
5031 init_chk->init.initiate_tag, op_err, vrf_id, port);
5034 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5036 /* No memory, INIT timer will re-attempt. */
5038 sctp_m_freem(op_err);
5041 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5043 /* the time I built cookie */
5044 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5046 /* populate any tie tags */
5048 /* unlock before tag selections */
5049 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5050 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5051 stc.cookie_life = asoc->cookie_life;
5052 net = asoc->primary_destination;
5054 stc.tie_tag_my_vtag = 0;
5055 stc.tie_tag_peer_vtag = 0;
5056 /* life I will award this cookie */
5057 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5060 /* copy in the ports for later check */
5061 stc.myport = sh->dest_port;
5062 stc.peerport = sh->src_port;
5065 * If we wanted to honor cookie life extentions, we would add to
5066 * stc.cookie_life. For now we should NOT honor any extension
5068 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5069 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5070 struct inpcb *in_inp;
5072 /* Its a V6 socket */
5073 in_inp = (struct inpcb *)inp;
5074 stc.ipv6_addr_legal = 1;
5075 /* Now look at the binding flag to see if V4 will be legal */
5076 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5077 stc.ipv4_addr_legal = 1;
5079 /* V4 addresses are NOT legal on the association */
5080 stc.ipv4_addr_legal = 0;
5083 /* Its a V4 socket, no - V6 */
5084 stc.ipv4_addr_legal = 1;
5085 stc.ipv6_addr_legal = 0;
5088 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5093 /* now for scope setup */
5094 memset((caddr_t)&store, 0, sizeof(store));
5095 memset((caddr_t)&store1, 0, sizeof(store1));
5097 to_sin = &store1.sin;
5100 to_sin6 = &store1.sin6;
5102 iph = mtod(init_pkt, struct ip *);
5103 /* establish the to_addr's */
5104 switch (iph->ip_v) {
5106 to_sin->sin_port = sh->dest_port;
5107 to_sin->sin_family = AF_INET;
5108 to_sin->sin_len = sizeof(struct sockaddr_in);
5109 to_sin->sin_addr = iph->ip_dst;
5112 case IPV6_VERSION >> 4:
5113 ip6 = mtod(init_pkt, struct ip6_hdr *);
5114 to_sin6->sin6_addr = ip6->ip6_dst;
5115 to_sin6->sin6_scope_id = 0;
5116 to_sin6->sin6_port = sh->dest_port;
5117 to_sin6->sin6_family = AF_INET6;
5118 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5127 to = (struct sockaddr *)&store;
5128 switch (iph->ip_v) {
5131 sin->sin_family = AF_INET;
5132 sin->sin_len = sizeof(struct sockaddr_in);
5133 sin->sin_port = sh->src_port;
5134 sin->sin_addr = iph->ip_src;
5135 /* lookup address */
5136 stc.address[0] = sin->sin_addr.s_addr;
5140 stc.addr_type = SCTP_IPV4_ADDRESS;
5141 /* local from address */
5142 stc.laddress[0] = to_sin->sin_addr.s_addr;
5143 stc.laddress[1] = 0;
5144 stc.laddress[2] = 0;
5145 stc.laddress[3] = 0;
5146 stc.laddr_type = SCTP_IPV4_ADDRESS;
5147 /* scope_id is only for v6 */
5149 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5150 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5155 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5156 /* Must use the address in this case */
5157 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5158 stc.loopback_scope = 1;
5161 stc.local_scope = 0;
5166 case IPV6_VERSION >> 4:
5168 ip6 = mtod(init_pkt, struct ip6_hdr *);
5169 sin6->sin6_family = AF_INET6;
5170 sin6->sin6_len = sizeof(struct sockaddr_in6);
5171 sin6->sin6_port = sh->src_port;
5172 sin6->sin6_addr = ip6->ip6_src;
5173 /* lookup address */
5174 memcpy(&stc.address, &sin6->sin6_addr,
5175 sizeof(struct in6_addr));
5176 sin6->sin6_scope_id = 0;
5177 stc.addr_type = SCTP_IPV6_ADDRESS;
5179 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5181 * FIX ME: does this have scope from
5184 (void)sa6_recoverscope(sin6);
5185 stc.scope_id = sin6->sin6_scope_id;
5186 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5187 stc.loopback_scope = 1;
5188 stc.local_scope = 0;
5191 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5193 * If the new destination is a
5194 * LINK_LOCAL we must have common
5195 * both site and local scope. Don't
5196 * set local scope though since we
5197 * must depend on the source to be
5198 * added implicitly. We cannot
5199 * assure just because we share one
5200 * link that all links are common.
5202 stc.local_scope = 0;
5206 * we start counting for the private
5207 * address stuff at 1. since the
5208 * link local we source from won't
5209 * show up in our scoped count.
5213 * pull out the scope_id from
5217 * FIX ME: does this have scope from
5220 (void)sa6_recoverscope(sin6);
5221 stc.scope_id = sin6->sin6_scope_id;
5222 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5223 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5225 * If the new destination is
5226 * SITE_LOCAL then we must have site
5231 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5232 stc.laddr_type = SCTP_IPV6_ADDRESS;
5242 /* set the scope per the existing tcb */
5245 struct sctp_nets *lnet;
5249 stc.loopback_scope = asoc->loopback_scope;
5250 stc.ipv4_scope = asoc->ipv4_local_scope;
5251 stc.site_scope = asoc->site_scope;
5252 stc.local_scope = asoc->local_scope;
5254 /* Why do we not consider IPv4 LL addresses? */
5255 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5256 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5257 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5259 * if we have a LL address, start
5267 /* use the net pointer */
5268 to = (struct sockaddr *)&net->ro._l_addr;
5269 switch (to->sa_family) {
5271 sin = (struct sockaddr_in *)to;
5272 stc.address[0] = sin->sin_addr.s_addr;
5276 stc.addr_type = SCTP_IPV4_ADDRESS;
5277 if (net->src_addr_selected == 0) {
5279 * strange case here, the INIT should have
5280 * did the selection.
5282 net->ro._s_addr = sctp_source_address_selection(inp,
5283 stcb, (sctp_route_t *) & net->ro,
5285 if (net->ro._s_addr == NULL)
5288 net->src_addr_selected = 1;
5291 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5292 stc.laddress[1] = 0;
5293 stc.laddress[2] = 0;
5294 stc.laddress[3] = 0;
5295 stc.laddr_type = SCTP_IPV4_ADDRESS;
5299 sin6 = (struct sockaddr_in6 *)to;
5300 memcpy(&stc.address, &sin6->sin6_addr,
5301 sizeof(struct in6_addr));
5302 stc.addr_type = SCTP_IPV6_ADDRESS;
5303 if (net->src_addr_selected == 0) {
5305 * strange case here, the INIT should have
5306 * did the selection.
5308 net->ro._s_addr = sctp_source_address_selection(inp,
5309 stcb, (sctp_route_t *) & net->ro,
5311 if (net->ro._s_addr == NULL)
5314 net->src_addr_selected = 1;
5316 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5317 sizeof(struct in6_addr));
5318 stc.laddr_type = SCTP_IPV6_ADDRESS;
5323 /* Now lets put the SCTP header in place */
5324 initack = mtod(m, struct sctp_init_ack_chunk *);
5325 /* Save it off for quick ref */
5326 stc.peers_vtag = init_chk->init.initiate_tag;
5328 memcpy(stc.identification, SCTP_VERSION_STRING,
5329 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5330 /* now the chunk header */
5331 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5332 initack->ch.chunk_flags = 0;
5333 /* fill in later from mbuf we build */
5334 initack->ch.chunk_length = 0;
5335 /* place in my tag */
5336 if ((asoc != NULL) &&
5337 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5338 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5339 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5340 /* re-use the v-tags and init-seq here */
5341 initack->init.initiate_tag = htonl(asoc->my_vtag);
5342 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5344 uint32_t vtag, itsn;
5346 if (hold_inp_lock) {
5347 SCTP_INP_INCR_REF(inp);
5348 SCTP_INP_RUNLOCK(inp);
5351 atomic_add_int(&asoc->refcnt, 1);
5352 SCTP_TCB_UNLOCK(stcb);
5354 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5355 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5357 * Got a duplicate vtag on some guy behind a
5358 * nat make sure we don't use it.
5362 initack->init.initiate_tag = htonl(vtag);
5363 /* get a TSN to use too */
5364 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5365 initack->init.initial_tsn = htonl(itsn);
5366 SCTP_TCB_LOCK(stcb);
5367 atomic_add_int(&asoc->refcnt, -1);
5369 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5370 initack->init.initiate_tag = htonl(vtag);
5371 /* get a TSN to use too */
5372 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5374 if (hold_inp_lock) {
5375 SCTP_INP_RLOCK(inp);
5376 SCTP_INP_DECR_REF(inp);
5379 /* save away my tag to */
5380 stc.my_vtag = initack->init.initiate_tag;
5382 /* set up some of the credits. */
5383 so = inp->sctp_socket;
5385 /* memory problem */
5389 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5391 /* set what I want */
5392 his_limit = ntohs(init_chk->init.num_inbound_streams);
5393 /* choose what I want */
5395 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5396 i_want = asoc->streamoutcnt;
5398 i_want = inp->sctp_ep.pre_open_stream_count;
5401 i_want = inp->sctp_ep.pre_open_stream_count;
5403 if (his_limit < i_want) {
5404 /* I Want more :< */
5405 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5407 /* I can have what I want :> */
5408 initack->init.num_outbound_streams = htons(i_want);
5410 /* tell him his limt. */
5411 initack->init.num_inbound_streams =
5412 htons(inp->sctp_ep.max_open_streams_intome);
5414 /* adaptation layer indication parameter */
5415 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5416 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5417 ali->ph.param_length = htons(sizeof(*ali));
5418 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5419 SCTP_BUF_LEN(m) += sizeof(*ali);
5420 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5423 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5424 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5425 ecn->ph.param_length = htons(sizeof(*ecn));
5426 SCTP_BUF_LEN(m) += sizeof(*ecn);
5428 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5431 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5433 /* And now tell the peer we do pr-sctp */
5434 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5435 prsctp->ph.param_length = htons(sizeof(*prsctp));
5436 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5438 /* Add NAT friendly parameter */
5439 struct sctp_paramhdr *ph;
5441 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5442 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5443 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5444 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5446 /* And now tell the peer we do all the extensions */
5447 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5448 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5450 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5451 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5452 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5453 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5454 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5455 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5456 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5457 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5458 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5459 p_len = sizeof(*pr_supported) + num_ext;
5460 pr_supported->ph.param_length = htons(p_len);
5461 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5462 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5464 /* ECN nonce: And now tell the peer we support ECN nonce */
5465 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5466 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5467 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5468 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5469 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5470 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5472 /* add authentication parameters */
5473 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5474 struct sctp_auth_random *randp;
5475 struct sctp_auth_hmac_algo *hmacs;
5476 struct sctp_auth_chunk_list *chunks;
5477 uint16_t random_len;
5479 /* generate and add RANDOM parameter */
5480 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5481 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5482 randp->ph.param_type = htons(SCTP_RANDOM);
5483 p_len = sizeof(*randp) + random_len;
5484 randp->ph.param_length = htons(p_len);
5485 SCTP_READ_RANDOM(randp->random_data, random_len);
5486 /* zero out any padding required */
5487 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5488 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5490 /* add HMAC_ALGO parameter */
5491 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5492 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5493 (uint8_t *) hmacs->hmac_ids);
5495 p_len += sizeof(*hmacs);
5496 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5497 hmacs->ph.param_length = htons(p_len);
5498 /* zero out any padding required */
5499 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5500 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5502 /* add CHUNKS parameter */
5503 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5504 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5505 chunks->chunk_types);
5507 p_len += sizeof(*chunks);
5508 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5509 chunks->ph.param_length = htons(p_len);
5510 /* zero out any padding required */
5511 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5512 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5516 /* now the addresses */
5518 struct sctp_scoping scp;
5521 * To optimize this we could put the scoping stuff into a
5522 * structure and remove the individual uint8's from the stc
5523 * structure. Then we could just sifa in the address within
5524 * the stc.. but for now this is a quick hack to get the
5525 * address stuff teased apart.
5527 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5528 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5529 scp.loopback_scope = stc.loopback_scope;
5530 scp.ipv4_local_scope = stc.ipv4_scope;
5531 scp.local_scope = stc.local_scope;
5532 scp.site_scope = stc.site_scope;
5533 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5536 /* tack on the operational error if present */
5544 llen += SCTP_BUF_LEN(ol);
5545 ol = SCTP_BUF_NEXT(ol);
5548 /* must add a pad to the param */
5549 uint32_t cpthis = 0;
5552 padlen = 4 - (llen % 4);
5553 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5555 while (SCTP_BUF_NEXT(m_at) != NULL) {
5556 m_at = SCTP_BUF_NEXT(m_at);
5558 SCTP_BUF_NEXT(m_at) = op_err;
5559 while (SCTP_BUF_NEXT(m_at) != NULL) {
5560 m_at = SCTP_BUF_NEXT(m_at);
5563 /* pre-calulate the size and update pkt header and chunk header */
5565 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5566 p_len += SCTP_BUF_LEN(m_tmp);
5567 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5568 /* m_tmp should now point to last one */
5573 /* Now we must build a cookie */
5574 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5575 if (m_cookie == NULL) {
5576 /* memory problem */
5580 /* Now append the cookie to the end and update the space/size */
5581 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5583 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5584 p_len += SCTP_BUF_LEN(m_tmp);
5585 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5586 /* m_tmp should now point to last one */
5592 * Place in the size, but we don't include the last pad (if any) in
5595 initack->ch.chunk_length = htons(p_len);
5598 * Time to sign the cookie, we don't sign over the cookie signature
5599 * though thus we set trailer.
5601 (void)sctp_hmac_m(SCTP_HMAC,
5602 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5603 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5604 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5606 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5607 * here since the timer will drive a retranmission.
5610 if ((padval) && (mp_last)) {
5611 /* see my previous comments on mp_last */
5614 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5616 /* Houston we have a problem, no space */
5622 if (stc.loopback_scope) {
5623 over_addr = &store1;
5628 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5630 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5631 port, SCTP_SO_NOT_LOCKED, over_addr);
5632 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5637 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5638 struct sctp_association *asoc,
5639 struct sctp_stream_out *strq, int holds_lock)
5641 if (holds_lock == 0) {
5642 SCTP_TCB_SEND_LOCK(stcb);
5644 if ((strq->next_spoke.tqe_next == NULL) &&
5645 (strq->next_spoke.tqe_prev == NULL)) {
5646 TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5648 if (holds_lock == 0) {
5649 SCTP_TCB_SEND_UNLOCK(stcb);
5654 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5655 struct sctp_association *asoc,
5656 struct sctp_stream_out *strq,
5659 /* take off and then setup so we know it is not on the wheel */
5660 if (holds_lock == 0) {
5661 SCTP_TCB_SEND_LOCK(stcb);
5663 if (TAILQ_EMPTY(&strq->outqueue)) {
5664 if (asoc->last_out_stream == strq) {
5665 asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5666 if (asoc->last_out_stream == NULL) {
5667 asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5669 if (asoc->last_out_stream == strq) {
5670 asoc->last_out_stream = NULL;
5673 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5674 strq->next_spoke.tqe_next = NULL;
5675 strq->next_spoke.tqe_prev = NULL;
5677 if (holds_lock == 0) {
5678 SCTP_TCB_SEND_UNLOCK(stcb);
5683 sctp_prune_prsctp(struct sctp_tcb *stcb,
5684 struct sctp_association *asoc,
5685 struct sctp_sndrcvinfo *srcv,
5689 struct sctp_tmit_chunk *chk, *nchk;
5691 SCTP_TCB_LOCK_ASSERT(stcb);
5692 if ((asoc->peer_supports_prsctp) &&
5693 (asoc->sent_queue_cnt_removeable > 0)) {
5694 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5696 * Look for chunks marked with the PR_SCTP flag AND
5697 * the buffer space flag. If the one being sent is
5698 * equal or greater priority then purge the old one
5699 * and free some space.
5701 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5703 * This one is PR-SCTP AND buffer space
5706 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5708 * Lower numbers equates to higher
5709 * priority so if the one we are
5710 * looking at has a larger or equal
5711 * priority we want to drop the data
5712 * and NOT retransmit it.
5716 * We release the book_size
5717 * if the mbuf is here
5722 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5723 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5725 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5726 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5729 freed_spc += ret_spc;
5730 if (freed_spc >= dataout) {
5733 } /* if chunk was present */
5734 } /* if of sufficent priority */
5735 } /* if chunk has enabled */
5736 } /* tailqforeach */
5738 chk = TAILQ_FIRST(&asoc->send_queue);
5740 nchk = TAILQ_NEXT(chk, sctp_next);
5741 /* Here we must move to the sent queue and mark */
5742 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5743 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5746 * We release the book_size
5747 * if the mbuf is here
5751 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5752 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5755 freed_spc += ret_spc;
5756 if (freed_spc >= dataout) {
5759 } /* end if chk->data */
5760 } /* end if right class */
5761 } /* end if chk pr-sctp */
5763 } /* end while (chk) */
5764 } /* if enabled in asoc */
5768 sctp_get_frag_point(struct sctp_tcb *stcb,
5769 struct sctp_association *asoc)
5774 * For endpoints that have both v6 and v4 addresses we must reserve
5775 * room for the ipv6 header, for those that are only dealing with V4
5776 * we use a larger frag point.
5778 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5779 ovh = SCTP_MED_OVERHEAD;
5781 ovh = SCTP_MED_V4_OVERHEAD;
5784 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5785 siz = asoc->smallest_mtu - ovh;
5787 siz = (stcb->asoc.sctp_frag_point - ovh);
5789 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5791 /* A data chunk MUST fit in a cluster */
5792 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5795 /* adjust for an AUTH chunk if DATA requires auth */
5796 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5797 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5800 /* make it an even word boundary please */
5807 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5811 * We assume that the user wants PR_SCTP_TTL if the user provides a
5812 * positive lifetime but does not specify any PR_SCTP policy. This
5813 * is a BAD assumption and causes problems at least with the
5814 * U-Vancovers MPI folks. I will change this to be no policy means
5817 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5818 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5823 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5824 case CHUNK_FLAGS_PR_SCTP_BUF:
5826 * Time to live is a priority stored in tv_sec when doing
5827 * the buffer drop thing.
5829 sp->ts.tv_sec = sp->timetolive;
5832 case CHUNK_FLAGS_PR_SCTP_TTL:
5836 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5837 tv.tv_sec = sp->timetolive / 1000;
5838 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5840 * TODO sctp_constants.h needs alternative time
5841 * macros when _KERNEL is undefined.
5843 timevaladd(&sp->ts, &tv);
5846 case CHUNK_FLAGS_PR_SCTP_RTX:
5848 * Time to live is a the number or retransmissions stored in
5851 sp->ts.tv_sec = sp->timetolive;
5855 SCTPDBG(SCTP_DEBUG_USRREQ1,
5856 "Unknown PR_SCTP policy %u.\n",
5857 PR_SCTP_POLICY(sp->sinfo_flags));
5863 sctp_msg_append(struct sctp_tcb *stcb,
5864 struct sctp_nets *net,
5866 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5868 int error = 0, holds_lock;
5870 struct sctp_stream_queue_pending *sp = NULL;
5871 struct sctp_stream_out *strm;
5874 * Given an mbuf chain, put it into the association send queue and
5875 * place it on the wheel
5877 holds_lock = hold_stcb_lock;
5878 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5879 /* Invalid stream number */
5880 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5884 if ((stcb->asoc.stream_locked) &&
5885 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5886 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5890 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5891 /* Now can we send this? */
5892 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5893 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5894 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5895 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5896 /* got data while shutting down */
5897 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5901 sctp_alloc_a_strmoq(stcb, sp);
5903 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5907 sp->sinfo_flags = srcv->sinfo_flags;
5908 sp->timetolive = srcv->sinfo_timetolive;
5909 sp->ppid = srcv->sinfo_ppid;
5910 sp->context = srcv->sinfo_context;
5912 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5915 sp->net = stcb->asoc.primary_destination;
5917 atomic_add_int(&sp->net->ref_count, 1);
5918 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5919 sp->stream = srcv->sinfo_stream;
5920 sp->msg_is_complete = 1;
5921 sp->sender_all_done = 1;
5924 sp->tail_mbuf = NULL;
5927 sctp_set_prsctp_policy(sp);
5929 * We could in theory (for sendall) sifa the length in, but we would
5930 * still have to hunt through the chain since we need to setup the
5934 if (SCTP_BUF_NEXT(at) == NULL)
5936 sp->length += SCTP_BUF_LEN(at);
5937 at = SCTP_BUF_NEXT(at);
5939 SCTP_TCB_SEND_LOCK(stcb);
5940 sctp_snd_sb_alloc(stcb, sp->length);
5941 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5942 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5943 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5944 sp->strseq = strm->next_sequence_sent;
5945 strm->next_sequence_sent++;
5947 if ((strm->next_spoke.tqe_next == NULL) &&
5948 (strm->next_spoke.tqe_prev == NULL)) {
5949 /* Not on wheel, insert */
5950 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5953 SCTP_TCB_SEND_UNLOCK(stcb);
5962 static struct mbuf *
5963 sctp_copy_mbufchain(struct mbuf *clonechain,
5964 struct mbuf *outchain,
5965 struct mbuf **endofchain,
5968 uint8_t copy_by_ref)
5971 struct mbuf *appendchain;
5975 if (endofchain == NULL) {
5979 sctp_m_freem(outchain);
5982 if (can_take_mbuf) {
5983 appendchain = clonechain;
5986 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5988 /* Its not in a cluster */
5989 if (*endofchain == NULL) {
5990 /* lets get a mbuf cluster */
5991 if (outchain == NULL) {
5992 /* This is the general case */
5994 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5995 if (outchain == NULL) {
5998 SCTP_BUF_LEN(outchain) = 0;
5999 *endofchain = outchain;
6000 /* get the prepend space */
6001 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6004 * We really should not get a NULL
6010 if (SCTP_BUF_NEXT(m) == NULL) {
6014 m = SCTP_BUF_NEXT(m);
6017 if (*endofchain == NULL) {
6019 * huh, TSNH XXX maybe we
6022 sctp_m_freem(outchain);
6026 /* get the new end of length */
6027 len = M_TRAILINGSPACE(*endofchain);
6029 /* how much is left at the end? */
6030 len = M_TRAILINGSPACE(*endofchain);
6032 /* Find the end of the data, for appending */
6033 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6035 /* Now lets copy it out */
6036 if (len >= sizeofcpy) {
6037 /* It all fits, copy it in */
6038 m_copydata(clonechain, 0, sizeofcpy, cp);
6039 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6041 /* fill up the end of the chain */
6043 m_copydata(clonechain, 0, len, cp);
6044 SCTP_BUF_LEN((*endofchain)) += len;
6045 /* now we need another one */
6048 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6053 SCTP_BUF_NEXT((*endofchain)) = m;
6055 cp = mtod((*endofchain), caddr_t);
6056 m_copydata(clonechain, len, sizeofcpy, cp);
6057 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6061 /* copy the old fashion way */
6062 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6063 #ifdef SCTP_MBUF_LOGGING
6064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6069 if (SCTP_BUF_IS_EXTENDED(mat)) {
6070 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6072 mat = SCTP_BUF_NEXT(mat);
6078 if (appendchain == NULL) {
6081 sctp_m_freem(outchain);
6085 /* tack on to the end */
6086 if (*endofchain != NULL) {
6087 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6091 if (SCTP_BUF_NEXT(m) == NULL) {
6092 SCTP_BUF_NEXT(m) = appendchain;
6095 m = SCTP_BUF_NEXT(m);
6099 * save off the end and update the end-chain postion
6103 if (SCTP_BUF_NEXT(m) == NULL) {
6107 m = SCTP_BUF_NEXT(m);
6111 /* save off the end and update the end-chain postion */
6114 if (SCTP_BUF_NEXT(m) == NULL) {
6118 m = SCTP_BUF_NEXT(m);
6120 return (appendchain);
6125 sctp_med_chunk_output(struct sctp_inpcb *inp,
6126 struct sctp_tcb *stcb,
6127 struct sctp_association *asoc,
6130 int control_only, int from_where,
6131 struct timeval *now, int *now_filled, int frag_point, int so_locked
6132 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6138 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6141 struct sctp_copy_all *ca;
6144 int added_control = 0;
6145 int un_sent, do_chunk_output = 1;
6146 struct sctp_association *asoc;
6148 ca = (struct sctp_copy_all *)ptr;
6149 if (ca->m == NULL) {
6152 if (ca->inp != inp) {
6156 if ((ca->m) && ca->sndlen) {
6157 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6159 /* can't copy so we are done */
6163 #ifdef SCTP_MBUF_LOGGING
6164 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6169 if (SCTP_BUF_IS_EXTENDED(mat)) {
6170 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6172 mat = SCTP_BUF_NEXT(mat);
6179 SCTP_TCB_LOCK_ASSERT(stcb);
6180 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6181 /* Abort this assoc with m as the user defined reason */
6183 struct sctp_paramhdr *ph;
6185 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6187 ph = mtod(m, struct sctp_paramhdr *);
6188 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6189 ph->param_length = htons(ca->sndlen);
6192 * We add one here to keep the assoc from
6193 * dis-appearing on us.
6195 atomic_add_int(&stcb->asoc.refcnt, 1);
6196 sctp_abort_an_association(inp, stcb,
6197 SCTP_RESPONSE_TO_USER_REQ,
6198 m, SCTP_SO_NOT_LOCKED);
6200 * sctp_abort_an_association calls sctp_free_asoc()
6201 * free association will NOT free it since we
6202 * incremented the refcnt .. we do this to prevent
6203 * it being freed and things getting tricky since we
6204 * could end up (from free_asoc) calling inpcb_free
6205 * which would get a recursive lock call to the
6206 * iterator lock.. But as a consequence of that the
6207 * stcb will return to us un-locked.. since
6208 * free_asoc returns with either no TCB or the TCB
6209 * unlocked, we must relock.. to unlock in the
6210 * iterator timer :-0
6212 SCTP_TCB_LOCK(stcb);
6213 atomic_add_int(&stcb->asoc.refcnt, -1);
6214 goto no_chunk_output;
6218 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6222 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6223 /* shutdown this assoc */
6226 cnt = sctp_is_there_unsent_data(stcb);
6228 if (TAILQ_EMPTY(&asoc->send_queue) &&
6229 TAILQ_EMPTY(&asoc->sent_queue) &&
6231 if (asoc->locked_on_sending) {
6235 * there is nothing queued to send, so I'm
6238 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6239 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6240 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6242 * only send SHUTDOWN the first time
6245 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6246 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6247 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6249 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6250 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6251 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6252 asoc->primary_destination);
6253 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6254 asoc->primary_destination);
6256 do_chunk_output = 0;
6260 * we still got (or just got) data to send,
6261 * so set SHUTDOWN_PENDING
6264 * XXX sockets draft says that SCTP_EOF
6265 * should be sent with no data. currently,
6266 * we will allow user data to be sent first
6267 * and move to SHUTDOWN-PENDING
6269 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6270 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6271 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6272 if (asoc->locked_on_sending) {
6274 * Locked to send out the
6277 struct sctp_stream_queue_pending *sp;
6279 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6281 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6282 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6285 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6286 if (TAILQ_EMPTY(&asoc->send_queue) &&
6287 TAILQ_EMPTY(&asoc->sent_queue) &&
6288 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6290 atomic_add_int(&stcb->asoc.refcnt, 1);
6291 sctp_abort_an_association(stcb->sctp_ep, stcb,
6292 SCTP_RESPONSE_TO_USER_REQ,
6293 NULL, SCTP_SO_NOT_LOCKED);
6294 atomic_add_int(&stcb->asoc.refcnt, -1);
6295 goto no_chunk_output;
6297 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6298 asoc->primary_destination);
6304 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6305 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6307 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6308 (stcb->asoc.total_flight > 0) &&
6309 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6311 do_chunk_output = 0;
6313 if (do_chunk_output)
6314 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6315 else if (added_control) {
6316 int num_out = 0, reason = 0, now_filled = 0;
6320 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6321 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6322 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6333 sctp_sendall_completes(void *ptr, uint32_t val)
6335 struct sctp_copy_all *ca;
6337 ca = (struct sctp_copy_all *)ptr;
6339 * Do a notify here? Kacheong suggests that the notify be done at
6340 * the send time.. so you would push up a notification if any send
6341 * failed. Don't know if this is feasable since the only failures we
6342 * have is "memory" related and if you cannot get an mbuf to send
6343 * the data you surely can't get an mbuf to send up to notify the
6344 * user you can't send the data :->
6347 /* now free everything */
6348 sctp_m_freem(ca->m);
6349 SCTP_FREE(ca, SCTP_M_COPYAL);
6353 #define MC_ALIGN(m, len) do { \
6354 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6359 static struct mbuf *
6360 sctp_copy_out_all(struct uio *uio, int len)
6362 struct mbuf *ret, *at;
6363 int left, willcpy, cancpy, error;
6365 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6371 SCTP_BUF_LEN(ret) = 0;
6372 /* save space for the data chunk header */
6373 cancpy = M_TRAILINGSPACE(ret);
6374 willcpy = min(cancpy, left);
6377 /* Align data to the end */
6378 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6384 SCTP_BUF_LEN(at) = willcpy;
6385 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6388 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6389 if (SCTP_BUF_NEXT(at) == NULL) {
6392 at = SCTP_BUF_NEXT(at);
6393 SCTP_BUF_LEN(at) = 0;
6394 cancpy = M_TRAILINGSPACE(at);
6395 willcpy = min(cancpy, left);
6402 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6403 struct sctp_sndrcvinfo *srcv)
6406 struct sctp_copy_all *ca;
6408 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6412 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6415 memset(ca, 0, sizeof(struct sctp_copy_all));
6418 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6420 * take off the sendall flag, it would be bad if we failed to do
6423 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6424 /* get length and mbuf chain */
6426 ca->sndlen = uio->uio_resid;
6427 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6428 if (ca->m == NULL) {
6429 SCTP_FREE(ca, SCTP_M_COPYAL);
6430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6434 /* Gather the length of the send */
6440 ca->sndlen += SCTP_BUF_LEN(m);
6441 m = SCTP_BUF_NEXT(m);
6445 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6446 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6447 SCTP_ASOC_ANY_STATE,
6449 sctp_sendall_completes, inp, 1);
6451 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6452 SCTP_FREE(ca, SCTP_M_COPYAL);
6453 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6461 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6463 struct sctp_tmit_chunk *chk, *nchk;
6465 chk = TAILQ_FIRST(&asoc->control_send_queue);
6467 nchk = TAILQ_NEXT(chk, sctp_next);
6468 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6469 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6471 sctp_m_freem(chk->data);
6474 asoc->ctrl_queue_cnt--;
6475 sctp_free_a_chunk(stcb, chk);
6482 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6484 struct sctp_association *asoc;
6485 struct sctp_tmit_chunk *chk, *chk_tmp;
6486 struct sctp_asconf_chunk *acp;
6489 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6492 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6493 /* find SCTP_ASCONF chunk in queue */
6494 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6496 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6497 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6502 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6504 sctp_m_freem(chk->data);
6507 asoc->ctrl_queue_cnt--;
6508 sctp_free_a_chunk(stcb, chk);
6515 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6517 struct sctp_association *asoc,
6518 struct sctp_tmit_chunk **data_list,
6520 struct sctp_nets *net)
6523 struct sctp_tmit_chunk *tp1;
6525 for (i = 0; i < bundle_at; i++) {
6526 /* off of the send queue */
6529 * Any chunk NOT 0 you zap the time chunk 0 gets
6530 * zapped or set based on if a RTO measurment is
6533 data_list[i]->do_rtt = 0;
6536 data_list[i]->sent_rcv_time = net->last_sent_time;
6537 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6538 TAILQ_REMOVE(&asoc->send_queue,
6541 /* on to the sent queue */
6542 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6543 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6544 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6545 struct sctp_tmit_chunk *tpp;
6547 /* need to move back */
6549 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6551 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6555 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6556 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6559 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6561 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6566 /* This does not lower until the cum-ack passes it */
6567 asoc->sent_queue_cnt++;
6568 asoc->send_queue_cnt--;
6569 if ((asoc->peers_rwnd <= 0) &&
6570 (asoc->total_flight == 0) &&
6572 /* Mark the chunk as being a window probe */
6573 SCTP_STAT_INCR(sctps_windowprobed);
6575 #ifdef SCTP_AUDITING_ENABLED
6576 sctp_audit_log(0xC2, 3);
6578 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6579 data_list[i]->snd_count = 1;
6580 data_list[i]->rec.data.chunk_was_revoked = 0;
6581 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6582 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6583 data_list[i]->whoTo->flight_size,
6584 data_list[i]->book_size,
6585 (uintptr_t) data_list[i]->whoTo,
6586 data_list[i]->rec.data.TSN_seq);
6588 sctp_flight_size_increase(data_list[i]);
6589 sctp_total_flight_increase(stcb, data_list[i]);
6590 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6591 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6592 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6594 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6595 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6596 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6597 /* SWS sender side engages */
6598 asoc->peers_rwnd = 0;
6604 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6606 struct sctp_tmit_chunk *chk, *nchk;
6608 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6610 nchk = TAILQ_NEXT(chk, sctp_next);
6611 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6612 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
6613 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6614 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6615 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6616 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6617 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6618 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6619 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6620 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6621 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6622 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6623 /* Stray chunks must be cleaned up */
6625 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6627 sctp_m_freem(chk->data);
6630 asoc->ctrl_queue_cnt--;
6631 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6632 asoc->fwd_tsn_cnt--;
6633 sctp_free_a_chunk(stcb, chk);
6634 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6635 /* special handling, we must look into the param */
6636 if (chk != asoc->str_reset) {
6637 goto clean_up_anyway;
6645 sctp_can_we_split_this(struct sctp_tcb *stcb,
6647 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6650 * Make a decision on if I should split a msg into multiple parts.
6651 * This is only asked of incomplete messages.
6655 * If we are doing EEOR we need to always send it if its the
6656 * entire thing, since it might be all the guy is putting in
6659 if (goal_mtu >= length) {
6661 * If we have data outstanding,
6662 * we get another chance when the sack
6663 * arrives to transmit - wait for more data
6665 if (stcb->asoc.total_flight == 0) {
6667 * If nothing is in flight, we zero the
6675 /* You can fill the rest */
6680 * For those strange folk that make the send buffer
6681 * smaller than our fragmentation point, we can't
6682 * get a full msg in so we have to allow splitting.
6684 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6687 if ((length <= goal_mtu) ||
6688 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6689 /* Sub-optimial residual don't split in non-eeor mode. */
6693 * If we reach here length is larger than the goal_mtu. Do we wish
6694 * to split it for the sake of packet putting together?
6696 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6697 /* Its ok to split it */
6698 return (min(goal_mtu, frag_point));
6700 /* Nope, can't split */
6706 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6707 struct sctp_stream_out *strq,
6709 uint32_t frag_point,
6715 /* Move from the stream to the send_queue keeping track of the total */
6716 struct sctp_association *asoc;
6717 struct sctp_stream_queue_pending *sp;
6718 struct sctp_tmit_chunk *chk;
6719 struct sctp_data_chunk *dchkh;
6720 uint32_t to_move, length;
6721 uint8_t rcv_flags = 0;
6723 uint8_t send_lock_up = 0;
6725 SCTP_TCB_LOCK_ASSERT(stcb);
6728 /* sa_ignore FREED_MEMORY */
6729 sp = TAILQ_FIRST(&strq->outqueue);
6732 if (send_lock_up == 0) {
6733 SCTP_TCB_SEND_LOCK(stcb);
6736 sp = TAILQ_FIRST(&strq->outqueue);
6740 if (strq->last_msg_incomplete) {
6741 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6743 strq->last_msg_incomplete);
6744 strq->last_msg_incomplete = 0;
6748 SCTP_TCB_SEND_UNLOCK(stcb);
6753 if ((sp->msg_is_complete) && (sp->length == 0)) {
6754 if (sp->sender_all_done) {
6756 * We are doing differed cleanup. Last time through
6757 * when we took all the data the sender_all_done was
6760 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6761 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6762 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6763 sp->sender_all_done,
6765 sp->msg_is_complete,
6769 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6770 SCTP_TCB_SEND_LOCK(stcb);
6773 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6774 TAILQ_REMOVE(&strq->outqueue, sp, next);
6775 sctp_free_remote_addr(sp->net);
6777 sctp_m_freem(sp->data);
6780 sctp_free_a_strmoq(stcb, sp);
6781 /* we can't be locked to it */
6783 stcb->asoc.locked_on_sending = NULL;
6785 SCTP_TCB_SEND_UNLOCK(stcb);
6788 /* back to get the next msg */
6792 * sender just finished this but still holds a
6801 /* is there some to get */
6802 if (sp->length == 0) {
6808 } else if (sp->discard_rest) {
6809 if (send_lock_up == 0) {
6810 SCTP_TCB_SEND_LOCK(stcb);
6813 /* Whack down the size */
6814 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6815 if ((stcb->sctp_socket != NULL) && \
6816 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6817 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6818 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6821 sctp_m_freem(sp->data);
6823 sp->tail_mbuf = NULL;
6833 some_taken = sp->some_taken;
6834 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6835 sp->msg_is_complete = 1;
6838 length = sp->length;
6839 if (sp->msg_is_complete) {
6840 /* The message is complete */
6841 to_move = min(length, frag_point);
6842 if (to_move == length) {
6843 /* All of it fits in the MTU */
6844 if (sp->some_taken) {
6845 rcv_flags |= SCTP_DATA_LAST_FRAG;
6846 sp->put_last_out = 1;
6848 rcv_flags |= SCTP_DATA_NOT_FRAG;
6849 sp->put_last_out = 1;
6852 /* Not all of it fits, we fragment */
6853 if (sp->some_taken == 0) {
6854 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6859 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6862 * We use a snapshot of length in case it
6863 * is expanding during the compare.
6868 if (to_move >= llen) {
6870 if (send_lock_up == 0) {
6872 * We are taking all of an incomplete msg
6873 * thus we need a send lock.
6875 SCTP_TCB_SEND_LOCK(stcb);
6877 if (sp->msg_is_complete) {
6879 * the sender finished the
6886 if (sp->some_taken == 0) {
6887 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6891 /* Nothing to take. */
6892 if (sp->some_taken) {
6901 /* If we reach here, we can copy out a chunk */
6902 sctp_alloc_a_chunk(stcb, chk);
6904 /* No chunk memory */
6910 * Setup for unordered if needed by looking at the user sent info
6913 if (sp->sinfo_flags & SCTP_UNORDERED) {
6914 rcv_flags |= SCTP_DATA_UNORDERED;
6916 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6917 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6918 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6920 /* clear out the chunk before setting up */
6921 memset(chk, 0, sizeof(*chk));
6922 chk->rec.data.rcv_flags = rcv_flags;
6924 if (to_move >= length) {
6925 /* we think we can steal the whole thing */
6926 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6927 SCTP_TCB_SEND_LOCK(stcb);
6930 if (to_move < sp->length) {
6931 /* bail, it changed */
6934 chk->data = sp->data;
6935 chk->last_mbuf = sp->tail_mbuf;
6936 /* register the stealing */
6937 sp->data = sp->tail_mbuf = NULL;
6942 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6943 chk->last_mbuf = NULL;
6944 if (chk->data == NULL) {
6945 sp->some_taken = some_taken;
6946 sctp_free_a_chunk(stcb, chk);
6951 #ifdef SCTP_MBUF_LOGGING
6952 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6957 if (SCTP_BUF_IS_EXTENDED(mat)) {
6958 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6960 mat = SCTP_BUF_NEXT(mat);
6964 /* Pull off the data */
6965 m_adj(sp->data, to_move);
6966 /* Now lets work our way down and compact it */
6968 while (m && (SCTP_BUF_LEN(m) == 0)) {
6969 sp->data = SCTP_BUF_NEXT(m);
6970 SCTP_BUF_NEXT(m) = NULL;
6971 if (sp->tail_mbuf == m) {
6973 * Freeing tail? TSNH since
6974 * we supposedly were taking less
6975 * than the sp->length.
6978 panic("Huh, freing tail? - TSNH");
6980 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6981 sp->tail_mbuf = sp->data = NULL;
6990 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6991 chk->copy_by_ref = 1;
6993 chk->copy_by_ref = 0;
6996 * get last_mbuf and counts of mb useage This is ugly but hopefully
6997 * its only one mbuf.
6999 if (chk->last_mbuf == NULL) {
7000 chk->last_mbuf = chk->data;
7001 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7002 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7005 if (to_move > length) {
7006 /*- This should not happen either
7007 * since we always lower to_move to the size
7008 * of sp->length if its larger.
7011 panic("Huh, how can to_move be larger?");
7013 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7017 atomic_subtract_int(&sp->length, to_move);
7019 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7020 /* Not enough room for a chunk header, get some */
7023 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7026 * we're in trouble here. _PREPEND below will free
7027 * all the data if there is no leading space, so we
7028 * must put the data back and restore.
7030 if (send_lock_up == 0) {
7031 SCTP_TCB_SEND_LOCK(stcb);
7034 if (chk->data == NULL) {
7035 /* unsteal the data */
7036 sp->data = chk->data;
7037 sp->tail_mbuf = chk->last_mbuf;
7041 /* reassemble the data */
7043 sp->data = chk->data;
7044 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7046 sp->some_taken = some_taken;
7047 atomic_add_int(&sp->length, to_move);
7050 sctp_free_a_chunk(stcb, chk);
7054 SCTP_BUF_LEN(m) = 0;
7055 SCTP_BUF_NEXT(m) = chk->data;
7057 M_ALIGN(chk->data, 4);
7060 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7061 if (chk->data == NULL) {
7062 /* HELP, TSNH since we assured it would not above? */
7064 panic("prepend failes HELP?");
7066 SCTP_PRINTF("prepend fails HELP?\n");
7067 sctp_free_a_chunk(stcb, chk);
7073 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7074 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7075 chk->book_size_scale = 0;
7076 chk->sent = SCTP_DATAGRAM_UNSENT;
7079 chk->asoc = &stcb->asoc;
7080 chk->pad_inplace = 0;
7081 chk->no_fr_allowed = 0;
7082 chk->rec.data.stream_seq = sp->strseq;
7083 chk->rec.data.stream_number = sp->stream;
7084 chk->rec.data.payloadtype = sp->ppid;
7085 chk->rec.data.context = sp->context;
7086 chk->rec.data.doing_fast_retransmit = 0;
7087 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
7089 chk->rec.data.timetodrop = sp->ts;
7090 chk->flags = sp->act_flags;
7093 atomic_add_int(&chk->whoTo->ref_count, 1);
7095 if (sp->holds_key_ref) {
7096 chk->auth_keyid = sp->auth_keyid;
7097 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7098 chk->holds_key_ref = 1;
7100 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7101 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7102 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7103 (uintptr_t) stcb, sp->length,
7104 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7105 chk->rec.data.TSN_seq);
7107 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7109 * Put the rest of the things in place now. Size was done earlier in
7110 * previous loop prior to padding.
7113 #ifdef SCTP_ASOCLOG_OF_TSNS
7114 SCTP_TCB_LOCK_ASSERT(stcb);
7115 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7116 asoc->tsn_out_at = 0;
7117 asoc->tsn_out_wrapped = 1;
7119 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7120 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7121 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7122 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7123 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7124 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7125 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7126 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7130 dchkh->ch.chunk_type = SCTP_DATA;
7131 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7132 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7133 dchkh->dp.stream_id = htons(strq->stream_no);
7134 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7135 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7136 dchkh->ch.chunk_length = htons(chk->send_size);
7137 /* Now advance the chk->send_size by the actual pad needed. */
7138 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7143 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7144 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7145 chk->pad_inplace = 1;
7147 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7148 /* pad added an mbuf */
7149 chk->last_mbuf = lm;
7151 chk->send_size += pads;
7153 /* We only re-set the policy if it is on */
7154 if (sp->pr_sctp_on) {
7155 sctp_set_prsctp_policy(sp);
7156 asoc->pr_sctp_cnt++;
7157 chk->pr_sctp_on = 1;
7159 chk->pr_sctp_on = 0;
7161 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7162 /* All done pull and kill the message */
7163 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7164 if (sp->put_last_out == 0) {
7165 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7166 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7167 sp->sender_all_done,
7169 sp->msg_is_complete,
7173 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7174 SCTP_TCB_SEND_LOCK(stcb);
7177 TAILQ_REMOVE(&strq->outqueue, sp, next);
7178 sctp_free_remote_addr(sp->net);
7180 sctp_m_freem(sp->data);
7183 sctp_free_a_strmoq(stcb, sp);
7185 /* we can't be locked to it */
7187 stcb->asoc.locked_on_sending = NULL;
7189 /* more to go, we are locked */
7192 asoc->chunks_on_out_queue++;
7193 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7194 asoc->send_queue_cnt++;
7197 SCTP_TCB_SEND_UNLOCK(stcb);
7204 static struct sctp_stream_out *
7205 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7207 struct sctp_stream_out *strq;
7209 /* Find the next stream to use */
7210 if (asoc->last_out_stream == NULL) {
7211 strq = TAILQ_FIRST(&asoc->out_wheel);
7213 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7215 strq = TAILQ_FIRST(&asoc->out_wheel);
7223 sctp_fill_outqueue(struct sctp_tcb *stcb,
7224 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7226 struct sctp_association *asoc;
7227 struct sctp_stream_out *strq, *strqn;
7228 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7230 struct sctp_stream_queue_pending *sp;
7232 SCTP_TCB_LOCK_ASSERT(stcb);
7235 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7236 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7238 /* ?? not sure what else to do */
7239 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7242 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7244 /* Need an allowance for the data chunk header too */
7245 goal_mtu -= sizeof(struct sctp_data_chunk);
7247 /* must make even word boundary */
7248 goal_mtu &= 0xfffffffc;
7249 if (asoc->locked_on_sending) {
7250 /* We are stuck on one stream until the message completes. */
7251 strqn = strq = asoc->locked_on_sending;
7254 strqn = strq = sctp_select_a_stream(stcb, asoc);
7258 while ((goal_mtu > 0) && strq) {
7259 sp = TAILQ_FIRST(&strq->outqueue);
7261 * If CMT is off, we must validate that the stream in
7262 * question has the first item pointed towards are network
7263 * destionation requested by the caller. Note that if we
7264 * turn out to be locked to a stream (assigning TSN's then
7265 * we must stop, since we cannot look for another stream
7266 * with data to send to that destination). In CMT's case, by
7267 * skipping this check, we will send one data packet towards
7268 * the requested net.
7273 if ((sp->net != net) &&
7274 (asoc->sctp_cmt_on_off == 0)) {
7275 /* none for this network */
7279 strq = sctp_select_a_stream(stcb, asoc);
7283 if (strqn == strq) {
7284 /* I have circled */
7292 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7293 &giveup, eeor_mode, &bail);
7295 asoc->last_out_stream = strq;
7298 asoc->locked_on_sending = strq;
7299 if ((moved_how_much == 0) || (giveup) || bail)
7300 /* no more to move for now */
7303 asoc->locked_on_sending = NULL;
7304 if (TAILQ_EMPTY(&strq->outqueue)) {
7305 if (strq == strqn) {
7306 /* Must move start to next one */
7307 strqn = TAILQ_NEXT(strq, next_spoke);
7308 if (strqn == NULL) {
7309 strqn = TAILQ_FIRST(&asoc->out_wheel);
7310 if (strqn == NULL) {
7315 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7317 if ((giveup) || bail) {
7320 strq = sctp_select_a_stream(stcb, asoc);
7325 total_moved += moved_how_much;
7326 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7327 goal_mtu &= 0xfffffffc;
7332 if (total_moved == 0) {
7333 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7334 (net == stcb->asoc.primary_destination)) {
7335 /* ran dry for primary network net */
7336 SCTP_STAT_INCR(sctps_primary_randry);
7337 } else if (stcb->asoc.sctp_cmt_on_off == 1) {
7338 /* ran dry with CMT on */
7339 SCTP_STAT_INCR(sctps_cmt_randry);
7345 sctp_fix_ecn_echo(struct sctp_association *asoc)
7347 struct sctp_tmit_chunk *chk;
7349 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7350 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7351 chk->sent = SCTP_DATAGRAM_UNSENT;
7357 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7358 struct sctp_association *asoc,
7359 struct sctp_nets *net)
7361 struct sctp_tmit_chunk *chk;
7362 struct sctp_nets *a_net;
7364 SCTP_TCB_LOCK_ASSERT(stcb);
7366 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7367 * destination using the PF algorithm for finding alternate
7370 if ((asoc->sctp_cmt_on_off == 1) &&
7371 (asoc->sctp_cmt_pf > 0)) {
7372 a_net = sctp_find_alternate_net(stcb, net, 2);
7374 a_net = sctp_find_alternate_net(stcb, net, 0);
7376 if ((a_net != net) &&
7377 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7379 * We only proceed if a valid alternate is found that is not
7380 * this one and is reachable. Here we must move all chunks
7381 * queued in the send queue off of the destination address
7384 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7385 if (chk->whoTo == net) {
7386 /* Move the chunk to our alternate */
7387 sctp_free_remote_addr(chk->whoTo);
7389 atomic_add_int(&a_net->ref_count, 1);
7396 sctp_med_chunk_output(struct sctp_inpcb *inp,
7397 struct sctp_tcb *stcb,
7398 struct sctp_association *asoc,
7401 int control_only, int from_where,
7402 struct timeval *now, int *now_filled, int frag_point, int so_locked
7403 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7409 * Ok this is the generic chunk service queue. we must do the
7410 * following: - Service the stream queue that is next, moving any
7411 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7412 * LAST to the out queue in one pass) and assigning TSN's - Check to
7413 * see if the cwnd/rwnd allows any output, if so we go ahead and
7414 * fomulate and send the low level chunks. Making sure to combine
7415 * any control in the control chunk queue also.
7417 struct sctp_nets *net, *start_at, *old_start_at = NULL;
7418 struct mbuf *outchain, *endoutchain;
7419 struct sctp_tmit_chunk *chk, *nchk;
7421 /* temp arrays for unlinking */
7422 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7423 int no_fragmentflg, error;
7424 unsigned int max_rwnd_per_dest;
7425 int one_chunk, hbflag, skip_data_for_this_net;
7426 int asconf, cookie, no_out_cnt;
7427 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7428 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7430 uint32_t auth_offset = 0;
7431 struct sctp_auth_chunk *auth = NULL;
7432 uint16_t auth_keyid;
7433 int override_ok = 1;
7434 int data_auth_reqd = 0;
7437 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7444 auth_keyid = stcb->asoc.authinfo.active_keyid;
7446 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7447 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7448 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7453 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7455 * First lets prime the pump. For each destination, if there is room
7456 * in the flight size, attempt to pull an MTU's worth out of the
7457 * stream queues into the general send_queue
7459 #ifdef SCTP_AUDITING_ENABLED
7460 sctp_audit_log(0xC2, 2);
7462 SCTP_TCB_LOCK_ASSERT(stcb);
7464 if ((control_only) || (asoc->stream_reset_outstanding))
7469 /* Nothing to possible to send? */
7470 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7471 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7472 TAILQ_EMPTY(&asoc->send_queue) &&
7473 TAILQ_EMPTY(&asoc->out_wheel)) {
7477 if (asoc->peers_rwnd == 0) {
7478 /* No room in peers rwnd */
7480 if (asoc->total_flight > 0) {
7481 /* we are allowed one chunk in flight */
7485 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7486 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7487 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7489 * This for loop we are in takes in each net, if
7490 * its's got space in cwnd and has data sent to it
7491 * (when CMT is off) then it calls
7492 * sctp_fill_outqueue for the net. This gets data on
7493 * the send queue for that network.
7495 * In sctp_fill_outqueue TSN's are assigned and data is
7496 * copied out of the stream buffers. Note mostly
7497 * copy by reference (we hope).
7499 net->window_probe = 0;
7500 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7501 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7502 sctp_log_cwnd(stcb, net, 1,
7503 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7507 if ((asoc->sctp_cmt_on_off == 0) &&
7508 (net->ref_count < 2)) {
7509 /* nothing can be in queue for this guy */
7510 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7511 sctp_log_cwnd(stcb, net, 2,
7512 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7516 if (net->flight_size >= net->cwnd) {
7517 /* skip this network, no room - can't fill */
7518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7519 sctp_log_cwnd(stcb, net, 3,
7520 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7524 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7525 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7527 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7529 /* memory alloc failure */
7535 /* now service each destination and send out what we can for it */
7536 /* Nothing to send? */
7537 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7538 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7539 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7543 if (asoc->sctp_cmt_on_off == 1) {
7544 /* get the last start point */
7545 start_at = asoc->last_net_cmt_send_started;
7546 if (start_at == NULL) {
7547 /* null so to beginning */
7548 start_at = TAILQ_FIRST(&asoc->nets);
7550 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7551 if (start_at == NULL) {
7552 start_at = TAILQ_FIRST(&asoc->nets);
7555 asoc->last_net_cmt_send_started = start_at;
7557 start_at = TAILQ_FIRST(&asoc->nets);
7559 old_start_at = NULL;
7560 again_one_more_time:
7561 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7562 /* how much can we send? */
7563 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7564 if (old_start_at && (old_start_at == net)) {
7565 /* through list ocmpletely. */
7569 if ((asoc->sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
7571 * Ref-count of 1 so we cannot have data or control
7572 * queued to this address. Skip it (non-CMT).
7576 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7577 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7578 (net->flight_size >= net->cwnd)) {
7580 * Nothing on control or asconf and flight is full,
7581 * we can skip even in the CMT case.
7585 ctl_cnt = bundle_at = 0;
7586 endoutchain = outchain = NULL;
7589 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7590 skip_data_for_this_net = 1;
7592 skip_data_for_this_net = 0;
7594 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7596 * if we have a route and an ifp check to see if we
7597 * have room to send to this guy
7601 ifp = net->ro.ro_rt->rt_ifp;
7602 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7603 SCTP_STAT_INCR(sctps_ifnomemqueued);
7604 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7605 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7610 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7612 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7616 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7626 if (mtu > asoc->peers_rwnd) {
7627 if (asoc->total_flight > 0) {
7628 /* We have a packet in flight somewhere */
7629 r_mtu = asoc->peers_rwnd;
7631 /* We are always allowed to send one MTU out */
7638 /************************/
7639 /* ASCONF transmission */
7640 /************************/
7641 /* Now first lets go through the asconf queue */
7642 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7644 nchk = TAILQ_NEXT(chk, sctp_next);
7645 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7648 if (chk->whoTo != net) {
7650 * No, not sent to the network we are
7655 if (chk->data == NULL) {
7658 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7659 chk->sent != SCTP_DATAGRAM_RESEND) {
7663 * if no AUTH is yet included and this chunk
7664 * requires it, make sure to account for it. We
7665 * don't apply the size until the AUTH chunk is
7666 * actually added below in case there is no room for
7667 * this chunk. NOTE: we overload the use of "omtu"
7670 if ((auth == NULL) &&
7671 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7672 stcb->asoc.peer_auth_chunks)) {
7673 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7676 /* Here we do NOT factor the r_mtu */
7677 if ((chk->send_size < (int)(mtu - omtu)) ||
7678 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7680 * We probably should glom the mbuf chain
7681 * from the chk->data for control but the
7682 * problem is it becomes yet one more level
7683 * of tracking to do if for some reason
7684 * output fails. Then I have got to
7685 * reconstruct the merged control chain.. el
7686 * yucko.. for now we take the easy way and
7690 * Add an AUTH chunk, if chunk requires it
7691 * save the offset into the chain for AUTH
7693 if ((auth == NULL) &&
7694 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7695 stcb->asoc.peer_auth_chunks))) {
7696 outchain = sctp_add_auth_chunk(outchain,
7701 chk->rec.chunk_id.id);
7702 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7704 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7705 (int)chk->rec.chunk_id.can_take_data,
7706 chk->send_size, chk->copy_by_ref);
7707 if (outchain == NULL) {
7709 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7712 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7713 /* update our MTU size */
7714 if (mtu > (chk->send_size + omtu))
7715 mtu -= (chk->send_size + omtu);
7718 to_out += (chk->send_size + omtu);
7719 /* Do clear IP_DF ? */
7720 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7723 if (chk->rec.chunk_id.can_take_data)
7726 * set hb flag since we can use these for
7732 * should sysctl this: don't bundle data
7733 * with ASCONF since it requires AUTH
7736 chk->sent = SCTP_DATAGRAM_SENT;
7740 * Ok we are out of room but we can
7741 * output without effecting the
7742 * flight size since this little guy
7743 * is a control only packet.
7745 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7747 * do NOT clear the asconf flag as
7748 * it is used to do appropriate
7749 * source address selection.
7751 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7752 (struct sockaddr *)&net->ro._l_addr,
7753 outchain, auth_offset, auth,
7754 stcb->asoc.authinfo.active_keyid,
7755 no_fragmentflg, 0, NULL, asconf,
7756 inp->sctp_lport, stcb->rport,
7757 htonl(stcb->asoc.peer_vtag),
7758 net->port, so_locked, NULL))) {
7759 if (error == ENOBUFS) {
7760 asoc->ifp_had_enobuf = 1;
7761 SCTP_STAT_INCR(sctps_lowlevelerr);
7763 if (from_where == 0) {
7764 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7766 if (*now_filled == 0) {
7767 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7769 *now = net->last_sent_time;
7771 net->last_sent_time = *now;
7774 /* error, could not output */
7775 if (error == EHOSTUNREACH) {
7781 sctp_move_to_an_alt(stcb, asoc, net);
7786 asoc->ifp_had_enobuf = 0;
7787 if (*now_filled == 0) {
7788 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7790 *now = net->last_sent_time;
7792 net->last_sent_time = *now;
7796 * increase the number we sent, if a
7797 * cookie is sent we don't tell them
7800 outchain = endoutchain = NULL;
7804 *num_out += ctl_cnt;
7805 /* recalc a clean slate and setup */
7806 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7807 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7809 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7816 /************************/
7817 /* Control transmission */
7818 /************************/
7819 /* Now first lets go through the control queue */
7820 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7822 nchk = TAILQ_NEXT(chk, sctp_next);
7823 if (chk->whoTo != net) {
7825 * No, not sent to the network we are
7830 if (chk->data == NULL) {
7833 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7835 * It must be unsent. Cookies and ASCONF's
7836 * hang around but there timers will force
7837 * when marked for resend.
7842 * if no AUTH is yet included and this chunk
7843 * requires it, make sure to account for it. We
7844 * don't apply the size until the AUTH chunk is
7845 * actually added below in case there is no room for
7846 * this chunk. NOTE: we overload the use of "omtu"
7849 if ((auth == NULL) &&
7850 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7851 stcb->asoc.peer_auth_chunks)) {
7852 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7855 /* Here we do NOT factor the r_mtu */
7856 if ((chk->send_size <= (int)(mtu - omtu)) ||
7857 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7859 * We probably should glom the mbuf chain
7860 * from the chk->data for control but the
7861 * problem is it becomes yet one more level
7862 * of tracking to do if for some reason
7863 * output fails. Then I have got to
7864 * reconstruct the merged control chain.. el
7865 * yucko.. for now we take the easy way and
7869 * Add an AUTH chunk, if chunk requires it
7870 * save the offset into the chain for AUTH
7872 if ((auth == NULL) &&
7873 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7874 stcb->asoc.peer_auth_chunks))) {
7875 outchain = sctp_add_auth_chunk(outchain,
7880 chk->rec.chunk_id.id);
7881 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7883 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7884 (int)chk->rec.chunk_id.can_take_data,
7885 chk->send_size, chk->copy_by_ref);
7886 if (outchain == NULL) {
7888 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7891 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7892 /* update our MTU size */
7893 if (mtu > (chk->send_size + omtu))
7894 mtu -= (chk->send_size + omtu);
7897 to_out += (chk->send_size + omtu);
7898 /* Do clear IP_DF ? */
7899 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7902 if (chk->rec.chunk_id.can_take_data)
7904 /* Mark things to be removed, if needed */
7905 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7906 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7907 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7908 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7909 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7910 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7911 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7912 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7913 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7914 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7915 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7917 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7920 * JRS 5/14/07 - Set the
7921 * flag to say a heartbeat
7926 /* remove these chunks at the end */
7927 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7928 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7929 /* turn off the timer */
7930 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7931 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7932 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7938 * Other chunks, since they have
7939 * timers running (i.e. COOKIE) we
7940 * just "trust" that it gets sent or
7944 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7948 chk->sent = SCTP_DATAGRAM_SENT;
7953 * Ok we are out of room but we can
7954 * output without effecting the
7955 * flight size since this little guy
7956 * is a control only packet.
7959 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7961 * do NOT clear the asconf
7962 * flag as it is used to do
7963 * appropriate source
7964 * address selection.
7968 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7971 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7972 (struct sockaddr *)&net->ro._l_addr,
7975 stcb->asoc.authinfo.active_keyid,
7976 no_fragmentflg, 0, NULL, asconf,
7977 inp->sctp_lport, stcb->rport,
7978 htonl(stcb->asoc.peer_vtag),
7979 net->port, so_locked, NULL))) {
7980 if (error == ENOBUFS) {
7981 asoc->ifp_had_enobuf = 1;
7982 SCTP_STAT_INCR(sctps_lowlevelerr);
7984 if (from_where == 0) {
7985 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7987 /* error, could not output */
7989 if (*now_filled == 0) {
7990 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7992 *now = net->last_sent_time;
7994 net->last_sent_time = *now;
7998 if (error == EHOSTUNREACH) {
8004 sctp_move_to_an_alt(stcb, asoc, net);
8009 asoc->ifp_had_enobuf = 0;
8010 /* Only HB or ASCONF advances time */
8012 if (*now_filled == 0) {
8013 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8015 *now = net->last_sent_time;
8017 net->last_sent_time = *now;
8022 * increase the number we sent, if a
8023 * cookie is sent we don't tell them
8026 outchain = endoutchain = NULL;
8030 *num_out += ctl_cnt;
8031 /* recalc a clean slate and setup */
8032 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8033 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8035 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8042 /* JRI: if dest is in PF state, do not send data to it */
8043 if ((asoc->sctp_cmt_on_off == 1) &&
8044 (asoc->sctp_cmt_pf > 0) &&
8045 (net->dest_state & SCTP_ADDR_PF)) {
8048 if (net->flight_size >= net->cwnd) {
8051 if ((asoc->sctp_cmt_on_off == 1) &&
8052 (net->flight_size > max_rwnd_per_dest)) {
8055 /*********************/
8056 /* Data transmission */
8057 /*********************/
8059 * if AUTH for DATA is required and no AUTH has been added
8060 * yet, account for this in the mtu now... if no data can be
8061 * bundled, this adjustment won't matter anyways since the
8062 * packet will be going out...
8064 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8065 stcb->asoc.peer_auth_chunks);
8066 if (data_auth_reqd && (auth == NULL)) {
8067 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8069 /* now lets add any data within the MTU constraints */
8070 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8072 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8073 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8079 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8080 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8090 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8091 (skip_data_for_this_net == 0)) ||
8093 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8094 if (no_data_chunks) {
8095 /* let only control go out */
8099 if (net->flight_size >= net->cwnd) {
8100 /* skip this net, no room for data */
8104 nchk = TAILQ_NEXT(chk, sctp_next);
8105 if (asoc->sctp_cmt_on_off == 1) {
8106 if (chk->whoTo != net) {
8108 * For CMT, steal the data
8109 * to this network if its
8112 sctp_free_remote_addr(chk->whoTo);
8114 atomic_add_int(&chk->whoTo->ref_count, 1);
8116 } else if (chk->whoTo != net) {
8117 /* No, not sent to this net */
8120 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8122 * strange, we have a chunk that is
8123 * to big for its destination and
8124 * yet no fragment ok flag.
8125 * Something went wrong when the
8126 * PMTU changed...we did not mark
8127 * this chunk for some reason?? I
8128 * will fix it here by letting IP
8129 * fragment it for now and printing
8130 * a warning. This really should not
8133 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8134 chk->send_size, mtu);
8135 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8137 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8138 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8139 struct sctp_data_chunk *dchkh;
8141 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8142 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8144 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8145 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8146 /* ok we will add this one */
8149 * Add an AUTH chunk, if chunk
8150 * requires it, save the offset into
8151 * the chain for AUTH
8153 if (data_auth_reqd) {
8155 outchain = sctp_add_auth_chunk(outchain,
8161 auth_keyid = chk->auth_keyid;
8163 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8164 } else if (override_ok) {
8169 auth_keyid = chk->auth_keyid;
8171 } else if (auth_keyid != chk->auth_keyid) {
8179 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8180 chk->send_size, chk->copy_by_ref);
8181 if (outchain == NULL) {
8182 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8183 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8184 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8187 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8190 /* upate our MTU size */
8191 /* Do clear IP_DF ? */
8192 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8195 /* unsigned subtraction of mtu */
8196 if (mtu > chk->send_size)
8197 mtu -= chk->send_size;
8200 /* unsigned subtraction of r_mtu */
8201 if (r_mtu > chk->send_size)
8202 r_mtu -= chk->send_size;
8206 to_out += chk->send_size;
8207 if ((to_out > mx_mtu) && no_fragmentflg) {
8209 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8211 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8215 chk->window_probe = 0;
8216 data_list[bundle_at++] = chk;
8217 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8221 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8222 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8223 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8225 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8227 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8228 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8238 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8240 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8241 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8242 data_list[0]->window_probe = 1;
8243 net->window_probe = 1;
8249 * Must be sent in order of the
8250 * TSN's (on a network)
8254 } /* for (chunk gather loop for this net) */
8255 } /* if asoc.state OPEN */
8257 /* Is there something to send for this destination? */
8259 /* We may need to start a control timer or two */
8261 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8264 * do NOT clear the asconf flag as it is
8265 * used to do appropriate source address
8270 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8273 /* must start a send timer if data is being sent */
8274 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8276 * no timer running on this destination
8279 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8280 } else if ((asoc->sctp_cmt_on_off == 1) &&
8281 (asoc->sctp_cmt_pf > 0) &&
8283 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8284 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8286 * JRS 5/14/07 - If a HB has been sent to a
8287 * PF destination and no T3 timer is
8288 * currently running, start the T3 timer to
8289 * track the HBs that were sent.
8291 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8293 /* Now send it, if there is anything to send :> */
8294 if ((error = sctp_lowlevel_chunk_output(inp,
8297 (struct sockaddr *)&net->ro._l_addr,
8306 inp->sctp_lport, stcb->rport,
8307 htonl(stcb->asoc.peer_vtag),
8308 net->port, so_locked, NULL))) {
8309 /* error, we could not output */
8310 if (error == ENOBUFS) {
8311 SCTP_STAT_INCR(sctps_lowlevelerr);
8312 asoc->ifp_had_enobuf = 1;
8314 if (from_where == 0) {
8315 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8317 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8319 if (*now_filled == 0) {
8320 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8322 *now = net->last_sent_time;
8324 net->last_sent_time = *now;
8328 if (error == EHOSTUNREACH) {
8330 * Destination went unreachable
8333 sctp_move_to_an_alt(stcb, asoc, net);
8337 * I add this line to be paranoid. As far as
8338 * I can tell the continue, takes us back to
8339 * the top of the for, but just to make sure
8340 * I will reset these again here.
8342 ctl_cnt = bundle_at = 0;
8343 continue; /* This takes us back to the
8344 * for() for the nets. */
8346 asoc->ifp_had_enobuf = 0;
8348 outchain = endoutchain = NULL;
8351 if (bundle_at || hbflag) {
8352 /* For data/asconf and hb set time */
8353 if (*now_filled == 0) {
8354 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8356 *now = net->last_sent_time;
8358 net->last_sent_time = *now;
8362 *num_out += (ctl_cnt + bundle_at);
8365 /* setup for a RTO measurement */
8366 tsns_sent = data_list[0]->rec.data.TSN_seq;
8367 /* fill time if not already filled */
8368 if (*now_filled == 0) {
8369 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8371 *now = asoc->time_last_sent;
8373 asoc->time_last_sent = *now;
8375 data_list[0]->do_rtt = 1;
8376 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8377 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8378 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8379 if (net->flight_size < net->cwnd) {
8380 /* start or restart it */
8381 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8382 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8383 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8385 SCTP_STAT_INCR(sctps_earlyfrstrout);
8386 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8388 /* stop it if its running */
8389 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8390 SCTP_STAT_INCR(sctps_earlyfrstpout);
8391 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8392 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8401 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8402 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8405 if (old_start_at == NULL) {
8406 old_start_at = start_at;
8407 start_at = TAILQ_FIRST(&asoc->nets);
8409 goto again_one_more_time;
8412 * At the end there should be no NON timed chunks hanging on this
8415 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8416 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8418 if ((*num_out == 0) && (*reason_code == 0)) {
8423 sctp_clean_up_ctl(stcb, asoc);
8428 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8431 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8432 * the control chunk queue.
8434 struct sctp_chunkhdr *hdr;
8435 struct sctp_tmit_chunk *chk;
8438 SCTP_TCB_LOCK_ASSERT(stcb);
8439 sctp_alloc_a_chunk(stcb, chk);
8442 sctp_m_freem(op_err);
8445 chk->copy_by_ref = 0;
8446 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8447 if (op_err == NULL) {
8448 sctp_free_a_chunk(stcb, chk);
8453 while (mat != NULL) {
8454 chk->send_size += SCTP_BUF_LEN(mat);
8455 mat = SCTP_BUF_NEXT(mat);
8457 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8458 chk->rec.chunk_id.can_take_data = 1;
8459 chk->sent = SCTP_DATAGRAM_UNSENT;
8462 chk->asoc = &stcb->asoc;
8464 chk->whoTo = chk->asoc->primary_destination;
8465 atomic_add_int(&chk->whoTo->ref_count, 1);
8466 hdr = mtod(op_err, struct sctp_chunkhdr *);
8467 hdr->chunk_type = SCTP_OPERATION_ERROR;
8468 hdr->chunk_flags = 0;
8469 hdr->chunk_length = htons(chk->send_size);
8470 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8473 chk->asoc->ctrl_queue_cnt++;
8477 sctp_send_cookie_echo(struct mbuf *m,
8479 struct sctp_tcb *stcb,
8480 struct sctp_nets *net)
8483 * pull out the cookie and put it at the front of the control chunk
8487 struct mbuf *cookie;
8488 struct sctp_paramhdr parm, *phdr;
8489 struct sctp_chunkhdr *hdr;
8490 struct sctp_tmit_chunk *chk;
8491 uint16_t ptype, plen;
8493 /* First find the cookie in the param area */
8495 at = offset + sizeof(struct sctp_init_chunk);
8497 SCTP_TCB_LOCK_ASSERT(stcb);
8499 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8503 ptype = ntohs(phdr->param_type);
8504 plen = ntohs(phdr->param_length);
8505 if (ptype == SCTP_STATE_COOKIE) {
8508 /* found the cookie */
8509 if ((pad = (plen % 4))) {
8512 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8513 if (cookie == NULL) {
8517 #ifdef SCTP_MBUF_LOGGING
8518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8523 if (SCTP_BUF_IS_EXTENDED(mat)) {
8524 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8526 mat = SCTP_BUF_NEXT(mat);
8532 at += SCTP_SIZE32(plen);
8534 if (cookie == NULL) {
8535 /* Did not find the cookie */
8538 /* ok, we got the cookie lets change it into a cookie echo chunk */
8540 /* first the change from param to cookie */
8541 hdr = mtod(cookie, struct sctp_chunkhdr *);
8542 hdr->chunk_type = SCTP_COOKIE_ECHO;
8543 hdr->chunk_flags = 0;
8544 /* get the chunk stuff now and place it in the FRONT of the queue */
8545 sctp_alloc_a_chunk(stcb, chk);
8548 sctp_m_freem(cookie);
8551 chk->copy_by_ref = 0;
8552 chk->send_size = plen;
8553 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8554 chk->rec.chunk_id.can_take_data = 0;
8555 chk->sent = SCTP_DATAGRAM_UNSENT;
8557 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8558 chk->asoc = &stcb->asoc;
8560 chk->whoTo = chk->asoc->primary_destination;
8561 atomic_add_int(&chk->whoTo->ref_count, 1);
8562 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8563 chk->asoc->ctrl_queue_cnt++;
8568 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8572 struct sctp_nets *net)
8575 * take a HB request and make it into a HB ack and send it.
8577 struct mbuf *outchain;
8578 struct sctp_chunkhdr *chdr;
8579 struct sctp_tmit_chunk *chk;
8583 /* must have a net pointer */
8586 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8587 if (outchain == NULL) {
8588 /* gak out of memory */
8591 #ifdef SCTP_MBUF_LOGGING
8592 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8597 if (SCTP_BUF_IS_EXTENDED(mat)) {
8598 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8600 mat = SCTP_BUF_NEXT(mat);
8604 chdr = mtod(outchain, struct sctp_chunkhdr *);
8605 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8606 chdr->chunk_flags = 0;
8607 if (chk_length % 4) {
8609 uint32_t cpthis = 0;
8612 padlen = 4 - (chk_length % 4);
8613 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8615 sctp_alloc_a_chunk(stcb, chk);
8618 sctp_m_freem(outchain);
8621 chk->copy_by_ref = 0;
8622 chk->send_size = chk_length;
8623 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8624 chk->rec.chunk_id.can_take_data = 1;
8625 chk->sent = SCTP_DATAGRAM_UNSENT;
8628 chk->asoc = &stcb->asoc;
8629 chk->data = outchain;
8631 atomic_add_int(&chk->whoTo->ref_count, 1);
8632 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8633 chk->asoc->ctrl_queue_cnt++;
8637 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8639 /* formulate and queue a cookie-ack back to sender */
8640 struct mbuf *cookie_ack;
8641 struct sctp_chunkhdr *hdr;
8642 struct sctp_tmit_chunk *chk;
8645 SCTP_TCB_LOCK_ASSERT(stcb);
8647 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8648 if (cookie_ack == NULL) {
8652 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8653 sctp_alloc_a_chunk(stcb, chk);
8656 sctp_m_freem(cookie_ack);
8659 chk->copy_by_ref = 0;
8660 chk->send_size = sizeof(struct sctp_chunkhdr);
8661 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8662 chk->rec.chunk_id.can_take_data = 1;
8663 chk->sent = SCTP_DATAGRAM_UNSENT;
8666 chk->asoc = &stcb->asoc;
8667 chk->data = cookie_ack;
8668 if (chk->asoc->last_control_chunk_from != NULL) {
8669 chk->whoTo = chk->asoc->last_control_chunk_from;
8671 chk->whoTo = chk->asoc->primary_destination;
8673 atomic_add_int(&chk->whoTo->ref_count, 1);
8674 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8675 hdr->chunk_type = SCTP_COOKIE_ACK;
8676 hdr->chunk_flags = 0;
8677 hdr->chunk_length = htons(chk->send_size);
8678 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8679 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8680 chk->asoc->ctrl_queue_cnt++;
8686 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8688 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8689 struct mbuf *m_shutdown_ack;
8690 struct sctp_shutdown_ack_chunk *ack_cp;
8691 struct sctp_tmit_chunk *chk;
8693 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8694 if (m_shutdown_ack == NULL) {
8698 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8699 sctp_alloc_a_chunk(stcb, chk);
8702 sctp_m_freem(m_shutdown_ack);
8705 chk->copy_by_ref = 0;
8706 chk->send_size = sizeof(struct sctp_chunkhdr);
8707 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8708 chk->rec.chunk_id.can_take_data = 1;
8709 chk->sent = SCTP_DATAGRAM_UNSENT;
8712 chk->asoc = &stcb->asoc;
8713 chk->data = m_shutdown_ack;
8715 atomic_add_int(&net->ref_count, 1);
8717 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8718 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8719 ack_cp->ch.chunk_flags = 0;
8720 ack_cp->ch.chunk_length = htons(chk->send_size);
8721 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8722 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8723 chk->asoc->ctrl_queue_cnt++;
8728 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8730 /* formulate and queue a SHUTDOWN to the sender */
8731 struct mbuf *m_shutdown;
8732 struct sctp_shutdown_chunk *shutdown_cp;
8733 struct sctp_tmit_chunk *chk;
8735 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8736 if (m_shutdown == NULL) {
8740 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8741 sctp_alloc_a_chunk(stcb, chk);
8744 sctp_m_freem(m_shutdown);
8747 chk->copy_by_ref = 0;
8748 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8749 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8750 chk->rec.chunk_id.can_take_data = 1;
8751 chk->sent = SCTP_DATAGRAM_UNSENT;
8754 chk->asoc = &stcb->asoc;
8755 chk->data = m_shutdown;
8757 atomic_add_int(&net->ref_count, 1);
8759 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8760 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8761 shutdown_cp->ch.chunk_flags = 0;
8762 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8763 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8764 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8765 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8766 chk->asoc->ctrl_queue_cnt++;
8771 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8774 * formulate and queue an ASCONF to the peer. ASCONF parameters
8775 * should be queued on the assoc queue.
8777 struct sctp_tmit_chunk *chk;
8778 struct mbuf *m_asconf;
8781 SCTP_TCB_LOCK_ASSERT(stcb);
8783 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8784 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8785 /* can't send a new one if there is one in flight already */
8788 /* compose an ASCONF chunk, maximum length is PMTU */
8789 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8790 if (m_asconf == NULL) {
8793 sctp_alloc_a_chunk(stcb, chk);
8796 sctp_m_freem(m_asconf);
8799 chk->copy_by_ref = 0;
8800 chk->data = m_asconf;
8801 chk->send_size = len;
8802 chk->rec.chunk_id.id = SCTP_ASCONF;
8803 chk->rec.chunk_id.can_take_data = 0;
8804 chk->sent = SCTP_DATAGRAM_UNSENT;
8806 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8807 chk->asoc = &stcb->asoc;
8809 atomic_add_int(&chk->whoTo->ref_count, 1);
8810 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8811 chk->asoc->ctrl_queue_cnt++;
8816 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8819 * formulate and queue a asconf-ack back to sender. the asconf-ack
8820 * must be stored in the tcb.
8822 struct sctp_tmit_chunk *chk;
8823 struct sctp_asconf_ack *ack, *latest_ack;
8824 struct mbuf *m_ack, *m;
8825 struct sctp_nets *net = NULL;
8827 SCTP_TCB_LOCK_ASSERT(stcb);
8828 /* Get the latest ASCONF-ACK */
8829 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8830 if (latest_ack == NULL) {
8833 if (latest_ack->last_sent_to != NULL &&
8834 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8835 /* we're doing a retransmission */
8836 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8839 if (stcb->asoc.last_control_chunk_from == NULL)
8840 net = stcb->asoc.primary_destination;
8842 net = stcb->asoc.last_control_chunk_from;
8846 if (stcb->asoc.last_control_chunk_from == NULL)
8847 net = stcb->asoc.primary_destination;
8849 net = stcb->asoc.last_control_chunk_from;
8851 latest_ack->last_sent_to = net;
8853 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8854 if (ack->data == NULL) {
8857 /* copy the asconf_ack */
8858 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8859 if (m_ack == NULL) {
8860 /* couldn't copy it */
8863 #ifdef SCTP_MBUF_LOGGING
8864 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8869 if (SCTP_BUF_IS_EXTENDED(mat)) {
8870 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8872 mat = SCTP_BUF_NEXT(mat);
8877 sctp_alloc_a_chunk(stcb, chk);
8881 sctp_m_freem(m_ack);
8884 chk->copy_by_ref = 0;
8891 chk->send_size = ack->len;
8892 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8893 chk->rec.chunk_id.can_take_data = 1;
8894 chk->sent = SCTP_DATAGRAM_UNSENT;
8896 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8897 chk->asoc = &stcb->asoc;
8898 atomic_add_int(&chk->whoTo->ref_count, 1);
8900 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8901 chk->asoc->ctrl_queue_cnt++;
8908 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8909 struct sctp_tcb *stcb,
8910 struct sctp_association *asoc,
8911 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8912 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8918 * send out one MTU of retransmission. If fast_retransmit is
8919 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8920 * rwnd. For a Cookie or Asconf in the control chunk queue we
8921 * retransmit them by themselves.
8923 * For data chunks we will pick out the lowest TSN's in the sent_queue
8924 * marked for resend and bundle them all together (up to a MTU of
8925 * destination). The address to send to should have been
8926 * selected/changed where the retransmission was marked (i.e. in FR
8927 * or t3-timeout routines).
8929 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8930 struct sctp_tmit_chunk *chk, *fwd;
8931 struct mbuf *m, *endofchain;
8932 struct sctp_nets *net = NULL;
8933 uint32_t tsns_sent = 0;
8934 int no_fragmentflg, bundle_at, cnt_thru;
8936 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8937 struct sctp_auth_chunk *auth = NULL;
8938 uint32_t auth_offset = 0;
8939 uint16_t auth_keyid;
8940 int override_ok = 1;
8941 int data_auth_reqd = 0;
8944 SCTP_TCB_LOCK_ASSERT(stcb);
8945 tmr_started = ctl_cnt = bundle_at = error = 0;
8950 endofchain = m = NULL;
8951 auth_keyid = stcb->asoc.authinfo.active_keyid;
8952 #ifdef SCTP_AUDITING_ENABLED
8953 sctp_audit_log(0xC3, 1);
8955 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8956 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8957 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8958 asoc->sent_queue_retran_cnt);
8959 asoc->sent_queue_cnt = 0;
8960 asoc->sent_queue_cnt_removeable = 0;
8961 /* send back 0/0 so we enter normal transmission */
8965 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8966 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8967 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8968 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8969 if (chk->sent != SCTP_DATAGRAM_RESEND) {
8972 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8973 if (chk != asoc->str_reset) {
8975 * not eligible for retran if its
8982 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8987 * Add an AUTH chunk, if chunk requires it save the
8988 * offset into the chain for AUTH
8990 if ((auth == NULL) &&
8991 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8992 stcb->asoc.peer_auth_chunks))) {
8993 m = sctp_add_auth_chunk(m, &endofchain,
8994 &auth, &auth_offset,
8996 chk->rec.chunk_id.id);
8997 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8999 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9005 /* do we have control chunks to retransmit? */
9007 /* Start a timer no matter if we suceed or fail */
9008 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9009 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9010 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9011 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9012 chk->snd_count++; /* update our count */
9013 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9014 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9015 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9016 no_fragmentflg, 0, NULL, 0,
9017 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9018 chk->whoTo->port, so_locked, NULL))) {
9019 SCTP_STAT_INCR(sctps_lowlevelerr);
9022 m = endofchain = NULL;
9026 * We don't want to mark the net->sent time here since this
9027 * we use this for HB and retrans cannot measure RTT
9029 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9031 chk->sent = SCTP_DATAGRAM_SENT;
9032 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9036 /* Clean up the fwd-tsn list */
9037 sctp_clean_up_ctl(stcb, asoc);
9042 * Ok, it is just data retransmission we need to do or that and a
9043 * fwd-tsn with it all.
9045 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9046 return (SCTP_RETRAN_DONE);
9048 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9049 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9050 /* not yet open, resend the cookie and that is it */
9053 #ifdef SCTP_AUDITING_ENABLED
9054 sctp_auditing(20, inp, stcb, NULL);
9056 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9057 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9058 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9059 /* No, not sent to this net or not ready for rtx */
9062 if (chk->data == NULL) {
9063 printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9064 chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9067 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9068 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9069 /* Gak, we have exceeded max unlucky retran, abort! */
9070 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9072 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9073 atomic_add_int(&stcb->asoc.refcnt, 1);
9074 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9075 SCTP_TCB_LOCK(stcb);
9076 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9077 return (SCTP_RETRAN_EXIT);
9079 /* pick up the net */
9081 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9082 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9084 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9087 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9088 /* No room in peers rwnd */
9091 tsn = asoc->last_acked_seq + 1;
9092 if (tsn == chk->rec.data.TSN_seq) {
9094 * we make a special exception for this
9095 * case. The peer has no rwnd but is missing
9096 * the lowest chunk.. which is probably what
9097 * is holding up the rwnd.
9099 goto one_chunk_around;
9104 if (asoc->peers_rwnd < mtu) {
9106 if ((asoc->peers_rwnd == 0) &&
9107 (asoc->total_flight == 0)) {
9108 chk->window_probe = 1;
9109 chk->whoTo->window_probe = 1;
9112 #ifdef SCTP_AUDITING_ENABLED
9113 sctp_audit_log(0xC3, 2);
9117 net->fast_retran_ip = 0;
9118 if (chk->rec.data.doing_fast_retransmit == 0) {
9120 * if no FR in progress skip destination that have
9121 * flight_size > cwnd.
9123 if (net->flight_size >= net->cwnd) {
9128 * Mark the destination net to have FR recovery
9132 net->fast_retran_ip = 1;
9136 * if no AUTH is yet included and this chunk requires it,
9137 * make sure to account for it. We don't apply the size
9138 * until the AUTH chunk is actually added below in case
9139 * there is no room for this chunk.
9141 if (data_auth_reqd && (auth == NULL)) {
9142 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9146 if ((chk->send_size <= (mtu - dmtu)) ||
9147 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9148 /* ok we will add this one */
9149 if (data_auth_reqd) {
9151 m = sctp_add_auth_chunk(m,
9157 auth_keyid = chk->auth_keyid;
9159 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9160 } else if (override_ok) {
9161 auth_keyid = chk->auth_keyid;
9163 } else if (chk->auth_keyid != auth_keyid) {
9164 /* different keyid, so done bundling */
9168 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9170 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9173 /* Do clear IP_DF ? */
9174 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9177 /* upate our MTU size */
9178 if (mtu > (chk->send_size + dmtu))
9179 mtu -= (chk->send_size + dmtu);
9182 data_list[bundle_at++] = chk;
9183 if (one_chunk && (asoc->total_flight <= 0)) {
9184 SCTP_STAT_INCR(sctps_windowprobed);
9187 if (one_chunk == 0) {
9189 * now are there anymore forward from chk to pick
9192 fwd = TAILQ_NEXT(chk, sctp_next);
9194 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9195 /* Nope, not for retran */
9196 fwd = TAILQ_NEXT(fwd, sctp_next);
9199 if (fwd->whoTo != net) {
9200 /* Nope, not the net in question */
9201 fwd = TAILQ_NEXT(fwd, sctp_next);
9204 if (data_auth_reqd && (auth == NULL)) {
9205 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9208 if (fwd->send_size <= (mtu - dmtu)) {
9209 if (data_auth_reqd) {
9211 m = sctp_add_auth_chunk(m,
9217 auth_keyid = fwd->auth_keyid;
9219 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9220 } else if (override_ok) {
9221 auth_keyid = fwd->auth_keyid;
9223 } else if (fwd->auth_keyid != auth_keyid) {
9231 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9233 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9236 /* Do clear IP_DF ? */
9237 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9240 /* upate our MTU size */
9241 if (mtu > (fwd->send_size + dmtu))
9242 mtu -= (fwd->send_size + dmtu);
9245 data_list[bundle_at++] = fwd;
9246 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9249 fwd = TAILQ_NEXT(fwd, sctp_next);
9251 /* can't fit so we are done */
9256 /* Is there something to send for this destination? */
9259 * No matter if we fail/or suceed we should start a
9260 * timer. A failure is like a lost IP packet :-)
9262 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9264 * no timer running on this destination
9267 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9270 /* Now lets send it, if there is anything to send :> */
9271 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9272 (struct sockaddr *)&net->ro._l_addr, m,
9273 auth_offset, auth, auth_keyid,
9274 no_fragmentflg, 0, NULL, 0,
9275 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9276 net->port, so_locked, NULL))) {
9277 /* error, we could not output */
9278 SCTP_STAT_INCR(sctps_lowlevelerr);
9281 m = endofchain = NULL;
9286 * We don't want to mark the net->sent time here
9287 * since this we use this for HB and retrans cannot
9290 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9292 /* For auto-close */
9294 if (*now_filled == 0) {
9295 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9296 *now = asoc->time_last_sent;
9299 asoc->time_last_sent = *now;
9301 *cnt_out += bundle_at;
9302 #ifdef SCTP_AUDITING_ENABLED
9303 sctp_audit_log(0xC4, bundle_at);
9306 tsns_sent = data_list[0]->rec.data.TSN_seq;
9308 for (i = 0; i < bundle_at; i++) {
9309 SCTP_STAT_INCR(sctps_sendretransdata);
9310 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9312 * When we have a revoked data, and we
9313 * retransmit it, then we clear the revoked
9314 * flag since this flag dictates if we
9315 * subtracted from the fs
9317 if (data_list[i]->rec.data.chunk_was_revoked) {
9318 /* Deflate the cwnd */
9319 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9320 data_list[i]->rec.data.chunk_was_revoked = 0;
9322 data_list[i]->snd_count++;
9323 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9324 /* record the time */
9325 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9326 if (data_list[i]->book_size_scale) {
9328 * need to double the book size on
9331 data_list[i]->book_size_scale = 0;
9333 * Since we double the booksize, we
9334 * must also double the output queue
9335 * size, since this get shrunk when
9336 * we free by this amount.
9338 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9339 data_list[i]->book_size *= 2;
9343 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9344 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9345 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9347 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9348 (uint32_t) (data_list[i]->send_size +
9349 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9351 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9352 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9353 data_list[i]->whoTo->flight_size,
9354 data_list[i]->book_size,
9355 (uintptr_t) data_list[i]->whoTo,
9356 data_list[i]->rec.data.TSN_seq);
9358 sctp_flight_size_increase(data_list[i]);
9359 sctp_total_flight_increase(stcb, data_list[i]);
9360 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9361 /* SWS sender side engages */
9362 asoc->peers_rwnd = 0;
9365 (data_list[i]->rec.data.doing_fast_retransmit)) {
9366 SCTP_STAT_INCR(sctps_sendfastretrans);
9367 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9368 (tmr_started == 0)) {
9370 * ok we just fast-retrans'd
9371 * the lowest TSN, i.e the
9372 * first on the list. In
9373 * this case we want to give
9374 * some more time to get a
9375 * SACK back without a
9378 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9379 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9380 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9384 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9385 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9387 #ifdef SCTP_AUDITING_ENABLED
9388 sctp_auditing(21, inp, stcb, NULL);
9394 if (asoc->sent_queue_retran_cnt <= 0) {
9395 /* all done we have no more to retran */
9396 asoc->sent_queue_retran_cnt = 0;
9400 /* No more room in rwnd */
9403 /* stop the for loop here. we sent out a packet */
9411 sctp_timer_validation(struct sctp_inpcb *inp,
9412 struct sctp_tcb *stcb,
9413 struct sctp_association *asoc,
9416 struct sctp_nets *net;
9418 /* Validate that a timer is running somewhere */
9419 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9420 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9421 /* Here is a timer */
9425 SCTP_TCB_LOCK_ASSERT(stcb);
9426 /* Gak, we did not have a timer somewhere */
9427 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9428 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9433 sctp_chunk_output(struct sctp_inpcb *inp,
9434 struct sctp_tcb *stcb,
9437 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9443 * Ok this is the generic chunk service queue. we must do the
9445 * - See if there are retransmits pending, if so we must
9447 * - Service the stream queue that is next, moving any
9448 * message (note I must get a complete message i.e.
9449 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9451 * - Check to see if the cwnd/rwnd allows any output, if so we
9452 * go ahead and fomulate and send the low level chunks. Making sure
9453 * to combine any control in the control chunk queue also.
9455 struct sctp_association *asoc;
9456 struct sctp_nets *net;
9457 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9458 burst_cnt = 0, burst_limit = 0;
9462 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9464 int fr_done, tot_frs = 0;
9467 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9468 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9474 SCTP_TCB_LOCK_ASSERT(stcb);
9476 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9478 if ((un_sent <= 0) &&
9479 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9480 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9481 (asoc->sent_queue_retran_cnt == 0)) {
9482 /* Nothing to do unless there is something to be sent left */
9486 * Do we have something to send, data or control AND a sack timer
9487 * running, if so piggy-back the sack.
9489 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9490 sctp_send_sack(stcb);
9491 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9493 while (asoc->sent_queue_retran_cnt) {
9495 * Ok, it is retransmission time only, we send out only ONE
9496 * packet with a single call off to the retran code.
9498 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9500 * Special hook for handling cookiess discarded
9501 * by peer that carried data. Send cookie-ack only
9502 * and then the next call with get the retran's.
9504 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9506 &now, &now_filled, frag_point, so_locked);
9508 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9509 /* if its not from a HB then do it */
9511 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9517 * its from any other place, we don't allow retran
9518 * output (only control)
9523 /* Can't send anymore */
9525 * now lets push out control by calling med-level
9526 * output once. this assures that we WILL send HB's
9529 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9531 &now, &now_filled, frag_point, so_locked);
9532 #ifdef SCTP_AUDITING_ENABLED
9533 sctp_auditing(8, inp, stcb, NULL);
9535 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9540 * The count was off.. retran is not happening so do
9541 * the normal retransmission.
9543 #ifdef SCTP_AUDITING_ENABLED
9544 sctp_auditing(9, inp, stcb, NULL);
9546 if (ret == SCTP_RETRAN_EXIT) {
9551 if (from_where == SCTP_OUTPUT_FROM_T3) {
9552 /* Only one transmission allowed out of a timeout */
9553 #ifdef SCTP_AUDITING_ENABLED
9554 sctp_auditing(10, inp, stcb, NULL);
9556 /* Push out any control */
9557 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9558 &now, &now_filled, frag_point, so_locked);
9561 if (tot_frs > asoc->max_burst) {
9562 /* Hit FR burst limit */
9565 if ((num_out == 0) && (ret == 0)) {
9567 /* No more retrans to send */
9571 #ifdef SCTP_AUDITING_ENABLED
9572 sctp_auditing(12, inp, stcb, NULL);
9574 /* Check for bad destinations, if they exist move chunks around. */
9575 burst_limit = asoc->max_burst;
9576 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9577 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9578 SCTP_ADDR_NOT_REACHABLE) {
9580 * if possible move things off of this address we
9581 * still may send below due to the dormant state but
9582 * we try to find an alternate address to send to
9583 * and if we have one we move all queued data on the
9584 * out wheel to this alternate address.
9586 if (net->ref_count > 1)
9587 sctp_move_to_an_alt(stcb, asoc, net);
9588 } else if ((asoc->sctp_cmt_on_off == 1) &&
9589 (asoc->sctp_cmt_pf > 0) &&
9590 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9592 * JRS 5/14/07 - If CMT PF is on and the current
9593 * destination is in PF state, move all queued data
9594 * to an alternate desination.
9596 if (net->ref_count > 1)
9597 sctp_move_to_an_alt(stcb, asoc, net);
9600 * if ((asoc->sat_network) || (net->addr_is_local))
9601 * { burst_limit = asoc->max_burst *
9602 * SCTP_SAT_NETWORK_BURST_INCR; }
9604 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9605 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9607 * JRS - Use the congestion control
9608 * given in the congestion control
9611 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9612 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9613 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9615 SCTP_STAT_INCR(sctps_maxburstqueued);
9617 net->fast_retran_ip = 0;
9619 if (net->flight_size == 0) {
9620 /* Should be decaying the cwnd here */
9629 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9630 &reason_code, 0, from_where,
9631 &now, &now_filled, frag_point, so_locked);
9633 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9634 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9635 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9637 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9638 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9639 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9643 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9647 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9648 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9650 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9655 * When nagle is on, we look at how much is un_sent, then
9656 * if its smaller than an MTU and we have data in
9659 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9660 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9661 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9662 (stcb->asoc.total_flight > 0)) {
9666 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9667 TAILQ_EMPTY(&asoc->send_queue) &&
9668 TAILQ_EMPTY(&asoc->out_wheel)) {
9669 /* Nothing left to send */
9672 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9673 /* Nothing left to send */
9676 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9677 (burst_cnt < burst_limit)));
9679 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9680 if (burst_cnt >= burst_limit) {
9681 SCTP_STAT_INCR(sctps_maxburstqueued);
9682 asoc->burst_limit_applied = 1;
9683 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9684 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9687 asoc->burst_limit_applied = 0;
9690 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9691 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9693 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9697 * Now we need to clean up the control chunk chain if a ECNE is on
9698 * it. It must be marked as UNSENT again so next call will continue
9699 * to send it until such time that we get a CWR, to remove it.
9701 if (stcb->asoc.ecn_echo_cnt_onq)
9702 sctp_fix_ecn_echo(asoc);
9708 sctp_output(inp, m, addr, control, p, flags)
9709 struct sctp_inpcb *inp;
9711 struct sockaddr *addr;
9712 struct mbuf *control;
9717 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9720 if (inp->sctp_socket == NULL) {
9721 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9724 return (sctp_sosend(inp->sctp_socket,
9734 send_forward_tsn(struct sctp_tcb *stcb,
9735 struct sctp_association *asoc)
9737 struct sctp_tmit_chunk *chk;
9738 struct sctp_forward_tsn_chunk *fwdtsn;
9739 uint32_t advance_peer_ack_point;
9741 SCTP_TCB_LOCK_ASSERT(stcb);
9742 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9743 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9744 /* mark it to unsent */
9745 chk->sent = SCTP_DATAGRAM_UNSENT;
9747 /* Do we correct its output location? */
9748 if (chk->whoTo != asoc->primary_destination) {
9749 sctp_free_remote_addr(chk->whoTo);
9750 chk->whoTo = asoc->primary_destination;
9751 atomic_add_int(&chk->whoTo->ref_count, 1);
9753 goto sctp_fill_in_rest;
9756 /* Ok if we reach here we must build one */
9757 sctp_alloc_a_chunk(stcb, chk);
9761 asoc->fwd_tsn_cnt++;
9762 chk->copy_by_ref = 0;
9763 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9764 chk->rec.chunk_id.can_take_data = 0;
9767 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9768 if (chk->data == NULL) {
9769 sctp_free_a_chunk(stcb, chk);
9772 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9773 chk->sent = SCTP_DATAGRAM_UNSENT;
9775 chk->whoTo = asoc->primary_destination;
9776 atomic_add_int(&chk->whoTo->ref_count, 1);
9777 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9778 asoc->ctrl_queue_cnt++;
9781 * Here we go through and fill out the part that deals with
9782 * stream/seq of the ones we skip.
9784 SCTP_BUF_LEN(chk->data) = 0;
9786 struct sctp_tmit_chunk *at, *tp1, *last;
9787 struct sctp_strseq *strseq;
9788 unsigned int cnt_of_space, i, ovh;
9789 unsigned int space_needed;
9790 unsigned int cnt_of_skipped = 0;
9792 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9793 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9794 /* no more to look at */
9797 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9798 /* We don't report these */
9803 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9804 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9806 cnt_of_space = M_TRAILINGSPACE(chk->data);
9808 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9809 ovh = SCTP_MIN_OVERHEAD;
9811 ovh = SCTP_MIN_V4_OVERHEAD;
9813 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9814 /* trim to a mtu size */
9815 cnt_of_space = asoc->smallest_mtu - ovh;
9817 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9818 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9819 0xff, 0, cnt_of_skipped,
9820 asoc->advanced_peer_ack_point);
9823 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9824 if (cnt_of_space < space_needed) {
9826 * ok we must trim down the chunk by lowering the
9827 * advance peer ack point.
9829 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9830 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9831 0xff, 0xff, cnt_of_space,
9834 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9835 cnt_of_skipped /= sizeof(struct sctp_strseq);
9837 * Go through and find the TSN that will be the one
9840 at = TAILQ_FIRST(&asoc->sent_queue);
9841 for (i = 0; i < cnt_of_skipped; i++) {
9842 tp1 = TAILQ_NEXT(at, sctp_next);
9848 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9849 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9850 0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9851 asoc->advanced_peer_ack_point);
9855 * last now points to last one I can report, update
9859 advance_peer_ack_point = last->rec.data.TSN_seq;
9860 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9861 cnt_of_skipped * sizeof(struct sctp_strseq);
9863 chk->send_size = space_needed;
9864 /* Setup the chunk */
9865 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9866 fwdtsn->ch.chunk_length = htons(chk->send_size);
9867 fwdtsn->ch.chunk_flags = 0;
9868 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9869 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9870 SCTP_BUF_LEN(chk->data) = chk->send_size;
9873 * Move pointer to after the fwdtsn and transfer to the
9876 strseq = (struct sctp_strseq *)fwdtsn;
9878 * Now populate the strseq list. This is done blindly
9879 * without pulling out duplicate stream info. This is
9880 * inefficent but won't harm the process since the peer will
9881 * look at these in sequence and will thus release anything.
9882 * It could mean we exceed the PMTU and chop off some that
9883 * we could have included.. but this is unlikely (aka 1432/4
9884 * would mean 300+ stream seq's would have to be reported in
9885 * one FWD-TSN. With a bit of work we can later FIX this to
9886 * optimize and pull out duplcates.. but it does add more
9887 * overhead. So for now... not!
9889 at = TAILQ_FIRST(&asoc->sent_queue);
9890 for (i = 0; i < cnt_of_skipped; i++) {
9891 tp1 = TAILQ_NEXT(at, sctp_next);
9894 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9895 /* We don't report these */
9900 if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9901 at->rec.data.fwd_tsn_cnt = 0;
9903 strseq->stream = ntohs(at->rec.data.stream_number);
9904 strseq->sequence = ntohs(at->rec.data.stream_seq);
9914 sctp_send_sack(struct sctp_tcb *stcb)
9917 * Queue up a SACK or NR-SACK in the control queue.
9918 * We must first check to see if a SACK or NR-SACK is
9919 * somehow on the control queue.
9920 * If so, we will take and and remove the old one.
9922 struct sctp_association *asoc;
9923 struct sctp_tmit_chunk *chk, *a_chk;
9924 struct sctp_sack_chunk *sack;
9925 struct sctp_nr_sack_chunk *nr_sack;
9926 struct sctp_gap_ack_block *gap_descriptor;
9927 struct sack_track *selector;
9932 int limit_reached = 0;
9933 unsigned int i, sel_start, siz, j, starting_index;
9934 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9937 uint32_t highest_tsn;
9941 if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
9942 (stcb->asoc.peer_supports_nr_sack == 1)) {
9943 type = SCTP_NR_SELECTIVE_ACK;
9945 type = SCTP_SELECTIVE_ACK;
9949 SCTP_TCB_LOCK_ASSERT(stcb);
9950 if (asoc->last_data_chunk_from == NULL) {
9951 /* Hmm we never received anything */
9954 sctp_slide_mapping_arrays(stcb);
9955 sctp_set_rwnd(stcb, asoc);
9956 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9957 if (chk->rec.chunk_id.id == type) {
9958 /* Hmm, found a sack already on queue, remove it */
9959 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9960 asoc->ctrl_queue_cnt++;
9963 sctp_m_freem(a_chk->data);
9966 sctp_free_remote_addr(a_chk->whoTo);
9967 a_chk->whoTo = NULL;
9971 if (a_chk == NULL) {
9972 sctp_alloc_a_chunk(stcb, a_chk);
9973 if (a_chk == NULL) {
9974 /* No memory so we drop the idea, and set a timer */
9975 if (stcb->asoc.delayed_ack) {
9976 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9977 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9978 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9979 stcb->sctp_ep, stcb, NULL);
9981 stcb->asoc.send_sack = 1;
9985 a_chk->copy_by_ref = 0;
9986 a_chk->rec.chunk_id.id = type;
9987 a_chk->rec.chunk_id.can_take_data = 1;
9989 /* Clear our pkt counts */
9990 asoc->data_pkts_seen = 0;
9993 a_chk->snd_count = 0;
9994 a_chk->send_size = 0; /* fill in later */
9995 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9996 a_chk->whoTo = NULL;
9998 if ((asoc->numduptsns) ||
9999 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
10002 * Ok, we have some duplicates or the destination for the
10003 * sack is unreachable, lets see if we can select an
10004 * alternate than asoc->last_data_chunk_from
10006 if ((!(asoc->last_data_chunk_from->dest_state &
10007 SCTP_ADDR_NOT_REACHABLE)) &&
10008 (asoc->used_alt_onsack > asoc->numnets)) {
10009 /* We used an alt last time, don't this time */
10010 a_chk->whoTo = NULL;
10012 asoc->used_alt_onsack++;
10013 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10015 if (a_chk->whoTo == NULL) {
10016 /* Nope, no alternate */
10017 a_chk->whoTo = asoc->last_data_chunk_from;
10018 asoc->used_alt_onsack = 0;
10022 * No duplicates so we use the last place we received data
10025 asoc->used_alt_onsack = 0;
10026 a_chk->whoTo = asoc->last_data_chunk_from;
10028 if (a_chk->whoTo) {
10029 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10031 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
10032 highest_tsn = asoc->highest_tsn_inside_map;
10034 highest_tsn = asoc->highest_tsn_inside_nr_map;
10036 if (highest_tsn == asoc->cumulative_tsn) {
10038 if (type == SCTP_SELECTIVE_ACK) {
10039 space_req = sizeof(struct sctp_sack_chunk);
10041 space_req = sizeof(struct sctp_nr_sack_chunk);
10044 /* gaps get a cluster */
10045 space_req = MCLBYTES;
10047 /* Ok now lets formulate a MBUF with our sack */
10048 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10049 if ((a_chk->data == NULL) ||
10050 (a_chk->whoTo == NULL)) {
10051 /* rats, no mbuf memory */
10053 /* was a problem with the destination */
10054 sctp_m_freem(a_chk->data);
10055 a_chk->data = NULL;
10057 sctp_free_a_chunk(stcb, a_chk);
10058 /* sa_ignore NO_NULL_CHK */
10059 if (stcb->asoc.delayed_ack) {
10060 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10061 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10062 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10063 stcb->sctp_ep, stcb, NULL);
10065 stcb->asoc.send_sack = 1;
10069 /* ok, lets go through and fill it in */
10070 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10071 space = M_TRAILINGSPACE(a_chk->data);
10072 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10073 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10075 limit = mtod(a_chk->data, caddr_t);
10078 /* 0x01 is used by nonce for ecn */
10079 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10080 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10081 (asoc->peer_supports_ecn_nonce))
10082 flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10086 if ((asoc->sctp_cmt_on_off == 1) &&
10087 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10089 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10090 * received, then set high bit to 1, else 0. Reset
10093 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10094 asoc->cmt_dac_pkts_rcvd = 0;
10096 #ifdef SCTP_ASOCLOG_OF_TSNS
10097 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10098 stcb->asoc.cumack_log_atsnt++;
10099 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10100 stcb->asoc.cumack_log_atsnt = 0;
10103 /* reset the readers interpretation */
10104 stcb->freed_by_sorcv_sincelast = 0;
10106 if (type == SCTP_SELECTIVE_ACK) {
10107 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10109 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10110 if (highest_tsn > asoc->mapping_array_base_tsn) {
10111 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10113 siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10117 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10118 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10119 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10120 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10122 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10126 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10129 * The base TSN is intialized to be the first TSN the peer
10130 * will send us. If the cum-ack is behind this then when they
10131 * send us the next in sequence it will mark the base_tsn bit.
10132 * Thus we need to use the very first selector and the offset
10133 * is 1. Our table is built for this case.
10135 starting_index = 0;
10139 * we skip the first selector when the cum-ack is at or above the
10140 * mapping array base. This is because the bits at the base or above
10141 * are turned on and our first selector in the table assumes they are
10142 * off. We thus will use the second selector (first is 0). We use
10143 * the reverse of our macro to fix the offset, in bits, that our
10144 * table is at. Note that this method assumes that the cum-tsn is
10145 * within the first bit, i.e. its value is 0-7 which means the
10146 * result to our offset will be either a 0 - -7. If the cumack
10147 * is NOT in the first byte (0) (which it should be since we did
10148 * a mapping array slide above) then we need to calculate the starting
10149 * index i.e. which byte of the mapping array we should start at. We
10150 * do this by dividing by 8 and pushing the remainder (mod) into offset.
10151 * then we multiply the offset to be negative, since we need a negative
10152 * offset into the selector table.
10154 SCTP_CALC_TSN_TO_GAP(offset, asoc->cumulative_tsn, asoc->mapping_array_base_tsn);
10156 starting_index = offset / 8;
10157 offset = offset % 8;
10158 printf("Strange starting index is %d offset:%d (not 0/x)\n",
10159 starting_index, offset);
10161 starting_index = 0;
10163 /* We need a negative offset in our table */
10167 if (((type == SCTP_SELECTIVE_ACK) &&
10168 compare_with_wrap(highest_tsn, asoc->cumulative_tsn, MAX_TSN)) ||
10169 ((type == SCTP_NR_SELECTIVE_ACK) &&
10170 compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN))) {
10171 /* we have a gap .. maybe */
10172 for (i = starting_index; i < siz; i++) {
10173 if (type == SCTP_SELECTIVE_ACK) {
10174 selector = &sack_array[asoc->mapping_array[i] | asoc->nr_mapping_array[i]];
10176 selector = &sack_array[asoc->mapping_array[i]];
10178 if (mergeable && selector->right_edge) {
10180 * Backup, left and right edges were ok to
10186 if (selector->num_entries == 0)
10189 for (j = sel_start; j < selector->num_entries; j++) {
10190 if (mergeable && selector->right_edge) {
10192 * do a merge by NOT setting
10198 * no merge, set the left
10202 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10204 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10207 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10213 if (selector->left_edge) {
10217 if (limit_reached) {
10218 /* Reached the limit stop */
10225 if ((type == SCTP_NR_SELECTIVE_ACK) &&
10226 (limit_reached == 0)) {
10230 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn)
10231 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10233 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10235 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10238 * cum-ack behind the mapping array, so we start and use all
10243 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10245 * we skip the first one when the cum-ack is at or above the
10246 * mapping array base. Note this only works if
10250 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10251 /* we have a gap .. maybe */
10252 for (i = 0; i < siz; i++) {
10253 selector = &sack_array[asoc->nr_mapping_array[i]];
10254 if (mergeable && selector->right_edge) {
10256 * Backup, left and right edges were
10259 num_nr_gap_blocks--;
10262 if (selector->num_entries == 0)
10265 for (j = sel_start; j < selector->num_entries; j++) {
10266 if (mergeable && selector->right_edge) {
10268 * do a merge by NOT
10275 * no merge, set the
10279 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10281 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10282 num_nr_gap_blocks++;
10284 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10290 if (selector->left_edge) {
10294 if (limit_reached) {
10295 /* Reached the limit stop */
10303 /* now we must add any dups we are going to report. */
10304 if ((limit_reached == 0) && (asoc->numduptsns)) {
10305 dup = (uint32_t *) gap_descriptor;
10306 for (i = 0; i < asoc->numduptsns; i++) {
10307 *dup = htonl(asoc->dup_tsns[i]);
10310 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10315 asoc->numduptsns = 0;
10318 * now that the chunk is prepared queue it to the control chunk
10321 if (type == SCTP_SELECTIVE_ACK) {
10322 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10323 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10324 num_dups * sizeof(int32_t);
10325 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10326 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10327 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10328 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10329 sack->sack.num_dup_tsns = htons(num_dups);
10330 sack->ch.chunk_type = type;
10331 sack->ch.chunk_flags = flags;
10332 sack->ch.chunk_length = htons(a_chk->send_size);
10334 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10335 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10336 num_dups * sizeof(int32_t);
10337 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10338 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10339 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10340 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10341 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10342 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10343 nr_sack->nr_sack.reserved = 0;
10344 nr_sack->ch.chunk_type = type;
10345 nr_sack->ch.chunk_flags = flags;
10346 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10348 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10349 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10350 asoc->ctrl_queue_cnt++;
10351 asoc->send_sack = 0;
10352 SCTP_STAT_INCR(sctps_sendsacks);
10357 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10358 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10363 struct mbuf *m_abort;
10364 struct mbuf *m_out = NULL, *m_end = NULL;
10365 struct sctp_abort_chunk *abort = NULL;
10367 uint32_t auth_offset = 0;
10368 struct sctp_auth_chunk *auth = NULL;
10371 * Add an AUTH chunk, if chunk requires it and save the offset into
10372 * the chain for AUTH
10374 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10375 stcb->asoc.peer_auth_chunks)) {
10376 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10377 stcb, SCTP_ABORT_ASSOCIATION);
10378 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10380 SCTP_TCB_LOCK_ASSERT(stcb);
10381 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10382 if (m_abort == NULL) {
10385 sctp_m_freem(m_out);
10388 /* link in any error */
10389 SCTP_BUF_NEXT(m_abort) = operr;
10396 sz += SCTP_BUF_LEN(n);
10397 n = SCTP_BUF_NEXT(n);
10400 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10401 if (m_out == NULL) {
10402 /* NO Auth chunk prepended, so reserve space in front */
10403 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10406 /* Put AUTH chunk at the front of the chain */
10407 SCTP_BUF_NEXT(m_end) = m_abort;
10410 /* fill in the ABORT chunk */
10411 abort = mtod(m_abort, struct sctp_abort_chunk *);
10412 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10413 abort->ch.chunk_flags = 0;
10414 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10416 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10417 stcb->asoc.primary_destination,
10418 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10419 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10420 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10421 stcb->asoc.primary_destination->port, so_locked, NULL);
10422 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10426 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10427 struct sctp_nets *net,
10430 /* formulate and SEND a SHUTDOWN-COMPLETE */
10431 struct mbuf *m_shutdown_comp;
10432 struct sctp_shutdown_complete_chunk *shutdown_complete;
10436 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10437 if (m_shutdown_comp == NULL) {
10441 if (reflect_vtag) {
10442 flags = SCTP_HAD_NO_TCB;
10443 vtag = stcb->asoc.my_vtag;
10446 vtag = stcb->asoc.peer_vtag;
10448 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10449 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10450 shutdown_complete->ch.chunk_flags = flags;
10451 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10452 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10453 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10454 (struct sockaddr *)&net->ro._l_addr,
10455 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10456 stcb->sctp_ep->sctp_lport, stcb->rport,
10458 net->port, SCTP_SO_NOT_LOCKED, NULL);
10459 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10464 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10465 uint32_t vrf_id, uint16_t port)
10467 /* formulate and SEND a SHUTDOWN-COMPLETE */
10468 struct mbuf *o_pak;
10470 struct ip *iph, *iph_out;
10471 struct udphdr *udp = NULL;
10474 struct ip6_hdr *ip6, *ip6_out;
10477 int offset_out, len, mlen;
10478 struct sctp_shutdown_complete_msg *comp_cp;
10480 iph = mtod(m, struct ip *);
10481 switch (iph->ip_v) {
10483 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10486 case IPV6_VERSION >> 4:
10487 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10494 len += sizeof(struct udphdr);
10496 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10497 if (mout == NULL) {
10500 SCTP_BUF_RESV_UF(mout, max_linkhdr);
10501 SCTP_BUF_LEN(mout) = len;
10502 SCTP_BUF_NEXT(mout) = NULL;
10509 switch (iph->ip_v) {
10511 iph_out = mtod(mout, struct ip *);
10513 /* Fill in the IP header for the ABORT */
10514 iph_out->ip_v = IPVERSION;
10515 iph_out->ip_hl = (sizeof(struct ip) / 4);
10516 iph_out->ip_tos = (u_char)0;
10517 iph_out->ip_id = 0;
10518 iph_out->ip_off = 0;
10519 iph_out->ip_ttl = MAXTTL;
10521 iph_out->ip_p = IPPROTO_UDP;
10523 iph_out->ip_p = IPPROTO_SCTP;
10525 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10526 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10528 /* let IP layer calculate this */
10529 iph_out->ip_sum = 0;
10530 offset_out += sizeof(*iph_out);
10531 comp_cp = (struct sctp_shutdown_complete_msg *)(
10532 (caddr_t)iph_out + offset_out);
10535 case IPV6_VERSION >> 4:
10536 ip6 = (struct ip6_hdr *)iph;
10537 ip6_out = mtod(mout, struct ip6_hdr *);
10539 /* Fill in the IPv6 header for the ABORT */
10540 ip6_out->ip6_flow = ip6->ip6_flow;
10541 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10543 ip6_out->ip6_nxt = IPPROTO_UDP;
10545 ip6_out->ip6_nxt = IPPROTO_SCTP;
10547 ip6_out->ip6_src = ip6->ip6_dst;
10548 ip6_out->ip6_dst = ip6->ip6_src;
10550 * ?? The old code had both the iph len + payload, I think
10551 * this is wrong and would never have worked
10553 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10554 offset_out += sizeof(*ip6_out);
10555 comp_cp = (struct sctp_shutdown_complete_msg *)(
10556 (caddr_t)ip6_out + offset_out);
10560 /* Currently not supported. */
10561 sctp_m_freem(mout);
10565 udp = (struct udphdr *)comp_cp;
10566 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10567 udp->uh_dport = port;
10568 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10570 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10571 offset_out += sizeof(struct udphdr);
10572 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10574 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10576 sctp_m_freem(mout);
10579 /* Now copy in and fill in the ABORT tags etc. */
10580 comp_cp->sh.src_port = sh->dest_port;
10581 comp_cp->sh.dest_port = sh->src_port;
10582 comp_cp->sh.checksum = 0;
10583 comp_cp->sh.v_tag = sh->v_tag;
10584 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10585 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10586 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10588 if (iph_out != NULL) {
10592 mlen = SCTP_BUF_LEN(mout);
10593 bzero(&ro, sizeof ro);
10594 /* set IPv4 length */
10595 iph_out->ip_len = mlen;
10596 #ifdef SCTP_PACKET_LOGGING
10597 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10598 sctp_packet_log(mout, mlen);
10601 #if defined(SCTP_WITH_NO_CSUM)
10602 SCTP_STAT_INCR(sctps_sendnocrc);
10604 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10605 SCTP_STAT_INCR(sctps_sendswcrc);
10607 SCTP_ENABLE_UDP_CSUM(mout);
10609 #if defined(SCTP_WITH_NO_CSUM)
10610 SCTP_STAT_INCR(sctps_sendnocrc);
10612 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10613 mout->m_pkthdr.csum_data = 0;
10614 SCTP_STAT_INCR(sctps_sendhwcrc);
10617 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10619 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
10621 /* Free the route if we got one back */
10626 if (ip6_out != NULL) {
10627 struct route_in6 ro;
10629 struct ifnet *ifp = NULL;
10631 bzero(&ro, sizeof(ro));
10632 mlen = SCTP_BUF_LEN(mout);
10633 #ifdef SCTP_PACKET_LOGGING
10634 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10635 sctp_packet_log(mout, mlen);
10637 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10639 #if defined(SCTP_WITH_NO_CSUM)
10640 SCTP_STAT_INCR(sctps_sendnocrc);
10642 comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10643 SCTP_STAT_INCR(sctps_sendswcrc);
10645 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10646 udp->uh_sum = 0xffff;
10649 #if defined(SCTP_WITH_NO_CSUM)
10650 SCTP_STAT_INCR(sctps_sendnocrc);
10652 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10653 mout->m_pkthdr.csum_data = 0;
10654 SCTP_STAT_INCR(sctps_sendhwcrc);
10657 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
10659 /* Free the route if we got one back */
10664 SCTP_STAT_INCR(sctps_sendpackets);
10665 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10666 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10671 static struct sctp_nets *
10672 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10674 struct sctp_nets *net, *hnet;
10675 int ms_goneby, highest_ms, state_overide = 0;
10677 (void)SCTP_GETTIME_TIMEVAL(now);
10680 SCTP_TCB_LOCK_ASSERT(stcb);
10681 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10683 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10684 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10687 * Skip this guy from consideration if HB is off AND
10692 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10693 /* skip this dest net from consideration */
10696 if (net->last_sent_time.tv_sec) {
10697 /* Sent to so we subtract */
10698 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10700 /* Never been sent to */
10701 ms_goneby = 0x7fffffff;
10703 * When the address state is unconfirmed but still
10704 * considered reachable, we HB at a higher rate. Once it
10705 * goes confirmed OR reaches the "unreachable" state, thenw
10706 * we cut it back to HB at a more normal pace.
10708 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10714 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10715 (ms_goneby > highest_ms)) {
10716 highest_ms = ms_goneby;
10721 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10727 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10729 * Found the one with longest delay bounds OR it is
10730 * unconfirmed and still not marked unreachable.
10732 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10735 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10736 (struct sockaddr *)&hnet->ro._l_addr);
10738 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10741 /* update the timer now */
10742 hnet->last_sent_time = *now;
10745 /* Nothing to HB */
10750 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10752 struct sctp_tmit_chunk *chk;
10753 struct sctp_nets *net;
10754 struct sctp_heartbeat_chunk *hb;
10755 struct timeval now;
10756 struct sockaddr_in *sin;
10757 struct sockaddr_in6 *sin6;
10759 SCTP_TCB_LOCK_ASSERT(stcb);
10760 if (user_req == 0) {
10761 net = sctp_select_hb_destination(stcb, &now);
10764 * All our busy none to send to, just start the
10767 if (stcb->asoc.state == 0) {
10770 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10781 (void)SCTP_GETTIME_TIMEVAL(&now);
10783 sin = (struct sockaddr_in *)&net->ro._l_addr;
10784 if (sin->sin_family != AF_INET) {
10785 if (sin->sin_family != AF_INET6) {
10790 sctp_alloc_a_chunk(stcb, chk);
10792 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10795 chk->copy_by_ref = 0;
10796 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10797 chk->rec.chunk_id.can_take_data = 1;
10798 chk->asoc = &stcb->asoc;
10799 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10801 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10802 if (chk->data == NULL) {
10803 sctp_free_a_chunk(stcb, chk);
10806 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10807 SCTP_BUF_LEN(chk->data) = chk->send_size;
10808 chk->sent = SCTP_DATAGRAM_UNSENT;
10809 chk->snd_count = 0;
10811 atomic_add_int(&chk->whoTo->ref_count, 1);
10812 /* Now we have a mbuf that we can fill in with the details */
10813 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10814 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10815 /* fill out chunk header */
10816 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10817 hb->ch.chunk_flags = 0;
10818 hb->ch.chunk_length = htons(chk->send_size);
10819 /* Fill out hb parameter */
10820 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10821 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10822 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10823 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10824 /* Did our user request this one, put it in */
10825 hb->heartbeat.hb_info.user_req = user_req;
10826 hb->heartbeat.hb_info.addr_family = sin->sin_family;
10827 hb->heartbeat.hb_info.addr_len = sin->sin_len;
10828 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10830 * we only take from the entropy pool if the address is not
10833 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10834 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10836 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10837 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10839 if (sin->sin_family == AF_INET) {
10840 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10841 } else if (sin->sin_family == AF_INET6) {
10842 /* We leave the scope the way it is in our lookup table. */
10843 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10844 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10846 /* huh compiler bug */
10851 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10852 * PF-heartbeats. Because of this, threshold management is done by
10853 * the t3 timer handler, and does not need to be done upon the send
10854 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10855 * heartbeat is being sent is in PF state, do NOT do threshold
10858 if ((stcb->asoc.sctp_cmt_pf == 0) ||
10859 ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10860 /* ok we have a destination that needs a beat */
10861 /* lets do the theshold management Qiaobing style */
10862 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10863 stcb->asoc.max_send_times)) {
10865 * we have lost the association, in a way this is
10866 * quite bad since we really are one less time since
10867 * we really did not send yet. This is the down side
10868 * to the Q's style as defined in the RFC and not my
10869 * alternate style defined in the RFC.
10871 if (chk->data != NULL) {
10872 sctp_m_freem(chk->data);
10876 * Here we do NOT use the macro since the
10877 * association is now gone.
10880 sctp_free_remote_addr(chk->whoTo);
10883 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10887 net->hb_responded = 0;
10888 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10889 stcb->asoc.ctrl_queue_cnt++;
10890 SCTP_STAT_INCR(sctps_sendheartbeat);
10892 * Call directly med level routine to put out the chunk. It will
10893 * always tumble out control chunks aka HB but it may even tumble
10900 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10903 struct sctp_association *asoc;
10904 struct sctp_ecne_chunk *ecne;
10905 struct sctp_tmit_chunk *chk;
10907 asoc = &stcb->asoc;
10908 SCTP_TCB_LOCK_ASSERT(stcb);
10909 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10910 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10911 /* found a previous ECN_ECHO update it if needed */
10912 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10913 ecne->tsn = htonl(high_tsn);
10917 /* nope could not find one to update so we must build one */
10918 sctp_alloc_a_chunk(stcb, chk);
10922 chk->copy_by_ref = 0;
10923 SCTP_STAT_INCR(sctps_sendecne);
10924 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10925 chk->rec.chunk_id.can_take_data = 0;
10926 chk->asoc = &stcb->asoc;
10927 chk->send_size = sizeof(struct sctp_ecne_chunk);
10928 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10929 if (chk->data == NULL) {
10930 sctp_free_a_chunk(stcb, chk);
10933 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10934 SCTP_BUF_LEN(chk->data) = chk->send_size;
10935 chk->sent = SCTP_DATAGRAM_UNSENT;
10936 chk->snd_count = 0;
10938 atomic_add_int(&chk->whoTo->ref_count, 1);
10939 stcb->asoc.ecn_echo_cnt_onq++;
10940 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10941 ecne->ch.chunk_type = SCTP_ECN_ECHO;
10942 ecne->ch.chunk_flags = 0;
10943 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10944 ecne->tsn = htonl(high_tsn);
10945 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10946 asoc->ctrl_queue_cnt++;
10950 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10951 struct mbuf *m, int iphlen, int bad_crc)
10953 struct sctp_association *asoc;
10954 struct sctp_pktdrop_chunk *drp;
10955 struct sctp_tmit_chunk *chk;
10962 struct ip6_hdr *ip6h;
10965 int fullsz = 0, extra = 0;
10968 struct sctp_chunkhdr *ch, chunk_buf;
10969 unsigned int chk_length;
10974 asoc = &stcb->asoc;
10975 SCTP_TCB_LOCK_ASSERT(stcb);
10976 if (asoc->peer_supports_pktdrop == 0) {
10978 * peer must declare support before I send one.
10982 if (stcb->sctp_socket == NULL) {
10985 sctp_alloc_a_chunk(stcb, chk);
10989 chk->copy_by_ref = 0;
10990 iph = mtod(m, struct ip *);
10992 sctp_free_a_chunk(stcb, chk);
10995 switch (iph->ip_v) {
10998 len = chk->send_size = iph->ip_len;
11001 case IPV6_VERSION >> 4:
11003 ip6h = mtod(m, struct ip6_hdr *);
11004 len = chk->send_size = htons(ip6h->ip6_plen);
11010 /* Validate that we do not have an ABORT in here. */
11011 offset = iphlen + sizeof(struct sctphdr);
11012 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11013 sizeof(*ch), (uint8_t *) & chunk_buf);
11014 while (ch != NULL) {
11015 chk_length = ntohs(ch->chunk_length);
11016 if (chk_length < sizeof(*ch)) {
11017 /* break to abort land */
11020 switch (ch->chunk_type) {
11021 case SCTP_PACKET_DROPPED:
11022 case SCTP_ABORT_ASSOCIATION:
11023 case SCTP_INITIATION_ACK:
11025 * We don't respond with an PKT-DROP to an ABORT
11026 * or PKT-DROP. We also do not respond to an
11027 * INIT-ACK, because we can't know if the initiation
11028 * tag is correct or not.
11030 sctp_free_a_chunk(stcb, chk);
11035 offset += SCTP_SIZE32(chk_length);
11036 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11037 sizeof(*ch), (uint8_t *) & chunk_buf);
11040 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11041 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11043 * only send 1 mtu worth, trim off the excess on the end.
11045 fullsz = len - extra;
11046 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11049 chk->asoc = &stcb->asoc;
11050 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11051 if (chk->data == NULL) {
11053 sctp_free_a_chunk(stcb, chk);
11056 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11057 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11059 sctp_m_freem(chk->data);
11063 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11064 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11065 chk->book_size_scale = 0;
11067 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11068 drp->trunc_len = htons(fullsz);
11070 * Len is already adjusted to size minus overhead above take
11071 * out the pkt_drop chunk itself from it.
11073 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11074 len = chk->send_size;
11076 /* no truncation needed */
11077 drp->ch.chunk_flags = 0;
11078 drp->trunc_len = htons(0);
11081 drp->ch.chunk_flags |= SCTP_BADCRC;
11083 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11084 SCTP_BUF_LEN(chk->data) = chk->send_size;
11085 chk->sent = SCTP_DATAGRAM_UNSENT;
11086 chk->snd_count = 0;
11088 /* we should hit here */
11091 chk->whoTo = asoc->primary_destination;
11093 atomic_add_int(&chk->whoTo->ref_count, 1);
11094 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11095 chk->rec.chunk_id.can_take_data = 1;
11096 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11097 drp->ch.chunk_length = htons(chk->send_size);
11098 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11102 drp->bottle_bw = htonl(spc);
11103 if (asoc->my_rwnd) {
11104 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11105 asoc->size_on_all_streams +
11106 asoc->my_rwnd_control_len +
11107 stcb->sctp_socket->so_rcv.sb_cc);
11110 * If my rwnd is 0, possibly from mbuf depletion as well as
11111 * space used, tell the peer there is NO space aka onq == bw
11113 drp->current_onq = htonl(spc);
11117 m_copydata(m, iphlen, len, (caddr_t)datap);
11118 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11119 asoc->ctrl_queue_cnt++;
11123 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11125 struct sctp_association *asoc;
11126 struct sctp_cwr_chunk *cwr;
11127 struct sctp_tmit_chunk *chk;
11129 asoc = &stcb->asoc;
11130 SCTP_TCB_LOCK_ASSERT(stcb);
11131 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11132 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11133 /* found a previous ECN_CWR update it if needed */
11134 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11135 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11137 cwr->tsn = htonl(high_tsn);
11142 /* nope could not find one to update so we must build one */
11143 sctp_alloc_a_chunk(stcb, chk);
11147 chk->copy_by_ref = 0;
11148 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11149 chk->rec.chunk_id.can_take_data = 1;
11150 chk->asoc = &stcb->asoc;
11151 chk->send_size = sizeof(struct sctp_cwr_chunk);
11152 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11153 if (chk->data == NULL) {
11154 sctp_free_a_chunk(stcb, chk);
11157 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11158 SCTP_BUF_LEN(chk->data) = chk->send_size;
11159 chk->sent = SCTP_DATAGRAM_UNSENT;
11160 chk->snd_count = 0;
11162 atomic_add_int(&chk->whoTo->ref_count, 1);
11163 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11164 cwr->ch.chunk_type = SCTP_ECN_CWR;
11165 cwr->ch.chunk_flags = 0;
11166 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11167 cwr->tsn = htonl(high_tsn);
11168 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11169 asoc->ctrl_queue_cnt++;
11173 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11174 int number_entries, uint16_t * list,
11175 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11177 int len, old_len, i;
11178 struct sctp_stream_reset_out_request *req_out;
11179 struct sctp_chunkhdr *ch;
11181 ch = mtod(chk->data, struct sctp_chunkhdr *);
11184 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11186 /* get to new offset for the param. */
11187 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11188 /* now how long will this param be? */
11189 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11190 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11191 req_out->ph.param_length = htons(len);
11192 req_out->request_seq = htonl(seq);
11193 req_out->response_seq = htonl(resp_seq);
11194 req_out->send_reset_at_tsn = htonl(last_sent);
11195 if (number_entries) {
11196 for (i = 0; i < number_entries; i++) {
11197 req_out->list_of_streams[i] = htons(list[i]);
11200 if (SCTP_SIZE32(len) > len) {
11202 * Need to worry about the pad we may end up adding to the
11203 * end. This is easy since the struct is either aligned to 4
11204 * bytes or 2 bytes off.
11206 req_out->list_of_streams[number_entries] = 0;
11208 /* now fix the chunk length */
11209 ch->chunk_length = htons(len + old_len);
11210 chk->book_size = len + old_len;
11211 chk->book_size_scale = 0;
11212 chk->send_size = SCTP_SIZE32(chk->book_size);
11213 SCTP_BUF_LEN(chk->data) = chk->send_size;
11219 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11220 int number_entries, uint16_t * list,
11223 int len, old_len, i;
11224 struct sctp_stream_reset_in_request *req_in;
11225 struct sctp_chunkhdr *ch;
11227 ch = mtod(chk->data, struct sctp_chunkhdr *);
11230 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11232 /* get to new offset for the param. */
11233 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11234 /* now how long will this param be? */
11235 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11236 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11237 req_in->ph.param_length = htons(len);
11238 req_in->request_seq = htonl(seq);
11239 if (number_entries) {
11240 for (i = 0; i < number_entries; i++) {
11241 req_in->list_of_streams[i] = htons(list[i]);
11244 if (SCTP_SIZE32(len) > len) {
11246 * Need to worry about the pad we may end up adding to the
11247 * end. This is easy since the struct is either aligned to 4
11248 * bytes or 2 bytes off.
11250 req_in->list_of_streams[number_entries] = 0;
11252 /* now fix the chunk length */
11253 ch->chunk_length = htons(len + old_len);
11254 chk->book_size = len + old_len;
11255 chk->book_size_scale = 0;
11256 chk->send_size = SCTP_SIZE32(chk->book_size);
11257 SCTP_BUF_LEN(chk->data) = chk->send_size;
11263 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11267 struct sctp_stream_reset_tsn_request *req_tsn;
11268 struct sctp_chunkhdr *ch;
11270 ch = mtod(chk->data, struct sctp_chunkhdr *);
11273 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11275 /* get to new offset for the param. */
11276 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11277 /* now how long will this param be? */
11278 len = sizeof(struct sctp_stream_reset_tsn_request);
11279 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11280 req_tsn->ph.param_length = htons(len);
11281 req_tsn->request_seq = htonl(seq);
11283 /* now fix the chunk length */
11284 ch->chunk_length = htons(len + old_len);
11285 chk->send_size = len + old_len;
11286 chk->book_size = SCTP_SIZE32(chk->send_size);
11287 chk->book_size_scale = 0;
11288 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11293 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11294 uint32_t resp_seq, uint32_t result)
11297 struct sctp_stream_reset_response *resp;
11298 struct sctp_chunkhdr *ch;
11300 ch = mtod(chk->data, struct sctp_chunkhdr *);
11303 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11305 /* get to new offset for the param. */
11306 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11307 /* now how long will this param be? */
11308 len = sizeof(struct sctp_stream_reset_response);
11309 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11310 resp->ph.param_length = htons(len);
11311 resp->response_seq = htonl(resp_seq);
11312 resp->result = ntohl(result);
11314 /* now fix the chunk length */
11315 ch->chunk_length = htons(len + old_len);
11316 chk->book_size = len + old_len;
11317 chk->book_size_scale = 0;
11318 chk->send_size = SCTP_SIZE32(chk->book_size);
11319 SCTP_BUF_LEN(chk->data) = chk->send_size;
11326 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11327 uint32_t resp_seq, uint32_t result,
11328 uint32_t send_una, uint32_t recv_next)
11331 struct sctp_stream_reset_response_tsn *resp;
11332 struct sctp_chunkhdr *ch;
11334 ch = mtod(chk->data, struct sctp_chunkhdr *);
11337 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11339 /* get to new offset for the param. */
11340 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11341 /* now how long will this param be? */
11342 len = sizeof(struct sctp_stream_reset_response_tsn);
11343 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11344 resp->ph.param_length = htons(len);
11345 resp->response_seq = htonl(resp_seq);
11346 resp->result = htonl(result);
11347 resp->senders_next_tsn = htonl(send_una);
11348 resp->receivers_next_tsn = htonl(recv_next);
11350 /* now fix the chunk length */
11351 ch->chunk_length = htons(len + old_len);
11352 chk->book_size = len + old_len;
11353 chk->send_size = SCTP_SIZE32(chk->book_size);
11354 chk->book_size_scale = 0;
11355 SCTP_BUF_LEN(chk->data) = chk->send_size;
11360 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11365 struct sctp_chunkhdr *ch;
11366 struct sctp_stream_reset_add_strm *addstr;
11368 ch = mtod(chk->data, struct sctp_chunkhdr *);
11369 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11371 /* get to new offset for the param. */
11372 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11373 /* now how long will this param be? */
11374 len = sizeof(struct sctp_stream_reset_add_strm);
11377 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11378 addstr->ph.param_length = htons(len);
11379 addstr->request_seq = htonl(seq);
11380 addstr->number_of_streams = htons(adding);
11381 addstr->reserved = 0;
11383 /* now fix the chunk length */
11384 ch->chunk_length = htons(len + old_len);
11385 chk->send_size = len + old_len;
11386 chk->book_size = SCTP_SIZE32(chk->send_size);
11387 chk->book_size_scale = 0;
11388 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11393 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11394 int number_entries, uint16_t * list,
11395 uint8_t send_out_req,
11397 uint8_t send_in_req,
11398 uint8_t send_tsn_req,
11399 uint8_t add_stream,
11404 struct sctp_association *asoc;
11405 struct sctp_tmit_chunk *chk;
11406 struct sctp_chunkhdr *ch;
11409 asoc = &stcb->asoc;
11410 if (asoc->stream_reset_outstanding) {
11412 * Already one pending, must get ACK back to clear the flag.
11414 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11417 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11418 (add_stream == 0)) {
11419 /* nothing to do */
11420 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11423 if (send_tsn_req && (send_out_req || send_in_req)) {
11424 /* error, can't do that */
11425 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11428 sctp_alloc_a_chunk(stcb, chk);
11430 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11433 chk->copy_by_ref = 0;
11434 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11435 chk->rec.chunk_id.can_take_data = 0;
11436 chk->asoc = &stcb->asoc;
11437 chk->book_size = sizeof(struct sctp_chunkhdr);
11438 chk->send_size = SCTP_SIZE32(chk->book_size);
11439 chk->book_size_scale = 0;
11441 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11442 if (chk->data == NULL) {
11443 sctp_free_a_chunk(stcb, chk);
11444 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11447 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11449 /* setup chunk parameters */
11450 chk->sent = SCTP_DATAGRAM_UNSENT;
11451 chk->snd_count = 0;
11452 chk->whoTo = asoc->primary_destination;
11453 atomic_add_int(&chk->whoTo->ref_count, 1);
11455 ch = mtod(chk->data, struct sctp_chunkhdr *);
11456 ch->chunk_type = SCTP_STREAM_RESET;
11457 ch->chunk_flags = 0;
11458 ch->chunk_length = htons(chk->book_size);
11459 SCTP_BUF_LEN(chk->data) = chk->send_size;
11461 seq = stcb->asoc.str_reset_seq_out;
11462 if (send_out_req) {
11463 sctp_add_stream_reset_out(chk, number_entries, list,
11464 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11465 asoc->stream_reset_out_is_outstanding = 1;
11467 asoc->stream_reset_outstanding++;
11470 sctp_add_a_stream(chk, seq, adding);
11472 asoc->stream_reset_outstanding++;
11475 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11476 asoc->stream_reset_outstanding++;
11478 if (send_tsn_req) {
11479 sctp_add_stream_reset_tsn(chk, seq);
11480 asoc->stream_reset_outstanding++;
11482 asoc->str_reset = chk;
11484 /* insert the chunk for sending */
11485 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11488 asoc->ctrl_queue_cnt++;
11489 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11494 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11495 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11498 * Formulate the abort message, and send it back down.
11500 struct mbuf *o_pak;
11502 struct sctp_abort_msg *abm;
11503 struct ip *iph, *iph_out;
11504 struct udphdr *udp;
11507 struct ip6_hdr *ip6, *ip6_out;
11510 int iphlen_out, len;
11512 /* don't respond to ABORT with ABORT */
11513 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11515 sctp_m_freem(err_cause);
11518 iph = mtod(m, struct ip *);
11519 switch (iph->ip_v) {
11521 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11524 case IPV6_VERSION >> 4:
11525 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11530 sctp_m_freem(err_cause);
11535 len += sizeof(struct udphdr);
11537 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11538 if (mout == NULL) {
11540 sctp_m_freem(err_cause);
11544 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11545 SCTP_BUF_LEN(mout) = len;
11546 SCTP_BUF_NEXT(mout) = err_cause;
11551 switch (iph->ip_v) {
11553 iph_out = mtod(mout, struct ip *);
11555 /* Fill in the IP header for the ABORT */
11556 iph_out->ip_v = IPVERSION;
11557 iph_out->ip_hl = (sizeof(struct ip) / 4);
11558 iph_out->ip_tos = (u_char)0;
11559 iph_out->ip_id = 0;
11560 iph_out->ip_off = 0;
11561 iph_out->ip_ttl = MAXTTL;
11563 iph_out->ip_p = IPPROTO_UDP;
11565 iph_out->ip_p = IPPROTO_SCTP;
11567 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11568 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11569 /* let IP layer calculate this */
11570 iph_out->ip_sum = 0;
11572 iphlen_out = sizeof(*iph_out);
11573 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11576 case IPV6_VERSION >> 4:
11577 ip6 = (struct ip6_hdr *)iph;
11578 ip6_out = mtod(mout, struct ip6_hdr *);
11580 /* Fill in the IP6 header for the ABORT */
11581 ip6_out->ip6_flow = ip6->ip6_flow;
11582 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11584 ip6_out->ip6_nxt = IPPROTO_UDP;
11586 ip6_out->ip6_nxt = IPPROTO_SCTP;
11588 ip6_out->ip6_src = ip6->ip6_dst;
11589 ip6_out->ip6_dst = ip6->ip6_src;
11591 iphlen_out = sizeof(*ip6_out);
11592 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11596 /* Currently not supported */
11597 sctp_m_freem(mout);
11601 udp = (struct udphdr *)abm;
11603 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11604 udp->uh_dport = port;
11605 /* set udp->uh_ulen later */
11607 iphlen_out += sizeof(struct udphdr);
11608 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11610 abm->sh.src_port = sh->dest_port;
11611 abm->sh.dest_port = sh->src_port;
11612 abm->sh.checksum = 0;
11614 abm->sh.v_tag = sh->v_tag;
11615 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11617 abm->sh.v_tag = htonl(vtag);
11618 abm->msg.ch.chunk_flags = 0;
11620 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11623 struct mbuf *m_tmp = err_cause;
11626 /* get length of the err_cause chain */
11627 while (m_tmp != NULL) {
11628 err_len += SCTP_BUF_LEN(m_tmp);
11629 m_tmp = SCTP_BUF_NEXT(m_tmp);
11631 len = SCTP_BUF_LEN(mout) + err_len;
11633 /* need pad at end of chunk */
11634 uint32_t cpthis = 0;
11637 padlen = 4 - (len % 4);
11638 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11641 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11643 len = SCTP_BUF_LEN(mout);
11644 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11647 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11649 sctp_m_freem(mout);
11652 if (iph_out != NULL) {
11656 /* zap the stack pointer to the route */
11657 bzero(&ro, sizeof ro);
11659 udp->uh_ulen = htons(len - sizeof(struct ip));
11660 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11662 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11663 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11664 /* set IPv4 length */
11665 iph_out->ip_len = len;
11667 #ifdef SCTP_PACKET_LOGGING
11668 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11669 sctp_packet_log(mout, len);
11671 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11673 #if defined(SCTP_WITH_NO_CSUM)
11674 SCTP_STAT_INCR(sctps_sendnocrc);
11676 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11677 SCTP_STAT_INCR(sctps_sendswcrc);
11679 SCTP_ENABLE_UDP_CSUM(o_pak);
11681 #if defined(SCTP_WITH_NO_CSUM)
11682 SCTP_STAT_INCR(sctps_sendnocrc);
11684 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11685 mout->m_pkthdr.csum_data = 0;
11686 SCTP_STAT_INCR(sctps_sendhwcrc);
11689 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11691 /* Free the route if we got one back */
11696 if (ip6_out != NULL) {
11697 struct route_in6 ro;
11699 struct ifnet *ifp = NULL;
11701 /* zap the stack pointer to the route */
11702 bzero(&ro, sizeof(ro));
11704 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11706 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11707 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11708 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11709 #ifdef SCTP_PACKET_LOGGING
11710 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11711 sctp_packet_log(mout, len);
11713 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11715 #if defined(SCTP_WITH_NO_CSUM)
11716 SCTP_STAT_INCR(sctps_sendnocrc);
11718 abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11719 SCTP_STAT_INCR(sctps_sendswcrc);
11721 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11722 udp->uh_sum = 0xffff;
11725 #if defined(SCTP_WITH_NO_CSUM)
11726 SCTP_STAT_INCR(sctps_sendnocrc);
11728 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11729 mout->m_pkthdr.csum_data = 0;
11730 SCTP_STAT_INCR(sctps_sendhwcrc);
11733 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11735 /* Free the route if we got one back */
11740 SCTP_STAT_INCR(sctps_sendpackets);
11741 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11742 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11746 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11747 uint32_t vrf_id, uint16_t port)
11749 struct mbuf *o_pak;
11750 struct sctphdr *sh, *sh_out;
11751 struct sctp_chunkhdr *ch;
11752 struct ip *iph, *iph_out;
11753 struct udphdr *udp = NULL;
11757 struct ip6_hdr *ip6, *ip6_out;
11760 int iphlen_out, len;
11762 iph = mtod(m, struct ip *);
11763 sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11764 switch (iph->ip_v) {
11766 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11769 case IPV6_VERSION >> 4:
11770 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11780 len += sizeof(struct udphdr);
11782 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11783 if (mout == NULL) {
11789 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11790 SCTP_BUF_LEN(mout) = len;
11791 SCTP_BUF_NEXT(mout) = scm;
11796 switch (iph->ip_v) {
11798 iph_out = mtod(mout, struct ip *);
11800 /* Fill in the IP header for the ABORT */
11801 iph_out->ip_v = IPVERSION;
11802 iph_out->ip_hl = (sizeof(struct ip) / 4);
11803 iph_out->ip_tos = (u_char)0;
11804 iph_out->ip_id = 0;
11805 iph_out->ip_off = 0;
11806 iph_out->ip_ttl = MAXTTL;
11808 iph_out->ip_p = IPPROTO_UDP;
11810 iph_out->ip_p = IPPROTO_SCTP;
11812 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11813 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11814 /* let IP layer calculate this */
11815 iph_out->ip_sum = 0;
11817 iphlen_out = sizeof(struct ip);
11818 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11821 case IPV6_VERSION >> 4:
11822 ip6 = (struct ip6_hdr *)iph;
11823 ip6_out = mtod(mout, struct ip6_hdr *);
11825 /* Fill in the IP6 header for the ABORT */
11826 ip6_out->ip6_flow = ip6->ip6_flow;
11827 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11829 ip6_out->ip6_nxt = IPPROTO_UDP;
11831 ip6_out->ip6_nxt = IPPROTO_SCTP;
11833 ip6_out->ip6_src = ip6->ip6_dst;
11834 ip6_out->ip6_dst = ip6->ip6_src;
11836 iphlen_out = sizeof(struct ip6_hdr);
11837 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11841 /* Currently not supported */
11842 sctp_m_freem(mout);
11846 udp = (struct udphdr *)sh_out;
11848 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11849 udp->uh_dport = port;
11850 /* set udp->uh_ulen later */
11852 iphlen_out += sizeof(struct udphdr);
11853 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11855 sh_out->src_port = sh->dest_port;
11856 sh_out->dest_port = sh->src_port;
11857 sh_out->v_tag = vtag;
11858 sh_out->checksum = 0;
11860 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11861 ch->chunk_type = SCTP_OPERATION_ERROR;
11862 ch->chunk_flags = 0;
11865 struct mbuf *m_tmp = scm;
11868 /* get length of the err_cause chain */
11869 while (m_tmp != NULL) {
11870 cause_len += SCTP_BUF_LEN(m_tmp);
11871 m_tmp = SCTP_BUF_NEXT(m_tmp);
11873 len = SCTP_BUF_LEN(mout) + cause_len;
11874 if (cause_len % 4) {
11875 /* need pad at end of chunk */
11876 uint32_t cpthis = 0;
11879 padlen = 4 - (len % 4);
11880 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11883 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11885 len = SCTP_BUF_LEN(mout);
11886 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11889 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11891 sctp_m_freem(mout);
11894 if (iph_out != NULL) {
11898 /* zap the stack pointer to the route */
11899 bzero(&ro, sizeof ro);
11901 udp->uh_ulen = htons(len - sizeof(struct ip));
11902 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11904 /* set IPv4 length */
11905 iph_out->ip_len = len;
11907 #ifdef SCTP_PACKET_LOGGING
11908 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11909 sctp_packet_log(mout, len);
11911 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11913 #if defined(SCTP_WITH_NO_CSUM)
11914 SCTP_STAT_INCR(sctps_sendnocrc);
11916 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11917 SCTP_STAT_INCR(sctps_sendswcrc);
11919 SCTP_ENABLE_UDP_CSUM(o_pak);
11921 #if defined(SCTP_WITH_NO_CSUM)
11922 SCTP_STAT_INCR(sctps_sendnocrc);
11924 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11925 mout->m_pkthdr.csum_data = 0;
11926 SCTP_STAT_INCR(sctps_sendhwcrc);
11929 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11931 /* Free the route if we got one back */
11936 if (ip6_out != NULL) {
11937 struct route_in6 ro;
11939 struct ifnet *ifp = NULL;
11941 /* zap the stack pointer to the route */
11942 bzero(&ro, sizeof(ro));
11944 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11946 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11947 #ifdef SCTP_PACKET_LOGGING
11948 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11949 sctp_packet_log(mout, len);
11951 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11953 #if defined(SCTP_WITH_NO_CSUM)
11954 SCTP_STAT_INCR(sctps_sendnocrc);
11956 sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11957 SCTP_STAT_INCR(sctps_sendswcrc);
11959 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11960 udp->uh_sum = 0xffff;
11963 #if defined(SCTP_WITH_NO_CSUM)
11964 SCTP_STAT_INCR(sctps_sendnocrc);
11966 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11967 mout->m_pkthdr.csum_data = 0;
11968 SCTP_STAT_INCR(sctps_sendhwcrc);
11971 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11973 /* Free the route if we got one back */
11978 SCTP_STAT_INCR(sctps_sendpackets);
11979 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11980 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11983 static struct mbuf *
11984 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11986 struct sctp_sndrcvinfo *srcv,
11988 int user_marks_eor,
11991 struct mbuf **new_tail)
11995 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11996 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11998 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12001 *sndout = m_length(m, NULL);
12002 *new_tail = m_last(m);
12008 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12015 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12017 if (sp->data == NULL) {
12018 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12021 sp->tail_mbuf = m_last(sp->data);
12027 static struct sctp_stream_queue_pending *
12028 sctp_copy_it_in(struct sctp_tcb *stcb,
12029 struct sctp_association *asoc,
12030 struct sctp_sndrcvinfo *srcv,
12032 struct sctp_nets *net,
12034 int user_marks_eor,
12039 * This routine must be very careful in its work. Protocol
12040 * processing is up and running so care must be taken to spl...()
12041 * when you need to do something that may effect the stcb/asoc. The
12042 * sb is locked however. When data is copied the protocol processing
12043 * should be enabled since this is a slower operation...
12045 struct sctp_stream_queue_pending *sp = NULL;
12049 /* Now can we send this? */
12050 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12051 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12052 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12053 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12054 /* got data while shutting down */
12055 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12056 *error = ECONNRESET;
12059 sctp_alloc_a_strmoq(stcb, sp);
12061 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12066 sp->sender_all_done = 0;
12067 sp->sinfo_flags = srcv->sinfo_flags;
12068 sp->timetolive = srcv->sinfo_timetolive;
12069 sp->ppid = srcv->sinfo_ppid;
12070 sp->context = srcv->sinfo_context;
12072 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12074 sp->stream = srcv->sinfo_stream;
12075 sp->length = min(uio->uio_resid, max_send_len);
12076 if ((sp->length == (uint32_t) uio->uio_resid) &&
12077 ((user_marks_eor == 0) ||
12078 (srcv->sinfo_flags & SCTP_EOF) ||
12079 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12080 sp->msg_is_complete = 1;
12082 sp->msg_is_complete = 0;
12084 sp->sender_all_done = 0;
12085 sp->some_taken = 0;
12086 sp->put_last_out = 0;
12087 resv_in_first = sizeof(struct sctp_data_chunk);
12088 sp->data = sp->tail_mbuf = NULL;
12089 if (sp->length == 0) {
12093 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12094 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12095 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12096 sp->holds_key_ref = 1;
12098 *error = sctp_copy_one(sp, uio, resv_in_first);
12101 sctp_free_a_strmoq(stcb, sp);
12104 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12107 sp->net = asoc->primary_destination;
12109 atomic_add_int(&sp->net->ref_count, 1);
12110 sctp_set_prsctp_policy(sp);
12118 sctp_sosend(struct socket *so,
12119 struct sockaddr *addr,
12122 struct mbuf *control,
12127 struct sctp_inpcb *inp;
12128 int error, use_rcvinfo = 0;
12129 struct sctp_sndrcvinfo srcv;
12130 struct sockaddr *addr_to_use;
12132 #if defined(INET) && defined(INET6)
12133 struct sockaddr_in sin;
12137 inp = (struct sctp_inpcb *)so->so_pcb;
12139 /* process cmsg snd/rcv info (maybe a assoc-id) */
12140 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12146 addr_to_use = addr;
12147 #if defined(INET) && defined(INET6)
12148 if ((addr) && (addr->sa_family == AF_INET6)) {
12149 struct sockaddr_in6 *sin6;
12151 sin6 = (struct sockaddr_in6 *)addr;
12152 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12153 in6_sin6_2_sin(&sin, sin6);
12154 addr_to_use = (struct sockaddr *)&sin;
12158 error = sctp_lower_sosend(so, addr_to_use, uio, top,
12161 use_rcvinfo ? &srcv : NULL
12169 sctp_lower_sosend(struct socket *so,
12170 struct sockaddr *addr,
12172 struct mbuf *i_pak,
12173 struct mbuf *control,
12175 struct sctp_sndrcvinfo *srcv
12180 unsigned int sndlen = 0, max_len;
12182 struct mbuf *top = NULL;
12183 int queue_only = 0, queue_only_for_init = 0;
12184 int free_cnt_applied = 0;
12186 int now_filled = 0;
12187 unsigned int inqueue_bytes = 0;
12188 struct sctp_block_entry be;
12189 struct sctp_inpcb *inp;
12190 struct sctp_tcb *stcb = NULL;
12191 struct timeval now;
12192 struct sctp_nets *net;
12193 struct sctp_association *asoc;
12194 struct sctp_inpcb *t_inp;
12195 int user_marks_eor;
12196 int create_lock_applied = 0;
12197 int nagle_applies = 0;
12198 int some_on_control = 0;
12199 int got_all_of_the_send = 0;
12200 int hold_tcblock = 0;
12201 int non_blocking = 0;
12202 uint32_t local_add_more, local_soresv = 0;
12204 uint16_t sinfo_flags;
12205 sctp_assoc_t sinfo_assoc_id;
12212 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12214 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12217 SCTP_RELEASE_PKT(i_pak);
12221 if ((uio == NULL) && (i_pak == NULL)) {
12222 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12225 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12226 atomic_add_int(&inp->total_sends, 1);
12228 if (uio->uio_resid < 0) {
12229 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12232 sndlen = uio->uio_resid;
12234 top = SCTP_HEADER_TO_CHAIN(i_pak);
12235 sndlen = SCTP_HEADER_LEN(i_pak);
12237 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12240 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12241 (inp->sctp_socket->so_qlimit)) {
12242 /* The listener can NOT send */
12243 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12248 * Pre-screen address, if one is given the sin-len
12249 * must be set correctly!
12252 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12254 switch (raddr->sa.sa_family) {
12257 if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12258 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12262 port = raddr->sin.sin_port;
12267 if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12268 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12272 port = raddr->sin6.sin6_port;
12276 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12277 error = EAFNOSUPPORT;
12284 sinfo_flags = srcv->sinfo_flags;
12285 sinfo_assoc_id = srcv->sinfo_assoc_id;
12286 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12287 PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12288 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12292 if (srcv->sinfo_flags)
12293 SCTP_STAT_INCR(sctps_sends_with_flags);
12295 sinfo_flags = inp->def_send.sinfo_flags;
12296 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12298 if (sinfo_flags & SCTP_SENDALL) {
12299 /* its a sendall */
12300 error = sctp_sendall(inp, uio, top, srcv);
12304 if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12305 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12309 /* now we must find the assoc */
12310 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12311 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12312 SCTP_INP_RLOCK(inp);
12313 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12314 if (stcb == NULL) {
12315 SCTP_INP_RUNLOCK(inp);
12316 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12320 SCTP_TCB_LOCK(stcb);
12322 SCTP_INP_RUNLOCK(inp);
12323 } else if (sinfo_assoc_id) {
12324 stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12327 * Since we did not use findep we must
12328 * increment it, and if we don't find a tcb
12331 SCTP_INP_WLOCK(inp);
12332 SCTP_INP_INCR_REF(inp);
12333 SCTP_INP_WUNLOCK(inp);
12334 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12335 if (stcb == NULL) {
12336 SCTP_INP_WLOCK(inp);
12337 SCTP_INP_DECR_REF(inp);
12338 SCTP_INP_WUNLOCK(inp);
12343 if ((stcb == NULL) && (addr)) {
12344 /* Possible implicit send? */
12345 SCTP_ASOC_CREATE_LOCK(inp);
12346 create_lock_applied = 1;
12347 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12348 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12349 /* Should I really unlock ? */
12350 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12355 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12356 (addr->sa_family == AF_INET6)) {
12357 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12361 SCTP_INP_WLOCK(inp);
12362 SCTP_INP_INCR_REF(inp);
12363 SCTP_INP_WUNLOCK(inp);
12364 /* With the lock applied look again */
12365 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12366 if (stcb == NULL) {
12367 SCTP_INP_WLOCK(inp);
12368 SCTP_INP_DECR_REF(inp);
12369 SCTP_INP_WUNLOCK(inp);
12373 if (t_inp != inp) {
12374 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12379 if (stcb == NULL) {
12380 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12381 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12382 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12386 if (addr == NULL) {
12387 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12392 * UDP style, we must go ahead and start the INIT
12397 if ((sinfo_flags & SCTP_ABORT) ||
12398 ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12400 * User asks to abort a non-existant assoc,
12401 * or EOF a non-existant assoc with no data
12403 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12407 /* get an asoc/stcb struct */
12408 vrf_id = inp->def_vrf_id;
12410 if (create_lock_applied == 0) {
12411 panic("Error, should hold create lock and I don't?");
12414 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12417 if (stcb == NULL) {
12418 /* Error is setup for us in the call */
12421 if (create_lock_applied) {
12422 SCTP_ASOC_CREATE_UNLOCK(inp);
12423 create_lock_applied = 0;
12425 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12428 * Turn on queue only flag to prevent data from
12432 asoc = &stcb->asoc;
12433 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12434 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12436 /* initialize authentication params for the assoc */
12437 sctp_initialize_auth_params(inp, stcb);
12441 * see if a init structure exists in cmsg
12444 struct sctp_initmsg initm;
12447 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12450 * we have an INIT override of the
12453 if (initm.sinit_max_attempts)
12454 asoc->max_init_times = initm.sinit_max_attempts;
12455 if (initm.sinit_num_ostreams)
12456 asoc->pre_open_streams = initm.sinit_num_ostreams;
12457 if (initm.sinit_max_instreams)
12458 asoc->max_inbound_streams = initm.sinit_max_instreams;
12459 if (initm.sinit_max_init_timeo)
12460 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12461 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12462 struct sctp_stream_out *tmp_str;
12465 /* Default is NOT correct */
12466 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12467 asoc->streamoutcnt, asoc->pre_open_streams);
12469 * What happens if this
12470 * fails? we panic ...
12473 if (hold_tcblock) {
12475 SCTP_TCB_UNLOCK(stcb);
12477 SCTP_MALLOC(tmp_str,
12478 struct sctp_stream_out *,
12479 (asoc->pre_open_streams *
12480 sizeof(struct sctp_stream_out)),
12483 SCTP_TCB_LOCK(stcb);
12485 if (tmp_str != NULL) {
12486 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12487 asoc->strmout = tmp_str;
12488 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12490 asoc->pre_open_streams = asoc->streamoutcnt;
12492 for (i = 0; i < asoc->streamoutcnt; i++) {
12494 * inbound side must be set
12495 * to 0xffff, also NOTE when
12496 * we get the INIT-ACK back
12497 * (for INIT sender) we MUST
12499 * (streamoutcnt) but first
12500 * check if we sent to any
12501 * of the upper streams that
12502 * were dropped (if some
12503 * were). Those that were
12504 * dropped must be notified
12505 * to the upper layer as
12508 asoc->strmout[i].next_sequence_sent = 0x0;
12509 TAILQ_INIT(&asoc->strmout[i].outqueue);
12510 asoc->strmout[i].stream_no = i;
12511 asoc->strmout[i].last_msg_incomplete = 0;
12512 asoc->strmout[i].next_spoke.tqe_next = 0;
12513 asoc->strmout[i].next_spoke.tqe_prev = 0;
12519 /* out with the INIT */
12520 queue_only_for_init = 1;
12522 * we may want to dig in after this call and adjust the MTU
12523 * value. It defaulted to 1500 (constant) but the ro
12524 * structure may now have an update and thus we may need to
12525 * change it BEFORE we append the message.
12530 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12531 if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12533 net = sctp_findnet(stcb, addr);
12536 if ((net == NULL) ||
12537 ((port != 0) && (port != stcb->rport))) {
12538 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12543 net = stcb->asoc.primary_destination;
12546 if ((SCTP_SO_IS_NBIO(so)
12547 || (flags & MSG_NBIO)
12551 asoc = &stcb->asoc;
12552 atomic_add_int(&stcb->total_sends, 1);
12554 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12555 if (sndlen > asoc->smallest_mtu) {
12556 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12561 /* would we block? */
12562 if (non_blocking) {
12563 if (hold_tcblock == 0) {
12564 SCTP_TCB_LOCK(stcb);
12567 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12568 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12569 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12570 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12571 if (sndlen > SCTP_SB_LIMIT_SND(so))
12574 error = EWOULDBLOCK;
12577 stcb->asoc.sb_send_resv += sndlen;
12578 SCTP_TCB_UNLOCK(stcb);
12581 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12583 local_soresv = sndlen;
12584 /* Keep the stcb from being freed under our feet */
12585 if (free_cnt_applied) {
12587 panic("refcnt already incremented");
12589 printf("refcnt:1 already incremented?\n");
12592 atomic_add_int(&stcb->asoc.refcnt, 1);
12593 free_cnt_applied = 1;
12595 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12596 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12597 error = ECONNRESET;
12600 if (create_lock_applied) {
12601 SCTP_ASOC_CREATE_UNLOCK(inp);
12602 create_lock_applied = 0;
12604 if (asoc->stream_reset_outstanding) {
12606 * Can't queue any data while stream reset is underway.
12608 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12612 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12613 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12616 /* we are now done with all control */
12618 sctp_m_freem(control);
12621 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12622 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12623 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12624 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12625 if (srcv->sinfo_flags & SCTP_ABORT) {
12628 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12629 error = ECONNRESET;
12633 /* Ok, we will attempt a msgsnd :> */
12635 p->td_ru.ru_msgsnd++;
12637 if ((net->flight_size > net->cwnd) &&
12638 (asoc->sctp_cmt_on_off == 0)) {
12640 * CMT: Added check for CMT above. net above is the primary
12641 * dest. If CMT is ON, sender should always attempt to send
12642 * with the output routine sctp_fill_outqueue() that loops
12643 * through all destination addresses. Therefore, if CMT is
12644 * ON, queue_only is NOT set to 1 here, so that
12645 * sctp_chunk_output() can be called below.
12648 } else if (asoc->ifp_had_enobuf) {
12649 SCTP_STAT_INCR(sctps_ifnomemqueued);
12650 if (net->flight_size > (net->mtu * 2))
12652 asoc->ifp_had_enobuf = 0;
12654 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12655 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12657 /* Are we aborting? */
12658 if (srcv->sinfo_flags & SCTP_ABORT) {
12660 int tot_demand, tot_out = 0, max_out;
12662 SCTP_STAT_INCR(sctps_sends_with_abort);
12663 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12664 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12665 /* It has to be up before we abort */
12666 /* how big is the user initiated abort? */
12667 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12671 if (hold_tcblock) {
12672 SCTP_TCB_UNLOCK(stcb);
12676 struct mbuf *cntm = NULL;
12678 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12682 tot_out += SCTP_BUF_LEN(cntm);
12683 cntm = SCTP_BUF_NEXT(cntm);
12686 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12688 /* Must fit in a MTU */
12690 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12691 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12693 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12697 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12700 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12704 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12705 max_out -= sizeof(struct sctp_abort_msg);
12706 if (tot_out > max_out) {
12710 struct sctp_paramhdr *ph;
12712 /* now move forward the data pointer */
12713 ph = mtod(mm, struct sctp_paramhdr *);
12714 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12715 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12717 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12719 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12722 * Here if we can't get his data we
12723 * still abort we just don't get to
12724 * send the users note :-0
12731 SCTP_BUF_NEXT(mm) = top;
12735 if (hold_tcblock == 0) {
12736 SCTP_TCB_LOCK(stcb);
12739 atomic_add_int(&stcb->asoc.refcnt, -1);
12740 free_cnt_applied = 0;
12741 /* release this lock, otherwise we hang on ourselves */
12742 sctp_abort_an_association(stcb->sctp_ep, stcb,
12743 SCTP_RESPONSE_TO_USER_REQ,
12744 mm, SCTP_SO_LOCKED);
12745 /* now relock the stcb so everything is sane */
12749 * In this case top is already chained to mm avoid double
12750 * free, since we free it below if top != NULL and driver
12751 * would free it after sending the packet out
12758 /* Calculate the maximum we can send */
12759 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12760 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12761 if (non_blocking) {
12762 /* we already checked for non-blocking above. */
12765 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12770 if (hold_tcblock) {
12771 SCTP_TCB_UNLOCK(stcb);
12774 /* Is the stream no. valid? */
12775 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12776 /* Invalid stream number */
12777 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12781 if (asoc->strmout == NULL) {
12782 /* huh? software error */
12783 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12787 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12788 if ((user_marks_eor == 0) &&
12789 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12790 /* It will NEVER fit */
12791 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12795 if ((uio == NULL) && user_marks_eor) {
12797 * We do not support eeor mode for
12798 * sending with mbuf chains (like sendfile).
12800 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12804 if (user_marks_eor) {
12805 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12808 * For non-eeor the whole message must fit in
12809 * the socket send buffer.
12811 local_add_more = sndlen;
12814 if (non_blocking) {
12815 goto skip_preblock;
12817 if (((max_len <= local_add_more) &&
12818 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12820 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12821 /* No room right now ! */
12822 SOCKBUF_LOCK(&so->so_snd);
12823 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12824 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12825 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12826 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12827 (unsigned int)SCTP_SB_LIMIT_SND(so),
12830 stcb->asoc.stream_queue_cnt,
12831 stcb->asoc.chunks_on_out_queue,
12832 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12833 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12834 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12837 stcb->block_entry = &be;
12838 error = sbwait(&so->so_snd);
12839 stcb->block_entry = NULL;
12840 if (error || so->so_error || be.error) {
12843 error = so->so_error;
12848 SOCKBUF_UNLOCK(&so->so_snd);
12851 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12852 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12853 so, asoc, stcb->asoc.total_output_queue_size);
12855 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12858 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12860 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12861 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12862 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12866 SOCKBUF_UNLOCK(&so->so_snd);
12869 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12873 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12874 * case NOTE: uio will be null when top/mbuf is passed
12877 if (srcv->sinfo_flags & SCTP_EOF) {
12878 got_all_of_the_send = 1;
12881 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12887 struct sctp_stream_queue_pending *sp;
12888 struct sctp_stream_out *strm;
12889 uint32_t sndout, initial_out;
12891 initial_out = uio->uio_resid;
12893 SCTP_TCB_SEND_LOCK(stcb);
12894 if ((asoc->stream_locked) &&
12895 (asoc->stream_locked_on != srcv->sinfo_stream)) {
12896 SCTP_TCB_SEND_UNLOCK(stcb);
12897 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12901 SCTP_TCB_SEND_UNLOCK(stcb);
12903 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12904 if (strm->last_msg_incomplete == 0) {
12906 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12907 if ((sp == NULL) || (error)) {
12910 SCTP_TCB_SEND_LOCK(stcb);
12911 if (sp->msg_is_complete) {
12912 strm->last_msg_incomplete = 0;
12913 asoc->stream_locked = 0;
12916 * Just got locked to this guy in case of an
12919 strm->last_msg_incomplete = 1;
12920 asoc->stream_locked = 1;
12921 asoc->stream_locked_on = srcv->sinfo_stream;
12922 sp->sender_all_done = 0;
12924 sctp_snd_sb_alloc(stcb, sp->length);
12925 atomic_add_int(&asoc->stream_queue_cnt, 1);
12926 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12927 sp->strseq = strm->next_sequence_sent;
12928 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12929 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12930 (uintptr_t) stcb, sp->length,
12931 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12933 strm->next_sequence_sent++;
12935 SCTP_STAT_INCR(sctps_sends_with_unord);
12937 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12938 if ((strm->next_spoke.tqe_next == NULL) &&
12939 (strm->next_spoke.tqe_prev == NULL)) {
12940 /* Not on wheel, insert */
12941 sctp_insert_on_wheel(stcb, asoc, strm, 1);
12943 SCTP_TCB_SEND_UNLOCK(stcb);
12945 SCTP_TCB_SEND_LOCK(stcb);
12946 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12947 SCTP_TCB_SEND_UNLOCK(stcb);
12949 /* ???? Huh ??? last msg is gone */
12951 panic("Warning: Last msg marked incomplete, yet nothing left?");
12953 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12954 strm->last_msg_incomplete = 0;
12960 while (uio->uio_resid > 0) {
12961 /* How much room do we have? */
12962 struct mbuf *new_tail, *mm;
12964 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12965 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12969 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12970 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12971 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
12974 if (hold_tcblock) {
12975 SCTP_TCB_UNLOCK(stcb);
12978 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12979 if ((mm == NULL) || error) {
12985 /* Update the mbuf and count */
12986 SCTP_TCB_SEND_LOCK(stcb);
12987 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12989 * we need to get out. Peer probably
12993 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12994 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12995 error = ECONNRESET;
12997 SCTP_TCB_SEND_UNLOCK(stcb);
13000 if (sp->tail_mbuf) {
13001 /* tack it to the end */
13002 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13003 sp->tail_mbuf = new_tail;
13005 /* A stolen mbuf */
13007 sp->tail_mbuf = new_tail;
13009 sctp_snd_sb_alloc(stcb, sndout);
13010 atomic_add_int(&sp->length, sndout);
13013 /* Did we reach EOR? */
13014 if ((uio->uio_resid == 0) &&
13015 ((user_marks_eor == 0) ||
13016 (srcv->sinfo_flags & SCTP_EOF) ||
13017 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13018 sp->msg_is_complete = 1;
13020 sp->msg_is_complete = 0;
13022 SCTP_TCB_SEND_UNLOCK(stcb);
13024 if (uio->uio_resid == 0) {
13029 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13031 * This is ugly but we must assure locking
13034 if (hold_tcblock == 0) {
13035 SCTP_TCB_LOCK(stcb);
13038 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13039 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13040 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13041 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13047 SCTP_TCB_UNLOCK(stcb);
13050 /* wait for space now */
13051 if (non_blocking) {
13052 /* Non-blocking io in place out */
13055 if ((net->flight_size > net->cwnd) &&
13056 (asoc->sctp_cmt_on_off == 0)) {
13058 } else if (asoc->ifp_had_enobuf) {
13059 SCTP_STAT_INCR(sctps_ifnomemqueued);
13060 if (net->flight_size > (net->mtu * 2)) {
13065 asoc->ifp_had_enobuf = 0;
13066 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13067 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13069 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13070 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13071 if (net->flight_size > net->cwnd) {
13073 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13078 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13079 (stcb->asoc.total_flight > 0) &&
13080 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13081 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13084 * Ok, Nagle is set on and we have data outstanding.
13085 * Don't send anything and let SACKs drive out the
13086 * data unless wen have a "full" segment to send.
13088 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13089 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13091 SCTP_STAT_INCR(sctps_naglequeued);
13094 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13095 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13096 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13098 SCTP_STAT_INCR(sctps_naglesent);
13101 /* What about the INIT, send it maybe */
13102 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13104 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13105 nagle_applies, un_sent);
13106 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13107 stcb->asoc.total_flight,
13108 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13110 if (queue_only_for_init) {
13111 if (hold_tcblock == 0) {
13112 SCTP_TCB_LOCK(stcb);
13115 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13116 /* a collision took us forward? */
13117 queue_only_for_init = 0;
13120 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13121 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13122 queue_only_for_init = 0;
13126 if ((queue_only == 0) && (nagle_applies == 0)) {
13128 * need to start chunk output
13129 * before blocking.. note that if
13130 * a lock is already applied, then
13131 * the input via the net is happening
13132 * and I don't need to start output :-D
13134 if (hold_tcblock == 0) {
13135 if (SCTP_TCB_TRYLOCK(stcb)) {
13137 sctp_chunk_output(inp,
13139 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13142 sctp_chunk_output(inp,
13144 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13146 if (hold_tcblock == 1) {
13147 SCTP_TCB_UNLOCK(stcb);
13151 SOCKBUF_LOCK(&so->so_snd);
13153 * This is a bit strange, but I think it will
13154 * work. The total_output_queue_size is locked and
13155 * protected by the TCB_LOCK, which we just released.
13156 * There is a race that can occur between releasing it
13157 * above, and me getting the socket lock, where sacks
13158 * come in but we have not put the SB_WAIT on the
13159 * so_snd buffer to get the wakeup. After the LOCK
13160 * is applied the sack_processing will also need to
13161 * LOCK the so->so_snd to do the actual sowwakeup(). So
13162 * once we have the socket buffer lock if we recheck the
13163 * size we KNOW we will get to sleep safely with the
13164 * wakeup flag in place.
13166 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13167 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13168 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13169 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13170 so, asoc, uio->uio_resid);
13173 stcb->block_entry = &be;
13174 error = sbwait(&so->so_snd);
13175 stcb->block_entry = NULL;
13177 if (error || so->so_error || be.error) {
13180 error = so->so_error;
13185 SOCKBUF_UNLOCK(&so->so_snd);
13188 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13189 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13190 so, asoc, stcb->asoc.total_output_queue_size);
13193 SOCKBUF_UNLOCK(&so->so_snd);
13194 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13198 SCTP_TCB_SEND_LOCK(stcb);
13200 if (sp->msg_is_complete == 0) {
13201 strm->last_msg_incomplete = 1;
13202 asoc->stream_locked = 1;
13203 asoc->stream_locked_on = srcv->sinfo_stream;
13205 sp->sender_all_done = 1;
13206 strm->last_msg_incomplete = 0;
13207 asoc->stream_locked = 0;
13210 SCTP_PRINTF("Huh no sp TSNH?\n");
13211 strm->last_msg_incomplete = 0;
13212 asoc->stream_locked = 0;
13214 SCTP_TCB_SEND_UNLOCK(stcb);
13215 if (uio->uio_resid == 0) {
13216 got_all_of_the_send = 1;
13219 /* We send in a 0, since we do NOT have any locks */
13220 error = sctp_msg_append(stcb, net, top, srcv, 0);
13222 if (srcv->sinfo_flags & SCTP_EOF) {
13224 * This should only happen for Panda for the mbuf
13225 * send case, which does NOT yet support EEOR mode.
13226 * Thus, we can just set this flag to do the proper
13229 got_all_of_the_send = 1;
13237 if ((srcv->sinfo_flags & SCTP_EOF) &&
13238 (got_all_of_the_send == 1) &&
13239 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13242 SCTP_STAT_INCR(sctps_sends_with_eof);
13244 if (hold_tcblock == 0) {
13245 SCTP_TCB_LOCK(stcb);
13248 cnt = sctp_is_there_unsent_data(stcb);
13249 if (TAILQ_EMPTY(&asoc->send_queue) &&
13250 TAILQ_EMPTY(&asoc->sent_queue) &&
13252 if (asoc->locked_on_sending) {
13255 /* there is nothing queued to send, so I'm done... */
13256 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13257 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13258 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13259 /* only send SHUTDOWN the first time through */
13260 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13261 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13262 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13264 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13265 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13266 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13267 asoc->primary_destination);
13268 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13269 asoc->primary_destination);
13273 * we still got (or just got) data to send, so set
13277 * XXX sockets draft says that SCTP_EOF should be
13278 * sent with no data. currently, we will allow user
13279 * data to be sent first and move to
13282 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13283 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13284 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13285 if (hold_tcblock == 0) {
13286 SCTP_TCB_LOCK(stcb);
13289 if (asoc->locked_on_sending) {
13290 /* Locked to send out the data */
13291 struct sctp_stream_queue_pending *sp;
13293 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13295 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13296 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13299 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13300 if (TAILQ_EMPTY(&asoc->send_queue) &&
13301 TAILQ_EMPTY(&asoc->sent_queue) &&
13302 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13304 if (free_cnt_applied) {
13305 atomic_add_int(&stcb->asoc.refcnt, -1);
13306 free_cnt_applied = 0;
13308 sctp_abort_an_association(stcb->sctp_ep, stcb,
13309 SCTP_RESPONSE_TO_USER_REQ,
13310 NULL, SCTP_SO_LOCKED);
13312 * now relock the stcb so everything
13319 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13320 asoc->primary_destination);
13321 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13326 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13327 some_on_control = 1;
13329 if ((net->flight_size > net->cwnd) &&
13330 (stcb->asoc.sctp_cmt_on_off == 0)) {
13332 } else if (asoc->ifp_had_enobuf) {
13333 SCTP_STAT_INCR(sctps_ifnomemqueued);
13334 if (net->flight_size > (net->mtu * 2)) {
13339 asoc->ifp_had_enobuf = 0;
13340 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13341 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13343 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13344 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13345 if (net->flight_size > net->cwnd) {
13347 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13352 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13353 (stcb->asoc.total_flight > 0) &&
13354 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13355 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13357 * Ok, Nagle is set on and we have data outstanding.
13358 * Don't send anything and let SACKs drive out the
13359 * data unless wen have a "full" segment to send.
13361 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13362 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13364 SCTP_STAT_INCR(sctps_naglequeued);
13367 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13368 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13369 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13371 SCTP_STAT_INCR(sctps_naglesent);
13374 if (queue_only_for_init) {
13375 if (hold_tcblock == 0) {
13376 SCTP_TCB_LOCK(stcb);
13379 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13380 /* a collision took us forward? */
13381 queue_only_for_init = 0;
13384 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13385 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13386 queue_only_for_init = 0;
13390 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13391 /* we can attempt to send too. */
13392 if (hold_tcblock == 0) {
13394 * If there is activity recv'ing sacks no need to
13397 if (SCTP_TCB_TRYLOCK(stcb)) {
13398 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13402 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13404 } else if ((queue_only == 0) &&
13405 (stcb->asoc.peers_rwnd == 0) &&
13406 (stcb->asoc.total_flight == 0)) {
13407 /* We get to have a probe outstanding */
13408 if (hold_tcblock == 0) {
13410 SCTP_TCB_LOCK(stcb);
13412 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13413 } else if (some_on_control) {
13414 int num_out, reason, frag_point;
13416 /* Here we do control only */
13417 if (hold_tcblock == 0) {
13419 SCTP_TCB_LOCK(stcb);
13421 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13422 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13423 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13425 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13426 queue_only, stcb->asoc.peers_rwnd, un_sent,
13427 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13428 stcb->asoc.total_output_queue_size, error);
13433 if (local_soresv && stcb) {
13434 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13437 if (create_lock_applied) {
13438 SCTP_ASOC_CREATE_UNLOCK(inp);
13439 create_lock_applied = 0;
13441 if ((stcb) && hold_tcblock) {
13442 SCTP_TCB_UNLOCK(stcb);
13444 if (stcb && free_cnt_applied) {
13445 atomic_add_int(&stcb->asoc.refcnt, -1);
13449 if (mtx_owned(&stcb->tcb_mtx)) {
13450 panic("Leaving with tcb mtx owned?");
13452 if (mtx_owned(&stcb->tcb_send_mtx)) {
13453 panic("Leaving with tcb send mtx owned?");
13459 sctp_validate_no_locks(inp);
13461 printf("Warning - inp is NULL so cant validate locks\n");
13468 sctp_m_freem(control);
13475 * generate an AUTHentication chunk, if required
13478 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13479 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13480 struct sctp_tcb *stcb, uint8_t chunk)
13482 struct mbuf *m_auth;
13483 struct sctp_auth_chunk *auth;
13486 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13490 /* sysctl disabled auth? */
13491 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13494 /* peer doesn't do auth... */
13495 if (!stcb->asoc.peer_supports_auth) {
13498 /* does the requested chunk require auth? */
13499 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13502 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13503 if (m_auth == NULL) {
13507 /* reserve some space if this will be the first mbuf */
13509 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13510 /* fill in the AUTH chunk details */
13511 auth = mtod(m_auth, struct sctp_auth_chunk *);
13512 bzero(auth, sizeof(*auth));
13513 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13514 auth->ch.chunk_flags = 0;
13515 chunk_len = sizeof(*auth) +
13516 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13517 auth->ch.chunk_length = htons(chunk_len);
13518 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13519 /* key id and hmac digest will be computed and filled in upon send */
13521 /* save the offset where the auth was inserted into the chain */
13528 *offset += SCTP_BUF_LEN(cn);
13529 cn = SCTP_BUF_NEXT(cn);
13534 /* update length and return pointer to the auth chunk */
13535 SCTP_BUF_LEN(m_auth) = chunk_len;
13536 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13537 if (auth_ret != NULL)
13545 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13547 struct nd_prefix *pfx = NULL;
13548 struct nd_pfxrouter *pfxrtr = NULL;
13549 struct sockaddr_in6 gw6;
13551 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13554 /* get prefix entry of address */
13555 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13556 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13558 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13559 &src6->sin6_addr, &pfx->ndpr_mask))
13562 /* no prefix entry in the prefix list */
13564 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13565 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13568 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13569 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13571 /* search installed gateway from prefix entry */
13572 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13573 pfxrtr->pfr_next) {
13574 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13575 gw6.sin6_family = AF_INET6;
13576 gw6.sin6_len = sizeof(struct sockaddr_in6);
13577 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13578 sizeof(struct in6_addr));
13579 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13580 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13581 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13582 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13583 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13584 ro->ro_rt->rt_gateway)) {
13585 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13589 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13596 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13598 struct sockaddr_in *sin, *mask;
13599 struct ifaddr *ifa;
13600 struct in_addr srcnetaddr, gwnetaddr;
13602 if (ro == NULL || ro->ro_rt == NULL ||
13603 sifa->address.sa.sa_family != AF_INET) {
13606 ifa = (struct ifaddr *)sifa->ifa;
13607 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13608 sin = (struct sockaddr_in *)&sifa->address.sin;
13609 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13610 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13611 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13612 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13614 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13615 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13616 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13617 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13618 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13619 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {