Rev 5976 | Rev 6476 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5363 | yogev_ezra | 3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
3545 | hidnplayr | 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: 6011 $ |
3545 | hidnplayr | 18 | |
19 | ; Socket states |
||
20 | TCPS_CLOSED = 0 |
||
21 | TCPS_LISTEN = 1 |
||
22 | TCPS_SYN_SENT = 2 |
||
23 | TCPS_SYN_RECEIVED = 3 |
||
24 | TCPS_ESTABLISHED = 4 |
||
25 | TCPS_CLOSE_WAIT = 5 |
||
26 | TCPS_FIN_WAIT_1 = 6 |
||
27 | TCPS_CLOSING = 7 |
||
28 | TCPS_LAST_ACK = 8 |
||
29 | TCPS_FIN_WAIT_2 = 9 |
||
30 | TCPS_TIMED_WAIT = 10 |
||
31 | |||
32 | ; Socket Flags |
||
33 | TF_ACKNOW = 1 shl 0 ; ack peer immediately |
||
34 | TF_DELACK = 1 shl 1 ; ack, but try to delay it |
||
35 | TF_NODELAY = 1 shl 2 ; don't delay packets to coalesce |
||
36 | TF_NOOPT = 1 shl 3 ; don't use tcp options |
||
37 | TF_SENTFIN = 1 shl 4 ; have sent FIN |
||
38 | TF_REQ_SCALE = 1 shl 5 ; have/will request window scaling |
||
39 | TF_RCVD_SCALE = 1 shl 6 ; other side has requested scaling |
||
40 | TF_REQ_TSTMP = 1 shl 7 ; have/will request timestamps |
||
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 |
||
43 | |||
44 | ; Segment flags |
||
45 | TH_FIN = 1 shl 0 |
||
46 | TH_SYN = 1 shl 1 |
||
47 | TH_RST = 1 shl 2 |
||
48 | TH_PUSH = 1 shl 3 |
||
49 | TH_ACK = 1 shl 4 |
||
50 | TH_URG = 1 shl 5 |
||
51 | |||
52 | ; Segment header options |
||
53 | TCP_OPT_EOL = 0 ; End of option list. |
||
54 | TCP_OPT_NOP = 1 ; No-Operation. |
||
55 | TCP_OPT_MAXSEG = 2 ; Maximum Segment Size. |
||
56 | TCP_OPT_WINDOW = 3 ; window scale |
||
57 | TCP_OPT_SACK_PERMIT = 4 ; Selective Acknowledgement |
||
58 | TCP_OPT_SACK = 5 |
||
59 | TCP_OPT_TIMESTAMP = 8 |
||
60 | |||
61 | ; Fundamental timer values |
||
62 | TCP_time_MSL = 47 ; max segment lifetime (30s) |
||
63 | TCP_time_re_min = 2 ; min retransmission (1,28s) |
||
64 | TCP_time_re_max = 100 ; max retransmission (64s) |
||
65 | TCP_time_pers_min = 8 ; min persist (5,12s) |
||
66 | TCP_time_pers_max = 94 ; max persist (60,16s) |
||
67 | TCP_time_keep_init = 118 ; connection establishment (75,52s) |
||
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) |
||
70 | TCP_time_rtt_default = 5 ; default Round Trip Time (3,2s) |
||
71 | TCP_time_srtt_default = 0 ; |
||
72 | TCP_time_max_idle = 8*TCP_time_keep_interval ; FIXME |
||
73 | |||
4030 | hidnplayr | 74 | TCP_time_connect = 300 ; in 1/100s (default=3s) |
75 | |||
3545 | hidnplayr | 76 | ; timer constants |
77 | TCP_max_rxtshift = 12 ; max retransmissions waiting for ACK |
||
78 | TCP_max_keepcnt = 8 ; max keepalive probes |
||
79 | |||
80 | ; |
||
81 | TCP_max_winshift = 14 |
||
82 | TCP_max_win = 65535 |
||
83 | |||
84 | TCP_re_xmit_thresh = 3 |
||
85 | |||
86 | TCP_mss_default = 1480 ; default max segment size |
||
87 | |||
88 | ; smoothed round trip time and estimated variance are stored as fixed point numbers, |
||
89 | ; shifted by the value below. |
||
90 | ; With these scales, srtt has 3 bits to the right of the binary point, and thus an "alpha" |
||
91 | ; of .875. rttvar has 2 bits to the right and thus "alpha" of 0.75 |
||
92 | TCP_RTT_SHIFT = 3 |
||
93 | TCP_RTTVAR_SHIFT = 2 |
||
94 | |||
95 | ; bits used by tcp_input and tcp_output |
||
96 | TCP_BIT_NEEDOUTPUT = 1 shl 0 |
||
97 | TCP_BIT_TIMESTAMP = 1 shl 1 |
||
98 | TCP_BIT_DROPSOCKET = 1 shl 2 |
||
5133 | hidnplayr | 99 | TCP_BIT_FIN_IS_ACKED = 1 shl 3 |
3545 | hidnplayr | 100 | |
101 | TCP_BIT_SENDALOT = 1 shl 0 |
||
102 | |||
103 | TCP_PAWS_IDLE = 24*24*60*60*100 ; 24 days, in 1/100 seconds |
||
104 | |||
105 | TCP_QUEUE_SIZE = 50 |
||
106 | |||
107 | struct TCP_header |
||
108 | |||
109 | SourcePort dw ? |
||
110 | DestinationPort dw ? |
||
111 | SequenceNumber dd ? |
||
112 | AckNumber dd ? |
||
113 | DataOffset db ? ; DataOffset[0-3 bits] and Reserved[4-7] |
||
114 | Flags db ? ; Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN |
||
115 | Window dw ? |
||
116 | Checksum dw ? |
||
117 | UrgentPointer dw ? |
||
118 | |||
119 | ends |
||
120 | |||
121 | struct TCP_queue_entry |
||
122 | |||
5133 | hidnplayr | 123 | ip_ptr dd ? |
124 | segment_ptr dd ? |
||
125 | segment_size dd ? |
||
126 | device_ptr dd ? |
||
5522 | hidnplayr | 127 | timestamp dd ? |
5133 | hidnplayr | 128 | buffer_ptr dd ? |
3545 | hidnplayr | 129 | |
130 | ends |
||
131 | |||
3697 | hidnplayr | 132 | uglobal |
3545 | hidnplayr | 133 | align 4 |
3697 | hidnplayr | 134 | |
3600 | hidnplayr | 135 | TCP_segments_tx rd NET_DEVICES_MAX |
136 | TCP_segments_rx rd NET_DEVICES_MAX |
||
137 | TCP_segments_missed rd NET_DEVICES_MAX |
||
138 | TCP_segments_dumped rd NET_DEVICES_MAX |
||
139 | ; TCP_bytes_rx rq NET_DEVICES_MAX |
||
140 | ; TCP_bytes_tx rq NET_DEVICES_MAX |
||
3545 | hidnplayr | 141 | TCP_sequence_num dd ? |
3697 | hidnplayr | 142 | TCP_queue rd (TCP_QUEUE_SIZE*sizeof.TCP_queue_entry + sizeof.queue)/4 |
3545 | hidnplayr | 143 | TCP_input_event dd ? |
5013 | hidnplayr | 144 | TCP_timer1_event dd ? |
3545 | hidnplayr | 145 | endg |
146 | |||
4387 | hidnplayr | 147 | uglobal |
148 | align 4 |
||
3545 | hidnplayr | 149 | |
4387 | hidnplayr | 150 | TCPS_accepts dd ? ; #SYNs received in LISTEN state |
151 | TCPS_closed dd ? ; #connections closed (includes drops) |
||
152 | TCPS_connattempt dd ? ; #connections initiated (calls to connect) |
||
153 | TCPS_conndrops dd ? ; #embryonic connections dropped (before SYN received) |
||
154 | TCPS_connects dd ? ; #connections established actively or passively |
||
155 | TCPS_delack dd ? ; #delayed ACKs sent |
||
156 | TCPS_drops dd ? ; #connections dropped (after SYN received) |
||
157 | TCPS_keepdrops dd ? ; #connections dropped in keepalive (established or awaiting SYN) |
||
158 | TCPS_keepprobe dd ? ; #keepalive probes sent |
||
159 | TCPS_keeptimeo dd ? ; #times keepalive timer or connections-establishment timer expire |
||
160 | TCPS_pawsdrop dd ? ; #segments dropped due to PAWS |
||
161 | TCPS_pcbcachemiss dd ? ; #times PCB cache comparison fails |
||
162 | TCPS_persisttimeo dd ? ; #times persist timer expires |
||
163 | TCPS_predack dd ? ; #times header prediction correct for ACKs |
||
164 | TCPS_preddat dd ? ; #times header prediction correct for data packets |
||
165 | TCPS_rcvackbyte dd ? ; #bytes ACKed by received ACKs |
||
166 | TCPS_rcvackpack dd ? ; #received ACK packets |
||
167 | TCPS_rcvacktoomuch dd ? ; #received ACKs for unsent data |
||
168 | TCPS_rcvafterclose dd ? ; #packets received after connection closed |
||
169 | TCPS_rcvbadoff dd ? ; #packets received with invalid header length |
||
170 | TCPS_rcvbadsum dd ? ; #packets received with checksum errors |
||
171 | TCPS_rcvbyte dd ? ; #bytes received in sequence |
||
172 | TCPS_rcvbyteafterwin dd ? ; #bytes received beyond advertised window |
||
173 | TCPS_rcvdupack dd ? ; #duplicate ACKs received |
||
174 | TCPS_rcvdupbyte dd ? ; #bytes receivedin completely duplicate packets |
||
175 | TCPS_rcvduppack dd ? ; #packets received with completely duplicate bytes |
||
176 | TCPS_rcvoobyte dd ? ; #out-of-order bytes received |
||
177 | TCPS_rcvoopack dd ? ; #out-of-order packets received |
||
178 | TCPS_rcvpack dd ? ; #packets received in sequence |
||
179 | TCPS_rcvpackafterwin dd ? ; #packets with some data beyond advertised window |
||
180 | TCPS_rcvpartdupbyte dd ? ; #duplicate bytes in part-duplicate packets |
||
181 | TCPS_rcvpartduppack dd ? ; #packets with some duplicate data |
||
182 | TCPS_rcvshort dd ? ; #packets received too short |
||
183 | TCPS_rcvtotal dd ? ; #total packets received |
||
184 | TCPS_rcvwinprobe dd ? ; #window probe packets received |
||
185 | TCPS_rcvwinupd dd ? ; #received window update packets |
||
186 | TCPS_rexmttimeo dd ? ; #retransmission timeouts |
||
187 | TCPS_rttupdated dd ? ; #times RTT estimators updated |
||
188 | TCPS_segstimed dd ? ; #segments for which TCP tried to measure RTT |
||
189 | TCPS_sndacks dd ? ; #ACK-only packets sent (data length = 0) |
||
190 | TCPS_sndbyte dd ? ; #data bytes sent |
||
191 | TCPS_sndctrl dd ? ; #control (SYN, FIN, RST) packets sent (data length = 0) |
||
192 | TCPS_sndpack dd ? ; #data packets sent (data length > 0) |
||
193 | TCPS_sndprobe dd ? ; #window probes sent (1 byte of data forced by persist timer) |
||
194 | TCPS_sndrexmitbyte dd ? ; #data bytes retransmitted |
||
195 | TCPS_sndrexmitpack dd ? ; #data packets retransmitted |
||
196 | TCPS_sndtotal dd ? ; total #packets sent |
||
197 | TCPS_sndurg dd ? ; #packets sent with URG-only (data length=0) |
||
198 | TCPS_sndwinup dd ? ; #window update-only packets sent (data length=0) |
||
199 | TCPS_timeoutdrop dd ? ; #connections dropped in retransmission timeout |
||
200 | |||
201 | endg |
||
202 | |||
203 | |||
5976 | hidnplayr | 204 | ;-----------------------------------------------------------------; |
205 | ; ; |
||
206 | ; TCP_init: Resets all TCP variables. ; |
||
207 | ; ; |
||
208 | ;-----------------------------------------------------------------; |
||
6011 | hidnplayr | 209 | macro tcp_init { |
3545 | hidnplayr | 210 | |
211 | xor eax, eax |
||
212 | mov edi, TCP_segments_tx |
||
3600 | hidnplayr | 213 | mov ecx, (6*NET_DEVICES_MAX) |
3711 | clevermous | 214 | rep stosd |
3545 | hidnplayr | 215 | |
216 | pseudo_random eax |
||
217 | mov [TCP_sequence_num], eax |
||
218 | |||
219 | init_queue TCP_queue |
||
220 | |||
3598 | clevermous | 221 | movi ebx, 1 |
6011 | hidnplayr | 222 | mov ecx, tcp_process_input |
3545 | hidnplayr | 223 | call new_sys_threads |
3697 | hidnplayr | 224 | test eax, eax |
225 | jns @f |
||
5013 | hidnplayr | 226 | DEBUGF DEBUG_NETWORK_ERROR,'K : cannot create kernel thread for TCP input, error %d\n', eax |
3697 | hidnplayr | 227 | @@: |
3545 | hidnplayr | 228 | |
5013 | hidnplayr | 229 | movi ebx, 1 |
6011 | hidnplayr | 230 | mov ecx, tcp_timer_640ms |
5013 | hidnplayr | 231 | call new_sys_threads |
232 | test eax, eax |
||
233 | jns @f |
||
234 | DEBUGF DEBUG_NETWORK_ERROR,'K : cannot create kernel thread for TCP timer, error %d\n', eax |
||
235 | @@: |
||
236 | |||
3545 | hidnplayr | 237 | } |
238 | |||
239 | |||
240 | include 'tcp_timer.inc' |
||
241 | include 'tcp_subr.inc' |
||
242 | include 'tcp_usreq.inc' |
||
243 | include 'tcp_input.inc' |
||
244 | include 'tcp_output.inc' |
||
245 | |||
246 | |||
5976 | hidnplayr | 247 | ;------------------------------------------------------------------; |
248 | ; ; |
||
6011 | hidnplayr | 249 | ; tcp_api: Part of system function 76 ; |
5976 | hidnplayr | 250 | ; ; |
251 | ; IN: bl = subfunction number ; |
||
252 | ; bh = device number ; |
||
253 | ; ecx, edx, .. depends on subfunction ; |
||
254 | ; ; |
||
255 | ; OUT: depends on subfunction ; |
||
256 | ; ; |
||
257 | ;------------------------------------------------------------------; |
||
3545 | hidnplayr | 258 | align 4 |
6011 | hidnplayr | 259 | tcp_api: |
3545 | hidnplayr | 260 | |
261 | movzx eax, bh |
||
262 | shl eax, 2 |
||
263 | |||
264 | test bl, bl |
||
265 | jz .packets_tx ; 0 |
||
266 | dec bl |
||
267 | jz .packets_rx ; 1 |
||
268 | dec bl |
||
269 | jz .packets_missed ; 2 |
||
270 | dec bl |
||
271 | jz .packets_dumped ; 3 |
||
5015 | hidnplayr | 272 | dec bl |
273 | jz .packets_queued ; 4 |
||
3545 | hidnplayr | 274 | |
275 | .error: |
||
276 | mov eax, -1 |
||
277 | ret |
||
278 | |||
279 | .packets_tx: |
||
280 | mov eax, [TCP_segments_tx + eax] |
||
281 | ret |
||
282 | |||
283 | .packets_rx: |
||
284 | mov eax, [TCP_segments_rx + eax] |
||
285 | ret |
||
286 | |||
287 | .packets_missed: |
||
288 | mov eax, [TCP_segments_missed + eax] |
||
289 | ret |
||
290 | |||
291 | .packets_dumped: |
||
292 | mov eax, [TCP_segments_dumped + eax] |
||
293 | ret |
||
5015 | hidnplayr | 294 | |
295 | .packets_queued: |
||
296 | mov eax, [TCP_queue + queue.size] |
||
297 | ret |