[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup tcp
|
---|
[89e57cee] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[89e57cee] | 34 | * TCP module.
|
---|
[21580dd] | 35 | */
|
---|
| 36 |
|
---|
[89e57cee] | 37 | #ifndef NET_TCP_H_
|
---|
| 38 | #define NET_TCP_H_
|
---|
[21580dd] | 39 |
|
---|
| 40 | #include <fibril_synch.h>
|
---|
| 41 |
|
---|
[c69d327] | 42 | #include <net/packet.h>
|
---|
[e526f08] | 43 | #include <net/device.h>
|
---|
[849ed54] | 44 | #include <socket_core.h>
|
---|
| 45 | #include <tl_common.h>
|
---|
[21580dd] | 46 |
|
---|
| 47 | /** Type definition of the TCP global data.
|
---|
[89e57cee] | 48 | * @see tcp_globals
|
---|
[21580dd] | 49 | */
|
---|
[89e57cee] | 50 | typedef struct tcp_globals tcp_globals_t;
|
---|
[21580dd] | 51 |
|
---|
| 52 | /** Type definition of the TCP socket specific data.
|
---|
[89e57cee] | 53 | * @see tcp_socket_data
|
---|
[21580dd] | 54 | */
|
---|
[89e57cee] | 55 | typedef struct tcp_socket_data tcp_socket_data_t;
|
---|
[21580dd] | 56 |
|
---|
| 57 | /** Type definition of the TCP operation data.
|
---|
[89e57cee] | 58 | * @see tcp_operation
|
---|
[21580dd] | 59 | */
|
---|
[89e57cee] | 60 | typedef struct tcp_operation tcp_operation_t;
|
---|
[21580dd] | 61 |
|
---|
| 62 | /** TCP socket state type definition.
|
---|
[89e57cee] | 63 | * @see tcp_socket_state
|
---|
[21580dd] | 64 | */
|
---|
[89e57cee] | 65 | typedef enum tcp_socket_state tcp_socket_state_t;
|
---|
[21580dd] | 66 |
|
---|
[89e57cee] | 67 | /** TCP socket state. */
|
---|
| 68 | enum tcp_socket_state {
|
---|
[21580dd] | 69 | /** Initial.
|
---|
[89e57cee] | 70 | *
|
---|
| 71 | * Not connected or bound.
|
---|
[21580dd] | 72 | */
|
---|
| 73 | TCP_SOCKET_INITIAL,
|
---|
[89e57cee] | 74 |
|
---|
[21580dd] | 75 | /** Listening.
|
---|
[89e57cee] | 76 | *
|
---|
| 77 | * Awaiting a connection request from another TCP layer.
|
---|
| 78 | * When SYN is received a new bound socket in the
|
---|
| 79 | * TCP_SOCKET_SYN_RECEIVED state should be created.
|
---|
[21580dd] | 80 | */
|
---|
| 81 | TCP_SOCKET_LISTEN,
|
---|
[89e57cee] | 82 |
|
---|
[21580dd] | 83 | /** Connecting issued.
|
---|
[89e57cee] | 84 | *
|
---|
| 85 | * A SYN has been sent, and TCP is awaiting the response SYN.
|
---|
| 86 | * Should continue to the TCP_SOCKET_ESTABLISHED state.
|
---|
[21580dd] | 87 | */
|
---|
| 88 | TCP_SOCKET_SYN_SENT,
|
---|
[89e57cee] | 89 |
|
---|
[21580dd] | 90 | /** Connecting received.
|
---|
[89e57cee] | 91 | *
|
---|
| 92 | * A SYN has been received, a SYN has been sent, and TCP is awaiting an
|
---|
| 93 | * ACK. Should continue to the TCP_SOCKET_ESTABLISHED state.
|
---|
[21580dd] | 94 | */
|
---|
| 95 | TCP_SOCKET_SYN_RECEIVED,
|
---|
[89e57cee] | 96 |
|
---|
[21580dd] | 97 | /** Connected.
|
---|
[89e57cee] | 98 | *
|
---|
| 99 | * The three-way handshake has been completed.
|
---|
[21580dd] | 100 | */
|
---|
| 101 | TCP_SOCKET_ESTABLISHED,
|
---|
[89e57cee] | 102 |
|
---|
[21580dd] | 103 | /** Closing started.
|
---|
[89e57cee] | 104 | *
|
---|
| 105 | * The local application has issued a CLOSE.
|
---|
| 106 | * TCP has sent a FIN, and is awaiting an ACK or a FIN.
|
---|
| 107 | * Should continue to the TCP_SOCKET_FIN_WAIT_2 state when an ACK is
|
---|
| 108 | * received.
|
---|
| 109 | * Should continue to the TCP_SOCKET_CLOSING state when a FIN is
|
---|
| 110 | * received.
|
---|
[21580dd] | 111 | */
|
---|
| 112 | TCP_SOCKET_FIN_WAIT_1,
|
---|
[89e57cee] | 113 |
|
---|
[21580dd] | 114 | /** Closing confirmed.
|
---|
[89e57cee] | 115 | *
|
---|
| 116 | * A FIN has been sent, and an ACK received.
|
---|
| 117 | * TCP is awaiting a~FIN from the remote TCP layer.
|
---|
| 118 | * Should continue to the TCP_SOCKET_CLOSING state.
|
---|
[21580dd] | 119 | */
|
---|
| 120 | TCP_SOCKET_FIN_WAIT_2,
|
---|
[89e57cee] | 121 |
|
---|
[21580dd] | 122 | /** Closing.
|
---|
[89e57cee] | 123 | *
|
---|
| 124 | * A FIN has been sent, a FIN has been received, and an ACK has been
|
---|
| 125 | * sent.
|
---|
| 126 | * TCP is awaiting an ACK for the FIN that was sent.
|
---|
| 127 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
---|
[21580dd] | 128 | */
|
---|
| 129 | TCP_SOCKET_CLOSING,
|
---|
[89e57cee] | 130 |
|
---|
[21580dd] | 131 | /** Closing received.
|
---|
[89e57cee] | 132 | *
|
---|
| 133 | * TCP has received a FIN, and has sent an ACK.
|
---|
| 134 | * It is awaiting a close request from the local application before
|
---|
| 135 | * sending a FIN.
|
---|
| 136 | * Should continue to the TCP_SOCKET_SOCKET_LAST_ACK state.
|
---|
[21580dd] | 137 | */
|
---|
| 138 | TCP_SOCKET_CLOSE_WAIT,
|
---|
[89e57cee] | 139 |
|
---|
| 140 | /**
|
---|
| 141 | * A FIN has been received, and an ACK and a FIN have been sent.
|
---|
| 142 | * TCP is awaiting an ACK.
|
---|
| 143 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
---|
[21580dd] | 144 | */
|
---|
| 145 | TCP_SOCKET_LAST_ACK,
|
---|
[89e57cee] | 146 |
|
---|
[21580dd] | 147 | /** Closing finished.
|
---|
[89e57cee] | 148 | *
|
---|
| 149 | * FINs have been received and ACK’d, and TCP is waiting two MSLs to
|
---|
| 150 | * remove the connection from the table.
|
---|
[21580dd] | 151 | */
|
---|
| 152 | TCP_SOCKET_TIME_WAIT,
|
---|
[89e57cee] | 153 |
|
---|
[21580dd] | 154 | /** Closed.
|
---|
[89e57cee] | 155 | *
|
---|
| 156 | * Imaginary, this indicates that a connection has been removed from
|
---|
| 157 | * the connection table.
|
---|
[21580dd] | 158 | */
|
---|
| 159 | TCP_SOCKET_CLOSED
|
---|
| 160 | };
|
---|
| 161 |
|
---|
[89e57cee] | 162 | /** TCP operation data. */
|
---|
| 163 | struct tcp_operation {
|
---|
| 164 | /** Operation result. */
|
---|
[aadf01e] | 165 | int result;
|
---|
[89e57cee] | 166 | /** Safety lock. */
|
---|
[aadf01e] | 167 | fibril_mutex_t mutex;
|
---|
[89e57cee] | 168 | /** Operation result signaling. */
|
---|
[aadf01e] | 169 | fibril_condvar_t condvar;
|
---|
[21580dd] | 170 | };
|
---|
| 171 |
|
---|
[89e57cee] | 172 | /** TCP socket specific data. */
|
---|
| 173 | struct tcp_socket_data {
|
---|
| 174 | /** TCP socket state. */
|
---|
[aadf01e] | 175 | tcp_socket_state_t state;
|
---|
[89e57cee] | 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Data fragment size.
|
---|
| 179 | * Sending optimalization.
|
---|
[21580dd] | 180 | */
|
---|
[aadf01e] | 181 | size_t data_fragment_size;
|
---|
[89e57cee] | 182 |
|
---|
| 183 | /** Device identifier. */
|
---|
[aadf01e] | 184 | device_id_t device_id;
|
---|
[89e57cee] | 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Listening backlog.
|
---|
| 188 | * The maximal number of connected but not yet accepted sockets.
|
---|
[21580dd] | 189 | */
|
---|
[aadf01e] | 190 | int backlog;
|
---|
[89e57cee] | 191 |
|
---|
| 192 | /**
|
---|
| 193 | * Parent listening socket identifier.
|
---|
| 194 | * Set if this socket is an accepted one.
|
---|
[21580dd] | 195 | */
|
---|
[aadf01e] | 196 | int listening_socket_id;
|
---|
[89e57cee] | 197 |
|
---|
| 198 | /** Treshold size in bytes. */
|
---|
[aadf01e] | 199 | size_t treshold;
|
---|
[89e57cee] | 200 | /** Window size in bytes. */
|
---|
[aadf01e] | 201 | size_t window;
|
---|
[89e57cee] | 202 | /** Acknowledgement timeout. */
|
---|
[aadf01e] | 203 | suseconds_t timeout;
|
---|
[89e57cee] | 204 | /** Last acknowledged byte. */
|
---|
[aadf01e] | 205 | uint32_t acknowledged;
|
---|
[89e57cee] | 206 | /** Next incoming sequence number. */
|
---|
[aadf01e] | 207 | uint32_t next_incoming;
|
---|
[89e57cee] | 208 | /** Incoming FIN. */
|
---|
[aadf01e] | 209 | uint32_t fin_incoming;
|
---|
[89e57cee] | 210 | /** Next outgoing sequence number. */
|
---|
[aadf01e] | 211 | uint32_t next_outgoing;
|
---|
[89e57cee] | 212 | /** Last outgoing sequence number. */
|
---|
[aadf01e] | 213 | uint32_t last_outgoing;
|
---|
[89e57cee] | 214 | /** Outgoing FIN. */
|
---|
[aadf01e] | 215 | uint32_t fin_outgoing;
|
---|
[89e57cee] | 216 |
|
---|
| 217 | /**
|
---|
| 218 | * Expected sequence number by the remote host.
|
---|
| 219 | * The sequence number the other host expects.
|
---|
| 220 | * The notification is sent only upon a packet reecival.
|
---|
[21580dd] | 221 | */
|
---|
[aadf01e] | 222 | uint32_t expected;
|
---|
[89e57cee] | 223 |
|
---|
| 224 | /**
|
---|
| 225 | * Expected sequence number counter.
|
---|
| 226 | * Counts the number of received notifications for the same sequence
|
---|
| 227 | * number.
|
---|
[21580dd] | 228 | */
|
---|
[aadf01e] | 229 | int expected_count;
|
---|
[89e57cee] | 230 |
|
---|
[21580dd] | 231 | /** Incoming packet queue.
|
---|
[89e57cee] | 232 | *
|
---|
| 233 | * Packets are buffered until received in the right order.
|
---|
| 234 | * The packets are excluded after successfully read.
|
---|
| 235 | * Packets are sorted by their starting byte.
|
---|
| 236 | * Packets metric is set as their data length.
|
---|
[21580dd] | 237 | */
|
---|
[46d4d9f] | 238 | packet_t *incoming;
|
---|
[89e57cee] | 239 |
|
---|
[21580dd] | 240 | /** Outgoing packet queue.
|
---|
[89e57cee] | 241 | *
|
---|
| 242 | * Packets are buffered until acknowledged by the remote host in the
|
---|
| 243 | * right order.
|
---|
| 244 | * The packets are excluded after acknowledged.
|
---|
| 245 | * Packets are sorted by their starting byte.
|
---|
| 246 | * Packets metric is set as their data length.
|
---|
[21580dd] | 247 | */
|
---|
[46d4d9f] | 248 | packet_t *outgoing;
|
---|
[89e57cee] | 249 |
|
---|
| 250 | /** IP pseudo header. */
|
---|
[14f1db0] | 251 | void *pseudo_header;
|
---|
[89e57cee] | 252 | /** IP pseudo header length. */
|
---|
[aadf01e] | 253 | size_t headerlen;
|
---|
[89e57cee] | 254 | /** Remote host address. */
|
---|
| 255 | struct sockaddr *addr;
|
---|
| 256 | /** Remote host address length. */
|
---|
[aadf01e] | 257 | socklen_t addrlen;
|
---|
[89e57cee] | 258 | /** Remote host port. */
|
---|
[aadf01e] | 259 | uint16_t dest_port;
|
---|
[89e57cee] | 260 | /** Parent local sockets. */
|
---|
[aaa3f33a] | 261 | socket_cores_t *local_sockets;
|
---|
[89e57cee] | 262 |
|
---|
[21580dd] | 263 | /** Local sockets safety lock.
|
---|
[89e57cee] | 264 | *
|
---|
| 265 | * May be locked for writing while holding the global lock for reading
|
---|
| 266 | * when changing the local sockets only.
|
---|
| 267 | * The global lock may be locked only before locking the local lock.
|
---|
| 268 | * The global lock may be locked more weakly than the local lock.
|
---|
| 269 | * The global lock may be released before releasing the local lock.
|
---|
| 270 | * @see tcp_globals:lock
|
---|
| 271 | */
|
---|
| 272 | fibril_rwlock_t *local_lock;
|
---|
| 273 |
|
---|
| 274 | /** Pending operation data. */
|
---|
[aadf01e] | 275 | tcp_operation_t operation;
|
---|
[89e57cee] | 276 |
|
---|
| 277 | /**
|
---|
| 278 | * Timeouts in a row counter.
|
---|
| 279 | * If TCP_MAX_TIMEOUTS is reached, the connection is lost.
|
---|
[21580dd] | 280 | */
|
---|
[aadf01e] | 281 | int timeout_count;
|
---|
[21580dd] | 282 | };
|
---|
| 283 |
|
---|
[89e57cee] | 284 | /** TCP global data. */
|
---|
| 285 | struct tcp_globals {
|
---|
| 286 | /** Networking module phone. */
|
---|
[aadf01e] | 287 | int net_phone;
|
---|
[89e57cee] | 288 | /** IP module phone. */
|
---|
[aadf01e] | 289 | int ip_phone;
|
---|
[89e57cee] | 290 | /** ICMP module phone. */
|
---|
[aadf01e] | 291 | int icmp_phone;
|
---|
[89e57cee] | 292 | /** Last used free port. */
|
---|
[aadf01e] | 293 | int last_used_port;
|
---|
[89e57cee] | 294 | /** Active sockets. */
|
---|
[aadf01e] | 295 | socket_ports_t sockets;
|
---|
[89e57cee] | 296 | /** Device packet dimensions. */
|
---|
[aadf01e] | 297 | packet_dimensions_t dimensions;
|
---|
[89e57cee] | 298 |
|
---|
| 299 | /**
|
---|
| 300 | * Safety lock.
|
---|
| 301 | * Write lock is used only for adding or removing socket ports.
|
---|
[21580dd] | 302 | */
|
---|
[aadf01e] | 303 | fibril_rwlock_t lock;
|
---|
[21580dd] | 304 | };
|
---|
| 305 |
|
---|
| 306 | #endif
|
---|
| 307 |
|
---|
| 308 | /** @}
|
---|
| 309 | */
|
---|