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)) {
3497 /* FreeBSD has a function for ip_id's */
3498 ip->ip_id = ip_newid();
3500 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3501 ip->ip_len = packet_length;
3503 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3505 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3508 ip->ip_tos = (u_char)(tos_value & 0xfc);
3511 /* no association at all */
3512 ip->ip_tos = (tos_value & 0xfc);
3515 ip->ip_p = IPPROTO_UDP;
3517 ip->ip_p = IPPROTO_SCTP;
3522 memset(&iproute, 0, sizeof(iproute));
3523 memcpy(&ro->ro_dst, to, to->sa_len);
3525 ro = (sctp_route_t *) & net->ro;
3527 /* Now the address selection part */
3528 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3530 /* call the routine to select the src address */
3531 if (net && out_of_asoc_ok == 0) {
3532 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3533 sctp_free_ifa(net->ro._s_addr);
3534 net->ro._s_addr = NULL;
3535 net->src_addr_selected = 0;
3541 if (net->src_addr_selected == 0) {
3542 /* Cache the source address */
3543 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3546 net->src_addr_selected = 1;
3548 if (net->ro._s_addr == NULL) {
3549 /* No route to host */
3550 net->src_addr_selected = 0;
3553 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3555 if (over_addr == NULL) {
3556 struct sctp_ifa *_lsrc;
3558 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3562 if (_lsrc == NULL) {
3565 ip->ip_src = _lsrc->address.sin.sin_addr;
3566 sctp_free_ifa(_lsrc);
3568 ip->ip_src = over_addr->sin.sin_addr;
3569 SCTP_RTALLOC(ro, vrf_id);
3573 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3574 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3575 udp->uh_dport = port;
3576 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3577 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3578 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3580 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3583 sctphdr->src_port = src_port;
3584 sctphdr->dest_port = dest_port;
3585 sctphdr->v_tag = v_tag;
3586 sctphdr->checksum = 0;
3589 * If source address selection fails and we find no route
3590 * then the ip_output should fail as well with a
3591 * NO_ROUTE_TO_HOST type error. We probably should catch
3592 * that somewhere and abort the association right away
3593 * (assuming this is an INIT being sent).
3595 if ((ro->ro_rt == NULL)) {
3597 * src addr selection failed to find a route (or
3598 * valid source addr), so we can't get there from
3602 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3603 "%s: dropped packet - no valid source addr\n",
3606 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3607 "Destination was ");
3608 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3609 &net->ro._l_addr.sa);
3610 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3611 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3612 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3613 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3615 SCTP_FAILED_THRESHOLD,
3618 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3619 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3621 * JRS 5/14/07 - If a
3623 * unreachable, the PF bit
3624 * is turned off. This
3625 * allows an unambiguous use
3627 * destinations that are
3628 * reachable but potentially
3630 * destination is set to the
3631 * unreachable state, also
3632 * set the destination to
3636 * Add debug message here if
3637 * destination is not in PF
3641 * Stop any running T3
3644 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3645 net->dest_state &= ~SCTP_ADDR_PF;
3646 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3652 if (net == stcb->asoc.primary_destination) {
3653 /* need a new primary */
3654 struct sctp_nets *alt;
3656 alt = sctp_find_alternate_net(stcb, net, 0);
3658 if (sctp_set_primary_addr(stcb,
3659 (struct sockaddr *)NULL,
3661 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3662 if (net->ro._s_addr) {
3663 sctp_free_ifa(net->ro._s_addr);
3664 net->ro._s_addr = NULL;
3666 net->src_addr_selected = 0;
3672 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3674 return (EHOSTUNREACH);
3676 if (ro != &iproute) {
3677 memcpy(&iproute, ro, sizeof(*ro));
3679 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3680 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3681 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3682 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3683 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3686 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3687 /* failed to prepend data, give up */
3688 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3692 #ifdef SCTP_PACKET_LOGGING
3693 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3694 sctp_packet_log(m, packet_length);
3696 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3698 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3700 (stcb->asoc.loopback_scope))) {
3701 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3702 SCTP_STAT_INCR(sctps_sendswcrc);
3704 SCTP_STAT_INCR(sctps_sendnocrc);
3706 SCTP_ENABLE_UDP_CSUM(o_pak);
3708 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3710 (stcb->asoc.loopback_scope))) {
3711 m->m_pkthdr.csum_flags = CSUM_SCTP;
3712 m->m_pkthdr.csum_data = 0;
3713 SCTP_STAT_INCR(sctps_sendhwcrc);
3715 SCTP_STAT_INCR(sctps_sendnocrc);
3718 /* send it out. table id is taken from stcb */
3719 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3720 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3721 so = SCTP_INP_SO(inp);
3722 SCTP_SOCKET_UNLOCK(so, 0);
3725 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3726 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3727 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3728 atomic_add_int(&stcb->asoc.refcnt, 1);
3729 SCTP_TCB_UNLOCK(stcb);
3730 SCTP_SOCKET_LOCK(so, 0);
3731 SCTP_TCB_LOCK(stcb);
3732 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3735 SCTP_STAT_INCR(sctps_sendpackets);
3736 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3738 SCTP_STAT_INCR(sctps_senderrors);
3740 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3742 /* free tempy routes */
3748 /* PMTU check versus smallest asoc MTU goes here */
3749 if ((ro->ro_rt != NULL) &&
3750 (net->ro._s_addr)) {
3753 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3755 mtu -= sizeof(struct udphdr);
3757 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3758 #ifdef SCTP_PRINT_FOR_B_AND_M
3759 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3761 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3764 } else if (ro->ro_rt == NULL) {
3765 /* route was freed */
3766 if (net->ro._s_addr &&
3767 net->src_addr_selected) {
3768 sctp_free_ifa(net->ro._s_addr);
3769 net->ro._s_addr = NULL;
3771 net->src_addr_selected = 0;
3777 else if (to->sa_family == AF_INET6) {
3779 struct ip6_hdr *ip6h;
3780 struct route_in6 ip6route;
3783 uint16_t flowBottom;
3784 u_char tosBottom, tosTop;
3785 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3787 struct sockaddr_in6 lsa6_storage;
3789 u_short prev_port = 0;
3793 flowlabel = net->tos_flowlabel;
3795 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3798 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3800 len += sizeof(struct udphdr);
3802 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3805 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3808 SCTP_ALIGN_TO_END(newm, len);
3809 SCTP_BUF_LEN(newm) = len;
3810 SCTP_BUF_NEXT(newm) = m;
3812 packet_length = sctp_calculate_len(m);
3814 ip6h = mtod(m, struct ip6_hdr *);
3816 * We assume here that inp_flow is in host byte order within
3819 flowBottom = flowlabel & 0x0000ffff;
3820 flowTop = ((flowlabel & 0x000f0000) >> 16);
3821 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3822 /* protect *sin6 from overwrite */
3823 sin6 = (struct sockaddr_in6 *)to;
3827 /* KAME hack: embed scopeid */
3828 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3829 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3833 memset(&ip6route, 0, sizeof(ip6route));
3834 ro = (sctp_route_t *) & ip6route;
3835 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3837 ro = (sctp_route_t *) & net->ro;
3840 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3842 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3845 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3848 /* we could get no asoc if it is a O-O-T-B packet */
3849 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3851 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3853 ip6h->ip6_nxt = IPPROTO_UDP;
3855 ip6h->ip6_nxt = IPPROTO_SCTP;
3857 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3858 ip6h->ip6_dst = sin6->sin6_addr;
3861 * Add SRC address selection here: we can only reuse to a
3862 * limited degree the kame src-addr-sel, since we can try
3863 * their selection but it may not be bound.
3865 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3866 lsa6_tmp.sin6_family = AF_INET6;
3867 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3869 if (net && out_of_asoc_ok == 0) {
3870 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3871 sctp_free_ifa(net->ro._s_addr);
3872 net->ro._s_addr = NULL;
3873 net->src_addr_selected = 0;
3879 if (net->src_addr_selected == 0) {
3880 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3881 /* KAME hack: embed scopeid */
3882 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3883 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3886 /* Cache the source address */
3887 net->ro._s_addr = sctp_source_address_selection(inp,
3893 (void)sa6_recoverscope(sin6);
3894 net->src_addr_selected = 1;
3896 if (net->ro._s_addr == NULL) {
3897 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3898 net->src_addr_selected = 0;
3901 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3903 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3904 /* KAME hack: embed scopeid */
3905 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3906 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3909 if (over_addr == NULL) {
3910 struct sctp_ifa *_lsrc;
3912 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3916 if (_lsrc == NULL) {
3919 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3920 sctp_free_ifa(_lsrc);
3922 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3923 SCTP_RTALLOC(ro, vrf_id);
3925 (void)sa6_recoverscope(sin6);
3927 lsa6->sin6_port = inp->sctp_lport;
3929 if (ro->ro_rt == NULL) {
3931 * src addr selection failed to find a route (or
3932 * valid source addr), so we can't get there from
3938 * XXX: sa6 may not have a valid sin6_scope_id in the
3939 * non-SCOPEDROUTING case.
3941 bzero(&lsa6_storage, sizeof(lsa6_storage));
3942 lsa6_storage.sin6_family = AF_INET6;
3943 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3944 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3945 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3946 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3951 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3952 lsa6_storage.sin6_port = inp->sctp_lport;
3953 lsa6 = &lsa6_storage;
3954 ip6h->ip6_src = lsa6->sin6_addr;
3957 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3958 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3959 udp->uh_dport = port;
3960 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3962 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3964 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3967 sctphdr->src_port = src_port;
3968 sctphdr->dest_port = dest_port;
3969 sctphdr->v_tag = v_tag;
3970 sctphdr->checksum = 0;
3973 * We set the hop limit now since there is a good chance
3974 * that our ro pointer is now filled
3976 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3977 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3980 /* Copy to be sure something bad is not happening */
3981 sin6->sin6_addr = ip6h->ip6_dst;
3982 lsa6->sin6_addr = ip6h->ip6_src;
3985 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3986 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3987 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3988 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3989 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3991 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3992 /* preserve the port and scope for link local send */
3993 prev_scope = sin6->sin6_scope_id;
3994 prev_port = sin6->sin6_port;
3996 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3997 /* failed to prepend data, give up */
3999 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4002 #ifdef SCTP_PACKET_LOGGING
4003 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4004 sctp_packet_log(m, packet_length);
4006 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4008 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4010 (stcb->asoc.loopback_scope))) {
4011 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4012 SCTP_STAT_INCR(sctps_sendswcrc);
4014 SCTP_STAT_INCR(sctps_sendnocrc);
4016 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4017 udp->uh_sum = 0xffff;
4020 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4022 (stcb->asoc.loopback_scope))) {
4023 m->m_pkthdr.csum_flags = CSUM_SCTP;
4024 m->m_pkthdr.csum_data = 0;
4025 SCTP_STAT_INCR(sctps_sendhwcrc);
4027 SCTP_STAT_INCR(sctps_sendnocrc);
4030 /* send it out. table id is taken from stcb */
4031 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4032 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4033 so = SCTP_INP_SO(inp);
4034 SCTP_SOCKET_UNLOCK(so, 0);
4037 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4038 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4039 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4040 atomic_add_int(&stcb->asoc.refcnt, 1);
4041 SCTP_TCB_UNLOCK(stcb);
4042 SCTP_SOCKET_LOCK(so, 0);
4043 SCTP_TCB_LOCK(stcb);
4044 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4048 /* for link local this must be done */
4049 sin6->sin6_scope_id = prev_scope;
4050 sin6->sin6_port = prev_port;
4052 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4053 SCTP_STAT_INCR(sctps_sendpackets);
4054 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4056 SCTP_STAT_INCR(sctps_senderrors);
4059 /* Now if we had a temp route free it */
4064 /* PMTU check versus smallest asoc MTU goes here */
4065 if (ro->ro_rt == NULL) {
4066 /* Route was freed */
4067 if (net->ro._s_addr &&
4068 net->src_addr_selected) {
4069 sctp_free_ifa(net->ro._s_addr);
4070 net->ro._s_addr = NULL;
4072 net->src_addr_selected = 0;
4074 if ((ro->ro_rt != NULL) &&
4075 (net->ro._s_addr)) {
4078 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4080 (stcb->asoc.smallest_mtu > mtu)) {
4081 #ifdef SCTP_PRINT_FOR_B_AND_M
4082 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4085 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4088 net->mtu -= sizeof(struct udphdr);
4092 if (ND_IFINFO(ifp)->linkmtu &&
4093 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4094 #ifdef SCTP_PRINT_FOR_B_AND_M
4095 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4096 ND_IFINFO(ifp)->linkmtu);
4098 sctp_mtu_size_reset(inp,
4100 ND_IFINFO(ifp)->linkmtu);
4108 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4109 ((struct sockaddr *)to)->sa_family);
4111 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4118 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4119 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4124 struct mbuf *m, *m_at, *mp_last;
4125 struct sctp_nets *net;
4126 struct sctp_init_chunk *init;
4127 struct sctp_supported_addr_param *sup_addr;
4128 struct sctp_adaptation_layer_indication *ali;
4129 struct sctp_ecn_supported_param *ecn;
4130 struct sctp_prsctp_supported_param *prsctp;
4131 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4132 struct sctp_supported_chunk_types_param *pr_supported;
4133 int cnt_inits_to = 0;
4138 /* INIT's always go to the primary (and usually ONLY address) */
4140 net = stcb->asoc.primary_destination;
4142 net = TAILQ_FIRST(&stcb->asoc.nets);
4147 /* we confirm any address we send an INIT to */
4148 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4149 (void)sctp_set_primary_addr(stcb, NULL, net);
4151 /* we confirm any address we send an INIT to */
4152 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4154 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4156 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4158 * special hook, if we are sending to link local it will not
4159 * show up in our private address count.
4161 struct sockaddr_in6 *sin6l;
4163 sin6l = &net->ro._l_addr.sin6;
4164 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4168 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4169 /* This case should not happen */
4170 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4173 /* start the INIT timer */
4174 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4176 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4178 /* No memory, INIT timer will re-attempt. */
4179 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4182 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4184 * assume peer supports asconf in order to be able to queue local
4185 * address changes while an INIT is in flight and before the assoc
4188 stcb->asoc.peer_supports_asconf = 1;
4189 /* Now lets put the SCTP header in place */
4190 init = mtod(m, struct sctp_init_chunk *);
4191 /* now the chunk header */
4192 init->ch.chunk_type = SCTP_INITIATION;
4193 init->ch.chunk_flags = 0;
4194 /* fill in later from mbuf we build */
4195 init->ch.chunk_length = 0;
4196 /* place in my tag */
4197 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4198 /* set up some of the credits. */
4199 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4200 SCTP_MINIMAL_RWND));
4202 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4203 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4204 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4205 /* now the address restriction */
4206 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4208 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4210 /* we support 2 types: IPv6/IPv4 */
4211 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4212 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4213 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4215 /* we support 1 type: IPv4 */
4216 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4217 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4218 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4220 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4221 /* adaptation layer indication parameter */
4222 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4223 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4224 ali->ph.param_length = htons(sizeof(*ali));
4225 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4226 SCTP_BUF_LEN(m) += sizeof(*ali);
4227 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4229 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4230 /* Add NAT friendly parameter */
4231 struct sctp_paramhdr *ph;
4233 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4234 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4235 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4236 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4237 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4239 /* now any cookie time extensions */
4240 if (stcb->asoc.cookie_preserve_req) {
4241 struct sctp_cookie_perserve_param *cookie_preserve;
4243 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4244 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4245 cookie_preserve->ph.param_length = htons(
4246 sizeof(*cookie_preserve));
4247 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4248 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4249 ecn = (struct sctp_ecn_supported_param *)(
4250 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4251 stcb->asoc.cookie_preserve_req = 0;
4254 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4255 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4256 ecn->ph.param_length = htons(sizeof(*ecn));
4257 SCTP_BUF_LEN(m) += sizeof(*ecn);
4258 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4261 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4263 /* And now tell the peer we do pr-sctp */
4264 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4265 prsctp->ph.param_length = htons(sizeof(*prsctp));
4266 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4268 /* And now tell the peer we do all the extensions */
4269 pr_supported = (struct sctp_supported_chunk_types_param *)
4270 ((caddr_t)prsctp + sizeof(*prsctp));
4271 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4273 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4274 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4275 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4276 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4277 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4278 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4279 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4282 * EY if the initiator supports nr_sacks, need to report that to
4283 * responder in INIT chunk
4285 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4286 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4288 p_len = sizeof(*pr_supported) + num_ext;
4289 pr_supported->ph.param_length = htons(p_len);
4290 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4291 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4294 /* ECN nonce: And now tell the peer we support ECN nonce */
4295 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4296 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4297 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4298 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4299 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4300 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4302 /* add authentication parameters */
4303 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4304 struct sctp_auth_random *randp;
4305 struct sctp_auth_hmac_algo *hmacs;
4306 struct sctp_auth_chunk_list *chunks;
4308 /* attach RANDOM parameter, if available */
4309 if (stcb->asoc.authinfo.random != NULL) {
4310 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4311 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4312 /* random key already contains the header */
4313 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4314 /* zero out any padding required */
4315 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4316 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4318 /* add HMAC_ALGO parameter */
4319 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4320 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4321 (uint8_t *) hmacs->hmac_ids);
4323 p_len += sizeof(*hmacs);
4324 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4325 hmacs->ph.param_length = htons(p_len);
4326 /* zero out any padding required */
4327 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4328 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4330 /* add CHUNKS parameter */
4331 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4332 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4333 chunks->chunk_types);
4335 p_len += sizeof(*chunks);
4336 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4337 chunks->ph.param_length = htons(p_len);
4338 /* zero out any padding required */
4339 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4340 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4344 /* now the addresses */
4346 struct sctp_scoping scp;
4349 * To optimize this we could put the scoping stuff into a
4350 * structure and remove the individual uint8's from the
4351 * assoc structure. Then we could just sifa in the address
4352 * within the stcb.. but for now this is a quick hack to get
4353 * the address stuff teased apart.
4355 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4356 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4357 scp.loopback_scope = stcb->asoc.loopback_scope;
4358 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4359 scp.local_scope = stcb->asoc.local_scope;
4360 scp.site_scope = stcb->asoc.site_scope;
4362 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4365 /* calulate the size and update pkt header and chunk header */
4367 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4368 if (SCTP_BUF_NEXT(m_at) == NULL)
4370 p_len += SCTP_BUF_LEN(m_at);
4372 init->ch.chunk_length = htons(p_len);
4374 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4375 * here since the timer will drive a retranmission.
4378 /* I don't expect this to execute but we will be safe here */
4380 if ((padval) && (mp_last)) {
4382 * The compiler worries that mp_last may not be set even
4383 * though I think it is impossible :-> however we add
4384 * mp_last here just in case.
4386 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4388 /* Houston we have a problem, no space */
4394 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4395 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4396 (struct sockaddr *)&net->ro._l_addr,
4397 m, 0, NULL, 0, 0, 0, NULL, 0,
4398 inp->sctp_lport, stcb->rport, htonl(0),
4399 net->port, so_locked, NULL);
4400 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4401 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4402 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4406 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4407 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4410 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4411 * being equal to the beginning of the params i.e. (iphlen +
4412 * sizeof(struct sctp_init_msg) parse through the parameters to the
4413 * end of the mbuf verifying that all parameters are known.
4415 * For unknown parameters build and return a mbuf with
4416 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4417 * processing this chunk stop, and set *abort_processing to 1.
4419 * By having param_offset be pre-set to where parameters begin it is
4420 * hoped that this routine may be reused in the future by new
4423 struct sctp_paramhdr *phdr, params;
4425 struct mbuf *mat, *op_err;
4426 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4427 int at, limit, pad_needed;
4428 uint16_t ptype, plen, padded_size;
4431 *abort_processing = 0;
4434 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4437 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4438 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4439 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4440 ptype = ntohs(phdr->param_type);
4441 plen = ntohs(phdr->param_length);
4442 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4443 /* wacked parameter */
4444 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4447 limit -= SCTP_SIZE32(plen);
4449 * All parameters for all chunks that we know/understand are
4450 * listed here. We process them other places and make
4451 * appropriate stop actions per the upper bits. However this
4452 * is the generic routine processor's can call to get back
4453 * an operr.. to either incorporate (init-ack) or send.
4455 padded_size = SCTP_SIZE32(plen);
4457 /* Param's with variable size */
4458 case SCTP_HEARTBEAT_INFO:
4459 case SCTP_STATE_COOKIE:
4460 case SCTP_UNRECOG_PARAM:
4461 case SCTP_ERROR_CAUSE_IND:
4465 /* Param's with variable size within a range */
4466 case SCTP_CHUNK_LIST:
4467 case SCTP_SUPPORTED_CHUNK_EXT:
4468 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4469 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4474 case SCTP_SUPPORTED_ADDRTYPE:
4475 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4476 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4482 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4483 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4488 case SCTP_SET_PRIM_ADDR:
4489 case SCTP_DEL_IP_ADDRESS:
4490 case SCTP_ADD_IP_ADDRESS:
4491 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4492 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4493 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4498 /* Param's with a fixed size */
4499 case SCTP_IPV4_ADDRESS:
4500 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4501 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4506 case SCTP_IPV6_ADDRESS:
4507 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4508 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4513 case SCTP_COOKIE_PRESERVE:
4514 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4515 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4520 case SCTP_HAS_NAT_SUPPORT:
4523 case SCTP_ECN_NONCE_SUPPORTED:
4524 case SCTP_PRSCTP_SUPPORTED:
4526 if (padded_size != sizeof(struct sctp_paramhdr)) {
4527 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4532 case SCTP_ECN_CAPABLE:
4533 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4534 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4539 case SCTP_ULP_ADAPTATION:
4540 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4541 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4546 case SCTP_SUCCESS_REPORT:
4547 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4548 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4553 case SCTP_HOSTNAME_ADDRESS:
4555 /* We can NOT handle HOST NAME addresses!! */
4558 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4559 *abort_processing = 1;
4560 if (op_err == NULL) {
4561 /* Ok need to try to get a mbuf */
4563 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4565 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4568 l_len += sizeof(struct sctp_paramhdr);
4569 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4571 SCTP_BUF_LEN(op_err) = 0;
4573 * pre-reserve space for ip
4574 * and sctp header and
4578 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4580 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4582 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4583 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4587 /* If we have space */
4588 struct sctp_paramhdr s;
4591 uint32_t cpthis = 0;
4593 pad_needed = 4 - (err_at % 4);
4594 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4595 err_at += pad_needed;
4597 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4598 s.param_length = htons(sizeof(s) + plen);
4599 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4600 err_at += sizeof(s);
4601 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4603 sctp_m_freem(op_err);
4605 * we are out of memory but
4606 * we still need to have a
4607 * look at what to do (the
4608 * system is in trouble
4613 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4621 * we do not recognize the parameter figure out what
4624 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4625 if ((ptype & 0x4000) == 0x4000) {
4626 /* Report bit is set?? */
4627 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4628 if (op_err == NULL) {
4631 /* Ok need to try to get an mbuf */
4633 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4635 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4638 l_len += sizeof(struct sctp_paramhdr);
4639 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4641 SCTP_BUF_LEN(op_err) = 0;
4643 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4645 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4647 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4648 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4652 /* If we have space */
4653 struct sctp_paramhdr s;
4656 uint32_t cpthis = 0;
4658 pad_needed = 4 - (err_at % 4);
4659 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4660 err_at += pad_needed;
4662 s.param_type = htons(SCTP_UNRECOG_PARAM);
4663 s.param_length = htons(sizeof(s) + plen);
4664 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4665 err_at += sizeof(s);
4666 if (plen > sizeof(tempbuf)) {
4667 plen = sizeof(tempbuf);
4669 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4671 sctp_m_freem(op_err);
4673 * we are out of memory but
4674 * we still need to have a
4675 * look at what to do (the
4676 * system is in trouble
4680 goto more_processing;
4682 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4687 if ((ptype & 0x8000) == 0x0000) {
4688 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4691 /* skip this chunk and continue processing */
4692 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4693 at += SCTP_SIZE32(plen);
4698 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4702 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4703 *abort_processing = 1;
4704 if ((op_err == NULL) && phdr) {
4708 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4710 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4712 l_len += (2 * sizeof(struct sctp_paramhdr));
4713 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4715 SCTP_BUF_LEN(op_err) = 0;
4717 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4719 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4721 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4722 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4725 if ((op_err) && phdr) {
4726 struct sctp_paramhdr s;
4729 uint32_t cpthis = 0;
4731 pad_needed = 4 - (err_at % 4);
4732 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4733 err_at += pad_needed;
4735 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4736 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4737 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4738 err_at += sizeof(s);
4739 /* Only copy back the p-hdr that caused the issue */
4740 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4746 sctp_are_there_new_addresses(struct sctp_association *asoc,
4747 struct mbuf *in_initpkt, int iphlen, int offset)
4750 * Given a INIT packet, look through the packet to verify that there
4751 * are NO new addresses. As we go through the parameters add reports
4752 * of any un-understood parameters that require an error. Also we
4753 * must return (1) to drop the packet if we see a un-understood
4754 * parameter that tells us to drop the chunk.
4756 struct sockaddr_in sin4, *sa4;
4759 struct sockaddr_in6 sin6, *sa6;
4762 struct sockaddr *sa_touse;
4763 struct sockaddr *sa;
4764 struct sctp_paramhdr *phdr, params;
4768 struct ip6_hdr *ip6h;
4772 uint16_t ptype, plen;
4775 struct sctp_nets *net;
4777 memset(&sin4, 0, sizeof(sin4));
4779 memset(&sin6, 0, sizeof(sin6));
4781 sin4.sin_family = AF_INET;
4782 sin4.sin_len = sizeof(sin4);
4784 sin6.sin6_family = AF_INET6;
4785 sin6.sin6_len = sizeof(sin6);
4788 /* First what about the src address of the pkt ? */
4789 iph = mtod(in_initpkt, struct ip *);
4790 switch (iph->ip_v) {
4792 /* source addr is IPv4 */
4793 sin4.sin_addr = iph->ip_src;
4794 sa_touse = (struct sockaddr *)&sin4;
4797 case IPV6_VERSION >> 4:
4798 /* source addr is IPv6 */
4799 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4800 sin6.sin6_addr = ip6h->ip6_src;
4801 sa_touse = (struct sockaddr *)&sin6;
4809 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4810 sa = (struct sockaddr *)&net->ro._l_addr;
4811 if (sa->sa_family == sa_touse->sa_family) {
4812 if (sa->sa_family == AF_INET) {
4813 sa4 = (struct sockaddr_in *)sa;
4814 if (sa4->sin_addr.s_addr ==
4815 sin4.sin_addr.s_addr) {
4821 if (sa->sa_family == AF_INET6) {
4822 sa6 = (struct sockaddr_in6 *)sa;
4823 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4833 /* New address added! no need to look futher. */
4836 /* Ok so far lets munge through the rest of the packet */
4840 offset += sizeof(struct sctp_init_chunk);
4841 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4843 ptype = ntohs(phdr->param_type);
4844 plen = ntohs(phdr->param_length);
4845 if (ptype == SCTP_IPV4_ADDRESS) {
4846 struct sctp_ipv4addr_param *p4, p4_buf;
4848 phdr = sctp_get_next_param(mat, offset,
4849 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4850 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4854 p4 = (struct sctp_ipv4addr_param *)phdr;
4855 sin4.sin_addr.s_addr = p4->addr;
4856 sa_touse = (struct sockaddr *)&sin4;
4857 } else if (ptype == SCTP_IPV6_ADDRESS) {
4858 struct sctp_ipv6addr_param *p6, p6_buf;
4860 phdr = sctp_get_next_param(mat, offset,
4861 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4862 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4866 p6 = (struct sctp_ipv6addr_param *)phdr;
4868 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4871 sa_touse = (struct sockaddr *)&sin4;
4874 /* ok, sa_touse points to one to check */
4876 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4877 sa = (struct sockaddr *)&net->ro._l_addr;
4878 if (sa->sa_family != sa_touse->sa_family) {
4881 if (sa->sa_family == AF_INET) {
4882 sa4 = (struct sockaddr_in *)sa;
4883 if (sa4->sin_addr.s_addr ==
4884 sin4.sin_addr.s_addr) {
4890 if (sa->sa_family == AF_INET6) {
4891 sa6 = (struct sockaddr_in6 *)sa;
4892 if (SCTP6_ARE_ADDR_EQUAL(
4901 /* New addr added! no need to look further */
4905 offset += SCTP_SIZE32(plen);
4906 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4912 * Given a MBUF chain that was sent into us containing an INIT. Build a
4913 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4914 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4915 * message (i.e. the struct sctp_init_msg).
4918 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4919 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4920 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4922 struct sctp_association *asoc;
4923 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4924 struct sctp_init_ack_chunk *initack;
4925 struct sctp_adaptation_layer_indication *ali;
4926 struct sctp_ecn_supported_param *ecn;
4927 struct sctp_prsctp_supported_param *prsctp;
4928 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4929 struct sctp_supported_chunk_types_param *pr_supported;
4930 union sctp_sockstore store, store1, *over_addr;
4931 struct sockaddr_in *sin, *to_sin;
4934 struct sockaddr_in6 *sin6, *to_sin6;
4940 struct ip6_hdr *ip6;
4943 struct sockaddr *to;
4944 struct sctp_state_cookie stc;
4945 struct sctp_nets *net = NULL;
4946 uint8_t *signature = NULL;
4947 int cnt_inits_to = 0;
4948 uint16_t his_limit, i_want;
4949 int abort_flag, padval;
4952 int nat_friendly = 0;
4960 if ((asoc != NULL) &&
4961 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4962 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4963 /* new addresses, out of here in non-cookie-wait states */
4965 * Send a ABORT, we don't add the new address error clause
4966 * though we even set the T bit and copy in the 0 tag.. this
4967 * looks no different than if no listener was present.
4969 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4973 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4974 (offset + sizeof(struct sctp_init_chunk)),
4975 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4978 sctp_send_abort(init_pkt, iphlen, sh,
4979 init_chk->init.initiate_tag, op_err, vrf_id, port);
4982 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4984 /* No memory, INIT timer will re-attempt. */
4986 sctp_m_freem(op_err);
4989 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4991 /* the time I built cookie */
4992 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4994 /* populate any tie tags */
4996 /* unlock before tag selections */
4997 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4998 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4999 stc.cookie_life = asoc->cookie_life;
5000 net = asoc->primary_destination;
5002 stc.tie_tag_my_vtag = 0;
5003 stc.tie_tag_peer_vtag = 0;
5004 /* life I will award this cookie */
5005 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5008 /* copy in the ports for later check */
5009 stc.myport = sh->dest_port;
5010 stc.peerport = sh->src_port;
5013 * If we wanted to honor cookie life extentions, we would add to
5014 * stc.cookie_life. For now we should NOT honor any extension
5016 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5017 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5018 struct inpcb *in_inp;
5020 /* Its a V6 socket */
5021 in_inp = (struct inpcb *)inp;
5022 stc.ipv6_addr_legal = 1;
5023 /* Now look at the binding flag to see if V4 will be legal */
5024 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5025 stc.ipv4_addr_legal = 1;
5027 /* V4 addresses are NOT legal on the association */
5028 stc.ipv4_addr_legal = 0;
5031 /* Its a V4 socket, no - V6 */
5032 stc.ipv4_addr_legal = 1;
5033 stc.ipv6_addr_legal = 0;
5036 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5041 /* now for scope setup */
5042 memset((caddr_t)&store, 0, sizeof(store));
5043 memset((caddr_t)&store1, 0, sizeof(store1));
5045 to_sin = &store1.sin;
5048 to_sin6 = &store1.sin6;
5050 iph = mtod(init_pkt, struct ip *);
5051 /* establish the to_addr's */
5052 switch (iph->ip_v) {
5054 to_sin->sin_port = sh->dest_port;
5055 to_sin->sin_family = AF_INET;
5056 to_sin->sin_len = sizeof(struct sockaddr_in);
5057 to_sin->sin_addr = iph->ip_dst;
5060 case IPV6_VERSION >> 4:
5061 ip6 = mtod(init_pkt, struct ip6_hdr *);
5062 to_sin6->sin6_addr = ip6->ip6_dst;
5063 to_sin6->sin6_scope_id = 0;
5064 to_sin6->sin6_port = sh->dest_port;
5065 to_sin6->sin6_family = AF_INET6;
5066 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5075 to = (struct sockaddr *)&store;
5076 switch (iph->ip_v) {
5079 sin->sin_family = AF_INET;
5080 sin->sin_len = sizeof(struct sockaddr_in);
5081 sin->sin_port = sh->src_port;
5082 sin->sin_addr = iph->ip_src;
5083 /* lookup address */
5084 stc.address[0] = sin->sin_addr.s_addr;
5088 stc.addr_type = SCTP_IPV4_ADDRESS;
5089 /* local from address */
5090 stc.laddress[0] = to_sin->sin_addr.s_addr;
5091 stc.laddress[1] = 0;
5092 stc.laddress[2] = 0;
5093 stc.laddress[3] = 0;
5094 stc.laddr_type = SCTP_IPV4_ADDRESS;
5095 /* scope_id is only for v6 */
5097 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5098 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5103 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5104 /* Must use the address in this case */
5105 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5106 stc.loopback_scope = 1;
5109 stc.local_scope = 0;
5114 case IPV6_VERSION >> 4:
5116 ip6 = mtod(init_pkt, struct ip6_hdr *);
5117 sin6->sin6_family = AF_INET6;
5118 sin6->sin6_len = sizeof(struct sockaddr_in6);
5119 sin6->sin6_port = sh->src_port;
5120 sin6->sin6_addr = ip6->ip6_src;
5121 /* lookup address */
5122 memcpy(&stc.address, &sin6->sin6_addr,
5123 sizeof(struct in6_addr));
5124 sin6->sin6_scope_id = 0;
5125 stc.addr_type = SCTP_IPV6_ADDRESS;
5127 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5129 * FIX ME: does this have scope from
5132 (void)sa6_recoverscope(sin6);
5133 stc.scope_id = sin6->sin6_scope_id;
5134 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5135 stc.loopback_scope = 1;
5136 stc.local_scope = 0;
5139 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5141 * If the new destination is a
5142 * LINK_LOCAL we must have common
5143 * both site and local scope. Don't
5144 * set local scope though since we
5145 * must depend on the source to be
5146 * added implicitly. We cannot
5147 * assure just because we share one
5148 * link that all links are common.
5150 stc.local_scope = 0;
5154 * we start counting for the private
5155 * address stuff at 1. since the
5156 * link local we source from won't
5157 * show up in our scoped count.
5161 * pull out the scope_id from
5165 * FIX ME: does this have scope from
5168 (void)sa6_recoverscope(sin6);
5169 stc.scope_id = sin6->sin6_scope_id;
5170 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5171 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5173 * If the new destination is
5174 * SITE_LOCAL then we must have site
5179 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5180 stc.laddr_type = SCTP_IPV6_ADDRESS;
5190 /* set the scope per the existing tcb */
5193 struct sctp_nets *lnet;
5197 stc.loopback_scope = asoc->loopback_scope;
5198 stc.ipv4_scope = asoc->ipv4_local_scope;
5199 stc.site_scope = asoc->site_scope;
5200 stc.local_scope = asoc->local_scope;
5202 /* Why do we not consider IPv4 LL addresses? */
5203 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5204 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5205 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5207 * if we have a LL address, start
5215 /* use the net pointer */
5216 to = (struct sockaddr *)&net->ro._l_addr;
5217 switch (to->sa_family) {
5219 sin = (struct sockaddr_in *)to;
5220 stc.address[0] = sin->sin_addr.s_addr;
5224 stc.addr_type = SCTP_IPV4_ADDRESS;
5225 if (net->src_addr_selected == 0) {
5227 * strange case here, the INIT should have
5228 * did the selection.
5230 net->ro._s_addr = sctp_source_address_selection(inp,
5231 stcb, (sctp_route_t *) & net->ro,
5233 if (net->ro._s_addr == NULL)
5236 net->src_addr_selected = 1;
5239 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5240 stc.laddress[1] = 0;
5241 stc.laddress[2] = 0;
5242 stc.laddress[3] = 0;
5243 stc.laddr_type = SCTP_IPV4_ADDRESS;
5247 sin6 = (struct sockaddr_in6 *)to;
5248 memcpy(&stc.address, &sin6->sin6_addr,
5249 sizeof(struct in6_addr));
5250 stc.addr_type = SCTP_IPV6_ADDRESS;
5251 if (net->src_addr_selected == 0) {
5253 * strange case here, the INIT should have
5254 * did the selection.
5256 net->ro._s_addr = sctp_source_address_selection(inp,
5257 stcb, (sctp_route_t *) & net->ro,
5259 if (net->ro._s_addr == NULL)
5262 net->src_addr_selected = 1;
5264 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5265 sizeof(struct in6_addr));
5266 stc.laddr_type = SCTP_IPV6_ADDRESS;
5271 /* Now lets put the SCTP header in place */
5272 initack = mtod(m, struct sctp_init_ack_chunk *);
5273 /* Save it off for quick ref */
5274 stc.peers_vtag = init_chk->init.initiate_tag;
5276 memcpy(stc.identification, SCTP_VERSION_STRING,
5277 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5278 /* now the chunk header */
5279 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5280 initack->ch.chunk_flags = 0;
5281 /* fill in later from mbuf we build */
5282 initack->ch.chunk_length = 0;
5283 /* place in my tag */
5284 if ((asoc != NULL) &&
5285 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5286 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5287 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5288 /* re-use the v-tags and init-seq here */
5289 initack->init.initiate_tag = htonl(asoc->my_vtag);
5290 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5292 uint32_t vtag, itsn;
5294 if (hold_inp_lock) {
5295 SCTP_INP_INCR_REF(inp);
5296 SCTP_INP_RUNLOCK(inp);
5299 atomic_add_int(&asoc->refcnt, 1);
5300 SCTP_TCB_UNLOCK(stcb);
5302 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5303 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5305 * Got a duplicate vtag on some guy behind a
5306 * nat make sure we don't use it.
5310 initack->init.initiate_tag = htonl(vtag);
5311 /* get a TSN to use too */
5312 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5313 initack->init.initial_tsn = htonl(itsn);
5314 SCTP_TCB_LOCK(stcb);
5315 atomic_add_int(&asoc->refcnt, -1);
5317 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5318 initack->init.initiate_tag = htonl(vtag);
5319 /* get a TSN to use too */
5320 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5322 if (hold_inp_lock) {
5323 SCTP_INP_RLOCK(inp);
5324 SCTP_INP_DECR_REF(inp);
5327 /* save away my tag to */
5328 stc.my_vtag = initack->init.initiate_tag;
5330 /* set up some of the credits. */
5331 so = inp->sctp_socket;
5333 /* memory problem */
5337 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5339 /* set what I want */
5340 his_limit = ntohs(init_chk->init.num_inbound_streams);
5341 /* choose what I want */
5343 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5344 i_want = asoc->streamoutcnt;
5346 i_want = inp->sctp_ep.pre_open_stream_count;
5349 i_want = inp->sctp_ep.pre_open_stream_count;
5351 if (his_limit < i_want) {
5352 /* I Want more :< */
5353 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5355 /* I can have what I want :> */
5356 initack->init.num_outbound_streams = htons(i_want);
5358 /* tell him his limt. */
5359 initack->init.num_inbound_streams =
5360 htons(inp->sctp_ep.max_open_streams_intome);
5362 /* adaptation layer indication parameter */
5363 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5364 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5365 ali->ph.param_length = htons(sizeof(*ali));
5366 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5367 SCTP_BUF_LEN(m) += sizeof(*ali);
5368 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5371 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5372 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5373 ecn->ph.param_length = htons(sizeof(*ecn));
5374 SCTP_BUF_LEN(m) += sizeof(*ecn);
5376 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5379 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5381 /* And now tell the peer we do pr-sctp */
5382 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5383 prsctp->ph.param_length = htons(sizeof(*prsctp));
5384 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5386 /* Add NAT friendly parameter */
5387 struct sctp_paramhdr *ph;
5389 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5390 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5391 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5392 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5394 /* And now tell the peer we do all the extensions */
5395 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5396 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5398 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5399 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5400 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5401 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5402 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5403 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5404 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5406 * EY if the sysctl variable is set, tell the assoc. initiator that
5409 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5410 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5411 p_len = sizeof(*pr_supported) + num_ext;
5412 pr_supported->ph.param_length = htons(p_len);
5413 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5414 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5416 /* ECN nonce: And now tell the peer we support ECN nonce */
5417 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5418 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5419 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5420 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5421 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5422 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5424 /* add authentication parameters */
5425 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5426 struct sctp_auth_random *randp;
5427 struct sctp_auth_hmac_algo *hmacs;
5428 struct sctp_auth_chunk_list *chunks;
5429 uint16_t random_len;
5431 /* generate and add RANDOM parameter */
5432 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5433 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5434 randp->ph.param_type = htons(SCTP_RANDOM);
5435 p_len = sizeof(*randp) + random_len;
5436 randp->ph.param_length = htons(p_len);
5437 SCTP_READ_RANDOM(randp->random_data, random_len);
5438 /* zero out any padding required */
5439 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5440 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5442 /* add HMAC_ALGO parameter */
5443 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5444 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5445 (uint8_t *) hmacs->hmac_ids);
5447 p_len += sizeof(*hmacs);
5448 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5449 hmacs->ph.param_length = htons(p_len);
5450 /* zero out any padding required */
5451 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5452 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5454 /* add CHUNKS parameter */
5455 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5456 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5457 chunks->chunk_types);
5459 p_len += sizeof(*chunks);
5460 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5461 chunks->ph.param_length = htons(p_len);
5462 /* zero out any padding required */
5463 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5464 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5468 /* now the addresses */
5470 struct sctp_scoping scp;
5473 * To optimize this we could put the scoping stuff into a
5474 * structure and remove the individual uint8's from the stc
5475 * structure. Then we could just sifa in the address within
5476 * the stc.. but for now this is a quick hack to get the
5477 * address stuff teased apart.
5479 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5480 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5481 scp.loopback_scope = stc.loopback_scope;
5482 scp.ipv4_local_scope = stc.ipv4_scope;
5483 scp.local_scope = stc.local_scope;
5484 scp.site_scope = stc.site_scope;
5485 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5488 /* tack on the operational error if present */
5496 llen += SCTP_BUF_LEN(ol);
5497 ol = SCTP_BUF_NEXT(ol);
5500 /* must add a pad to the param */
5501 uint32_t cpthis = 0;
5504 padlen = 4 - (llen % 4);
5505 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5507 while (SCTP_BUF_NEXT(m_at) != NULL) {
5508 m_at = SCTP_BUF_NEXT(m_at);
5510 SCTP_BUF_NEXT(m_at) = op_err;
5511 while (SCTP_BUF_NEXT(m_at) != NULL) {
5512 m_at = SCTP_BUF_NEXT(m_at);
5515 /* pre-calulate the size and update pkt header and chunk header */
5517 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5518 p_len += SCTP_BUF_LEN(m_tmp);
5519 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5520 /* m_tmp should now point to last one */
5525 /* Now we must build a cookie */
5526 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5527 if (m_cookie == NULL) {
5528 /* memory problem */
5532 /* Now append the cookie to the end and update the space/size */
5533 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5535 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5536 p_len += SCTP_BUF_LEN(m_tmp);
5537 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5538 /* m_tmp should now point to last one */
5544 * Place in the size, but we don't include the last pad (if any) in
5547 initack->ch.chunk_length = htons(p_len);
5550 * Time to sign the cookie, we don't sign over the cookie signature
5551 * though thus we set trailer.
5553 (void)sctp_hmac_m(SCTP_HMAC,
5554 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5555 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5556 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5558 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5559 * here since the timer will drive a retranmission.
5562 if ((padval) && (mp_last)) {
5563 /* see my previous comments on mp_last */
5566 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5568 /* Houston we have a problem, no space */
5574 if (stc.loopback_scope) {
5575 over_addr = &store1;
5580 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5582 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5583 port, SCTP_SO_NOT_LOCKED, over_addr);
5584 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5589 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5590 struct sctp_association *asoc,
5591 struct sctp_stream_out *strq, int holds_lock)
5593 if (holds_lock == 0) {
5594 SCTP_TCB_SEND_LOCK(stcb);
5596 if ((strq->next_spoke.tqe_next == NULL) &&
5597 (strq->next_spoke.tqe_prev == NULL)) {
5598 TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5600 if (holds_lock == 0) {
5601 SCTP_TCB_SEND_UNLOCK(stcb);
5606 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5607 struct sctp_association *asoc,
5608 struct sctp_stream_out *strq,
5611 /* take off and then setup so we know it is not on the wheel */
5612 if (holds_lock == 0) {
5613 SCTP_TCB_SEND_LOCK(stcb);
5615 if (TAILQ_EMPTY(&strq->outqueue)) {
5616 if (asoc->last_out_stream == strq) {
5617 asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5618 if (asoc->last_out_stream == NULL) {
5619 asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5621 if (asoc->last_out_stream == strq) {
5622 asoc->last_out_stream = NULL;
5625 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5626 strq->next_spoke.tqe_next = NULL;
5627 strq->next_spoke.tqe_prev = NULL;
5629 if (holds_lock == 0) {
5630 SCTP_TCB_SEND_UNLOCK(stcb);
5635 sctp_prune_prsctp(struct sctp_tcb *stcb,
5636 struct sctp_association *asoc,
5637 struct sctp_sndrcvinfo *srcv,
5641 struct sctp_tmit_chunk *chk, *nchk;
5643 SCTP_TCB_LOCK_ASSERT(stcb);
5644 if ((asoc->peer_supports_prsctp) &&
5645 (asoc->sent_queue_cnt_removeable > 0)) {
5646 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5648 * Look for chunks marked with the PR_SCTP flag AND
5649 * the buffer space flag. If the one being sent is
5650 * equal or greater priority then purge the old one
5651 * and free some space.
5653 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5655 * This one is PR-SCTP AND buffer space
5658 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5660 * Lower numbers equates to higher
5661 * priority so if the one we are
5662 * looking at has a larger or equal
5663 * priority we want to drop the data
5664 * and NOT retransmit it.
5668 * We release the book_size
5669 * if the mbuf is here
5674 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5675 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5677 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5678 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5681 freed_spc += ret_spc;
5682 if (freed_spc >= dataout) {
5685 } /* if chunk was present */
5686 } /* if of sufficent priority */
5687 } /* if chunk has enabled */
5688 } /* tailqforeach */
5690 chk = TAILQ_FIRST(&asoc->send_queue);
5692 nchk = TAILQ_NEXT(chk, sctp_next);
5693 /* Here we must move to the sent queue and mark */
5694 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5695 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5698 * We release the book_size
5699 * if the mbuf is here
5703 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5704 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5707 freed_spc += ret_spc;
5708 if (freed_spc >= dataout) {
5711 } /* end if chk->data */
5712 } /* end if right class */
5713 } /* end if chk pr-sctp */
5715 } /* end while (chk) */
5716 } /* if enabled in asoc */
5720 sctp_get_frag_point(struct sctp_tcb *stcb,
5721 struct sctp_association *asoc)
5726 * For endpoints that have both v6 and v4 addresses we must reserve
5727 * room for the ipv6 header, for those that are only dealing with V4
5728 * we use a larger frag point.
5730 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5731 ovh = SCTP_MED_OVERHEAD;
5733 ovh = SCTP_MED_V4_OVERHEAD;
5736 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5737 siz = asoc->smallest_mtu - ovh;
5739 siz = (stcb->asoc.sctp_frag_point - ovh);
5741 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5743 /* A data chunk MUST fit in a cluster */
5744 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5747 /* adjust for an AUTH chunk if DATA requires auth */
5748 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5749 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5752 /* make it an even word boundary please */
5759 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5763 * We assume that the user wants PR_SCTP_TTL if the user provides a
5764 * positive lifetime but does not specify any PR_SCTP policy. This
5765 * is a BAD assumption and causes problems at least with the
5766 * U-Vancovers MPI folks. I will change this to be no policy means
5769 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5770 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5775 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5776 case CHUNK_FLAGS_PR_SCTP_BUF:
5778 * Time to live is a priority stored in tv_sec when doing
5779 * the buffer drop thing.
5781 sp->ts.tv_sec = sp->timetolive;
5784 case CHUNK_FLAGS_PR_SCTP_TTL:
5788 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5789 tv.tv_sec = sp->timetolive / 1000;
5790 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5792 * TODO sctp_constants.h needs alternative time
5793 * macros when _KERNEL is undefined.
5795 timevaladd(&sp->ts, &tv);
5798 case CHUNK_FLAGS_PR_SCTP_RTX:
5800 * Time to live is a the number or retransmissions stored in
5803 sp->ts.tv_sec = sp->timetolive;
5807 SCTPDBG(SCTP_DEBUG_USRREQ1,
5808 "Unknown PR_SCTP policy %u.\n",
5809 PR_SCTP_POLICY(sp->sinfo_flags));
5815 sctp_msg_append(struct sctp_tcb *stcb,
5816 struct sctp_nets *net,
5818 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5820 int error = 0, holds_lock;
5822 struct sctp_stream_queue_pending *sp = NULL;
5823 struct sctp_stream_out *strm;
5826 * Given an mbuf chain, put it into the association send queue and
5827 * place it on the wheel
5829 holds_lock = hold_stcb_lock;
5830 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5831 /* Invalid stream number */
5832 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5836 if ((stcb->asoc.stream_locked) &&
5837 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5838 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5842 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5843 /* Now can we send this? */
5844 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5845 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5846 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5847 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5848 /* got data while shutting down */
5849 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5853 sctp_alloc_a_strmoq(stcb, sp);
5855 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5859 sp->sinfo_flags = srcv->sinfo_flags;
5860 sp->timetolive = srcv->sinfo_timetolive;
5861 sp->ppid = srcv->sinfo_ppid;
5862 sp->context = srcv->sinfo_context;
5864 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5867 sp->net = stcb->asoc.primary_destination;
5869 atomic_add_int(&sp->net->ref_count, 1);
5870 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5871 sp->stream = srcv->sinfo_stream;
5872 sp->msg_is_complete = 1;
5873 sp->sender_all_done = 1;
5876 sp->tail_mbuf = NULL;
5879 sctp_set_prsctp_policy(sp);
5881 * We could in theory (for sendall) sifa the length in, but we would
5882 * still have to hunt through the chain since we need to setup the
5886 if (SCTP_BUF_NEXT(at) == NULL)
5888 sp->length += SCTP_BUF_LEN(at);
5889 at = SCTP_BUF_NEXT(at);
5891 SCTP_TCB_SEND_LOCK(stcb);
5892 sctp_snd_sb_alloc(stcb, sp->length);
5893 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5894 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5895 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5896 sp->strseq = strm->next_sequence_sent;
5897 strm->next_sequence_sent++;
5899 if ((strm->next_spoke.tqe_next == NULL) &&
5900 (strm->next_spoke.tqe_prev == NULL)) {
5901 /* Not on wheel, insert */
5902 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5905 SCTP_TCB_SEND_UNLOCK(stcb);
5914 static struct mbuf *
5915 sctp_copy_mbufchain(struct mbuf *clonechain,
5916 struct mbuf *outchain,
5917 struct mbuf **endofchain,
5920 uint8_t copy_by_ref)
5923 struct mbuf *appendchain;
5927 if (endofchain == NULL) {
5931 sctp_m_freem(outchain);
5934 if (can_take_mbuf) {
5935 appendchain = clonechain;
5938 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5940 /* Its not in a cluster */
5941 if (*endofchain == NULL) {
5942 /* lets get a mbuf cluster */
5943 if (outchain == NULL) {
5944 /* This is the general case */
5946 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5947 if (outchain == NULL) {
5950 SCTP_BUF_LEN(outchain) = 0;
5951 *endofchain = outchain;
5952 /* get the prepend space */
5953 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5956 * We really should not get a NULL
5962 if (SCTP_BUF_NEXT(m) == NULL) {
5966 m = SCTP_BUF_NEXT(m);
5969 if (*endofchain == NULL) {
5971 * huh, TSNH XXX maybe we
5974 sctp_m_freem(outchain);
5978 /* get the new end of length */
5979 len = M_TRAILINGSPACE(*endofchain);
5981 /* how much is left at the end? */
5982 len = M_TRAILINGSPACE(*endofchain);
5984 /* Find the end of the data, for appending */
5985 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5987 /* Now lets copy it out */
5988 if (len >= sizeofcpy) {
5989 /* It all fits, copy it in */
5990 m_copydata(clonechain, 0, sizeofcpy, cp);
5991 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5993 /* fill up the end of the chain */
5995 m_copydata(clonechain, 0, len, cp);
5996 SCTP_BUF_LEN((*endofchain)) += len;
5997 /* now we need another one */
6000 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6005 SCTP_BUF_NEXT((*endofchain)) = m;
6007 cp = mtod((*endofchain), caddr_t);
6008 m_copydata(clonechain, len, sizeofcpy, cp);
6009 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6013 /* copy the old fashion way */
6014 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6015 #ifdef SCTP_MBUF_LOGGING
6016 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6021 if (SCTP_BUF_IS_EXTENDED(mat)) {
6022 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6024 mat = SCTP_BUF_NEXT(mat);
6030 if (appendchain == NULL) {
6033 sctp_m_freem(outchain);
6037 /* tack on to the end */
6038 if (*endofchain != NULL) {
6039 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6043 if (SCTP_BUF_NEXT(m) == NULL) {
6044 SCTP_BUF_NEXT(m) = appendchain;
6047 m = SCTP_BUF_NEXT(m);
6051 * save off the end and update the end-chain postion
6055 if (SCTP_BUF_NEXT(m) == NULL) {
6059 m = SCTP_BUF_NEXT(m);
6063 /* save off the end and update the end-chain postion */
6066 if (SCTP_BUF_NEXT(m) == NULL) {
6070 m = SCTP_BUF_NEXT(m);
6072 return (appendchain);
6077 sctp_med_chunk_output(struct sctp_inpcb *inp,
6078 struct sctp_tcb *stcb,
6079 struct sctp_association *asoc,
6082 int control_only, int from_where,
6083 struct timeval *now, int *now_filled, int frag_point, int so_locked
6084 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6090 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6093 struct sctp_copy_all *ca;
6096 int added_control = 0;
6097 int un_sent, do_chunk_output = 1;
6098 struct sctp_association *asoc;
6100 ca = (struct sctp_copy_all *)ptr;
6101 if (ca->m == NULL) {
6104 if (ca->inp != inp) {
6108 if ((ca->m) && ca->sndlen) {
6109 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6111 /* can't copy so we are done */
6115 #ifdef SCTP_MBUF_LOGGING
6116 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6121 if (SCTP_BUF_IS_EXTENDED(mat)) {
6122 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6124 mat = SCTP_BUF_NEXT(mat);
6131 SCTP_TCB_LOCK_ASSERT(stcb);
6132 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6133 /* Abort this assoc with m as the user defined reason */
6135 struct sctp_paramhdr *ph;
6137 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6139 ph = mtod(m, struct sctp_paramhdr *);
6140 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6141 ph->param_length = htons(ca->sndlen);
6144 * We add one here to keep the assoc from
6145 * dis-appearing on us.
6147 atomic_add_int(&stcb->asoc.refcnt, 1);
6148 sctp_abort_an_association(inp, stcb,
6149 SCTP_RESPONSE_TO_USER_REQ,
6150 m, SCTP_SO_NOT_LOCKED);
6152 * sctp_abort_an_association calls sctp_free_asoc()
6153 * free association will NOT free it since we
6154 * incremented the refcnt .. we do this to prevent
6155 * it being freed and things getting tricky since we
6156 * could end up (from free_asoc) calling inpcb_free
6157 * which would get a recursive lock call to the
6158 * iterator lock.. But as a consequence of that the
6159 * stcb will return to us un-locked.. since
6160 * free_asoc returns with either no TCB or the TCB
6161 * unlocked, we must relock.. to unlock in the
6162 * iterator timer :-0
6164 SCTP_TCB_LOCK(stcb);
6165 atomic_add_int(&stcb->asoc.refcnt, -1);
6166 goto no_chunk_output;
6170 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6174 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6175 /* shutdown this assoc */
6178 cnt = sctp_is_there_unsent_data(stcb);
6180 if (TAILQ_EMPTY(&asoc->send_queue) &&
6181 TAILQ_EMPTY(&asoc->sent_queue) &&
6183 if (asoc->locked_on_sending) {
6187 * there is nothing queued to send, so I'm
6190 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6191 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6192 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6194 * only send SHUTDOWN the first time
6197 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6198 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6199 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6201 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6202 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6203 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6204 asoc->primary_destination);
6205 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6206 asoc->primary_destination);
6208 do_chunk_output = 0;
6212 * we still got (or just got) data to send,
6213 * so set SHUTDOWN_PENDING
6216 * XXX sockets draft says that SCTP_EOF
6217 * should be sent with no data. currently,
6218 * we will allow user data to be sent first
6219 * and move to SHUTDOWN-PENDING
6221 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6222 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6223 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6224 if (asoc->locked_on_sending) {
6226 * Locked to send out the
6229 struct sctp_stream_queue_pending *sp;
6231 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6233 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6234 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6237 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6238 if (TAILQ_EMPTY(&asoc->send_queue) &&
6239 TAILQ_EMPTY(&asoc->sent_queue) &&
6240 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6242 atomic_add_int(&stcb->asoc.refcnt, 1);
6243 sctp_abort_an_association(stcb->sctp_ep, stcb,
6244 SCTP_RESPONSE_TO_USER_REQ,
6245 NULL, SCTP_SO_NOT_LOCKED);
6246 atomic_add_int(&stcb->asoc.refcnt, -1);
6247 goto no_chunk_output;
6249 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6250 asoc->primary_destination);
6256 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6257 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6259 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6260 (stcb->asoc.total_flight > 0) &&
6261 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6263 do_chunk_output = 0;
6265 if (do_chunk_output)
6266 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6267 else if (added_control) {
6268 int num_out = 0, reason = 0, now_filled = 0;
6272 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6273 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6274 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6285 sctp_sendall_completes(void *ptr, uint32_t val)
6287 struct sctp_copy_all *ca;
6289 ca = (struct sctp_copy_all *)ptr;
6291 * Do a notify here? Kacheong suggests that the notify be done at
6292 * the send time.. so you would push up a notification if any send
6293 * failed. Don't know if this is feasable since the only failures we
6294 * have is "memory" related and if you cannot get an mbuf to send
6295 * the data you surely can't get an mbuf to send up to notify the
6296 * user you can't send the data :->
6299 /* now free everything */
6300 sctp_m_freem(ca->m);
6301 SCTP_FREE(ca, SCTP_M_COPYAL);
6305 #define MC_ALIGN(m, len) do { \
6306 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6311 static struct mbuf *
6312 sctp_copy_out_all(struct uio *uio, int len)
6314 struct mbuf *ret, *at;
6315 int left, willcpy, cancpy, error;
6317 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6323 SCTP_BUF_LEN(ret) = 0;
6324 /* save space for the data chunk header */
6325 cancpy = M_TRAILINGSPACE(ret);
6326 willcpy = min(cancpy, left);
6329 /* Align data to the end */
6330 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6336 SCTP_BUF_LEN(at) = willcpy;
6337 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6340 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6341 if (SCTP_BUF_NEXT(at) == NULL) {
6344 at = SCTP_BUF_NEXT(at);
6345 SCTP_BUF_LEN(at) = 0;
6346 cancpy = M_TRAILINGSPACE(at);
6347 willcpy = min(cancpy, left);
6354 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6355 struct sctp_sndrcvinfo *srcv)
6358 struct sctp_copy_all *ca;
6360 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6364 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6367 memset(ca, 0, sizeof(struct sctp_copy_all));
6370 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6372 * take off the sendall flag, it would be bad if we failed to do
6375 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6376 /* get length and mbuf chain */
6378 ca->sndlen = uio->uio_resid;
6379 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6380 if (ca->m == NULL) {
6381 SCTP_FREE(ca, SCTP_M_COPYAL);
6382 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6386 /* Gather the length of the send */
6392 ca->sndlen += SCTP_BUF_LEN(m);
6393 m = SCTP_BUF_NEXT(m);
6397 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6398 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6399 SCTP_ASOC_ANY_STATE,
6401 sctp_sendall_completes, inp, 1);
6403 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6404 SCTP_FREE(ca, SCTP_M_COPYAL);
6405 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6413 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6415 struct sctp_tmit_chunk *chk, *nchk;
6417 chk = TAILQ_FIRST(&asoc->control_send_queue);
6419 nchk = TAILQ_NEXT(chk, sctp_next);
6420 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6421 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6423 sctp_m_freem(chk->data);
6426 asoc->ctrl_queue_cnt--;
6427 sctp_free_a_chunk(stcb, chk);
6434 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6436 struct sctp_association *asoc;
6437 struct sctp_tmit_chunk *chk, *chk_tmp;
6438 struct sctp_asconf_chunk *acp;
6441 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6444 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6445 /* find SCTP_ASCONF chunk in queue */
6446 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6448 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6449 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6454 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6456 sctp_m_freem(chk->data);
6459 asoc->ctrl_queue_cnt--;
6460 sctp_free_a_chunk(stcb, chk);
6467 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6469 struct sctp_association *asoc,
6470 struct sctp_tmit_chunk **data_list,
6472 struct sctp_nets *net)
6475 struct sctp_tmit_chunk *tp1;
6477 for (i = 0; i < bundle_at; i++) {
6478 /* off of the send queue */
6481 * Any chunk NOT 0 you zap the time chunk 0 gets
6482 * zapped or set based on if a RTO measurment is
6485 data_list[i]->do_rtt = 0;
6488 data_list[i]->sent_rcv_time = net->last_sent_time;
6489 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6490 TAILQ_REMOVE(&asoc->send_queue,
6493 /* on to the sent queue */
6494 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6495 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6496 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6497 struct sctp_tmit_chunk *tpp;
6499 /* need to move back */
6501 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6503 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6507 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6508 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6511 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6513 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6518 /* This does not lower until the cum-ack passes it */
6519 asoc->sent_queue_cnt++;
6520 asoc->send_queue_cnt--;
6521 if ((asoc->peers_rwnd <= 0) &&
6522 (asoc->total_flight == 0) &&
6524 /* Mark the chunk as being a window probe */
6525 SCTP_STAT_INCR(sctps_windowprobed);
6527 #ifdef SCTP_AUDITING_ENABLED
6528 sctp_audit_log(0xC2, 3);
6530 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6531 data_list[i]->snd_count = 1;
6532 data_list[i]->rec.data.chunk_was_revoked = 0;
6533 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6534 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6535 data_list[i]->whoTo->flight_size,
6536 data_list[i]->book_size,
6537 (uintptr_t) data_list[i]->whoTo,
6538 data_list[i]->rec.data.TSN_seq);
6540 sctp_flight_size_increase(data_list[i]);
6541 sctp_total_flight_increase(stcb, data_list[i]);
6542 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6543 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6544 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6546 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6547 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6548 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6549 /* SWS sender side engages */
6550 asoc->peers_rwnd = 0;
6556 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6558 struct sctp_tmit_chunk *chk, *nchk;
6560 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6562 nchk = TAILQ_NEXT(chk, sctp_next);
6563 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6564 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
6565 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6566 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6567 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6568 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6569 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6570 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6571 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6572 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6573 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6574 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6575 /* Stray chunks must be cleaned up */
6577 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6579 sctp_m_freem(chk->data);
6582 asoc->ctrl_queue_cnt--;
6583 sctp_free_a_chunk(stcb, chk);
6584 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6585 /* special handling, we must look into the param */
6586 if (chk != asoc->str_reset) {
6587 goto clean_up_anyway;
6595 sctp_can_we_split_this(struct sctp_tcb *stcb,
6597 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6600 * Make a decision on if I should split a msg into multiple parts.
6601 * This is only asked of incomplete messages.
6605 * If we are doing EEOR we need to always send it if its the
6606 * entire thing, since it might be all the guy is putting in
6609 if (goal_mtu >= length) {
6611 * If we have data outstanding,
6612 * we get another chance when the sack
6613 * arrives to transmit - wait for more data
6615 if (stcb->asoc.total_flight == 0) {
6617 * If nothing is in flight, we zero the
6625 /* You can fill the rest */
6630 * For those strange folk that make the send buffer
6631 * smaller than our fragmentation point, we can't
6632 * get a full msg in so we have to allow splitting.
6634 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6637 if ((length <= goal_mtu) ||
6638 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6639 /* Sub-optimial residual don't split in non-eeor mode. */
6643 * If we reach here length is larger than the goal_mtu. Do we wish
6644 * to split it for the sake of packet putting together?
6646 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6647 /* Its ok to split it */
6648 return (min(goal_mtu, frag_point));
6650 /* Nope, can't split */
6656 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6657 struct sctp_stream_out *strq,
6659 uint32_t frag_point,
6665 /* Move from the stream to the send_queue keeping track of the total */
6666 struct sctp_association *asoc;
6667 struct sctp_stream_queue_pending *sp;
6668 struct sctp_tmit_chunk *chk;
6669 struct sctp_data_chunk *dchkh;
6670 uint32_t to_move, length;
6671 uint8_t rcv_flags = 0;
6673 uint8_t send_lock_up = 0;
6675 SCTP_TCB_LOCK_ASSERT(stcb);
6678 /* sa_ignore FREED_MEMORY */
6679 sp = TAILQ_FIRST(&strq->outqueue);
6682 if (send_lock_up == 0) {
6683 SCTP_TCB_SEND_LOCK(stcb);
6686 sp = TAILQ_FIRST(&strq->outqueue);
6690 if (strq->last_msg_incomplete) {
6691 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6693 strq->last_msg_incomplete);
6694 strq->last_msg_incomplete = 0;
6698 SCTP_TCB_SEND_UNLOCK(stcb);
6703 if ((sp->msg_is_complete) && (sp->length == 0)) {
6704 if (sp->sender_all_done) {
6706 * We are doing differed cleanup. Last time through
6707 * when we took all the data the sender_all_done was
6710 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6711 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6712 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6713 sp->sender_all_done,
6715 sp->msg_is_complete,
6719 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6720 SCTP_TCB_SEND_LOCK(stcb);
6723 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6724 TAILQ_REMOVE(&strq->outqueue, sp, next);
6725 sctp_free_remote_addr(sp->net);
6727 sctp_m_freem(sp->data);
6730 sctp_free_a_strmoq(stcb, sp);
6731 /* we can't be locked to it */
6733 stcb->asoc.locked_on_sending = NULL;
6735 SCTP_TCB_SEND_UNLOCK(stcb);
6738 /* back to get the next msg */
6742 * sender just finished this but still holds a
6751 /* is there some to get */
6752 if (sp->length == 0) {
6758 } else if (sp->discard_rest) {
6759 if (send_lock_up == 0) {
6760 SCTP_TCB_SEND_LOCK(stcb);
6763 /* Whack down the size */
6764 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6765 if ((stcb->sctp_socket != NULL) && \
6766 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6767 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6768 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6771 sctp_m_freem(sp->data);
6773 sp->tail_mbuf = NULL;
6783 some_taken = sp->some_taken;
6784 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6785 sp->msg_is_complete = 1;
6788 length = sp->length;
6789 if (sp->msg_is_complete) {
6790 /* The message is complete */
6791 to_move = min(length, frag_point);
6792 if (to_move == length) {
6793 /* All of it fits in the MTU */
6794 if (sp->some_taken) {
6795 rcv_flags |= SCTP_DATA_LAST_FRAG;
6796 sp->put_last_out = 1;
6798 rcv_flags |= SCTP_DATA_NOT_FRAG;
6799 sp->put_last_out = 1;
6802 /* Not all of it fits, we fragment */
6803 if (sp->some_taken == 0) {
6804 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6809 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6812 * We use a snapshot of length in case it
6813 * is expanding during the compare.
6818 if (to_move >= llen) {
6820 if (send_lock_up == 0) {
6822 * We are taking all of an incomplete msg
6823 * thus we need a send lock.
6825 SCTP_TCB_SEND_LOCK(stcb);
6827 if (sp->msg_is_complete) {
6829 * the sender finished the
6836 if (sp->some_taken == 0) {
6837 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6841 /* Nothing to take. */
6842 if (sp->some_taken) {
6851 /* If we reach here, we can copy out a chunk */
6852 sctp_alloc_a_chunk(stcb, chk);
6854 /* No chunk memory */
6860 * Setup for unordered if needed by looking at the user sent info
6863 if (sp->sinfo_flags & SCTP_UNORDERED) {
6864 rcv_flags |= SCTP_DATA_UNORDERED;
6866 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6867 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6868 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6870 /* clear out the chunk before setting up */
6871 memset(chk, 0, sizeof(*chk));
6872 chk->rec.data.rcv_flags = rcv_flags;
6874 if (to_move >= length) {
6875 /* we think we can steal the whole thing */
6876 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6877 SCTP_TCB_SEND_LOCK(stcb);
6880 if (to_move < sp->length) {
6881 /* bail, it changed */
6884 chk->data = sp->data;
6885 chk->last_mbuf = sp->tail_mbuf;
6886 /* register the stealing */
6887 sp->data = sp->tail_mbuf = NULL;
6892 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6893 chk->last_mbuf = NULL;
6894 if (chk->data == NULL) {
6895 sp->some_taken = some_taken;
6896 sctp_free_a_chunk(stcb, chk);
6901 #ifdef SCTP_MBUF_LOGGING
6902 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6907 if (SCTP_BUF_IS_EXTENDED(mat)) {
6908 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6910 mat = SCTP_BUF_NEXT(mat);
6914 /* Pull off the data */
6915 m_adj(sp->data, to_move);
6916 /* Now lets work our way down and compact it */
6918 while (m && (SCTP_BUF_LEN(m) == 0)) {
6919 sp->data = SCTP_BUF_NEXT(m);
6920 SCTP_BUF_NEXT(m) = NULL;
6921 if (sp->tail_mbuf == m) {
6923 * Freeing tail? TSNH since
6924 * we supposedly were taking less
6925 * than the sp->length.
6928 panic("Huh, freing tail? - TSNH");
6930 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6931 sp->tail_mbuf = sp->data = NULL;
6940 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6941 chk->copy_by_ref = 1;
6943 chk->copy_by_ref = 0;
6946 * get last_mbuf and counts of mb useage This is ugly but hopefully
6947 * its only one mbuf.
6949 if (chk->last_mbuf == NULL) {
6950 chk->last_mbuf = chk->data;
6951 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6952 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6955 if (to_move > length) {
6956 /*- This should not happen either
6957 * since we always lower to_move to the size
6958 * of sp->length if its larger.
6961 panic("Huh, how can to_move be larger?");
6963 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6967 atomic_subtract_int(&sp->length, to_move);
6969 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6970 /* Not enough room for a chunk header, get some */
6973 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6976 * we're in trouble here. _PREPEND below will free
6977 * all the data if there is no leading space, so we
6978 * must put the data back and restore.
6980 if (send_lock_up == 0) {
6981 SCTP_TCB_SEND_LOCK(stcb);
6984 if (chk->data == NULL) {
6985 /* unsteal the data */
6986 sp->data = chk->data;
6987 sp->tail_mbuf = chk->last_mbuf;
6991 /* reassemble the data */
6993 sp->data = chk->data;
6994 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6996 sp->some_taken = some_taken;
6997 atomic_add_int(&sp->length, to_move);
7000 sctp_free_a_chunk(stcb, chk);
7004 SCTP_BUF_LEN(m) = 0;
7005 SCTP_BUF_NEXT(m) = chk->data;
7007 M_ALIGN(chk->data, 4);
7010 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7011 if (chk->data == NULL) {
7012 /* HELP, TSNH since we assured it would not above? */
7014 panic("prepend failes HELP?");
7016 SCTP_PRINTF("prepend fails HELP?\n");
7017 sctp_free_a_chunk(stcb, chk);
7023 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7024 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7025 chk->book_size_scale = 0;
7026 chk->sent = SCTP_DATAGRAM_UNSENT;
7029 chk->asoc = &stcb->asoc;
7030 chk->pad_inplace = 0;
7031 chk->no_fr_allowed = 0;
7032 chk->rec.data.stream_seq = sp->strseq;
7033 chk->rec.data.stream_number = sp->stream;
7034 chk->rec.data.payloadtype = sp->ppid;
7035 chk->rec.data.context = sp->context;
7036 chk->rec.data.doing_fast_retransmit = 0;
7037 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
7039 chk->rec.data.timetodrop = sp->ts;
7040 chk->flags = sp->act_flags;
7043 atomic_add_int(&chk->whoTo->ref_count, 1);
7045 if (sp->holds_key_ref) {
7046 chk->auth_keyid = sp->auth_keyid;
7047 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7048 chk->holds_key_ref = 1;
7050 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7051 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7052 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7053 (uintptr_t) stcb, sp->length,
7054 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7055 chk->rec.data.TSN_seq);
7057 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7059 * Put the rest of the things in place now. Size was done earlier in
7060 * previous loop prior to padding.
7063 #ifdef SCTP_ASOCLOG_OF_TSNS
7064 SCTP_TCB_LOCK_ASSERT(stcb);
7065 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7066 asoc->tsn_out_at = 0;
7067 asoc->tsn_out_wrapped = 1;
7069 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7070 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7071 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7072 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7073 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7074 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7075 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7076 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7080 dchkh->ch.chunk_type = SCTP_DATA;
7081 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7082 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7083 dchkh->dp.stream_id = htons(strq->stream_no);
7084 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7085 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7086 dchkh->ch.chunk_length = htons(chk->send_size);
7087 /* Now advance the chk->send_size by the actual pad needed. */
7088 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7093 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7094 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7095 chk->pad_inplace = 1;
7097 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7098 /* pad added an mbuf */
7099 chk->last_mbuf = lm;
7101 chk->send_size += pads;
7103 /* We only re-set the policy if it is on */
7104 if (sp->pr_sctp_on) {
7105 sctp_set_prsctp_policy(sp);
7106 asoc->pr_sctp_cnt++;
7107 chk->pr_sctp_on = 1;
7109 chk->pr_sctp_on = 0;
7111 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7112 /* All done pull and kill the message */
7113 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7114 if (sp->put_last_out == 0) {
7115 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7116 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7117 sp->sender_all_done,
7119 sp->msg_is_complete,
7123 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7124 SCTP_TCB_SEND_LOCK(stcb);
7127 TAILQ_REMOVE(&strq->outqueue, sp, next);
7128 sctp_free_remote_addr(sp->net);
7130 sctp_m_freem(sp->data);
7133 sctp_free_a_strmoq(stcb, sp);
7135 /* we can't be locked to it */
7137 stcb->asoc.locked_on_sending = NULL;
7139 /* more to go, we are locked */
7142 asoc->chunks_on_out_queue++;
7143 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7144 asoc->send_queue_cnt++;
7147 SCTP_TCB_SEND_UNLOCK(stcb);
7154 static struct sctp_stream_out *
7155 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7157 struct sctp_stream_out *strq;
7159 /* Find the next stream to use */
7160 if (asoc->last_out_stream == NULL) {
7161 strq = TAILQ_FIRST(&asoc->out_wheel);
7163 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7165 strq = TAILQ_FIRST(&asoc->out_wheel);
7173 sctp_fill_outqueue(struct sctp_tcb *stcb,
7174 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7176 struct sctp_association *asoc;
7177 struct sctp_stream_out *strq, *strqn;
7178 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7180 struct sctp_stream_queue_pending *sp;
7182 SCTP_TCB_LOCK_ASSERT(stcb);
7185 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7186 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7188 /* ?? not sure what else to do */
7189 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7192 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7194 /* Need an allowance for the data chunk header too */
7195 goal_mtu -= sizeof(struct sctp_data_chunk);
7197 /* must make even word boundary */
7198 goal_mtu &= 0xfffffffc;
7199 if (asoc->locked_on_sending) {
7200 /* We are stuck on one stream until the message completes. */
7201 strqn = strq = asoc->locked_on_sending;
7204 strqn = strq = sctp_select_a_stream(stcb, asoc);
7208 while ((goal_mtu > 0) && strq) {
7209 sp = TAILQ_FIRST(&strq->outqueue);
7211 * If CMT is off, we must validate that the stream in
7212 * question has the first item pointed towards are network
7213 * destionation requested by the caller. Note that if we
7214 * turn out to be locked to a stream (assigning TSN's then
7215 * we must stop, since we cannot look for another stream
7216 * with data to send to that destination). In CMT's case, by
7217 * skipping this check, we will send one data packet towards
7218 * the requested net.
7223 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7224 /* none for this network */
7228 strq = sctp_select_a_stream(stcb, asoc);
7232 if (strqn == strq) {
7233 /* I have circled */
7241 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7242 &giveup, eeor_mode, &bail);
7244 asoc->last_out_stream = strq;
7247 asoc->locked_on_sending = strq;
7248 if ((moved_how_much == 0) || (giveup) || bail)
7249 /* no more to move for now */
7252 asoc->locked_on_sending = NULL;
7253 if (TAILQ_EMPTY(&strq->outqueue)) {
7254 if (strq == strqn) {
7255 /* Must move start to next one */
7256 strqn = TAILQ_NEXT(strq, next_spoke);
7257 if (strqn == NULL) {
7258 strqn = TAILQ_FIRST(&asoc->out_wheel);
7259 if (strqn == NULL) {
7264 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7266 if ((giveup) || bail) {
7269 strq = sctp_select_a_stream(stcb, asoc);
7274 total_moved += moved_how_much;
7275 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7276 goal_mtu &= 0xfffffffc;
7281 if (total_moved == 0) {
7282 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7283 (net == stcb->asoc.primary_destination)) {
7284 /* ran dry for primary network net */
7285 SCTP_STAT_INCR(sctps_primary_randry);
7286 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7287 /* ran dry with CMT on */
7288 SCTP_STAT_INCR(sctps_cmt_randry);
7294 sctp_fix_ecn_echo(struct sctp_association *asoc)
7296 struct sctp_tmit_chunk *chk;
7298 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7299 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7300 chk->sent = SCTP_DATAGRAM_UNSENT;
7306 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7307 struct sctp_association *asoc,
7308 struct sctp_nets *net)
7310 struct sctp_tmit_chunk *chk;
7311 struct sctp_nets *a_net;
7313 SCTP_TCB_LOCK_ASSERT(stcb);
7315 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7316 * destination using the PF algorithm for finding alternate
7319 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7320 a_net = sctp_find_alternate_net(stcb, net, 2);
7322 a_net = sctp_find_alternate_net(stcb, net, 0);
7324 if ((a_net != net) &&
7325 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7327 * We only proceed if a valid alternate is found that is not
7328 * this one and is reachable. Here we must move all chunks
7329 * queued in the send queue off of the destination address
7332 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7333 if (chk->whoTo == net) {
7334 /* Move the chunk to our alternate */
7335 sctp_free_remote_addr(chk->whoTo);
7337 atomic_add_int(&a_net->ref_count, 1);
7344 sctp_med_chunk_output(struct sctp_inpcb *inp,
7345 struct sctp_tcb *stcb,
7346 struct sctp_association *asoc,
7349 int control_only, int from_where,
7350 struct timeval *now, int *now_filled, int frag_point, int so_locked
7351 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7357 * Ok this is the generic chunk service queue. we must do the
7358 * following: - Service the stream queue that is next, moving any
7359 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7360 * LAST to the out queue in one pass) and assigning TSN's - Check to
7361 * see if the cwnd/rwnd allows any output, if so we go ahead and
7362 * fomulate and send the low level chunks. Making sure to combine
7363 * any control in the control chunk queue also.
7365 struct sctp_nets *net, *start_at, *old_start_at = NULL;
7366 struct mbuf *outchain, *endoutchain;
7367 struct sctp_tmit_chunk *chk, *nchk;
7369 /* temp arrays for unlinking */
7370 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7371 int no_fragmentflg, error;
7372 unsigned int max_rwnd_per_dest;
7373 int one_chunk, hbflag, skip_data_for_this_net;
7374 int asconf, cookie, no_out_cnt;
7375 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7376 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7378 uint32_t auth_offset = 0;
7379 struct sctp_auth_chunk *auth = NULL;
7380 uint16_t auth_keyid;
7381 int override_ok = 1;
7382 int data_auth_reqd = 0;
7385 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7392 auth_keyid = stcb->asoc.authinfo.active_keyid;
7394 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7395 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7396 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7401 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7403 * First lets prime the pump. For each destination, if there is room
7404 * in the flight size, attempt to pull an MTU's worth out of the
7405 * stream queues into the general send_queue
7407 #ifdef SCTP_AUDITING_ENABLED
7408 sctp_audit_log(0xC2, 2);
7410 SCTP_TCB_LOCK_ASSERT(stcb);
7412 if ((control_only) || (asoc->stream_reset_outstanding))
7417 /* Nothing to possible to send? */
7418 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7419 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7420 TAILQ_EMPTY(&asoc->send_queue) &&
7421 TAILQ_EMPTY(&asoc->out_wheel)) {
7425 if (asoc->peers_rwnd == 0) {
7426 /* No room in peers rwnd */
7428 if (asoc->total_flight > 0) {
7429 /* we are allowed one chunk in flight */
7433 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7434 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7435 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7437 * This for loop we are in takes in each net, if
7438 * its's got space in cwnd and has data sent to it
7439 * (when CMT is off) then it calls
7440 * sctp_fill_outqueue for the net. This gets data on
7441 * the send queue for that network.
7443 * In sctp_fill_outqueue TSN's are assigned and data is
7444 * copied out of the stream buffers. Note mostly
7445 * copy by reference (we hope).
7447 net->window_probe = 0;
7448 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7449 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7450 sctp_log_cwnd(stcb, net, 1,
7451 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7455 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7456 /* nothing can be in queue for this guy */
7457 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7458 sctp_log_cwnd(stcb, net, 2,
7459 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7463 if (net->flight_size >= net->cwnd) {
7464 /* skip this network, no room - can't fill */
7465 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7466 sctp_log_cwnd(stcb, net, 3,
7467 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7471 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7472 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7474 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7476 /* memory alloc failure */
7482 /* now service each destination and send out what we can for it */
7483 /* Nothing to send? */
7484 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7485 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7486 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7490 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7491 /* get the last start point */
7492 start_at = asoc->last_net_cmt_send_started;
7493 if (start_at == NULL) {
7494 /* null so to beginning */
7495 start_at = TAILQ_FIRST(&asoc->nets);
7497 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7498 if (start_at == NULL) {
7499 start_at = TAILQ_FIRST(&asoc->nets);
7502 asoc->last_net_cmt_send_started = start_at;
7504 start_at = TAILQ_FIRST(&asoc->nets);
7506 old_start_at = NULL;
7507 again_one_more_time:
7508 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7509 /* how much can we send? */
7510 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7511 if (old_start_at && (old_start_at == net)) {
7512 /* through list ocmpletely. */
7516 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7518 * Ref-count of 1 so we cannot have data or control
7519 * queued to this address. Skip it (non-CMT).
7523 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7524 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7525 (net->flight_size >= net->cwnd)) {
7527 * Nothing on control or asconf and flight is full,
7528 * we can skip even in the CMT case.
7532 ctl_cnt = bundle_at = 0;
7533 endoutchain = outchain = NULL;
7536 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7537 skip_data_for_this_net = 1;
7539 skip_data_for_this_net = 0;
7541 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7543 * if we have a route and an ifp check to see if we
7544 * have room to send to this guy
7548 ifp = net->ro.ro_rt->rt_ifp;
7549 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7550 SCTP_STAT_INCR(sctps_ifnomemqueued);
7551 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7552 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7557 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7559 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7563 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7573 if (mtu > asoc->peers_rwnd) {
7574 if (asoc->total_flight > 0) {
7575 /* We have a packet in flight somewhere */
7576 r_mtu = asoc->peers_rwnd;
7578 /* We are always allowed to send one MTU out */
7585 /************************/
7586 /* ASCONF transmission */
7587 /************************/
7588 /* Now first lets go through the asconf queue */
7589 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7591 nchk = TAILQ_NEXT(chk, sctp_next);
7592 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7595 if (chk->whoTo != net) {
7597 * No, not sent to the network we are
7602 if (chk->data == NULL) {
7605 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7606 chk->sent != SCTP_DATAGRAM_RESEND) {
7610 * if no AUTH is yet included and this chunk
7611 * requires it, make sure to account for it. We
7612 * don't apply the size until the AUTH chunk is
7613 * actually added below in case there is no room for
7614 * this chunk. NOTE: we overload the use of "omtu"
7617 if ((auth == NULL) &&
7618 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7619 stcb->asoc.peer_auth_chunks)) {
7620 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7623 /* Here we do NOT factor the r_mtu */
7624 if ((chk->send_size < (int)(mtu - omtu)) ||
7625 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7627 * We probably should glom the mbuf chain
7628 * from the chk->data for control but the
7629 * problem is it becomes yet one more level
7630 * of tracking to do if for some reason
7631 * output fails. Then I have got to
7632 * reconstruct the merged control chain.. el
7633 * yucko.. for now we take the easy way and
7637 * Add an AUTH chunk, if chunk requires it
7638 * save the offset into the chain for AUTH
7640 if ((auth == NULL) &&
7641 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7642 stcb->asoc.peer_auth_chunks))) {
7643 outchain = sctp_add_auth_chunk(outchain,
7648 chk->rec.chunk_id.id);
7649 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7651 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7652 (int)chk->rec.chunk_id.can_take_data,
7653 chk->send_size, chk->copy_by_ref);
7654 if (outchain == NULL) {
7656 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7659 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7660 /* update our MTU size */
7661 if (mtu > (chk->send_size + omtu))
7662 mtu -= (chk->send_size + omtu);
7665 to_out += (chk->send_size + omtu);
7666 /* Do clear IP_DF ? */
7667 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7670 if (chk->rec.chunk_id.can_take_data)
7673 * set hb flag since we can use these for
7679 * should sysctl this: don't bundle data
7680 * with ASCONF since it requires AUTH
7683 chk->sent = SCTP_DATAGRAM_SENT;
7687 * Ok we are out of room but we can
7688 * output without effecting the
7689 * flight size since this little guy
7690 * is a control only packet.
7692 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7694 * do NOT clear the asconf flag as
7695 * it is used to do appropriate
7696 * source address selection.
7698 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7699 (struct sockaddr *)&net->ro._l_addr,
7700 outchain, auth_offset, auth,
7701 stcb->asoc.authinfo.active_keyid,
7702 no_fragmentflg, 0, NULL, asconf,
7703 inp->sctp_lport, stcb->rport,
7704 htonl(stcb->asoc.peer_vtag),
7705 net->port, so_locked, NULL))) {
7706 if (error == ENOBUFS) {
7707 asoc->ifp_had_enobuf = 1;
7708 SCTP_STAT_INCR(sctps_lowlevelerr);
7710 if (from_where == 0) {
7711 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7713 if (*now_filled == 0) {
7714 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7716 *now = net->last_sent_time;
7718 net->last_sent_time = *now;
7721 /* error, could not output */
7722 if (error == EHOSTUNREACH) {
7728 sctp_move_to_an_alt(stcb, asoc, net);
7733 asoc->ifp_had_enobuf = 0;
7734 if (*now_filled == 0) {
7735 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7737 *now = net->last_sent_time;
7739 net->last_sent_time = *now;
7743 * increase the number we sent, if a
7744 * cookie is sent we don't tell them
7747 outchain = endoutchain = NULL;
7751 *num_out += ctl_cnt;
7752 /* recalc a clean slate and setup */
7753 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7754 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7756 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7763 /************************/
7764 /* Control transmission */
7765 /************************/
7766 /* Now first lets go through the control queue */
7767 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7769 nchk = TAILQ_NEXT(chk, sctp_next);
7770 if (chk->whoTo != net) {
7772 * No, not sent to the network we are
7777 if (chk->data == NULL) {
7780 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7782 * It must be unsent. Cookies and ASCONF's
7783 * hang around but there timers will force
7784 * when marked for resend.
7789 * if no AUTH is yet included and this chunk
7790 * requires it, make sure to account for it. We
7791 * don't apply the size until the AUTH chunk is
7792 * actually added below in case there is no room for
7793 * this chunk. NOTE: we overload the use of "omtu"
7796 if ((auth == NULL) &&
7797 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7798 stcb->asoc.peer_auth_chunks)) {
7799 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7802 /* Here we do NOT factor the r_mtu */
7803 if ((chk->send_size < (int)(mtu - omtu)) ||
7804 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7806 * We probably should glom the mbuf chain
7807 * from the chk->data for control but the
7808 * problem is it becomes yet one more level
7809 * of tracking to do if for some reason
7810 * output fails. Then I have got to
7811 * reconstruct the merged control chain.. el
7812 * yucko.. for now we take the easy way and
7816 * Add an AUTH chunk, if chunk requires it
7817 * save the offset into the chain for AUTH
7819 if ((auth == NULL) &&
7820 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7821 stcb->asoc.peer_auth_chunks))) {
7822 outchain = sctp_add_auth_chunk(outchain,
7827 chk->rec.chunk_id.id);
7828 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7830 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7831 (int)chk->rec.chunk_id.can_take_data,
7832 chk->send_size, chk->copy_by_ref);
7833 if (outchain == NULL) {
7835 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7838 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7839 /* update our MTU size */
7840 if (mtu > (chk->send_size + omtu))
7841 mtu -= (chk->send_size + omtu);
7844 to_out += (chk->send_size + omtu);
7845 /* Do clear IP_DF ? */
7846 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7849 if (chk->rec.chunk_id.can_take_data)
7851 /* Mark things to be removed, if needed */
7852 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7853 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7854 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7855 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7856 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7857 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7858 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7859 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7860 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7861 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7862 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7864 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7867 * JRS 5/14/07 - Set the
7868 * flag to say a heartbeat
7873 /* remove these chunks at the end */
7874 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7875 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7876 /* turn off the timer */
7877 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7878 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7879 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7885 * Other chunks, since they have
7886 * timers running (i.e. COOKIE) we
7887 * just "trust" that it gets sent or
7891 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7895 chk->sent = SCTP_DATAGRAM_SENT;
7900 * Ok we are out of room but we can
7901 * output without effecting the
7902 * flight size since this little guy
7903 * is a control only packet.
7906 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7908 * do NOT clear the asconf
7909 * flag as it is used to do
7910 * appropriate source
7911 * address selection.
7915 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7918 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7919 (struct sockaddr *)&net->ro._l_addr,
7922 stcb->asoc.authinfo.active_keyid,
7923 no_fragmentflg, 0, NULL, asconf,
7924 inp->sctp_lport, stcb->rport,
7925 htonl(stcb->asoc.peer_vtag),
7926 net->port, so_locked, NULL))) {
7927 if (error == ENOBUFS) {
7928 asoc->ifp_had_enobuf = 1;
7929 SCTP_STAT_INCR(sctps_lowlevelerr);
7931 if (from_where == 0) {
7932 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7934 /* error, could not output */
7936 if (*now_filled == 0) {
7937 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7939 *now = net->last_sent_time;
7941 net->last_sent_time = *now;
7945 if (error == EHOSTUNREACH) {
7951 sctp_move_to_an_alt(stcb, asoc, net);
7956 asoc->ifp_had_enobuf = 0;
7957 /* Only HB or ASCONF advances time */
7959 if (*now_filled == 0) {
7960 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7962 *now = net->last_sent_time;
7964 net->last_sent_time = *now;
7969 * increase the number we sent, if a
7970 * cookie is sent we don't tell them
7973 outchain = endoutchain = NULL;
7977 *num_out += ctl_cnt;
7978 /* recalc a clean slate and setup */
7979 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7980 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7982 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7989 /* JRI: if dest is in PF state, do not send data to it */
7990 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
7991 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
7992 (net->dest_state & SCTP_ADDR_PF)) {
7995 if (net->flight_size >= net->cwnd) {
7998 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
7999 (net->flight_size > max_rwnd_per_dest)) {
8002 /*********************/
8003 /* Data transmission */
8004 /*********************/
8006 * if AUTH for DATA is required and no AUTH has been added
8007 * yet, account for this in the mtu now... if no data can be
8008 * bundled, this adjustment won't matter anyways since the
8009 * packet will be going out...
8011 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8012 stcb->asoc.peer_auth_chunks);
8013 if (data_auth_reqd && (auth == NULL)) {
8014 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8016 /* now lets add any data within the MTU constraints */
8017 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8019 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8020 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8026 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8027 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8037 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8038 (skip_data_for_this_net == 0)) ||
8040 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8041 if (no_data_chunks) {
8042 /* let only control go out */
8046 if (net->flight_size >= net->cwnd) {
8047 /* skip this net, no room for data */
8051 nchk = TAILQ_NEXT(chk, sctp_next);
8052 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8053 if (chk->whoTo != net) {
8055 * For CMT, steal the data
8056 * to this network if its
8059 sctp_free_remote_addr(chk->whoTo);
8061 atomic_add_int(&chk->whoTo->ref_count, 1);
8063 } else if (chk->whoTo != net) {
8064 /* No, not sent to this net */
8067 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8069 * strange, we have a chunk that is
8070 * to big for its destination and
8071 * yet no fragment ok flag.
8072 * Something went wrong when the
8073 * PMTU changed...we did not mark
8074 * this chunk for some reason?? I
8075 * will fix it here by letting IP
8076 * fragment it for now and printing
8077 * a warning. This really should not
8080 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8081 chk->send_size, mtu);
8082 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8084 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8085 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8086 struct sctp_data_chunk *dchkh;
8088 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8089 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8091 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8092 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8093 /* ok we will add this one */
8096 * Add an AUTH chunk, if chunk
8097 * requires it, save the offset into
8098 * the chain for AUTH
8100 if (data_auth_reqd) {
8102 outchain = sctp_add_auth_chunk(outchain,
8108 auth_keyid = chk->auth_keyid;
8110 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8111 } else if (override_ok) {
8116 auth_keyid = chk->auth_keyid;
8118 } else if (auth_keyid != chk->auth_keyid) {
8126 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8127 chk->send_size, chk->copy_by_ref);
8128 if (outchain == NULL) {
8129 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8130 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8131 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8134 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8137 /* upate our MTU size */
8138 /* Do clear IP_DF ? */
8139 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8142 /* unsigned subtraction of mtu */
8143 if (mtu > chk->send_size)
8144 mtu -= chk->send_size;
8147 /* unsigned subtraction of r_mtu */
8148 if (r_mtu > chk->send_size)
8149 r_mtu -= chk->send_size;
8153 to_out += chk->send_size;
8154 if ((to_out > mx_mtu) && no_fragmentflg) {
8156 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8158 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8162 chk->window_probe = 0;
8163 data_list[bundle_at++] = chk;
8164 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8168 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8169 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8170 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8172 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8174 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8175 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8185 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8187 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8188 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8189 data_list[0]->window_probe = 1;
8190 net->window_probe = 1;
8196 * Must be sent in order of the
8197 * TSN's (on a network)
8201 } /* for (chunk gather loop for this net) */
8202 } /* if asoc.state OPEN */
8204 /* Is there something to send for this destination? */
8206 /* We may need to start a control timer or two */
8208 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8211 * do NOT clear the asconf flag as it is
8212 * used to do appropriate source address
8217 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8220 /* must start a send timer if data is being sent */
8221 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8223 * no timer running on this destination
8226 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8227 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8228 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8230 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8231 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8233 * JRS 5/14/07 - If a HB has been sent to a
8234 * PF destination and no T3 timer is
8235 * currently running, start the T3 timer to
8236 * track the HBs that were sent.
8238 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8240 /* Now send it, if there is anything to send :> */
8241 if ((error = sctp_lowlevel_chunk_output(inp,
8244 (struct sockaddr *)&net->ro._l_addr,
8253 inp->sctp_lport, stcb->rport,
8254 htonl(stcb->asoc.peer_vtag),
8255 net->port, so_locked, NULL))) {
8256 /* error, we could not output */
8257 if (error == ENOBUFS) {
8258 SCTP_STAT_INCR(sctps_lowlevelerr);
8259 asoc->ifp_had_enobuf = 1;
8261 if (from_where == 0) {
8262 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8264 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8266 if (*now_filled == 0) {
8267 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8269 *now = net->last_sent_time;
8271 net->last_sent_time = *now;
8275 if (error == EHOSTUNREACH) {
8277 * Destination went unreachable
8280 sctp_move_to_an_alt(stcb, asoc, net);
8284 * I add this line to be paranoid. As far as
8285 * I can tell the continue, takes us back to
8286 * the top of the for, but just to make sure
8287 * I will reset these again here.
8289 ctl_cnt = bundle_at = 0;
8290 continue; /* This takes us back to the
8291 * for() for the nets. */
8293 asoc->ifp_had_enobuf = 0;
8295 outchain = endoutchain = NULL;
8298 if (bundle_at || hbflag) {
8299 /* For data/asconf and hb set time */
8300 if (*now_filled == 0) {
8301 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8303 *now = net->last_sent_time;
8305 net->last_sent_time = *now;
8309 *num_out += (ctl_cnt + bundle_at);
8312 /* setup for a RTO measurement */
8313 tsns_sent = data_list[0]->rec.data.TSN_seq;
8314 /* fill time if not already filled */
8315 if (*now_filled == 0) {
8316 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8318 *now = asoc->time_last_sent;
8320 asoc->time_last_sent = *now;
8322 data_list[0]->do_rtt = 1;
8323 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8324 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8325 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8326 if (net->flight_size < net->cwnd) {
8327 /* start or restart it */
8328 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8329 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8330 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8332 SCTP_STAT_INCR(sctps_earlyfrstrout);
8333 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8335 /* stop it if its running */
8336 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8337 SCTP_STAT_INCR(sctps_earlyfrstpout);
8338 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8339 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8349 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8352 if (old_start_at == NULL) {
8353 old_start_at = start_at;
8354 start_at = TAILQ_FIRST(&asoc->nets);
8356 goto again_one_more_time;
8359 * At the end there should be no NON timed chunks hanging on this
8362 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8363 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8365 if ((*num_out == 0) && (*reason_code == 0)) {
8370 sctp_clean_up_ctl(stcb, asoc);
8375 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8378 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8379 * the control chunk queue.
8381 struct sctp_chunkhdr *hdr;
8382 struct sctp_tmit_chunk *chk;
8385 SCTP_TCB_LOCK_ASSERT(stcb);
8386 sctp_alloc_a_chunk(stcb, chk);
8389 sctp_m_freem(op_err);
8392 chk->copy_by_ref = 0;
8393 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8394 if (op_err == NULL) {
8395 sctp_free_a_chunk(stcb, chk);
8400 while (mat != NULL) {
8401 chk->send_size += SCTP_BUF_LEN(mat);
8402 mat = SCTP_BUF_NEXT(mat);
8404 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8405 chk->rec.chunk_id.can_take_data = 1;
8406 chk->sent = SCTP_DATAGRAM_UNSENT;
8409 chk->asoc = &stcb->asoc;
8411 chk->whoTo = chk->asoc->primary_destination;
8412 atomic_add_int(&chk->whoTo->ref_count, 1);
8413 hdr = mtod(op_err, struct sctp_chunkhdr *);
8414 hdr->chunk_type = SCTP_OPERATION_ERROR;
8415 hdr->chunk_flags = 0;
8416 hdr->chunk_length = htons(chk->send_size);
8417 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8420 chk->asoc->ctrl_queue_cnt++;
8424 sctp_send_cookie_echo(struct mbuf *m,
8426 struct sctp_tcb *stcb,
8427 struct sctp_nets *net)
8430 * pull out the cookie and put it at the front of the control chunk
8434 struct mbuf *cookie;
8435 struct sctp_paramhdr parm, *phdr;
8436 struct sctp_chunkhdr *hdr;
8437 struct sctp_tmit_chunk *chk;
8438 uint16_t ptype, plen;
8440 /* First find the cookie in the param area */
8442 at = offset + sizeof(struct sctp_init_chunk);
8444 SCTP_TCB_LOCK_ASSERT(stcb);
8446 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8450 ptype = ntohs(phdr->param_type);
8451 plen = ntohs(phdr->param_length);
8452 if (ptype == SCTP_STATE_COOKIE) {
8455 /* found the cookie */
8456 if ((pad = (plen % 4))) {
8459 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8460 if (cookie == NULL) {
8464 #ifdef SCTP_MBUF_LOGGING
8465 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8470 if (SCTP_BUF_IS_EXTENDED(mat)) {
8471 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8473 mat = SCTP_BUF_NEXT(mat);
8479 at += SCTP_SIZE32(plen);
8481 if (cookie == NULL) {
8482 /* Did not find the cookie */
8485 /* ok, we got the cookie lets change it into a cookie echo chunk */
8487 /* first the change from param to cookie */
8488 hdr = mtod(cookie, struct sctp_chunkhdr *);
8489 hdr->chunk_type = SCTP_COOKIE_ECHO;
8490 hdr->chunk_flags = 0;
8491 /* get the chunk stuff now and place it in the FRONT of the queue */
8492 sctp_alloc_a_chunk(stcb, chk);
8495 sctp_m_freem(cookie);
8498 chk->copy_by_ref = 0;
8499 chk->send_size = plen;
8500 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8501 chk->rec.chunk_id.can_take_data = 0;
8502 chk->sent = SCTP_DATAGRAM_UNSENT;
8504 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8505 chk->asoc = &stcb->asoc;
8507 chk->whoTo = chk->asoc->primary_destination;
8508 atomic_add_int(&chk->whoTo->ref_count, 1);
8509 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8510 chk->asoc->ctrl_queue_cnt++;
8515 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8519 struct sctp_nets *net)
8522 * take a HB request and make it into a HB ack and send it.
8524 struct mbuf *outchain;
8525 struct sctp_chunkhdr *chdr;
8526 struct sctp_tmit_chunk *chk;
8530 /* must have a net pointer */
8533 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8534 if (outchain == NULL) {
8535 /* gak out of memory */
8538 #ifdef SCTP_MBUF_LOGGING
8539 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8544 if (SCTP_BUF_IS_EXTENDED(mat)) {
8545 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8547 mat = SCTP_BUF_NEXT(mat);
8551 chdr = mtod(outchain, struct sctp_chunkhdr *);
8552 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8553 chdr->chunk_flags = 0;
8554 if (chk_length % 4) {
8556 uint32_t cpthis = 0;
8559 padlen = 4 - (chk_length % 4);
8560 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8562 sctp_alloc_a_chunk(stcb, chk);
8565 sctp_m_freem(outchain);
8568 chk->copy_by_ref = 0;
8569 chk->send_size = chk_length;
8570 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8571 chk->rec.chunk_id.can_take_data = 1;
8572 chk->sent = SCTP_DATAGRAM_UNSENT;
8575 chk->asoc = &stcb->asoc;
8576 chk->data = outchain;
8578 atomic_add_int(&chk->whoTo->ref_count, 1);
8579 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8580 chk->asoc->ctrl_queue_cnt++;
8584 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8586 /* formulate and queue a cookie-ack back to sender */
8587 struct mbuf *cookie_ack;
8588 struct sctp_chunkhdr *hdr;
8589 struct sctp_tmit_chunk *chk;
8592 SCTP_TCB_LOCK_ASSERT(stcb);
8594 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8595 if (cookie_ack == NULL) {
8599 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8600 sctp_alloc_a_chunk(stcb, chk);
8603 sctp_m_freem(cookie_ack);
8606 chk->copy_by_ref = 0;
8607 chk->send_size = sizeof(struct sctp_chunkhdr);
8608 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8609 chk->rec.chunk_id.can_take_data = 1;
8610 chk->sent = SCTP_DATAGRAM_UNSENT;
8613 chk->asoc = &stcb->asoc;
8614 chk->data = cookie_ack;
8615 if (chk->asoc->last_control_chunk_from != NULL) {
8616 chk->whoTo = chk->asoc->last_control_chunk_from;
8618 chk->whoTo = chk->asoc->primary_destination;
8620 atomic_add_int(&chk->whoTo->ref_count, 1);
8621 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8622 hdr->chunk_type = SCTP_COOKIE_ACK;
8623 hdr->chunk_flags = 0;
8624 hdr->chunk_length = htons(chk->send_size);
8625 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8626 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8627 chk->asoc->ctrl_queue_cnt++;
8633 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8635 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8636 struct mbuf *m_shutdown_ack;
8637 struct sctp_shutdown_ack_chunk *ack_cp;
8638 struct sctp_tmit_chunk *chk;
8640 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8641 if (m_shutdown_ack == NULL) {
8645 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8646 sctp_alloc_a_chunk(stcb, chk);
8649 sctp_m_freem(m_shutdown_ack);
8652 chk->copy_by_ref = 0;
8653 chk->send_size = sizeof(struct sctp_chunkhdr);
8654 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8655 chk->rec.chunk_id.can_take_data = 1;
8656 chk->sent = SCTP_DATAGRAM_UNSENT;
8659 chk->asoc = &stcb->asoc;
8660 chk->data = m_shutdown_ack;
8662 atomic_add_int(&net->ref_count, 1);
8664 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8665 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8666 ack_cp->ch.chunk_flags = 0;
8667 ack_cp->ch.chunk_length = htons(chk->send_size);
8668 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8669 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8670 chk->asoc->ctrl_queue_cnt++;
8675 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8677 /* formulate and queue a SHUTDOWN to the sender */
8678 struct mbuf *m_shutdown;
8679 struct sctp_shutdown_chunk *shutdown_cp;
8680 struct sctp_tmit_chunk *chk;
8682 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8683 if (m_shutdown == NULL) {
8687 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8688 sctp_alloc_a_chunk(stcb, chk);
8691 sctp_m_freem(m_shutdown);
8694 chk->copy_by_ref = 0;
8695 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8696 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8697 chk->rec.chunk_id.can_take_data = 1;
8698 chk->sent = SCTP_DATAGRAM_UNSENT;
8701 chk->asoc = &stcb->asoc;
8702 chk->data = m_shutdown;
8704 atomic_add_int(&net->ref_count, 1);
8706 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8707 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8708 shutdown_cp->ch.chunk_flags = 0;
8709 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8710 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8711 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8712 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8713 chk->asoc->ctrl_queue_cnt++;
8718 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8721 * formulate and queue an ASCONF to the peer. ASCONF parameters
8722 * should be queued on the assoc queue.
8724 struct sctp_tmit_chunk *chk;
8725 struct mbuf *m_asconf;
8728 SCTP_TCB_LOCK_ASSERT(stcb);
8730 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8731 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8732 /* can't send a new one if there is one in flight already */
8735 /* compose an ASCONF chunk, maximum length is PMTU */
8736 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8737 if (m_asconf == NULL) {
8740 sctp_alloc_a_chunk(stcb, chk);
8743 sctp_m_freem(m_asconf);
8746 chk->copy_by_ref = 0;
8747 chk->data = m_asconf;
8748 chk->send_size = len;
8749 chk->rec.chunk_id.id = SCTP_ASCONF;
8750 chk->rec.chunk_id.can_take_data = 0;
8751 chk->sent = SCTP_DATAGRAM_UNSENT;
8753 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8754 chk->asoc = &stcb->asoc;
8756 atomic_add_int(&chk->whoTo->ref_count, 1);
8757 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8758 chk->asoc->ctrl_queue_cnt++;
8763 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8766 * formulate and queue a asconf-ack back to sender. the asconf-ack
8767 * must be stored in the tcb.
8769 struct sctp_tmit_chunk *chk;
8770 struct sctp_asconf_ack *ack, *latest_ack;
8771 struct mbuf *m_ack, *m;
8772 struct sctp_nets *net = NULL;
8774 SCTP_TCB_LOCK_ASSERT(stcb);
8775 /* Get the latest ASCONF-ACK */
8776 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8777 if (latest_ack == NULL) {
8780 if (latest_ack->last_sent_to != NULL &&
8781 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8782 /* we're doing a retransmission */
8783 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8786 if (stcb->asoc.last_control_chunk_from == NULL)
8787 net = stcb->asoc.primary_destination;
8789 net = stcb->asoc.last_control_chunk_from;
8793 if (stcb->asoc.last_control_chunk_from == NULL)
8794 net = stcb->asoc.primary_destination;
8796 net = stcb->asoc.last_control_chunk_from;
8798 latest_ack->last_sent_to = net;
8800 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8801 if (ack->data == NULL) {
8804 /* copy the asconf_ack */
8805 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8806 if (m_ack == NULL) {
8807 /* couldn't copy it */
8810 #ifdef SCTP_MBUF_LOGGING
8811 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8816 if (SCTP_BUF_IS_EXTENDED(mat)) {
8817 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8819 mat = SCTP_BUF_NEXT(mat);
8824 sctp_alloc_a_chunk(stcb, chk);
8828 sctp_m_freem(m_ack);
8831 chk->copy_by_ref = 0;
8838 chk->send_size = ack->len;
8839 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8840 chk->rec.chunk_id.can_take_data = 1;
8841 chk->sent = SCTP_DATAGRAM_UNSENT;
8843 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8844 chk->asoc = &stcb->asoc;
8845 atomic_add_int(&chk->whoTo->ref_count, 1);
8847 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8848 chk->asoc->ctrl_queue_cnt++;
8855 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8856 struct sctp_tcb *stcb,
8857 struct sctp_association *asoc,
8858 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8859 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8865 * send out one MTU of retransmission. If fast_retransmit is
8866 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8867 * rwnd. For a Cookie or Asconf in the control chunk queue we
8868 * retransmit them by themselves.
8870 * For data chunks we will pick out the lowest TSN's in the sent_queue
8871 * marked for resend and bundle them all together (up to a MTU of
8872 * destination). The address to send to should have been
8873 * selected/changed where the retransmission was marked (i.e. in FR
8874 * or t3-timeout routines).
8876 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8877 struct sctp_tmit_chunk *chk, *fwd;
8878 struct mbuf *m, *endofchain;
8879 struct sctp_nets *net = NULL;
8880 uint32_t tsns_sent = 0;
8881 int no_fragmentflg, bundle_at, cnt_thru;
8883 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8884 struct sctp_auth_chunk *auth = NULL;
8885 uint32_t auth_offset = 0;
8886 uint16_t auth_keyid;
8887 int override_ok = 1;
8888 int data_auth_reqd = 0;
8891 SCTP_TCB_LOCK_ASSERT(stcb);
8892 tmr_started = ctl_cnt = bundle_at = error = 0;
8897 endofchain = m = NULL;
8898 auth_keyid = stcb->asoc.authinfo.active_keyid;
8899 #ifdef SCTP_AUDITING_ENABLED
8900 sctp_audit_log(0xC3, 1);
8902 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8903 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8904 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8905 asoc->sent_queue_retran_cnt);
8906 asoc->sent_queue_cnt = 0;
8907 asoc->sent_queue_cnt_removeable = 0;
8908 /* send back 0/0 so we enter normal transmission */
8912 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8913 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8914 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8915 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8916 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8917 if (chk != asoc->str_reset) {
8919 * not eligible for retran if its
8926 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8931 * Add an AUTH chunk, if chunk requires it save the
8932 * offset into the chain for AUTH
8934 if ((auth == NULL) &&
8935 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8936 stcb->asoc.peer_auth_chunks))) {
8937 m = sctp_add_auth_chunk(m, &endofchain,
8938 &auth, &auth_offset,
8940 chk->rec.chunk_id.id);
8941 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8943 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8949 /* do we have control chunks to retransmit? */
8951 /* Start a timer no matter if we suceed or fail */
8952 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8953 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8954 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8955 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8956 chk->snd_count++; /* update our count */
8957 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8958 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
8959 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
8960 no_fragmentflg, 0, NULL, 0,
8961 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
8962 chk->whoTo->port, so_locked, NULL))) {
8963 SCTP_STAT_INCR(sctps_lowlevelerr);
8966 m = endofchain = NULL;
8970 * We don't want to mark the net->sent time here since this
8971 * we use this for HB and retrans cannot measure RTT
8973 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8975 chk->sent = SCTP_DATAGRAM_SENT;
8976 /* sctp_ucount_decr(asoc->sent_queue_retran_cnt); */
8980 /* Clean up the fwd-tsn list */
8981 sctp_clean_up_ctl(stcb, asoc);
8986 * Ok, it is just data retransmission we need to do or that and a
8987 * fwd-tsn with it all.
8989 if (TAILQ_EMPTY(&asoc->sent_queue)) {
8990 return (SCTP_RETRAN_DONE);
8992 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8993 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8994 /* not yet open, resend the cookie and that is it */
8997 #ifdef SCTP_AUDITING_ENABLED
8998 sctp_auditing(20, inp, stcb, NULL);
9000 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9001 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9002 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9003 /* No, not sent to this net or not ready for rtx */
9006 if (chk->data == NULL) {
9007 printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9008 chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9011 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9012 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9013 /* Gak, we have exceeded max unlucky retran, abort! */
9014 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9016 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9017 atomic_add_int(&stcb->asoc.refcnt, 1);
9018 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9019 SCTP_TCB_LOCK(stcb);
9020 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9021 return (SCTP_RETRAN_EXIT);
9023 /* pick up the net */
9025 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9026 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9028 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9031 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9032 /* No room in peers rwnd */
9035 tsn = asoc->last_acked_seq + 1;
9036 if (tsn == chk->rec.data.TSN_seq) {
9038 * we make a special exception for this
9039 * case. The peer has no rwnd but is missing
9040 * the lowest chunk.. which is probably what
9041 * is holding up the rwnd.
9043 goto one_chunk_around;
9048 if (asoc->peers_rwnd < mtu) {
9050 if ((asoc->peers_rwnd == 0) &&
9051 (asoc->total_flight == 0)) {
9052 chk->window_probe = 1;
9053 chk->whoTo->window_probe = 1;
9056 #ifdef SCTP_AUDITING_ENABLED
9057 sctp_audit_log(0xC3, 2);
9061 net->fast_retran_ip = 0;
9062 if (chk->rec.data.doing_fast_retransmit == 0) {
9064 * if no FR in progress skip destination that have
9065 * flight_size > cwnd.
9067 if (net->flight_size >= net->cwnd) {
9072 * Mark the destination net to have FR recovery
9076 net->fast_retran_ip = 1;
9080 * if no AUTH is yet included and this chunk requires it,
9081 * make sure to account for it. We don't apply the size
9082 * until the AUTH chunk is actually added below in case
9083 * there is no room for this chunk.
9085 if (data_auth_reqd && (auth == NULL)) {
9086 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9090 if ((chk->send_size <= (mtu - dmtu)) ||
9091 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9092 /* ok we will add this one */
9093 if (data_auth_reqd) {
9095 m = sctp_add_auth_chunk(m,
9101 auth_keyid = chk->auth_keyid;
9103 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9104 } else if (override_ok) {
9105 auth_keyid = chk->auth_keyid;
9107 } else if (chk->auth_keyid != auth_keyid) {
9108 /* different keyid, so done bundling */
9112 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9114 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9117 /* Do clear IP_DF ? */
9118 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9121 /* upate our MTU size */
9122 if (mtu > (chk->send_size + dmtu))
9123 mtu -= (chk->send_size + dmtu);
9126 data_list[bundle_at++] = chk;
9127 if (one_chunk && (asoc->total_flight <= 0)) {
9128 SCTP_STAT_INCR(sctps_windowprobed);
9131 if (one_chunk == 0) {
9133 * now are there anymore forward from chk to pick
9136 fwd = TAILQ_NEXT(chk, sctp_next);
9138 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9139 /* Nope, not for retran */
9140 fwd = TAILQ_NEXT(fwd, sctp_next);
9143 if (fwd->whoTo != net) {
9144 /* Nope, not the net in question */
9145 fwd = TAILQ_NEXT(fwd, sctp_next);
9148 if (data_auth_reqd && (auth == NULL)) {
9149 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9152 if (fwd->send_size <= (mtu - dmtu)) {
9153 if (data_auth_reqd) {
9155 m = sctp_add_auth_chunk(m,
9161 auth_keyid = fwd->auth_keyid;
9163 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9164 } else if (override_ok) {
9165 auth_keyid = fwd->auth_keyid;
9167 } else if (fwd->auth_keyid != auth_keyid) {
9175 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9177 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9180 /* Do clear IP_DF ? */
9181 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9184 /* upate our MTU size */
9185 if (mtu > (fwd->send_size + dmtu))
9186 mtu -= (fwd->send_size + dmtu);
9189 data_list[bundle_at++] = fwd;
9190 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9193 fwd = TAILQ_NEXT(fwd, sctp_next);
9195 /* can't fit so we are done */
9200 /* Is there something to send for this destination? */
9203 * No matter if we fail/or suceed we should start a
9204 * timer. A failure is like a lost IP packet :-)
9206 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9208 * no timer running on this destination
9211 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9214 /* Now lets send it, if there is anything to send :> */
9215 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9216 (struct sockaddr *)&net->ro._l_addr, m,
9217 auth_offset, auth, auth_keyid,
9218 no_fragmentflg, 0, NULL, 0,
9219 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9220 net->port, so_locked, NULL))) {
9221 /* error, we could not output */
9222 SCTP_STAT_INCR(sctps_lowlevelerr);
9225 m = endofchain = NULL;
9230 * We don't want to mark the net->sent time here
9231 * since this we use this for HB and retrans cannot
9234 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9236 /* For auto-close */
9238 if (*now_filled == 0) {
9239 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9240 *now = asoc->time_last_sent;
9243 asoc->time_last_sent = *now;
9245 *cnt_out += bundle_at;
9246 #ifdef SCTP_AUDITING_ENABLED
9247 sctp_audit_log(0xC4, bundle_at);
9250 tsns_sent = data_list[0]->rec.data.TSN_seq;
9252 for (i = 0; i < bundle_at; i++) {
9253 SCTP_STAT_INCR(sctps_sendretransdata);
9254 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9256 * When we have a revoked data, and we
9257 * retransmit it, then we clear the revoked
9258 * flag since this flag dictates if we
9259 * subtracted from the fs
9261 if (data_list[i]->rec.data.chunk_was_revoked) {
9262 /* Deflate the cwnd */
9263 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9264 data_list[i]->rec.data.chunk_was_revoked = 0;
9266 data_list[i]->snd_count++;
9267 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9268 /* record the time */
9269 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9270 if (data_list[i]->book_size_scale) {
9272 * need to double the book size on
9275 data_list[i]->book_size_scale = 0;
9277 * Since we double the booksize, we
9278 * must also double the output queue
9279 * size, since this get shrunk when
9280 * we free by this amount.
9282 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9283 data_list[i]->book_size *= 2;
9287 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9288 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9289 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9291 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9292 (uint32_t) (data_list[i]->send_size +
9293 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9295 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9296 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9297 data_list[i]->whoTo->flight_size,
9298 data_list[i]->book_size,
9299 (uintptr_t) data_list[i]->whoTo,
9300 data_list[i]->rec.data.TSN_seq);
9302 sctp_flight_size_increase(data_list[i]);
9303 sctp_total_flight_increase(stcb, data_list[i]);
9304 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9305 /* SWS sender side engages */
9306 asoc->peers_rwnd = 0;
9309 (data_list[i]->rec.data.doing_fast_retransmit)) {
9310 SCTP_STAT_INCR(sctps_sendfastretrans);
9311 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9312 (tmr_started == 0)) {
9314 * ok we just fast-retrans'd
9315 * the lowest TSN, i.e the
9316 * first on the list. In
9317 * this case we want to give
9318 * some more time to get a
9319 * SACK back without a
9322 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9323 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9324 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9329 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9331 #ifdef SCTP_AUDITING_ENABLED
9332 sctp_auditing(21, inp, stcb, NULL);
9338 if (asoc->sent_queue_retran_cnt <= 0) {
9339 /* all done we have no more to retran */
9340 asoc->sent_queue_retran_cnt = 0;
9344 /* No more room in rwnd */
9347 /* stop the for loop here. we sent out a packet */
9355 sctp_timer_validation(struct sctp_inpcb *inp,
9356 struct sctp_tcb *stcb,
9357 struct sctp_association *asoc,
9360 struct sctp_nets *net;
9362 /* Validate that a timer is running somewhere */
9363 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9364 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9365 /* Here is a timer */
9369 SCTP_TCB_LOCK_ASSERT(stcb);
9370 /* Gak, we did not have a timer somewhere */
9371 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9372 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9377 sctp_chunk_output(struct sctp_inpcb *inp,
9378 struct sctp_tcb *stcb,
9381 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9387 * Ok this is the generic chunk service queue. we must do the
9389 * - See if there are retransmits pending, if so we must
9391 * - Service the stream queue that is next, moving any
9392 * message (note I must get a complete message i.e.
9393 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9395 * - Check to see if the cwnd/rwnd allows any output, if so we
9396 * go ahead and fomulate and send the low level chunks. Making sure
9397 * to combine any control in the control chunk queue also.
9399 struct sctp_association *asoc;
9400 struct sctp_nets *net;
9401 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9402 burst_cnt = 0, burst_limit = 0;
9406 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9408 int fr_done, tot_frs = 0;
9411 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9412 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9418 SCTP_TCB_LOCK_ASSERT(stcb);
9420 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9422 if ((un_sent <= 0) &&
9423 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9424 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9425 (asoc->sent_queue_retran_cnt == 0)) {
9426 /* Nothing to do unless there is something to be sent left */
9430 * Do we have something to send, data or control AND a sack timer
9431 * running, if so piggy-back the sack.
9433 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9434 sctp_send_sack(stcb);
9435 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9437 while (asoc->sent_queue_retran_cnt) {
9439 * Ok, it is retransmission time only, we send out only ONE
9440 * packet with a single call off to the retran code.
9442 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9444 * Special hook for handling cookiess discarded
9445 * by peer that carried data. Send cookie-ack only
9446 * and then the next call with get the retran's.
9448 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9450 &now, &now_filled, frag_point, so_locked);
9452 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9453 /* if its not from a HB then do it */
9455 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9461 * its from any other place, we don't allow retran
9462 * output (only control)
9467 /* Can't send anymore */
9469 * now lets push out control by calling med-level
9470 * output once. this assures that we WILL send HB's
9473 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9475 &now, &now_filled, frag_point, so_locked);
9476 #ifdef SCTP_AUDITING_ENABLED
9477 sctp_auditing(8, inp, stcb, NULL);
9479 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9484 * The count was off.. retran is not happening so do
9485 * the normal retransmission.
9487 #ifdef SCTP_AUDITING_ENABLED
9488 sctp_auditing(9, inp, stcb, NULL);
9490 if (ret == SCTP_RETRAN_EXIT) {
9495 if (from_where == SCTP_OUTPUT_FROM_T3) {
9496 /* Only one transmission allowed out of a timeout */
9497 #ifdef SCTP_AUDITING_ENABLED
9498 sctp_auditing(10, inp, stcb, NULL);
9500 /* Push out any control */
9501 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9502 &now, &now_filled, frag_point, so_locked);
9505 if (tot_frs > asoc->max_burst) {
9506 /* Hit FR burst limit */
9509 if ((num_out == 0) && (ret == 0)) {
9511 /* No more retrans to send */
9515 #ifdef SCTP_AUDITING_ENABLED
9516 sctp_auditing(12, inp, stcb, NULL);
9518 /* Check for bad destinations, if they exist move chunks around. */
9519 burst_limit = asoc->max_burst;
9520 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9521 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9522 SCTP_ADDR_NOT_REACHABLE) {
9524 * if possible move things off of this address we
9525 * still may send below due to the dormant state but
9526 * we try to find an alternate address to send to
9527 * and if we have one we move all queued data on the
9528 * out wheel to this alternate address.
9530 if (net->ref_count > 1)
9531 sctp_move_to_an_alt(stcb, asoc, net);
9532 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9533 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9534 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9536 * JRS 5/14/07 - If CMT PF is on and the current
9537 * destination is in PF state, move all queued data
9538 * to an alternate desination.
9540 if (net->ref_count > 1)
9541 sctp_move_to_an_alt(stcb, asoc, net);
9544 * if ((asoc->sat_network) || (net->addr_is_local))
9545 * { burst_limit = asoc->max_burst *
9546 * SCTP_SAT_NETWORK_BURST_INCR; }
9548 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9549 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9551 * JRS - Use the congestion control
9552 * given in the congestion control
9555 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9556 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9557 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9559 SCTP_STAT_INCR(sctps_maxburstqueued);
9561 net->fast_retran_ip = 0;
9563 if (net->flight_size == 0) {
9564 /* Should be decaying the cwnd here */
9573 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9574 &reason_code, 0, from_where,
9575 &now, &now_filled, frag_point, so_locked);
9577 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9578 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9579 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9581 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9582 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9583 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9587 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9591 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9592 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9594 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9599 * When nagle is on, we look at how much is un_sent, then
9600 * if its smaller than an MTU and we have data in
9603 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9604 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9605 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9606 (stcb->asoc.total_flight > 0)) {
9610 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9611 TAILQ_EMPTY(&asoc->send_queue) &&
9612 TAILQ_EMPTY(&asoc->out_wheel)) {
9613 /* Nothing left to send */
9616 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9617 /* Nothing left to send */
9620 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9621 (burst_cnt < burst_limit)));
9623 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9624 if (burst_cnt >= burst_limit) {
9625 SCTP_STAT_INCR(sctps_maxburstqueued);
9626 asoc->burst_limit_applied = 1;
9627 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9628 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9631 asoc->burst_limit_applied = 0;
9634 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9635 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9637 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9641 * Now we need to clean up the control chunk chain if a ECNE is on
9642 * it. It must be marked as UNSENT again so next call will continue
9643 * to send it until such time that we get a CWR, to remove it.
9645 if (stcb->asoc.ecn_echo_cnt_onq)
9646 sctp_fix_ecn_echo(asoc);
9652 sctp_output(inp, m, addr, control, p, flags)
9653 struct sctp_inpcb *inp;
9655 struct sockaddr *addr;
9656 struct mbuf *control;
9661 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9664 if (inp->sctp_socket == NULL) {
9665 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9668 return (sctp_sosend(inp->sctp_socket,
9678 send_forward_tsn(struct sctp_tcb *stcb,
9679 struct sctp_association *asoc)
9681 struct sctp_tmit_chunk *chk;
9682 struct sctp_forward_tsn_chunk *fwdtsn;
9683 uint32_t advance_peer_ack_point;
9685 SCTP_TCB_LOCK_ASSERT(stcb);
9686 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9687 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9688 /* mark it to unsent */
9689 chk->sent = SCTP_DATAGRAM_UNSENT;
9691 /* Do we correct its output location? */
9692 if (chk->whoTo != asoc->primary_destination) {
9693 sctp_free_remote_addr(chk->whoTo);
9694 chk->whoTo = asoc->primary_destination;
9695 atomic_add_int(&chk->whoTo->ref_count, 1);
9697 goto sctp_fill_in_rest;
9700 /* Ok if we reach here we must build one */
9701 sctp_alloc_a_chunk(stcb, chk);
9705 chk->copy_by_ref = 0;
9706 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9707 chk->rec.chunk_id.can_take_data = 0;
9711 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9712 if (chk->data == NULL) {
9713 sctp_free_a_chunk(stcb, chk);
9716 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9717 chk->sent = SCTP_DATAGRAM_UNSENT;
9719 chk->whoTo = asoc->primary_destination;
9720 atomic_add_int(&chk->whoTo->ref_count, 1);
9721 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9722 asoc->ctrl_queue_cnt++;
9725 * Here we go through and fill out the part that deals with
9726 * stream/seq of the ones we skip.
9728 SCTP_BUF_LEN(chk->data) = 0;
9730 struct sctp_tmit_chunk *at, *tp1, *last;
9731 struct sctp_strseq *strseq;
9732 unsigned int cnt_of_space, i, ovh;
9733 unsigned int space_needed;
9734 unsigned int cnt_of_skipped = 0;
9736 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9737 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9738 /* no more to look at */
9741 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9742 /* We don't report these */
9747 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9748 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9750 cnt_of_space = M_TRAILINGSPACE(chk->data);
9752 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9753 ovh = SCTP_MIN_OVERHEAD;
9755 ovh = SCTP_MIN_V4_OVERHEAD;
9757 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9758 /* trim to a mtu size */
9759 cnt_of_space = asoc->smallest_mtu - ovh;
9761 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9762 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9763 0xff, 0, cnt_of_skipped,
9764 asoc->advanced_peer_ack_point);
9767 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9768 if (cnt_of_space < space_needed) {
9770 * ok we must trim down the chunk by lowering the
9771 * advance peer ack point.
9773 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9774 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9775 0xff, 0xff, cnt_of_space,
9778 cnt_of_skipped = (cnt_of_space -
9779 ((sizeof(struct sctp_forward_tsn_chunk)) /
9780 sizeof(struct sctp_strseq)));
9782 * Go through and find the TSN that will be the one
9785 at = TAILQ_FIRST(&asoc->sent_queue);
9786 for (i = 0; i < cnt_of_skipped; i++) {
9787 tp1 = TAILQ_NEXT(at, sctp_next);
9790 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9791 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9792 0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9793 asoc->advanced_peer_ack_point);
9797 * last now points to last one I can report, update
9800 advance_peer_ack_point = last->rec.data.TSN_seq;
9801 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9803 chk->send_size = space_needed;
9804 /* Setup the chunk */
9805 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9806 fwdtsn->ch.chunk_length = htons(chk->send_size);
9807 fwdtsn->ch.chunk_flags = 0;
9808 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9809 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9810 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9811 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9812 SCTP_BUF_LEN(chk->data) = chk->send_size;
9815 * Move pointer to after the fwdtsn and transfer to the
9818 strseq = (struct sctp_strseq *)fwdtsn;
9820 * Now populate the strseq list. This is done blindly
9821 * without pulling out duplicate stream info. This is
9822 * inefficent but won't harm the process since the peer will
9823 * look at these in sequence and will thus release anything.
9824 * It could mean we exceed the PMTU and chop off some that
9825 * we could have included.. but this is unlikely (aka 1432/4
9826 * would mean 300+ stream seq's would have to be reported in
9827 * one FWD-TSN. With a bit of work we can later FIX this to
9828 * optimize and pull out duplcates.. but it does add more
9829 * overhead. So for now... not!
9831 at = TAILQ_FIRST(&asoc->sent_queue);
9832 for (i = 0; i < cnt_of_skipped; i++) {
9833 tp1 = TAILQ_NEXT(at, sctp_next);
9834 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9835 /* We don't report these */
9840 if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9841 at->rec.data.fwd_tsn_cnt = 0;
9843 strseq->stream = ntohs(at->rec.data.stream_number);
9844 strseq->sequence = ntohs(at->rec.data.stream_seq);
9854 sctp_send_sack(struct sctp_tcb *stcb)
9857 * Queue up a SACK or NR-SACK in the control queue.
9858 * We must first check to see if a SACK or NR-SACK is
9859 * somehow on the control queue.
9860 * If so, we will take and and remove the old one.
9862 struct sctp_association *asoc;
9863 struct sctp_tmit_chunk *chk, *a_chk;
9864 struct sctp_sack_chunk *sack;
9865 struct sctp_nr_sack_chunk *nr_sack;
9866 struct sctp_gap_ack_block *gap_descriptor;
9867 struct sack_track *selector;
9872 int limit_reached = 0;
9873 unsigned int i, sel_start, siz, j, starting_index;
9874 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9877 uint32_t highest_tsn;
9881 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
9882 stcb->asoc.peer_supports_nr_sack) {
9883 type = SCTP_NR_SELECTIVE_ACK;
9885 type = SCTP_SELECTIVE_ACK;
9889 SCTP_TCB_LOCK_ASSERT(stcb);
9890 if (asoc->last_data_chunk_from == NULL) {
9891 /* Hmm we never received anything */
9894 sctp_slide_mapping_arrays(stcb);
9895 sctp_set_rwnd(stcb, asoc);
9896 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9897 if (chk->rec.chunk_id.id == type) {
9898 /* Hmm, found a sack already on queue, remove it */
9899 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9900 asoc->ctrl_queue_cnt++;
9903 sctp_m_freem(a_chk->data);
9906 sctp_free_remote_addr(a_chk->whoTo);
9907 a_chk->whoTo = NULL;
9911 if (a_chk == NULL) {
9912 sctp_alloc_a_chunk(stcb, a_chk);
9913 if (a_chk == NULL) {
9914 /* No memory so we drop the idea, and set a timer */
9915 if (stcb->asoc.delayed_ack) {
9916 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9917 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9918 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9919 stcb->sctp_ep, stcb, NULL);
9921 stcb->asoc.send_sack = 1;
9925 a_chk->copy_by_ref = 0;
9926 a_chk->rec.chunk_id.id = type;
9927 a_chk->rec.chunk_id.can_take_data = 1;
9929 /* Clear our pkt counts */
9930 asoc->data_pkts_seen = 0;
9933 a_chk->snd_count = 0;
9934 a_chk->send_size = 0; /* fill in later */
9935 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9936 a_chk->whoTo = NULL;
9938 if ((asoc->numduptsns) ||
9939 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9942 * Ok, we have some duplicates or the destination for the
9943 * sack is unreachable, lets see if we can select an
9944 * alternate than asoc->last_data_chunk_from
9946 if ((!(asoc->last_data_chunk_from->dest_state &
9947 SCTP_ADDR_NOT_REACHABLE)) &&
9948 (asoc->used_alt_onsack > asoc->numnets)) {
9949 /* We used an alt last time, don't this time */
9950 a_chk->whoTo = NULL;
9952 asoc->used_alt_onsack++;
9953 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9955 if (a_chk->whoTo == NULL) {
9956 /* Nope, no alternate */
9957 a_chk->whoTo = asoc->last_data_chunk_from;
9958 asoc->used_alt_onsack = 0;
9962 * No duplicates so we use the last place we received data
9965 asoc->used_alt_onsack = 0;
9966 a_chk->whoTo = asoc->last_data_chunk_from;
9969 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9971 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
9972 highest_tsn = asoc->highest_tsn_inside_map;
9974 highest_tsn = asoc->highest_tsn_inside_nr_map;
9976 if (highest_tsn == asoc->cumulative_tsn) {
9978 if (type == SCTP_SELECTIVE_ACK) {
9979 space_req = sizeof(struct sctp_sack_chunk);
9981 space_req = sizeof(struct sctp_nr_sack_chunk);
9984 /* gaps get a cluster */
9985 space_req = MCLBYTES;
9987 /* Ok now lets formulate a MBUF with our sack */
9988 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9989 if ((a_chk->data == NULL) ||
9990 (a_chk->whoTo == NULL)) {
9991 /* rats, no mbuf memory */
9993 /* was a problem with the destination */
9994 sctp_m_freem(a_chk->data);
9997 sctp_free_a_chunk(stcb, a_chk);
9998 /* sa_ignore NO_NULL_CHK */
9999 if (stcb->asoc.delayed_ack) {
10000 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10001 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10002 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10003 stcb->sctp_ep, stcb, NULL);
10005 stcb->asoc.send_sack = 1;
10009 /* ok, lets go through and fill it in */
10010 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10011 space = M_TRAILINGSPACE(a_chk->data);
10012 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10013 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10015 limit = mtod(a_chk->data, caddr_t);
10018 /* 0x01 is used by nonce for ecn */
10019 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10020 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10021 (asoc->peer_supports_ecn_nonce))
10022 flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10026 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10028 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10029 * received, then set high bit to 1, else 0. Reset
10032 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10033 asoc->cmt_dac_pkts_rcvd = 0;
10035 #ifdef SCTP_ASOCLOG_OF_TSNS
10036 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10037 stcb->asoc.cumack_log_atsnt++;
10038 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10039 stcb->asoc.cumack_log_atsnt = 0;
10042 /* reset the readers interpretation */
10043 stcb->freed_by_sorcv_sincelast = 0;
10045 if (type == SCTP_SELECTIVE_ACK) {
10046 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10048 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10049 if (highest_tsn > asoc->mapping_array_base_tsn) {
10050 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10052 siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10056 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10057 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10058 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10059 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10061 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10065 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10068 * The base TSN is intialized to be the first TSN the peer
10069 * will send us. If the cum-ack is behind this then when they
10070 * send us the next in sequence it will mark the base_tsn bit.
10071 * Thus we need to use the very first selector and the offset
10072 * is 1. Our table is built for this case.
10074 starting_index = 0;
10078 * we skip the first selector when the cum-ack is at or above the
10079 * mapping array base. This is because the bits at the base or above
10080 * are turned on and our first selector in the table assumes they are
10081 * off. We thus will use the second selector (first is 0). We use
10082 * the reverse of our macro to fix the offset, in bits, that our
10083 * table is at. Note that this method assumes that the cum-tsn is
10084 * within the first bit, i.e. its value is 0-7 which means the
10085 * result to our offset will be either a 0 - -7. If the cumack
10086 * is NOT in the first byte (0) (which it should be since we did
10087 * a mapping array slide above) then we need to calculate the starting
10088 * index i.e. which byte of the mapping array we should start at. We
10089 * do this by dividing by 8 and pushing the remainder (mod) into offset.
10090 * then we multiply the offset to be negative, since we need a negative
10091 * offset into the selector table.
10093 SCTP_CALC_TSN_TO_GAP(offset, asoc->cumulative_tsn, asoc->mapping_array_base_tsn);
10095 starting_index = offset / 8;
10096 offset = offset % 8;
10097 printf("Strange starting index is %d offset:%d (not 0/x)\n",
10098 starting_index, offset);
10100 starting_index = 0;
10102 /* We need a negative offset in our table */
10106 if (((type == SCTP_SELECTIVE_ACK) &&
10107 compare_with_wrap(highest_tsn, asoc->cumulative_tsn, MAX_TSN)) ||
10108 ((type == SCTP_NR_SELECTIVE_ACK) &&
10109 compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN))) {
10110 /* we have a gap .. maybe */
10111 for (i = starting_index; i < siz; i++) {
10112 if (type == SCTP_SELECTIVE_ACK) {
10113 selector = &sack_array[asoc->mapping_array[i] | asoc->nr_mapping_array[i]];
10115 selector = &sack_array[asoc->mapping_array[i]];
10117 if (mergeable && selector->right_edge) {
10119 * Backup, left and right edges were ok to
10125 if (selector->num_entries == 0)
10128 for (j = sel_start; j < selector->num_entries; j++) {
10129 if (mergeable && selector->right_edge) {
10131 * do a merge by NOT setting
10137 * no merge, set the left
10141 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10143 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10146 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10152 if (selector->left_edge) {
10156 if (limit_reached) {
10157 /* Reached the limit stop */
10164 if ((type == SCTP_NR_SELECTIVE_ACK) &&
10165 (limit_reached == 0)) {
10169 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn)
10170 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10172 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10174 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10177 * cum-ack behind the mapping array, so we start and use all
10182 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10184 * we skip the first one when the cum-ack is at or above the
10185 * mapping array base. Note this only works if
10189 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10190 /* we have a gap .. maybe */
10191 for (i = 0; i < siz; i++) {
10192 selector = &sack_array[asoc->nr_mapping_array[i]];
10193 if (mergeable && selector->right_edge) {
10195 * Backup, left and right edges were
10198 num_nr_gap_blocks--;
10201 if (selector->num_entries == 0)
10204 for (j = sel_start; j < selector->num_entries; j++) {
10205 if (mergeable && selector->right_edge) {
10207 * do a merge by NOT
10214 * no merge, set the
10218 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10220 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10221 num_nr_gap_blocks++;
10223 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10229 if (selector->left_edge) {
10233 if (limit_reached) {
10234 /* Reached the limit stop */
10242 /* now we must add any dups we are going to report. */
10243 if ((limit_reached == 0) && (asoc->numduptsns)) {
10244 dup = (uint32_t *) gap_descriptor;
10245 for (i = 0; i < asoc->numduptsns; i++) {
10246 *dup = htonl(asoc->dup_tsns[i]);
10249 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10254 asoc->numduptsns = 0;
10257 * now that the chunk is prepared queue it to the control chunk
10260 if (type == SCTP_SELECTIVE_ACK) {
10261 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10262 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10263 num_dups * sizeof(int32_t);
10264 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10265 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10266 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10267 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10268 sack->sack.num_dup_tsns = htons(num_dups);
10269 sack->ch.chunk_type = type;
10270 sack->ch.chunk_flags = flags;
10271 sack->ch.chunk_length = htons(a_chk->send_size);
10273 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10274 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10275 num_dups * sizeof(int32_t);
10276 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10277 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10278 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10279 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10280 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10281 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10282 nr_sack->nr_sack.reserved = 0;
10283 nr_sack->ch.chunk_type = type;
10284 nr_sack->ch.chunk_flags = flags;
10285 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10287 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10288 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10289 asoc->ctrl_queue_cnt++;
10290 asoc->send_sack = 0;
10291 SCTP_STAT_INCR(sctps_sendsacks);
10296 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10297 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10302 struct mbuf *m_abort;
10303 struct mbuf *m_out = NULL, *m_end = NULL;
10304 struct sctp_abort_chunk *abort = NULL;
10306 uint32_t auth_offset = 0;
10307 struct sctp_auth_chunk *auth = NULL;
10310 * Add an AUTH chunk, if chunk requires it and save the offset into
10311 * the chain for AUTH
10313 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10314 stcb->asoc.peer_auth_chunks)) {
10315 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10316 stcb, SCTP_ABORT_ASSOCIATION);
10317 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10319 SCTP_TCB_LOCK_ASSERT(stcb);
10320 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10321 if (m_abort == NULL) {
10324 sctp_m_freem(m_out);
10327 /* link in any error */
10328 SCTP_BUF_NEXT(m_abort) = operr;
10335 sz += SCTP_BUF_LEN(n);
10336 n = SCTP_BUF_NEXT(n);
10339 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10340 if (m_out == NULL) {
10341 /* NO Auth chunk prepended, so reserve space in front */
10342 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10345 /* Put AUTH chunk at the front of the chain */
10346 SCTP_BUF_NEXT(m_end) = m_abort;
10349 /* fill in the ABORT chunk */
10350 abort = mtod(m_abort, struct sctp_abort_chunk *);
10351 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10352 abort->ch.chunk_flags = 0;
10353 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10355 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10356 stcb->asoc.primary_destination,
10357 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10358 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10359 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10360 stcb->asoc.primary_destination->port, so_locked, NULL);
10361 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10365 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10366 struct sctp_nets *net,
10369 /* formulate and SEND a SHUTDOWN-COMPLETE */
10370 struct mbuf *m_shutdown_comp;
10371 struct sctp_shutdown_complete_chunk *shutdown_complete;
10375 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10376 if (m_shutdown_comp == NULL) {
10380 if (reflect_vtag) {
10381 flags = SCTP_HAD_NO_TCB;
10382 vtag = stcb->asoc.my_vtag;
10385 vtag = stcb->asoc.peer_vtag;
10387 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10388 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10389 shutdown_complete->ch.chunk_flags = flags;
10390 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10391 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10392 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10393 (struct sockaddr *)&net->ro._l_addr,
10394 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10395 stcb->sctp_ep->sctp_lport, stcb->rport,
10397 net->port, SCTP_SO_NOT_LOCKED, NULL);
10398 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10403 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10404 uint32_t vrf_id, uint16_t port)
10406 /* formulate and SEND a SHUTDOWN-COMPLETE */
10407 struct mbuf *o_pak;
10409 struct ip *iph, *iph_out;
10410 struct udphdr *udp = NULL;
10413 struct ip6_hdr *ip6, *ip6_out;
10416 int offset_out, len, mlen;
10417 struct sctp_shutdown_complete_msg *comp_cp;
10419 iph = mtod(m, struct ip *);
10420 switch (iph->ip_v) {
10422 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10425 case IPV6_VERSION >> 4:
10426 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10433 len += sizeof(struct udphdr);
10435 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10436 if (mout == NULL) {
10439 SCTP_BUF_RESV_UF(mout, max_linkhdr);
10440 SCTP_BUF_LEN(mout) = len;
10441 SCTP_BUF_NEXT(mout) = NULL;
10448 switch (iph->ip_v) {
10450 iph_out = mtod(mout, struct ip *);
10452 /* Fill in the IP header for the ABORT */
10453 iph_out->ip_v = IPVERSION;
10454 iph_out->ip_hl = (sizeof(struct ip) / 4);
10455 iph_out->ip_tos = (u_char)0;
10456 iph_out->ip_id = 0;
10457 iph_out->ip_off = 0;
10458 iph_out->ip_ttl = MAXTTL;
10460 iph_out->ip_p = IPPROTO_UDP;
10462 iph_out->ip_p = IPPROTO_SCTP;
10464 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10465 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10467 /* let IP layer calculate this */
10468 iph_out->ip_sum = 0;
10469 offset_out += sizeof(*iph_out);
10470 comp_cp = (struct sctp_shutdown_complete_msg *)(
10471 (caddr_t)iph_out + offset_out);
10474 case IPV6_VERSION >> 4:
10475 ip6 = (struct ip6_hdr *)iph;
10476 ip6_out = mtod(mout, struct ip6_hdr *);
10478 /* Fill in the IPv6 header for the ABORT */
10479 ip6_out->ip6_flow = ip6->ip6_flow;
10480 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10482 ip6_out->ip6_nxt = IPPROTO_UDP;
10484 ip6_out->ip6_nxt = IPPROTO_SCTP;
10486 ip6_out->ip6_src = ip6->ip6_dst;
10487 ip6_out->ip6_dst = ip6->ip6_src;
10489 * ?? The old code had both the iph len + payload, I think
10490 * this is wrong and would never have worked
10492 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10493 offset_out += sizeof(*ip6_out);
10494 comp_cp = (struct sctp_shutdown_complete_msg *)(
10495 (caddr_t)ip6_out + offset_out);
10499 /* Currently not supported. */
10500 sctp_m_freem(mout);
10504 udp = (struct udphdr *)comp_cp;
10505 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10506 udp->uh_dport = port;
10507 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10508 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10509 offset_out += sizeof(struct udphdr);
10510 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10512 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10514 sctp_m_freem(mout);
10517 /* Now copy in and fill in the ABORT tags etc. */
10518 comp_cp->sh.src_port = sh->dest_port;
10519 comp_cp->sh.dest_port = sh->src_port;
10520 comp_cp->sh.checksum = 0;
10521 comp_cp->sh.v_tag = sh->v_tag;
10522 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10523 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10524 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10526 if (iph_out != NULL) {
10529 struct sctp_tcb *stcb = NULL;
10531 mlen = SCTP_BUF_LEN(mout);
10532 bzero(&ro, sizeof ro);
10533 /* set IPv4 length */
10534 iph_out->ip_len = mlen;
10535 #ifdef SCTP_PACKET_LOGGING
10536 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10537 sctp_packet_log(mout, mlen);
10540 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10541 SCTP_STAT_INCR(sctps_sendswcrc);
10542 SCTP_ENABLE_UDP_CSUM(mout);
10544 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10545 mout->m_pkthdr.csum_data = 0;
10546 SCTP_STAT_INCR(sctps_sendhwcrc);
10548 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10550 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10552 /* Free the route if we got one back */
10557 if (ip6_out != NULL) {
10558 struct route_in6 ro;
10560 struct sctp_tcb *stcb = NULL;
10561 struct ifnet *ifp = NULL;
10563 bzero(&ro, sizeof(ro));
10564 mlen = SCTP_BUF_LEN(mout);
10565 #ifdef SCTP_PACKET_LOGGING
10566 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10567 sctp_packet_log(mout, mlen);
10569 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10571 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10573 (stcb->asoc.loopback_scope))) {
10574 comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10575 SCTP_STAT_INCR(sctps_sendswcrc);
10577 SCTP_STAT_INCR(sctps_sendnocrc);
10579 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10580 udp->uh_sum = 0xffff;
10583 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10585 (stcb->asoc.loopback_scope))) {
10586 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10587 mout->m_pkthdr.csum_data = 0;
10588 SCTP_STAT_INCR(sctps_sendhwcrc);
10590 SCTP_STAT_INCR(sctps_sendnocrc);
10593 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10595 /* Free the route if we got one back */
10600 SCTP_STAT_INCR(sctps_sendpackets);
10601 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10602 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10607 static struct sctp_nets *
10608 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10610 struct sctp_nets *net, *hnet;
10611 int ms_goneby, highest_ms, state_overide = 0;
10613 (void)SCTP_GETTIME_TIMEVAL(now);
10616 SCTP_TCB_LOCK_ASSERT(stcb);
10617 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10619 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10620 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10623 * Skip this guy from consideration if HB is off AND
10628 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10629 /* skip this dest net from consideration */
10632 if (net->last_sent_time.tv_sec) {
10633 /* Sent to so we subtract */
10634 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10636 /* Never been sent to */
10637 ms_goneby = 0x7fffffff;
10639 * When the address state is unconfirmed but still
10640 * considered reachable, we HB at a higher rate. Once it
10641 * goes confirmed OR reaches the "unreachable" state, thenw
10642 * we cut it back to HB at a more normal pace.
10644 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10650 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10651 (ms_goneby > highest_ms)) {
10652 highest_ms = ms_goneby;
10657 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10663 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10665 * Found the one with longest delay bounds OR it is
10666 * unconfirmed and still not marked unreachable.
10668 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10671 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10672 (struct sockaddr *)&hnet->ro._l_addr);
10674 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10677 /* update the timer now */
10678 hnet->last_sent_time = *now;
10681 /* Nothing to HB */
10686 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10688 struct sctp_tmit_chunk *chk;
10689 struct sctp_nets *net;
10690 struct sctp_heartbeat_chunk *hb;
10691 struct timeval now;
10692 struct sockaddr_in *sin;
10693 struct sockaddr_in6 *sin6;
10695 SCTP_TCB_LOCK_ASSERT(stcb);
10696 if (user_req == 0) {
10697 net = sctp_select_hb_destination(stcb, &now);
10700 * All our busy none to send to, just start the
10703 if (stcb->asoc.state == 0) {
10706 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10717 (void)SCTP_GETTIME_TIMEVAL(&now);
10719 sin = (struct sockaddr_in *)&net->ro._l_addr;
10720 if (sin->sin_family != AF_INET) {
10721 if (sin->sin_family != AF_INET6) {
10726 sctp_alloc_a_chunk(stcb, chk);
10728 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10731 chk->copy_by_ref = 0;
10732 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10733 chk->rec.chunk_id.can_take_data = 1;
10734 chk->asoc = &stcb->asoc;
10735 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10737 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10738 if (chk->data == NULL) {
10739 sctp_free_a_chunk(stcb, chk);
10742 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10743 SCTP_BUF_LEN(chk->data) = chk->send_size;
10744 chk->sent = SCTP_DATAGRAM_UNSENT;
10745 chk->snd_count = 0;
10747 atomic_add_int(&chk->whoTo->ref_count, 1);
10748 /* Now we have a mbuf that we can fill in with the details */
10749 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10750 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10751 /* fill out chunk header */
10752 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10753 hb->ch.chunk_flags = 0;
10754 hb->ch.chunk_length = htons(chk->send_size);
10755 /* Fill out hb parameter */
10756 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10757 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10758 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10759 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10760 /* Did our user request this one, put it in */
10761 hb->heartbeat.hb_info.user_req = user_req;
10762 hb->heartbeat.hb_info.addr_family = sin->sin_family;
10763 hb->heartbeat.hb_info.addr_len = sin->sin_len;
10764 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10766 * we only take from the entropy pool if the address is not
10769 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10770 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10772 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10773 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10775 if (sin->sin_family == AF_INET) {
10776 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10777 } else if (sin->sin_family == AF_INET6) {
10778 /* We leave the scope the way it is in our lookup table. */
10779 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10780 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10782 /* huh compiler bug */
10787 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10788 * PF-heartbeats. Because of this, threshold management is done by
10789 * the t3 timer handler, and does not need to be done upon the send
10790 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10791 * heartbeat is being sent is in PF state, do NOT do threshold
10794 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10795 /* ok we have a destination that needs a beat */
10796 /* lets do the theshold management Qiaobing style */
10797 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10798 stcb->asoc.max_send_times)) {
10800 * we have lost the association, in a way this is
10801 * quite bad since we really are one less time since
10802 * we really did not send yet. This is the down side
10803 * to the Q's style as defined in the RFC and not my
10804 * alternate style defined in the RFC.
10806 if (chk->data != NULL) {
10807 sctp_m_freem(chk->data);
10811 * Here we do NOT use the macro since the
10812 * association is now gone.
10815 sctp_free_remote_addr(chk->whoTo);
10818 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10822 net->hb_responded = 0;
10823 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10824 stcb->asoc.ctrl_queue_cnt++;
10825 SCTP_STAT_INCR(sctps_sendheartbeat);
10827 * Call directly med level routine to put out the chunk. It will
10828 * always tumble out control chunks aka HB but it may even tumble
10835 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10838 struct sctp_association *asoc;
10839 struct sctp_ecne_chunk *ecne;
10840 struct sctp_tmit_chunk *chk;
10842 asoc = &stcb->asoc;
10843 SCTP_TCB_LOCK_ASSERT(stcb);
10844 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10845 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10846 /* found a previous ECN_ECHO update it if needed */
10847 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10848 ecne->tsn = htonl(high_tsn);
10852 /* nope could not find one to update so we must build one */
10853 sctp_alloc_a_chunk(stcb, chk);
10857 chk->copy_by_ref = 0;
10858 SCTP_STAT_INCR(sctps_sendecne);
10859 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10860 chk->rec.chunk_id.can_take_data = 0;
10861 chk->asoc = &stcb->asoc;
10862 chk->send_size = sizeof(struct sctp_ecne_chunk);
10863 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10864 if (chk->data == NULL) {
10865 sctp_free_a_chunk(stcb, chk);
10868 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10869 SCTP_BUF_LEN(chk->data) = chk->send_size;
10870 chk->sent = SCTP_DATAGRAM_UNSENT;
10871 chk->snd_count = 0;
10873 atomic_add_int(&chk->whoTo->ref_count, 1);
10874 stcb->asoc.ecn_echo_cnt_onq++;
10875 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10876 ecne->ch.chunk_type = SCTP_ECN_ECHO;
10877 ecne->ch.chunk_flags = 0;
10878 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10879 ecne->tsn = htonl(high_tsn);
10880 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10881 asoc->ctrl_queue_cnt++;
10885 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10886 struct mbuf *m, int iphlen, int bad_crc)
10888 struct sctp_association *asoc;
10889 struct sctp_pktdrop_chunk *drp;
10890 struct sctp_tmit_chunk *chk;
10897 struct ip6_hdr *ip6h;
10900 int fullsz = 0, extra = 0;
10903 struct sctp_chunkhdr *ch, chunk_buf;
10904 unsigned int chk_length;
10909 asoc = &stcb->asoc;
10910 SCTP_TCB_LOCK_ASSERT(stcb);
10911 if (asoc->peer_supports_pktdrop == 0) {
10913 * peer must declare support before I send one.
10917 if (stcb->sctp_socket == NULL) {
10920 sctp_alloc_a_chunk(stcb, chk);
10924 chk->copy_by_ref = 0;
10925 iph = mtod(m, struct ip *);
10927 sctp_free_a_chunk(stcb, chk);
10930 switch (iph->ip_v) {
10933 len = chk->send_size = iph->ip_len;
10936 case IPV6_VERSION >> 4:
10938 ip6h = mtod(m, struct ip6_hdr *);
10939 len = chk->send_size = htons(ip6h->ip6_plen);
10945 /* Validate that we do not have an ABORT in here. */
10946 offset = iphlen + sizeof(struct sctphdr);
10947 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10948 sizeof(*ch), (uint8_t *) & chunk_buf);
10949 while (ch != NULL) {
10950 chk_length = ntohs(ch->chunk_length);
10951 if (chk_length < sizeof(*ch)) {
10952 /* break to abort land */
10955 switch (ch->chunk_type) {
10956 case SCTP_PACKET_DROPPED:
10957 case SCTP_ABORT_ASSOCIATION:
10959 * we don't respond with an PKT-DROP to an ABORT
10962 sctp_free_a_chunk(stcb, chk);
10967 offset += SCTP_SIZE32(chk_length);
10968 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10969 sizeof(*ch), (uint8_t *) & chunk_buf);
10972 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10973 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10975 * only send 1 mtu worth, trim off the excess on the end.
10977 fullsz = len - extra;
10978 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10981 chk->asoc = &stcb->asoc;
10982 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10983 if (chk->data == NULL) {
10985 sctp_free_a_chunk(stcb, chk);
10988 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10989 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10991 sctp_m_freem(chk->data);
10995 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10996 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10997 chk->book_size_scale = 0;
10999 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11000 drp->trunc_len = htons(fullsz);
11002 * Len is already adjusted to size minus overhead above take
11003 * out the pkt_drop chunk itself from it.
11005 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11006 len = chk->send_size;
11008 /* no truncation needed */
11009 drp->ch.chunk_flags = 0;
11010 drp->trunc_len = htons(0);
11013 drp->ch.chunk_flags |= SCTP_BADCRC;
11015 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11016 SCTP_BUF_LEN(chk->data) = chk->send_size;
11017 chk->sent = SCTP_DATAGRAM_UNSENT;
11018 chk->snd_count = 0;
11020 /* we should hit here */
11023 chk->whoTo = asoc->primary_destination;
11025 atomic_add_int(&chk->whoTo->ref_count, 1);
11026 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11027 chk->rec.chunk_id.can_take_data = 1;
11028 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11029 drp->ch.chunk_length = htons(chk->send_size);
11030 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11034 drp->bottle_bw = htonl(spc);
11035 if (asoc->my_rwnd) {
11036 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11037 asoc->size_on_all_streams +
11038 asoc->my_rwnd_control_len +
11039 stcb->sctp_socket->so_rcv.sb_cc);
11042 * If my rwnd is 0, possibly from mbuf depletion as well as
11043 * space used, tell the peer there is NO space aka onq == bw
11045 drp->current_onq = htonl(spc);
11049 m_copydata(m, iphlen, len, (caddr_t)datap);
11050 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11051 asoc->ctrl_queue_cnt++;
11055 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11057 struct sctp_association *asoc;
11058 struct sctp_cwr_chunk *cwr;
11059 struct sctp_tmit_chunk *chk;
11061 asoc = &stcb->asoc;
11062 SCTP_TCB_LOCK_ASSERT(stcb);
11063 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11064 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11065 /* found a previous ECN_CWR update it if needed */
11066 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11067 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11069 cwr->tsn = htonl(high_tsn);
11074 /* nope could not find one to update so we must build one */
11075 sctp_alloc_a_chunk(stcb, chk);
11079 chk->copy_by_ref = 0;
11080 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11081 chk->rec.chunk_id.can_take_data = 1;
11082 chk->asoc = &stcb->asoc;
11083 chk->send_size = sizeof(struct sctp_cwr_chunk);
11084 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11085 if (chk->data == NULL) {
11086 sctp_free_a_chunk(stcb, chk);
11089 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11090 SCTP_BUF_LEN(chk->data) = chk->send_size;
11091 chk->sent = SCTP_DATAGRAM_UNSENT;
11092 chk->snd_count = 0;
11094 atomic_add_int(&chk->whoTo->ref_count, 1);
11095 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11096 cwr->ch.chunk_type = SCTP_ECN_CWR;
11097 cwr->ch.chunk_flags = 0;
11098 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11099 cwr->tsn = htonl(high_tsn);
11100 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11101 asoc->ctrl_queue_cnt++;
11105 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11106 int number_entries, uint16_t * list,
11107 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11109 int len, old_len, i;
11110 struct sctp_stream_reset_out_request *req_out;
11111 struct sctp_chunkhdr *ch;
11113 ch = mtod(chk->data, struct sctp_chunkhdr *);
11116 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11118 /* get to new offset for the param. */
11119 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11120 /* now how long will this param be? */
11121 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11122 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11123 req_out->ph.param_length = htons(len);
11124 req_out->request_seq = htonl(seq);
11125 req_out->response_seq = htonl(resp_seq);
11126 req_out->send_reset_at_tsn = htonl(last_sent);
11127 if (number_entries) {
11128 for (i = 0; i < number_entries; i++) {
11129 req_out->list_of_streams[i] = htons(list[i]);
11132 if (SCTP_SIZE32(len) > len) {
11134 * Need to worry about the pad we may end up adding to the
11135 * end. This is easy since the struct is either aligned to 4
11136 * bytes or 2 bytes off.
11138 req_out->list_of_streams[number_entries] = 0;
11140 /* now fix the chunk length */
11141 ch->chunk_length = htons(len + old_len);
11142 chk->book_size = len + old_len;
11143 chk->book_size_scale = 0;
11144 chk->send_size = SCTP_SIZE32(chk->book_size);
11145 SCTP_BUF_LEN(chk->data) = chk->send_size;
11151 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11152 int number_entries, uint16_t * list,
11155 int len, old_len, i;
11156 struct sctp_stream_reset_in_request *req_in;
11157 struct sctp_chunkhdr *ch;
11159 ch = mtod(chk->data, struct sctp_chunkhdr *);
11162 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11164 /* get to new offset for the param. */
11165 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11166 /* now how long will this param be? */
11167 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11168 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11169 req_in->ph.param_length = htons(len);
11170 req_in->request_seq = htonl(seq);
11171 if (number_entries) {
11172 for (i = 0; i < number_entries; i++) {
11173 req_in->list_of_streams[i] = htons(list[i]);
11176 if (SCTP_SIZE32(len) > len) {
11178 * Need to worry about the pad we may end up adding to the
11179 * end. This is easy since the struct is either aligned to 4
11180 * bytes or 2 bytes off.
11182 req_in->list_of_streams[number_entries] = 0;
11184 /* now fix the chunk length */
11185 ch->chunk_length = htons(len + old_len);
11186 chk->book_size = len + old_len;
11187 chk->book_size_scale = 0;
11188 chk->send_size = SCTP_SIZE32(chk->book_size);
11189 SCTP_BUF_LEN(chk->data) = chk->send_size;
11195 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11199 struct sctp_stream_reset_tsn_request *req_tsn;
11200 struct sctp_chunkhdr *ch;
11202 ch = mtod(chk->data, struct sctp_chunkhdr *);
11205 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11207 /* get to new offset for the param. */
11208 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11209 /* now how long will this param be? */
11210 len = sizeof(struct sctp_stream_reset_tsn_request);
11211 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11212 req_tsn->ph.param_length = htons(len);
11213 req_tsn->request_seq = htonl(seq);
11215 /* now fix the chunk length */
11216 ch->chunk_length = htons(len + old_len);
11217 chk->send_size = len + old_len;
11218 chk->book_size = SCTP_SIZE32(chk->send_size);
11219 chk->book_size_scale = 0;
11220 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11225 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11226 uint32_t resp_seq, uint32_t result)
11229 struct sctp_stream_reset_response *resp;
11230 struct sctp_chunkhdr *ch;
11232 ch = mtod(chk->data, struct sctp_chunkhdr *);
11235 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11237 /* get to new offset for the param. */
11238 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11239 /* now how long will this param be? */
11240 len = sizeof(struct sctp_stream_reset_response);
11241 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11242 resp->ph.param_length = htons(len);
11243 resp->response_seq = htonl(resp_seq);
11244 resp->result = ntohl(result);
11246 /* now fix the chunk length */
11247 ch->chunk_length = htons(len + old_len);
11248 chk->book_size = len + old_len;
11249 chk->book_size_scale = 0;
11250 chk->send_size = SCTP_SIZE32(chk->book_size);
11251 SCTP_BUF_LEN(chk->data) = chk->send_size;
11258 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11259 uint32_t resp_seq, uint32_t result,
11260 uint32_t send_una, uint32_t recv_next)
11263 struct sctp_stream_reset_response_tsn *resp;
11264 struct sctp_chunkhdr *ch;
11266 ch = mtod(chk->data, struct sctp_chunkhdr *);
11269 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11271 /* get to new offset for the param. */
11272 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11273 /* now how long will this param be? */
11274 len = sizeof(struct sctp_stream_reset_response_tsn);
11275 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11276 resp->ph.param_length = htons(len);
11277 resp->response_seq = htonl(resp_seq);
11278 resp->result = htonl(result);
11279 resp->senders_next_tsn = htonl(send_una);
11280 resp->receivers_next_tsn = htonl(recv_next);
11282 /* now fix the chunk length */
11283 ch->chunk_length = htons(len + old_len);
11284 chk->book_size = len + old_len;
11285 chk->send_size = SCTP_SIZE32(chk->book_size);
11286 chk->book_size_scale = 0;
11287 SCTP_BUF_LEN(chk->data) = chk->send_size;
11292 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11297 struct sctp_chunkhdr *ch;
11298 struct sctp_stream_reset_add_strm *addstr;
11300 ch = mtod(chk->data, struct sctp_chunkhdr *);
11301 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11303 /* get to new offset for the param. */
11304 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11305 /* now how long will this param be? */
11306 len = sizeof(struct sctp_stream_reset_add_strm);
11309 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11310 addstr->ph.param_length = htons(len);
11311 addstr->request_seq = htonl(seq);
11312 addstr->number_of_streams = htons(adding);
11313 addstr->reserved = 0;
11315 /* now fix the chunk length */
11316 ch->chunk_length = htons(len + old_len);
11317 chk->send_size = len + old_len;
11318 chk->book_size = SCTP_SIZE32(chk->send_size);
11319 chk->book_size_scale = 0;
11320 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11325 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11326 int number_entries, uint16_t * list,
11327 uint8_t send_out_req,
11329 uint8_t send_in_req,
11330 uint8_t send_tsn_req,
11331 uint8_t add_stream,
11336 struct sctp_association *asoc;
11337 struct sctp_tmit_chunk *chk;
11338 struct sctp_chunkhdr *ch;
11341 asoc = &stcb->asoc;
11342 if (asoc->stream_reset_outstanding) {
11344 * Already one pending, must get ACK back to clear the flag.
11346 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11349 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11350 (add_stream == 0)) {
11351 /* nothing to do */
11352 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11355 if (send_tsn_req && (send_out_req || send_in_req)) {
11356 /* error, can't do that */
11357 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11360 sctp_alloc_a_chunk(stcb, chk);
11362 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11365 chk->copy_by_ref = 0;
11366 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11367 chk->rec.chunk_id.can_take_data = 0;
11368 chk->asoc = &stcb->asoc;
11369 chk->book_size = sizeof(struct sctp_chunkhdr);
11370 chk->send_size = SCTP_SIZE32(chk->book_size);
11371 chk->book_size_scale = 0;
11373 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11374 if (chk->data == NULL) {
11375 sctp_free_a_chunk(stcb, chk);
11376 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11379 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11381 /* setup chunk parameters */
11382 chk->sent = SCTP_DATAGRAM_UNSENT;
11383 chk->snd_count = 0;
11384 chk->whoTo = asoc->primary_destination;
11385 atomic_add_int(&chk->whoTo->ref_count, 1);
11387 ch = mtod(chk->data, struct sctp_chunkhdr *);
11388 ch->chunk_type = SCTP_STREAM_RESET;
11389 ch->chunk_flags = 0;
11390 ch->chunk_length = htons(chk->book_size);
11391 SCTP_BUF_LEN(chk->data) = chk->send_size;
11393 seq = stcb->asoc.str_reset_seq_out;
11394 if (send_out_req) {
11395 sctp_add_stream_reset_out(chk, number_entries, list,
11396 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11397 asoc->stream_reset_out_is_outstanding = 1;
11399 asoc->stream_reset_outstanding++;
11402 sctp_add_a_stream(chk, seq, adding);
11404 asoc->stream_reset_outstanding++;
11407 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11408 asoc->stream_reset_outstanding++;
11410 if (send_tsn_req) {
11411 sctp_add_stream_reset_tsn(chk, seq);
11412 asoc->stream_reset_outstanding++;
11414 asoc->str_reset = chk;
11416 /* insert the chunk for sending */
11417 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11420 asoc->ctrl_queue_cnt++;
11421 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11426 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11427 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11430 * Formulate the abort message, and send it back down.
11432 struct mbuf *o_pak;
11434 struct sctp_abort_msg *abm;
11435 struct ip *iph, *iph_out;
11436 struct udphdr *udp;
11439 struct ip6_hdr *ip6, *ip6_out;
11442 int iphlen_out, len;
11444 /* don't respond to ABORT with ABORT */
11445 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11447 sctp_m_freem(err_cause);
11450 iph = mtod(m, struct ip *);
11451 switch (iph->ip_v) {
11453 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11456 case IPV6_VERSION >> 4:
11457 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11462 sctp_m_freem(err_cause);
11467 len += sizeof(struct udphdr);
11469 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11470 if (mout == NULL) {
11472 sctp_m_freem(err_cause);
11476 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11477 SCTP_BUF_LEN(mout) = len;
11478 SCTP_BUF_NEXT(mout) = err_cause;
11483 switch (iph->ip_v) {
11485 iph_out = mtod(mout, struct ip *);
11487 /* Fill in the IP header for the ABORT */
11488 iph_out->ip_v = IPVERSION;
11489 iph_out->ip_hl = (sizeof(struct ip) / 4);
11490 iph_out->ip_tos = (u_char)0;
11491 iph_out->ip_id = 0;
11492 iph_out->ip_off = 0;
11493 iph_out->ip_ttl = MAXTTL;
11495 iph_out->ip_p = IPPROTO_UDP;
11497 iph_out->ip_p = IPPROTO_SCTP;
11499 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11500 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11501 /* let IP layer calculate this */
11502 iph_out->ip_sum = 0;
11504 iphlen_out = sizeof(*iph_out);
11505 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11508 case IPV6_VERSION >> 4:
11509 ip6 = (struct ip6_hdr *)iph;
11510 ip6_out = mtod(mout, struct ip6_hdr *);
11512 /* Fill in the IP6 header for the ABORT */
11513 ip6_out->ip6_flow = ip6->ip6_flow;
11514 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11516 ip6_out->ip6_nxt = IPPROTO_UDP;
11518 ip6_out->ip6_nxt = IPPROTO_SCTP;
11520 ip6_out->ip6_src = ip6->ip6_dst;
11521 ip6_out->ip6_dst = ip6->ip6_src;
11523 iphlen_out = sizeof(*ip6_out);
11524 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11528 /* Currently not supported */
11529 sctp_m_freem(mout);
11533 udp = (struct udphdr *)abm;
11535 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11536 udp->uh_dport = port;
11537 /* set udp->uh_ulen later */
11539 iphlen_out += sizeof(struct udphdr);
11540 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11542 abm->sh.src_port = sh->dest_port;
11543 abm->sh.dest_port = sh->src_port;
11544 abm->sh.checksum = 0;
11546 abm->sh.v_tag = sh->v_tag;
11547 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11549 abm->sh.v_tag = htonl(vtag);
11550 abm->msg.ch.chunk_flags = 0;
11552 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11555 struct mbuf *m_tmp = err_cause;
11558 /* get length of the err_cause chain */
11559 while (m_tmp != NULL) {
11560 err_len += SCTP_BUF_LEN(m_tmp);
11561 m_tmp = SCTP_BUF_NEXT(m_tmp);
11563 len = SCTP_BUF_LEN(mout) + err_len;
11565 /* need pad at end of chunk */
11566 uint32_t cpthis = 0;
11569 padlen = 4 - (len % 4);
11570 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11573 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11575 len = SCTP_BUF_LEN(mout);
11576 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11579 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11581 sctp_m_freem(mout);
11584 if (iph_out != NULL) {
11586 struct sctp_tcb *stcb = NULL;
11589 /* zap the stack pointer to the route */
11590 bzero(&ro, sizeof ro);
11592 udp->uh_ulen = htons(len - sizeof(struct ip));
11593 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11595 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11596 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11597 /* set IPv4 length */
11598 iph_out->ip_len = len;
11600 #ifdef SCTP_PACKET_LOGGING
11601 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11602 sctp_packet_log(mout, len);
11604 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11606 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11607 SCTP_STAT_INCR(sctps_sendswcrc);
11608 SCTP_ENABLE_UDP_CSUM(o_pak);
11610 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11611 mout->m_pkthdr.csum_data = 0;
11612 SCTP_STAT_INCR(sctps_sendhwcrc);
11614 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11616 /* Free the route if we got one back */
11621 if (ip6_out != NULL) {
11622 struct route_in6 ro;
11624 struct sctp_tcb *stcb = NULL;
11625 struct ifnet *ifp = NULL;
11627 /* zap the stack pointer to the route */
11628 bzero(&ro, sizeof(ro));
11630 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11632 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11633 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11634 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11635 #ifdef SCTP_PACKET_LOGGING
11636 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11637 sctp_packet_log(mout, len);
11639 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11641 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11643 (stcb->asoc.loopback_scope))) {
11644 abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11645 SCTP_STAT_INCR(sctps_sendswcrc);
11647 SCTP_STAT_INCR(sctps_sendnocrc);
11649 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11650 udp->uh_sum = 0xffff;
11653 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11655 (stcb->asoc.loopback_scope))) {
11656 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11657 mout->m_pkthdr.csum_data = 0;
11658 SCTP_STAT_INCR(sctps_sendhwcrc);
11660 SCTP_STAT_INCR(sctps_sendnocrc);
11663 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11665 /* Free the route if we got one back */
11670 SCTP_STAT_INCR(sctps_sendpackets);
11671 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11672 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11676 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11677 uint32_t vrf_id, uint16_t port)
11679 struct mbuf *o_pak;
11680 struct sctphdr *sh, *sh_out;
11681 struct sctp_chunkhdr *ch;
11682 struct ip *iph, *iph_out;
11683 struct udphdr *udp = NULL;
11687 struct ip6_hdr *ip6, *ip6_out;
11690 int iphlen_out, len;
11692 iph = mtod(m, struct ip *);
11693 sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11694 switch (iph->ip_v) {
11696 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11699 case IPV6_VERSION >> 4:
11700 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11710 len += sizeof(struct udphdr);
11712 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11713 if (mout == NULL) {
11719 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11720 SCTP_BUF_LEN(mout) = len;
11721 SCTP_BUF_NEXT(mout) = scm;
11726 switch (iph->ip_v) {
11728 iph_out = mtod(mout, struct ip *);
11730 /* Fill in the IP header for the ABORT */
11731 iph_out->ip_v = IPVERSION;
11732 iph_out->ip_hl = (sizeof(struct ip) / 4);
11733 iph_out->ip_tos = (u_char)0;
11734 iph_out->ip_id = 0;
11735 iph_out->ip_off = 0;
11736 iph_out->ip_ttl = MAXTTL;
11738 iph_out->ip_p = IPPROTO_UDP;
11740 iph_out->ip_p = IPPROTO_SCTP;
11742 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11743 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11744 /* let IP layer calculate this */
11745 iph_out->ip_sum = 0;
11747 iphlen_out = sizeof(struct ip);
11748 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11751 case IPV6_VERSION >> 4:
11752 ip6 = (struct ip6_hdr *)iph;
11753 ip6_out = mtod(mout, struct ip6_hdr *);
11755 /* Fill in the IP6 header for the ABORT */
11756 ip6_out->ip6_flow = ip6->ip6_flow;
11757 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11759 ip6_out->ip6_nxt = IPPROTO_UDP;
11761 ip6_out->ip6_nxt = IPPROTO_SCTP;
11763 ip6_out->ip6_src = ip6->ip6_dst;
11764 ip6_out->ip6_dst = ip6->ip6_src;
11766 iphlen_out = sizeof(struct ip6_hdr);
11767 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11771 /* Currently not supported */
11772 sctp_m_freem(mout);
11776 udp = (struct udphdr *)sh_out;
11778 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11779 udp->uh_dport = port;
11780 /* set udp->uh_ulen later */
11782 iphlen_out += sizeof(struct udphdr);
11783 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11785 sh_out->src_port = sh->dest_port;
11786 sh_out->dest_port = sh->src_port;
11787 sh_out->v_tag = vtag;
11788 sh_out->checksum = 0;
11790 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11791 ch->chunk_type = SCTP_OPERATION_ERROR;
11792 ch->chunk_flags = 0;
11795 struct mbuf *m_tmp = scm;
11798 /* get length of the err_cause chain */
11799 while (m_tmp != NULL) {
11800 cause_len += SCTP_BUF_LEN(m_tmp);
11801 m_tmp = SCTP_BUF_NEXT(m_tmp);
11803 len = SCTP_BUF_LEN(mout) + cause_len;
11804 if (cause_len % 4) {
11805 /* need pad at end of chunk */
11806 uint32_t cpthis = 0;
11809 padlen = 4 - (len % 4);
11810 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11813 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11815 len = SCTP_BUF_LEN(mout);
11816 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11819 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11821 sctp_m_freem(mout);
11824 if (iph_out != NULL) {
11826 struct sctp_tcb *stcb = NULL;
11829 /* zap the stack pointer to the route */
11830 bzero(&ro, sizeof ro);
11832 udp->uh_ulen = htons(len - sizeof(struct ip));
11833 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11835 /* set IPv4 length */
11836 iph_out->ip_len = len;
11838 #ifdef SCTP_PACKET_LOGGING
11839 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11840 sctp_packet_log(mout, len);
11842 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11844 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11845 SCTP_STAT_INCR(sctps_sendswcrc);
11846 SCTP_ENABLE_UDP_CSUM(o_pak);
11848 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11849 mout->m_pkthdr.csum_data = 0;
11850 SCTP_STAT_INCR(sctps_sendhwcrc);
11852 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11854 /* Free the route if we got one back */
11859 if (ip6_out != NULL) {
11860 struct route_in6 ro;
11862 struct sctp_tcb *stcb = NULL;
11863 struct ifnet *ifp = NULL;
11865 /* zap the stack pointer to the route */
11866 bzero(&ro, sizeof(ro));
11868 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11870 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11871 #ifdef SCTP_PACKET_LOGGING
11872 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11873 sctp_packet_log(mout, len);
11875 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11877 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11879 (stcb->asoc.loopback_scope))) {
11880 sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11881 SCTP_STAT_INCR(sctps_sendswcrc);
11883 SCTP_STAT_INCR(sctps_sendnocrc);
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 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11891 (stcb->asoc.loopback_scope))) {
11892 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11893 mout->m_pkthdr.csum_data = 0;
11894 SCTP_STAT_INCR(sctps_sendhwcrc);
11896 SCTP_STAT_INCR(sctps_sendnocrc);
11899 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11901 /* Free the route if we got one back */
11906 SCTP_STAT_INCR(sctps_sendpackets);
11907 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11908 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11911 static struct mbuf *
11912 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11914 struct sctp_sndrcvinfo *srcv,
11916 int user_marks_eor,
11919 struct mbuf **new_tail)
11923 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11924 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11926 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11929 *sndout = m_length(m, NULL);
11930 *new_tail = m_last(m);
11936 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11943 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11945 if (sp->data == NULL) {
11946 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11949 sp->tail_mbuf = m_last(sp->data);
11955 static struct sctp_stream_queue_pending *
11956 sctp_copy_it_in(struct sctp_tcb *stcb,
11957 struct sctp_association *asoc,
11958 struct sctp_sndrcvinfo *srcv,
11960 struct sctp_nets *net,
11962 int user_marks_eor,
11967 * This routine must be very careful in its work. Protocol
11968 * processing is up and running so care must be taken to spl...()
11969 * when you need to do something that may effect the stcb/asoc. The
11970 * sb is locked however. When data is copied the protocol processing
11971 * should be enabled since this is a slower operation...
11973 struct sctp_stream_queue_pending *sp = NULL;
11977 /* Now can we send this? */
11978 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11979 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11980 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11981 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11982 /* got data while shutting down */
11983 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11984 *error = ECONNRESET;
11987 sctp_alloc_a_strmoq(stcb, sp);
11989 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11994 sp->sender_all_done = 0;
11995 sp->sinfo_flags = srcv->sinfo_flags;
11996 sp->timetolive = srcv->sinfo_timetolive;
11997 sp->ppid = srcv->sinfo_ppid;
11998 sp->context = srcv->sinfo_context;
12000 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12002 sp->stream = srcv->sinfo_stream;
12003 sp->length = min(uio->uio_resid, max_send_len);
12004 if ((sp->length == (uint32_t) uio->uio_resid) &&
12005 ((user_marks_eor == 0) ||
12006 (srcv->sinfo_flags & SCTP_EOF) ||
12007 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12008 sp->msg_is_complete = 1;
12010 sp->msg_is_complete = 0;
12012 sp->sender_all_done = 0;
12013 sp->some_taken = 0;
12014 sp->put_last_out = 0;
12015 resv_in_first = sizeof(struct sctp_data_chunk);
12016 sp->data = sp->tail_mbuf = NULL;
12017 if (sp->length == 0) {
12021 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12022 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12023 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12024 sp->holds_key_ref = 1;
12026 *error = sctp_copy_one(sp, uio, resv_in_first);
12029 sctp_free_a_strmoq(stcb, sp);
12032 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12035 sp->net = asoc->primary_destination;
12037 atomic_add_int(&sp->net->ref_count, 1);
12038 sctp_set_prsctp_policy(sp);
12046 sctp_sosend(struct socket *so,
12047 struct sockaddr *addr,
12050 struct mbuf *control,
12055 struct sctp_inpcb *inp;
12056 int error, use_rcvinfo = 0;
12057 struct sctp_sndrcvinfo srcv;
12058 struct sockaddr *addr_to_use;
12061 struct sockaddr_in sin;
12065 inp = (struct sctp_inpcb *)so->so_pcb;
12067 /* process cmsg snd/rcv info (maybe a assoc-id) */
12068 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12074 addr_to_use = addr;
12075 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
12076 if ((addr) && (addr->sa_family == AF_INET6)) {
12077 struct sockaddr_in6 *sin6;
12079 sin6 = (struct sockaddr_in6 *)addr;
12080 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12081 in6_sin6_2_sin(&sin, sin6);
12082 addr_to_use = (struct sockaddr *)&sin;
12086 error = sctp_lower_sosend(so, addr_to_use, uio, top,
12097 sctp_lower_sosend(struct socket *so,
12098 struct sockaddr *addr,
12100 struct mbuf *i_pak,
12101 struct mbuf *control,
12104 struct sctp_sndrcvinfo *srcv
12109 unsigned int sndlen = 0, max_len;
12111 struct mbuf *top = NULL;
12112 int queue_only = 0, queue_only_for_init = 0;
12113 int free_cnt_applied = 0;
12115 int now_filled = 0;
12116 unsigned int inqueue_bytes = 0;
12117 struct sctp_block_entry be;
12118 struct sctp_inpcb *inp;
12119 struct sctp_tcb *stcb = NULL;
12120 struct timeval now;
12121 struct sctp_nets *net;
12122 struct sctp_association *asoc;
12123 struct sctp_inpcb *t_inp;
12124 int user_marks_eor;
12125 int create_lock_applied = 0;
12126 int nagle_applies = 0;
12127 int some_on_control = 0;
12128 int got_all_of_the_send = 0;
12129 int hold_tcblock = 0;
12130 int non_blocking = 0;
12131 int temp_flags = 0;
12132 uint32_t local_add_more, local_soresv = 0;
12139 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12141 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12144 SCTP_RELEASE_PKT(i_pak);
12148 if ((uio == NULL) && (i_pak == NULL)) {
12149 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12152 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12153 atomic_add_int(&inp->total_sends, 1);
12155 if (uio->uio_resid < 0) {
12156 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12159 sndlen = uio->uio_resid;
12161 top = SCTP_HEADER_TO_CHAIN(i_pak);
12162 sndlen = SCTP_HEADER_LEN(i_pak);
12164 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12168 * Pre-screen address, if one is given the sin-len
12169 * must be set correctly!
12172 if ((addr->sa_family == AF_INET) &&
12173 (addr->sa_len != sizeof(struct sockaddr_in))) {
12174 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12177 } else if ((addr->sa_family == AF_INET6) &&
12178 (addr->sa_len != sizeof(struct sockaddr_in6))) {
12179 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12186 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12187 (inp->sctp_socket->so_qlimit)) {
12188 /* The listener can NOT send */
12189 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12193 if ((use_rcvinfo) && srcv) {
12194 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12195 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12196 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12200 if (srcv->sinfo_flags)
12201 SCTP_STAT_INCR(sctps_sends_with_flags);
12203 if (srcv->sinfo_flags & SCTP_SENDALL) {
12204 /* its a sendall */
12205 error = sctp_sendall(inp, uio, top, srcv);
12210 /* now we must find the assoc */
12211 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12212 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12213 SCTP_INP_RLOCK(inp);
12214 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12215 if (stcb == NULL) {
12216 SCTP_INP_RUNLOCK(inp);
12217 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12221 SCTP_TCB_LOCK(stcb);
12223 SCTP_INP_RUNLOCK(inp);
12225 /* Must locate the net structure if addr given */
12226 net = sctp_findnet(stcb, addr);
12228 /* validate port was 0 or correct */
12229 struct sockaddr_in *sin;
12231 sin = (struct sockaddr_in *)addr;
12232 if ((sin->sin_port != 0) &&
12233 (sin->sin_port != stcb->rport)) {
12237 temp_flags |= SCTP_ADDR_OVER;
12239 net = stcb->asoc.primary_destination;
12240 if (addr && (net == NULL)) {
12241 /* Could not find address, was it legal */
12242 if (addr->sa_family == AF_INET) {
12243 struct sockaddr_in *sin;
12245 sin = (struct sockaddr_in *)addr;
12246 if (sin->sin_addr.s_addr == 0) {
12247 if ((sin->sin_port == 0) ||
12248 (sin->sin_port == stcb->rport)) {
12249 net = stcb->asoc.primary_destination;
12253 struct sockaddr_in6 *sin6;
12255 sin6 = (struct sockaddr_in6 *)addr;
12256 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12257 if ((sin6->sin6_port == 0) ||
12258 (sin6->sin6_port == stcb->rport)) {
12259 net = stcb->asoc.primary_destination;
12265 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12269 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12270 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12274 * Must locate the net structure if addr
12277 net = sctp_findnet(stcb, addr);
12279 net = stcb->asoc.primary_destination;
12280 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12281 ((net == NULL) || (addr == NULL))) {
12282 struct sockaddr_in *sin;
12284 if (addr == NULL) {
12285 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12289 sin = (struct sockaddr_in *)addr;
12290 /* Validate port is 0 or correct */
12291 if ((sin->sin_port != 0) &&
12292 (sin->sin_port != stcb->rport)) {
12300 * Since we did not use findep we must
12301 * increment it, and if we don't find a tcb
12304 SCTP_INP_WLOCK(inp);
12305 SCTP_INP_INCR_REF(inp);
12306 SCTP_INP_WUNLOCK(inp);
12307 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12308 if (stcb == NULL) {
12309 SCTP_INP_WLOCK(inp);
12310 SCTP_INP_DECR_REF(inp);
12311 SCTP_INP_WUNLOCK(inp);
12316 if ((stcb == NULL) && (addr)) {
12317 /* Possible implicit send? */
12318 SCTP_ASOC_CREATE_LOCK(inp);
12319 create_lock_applied = 1;
12320 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12321 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12322 /* Should I really unlock ? */
12323 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12328 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12329 (addr->sa_family == AF_INET6)) {
12330 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12334 SCTP_INP_WLOCK(inp);
12335 SCTP_INP_INCR_REF(inp);
12336 SCTP_INP_WUNLOCK(inp);
12337 /* With the lock applied look again */
12338 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12339 if (stcb == NULL) {
12340 SCTP_INP_WLOCK(inp);
12341 SCTP_INP_DECR_REF(inp);
12342 SCTP_INP_WUNLOCK(inp);
12346 if (t_inp != inp) {
12347 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12352 if (stcb == NULL) {
12353 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12354 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12355 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12359 if (addr == NULL) {
12360 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12365 * UDP style, we must go ahead and start the INIT
12370 if ((use_rcvinfo) && (srcv) &&
12371 ((srcv->sinfo_flags & SCTP_ABORT) ||
12372 ((srcv->sinfo_flags & SCTP_EOF) &&
12375 * User asks to abort a non-existant assoc,
12376 * or EOF a non-existant assoc with no data
12378 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12382 /* get an asoc/stcb struct */
12383 vrf_id = inp->def_vrf_id;
12385 if (create_lock_applied == 0) {
12386 panic("Error, should hold create lock and I don't?");
12389 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12392 if (stcb == NULL) {
12393 /* Error is setup for us in the call */
12396 if (create_lock_applied) {
12397 SCTP_ASOC_CREATE_UNLOCK(inp);
12398 create_lock_applied = 0;
12400 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12403 * Turn on queue only flag to prevent data from
12407 asoc = &stcb->asoc;
12408 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12409 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12411 /* initialize authentication params for the assoc */
12412 sctp_initialize_auth_params(inp, stcb);
12416 * see if a init structure exists in cmsg
12419 struct sctp_initmsg initm;
12422 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12425 * we have an INIT override of the
12428 if (initm.sinit_max_attempts)
12429 asoc->max_init_times = initm.sinit_max_attempts;
12430 if (initm.sinit_num_ostreams)
12431 asoc->pre_open_streams = initm.sinit_num_ostreams;
12432 if (initm.sinit_max_instreams)
12433 asoc->max_inbound_streams = initm.sinit_max_instreams;
12434 if (initm.sinit_max_init_timeo)
12435 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12436 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12437 struct sctp_stream_out *tmp_str;
12440 /* Default is NOT correct */
12441 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12442 asoc->streamoutcnt, asoc->pre_open_streams);
12444 * What happens if this
12445 * fails? we panic ...
12448 if (hold_tcblock) {
12450 SCTP_TCB_UNLOCK(stcb);
12452 SCTP_MALLOC(tmp_str,
12453 struct sctp_stream_out *,
12454 (asoc->pre_open_streams *
12455 sizeof(struct sctp_stream_out)),
12458 SCTP_TCB_LOCK(stcb);
12460 if (tmp_str != NULL) {
12461 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12462 asoc->strmout = tmp_str;
12463 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12465 asoc->pre_open_streams = asoc->streamoutcnt;
12467 for (i = 0; i < asoc->streamoutcnt; i++) {
12469 * inbound side must be set
12470 * to 0xffff, also NOTE when
12471 * we get the INIT-ACK back
12472 * (for INIT sender) we MUST
12474 * (streamoutcnt) but first
12475 * check if we sent to any
12476 * of the upper streams that
12477 * were dropped (if some
12478 * were). Those that were
12479 * dropped must be notified
12480 * to the upper layer as
12483 asoc->strmout[i].next_sequence_sent = 0x0;
12484 TAILQ_INIT(&asoc->strmout[i].outqueue);
12485 asoc->strmout[i].stream_no = i;
12486 asoc->strmout[i].last_msg_incomplete = 0;
12487 asoc->strmout[i].next_spoke.tqe_next = 0;
12488 asoc->strmout[i].next_spoke.tqe_prev = 0;
12494 /* out with the INIT */
12495 queue_only_for_init = 1;
12497 * we may want to dig in after this call and adjust the MTU
12498 * value. It defaulted to 1500 (constant) but the ro
12499 * structure may now have an update and thus we may need to
12500 * change it BEFORE we append the message.
12502 net = stcb->asoc.primary_destination;
12503 asoc = &stcb->asoc;
12506 if ((SCTP_SO_IS_NBIO(so)
12507 || (flags & MSG_NBIO)
12511 asoc = &stcb->asoc;
12512 atomic_add_int(&stcb->total_sends, 1);
12514 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12515 if (sndlen > asoc->smallest_mtu) {
12516 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12521 /* would we block? */
12522 if (non_blocking) {
12523 if (hold_tcblock == 0) {
12524 SCTP_TCB_LOCK(stcb);
12527 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12528 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12529 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12530 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12531 if (sndlen > SCTP_SB_LIMIT_SND(so))
12534 error = EWOULDBLOCK;
12537 stcb->asoc.sb_send_resv += sndlen;
12538 SCTP_TCB_UNLOCK(stcb);
12541 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12543 local_soresv = sndlen;
12544 /* Keep the stcb from being freed under our feet */
12545 if (free_cnt_applied) {
12547 panic("refcnt already incremented");
12549 printf("refcnt:1 already incremented?\n");
12552 atomic_add_int(&stcb->asoc.refcnt, 1);
12553 free_cnt_applied = 1;
12555 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12556 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12557 error = ECONNRESET;
12560 if (create_lock_applied) {
12561 SCTP_ASOC_CREATE_UNLOCK(inp);
12562 create_lock_applied = 0;
12564 if (asoc->stream_reset_outstanding) {
12566 * Can't queue any data while stream reset is underway.
12568 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12572 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12573 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12576 if ((use_rcvinfo == 0) || (srcv == NULL)) {
12577 /* Grab the default stuff from the asoc */
12578 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12580 /* we are now done with all control */
12582 sctp_m_freem(control);
12585 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12586 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12587 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12588 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12589 if ((use_rcvinfo) &&
12590 (srcv->sinfo_flags & SCTP_ABORT)) {
12593 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12594 error = ECONNRESET;
12598 /* Ok, we will attempt a msgsnd :> */
12600 p->td_ru.ru_msgsnd++;
12603 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12604 net = stcb->asoc.primary_destination;
12608 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12612 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12614 * CMT: Added check for CMT above. net above is the primary
12615 * dest. If CMT is ON, sender should always attempt to send
12616 * with the output routine sctp_fill_outqueue() that loops
12617 * through all destination addresses. Therefore, if CMT is
12618 * ON, queue_only is NOT set to 1 here, so that
12619 * sctp_chunk_output() can be called below.
12622 } else if (asoc->ifp_had_enobuf) {
12623 SCTP_STAT_INCR(sctps_ifnomemqueued);
12624 if (net->flight_size > (net->mtu * 2))
12626 asoc->ifp_had_enobuf = 0;
12628 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12629 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12631 /* Are we aborting? */
12632 if (srcv->sinfo_flags & SCTP_ABORT) {
12634 int tot_demand, tot_out = 0, max_out;
12636 SCTP_STAT_INCR(sctps_sends_with_abort);
12637 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12638 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12639 /* It has to be up before we abort */
12640 /* how big is the user initiated abort? */
12641 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12645 if (hold_tcblock) {
12646 SCTP_TCB_UNLOCK(stcb);
12650 struct mbuf *cntm = NULL;
12652 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12656 tot_out += SCTP_BUF_LEN(cntm);
12657 cntm = SCTP_BUF_NEXT(cntm);
12660 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12662 /* Must fit in a MTU */
12664 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12665 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12667 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12671 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12674 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12678 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12679 max_out -= sizeof(struct sctp_abort_msg);
12680 if (tot_out > max_out) {
12684 struct sctp_paramhdr *ph;
12686 /* now move forward the data pointer */
12687 ph = mtod(mm, struct sctp_paramhdr *);
12688 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12689 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12691 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12693 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12696 * Here if we can't get his data we
12697 * still abort we just don't get to
12698 * send the users note :-0
12705 SCTP_BUF_NEXT(mm) = top;
12709 if (hold_tcblock == 0) {
12710 SCTP_TCB_LOCK(stcb);
12713 atomic_add_int(&stcb->asoc.refcnt, -1);
12714 free_cnt_applied = 0;
12715 /* release this lock, otherwise we hang on ourselves */
12716 sctp_abort_an_association(stcb->sctp_ep, stcb,
12717 SCTP_RESPONSE_TO_USER_REQ,
12718 mm, SCTP_SO_LOCKED);
12719 /* now relock the stcb so everything is sane */
12723 * In this case top is already chained to mm avoid double
12724 * free, since we free it below if top != NULL and driver
12725 * would free it after sending the packet out
12732 /* Calculate the maximum we can send */
12733 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12734 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12735 if (non_blocking) {
12736 /* we already checked for non-blocking above. */
12739 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12744 if (hold_tcblock) {
12745 SCTP_TCB_UNLOCK(stcb);
12748 /* Is the stream no. valid? */
12749 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12750 /* Invalid stream number */
12751 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12755 if (asoc->strmout == NULL) {
12756 /* huh? software error */
12757 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12761 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12762 if ((user_marks_eor == 0) &&
12763 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12764 /* It will NEVER fit */
12765 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12769 if ((uio == NULL) && user_marks_eor) {
12771 * We do not support eeor mode for
12772 * sending with mbuf chains (like sendfile).
12774 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12778 if (user_marks_eor) {
12779 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12782 * For non-eeor the whole message must fit in
12783 * the socket send buffer.
12785 local_add_more = sndlen;
12788 if (non_blocking) {
12789 goto skip_preblock;
12791 if (((max_len <= local_add_more) &&
12792 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12794 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12795 /* No room right now ! */
12796 SOCKBUF_LOCK(&so->so_snd);
12797 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12798 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12799 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12800 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12801 (unsigned int)SCTP_SB_LIMIT_SND(so),
12804 stcb->asoc.stream_queue_cnt,
12805 stcb->asoc.chunks_on_out_queue,
12806 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12807 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12808 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12811 stcb->block_entry = &be;
12812 error = sbwait(&so->so_snd);
12813 stcb->block_entry = NULL;
12814 if (error || so->so_error || be.error) {
12817 error = so->so_error;
12822 SOCKBUF_UNLOCK(&so->so_snd);
12825 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12826 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12827 so, asoc, stcb->asoc.total_output_queue_size);
12829 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12832 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12834 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12835 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12836 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12840 SOCKBUF_UNLOCK(&so->so_snd);
12843 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12847 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12848 * case NOTE: uio will be null when top/mbuf is passed
12851 if (srcv->sinfo_flags & SCTP_EOF) {
12852 got_all_of_the_send = 1;
12855 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12861 struct sctp_stream_queue_pending *sp;
12862 struct sctp_stream_out *strm;
12863 uint32_t sndout, initial_out;
12865 initial_out = uio->uio_resid;
12867 SCTP_TCB_SEND_LOCK(stcb);
12868 if ((asoc->stream_locked) &&
12869 (asoc->stream_locked_on != srcv->sinfo_stream)) {
12870 SCTP_TCB_SEND_UNLOCK(stcb);
12871 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12875 SCTP_TCB_SEND_UNLOCK(stcb);
12877 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12878 if (strm->last_msg_incomplete == 0) {
12880 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12881 if ((sp == NULL) || (error)) {
12884 SCTP_TCB_SEND_LOCK(stcb);
12885 if (sp->msg_is_complete) {
12886 strm->last_msg_incomplete = 0;
12887 asoc->stream_locked = 0;
12890 * Just got locked to this guy in case of an
12893 strm->last_msg_incomplete = 1;
12894 asoc->stream_locked = 1;
12895 asoc->stream_locked_on = srcv->sinfo_stream;
12896 sp->sender_all_done = 0;
12898 sctp_snd_sb_alloc(stcb, sp->length);
12899 atomic_add_int(&asoc->stream_queue_cnt, 1);
12900 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12901 sp->strseq = strm->next_sequence_sent;
12902 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12903 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12904 (uintptr_t) stcb, sp->length,
12905 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12907 strm->next_sequence_sent++;
12909 SCTP_STAT_INCR(sctps_sends_with_unord);
12911 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12912 if ((strm->next_spoke.tqe_next == NULL) &&
12913 (strm->next_spoke.tqe_prev == NULL)) {
12914 /* Not on wheel, insert */
12915 sctp_insert_on_wheel(stcb, asoc, strm, 1);
12917 SCTP_TCB_SEND_UNLOCK(stcb);
12919 SCTP_TCB_SEND_LOCK(stcb);
12920 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12921 SCTP_TCB_SEND_UNLOCK(stcb);
12923 /* ???? Huh ??? last msg is gone */
12925 panic("Warning: Last msg marked incomplete, yet nothing left?");
12927 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12928 strm->last_msg_incomplete = 0;
12934 while (uio->uio_resid > 0) {
12935 /* How much room do we have? */
12936 struct mbuf *new_tail, *mm;
12938 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12939 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12943 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12944 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12945 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
12948 if (hold_tcblock) {
12949 SCTP_TCB_UNLOCK(stcb);
12952 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12953 if ((mm == NULL) || error) {
12959 /* Update the mbuf and count */
12960 SCTP_TCB_SEND_LOCK(stcb);
12961 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12963 * we need to get out. Peer probably
12967 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12968 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12969 error = ECONNRESET;
12971 SCTP_TCB_SEND_UNLOCK(stcb);
12974 if (sp->tail_mbuf) {
12975 /* tack it to the end */
12976 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12977 sp->tail_mbuf = new_tail;
12979 /* A stolen mbuf */
12981 sp->tail_mbuf = new_tail;
12983 sctp_snd_sb_alloc(stcb, sndout);
12984 atomic_add_int(&sp->length, sndout);
12987 /* Did we reach EOR? */
12988 if ((uio->uio_resid == 0) &&
12989 ((user_marks_eor == 0) ||
12990 (srcv->sinfo_flags & SCTP_EOF) ||
12991 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12992 sp->msg_is_complete = 1;
12994 sp->msg_is_complete = 0;
12996 SCTP_TCB_SEND_UNLOCK(stcb);
12998 if (uio->uio_resid == 0) {
13003 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13005 * This is ugly but we must assure locking
13008 if (hold_tcblock == 0) {
13009 SCTP_TCB_LOCK(stcb);
13012 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13013 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13014 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13015 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13021 SCTP_TCB_UNLOCK(stcb);
13024 /* wait for space now */
13025 if (non_blocking) {
13026 /* Non-blocking io in place out */
13029 if ((net->flight_size > net->cwnd) &&
13030 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13032 } else if (asoc->ifp_had_enobuf) {
13033 SCTP_STAT_INCR(sctps_ifnomemqueued);
13034 if (net->flight_size > (net->mtu * 2)) {
13039 asoc->ifp_had_enobuf = 0;
13040 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13041 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13043 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13044 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13045 if (net->flight_size > net->cwnd) {
13047 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13052 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13053 (stcb->asoc.total_flight > 0) &&
13054 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13055 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13058 * Ok, Nagle is set on and we have data outstanding.
13059 * Don't send anything and let SACKs drive out the
13060 * data unless wen have a "full" segment to send.
13062 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13063 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13065 SCTP_STAT_INCR(sctps_naglequeued);
13068 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13069 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13070 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13072 SCTP_STAT_INCR(sctps_naglesent);
13075 /* What about the INIT, send it maybe */
13076 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13078 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13079 nagle_applies, un_sent);
13080 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13081 stcb->asoc.total_flight,
13082 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13084 if (queue_only_for_init) {
13085 if (hold_tcblock == 0) {
13086 SCTP_TCB_LOCK(stcb);
13089 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13090 /* a collision took us forward? */
13091 queue_only_for_init = 0;
13094 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13095 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13096 queue_only_for_init = 0;
13100 if ((queue_only == 0) && (nagle_applies == 0)) {
13102 * need to start chunk output
13103 * before blocking.. note that if
13104 * a lock is already applied, then
13105 * the input via the net is happening
13106 * and I don't need to start output :-D
13108 if (hold_tcblock == 0) {
13109 if (SCTP_TCB_TRYLOCK(stcb)) {
13111 sctp_chunk_output(inp,
13113 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13116 sctp_chunk_output(inp,
13118 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13120 if (hold_tcblock == 1) {
13121 SCTP_TCB_UNLOCK(stcb);
13125 SOCKBUF_LOCK(&so->so_snd);
13127 * This is a bit strange, but I think it will
13128 * work. The total_output_queue_size is locked and
13129 * protected by the TCB_LOCK, which we just released.
13130 * There is a race that can occur between releasing it
13131 * above, and me getting the socket lock, where sacks
13132 * come in but we have not put the SB_WAIT on the
13133 * so_snd buffer to get the wakeup. After the LOCK
13134 * is applied the sack_processing will also need to
13135 * LOCK the so->so_snd to do the actual sowwakeup(). So
13136 * once we have the socket buffer lock if we recheck the
13137 * size we KNOW we will get to sleep safely with the
13138 * wakeup flag in place.
13140 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13141 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13142 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13143 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13144 so, asoc, uio->uio_resid);
13147 stcb->block_entry = &be;
13148 error = sbwait(&so->so_snd);
13149 stcb->block_entry = NULL;
13151 if (error || so->so_error || be.error) {
13154 error = so->so_error;
13159 SOCKBUF_UNLOCK(&so->so_snd);
13162 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13163 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13164 so, asoc, stcb->asoc.total_output_queue_size);
13167 SOCKBUF_UNLOCK(&so->so_snd);
13168 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13172 SCTP_TCB_SEND_LOCK(stcb);
13174 if (sp->msg_is_complete == 0) {
13175 strm->last_msg_incomplete = 1;
13176 asoc->stream_locked = 1;
13177 asoc->stream_locked_on = srcv->sinfo_stream;
13179 sp->sender_all_done = 1;
13180 strm->last_msg_incomplete = 0;
13181 asoc->stream_locked = 0;
13184 SCTP_PRINTF("Huh no sp TSNH?\n");
13185 strm->last_msg_incomplete = 0;
13186 asoc->stream_locked = 0;
13188 SCTP_TCB_SEND_UNLOCK(stcb);
13189 if (uio->uio_resid == 0) {
13190 got_all_of_the_send = 1;
13193 /* We send in a 0, since we do NOT have any locks */
13194 error = sctp_msg_append(stcb, net, top, srcv, 0);
13196 if (srcv->sinfo_flags & SCTP_EOF) {
13198 * This should only happen for Panda for the mbuf
13199 * send case, which does NOT yet support EEOR mode.
13200 * Thus, we can just set this flag to do the proper
13203 got_all_of_the_send = 1;
13211 if ((srcv->sinfo_flags & SCTP_EOF) &&
13212 (got_all_of_the_send == 1) &&
13213 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13216 SCTP_STAT_INCR(sctps_sends_with_eof);
13218 if (hold_tcblock == 0) {
13219 SCTP_TCB_LOCK(stcb);
13222 cnt = sctp_is_there_unsent_data(stcb);
13223 if (TAILQ_EMPTY(&asoc->send_queue) &&
13224 TAILQ_EMPTY(&asoc->sent_queue) &&
13226 if (asoc->locked_on_sending) {
13229 /* there is nothing queued to send, so I'm done... */
13230 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13231 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13232 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13233 /* only send SHUTDOWN the first time through */
13234 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13235 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13236 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13238 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13239 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13240 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13241 asoc->primary_destination);
13242 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13243 asoc->primary_destination);
13247 * we still got (or just got) data to send, so set
13251 * XXX sockets draft says that SCTP_EOF should be
13252 * sent with no data. currently, we will allow user
13253 * data to be sent first and move to
13256 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13257 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13258 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13259 if (hold_tcblock == 0) {
13260 SCTP_TCB_LOCK(stcb);
13263 if (asoc->locked_on_sending) {
13264 /* Locked to send out the data */
13265 struct sctp_stream_queue_pending *sp;
13267 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13269 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13270 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13273 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13274 if (TAILQ_EMPTY(&asoc->send_queue) &&
13275 TAILQ_EMPTY(&asoc->sent_queue) &&
13276 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13278 if (free_cnt_applied) {
13279 atomic_add_int(&stcb->asoc.refcnt, -1);
13280 free_cnt_applied = 0;
13282 sctp_abort_an_association(stcb->sctp_ep, stcb,
13283 SCTP_RESPONSE_TO_USER_REQ,
13284 NULL, SCTP_SO_LOCKED);
13286 * now relock the stcb so everything
13293 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13294 asoc->primary_destination);
13295 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13300 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13301 some_on_control = 1;
13303 if ((net->flight_size > net->cwnd) &&
13304 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13306 } else if (asoc->ifp_had_enobuf) {
13307 SCTP_STAT_INCR(sctps_ifnomemqueued);
13308 if (net->flight_size > (net->mtu * 2)) {
13313 asoc->ifp_had_enobuf = 0;
13314 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13315 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13317 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13318 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13319 if (net->flight_size > net->cwnd) {
13321 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13326 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13327 (stcb->asoc.total_flight > 0) &&
13328 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13329 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13331 * Ok, Nagle is set on and we have data outstanding.
13332 * Don't send anything and let SACKs drive out the
13333 * data unless wen have a "full" segment to send.
13335 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13336 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13338 SCTP_STAT_INCR(sctps_naglequeued);
13341 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13342 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13343 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13345 SCTP_STAT_INCR(sctps_naglesent);
13348 if (queue_only_for_init) {
13349 if (hold_tcblock == 0) {
13350 SCTP_TCB_LOCK(stcb);
13353 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13354 /* a collision took us forward? */
13355 queue_only_for_init = 0;
13358 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13359 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13360 queue_only_for_init = 0;
13364 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13365 /* we can attempt to send too. */
13366 if (hold_tcblock == 0) {
13368 * If there is activity recv'ing sacks no need to
13371 if (SCTP_TCB_TRYLOCK(stcb)) {
13372 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13376 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13378 } else if ((queue_only == 0) &&
13379 (stcb->asoc.peers_rwnd == 0) &&
13380 (stcb->asoc.total_flight == 0)) {
13381 /* We get to have a probe outstanding */
13382 if (hold_tcblock == 0) {
13384 SCTP_TCB_LOCK(stcb);
13386 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13387 } else if (some_on_control) {
13388 int num_out, reason, frag_point;
13390 /* Here we do control only */
13391 if (hold_tcblock == 0) {
13393 SCTP_TCB_LOCK(stcb);
13395 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13396 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13397 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13399 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13400 queue_only, stcb->asoc.peers_rwnd, un_sent,
13401 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13402 stcb->asoc.total_output_queue_size, error);
13407 if (local_soresv && stcb) {
13408 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13411 if (create_lock_applied) {
13412 SCTP_ASOC_CREATE_UNLOCK(inp);
13413 create_lock_applied = 0;
13415 if ((stcb) && hold_tcblock) {
13416 SCTP_TCB_UNLOCK(stcb);
13418 if (stcb && free_cnt_applied) {
13419 atomic_add_int(&stcb->asoc.refcnt, -1);
13423 if (mtx_owned(&stcb->tcb_mtx)) {
13424 panic("Leaving with tcb mtx owned?");
13426 if (mtx_owned(&stcb->tcb_send_mtx)) {
13427 panic("Leaving with tcb send mtx owned?");
13435 sctp_m_freem(control);
13442 * generate an AUTHentication chunk, if required
13445 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13446 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13447 struct sctp_tcb *stcb, uint8_t chunk)
13449 struct mbuf *m_auth;
13450 struct sctp_auth_chunk *auth;
13453 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13457 /* sysctl disabled auth? */
13458 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13461 /* peer doesn't do auth... */
13462 if (!stcb->asoc.peer_supports_auth) {
13465 /* does the requested chunk require auth? */
13466 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13469 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13470 if (m_auth == NULL) {
13474 /* reserve some space if this will be the first mbuf */
13476 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13477 /* fill in the AUTH chunk details */
13478 auth = mtod(m_auth, struct sctp_auth_chunk *);
13479 bzero(auth, sizeof(*auth));
13480 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13481 auth->ch.chunk_flags = 0;
13482 chunk_len = sizeof(*auth) +
13483 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13484 auth->ch.chunk_length = htons(chunk_len);
13485 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13486 /* key id and hmac digest will be computed and filled in upon send */
13488 /* save the offset where the auth was inserted into the chain */
13495 *offset += SCTP_BUF_LEN(cn);
13496 cn = SCTP_BUF_NEXT(cn);
13501 /* update length and return pointer to the auth chunk */
13502 SCTP_BUF_LEN(m_auth) = chunk_len;
13503 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13504 if (auth_ret != NULL)
13512 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13514 struct nd_prefix *pfx = NULL;
13515 struct nd_pfxrouter *pfxrtr = NULL;
13516 struct sockaddr_in6 gw6;
13518 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13521 /* get prefix entry of address */
13522 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13523 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13525 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13526 &src6->sin6_addr, &pfx->ndpr_mask))
13529 /* no prefix entry in the prefix list */
13531 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13532 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13535 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13536 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13538 /* search installed gateway from prefix entry */
13539 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13540 pfxrtr->pfr_next) {
13541 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13542 gw6.sin6_family = AF_INET6;
13543 gw6.sin6_len = sizeof(struct sockaddr_in6);
13544 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13545 sizeof(struct in6_addr));
13546 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13547 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13548 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13549 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13550 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13551 ro->ro_rt->rt_gateway)) {
13552 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13556 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13563 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13565 struct sockaddr_in *sin, *mask;
13566 struct ifaddr *ifa;
13567 struct in_addr srcnetaddr, gwnetaddr;
13569 if (ro == NULL || ro->ro_rt == NULL ||
13570 sifa->address.sa.sa_family != AF_INET) {
13573 ifa = (struct ifaddr *)sifa->ifa;
13574 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13575 sin = (struct sockaddr_in *)&sifa->address.sin;
13576 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13577 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13578 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13579 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13581 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13582 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13583 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13584 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13585 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13586 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {