| 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
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * TCP module.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef NET_TCP_H_
|
|---|
| 38 | #define NET_TCP_H_
|
|---|
| 39 |
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <net/packet.h>
|
|---|
| 43 | #include <net/device.h>
|
|---|
| 44 | #include <socket_core.h>
|
|---|
| 45 | #include <tl_common.h>
|
|---|
| 46 |
|
|---|
| 47 | /** Type definition of the TCP global data.
|
|---|
| 48 | * @see tcp_globals
|
|---|
| 49 | */
|
|---|
| 50 | typedef struct tcp_globals tcp_globals_t;
|
|---|
| 51 |
|
|---|
| 52 | /** Type definition of the TCP socket specific data.
|
|---|
| 53 | * @see tcp_socket_data
|
|---|
| 54 | */
|
|---|
| 55 | typedef struct tcp_socket_data tcp_socket_data_t;
|
|---|
| 56 |
|
|---|
| 57 | /** Type definition of the TCP socket specific data pointer.
|
|---|
| 58 | * @see tcp_socket_data
|
|---|
| 59 | */
|
|---|
| 60 | typedef tcp_socket_data_t *tcp_socket_data_ref;
|
|---|
| 61 |
|
|---|
| 62 | /** Type definition of the TCP operation data.
|
|---|
| 63 | * @see tcp_operation
|
|---|
| 64 | */
|
|---|
| 65 | typedef struct tcp_operation tcp_operation_t;
|
|---|
| 66 |
|
|---|
| 67 | /** Type definition of the TCP operation data pointer.
|
|---|
| 68 | * @see tcp_operation
|
|---|
| 69 | */
|
|---|
| 70 | typedef tcp_operation_t *tcp_operation_ref;
|
|---|
| 71 |
|
|---|
| 72 | /** TCP socket state type definition.
|
|---|
| 73 | * @see tcp_socket_state
|
|---|
| 74 | */
|
|---|
| 75 | typedef enum tcp_socket_state tcp_socket_state_t;
|
|---|
| 76 |
|
|---|
| 77 | /** TCP socket state. */
|
|---|
| 78 | enum tcp_socket_state {
|
|---|
| 79 | /** Initial.
|
|---|
| 80 | *
|
|---|
| 81 | * Not connected or bound.
|
|---|
| 82 | */
|
|---|
| 83 | TCP_SOCKET_INITIAL,
|
|---|
| 84 |
|
|---|
| 85 | /** Listening.
|
|---|
| 86 | *
|
|---|
| 87 | * Awaiting a connection request from another TCP layer.
|
|---|
| 88 | * When SYN is received a new bound socket in the
|
|---|
| 89 | * TCP_SOCKET_SYN_RECEIVED state should be created.
|
|---|
| 90 | */
|
|---|
| 91 | TCP_SOCKET_LISTEN,
|
|---|
| 92 |
|
|---|
| 93 | /** Connecting issued.
|
|---|
| 94 | *
|
|---|
| 95 | * A SYN has been sent, and TCP is awaiting the response SYN.
|
|---|
| 96 | * Should continue to the TCP_SOCKET_ESTABLISHED state.
|
|---|
| 97 | */
|
|---|
| 98 | TCP_SOCKET_SYN_SENT,
|
|---|
| 99 |
|
|---|
| 100 | /** Connecting received.
|
|---|
| 101 | *
|
|---|
| 102 | * A SYN has been received, a SYN has been sent, and TCP is awaiting an
|
|---|
| 103 | * ACK. Should continue to the TCP_SOCKET_ESTABLISHED state.
|
|---|
| 104 | */
|
|---|
| 105 | TCP_SOCKET_SYN_RECEIVED,
|
|---|
| 106 |
|
|---|
| 107 | /** Connected.
|
|---|
| 108 | *
|
|---|
| 109 | * The three-way handshake has been completed.
|
|---|
| 110 | */
|
|---|
| 111 | TCP_SOCKET_ESTABLISHED,
|
|---|
| 112 |
|
|---|
| 113 | /** Closing started.
|
|---|
| 114 | *
|
|---|
| 115 | * The local application has issued a CLOSE.
|
|---|
| 116 | * TCP has sent a FIN, and is awaiting an ACK or a FIN.
|
|---|
| 117 | * Should continue to the TCP_SOCKET_FIN_WAIT_2 state when an ACK is
|
|---|
| 118 | * received.
|
|---|
| 119 | * Should continue to the TCP_SOCKET_CLOSING state when a FIN is
|
|---|
| 120 | * received.
|
|---|
| 121 | */
|
|---|
| 122 | TCP_SOCKET_FIN_WAIT_1,
|
|---|
| 123 |
|
|---|
| 124 | /** Closing confirmed.
|
|---|
| 125 | *
|
|---|
| 126 | * A FIN has been sent, and an ACK received.
|
|---|
| 127 | * TCP is awaiting a~FIN from the remote TCP layer.
|
|---|
| 128 | * Should continue to the TCP_SOCKET_CLOSING state.
|
|---|
| 129 | */
|
|---|
| 130 | TCP_SOCKET_FIN_WAIT_2,
|
|---|
| 131 |
|
|---|
| 132 | /** Closing.
|
|---|
| 133 | *
|
|---|
| 134 | * A FIN has been sent, a FIN has been received, and an ACK has been
|
|---|
| 135 | * sent.
|
|---|
| 136 | * TCP is awaiting an ACK for the FIN that was sent.
|
|---|
| 137 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
|---|
| 138 | */
|
|---|
| 139 | TCP_SOCKET_CLOSING,
|
|---|
| 140 |
|
|---|
| 141 | /** Closing received.
|
|---|
| 142 | *
|
|---|
| 143 | * TCP has received a FIN, and has sent an ACK.
|
|---|
| 144 | * It is awaiting a close request from the local application before
|
|---|
| 145 | * sending a FIN.
|
|---|
| 146 | * Should continue to the TCP_SOCKET_SOCKET_LAST_ACK state.
|
|---|
| 147 | */
|
|---|
| 148 | TCP_SOCKET_CLOSE_WAIT,
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * A FIN has been received, and an ACK and a FIN have been sent.
|
|---|
| 152 | * TCP is awaiting an ACK.
|
|---|
| 153 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
|---|
| 154 | */
|
|---|
| 155 | TCP_SOCKET_LAST_ACK,
|
|---|
| 156 |
|
|---|
| 157 | /** Closing finished.
|
|---|
| 158 | *
|
|---|
| 159 | * FINs have been received and ACK’d, and TCP is waiting two MSLs to
|
|---|
| 160 | * remove the connection from the table.
|
|---|
| 161 | */
|
|---|
| 162 | TCP_SOCKET_TIME_WAIT,
|
|---|
| 163 |
|
|---|
| 164 | /** Closed.
|
|---|
| 165 | *
|
|---|
| 166 | * Imaginary, this indicates that a connection has been removed from
|
|---|
| 167 | * the connection table.
|
|---|
| 168 | */
|
|---|
| 169 | TCP_SOCKET_CLOSED
|
|---|
| 170 | };
|
|---|
| 171 |
|
|---|
| 172 | /** TCP operation data. */
|
|---|
| 173 | struct tcp_operation {
|
|---|
| 174 | /** Operation result. */
|
|---|
| 175 | int result;
|
|---|
| 176 | /** Safety lock. */
|
|---|
| 177 | fibril_mutex_t mutex;
|
|---|
| 178 | /** Operation result signaling. */
|
|---|
| 179 | fibril_condvar_t condvar;
|
|---|
| 180 | };
|
|---|
| 181 |
|
|---|
| 182 | /** TCP socket specific data. */
|
|---|
| 183 | struct tcp_socket_data {
|
|---|
| 184 | /** TCP socket state. */
|
|---|
| 185 | tcp_socket_state_t state;
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Data fragment size.
|
|---|
| 189 | * Sending optimalization.
|
|---|
| 190 | */
|
|---|
| 191 | size_t data_fragment_size;
|
|---|
| 192 |
|
|---|
| 193 | /** Device identifier. */
|
|---|
| 194 | device_id_t device_id;
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Listening backlog.
|
|---|
| 198 | * The maximal number of connected but not yet accepted sockets.
|
|---|
| 199 | */
|
|---|
| 200 | int backlog;
|
|---|
| 201 |
|
|---|
| 202 | // /** Segment size. */
|
|---|
| 203 | // size_t segment_size;
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Parent listening socket identifier.
|
|---|
| 207 | * Set if this socket is an accepted one.
|
|---|
| 208 | */
|
|---|
| 209 | int listening_socket_id;
|
|---|
| 210 |
|
|---|
| 211 | /** Treshold size in bytes. */
|
|---|
| 212 | size_t treshold;
|
|---|
| 213 | /** Window size in bytes. */
|
|---|
| 214 | size_t window;
|
|---|
| 215 | /** Acknowledgement timeout. */
|
|---|
| 216 | suseconds_t timeout;
|
|---|
| 217 | /** Last acknowledged byte. */
|
|---|
| 218 | uint32_t acknowledged;
|
|---|
| 219 | /** Next incoming sequence number. */
|
|---|
| 220 | uint32_t next_incoming;
|
|---|
| 221 | /** Incoming FIN. */
|
|---|
| 222 | uint32_t fin_incoming;
|
|---|
| 223 | /** Next outgoing sequence number. */
|
|---|
| 224 | uint32_t next_outgoing;
|
|---|
| 225 | /** Last outgoing sequence number. */
|
|---|
| 226 | uint32_t last_outgoing;
|
|---|
| 227 | /** Outgoing FIN. */
|
|---|
| 228 | uint32_t fin_outgoing;
|
|---|
| 229 |
|
|---|
| 230 | /**
|
|---|
| 231 | * Expected sequence number by the remote host.
|
|---|
| 232 | * The sequence number the other host expects.
|
|---|
| 233 | * The notification is sent only upon a packet reecival.
|
|---|
| 234 | */
|
|---|
| 235 | uint32_t expected;
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Expected sequence number counter.
|
|---|
| 239 | * Counts the number of received notifications for the same sequence
|
|---|
| 240 | * number.
|
|---|
| 241 | */
|
|---|
| 242 | int expected_count;
|
|---|
| 243 |
|
|---|
| 244 | /** Incoming packet queue.
|
|---|
| 245 | *
|
|---|
| 246 | * Packets are buffered until received in the right order.
|
|---|
| 247 | * The packets are excluded after successfully read.
|
|---|
| 248 | * Packets are sorted by their starting byte.
|
|---|
| 249 | * Packets metric is set as their data length.
|
|---|
| 250 | */
|
|---|
| 251 | packet_t incoming;
|
|---|
| 252 |
|
|---|
| 253 | /** Outgoing packet queue.
|
|---|
| 254 | *
|
|---|
| 255 | * Packets are buffered until acknowledged by the remote host in the
|
|---|
| 256 | * right order.
|
|---|
| 257 | * The packets are excluded after acknowledged.
|
|---|
| 258 | * Packets are sorted by their starting byte.
|
|---|
| 259 | * Packets metric is set as their data length.
|
|---|
| 260 | */
|
|---|
| 261 | packet_t outgoing;
|
|---|
| 262 |
|
|---|
| 263 | /** IP pseudo header. */
|
|---|
| 264 | void *pseudo_header;
|
|---|
| 265 | /** IP pseudo header length. */
|
|---|
| 266 | size_t headerlen;
|
|---|
| 267 | /** Remote host address. */
|
|---|
| 268 | struct sockaddr *addr;
|
|---|
| 269 | /** Remote host address length. */
|
|---|
| 270 | socklen_t addrlen;
|
|---|
| 271 | /** Remote host port. */
|
|---|
| 272 | uint16_t dest_port;
|
|---|
| 273 | /** Parent local sockets. */
|
|---|
| 274 | socket_cores_ref local_sockets;
|
|---|
| 275 |
|
|---|
| 276 | /** Local sockets safety lock.
|
|---|
| 277 | *
|
|---|
| 278 | * May be locked for writing while holding the global lock for reading
|
|---|
| 279 | * when changing the local sockets only.
|
|---|
| 280 | * The global lock may be locked only before locking the local lock.
|
|---|
| 281 | * The global lock may be locked more weakly than the local lock.
|
|---|
| 282 | * The global lock may be released before releasing the local lock.
|
|---|
| 283 | * @see tcp_globals:lock
|
|---|
| 284 | */
|
|---|
| 285 | fibril_rwlock_t *local_lock;
|
|---|
| 286 |
|
|---|
| 287 | /** Pending operation data. */
|
|---|
| 288 | tcp_operation_t operation;
|
|---|
| 289 |
|
|---|
| 290 | /**
|
|---|
| 291 | * Timeouts in a row counter.
|
|---|
| 292 | * If TCP_MAX_TIMEOUTS is reached, the connection is lost.
|
|---|
| 293 | */
|
|---|
| 294 | int timeout_count;
|
|---|
| 295 | };
|
|---|
| 296 |
|
|---|
| 297 | /** TCP global data. */
|
|---|
| 298 | struct tcp_globals {
|
|---|
| 299 | /** Networking module phone. */
|
|---|
| 300 | int net_phone;
|
|---|
| 301 | /** IP module phone. */
|
|---|
| 302 | int ip_phone;
|
|---|
| 303 | /** ICMP module phone. */
|
|---|
| 304 | int icmp_phone;
|
|---|
| 305 | /** Last used free port. */
|
|---|
| 306 | int last_used_port;
|
|---|
| 307 | /** Active sockets. */
|
|---|
| 308 | socket_ports_t sockets;
|
|---|
| 309 | /** Device packet dimensions. */
|
|---|
| 310 | packet_dimensions_t dimensions;
|
|---|
| 311 |
|
|---|
| 312 | /**
|
|---|
| 313 | * Safety lock.
|
|---|
| 314 | * Write lock is used only for adding or removing socket ports.
|
|---|
| 315 | */
|
|---|
| 316 | fibril_rwlock_t lock;
|
|---|
| 317 | };
|
|---|
| 318 |
|
|---|
| 319 | #endif
|
|---|
| 320 |
|
|---|
| 321 | /** @}
|
|---|
| 322 | */
|
|---|