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 * ----------------------------------------- src | dest | result
2194 * ---------------------------------------- L | L | yes
2195 * ----------------------------------------- P | L |
2196 * yes-v4 no-v6 ----------------------------------------- G |
2197 * L | yes-v4 no-v6 ----------------------------------------- L
2198 * | P | no ----------------------------------------- P |
2199 * P | yes ----------------------------------------- G |
2200 * P | no ----------------------------------------- L | G
2201 * | no ----------------------------------------- P | G |
2202 * no ----------------------------------------- G | G |
2203 * yes -----------------------------------------
2206 if (ifa->address.sa.sa_family != fam) {
2207 /* forget mis-matched family */
2210 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2213 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2214 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2215 /* Ok the address may be ok */
2216 if (fam == AF_INET6) {
2217 /* ok to use deprecated addresses? no lets not! */
2218 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2219 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2222 if (ifa->src_is_priv && !ifa->src_is_loop) {
2224 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2228 if (ifa->src_is_glob) {
2230 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2236 * Now that we know what is what, implement or table this could in
2237 * theory be done slicker (it used to be), but this is
2238 * straightforward and easier to validate :-)
2240 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2241 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2242 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2243 dest_is_loop, dest_is_priv, dest_is_global);
2245 if ((ifa->src_is_loop) && (dest_is_priv)) {
2246 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2249 if ((ifa->src_is_glob) && (dest_is_priv)) {
2250 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2253 if ((ifa->src_is_loop) && (dest_is_global)) {
2254 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2257 if ((ifa->src_is_priv) && (dest_is_global)) {
2258 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2261 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2262 /* its a preferred address */
2266 static struct sctp_ifa *
2267 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2268 uint8_t dest_is_loop,
2269 uint8_t dest_is_priv,
2272 uint8_t dest_is_global = 0;
2276 * Here we determine if its a acceptable address. A acceptable
2277 * address means it is the same scope or higher scope but we can
2278 * allow for NAT which means its ok to have a global dest and a
2281 * L = loopback, P = private, G = global
2282 * ----------------------------------------- src | dest | result
2283 * ----------------------------------------- L | L | yes
2284 * ----------------------------------------- P | L |
2285 * yes-v4 no-v6 ----------------------------------------- G |
2286 * L | yes ----------------------------------------- L |
2287 * P | no ----------------------------------------- P | P
2288 * | yes ----------------------------------------- G | P
2289 * | yes - May not work -----------------------------------------
2290 * L | G | no ----------------------------------------- P
2291 * | G | yes - May not work
2292 * ----------------------------------------- G | G | yes
2293 * -----------------------------------------
2296 if (ifa->address.sa.sa_family != fam) {
2297 /* forget non matching family */
2300 /* Ok the address may be ok */
2301 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2304 if (fam == AF_INET6) {
2305 /* ok to use deprecated addresses? */
2306 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2309 if (ifa->src_is_priv) {
2310 /* Special case, linklocal to loop */
2316 * Now that we know what is what, implement our table. This could in
2317 * theory be done slicker (it used to be), but this is
2318 * straightforward and easier to validate :-)
2320 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2323 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2326 /* its an acceptable address */
2331 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2333 struct sctp_laddr *laddr;
2336 /* There are no restrictions, no TCB :-) */
2339 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2340 if (laddr->ifa == NULL) {
2341 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2345 if (laddr->ifa == ifa) {
2346 /* Yes it is on the list */
2355 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2357 struct sctp_laddr *laddr;
2361 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2362 if (laddr->ifa == NULL) {
2363 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2367 if ((laddr->ifa == ifa) && laddr->action == 0)
2376 static struct sctp_ifa *
2377 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2380 int non_asoc_addr_ok,
2381 uint8_t dest_is_priv,
2382 uint8_t dest_is_loop,
2385 struct sctp_laddr *laddr, *starting_point;
2388 struct sctp_ifn *sctp_ifn;
2389 struct sctp_ifa *sctp_ifa, *sifa;
2390 struct sctp_vrf *vrf;
2393 vrf = sctp_find_vrf(vrf_id);
2397 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2398 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2399 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2401 * first question, is the ifn we will emit on in our list, if so, we
2402 * want such an address. Note that we first looked for a preferred
2406 /* is a preferred one on the interface we route out? */
2407 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2408 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2409 (non_asoc_addr_ok == 0))
2411 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2416 if (sctp_is_addr_in_ep(inp, sifa)) {
2417 atomic_add_int(&sifa->refcount, 1);
2423 * ok, now we now need to find one on the list of the addresses. We
2424 * can't get one on the emitting interface so let's find first a
2425 * preferred one. If not that an acceptable one otherwise... we
2428 starting_point = inp->next_addr_touse;
2430 if (inp->next_addr_touse == NULL) {
2431 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2434 for (laddr = inp->next_addr_touse; laddr;
2435 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2436 if (laddr->ifa == NULL) {
2437 /* address has been removed */
2440 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2441 /* address is being deleted */
2444 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2448 atomic_add_int(&sifa->refcount, 1);
2451 if (resettotop == 0) {
2452 inp->next_addr_touse = NULL;
2455 inp->next_addr_touse = starting_point;
2458 if (inp->next_addr_touse == NULL) {
2459 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2462 /* ok, what about an acceptable address in the inp */
2463 for (laddr = inp->next_addr_touse; laddr;
2464 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2465 if (laddr->ifa == NULL) {
2466 /* address has been removed */
2469 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2470 /* address is being deleted */
2473 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2477 atomic_add_int(&sifa->refcount, 1);
2480 if (resettotop == 0) {
2481 inp->next_addr_touse = NULL;
2482 goto once_again_too;
2485 * no address bound can be a source for the destination we are in
2493 static struct sctp_ifa *
2494 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2495 struct sctp_tcb *stcb,
2496 struct sctp_nets *net,
2499 uint8_t dest_is_priv,
2500 uint8_t dest_is_loop,
2501 int non_asoc_addr_ok,
2504 struct sctp_laddr *laddr, *starting_point;
2506 struct sctp_ifn *sctp_ifn;
2507 struct sctp_ifa *sctp_ifa, *sifa;
2508 uint8_t start_at_beginning = 0;
2509 struct sctp_vrf *vrf;
2513 * first question, is the ifn we will emit on in our list, if so, we
2516 vrf = sctp_find_vrf(vrf_id);
2520 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2521 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2522 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2525 * first question, is the ifn we will emit on in our list? If so,
2526 * we want that one. First we look for a preferred. Second, we go
2527 * for an acceptable.
2530 /* first try for a preferred address on the ep */
2531 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2532 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2534 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2535 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2538 if (((non_asoc_addr_ok == 0) &&
2539 (sctp_is_addr_restricted(stcb, sifa))) ||
2540 (non_asoc_addr_ok &&
2541 (sctp_is_addr_restricted(stcb, sifa)) &&
2542 (!sctp_is_addr_pending(stcb, sifa)))) {
2543 /* on the no-no list */
2546 atomic_add_int(&sifa->refcount, 1);
2550 /* next try for an acceptable address on the ep */
2551 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2552 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2554 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2555 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2558 if (((non_asoc_addr_ok == 0) &&
2559 (sctp_is_addr_restricted(stcb, sifa))) ||
2560 (non_asoc_addr_ok &&
2561 (sctp_is_addr_restricted(stcb, sifa)) &&
2562 (!sctp_is_addr_pending(stcb, sifa)))) {
2563 /* on the no-no list */
2566 atomic_add_int(&sifa->refcount, 1);
2573 * if we can't find one like that then we must look at all addresses
2574 * bound to pick one at first preferable then secondly acceptable.
2576 starting_point = stcb->asoc.last_used_address;
2578 if (stcb->asoc.last_used_address == NULL) {
2579 start_at_beginning = 1;
2580 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2582 /* search beginning with the last used address */
2583 for (laddr = stcb->asoc.last_used_address; laddr;
2584 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2585 if (laddr->ifa == NULL) {
2586 /* address has been removed */
2589 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2590 /* address is being deleted */
2593 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2596 if (((non_asoc_addr_ok == 0) &&
2597 (sctp_is_addr_restricted(stcb, sifa))) ||
2598 (non_asoc_addr_ok &&
2599 (sctp_is_addr_restricted(stcb, sifa)) &&
2600 (!sctp_is_addr_pending(stcb, sifa)))) {
2601 /* on the no-no list */
2604 stcb->asoc.last_used_address = laddr;
2605 atomic_add_int(&sifa->refcount, 1);
2608 if (start_at_beginning == 0) {
2609 stcb->asoc.last_used_address = NULL;
2610 goto sctp_from_the_top;
2612 /* now try for any higher scope than the destination */
2613 stcb->asoc.last_used_address = starting_point;
2614 start_at_beginning = 0;
2616 if (stcb->asoc.last_used_address == NULL) {
2617 start_at_beginning = 1;
2618 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2620 /* search beginning with the last used address */
2621 for (laddr = stcb->asoc.last_used_address; laddr;
2622 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2623 if (laddr->ifa == NULL) {
2624 /* address has been removed */
2627 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2628 /* address is being deleted */
2631 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2635 if (((non_asoc_addr_ok == 0) &&
2636 (sctp_is_addr_restricted(stcb, sifa))) ||
2637 (non_asoc_addr_ok &&
2638 (sctp_is_addr_restricted(stcb, sifa)) &&
2639 (!sctp_is_addr_pending(stcb, sifa)))) {
2640 /* on the no-no list */
2643 stcb->asoc.last_used_address = laddr;
2644 atomic_add_int(&sifa->refcount, 1);
2647 if (start_at_beginning == 0) {
2648 stcb->asoc.last_used_address = NULL;
2649 goto sctp_from_the_top2;
2654 static struct sctp_ifa *
2655 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2656 struct sctp_tcb *stcb,
2657 int non_asoc_addr_ok,
2658 uint8_t dest_is_loop,
2659 uint8_t dest_is_priv,
2665 struct sctp_ifa *ifa, *sifa;
2666 int num_eligible_addr = 0;
2669 struct sockaddr_in6 sin6, lsa6;
2671 if (fam == AF_INET6) {
2672 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2673 (void)sa6_recoverscope(&sin6);
2676 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2677 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2678 (non_asoc_addr_ok == 0))
2680 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2685 if (fam == AF_INET6 &&
2687 sifa->src_is_loop && sifa->src_is_priv) {
2689 * don't allow fe80::1 to be a src on loop ::1, we
2690 * don't list it to the peer so we will get an
2695 if (fam == AF_INET6 &&
2696 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2697 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2699 * link-local <-> link-local must belong to the same
2702 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2703 (void)sa6_recoverscope(&lsa6);
2704 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2711 * Check if the IPv6 address matches to next-hop. In the
2712 * mobile case, old IPv6 address may be not deleted from the
2713 * interface. Then, the interface has previous and new
2714 * addresses. We should use one corresponding to the
2715 * next-hop. (by micchie)
2718 if (stcb && fam == AF_INET6 &&
2719 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2720 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2726 /* Avoid topologically incorrect IPv4 address */
2727 if (stcb && fam == AF_INET &&
2728 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2734 if (((non_asoc_addr_ok == 0) &&
2735 (sctp_is_addr_restricted(stcb, sifa))) ||
2736 (non_asoc_addr_ok &&
2737 (sctp_is_addr_restricted(stcb, sifa)) &&
2738 (!sctp_is_addr_pending(stcb, sifa)))) {
2740 * It is restricted for some reason..
2741 * probably not yet added.
2746 if (num_eligible_addr >= addr_wanted) {
2749 num_eligible_addr++;
2756 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2757 struct sctp_tcb *stcb,
2758 int non_asoc_addr_ok,
2759 uint8_t dest_is_loop,
2760 uint8_t dest_is_priv,
2763 struct sctp_ifa *ifa, *sifa;
2764 int num_eligible_addr = 0;
2766 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2767 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2768 (non_asoc_addr_ok == 0)) {
2771 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2777 if (((non_asoc_addr_ok == 0) &&
2778 (sctp_is_addr_restricted(stcb, sifa))) ||
2779 (non_asoc_addr_ok &&
2780 (sctp_is_addr_restricted(stcb, sifa)) &&
2781 (!sctp_is_addr_pending(stcb, sifa)))) {
2783 * It is restricted for some reason..
2784 * probably not yet added.
2789 num_eligible_addr++;
2791 return (num_eligible_addr);
2794 static struct sctp_ifa *
2795 sctp_choose_boundall(struct sctp_inpcb *inp,
2796 struct sctp_tcb *stcb,
2797 struct sctp_nets *net,
2800 uint8_t dest_is_priv,
2801 uint8_t dest_is_loop,
2802 int non_asoc_addr_ok,
2805 int cur_addr_num = 0, num_preferred = 0;
2807 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2808 struct sctp_ifa *sctp_ifa, *sifa;
2810 struct sctp_vrf *vrf;
2813 * For boundall we can use any address in the association.
2814 * If non_asoc_addr_ok is set we can use any address (at least in
2815 * theory). So we look for preferred addresses first. If we find one,
2816 * we use it. Otherwise we next try to get an address on the
2817 * interface, which we should be able to do (unless non_asoc_addr_ok
2818 * is false and we are routed out that way). In these cases where we
2819 * can't use the address of the interface we go through all the
2820 * ifn's looking for an address we can use and fill that in. Punting
2821 * means we send back address 0, which will probably cause problems
2822 * actually since then IP will fill in the address of the route ifn,
2823 * which means we probably already rejected it.. i.e. here comes an
2826 vrf = sctp_find_vrf(vrf_id);
2830 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2831 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2832 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2833 if (sctp_ifn == NULL) {
2834 /* ?? We don't have this guy ?? */
2835 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2836 goto bound_all_plan_b;
2838 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2839 ifn_index, sctp_ifn->ifn_name);
2842 cur_addr_num = net->indx_of_eligible_next_to_use;
2844 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2849 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2850 num_preferred, sctp_ifn->ifn_name);
2851 if (num_preferred == 0) {
2853 * no eligible addresses, we must use some other interface
2854 * address if we can find one.
2856 goto bound_all_plan_b;
2859 * Ok we have num_eligible_addr set with how many we can use, this
2860 * may vary from call to call due to addresses being deprecated
2863 if (cur_addr_num >= num_preferred) {
2867 * select the nth address from the list (where cur_addr_num is the
2868 * nth) and 0 is the first one, 1 is the second one etc...
2870 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2872 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2873 dest_is_priv, cur_addr_num, fam, ro);
2875 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2877 atomic_add_int(&sctp_ifa->refcount, 1);
2879 /* save off where the next one we will want */
2880 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2885 * plan_b: Look at all interfaces and find a preferred address. If
2886 * no preferred fall through to plan_c.
2889 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2890 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2891 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2892 sctp_ifn->ifn_name);
2893 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2894 /* wrong base scope */
2895 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2898 if ((sctp_ifn == looked_at) && looked_at) {
2899 /* already looked at this guy */
2900 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2903 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2904 dest_is_loop, dest_is_priv, fam);
2905 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2906 "Found ifn:%p %d preferred source addresses\n",
2907 ifn, num_preferred);
2908 if (num_preferred == 0) {
2909 /* None on this interface. */
2910 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2913 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2914 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2915 num_preferred, sctp_ifn, cur_addr_num);
2918 * Ok we have num_eligible_addr set with how many we can
2919 * use, this may vary from call to call due to addresses
2920 * being deprecated etc..
2922 if (cur_addr_num >= num_preferred) {
2925 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2926 dest_is_priv, cur_addr_num, fam, ro);
2930 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2931 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2933 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2934 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2935 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2936 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2938 atomic_add_int(&sifa->refcount, 1);
2943 /* plan_c: do we have an acceptable address on the emit interface */
2944 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2945 if (emit_ifn == NULL) {
2948 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2949 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2950 (non_asoc_addr_ok == 0))
2952 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2957 if (((non_asoc_addr_ok == 0) &&
2958 (sctp_is_addr_restricted(stcb, sifa))) ||
2959 (non_asoc_addr_ok &&
2960 (sctp_is_addr_restricted(stcb, sifa)) &&
2961 (!sctp_is_addr_pending(stcb, sifa)))) {
2963 * It is restricted for some reason..
2964 * probably not yet added.
2969 atomic_add_int(&sifa->refcount, 1);
2974 * plan_d: We are in trouble. No preferred address on the emit
2975 * interface. And not even a preferred address on all interfaces. Go
2976 * out and see if we can find an acceptable address somewhere
2977 * amongst all interfaces.
2979 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2980 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2981 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2982 /* wrong base scope */
2985 if ((sctp_ifn == looked_at) && looked_at)
2986 /* already looked at this guy */
2989 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2990 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2991 (non_asoc_addr_ok == 0))
2993 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2999 if (((non_asoc_addr_ok == 0) &&
3000 (sctp_is_addr_restricted(stcb, sifa))) ||
3001 (non_asoc_addr_ok &&
3002 (sctp_is_addr_restricted(stcb, sifa)) &&
3003 (!sctp_is_addr_pending(stcb, sifa)))) {
3005 * It is restricted for some
3006 * reason.. probably not yet added.
3011 atomic_add_int(&sifa->refcount, 1);
3016 * Ok we can find NO address to source from that is not on our
3017 * restricted list and non_asoc_address is NOT ok, or it is on our
3018 * restricted list. We can't source to it :-(
3025 /* tcb may be NULL */
3027 sctp_source_address_selection(struct sctp_inpcb *inp,
3028 struct sctp_tcb *stcb,
3030 struct sctp_nets *net,
3031 int non_asoc_addr_ok, uint32_t vrf_id)
3033 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3036 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3039 struct sctp_ifa *answer;
3040 uint8_t dest_is_priv, dest_is_loop;
3044 * Rules: - Find the route if needed, cache if I can. - Look at
3045 * interface address in route, Is it in the bound list. If so we
3046 * have the best source. - If not we must rotate amongst the
3051 * Do we need to pay attention to scope. We can have a private address
3052 * or a global address we are sourcing or sending to. So if we draw
3054 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3056 *------------------------------------------
3057 * source * dest * result
3058 * -----------------------------------------
3059 * <a> Private * Global * NAT
3060 * -----------------------------------------
3061 * <b> Private * Private * No problem
3062 * -----------------------------------------
3063 * <c> Global * Private * Huh, How will this work?
3064 * -----------------------------------------
3065 * <d> Global * Global * No Problem
3066 *------------------------------------------
3067 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3069 *------------------------------------------
3070 * source * dest * result
3071 * -----------------------------------------
3072 * <a> Linklocal * Global *
3073 * -----------------------------------------
3074 * <b> Linklocal * Linklocal * No problem
3075 * -----------------------------------------
3076 * <c> Global * Linklocal * Huh, How will this work?
3077 * -----------------------------------------
3078 * <d> Global * Global * No Problem
3079 *------------------------------------------
3080 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3082 * And then we add to that what happens if there are multiple addresses
3083 * assigned to an interface. Remember the ifa on a ifn is a linked
3084 * list of addresses. So one interface can have more than one IP
3085 * address. What happens if we have both a private and a global
3086 * address? Do we then use context of destination to sort out which
3087 * one is best? And what about NAT's sending P->G may get you a NAT
3088 * translation, or should you select the G thats on the interface in
3093 * - count the number of addresses on the interface.
3094 * - if it is one, no problem except case <c>.
3095 * For <a> we will assume a NAT out there.
3096 * - if there are more than one, then we need to worry about scope P
3097 * or G. We should prefer G -> G and P -> P if possible.
3098 * Then as a secondary fall back to mixed types G->P being a last
3100 * - The above all works for bound all, but bound specific we need to
3101 * use the same concept but instead only consider the bound
3102 * addresses. If the bound set is NOT assigned to the interface then
3103 * we must use rotation amongst the bound addresses..
3105 if (ro->ro_rt == NULL) {
3107 * Need a route to cache.
3109 SCTP_RTALLOC(ro, vrf_id);
3111 if (ro->ro_rt == NULL) {
3114 fam = to->sin_family;
3115 dest_is_priv = dest_is_loop = 0;
3116 /* Setup our scopes for the destination */
3119 /* Scope based on outbound address */
3120 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3123 /* mark it as local */
3124 net->addr_is_local = 1;
3126 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3132 /* Scope based on outbound address */
3133 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3134 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3136 * If the address is a loopback address, which
3137 * consists of "::1" OR "fe80::1%lo0", we are
3138 * loopback scope. But we don't use dest_is_priv
3139 * (link local addresses).
3143 /* mark it as local */
3144 net->addr_is_local = 1;
3146 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3152 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3153 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3154 SCTP_IPI_ADDR_RLOCK();
3155 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3159 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3160 dest_is_priv, dest_is_loop,
3161 non_asoc_addr_ok, fam);
3162 SCTP_IPI_ADDR_RUNLOCK();
3169 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3170 vrf_id, dest_is_priv,
3172 non_asoc_addr_ok, fam);
3174 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3179 SCTP_IPI_ADDR_RUNLOCK();
3184 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3189 tlen = SCTP_BUF_LEN(control);
3192 * Independent of how many mbufs, find the c_type inside the control
3193 * structure and copy out the data.
3196 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3197 /* not enough room for one more we are done. */
3200 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3201 if (((int)cmh.cmsg_len + at) > tlen) {
3203 * this is real messed up since there is not enough
3204 * data here to cover the cmsg header. We are done.
3208 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3209 (c_type == cmh.cmsg_type)) {
3210 /* found the one we want, copy it out */
3211 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3212 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3214 * space of cmsg_len after header not big
3219 m_copydata(control, at, cpsize, data);
3222 at += CMSG_ALIGN(cmh.cmsg_len);
3223 if (cmh.cmsg_len == 0) {
3232 static struct mbuf *
3233 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3234 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3236 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3237 struct sctp_state_cookie *stc;
3238 struct sctp_paramhdr *ph;
3244 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3245 sizeof(struct sctp_paramhdr)), 0,
3246 M_DONTWAIT, 1, MT_DATA);
3250 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3251 if (copy_init == NULL) {
3255 #ifdef SCTP_MBUF_LOGGING
3256 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3261 if (SCTP_BUF_IS_EXTENDED(mat)) {
3262 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3264 mat = SCTP_BUF_NEXT(mat);
3268 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3270 if (copy_initack == NULL) {
3272 sctp_m_freem(copy_init);
3275 #ifdef SCTP_MBUF_LOGGING
3276 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3281 if (SCTP_BUF_IS_EXTENDED(mat)) {
3282 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3284 mat = SCTP_BUF_NEXT(mat);
3288 /* easy side we just drop it on the end */
3289 ph = mtod(mret, struct sctp_paramhdr *);
3290 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3291 sizeof(struct sctp_paramhdr);
3292 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3293 sizeof(struct sctp_paramhdr));
3294 ph->param_type = htons(SCTP_STATE_COOKIE);
3295 ph->param_length = 0; /* fill in at the end */
3296 /* Fill in the stc cookie data */
3297 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3299 /* tack the INIT and then the INIT-ACK onto the chain */
3302 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3303 cookie_sz += SCTP_BUF_LEN(m_at);
3304 if (SCTP_BUF_NEXT(m_at) == NULL) {
3305 SCTP_BUF_NEXT(m_at) = copy_init;
3310 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3311 cookie_sz += SCTP_BUF_LEN(m_at);
3312 if (SCTP_BUF_NEXT(m_at) == NULL) {
3313 SCTP_BUF_NEXT(m_at) = copy_initack;
3318 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3319 cookie_sz += SCTP_BUF_LEN(m_at);
3320 if (SCTP_BUF_NEXT(m_at) == NULL) {
3324 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3326 /* no space, so free the entire chain */
3330 SCTP_BUF_LEN(sig) = 0;
3331 SCTP_BUF_NEXT(m_at) = sig;
3333 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3334 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3336 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3337 cookie_sz += SCTP_SIGNATURE_SIZE;
3338 ph->param_length = htons(cookie_sz);
3344 sctp_get_ect(struct sctp_tcb *stcb,
3345 struct sctp_tmit_chunk *chk)
3347 uint8_t this_random;
3350 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3353 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3354 /* no nonce, always return ECT0 */
3355 return (SCTP_ECT0_BIT);
3357 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3358 /* Peer does NOT support it, so we send a ECT0 only */
3359 return (SCTP_ECT0_BIT);
3362 return (SCTP_ECT0_BIT);
3364 if ((stcb->asoc.hb_random_idx > 3) ||
3365 ((stcb->asoc.hb_random_idx == 3) &&
3366 (stcb->asoc.hb_ect_randombit > 7))) {
3370 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3371 memcpy(stcb->asoc.hb_random_values, &rndval,
3372 sizeof(stcb->asoc.hb_random_values));
3373 this_random = stcb->asoc.hb_random_values[0];
3374 stcb->asoc.hb_random_idx = 0;
3375 stcb->asoc.hb_ect_randombit = 0;
3377 if (stcb->asoc.hb_ect_randombit > 7) {
3378 stcb->asoc.hb_ect_randombit = 0;
3379 stcb->asoc.hb_random_idx++;
3380 if (stcb->asoc.hb_random_idx > 3) {
3384 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3386 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3388 /* ECN Nonce stuff */
3389 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3390 stcb->asoc.hb_ect_randombit++;
3391 return (SCTP_ECT1_BIT);
3393 stcb->asoc.hb_ect_randombit++;
3394 return (SCTP_ECT0_BIT);
3399 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3400 struct sctp_tcb *stcb, /* may be NULL */
3401 struct sctp_nets *net,
3402 struct sockaddr *to,
3404 uint32_t auth_offset,
3405 struct sctp_auth_chunk *auth,
3406 uint16_t auth_keyid,
3407 int nofragment_flag,
3409 struct sctp_tmit_chunk *chk,
3416 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3419 union sctp_sockstore *over_addr
3421 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3424 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3425 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3426 * structure: - fill in the HMAC digest of any AUTH chunk in the
3427 * packet. - calculate and fill in the SCTP checksum. - prepend an
3428 * IP address header. - if boundall use INADDR_ANY. - if
3429 * boundspecific do source address selection. - set fragmentation
3430 * option for ipV4. - On return from IP output, check/adjust mtu
3431 * size of output interface and smallest_mtu size as well.
3433 /* Will need ifdefs around this */
3436 struct sctphdr *sctphdr;
3440 sctp_route_t *ro = NULL;
3441 struct udphdr *udp = NULL;
3443 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3444 struct socket *so = NULL;
3448 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3449 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3454 vrf_id = stcb->asoc.vrf_id;
3456 vrf_id = inp->def_vrf_id;
3459 /* fill in the HMAC digest for any AUTH chunk in the packet */
3460 if ((auth != NULL) && (stcb != NULL)) {
3461 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3463 if (to->sa_family == AF_INET) {
3464 struct ip *ip = NULL;
3465 sctp_route_t iproute;
3469 len = sizeof(struct ip) + sizeof(struct sctphdr);
3471 len += sizeof(struct udphdr);
3473 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3476 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3479 SCTP_ALIGN_TO_END(newm, len);
3480 SCTP_BUF_LEN(newm) = len;
3481 SCTP_BUF_NEXT(newm) = m;
3483 packet_length = sctp_calculate_len(m);
3484 ip = mtod(m, struct ip *);
3485 ip->ip_v = IPVERSION;
3486 ip->ip_hl = (sizeof(struct ip) >> 2);
3488 tos_value = net->tos_flowlabel & 0x000000ff;
3490 tos_value = inp->ip_inp.inp.inp_ip_tos;
3492 if ((nofragment_flag) && (port == 0)) {
3493 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3496 ip->ip_off = htons(IP_DF);
3501 /* FreeBSD has a function for ip_id's */
3502 ip->ip_id = ip_newid();
3504 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3505 ip->ip_len = packet_length;
3507 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3509 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3512 ip->ip_tos = (u_char)(tos_value & 0xfc);
3515 /* no association at all */
3516 ip->ip_tos = (tos_value & 0xfc);
3519 ip->ip_p = IPPROTO_UDP;
3521 ip->ip_p = IPPROTO_SCTP;
3526 memset(&iproute, 0, sizeof(iproute));
3527 memcpy(&ro->ro_dst, to, to->sa_len);
3529 ro = (sctp_route_t *) & net->ro;
3531 /* Now the address selection part */
3532 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3534 /* call the routine to select the src address */
3535 if (net && out_of_asoc_ok == 0) {
3536 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3537 sctp_free_ifa(net->ro._s_addr);
3538 net->ro._s_addr = NULL;
3539 net->src_addr_selected = 0;
3545 if (net->src_addr_selected == 0) {
3546 /* Cache the source address */
3547 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3550 net->src_addr_selected = 1;
3552 if (net->ro._s_addr == NULL) {
3553 /* No route to host */
3554 net->src_addr_selected = 0;
3557 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3559 if (over_addr == NULL) {
3560 struct sctp_ifa *_lsrc;
3562 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3566 if (_lsrc == NULL) {
3569 ip->ip_src = _lsrc->address.sin.sin_addr;
3570 sctp_free_ifa(_lsrc);
3572 ip->ip_src = over_addr->sin.sin_addr;
3573 SCTP_RTALLOC(ro, vrf_id);
3577 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3578 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3579 udp->uh_dport = port;
3580 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3581 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3582 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3584 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3587 sctphdr->src_port = src_port;
3588 sctphdr->dest_port = dest_port;
3589 sctphdr->v_tag = v_tag;
3590 sctphdr->checksum = 0;
3593 * If source address selection fails and we find no route
3594 * then the ip_output should fail as well with a
3595 * NO_ROUTE_TO_HOST type error. We probably should catch
3596 * that somewhere and abort the association right away
3597 * (assuming this is an INIT being sent).
3599 if ((ro->ro_rt == NULL)) {
3601 * src addr selection failed to find a route (or
3602 * valid source addr), so we can't get there from
3606 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3607 "%s: dropped packet - no valid source addr\n",
3610 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3611 "Destination was ");
3612 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3613 &net->ro._l_addr.sa);
3614 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3615 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3616 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3617 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3619 SCTP_FAILED_THRESHOLD,
3622 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3623 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3625 * JRS 5/14/07 - If a
3627 * unreachable, the PF bit
3628 * is turned off. This
3629 * allows an unambiguous use
3631 * destinations that are
3632 * reachable but potentially
3634 * destination is set to the
3635 * unreachable state, also
3636 * set the destination to
3640 * Add debug message here if
3641 * destination is not in PF
3645 * Stop any running T3
3648 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3649 net->dest_state &= ~SCTP_ADDR_PF;
3650 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3656 if (net == stcb->asoc.primary_destination) {
3657 /* need a new primary */
3658 struct sctp_nets *alt;
3660 alt = sctp_find_alternate_net(stcb, net, 0);
3662 if (sctp_set_primary_addr(stcb,
3663 (struct sockaddr *)NULL,
3665 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3666 if (net->ro._s_addr) {
3667 sctp_free_ifa(net->ro._s_addr);
3668 net->ro._s_addr = NULL;
3670 net->src_addr_selected = 0;
3676 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3678 return (EHOSTUNREACH);
3680 if (ro != &iproute) {
3681 memcpy(&iproute, ro, sizeof(*ro));
3683 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3684 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3685 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3686 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3687 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3690 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3691 /* failed to prepend data, give up */
3692 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3696 #ifdef SCTP_PACKET_LOGGING
3697 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3698 sctp_packet_log(m, packet_length);
3700 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3702 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3704 (stcb->asoc.loopback_scope))) {
3705 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3706 SCTP_STAT_INCR(sctps_sendswcrc);
3708 SCTP_STAT_INCR(sctps_sendnocrc);
3710 SCTP_ENABLE_UDP_CSUM(o_pak);
3712 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3714 (stcb->asoc.loopback_scope))) {
3715 m->m_pkthdr.csum_flags = CSUM_SCTP;
3716 m->m_pkthdr.csum_data = 0; /* FIXME MT */
3717 SCTP_STAT_INCR(sctps_sendhwcrc);
3719 SCTP_STAT_INCR(sctps_sendnocrc);
3722 /* send it out. table id is taken from stcb */
3723 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 so = SCTP_INP_SO(inp);
3726 SCTP_SOCKET_UNLOCK(so, 0);
3729 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3730 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3731 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3732 atomic_add_int(&stcb->asoc.refcnt, 1);
3733 SCTP_TCB_UNLOCK(stcb);
3734 SCTP_SOCKET_LOCK(so, 0);
3735 SCTP_TCB_LOCK(stcb);
3736 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3739 SCTP_STAT_INCR(sctps_sendpackets);
3740 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3742 SCTP_STAT_INCR(sctps_senderrors);
3744 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3746 /* free tempy routes */
3752 /* PMTU check versus smallest asoc MTU goes here */
3753 if ((ro->ro_rt != NULL) &&
3754 (net->ro._s_addr)) {
3757 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3759 mtu -= sizeof(struct udphdr);
3761 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3762 #ifdef SCTP_PRINT_FOR_B_AND_M
3763 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3765 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3768 } else if (ro->ro_rt == NULL) {
3769 /* route was freed */
3770 if (net->ro._s_addr &&
3771 net->src_addr_selected) {
3772 sctp_free_ifa(net->ro._s_addr);
3773 net->ro._s_addr = NULL;
3775 net->src_addr_selected = 0;
3781 else if (to->sa_family == AF_INET6) {
3783 struct ip6_hdr *ip6h;
3784 struct route_in6 ip6route;
3787 uint16_t flowBottom;
3788 u_char tosBottom, tosTop;
3789 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3791 struct sockaddr_in6 lsa6_storage;
3793 u_short prev_port = 0;
3797 flowlabel = net->tos_flowlabel;
3799 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3802 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3804 len += sizeof(struct udphdr);
3806 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3809 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3812 SCTP_ALIGN_TO_END(newm, len);
3813 SCTP_BUF_LEN(newm) = len;
3814 SCTP_BUF_NEXT(newm) = m;
3816 packet_length = sctp_calculate_len(m);
3818 ip6h = mtod(m, struct ip6_hdr *);
3820 * We assume here that inp_flow is in host byte order within
3823 flowBottom = flowlabel & 0x0000ffff;
3824 flowTop = ((flowlabel & 0x000f0000) >> 16);
3825 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3826 /* protect *sin6 from overwrite */
3827 sin6 = (struct sockaddr_in6 *)to;
3831 /* KAME hack: embed scopeid */
3832 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3833 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3837 memset(&ip6route, 0, sizeof(ip6route));
3838 ro = (sctp_route_t *) & ip6route;
3839 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3841 ro = (sctp_route_t *) & net->ro;
3844 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3846 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3849 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3852 /* we could get no asoc if it is a O-O-T-B packet */
3853 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3855 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3857 ip6h->ip6_nxt = IPPROTO_UDP;
3859 ip6h->ip6_nxt = IPPROTO_SCTP;
3861 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3862 ip6h->ip6_dst = sin6->sin6_addr;
3865 * Add SRC address selection here: we can only reuse to a
3866 * limited degree the kame src-addr-sel, since we can try
3867 * their selection but it may not be bound.
3869 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3870 lsa6_tmp.sin6_family = AF_INET6;
3871 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3873 if (net && out_of_asoc_ok == 0) {
3874 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3875 sctp_free_ifa(net->ro._s_addr);
3876 net->ro._s_addr = NULL;
3877 net->src_addr_selected = 0;
3883 if (net->src_addr_selected == 0) {
3884 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3885 /* KAME hack: embed scopeid */
3886 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3887 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3890 /* Cache the source address */
3891 net->ro._s_addr = sctp_source_address_selection(inp,
3897 (void)sa6_recoverscope(sin6);
3898 net->src_addr_selected = 1;
3900 if (net->ro._s_addr == NULL) {
3901 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3902 net->src_addr_selected = 0;
3905 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3907 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3908 /* KAME hack: embed scopeid */
3909 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3910 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3913 if (over_addr == NULL) {
3914 struct sctp_ifa *_lsrc;
3916 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3920 if (_lsrc == NULL) {
3923 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3924 sctp_free_ifa(_lsrc);
3926 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3927 SCTP_RTALLOC(ro, vrf_id);
3929 (void)sa6_recoverscope(sin6);
3931 lsa6->sin6_port = inp->sctp_lport;
3933 if (ro->ro_rt == NULL) {
3935 * src addr selection failed to find a route (or
3936 * valid source addr), so we can't get there from
3942 * XXX: sa6 may not have a valid sin6_scope_id in the
3943 * non-SCOPEDROUTING case.
3945 bzero(&lsa6_storage, sizeof(lsa6_storage));
3946 lsa6_storage.sin6_family = AF_INET6;
3947 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3948 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3949 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3950 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3955 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3956 lsa6_storage.sin6_port = inp->sctp_lport;
3957 lsa6 = &lsa6_storage;
3958 ip6h->ip6_src = lsa6->sin6_addr;
3961 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3962 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3963 udp->uh_dport = port;
3964 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3966 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3968 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3971 sctphdr->src_port = src_port;
3972 sctphdr->dest_port = dest_port;
3973 sctphdr->v_tag = v_tag;
3974 sctphdr->checksum = 0;
3977 * We set the hop limit now since there is a good chance
3978 * that our ro pointer is now filled
3980 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3981 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3984 /* Copy to be sure something bad is not happening */
3985 sin6->sin6_addr = ip6h->ip6_dst;
3986 lsa6->sin6_addr = ip6h->ip6_src;
3989 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3990 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3991 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3992 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3993 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3995 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3996 /* preserve the port and scope for link local send */
3997 prev_scope = sin6->sin6_scope_id;
3998 prev_port = sin6->sin6_port;
4000 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4001 /* failed to prepend data, give up */
4003 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4006 #ifdef SCTP_PACKET_LOGGING
4007 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4008 sctp_packet_log(m, packet_length);
4010 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4012 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4014 (stcb->asoc.loopback_scope))) {
4015 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4016 SCTP_STAT_INCR(sctps_sendswcrc);
4018 SCTP_STAT_INCR(sctps_sendnocrc);
4020 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4021 udp->uh_sum = 0xffff;
4024 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4026 (stcb->asoc.loopback_scope))) {
4027 m->m_pkthdr.csum_flags = CSUM_SCTP;
4028 m->m_pkthdr.csum_data = 0; /* FIXME MT */
4029 SCTP_STAT_INCR(sctps_sendhwcrc);
4031 SCTP_STAT_INCR(sctps_sendnocrc);
4034 /* send it out. table id is taken from stcb */
4035 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4036 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4037 so = SCTP_INP_SO(inp);
4038 SCTP_SOCKET_UNLOCK(so, 0);
4041 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4042 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4043 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4044 atomic_add_int(&stcb->asoc.refcnt, 1);
4045 SCTP_TCB_UNLOCK(stcb);
4046 SCTP_SOCKET_LOCK(so, 0);
4047 SCTP_TCB_LOCK(stcb);
4048 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4052 /* for link local this must be done */
4053 sin6->sin6_scope_id = prev_scope;
4054 sin6->sin6_port = prev_port;
4056 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4057 SCTP_STAT_INCR(sctps_sendpackets);
4058 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4060 SCTP_STAT_INCR(sctps_senderrors);
4063 /* Now if we had a temp route free it */
4068 /* PMTU check versus smallest asoc MTU goes here */
4069 if (ro->ro_rt == NULL) {
4070 /* Route was freed */
4071 if (net->ro._s_addr &&
4072 net->src_addr_selected) {
4073 sctp_free_ifa(net->ro._s_addr);
4074 net->ro._s_addr = NULL;
4076 net->src_addr_selected = 0;
4078 if ((ro->ro_rt != NULL) &&
4079 (net->ro._s_addr)) {
4082 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4084 (stcb->asoc.smallest_mtu > mtu)) {
4085 #ifdef SCTP_PRINT_FOR_B_AND_M
4086 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4089 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4092 net->mtu -= sizeof(struct udphdr);
4096 if (ND_IFINFO(ifp)->linkmtu &&
4097 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4098 #ifdef SCTP_PRINT_FOR_B_AND_M
4099 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4100 ND_IFINFO(ifp)->linkmtu);
4102 sctp_mtu_size_reset(inp,
4104 ND_IFINFO(ifp)->linkmtu);
4112 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4113 ((struct sockaddr *)to)->sa_family);
4115 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4122 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4123 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4128 struct mbuf *m, *m_at, *mp_last;
4129 struct sctp_nets *net;
4130 struct sctp_init_chunk *init;
4131 struct sctp_supported_addr_param *sup_addr;
4132 struct sctp_adaptation_layer_indication *ali;
4133 struct sctp_ecn_supported_param *ecn;
4134 struct sctp_prsctp_supported_param *prsctp;
4135 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4136 struct sctp_supported_chunk_types_param *pr_supported;
4137 int cnt_inits_to = 0;
4142 /* INIT's always go to the primary (and usually ONLY address) */
4144 net = stcb->asoc.primary_destination;
4146 net = TAILQ_FIRST(&stcb->asoc.nets);
4151 /* we confirm any address we send an INIT to */
4152 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4153 (void)sctp_set_primary_addr(stcb, NULL, net);
4155 /* we confirm any address we send an INIT to */
4156 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4158 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4160 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4162 * special hook, if we are sending to link local it will not
4163 * show up in our private address count.
4165 struct sockaddr_in6 *sin6l;
4167 sin6l = &net->ro._l_addr.sin6;
4168 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4172 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4173 /* This case should not happen */
4174 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4177 /* start the INIT timer */
4178 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4180 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4182 /* No memory, INIT timer will re-attempt. */
4183 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4186 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4188 * assume peer supports asconf in order to be able to queue local
4189 * address changes while an INIT is in flight and before the assoc
4192 stcb->asoc.peer_supports_asconf = 1;
4193 /* Now lets put the SCTP header in place */
4194 init = mtod(m, struct sctp_init_chunk *);
4195 /* now the chunk header */
4196 init->ch.chunk_type = SCTP_INITIATION;
4197 init->ch.chunk_flags = 0;
4198 /* fill in later from mbuf we build */
4199 init->ch.chunk_length = 0;
4200 /* place in my tag */
4201 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4202 /* set up some of the credits. */
4203 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4204 SCTP_MINIMAL_RWND));
4206 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4207 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4208 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4209 /* now the address restriction */
4210 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4212 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4214 /* we support 2 types: IPv6/IPv4 */
4215 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4216 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4217 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4219 /* we support 1 type: IPv4 */
4220 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4221 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4222 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4224 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4225 /* adaptation layer indication parameter */
4226 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4227 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4228 ali->ph.param_length = htons(sizeof(*ali));
4229 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4230 SCTP_BUF_LEN(m) += sizeof(*ali);
4231 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4233 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4234 /* Add NAT friendly parameter */
4235 struct sctp_paramhdr *ph;
4237 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4238 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4239 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4240 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4241 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4243 /* now any cookie time extensions */
4244 if (stcb->asoc.cookie_preserve_req) {
4245 struct sctp_cookie_perserve_param *cookie_preserve;
4247 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4248 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4249 cookie_preserve->ph.param_length = htons(
4250 sizeof(*cookie_preserve));
4251 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4252 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4253 ecn = (struct sctp_ecn_supported_param *)(
4254 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4255 stcb->asoc.cookie_preserve_req = 0;
4258 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4259 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4260 ecn->ph.param_length = htons(sizeof(*ecn));
4261 SCTP_BUF_LEN(m) += sizeof(*ecn);
4262 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4265 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4267 /* And now tell the peer we do pr-sctp */
4268 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4269 prsctp->ph.param_length = htons(sizeof(*prsctp));
4270 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4272 /* And now tell the peer we do all the extensions */
4273 pr_supported = (struct sctp_supported_chunk_types_param *)
4274 ((caddr_t)prsctp + sizeof(*prsctp));
4275 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4277 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4278 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4279 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4280 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4281 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4282 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4283 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4286 * EY if the initiator supports nr_sacks, need to report that to
4287 * responder in INIT chunk
4289 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4290 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4292 p_len = sizeof(*pr_supported) + num_ext;
4293 pr_supported->ph.param_length = htons(p_len);
4294 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4295 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4298 /* ECN nonce: And now tell the peer we support ECN nonce */
4299 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4300 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4301 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4302 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4303 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4304 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4306 /* add authentication parameters */
4307 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4308 struct sctp_auth_random *randp;
4309 struct sctp_auth_hmac_algo *hmacs;
4310 struct sctp_auth_chunk_list *chunks;
4312 /* attach RANDOM parameter, if available */
4313 if (stcb->asoc.authinfo.random != NULL) {
4314 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4315 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4316 /* random key already contains the header */
4317 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4318 /* zero out any padding required */
4319 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4320 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4322 /* add HMAC_ALGO parameter */
4323 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4324 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4325 (uint8_t *) hmacs->hmac_ids);
4327 p_len += sizeof(*hmacs);
4328 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4329 hmacs->ph.param_length = htons(p_len);
4330 /* zero out any padding required */
4331 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4332 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4334 /* add CHUNKS parameter */
4335 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4336 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4337 chunks->chunk_types);
4339 p_len += sizeof(*chunks);
4340 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4341 chunks->ph.param_length = htons(p_len);
4342 /* zero out any padding required */
4343 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4344 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4348 /* now the addresses */
4350 struct sctp_scoping scp;
4353 * To optimize this we could put the scoping stuff into a
4354 * structure and remove the individual uint8's from the
4355 * assoc structure. Then we could just sifa in the address
4356 * within the stcb.. but for now this is a quick hack to get
4357 * the address stuff teased apart.
4359 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4360 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4361 scp.loopback_scope = stcb->asoc.loopback_scope;
4362 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4363 scp.local_scope = stcb->asoc.local_scope;
4364 scp.site_scope = stcb->asoc.site_scope;
4366 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4369 /* calulate the size and update pkt header and chunk header */
4371 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4372 if (SCTP_BUF_NEXT(m_at) == NULL)
4374 p_len += SCTP_BUF_LEN(m_at);
4376 init->ch.chunk_length = htons(p_len);
4378 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4379 * here since the timer will drive a retranmission.
4382 /* I don't expect this to execute but we will be safe here */
4384 if ((padval) && (mp_last)) {
4386 * The compiler worries that mp_last may not be set even
4387 * though I think it is impossible :-> however we add
4388 * mp_last here just in case.
4390 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4392 /* Houston we have a problem, no space */
4398 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4399 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4400 (struct sockaddr *)&net->ro._l_addr,
4401 m, 0, NULL, 0, 0, 0, NULL, 0,
4402 inp->sctp_lport, stcb->rport, htonl(0),
4403 net->port, so_locked, NULL);
4404 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4405 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4406 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4410 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4411 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4414 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4415 * being equal to the beginning of the params i.e. (iphlen +
4416 * sizeof(struct sctp_init_msg) parse through the parameters to the
4417 * end of the mbuf verifying that all parameters are known.
4419 * For unknown parameters build and return a mbuf with
4420 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4421 * processing this chunk stop, and set *abort_processing to 1.
4423 * By having param_offset be pre-set to where parameters begin it is
4424 * hoped that this routine may be reused in the future by new
4427 struct sctp_paramhdr *phdr, params;
4429 struct mbuf *mat, *op_err;
4430 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4431 int at, limit, pad_needed;
4432 uint16_t ptype, plen, padded_size;
4435 *abort_processing = 0;
4438 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4441 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4442 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4443 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4444 ptype = ntohs(phdr->param_type);
4445 plen = ntohs(phdr->param_length);
4446 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4447 /* wacked parameter */
4448 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4451 limit -= SCTP_SIZE32(plen);
4453 * All parameters for all chunks that we know/understand are
4454 * listed here. We process them other places and make
4455 * appropriate stop actions per the upper bits. However this
4456 * is the generic routine processor's can call to get back
4457 * an operr.. to either incorporate (init-ack) or send.
4459 padded_size = SCTP_SIZE32(plen);
4461 /* Param's with variable size */
4462 case SCTP_HEARTBEAT_INFO:
4463 case SCTP_STATE_COOKIE:
4464 case SCTP_UNRECOG_PARAM:
4465 case SCTP_ERROR_CAUSE_IND:
4469 /* Param's with variable size within a range */
4470 case SCTP_CHUNK_LIST:
4471 case SCTP_SUPPORTED_CHUNK_EXT:
4472 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4473 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4478 case SCTP_SUPPORTED_ADDRTYPE:
4479 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4480 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4486 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4487 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4492 case SCTP_SET_PRIM_ADDR:
4493 case SCTP_DEL_IP_ADDRESS:
4494 case SCTP_ADD_IP_ADDRESS:
4495 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4496 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4497 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4502 /* Param's with a fixed size */
4503 case SCTP_IPV4_ADDRESS:
4504 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4505 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4510 case SCTP_IPV6_ADDRESS:
4511 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4512 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4517 case SCTP_COOKIE_PRESERVE:
4518 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4519 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4524 case SCTP_HAS_NAT_SUPPORT:
4527 case SCTP_ECN_NONCE_SUPPORTED:
4528 case SCTP_PRSCTP_SUPPORTED:
4530 if (padded_size != sizeof(struct sctp_paramhdr)) {
4531 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4536 case SCTP_ECN_CAPABLE:
4537 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4538 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4543 case SCTP_ULP_ADAPTATION:
4544 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4545 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4550 case SCTP_SUCCESS_REPORT:
4551 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4552 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4557 case SCTP_HOSTNAME_ADDRESS:
4559 /* We can NOT handle HOST NAME addresses!! */
4562 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4563 *abort_processing = 1;
4564 if (op_err == NULL) {
4565 /* Ok need to try to get a mbuf */
4567 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4569 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4572 l_len += sizeof(struct sctp_paramhdr);
4573 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4575 SCTP_BUF_LEN(op_err) = 0;
4577 * pre-reserve space for ip
4578 * and sctp header and
4582 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4584 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4586 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4587 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4591 /* If we have space */
4592 struct sctp_paramhdr s;
4595 uint32_t cpthis = 0;
4597 pad_needed = 4 - (err_at % 4);
4598 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4599 err_at += pad_needed;
4601 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4602 s.param_length = htons(sizeof(s) + plen);
4603 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4604 err_at += sizeof(s);
4605 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4607 sctp_m_freem(op_err);
4609 * we are out of memory but
4610 * we still need to have a
4611 * look at what to do (the
4612 * system is in trouble
4617 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4625 * we do not recognize the parameter figure out what
4628 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4629 if ((ptype & 0x4000) == 0x4000) {
4630 /* Report bit is set?? */
4631 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4632 if (op_err == NULL) {
4635 /* Ok need to try to get an mbuf */
4637 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4639 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4642 l_len += sizeof(struct sctp_paramhdr);
4643 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4645 SCTP_BUF_LEN(op_err) = 0;
4647 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4649 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4651 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4652 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4656 /* If we have space */
4657 struct sctp_paramhdr s;
4660 uint32_t cpthis = 0;
4662 pad_needed = 4 - (err_at % 4);
4663 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4664 err_at += pad_needed;
4666 s.param_type = htons(SCTP_UNRECOG_PARAM);
4667 s.param_length = htons(sizeof(s) + plen);
4668 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4669 err_at += sizeof(s);
4670 if (plen > sizeof(tempbuf)) {
4671 plen = sizeof(tempbuf);
4673 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4675 sctp_m_freem(op_err);
4677 * we are out of memory but
4678 * we still need to have a
4679 * look at what to do (the
4680 * system is in trouble
4684 goto more_processing;
4686 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4691 if ((ptype & 0x8000) == 0x0000) {
4692 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4695 /* skip this chunk and continue processing */
4696 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4697 at += SCTP_SIZE32(plen);
4702 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4706 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4707 *abort_processing = 1;
4708 if ((op_err == NULL) && phdr) {
4712 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4714 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4716 l_len += (2 * sizeof(struct sctp_paramhdr));
4717 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4719 SCTP_BUF_LEN(op_err) = 0;
4721 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4723 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4725 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4726 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4729 if ((op_err) && phdr) {
4730 struct sctp_paramhdr s;
4733 uint32_t cpthis = 0;
4735 pad_needed = 4 - (err_at % 4);
4736 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4737 err_at += pad_needed;
4739 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4740 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4741 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4742 err_at += sizeof(s);
4743 /* Only copy back the p-hdr that caused the issue */
4744 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4750 sctp_are_there_new_addresses(struct sctp_association *asoc,
4751 struct mbuf *in_initpkt, int iphlen, int offset)
4754 * Given a INIT packet, look through the packet to verify that there
4755 * are NO new addresses. As we go through the parameters add reports
4756 * of any un-understood parameters that require an error. Also we
4757 * must return (1) to drop the packet if we see a un-understood
4758 * parameter that tells us to drop the chunk.
4760 struct sockaddr_in sin4, *sa4;
4763 struct sockaddr_in6 sin6, *sa6;
4766 struct sockaddr *sa_touse;
4767 struct sockaddr *sa;
4768 struct sctp_paramhdr *phdr, params;
4772 struct ip6_hdr *ip6h;
4776 uint16_t ptype, plen;
4779 struct sctp_nets *net;
4781 memset(&sin4, 0, sizeof(sin4));
4783 memset(&sin6, 0, sizeof(sin6));
4785 sin4.sin_family = AF_INET;
4786 sin4.sin_len = sizeof(sin4);
4788 sin6.sin6_family = AF_INET6;
4789 sin6.sin6_len = sizeof(sin6);
4792 /* First what about the src address of the pkt ? */
4793 iph = mtod(in_initpkt, struct ip *);
4794 switch (iph->ip_v) {
4796 /* source addr is IPv4 */
4797 sin4.sin_addr = iph->ip_src;
4798 sa_touse = (struct sockaddr *)&sin4;
4801 case IPV6_VERSION >> 4:
4802 /* source addr is IPv6 */
4803 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4804 sin6.sin6_addr = ip6h->ip6_src;
4805 sa_touse = (struct sockaddr *)&sin6;
4813 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4814 sa = (struct sockaddr *)&net->ro._l_addr;
4815 if (sa->sa_family == sa_touse->sa_family) {
4816 if (sa->sa_family == AF_INET) {
4817 sa4 = (struct sockaddr_in *)sa;
4818 if (sa4->sin_addr.s_addr ==
4819 sin4.sin_addr.s_addr) {
4825 if (sa->sa_family == AF_INET6) {
4826 sa6 = (struct sockaddr_in6 *)sa;
4827 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4837 /* New address added! no need to look futher. */
4840 /* Ok so far lets munge through the rest of the packet */
4844 offset += sizeof(struct sctp_init_chunk);
4845 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4847 ptype = ntohs(phdr->param_type);
4848 plen = ntohs(phdr->param_length);
4849 if (ptype == SCTP_IPV4_ADDRESS) {
4850 struct sctp_ipv4addr_param *p4, p4_buf;
4852 phdr = sctp_get_next_param(mat, offset,
4853 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4854 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4858 p4 = (struct sctp_ipv4addr_param *)phdr;
4859 sin4.sin_addr.s_addr = p4->addr;
4860 sa_touse = (struct sockaddr *)&sin4;
4861 } else if (ptype == SCTP_IPV6_ADDRESS) {
4862 struct sctp_ipv6addr_param *p6, p6_buf;
4864 phdr = sctp_get_next_param(mat, offset,
4865 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4866 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4870 p6 = (struct sctp_ipv6addr_param *)phdr;
4872 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4875 sa_touse = (struct sockaddr *)&sin4;
4878 /* ok, sa_touse points to one to check */
4880 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4881 sa = (struct sockaddr *)&net->ro._l_addr;
4882 if (sa->sa_family != sa_touse->sa_family) {
4885 if (sa->sa_family == AF_INET) {
4886 sa4 = (struct sockaddr_in *)sa;
4887 if (sa4->sin_addr.s_addr ==
4888 sin4.sin_addr.s_addr) {
4894 if (sa->sa_family == AF_INET6) {
4895 sa6 = (struct sockaddr_in6 *)sa;
4896 if (SCTP6_ARE_ADDR_EQUAL(
4905 /* New addr added! no need to look further */
4909 offset += SCTP_SIZE32(plen);
4910 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4916 * Given a MBUF chain that was sent into us containing an INIT. Build a
4917 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4918 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4919 * message (i.e. the struct sctp_init_msg).
4922 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4923 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4924 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4926 struct sctp_association *asoc;
4927 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4928 struct sctp_init_ack_chunk *initack;
4929 struct sctp_adaptation_layer_indication *ali;
4930 struct sctp_ecn_supported_param *ecn;
4931 struct sctp_prsctp_supported_param *prsctp;
4932 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4933 struct sctp_supported_chunk_types_param *pr_supported;
4934 union sctp_sockstore store, store1, *over_addr;
4935 struct sockaddr_in *sin, *to_sin;
4938 struct sockaddr_in6 *sin6, *to_sin6;
4944 struct ip6_hdr *ip6;
4947 struct sockaddr *to;
4948 struct sctp_state_cookie stc;
4949 struct sctp_nets *net = NULL;
4950 uint8_t *signature = NULL;
4951 int cnt_inits_to = 0;
4952 uint16_t his_limit, i_want;
4953 int abort_flag, padval;
4956 int nat_friendly = 0;
4964 if ((asoc != NULL) &&
4965 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4966 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4967 /* new addresses, out of here in non-cookie-wait states */
4969 * Send a ABORT, we don't add the new address error clause
4970 * though we even set the T bit and copy in the 0 tag.. this
4971 * looks no different than if no listener was present.
4973 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4977 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4978 (offset + sizeof(struct sctp_init_chunk)),
4979 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4982 sctp_send_abort(init_pkt, iphlen, sh,
4983 init_chk->init.initiate_tag, op_err, vrf_id, port);
4986 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4988 /* No memory, INIT timer will re-attempt. */
4990 sctp_m_freem(op_err);
4993 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4995 /* the time I built cookie */
4996 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4998 /* populate any tie tags */
5000 /* unlock before tag selections */
5001 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5002 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5003 stc.cookie_life = asoc->cookie_life;
5004 net = asoc->primary_destination;
5006 stc.tie_tag_my_vtag = 0;
5007 stc.tie_tag_peer_vtag = 0;
5008 /* life I will award this cookie */
5009 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5012 /* copy in the ports for later check */
5013 stc.myport = sh->dest_port;
5014 stc.peerport = sh->src_port;
5017 * If we wanted to honor cookie life extentions, we would add to
5018 * stc.cookie_life. For now we should NOT honor any extension
5020 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5021 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5022 struct inpcb *in_inp;
5024 /* Its a V6 socket */
5025 in_inp = (struct inpcb *)inp;
5026 stc.ipv6_addr_legal = 1;
5027 /* Now look at the binding flag to see if V4 will be legal */
5028 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5029 stc.ipv4_addr_legal = 1;
5031 /* V4 addresses are NOT legal on the association */
5032 stc.ipv4_addr_legal = 0;
5035 /* Its a V4 socket, no - V6 */
5036 stc.ipv4_addr_legal = 1;
5037 stc.ipv6_addr_legal = 0;
5040 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5045 /* now for scope setup */
5046 memset((caddr_t)&store, 0, sizeof(store));
5047 memset((caddr_t)&store1, 0, sizeof(store1));
5049 to_sin = &store1.sin;
5052 to_sin6 = &store1.sin6;
5054 iph = mtod(init_pkt, struct ip *);
5055 /* establish the to_addr's */
5056 switch (iph->ip_v) {
5058 to_sin->sin_port = sh->dest_port;
5059 to_sin->sin_family = AF_INET;
5060 to_sin->sin_len = sizeof(struct sockaddr_in);
5061 to_sin->sin_addr = iph->ip_dst;
5064 case IPV6_VERSION >> 4:
5065 ip6 = mtod(init_pkt, struct ip6_hdr *);
5066 to_sin6->sin6_addr = ip6->ip6_dst;
5067 to_sin6->sin6_scope_id = 0;
5068 to_sin6->sin6_port = sh->dest_port;
5069 to_sin6->sin6_family = AF_INET6;
5070 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5079 to = (struct sockaddr *)&store;
5080 switch (iph->ip_v) {
5083 sin->sin_family = AF_INET;
5084 sin->sin_len = sizeof(struct sockaddr_in);
5085 sin->sin_port = sh->src_port;
5086 sin->sin_addr = iph->ip_src;
5087 /* lookup address */
5088 stc.address[0] = sin->sin_addr.s_addr;
5092 stc.addr_type = SCTP_IPV4_ADDRESS;
5093 /* local from address */
5094 stc.laddress[0] = to_sin->sin_addr.s_addr;
5095 stc.laddress[1] = 0;
5096 stc.laddress[2] = 0;
5097 stc.laddress[3] = 0;
5098 stc.laddr_type = SCTP_IPV4_ADDRESS;
5099 /* scope_id is only for v6 */
5101 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5102 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5107 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5108 /* Must use the address in this case */
5109 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5110 stc.loopback_scope = 1;
5113 stc.local_scope = 0;
5118 case IPV6_VERSION >> 4:
5120 ip6 = mtod(init_pkt, struct ip6_hdr *);
5121 sin6->sin6_family = AF_INET6;
5122 sin6->sin6_len = sizeof(struct sockaddr_in6);
5123 sin6->sin6_port = sh->src_port;
5124 sin6->sin6_addr = ip6->ip6_src;
5125 /* lookup address */
5126 memcpy(&stc.address, &sin6->sin6_addr,
5127 sizeof(struct in6_addr));
5128 sin6->sin6_scope_id = 0;
5129 stc.addr_type = SCTP_IPV6_ADDRESS;
5131 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5133 * FIX ME: does this have scope from
5136 (void)sa6_recoverscope(sin6);
5137 stc.scope_id = sin6->sin6_scope_id;
5138 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5139 stc.loopback_scope = 1;
5140 stc.local_scope = 0;
5143 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5145 * If the new destination is a
5146 * LINK_LOCAL we must have common
5147 * both site and local scope. Don't
5148 * set local scope though since we
5149 * must depend on the source to be
5150 * added implicitly. We cannot
5151 * assure just because we share one
5152 * link that all links are common.
5154 stc.local_scope = 0;
5158 * we start counting for the private
5159 * address stuff at 1. since the
5160 * link local we source from won't
5161 * show up in our scoped count.
5165 * pull out the scope_id from
5169 * FIX ME: does this have scope from
5172 (void)sa6_recoverscope(sin6);
5173 stc.scope_id = sin6->sin6_scope_id;
5174 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5175 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5177 * If the new destination is
5178 * SITE_LOCAL then we must have site
5183 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5184 stc.laddr_type = SCTP_IPV6_ADDRESS;
5194 /* set the scope per the existing tcb */
5197 struct sctp_nets *lnet;
5201 stc.loopback_scope = asoc->loopback_scope;
5202 stc.ipv4_scope = asoc->ipv4_local_scope;
5203 stc.site_scope = asoc->site_scope;
5204 stc.local_scope = asoc->local_scope;
5206 /* Why do we not consider IPv4 LL addresses? */
5207 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5208 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5209 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5211 * if we have a LL address, start
5219 /* use the net pointer */
5220 to = (struct sockaddr *)&net->ro._l_addr;
5221 switch (to->sa_family) {
5223 sin = (struct sockaddr_in *)to;
5224 stc.address[0] = sin->sin_addr.s_addr;
5228 stc.addr_type = SCTP_IPV4_ADDRESS;
5229 if (net->src_addr_selected == 0) {
5231 * strange case here, the INIT should have
5232 * did the selection.
5234 net->ro._s_addr = sctp_source_address_selection(inp,
5235 stcb, (sctp_route_t *) & net->ro,
5237 if (net->ro._s_addr == NULL)
5240 net->src_addr_selected = 1;
5243 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5244 stc.laddress[1] = 0;
5245 stc.laddress[2] = 0;
5246 stc.laddress[3] = 0;
5247 stc.laddr_type = SCTP_IPV4_ADDRESS;
5251 sin6 = (struct sockaddr_in6 *)to;
5252 memcpy(&stc.address, &sin6->sin6_addr,
5253 sizeof(struct in6_addr));
5254 stc.addr_type = SCTP_IPV6_ADDRESS;
5255 if (net->src_addr_selected == 0) {
5257 * strange case here, the INIT should have
5258 * did the selection.
5260 net->ro._s_addr = sctp_source_address_selection(inp,
5261 stcb, (sctp_route_t *) & net->ro,
5263 if (net->ro._s_addr == NULL)
5266 net->src_addr_selected = 1;
5268 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5269 sizeof(struct in6_addr));
5270 stc.laddr_type = SCTP_IPV6_ADDRESS;
5275 /* Now lets put the SCTP header in place */
5276 initack = mtod(m, struct sctp_init_ack_chunk *);
5277 /* Save it off for quick ref */
5278 stc.peers_vtag = init_chk->init.initiate_tag;
5280 memcpy(stc.identification, SCTP_VERSION_STRING,
5281 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5282 /* now the chunk header */
5283 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5284 initack->ch.chunk_flags = 0;
5285 /* fill in later from mbuf we build */
5286 initack->ch.chunk_length = 0;
5287 /* place in my tag */
5288 if ((asoc != NULL) &&
5289 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5290 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5291 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5292 /* re-use the v-tags and init-seq here */
5293 initack->init.initiate_tag = htonl(asoc->my_vtag);
5294 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5296 uint32_t vtag, itsn;
5298 if (hold_inp_lock) {
5299 SCTP_INP_INCR_REF(inp);
5300 SCTP_INP_RUNLOCK(inp);
5303 atomic_add_int(&asoc->refcnt, 1);
5304 SCTP_TCB_UNLOCK(stcb);
5306 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5307 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5309 * Got a duplicate vtag on some guy behind a
5310 * nat make sure we don't use it.
5314 initack->init.initiate_tag = htonl(vtag);
5315 /* get a TSN to use too */
5316 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5317 initack->init.initial_tsn = htonl(itsn);
5318 SCTP_TCB_LOCK(stcb);
5319 atomic_add_int(&asoc->refcnt, -1);
5321 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5322 initack->init.initiate_tag = htonl(vtag);
5323 /* get a TSN to use too */
5324 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5326 if (hold_inp_lock) {
5327 SCTP_INP_RLOCK(inp);
5328 SCTP_INP_DECR_REF(inp);
5331 /* save away my tag to */
5332 stc.my_vtag = initack->init.initiate_tag;
5334 /* set up some of the credits. */
5335 so = inp->sctp_socket;
5337 /* memory problem */
5341 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5343 /* set what I want */
5344 his_limit = ntohs(init_chk->init.num_inbound_streams);
5345 /* choose what I want */
5347 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5348 i_want = asoc->streamoutcnt;
5350 i_want = inp->sctp_ep.pre_open_stream_count;
5353 i_want = inp->sctp_ep.pre_open_stream_count;
5355 if (his_limit < i_want) {
5356 /* I Want more :< */
5357 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5359 /* I can have what I want :> */
5360 initack->init.num_outbound_streams = htons(i_want);
5362 /* tell him his limt. */
5363 initack->init.num_inbound_streams =
5364 htons(inp->sctp_ep.max_open_streams_intome);
5366 /* adaptation layer indication parameter */
5367 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5368 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5369 ali->ph.param_length = htons(sizeof(*ali));
5370 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5371 SCTP_BUF_LEN(m) += sizeof(*ali);
5372 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5375 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5376 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5377 ecn->ph.param_length = htons(sizeof(*ecn));
5378 SCTP_BUF_LEN(m) += sizeof(*ecn);
5380 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5383 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5385 /* And now tell the peer we do pr-sctp */
5386 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5387 prsctp->ph.param_length = htons(sizeof(*prsctp));
5388 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5390 /* Add NAT friendly parameter */
5391 struct sctp_paramhdr *ph;
5393 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5394 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5395 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5396 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5398 /* And now tell the peer we do all the extensions */
5399 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5400 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5402 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5403 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5404 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5405 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5406 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5407 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5408 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5410 * EY if the sysctl variable is set, tell the assoc. initiator that
5413 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5414 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5415 p_len = sizeof(*pr_supported) + num_ext;
5416 pr_supported->ph.param_length = htons(p_len);
5417 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5418 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5420 /* ECN nonce: And now tell the peer we support ECN nonce */
5421 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5422 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5423 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5424 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5425 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5426 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5428 /* add authentication parameters */
5429 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5430 struct sctp_auth_random *randp;
5431 struct sctp_auth_hmac_algo *hmacs;
5432 struct sctp_auth_chunk_list *chunks;
5433 uint16_t random_len;
5435 /* generate and add RANDOM parameter */
5436 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5437 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5438 randp->ph.param_type = htons(SCTP_RANDOM);
5439 p_len = sizeof(*randp) + random_len;
5440 randp->ph.param_length = htons(p_len);
5441 SCTP_READ_RANDOM(randp->random_data, random_len);
5442 /* zero out any padding required */
5443 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5444 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5446 /* add HMAC_ALGO parameter */
5447 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5448 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5449 (uint8_t *) hmacs->hmac_ids);
5451 p_len += sizeof(*hmacs);
5452 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5453 hmacs->ph.param_length = htons(p_len);
5454 /* zero out any padding required */
5455 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5456 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5458 /* add CHUNKS parameter */
5459 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5460 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5461 chunks->chunk_types);
5463 p_len += sizeof(*chunks);
5464 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5465 chunks->ph.param_length = htons(p_len);
5466 /* zero out any padding required */
5467 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5468 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5472 /* now the addresses */
5474 struct sctp_scoping scp;
5477 * To optimize this we could put the scoping stuff into a
5478 * structure and remove the individual uint8's from the stc
5479 * structure. Then we could just sifa in the address within
5480 * the stc.. but for now this is a quick hack to get the
5481 * address stuff teased apart.
5483 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5484 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5485 scp.loopback_scope = stc.loopback_scope;
5486 scp.ipv4_local_scope = stc.ipv4_scope;
5487 scp.local_scope = stc.local_scope;
5488 scp.site_scope = stc.site_scope;
5489 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5492 /* tack on the operational error if present */
5500 llen += SCTP_BUF_LEN(ol);
5501 ol = SCTP_BUF_NEXT(ol);
5504 /* must add a pad to the param */
5505 uint32_t cpthis = 0;
5508 padlen = 4 - (llen % 4);
5509 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5511 while (SCTP_BUF_NEXT(m_at) != NULL) {
5512 m_at = SCTP_BUF_NEXT(m_at);
5514 SCTP_BUF_NEXT(m_at) = op_err;
5515 while (SCTP_BUF_NEXT(m_at) != NULL) {
5516 m_at = SCTP_BUF_NEXT(m_at);
5519 /* pre-calulate the size and update pkt header and chunk header */
5521 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5522 p_len += SCTP_BUF_LEN(m_tmp);
5523 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5524 /* m_tmp should now point to last one */
5529 /* Now we must build a cookie */
5530 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5531 if (m_cookie == NULL) {
5532 /* memory problem */
5536 /* Now append the cookie to the end and update the space/size */
5537 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5539 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5540 p_len += SCTP_BUF_LEN(m_tmp);
5541 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5542 /* m_tmp should now point to last one */
5548 * Place in the size, but we don't include the last pad (if any) in
5551 initack->ch.chunk_length = htons(p_len);
5554 * Time to sign the cookie, we don't sign over the cookie signature
5555 * though thus we set trailer.
5557 (void)sctp_hmac_m(SCTP_HMAC,
5558 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5559 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5560 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5562 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5563 * here since the timer will drive a retranmission.
5566 if ((padval) && (mp_last)) {
5567 /* see my previous comments on mp_last */
5570 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5572 /* Houston we have a problem, no space */
5578 if (stc.loopback_scope) {
5579 over_addr = &store1;
5584 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5586 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5587 port, SCTP_SO_NOT_LOCKED, over_addr);
5588 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5593 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5594 struct sctp_association *asoc,
5595 struct sctp_stream_out *strq, int holds_lock)
5597 if (holds_lock == 0) {
5598 SCTP_TCB_SEND_LOCK(stcb);
5600 if ((strq->next_spoke.tqe_next == NULL) &&
5601 (strq->next_spoke.tqe_prev == NULL)) {
5602 TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5604 if (holds_lock == 0) {
5605 SCTP_TCB_SEND_UNLOCK(stcb);
5610 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5611 struct sctp_association *asoc,
5612 struct sctp_stream_out *strq,
5615 /* take off and then setup so we know it is not on the wheel */
5616 if (holds_lock == 0) {
5617 SCTP_TCB_SEND_LOCK(stcb);
5619 if (TAILQ_EMPTY(&strq->outqueue)) {
5620 if (asoc->last_out_stream == strq) {
5621 asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5622 if (asoc->last_out_stream == NULL) {
5623 asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5625 if (asoc->last_out_stream == strq) {
5626 asoc->last_out_stream = NULL;
5629 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5630 strq->next_spoke.tqe_next = NULL;
5631 strq->next_spoke.tqe_prev = NULL;
5633 if (holds_lock == 0) {
5634 SCTP_TCB_SEND_UNLOCK(stcb);
5639 sctp_prune_prsctp(struct sctp_tcb *stcb,
5640 struct sctp_association *asoc,
5641 struct sctp_sndrcvinfo *srcv,
5645 struct sctp_tmit_chunk *chk, *nchk;
5647 SCTP_TCB_LOCK_ASSERT(stcb);
5648 if ((asoc->peer_supports_prsctp) &&
5649 (asoc->sent_queue_cnt_removeable > 0)) {
5650 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5652 * Look for chunks marked with the PR_SCTP flag AND
5653 * the buffer space flag. If the one being sent is
5654 * equal or greater priority then purge the old one
5655 * and free some space.
5657 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5659 * This one is PR-SCTP AND buffer space
5662 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5664 * Lower numbers equates to higher
5665 * priority so if the one we are
5666 * looking at has a larger or equal
5667 * priority we want to drop the data
5668 * and NOT retransmit it.
5672 * We release the book_size
5673 * if the mbuf is here
5678 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5679 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5681 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5682 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5685 freed_spc += ret_spc;
5686 if (freed_spc >= dataout) {
5689 } /* if chunk was present */
5690 } /* if of sufficent priority */
5691 } /* if chunk has enabled */
5692 } /* tailqforeach */
5694 chk = TAILQ_FIRST(&asoc->send_queue);
5696 nchk = TAILQ_NEXT(chk, sctp_next);
5697 /* Here we must move to the sent queue and mark */
5698 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5699 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5702 * We release the book_size
5703 * if the mbuf is here
5707 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5708 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5711 freed_spc += ret_spc;
5712 if (freed_spc >= dataout) {
5715 } /* end if chk->data */
5716 } /* end if right class */
5717 } /* end if chk pr-sctp */
5719 } /* end while (chk) */
5720 } /* if enabled in asoc */
5724 sctp_get_frag_point(struct sctp_tcb *stcb,
5725 struct sctp_association *asoc)
5730 * For endpoints that have both v6 and v4 addresses we must reserve
5731 * room for the ipv6 header, for those that are only dealing with V4
5732 * we use a larger frag point.
5734 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5735 ovh = SCTP_MED_OVERHEAD;
5737 ovh = SCTP_MED_V4_OVERHEAD;
5740 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5741 siz = asoc->smallest_mtu - ovh;
5743 siz = (stcb->asoc.sctp_frag_point - ovh);
5745 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5747 /* A data chunk MUST fit in a cluster */
5748 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5751 /* adjust for an AUTH chunk if DATA requires auth */
5752 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5753 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5756 /* make it an even word boundary please */
5763 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5767 * We assume that the user wants PR_SCTP_TTL if the user provides a
5768 * positive lifetime but does not specify any PR_SCTP policy. This
5769 * is a BAD assumption and causes problems at least with the
5770 * U-Vancovers MPI folks. I will change this to be no policy means
5773 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5774 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5779 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5780 case CHUNK_FLAGS_PR_SCTP_BUF:
5782 * Time to live is a priority stored in tv_sec when doing
5783 * the buffer drop thing.
5785 sp->ts.tv_sec = sp->timetolive;
5788 case CHUNK_FLAGS_PR_SCTP_TTL:
5792 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5793 tv.tv_sec = sp->timetolive / 1000;
5794 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5796 * TODO sctp_constants.h needs alternative time
5797 * macros when _KERNEL is undefined.
5799 timevaladd(&sp->ts, &tv);
5802 case CHUNK_FLAGS_PR_SCTP_RTX:
5804 * Time to live is a the number or retransmissions stored in
5807 sp->ts.tv_sec = sp->timetolive;
5811 SCTPDBG(SCTP_DEBUG_USRREQ1,
5812 "Unknown PR_SCTP policy %u.\n",
5813 PR_SCTP_POLICY(sp->sinfo_flags));
5819 sctp_msg_append(struct sctp_tcb *stcb,
5820 struct sctp_nets *net,
5822 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5824 int error = 0, holds_lock;
5826 struct sctp_stream_queue_pending *sp = NULL;
5827 struct sctp_stream_out *strm;
5830 * Given an mbuf chain, put it into the association send queue and
5831 * place it on the wheel
5833 holds_lock = hold_stcb_lock;
5834 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5835 /* Invalid stream number */
5836 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5840 if ((stcb->asoc.stream_locked) &&
5841 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5842 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5846 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5847 /* Now can we send this? */
5848 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5849 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5850 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5851 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5852 /* got data while shutting down */
5853 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5857 sctp_alloc_a_strmoq(stcb, sp);
5859 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5863 sp->sinfo_flags = srcv->sinfo_flags;
5864 sp->timetolive = srcv->sinfo_timetolive;
5865 sp->ppid = srcv->sinfo_ppid;
5866 sp->context = srcv->sinfo_context;
5868 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5871 sp->net = stcb->asoc.primary_destination;
5873 atomic_add_int(&sp->net->ref_count, 1);
5874 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5875 sp->stream = srcv->sinfo_stream;
5876 sp->msg_is_complete = 1;
5877 sp->sender_all_done = 1;
5880 sp->tail_mbuf = NULL;
5883 sctp_set_prsctp_policy(sp);
5885 * We could in theory (for sendall) sifa the length in, but we would
5886 * still have to hunt through the chain since we need to setup the
5890 if (SCTP_BUF_NEXT(at) == NULL)
5892 sp->length += SCTP_BUF_LEN(at);
5893 at = SCTP_BUF_NEXT(at);
5895 SCTP_TCB_SEND_LOCK(stcb);
5896 sctp_snd_sb_alloc(stcb, sp->length);
5897 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5898 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5899 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5900 sp->strseq = strm->next_sequence_sent;
5901 strm->next_sequence_sent++;
5903 if ((strm->next_spoke.tqe_next == NULL) &&
5904 (strm->next_spoke.tqe_prev == NULL)) {
5905 /* Not on wheel, insert */
5906 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5909 SCTP_TCB_SEND_UNLOCK(stcb);
5918 static struct mbuf *
5919 sctp_copy_mbufchain(struct mbuf *clonechain,
5920 struct mbuf *outchain,
5921 struct mbuf **endofchain,
5924 uint8_t copy_by_ref)
5927 struct mbuf *appendchain;
5931 if (endofchain == NULL) {
5935 sctp_m_freem(outchain);
5938 if (can_take_mbuf) {
5939 appendchain = clonechain;
5942 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5944 /* Its not in a cluster */
5945 if (*endofchain == NULL) {
5946 /* lets get a mbuf cluster */
5947 if (outchain == NULL) {
5948 /* This is the general case */
5950 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5951 if (outchain == NULL) {
5954 SCTP_BUF_LEN(outchain) = 0;
5955 *endofchain = outchain;
5956 /* get the prepend space */
5957 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5960 * We really should not get a NULL
5966 if (SCTP_BUF_NEXT(m) == NULL) {
5970 m = SCTP_BUF_NEXT(m);
5973 if (*endofchain == NULL) {
5975 * huh, TSNH XXX maybe we
5978 sctp_m_freem(outchain);
5982 /* get the new end of length */
5983 len = M_TRAILINGSPACE(*endofchain);
5985 /* how much is left at the end? */
5986 len = M_TRAILINGSPACE(*endofchain);
5988 /* Find the end of the data, for appending */
5989 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5991 /* Now lets copy it out */
5992 if (len >= sizeofcpy) {
5993 /* It all fits, copy it in */
5994 m_copydata(clonechain, 0, sizeofcpy, cp);
5995 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5997 /* fill up the end of the chain */
5999 m_copydata(clonechain, 0, len, cp);
6000 SCTP_BUF_LEN((*endofchain)) += len;
6001 /* now we need another one */
6004 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6009 SCTP_BUF_NEXT((*endofchain)) = m;
6011 cp = mtod((*endofchain), caddr_t);
6012 m_copydata(clonechain, len, sizeofcpy, cp);
6013 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6017 /* copy the old fashion way */
6018 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6019 #ifdef SCTP_MBUF_LOGGING
6020 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6025 if (SCTP_BUF_IS_EXTENDED(mat)) {
6026 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6028 mat = SCTP_BUF_NEXT(mat);
6034 if (appendchain == NULL) {
6037 sctp_m_freem(outchain);
6041 /* tack on to the end */
6042 if (*endofchain != NULL) {
6043 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6047 if (SCTP_BUF_NEXT(m) == NULL) {
6048 SCTP_BUF_NEXT(m) = appendchain;
6051 m = SCTP_BUF_NEXT(m);
6055 * save off the end and update the end-chain postion
6059 if (SCTP_BUF_NEXT(m) == NULL) {
6063 m = SCTP_BUF_NEXT(m);
6067 /* save off the end and update the end-chain postion */
6070 if (SCTP_BUF_NEXT(m) == NULL) {
6074 m = SCTP_BUF_NEXT(m);
6076 return (appendchain);
6081 sctp_med_chunk_output(struct sctp_inpcb *inp,
6082 struct sctp_tcb *stcb,
6083 struct sctp_association *asoc,
6086 int control_only, int from_where,
6087 struct timeval *now, int *now_filled, int frag_point, int so_locked
6088 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6094 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6097 struct sctp_copy_all *ca;
6100 int added_control = 0;
6101 int un_sent, do_chunk_output = 1;
6102 struct sctp_association *asoc;
6104 ca = (struct sctp_copy_all *)ptr;
6105 if (ca->m == NULL) {
6108 if (ca->inp != inp) {
6112 if ((ca->m) && ca->sndlen) {
6113 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6115 /* can't copy so we are done */
6119 #ifdef SCTP_MBUF_LOGGING
6120 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6125 if (SCTP_BUF_IS_EXTENDED(mat)) {
6126 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6128 mat = SCTP_BUF_NEXT(mat);
6135 SCTP_TCB_LOCK_ASSERT(stcb);
6136 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6137 /* Abort this assoc with m as the user defined reason */
6139 struct sctp_paramhdr *ph;
6141 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6143 ph = mtod(m, struct sctp_paramhdr *);
6144 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6145 ph->param_length = htons(ca->sndlen);
6148 * We add one here to keep the assoc from
6149 * dis-appearing on us.
6151 atomic_add_int(&stcb->asoc.refcnt, 1);
6152 sctp_abort_an_association(inp, stcb,
6153 SCTP_RESPONSE_TO_USER_REQ,
6154 m, SCTP_SO_NOT_LOCKED);
6156 * sctp_abort_an_association calls sctp_free_asoc()
6157 * free association will NOT free it since we
6158 * incremented the refcnt .. we do this to prevent
6159 * it being freed and things getting tricky since we
6160 * could end up (from free_asoc) calling inpcb_free
6161 * which would get a recursive lock call to the
6162 * iterator lock.. But as a consequence of that the
6163 * stcb will return to us un-locked.. since
6164 * free_asoc returns with either no TCB or the TCB
6165 * unlocked, we must relock.. to unlock in the
6166 * iterator timer :-0
6168 SCTP_TCB_LOCK(stcb);
6169 atomic_add_int(&stcb->asoc.refcnt, -1);
6170 goto no_chunk_output;
6174 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6178 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6179 /* shutdown this assoc */
6182 cnt = sctp_is_there_unsent_data(stcb);
6184 if (TAILQ_EMPTY(&asoc->send_queue) &&
6185 TAILQ_EMPTY(&asoc->sent_queue) &&
6187 if (asoc->locked_on_sending) {
6191 * there is nothing queued to send, so I'm
6194 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6195 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6196 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6198 * only send SHUTDOWN the first time
6201 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6202 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6203 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6205 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6206 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6207 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6208 asoc->primary_destination);
6209 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6210 asoc->primary_destination);
6212 do_chunk_output = 0;
6216 * we still got (or just got) data to send,
6217 * so set SHUTDOWN_PENDING
6220 * XXX sockets draft says that SCTP_EOF
6221 * should be sent with no data. currently,
6222 * we will allow user data to be sent first
6223 * and move to SHUTDOWN-PENDING
6225 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6226 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6227 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6228 if (asoc->locked_on_sending) {
6230 * Locked to send out the
6233 struct sctp_stream_queue_pending *sp;
6235 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6237 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6238 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6241 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6242 if (TAILQ_EMPTY(&asoc->send_queue) &&
6243 TAILQ_EMPTY(&asoc->sent_queue) &&
6244 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6246 atomic_add_int(&stcb->asoc.refcnt, 1);
6247 sctp_abort_an_association(stcb->sctp_ep, stcb,
6248 SCTP_RESPONSE_TO_USER_REQ,
6249 NULL, SCTP_SO_NOT_LOCKED);
6250 atomic_add_int(&stcb->asoc.refcnt, -1);
6251 goto no_chunk_output;
6253 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6254 asoc->primary_destination);
6260 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6261 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6263 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6264 (stcb->asoc.total_flight > 0) &&
6265 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6267 do_chunk_output = 0;
6269 if (do_chunk_output)
6270 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6271 else if (added_control) {
6272 int num_out = 0, reason = 0, now_filled = 0;
6276 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6277 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6278 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6289 sctp_sendall_completes(void *ptr, uint32_t val)
6291 struct sctp_copy_all *ca;
6293 ca = (struct sctp_copy_all *)ptr;
6295 * Do a notify here? Kacheong suggests that the notify be done at
6296 * the send time.. so you would push up a notification if any send
6297 * failed. Don't know if this is feasable since the only failures we
6298 * have is "memory" related and if you cannot get an mbuf to send
6299 * the data you surely can't get an mbuf to send up to notify the
6300 * user you can't send the data :->
6303 /* now free everything */
6304 sctp_m_freem(ca->m);
6305 SCTP_FREE(ca, SCTP_M_COPYAL);
6309 #define MC_ALIGN(m, len) do { \
6310 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6315 static struct mbuf *
6316 sctp_copy_out_all(struct uio *uio, int len)
6318 struct mbuf *ret, *at;
6319 int left, willcpy, cancpy, error;
6321 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6327 SCTP_BUF_LEN(ret) = 0;
6328 /* save space for the data chunk header */
6329 cancpy = M_TRAILINGSPACE(ret);
6330 willcpy = min(cancpy, left);
6333 /* Align data to the end */
6334 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6340 SCTP_BUF_LEN(at) = willcpy;
6341 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6344 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6345 if (SCTP_BUF_NEXT(at) == NULL) {
6348 at = SCTP_BUF_NEXT(at);
6349 SCTP_BUF_LEN(at) = 0;
6350 cancpy = M_TRAILINGSPACE(at);
6351 willcpy = min(cancpy, left);
6358 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6359 struct sctp_sndrcvinfo *srcv)
6362 struct sctp_copy_all *ca;
6364 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6371 memset(ca, 0, sizeof(struct sctp_copy_all));
6374 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6376 * take off the sendall flag, it would be bad if we failed to do
6379 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6380 /* get length and mbuf chain */
6382 ca->sndlen = uio->uio_resid;
6383 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6384 if (ca->m == NULL) {
6385 SCTP_FREE(ca, SCTP_M_COPYAL);
6386 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6390 /* Gather the length of the send */
6396 ca->sndlen += SCTP_BUF_LEN(m);
6397 m = SCTP_BUF_NEXT(m);
6401 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6402 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6403 SCTP_ASOC_ANY_STATE,
6405 sctp_sendall_completes, inp, 1);
6407 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6408 SCTP_FREE(ca, SCTP_M_COPYAL);
6409 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6417 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6419 struct sctp_tmit_chunk *chk, *nchk;
6421 chk = TAILQ_FIRST(&asoc->control_send_queue);
6423 nchk = TAILQ_NEXT(chk, sctp_next);
6424 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6425 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6427 sctp_m_freem(chk->data);
6430 asoc->ctrl_queue_cnt--;
6431 sctp_free_a_chunk(stcb, chk);
6438 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6440 struct sctp_association *asoc;
6441 struct sctp_tmit_chunk *chk, *chk_tmp;
6442 struct sctp_asconf_chunk *acp;
6445 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6448 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6449 /* find SCTP_ASCONF chunk in queue */
6450 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6452 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6453 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6458 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6460 sctp_m_freem(chk->data);
6463 asoc->ctrl_queue_cnt--;
6464 sctp_free_a_chunk(stcb, chk);
6471 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6473 struct sctp_association *asoc,
6474 struct sctp_tmit_chunk **data_list,
6476 struct sctp_nets *net)
6479 struct sctp_tmit_chunk *tp1;
6481 for (i = 0; i < bundle_at; i++) {
6482 /* off of the send queue */
6485 * Any chunk NOT 0 you zap the time chunk 0 gets
6486 * zapped or set based on if a RTO measurment is
6489 data_list[i]->do_rtt = 0;
6492 data_list[i]->sent_rcv_time = net->last_sent_time;
6493 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6494 TAILQ_REMOVE(&asoc->send_queue,
6497 /* on to the sent queue */
6498 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6499 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6500 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6501 struct sctp_tmit_chunk *tpp;
6503 /* need to move back */
6505 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6507 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6511 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6512 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6515 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6517 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6522 /* This does not lower until the cum-ack passes it */
6523 asoc->sent_queue_cnt++;
6524 asoc->send_queue_cnt--;
6525 if ((asoc->peers_rwnd <= 0) &&
6526 (asoc->total_flight == 0) &&
6528 /* Mark the chunk as being a window probe */
6529 SCTP_STAT_INCR(sctps_windowprobed);
6531 #ifdef SCTP_AUDITING_ENABLED
6532 sctp_audit_log(0xC2, 3);
6534 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6535 data_list[i]->snd_count = 1;
6536 data_list[i]->rec.data.chunk_was_revoked = 0;
6537 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6538 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6539 data_list[i]->whoTo->flight_size,
6540 data_list[i]->book_size,
6541 (uintptr_t) data_list[i]->whoTo,
6542 data_list[i]->rec.data.TSN_seq);
6544 sctp_flight_size_increase(data_list[i]);
6545 sctp_total_flight_increase(stcb, data_list[i]);
6546 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6547 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6548 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6550 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6551 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6552 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6553 /* SWS sender side engages */
6554 asoc->peers_rwnd = 0;
6560 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6562 struct sctp_tmit_chunk *chk, *nchk;
6564 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6566 nchk = TAILQ_NEXT(chk, sctp_next);
6567 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6568 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
6569 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6570 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6571 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6572 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6573 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6574 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6575 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6576 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6577 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6578 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6579 /* Stray chunks must be cleaned up */
6581 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6583 sctp_m_freem(chk->data);
6586 asoc->ctrl_queue_cnt--;
6587 sctp_free_a_chunk(stcb, chk);
6588 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6589 /* special handling, we must look into the param */
6590 if (chk != asoc->str_reset) {
6591 goto clean_up_anyway;
6599 sctp_can_we_split_this(struct sctp_tcb *stcb,
6601 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6604 * Make a decision on if I should split a msg into multiple parts.
6605 * This is only asked of incomplete messages.
6609 * If we are doing EEOR we need to always send it if its the
6610 * entire thing, since it might be all the guy is putting in
6613 if (goal_mtu >= length) {
6615 * If we have data outstanding,
6616 * we get another chance when the sack
6617 * arrives to transmit - wait for more data
6619 if (stcb->asoc.total_flight == 0) {
6621 * If nothing is in flight, we zero the
6629 /* You can fill the rest */
6634 * For those strange folk that make the send buffer
6635 * smaller than our fragmentation point, we can't
6636 * get a full msg in so we have to allow splitting.
6638 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6641 if ((length <= goal_mtu) ||
6642 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6643 /* Sub-optimial residual don't split in non-eeor mode. */
6647 * If we reach here length is larger than the goal_mtu. Do we wish
6648 * to split it for the sake of packet putting together?
6650 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6651 /* Its ok to split it */
6652 return (min(goal_mtu, frag_point));
6654 /* Nope, can't split */
6660 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6661 struct sctp_stream_out *strq,
6663 uint32_t frag_point,
6669 /* Move from the stream to the send_queue keeping track of the total */
6670 struct sctp_association *asoc;
6671 struct sctp_stream_queue_pending *sp;
6672 struct sctp_tmit_chunk *chk;
6673 struct sctp_data_chunk *dchkh;
6674 uint32_t to_move, length;
6675 uint8_t rcv_flags = 0;
6677 uint8_t send_lock_up = 0;
6679 SCTP_TCB_LOCK_ASSERT(stcb);
6682 /* sa_ignore FREED_MEMORY */
6683 sp = TAILQ_FIRST(&strq->outqueue);
6686 if (send_lock_up == 0) {
6687 SCTP_TCB_SEND_LOCK(stcb);
6690 sp = TAILQ_FIRST(&strq->outqueue);
6694 if (strq->last_msg_incomplete) {
6695 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6697 strq->last_msg_incomplete);
6698 strq->last_msg_incomplete = 0;
6702 SCTP_TCB_SEND_UNLOCK(stcb);
6707 if ((sp->msg_is_complete) && (sp->length == 0)) {
6708 if (sp->sender_all_done) {
6710 * We are doing differed cleanup. Last time through
6711 * when we took all the data the sender_all_done was
6714 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6715 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6716 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6717 sp->sender_all_done,
6719 sp->msg_is_complete,
6723 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6724 SCTP_TCB_SEND_LOCK(stcb);
6727 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6728 TAILQ_REMOVE(&strq->outqueue, sp, next);
6729 sctp_free_remote_addr(sp->net);
6731 sctp_m_freem(sp->data);
6734 sctp_free_a_strmoq(stcb, sp);
6735 /* we can't be locked to it */
6737 stcb->asoc.locked_on_sending = NULL;
6739 SCTP_TCB_SEND_UNLOCK(stcb);
6742 /* back to get the next msg */
6746 * sender just finished this but still holds a
6755 /* is there some to get */
6756 if (sp->length == 0) {
6762 } else if (sp->discard_rest) {
6763 if (send_lock_up == 0) {
6764 SCTP_TCB_SEND_LOCK(stcb);
6767 /* Whack down the size */
6768 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6769 if ((stcb->sctp_socket != NULL) && \
6770 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6771 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6772 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6775 sctp_m_freem(sp->data);
6777 sp->tail_mbuf = NULL;
6787 some_taken = sp->some_taken;
6788 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6789 sp->msg_is_complete = 1;
6792 length = sp->length;
6793 if (sp->msg_is_complete) {
6794 /* The message is complete */
6795 to_move = min(length, frag_point);
6796 if (to_move == length) {
6797 /* All of it fits in the MTU */
6798 if (sp->some_taken) {
6799 rcv_flags |= SCTP_DATA_LAST_FRAG;
6800 sp->put_last_out = 1;
6802 rcv_flags |= SCTP_DATA_NOT_FRAG;
6803 sp->put_last_out = 1;
6806 /* Not all of it fits, we fragment */
6807 if (sp->some_taken == 0) {
6808 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6813 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6816 * We use a snapshot of length in case it
6817 * is expanding during the compare.
6822 if (to_move >= llen) {
6824 if (send_lock_up == 0) {
6826 * We are taking all of an incomplete msg
6827 * thus we need a send lock.
6829 SCTP_TCB_SEND_LOCK(stcb);
6831 if (sp->msg_is_complete) {
6833 * the sender finished the
6840 if (sp->some_taken == 0) {
6841 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6845 /* Nothing to take. */
6846 if (sp->some_taken) {
6855 /* If we reach here, we can copy out a chunk */
6856 sctp_alloc_a_chunk(stcb, chk);
6858 /* No chunk memory */
6864 * Setup for unordered if needed by looking at the user sent info
6867 if (sp->sinfo_flags & SCTP_UNORDERED) {
6868 rcv_flags |= SCTP_DATA_UNORDERED;
6870 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6871 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6872 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6874 /* clear out the chunk before setting up */
6875 memset(chk, 0, sizeof(*chk));
6876 chk->rec.data.rcv_flags = rcv_flags;
6878 if (to_move >= length) {
6879 /* we think we can steal the whole thing */
6880 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6881 SCTP_TCB_SEND_LOCK(stcb);
6884 if (to_move < sp->length) {
6885 /* bail, it changed */
6888 chk->data = sp->data;
6889 chk->last_mbuf = sp->tail_mbuf;
6890 /* register the stealing */
6891 sp->data = sp->tail_mbuf = NULL;
6896 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6897 chk->last_mbuf = NULL;
6898 if (chk->data == NULL) {
6899 sp->some_taken = some_taken;
6900 sctp_free_a_chunk(stcb, chk);
6905 #ifdef SCTP_MBUF_LOGGING
6906 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6911 if (SCTP_BUF_IS_EXTENDED(mat)) {
6912 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6914 mat = SCTP_BUF_NEXT(mat);
6918 /* Pull off the data */
6919 m_adj(sp->data, to_move);
6920 /* Now lets work our way down and compact it */
6922 while (m && (SCTP_BUF_LEN(m) == 0)) {
6923 sp->data = SCTP_BUF_NEXT(m);
6924 SCTP_BUF_NEXT(m) = NULL;
6925 if (sp->tail_mbuf == m) {
6927 * Freeing tail? TSNH since
6928 * we supposedly were taking less
6929 * than the sp->length.
6932 panic("Huh, freing tail? - TSNH");
6934 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6935 sp->tail_mbuf = sp->data = NULL;
6944 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6945 chk->copy_by_ref = 1;
6947 chk->copy_by_ref = 0;
6950 * get last_mbuf and counts of mb useage This is ugly but hopefully
6951 * its only one mbuf.
6953 if (chk->last_mbuf == NULL) {
6954 chk->last_mbuf = chk->data;
6955 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6956 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6959 if (to_move > length) {
6960 /*- This should not happen either
6961 * since we always lower to_move to the size
6962 * of sp->length if its larger.
6965 panic("Huh, how can to_move be larger?");
6967 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6971 atomic_subtract_int(&sp->length, to_move);
6973 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6974 /* Not enough room for a chunk header, get some */
6977 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6980 * we're in trouble here. _PREPEND below will free
6981 * all the data if there is no leading space, so we
6982 * must put the data back and restore.
6984 if (send_lock_up == 0) {
6985 SCTP_TCB_SEND_LOCK(stcb);
6988 if (chk->data == NULL) {
6989 /* unsteal the data */
6990 sp->data = chk->data;
6991 sp->tail_mbuf = chk->last_mbuf;
6995 /* reassemble the data */
6997 sp->data = chk->data;
6998 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7000 sp->some_taken = some_taken;
7001 atomic_add_int(&sp->length, to_move);
7004 sctp_free_a_chunk(stcb, chk);
7008 SCTP_BUF_LEN(m) = 0;
7009 SCTP_BUF_NEXT(m) = chk->data;
7011 M_ALIGN(chk->data, 4);
7014 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7015 if (chk->data == NULL) {
7016 /* HELP, TSNH since we assured it would not above? */
7018 panic("prepend failes HELP?");
7020 SCTP_PRINTF("prepend fails HELP?\n");
7021 sctp_free_a_chunk(stcb, chk);
7027 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7028 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7029 chk->book_size_scale = 0;
7030 chk->sent = SCTP_DATAGRAM_UNSENT;
7033 chk->asoc = &stcb->asoc;
7034 chk->pad_inplace = 0;
7035 chk->no_fr_allowed = 0;
7036 chk->rec.data.stream_seq = sp->strseq;
7037 chk->rec.data.stream_number = sp->stream;
7038 chk->rec.data.payloadtype = sp->ppid;
7039 chk->rec.data.context = sp->context;
7040 chk->rec.data.doing_fast_retransmit = 0;
7041 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
7043 chk->rec.data.timetodrop = sp->ts;
7044 chk->flags = sp->act_flags;
7047 atomic_add_int(&chk->whoTo->ref_count, 1);
7049 if (sp->holds_key_ref) {
7050 chk->auth_keyid = sp->auth_keyid;
7051 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7052 chk->holds_key_ref = 1;
7054 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7055 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7056 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7057 (uintptr_t) stcb, sp->length,
7058 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7059 chk->rec.data.TSN_seq);
7061 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7063 * Put the rest of the things in place now. Size was done earlier in
7064 * previous loop prior to padding.
7067 #ifdef SCTP_ASOCLOG_OF_TSNS
7068 SCTP_TCB_LOCK_ASSERT(stcb);
7069 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7070 asoc->tsn_out_at = 0;
7071 asoc->tsn_out_wrapped = 1;
7073 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7074 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7075 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7076 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7077 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7078 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7079 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7080 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7084 dchkh->ch.chunk_type = SCTP_DATA;
7085 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7086 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7087 dchkh->dp.stream_id = htons(strq->stream_no);
7088 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7089 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7090 dchkh->ch.chunk_length = htons(chk->send_size);
7091 /* Now advance the chk->send_size by the actual pad needed. */
7092 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7097 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7098 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7099 chk->pad_inplace = 1;
7101 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7102 /* pad added an mbuf */
7103 chk->last_mbuf = lm;
7105 chk->send_size += pads;
7107 /* We only re-set the policy if it is on */
7108 if (sp->pr_sctp_on) {
7109 sctp_set_prsctp_policy(sp);
7110 asoc->pr_sctp_cnt++;
7111 chk->pr_sctp_on = 1;
7113 chk->pr_sctp_on = 0;
7115 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7116 /* All done pull and kill the message */
7117 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7118 if (sp->put_last_out == 0) {
7119 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7120 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7121 sp->sender_all_done,
7123 sp->msg_is_complete,
7127 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7128 SCTP_TCB_SEND_LOCK(stcb);
7131 TAILQ_REMOVE(&strq->outqueue, sp, next);
7132 sctp_free_remote_addr(sp->net);
7134 sctp_m_freem(sp->data);
7137 sctp_free_a_strmoq(stcb, sp);
7139 /* we can't be locked to it */
7141 stcb->asoc.locked_on_sending = NULL;
7143 /* more to go, we are locked */
7146 asoc->chunks_on_out_queue++;
7147 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7148 asoc->send_queue_cnt++;
7151 SCTP_TCB_SEND_UNLOCK(stcb);
7158 static struct sctp_stream_out *
7159 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7161 struct sctp_stream_out *strq;
7163 /* Find the next stream to use */
7164 if (asoc->last_out_stream == NULL) {
7165 strq = TAILQ_FIRST(&asoc->out_wheel);
7167 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7169 strq = TAILQ_FIRST(&asoc->out_wheel);
7177 sctp_fill_outqueue(struct sctp_tcb *stcb,
7178 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7180 struct sctp_association *asoc;
7181 struct sctp_stream_out *strq, *strqn;
7182 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7184 struct sctp_stream_queue_pending *sp;
7186 SCTP_TCB_LOCK_ASSERT(stcb);
7189 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7190 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7192 /* ?? not sure what else to do */
7193 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7196 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7198 /* Need an allowance for the data chunk header too */
7199 goal_mtu -= sizeof(struct sctp_data_chunk);
7201 /* must make even word boundary */
7202 goal_mtu &= 0xfffffffc;
7203 if (asoc->locked_on_sending) {
7204 /* We are stuck on one stream until the message completes. */
7205 strqn = strq = asoc->locked_on_sending;
7208 strqn = strq = sctp_select_a_stream(stcb, asoc);
7212 while ((goal_mtu > 0) && strq) {
7213 sp = TAILQ_FIRST(&strq->outqueue);
7215 * If CMT is off, we must validate that the stream in
7216 * question has the first item pointed towards are network
7217 * destionation requested by the caller. Note that if we
7218 * turn out to be locked to a stream (assigning TSN's then
7219 * we must stop, since we cannot look for another stream
7220 * with data to send to that destination). In CMT's case, by
7221 * skipping this check, we will send one data packet towards
7222 * the requested net.
7227 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7228 /* none for this network */
7232 strq = sctp_select_a_stream(stcb, asoc);
7236 if (strqn == strq) {
7237 /* I have circled */
7245 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7246 &giveup, eeor_mode, &bail);
7248 asoc->last_out_stream = strq;
7251 asoc->locked_on_sending = strq;
7252 if ((moved_how_much == 0) || (giveup) || bail)
7253 /* no more to move for now */
7256 asoc->locked_on_sending = NULL;
7257 if (TAILQ_EMPTY(&strq->outqueue)) {
7258 if (strq == strqn) {
7259 /* Must move start to next one */
7260 strqn = TAILQ_NEXT(strq, next_spoke);
7261 if (strqn == NULL) {
7262 strqn = TAILQ_FIRST(&asoc->out_wheel);
7263 if (strqn == NULL) {
7268 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7270 if ((giveup) || bail) {
7273 strq = sctp_select_a_stream(stcb, asoc);
7278 total_moved += moved_how_much;
7279 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7280 goal_mtu &= 0xfffffffc;
7285 if (total_moved == 0) {
7286 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7287 (net == stcb->asoc.primary_destination)) {
7288 /* ran dry for primary network net */
7289 SCTP_STAT_INCR(sctps_primary_randry);
7290 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7291 /* ran dry with CMT on */
7292 SCTP_STAT_INCR(sctps_cmt_randry);
7298 sctp_fix_ecn_echo(struct sctp_association *asoc)
7300 struct sctp_tmit_chunk *chk;
7302 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7303 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7304 chk->sent = SCTP_DATAGRAM_UNSENT;
7310 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7311 struct sctp_association *asoc,
7312 struct sctp_nets *net)
7314 struct sctp_tmit_chunk *chk;
7315 struct sctp_nets *a_net;
7317 SCTP_TCB_LOCK_ASSERT(stcb);
7319 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7320 * destination using the PF algorithm for finding alternate
7323 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7324 a_net = sctp_find_alternate_net(stcb, net, 2);
7326 a_net = sctp_find_alternate_net(stcb, net, 0);
7328 if ((a_net != net) &&
7329 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7331 * We only proceed if a valid alternate is found that is not
7332 * this one and is reachable. Here we must move all chunks
7333 * queued in the send queue off of the destination address
7336 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7337 if (chk->whoTo == net) {
7338 /* Move the chunk to our alternate */
7339 sctp_free_remote_addr(chk->whoTo);
7341 atomic_add_int(&a_net->ref_count, 1);
7348 sctp_med_chunk_output(struct sctp_inpcb *inp,
7349 struct sctp_tcb *stcb,
7350 struct sctp_association *asoc,
7353 int control_only, int from_where,
7354 struct timeval *now, int *now_filled, int frag_point, int so_locked
7355 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7361 * Ok this is the generic chunk service queue. we must do the
7362 * following: - Service the stream queue that is next, moving any
7363 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7364 * LAST to the out queue in one pass) and assigning TSN's - Check to
7365 * see if the cwnd/rwnd allows any output, if so we go ahead and
7366 * fomulate and send the low level chunks. Making sure to combine
7367 * any control in the control chunk queue also.
7369 struct sctp_nets *net, *start_at, *old_start_at = NULL;
7370 struct mbuf *outchain, *endoutchain;
7371 struct sctp_tmit_chunk *chk, *nchk;
7373 /* temp arrays for unlinking */
7374 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7375 int no_fragmentflg, error;
7376 unsigned int max_rwnd_per_dest;
7377 int one_chunk, hbflag, skip_data_for_this_net;
7378 int asconf, cookie, no_out_cnt;
7379 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7380 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7382 uint32_t auth_offset = 0;
7383 struct sctp_auth_chunk *auth = NULL;
7384 uint16_t auth_keyid;
7385 int override_ok = 1;
7386 int data_auth_reqd = 0;
7389 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7396 auth_keyid = stcb->asoc.authinfo.active_keyid;
7398 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7399 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7400 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7405 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7407 * First lets prime the pump. For each destination, if there is room
7408 * in the flight size, attempt to pull an MTU's worth out of the
7409 * stream queues into the general send_queue
7411 #ifdef SCTP_AUDITING_ENABLED
7412 sctp_audit_log(0xC2, 2);
7414 SCTP_TCB_LOCK_ASSERT(stcb);
7416 if ((control_only) || (asoc->stream_reset_outstanding))
7421 /* Nothing to possible to send? */
7422 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7423 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7424 TAILQ_EMPTY(&asoc->send_queue) &&
7425 TAILQ_EMPTY(&asoc->out_wheel)) {
7429 if (asoc->peers_rwnd == 0) {
7430 /* No room in peers rwnd */
7432 if (asoc->total_flight > 0) {
7433 /* we are allowed one chunk in flight */
7437 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7438 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7439 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7441 * This for loop we are in takes in each net, if
7442 * its's got space in cwnd and has data sent to it
7443 * (when CMT is off) then it calls
7444 * sctp_fill_outqueue for the net. This gets data on
7445 * the send queue for that network.
7447 * In sctp_fill_outqueue TSN's are assigned and data is
7448 * copied out of the stream buffers. Note mostly
7449 * copy by reference (we hope).
7451 net->window_probe = 0;
7452 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7453 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7454 sctp_log_cwnd(stcb, net, 1,
7455 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7459 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7460 /* nothing can be in queue for this guy */
7461 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7462 sctp_log_cwnd(stcb, net, 2,
7463 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7467 if (net->flight_size >= net->cwnd) {
7468 /* skip this network, no room - can't fill */
7469 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7470 sctp_log_cwnd(stcb, net, 3,
7471 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7475 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7476 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7478 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7480 /* memory alloc failure */
7486 /* now service each destination and send out what we can for it */
7487 /* Nothing to send? */
7488 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7489 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7490 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7494 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7495 /* get the last start point */
7496 start_at = asoc->last_net_cmt_send_started;
7497 if (start_at == NULL) {
7498 /* null so to beginning */
7499 start_at = TAILQ_FIRST(&asoc->nets);
7501 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7502 if (start_at == NULL) {
7503 start_at = TAILQ_FIRST(&asoc->nets);
7506 asoc->last_net_cmt_send_started = start_at;
7508 start_at = TAILQ_FIRST(&asoc->nets);
7510 old_start_at = NULL;
7511 again_one_more_time:
7512 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7513 /* how much can we send? */
7514 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7515 if (old_start_at && (old_start_at == net)) {
7516 /* through list ocmpletely. */
7520 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7522 * Ref-count of 1 so we cannot have data or control
7523 * queued to this address. Skip it (non-CMT).
7527 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7528 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7529 (net->flight_size >= net->cwnd)) {
7531 * Nothing on control or asconf and flight is full,
7532 * we can skip even in the CMT case.
7536 ctl_cnt = bundle_at = 0;
7537 endoutchain = outchain = NULL;
7540 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7541 skip_data_for_this_net = 1;
7543 skip_data_for_this_net = 0;
7545 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7547 * if we have a route and an ifp check to see if we
7548 * have room to send to this guy
7552 ifp = net->ro.ro_rt->rt_ifp;
7553 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7554 SCTP_STAT_INCR(sctps_ifnomemqueued);
7555 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7556 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7561 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7563 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7567 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7577 if (mtu > asoc->peers_rwnd) {
7578 if (asoc->total_flight > 0) {
7579 /* We have a packet in flight somewhere */
7580 r_mtu = asoc->peers_rwnd;
7582 /* We are always allowed to send one MTU out */
7589 /************************/
7590 /* ASCONF transmission */
7591 /************************/
7592 /* Now first lets go through the asconf queue */
7593 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7595 nchk = TAILQ_NEXT(chk, sctp_next);
7596 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7599 if (chk->whoTo != net) {
7601 * No, not sent to the network we are
7606 if (chk->data == NULL) {
7609 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7610 chk->sent != SCTP_DATAGRAM_RESEND) {
7614 * if no AUTH is yet included and this chunk
7615 * requires it, make sure to account for it. We
7616 * don't apply the size until the AUTH chunk is
7617 * actually added below in case there is no room for
7618 * this chunk. NOTE: we overload the use of "omtu"
7621 if ((auth == NULL) &&
7622 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7623 stcb->asoc.peer_auth_chunks)) {
7624 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7627 /* Here we do NOT factor the r_mtu */
7628 if ((chk->send_size < (int)(mtu - omtu)) ||
7629 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7631 * We probably should glom the mbuf chain
7632 * from the chk->data for control but the
7633 * problem is it becomes yet one more level
7634 * of tracking to do if for some reason
7635 * output fails. Then I have got to
7636 * reconstruct the merged control chain.. el
7637 * yucko.. for now we take the easy way and
7641 * Add an AUTH chunk, if chunk requires it
7642 * save the offset into the chain for AUTH
7644 if ((auth == NULL) &&
7645 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7646 stcb->asoc.peer_auth_chunks))) {
7647 outchain = sctp_add_auth_chunk(outchain,
7652 chk->rec.chunk_id.id);
7653 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7655 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7656 (int)chk->rec.chunk_id.can_take_data,
7657 chk->send_size, chk->copy_by_ref);
7658 if (outchain == NULL) {
7660 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7663 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7664 /* update our MTU size */
7665 if (mtu > (chk->send_size + omtu))
7666 mtu -= (chk->send_size + omtu);
7669 to_out += (chk->send_size + omtu);
7670 /* Do clear IP_DF ? */
7671 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7674 if (chk->rec.chunk_id.can_take_data)
7677 * set hb flag since we can use these for
7683 * should sysctl this: don't bundle data
7684 * with ASCONF since it requires AUTH
7687 chk->sent = SCTP_DATAGRAM_SENT;
7691 * Ok we are out of room but we can
7692 * output without effecting the
7693 * flight size since this little guy
7694 * is a control only packet.
7696 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7698 * do NOT clear the asconf flag as
7699 * it is used to do appropriate
7700 * source address selection.
7702 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7703 (struct sockaddr *)&net->ro._l_addr,
7704 outchain, auth_offset, auth,
7705 stcb->asoc.authinfo.active_keyid,
7706 no_fragmentflg, 0, NULL, asconf,
7707 inp->sctp_lport, stcb->rport,
7708 htonl(stcb->asoc.peer_vtag),
7709 net->port, so_locked, NULL))) {
7710 if (error == ENOBUFS) {
7711 asoc->ifp_had_enobuf = 1;
7712 SCTP_STAT_INCR(sctps_lowlevelerr);
7714 if (from_where == 0) {
7715 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7717 if (*now_filled == 0) {
7718 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7720 *now = net->last_sent_time;
7722 net->last_sent_time = *now;
7725 /* error, could not output */
7726 if (error == EHOSTUNREACH) {
7732 sctp_move_to_an_alt(stcb, asoc, net);
7737 asoc->ifp_had_enobuf = 0;
7738 if (*now_filled == 0) {
7739 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7741 *now = net->last_sent_time;
7743 net->last_sent_time = *now;
7747 * increase the number we sent, if a
7748 * cookie is sent we don't tell them
7751 outchain = endoutchain = NULL;
7755 *num_out += ctl_cnt;
7756 /* recalc a clean slate and setup */
7757 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7758 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7760 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7767 /************************/
7768 /* Control transmission */
7769 /************************/
7770 /* Now first lets go through the control queue */
7771 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7773 nchk = TAILQ_NEXT(chk, sctp_next);
7774 if (chk->whoTo != net) {
7776 * No, not sent to the network we are
7781 if (chk->data == NULL) {
7784 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7786 * It must be unsent. Cookies and ASCONF's
7787 * hang around but there timers will force
7788 * when marked for resend.
7793 * if no AUTH is yet included and this chunk
7794 * requires it, make sure to account for it. We
7795 * don't apply the size until the AUTH chunk is
7796 * actually added below in case there is no room for
7797 * this chunk. NOTE: we overload the use of "omtu"
7800 if ((auth == NULL) &&
7801 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7802 stcb->asoc.peer_auth_chunks)) {
7803 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7806 /* Here we do NOT factor the r_mtu */
7807 if ((chk->send_size < (int)(mtu - omtu)) ||
7808 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7810 * We probably should glom the mbuf chain
7811 * from the chk->data for control but the
7812 * problem is it becomes yet one more level
7813 * of tracking to do if for some reason
7814 * output fails. Then I have got to
7815 * reconstruct the merged control chain.. el
7816 * yucko.. for now we take the easy way and
7820 * Add an AUTH chunk, if chunk requires it
7821 * save the offset into the chain for AUTH
7823 if ((auth == NULL) &&
7824 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7825 stcb->asoc.peer_auth_chunks))) {
7826 outchain = sctp_add_auth_chunk(outchain,
7831 chk->rec.chunk_id.id);
7832 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7834 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7835 (int)chk->rec.chunk_id.can_take_data,
7836 chk->send_size, chk->copy_by_ref);
7837 if (outchain == NULL) {
7839 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7842 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7843 /* update our MTU size */
7844 if (mtu > (chk->send_size + omtu))
7845 mtu -= (chk->send_size + omtu);
7848 to_out += (chk->send_size + omtu);
7849 /* Do clear IP_DF ? */
7850 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7853 if (chk->rec.chunk_id.can_take_data)
7855 /* Mark things to be removed, if needed */
7856 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7857 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7858 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7859 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7860 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7861 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7862 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7863 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7864 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7865 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7866 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7868 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7871 * JRS 5/14/07 - Set the
7872 * flag to say a heartbeat
7877 /* remove these chunks at the end */
7878 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7879 /* turn off the timer */
7880 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7881 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7882 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7886 * EY -Nr-sack version of the above
7889 if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
7890 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { /* EY !?! */
7891 /* turn off the timer */
7892 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7893 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7894 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7900 * Other chunks, since they have
7901 * timers running (i.e. COOKIE) we
7902 * just "trust" that it gets sent or
7906 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7910 chk->sent = SCTP_DATAGRAM_SENT;
7915 * Ok we are out of room but we can
7916 * output without effecting the
7917 * flight size since this little guy
7918 * is a control only packet.
7921 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7923 * do NOT clear the asconf
7924 * flag as it is used to do
7925 * appropriate source
7926 * address selection.
7930 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7933 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7934 (struct sockaddr *)&net->ro._l_addr,
7937 stcb->asoc.authinfo.active_keyid,
7938 no_fragmentflg, 0, NULL, asconf,
7939 inp->sctp_lport, stcb->rport,
7940 htonl(stcb->asoc.peer_vtag),
7941 net->port, so_locked, NULL))) {
7942 if (error == ENOBUFS) {
7943 asoc->ifp_had_enobuf = 1;
7944 SCTP_STAT_INCR(sctps_lowlevelerr);
7946 if (from_where == 0) {
7947 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7949 /* error, could not output */
7951 if (*now_filled == 0) {
7952 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7954 *now = net->last_sent_time;
7956 net->last_sent_time = *now;
7960 if (error == EHOSTUNREACH) {
7966 sctp_move_to_an_alt(stcb, asoc, net);
7971 asoc->ifp_had_enobuf = 0;
7972 /* Only HB or ASCONF advances time */
7974 if (*now_filled == 0) {
7975 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7977 *now = net->last_sent_time;
7979 net->last_sent_time = *now;
7984 * increase the number we sent, if a
7985 * cookie is sent we don't tell them
7988 outchain = endoutchain = NULL;
7992 *num_out += ctl_cnt;
7993 /* recalc a clean slate and setup */
7994 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7995 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7997 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8004 /* JRI: if dest is in PF state, do not send data to it */
8005 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8006 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8007 (net->dest_state & SCTP_ADDR_PF)) {
8010 if (net->flight_size >= net->cwnd) {
8013 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
8014 (net->flight_size > max_rwnd_per_dest)) {
8017 /*********************/
8018 /* Data transmission */
8019 /*********************/
8021 * if AUTH for DATA is required and no AUTH has been added
8022 * yet, account for this in the mtu now... if no data can be
8023 * bundled, this adjustment won't matter anyways since the
8024 * packet will be going out...
8026 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8027 stcb->asoc.peer_auth_chunks);
8028 if (data_auth_reqd && (auth == NULL)) {
8029 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8031 /* now lets add any data within the MTU constraints */
8032 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8034 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8035 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8041 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8042 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8052 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8053 (skip_data_for_this_net == 0)) ||
8055 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8056 if (no_data_chunks) {
8057 /* let only control go out */
8061 if (net->flight_size >= net->cwnd) {
8062 /* skip this net, no room for data */
8066 nchk = TAILQ_NEXT(chk, sctp_next);
8067 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8068 if (chk->whoTo != net) {
8070 * For CMT, steal the data
8071 * to this network if its
8074 sctp_free_remote_addr(chk->whoTo);
8076 atomic_add_int(&chk->whoTo->ref_count, 1);
8078 } else if (chk->whoTo != net) {
8079 /* No, not sent to this net */
8082 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8084 * strange, we have a chunk that is
8085 * to big for its destination and
8086 * yet no fragment ok flag.
8087 * Something went wrong when the
8088 * PMTU changed...we did not mark
8089 * this chunk for some reason?? I
8090 * will fix it here by letting IP
8091 * fragment it for now and printing
8092 * a warning. This really should not
8095 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8096 chk->send_size, mtu);
8097 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8099 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8100 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8101 struct sctp_data_chunk *dchkh;
8103 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8104 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8106 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8107 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8108 /* ok we will add this one */
8111 * Add an AUTH chunk, if chunk
8112 * requires it, save the offset into
8113 * the chain for AUTH
8115 if (data_auth_reqd) {
8117 outchain = sctp_add_auth_chunk(outchain,
8123 auth_keyid = chk->auth_keyid;
8125 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8126 } else if (override_ok) {
8131 auth_keyid = chk->auth_keyid;
8133 } else if (auth_keyid != chk->auth_keyid) {
8141 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8142 chk->send_size, chk->copy_by_ref);
8143 if (outchain == NULL) {
8144 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8145 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8146 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8149 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8152 /* upate our MTU size */
8153 /* Do clear IP_DF ? */
8154 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8157 /* unsigned subtraction of mtu */
8158 if (mtu > chk->send_size)
8159 mtu -= chk->send_size;
8162 /* unsigned subtraction of r_mtu */
8163 if (r_mtu > chk->send_size)
8164 r_mtu -= chk->send_size;
8168 to_out += chk->send_size;
8169 if ((to_out > mx_mtu) && no_fragmentflg) {
8171 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8173 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8177 chk->window_probe = 0;
8178 data_list[bundle_at++] = chk;
8179 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8183 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8184 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8185 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8187 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8189 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8190 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8200 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8202 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8203 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8204 data_list[0]->window_probe = 1;
8205 net->window_probe = 1;
8211 * Must be sent in order of the
8212 * TSN's (on a network)
8216 } /* for (chunk gather loop for this net) */
8217 } /* if asoc.state OPEN */
8219 /* Is there something to send for this destination? */
8221 /* We may need to start a control timer or two */
8223 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8226 * do NOT clear the asconf flag as it is
8227 * used to do appropriate source address
8232 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8235 /* must start a send timer if data is being sent */
8236 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8238 * no timer running on this destination
8241 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8242 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8243 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8245 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8246 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8248 * JRS 5/14/07 - If a HB has been sent to a
8249 * PF destination and no T3 timer is
8250 * currently running, start the T3 timer to
8251 * track the HBs that were sent.
8253 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8255 /* Now send it, if there is anything to send :> */
8256 if ((error = sctp_lowlevel_chunk_output(inp,
8259 (struct sockaddr *)&net->ro._l_addr,
8268 inp->sctp_lport, stcb->rport,
8269 htonl(stcb->asoc.peer_vtag),
8270 net->port, so_locked, NULL))) {
8271 /* error, we could not output */
8272 if (error == ENOBUFS) {
8273 SCTP_STAT_INCR(sctps_lowlevelerr);
8274 asoc->ifp_had_enobuf = 1;
8276 if (from_where == 0) {
8277 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8279 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8281 if (*now_filled == 0) {
8282 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8284 *now = net->last_sent_time;
8286 net->last_sent_time = *now;
8290 if (error == EHOSTUNREACH) {
8292 * Destination went unreachable
8295 sctp_move_to_an_alt(stcb, asoc, net);
8299 * I add this line to be paranoid. As far as
8300 * I can tell the continue, takes us back to
8301 * the top of the for, but just to make sure
8302 * I will reset these again here.
8304 ctl_cnt = bundle_at = 0;
8305 continue; /* This takes us back to the
8306 * for() for the nets. */
8308 asoc->ifp_had_enobuf = 0;
8310 outchain = endoutchain = NULL;
8313 if (bundle_at || hbflag) {
8314 /* For data/asconf and hb set time */
8315 if (*now_filled == 0) {
8316 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8318 *now = net->last_sent_time;
8320 net->last_sent_time = *now;
8324 *num_out += (ctl_cnt + bundle_at);
8327 /* setup for a RTO measurement */
8328 tsns_sent = data_list[0]->rec.data.TSN_seq;
8329 /* fill time if not already filled */
8330 if (*now_filled == 0) {
8331 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8333 *now = asoc->time_last_sent;
8335 asoc->time_last_sent = *now;
8337 data_list[0]->do_rtt = 1;
8338 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8339 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8340 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8341 if (net->flight_size < net->cwnd) {
8342 /* start or restart it */
8343 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8344 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8345 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8347 SCTP_STAT_INCR(sctps_earlyfrstrout);
8348 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8350 /* stop it if its running */
8351 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8352 SCTP_STAT_INCR(sctps_earlyfrstpout);
8353 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8354 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8363 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8364 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8367 if (old_start_at == NULL) {
8368 old_start_at = start_at;
8369 start_at = TAILQ_FIRST(&asoc->nets);
8371 goto again_one_more_time;
8374 * At the end there should be no NON timed chunks hanging on this
8377 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8378 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8380 if ((*num_out == 0) && (*reason_code == 0)) {
8385 sctp_clean_up_ctl(stcb, asoc);
8390 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8393 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8394 * the control chunk queue.
8396 struct sctp_chunkhdr *hdr;
8397 struct sctp_tmit_chunk *chk;
8400 SCTP_TCB_LOCK_ASSERT(stcb);
8401 sctp_alloc_a_chunk(stcb, chk);
8404 sctp_m_freem(op_err);
8407 chk->copy_by_ref = 0;
8408 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8409 if (op_err == NULL) {
8410 sctp_free_a_chunk(stcb, chk);
8415 while (mat != NULL) {
8416 chk->send_size += SCTP_BUF_LEN(mat);
8417 mat = SCTP_BUF_NEXT(mat);
8419 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8420 chk->rec.chunk_id.can_take_data = 1;
8421 chk->sent = SCTP_DATAGRAM_UNSENT;
8424 chk->asoc = &stcb->asoc;
8426 chk->whoTo = chk->asoc->primary_destination;
8427 atomic_add_int(&chk->whoTo->ref_count, 1);
8428 hdr = mtod(op_err, struct sctp_chunkhdr *);
8429 hdr->chunk_type = SCTP_OPERATION_ERROR;
8430 hdr->chunk_flags = 0;
8431 hdr->chunk_length = htons(chk->send_size);
8432 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8435 chk->asoc->ctrl_queue_cnt++;
8439 sctp_send_cookie_echo(struct mbuf *m,
8441 struct sctp_tcb *stcb,
8442 struct sctp_nets *net)
8445 * pull out the cookie and put it at the front of the control chunk
8449 struct mbuf *cookie;
8450 struct sctp_paramhdr parm, *phdr;
8451 struct sctp_chunkhdr *hdr;
8452 struct sctp_tmit_chunk *chk;
8453 uint16_t ptype, plen;
8455 /* First find the cookie in the param area */
8457 at = offset + sizeof(struct sctp_init_chunk);
8459 SCTP_TCB_LOCK_ASSERT(stcb);
8461 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8465 ptype = ntohs(phdr->param_type);
8466 plen = ntohs(phdr->param_length);
8467 if (ptype == SCTP_STATE_COOKIE) {
8470 /* found the cookie */
8471 if ((pad = (plen % 4))) {
8474 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8475 if (cookie == NULL) {
8479 #ifdef SCTP_MBUF_LOGGING
8480 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8485 if (SCTP_BUF_IS_EXTENDED(mat)) {
8486 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8488 mat = SCTP_BUF_NEXT(mat);
8494 at += SCTP_SIZE32(plen);
8496 if (cookie == NULL) {
8497 /* Did not find the cookie */
8500 /* ok, we got the cookie lets change it into a cookie echo chunk */
8502 /* first the change from param to cookie */
8503 hdr = mtod(cookie, struct sctp_chunkhdr *);
8504 hdr->chunk_type = SCTP_COOKIE_ECHO;
8505 hdr->chunk_flags = 0;
8506 /* get the chunk stuff now and place it in the FRONT of the queue */
8507 sctp_alloc_a_chunk(stcb, chk);
8510 sctp_m_freem(cookie);
8513 chk->copy_by_ref = 0;
8514 chk->send_size = plen;
8515 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8516 chk->rec.chunk_id.can_take_data = 0;
8517 chk->sent = SCTP_DATAGRAM_UNSENT;
8519 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8520 chk->asoc = &stcb->asoc;
8522 chk->whoTo = chk->asoc->primary_destination;
8523 atomic_add_int(&chk->whoTo->ref_count, 1);
8524 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8525 chk->asoc->ctrl_queue_cnt++;
8530 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8534 struct sctp_nets *net)
8537 * take a HB request and make it into a HB ack and send it.
8539 struct mbuf *outchain;
8540 struct sctp_chunkhdr *chdr;
8541 struct sctp_tmit_chunk *chk;
8545 /* must have a net pointer */
8548 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8549 if (outchain == NULL) {
8550 /* gak out of memory */
8553 #ifdef SCTP_MBUF_LOGGING
8554 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8559 if (SCTP_BUF_IS_EXTENDED(mat)) {
8560 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8562 mat = SCTP_BUF_NEXT(mat);
8566 chdr = mtod(outchain, struct sctp_chunkhdr *);
8567 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8568 chdr->chunk_flags = 0;
8569 if (chk_length % 4) {
8571 uint32_t cpthis = 0;
8574 padlen = 4 - (chk_length % 4);
8575 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8577 sctp_alloc_a_chunk(stcb, chk);
8580 sctp_m_freem(outchain);
8583 chk->copy_by_ref = 0;
8584 chk->send_size = chk_length;
8585 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8586 chk->rec.chunk_id.can_take_data = 1;
8587 chk->sent = SCTP_DATAGRAM_UNSENT;
8590 chk->asoc = &stcb->asoc;
8591 chk->data = outchain;
8593 atomic_add_int(&chk->whoTo->ref_count, 1);
8594 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8595 chk->asoc->ctrl_queue_cnt++;
8599 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8601 /* formulate and queue a cookie-ack back to sender */
8602 struct mbuf *cookie_ack;
8603 struct sctp_chunkhdr *hdr;
8604 struct sctp_tmit_chunk *chk;
8607 SCTP_TCB_LOCK_ASSERT(stcb);
8609 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8610 if (cookie_ack == NULL) {
8614 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8615 sctp_alloc_a_chunk(stcb, chk);
8618 sctp_m_freem(cookie_ack);
8621 chk->copy_by_ref = 0;
8622 chk->send_size = sizeof(struct sctp_chunkhdr);
8623 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8624 chk->rec.chunk_id.can_take_data = 1;
8625 chk->sent = SCTP_DATAGRAM_UNSENT;
8628 chk->asoc = &stcb->asoc;
8629 chk->data = cookie_ack;
8630 if (chk->asoc->last_control_chunk_from != NULL) {
8631 chk->whoTo = chk->asoc->last_control_chunk_from;
8633 chk->whoTo = chk->asoc->primary_destination;
8635 atomic_add_int(&chk->whoTo->ref_count, 1);
8636 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8637 hdr->chunk_type = SCTP_COOKIE_ACK;
8638 hdr->chunk_flags = 0;
8639 hdr->chunk_length = htons(chk->send_size);
8640 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8641 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8642 chk->asoc->ctrl_queue_cnt++;
8648 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8650 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8651 struct mbuf *m_shutdown_ack;
8652 struct sctp_shutdown_ack_chunk *ack_cp;
8653 struct sctp_tmit_chunk *chk;
8655 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8656 if (m_shutdown_ack == NULL) {
8660 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8661 sctp_alloc_a_chunk(stcb, chk);
8664 sctp_m_freem(m_shutdown_ack);
8667 chk->copy_by_ref = 0;
8668 chk->send_size = sizeof(struct sctp_chunkhdr);
8669 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8670 chk->rec.chunk_id.can_take_data = 1;
8671 chk->sent = SCTP_DATAGRAM_UNSENT;
8674 chk->asoc = &stcb->asoc;
8675 chk->data = m_shutdown_ack;
8677 atomic_add_int(&net->ref_count, 1);
8679 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8680 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8681 ack_cp->ch.chunk_flags = 0;
8682 ack_cp->ch.chunk_length = htons(chk->send_size);
8683 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8684 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8685 chk->asoc->ctrl_queue_cnt++;
8690 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8692 /* formulate and queue a SHUTDOWN to the sender */
8693 struct mbuf *m_shutdown;
8694 struct sctp_shutdown_chunk *shutdown_cp;
8695 struct sctp_tmit_chunk *chk;
8697 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8698 if (m_shutdown == NULL) {
8702 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8703 sctp_alloc_a_chunk(stcb, chk);
8706 sctp_m_freem(m_shutdown);
8709 chk->copy_by_ref = 0;
8710 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8711 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8712 chk->rec.chunk_id.can_take_data = 1;
8713 chk->sent = SCTP_DATAGRAM_UNSENT;
8716 chk->asoc = &stcb->asoc;
8717 chk->data = m_shutdown;
8719 atomic_add_int(&net->ref_count, 1);
8721 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8722 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8723 shutdown_cp->ch.chunk_flags = 0;
8724 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8725 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8726 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8727 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8728 chk->asoc->ctrl_queue_cnt++;
8733 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8736 * formulate and queue an ASCONF to the peer. ASCONF parameters
8737 * should be queued on the assoc queue.
8739 struct sctp_tmit_chunk *chk;
8740 struct mbuf *m_asconf;
8743 SCTP_TCB_LOCK_ASSERT(stcb);
8745 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8746 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8747 /* can't send a new one if there is one in flight already */
8750 /* compose an ASCONF chunk, maximum length is PMTU */
8751 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8752 if (m_asconf == NULL) {
8755 sctp_alloc_a_chunk(stcb, chk);
8758 sctp_m_freem(m_asconf);
8761 chk->copy_by_ref = 0;
8762 chk->data = m_asconf;
8763 chk->send_size = len;
8764 chk->rec.chunk_id.id = SCTP_ASCONF;
8765 chk->rec.chunk_id.can_take_data = 0;
8766 chk->sent = SCTP_DATAGRAM_UNSENT;
8768 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8769 chk->asoc = &stcb->asoc;
8771 atomic_add_int(&chk->whoTo->ref_count, 1);
8772 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8773 chk->asoc->ctrl_queue_cnt++;
8778 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8781 * formulate and queue a asconf-ack back to sender. the asconf-ack
8782 * must be stored in the tcb.
8784 struct sctp_tmit_chunk *chk;
8785 struct sctp_asconf_ack *ack, *latest_ack;
8786 struct mbuf *m_ack, *m;
8787 struct sctp_nets *net = NULL;
8789 SCTP_TCB_LOCK_ASSERT(stcb);
8790 /* Get the latest ASCONF-ACK */
8791 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8792 if (latest_ack == NULL) {
8795 if (latest_ack->last_sent_to != NULL &&
8796 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8797 /* we're doing a retransmission */
8798 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8801 if (stcb->asoc.last_control_chunk_from == NULL)
8802 net = stcb->asoc.primary_destination;
8804 net = stcb->asoc.last_control_chunk_from;
8808 if (stcb->asoc.last_control_chunk_from == NULL)
8809 net = stcb->asoc.primary_destination;
8811 net = stcb->asoc.last_control_chunk_from;
8813 latest_ack->last_sent_to = net;
8815 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8816 if (ack->data == NULL) {
8819 /* copy the asconf_ack */
8820 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8821 if (m_ack == NULL) {
8822 /* couldn't copy it */
8825 #ifdef SCTP_MBUF_LOGGING
8826 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8831 if (SCTP_BUF_IS_EXTENDED(mat)) {
8832 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8834 mat = SCTP_BUF_NEXT(mat);
8839 sctp_alloc_a_chunk(stcb, chk);
8843 sctp_m_freem(m_ack);
8846 chk->copy_by_ref = 0;
8853 chk->send_size = ack->len;
8854 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8855 chk->rec.chunk_id.can_take_data = 1;
8856 chk->sent = SCTP_DATAGRAM_UNSENT;
8858 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8859 chk->asoc = &stcb->asoc;
8860 atomic_add_int(&chk->whoTo->ref_count, 1);
8862 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8863 chk->asoc->ctrl_queue_cnt++;
8870 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8871 struct sctp_tcb *stcb,
8872 struct sctp_association *asoc,
8873 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8874 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8880 * send out one MTU of retransmission. If fast_retransmit is
8881 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8882 * rwnd. For a Cookie or Asconf in the control chunk queue we
8883 * retransmit them by themselves.
8885 * For data chunks we will pick out the lowest TSN's in the sent_queue
8886 * marked for resend and bundle them all together (up to a MTU of
8887 * destination). The address to send to should have been
8888 * selected/changed where the retransmission was marked (i.e. in FR
8889 * or t3-timeout routines).
8891 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8892 struct sctp_tmit_chunk *chk, *fwd;
8893 struct mbuf *m, *endofchain;
8894 struct sctp_nets *net = NULL;
8895 uint32_t tsns_sent = 0;
8896 int no_fragmentflg, bundle_at, cnt_thru;
8898 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8899 struct sctp_auth_chunk *auth = NULL;
8900 uint32_t auth_offset = 0;
8901 uint16_t auth_keyid;
8902 int override_ok = 1;
8903 int data_auth_reqd = 0;
8906 SCTP_TCB_LOCK_ASSERT(stcb);
8907 tmr_started = ctl_cnt = bundle_at = error = 0;
8912 endofchain = m = NULL;
8913 auth_keyid = stcb->asoc.authinfo.active_keyid;
8914 #ifdef SCTP_AUDITING_ENABLED
8915 sctp_audit_log(0xC3, 1);
8917 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8918 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8919 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8920 asoc->sent_queue_retran_cnt);
8921 asoc->sent_queue_cnt = 0;
8922 asoc->sent_queue_cnt_removeable = 0;
8923 /* send back 0/0 so we enter normal transmission */
8927 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8928 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8929 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8930 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8931 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8932 if (chk != asoc->str_reset) {
8934 * not eligible for retran if its
8941 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8946 * Add an AUTH chunk, if chunk requires it save the
8947 * offset into the chain for AUTH
8949 if ((auth == NULL) &&
8950 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8951 stcb->asoc.peer_auth_chunks))) {
8952 m = sctp_add_auth_chunk(m, &endofchain,
8953 &auth, &auth_offset,
8955 chk->rec.chunk_id.id);
8956 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8958 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8964 /* do we have control chunks to retransmit? */
8966 /* Start a timer no matter if we suceed or fail */
8967 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8968 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8969 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8970 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8971 chk->snd_count++; /* update our count */
8972 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8973 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
8974 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
8975 no_fragmentflg, 0, NULL, 0,
8976 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
8977 chk->whoTo->port, so_locked, NULL))) {
8978 SCTP_STAT_INCR(sctps_lowlevelerr);
8981 m = endofchain = NULL;
8985 * We don't want to mark the net->sent time here since this
8986 * we use this for HB and retrans cannot measure RTT
8988 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8990 chk->sent = SCTP_DATAGRAM_SENT;
8991 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8995 /* Clean up the fwd-tsn list */
8996 sctp_clean_up_ctl(stcb, asoc);
9001 * Ok, it is just data retransmission we need to do or that and a
9002 * fwd-tsn with it all.
9004 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9005 return (SCTP_RETRAN_DONE);
9007 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9008 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9009 /* not yet open, resend the cookie and that is it */
9012 #ifdef SCTP_AUDITING_ENABLED
9013 sctp_auditing(20, inp, stcb, NULL);
9015 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9016 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9017 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9018 /* No, not sent to this net or not ready for rtx */
9021 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9022 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9023 /* Gak, we have exceeded max unlucky retran, abort! */
9024 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9026 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9027 atomic_add_int(&stcb->asoc.refcnt, 1);
9028 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9029 SCTP_TCB_LOCK(stcb);
9030 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9031 return (SCTP_RETRAN_EXIT);
9033 /* pick up the net */
9035 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9036 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9038 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9041 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9042 /* No room in peers rwnd */
9045 tsn = asoc->last_acked_seq + 1;
9046 if (tsn == chk->rec.data.TSN_seq) {
9048 * we make a special exception for this
9049 * case. The peer has no rwnd but is missing
9050 * the lowest chunk.. which is probably what
9051 * is holding up the rwnd.
9053 goto one_chunk_around;
9058 if (asoc->peers_rwnd < mtu) {
9060 if ((asoc->peers_rwnd == 0) &&
9061 (asoc->total_flight == 0)) {
9062 chk->window_probe = 1;
9063 chk->whoTo->window_probe = 1;
9066 #ifdef SCTP_AUDITING_ENABLED
9067 sctp_audit_log(0xC3, 2);
9071 net->fast_retran_ip = 0;
9072 if (chk->rec.data.doing_fast_retransmit == 0) {
9074 * if no FR in progress skip destination that have
9075 * flight_size > cwnd.
9077 if (net->flight_size >= net->cwnd) {
9082 * Mark the destination net to have FR recovery
9086 net->fast_retran_ip = 1;
9090 * if no AUTH is yet included and this chunk requires it,
9091 * make sure to account for it. We don't apply the size
9092 * until the AUTH chunk is actually added below in case
9093 * there is no room for this chunk.
9095 if (data_auth_reqd && (auth == NULL)) {
9096 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9100 if ((chk->send_size <= (mtu - dmtu)) ||
9101 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9102 /* ok we will add this one */
9103 if (data_auth_reqd) {
9105 m = sctp_add_auth_chunk(m,
9111 auth_keyid = chk->auth_keyid;
9113 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9114 } else if (override_ok) {
9115 auth_keyid = chk->auth_keyid;
9117 } else if (chk->auth_keyid != auth_keyid) {
9118 /* different keyid, so done bundling */
9122 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9124 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9127 /* Do clear IP_DF ? */
9128 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9131 /* upate our MTU size */
9132 if (mtu > (chk->send_size + dmtu))
9133 mtu -= (chk->send_size + dmtu);
9136 data_list[bundle_at++] = chk;
9137 if (one_chunk && (asoc->total_flight <= 0)) {
9138 SCTP_STAT_INCR(sctps_windowprobed);
9141 if (one_chunk == 0) {
9143 * now are there anymore forward from chk to pick
9146 fwd = TAILQ_NEXT(chk, sctp_next);
9148 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9149 /* Nope, not for retran */
9150 fwd = TAILQ_NEXT(fwd, sctp_next);
9153 if (fwd->whoTo != net) {
9154 /* Nope, not the net in question */
9155 fwd = TAILQ_NEXT(fwd, sctp_next);
9158 if (data_auth_reqd && (auth == NULL)) {
9159 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9162 if (fwd->send_size <= (mtu - dmtu)) {
9163 if (data_auth_reqd) {
9165 m = sctp_add_auth_chunk(m,
9171 auth_keyid = fwd->auth_keyid;
9173 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9174 } else if (override_ok) {
9175 auth_keyid = fwd->auth_keyid;
9177 } else if (fwd->auth_keyid != auth_keyid) {
9185 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9187 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9190 /* Do clear IP_DF ? */
9191 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9194 /* upate our MTU size */
9195 if (mtu > (fwd->send_size + dmtu))
9196 mtu -= (fwd->send_size + dmtu);
9199 data_list[bundle_at++] = fwd;
9200 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9203 fwd = TAILQ_NEXT(fwd, sctp_next);
9205 /* can't fit so we are done */
9210 /* Is there something to send for this destination? */
9213 * No matter if we fail/or suceed we should start a
9214 * timer. A failure is like a lost IP packet :-)
9216 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9218 * no timer running on this destination
9221 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9224 /* Now lets send it, if there is anything to send :> */
9225 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9226 (struct sockaddr *)&net->ro._l_addr, m,
9227 auth_offset, auth, auth_keyid,
9228 no_fragmentflg, 0, NULL, 0,
9229 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9230 net->port, so_locked, NULL))) {
9231 /* error, we could not output */
9232 SCTP_STAT_INCR(sctps_lowlevelerr);
9235 m = endofchain = NULL;
9240 * We don't want to mark the net->sent time here
9241 * since this we use this for HB and retrans cannot
9244 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9246 /* For auto-close */
9248 if (*now_filled == 0) {
9249 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9250 *now = asoc->time_last_sent;
9253 asoc->time_last_sent = *now;
9255 *cnt_out += bundle_at;
9256 #ifdef SCTP_AUDITING_ENABLED
9257 sctp_audit_log(0xC4, bundle_at);
9260 tsns_sent = data_list[0]->rec.data.TSN_seq;
9262 for (i = 0; i < bundle_at; i++) {
9263 SCTP_STAT_INCR(sctps_sendretransdata);
9264 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9266 * When we have a revoked data, and we
9267 * retransmit it, then we clear the revoked
9268 * flag since this flag dictates if we
9269 * subtracted from the fs
9271 if (data_list[i]->rec.data.chunk_was_revoked) {
9272 /* Deflate the cwnd */
9273 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9274 data_list[i]->rec.data.chunk_was_revoked = 0;
9276 data_list[i]->snd_count++;
9277 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9278 /* record the time */
9279 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9280 if (data_list[i]->book_size_scale) {
9282 * need to double the book size on
9285 data_list[i]->book_size_scale = 0;
9287 * Since we double the booksize, we
9288 * must also double the output queue
9289 * size, since this get shrunk when
9290 * we free by this amount.
9292 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9293 data_list[i]->book_size *= 2;
9297 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9298 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9299 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9301 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9302 (uint32_t) (data_list[i]->send_size +
9303 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9306 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9307 data_list[i]->whoTo->flight_size,
9308 data_list[i]->book_size,
9309 (uintptr_t) data_list[i]->whoTo,
9310 data_list[i]->rec.data.TSN_seq);
9312 sctp_flight_size_increase(data_list[i]);
9313 sctp_total_flight_increase(stcb, data_list[i]);
9314 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9315 /* SWS sender side engages */
9316 asoc->peers_rwnd = 0;
9319 (data_list[i]->rec.data.doing_fast_retransmit)) {
9320 SCTP_STAT_INCR(sctps_sendfastretrans);
9321 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9322 (tmr_started == 0)) {
9324 * ok we just fast-retrans'd
9325 * the lowest TSN, i.e the
9326 * first on the list. In
9327 * this case we want to give
9328 * some more time to get a
9329 * SACK back without a
9332 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9333 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9334 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9338 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9339 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9341 #ifdef SCTP_AUDITING_ENABLED
9342 sctp_auditing(21, inp, stcb, NULL);
9348 if (asoc->sent_queue_retran_cnt <= 0) {
9349 /* all done we have no more to retran */
9350 asoc->sent_queue_retran_cnt = 0;
9354 /* No more room in rwnd */
9357 /* stop the for loop here. we sent out a packet */
9365 sctp_timer_validation(struct sctp_inpcb *inp,
9366 struct sctp_tcb *stcb,
9367 struct sctp_association *asoc,
9370 struct sctp_nets *net;
9372 /* Validate that a timer is running somewhere */
9373 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9374 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9375 /* Here is a timer */
9379 SCTP_TCB_LOCK_ASSERT(stcb);
9380 /* Gak, we did not have a timer somewhere */
9381 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9382 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9387 sctp_chunk_output(struct sctp_inpcb *inp,
9388 struct sctp_tcb *stcb,
9391 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9397 * Ok this is the generic chunk service queue. we must do the
9399 * - See if there are retransmits pending, if so we must
9401 * - Service the stream queue that is next, moving any
9402 * message (note I must get a complete message i.e.
9403 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9405 * - Check to see if the cwnd/rwnd allows any output, if so we
9406 * go ahead and fomulate and send the low level chunks. Making sure
9407 * to combine any control in the control chunk queue also.
9409 struct sctp_association *asoc;
9410 struct sctp_nets *net;
9411 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9412 burst_cnt = 0, burst_limit = 0;
9416 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9418 int fr_done, tot_frs = 0;
9421 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9422 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9428 SCTP_TCB_LOCK_ASSERT(stcb);
9430 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9432 if ((un_sent <= 0) &&
9433 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9434 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9435 (asoc->sent_queue_retran_cnt == 0)) {
9436 /* Nothing to do unless there is something to be sent left */
9440 * Do we have something to send, data or control AND a sack timer
9441 * running, if so piggy-back the sack.
9443 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9445 * EY if nr_sacks used then send an nr-sack , a sack
9448 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
9449 sctp_send_nr_sack(stcb);
9451 sctp_send_sack(stcb);
9452 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9454 while (asoc->sent_queue_retran_cnt) {
9456 * Ok, it is retransmission time only, we send out only ONE
9457 * packet with a single call off to the retran code.
9459 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9461 * Special hook for handling cookiess discarded
9462 * by peer that carried data. Send cookie-ack only
9463 * and then the next call with get the retran's.
9465 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9467 &now, &now_filled, frag_point, so_locked);
9469 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9470 /* if its not from a HB then do it */
9472 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9478 * its from any other place, we don't allow retran
9479 * output (only control)
9484 /* Can't send anymore */
9486 * now lets push out control by calling med-level
9487 * output once. this assures that we WILL send HB's
9490 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9492 &now, &now_filled, frag_point, so_locked);
9493 #ifdef SCTP_AUDITING_ENABLED
9494 sctp_auditing(8, inp, stcb, NULL);
9496 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9501 * The count was off.. retran is not happening so do
9502 * the normal retransmission.
9504 #ifdef SCTP_AUDITING_ENABLED
9505 sctp_auditing(9, inp, stcb, NULL);
9507 if (ret == SCTP_RETRAN_EXIT) {
9512 if (from_where == SCTP_OUTPUT_FROM_T3) {
9513 /* Only one transmission allowed out of a timeout */
9514 #ifdef SCTP_AUDITING_ENABLED
9515 sctp_auditing(10, inp, stcb, NULL);
9517 /* Push out any control */
9518 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9519 &now, &now_filled, frag_point, so_locked);
9522 if (tot_frs > asoc->max_burst) {
9523 /* Hit FR burst limit */
9526 if ((num_out == 0) && (ret == 0)) {
9528 /* No more retrans to send */
9532 #ifdef SCTP_AUDITING_ENABLED
9533 sctp_auditing(12, inp, stcb, NULL);
9535 /* Check for bad destinations, if they exist move chunks around. */
9536 burst_limit = asoc->max_burst;
9537 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9538 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9539 SCTP_ADDR_NOT_REACHABLE) {
9541 * if possible move things off of this address we
9542 * still may send below due to the dormant state but
9543 * we try to find an alternate address to send to
9544 * and if we have one we move all queued data on the
9545 * out wheel to this alternate address.
9547 if (net->ref_count > 1)
9548 sctp_move_to_an_alt(stcb, asoc, net);
9549 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9550 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9551 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9553 * JRS 5/14/07 - If CMT PF is on and the current
9554 * destination is in PF state, move all queued data
9555 * to an alternate desination.
9557 if (net->ref_count > 1)
9558 sctp_move_to_an_alt(stcb, asoc, net);
9561 * if ((asoc->sat_network) || (net->addr_is_local))
9562 * { burst_limit = asoc->max_burst *
9563 * SCTP_SAT_NETWORK_BURST_INCR; }
9565 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9566 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9568 * JRS - Use the congestion control
9569 * given in the congestion control
9572 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9573 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9574 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9576 SCTP_STAT_INCR(sctps_maxburstqueued);
9578 net->fast_retran_ip = 0;
9580 if (net->flight_size == 0) {
9581 /* Should be decaying the cwnd here */
9590 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9591 &reason_code, 0, from_where,
9592 &now, &now_filled, frag_point, so_locked);
9594 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9595 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9596 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9598 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9599 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9600 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9604 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9608 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9609 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9611 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9616 * When nagle is on, we look at how much is un_sent, then
9617 * if its smaller than an MTU and we have data in
9620 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9621 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9622 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9623 (stcb->asoc.total_flight > 0)) {
9627 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9628 TAILQ_EMPTY(&asoc->send_queue) &&
9629 TAILQ_EMPTY(&asoc->out_wheel)) {
9630 /* Nothing left to send */
9633 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9634 /* Nothing left to send */
9637 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9638 (burst_cnt < burst_limit)));
9640 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9641 if (burst_cnt >= burst_limit) {
9642 SCTP_STAT_INCR(sctps_maxburstqueued);
9643 asoc->burst_limit_applied = 1;
9644 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9645 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9648 asoc->burst_limit_applied = 0;
9651 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9652 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9654 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9658 * Now we need to clean up the control chunk chain if a ECNE is on
9659 * it. It must be marked as UNSENT again so next call will continue
9660 * to send it until such time that we get a CWR, to remove it.
9662 if (stcb->asoc.ecn_echo_cnt_onq)
9663 sctp_fix_ecn_echo(asoc);
9669 sctp_output(inp, m, addr, control, p, flags)
9670 struct sctp_inpcb *inp;
9672 struct sockaddr *addr;
9673 struct mbuf *control;
9678 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9681 if (inp->sctp_socket == NULL) {
9682 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9685 return (sctp_sosend(inp->sctp_socket,
9695 send_forward_tsn(struct sctp_tcb *stcb,
9696 struct sctp_association *asoc)
9698 struct sctp_tmit_chunk *chk;
9699 struct sctp_forward_tsn_chunk *fwdtsn;
9700 uint32_t advance_peer_ack_point;
9702 SCTP_TCB_LOCK_ASSERT(stcb);
9703 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9704 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9705 /* mark it to unsent */
9706 chk->sent = SCTP_DATAGRAM_UNSENT;
9708 /* Do we correct its output location? */
9709 if (chk->whoTo != asoc->primary_destination) {
9710 sctp_free_remote_addr(chk->whoTo);
9711 chk->whoTo = asoc->primary_destination;
9712 atomic_add_int(&chk->whoTo->ref_count, 1);
9714 goto sctp_fill_in_rest;
9717 /* Ok if we reach here we must build one */
9718 sctp_alloc_a_chunk(stcb, chk);
9722 chk->copy_by_ref = 0;
9723 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9724 chk->rec.chunk_id.can_take_data = 0;
9728 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9729 if (chk->data == NULL) {
9730 sctp_free_a_chunk(stcb, chk);
9733 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9734 chk->sent = SCTP_DATAGRAM_UNSENT;
9736 chk->whoTo = asoc->primary_destination;
9737 atomic_add_int(&chk->whoTo->ref_count, 1);
9738 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9739 asoc->ctrl_queue_cnt++;
9742 * Here we go through and fill out the part that deals with
9743 * stream/seq of the ones we skip.
9745 SCTP_BUF_LEN(chk->data) = 0;
9747 struct sctp_tmit_chunk *at, *tp1, *last;
9748 struct sctp_strseq *strseq;
9749 unsigned int cnt_of_space, i, ovh;
9750 unsigned int space_needed;
9751 unsigned int cnt_of_skipped = 0;
9753 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9754 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9755 /* no more to look at */
9758 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9759 /* We don't report these */
9764 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9765 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9767 cnt_of_space = M_TRAILINGSPACE(chk->data);
9769 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9770 ovh = SCTP_MIN_OVERHEAD;
9772 ovh = SCTP_MIN_V4_OVERHEAD;
9774 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9775 /* trim to a mtu size */
9776 cnt_of_space = asoc->smallest_mtu - ovh;
9778 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9779 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9780 0xff, 0, cnt_of_skipped,
9781 asoc->advanced_peer_ack_point);
9784 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9785 if (cnt_of_space < space_needed) {
9787 * ok we must trim down the chunk by lowering the
9788 * advance peer ack point.
9790 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9791 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9792 0xff, 0xff, cnt_of_space,
9795 cnt_of_skipped = (cnt_of_space -
9796 ((sizeof(struct sctp_forward_tsn_chunk)) /
9797 sizeof(struct sctp_strseq)));
9799 * Go through and find the TSN that will be the one
9802 at = TAILQ_FIRST(&asoc->sent_queue);
9803 for (i = 0; i < cnt_of_skipped; i++) {
9804 tp1 = TAILQ_NEXT(at, sctp_next);
9807 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9808 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9809 0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9810 asoc->advanced_peer_ack_point);
9814 * last now points to last one I can report, update
9817 advance_peer_ack_point = last->rec.data.TSN_seq;
9818 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9820 chk->send_size = space_needed;
9821 /* Setup the chunk */
9822 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9823 fwdtsn->ch.chunk_length = htons(chk->send_size);
9824 fwdtsn->ch.chunk_flags = 0;
9825 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9826 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9827 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9828 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9829 SCTP_BUF_LEN(chk->data) = chk->send_size;
9832 * Move pointer to after the fwdtsn and transfer to the
9835 strseq = (struct sctp_strseq *)fwdtsn;
9837 * Now populate the strseq list. This is done blindly
9838 * without pulling out duplicate stream info. This is
9839 * inefficent but won't harm the process since the peer will
9840 * look at these in sequence and will thus release anything.
9841 * It could mean we exceed the PMTU and chop off some that
9842 * we could have included.. but this is unlikely (aka 1432/4
9843 * would mean 300+ stream seq's would have to be reported in
9844 * one FWD-TSN. With a bit of work we can later FIX this to
9845 * optimize and pull out duplcates.. but it does add more
9846 * overhead. So for now... not!
9848 at = TAILQ_FIRST(&asoc->sent_queue);
9849 for (i = 0; i < cnt_of_skipped; i++) {
9850 tp1 = TAILQ_NEXT(at, sctp_next);
9851 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9852 /* We don't report these */
9857 if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9858 at->rec.data.fwd_tsn_cnt = 0;
9860 strseq->stream = ntohs(at->rec.data.stream_number);
9861 strseq->sequence = ntohs(at->rec.data.stream_seq);
9871 sctp_send_sack(struct sctp_tcb *stcb)
9874 * Queue up a SACK in the control queue. We must first check to see
9875 * if a SACK is somehow on the control queue. If so, we will take
9876 * and and remove the old one.
9878 struct sctp_association *asoc;
9879 struct sctp_tmit_chunk *chk, *a_chk;
9880 struct sctp_sack_chunk *sack;
9881 struct sctp_gap_ack_block *gap_descriptor;
9882 struct sack_track *selector;
9887 int limit_reached = 0;
9888 unsigned int i, jstart, siz, j;
9889 unsigned int num_gap_blocks = 0, space;
9895 SCTP_TCB_LOCK_ASSERT(stcb);
9896 if (asoc->last_data_chunk_from == NULL) {
9897 /* Hmm we never received anything */
9900 sctp_set_rwnd(stcb, asoc);
9901 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9902 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9903 /* Hmm, found a sack already on queue, remove it */
9904 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9905 asoc->ctrl_queue_cnt++;
9908 sctp_m_freem(a_chk->data);
9911 sctp_free_remote_addr(a_chk->whoTo);
9912 a_chk->whoTo = NULL;
9916 if (a_chk == NULL) {
9917 sctp_alloc_a_chunk(stcb, a_chk);
9918 if (a_chk == NULL) {
9919 /* No memory so we drop the idea, and set a timer */
9920 if (stcb->asoc.delayed_ack) {
9921 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9922 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9923 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9924 stcb->sctp_ep, stcb, NULL);
9926 stcb->asoc.send_sack = 1;
9930 a_chk->copy_by_ref = 0;
9931 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9932 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9933 a_chk->rec.chunk_id.can_take_data = 1;
9935 /* Clear our pkt counts */
9936 asoc->data_pkts_seen = 0;
9939 a_chk->snd_count = 0;
9940 a_chk->send_size = 0; /* fill in later */
9941 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9942 a_chk->whoTo = NULL;
9944 if ((asoc->numduptsns) ||
9945 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9948 * Ok, we have some duplicates or the destination for the
9949 * sack is unreachable, lets see if we can select an
9950 * alternate than asoc->last_data_chunk_from
9952 if ((!(asoc->last_data_chunk_from->dest_state &
9953 SCTP_ADDR_NOT_REACHABLE)) &&
9954 (asoc->used_alt_onsack > asoc->numnets)) {
9955 /* We used an alt last time, don't this time */
9956 a_chk->whoTo = NULL;
9958 asoc->used_alt_onsack++;
9959 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9961 if (a_chk->whoTo == NULL) {
9962 /* Nope, no alternate */
9963 a_chk->whoTo = asoc->last_data_chunk_from;
9964 asoc->used_alt_onsack = 0;
9968 * No duplicates so we use the last place we received data
9971 asoc->used_alt_onsack = 0;
9972 a_chk->whoTo = asoc->last_data_chunk_from;
9975 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9977 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9979 space_req = sizeof(struct sctp_sack_chunk);
9981 /* gaps get a cluster */
9982 space_req = MCLBYTES;
9984 /* Ok now lets formulate a MBUF with our sack */
9985 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9986 if ((a_chk->data == NULL) ||
9987 (a_chk->whoTo == NULL)) {
9988 /* rats, no mbuf memory */
9990 /* was a problem with the destination */
9991 sctp_m_freem(a_chk->data);
9994 sctp_free_a_chunk(stcb, a_chk);
9995 /* sa_ignore NO_NULL_CHK */
9996 if (stcb->asoc.delayed_ack) {
9997 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9998 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9999 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10000 stcb->sctp_ep, stcb, NULL);
10002 stcb->asoc.send_sack = 1;
10006 /* ok, lets go through and fill it in */
10007 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10008 space = M_TRAILINGSPACE(a_chk->data);
10009 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10010 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10012 limit = mtod(a_chk->data, caddr_t);
10015 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10016 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
10017 /* 0x01 is used by nonce for ecn */
10018 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10019 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10020 (asoc->peer_supports_ecn_nonce))
10021 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10023 sack->ch.chunk_flags = 0;
10025 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10027 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10028 * received, then set high bit to 1, else 0. Reset
10031 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10032 asoc->cmt_dac_pkts_rcvd = 0;
10034 #ifdef SCTP_ASOCLOG_OF_TSNS
10035 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10036 stcb->asoc.cumack_log_atsnt++;
10037 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10038 stcb->asoc.cumack_log_atsnt = 0;
10041 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10042 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10043 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10045 /* reset the readers interpretation */
10046 stcb->freed_by_sorcv_sincelast = 0;
10048 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10050 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10051 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10053 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10055 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10058 * cum-ack behind the mapping array, so we start and use all
10063 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10065 * we skip the first one when the cum-ack is at or above the
10066 * mapping array base. Note this only works if
10070 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10071 /* we have a gap .. maybe */
10072 for (i = 0; i < siz; i++) {
10073 selector = &sack_array[asoc->mapping_array[i]];
10074 if (mergeable && selector->right_edge) {
10076 * Backup, left and right edges were ok to
10082 if (selector->num_entries == 0)
10085 for (j = jstart; j < selector->num_entries; j++) {
10086 if (mergeable && selector->right_edge) {
10088 * do a merge by NOT setting
10094 * no merge, set the left
10098 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10100 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10103 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10109 if (selector->left_edge) {
10113 if (limit_reached) {
10114 /* Reached the limit stop */
10120 if (num_gap_blocks == 0) {
10122 * slide not yet happened, and somehow we got called
10123 * to send a sack. Cumack needs to move up.
10125 int abort_flag = 0;
10127 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10128 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10129 sctp_sack_check(stcb, 0, 0, &abort_flag);
10132 /* now we must add any dups we are going to report. */
10133 if ((limit_reached == 0) && (asoc->numduptsns)) {
10134 dup = (uint32_t *) gap_descriptor;
10135 for (i = 0; i < asoc->numduptsns; i++) {
10136 *dup = htonl(asoc->dup_tsns[i]);
10139 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10144 asoc->numduptsns = 0;
10147 * now that the chunk is prepared queue it to the control chunk
10150 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10151 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10152 (num_dups * sizeof(int32_t)));
10153 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10154 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10155 sack->sack.num_dup_tsns = htons(num_dups);
10156 sack->ch.chunk_length = htons(a_chk->send_size);
10157 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10158 asoc->ctrl_queue_cnt++;
10159 asoc->send_sack = 0;
10160 SCTP_STAT_INCR(sctps_sendsacks);
10164 /* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
10166 sctp_send_nr_sack(struct sctp_tcb *stcb)
10169 * Queue up an NR-SACK in the control queue. We must first check to see
10170 * if an NR-SACK is somehow on the control queue. If so, we will take
10171 * and and remove the old one.
10173 struct sctp_association *asoc;
10174 struct sctp_tmit_chunk *chk, *a_chk;
10176 struct sctp_nr_sack_chunk *nr_sack;
10178 struct sctp_gap_ack_block *gap_descriptor;
10179 struct sctp_nr_gap_ack_block *nr_gap_descriptor;
10181 struct sack_track *selector;
10182 struct sack_track *nr_selector;
10184 /* EY do we need nr_mergeable, NO */
10189 int limit_reached = 0;
10190 unsigned int i, jstart, siz, j;
10191 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10194 unsigned int reserved = 0;
10197 asoc = &stcb->asoc;
10198 SCTP_TCB_LOCK_ASSERT(stcb);
10199 if (asoc->last_data_chunk_from == NULL) {
10200 /* Hmm we never received anything */
10203 sctp_set_rwnd(stcb, asoc);
10204 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10205 if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
10206 /* Hmm, found a sack already on queue, remove it */
10207 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10208 asoc->ctrl_queue_cnt++;
10211 sctp_m_freem(a_chk->data);
10212 a_chk->data = NULL;
10214 sctp_free_remote_addr(a_chk->whoTo);
10215 a_chk->whoTo = NULL;
10219 if (a_chk == NULL) {
10220 sctp_alloc_a_chunk(stcb, a_chk);
10221 if (a_chk == NULL) {
10222 /* No memory so we drop the idea, and set a timer */
10223 if (stcb->asoc.delayed_ack) {
10224 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10225 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10226 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10227 stcb->sctp_ep, stcb, NULL);
10229 stcb->asoc.send_sack = 1;
10233 a_chk->copy_by_ref = 0;
10234 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
10235 a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
10236 a_chk->rec.chunk_id.can_take_data = 1;
10238 /* Clear our pkt counts */
10239 asoc->data_pkts_seen = 0;
10241 a_chk->asoc = asoc;
10242 a_chk->snd_count = 0;
10243 a_chk->send_size = 0; /* fill in later */
10244 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10245 a_chk->whoTo = NULL;
10247 if ((asoc->numduptsns) ||
10248 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
10251 * Ok, we have some duplicates or the destination for the
10252 * sack is unreachable, lets see if we can select an
10253 * alternate than asoc->last_data_chunk_from
10255 if ((!(asoc->last_data_chunk_from->dest_state &
10256 SCTP_ADDR_NOT_REACHABLE)) &&
10257 (asoc->used_alt_onsack > asoc->numnets)) {
10258 /* We used an alt last time, don't this time */
10259 a_chk->whoTo = NULL;
10261 asoc->used_alt_onsack++;
10262 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10264 if (a_chk->whoTo == NULL) {
10265 /* Nope, no alternate */
10266 a_chk->whoTo = asoc->last_data_chunk_from;
10267 asoc->used_alt_onsack = 0;
10271 * No duplicates so we use the last place we received data
10274 asoc->used_alt_onsack = 0;
10275 a_chk->whoTo = asoc->last_data_chunk_from;
10277 if (a_chk->whoTo) {
10278 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10280 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
10282 space_req = sizeof(struct sctp_nr_sack_chunk);
10284 /* EY - what is this about? */
10285 /* gaps get a cluster */
10286 space_req = MCLBYTES;
10288 /* Ok now lets formulate a MBUF with our sack */
10289 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10290 if ((a_chk->data == NULL) ||
10291 (a_chk->whoTo == NULL)) {
10292 /* rats, no mbuf memory */
10294 /* was a problem with the destination */
10295 sctp_m_freem(a_chk->data);
10296 a_chk->data = NULL;
10298 sctp_free_a_chunk(stcb, a_chk);
10299 /* sa_ignore NO_NULL_CHK */
10300 if (stcb->asoc.delayed_ack) {
10301 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10302 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10303 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10304 stcb->sctp_ep, stcb, NULL);
10306 stcb->asoc.send_sack = 1;
10310 /* ok, lets go through and fill it in */
10311 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10312 space = M_TRAILINGSPACE(a_chk->data);
10313 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10314 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10316 limit = mtod(a_chk->data, caddr_t);
10319 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10320 nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
10322 /* 0x01 is used by nonce for ecn */
10323 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10324 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10325 (asoc->peer_supports_ecn_nonce))
10326 nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10328 nr_sack->ch.chunk_flags = 0;
10330 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10332 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10333 * received, then set high bit to 1, else 0. Reset
10336 /* EY - TODO: which chunk flag is used in here? -The LSB */
10337 nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10338 asoc->cmt_dac_pkts_rcvd = 0;
10340 #ifdef SCTP_ASOCLOG_OF_TSNS
10341 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10342 stcb->asoc.cumack_log_atsnt++;
10343 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10344 stcb->asoc.cumack_log_atsnt = 0;
10347 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10348 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10349 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10351 /* reset the readers interpretation */
10352 stcb->freed_by_sorcv_sincelast = 0;
10354 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10356 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn)
10357 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10359 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10361 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10364 * cum-ack behind the mapping array, so we start and use all
10369 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10371 * we skip the first one when the cum-ack is at or above the
10372 * mapping array base. Note this only works if
10376 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
10377 /* we have a gap .. maybe */
10378 for (i = 0; i < siz; i++) {
10379 selector = &sack_array[asoc->mapping_array[i]];
10380 if (mergeable && selector->right_edge) {
10382 * Backup, left and right edges were ok to
10388 if (selector->num_entries == 0)
10391 for (j = jstart; j < selector->num_entries; j++) {
10392 if (mergeable && selector->right_edge) {
10394 * do a merge by NOT setting
10400 * no merge, set the left
10404 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10406 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10409 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10415 if (selector->left_edge) {
10419 if (limit_reached) {
10420 /* Reached the limit stop */
10426 if (num_gap_blocks == 0) {
10428 * slide not yet happened, and somehow we got called
10429 * to send a sack. Cumack needs to move up.
10431 int abort_flag = 0;
10433 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10434 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10435 sctp_sack_check(stcb, 0, 0, &abort_flag);
10438 /*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
10440 nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
10442 /* EY - there will be gaps + nr_gaps if draining is possible */
10443 if ((SCTP_BASE_SYSCTL(sctp_do_drain)) && (limit_reached == 0)) {
10447 if (asoc->highest_tsn_inside_nr_map > asoc->nr_mapping_array_base_tsn)
10448 siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
10450 siz = (((MAX_TSN - asoc->nr_mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10452 if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10455 * cum-ack behind the mapping array, so we start and use all
10460 offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
10462 * we skip the first one when the cum-ack is at or above the
10463 * mapping array base. Note this only works if
10467 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10468 /* we have a gap .. maybe */
10469 for (i = 0; i < siz; i++) {
10470 nr_selector = &sack_array[asoc->nr_mapping_array[i]];
10471 if (mergeable && nr_selector->right_edge) {
10473 * Backup, left and right edges were
10476 num_nr_gap_blocks--;
10477 nr_gap_descriptor--;
10479 if (nr_selector->num_entries == 0)
10482 for (j = jstart; j < nr_selector->num_entries; j++) {
10483 if (mergeable && nr_selector->right_edge) {
10485 * do a merge by NOT
10492 * no merge, set the
10496 nr_gap_descriptor->start = htons((nr_selector->gaps[j].start + offset));
10498 nr_gap_descriptor->end = htons((nr_selector->gaps[j].end + offset));
10499 num_nr_gap_blocks++;
10500 nr_gap_descriptor++;
10501 if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
10507 if (nr_selector->left_edge) {
10511 if (limit_reached) {
10512 /* Reached the limit stop */
10520 /*---------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
10522 /* now we must add any dups we are going to report. */
10523 if ((limit_reached == 0) && (asoc->numduptsns)) {
10524 dup = (uint32_t *) nr_gap_descriptor;
10525 for (i = 0; i < asoc->numduptsns; i++) {
10526 *dup = htonl(asoc->dup_tsns[i]);
10529 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10534 asoc->numduptsns = 0;
10537 * now that the chunk is prepared queue it to the control chunk
10540 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
10541 num_nr_gap_blocks = num_gap_blocks;
10542 num_gap_blocks = 0;
10544 a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
10545 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10546 (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
10547 (num_dups * sizeof(int32_t)));
10549 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10550 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10551 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10552 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10553 nr_sack->nr_sack.reserved = htons(reserved);
10554 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10555 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10556 asoc->ctrl_queue_cnt++;
10557 asoc->send_sack = 0;
10558 SCTP_STAT_INCR(sctps_sendsacks);
10563 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10564 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10569 struct mbuf *m_abort;
10570 struct mbuf *m_out = NULL, *m_end = NULL;
10571 struct sctp_abort_chunk *abort = NULL;
10573 uint32_t auth_offset = 0;
10574 struct sctp_auth_chunk *auth = NULL;
10577 * Add an AUTH chunk, if chunk requires it and save the offset into
10578 * the chain for AUTH
10580 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10581 stcb->asoc.peer_auth_chunks)) {
10582 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10583 stcb, SCTP_ABORT_ASSOCIATION);
10584 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10586 SCTP_TCB_LOCK_ASSERT(stcb);
10587 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10588 if (m_abort == NULL) {
10591 sctp_m_freem(m_out);
10594 /* link in any error */
10595 SCTP_BUF_NEXT(m_abort) = operr;
10602 sz += SCTP_BUF_LEN(n);
10603 n = SCTP_BUF_NEXT(n);
10606 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10607 if (m_out == NULL) {
10608 /* NO Auth chunk prepended, so reserve space in front */
10609 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10612 /* Put AUTH chunk at the front of the chain */
10613 SCTP_BUF_NEXT(m_end) = m_abort;
10616 /* fill in the ABORT chunk */
10617 abort = mtod(m_abort, struct sctp_abort_chunk *);
10618 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10619 abort->ch.chunk_flags = 0;
10620 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10622 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10623 stcb->asoc.primary_destination,
10624 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10625 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10626 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10627 stcb->asoc.primary_destination->port, so_locked, NULL);
10628 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10632 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10633 struct sctp_nets *net)
10635 /* formulate and SEND a SHUTDOWN-COMPLETE */
10636 struct mbuf *m_shutdown_comp;
10637 struct sctp_shutdown_complete_chunk *shutdown_complete;
10639 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10640 if (m_shutdown_comp == NULL) {
10644 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10645 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10646 shutdown_complete->ch.chunk_flags = 0;
10647 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10648 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10649 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10650 (struct sockaddr *)&net->ro._l_addr,
10651 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10652 stcb->sctp_ep->sctp_lport, stcb->rport,
10653 htonl(stcb->asoc.peer_vtag),
10654 net->port, SCTP_SO_NOT_LOCKED, NULL);
10655 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10660 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10661 uint32_t vrf_id, uint16_t port)
10663 /* formulate and SEND a SHUTDOWN-COMPLETE */
10664 struct mbuf *o_pak;
10666 struct ip *iph, *iph_out;
10667 struct udphdr *udp = NULL;
10670 struct ip6_hdr *ip6, *ip6_out;
10673 int offset_out, len, mlen;
10674 struct sctp_shutdown_complete_msg *comp_cp;
10676 iph = mtod(m, struct ip *);
10677 switch (iph->ip_v) {
10679 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10682 case IPV6_VERSION >> 4:
10683 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10690 len += sizeof(struct udphdr);
10692 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10693 if (mout == NULL) {
10696 SCTP_BUF_RESV_UF(mout, max_linkhdr);
10697 SCTP_BUF_LEN(mout) = len;
10698 SCTP_BUF_NEXT(mout) = NULL;
10705 switch (iph->ip_v) {
10707 iph_out = mtod(mout, struct ip *);
10709 /* Fill in the IP header for the ABORT */
10710 iph_out->ip_v = IPVERSION;
10711 iph_out->ip_hl = (sizeof(struct ip) / 4);
10712 iph_out->ip_tos = (u_char)0;
10713 iph_out->ip_id = 0;
10714 iph_out->ip_off = 0;
10715 iph_out->ip_ttl = MAXTTL;
10717 iph_out->ip_p = IPPROTO_UDP;
10719 iph_out->ip_p = IPPROTO_SCTP;
10721 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10722 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10724 /* let IP layer calculate this */
10725 iph_out->ip_sum = 0;
10726 offset_out += sizeof(*iph_out);
10727 comp_cp = (struct sctp_shutdown_complete_msg *)(
10728 (caddr_t)iph_out + offset_out);
10731 case IPV6_VERSION >> 4:
10732 ip6 = (struct ip6_hdr *)iph;
10733 ip6_out = mtod(mout, struct ip6_hdr *);
10735 /* Fill in the IPv6 header for the ABORT */
10736 ip6_out->ip6_flow = ip6->ip6_flow;
10737 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10739 ip6_out->ip6_nxt = IPPROTO_UDP;
10741 ip6_out->ip6_nxt = IPPROTO_SCTP;
10743 ip6_out->ip6_src = ip6->ip6_dst;
10744 ip6_out->ip6_dst = ip6->ip6_src;
10746 * ?? The old code had both the iph len + payload, I think
10747 * this is wrong and would never have worked
10749 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10750 offset_out += sizeof(*ip6_out);
10751 comp_cp = (struct sctp_shutdown_complete_msg *)(
10752 (caddr_t)ip6_out + offset_out);
10756 /* Currently not supported. */
10757 sctp_m_freem(mout);
10761 udp = (struct udphdr *)comp_cp;
10762 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10763 udp->uh_dport = port;
10764 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10765 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10766 offset_out += sizeof(struct udphdr);
10767 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10769 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10771 sctp_m_freem(mout);
10774 /* Now copy in and fill in the ABORT tags etc. */
10775 comp_cp->sh.src_port = sh->dest_port;
10776 comp_cp->sh.dest_port = sh->src_port;
10777 comp_cp->sh.checksum = 0;
10778 comp_cp->sh.v_tag = sh->v_tag;
10779 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10780 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10781 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10783 if (iph_out != NULL) {
10786 struct sctp_tcb *stcb = NULL;
10788 mlen = SCTP_BUF_LEN(mout);
10789 bzero(&ro, sizeof ro);
10790 /* set IPv4 length */
10791 iph_out->ip_len = mlen;
10792 #ifdef SCTP_PACKET_LOGGING
10793 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10794 sctp_packet_log(mout, mlen);
10797 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10798 SCTP_STAT_INCR(sctps_sendswcrc);
10799 SCTP_ENABLE_UDP_CSUM(mout);
10801 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10802 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
10803 SCTP_STAT_INCR(sctps_sendhwcrc);
10805 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10807 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10809 /* Free the route if we got one back */
10814 if (ip6_out != NULL) {
10815 struct route_in6 ro;
10817 struct sctp_tcb *stcb = NULL;
10818 struct ifnet *ifp = NULL;
10820 bzero(&ro, sizeof(ro));
10821 mlen = SCTP_BUF_LEN(mout);
10822 #ifdef SCTP_PACKET_LOGGING
10823 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10824 sctp_packet_log(mout, mlen);
10826 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10827 SCTP_STAT_INCR(sctps_sendswcrc);
10828 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10830 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10831 sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10832 udp->uh_sum = 0xffff;
10835 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10837 /* Free the route if we got one back */
10842 SCTP_STAT_INCR(sctps_sendpackets);
10843 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10844 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10849 static struct sctp_nets *
10850 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10852 struct sctp_nets *net, *hnet;
10853 int ms_goneby, highest_ms, state_overide = 0;
10855 (void)SCTP_GETTIME_TIMEVAL(now);
10858 SCTP_TCB_LOCK_ASSERT(stcb);
10859 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10861 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10862 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10865 * Skip this guy from consideration if HB is off AND
10870 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10871 /* skip this dest net from consideration */
10874 if (net->last_sent_time.tv_sec) {
10875 /* Sent to so we subtract */
10876 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10878 /* Never been sent to */
10879 ms_goneby = 0x7fffffff;
10881 * When the address state is unconfirmed but still
10882 * considered reachable, we HB at a higher rate. Once it
10883 * goes confirmed OR reaches the "unreachable" state, thenw
10884 * we cut it back to HB at a more normal pace.
10886 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10892 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10893 (ms_goneby > highest_ms)) {
10894 highest_ms = ms_goneby;
10899 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10905 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10907 * Found the one with longest delay bounds OR it is
10908 * unconfirmed and still not marked unreachable.
10910 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10913 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10914 (struct sockaddr *)&hnet->ro._l_addr);
10916 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10919 /* update the timer now */
10920 hnet->last_sent_time = *now;
10923 /* Nothing to HB */
10928 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10930 struct sctp_tmit_chunk *chk;
10931 struct sctp_nets *net;
10932 struct sctp_heartbeat_chunk *hb;
10933 struct timeval now;
10934 struct sockaddr_in *sin;
10935 struct sockaddr_in6 *sin6;
10937 SCTP_TCB_LOCK_ASSERT(stcb);
10938 if (user_req == 0) {
10939 net = sctp_select_hb_destination(stcb, &now);
10942 * All our busy none to send to, just start the
10945 if (stcb->asoc.state == 0) {
10948 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10959 (void)SCTP_GETTIME_TIMEVAL(&now);
10961 sin = (struct sockaddr_in *)&net->ro._l_addr;
10962 if (sin->sin_family != AF_INET) {
10963 if (sin->sin_family != AF_INET6) {
10968 sctp_alloc_a_chunk(stcb, chk);
10970 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10973 chk->copy_by_ref = 0;
10974 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10975 chk->rec.chunk_id.can_take_data = 1;
10976 chk->asoc = &stcb->asoc;
10977 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10979 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10980 if (chk->data == NULL) {
10981 sctp_free_a_chunk(stcb, chk);
10984 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10985 SCTP_BUF_LEN(chk->data) = chk->send_size;
10986 chk->sent = SCTP_DATAGRAM_UNSENT;
10987 chk->snd_count = 0;
10989 atomic_add_int(&chk->whoTo->ref_count, 1);
10990 /* Now we have a mbuf that we can fill in with the details */
10991 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10992 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10993 /* fill out chunk header */
10994 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10995 hb->ch.chunk_flags = 0;
10996 hb->ch.chunk_length = htons(chk->send_size);
10997 /* Fill out hb parameter */
10998 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10999 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11000 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11001 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11002 /* Did our user request this one, put it in */
11003 hb->heartbeat.hb_info.user_req = user_req;
11004 hb->heartbeat.hb_info.addr_family = sin->sin_family;
11005 hb->heartbeat.hb_info.addr_len = sin->sin_len;
11006 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11008 * we only take from the entropy pool if the address is not
11011 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11012 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11014 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11015 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11017 if (sin->sin_family == AF_INET) {
11018 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
11019 } else if (sin->sin_family == AF_INET6) {
11020 /* We leave the scope the way it is in our lookup table. */
11021 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
11022 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
11024 /* huh compiler bug */
11029 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
11030 * PF-heartbeats. Because of this, threshold management is done by
11031 * the t3 timer handler, and does not need to be done upon the send
11032 * of a PF-heartbeat. If CMT PF is on and the destination to which a
11033 * heartbeat is being sent is in PF state, do NOT do threshold
11036 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
11037 /* ok we have a destination that needs a beat */
11038 /* lets do the theshold management Qiaobing style */
11039 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
11040 stcb->asoc.max_send_times)) {
11042 * we have lost the association, in a way this is
11043 * quite bad since we really are one less time since
11044 * we really did not send yet. This is the down side
11045 * to the Q's style as defined in the RFC and not my
11046 * alternate style defined in the RFC.
11048 if (chk->data != NULL) {
11049 sctp_m_freem(chk->data);
11053 * Here we do NOT use the macro since the
11054 * association is now gone.
11057 sctp_free_remote_addr(chk->whoTo);
11060 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
11064 net->hb_responded = 0;
11065 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11066 stcb->asoc.ctrl_queue_cnt++;
11067 SCTP_STAT_INCR(sctps_sendheartbeat);
11069 * Call directly med level routine to put out the chunk. It will
11070 * always tumble out control chunks aka HB but it may even tumble
11077 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11080 struct sctp_association *asoc;
11081 struct sctp_ecne_chunk *ecne;
11082 struct sctp_tmit_chunk *chk;
11084 asoc = &stcb->asoc;
11085 SCTP_TCB_LOCK_ASSERT(stcb);
11086 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11087 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
11088 /* found a previous ECN_ECHO update it if needed */
11089 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11090 ecne->tsn = htonl(high_tsn);
11094 /* nope could not find one to update so we must build one */
11095 sctp_alloc_a_chunk(stcb, chk);
11099 chk->copy_by_ref = 0;
11100 SCTP_STAT_INCR(sctps_sendecne);
11101 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11102 chk->rec.chunk_id.can_take_data = 0;
11103 chk->asoc = &stcb->asoc;
11104 chk->send_size = sizeof(struct sctp_ecne_chunk);
11105 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11106 if (chk->data == NULL) {
11107 sctp_free_a_chunk(stcb, chk);
11110 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11111 SCTP_BUF_LEN(chk->data) = chk->send_size;
11112 chk->sent = SCTP_DATAGRAM_UNSENT;
11113 chk->snd_count = 0;
11115 atomic_add_int(&chk->whoTo->ref_count, 1);
11116 stcb->asoc.ecn_echo_cnt_onq++;
11117 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11118 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11119 ecne->ch.chunk_flags = 0;
11120 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11121 ecne->tsn = htonl(high_tsn);
11122 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11123 asoc->ctrl_queue_cnt++;
11127 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11128 struct mbuf *m, int iphlen, int bad_crc)
11130 struct sctp_association *asoc;
11131 struct sctp_pktdrop_chunk *drp;
11132 struct sctp_tmit_chunk *chk;
11139 struct ip6_hdr *ip6h;
11142 int fullsz = 0, extra = 0;
11145 struct sctp_chunkhdr *ch, chunk_buf;
11146 unsigned int chk_length;
11151 asoc = &stcb->asoc;
11152 SCTP_TCB_LOCK_ASSERT(stcb);
11153 if (asoc->peer_supports_pktdrop == 0) {
11155 * peer must declare support before I send one.
11159 if (stcb->sctp_socket == NULL) {
11162 sctp_alloc_a_chunk(stcb, chk);
11166 chk->copy_by_ref = 0;
11167 iph = mtod(m, struct ip *);
11169 sctp_free_a_chunk(stcb, chk);
11172 switch (iph->ip_v) {
11175 len = chk->send_size = iph->ip_len;
11178 case IPV6_VERSION >> 4:
11180 ip6h = mtod(m, struct ip6_hdr *);
11181 len = chk->send_size = htons(ip6h->ip6_plen);
11187 /* Validate that we do not have an ABORT in here. */
11188 offset = iphlen + sizeof(struct sctphdr);
11189 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11190 sizeof(*ch), (uint8_t *) & chunk_buf);
11191 while (ch != NULL) {
11192 chk_length = ntohs(ch->chunk_length);
11193 if (chk_length < sizeof(*ch)) {
11194 /* break to abort land */
11197 switch (ch->chunk_type) {
11198 case SCTP_PACKET_DROPPED:
11199 case SCTP_ABORT_ASSOCIATION:
11201 * we don't respond with an PKT-DROP to an ABORT
11204 sctp_free_a_chunk(stcb, chk);
11209 offset += SCTP_SIZE32(chk_length);
11210 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11211 sizeof(*ch), (uint8_t *) & chunk_buf);
11214 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11215 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11217 * only send 1 mtu worth, trim off the excess on the end.
11219 fullsz = len - extra;
11220 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11223 chk->asoc = &stcb->asoc;
11224 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11225 if (chk->data == NULL) {
11227 sctp_free_a_chunk(stcb, chk);
11230 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11231 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11233 sctp_m_freem(chk->data);
11237 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11238 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11239 chk->book_size_scale = 0;
11241 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11242 drp->trunc_len = htons(fullsz);
11244 * Len is already adjusted to size minus overhead above take
11245 * out the pkt_drop chunk itself from it.
11247 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11248 len = chk->send_size;
11250 /* no truncation needed */
11251 drp->ch.chunk_flags = 0;
11252 drp->trunc_len = htons(0);
11255 drp->ch.chunk_flags |= SCTP_BADCRC;
11257 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11258 SCTP_BUF_LEN(chk->data) = chk->send_size;
11259 chk->sent = SCTP_DATAGRAM_UNSENT;
11260 chk->snd_count = 0;
11262 /* we should hit here */
11265 chk->whoTo = asoc->primary_destination;
11267 atomic_add_int(&chk->whoTo->ref_count, 1);
11268 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11269 chk->rec.chunk_id.can_take_data = 1;
11270 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11271 drp->ch.chunk_length = htons(chk->send_size);
11272 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11276 drp->bottle_bw = htonl(spc);
11277 if (asoc->my_rwnd) {
11278 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11279 asoc->size_on_all_streams +
11280 asoc->my_rwnd_control_len +
11281 stcb->sctp_socket->so_rcv.sb_cc);
11284 * If my rwnd is 0, possibly from mbuf depletion as well as
11285 * space used, tell the peer there is NO space aka onq == bw
11287 drp->current_onq = htonl(spc);
11291 m_copydata(m, iphlen, len, (caddr_t)datap);
11292 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11293 asoc->ctrl_queue_cnt++;
11297 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11299 struct sctp_association *asoc;
11300 struct sctp_cwr_chunk *cwr;
11301 struct sctp_tmit_chunk *chk;
11303 asoc = &stcb->asoc;
11304 SCTP_TCB_LOCK_ASSERT(stcb);
11305 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11306 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11307 /* found a previous ECN_CWR update it if needed */
11308 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11309 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11311 cwr->tsn = htonl(high_tsn);
11316 /* nope could not find one to update so we must build one */
11317 sctp_alloc_a_chunk(stcb, chk);
11321 chk->copy_by_ref = 0;
11322 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11323 chk->rec.chunk_id.can_take_data = 1;
11324 chk->asoc = &stcb->asoc;
11325 chk->send_size = sizeof(struct sctp_cwr_chunk);
11326 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11327 if (chk->data == NULL) {
11328 sctp_free_a_chunk(stcb, chk);
11331 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11332 SCTP_BUF_LEN(chk->data) = chk->send_size;
11333 chk->sent = SCTP_DATAGRAM_UNSENT;
11334 chk->snd_count = 0;
11336 atomic_add_int(&chk->whoTo->ref_count, 1);
11337 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11338 cwr->ch.chunk_type = SCTP_ECN_CWR;
11339 cwr->ch.chunk_flags = 0;
11340 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11341 cwr->tsn = htonl(high_tsn);
11342 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11343 asoc->ctrl_queue_cnt++;
11347 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11348 int number_entries, uint16_t * list,
11349 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11351 int len, old_len, i;
11352 struct sctp_stream_reset_out_request *req_out;
11353 struct sctp_chunkhdr *ch;
11355 ch = mtod(chk->data, struct sctp_chunkhdr *);
11358 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11360 /* get to new offset for the param. */
11361 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11362 /* now how long will this param be? */
11363 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11364 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11365 req_out->ph.param_length = htons(len);
11366 req_out->request_seq = htonl(seq);
11367 req_out->response_seq = htonl(resp_seq);
11368 req_out->send_reset_at_tsn = htonl(last_sent);
11369 if (number_entries) {
11370 for (i = 0; i < number_entries; i++) {
11371 req_out->list_of_streams[i] = htons(list[i]);
11374 if (SCTP_SIZE32(len) > len) {
11376 * Need to worry about the pad we may end up adding to the
11377 * end. This is easy since the struct is either aligned to 4
11378 * bytes or 2 bytes off.
11380 req_out->list_of_streams[number_entries] = 0;
11382 /* now fix the chunk length */
11383 ch->chunk_length = htons(len + old_len);
11384 chk->book_size = len + old_len;
11385 chk->book_size_scale = 0;
11386 chk->send_size = SCTP_SIZE32(chk->book_size);
11387 SCTP_BUF_LEN(chk->data) = chk->send_size;
11393 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11394 int number_entries, uint16_t * list,
11397 int len, old_len, i;
11398 struct sctp_stream_reset_in_request *req_in;
11399 struct sctp_chunkhdr *ch;
11401 ch = mtod(chk->data, struct sctp_chunkhdr *);
11404 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11406 /* get to new offset for the param. */
11407 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11408 /* now how long will this param be? */
11409 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11410 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11411 req_in->ph.param_length = htons(len);
11412 req_in->request_seq = htonl(seq);
11413 if (number_entries) {
11414 for (i = 0; i < number_entries; i++) {
11415 req_in->list_of_streams[i] = htons(list[i]);
11418 if (SCTP_SIZE32(len) > len) {
11420 * Need to worry about the pad we may end up adding to the
11421 * end. This is easy since the struct is either aligned to 4
11422 * bytes or 2 bytes off.
11424 req_in->list_of_streams[number_entries] = 0;
11426 /* now fix the chunk length */
11427 ch->chunk_length = htons(len + old_len);
11428 chk->book_size = len + old_len;
11429 chk->book_size_scale = 0;
11430 chk->send_size = SCTP_SIZE32(chk->book_size);
11431 SCTP_BUF_LEN(chk->data) = chk->send_size;
11437 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11441 struct sctp_stream_reset_tsn_request *req_tsn;
11442 struct sctp_chunkhdr *ch;
11444 ch = mtod(chk->data, struct sctp_chunkhdr *);
11447 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11449 /* get to new offset for the param. */
11450 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11451 /* now how long will this param be? */
11452 len = sizeof(struct sctp_stream_reset_tsn_request);
11453 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11454 req_tsn->ph.param_length = htons(len);
11455 req_tsn->request_seq = htonl(seq);
11457 /* now fix the chunk length */
11458 ch->chunk_length = htons(len + old_len);
11459 chk->send_size = len + old_len;
11460 chk->book_size = SCTP_SIZE32(chk->send_size);
11461 chk->book_size_scale = 0;
11462 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11467 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11468 uint32_t resp_seq, uint32_t result)
11471 struct sctp_stream_reset_response *resp;
11472 struct sctp_chunkhdr *ch;
11474 ch = mtod(chk->data, struct sctp_chunkhdr *);
11477 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11479 /* get to new offset for the param. */
11480 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11481 /* now how long will this param be? */
11482 len = sizeof(struct sctp_stream_reset_response);
11483 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11484 resp->ph.param_length = htons(len);
11485 resp->response_seq = htonl(resp_seq);
11486 resp->result = ntohl(result);
11488 /* now fix the chunk length */
11489 ch->chunk_length = htons(len + old_len);
11490 chk->book_size = len + old_len;
11491 chk->book_size_scale = 0;
11492 chk->send_size = SCTP_SIZE32(chk->book_size);
11493 SCTP_BUF_LEN(chk->data) = chk->send_size;
11500 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11501 uint32_t resp_seq, uint32_t result,
11502 uint32_t send_una, uint32_t recv_next)
11505 struct sctp_stream_reset_response_tsn *resp;
11506 struct sctp_chunkhdr *ch;
11508 ch = mtod(chk->data, struct sctp_chunkhdr *);
11511 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11513 /* get to new offset for the param. */
11514 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11515 /* now how long will this param be? */
11516 len = sizeof(struct sctp_stream_reset_response_tsn);
11517 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11518 resp->ph.param_length = htons(len);
11519 resp->response_seq = htonl(resp_seq);
11520 resp->result = htonl(result);
11521 resp->senders_next_tsn = htonl(send_una);
11522 resp->receivers_next_tsn = htonl(recv_next);
11524 /* now fix the chunk length */
11525 ch->chunk_length = htons(len + old_len);
11526 chk->book_size = len + old_len;
11527 chk->send_size = SCTP_SIZE32(chk->book_size);
11528 chk->book_size_scale = 0;
11529 SCTP_BUF_LEN(chk->data) = chk->send_size;
11534 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11539 struct sctp_chunkhdr *ch;
11540 struct sctp_stream_reset_add_strm *addstr;
11542 ch = mtod(chk->data, struct sctp_chunkhdr *);
11543 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11545 /* get to new offset for the param. */
11546 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11547 /* now how long will this param be? */
11548 len = sizeof(struct sctp_stream_reset_add_strm);
11551 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11552 addstr->ph.param_length = htons(len);
11553 addstr->request_seq = htonl(seq);
11554 addstr->number_of_streams = htons(adding);
11555 addstr->reserved = 0;
11557 /* now fix the chunk length */
11558 ch->chunk_length = htons(len + old_len);
11559 chk->send_size = len + old_len;
11560 chk->book_size = SCTP_SIZE32(chk->send_size);
11561 chk->book_size_scale = 0;
11562 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11567 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11568 int number_entries, uint16_t * list,
11569 uint8_t send_out_req,
11571 uint8_t send_in_req,
11572 uint8_t send_tsn_req,
11573 uint8_t add_stream,
11578 struct sctp_association *asoc;
11579 struct sctp_tmit_chunk *chk;
11580 struct sctp_chunkhdr *ch;
11583 asoc = &stcb->asoc;
11584 if (asoc->stream_reset_outstanding) {
11586 * Already one pending, must get ACK back to clear the flag.
11588 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11591 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11592 (add_stream == 0)) {
11593 /* nothing to do */
11594 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11597 if (send_tsn_req && (send_out_req || send_in_req)) {
11598 /* error, can't do that */
11599 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11602 sctp_alloc_a_chunk(stcb, chk);
11604 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11607 chk->copy_by_ref = 0;
11608 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11609 chk->rec.chunk_id.can_take_data = 0;
11610 chk->asoc = &stcb->asoc;
11611 chk->book_size = sizeof(struct sctp_chunkhdr);
11612 chk->send_size = SCTP_SIZE32(chk->book_size);
11613 chk->book_size_scale = 0;
11615 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11616 if (chk->data == NULL) {
11617 sctp_free_a_chunk(stcb, chk);
11618 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11621 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11623 /* setup chunk parameters */
11624 chk->sent = SCTP_DATAGRAM_UNSENT;
11625 chk->snd_count = 0;
11626 chk->whoTo = asoc->primary_destination;
11627 atomic_add_int(&chk->whoTo->ref_count, 1);
11629 ch = mtod(chk->data, struct sctp_chunkhdr *);
11630 ch->chunk_type = SCTP_STREAM_RESET;
11631 ch->chunk_flags = 0;
11632 ch->chunk_length = htons(chk->book_size);
11633 SCTP_BUF_LEN(chk->data) = chk->send_size;
11635 seq = stcb->asoc.str_reset_seq_out;
11636 if (send_out_req) {
11637 sctp_add_stream_reset_out(chk, number_entries, list,
11638 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11639 asoc->stream_reset_out_is_outstanding = 1;
11641 asoc->stream_reset_outstanding++;
11644 sctp_add_a_stream(chk, seq, adding);
11646 asoc->stream_reset_outstanding++;
11649 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11650 asoc->stream_reset_outstanding++;
11652 if (send_tsn_req) {
11653 sctp_add_stream_reset_tsn(chk, seq);
11654 asoc->stream_reset_outstanding++;
11656 asoc->str_reset = chk;
11658 /* insert the chunk for sending */
11659 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11662 asoc->ctrl_queue_cnt++;
11663 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11668 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11669 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11672 * Formulate the abort message, and send it back down.
11674 struct mbuf *o_pak;
11676 struct sctp_abort_msg *abm;
11677 struct ip *iph, *iph_out;
11678 struct udphdr *udp;
11681 struct ip6_hdr *ip6, *ip6_out;
11684 int iphlen_out, len;
11686 /* don't respond to ABORT with ABORT */
11687 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11689 sctp_m_freem(err_cause);
11692 iph = mtod(m, struct ip *);
11693 switch (iph->ip_v) {
11695 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11698 case IPV6_VERSION >> 4:
11699 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11704 sctp_m_freem(err_cause);
11709 len += sizeof(struct udphdr);
11711 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11712 if (mout == NULL) {
11714 sctp_m_freem(err_cause);
11718 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11719 SCTP_BUF_LEN(mout) = len;
11720 SCTP_BUF_NEXT(mout) = err_cause;
11725 switch (iph->ip_v) {
11727 iph_out = mtod(mout, struct ip *);
11729 /* Fill in the IP header for the ABORT */
11730 iph_out->ip_v = IPVERSION;
11731 iph_out->ip_hl = (sizeof(struct ip) / 4);
11732 iph_out->ip_tos = (u_char)0;
11733 iph_out->ip_id = 0;
11734 iph_out->ip_off = 0;
11735 iph_out->ip_ttl = MAXTTL;
11737 iph_out->ip_p = IPPROTO_UDP;
11739 iph_out->ip_p = IPPROTO_SCTP;
11741 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11742 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11743 /* let IP layer calculate this */
11744 iph_out->ip_sum = 0;
11746 iphlen_out = sizeof(*iph_out);
11747 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11750 case IPV6_VERSION >> 4:
11751 ip6 = (struct ip6_hdr *)iph;
11752 ip6_out = mtod(mout, struct ip6_hdr *);
11754 /* Fill in the IP6 header for the ABORT */
11755 ip6_out->ip6_flow = ip6->ip6_flow;
11756 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11758 ip6_out->ip6_nxt = IPPROTO_UDP;
11760 ip6_out->ip6_nxt = IPPROTO_SCTP;
11762 ip6_out->ip6_src = ip6->ip6_dst;
11763 ip6_out->ip6_dst = ip6->ip6_src;
11765 iphlen_out = sizeof(*ip6_out);
11766 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11770 /* Currently not supported */
11771 sctp_m_freem(mout);
11775 udp = (struct udphdr *)abm;
11777 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11778 udp->uh_dport = port;
11779 /* set udp->uh_ulen later */
11781 iphlen_out += sizeof(struct udphdr);
11782 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11784 abm->sh.src_port = sh->dest_port;
11785 abm->sh.dest_port = sh->src_port;
11786 abm->sh.checksum = 0;
11788 abm->sh.v_tag = sh->v_tag;
11789 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11791 abm->sh.v_tag = htonl(vtag);
11792 abm->msg.ch.chunk_flags = 0;
11794 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11797 struct mbuf *m_tmp = err_cause;
11800 /* get length of the err_cause chain */
11801 while (m_tmp != NULL) {
11802 err_len += SCTP_BUF_LEN(m_tmp);
11803 m_tmp = SCTP_BUF_NEXT(m_tmp);
11805 len = SCTP_BUF_LEN(mout) + err_len;
11807 /* need pad at end of chunk */
11808 uint32_t cpthis = 0;
11811 padlen = 4 - (len % 4);
11812 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11815 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11817 len = SCTP_BUF_LEN(mout);
11818 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11821 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11823 sctp_m_freem(mout);
11826 if (iph_out != NULL) {
11828 struct sctp_tcb *stcb = NULL;
11831 /* zap the stack pointer to the route */
11832 bzero(&ro, sizeof ro);
11834 udp->uh_ulen = htons(len - sizeof(struct ip));
11835 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11837 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11838 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11839 /* set IPv4 length */
11840 iph_out->ip_len = len;
11842 #ifdef SCTP_PACKET_LOGGING
11843 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11844 sctp_packet_log(mout, len);
11846 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11848 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11849 SCTP_STAT_INCR(sctps_sendswcrc);
11850 SCTP_ENABLE_UDP_CSUM(o_pak);
11852 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11853 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
11854 SCTP_STAT_INCR(sctps_sendhwcrc);
11856 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11858 /* Free the route if we got one back */
11863 if (ip6_out != NULL) {
11864 struct route_in6 ro;
11866 struct sctp_tcb *stcb = NULL;
11867 struct ifnet *ifp = NULL;
11869 /* zap the stack pointer to the route */
11870 bzero(&ro, sizeof(ro));
11872 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11874 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11875 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11876 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11877 #ifdef SCTP_PACKET_LOGGING
11878 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11879 sctp_packet_log(mout, len);
11881 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11882 SCTP_STAT_INCR(sctps_sendswcrc);
11883 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11885 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11886 udp->uh_sum = 0xffff;
11889 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11891 /* Free the route if we got one back */
11896 SCTP_STAT_INCR(sctps_sendpackets);
11897 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11898 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11902 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11903 uint32_t vrf_id, uint16_t port)
11905 struct mbuf *o_pak;
11906 struct sctphdr *sh, *sh_out;
11907 struct sctp_chunkhdr *ch;
11908 struct ip *iph, *iph_out;
11909 struct udphdr *udp = NULL;
11913 struct ip6_hdr *ip6, *ip6_out;
11916 int iphlen_out, len;
11918 iph = mtod(m, struct ip *);
11919 sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11920 switch (iph->ip_v) {
11922 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11925 case IPV6_VERSION >> 4:
11926 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11936 len += sizeof(struct udphdr);
11938 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11939 if (mout == NULL) {
11945 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11946 SCTP_BUF_LEN(mout) = len;
11947 SCTP_BUF_NEXT(mout) = scm;
11952 switch (iph->ip_v) {
11954 iph_out = mtod(mout, struct ip *);
11956 /* Fill in the IP header for the ABORT */
11957 iph_out->ip_v = IPVERSION;
11958 iph_out->ip_hl = (sizeof(struct ip) / 4);
11959 iph_out->ip_tos = (u_char)0;
11960 iph_out->ip_id = 0;
11961 iph_out->ip_off = 0;
11962 iph_out->ip_ttl = MAXTTL;
11964 iph_out->ip_p = IPPROTO_UDP;
11966 iph_out->ip_p = IPPROTO_SCTP;
11968 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11969 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11970 /* let IP layer calculate this */
11971 iph_out->ip_sum = 0;
11973 iphlen_out = sizeof(struct ip);
11974 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11977 case IPV6_VERSION >> 4:
11978 ip6 = (struct ip6_hdr *)iph;
11979 ip6_out = mtod(mout, struct ip6_hdr *);
11981 /* Fill in the IP6 header for the ABORT */
11982 ip6_out->ip6_flow = ip6->ip6_flow;
11983 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11985 ip6_out->ip6_nxt = IPPROTO_UDP;
11987 ip6_out->ip6_nxt = IPPROTO_SCTP;
11989 ip6_out->ip6_src = ip6->ip6_dst;
11990 ip6_out->ip6_dst = ip6->ip6_src;
11992 iphlen_out = sizeof(struct ip6_hdr);
11993 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11997 /* Currently not supported */
11998 sctp_m_freem(mout);
12002 udp = (struct udphdr *)sh_out;
12004 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12005 udp->uh_dport = port;
12006 /* set udp->uh_ulen later */
12008 iphlen_out += sizeof(struct udphdr);
12009 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12011 sh_out->src_port = sh->dest_port;
12012 sh_out->dest_port = sh->src_port;
12013 sh_out->v_tag = vtag;
12014 sh_out->checksum = 0;
12016 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12017 ch->chunk_type = SCTP_OPERATION_ERROR;
12018 ch->chunk_flags = 0;
12021 struct mbuf *m_tmp = scm;
12024 /* get length of the err_cause chain */
12025 while (m_tmp != NULL) {
12026 cause_len += SCTP_BUF_LEN(m_tmp);
12027 m_tmp = SCTP_BUF_NEXT(m_tmp);
12029 len = SCTP_BUF_LEN(mout) + cause_len;
12030 if (cause_len % 4) {
12031 /* need pad at end of chunk */
12032 uint32_t cpthis = 0;
12035 padlen = 4 - (len % 4);
12036 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12039 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12041 len = SCTP_BUF_LEN(mout);
12042 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12045 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12047 sctp_m_freem(mout);
12050 if (iph_out != NULL) {
12052 struct sctp_tcb *stcb = NULL;
12055 /* zap the stack pointer to the route */
12056 bzero(&ro, sizeof ro);
12058 udp->uh_ulen = htons(len - sizeof(struct ip));
12059 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12061 /* set IPv4 length */
12062 iph_out->ip_len = len;
12064 #ifdef SCTP_PACKET_LOGGING
12065 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12066 sctp_packet_log(mout, len);
12068 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12070 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12071 SCTP_STAT_INCR(sctps_sendswcrc);
12072 SCTP_ENABLE_UDP_CSUM(o_pak);
12074 mout->m_pkthdr.csum_flags = CSUM_SCTP;
12075 mout->m_pkthdr.csum_data = 0; /* FIXME MT */
12076 SCTP_STAT_INCR(sctps_sendhwcrc);
12078 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12080 /* Free the route if we got one back */
12085 if (ip6_out != NULL) {
12086 struct route_in6 ro;
12088 struct sctp_tcb *stcb = NULL;
12089 struct ifnet *ifp = NULL;
12091 /* zap the stack pointer to the route */
12092 bzero(&ro, sizeof(ro));
12094 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12096 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12097 #ifdef SCTP_PACKET_LOGGING
12098 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12099 sctp_packet_log(mout, len);
12101 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12102 SCTP_STAT_INCR(sctps_sendswcrc);
12103 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12105 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12106 udp->uh_sum = 0xffff;
12109 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12111 /* Free the route if we got one back */
12116 SCTP_STAT_INCR(sctps_sendpackets);
12117 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12118 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12121 static struct mbuf *
12122 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
12124 struct sctp_sndrcvinfo *srcv,
12126 int user_marks_eor,
12129 struct mbuf **new_tail)
12133 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12134 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12136 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12139 *sndout = m_length(m, NULL);
12140 *new_tail = m_last(m);
12146 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12153 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12155 if (sp->data == NULL) {
12156 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12159 sp->tail_mbuf = m_last(sp->data);
12165 static struct sctp_stream_queue_pending *
12166 sctp_copy_it_in(struct sctp_tcb *stcb,
12167 struct sctp_association *asoc,
12168 struct sctp_sndrcvinfo *srcv,
12170 struct sctp_nets *net,
12172 int user_marks_eor,
12177 * This routine must be very careful in its work. Protocol
12178 * processing is up and running so care must be taken to spl...()
12179 * when you need to do something that may effect the stcb/asoc. The
12180 * sb is locked however. When data is copied the protocol processing
12181 * should be enabled since this is a slower operation...
12183 struct sctp_stream_queue_pending *sp = NULL;
12187 /* Now can we send this? */
12188 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12189 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12190 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12191 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12192 /* got data while shutting down */
12193 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12194 *error = ECONNRESET;
12197 sctp_alloc_a_strmoq(stcb, sp);
12199 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12204 sp->sender_all_done = 0;
12205 sp->sinfo_flags = srcv->sinfo_flags;
12206 sp->timetolive = srcv->sinfo_timetolive;
12207 sp->ppid = srcv->sinfo_ppid;
12208 sp->context = srcv->sinfo_context;
12210 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12212 sp->stream = srcv->sinfo_stream;
12213 sp->length = min(uio->uio_resid, max_send_len);
12214 if ((sp->length == (uint32_t) uio->uio_resid) &&
12215 ((user_marks_eor == 0) ||
12216 (srcv->sinfo_flags & SCTP_EOF) ||
12217 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12218 sp->msg_is_complete = 1;
12220 sp->msg_is_complete = 0;
12222 sp->sender_all_done = 0;
12223 sp->some_taken = 0;
12224 sp->put_last_out = 0;
12225 resv_in_first = sizeof(struct sctp_data_chunk);
12226 sp->data = sp->tail_mbuf = NULL;
12227 if (sp->length == 0) {
12231 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12232 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12233 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12234 sp->holds_key_ref = 1;
12236 *error = sctp_copy_one(sp, uio, resv_in_first);
12239 sctp_free_a_strmoq(stcb, sp);
12242 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12245 sp->net = asoc->primary_destination;
12247 atomic_add_int(&sp->net->ref_count, 1);
12248 sctp_set_prsctp_policy(sp);
12256 sctp_sosend(struct socket *so,
12257 struct sockaddr *addr,
12260 struct mbuf *control,
12265 struct sctp_inpcb *inp;
12266 int error, use_rcvinfo = 0;
12267 struct sctp_sndrcvinfo srcv;
12268 struct sockaddr *addr_to_use;
12271 struct sockaddr_in sin;
12275 inp = (struct sctp_inpcb *)so->so_pcb;
12277 /* process cmsg snd/rcv info (maybe a assoc-id) */
12278 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12284 addr_to_use = addr;
12285 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
12286 if ((addr) && (addr->sa_family == AF_INET6)) {
12287 struct sockaddr_in6 *sin6;
12289 sin6 = (struct sockaddr_in6 *)addr;
12290 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12291 in6_sin6_2_sin(&sin, sin6);
12292 addr_to_use = (struct sockaddr *)&sin;
12296 error = sctp_lower_sosend(so, addr_to_use, uio, top,
12307 sctp_lower_sosend(struct socket *so,
12308 struct sockaddr *addr,
12310 struct mbuf *i_pak,
12311 struct mbuf *control,
12314 struct sctp_sndrcvinfo *srcv
12319 unsigned int sndlen = 0, max_len;
12321 struct mbuf *top = NULL;
12322 int queue_only = 0, queue_only_for_init = 0;
12323 int free_cnt_applied = 0;
12325 int now_filled = 0;
12326 unsigned int inqueue_bytes = 0;
12327 struct sctp_block_entry be;
12328 struct sctp_inpcb *inp;
12329 struct sctp_tcb *stcb = NULL;
12330 struct timeval now;
12331 struct sctp_nets *net;
12332 struct sctp_association *asoc;
12333 struct sctp_inpcb *t_inp;
12334 int user_marks_eor;
12335 int create_lock_applied = 0;
12336 int nagle_applies = 0;
12337 int some_on_control = 0;
12338 int got_all_of_the_send = 0;
12339 int hold_tcblock = 0;
12340 int non_blocking = 0;
12341 int temp_flags = 0;
12342 uint32_t local_add_more, local_soresv = 0;
12349 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12351 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12354 SCTP_RELEASE_PKT(i_pak);
12358 if ((uio == NULL) && (i_pak == NULL)) {
12359 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12362 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12363 atomic_add_int(&inp->total_sends, 1);
12365 if (uio->uio_resid < 0) {
12366 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12369 sndlen = uio->uio_resid;
12371 top = SCTP_HEADER_TO_CHAIN(i_pak);
12372 sndlen = SCTP_HEADER_LEN(i_pak);
12374 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12378 * Pre-screen address, if one is given the sin-len
12379 * must be set correctly!
12382 if ((addr->sa_family == AF_INET) &&
12383 (addr->sa_len != sizeof(struct sockaddr_in))) {
12384 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12387 } else if ((addr->sa_family == AF_INET6) &&
12388 (addr->sa_len != sizeof(struct sockaddr_in6))) {
12389 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12396 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12397 (inp->sctp_socket->so_qlimit)) {
12398 /* The listener can NOT send */
12399 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12403 if ((use_rcvinfo) && srcv) {
12404 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12405 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12406 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12410 if (srcv->sinfo_flags)
12411 SCTP_STAT_INCR(sctps_sends_with_flags);
12413 if (srcv->sinfo_flags & SCTP_SENDALL) {
12414 /* its a sendall */
12415 error = sctp_sendall(inp, uio, top, srcv);
12420 /* now we must find the assoc */
12421 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12422 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12423 SCTP_INP_RLOCK(inp);
12424 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12425 if (stcb == NULL) {
12426 SCTP_INP_RUNLOCK(inp);
12427 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12431 SCTP_TCB_LOCK(stcb);
12433 SCTP_INP_RUNLOCK(inp);
12435 /* Must locate the net structure if addr given */
12436 net = sctp_findnet(stcb, addr);
12438 /* validate port was 0 or correct */
12439 struct sockaddr_in *sin;
12441 sin = (struct sockaddr_in *)addr;
12442 if ((sin->sin_port != 0) &&
12443 (sin->sin_port != stcb->rport)) {
12447 temp_flags |= SCTP_ADDR_OVER;
12449 net = stcb->asoc.primary_destination;
12450 if (addr && (net == NULL)) {
12451 /* Could not find address, was it legal */
12452 if (addr->sa_family == AF_INET) {
12453 struct sockaddr_in *sin;
12455 sin = (struct sockaddr_in *)addr;
12456 if (sin->sin_addr.s_addr == 0) {
12457 if ((sin->sin_port == 0) ||
12458 (sin->sin_port == stcb->rport)) {
12459 net = stcb->asoc.primary_destination;
12463 struct sockaddr_in6 *sin6;
12465 sin6 = (struct sockaddr_in6 *)addr;
12466 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12467 if ((sin6->sin6_port == 0) ||
12468 (sin6->sin6_port == stcb->rport)) {
12469 net = stcb->asoc.primary_destination;
12475 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12479 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12480 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12484 * Must locate the net structure if addr
12487 net = sctp_findnet(stcb, addr);
12489 net = stcb->asoc.primary_destination;
12490 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12491 ((net == NULL) || (addr == NULL))) {
12492 struct sockaddr_in *sin;
12494 if (addr == NULL) {
12495 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12499 sin = (struct sockaddr_in *)addr;
12500 /* Validate port is 0 or correct */
12501 if ((sin->sin_port != 0) &&
12502 (sin->sin_port != stcb->rport)) {
12510 * Since we did not use findep we must
12511 * increment it, and if we don't find a tcb
12514 SCTP_INP_WLOCK(inp);
12515 SCTP_INP_INCR_REF(inp);
12516 SCTP_INP_WUNLOCK(inp);
12517 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12518 if (stcb == NULL) {
12519 SCTP_INP_WLOCK(inp);
12520 SCTP_INP_DECR_REF(inp);
12521 SCTP_INP_WUNLOCK(inp);
12526 if ((stcb == NULL) && (addr)) {
12527 /* Possible implicit send? */
12528 SCTP_ASOC_CREATE_LOCK(inp);
12529 create_lock_applied = 1;
12530 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12531 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12532 /* Should I really unlock ? */
12533 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12538 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12539 (addr->sa_family == AF_INET6)) {
12540 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12544 SCTP_INP_WLOCK(inp);
12545 SCTP_INP_INCR_REF(inp);
12546 SCTP_INP_WUNLOCK(inp);
12547 /* With the lock applied look again */
12548 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12549 if (stcb == NULL) {
12550 SCTP_INP_WLOCK(inp);
12551 SCTP_INP_DECR_REF(inp);
12552 SCTP_INP_WUNLOCK(inp);
12556 if (t_inp != inp) {
12557 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12562 if (stcb == NULL) {
12563 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12564 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12565 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12569 if (addr == NULL) {
12570 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12575 * UDP style, we must go ahead and start the INIT
12580 if ((use_rcvinfo) && (srcv) &&
12581 ((srcv->sinfo_flags & SCTP_ABORT) ||
12582 ((srcv->sinfo_flags & SCTP_EOF) &&
12585 * User asks to abort a non-existant assoc,
12586 * or EOF a non-existant assoc with no data
12588 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12592 /* get an asoc/stcb struct */
12593 vrf_id = inp->def_vrf_id;
12595 if (create_lock_applied == 0) {
12596 panic("Error, should hold create lock and I don't?");
12599 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12602 if (stcb == NULL) {
12603 /* Error is setup for us in the call */
12606 if (create_lock_applied) {
12607 SCTP_ASOC_CREATE_UNLOCK(inp);
12608 create_lock_applied = 0;
12610 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12613 * Turn on queue only flag to prevent data from
12617 asoc = &stcb->asoc;
12618 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12619 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12621 /* initialize authentication params for the assoc */
12622 sctp_initialize_auth_params(inp, stcb);
12626 * see if a init structure exists in cmsg
12629 struct sctp_initmsg initm;
12632 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12635 * we have an INIT override of the
12638 if (initm.sinit_max_attempts)
12639 asoc->max_init_times = initm.sinit_max_attempts;
12640 if (initm.sinit_num_ostreams)
12641 asoc->pre_open_streams = initm.sinit_num_ostreams;
12642 if (initm.sinit_max_instreams)
12643 asoc->max_inbound_streams = initm.sinit_max_instreams;
12644 if (initm.sinit_max_init_timeo)
12645 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12646 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12647 struct sctp_stream_out *tmp_str;
12650 /* Default is NOT correct */
12651 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12652 asoc->streamoutcnt, asoc->pre_open_streams);
12654 * What happens if this
12655 * fails? we panic ...
12658 if (hold_tcblock) {
12660 SCTP_TCB_UNLOCK(stcb);
12662 SCTP_MALLOC(tmp_str,
12663 struct sctp_stream_out *,
12664 (asoc->pre_open_streams *
12665 sizeof(struct sctp_stream_out)),
12668 SCTP_TCB_LOCK(stcb);
12670 if (tmp_str != NULL) {
12671 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12672 asoc->strmout = tmp_str;
12673 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12675 asoc->pre_open_streams = asoc->streamoutcnt;
12677 for (i = 0; i < asoc->streamoutcnt; i++) {
12679 * inbound side must be set
12680 * to 0xffff, also NOTE when
12681 * we get the INIT-ACK back
12682 * (for INIT sender) we MUST
12684 * (streamoutcnt) but first
12685 * check if we sent to any
12686 * of the upper streams that
12687 * were dropped (if some
12688 * were). Those that were
12689 * dropped must be notified
12690 * to the upper layer as
12693 asoc->strmout[i].next_sequence_sent = 0x0;
12694 TAILQ_INIT(&asoc->strmout[i].outqueue);
12695 asoc->strmout[i].stream_no = i;
12696 asoc->strmout[i].last_msg_incomplete = 0;
12697 asoc->strmout[i].next_spoke.tqe_next = 0;
12698 asoc->strmout[i].next_spoke.tqe_prev = 0;
12704 /* out with the INIT */
12705 queue_only_for_init = 1;
12707 * we may want to dig in after this call and adjust the MTU
12708 * value. It defaulted to 1500 (constant) but the ro
12709 * structure may now have an update and thus we may need to
12710 * change it BEFORE we append the message.
12712 net = stcb->asoc.primary_destination;
12713 asoc = &stcb->asoc;
12716 if ((SCTP_SO_IS_NBIO(so)
12717 || (flags & MSG_NBIO)
12721 asoc = &stcb->asoc;
12722 atomic_add_int(&stcb->total_sends, 1);
12724 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12725 if (sndlen > asoc->smallest_mtu) {
12726 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12731 /* would we block? */
12732 if (non_blocking) {
12733 if (hold_tcblock == 0) {
12734 SCTP_TCB_LOCK(stcb);
12737 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12738 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12739 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12740 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12741 if (sndlen > SCTP_SB_LIMIT_SND(so))
12744 error = EWOULDBLOCK;
12747 stcb->asoc.sb_send_resv += sndlen;
12748 SCTP_TCB_UNLOCK(stcb);
12751 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12753 local_soresv = sndlen;
12754 /* Keep the stcb from being freed under our feet */
12755 if (free_cnt_applied) {
12757 panic("refcnt already incremented");
12759 printf("refcnt:1 already incremented?\n");
12762 atomic_add_int(&stcb->asoc.refcnt, 1);
12763 free_cnt_applied = 1;
12765 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12766 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12767 error = ECONNRESET;
12770 if (create_lock_applied) {
12771 SCTP_ASOC_CREATE_UNLOCK(inp);
12772 create_lock_applied = 0;
12774 if (asoc->stream_reset_outstanding) {
12776 * Can't queue any data while stream reset is underway.
12778 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12782 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12783 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12786 if ((use_rcvinfo == 0) || (srcv == NULL)) {
12787 /* Grab the default stuff from the asoc */
12788 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12790 /* we are now done with all control */
12792 sctp_m_freem(control);
12795 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12796 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12797 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12798 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12799 if ((use_rcvinfo) &&
12800 (srcv->sinfo_flags & SCTP_ABORT)) {
12803 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12804 error = ECONNRESET;
12808 /* Ok, we will attempt a msgsnd :> */
12810 p->td_ru.ru_msgsnd++;
12813 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12814 net = stcb->asoc.primary_destination;
12818 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12822 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12824 * CMT: Added check for CMT above. net above is the primary
12825 * dest. If CMT is ON, sender should always attempt to send
12826 * with the output routine sctp_fill_outqueue() that loops
12827 * through all destination addresses. Therefore, if CMT is
12828 * ON, queue_only is NOT set to 1 here, so that
12829 * sctp_chunk_output() can be called below.
12832 } else if (asoc->ifp_had_enobuf) {
12833 SCTP_STAT_INCR(sctps_ifnomemqueued);
12834 if (net->flight_size > (net->mtu * 2))
12836 asoc->ifp_had_enobuf = 0;
12838 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12839 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12841 /* Are we aborting? */
12842 if (srcv->sinfo_flags & SCTP_ABORT) {
12844 int tot_demand, tot_out = 0, max_out;
12846 SCTP_STAT_INCR(sctps_sends_with_abort);
12847 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12848 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12849 /* It has to be up before we abort */
12850 /* how big is the user initiated abort? */
12851 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12855 if (hold_tcblock) {
12856 SCTP_TCB_UNLOCK(stcb);
12860 struct mbuf *cntm = NULL;
12862 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12866 tot_out += SCTP_BUF_LEN(cntm);
12867 cntm = SCTP_BUF_NEXT(cntm);
12870 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12872 /* Must fit in a MTU */
12874 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12875 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12877 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12881 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12884 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12888 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12889 max_out -= sizeof(struct sctp_abort_msg);
12890 if (tot_out > max_out) {
12894 struct sctp_paramhdr *ph;
12896 /* now move forward the data pointer */
12897 ph = mtod(mm, struct sctp_paramhdr *);
12898 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12899 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12901 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12903 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12906 * Here if we can't get his data we
12907 * still abort we just don't get to
12908 * send the users note :-0
12915 SCTP_BUF_NEXT(mm) = top;
12919 if (hold_tcblock == 0) {
12920 SCTP_TCB_LOCK(stcb);
12923 atomic_add_int(&stcb->asoc.refcnt, -1);
12924 free_cnt_applied = 0;
12925 /* release this lock, otherwise we hang on ourselves */
12926 sctp_abort_an_association(stcb->sctp_ep, stcb,
12927 SCTP_RESPONSE_TO_USER_REQ,
12928 mm, SCTP_SO_LOCKED);
12929 /* now relock the stcb so everything is sane */
12933 * In this case top is already chained to mm avoid double
12934 * free, since we free it below if top != NULL and driver
12935 * would free it after sending the packet out
12942 /* Calculate the maximum we can send */
12943 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12944 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12945 if (non_blocking) {
12946 /* we already checked for non-blocking above. */
12949 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12954 if (hold_tcblock) {
12955 SCTP_TCB_UNLOCK(stcb);
12958 /* Is the stream no. valid? */
12959 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12960 /* Invalid stream number */
12961 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12965 if (asoc->strmout == NULL) {
12966 /* huh? software error */
12967 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12971 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12972 if ((user_marks_eor == 0) &&
12973 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12974 /* It will NEVER fit */
12975 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12979 if ((uio == NULL) && user_marks_eor) {
12981 * We do not support eeor mode for
12982 * sending with mbuf chains (like sendfile).
12984 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12988 if (user_marks_eor) {
12989 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12992 * For non-eeor the whole message must fit in
12993 * the socket send buffer.
12995 local_add_more = sndlen;
12998 if (non_blocking) {
12999 goto skip_preblock;
13001 if (((max_len <= local_add_more) &&
13002 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13004 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13005 /* No room right now ! */
13006 SOCKBUF_LOCK(&so->so_snd);
13007 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13008 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13009 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13010 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13011 (unsigned int)SCTP_SB_LIMIT_SND(so),
13014 stcb->asoc.stream_queue_cnt,
13015 stcb->asoc.chunks_on_out_queue,
13016 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13017 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13018 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
13021 stcb->block_entry = &be;
13022 error = sbwait(&so->so_snd);
13023 stcb->block_entry = NULL;
13024 if (error || so->so_error || be.error) {
13027 error = so->so_error;
13032 SOCKBUF_UNLOCK(&so->so_snd);
13035 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13036 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13037 so, asoc, stcb->asoc.total_output_queue_size);
13039 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13042 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13044 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13045 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13046 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13050 SOCKBUF_UNLOCK(&so->so_snd);
13053 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13057 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13058 * case NOTE: uio will be null when top/mbuf is passed
13061 if (srcv->sinfo_flags & SCTP_EOF) {
13062 got_all_of_the_send = 1;
13065 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13071 struct sctp_stream_queue_pending *sp;
13072 struct sctp_stream_out *strm;
13073 uint32_t sndout, initial_out;
13075 initial_out = uio->uio_resid;
13077 SCTP_TCB_SEND_LOCK(stcb);
13078 if ((asoc->stream_locked) &&
13079 (asoc->stream_locked_on != srcv->sinfo_stream)) {
13080 SCTP_TCB_SEND_UNLOCK(stcb);
13081 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13085 SCTP_TCB_SEND_UNLOCK(stcb);
13087 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13088 if (strm->last_msg_incomplete == 0) {
13090 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
13091 if ((sp == NULL) || (error)) {
13094 SCTP_TCB_SEND_LOCK(stcb);
13095 if (sp->msg_is_complete) {
13096 strm->last_msg_incomplete = 0;
13097 asoc->stream_locked = 0;
13100 * Just got locked to this guy in case of an
13103 strm->last_msg_incomplete = 1;
13104 asoc->stream_locked = 1;
13105 asoc->stream_locked_on = srcv->sinfo_stream;
13106 sp->sender_all_done = 0;
13108 sctp_snd_sb_alloc(stcb, sp->length);
13109 atomic_add_int(&asoc->stream_queue_cnt, 1);
13110 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13111 sp->strseq = strm->next_sequence_sent;
13112 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13113 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13114 (uintptr_t) stcb, sp->length,
13115 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13117 strm->next_sequence_sent++;
13119 SCTP_STAT_INCR(sctps_sends_with_unord);
13121 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13122 if ((strm->next_spoke.tqe_next == NULL) &&
13123 (strm->next_spoke.tqe_prev == NULL)) {
13124 /* Not on wheel, insert */
13125 sctp_insert_on_wheel(stcb, asoc, strm, 1);
13127 SCTP_TCB_SEND_UNLOCK(stcb);
13129 SCTP_TCB_SEND_LOCK(stcb);
13130 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13131 SCTP_TCB_SEND_UNLOCK(stcb);
13133 /* ???? Huh ??? last msg is gone */
13135 panic("Warning: Last msg marked incomplete, yet nothing left?");
13137 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13138 strm->last_msg_incomplete = 0;
13144 while (uio->uio_resid > 0) {
13145 /* How much room do we have? */
13146 struct mbuf *new_tail, *mm;
13148 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13149 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13153 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13154 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13155 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13158 if (hold_tcblock) {
13159 SCTP_TCB_UNLOCK(stcb);
13162 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13163 if ((mm == NULL) || error) {
13169 /* Update the mbuf and count */
13170 SCTP_TCB_SEND_LOCK(stcb);
13171 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13173 * we need to get out. Peer probably
13177 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13178 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13179 error = ECONNRESET;
13181 SCTP_TCB_SEND_UNLOCK(stcb);
13184 if (sp->tail_mbuf) {
13185 /* tack it to the end */
13186 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13187 sp->tail_mbuf = new_tail;
13189 /* A stolen mbuf */
13191 sp->tail_mbuf = new_tail;
13193 sctp_snd_sb_alloc(stcb, sndout);
13194 atomic_add_int(&sp->length, sndout);
13197 /* Did we reach EOR? */
13198 if ((uio->uio_resid == 0) &&
13199 ((user_marks_eor == 0) ||
13200 (srcv->sinfo_flags & SCTP_EOF) ||
13201 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13202 sp->msg_is_complete = 1;
13204 sp->msg_is_complete = 0;
13206 SCTP_TCB_SEND_UNLOCK(stcb);
13208 if (uio->uio_resid == 0) {
13213 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13215 * This is ugly but we must assure locking
13218 if (hold_tcblock == 0) {
13219 SCTP_TCB_LOCK(stcb);
13222 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13223 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13224 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13225 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13231 SCTP_TCB_UNLOCK(stcb);
13234 /* wait for space now */
13235 if (non_blocking) {
13236 /* Non-blocking io in place out */
13239 if ((net->flight_size > net->cwnd) &&
13240 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13242 } else if (asoc->ifp_had_enobuf) {
13243 SCTP_STAT_INCR(sctps_ifnomemqueued);
13244 if (net->flight_size > (net->mtu * 2)) {
13249 asoc->ifp_had_enobuf = 0;
13250 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13251 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13253 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13254 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13255 if (net->flight_size > net->cwnd) {
13257 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13262 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13263 (stcb->asoc.total_flight > 0) &&
13264 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13265 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13268 * Ok, Nagle is set on and we have data outstanding.
13269 * Don't send anything and let SACKs drive out the
13270 * data unless wen have a "full" segment to send.
13272 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13273 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13275 SCTP_STAT_INCR(sctps_naglequeued);
13278 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13279 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13280 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13282 SCTP_STAT_INCR(sctps_naglesent);
13285 /* What about the INIT, send it maybe */
13286 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13288 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13289 nagle_applies, un_sent);
13290 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13291 stcb->asoc.total_flight,
13292 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13294 if (queue_only_for_init) {
13295 if (hold_tcblock == 0) {
13296 SCTP_TCB_LOCK(stcb);
13299 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13300 /* a collision took us forward? */
13301 queue_only_for_init = 0;
13304 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13305 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13306 queue_only_for_init = 0;
13310 if ((queue_only == 0) && (nagle_applies == 0)) {
13312 * need to start chunk output
13313 * before blocking.. note that if
13314 * a lock is already applied, then
13315 * the input via the net is happening
13316 * and I don't need to start output :-D
13318 if (hold_tcblock == 0) {
13319 if (SCTP_TCB_TRYLOCK(stcb)) {
13321 sctp_chunk_output(inp,
13323 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13326 sctp_chunk_output(inp,
13328 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13330 if (hold_tcblock == 1) {
13331 SCTP_TCB_UNLOCK(stcb);
13335 SOCKBUF_LOCK(&so->so_snd);
13337 * This is a bit strange, but I think it will
13338 * work. The total_output_queue_size is locked and
13339 * protected by the TCB_LOCK, which we just released.
13340 * There is a race that can occur between releasing it
13341 * above, and me getting the socket lock, where sacks
13342 * come in but we have not put the SB_WAIT on the
13343 * so_snd buffer to get the wakeup. After the LOCK
13344 * is applied the sack_processing will also need to
13345 * LOCK the so->so_snd to do the actual sowwakeup(). So
13346 * once we have the socket buffer lock if we recheck the
13347 * size we KNOW we will get to sleep safely with the
13348 * wakeup flag in place.
13350 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13351 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13352 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13353 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13354 so, asoc, uio->uio_resid);
13357 stcb->block_entry = &be;
13358 error = sbwait(&so->so_snd);
13359 stcb->block_entry = NULL;
13361 if (error || so->so_error || be.error) {
13364 error = so->so_error;
13369 SOCKBUF_UNLOCK(&so->so_snd);
13372 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13373 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13374 so, asoc, stcb->asoc.total_output_queue_size);
13377 SOCKBUF_UNLOCK(&so->so_snd);
13378 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13382 SCTP_TCB_SEND_LOCK(stcb);
13384 if (sp->msg_is_complete == 0) {
13385 strm->last_msg_incomplete = 1;
13386 asoc->stream_locked = 1;
13387 asoc->stream_locked_on = srcv->sinfo_stream;
13389 sp->sender_all_done = 1;
13390 strm->last_msg_incomplete = 0;
13391 asoc->stream_locked = 0;
13394 SCTP_PRINTF("Huh no sp TSNH?\n");
13395 strm->last_msg_incomplete = 0;
13396 asoc->stream_locked = 0;
13398 SCTP_TCB_SEND_UNLOCK(stcb);
13399 if (uio->uio_resid == 0) {
13400 got_all_of_the_send = 1;
13403 /* We send in a 0, since we do NOT have any locks */
13404 error = sctp_msg_append(stcb, net, top, srcv, 0);
13406 if (srcv->sinfo_flags & SCTP_EOF) {
13408 * This should only happen for Panda for the mbuf
13409 * send case, which does NOT yet support EEOR mode.
13410 * Thus, we can just set this flag to do the proper
13413 got_all_of_the_send = 1;
13421 if ((srcv->sinfo_flags & SCTP_EOF) &&
13422 (got_all_of_the_send == 1) &&
13423 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13426 SCTP_STAT_INCR(sctps_sends_with_eof);
13428 if (hold_tcblock == 0) {
13429 SCTP_TCB_LOCK(stcb);
13432 cnt = sctp_is_there_unsent_data(stcb);
13433 if (TAILQ_EMPTY(&asoc->send_queue) &&
13434 TAILQ_EMPTY(&asoc->sent_queue) &&
13436 if (asoc->locked_on_sending) {
13439 /* there is nothing queued to send, so I'm done... */
13440 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13441 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13442 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13443 /* only send SHUTDOWN the first time through */
13444 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13445 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13446 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13448 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13449 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13450 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13451 asoc->primary_destination);
13452 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13453 asoc->primary_destination);
13457 * we still got (or just got) data to send, so set
13461 * XXX sockets draft says that SCTP_EOF should be
13462 * sent with no data. currently, we will allow user
13463 * data to be sent first and move to
13466 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13467 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13468 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13469 if (hold_tcblock == 0) {
13470 SCTP_TCB_LOCK(stcb);
13473 if (asoc->locked_on_sending) {
13474 /* Locked to send out the data */
13475 struct sctp_stream_queue_pending *sp;
13477 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13479 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13480 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13483 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13484 if (TAILQ_EMPTY(&asoc->send_queue) &&
13485 TAILQ_EMPTY(&asoc->sent_queue) &&
13486 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13488 if (free_cnt_applied) {
13489 atomic_add_int(&stcb->asoc.refcnt, -1);
13490 free_cnt_applied = 0;
13492 sctp_abort_an_association(stcb->sctp_ep, stcb,
13493 SCTP_RESPONSE_TO_USER_REQ,
13494 NULL, SCTP_SO_LOCKED);
13496 * now relock the stcb so everything
13503 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13504 asoc->primary_destination);
13505 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13510 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13511 some_on_control = 1;
13513 if ((net->flight_size > net->cwnd) &&
13514 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13516 } else if (asoc->ifp_had_enobuf) {
13517 SCTP_STAT_INCR(sctps_ifnomemqueued);
13518 if (net->flight_size > (net->mtu * 2)) {
13523 asoc->ifp_had_enobuf = 0;
13524 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13525 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13527 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13528 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13529 if (net->flight_size > net->cwnd) {
13531 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13536 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13537 (stcb->asoc.total_flight > 0) &&
13538 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13539 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13541 * Ok, Nagle is set on and we have data outstanding.
13542 * Don't send anything and let SACKs drive out the
13543 * data unless wen have a "full" segment to send.
13545 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13546 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13548 SCTP_STAT_INCR(sctps_naglequeued);
13551 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13552 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13553 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13555 SCTP_STAT_INCR(sctps_naglesent);
13558 if (queue_only_for_init) {
13559 if (hold_tcblock == 0) {
13560 SCTP_TCB_LOCK(stcb);
13563 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13564 /* a collision took us forward? */
13565 queue_only_for_init = 0;
13568 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13569 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13570 queue_only_for_init = 0;
13574 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13575 /* we can attempt to send too. */
13576 if (hold_tcblock == 0) {
13578 * If there is activity recv'ing sacks no need to
13581 if (SCTP_TCB_TRYLOCK(stcb)) {
13582 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13586 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13588 } else if ((queue_only == 0) &&
13589 (stcb->asoc.peers_rwnd == 0) &&
13590 (stcb->asoc.total_flight == 0)) {
13591 /* We get to have a probe outstanding */
13592 if (hold_tcblock == 0) {
13594 SCTP_TCB_LOCK(stcb);
13596 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13597 } else if (some_on_control) {
13598 int num_out, reason, frag_point;
13600 /* Here we do control only */
13601 if (hold_tcblock == 0) {
13603 SCTP_TCB_LOCK(stcb);
13605 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13606 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13607 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13609 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13610 queue_only, stcb->asoc.peers_rwnd, un_sent,
13611 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13612 stcb->asoc.total_output_queue_size, error);
13617 if (local_soresv && stcb) {
13618 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13621 if (create_lock_applied) {
13622 SCTP_ASOC_CREATE_UNLOCK(inp);
13623 create_lock_applied = 0;
13625 if ((stcb) && hold_tcblock) {
13626 SCTP_TCB_UNLOCK(stcb);
13628 if (stcb && free_cnt_applied) {
13629 atomic_add_int(&stcb->asoc.refcnt, -1);
13633 if (mtx_owned(&stcb->tcb_mtx)) {
13634 panic("Leaving with tcb mtx owned?");
13636 if (mtx_owned(&stcb->tcb_send_mtx)) {
13637 panic("Leaving with tcb send mtx owned?");
13645 sctp_m_freem(control);
13652 * generate an AUTHentication chunk, if required
13655 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13656 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13657 struct sctp_tcb *stcb, uint8_t chunk)
13659 struct mbuf *m_auth;
13660 struct sctp_auth_chunk *auth;
13663 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13667 /* sysctl disabled auth? */
13668 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13671 /* peer doesn't do auth... */
13672 if (!stcb->asoc.peer_supports_auth) {
13675 /* does the requested chunk require auth? */
13676 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13679 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13680 if (m_auth == NULL) {
13684 /* reserve some space if this will be the first mbuf */
13686 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13687 /* fill in the AUTH chunk details */
13688 auth = mtod(m_auth, struct sctp_auth_chunk *);
13689 bzero(auth, sizeof(*auth));
13690 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13691 auth->ch.chunk_flags = 0;
13692 chunk_len = sizeof(*auth) +
13693 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13694 auth->ch.chunk_length = htons(chunk_len);
13695 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13696 /* key id and hmac digest will be computed and filled in upon send */
13698 /* save the offset where the auth was inserted into the chain */
13705 *offset += SCTP_BUF_LEN(cn);
13706 cn = SCTP_BUF_NEXT(cn);
13711 /* update length and return pointer to the auth chunk */
13712 SCTP_BUF_LEN(m_auth) = chunk_len;
13713 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13714 if (auth_ret != NULL)
13722 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13724 struct nd_prefix *pfx = NULL;
13725 struct nd_pfxrouter *pfxrtr = NULL;
13726 struct sockaddr_in6 gw6;
13728 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13731 /* get prefix entry of address */
13732 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13733 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13735 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13736 &src6->sin6_addr, &pfx->ndpr_mask))
13739 /* no prefix entry in the prefix list */
13741 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13742 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13745 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13746 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13748 /* search installed gateway from prefix entry */
13749 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13750 pfxrtr->pfr_next) {
13751 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13752 gw6.sin6_family = AF_INET6;
13753 gw6.sin6_len = sizeof(struct sockaddr_in6);
13754 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13755 sizeof(struct in6_addr));
13756 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13757 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13758 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13759 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13760 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13761 ro->ro_rt->rt_gateway)) {
13762 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13766 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13773 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13775 struct sockaddr_in *sin, *mask;
13776 struct ifaddr *ifa;
13777 struct in_addr srcnetaddr, gwnetaddr;
13779 if (ro == NULL || ro->ro_rt == NULL ||
13780 sifa->address.sa.sa_family != AF_INET) {
13783 ifa = (struct ifaddr *)sifa->ifa;
13784 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13785 sin = (struct sockaddr_in *)&sifa->address.sin;
13786 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13787 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13788 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13789 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13791 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13792 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13793 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13794 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13795 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13796 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {