Rev 3389 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3389 | Rev 3406 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2013. 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 code of 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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
16 | 16 | ||
17 | $Revision: 3389 $ |
17 | $Revision: 3406 $ |
18 | 18 | ||
19 | ; Socket states |
19 | ; Socket states |
20 | TCPS_CLOSED = 0 |
20 | TCPS_CLOSED = 0 |
21 | TCPS_LISTEN = 1 |
21 | TCPS_LISTEN = 1 |
22 | TCPS_SYN_SENT = 2 |
22 | TCPS_SYN_SENT = 2 |
23 | TCPS_SYN_RECEIVED = 3 |
23 | TCPS_SYN_RECEIVED = 3 |
24 | TCPS_ESTABLISHED = 4 |
24 | TCPS_ESTABLISHED = 4 |
25 | TCPS_CLOSE_WAIT = 5 |
25 | TCPS_CLOSE_WAIT = 5 |
26 | TCPS_FIN_WAIT_1 = 6 |
26 | TCPS_FIN_WAIT_1 = 6 |
27 | TCPS_CLOSING = 7 |
27 | TCPS_CLOSING = 7 |
28 | TCPS_LAST_ACK = 8 |
28 | TCPS_LAST_ACK = 8 |
29 | TCPS_FIN_WAIT_2 = 9 |
29 | TCPS_FIN_WAIT_2 = 9 |
30 | TCPS_TIMED_WAIT = 10 |
30 | TCPS_TIMED_WAIT = 10 |
31 | 31 | ||
32 | ; Socket Flags |
32 | ; Socket Flags |
33 | TF_ACKNOW = 1 shl 0 ; ack peer immediately |
33 | TF_ACKNOW = 1 shl 0 ; ack peer immediately |
34 | TF_DELACK = 1 shl 1 ; ack, but try to delay it |
34 | TF_DELACK = 1 shl 1 ; ack, but try to delay it |
35 | TF_NODELAY = 1 shl 2 ; don't delay packets to coalesce |
35 | TF_NODELAY = 1 shl 2 ; don't delay packets to coalesce |
36 | TF_NOOPT = 1 shl 3 ; don't use tcp options |
36 | TF_NOOPT = 1 shl 3 ; don't use tcp options |
37 | TF_SENTFIN = 1 shl 4 ; have sent FIN |
37 | TF_SENTFIN = 1 shl 4 ; have sent FIN |
38 | TF_REQ_SCALE = 1 shl 5 ; have/will request window scaling |
38 | TF_REQ_SCALE = 1 shl 5 ; have/will request window scaling |
39 | TF_RCVD_SCALE = 1 shl 6 ; other side has requested scaling |
39 | TF_RCVD_SCALE = 1 shl 6 ; other side has requested scaling |
40 | TF_REQ_TSTMP = 1 shl 7 ; have/will request timestamps |
40 | TF_REQ_TSTMP = 1 shl 7 ; have/will request timestamps |
41 | TF_RCVD_TSTMP = 1 shl 8 ; a timestamp was received in SYN |
41 | TF_RCVD_TSTMP = 1 shl 8 ; a timestamp was received in SYN |
42 | TF_SACK_PERMIT = 1 shl 9 ; other side said I could SACK |
42 | TF_SACK_PERMIT = 1 shl 9 ; other side said I could SACK |
43 | 43 | ||
44 | ; Segment flags |
44 | ; Segment flags |
45 | TH_FIN = 1 shl 0 |
45 | TH_FIN = 1 shl 0 |
46 | TH_SYN = 1 shl 1 |
46 | TH_SYN = 1 shl 1 |
47 | TH_RST = 1 shl 2 |
47 | TH_RST = 1 shl 2 |
48 | TH_PUSH = 1 shl 3 |
48 | TH_PUSH = 1 shl 3 |
49 | TH_ACK = 1 shl 4 |
49 | TH_ACK = 1 shl 4 |
50 | TH_URG = 1 shl 5 |
50 | TH_URG = 1 shl 5 |
51 | 51 | ||
52 | ; Segment header options |
52 | ; Segment header options |
53 | TCP_OPT_EOL = 0 ; End of option list. |
53 | TCP_OPT_EOL = 0 ; End of option list. |
54 | TCP_OPT_NOP = 1 ; No-Operation. |
54 | TCP_OPT_NOP = 1 ; No-Operation. |
55 | TCP_OPT_MAXSEG = 2 ; Maximum Segment Size. |
55 | TCP_OPT_MAXSEG = 2 ; Maximum Segment Size. |
56 | TCP_OPT_WINDOW = 3 ; window scale |
56 | TCP_OPT_WINDOW = 3 ; window scale |
57 | TCP_OPT_SACK_PERMIT = 4 ; Selective Acknowledgement |
57 | TCP_OPT_SACK_PERMIT = 4 ; Selective Acknowledgement |
58 | TCP_OPT_SACK = 5 |
58 | TCP_OPT_SACK = 5 |
59 | TCP_OPT_TIMESTAMP = 8 |
59 | TCP_OPT_TIMESTAMP = 8 |
60 | 60 | ||
61 | ; Fundamental timer values |
61 | ; Fundamental timer values |
62 | TCP_time_MSL = 47 ; max segment lifetime (30s) |
62 | TCP_time_MSL = 47 ; max segment lifetime (30s) |
63 | TCP_time_re_min = 2 ; min retransmission (1,28s) |
63 | TCP_time_re_min = 2 ; min retransmission (1,28s) |
64 | TCP_time_re_max = 100 ; max retransmission (64s) |
64 | TCP_time_re_max = 100 ; max retransmission (64s) |
65 | TCP_time_pers_min = 8 ; min persist (5,12s) |
65 | TCP_time_pers_min = 8 ; min persist (5,12s) |
66 | TCP_time_pers_max = 94 ; max persist (60,16s) |
66 | TCP_time_pers_max = 94 ; max persist (60,16s) |
67 | TCP_time_keep_init = 118 ; connection establishment (75,52s) |
67 | TCP_time_keep_init = 118 ; connection establishment (75,52s) |
68 | TCP_time_keep_idle = 4608 ; idle time before 1st probe (2h) |
68 | TCP_time_keep_idle = 4608 ; idle time before 1st probe (2h) |
69 | TCP_time_keep_interval = 118 ; between probes when no response (75,52s) |
69 | TCP_time_keep_interval = 118 ; between probes when no response (75,52s) |
70 | TCP_time_rtt_default = 5 ; default Round Trip Time (3,2s) |
70 | TCP_time_rtt_default = 5 ; default Round Trip Time (3,2s) |
71 | TCP_time_srtt_default = 0 ; |
71 | TCP_time_srtt_default = 0 ; |
72 | TCP_time_max_idle = 8*TCP_time_keep_interval ; FIXME |
72 | TCP_time_max_idle = 8*TCP_time_keep_interval ; FIXME |
73 | 73 | ||
74 | ; timer constants |
74 | ; timer constants |
75 | TCP_max_rxtshift = 12 ; max retransmissions waiting for ACK |
75 | TCP_max_rxtshift = 12 ; max retransmissions waiting for ACK |
76 | TCP_max_keepcnt = 8 ; max keepalive probes |
76 | TCP_max_keepcnt = 8 ; max keepalive probes |
77 | 77 | ||
78 | ; |
78 | ; |
79 | TCP_max_winshift = 14 |
79 | TCP_max_winshift = 14 |
80 | TCP_max_win = 65535 |
80 | TCP_max_win = 65535 |
81 | 81 | ||
82 | TCP_re_xmit_thresh = 3 |
82 | TCP_re_xmit_thresh = 3 |
83 | 83 | ||
84 | TCP_mss_default = 1480 ; default max segment size |
84 | TCP_mss_default = 1480 ; default max segment size |
85 | 85 | ||
86 | ; smoothed round trip time and estimated variance are stored as fixed point numbers, |
86 | ; smoothed round trip time and estimated variance are stored as fixed point numbers, |
87 | ; shifted by the value below. |
87 | ; shifted by the value below. |
88 | ; With these scales, srtt has 3 bits to the right of the binary point, and thus an "alpha" |
88 | ; With these scales, srtt has 3 bits to the right of the binary point, and thus an "alpha" |
89 | ; of .875. rttvar has 2 bits to the right and thus "alpha" of 0.75 |
89 | ; of .875. rttvar has 2 bits to the right and thus "alpha" of 0.75 |
90 | TCP_RTT_SHIFT = 3 |
90 | TCP_RTT_SHIFT = 3 |
91 | TCP_RTTVAR_SHIFT = 2 |
91 | TCP_RTTVAR_SHIFT = 2 |
92 | 92 | ||
93 | ; bits used by tcp_input and tcp_output |
93 | ; bits used by tcp_input and tcp_output |
94 | TCP_BIT_NEEDOUTPUT = 1 shl 0 |
94 | TCP_BIT_NEEDOUTPUT = 1 shl 0 |
95 | TCP_BIT_TIMESTAMP = 1 shl 1 |
95 | TCP_BIT_TIMESTAMP = 1 shl 1 |
96 | TCP_BIT_DROPSOCKET = 1 shl 2 |
96 | TCP_BIT_DROPSOCKET = 1 shl 2 |
97 | 97 | ||
98 | TCP_BIT_SENDALOT = 1 shl 0 |
98 | TCP_BIT_SENDALOT = 1 shl 0 |
99 | 99 | ||
100 | TCP_PAWS_IDLE = 24*24*60*60*100 ; 24 days, in 1/100 seconds |
100 | TCP_PAWS_IDLE = 24*24*60*60*100 ; 24 days, in 1/100 seconds |
101 | 101 | ||
102 | TCP_QUEUE_SIZE = 50 |
102 | TCP_QUEUE_SIZE = 50 |
103 | 103 | ||
104 | struct TCP_header |
104 | struct TCP_header |
105 | 105 | ||
106 | SourcePort dw ? |
106 | SourcePort dw ? |
107 | DestinationPort dw ? |
107 | DestinationPort dw ? |
108 | SequenceNumber dd ? |
108 | SequenceNumber dd ? |
109 | AckNumber dd ? |
109 | AckNumber dd ? |
110 | DataOffset db ? ; DataOffset[0-3 bits] and Reserved[4-7] |
110 | DataOffset db ? ; DataOffset[0-3 bits] and Reserved[4-7] |
111 | Flags db ? ; Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN |
111 | Flags db ? ; Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN |
112 | Window dw ? |
112 | Window dw ? |
113 | Checksum dw ? |
113 | Checksum dw ? |
114 | UrgentPointer dw ? |
114 | UrgentPointer dw ? |
115 | 115 | ||
116 | ends |
116 | ends |
117 | 117 | ||
118 | struct TCP_queue_entry |
118 | struct TCP_queue_entry |
119 | 119 | ||
120 | ip_ptr dd ? |
120 | ip_ptr dd ? |
121 | segment_ptr dd ? |
121 | segment_ptr dd ? |
122 | segment_size dd ? |
122 | segment_size dd ? |
123 | device_ptr dd ? |
123 | device_ptr dd ? |
124 | 124 | ||
125 | buffer_ptr dd ? |
125 | buffer_ptr dd ? |
126 | buffer_size dd ? |
126 | timestamp dd ? |
127 | 127 | ||
128 | ends |
128 | ends |
129 | 129 | ||
130 | align 4 |
130 | align 4 |
131 | uglobal |
131 | uglobal |
132 | TCP_segments_tx rd MAX_NET_DEVICES |
132 | TCP_segments_tx rd MAX_NET_DEVICES |
133 | TCP_segments_rx rd MAX_NET_DEVICES |
133 | TCP_segments_rx rd MAX_NET_DEVICES |
134 | TCP_segments_missed rd MAX_NET_DEVICES |
134 | TCP_segments_missed rd MAX_NET_DEVICES |
135 | TCP_segments_dumped rd MAX_NET_DEVICES |
135 | TCP_segments_dumped rd MAX_NET_DEVICES |
136 | ; TCP_bytes_rx rq MAX_NET_DEVICES |
136 | ; TCP_bytes_rx rq MAX_NET_DEVICES |
137 | ; TCP_bytes_tx rq MAX_NET_DEVICES |
137 | ; TCP_bytes_tx rq MAX_NET_DEVICES |
138 | TCP_sequence_num dd ? |
138 | TCP_sequence_num dd ? |
139 | TCP_queue rd TCP_QUEUE_SIZE*sizeof.TCP_queue_entry/4 |
139 | TCP_queue rd TCP_QUEUE_SIZE*sizeof.TCP_queue_entry/4 |
140 | TCP_input_event dd ? |
140 | TCP_input_event dd ? |
141 | endg |
141 | endg |
142 | 142 | ||
143 | 143 | ||
144 | ;----------------------------------------------------------------- |
144 | ;----------------------------------------------------------------- |
145 | ; |
145 | ; |
146 | ; TCP_init |
146 | ; TCP_init |
147 | ; |
147 | ; |
148 | ; This function resets all TCP variables |
148 | ; This function resets all TCP variables |
149 | ; |
149 | ; |
150 | ;----------------------------------------------------------------- |
150 | ;----------------------------------------------------------------- |
151 | macro TCP_init { |
151 | macro TCP_init { |
152 | 152 | ||
153 | xor eax, eax |
153 | xor eax, eax |
154 | mov edi, TCP_segments_tx |
154 | mov edi, TCP_segments_tx |
155 | mov ecx, (6*MAX_NET_DEVICES) |
155 | mov ecx, (6*MAX_NET_DEVICES) |
156 | rep stosd |
156 | rep stosd |
157 | 157 | ||
158 | pseudo_random eax |
158 | pseudo_random eax |
159 | mov [TCP_sequence_num], eax |
159 | mov [TCP_sequence_num], eax |
160 | 160 | ||
161 | init_queue TCP_queue |
161 | init_queue TCP_queue |
162 | 162 | ||
163 | push 1 |
163 | push 1 |
164 | pop ebx |
164 | pop ebx |
165 | mov ecx, TCP_process_input |
165 | mov ecx, TCP_process_input |
166 | call new_sys_threads |
166 | call new_sys_threads |
167 | 167 | ||
168 | } |
168 | } |
169 | 169 | ||
170 | 170 | ||
171 | include 'tcp_timer.inc' |
171 | include 'tcp_timer.inc' |
172 | include 'tcp_subr.inc' |
172 | include 'tcp_subr.inc' |
173 | include 'tcp_usreq.inc' |
173 | include 'tcp_usreq.inc' |
174 | include 'tcp_input.inc' |
174 | include 'tcp_input.inc' |
175 | include 'tcp_output.inc' |
175 | include 'tcp_output.inc' |
176 | 176 | ||
177 | 177 | ||
178 | ;--------------------------------------------------------------------------- |
178 | ;--------------------------------------------------------------------------- |
179 | ; |
179 | ; |
180 | ; TCP_API |
180 | ; TCP_API |
181 | ; |
181 | ; |
182 | ; This function is called by system function 76 |
182 | ; This function is called by system function 76 |
183 | ; |
183 | ; |
184 | ; IN: subfunction number in bl |
184 | ; IN: subfunction number in bl |
185 | ; device number in bh |
185 | ; device number in bh |
186 | ; ecx, edx, .. depends on subfunction |
186 | ; ecx, edx, .. depends on subfunction |
187 | ; |
187 | ; |
188 | ; OUT: |
188 | ; OUT: |
189 | ; |
189 | ; |
190 | ;--------------------------------------------------------------------------- |
190 | ;--------------------------------------------------------------------------- |
191 | align 4 |
191 | align 4 |
192 | TCP_api: |
192 | TCP_api: |
193 | 193 | ||
194 | movzx eax, bh |
194 | movzx eax, bh |
195 | shl eax, 2 |
195 | shl eax, 2 |
196 | 196 | ||
197 | test bl, bl |
197 | test bl, bl |
198 | jz .packets_tx ; 0 |
198 | jz .packets_tx ; 0 |
199 | dec bl |
199 | dec bl |
200 | jz .packets_rx ; 1 |
200 | jz .packets_rx ; 1 |
201 | dec bl |
201 | dec bl |
202 | jz .packets_missed ; 2 |
202 | jz .packets_missed ; 2 |
203 | dec bl |
203 | dec bl |
204 | jz .packets_dumped ; 3 |
204 | jz .packets_dumped ; 3 |
205 | 205 | ||
206 | .error: |
206 | .error: |
207 | mov eax, -1 |
207 | mov eax, -1 |
208 | ret |
208 | ret |
209 | 209 | ||
210 | .packets_tx: |
210 | .packets_tx: |
211 | mov eax, [TCP_segments_tx + eax] |
211 | mov eax, [TCP_segments_tx + eax] |
212 | ret |
212 | ret |
213 | 213 | ||
214 | .packets_rx: |
214 | .packets_rx: |
215 | mov eax, [TCP_segments_rx + eax] |
215 | mov eax, [TCP_segments_rx + eax] |
216 | ret |
216 | ret |
217 | 217 | ||
218 | .packets_missed: |
218 | .packets_missed: |
219 | mov eax, [TCP_segments_missed + eax] |
219 | mov eax, [TCP_segments_missed + eax] |
220 | ret |
220 | ret |
221 | 221 | ||
222 | .packets_dumped: |
222 | .packets_dumped: |
223 | mov eax, [TCP_segments_dumped + eax] |
223 | mov eax, [TCP_segments_dumped + eax] |
224 | ret |
224 | ret |