2 * Copyright (c) 2001-2007, 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/udp.h>
53 #include <machine/in_cksum.h>
57 #define SCTP_MAX_GAPS_INARRAY 4
59 uint8_t right_edge; /* mergable on the right edge */
60 uint8_t left_edge; /* mergable on the left edge */
63 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
66 struct sack_track sack_array[256] = {
67 {0, 0, 0, 0, /* 0x00 */
74 {1, 0, 1, 0, /* 0x01 */
81 {0, 0, 1, 0, /* 0x02 */
88 {1, 0, 1, 0, /* 0x03 */
95 {0, 0, 1, 0, /* 0x04 */
102 {1, 0, 2, 0, /* 0x05 */
109 {0, 0, 1, 0, /* 0x06 */
116 {1, 0, 1, 0, /* 0x07 */
123 {0, 0, 1, 0, /* 0x08 */
130 {1, 0, 2, 0, /* 0x09 */
137 {0, 0, 2, 0, /* 0x0a */
144 {1, 0, 2, 0, /* 0x0b */
151 {0, 0, 1, 0, /* 0x0c */
158 {1, 0, 2, 0, /* 0x0d */
165 {0, 0, 1, 0, /* 0x0e */
172 {1, 0, 1, 0, /* 0x0f */
179 {0, 0, 1, 0, /* 0x10 */
186 {1, 0, 2, 0, /* 0x11 */
193 {0, 0, 2, 0, /* 0x12 */
200 {1, 0, 2, 0, /* 0x13 */
207 {0, 0, 2, 0, /* 0x14 */
214 {1, 0, 3, 0, /* 0x15 */
221 {0, 0, 2, 0, /* 0x16 */
228 {1, 0, 2, 0, /* 0x17 */
235 {0, 0, 1, 0, /* 0x18 */
242 {1, 0, 2, 0, /* 0x19 */
249 {0, 0, 2, 0, /* 0x1a */
256 {1, 0, 2, 0, /* 0x1b */
263 {0, 0, 1, 0, /* 0x1c */
270 {1, 0, 2, 0, /* 0x1d */
277 {0, 0, 1, 0, /* 0x1e */
284 {1, 0, 1, 0, /* 0x1f */
291 {0, 0, 1, 0, /* 0x20 */
298 {1, 0, 2, 0, /* 0x21 */
305 {0, 0, 2, 0, /* 0x22 */
312 {1, 0, 2, 0, /* 0x23 */
319 {0, 0, 2, 0, /* 0x24 */
326 {1, 0, 3, 0, /* 0x25 */
333 {0, 0, 2, 0, /* 0x26 */
340 {1, 0, 2, 0, /* 0x27 */
347 {0, 0, 2, 0, /* 0x28 */
354 {1, 0, 3, 0, /* 0x29 */
361 {0, 0, 3, 0, /* 0x2a */
368 {1, 0, 3, 0, /* 0x2b */
375 {0, 0, 2, 0, /* 0x2c */
382 {1, 0, 3, 0, /* 0x2d */
389 {0, 0, 2, 0, /* 0x2e */
396 {1, 0, 2, 0, /* 0x2f */
403 {0, 0, 1, 0, /* 0x30 */
410 {1, 0, 2, 0, /* 0x31 */
417 {0, 0, 2, 0, /* 0x32 */
424 {1, 0, 2, 0, /* 0x33 */
431 {0, 0, 2, 0, /* 0x34 */
438 {1, 0, 3, 0, /* 0x35 */
445 {0, 0, 2, 0, /* 0x36 */
452 {1, 0, 2, 0, /* 0x37 */
459 {0, 0, 1, 0, /* 0x38 */
466 {1, 0, 2, 0, /* 0x39 */
473 {0, 0, 2, 0, /* 0x3a */
480 {1, 0, 2, 0, /* 0x3b */
487 {0, 0, 1, 0, /* 0x3c */
494 {1, 0, 2, 0, /* 0x3d */
501 {0, 0, 1, 0, /* 0x3e */
508 {1, 0, 1, 0, /* 0x3f */
515 {0, 0, 1, 0, /* 0x40 */
522 {1, 0, 2, 0, /* 0x41 */
529 {0, 0, 2, 0, /* 0x42 */
536 {1, 0, 2, 0, /* 0x43 */
543 {0, 0, 2, 0, /* 0x44 */
550 {1, 0, 3, 0, /* 0x45 */
557 {0, 0, 2, 0, /* 0x46 */
564 {1, 0, 2, 0, /* 0x47 */
571 {0, 0, 2, 0, /* 0x48 */
578 {1, 0, 3, 0, /* 0x49 */
585 {0, 0, 3, 0, /* 0x4a */
592 {1, 0, 3, 0, /* 0x4b */
599 {0, 0, 2, 0, /* 0x4c */
606 {1, 0, 3, 0, /* 0x4d */
613 {0, 0, 2, 0, /* 0x4e */
620 {1, 0, 2, 0, /* 0x4f */
627 {0, 0, 2, 0, /* 0x50 */
634 {1, 0, 3, 0, /* 0x51 */
641 {0, 0, 3, 0, /* 0x52 */
648 {1, 0, 3, 0, /* 0x53 */
655 {0, 0, 3, 0, /* 0x54 */
662 {1, 0, 4, 0, /* 0x55 */
669 {0, 0, 3, 0, /* 0x56 */
676 {1, 0, 3, 0, /* 0x57 */
683 {0, 0, 2, 0, /* 0x58 */
690 {1, 0, 3, 0, /* 0x59 */
697 {0, 0, 3, 0, /* 0x5a */
704 {1, 0, 3, 0, /* 0x5b */
711 {0, 0, 2, 0, /* 0x5c */
718 {1, 0, 3, 0, /* 0x5d */
725 {0, 0, 2, 0, /* 0x5e */
732 {1, 0, 2, 0, /* 0x5f */
739 {0, 0, 1, 0, /* 0x60 */
746 {1, 0, 2, 0, /* 0x61 */
753 {0, 0, 2, 0, /* 0x62 */
760 {1, 0, 2, 0, /* 0x63 */
767 {0, 0, 2, 0, /* 0x64 */
774 {1, 0, 3, 0, /* 0x65 */
781 {0, 0, 2, 0, /* 0x66 */
788 {1, 0, 2, 0, /* 0x67 */
795 {0, 0, 2, 0, /* 0x68 */
802 {1, 0, 3, 0, /* 0x69 */
809 {0, 0, 3, 0, /* 0x6a */
816 {1, 0, 3, 0, /* 0x6b */
823 {0, 0, 2, 0, /* 0x6c */
830 {1, 0, 3, 0, /* 0x6d */
837 {0, 0, 2, 0, /* 0x6e */
844 {1, 0, 2, 0, /* 0x6f */
851 {0, 0, 1, 0, /* 0x70 */
858 {1, 0, 2, 0, /* 0x71 */
865 {0, 0, 2, 0, /* 0x72 */
872 {1, 0, 2, 0, /* 0x73 */
879 {0, 0, 2, 0, /* 0x74 */
886 {1, 0, 3, 0, /* 0x75 */
893 {0, 0, 2, 0, /* 0x76 */
900 {1, 0, 2, 0, /* 0x77 */
907 {0, 0, 1, 0, /* 0x78 */
914 {1, 0, 2, 0, /* 0x79 */
921 {0, 0, 2, 0, /* 0x7a */
928 {1, 0, 2, 0, /* 0x7b */
935 {0, 0, 1, 0, /* 0x7c */
942 {1, 0, 2, 0, /* 0x7d */
949 {0, 0, 1, 0, /* 0x7e */
956 {1, 0, 1, 0, /* 0x7f */
963 {0, 1, 1, 0, /* 0x80 */
970 {1, 1, 2, 0, /* 0x81 */
977 {0, 1, 2, 0, /* 0x82 */
984 {1, 1, 2, 0, /* 0x83 */
991 {0, 1, 2, 0, /* 0x84 */
998 {1, 1, 3, 0, /* 0x85 */
1005 {0, 1, 2, 0, /* 0x86 */
1012 {1, 1, 2, 0, /* 0x87 */
1019 {0, 1, 2, 0, /* 0x88 */
1026 {1, 1, 3, 0, /* 0x89 */
1033 {0, 1, 3, 0, /* 0x8a */
1040 {1, 1, 3, 0, /* 0x8b */
1047 {0, 1, 2, 0, /* 0x8c */
1054 {1, 1, 3, 0, /* 0x8d */
1061 {0, 1, 2, 0, /* 0x8e */
1068 {1, 1, 2, 0, /* 0x8f */
1075 {0, 1, 2, 0, /* 0x90 */
1082 {1, 1, 3, 0, /* 0x91 */
1089 {0, 1, 3, 0, /* 0x92 */
1096 {1, 1, 3, 0, /* 0x93 */
1103 {0, 1, 3, 0, /* 0x94 */
1110 {1, 1, 4, 0, /* 0x95 */
1117 {0, 1, 3, 0, /* 0x96 */
1124 {1, 1, 3, 0, /* 0x97 */
1131 {0, 1, 2, 0, /* 0x98 */
1138 {1, 1, 3, 0, /* 0x99 */
1145 {0, 1, 3, 0, /* 0x9a */
1152 {1, 1, 3, 0, /* 0x9b */
1159 {0, 1, 2, 0, /* 0x9c */
1166 {1, 1, 3, 0, /* 0x9d */
1173 {0, 1, 2, 0, /* 0x9e */
1180 {1, 1, 2, 0, /* 0x9f */
1187 {0, 1, 2, 0, /* 0xa0 */
1194 {1, 1, 3, 0, /* 0xa1 */
1201 {0, 1, 3, 0, /* 0xa2 */
1208 {1, 1, 3, 0, /* 0xa3 */
1215 {0, 1, 3, 0, /* 0xa4 */
1222 {1, 1, 4, 0, /* 0xa5 */
1229 {0, 1, 3, 0, /* 0xa6 */
1236 {1, 1, 3, 0, /* 0xa7 */
1243 {0, 1, 3, 0, /* 0xa8 */
1250 {1, 1, 4, 0, /* 0xa9 */
1257 {0, 1, 4, 0, /* 0xaa */
1264 {1, 1, 4, 0, /* 0xab */
1271 {0, 1, 3, 0, /* 0xac */
1278 {1, 1, 4, 0, /* 0xad */
1285 {0, 1, 3, 0, /* 0xae */
1292 {1, 1, 3, 0, /* 0xaf */
1299 {0, 1, 2, 0, /* 0xb0 */
1306 {1, 1, 3, 0, /* 0xb1 */
1313 {0, 1, 3, 0, /* 0xb2 */
1320 {1, 1, 3, 0, /* 0xb3 */
1327 {0, 1, 3, 0, /* 0xb4 */
1334 {1, 1, 4, 0, /* 0xb5 */
1341 {0, 1, 3, 0, /* 0xb6 */
1348 {1, 1, 3, 0, /* 0xb7 */
1355 {0, 1, 2, 0, /* 0xb8 */
1362 {1, 1, 3, 0, /* 0xb9 */
1369 {0, 1, 3, 0, /* 0xba */
1376 {1, 1, 3, 0, /* 0xbb */
1383 {0, 1, 2, 0, /* 0xbc */
1390 {1, 1, 3, 0, /* 0xbd */
1397 {0, 1, 2, 0, /* 0xbe */
1404 {1, 1, 2, 0, /* 0xbf */
1411 {0, 1, 1, 0, /* 0xc0 */
1418 {1, 1, 2, 0, /* 0xc1 */
1425 {0, 1, 2, 0, /* 0xc2 */
1432 {1, 1, 2, 0, /* 0xc3 */
1439 {0, 1, 2, 0, /* 0xc4 */
1446 {1, 1, 3, 0, /* 0xc5 */
1453 {0, 1, 2, 0, /* 0xc6 */
1460 {1, 1, 2, 0, /* 0xc7 */
1467 {0, 1, 2, 0, /* 0xc8 */
1474 {1, 1, 3, 0, /* 0xc9 */
1481 {0, 1, 3, 0, /* 0xca */
1488 {1, 1, 3, 0, /* 0xcb */
1495 {0, 1, 2, 0, /* 0xcc */
1502 {1, 1, 3, 0, /* 0xcd */
1509 {0, 1, 2, 0, /* 0xce */
1516 {1, 1, 2, 0, /* 0xcf */
1523 {0, 1, 2, 0, /* 0xd0 */
1530 {1, 1, 3, 0, /* 0xd1 */
1537 {0, 1, 3, 0, /* 0xd2 */
1544 {1, 1, 3, 0, /* 0xd3 */
1551 {0, 1, 3, 0, /* 0xd4 */
1558 {1, 1, 4, 0, /* 0xd5 */
1565 {0, 1, 3, 0, /* 0xd6 */
1572 {1, 1, 3, 0, /* 0xd7 */
1579 {0, 1, 2, 0, /* 0xd8 */
1586 {1, 1, 3, 0, /* 0xd9 */
1593 {0, 1, 3, 0, /* 0xda */
1600 {1, 1, 3, 0, /* 0xdb */
1607 {0, 1, 2, 0, /* 0xdc */
1614 {1, 1, 3, 0, /* 0xdd */
1621 {0, 1, 2, 0, /* 0xde */
1628 {1, 1, 2, 0, /* 0xdf */
1635 {0, 1, 1, 0, /* 0xe0 */
1642 {1, 1, 2, 0, /* 0xe1 */
1649 {0, 1, 2, 0, /* 0xe2 */
1656 {1, 1, 2, 0, /* 0xe3 */
1663 {0, 1, 2, 0, /* 0xe4 */
1670 {1, 1, 3, 0, /* 0xe5 */
1677 {0, 1, 2, 0, /* 0xe6 */
1684 {1, 1, 2, 0, /* 0xe7 */
1691 {0, 1, 2, 0, /* 0xe8 */
1698 {1, 1, 3, 0, /* 0xe9 */
1705 {0, 1, 3, 0, /* 0xea */
1712 {1, 1, 3, 0, /* 0xeb */
1719 {0, 1, 2, 0, /* 0xec */
1726 {1, 1, 3, 0, /* 0xed */
1733 {0, 1, 2, 0, /* 0xee */
1740 {1, 1, 2, 0, /* 0xef */
1747 {0, 1, 1, 0, /* 0xf0 */
1754 {1, 1, 2, 0, /* 0xf1 */
1761 {0, 1, 2, 0, /* 0xf2 */
1768 {1, 1, 2, 0, /* 0xf3 */
1775 {0, 1, 2, 0, /* 0xf4 */
1782 {1, 1, 3, 0, /* 0xf5 */
1789 {0, 1, 2, 0, /* 0xf6 */
1796 {1, 1, 2, 0, /* 0xf7 */
1803 {0, 1, 1, 0, /* 0xf8 */
1810 {1, 1, 2, 0, /* 0xf9 */
1817 {0, 1, 2, 0, /* 0xfa */
1824 {1, 1, 2, 0, /* 0xfb */
1831 {0, 1, 1, 0, /* 0xfc */
1838 {1, 1, 2, 0, /* 0xfd */
1845 {0, 1, 1, 0, /* 0xfe */
1852 {1, 1, 1, 0, /* 0xff */
1863 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1864 int ipv4_addr_legal,
1865 int ipv6_addr_legal,
1867 int ipv4_local_scope,
1872 if ((loopback_scope == 0) &&
1873 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 * skip loopback if not in scope *
1879 switch (ifa->address.sa.sa_family) {
1881 if (ipv4_addr_legal) {
1882 struct sockaddr_in *sin;
1884 sin = (struct sockaddr_in *)&ifa->address.sin;
1885 if (sin->sin_addr.s_addr == 0) {
1886 /* not in scope , unspecified */
1889 if ((ipv4_local_scope == 0) &&
1890 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1891 /* private address not in scope */
1900 if (ipv6_addr_legal) {
1901 struct sockaddr_in6 *sin6;
1904 * Must update the flags, bummer, which means any
1905 * IFA locks must now be applied HERE <->
1908 sctp_gather_internal_ifa_flags(ifa);
1910 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1913 /* ok to use deprecated addresses? */
1914 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1915 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1916 /* skip unspecifed addresses */
1919 if ( /* (local_scope == 0) && */
1920 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1923 if ((site_scope == 0) &&
1924 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1938 static struct mbuf *
1939 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941 struct sctp_paramhdr *parmh;
1945 if (ifa->address.sa.sa_family == AF_INET) {
1946 len = sizeof(struct sctp_ipv4addr_param);
1947 } else if (ifa->address.sa.sa_family == AF_INET6) {
1948 len = sizeof(struct sctp_ipv6addr_param);
1953 if (M_TRAILINGSPACE(m) >= len) {
1954 /* easy side we just drop it on the end */
1955 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1958 /* Need more space */
1960 while (SCTP_BUF_NEXT(mret) != NULL) {
1961 mret = SCTP_BUF_NEXT(mret);
1963 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1964 if (SCTP_BUF_NEXT(mret) == NULL) {
1965 /* We are hosed, can't add more addresses */
1968 mret = SCTP_BUF_NEXT(mret);
1969 parmh = mtod(mret, struct sctp_paramhdr *);
1971 /* now add the parameter */
1972 switch (ifa->address.sa.sa_family) {
1975 struct sctp_ipv4addr_param *ipv4p;
1976 struct sockaddr_in *sin;
1978 sin = (struct sockaddr_in *)&ifa->address.sin;
1979 ipv4p = (struct sctp_ipv4addr_param *)parmh;
1980 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1981 parmh->param_length = htons(len);
1982 ipv4p->addr = sin->sin_addr.s_addr;
1983 SCTP_BUF_LEN(mret) += len;
1989 struct sctp_ipv6addr_param *ipv6p;
1990 struct sockaddr_in6 *sin6;
1992 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1993 ipv6p = (struct sctp_ipv6addr_param *)parmh;
1994 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1995 parmh->param_length = htons(len);
1996 memcpy(ipv6p->addr, &sin6->sin6_addr,
1997 sizeof(ipv6p->addr));
1998 /* clear embedded scope in the address */
1999 in6_clearscope((struct in6_addr *)ipv6p->addr);
2000 SCTP_BUF_LEN(mret) += len;
2012 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2013 struct mbuf *m_at, int cnt_inits_to)
2015 struct sctp_vrf *vrf = NULL;
2016 int cnt, limit_out = 0, total_count;
2019 vrf_id = inp->def_vrf_id;
2020 SCTP_IPI_ADDR_RLOCK();
2021 vrf = sctp_find_vrf(vrf_id);
2023 SCTP_IPI_ADDR_RUNLOCK();
2026 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2027 struct sctp_ifa *sctp_ifap;
2028 struct sctp_ifn *sctp_ifnp;
2031 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 cnt = SCTP_ADDRESS_LIMIT;
2036 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2037 if ((scope->loopback_scope == 0) &&
2038 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 * Skip loopback devices if loopback_scope
2045 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2046 if (sctp_is_address_in_scope(sctp_ifap,
2047 scope->ipv4_addr_legal,
2048 scope->ipv6_addr_legal,
2049 scope->loopback_scope,
2050 scope->ipv4_local_scope,
2052 scope->site_scope, 1) == 0) {
2056 if (cnt > SCTP_ADDRESS_LIMIT) {
2060 if (cnt > SCTP_ADDRESS_LIMIT) {
2067 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 if ((scope->loopback_scope == 0) &&
2070 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 * Skip loopback devices if
2073 * loopback_scope not set
2077 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2078 if (sctp_is_address_in_scope(sctp_ifap,
2079 scope->ipv4_addr_legal,
2080 scope->ipv6_addr_legal,
2081 scope->loopback_scope,
2082 scope->ipv4_local_scope,
2084 scope->site_scope, 0) == 0) {
2087 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2098 if (total_count > SCTP_ADDRESS_LIMIT) {
2099 /* No more addresses */
2107 struct sctp_laddr *laddr;
2110 /* First, how many ? */
2111 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2112 if (laddr->ifa == NULL) {
2115 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 * Address being deleted by the system, dont
2121 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 * Address being deleted on this ep don't
2128 if (sctp_is_address_in_scope(laddr->ifa,
2129 scope->ipv4_addr_legal,
2130 scope->ipv6_addr_legal,
2131 scope->loopback_scope,
2132 scope->ipv4_local_scope,
2134 scope->site_scope, 1) == 0) {
2139 if (cnt > SCTP_ADDRESS_LIMIT) {
2143 * To get through a NAT we only list addresses if we have
2144 * more than one. That way if you just bind a single address
2145 * we let the source of the init dictate our address.
2148 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 if (laddr->ifa == NULL) {
2153 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2156 if (sctp_is_address_in_scope(laddr->ifa,
2157 scope->ipv4_addr_legal,
2158 scope->ipv6_addr_legal,
2159 scope->loopback_scope,
2160 scope->ipv4_local_scope,
2162 scope->site_scope, 0) == 0) {
2165 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 if (cnt >= SCTP_ADDRESS_LIMIT) {
2173 SCTP_IPI_ADDR_RUNLOCK();
2177 static struct sctp_ifa *
2178 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2179 uint8_t dest_is_loop,
2180 uint8_t dest_is_priv,
2183 uint8_t dest_is_global = 0;
2185 /* dest_is_priv is true if destination is a private address */
2186 /* dest_is_loop is true if destination is a loopback addresses */
2189 * Here we determine if its a preferred address. A preferred address
2190 * means it is the same scope or higher scope then the destination.
2191 * L = loopback, P = private, G = global
2192 * ----------------------------------------- src | dest | result
2193 * ---------------------------------------- L | L | yes
2194 * ----------------------------------------- P | L |
2195 * yes-v4 no-v6 ----------------------------------------- G |
2196 * L | yes-v4 no-v6 ----------------------------------------- L
2197 * | P | no ----------------------------------------- P |
2198 * P | yes ----------------------------------------- G |
2199 * P | no ----------------------------------------- L | G
2200 * | no ----------------------------------------- P | G |
2201 * no ----------------------------------------- G | G |
2202 * yes -----------------------------------------
2205 if (ifa->address.sa.sa_family != fam) {
2206 /* forget mis-matched family */
2209 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2212 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2213 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2214 /* Ok the address may be ok */
2215 if (fam == AF_INET6) {
2216 /* ok to use deprecated addresses? no lets not! */
2217 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2218 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2221 if (ifa->src_is_priv && !ifa->src_is_loop) {
2223 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2227 if (ifa->src_is_glob) {
2229 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2235 * Now that we know what is what, implement or table this could in
2236 * theory be done slicker (it used to be), but this is
2237 * straightforward and easier to validate :-)
2239 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2240 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2241 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2242 dest_is_loop, dest_is_priv, dest_is_global);
2244 if ((ifa->src_is_loop) && (dest_is_priv)) {
2245 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2248 if ((ifa->src_is_glob) && (dest_is_priv)) {
2249 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2252 if ((ifa->src_is_loop) && (dest_is_global)) {
2253 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2256 if ((ifa->src_is_priv) && (dest_is_global)) {
2257 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2260 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2261 /* its a preferred address */
2265 static struct sctp_ifa *
2266 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2267 uint8_t dest_is_loop,
2268 uint8_t dest_is_priv,
2271 uint8_t dest_is_global = 0;
2275 * Here we determine if its a acceptable address. A acceptable
2276 * address means it is the same scope or higher scope but we can
2277 * allow for NAT which means its ok to have a global dest and a
2280 * L = loopback, P = private, G = global
2281 * ----------------------------------------- src | dest | result
2282 * ----------------------------------------- L | L | yes
2283 * ----------------------------------------- P | L |
2284 * yes-v4 no-v6 ----------------------------------------- G |
2285 * L | yes ----------------------------------------- L |
2286 * P | no ----------------------------------------- P | P
2287 * | yes ----------------------------------------- G | P
2288 * | yes - May not work -----------------------------------------
2289 * L | G | no ----------------------------------------- P
2290 * | G | yes - May not work
2291 * ----------------------------------------- G | G | yes
2292 * -----------------------------------------
2295 if (ifa->address.sa.sa_family != fam) {
2296 /* forget non matching family */
2299 /* Ok the address may be ok */
2300 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2303 if (fam == AF_INET6) {
2304 /* ok to use deprecated addresses? */
2305 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2308 if (ifa->src_is_priv) {
2309 /* Special case, linklocal to loop */
2315 * Now that we know what is what, implement our table. This could in
2316 * theory be done slicker (it used to be), but this is
2317 * straightforward and easier to validate :-)
2319 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2322 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2325 /* its an acceptable address */
2330 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2332 struct sctp_laddr *laddr;
2335 /* There are no restrictions, no TCB :-) */
2338 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2339 if (laddr->ifa == NULL) {
2340 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2344 if (laddr->ifa == ifa) {
2345 /* Yes it is on the list */
2354 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2356 struct sctp_laddr *laddr;
2360 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2361 if (laddr->ifa == NULL) {
2362 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2366 if ((laddr->ifa == ifa) && laddr->action == 0)
2375 static struct sctp_ifa *
2376 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2379 int non_asoc_addr_ok,
2380 uint8_t dest_is_priv,
2381 uint8_t dest_is_loop,
2384 struct sctp_laddr *laddr, *starting_point;
2387 struct sctp_ifn *sctp_ifn;
2388 struct sctp_ifa *sctp_ifa, *sifa;
2389 struct sctp_vrf *vrf;
2392 vrf = sctp_find_vrf(vrf_id);
2396 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2397 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2398 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2400 * first question, is the ifn we will emit on in our list, if so, we
2401 * want such an address. Note that we first looked for a preferred
2405 /* is a preferred one on the interface we route out? */
2406 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2407 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2408 (non_asoc_addr_ok == 0))
2410 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2415 if (sctp_is_addr_in_ep(inp, sifa)) {
2416 atomic_add_int(&sifa->refcount, 1);
2422 * ok, now we now need to find one on the list of the addresses. We
2423 * can't get one on the emitting interface so let's find first a
2424 * preferred one. If not that an acceptable one otherwise... we
2427 starting_point = inp->next_addr_touse;
2429 if (inp->next_addr_touse == NULL) {
2430 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2433 for (laddr = inp->next_addr_touse; laddr;
2434 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2435 if (laddr->ifa == NULL) {
2436 /* address has been removed */
2439 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2440 /* address is being deleted */
2443 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2447 atomic_add_int(&sifa->refcount, 1);
2450 if (resettotop == 0) {
2451 inp->next_addr_touse = NULL;
2454 inp->next_addr_touse = starting_point;
2457 if (inp->next_addr_touse == NULL) {
2458 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2461 /* ok, what about an acceptable address in the inp */
2462 for (laddr = inp->next_addr_touse; laddr;
2463 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2464 if (laddr->ifa == NULL) {
2465 /* address has been removed */
2468 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2469 /* address is being deleted */
2472 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2476 atomic_add_int(&sifa->refcount, 1);
2479 if (resettotop == 0) {
2480 inp->next_addr_touse = NULL;
2481 goto once_again_too;
2484 * no address bound can be a source for the destination we are in
2492 static struct sctp_ifa *
2493 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2494 struct sctp_tcb *stcb,
2495 struct sctp_nets *net,
2498 uint8_t dest_is_priv,
2499 uint8_t dest_is_loop,
2500 int non_asoc_addr_ok,
2503 struct sctp_laddr *laddr, *starting_point;
2505 struct sctp_ifn *sctp_ifn;
2506 struct sctp_ifa *sctp_ifa, *sifa;
2507 uint8_t start_at_beginning = 0;
2508 struct sctp_vrf *vrf;
2512 * first question, is the ifn we will emit on in our list, if so, we
2515 vrf = sctp_find_vrf(vrf_id);
2519 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2520 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2521 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2524 * first question, is the ifn we will emit on in our list? If so,
2525 * we want that one. First we look for a preferred. Second, we go
2526 * for an acceptable.
2529 /* first try for a preferred address on the ep */
2530 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2531 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2533 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2534 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2537 if (((non_asoc_addr_ok == 0) &&
2538 (sctp_is_addr_restricted(stcb, sifa))) ||
2539 (non_asoc_addr_ok &&
2540 (sctp_is_addr_restricted(stcb, sifa)) &&
2541 (!sctp_is_addr_pending(stcb, sifa)))) {
2542 /* on the no-no list */
2545 atomic_add_int(&sifa->refcount, 1);
2549 /* next try for an acceptable address on the ep */
2550 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2551 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2553 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2554 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2557 if (((non_asoc_addr_ok == 0) &&
2558 (sctp_is_addr_restricted(stcb, sifa))) ||
2559 (non_asoc_addr_ok &&
2560 (sctp_is_addr_restricted(stcb, sifa)) &&
2561 (!sctp_is_addr_pending(stcb, sifa)))) {
2562 /* on the no-no list */
2565 atomic_add_int(&sifa->refcount, 1);
2572 * if we can't find one like that then we must look at all addresses
2573 * bound to pick one at first preferable then secondly acceptable.
2575 starting_point = stcb->asoc.last_used_address;
2577 if (stcb->asoc.last_used_address == NULL) {
2578 start_at_beginning = 1;
2579 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2581 /* search beginning with the last used address */
2582 for (laddr = stcb->asoc.last_used_address; laddr;
2583 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2584 if (laddr->ifa == NULL) {
2585 /* address has been removed */
2588 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2589 /* address is being deleted */
2592 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2595 if (((non_asoc_addr_ok == 0) &&
2596 (sctp_is_addr_restricted(stcb, sifa))) ||
2597 (non_asoc_addr_ok &&
2598 (sctp_is_addr_restricted(stcb, sifa)) &&
2599 (!sctp_is_addr_pending(stcb, sifa)))) {
2600 /* on the no-no list */
2603 stcb->asoc.last_used_address = laddr;
2604 atomic_add_int(&sifa->refcount, 1);
2607 if (start_at_beginning == 0) {
2608 stcb->asoc.last_used_address = NULL;
2609 goto sctp_from_the_top;
2611 /* now try for any higher scope than the destination */
2612 stcb->asoc.last_used_address = starting_point;
2613 start_at_beginning = 0;
2615 if (stcb->asoc.last_used_address == NULL) {
2616 start_at_beginning = 1;
2617 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2619 /* search beginning with the last used address */
2620 for (laddr = stcb->asoc.last_used_address; laddr;
2621 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2622 if (laddr->ifa == NULL) {
2623 /* address has been removed */
2626 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2627 /* address is being deleted */
2630 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2634 if (((non_asoc_addr_ok == 0) &&
2635 (sctp_is_addr_restricted(stcb, sifa))) ||
2636 (non_asoc_addr_ok &&
2637 (sctp_is_addr_restricted(stcb, sifa)) &&
2638 (!sctp_is_addr_pending(stcb, sifa)))) {
2639 /* on the no-no list */
2642 stcb->asoc.last_used_address = laddr;
2643 atomic_add_int(&sifa->refcount, 1);
2646 if (start_at_beginning == 0) {
2647 stcb->asoc.last_used_address = NULL;
2648 goto sctp_from_the_top2;
2653 static struct sctp_ifa *
2654 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2655 struct sctp_tcb *stcb,
2656 int non_asoc_addr_ok,
2657 uint8_t dest_is_loop,
2658 uint8_t dest_is_priv,
2664 struct sctp_ifa *ifa, *sifa;
2665 int num_eligible_addr = 0;
2668 struct sockaddr_in6 sin6, lsa6;
2670 if (fam == AF_INET6) {
2671 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2672 (void)sa6_recoverscope(&sin6);
2675 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2676 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2677 (non_asoc_addr_ok == 0))
2679 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2684 if (fam == AF_INET6 &&
2686 sifa->src_is_loop && sifa->src_is_priv) {
2688 * don't allow fe80::1 to be a src on loop ::1, we
2689 * don't list it to the peer so we will get an
2694 if (fam == AF_INET6 &&
2695 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2696 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2698 * link-local <-> link-local must belong to the same
2701 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2702 (void)sa6_recoverscope(&lsa6);
2703 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2710 * Check if the IPv6 address matches to next-hop. In the
2711 * mobile case, old IPv6 address may be not deleted from the
2712 * interface. Then, the interface has previous and new
2713 * addresses. We should use one corresponding to the
2714 * next-hop. (by micchie)
2717 if (stcb && fam == AF_INET6 &&
2718 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2719 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2725 /* Avoid topologically incorrect IPv4 address */
2726 if (stcb && fam == AF_INET &&
2727 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2728 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2733 if (((non_asoc_addr_ok == 0) &&
2734 (sctp_is_addr_restricted(stcb, sifa))) ||
2735 (non_asoc_addr_ok &&
2736 (sctp_is_addr_restricted(stcb, sifa)) &&
2737 (!sctp_is_addr_pending(stcb, sifa)))) {
2739 * It is restricted for some reason..
2740 * probably not yet added.
2745 if (num_eligible_addr >= addr_wanted) {
2748 num_eligible_addr++;
2755 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2756 struct sctp_tcb *stcb,
2757 int non_asoc_addr_ok,
2758 uint8_t dest_is_loop,
2759 uint8_t dest_is_priv,
2762 struct sctp_ifa *ifa, *sifa;
2763 int num_eligible_addr = 0;
2765 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2766 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2767 (non_asoc_addr_ok == 0)) {
2770 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2776 if (((non_asoc_addr_ok == 0) &&
2777 (sctp_is_addr_restricted(stcb, sifa))) ||
2778 (non_asoc_addr_ok &&
2779 (sctp_is_addr_restricted(stcb, sifa)) &&
2780 (!sctp_is_addr_pending(stcb, sifa)))) {
2782 * It is restricted for some reason..
2783 * probably not yet added.
2788 num_eligible_addr++;
2790 return (num_eligible_addr);
2793 static struct sctp_ifa *
2794 sctp_choose_boundall(struct sctp_inpcb *inp,
2795 struct sctp_tcb *stcb,
2796 struct sctp_nets *net,
2799 uint8_t dest_is_priv,
2800 uint8_t dest_is_loop,
2801 int non_asoc_addr_ok,
2804 int cur_addr_num = 0, num_preferred = 0;
2806 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2807 struct sctp_ifa *sctp_ifa, *sifa;
2809 struct sctp_vrf *vrf;
2812 * For boundall we can use any address in the association.
2813 * If non_asoc_addr_ok is set we can use any address (at least in
2814 * theory). So we look for preferred addresses first. If we find one,
2815 * we use it. Otherwise we next try to get an address on the
2816 * interface, which we should be able to do (unless non_asoc_addr_ok
2817 * is false and we are routed out that way). In these cases where we
2818 * can't use the address of the interface we go through all the
2819 * ifn's looking for an address we can use and fill that in. Punting
2820 * means we send back address 0, which will probably cause problems
2821 * actually since then IP will fill in the address of the route ifn,
2822 * which means we probably already rejected it.. i.e. here comes an
2825 vrf = sctp_find_vrf(vrf_id);
2829 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2830 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2831 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2832 if (sctp_ifn == NULL) {
2833 /* ?? We don't have this guy ?? */
2834 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2835 goto bound_all_plan_b;
2837 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2838 ifn_index, sctp_ifn->ifn_name);
2841 cur_addr_num = net->indx_of_eligible_next_to_use;
2843 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2848 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2849 num_preferred, sctp_ifn->ifn_name);
2850 if (num_preferred == 0) {
2852 * no eligible addresses, we must use some other interface
2853 * address if we can find one.
2855 goto bound_all_plan_b;
2858 * Ok we have num_eligible_addr set with how many we can use, this
2859 * may vary from call to call due to addresses being deprecated
2862 if (cur_addr_num >= num_preferred) {
2866 * select the nth address from the list (where cur_addr_num is the
2867 * nth) and 0 is the first one, 1 is the second one etc...
2869 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2871 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2872 dest_is_priv, cur_addr_num, fam, ro);
2874 /* if sctp_ifa is NULL something changed??, fall to plan b. */
2876 atomic_add_int(&sctp_ifa->refcount, 1);
2878 /* save off where the next one we will want */
2879 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2884 * plan_b: Look at all interfaces and find a preferred address. If
2885 * no preferred fall through to plan_c.
2888 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2889 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2890 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2891 sctp_ifn->ifn_name);
2892 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2893 /* wrong base scope */
2894 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2897 if ((sctp_ifn == looked_at) && looked_at) {
2898 /* already looked at this guy */
2899 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2902 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2903 dest_is_loop, dest_is_priv, fam);
2904 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2905 "Found ifn:%p %d preferred source addresses\n",
2906 ifn, num_preferred);
2907 if (num_preferred == 0) {
2908 /* None on this interface. */
2909 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2912 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2913 "num preferred:%d on interface:%p cur_addr_num:%d\n",
2914 num_preferred, sctp_ifn, cur_addr_num);
2917 * Ok we have num_eligible_addr set with how many we can
2918 * use, this may vary from call to call due to addresses
2919 * being deprecated etc..
2921 if (cur_addr_num >= num_preferred) {
2924 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2925 dest_is_priv, cur_addr_num, fam, ro);
2929 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2930 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2932 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2933 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2934 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2935 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2937 atomic_add_int(&sifa->refcount, 1);
2942 /* plan_c: do we have an acceptable address on the emit interface */
2943 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2944 if (emit_ifn == NULL) {
2947 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2948 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2949 (non_asoc_addr_ok == 0))
2951 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2956 if (((non_asoc_addr_ok == 0) &&
2957 (sctp_is_addr_restricted(stcb, sifa))) ||
2958 (non_asoc_addr_ok &&
2959 (sctp_is_addr_restricted(stcb, sifa)) &&
2960 (!sctp_is_addr_pending(stcb, sifa)))) {
2962 * It is restricted for some reason..
2963 * probably not yet added.
2968 atomic_add_int(&sifa->refcount, 1);
2973 * plan_d: We are in trouble. No preferred address on the emit
2974 * interface. And not even a preferred address on all interfaces. Go
2975 * out and see if we can find an acceptable address somewhere
2976 * amongst all interfaces.
2978 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2979 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2980 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2981 /* wrong base scope */
2984 if ((sctp_ifn == looked_at) && looked_at)
2985 /* already looked at this guy */
2988 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2989 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2990 (non_asoc_addr_ok == 0))
2992 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2998 if (((non_asoc_addr_ok == 0) &&
2999 (sctp_is_addr_restricted(stcb, sifa))) ||
3000 (non_asoc_addr_ok &&
3001 (sctp_is_addr_restricted(stcb, sifa)) &&
3002 (!sctp_is_addr_pending(stcb, sifa)))) {
3004 * It is restricted for some
3005 * reason.. probably not yet added.
3010 atomic_add_int(&sifa->refcount, 1);
3015 * Ok we can find NO address to source from that is not on our
3016 * restricted list and non_asoc_address is NOT ok, or it is on our
3017 * restricted list. We can't source to it :-(
3024 /* tcb may be NULL */
3026 sctp_source_address_selection(struct sctp_inpcb *inp,
3027 struct sctp_tcb *stcb,
3029 struct sctp_nets *net,
3030 int non_asoc_addr_ok, uint32_t vrf_id)
3032 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3035 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3038 struct sctp_ifa *answer;
3039 uint8_t dest_is_priv, dest_is_loop;
3043 * Rules: - Find the route if needed, cache if I can. - Look at
3044 * interface address in route, Is it in the bound list. If so we
3045 * have the best source. - If not we must rotate amongst the
3050 * Do we need to pay attention to scope. We can have a private address
3051 * or a global address we are sourcing or sending to. So if we draw
3053 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3055 *------------------------------------------
3056 * source * dest * result
3057 * -----------------------------------------
3058 * <a> Private * Global * NAT
3059 * -----------------------------------------
3060 * <b> Private * Private * No problem
3061 * -----------------------------------------
3062 * <c> Global * Private * Huh, How will this work?
3063 * -----------------------------------------
3064 * <d> Global * Global * No Problem
3065 *------------------------------------------
3066 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3068 *------------------------------------------
3069 * source * dest * result
3070 * -----------------------------------------
3071 * <a> Linklocal * Global *
3072 * -----------------------------------------
3073 * <b> Linklocal * Linklocal * No problem
3074 * -----------------------------------------
3075 * <c> Global * Linklocal * Huh, How will this work?
3076 * -----------------------------------------
3077 * <d> Global * Global * No Problem
3078 *------------------------------------------
3079 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3081 * And then we add to that what happens if there are multiple addresses
3082 * assigned to an interface. Remember the ifa on a ifn is a linked
3083 * list of addresses. So one interface can have more than one IP
3084 * address. What happens if we have both a private and a global
3085 * address? Do we then use context of destination to sort out which
3086 * one is best? And what about NAT's sending P->G may get you a NAT
3087 * translation, or should you select the G thats on the interface in
3092 * - count the number of addresses on the interface.
3093 * - if it is one, no problem except case <c>.
3094 * For <a> we will assume a NAT out there.
3095 * - if there are more than one, then we need to worry about scope P
3096 * or G. We should prefer G -> G and P -> P if possible.
3097 * Then as a secondary fall back to mixed types G->P being a last
3099 * - The above all works for bound all, but bound specific we need to
3100 * use the same concept but instead only consider the bound
3101 * addresses. If the bound set is NOT assigned to the interface then
3102 * we must use rotation amongst the bound addresses..
3104 if (ro->ro_rt == NULL) {
3106 * Need a route to cache.
3108 SCTP_RTALLOC(ro, vrf_id);
3110 if (ro->ro_rt == NULL) {
3113 fam = to->sin_family;
3114 dest_is_priv = dest_is_loop = 0;
3115 /* Setup our scopes for the destination */
3118 /* Scope based on outbound address */
3119 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3122 /* mark it as local */
3123 net->addr_is_local = 1;
3125 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3131 /* Scope based on outbound address */
3132 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3133 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3135 * If the address is a loopback address, which
3136 * consists of "::1" OR "fe80::1%lo0", we are
3137 * loopback scope. But we don't use dest_is_priv
3138 * (link local addresses).
3142 /* mark it as local */
3143 net->addr_is_local = 1;
3145 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3151 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3152 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3153 SCTP_IPI_ADDR_RLOCK();
3154 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3158 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3159 dest_is_priv, dest_is_loop,
3160 non_asoc_addr_ok, fam);
3161 SCTP_IPI_ADDR_RUNLOCK();
3168 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3169 vrf_id, dest_is_priv,
3171 non_asoc_addr_ok, fam);
3173 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3178 SCTP_IPI_ADDR_RUNLOCK();
3183 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3188 tlen = SCTP_BUF_LEN(control);
3191 * Independent of how many mbufs, find the c_type inside the control
3192 * structure and copy out the data.
3195 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3196 /* not enough room for one more we are done. */
3199 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3200 if (((int)cmh.cmsg_len + at) > tlen) {
3202 * this is real messed up since there is not enough
3203 * data here to cover the cmsg header. We are done.
3207 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3208 (c_type == cmh.cmsg_type)) {
3209 /* found the one we want, copy it out */
3210 at += CMSG_ALIGN(sizeof(struct cmsghdr));
3211 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3213 * space of cmsg_len after header not big
3218 m_copydata(control, at, cpsize, data);
3221 at += CMSG_ALIGN(cmh.cmsg_len);
3222 if (cmh.cmsg_len == 0) {
3231 static struct mbuf *
3232 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3233 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3235 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3236 struct sctp_state_cookie *stc;
3237 struct sctp_paramhdr *ph;
3243 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3244 sizeof(struct sctp_paramhdr)), 0,
3245 M_DONTWAIT, 1, MT_DATA);
3249 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3250 if (copy_init == NULL) {
3254 #ifdef SCTP_MBUF_LOGGING
3255 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3260 if (SCTP_BUF_IS_EXTENDED(mat)) {
3261 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3263 mat = SCTP_BUF_NEXT(mat);
3267 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3269 if (copy_initack == NULL) {
3271 sctp_m_freem(copy_init);
3274 #ifdef SCTP_MBUF_LOGGING
3275 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3280 if (SCTP_BUF_IS_EXTENDED(mat)) {
3281 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3283 mat = SCTP_BUF_NEXT(mat);
3287 /* easy side we just drop it on the end */
3288 ph = mtod(mret, struct sctp_paramhdr *);
3289 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3290 sizeof(struct sctp_paramhdr);
3291 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3292 sizeof(struct sctp_paramhdr));
3293 ph->param_type = htons(SCTP_STATE_COOKIE);
3294 ph->param_length = 0; /* fill in at the end */
3295 /* Fill in the stc cookie data */
3296 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3298 /* tack the INIT and then the INIT-ACK onto the chain */
3301 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3302 cookie_sz += SCTP_BUF_LEN(m_at);
3303 if (SCTP_BUF_NEXT(m_at) == NULL) {
3304 SCTP_BUF_NEXT(m_at) = copy_init;
3309 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3310 cookie_sz += SCTP_BUF_LEN(m_at);
3311 if (SCTP_BUF_NEXT(m_at) == NULL) {
3312 SCTP_BUF_NEXT(m_at) = copy_initack;
3317 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3318 cookie_sz += SCTP_BUF_LEN(m_at);
3319 if (SCTP_BUF_NEXT(m_at) == NULL) {
3323 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3325 /* no space, so free the entire chain */
3329 SCTP_BUF_LEN(sig) = 0;
3330 SCTP_BUF_NEXT(m_at) = sig;
3332 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3333 memset(foo, 0, SCTP_SIGNATURE_SIZE);
3335 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3336 cookie_sz += SCTP_SIGNATURE_SIZE;
3337 ph->param_length = htons(cookie_sz);
3343 sctp_get_ect(struct sctp_tcb *stcb,
3344 struct sctp_tmit_chunk *chk)
3346 uint8_t this_random;
3349 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3352 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3353 /* no nonce, always return ECT0 */
3354 return (SCTP_ECT0_BIT);
3356 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3357 /* Peer does NOT support it, so we send a ECT0 only */
3358 return (SCTP_ECT0_BIT);
3361 return (SCTP_ECT0_BIT);
3363 if ((stcb->asoc.hb_random_idx > 3) ||
3364 ((stcb->asoc.hb_random_idx == 3) &&
3365 (stcb->asoc.hb_ect_randombit > 7))) {
3369 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3370 memcpy(stcb->asoc.hb_random_values, &rndval,
3371 sizeof(stcb->asoc.hb_random_values));
3372 this_random = stcb->asoc.hb_random_values[0];
3373 stcb->asoc.hb_random_idx = 0;
3374 stcb->asoc.hb_ect_randombit = 0;
3376 if (stcb->asoc.hb_ect_randombit > 7) {
3377 stcb->asoc.hb_ect_randombit = 0;
3378 stcb->asoc.hb_random_idx++;
3379 if (stcb->asoc.hb_random_idx > 3) {
3383 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3385 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3387 /* ECN Nonce stuff */
3388 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3389 stcb->asoc.hb_ect_randombit++;
3390 return (SCTP_ECT1_BIT);
3392 stcb->asoc.hb_ect_randombit++;
3393 return (SCTP_ECT0_BIT);
3398 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3399 struct sctp_tcb *stcb, /* may be NULL */
3400 struct sctp_nets *net,
3401 struct sockaddr *to,
3403 uint32_t auth_offset,
3404 struct sctp_auth_chunk *auth,
3405 int nofragment_flag,
3407 struct sctp_tmit_chunk *chk,
3411 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3414 union sctp_sockstore *over_addr
3416 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3419 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3420 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3421 * structure: - fill in the HMAC digest of any AUTH chunk in the
3422 * packet. - calculate and fill in the SCTP checksum. - prepend an
3423 * IP address header. - if boundall use INADDR_ANY. - if
3424 * boundspecific do source address selection. - set fragmentation
3425 * option for ipV4. - On return from IP output, check/adjust mtu
3426 * size of output interface and smallest_mtu size as well.
3428 /* Will need ifdefs around this */
3431 struct sctphdr *sctphdr;
3436 sctp_route_t *ro = NULL;
3437 struct udphdr *udp = NULL;
3439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3440 struct socket *so = NULL;
3444 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3445 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3450 vrf_id = stcb->asoc.vrf_id;
3452 vrf_id = inp->def_vrf_id;
3455 /* fill in the HMAC digest for any AUTH chunk in the packet */
3456 if ((auth != NULL) && (stcb != NULL)) {
3457 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3459 /* Calculate the csum and fill in the length of the packet */
3460 sctphdr = mtod(m, struct sctphdr *);
3461 if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3463 (to->sa_family == AF_INET) &&
3464 (stcb->asoc.loopback_scope)) {
3465 sctphdr->checksum = 0;
3467 * This can probably now be taken out since my audit shows
3468 * no more bad pktlen's coming in. But we will wait a while
3471 packet_length = sctp_calculate_len(m);
3473 sctphdr->checksum = 0;
3474 csum = sctp_calculate_sum(m, &packet_length, 0);
3475 sctphdr->checksum = csum;
3478 if (to->sa_family == AF_INET) {
3479 struct ip *ip = NULL;
3480 sctp_route_t iproute;
3484 newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3486 newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3490 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3494 SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
3495 SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
3496 packet_length += sizeof(struct ip) + sizeof(struct udphdr);
3498 SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3499 SCTP_BUF_LEN(newm) = sizeof(struct ip);
3500 packet_length += sizeof(struct ip);
3502 SCTP_BUF_NEXT(newm) = m;
3504 ip = mtod(m, struct ip *);
3505 ip->ip_v = IPVERSION;
3506 ip->ip_hl = (sizeof(struct ip) >> 2);
3508 tos_value = net->tos_flowlabel & 0x000000ff;
3510 tos_value = inp->ip_inp.inp.inp_ip_tos;
3512 if ((nofragment_flag) && (port == 0)) {
3513 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3516 ip->ip_off = htons(IP_DF);
3521 /* FreeBSD has a function for ip_id's */
3522 ip->ip_id = ip_newid();
3524 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3525 ip->ip_len = packet_length;
3527 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3529 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3532 ip->ip_tos = (u_char)(tos_value & 0xfc);
3535 /* no association at all */
3536 ip->ip_tos = (tos_value & 0xfc);
3539 ip->ip_p = IPPROTO_UDP;
3541 ip->ip_p = IPPROTO_SCTP;
3546 memset(&iproute, 0, sizeof(iproute));
3547 memcpy(&ro->ro_dst, to, to->sa_len);
3549 ro = (sctp_route_t *) & net->ro;
3551 /* Now the address selection part */
3552 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3554 /* call the routine to select the src address */
3555 if (net && out_of_asoc_ok == 0) {
3556 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3557 sctp_free_ifa(net->ro._s_addr);
3558 net->ro._s_addr = NULL;
3559 net->src_addr_selected = 0;
3565 if (net->src_addr_selected == 0) {
3566 /* Cache the source address */
3567 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3570 net->src_addr_selected = 1;
3572 if (net->ro._s_addr == NULL) {
3573 /* No route to host */
3574 net->src_addr_selected = 0;
3577 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3579 if (over_addr == NULL) {
3580 struct sctp_ifa *_lsrc;
3582 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3586 if (_lsrc == NULL) {
3589 ip->ip_src = _lsrc->address.sin.sin_addr;
3590 sctp_free_ifa(_lsrc);
3592 ip->ip_src = over_addr->sin.sin_addr;
3593 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3597 udp = (struct udphdr *)(ip + 1);
3598 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3599 udp->uh_dport = port;
3600 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3601 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3604 * If source address selection fails and we find no route
3605 * then the ip_output should fail as well with a
3606 * NO_ROUTE_TO_HOST type error. We probably should catch
3607 * that somewhere and abort the association right away
3608 * (assuming this is an INIT being sent).
3610 if ((ro->ro_rt == NULL)) {
3612 * src addr selection failed to find a route (or
3613 * valid source addr), so we can't get there from
3617 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3618 "%s: dropped packet - no valid source addr\n",
3621 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3622 "Destination was ");
3623 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3624 &net->ro._l_addr.sa);
3625 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3626 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3627 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3628 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3630 SCTP_FAILED_THRESHOLD,
3633 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3634 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3636 * JRS 5/14/07 - If a
3638 * unreachable, the PF bit
3639 * is turned off. This
3640 * allows an unambiguous use
3642 * destinations that are
3643 * reachable but potentially
3645 * destination is set to the
3646 * unreachable state, also
3647 * set the destination to
3651 * Add debug message here if
3652 * destination is not in PF
3656 * Stop any running T3
3659 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3660 net->dest_state &= ~SCTP_ADDR_PF;
3661 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3667 if (net == stcb->asoc.primary_destination) {
3668 /* need a new primary */
3669 struct sctp_nets *alt;
3671 alt = sctp_find_alternate_net(stcb, net, 0);
3673 if (sctp_set_primary_addr(stcb,
3674 (struct sockaddr *)NULL,
3676 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3677 if (net->ro._s_addr) {
3678 sctp_free_ifa(net->ro._s_addr);
3679 net->ro._s_addr = NULL;
3681 net->src_addr_selected = 0;
3687 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3689 return (EHOSTUNREACH);
3691 if (ro != &iproute) {
3692 memcpy(&iproute, ro, sizeof(*ro));
3694 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3695 (uint32_t) (ntohl(ip->ip_src.s_addr)));
3696 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3697 (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3698 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3701 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3702 /* failed to prepend data, give up */
3703 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3707 #ifdef SCTP_PACKET_LOGGING
3708 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3709 sctp_packet_log(m, packet_length);
3711 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3713 SCTP_ENABLE_UDP_CSUM(o_pak);
3715 /* send it out. table id is taken from stcb */
3716 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3717 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3718 so = SCTP_INP_SO(inp);
3719 SCTP_SOCKET_UNLOCK(so, 0);
3722 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3723 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 atomic_add_int(&stcb->asoc.refcnt, 1);
3726 SCTP_TCB_UNLOCK(stcb);
3727 SCTP_SOCKET_LOCK(so, 0);
3728 SCTP_TCB_LOCK(stcb);
3729 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3732 SCTP_STAT_INCR(sctps_sendpackets);
3733 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3735 SCTP_STAT_INCR(sctps_senderrors);
3737 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3739 /* free tempy routes */
3745 /* PMTU check versus smallest asoc MTU goes here */
3746 if ((ro->ro_rt != NULL) &&
3747 (net->ro._s_addr)) {
3750 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3752 (stcb->asoc.smallest_mtu > mtu)) {
3753 #ifdef SCTP_PRINT_FOR_B_AND_M
3754 SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3757 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3760 } else if (ro->ro_rt == NULL) {
3761 /* route was freed */
3762 if (net->ro._s_addr &&
3763 net->src_addr_selected) {
3764 sctp_free_ifa(net->ro._s_addr);
3765 net->ro._s_addr = NULL;
3767 net->src_addr_selected = 0;
3773 else if (to->sa_family == AF_INET6) {
3775 struct ip6_hdr *ip6h;
3776 struct route_in6 ip6route;
3779 uint16_t flowBottom;
3780 u_char tosBottom, tosTop;
3781 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3783 struct sockaddr_in6 lsa6_storage;
3785 u_short prev_port = 0;
3788 flowlabel = net->tos_flowlabel;
3790 flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3794 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3796 newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3800 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3804 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3805 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3806 packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3808 SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3809 SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3810 packet_length += sizeof(struct ip6_hdr);
3812 SCTP_BUF_NEXT(newm) = m;
3815 ip6h = mtod(m, struct ip6_hdr *);
3817 * We assume here that inp_flow is in host byte order within
3820 flowBottom = flowlabel & 0x0000ffff;
3821 flowTop = ((flowlabel & 0x000f0000) >> 16);
3822 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3823 /* protect *sin6 from overwrite */
3824 sin6 = (struct sockaddr_in6 *)to;
3828 /* KAME hack: embed scopeid */
3829 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3830 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3834 memset(&ip6route, 0, sizeof(ip6route));
3835 ro = (sctp_route_t *) & ip6route;
3836 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3838 ro = (sctp_route_t *) & net->ro;
3841 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3843 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3846 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3849 /* we could get no asoc if it is a O-O-T-B packet */
3850 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3852 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3854 ip6h->ip6_nxt = IPPROTO_UDP;
3856 ip6h->ip6_nxt = IPPROTO_SCTP;
3858 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3859 ip6h->ip6_dst = sin6->sin6_addr;
3862 * Add SRC address selection here: we can only reuse to a
3863 * limited degree the kame src-addr-sel, since we can try
3864 * their selection but it may not be bound.
3866 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3867 lsa6_tmp.sin6_family = AF_INET6;
3868 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3870 if (net && out_of_asoc_ok == 0) {
3871 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3872 sctp_free_ifa(net->ro._s_addr);
3873 net->ro._s_addr = NULL;
3874 net->src_addr_selected = 0;
3880 if (net->src_addr_selected == 0) {
3881 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3882 /* KAME hack: embed scopeid */
3883 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3884 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3887 /* Cache the source address */
3888 net->ro._s_addr = sctp_source_address_selection(inp,
3894 (void)sa6_recoverscope(sin6);
3895 net->src_addr_selected = 1;
3897 if (net->ro._s_addr == NULL) {
3898 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3899 net->src_addr_selected = 0;
3902 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3904 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3905 /* KAME hack: embed scopeid */
3906 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3907 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3910 if (over_addr == NULL) {
3911 struct sctp_ifa *_lsrc;
3913 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3917 if (_lsrc == NULL) {
3920 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3921 sctp_free_ifa(_lsrc);
3923 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3924 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3926 (void)sa6_recoverscope(sin6);
3928 lsa6->sin6_port = inp->sctp_lport;
3930 if (ro->ro_rt == NULL) {
3932 * src addr selection failed to find a route (or
3933 * valid source addr), so we can't get there from
3939 * XXX: sa6 may not have a valid sin6_scope_id in the
3940 * non-SCOPEDROUTING case.
3942 bzero(&lsa6_storage, sizeof(lsa6_storage));
3943 lsa6_storage.sin6_family = AF_INET6;
3944 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3945 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3946 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3947 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3952 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3953 lsa6_storage.sin6_port = inp->sctp_lport;
3954 lsa6 = &lsa6_storage;
3955 ip6h->ip6_src = lsa6->sin6_addr;
3958 udp = (struct udphdr *)(ip6h + 1);
3959 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3960 udp->uh_dport = port;
3961 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3965 * We set the hop limit now since there is a good chance
3966 * that our ro pointer is now filled
3968 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3969 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3972 /* Copy to be sure something bad is not happening */
3973 sin6->sin6_addr = ip6h->ip6_dst;
3974 lsa6->sin6_addr = ip6h->ip6_src;
3977 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3978 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3979 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3980 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3981 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3983 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3984 /* preserve the port and scope for link local send */
3985 prev_scope = sin6->sin6_scope_id;
3986 prev_port = sin6->sin6_port;
3988 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3989 /* failed to prepend data, give up */
3991 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3994 #ifdef SCTP_PACKET_LOGGING
3995 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3996 sctp_packet_log(m, packet_length);
3998 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4000 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4001 udp->uh_sum = 0xffff;
4004 /* send it out. table id is taken from stcb */
4005 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4006 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4007 so = SCTP_INP_SO(inp);
4008 SCTP_SOCKET_UNLOCK(so, 0);
4011 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4012 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4013 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4014 atomic_add_int(&stcb->asoc.refcnt, 1);
4015 SCTP_TCB_UNLOCK(stcb);
4016 SCTP_SOCKET_LOCK(so, 0);
4017 SCTP_TCB_LOCK(stcb);
4018 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4022 /* for link local this must be done */
4023 sin6->sin6_scope_id = prev_scope;
4024 sin6->sin6_port = prev_port;
4026 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4027 SCTP_STAT_INCR(sctps_sendpackets);
4028 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4030 SCTP_STAT_INCR(sctps_senderrors);
4033 /* Now if we had a temp route free it */
4038 /* PMTU check versus smallest asoc MTU goes here */
4039 if (ro->ro_rt == NULL) {
4040 /* Route was freed */
4041 if (net->ro._s_addr &&
4042 net->src_addr_selected) {
4043 sctp_free_ifa(net->ro._s_addr);
4044 net->ro._s_addr = NULL;
4046 net->src_addr_selected = 0;
4048 if ((ro->ro_rt != NULL) &&
4049 (net->ro._s_addr)) {
4052 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4054 (stcb->asoc.smallest_mtu > mtu)) {
4055 #ifdef SCTP_PRINT_FOR_B_AND_M
4056 SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4059 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4063 if (ND_IFINFO(ifp)->linkmtu &&
4064 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4065 #ifdef SCTP_PRINT_FOR_B_AND_M
4066 SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4067 ND_IFINFO(ifp)->linkmtu);
4069 sctp_mtu_size_reset(inp,
4071 ND_IFINFO(ifp)->linkmtu);
4079 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4080 ((struct sockaddr *)to)->sa_family);
4082 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4089 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4090 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4095 struct mbuf *m, *m_at, *mp_last;
4096 struct sctp_nets *net;
4097 struct sctp_init_msg *initm;
4098 struct sctp_supported_addr_param *sup_addr;
4099 struct sctp_ecn_supported_param *ecn;
4100 struct sctp_prsctp_supported_param *prsctp;
4101 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4102 struct sctp_supported_chunk_types_param *pr_supported;
4103 int cnt_inits_to = 0;
4108 /* INIT's always go to the primary (and usually ONLY address) */
4110 net = stcb->asoc.primary_destination;
4112 net = TAILQ_FIRST(&stcb->asoc.nets);
4117 /* we confirm any address we send an INIT to */
4118 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4119 (void)sctp_set_primary_addr(stcb, NULL, net);
4121 /* we confirm any address we send an INIT to */
4122 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4124 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4126 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4128 * special hook, if we are sending to link local it will not
4129 * show up in our private address count.
4131 struct sockaddr_in6 *sin6l;
4133 sin6l = &net->ro._l_addr.sin6;
4134 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4138 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4139 /* This case should not happen */
4140 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4143 /* start the INIT timer */
4144 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4146 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4148 /* No memory, INIT timer will re-attempt. */
4149 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4152 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4154 * assume peer supports asconf in order to be able to queue local
4155 * address changes while an INIT is in flight and before the assoc
4158 stcb->asoc.peer_supports_asconf = 1;
4159 /* Now lets put the SCTP header in place */
4160 initm = mtod(m, struct sctp_init_msg *);
4161 initm->sh.src_port = inp->sctp_lport;
4162 initm->sh.dest_port = stcb->rport;
4163 initm->sh.v_tag = 0;
4164 initm->sh.checksum = 0; /* calculate later */
4165 /* now the chunk header */
4166 initm->msg.ch.chunk_type = SCTP_INITIATION;
4167 initm->msg.ch.chunk_flags = 0;
4168 /* fill in later from mbuf we build */
4169 initm->msg.ch.chunk_length = 0;
4170 /* place in my tag */
4171 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
4172 /* set up some of the credits. */
4173 initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
4174 SCTP_MINIMAL_RWND));
4176 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4177 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4178 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4179 /* now the address restriction */
4180 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
4182 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4184 /* we support 2 types: IPv6/IPv4 */
4185 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4186 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4187 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4189 /* we support 1 type: IPv4 */
4190 sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4191 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4192 sup_addr->addr_type[1] = htons(0); /* this is the padding */
4194 SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4196 if (inp->sctp_ep.adaptation_layer_indicator) {
4197 struct sctp_adaptation_layer_indication *ali;
4199 ali = (struct sctp_adaptation_layer_indication *)(
4200 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4201 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4202 ali->ph.param_length = htons(sizeof(*ali));
4203 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4204 SCTP_BUF_LEN(m) += sizeof(*ali);
4205 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
4208 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
4209 sizeof(*sup_addr) + sizeof(uint16_t));
4212 /* now any cookie time extensions */
4213 if (stcb->asoc.cookie_preserve_req) {
4214 struct sctp_cookie_perserve_param *cookie_preserve;
4216 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4217 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4218 cookie_preserve->ph.param_length = htons(
4219 sizeof(*cookie_preserve));
4220 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4221 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4222 ecn = (struct sctp_ecn_supported_param *)(
4223 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4224 stcb->asoc.cookie_preserve_req = 0;
4227 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4228 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4229 ecn->ph.param_length = htons(sizeof(*ecn));
4230 SCTP_BUF_LEN(m) += sizeof(*ecn);
4231 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4234 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4236 /* And now tell the peer we do pr-sctp */
4237 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4238 prsctp->ph.param_length = htons(sizeof(*prsctp));
4239 SCTP_BUF_LEN(m) += sizeof(*prsctp);
4241 /* And now tell the peer we do all the extensions */
4242 pr_supported = (struct sctp_supported_chunk_types_param *)
4243 ((caddr_t)prsctp + sizeof(*prsctp));
4244 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4246 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4247 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4248 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4249 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4250 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4251 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
4252 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4253 p_len = sizeof(*pr_supported) + num_ext;
4254 pr_supported->ph.param_length = htons(p_len);
4255 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4256 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4258 /* ECN nonce: And now tell the peer we support ECN nonce */
4259 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4260 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4261 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4262 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4263 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4264 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4266 /* add authentication parameters */
4267 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4268 struct sctp_auth_random *randp;
4269 struct sctp_auth_hmac_algo *hmacs;
4270 struct sctp_auth_chunk_list *chunks;
4272 /* attach RANDOM parameter, if available */
4273 if (stcb->asoc.authinfo.random != NULL) {
4274 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4275 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4276 #ifdef SCTP_AUTH_DRAFT_04
4277 randp->ph.param_type = htons(SCTP_RANDOM);
4278 randp->ph.param_length = htons(p_len);
4279 bcopy(stcb->asoc.authinfo.random->key,
4281 stcb->asoc.authinfo.random_len);
4283 /* random key already contains the header */
4284 bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4286 /* zero out any padding required */
4287 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4288 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4290 /* add HMAC_ALGO parameter */
4291 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4292 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4293 (uint8_t *) hmacs->hmac_ids);
4295 p_len += sizeof(*hmacs);
4296 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4297 hmacs->ph.param_length = htons(p_len);
4298 /* zero out any padding required */
4299 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4300 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4302 /* add CHUNKS parameter */
4303 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4304 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4305 chunks->chunk_types);
4307 p_len += sizeof(*chunks);
4308 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4309 chunks->ph.param_length = htons(p_len);
4310 /* zero out any padding required */
4311 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4312 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4316 /* now the addresses */
4318 struct sctp_scoping scp;
4321 * To optimize this we could put the scoping stuff into a
4322 * structure and remove the individual uint8's from the
4323 * assoc structure. Then we could just sifa in the address
4324 * within the stcb.. but for now this is a quick hack to get
4325 * the address stuff teased apart.
4327 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4328 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4329 scp.loopback_scope = stcb->asoc.loopback_scope;
4330 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4331 scp.local_scope = stcb->asoc.local_scope;
4332 scp.site_scope = stcb->asoc.site_scope;
4334 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4337 /* calulate the size and update pkt header and chunk header */
4339 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4340 if (SCTP_BUF_NEXT(m_at) == NULL)
4342 p_len += SCTP_BUF_LEN(m_at);
4344 initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4346 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4347 * here since the timer will drive a retranmission.
4350 /* I don't expect this to execute but we will be safe here */
4352 if ((padval) && (mp_last)) {
4354 * The compiler worries that mp_last may not be set even
4355 * though I think it is impossible :-> however we add
4356 * mp_last here just in case.
4358 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4360 /* Houston we have a problem, no space */
4366 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4367 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4368 (struct sockaddr *)&net->ro._l_addr,
4369 m, 0, NULL, 0, 0, NULL, 0, net->port, so_locked, NULL);
4370 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4371 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4372 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4373 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4377 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4378 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4381 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4382 * being equal to the beginning of the params i.e. (iphlen +
4383 * sizeof(struct sctp_init_msg) parse through the parameters to the
4384 * end of the mbuf verifying that all parameters are known.
4386 * For unknown parameters build and return a mbuf with
4387 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4388 * processing this chunk stop, and set *abort_processing to 1.
4390 * By having param_offset be pre-set to where parameters begin it is
4391 * hoped that this routine may be reused in the future by new
4394 struct sctp_paramhdr *phdr, params;
4396 struct mbuf *mat, *op_err;
4397 char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4398 int at, limit, pad_needed;
4399 uint16_t ptype, plen, padded_size;
4402 *abort_processing = 0;
4405 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4408 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4409 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4410 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4411 ptype = ntohs(phdr->param_type);
4412 plen = ntohs(phdr->param_length);
4413 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4414 /* wacked parameter */
4415 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4418 limit -= SCTP_SIZE32(plen);
4420 * All parameters for all chunks that we know/understand are
4421 * listed here. We process them other places and make
4422 * appropriate stop actions per the upper bits. However this
4423 * is the generic routine processor's can call to get back
4424 * an operr.. to either incorporate (init-ack) or send.
4426 padded_size = SCTP_SIZE32(plen);
4428 /* Param's with variable size */
4429 case SCTP_HEARTBEAT_INFO:
4430 case SCTP_STATE_COOKIE:
4431 case SCTP_UNRECOG_PARAM:
4432 case SCTP_ERROR_CAUSE_IND:
4436 /* Param's with variable size within a range */
4437 case SCTP_CHUNK_LIST:
4438 case SCTP_SUPPORTED_CHUNK_EXT:
4439 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4440 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4445 case SCTP_SUPPORTED_ADDRTYPE:
4446 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4447 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4453 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4454 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4459 case SCTP_SET_PRIM_ADDR:
4460 case SCTP_DEL_IP_ADDRESS:
4461 case SCTP_ADD_IP_ADDRESS:
4462 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4463 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4464 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4469 /* Param's with a fixed size */
4470 case SCTP_IPV4_ADDRESS:
4471 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4472 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4477 case SCTP_IPV6_ADDRESS:
4478 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4479 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4484 case SCTP_COOKIE_PRESERVE:
4485 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4486 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4491 case SCTP_ECN_NONCE_SUPPORTED:
4492 case SCTP_PRSCTP_SUPPORTED:
4493 if (padded_size != sizeof(struct sctp_paramhdr)) {
4494 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4499 case SCTP_ECN_CAPABLE:
4500 if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4501 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4506 case SCTP_ULP_ADAPTATION:
4507 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4508 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4513 case SCTP_SUCCESS_REPORT:
4514 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4515 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4520 case SCTP_HOSTNAME_ADDRESS:
4522 /* We can NOT handle HOST NAME addresses!! */
4525 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4526 *abort_processing = 1;
4527 if (op_err == NULL) {
4528 /* Ok need to try to get a mbuf */
4530 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4532 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4535 l_len += sizeof(struct sctp_paramhdr);
4536 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4538 SCTP_BUF_LEN(op_err) = 0;
4540 * pre-reserve space for ip
4541 * and sctp header and
4545 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4547 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4549 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4550 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4554 /* If we have space */
4555 struct sctp_paramhdr s;
4558 uint32_t cpthis = 0;
4560 pad_needed = 4 - (err_at % 4);
4561 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4562 err_at += pad_needed;
4564 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4565 s.param_length = htons(sizeof(s) + plen);
4566 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4567 err_at += sizeof(s);
4568 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4570 sctp_m_freem(op_err);
4572 * we are out of memory but
4573 * we still need to have a
4574 * look at what to do (the
4575 * system is in trouble
4580 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4588 * we do not recognize the parameter figure out what
4591 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4592 if ((ptype & 0x4000) == 0x4000) {
4593 /* Report bit is set?? */
4594 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4595 if (op_err == NULL) {
4598 /* Ok need to try to get an mbuf */
4600 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4602 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4605 l_len += sizeof(struct sctp_paramhdr);
4606 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4608 SCTP_BUF_LEN(op_err) = 0;
4610 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4612 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4614 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4615 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4619 /* If we have space */
4620 struct sctp_paramhdr s;
4623 uint32_t cpthis = 0;
4625 pad_needed = 4 - (err_at % 4);
4626 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4627 err_at += pad_needed;
4629 s.param_type = htons(SCTP_UNRECOG_PARAM);
4630 s.param_length = htons(sizeof(s) + plen);
4631 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4632 err_at += sizeof(s);
4633 if (plen > sizeof(tempbuf)) {
4634 plen = sizeof(tempbuf);
4636 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4638 sctp_m_freem(op_err);
4640 * we are out of memory but
4641 * we still need to have a
4642 * look at what to do (the
4643 * system is in trouble
4647 goto more_processing;
4649 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4654 if ((ptype & 0x8000) == 0x0000) {
4655 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4658 /* skip this chunk and continue processing */
4659 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4660 at += SCTP_SIZE32(plen);
4665 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4669 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4670 *abort_processing = 1;
4671 if ((op_err == NULL) && phdr) {
4675 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4677 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4679 l_len += (2 * sizeof(struct sctp_paramhdr));
4680 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4682 SCTP_BUF_LEN(op_err) = 0;
4684 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4686 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4688 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4689 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4692 if ((op_err) && phdr) {
4693 struct sctp_paramhdr s;
4696 uint32_t cpthis = 0;
4698 pad_needed = 4 - (err_at % 4);
4699 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4700 err_at += pad_needed;
4702 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4703 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4704 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4705 err_at += sizeof(s);
4706 /* Only copy back the p-hdr that caused the issue */
4707 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4713 sctp_are_there_new_addresses(struct sctp_association *asoc,
4714 struct mbuf *in_initpkt, int iphlen, int offset)
4717 * Given a INIT packet, look through the packet to verify that there
4718 * are NO new addresses. As we go through the parameters add reports
4719 * of any un-understood parameters that require an error. Also we
4720 * must return (1) to drop the packet if we see a un-understood
4721 * parameter that tells us to drop the chunk.
4723 struct sockaddr_in sin4, *sa4;
4726 struct sockaddr_in6 sin6, *sa6;
4729 struct sockaddr *sa_touse;
4730 struct sockaddr *sa;
4731 struct sctp_paramhdr *phdr, params;
4735 struct ip6_hdr *ip6h;
4739 uint16_t ptype, plen;
4742 struct sctp_nets *net;
4744 memset(&sin4, 0, sizeof(sin4));
4746 memset(&sin6, 0, sizeof(sin6));
4748 sin4.sin_family = AF_INET;
4749 sin4.sin_len = sizeof(sin4);
4751 sin6.sin6_family = AF_INET6;
4752 sin6.sin6_len = sizeof(sin6);
4755 /* First what about the src address of the pkt ? */
4756 iph = mtod(in_initpkt, struct ip *);
4757 switch (iph->ip_v) {
4759 /* source addr is IPv4 */
4760 sin4.sin_addr = iph->ip_src;
4761 sa_touse = (struct sockaddr *)&sin4;
4764 case IPV6_VERSION >> 4:
4765 /* source addr is IPv6 */
4766 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4767 sin6.sin6_addr = ip6h->ip6_src;
4768 sa_touse = (struct sockaddr *)&sin6;
4776 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4777 sa = (struct sockaddr *)&net->ro._l_addr;
4778 if (sa->sa_family == sa_touse->sa_family) {
4779 if (sa->sa_family == AF_INET) {
4780 sa4 = (struct sockaddr_in *)sa;
4781 if (sa4->sin_addr.s_addr ==
4782 sin4.sin_addr.s_addr) {
4788 if (sa->sa_family == AF_INET6) {
4789 sa6 = (struct sockaddr_in6 *)sa;
4790 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4800 /* New address added! no need to look futher. */
4803 /* Ok so far lets munge through the rest of the packet */
4807 offset += sizeof(struct sctp_init_chunk);
4808 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4810 ptype = ntohs(phdr->param_type);
4811 plen = ntohs(phdr->param_length);
4812 if (ptype == SCTP_IPV4_ADDRESS) {
4813 struct sctp_ipv4addr_param *p4, p4_buf;
4815 phdr = sctp_get_next_param(mat, offset,
4816 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4817 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4821 p4 = (struct sctp_ipv4addr_param *)phdr;
4822 sin4.sin_addr.s_addr = p4->addr;
4823 sa_touse = (struct sockaddr *)&sin4;
4824 } else if (ptype == SCTP_IPV6_ADDRESS) {
4825 struct sctp_ipv6addr_param *p6, p6_buf;
4827 phdr = sctp_get_next_param(mat, offset,
4828 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4829 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4833 p6 = (struct sctp_ipv6addr_param *)phdr;
4835 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4838 sa_touse = (struct sockaddr *)&sin4;
4841 /* ok, sa_touse points to one to check */
4843 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4844 sa = (struct sockaddr *)&net->ro._l_addr;
4845 if (sa->sa_family != sa_touse->sa_family) {
4848 if (sa->sa_family == AF_INET) {
4849 sa4 = (struct sockaddr_in *)sa;
4850 if (sa4->sin_addr.s_addr ==
4851 sin4.sin_addr.s_addr) {
4857 if (sa->sa_family == AF_INET6) {
4858 sa6 = (struct sockaddr_in6 *)sa;
4859 if (SCTP6_ARE_ADDR_EQUAL(
4868 /* New addr added! no need to look further */
4872 offset += SCTP_SIZE32(plen);
4873 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
4879 * Given a MBUF chain that was sent into us containing an INIT. Build a
4880 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4881 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4882 * message (i.e. the struct sctp_init_msg).
4885 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4886 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4887 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4889 struct sctp_association *asoc;
4890 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4891 struct sctp_init_msg *initackm_out;
4892 struct sctp_ecn_supported_param *ecn;
4893 struct sctp_prsctp_supported_param *prsctp;
4894 struct sctp_ecn_nonce_supported_param *ecn_nonce;
4895 struct sctp_supported_chunk_types_param *pr_supported;
4896 union sctp_sockstore store, store1, *over_addr;
4897 struct sockaddr_in *sin, *to_sin;
4900 struct sockaddr_in6 *sin6, *to_sin6;
4906 struct ip6_hdr *ip6;
4909 struct sockaddr *to;
4910 struct sctp_state_cookie stc;
4911 struct sctp_nets *net = NULL;
4912 uint8_t *signature = NULL;
4913 int cnt_inits_to = 0;
4914 uint16_t his_limit, i_want;
4915 int abort_flag, padval;
4925 if ((asoc != NULL) &&
4926 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4927 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4928 /* new addresses, out of here in non-cookie-wait states */
4930 * Send a ABORT, we don't add the new address error clause
4931 * though we even set the T bit and copy in the 0 tag.. this
4932 * looks no different than if no listener was present.
4934 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4938 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4939 (offset + sizeof(struct sctp_init_chunk)),
4940 &abort_flag, (struct sctp_chunkhdr *)init_chk);
4943 sctp_send_abort(init_pkt, iphlen, sh,
4944 init_chk->init.initiate_tag, op_err, vrf_id, port);
4947 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4949 /* No memory, INIT timer will re-attempt. */
4951 sctp_m_freem(op_err);
4954 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4956 /* the time I built cookie */
4957 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4959 /* populate any tie tags */
4961 /* unlock before tag selections */
4962 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4963 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4964 stc.cookie_life = asoc->cookie_life;
4965 net = asoc->primary_destination;
4967 stc.tie_tag_my_vtag = 0;
4968 stc.tie_tag_peer_vtag = 0;
4969 /* life I will award this cookie */
4970 stc.cookie_life = inp->sctp_ep.def_cookie_life;
4973 /* copy in the ports for later check */
4974 stc.myport = sh->dest_port;
4975 stc.peerport = sh->src_port;
4978 * If we wanted to honor cookie life extentions, we would add to
4979 * stc.cookie_life. For now we should NOT honor any extension
4981 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4982 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4983 struct inpcb *in_inp;
4985 /* Its a V6 socket */
4986 in_inp = (struct inpcb *)inp;
4987 stc.ipv6_addr_legal = 1;
4988 /* Now look at the binding flag to see if V4 will be legal */
4989 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4990 stc.ipv4_addr_legal = 1;
4992 /* V4 addresses are NOT legal on the association */
4993 stc.ipv4_addr_legal = 0;
4996 /* Its a V4 socket, no - V6 */
4997 stc.ipv4_addr_legal = 1;
4998 stc.ipv6_addr_legal = 0;
5001 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5006 /* now for scope setup */
5007 memset((caddr_t)&store, 0, sizeof(store));
5008 memset((caddr_t)&store1, 0, sizeof(store1));
5010 to_sin = &store1.sin;
5013 to_sin6 = &store1.sin6;
5015 iph = mtod(init_pkt, struct ip *);
5016 /* establish the to_addr's */
5017 switch (iph->ip_v) {
5019 to_sin->sin_port = sh->dest_port;
5020 to_sin->sin_family = AF_INET;
5021 to_sin->sin_len = sizeof(struct sockaddr_in);
5022 to_sin->sin_addr = iph->ip_dst;
5025 case IPV6_VERSION >> 4:
5026 ip6 = mtod(init_pkt, struct ip6_hdr *);
5027 to_sin6->sin6_addr = ip6->ip6_dst;
5028 to_sin6->sin6_scope_id = 0;
5029 to_sin6->sin6_port = sh->dest_port;
5030 to_sin6->sin6_family = AF_INET6;
5031 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5040 to = (struct sockaddr *)&store;
5041 switch (iph->ip_v) {
5044 sin->sin_family = AF_INET;
5045 sin->sin_len = sizeof(struct sockaddr_in);
5046 sin->sin_port = sh->src_port;
5047 sin->sin_addr = iph->ip_src;
5048 /* lookup address */
5049 stc.address[0] = sin->sin_addr.s_addr;
5053 stc.addr_type = SCTP_IPV4_ADDRESS;
5054 /* local from address */
5055 stc.laddress[0] = to_sin->sin_addr.s_addr;
5056 stc.laddress[1] = 0;
5057 stc.laddress[2] = 0;
5058 stc.laddress[3] = 0;
5059 stc.laddr_type = SCTP_IPV4_ADDRESS;
5060 /* scope_id is only for v6 */
5062 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5063 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5068 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5069 /* Must use the address in this case */
5070 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5071 stc.loopback_scope = 1;
5074 stc.local_scope = 0;
5079 case IPV6_VERSION >> 4:
5081 ip6 = mtod(init_pkt, struct ip6_hdr *);
5082 sin6->sin6_family = AF_INET6;
5083 sin6->sin6_len = sizeof(struct sockaddr_in6);
5084 sin6->sin6_port = sh->src_port;
5085 sin6->sin6_addr = ip6->ip6_src;
5086 /* lookup address */
5087 memcpy(&stc.address, &sin6->sin6_addr,
5088 sizeof(struct in6_addr));
5089 sin6->sin6_scope_id = 0;
5090 stc.addr_type = SCTP_IPV6_ADDRESS;
5092 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5094 * FIX ME: does this have scope from
5097 (void)sa6_recoverscope(sin6);
5098 stc.scope_id = sin6->sin6_scope_id;
5099 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5100 stc.loopback_scope = 1;
5101 stc.local_scope = 0;
5104 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5106 * If the new destination is a
5107 * LINK_LOCAL we must have common
5108 * both site and local scope. Don't
5109 * set local scope though since we
5110 * must depend on the source to be
5111 * added implicitly. We cannot
5112 * assure just because we share one
5113 * link that all links are common.
5115 stc.local_scope = 0;
5119 * we start counting for the private
5120 * address stuff at 1. since the
5121 * link local we source from won't
5122 * show up in our scoped count.
5126 * pull out the scope_id from
5130 * FIX ME: does this have scope from
5133 (void)sa6_recoverscope(sin6);
5134 stc.scope_id = sin6->sin6_scope_id;
5135 sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5136 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5138 * If the new destination is
5139 * SITE_LOCAL then we must have site
5144 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5145 stc.laddr_type = SCTP_IPV6_ADDRESS;
5155 /* set the scope per the existing tcb */
5158 struct sctp_nets *lnet;
5162 stc.loopback_scope = asoc->loopback_scope;
5163 stc.ipv4_scope = asoc->ipv4_local_scope;
5164 stc.site_scope = asoc->site_scope;
5165 stc.local_scope = asoc->local_scope;
5167 /* Why do we not consider IPv4 LL addresses? */
5168 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5169 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5170 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5172 * if we have a LL address, start
5180 /* use the net pointer */
5181 to = (struct sockaddr *)&net->ro._l_addr;
5182 switch (to->sa_family) {
5184 sin = (struct sockaddr_in *)to;
5185 stc.address[0] = sin->sin_addr.s_addr;
5189 stc.addr_type = SCTP_IPV4_ADDRESS;
5190 if (net->src_addr_selected == 0) {
5192 * strange case here, the INIT should have
5193 * did the selection.
5195 net->ro._s_addr = sctp_source_address_selection(inp,
5196 stcb, (sctp_route_t *) & net->ro,
5198 if (net->ro._s_addr == NULL)
5201 net->src_addr_selected = 1;
5204 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5205 stc.laddress[1] = 0;
5206 stc.laddress[2] = 0;
5207 stc.laddress[3] = 0;
5208 stc.laddr_type = SCTP_IPV4_ADDRESS;
5212 sin6 = (struct sockaddr_in6 *)to;
5213 memcpy(&stc.address, &sin6->sin6_addr,
5214 sizeof(struct in6_addr));
5215 stc.addr_type = SCTP_IPV6_ADDRESS;
5216 if (net->src_addr_selected == 0) {
5218 * strange case here, the INIT should have
5219 * did the selection.
5221 net->ro._s_addr = sctp_source_address_selection(inp,
5222 stcb, (sctp_route_t *) & net->ro,
5224 if (net->ro._s_addr == NULL)
5227 net->src_addr_selected = 1;
5229 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5230 sizeof(struct in6_addr));
5231 stc.laddr_type = SCTP_IPV6_ADDRESS;
5236 /* Now lets put the SCTP header in place */
5237 initackm_out = mtod(m, struct sctp_init_msg *);
5238 initackm_out->sh.src_port = inp->sctp_lport;
5239 initackm_out->sh.dest_port = sh->src_port;
5240 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
5241 /* Save it off for quick ref */
5242 stc.peers_vtag = init_chk->init.initiate_tag;
5243 initackm_out->sh.checksum = 0; /* calculate later */
5245 memcpy(stc.identification, SCTP_VERSION_STRING,
5246 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5247 /* now the chunk header */
5248 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
5249 initackm_out->msg.ch.chunk_flags = 0;
5250 /* fill in later from mbuf we build */
5251 initackm_out->msg.ch.chunk_length = 0;
5252 /* place in my tag */
5253 if ((asoc != NULL) &&
5254 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5255 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5256 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5257 /* re-use the v-tags and init-seq here */
5258 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
5259 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
5261 uint32_t vtag, itsn;
5263 if (hold_inp_lock) {
5264 SCTP_INP_INCR_REF(inp);
5265 SCTP_INP_RUNLOCK(inp);
5268 atomic_add_int(&asoc->refcnt, 1);
5269 SCTP_TCB_UNLOCK(stcb);
5270 vtag = sctp_select_a_tag(inp, 1);
5271 initackm_out->msg.init.initiate_tag = htonl(vtag);
5272 /* get a TSN to use too */
5273 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5274 initackm_out->msg.init.initial_tsn = htonl(itsn);
5275 SCTP_TCB_LOCK(stcb);
5276 atomic_add_int(&asoc->refcnt, -1);
5278 vtag = sctp_select_a_tag(inp, 1);
5279 initackm_out->msg.init.initiate_tag = htonl(vtag);
5280 /* get a TSN to use too */
5281 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5283 if (hold_inp_lock) {
5284 SCTP_INP_RLOCK(inp);
5285 SCTP_INP_DECR_REF(inp);
5288 /* save away my tag to */
5289 stc.my_vtag = initackm_out->msg.init.initiate_tag;
5291 /* set up some of the credits. */
5292 so = inp->sctp_socket;
5294 /* memory problem */
5298 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5300 /* set what I want */
5301 his_limit = ntohs(init_chk->init.num_inbound_streams);
5302 /* choose what I want */
5304 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5305 i_want = asoc->streamoutcnt;
5307 i_want = inp->sctp_ep.pre_open_stream_count;
5310 i_want = inp->sctp_ep.pre_open_stream_count;
5312 if (his_limit < i_want) {
5313 /* I Want more :< */
5314 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
5316 /* I can have what I want :> */
5317 initackm_out->msg.init.num_outbound_streams = htons(i_want);
5319 /* tell him his limt. */
5320 initackm_out->msg.init.num_inbound_streams =
5321 htons(inp->sctp_ep.max_open_streams_intome);
5322 /* setup the ECN pointer */
5324 if (inp->sctp_ep.adaptation_layer_indicator) {
5325 struct sctp_adaptation_layer_indication *ali;
5327 ali = (struct sctp_adaptation_layer_indication *)(
5328 (caddr_t)initackm_out + sizeof(*initackm_out));
5329 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5330 ali->ph.param_length = htons(sizeof(*ali));
5331 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5332 SCTP_BUF_LEN(m) += sizeof(*ali);
5333 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
5336 ecn = (struct sctp_ecn_supported_param *)(
5337 (caddr_t)initackm_out + sizeof(*initackm_out));
5341 if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5342 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5343 ecn->ph.param_length = htons(sizeof(*ecn));
5344 SCTP_BUF_LEN(m) += sizeof(*ecn);
5346 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5349 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5351 /* And now tell the peer we do pr-sctp */
5352 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5353 prsctp->ph.param_length = htons(sizeof(*prsctp));
5354 SCTP_BUF_LEN(m) += sizeof(*prsctp);
5356 /* And now tell the peer we do all the extensions */
5357 pr_supported = (struct sctp_supported_chunk_types_param *)
5358 ((caddr_t)prsctp + sizeof(*prsctp));
5360 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5362 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5363 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5364 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5365 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5366 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5367 if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5368 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5369 p_len = sizeof(*pr_supported) + num_ext;
5370 pr_supported->ph.param_length = htons(p_len);
5371 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5372 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5374 /* ECN nonce: And now tell the peer we support ECN nonce */
5375 if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5376 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5377 ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5378 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5379 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5380 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5382 /* add authentication parameters */
5383 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5384 struct sctp_auth_random *randp;
5385 struct sctp_auth_hmac_algo *hmacs;
5386 struct sctp_auth_chunk_list *chunks;
5387 uint16_t random_len;
5389 /* generate and add RANDOM parameter */
5390 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5391 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5392 randp->ph.param_type = htons(SCTP_RANDOM);
5393 p_len = sizeof(*randp) + random_len;
5394 randp->ph.param_length = htons(p_len);
5395 SCTP_READ_RANDOM(randp->random_data, random_len);
5396 /* zero out any padding required */
5397 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5398 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5400 /* add HMAC_ALGO parameter */
5401 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5402 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5403 (uint8_t *) hmacs->hmac_ids);
5405 p_len += sizeof(*hmacs);
5406 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5407 hmacs->ph.param_length = htons(p_len);
5408 /* zero out any padding required */
5409 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5410 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5412 /* add CHUNKS parameter */
5413 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5414 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5415 chunks->chunk_types);
5417 p_len += sizeof(*chunks);
5418 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5419 chunks->ph.param_length = htons(p_len);
5420 /* zero out any padding required */
5421 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5422 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5426 /* now the addresses */
5428 struct sctp_scoping scp;
5431 * To optimize this we could put the scoping stuff into a
5432 * structure and remove the individual uint8's from the stc
5433 * structure. Then we could just sifa in the address within
5434 * the stc.. but for now this is a quick hack to get the
5435 * address stuff teased apart.
5437 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5438 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5439 scp.loopback_scope = stc.loopback_scope;
5440 scp.ipv4_local_scope = stc.ipv4_scope;
5441 scp.local_scope = stc.local_scope;
5442 scp.site_scope = stc.site_scope;
5443 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5446 /* tack on the operational error if present */
5454 llen += SCTP_BUF_LEN(ol);
5455 ol = SCTP_BUF_NEXT(ol);
5458 /* must add a pad to the param */
5459 uint32_t cpthis = 0;
5462 padlen = 4 - (llen % 4);
5463 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5465 while (SCTP_BUF_NEXT(m_at) != NULL) {
5466 m_at = SCTP_BUF_NEXT(m_at);
5468 SCTP_BUF_NEXT(m_at) = op_err;
5469 while (SCTP_BUF_NEXT(m_at) != NULL) {
5470 m_at = SCTP_BUF_NEXT(m_at);
5473 /* pre-calulate the size and update pkt header and chunk header */
5475 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5476 p_len += SCTP_BUF_LEN(m_tmp);
5477 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5478 /* m_tmp should now point to last one */
5483 /* Now we must build a cookie */
5484 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5485 sizeof(struct sctphdr), &stc, &signature);
5486 if (m_cookie == NULL) {
5487 /* memory problem */
5491 /* Now append the cookie to the end and update the space/size */
5492 SCTP_BUF_NEXT(m_tmp) = m_cookie;
5494 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5495 p_len += SCTP_BUF_LEN(m_tmp);
5496 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5497 /* m_tmp should now point to last one */
5503 * Place in the size, but we don't include the last pad (if any) in
5506 initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5509 * Time to sign the cookie, we don't sign over the cookie signature
5510 * though thus we set trailer.
5512 (void)sctp_hmac_m(SCTP_HMAC,
5513 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5514 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5515 (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5517 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5518 * here since the timer will drive a retranmission.
5521 if ((padval) && (mp_last)) {
5522 /* see my previous comments on mp_last */
5525 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5527 /* Houston we have a problem, no space */
5533 if (stc.loopback_scope) {
5534 over_addr = &store1;
5540 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5541 NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
5542 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5547 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5548 struct sctp_association *asoc,
5549 struct sctp_stream_out *strq, int holds_lock)
5551 struct sctp_stream_out *stre, *strn;
5553 if (holds_lock == 0) {
5554 SCTP_TCB_SEND_LOCK(stcb);
5556 if ((strq->next_spoke.tqe_next) ||
5557 (strq->next_spoke.tqe_prev)) {
5558 /* already on wheel */
5561 stre = TAILQ_FIRST(&asoc->out_wheel);
5563 /* only one on wheel */
5564 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5567 for (; stre; stre = strn) {
5568 strn = TAILQ_NEXT(stre, next_spoke);
5569 if (stre->stream_no > strq->stream_no) {
5570 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5572 } else if (stre->stream_no == strq->stream_no) {
5573 /* huh, should not happen */
5575 } else if (strn == NULL) {
5576 /* next one is null */
5577 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5582 if (holds_lock == 0) {
5583 SCTP_TCB_SEND_UNLOCK(stcb);
5588 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5589 struct sctp_association *asoc,
5590 struct sctp_stream_out *strq)
5592 /* take off and then setup so we know it is not on the wheel */
5593 SCTP_TCB_SEND_LOCK(stcb);
5594 if (TAILQ_FIRST(&strq->outqueue)) {
5595 /* more was added */
5596 SCTP_TCB_SEND_UNLOCK(stcb);
5599 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5600 strq->next_spoke.tqe_next = NULL;
5601 strq->next_spoke.tqe_prev = NULL;
5602 SCTP_TCB_SEND_UNLOCK(stcb);
5606 sctp_prune_prsctp(struct sctp_tcb *stcb,
5607 struct sctp_association *asoc,
5608 struct sctp_sndrcvinfo *srcv,
5612 struct sctp_tmit_chunk *chk, *nchk;
5614 SCTP_TCB_LOCK_ASSERT(stcb);
5615 if ((asoc->peer_supports_prsctp) &&
5616 (asoc->sent_queue_cnt_removeable > 0)) {
5617 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5619 * Look for chunks marked with the PR_SCTP flag AND
5620 * the buffer space flag. If the one being sent is
5621 * equal or greater priority then purge the old one
5622 * and free some space.
5624 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5626 * This one is PR-SCTP AND buffer space
5629 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5631 * Lower numbers equates to higher
5632 * priority so if the one we are
5633 * looking at has a larger or equal
5634 * priority we want to drop the data
5635 * and NOT retransmit it.
5639 * We release the book_size
5640 * if the mbuf is here
5645 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5646 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5648 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5649 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5651 &asoc->sent_queue, SCTP_SO_LOCKED);
5652 freed_spc += ret_spc;
5653 if (freed_spc >= dataout) {
5656 } /* if chunk was present */
5657 } /* if of sufficent priority */
5658 } /* if chunk has enabled */
5659 } /* tailqforeach */
5661 chk = TAILQ_FIRST(&asoc->send_queue);
5663 nchk = TAILQ_NEXT(chk, sctp_next);
5664 /* Here we must move to the sent queue and mark */
5665 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5666 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5669 * We release the book_size
5670 * if the mbuf is here
5674 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5675 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5676 &asoc->send_queue, SCTP_SO_LOCKED);
5678 freed_spc += ret_spc;
5679 if (freed_spc >= dataout) {
5682 } /* end if chk->data */
5683 } /* end if right class */
5684 } /* end if chk pr-sctp */
5686 } /* end while (chk) */
5687 } /* if enabled in asoc */
5691 sctp_get_frag_point(struct sctp_tcb *stcb,
5692 struct sctp_association *asoc)
5697 * For endpoints that have both v6 and v4 addresses we must reserve
5698 * room for the ipv6 header, for those that are only dealing with V4
5699 * we use a larger frag point.
5701 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5702 ovh = SCTP_MED_OVERHEAD;
5704 ovh = SCTP_MED_V4_OVERHEAD;
5707 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5708 siz = asoc->smallest_mtu - ovh;
5710 siz = (stcb->asoc.sctp_frag_point - ovh);
5712 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5714 /* A data chunk MUST fit in a cluster */
5715 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5718 /* adjust for an AUTH chunk if DATA requires auth */
5719 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5720 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5723 /* make it an even word boundary please */
5730 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5731 struct sctp_stream_queue_pending *sp)
5734 if (stcb->asoc.peer_supports_prsctp) {
5736 * We assume that the user wants PR_SCTP_TTL if the user
5737 * provides a positive lifetime but does not specify any
5738 * PR_SCTP policy. This is a BAD assumption and causes
5739 * problems at least with the U-Vancovers MPI folks. I will
5740 * change this to be no policy means NO PR-SCTP.
5742 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5743 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5748 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5749 case CHUNK_FLAGS_PR_SCTP_BUF:
5751 * Time to live is a priority stored in tv_sec when
5752 * doing the buffer drop thing.
5754 sp->ts.tv_sec = sp->timetolive;
5757 case CHUNK_FLAGS_PR_SCTP_TTL:
5761 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5762 tv.tv_sec = sp->timetolive / 1000;
5763 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5765 * TODO sctp_constants.h needs alternative
5766 * time macros when _KERNEL is undefined.
5768 timevaladd(&sp->ts, &tv);
5771 case CHUNK_FLAGS_PR_SCTP_RTX:
5773 * Time to live is a the number or retransmissions
5776 sp->ts.tv_sec = sp->timetolive;
5780 SCTPDBG(SCTP_DEBUG_USRREQ1,
5781 "Unknown PR_SCTP policy %u.\n",
5782 PR_SCTP_POLICY(sp->sinfo_flags));
5789 sctp_msg_append(struct sctp_tcb *stcb,
5790 struct sctp_nets *net,
5792 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5794 int error = 0, holds_lock;
5796 struct sctp_stream_queue_pending *sp = NULL;
5797 struct sctp_stream_out *strm;
5800 * Given an mbuf chain, put it into the association send queue and
5801 * place it on the wheel
5803 holds_lock = hold_stcb_lock;
5804 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5805 /* Invalid stream number */
5806 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5810 if ((stcb->asoc.stream_locked) &&
5811 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5812 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5816 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5817 /* Now can we send this? */
5818 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5819 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5820 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5821 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5822 /* got data while shutting down */
5823 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5827 sctp_alloc_a_strmoq(stcb, sp);
5829 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5833 sp->sinfo_flags = srcv->sinfo_flags;
5834 sp->timetolive = srcv->sinfo_timetolive;
5835 sp->ppid = srcv->sinfo_ppid;
5836 sp->context = srcv->sinfo_context;
5838 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5842 sp->net = stcb->asoc.primary_destination;
5845 atomic_add_int(&sp->net->ref_count, 1);
5846 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5847 sp->stream = srcv->sinfo_stream;
5848 sp->msg_is_complete = 1;
5849 sp->sender_all_done = 1;
5852 sp->tail_mbuf = NULL;
5855 sctp_set_prsctp_policy(stcb, sp);
5857 * We could in theory (for sendall) sifa the length in, but we would
5858 * still have to hunt through the chain since we need to setup the
5862 if (SCTP_BUF_NEXT(at) == NULL)
5864 sp->length += SCTP_BUF_LEN(at);
5865 at = SCTP_BUF_NEXT(at);
5867 SCTP_TCB_SEND_LOCK(stcb);
5868 sctp_snd_sb_alloc(stcb, sp->length);
5869 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5870 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5871 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5872 sp->strseq = strm->next_sequence_sent;
5873 strm->next_sequence_sent++;
5875 if ((strm->next_spoke.tqe_next == NULL) &&
5876 (strm->next_spoke.tqe_prev == NULL)) {
5877 /* Not on wheel, insert */
5878 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5881 SCTP_TCB_SEND_UNLOCK(stcb);
5890 static struct mbuf *
5891 sctp_copy_mbufchain(struct mbuf *clonechain,
5892 struct mbuf *outchain,
5893 struct mbuf **endofchain,
5896 uint8_t copy_by_ref)
5899 struct mbuf *appendchain;
5903 if (endofchain == NULL) {
5907 sctp_m_freem(outchain);
5910 if (can_take_mbuf) {
5911 appendchain = clonechain;
5914 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5916 /* Its not in a cluster */
5917 if (*endofchain == NULL) {
5918 /* lets get a mbuf cluster */
5919 if (outchain == NULL) {
5920 /* This is the general case */
5922 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5923 if (outchain == NULL) {
5926 SCTP_BUF_LEN(outchain) = 0;
5927 *endofchain = outchain;
5928 /* get the prepend space */
5929 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5932 * We really should not get a NULL
5938 if (SCTP_BUF_NEXT(m) == NULL) {
5942 m = SCTP_BUF_NEXT(m);
5945 if (*endofchain == NULL) {
5947 * huh, TSNH XXX maybe we
5950 sctp_m_freem(outchain);
5954 /* get the new end of length */
5955 len = M_TRAILINGSPACE(*endofchain);
5957 /* how much is left at the end? */
5958 len = M_TRAILINGSPACE(*endofchain);
5960 /* Find the end of the data, for appending */
5961 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5963 /* Now lets copy it out */
5964 if (len >= sizeofcpy) {
5965 /* It all fits, copy it in */
5966 m_copydata(clonechain, 0, sizeofcpy, cp);
5967 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5969 /* fill up the end of the chain */
5971 m_copydata(clonechain, 0, len, cp);
5972 SCTP_BUF_LEN((*endofchain)) += len;
5973 /* now we need another one */
5976 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5981 SCTP_BUF_NEXT((*endofchain)) = m;
5983 cp = mtod((*endofchain), caddr_t);
5984 m_copydata(clonechain, len, sizeofcpy, cp);
5985 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5989 /* copy the old fashion way */
5990 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5991 #ifdef SCTP_MBUF_LOGGING
5992 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5997 if (SCTP_BUF_IS_EXTENDED(mat)) {
5998 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6000 mat = SCTP_BUF_NEXT(mat);
6006 if (appendchain == NULL) {
6009 sctp_m_freem(outchain);
6013 /* tack on to the end */
6014 if (*endofchain != NULL) {
6015 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6019 if (SCTP_BUF_NEXT(m) == NULL) {
6020 SCTP_BUF_NEXT(m) = appendchain;
6023 m = SCTP_BUF_NEXT(m);
6027 * save off the end and update the end-chain postion
6031 if (SCTP_BUF_NEXT(m) == NULL) {
6035 m = SCTP_BUF_NEXT(m);
6039 /* save off the end and update the end-chain postion */
6042 if (SCTP_BUF_NEXT(m) == NULL) {
6046 m = SCTP_BUF_NEXT(m);
6048 return (appendchain);
6053 sctp_med_chunk_output(struct sctp_inpcb *inp,
6054 struct sctp_tcb *stcb,
6055 struct sctp_association *asoc,
6058 int control_only, int *cwnd_full, int from_where,
6059 struct timeval *now, int *now_filled, int frag_point, int so_locked
6060 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6066 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6069 struct sctp_copy_all *ca;
6072 int added_control = 0;
6073 int un_sent, do_chunk_output = 1;
6074 struct sctp_association *asoc;
6076 ca = (struct sctp_copy_all *)ptr;
6077 if (ca->m == NULL) {
6080 if (ca->inp != inp) {
6084 if ((ca->m) && ca->sndlen) {
6085 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6087 /* can't copy so we are done */
6091 #ifdef SCTP_MBUF_LOGGING
6092 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6097 if (SCTP_BUF_IS_EXTENDED(mat)) {
6098 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6100 mat = SCTP_BUF_NEXT(mat);
6107 SCTP_TCB_LOCK_ASSERT(stcb);
6108 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6109 /* Abort this assoc with m as the user defined reason */
6111 struct sctp_paramhdr *ph;
6113 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6115 ph = mtod(m, struct sctp_paramhdr *);
6116 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6117 ph->param_length = htons(ca->sndlen);
6120 * We add one here to keep the assoc from
6121 * dis-appearing on us.
6123 atomic_add_int(&stcb->asoc.refcnt, 1);
6124 sctp_abort_an_association(inp, stcb,
6125 SCTP_RESPONSE_TO_USER_REQ,
6126 m, SCTP_SO_NOT_LOCKED);
6128 * sctp_abort_an_association calls sctp_free_asoc()
6129 * free association will NOT free it since we
6130 * incremented the refcnt .. we do this to prevent
6131 * it being freed and things getting tricky since we
6132 * could end up (from free_asoc) calling inpcb_free
6133 * which would get a recursive lock call to the
6134 * iterator lock.. But as a consequence of that the
6135 * stcb will return to us un-locked.. since
6136 * free_asoc returns with either no TCB or the TCB
6137 * unlocked, we must relock.. to unlock in the
6138 * iterator timer :-0
6140 SCTP_TCB_LOCK(stcb);
6141 atomic_add_int(&stcb->asoc.refcnt, -1);
6142 goto no_chunk_output;
6146 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6150 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6151 /* shutdown this assoc */
6154 cnt = sctp_is_there_unsent_data(stcb);
6156 if (TAILQ_EMPTY(&asoc->send_queue) &&
6157 TAILQ_EMPTY(&asoc->sent_queue) &&
6159 if (asoc->locked_on_sending) {
6163 * there is nothing queued to send, so I'm
6166 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6167 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6168 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6170 * only send SHUTDOWN the first time
6173 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6174 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6175 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6177 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6178 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6179 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6180 asoc->primary_destination);
6181 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6182 asoc->primary_destination);
6184 do_chunk_output = 0;
6188 * we still got (or just got) data to send,
6189 * so set SHUTDOWN_PENDING
6192 * XXX sockets draft says that SCTP_EOF
6193 * should be sent with no data. currently,
6194 * we will allow user data to be sent first
6195 * and move to SHUTDOWN-PENDING
6197 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6198 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6199 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6200 if (asoc->locked_on_sending) {
6202 * Locked to send out the
6205 struct sctp_stream_queue_pending *sp;
6207 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6209 if ((sp->length == 0) && (sp->msg_is_complete == 0))
6210 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6213 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6214 if (TAILQ_EMPTY(&asoc->send_queue) &&
6215 TAILQ_EMPTY(&asoc->sent_queue) &&
6216 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6218 atomic_add_int(&stcb->asoc.refcnt, 1);
6219 sctp_abort_an_association(stcb->sctp_ep, stcb,
6220 SCTP_RESPONSE_TO_USER_REQ,
6221 NULL, SCTP_SO_NOT_LOCKED);
6222 atomic_add_int(&stcb->asoc.refcnt, -1);
6223 goto no_chunk_output;
6225 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6226 asoc->primary_destination);
6232 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6233 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6235 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6236 (stcb->asoc.total_flight > 0) &&
6237 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6239 do_chunk_output = 0;
6241 if (do_chunk_output)
6242 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6243 else if (added_control) {
6244 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
6248 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6249 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6250 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6261 sctp_sendall_completes(void *ptr, uint32_t val)
6263 struct sctp_copy_all *ca;
6265 ca = (struct sctp_copy_all *)ptr;
6267 * Do a notify here? Kacheong suggests that the notify be done at
6268 * the send time.. so you would push up a notification if any send
6269 * failed. Don't know if this is feasable since the only failures we
6270 * have is "memory" related and if you cannot get an mbuf to send
6271 * the data you surely can't get an mbuf to send up to notify the
6272 * user you can't send the data :->
6275 /* now free everything */
6276 sctp_m_freem(ca->m);
6277 SCTP_FREE(ca, SCTP_M_COPYAL);
6281 #define MC_ALIGN(m, len) do { \
6282 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6287 static struct mbuf *
6288 sctp_copy_out_all(struct uio *uio, int len)
6290 struct mbuf *ret, *at;
6291 int left, willcpy, cancpy, error;
6293 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6299 SCTP_BUF_LEN(ret) = 0;
6300 /* save space for the data chunk header */
6301 cancpy = M_TRAILINGSPACE(ret);
6302 willcpy = min(cancpy, left);
6305 /* Align data to the end */
6306 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6312 SCTP_BUF_LEN(at) = willcpy;
6313 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6316 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6317 if (SCTP_BUF_NEXT(at) == NULL) {
6320 at = SCTP_BUF_NEXT(at);
6321 SCTP_BUF_LEN(at) = 0;
6322 cancpy = M_TRAILINGSPACE(at);
6323 willcpy = min(cancpy, left);
6330 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6331 struct sctp_sndrcvinfo *srcv)
6334 struct sctp_copy_all *ca;
6336 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6340 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6343 memset(ca, 0, sizeof(struct sctp_copy_all));
6346 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6348 * take off the sendall flag, it would be bad if we failed to do
6351 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6352 /* get length and mbuf chain */
6354 ca->sndlen = uio->uio_resid;
6355 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6356 if (ca->m == NULL) {
6357 SCTP_FREE(ca, SCTP_M_COPYAL);
6358 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6362 /* Gather the length of the send */
6368 ca->sndlen += SCTP_BUF_LEN(m);
6369 m = SCTP_BUF_NEXT(m);
6373 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6374 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6375 SCTP_ASOC_ANY_STATE,
6377 sctp_sendall_completes, inp, 1);
6379 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6380 SCTP_FREE(ca, SCTP_M_COPYAL);
6381 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6389 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6391 struct sctp_tmit_chunk *chk, *nchk;
6393 chk = TAILQ_FIRST(&asoc->control_send_queue);
6395 nchk = TAILQ_NEXT(chk, sctp_next);
6396 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6397 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6399 sctp_m_freem(chk->data);
6402 asoc->ctrl_queue_cnt--;
6403 sctp_free_a_chunk(stcb, chk);
6410 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6412 struct sctp_association *asoc;
6413 struct sctp_tmit_chunk *chk, *chk_tmp;
6414 struct sctp_asconf_chunk *acp;
6417 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6420 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6421 /* find SCTP_ASCONF chunk in queue */
6422 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6424 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6425 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6430 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6432 sctp_m_freem(chk->data);
6435 asoc->ctrl_queue_cnt--;
6436 sctp_free_a_chunk(stcb, chk);
6443 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6445 struct sctp_association *asoc,
6446 struct sctp_tmit_chunk **data_list,
6448 struct sctp_nets *net)
6451 struct sctp_tmit_chunk *tp1;
6453 for (i = 0; i < bundle_at; i++) {
6454 /* off of the send queue */
6457 * Any chunk NOT 0 you zap the time chunk 0 gets
6458 * zapped or set based on if a RTO measurment is
6461 data_list[i]->do_rtt = 0;
6464 data_list[i]->sent_rcv_time = net->last_sent_time;
6465 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6466 TAILQ_REMOVE(&asoc->send_queue,
6469 /* on to the sent queue */
6470 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6471 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6472 data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6473 struct sctp_tmit_chunk *tpp;
6475 /* need to move back */
6477 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6479 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6483 if (compare_with_wrap(tp1->rec.data.TSN_seq,
6484 data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6487 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6489 TAILQ_INSERT_TAIL(&asoc->sent_queue,
6494 /* This does not lower until the cum-ack passes it */
6495 asoc->sent_queue_cnt++;
6496 asoc->send_queue_cnt--;
6497 if ((asoc->peers_rwnd <= 0) &&
6498 (asoc->total_flight == 0) &&
6500 /* Mark the chunk as being a window probe */
6501 SCTP_STAT_INCR(sctps_windowprobed);
6503 #ifdef SCTP_AUDITING_ENABLED
6504 sctp_audit_log(0xC2, 3);
6506 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6507 data_list[i]->snd_count = 1;
6508 data_list[i]->rec.data.chunk_was_revoked = 0;
6509 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6510 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6511 data_list[i]->whoTo->flight_size,
6512 data_list[i]->book_size,
6513 (uintptr_t) data_list[i]->whoTo,
6514 data_list[i]->rec.data.TSN_seq);
6516 sctp_flight_size_increase(data_list[i]);
6517 sctp_total_flight_increase(stcb, data_list[i]);
6518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6519 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6520 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6522 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6523 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6524 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6525 /* SWS sender side engages */
6526 asoc->peers_rwnd = 0;
6532 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6534 struct sctp_tmit_chunk *chk, *nchk;
6536 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6538 nchk = TAILQ_NEXT(chk, sctp_next);
6539 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6540 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6541 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6542 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6543 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6544 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6545 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6546 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6547 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6548 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6549 /* Stray chunks must be cleaned up */
6551 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6553 sctp_m_freem(chk->data);
6556 asoc->ctrl_queue_cnt--;
6557 sctp_free_a_chunk(stcb, chk);
6558 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6559 /* special handling, we must look into the param */
6560 if (chk != asoc->str_reset) {
6561 goto clean_up_anyway;
6569 sctp_can_we_split_this(struct sctp_tcb *stcb,
6571 uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6574 * Make a decision on if I should split a msg into multiple parts.
6575 * This is only asked of incomplete messages.
6579 * If we are doing EEOR we need to always send it if its the
6580 * entire thing, since it might be all the guy is putting in
6583 if (goal_mtu >= length) {
6585 * If we have data outstanding,
6586 * we get another chance when the sack
6587 * arrives to transmit - wait for more data
6589 if (stcb->asoc.total_flight == 0) {
6591 * If nothing is in flight, we zero the
6599 /* You can fill the rest */
6604 * For those strange folk that make the send buffer
6605 * smaller than our fragmentation point, we can't
6606 * get a full msg in so we have to allow splitting.
6608 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6611 if ((length <= goal_mtu) ||
6612 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6613 /* Sub-optimial residual don't split in non-eeor mode. */
6617 * If we reach here length is larger than the goal_mtu. Do we wish
6618 * to split it for the sake of packet putting together?
6620 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6621 /* Its ok to split it */
6622 return (min(goal_mtu, frag_point));
6624 /* Nope, can't split */
6630 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6631 struct sctp_stream_out *strq,
6633 uint32_t frag_point,
6639 /* Move from the stream to the send_queue keeping track of the total */
6640 struct sctp_association *asoc;
6641 struct sctp_stream_queue_pending *sp;
6642 struct sctp_tmit_chunk *chk;
6643 struct sctp_data_chunk *dchkh;
6644 uint32_t to_move, length;
6645 uint8_t rcv_flags = 0;
6647 uint8_t send_lock_up = 0;
6649 SCTP_TCB_LOCK_ASSERT(stcb);
6652 /* sa_ignore FREED_MEMORY */
6653 sp = TAILQ_FIRST(&strq->outqueue);
6656 if (send_lock_up == 0) {
6657 SCTP_TCB_SEND_LOCK(stcb);
6660 sp = TAILQ_FIRST(&strq->outqueue);
6664 if (strq->last_msg_incomplete) {
6665 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6667 strq->last_msg_incomplete);
6668 strq->last_msg_incomplete = 0;
6672 SCTP_TCB_SEND_UNLOCK(stcb);
6677 if ((sp->msg_is_complete) && (sp->length == 0)) {
6678 if (sp->sender_all_done) {
6680 * We are doing differed cleanup. Last time through
6681 * when we took all the data the sender_all_done was
6684 if (sp->put_last_out == 0) {
6685 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6686 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6687 sp->sender_all_done,
6689 sp->msg_is_complete,
6693 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6694 SCTP_TCB_SEND_LOCK(stcb);
6697 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6698 TAILQ_REMOVE(&strq->outqueue, sp, next);
6699 sctp_free_remote_addr(sp->net);
6701 sctp_m_freem(sp->data);
6704 sctp_free_a_strmoq(stcb, sp);
6706 /* we can't be locked to it */
6708 stcb->asoc.locked_on_sending = NULL;
6710 SCTP_TCB_SEND_UNLOCK(stcb);
6713 /* back to get the next msg */
6717 * sender just finished this but still holds a
6726 /* is there some to get */
6727 if (sp->length == 0) {
6735 some_taken = sp->some_taken;
6736 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6737 sp->msg_is_complete = 1;
6740 length = sp->length;
6741 if (sp->msg_is_complete) {
6742 /* The message is complete */
6743 to_move = min(length, frag_point);
6744 if (to_move == length) {
6745 /* All of it fits in the MTU */
6746 if (sp->some_taken) {
6747 rcv_flags |= SCTP_DATA_LAST_FRAG;
6748 sp->put_last_out = 1;
6750 rcv_flags |= SCTP_DATA_NOT_FRAG;
6751 sp->put_last_out = 1;
6754 /* Not all of it fits, we fragment */
6755 if (sp->some_taken == 0) {
6756 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6761 to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6762 frag_point, eeor_mode);
6765 * We use a snapshot of length in case it
6766 * is expanding during the compare.
6771 if (to_move >= llen) {
6773 if (send_lock_up == 0) {
6775 * We are taking all of an incomplete msg
6776 * thus we need a send lock.
6778 SCTP_TCB_SEND_LOCK(stcb);
6780 if (sp->msg_is_complete) {
6782 * the sender finished the
6789 if (sp->some_taken == 0) {
6790 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6794 /* Nothing to take. */
6795 if (sp->some_taken) {
6804 /* If we reach here, we can copy out a chunk */
6805 sctp_alloc_a_chunk(stcb, chk);
6807 /* No chunk memory */
6813 * Setup for unordered if needed by looking at the user sent info
6816 if (sp->sinfo_flags & SCTP_UNORDERED) {
6817 rcv_flags |= SCTP_DATA_UNORDERED;
6819 /* clear out the chunk before setting up */
6820 memset(chk, 0, sizeof(*chk));
6821 chk->rec.data.rcv_flags = rcv_flags;
6823 if (to_move >= length) {
6824 /* we think we can steal the whole thing */
6825 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6826 SCTP_TCB_SEND_LOCK(stcb);
6829 if (to_move < sp->length) {
6830 /* bail, it changed */
6833 chk->data = sp->data;
6834 chk->last_mbuf = sp->tail_mbuf;
6835 /* register the stealing */
6836 sp->data = sp->tail_mbuf = NULL;
6841 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6842 chk->last_mbuf = NULL;
6843 if (chk->data == NULL) {
6844 sp->some_taken = some_taken;
6845 sctp_free_a_chunk(stcb, chk);
6850 #ifdef SCTP_MBUF_LOGGING
6851 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6856 if (SCTP_BUF_IS_EXTENDED(mat)) {
6857 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6859 mat = SCTP_BUF_NEXT(mat);
6863 /* Pull off the data */
6864 m_adj(sp->data, to_move);
6865 /* Now lets work our way down and compact it */
6867 while (m && (SCTP_BUF_LEN(m) == 0)) {
6868 sp->data = SCTP_BUF_NEXT(m);
6869 SCTP_BUF_NEXT(m) = NULL;
6870 if (sp->tail_mbuf == m) {
6872 * Freeing tail? TSNH since
6873 * we supposedly were taking less
6874 * than the sp->length.
6877 panic("Huh, freing tail? - TSNH");
6879 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6880 sp->tail_mbuf = sp->data = NULL;
6889 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6890 chk->copy_by_ref = 1;
6892 chk->copy_by_ref = 0;
6895 * get last_mbuf and counts of mb useage This is ugly but hopefully
6896 * its only one mbuf.
6898 if (chk->last_mbuf == NULL) {
6899 chk->last_mbuf = chk->data;
6900 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6901 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6904 if (to_move > length) {
6905 /*- This should not happen either
6906 * since we always lower to_move to the size
6907 * of sp->length if its larger.
6910 panic("Huh, how can to_move be larger?");
6912 SCTP_PRINTF("Huh, how can to_move be larger?\n");
6916 atomic_subtract_int(&sp->length, to_move);
6918 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6919 /* Not enough room for a chunk header, get some */
6922 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6925 * we're in trouble here. _PREPEND below will free
6926 * all the data if there is no leading space, so we
6927 * must put the data back and restore.
6929 if (send_lock_up == 0) {
6930 SCTP_TCB_SEND_LOCK(stcb);
6933 if (chk->data == NULL) {
6934 /* unsteal the data */
6935 sp->data = chk->data;
6936 sp->tail_mbuf = chk->last_mbuf;
6940 /* reassemble the data */
6942 sp->data = chk->data;
6943 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6945 sp->some_taken = some_taken;
6946 atomic_add_int(&sp->length, to_move);
6949 sctp_free_a_chunk(stcb, chk);
6953 SCTP_BUF_LEN(m) = 0;
6954 SCTP_BUF_NEXT(m) = chk->data;
6956 M_ALIGN(chk->data, 4);
6959 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6960 if (chk->data == NULL) {
6961 /* HELP, TSNH since we assured it would not above? */
6963 panic("prepend failes HELP?");
6965 SCTP_PRINTF("prepend fails HELP?\n");
6966 sctp_free_a_chunk(stcb, chk);
6972 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6973 chk->book_size = chk->send_size = (to_move +
6974 sizeof(struct sctp_data_chunk));
6975 chk->book_size_scale = 0;
6976 chk->sent = SCTP_DATAGRAM_UNSENT;
6979 chk->asoc = &stcb->asoc;
6980 chk->pad_inplace = 0;
6981 chk->no_fr_allowed = 0;
6982 chk->rec.data.stream_seq = sp->strseq;
6983 chk->rec.data.stream_number = sp->stream;
6984 chk->rec.data.payloadtype = sp->ppid;
6985 chk->rec.data.context = sp->context;
6986 chk->rec.data.doing_fast_retransmit = 0;
6987 chk->rec.data.ect_nonce = 0; /* ECN Nonce */
6989 chk->rec.data.timetodrop = sp->ts;
6990 chk->flags = sp->act_flags;
6991 chk->addr_over = sp->addr_over;
6994 atomic_add_int(&chk->whoTo->ref_count, 1);
6996 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6997 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
6998 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6999 (uintptr_t) stcb, sp->length,
7000 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7001 chk->rec.data.TSN_seq);
7003 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7005 * Put the rest of the things in place now. Size was done earlier in
7006 * previous loop prior to padding.
7009 #ifdef SCTP_ASOCLOG_OF_TSNS
7010 SCTP_TCB_LOCK_ASSERT(stcb);
7011 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7012 asoc->tsn_out_at = 0;
7013 asoc->tsn_out_wrapped = 1;
7015 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7016 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7017 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7018 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7019 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7020 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7021 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7022 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7026 dchkh->ch.chunk_type = SCTP_DATA;
7027 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7028 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7029 dchkh->dp.stream_id = htons(strq->stream_no);
7030 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7031 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7032 dchkh->ch.chunk_length = htons(chk->send_size);
7033 /* Now advance the chk->send_size by the actual pad needed. */
7034 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7039 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7040 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7041 chk->pad_inplace = 1;
7043 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7044 /* pad added an mbuf */
7045 chk->last_mbuf = lm;
7047 chk->send_size += pads;
7049 /* We only re-set the policy if it is on */
7050 if (sp->pr_sctp_on) {
7051 sctp_set_prsctp_policy(stcb, sp);
7052 asoc->pr_sctp_cnt++;
7053 chk->pr_sctp_on = 1;
7055 chk->pr_sctp_on = 0;
7057 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7058 /* All done pull and kill the message */
7059 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7060 if (sp->put_last_out == 0) {
7061 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7062 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7063 sp->sender_all_done,
7065 sp->msg_is_complete,
7069 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7070 SCTP_TCB_SEND_LOCK(stcb);
7073 TAILQ_REMOVE(&strq->outqueue, sp, next);
7074 sctp_free_remote_addr(sp->net);
7076 sctp_m_freem(sp->data);
7079 sctp_free_a_strmoq(stcb, sp);
7081 /* we can't be locked to it */
7083 stcb->asoc.locked_on_sending = NULL;
7085 /* more to go, we are locked */
7088 asoc->chunks_on_out_queue++;
7089 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7090 asoc->send_queue_cnt++;
7093 SCTP_TCB_SEND_UNLOCK(stcb);
7100 static struct sctp_stream_out *
7101 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7103 struct sctp_stream_out *strq;
7105 /* Find the next stream to use */
7106 if (asoc->last_out_stream == NULL) {
7107 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7108 if (asoc->last_out_stream == NULL) {
7109 /* huh nothing on the wheel, TSNH */
7114 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7117 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7119 /* Save off the last stream */
7120 asoc->last_out_stream = strq;
7127 sctp_fill_outqueue(struct sctp_tcb *stcb,
7128 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7130 struct sctp_association *asoc;
7131 struct sctp_stream_out *strq, *strqn, *strqt;
7132 int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7134 struct sctp_stream_queue_pending *sp;
7136 SCTP_TCB_LOCK_ASSERT(stcb);
7139 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7140 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7142 /* ?? not sure what else to do */
7143 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7146 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7148 /* Need an allowance for the data chunk header too */
7149 goal_mtu -= sizeof(struct sctp_data_chunk);
7151 /* must make even word boundary */
7152 goal_mtu &= 0xfffffffc;
7153 if (asoc->locked_on_sending) {
7154 /* We are stuck on one stream until the message completes. */
7155 strqn = strq = asoc->locked_on_sending;
7158 strqn = strq = sctp_select_a_stream(stcb, asoc);
7162 while ((goal_mtu > 0) && strq) {
7163 sp = TAILQ_FIRST(&strq->outqueue);
7165 * If CMT is off, we must validate that the stream in
7166 * question has the first item pointed towards are network
7167 * destionation requested by the caller. Note that if we
7168 * turn out to be locked to a stream (assigning TSN's then
7169 * we must stop, since we cannot look for another stream
7170 * with data to send to that destination). In CMT's case, by
7171 * skipping this check, we will send one data packet towards
7172 * the requested net.
7177 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7178 /* none for this network */
7182 strq = sctp_select_a_stream(stcb, asoc);
7186 if (strqn == strq) {
7187 /* I have circled */
7195 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7196 &giveup, eeor_mode, &bail);
7197 asoc->last_out_stream = strq;
7199 asoc->locked_on_sending = strq;
7200 if ((moved_how_much == 0) || (giveup) || bail)
7201 /* no more to move for now */
7204 asoc->locked_on_sending = NULL;
7205 strqt = sctp_select_a_stream(stcb, asoc);
7206 if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7207 if (strq == strqn) {
7208 /* Must move start to next one */
7209 strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7210 if (strqn == NULL) {
7211 strqn = TAILQ_FIRST(&asoc->out_wheel);
7212 if (strqn == NULL) {
7217 sctp_remove_from_wheel(stcb, asoc, strq);
7219 if ((giveup) || bail) {
7227 total_moved += moved_how_much;
7228 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7229 goal_mtu &= 0xfffffffc;
7234 if (total_moved == 0) {
7235 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7236 (net == stcb->asoc.primary_destination)) {
7237 /* ran dry for primary network net */
7238 SCTP_STAT_INCR(sctps_primary_randry);
7239 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7240 /* ran dry with CMT on */
7241 SCTP_STAT_INCR(sctps_cmt_randry);
7247 sctp_fix_ecn_echo(struct sctp_association *asoc)
7249 struct sctp_tmit_chunk *chk;
7251 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7252 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7253 chk->sent = SCTP_DATAGRAM_UNSENT;
7259 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7260 struct sctp_association *asoc,
7261 struct sctp_nets *net)
7263 struct sctp_tmit_chunk *chk;
7264 struct sctp_nets *a_net;
7266 SCTP_TCB_LOCK_ASSERT(stcb);
7268 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7269 * destination using the PF algorithm for finding alternate
7272 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7273 a_net = sctp_find_alternate_net(stcb, net, 2);
7275 a_net = sctp_find_alternate_net(stcb, net, 0);
7277 if ((a_net != net) &&
7278 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7280 * We only proceed if a valid alternate is found that is not
7281 * this one and is reachable. Here we must move all chunks
7282 * queued in the send queue off of the destination address
7285 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7286 if (chk->whoTo == net) {
7287 /* Move the chunk to our alternate */
7288 sctp_free_remote_addr(chk->whoTo);
7290 atomic_add_int(&a_net->ref_count, 1);
7297 sctp_med_chunk_output(struct sctp_inpcb *inp,
7298 struct sctp_tcb *stcb,
7299 struct sctp_association *asoc,
7302 int control_only, int *cwnd_full, int from_where,
7303 struct timeval *now, int *now_filled, int frag_point, int so_locked
7304 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7310 * Ok this is the generic chunk service queue. we must do the
7311 * following: - Service the stream queue that is next, moving any
7312 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7313 * LAST to the out queue in one pass) and assigning TSN's - Check to
7314 * see if the cwnd/rwnd allows any output, if so we go ahead and
7315 * fomulate and send the low level chunks. Making sure to combine
7316 * any control in the control chunk queue also.
7318 struct sctp_nets *net;
7319 struct mbuf *outchain, *endoutchain;
7320 struct sctp_tmit_chunk *chk, *nchk;
7321 struct sctphdr *shdr;
7323 /* temp arrays for unlinking */
7324 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7325 int no_fragmentflg, error;
7326 int one_chunk, hbflag, skip_data_for_this_net;
7327 int asconf, cookie, no_out_cnt;
7328 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
7329 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7330 struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
7332 uint32_t auth_offset = 0;
7333 struct sctp_auth_chunk *auth = NULL;
7336 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7345 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7346 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7347 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7352 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7354 * First lets prime the pump. For each destination, if there is room
7355 * in the flight size, attempt to pull an MTU's worth out of the
7356 * stream queues into the general send_queue
7358 #ifdef SCTP_AUDITING_ENABLED
7359 sctp_audit_log(0xC2, 2);
7361 SCTP_TCB_LOCK_ASSERT(stcb);
7363 if ((control_only) || (asoc->stream_reset_outstanding))
7368 /* Nothing to possible to send? */
7369 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7370 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7371 TAILQ_EMPTY(&asoc->send_queue) &&
7372 TAILQ_EMPTY(&asoc->out_wheel)) {
7376 if (asoc->peers_rwnd == 0) {
7377 /* No room in peers rwnd */
7380 if (asoc->total_flight > 0) {
7381 /* we are allowed one chunk in flight */
7385 if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7386 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7388 * for CMT we start at the next one past the one we
7389 * last added data to.
7391 if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7392 goto skip_the_fill_from_streams;
7394 if (asoc->last_net_data_came_from) {
7395 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7397 net = TAILQ_FIRST(&asoc->nets);
7401 net = TAILQ_FIRST(&asoc->nets);
7405 * JRI-TODO: CMT-MPI. Simply set the first
7406 * destination (net) to be optimized for the next
7407 * message to be pulled out of the outwheel. 1. peek
7408 * at outwheel 2. If large message, set net =
7409 * highest_cwnd 3. If small message, set net =
7413 net = asoc->primary_destination;
7416 net = TAILQ_FIRST(&asoc->nets);
7422 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7423 net->window_probe = 0;
7424 if (old_startat && (old_startat == net)) {
7428 * JRI: if dest is unreachable or unconfirmed, do
7429 * not send data to it
7431 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7435 * JRI: if dest is in PF state, do not send data to
7438 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
7439 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
7440 (net->dest_state & SCTP_ADDR_PF)) {
7443 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7444 /* nothing can be in queue for this guy */
7447 if (net->flight_size >= net->cwnd) {
7448 /* skip this network, no room */
7453 * JRI : this for loop we are in takes in each net,
7454 * if its's got space in cwnd and has data sent to
7455 * it (when CMT is off) then it calls
7456 * sctp_fill_outqueue for the net. This gets data on
7457 * the send queue for that network.
7459 * In sctp_fill_outqueue TSN's are assigned and data is
7460 * copied out of the stream buffers. Note mostly
7461 * copy by reference (we hope).
7463 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7464 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7466 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7468 /* memory alloc failure */
7470 goto skip_the_fill_from_streams;
7473 if (start_at != TAILQ_FIRST(&asoc->nets)) {
7474 /* got to pick up the beginning stuff. */
7475 old_startat = start_at;
7476 start_at = net = TAILQ_FIRST(&asoc->nets);
7481 skip_the_fill_from_streams:
7482 *cwnd_full = cwnd_full_ind;
7484 /* now service each destination and send out what we can for it */
7485 /* Nothing to send? */
7486 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7487 (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7488 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7492 if (no_data_chunks) {
7493 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7495 chk = TAILQ_FIRST(&asoc->control_send_queue);
7497 chk = TAILQ_FIRST(&asoc->send_queue);
7500 send_start_at = chk->whoTo;
7502 send_start_at = TAILQ_FIRST(&asoc->nets);
7505 again_one_more_time:
7506 for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7507 /* how much can we send? */
7508 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7509 if (old_startat && (old_startat == net)) {
7510 /* through list ocmpletely. */
7514 if (net->ref_count < 2) {
7516 * Ref-count of 1 so we cannot have data or control
7517 * queued to this address. Skip it.
7521 ctl_cnt = bundle_at = 0;
7522 endoutchain = outchain = NULL;
7525 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7526 skip_data_for_this_net = 1;
7528 skip_data_for_this_net = 0;
7530 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7532 * if we have a route and an ifp check to see if we
7533 * have room to send to this guy
7537 ifp = net->ro.ro_rt->rt_ifp;
7538 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7539 SCTP_STAT_INCR(sctps_ifnomemqueued);
7540 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7541 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7546 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7548 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7552 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7562 if (mtu > asoc->peers_rwnd) {
7563 if (asoc->total_flight > 0) {
7564 /* We have a packet in flight somewhere */
7565 r_mtu = asoc->peers_rwnd;
7567 /* We are always allowed to send one MTU out */
7574 /************************/
7575 /* ASCONF transmission */
7576 /************************/
7577 /* Now first lets go through the asconf queue */
7578 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7580 nchk = TAILQ_NEXT(chk, sctp_next);
7581 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7584 if (chk->whoTo != net) {
7586 * No, not sent to the network we are
7591 if (chk->data == NULL) {
7594 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7595 chk->sent != SCTP_DATAGRAM_RESEND) {
7599 * if no AUTH is yet included and this chunk
7600 * requires it, make sure to account for it. We
7601 * don't apply the size until the AUTH chunk is
7602 * actually added below in case there is no room for
7603 * this chunk. NOTE: we overload the use of "omtu"
7606 if ((auth == NULL) &&
7607 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7608 stcb->asoc.peer_auth_chunks)) {
7609 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7612 /* Here we do NOT factor the r_mtu */
7613 if ((chk->send_size < (int)(mtu - omtu)) ||
7614 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7616 * We probably should glom the mbuf chain
7617 * from the chk->data for control but the
7618 * problem is it becomes yet one more level
7619 * of tracking to do if for some reason
7620 * output fails. Then I have got to
7621 * reconstruct the merged control chain.. el
7622 * yucko.. for now we take the easy way and
7626 * Add an AUTH chunk, if chunk requires it
7627 * save the offset into the chain for AUTH
7629 if ((auth == NULL) &&
7630 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7631 stcb->asoc.peer_auth_chunks))) {
7632 outchain = sctp_add_auth_chunk(outchain,
7637 chk->rec.chunk_id.id);
7638 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7640 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7641 (int)chk->rec.chunk_id.can_take_data,
7642 chk->send_size, chk->copy_by_ref);
7643 if (outchain == NULL) {
7645 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7648 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7649 /* update our MTU size */
7650 if (mtu > (chk->send_size + omtu))
7651 mtu -= (chk->send_size + omtu);
7654 to_out += (chk->send_size + omtu);
7655 /* Do clear IP_DF ? */
7656 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7659 if (chk->rec.chunk_id.can_take_data)
7662 * set hb flag since we can use these for
7668 * should sysctl this: don't bundle data
7669 * with ASCONF since it requires AUTH
7672 chk->sent = SCTP_DATAGRAM_SENT;
7676 * Ok we are out of room but we can
7677 * output without effecting the
7678 * flight size since this little guy
7679 * is a control only packet.
7681 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7683 * do NOT clear the asconf flag as
7684 * it is used to do appropriate
7685 * source address selection.
7687 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7688 if (outchain == NULL) {
7690 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7695 shdr = mtod(outchain, struct sctphdr *);
7696 shdr->src_port = inp->sctp_lport;
7697 shdr->dest_port = stcb->rport;
7698 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7700 auth_offset += sizeof(struct sctphdr);
7701 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7702 (struct sockaddr *)&net->ro._l_addr,
7703 outchain, auth_offset, auth,
7704 no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7705 if (error == ENOBUFS) {
7706 asoc->ifp_had_enobuf = 1;
7707 SCTP_STAT_INCR(sctps_lowlevelerr);
7709 if (from_where == 0) {
7710 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7712 if (*now_filled == 0) {
7713 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7715 *now = net->last_sent_time;
7717 net->last_sent_time = *now;
7720 /* error, could not output */
7721 if (error == EHOSTUNREACH) {
7727 sctp_move_to_an_alt(stcb, asoc, net);
7732 asoc->ifp_had_enobuf = 0;
7733 if (*now_filled == 0) {
7734 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7736 *now = net->last_sent_time;
7738 net->last_sent_time = *now;
7742 * increase the number we sent, if a
7743 * cookie is sent we don't tell them
7746 outchain = endoutchain = NULL;
7750 *num_out += ctl_cnt;
7751 /* recalc a clean slate and setup */
7752 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7753 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7755 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7762 /************************/
7763 /* Control transmission */
7764 /************************/
7765 /* Now first lets go through the control queue */
7766 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7768 nchk = TAILQ_NEXT(chk, sctp_next);
7769 if (chk->whoTo != net) {
7771 * No, not sent to the network we are
7776 if (chk->data == NULL) {
7779 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7781 * It must be unsent. Cookies and ASCONF's
7782 * hang around but there timers will force
7783 * when marked for resend.
7788 * if no AUTH is yet included and this chunk
7789 * requires it, make sure to account for it. We
7790 * don't apply the size until the AUTH chunk is
7791 * actually added below in case there is no room for
7792 * this chunk. NOTE: we overload the use of "omtu"
7795 if ((auth == NULL) &&
7796 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7797 stcb->asoc.peer_auth_chunks)) {
7798 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7801 /* Here we do NOT factor the r_mtu */
7802 if ((chk->send_size < (int)(mtu - omtu)) ||
7803 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7805 * We probably should glom the mbuf chain
7806 * from the chk->data for control but the
7807 * problem is it becomes yet one more level
7808 * of tracking to do if for some reason
7809 * output fails. Then I have got to
7810 * reconstruct the merged control chain.. el
7811 * yucko.. for now we take the easy way and
7815 * Add an AUTH chunk, if chunk requires it
7816 * save the offset into the chain for AUTH
7818 if ((auth == NULL) &&
7819 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7820 stcb->asoc.peer_auth_chunks))) {
7821 outchain = sctp_add_auth_chunk(outchain,
7826 chk->rec.chunk_id.id);
7827 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7829 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7830 (int)chk->rec.chunk_id.can_take_data,
7831 chk->send_size, chk->copy_by_ref);
7832 if (outchain == NULL) {
7834 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7837 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7838 /* update our MTU size */
7839 if (mtu > (chk->send_size + omtu))
7840 mtu -= (chk->send_size + omtu);
7843 to_out += (chk->send_size + omtu);
7844 /* Do clear IP_DF ? */
7845 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7848 if (chk->rec.chunk_id.can_take_data)
7850 /* Mark things to be removed, if needed */
7851 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7852 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7853 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7854 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7855 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7856 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7857 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7858 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7859 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7860 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7862 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7865 * JRS 5/14/07 - Set the
7866 * flag to say a heartbeat
7871 /* remove these chunks at the end */
7872 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7873 /* turn off the timer */
7874 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7875 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7876 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7882 * Other chunks, since they have
7883 * timers running (i.e. COOKIE) we
7884 * just "trust" that it gets sent or
7888 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7892 chk->sent = SCTP_DATAGRAM_SENT;
7897 * Ok we are out of room but we can
7898 * output without effecting the
7899 * flight size since this little guy
7900 * is a control only packet.
7903 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7905 * do NOT clear the asconf
7906 * flag as it is used to do
7907 * appropriate source
7908 * address selection.
7912 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7915 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7916 if (outchain == NULL) {
7918 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7920 goto error_out_again;
7922 shdr = mtod(outchain, struct sctphdr *);
7923 shdr->src_port = inp->sctp_lport;
7924 shdr->dest_port = stcb->rport;
7925 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7927 auth_offset += sizeof(struct sctphdr);
7928 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7929 (struct sockaddr *)&net->ro._l_addr,
7930 outchain, auth_offset, auth,
7931 no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7932 if (error == ENOBUFS) {
7933 asoc->ifp_had_enobuf = 1;
7934 SCTP_STAT_INCR(sctps_lowlevelerr);
7936 if (from_where == 0) {
7937 SCTP_STAT_INCR(sctps_lowlevelerrusr);
7940 /* error, could not output */
7942 if (*now_filled == 0) {
7943 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7945 *now = net->last_sent_time;
7947 net->last_sent_time = *now;
7951 if (error == EHOSTUNREACH) {
7957 sctp_move_to_an_alt(stcb, asoc, net);
7962 asoc->ifp_had_enobuf = 0;
7963 /* Only HB or ASCONF advances time */
7965 if (*now_filled == 0) {
7966 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7968 *now = net->last_sent_time;
7970 net->last_sent_time = *now;
7975 * increase the number we sent, if a
7976 * cookie is sent we don't tell them
7979 outchain = endoutchain = NULL;
7983 *num_out += ctl_cnt;
7984 /* recalc a clean slate and setup */
7985 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7986 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7988 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7995 /*********************/
7996 /* Data transmission */
7997 /*********************/
7999 * if AUTH for DATA is required and no AUTH has been added
8000 * yet, account for this in the mtu now... if no data can be
8001 * bundled, this adjustment won't matter anyways since the
8002 * packet will be going out...
8004 if ((auth == NULL) &&
8005 sctp_auth_is_required_chunk(SCTP_DATA,
8006 stcb->asoc.peer_auth_chunks)) {
8007 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8009 /* now lets add any data within the MTU constraints */
8010 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8012 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8013 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8019 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8020 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8030 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
8032 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8033 if (no_data_chunks) {
8034 /* let only control go out */
8038 if (net->flight_size >= net->cwnd) {
8039 /* skip this net, no room for data */
8043 nchk = TAILQ_NEXT(chk, sctp_next);
8044 if (chk->whoTo != net) {
8045 /* No, not sent to this net */
8048 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8050 * strange, we have a chunk that is
8051 * to big for its destination and
8052 * yet no fragment ok flag.
8053 * Something went wrong when the
8054 * PMTU changed...we did not mark
8055 * this chunk for some reason?? I
8056 * will fix it here by letting IP
8057 * fragment it for now and printing
8058 * a warning. This really should not
8061 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8062 chk->send_size, mtu);
8063 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8065 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8066 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8067 /* ok we will add this one */
8070 * Add an AUTH chunk, if chunk
8071 * requires it, save the offset into
8072 * the chain for AUTH
8074 if ((auth == NULL) &&
8075 (sctp_auth_is_required_chunk(SCTP_DATA,
8076 stcb->asoc.peer_auth_chunks))) {
8078 outchain = sctp_add_auth_chunk(outchain,
8084 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8086 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8087 chk->send_size, chk->copy_by_ref);
8088 if (outchain == NULL) {
8089 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8090 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8091 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8094 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8097 /* upate our MTU size */
8098 /* Do clear IP_DF ? */
8099 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8102 /* unsigned subtraction of mtu */
8103 if (mtu > chk->send_size)
8104 mtu -= chk->send_size;
8107 /* unsigned subtraction of r_mtu */
8108 if (r_mtu > chk->send_size)
8109 r_mtu -= chk->send_size;
8113 to_out += chk->send_size;
8114 if ((to_out > mx_mtu) && no_fragmentflg) {
8116 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8118 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8122 chk->window_probe = 0;
8123 data_list[bundle_at++] = chk;
8124 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8128 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8129 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8130 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8132 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8134 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8135 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8145 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8147 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8149 data_list[0]->window_probe = 1;
8150 net->window_probe = 1;
8156 * Must be sent in order of the
8157 * TSN's (on a network)
8161 } /* for (chunk gather loop for this net) */
8162 } /* if asoc.state OPEN */
8163 /* Is there something to send for this destination? */
8165 /* We may need to start a control timer or two */
8167 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8170 * do NOT clear the asconf flag as it is
8171 * used to do appropriate source address
8176 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8179 /* must start a send timer if data is being sent */
8180 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8182 * no timer running on this destination
8185 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8186 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8187 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8189 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8190 (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8192 * JRS 5/14/07 - If a HB has been sent to a
8193 * PF destination and no T3 timer is
8194 * currently running, start the T3 timer to
8195 * track the HBs that were sent.
8197 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8199 /* Now send it, if there is anything to send :> */
8200 SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
8201 if (outchain == NULL) {
8203 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8207 shdr = mtod(outchain, struct sctphdr *);
8208 shdr->src_port = inp->sctp_lport;
8209 shdr->dest_port = stcb->rport;
8210 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8212 auth_offset += sizeof(struct sctphdr);
8213 if ((error = sctp_lowlevel_chunk_output(inp,
8216 (struct sockaddr *)&net->ro._l_addr,
8223 asconf, net->port, so_locked, NULL))) {
8224 /* error, we could not output */
8225 if (error == ENOBUFS) {
8226 SCTP_STAT_INCR(sctps_lowlevelerr);
8227 asoc->ifp_had_enobuf = 1;
8229 if (from_where == 0) {
8230 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8233 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8235 if (*now_filled == 0) {
8236 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8238 *now = net->last_sent_time;
8240 net->last_sent_time = *now;
8244 if (error == EHOSTUNREACH) {
8246 * Destination went unreachable
8249 sctp_move_to_an_alt(stcb, asoc, net);
8253 * I add this line to be paranoid. As far as
8254 * I can tell the continue, takes us back to
8255 * the top of the for, but just to make sure
8256 * I will reset these again here.
8258 ctl_cnt = bundle_at = 0;
8259 continue; /* This takes us back to the
8260 * for() for the nets. */
8262 asoc->ifp_had_enobuf = 0;
8264 outchain = endoutchain = NULL;
8267 if (bundle_at || hbflag) {
8268 /* For data/asconf and hb set time */
8269 if (*now_filled == 0) {
8270 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8272 *now = net->last_sent_time;
8274 net->last_sent_time = *now;
8278 *num_out += (ctl_cnt + bundle_at);
8281 /* setup for a RTO measurement */
8282 tsns_sent = data_list[0]->rec.data.TSN_seq;
8283 /* fill time if not already filled */
8284 if (*now_filled == 0) {
8285 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8287 *now = asoc->time_last_sent;
8289 asoc->time_last_sent = *now;
8291 data_list[0]->do_rtt = 1;
8292 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8293 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8294 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8295 if (net->flight_size < net->cwnd) {
8296 /* start or restart it */
8297 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8298 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8299 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8301 SCTP_STAT_INCR(sctps_earlyfrstrout);
8302 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8304 /* stop it if its running */
8305 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8306 SCTP_STAT_INCR(sctps_earlyfrstpout);
8307 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8308 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8317 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8318 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8321 if (old_startat == NULL) {
8322 old_startat = send_start_at;
8323 send_start_at = TAILQ_FIRST(&asoc->nets);
8325 goto again_one_more_time;
8328 * At the end there should be no NON timed chunks hanging on this
8331 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8332 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8334 if ((*num_out == 0) && (*reason_code == 0)) {
8339 sctp_clean_up_ctl(stcb, asoc);
8344 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8347 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8348 * the control chunk queue.
8350 struct sctp_chunkhdr *hdr;
8351 struct sctp_tmit_chunk *chk;
8354 SCTP_TCB_LOCK_ASSERT(stcb);
8355 sctp_alloc_a_chunk(stcb, chk);
8358 sctp_m_freem(op_err);
8361 chk->copy_by_ref = 0;
8362 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8363 if (op_err == NULL) {
8364 sctp_free_a_chunk(stcb, chk);
8369 while (mat != NULL) {
8370 chk->send_size += SCTP_BUF_LEN(mat);
8371 mat = SCTP_BUF_NEXT(mat);
8373 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8374 chk->rec.chunk_id.can_take_data = 1;
8375 chk->sent = SCTP_DATAGRAM_UNSENT;
8378 chk->asoc = &stcb->asoc;
8380 chk->whoTo = chk->asoc->primary_destination;
8381 atomic_add_int(&chk->whoTo->ref_count, 1);
8382 hdr = mtod(op_err, struct sctp_chunkhdr *);
8383 hdr->chunk_type = SCTP_OPERATION_ERROR;
8384 hdr->chunk_flags = 0;
8385 hdr->chunk_length = htons(chk->send_size);
8386 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8389 chk->asoc->ctrl_queue_cnt++;
8393 sctp_send_cookie_echo(struct mbuf *m,
8395 struct sctp_tcb *stcb,
8396 struct sctp_nets *net)
8399 * pull out the cookie and put it at the front of the control chunk
8403 struct mbuf *cookie;
8404 struct sctp_paramhdr parm, *phdr;
8405 struct sctp_chunkhdr *hdr;
8406 struct sctp_tmit_chunk *chk;
8407 uint16_t ptype, plen;
8409 /* First find the cookie in the param area */
8411 at = offset + sizeof(struct sctp_init_chunk);
8413 SCTP_TCB_LOCK_ASSERT(stcb);
8415 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8419 ptype = ntohs(phdr->param_type);
8420 plen = ntohs(phdr->param_length);
8421 if (ptype == SCTP_STATE_COOKIE) {
8424 /* found the cookie */
8425 if ((pad = (plen % 4))) {
8428 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8429 if (cookie == NULL) {
8433 #ifdef SCTP_MBUF_LOGGING
8434 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8439 if (SCTP_BUF_IS_EXTENDED(mat)) {
8440 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8442 mat = SCTP_BUF_NEXT(mat);
8448 at += SCTP_SIZE32(plen);
8450 if (cookie == NULL) {
8451 /* Did not find the cookie */
8454 /* ok, we got the cookie lets change it into a cookie echo chunk */
8456 /* first the change from param to cookie */
8457 hdr = mtod(cookie, struct sctp_chunkhdr *);
8458 hdr->chunk_type = SCTP_COOKIE_ECHO;
8459 hdr->chunk_flags = 0;
8460 /* get the chunk stuff now and place it in the FRONT of the queue */
8461 sctp_alloc_a_chunk(stcb, chk);
8464 sctp_m_freem(cookie);
8467 chk->copy_by_ref = 0;
8468 chk->send_size = plen;
8469 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8470 chk->rec.chunk_id.can_take_data = 0;
8471 chk->sent = SCTP_DATAGRAM_UNSENT;
8473 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8474 chk->asoc = &stcb->asoc;
8476 chk->whoTo = chk->asoc->primary_destination;
8477 atomic_add_int(&chk->whoTo->ref_count, 1);
8478 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8479 chk->asoc->ctrl_queue_cnt++;
8484 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8488 struct sctp_nets *net)
8491 * take a HB request and make it into a HB ack and send it.
8493 struct mbuf *outchain;
8494 struct sctp_chunkhdr *chdr;
8495 struct sctp_tmit_chunk *chk;
8499 /* must have a net pointer */
8502 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8503 if (outchain == NULL) {
8504 /* gak out of memory */
8507 #ifdef SCTP_MBUF_LOGGING
8508 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8513 if (SCTP_BUF_IS_EXTENDED(mat)) {
8514 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8516 mat = SCTP_BUF_NEXT(mat);
8520 chdr = mtod(outchain, struct sctp_chunkhdr *);
8521 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8522 chdr->chunk_flags = 0;
8523 if (chk_length % 4) {
8525 uint32_t cpthis = 0;
8528 padlen = 4 - (chk_length % 4);
8529 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8531 sctp_alloc_a_chunk(stcb, chk);
8534 sctp_m_freem(outchain);
8537 chk->copy_by_ref = 0;
8538 chk->send_size = chk_length;
8539 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8540 chk->rec.chunk_id.can_take_data = 1;
8541 chk->sent = SCTP_DATAGRAM_UNSENT;
8544 chk->asoc = &stcb->asoc;
8545 chk->data = outchain;
8547 atomic_add_int(&chk->whoTo->ref_count, 1);
8548 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8549 chk->asoc->ctrl_queue_cnt++;
8553 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8555 /* formulate and queue a cookie-ack back to sender */
8556 struct mbuf *cookie_ack;
8557 struct sctp_chunkhdr *hdr;
8558 struct sctp_tmit_chunk *chk;
8561 SCTP_TCB_LOCK_ASSERT(stcb);
8563 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8564 if (cookie_ack == NULL) {
8568 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8569 sctp_alloc_a_chunk(stcb, chk);
8572 sctp_m_freem(cookie_ack);
8575 chk->copy_by_ref = 0;
8576 chk->send_size = sizeof(struct sctp_chunkhdr);
8577 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8578 chk->rec.chunk_id.can_take_data = 1;
8579 chk->sent = SCTP_DATAGRAM_UNSENT;
8582 chk->asoc = &stcb->asoc;
8583 chk->data = cookie_ack;
8584 if (chk->asoc->last_control_chunk_from != NULL) {
8585 chk->whoTo = chk->asoc->last_control_chunk_from;
8587 chk->whoTo = chk->asoc->primary_destination;
8589 atomic_add_int(&chk->whoTo->ref_count, 1);
8590 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8591 hdr->chunk_type = SCTP_COOKIE_ACK;
8592 hdr->chunk_flags = 0;
8593 hdr->chunk_length = htons(chk->send_size);
8594 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8595 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8596 chk->asoc->ctrl_queue_cnt++;
8602 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8604 /* formulate and queue a SHUTDOWN-ACK back to the sender */
8605 struct mbuf *m_shutdown_ack;
8606 struct sctp_shutdown_ack_chunk *ack_cp;
8607 struct sctp_tmit_chunk *chk;
8609 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8610 if (m_shutdown_ack == NULL) {
8614 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8615 sctp_alloc_a_chunk(stcb, chk);
8618 sctp_m_freem(m_shutdown_ack);
8621 chk->copy_by_ref = 0;
8622 chk->send_size = sizeof(struct sctp_chunkhdr);
8623 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8624 chk->rec.chunk_id.can_take_data = 1;
8625 chk->sent = SCTP_DATAGRAM_UNSENT;
8628 chk->asoc = &stcb->asoc;
8629 chk->data = m_shutdown_ack;
8631 atomic_add_int(&net->ref_count, 1);
8633 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8634 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8635 ack_cp->ch.chunk_flags = 0;
8636 ack_cp->ch.chunk_length = htons(chk->send_size);
8637 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8638 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8639 chk->asoc->ctrl_queue_cnt++;
8644 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8646 /* formulate and queue a SHUTDOWN to the sender */
8647 struct mbuf *m_shutdown;
8648 struct sctp_shutdown_chunk *shutdown_cp;
8649 struct sctp_tmit_chunk *chk;
8651 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8652 if (m_shutdown == NULL) {
8656 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8657 sctp_alloc_a_chunk(stcb, chk);
8660 sctp_m_freem(m_shutdown);
8663 chk->copy_by_ref = 0;
8664 chk->send_size = sizeof(struct sctp_shutdown_chunk);
8665 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8666 chk->rec.chunk_id.can_take_data = 1;
8667 chk->sent = SCTP_DATAGRAM_UNSENT;
8670 chk->asoc = &stcb->asoc;
8671 chk->data = m_shutdown;
8673 atomic_add_int(&net->ref_count, 1);
8675 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8676 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8677 shutdown_cp->ch.chunk_flags = 0;
8678 shutdown_cp->ch.chunk_length = htons(chk->send_size);
8679 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8680 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8681 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8682 chk->asoc->ctrl_queue_cnt++;
8687 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8690 * formulate and queue an ASCONF to the peer. ASCONF parameters
8691 * should be queued on the assoc queue.
8693 struct sctp_tmit_chunk *chk;
8694 struct mbuf *m_asconf;
8697 SCTP_TCB_LOCK_ASSERT(stcb);
8699 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8700 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8701 /* can't send a new one if there is one in flight already */
8704 /* compose an ASCONF chunk, maximum length is PMTU */
8705 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8706 if (m_asconf == NULL) {
8709 sctp_alloc_a_chunk(stcb, chk);
8712 sctp_m_freem(m_asconf);
8715 chk->copy_by_ref = 0;
8716 chk->data = m_asconf;
8717 chk->send_size = len;
8718 chk->rec.chunk_id.id = SCTP_ASCONF;
8719 chk->rec.chunk_id.can_take_data = 0;
8720 chk->sent = SCTP_DATAGRAM_UNSENT;
8722 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8723 chk->asoc = &stcb->asoc;
8725 atomic_add_int(&chk->whoTo->ref_count, 1);
8726 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8727 chk->asoc->ctrl_queue_cnt++;
8732 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8735 * formulate and queue a asconf-ack back to sender. the asconf-ack
8736 * must be stored in the tcb.
8738 struct sctp_tmit_chunk *chk;
8739 struct sctp_asconf_ack *ack, *latest_ack;
8740 struct mbuf *m_ack, *m;
8741 struct sctp_nets *net = NULL;
8743 SCTP_TCB_LOCK_ASSERT(stcb);
8744 /* Get the latest ASCONF-ACK */
8745 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8746 if (latest_ack == NULL) {
8749 if (latest_ack->last_sent_to != NULL &&
8750 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8751 /* we're doing a retransmission */
8752 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8755 if (stcb->asoc.last_control_chunk_from == NULL)
8756 net = stcb->asoc.primary_destination;
8758 net = stcb->asoc.last_control_chunk_from;
8762 if (stcb->asoc.last_control_chunk_from == NULL)
8763 net = stcb->asoc.primary_destination;
8765 net = stcb->asoc.last_control_chunk_from;
8767 latest_ack->last_sent_to = net;
8769 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8770 if (ack->data == NULL) {
8773 /* copy the asconf_ack */
8774 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8775 if (m_ack == NULL) {
8776 /* couldn't copy it */
8779 #ifdef SCTP_MBUF_LOGGING
8780 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8785 if (SCTP_BUF_IS_EXTENDED(mat)) {
8786 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8788 mat = SCTP_BUF_NEXT(mat);
8793 sctp_alloc_a_chunk(stcb, chk);
8797 sctp_m_freem(m_ack);
8800 chk->copy_by_ref = 0;
8807 chk->send_size = ack->len;
8808 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8809 chk->rec.chunk_id.can_take_data = 1;
8810 chk->sent = SCTP_DATAGRAM_UNSENT;
8812 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */
8813 chk->asoc = &stcb->asoc;
8814 atomic_add_int(&chk->whoTo->ref_count, 1);
8816 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8817 chk->asoc->ctrl_queue_cnt++;
8824 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8825 struct sctp_tcb *stcb,
8826 struct sctp_association *asoc,
8827 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8828 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8834 * send out one MTU of retransmission. If fast_retransmit is
8835 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8836 * rwnd. For a Cookie or Asconf in the control chunk queue we
8837 * retransmit them by themselves.
8839 * For data chunks we will pick out the lowest TSN's in the sent_queue
8840 * marked for resend and bundle them all together (up to a MTU of
8841 * destination). The address to send to should have been
8842 * selected/changed where the retransmission was marked (i.e. in FR
8843 * or t3-timeout routines).
8845 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8846 struct sctp_tmit_chunk *chk, *fwd;
8847 struct mbuf *m, *endofchain;
8848 struct sctphdr *shdr;
8849 struct sctp_nets *net = NULL;
8850 uint32_t tsns_sent = 0;
8851 int no_fragmentflg, bundle_at, cnt_thru;
8853 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8854 struct sctp_auth_chunk *auth = NULL;
8855 uint32_t auth_offset = 0;
8858 SCTP_TCB_LOCK_ASSERT(stcb);
8859 tmr_started = ctl_cnt = bundle_at = error = 0;
8864 endofchain = m = NULL;
8865 #ifdef SCTP_AUDITING_ENABLED
8866 sctp_audit_log(0xC3, 1);
8868 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8869 (TAILQ_EMPTY(&asoc->control_send_queue))) {
8870 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8871 asoc->sent_queue_retran_cnt);
8872 asoc->sent_queue_cnt = 0;
8873 asoc->sent_queue_cnt_removeable = 0;
8874 /* send back 0/0 so we enter normal transmission */
8878 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8879 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8880 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8881 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8882 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8883 if (chk != asoc->str_reset) {
8885 * not eligible for retran if its
8892 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8897 * Add an AUTH chunk, if chunk requires it save the
8898 * offset into the chain for AUTH
8900 if ((auth == NULL) &&
8901 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8902 stcb->asoc.peer_auth_chunks))) {
8903 m = sctp_add_auth_chunk(m, &endofchain,
8904 &auth, &auth_offset,
8906 chk->rec.chunk_id.id);
8908 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8914 /* do we have control chunks to retransmit? */
8916 /* Start a timer no matter if we suceed or fail */
8917 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8918 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8919 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8920 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8922 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8924 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8927 shdr = mtod(m, struct sctphdr *);
8928 shdr->src_port = inp->sctp_lport;
8929 shdr->dest_port = stcb->rport;
8930 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8932 auth_offset += sizeof(struct sctphdr);
8933 chk->snd_count++; /* update our count */
8935 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8936 (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8937 auth, no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
8938 SCTP_STAT_INCR(sctps_lowlevelerr);
8941 m = endofchain = NULL;
8945 * We don't want to mark the net->sent time here since this
8946 * we use this for HB and retrans cannot measure RTT
8948 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8950 chk->sent = SCTP_DATAGRAM_SENT;
8951 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8955 /* Clean up the fwd-tsn list */
8956 sctp_clean_up_ctl(stcb, asoc);
8961 * Ok, it is just data retransmission we need to do or that and a
8962 * fwd-tsn with it all.
8964 if (TAILQ_EMPTY(&asoc->sent_queue)) {
8965 return (SCTP_RETRAN_DONE);
8967 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8968 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8969 /* not yet open, resend the cookie and that is it */
8972 #ifdef SCTP_AUDITING_ENABLED
8973 sctp_auditing(20, inp, stcb, NULL);
8975 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8976 if (chk->sent != SCTP_DATAGRAM_RESEND) {
8977 /* No, not sent to this net or not ready for rtx */
8980 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
8981 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
8982 /* Gak, we have exceeded max unlucky retran, abort! */
8983 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8985 SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
8986 atomic_add_int(&stcb->asoc.refcnt, 1);
8987 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8988 SCTP_TCB_LOCK(stcb);
8989 atomic_subtract_int(&stcb->asoc.refcnt, 1);
8990 return (SCTP_RETRAN_EXIT);
8992 /* pick up the net */
8994 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8995 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8997 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9000 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9001 /* No room in peers rwnd */
9004 tsn = asoc->last_acked_seq + 1;
9005 if (tsn == chk->rec.data.TSN_seq) {
9007 * we make a special exception for this
9008 * case. The peer has no rwnd but is missing
9009 * the lowest chunk.. which is probably what
9010 * is holding up the rwnd.
9012 goto one_chunk_around;
9017 if (asoc->peers_rwnd < mtu) {
9019 if ((asoc->peers_rwnd == 0) &&
9020 (asoc->total_flight == 0)) {
9021 chk->window_probe = 1;
9022 chk->whoTo->window_probe = 1;
9025 #ifdef SCTP_AUDITING_ENABLED
9026 sctp_audit_log(0xC3, 2);
9030 net->fast_retran_ip = 0;
9031 if (chk->rec.data.doing_fast_retransmit == 0) {
9033 * if no FR in progress skip destination that have
9034 * flight_size > cwnd.
9036 if (net->flight_size >= net->cwnd) {
9041 * Mark the destination net to have FR recovery
9045 net->fast_retran_ip = 1;
9049 * if no AUTH is yet included and this chunk requires it,
9050 * make sure to account for it. We don't apply the size
9051 * until the AUTH chunk is actually added below in case
9052 * there is no room for this chunk.
9054 if ((auth == NULL) &&
9055 sctp_auth_is_required_chunk(SCTP_DATA,
9056 stcb->asoc.peer_auth_chunks)) {
9057 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9061 if ((chk->send_size <= (mtu - dmtu)) ||
9062 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9063 /* ok we will add this one */
9064 if ((auth == NULL) &&
9065 (sctp_auth_is_required_chunk(SCTP_DATA,
9066 stcb->asoc.peer_auth_chunks))) {
9067 m = sctp_add_auth_chunk(m, &endofchain,
9068 &auth, &auth_offset,
9071 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9073 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9076 /* Do clear IP_DF ? */
9077 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9080 /* upate our MTU size */
9081 if (mtu > (chk->send_size + dmtu))
9082 mtu -= (chk->send_size + dmtu);
9085 data_list[bundle_at++] = chk;
9086 if (one_chunk && (asoc->total_flight <= 0)) {
9087 SCTP_STAT_INCR(sctps_windowprobed);
9090 if (one_chunk == 0) {
9092 * now are there anymore forward from chk to pick
9095 fwd = TAILQ_NEXT(chk, sctp_next);
9097 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9098 /* Nope, not for retran */
9099 fwd = TAILQ_NEXT(fwd, sctp_next);
9102 if (fwd->whoTo != net) {
9103 /* Nope, not the net in question */
9104 fwd = TAILQ_NEXT(fwd, sctp_next);
9107 if ((auth == NULL) &&
9108 sctp_auth_is_required_chunk(SCTP_DATA,
9109 stcb->asoc.peer_auth_chunks)) {
9110 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9113 if (fwd->send_size <= (mtu - dmtu)) {
9114 if ((auth == NULL) &&
9115 (sctp_auth_is_required_chunk(SCTP_DATA,
9116 stcb->asoc.peer_auth_chunks))) {
9117 m = sctp_add_auth_chunk(m,
9119 &auth, &auth_offset,
9123 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9125 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9128 /* Do clear IP_DF ? */
9129 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9132 /* upate our MTU size */
9133 if (mtu > (fwd->send_size + dmtu))
9134 mtu -= (fwd->send_size + dmtu);
9137 data_list[bundle_at++] = fwd;
9138 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9141 fwd = TAILQ_NEXT(fwd, sctp_next);
9143 /* can't fit so we are done */
9148 /* Is there something to send for this destination? */
9151 * No matter if we fail/or suceed we should start a
9152 * timer. A failure is like a lost IP packet :-)
9154 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9156 * no timer running on this destination
9159 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9162 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
9164 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9167 shdr = mtod(m, struct sctphdr *);
9168 shdr->src_port = inp->sctp_lport;
9169 shdr->dest_port = stcb->rport;
9170 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9172 auth_offset += sizeof(struct sctphdr);
9173 /* Now lets send it, if there is anything to send :> */
9174 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9175 (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
9176 auth, no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
9177 /* error, we could not output */
9178 SCTP_STAT_INCR(sctps_lowlevelerr);
9181 m = endofchain = NULL;
9186 * We don't want to mark the net->sent time here
9187 * since this we use this for HB and retrans cannot
9190 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9192 /* For auto-close */
9194 if (*now_filled == 0) {
9195 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9196 *now = asoc->time_last_sent;
9199 asoc->time_last_sent = *now;
9201 *cnt_out += bundle_at;
9202 #ifdef SCTP_AUDITING_ENABLED
9203 sctp_audit_log(0xC4, bundle_at);
9206 tsns_sent = data_list[0]->rec.data.TSN_seq;
9208 for (i = 0; i < bundle_at; i++) {
9209 SCTP_STAT_INCR(sctps_sendretransdata);
9210 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9212 * When we have a revoked data, and we
9213 * retransmit it, then we clear the revoked
9214 * flag since this flag dictates if we
9215 * subtracted from the fs
9217 if (data_list[i]->rec.data.chunk_was_revoked) {
9218 /* Deflate the cwnd */
9219 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9220 data_list[i]->rec.data.chunk_was_revoked = 0;
9222 data_list[i]->snd_count++;
9223 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9224 /* record the time */
9225 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9226 if (data_list[i]->book_size_scale) {
9228 * need to double the book size on
9231 data_list[i]->book_size_scale = 0;
9233 * Since we double the booksize, we
9234 * must also double the output queue
9235 * size, since this get shrunk when
9236 * we free by this amount.
9238 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9239 data_list[i]->book_size *= 2;
9243 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9244 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9245 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9247 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9248 (uint32_t) (data_list[i]->send_size +
9249 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9251 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9252 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9253 data_list[i]->whoTo->flight_size,
9254 data_list[i]->book_size,
9255 (uintptr_t) data_list[i]->whoTo,
9256 data_list[i]->rec.data.TSN_seq);
9258 sctp_flight_size_increase(data_list[i]);
9259 sctp_total_flight_increase(stcb, data_list[i]);
9260 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9261 /* SWS sender side engages */
9262 asoc->peers_rwnd = 0;
9265 (data_list[i]->rec.data.doing_fast_retransmit)) {
9266 SCTP_STAT_INCR(sctps_sendfastretrans);
9267 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9268 (tmr_started == 0)) {
9270 * ok we just fast-retrans'd
9271 * the lowest TSN, i.e the
9272 * first on the list. In
9273 * this case we want to give
9274 * some more time to get a
9275 * SACK back without a
9278 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9279 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9280 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9284 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9285 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9287 #ifdef SCTP_AUDITING_ENABLED
9288 sctp_auditing(21, inp, stcb, NULL);
9294 if (asoc->sent_queue_retran_cnt <= 0) {
9295 /* all done we have no more to retran */
9296 asoc->sent_queue_retran_cnt = 0;
9300 /* No more room in rwnd */
9303 /* stop the for loop here. we sent out a packet */
9311 sctp_timer_validation(struct sctp_inpcb *inp,
9312 struct sctp_tcb *stcb,
9313 struct sctp_association *asoc,
9316 struct sctp_nets *net;
9318 /* Validate that a timer is running somewhere */
9319 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9320 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9321 /* Here is a timer */
9325 SCTP_TCB_LOCK_ASSERT(stcb);
9326 /* Gak, we did not have a timer somewhere */
9327 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9328 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9333 sctp_chunk_output(struct sctp_inpcb *inp,
9334 struct sctp_tcb *stcb,
9337 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9343 * Ok this is the generic chunk service queue. we must do the
9345 * - See if there are retransmits pending, if so we must
9347 * - Service the stream queue that is next, moving any
9348 * message (note I must get a complete message i.e.
9349 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9351 * - Check to see if the cwnd/rwnd allows any output, if so we
9352 * go ahead and fomulate and send the low level chunks. Making sure
9353 * to combine any control in the control chunk queue also.
9355 struct sctp_association *asoc;
9356 struct sctp_nets *net;
9357 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9358 burst_cnt = 0, burst_limit = 0;
9363 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9365 int fr_done, tot_frs = 0;
9368 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9369 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9375 SCTP_TCB_LOCK_ASSERT(stcb);
9377 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9379 if ((un_sent <= 0) &&
9380 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9381 (asoc->sent_queue_retran_cnt == 0)) {
9382 /* Nothing to do unless there is something to be sent left */
9386 * Do we have something to send, data or control AND a sack timer
9387 * running, if so piggy-back the sack.
9389 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9390 sctp_send_sack(stcb);
9391 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9393 while (asoc->sent_queue_retran_cnt) {
9395 * Ok, it is retransmission time only, we send out only ONE
9396 * packet with a single call off to the retran code.
9398 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9400 * Special hook for handling cookiess discarded
9401 * by peer that carried data. Send cookie-ack only
9402 * and then the next call with get the retran's.
9404 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9405 &cwnd_full, from_where,
9406 &now, &now_filled, frag_point, so_locked);
9408 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9409 /* if its not from a HB then do it */
9411 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9417 * its from any other place, we don't allow retran
9418 * output (only control)
9423 /* Can't send anymore */
9425 * now lets push out control by calling med-level
9426 * output once. this assures that we WILL send HB's
9429 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9430 &cwnd_full, from_where,
9431 &now, &now_filled, frag_point, so_locked);
9432 #ifdef SCTP_AUDITING_ENABLED
9433 sctp_auditing(8, inp, stcb, NULL);
9435 (void)sctp_timer_validation(inp, stcb, asoc, ret);
9440 * The count was off.. retran is not happening so do
9441 * the normal retransmission.
9443 #ifdef SCTP_AUDITING_ENABLED
9444 sctp_auditing(9, inp, stcb, NULL);
9446 if (ret == SCTP_RETRAN_EXIT) {
9451 if (from_where == SCTP_OUTPUT_FROM_T3) {
9452 /* Only one transmission allowed out of a timeout */
9453 #ifdef SCTP_AUDITING_ENABLED
9454 sctp_auditing(10, inp, stcb, NULL);
9456 /* Push out any control */
9457 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
9458 &now, &now_filled, frag_point, so_locked);
9461 if (tot_frs > asoc->max_burst) {
9462 /* Hit FR burst limit */
9465 if ((num_out == 0) && (ret == 0)) {
9467 /* No more retrans to send */
9471 #ifdef SCTP_AUDITING_ENABLED
9472 sctp_auditing(12, inp, stcb, NULL);
9474 /* Check for bad destinations, if they exist move chunks around. */
9475 burst_limit = asoc->max_burst;
9476 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9477 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9478 SCTP_ADDR_NOT_REACHABLE) {
9480 * if possible move things off of this address we
9481 * still may send below due to the dormant state but
9482 * we try to find an alternate address to send to
9483 * and if we have one we move all queued data on the
9484 * out wheel to this alternate address.
9486 if (net->ref_count > 1)
9487 sctp_move_to_an_alt(stcb, asoc, net);
9488 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9489 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9490 ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9492 * JRS 5/14/07 - If CMT PF is on and the current
9493 * destination is in PF state, move all queued data
9494 * to an alternate desination.
9496 if (net->ref_count > 1)
9497 sctp_move_to_an_alt(stcb, asoc, net);
9500 * if ((asoc->sat_network) || (net->addr_is_local))
9501 * { burst_limit = asoc->max_burst *
9502 * SCTP_SAT_NETWORK_BURST_INCR; }
9504 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9505 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9507 * JRS - Use the congestion control
9508 * given in the congestion control
9511 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9512 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9513 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9515 SCTP_STAT_INCR(sctps_maxburstqueued);
9517 net->fast_retran_ip = 0;
9519 if (net->flight_size == 0) {
9520 /* Should be decaying the cwnd here */
9530 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9531 &reason_code, 0, &cwnd_full, from_where,
9532 &now, &now_filled, frag_point, so_locked);
9534 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9535 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9536 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9538 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9539 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9540 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9544 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9548 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9549 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9551 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9556 * When nagle is on, we look at how much is un_sent, then
9557 * if its smaller than an MTU and we have data in
9560 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9561 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9562 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9563 (stcb->asoc.total_flight > 0)) {
9567 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9568 TAILQ_EMPTY(&asoc->send_queue) &&
9569 TAILQ_EMPTY(&asoc->out_wheel)) {
9570 /* Nothing left to send */
9573 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9574 /* Nothing left to send */
9577 } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9578 (burst_cnt < burst_limit)));
9580 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9581 if (burst_cnt >= burst_limit) {
9582 SCTP_STAT_INCR(sctps_maxburstqueued);
9583 asoc->burst_limit_applied = 1;
9584 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9585 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9588 asoc->burst_limit_applied = 0;
9591 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9592 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9594 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9598 * Now we need to clean up the control chunk chain if a ECNE is on
9599 * it. It must be marked as UNSENT again so next call will continue
9600 * to send it until such time that we get a CWR, to remove it.
9602 if (stcb->asoc.ecn_echo_cnt_onq)
9603 sctp_fix_ecn_echo(asoc);
9609 sctp_output(inp, m, addr, control, p, flags)
9610 struct sctp_inpcb *inp;
9612 struct sockaddr *addr;
9613 struct mbuf *control;
9618 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9621 if (inp->sctp_socket == NULL) {
9622 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9625 return (sctp_sosend(inp->sctp_socket,
9635 send_forward_tsn(struct sctp_tcb *stcb,
9636 struct sctp_association *asoc)
9638 struct sctp_tmit_chunk *chk;
9639 struct sctp_forward_tsn_chunk *fwdtsn;
9641 SCTP_TCB_LOCK_ASSERT(stcb);
9642 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9643 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9644 /* mark it to unsent */
9645 chk->sent = SCTP_DATAGRAM_UNSENT;
9647 /* Do we correct its output location? */
9648 if (chk->whoTo != asoc->primary_destination) {
9649 sctp_free_remote_addr(chk->whoTo);
9650 chk->whoTo = asoc->primary_destination;
9651 atomic_add_int(&chk->whoTo->ref_count, 1);
9653 goto sctp_fill_in_rest;
9656 /* Ok if we reach here we must build one */
9657 sctp_alloc_a_chunk(stcb, chk);
9661 chk->copy_by_ref = 0;
9662 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9663 chk->rec.chunk_id.can_take_data = 0;
9667 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9668 if (chk->data == NULL) {
9669 sctp_free_a_chunk(stcb, chk);
9672 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9673 chk->sent = SCTP_DATAGRAM_UNSENT;
9675 chk->whoTo = asoc->primary_destination;
9676 atomic_add_int(&chk->whoTo->ref_count, 1);
9677 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9678 asoc->ctrl_queue_cnt++;
9681 * Here we go through and fill out the part that deals with
9682 * stream/seq of the ones we skip.
9684 SCTP_BUF_LEN(chk->data) = 0;
9686 struct sctp_tmit_chunk *at, *tp1, *last;
9687 struct sctp_strseq *strseq;
9688 unsigned int cnt_of_space, i, ovh;
9689 unsigned int space_needed;
9690 unsigned int cnt_of_skipped = 0;
9692 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9693 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9694 /* no more to look at */
9697 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9698 /* We don't report these */
9703 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9704 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9706 cnt_of_space = M_TRAILINGSPACE(chk->data);
9708 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9709 ovh = SCTP_MIN_OVERHEAD;
9711 ovh = SCTP_MIN_V4_OVERHEAD;
9713 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9714 /* trim to a mtu size */
9715 cnt_of_space = asoc->smallest_mtu - ovh;
9717 if (cnt_of_space < space_needed) {
9719 * ok we must trim down the chunk by lowering the
9720 * advance peer ack point.
9722 cnt_of_skipped = (cnt_of_space -
9723 ((sizeof(struct sctp_forward_tsn_chunk)) /
9724 sizeof(struct sctp_strseq)));
9726 * Go through and find the TSN that will be the one
9729 at = TAILQ_FIRST(&asoc->sent_queue);
9730 for (i = 0; i < cnt_of_skipped; i++) {
9731 tp1 = TAILQ_NEXT(at, sctp_next);
9736 * last now points to last one I can report, update
9739 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9740 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9742 chk->send_size = space_needed;
9743 /* Setup the chunk */
9744 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9745 fwdtsn->ch.chunk_length = htons(chk->send_size);
9746 fwdtsn->ch.chunk_flags = 0;
9747 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9748 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9749 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9750 (cnt_of_skipped * sizeof(struct sctp_strseq)));
9751 SCTP_BUF_LEN(chk->data) = chk->send_size;
9754 * Move pointer to after the fwdtsn and transfer to the
9757 strseq = (struct sctp_strseq *)fwdtsn;
9759 * Now populate the strseq list. This is done blindly
9760 * without pulling out duplicate stream info. This is
9761 * inefficent but won't harm the process since the peer will
9762 * look at these in sequence and will thus release anything.
9763 * It could mean we exceed the PMTU and chop off some that
9764 * we could have included.. but this is unlikely (aka 1432/4
9765 * would mean 300+ stream seq's would have to be reported in
9766 * one FWD-TSN. With a bit of work we can later FIX this to
9767 * optimize and pull out duplcates.. but it does add more
9768 * overhead. So for now... not!
9770 at = TAILQ_FIRST(&asoc->sent_queue);
9771 for (i = 0; i < cnt_of_skipped; i++) {
9772 tp1 = TAILQ_NEXT(at, sctp_next);
9773 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9774 /* We don't report these */
9779 strseq->stream = ntohs(at->rec.data.stream_number);
9780 strseq->sequence = ntohs(at->rec.data.stream_seq);
9790 sctp_send_sack(struct sctp_tcb *stcb)
9793 * Queue up a SACK in the control queue. We must first check to see
9794 * if a SACK is somehow on the control queue. If so, we will take
9795 * and and remove the old one.
9797 struct sctp_association *asoc;
9798 struct sctp_tmit_chunk *chk, *a_chk;
9799 struct sctp_sack_chunk *sack;
9800 struct sctp_gap_ack_block *gap_descriptor;
9801 struct sack_track *selector;
9806 int limit_reached = 0;
9807 unsigned int i, jstart, siz, j;
9808 unsigned int num_gap_blocks = 0, space;
9814 SCTP_TCB_LOCK_ASSERT(stcb);
9815 if (asoc->last_data_chunk_from == NULL) {
9816 /* Hmm we never received anything */
9819 sctp_set_rwnd(stcb, asoc);
9820 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9821 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9822 /* Hmm, found a sack already on queue, remove it */
9823 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9824 asoc->ctrl_queue_cnt++;
9827 sctp_m_freem(a_chk->data);
9830 sctp_free_remote_addr(a_chk->whoTo);
9831 a_chk->whoTo = NULL;
9835 if (a_chk == NULL) {
9836 sctp_alloc_a_chunk(stcb, a_chk);
9837 if (a_chk == NULL) {
9838 /* No memory so we drop the idea, and set a timer */
9839 if (stcb->asoc.delayed_ack) {
9840 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9841 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9842 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9843 stcb->sctp_ep, stcb, NULL);
9845 stcb->asoc.send_sack = 1;
9849 a_chk->copy_by_ref = 0;
9850 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9851 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9852 a_chk->rec.chunk_id.can_take_data = 1;
9854 /* Clear our pkt counts */
9855 asoc->data_pkts_seen = 0;
9858 a_chk->snd_count = 0;
9859 a_chk->send_size = 0; /* fill in later */
9860 a_chk->sent = SCTP_DATAGRAM_UNSENT;
9861 a_chk->whoTo = NULL;
9863 if ((asoc->numduptsns) ||
9864 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9867 * Ok, we have some duplicates or the destination for the
9868 * sack is unreachable, lets see if we can select an
9869 * alternate than asoc->last_data_chunk_from
9871 if ((!(asoc->last_data_chunk_from->dest_state &
9872 SCTP_ADDR_NOT_REACHABLE)) &&
9873 (asoc->used_alt_onsack > asoc->numnets)) {
9874 /* We used an alt last time, don't this time */
9875 a_chk->whoTo = NULL;
9877 asoc->used_alt_onsack++;
9878 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9880 if (a_chk->whoTo == NULL) {
9881 /* Nope, no alternate */
9882 a_chk->whoTo = asoc->last_data_chunk_from;
9883 asoc->used_alt_onsack = 0;
9887 * No duplicates so we use the last place we received data
9890 asoc->used_alt_onsack = 0;
9891 a_chk->whoTo = asoc->last_data_chunk_from;
9894 atomic_add_int(&a_chk->whoTo->ref_count, 1);
9896 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9898 space_req = sizeof(struct sctp_sack_chunk);
9900 /* gaps get a cluster */
9901 space_req = MCLBYTES;
9903 /* Ok now lets formulate a MBUF with our sack */
9904 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9905 if ((a_chk->data == NULL) ||
9906 (a_chk->whoTo == NULL)) {
9907 /* rats, no mbuf memory */
9909 /* was a problem with the destination */
9910 sctp_m_freem(a_chk->data);
9913 sctp_free_a_chunk(stcb, a_chk);
9914 /* sa_ignore NO_NULL_CHK */
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_6);
9918 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9919 stcb->sctp_ep, stcb, NULL);
9921 stcb->asoc.send_sack = 1;
9925 /* ok, lets go through and fill it in */
9926 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9927 space = M_TRAILINGSPACE(a_chk->data);
9928 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9929 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9931 limit = mtod(a_chk->data, caddr_t);
9934 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9935 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9936 /* 0x01 is used by nonce for ecn */
9937 if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
9938 (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
9939 (asoc->peer_supports_ecn_nonce))
9940 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9942 sack->ch.chunk_flags = 0;
9944 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
9946 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9947 * received, then set high bit to 1, else 0. Reset
9950 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9951 asoc->cmt_dac_pkts_rcvd = 0;
9953 #ifdef SCTP_ASOCLOG_OF_TSNS
9954 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9955 stcb->asoc.cumack_log_atsnt++;
9956 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9957 stcb->asoc.cumack_log_atsnt = 0;
9960 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9961 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9962 asoc->my_last_reported_rwnd = asoc->my_rwnd;
9964 /* reset the readers interpretation */
9965 stcb->freed_by_sorcv_sincelast = 0;
9967 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9969 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9970 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9973 * cum-ack behind the mapping array, so we start and use all
9978 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9980 * we skip the first one when the cum-ack is at or above the
9981 * mapping array base. Note this only works if
9985 if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9986 /* we have a gap .. maybe */
9987 for (i = 0; i < siz; i++) {
9988 selector = &sack_array[asoc->mapping_array[i]];
9989 if (mergeable && selector->right_edge) {
9991 * Backup, left and right edges were ok to
9997 if (selector->num_entries == 0)
10000 for (j = jstart; j < selector->num_entries; j++) {
10001 if (mergeable && selector->right_edge) {
10003 * do a merge by NOT setting
10009 * no merge, set the left
10013 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10015 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10018 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10024 if (selector->left_edge) {
10028 if (limit_reached) {
10029 /* Reached the limit stop */
10035 if (num_gap_blocks == 0) {
10037 * slide not yet happened, and somehow we got called
10038 * to send a sack. Cumack needs to move up.
10040 int abort_flag = 0;
10042 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10043 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10044 sctp_sack_check(stcb, 0, 0, &abort_flag);
10047 /* now we must add any dups we are going to report. */
10048 if ((limit_reached == 0) && (asoc->numduptsns)) {
10049 dup = (uint32_t *) gap_descriptor;
10050 for (i = 0; i < asoc->numduptsns; i++) {
10051 *dup = htonl(asoc->dup_tsns[i]);
10054 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10059 asoc->numduptsns = 0;
10062 * now that the chunk is prepared queue it to the control chunk
10065 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10066 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10067 (num_dups * sizeof(int32_t)));
10068 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10069 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10070 sack->sack.num_dup_tsns = htons(num_dups);
10071 sack->ch.chunk_length = htons(a_chk->send_size);
10072 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10073 asoc->ctrl_queue_cnt++;
10074 asoc->send_sack = 0;
10075 SCTP_STAT_INCR(sctps_sendsacks);
10081 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10082 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10087 struct mbuf *m_abort;
10088 struct mbuf *m_out = NULL, *m_end = NULL;
10089 struct sctp_abort_chunk *abort = NULL;
10091 uint32_t auth_offset = 0;
10092 struct sctp_auth_chunk *auth = NULL;
10093 struct sctphdr *shdr;
10096 * Add an AUTH chunk, if chunk requires it and save the offset into
10097 * the chain for AUTH
10099 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10100 stcb->asoc.peer_auth_chunks)) {
10101 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10102 stcb, SCTP_ABORT_ASSOCIATION);
10104 SCTP_TCB_LOCK_ASSERT(stcb);
10105 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10106 if (m_abort == NULL) {
10109 sctp_m_freem(m_out);
10112 /* link in any error */
10113 SCTP_BUF_NEXT(m_abort) = operr;
10120 sz += SCTP_BUF_LEN(n);
10121 n = SCTP_BUF_NEXT(n);
10124 SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10125 if (m_out == NULL) {
10126 /* NO Auth chunk prepended, so reserve space in front */
10127 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10130 /* Put AUTH chunk at the front of the chain */
10131 SCTP_BUF_NEXT(m_end) = m_abort;
10134 /* fill in the ABORT chunk */
10135 abort = mtod(m_abort, struct sctp_abort_chunk *);
10136 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10137 abort->ch.chunk_flags = 0;
10138 abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10140 /* prepend and fill in the SCTP header */
10141 SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
10142 if (m_out == NULL) {
10143 /* TSNH: no memory */
10146 shdr = mtod(m_out, struct sctphdr *);
10147 shdr->src_port = stcb->sctp_ep->sctp_lport;
10148 shdr->dest_port = stcb->rport;
10149 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10150 shdr->checksum = 0;
10151 auth_offset += sizeof(struct sctphdr);
10153 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10154 stcb->asoc.primary_destination,
10155 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10156 m_out, auth_offset, auth, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
10157 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10161 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10162 struct sctp_nets *net)
10164 /* formulate and SEND a SHUTDOWN-COMPLETE */
10165 struct mbuf *m_shutdown_comp;
10166 struct sctp_shutdown_complete_msg *comp_cp;
10168 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
10169 if (m_shutdown_comp == NULL) {
10173 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
10174 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10175 comp_cp->shut_cmp.ch.chunk_flags = 0;
10176 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10177 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
10178 comp_cp->sh.dest_port = stcb->rport;
10179 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
10180 comp_cp->sh.checksum = 0;
10182 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
10183 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10184 (struct sockaddr *)&net->ro._l_addr,
10185 m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
10186 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10191 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10192 uint32_t vrf_id, uint16_t port)
10194 /* formulate and SEND a SHUTDOWN-COMPLETE */
10195 struct mbuf *o_pak;
10197 struct ip *iph, *iph_out;
10198 struct udphdr *udp = NULL;
10201 struct ip6_hdr *ip6, *ip6_out;
10204 int offset_out, len, mlen;
10205 struct sctp_shutdown_complete_msg *comp_cp;
10207 iph = mtod(m, struct ip *);
10208 switch (iph->ip_v) {
10210 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10213 case IPV6_VERSION >> 4:
10214 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10221 len += sizeof(struct udphdr);
10223 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10224 if (mout == NULL) {
10227 SCTP_BUF_LEN(mout) = len;
10228 SCTP_BUF_NEXT(mout) = NULL;
10235 switch (iph->ip_v) {
10237 iph_out = mtod(mout, struct ip *);
10239 /* Fill in the IP header for the ABORT */
10240 iph_out->ip_v = IPVERSION;
10241 iph_out->ip_hl = (sizeof(struct ip) / 4);
10242 iph_out->ip_tos = (u_char)0;
10243 iph_out->ip_id = 0;
10244 iph_out->ip_off = 0;
10245 iph_out->ip_ttl = MAXTTL;
10247 iph_out->ip_p = IPPROTO_UDP;
10249 iph_out->ip_p = IPPROTO_SCTP;
10251 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10252 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10254 /* let IP layer calculate this */
10255 iph_out->ip_sum = 0;
10256 offset_out += sizeof(*iph_out);
10257 comp_cp = (struct sctp_shutdown_complete_msg *)(
10258 (caddr_t)iph_out + offset_out);
10261 case IPV6_VERSION >> 4:
10262 ip6 = (struct ip6_hdr *)iph;
10263 ip6_out = mtod(mout, struct ip6_hdr *);
10265 /* Fill in the IPv6 header for the ABORT */
10266 ip6_out->ip6_flow = ip6->ip6_flow;
10267 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
10269 ip6_out->ip6_nxt = IPPROTO_UDP;
10271 ip6_out->ip6_nxt = IPPROTO_SCTP;
10273 ip6_out->ip6_src = ip6->ip6_dst;
10274 ip6_out->ip6_dst = ip6->ip6_src;
10276 * ?? The old code had both the iph len + payload, I think
10277 * this is wrong and would never have worked
10279 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10280 offset_out += sizeof(*ip6_out);
10281 comp_cp = (struct sctp_shutdown_complete_msg *)(
10282 (caddr_t)ip6_out + offset_out);
10286 /* Currently not supported. */
10290 udp = (struct udphdr *)comp_cp;
10291 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10292 udp->uh_dport = port;
10293 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10294 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10295 offset_out += sizeof(struct udphdr);
10296 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10298 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10300 sctp_m_freem(mout);
10303 /* Now copy in and fill in the ABORT tags etc. */
10304 comp_cp->sh.src_port = sh->dest_port;
10305 comp_cp->sh.dest_port = sh->src_port;
10306 comp_cp->sh.checksum = 0;
10307 comp_cp->sh.v_tag = sh->v_tag;
10308 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10309 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10310 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10313 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
10314 if (iph_out != NULL) {
10317 struct sctp_tcb *stcb = NULL;
10319 mlen = SCTP_BUF_LEN(mout);
10320 bzero(&ro, sizeof ro);
10321 /* set IPv4 length */
10322 iph_out->ip_len = mlen;
10323 #ifdef SCTP_PACKET_LOGGING
10324 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10325 sctp_packet_log(mout, mlen);
10327 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10329 SCTP_ENABLE_UDP_CSUM(o_pak);
10332 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10334 /* Free the route if we got one back */
10339 if (ip6_out != NULL) {
10340 struct route_in6 ro;
10342 struct sctp_tcb *stcb = NULL;
10343 struct ifnet *ifp = NULL;
10345 bzero(&ro, sizeof(ro));
10346 mlen = SCTP_BUF_LEN(mout);
10347 #ifdef SCTP_PACKET_LOGGING
10348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10349 sctp_packet_log(mout, mlen);
10351 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10353 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10354 sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10355 udp->uh_sum = 0xffff;
10358 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10360 /* Free the route if we got one back */
10365 SCTP_STAT_INCR(sctps_sendpackets);
10366 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10367 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10372 static struct sctp_nets *
10373 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10375 struct sctp_nets *net, *hnet;
10376 int ms_goneby, highest_ms, state_overide = 0;
10378 (void)SCTP_GETTIME_TIMEVAL(now);
10381 SCTP_TCB_LOCK_ASSERT(stcb);
10382 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10384 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10385 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10388 * Skip this guy from consideration if HB is off AND
10393 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10394 /* skip this dest net from consideration */
10397 if (net->last_sent_time.tv_sec) {
10398 /* Sent to so we subtract */
10399 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10401 /* Never been sent to */
10402 ms_goneby = 0x7fffffff;
10404 * When the address state is unconfirmed but still
10405 * considered reachable, we HB at a higher rate. Once it
10406 * goes confirmed OR reaches the "unreachable" state, thenw
10407 * we cut it back to HB at a more normal pace.
10409 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10415 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10416 (ms_goneby > highest_ms)) {
10417 highest_ms = ms_goneby;
10422 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10428 if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10430 * Found the one with longest delay bounds OR it is
10431 * unconfirmed and still not marked unreachable.
10433 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10436 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10437 (struct sockaddr *)&hnet->ro._l_addr);
10439 SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10442 /* update the timer now */
10443 hnet->last_sent_time = *now;
10446 /* Nothing to HB */
10451 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10453 struct sctp_tmit_chunk *chk;
10454 struct sctp_nets *net;
10455 struct sctp_heartbeat_chunk *hb;
10456 struct timeval now;
10457 struct sockaddr_in *sin;
10458 struct sockaddr_in6 *sin6;
10460 SCTP_TCB_LOCK_ASSERT(stcb);
10461 if (user_req == 0) {
10462 net = sctp_select_hb_destination(stcb, &now);
10465 * All our busy none to send to, just start the
10468 if (stcb->asoc.state == 0) {
10471 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10482 (void)SCTP_GETTIME_TIMEVAL(&now);
10484 sin = (struct sockaddr_in *)&net->ro._l_addr;
10485 if (sin->sin_family != AF_INET) {
10486 if (sin->sin_family != AF_INET6) {
10491 sctp_alloc_a_chunk(stcb, chk);
10493 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10496 chk->copy_by_ref = 0;
10497 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10498 chk->rec.chunk_id.can_take_data = 1;
10499 chk->asoc = &stcb->asoc;
10500 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10502 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10503 if (chk->data == NULL) {
10504 sctp_free_a_chunk(stcb, chk);
10507 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10508 SCTP_BUF_LEN(chk->data) = chk->send_size;
10509 chk->sent = SCTP_DATAGRAM_UNSENT;
10510 chk->snd_count = 0;
10512 atomic_add_int(&chk->whoTo->ref_count, 1);
10513 /* Now we have a mbuf that we can fill in with the details */
10514 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10515 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10516 /* fill out chunk header */
10517 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10518 hb->ch.chunk_flags = 0;
10519 hb->ch.chunk_length = htons(chk->send_size);
10520 /* Fill out hb parameter */
10521 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10522 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10523 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10524 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10525 /* Did our user request this one, put it in */
10526 hb->heartbeat.hb_info.user_req = user_req;
10527 hb->heartbeat.hb_info.addr_family = sin->sin_family;
10528 hb->heartbeat.hb_info.addr_len = sin->sin_len;
10529 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10531 * we only take from the entropy pool if the address is not
10534 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10535 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10537 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10538 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10540 if (sin->sin_family == AF_INET) {
10541 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10542 } else if (sin->sin_family == AF_INET6) {
10543 /* We leave the scope the way it is in our lookup table. */
10544 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10545 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10547 /* huh compiler bug */
10552 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10553 * PF-heartbeats. Because of this, threshold management is done by
10554 * the t3 timer handler, and does not need to be done upon the send
10555 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10556 * heartbeat is being sent is in PF state, do NOT do threshold
10559 if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10560 /* ok we have a destination that needs a beat */
10561 /* lets do the theshold management Qiaobing style */
10562 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10563 stcb->asoc.max_send_times)) {
10565 * we have lost the association, in a way this is
10566 * quite bad since we really are one less time since
10567 * we really did not send yet. This is the down side
10568 * to the Q's style as defined in the RFC and not my
10569 * alternate style defined in the RFC.
10571 if (chk->data != NULL) {
10572 sctp_m_freem(chk->data);
10576 * Here we do NOT use the macro since the
10577 * association is now gone.
10580 sctp_free_remote_addr(chk->whoTo);
10583 sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10587 net->hb_responded = 0;
10588 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10589 stcb->asoc.ctrl_queue_cnt++;
10590 SCTP_STAT_INCR(sctps_sendheartbeat);
10592 * Call directly med level routine to put out the chunk. It will
10593 * always tumble out control chunks aka HB but it may even tumble
10600 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10603 struct sctp_association *asoc;
10604 struct sctp_ecne_chunk *ecne;
10605 struct sctp_tmit_chunk *chk;
10607 asoc = &stcb->asoc;
10608 SCTP_TCB_LOCK_ASSERT(stcb);
10609 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10610 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10611 /* found a previous ECN_ECHO update it if needed */
10612 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10613 ecne->tsn = htonl(high_tsn);
10617 /* nope could not find one to update so we must build one */
10618 sctp_alloc_a_chunk(stcb, chk);
10622 chk->copy_by_ref = 0;
10623 SCTP_STAT_INCR(sctps_sendecne);
10624 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10625 chk->rec.chunk_id.can_take_data = 0;
10626 chk->asoc = &stcb->asoc;
10627 chk->send_size = sizeof(struct sctp_ecne_chunk);
10628 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10629 if (chk->data == NULL) {
10630 sctp_free_a_chunk(stcb, chk);
10633 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10634 SCTP_BUF_LEN(chk->data) = chk->send_size;
10635 chk->sent = SCTP_DATAGRAM_UNSENT;
10636 chk->snd_count = 0;
10638 atomic_add_int(&chk->whoTo->ref_count, 1);
10639 stcb->asoc.ecn_echo_cnt_onq++;
10640 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10641 ecne->ch.chunk_type = SCTP_ECN_ECHO;
10642 ecne->ch.chunk_flags = 0;
10643 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10644 ecne->tsn = htonl(high_tsn);
10645 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10646 asoc->ctrl_queue_cnt++;
10650 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10651 struct mbuf *m, int iphlen, int bad_crc)
10653 struct sctp_association *asoc;
10654 struct sctp_pktdrop_chunk *drp;
10655 struct sctp_tmit_chunk *chk;
10662 struct ip6_hdr *ip6h;
10665 int fullsz = 0, extra = 0;
10668 struct sctp_chunkhdr *ch, chunk_buf;
10669 unsigned int chk_length;
10674 asoc = &stcb->asoc;
10675 SCTP_TCB_LOCK_ASSERT(stcb);
10676 if (asoc->peer_supports_pktdrop == 0) {
10678 * peer must declare support before I send one.
10682 if (stcb->sctp_socket == NULL) {
10685 sctp_alloc_a_chunk(stcb, chk);
10689 chk->copy_by_ref = 0;
10690 iph = mtod(m, struct ip *);
10692 sctp_free_a_chunk(stcb, chk);
10695 switch (iph->ip_v) {
10698 len = chk->send_size = iph->ip_len;
10701 case IPV6_VERSION >> 4:
10703 ip6h = mtod(m, struct ip6_hdr *);
10704 len = chk->send_size = htons(ip6h->ip6_plen);
10710 /* Validate that we do not have an ABORT in here. */
10711 offset = iphlen + sizeof(struct sctphdr);
10712 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10713 sizeof(*ch), (uint8_t *) & chunk_buf);
10714 while (ch != NULL) {
10715 chk_length = ntohs(ch->chunk_length);
10716 if (chk_length < sizeof(*ch)) {
10717 /* break to abort land */
10720 switch (ch->chunk_type) {
10721 case SCTP_PACKET_DROPPED:
10722 case SCTP_ABORT_ASSOCIATION:
10724 * we don't respond with an PKT-DROP to an ABORT
10727 sctp_free_a_chunk(stcb, chk);
10732 offset += SCTP_SIZE32(chk_length);
10733 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10734 sizeof(*ch), (uint8_t *) & chunk_buf);
10737 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10738 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10740 * only send 1 mtu worth, trim off the excess on the end.
10742 fullsz = len - extra;
10743 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10746 chk->asoc = &stcb->asoc;
10747 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10748 if (chk->data == NULL) {
10750 sctp_free_a_chunk(stcb, chk);
10753 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10754 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10756 sctp_m_freem(chk->data);
10760 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10761 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10762 chk->book_size_scale = 0;
10764 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10765 drp->trunc_len = htons(fullsz);
10767 * Len is already adjusted to size minus overhead above take
10768 * out the pkt_drop chunk itself from it.
10770 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10771 len = chk->send_size;
10773 /* no truncation needed */
10774 drp->ch.chunk_flags = 0;
10775 drp->trunc_len = htons(0);
10778 drp->ch.chunk_flags |= SCTP_BADCRC;
10780 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10781 SCTP_BUF_LEN(chk->data) = chk->send_size;
10782 chk->sent = SCTP_DATAGRAM_UNSENT;
10783 chk->snd_count = 0;
10785 /* we should hit here */
10788 chk->whoTo = asoc->primary_destination;
10790 atomic_add_int(&chk->whoTo->ref_count, 1);
10791 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10792 chk->rec.chunk_id.can_take_data = 1;
10793 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10794 drp->ch.chunk_length = htons(chk->send_size);
10795 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10799 drp->bottle_bw = htonl(spc);
10800 if (asoc->my_rwnd) {
10801 drp->current_onq = htonl(asoc->size_on_reasm_queue +
10802 asoc->size_on_all_streams +
10803 asoc->my_rwnd_control_len +
10804 stcb->sctp_socket->so_rcv.sb_cc);
10807 * If my rwnd is 0, possibly from mbuf depletion as well as
10808 * space used, tell the peer there is NO space aka onq == bw
10810 drp->current_onq = htonl(spc);
10814 m_copydata(m, iphlen, len, (caddr_t)datap);
10815 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10816 asoc->ctrl_queue_cnt++;
10820 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10822 struct sctp_association *asoc;
10823 struct sctp_cwr_chunk *cwr;
10824 struct sctp_tmit_chunk *chk;
10826 asoc = &stcb->asoc;
10827 SCTP_TCB_LOCK_ASSERT(stcb);
10828 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10829 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10830 /* found a previous ECN_CWR update it if needed */
10831 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10832 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10834 cwr->tsn = htonl(high_tsn);
10839 /* nope could not find one to update so we must build one */
10840 sctp_alloc_a_chunk(stcb, chk);
10844 chk->copy_by_ref = 0;
10845 chk->rec.chunk_id.id = SCTP_ECN_CWR;
10846 chk->rec.chunk_id.can_take_data = 1;
10847 chk->asoc = &stcb->asoc;
10848 chk->send_size = sizeof(struct sctp_cwr_chunk);
10849 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10850 if (chk->data == NULL) {
10851 sctp_free_a_chunk(stcb, chk);
10854 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10855 SCTP_BUF_LEN(chk->data) = chk->send_size;
10856 chk->sent = SCTP_DATAGRAM_UNSENT;
10857 chk->snd_count = 0;
10859 atomic_add_int(&chk->whoTo->ref_count, 1);
10860 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10861 cwr->ch.chunk_type = SCTP_ECN_CWR;
10862 cwr->ch.chunk_flags = 0;
10863 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10864 cwr->tsn = htonl(high_tsn);
10865 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10866 asoc->ctrl_queue_cnt++;
10870 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10871 int number_entries, uint16_t * list,
10872 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10874 int len, old_len, i;
10875 struct sctp_stream_reset_out_request *req_out;
10876 struct sctp_chunkhdr *ch;
10878 ch = mtod(chk->data, struct sctp_chunkhdr *);
10881 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10883 /* get to new offset for the param. */
10884 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10885 /* now how long will this param be? */
10886 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10887 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10888 req_out->ph.param_length = htons(len);
10889 req_out->request_seq = htonl(seq);
10890 req_out->response_seq = htonl(resp_seq);
10891 req_out->send_reset_at_tsn = htonl(last_sent);
10892 if (number_entries) {
10893 for (i = 0; i < number_entries; i++) {
10894 req_out->list_of_streams[i] = htons(list[i]);
10897 if (SCTP_SIZE32(len) > len) {
10899 * Need to worry about the pad we may end up adding to the
10900 * end. This is easy since the struct is either aligned to 4
10901 * bytes or 2 bytes off.
10903 req_out->list_of_streams[number_entries] = 0;
10905 /* now fix the chunk length */
10906 ch->chunk_length = htons(len + old_len);
10907 chk->book_size = len + old_len;
10908 chk->book_size_scale = 0;
10909 chk->send_size = SCTP_SIZE32(chk->book_size);
10910 SCTP_BUF_LEN(chk->data) = chk->send_size;
10916 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10917 int number_entries, uint16_t * list,
10920 int len, old_len, i;
10921 struct sctp_stream_reset_in_request *req_in;
10922 struct sctp_chunkhdr *ch;
10924 ch = mtod(chk->data, struct sctp_chunkhdr *);
10927 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10929 /* get to new offset for the param. */
10930 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10931 /* now how long will this param be? */
10932 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10933 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10934 req_in->ph.param_length = htons(len);
10935 req_in->request_seq = htonl(seq);
10936 if (number_entries) {
10937 for (i = 0; i < number_entries; i++) {
10938 req_in->list_of_streams[i] = htons(list[i]);
10941 if (SCTP_SIZE32(len) > len) {
10943 * Need to worry about the pad we may end up adding to the
10944 * end. This is easy since the struct is either aligned to 4
10945 * bytes or 2 bytes off.
10947 req_in->list_of_streams[number_entries] = 0;
10949 /* now fix the chunk length */
10950 ch->chunk_length = htons(len + old_len);
10951 chk->book_size = len + old_len;
10952 chk->book_size_scale = 0;
10953 chk->send_size = SCTP_SIZE32(chk->book_size);
10954 SCTP_BUF_LEN(chk->data) = chk->send_size;
10960 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10964 struct sctp_stream_reset_tsn_request *req_tsn;
10965 struct sctp_chunkhdr *ch;
10967 ch = mtod(chk->data, struct sctp_chunkhdr *);
10970 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10972 /* get to new offset for the param. */
10973 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10974 /* now how long will this param be? */
10975 len = sizeof(struct sctp_stream_reset_tsn_request);
10976 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10977 req_tsn->ph.param_length = htons(len);
10978 req_tsn->request_seq = htonl(seq);
10980 /* now fix the chunk length */
10981 ch->chunk_length = htons(len + old_len);
10982 chk->send_size = len + old_len;
10983 chk->book_size = SCTP_SIZE32(chk->send_size);
10984 chk->book_size_scale = 0;
10985 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10990 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10991 uint32_t resp_seq, uint32_t result)
10994 struct sctp_stream_reset_response *resp;
10995 struct sctp_chunkhdr *ch;
10997 ch = mtod(chk->data, struct sctp_chunkhdr *);
11000 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11002 /* get to new offset for the param. */
11003 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11004 /* now how long will this param be? */
11005 len = sizeof(struct sctp_stream_reset_response);
11006 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11007 resp->ph.param_length = htons(len);
11008 resp->response_seq = htonl(resp_seq);
11009 resp->result = ntohl(result);
11011 /* now fix the chunk length */
11012 ch->chunk_length = htons(len + old_len);
11013 chk->book_size = len + old_len;
11014 chk->book_size_scale = 0;
11015 chk->send_size = SCTP_SIZE32(chk->book_size);
11016 SCTP_BUF_LEN(chk->data) = chk->send_size;
11023 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11024 uint32_t resp_seq, uint32_t result,
11025 uint32_t send_una, uint32_t recv_next)
11028 struct sctp_stream_reset_response_tsn *resp;
11029 struct sctp_chunkhdr *ch;
11031 ch = mtod(chk->data, struct sctp_chunkhdr *);
11034 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11036 /* get to new offset for the param. */
11037 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11038 /* now how long will this param be? */
11039 len = sizeof(struct sctp_stream_reset_response_tsn);
11040 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11041 resp->ph.param_length = htons(len);
11042 resp->response_seq = htonl(resp_seq);
11043 resp->result = htonl(result);
11044 resp->senders_next_tsn = htonl(send_una);
11045 resp->receivers_next_tsn = htonl(recv_next);
11047 /* now fix the chunk length */
11048 ch->chunk_length = htons(len + old_len);
11049 chk->book_size = len + old_len;
11050 chk->send_size = SCTP_SIZE32(chk->book_size);
11051 chk->book_size_scale = 0;
11052 SCTP_BUF_LEN(chk->data) = chk->send_size;
11058 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11059 int number_entries, uint16_t * list,
11060 uint8_t send_out_req, uint32_t resp_seq,
11061 uint8_t send_in_req,
11062 uint8_t send_tsn_req)
11065 struct sctp_association *asoc;
11066 struct sctp_tmit_chunk *chk;
11067 struct sctp_chunkhdr *ch;
11070 asoc = &stcb->asoc;
11071 if (asoc->stream_reset_outstanding) {
11073 * Already one pending, must get ACK back to clear the flag.
11075 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11078 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
11079 /* nothing to do */
11080 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11083 if (send_tsn_req && (send_out_req || send_in_req)) {
11084 /* error, can't do that */
11085 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11088 sctp_alloc_a_chunk(stcb, chk);
11090 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11093 chk->copy_by_ref = 0;
11094 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11095 chk->rec.chunk_id.can_take_data = 0;
11096 chk->asoc = &stcb->asoc;
11097 chk->book_size = sizeof(struct sctp_chunkhdr);
11098 chk->send_size = SCTP_SIZE32(chk->book_size);
11099 chk->book_size_scale = 0;
11101 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11102 if (chk->data == NULL) {
11103 sctp_free_a_chunk(stcb, chk);
11104 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11107 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11109 /* setup chunk parameters */
11110 chk->sent = SCTP_DATAGRAM_UNSENT;
11111 chk->snd_count = 0;
11112 chk->whoTo = asoc->primary_destination;
11113 atomic_add_int(&chk->whoTo->ref_count, 1);
11115 ch = mtod(chk->data, struct sctp_chunkhdr *);
11116 ch->chunk_type = SCTP_STREAM_RESET;
11117 ch->chunk_flags = 0;
11118 ch->chunk_length = htons(chk->book_size);
11119 SCTP_BUF_LEN(chk->data) = chk->send_size;
11121 seq = stcb->asoc.str_reset_seq_out;
11122 if (send_out_req) {
11123 sctp_add_stream_reset_out(chk, number_entries, list,
11124 seq, resp_seq, (stcb->asoc.sending_seq - 1));
11125 asoc->stream_reset_out_is_outstanding = 1;
11127 asoc->stream_reset_outstanding++;
11130 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11131 asoc->stream_reset_outstanding++;
11133 if (send_tsn_req) {
11134 sctp_add_stream_reset_tsn(chk, seq);
11135 asoc->stream_reset_outstanding++;
11137 asoc->str_reset = chk;
11139 /* insert the chunk for sending */
11140 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11143 asoc->ctrl_queue_cnt++;
11144 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11149 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11150 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11153 * Formulate the abort message, and send it back down.
11155 struct mbuf *o_pak;
11157 struct sctp_abort_msg *abm;
11158 struct ip *iph, *iph_out;
11159 struct udphdr *udp;
11162 struct ip6_hdr *ip6, *ip6_out;
11165 int iphlen_out, len;
11167 /* don't respond to ABORT with ABORT */
11168 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11170 sctp_m_freem(err_cause);
11173 iph = mtod(m, struct ip *);
11174 switch (iph->ip_v) {
11176 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11179 case IPV6_VERSION >> 4:
11180 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11187 len += sizeof(struct udphdr);
11189 mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
11190 if (mout == NULL) {
11192 sctp_m_freem(err_cause);
11195 SCTP_BUF_LEN(mout) = len;
11196 SCTP_BUF_NEXT(mout) = err_cause;
11201 switch (iph->ip_v) {
11203 iph_out = mtod(mout, struct ip *);
11205 /* Fill in the IP header for the ABORT */
11206 iph_out->ip_v = IPVERSION;
11207 iph_out->ip_hl = (sizeof(struct ip) / 4);
11208 iph_out->ip_tos = (u_char)0;
11209 iph_out->ip_id = 0;
11210 iph_out->ip_off = 0;
11211 iph_out->ip_ttl = MAXTTL;
11213 iph_out->ip_p = IPPROTO_UDP;
11215 iph_out->ip_p = IPPROTO_SCTP;
11217 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11218 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11219 /* let IP layer calculate this */
11220 iph_out->ip_sum = 0;
11222 iphlen_out = sizeof(*iph_out);
11223 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11226 case IPV6_VERSION >> 4:
11227 ip6 = (struct ip6_hdr *)iph;
11228 ip6_out = mtod(mout, struct ip6_hdr *);
11230 /* Fill in the IP6 header for the ABORT */
11231 ip6_out->ip6_flow = ip6->ip6_flow;
11232 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11234 ip6_out->ip6_nxt = IPPROTO_UDP;
11236 ip6_out->ip6_nxt = IPPROTO_SCTP;
11238 ip6_out->ip6_src = ip6->ip6_dst;
11239 ip6_out->ip6_dst = ip6->ip6_src;
11241 iphlen_out = sizeof(*ip6_out);
11242 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11246 /* Currently not supported */
11248 sctp_m_freem(err_cause);
11249 sctp_m_freem(mout);
11253 udp = (struct udphdr *)abm;
11255 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11256 udp->uh_dport = port;
11257 /* set udp->uh_ulen later */
11259 iphlen_out += sizeof(struct udphdr);
11260 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11262 abm->sh.src_port = sh->dest_port;
11263 abm->sh.dest_port = sh->src_port;
11264 abm->sh.checksum = 0;
11266 abm->sh.v_tag = sh->v_tag;
11267 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11269 abm->sh.v_tag = htonl(vtag);
11270 abm->msg.ch.chunk_flags = 0;
11272 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11275 struct mbuf *m_tmp = err_cause;
11278 /* get length of the err_cause chain */
11279 while (m_tmp != NULL) {
11280 err_len += SCTP_BUF_LEN(m_tmp);
11281 m_tmp = SCTP_BUF_NEXT(m_tmp);
11283 len = SCTP_BUF_LEN(mout) + err_len;
11285 /* need pad at end of chunk */
11286 uint32_t cpthis = 0;
11289 padlen = 4 - (len % 4);
11290 m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11293 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11295 len = SCTP_BUF_LEN(mout);
11296 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11300 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
11301 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11303 sctp_m_freem(mout);
11306 if (iph_out != NULL) {
11308 struct sctp_tcb *stcb = NULL;
11311 /* zap the stack pointer to the route */
11312 bzero(&ro, sizeof ro);
11314 udp->uh_ulen = htons(len - sizeof(struct ip));
11315 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11317 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11318 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11319 /* set IPv4 length */
11320 iph_out->ip_len = len;
11322 #ifdef SCTP_PACKET_LOGGING
11323 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11324 sctp_packet_log(mout, len);
11326 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11328 SCTP_ENABLE_UDP_CSUM(o_pak);
11330 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11332 /* Free the route if we got one back */
11337 if (ip6_out != NULL) {
11338 struct route_in6 ro;
11340 struct sctp_tcb *stcb = NULL;
11341 struct ifnet *ifp = NULL;
11343 /* zap the stack pointer to the route */
11344 bzero(&ro, sizeof(ro));
11346 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11348 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11349 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11350 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11351 #ifdef SCTP_PACKET_LOGGING
11352 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11353 sctp_packet_log(mout, len);
11355 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11357 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11358 udp->uh_sum = 0xffff;
11361 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11363 /* Free the route if we got one back */
11368 SCTP_STAT_INCR(sctps_sendpackets);
11369 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11373 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11374 uint32_t vrf_id, uint16_t port)
11376 struct mbuf *o_pak;
11377 struct sctphdr *ihdr;
11379 struct sctphdr *ohdr;
11380 struct sctp_chunkhdr *ophdr;
11382 struct udphdr *udp = NULL;
11386 struct sockaddr_in6 lsa6, fsa6;
11393 iph = mtod(m, struct ip *);
11394 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
11396 SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
11398 /* can't send because we can't add a mbuf */
11401 ohdr = mtod(scm, struct sctphdr *);
11402 ohdr->src_port = ihdr->dest_port;
11403 ohdr->dest_port = ihdr->src_port;
11404 ohdr->v_tag = vtag;
11405 ohdr->checksum = 0;
11406 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
11407 ophdr->chunk_type = SCTP_OPERATION_ERROR;
11408 ophdr->chunk_flags = 0;
11412 len += SCTP_BUF_LEN(at);
11413 at = SCTP_BUF_NEXT(at);
11415 ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
11418 uint32_t cpthis = 0;
11421 padlen = 4 - (len % 4);
11422 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
11425 val = sctp_calculate_sum(scm, NULL, 0);
11428 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11430 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
11434 mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11436 mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
11439 if (mout == NULL) {
11443 SCTP_BUF_NEXT(mout) = scm;
11444 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11445 sctp_m_freem(mout);
11448 ohdr->checksum = val;
11449 switch (iph->ip_v) {
11455 struct sctp_tcb *stcb = NULL;
11457 SCTP_BUF_LEN(mout) = sizeof(struct ip);
11458 len += sizeof(struct ip);
11460 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11461 len += sizeof(struct udphdr);
11463 bzero(&ro, sizeof ro);
11464 out = mtod(mout, struct ip *);
11465 out->ip_v = iph->ip_v;
11466 out->ip_hl = (sizeof(struct ip) / 4);
11467 out->ip_tos = iph->ip_tos;
11468 out->ip_id = iph->ip_id;
11470 out->ip_ttl = MAXTTL;
11472 out->ip_p = IPPROTO_UDP;
11474 out->ip_p = IPPROTO_SCTP;
11477 out->ip_src = iph->ip_dst;
11478 out->ip_dst = iph->ip_src;
11481 udp = (struct udphdr *)(out + 1);
11482 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11483 udp->uh_dport = port;
11484 udp->uh_ulen = htons(len - sizeof(struct ip));
11485 udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11487 #ifdef SCTP_PACKET_LOGGING
11488 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11489 sctp_packet_log(mout, len);
11491 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11493 SCTP_ENABLE_UDP_CSUM(o_pak);
11495 SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
11497 SCTP_STAT_INCR(sctps_sendpackets);
11498 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11499 /* Free the route if we got one back */
11505 case IPV6_VERSION >> 4:
11508 struct route_in6 ro;
11510 struct sctp_tcb *stcb = NULL;
11511 struct ifnet *ifp = NULL;
11512 struct ip6_hdr *out6, *in6;
11514 SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
11515 len += sizeof(struct ip6_hdr);
11516 bzero(&ro, sizeof ro);
11518 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11519 len += sizeof(struct udphdr);
11521 in6 = mtod(m, struct ip6_hdr *);
11522 out6 = mtod(mout, struct ip6_hdr *);
11523 out6->ip6_flow = in6->ip6_flow;
11524 out6->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11526 out6->ip6_nxt = IPPROTO_UDP;
11528 out6->ip6_nxt = IPPROTO_SCTP;
11530 out6->ip6_src = in6->ip6_dst;
11531 out6->ip6_dst = in6->ip6_src;
11532 out6->ip6_plen = len - sizeof(struct ip6_hdr);
11534 udp = (struct udphdr *)(out6 + 1);
11535 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11536 udp->uh_dport = port;
11537 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11541 bzero(&lsa6, sizeof(lsa6));
11542 lsa6.sin6_len = sizeof(lsa6);
11543 lsa6.sin6_family = AF_INET6;
11544 lsa6.sin6_addr = out6->ip6_src;
11545 bzero(&fsa6, sizeof(fsa6));
11546 fsa6.sin6_len = sizeof(fsa6);
11547 fsa6.sin6_family = AF_INET6;
11548 fsa6.sin6_addr = out6->ip6_dst;
11550 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
11551 SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
11552 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
11553 SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
11554 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
11556 #ifdef SCTP_PACKET_LOGGING
11557 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11558 sctp_packet_log(mout, len);
11560 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11562 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11563 udp->uh_sum = 0xffff;
11566 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11568 SCTP_STAT_INCR(sctps_sendpackets);
11569 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11570 /* Free the route if we got one back */
11582 static struct mbuf *
11583 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11585 struct sctp_sndrcvinfo *srcv,
11587 int user_marks_eor,
11590 struct mbuf **new_tail)
11594 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11595 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11597 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11600 *sndout = m_length(m, NULL);
11601 *new_tail = m_last(m);
11607 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11614 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11616 if (sp->data == NULL) {
11617 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11620 sp->tail_mbuf = m_last(sp->data);
11626 static struct sctp_stream_queue_pending *
11627 sctp_copy_it_in(struct sctp_tcb *stcb,
11628 struct sctp_association *asoc,
11629 struct sctp_sndrcvinfo *srcv,
11631 struct sctp_nets *net,
11633 int user_marks_eor,
11638 * This routine must be very careful in its work. Protocol
11639 * processing is up and running so care must be taken to spl...()
11640 * when you need to do something that may effect the stcb/asoc. The
11641 * sb is locked however. When data is copied the protocol processing
11642 * should be enabled since this is a slower operation...
11644 struct sctp_stream_queue_pending *sp = NULL;
11648 /* Now can we send this? */
11649 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11650 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11651 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11652 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11653 /* got data while shutting down */
11654 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11655 *error = ECONNRESET;
11658 sctp_alloc_a_strmoq(stcb, sp);
11660 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11665 sp->sender_all_done = 0;
11666 sp->sinfo_flags = srcv->sinfo_flags;
11667 sp->timetolive = srcv->sinfo_timetolive;
11668 sp->ppid = srcv->sinfo_ppid;
11669 sp->context = srcv->sinfo_context;
11671 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11673 sp->stream = srcv->sinfo_stream;
11674 sp->length = min(uio->uio_resid, max_send_len);
11675 if ((sp->length == (uint32_t) uio->uio_resid) &&
11676 ((user_marks_eor == 0) ||
11677 (srcv->sinfo_flags & SCTP_EOF) ||
11678 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11679 sp->msg_is_complete = 1;
11681 sp->msg_is_complete = 0;
11683 sp->sender_all_done = 0;
11684 sp->some_taken = 0;
11685 sp->put_last_out = 0;
11686 resv_in_first = sizeof(struct sctp_data_chunk);
11687 sp->data = sp->tail_mbuf = NULL;
11688 *error = sctp_copy_one(sp, uio, resv_in_first);
11690 sctp_free_a_strmoq(stcb, sp);
11693 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11697 sp->net = asoc->primary_destination;
11700 atomic_add_int(&sp->net->ref_count, 1);
11701 sctp_set_prsctp_policy(stcb, sp);
11709 sctp_sosend(struct socket *so,
11710 struct sockaddr *addr,
11713 struct mbuf *control,
11718 struct sctp_inpcb *inp;
11719 int error, use_rcvinfo = 0;
11720 struct sctp_sndrcvinfo srcv;
11721 struct sockaddr *addr_to_use;
11724 struct sockaddr_in sin;
11728 inp = (struct sctp_inpcb *)so->so_pcb;
11730 /* process cmsg snd/rcv info (maybe a assoc-id) */
11731 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11737 addr_to_use = addr;
11739 if ((addr) && (addr->sa_family == AF_INET6)) {
11740 struct sockaddr_in6 *sin6;
11742 sin6 = (struct sockaddr_in6 *)addr;
11743 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
11744 in6_sin6_2_sin(&sin, sin6);
11745 addr_to_use = (struct sockaddr *)&sin;
11749 error = sctp_lower_sosend(so, addr_to_use, uio, top,
11760 sctp_lower_sosend(struct socket *so,
11761 struct sockaddr *addr,
11763 struct mbuf *i_pak,
11764 struct mbuf *control,
11767 struct sctp_sndrcvinfo *srcv
11772 unsigned int sndlen = 0, max_len;
11774 struct mbuf *top = NULL;
11775 int queue_only = 0, queue_only_for_init = 0;
11776 int free_cnt_applied = 0;
11778 int now_filled = 0;
11779 unsigned int inqueue_bytes = 0;
11780 struct sctp_block_entry be;
11781 struct sctp_inpcb *inp;
11782 struct sctp_tcb *stcb = NULL;
11783 struct timeval now;
11784 struct sctp_nets *net;
11785 struct sctp_association *asoc;
11786 struct sctp_inpcb *t_inp;
11787 int user_marks_eor;
11788 int create_lock_applied = 0;
11789 int nagle_applies = 0;
11790 int some_on_control = 0;
11791 int got_all_of_the_send = 0;
11792 int hold_tcblock = 0;
11793 int non_blocking = 0;
11794 int temp_flags = 0;
11795 uint32_t local_add_more, local_soresv = 0;
11802 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11804 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11807 SCTP_RELEASE_PKT(i_pak);
11811 if ((uio == NULL) && (i_pak == NULL)) {
11812 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11815 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11816 atomic_add_int(&inp->total_sends, 1);
11818 if (uio->uio_resid < 0) {
11819 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11822 sndlen = uio->uio_resid;
11824 top = SCTP_HEADER_TO_CHAIN(i_pak);
11825 sndlen = SCTP_HEADER_LEN(i_pak);
11827 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11831 * Pre-screen address, if one is given the sin-len
11832 * must be set correctly!
11835 if ((addr->sa_family == AF_INET) &&
11836 (addr->sa_len != sizeof(struct sockaddr_in))) {
11837 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11840 } else if ((addr->sa_family == AF_INET6) &&
11841 (addr->sa_len != sizeof(struct sockaddr_in6))) {
11842 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11849 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11850 (inp->sctp_socket->so_qlimit)) {
11851 /* The listener can NOT send */
11852 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11856 if ((use_rcvinfo) && srcv) {
11857 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11858 PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11859 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11863 if (srcv->sinfo_flags)
11864 SCTP_STAT_INCR(sctps_sends_with_flags);
11866 if (srcv->sinfo_flags & SCTP_SENDALL) {
11867 /* its a sendall */
11868 error = sctp_sendall(inp, uio, top, srcv);
11873 /* now we must find the assoc */
11874 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11875 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11876 SCTP_INP_RLOCK(inp);
11877 stcb = LIST_FIRST(&inp->sctp_asoc_list);
11878 if (stcb == NULL) {
11879 SCTP_INP_RUNLOCK(inp);
11880 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11885 SCTP_INP_RUNLOCK(inp);
11887 /* Must locate the net structure if addr given */
11888 net = sctp_findnet(stcb, addr);
11890 /* validate port was 0 or correct */
11891 struct sockaddr_in *sin;
11893 sin = (struct sockaddr_in *)addr;
11894 if ((sin->sin_port != 0) &&
11895 (sin->sin_port != stcb->rport)) {
11899 temp_flags |= SCTP_ADDR_OVER;
11901 net = stcb->asoc.primary_destination;
11902 if (addr && (net == NULL)) {
11903 /* Could not find address, was it legal */
11904 if (addr->sa_family == AF_INET) {
11905 struct sockaddr_in *sin;
11907 sin = (struct sockaddr_in *)addr;
11908 if (sin->sin_addr.s_addr == 0) {
11909 if ((sin->sin_port == 0) ||
11910 (sin->sin_port == stcb->rport)) {
11911 net = stcb->asoc.primary_destination;
11915 struct sockaddr_in6 *sin6;
11917 sin6 = (struct sockaddr_in6 *)addr;
11918 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11919 if ((sin6->sin6_port == 0) ||
11920 (sin6->sin6_port == stcb->rport)) {
11921 net = stcb->asoc.primary_destination;
11927 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11931 } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11932 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11936 * Must locate the net structure if addr
11939 net = sctp_findnet(stcb, addr);
11941 net = stcb->asoc.primary_destination;
11942 if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11943 ((net == NULL) || (addr == NULL))) {
11944 struct sockaddr_in *sin;
11946 if (addr == NULL) {
11947 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11951 sin = (struct sockaddr_in *)addr;
11952 /* Validate port is 0 or correct */
11953 if ((sin->sin_port != 0) &&
11954 (sin->sin_port != stcb->rport)) {
11962 * Since we did not use findep we must
11963 * increment it, and if we don't find a tcb
11966 SCTP_INP_WLOCK(inp);
11967 SCTP_INP_INCR_REF(inp);
11968 SCTP_INP_WUNLOCK(inp);
11969 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11970 if (stcb == NULL) {
11971 SCTP_INP_WLOCK(inp);
11972 SCTP_INP_DECR_REF(inp);
11973 SCTP_INP_WUNLOCK(inp);
11978 if ((stcb == NULL) && (addr)) {
11979 /* Possible implicit send? */
11980 SCTP_ASOC_CREATE_LOCK(inp);
11981 create_lock_applied = 1;
11982 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11983 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11984 /* Should I really unlock ? */
11985 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11990 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11991 (addr->sa_family == AF_INET6)) {
11992 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11996 SCTP_INP_WLOCK(inp);
11997 SCTP_INP_INCR_REF(inp);
11998 SCTP_INP_WUNLOCK(inp);
11999 /* With the lock applied look again */
12000 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12001 if (stcb == NULL) {
12002 SCTP_INP_WLOCK(inp);
12003 SCTP_INP_DECR_REF(inp);
12004 SCTP_INP_WUNLOCK(inp);
12008 if (t_inp != inp) {
12009 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12014 if (stcb == NULL) {
12015 if (addr == NULL) {
12016 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12021 * UDP style, we must go ahead and start the INIT
12026 if ((use_rcvinfo) && (srcv) &&
12027 ((srcv->sinfo_flags & SCTP_ABORT) ||
12028 ((srcv->sinfo_flags & SCTP_EOF) &&
12031 * User asks to abort a non-existant assoc,
12032 * or EOF a non-existant assoc with no data
12034 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12038 /* get an asoc/stcb struct */
12039 vrf_id = inp->def_vrf_id;
12041 if (create_lock_applied == 0) {
12042 panic("Error, should hold create lock and I don't?");
12045 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12048 if (stcb == NULL) {
12049 /* Error is setup for us in the call */
12052 if (create_lock_applied) {
12053 SCTP_ASOC_CREATE_UNLOCK(inp);
12054 create_lock_applied = 0;
12056 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12059 * Turn on queue only flag to prevent data from
12063 asoc = &stcb->asoc;
12064 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12065 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12067 /* initialize authentication params for the assoc */
12068 sctp_initialize_auth_params(inp, stcb);
12072 * see if a init structure exists in cmsg
12075 struct sctp_initmsg initm;
12078 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12081 * we have an INIT override of the
12084 if (initm.sinit_max_attempts)
12085 asoc->max_init_times = initm.sinit_max_attempts;
12086 if (initm.sinit_num_ostreams)
12087 asoc->pre_open_streams = initm.sinit_num_ostreams;
12088 if (initm.sinit_max_instreams)
12089 asoc->max_inbound_streams = initm.sinit_max_instreams;
12090 if (initm.sinit_max_init_timeo)
12091 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12092 if (asoc->streamoutcnt < asoc->pre_open_streams) {
12093 /* Default is NOT correct */
12094 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12095 asoc->streamoutcnt, asoc->pre_open_streams);
12097 * What happens if this
12098 * fails? we panic ...
12101 struct sctp_stream_out *tmp_str;
12104 if (hold_tcblock) {
12106 SCTP_TCB_UNLOCK(stcb);
12108 SCTP_MALLOC(tmp_str,
12109 struct sctp_stream_out *,
12110 (asoc->pre_open_streams *
12111 sizeof(struct sctp_stream_out)),
12114 SCTP_TCB_LOCK(stcb);
12116 if (tmp_str != NULL) {
12117 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12118 asoc->strmout = tmp_str;
12119 asoc->streamoutcnt = asoc->pre_open_streams;
12121 asoc->pre_open_streams = asoc->streamoutcnt;
12124 for (i = 0; i < asoc->streamoutcnt; i++) {
12126 * inbound side must be set
12127 * to 0xffff, also NOTE when
12128 * we get the INIT-ACK back
12129 * (for INIT sender) we MUST
12131 * (streamoutcnt) but first
12132 * check if we sent to any
12133 * of the upper streams that
12134 * were dropped (if some
12135 * were). Those that were
12136 * dropped must be notified
12137 * to the upper layer as
12140 asoc->strmout[i].next_sequence_sent = 0x0;
12141 TAILQ_INIT(&asoc->strmout[i].outqueue);
12142 asoc->strmout[i].stream_no = i;
12143 asoc->strmout[i].last_msg_incomplete = 0;
12144 asoc->strmout[i].next_spoke.tqe_next = 0;
12145 asoc->strmout[i].next_spoke.tqe_prev = 0;
12151 /* out with the INIT */
12152 queue_only_for_init = 1;
12154 * we may want to dig in after this call and adjust the MTU
12155 * value. It defaulted to 1500 (constant) but the ro
12156 * structure may now have an update and thus we may need to
12157 * change it BEFORE we append the message.
12159 net = stcb->asoc.primary_destination;
12160 asoc = &stcb->asoc;
12163 if ((SCTP_SO_IS_NBIO(so)
12164 || (flags & MSG_NBIO)
12168 asoc = &stcb->asoc;
12170 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12171 if (sndlen > asoc->smallest_mtu) {
12172 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12177 /* would we block? */
12178 if (non_blocking) {
12179 if (hold_tcblock == 0) {
12180 SCTP_TCB_LOCK(stcb);
12183 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12184 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12185 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12186 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12187 if (sndlen > SCTP_SB_LIMIT_SND(so))
12190 error = EWOULDBLOCK;
12193 stcb->asoc.sb_send_resv += sndlen;
12194 SCTP_TCB_UNLOCK(stcb);
12197 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12199 local_soresv = sndlen;
12200 /* Keep the stcb from being freed under our feet */
12201 if (free_cnt_applied) {
12203 panic("refcnt already incremented");
12205 printf("refcnt:1 already incremented?\n");
12208 atomic_add_int(&stcb->asoc.refcnt, 1);
12209 free_cnt_applied = 1;
12211 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12212 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12213 error = ECONNRESET;
12216 if (create_lock_applied) {
12217 SCTP_ASOC_CREATE_UNLOCK(inp);
12218 create_lock_applied = 0;
12220 if (asoc->stream_reset_outstanding) {
12222 * Can't queue any data while stream reset is underway.
12224 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12228 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12229 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12232 if ((use_rcvinfo == 0) || (srcv == NULL)) {
12233 /* Grab the default stuff from the asoc */
12234 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12236 /* we are now done with all control */
12238 sctp_m_freem(control);
12241 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12242 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12243 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12244 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12245 if ((use_rcvinfo) &&
12246 (srcv->sinfo_flags & SCTP_ABORT)) {
12249 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12250 error = ECONNRESET;
12254 /* Ok, we will attempt a msgsnd :> */
12256 p->td_ru.ru_msgsnd++;
12259 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12260 net = stcb->asoc.primary_destination;
12264 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12268 if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12270 * CMT: Added check for CMT above. net above is the primary
12271 * dest. If CMT is ON, sender should always attempt to send
12272 * with the output routine sctp_fill_outqueue() that loops
12273 * through all destination addresses. Therefore, if CMT is
12274 * ON, queue_only is NOT set to 1 here, so that
12275 * sctp_chunk_output() can be called below.
12279 } else if (asoc->ifp_had_enobuf) {
12280 SCTP_STAT_INCR(sctps_ifnomemqueued);
12281 if (net->flight_size > (net->mtu * 2))
12283 asoc->ifp_had_enobuf = 0;
12285 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12286 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12288 /* Are we aborting? */
12289 if (srcv->sinfo_flags & SCTP_ABORT) {
12291 int tot_demand, tot_out = 0, max_out;
12293 SCTP_STAT_INCR(sctps_sends_with_abort);
12294 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12295 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12296 /* It has to be up before we abort */
12297 /* how big is the user initiated abort? */
12298 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12302 if (hold_tcblock) {
12303 SCTP_TCB_UNLOCK(stcb);
12307 struct mbuf *cntm = NULL;
12309 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12313 tot_out += SCTP_BUF_LEN(cntm);
12314 cntm = SCTP_BUF_NEXT(cntm);
12317 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12319 /* Must fit in a MTU */
12321 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12322 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12324 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12328 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12331 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12335 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12336 max_out -= sizeof(struct sctp_abort_msg);
12337 if (tot_out > max_out) {
12341 struct sctp_paramhdr *ph;
12343 /* now move forward the data pointer */
12344 ph = mtod(mm, struct sctp_paramhdr *);
12345 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12346 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12348 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12350 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12353 * Here if we can't get his data we
12354 * still abort we just don't get to
12355 * send the users note :-0
12362 SCTP_BUF_NEXT(mm) = top;
12366 if (hold_tcblock == 0) {
12367 SCTP_TCB_LOCK(stcb);
12370 atomic_add_int(&stcb->asoc.refcnt, -1);
12371 free_cnt_applied = 0;
12372 /* release this lock, otherwise we hang on ourselves */
12373 sctp_abort_an_association(stcb->sctp_ep, stcb,
12374 SCTP_RESPONSE_TO_USER_REQ,
12375 mm, SCTP_SO_LOCKED);
12376 /* now relock the stcb so everything is sane */
12380 * In this case top is already chained to mm avoid double
12381 * free, since we free it below if top != NULL and driver
12382 * would free it after sending the packet out
12389 /* Calculate the maximum we can send */
12390 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12391 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12392 if (non_blocking) {
12393 /* we already checked for non-blocking above. */
12396 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12401 if (hold_tcblock) {
12402 SCTP_TCB_UNLOCK(stcb);
12405 /* Is the stream no. valid? */
12406 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12407 /* Invalid stream number */
12408 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12412 if (asoc->strmout == NULL) {
12413 /* huh? software error */
12414 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12418 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12419 if ((user_marks_eor == 0) &&
12420 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12421 /* It will NEVER fit */
12422 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12426 if ((uio == NULL) && user_marks_eor) {
12428 * We do not support eeor mode for
12429 * sending with mbuf chains (like sendfile).
12431 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12435 if (user_marks_eor) {
12436 local_add_more = SCTP_BASE_SYSCTL(sctp_add_more_threshold);
12439 * For non-eeor the whole message must fit in
12440 * the socket send buffer.
12442 local_add_more = sndlen;
12445 if (non_blocking) {
12446 goto skip_preblock;
12448 if (((max_len <= local_add_more) &&
12449 (SCTP_SB_LIMIT_SND(so) > local_add_more)) ||
12450 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { /* if */
12451 /* No room right now ! */
12452 SOCKBUF_LOCK(&so->so_snd);
12453 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12454 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12455 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) /* while */ )) {
12457 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12458 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
12462 stcb->block_entry = &be;
12463 error = sbwait(&so->so_snd);
12464 stcb->block_entry = NULL;
12465 if (error || so->so_error || be.error) {
12468 error = so->so_error;
12473 SOCKBUF_UNLOCK(&so->so_snd);
12476 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12477 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12478 so, asoc, stcb->asoc.total_output_queue_size);
12480 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12483 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12485 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12486 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12487 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12491 SOCKBUF_UNLOCK(&so->so_snd);
12494 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12497 atomic_add_int(&stcb->total_sends, 1);
12499 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12500 * case NOTE: uio will be null when top/mbuf is passed
12503 if (srcv->sinfo_flags & SCTP_EOF) {
12504 got_all_of_the_send = 1;
12507 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12513 struct sctp_stream_queue_pending *sp;
12514 struct sctp_stream_out *strm;
12515 uint32_t sndout, initial_out;
12517 initial_out = uio->uio_resid;
12519 SCTP_TCB_SEND_LOCK(stcb);
12520 if ((asoc->stream_locked) &&
12521 (asoc->stream_locked_on != srcv->sinfo_stream)) {
12522 SCTP_TCB_SEND_UNLOCK(stcb);
12523 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12527 SCTP_TCB_SEND_UNLOCK(stcb);
12529 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12530 if (strm->last_msg_incomplete == 0) {
12532 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12533 if ((sp == NULL) || (error)) {
12536 SCTP_TCB_SEND_LOCK(stcb);
12537 if (sp->msg_is_complete) {
12538 strm->last_msg_incomplete = 0;
12539 asoc->stream_locked = 0;
12542 * Just got locked to this guy in case of an
12545 strm->last_msg_incomplete = 1;
12546 asoc->stream_locked = 1;
12547 asoc->stream_locked_on = srcv->sinfo_stream;
12548 sp->sender_all_done = 0;
12550 sctp_snd_sb_alloc(stcb, sp->length);
12551 atomic_add_int(&asoc->stream_queue_cnt, 1);
12552 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12553 sp->strseq = strm->next_sequence_sent;
12554 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12555 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12556 (uintptr_t) stcb, sp->length,
12557 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12559 strm->next_sequence_sent++;
12561 SCTP_STAT_INCR(sctps_sends_with_unord);
12563 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12564 if ((strm->next_spoke.tqe_next == NULL) &&
12565 (strm->next_spoke.tqe_prev == NULL)) {
12566 /* Not on wheel, insert */
12567 sctp_insert_on_wheel(stcb, asoc, strm, 1);
12569 SCTP_TCB_SEND_UNLOCK(stcb);
12571 SCTP_TCB_SEND_LOCK(stcb);
12572 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12573 SCTP_TCB_SEND_UNLOCK(stcb);
12575 /* ???? Huh ??? last msg is gone */
12577 panic("Warning: Last msg marked incomplete, yet nothing left?");
12579 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12580 strm->last_msg_incomplete = 0;
12586 while (uio->uio_resid > 0) {
12587 /* How much room do we have? */
12588 struct mbuf *new_tail, *mm;
12590 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12591 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12595 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12596 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12598 (uio->uio_resid <= (int)max_len))) {
12601 if (hold_tcblock) {
12602 SCTP_TCB_UNLOCK(stcb);
12605 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12606 if ((mm == NULL) || error) {
12612 /* Update the mbuf and count */
12613 SCTP_TCB_SEND_LOCK(stcb);
12614 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12616 * we need to get out. Peer probably
12620 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12621 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12622 error = ECONNRESET;
12624 SCTP_TCB_SEND_UNLOCK(stcb);
12627 if (sp->tail_mbuf) {
12628 /* tack it to the end */
12629 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12630 sp->tail_mbuf = new_tail;
12632 /* A stolen mbuf */
12634 sp->tail_mbuf = new_tail;
12636 sctp_snd_sb_alloc(stcb, sndout);
12637 atomic_add_int(&sp->length, sndout);
12640 /* Did we reach EOR? */
12641 if ((uio->uio_resid == 0) &&
12642 ((user_marks_eor == 0) ||
12643 (srcv->sinfo_flags & SCTP_EOF) ||
12644 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
12646 sp->msg_is_complete = 1;
12648 sp->msg_is_complete = 0;
12650 SCTP_TCB_SEND_UNLOCK(stcb);
12652 if (uio->uio_resid == 0) {
12657 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12659 * This is ugly but we must assure locking
12662 if (hold_tcblock == 0) {
12663 SCTP_TCB_LOCK(stcb);
12666 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12667 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12668 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12669 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12675 SCTP_TCB_UNLOCK(stcb);
12678 /* wait for space now */
12679 if (non_blocking) {
12680 /* Non-blocking io in place out */
12683 if ((net->flight_size > net->cwnd) &&
12684 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12686 } else if (asoc->ifp_had_enobuf) {
12687 SCTP_STAT_INCR(sctps_ifnomemqueued);
12688 if (net->flight_size > (net->mtu * 2)) {
12693 asoc->ifp_had_enobuf = 0;
12694 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12695 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12697 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12698 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12699 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12701 SCTP_STAT_INCR(sctps_send_burst_avoid);
12702 } else if (net->flight_size > net->cwnd) {
12704 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12709 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12710 (stcb->asoc.total_flight > 0) &&
12711 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12712 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12716 * Ok, Nagle is set on and we have data outstanding.
12717 * Don't send anything and let SACKs drive out the
12718 * data unless wen have a "full" segment to send.
12720 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12721 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12723 SCTP_STAT_INCR(sctps_naglequeued);
12726 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12727 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12728 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12730 SCTP_STAT_INCR(sctps_naglesent);
12733 /* What about the INIT, send it maybe */
12734 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12736 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12737 nagle_applies, un_sent);
12738 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12739 stcb->asoc.total_flight,
12740 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12742 if (queue_only_for_init) {
12743 if (hold_tcblock == 0) {
12744 SCTP_TCB_LOCK(stcb);
12747 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12748 /* a collision took us forward? */
12749 queue_only_for_init = 0;
12752 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12753 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12754 queue_only_for_init = 0;
12758 if ((queue_only == 0) && (nagle_applies == 0)
12761 * need to start chunk output
12762 * before blocking.. note that if
12763 * a lock is already applied, then
12764 * the input via the net is happening
12765 * and I don't need to start output :-D
12767 if (hold_tcblock == 0) {
12768 if (SCTP_TCB_TRYLOCK(stcb)) {
12770 sctp_chunk_output(inp,
12772 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12775 sctp_chunk_output(inp,
12777 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12779 if (hold_tcblock == 1) {
12780 SCTP_TCB_UNLOCK(stcb);
12784 SOCKBUF_LOCK(&so->so_snd);
12786 * This is a bit strange, but I think it will
12787 * work. The total_output_queue_size is locked and
12788 * protected by the TCB_LOCK, which we just released.
12789 * There is a race that can occur between releasing it
12790 * above, and me getting the socket lock, where sacks
12791 * come in but we have not put the SB_WAIT on the
12792 * so_snd buffer to get the wakeup. After the LOCK
12793 * is applied the sack_processing will also need to
12794 * LOCK the so->so_snd to do the actual sowwakeup(). So
12795 * once we have the socket buffer lock if we recheck the
12796 * size we KNOW we will get to sleep safely with the
12797 * wakeup flag in place.
12799 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12800 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))
12802 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12803 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12804 so, asoc, uio->uio_resid);
12807 stcb->block_entry = &be;
12808 error = sbwait(&so->so_snd);
12809 stcb->block_entry = NULL;
12811 if (error || so->so_error || be.error) {
12814 error = so->so_error;
12819 SOCKBUF_UNLOCK(&so->so_snd);
12822 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12823 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12824 so, asoc, stcb->asoc.total_output_queue_size);
12827 SOCKBUF_UNLOCK(&so->so_snd);
12828 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12832 SCTP_TCB_SEND_LOCK(stcb);
12834 if (sp->msg_is_complete == 0) {
12835 strm->last_msg_incomplete = 1;
12836 asoc->stream_locked = 1;
12837 asoc->stream_locked_on = srcv->sinfo_stream;
12839 sp->sender_all_done = 1;
12840 strm->last_msg_incomplete = 0;
12841 asoc->stream_locked = 0;
12844 SCTP_PRINTF("Huh no sp TSNH?\n");
12845 strm->last_msg_incomplete = 0;
12846 asoc->stream_locked = 0;
12848 SCTP_TCB_SEND_UNLOCK(stcb);
12849 if (uio->uio_resid == 0) {
12850 got_all_of_the_send = 1;
12853 /* We send in a 0, since we do NOT have any locks */
12854 error = sctp_msg_append(stcb, net, top, srcv, 0);
12856 if (srcv->sinfo_flags & SCTP_EOF) {
12858 * This should only happen for Panda for the mbuf
12859 * send case, which does NOT yet support EEOR mode.
12860 * Thus, we can just set this flag to do the proper
12863 got_all_of_the_send = 1;
12871 if ((srcv->sinfo_flags & SCTP_EOF) &&
12872 (got_all_of_the_send == 1) &&
12873 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12877 SCTP_STAT_INCR(sctps_sends_with_eof);
12879 if (hold_tcblock == 0) {
12880 SCTP_TCB_LOCK(stcb);
12883 cnt = sctp_is_there_unsent_data(stcb);
12884 if (TAILQ_EMPTY(&asoc->send_queue) &&
12885 TAILQ_EMPTY(&asoc->sent_queue) &&
12887 if (asoc->locked_on_sending) {
12890 /* there is nothing queued to send, so I'm done... */
12891 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12892 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12893 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12894 /* only send SHUTDOWN the first time through */
12895 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12896 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12897 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12899 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12900 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12901 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12902 asoc->primary_destination);
12903 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12904 asoc->primary_destination);
12908 * we still got (or just got) data to send, so set
12912 * XXX sockets draft says that SCTP_EOF should be
12913 * sent with no data. currently, we will allow user
12914 * data to be sent first and move to
12917 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12918 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12919 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12920 if (hold_tcblock == 0) {
12921 SCTP_TCB_LOCK(stcb);
12924 if (asoc->locked_on_sending) {
12925 /* Locked to send out the data */
12926 struct sctp_stream_queue_pending *sp;
12928 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12930 if ((sp->length == 0) && (sp->msg_is_complete == 0))
12931 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12934 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12935 if (TAILQ_EMPTY(&asoc->send_queue) &&
12936 TAILQ_EMPTY(&asoc->sent_queue) &&
12937 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12939 if (free_cnt_applied) {
12940 atomic_add_int(&stcb->asoc.refcnt, -1);
12941 free_cnt_applied = 0;
12943 sctp_abort_an_association(stcb->sctp_ep, stcb,
12944 SCTP_RESPONSE_TO_USER_REQ,
12945 NULL, SCTP_SO_LOCKED);
12947 * now relock the stcb so everything
12954 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12955 asoc->primary_destination);
12956 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12961 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12962 some_on_control = 1;
12964 if ((net->flight_size > net->cwnd) &&
12965 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12967 } else if (asoc->ifp_had_enobuf) {
12968 SCTP_STAT_INCR(sctps_ifnomemqueued);
12969 if (net->flight_size > (net->mtu * 2)) {
12974 asoc->ifp_had_enobuf = 0;
12975 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12976 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12978 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12979 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12980 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12982 SCTP_STAT_INCR(sctps_send_burst_avoid);
12983 } else if (net->flight_size > net->cwnd) {
12985 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12990 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12991 (stcb->asoc.total_flight > 0) &&
12992 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12993 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12996 * Ok, Nagle is set on and we have data outstanding.
12997 * Don't send anything and let SACKs drive out the
12998 * data unless wen have a "full" segment to send.
13000 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13001 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13003 SCTP_STAT_INCR(sctps_naglequeued);
13006 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13007 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13008 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13010 SCTP_STAT_INCR(sctps_naglesent);
13013 if (queue_only_for_init) {
13014 if (hold_tcblock == 0) {
13015 SCTP_TCB_LOCK(stcb);
13018 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13019 /* a collision took us forward? */
13020 queue_only_for_init = 0;
13023 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13024 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13025 queue_only_for_init = 0;
13029 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13030 /* we can attempt to send too. */
13031 if (hold_tcblock == 0) {
13033 * If there is activity recv'ing sacks no need to
13036 if (SCTP_TCB_TRYLOCK(stcb)) {
13037 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13041 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13043 } else if ((queue_only == 0) &&
13044 (stcb->asoc.peers_rwnd == 0) &&
13045 (stcb->asoc.total_flight == 0)) {
13046 /* We get to have a probe outstanding */
13047 if (hold_tcblock == 0) {
13049 SCTP_TCB_LOCK(stcb);
13051 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13052 } else if (some_on_control) {
13053 int num_out, reason, cwnd_full, frag_point;
13055 /* Here we do control only */
13056 if (hold_tcblock == 0) {
13058 SCTP_TCB_LOCK(stcb);
13060 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13061 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13062 &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13064 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d",
13065 queue_only, stcb->asoc.peers_rwnd, un_sent,
13066 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13067 stcb->asoc.total_output_queue_size, error);
13072 if (local_soresv && stcb) {
13073 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13076 if (create_lock_applied) {
13077 SCTP_ASOC_CREATE_UNLOCK(inp);
13078 create_lock_applied = 0;
13080 if ((stcb) && hold_tcblock) {
13081 SCTP_TCB_UNLOCK(stcb);
13083 if (stcb && free_cnt_applied) {
13084 atomic_add_int(&stcb->asoc.refcnt, -1);
13088 if (mtx_owned(&stcb->tcb_mtx)) {
13089 panic("Leaving with tcb mtx owned?");
13091 if (mtx_owned(&stcb->tcb_send_mtx)) {
13092 panic("Leaving with tcb send mtx owned?");
13100 sctp_m_freem(control);
13107 * generate an AUTHentication chunk, if required
13110 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13111 struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13112 struct sctp_tcb *stcb, uint8_t chunk)
13114 struct mbuf *m_auth;
13115 struct sctp_auth_chunk *auth;
13118 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13122 /* sysctl disabled auth? */
13123 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13126 /* peer doesn't do auth... */
13127 if (!stcb->asoc.peer_supports_auth) {
13130 /* does the requested chunk require auth? */
13131 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13134 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13135 if (m_auth == NULL) {
13139 /* reserve some space if this will be the first mbuf */
13141 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13142 /* fill in the AUTH chunk details */
13143 auth = mtod(m_auth, struct sctp_auth_chunk *);
13144 bzero(auth, sizeof(*auth));
13145 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13146 auth->ch.chunk_flags = 0;
13147 chunk_len = sizeof(*auth) +
13148 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13149 auth->ch.chunk_length = htons(chunk_len);
13150 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13151 /* key id and hmac digest will be computed and filled in upon send */
13153 /* save the offset where the auth was inserted into the chain */
13160 *offset += SCTP_BUF_LEN(cn);
13161 cn = SCTP_BUF_NEXT(cn);
13166 /* update length and return pointer to the auth chunk */
13167 SCTP_BUF_LEN(m_auth) = chunk_len;
13168 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13169 if (auth_ret != NULL)
13177 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13179 struct nd_prefix *pfx = NULL;
13180 struct nd_pfxrouter *pfxrtr = NULL;
13181 struct sockaddr_in6 gw6;
13183 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13186 /* get prefix entry of address */
13187 LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
13188 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13190 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13191 &src6->sin6_addr, &pfx->ndpr_mask))
13194 /* no prefix entry in the prefix list */
13196 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13197 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13200 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13201 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13203 /* search installed gateway from prefix entry */
13204 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13205 pfxrtr->pfr_next) {
13206 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13207 gw6.sin6_family = AF_INET6;
13208 gw6.sin6_len = sizeof(struct sockaddr_in6);
13209 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13210 sizeof(struct in6_addr));
13211 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13212 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13213 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13214 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13215 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13216 ro->ro_rt->rt_gateway)) {
13217 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13221 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13228 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13230 struct sockaddr_in *sin, *mask;
13231 struct ifaddr *ifa;
13232 struct in_addr srcnetaddr, gwnetaddr;
13234 if (ro == NULL || ro->ro_rt == NULL ||
13235 sifa->address.sa.sa_family != AF_INET) {
13238 ifa = (struct ifaddr *)sifa->ifa;
13239 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13240 sin = (struct sockaddr_in *)&sifa->address.sin;
13241 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13242 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13243 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13244 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13246 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13247 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13248 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13249 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13250 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13251 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {