Subversion Repositories Kolibri OS

Rev

Rev 4423 | Rev 5565 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4423 Rev 5201
Line 1... Line 1...
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2004-2014. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  Part of the TCP/IP network stack for KolibriOS                 ;;
6
;;  Part of the TCP/IP network stack for KolibriOS                 ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;   Written by hidnplayr@kolibrios.org                            ;;
8
;;   Written by hidnplayr@kolibrios.org                            ;;
9
;;                                                                 ;;
9
;;                                                                 ;;
10
;;    Based on the code of 4.4BSD                                  ;;
10
;;    Based on the algorithms used in 4.4BSD                       ;;
11
;;                                                                 ;;
11
;;                                                                 ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 16... Line 16...
16
 
16
 
Line 17... Line 17...
17
$Revision: 3407 $
17
$Revision: 5155 $
18
 
18
 
19
;-----------------------------------------------------------------
19
;-----------------------------------------------------------------
20
;
20
;
Line 41... Line 41...
41
        mov     [esp + 4], eax
41
        mov     [esp + 4], eax
Line 42... Line 42...
42
 
42
 
43
        push    ebx ecx esi edi         ; mind the order
43
        push    ebx ecx esi edi         ; mind the order
Line 44... Line -...
44
        mov     esi, esp
-
 
45
 
-
 
46
        pushf
44
        mov     esi, esp
47
        cli
-
 
48
        add_to_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .fail
-
 
49
        popf
45
 
Line 50... Line 46...
50
 
46
        add_to_queue TCP_queue, TCP_QUEUE_SIZE, sizeof.TCP_queue_entry, .fail
51
        add     esp, sizeof.TCP_queue_entry
47
        add     esp, sizeof.TCP_queue_entry
Line 60... Line 56...
60
        call    raise_event
56
        call    raise_event
Line 61... Line 57...
61
 
57
 
Line 62... Line 58...
62
        ret
58
        ret
63
 
-
 
64
  .fail:
59
 
Line 65... Line 60...
65
        popf
60
  .fail:
66
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP incoming queue is full, discarding packet!\n"
61
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP incoming queue is full, discarding packet!\n"
Line 534... Line 529...
534
; - The acknowledgment field equals the largest unacknowledged sequence number. This means no data is acknowledged by this segment.
529
; - The acknowledgment field equals the largest unacknowledged sequence number. This means no data is acknowledged by this segment.
535
        mov     eax, [edx + TCP_header.AckNumber]
530
        mov     eax, [edx + TCP_header.AckNumber]
536
        cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
531
        cmp     eax, [ebx + TCP_SOCKET.SND_UNA]
537
        jne     .not_uni_xfer
532
        jne     .not_uni_xfer
Line 538... Line 533...
538
 
533
 
539
; - The reassembly list of out-of-order segments for the connection is empty (seg_next equals tp).
-
 
540
 
534
; - The reassembly list of out-of-order segments for the connection is empty.
541
;;; TODO
-
 
542
 
535
        cmp     [ebx + TCP_SOCKET.seg_next], 0
Line 543... Line 536...
543
;       jnz     .not_uni_xfer
536
        jne     .not_uni_xfer
Line 544... Line 537...
544
 
537
 
Line 843... Line 836...
843
 
836
 
844
        push    word [ebx + TCP_SOCKET.requested_s_scale]       ; Set send and receive scale factors to the received values
837
        push    word [ebx + TCP_SOCKET.requested_s_scale]       ; Set send and receive scale factors to the received values
845
        pop     word [ebx + TCP_SOCKET.SND_SCALE]
838
        pop     word [ebx + TCP_SOCKET.SND_SCALE]
Line 846... Line 839...
846
       @@:
839
       @@:
Line 847... Line 840...
847
 
840
 
848
;;; TODO: call TCP_reassemble
841
        call    TCP_reassemble
849
 
842
 
Line 1097... Line 1090...
1097
        sub     [ebx + TCP_SOCKET.SND_WND], ecx
1090
        sub     [ebx + TCP_SOCKET.SND_WND], ecx
1098
        call    SOCKET_ring_free
1091
        call    SOCKET_ring_free
1099
        pop     ebx edx ecx
1092
        pop     ebx edx ecx
Line 1100... Line 1093...
1100
 
1093
 
1101
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n"
1094
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is acked\n"
1102
        stc
-
 
1103
 
1095
        or      [temp_bits], TCP_BIT_FIN_IS_ACKED
Line 1104... Line 1096...
1104
        jmp     .wakeup
1096
        jmp     .wakeup
Line 1105... Line 1097...
1105
 
1097
 
Line 1112... Line 1104...
1112
        pop     ebx
1104
        pop     ebx
1113
        sub     [ebx + TCP_SOCKET.SND_WND], ecx
1105
        sub     [ebx + TCP_SOCKET.SND_WND], ecx
1114
        pop     edx ecx
1106
        pop     edx ecx
Line 1115... Line 1107...
1115
 
1107
 
1116
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is not acked\n"
-
 
Line 1117... Line 1108...
1117
        clc
1108
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: our FIN is not acked\n"
1118
 
1109
 
Line 1119... Line 1110...
1119
;----------------------------------------
1110
;----------------------------------------
1120
; Wake up process waiting on send buffer
-
 
1121
 
-
 
1122
  .wakeup:
1111
; Wake up process waiting on send buffer
1123
 
1112
 
Line 1124... Line 1113...
1124
        pushf                   ; Keep the flags (Carry flag)
1113
  .wakeup:
1125
        mov     eax, ebx
-
 
1126
        call    SOCKET_notify
1114
        mov     eax, ebx
1127
 
1115
        call    SOCKET_notify
1128
; Update TCPS
1116
 
1129
 
1117
; Update TCPS
1130
        mov     eax, [edx + TCP_header.AckNumber]
1118
        mov     eax, [edx + TCP_header.AckNumber]
1131
        mov     [ebx + TCP_SOCKET.SND_UNA], eax
1119
        mov     [ebx + TCP_SOCKET.SND_UNA], eax
Line 1132... Line -...
1132
        cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
-
 
1133
        jb      @f
-
 
1134
        mov     [ebx + TCP_SOCKET.SND_NXT], eax
1120
        cmp     eax, [ebx + TCP_SOCKET.SND_NXT]
1135
       @@:
1121
        jb      @f
1136
 
1122
        mov     [ebx + TCP_SOCKET.SND_NXT], eax
Line 1137... Line 1123...
1137
        popf
1123
       @@:
Line 1156... Line 1142...
1156
        dd      .ack_processed  ; TCPS_FIN_WAIT_2
1142
        dd      .ack_processed  ; TCPS_FIN_WAIT_2
1157
        dd      .ack_tw         ; TCPS_TIMED_WAIT
1143
        dd      .ack_tw         ; TCPS_TIMED_WAIT
Line 1158... Line 1144...
1158
 
1144
 
-
 
1145
 
1159
 
1146
  .ack_fw1:
Line 1160... Line 1147...
1160
  .ack_fw1:
1147
        test    [temp_bits], TCP_BIT_FIN_IS_ACKED
1161
        jnc     .ack_processed
1148
        jz      .ack_processed
1162
 
1149
 
1163
        test    [ebx + SOCKET.state], SS_CANTRCVMORE
1150
        test    [ebx + SOCKET.state], SS_CANTRCVMORE
Line 1169... Line 1156...
1169
       @@:
1156
       @@:
1170
        mov     [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
1157
        mov     [ebx + TCP_SOCKET.t_state], TCPS_FIN_WAIT_2
1171
        jmp     .ack_processed
1158
        jmp     .ack_processed
Line 1172... Line 1159...
1172
 
1159
 
-
 
1160
  .ack_c:
1173
  .ack_c:
1161
        test    [temp_bits], TCP_BIT_FIN_IS_ACKED
Line 1174... Line 1162...
1174
        jnc     .ack_processed
1162
        jz      .ack_processed
1175
 
1163
 
1176
        mov     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
1164
        mov     [ebx + TCP_SOCKET.t_state], TCPS_TIMED_WAIT
1177
        mov     eax, ebx
1165
        mov     eax, ebx
Line 1181... Line 1169...
1181
        mov     eax, ebx
1169
        mov     eax, ebx
1182
        call    SOCKET_is_disconnected
1170
        call    SOCKET_is_disconnected
1183
        jmp     .ack_processed
1171
        jmp     .ack_processed
Line 1184... Line 1172...
1184
 
1172
 
-
 
1173
  .ack_la:
1185
  .ack_la:
1174
        test    [temp_bits], TCP_BIT_FIN_IS_ACKED
Line 1186... Line 1175...
1186
        jnc     .ack_processed
1175
        jz      .ack_processed
1187
 
1176
 
1188
        push    ebx
1177
        push    ebx
1189
        lea     ecx, [ebx + SOCKET.mutex]
1178
        lea     ecx, [ebx + SOCKET.mutex]
Line 1244... Line 1233...
1244
        mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval  ;;;; macro
1233
        mov     [ebx + TCP_SOCKET.timer_keepalive], TCP_time_keep_interval  ;;;; macro
1245
        or      [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
1234
        or      [ebx + TCP_SOCKET.timer_flags], timer_flag_keepalive
Line 1246... Line 1235...
1246
 
1235
 
1247
        lea     eax, [ebx + STREAM_SOCKET.snd]
1236
        lea     eax, [ebx + STREAM_SOCKET.snd]
-
 
1237
        call    SOCKET_ring_create
-
 
1238
        test    eax, eax
Line 1248... Line 1239...
1248
        call    SOCKET_ring_create
1239
        jz      .drop
1249
 
1240
 
-
 
1241
        lea     eax, [ebx + STREAM_SOCKET.rcv]
-
 
1242
        call    SOCKET_ring_create
Line 1250... Line 1243...
1250
        lea     eax, [ebx + STREAM_SOCKET.rcv]
1243
        test    eax, eax
Line 1251... Line 1244...
1251
        call    SOCKET_ring_create
1244
        jz      .drop
1252
 
1245
 
1253
        and     [temp_bits], not TCP_BIT_DROPSOCKET
1246
        and     [temp_bits], not TCP_BIT_DROPSOCKET
1254
 
1247
 
Line 1255... Line 1248...
1255
        pusha
1248
        pusha
Line 1256... Line 1249...
1256
        mov     eax, ebx
1249
        mov     eax, ebx
1257
        call    SOCKET_notify
1250
        call    SOCKET_notify
Line 1258... Line 1251...
1258
        popa
1251
        popa
Line 1348... Line 1341...
1348
 
1341
 
Line 1349... Line 1342...
1349
;;; TODO: reassemble packets queue
1342
;;; TODO: reassemble packets queue
1350
 
1343
 
1351
        mov     eax, [ebx + TCP_SOCKET.t_rtt]
1344
        mov     eax, [ebx + TCP_SOCKET.t_rtt]
1352
        test    eax, eax
1345
        test    eax, eax
1353
        je      .trim_then_step6
1346
        je      .trim
Line 1354... Line 1347...
1354
        call    TCP_xmit_timer
1347
        call    TCP_xmit_timer
Line 1355... Line 1348...
1355
        jmp     .trim_then_step6
1348
        jmp     .trim
1356
 
1349
 
1357
  .simultaneous_open:
1350
  .simultaneous_open:
Line 1358... Line 1351...
1358
 
1351
 
1359
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n"
1352
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: simultaneous open\n"
Line 1360... Line 1353...
1360
; We have received a syn but no ACK, so we are having a simultaneous open..
1353
; We have received a syn but no ACK, so we are having a simultaneous open..
1361
        mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
-
 
1362
 
1354
        mov     [ebx + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
Line 1363... Line 1355...
1363
;-------------------------------------
1355
 
1364
; Common processing for receipt of SYN
1356
;-------------------------------------
1365
 
1357
; Common processing for receipt of SYN
Line 1375... Line 1367...
1375
        mov     ecx, [ebx + TCP_SOCKET.RCV_WND]
1367
        mov     ecx, [ebx + TCP_SOCKET.RCV_WND]
1376
        and     [edx + TCP_header.Flags], not (TH_FIN)
1368
        and     [edx + TCP_header.Flags], not (TH_FIN)
1377
;;; TODO: update stats
1369
;;; TODO: update stats
Line 1378... Line 1370...
1378
 
1370
 
1379
  .dont_trim:
-
 
1380
 
1371
  .dont_trim:
1381
        mov     eax, [edx + TCP_header.SequenceNumber]
1372
        mov     eax, [edx + TCP_header.SequenceNumber]
1382
        mov     [ebx + TCP_SOCKET.RCV_UP], eax
1373
        mov     [ebx + TCP_SOCKET.RCV_UP], eax
1383
        dec     eax
1374
        dec     eax
Line 1384... Line -...
1384
        mov     [ebx + TCP_SOCKET.SND_WL1], eax
-
 
1385
 
-
 
1386
;-------
-
 
1387
; step 6
1375
        mov     [ebx + TCP_SOCKET.SND_WL1], eax
1388
 
-
 
1389
  .ack_processed:
1376
 
Line 1390... Line 1377...
1390
 
1377
  .ack_processed:
1391
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n"
1378
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK processed\n"
Line 1590... Line 1577...
1590
 
1577
 
1591
        test    [temp_bits], TCP_BIT_NEEDOUTPUT
1578
        test    [temp_bits], TCP_BIT_NEEDOUTPUT
Line 1592... Line 1579...
1592
        jnz     .need_output
1579
        jnz     .need_output
1593
 
1580
 
1594
        test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW
1581
        test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW
Line 1595... Line 1582...
1595
        jz      .dumpit
1582
        jz      .done
1596
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n"
1583
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: ACK now!\n"
1597
 
1584
 
Line 1598... Line 1585...
1598
  .need_output:
1585
  .need_output:
1599
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n"
1586
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: need output\n"
Line 1600... Line 1587...
1600
        call    TCP_output
1587
        call    TCP_output
1601
 
1588
 
Line 1617... Line 1604...
1617
        lea     ecx, [ebx + SOCKET.mutex]
1604
        lea     ecx, [ebx + SOCKET.mutex]
1618
        call    mutex_unlock
1605
        call    mutex_unlock
1619
        pop     eax edx
1606
        pop     eax edx
Line 1620... Line 1607...
1620
 
1607
 
1621
        test    [edx + TCP_header.Flags], TH_RST
1608
        test    [edx + TCP_header.Flags], TH_RST
Line 1622... Line 1609...
1622
        jnz     .dumpit
1609
        jnz     .done
1623
 
1610
 
Line 1624... Line 1611...
1624
        or      [eax + TCP_SOCKET.t_flags], TF_ACKNOW
1611
        or      [eax + TCP_SOCKET.t_flags], TF_ACKNOW
Line 1631... Line 1618...
1631
        lea     ecx, [ebx + SOCKET.mutex]
1618
        lea     ecx, [ebx + SOCKET.mutex]
1632
        call    mutex_unlock
1619
        call    mutex_unlock
1633
        pop     edx ebx
1620
        pop     edx ebx
Line 1634... Line 1621...
1634
 
1621
 
1635
        test    [edx + TCP_header.Flags], TH_RST
1622
        test    [edx + TCP_header.Flags], TH_RST
Line 1636... Line 1623...
1636
        jnz     .dumpit
1623
        jnz     .done
Line 1637... Line 1624...
1637
 
1624
 
1638
        ;;; if its a multicast/broadcast, also drop
1625
        ;;; if its a multicast/broadcast, also drop
Line 1639... Line 1626...
1639
 
1626
 
1640
        test    [edx + TCP_header.Flags], TH_ACK
1627
        test    [edx + TCP_header.Flags], TH_ACK
1641
        jnz     .respond_ack
1628
        jnz     .respond_ack
Line 1642... Line 1629...
1642
 
1629
 
1643
        test    [edx + TCP_header.Flags], TH_SYN
1630
        test    [edx + TCP_header.Flags], TH_SYN
Line 1644... Line 1631...
1644
        jnz     .respond_syn
1631
        jnz     .respond_syn
Line 1659... Line 1646...
1659
        mov     cl, TH_RST + TH_ACK
1646
        mov     cl, TH_RST + TH_ACK
1660
        call    TCP_respond
1647
        call    TCP_respond
1661
        pop     ebx
1648
        pop     ebx
1662
        jmp     .destroy_new_socket
1649
        jmp     .destroy_new_socket
Line -... Line 1650...
-
 
1650
 
1663
 
1651
;-----------------------------------------
Line -... Line 1652...
-
 
1652
; The connection has no associated socket
1664
  .no_socket:
1653
 
1665
 
1654
  .no_socket:
1666
        pusha
1655
        pusha
1667
        mov     ecx, socket_mutex
1656
        mov     ecx, socket_mutex
Line 1690... Line 1679...
1690
  .respond_seg_syn:
1679
  .respond_seg_syn:
1691
        mov     cl, TH_RST + TH_ACK
1680
        mov     cl, TH_RST + TH_ACK
1692
        call    TCP_respond_segment
1681
        call    TCP_respond_segment
1693
        jmp     .drop_no_socket
1682
        jmp     .drop_no_socket
Line 1694... Line 1683...
1694
 
1683
 
1695
;-----
1684
;------------------------------------------------
Line 1696... Line 1685...
1696
; Drop
1685
; Unlock socket mutex and prepare to drop segment
1697
 
1686
 
Line 1698... Line 1687...
1698
  .drop:
1687
  .drop:
1699
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n"
1688
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_input: Dropping segment\n"
1700
 
1689
 
1701
        pusha
1690
        pusha
Line -... Line 1691...
-
 
1691
        lea     ecx, [ebx + SOCKET.mutex]
-
 
1692
        call    mutex_unlock
-
 
1693
        popa
1702
        lea     ecx, [ebx + SOCKET.mutex]
1694
 
1703
        call    mutex_unlock
1695
;--------------------------------------------
1704
        popa
1696
; Destroy the newly created socket if needed
Line 1705... Line 1697...
1705
 
1697
 
1706
  .destroy_new_socket:
1698
  .destroy_new_socket:
Line -... Line 1699...
-
 
1699
        test    [temp_bits], TCP_BIT_DROPSOCKET
-
 
1700
        jz      .drop_no_socket
-
 
1701
 
1707
        test    [temp_bits], TCP_BIT_DROPSOCKET
1702
        mov     eax, ebx
1708
        jz      .drop_no_socket
1703
        call    SOCKET_free
Line 1709... Line 1704...
1709
 
1704
 
1710
        mov     eax, ebx
1705
;------------------