Rev 4850 | Rev 5133 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
||
7 | ;; ;; |
||
8 | ;; Written by hidnplayr@kolibrios.org ;; |
||
9 | ;; ;; |
||
10 | ;; Based on the code of 4.4BSD ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
16 | |||
4850 | mario79 | 17 | $Revision: 5000 $ |
3545 | hidnplayr | 18 | |
19 | ;----------------------------------------------------------------- |
||
20 | ; |
||
21 | ; TCP_input: |
||
22 | ; |
||
23 | ; Add a segment to the incoming TCP queue |
||
24 | ; |
||
25 | ; IN: [esp] = ptr to buffer |
||
26 | ; [esp+4] = buffer size (dont care) |
||
27 | ; ebx = ptr to device struct |
||
28 | ; ecx = segment size |
||
29 | ; esi = ptr to TCP segment |
||
30 | ; edi = ptr to ipv4 source address, followed by ipv4 dest address |
||
31 | ; |
||
32 | ; OUT: / |
||
33 | ; |
||
34 | ;----------------------------------------------------------------- |
||
35 | |||
36 | align 4 |
||
37 | TCP_input: |
||
38 | |||
39 | ; record the current time |
||
40 | mov eax, [timer_ticks] ; in 1/100 seconds |
||
41 | mov [esp + 4], eax |
||
42 | |||
43 | push ebx ecx esi edi ; mind the order |
||
44 | mov esi, esp |
||
45 | |||
46 | add_to_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .fail |
||
47 | add esp, sizeof.TCP_queue_entry |
||
48 | |||
3644 | hidnplayr | 49 | call NET_ptr_to_num4 |
50 | inc [TCP_segments_rx + edi] |
||
51 | |||
3545 | hidnplayr | 52 | xor edx, edx |
53 | mov eax, [TCP_input_event] |
||
54 | mov ebx, [eax + EVENT.id] |
||
55 | xor esi, esi |
||
56 | call raise_event |
||
57 | |||
58 | ret |
||
59 | |||
60 | .fail: |
||
3556 | hidnplayr | 61 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP incoming queue is full, discarding packet!\n" |
3545 | hidnplayr | 62 | |
3644 | hidnplayr | 63 | call NET_ptr_to_num4 |
64 | inc [TCP_segments_missed + edi] |
||
3545 | hidnplayr | 65 | |
66 | add esp, sizeof.TCP_queue_entry - 8 |
||
3861 | hidnplayr | 67 | call NET_packet_free |
3545 | hidnplayr | 68 | add esp, 4 |
69 | |||
70 | ret |
||
71 | |||
72 | |||
73 | |||
74 | |||
75 | align 4 |
||
4339 | hidnplayr | 76 | proc TCP_process_input |
3545 | hidnplayr | 77 | |
4339 | hidnplayr | 78 | locals |
79 | dataoffset dd ? |
||
4344 | hidnplayr | 80 | timestamp dd ? |
4347 | hidnplayr | 81 | temp_bits db ? |
4339 | hidnplayr | 82 | endl |
83 | |||
3545 | hidnplayr | 84 | xor esi, esi |
85 | mov ecx, MANUAL_DESTROY |
||
86 | call create_event |
||
87 | mov [TCP_input_event], eax |
||
88 | |||
89 | .wait: |
||
90 | mov eax, [TCP_input_event] |
||
91 | mov ebx, [eax + EVENT.id] |
||
92 | call wait_event |
||
93 | |||
94 | .loop: |
||
95 | get_from_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .wait |
||
96 | |||
97 | push [esi + TCP_queue_entry.timestamp] |
||
4344 | hidnplayr | 98 | pop [timestamp] |
3545 | hidnplayr | 99 | push [esi + TCP_queue_entry.buffer_ptr] |
100 | |||
101 | mov ebx, [esi + TCP_queue_entry.device_ptr] |
||
102 | mov ecx, [esi + TCP_queue_entry.segment_size] |
||
103 | mov edi, [esi + TCP_queue_entry.ip_ptr] ; ptr to ipv4 source address, followed by ipv4 destination address |
||
104 | mov esi, [esi + TCP_queue_entry.segment_ptr] ; change esi last |
||
105 | |||
3556 | hidnplayr | 106 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: size=%u time=%d\n", ecx, [timer_ticks] |
3545 | hidnplayr | 107 | |
108 | mov edx, esi |
||
109 | |||
110 | cmp ebx, LOOPBACK_DEVICE |
||
111 | je .checksum_ok |
||
112 | |||
113 | ; re-calculate the checksum (if not already done by hw) |
||
4388 | hidnplayr | 114 | test [ebx + NET_DEVICE.hwacc], NET_HWACC_TCP_IPv4_IN |
115 | jnz .checksum_ok |
||
3545 | hidnplayr | 116 | |
117 | push ecx esi |
||
118 | pushw [esi + TCP_header.Checksum] |
||
119 | mov [esi + TCP_header.Checksum], 0 |
||
120 | TCP_checksum (edi), (edi+4) |
||
121 | pop cx ; previous checksum |
||
122 | cmp cx, dx |
||
123 | pop edx ecx |
||
124 | jne .drop_no_socket |
||
125 | .checksum_ok: |
||
126 | |||
127 | ; Verify the data offset |
||
4339 | hidnplayr | 128 | movzx eax, [edx + TCP_header.DataOffset] |
129 | and al, 0xf0 ; Calculate TCP segment header size (throwing away unused reserved bits in TCP header) |
||
130 | shr al, 2 |
||
131 | cmp al, sizeof.TCP_header ; Now see if it's at least the size of a standard TCP header |
||
132 | jb .drop_no_socket ; If not, drop the packet |
||
133 | mov [dataoffset], eax |
||
3545 | hidnplayr | 134 | |
135 | sub ecx, eax ; substract TCP header size from total segment size |
||
136 | jb .drop_no_socket ; If total segment size is less then the advertised header size, drop packet |
||
3556 | hidnplayr | 137 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes of data\n", ecx |
3545 | hidnplayr | 138 | |
139 | ;------------------------------------------- |
||
140 | ; Convert Big-endian values to little endian |
||
141 | |||
142 | ntohd [edx + TCP_header.SequenceNumber] |
||
143 | ntohd [edx + TCP_header.AckNumber] |
||
144 | |||
145 | ntohw [edx + TCP_header.Window] |
||
146 | ntohw [edx + TCP_header.UrgentPointer] |
||
147 | |||
148 | ;------------------------ |
||
149 | ; Find the socket pointer |
||
150 | |||
151 | ; IP Packet TCP Destination Port = local Port |
||
152 | ; (IP Packet SenderAddress = Remote IP) OR (Remote IP = 0) |
||
153 | ; (IP Packet TCP Source Port = remote Port) OR (remote Port = 0) |
||
154 | |||
155 | .findpcb: |
||
3647 | hidnplayr | 156 | pusha |
157 | mov ecx, socket_mutex |
||
158 | call mutex_lock |
||
159 | popa |
||
160 | |||
3545 | hidnplayr | 161 | mov ebx, net_sockets |
162 | mov si, [edx + TCP_header.DestinationPort] |
||
163 | |||
164 | .socket_loop: |
||
165 | mov ebx, [ebx + SOCKET.NextPtr] |
||
166 | or ebx, ebx |
||
3647 | hidnplayr | 167 | jz .no_socket ;respond_seg_reset |
3545 | hidnplayr | 168 | |
169 | cmp [ebx + SOCKET.Domain], AF_INET4 |
||
170 | jne .socket_loop |
||
171 | |||
172 | cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP |
||
173 | jne .socket_loop |
||
174 | |||
175 | cmp [ebx + TCP_SOCKET.LocalPort], si |
||
176 | jne .socket_loop |
||
177 | |||
178 | mov eax, [ebx + IP_SOCKET.RemoteIP] |
||
179 | cmp eax, [edi] ; Ipv4 source address |
||
180 | je @f |
||
181 | test eax, eax |
||
182 | jnz .socket_loop |
||
183 | @@: |
||
184 | |||
185 | mov ax, [ebx + TCP_SOCKET.RemotePort] |
||
186 | cmp [edx + TCP_header.SourcePort], ax |
||
187 | je .found_socket |
||
188 | test ax, ax |
||
189 | jnz .socket_loop |
||
190 | .found_socket: ; ebx now contains the socketpointer |
||
3647 | hidnplayr | 191 | pusha |
192 | mov ecx, socket_mutex |
||
193 | call mutex_unlock |
||
194 | popa |
||
195 | |||
3556 | hidnplayr | 196 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: socket ptr=%x state=%u flags=%x\n", ebx, [ebx + TCP_SOCKET.t_state], [edx + TCP_header.Flags]:2 |
3545 | hidnplayr | 197 | |
198 | ;---------------------------- |
||
199 | ; Check if socket isnt closed |
||
200 | |||
201 | cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSED |
||
202 | je .drop_no_socket |
||
203 | |||
204 | ;---------------- |
||
205 | ; Lock the socket |
||
206 | |||
207 | pusha |
||
208 | lea ecx, [ebx + SOCKET.mutex] |
||
209 | call mutex_lock |
||
210 | popa |
||
211 | |||
3556 | hidnplayr | 212 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: socket locked\n" |
3545 | hidnplayr | 213 | |
214 | ;--------------------------- |
||
215 | ; disable all temporary bits |
||
216 | |||
4347 | hidnplayr | 217 | mov [temp_bits], 0 |
3545 | hidnplayr | 218 | |
219 | ;--------------------------------------- |
||
220 | ; unscale the window into a 32 bit value |
||
221 | |||
222 | movzx eax, [edx + TCP_header.Window] |
||
223 | push ecx |
||
224 | mov cl, [ebx + TCP_SOCKET.SND_SCALE] |
||
225 | shl eax, cl |
||
226 | mov dword [edx + TCP_header.Window], eax ; word after window is checksum, we dont need checksum anymore |
||
227 | pop ecx |
||
228 | |||
229 | ;--------------------------------------- |
||
230 | ; Are we accepting incoming connections? |
||
231 | |||
232 | test [ebx + SOCKET.options], SO_ACCEPTCON |
||
233 | jz .no_accept |
||
234 | |||
3556 | hidnplayr | 235 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Accepting new connection\n" |
3545 | hidnplayr | 236 | |
237 | pusha |
||
238 | lea ecx, [ebx + SOCKET.mutex] |
||
239 | call mutex_unlock |
||
240 | popa |
||
241 | |||
242 | push ecx edx esi edi ;;; |
||
243 | call SOCKET_fork |
||
244 | pop edi esi edx ecx |
||
245 | |||
246 | test eax, eax |
||
247 | jz .drop_no_socket |
||
248 | |||
249 | mov ebx, eax |
||
250 | |||
4347 | hidnplayr | 251 | mov [temp_bits], TCP_BIT_DROPSOCKET |
3545 | hidnplayr | 252 | |
253 | push dword [edi + 4] ; Ipv4 destination addres |
||
254 | pop [ebx + IP_SOCKET.LocalIP] |
||
255 | |||
256 | push [edx + TCP_header.DestinationPort] |
||
257 | pop [ebx + TCP_SOCKET.LocalPort] |
||
258 | |||
259 | mov [ebx + TCP_SOCKET.t_state], TCPS_LISTEN |
||
260 | .no_accept: |
||
261 | |||
262 | |||
263 | ;------------------------------------- |
||
264 | ; Reset idle timer and keepalive timer |
||
265 | |||
266 | mov [ebx + TCP_SOCKET.t_idle], 0 |
||
267 | mov [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_idle |
||
3600 | hidnplayr | 268 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive |
3545 | hidnplayr | 269 | |
270 | ;-------------------- |
||
271 | ; Process TCP options |
||
272 | |||
4339 | hidnplayr | 273 | ;;; FIXME: for LISTEN, options should be called after we determined route, we need it for MSS |
274 | ;;; cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN ; no options when in listen state |
||
275 | ;;; jz .not_uni_xfer ; also no header prediction |
||
276 | |||
3545 | hidnplayr | 277 | push ecx |
278 | |||
4339 | hidnplayr | 279 | mov ecx, [dataoffset] |
3545 | hidnplayr | 280 | cmp ecx, sizeof.TCP_header ; Does header contain any options? |
281 | je .no_options |
||
282 | |||
3556 | hidnplayr | 283 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Segment has options\n" |
3545 | hidnplayr | 284 | |
285 | add ecx, edx |
||
286 | lea esi, [edx + sizeof.TCP_header] |
||
287 | |||
288 | .opt_loop: |
||
289 | cmp esi, ecx ; are we scanning outside of header? |
||
290 | jae .no_options |
||
291 | lodsb |
||
292 | cmp al, TCP_OPT_EOL ; end of option list? |
||
293 | je .no_options |
||
294 | cmp al, TCP_OPT_NOP |
||
295 | je .opt_loop |
||
296 | cmp al, TCP_OPT_MAXSEG |
||
297 | je .opt_maxseg |
||
298 | cmp al, TCP_OPT_WINDOW |
||
299 | je .opt_window |
||
300 | cmp al, TCP_OPT_SACK_PERMIT |
||
301 | je .opt_sack_permit |
||
302 | ; cmp al, TCP_OPT_SACK |
||
303 | ; je .opt_sack |
||
304 | cmp al, TCP_OPT_TIMESTAMP |
||
305 | je .opt_timestamp |
||
3556 | hidnplayr | 306 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: unknown option:%u\n", al |
3545 | hidnplayr | 307 | jmp .no_options ; If we reach here, some unknown options were received, skip them all! |
308 | |||
309 | .opt_maxseg: |
||
310 | lodsb |
||
311 | cmp al, 4 |
||
312 | jne .no_options ; error occured, ignore all options! |
||
313 | |||
314 | test [edx + TCP_header.Flags], TH_SYN |
||
315 | jz @f |
||
316 | |||
4339 | hidnplayr | 317 | xor eax, eax |
3545 | hidnplayr | 318 | lodsw |
319 | rol ax, 8 |
||
4339 | hidnplayr | 320 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Maxseg=%u\n", eax |
3545 | hidnplayr | 321 | call TCP_mss |
322 | @@: |
||
323 | jmp .opt_loop |
||
324 | |||
325 | |||
326 | .opt_window: |
||
327 | lodsb |
||
328 | cmp al, 3 |
||
329 | jne .no_options |
||
330 | |||
331 | test [edx + TCP_header.Flags], TH_SYN |
||
332 | jz @f |
||
333 | |||
3556 | hidnplayr | 334 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got window scale option\n" |
3545 | hidnplayr | 335 | or [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE |
336 | |||
337 | lodsb |
||
338 | mov [ebx + TCP_SOCKET.SND_SCALE], al |
||
339 | ;;;;; TODO |
||
340 | |||
341 | @@: |
||
342 | jmp .opt_loop |
||
343 | |||
344 | |||
345 | .opt_sack_permit: |
||
346 | lodsb |
||
347 | cmp al, 2 |
||
348 | jne .no_options |
||
349 | |||
350 | test [edx + TCP_header.Flags], TH_SYN |
||
351 | jz @f |
||
352 | |||
3556 | hidnplayr | 353 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Selective Acknowledgement permitted\n" |
3545 | hidnplayr | 354 | or [ebx + TCP_SOCKET.t_flags], TF_SACK_PERMIT |
355 | |||
356 | @@: |
||
357 | jmp .opt_loop |
||
358 | |||
359 | |||
360 | .opt_timestamp: |
||
361 | lodsb |
||
362 | cmp al, 10 ; length must be 10 |
||
363 | jne .no_options |
||
364 | |||
3556 | hidnplayr | 365 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got timestamp option\n" |
3545 | hidnplayr | 366 | |
367 | test [edx + TCP_header.Flags], TH_SYN |
||
368 | jz @f |
||
369 | or [ebx + TCP_SOCKET.t_flags], TF_RCVD_TSTMP |
||
370 | @@: |
||
371 | |||
372 | lodsd |
||
4344 | hidnplayr | 373 | bswap eax |
3545 | hidnplayr | 374 | mov [ebx + TCP_SOCKET.ts_val], eax |
375 | lodsd ; timestamp echo reply |
||
376 | mov [ebx + TCP_SOCKET.ts_ecr], eax |
||
4347 | hidnplayr | 377 | or [temp_bits], TCP_BIT_TIMESTAMP |
3545 | hidnplayr | 378 | |
379 | ; Since we have a timestamp, lets do the paws test right away! |
||
380 | |||
381 | test [edx + TCP_header.Flags], TH_RST |
||
382 | jnz .no_paws |
||
383 | |||
384 | mov eax, [ebx + TCP_SOCKET.ts_recent] |
||
385 | test eax, eax |
||
386 | jz .no_paws |
||
387 | cmp eax, [ebx + TCP_SOCKET.ts_val] |
||
4344 | hidnplayr | 388 | jbe .no_paws |
3545 | hidnplayr | 389 | |
3556 | hidnplayr | 390 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: PAWS: detected an old segment\n" |
3545 | hidnplayr | 391 | |
4344 | hidnplayr | 392 | mov eax, [timestamp] |
3545 | hidnplayr | 393 | sub eax, [ebx + TCP_SOCKET.ts_recent_age] |
394 | |||
395 | pop ecx |
||
396 | cmp eax, TCP_PAWS_IDLE |
||
397 | jle .drop_after_ack ; TODO: update stats |
||
398 | push ecx |
||
399 | |||
400 | mov [ebx + TCP_SOCKET.ts_recent], 0 ; timestamp was invalid, fix it. |
||
401 | .no_paws: |
||
402 | jmp .opt_loop |
||
403 | |||
404 | .no_options: |
||
405 | |||
406 | pop ecx |
||
407 | |||
408 | ;----------------------------------------------------------------------- |
||
409 | ; Time to do some header prediction (Original Principle by Van Jacobson) |
||
410 | |||
411 | ; There are two common cases for an uni-directional data transfer. |
||
412 | ; |
||
413 | ; General rule: the packets has no control flags, is in-sequence, |
||
414 | ; window width didnt change and we're not retransmitting. |
||
415 | ; |
||
416 | ; Second rules: |
||
417 | ; - If the length is 0 and the ACK moved forward, we're the sender side of the transfer. |
||
418 | ; In this case we'll free the ACK'ed data and notify higher levels that we have free space in buffer |
||
419 | ; |
||
420 | ; - If the length is not 0 and the ACK didn't move, we're the receiver side of the transfer. |
||
421 | ; If the packets are in order (data queue is empty), add the data to the socket buffer and request a delayed ACK |
||
422 | |||
423 | cmp [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED |
||
424 | jnz .not_uni_xfer |
||
425 | |||
426 | test [edx + TCP_header.Flags], TH_SYN + TH_FIN + TH_RST + TH_URG |
||
427 | jnz .not_uni_xfer |
||
428 | |||
429 | test [edx + TCP_header.Flags], TH_ACK |
||
430 | jz .not_uni_xfer |
||
431 | |||
432 | mov eax, [edx + TCP_header.SequenceNumber] |
||
433 | cmp eax, [ebx + TCP_SOCKET.RCV_NXT] |
||
434 | jne .not_uni_xfer |
||
435 | |||
436 | mov eax, dword [edx + TCP_header.Window] |
||
437 | cmp eax, [ebx + TCP_SOCKET.SND_WND] |
||
438 | jne .not_uni_xfer |
||
439 | |||
440 | mov eax, [ebx + TCP_SOCKET.SND_NXT] |
||
441 | cmp eax, [ebx + TCP_SOCKET.SND_MAX] |
||
442 | jne .not_uni_xfer |
||
443 | |||
444 | ;--------------------------------------- |
||
445 | ; check if we are sender in the uni-xfer |
||
446 | |||
447 | ; If the following 4 conditions are all true, this segment is a pure ACK. |
||
448 | ; |
||
449 | ; - The segment contains no data. |
||
450 | test ecx, ecx |
||
451 | jnz .not_sender |
||
452 | |||
453 | ; - The congestion window is greater than or equal to the current send window. |
||
454 | ; This test is true only if the window is fully open, that is, the connection is not in the middle of slow start or congestion avoidance. |
||
455 | mov eax, [ebx + TCP_SOCKET.SND_CWND] |
||
456 | cmp eax, [ebx + TCP_SOCKET.SND_WND] |
||
457 | jb .not_uni_xfer |
||
458 | |||
459 | ; - The acknowledgment field in the segment is less than or equal to the maximum sequence number sent. |
||
460 | mov eax, [edx + TCP_header.AckNumber] |
||
461 | cmp eax, [ebx + TCP_SOCKET.SND_MAX] |
||
462 | ja .not_uni_xfer |
||
463 | |||
464 | ; - The acknowledgment field in the segment is greater than the largest unacknowledged sequence number. |
||
465 | sub eax, [ebx + TCP_SOCKET.SND_UNA] |
||
466 | jbe .not_uni_xfer |
||
467 | |||
3556 | hidnplayr | 468 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are sender\n" |
3545 | hidnplayr | 469 | |
470 | ;--------------------------------- |
||
471 | ; Packet is a pure ACK, process it |
||
472 | |||
473 | ; Delete acknowledged bytes from send buffer |
||
474 | pusha |
||
475 | mov ecx, eax |
||
476 | lea eax, [ebx + STREAM_SOCKET.snd] |
||
477 | call SOCKET_ring_free |
||
478 | popa |
||
479 | |||
480 | ; Update RTT estimators |
||
481 | |||
4347 | hidnplayr | 482 | test [temp_bits], TCP_BIT_TIMESTAMP |
3545 | hidnplayr | 483 | jz .no_timestamp_rtt |
4344 | hidnplayr | 484 | mov eax, [timestamp] |
3545 | hidnplayr | 485 | sub eax, [ebx + TCP_SOCKET.ts_ecr] |
486 | inc eax |
||
487 | call TCP_xmit_timer |
||
488 | jmp .rtt_done |
||
489 | |||
490 | .no_timestamp_rtt: |
||
491 | cmp [ebx + TCP_SOCKET.t_rtt], 0 |
||
492 | je .rtt_done |
||
493 | mov eax, [edx + TCP_header.AckNumber] |
||
494 | cmp eax, [ebx + TCP_SOCKET.t_rtseq] |
||
495 | jbe .rtt_done |
||
496 | mov eax, [ebx + TCP_SOCKET.t_rtt] |
||
497 | call TCP_xmit_timer |
||
498 | |||
499 | .rtt_done: |
||
500 | |||
501 | ; update window pointers |
||
502 | mov eax, [edx + TCP_header.AckNumber] |
||
503 | mov [ebx + TCP_SOCKET.SND_UNA], eax |
||
504 | |||
505 | ; Stop retransmit timer |
||
3600 | hidnplayr | 506 | and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission |
3545 | hidnplayr | 507 | |
508 | ; Unlock the socket |
||
509 | pusha |
||
510 | lea ecx, [ebx + SOCKET.mutex] |
||
511 | call mutex_unlock |
||
512 | popa |
||
513 | |||
514 | ; Awaken waiting processes |
||
515 | mov eax, ebx |
||
516 | call SOCKET_notify |
||
517 | |||
518 | ; Generate more output |
||
519 | call TCP_output |
||
520 | |||
521 | jmp .drop_no_socket |
||
522 | |||
523 | ;------------------------------------------------- |
||
524 | ; maybe we are the receiver in the uni-xfer then.. |
||
525 | |||
526 | .not_sender: |
||
527 | ; - The amount of data in the segment is greater than 0 (data count is in ecx) |
||
528 | |||
529 | ; - The acknowledgment field equals the largest unacknowledged sequence number. This means no data is acknowledged by this segment. |
||
530 | mov eax, [edx + TCP_header.AckNumber] |
||
531 | cmp eax, [ebx + TCP_SOCKET.SND_UNA] |
||
532 | jne .not_uni_xfer |
||
533 | |||
534 | ; - The reassembly list of out-of-order segments for the connection is empty (seg_next equals tp). |
||
535 | |||
536 | ;;; TODO |
||
537 | |||
538 | ; jnz .not_uni_xfer |
||
539 | |||
540 | ; Complete processing of received data |
||
541 | |||
3556 | hidnplayr | 542 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction: we are receiving %u bytes\n", ecx |
3545 | hidnplayr | 543 | |
4339 | hidnplayr | 544 | mov esi, [dataoffset] |
3545 | hidnplayr | 545 | add esi, edx |
546 | lea eax, [ebx + STREAM_SOCKET.rcv] |
||
547 | call SOCKET_ring_write ; Add the data to the socket buffer |
||
4344 | hidnplayr | 548 | add [ebx + TCP_SOCKET.RCV_NXT], ecx ; Update sequence number with number of bytes we have copied |
3545 | hidnplayr | 549 | |
550 | mov eax, ebx |
||
551 | call SOCKET_notify |
||
552 | |||
553 | or [ebx + TCP_SOCKET.t_flags], TF_DELACK ; Set delayed ack flag |
||
554 | |||
555 | jmp .drop |
||
556 | |||
557 | ;-------------------------------------------------- |
||
558 | ; Header prediction failed, do it the slow way |
||
559 | |||
560 | .not_uni_xfer: |
||
561 | |||
3556 | hidnplayr | 562 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction failed\n" |
3545 | hidnplayr | 563 | |
564 | ; Calculate receive window size |
||
565 | push edx |
||
4366 | hidnplayr | 566 | mov eax, SOCKET_MAXDATA |
3545 | hidnplayr | 567 | sub eax, [ebx + STREAM_SOCKET.rcv.size] |
4366 | hidnplayr | 568 | DEBUGF DEBUG_NETWORK_VERBOSE, "Space in receive buffer=%d\n", eax |
3545 | hidnplayr | 569 | mov edx, [ebx + TCP_SOCKET.RCV_ADV] |
570 | sub edx, [ebx + TCP_SOCKET.RCV_NXT] |
||
4366 | hidnplayr | 571 | DEBUGF DEBUG_NETWORK_VERBOSE, "Current advertised window=%d\n", edx |
3545 | hidnplayr | 572 | cmp eax, edx |
573 | jg @f |
||
574 | mov eax, edx |
||
575 | @@: |
||
3556 | hidnplayr | 576 | DEBUGF DEBUG_NETWORK_VERBOSE, "Receive window size=%d\n", eax |
3545 | hidnplayr | 577 | mov [ebx + TCP_SOCKET.RCV_WND], eax |
578 | pop edx |
||
579 | |||
580 | ; If we are in listen or syn_sent state, go to that specific code right away |
||
581 | |||
582 | cmp [ebx + TCP_SOCKET.t_state], TCPS_LISTEN |
||
583 | je .LISTEN |
||
584 | |||
585 | cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_SENT |
||
586 | je .SYN_SENT |
||
587 | |||
588 | ;---------------------------- |
||
589 | ; trim any data not in window |
||
590 | |||
4339 | hidnplayr | 591 | ; 1. Check for duplicate data at beginning of segment |
3545 | hidnplayr | 592 | |
4339 | hidnplayr | 593 | ; Calculate number of bytes we need to drop |
3545 | hidnplayr | 594 | mov eax, [ebx + TCP_SOCKET.RCV_NXT] |
595 | sub eax, [edx + TCP_header.SequenceNumber] |
||
596 | jle .no_duplicate |
||
597 | |||
3556 | hidnplayr | 598 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: %u bytes duplicate data!\n", eax |
3545 | hidnplayr | 599 | |
600 | test [edx + TCP_header.Flags], TH_SYN |
||
601 | jz .no_dup_syn |
||
602 | |||
3556 | hidnplayr | 603 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: got duplicate syn\n" |
3545 | hidnplayr | 604 | |
605 | and [edx + TCP_header.Flags], not (TH_SYN) |
||
606 | inc [edx + TCP_header.SequenceNumber] |
||
607 | |||
608 | cmp [edx + TCP_header.UrgentPointer], 1 |
||
609 | jbe @f |
||
610 | dec [edx + TCP_header.UrgentPointer] |
||
611 | jmp .dup_syn |
||
612 | @@: |
||
613 | and [edx + TCP_header.Flags], not (TH_URG) |
||
614 | .dup_syn: |
||
615 | dec eax |
||
616 | .no_dup_syn: |
||
617 | |||
4339 | hidnplayr | 618 | ; 2. Check for entire duplicate segment |
3545 | hidnplayr | 619 | cmp eax, ecx ; eax holds number of bytes to drop, ecx is data size |
620 | jb .duplicate |
||
621 | jnz @f |
||
622 | test [edx + TCP_header.Flags], TH_FIN |
||
623 | jnz .duplicate |
||
624 | @@: |
||
625 | |||
626 | ; Any valid FIN must be to the left of the window. |
||
627 | ; At this point the FIN must be out of sequence or a duplicate, drop it |
||
628 | and [edx + TCP_header.Flags], not TH_FIN |
||
629 | |||
630 | ; send an ACK and resynchronize and drop any data. |
||
631 | ; But keep on processing for RST or ACK |
||
632 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
||
633 | mov eax, ecx |
||
634 | |||
4339 | hidnplayr | 635 | ;;; TODO: update stats |
636 | |||
3545 | hidnplayr | 637 | ;----------------------------------------------- |
638 | ; Remove duplicate data and update urgent offset |
||
639 | |||
640 | .duplicate: |
||
4387 | hidnplayr | 641 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: trimming duplicate data\n" |
4339 | hidnplayr | 642 | |
643 | ; Trim data from left side of window |
||
644 | add [dataoffset], eax |
||
3545 | hidnplayr | 645 | add [edx + TCP_header.SequenceNumber], eax |
646 | sub ecx, eax |
||
647 | |||
648 | sub [edx + TCP_header.UrgentPointer], ax |
||
649 | jg @f |
||
650 | and [edx + TCP_header.Flags], not (TH_URG) |
||
651 | mov [edx + TCP_header.UrgentPointer], 0 |
||
652 | @@: |
||
653 | |||
654 | ;-------------------------------------------------- |
||
4339 | hidnplayr | 655 | ; Handle data that arrives after process terminates |
3545 | hidnplayr | 656 | |
657 | .no_duplicate: |
||
4339 | hidnplayr | 658 | cmp [ebx + SOCKET.PID], 0 ;;; TODO: use socket flags instead?? |
3545 | hidnplayr | 659 | jne .not_terminated |
660 | cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT |
||
661 | jbe .not_terminated |
||
662 | test ecx, ecx |
||
663 | jz .not_terminated |
||
664 | |||
665 | mov eax, ebx |
||
666 | call TCP_close |
||
4339 | hidnplayr | 667 | ;;; TODO: update stats |
3545 | hidnplayr | 668 | jmp .respond_seg_reset |
669 | |||
670 | ;---------------------------------------- |
||
4339 | hidnplayr | 671 | ; Remove data beyond right edge of window |
3545 | hidnplayr | 672 | |
673 | .not_terminated: |
||
674 | mov eax, [edx + TCP_header.SequenceNumber] |
||
675 | add eax, ecx |
||
676 | sub eax, [ebx + TCP_SOCKET.RCV_NXT] |
||
677 | sub eax, [ebx + TCP_SOCKET.RCV_WND] ; eax now holds the number of bytes to drop |
||
678 | jle .no_excess_data |
||
679 | |||
3556 | hidnplayr | 680 | DEBUGF DEBUG_NETWORK_VERBOSE, "%d bytes beyond right edge of window\n", eax |
3545 | hidnplayr | 681 | |
682 | ;;; TODO: update stats |
||
683 | cmp eax, ecx |
||
684 | jl .dont_drop_all |
||
685 | ; If a new connection request is received while in TIME_WAIT, drop the old connection and start over, |
||
686 | ; if the sequence numbers are above the previous ones |
||
687 | |||
688 | test [edx + TCP_header.Flags], TH_SYN |
||
689 | jz .no_new_request |
||
690 | cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
691 | jne .no_new_request |
||
692 | ; mov edx, [ebx + TCP_SOCKET.RCV_NXT] |
||
693 | ; cmp edx, [edx + TCP_header.SequenceNumber] |
||
694 | ; add edx, 64000 ; TCP_ISSINCR FIXME |
||
695 | mov eax, ebx |
||
696 | call TCP_close |
||
697 | jmp .findpcb ; FIXME: skip code for unscaling window, ... |
||
698 | .no_new_request: |
||
699 | |||
4339 | hidnplayr | 700 | ; If window is closed, we can only take segments at window edge, and have to drop data and PUSH from |
3545 | hidnplayr | 701 | ; incoming segments. Continue processing, but remember to ACK. Otherwise drop segment and ACK |
702 | |||
703 | cmp [ebx + TCP_SOCKET.RCV_WND], 0 |
||
704 | jne .drop_after_ack |
||
4339 | hidnplayr | 705 | mov esi, [edx + TCP_header.SequenceNumber] |
706 | cmp esi, [ebx + TCP_SOCKET.RCV_NXT] |
||
3545 | hidnplayr | 707 | jne .drop_after_ack |
708 | |||
709 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
||
710 | ;;; TODO: update stats |
||
711 | .dont_drop_all: |
||
712 | ;;; TODO: update stats |
||
4347 | hidnplayr | 713 | DEBUGF DEBUG_NETWORK_VERBOSE, "Trimming %u bytes from the right of the window\n" |
4339 | hidnplayr | 714 | sub ecx, eax ; remove data from the right side of window (decrease data length) |
4347 | hidnplayr | 715 | and [edx + TCP_header.Flags], not (TH_PUSH or TH_FIN) |
3545 | hidnplayr | 716 | .no_excess_data: |
717 | |||
718 | ;----------------- |
||
4339 | hidnplayr | 719 | ; Record timestamp |
3545 | hidnplayr | 720 | |
721 | ; If last ACK falls within this segments sequence numbers, record its timestamp |
||
4347 | hidnplayr | 722 | test [temp_bits], TCP_BIT_TIMESTAMP |
3545 | hidnplayr | 723 | jz .no_timestamp |
724 | mov eax, [ebx + TCP_SOCKET.last_ack_sent] |
||
725 | sub eax, [edx + TCP_header.SequenceNumber] |
||
726 | jb .no_timestamp |
||
727 | test [ebx + TCP_header.Flags], TH_SYN or TH_FIN ; syn and fin occupy one byte |
||
728 | jz @f |
||
729 | dec eax |
||
730 | @@: |
||
731 | sub eax, ecx |
||
732 | jae .no_timestamp |
||
733 | |||
3556 | hidnplayr | 734 | DEBUGF DEBUG_NETWORK_VERBOSE, "Recording timestamp\n" |
3545 | hidnplayr | 735 | |
4344 | hidnplayr | 736 | mov eax, [timestamp] |
3545 | hidnplayr | 737 | mov [ebx + TCP_SOCKET.ts_recent_age], eax |
738 | mov eax, [ebx + TCP_SOCKET.ts_val] |
||
739 | mov [ebx + TCP_SOCKET.ts_recent], eax |
||
740 | .no_timestamp: |
||
741 | |||
742 | ;------------------ |
||
743 | ; Process RST flags |
||
744 | |||
745 | test [edx + TCP_header.Flags], TH_RST |
||
746 | jz .no_rst |
||
747 | |||
3556 | hidnplayr | 748 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Got an RST flag\n" |
3545 | hidnplayr | 749 | |
750 | mov eax, [ebx + TCP_SOCKET.t_state] |
||
751 | shl eax, 2 |
||
752 | jmp dword [eax + .rst_sw_list] |
||
753 | |||
754 | .rst_sw_list: |
||
755 | dd .no_rst ; TCPS_CLOSED |
||
756 | dd .no_rst ; TCPS_LISTEN |
||
757 | dd .no_rst ; TCPS_SYN_SENT |
||
758 | dd .econnrefused ; TCPS_SYN_RECEIVED |
||
759 | dd .econnreset ; TCPS_ESTABLISHED |
||
760 | dd .econnreset ; TCPS_CLOSE_WAIT |
||
761 | dd .econnreset ; TCPS_FIN_WAIT_1 |
||
762 | dd .rst_close ; TCPS_CLOSING |
||
763 | dd .rst_close ; TCPS_LAST_ACK |
||
764 | dd .econnreset ; TCPS_FIN_WAIT_2 |
||
765 | dd .rst_close ; TCPS_TIMED_WAIT |
||
766 | |||
767 | .econnrefused: |
||
3556 | hidnplayr | 768 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Connection refused\n" |
3545 | hidnplayr | 769 | |
770 | mov [ebx + SOCKET.errorcode], ECONNREFUSED |
||
771 | jmp .close |
||
772 | |||
773 | .econnreset: |
||
3556 | hidnplayr | 774 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Connection reset\n" |
3545 | hidnplayr | 775 | |
776 | mov [ebx + SOCKET.errorcode], ECONNRESET |
||
777 | |||
778 | .close: |
||
3556 | hidnplayr | 779 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Closing connection\n" |
3545 | hidnplayr | 780 | |
781 | mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSED |
||
782 | ;;; TODO: update stats (tcp drops) |
||
783 | mov eax, ebx |
||
784 | call TCP_close |
||
785 | jmp .drop_no_socket |
||
786 | |||
787 | .rst_close: |
||
3556 | hidnplayr | 788 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Closing with reset\n" |
3545 | hidnplayr | 789 | |
790 | mov eax, ebx |
||
791 | call TCP_close |
||
792 | jmp .drop_no_socket |
||
793 | |||
794 | .no_rst: |
||
795 | |||
796 | ;-------------------------------------- |
||
797 | ; handle SYN-full and ACK-less segments |
||
798 | |||
799 | test [edx + TCP_header.Flags], TH_SYN |
||
800 | jz .not_syn_full |
||
801 | |||
802 | mov eax, ebx |
||
803 | mov ebx, ECONNRESET |
||
804 | call TCP_drop |
||
805 | jmp .drop_with_reset |
||
806 | .not_syn_full: |
||
807 | |||
808 | ;--------------- |
||
809 | ; ACK processing |
||
810 | |||
811 | test [edx + TCP_header.Flags], TH_ACK |
||
812 | jz .drop |
||
813 | |||
814 | cmp [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED |
||
815 | jb .ack_processed ; states: closed, listen, syn_sent |
||
816 | ja .no_syn_rcv ; established, fin_wait_1, fin_wait_2, close_wait, closing, last_ack, time_wait |
||
817 | |||
3556 | hidnplayr | 818 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_received\n" |
3545 | hidnplayr | 819 | |
820 | mov eax, [edx + TCP_header.AckNumber] |
||
821 | cmp [ebx + TCP_SOCKET.SND_UNA], eax |
||
822 | ja .drop_with_reset |
||
823 | cmp eax, [ebx + TCP_SOCKET.SND_MAX] |
||
824 | ja .drop_with_reset |
||
825 | |||
826 | ;;; TODO: update stats |
||
827 | |||
828 | mov eax, ebx |
||
829 | call SOCKET_is_connected |
||
830 | mov [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED |
||
831 | |||
832 | ; Do window scaling? |
||
833 | |||
834 | test [ebx + TCP_SOCKET.t_flags], TF_RCVD_SCALE |
||
835 | jz @f |
||
836 | test [ebx + TCP_SOCKET.t_flags], TF_REQ_SCALE |
||
837 | jz @f |
||
838 | |||
839 | push word [ebx + TCP_SOCKET.requested_s_scale] ; Set send and receive scale factors to the received values |
||
840 | pop word [ebx + TCP_SOCKET.SND_SCALE] |
||
841 | @@: |
||
842 | |||
843 | ;;; TODO: call TCP_reassemble |
||
844 | |||
845 | mov eax, [edx + TCP_header.SequenceNumber] |
||
846 | dec eax |
||
847 | mov [ebx + TCP_SOCKET.SND_WL1], eax |
||
848 | |||
849 | .no_syn_rcv: |
||
850 | |||
851 | ;------------------------- |
||
852 | ; check for duplicate ACKs |
||
853 | |||
854 | mov eax, [edx + TCP_header.AckNumber] |
||
855 | cmp eax, [ebx + TCP_SOCKET.SND_UNA] |
||
856 | ja .not_dup_ack |
||
857 | |||
858 | test ecx, ecx |
||
859 | jnz .reset_dupacks |
||
860 | |||
861 | mov eax, dword [edx + TCP_header.Window] |
||
862 | cmp eax, [ebx + TCP_SOCKET.SND_WND] |
||
863 | jne .reset_dupacks |
||
864 | |||
3556 | hidnplayr | 865 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Processing duplicate ACK\n" |
3545 | hidnplayr | 866 | |
867 | ; If we have outstanding data, other than a window probe, this is a completely duplicate ACK |
||
868 | ; (window info didnt change) The ACK is the biggest we've seen and we've seen exactly our rexmt threshold of them, |
||
869 | ; assume a packet has been dropped and retransmit it. Kludge snd_nxt & the congestion window so we send only this one packet. |
||
870 | |||
3600 | hidnplayr | 871 | test [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission |
872 | jz @f |
||
3545 | hidnplayr | 873 | |
874 | mov eax, [edx + TCP_header.AckNumber] |
||
875 | cmp eax, [ebx + TCP_SOCKET.SND_UNA] |
||
876 | je .dup_ack |
||
877 | |||
878 | @@: |
||
879 | mov [ebx + TCP_SOCKET.t_dupacks], 0 |
||
880 | jmp .not_dup_ack |
||
881 | |||
882 | .dup_ack: |
||
883 | inc [ebx + TCP_SOCKET.t_dupacks] |
||
884 | cmp [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh |
||
885 | jne .no_re_xmit |
||
886 | |||
887 | push [ebx + TCP_SOCKET.SND_NXT] ; >>>> |
||
888 | |||
889 | mov eax, [ebx + TCP_SOCKET.SND_WND] |
||
890 | cmp eax, [ebx + TCP_SOCKET.SND_CWND] |
||
4296 | hidnplayr | 891 | jbe @f |
892 | mov eax, [ebx + TCP_SOCKET.SND_CWND] |
||
893 | @@: |
||
3545 | hidnplayr | 894 | shr eax, 1 |
895 | push edx |
||
896 | xor edx, edx |
||
897 | div [ebx + TCP_SOCKET.t_maxseg] |
||
898 | cmp eax, 2 |
||
899 | ja @f |
||
900 | xor eax, eax |
||
901 | mov al, 2 |
||
902 | @@: |
||
903 | mul [ebx + TCP_SOCKET.t_maxseg] |
||
904 | pop edx |
||
905 | mov [ebx + TCP_SOCKET.SND_SSTHRESH], eax |
||
906 | |||
3600 | hidnplayr | 907 | and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission ; turn off retransmission timer |
3545 | hidnplayr | 908 | mov [ebx + TCP_SOCKET.t_rtt], 0 |
909 | mov eax, [edx + TCP_header.AckNumber] |
||
910 | mov [ebx + TCP_SOCKET.SND_NXT], eax |
||
911 | mov eax, [ebx + TCP_SOCKET.t_maxseg] |
||
912 | mov [ebx + TCP_SOCKET.SND_CWND], eax |
||
913 | |||
914 | ; Unlock the socket |
||
915 | push ebx |
||
916 | lea ecx, [ebx + SOCKET.mutex] |
||
917 | call mutex_unlock |
||
918 | |||
919 | ; retransmit missing segment |
||
920 | mov eax, [esp] |
||
921 | call TCP_output |
||
922 | |||
923 | ; Lock the socket again |
||
924 | mov ecx, [esp] |
||
925 | add ecx, SOCKET.mutex |
||
926 | call mutex_lock |
||
3711 | clevermous | 927 | pop ebx |
3545 | hidnplayr | 928 | |
929 | ; Continue processing |
||
930 | xor edx, edx |
||
931 | mov eax, [ebx + TCP_SOCKET.t_maxseg] |
||
932 | mul [ebx + TCP_SOCKET.t_dupacks] |
||
933 | add eax, [ebx + TCP_SOCKET.SND_SSTHRESH] |
||
934 | mov [ebx + TCP_SOCKET.SND_CWND], eax |
||
935 | |||
936 | pop eax ; <<<< |
||
937 | cmp eax, [ebx + TCP_SOCKET.SND_NXT] |
||
938 | jb @f |
||
939 | mov [ebx + TCP_SOCKET.SND_NXT], eax |
||
940 | @@: |
||
941 | |||
942 | jmp .drop |
||
943 | |||
944 | |||
945 | .no_re_xmit: |
||
946 | jbe .not_dup_ack |
||
947 | |||
3556 | hidnplayr | 948 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Increasing congestion window\n" |
3545 | hidnplayr | 949 | |
950 | mov eax, [ebx + TCP_SOCKET.t_maxseg] |
||
951 | add [ebx + TCP_SOCKET.SND_CWND], eax |
||
952 | |||
953 | ; Unlock the socket |
||
954 | push ebx |
||
955 | lea ecx, [ebx + SOCKET.mutex] |
||
956 | call mutex_unlock |
||
957 | |||
958 | ; retransmit missing segment |
||
959 | mov eax, [esp] |
||
960 | call TCP_output |
||
961 | |||
962 | ; Lock the socket again |
||
963 | mov ecx, [esp] |
||
964 | add ecx, SOCKET.mutex |
||
965 | call mutex_lock |
||
966 | pop ebx |
||
967 | |||
968 | jmp .drop |
||
969 | |||
970 | |||
971 | .not_dup_ack: |
||
972 | |||
973 | ;------------------------------------------------- |
||
974 | ; If the congestion window was inflated to account |
||
975 | ; for the other side's cached packets, retract it |
||
976 | |||
977 | mov eax, [ebx + TCP_SOCKET.SND_SSTHRESH] |
||
978 | cmp eax, [ebx + TCP_SOCKET.SND_CWND] |
||
979 | ja @f |
||
980 | cmp [ebx + TCP_SOCKET.t_dupacks], TCP_re_xmit_thresh |
||
981 | jbe @f |
||
982 | mov [ebx + TCP_SOCKET.SND_CWND], eax |
||
983 | @@: |
||
984 | |||
985 | mov [ebx + TCP_SOCKET.t_dupacks], 0 |
||
986 | |||
987 | mov eax, [edx + TCP_header.AckNumber] |
||
988 | cmp eax, [ebx + TCP_SOCKET.SND_MAX] |
||
989 | jbe @f |
||
990 | |||
991 | ;;; TODO: update stats |
||
992 | jmp .drop_after_ack |
||
993 | |||
994 | @@: |
||
995 | |||
996 | mov edi, [edx + TCP_header.AckNumber] |
||
997 | sub edi, [ebx + TCP_SOCKET.SND_UNA] ; now we got the number of acked bytes in edi |
||
998 | |||
999 | ;;; TODO: update stats |
||
1000 | |||
3556 | hidnplayr | 1001 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: acceptable ACK for %u bytes\n", edi |
3545 | hidnplayr | 1002 | |
1003 | ;------------------------------------------ |
||
4347 | hidnplayr | 1004 | ; RTT measurements and retransmission timer |
3545 | hidnplayr | 1005 | |
1006 | ; If we have a timestamp, update smoothed RTT |
||
1007 | |||
4347 | hidnplayr | 1008 | test [temp_bits], TCP_BIT_TIMESTAMP |
3545 | hidnplayr | 1009 | jz .timestamp_not_present |
4344 | hidnplayr | 1010 | mov eax, [timestamp] |
3545 | hidnplayr | 1011 | sub eax, [ebx + TCP_SOCKET.ts_ecr] |
1012 | inc eax |
||
1013 | call TCP_xmit_timer |
||
1014 | jmp .rtt_done_ |
||
1015 | |||
1016 | ; If no timestamp but transmit timer is running and timed sequence number was acked, |
||
1017 | ; update smoothed RTT. Since we now have an RTT measurement, cancel the timer backoff |
||
1018 | ; (Phil Karn's retransmit algo) |
||
1019 | ; Recompute the initial retransmit timer |
||
1020 | |||
1021 | .timestamp_not_present: |
||
1022 | mov eax, [edx + TCP_header.AckNumber] |
||
1023 | cmp eax, [ebx + TCP_SOCKET.t_rtseq] |
||
1024 | jbe .rtt_done_ |
||
1025 | mov eax, [ebx + TCP_SOCKET.t_rtt] |
||
1026 | test eax, eax |
||
1027 | jz .rtt_done_ |
||
1028 | call TCP_xmit_timer |
||
1029 | |||
1030 | .rtt_done_: |
||
1031 | |||
1032 | ; If all outstanding data is acked, stop retransmit timer and remember to restart (more output or persist) |
||
1033 | ; If there is more data to be acked, restart retransmit timer, using current (possible backed-off) value. |
||
1034 | |||
1035 | mov eax, [ebx + TCP_SOCKET.SND_MAX] |
||
1036 | cmp eax, [edx + TCP_header.AckNumber] |
||
1037 | jne .more_data |
||
3600 | hidnplayr | 1038 | and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission |
4347 | hidnplayr | 1039 | or [temp_bits], TCP_BIT_NEEDOUTPUT |
3545 | hidnplayr | 1040 | jmp .no_restart |
1041 | .more_data: |
||
3600 | hidnplayr | 1042 | test [ebx + TCP_SOCKET.timer_flags], timer_flag_persist |
1043 | jnz .no_restart |
||
3545 | hidnplayr | 1044 | |
1045 | mov eax, [ebx + TCP_SOCKET.t_rxtcur] |
||
1046 | mov [ebx + TCP_SOCKET.timer_retransmission], eax |
||
3600 | hidnplayr | 1047 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_retransmission |
3545 | hidnplayr | 1048 | .no_restart: |
1049 | |||
1050 | |||
1051 | ;------------------------------------------- |
||
1052 | ; Open congestion window in response to ACKs |
||
1053 | |||
1054 | mov esi, [ebx + TCP_SOCKET.SND_CWND] |
||
1055 | mov eax, [ebx + TCP_SOCKET.t_maxseg] |
||
1056 | |||
1057 | cmp esi, [ebx + TCP_SOCKET.SND_SSTHRESH] |
||
1058 | jbe @f |
||
1059 | push edx |
||
1060 | push eax |
||
1061 | mul eax |
||
1062 | div esi |
||
1063 | pop edx |
||
1064 | shr edx, 3 |
||
1065 | add eax, edx |
||
1066 | pop edx |
||
1067 | @@: |
||
1068 | |||
1069 | add esi, eax |
||
1070 | |||
1071 | push ecx |
||
1072 | mov cl, [ebx + TCP_SOCKET.SND_SCALE] |
||
1073 | mov eax, TCP_max_win |
||
1074 | shl eax, cl |
||
1075 | pop ecx |
||
1076 | |||
1077 | cmp esi, eax |
||
4296 | hidnplayr | 1078 | jbe @f |
1079 | mov esi, eax |
||
1080 | @@: |
||
3545 | hidnplayr | 1081 | mov [ebx + TCP_SOCKET.SND_CWND], esi |
1082 | |||
1083 | ;------------------------------------------ |
||
1084 | ; Remove acknowledged data from send buffer |
||
1085 | |||
1086 | cmp edi, [ebx + STREAM_SOCKET.snd.size] |
||
1087 | jbe .finiacked |
||
1088 | |||
1089 | push ecx edx ebx |
||
1090 | mov ecx, [ebx + STREAM_SOCKET.snd.size] |
||
1091 | lea eax, [ebx + STREAM_SOCKET.snd] |
||
1092 | sub [ebx + TCP_SOCKET.SND_WND], ecx |
||
1093 | call SOCKET_ring_free |
||
1094 | pop ebx edx ecx |
||
1095 | |||
3556 | hidnplayr | 1096 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n" |
3545 | hidnplayr | 1097 | stc |
1098 | |||
1099 | jmp .wakeup |
||
1100 | |||
1101 | .finiacked: |
||
1102 | |||
1103 | push ecx edx ebx |
||
1104 | mov ecx, edi |
||
1105 | lea eax, [ebx + STREAM_SOCKET.snd] |
||
1106 | call SOCKET_ring_free |
||
1107 | pop ebx |
||
1108 | sub [ebx + TCP_SOCKET.SND_WND], ecx |
||
1109 | pop edx ecx |
||
1110 | |||
3556 | hidnplayr | 1111 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is not acked\n" |
3545 | hidnplayr | 1112 | clc |
1113 | |||
1114 | ;---------------------------------------- |
||
1115 | ; Wake up process waiting on send buffer |
||
1116 | |||
1117 | .wakeup: |
||
1118 | |||
1119 | pushf ; Keep the flags (Carry flag) |
||
1120 | mov eax, ebx |
||
1121 | call SOCKET_notify |
||
1122 | |||
1123 | ; Update TCPS |
||
1124 | |||
1125 | mov eax, [edx + TCP_header.AckNumber] |
||
1126 | mov [ebx + TCP_SOCKET.SND_UNA], eax |
||
1127 | cmp eax, [ebx + TCP_SOCKET.SND_NXT] |
||
1128 | jb @f |
||
1129 | mov [ebx + TCP_SOCKET.SND_NXT], eax |
||
1130 | @@: |
||
1131 | |||
1132 | popf |
||
1133 | |||
1134 | ; General ACK handling complete |
||
1135 | ; Now do the state-specific ones |
||
1136 | ; Carry flag is set when our FIN is acked |
||
1137 | |||
1138 | mov eax, [ebx + TCP_SOCKET.t_state] |
||
1139 | jmp dword [eax*4 + .ACK_sw_list] |
||
1140 | |||
1141 | .ACK_sw_list: |
||
1142 | dd .ack_processed ; TCPS_CLOSED |
||
1143 | dd .ack_processed ; TCPS_LISTEN |
||
1144 | dd .ack_processed ; TCPS_SYN_SENT |
||
1145 | dd .ack_processed ; TCPS_SYN_RECEIVED |
||
1146 | dd .ack_processed ; TCPS_ESTABLISHED |
||
1147 | dd .ack_processed ; TCPS_CLOSE_WAIT |
||
1148 | dd .ack_fw1 ; TCPS_FIN_WAIT_1 |
||
1149 | dd .ack_c ; TCPS_CLOSING |
||
1150 | dd .ack_la ; TCPS_LAST_ACK |
||
1151 | dd .ack_processed ; TCPS_FIN_WAIT_2 |
||
1152 | dd .ack_tw ; TCPS_TIMED_WAIT |
||
1153 | |||
1154 | |||
1155 | .ack_fw1: |
||
1156 | jnc .ack_processed |
||
1157 | |||
1158 | test [ebx + SOCKET.state], SS_CANTRCVMORE |
||
1159 | jnz @f |
||
1160 | mov eax, ebx |
||
1161 | call SOCKET_is_disconnected |
||
1162 | mov [ebx + TCP_SOCKET.timer_timed_wait], TCP_time_max_idle |
||
3600 | hidnplayr | 1163 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait |
3545 | hidnplayr | 1164 | @@: |
1165 | mov [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2 |
||
1166 | jmp .ack_processed |
||
1167 | |||
1168 | .ack_c: |
||
1169 | jnc .ack_processed |
||
1170 | |||
1171 | mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
1172 | mov eax, ebx |
||
1173 | call TCP_cancel_timers |
||
1174 | mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL |
||
3600 | hidnplayr | 1175 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait |
3545 | hidnplayr | 1176 | mov eax, ebx |
1177 | call SOCKET_is_disconnected |
||
1178 | jmp .ack_processed |
||
1179 | |||
1180 | .ack_la: |
||
1181 | jnc .ack_processed |
||
1182 | |||
3652 | hidnplayr | 1183 | push ebx |
1184 | lea ecx, [ebx + SOCKET.mutex] |
||
1185 | call mutex_unlock |
||
1186 | pop ebx |
||
1187 | |||
3545 | hidnplayr | 1188 | mov eax, ebx |
4366 | hidnplayr | 1189 | call TCP_close |
1190 | jmp .drop_no_socket |
||
3545 | hidnplayr | 1191 | |
1192 | .ack_tw: |
||
1193 | mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL |
||
3600 | hidnplayr | 1194 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait |
3545 | hidnplayr | 1195 | jmp .drop_after_ack |
1196 | |||
1197 | .reset_dupacks: ; We got a new ACK, reset duplicate ACK counter |
||
1198 | mov [ebx + TCP_SOCKET.t_dupacks], 0 |
||
1199 | jmp .ack_processed |
||
1200 | |||
1201 | ;------- |
||
1202 | ; LISTEN |
||
1203 | |||
1204 | align 4 |
||
1205 | .LISTEN: |
||
1206 | |||
3556 | hidnplayr | 1207 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=listen\n" |
3545 | hidnplayr | 1208 | |
1209 | test [edx + TCP_header.Flags], TH_RST |
||
1210 | jnz .drop |
||
1211 | |||
1212 | test [edx + TCP_header.Flags], TH_ACK |
||
1213 | jnz .drop_with_reset |
||
1214 | |||
1215 | test [edx + TCP_header.Flags], TH_SYN |
||
1216 | jz .drop |
||
1217 | |||
1218 | ;;; TODO: check if it's a broadcast or multicast, and drop if so |
||
1219 | |||
1220 | push dword [edi] ; Ipv4 source addres |
||
1221 | pop [ebx + IP_SOCKET.RemoteIP] |
||
1222 | |||
1223 | push [edx + TCP_header.SourcePort] |
||
1224 | pop [ebx + TCP_SOCKET.RemotePort] |
||
1225 | |||
1226 | push [edx + TCP_header.SequenceNumber] |
||
1227 | pop [ebx + TCP_SOCKET.IRS] |
||
1228 | |||
1229 | mov eax, [TCP_sequence_num] |
||
1230 | add [TCP_sequence_num], 64000 / 2 |
||
1231 | mov [ebx + TCP_SOCKET.ISS], eax |
||
1232 | mov [ebx + TCP_SOCKET.SND_NXT], eax |
||
1233 | |||
1234 | TCP_sendseqinit ebx |
||
1235 | TCP_rcvseqinit ebx |
||
1236 | |||
1237 | mov [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED |
||
4347 | hidnplayr | 1238 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
3545 | hidnplayr | 1239 | mov [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval ;;;; macro |
3600 | hidnplayr | 1240 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive |
3545 | hidnplayr | 1241 | |
1242 | lea eax, [ebx + STREAM_SOCKET.snd] |
||
1243 | call SOCKET_ring_create |
||
1244 | |||
1245 | lea eax, [ebx + STREAM_SOCKET.rcv] |
||
1246 | call SOCKET_ring_create |
||
1247 | |||
4347 | hidnplayr | 1248 | and [temp_bits], not TCP_BIT_DROPSOCKET |
3545 | hidnplayr | 1249 | |
3674 | hidnplayr | 1250 | pusha |
1251 | mov eax, ebx |
||
1252 | call SOCKET_notify |
||
1253 | popa |
||
3545 | hidnplayr | 1254 | |
1255 | jmp .trim_then_step6 |
||
1256 | |||
1257 | ;------------ |
||
1258 | ; Active Open |
||
1259 | |||
1260 | align 4 |
||
1261 | .SYN_SENT: |
||
1262 | |||
3556 | hidnplayr | 1263 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: state=syn_sent\n" |
3545 | hidnplayr | 1264 | |
1265 | test [edx + TCP_header.Flags], TH_ACK |
||
1266 | jz @f |
||
1267 | |||
1268 | mov eax, [edx + TCP_header.AckNumber] |
||
1269 | cmp eax, [ebx + TCP_SOCKET.ISS] |
||
1270 | jbe .drop_with_reset |
||
1271 | |||
1272 | cmp eax, [ebx + TCP_SOCKET.SND_MAX] |
||
1273 | ja .drop_with_reset |
||
1274 | @@: |
||
1275 | |||
1276 | test [edx + TCP_header.Flags], TH_RST |
||
1277 | jz @f |
||
1278 | |||
1279 | test [edx + TCP_header.Flags], TH_ACK |
||
1280 | jz .drop |
||
1281 | |||
1282 | mov eax, ebx |
||
1283 | mov ebx, ECONNREFUSED |
||
1284 | call TCP_drop |
||
1285 | |||
1286 | jmp .drop |
||
1287 | @@: |
||
1288 | |||
1289 | test [edx + TCP_header.Flags], TH_SYN |
||
1290 | jz .drop |
||
1291 | |||
1292 | ; at this point, segment seems to be valid |
||
1293 | |||
1294 | test [edx + TCP_header.Flags], TH_ACK |
||
1295 | jz .no_syn_ack |
||
1296 | |||
1297 | ; now, process received SYN in response to an active open |
||
1298 | |||
1299 | mov eax, [edx + TCP_header.AckNumber] |
||
1300 | mov [ebx + TCP_SOCKET.SND_UNA], eax |
||
1301 | cmp eax, [ebx + TCP_SOCKET.SND_NXT] |
||
1302 | jbe @f |
||
1303 | mov [ebx + TCP_SOCKET.SND_NXT], eax |
||
1304 | @@: |
||
1305 | |||
1306 | .no_syn_ack: |
||
3600 | hidnplayr | 1307 | and [ebx + TCP_SOCKET.timer_flags], not timer_flag_retransmission ; disable retransmission timer |
3545 | hidnplayr | 1308 | |
1309 | push [edx + TCP_header.SequenceNumber] |
||
1310 | pop [ebx + TCP_SOCKET.IRS] |
||
1311 | |||
1312 | TCP_rcvseqinit ebx |
||
1313 | |||
1314 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
||
1315 | |||
1316 | mov eax, [ebx + TCP_SOCKET.SND_UNA] |
||
1317 | cmp eax, [ebx + TCP_SOCKET.ISS] |
||
1318 | jbe .simultaneous_open |
||
1319 | |||
1320 | test [edx + TCP_header.Flags], TH_ACK |
||
1321 | jz .simultaneous_open |
||
1322 | |||
3556 | hidnplayr | 1323 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: active open\n" |
3545 | hidnplayr | 1324 | |
1325 | ;;; TODO: update stats |
||
1326 | |||
1327 | ; set socket state to connected |
||
3674 | hidnplayr | 1328 | push eax |
1329 | mov eax, ebx |
||
1330 | call SOCKET_is_connected |
||
1331 | pop eax |
||
3545 | hidnplayr | 1332 | mov [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED |
1333 | |||
1334 | ; Do window scaling on this connection ? |
||
1335 | mov eax, [ebx + TCP_SOCKET.t_flags] |
||
1336 | and eax, TF_REQ_SCALE or TF_RCVD_SCALE |
||
1337 | cmp eax, TF_REQ_SCALE or TF_RCVD_SCALE |
||
1338 | jne .no_scaling |
||
1339 | |||
1340 | mov ax, word [ebx + TCP_SOCKET.requested_s_scale] |
||
1341 | mov word [ebx + TCP_SOCKET.SND_SCALE], ax |
||
1342 | .no_scaling: |
||
1343 | |||
1344 | ;;; TODO: reassemble packets queue |
||
1345 | |||
1346 | mov eax, [ebx + TCP_SOCKET.t_rtt] |
||
1347 | test eax, eax |
||
1348 | je .trim_then_step6 |
||
1349 | call TCP_xmit_timer |
||
1350 | jmp .trim_then_step6 |
||
1351 | |||
1352 | .simultaneous_open: |
||
1353 | |||
3556 | hidnplayr | 1354 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n" |
3545 | hidnplayr | 1355 | ; We have received a syn but no ACK, so we are having a simultaneous open.. |
1356 | mov [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED |
||
1357 | |||
1358 | ;------------------------------------- |
||
1359 | ; Common processing for receipt of SYN |
||
1360 | |||
1361 | .trim_then_step6: |
||
1362 | |||
1363 | inc [edx + TCP_header.SequenceNumber] |
||
1364 | |||
4339 | hidnplayr | 1365 | ; Drop any received data that doesnt fit in the receive window. |
1366 | cmp ecx, [ebx + TCP_SOCKET.RCV_WND] |
||
1367 | jbe .dont_trim |
||
3545 | hidnplayr | 1368 | |
4339 | hidnplayr | 1369 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: received data does not fit in window, trimming %u bytes\n", eax |
1370 | mov ecx, [ebx + TCP_SOCKET.RCV_WND] |
||
1371 | and [edx + TCP_header.Flags], not (TH_FIN) |
||
1372 | ;;; TODO: update stats |
||
1373 | |||
1374 | .dont_trim: |
||
1375 | |||
3545 | hidnplayr | 1376 | mov eax, [edx + TCP_header.SequenceNumber] |
1377 | mov [ebx + TCP_SOCKET.RCV_UP], eax |
||
1378 | dec eax |
||
1379 | mov [ebx + TCP_SOCKET.SND_WL1], eax |
||
1380 | |||
1381 | ;------- |
||
1382 | ; step 6 |
||
1383 | |||
1384 | .ack_processed: |
||
1385 | |||
3556 | hidnplayr | 1386 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n" |
3545 | hidnplayr | 1387 | |
1388 | ;---------------------------------------------- |
||
1389 | ; check if we need to update window information |
||
1390 | |||
1391 | test [edx + TCP_header.Flags], TH_ACK |
||
1392 | jz .no_window_update |
||
1393 | |||
1394 | mov eax, [ebx + TCP_SOCKET.SND_WL1] |
||
1395 | cmp eax, [edx + TCP_header.SequenceNumber] |
||
1396 | jb .update_window |
||
1397 | ja @f |
||
1398 | |||
1399 | mov eax, [ebx + TCP_SOCKET.SND_WL2] |
||
1400 | cmp eax, [edx + TCP_header.AckNumber] |
||
1401 | jb .update_window |
||
1402 | ja .no_window_update |
||
1403 | @@: |
||
1404 | |||
1405 | mov eax, dword [edx + TCP_header.Window] |
||
1406 | cmp eax, [ebx + TCP_SOCKET.SND_WND] |
||
1407 | jbe .no_window_update |
||
1408 | |||
1409 | .update_window: |
||
1410 | |||
1411 | ;;; TODO: update stats (Keep track of pure window updates) |
||
1412 | |||
1413 | mov eax, dword [edx + TCP_header.Window] |
||
1414 | cmp eax, [ebx + TCP_SOCKET.max_sndwnd] |
||
1415 | jbe @f |
||
1416 | mov [ebx + TCP_SOCKET.max_sndwnd], eax |
||
1417 | @@: |
||
1418 | mov [ebx + TCP_SOCKET.SND_WND], eax |
||
1419 | |||
3556 | hidnplayr | 1420 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Updating window to %u\n", eax |
3545 | hidnplayr | 1421 | |
1422 | push [edx + TCP_header.SequenceNumber] |
||
1423 | pop [ebx + TCP_SOCKET.SND_WL1] |
||
1424 | |||
1425 | push [edx + TCP_header.AckNumber] |
||
1426 | pop [ebx + TCP_SOCKET.SND_WL2] |
||
1427 | |||
4347 | hidnplayr | 1428 | or [temp_bits], TCP_BIT_NEEDOUTPUT |
3545 | hidnplayr | 1429 | |
1430 | .no_window_update: |
||
1431 | |||
1432 | ;----------------- |
||
1433 | ; process URG flag |
||
1434 | |||
1435 | test [edx + TCP_header.Flags], TH_URG |
||
1436 | jz .not_urgent |
||
1437 | |||
1438 | cmp [edx + TCP_header.UrgentPointer], 0 |
||
1439 | jz .not_urgent |
||
1440 | |||
1441 | cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
1442 | je .not_urgent |
||
1443 | |||
1444 | ; Ignore bogus urgent offsets |
||
1445 | |||
1446 | movzx eax, [edx + TCP_header.UrgentPointer] |
||
1447 | add eax, [ebx + STREAM_SOCKET.rcv.size] |
||
1448 | cmp eax, SOCKET_MAXDATA |
||
1449 | jbe .not_urgent |
||
1450 | |||
1451 | mov [edx + TCP_header.UrgentPointer], 0 |
||
1452 | and [edx + TCP_header.Flags], not (TH_URG) |
||
1453 | jmp .do_data |
||
1454 | |||
1455 | .not_urgent: |
||
1456 | |||
1457 | ; processing of received urgent pointer |
||
1458 | |||
1459 | ;;; TODO (1051-1093) |
||
1460 | |||
1461 | |||
1462 | ;--------------------------------------- |
||
1463 | ; process the data in the segment (1094) |
||
1464 | |||
1465 | .do_data: |
||
1466 | |||
1467 | cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
1468 | jae .final_processing |
||
1469 | |||
1470 | test [edx + TCP_header.Flags], TH_FIN |
||
1471 | jnz @f |
||
1472 | |||
1473 | test ecx, ecx |
||
3756 | hidnplayr | 1474 | jz .final_processing |
3545 | hidnplayr | 1475 | @@: |
1476 | |||
1477 | ; The segment is in order? |
||
1478 | mov eax, [edx + TCP_header.SequenceNumber] |
||
1479 | cmp eax, [ebx + TCP_SOCKET.RCV_NXT] |
||
1480 | jne .out_of_order |
||
1481 | |||
1482 | ; The reassembly queue is empty? |
||
1483 | cmp [ebx + TCP_SOCKET.seg_next], 0 |
||
1484 | jne .out_of_order |
||
1485 | |||
1486 | ; The connection is established? |
||
1487 | cmp [ebx + TCP_SOCKET.t_state], TCPS_ESTABLISHED |
||
1488 | jne .out_of_order |
||
1489 | |||
1490 | ; Ok, lets do this.. Set delayed ACK flag and copy data into socket buffer |
||
1491 | or [ebx + TCP_SOCKET.t_flags], TF_DELACK |
||
1492 | |||
1493 | pusha |
||
4339 | hidnplayr | 1494 | mov esi, [dataoffset] |
3545 | hidnplayr | 1495 | add esi, edx |
1496 | lea eax, [ebx + STREAM_SOCKET.rcv] |
||
1497 | call SOCKET_ring_write ; Add the data to the socket buffer |
||
1498 | add [ebx + TCP_SOCKET.RCV_NXT], ecx ; Update sequence number with number of bytes we have copied |
||
1499 | popa |
||
1500 | |||
1501 | ; Wake up the sleeping process |
||
1502 | mov eax, ebx |
||
1503 | call SOCKET_notify |
||
1504 | |||
1505 | jmp .data_done |
||
1506 | |||
1507 | .out_of_order: |
||
4339 | hidnplayr | 1508 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP data is out of order!\nSequencenumber is %u, we expected %u.\n", \ |
1509 | [edx + TCP_header.SequenceNumber], [ebx + TCP_SOCKET.RCV_NXT] |
||
3545 | hidnplayr | 1510 | |
1511 | ; Uh-oh, some data is out of order, lets call TCP reassemble for help |
||
1512 | |||
1513 | call TCP_reassemble |
||
1514 | |||
4339 | hidnplayr | 1515 | ; Generate ACK immediately, to let the other end know that a segment was received out of order, |
1516 | ; and to tell it what sequence number is expected. This aids the fast-retransmit algorithm. |
||
3545 | hidnplayr | 1517 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
1518 | .data_done: |
||
1519 | |||
1520 | ;--------------- |
||
1521 | ; FIN processing |
||
1522 | |||
1523 | test [edx + TCP_header.Flags], TH_FIN |
||
1524 | jz .final_processing |
||
1525 | |||
3556 | hidnplayr | 1526 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Processing FIN\n" |
3545 | hidnplayr | 1527 | |
1528 | cmp [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
1529 | jae .not_first_fin |
||
1530 | |||
3556 | hidnplayr | 1531 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: First FIN for this connection\n" |
3545 | hidnplayr | 1532 | |
1533 | mov eax, ebx |
||
1534 | call SOCKET_cant_recv_more |
||
1535 | |||
4347 | hidnplayr | 1536 | or [ebx + TCP_SOCKET.t_flags], TF_ACKNOW |
3545 | hidnplayr | 1537 | inc [ebx + TCP_SOCKET.RCV_NXT] |
1538 | |||
1539 | .not_first_fin: |
||
1540 | mov eax, [ebx + TCP_SOCKET.t_state] |
||
1541 | shl eax, 2 |
||
1542 | jmp dword [eax + .FIN_sw_list] |
||
1543 | |||
1544 | .FIN_sw_list: |
||
1545 | dd .final_processing ; TCPS_CLOSED |
||
1546 | dd .final_processing ; TCPS_LISTEN |
||
1547 | dd .final_processing ; TCPS_SYN_SENT |
||
1548 | dd .fin_syn_est ; TCPS_SYN_RECEIVED |
||
1549 | dd .fin_syn_est ; TCPS_ESTABLISHED |
||
1550 | dd .final_processing ; TCPS_CLOSE_WAIT |
||
1551 | dd .fin_wait1 ; TCPS_FIN_WAIT_1 |
||
1552 | dd .final_processing ; TCPS_CLOSING |
||
1553 | dd .final_processing ; TCPS_LAST_ACK |
||
1554 | dd .fin_wait2 ; TCPS_FIN_WAIT_2 |
||
1555 | dd .fin_timed ; TCPS_TIMED_WAIT |
||
1556 | |||
1557 | .fin_syn_est: |
||
1558 | mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT |
||
1559 | jmp .final_processing |
||
1560 | |||
1561 | .fin_wait1: |
||
1562 | mov [ebx + TCP_SOCKET.t_state], TCPS_CLOSING |
||
1563 | jmp .final_processing |
||
1564 | |||
1565 | .fin_wait2: |
||
1566 | mov [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT |
||
1567 | mov eax, ebx |
||
1568 | call TCP_cancel_timers |
||
1569 | call SOCKET_is_disconnected |
||
1570 | |||
1571 | .fin_timed: |
||
1572 | mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL |
||
3600 | hidnplayr | 1573 | or [ebx + TCP_SOCKET.timer_flags], timer_flag_wait |
3545 | hidnplayr | 1574 | |
4347 | hidnplayr | 1575 | ;----------------- |
1576 | ; Final processing |
||
3545 | hidnplayr | 1577 | |
4347 | hidnplayr | 1578 | .final_processing: |
1579 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Final processing\n" |
||
1580 | |||
1581 | push ebx |
||
1582 | lea ecx, [ebx + SOCKET.mutex] |
||
1583 | call mutex_unlock |
||
1584 | pop eax |
||
1585 | |||
1586 | test [temp_bits], TCP_BIT_NEEDOUTPUT |
||
1587 | jnz .need_output |
||
1588 | |||
1589 | test [eax + TCP_SOCKET.t_flags], TF_ACKNOW |
||
1590 | jz .dumpit |
||
1591 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n" |
||
1592 | |||
1593 | .need_output: |
||
1594 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n" |
||
1595 | call TCP_output |
||
1596 | |||
1597 | .dumpit: |
||
1598 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: dumping\n" |
||
1599 | |||
1600 | call NET_packet_free |
||
1601 | jmp .loop |
||
1602 | |||
1603 | |||
1604 | ;----------------- |
||
1605 | ; Drop the segment |
||
1606 | |||
1607 | |||
3545 | hidnplayr | 1608 | .drop_after_ack: |
3556 | hidnplayr | 1609 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop after ACK\n" |
3545 | hidnplayr | 1610 | |
1611 | push edx ebx |
||
1612 | lea ecx, [ebx + SOCKET.mutex] |
||
1613 | call mutex_unlock |
||
1614 | pop eax edx |
||
1615 | |||
1616 | test [edx + TCP_header.Flags], TH_RST |
||
1617 | jnz .dumpit |
||
1618 | |||
1619 | or [eax + TCP_SOCKET.t_flags], TF_ACKNOW |
||
1620 | jmp .need_output |
||
1621 | |||
1622 | .drop_with_reset: |
||
3556 | hidnplayr | 1623 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop with reset\n" |
3545 | hidnplayr | 1624 | |
1625 | push ebx edx |
||
1626 | lea ecx, [ebx + SOCKET.mutex] |
||
1627 | call mutex_unlock |
||
1628 | pop edx ebx |
||
1629 | |||
1630 | test [edx + TCP_header.Flags], TH_RST |
||
1631 | jnz .dumpit |
||
1632 | |||
1633 | ;;; if its a multicast/broadcast, also drop |
||
1634 | |||
1635 | test [edx + TCP_header.Flags], TH_ACK |
||
1636 | jnz .respond_ack |
||
1637 | |||
1638 | test [edx + TCP_header.Flags], TH_SYN |
||
1639 | jnz .respond_syn |
||
1640 | jmp .dumpit |
||
1641 | |||
1642 | ;--------- |
||
1643 | ; Respond |
||
1644 | |||
1645 | .respond_ack: |
||
1646 | push ebx |
||
1647 | mov cl, TH_RST |
||
1648 | call TCP_respond |
||
1649 | pop ebx |
||
1650 | jmp .destroy_new_socket |
||
1651 | |||
1652 | .respond_syn: |
||
1653 | push ebx |
||
1654 | mov cl, TH_RST + TH_ACK |
||
1655 | call TCP_respond |
||
1656 | pop ebx |
||
1657 | jmp .destroy_new_socket |
||
1658 | |||
3647 | hidnplayr | 1659 | .no_socket: |
1660 | |||
1661 | pusha |
||
1662 | mov ecx, socket_mutex |
||
1663 | call mutex_unlock |
||
1664 | popa |
||
1665 | |||
3545 | hidnplayr | 1666 | .respond_seg_reset: |
1667 | test [edx + TCP_header.Flags], TH_RST |
||
1668 | jnz .drop_no_socket |
||
1669 | |||
1670 | ;;; TODO: if its a multicast/broadcast, also drop |
||
1671 | |||
1672 | test [edx + TCP_header.Flags], TH_ACK |
||
1673 | jnz .respond_seg_ack |
||
1674 | |||
1675 | test [edx + TCP_header.Flags], TH_SYN |
||
1676 | jnz .respond_seg_syn |
||
1677 | |||
1678 | jmp .drop_no_socket |
||
1679 | |||
1680 | .respond_seg_ack: |
||
1681 | mov cl, TH_RST |
||
1682 | call TCP_respond_segment |
||
1683 | jmp .drop_no_socket |
||
1684 | |||
1685 | .respond_seg_syn: |
||
1686 | mov cl, TH_RST + TH_ACK |
||
1687 | call TCP_respond_segment |
||
1688 | jmp .drop_no_socket |
||
1689 | |||
1690 | ;----- |
||
1691 | ; Drop |
||
1692 | |||
1693 | .drop: |
||
3556 | hidnplayr | 1694 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n" |
3545 | hidnplayr | 1695 | |
1696 | pusha |
||
1697 | lea ecx, [ebx + SOCKET.mutex] |
||
1698 | call mutex_unlock |
||
1699 | popa |
||
1700 | |||
1701 | .destroy_new_socket: |
||
4347 | hidnplayr | 1702 | test [temp_bits], TCP_BIT_DROPSOCKET |
3545 | hidnplayr | 1703 | jz .drop_no_socket |
1704 | |||
1705 | mov eax, ebx |
||
1706 | call SOCKET_free |
||
1707 | |||
1708 | .drop_no_socket: |
||
3556 | hidnplayr | 1709 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Drop (no socket)\n" |
3545 | hidnplayr | 1710 | |
3861 | hidnplayr | 1711 | call NET_packet_free |
3545 | hidnplayr | 1712 | jmp .loop |
4339 | hidnplayr | 1713 | |
1714 | endp |