Rev 1251 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1171 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; STACK.INC ;; |
||
7 | ;; ;; |
||
8 | ;; BASIC TCP/IP stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
12 | ;; based on the work of Mike Hibbett, mikeh@oceanfree.net ;; |
||
13 | ;; but also Paolo Franchetti ;; |
||
14 | ;; ;; |
||
15 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
16 | ;; Version 2, June 1991 ;; |
||
17 | ;; ;; |
||
18 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
1159 | hidnplayr | 19 | |
1206 | hidnplayr | 20 | $Revision: 1254 $ |
1159 | hidnplayr | 21 | |
22 | uglobal |
||
23 | last_1sTick db ? |
||
24 | last_1hsTick dd ? |
||
25 | endg |
||
26 | |||
27 | MAX_NET_DEVICES equ 16 |
||
28 | |||
1185 | hidnplayr | 29 | MIN_EPHEMERAL_PORT equ 49152 |
30 | MAX_EPHEMERAL_PORT equ 61000 |
||
1159 | hidnplayr | 31 | |
1185 | hidnplayr | 32 | ETHER equ 1337 |
33 | ETHER_ARP equ 0x0608 |
||
34 | |||
1254 | hidnplayr | 35 | AF_UNSPEC equ 0 |
1249 | hidnplayr | 36 | AF_UNIX equ 1 |
1159 | hidnplayr | 37 | AF_INET4 equ 2 |
38 | ;AF_AX25 equ 3 |
||
39 | ;AF_IPX equ 4 |
||
40 | ;AF_APPLETALK equ 5 |
||
41 | ;AF_NETROM equ 6 |
||
42 | ;AF_BRIDGE equ 7 |
||
43 | ;AF_AAL5 equ 8 |
||
44 | ;AF_X25 equ 9 |
||
45 | ;AF_INET6 equ 10 |
||
46 | ;AF_MAX equ 12 |
||
47 | |||
48 | IP_PROTO_IP equ 0 |
||
49 | IP_PROTO_ICMP equ 1 |
||
50 | IP_PROTO_TCP equ 6 |
||
51 | IP_PROTO_UDP equ 17 |
||
52 | |||
1200 | hidnplayr | 53 | ; Socket types |
54 | SOCK_STREAM = 1 |
||
55 | SOCK_DGRAM = 2 |
||
56 | SOCK_RAW = 3 |
||
57 | |||
1254 | hidnplayr | 58 | TCB_LISTEN equ 1 |
59 | TCB_SYN_SENT equ 2 |
||
60 | TCB_SYN_RECEIVED equ 3 |
||
61 | TCB_ESTABLISHED equ 4 |
||
62 | TCB_FIN_WAIT_1 equ 5 |
||
63 | TCB_FIN_WAIT_2 equ 6 |
||
64 | TCB_CLOSE_WAIT equ 7 |
||
65 | TCB_CLOSING equ 8 |
||
66 | TCB_LAST_ACK equ 9 |
||
67 | TCB_TIMED_WAIT equ 10 |
||
68 | TCB_CLOSED equ 11 |
||
1159 | hidnplayr | 69 | |
1254 | hidnplayr | 70 | TH_FIN equ 1 shl 0 |
71 | TH_SYN equ 1 shl 1 |
||
72 | TH_RST equ 1 shl 2 |
||
73 | TH_PUSH equ 1 shl 3 |
||
74 | TH_ACK equ 1 shl 4 |
||
75 | TH_URG equ 1 shl 5 |
||
76 | |||
77 | |||
78 | macro inc_INET reg { |
||
79 | |||
80 | inc byte [reg + 3] |
||
81 | adc byte [reg + 2], 0 |
||
82 | adc byte [reg + 1], 0 |
||
83 | adc byte [reg + 0], 0 |
||
84 | |||
85 | } |
||
86 | |||
87 | |||
88 | macro add_INET reg { |
||
89 | |||
90 | rol ecx, 16 |
||
91 | adc byte [reg + 3], ch |
||
92 | adc byte [reg + 2], cl |
||
93 | rol ecx, 16 |
||
94 | adc byte [reg + 1], ch |
||
95 | adc byte [reg + 0], cl |
||
96 | |||
97 | } |
||
98 | |||
1159 | hidnplayr | 99 | include "queue.inc" |
1187 | hidnplayr | 100 | include "ARP.inc" |
101 | include "IPv4.inc" |
||
1159 | hidnplayr | 102 | include "ethernet.inc" |
103 | include "socket.inc" |
||
1249 | hidnplayr | 104 | include "tcp.inc" |
1185 | hidnplayr | 105 | include "udp.inc" |
106 | include "icmp.inc" |
||
1159 | hidnplayr | 107 | |
108 | ;----------------------------------------------- |
||
109 | ; |
||
110 | ; stack_init |
||
111 | ; |
||
112 | ; This function calls all network init procedures |
||
113 | ; |
||
114 | ; IN: / |
||
115 | ; OUT: / |
||
116 | ; |
||
117 | ;----------------------------------------------- |
||
118 | |||
119 | align 4 |
||
120 | stack_init: |
||
121 | |||
122 | call ETH_init |
||
123 | call IPv4_init |
||
124 | call ARP_init |
||
125 | call UDP_init |
||
1249 | hidnplayr | 126 | call TCP_init |
1159 | hidnplayr | 127 | call ICMP_init |
128 | call socket_init |
||
129 | |||
1254 | hidnplayr | 130 | mov al, 0 ; set up 1s timer |
1159 | hidnplayr | 131 | out 0x70, al |
132 | in al, 0x71 |
||
133 | mov [last_1sTick], al |
||
134 | |||
135 | ret |
||
136 | |||
137 | |||
138 | |||
139 | ;----------------------------------------------- |
||
140 | ; |
||
141 | ; stack_handler |
||
142 | ; |
||
143 | ; This function calls all network init procedures |
||
144 | ; |
||
145 | ; IN: / |
||
146 | ; OUT: / |
||
147 | ; |
||
148 | ;----------------------------------------------- |
||
149 | |||
150 | align 4 |
||
151 | stack_handler: |
||
152 | |||
153 | cmp [ETH_RUNNING], 0 |
||
154 | je .exit |
||
155 | |||
1254 | hidnplayr | 156 | ; Test for 1/100 s (10ms) tick |
1159 | hidnplayr | 157 | mov eax, [timer_ticks] |
158 | cmp eax, [last_1hsTick] |
||
1185 | hidnplayr | 159 | je .exit |
1159 | hidnplayr | 160 | |
161 | mov [last_1hsTick], eax |
||
162 | |||
1249 | hidnplayr | 163 | call ETH_handler ; handle all queued ethernet packets |
164 | call ETH_send_queued |
||
165 | call TCP_send_queued |
||
166 | |||
1159 | hidnplayr | 167 | .sec_tick: |
168 | |||
1254 | hidnplayr | 169 | ; Test for 1 second tick |
170 | mov al, 0 |
||
1159 | hidnplayr | 171 | out 0x70, al |
172 | in al, 0x71 |
||
173 | cmp al, [last_1sTick] |
||
174 | je .exit |
||
175 | |||
176 | mov [last_1sTick], al |
||
177 | |||
1187 | hidnplayr | 178 | call ARP_decrease_entry_ttls |
1159 | hidnplayr | 179 | call IPv4_decrease_fragment_ttls |
1249 | hidnplayr | 180 | call TCP_decrease_socket_ttls |
1159 | hidnplayr | 181 | |
182 | .exit: |
||
183 | ret |
||
184 | |||
185 | |||
186 | |||
187 | |||
188 | |||
1249 | hidnplayr | 189 | ;----------------------------------------------------------------- |
190 | ; |
||
191 | ; checksum_1 |
||
192 | ; |
||
193 | ; This is the first of two functions needed to calculate the TCP checksum. |
||
194 | ; |
||
195 | ; IN: edx = start offeset for semi-checksum |
||
196 | ; esi = pointer to data |
||
197 | ; ecx = data size |
||
198 | ; OUT: edx = semi-checksum |
||
199 | ; |
||
200 | ;----------------------------------------------------------------- |
||
1159 | hidnplayr | 201 | |
1249 | hidnplayr | 202 | align 4 |
203 | checksum_1: |
||
1159 | hidnplayr | 204 | |
1249 | hidnplayr | 205 | xor eax, eax |
206 | shr ecx, 1 |
||
207 | pushf |
||
208 | .loop: |
||
209 | lodsw |
||
210 | xchg al, ah |
||
211 | add edx, eax |
||
212 | loop .loop |
||
1159 | hidnplayr | 213 | |
1249 | hidnplayr | 214 | popf |
215 | jnc .end |
||
1159 | hidnplayr | 216 | |
1251 | clevermous | 217 | add dh, [esi] |
218 | adc edx, 0 |
||
1159 | hidnplayr | 219 | |
1249 | hidnplayr | 220 | .end: |
221 | |||
222 | ret |
||
223 | |||
224 | |||
225 | |||
226 | ;----------------------------------------------------------------- |
||
227 | ; |
||
228 | ; checksum_2 |
||
229 | ; |
||
230 | ; This function calculates the final ip/tcp/udp checksum for you |
||
231 | ; |
||
232 | ; IN: edx = semi-checksum |
||
233 | ; OUT: dx = checksum (in INET byte order) |
||
234 | ; |
||
235 | ;----------------------------------------------------------------- |
||
236 | |||
237 | align 4 |
||
238 | checksum_2: |
||
239 | |||
240 | mov ecx, edx |
||
241 | shr ecx, 16 |
||
242 | and edx, 0xffff |
||
243 | add edx, ecx |
||
244 | mov eax, edx |
||
245 | shr eax, 16 |
||
246 | add edx, eax |
||
247 | |||
248 | not dx |
||
249 | jnz .not_zero |
||
250 | dec dx |
||
251 | .not_zero: |
||
252 | xchg dl, dh |
||
253 | |||
254 | DEBUGF 1,"Checksum: %x\n",dx |
||
255 | |||
256 | ret |
||
257 | |||
258 | |||
259 | |||
1159 | hidnplayr | 260 | ;---------------------------------------------------------------- |
261 | ; |
||
1171 | hidnplayr | 262 | ; System function to work with network devices (73) |
1159 | hidnplayr | 263 | ; |
264 | ;---------------------------------------------------------------- |
||
265 | |||
266 | align 4 |
||
267 | sys_network: |
||
268 | |||
1196 | hidnplayr | 269 | cmp ebx, -1 |
270 | jne @f |
||
271 | |||
272 | mov eax, [ETH_RUNNING] |
||
273 | jmp .return |
||
274 | |||
275 | @@: |
||
1159 | hidnplayr | 276 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
277 | jge .doesnt_exist |
||
278 | |||
279 | mov esi, ebx |
||
280 | and esi, 0x0000ff00 |
||
281 | shr esi, 6 |
||
282 | |||
283 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running |
||
284 | je .doesnt_exist |
||
285 | |||
286 | test bl, bl ; 0 = Get device type (ethernet/token ring/...) |
||
287 | jnz @f |
||
1196 | hidnplayr | 288 | ; todo |
1192 | hidnplayr | 289 | xor eax, eax |
290 | jmp .return |
||
1159 | hidnplayr | 291 | |
292 | |||
293 | @@: |
||
294 | dec bl ; 1 = Get device name |
||
295 | jnz @f |
||
296 | |||
297 | mov esi, [esi + ETH_DRV_LIST] |
||
298 | mov esi, [esi + ETH_DEVICE.name] |
||
299 | mov edi, ecx |
||
300 | |||
301 | mov ecx, 64 ; max length |
||
302 | repnz movsb |
||
303 | |||
1192 | hidnplayr | 304 | xor eax, eax |
305 | jmp .return |
||
1159 | hidnplayr | 306 | |
1192 | hidnplayr | 307 | @@: |
1159 | hidnplayr | 308 | |
1192 | hidnplayr | 309 | dec bl ; 2 = Reset the device |
310 | jnz @f |
||
311 | |||
312 | mov esi, [esi + ETH_DRV_LIST] |
||
313 | call [esi + ETH_DEVICE.reset] |
||
314 | jmp .return |
||
315 | |||
1159 | hidnplayr | 316 | @@: |
1192 | hidnplayr | 317 | |
318 | dec bl ; 3 = Stop driver for this device |
||
319 | jnz @f |
||
320 | |||
321 | mov esi, [esi + ETH_DRV_LIST] |
||
322 | call [esi + ETH_DEVICE.unload] |
||
323 | jmp .return |
||
324 | |||
325 | @@: |
||
1249 | hidnplayr | 326 | dec bl ; 4 = Get driver pointer |
327 | jnz @f |
||
1192 | hidnplayr | 328 | |
1249 | hidnplayr | 329 | ; ..; |
330 | |||
331 | |||
332 | @@: |
||
333 | ; ... ; 5 Get driver name |
||
334 | |||
1159 | hidnplayr | 335 | .doesnt_exist: |
336 | DEBUGF 1,"sys_network: invalid device/function specified!\n" |
||
337 | mov eax, -1 |
||
338 | |||
1192 | hidnplayr | 339 | .return: |
340 | mov [esp+28+4], eax |
||
1159 | hidnplayr | 341 | ret |
342 | |||
343 | |||
344 | ;---------------------------------------------------------------- |
||
345 | ; |
||
1171 | hidnplayr | 346 | ; System Function To work with Protocols (75) |
1159 | hidnplayr | 347 | ; |
348 | ;---------------------------------------------------------------- |
||
349 | |||
350 | align 4 |
||
351 | sys_protocols: |
||
352 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
||
353 | jge .doesnt_exist |
||
354 | |||
355 | mov esi, ebx |
||
356 | and esi, 0x0000ff00 |
||
357 | shr esi, 6 |
||
1171 | hidnplayr | 358 | cmp dword [esi + ETH_DRV_LIST], 0 ; check if driver is running TODO: check other lists too |
1159 | hidnplayr | 359 | je .doesnt_exist |
360 | |||
361 | push .return ; return address (we will be using jumps instead of calls) |
||
362 | |||
363 | mov eax, ebx ; set ax to protocol number |
||
364 | shr eax, 16 ; |
||
365 | |||
366 | cmp ax , IP_PROTO_IP |
||
367 | je IPv4_API |
||
368 | |||
369 | cmp ax , IP_PROTO_ICMP |
||
370 | je ICMP_API |
||
371 | |||
372 | cmp ax , IP_PROTO_UDP |
||
373 | je UDP_API |
||
374 | |||
1171 | hidnplayr | 375 | cmp ax , IP_PROTO_TCP |
1254 | hidnplayr | 376 | je TCP_API |
1159 | hidnplayr | 377 | |
1171 | hidnplayr | 378 | cmp ax , ETHER_ARP |
1159 | hidnplayr | 379 | je ARP_API |
380 | |||
1185 | hidnplayr | 381 | cmp ax , ETHER |
1159 | hidnplayr | 382 | je ETH_API |
383 | |||
1171 | hidnplayr | 384 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
1159 | hidnplayr | 385 | |
386 | .doesnt_exist: |
||
1171 | hidnplayr | 387 | DEBUGF 1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh |
1159 | hidnplayr | 388 | mov eax, -1 |
389 | |||
390 | .return: |
||
1171 | hidnplayr | 391 | mov [esp+28+4], eax |
1159 | hidnplayr | 392 | ret |