michael@0: /*- michael@0: * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: __FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 262252 2014-02-20 20:14:43Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #if defined(__Userspace_os_Linux) michael@0: #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */ michael@0: #endif michael@0: #if !defined(__Userspace_os_Windows) michael@0: #include michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: #include michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: #include michael@0: #endif michael@0: #include michael@0: #endif michael@0: #if defined(__Userspace__) && defined(INET6) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 3 michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #if !(defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)) michael@0: #define SCTP_MAX_LINKHDR 16 michael@0: #endif michael@0: #endif michael@0: michael@0: #define SCTP_MAX_GAPS_INARRAY 4 michael@0: struct sack_track { michael@0: uint8_t right_edge; /* mergable on the right edge */ michael@0: uint8_t left_edge; /* mergable on the left edge */ michael@0: uint8_t num_entries; michael@0: uint8_t spare; michael@0: struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY]; michael@0: }; michael@0: michael@0: struct sack_track sack_array[256] = { michael@0: {0, 0, 0, 0, /* 0x00 */ michael@0: {{0, 0}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x01 */ michael@0: {{0, 0}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x02 */ michael@0: {{1, 1}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x03 */ michael@0: {{0, 1}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x04 */ michael@0: {{2, 2}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x05 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x06 */ michael@0: {{1, 2}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x07 */ michael@0: {{0, 2}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x08 */ michael@0: {{3, 3}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x09 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x0a */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x0b */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x0c */ michael@0: {{2, 3}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x0d */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x0e */ michael@0: {{1, 3}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x0f */ michael@0: {{0, 3}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x10 */ michael@0: {{4, 4}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x11 */ michael@0: {{0, 0}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x12 */ michael@0: {{1, 1}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x13 */ michael@0: {{0, 1}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x14 */ michael@0: {{2, 2}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x15 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 4}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x16 */ michael@0: {{1, 2}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x17 */ michael@0: {{0, 2}, michael@0: {4, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x18 */ michael@0: {{3, 4}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x19 */ michael@0: {{0, 0}, michael@0: {3, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x1a */ michael@0: {{1, 1}, michael@0: {3, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x1b */ michael@0: {{0, 1}, michael@0: {3, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x1c */ michael@0: {{2, 4}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x1d */ michael@0: {{0, 0}, michael@0: {2, 4}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x1e */ michael@0: {{1, 4}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x1f */ michael@0: {{0, 4}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x20 */ michael@0: {{5, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x21 */ michael@0: {{0, 0}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x22 */ michael@0: {{1, 1}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x23 */ michael@0: {{0, 1}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x24 */ michael@0: {{2, 2}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x25 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {5, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x26 */ michael@0: {{1, 2}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x27 */ michael@0: {{0, 2}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x28 */ michael@0: {{3, 3}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x29 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x2a */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x2b */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x2c */ michael@0: {{2, 3}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x2d */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {5, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x2e */ michael@0: {{1, 3}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x2f */ michael@0: {{0, 3}, michael@0: {5, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x30 */ michael@0: {{4, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x31 */ michael@0: {{0, 0}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x32 */ michael@0: {{1, 1}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x33 */ michael@0: {{0, 1}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x34 */ michael@0: {{2, 2}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x35 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 5}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x36 */ michael@0: {{1, 2}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x37 */ michael@0: {{0, 2}, michael@0: {4, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x38 */ michael@0: {{3, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x39 */ michael@0: {{0, 0}, michael@0: {3, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x3a */ michael@0: {{1, 1}, michael@0: {3, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x3b */ michael@0: {{0, 1}, michael@0: {3, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x3c */ michael@0: {{2, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x3d */ michael@0: {{0, 0}, michael@0: {2, 5}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x3e */ michael@0: {{1, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x3f */ michael@0: {{0, 5}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x40 */ michael@0: {{6, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x41 */ michael@0: {{0, 0}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x42 */ michael@0: {{1, 1}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x43 */ michael@0: {{0, 1}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x44 */ michael@0: {{2, 2}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x45 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x46 */ michael@0: {{1, 2}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x47 */ michael@0: {{0, 2}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x48 */ michael@0: {{3, 3}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x49 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x4a */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x4b */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x4c */ michael@0: {{2, 3}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x4d */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x4e */ michael@0: {{1, 3}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x4f */ michael@0: {{0, 3}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x50 */ michael@0: {{4, 4}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x51 */ michael@0: {{0, 0}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x52 */ michael@0: {{1, 1}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x53 */ michael@0: {{0, 1}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x54 */ michael@0: {{2, 2}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 4, 0, /* 0x55 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 4}, michael@0: {6, 6} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x56 */ michael@0: {{1, 2}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x57 */ michael@0: {{0, 2}, michael@0: {4, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x58 */ michael@0: {{3, 4}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x59 */ michael@0: {{0, 0}, michael@0: {3, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x5a */ michael@0: {{1, 1}, michael@0: {3, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x5b */ michael@0: {{0, 1}, michael@0: {3, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x5c */ michael@0: {{2, 4}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x5d */ michael@0: {{0, 0}, michael@0: {2, 4}, michael@0: {6, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x5e */ michael@0: {{1, 4}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x5f */ michael@0: {{0, 4}, michael@0: {6, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x60 */ michael@0: {{5, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x61 */ michael@0: {{0, 0}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x62 */ michael@0: {{1, 1}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x63 */ michael@0: {{0, 1}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x64 */ michael@0: {{2, 2}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x65 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {5, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x66 */ michael@0: {{1, 2}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x67 */ michael@0: {{0, 2}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x68 */ michael@0: {{3, 3}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x69 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {5, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 3, 0, /* 0x6a */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {5, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x6b */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {5, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x6c */ michael@0: {{2, 3}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x6d */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {5, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x6e */ michael@0: {{1, 3}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x6f */ michael@0: {{0, 3}, michael@0: {5, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x70 */ michael@0: {{4, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x71 */ michael@0: {{0, 0}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x72 */ michael@0: {{1, 1}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x73 */ michael@0: {{0, 1}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x74 */ michael@0: {{2, 2}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 3, 0, /* 0x75 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 6}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x76 */ michael@0: {{1, 2}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x77 */ michael@0: {{0, 2}, michael@0: {4, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x78 */ michael@0: {{3, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x79 */ michael@0: {{0, 0}, michael@0: {3, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 2, 0, /* 0x7a */ michael@0: {{1, 1}, michael@0: {3, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x7b */ michael@0: {{0, 1}, michael@0: {3, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x7c */ michael@0: {{2, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 2, 0, /* 0x7d */ michael@0: {{0, 0}, michael@0: {2, 6}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 0, 1, 0, /* 0x7e */ michael@0: {{1, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 0, 1, 0, /* 0x7f */ michael@0: {{0, 6}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0x80 */ michael@0: {{7, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0x81 */ michael@0: {{0, 0}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x82 */ michael@0: {{1, 1}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0x83 */ michael@0: {{0, 1}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x84 */ michael@0: {{2, 2}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x85 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x86 */ michael@0: {{1, 2}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0x87 */ michael@0: {{0, 2}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x88 */ michael@0: {{3, 3}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x89 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0x8a */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x8b */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x8c */ michael@0: {{2, 3}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x8d */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x8e */ michael@0: {{1, 3}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0x8f */ michael@0: {{0, 3}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x90 */ michael@0: {{4, 4}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x91 */ michael@0: {{0, 0}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0x92 */ michael@0: {{1, 1}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x93 */ michael@0: {{0, 1}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0x94 */ michael@0: {{2, 2}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0x95 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 4}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0x96 */ michael@0: {{1, 2}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x97 */ michael@0: {{0, 2}, michael@0: {4, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x98 */ michael@0: {{3, 4}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x99 */ michael@0: {{0, 0}, michael@0: {3, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0x9a */ michael@0: {{1, 1}, michael@0: {3, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x9b */ michael@0: {{0, 1}, michael@0: {3, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x9c */ michael@0: {{2, 4}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0x9d */ michael@0: {{0, 0}, michael@0: {2, 4}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0x9e */ michael@0: {{1, 4}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0x9f */ michael@0: {{0, 4}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xa0 */ michael@0: {{5, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xa1 */ michael@0: {{0, 0}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xa2 */ michael@0: {{1, 1}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xa3 */ michael@0: {{0, 1}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xa4 */ michael@0: {{2, 2}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xa5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {5, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xa6 */ michael@0: {{1, 2}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xa7 */ michael@0: {{0, 2}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xa8 */ michael@0: {{3, 3}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xa9 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 4, 0, /* 0xaa */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xab */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {5, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xac */ michael@0: {{2, 3}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xad */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {5, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xae */ michael@0: {{1, 3}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xaf */ michael@0: {{0, 3}, michael@0: {5, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xb0 */ michael@0: {{4, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xb1 */ michael@0: {{0, 0}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xb2 */ michael@0: {{1, 1}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xb3 */ michael@0: {{0, 1}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xb4 */ michael@0: {{2, 2}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xb5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 5}, michael@0: {7, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xb6 */ michael@0: {{1, 2}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xb7 */ michael@0: {{0, 2}, michael@0: {4, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xb8 */ michael@0: {{3, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xb9 */ michael@0: {{0, 0}, michael@0: {3, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xba */ michael@0: {{1, 1}, michael@0: {3, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xbb */ michael@0: {{0, 1}, michael@0: {3, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xbc */ michael@0: {{2, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xbd */ michael@0: {{0, 0}, michael@0: {2, 5}, michael@0: {7, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xbe */ michael@0: {{1, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xbf */ michael@0: {{0, 5}, michael@0: {7, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xc0 */ michael@0: {{6, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xc1 */ michael@0: {{0, 0}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xc2 */ michael@0: {{1, 1}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xc3 */ michael@0: {{0, 1}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xc4 */ michael@0: {{2, 2}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xc5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xc6 */ michael@0: {{1, 2}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xc7 */ michael@0: {{0, 2}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xc8 */ michael@0: {{3, 3}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xc9 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xca */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xcb */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xcc */ michael@0: {{2, 3}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xcd */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xce */ michael@0: {{1, 3}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xcf */ michael@0: {{0, 3}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xd0 */ michael@0: {{4, 4}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xd1 */ michael@0: {{0, 0}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xd2 */ michael@0: {{1, 1}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xd3 */ michael@0: {{0, 1}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xd4 */ michael@0: {{2, 2}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 4, 0, /* 0xd5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 4}, michael@0: {6, 7} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xd6 */ michael@0: {{1, 2}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xd7 */ michael@0: {{0, 2}, michael@0: {4, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xd8 */ michael@0: {{3, 4}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xd9 */ michael@0: {{0, 0}, michael@0: {3, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xda */ michael@0: {{1, 1}, michael@0: {3, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xdb */ michael@0: {{0, 1}, michael@0: {3, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xdc */ michael@0: {{2, 4}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xdd */ michael@0: {{0, 0}, michael@0: {2, 4}, michael@0: {6, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xde */ michael@0: {{1, 4}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xdf */ michael@0: {{0, 4}, michael@0: {6, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xe0 */ michael@0: {{5, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xe1 */ michael@0: {{0, 0}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xe2 */ michael@0: {{1, 1}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xe3 */ michael@0: {{0, 1}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xe4 */ michael@0: {{2, 2}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xe5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {5, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xe6 */ michael@0: {{1, 2}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xe7 */ michael@0: {{0, 2}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xe8 */ michael@0: {{3, 3}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xe9 */ michael@0: {{0, 0}, michael@0: {3, 3}, michael@0: {5, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 3, 0, /* 0xea */ michael@0: {{1, 1}, michael@0: {3, 3}, michael@0: {5, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xeb */ michael@0: {{0, 1}, michael@0: {3, 3}, michael@0: {5, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xec */ michael@0: {{2, 3}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xed */ michael@0: {{0, 0}, michael@0: {2, 3}, michael@0: {5, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xee */ michael@0: {{1, 3}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xef */ michael@0: {{0, 3}, michael@0: {5, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xf0 */ michael@0: {{4, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xf1 */ michael@0: {{0, 0}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xf2 */ michael@0: {{1, 1}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xf3 */ michael@0: {{0, 1}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xf4 */ michael@0: {{2, 2}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 3, 0, /* 0xf5 */ michael@0: {{0, 0}, michael@0: {2, 2}, michael@0: {4, 7}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xf6 */ michael@0: {{1, 2}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xf7 */ michael@0: {{0, 2}, michael@0: {4, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xf8 */ michael@0: {{3, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xf9 */ michael@0: {{0, 0}, michael@0: {3, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 2, 0, /* 0xfa */ michael@0: {{1, 1}, michael@0: {3, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xfb */ michael@0: {{0, 1}, michael@0: {3, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xfc */ michael@0: {{2, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 2, 0, /* 0xfd */ michael@0: {{0, 0}, michael@0: {2, 7}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {0, 1, 1, 0, /* 0xfe */ michael@0: {{1, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: }, michael@0: {1, 1, 1, 0, /* 0xff */ michael@0: {{0, 7}, michael@0: {0, 0}, michael@0: {0, 0}, michael@0: {0, 0} michael@0: } michael@0: } michael@0: }; michael@0: michael@0: michael@0: int michael@0: sctp_is_address_in_scope(struct sctp_ifa *ifa, michael@0: struct sctp_scoping *scope, michael@0: int do_update) michael@0: { michael@0: if ((scope->loopback_scope == 0) && michael@0: (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) { michael@0: /* michael@0: * skip loopback if not in scope * michael@0: */ michael@0: return (0); michael@0: } michael@0: switch (ifa->address.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (scope->ipv4_addr_legal) { michael@0: struct sockaddr_in *sin; michael@0: michael@0: sin = (struct sockaddr_in *)&ifa->address.sin; michael@0: if (sin->sin_addr.s_addr == 0) { michael@0: /* not in scope , unspecified */ michael@0: return (0); michael@0: } michael@0: if ((scope->ipv4_local_scope == 0) && michael@0: (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { michael@0: /* private address not in scope */ michael@0: return (0); michael@0: } michael@0: } else { michael@0: return (0); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (scope->ipv6_addr_legal) { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: #if !defined(__Panda__) michael@0: /* Must update the flags, bummer, which michael@0: * means any IFA locks must now be applied HERE <-> michael@0: */ michael@0: if (do_update) { michael@0: sctp_gather_internal_ifa_flags(ifa); michael@0: } michael@0: #endif michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: return (0); michael@0: } michael@0: /* ok to use deprecated addresses? */ michael@0: sin6 = (struct sockaddr_in6 *)&ifa->address.sin6; michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { michael@0: /* skip unspecifed addresses */ michael@0: return (0); michael@0: } michael@0: if ( /* (local_scope == 0) && */ michael@0: (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) { michael@0: return (0); michael@0: } michael@0: if ((scope->site_scope == 0) && michael@0: (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { michael@0: return (0); michael@0: } michael@0: } else { michael@0: return (0); michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (!scope->conn_addr_legal) { michael@0: return (0); michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: return (0); michael@0: } michael@0: return (1); michael@0: } michael@0: michael@0: static struct mbuf * michael@0: sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len) michael@0: { michael@0: #if defined(INET) || defined(INET6) michael@0: struct sctp_paramhdr *parmh; michael@0: struct mbuf *mret; michael@0: uint16_t plen; michael@0: #endif michael@0: michael@0: switch (ifa->address.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: plen = (uint16_t)sizeof(struct sctp_ipv4addr_param); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: plen = (uint16_t)sizeof(struct sctp_ipv6addr_param); michael@0: break; michael@0: #endif michael@0: default: michael@0: return (m); michael@0: } michael@0: #if defined(INET) || defined(INET6) michael@0: if (M_TRAILINGSPACE(m) >= plen) { michael@0: /* easy side we just drop it on the end */ michael@0: parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m))); michael@0: mret = m; michael@0: } else { michael@0: /* Need more space */ michael@0: mret = m; michael@0: while (SCTP_BUF_NEXT(mret) != NULL) { michael@0: mret = SCTP_BUF_NEXT(mret); michael@0: } michael@0: SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (SCTP_BUF_NEXT(mret) == NULL) { michael@0: /* We are hosed, can't add more addresses */ michael@0: return (m); michael@0: } michael@0: mret = SCTP_BUF_NEXT(mret); michael@0: parmh = mtod(mret, struct sctp_paramhdr *); michael@0: } michael@0: /* now add the parameter */ michael@0: switch (ifa->address.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sctp_ipv4addr_param *ipv4p; michael@0: struct sockaddr_in *sin; michael@0: michael@0: sin = (struct sockaddr_in *)&ifa->address.sin; michael@0: ipv4p = (struct sctp_ipv4addr_param *)parmh; michael@0: parmh->param_type = htons(SCTP_IPV4_ADDRESS); michael@0: parmh->param_length = htons(plen); michael@0: ipv4p->addr = sin->sin_addr.s_addr; michael@0: SCTP_BUF_LEN(mret) += plen; michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sctp_ipv6addr_param *ipv6p; michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&ifa->address.sin6; michael@0: ipv6p = (struct sctp_ipv6addr_param *)parmh; michael@0: parmh->param_type = htons(SCTP_IPV6_ADDRESS); michael@0: parmh->param_length = htons(plen); michael@0: memcpy(ipv6p->addr, &sin6->sin6_addr, michael@0: sizeof(ipv6p->addr)); michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) michael@0: /* clear embedded scope in the address */ michael@0: in6_clearscope((struct in6_addr *)ipv6p->addr); michael@0: #endif michael@0: SCTP_BUF_LEN(mret) += plen; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: return (m); michael@0: } michael@0: if (len != NULL) { michael@0: *len += plen; michael@0: } michael@0: return (mret); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: struct mbuf * michael@0: sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct sctp_scoping *scope, michael@0: struct mbuf *m_at, int cnt_inits_to, michael@0: uint16_t *padding_len, uint16_t *chunk_len) michael@0: { michael@0: struct sctp_vrf *vrf = NULL; michael@0: int cnt, limit_out = 0, total_count; michael@0: uint32_t vrf_id; michael@0: michael@0: vrf_id = inp->def_vrf_id; michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (m_at); michael@0: } michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: struct sctp_ifa *sctp_ifap; michael@0: struct sctp_ifn *sctp_ifnp; michael@0: michael@0: cnt = cnt_inits_to; michael@0: if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) { michael@0: limit_out = 1; michael@0: cnt = SCTP_ADDRESS_LIMIT; michael@0: goto skip_count; michael@0: } michael@0: LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { michael@0: if ((scope->loopback_scope == 0) && michael@0: SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { michael@0: /* michael@0: * Skip loopback devices if loopback_scope michael@0: * not set michael@0: */ michael@0: continue; michael@0: } michael@0: LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { michael@0: if (sctp_is_addr_restricted(stcb, sctp_ifap)) { michael@0: continue; michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (sctp_ifap->address.sa.sa_family == AF_CONN) { michael@0: continue; michael@0: } michael@0: #endif michael@0: if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) { michael@0: continue; michael@0: } michael@0: cnt++; michael@0: if (cnt > SCTP_ADDRESS_LIMIT) { michael@0: break; michael@0: } michael@0: } michael@0: if (cnt > SCTP_ADDRESS_LIMIT) { michael@0: break; michael@0: } michael@0: } michael@0: skip_count: michael@0: if (cnt > 1) { michael@0: total_count = 0; michael@0: LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { michael@0: cnt = 0; michael@0: if ((scope->loopback_scope == 0) && michael@0: SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { michael@0: /* michael@0: * Skip loopback devices if michael@0: * loopback_scope not set michael@0: */ michael@0: continue; michael@0: } michael@0: LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { michael@0: if (sctp_is_addr_restricted(stcb, sctp_ifap)) { michael@0: continue; michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (sctp_ifap->address.sa.sa_family == AF_CONN) { michael@0: continue; michael@0: } michael@0: #endif michael@0: if (sctp_is_address_in_scope(sctp_ifap, michael@0: scope, 0) == 0) { michael@0: continue; michael@0: } michael@0: if ((chunk_len != NULL) && michael@0: (padding_len != NULL) && michael@0: (*padding_len > 0)) { michael@0: memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); michael@0: SCTP_BUF_LEN(m_at) += *padding_len; michael@0: *chunk_len += *padding_len; michael@0: *padding_len = 0; michael@0: } michael@0: m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len); michael@0: if (limit_out) { michael@0: cnt++; michael@0: total_count++; michael@0: if (cnt >= 2) { michael@0: /* two from each address */ michael@0: break; michael@0: } michael@0: if (total_count > SCTP_ADDRESS_LIMIT) { michael@0: /* No more addresses */ michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: cnt = cnt_inits_to; michael@0: /* First, how many ? */ michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: continue; michael@0: } michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) michael@0: /* Address being deleted by the system, dont michael@0: * list. michael@0: */ michael@0: continue; michael@0: if (laddr->action == SCTP_DEL_IP_ADDRESS) { michael@0: /* Address being deleted on this ep michael@0: * don't list. michael@0: */ michael@0: continue; michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (laddr->ifa->address.sa.sa_family == AF_CONN) { michael@0: continue; michael@0: } michael@0: #endif michael@0: if (sctp_is_address_in_scope(laddr->ifa, michael@0: scope, 1) == 0) { michael@0: continue; michael@0: } michael@0: cnt++; michael@0: } michael@0: /* michael@0: * To get through a NAT we only list addresses if we have michael@0: * more than one. That way if you just bind a single address michael@0: * we let the source of the init dictate our address. michael@0: */ michael@0: if (cnt > 1) { michael@0: cnt = cnt_inits_to; michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: continue; michael@0: } michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { michael@0: continue; michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (laddr->ifa->address.sa.sa_family == AF_CONN) { michael@0: continue; michael@0: } michael@0: #endif michael@0: if (sctp_is_address_in_scope(laddr->ifa, michael@0: scope, 0) == 0) { michael@0: continue; michael@0: } michael@0: if ((chunk_len != NULL) && michael@0: (padding_len != NULL) && michael@0: (*padding_len > 0)) { michael@0: memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); michael@0: SCTP_BUF_LEN(m_at) += *padding_len; michael@0: *chunk_len += *padding_len; michael@0: *padding_len = 0; michael@0: } michael@0: m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len); michael@0: cnt++; michael@0: if (cnt >= SCTP_ADDRESS_LIMIT) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (m_at); michael@0: } michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa, michael@0: uint8_t dest_is_loop, michael@0: uint8_t dest_is_priv, michael@0: sa_family_t fam) michael@0: { michael@0: uint8_t dest_is_global = 0; michael@0: /* dest_is_priv is true if destination is a private address */ michael@0: /* dest_is_loop is true if destination is a loopback addresses */ michael@0: michael@0: /** michael@0: * Here we determine if its a preferred address. A preferred address michael@0: * means it is the same scope or higher scope then the destination. michael@0: * L = loopback, P = private, G = global michael@0: * ----------------------------------------- michael@0: * src | dest | result michael@0: * ---------------------------------------- michael@0: * L | L | yes michael@0: * ----------------------------------------- michael@0: * P | L | yes-v4 no-v6 michael@0: * ----------------------------------------- michael@0: * G | L | yes-v4 no-v6 michael@0: * ----------------------------------------- michael@0: * L | P | no michael@0: * ----------------------------------------- michael@0: * P | P | yes michael@0: * ----------------------------------------- michael@0: * G | P | no michael@0: * ----------------------------------------- michael@0: * L | G | no michael@0: * ----------------------------------------- michael@0: * P | G | no michael@0: * ----------------------------------------- michael@0: * G | G | yes michael@0: * ----------------------------------------- michael@0: */ michael@0: michael@0: if (ifa->address.sa.sa_family != fam) { michael@0: /* forget mis-matched family */ michael@0: return (NULL); michael@0: } michael@0: if ((dest_is_priv == 0) && (dest_is_loop == 0)) { michael@0: dest_is_global = 1; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:"); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa); michael@0: /* Ok the address may be ok */ michael@0: #ifdef INET6 michael@0: if (fam == AF_INET6) { michael@0: /* ok to use deprecated addresses? no lets not! */ michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n"); michael@0: return (NULL); michael@0: } michael@0: if (ifa->src_is_priv && !ifa->src_is_loop) { michael@0: if (dest_is_loop) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n"); michael@0: return (NULL); michael@0: } michael@0: } michael@0: if (ifa->src_is_glob) { michael@0: if (dest_is_loop) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n"); michael@0: return (NULL); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: /* Now that we know what is what, implement or table michael@0: * this could in theory be done slicker (it used to be), but this michael@0: * is straightforward and easier to validate :-) michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n", michael@0: ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n", michael@0: dest_is_loop, dest_is_priv, dest_is_global); michael@0: michael@0: if ((ifa->src_is_loop) && (dest_is_priv)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n"); michael@0: return (NULL); michael@0: } michael@0: if ((ifa->src_is_glob) && (dest_is_priv)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n"); michael@0: return (NULL); michael@0: } michael@0: if ((ifa->src_is_loop) && (dest_is_global)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n"); michael@0: return (NULL); michael@0: } michael@0: if ((ifa->src_is_priv) && (dest_is_global)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n"); michael@0: return (NULL); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n"); michael@0: /* its a preferred address */ michael@0: return (ifa); michael@0: } michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa, michael@0: uint8_t dest_is_loop, michael@0: uint8_t dest_is_priv, michael@0: sa_family_t fam) michael@0: { michael@0: uint8_t dest_is_global = 0; michael@0: michael@0: /** michael@0: * Here we determine if its a acceptable address. A acceptable michael@0: * address means it is the same scope or higher scope but we can michael@0: * allow for NAT which means its ok to have a global dest and a michael@0: * private src. michael@0: * michael@0: * L = loopback, P = private, G = global michael@0: * ----------------------------------------- michael@0: * src | dest | result michael@0: * ----------------------------------------- michael@0: * L | L | yes michael@0: * ----------------------------------------- michael@0: * P | L | yes-v4 no-v6 michael@0: * ----------------------------------------- michael@0: * G | L | yes michael@0: * ----------------------------------------- michael@0: * L | P | no michael@0: * ----------------------------------------- michael@0: * P | P | yes michael@0: * ----------------------------------------- michael@0: * G | P | yes - May not work michael@0: * ----------------------------------------- michael@0: * L | G | no michael@0: * ----------------------------------------- michael@0: * P | G | yes - May not work michael@0: * ----------------------------------------- michael@0: * G | G | yes michael@0: * ----------------------------------------- michael@0: */ michael@0: michael@0: if (ifa->address.sa.sa_family != fam) { michael@0: /* forget non matching family */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n", michael@0: ifa->address.sa.sa_family, fam); michael@0: return (NULL); michael@0: } michael@0: /* Ok the address may be ok */ michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n", michael@0: dest_is_loop, dest_is_priv); michael@0: if ((dest_is_loop == 0) && (dest_is_priv == 0)) { michael@0: dest_is_global = 1; michael@0: } michael@0: #ifdef INET6 michael@0: if (fam == AF_INET6) { michael@0: /* ok to use deprecated addresses? */ michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: return (NULL); michael@0: } michael@0: if (ifa->src_is_priv) { michael@0: /* Special case, linklocal to loop */ michael@0: if (dest_is_loop) michael@0: return (NULL); michael@0: } michael@0: } michael@0: #endif michael@0: /* michael@0: * Now that we know what is what, implement our table. michael@0: * This could in theory be done slicker (it used to be), but this michael@0: * is straightforward and easier to validate :-) michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n", michael@0: ifa->src_is_loop, michael@0: dest_is_priv); michael@0: if ((ifa->src_is_loop == 1) && (dest_is_priv)) { michael@0: return (NULL); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n", michael@0: ifa->src_is_loop, michael@0: dest_is_global); michael@0: if ((ifa->src_is_loop == 1) && (dest_is_global)) { michael@0: return (NULL); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n"); michael@0: /* its an acceptable address */ michael@0: return (ifa); michael@0: } michael@0: michael@0: int michael@0: sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: if (stcb == NULL) { michael@0: /* There are no restrictions, no TCB :-) */ michael@0: return (0); michael@0: } michael@0: LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", michael@0: __FUNCTION__); michael@0: continue; michael@0: } michael@0: if (laddr->ifa == ifa) { michael@0: /* Yes it is on the list */ michael@0: return (1); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: if (ifa == NULL) michael@0: return (0); michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", michael@0: __FUNCTION__); michael@0: continue; michael@0: } michael@0: if ((laddr->ifa == ifa) && laddr->action == 0) michael@0: /* same pointer */ michael@0: return (1); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_choose_boundspecific_inp(struct sctp_inpcb *inp, michael@0: sctp_route_t *ro, michael@0: uint32_t vrf_id, michael@0: int non_asoc_addr_ok, michael@0: uint8_t dest_is_priv, michael@0: uint8_t dest_is_loop, michael@0: sa_family_t fam) michael@0: { michael@0: struct sctp_laddr *laddr, *starting_point; michael@0: void *ifn; michael@0: int resettotop = 0; michael@0: struct sctp_ifn *sctp_ifn; michael@0: struct sctp_ifa *sctp_ifa, *sifa; michael@0: struct sctp_vrf *vrf; michael@0: uint32_t ifn_index; michael@0: michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) michael@0: return (NULL); michael@0: michael@0: ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); michael@0: ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); michael@0: sctp_ifn = sctp_find_ifn(ifn, ifn_index); michael@0: /* michael@0: * first question, is the ifn we will emit on in our list, if so, we michael@0: * want such an address. Note that we first looked for a michael@0: * preferred address. michael@0: */ michael@0: if (sctp_ifn) { michael@0: /* is a preferred one on the interface we route out? */ michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: sifa = sctp_is_ifa_addr_preferred(sctp_ifa, michael@0: dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (sctp_is_addr_in_ep(inp, sifa)) { michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: } michael@0: } michael@0: /* michael@0: * ok, now we now need to find one on the list of the addresses. michael@0: * We can't get one on the emitting interface so let's find first michael@0: * a preferred one. If not that an acceptable one otherwise... michael@0: * we return NULL. michael@0: */ michael@0: starting_point = inp->next_addr_touse; michael@0: once_again: michael@0: if (inp->next_addr_touse == NULL) { michael@0: inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); michael@0: resettotop = 1; michael@0: } michael@0: for (laddr = inp->next_addr_touse; laddr; michael@0: laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { michael@0: if (laddr->ifa == NULL) { michael@0: /* address has been removed */ michael@0: continue; michael@0: } michael@0: if (laddr->action == SCTP_DEL_IP_ADDRESS) { michael@0: /* address is being deleted */ michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: if (resettotop == 0) { michael@0: inp->next_addr_touse = NULL; michael@0: goto once_again; michael@0: } michael@0: michael@0: inp->next_addr_touse = starting_point; michael@0: resettotop = 0; michael@0: once_again_too: michael@0: if (inp->next_addr_touse == NULL) { michael@0: inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); michael@0: resettotop = 1; michael@0: } michael@0: michael@0: /* ok, what about an acceptable address in the inp */ michael@0: for (laddr = inp->next_addr_touse; laddr; michael@0: laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { michael@0: if (laddr->ifa == NULL) { michael@0: /* address has been removed */ michael@0: continue; michael@0: } michael@0: if (laddr->action == SCTP_DEL_IP_ADDRESS) { michael@0: /* address is being deleted */ michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: if (resettotop == 0) { michael@0: inp->next_addr_touse = NULL; michael@0: goto once_again_too; michael@0: } michael@0: michael@0: /* michael@0: * no address bound can be a source for the destination we are in michael@0: * trouble michael@0: */ michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: sctp_route_t *ro, michael@0: uint32_t vrf_id, michael@0: uint8_t dest_is_priv, michael@0: uint8_t dest_is_loop, michael@0: int non_asoc_addr_ok, michael@0: sa_family_t fam) michael@0: { michael@0: struct sctp_laddr *laddr, *starting_point; michael@0: void *ifn; michael@0: struct sctp_ifn *sctp_ifn; michael@0: struct sctp_ifa *sctp_ifa, *sifa; michael@0: uint8_t start_at_beginning = 0; michael@0: struct sctp_vrf *vrf; michael@0: uint32_t ifn_index; michael@0: michael@0: /* michael@0: * first question, is the ifn we will emit on in our list, if so, we michael@0: * want that one. michael@0: */ michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) michael@0: return (NULL); michael@0: michael@0: ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); michael@0: ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); michael@0: sctp_ifn = sctp_find_ifn( ifn, ifn_index); michael@0: michael@0: /* michael@0: * first question, is the ifn we will emit on in our list? If so, michael@0: * we want that one. First we look for a preferred. Second, we go michael@0: * for an acceptable. michael@0: */ michael@0: if (sctp_ifn) { michael@0: /* first try for a preferred address on the ep */ michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: if (sctp_is_addr_in_ep(inp, sctp_ifa)) { michael@0: sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* on the no-no list */ michael@0: continue; michael@0: } michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: } michael@0: /* next try for an acceptable address on the ep */ michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: if (sctp_is_addr_in_ep(inp, sctp_ifa)) { michael@0: sifa= sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv,fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* on the no-no list */ michael@0: continue; michael@0: } michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: } michael@0: michael@0: } michael@0: /* michael@0: * if we can't find one like that then we must look at all michael@0: * addresses bound to pick one at first preferable then michael@0: * secondly acceptable. michael@0: */ michael@0: starting_point = stcb->asoc.last_used_address; michael@0: sctp_from_the_top: michael@0: if (stcb->asoc.last_used_address == NULL) { michael@0: start_at_beginning = 1; michael@0: stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); michael@0: } michael@0: /* search beginning with the last used address */ michael@0: for (laddr = stcb->asoc.last_used_address; laddr; michael@0: laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { michael@0: if (laddr->ifa == NULL) { michael@0: /* address has been removed */ michael@0: continue; michael@0: } michael@0: if (laddr->action == SCTP_DEL_IP_ADDRESS) { michael@0: /* address is being deleted */ michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* on the no-no list */ michael@0: continue; michael@0: } michael@0: stcb->asoc.last_used_address = laddr; michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: if (start_at_beginning == 0) { michael@0: stcb->asoc.last_used_address = NULL; michael@0: goto sctp_from_the_top; michael@0: } michael@0: /* now try for any higher scope than the destination */ michael@0: stcb->asoc.last_used_address = starting_point; michael@0: start_at_beginning = 0; michael@0: sctp_from_the_top2: michael@0: if (stcb->asoc.last_used_address == NULL) { michael@0: start_at_beginning = 1; michael@0: stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); michael@0: } michael@0: /* search beginning with the last used address */ michael@0: for (laddr = stcb->asoc.last_used_address; laddr; michael@0: laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { michael@0: if (laddr->ifa == NULL) { michael@0: /* address has been removed */ michael@0: continue; michael@0: } michael@0: if (laddr->action == SCTP_DEL_IP_ADDRESS) { michael@0: /* address is being deleted */ michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* on the no-no list */ michael@0: continue; michael@0: } michael@0: stcb->asoc.last_used_address = laddr; michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: if (start_at_beginning == 0) { michael@0: stcb->asoc.last_used_address = NULL; michael@0: goto sctp_from_the_top2; michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn, michael@0: struct sctp_tcb *stcb, michael@0: int non_asoc_addr_ok, michael@0: uint8_t dest_is_loop, michael@0: uint8_t dest_is_priv, michael@0: int addr_wanted, michael@0: sa_family_t fam, michael@0: sctp_route_t *ro michael@0: ) michael@0: { michael@0: struct sctp_ifa *ifa, *sifa; michael@0: int num_eligible_addr = 0; michael@0: #ifdef INET6 michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: struct sockaddr_in6 sin6, lsa6; michael@0: michael@0: if (fam == AF_INET6) { michael@0: memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6)); michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(&sin6); michael@0: #else michael@0: (void)in6_recoverscope(&sin6, &sin6.sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif /* INET6 */ michael@0: LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { michael@0: if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: #ifdef INET6 michael@0: if (fam == AF_INET6 && michael@0: dest_is_loop && michael@0: sifa->src_is_loop && sifa->src_is_priv) { michael@0: /* don't allow fe80::1 to be a src on loop ::1, we don't list it michael@0: * to the peer so we will get an abort. michael@0: */ michael@0: continue; michael@0: } michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: if (fam == AF_INET6 && michael@0: IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) && michael@0: IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { michael@0: /* link-local <-> link-local must belong to the same scope. */ michael@0: memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6)); michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(&lsa6); michael@0: #else michael@0: (void)in6_recoverscope(&lsa6, &lsa6.sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: if (sin6.sin6_scope_id != lsa6.sin6_scope_id) { michael@0: continue; michael@0: } michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif /* INET6 */ michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__) michael@0: /* Check if the IPv6 address matches to next-hop. michael@0: In the mobile case, old IPv6 address may be not deleted michael@0: from the interface. Then, the interface has previous and michael@0: new addresses. We should use one corresponding to the michael@0: next-hop. (by micchie) michael@0: */ michael@0: #ifdef INET6 michael@0: if (stcb && fam == AF_INET6 && michael@0: sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { michael@0: if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) michael@0: == 0) { michael@0: continue; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: /* Avoid topologically incorrect IPv4 address */ michael@0: if (stcb && fam == AF_INET && michael@0: sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { michael@0: if (sctp_v4src_match_nexthop(sifa, ro) == 0) { michael@0: continue; michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: if (stcb) { michael@0: if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { michael@0: continue; michael@0: } michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* michael@0: * It is restricted for some reason.. michael@0: * probably not yet added. michael@0: */ michael@0: continue; michael@0: } michael@0: } michael@0: if (num_eligible_addr >= addr_wanted) { michael@0: return (sifa); michael@0: } michael@0: num_eligible_addr++; michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: static int michael@0: sctp_count_num_preferred_boundall(struct sctp_ifn *ifn, michael@0: struct sctp_tcb *stcb, michael@0: int non_asoc_addr_ok, michael@0: uint8_t dest_is_loop, michael@0: uint8_t dest_is_priv, michael@0: sa_family_t fam) michael@0: { michael@0: struct sctp_ifa *ifa, *sifa; michael@0: int num_eligible_addr = 0; michael@0: michael@0: LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { michael@0: if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) { michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) { michael@0: continue; michael@0: } michael@0: if (stcb) { michael@0: if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { michael@0: continue; michael@0: } michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* michael@0: * It is restricted for some reason.. michael@0: * probably not yet added. michael@0: */ michael@0: continue; michael@0: } michael@0: } michael@0: num_eligible_addr++; michael@0: } michael@0: return (num_eligible_addr); michael@0: } michael@0: michael@0: static struct sctp_ifa * michael@0: sctp_choose_boundall(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, michael@0: sctp_route_t *ro, michael@0: uint32_t vrf_id, michael@0: uint8_t dest_is_priv, michael@0: uint8_t dest_is_loop, michael@0: int non_asoc_addr_ok, michael@0: sa_family_t fam) michael@0: { michael@0: int cur_addr_num = 0, num_preferred = 0; michael@0: void *ifn; michael@0: struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn; michael@0: struct sctp_ifa *sctp_ifa, *sifa; michael@0: uint32_t ifn_index; michael@0: struct sctp_vrf *vrf; michael@0: #ifdef INET michael@0: int retried = 0; michael@0: #endif michael@0: michael@0: /*- michael@0: * For boundall we can use any address in the association. michael@0: * If non_asoc_addr_ok is set we can use any address (at least in michael@0: * theory). So we look for preferred addresses first. If we find one, michael@0: * we use it. Otherwise we next try to get an address on the michael@0: * interface, which we should be able to do (unless non_asoc_addr_ok michael@0: * is false and we are routed out that way). In these cases where we michael@0: * can't use the address of the interface we go through all the michael@0: * ifn's looking for an address we can use and fill that in. Punting michael@0: * means we send back address 0, which will probably cause problems michael@0: * actually since then IP will fill in the address of the route ifn, michael@0: * which means we probably already rejected it.. i.e. here comes an michael@0: * abort :-<. michael@0: */ michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) michael@0: return (NULL); michael@0: michael@0: ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); michael@0: ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn from route:%p ifn_index:%d\n", ifn, ifn_index); michael@0: emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index); michael@0: if (sctp_ifn == NULL) { michael@0: /* ?? We don't have this guy ?? */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"No ifn emit interface?\n"); michael@0: goto bound_all_plan_b; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn_index:%d name:%s is emit interface\n", michael@0: ifn_index, sctp_ifn->ifn_name); michael@0: michael@0: if (net) { michael@0: cur_addr_num = net->indx_of_eligible_next_to_use; michael@0: } michael@0: num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, michael@0: stcb, michael@0: non_asoc_addr_ok, michael@0: dest_is_loop, michael@0: dest_is_priv, fam); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n", michael@0: num_preferred, sctp_ifn->ifn_name); michael@0: if (num_preferred == 0) { michael@0: /* michael@0: * no eligible addresses, we must use some other interface michael@0: * address if we can find one. michael@0: */ michael@0: goto bound_all_plan_b; michael@0: } michael@0: /* michael@0: * Ok we have num_eligible_addr set with how many we can use, this michael@0: * may vary from call to call due to addresses being deprecated michael@0: * etc.. michael@0: */ michael@0: if (cur_addr_num >= num_preferred) { michael@0: cur_addr_num = 0; michael@0: } michael@0: /* michael@0: * select the nth address from the list (where cur_addr_num is the michael@0: * nth) and 0 is the first one, 1 is the second one etc... michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num); michael@0: michael@0: sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop, michael@0: dest_is_priv, cur_addr_num, fam, ro); michael@0: michael@0: /* if sctp_ifa is NULL something changed??, fall to plan b. */ michael@0: if (sctp_ifa) { michael@0: atomic_add_int(&sctp_ifa->refcount, 1); michael@0: if (net) { michael@0: /* save off where the next one we will want */ michael@0: net->indx_of_eligible_next_to_use = cur_addr_num + 1; michael@0: } michael@0: return (sctp_ifa); michael@0: } michael@0: /* michael@0: * plan_b: Look at all interfaces and find a preferred address. If michael@0: * no preferred fall through to plan_c. michael@0: */ michael@0: bound_all_plan_b: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n"); michael@0: LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n", michael@0: sctp_ifn->ifn_name); michael@0: if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { michael@0: /* wrong base scope */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n"); michael@0: continue; michael@0: } michael@0: if ((sctp_ifn == looked_at) && looked_at) { michael@0: /* already looked at this guy */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n"); michael@0: continue; michael@0: } michael@0: num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok, michael@0: dest_is_loop, dest_is_priv, fam); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, michael@0: "Found ifn:%p %d preferred source addresses\n", michael@0: ifn, num_preferred); michael@0: if (num_preferred == 0) { michael@0: /* None on this interface. */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n"); michael@0: continue; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, michael@0: "num preferred:%d on interface:%p cur_addr_num:%d\n", michael@0: num_preferred, (void *)sctp_ifn, cur_addr_num); michael@0: michael@0: /* michael@0: * Ok we have num_eligible_addr set with how many we can michael@0: * use, this may vary from call to call due to addresses michael@0: * being deprecated etc.. michael@0: */ michael@0: if (cur_addr_num >= num_preferred) { michael@0: cur_addr_num = 0; michael@0: } michael@0: sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop, michael@0: dest_is_priv, cur_addr_num, fam, ro); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (net) { michael@0: net->indx_of_eligible_next_to_use = cur_addr_num + 1; michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n", michael@0: cur_addr_num); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:"); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:"); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa); michael@0: } michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: return (sifa); michael@0: } michael@0: #ifdef INET michael@0: again_with_private_addresses_allowed: michael@0: #endif michael@0: /* plan_c: do we have an acceptable address on the emit interface */ michael@0: sifa = NULL; michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"Trying Plan C: find acceptable on interface\n"); michael@0: if (emit_ifn == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jump to Plan D - no emit_ifn\n"); michael@0: goto plan_d; michael@0: } michael@0: LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa); michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2,"Defer\n"); michael@0: continue; michael@0: } michael@0: sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n"); michael@0: continue; michael@0: } michael@0: if (stcb) { michael@0: if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n"); michael@0: sifa = NULL; michael@0: continue; michael@0: } michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* michael@0: * It is restricted for some michael@0: * reason.. probably not yet added. michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n"); michael@0: sifa = NULL; michael@0: continue; michael@0: } michael@0: } else { michael@0: SCTP_PRINTF("Stcb is null - no print\n"); michael@0: } michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: goto out; michael@0: } michael@0: plan_d: michael@0: /* michael@0: * plan_d: We are in trouble. No preferred address on the emit michael@0: * interface. And not even a preferred address on all interfaces. michael@0: * Go out and see if we can find an acceptable address somewhere michael@0: * amongst all interfaces. michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at); michael@0: LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { michael@0: if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { michael@0: /* wrong base scope */ michael@0: continue; michael@0: } michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, michael@0: dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (sifa == NULL) michael@0: continue; michael@0: if (stcb) { michael@0: if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { michael@0: sifa = NULL; michael@0: continue; michael@0: } michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, sifa)) && michael@0: (!sctp_is_addr_pending(stcb, sifa)))) { michael@0: /* michael@0: * It is restricted for some michael@0: * reason.. probably not yet added. michael@0: */ michael@0: sifa = NULL; michael@0: continue; michael@0: } michael@0: } michael@0: goto out; michael@0: } michael@0: } michael@0: #ifdef INET michael@0: if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) { michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: retried = 1; michael@0: goto again_with_private_addresses_allowed; michael@0: } else if (retried == 1) { michael@0: stcb->asoc.scope.ipv4_local_scope = 0; michael@0: } michael@0: #endif michael@0: out: michael@0: #ifdef INET michael@0: if (sifa) { michael@0: if (retried == 1) { michael@0: LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { michael@0: if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { michael@0: /* wrong base scope */ michael@0: continue; michael@0: } michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: struct sctp_ifa *tmp_sifa; michael@0: michael@0: if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && michael@0: (non_asoc_addr_ok == 0)) michael@0: continue; michael@0: tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, michael@0: dest_is_loop, michael@0: dest_is_priv, fam); michael@0: if (tmp_sifa == NULL) { michael@0: continue; michael@0: } michael@0: if (tmp_sifa == sifa) { michael@0: continue; michael@0: } michael@0: if (stcb) { michael@0: if (sctp_is_address_in_scope(tmp_sifa, michael@0: &stcb->asoc.scope, 0) == 0) { michael@0: continue; michael@0: } michael@0: if (((non_asoc_addr_ok == 0) && michael@0: (sctp_is_addr_restricted(stcb, tmp_sifa))) || michael@0: (non_asoc_addr_ok && michael@0: (sctp_is_addr_restricted(stcb, tmp_sifa)) && michael@0: (!sctp_is_addr_pending(stcb, tmp_sifa)))) { michael@0: /* michael@0: * It is restricted for some michael@0: * reason.. probably not yet added. michael@0: */ michael@0: continue; michael@0: } michael@0: } michael@0: if ((tmp_sifa->address.sin.sin_family == AF_INET) && michael@0: (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) { michael@0: sctp_add_local_addr_restricted(stcb, tmp_sifa); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: atomic_add_int(&sifa->refcount, 1); michael@0: } michael@0: #endif michael@0: return (sifa); michael@0: } michael@0: michael@0: michael@0: michael@0: /* tcb may be NULL */ michael@0: struct sctp_ifa * michael@0: sctp_source_address_selection(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: sctp_route_t *ro, michael@0: struct sctp_nets *net, michael@0: int non_asoc_addr_ok, uint32_t vrf_id) michael@0: { michael@0: struct sctp_ifa *answer; michael@0: uint8_t dest_is_priv, dest_is_loop; michael@0: sa_family_t fam; michael@0: #ifdef INET michael@0: struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst; michael@0: #endif michael@0: michael@0: /** michael@0: * Rules: - Find the route if needed, cache if I can. - Look at michael@0: * interface address in route, Is it in the bound list. If so we michael@0: * have the best source. - If not we must rotate amongst the michael@0: * addresses. michael@0: * michael@0: * Cavets and issues michael@0: * michael@0: * Do we need to pay attention to scope. We can have a private address michael@0: * or a global address we are sourcing or sending to. So if we draw michael@0: * it out michael@0: * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz michael@0: * For V4 michael@0: * ------------------------------------------ michael@0: * source * dest * result michael@0: * ----------------------------------------- michael@0: * Private * Global * NAT michael@0: * ----------------------------------------- michael@0: * Private * Private * No problem michael@0: * ----------------------------------------- michael@0: * Global * Private * Huh, How will this work? michael@0: * ----------------------------------------- michael@0: * Global * Global * No Problem michael@0: *------------------------------------------ michael@0: * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz michael@0: * For V6 michael@0: *------------------------------------------ michael@0: * source * dest * result michael@0: * ----------------------------------------- michael@0: * Linklocal * Global * michael@0: * ----------------------------------------- michael@0: * Linklocal * Linklocal * No problem michael@0: * ----------------------------------------- michael@0: * Global * Linklocal * Huh, How will this work? michael@0: * ----------------------------------------- michael@0: * Global * Global * No Problem michael@0: *------------------------------------------ michael@0: * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz michael@0: * michael@0: * And then we add to that what happens if there are multiple addresses michael@0: * assigned to an interface. Remember the ifa on a ifn is a linked michael@0: * list of addresses. So one interface can have more than one IP michael@0: * address. What happens if we have both a private and a global michael@0: * address? Do we then use context of destination to sort out which michael@0: * one is best? And what about NAT's sending P->G may get you a NAT michael@0: * translation, or should you select the G thats on the interface in michael@0: * preference. michael@0: * michael@0: * Decisions: michael@0: * michael@0: * - count the number of addresses on the interface. michael@0: * - if it is one, no problem except case . michael@0: * For we will assume a NAT out there. michael@0: * - if there are more than one, then we need to worry about scope P michael@0: * or G. We should prefer G -> G and P -> P if possible. michael@0: * Then as a secondary fall back to mixed types G->P being a last michael@0: * ditch one. michael@0: * - The above all works for bound all, but bound specific we need to michael@0: * use the same concept but instead only consider the bound michael@0: * addresses. If the bound set is NOT assigned to the interface then michael@0: * we must use rotation amongst the bound addresses.. michael@0: */ michael@0: if (ro->ro_rt == NULL) { michael@0: /* michael@0: * Need a route to cache. michael@0: */ michael@0: SCTP_RTALLOC(ro, vrf_id); michael@0: } michael@0: if (ro->ro_rt == NULL) { michael@0: return (NULL); michael@0: } michael@0: fam = ro->ro_dst.sa_family; michael@0: dest_is_priv = dest_is_loop = 0; michael@0: /* Setup our scopes for the destination */ michael@0: switch (fam) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: /* Scope based on outbound address */ michael@0: if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) { michael@0: dest_is_loop = 1; michael@0: if (net != NULL) { michael@0: /* mark it as local */ michael@0: net->addr_is_local = 1; michael@0: } michael@0: } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) { michael@0: dest_is_priv = 1; michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: /* Scope based on outbound address */ michael@0: #if defined(__Userspace_os_Windows) michael@0: if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) { michael@0: #else michael@0: if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) || michael@0: SCTP_ROUTE_IS_REAL_LOOP(ro)) { michael@0: #endif michael@0: /* michael@0: * If the address is a loopback address, which michael@0: * consists of "::1" OR "fe80::1%lo0", we are loopback michael@0: * scope. But we don't use dest_is_priv (link local michael@0: * addresses). michael@0: */ michael@0: dest_is_loop = 1; michael@0: if (net != NULL) { michael@0: /* mark it as local */ michael@0: net->addr_is_local = 1; michael@0: } michael@0: } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) { michael@0: dest_is_priv = 1; michael@0: } michael@0: break; michael@0: #endif michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:"); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst); michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* michael@0: * Bound all case michael@0: */ michael@0: answer = sctp_choose_boundall(stcb, net, ro, vrf_id, michael@0: dest_is_priv, dest_is_loop, michael@0: non_asoc_addr_ok, fam); michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (answer); michael@0: } michael@0: /* michael@0: * Subset bound case michael@0: */ michael@0: if (stcb) { michael@0: answer = sctp_choose_boundspecific_stcb(inp, stcb, ro, michael@0: vrf_id, dest_is_priv, michael@0: dest_is_loop, michael@0: non_asoc_addr_ok, fam); michael@0: } else { michael@0: answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id, michael@0: non_asoc_addr_ok, michael@0: dest_is_priv, michael@0: dest_is_loop, fam); michael@0: } michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (answer); michael@0: } michael@0: michael@0: static int michael@0: sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize) michael@0: { michael@0: #if defined(__Userspace_os_Windows) michael@0: WSACMSGHDR cmh; michael@0: #else michael@0: struct cmsghdr cmh; michael@0: #endif michael@0: int tlen, at, found; michael@0: struct sctp_sndinfo sndinfo; michael@0: struct sctp_prinfo prinfo; michael@0: struct sctp_authinfo authinfo; michael@0: michael@0: tlen = SCTP_BUF_LEN(control); michael@0: at = 0; michael@0: found = 0; michael@0: /* michael@0: * Independent of how many mbufs, find the c_type inside the control michael@0: * structure and copy out the data. michael@0: */ michael@0: while (at < tlen) { michael@0: if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { michael@0: /* There is not enough room for one more. */ michael@0: return (found); michael@0: } michael@0: m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); michael@0: if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { michael@0: /* We dont't have a complete CMSG header. */ michael@0: return (found); michael@0: } michael@0: if (((int)cmh.cmsg_len + at) > tlen) { michael@0: /* We don't have the complete CMSG. */ michael@0: return (found); michael@0: } michael@0: if ((cmh.cmsg_level == IPPROTO_SCTP) && michael@0: ((c_type == cmh.cmsg_type) || michael@0: ((c_type == SCTP_SNDRCV) && michael@0: ((cmh.cmsg_type == SCTP_SNDINFO) || michael@0: (cmh.cmsg_type == SCTP_PRINFO) || michael@0: (cmh.cmsg_type == SCTP_AUTHINFO))))) { michael@0: if (c_type == cmh.cmsg_type) { michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < cpsize) { michael@0: return (found); michael@0: } michael@0: /* It is exactly what we want. Copy it out. */ michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), cpsize, (caddr_t)data); michael@0: return (1); michael@0: } else { michael@0: struct sctp_sndrcvinfo *sndrcvinfo; michael@0: michael@0: sndrcvinfo = (struct sctp_sndrcvinfo *)data; michael@0: if (found == 0) { michael@0: if (cpsize < sizeof(struct sctp_sndrcvinfo)) { michael@0: return (found); michael@0: } michael@0: memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo)); michael@0: } michael@0: switch (cmh.cmsg_type) { michael@0: case SCTP_SNDINFO: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct sctp_sndinfo)) { michael@0: return (found); michael@0: } michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo); michael@0: sndrcvinfo->sinfo_stream = sndinfo.snd_sid; michael@0: sndrcvinfo->sinfo_flags = sndinfo.snd_flags; michael@0: sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid; michael@0: sndrcvinfo->sinfo_context = sndinfo.snd_context; michael@0: sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id; michael@0: break; michael@0: case SCTP_PRINFO: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct sctp_prinfo)) { michael@0: return (found); michael@0: } michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo); michael@0: if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) { michael@0: sndrcvinfo->sinfo_timetolive = prinfo.pr_value; michael@0: } else { michael@0: sndrcvinfo->sinfo_timetolive = 0; michael@0: } michael@0: sndrcvinfo->sinfo_flags |= prinfo.pr_policy; michael@0: break; michael@0: case SCTP_AUTHINFO: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct sctp_authinfo)) { michael@0: return (found); michael@0: } michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo); michael@0: sndrcvinfo->sinfo_keynumber_valid = 1; michael@0: sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber; michael@0: break; michael@0: default: michael@0: return (found); michael@0: } michael@0: found = 1; michael@0: } michael@0: } michael@0: at += CMSG_ALIGN(cmh.cmsg_len); michael@0: } michael@0: return (found); michael@0: } michael@0: michael@0: static int michael@0: sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error) michael@0: { michael@0: #if defined(__Userspace_os_Windows) michael@0: WSACMSGHDR cmh; michael@0: #else michael@0: struct cmsghdr cmh; michael@0: #endif michael@0: int tlen, at; michael@0: struct sctp_initmsg initmsg; michael@0: #ifdef INET michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6; michael@0: #endif michael@0: michael@0: tlen = SCTP_BUF_LEN(control); michael@0: at = 0; michael@0: while (at < tlen) { michael@0: if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { michael@0: /* There is not enough room for one more. */ michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); michael@0: if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { michael@0: /* We dont't have a complete CMSG header. */ michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: if (((int)cmh.cmsg_len + at) > tlen) { michael@0: /* We don't have the complete CMSG. */ michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: if (cmh.cmsg_level == IPPROTO_SCTP) { michael@0: switch (cmh.cmsg_type) { michael@0: case SCTP_INIT: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct sctp_initmsg)) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg); michael@0: if (initmsg.sinit_max_attempts) michael@0: stcb->asoc.max_init_times = initmsg.sinit_max_attempts; michael@0: if (initmsg.sinit_num_ostreams) michael@0: stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams; michael@0: if (initmsg.sinit_max_instreams) michael@0: stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams; michael@0: if (initmsg.sinit_max_init_timeo) michael@0: stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo; michael@0: if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) { michael@0: struct sctp_stream_out *tmp_str; michael@0: unsigned int i; michael@0: michael@0: /* Default is NOT correct */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n", michael@0: stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_MALLOC(tmp_str, michael@0: struct sctp_stream_out *, michael@0: (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)), michael@0: SCTP_M_STRMO); michael@0: SCTP_TCB_LOCK(stcb); michael@0: if (tmp_str != NULL) { michael@0: SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO); michael@0: stcb->asoc.strmout = tmp_str; michael@0: stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams; michael@0: } else { michael@0: stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt; michael@0: } michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); michael@0: stcb->asoc.strmout[i].chunks_on_queues = 0; michael@0: stcb->asoc.strmout[i].next_sequence_send = 0; michael@0: stcb->asoc.strmout[i].stream_no = i; michael@0: stcb->asoc.strmout[i].last_msg_incomplete = 0; michael@0: stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL); michael@0: } michael@0: } michael@0: break; michael@0: #ifdef INET michael@0: case SCTP_DSTADDRV4: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct in_addr)) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: memset(&sin, 0, sizeof(struct sockaddr_in)); michael@0: sin.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: sin.sin_port = stcb->rport; michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr); michael@0: if ((sin.sin_addr.s_addr == INADDR_ANY) || michael@0: (sin.sin_addr.s_addr == INADDR_BROADCAST) || michael@0: IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, michael@0: SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: *error = ENOBUFS; michael@0: return (1); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case SCTP_DSTADDRV6: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct in6_addr)) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: memset(&sin6, 0, sizeof(struct sockaddr_in6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: sin6.sin6_port = stcb->rport; michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) || michael@0: IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: #ifdef INET michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { michael@0: in6_sin6_2_sin(&sin, &sin6); michael@0: if ((sin.sin_addr.s_addr == INADDR_ANY) || michael@0: (sin.sin_addr.s_addr == INADDR_BROADCAST) || michael@0: IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { michael@0: *error = EINVAL; michael@0: return (1); michael@0: } michael@0: if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, michael@0: SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: *error = ENOBUFS; michael@0: return (1); michael@0: } michael@0: } else michael@0: #endif michael@0: if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, michael@0: SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: *error = ENOBUFS; michael@0: return (1); michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: at += CMSG_ALIGN(cmh.cmsg_len); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static struct sctp_tcb * michael@0: sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p, michael@0: uint16_t port, michael@0: struct mbuf *control, michael@0: struct sctp_nets **net_p, michael@0: int *error) michael@0: { michael@0: #if defined(__Userspace_os_Windows) michael@0: WSACMSGHDR cmh; michael@0: #else michael@0: struct cmsghdr cmh; michael@0: #endif michael@0: int tlen, at; michael@0: struct sctp_tcb *stcb; michael@0: struct sockaddr *addr; michael@0: #ifdef INET michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6; michael@0: #endif michael@0: michael@0: tlen = SCTP_BUF_LEN(control); michael@0: at = 0; michael@0: while (at < tlen) { michael@0: if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { michael@0: /* There is not enough room for one more. */ michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); michael@0: if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { michael@0: /* We dont't have a complete CMSG header. */ michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: if (((int)cmh.cmsg_len + at) > tlen) { michael@0: /* We don't have the complete CMSG. */ michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: if (cmh.cmsg_level == IPPROTO_SCTP) { michael@0: switch (cmh.cmsg_type) { michael@0: #ifdef INET michael@0: case SCTP_DSTADDRV4: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct in_addr)) { michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: memset(&sin, 0, sizeof(struct sockaddr_in)); michael@0: sin.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: sin.sin_port = port; michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr); michael@0: addr = (struct sockaddr *)&sin; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case SCTP_DSTADDRV6: michael@0: if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh))) < sizeof(struct in6_addr)) { michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: memset(&sin6, 0, sizeof(struct sockaddr_in6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: sin6.sin6_port = port; michael@0: m_copydata(control, at + CMSG_ALIGN(sizeof(cmh)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); michael@0: #ifdef INET michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { michael@0: in6_sin6_2_sin(&sin, &sin6); michael@0: addr = (struct sockaddr *)&sin; michael@0: } else michael@0: #endif michael@0: addr = (struct sockaddr *)&sin6; michael@0: break; michael@0: #endif michael@0: default: michael@0: addr = NULL; michael@0: break; michael@0: } michael@0: if (addr) { michael@0: stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL); michael@0: if (stcb != NULL) { michael@0: return (stcb); michael@0: } michael@0: } michael@0: } michael@0: at += CMSG_ALIGN(cmh.cmsg_len); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: static struct mbuf * michael@0: sctp_add_cookie(struct mbuf *init, int init_offset, michael@0: struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature) michael@0: { michael@0: struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret; michael@0: struct sctp_state_cookie *stc; michael@0: struct sctp_paramhdr *ph; michael@0: uint8_t *foo; michael@0: int sig_offset; michael@0: uint16_t cookie_sz; michael@0: michael@0: mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) + michael@0: sizeof(struct sctp_paramhdr)), 0, michael@0: M_NOWAIT, 1, MT_DATA); michael@0: if (mret == NULL) { michael@0: return (NULL); michael@0: } michael@0: copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT); michael@0: if (copy_init == NULL) { michael@0: sctp_m_freem(mret); michael@0: return (NULL); michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = copy_init; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL, michael@0: M_NOWAIT); michael@0: if (copy_initack == NULL) { michael@0: sctp_m_freem(mret); michael@0: sctp_m_freem(copy_init); michael@0: return (NULL); michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = copy_initack; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: /* easy side we just drop it on the end */ michael@0: ph = mtod(mret, struct sctp_paramhdr *); michael@0: SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) + michael@0: sizeof(struct sctp_paramhdr); michael@0: stc = (struct sctp_state_cookie *)((caddr_t)ph + michael@0: sizeof(struct sctp_paramhdr)); michael@0: ph->param_type = htons(SCTP_STATE_COOKIE); michael@0: ph->param_length = 0; /* fill in at the end */ michael@0: /* Fill in the stc cookie data */ michael@0: memcpy(stc, stc_in, sizeof(struct sctp_state_cookie)); michael@0: michael@0: /* tack the INIT and then the INIT-ACK onto the chain */ michael@0: cookie_sz = 0; michael@0: for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: cookie_sz += SCTP_BUF_LEN(m_at); michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) { michael@0: SCTP_BUF_NEXT(m_at) = copy_init; michael@0: break; michael@0: } michael@0: } michael@0: for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: cookie_sz += SCTP_BUF_LEN(m_at); michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) { michael@0: SCTP_BUF_NEXT(m_at) = copy_initack; michael@0: break; michael@0: } michael@0: } michael@0: for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: cookie_sz += SCTP_BUF_LEN(m_at); michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) { michael@0: break; michael@0: } michael@0: } michael@0: sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (sig == NULL) { michael@0: /* no space, so free the entire chain */ michael@0: sctp_m_freem(mret); michael@0: return (NULL); michael@0: } michael@0: SCTP_BUF_LEN(sig) = 0; michael@0: SCTP_BUF_NEXT(m_at) = sig; michael@0: sig_offset = 0; michael@0: foo = (uint8_t *) (mtod(sig, caddr_t) + sig_offset); michael@0: memset(foo, 0, SCTP_SIGNATURE_SIZE); michael@0: *signature = foo; michael@0: SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE; michael@0: cookie_sz += SCTP_SIGNATURE_SIZE; michael@0: ph->param_length = htons(cookie_sz); michael@0: return (mret); michael@0: } michael@0: michael@0: michael@0: static uint8_t michael@0: sctp_get_ect(struct sctp_tcb *stcb) michael@0: { michael@0: if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) { michael@0: return (SCTP_ECT0_BIT); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: #if defined(INET) || defined(INET6) michael@0: static void michael@0: sctp_handle_no_route(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, michael@0: int so_locked) michael@0: { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n"); michael@0: michael@0: if (net) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa); michael@0: if (net->dest_state & SCTP_ADDR_CONFIRMED) { michael@0: if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net); michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, michael@0: stcb, 0, michael@0: (void *)net, michael@0: so_locked); michael@0: net->dest_state &= ~SCTP_ADDR_REACHABLE; michael@0: net->dest_state &= ~SCTP_ADDR_PF; michael@0: } michael@0: } michael@0: if (stcb) { michael@0: if (net == stcb->asoc.primary_destination) { michael@0: /* need a new primary */ michael@0: struct sctp_nets *alt; michael@0: michael@0: alt = sctp_find_alternate_net(stcb, net, 0); michael@0: if (alt != net) { michael@0: if (stcb->asoc.alternate) { michael@0: sctp_free_remote_addr(stcb->asoc.alternate); michael@0: } michael@0: stcb->asoc.alternate = alt; michael@0: atomic_add_int(&stcb->asoc.alternate->ref_count, 1); michael@0: if (net->ro._s_addr) { michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: } michael@0: net->src_addr_selected = 0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static int michael@0: sctp_lowlevel_chunk_output(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, /* may be NULL */ michael@0: struct sctp_nets *net, michael@0: struct sockaddr *to, michael@0: struct mbuf *m, michael@0: uint32_t auth_offset, michael@0: struct sctp_auth_chunk *auth, michael@0: uint16_t auth_keyid, michael@0: int nofragment_flag, michael@0: int ecn_ok, michael@0: int out_of_asoc_ok, michael@0: uint16_t src_port, michael@0: uint16_t dest_port, michael@0: uint32_t v_tag, michael@0: uint16_t port, michael@0: union sctp_sockstore *over_addr, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: int so_locked SCTP_UNUSED michael@0: #else michael@0: int so_locked michael@0: #endif michael@0: ) michael@0: /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */ michael@0: { michael@0: /** michael@0: * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header michael@0: * WITH an SCTPHDR but no IP header, endpoint inp and sa structure: michael@0: * - fill in the HMAC digest of any AUTH chunk in the packet. michael@0: * - calculate and fill in the SCTP checksum. michael@0: * - prepend an IP address header. michael@0: * - if boundall use INADDR_ANY. michael@0: * - if boundspecific do source address selection. michael@0: * - set fragmentation option for ipV4. michael@0: * - On return from IP output, check/adjust mtu size of output michael@0: * interface and smallest_mtu size as well. michael@0: */ michael@0: /* Will need ifdefs around this */ michael@0: #ifdef __Panda__ michael@0: pakhandle_type o_pak; michael@0: #endif michael@0: struct mbuf *newm; michael@0: struct sctphdr *sctphdr; michael@0: int packet_length; michael@0: int ret; michael@0: #if defined(INET) || defined(INET6) michael@0: uint32_t vrf_id; michael@0: #endif michael@0: #if defined(INET) || defined(INET6) michael@0: #if !defined(__Panda__) michael@0: struct mbuf *o_pak; michael@0: #endif michael@0: sctp_route_t *ro = NULL; michael@0: struct udphdr *udp = NULL; michael@0: #endif michael@0: uint8_t tos_value; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so = NULL; michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: sctp_m_freem(m); michael@0: return (EFAULT); michael@0: } michael@0: #if defined(INET) || defined(INET6) michael@0: if (stcb) { michael@0: vrf_id = stcb->asoc.vrf_id; michael@0: } else { michael@0: vrf_id = inp->def_vrf_id; michael@0: } michael@0: #endif michael@0: /* fill in the HMAC digest for any AUTH chunk in the packet */ michael@0: if ((auth != NULL) && (stcb != NULL)) { michael@0: sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid); michael@0: } michael@0: michael@0: if (net) { michael@0: tos_value = net->dscp; michael@0: } else if (stcb) { michael@0: tos_value = stcb->asoc.default_dscp; michael@0: } else { michael@0: tos_value = inp->sctp_ep.default_dscp; michael@0: } michael@0: michael@0: switch (to->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct ip *ip = NULL; michael@0: sctp_route_t iproute; michael@0: int len; michael@0: michael@0: len = sizeof(struct ip) + sizeof(struct sctphdr); michael@0: if (port) { michael@0: len += sizeof(struct udphdr); michael@0: } michael@0: newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); michael@0: if (newm == NULL) { michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_ALIGN_TO_END(newm, len); michael@0: SCTP_BUF_LEN(newm) = len; michael@0: SCTP_BUF_NEXT(newm) = m; michael@0: m = newm; michael@0: #if defined(__FreeBSD__) michael@0: if (net != NULL) { michael@0: #ifdef INVARIANTS michael@0: if (net->flowidset == 0) { michael@0: panic("Flow ID not set"); michael@0: } michael@0: #endif michael@0: m->m_pkthdr.flowid = net->flowid; michael@0: m->m_flags |= M_FLOWID; michael@0: } else { michael@0: if (use_mflowid != 0) { michael@0: m->m_pkthdr.flowid = mflowid; michael@0: m->m_flags |= M_FLOWID; michael@0: } michael@0: } michael@0: #endif michael@0: packet_length = sctp_calculate_len(m); michael@0: ip = mtod(m, struct ip *); michael@0: ip->ip_v = IPVERSION; michael@0: ip->ip_hl = (sizeof(struct ip) >> 2); michael@0: if (tos_value == 0) { michael@0: /* michael@0: * This means especially, that it is not set at the michael@0: * SCTP layer. So use the value from the IP layer. michael@0: */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) michael@0: tos_value = inp->ip_inp.inp.inp_ip_tos; michael@0: #else michael@0: tos_value = inp->inp_ip_tos; michael@0: #endif michael@0: } michael@0: tos_value &= 0xfc; michael@0: if (ecn_ok) { michael@0: tos_value |= sctp_get_ect(stcb); michael@0: } michael@0: if ((nofragment_flag) && (port == 0)) { michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version >= 1000000 michael@0: ip->ip_off = htons(IP_DF); michael@0: #else michael@0: ip->ip_off = IP_DF; michael@0: #endif michael@0: #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__) || defined(__Userspace__) michael@0: ip->ip_off = IP_DF; michael@0: #else michael@0: ip->ip_off = htons(IP_DF); michael@0: #endif michael@0: } else { michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 michael@0: ip->ip_off = htons(0); michael@0: #else michael@0: ip->ip_off = 0; michael@0: #endif michael@0: } michael@0: #if defined(__FreeBSD__) michael@0: /* FreeBSD has a function for ip_id's */ michael@0: ip->ip_id = ip_newid(); michael@0: #elif defined(RANDOM_IP_ID) michael@0: /* Apple has RANDOM_IP_ID switch */ michael@0: ip->ip_id = htons(ip_randomid()); michael@0: #elif defined(__Userspace__) michael@0: ip->ip_id = htons(SCTP_IP_ID(inp)++); michael@0: #else michael@0: ip->ip_id = SCTP_IP_ID(inp)++; michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) michael@0: ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl; michael@0: #else michael@0: ip->ip_ttl = inp->inp_ip_ttl; michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 michael@0: ip->ip_len = htons(packet_length); michael@0: #else michael@0: ip->ip_len = packet_length; michael@0: #endif michael@0: ip->ip_tos = tos_value; michael@0: if (port) { michael@0: ip->ip_p = IPPROTO_UDP; michael@0: } else { michael@0: ip->ip_p = IPPROTO_SCTP; michael@0: } michael@0: ip->ip_sum = 0; michael@0: if (net == NULL) { michael@0: ro = &iproute; michael@0: memset(&iproute, 0, sizeof(iproute)); michael@0: #ifdef HAVE_SA_LEN michael@0: memcpy(&ro->ro_dst, to, to->sa_len); michael@0: #else michael@0: memcpy(&ro->ro_dst, to, sizeof(struct sockaddr_in)); michael@0: #endif michael@0: } else { michael@0: ro = (sctp_route_t *)&net->ro; michael@0: } michael@0: /* Now the address selection part */ michael@0: ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr; michael@0: michael@0: /* call the routine to select the src address */ michael@0: if (net && out_of_asoc_ok == 0) { michael@0: if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: net->src_addr_selected = 0; michael@0: if (ro->ro_rt) { michael@0: RTFREE(ro->ro_rt); michael@0: ro->ro_rt = NULL; michael@0: } michael@0: } michael@0: if (net->src_addr_selected == 0) { michael@0: /* Cache the source address */ michael@0: net->ro._s_addr = sctp_source_address_selection(inp,stcb, michael@0: ro, net, 0, michael@0: vrf_id); michael@0: net->src_addr_selected = 1; michael@0: } michael@0: if (net->ro._s_addr == NULL) { michael@0: /* No route to host */ michael@0: net->src_addr_selected = 0; michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: ip->ip_src = net->ro._s_addr->address.sin.sin_addr; michael@0: } else { michael@0: if (over_addr == NULL) { michael@0: struct sctp_ifa *_lsrc; michael@0: michael@0: _lsrc = sctp_source_address_selection(inp, stcb, ro, michael@0: net, michael@0: out_of_asoc_ok, michael@0: vrf_id); michael@0: if (_lsrc == NULL) { michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: ip->ip_src = _lsrc->address.sin.sin_addr; michael@0: sctp_free_ifa(_lsrc); michael@0: } else { michael@0: ip->ip_src = over_addr->sin.sin_addr; michael@0: SCTP_RTALLOC(ro, vrf_id); michael@0: } michael@0: } michael@0: if (port) { michael@0: if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); michael@0: udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); michael@0: udp->uh_dport = port; michael@0: udp->uh_ulen = htons(packet_length - sizeof(struct ip)); michael@0: #if !defined(__Windows__) && !defined(__Userspace__) michael@0: #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) michael@0: if (V_udp_cksum) { michael@0: udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); michael@0: } else { michael@0: udp->uh_sum = 0; michael@0: } michael@0: #else michael@0: udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); michael@0: #endif michael@0: #else michael@0: udp->uh_sum = 0; michael@0: #endif michael@0: sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); michael@0: } else { michael@0: sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip)); michael@0: } michael@0: michael@0: sctphdr->src_port = src_port; michael@0: sctphdr->dest_port = dest_port; michael@0: sctphdr->v_tag = v_tag; michael@0: sctphdr->checksum = 0; michael@0: michael@0: /* michael@0: * If source address selection fails and we find no route michael@0: * then the ip_output should fail as well with a michael@0: * NO_ROUTE_TO_HOST type error. We probably should catch michael@0: * that somewhere and abort the association right away michael@0: * (assuming this is an INIT being sent). michael@0: */ michael@0: if (ro->ro_rt == NULL) { michael@0: /* michael@0: * src addr selection failed to find a route (or michael@0: * valid source addr), so we can't get there from michael@0: * here (yet)! michael@0: */ michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: if (ro != &iproute) { michael@0: memcpy(&iproute, ro, sizeof(*ro)); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n", michael@0: (uint32_t) (ntohl(ip->ip_src.s_addr))); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n", michael@0: (uint32_t)(ntohl(ip->ip_dst.s_addr))); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n", michael@0: (void *)ro->ro_rt); michael@0: michael@0: if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { michael@0: /* failed to prepend data, give up */ michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: sctp_m_freem(m); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_ATTACH_CHAIN(o_pak, m, packet_length); michael@0: if (port) { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) michael@0: if (V_udp_cksum) { michael@0: SCTP_ENABLE_UDP_CSUM(o_pak); michael@0: } michael@0: #else michael@0: SCTP_ENABLE_UDP_CSUM(o_pak); michael@0: #endif michael@0: } else { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: m->m_pkthdr.csum_flags = CSUM_SCTP; michael@0: m->m_pkthdr.csum_data = 0; michael@0: SCTP_STAT_INCR(sctps_sendhwcrc); michael@0: #else michael@0: if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && michael@0: (stcb) && (stcb->asoc.scope.loopback_scope))) { michael@0: sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: } else { michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: } michael@0: #endif michael@0: #endif michael@0: } michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) michael@0: sctp_packet_log(o_pak); michael@0: #endif michael@0: /* send it out. table id is taken from stcb */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { michael@0: so = SCTP_INP_SO(inp); michael@0: SCTP_SOCKET_UNLOCK(so, 0); michael@0: } michael@0: #endif michael@0: SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 0); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_sendpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outpackets); michael@0: if (ret) michael@0: SCTP_STAT_INCR(sctps_senderrors); michael@0: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret); michael@0: if (net == NULL) { michael@0: /* free tempy routes */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 901000 michael@0: RO_RTFREE(ro); michael@0: #else michael@0: if (ro->ro_rt) { michael@0: RTFREE(ro->ro_rt); michael@0: ro->ro_rt = NULL; michael@0: } michael@0: #endif michael@0: } else { michael@0: /* PMTU check versus smallest asoc MTU goes here */ michael@0: if ((ro->ro_rt != NULL) && michael@0: (net->ro._s_addr)) { michael@0: uint32_t mtu; michael@0: mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); michael@0: if (net->port) { michael@0: mtu -= sizeof(struct udphdr); michael@0: } michael@0: if (mtu && (stcb->asoc.smallest_mtu > mtu)) { michael@0: sctp_mtu_size_reset(inp, &stcb->asoc, mtu); michael@0: net->mtu = mtu; michael@0: } michael@0: } else if (ro->ro_rt == NULL) { michael@0: /* route was freed */ michael@0: if (net->ro._s_addr && michael@0: net->src_addr_selected) { michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: } michael@0: net->src_addr_selected = 0; michael@0: } michael@0: } michael@0: return (ret); michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: uint32_t flowlabel, flowinfo; michael@0: struct ip6_hdr *ip6h; michael@0: struct route_in6 ip6route; michael@0: #if !(defined(__Panda__) || defined(__Userspace__)) michael@0: struct ifnet *ifp; michael@0: #endif michael@0: struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp; michael@0: int prev_scope = 0; michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: struct sockaddr_in6 lsa6_storage; michael@0: int error; michael@0: #endif michael@0: u_short prev_port = 0; michael@0: int len; michael@0: michael@0: if (net) { michael@0: flowlabel = net->flowlabel; michael@0: } else if (stcb) { michael@0: flowlabel = stcb->asoc.default_flowlabel; michael@0: } else { michael@0: flowlabel = inp->sctp_ep.default_flowlabel; michael@0: } michael@0: if (flowlabel == 0) { michael@0: /* michael@0: * This means especially, that it is not set at the michael@0: * SCTP layer. So use the value from the IP layer. michael@0: */ michael@0: #if defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) michael@0: flowlabel = ntohl(inp->ip_inp.inp.inp_flow); michael@0: #else michael@0: flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo); michael@0: #endif michael@0: } michael@0: flowlabel &= 0x000fffff; michael@0: len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr); michael@0: if (port) { michael@0: len += sizeof(struct udphdr); michael@0: } michael@0: newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); michael@0: if (newm == NULL) { michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_ALIGN_TO_END(newm, len); michael@0: SCTP_BUF_LEN(newm) = len; michael@0: SCTP_BUF_NEXT(newm) = m; michael@0: m = newm; michael@0: #if defined(__FreeBSD__) michael@0: if (net != NULL) { michael@0: #ifdef INVARIANTS michael@0: if (net->flowidset == 0) { michael@0: panic("Flow ID not set"); michael@0: } michael@0: #endif michael@0: m->m_pkthdr.flowid = net->flowid; michael@0: m->m_flags |= M_FLOWID; michael@0: } else { michael@0: if (use_mflowid != 0) { michael@0: m->m_pkthdr.flowid = mflowid; michael@0: m->m_flags |= M_FLOWID; michael@0: } michael@0: } michael@0: #endif michael@0: packet_length = sctp_calculate_len(m); michael@0: michael@0: ip6h = mtod(m, struct ip6_hdr *); michael@0: /* protect *sin6 from overwrite */ michael@0: sin6 = (struct sockaddr_in6 *)to; michael@0: tmp = *sin6; michael@0: sin6 = &tmp; michael@0: michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: /* KAME hack: embed scopeid */ michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) michael@0: #endif michael@0: #elif defined(SCTP_KAME) michael@0: if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) michael@0: #endif michael@0: { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: if (net == NULL) { michael@0: memset(&ip6route, 0, sizeof(ip6route)); michael@0: ro = (sctp_route_t *)&ip6route; michael@0: #ifdef HAVE_SIN6_LEN michael@0: memcpy(&ro->ro_dst, sin6, sin6->sin6_len); michael@0: #else michael@0: memcpy(&ro->ro_dst, sin6, sizeof(struct sockaddr_in6)); michael@0: #endif michael@0: } else { michael@0: ro = (sctp_route_t *)&net->ro; michael@0: } michael@0: /* michael@0: * We assume here that inp_flow is in host byte order within michael@0: * the TCB! michael@0: */ michael@0: if (tos_value == 0) { michael@0: /* michael@0: * This means especially, that it is not set at the michael@0: * SCTP layer. So use the value from the IP layer. michael@0: */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) michael@0: #if defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) michael@0: tos_value = (ntohl(inp->ip_inp.inp.inp_flow) >> 20) & 0xff; michael@0: #else michael@0: tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff; michael@0: #endif michael@0: #endif michael@0: } michael@0: tos_value &= 0xfc; michael@0: if (ecn_ok) { michael@0: tos_value |= sctp_get_ect(stcb); michael@0: } michael@0: flowinfo = 0x06; michael@0: flowinfo <<= 8; michael@0: flowinfo |= tos_value; michael@0: flowinfo <<= 20; michael@0: flowinfo |= flowlabel; michael@0: ip6h->ip6_flow = htonl(flowinfo); michael@0: if (port) { michael@0: ip6h->ip6_nxt = IPPROTO_UDP; michael@0: } else { michael@0: ip6h->ip6_nxt = IPPROTO_SCTP; michael@0: } michael@0: ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr)); michael@0: ip6h->ip6_dst = sin6->sin6_addr; michael@0: michael@0: /* michael@0: * Add SRC address selection here: we can only reuse to a michael@0: * limited degree the kame src-addr-sel, since we can try michael@0: * their selection but it may not be bound. michael@0: */ michael@0: bzero(&lsa6_tmp, sizeof(lsa6_tmp)); michael@0: lsa6_tmp.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: lsa6_tmp.sin6_len = sizeof(lsa6_tmp); michael@0: #endif michael@0: lsa6 = &lsa6_tmp; michael@0: if (net && out_of_asoc_ok == 0) { michael@0: if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: net->src_addr_selected = 0; michael@0: if (ro->ro_rt) { michael@0: RTFREE(ro->ro_rt); michael@0: ro->ro_rt = NULL; michael@0: } michael@0: } michael@0: if (net->src_addr_selected == 0) { michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: /* KAME hack: embed scopeid */ michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) michael@0: #endif michael@0: #elif defined(SCTP_KAME) michael@0: if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) michael@0: #endif michael@0: { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: /* Cache the source address */ michael@0: net->ro._s_addr = sctp_source_address_selection(inp, michael@0: stcb, michael@0: ro, michael@0: net, michael@0: 0, michael@0: vrf_id); michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(sin6); michael@0: #else michael@0: (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: net->src_addr_selected = 1; michael@0: } michael@0: if (net->ro._s_addr == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n"); michael@0: net->src_addr_selected = 0; michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr; michael@0: } else { michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: sin6 = (struct sockaddr_in6 *)&ro->ro_dst; michael@0: /* KAME hack: embed scopeid */ michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) michael@0: #endif michael@0: #elif defined(SCTP_KAME) michael@0: if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) michael@0: #endif michael@0: { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: if (over_addr == NULL) { michael@0: struct sctp_ifa *_lsrc; michael@0: michael@0: _lsrc = sctp_source_address_selection(inp, stcb, ro, michael@0: net, michael@0: out_of_asoc_ok, michael@0: vrf_id); michael@0: if (_lsrc == NULL) { michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr; michael@0: sctp_free_ifa(_lsrc); michael@0: } else { michael@0: lsa6->sin6_addr = over_addr->sin6.sin6_addr; michael@0: SCTP_RTALLOC(ro, vrf_id); michael@0: } michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(sin6); michael@0: #else michael@0: (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: } michael@0: lsa6->sin6_port = inp->sctp_lport; michael@0: michael@0: if (ro->ro_rt == NULL) { michael@0: /* michael@0: * src addr selection failed to find a route (or michael@0: * valid source addr), so we can't get there from michael@0: * here! michael@0: */ michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: #ifndef SCOPEDROUTING michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: /* michael@0: * XXX: sa6 may not have a valid sin6_scope_id in the michael@0: * non-SCOPEDROUTING case. michael@0: */ michael@0: bzero(&lsa6_storage, sizeof(lsa6_storage)); michael@0: lsa6_storage.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: lsa6_storage.sin6_len = sizeof(lsa6_storage); michael@0: #endif michael@0: #ifdef SCTP_KAME michael@0: lsa6_storage.sin6_addr = lsa6->sin6_addr; michael@0: if ((error = sa6_recoverscope(&lsa6_storage)) != 0) { michael@0: #else michael@0: if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr, michael@0: NULL)) != 0) { michael@0: #endif /* SCTP_KAME */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error); michael@0: sctp_m_freem(m); michael@0: return (error); michael@0: } michael@0: /* XXX */ michael@0: lsa6_storage.sin6_addr = lsa6->sin6_addr; michael@0: lsa6_storage.sin6_port = inp->sctp_lport; michael@0: lsa6 = &lsa6_storage; michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif /* SCOPEDROUTING */ michael@0: ip6h->ip6_src = lsa6->sin6_addr; michael@0: michael@0: if (port) { michael@0: if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { michael@0: sctp_handle_no_route(stcb, net, so_locked); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); michael@0: sctp_m_freem(m); michael@0: return (EHOSTUNREACH); michael@0: } michael@0: udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); michael@0: udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); michael@0: udp->uh_dport = port; michael@0: udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr)); michael@0: udp->uh_sum = 0; michael@0: sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); michael@0: } else { michael@0: sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); michael@0: } michael@0: michael@0: sctphdr->src_port = src_port; michael@0: sctphdr->dest_port = dest_port; michael@0: sctphdr->v_tag = v_tag; michael@0: sctphdr->checksum = 0; michael@0: michael@0: /* michael@0: * We set the hop limit now since there is a good chance michael@0: * that our ro pointer is now filled michael@0: */ michael@0: ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro); michael@0: #if !(defined(__Panda__) || defined(__Userspace__)) michael@0: ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); michael@0: #endif michael@0: michael@0: #ifdef SCTP_DEBUG michael@0: /* Copy to be sure something bad is not happening */ michael@0: sin6->sin6_addr = ip6h->ip6_dst; michael@0: lsa6->sin6_addr = ip6h->ip6_src; michael@0: #endif michael@0: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n"); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6); michael@0: if (net) { michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: /* preserve the port and scope for link local send */ michael@0: prev_scope = sin6->sin6_scope_id; michael@0: prev_port = sin6->sin6_port; michael@0: } michael@0: michael@0: if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { michael@0: /* failed to prepend data, give up */ michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_ATTACH_CHAIN(o_pak, m, packet_length); michael@0: if (port) { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #if defined(__Windows__) michael@0: udp->uh_sum = 0; michael@0: #elif !defined(__Userspace__) michael@0: if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) { michael@0: udp->uh_sum = 0xffff; michael@0: } michael@0: #endif michael@0: } else { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: #if __FreeBSD_version < 900000 michael@0: sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #else michael@0: #if __FreeBSD_version > 901000 michael@0: m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; michael@0: #else michael@0: m->m_pkthdr.csum_flags = CSUM_SCTP; michael@0: #endif michael@0: m->m_pkthdr.csum_data = 0; michael@0: SCTP_STAT_INCR(sctps_sendhwcrc); michael@0: #endif michael@0: #else michael@0: if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && michael@0: (stcb) && (stcb->asoc.scope.loopback_scope))) { michael@0: sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: } else { michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: } michael@0: #endif michael@0: #endif michael@0: } michael@0: /* send it out. table id is taken from stcb */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { michael@0: so = SCTP_INP_SO(inp); michael@0: SCTP_SOCKET_UNLOCK(so, 0); michael@0: } michael@0: #endif michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) michael@0: sctp_packet_log(o_pak); michael@0: #endif michael@0: #if !(defined(__Panda__) || defined(__Userspace__)) michael@0: SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id); michael@0: #else michael@0: SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, NULL, stcb, vrf_id); michael@0: #endif michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 0); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: #endif michael@0: if (net) { michael@0: /* for link local this must be done */ michael@0: sin6->sin6_scope_id = prev_scope; michael@0: sin6->sin6_port = prev_port; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); michael@0: SCTP_STAT_INCR(sctps_sendpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outpackets); michael@0: if (ret) { michael@0: SCTP_STAT_INCR(sctps_senderrors); michael@0: } michael@0: if (net == NULL) { michael@0: /* Now if we had a temp route free it */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 901000 michael@0: RO_RTFREE(ro); michael@0: #else michael@0: if (ro->ro_rt) { michael@0: RTFREE(ro->ro_rt); michael@0: ro->ro_rt = NULL; michael@0: } michael@0: #endif michael@0: } else { michael@0: /* PMTU check versus smallest asoc MTU goes here */ michael@0: if (ro->ro_rt == NULL) { michael@0: /* Route was freed */ michael@0: if (net->ro._s_addr && michael@0: net->src_addr_selected) { michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: } michael@0: net->src_addr_selected = 0; michael@0: } michael@0: if ((ro->ro_rt != NULL) && michael@0: (net->ro._s_addr)) { michael@0: uint32_t mtu; michael@0: mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); michael@0: if (mtu && michael@0: (stcb->asoc.smallest_mtu > mtu)) { michael@0: sctp_mtu_size_reset(inp, &stcb->asoc, mtu); michael@0: net->mtu = mtu; michael@0: if (net->port) { michael@0: net->mtu -= sizeof(struct udphdr); michael@0: } michael@0: } michael@0: } michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: else if (ifp) { michael@0: #if defined(__Windows__) michael@0: #define ND_IFINFO(ifp) (ifp) michael@0: #define linkmtu if_mtu michael@0: #endif michael@0: if (ND_IFINFO(ifp)->linkmtu && michael@0: (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) { michael@0: sctp_mtu_size_reset(inp, michael@0: &stcb->asoc, michael@0: ND_IFINFO(ifp)->linkmtu); michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: return (ret); michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: char *buffer; michael@0: struct sockaddr_conn *sconn; michael@0: int len; michael@0: michael@0: sconn = (struct sockaddr_conn *)to; michael@0: len = sizeof(struct sctphdr); michael@0: newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); michael@0: if (newm == NULL) { michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_ALIGN_TO_END(newm, len); michael@0: SCTP_BUF_LEN(newm) = len; michael@0: SCTP_BUF_NEXT(newm) = m; michael@0: m = newm; michael@0: packet_length = sctp_calculate_len(m); michael@0: sctphdr = mtod(m, struct sctphdr *); michael@0: sctphdr->src_port = src_port; michael@0: sctphdr->dest_port = dest_port; michael@0: sctphdr->v_tag = v_tag; michael@0: sctphdr->checksum = 0; michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: sctphdr->checksum = sctp_calculate_cksum(m, 0); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: if (tos_value == 0) { michael@0: tos_value = inp->ip_inp.inp.inp_ip_tos; michael@0: } michael@0: tos_value &= 0xfc; michael@0: if (ecn_ok) { michael@0: tos_value |= sctp_get_ect(stcb); michael@0: } michael@0: /* Don't alloc/free for each packet */ michael@0: if ((buffer = malloc(packet_length)) != NULL) { michael@0: m_copydata(m, 0, packet_length, buffer); michael@0: ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, packet_length, tos_value, nofragment_flag); michael@0: free(buffer); michael@0: } else { michael@0: ret = ENOMEM; michael@0: } michael@0: sctp_m_freem(m); michael@0: return (ret); michael@0: } michael@0: #endif michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", michael@0: ((struct sockaddr *)to)->sa_family); michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m; michael@0: struct sctp_nets *net; michael@0: struct sctp_init_chunk *init; michael@0: struct sctp_supported_addr_param *sup_addr; michael@0: struct sctp_adaptation_layer_indication *ali; michael@0: struct sctp_supported_chunk_types_param *pr_supported; michael@0: struct sctp_paramhdr *ph; michael@0: int cnt_inits_to = 0; michael@0: int ret; michael@0: uint16_t num_ext, chunk_len, padding_len, parameter_len; michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: /* INIT's always go to the primary (and usually ONLY address) */ michael@0: net = stcb->asoc.primary_destination; michael@0: if (net == NULL) { michael@0: net = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if (net == NULL) { michael@0: /* TSNH */ michael@0: return; michael@0: } michael@0: /* we confirm any address we send an INIT to */ michael@0: net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; michael@0: (void)sctp_set_primary_addr(stcb, NULL, net); michael@0: } else { michael@0: /* we confirm any address we send an INIT to */ michael@0: net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n"); michael@0: #ifdef INET6 michael@0: if (net->ro._l_addr.sa.sa_family == AF_INET6) { michael@0: /* michael@0: * special hook, if we are sending to link local it will not michael@0: * show up in our private address count. michael@0: */ michael@0: if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr)) michael@0: cnt_inits_to = 1; michael@0: } michael@0: #endif michael@0: if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { michael@0: /* This case should not happen */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n"); michael@0: return; michael@0: } michael@0: /* start the INIT timer */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net); michael@0: michael@0: m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA); michael@0: if (m == NULL) { michael@0: /* No memory, INIT timer will re-attempt. */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n"); michael@0: return; michael@0: } michael@0: chunk_len = (uint16_t)sizeof(struct sctp_init_chunk); michael@0: padding_len = 0; michael@0: /* michael@0: * assume peer supports asconf in order to be able to queue michael@0: * local address changes while an INIT is in flight and before michael@0: * the assoc is established. michael@0: */ michael@0: stcb->asoc.peer_supports_asconf = 1; michael@0: /* Now lets put the chunk header in place */ michael@0: init = mtod(m, struct sctp_init_chunk *); michael@0: /* now the chunk header */ michael@0: init->ch.chunk_type = SCTP_INITIATION; michael@0: init->ch.chunk_flags = 0; michael@0: /* fill in later from mbuf we build */ michael@0: init->ch.chunk_length = 0; michael@0: /* place in my tag */ michael@0: init->init.initiate_tag = htonl(stcb->asoc.my_vtag); michael@0: /* set up some of the credits. */ michael@0: init->init.a_rwnd = htonl(max(inp->sctp_socket?SCTP_SB_LIMIT_RCV(inp->sctp_socket):0, michael@0: SCTP_MINIMAL_RWND)); michael@0: init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams); michael@0: init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams); michael@0: init->init.initial_tsn = htonl(stcb->asoc.init_seq_number); michael@0: michael@0: if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) { michael@0: uint8_t i; michael@0: michael@0: parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); michael@0: if (stcb->asoc.scope.ipv4_addr_legal) { michael@0: parameter_len += (uint16_t)sizeof(uint16_t); michael@0: } michael@0: if (stcb->asoc.scope.ipv6_addr_legal) { michael@0: parameter_len += (uint16_t)sizeof(uint16_t); michael@0: } michael@0: sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t) + chunk_len); michael@0: sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE); michael@0: sup_addr->ph.param_length = htons(parameter_len); michael@0: i = 0; michael@0: if (stcb->asoc.scope.ipv4_addr_legal) { michael@0: sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS); michael@0: } michael@0: if (stcb->asoc.scope.ipv6_addr_legal) { michael@0: sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS); michael@0: } michael@0: padding_len = 4 - 2 * i; michael@0: chunk_len += parameter_len; michael@0: } michael@0: michael@0: /* Adaptation layer indication parameter */ michael@0: if (inp->sctp_ep.adaptation_layer_indicator_provided) { michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication); michael@0: ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len); michael@0: ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); michael@0: ali->ph.param_length = htons(parameter_len); michael@0: ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator); michael@0: chunk_len += parameter_len; michael@0: } michael@0: michael@0: if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) { michael@0: /* Add NAT friendly parameter. */ michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); michael@0: ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); michael@0: ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); michael@0: ph->param_length = htons(parameter_len); michael@0: chunk_len += parameter_len; michael@0: } michael@0: michael@0: /* now any cookie time extensions */ michael@0: if (stcb->asoc.cookie_preserve_req) { michael@0: struct sctp_cookie_perserve_param *cookie_preserve; michael@0: michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param); michael@0: cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t) + chunk_len); michael@0: cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE); michael@0: cookie_preserve->ph.param_length = htons(parameter_len); michael@0: cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req); michael@0: stcb->asoc.cookie_preserve_req = 0; michael@0: chunk_len += parameter_len; michael@0: } michael@0: michael@0: /* ECN parameter */ michael@0: if (stcb->asoc.ecn_allowed == 1) { michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); michael@0: ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); michael@0: ph->param_type = htons(SCTP_ECN_CAPABLE); michael@0: ph->param_length = htons(parameter_len); michael@0: chunk_len += parameter_len; michael@0: } michael@0: michael@0: /* And now tell the peer we do support PR-SCTP. */ michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); michael@0: ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); michael@0: ph->param_type = htons(SCTP_PRSCTP_SUPPORTED); michael@0: ph->param_length = htons(parameter_len); michael@0: chunk_len += parameter_len; michael@0: michael@0: /* And now tell the peer we do all the extensions */ michael@0: pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len); michael@0: pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); michael@0: num_ext = 0; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; michael@0: if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { michael@0: pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; michael@0: } michael@0: if (stcb->asoc.sctp_nr_sack_on_off == 1) { michael@0: pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; michael@0: } michael@0: parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext; michael@0: pr_supported->ph.param_length = htons(parameter_len); michael@0: padding_len = SCTP_SIZE32(parameter_len) - parameter_len; michael@0: chunk_len += parameter_len; michael@0: michael@0: /* add authentication parameters */ michael@0: if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { michael@0: /* attach RANDOM parameter, if available */ michael@0: if (stcb->asoc.authinfo.random != NULL) { michael@0: struct sctp_auth_random *randp; michael@0: michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len); michael@0: parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len; michael@0: /* random key already contains the header */ michael@0: memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len); michael@0: padding_len = SCTP_SIZE32(parameter_len) - parameter_len; michael@0: chunk_len += parameter_len; michael@0: } michael@0: /* add HMAC_ALGO parameter */ michael@0: if ((stcb->asoc.local_hmacs != NULL) && michael@0: (stcb->asoc.local_hmacs->num_algo > 0)) { michael@0: struct sctp_auth_hmac_algo *hmacs; michael@0: michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len); michael@0: parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) + michael@0: stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t)); michael@0: hmacs->ph.param_type = htons(SCTP_HMAC_LIST); michael@0: hmacs->ph.param_length = htons(parameter_len); michael@0: sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids); michael@0: padding_len = SCTP_SIZE32(parameter_len) - parameter_len; michael@0: chunk_len += parameter_len; michael@0: } michael@0: /* add CHUNKS parameter */ michael@0: if (sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks) > 0) { michael@0: struct sctp_auth_chunk_list *chunks; michael@0: michael@0: if (padding_len > 0) { michael@0: memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); michael@0: chunk_len += padding_len; michael@0: padding_len = 0; michael@0: } michael@0: chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len); michael@0: parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) + michael@0: sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks)); michael@0: chunks->ph.param_type = htons(SCTP_CHUNK_LIST); michael@0: chunks->ph.param_length = htons(parameter_len); michael@0: sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types); michael@0: padding_len = SCTP_SIZE32(parameter_len) - parameter_len; michael@0: chunk_len += parameter_len; michael@0: } michael@0: } michael@0: SCTP_BUF_LEN(m) = chunk_len; michael@0: michael@0: /* now the addresses */ michael@0: /* To optimize this we could put the scoping stuff michael@0: * into a structure and remove the individual uint8's from michael@0: * the assoc structure. Then we could just sifa in the michael@0: * address within the stcb. But for now this is a quick michael@0: * hack to get the address stuff teased apart. michael@0: */ michael@0: sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope, m, cnt_inits_to, &padding_len, &chunk_len); michael@0: michael@0: init->ch.chunk_length = htons(chunk_len); michael@0: if (padding_len > 0) { michael@0: struct mbuf *m_at, *mp_last; michael@0: michael@0: mp_last = NULL; michael@0: for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) michael@0: mp_last = m_at; michael@0: } michael@0: if ((mp_last == NULL) || sctp_add_pad_tombuf(mp_last, padding_len)) { michael@0: sctp_m_freem(m); michael@0: return; michael@0: } michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n"); michael@0: ret = sctp_lowlevel_chunk_output(inp, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: m, 0, NULL, 0, 0, 0, 0, michael@0: inp->sctp_lport, stcb->rport, htonl(0), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: } michael@0: michael@0: struct mbuf * michael@0: sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt, michael@0: int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly) michael@0: { michael@0: /* michael@0: * Given a mbuf containing an INIT or INIT-ACK with the param_offset michael@0: * being equal to the beginning of the params i.e. (iphlen + michael@0: * sizeof(struct sctp_init_msg) parse through the parameters to the michael@0: * end of the mbuf verifying that all parameters are known. michael@0: * michael@0: * For unknown parameters build and return a mbuf with michael@0: * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop michael@0: * processing this chunk stop, and set *abort_processing to 1. michael@0: * michael@0: * By having param_offset be pre-set to where parameters begin it is michael@0: * hoped that this routine may be reused in the future by new michael@0: * features. michael@0: */ michael@0: struct sctp_paramhdr *phdr, params; michael@0: michael@0: struct mbuf *mat, *op_err; michael@0: char tempbuf[SCTP_PARAM_BUFFER_SIZE]; michael@0: int at, limit, pad_needed; michael@0: uint16_t ptype, plen, padded_size; michael@0: int err_at; michael@0: michael@0: *abort_processing = 0; michael@0: mat = in_initpkt; michael@0: err_at = 0; michael@0: limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk); michael@0: at = param_offset; michael@0: op_err = NULL; michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n"); michael@0: phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); michael@0: while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) { michael@0: ptype = ntohs(phdr->param_type); michael@0: plen = ntohs(phdr->param_length); michael@0: if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) { michael@0: /* wacked parameter */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: limit -= SCTP_SIZE32(plen); michael@0: /*- michael@0: * All parameters for all chunks that we know/understand are michael@0: * listed here. We process them other places and make michael@0: * appropriate stop actions per the upper bits. However this michael@0: * is the generic routine processor's can call to get back michael@0: * an operr.. to either incorporate (init-ack) or send. michael@0: */ michael@0: padded_size = SCTP_SIZE32(plen); michael@0: switch (ptype) { michael@0: /* Param's with variable size */ michael@0: case SCTP_HEARTBEAT_INFO: michael@0: case SCTP_STATE_COOKIE: michael@0: case SCTP_UNRECOG_PARAM: michael@0: case SCTP_ERROR_CAUSE_IND: michael@0: /* ok skip fwd */ michael@0: at += padded_size; michael@0: break; michael@0: /* Param's with variable size within a range */ michael@0: case SCTP_CHUNK_LIST: michael@0: case SCTP_SUPPORTED_CHUNK_EXT: michael@0: if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_SUPPORTED_ADDRTYPE: michael@0: if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_RANDOM: michael@0: if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_SET_PRIM_ADDR: michael@0: case SCTP_DEL_IP_ADDRESS: michael@0: case SCTP_ADD_IP_ADDRESS: michael@0: if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) && michael@0: (padded_size != sizeof(struct sctp_asconf_addr_param))) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: /* Param's with a fixed size */ michael@0: case SCTP_IPV4_ADDRESS: michael@0: if (padded_size != sizeof(struct sctp_ipv4addr_param)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_IPV6_ADDRESS: michael@0: if (padded_size != sizeof(struct sctp_ipv6addr_param)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_COOKIE_PRESERVE: michael@0: if (padded_size != sizeof(struct sctp_cookie_perserve_param)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_HAS_NAT_SUPPORT: michael@0: *nat_friendly = 1; michael@0: /* fall through */ michael@0: case SCTP_PRSCTP_SUPPORTED: michael@0: michael@0: if (padded_size != sizeof(struct sctp_paramhdr)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_ECN_CAPABLE: michael@0: if (padded_size != sizeof(struct sctp_ecn_supported_param)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_ULP_ADAPTATION: michael@0: if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_SUCCESS_REPORT: michael@0: if (padded_size != sizeof(struct sctp_asconf_paramhdr)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen); michael@0: goto invalid_size; michael@0: } michael@0: at += padded_size; michael@0: break; michael@0: case SCTP_HOSTNAME_ADDRESS: michael@0: { michael@0: /* We can NOT handle HOST NAME addresses!! */ michael@0: int l_len; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n"); michael@0: *abort_processing = 1; michael@0: if (op_err == NULL) { michael@0: /* Ok need to try to get a mbuf */ michael@0: #ifdef INET6 michael@0: l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #else michael@0: l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #endif michael@0: l_len += plen; michael@0: l_len += sizeof(struct sctp_paramhdr); michael@0: op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: SCTP_BUF_LEN(op_err) = 0; michael@0: /* michael@0: * pre-reserve space for ip and sctp michael@0: * header and chunk hdr michael@0: */ michael@0: #ifdef INET6 michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); michael@0: #else michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); michael@0: #endif michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); michael@0: } michael@0: } michael@0: if (op_err) { michael@0: /* If we have space */ michael@0: struct sctp_paramhdr s; michael@0: michael@0: if (err_at % 4) { michael@0: uint32_t cpthis = 0; michael@0: michael@0: pad_needed = 4 - (err_at % 4); michael@0: m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); michael@0: err_at += pad_needed; michael@0: } michael@0: s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR); michael@0: s.param_length = htons(sizeof(s) + plen); michael@0: m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); michael@0: err_at += sizeof(s); michael@0: phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf),plen)); michael@0: if (phdr == NULL) { michael@0: sctp_m_freem(op_err); michael@0: /* michael@0: * we are out of memory but we still michael@0: * need to have a look at what to do michael@0: * (the system is in trouble michael@0: * though). michael@0: */ michael@0: return (NULL); michael@0: } michael@0: m_copyback(op_err, err_at, plen, (caddr_t)phdr); michael@0: } michael@0: return (op_err); michael@0: break; michael@0: } michael@0: default: michael@0: /* michael@0: * we do not recognize the parameter figure out what michael@0: * we do. michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype); michael@0: if ((ptype & 0x4000) == 0x4000) { michael@0: /* Report bit is set?? */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n"); michael@0: if (op_err == NULL) { michael@0: int l_len; michael@0: /* Ok need to try to get an mbuf */ michael@0: #ifdef INET6 michael@0: l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #else michael@0: l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #endif michael@0: l_len += plen; michael@0: l_len += sizeof(struct sctp_paramhdr); michael@0: op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: SCTP_BUF_LEN(op_err) = 0; michael@0: #ifdef INET6 michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); michael@0: #else michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); michael@0: #endif michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); michael@0: } michael@0: } michael@0: if (op_err) { michael@0: /* If we have space */ michael@0: struct sctp_paramhdr s; michael@0: michael@0: if (err_at % 4) { michael@0: uint32_t cpthis = 0; michael@0: michael@0: pad_needed = 4 - (err_at % 4); michael@0: m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); michael@0: err_at += pad_needed; michael@0: } michael@0: s.param_type = htons(SCTP_UNRECOG_PARAM); michael@0: s.param_length = htons(sizeof(s) + plen); michael@0: m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); michael@0: err_at += sizeof(s); michael@0: if (plen > sizeof(tempbuf)) { michael@0: plen = sizeof(tempbuf); michael@0: } michael@0: phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf),plen)); michael@0: if (phdr == NULL) { michael@0: sctp_m_freem(op_err); michael@0: /* michael@0: * we are out of memory but michael@0: * we still need to have a michael@0: * look at what to do (the michael@0: * system is in trouble michael@0: * though). michael@0: */ michael@0: op_err = NULL; michael@0: goto more_processing; michael@0: } michael@0: m_copyback(op_err, err_at, plen, (caddr_t)phdr); michael@0: err_at += plen; michael@0: } michael@0: } michael@0: more_processing: michael@0: if ((ptype & 0x8000) == 0x0000) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n"); michael@0: return (op_err); michael@0: } else { michael@0: /* skip this chunk and continue processing */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n"); michael@0: at += SCTP_SIZE32(plen); michael@0: } michael@0: break; michael@0: michael@0: } michael@0: phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); michael@0: } michael@0: return (op_err); michael@0: invalid_size: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n"); michael@0: *abort_processing = 1; michael@0: if ((op_err == NULL) && phdr) { michael@0: int l_len; michael@0: #ifdef INET6 michael@0: l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #else michael@0: l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: #endif michael@0: l_len += (2 * sizeof(struct sctp_paramhdr)); michael@0: op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: SCTP_BUF_LEN(op_err) = 0; michael@0: #ifdef INET6 michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); michael@0: #else michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); michael@0: #endif michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); michael@0: } michael@0: } michael@0: if ((op_err) && phdr) { michael@0: struct sctp_paramhdr s; michael@0: michael@0: if (err_at % 4) { michael@0: uint32_t cpthis = 0; michael@0: michael@0: pad_needed = 4 - (err_at % 4); michael@0: m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); michael@0: err_at += pad_needed; michael@0: } michael@0: s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION); michael@0: s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr)); michael@0: m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); michael@0: err_at += sizeof(s); michael@0: /* Only copy back the p-hdr that caused the issue */ michael@0: m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr); michael@0: } michael@0: return (op_err); michael@0: } michael@0: michael@0: static int michael@0: sctp_are_there_new_addresses(struct sctp_association *asoc, michael@0: struct mbuf *in_initpkt, int offset, struct sockaddr *src) michael@0: { michael@0: /* michael@0: * Given a INIT packet, look through the packet to verify that there michael@0: * are NO new addresses. As we go through the parameters add reports michael@0: * of any un-understood parameters that require an error. Also we michael@0: * must return (1) to drop the packet if we see a un-understood michael@0: * parameter that tells us to drop the chunk. michael@0: */ michael@0: struct sockaddr *sa_touse; michael@0: struct sockaddr *sa; michael@0: struct sctp_paramhdr *phdr, params; michael@0: uint16_t ptype, plen; michael@0: uint8_t fnd; michael@0: struct sctp_nets *net; michael@0: #ifdef INET michael@0: struct sockaddr_in sin4, *sa4; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6, *sa6; michael@0: #endif michael@0: michael@0: #ifdef INET michael@0: memset(&sin4, 0, sizeof(sin4)); michael@0: sin4.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin4.sin_len = sizeof(sin4); michael@0: #endif michael@0: #endif michael@0: #ifdef INET6 michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(sin6); michael@0: #endif michael@0: #endif michael@0: /* First what about the src address of the pkt ? */ michael@0: fnd = 0; michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: sa = (struct sockaddr *)&net->ro._l_addr; michael@0: if (sa->sa_family == src->sa_family) { michael@0: #ifdef INET michael@0: if (sa->sa_family == AF_INET) { michael@0: struct sockaddr_in *src4; michael@0: michael@0: sa4 = (struct sockaddr_in *)sa; michael@0: src4 = (struct sockaddr_in *)src; michael@0: if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (sa->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *src6; michael@0: michael@0: sa6 = (struct sockaddr_in6 *)sa; michael@0: src6 = (struct sockaddr_in6 *)src; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: if (fnd == 0) { michael@0: /* New address added! no need to look futher. */ michael@0: return (1); michael@0: } michael@0: /* Ok so far lets munge through the rest of the packet */ michael@0: offset += sizeof(struct sctp_init_chunk); michael@0: phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); michael@0: while (phdr) { michael@0: sa_touse = NULL; michael@0: ptype = ntohs(phdr->param_type); michael@0: plen = ntohs(phdr->param_length); michael@0: switch (ptype) { michael@0: #ifdef INET michael@0: case SCTP_IPV4_ADDRESS: michael@0: { michael@0: struct sctp_ipv4addr_param *p4, p4_buf; michael@0: michael@0: phdr = sctp_get_next_param(in_initpkt, offset, michael@0: (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); michael@0: if (plen != sizeof(struct sctp_ipv4addr_param) || michael@0: phdr == NULL) { michael@0: return (1); michael@0: } michael@0: p4 = (struct sctp_ipv4addr_param *)phdr; michael@0: sin4.sin_addr.s_addr = p4->addr; michael@0: sa_touse = (struct sockaddr *)&sin4; michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case SCTP_IPV6_ADDRESS: michael@0: { michael@0: struct sctp_ipv6addr_param *p6, p6_buf; michael@0: michael@0: phdr = sctp_get_next_param(in_initpkt, offset, michael@0: (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); michael@0: if (plen != sizeof(struct sctp_ipv6addr_param) || michael@0: phdr == NULL) { michael@0: return (1); michael@0: } michael@0: p6 = (struct sctp_ipv6addr_param *)phdr; michael@0: memcpy((caddr_t)&sin6.sin6_addr, p6->addr, michael@0: sizeof(p6->addr)); michael@0: sa_touse = (struct sockaddr *)&sin6; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: sa_touse = NULL; michael@0: break; michael@0: } michael@0: if (sa_touse) { michael@0: /* ok, sa_touse points to one to check */ michael@0: fnd = 0; michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: sa = (struct sockaddr *)&net->ro._l_addr; michael@0: if (sa->sa_family != sa_touse->sa_family) { michael@0: continue; michael@0: } michael@0: #ifdef INET michael@0: if (sa->sa_family == AF_INET) { michael@0: sa4 = (struct sockaddr_in *)sa; michael@0: if (sa4->sin_addr.s_addr == michael@0: sin4.sin_addr.s_addr) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (sa->sa_family == AF_INET6) { michael@0: sa6 = (struct sockaddr_in6 *)sa; michael@0: if (SCTP6_ARE_ADDR_EQUAL( michael@0: sa6, &sin6)) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: if (!fnd) { michael@0: /* New addr added! no need to look further */ michael@0: return (1); michael@0: } michael@0: } michael@0: offset += SCTP_SIZE32(plen); michael@0: phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * Given a MBUF chain that was sent into us containing an INIT. Build a michael@0: * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done michael@0: * a pullup to include IPv6/4header, SCTP header and initial part of INIT michael@0: * message (i.e. the struct sctp_init_msg). michael@0: */ michael@0: void michael@0: sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct mbuf *init_pkt, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_init_chunk *init_chk, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port, int hold_inp_lock) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last; michael@0: struct sctp_init_ack_chunk *initack; michael@0: struct sctp_adaptation_layer_indication *ali; michael@0: struct sctp_ecn_supported_param *ecn; michael@0: struct sctp_prsctp_supported_param *prsctp; michael@0: struct sctp_supported_chunk_types_param *pr_supported; michael@0: union sctp_sockstore *over_addr; michael@0: #ifdef INET michael@0: struct sockaddr_in *dst4 = (struct sockaddr_in *)dst; michael@0: struct sockaddr_in *src4 = (struct sockaddr_in *)src; michael@0: struct sockaddr_in *sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst; michael@0: struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src; michael@0: struct sockaddr_in6 *sin6; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: struct sockaddr_conn *dstconn = (struct sockaddr_conn *)dst; michael@0: struct sockaddr_conn *srcconn = (struct sockaddr_conn *)src; michael@0: struct sockaddr_conn *sconn; michael@0: #endif michael@0: struct sockaddr *to; michael@0: struct sctp_state_cookie stc; michael@0: struct sctp_nets *net = NULL; michael@0: uint8_t *signature = NULL; michael@0: int cnt_inits_to = 0; michael@0: uint16_t his_limit, i_want; michael@0: int abort_flag, padval; michael@0: int num_ext; michael@0: int p_len; michael@0: int nat_friendly = 0; michael@0: struct socket *so; michael@0: michael@0: if (stcb) { michael@0: asoc = &stcb->asoc; michael@0: } else { michael@0: asoc = NULL; michael@0: } michael@0: mp_last = NULL; michael@0: if ((asoc != NULL) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) && michael@0: (sctp_are_there_new_addresses(asoc, init_pkt, offset, src))) { michael@0: /* new addresses, out of here in non-cookie-wait states */ michael@0: /* michael@0: * Send a ABORT, we don't add the new address error clause michael@0: * though we even set the T bit and copy in the 0 tag.. this michael@0: * looks no different than if no listener was present. michael@0: */ michael@0: sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return; michael@0: } michael@0: abort_flag = 0; michael@0: op_err = sctp_arethere_unrecognized_parameters(init_pkt, michael@0: (offset + sizeof(struct sctp_init_chunk)), michael@0: &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly); michael@0: if (abort_flag) { michael@0: do_a_abort: michael@0: sctp_send_abort(init_pkt, iphlen, src, dst, sh, michael@0: init_chk->init.initiate_tag, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return; michael@0: } michael@0: m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m == NULL) { michael@0: /* No memory, INIT timer will re-attempt. */ michael@0: if (op_err) michael@0: sctp_m_freem(op_err); michael@0: return; michael@0: } michael@0: SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk); michael@0: michael@0: /* michael@0: * We might not overwrite the identification[] completely and on michael@0: * some platforms time_entered will contain some padding. michael@0: * Therefore zero out the cookie to avoid putting michael@0: * uninitialized memory on the wire. michael@0: */ michael@0: memset(&stc, 0, sizeof(struct sctp_state_cookie)); michael@0: michael@0: /* the time I built cookie */ michael@0: (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered); michael@0: michael@0: /* populate any tie tags */ michael@0: if (asoc != NULL) { michael@0: /* unlock before tag selections */ michael@0: stc.tie_tag_my_vtag = asoc->my_vtag_nonce; michael@0: stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce; michael@0: stc.cookie_life = asoc->cookie_life; michael@0: net = asoc->primary_destination; michael@0: } else { michael@0: stc.tie_tag_my_vtag = 0; michael@0: stc.tie_tag_peer_vtag = 0; michael@0: /* life I will award this cookie */ michael@0: stc.cookie_life = inp->sctp_ep.def_cookie_life; michael@0: } michael@0: michael@0: /* copy in the ports for later check */ michael@0: stc.myport = sh->dest_port; michael@0: stc.peerport = sh->src_port; michael@0: michael@0: /* michael@0: * If we wanted to honor cookie life extentions, we would add to michael@0: * stc.cookie_life. For now we should NOT honor any extension michael@0: */ michael@0: stc.site_scope = stc.local_scope = stc.loopback_scope = 0; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: stc.ipv6_addr_legal = 1; michael@0: if (SCTP_IPV6_V6ONLY(inp)) { michael@0: stc.ipv4_addr_legal = 0; michael@0: } else { michael@0: stc.ipv4_addr_legal = 1; michael@0: } michael@0: #if defined(__Userspace__) michael@0: stc.conn_addr_legal = 0; michael@0: #endif michael@0: } else { michael@0: stc.ipv6_addr_legal = 0; michael@0: #if defined(__Userspace__) michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { michael@0: stc.conn_addr_legal = 1; michael@0: stc.ipv4_addr_legal = 0; michael@0: } else { michael@0: stc.conn_addr_legal = 0; michael@0: stc.ipv4_addr_legal = 1; michael@0: } michael@0: #else michael@0: stc.ipv4_addr_legal = 1; michael@0: #endif michael@0: } michael@0: #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE michael@0: stc.ipv4_scope = 1; michael@0: #else michael@0: stc.ipv4_scope = 0; michael@0: #endif michael@0: if (net == NULL) { michael@0: to = src; michael@0: switch (dst->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: /* lookup address */ michael@0: stc.address[0] = src4->sin_addr.s_addr; michael@0: stc.address[1] = 0; michael@0: stc.address[2] = 0; michael@0: stc.address[3] = 0; michael@0: stc.addr_type = SCTP_IPV4_ADDRESS; michael@0: /* local from address */ michael@0: stc.laddress[0] = dst4->sin_addr.s_addr; michael@0: stc.laddress[1] = 0; michael@0: stc.laddress[2] = 0; michael@0: stc.laddress[3] = 0; michael@0: stc.laddr_type = SCTP_IPV4_ADDRESS; michael@0: /* scope_id is only for v6 */ michael@0: stc.scope_id = 0; michael@0: #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE michael@0: if (IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) { michael@0: stc.ipv4_scope = 1; michael@0: } michael@0: #else michael@0: stc.ipv4_scope = 1; michael@0: #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ michael@0: /* Must use the address in this case */ michael@0: if (sctp_is_address_on_local_host(src, vrf_id)) { michael@0: stc.loopback_scope = 1; michael@0: stc.ipv4_scope = 1; michael@0: stc.site_scope = 1; michael@0: stc.local_scope = 0; michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: stc.addr_type = SCTP_IPV6_ADDRESS; michael@0: memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr)); michael@0: #if defined(__FreeBSD__) && (((__FreeBSD_version < 900000) && (__FreeBSD_version >= 804000)) || (__FreeBSD_version > 900000)) michael@0: stc.scope_id = in6_getscope(&src6->sin6_addr); michael@0: #else michael@0: stc.scope_id = 0; michael@0: #endif michael@0: if (sctp_is_address_on_local_host(src, vrf_id)) { michael@0: stc.loopback_scope = 1; michael@0: stc.local_scope = 0; michael@0: stc.site_scope = 1; michael@0: stc.ipv4_scope = 1; michael@0: } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr)) { michael@0: /* michael@0: * If the new destination is a LINK_LOCAL we michael@0: * must have common both site and local michael@0: * scope. Don't set local scope though since michael@0: * we must depend on the source to be added michael@0: * implicitly. We cannot assure just because michael@0: * we share one link that all links are michael@0: * common. michael@0: */ michael@0: #if defined(__APPLE__) michael@0: /* Mac OS X currently doesn't have in6_getscope() */ michael@0: stc.scope_id = src6->sin6_addr.s6_addr16[1]; michael@0: #endif michael@0: stc.local_scope = 0; michael@0: stc.site_scope = 1; michael@0: stc.ipv4_scope = 1; michael@0: /* michael@0: * we start counting for the private address michael@0: * stuff at 1. since the link local we michael@0: * source from won't show up in our scoped michael@0: * count. michael@0: */ michael@0: cnt_inits_to = 1; michael@0: /* pull out the scope_id from incoming pkt */ michael@0: } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr)) { michael@0: /* michael@0: * If the new destination is SITE_LOCAL then michael@0: * we must have site scope in common. michael@0: */ michael@0: stc.site_scope = 1; michael@0: } michael@0: memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr)); michael@0: stc.laddr_type = SCTP_IPV6_ADDRESS; michael@0: break; michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: /* lookup address */ michael@0: stc.address[0] = 0; michael@0: stc.address[1] = 0; michael@0: stc.address[2] = 0; michael@0: stc.address[3] = 0; michael@0: memcpy(&stc.address, &srcconn->sconn_addr, sizeof(void *)); michael@0: stc.addr_type = SCTP_CONN_ADDRESS; michael@0: /* local from address */ michael@0: stc.laddress[0] = 0; michael@0: stc.laddress[1] = 0; michael@0: stc.laddress[2] = 0; michael@0: stc.laddress[3] = 0; michael@0: memcpy(&stc.laddress, &dstconn->sconn_addr, sizeof(void *)); michael@0: stc.laddr_type = SCTP_CONN_ADDRESS; michael@0: /* scope_id is only for v6 */ michael@0: stc.scope_id = 0; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: goto do_a_abort; michael@0: break; michael@0: } michael@0: } else { michael@0: /* set the scope per the existing tcb */ michael@0: michael@0: #ifdef INET6 michael@0: struct sctp_nets *lnet; michael@0: #endif michael@0: michael@0: stc.loopback_scope = asoc->scope.loopback_scope; michael@0: stc.ipv4_scope = asoc->scope.ipv4_local_scope; michael@0: stc.site_scope = asoc->scope.site_scope; michael@0: stc.local_scope = asoc->scope.local_scope; michael@0: #ifdef INET6 michael@0: /* Why do we not consider IPv4 LL addresses? */ michael@0: TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { michael@0: if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) { michael@0: if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) { michael@0: /* michael@0: * if we have a LL address, start michael@0: * counting at 1. michael@0: */ michael@0: cnt_inits_to = 1; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: /* use the net pointer */ michael@0: to = (struct sockaddr *)&net->ro._l_addr; michael@0: switch (to->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sin = (struct sockaddr_in *)to; michael@0: stc.address[0] = sin->sin_addr.s_addr; michael@0: stc.address[1] = 0; michael@0: stc.address[2] = 0; michael@0: stc.address[3] = 0; michael@0: stc.addr_type = SCTP_IPV4_ADDRESS; michael@0: if (net->src_addr_selected == 0) { michael@0: /* michael@0: * strange case here, the INIT should have michael@0: * did the selection. michael@0: */ michael@0: net->ro._s_addr = sctp_source_address_selection(inp, michael@0: stcb, (sctp_route_t *)&net->ro, michael@0: net, 0, vrf_id); michael@0: if (net->ro._s_addr == NULL) michael@0: return; michael@0: michael@0: net->src_addr_selected = 1; michael@0: michael@0: } michael@0: stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr; michael@0: stc.laddress[1] = 0; michael@0: stc.laddress[2] = 0; michael@0: stc.laddress[3] = 0; michael@0: stc.laddr_type = SCTP_IPV4_ADDRESS; michael@0: /* scope_id is only for v6 */ michael@0: stc.scope_id = 0; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sin6 = (struct sockaddr_in6 *)to; michael@0: memcpy(&stc.address, &sin6->sin6_addr, michael@0: sizeof(struct in6_addr)); michael@0: stc.addr_type = SCTP_IPV6_ADDRESS; michael@0: stc.scope_id = sin6->sin6_scope_id; michael@0: if (net->src_addr_selected == 0) { michael@0: /* michael@0: * strange case here, the INIT should have michael@0: * done the selection. michael@0: */ michael@0: net->ro._s_addr = sctp_source_address_selection(inp, michael@0: stcb, (sctp_route_t *)&net->ro, michael@0: net, 0, vrf_id); michael@0: if (net->ro._s_addr == NULL) michael@0: return; michael@0: michael@0: net->src_addr_selected = 1; michael@0: } michael@0: memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr, michael@0: sizeof(struct in6_addr)); michael@0: stc.laddr_type = SCTP_IPV6_ADDRESS; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: sconn = (struct sockaddr_conn *)to; michael@0: stc.address[0] = 0; michael@0: stc.address[1] = 0; michael@0: stc.address[2] = 0; michael@0: stc.address[3] = 0; michael@0: memcpy(&stc.address, &sconn->sconn_addr, sizeof(void *)); michael@0: stc.addr_type = SCTP_CONN_ADDRESS; michael@0: stc.laddress[0] = 0; michael@0: stc.laddress[1] = 0; michael@0: stc.laddress[2] = 0; michael@0: stc.laddress[3] = 0; michael@0: memcpy(&stc.laddress, &sconn->sconn_addr, sizeof(void *)); michael@0: stc.laddr_type = SCTP_CONN_ADDRESS; michael@0: stc.scope_id = 0; michael@0: break; michael@0: #endif michael@0: } michael@0: } michael@0: /* Now lets put the SCTP header in place */ michael@0: initack = mtod(m, struct sctp_init_ack_chunk *); michael@0: /* Save it off for quick ref */ michael@0: stc.peers_vtag = init_chk->init.initiate_tag; michael@0: /* who are we */ michael@0: memcpy(stc.identification, SCTP_VERSION_STRING, michael@0: min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification))); michael@0: memset(stc.reserved, 0, SCTP_RESERVE_SPACE); michael@0: /* now the chunk header */ michael@0: initack->ch.chunk_type = SCTP_INITIATION_ACK; michael@0: initack->ch.chunk_flags = 0; michael@0: /* fill in later from mbuf we build */ michael@0: initack->ch.chunk_length = 0; michael@0: /* place in my tag */ michael@0: if ((asoc != NULL) && michael@0: ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) { michael@0: /* re-use the v-tags and init-seq here */ michael@0: initack->init.initiate_tag = htonl(asoc->my_vtag); michael@0: initack->init.initial_tsn = htonl(asoc->init_seq_number); michael@0: } else { michael@0: uint32_t vtag, itsn; michael@0: if (hold_inp_lock) { michael@0: SCTP_INP_INCR_REF(inp); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: if (asoc) { michael@0: atomic_add_int(&asoc->refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: new_tag: michael@0: vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); michael@0: if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) { michael@0: /* Got a duplicate vtag on some guy behind a nat michael@0: * make sure we don't use it. michael@0: */ michael@0: goto new_tag; michael@0: } michael@0: initack->init.initiate_tag = htonl(vtag); michael@0: /* get a TSN to use too */ michael@0: itsn = sctp_select_initial_TSN(&inp->sctp_ep); michael@0: initack->init.initial_tsn = htonl(itsn); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&asoc->refcnt, -1); michael@0: } else { michael@0: vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); michael@0: initack->init.initiate_tag = htonl(vtag); michael@0: /* get a TSN to use too */ michael@0: initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep)); michael@0: } michael@0: if (hold_inp_lock) { michael@0: SCTP_INP_RLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: } michael@0: /* save away my tag to */ michael@0: stc.my_vtag = initack->init.initiate_tag; michael@0: michael@0: /* set up some of the credits. */ michael@0: so = inp->sctp_socket; michael@0: if (so == NULL) { michael@0: /* memory problem */ michael@0: sctp_m_freem(m); michael@0: return; michael@0: } else { michael@0: initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND)); michael@0: } michael@0: /* set what I want */ michael@0: his_limit = ntohs(init_chk->init.num_inbound_streams); michael@0: /* choose what I want */ michael@0: if (asoc != NULL) { michael@0: if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) { michael@0: i_want = asoc->streamoutcnt; michael@0: } else { michael@0: i_want = inp->sctp_ep.pre_open_stream_count; michael@0: } michael@0: } else { michael@0: i_want = inp->sctp_ep.pre_open_stream_count; michael@0: } michael@0: if (his_limit < i_want) { michael@0: /* I Want more :< */ michael@0: initack->init.num_outbound_streams = init_chk->init.num_inbound_streams; michael@0: } else { michael@0: /* I can have what I want :> */ michael@0: initack->init.num_outbound_streams = htons(i_want); michael@0: } michael@0: /* tell him his limit. */ michael@0: initack->init.num_inbound_streams = michael@0: htons(inp->sctp_ep.max_open_streams_intome); michael@0: michael@0: /* adaptation layer indication parameter */ michael@0: if (inp->sctp_ep.adaptation_layer_indicator_provided) { michael@0: ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack)); michael@0: ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); michael@0: ali->ph.param_length = htons(sizeof(*ali)); michael@0: ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator); michael@0: SCTP_BUF_LEN(m) += sizeof(*ali); michael@0: ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali)); michael@0: } else { michael@0: ecn = (struct sctp_ecn_supported_param *)((caddr_t)initack + sizeof(*initack)); michael@0: } michael@0: michael@0: /* ECN parameter */ michael@0: if (((asoc != NULL) && (asoc->ecn_allowed == 1)) || michael@0: (inp->sctp_ecn_enable == 1)) { michael@0: ecn->ph.param_type = htons(SCTP_ECN_CAPABLE); michael@0: ecn->ph.param_length = htons(sizeof(*ecn)); michael@0: SCTP_BUF_LEN(m) += sizeof(*ecn); michael@0: michael@0: prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn + michael@0: sizeof(*ecn)); michael@0: } else { michael@0: prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn); michael@0: } michael@0: /* And now tell the peer we do pr-sctp */ michael@0: prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED); michael@0: prsctp->ph.param_length = htons(sizeof(*prsctp)); michael@0: SCTP_BUF_LEN(m) += sizeof(*prsctp); michael@0: if (nat_friendly) { michael@0: /* Add NAT friendly parameter */ michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr)); michael@0: SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr); michael@0: } michael@0: /* And now tell the peer we do all the extensions */ michael@0: pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); michael@0: num_ext = 0; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; michael@0: pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; michael@0: if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) michael@0: pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; michael@0: if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) michael@0: pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; michael@0: p_len = sizeof(*pr_supported) + num_ext; michael@0: pr_supported->ph.param_length = htons(p_len); michael@0: bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len); michael@0: SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); michael@0: michael@0: /* add authentication parameters */ michael@0: if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { michael@0: struct sctp_auth_random *randp; michael@0: struct sctp_auth_hmac_algo *hmacs; michael@0: struct sctp_auth_chunk_list *chunks; michael@0: uint16_t random_len; michael@0: michael@0: /* generate and add RANDOM parameter */ michael@0: random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; michael@0: randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: randp->ph.param_type = htons(SCTP_RANDOM); michael@0: p_len = sizeof(*randp) + random_len; michael@0: randp->ph.param_length = htons(p_len); michael@0: SCTP_READ_RANDOM(randp->random_data, random_len); michael@0: /* zero out any padding required */ michael@0: bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len); michael@0: SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); michael@0: michael@0: /* add HMAC_ALGO parameter */ michael@0: hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs, michael@0: (uint8_t *) hmacs->hmac_ids); michael@0: if (p_len > 0) { michael@0: p_len += sizeof(*hmacs); michael@0: hmacs->ph.param_type = htons(SCTP_HMAC_LIST); michael@0: hmacs->ph.param_length = htons(p_len); michael@0: /* zero out any padding required */ michael@0: bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len); michael@0: SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); michael@0: } michael@0: /* add CHUNKS parameter */ michael@0: chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks, michael@0: chunks->chunk_types); michael@0: if (p_len > 0) { michael@0: p_len += sizeof(*chunks); michael@0: chunks->ph.param_type = htons(SCTP_CHUNK_LIST); michael@0: chunks->ph.param_length = htons(p_len); michael@0: /* zero out any padding required */ michael@0: bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len); michael@0: SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); michael@0: } michael@0: } michael@0: m_at = m; michael@0: /* now the addresses */ michael@0: { michael@0: struct sctp_scoping scp; michael@0: /* To optimize this we could put the scoping stuff michael@0: * into a structure and remove the individual uint8's from michael@0: * the stc structure. Then we could just sifa in the michael@0: * address within the stc.. but for now this is a quick michael@0: * hack to get the address stuff teased apart. michael@0: */ michael@0: scp.ipv4_addr_legal = stc.ipv4_addr_legal; michael@0: scp.ipv6_addr_legal = stc.ipv6_addr_legal; michael@0: #if defined(__Userspace__) michael@0: scp.conn_addr_legal = stc.conn_addr_legal; michael@0: #endif michael@0: scp.loopback_scope = stc.loopback_scope; michael@0: scp.ipv4_local_scope = stc.ipv4_scope; michael@0: scp.local_scope = stc.local_scope; michael@0: scp.site_scope = stc.site_scope; michael@0: m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to, NULL, NULL); michael@0: } michael@0: michael@0: /* tack on the operational error if present */ michael@0: if (op_err) { michael@0: struct mbuf *ol; michael@0: int llen; michael@0: llen = 0; michael@0: ol = op_err; michael@0: michael@0: while (ol) { michael@0: llen += SCTP_BUF_LEN(ol); michael@0: ol = SCTP_BUF_NEXT(ol); michael@0: } michael@0: if (llen % 4) { michael@0: /* must add a pad to the param */ michael@0: uint32_t cpthis = 0; michael@0: int padlen; michael@0: michael@0: padlen = 4 - (llen % 4); michael@0: m_copyback(op_err, llen, padlen, (caddr_t)&cpthis); michael@0: } michael@0: while (SCTP_BUF_NEXT(m_at) != NULL) { michael@0: m_at = SCTP_BUF_NEXT(m_at); michael@0: } michael@0: SCTP_BUF_NEXT(m_at) = op_err; michael@0: while (SCTP_BUF_NEXT(m_at) != NULL) { michael@0: m_at = SCTP_BUF_NEXT(m_at); michael@0: } michael@0: } michael@0: /* pre-calulate the size and update pkt header and chunk header */ michael@0: p_len = 0; michael@0: for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) { michael@0: p_len += SCTP_BUF_LEN(m_tmp); michael@0: if (SCTP_BUF_NEXT(m_tmp) == NULL) { michael@0: /* m_tmp should now point to last one */ michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* Now we must build a cookie */ michael@0: m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature); michael@0: if (m_cookie == NULL) { michael@0: /* memory problem */ michael@0: sctp_m_freem(m); michael@0: return; michael@0: } michael@0: /* Now append the cookie to the end and update the space/size */ michael@0: SCTP_BUF_NEXT(m_tmp) = m_cookie; michael@0: michael@0: for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) { michael@0: p_len += SCTP_BUF_LEN(m_tmp); michael@0: if (SCTP_BUF_NEXT(m_tmp) == NULL) { michael@0: /* m_tmp should now point to last one */ michael@0: mp_last = m_tmp; michael@0: break; michael@0: } michael@0: } michael@0: /* Place in the size, but we don't include michael@0: * the last pad (if any) in the INIT-ACK. michael@0: */ michael@0: initack->ch.chunk_length = htons(p_len); michael@0: michael@0: /* Time to sign the cookie, we don't sign over the cookie michael@0: * signature though thus we set trailer. michael@0: */ michael@0: (void)sctp_hmac_m(SCTP_HMAC, michael@0: (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)], michael@0: SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr), michael@0: (uint8_t *)signature, SCTP_SIGNATURE_SIZE); michael@0: /* michael@0: * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return michael@0: * here since the timer will drive a retranmission. michael@0: */ michael@0: padval = p_len % 4; michael@0: if ((padval) && (mp_last)) { michael@0: /* see my previous comments on mp_last */ michael@0: if (sctp_add_pad_tombuf(mp_last, (4 - padval))) { michael@0: /* Houston we have a problem, no space */ michael@0: sctp_m_freem(m); michael@0: return; michael@0: } michael@0: } michael@0: if (stc.loopback_scope) { michael@0: over_addr = (union sctp_sockstore *)dst; michael@0: } else { michael@0: over_addr = NULL; michael@0: } michael@0: michael@0: (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0, michael@0: 0, 0, michael@0: inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag, michael@0: port, over_addr, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: SCTP_SO_NOT_LOCKED); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_prune_prsctp(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: struct sctp_sndrcvinfo *srcv, michael@0: int dataout) michael@0: { michael@0: int freed_spc = 0; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if ((asoc->peer_supports_prsctp) && michael@0: (asoc->sent_queue_cnt_removeable > 0)) { michael@0: TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { michael@0: /* michael@0: * Look for chunks marked with the PR_SCTP flag AND michael@0: * the buffer space flag. If the one being sent is michael@0: * equal or greater priority then purge the old one michael@0: * and free some space. michael@0: */ michael@0: if (PR_SCTP_BUF_ENABLED(chk->flags)) { michael@0: /* michael@0: * This one is PR-SCTP AND buffer space michael@0: * limited type michael@0: */ michael@0: if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { michael@0: /* michael@0: * Lower numbers equates to higher michael@0: * priority so if the one we are michael@0: * looking at has a larger or equal michael@0: * priority we want to drop the data michael@0: * and NOT retransmit it. michael@0: */ michael@0: if (chk->data) { michael@0: /* michael@0: * We release the book_size michael@0: * if the mbuf is here michael@0: */ michael@0: int ret_spc; michael@0: uint8_t sent; michael@0: michael@0: if (chk->sent > SCTP_DATAGRAM_UNSENT) michael@0: sent = 1; michael@0: else michael@0: sent = 0; michael@0: ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, michael@0: sent, michael@0: SCTP_SO_LOCKED); michael@0: freed_spc += ret_spc; michael@0: if (freed_spc >= dataout) { michael@0: return; michael@0: } michael@0: } /* if chunk was present */ michael@0: } /* if of sufficent priority */ michael@0: } /* if chunk has enabled */ michael@0: } /* tailqforeach */ michael@0: michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { michael@0: /* Here we must move to the sent queue and mark */ michael@0: if (PR_SCTP_BUF_ENABLED(chk->flags)) { michael@0: if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { michael@0: if (chk->data) { michael@0: /* michael@0: * We release the book_size michael@0: * if the mbuf is here michael@0: */ michael@0: int ret_spc; michael@0: michael@0: ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, michael@0: 0, SCTP_SO_LOCKED); michael@0: michael@0: freed_spc += ret_spc; michael@0: if (freed_spc >= dataout) { michael@0: return; michael@0: } michael@0: } /* end if chk->data */ michael@0: } /* end if right class */ michael@0: } /* end if chk pr-sctp */ michael@0: } /* tailqforeachsafe (chk) */ michael@0: } /* if enabled in asoc */ michael@0: } michael@0: michael@0: int michael@0: sctp_get_frag_point(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc) michael@0: { michael@0: int siz, ovh; michael@0: michael@0: /* michael@0: * For endpoints that have both v6 and v4 addresses we must reserve michael@0: * room for the ipv6 header, for those that are only dealing with V4 michael@0: * we use a larger frag point. michael@0: */ michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: ovh = SCTP_MED_OVERHEAD; michael@0: } else { michael@0: ovh = SCTP_MED_V4_OVERHEAD; michael@0: } michael@0: michael@0: if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu) michael@0: siz = asoc->smallest_mtu - ovh; michael@0: else michael@0: siz = (stcb->asoc.sctp_frag_point - ovh); michael@0: /* michael@0: * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { michael@0: */ michael@0: /* A data chunk MUST fit in a cluster */ michael@0: /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */ michael@0: /* } */ michael@0: michael@0: /* adjust for an AUTH chunk if DATA requires auth */ michael@0: if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) michael@0: siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: michael@0: if (siz % 4) { michael@0: /* make it an even word boundary please */ michael@0: siz -= (siz % 4); michael@0: } michael@0: return (siz); michael@0: } michael@0: michael@0: static void michael@0: sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp) michael@0: { michael@0: /* michael@0: * We assume that the user wants PR_SCTP_TTL if the user michael@0: * provides a positive lifetime but does not specify any michael@0: * PR_SCTP policy. michael@0: */ michael@0: if (PR_SCTP_ENABLED(sp->sinfo_flags)) { michael@0: sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); michael@0: } else if (sp->timetolive > 0) { michael@0: sp->sinfo_flags |= SCTP_PR_SCTP_TTL; michael@0: sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); michael@0: } else { michael@0: return; michael@0: } michael@0: switch (PR_SCTP_POLICY(sp->sinfo_flags)) { michael@0: case CHUNK_FLAGS_PR_SCTP_BUF: michael@0: /* michael@0: * Time to live is a priority stored in tv_sec when michael@0: * doing the buffer drop thing. michael@0: */ michael@0: sp->ts.tv_sec = sp->timetolive; michael@0: sp->ts.tv_usec = 0; michael@0: break; michael@0: case CHUNK_FLAGS_PR_SCTP_TTL: michael@0: { michael@0: struct timeval tv; michael@0: (void)SCTP_GETTIME_TIMEVAL(&sp->ts); michael@0: tv.tv_sec = sp->timetolive / 1000; michael@0: tv.tv_usec = (sp->timetolive * 1000) % 1000000; michael@0: /* TODO sctp_constants.h needs alternative time macros when michael@0: * _KERNEL is undefined. michael@0: */ michael@0: #ifndef __FreeBSD__ michael@0: timeradd(&sp->ts, &tv, &sp->ts); michael@0: #else michael@0: timevaladd(&sp->ts, &tv); michael@0: #endif michael@0: } michael@0: break; michael@0: case CHUNK_FLAGS_PR_SCTP_RTX: michael@0: /* michael@0: * Time to live is a the number or retransmissions michael@0: * stored in tv_sec. michael@0: */ michael@0: sp->ts.tv_sec = sp->timetolive; michael@0: sp->ts.tv_usec = 0; michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_USRREQ1, michael@0: "Unknown PR_SCTP policy %u.\n", michael@0: PR_SCTP_POLICY(sp->sinfo_flags)); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: sctp_msg_append(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, michael@0: struct mbuf *m, michael@0: struct sctp_sndrcvinfo *srcv, int hold_stcb_lock) michael@0: { michael@0: int error = 0; michael@0: struct mbuf *at; michael@0: struct sctp_stream_queue_pending *sp = NULL; michael@0: struct sctp_stream_out *strm; michael@0: michael@0: /* Given an mbuf chain, put it michael@0: * into the association send queue and michael@0: * place it on the wheel michael@0: */ michael@0: if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) { michael@0: /* Invalid stream number */ michael@0: SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_now; michael@0: } michael@0: if ((stcb->asoc.stream_locked) && michael@0: (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_now; michael@0: } michael@0: strm = &stcb->asoc.strmout[srcv->sinfo_stream]; michael@0: /* Now can we send this? */ michael@0: if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || michael@0: (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) { michael@0: /* got data while shutting down */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); michael@0: error = ECONNRESET; michael@0: goto out_now; michael@0: } michael@0: sctp_alloc_a_strmoq(stcb, sp); michael@0: if (sp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: error = ENOMEM; michael@0: goto out_now; michael@0: } michael@0: sp->sinfo_flags = srcv->sinfo_flags; michael@0: sp->timetolive = srcv->sinfo_timetolive; michael@0: sp->ppid = srcv->sinfo_ppid; michael@0: sp->context = srcv->sinfo_context; michael@0: if (sp->sinfo_flags & SCTP_ADDR_OVER) { michael@0: sp->net = net; michael@0: atomic_add_int(&sp->net->ref_count, 1); michael@0: } else { michael@0: sp->net = NULL; michael@0: } michael@0: (void)SCTP_GETTIME_TIMEVAL(&sp->ts); michael@0: sp->stream = srcv->sinfo_stream; michael@0: sp->msg_is_complete = 1; michael@0: sp->sender_all_done = 1; michael@0: sp->some_taken = 0; michael@0: sp->data = m; michael@0: sp->tail_mbuf = NULL; michael@0: sctp_set_prsctp_policy(sp); michael@0: /* We could in theory (for sendall) sifa the length michael@0: * in, but we would still have to hunt through the michael@0: * chain since we need to setup the tail_mbuf michael@0: */ michael@0: sp->length = 0; michael@0: for (at = m; at; at = SCTP_BUF_NEXT(at)) { michael@0: if (SCTP_BUF_NEXT(at) == NULL) michael@0: sp->tail_mbuf = at; michael@0: sp->length += SCTP_BUF_LEN(at); michael@0: } michael@0: if (srcv->sinfo_keynumber_valid) { michael@0: sp->auth_keyid = srcv->sinfo_keynumber; michael@0: } else { michael@0: sp->auth_keyid = stcb->asoc.authinfo.active_keyid; michael@0: } michael@0: if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { michael@0: sctp_auth_key_acquire(stcb, sp->auth_keyid); michael@0: sp->holds_key_ref = 1; michael@0: } michael@0: if (hold_stcb_lock == 0) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: } michael@0: sctp_snd_sb_alloc(stcb, sp->length); michael@0: atomic_add_int(&stcb->asoc.stream_queue_cnt, 1); michael@0: TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); michael@0: stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1); michael@0: m = NULL; michael@0: if (hold_stcb_lock == 0) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: out_now: michael@0: if (m) { michael@0: sctp_m_freem(m); michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: static struct mbuf * michael@0: sctp_copy_mbufchain(struct mbuf *clonechain, michael@0: struct mbuf *outchain, michael@0: struct mbuf **endofchain, michael@0: int can_take_mbuf, michael@0: int sizeofcpy, michael@0: uint8_t copy_by_ref) michael@0: { michael@0: struct mbuf *m; michael@0: struct mbuf *appendchain; michael@0: caddr_t cp; michael@0: int len; michael@0: michael@0: if (endofchain == NULL) { michael@0: /* error */ michael@0: error_out: michael@0: if (outchain) michael@0: sctp_m_freem(outchain); michael@0: return (NULL); michael@0: } michael@0: if (can_take_mbuf) { michael@0: appendchain = clonechain; michael@0: } else { michael@0: if (!copy_by_ref && michael@0: #if defined(__Panda__) michael@0: 0 michael@0: #else michael@0: (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN))) michael@0: #endif michael@0: ) { michael@0: /* Its not in a cluster */ michael@0: if (*endofchain == NULL) { michael@0: /* lets get a mbuf cluster */ michael@0: if (outchain == NULL) { michael@0: /* This is the general case */ michael@0: new_mbuf: michael@0: outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (outchain == NULL) { michael@0: goto error_out; michael@0: } michael@0: SCTP_BUF_LEN(outchain) = 0; michael@0: *endofchain = outchain; michael@0: /* get the prepend space */ michael@0: SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV+4)); michael@0: } else { michael@0: /* We really should not get a NULL in endofchain */ michael@0: /* find end */ michael@0: m = outchain; michael@0: while (m) { michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: *endofchain = m; michael@0: break; michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: /* sanity */ michael@0: if (*endofchain == NULL) { michael@0: /* huh, TSNH XXX maybe we should panic */ michael@0: sctp_m_freem(outchain); michael@0: goto new_mbuf; michael@0: } michael@0: } michael@0: /* get the new end of length */ michael@0: len = M_TRAILINGSPACE(*endofchain); michael@0: } else { michael@0: /* how much is left at the end? */ michael@0: len = M_TRAILINGSPACE(*endofchain); michael@0: } michael@0: /* Find the end of the data, for appending */ michael@0: cp = (mtod((*endofchain), caddr_t) + SCTP_BUF_LEN((*endofchain))); michael@0: michael@0: /* Now lets copy it out */ michael@0: if (len >= sizeofcpy) { michael@0: /* It all fits, copy it in */ michael@0: m_copydata(clonechain, 0, sizeofcpy, cp); michael@0: SCTP_BUF_LEN((*endofchain)) += sizeofcpy; michael@0: } else { michael@0: /* fill up the end of the chain */ michael@0: if (len > 0) { michael@0: m_copydata(clonechain, 0, len, cp); michael@0: SCTP_BUF_LEN((*endofchain)) += len; michael@0: /* now we need another one */ michael@0: sizeofcpy -= len; michael@0: } michael@0: m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m == NULL) { michael@0: /* We failed */ michael@0: goto error_out; michael@0: } michael@0: SCTP_BUF_NEXT((*endofchain)) = m; michael@0: *endofchain = m; michael@0: cp = mtod((*endofchain), caddr_t); michael@0: m_copydata(clonechain, len, sizeofcpy, cp); michael@0: SCTP_BUF_LEN((*endofchain)) += sizeofcpy; michael@0: } michael@0: return (outchain); michael@0: } else { michael@0: /* copy the old fashion way */ michael@0: appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT); michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = appendchain; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: if (appendchain == NULL) { michael@0: /* error */ michael@0: if (outchain) michael@0: sctp_m_freem(outchain); michael@0: return (NULL); michael@0: } michael@0: if (outchain) { michael@0: /* tack on to the end */ michael@0: if (*endofchain != NULL) { michael@0: SCTP_BUF_NEXT(((*endofchain))) = appendchain; michael@0: } else { michael@0: m = outchain; michael@0: while (m) { michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: SCTP_BUF_NEXT(m) = appendchain; michael@0: break; michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: } michael@0: /* michael@0: * save off the end and update the end-chain michael@0: * postion michael@0: */ michael@0: m = appendchain; michael@0: while (m) { michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: *endofchain = m; michael@0: break; michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: return (outchain); michael@0: } else { michael@0: /* save off the end and update the end-chain postion */ michael@0: m = appendchain; michael@0: while (m) { michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: *endofchain = m; michael@0: break; michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: return (appendchain); michael@0: } michael@0: } michael@0: michael@0: static int michael@0: sctp_med_chunk_output(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: int *num_out, michael@0: int *reason_code, michael@0: int control_only, int from_where, michael@0: struct timeval *now, int *now_filled, int frag_point, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ); michael@0: michael@0: static void michael@0: sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, michael@0: uint32_t val SCTP_UNUSED) michael@0: { michael@0: struct sctp_copy_all *ca; michael@0: struct mbuf *m; michael@0: int ret = 0; michael@0: int added_control = 0; michael@0: int un_sent, do_chunk_output = 1; michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *net; michael@0: michael@0: ca = (struct sctp_copy_all *)ptr; michael@0: if (ca->m == NULL) { michael@0: return; michael@0: } michael@0: if (ca->inp != inp) { michael@0: /* TSNH */ michael@0: return; michael@0: } michael@0: if (ca->sndlen > 0) { michael@0: m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT); michael@0: if (m == NULL) { michael@0: /* can't copy so we are done */ michael@0: ca->cnt_failed++; michael@0: return; michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: } else { michael@0: m = NULL; michael@0: } michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (stcb->asoc.alternate) { michael@0: net = stcb->asoc.alternate; michael@0: } else { michael@0: net = stcb->asoc.primary_destination; michael@0: } michael@0: if (ca->sndrcv.sinfo_flags & SCTP_ABORT) { michael@0: /* Abort this assoc with m as the user defined reason */ michael@0: if (m != NULL) { michael@0: SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT); michael@0: } else { michael@0: m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr); michael@0: } michael@0: if (m != NULL) { michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: ph = mtod(m, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr) + ca->sndlen); michael@0: } michael@0: /* We add one here to keep the assoc from michael@0: * dis-appearing on us. michael@0: */ michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: sctp_abort_an_association(inp, stcb, m, SCTP_SO_NOT_LOCKED); michael@0: /* sctp_abort_an_association calls sctp_free_asoc() michael@0: * free association will NOT free it since we michael@0: * incremented the refcnt .. we do this to prevent michael@0: * it being freed and things getting tricky since michael@0: * we could end up (from free_asoc) calling inpcb_free michael@0: * which would get a recursive lock call to the michael@0: * iterator lock.. But as a consequence of that the michael@0: * stcb will return to us un-locked.. since free_asoc michael@0: * returns with either no TCB or the TCB unlocked, we michael@0: * must relock.. to unlock in the iterator timer :-0 michael@0: */ michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: goto no_chunk_output; michael@0: } else { michael@0: if (m) { michael@0: ret = sctp_msg_append(stcb, net, m, michael@0: &ca->sndrcv, 1); michael@0: } michael@0: asoc = &stcb->asoc; michael@0: if (ca->sndrcv.sinfo_flags & SCTP_EOF) { michael@0: /* shutdown this assoc */ michael@0: int cnt; michael@0: cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); michael@0: michael@0: if (TAILQ_EMPTY(&asoc->send_queue) && michael@0: TAILQ_EMPTY(&asoc->sent_queue) && michael@0: (cnt == 0)) { michael@0: if (asoc->locked_on_sending) { michael@0: goto abort_anyway; michael@0: } michael@0: /* there is nothing queued to send, so I'm done... */ michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: /* only send SHUTDOWN the first time through */ michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); michael@0: SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); michael@0: sctp_stop_timers_for_shutdown(stcb); michael@0: sctp_send_shutdown(stcb, net); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, michael@0: net); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, michael@0: asoc->primary_destination); michael@0: added_control = 1; michael@0: do_chunk_output = 0; michael@0: } michael@0: } else { michael@0: /* michael@0: * we still got (or just got) data to send, so set michael@0: * SHUTDOWN_PENDING michael@0: */ michael@0: /* michael@0: * XXX sockets draft says that SCTP_EOF should be michael@0: * sent with no data. currently, we will allow user michael@0: * data to be sent first and move to michael@0: * SHUTDOWN-PENDING michael@0: */ michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: if (asoc->locked_on_sending) { michael@0: /* Locked to send out the data */ michael@0: struct sctp_stream_queue_pending *sp; michael@0: sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); michael@0: if (sp) { michael@0: if ((sp->length == 0) && (sp->msg_is_complete == 0)) michael@0: asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; michael@0: } michael@0: } michael@0: asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; michael@0: if (TAILQ_EMPTY(&asoc->send_queue) && michael@0: TAILQ_EMPTY(&asoc->sent_queue) && michael@0: (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { michael@0: abort_anyway: michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: sctp_abort_an_association(stcb->sctp_ep, stcb, michael@0: NULL, SCTP_SO_NOT_LOCKED); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: goto no_chunk_output; michael@0: } michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, michael@0: asoc->primary_destination); michael@0: } michael@0: } michael@0: michael@0: } michael@0: } michael@0: un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + michael@0: (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); michael@0: michael@0: if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && michael@0: (stcb->asoc.total_flight > 0) && michael@0: (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { michael@0: do_chunk_output = 0; michael@0: } michael@0: if (do_chunk_output) michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED); michael@0: else if (added_control) { michael@0: int num_out = 0, reason = 0, now_filled = 0; michael@0: struct timeval now; michael@0: int frag_point; michael@0: frag_point = sctp_get_frag_point(stcb, &stcb->asoc); michael@0: (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, michael@0: &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: no_chunk_output: michael@0: if (ret) { michael@0: ca->cnt_failed++; michael@0: } else { michael@0: ca->cnt_sent++; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED) michael@0: { michael@0: struct sctp_copy_all *ca; michael@0: michael@0: ca = (struct sctp_copy_all *)ptr; michael@0: /* michael@0: * Do a notify here? Kacheong suggests that the notify be done at michael@0: * the send time.. so you would push up a notification if any send michael@0: * failed. Don't know if this is feasable since the only failures we michael@0: * have is "memory" related and if you cannot get an mbuf to send michael@0: * the data you surely can't get an mbuf to send up to notify the michael@0: * user you can't send the data :-> michael@0: */ michael@0: michael@0: /* now free everything */ michael@0: sctp_m_freem(ca->m); michael@0: SCTP_FREE(ca, SCTP_M_COPYAL); michael@0: } michael@0: michael@0: michael@0: #define MC_ALIGN(m, len) do { \ michael@0: SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \ michael@0: } while (0) michael@0: michael@0: michael@0: michael@0: static struct mbuf * michael@0: sctp_copy_out_all(struct uio *uio, int len) michael@0: { michael@0: struct mbuf *ret, *at; michael@0: int left, willcpy, cancpy, error; michael@0: michael@0: ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA); michael@0: if (ret == NULL) { michael@0: /* TSNH */ michael@0: return (NULL); michael@0: } michael@0: left = len; michael@0: SCTP_BUF_LEN(ret) = 0; michael@0: /* save space for the data chunk header */ michael@0: cancpy = M_TRAILINGSPACE(ret); michael@0: willcpy = min(cancpy, left); michael@0: at = ret; michael@0: while (left > 0) { michael@0: /* Align data to the end */ michael@0: error = uiomove(mtod(at, caddr_t), willcpy, uio); michael@0: if (error) { michael@0: err_out_now: michael@0: sctp_m_freem(at); michael@0: return (NULL); michael@0: } michael@0: SCTP_BUF_LEN(at) = willcpy; michael@0: SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0; michael@0: left -= willcpy; michael@0: if (left > 0) { michael@0: SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 1, MT_DATA); michael@0: if (SCTP_BUF_NEXT(at) == NULL) { michael@0: goto err_out_now; michael@0: } michael@0: at = SCTP_BUF_NEXT(at); michael@0: SCTP_BUF_LEN(at) = 0; michael@0: cancpy = M_TRAILINGSPACE(at); michael@0: willcpy = min(cancpy, left); michael@0: } michael@0: } michael@0: return (ret); michael@0: } michael@0: michael@0: static int michael@0: sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, michael@0: struct sctp_sndrcvinfo *srcv) michael@0: { michael@0: int ret; michael@0: struct sctp_copy_all *ca; michael@0: michael@0: SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all), michael@0: SCTP_M_COPYAL); michael@0: if (ca == NULL) { michael@0: sctp_m_freem(m); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: memset(ca, 0, sizeof(struct sctp_copy_all)); michael@0: michael@0: ca->inp = inp; michael@0: if (srcv) { michael@0: memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo)); michael@0: } michael@0: /* michael@0: * take off the sendall flag, it would be bad if we failed to do michael@0: * this :-0 michael@0: */ michael@0: ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL; michael@0: /* get length and mbuf chain */ michael@0: if (uio) { michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: ca->sndlen = uio->uio_resid; michael@0: #else michael@0: ca->sndlen = uio_resid(uio); michael@0: #endif michael@0: #else michael@0: ca->sndlen = uio->uio_resid; michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 0); michael@0: #endif michael@0: ca->m = sctp_copy_out_all(uio, ca->sndlen); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 0); michael@0: #endif michael@0: if (ca->m == NULL) { michael@0: SCTP_FREE(ca, SCTP_M_COPYAL); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: } else { michael@0: /* Gather the length of the send */ michael@0: struct mbuf *mat; michael@0: michael@0: ca->sndlen = 0; michael@0: for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: ca->sndlen += SCTP_BUF_LEN(mat); michael@0: } michael@0: } michael@0: ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL, michael@0: SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, michael@0: SCTP_ASOC_ANY_STATE, michael@0: (void *)ca, 0, michael@0: sctp_sendall_completes, inp, 1); michael@0: if (ret) { michael@0: SCTP_PRINTF("Failed to initiate iterator for sendall\n"); michael@0: SCTP_FREE(ca, SCTP_M_COPYAL); michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc) michael@0: { michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { michael@0: if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { michael@0: TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: asoc->ctrl_queue_cnt--; michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_toss_old_asconf(struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: struct sctp_asconf_chunk *acp; michael@0: michael@0: asoc = &stcb->asoc; michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { michael@0: /* find SCTP_ASCONF chunk in queue */ michael@0: if (chk->rec.chunk_id.id == SCTP_ASCONF) { michael@0: if (chk->data) { michael@0: acp = mtod(chk->data, struct sctp_asconf_chunk *); michael@0: if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) { michael@0: /* Not Acked yet */ michael@0: break; michael@0: } michael@0: } michael@0: TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: asoc->ctrl_queue_cnt--; michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_clean_up_datalist(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: struct sctp_tmit_chunk **data_list, michael@0: int bundle_at, michael@0: struct sctp_nets *net) michael@0: { michael@0: int i; michael@0: struct sctp_tmit_chunk *tp1; michael@0: michael@0: for (i = 0; i < bundle_at; i++) { michael@0: /* off of the send queue */ michael@0: TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next); michael@0: asoc->send_queue_cnt--; michael@0: if (i > 0) { michael@0: /* michael@0: * Any chunk NOT 0 you zap the time chunk 0 gets michael@0: * zapped or set based on if a RTO measurment is michael@0: * needed. michael@0: */ michael@0: data_list[i]->do_rtt = 0; michael@0: } michael@0: /* record time */ michael@0: data_list[i]->sent_rcv_time = net->last_sent_time; michael@0: data_list[i]->rec.data.cwnd_at_send = net->cwnd; michael@0: data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq; michael@0: if (data_list[i]->whoTo == NULL) { michael@0: data_list[i]->whoTo = net; michael@0: atomic_add_int(&net->ref_count, 1); michael@0: } michael@0: /* on to the sent queue */ michael@0: tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead); michael@0: if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) { michael@0: struct sctp_tmit_chunk *tpp; michael@0: michael@0: /* need to move back */ michael@0: back_up_more: michael@0: tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next); michael@0: if (tpp == NULL) { michael@0: TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next); michael@0: goto all_done; michael@0: } michael@0: tp1 = tpp; michael@0: if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) { michael@0: goto back_up_more; michael@0: } michael@0: TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next); michael@0: } else { michael@0: TAILQ_INSERT_TAIL(&asoc->sent_queue, michael@0: data_list[i], michael@0: sctp_next); michael@0: } michael@0: all_done: michael@0: /* This does not lower until the cum-ack passes it */ michael@0: asoc->sent_queue_cnt++; michael@0: if ((asoc->peers_rwnd <= 0) && michael@0: (asoc->total_flight == 0) && michael@0: (bundle_at == 1)) { michael@0: /* Mark the chunk as being a window probe */ michael@0: SCTP_STAT_INCR(sctps_windowprobed); michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xC2, 3); michael@0: #endif michael@0: data_list[i]->sent = SCTP_DATAGRAM_SENT; michael@0: data_list[i]->snd_count = 1; michael@0: data_list[i]->rec.data.chunk_was_revoked = 0; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { michael@0: sctp_misc_ints(SCTP_FLIGHT_LOG_UP, michael@0: data_list[i]->whoTo->flight_size, michael@0: data_list[i]->book_size, michael@0: (uintptr_t)data_list[i]->whoTo, michael@0: data_list[i]->rec.data.TSN_seq); michael@0: } michael@0: sctp_flight_size_increase(data_list[i]); michael@0: sctp_total_flight_increase(stcb, data_list[i]); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { michael@0: sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, michael@0: asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); michael@0: } michael@0: asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, michael@0: (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); michael@0: if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { michael@0: /* SWS sender side engages */ michael@0: asoc->peers_rwnd = 0; michael@0: } michael@0: } michael@0: if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) { michael@0: (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted)(stcb, net); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { michael@0: if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ michael@0: (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || michael@0: (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) || michael@0: (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || michael@0: (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || michael@0: (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || michael@0: (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_ECN_CWR) || michael@0: (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { michael@0: /* Stray chunks must be cleaned up */ michael@0: clean_up_anyway: michael@0: TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: asoc->ctrl_queue_cnt--; michael@0: if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) michael@0: asoc->fwd_tsn_cnt--; michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { michael@0: /* special handling, we must look into the param */ michael@0: if (chk != asoc->str_reset) { michael@0: goto clean_up_anyway; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: static int michael@0: sctp_can_we_split_this(struct sctp_tcb *stcb, michael@0: uint32_t length, michael@0: uint32_t goal_mtu, uint32_t frag_point, int eeor_on) michael@0: { michael@0: /* Make a decision on if I should split a michael@0: * msg into multiple parts. This is only asked of michael@0: * incomplete messages. michael@0: */ michael@0: if (eeor_on) { michael@0: /* If we are doing EEOR we need to always send michael@0: * it if its the entire thing, since it might michael@0: * be all the guy is putting in the hopper. michael@0: */ michael@0: if (goal_mtu >= length) { michael@0: /*- michael@0: * If we have data outstanding, michael@0: * we get another chance when the sack michael@0: * arrives to transmit - wait for more data michael@0: */ michael@0: if (stcb->asoc.total_flight == 0) { michael@0: /* If nothing is in flight, we zero michael@0: * the packet counter. michael@0: */ michael@0: return (length); michael@0: } michael@0: return (0); michael@0: michael@0: } else { michael@0: /* You can fill the rest */ michael@0: return (goal_mtu); michael@0: } michael@0: } michael@0: /*- michael@0: * For those strange folk that make the send buffer michael@0: * smaller than our fragmentation point, we can't michael@0: * get a full msg in so we have to allow splitting. michael@0: */ michael@0: if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) { michael@0: return (length); michael@0: } michael@0: michael@0: if ((length <= goal_mtu) || michael@0: ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) { michael@0: /* Sub-optimial residual don't split in non-eeor mode. */ michael@0: return (0); michael@0: } michael@0: /* If we reach here length is larger michael@0: * than the goal_mtu. Do we wish to split michael@0: * it for the sake of packet putting together? michael@0: */ michael@0: if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) { michael@0: /* Its ok to split it */ michael@0: return (min(goal_mtu, frag_point)); michael@0: } michael@0: /* Nope, can't split */ michael@0: return (0); michael@0: michael@0: } michael@0: michael@0: static uint32_t michael@0: sctp_move_to_outqueue(struct sctp_tcb *stcb, michael@0: struct sctp_stream_out *strq, michael@0: uint32_t goal_mtu, michael@0: uint32_t frag_point, michael@0: int *locked, michael@0: int *giveup, michael@0: int eeor_mode, michael@0: int *bail, michael@0: int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /* Move from the stream to the send_queue keeping track of the total */ michael@0: struct sctp_association *asoc; michael@0: struct sctp_stream_queue_pending *sp; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_data_chunk *dchkh; michael@0: uint32_t to_move, length; michael@0: uint8_t rcv_flags = 0; michael@0: uint8_t some_taken; michael@0: uint8_t send_lock_up = 0; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: asoc = &stcb->asoc; michael@0: one_more_time: michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: sp = TAILQ_FIRST(&strq->outqueue); michael@0: if (sp == NULL) { michael@0: *locked = 0; michael@0: if (send_lock_up == 0) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: sp = TAILQ_FIRST(&strq->outqueue); michael@0: if (sp) { michael@0: goto one_more_time; michael@0: } michael@0: if (strq->last_msg_incomplete) { michael@0: SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n", michael@0: strq->stream_no, michael@0: strq->last_msg_incomplete); michael@0: strq->last_msg_incomplete = 0; michael@0: } michael@0: to_move = 0; michael@0: if (send_lock_up) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: send_lock_up = 0; michael@0: } michael@0: goto out_of; michael@0: } michael@0: if ((sp->msg_is_complete) && (sp->length == 0)) { michael@0: if (sp->sender_all_done) { michael@0: /* We are doing differed cleanup. Last michael@0: * time through when we took all the data michael@0: * the sender_all_done was not set. michael@0: */ michael@0: if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) { michael@0: SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); michael@0: SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", michael@0: sp->sender_all_done, michael@0: sp->length, michael@0: sp->msg_is_complete, michael@0: sp->put_last_out, michael@0: send_lock_up); michael@0: } michael@0: if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: atomic_subtract_int(&asoc->stream_queue_cnt, 1); michael@0: TAILQ_REMOVE(&strq->outqueue, sp, next); michael@0: stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); michael@0: if (sp->net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: } michael@0: sctp_free_a_strmoq(stcb, sp, so_locked); michael@0: /* we can't be locked to it */ michael@0: *locked = 0; michael@0: stcb->asoc.locked_on_sending = NULL; michael@0: if (send_lock_up) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: send_lock_up = 0; michael@0: } michael@0: /* back to get the next msg */ michael@0: goto one_more_time; michael@0: } else { michael@0: /* sender just finished this but michael@0: * still holds a reference michael@0: */ michael@0: *locked = 1; michael@0: *giveup = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: } else { michael@0: /* is there some to get */ michael@0: if (sp->length == 0) { michael@0: /* no */ michael@0: *locked = 1; michael@0: *giveup = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } else if (sp->discard_rest) { michael@0: if (send_lock_up == 0) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: /* Whack down the size */ michael@0: atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length); michael@0: if ((stcb->sctp_socket != NULL) && \ michael@0: ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { michael@0: atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length); michael@0: } michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: sp->tail_mbuf = NULL; michael@0: } michael@0: sp->length = 0; michael@0: sp->some_taken = 1; michael@0: *locked = 1; michael@0: *giveup = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: } michael@0: some_taken = sp->some_taken; michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: sp->msg_is_complete = 1; michael@0: } michael@0: re_look: michael@0: length = sp->length; michael@0: if (sp->msg_is_complete) { michael@0: /* The message is complete */ michael@0: to_move = min(length, frag_point); michael@0: if (to_move == length) { michael@0: /* All of it fits in the MTU */ michael@0: if (sp->some_taken) { michael@0: rcv_flags |= SCTP_DATA_LAST_FRAG; michael@0: sp->put_last_out = 1; michael@0: } else { michael@0: rcv_flags |= SCTP_DATA_NOT_FRAG; michael@0: sp->put_last_out = 1; michael@0: } michael@0: } else { michael@0: /* Not all of it fits, we fragment */ michael@0: if (sp->some_taken == 0) { michael@0: rcv_flags |= SCTP_DATA_FIRST_FRAG; michael@0: } michael@0: sp->some_taken = 1; michael@0: } michael@0: } else { michael@0: to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode); michael@0: if (to_move) { michael@0: /*- michael@0: * We use a snapshot of length in case it michael@0: * is expanding during the compare. michael@0: */ michael@0: uint32_t llen; michael@0: michael@0: llen = length; michael@0: if (to_move >= llen) { michael@0: to_move = llen; michael@0: if (send_lock_up == 0) { michael@0: /*- michael@0: * We are taking all of an incomplete msg michael@0: * thus we need a send lock. michael@0: */ michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: if (sp->msg_is_complete) { michael@0: /* the sender finished the msg */ michael@0: goto re_look; michael@0: } michael@0: } michael@0: } michael@0: if (sp->some_taken == 0) { michael@0: rcv_flags |= SCTP_DATA_FIRST_FRAG; michael@0: sp->some_taken = 1; michael@0: } michael@0: } else { michael@0: /* Nothing to take. */ michael@0: if (sp->some_taken) { michael@0: *locked = 1; michael@0: } michael@0: *giveup = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: } michael@0: michael@0: /* If we reach here, we can copy out a chunk */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* No chunk memory */ michael@0: *giveup = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: /* Setup for unordered if needed by looking michael@0: * at the user sent info flags. michael@0: */ michael@0: if (sp->sinfo_flags & SCTP_UNORDERED) { michael@0: rcv_flags |= SCTP_DATA_UNORDERED; michael@0: } michael@0: if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) || michael@0: ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) { michael@0: rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; michael@0: } michael@0: /* clear out the chunk before setting up */ michael@0: memset(chk, 0, sizeof(*chk)); michael@0: chk->rec.data.rcv_flags = rcv_flags; michael@0: michael@0: if (to_move >= length) { michael@0: /* we think we can steal the whole thing */ michael@0: if ((sp->sender_all_done == 0) && (send_lock_up == 0)) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: if (to_move < sp->length) { michael@0: /* bail, it changed */ michael@0: goto dont_do_it; michael@0: } michael@0: chk->data = sp->data; michael@0: chk->last_mbuf = sp->tail_mbuf; michael@0: /* register the stealing */ michael@0: sp->data = sp->tail_mbuf = NULL; michael@0: } else { michael@0: struct mbuf *m; michael@0: dont_do_it: michael@0: chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT); michael@0: chk->last_mbuf = NULL; michael@0: if (chk->data == NULL) { michael@0: sp->some_taken = some_taken; michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: *bail = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = chk->data; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: /* Pull off the data */ michael@0: m_adj(sp->data, to_move); michael@0: /* Now lets work our way down and compact it */ michael@0: m = sp->data; michael@0: while (m && (SCTP_BUF_LEN(m) == 0)) { michael@0: sp->data = SCTP_BUF_NEXT(m); michael@0: SCTP_BUF_NEXT(m) = NULL; michael@0: if (sp->tail_mbuf == m) { michael@0: /*- michael@0: * Freeing tail? TSNH since michael@0: * we supposedly were taking less michael@0: * than the sp->length. michael@0: */ michael@0: #ifdef INVARIANTS michael@0: panic("Huh, freing tail? - TSNH"); michael@0: #else michael@0: SCTP_PRINTF("Huh, freeing tail? - TSNH\n"); michael@0: sp->tail_mbuf = sp->data = NULL; michael@0: sp->length = 0; michael@0: #endif michael@0: michael@0: } michael@0: sctp_m_free(m); michael@0: m = sp->data; michael@0: } michael@0: } michael@0: if (SCTP_BUF_IS_EXTENDED(chk->data)) { michael@0: chk->copy_by_ref = 1; michael@0: } else { michael@0: chk->copy_by_ref = 0; michael@0: } michael@0: /* get last_mbuf and counts of mb useage michael@0: * This is ugly but hopefully its only one mbuf. michael@0: */ michael@0: if (chk->last_mbuf == NULL) { michael@0: chk->last_mbuf = chk->data; michael@0: while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) { michael@0: chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf); michael@0: } michael@0: } michael@0: michael@0: if (to_move > length) { michael@0: /*- This should not happen either michael@0: * since we always lower to_move to the size michael@0: * of sp->length if its larger. michael@0: */ michael@0: #ifdef INVARIANTS michael@0: panic("Huh, how can to_move be larger?"); michael@0: #else michael@0: SCTP_PRINTF("Huh, how can to_move be larger?\n"); michael@0: sp->length = 0; michael@0: #endif michael@0: } else { michael@0: atomic_subtract_int(&sp->length, to_move); michael@0: } michael@0: if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) { michael@0: /* Not enough room for a chunk header, get some */ michael@0: struct mbuf *m; michael@0: m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 0, MT_DATA); michael@0: if (m == NULL) { michael@0: /* michael@0: * we're in trouble here. _PREPEND below will free michael@0: * all the data if there is no leading space, so we michael@0: * must put the data back and restore. michael@0: */ michael@0: if (send_lock_up == 0) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: if (chk->data == NULL) { michael@0: /* unsteal the data */ michael@0: sp->data = chk->data; michael@0: sp->tail_mbuf = chk->last_mbuf; michael@0: } else { michael@0: struct mbuf *m_tmp; michael@0: /* reassemble the data */ michael@0: m_tmp = sp->data; michael@0: sp->data = chk->data; michael@0: SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp; michael@0: } michael@0: sp->some_taken = some_taken; michael@0: atomic_add_int(&sp->length, to_move); michael@0: chk->data = NULL; michael@0: *bail = 1; michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: to_move = 0; michael@0: goto out_of; michael@0: } else { michael@0: SCTP_BUF_LEN(m) = 0; michael@0: SCTP_BUF_NEXT(m) = chk->data; michael@0: chk->data = m; michael@0: M_ALIGN(chk->data, 4); michael@0: } michael@0: } michael@0: SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_NOWAIT); michael@0: if (chk->data == NULL) { michael@0: /* HELP, TSNH since we assured it would not above? */ michael@0: #ifdef INVARIANTS michael@0: panic("prepend failes HELP?"); michael@0: #else michael@0: SCTP_PRINTF("prepend fails HELP?\n"); michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: #endif michael@0: *bail = 1; michael@0: to_move = 0; michael@0: goto out_of; michael@0: } michael@0: sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk)); michael@0: chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk)); michael@0: chk->book_size_scale = 0; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->pad_inplace = 0; michael@0: chk->no_fr_allowed = 0; michael@0: chk->rec.data.stream_seq = strq->next_sequence_send; michael@0: if (rcv_flags & SCTP_DATA_LAST_FRAG) { michael@0: strq->next_sequence_send++; michael@0: } michael@0: chk->rec.data.stream_number = sp->stream; michael@0: chk->rec.data.payloadtype = sp->ppid; michael@0: chk->rec.data.context = sp->context; michael@0: chk->rec.data.doing_fast_retransmit = 0; michael@0: michael@0: chk->rec.data.timetodrop = sp->ts; michael@0: chk->flags = sp->act_flags; michael@0: michael@0: if (sp->net) { michael@0: chk->whoTo = sp->net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } else michael@0: chk->whoTo = NULL; michael@0: michael@0: if (sp->holds_key_ref) { michael@0: chk->auth_keyid = sp->auth_keyid; michael@0: sctp_auth_key_acquire(stcb, chk->auth_keyid); michael@0: chk->holds_key_ref = 1; michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) || defined(__Panda__) michael@0: chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1); michael@0: #else michael@0: chk->rec.data.TSN_seq = asoc->sending_seq++; michael@0: #endif michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) { michael@0: sctp_misc_ints(SCTP_STRMOUT_LOG_SEND, michael@0: (uintptr_t)stcb, sp->length, michael@0: (uint32_t)((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq), michael@0: chk->rec.data.TSN_seq); michael@0: } michael@0: dchkh = mtod(chk->data, struct sctp_data_chunk *); michael@0: /* michael@0: * Put the rest of the things in place now. Size was done michael@0: * earlier in previous loop prior to padding. michael@0: */ michael@0: michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) { michael@0: asoc->tsn_out_at = 0; michael@0: asoc->tsn_out_wrapped = 1; michael@0: } michael@0: asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at; michael@0: asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2; michael@0: asoc->tsn_out_at++; michael@0: #endif michael@0: michael@0: dchkh->ch.chunk_type = SCTP_DATA; michael@0: dchkh->ch.chunk_flags = chk->rec.data.rcv_flags; michael@0: dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq); michael@0: dchkh->dp.stream_id = htons(strq->stream_no); michael@0: dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq); michael@0: dchkh->dp.protocol_id = chk->rec.data.payloadtype; michael@0: dchkh->ch.chunk_length = htons(chk->send_size); michael@0: /* Now advance the chk->send_size by the actual pad needed. */ michael@0: if (chk->send_size < SCTP_SIZE32(chk->book_size)) { michael@0: /* need a pad */ michael@0: struct mbuf *lm; michael@0: int pads; michael@0: michael@0: pads = SCTP_SIZE32(chk->book_size) - chk->send_size; michael@0: if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) { michael@0: chk->pad_inplace = 1; michael@0: } michael@0: if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) { michael@0: /* pad added an mbuf */ michael@0: chk->last_mbuf = lm; michael@0: } michael@0: chk->send_size += pads; michael@0: } michael@0: if (PR_SCTP_ENABLED(chk->flags)) { michael@0: asoc->pr_sctp_cnt++; michael@0: } michael@0: if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) { michael@0: /* All done pull and kill the message */ michael@0: atomic_subtract_int(&asoc->stream_queue_cnt, 1); michael@0: if (sp->put_last_out == 0) { michael@0: SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n"); michael@0: SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", michael@0: sp->sender_all_done, michael@0: sp->length, michael@0: sp->msg_is_complete, michael@0: sp->put_last_out, michael@0: send_lock_up); michael@0: } michael@0: if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: send_lock_up = 1; michael@0: } michael@0: TAILQ_REMOVE(&strq->outqueue, sp, next); michael@0: stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); michael@0: if (sp->net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: } michael@0: sctp_free_a_strmoq(stcb, sp, so_locked); michael@0: michael@0: /* we can't be locked to it */ michael@0: *locked = 0; michael@0: stcb->asoc.locked_on_sending = NULL; michael@0: } else { michael@0: /* more to go, we are locked */ michael@0: *locked = 1; michael@0: } michael@0: asoc->chunks_on_out_queue++; michael@0: strq->chunks_on_queues++; michael@0: TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next); michael@0: asoc->send_queue_cnt++; michael@0: out_of: michael@0: if (send_lock_up) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: return (to_move); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_fill_outqueue(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_stream_out *strq; michael@0: int goal_mtu, moved_how_much, total_moved = 0, bail = 0; michael@0: int locked, giveup; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: asoc = &stcb->asoc; michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: goal_mtu = net->mtu - SCTP_MIN_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: goal_mtu = net->mtu - sizeof(struct sctphdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: goal_mtu = net->mtu; michael@0: break; michael@0: } michael@0: /* Need an allowance for the data chunk header too */ michael@0: goal_mtu -= sizeof(struct sctp_data_chunk); michael@0: michael@0: /* must make even word boundary */ michael@0: goal_mtu &= 0xfffffffc; michael@0: if (asoc->locked_on_sending) { michael@0: /* We are stuck on one stream until the message completes. */ michael@0: strq = asoc->locked_on_sending; michael@0: locked = 1; michael@0: } else { michael@0: strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); michael@0: locked = 0; michael@0: } michael@0: while ((goal_mtu > 0) && strq) { michael@0: giveup = 0; michael@0: bail = 0; michael@0: moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked, michael@0: &giveup, eeor_mode, &bail, so_locked); michael@0: if (moved_how_much) michael@0: stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much); michael@0: michael@0: if (locked) { michael@0: asoc->locked_on_sending = strq; michael@0: if ((moved_how_much == 0) || (giveup) || bail) michael@0: /* no more to move for now */ michael@0: break; michael@0: } else { michael@0: asoc->locked_on_sending = NULL; michael@0: if ((giveup) || bail) { michael@0: break; michael@0: } michael@0: strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); michael@0: if (strq == NULL) { michael@0: break; michael@0: } michael@0: } michael@0: total_moved += moved_how_much; michael@0: goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk)); michael@0: goal_mtu &= 0xfffffffc; michael@0: } michael@0: if (bail) michael@0: *quit_now = 1; michael@0: michael@0: stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc); michael@0: michael@0: if (total_moved == 0) { michael@0: if ((stcb->asoc.sctp_cmt_on_off == 0) && michael@0: (net == stcb->asoc.primary_destination)) { michael@0: /* ran dry for primary network net */ michael@0: SCTP_STAT_INCR(sctps_primary_randry); michael@0: } else if (stcb->asoc.sctp_cmt_on_off > 0) { michael@0: /* ran dry with CMT on */ michael@0: SCTP_STAT_INCR(sctps_cmt_randry); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_fix_ecn_echo(struct sctp_association *asoc) michael@0: { michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_stream_queue_pending *sp; michael@0: unsigned int i; michael@0: michael@0: if (net == NULL) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) { michael@0: if (sp->net == net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: } michael@0: } michael@0: TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { michael@0: if (chk->whoTo == net) { michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: chk->whoTo = NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: int michael@0: sctp_med_chunk_output(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: int *num_out, michael@0: int *reason_code, michael@0: int control_only, int from_where, michael@0: struct timeval *now, int *now_filled, int frag_point, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /** michael@0: * Ok this is the generic chunk service queue. we must do the michael@0: * following: - Service the stream queue that is next, moving any michael@0: * message (note I must get a complete message i.e. FIRST/MIDDLE and michael@0: * LAST to the out queue in one pass) and assigning TSN's - Check to michael@0: * see if the cwnd/rwnd allows any output, if so we go ahead and michael@0: * fomulate and send the low level chunks. Making sure to combine michael@0: * any control in the control chunk queue also. michael@0: */ michael@0: struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL; michael@0: struct mbuf *outchain, *endoutchain; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: michael@0: /* temp arrays for unlinking */ michael@0: struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; michael@0: int no_fragmentflg, error; michael@0: unsigned int max_rwnd_per_dest, max_send_per_dest; michael@0: int one_chunk, hbflag, skip_data_for_this_net; michael@0: int asconf, cookie, no_out_cnt; michael@0: int bundle_at, ctl_cnt, no_data_chunks, eeor_mode; michael@0: unsigned int mtu, r_mtu, omtu, mx_mtu, to_out; michael@0: int tsns_sent = 0; michael@0: uint32_t auth_offset = 0; michael@0: struct sctp_auth_chunk *auth = NULL; michael@0: uint16_t auth_keyid; michael@0: int override_ok = 1; michael@0: int skip_fill_up = 0; michael@0: int data_auth_reqd = 0; michael@0: /* JRS 5/14/07 - Add flag for whether a heartbeat is sent to michael@0: the destination. */ michael@0: int quit_now = 0; michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: *num_out = 0; michael@0: auth_keyid = stcb->asoc.authinfo.active_keyid; michael@0: michael@0: if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || michael@0: (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) || michael@0: (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { michael@0: eeor_mode = 1; michael@0: } else { michael@0: eeor_mode = 0; michael@0: } michael@0: ctl_cnt = no_out_cnt = asconf = cookie = 0; michael@0: /* michael@0: * First lets prime the pump. For each destination, if there is room michael@0: * in the flight size, attempt to pull an MTU's worth out of the michael@0: * stream queues into the general send_queue michael@0: */ michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xC2, 2); michael@0: #endif michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: hbflag = 0; michael@0: if ((control_only) || (asoc->stream_reset_outstanding)) michael@0: no_data_chunks = 1; michael@0: else michael@0: no_data_chunks = 0; michael@0: michael@0: /* Nothing to possible to send? */ michael@0: if ((TAILQ_EMPTY(&asoc->control_send_queue) || michael@0: (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) && michael@0: TAILQ_EMPTY(&asoc->asconf_send_queue) && michael@0: TAILQ_EMPTY(&asoc->send_queue) && michael@0: stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { michael@0: nothing_to_send: michael@0: *reason_code = 9; michael@0: return (0); michael@0: } michael@0: if (asoc->peers_rwnd == 0) { michael@0: /* No room in peers rwnd */ michael@0: *reason_code = 1; michael@0: if (asoc->total_flight > 0) { michael@0: /* we are allowed one chunk in flight */ michael@0: no_data_chunks = 1; michael@0: } michael@0: } michael@0: if (stcb->asoc.ecn_echo_cnt_onq) { michael@0: /* Record where a sack goes, if any */ michael@0: if (no_data_chunks && michael@0: (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) { michael@0: /* Nothing but ECNe to send - we don't do that */ michael@0: goto nothing_to_send; michael@0: } michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { michael@0: sack_goes_to = chk->whoTo; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets); michael@0: if (stcb->sctp_socket) michael@0: max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets; michael@0: else michael@0: max_send_per_dest = 0; michael@0: if (no_data_chunks == 0) { michael@0: /* How many non-directed chunks are there? */ michael@0: TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { michael@0: if (chk->whoTo == NULL) { michael@0: /* We already have non-directed michael@0: * chunks on the queue, no need michael@0: * to do a fill-up. michael@0: */ michael@0: skip_fill_up = 1; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: } michael@0: if ((no_data_chunks == 0) && michael@0: (skip_fill_up == 0) && michael@0: (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) { michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: /* michael@0: * This for loop we are in takes in michael@0: * each net, if its's got space in cwnd and michael@0: * has data sent to it (when CMT is off) then it michael@0: * calls sctp_fill_outqueue for the net. This gets michael@0: * data on the send queue for that network. michael@0: * michael@0: * In sctp_fill_outqueue TSN's are assigned and michael@0: * data is copied out of the stream buffers. Note michael@0: * mostly copy by reference (we hope). michael@0: */ michael@0: net->window_probe = 0; michael@0: if ((net != stcb->asoc.alternate) && michael@0: ((net->dest_state & SCTP_ADDR_PF) || michael@0: (!(net->dest_state & SCTP_ADDR_REACHABLE)) || michael@0: (net->dest_state & SCTP_ADDR_UNCONFIRMED))) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, 1, michael@0: SCTP_CWND_LOG_FILL_OUTQ_CALLED); michael@0: } michael@0: continue; michael@0: } michael@0: if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) && michael@0: (net->flight_size == 0)) { michael@0: (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net); michael@0: } michael@0: if (net->flight_size >= net->cwnd) { michael@0: /* skip this network, no room - can't fill */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, 3, michael@0: SCTP_CWND_LOG_FILL_OUTQ_CALLED); michael@0: } michael@0: continue; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED); michael@0: } michael@0: sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked); michael@0: if (quit_now) { michael@0: /* memory alloc failure */ michael@0: no_data_chunks = 1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: /* now service each destination and send out what we can for it */ michael@0: /* Nothing to send? */ michael@0: if (TAILQ_EMPTY(&asoc->control_send_queue) && michael@0: TAILQ_EMPTY(&asoc->asconf_send_queue) && michael@0: TAILQ_EMPTY(&asoc->send_queue)) { michael@0: *reason_code = 8; michael@0: return (0); michael@0: } michael@0: michael@0: if (asoc->sctp_cmt_on_off > 0) { michael@0: /* get the last start point */ michael@0: start_at = asoc->last_net_cmt_send_started; michael@0: if (start_at == NULL) { michael@0: /* null so to beginning */ michael@0: start_at = TAILQ_FIRST(&asoc->nets); michael@0: } else { michael@0: start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next); michael@0: if (start_at == NULL) { michael@0: start_at = TAILQ_FIRST(&asoc->nets); michael@0: } michael@0: } michael@0: asoc->last_net_cmt_send_started = start_at; michael@0: } else { michael@0: start_at = TAILQ_FIRST(&asoc->nets); michael@0: } michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if (chk->whoTo == NULL) { michael@0: if (asoc->alternate) { michael@0: chk->whoTo = asoc->alternate; michael@0: } else { michael@0: chk->whoTo = asoc->primary_destination; michael@0: } michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } michael@0: } michael@0: old_start_at = NULL; michael@0: again_one_more_time: michael@0: for (net = start_at ; net != NULL; net = TAILQ_NEXT(net, sctp_next)) { michael@0: /* how much can we send? */ michael@0: /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */ michael@0: if (old_start_at && (old_start_at == net)) { michael@0: /* through list ocmpletely. */ michael@0: break; michael@0: } michael@0: tsns_sent = 0xa; michael@0: if (TAILQ_EMPTY(&asoc->control_send_queue) && michael@0: TAILQ_EMPTY(&asoc->asconf_send_queue) && michael@0: (net->flight_size >= net->cwnd)) { michael@0: /* Nothing on control or asconf and flight is full, we can skip michael@0: * even in the CMT case. michael@0: */ michael@0: continue; michael@0: } michael@0: bundle_at = 0; michael@0: endoutchain = outchain = NULL; michael@0: no_fragmentflg = 1; michael@0: one_chunk = 0; michael@0: if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { michael@0: skip_data_for_this_net = 1; michael@0: } else { michael@0: skip_data_for_this_net = 0; michael@0: } michael@0: #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) || defined(__APPLE__)) michael@0: if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) { michael@0: /* michael@0: * if we have a route and an ifp check to see if we michael@0: * have room to send to this guy michael@0: */ michael@0: struct ifnet *ifp; michael@0: michael@0: ifp = net->ro.ro_rt->rt_ifp; michael@0: if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) { michael@0: SCTP_STAT_INCR(sctps_ifnomemqueued); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { michael@0: sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED); michael@0: } michael@0: continue; michael@0: } michael@0: } michael@0: #endif michael@0: switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: mtu = net->mtu - sizeof(struct sctphdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: mtu = net->mtu; michael@0: break; michael@0: } michael@0: mx_mtu = mtu; michael@0: to_out = 0; michael@0: if (mtu > asoc->peers_rwnd) { michael@0: if (asoc->total_flight > 0) { michael@0: /* We have a packet in flight somewhere */ michael@0: r_mtu = asoc->peers_rwnd; michael@0: } else { michael@0: /* We are always allowed to send one MTU out */ michael@0: one_chunk = 1; michael@0: r_mtu = mtu; michael@0: } michael@0: } else { michael@0: r_mtu = mtu; michael@0: } michael@0: /************************/ michael@0: /* ASCONF transmission */ michael@0: /************************/ michael@0: /* Now first lets go through the asconf queue */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { michael@0: if (chk->rec.chunk_id.id != SCTP_ASCONF) { michael@0: continue; michael@0: } michael@0: if (chk->whoTo == NULL) { michael@0: if (asoc->alternate == NULL) { michael@0: if (asoc->primary_destination != net) { michael@0: break; michael@0: } michael@0: } else { michael@0: if (asoc->alternate != net) { michael@0: break; michael@0: } michael@0: } michael@0: } else { michael@0: if (chk->whoTo != net) { michael@0: break; michael@0: } michael@0: } michael@0: if (chk->data == NULL) { michael@0: break; michael@0: } michael@0: if (chk->sent != SCTP_DATAGRAM_UNSENT && michael@0: chk->sent != SCTP_DATAGRAM_RESEND) { michael@0: break; michael@0: } michael@0: /* michael@0: * if no AUTH is yet included and this chunk michael@0: * requires it, make sure to account for it. We michael@0: * don't apply the size until the AUTH chunk is michael@0: * actually added below in case there is no room for michael@0: * this chunk. NOTE: we overload the use of "omtu" michael@0: * here michael@0: */ michael@0: if ((auth == NULL) && michael@0: sctp_auth_is_required_chunk(chk->rec.chunk_id.id, michael@0: stcb->asoc.peer_auth_chunks)) { michael@0: omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: } else michael@0: omtu = 0; michael@0: /* Here we do NOT factor the r_mtu */ michael@0: if ((chk->send_size < (int)(mtu - omtu)) || michael@0: (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { michael@0: /* michael@0: * We probably should glom the mbuf chain michael@0: * from the chk->data for control but the michael@0: * problem is it becomes yet one more level michael@0: * of tracking to do if for some reason michael@0: * output fails. Then I have got to michael@0: * reconstruct the merged control chain.. el michael@0: * yucko.. for now we take the easy way and michael@0: * do the copy michael@0: */ michael@0: /* michael@0: * Add an AUTH chunk, if chunk requires it michael@0: * save the offset into the chain for AUTH michael@0: */ michael@0: if ((auth == NULL) && michael@0: (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, michael@0: stcb->asoc.peer_auth_chunks))) { michael@0: outchain = sctp_add_auth_chunk(outchain, michael@0: &endoutchain, michael@0: &auth, michael@0: &auth_offset, michael@0: stcb, michael@0: chk->rec.chunk_id.id); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } michael@0: outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, michael@0: (int)chk->rec.chunk_id.can_take_data, michael@0: chk->send_size, chk->copy_by_ref); michael@0: if (outchain == NULL) { michael@0: *reason_code = 8; michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: /* update our MTU size */ michael@0: if (mtu > (chk->send_size + omtu)) michael@0: mtu -= (chk->send_size + omtu); michael@0: else michael@0: mtu = 0; michael@0: to_out += (chk->send_size + omtu); michael@0: /* Do clear IP_DF ? */ michael@0: if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { michael@0: no_fragmentflg = 0; michael@0: } michael@0: if (chk->rec.chunk_id.can_take_data) michael@0: chk->data = NULL; michael@0: /* michael@0: * set hb flag since we can michael@0: * use these for RTO michael@0: */ michael@0: hbflag = 1; michael@0: asconf = 1; michael@0: /* michael@0: * should sysctl this: don't michael@0: * bundle data with ASCONF michael@0: * since it requires AUTH michael@0: */ michael@0: no_data_chunks = 1; michael@0: chk->sent = SCTP_DATAGRAM_SENT; michael@0: if (chk->whoTo == NULL) { michael@0: chk->whoTo = net; michael@0: atomic_add_int(&net->ref_count, 1); michael@0: } michael@0: chk->snd_count++; michael@0: if (mtu == 0) { michael@0: /* michael@0: * Ok we are out of room but we can michael@0: * output without effecting the michael@0: * flight size since this little guy michael@0: * is a control only packet. michael@0: */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); michael@0: /* michael@0: * do NOT clear the asconf michael@0: * flag as it is used to do michael@0: * appropriate source address michael@0: * selection. michael@0: */ michael@0: if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: outchain, auth_offset, auth, michael@0: stcb->asoc.authinfo.active_keyid, michael@0: no_fragmentflg, 0, asconf, michael@0: inp->sctp_lport, stcb->rport, michael@0: htonl(stcb->asoc.peer_vtag), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked))) { michael@0: if (error == ENOBUFS) { michael@0: asoc->ifp_had_enobuf = 1; michael@0: SCTP_STAT_INCR(sctps_lowlevelerr); michael@0: } michael@0: if (from_where == 0) { michael@0: SCTP_STAT_INCR(sctps_lowlevelerrusr); michael@0: } michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: hbflag = 0; michael@0: /* error, could not output */ michael@0: if (error == EHOSTUNREACH) { michael@0: /* michael@0: * Destination went michael@0: * unreachable michael@0: * during this send michael@0: */ michael@0: sctp_move_chunks_from_net(stcb, net); michael@0: } michael@0: *reason_code = 7; michael@0: continue; michael@0: } else michael@0: asoc->ifp_had_enobuf = 0; michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: hbflag = 0; michael@0: /* michael@0: * increase the number we sent, if a michael@0: * cookie is sent we don't tell them michael@0: * any was sent out. michael@0: */ michael@0: outchain = endoutchain = NULL; michael@0: auth = NULL; michael@0: auth_offset = 0; michael@0: if (!no_out_cnt) michael@0: *num_out += ctl_cnt; michael@0: /* recalc a clean slate and setup */ michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: mtu = net->mtu - SCTP_MIN_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: mtu = net->mtu - sizeof(struct sctphdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: mtu = net->mtu; michael@0: break; michael@0: } michael@0: to_out = 0; michael@0: no_fragmentflg = 1; michael@0: } michael@0: } michael@0: } michael@0: /************************/ michael@0: /* Control transmission */ michael@0: /************************/ michael@0: /* Now first lets go through the control queue */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { michael@0: if ((sack_goes_to) && michael@0: (chk->rec.chunk_id.id == SCTP_ECN_ECHO) && michael@0: (chk->whoTo != sack_goes_to)) { michael@0: /* michael@0: * if we have a sack in queue, and we are looking at an michael@0: * ecn echo that is NOT queued to where the sack is going.. michael@0: */ michael@0: if (chk->whoTo == net) { michael@0: /* Don't transmit it to where its going (current net) */ michael@0: continue; michael@0: } else if (sack_goes_to == net) { michael@0: /* But do transmit it to this address */ michael@0: goto skip_net_check; michael@0: } michael@0: } michael@0: if (chk->whoTo == NULL) { michael@0: if (asoc->alternate == NULL) { michael@0: if (asoc->primary_destination != net) { michael@0: continue; michael@0: } michael@0: } else { michael@0: if (asoc->alternate != net) { michael@0: continue; michael@0: } michael@0: } michael@0: } else { michael@0: if (chk->whoTo != net) { michael@0: continue; michael@0: } michael@0: } michael@0: skip_net_check: michael@0: if (chk->data == NULL) { michael@0: continue; michael@0: } michael@0: if (chk->sent != SCTP_DATAGRAM_UNSENT) { michael@0: /* michael@0: * It must be unsent. Cookies and ASCONF's michael@0: * hang around but there timers will force michael@0: * when marked for resend. michael@0: */ michael@0: continue; michael@0: } michael@0: /* michael@0: * if no AUTH is yet included and this chunk michael@0: * requires it, make sure to account for it. We michael@0: * don't apply the size until the AUTH chunk is michael@0: * actually added below in case there is no room for michael@0: * this chunk. NOTE: we overload the use of "omtu" michael@0: * here michael@0: */ michael@0: if ((auth == NULL) && michael@0: sctp_auth_is_required_chunk(chk->rec.chunk_id.id, michael@0: stcb->asoc.peer_auth_chunks)) { michael@0: omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: } else michael@0: omtu = 0; michael@0: /* Here we do NOT factor the r_mtu */ michael@0: if ((chk->send_size <= (int)(mtu - omtu)) || michael@0: (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { michael@0: /* michael@0: * We probably should glom the mbuf chain michael@0: * from the chk->data for control but the michael@0: * problem is it becomes yet one more level michael@0: * of tracking to do if for some reason michael@0: * output fails. Then I have got to michael@0: * reconstruct the merged control chain.. el michael@0: * yucko.. for now we take the easy way and michael@0: * do the copy michael@0: */ michael@0: /* michael@0: * Add an AUTH chunk, if chunk requires it michael@0: * save the offset into the chain for AUTH michael@0: */ michael@0: if ((auth == NULL) && michael@0: (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, michael@0: stcb->asoc.peer_auth_chunks))) { michael@0: outchain = sctp_add_auth_chunk(outchain, michael@0: &endoutchain, michael@0: &auth, michael@0: &auth_offset, michael@0: stcb, michael@0: chk->rec.chunk_id.id); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } michael@0: outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, michael@0: (int)chk->rec.chunk_id.can_take_data, michael@0: chk->send_size, chk->copy_by_ref); michael@0: if (outchain == NULL) { michael@0: *reason_code = 8; michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: /* update our MTU size */ michael@0: if (mtu > (chk->send_size + omtu)) michael@0: mtu -= (chk->send_size + omtu); michael@0: else michael@0: mtu = 0; michael@0: to_out += (chk->send_size + omtu); michael@0: /* Do clear IP_DF ? */ michael@0: if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { michael@0: no_fragmentflg = 0; michael@0: } michael@0: if (chk->rec.chunk_id.can_take_data) michael@0: chk->data = NULL; michael@0: /* Mark things to be removed, if needed */ michael@0: if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ michael@0: (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || michael@0: (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || michael@0: (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || michael@0: (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_ECN_CWR) || michael@0: (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || michael@0: (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { michael@0: if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) { michael@0: hbflag = 1; michael@0: } michael@0: /* remove these chunks at the end */ michael@0: if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || michael@0: (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { michael@0: /* turn off the timer */ michael@0: if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_RECV, michael@0: inp, stcb, net, SCTP_FROM_SCTP_OUTPUT+SCTP_LOC_1); michael@0: } michael@0: } michael@0: ctl_cnt++; michael@0: } else { michael@0: /* michael@0: * Other chunks, since they have michael@0: * timers running (i.e. COOKIE) michael@0: * we just "trust" that it michael@0: * gets sent or retransmitted. michael@0: */ michael@0: ctl_cnt++; michael@0: if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { michael@0: cookie = 1; michael@0: no_out_cnt = 1; michael@0: } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { michael@0: /* michael@0: * Increment ecne send count here michael@0: * this means we may be over-zealous in michael@0: * our counting if the send fails, but its michael@0: * the best place to do it (we used to do michael@0: * it in the queue of the chunk, but that did michael@0: * not tell how many times it was sent. michael@0: */ michael@0: SCTP_STAT_INCR(sctps_sendecne); michael@0: } michael@0: chk->sent = SCTP_DATAGRAM_SENT; michael@0: if (chk->whoTo == NULL) { michael@0: chk->whoTo = net; michael@0: atomic_add_int(&net->ref_count, 1); michael@0: } michael@0: chk->snd_count++; michael@0: } michael@0: if (mtu == 0) { michael@0: /* michael@0: * Ok we are out of room but we can michael@0: * output without effecting the michael@0: * flight size since this little guy michael@0: * is a control only packet. michael@0: */ michael@0: if (asconf) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); michael@0: /* michael@0: * do NOT clear the asconf michael@0: * flag as it is used to do michael@0: * appropriate source address michael@0: * selection. michael@0: */ michael@0: } michael@0: if (cookie) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); michael@0: cookie = 0; michael@0: } michael@0: if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: outchain, michael@0: auth_offset, auth, michael@0: stcb->asoc.authinfo.active_keyid, michael@0: no_fragmentflg, 0, asconf, michael@0: inp->sctp_lport, stcb->rport, michael@0: htonl(stcb->asoc.peer_vtag), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked))) { michael@0: if (error == ENOBUFS) { michael@0: asoc->ifp_had_enobuf = 1; michael@0: SCTP_STAT_INCR(sctps_lowlevelerr); michael@0: } michael@0: if (from_where == 0) { michael@0: SCTP_STAT_INCR(sctps_lowlevelerrusr); michael@0: } michael@0: /* error, could not output */ michael@0: if (hbflag) { michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: hbflag = 0; michael@0: } michael@0: if (error == EHOSTUNREACH) { michael@0: /* michael@0: * Destination went michael@0: * unreachable michael@0: * during this send michael@0: */ michael@0: sctp_move_chunks_from_net(stcb, net); michael@0: } michael@0: *reason_code = 7; michael@0: continue; michael@0: } else michael@0: asoc->ifp_had_enobuf = 0; michael@0: /* Only HB or ASCONF advances time */ michael@0: if (hbflag) { michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: hbflag = 0; michael@0: } michael@0: /* michael@0: * increase the number we sent, if a michael@0: * cookie is sent we don't tell them michael@0: * any was sent out. michael@0: */ michael@0: outchain = endoutchain = NULL; michael@0: auth = NULL; michael@0: auth_offset = 0; michael@0: if (!no_out_cnt) michael@0: *num_out += ctl_cnt; michael@0: /* recalc a clean slate and setup */ michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: mtu = net->mtu - SCTP_MIN_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: mtu = net->mtu - sizeof(struct sctphdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: mtu = net->mtu; michael@0: break; michael@0: } michael@0: to_out = 0; michael@0: no_fragmentflg = 1; michael@0: } michael@0: } michael@0: } michael@0: /* JRI: if dest is in PF state, do not send data to it */ michael@0: if ((asoc->sctp_cmt_on_off > 0) && michael@0: (net != stcb->asoc.alternate) && michael@0: (net->dest_state & SCTP_ADDR_PF)) { michael@0: goto no_data_fill; michael@0: } michael@0: if (net->flight_size >= net->cwnd) { michael@0: goto no_data_fill; michael@0: } michael@0: if ((asoc->sctp_cmt_on_off > 0) && michael@0: (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) && michael@0: (net->flight_size > max_rwnd_per_dest)) { michael@0: goto no_data_fill; michael@0: } michael@0: /* michael@0: * We need a specific accounting for the usage of the michael@0: * send buffer. We also need to check the number of messages michael@0: * per net. For now, this is better than nothing and it michael@0: * disabled by default... michael@0: */ michael@0: if ((asoc->sctp_cmt_on_off > 0) && michael@0: (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) && michael@0: (max_send_per_dest > 0) && michael@0: (net->flight_size > max_send_per_dest)) { michael@0: goto no_data_fill; michael@0: } michael@0: /*********************/ michael@0: /* Data transmission */ michael@0: /*********************/ michael@0: /* michael@0: * if AUTH for DATA is required and no AUTH has been added michael@0: * yet, account for this in the mtu now... if no data can be michael@0: * bundled, this adjustment won't matter anyways since the michael@0: * packet will be going out... michael@0: */ michael@0: data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, michael@0: stcb->asoc.peer_auth_chunks); michael@0: if (data_auth_reqd && (auth == NULL)) { michael@0: mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: } michael@0: /* now lets add any data within the MTU constraints */ michael@0: switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr))) michael@0: omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr)); michael@0: else michael@0: omtu = 0; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr))) michael@0: omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)); michael@0: else michael@0: omtu = 0; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (net->mtu > sizeof(struct sctphdr)) { michael@0: omtu = net->mtu - sizeof(struct sctphdr); michael@0: } else { michael@0: omtu = 0; michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: omtu = 0; michael@0: break; michael@0: } michael@0: if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && michael@0: (skip_data_for_this_net == 0)) || michael@0: (cookie)) { michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { michael@0: if (no_data_chunks) { michael@0: /* let only control go out */ michael@0: *reason_code = 1; michael@0: break; michael@0: } michael@0: if (net->flight_size >= net->cwnd) { michael@0: /* skip this net, no room for data */ michael@0: *reason_code = 2; michael@0: break; michael@0: } michael@0: if ((chk->whoTo != NULL) && michael@0: (chk->whoTo != net)) { michael@0: /* Don't send the chunk on this net */ michael@0: continue; michael@0: } michael@0: michael@0: if (asoc->sctp_cmt_on_off == 0) { michael@0: if ((asoc->alternate) && michael@0: (asoc->alternate != net) && michael@0: (chk->whoTo == NULL)) { michael@0: continue; michael@0: } else if ((net != asoc->primary_destination) && michael@0: (asoc->alternate == NULL) && michael@0: (chk->whoTo == NULL)) { michael@0: continue; michael@0: } michael@0: } michael@0: if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) { michael@0: /*- michael@0: * strange, we have a chunk that is michael@0: * to big for its destination and michael@0: * yet no fragment ok flag. michael@0: * Something went wrong when the michael@0: * PMTU changed...we did not mark michael@0: * this chunk for some reason?? I michael@0: * will fix it here by letting IP michael@0: * fragment it for now and printing michael@0: * a warning. This really should not michael@0: * happen ... michael@0: */ michael@0: SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n", michael@0: chk->send_size, mtu); michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && michael@0: ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) { michael@0: struct sctp_data_chunk *dchkh; michael@0: michael@0: dchkh = mtod(chk->data, struct sctp_data_chunk *); michael@0: dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY; michael@0: } michael@0: if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) || michael@0: ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) { michael@0: /* ok we will add this one */ michael@0: michael@0: /* michael@0: * Add an AUTH chunk, if chunk michael@0: * requires it, save the offset into michael@0: * the chain for AUTH michael@0: */ michael@0: if (data_auth_reqd) { michael@0: if (auth == NULL) { michael@0: outchain = sctp_add_auth_chunk(outchain, michael@0: &endoutchain, michael@0: &auth, michael@0: &auth_offset, michael@0: stcb, michael@0: SCTP_DATA); michael@0: auth_keyid = chk->auth_keyid; michael@0: override_ok = 0; michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } else if (override_ok) { michael@0: /* use this data's keyid */ michael@0: auth_keyid = chk->auth_keyid; michael@0: override_ok = 0; michael@0: } else if (auth_keyid != chk->auth_keyid) { michael@0: /* different keyid, so done bundling */ michael@0: break; michael@0: } michael@0: } michael@0: outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0, michael@0: chk->send_size, chk->copy_by_ref); michael@0: if (outchain == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n"); michael@0: if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); michael@0: } michael@0: *reason_code = 3; michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: /* upate our MTU size */ michael@0: /* Do clear IP_DF ? */ michael@0: if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { michael@0: no_fragmentflg = 0; michael@0: } michael@0: /* unsigned subtraction of mtu */ michael@0: if (mtu > chk->send_size) michael@0: mtu -= chk->send_size; michael@0: else michael@0: mtu = 0; michael@0: /* unsigned subtraction of r_mtu */ michael@0: if (r_mtu > chk->send_size) michael@0: r_mtu -= chk->send_size; michael@0: else michael@0: r_mtu = 0; michael@0: michael@0: to_out += chk->send_size; michael@0: if ((to_out > mx_mtu) && no_fragmentflg) { michael@0: #ifdef INVARIANTS michael@0: panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out); michael@0: #else michael@0: SCTP_PRINTF("Exceeding mtu of %d out size is %d\n", michael@0: mx_mtu, to_out); michael@0: #endif michael@0: } michael@0: chk->window_probe = 0; michael@0: data_list[bundle_at++] = chk; michael@0: if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { michael@0: break; michael@0: } michael@0: if (chk->sent == SCTP_DATAGRAM_UNSENT) { michael@0: if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks); michael@0: } else { michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks); michael@0: } michael@0: if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) && michael@0: ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) michael@0: /* Count number of user msg's that were fragmented michael@0: * we do this by counting when we see a LAST fragment michael@0: * only. michael@0: */ michael@0: SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs); michael@0: } michael@0: if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) { michael@0: if ((one_chunk) && (stcb->asoc.total_flight == 0)) { michael@0: data_list[0]->window_probe = 1; michael@0: net->window_probe = 1; michael@0: } michael@0: break; michael@0: } michael@0: } else { michael@0: /* michael@0: * Must be sent in order of the michael@0: * TSN's (on a network) michael@0: */ michael@0: break; michael@0: } michael@0: } /* for (chunk gather loop for this net) */ michael@0: } /* if asoc.state OPEN */ michael@0: no_data_fill: michael@0: /* Is there something to send for this destination? */ michael@0: if (outchain) { michael@0: /* We may need to start a control timer or two */ michael@0: if (asconf) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, michael@0: stcb, net); michael@0: /* michael@0: * do NOT clear the asconf flag as it is used michael@0: * to do appropriate source address selection. michael@0: */ michael@0: } michael@0: if (cookie) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); michael@0: cookie = 0; michael@0: } michael@0: /* must start a send timer if data is being sent */ michael@0: if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) { michael@0: /* michael@0: * no timer running on this destination michael@0: * restart it. michael@0: */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); michael@0: } michael@0: /* Now send it, if there is anything to send :> */ michael@0: if ((error = sctp_lowlevel_chunk_output(inp, michael@0: stcb, michael@0: net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: outchain, michael@0: auth_offset, michael@0: auth, michael@0: auth_keyid, michael@0: no_fragmentflg, michael@0: bundle_at, michael@0: asconf, michael@0: inp->sctp_lport, stcb->rport, michael@0: htonl(stcb->asoc.peer_vtag), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked))) { michael@0: /* error, we could not output */ michael@0: if (error == ENOBUFS) { michael@0: SCTP_STAT_INCR(sctps_lowlevelerr); michael@0: asoc->ifp_had_enobuf = 1; michael@0: } michael@0: if (from_where == 0) { michael@0: SCTP_STAT_INCR(sctps_lowlevelerrusr); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); michael@0: if (hbflag) { michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: hbflag = 0; michael@0: } michael@0: if (error == EHOSTUNREACH) { michael@0: /* michael@0: * Destination went unreachable michael@0: * during this send michael@0: */ michael@0: sctp_move_chunks_from_net(stcb, net); michael@0: } michael@0: *reason_code = 6; michael@0: /*- michael@0: * I add this line to be paranoid. As far as michael@0: * I can tell the continue, takes us back to michael@0: * the top of the for, but just to make sure michael@0: * I will reset these again here. michael@0: */ michael@0: ctl_cnt = bundle_at = 0; michael@0: continue; /* This takes us back to the for() for the nets. */ michael@0: } else { michael@0: asoc->ifp_had_enobuf = 0; michael@0: } michael@0: endoutchain = NULL; michael@0: auth = NULL; michael@0: auth_offset = 0; michael@0: if (bundle_at || hbflag) { michael@0: /* For data/asconf and hb set time */ michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); michael@0: *now_filled = 1; michael@0: *now = net->last_sent_time; michael@0: } else { michael@0: net->last_sent_time = *now; michael@0: } michael@0: } michael@0: if (!no_out_cnt) { michael@0: *num_out += (ctl_cnt + bundle_at); michael@0: } michael@0: if (bundle_at) { michael@0: /* setup for a RTO measurement */ michael@0: tsns_sent = data_list[0]->rec.data.TSN_seq; michael@0: /* fill time if not already filled */ michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); michael@0: *now_filled = 1; michael@0: *now = asoc->time_last_sent; michael@0: } else { michael@0: asoc->time_last_sent = *now; michael@0: } michael@0: if (net->rto_needed) { michael@0: data_list[0]->do_rtt = 1; michael@0: net->rto_needed = 0; michael@0: } michael@0: SCTP_STAT_INCR_BY(sctps_senddata, bundle_at); michael@0: sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net); michael@0: } michael@0: if (one_chunk) { michael@0: break; michael@0: } michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND); michael@0: } michael@0: } michael@0: if (old_start_at == NULL) { michael@0: old_start_at = start_at; michael@0: start_at = TAILQ_FIRST(&asoc->nets); michael@0: if (old_start_at) michael@0: goto again_one_more_time; michael@0: } michael@0: michael@0: /* michael@0: * At the end there should be no NON timed chunks hanging on this michael@0: * queue. michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND); michael@0: } michael@0: if ((*num_out == 0) && (*reason_code == 0)) { michael@0: *reason_code = 4; michael@0: } else { michael@0: *reason_code = 5; michael@0: } michael@0: sctp_clean_up_ctl(stcb, asoc, so_locked); michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err) michael@0: { michael@0: /*- michael@0: * Prepend a OPERATIONAL_ERROR chunk header and put on the end of michael@0: * the control chunk queue. michael@0: */ michael@0: struct sctp_chunkhdr *hdr; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct mbuf *mat; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(op_err); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT); michael@0: if (op_err == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: } michael@0: chk->send_size = 0; michael@0: mat = op_err; michael@0: while (mat != NULL) { michael@0: chk->send_size += SCTP_BUF_LEN(mat); michael@0: mat = SCTP_BUF_NEXT(mat); michael@0: } michael@0: chk->rec.chunk_id.id = SCTP_OPERATION_ERROR; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = op_err; michael@0: chk->whoTo = NULL; michael@0: hdr = mtod(op_err, struct sctp_chunkhdr *); michael@0: hdr->chunk_type = SCTP_OPERATION_ERROR; michael@0: hdr->chunk_flags = 0; michael@0: hdr->chunk_length = htons(chk->send_size); michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, michael@0: chk, michael@0: sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: } michael@0: michael@0: int michael@0: sctp_send_cookie_echo(struct mbuf *m, michael@0: int offset, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: { michael@0: /*- michael@0: * pull out the cookie and put it at the front of the control chunk michael@0: * queue. michael@0: */ michael@0: int at; michael@0: struct mbuf *cookie; michael@0: struct sctp_paramhdr parm, *phdr; michael@0: struct sctp_chunkhdr *hdr; michael@0: struct sctp_tmit_chunk *chk; michael@0: uint16_t ptype, plen; michael@0: michael@0: /* First find the cookie in the param area */ michael@0: cookie = NULL; michael@0: at = offset + sizeof(struct sctp_init_chunk); michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: do { michael@0: phdr = sctp_get_next_param(m, at, &parm, sizeof(parm)); michael@0: if (phdr == NULL) { michael@0: return (-3); michael@0: } michael@0: ptype = ntohs(phdr->param_type); michael@0: plen = ntohs(phdr->param_length); michael@0: if (ptype == SCTP_STATE_COOKIE) { michael@0: int pad; michael@0: michael@0: /* found the cookie */ michael@0: if ((pad = (plen % 4))) { michael@0: plen += 4 - pad; michael@0: } michael@0: cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT); michael@0: if (cookie == NULL) { michael@0: /* No memory */ michael@0: return (-2); michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = cookie; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: break; michael@0: } michael@0: at += SCTP_SIZE32(plen); michael@0: } while (phdr); michael@0: if (cookie == NULL) { michael@0: /* Did not find the cookie */ michael@0: return (-3); michael@0: } michael@0: /* ok, we got the cookie lets change it into a cookie echo chunk */ michael@0: michael@0: /* first the change from param to cookie */ michael@0: hdr = mtod(cookie, struct sctp_chunkhdr *); michael@0: hdr->chunk_type = SCTP_COOKIE_ECHO; michael@0: hdr->chunk_flags = 0; michael@0: /* get the chunk stuff now and place it in the FRONT of the queue */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(cookie); michael@0: return (-5); michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->send_size = plen; michael@0: chk->rec.chunk_id.id = SCTP_COOKIE_ECHO; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = CHUNK_FLAGS_FRAGMENT_OK; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = cookie; michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: sctp_send_heartbeat_ack(struct sctp_tcb *stcb, michael@0: struct mbuf *m, michael@0: int offset, michael@0: int chk_length, michael@0: struct sctp_nets *net) michael@0: { michael@0: /* michael@0: * take a HB request and make it into a HB ack and send it. michael@0: */ michael@0: struct mbuf *outchain; michael@0: struct sctp_chunkhdr *chdr; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: michael@0: if (net == NULL) michael@0: /* must have a net pointer */ michael@0: return; michael@0: michael@0: outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT); michael@0: if (outchain == NULL) { michael@0: /* gak out of memory */ michael@0: return; michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = outchain; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: chdr = mtod(outchain, struct sctp_chunkhdr *); michael@0: chdr->chunk_type = SCTP_HEARTBEAT_ACK; michael@0: chdr->chunk_flags = 0; michael@0: if (chk_length % 4) { michael@0: /* need pad */ michael@0: uint32_t cpthis = 0; michael@0: int padlen; michael@0: michael@0: padlen = 4 - (chk_length % 4); michael@0: m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis); michael@0: } michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(outchain); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->send_size = chk_length; michael@0: chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = outchain; michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: } michael@0: michael@0: void michael@0: sctp_send_cookie_ack(struct sctp_tcb *stcb) michael@0: { michael@0: /* formulate and queue a cookie-ack back to sender */ michael@0: struct mbuf *cookie_ack; michael@0: struct sctp_chunkhdr *hdr; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: michael@0: cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (cookie_ack == NULL) { michael@0: /* no mbuf's */ michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD); michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(cookie_ack); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->send_size = sizeof(struct sctp_chunkhdr); michael@0: chk->rec.chunk_id.id = SCTP_COOKIE_ACK; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = cookie_ack; michael@0: if (chk->asoc->last_control_chunk_from != NULL) { michael@0: chk->whoTo = chk->asoc->last_control_chunk_from; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } else { michael@0: chk->whoTo = NULL; michael@0: } michael@0: hdr = mtod(cookie_ack, struct sctp_chunkhdr *); michael@0: hdr->chunk_type = SCTP_COOKIE_ACK; michael@0: hdr->chunk_flags = 0; michael@0: hdr->chunk_length = htons(chk->send_size); michael@0: SCTP_BUF_LEN(cookie_ack) = chk->send_size; michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: return; michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: /* formulate and queue a SHUTDOWN-ACK back to the sender */ michael@0: struct mbuf *m_shutdown_ack; michael@0: struct sctp_shutdown_ack_chunk *ack_cp; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m_shutdown_ack == NULL) { michael@0: /* no mbuf's */ michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD); michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_shutdown_ack); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->send_size = sizeof(struct sctp_chunkhdr); michael@0: chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = m_shutdown_ack; michael@0: chk->whoTo = net; michael@0: if (chk->whoTo) { michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } michael@0: ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *); michael@0: ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK; michael@0: ack_cp->ch.chunk_flags = 0; michael@0: ack_cp->ch.chunk_length = htons(chk->send_size); michael@0: SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size; michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: /* formulate and queue a SHUTDOWN to the sender */ michael@0: struct mbuf *m_shutdown; michael@0: struct sctp_shutdown_chunk *shutdown_cp; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m_shutdown == NULL) { michael@0: /* no mbuf's */ michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD); michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_shutdown); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->send_size = sizeof(struct sctp_shutdown_chunk); michael@0: chk->rec.chunk_id.id = SCTP_SHUTDOWN; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = m_shutdown; michael@0: chk->whoTo = net; michael@0: if (chk->whoTo) { michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } michael@0: shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *); michael@0: shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN; michael@0: shutdown_cp->ch.chunk_flags = 0; michael@0: shutdown_cp->ch.chunk_length = htons(chk->send_size); michael@0: shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); michael@0: SCTP_BUF_LEN(m_shutdown) = chk->send_size; michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked) michael@0: { michael@0: /* michael@0: * formulate and queue an ASCONF to the peer. michael@0: * ASCONF parameters should be queued on the assoc queue. michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: struct mbuf *m_asconf; michael@0: int len; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: michael@0: if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) && michael@0: (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) { michael@0: /* can't send a new one if there is one in flight already */ michael@0: return; michael@0: } michael@0: michael@0: /* compose an ASCONF chunk, maximum length is PMTU */ michael@0: m_asconf = sctp_compose_asconf(stcb, &len, addr_locked); michael@0: if (m_asconf == NULL) { michael@0: return; michael@0: } michael@0: michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_asconf); michael@0: return; michael@0: } michael@0: michael@0: chk->copy_by_ref = 0; michael@0: chk->data = m_asconf; michael@0: chk->send_size = len; michael@0: chk->rec.chunk_id.id = SCTP_ASCONF; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags = CHUNK_FLAGS_FRAGMENT_OK; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->whoTo = net; michael@0: if (chk->whoTo) { michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } michael@0: TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_asconf_ack(struct sctp_tcb *stcb) michael@0: { michael@0: /* michael@0: * formulate and queue a asconf-ack back to sender. michael@0: * the asconf-ack must be stored in the tcb. michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_asconf_ack *ack, *latest_ack; michael@0: struct mbuf *m_ack; michael@0: struct sctp_nets *net = NULL; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: /* Get the latest ASCONF-ACK */ michael@0: latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead); michael@0: if (latest_ack == NULL) { michael@0: return; michael@0: } michael@0: if (latest_ack->last_sent_to != NULL && michael@0: latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) { michael@0: /* we're doing a retransmission */ michael@0: net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0); michael@0: if (net == NULL) { michael@0: /* no alternate */ michael@0: if (stcb->asoc.last_control_chunk_from == NULL) { michael@0: if (stcb->asoc.alternate) { michael@0: net = stcb->asoc.alternate; michael@0: } else { michael@0: net = stcb->asoc.primary_destination; michael@0: } michael@0: } else { michael@0: net = stcb->asoc.last_control_chunk_from; michael@0: } michael@0: } michael@0: } else { michael@0: /* normal case */ michael@0: if (stcb->asoc.last_control_chunk_from == NULL) { michael@0: if (stcb->asoc.alternate) { michael@0: net = stcb->asoc.alternate; michael@0: } else { michael@0: net = stcb->asoc.primary_destination; michael@0: } michael@0: } else { michael@0: net = stcb->asoc.last_control_chunk_from; michael@0: } michael@0: } michael@0: latest_ack->last_sent_to = net; michael@0: michael@0: TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) { michael@0: if (ack->data == NULL) { michael@0: continue; michael@0: } michael@0: michael@0: /* copy the asconf_ack */ michael@0: m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT); michael@0: if (m_ack == NULL) { michael@0: /* couldn't copy it */ michael@0: return; michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = m_ack; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* no memory */ michael@0: if (m_ack) michael@0: sctp_m_freem(m_ack); michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: michael@0: chk->whoTo = net; michael@0: if (chk->whoTo) { michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } michael@0: chk->data = m_ack; michael@0: chk->send_size = 0; michael@0: /* Get size */ michael@0: chk->send_size = ack->len; michael@0: chk->rec.chunk_id.id = SCTP_ASCONF_ACK; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */ michael@0: chk->asoc = &stcb->asoc; michael@0: michael@0: TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); michael@0: chk->asoc->ctrl_queue_cnt++; michael@0: } michael@0: return; michael@0: } michael@0: michael@0: michael@0: static int michael@0: sctp_chunk_retransmission(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /*- michael@0: * send out one MTU of retransmission. If fast_retransmit is michael@0: * happening we ignore the cwnd. Otherwise we obey the cwnd and michael@0: * rwnd. For a Cookie or Asconf in the control chunk queue we michael@0: * retransmit them by themselves. michael@0: * michael@0: * For data chunks we will pick out the lowest TSN's in the sent_queue michael@0: * marked for resend and bundle them all together (up to a MTU of michael@0: * destination). The address to send to should have been michael@0: * selected/changed where the retransmission was marked (i.e. in FR michael@0: * or t3-timeout routines). michael@0: */ michael@0: struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; michael@0: struct sctp_tmit_chunk *chk, *fwd; michael@0: struct mbuf *m, *endofchain; michael@0: struct sctp_nets *net = NULL; michael@0: uint32_t tsns_sent = 0; michael@0: int no_fragmentflg, bundle_at, cnt_thru; michael@0: unsigned int mtu; michael@0: int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started; michael@0: struct sctp_auth_chunk *auth = NULL; michael@0: uint32_t auth_offset = 0; michael@0: uint16_t auth_keyid; michael@0: int override_ok = 1; michael@0: int data_auth_reqd = 0; michael@0: uint32_t dmtu = 0; michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: tmr_started = ctl_cnt = bundle_at = error = 0; michael@0: no_fragmentflg = 1; michael@0: fwd_tsn = 0; michael@0: *cnt_out = 0; michael@0: fwd = NULL; michael@0: endofchain = m = NULL; michael@0: auth_keyid = stcb->asoc.authinfo.active_keyid; michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xC3, 1); michael@0: #endif michael@0: if ((TAILQ_EMPTY(&asoc->sent_queue)) && michael@0: (TAILQ_EMPTY(&asoc->control_send_queue))) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1,"SCTP hits empty queue with cnt set to %d?\n", michael@0: asoc->sent_queue_retran_cnt); michael@0: asoc->sent_queue_cnt = 0; michael@0: asoc->sent_queue_cnt_removeable = 0; michael@0: /* send back 0/0 so we enter normal transmission */ michael@0: *cnt_out = 0; michael@0: return (0); michael@0: } michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) || michael@0: (chk->rec.chunk_id.id == SCTP_STREAM_RESET) || michael@0: (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) { michael@0: if (chk->sent != SCTP_DATAGRAM_RESEND) { michael@0: continue; michael@0: } michael@0: if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { michael@0: if (chk != asoc->str_reset) { michael@0: /* michael@0: * not eligible for retran if its michael@0: * not ours michael@0: */ michael@0: continue; michael@0: } michael@0: } michael@0: ctl_cnt++; michael@0: if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { michael@0: fwd_tsn = 1; michael@0: } michael@0: /* michael@0: * Add an AUTH chunk, if chunk requires it save the michael@0: * offset into the chain for AUTH michael@0: */ michael@0: if ((auth == NULL) && michael@0: (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, michael@0: stcb->asoc.peer_auth_chunks))) { michael@0: m = sctp_add_auth_chunk(m, &endofchain, michael@0: &auth, &auth_offset, michael@0: stcb, michael@0: chk->rec.chunk_id.id); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } michael@0: m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); michael@0: break; michael@0: } michael@0: } michael@0: one_chunk = 0; michael@0: cnt_thru = 0; michael@0: /* do we have control chunks to retransmit? */ michael@0: if (m != NULL) { michael@0: /* Start a timer no matter if we suceed or fail */ michael@0: if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo); michael@0: } else if (chk->rec.chunk_id.id == SCTP_ASCONF) michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo); michael@0: chk->snd_count++; /* update our count */ michael@0: if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo, michael@0: (struct sockaddr *)&chk->whoTo->ro._l_addr, m, michael@0: auth_offset, auth, stcb->asoc.authinfo.active_keyid, michael@0: no_fragmentflg, 0, 0, michael@0: inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), michael@0: chk->whoTo->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked))) { michael@0: SCTP_STAT_INCR(sctps_lowlevelerr); michael@0: return (error); michael@0: } michael@0: endofchain = NULL; michael@0: auth = NULL; michael@0: auth_offset = 0; michael@0: /* michael@0: * We don't want to mark the net->sent time here since this michael@0: * we use this for HB and retrans cannot measure RTT michael@0: */ michael@0: /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */ michael@0: *cnt_out += 1; michael@0: chk->sent = SCTP_DATAGRAM_SENT; michael@0: sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt); michael@0: if (fwd_tsn == 0) { michael@0: return (0); michael@0: } else { michael@0: /* Clean up the fwd-tsn list */ michael@0: sctp_clean_up_ctl(stcb, asoc, so_locked); michael@0: return (0); michael@0: } michael@0: } michael@0: /* michael@0: * Ok, it is just data retransmission we need to do or that and a michael@0: * fwd-tsn with it all. michael@0: */ michael@0: if (TAILQ_EMPTY(&asoc->sent_queue)) { michael@0: return (SCTP_RETRAN_DONE); michael@0: } michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) { michael@0: /* not yet open, resend the cookie and that is it */ michael@0: return (1); michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(20, inp, stcb, NULL); michael@0: #endif michael@0: data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks); michael@0: TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { michael@0: if (chk->sent != SCTP_DATAGRAM_RESEND) { michael@0: /* No, not sent to this net or not ready for rtx */ michael@0: continue; michael@0: } michael@0: if (chk->data == NULL) { michael@0: SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n", michael@0: chk->rec.data.TSN_seq, chk->snd_count, chk->sent); michael@0: continue; michael@0: } michael@0: if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) && michael@0: (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) { michael@0: /* Gak, we have exceeded max unlucky retran, abort! */ michael@0: SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n", michael@0: chk->snd_count, michael@0: SCTP_BASE_SYSCTL(sctp_max_retran_chunk)); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: sctp_abort_an_association(stcb->sctp_ep, stcb, NULL, so_locked); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (SCTP_RETRAN_EXIT); michael@0: } michael@0: /* pick up the net */ michael@0: net = chk->whoTo; michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: mtu = net->mtu - SCTP_MIN_OVERHEAD; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: mtu = net->mtu - sizeof(struct sctphdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: mtu = net->mtu; michael@0: break; michael@0: } michael@0: michael@0: if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) { michael@0: /* No room in peers rwnd */ michael@0: uint32_t tsn; michael@0: michael@0: tsn = asoc->last_acked_seq + 1; michael@0: if (tsn == chk->rec.data.TSN_seq) { michael@0: /* michael@0: * we make a special exception for this michael@0: * case. The peer has no rwnd but is missing michael@0: * the lowest chunk.. which is probably what michael@0: * is holding up the rwnd. michael@0: */ michael@0: goto one_chunk_around; michael@0: } michael@0: return (1); michael@0: } michael@0: one_chunk_around: michael@0: if (asoc->peers_rwnd < mtu) { michael@0: one_chunk = 1; michael@0: if ((asoc->peers_rwnd == 0) && michael@0: (asoc->total_flight == 0)) { michael@0: chk->window_probe = 1; michael@0: chk->whoTo->window_probe = 1; michael@0: } michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xC3, 2); michael@0: #endif michael@0: bundle_at = 0; michael@0: m = NULL; michael@0: net->fast_retran_ip = 0; michael@0: if (chk->rec.data.doing_fast_retransmit == 0) { michael@0: /* michael@0: * if no FR in progress skip destination that have michael@0: * flight_size > cwnd. michael@0: */ michael@0: if (net->flight_size >= net->cwnd) { michael@0: continue; michael@0: } michael@0: } else { michael@0: /* michael@0: * Mark the destination net to have FR recovery michael@0: * limits put on it. michael@0: */ michael@0: *fr_done = 1; michael@0: net->fast_retran_ip = 1; michael@0: } michael@0: michael@0: /* michael@0: * if no AUTH is yet included and this chunk requires it, michael@0: * make sure to account for it. We don't apply the size michael@0: * until the AUTH chunk is actually added below in case michael@0: * there is no room for this chunk. michael@0: */ michael@0: if (data_auth_reqd && (auth == NULL)) { michael@0: dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: } else michael@0: dmtu = 0; michael@0: michael@0: if ((chk->send_size <= (mtu - dmtu)) || michael@0: (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { michael@0: /* ok we will add this one */ michael@0: if (data_auth_reqd) { michael@0: if (auth == NULL) { michael@0: m = sctp_add_auth_chunk(m, michael@0: &endofchain, michael@0: &auth, michael@0: &auth_offset, michael@0: stcb, michael@0: SCTP_DATA); michael@0: auth_keyid = chk->auth_keyid; michael@0: override_ok = 0; michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } else if (override_ok) { michael@0: auth_keyid = chk->auth_keyid; michael@0: override_ok = 0; michael@0: } else if (chk->auth_keyid != auth_keyid) { michael@0: /* different keyid, so done bundling */ michael@0: break; michael@0: } michael@0: } michael@0: m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); michael@0: if (m == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: /* Do clear IP_DF ? */ michael@0: if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { michael@0: no_fragmentflg = 0; michael@0: } michael@0: /* upate our MTU size */ michael@0: if (mtu > (chk->send_size + dmtu)) michael@0: mtu -= (chk->send_size + dmtu); michael@0: else michael@0: mtu = 0; michael@0: data_list[bundle_at++] = chk; michael@0: if (one_chunk && (asoc->total_flight <= 0)) { michael@0: SCTP_STAT_INCR(sctps_windowprobed); michael@0: } michael@0: } michael@0: if (one_chunk == 0) { michael@0: /* michael@0: * now are there anymore forward from chk to pick michael@0: * up? michael@0: */ michael@0: for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) { michael@0: if (fwd->sent != SCTP_DATAGRAM_RESEND) { michael@0: /* Nope, not for retran */ michael@0: continue; michael@0: } michael@0: if (fwd->whoTo != net) { michael@0: /* Nope, not the net in question */ michael@0: continue; michael@0: } michael@0: if (data_auth_reqd && (auth == NULL)) { michael@0: dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); michael@0: } else michael@0: dmtu = 0; michael@0: if (fwd->send_size <= (mtu - dmtu)) { michael@0: if (data_auth_reqd) { michael@0: if (auth == NULL) { michael@0: m = sctp_add_auth_chunk(m, michael@0: &endofchain, michael@0: &auth, michael@0: &auth_offset, michael@0: stcb, michael@0: SCTP_DATA); michael@0: auth_keyid = fwd->auth_keyid; michael@0: override_ok = 0; michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } else if (override_ok) { michael@0: auth_keyid = fwd->auth_keyid; michael@0: override_ok = 0; michael@0: } else if (fwd->auth_keyid != auth_keyid) { michael@0: /* different keyid, so done bundling */ michael@0: break; michael@0: } michael@0: } michael@0: m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref); michael@0: if (m == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: /* Do clear IP_DF ? */ michael@0: if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) { michael@0: no_fragmentflg = 0; michael@0: } michael@0: /* upate our MTU size */ michael@0: if (mtu > (fwd->send_size + dmtu)) michael@0: mtu -= (fwd->send_size + dmtu); michael@0: else michael@0: mtu = 0; michael@0: data_list[bundle_at++] = fwd; michael@0: if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { michael@0: break; michael@0: } michael@0: } else { michael@0: /* can't fit so we are done */ michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: /* Is there something to send for this destination? */ michael@0: if (m) { michael@0: /* michael@0: * No matter if we fail/or suceed we should start a michael@0: * timer. A failure is like a lost IP packet :-) michael@0: */ michael@0: if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { michael@0: /* michael@0: * no timer running on this destination michael@0: * restart it. michael@0: */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); michael@0: tmr_started = 1; michael@0: } michael@0: /* Now lets send it, if there is anything to send :> */ michael@0: if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, m, michael@0: auth_offset, auth, auth_keyid, michael@0: no_fragmentflg, 0, 0, michael@0: inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked))) { michael@0: /* error, we could not output */ michael@0: SCTP_STAT_INCR(sctps_lowlevelerr); michael@0: return (error); michael@0: } michael@0: endofchain = NULL; michael@0: auth = NULL; michael@0: auth_offset = 0; michael@0: /* For HB's */ michael@0: /* michael@0: * We don't want to mark the net->sent time here michael@0: * since this we use this for HB and retrans cannot michael@0: * measure RTT michael@0: */ michael@0: /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */ michael@0: michael@0: /* For auto-close */ michael@0: cnt_thru++; michael@0: if (*now_filled == 0) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); michael@0: *now = asoc->time_last_sent; michael@0: *now_filled = 1; michael@0: } else { michael@0: asoc->time_last_sent = *now; michael@0: } michael@0: *cnt_out += bundle_at; michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xC4, bundle_at); michael@0: #endif michael@0: if (bundle_at) { michael@0: tsns_sent = data_list[0]->rec.data.TSN_seq; michael@0: } michael@0: for (i = 0; i < bundle_at; i++) { michael@0: SCTP_STAT_INCR(sctps_sendretransdata); michael@0: data_list[i]->sent = SCTP_DATAGRAM_SENT; michael@0: /* michael@0: * When we have a revoked data, and we michael@0: * retransmit it, then we clear the revoked michael@0: * flag since this flag dictates if we michael@0: * subtracted from the fs michael@0: */ michael@0: if (data_list[i]->rec.data.chunk_was_revoked) { michael@0: /* Deflate the cwnd */ michael@0: data_list[i]->whoTo->cwnd -= data_list[i]->book_size; michael@0: data_list[i]->rec.data.chunk_was_revoked = 0; michael@0: } michael@0: data_list[i]->snd_count++; michael@0: sctp_ucount_decr(asoc->sent_queue_retran_cnt); michael@0: /* record the time */ michael@0: data_list[i]->sent_rcv_time = asoc->time_last_sent; michael@0: if (data_list[i]->book_size_scale) { michael@0: /* michael@0: * need to double the book size on michael@0: * this one michael@0: */ michael@0: data_list[i]->book_size_scale = 0; michael@0: /* Since we double the booksize, we must michael@0: * also double the output queue size, since this michael@0: * get shrunk when we free by this amount. michael@0: */ michael@0: atomic_add_int(&((asoc)->total_output_queue_size),data_list[i]->book_size); michael@0: data_list[i]->book_size *= 2; michael@0: michael@0: michael@0: } else { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { michael@0: sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, michael@0: asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); michael@0: } michael@0: asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, michael@0: (uint32_t) (data_list[i]->send_size + michael@0: SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { michael@0: sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND, michael@0: data_list[i]->whoTo->flight_size, michael@0: data_list[i]->book_size, michael@0: (uintptr_t)data_list[i]->whoTo, michael@0: data_list[i]->rec.data.TSN_seq); michael@0: } michael@0: sctp_flight_size_increase(data_list[i]); michael@0: sctp_total_flight_increase(stcb, data_list[i]); michael@0: if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { michael@0: /* SWS sender side engages */ michael@0: asoc->peers_rwnd = 0; michael@0: } michael@0: if ((i == 0) && michael@0: (data_list[i]->rec.data.doing_fast_retransmit)) { michael@0: SCTP_STAT_INCR(sctps_sendfastretrans); michael@0: if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) && michael@0: (tmr_started == 0)) { michael@0: /*- michael@0: * ok we just fast-retrans'd michael@0: * the lowest TSN, i.e the michael@0: * first on the list. In michael@0: * this case we want to give michael@0: * some more time to get a michael@0: * SACK back without a michael@0: * t3-expiring. michael@0: */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net, michael@0: SCTP_FROM_SCTP_OUTPUT+SCTP_LOC_4); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); michael@0: } michael@0: } michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND); michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(21, inp, stcb, NULL); michael@0: #endif michael@0: } else { michael@0: /* None will fit */ michael@0: return (1); michael@0: } michael@0: if (asoc->sent_queue_retran_cnt <= 0) { michael@0: /* all done we have no more to retran */ michael@0: asoc->sent_queue_retran_cnt = 0; michael@0: break; michael@0: } michael@0: if (one_chunk) { michael@0: /* No more room in rwnd */ michael@0: return (1); michael@0: } michael@0: /* stop the for loop here. we sent out a packet */ michael@0: break; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: sctp_timer_validation(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc) michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: /* Validate that a timer is running somewhere */ michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { michael@0: /* Here is a timer */ michael@0: return; michael@0: } michael@0: } michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: /* Gak, we did not have a timer somewhere */ michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n"); michael@0: if (asoc->alternate) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate); michael@0: } else { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_chunk_output (struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: int from_where, michael@0: int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /*- michael@0: * Ok this is the generic chunk service queue. we must do the michael@0: * following: michael@0: * - See if there are retransmits pending, if so we must michael@0: * do these first. michael@0: * - Service the stream queue that is next, moving any michael@0: * message (note I must get a complete message i.e. michael@0: * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning michael@0: * TSN's michael@0: * - Check to see if the cwnd/rwnd allows any output, if so we michael@0: * go ahead and fomulate and send the low level chunks. Making sure michael@0: * to combine any control in the control chunk queue also. michael@0: */ michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *net; michael@0: int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0; michael@0: unsigned int burst_cnt = 0; michael@0: struct timeval now; michael@0: int now_filled = 0; michael@0: int nagle_on; michael@0: int frag_point = sctp_get_frag_point(stcb, &stcb->asoc); michael@0: int un_sent = 0; michael@0: int fr_done; michael@0: unsigned int tot_frs = 0; michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: asoc = &stcb->asoc; michael@0: /* The Nagle algorithm is only applied when handling a send call. */ michael@0: if (from_where == SCTP_OUTPUT_FROM_USR_SEND) { michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) { michael@0: nagle_on = 0; michael@0: } else { michael@0: nagle_on = 1; michael@0: } michael@0: } else { michael@0: nagle_on = 0; michael@0: } michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: michael@0: un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); michael@0: michael@0: if ((un_sent <= 0) && michael@0: (TAILQ_EMPTY(&asoc->control_send_queue)) && michael@0: (TAILQ_EMPTY(&asoc->asconf_send_queue)) && michael@0: (asoc->sent_queue_retran_cnt == 0)) { michael@0: /* Nothing to do unless there is something to be sent left */ michael@0: return; michael@0: } michael@0: /* Do we have something to send, data or control AND michael@0: * a sack timer running, if so piggy-back the sack. michael@0: */ michael@0: if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { michael@0: sctp_send_sack(stcb, so_locked); michael@0: (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); michael@0: } michael@0: while (asoc->sent_queue_retran_cnt) { michael@0: /*- michael@0: * Ok, it is retransmission time only, we send out only ONE michael@0: * packet with a single call off to the retran code. michael@0: */ michael@0: if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) { michael@0: /*- michael@0: * Special hook for handling cookiess discarded michael@0: * by peer that carried data. Send cookie-ack only michael@0: * and then the next call with get the retran's. michael@0: */ michael@0: (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, michael@0: from_where, michael@0: &now, &now_filled, frag_point, so_locked); michael@0: return; michael@0: } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) { michael@0: /* if its not from a HB then do it */ michael@0: fr_done = 0; michael@0: ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked); michael@0: if (fr_done) { michael@0: tot_frs++; michael@0: } michael@0: } else { michael@0: /* michael@0: * its from any other place, we don't allow retran michael@0: * output (only control) michael@0: */ michael@0: ret = 1; michael@0: } michael@0: if (ret > 0) { michael@0: /* Can't send anymore */ michael@0: /*- michael@0: * now lets push out control by calling med-level michael@0: * output once. this assures that we WILL send HB's michael@0: * if queued too. michael@0: */ michael@0: (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, michael@0: from_where, michael@0: &now, &now_filled, frag_point, so_locked); michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(8, inp, stcb, NULL); michael@0: #endif michael@0: sctp_timer_validation(inp, stcb, asoc); michael@0: return; michael@0: } michael@0: if (ret < 0) { michael@0: /*- michael@0: * The count was off.. retran is not happening so do michael@0: * the normal retransmission. michael@0: */ michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(9, inp, stcb, NULL); michael@0: #endif michael@0: if (ret == SCTP_RETRAN_EXIT) { michael@0: return; michael@0: } michael@0: break; michael@0: } michael@0: if (from_where == SCTP_OUTPUT_FROM_T3) { michael@0: /* Only one transmission allowed out of a timeout */ michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(10, inp, stcb, NULL); michael@0: #endif michael@0: /* Push out any control */ michael@0: (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where, michael@0: &now, &now_filled, frag_point, so_locked); michael@0: return; michael@0: } michael@0: if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) { michael@0: /* Hit FR burst limit */ michael@0: return; michael@0: } michael@0: if ((num_out == 0) && (ret == 0)) { michael@0: /* No more retrans to send */ michael@0: break; michael@0: } michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(12, inp, stcb, NULL); michael@0: #endif michael@0: /* Check for bad destinations, if they exist move chunks around. */ michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { michael@0: /*- michael@0: * if possible move things off of this address we michael@0: * still may send below due to the dormant state but michael@0: * we try to find an alternate address to send to michael@0: * and if we have one we move all queued data on the michael@0: * out wheel to this alternate address. michael@0: */ michael@0: if (net->ref_count > 1) michael@0: sctp_move_chunks_from_net(stcb, net); michael@0: } else { michael@0: /*- michael@0: * if ((asoc->sat_network) || (net->addr_is_local)) michael@0: * { burst_limit = asoc->max_burst * michael@0: * SCTP_SAT_NETWORK_BURST_INCR; } michael@0: */ michael@0: if (asoc->max_burst > 0) { michael@0: if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) { michael@0: if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) { michael@0: /* JRS - Use the congestion control given in the congestion control module */ michael@0: asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { michael@0: sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED); michael@0: } michael@0: SCTP_STAT_INCR(sctps_maxburstqueued); michael@0: } michael@0: net->fast_retran_ip = 0; michael@0: } else { michael@0: if (net->flight_size == 0) { michael@0: /* Should be decaying the cwnd here */ michael@0: ; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: } michael@0: burst_cnt = 0; michael@0: do { michael@0: error = sctp_med_chunk_output(inp, stcb, asoc, &num_out, michael@0: &reason_code, 0, from_where, michael@0: &now, &now_filled, frag_point, so_locked); michael@0: if (error) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { michael@0: sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP); michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES); michael@0: sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES); michael@0: } michael@0: break; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out); michael@0: michael@0: tot_out += num_out; michael@0: burst_cnt++; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES); michael@0: if (num_out == 0) { michael@0: sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES); michael@0: } michael@0: } michael@0: if (nagle_on) { michael@0: /* michael@0: * When the Nagle algorithm is used, look at how much michael@0: * is unsent, then if its smaller than an MTU and we michael@0: * have data in flight we stop, except if we are michael@0: * handling a fragmented user message. michael@0: */ michael@0: un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + michael@0: (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); michael@0: if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) && michael@0: (stcb->asoc.total_flight > 0) && michael@0: ((stcb->asoc.locked_on_sending == NULL) || michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { michael@0: break; michael@0: } michael@0: } michael@0: if (TAILQ_EMPTY(&asoc->control_send_queue) && michael@0: TAILQ_EMPTY(&asoc->send_queue) && michael@0: stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { michael@0: /* Nothing left to send */ michael@0: break; michael@0: } michael@0: if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) { michael@0: /* Nothing left to send */ michael@0: break; michael@0: } michael@0: } while (num_out && michael@0: ((asoc->max_burst == 0) || michael@0: SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) || michael@0: (burst_cnt < asoc->max_burst))); michael@0: michael@0: if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) { michael@0: if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) { michael@0: SCTP_STAT_INCR(sctps_maxburstqueued); michael@0: asoc->burst_limit_applied = 1; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { michael@0: sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED); michael@0: } michael@0: } else { michael@0: asoc->burst_limit_applied = 0; michael@0: } michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { michael@0: sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n", michael@0: tot_out); michael@0: michael@0: /*- michael@0: * Now we need to clean up the control chunk chain if a ECNE is on michael@0: * it. It must be marked as UNSENT again so next call will continue michael@0: * to send it until such time that we get a CWR, to remove it. michael@0: */ michael@0: if (stcb->asoc.ecn_echo_cnt_onq) michael@0: sctp_fix_ecn_echo(asoc); michael@0: return; michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_output( michael@0: struct sctp_inpcb *inp, michael@0: #if defined(__Panda__) michael@0: pakhandle_type m, michael@0: #else michael@0: struct mbuf *m, michael@0: #endif michael@0: struct sockaddr *addr, michael@0: #if defined(__Panda__) michael@0: pakhandle_type control, michael@0: #else michael@0: struct mbuf *control, michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: struct thread *p, michael@0: #elif defined(__Windows__) michael@0: PKTHREAD p, michael@0: #else michael@0: #if defined(__APPLE__) michael@0: struct proc *p SCTP_UNUSED, michael@0: #else michael@0: struct proc *p, michael@0: #endif michael@0: #endif michael@0: int flags) michael@0: { michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: if (inp->sctp_socket == NULL) { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: return (sctp_sosend(inp->sctp_socket, michael@0: addr, michael@0: (struct uio *)NULL, michael@0: m, michael@0: control, michael@0: #if defined(__APPLE__) || defined(__Panda__) michael@0: flags michael@0: #else michael@0: flags, p michael@0: #endif michael@0: )); michael@0: } michael@0: michael@0: void michael@0: send_forward_tsn(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc) michael@0: { michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_forward_tsn_chunk *fwdtsn; michael@0: uint32_t advance_peer_ack_point; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { michael@0: /* mark it to unsent */ michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: /* Do we correct its output location? */ michael@0: if (chk->whoTo) { michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: chk->whoTo = NULL; michael@0: } michael@0: goto sctp_fill_in_rest; michael@0: } michael@0: } michael@0: /* Ok if we reach here we must build one */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: return; michael@0: } michael@0: asoc->fwd_tsn_cnt++; michael@0: chk->copy_by_ref = 0; michael@0: chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->asoc = asoc; michael@0: chk->whoTo = NULL; michael@0: chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (chk->data == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next); michael@0: asoc->ctrl_queue_cnt++; michael@0: sctp_fill_in_rest: michael@0: /*- michael@0: * Here we go through and fill out the part that deals with michael@0: * stream/seq of the ones we skip. michael@0: */ michael@0: SCTP_BUF_LEN(chk->data) = 0; michael@0: { michael@0: struct sctp_tmit_chunk *at, *tp1, *last; michael@0: struct sctp_strseq *strseq; michael@0: unsigned int cnt_of_space, i, ovh; michael@0: unsigned int space_needed; michael@0: unsigned int cnt_of_skipped = 0; michael@0: michael@0: TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { michael@0: if ((at->sent != SCTP_FORWARD_TSN_SKIP) && michael@0: (at->sent != SCTP_DATAGRAM_NR_ACKED)) { michael@0: /* no more to look at */ michael@0: break; michael@0: } michael@0: if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { michael@0: /* We don't report these */ michael@0: continue; michael@0: } michael@0: cnt_of_skipped++; michael@0: } michael@0: space_needed = (sizeof(struct sctp_forward_tsn_chunk) + michael@0: (cnt_of_skipped * sizeof(struct sctp_strseq))); michael@0: michael@0: cnt_of_space = M_TRAILINGSPACE(chk->data); michael@0: michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: ovh = SCTP_MIN_OVERHEAD; michael@0: } else { michael@0: ovh = SCTP_MIN_V4_OVERHEAD; michael@0: } michael@0: if (cnt_of_space > (asoc->smallest_mtu - ovh)) { michael@0: /* trim to a mtu size */ michael@0: cnt_of_space = asoc->smallest_mtu - ovh; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { michael@0: sctp_misc_ints(SCTP_FWD_TSN_CHECK, michael@0: 0xff, 0, cnt_of_skipped, michael@0: asoc->advanced_peer_ack_point); michael@0: michael@0: } michael@0: advance_peer_ack_point = asoc->advanced_peer_ack_point; michael@0: if (cnt_of_space < space_needed) { michael@0: /*- michael@0: * ok we must trim down the chunk by lowering the michael@0: * advance peer ack point. michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { michael@0: sctp_misc_ints(SCTP_FWD_TSN_CHECK, michael@0: 0xff, 0xff, cnt_of_space, michael@0: space_needed); michael@0: } michael@0: cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk); michael@0: cnt_of_skipped /= sizeof(struct sctp_strseq); michael@0: /*- michael@0: * Go through and find the TSN that will be the one michael@0: * we report. michael@0: */ michael@0: at = TAILQ_FIRST(&asoc->sent_queue); michael@0: if (at != NULL) { michael@0: for (i = 0; i < cnt_of_skipped; i++) { michael@0: tp1 = TAILQ_NEXT(at, sctp_next); michael@0: if (tp1 == NULL) { michael@0: break; michael@0: } michael@0: at = tp1; michael@0: } michael@0: } michael@0: if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { michael@0: sctp_misc_ints(SCTP_FWD_TSN_CHECK, michael@0: 0xff, cnt_of_skipped, at->rec.data.TSN_seq, michael@0: asoc->advanced_peer_ack_point); michael@0: } michael@0: last = at; michael@0: /*- michael@0: * last now points to last one I can report, update michael@0: * peer ack point michael@0: */ michael@0: if (last) michael@0: advance_peer_ack_point = last->rec.data.TSN_seq; michael@0: space_needed = sizeof(struct sctp_forward_tsn_chunk) + michael@0: cnt_of_skipped * sizeof(struct sctp_strseq); michael@0: } michael@0: chk->send_size = space_needed; michael@0: /* Setup the chunk */ michael@0: fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *); michael@0: fwdtsn->ch.chunk_length = htons(chk->send_size); michael@0: fwdtsn->ch.chunk_flags = 0; michael@0: fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN; michael@0: fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: fwdtsn++; michael@0: /*- michael@0: * Move pointer to after the fwdtsn and transfer to the michael@0: * strseq pointer. michael@0: */ michael@0: strseq = (struct sctp_strseq *)fwdtsn; michael@0: /*- michael@0: * Now populate the strseq list. This is done blindly michael@0: * without pulling out duplicate stream info. This is michael@0: * inefficent but won't harm the process since the peer will michael@0: * look at these in sequence and will thus release anything. michael@0: * It could mean we exceed the PMTU and chop off some that michael@0: * we could have included.. but this is unlikely (aka 1432/4 michael@0: * would mean 300+ stream seq's would have to be reported in michael@0: * one FWD-TSN. With a bit of work we can later FIX this to michael@0: * optimize and pull out duplcates.. but it does add more michael@0: * overhead. So for now... not! michael@0: */ michael@0: at = TAILQ_FIRST(&asoc->sent_queue); michael@0: for (i = 0; i < cnt_of_skipped; i++) { michael@0: tp1 = TAILQ_NEXT(at, sctp_next); michael@0: if (tp1 == NULL) michael@0: break; michael@0: if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { michael@0: /* We don't report these */ michael@0: i--; michael@0: at = tp1; michael@0: continue; michael@0: } michael@0: if (at->rec.data.TSN_seq == advance_peer_ack_point) { michael@0: at->rec.data.fwd_tsn_cnt = 0; michael@0: } michael@0: strseq->stream = ntohs(at->rec.data.stream_number); michael@0: strseq->sequence = ntohs(at->rec.data.stream_seq); michael@0: strseq++; michael@0: at = tp1; michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_sack(struct sctp_tcb *stcb, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /*- michael@0: * Queue up a SACK or NR-SACK in the control queue. michael@0: * We must first check to see if a SACK or NR-SACK is michael@0: * somehow on the control queue. michael@0: * If so, we will take and and remove the old one. michael@0: */ michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk, *a_chk; michael@0: struct sctp_sack_chunk *sack; michael@0: struct sctp_nr_sack_chunk *nr_sack; michael@0: struct sctp_gap_ack_block *gap_descriptor; michael@0: struct sack_track *selector; michael@0: int mergeable = 0; michael@0: int offset; michael@0: caddr_t limit; michael@0: uint32_t *dup; michael@0: int limit_reached = 0; michael@0: unsigned int i, siz, j; michael@0: unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space; michael@0: int num_dups = 0; michael@0: int space_req; michael@0: uint32_t highest_tsn; michael@0: uint8_t flags; michael@0: uint8_t type; michael@0: uint8_t tsn_map; michael@0: michael@0: if ((stcb->asoc.sctp_nr_sack_on_off == 1) && michael@0: (stcb->asoc.peer_supports_nr_sack == 1)) { michael@0: type = SCTP_NR_SELECTIVE_ACK; michael@0: } else { michael@0: type = SCTP_SELECTIVE_ACK; michael@0: } michael@0: a_chk = NULL; michael@0: asoc = &stcb->asoc; michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (asoc->last_data_chunk_from == NULL) { michael@0: /* Hmm we never received anything */ michael@0: return; michael@0: } michael@0: sctp_slide_mapping_arrays(stcb); michael@0: sctp_set_rwnd(stcb, asoc); michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if (chk->rec.chunk_id.id == type) { michael@0: /* Hmm, found a sack already on queue, remove it */ michael@0: TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); michael@0: asoc->ctrl_queue_cnt--; michael@0: a_chk = chk; michael@0: if (a_chk->data) { michael@0: sctp_m_freem(a_chk->data); michael@0: a_chk->data = NULL; michael@0: } michael@0: if (a_chk->whoTo) { michael@0: sctp_free_remote_addr(a_chk->whoTo); michael@0: a_chk->whoTo = NULL; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if (a_chk == NULL) { michael@0: sctp_alloc_a_chunk(stcb, a_chk); michael@0: if (a_chk == NULL) { michael@0: /* No memory so we drop the idea, and set a timer */ michael@0: if (stcb->asoc.delayed_ack) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_RECV, michael@0: stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_RECV, michael@0: stcb->sctp_ep, stcb, NULL); michael@0: } else { michael@0: stcb->asoc.send_sack = 1; michael@0: } michael@0: return; michael@0: } michael@0: a_chk->copy_by_ref = 0; michael@0: a_chk->rec.chunk_id.id = type; michael@0: a_chk->rec.chunk_id.can_take_data = 1; michael@0: } michael@0: /* Clear our pkt counts */ michael@0: asoc->data_pkts_seen = 0; michael@0: michael@0: a_chk->asoc = asoc; michael@0: a_chk->snd_count = 0; michael@0: a_chk->send_size = 0; /* fill in later */ michael@0: a_chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: a_chk->whoTo = NULL; michael@0: michael@0: if ((asoc->numduptsns) || michael@0: (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) { michael@0: /*- michael@0: * Ok, we have some duplicates or the destination for the michael@0: * sack is unreachable, lets see if we can select an michael@0: * alternate than asoc->last_data_chunk_from michael@0: */ michael@0: if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) && michael@0: (asoc->used_alt_onsack > asoc->numnets)) { michael@0: /* We used an alt last time, don't this time */ michael@0: a_chk->whoTo = NULL; michael@0: } else { michael@0: asoc->used_alt_onsack++; michael@0: a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0); michael@0: } michael@0: if (a_chk->whoTo == NULL) { michael@0: /* Nope, no alternate */ michael@0: a_chk->whoTo = asoc->last_data_chunk_from; michael@0: asoc->used_alt_onsack = 0; michael@0: } michael@0: } else { michael@0: /* michael@0: * No duplicates so we use the last place we received data michael@0: * from. michael@0: */ michael@0: asoc->used_alt_onsack = 0; michael@0: a_chk->whoTo = asoc->last_data_chunk_from; michael@0: } michael@0: if (a_chk->whoTo) { michael@0: atomic_add_int(&a_chk->whoTo->ref_count, 1); michael@0: } michael@0: if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) { michael@0: highest_tsn = asoc->highest_tsn_inside_map; michael@0: } else { michael@0: highest_tsn = asoc->highest_tsn_inside_nr_map; michael@0: } michael@0: if (highest_tsn == asoc->cumulative_tsn) { michael@0: /* no gaps */ michael@0: if (type == SCTP_SELECTIVE_ACK) { michael@0: space_req = sizeof(struct sctp_sack_chunk); michael@0: } else { michael@0: space_req = sizeof(struct sctp_nr_sack_chunk); michael@0: } michael@0: } else { michael@0: /* gaps get a cluster */ michael@0: space_req = MCLBYTES; michael@0: } michael@0: /* Ok now lets formulate a MBUF with our sack */ michael@0: a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA); michael@0: if ((a_chk->data == NULL) || michael@0: (a_chk->whoTo == NULL)) { michael@0: /* rats, no mbuf memory */ michael@0: if (a_chk->data) { michael@0: /* was a problem with the destination */ michael@0: sctp_m_freem(a_chk->data); michael@0: a_chk->data = NULL; michael@0: } michael@0: sctp_free_a_chunk(stcb, a_chk, so_locked); michael@0: /* sa_ignore NO_NULL_CHK */ michael@0: if (stcb->asoc.delayed_ack) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_RECV, michael@0: stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_RECV, michael@0: stcb->sctp_ep, stcb, NULL); michael@0: } else { michael@0: stcb->asoc.send_sack = 1; michael@0: } michael@0: return; michael@0: } michael@0: /* ok, lets go through and fill it in */ michael@0: SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD); michael@0: space = M_TRAILINGSPACE(a_chk->data); michael@0: if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) { michael@0: space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD); michael@0: } michael@0: limit = mtod(a_chk->data, caddr_t); michael@0: limit += space; michael@0: michael@0: flags = 0; michael@0: michael@0: if ((asoc->sctp_cmt_on_off > 0) && michael@0: SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { michael@0: /*- michael@0: * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been michael@0: * received, then set high bit to 1, else 0. Reset michael@0: * pkts_rcvd. michael@0: */ michael@0: flags |= (asoc->cmt_dac_pkts_rcvd << 6); michael@0: asoc->cmt_dac_pkts_rcvd = 0; michael@0: } michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn; michael@0: stcb->asoc.cumack_log_atsnt++; michael@0: if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) { michael@0: stcb->asoc.cumack_log_atsnt = 0; michael@0: } michael@0: #endif michael@0: /* reset the readers interpretation */ michael@0: stcb->freed_by_sorcv_sincelast = 0; michael@0: michael@0: if (type == SCTP_SELECTIVE_ACK) { michael@0: sack = mtod(a_chk->data, struct sctp_sack_chunk *); michael@0: nr_sack = NULL; michael@0: gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk)); michael@0: if (highest_tsn > asoc->mapping_array_base_tsn) { michael@0: siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8; michael@0: } else { michael@0: siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8; michael@0: } michael@0: } else { michael@0: sack = NULL; michael@0: nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *); michael@0: gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk)); michael@0: if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) { michael@0: siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; michael@0: } else { michael@0: siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8; michael@0: } michael@0: } michael@0: michael@0: if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { michael@0: offset = 1; michael@0: } else { michael@0: offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; michael@0: } michael@0: if (((type == SCTP_SELECTIVE_ACK) && michael@0: SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) || michael@0: ((type == SCTP_NR_SELECTIVE_ACK) && michael@0: SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) { michael@0: /* we have a gap .. maybe */ michael@0: for (i = 0; i < siz; i++) { michael@0: tsn_map = asoc->mapping_array[i]; michael@0: if (type == SCTP_SELECTIVE_ACK) { michael@0: tsn_map |= asoc->nr_mapping_array[i]; michael@0: } michael@0: if (i == 0) { michael@0: /* michael@0: * Clear all bits corresponding to TSNs michael@0: * smaller or equal to the cumulative TSN. michael@0: */ michael@0: tsn_map &= (~0 << (1 - offset)); michael@0: } michael@0: selector = &sack_array[tsn_map]; michael@0: if (mergeable && selector->right_edge) { michael@0: /* michael@0: * Backup, left and right edges were ok to michael@0: * merge. michael@0: */ michael@0: num_gap_blocks--; michael@0: gap_descriptor--; michael@0: } michael@0: if (selector->num_entries == 0) michael@0: mergeable = 0; michael@0: else { michael@0: for (j = 0; j < selector->num_entries; j++) { michael@0: if (mergeable && selector->right_edge) { michael@0: /* michael@0: * do a merge by NOT setting michael@0: * the left side michael@0: */ michael@0: mergeable = 0; michael@0: } else { michael@0: /* michael@0: * no merge, set the left michael@0: * side michael@0: */ michael@0: mergeable = 0; michael@0: gap_descriptor->start = htons((selector->gaps[j].start + offset)); michael@0: } michael@0: gap_descriptor->end = htons((selector->gaps[j].end + offset)); michael@0: num_gap_blocks++; michael@0: gap_descriptor++; michael@0: if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { michael@0: /* no more room */ michael@0: limit_reached = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (selector->left_edge) { michael@0: mergeable = 1; michael@0: } michael@0: } michael@0: if (limit_reached) { michael@0: /* Reached the limit stop */ michael@0: break; michael@0: } michael@0: offset += 8; michael@0: } michael@0: } michael@0: if ((type == SCTP_NR_SELECTIVE_ACK) && michael@0: (limit_reached == 0)) { michael@0: michael@0: mergeable = 0; michael@0: michael@0: if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) { michael@0: siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; michael@0: } else { michael@0: siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8; michael@0: } michael@0: michael@0: if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { michael@0: offset = 1; michael@0: } else { michael@0: offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; michael@0: } michael@0: if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) { michael@0: /* we have a gap .. maybe */ michael@0: for (i = 0; i < siz; i++) { michael@0: tsn_map = asoc->nr_mapping_array[i]; michael@0: if (i == 0) { michael@0: /* michael@0: * Clear all bits corresponding to TSNs michael@0: * smaller or equal to the cumulative TSN. michael@0: */ michael@0: tsn_map &= (~0 << (1 - offset)); michael@0: } michael@0: selector = &sack_array[tsn_map]; michael@0: if (mergeable && selector->right_edge) { michael@0: /* michael@0: * Backup, left and right edges were ok to michael@0: * merge. michael@0: */ michael@0: num_nr_gap_blocks--; michael@0: gap_descriptor--; michael@0: } michael@0: if (selector->num_entries == 0) michael@0: mergeable = 0; michael@0: else { michael@0: for (j = 0; j < selector->num_entries; j++) { michael@0: if (mergeable && selector->right_edge) { michael@0: /* michael@0: * do a merge by NOT setting michael@0: * the left side michael@0: */ michael@0: mergeable = 0; michael@0: } else { michael@0: /* michael@0: * no merge, set the left michael@0: * side michael@0: */ michael@0: mergeable = 0; michael@0: gap_descriptor->start = htons((selector->gaps[j].start + offset)); michael@0: } michael@0: gap_descriptor->end = htons((selector->gaps[j].end + offset)); michael@0: num_nr_gap_blocks++; michael@0: gap_descriptor++; michael@0: if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { michael@0: /* no more room */ michael@0: limit_reached = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (selector->left_edge) { michael@0: mergeable = 1; michael@0: } michael@0: } michael@0: if (limit_reached) { michael@0: /* Reached the limit stop */ michael@0: break; michael@0: } michael@0: offset += 8; michael@0: } michael@0: } michael@0: } michael@0: /* now we must add any dups we are going to report. */ michael@0: if ((limit_reached == 0) && (asoc->numduptsns)) { michael@0: dup = (uint32_t *) gap_descriptor; michael@0: for (i = 0; i < asoc->numduptsns; i++) { michael@0: *dup = htonl(asoc->dup_tsns[i]); michael@0: dup++; michael@0: num_dups++; michael@0: if (((caddr_t)dup + sizeof(uint32_t)) > limit) { michael@0: /* no more room */ michael@0: break; michael@0: } michael@0: } michael@0: asoc->numduptsns = 0; michael@0: } michael@0: /* michael@0: * now that the chunk is prepared queue it to the control chunk michael@0: * queue. michael@0: */ michael@0: if (type == SCTP_SELECTIVE_ACK) { michael@0: a_chk->send_size = sizeof(struct sctp_sack_chunk) + michael@0: (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + michael@0: num_dups * sizeof(int32_t); michael@0: SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; michael@0: sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); michael@0: sack->sack.a_rwnd = htonl(asoc->my_rwnd); michael@0: sack->sack.num_gap_ack_blks = htons(num_gap_blocks); michael@0: sack->sack.num_dup_tsns = htons(num_dups); michael@0: sack->ch.chunk_type = type; michael@0: sack->ch.chunk_flags = flags; michael@0: sack->ch.chunk_length = htons(a_chk->send_size); michael@0: } else { michael@0: a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) + michael@0: (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + michael@0: num_dups * sizeof(int32_t); michael@0: SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; michael@0: nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); michael@0: nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd); michael@0: nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks); michael@0: nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks); michael@0: nr_sack->nr_sack.num_dup_tsns = htons(num_dups); michael@0: nr_sack->nr_sack.reserved = 0; michael@0: nr_sack->ch.chunk_type = type; michael@0: nr_sack->ch.chunk_flags = flags; michael@0: nr_sack->ch.chunk_length = htons(a_chk->send_size); michael@0: } michael@0: TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next); michael@0: asoc->my_last_reported_rwnd = asoc->my_rwnd; michael@0: asoc->ctrl_queue_cnt++; michael@0: asoc->send_sack = 0; michael@0: SCTP_STAT_INCR(sctps_sendsacks); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_abort, *m, *m_last; michael@0: struct mbuf *m_out, *m_end = NULL; michael@0: struct sctp_abort_chunk *abort; michael@0: struct sctp_auth_chunk *auth = NULL; michael@0: struct sctp_nets *net; michael@0: uint32_t vtag; michael@0: uint32_t auth_offset = 0; michael@0: uint16_t cause_len, chunk_len, padding_len; michael@0: michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } michael@0: #endif michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: /*- michael@0: * Add an AUTH chunk, if chunk requires it and save the offset into michael@0: * the chain for AUTH michael@0: */ michael@0: if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION, michael@0: stcb->asoc.peer_auth_chunks)) { michael@0: m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset, michael@0: stcb, SCTP_ABORT_ASSOCIATION); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } else { michael@0: m_out = NULL; michael@0: } michael@0: m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m_abort == NULL) { michael@0: if (m_out) { michael@0: sctp_m_freem(m_out); michael@0: } michael@0: if (operr) { michael@0: sctp_m_freem(operr); michael@0: } michael@0: return; michael@0: } michael@0: /* link in any error */ michael@0: SCTP_BUF_NEXT(m_abort) = operr; michael@0: cause_len = 0; michael@0: m_last = NULL; michael@0: for (m = operr; m; m = SCTP_BUF_NEXT(m)) { michael@0: cause_len += (uint16_t)SCTP_BUF_LEN(m); michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: m_last = m; michael@0: } michael@0: } michael@0: SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk); michael@0: chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len; michael@0: padding_len = SCTP_SIZE32(chunk_len) - chunk_len; michael@0: if (m_out == NULL) { michael@0: /* NO Auth chunk prepended, so reserve space in front */ michael@0: SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD); michael@0: m_out = m_abort; michael@0: } else { michael@0: /* Put AUTH chunk at the front of the chain */ michael@0: SCTP_BUF_NEXT(m_end) = m_abort; michael@0: } michael@0: if (stcb->asoc.alternate) { michael@0: net = stcb->asoc.alternate; michael@0: } else { michael@0: net = stcb->asoc.primary_destination; michael@0: } michael@0: /* Fill in the ABORT chunk header. */ michael@0: abort = mtod(m_abort, struct sctp_abort_chunk *); michael@0: abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION; michael@0: if (stcb->asoc.peer_vtag == 0) { michael@0: /* This happens iff the assoc is in COOKIE-WAIT state. */ michael@0: vtag = stcb->asoc.my_vtag; michael@0: abort->ch.chunk_flags = SCTP_HAD_NO_TCB; michael@0: } else { michael@0: vtag = stcb->asoc.peer_vtag; michael@0: abort->ch.chunk_flags = 0; michael@0: } michael@0: abort->ch.chunk_length = htons(chunk_len); michael@0: /* Add padding, if necessary. */ michael@0: if (padding_len > 0) { michael@0: if ((m_last == NULL) || sctp_add_pad_tombuf(m_last, padding_len)) { michael@0: sctp_m_freem(m_out); michael@0: return; michael@0: } michael@0: } michael@0: (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0, michael@0: stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag), michael@0: stcb->asoc.primary_destination->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: so_locked); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: } michael@0: michael@0: void michael@0: sctp_send_shutdown_complete(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, michael@0: int reflect_vtag) michael@0: { michael@0: /* formulate and SEND a SHUTDOWN-COMPLETE */ michael@0: struct mbuf *m_shutdown_comp; michael@0: struct sctp_shutdown_complete_chunk *shutdown_complete; michael@0: uint32_t vtag; michael@0: uint8_t flags; michael@0: michael@0: m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m_shutdown_comp == NULL) { michael@0: /* no mbuf's */ michael@0: return; michael@0: } michael@0: if (reflect_vtag) { michael@0: flags = SCTP_HAD_NO_TCB; michael@0: vtag = stcb->asoc.my_vtag; michael@0: } else { michael@0: flags = 0; michael@0: vtag = stcb->asoc.peer_vtag; michael@0: } michael@0: shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *); michael@0: shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE; michael@0: shutdown_complete->ch.chunk_flags = flags; michael@0: shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk)); michael@0: SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk); michael@0: (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, michael@0: (struct sockaddr *)&net->ro._l_addr, michael@0: m_shutdown_comp, 0, NULL, 0, 1, 0, 0, michael@0: stcb->sctp_ep->sctp_lport, stcb->rport, michael@0: htonl(vtag), michael@0: net->port, NULL, michael@0: #if defined(__FreeBSD__) michael@0: 0, 0, michael@0: #endif michael@0: SCTP_SO_NOT_LOCKED); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: return; michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) michael@0: static void michael@0: sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, uint32_t vtag, michael@0: uint8_t type, struct mbuf *cause, michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: uint32_t vrf_id, uint16_t port) michael@0: #else michael@0: static void michael@0: sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, uint32_t vtag, michael@0: uint8_t type, struct mbuf *cause, michael@0: uint32_t vrf_id SCTP_UNUSED, uint16_t port) michael@0: #endif michael@0: { michael@0: #ifdef __Panda__ michael@0: pakhandle_type o_pak; michael@0: #else michael@0: struct mbuf *o_pak; michael@0: #endif michael@0: struct mbuf *mout; michael@0: struct sctphdr *shout; michael@0: struct sctp_chunkhdr *ch; michael@0: struct udphdr *udp; michael@0: int len, cause_len, padding_len; michael@0: #if defined(INET) || defined(INET6) michael@0: int ret; michael@0: #endif michael@0: #ifdef INET michael@0: #if defined(__APPLE__) || defined(__Panda__) michael@0: sctp_route_t ro; michael@0: #endif michael@0: struct sockaddr_in *src_sin, *dst_sin; michael@0: struct ip *ip; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *src_sin6, *dst_sin6; michael@0: struct ip6_hdr *ip6; michael@0: #endif michael@0: michael@0: /* Compute the length of the cause and add final padding. */ michael@0: cause_len = 0; michael@0: if (cause != NULL) { michael@0: struct mbuf *m_at, *m_last = NULL; michael@0: michael@0: for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) michael@0: m_last = m_at; michael@0: cause_len += SCTP_BUF_LEN(m_at); michael@0: } michael@0: padding_len = cause_len % 4; michael@0: if (padding_len != 0) { michael@0: padding_len = 4 - padding_len; michael@0: } michael@0: if (padding_len != 0) { michael@0: if (sctp_add_pad_tombuf(m_last, padding_len)) { michael@0: sctp_m_freem(cause); michael@0: return; michael@0: } michael@0: } michael@0: } else { michael@0: padding_len = 0; michael@0: } michael@0: /* Get an mbuf for the header. */ michael@0: len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: switch (dst->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: len += sizeof(struct ip); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: len += sizeof(struct ip6_hdr); michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: if (port) { michael@0: len += sizeof(struct udphdr); michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); michael@0: #else michael@0: mout = sctp_get_mbuf_for_msg(len + SCTP_MAX_LINKHDR, 1, M_NOWAIT, 1, MT_DATA); michael@0: #endif michael@0: #else michael@0: mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); michael@0: #endif michael@0: if (mout == NULL) { michael@0: if (cause) { michael@0: sctp_m_freem(cause); michael@0: } michael@0: return; michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: SCTP_BUF_RESV_UF(mout, max_linkhdr); michael@0: #else michael@0: SCTP_BUF_RESV_UF(mout, SCTP_MAX_LINKHDR); michael@0: #endif michael@0: #else michael@0: SCTP_BUF_RESV_UF(mout, max_linkhdr); michael@0: #endif michael@0: SCTP_BUF_LEN(mout) = len; michael@0: SCTP_BUF_NEXT(mout) = cause; michael@0: #if defined(__FreeBSD__) michael@0: if (use_mflowid != 0) { michael@0: mout->m_pkthdr.flowid = mflowid; michael@0: mout->m_flags |= M_FLOWID; michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: ip = NULL; michael@0: #endif michael@0: #ifdef INET6 michael@0: ip6 = NULL; michael@0: #endif michael@0: switch (dst->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: src_sin = (struct sockaddr_in *)src; michael@0: dst_sin = (struct sockaddr_in *)dst; michael@0: ip = mtod(mout, struct ip *); michael@0: ip->ip_v = IPVERSION; michael@0: ip->ip_hl = (sizeof(struct ip) >> 2); michael@0: ip->ip_tos = 0; michael@0: #if defined(__FreeBSD__) michael@0: ip->ip_id = ip_newid(); michael@0: #elif defined(__APPLE__) michael@0: #if RANDOM_IP_ID michael@0: ip->ip_id = ip_randomid(); michael@0: #else michael@0: ip->ip_id = htons(ip_id++); michael@0: #endif michael@0: #else michael@0: ip->ip_id = htons(ip_id++); michael@0: #endif michael@0: ip->ip_off = 0; michael@0: ip->ip_ttl = MODULE_GLOBAL(ip_defttl); michael@0: if (port) { michael@0: ip->ip_p = IPPROTO_UDP; michael@0: } else { michael@0: ip->ip_p = IPPROTO_SCTP; michael@0: } michael@0: ip->ip_src.s_addr = dst_sin->sin_addr.s_addr; michael@0: ip->ip_dst.s_addr = src_sin->sin_addr.s_addr; michael@0: ip->ip_sum = 0; michael@0: len = sizeof(struct ip); michael@0: shout = (struct sctphdr *)((caddr_t)ip + len); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: src_sin6 = (struct sockaddr_in6 *)src; michael@0: dst_sin6 = (struct sockaddr_in6 *)dst; michael@0: ip6 = mtod(mout, struct ip6_hdr *); michael@0: ip6->ip6_flow = htonl(0x60000000); michael@0: #if defined(__FreeBSD__) michael@0: if (V_ip6_auto_flowlabel) { michael@0: ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: ip6->ip6_hlim = IPv6_HOP_LIMIT; michael@0: #else michael@0: ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); michael@0: #endif michael@0: if (port) { michael@0: ip6->ip6_nxt = IPPROTO_UDP; michael@0: } else { michael@0: ip6->ip6_nxt = IPPROTO_SCTP; michael@0: } michael@0: ip6->ip6_src = dst_sin6->sin6_addr; michael@0: ip6->ip6_dst = src_sin6->sin6_addr; michael@0: len = sizeof(struct ip6_hdr); michael@0: shout = (struct sctphdr *)((caddr_t)ip6 + len); michael@0: break; michael@0: #endif michael@0: default: michael@0: len = 0; michael@0: shout = mtod(mout, struct sctphdr *); michael@0: break; michael@0: } michael@0: if (port) { michael@0: if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { michael@0: sctp_m_freem(mout); michael@0: return; michael@0: } michael@0: udp = (struct udphdr *)shout; michael@0: udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); michael@0: udp->uh_dport = port; michael@0: udp->uh_sum = 0; michael@0: udp->uh_ulen = htons(sizeof(struct udphdr) + michael@0: sizeof(struct sctphdr) + michael@0: sizeof(struct sctp_chunkhdr) + michael@0: cause_len + padding_len); michael@0: len += sizeof(struct udphdr); michael@0: shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr)); michael@0: } else { michael@0: udp = NULL; michael@0: } michael@0: shout->src_port = sh->dest_port; michael@0: shout->dest_port = sh->src_port; michael@0: shout->checksum = 0; michael@0: if (vtag) { michael@0: shout->v_tag = htonl(vtag); michael@0: } else { michael@0: shout->v_tag = sh->v_tag; michael@0: } michael@0: len += sizeof(struct sctphdr); michael@0: ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr)); michael@0: ch->chunk_type = type; michael@0: if (vtag) { michael@0: ch->chunk_flags = 0; michael@0: } else { michael@0: ch->chunk_flags = SCTP_HAD_NO_TCB; michael@0: } michael@0: ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len); michael@0: len += sizeof(struct sctp_chunkhdr); michael@0: len += cause_len + padding_len; michael@0: michael@0: if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { michael@0: sctp_m_freem(mout); michael@0: return; michael@0: } michael@0: SCTP_ATTACH_CHAIN(o_pak, mout, len); michael@0: switch (dst->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #if defined(__APPLE__) || defined(__Panda__) michael@0: /* zap the stack pointer to the route */ michael@0: bzero(&ro, sizeof(sctp_route_t)); michael@0: #if defined(__Panda__) michael@0: ro._l_addr.sa.sa_family = AF_INET; michael@0: #endif michael@0: #endif michael@0: if (port) { michael@0: #if !defined(__Windows__) && !defined(__Userspace__) michael@0: #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) michael@0: if (V_udp_cksum) { michael@0: udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); michael@0: } else { michael@0: udp->uh_sum = 0; michael@0: } michael@0: #else michael@0: udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); michael@0: #endif michael@0: #else michael@0: udp->uh_sum = 0; michael@0: #endif michael@0: } michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version >= 1000000 michael@0: ip->ip_len = htons(len); michael@0: #else michael@0: ip->ip_len = len; michael@0: #endif michael@0: #elif defined(__APPLE__) || defined(__Userspace__) michael@0: ip->ip_len = len; michael@0: #else michael@0: ip->ip_len = htons(len); michael@0: #endif michael@0: if (port) { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) michael@0: if (V_udp_cksum) { michael@0: SCTP_ENABLE_UDP_CSUM(o_pak); michael@0: } michael@0: #else michael@0: SCTP_ENABLE_UDP_CSUM(o_pak); michael@0: #endif michael@0: } else { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: mout->m_pkthdr.csum_flags = CSUM_SCTP; michael@0: mout->m_pkthdr.csum_data = 0; michael@0: SCTP_STAT_INCR(sctps_sendhwcrc); michael@0: #else michael@0: shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #endif michael@0: } michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { michael@0: sctp_packet_log(o_pak); michael@0: } michael@0: #endif michael@0: #if defined(__APPLE__) || defined(__Panda__) michael@0: SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); michael@0: /* Free the route if we got one back */ michael@0: if (ro.ro_rt) { michael@0: RTFREE(ro.ro_rt); michael@0: ro.ro_rt = NULL; michael@0: } michael@0: #else michael@0: SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id); michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: ip6->ip6_plen = len - sizeof(struct ip6_hdr); michael@0: if (port) { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #if defined(__Windows__) michael@0: udp->uh_sum = 0; michael@0: #elif !defined(__Userspace__) michael@0: if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) { michael@0: udp->uh_sum = 0xffff; michael@0: } michael@0: #endif michael@0: } else { michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 900000 michael@0: #if __FreeBSD_version > 901000 michael@0: mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; michael@0: #else michael@0: mout->m_pkthdr.csum_flags = CSUM_SCTP; michael@0: #endif michael@0: mout->m_pkthdr.csum_data = 0; michael@0: SCTP_STAT_INCR(sctps_sendhwcrc); michael@0: #else michael@0: shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr)); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #endif michael@0: } michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { michael@0: sctp_packet_log(o_pak); michael@0: } michael@0: #endif michael@0: SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: char *buffer; michael@0: struct sockaddr_conn *sconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)src; michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_sendnocrc); michael@0: #else michael@0: shout->checksum = sctp_calculate_cksum(mout, 0); michael@0: SCTP_STAT_INCR(sctps_sendswcrc); michael@0: #endif michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { michael@0: sctp_packet_log(mout); michael@0: } michael@0: #endif michael@0: /* Don't alloc/free for each packet */ michael@0: if ((buffer = malloc(len)) != NULL) { michael@0: m_copydata(mout, 0, len, buffer); michael@0: SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, len, 0, 0); michael@0: free(buffer); michael@0: } michael@0: sctp_m_freem(mout); michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", michael@0: dst->sa_family); michael@0: sctp_m_freem(mout); michael@0: SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: return; michael@0: } michael@0: SCTP_STAT_INCR(sctps_sendpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: michael@0: void michael@0: sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net,int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_heartbeat_chunk *hb; michael@0: struct timeval now; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (net == NULL) { michael@0: return; michael@0: } michael@0: (void)SCTP_GETTIME_TIMEVAL(&now); michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: break; michael@0: #endif michael@0: default: michael@0: return; michael@0: } michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n"); michael@0: return; michael@0: } michael@0: michael@0: chk->copy_by_ref = 0; michael@0: chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->send_size = sizeof(struct sctp_heartbeat_chunk); michael@0: michael@0: chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (chk->data == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: /* Now we have a mbuf that we can fill in with the details */ michael@0: hb = mtod(chk->data, struct sctp_heartbeat_chunk *); michael@0: memset(hb, 0, sizeof(struct sctp_heartbeat_chunk)); michael@0: /* fill out chunk header */ michael@0: hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST; michael@0: hb->ch.chunk_flags = 0; michael@0: hb->ch.chunk_length = htons(chk->send_size); michael@0: /* Fill out hb parameter */ michael@0: hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO); michael@0: hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param)); michael@0: hb->heartbeat.hb_info.time_value_1 = now.tv_sec; michael@0: hb->heartbeat.hb_info.time_value_2 = now.tv_usec; michael@0: /* Did our user request this one, put it in */ michael@0: hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family; michael@0: #ifdef HAVE_SA_LEN michael@0: hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len; michael@0: #else michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in6); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_conn); michael@0: break; michael@0: #endif michael@0: default: michael@0: hb->heartbeat.hb_info.addr_len = 0; michael@0: break; michael@0: } michael@0: #endif michael@0: if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { michael@0: /* michael@0: * we only take from the entropy pool if the address is not michael@0: * confirmed. michael@0: */ michael@0: net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); michael@0: net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); michael@0: } else { michael@0: net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0; michael@0: net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0; michael@0: } michael@0: switch (net->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: memcpy(hb->heartbeat.hb_info.address, michael@0: &net->ro._l_addr.sin.sin_addr, michael@0: sizeof(net->ro._l_addr.sin.sin_addr)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: memcpy(hb->heartbeat.hb_info.address, michael@0: &net->ro._l_addr.sin6.sin6_addr, michael@0: sizeof(net->ro._l_addr.sin6.sin6_addr)); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: memcpy(hb->heartbeat.hb_info.address, michael@0: &net->ro._l_addr.sconn.sconn_addr, michael@0: sizeof(net->ro._l_addr.sconn.sconn_addr)); michael@0: break; michael@0: #endif michael@0: default: michael@0: return; michael@0: break; michael@0: } michael@0: net->hb_responded = 0; michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); michael@0: stcb->asoc.ctrl_queue_cnt++; michael@0: SCTP_STAT_INCR(sctps_sendheartbeat); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net, michael@0: uint32_t high_tsn) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_ecne_chunk *ecne; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: if (net == NULL) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) { michael@0: /* found a previous ECN_ECHO update it if needed */ michael@0: uint32_t cnt, ctsn; michael@0: ecne = mtod(chk->data, struct sctp_ecne_chunk *); michael@0: ctsn = ntohl(ecne->tsn); michael@0: if (SCTP_TSN_GT(high_tsn, ctsn)) { michael@0: ecne->tsn = htonl(high_tsn); michael@0: SCTP_STAT_INCR(sctps_queue_upd_ecne); michael@0: } michael@0: cnt = ntohl(ecne->num_pkts_since_cwr); michael@0: cnt++; michael@0: ecne->num_pkts_since_cwr = htonl(cnt); michael@0: return; michael@0: } michael@0: } michael@0: /* nope could not find one to update so we must build one */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: SCTP_STAT_INCR(sctps_queue_upd_ecne); michael@0: chk->rec.chunk_id.id = SCTP_ECN_ECHO; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->send_size = sizeof(struct sctp_ecne_chunk); michael@0: chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (chk->data == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: michael@0: stcb->asoc.ecn_echo_cnt_onq++; michael@0: ecne = mtod(chk->data, struct sctp_ecne_chunk *); michael@0: ecne->ch.chunk_type = SCTP_ECN_ECHO; michael@0: ecne->ch.chunk_flags = 0; michael@0: ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk)); michael@0: ecne->tsn = htonl(high_tsn); michael@0: ecne->num_pkts_since_cwr = htonl(1); michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next); michael@0: asoc->ctrl_queue_cnt++; michael@0: } michael@0: michael@0: void michael@0: sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net, michael@0: struct mbuf *m, int len, int iphlen, int bad_crc) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_pktdrop_chunk *drp; michael@0: struct sctp_tmit_chunk *chk; michael@0: uint8_t *datap; michael@0: int was_trunc = 0; michael@0: int fullsz = 0; michael@0: long spc; michael@0: int offset; michael@0: struct sctp_chunkhdr *ch, chunk_buf; michael@0: unsigned int chk_length; michael@0: michael@0: if (!stcb) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (asoc->peer_supports_pktdrop == 0) { michael@0: /*- michael@0: * peer must declare support before I send one. michael@0: */ michael@0: return; michael@0: } michael@0: if (stcb->sctp_socket == NULL) { michael@0: return; michael@0: } michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: len -= iphlen; michael@0: chk->send_size = len; michael@0: /* Validate that we do not have an ABORT in here. */ michael@0: offset = iphlen + sizeof(struct sctphdr); michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, michael@0: sizeof(*ch), (uint8_t *) & chunk_buf); michael@0: while (ch != NULL) { michael@0: chk_length = ntohs(ch->chunk_length); michael@0: if (chk_length < sizeof(*ch)) { michael@0: /* break to abort land */ michael@0: break; michael@0: } michael@0: switch (ch->chunk_type) { michael@0: case SCTP_PACKET_DROPPED: michael@0: case SCTP_ABORT_ASSOCIATION: michael@0: case SCTP_INITIATION_ACK: michael@0: /** michael@0: * We don't respond with an PKT-DROP to an ABORT michael@0: * or PKT-DROP. We also do not respond to an michael@0: * INIT-ACK, because we can't know if the initiation michael@0: * tag is correct or not. michael@0: */ michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: default: michael@0: break; michael@0: } michael@0: offset += SCTP_SIZE32(chk_length); michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, michael@0: sizeof(*ch), (uint8_t *) & chunk_buf); michael@0: } michael@0: michael@0: if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) > michael@0: min(stcb->asoc.smallest_mtu, MCLBYTES)) { michael@0: /* only send 1 mtu worth, trim off the michael@0: * excess on the end. michael@0: */ michael@0: fullsz = len; michael@0: len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD; michael@0: was_trunc = 1; michael@0: } michael@0: chk->asoc = &stcb->asoc; michael@0: chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (chk->data == NULL) { michael@0: jump_out: michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: drp = mtod(chk->data, struct sctp_pktdrop_chunk *); michael@0: if (drp == NULL) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: goto jump_out; michael@0: } michael@0: chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) + michael@0: sizeof(struct sctphdr) + SCTP_MED_OVERHEAD)); michael@0: chk->book_size_scale = 0; michael@0: if (was_trunc) { michael@0: drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED; michael@0: drp->trunc_len = htons(fullsz); michael@0: /* Len is already adjusted to size minus overhead above michael@0: * take out the pkt_drop chunk itself from it. michael@0: */ michael@0: chk->send_size = len - sizeof(struct sctp_pktdrop_chunk); michael@0: len = chk->send_size; michael@0: } else { michael@0: /* no truncation needed */ michael@0: drp->ch.chunk_flags = 0; michael@0: drp->trunc_len = htons(0); michael@0: } michael@0: if (bad_crc) { michael@0: drp->ch.chunk_flags |= SCTP_BADCRC; michael@0: } michael@0: chk->send_size += sizeof(struct sctp_pktdrop_chunk); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: if (net) { michael@0: /* we should hit here */ michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: } else { michael@0: chk->whoTo = NULL; michael@0: } michael@0: chk->rec.chunk_id.id = SCTP_PACKET_DROPPED; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: drp->ch.chunk_type = SCTP_PACKET_DROPPED; michael@0: drp->ch.chunk_length = htons(chk->send_size); michael@0: spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket); michael@0: if (spc < 0) { michael@0: spc = 0; michael@0: } michael@0: drp->bottle_bw = htonl(spc); michael@0: if (asoc->my_rwnd) { michael@0: drp->current_onq = htonl(asoc->size_on_reasm_queue + michael@0: asoc->size_on_all_streams + michael@0: asoc->my_rwnd_control_len + michael@0: stcb->sctp_socket->so_rcv.sb_cc); michael@0: } else { michael@0: /*- michael@0: * If my rwnd is 0, possibly from mbuf depletion as well as michael@0: * space used, tell the peer there is NO space aka onq == bw michael@0: */ michael@0: drp->current_onq = htonl(spc); michael@0: } michael@0: drp->reserved = 0; michael@0: datap = drp->data; michael@0: m_copydata(m, iphlen, len, (caddr_t)datap); michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); michael@0: asoc->ctrl_queue_cnt++; michael@0: } michael@0: michael@0: void michael@0: sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_cwr_chunk *cwr; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (net == NULL) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { michael@0: if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) { michael@0: /* found a previous CWR queued to same destination update it if needed */ michael@0: uint32_t ctsn; michael@0: cwr = mtod(chk->data, struct sctp_cwr_chunk *); michael@0: ctsn = ntohl(cwr->tsn); michael@0: if (SCTP_TSN_GT(high_tsn, ctsn)) { michael@0: cwr->tsn = htonl(high_tsn); michael@0: } michael@0: if (override & SCTP_CWR_REDUCE_OVERRIDE) { michael@0: /* Make sure override is carried */ michael@0: cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE; michael@0: } michael@0: return; michael@0: } michael@0: } michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: return; michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->rec.chunk_id.id = SCTP_ECN_CWR; michael@0: chk->rec.chunk_id.can_take_data = 1; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->send_size = sizeof(struct sctp_cwr_chunk); michael@0: chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (chk->data == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return; michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->whoTo = net; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: cwr = mtod(chk->data, struct sctp_cwr_chunk *); michael@0: cwr->ch.chunk_type = SCTP_ECN_CWR; michael@0: cwr->ch.chunk_flags = override; michael@0: cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk)); michael@0: cwr->tsn = htonl(high_tsn); michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); michael@0: asoc->ctrl_queue_cnt++; michael@0: } michael@0: michael@0: void michael@0: sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk, michael@0: int number_entries, uint16_t * list, michael@0: uint32_t seq, uint32_t resp_seq, uint32_t last_sent) michael@0: { michael@0: uint16_t len, old_len, i; michael@0: struct sctp_stream_reset_out_request *req_out; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries)); michael@0: req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST); michael@0: req_out->ph.param_length = htons(len); michael@0: req_out->request_seq = htonl(seq); michael@0: req_out->response_seq = htonl(resp_seq); michael@0: req_out->send_reset_at_tsn = htonl(last_sent); michael@0: if (number_entries) { michael@0: for (i = 0; i < number_entries; i++) { michael@0: req_out->list_of_streams[i] = htons(list[i]); michael@0: } michael@0: } michael@0: if (SCTP_SIZE32(len) > len) { michael@0: /*- michael@0: * Need to worry about the pad we may end up adding to the michael@0: * end. This is easy since the struct is either aligned to 4 michael@0: * bytes or 2 bytes off. michael@0: */ michael@0: req_out->list_of_streams[number_entries] = 0; michael@0: } michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->book_size = len + old_len; michael@0: chk->book_size_scale = 0; michael@0: chk->send_size = SCTP_SIZE32(chk->book_size); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk, michael@0: int number_entries, uint16_t *list, michael@0: uint32_t seq) michael@0: { michael@0: uint16_t len, old_len, i; michael@0: struct sctp_stream_reset_in_request *req_in; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries)); michael@0: req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST); michael@0: req_in->ph.param_length = htons(len); michael@0: req_in->request_seq = htonl(seq); michael@0: if (number_entries) { michael@0: for (i = 0; i < number_entries; i++) { michael@0: req_in->list_of_streams[i] = htons(list[i]); michael@0: } michael@0: } michael@0: if (SCTP_SIZE32(len) > len) { michael@0: /*- michael@0: * Need to worry about the pad we may end up adding to the michael@0: * end. This is easy since the struct is either aligned to 4 michael@0: * bytes or 2 bytes off. michael@0: */ michael@0: req_in->list_of_streams[number_entries] = 0; michael@0: } michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->book_size = len + old_len; michael@0: chk->book_size_scale = 0; michael@0: chk->send_size = SCTP_SIZE32(chk->book_size); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk, michael@0: uint32_t seq) michael@0: { michael@0: uint16_t len, old_len; michael@0: struct sctp_stream_reset_tsn_request *req_tsn; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = sizeof(struct sctp_stream_reset_tsn_request); michael@0: req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST); michael@0: req_tsn->ph.param_length = htons(len); michael@0: req_tsn->request_seq = htonl(seq); michael@0: michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->send_size = len + old_len; michael@0: chk->book_size = SCTP_SIZE32(chk->send_size); michael@0: chk->book_size_scale = 0; michael@0: SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk, michael@0: uint32_t resp_seq, uint32_t result) michael@0: { michael@0: uint16_t len, old_len; michael@0: struct sctp_stream_reset_response *resp; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = sizeof(struct sctp_stream_reset_response); michael@0: resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); michael@0: resp->ph.param_length = htons(len); michael@0: resp->response_seq = htonl(resp_seq); michael@0: resp->result = ntohl(result); michael@0: michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->book_size = len + old_len; michael@0: chk->book_size_scale = 0; michael@0: chk->send_size = SCTP_SIZE32(chk->book_size); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk, michael@0: uint32_t resp_seq, uint32_t result, michael@0: uint32_t send_una, uint32_t recv_next) michael@0: { michael@0: uint16_t len, old_len; michael@0: struct sctp_stream_reset_response_tsn *resp; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = sizeof(struct sctp_stream_reset_response_tsn); michael@0: resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); michael@0: resp->ph.param_length = htons(len); michael@0: resp->response_seq = htonl(resp_seq); michael@0: resp->result = htonl(result); michael@0: resp->senders_next_tsn = htonl(send_una); michael@0: resp->receivers_next_tsn = htonl(recv_next); michael@0: michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->book_size = len + old_len; michael@0: chk->send_size = SCTP_SIZE32(chk->book_size); michael@0: chk->book_size_scale = 0; michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: sctp_add_an_out_stream(struct sctp_tmit_chunk *chk, michael@0: uint32_t seq, michael@0: uint16_t adding) michael@0: { michael@0: uint16_t len, old_len; michael@0: struct sctp_chunkhdr *ch; michael@0: struct sctp_stream_reset_add_strm *addstr; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = sizeof(struct sctp_stream_reset_add_strm); michael@0: michael@0: /* Fill it out. */ michael@0: addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS); michael@0: addstr->ph.param_length = htons(len); michael@0: addstr->request_seq = htonl(seq); michael@0: addstr->number_of_streams = htons(adding); michael@0: addstr->reserved = 0; michael@0: michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->send_size = len + old_len; michael@0: chk->book_size = SCTP_SIZE32(chk->send_size); michael@0: chk->book_size_scale = 0; michael@0: SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: sctp_add_an_in_stream(struct sctp_tmit_chunk *chk, michael@0: uint32_t seq, michael@0: uint16_t adding) michael@0: { michael@0: uint16_t len, old_len; michael@0: struct sctp_chunkhdr *ch; michael@0: struct sctp_stream_reset_add_strm *addstr; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); michael@0: michael@0: /* get to new offset for the param. */ michael@0: addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); michael@0: /* now how long will this param be? */ michael@0: len = sizeof(struct sctp_stream_reset_add_strm); michael@0: /* Fill it out. */ michael@0: addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS); michael@0: addstr->ph.param_length = htons(len); michael@0: addstr->request_seq = htonl(seq); michael@0: addstr->number_of_streams = htons(adding); michael@0: addstr->reserved = 0; michael@0: michael@0: /* now fix the chunk length */ michael@0: ch->chunk_length = htons(len + old_len); michael@0: chk->send_size = len + old_len; michael@0: chk->book_size = SCTP_SIZE32(chk->send_size); michael@0: chk->book_size_scale = 0; michael@0: SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); michael@0: return; michael@0: } michael@0: michael@0: int michael@0: sctp_send_str_reset_req(struct sctp_tcb *stcb, michael@0: int number_entries, uint16_t *list, michael@0: uint8_t send_out_req, michael@0: uint8_t send_in_req, michael@0: uint8_t send_tsn_req, michael@0: uint8_t add_stream, michael@0: uint16_t adding_o, michael@0: uint16_t adding_i, uint8_t peer_asked) michael@0: { michael@0: michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_chunkhdr *ch; michael@0: uint32_t seq; michael@0: michael@0: asoc = &stcb->asoc; michael@0: if (asoc->stream_reset_outstanding) { michael@0: /*- michael@0: * Already one pending, must get ACK back to clear the flag. michael@0: */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY); michael@0: return (EBUSY); michael@0: } michael@0: if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) && michael@0: (add_stream == 0)) { michael@0: /* nothing to do */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if (send_tsn_req && (send_out_req || send_in_req)) { michael@0: /* error, can't do that */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: chk->copy_by_ref = 0; michael@0: chk->rec.chunk_id.id = SCTP_STREAM_RESET; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->book_size = sizeof(struct sctp_chunkhdr); michael@0: chk->send_size = SCTP_SIZE32(chk->book_size); michael@0: chk->book_size_scale = 0; michael@0: michael@0: chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (chk->data == NULL) { michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: michael@0: /* setup chunk parameters */ michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: if (stcb->asoc.alternate) { michael@0: chk->whoTo = stcb->asoc.alternate; michael@0: } else { michael@0: chk->whoTo = stcb->asoc.primary_destination; michael@0: } michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: ch->chunk_type = SCTP_STREAM_RESET; michael@0: ch->chunk_flags = 0; michael@0: ch->chunk_length = htons(chk->book_size); michael@0: SCTP_BUF_LEN(chk->data) = chk->send_size; michael@0: michael@0: seq = stcb->asoc.str_reset_seq_out; michael@0: if (send_out_req) { michael@0: sctp_add_stream_reset_out(chk, number_entries, list, michael@0: seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1)); michael@0: asoc->stream_reset_out_is_outstanding = 1; michael@0: seq++; michael@0: asoc->stream_reset_outstanding++; michael@0: } michael@0: if ((add_stream & 1) && michael@0: ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) { michael@0: /* Need to allocate more */ michael@0: struct sctp_stream_out *oldstream; michael@0: struct sctp_stream_queue_pending *sp, *nsp; michael@0: int i; michael@0: michael@0: oldstream = stcb->asoc.strmout; michael@0: /* get some more */ michael@0: SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *, michael@0: ((stcb->asoc.streamoutcnt+adding_o) * sizeof(struct sctp_stream_out)), michael@0: SCTP_M_STRMO); michael@0: if (stcb->asoc.strmout == NULL) { michael@0: uint8_t x; michael@0: stcb->asoc.strmout = oldstream; michael@0: /* Turn off the bit */ michael@0: x = add_stream & 0xfe; michael@0: add_stream = x; michael@0: goto skip_stuff; michael@0: } michael@0: /* Ok now we proceed with copying the old out stuff and michael@0: * initializing the new stuff. michael@0: */ michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1); michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); michael@0: stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues; michael@0: stcb->asoc.strmout[i].next_sequence_send = oldstream[i].next_sequence_send; michael@0: stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete; michael@0: stcb->asoc.strmout[i].stream_no = i; michael@0: stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]); michael@0: /* now anything on those queues? */ michael@0: TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) { michael@0: TAILQ_REMOVE(&oldstream[i].outqueue, sp, next); michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next); michael@0: } michael@0: /* Now move assoc pointers too */ michael@0: if (stcb->asoc.last_out_stream == &oldstream[i]) { michael@0: stcb->asoc.last_out_stream = &stcb->asoc.strmout[i]; michael@0: } michael@0: if (stcb->asoc.locked_on_sending == &oldstream[i]) { michael@0: stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i]; michael@0: } michael@0: } michael@0: /* now the new streams */ michael@0: stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1); michael@0: for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) { michael@0: TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); michael@0: stcb->asoc.strmout[i].chunks_on_queues = 0; michael@0: stcb->asoc.strmout[i].next_sequence_send = 0x0; michael@0: stcb->asoc.strmout[i].stream_no = i; michael@0: stcb->asoc.strmout[i].last_msg_incomplete = 0; michael@0: stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL); michael@0: } michael@0: stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o; michael@0: SCTP_FREE(oldstream, SCTP_M_STRMO); michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: skip_stuff: michael@0: if ((add_stream & 1) && (adding_o > 0)) { michael@0: asoc->strm_pending_add_size = adding_o; michael@0: asoc->peer_req_out = peer_asked; michael@0: sctp_add_an_out_stream(chk, seq, adding_o); michael@0: seq++; michael@0: asoc->stream_reset_outstanding++; michael@0: } michael@0: if ((add_stream & 2) && (adding_i > 0)) { michael@0: sctp_add_an_in_stream(chk, seq, adding_i); michael@0: seq++; michael@0: asoc->stream_reset_outstanding++; michael@0: } michael@0: if (send_in_req) { michael@0: sctp_add_stream_reset_in(chk, number_entries, list, seq); michael@0: seq++; michael@0: asoc->stream_reset_outstanding++; michael@0: } michael@0: if (send_tsn_req) { michael@0: sctp_add_stream_reset_tsn(chk, seq); michael@0: asoc->stream_reset_outstanding++; michael@0: } michael@0: asoc->str_reset = chk; michael@0: /* insert the chunk for sending */ michael@0: TAILQ_INSERT_TAIL(&asoc->control_send_queue, michael@0: chk, michael@0: sctp_next); michael@0: asoc->ctrl_queue_cnt++; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: /* Don't respond to an ABORT with an ABORT. */ michael@0: if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) { michael@0: if (cause) michael@0: sctp_m_freem(cause); michael@0: return; michael@0: } michael@0: sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return; michael@0: } michael@0: michael@0: static struct mbuf * michael@0: sctp_copy_resume(struct uio *uio, michael@0: int max_send_len, michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 602000 michael@0: int user_marks_eor, michael@0: #endif michael@0: int *error, michael@0: uint32_t *sndout, michael@0: struct mbuf **new_tail) michael@0: { michael@0: #if defined(__Panda__) michael@0: struct mbuf *m; michael@0: michael@0: m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, michael@0: (user_marks_eor ? M_EOR : 0)); michael@0: if (m == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: *error = ENOMEM; michael@0: } else { michael@0: *sndout = m_length(m, NULL); michael@0: *new_tail = m_last(m); michael@0: } michael@0: return (m); michael@0: #elif defined(__FreeBSD__) && __FreeBSD_version > 602000 michael@0: struct mbuf *m; michael@0: michael@0: m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, michael@0: (M_PKTHDR | (user_marks_eor ? M_EOR : 0))); michael@0: if (m == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: *error = ENOMEM; michael@0: } else { michael@0: *sndout = m_length(m, NULL); michael@0: *new_tail = m_last(m); michael@0: } michael@0: return (m); michael@0: #else michael@0: int left, cancpy, willcpy; michael@0: struct mbuf *m, *head; michael@0: michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: left = min(uio->uio_resid, max_send_len); michael@0: #else michael@0: left = min(uio_resid(uio), max_send_len); michael@0: #endif michael@0: #else michael@0: left = min(uio->uio_resid, max_send_len); michael@0: #endif michael@0: /* Always get a header just in case */ michael@0: head = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); michael@0: if (head == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: *error = ENOMEM; michael@0: return (NULL); michael@0: } michael@0: cancpy = M_TRAILINGSPACE(head); michael@0: willcpy = min(cancpy, left); michael@0: *error = uiomove(mtod(head, caddr_t), willcpy, uio); michael@0: if (*error) { michael@0: sctp_m_freem(head); michael@0: return (NULL); michael@0: } michael@0: *sndout += willcpy; michael@0: left -= willcpy; michael@0: SCTP_BUF_LEN(head) = willcpy; michael@0: m = head; michael@0: *new_tail = head; michael@0: while (left > 0) { michael@0: /* move in user data */ michael@0: SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: sctp_m_freem(head); michael@0: *new_tail = NULL; michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: *error = ENOMEM; michael@0: return (NULL); michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: cancpy = M_TRAILINGSPACE(m); michael@0: willcpy = min(cancpy, left); michael@0: *error = uiomove(mtod(m, caddr_t), willcpy, uio); michael@0: if (*error) { michael@0: sctp_m_freem(head); michael@0: *new_tail = NULL; michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: *error = EFAULT; michael@0: return (NULL); michael@0: } michael@0: SCTP_BUF_LEN(m) = willcpy; michael@0: left -= willcpy; michael@0: *sndout += willcpy; michael@0: *new_tail = m; michael@0: if (left == 0) { michael@0: SCTP_BUF_NEXT(m) = NULL; michael@0: } michael@0: } michael@0: return (head); michael@0: #endif michael@0: } michael@0: michael@0: static int michael@0: sctp_copy_one(struct sctp_stream_queue_pending *sp, michael@0: struct uio *uio, michael@0: int resv_upfront) michael@0: { michael@0: int left; michael@0: #if defined(__Panda__) michael@0: left = sp->length; michael@0: sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, michael@0: resv_upfront, 0); michael@0: if (sp->data == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: michael@0: sp->tail_mbuf = m_last(sp->data); michael@0: return (0); michael@0: michael@0: #elif defined(__FreeBSD__) && __FreeBSD_version > 602000 michael@0: left = sp->length; michael@0: sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, michael@0: resv_upfront, 0); michael@0: if (sp->data == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: michael@0: sp->tail_mbuf = m_last(sp->data); michael@0: return (0); michael@0: #else michael@0: int cancpy, willcpy, error; michael@0: struct mbuf *m, *head; michael@0: int cpsz = 0; michael@0: michael@0: /* First one gets a header */ michael@0: left = sp->length; michael@0: head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 0, M_WAITOK, 0, MT_DATA); michael@0: if (m == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: /*- michael@0: * Add this one for m in now, that way if the alloc fails we won't michael@0: * have a bad cnt. michael@0: */ michael@0: SCTP_BUF_RESV_UF(m, resv_upfront); michael@0: cancpy = M_TRAILINGSPACE(m); michael@0: willcpy = min(cancpy, left); michael@0: while (left > 0) { michael@0: /* move in user data */ michael@0: error = uiomove(mtod(m, caddr_t), willcpy, uio); michael@0: if (error) { michael@0: sctp_m_freem(head); michael@0: return (error); michael@0: } michael@0: SCTP_BUF_LEN(m) = willcpy; michael@0: left -= willcpy; michael@0: cpsz += willcpy; michael@0: if (left > 0) { michael@0: SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); michael@0: if (SCTP_BUF_NEXT(m) == NULL) { michael@0: /* michael@0: * the head goes back to caller, he can free michael@0: * the rest michael@0: */ michael@0: sctp_m_freem(head); michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: cancpy = M_TRAILINGSPACE(m); michael@0: willcpy = min(cancpy, left); michael@0: } else { michael@0: sp->tail_mbuf = m; michael@0: SCTP_BUF_NEXT(m) = NULL; michael@0: } michael@0: } michael@0: sp->data = head; michael@0: sp->length = cpsz; michael@0: return (0); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: michael@0: static struct sctp_stream_queue_pending * michael@0: sctp_copy_it_in(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: struct sctp_sndrcvinfo *srcv, michael@0: struct uio *uio, michael@0: struct sctp_nets *net, michael@0: int max_send_len, michael@0: int user_marks_eor, michael@0: int *error) michael@0: michael@0: { michael@0: /*- michael@0: * This routine must be very careful in its work. Protocol michael@0: * processing is up and running so care must be taken to spl...() michael@0: * when you need to do something that may effect the stcb/asoc. The michael@0: * sb is locked however. When data is copied the protocol processing michael@0: * should be enabled since this is a slower operation... michael@0: */ michael@0: struct sctp_stream_queue_pending *sp = NULL; michael@0: int resv_in_first; michael@0: michael@0: *error = 0; michael@0: /* Now can we send this? */ michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || michael@0: (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { michael@0: /* got data while shutting down */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); michael@0: *error = ECONNRESET; michael@0: goto out_now; michael@0: } michael@0: sctp_alloc_a_strmoq(stcb, sp); michael@0: if (sp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: *error = ENOMEM; michael@0: goto out_now; michael@0: } michael@0: sp->act_flags = 0; michael@0: sp->sender_all_done = 0; michael@0: sp->sinfo_flags = srcv->sinfo_flags; michael@0: sp->timetolive = srcv->sinfo_timetolive; michael@0: sp->ppid = srcv->sinfo_ppid; michael@0: sp->context = srcv->sinfo_context; michael@0: (void)SCTP_GETTIME_TIMEVAL(&sp->ts); michael@0: michael@0: sp->stream = srcv->sinfo_stream; michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: sp->length = min(uio->uio_resid, max_send_len); michael@0: #else michael@0: sp->length = min(uio_resid(uio), max_send_len); michael@0: #endif michael@0: #else michael@0: sp->length = min(uio->uio_resid, max_send_len); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if ((sp->length == (uint32_t)uio->uio_resid) && michael@0: #else michael@0: if ((sp->length == (uint32_t)uio_resid(uio)) && michael@0: #endif michael@0: #else michael@0: if ((sp->length == (uint32_t)uio->uio_resid) && michael@0: #endif michael@0: ((user_marks_eor == 0) || michael@0: (srcv->sinfo_flags & SCTP_EOF) || michael@0: (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { michael@0: sp->msg_is_complete = 1; michael@0: } else { michael@0: sp->msg_is_complete = 0; michael@0: } michael@0: sp->sender_all_done = 0; michael@0: sp->some_taken = 0; michael@0: sp->put_last_out = 0; michael@0: resv_in_first = sizeof(struct sctp_data_chunk); michael@0: sp->data = sp->tail_mbuf = NULL; michael@0: if (sp->length == 0) { michael@0: *error = 0; michael@0: goto skip_copy; michael@0: } michael@0: if (srcv->sinfo_keynumber_valid) { michael@0: sp->auth_keyid = srcv->sinfo_keynumber; michael@0: } else { michael@0: sp->auth_keyid = stcb->asoc.authinfo.active_keyid; michael@0: } michael@0: if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { michael@0: sctp_auth_key_acquire(stcb, sp->auth_keyid); michael@0: sp->holds_key_ref = 1; michael@0: } michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(SCTP_INP_SO(stcb->sctp_ep), 0); michael@0: #endif michael@0: *error = sctp_copy_one(sp, uio, resv_in_first); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(SCTP_INP_SO(stcb->sctp_ep), 0); michael@0: #endif michael@0: skip_copy: michael@0: if (*error) { michael@0: sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); michael@0: sp = NULL; michael@0: } else { michael@0: if (sp->sinfo_flags & SCTP_ADDR_OVER) { michael@0: sp->net = net; michael@0: atomic_add_int(&sp->net->ref_count, 1); michael@0: } else { michael@0: sp->net = NULL; michael@0: } michael@0: sctp_set_prsctp_policy(sp); michael@0: } michael@0: out_now: michael@0: return (sp); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_sosend(struct socket *so, michael@0: struct sockaddr *addr, michael@0: struct uio *uio, michael@0: #ifdef __Panda__ michael@0: pakhandle_type top, michael@0: pakhandle_type icontrol, michael@0: #else michael@0: struct mbuf *top, michael@0: struct mbuf *control, michael@0: #endif michael@0: #if defined(__APPLE__) || defined(__Panda__) michael@0: int flags michael@0: #else michael@0: int flags, michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: struct thread *p michael@0: #elif defined(__Windows__) michael@0: PKTHREAD p michael@0: #else michael@0: #if defined(__Userspace__) michael@0: /* michael@0: * proc is a dummy in __Userspace__ and will not be passed michael@0: * to sctp_lower_sosend michael@0: */ michael@0: #endif michael@0: struct proc *p michael@0: #endif michael@0: #endif michael@0: ) michael@0: { michael@0: #ifdef __Panda__ michael@0: struct mbuf *control = NULL; michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: struct proc *p = current_proc(); michael@0: #endif michael@0: int error, use_sndinfo = 0; michael@0: struct sctp_sndrcvinfo sndrcvninfo; michael@0: struct sockaddr *addr_to_use; michael@0: #if defined(INET) && defined(INET6) michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: #endif michael@0: #ifdef __Panda__ michael@0: control = SCTP_HEADER_TO_CHAIN(icontrol); michael@0: #endif michael@0: if (control) { michael@0: /* process cmsg snd/rcv info (maybe a assoc-id) */ michael@0: if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, michael@0: sizeof(sndrcvninfo))) { michael@0: /* got one */ michael@0: use_sndinfo = 1; michael@0: } michael@0: } michael@0: addr_to_use = addr; michael@0: #if defined(INET) && defined(INET6) michael@0: if ((addr) && (addr->sa_family == AF_INET6)) { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)addr; michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: in6_sin6_2_sin(&sin, sin6); michael@0: addr_to_use = (struct sockaddr *)&sin; michael@0: } michael@0: } michael@0: #endif michael@0: error = sctp_lower_sosend(so, addr_to_use, uio, top, michael@0: #ifdef __Panda__ michael@0: icontrol, michael@0: #else michael@0: control, michael@0: #endif michael@0: flags, michael@0: use_sndinfo ? &sndrcvninfo: NULL michael@0: #if !(defined(__Panda__) || defined(__Userspace__)) michael@0: , p michael@0: #endif michael@0: ); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_lower_sosend(struct socket *so, michael@0: struct sockaddr *addr, michael@0: struct uio *uio, michael@0: #ifdef __Panda__ michael@0: pakhandle_type i_pak, michael@0: pakhandle_type i_control, michael@0: #else michael@0: struct mbuf *i_pak, michael@0: struct mbuf *control, michael@0: #endif michael@0: int flags, michael@0: struct sctp_sndrcvinfo *srcv michael@0: #if !(defined( __Panda__) || defined(__Userspace__)) michael@0: , michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: struct thread *p michael@0: #elif defined(__Windows__) michael@0: PKTHREAD p michael@0: #else michael@0: struct proc *p michael@0: #endif michael@0: #endif michael@0: ) michael@0: { michael@0: unsigned int sndlen = 0, max_len; michael@0: int error, len; michael@0: struct mbuf *top = NULL; michael@0: #ifdef __Panda__ michael@0: struct mbuf *control = NULL; michael@0: #endif michael@0: int queue_only = 0, queue_only_for_init = 0; michael@0: int free_cnt_applied = 0; michael@0: int un_sent; michael@0: int now_filled = 0; michael@0: unsigned int inqueue_bytes = 0; michael@0: struct sctp_block_entry be; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb = NULL; michael@0: struct timeval now; michael@0: struct sctp_nets *net; michael@0: struct sctp_association *asoc; michael@0: struct sctp_inpcb *t_inp; michael@0: int user_marks_eor; michael@0: int create_lock_applied = 0; michael@0: int nagle_applies = 0; michael@0: int some_on_control = 0; michael@0: int got_all_of_the_send = 0; michael@0: int hold_tcblock = 0; michael@0: int non_blocking = 0; michael@0: uint32_t local_add_more, local_soresv = 0; michael@0: uint16_t port; michael@0: uint16_t sinfo_flags; michael@0: sctp_assoc_t sinfo_assoc_id; michael@0: michael@0: error = 0; michael@0: net = NULL; michael@0: stcb = NULL; michael@0: asoc = NULL; michael@0: michael@0: #if defined(__APPLE__) michael@0: sctp_lock_assert(so); michael@0: #endif michael@0: t_inp = inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: if (i_pak) { michael@0: SCTP_RELEASE_PKT(i_pak); michael@0: } michael@0: return (error); michael@0: } michael@0: if ((uio == NULL) && (i_pak == NULL)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); michael@0: atomic_add_int(&inp->total_sends, 1); michael@0: if (uio) { michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if (uio->uio_resid < 0) { michael@0: #else michael@0: if (uio_resid(uio) < 0) { michael@0: #endif michael@0: #else michael@0: if (uio->uio_resid < 0) { michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: sndlen = uio->uio_resid; michael@0: #else michael@0: sndlen = uio_resid(uio); michael@0: #endif michael@0: #else michael@0: sndlen = uio->uio_resid; michael@0: #endif michael@0: } else { michael@0: top = SCTP_HEADER_TO_CHAIN(i_pak); michael@0: #ifdef __Panda__ michael@0: /*- michael@0: * app len indicates the datalen, dgsize for cases michael@0: * of SCTP_EOF/ABORT will not have the right len michael@0: */ michael@0: sndlen = SCTP_APP_DATA_LEN(i_pak); michael@0: /*- michael@0: * Set the particle len also to zero to match michael@0: * up with app len. We only have one particle michael@0: * if app len is zero for Panda. This is ensured michael@0: * in the socket lib michael@0: */ michael@0: if (sndlen == 0) { michael@0: SCTP_BUF_LEN(top) = 0; michael@0: } michael@0: /*- michael@0: * We delink the chain from header, but keep michael@0: * the header around as we will need it in michael@0: * EAGAIN case michael@0: */ michael@0: SCTP_DETACH_HEADER_FROM_CHAIN(i_pak); michael@0: #else michael@0: sndlen = SCTP_HEADER_LEN(i_pak); michael@0: #endif michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n", michael@0: (void *)addr, michael@0: sndlen); michael@0: #ifdef __Panda__ michael@0: if (i_control) { michael@0: control = SCTP_HEADER_TO_CHAIN(i_control); michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && michael@0: (inp->sctp_socket->so_qlimit)) { michael@0: /* The listener can NOT send */ michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); michael@0: error = ENOTCONN; michael@0: goto out_unlocked; michael@0: } michael@0: /** michael@0: * Pre-screen address, if one is given the sin-len michael@0: * must be set correctly! michael@0: */ michael@0: if (addr) { michael@0: union sctp_sockstore *raddr = (union sctp_sockstore *)addr; michael@0: switch (raddr->sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifdef HAVE_SIN_LEN michael@0: if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: #endif michael@0: port = raddr->sin.sin_port; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifdef HAVE_SIN6_LEN michael@0: if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: #endif michael@0: port = raddr->sin6.sin6_port; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: #ifdef HAVE_SCONN_LEN michael@0: if (raddr->sconn.sconn_len != sizeof(struct sockaddr_conn)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: #endif michael@0: port = raddr->sconn.sconn_port; michael@0: break; michael@0: #endif michael@0: default: michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT); michael@0: error = EAFNOSUPPORT; michael@0: goto out_unlocked; michael@0: } michael@0: } else michael@0: port = 0; michael@0: michael@0: if (srcv) { michael@0: sinfo_flags = srcv->sinfo_flags; michael@0: sinfo_assoc_id = srcv->sinfo_assoc_id; michael@0: if (INVALID_SINFO_FLAG(sinfo_flags) || michael@0: PR_SCTP_INVALID_POLICY(sinfo_flags)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: if (srcv->sinfo_flags) michael@0: SCTP_STAT_INCR(sctps_sends_with_flags); michael@0: } else { michael@0: sinfo_flags = inp->def_send.sinfo_flags; michael@0: sinfo_assoc_id = inp->def_send.sinfo_assoc_id; michael@0: } michael@0: if (sinfo_flags & SCTP_SENDALL) { michael@0: /* its a sendall */ michael@0: error = sctp_sendall(inp, uio, top, srcv); michael@0: top = NULL; michael@0: goto out_unlocked; michael@0: } michael@0: if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: /* now we must find the assoc */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: SCTP_INP_RLOCK(inp); michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } else if (sinfo_assoc_id) { michael@0: stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0); michael@0: } else if (addr) { michael@0: /*- michael@0: * Since we did not use findep we must michael@0: * increment it, and if we don't find a tcb michael@0: * decrement it. michael@0: */ michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_INCR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); michael@0: if (stcb == NULL) { michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } else { michael@0: hold_tcblock = 1; michael@0: } michael@0: } michael@0: if ((stcb == NULL) && (addr)) { michael@0: /* Possible implicit send? */ michael@0: SCTP_ASOC_CREATE_LOCK(inp); michael@0: create_lock_applied = 1; michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: /* Should I really unlock ? */ michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: michael@0: } michael@0: if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && michael@0: (addr->sa_family == AF_INET6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_INCR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: /* With the lock applied look again */ michael@0: stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); michael@0: if ((stcb == NULL) && (control != NULL) && (port > 0)) { michael@0: stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error); michael@0: } michael@0: if (stcb == NULL) { michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } else { michael@0: hold_tcblock = 1; michael@0: } michael@0: if (error) { michael@0: goto out_unlocked; michael@0: } michael@0: if (t_inp != inp) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); michael@0: error = ENOTCONN; michael@0: goto out_unlocked; michael@0: } michael@0: } michael@0: if (stcb == NULL) { michael@0: if (addr == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); michael@0: error = ENOENT; michael@0: goto out_unlocked; michael@0: } else { michael@0: /* We must go ahead and start the INIT process */ michael@0: uint32_t vrf_id; michael@0: michael@0: if ((sinfo_flags & SCTP_ABORT) || michael@0: ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) { michael@0: /*- michael@0: * User asks to abort a non-existant assoc, michael@0: * or EOF a non-existant assoc with no data michael@0: */ michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); michael@0: error = ENOENT; michael@0: goto out_unlocked; michael@0: } michael@0: /* get an asoc/stcb struct */ michael@0: vrf_id = inp->def_vrf_id; michael@0: #ifdef INVARIANTS michael@0: if (create_lock_applied == 0) { michael@0: panic("Error, should hold create lock and I don't?"); michael@0: } michael@0: #endif michael@0: stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, michael@0: #if !(defined( __Panda__) || defined(__Userspace__)) michael@0: p michael@0: #else michael@0: (struct proc *)NULL michael@0: #endif michael@0: ); michael@0: if (stcb == NULL) { michael@0: /* Error is setup for us in the call */ michael@0: goto out_unlocked; michael@0: } michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { michael@0: stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; michael@0: /* Set the connected flag so we can queue data */ michael@0: soisconnecting(so); michael@0: } michael@0: hold_tcblock = 1; michael@0: if (create_lock_applied) { michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: create_lock_applied = 0; michael@0: } else { michael@0: SCTP_PRINTF("Huh-3? create lock should have been on??\n"); michael@0: } michael@0: /* Turn on queue only flag to prevent data from being sent */ michael@0: queue_only = 1; michael@0: asoc = &stcb->asoc; michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT); michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); michael@0: michael@0: /* initialize authentication params for the assoc */ michael@0: sctp_initialize_auth_params(inp, stcb); michael@0: michael@0: if (control) { michael@0: if (sctp_process_cmsgs_for_init(stcb, control, &error)) { michael@0: sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7); michael@0: hold_tcblock = 0; michael@0: stcb = NULL; michael@0: goto out_unlocked; michael@0: } michael@0: } michael@0: /* out with the INIT */ michael@0: queue_only_for_init = 1; michael@0: /*- michael@0: * we may want to dig in after this call and adjust the MTU michael@0: * value. It defaulted to 1500 (constant) but the ro michael@0: * structure may now have an update and thus we may need to michael@0: * change it BEFORE we append the message. michael@0: */ michael@0: } michael@0: } else michael@0: asoc = &stcb->asoc; michael@0: if (srcv == NULL) michael@0: srcv = (struct sctp_sndrcvinfo *)&asoc->def_send; michael@0: if (srcv->sinfo_flags & SCTP_ADDR_OVER) { michael@0: if (addr) michael@0: net = sctp_findnet(stcb, addr); michael@0: else michael@0: net = NULL; michael@0: if ((net == NULL) || michael@0: ((port != 0) && (port != stcb->rport))) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: } else { michael@0: if (stcb->asoc.alternate) { michael@0: net = stcb->asoc.alternate; michael@0: } else { michael@0: net = stcb->asoc.primary_destination; michael@0: } michael@0: } michael@0: atomic_add_int(&stcb->total_sends, 1); michael@0: /* Keep the stcb from being freed under our feet */ michael@0: atomic_add_int(&asoc->refcnt, 1); michael@0: free_cnt_applied = 1; michael@0: michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) { michael@0: if (sndlen > asoc->smallest_mtu) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); michael@0: error = EMSGSIZE; michael@0: goto out_unlocked; michael@0: } michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (inp->recv_callback) { michael@0: non_blocking = 1; michael@0: } michael@0: #else michael@0: if (SCTP_SO_IS_NBIO(so) michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: || (flags & MSG_NBIO) michael@0: #endif michael@0: ) { michael@0: non_blocking = 1; michael@0: } michael@0: #endif michael@0: /* would we block? */ michael@0: if (non_blocking) { michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); michael@0: if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) || michael@0: (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK); michael@0: if (sndlen > SCTP_SB_LIMIT_SND(so)) michael@0: error = EMSGSIZE; michael@0: else michael@0: error = EWOULDBLOCK; michael@0: goto out_unlocked; michael@0: } michael@0: stcb->asoc.sb_send_resv += sndlen; michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } else { michael@0: atomic_add_int(&stcb->asoc.sb_send_resv, sndlen); michael@0: } michael@0: local_soresv = sndlen; michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); michael@0: error = ECONNRESET; michael@0: goto out_unlocked; michael@0: } michael@0: if (create_lock_applied) { michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: create_lock_applied = 0; michael@0: } michael@0: if (asoc->stream_reset_outstanding) { michael@0: /* michael@0: * Can't queue any data while stream reset is underway. michael@0: */ michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN); michael@0: error = EAGAIN; michael@0: goto out_unlocked; michael@0: } michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: queue_only = 1; michael@0: } michael@0: /* we are now done with all control */ michael@0: if (control) { michael@0: sctp_m_freem(control); michael@0: control = NULL; michael@0: } michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || michael@0: (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { michael@0: if (srcv->sinfo_flags & SCTP_ABORT) { michael@0: ; michael@0: } else { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); michael@0: error = ECONNRESET; michael@0: goto out_unlocked; michael@0: } michael@0: } michael@0: /* Ok, we will attempt a msgsnd :> */ michael@0: #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) michael@0: if (p) { michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 603000 michael@0: p->td_ru.ru_msgsnd++; michael@0: #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: p->td_proc->p_stats->p_ru.ru_msgsnd++; michael@0: #else michael@0: p->p_stats->p_ru.ru_msgsnd++; michael@0: #endif michael@0: } michael@0: #endif michael@0: /* Are we aborting? */ michael@0: if (srcv->sinfo_flags & SCTP_ABORT) { michael@0: struct mbuf *mm; michael@0: int tot_demand, tot_out = 0, max_out; michael@0: michael@0: SCTP_STAT_INCR(sctps_sends_with_abort); michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: /* It has to be up before we abort */ michael@0: /* how big is the user initiated abort? */ michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out; michael@0: } michael@0: if (hold_tcblock) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } michael@0: if (top) { michael@0: struct mbuf *cntm = NULL; michael@0: michael@0: mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAITOK, 1, MT_DATA); michael@0: if (sndlen != 0) { michael@0: for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) { michael@0: tot_out += SCTP_BUF_LEN(cntm); michael@0: } michael@0: } michael@0: } else { michael@0: /* Must fit in a MTU */ michael@0: tot_out = sndlen; michael@0: tot_demand = (tot_out + sizeof(struct sctp_paramhdr)); michael@0: if (tot_demand > SCTP_DEFAULT_ADD_MORE) { michael@0: /* To big */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); michael@0: error = EMSGSIZE; michael@0: goto out; michael@0: } michael@0: mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAITOK, 1, MT_DATA); michael@0: } michael@0: if (mm == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); michael@0: error = ENOMEM; michael@0: goto out; michael@0: } michael@0: max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr); michael@0: max_out -= sizeof(struct sctp_abort_msg); michael@0: if (tot_out > max_out) { michael@0: tot_out = max_out; michael@0: } michael@0: if (mm) { michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: /* now move forward the data pointer */ michael@0: ph = mtod(mm, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr) + tot_out); michael@0: ph++; michael@0: SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr); michael@0: if (top == NULL) { michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(so, 0); michael@0: #endif michael@0: error = uiomove((caddr_t)ph, (int)tot_out, uio); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(so, 0); michael@0: #endif michael@0: if (error) { michael@0: /*- michael@0: * Here if we can't get his data we michael@0: * still abort we just don't get to michael@0: * send the users note :-0 michael@0: */ michael@0: sctp_m_freem(mm); michael@0: mm = NULL; michael@0: } michael@0: } else { michael@0: if (sndlen != 0) { michael@0: SCTP_BUF_NEXT(mm) = top; michael@0: } michael@0: } michael@0: } michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: free_cnt_applied = 0; michael@0: /* release this lock, otherwise we hang on ourselves */ michael@0: sctp_abort_an_association(stcb->sctp_ep, stcb, mm, SCTP_SO_LOCKED); michael@0: /* now relock the stcb so everything is sane */ michael@0: hold_tcblock = 0; michael@0: stcb = NULL; michael@0: /* In this case top is already chained to mm michael@0: * avoid double free, since we free it below if michael@0: * top != NULL and driver would free it after sending michael@0: * the packet out michael@0: */ michael@0: if (sndlen != 0) { michael@0: top = NULL; michael@0: } michael@0: goto out_unlocked; michael@0: } michael@0: /* Calculate the maximum we can send */ michael@0: inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); michael@0: if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { michael@0: if (non_blocking) { michael@0: /* we already checked for non-blocking above. */ michael@0: max_len = sndlen; michael@0: } else { michael@0: max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; michael@0: } michael@0: } else { michael@0: max_len = 0; michael@0: } michael@0: if (hold_tcblock) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } michael@0: /* Is the stream no. valid? */ michael@0: if (srcv->sinfo_stream >= asoc->streamoutcnt) { michael@0: /* Invalid stream number */ michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: if (asoc->strmout == NULL) { michael@0: /* huh? software error */ michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); michael@0: error = EFAULT; michael@0: goto out_unlocked; michael@0: } michael@0: michael@0: /* Unless E_EOR mode is on, we must make a send FIT in one call. */ michael@0: if ((user_marks_eor == 0) && michael@0: (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) { michael@0: /* It will NEVER fit */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); michael@0: error = EMSGSIZE; michael@0: goto out_unlocked; michael@0: } michael@0: if ((uio == NULL) && user_marks_eor) { michael@0: /*- michael@0: * We do not support eeor mode for michael@0: * sending with mbuf chains (like sendfile). michael@0: */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out_unlocked; michael@0: } michael@0: michael@0: if (user_marks_eor) { michael@0: local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold)); michael@0: } else { michael@0: /*- michael@0: * For non-eeor the whole message must fit in michael@0: * the socket send buffer. michael@0: */ michael@0: local_add_more = sndlen; michael@0: } michael@0: len = 0; michael@0: if (non_blocking) { michael@0: goto skip_preblock; michael@0: } michael@0: if (((max_len <= local_add_more) && michael@0: (SCTP_SB_LIMIT_SND(so) >= local_add_more)) || michael@0: (max_len == 0) || michael@0: ((stcb->asoc.chunks_on_out_queue+stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { michael@0: /* No room right now ! */ michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); michael@0: while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) || michael@0: ((stcb->asoc.stream_queue_cnt+stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1,"pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n", michael@0: (unsigned int)SCTP_SB_LIMIT_SND(so), michael@0: inqueue_bytes, michael@0: local_add_more, michael@0: stcb->asoc.stream_queue_cnt, michael@0: stcb->asoc.chunks_on_out_queue, michael@0: SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen); michael@0: } michael@0: be.error = 0; michael@0: #if !defined(__Panda__) && !defined(__Windows__) michael@0: stcb->block_entry = &be; michael@0: #endif michael@0: error = sbwait(&so->so_snd); michael@0: stcb->block_entry = NULL; michael@0: if (error || so->so_error || be.error) { michael@0: if (error == 0) { michael@0: if (so->so_error) michael@0: error = so->so_error; michael@0: if (be.error) { michael@0: error = be.error; michael@0: } michael@0: } michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: goto out_unlocked; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, michael@0: asoc, stcb->asoc.total_output_queue_size); michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: goto out_unlocked; michael@0: } michael@0: inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); michael@0: } michael@0: if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { michael@0: max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; michael@0: } else { michael@0: max_len = 0; michael@0: } michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: } michael@0: michael@0: skip_preblock: michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: goto out_unlocked; michael@0: } michael@0: #if defined(__APPLE__) michael@0: error = sblock(&so->so_snd, SBLOCKWAIT(flags)); michael@0: #endif michael@0: /* sndlen covers for mbuf case michael@0: * uio_resid covers for the non-mbuf case michael@0: * NOTE: uio will be null when top/mbuf is passed michael@0: */ michael@0: if (sndlen == 0) { michael@0: if (srcv->sinfo_flags & SCTP_EOF) { michael@0: got_all_of_the_send = 1; michael@0: goto dataless_eof; michael@0: } else { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out; michael@0: } michael@0: } michael@0: if (top == NULL) { michael@0: struct sctp_stream_queue_pending *sp; michael@0: struct sctp_stream_out *strm; michael@0: uint32_t sndout; michael@0: michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if ((asoc->stream_locked) && michael@0: (asoc->stream_locked_on != srcv->sinfo_stream)) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); michael@0: error = EINVAL; michael@0: goto out; michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: michael@0: strm = &stcb->asoc.strmout[srcv->sinfo_stream]; michael@0: if (strm->last_msg_incomplete == 0) { michael@0: do_a_copy_in: michael@0: sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error); michael@0: if ((sp == NULL) || (error)) { michael@0: goto out; michael@0: } michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if (sp->msg_is_complete) { michael@0: strm->last_msg_incomplete = 0; michael@0: asoc->stream_locked = 0; michael@0: } else { michael@0: /* Just got locked to this guy in michael@0: * case of an interrupt. michael@0: */ michael@0: strm->last_msg_incomplete = 1; michael@0: asoc->stream_locked = 1; michael@0: asoc->stream_locked_on = srcv->sinfo_stream; michael@0: sp->sender_all_done = 0; michael@0: } michael@0: sctp_snd_sb_alloc(stcb, sp->length); michael@0: atomic_add_int(&asoc->stream_queue_cnt, 1); michael@0: if (srcv->sinfo_flags & SCTP_UNORDERED) { michael@0: SCTP_STAT_INCR(sctps_sends_with_unord); michael@0: } michael@0: TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); michael@0: stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1); michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } else { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead); michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: if (sp == NULL) { michael@0: /* ???? Huh ??? last msg is gone */ michael@0: #ifdef INVARIANTS michael@0: panic("Warning: Last msg marked incomplete, yet nothing left?"); michael@0: #else michael@0: SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n"); michael@0: strm->last_msg_incomplete = 0; michael@0: #endif michael@0: goto do_a_copy_in; michael@0: michael@0: } michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: while (uio->uio_resid > 0) { michael@0: #else michael@0: while (uio_resid(uio) > 0) { michael@0: #endif michael@0: #else michael@0: while (uio->uio_resid > 0) { michael@0: #endif michael@0: /* How much room do we have? */ michael@0: struct mbuf *new_tail, *mm; michael@0: michael@0: if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) michael@0: max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size; michael@0: else michael@0: max_len = 0; michael@0: michael@0: if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) || michael@0: (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) || michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: (uio->uio_resid && (uio->uio_resid <= (int)max_len))) { michael@0: #else michael@0: (uio_resid(uio) && (uio_resid(uio) <= (int)max_len))) { michael@0: #endif michael@0: #else michael@0: (uio->uio_resid && (uio->uio_resid <= (int)max_len))) { michael@0: #endif michael@0: sndout = 0; michael@0: new_tail = NULL; michael@0: if (hold_tcblock) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(so, 0); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 602000 michael@0: mm = sctp_copy_resume(uio, max_len, user_marks_eor, &error, &sndout, &new_tail); michael@0: #else michael@0: mm = sctp_copy_resume(uio, max_len, &error, &sndout, &new_tail); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(so, 0); michael@0: #endif michael@0: if ((mm == NULL) || error) { michael@0: if (mm) { michael@0: sctp_m_freem(mm); michael@0: } michael@0: goto out; michael@0: } michael@0: /* Update the mbuf and count */ michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: /* we need to get out. michael@0: * Peer probably aborted. michael@0: */ michael@0: sctp_m_freem(mm); michael@0: if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); michael@0: error = ECONNRESET; michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: goto out; michael@0: } michael@0: if (sp->tail_mbuf) { michael@0: /* tack it to the end */ michael@0: SCTP_BUF_NEXT(sp->tail_mbuf) = mm; michael@0: sp->tail_mbuf = new_tail; michael@0: } else { michael@0: /* A stolen mbuf */ michael@0: sp->data = mm; michael@0: sp->tail_mbuf = new_tail; michael@0: } michael@0: sctp_snd_sb_alloc(stcb, sndout); michael@0: atomic_add_int(&sp->length,sndout); michael@0: len += sndout; michael@0: michael@0: /* Did we reach EOR? */ michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if ((uio->uio_resid == 0) && michael@0: #else michael@0: if ((uio_resid(uio) == 0) && michael@0: #endif michael@0: #else michael@0: if ((uio->uio_resid == 0) && michael@0: #endif michael@0: ((user_marks_eor == 0) || michael@0: (srcv->sinfo_flags & SCTP_EOF) || michael@0: (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { michael@0: sp->msg_is_complete = 1; michael@0: } else { michael@0: sp->msg_is_complete = 0; michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if (uio->uio_resid == 0) { michael@0: #else michael@0: if (uio_resid(uio) == 0) { michael@0: #endif michael@0: #else michael@0: if (uio->uio_resid == 0) { michael@0: #endif michael@0: /* got it all? */ michael@0: continue; michael@0: } michael@0: /* PR-SCTP? */ michael@0: if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) { michael@0: /* This is ugly but we must assure locking order */ michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: sctp_prune_prsctp(stcb, asoc, srcv, sndlen); michael@0: inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); michael@0: if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) michael@0: max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; michael@0: else michael@0: max_len = 0; michael@0: if (max_len > 0) { michael@0: continue; michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } michael@0: /* wait for space now */ michael@0: if (non_blocking) { michael@0: /* Non-blocking io in place out */ michael@0: goto skip_out_eof; michael@0: } michael@0: /* What about the INIT, send it maybe */ michael@0: if (queue_only_for_init) { michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) { michael@0: /* a collision took us forward? */ michael@0: queue_only = 0; michael@0: } else { michael@0: sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT); michael@0: queue_only = 1; michael@0: } michael@0: } michael@0: if ((net->flight_size > net->cwnd) && michael@0: (asoc->sctp_cmt_on_off == 0)) { michael@0: SCTP_STAT_INCR(sctps_send_cwnd_avoid); michael@0: queue_only = 1; michael@0: } else if (asoc->ifp_had_enobuf) { michael@0: SCTP_STAT_INCR(sctps_ifnomemqueued); michael@0: if (net->flight_size > (2 * net->mtu)) { michael@0: queue_only = 1; michael@0: } michael@0: asoc->ifp_had_enobuf = 0; michael@0: } michael@0: un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + michael@0: (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); michael@0: if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && michael@0: (stcb->asoc.total_flight > 0) && michael@0: (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && michael@0: (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { michael@0: michael@0: /*- michael@0: * Ok, Nagle is set on and we have data outstanding. michael@0: * Don't send anything and let SACKs drive out the michael@0: * data unless wen have a "full" segment to send. michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { michael@0: sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); michael@0: } michael@0: SCTP_STAT_INCR(sctps_naglequeued); michael@0: nagle_applies = 1; michael@0: } else { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { michael@0: if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) michael@0: sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); michael@0: } michael@0: SCTP_STAT_INCR(sctps_naglesent); michael@0: nagle_applies = 0; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: michael@0: sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, michael@0: nagle_applies, un_sent); michael@0: sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, michael@0: stcb->asoc.total_flight, michael@0: stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); michael@0: } michael@0: if (queue_only_for_init) michael@0: queue_only_for_init = 0; michael@0: if ((queue_only == 0) && (nagle_applies == 0)) { michael@0: /*- michael@0: * need to start chunk output michael@0: * before blocking.. note that if michael@0: * a lock is already applied, then michael@0: * the input via the net is happening michael@0: * and I don't need to start output :-D michael@0: */ michael@0: if (hold_tcblock == 0) { michael@0: if (SCTP_TCB_TRYLOCK(stcb)) { michael@0: hold_tcblock = 1; michael@0: sctp_chunk_output(inp, michael@0: stcb, michael@0: SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); michael@0: } michael@0: } else { michael@0: sctp_chunk_output(inp, michael@0: stcb, michael@0: SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); michael@0: } michael@0: if (hold_tcblock == 1) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: hold_tcblock = 0; michael@0: } michael@0: } michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: /*- michael@0: * This is a bit strange, but I think it will michael@0: * work. The total_output_queue_size is locked and michael@0: * protected by the TCB_LOCK, which we just released. michael@0: * There is a race that can occur between releasing it michael@0: * above, and me getting the socket lock, where sacks michael@0: * come in but we have not put the SB_WAIT on the michael@0: * so_snd buffer to get the wakeup. After the LOCK michael@0: * is applied the sack_processing will also need to michael@0: * LOCK the so->so_snd to do the actual sowwakeup(). So michael@0: * once we have the socket buffer lock if we recheck the michael@0: * size we KNOW we will get to sleep safely with the michael@0: * wakeup flag in place. michael@0: */ michael@0: if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size + michael@0: min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, michael@0: asoc, uio->uio_resid); michael@0: #else michael@0: sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, michael@0: asoc, uio_resid(uio)); michael@0: #endif michael@0: #else michael@0: sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, michael@0: asoc, uio->uio_resid); michael@0: #endif michael@0: } michael@0: be.error = 0; michael@0: #if !defined(__Panda__) && !defined(__Windows__) michael@0: stcb->block_entry = &be; michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: sbunlock(&so->so_snd, 1); michael@0: #endif michael@0: error = sbwait(&so->so_snd); michael@0: stcb->block_entry = NULL; michael@0: michael@0: if (error || so->so_error || be.error) { michael@0: if (error == 0) { michael@0: if (so->so_error) michael@0: error = so->so_error; michael@0: if (be.error) { michael@0: error = be.error; michael@0: } michael@0: } michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: goto out_unlocked; michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: error = sblock(&so->so_snd, SBLOCKWAIT(flags)); michael@0: #endif michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, michael@0: asoc, stcb->asoc.total_output_queue_size); michael@0: } michael@0: } michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: goto out_unlocked; michael@0: } michael@0: } michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if (sp) { michael@0: if (sp->msg_is_complete == 0) { michael@0: strm->last_msg_incomplete = 1; michael@0: asoc->stream_locked = 1; michael@0: asoc->stream_locked_on = srcv->sinfo_stream; michael@0: } else { michael@0: sp->sender_all_done = 1; michael@0: strm->last_msg_incomplete = 0; michael@0: asoc->stream_locked = 0; michael@0: } michael@0: } else { michael@0: SCTP_PRINTF("Huh no sp TSNH?\n"); michael@0: strm->last_msg_incomplete = 0; michael@0: asoc->stream_locked = 0; michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if (uio->uio_resid == 0) { michael@0: #else michael@0: if (uio_resid(uio) == 0) { michael@0: #endif michael@0: #else michael@0: if (uio->uio_resid == 0) { michael@0: #endif michael@0: got_all_of_the_send = 1; michael@0: } michael@0: } else { michael@0: /* We send in a 0, since we do NOT have any locks */ michael@0: error = sctp_msg_append(stcb, net, top, srcv, 0); michael@0: top = NULL; michael@0: if (srcv->sinfo_flags & SCTP_EOF) { michael@0: /* michael@0: * This should only happen for Panda for the mbuf michael@0: * send case, which does NOT yet support EEOR mode. michael@0: * Thus, we can just set this flag to do the proper michael@0: * EOF handling. michael@0: */ michael@0: got_all_of_the_send = 1; michael@0: } michael@0: } michael@0: if (error) { michael@0: goto out; michael@0: } michael@0: dataless_eof: michael@0: /* EOF thing ? */ michael@0: if ((srcv->sinfo_flags & SCTP_EOF) && michael@0: (got_all_of_the_send == 1)) { michael@0: int cnt; michael@0: SCTP_STAT_INCR(sctps_sends_with_eof); michael@0: error = 0; michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED); michael@0: if (TAILQ_EMPTY(&asoc->send_queue) && michael@0: TAILQ_EMPTY(&asoc->sent_queue) && michael@0: (cnt == 0)) { michael@0: if (asoc->locked_on_sending) { michael@0: goto abort_anyway; michael@0: } michael@0: /* there is nothing queued to send, so I'm done... */ michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: struct sctp_nets *netp; michael@0: michael@0: /* only send SHUTDOWN the first time through */ michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); michael@0: SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); michael@0: sctp_stop_timers_for_shutdown(stcb); michael@0: if (stcb->asoc.alternate) { michael@0: netp = stcb->asoc.alternate; michael@0: } else { michael@0: netp = stcb->asoc.primary_destination; michael@0: } michael@0: sctp_send_shutdown(stcb, netp); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, michael@0: netp); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, michael@0: asoc->primary_destination); michael@0: } michael@0: } else { michael@0: /*- michael@0: * we still got (or just got) data to send, so set michael@0: * SHUTDOWN_PENDING michael@0: */ michael@0: /*- michael@0: * XXX sockets draft says that SCTP_EOF should be michael@0: * sent with no data. currently, we will allow user michael@0: * data to be sent first and move to michael@0: * SHUTDOWN-PENDING michael@0: */ michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: if (asoc->locked_on_sending) { michael@0: /* Locked to send out the data */ michael@0: struct sctp_stream_queue_pending *sp; michael@0: sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); michael@0: if (sp) { michael@0: if ((sp->length == 0) && (sp->msg_is_complete == 0)) michael@0: asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; michael@0: } michael@0: } michael@0: asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; michael@0: if (TAILQ_EMPTY(&asoc->send_queue) && michael@0: TAILQ_EMPTY(&asoc->sent_queue) && michael@0: (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { michael@0: abort_anyway: michael@0: if (free_cnt_applied) { michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: free_cnt_applied = 0; michael@0: } michael@0: sctp_abort_an_association(stcb->sctp_ep, stcb, michael@0: NULL, SCTP_SO_LOCKED); michael@0: /* now relock the stcb so everything is sane */ michael@0: hold_tcblock = 0; michael@0: stcb = NULL; michael@0: goto out; michael@0: } michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, michael@0: asoc->primary_destination); michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY); michael@0: } michael@0: } michael@0: } michael@0: skip_out_eof: michael@0: if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { michael@0: some_on_control = 1; michael@0: } michael@0: if (queue_only_for_init) { michael@0: if (hold_tcblock == 0) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: hold_tcblock = 1; michael@0: } michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) { michael@0: /* a collision took us forward? */ michael@0: queue_only = 0; michael@0: } else { michael@0: sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); michael@0: SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT); michael@0: queue_only = 1; michael@0: } michael@0: } michael@0: if ((net->flight_size > net->cwnd) && michael@0: (stcb->asoc.sctp_cmt_on_off == 0)) { michael@0: SCTP_STAT_INCR(sctps_send_cwnd_avoid); michael@0: queue_only = 1; michael@0: } else if (asoc->ifp_had_enobuf) { michael@0: SCTP_STAT_INCR(sctps_ifnomemqueued); michael@0: if (net->flight_size > (2 * net->mtu)) { michael@0: queue_only = 1; michael@0: } michael@0: asoc->ifp_had_enobuf = 0; michael@0: } michael@0: un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + michael@0: (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); michael@0: if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && michael@0: (stcb->asoc.total_flight > 0) && michael@0: (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && michael@0: (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { michael@0: /*- michael@0: * Ok, Nagle is set on and we have data outstanding. michael@0: * Don't send anything and let SACKs drive out the michael@0: * data unless wen have a "full" segment to send. michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { michael@0: sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); michael@0: } michael@0: SCTP_STAT_INCR(sctps_naglequeued); michael@0: nagle_applies = 1; michael@0: } else { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { michael@0: if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) michael@0: sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); michael@0: } michael@0: SCTP_STAT_INCR(sctps_naglesent); michael@0: nagle_applies = 0; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { michael@0: sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, michael@0: nagle_applies, un_sent); michael@0: sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, michael@0: stcb->asoc.total_flight, michael@0: stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); michael@0: } michael@0: if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) { michael@0: /* we can attempt to send too. */ michael@0: if (hold_tcblock == 0) { michael@0: /* If there is activity recv'ing sacks no need to send */ michael@0: if (SCTP_TCB_TRYLOCK(stcb)) { michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); michael@0: hold_tcblock = 1; michael@0: } michael@0: } else { michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); michael@0: } michael@0: } else if ((queue_only == 0) && michael@0: (stcb->asoc.peers_rwnd == 0) && michael@0: (stcb->asoc.total_flight == 0)) { michael@0: /* We get to have a probe outstanding */ michael@0: if (hold_tcblock == 0) { michael@0: hold_tcblock = 1; michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); michael@0: } else if (some_on_control) { michael@0: int num_out, reason, frag_point; michael@0: michael@0: /* Here we do control only */ michael@0: if (hold_tcblock == 0) { michael@0: hold_tcblock = 1; michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: frag_point = sctp_get_frag_point(stcb, &stcb->asoc); michael@0: (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, michael@0: &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n", michael@0: queue_only, stcb->asoc.peers_rwnd, un_sent, michael@0: stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue, michael@0: stcb->asoc.total_output_queue_size, error); michael@0: michael@0: out: michael@0: #if defined(__APPLE__) michael@0: sbunlock(&so->so_snd, 1); michael@0: #endif michael@0: out_unlocked: michael@0: michael@0: if (local_soresv && stcb) { michael@0: atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen); michael@0: } michael@0: if (create_lock_applied) { michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: } michael@0: if ((stcb) && hold_tcblock) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: if (stcb && free_cnt_applied) { michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: } michael@0: #ifdef INVARIANTS michael@0: #if !defined(__APPLE__) michael@0: if (stcb) { michael@0: if (mtx_owned(&stcb->tcb_mtx)) { michael@0: panic("Leaving with tcb mtx owned?"); michael@0: } michael@0: if (mtx_owned(&stcb->tcb_send_mtx)) { michael@0: panic("Leaving with tcb send mtx owned?"); michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: #ifdef __Panda__ michael@0: /* michael@0: * Handle the EAGAIN/ENOMEM cases to reattach the pak header michael@0: * to particle when pak is passed in, so that caller michael@0: * can try again with this pak michael@0: * michael@0: * NOTE: For other cases, including success case, michael@0: * we simply want to return the header back to free michael@0: * pool michael@0: */ michael@0: if (top) { michael@0: if ((error == EAGAIN) || (error == ENOMEM)) { michael@0: SCTP_ATTACH_CHAIN(i_pak, top, sndlen); michael@0: top = NULL; michael@0: } else { michael@0: (void)SCTP_RELEASE_HEADER(i_pak); michael@0: } michael@0: } else { michael@0: /* This is to handle cases when top has michael@0: * been reset to NULL but pak might not michael@0: * be freed michael@0: */ michael@0: if (i_pak) { michael@0: (void)SCTP_RELEASE_HEADER(i_pak); michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INVARIANTS michael@0: if (inp) { michael@0: sctp_validate_no_locks(inp); michael@0: } else { michael@0: SCTP_PRINTF("Warning - inp is NULL so cant validate locks\n"); michael@0: } michael@0: #endif michael@0: if (top) { michael@0: sctp_m_freem(top); michael@0: } michael@0: if (control) { michael@0: sctp_m_freem(control); michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * generate an AUTHentication chunk, if required michael@0: */ michael@0: struct mbuf * michael@0: sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, michael@0: struct sctp_auth_chunk **auth_ret, uint32_t * offset, michael@0: struct sctp_tcb *stcb, uint8_t chunk) michael@0: { michael@0: struct mbuf *m_auth; michael@0: struct sctp_auth_chunk *auth; michael@0: int chunk_len; michael@0: struct mbuf *cn; michael@0: michael@0: if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) || michael@0: (stcb == NULL)) michael@0: return (m); michael@0: michael@0: /* sysctl disabled auth? */ michael@0: if (SCTP_BASE_SYSCTL(sctp_auth_disable)) michael@0: return (m); michael@0: michael@0: /* peer doesn't do auth... */ michael@0: if (!stcb->asoc.peer_supports_auth) { michael@0: return (m); michael@0: } michael@0: /* does the requested chunk require auth? */ michael@0: if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) { michael@0: return (m); michael@0: } michael@0: m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER); michael@0: if (m_auth == NULL) { michael@0: /* no mbuf's */ michael@0: return (m); michael@0: } michael@0: /* reserve some space if this will be the first mbuf */ michael@0: if (m == NULL) michael@0: SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD); michael@0: /* fill in the AUTH chunk details */ michael@0: auth = mtod(m_auth, struct sctp_auth_chunk *); michael@0: bzero(auth, sizeof(*auth)); michael@0: auth->ch.chunk_type = SCTP_AUTHENTICATION; michael@0: auth->ch.chunk_flags = 0; michael@0: chunk_len = sizeof(*auth) + michael@0: sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); michael@0: auth->ch.chunk_length = htons(chunk_len); michael@0: auth->hmac_id = htons(stcb->asoc.peer_hmac_id); michael@0: /* key id and hmac digest will be computed and filled in upon send */ michael@0: michael@0: /* save the offset where the auth was inserted into the chain */ michael@0: *offset = 0; michael@0: for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) { michael@0: *offset += SCTP_BUF_LEN(cn); michael@0: } michael@0: michael@0: /* update length and return pointer to the auth chunk */ michael@0: SCTP_BUF_LEN(m_auth) = chunk_len; michael@0: m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0); michael@0: if (auth_ret != NULL) michael@0: *auth_ret = auth; michael@0: michael@0: return (m); michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: #ifdef INET6 michael@0: int michael@0: sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) michael@0: { michael@0: struct nd_prefix *pfx = NULL; michael@0: struct nd_pfxrouter *pfxrtr = NULL; michael@0: struct sockaddr_in6 gw6; michael@0: michael@0: if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6) michael@0: return (0); michael@0: michael@0: /* get prefix entry of address */ michael@0: LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) { michael@0: if (pfx->ndpr_stateflags & NDPRF_DETACHED) michael@0: continue; michael@0: if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr, michael@0: &src6->sin6_addr, &pfx->ndpr_mask)) michael@0: break; michael@0: } michael@0: /* no prefix entry in the prefix list */ michael@0: if (pfx == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); michael@0: return (0); michael@0: } michael@0: michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); michael@0: michael@0: /* search installed gateway from prefix entry */ michael@0: LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) { michael@0: memset(&gw6, 0, sizeof(struct sockaddr_in6)); michael@0: gw6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: gw6.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr, michael@0: sizeof(struct in6_addr)); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); michael@0: if (sctp_cmpaddr((struct sockaddr *)&gw6, michael@0: ro->ro_rt->rt_gateway)) { michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n"); michael@0: return (1); michael@0: } michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n"); michael@0: return (0); michael@0: } michael@0: #endif michael@0: michael@0: int michael@0: sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr_in *sin, *mask; michael@0: struct ifaddr *ifa; michael@0: struct in_addr srcnetaddr, gwnetaddr; michael@0: michael@0: if (ro == NULL || ro->ro_rt == NULL || michael@0: sifa->address.sa.sa_family != AF_INET) { michael@0: return (0); michael@0: } michael@0: ifa = (struct ifaddr *)sifa->ifa; michael@0: mask = (struct sockaddr_in *)(ifa->ifa_netmask); michael@0: sin = (struct sockaddr_in *)&sifa->address.sin; michael@0: srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr); michael@0: michael@0: sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway; michael@0: gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); michael@0: SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr); michael@0: if (srcnetaddr.s_addr == gwnetaddr.s_addr) { michael@0: return (1); michael@0: } michael@0: #endif michael@0: return (0); michael@0: } michael@0: #elif defined(__Userspace__) michael@0: /* TODO __Userspace__ versions of sctp_vXsrc_match_nexthop(). */ michael@0: int michael@0: sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) michael@0: { michael@0: return (0); michael@0: } michael@0: int michael@0: sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) michael@0: { michael@0: return (0); michael@0: } michael@0: michael@0: #endif