Rev 2302 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1171 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
1763 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;; |
1171 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; STACK.INC ;; |
||
7 | ;; ;; |
||
1763 | hidnplayr | 8 | ;; TCP/IP stack for KolibriOS ;; |
1171 | hidnplayr | 9 | ;; ;; |
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
1763 | hidnplayr | 12 | ;; Some parts of code are based on the work of: ;; |
13 | ;; Mike Hibbett (menuetos network stack) ;; |
||
14 | ;; Eugen Brasoveanu (solar os network stack and drivers) ;; |
||
15 | ;; mike.dld (kolibrios socket code) ;; |
||
1171 | hidnplayr | 16 | ;; ;; |
1763 | hidnplayr | 17 | ;; TCP part is based on 4.4BSD ;; |
18 | ;; ;; |
||
1171 | hidnplayr | 19 | ;; GNU GENERAL PUBLIC LICENSE ;; |
20 | ;; Version 2, June 1991 ;; |
||
21 | ;; ;; |
||
22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
1159 | hidnplayr | 23 | |
1206 | hidnplayr | 24 | $Revision: 2305 $ |
1159 | hidnplayr | 25 | |
1763 | hidnplayr | 26 | __DEBUG_LEVEL_OLD__ equ __DEBUG_LEVEL__ ; use seperate debug level for network part of kernel |
27 | __DEBUG_LEVEL__ equ 1 |
||
1473 | hidnplayr | 28 | |
1159 | hidnplayr | 29 | uglobal |
1763 | hidnplayr | 30 | net_10ms dd ? |
31 | net_tmr_count dw ? |
||
1159 | hidnplayr | 32 | endg |
33 | |||
1259 | hidnplayr | 34 | MAX_NET_DEVICES equ 16 |
1159 | hidnplayr | 35 | |
1763 | hidnplayr | 36 | MIN_EPHEMERAL_PORT equ 49152 |
37 | MAX_EPHEMERAL_PORT equ 61000 |
||
1159 | hidnplayr | 38 | |
1514 | hidnplayr | 39 | ; Ethernet protocol numbers |
1519 | hidnplayr | 40 | ETHER_ARP equ 0x0608 |
1733 | hidnplayr | 41 | ETHER_IPv4 equ 0x0008 |
1519 | hidnplayr | 42 | ETHER_PPP_DISCOVERY equ 0x6388 |
43 | ETHER_PPP_SESSION equ 0x6488 |
||
1185 | hidnplayr | 44 | |
1514 | hidnplayr | 45 | ;Protocol family |
1774 | hidnplayr | 46 | AF_UNSPEC equ 0 |
47 | AF_UNIX equ 1 |
||
48 | AF_INET4 equ 2 |
||
49 | AF_INET6 equ 10 |
||
1159 | hidnplayr | 50 | |
1514 | hidnplayr | 51 | ; Internet protocol numbers |
1774 | hidnplayr | 52 | IP_PROTO_IP equ 0 |
53 | IP_PROTO_ICMP equ 1 |
||
54 | IP_PROTO_TCP equ 6 |
||
55 | IP_PROTO_UDP equ 17 |
||
1159 | hidnplayr | 56 | |
1200 | hidnplayr | 57 | ; Socket types |
1774 | hidnplayr | 58 | SOCK_STREAM equ 1 |
59 | SOCK_DGRAM equ 2 |
||
60 | SOCK_RAW equ 3 |
||
1200 | hidnplayr | 61 | |
1514 | hidnplayr | 62 | ; Socket options |
1885 | hidnplayr | 63 | SO_ACCEPTCON equ 1 shl 0 |
64 | SO_BROADCAST equ 1 shl 1 |
||
65 | SO_DEBUG equ 1 shl 2 |
||
66 | SO_DONTROUTE equ 1 shl 3 |
||
67 | SO_KEEPALIVE equ 1 shl 4 |
||
68 | SO_OOBINLINE equ 1 shl 5 |
||
69 | SO_REUSEADDR equ 1 shl 6 |
||
70 | SO_REUSEPORT equ 1 shl 7 |
||
71 | SO_USELOOPBACK equ 1 shl 8 |
||
1159 | hidnplayr | 72 | |
1885 | hidnplayr | 73 | |
1773 | hidnplayr | 74 | ; Socket States |
1774 | hidnplayr | 75 | SS_NOFDREF equ 0x001 ; no file table ref any more |
76 | SS_ISCONNECTED equ 0x002 ; socket connected to a peer |
||
77 | SS_ISCONNECTING equ 0x004 ; in process of connecting to peer |
||
78 | SS_ISDISCONNECTING equ 0x008 ; in process of disconnecting |
||
79 | SS_CANTSENDMORE equ 0x010 ; can't send more data to peer |
||
80 | SS_CANTRCVMORE equ 0x020 ; can't receive more data from peer |
||
81 | SS_RCVATMARK equ 0x040 ; at mark on input |
||
82 | SS_ISABORTING equ 0x080 ; aborting fd references - close() |
||
83 | SS_RESTARTSYS equ 0x100 ; restart blocked system calls |
||
84 | SS_ISDISCONNECTED equ 0x800 ; socket disconnected from peer |
||
1773 | hidnplayr | 85 | |
1774 | hidnplayr | 86 | SS_ASYNC equ 0x100 ; async i/o notify |
87 | SS_ISCONFIRMING equ 0x200 ; deciding to accept connection req |
||
88 | SS_MORETOCOME equ 0x400 |
||
1773 | hidnplayr | 89 | |
90 | |||
1774 | hidnplayr | 91 | SOCKET_MAXDATA equ 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8 |
1254 | hidnplayr | 92 | |
1514 | hidnplayr | 93 | ; Network driver types |
1774 | hidnplayr | 94 | NET_TYPE_ETH equ 1 |
95 | NET_TYPE_SLIP equ 2 |
||
1254 | hidnplayr | 96 | |
1774 | hidnplayr | 97 | MAX_backlog equ 20 ; maximum backlog for stream sockets |
1254 | hidnplayr | 98 | |
1716 | hidnplayr | 99 | ; Error Codes |
1774 | hidnplayr | 100 | ENOBUFS equ 55 |
101 | ECONNREFUSED equ 61 |
||
102 | ECONNRESET equ 52 |
||
103 | ETIMEDOUT equ 60 |
||
104 | ECONNABORTED equ 53 |
||
1529 | hidnplayr | 105 | |
1716 | hidnplayr | 106 | |
107 | |||
2305 | hidnplayr | 108 | struct NET_DEVICE |
1254 | hidnplayr | 109 | |
2305 | hidnplayr | 110 | type dd ? ; Type field |
111 | mtu dd ? ; Maximal Transmission Unit |
||
112 | name dd ? ; Ptr to 0 terminated string |
||
1519 | hidnplayr | 113 | |
2305 | hidnplayr | 114 | unload dd ? ; Ptrs to driver functions |
115 | reset dd ? ; |
||
116 | transmit dd ? ; |
||
1519 | hidnplayr | 117 | |
2305 | hidnplayr | 118 | bytes_tx dq ? ; Statistics, updated by the driver |
119 | bytes_rx dq ? ; |
||
120 | packets_tx dd ? ; |
||
121 | packets_rx dd ? ; |
||
1519 | hidnplayr | 122 | |
2305 | hidnplayr | 123 | ; hwacc dd ? ; bitmask stating available hardware accelerations (offload engines) |
1519 | hidnplayr | 124 | |
2305 | hidnplayr | 125 | ends |
1529 | hidnplayr | 126 | |
1254 | hidnplayr | 127 | |
1514 | hidnplayr | 128 | ; Exactly as it says.. |
1318 | hidnplayr | 129 | macro pseudo_random reg { |
130 | add reg, [esp] |
||
131 | rol reg, 5 |
||
132 | xor reg, [timer_ticks] |
||
1529 | hidnplayr | 133 | add reg, [CPU_FREQ] |
1318 | hidnplayr | 134 | imul reg, 214013 |
135 | xor reg, 0xdeadbeef |
||
136 | rol reg, 9 |
||
1514 | hidnplayr | 137 | } |
1318 | hidnplayr | 138 | |
1543 | hidnplayr | 139 | macro ntohd reg { |
1318 | hidnplayr | 140 | |
1514 | hidnplayr | 141 | rol word reg, 8 |
142 | rol dword reg, 16 |
||
1529 | hidnplayr | 143 | rol word reg , 8 |
1514 | hidnplayr | 144 | |
1318 | hidnplayr | 145 | } |
146 | |||
1543 | hidnplayr | 147 | macro ntohw reg { |
1514 | hidnplayr | 148 | |
149 | rol word reg, 8 |
||
150 | |||
151 | } |
||
152 | |||
1159 | hidnplayr | 153 | include "queue.inc" |
1514 | hidnplayr | 154 | |
155 | include "ethernet.inc" |
||
1719 | hidnplayr | 156 | |
1514 | hidnplayr | 157 | ;include "slip.inc" |
1719 | hidnplayr | 158 | ;include "pppoe.inc" |
1514 | hidnplayr | 159 | |
1187 | hidnplayr | 160 | include "ARP.inc" |
161 | include "IPv4.inc" |
||
1514 | hidnplayr | 162 | |
163 | include "icmp.inc" |
||
164 | include "udp.inc" |
||
1249 | hidnplayr | 165 | include "tcp.inc" |
1159 | hidnplayr | 166 | |
1514 | hidnplayr | 167 | include "socket.inc" |
168 | |||
169 | |||
170 | |||
171 | align 4 |
||
172 | uglobal |
||
173 | |||
174 | NET_RUNNING dd ? |
||
2220 | hidnplayr | 175 | NET_DRV_LIST rd (MAX_NET_DEVICES + 1) ; device 0 is a link to the default device |
1514 | hidnplayr | 176 | |
177 | endg |
||
178 | |||
179 | |||
1257 | hidnplayr | 180 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 181 | ; |
182 | ; stack_init |
||
183 | ; |
||
184 | ; This function calls all network init procedures |
||
185 | ; |
||
186 | ; IN: / |
||
187 | ; OUT: / |
||
188 | ; |
||
1257 | hidnplayr | 189 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 190 | align 4 |
191 | stack_init: |
||
192 | |||
1514 | hidnplayr | 193 | ; Init the network drivers list |
194 | xor eax, eax |
||
195 | mov edi, NET_RUNNING |
||
2220 | hidnplayr | 196 | mov ecx, (MAX_NET_DEVICES + 2) |
1514 | hidnplayr | 197 | rep stosd |
198 | |||
1529 | hidnplayr | 199 | ; SLIP_init |
200 | ; PPPOE_init |
||
1514 | hidnplayr | 201 | |
1529 | hidnplayr | 202 | IPv4_init |
203 | ICMP_init |
||
1514 | hidnplayr | 204 | |
1529 | hidnplayr | 205 | ARP_init |
206 | UDP_init |
||
207 | TCP_init |
||
1514 | hidnplayr | 208 | |
1529 | hidnplayr | 209 | SOCKET_init |
1514 | hidnplayr | 210 | |
211 | mov [net_tmr_count], 0 |
||
1159 | hidnplayr | 212 | |
213 | ret |
||
214 | |||
215 | |||
1257 | hidnplayr | 216 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 217 | ; |
218 | ; stack_handler |
||
219 | ; |
||
1514 | hidnplayr | 220 | ; This function is called in kernel loop |
1159 | hidnplayr | 221 | ; |
222 | ; IN: / |
||
223 | ; OUT: / |
||
224 | ; |
||
1257 | hidnplayr | 225 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 226 | align 4 |
227 | stack_handler: |
||
228 | |||
1514 | hidnplayr | 229 | cmp [NET_RUNNING], 0 |
1257 | hidnplayr | 230 | je .exit |
1159 | hidnplayr | 231 | |
1318 | hidnplayr | 232 | ; Test for 10ms tick |
1257 | hidnplayr | 233 | mov eax, [timer_ticks] |
1514 | hidnplayr | 234 | cmp eax, [net_10ms] |
1257 | hidnplayr | 235 | je .exit |
1514 | hidnplayr | 236 | mov [net_10ms], eax |
1159 | hidnplayr | 237 | |
1519 | hidnplayr | 238 | test [net_10ms], 0x0f ; 160ms |
239 | jnz .exit |
||
1249 | hidnplayr | 240 | |
1529 | hidnplayr | 241 | TCP_timer_160ms |
1159 | hidnplayr | 242 | |
1519 | hidnplayr | 243 | test [net_10ms], 0x3f ; 640ms |
244 | jnz .exit |
||
245 | |||
1529 | hidnplayr | 246 | TCP_timer_640ms |
247 | ARP_decrease_entry_ttls |
||
248 | IPv4_decrease_fragment_ttls |
||
1159 | hidnplayr | 249 | |
250 | .exit: |
||
1257 | hidnplayr | 251 | ret |
1159 | hidnplayr | 252 | |
253 | |||
1514 | hidnplayr | 254 | |
1249 | hidnplayr | 255 | ;----------------------------------------------------------------- |
256 | ; |
||
2220 | hidnplayr | 257 | ; NET_add_device: |
1514 | hidnplayr | 258 | ; |
259 | ; This function is called by the network drivers, |
||
260 | ; to register each running NIC to the kernel |
||
261 | ; |
||
262 | ; IN: Pointer to device structure in ebx |
||
263 | ; OUT: Device num in eax, -1 on error |
||
264 | ; |
||
265 | ;----------------------------------------------------------------- |
||
266 | align 4 |
||
267 | NET_add_device: |
||
268 | |||
2220 | hidnplayr | 269 | DEBUGF 1,"NET_Add_Device: %x\n", ebx ;;; TODO: use mutex to lock net device list |
1514 | hidnplayr | 270 | |
271 | mov eax, [NET_RUNNING] |
||
272 | cmp eax, MAX_NET_DEVICES |
||
2300 | hidnplayr | 273 | jae .error |
1514 | hidnplayr | 274 | |
275 | ;---------------------------------- |
||
276 | ; Check if device is already listed |
||
277 | mov eax, ebx |
||
2220 | hidnplayr | 278 | mov ecx, MAX_NET_DEVICES ; We need to check whole list because a device may be removed without re-organizing list |
279 | mov edi, NET_DRV_LIST+4 |
||
1514 | hidnplayr | 280 | |
2220 | hidnplayr | 281 | repne scasd ; See if device is already in the list |
1514 | hidnplayr | 282 | jz .error |
283 | |||
284 | ;---------------------------- |
||
285 | ; Find empty slot in the list |
||
286 | xor eax, eax |
||
287 | mov ecx, MAX_NET_DEVICES |
||
2220 | hidnplayr | 288 | mov edi, NET_DRV_LIST+4 |
1514 | hidnplayr | 289 | |
290 | repne scasd |
||
291 | jnz .error |
||
292 | |||
293 | sub edi, 4 |
||
294 | |||
2220 | hidnplayr | 295 | ;----------------------------- |
296 | ; Add device to the found slot |
||
297 | mov [edi], ebx ; add device to list |
||
1514 | hidnplayr | 298 | |
2220 | hidnplayr | 299 | mov eax, edi ; Calculate device number in eax |
300 | sub eax, NET_DRV_LIST |
||
301 | shr eax, 2 |
||
1514 | hidnplayr | 302 | |
2220 | hidnplayr | 303 | inc [NET_RUNNING] ; Indicate that one more network device is up and running |
1514 | hidnplayr | 304 | |
2220 | hidnplayr | 305 | cmp eax, 1 ; If it's the first network device, try to set it as default |
306 | jne @f |
||
307 | push eax |
||
308 | call NET_set_default |
||
309 | pop eax |
||
310 | @@: |
||
1514 | hidnplayr | 311 | |
2220 | hidnplayr | 312 | DEBUGF 1,"Device number: %u\n", eax |
313 | ret |
||
1514 | hidnplayr | 314 | |
2220 | hidnplayr | 315 | .error: |
316 | or eax, -1 |
||
317 | DEBUGF 2,"Adding network device failed\n" |
||
318 | ret |
||
1514 | hidnplayr | 319 | |
320 | |||
321 | |||
2220 | hidnplayr | 322 | ;----------------------------------------------------------------- |
323 | ; |
||
324 | ; NET_set_default |
||
325 | ; |
||
326 | ; API to set the default interface |
||
327 | ; |
||
328 | ; IN: Device num in eax |
||
329 | ; OUT: Device num in eax, -1 on error |
||
330 | ; |
||
331 | ;----------------------------------------------------------------- |
||
332 | align 4 |
||
333 | NET_set_default: |
||
1514 | hidnplayr | 334 | |
2220 | hidnplayr | 335 | DEBUGF 1,"NET_set_default %x\n", eax |
1514 | hidnplayr | 336 | |
2220 | hidnplayr | 337 | cmp eax, MAX_NET_DEVICES |
2300 | hidnplayr | 338 | jae .error |
1514 | hidnplayr | 339 | |
2220 | hidnplayr | 340 | cmp [NET_DRV_LIST+eax*4], 0 |
341 | je .error |
||
342 | |||
343 | push [NET_DRV_LIST+eax*4] |
||
344 | pop [NET_DRV_LIST] |
||
345 | |||
346 | DEBUGF 1,"Device number %u is now default!\n", eax |
||
1514 | hidnplayr | 347 | ret |
348 | |||
349 | .error: |
||
350 | or eax, -1 |
||
2220 | hidnplayr | 351 | DEBUGF 2,"Setting default network device failed\n" |
1514 | hidnplayr | 352 | ret |
353 | |||
354 | |||
355 | ;----------------------------------------------------------------- |
||
356 | ; |
||
357 | ; NET_Remove_Device: |
||
358 | ; |
||
359 | ; This function is called by etwork drivers, |
||
360 | ; to unregister network devices from the kernel |
||
2220 | hidnplayr | 361 | ; d |
1514 | hidnplayr | 362 | ; IN: Pointer to device structure in ebx |
363 | ; OUT: eax: -1 on error |
||
364 | ; |
||
365 | ;----------------------------------------------------------------- |
||
366 | align 4 |
||
367 | NET_remove_device: |
||
368 | |||
369 | cmp [NET_RUNNING], 0 |
||
370 | je .error |
||
371 | |||
2220 | hidnplayr | 372 | cmp [NET_DRV_LIST], ebx |
373 | jne @f |
||
374 | mov [NET_DRV_LIST], 0 |
||
375 | cmp [NET_RUNNING], 1 |
||
376 | je @f |
||
377 | ; there are still active devices, find one and make it default |
||
378 | xor eax, eax |
||
379 | mov ecx, MAX_NET_DEVICES |
||
380 | mov edi, NET_DRV_LIST+4 |
||
381 | repe scasd |
||
2302 | hidnplayr | 382 | je @f |
2220 | hidnplayr | 383 | push dword [edi-4] |
384 | pop [NET_DRV_LIST] |
||
385 | @@: |
||
386 | |||
1514 | hidnplayr | 387 | ;---------------------------- |
388 | ; Find the driver in the list |
||
389 | |||
390 | mov eax, ebx |
||
391 | mov ecx, MAX_NET_DEVICES |
||
2220 | hidnplayr | 392 | mov edi, NET_DRV_LIST+4 |
1514 | hidnplayr | 393 | |
394 | repne scasd |
||
395 | jnz .error |
||
396 | |||
397 | ;------------------------ |
||
398 | ; Remove it from the list |
||
399 | |||
400 | xor eax, eax |
||
401 | mov dword [edi-4], eax |
||
402 | |||
403 | dec [NET_RUNNING] |
||
404 | ret |
||
405 | |||
406 | .error: |
||
407 | or eax, -1 |
||
408 | ret |
||
409 | |||
410 | |||
411 | |||
412 | ;----------------------------------------------------------------- |
||
413 | ; |
||
414 | ; NET_ptr_to_num |
||
415 | ; |
||
416 | ; IN: ebx = ptr to device struct |
||
417 | ; OUT: edi = -1 on error, device number otherwise |
||
418 | ; |
||
419 | ;----------------------------------------------------------------- |
||
420 | align 4 |
||
421 | NET_ptr_to_num: |
||
422 | push ecx |
||
423 | |||
424 | mov ecx, MAX_NET_DEVICES |
||
2220 | hidnplayr | 425 | mov edi, NET_DRV_LIST+4 |
1514 | hidnplayr | 426 | |
427 | .loop: |
||
428 | cmp ebx, [edi] |
||
429 | jz .found |
||
430 | add edi, 4 |
||
431 | dec ecx |
||
432 | jnz .loop |
||
433 | |||
434 | ; repnz scasd could work too if eax is used instead of ebx! |
||
435 | |||
436 | or edi, -1 |
||
437 | |||
438 | pop ecx |
||
439 | ret |
||
440 | |||
441 | .found: |
||
442 | sub edi, NET_DRV_LIST |
||
443 | shr edi, 2 |
||
444 | |||
445 | pop ecx |
||
446 | ret |
||
447 | |||
448 | ;----------------------------------------------------------------- |
||
449 | ; |
||
1249 | hidnplayr | 450 | ; checksum_1 |
451 | ; |
||
1482 | hidnplayr | 452 | ; This is the first of two functions needed to calculate a checksum. |
1249 | hidnplayr | 453 | ; |
1473 | hidnplayr | 454 | ; IN: edx = start offset for semi-checksum |
1249 | hidnplayr | 455 | ; esi = pointer to data |
456 | ; ecx = data size |
||
457 | ; OUT: edx = semi-checksum |
||
458 | ; |
||
1473 | hidnplayr | 459 | ; |
460 | ; Code was optimized by diamond |
||
461 | ; |
||
1249 | hidnplayr | 462 | ;----------------------------------------------------------------- |
463 | align 4 |
||
464 | checksum_1: |
||
1159 | hidnplayr | 465 | |
1249 | hidnplayr | 466 | shr ecx, 1 |
467 | pushf |
||
1473 | hidnplayr | 468 | jz .no_2 |
1159 | hidnplayr | 469 | |
1473 | hidnplayr | 470 | shr ecx, 1 |
471 | pushf |
||
472 | jz .no_4 |
||
473 | |||
474 | shr ecx, 1 |
||
475 | pushf |
||
476 | jz .no_8 |
||
477 | |||
478 | .loop: |
||
479 | add dl, [esi+1] |
||
480 | adc dh, [esi+0] |
||
481 | |||
482 | adc dl, [esi+3] |
||
483 | adc dh, [esi+2] |
||
484 | |||
485 | adc dl, [esi+5] |
||
486 | adc dh, [esi+4] |
||
487 | |||
488 | adc dl, [esi+7] |
||
489 | adc dh, [esi+6] |
||
490 | |||
491 | adc edx, 0 |
||
492 | add esi, 8 |
||
493 | |||
494 | dec ecx |
||
495 | jnz .loop |
||
496 | |||
497 | adc edx, 0 |
||
498 | |||
499 | .no_8: |
||
1249 | hidnplayr | 500 | popf |
1473 | hidnplayr | 501 | jnc .no_4 |
502 | |||
503 | add dl, [esi+1] |
||
504 | adc dh, [esi+0] |
||
505 | |||
506 | adc dl, [esi+3] |
||
507 | adc dh, [esi+2] |
||
508 | |||
509 | adc edx, 0 |
||
510 | add esi, 4 |
||
511 | |||
512 | .no_4: |
||
513 | popf |
||
514 | jnc .no_2 |
||
515 | |||
516 | add dl, [esi+1] |
||
517 | adc dh, [esi+0] |
||
518 | |||
519 | adc edx, 0 |
||
1483 | hidnplayr | 520 | inc esi |
521 | inc esi |
||
1473 | hidnplayr | 522 | |
523 | .no_2: |
||
524 | popf |
||
1249 | hidnplayr | 525 | jnc .end |
1159 | hidnplayr | 526 | |
1473 | hidnplayr | 527 | add dh, [esi+0] |
1251 | clevermous | 528 | adc edx, 0 |
1473 | hidnplayr | 529 | .end: |
530 | ret |
||
1159 | hidnplayr | 531 | |
1249 | hidnplayr | 532 | ;----------------------------------------------------------------- |
533 | ; |
||
534 | ; checksum_2 |
||
535 | ; |
||
536 | ; This function calculates the final ip/tcp/udp checksum for you |
||
537 | ; |
||
538 | ; IN: edx = semi-checksum |
||
539 | ; OUT: dx = checksum (in INET byte order) |
||
540 | ; |
||
541 | ;----------------------------------------------------------------- |
||
542 | align 4 |
||
543 | checksum_2: |
||
544 | |||
545 | mov ecx, edx |
||
546 | shr ecx, 16 |
||
547 | and edx, 0xffff |
||
548 | add edx, ecx |
||
549 | |||
1482 | hidnplayr | 550 | mov ecx, edx |
551 | shr ecx, 16 |
||
1529 | hidnplayr | 552 | add dx, cx |
553 | test dx, dx ; it seems that ZF is not set when CF is set :( |
||
1249 | hidnplayr | 554 | not dx |
555 | jnz .not_zero |
||
556 | dec dx |
||
557 | .not_zero: |
||
558 | xchg dl, dh |
||
559 | |||
1529 | hidnplayr | 560 | DEBUGF 1,"Checksum: %x\n", dx |
1249 | hidnplayr | 561 | |
562 | ret |
||
563 | |||
564 | |||
565 | |||
1159 | hidnplayr | 566 | ;---------------------------------------------------------------- |
567 | ; |
||
1171 | hidnplayr | 568 | ; System function to work with network devices (73) |
1159 | hidnplayr | 569 | ; |
570 | ;---------------------------------------------------------------- |
||
571 | align 4 |
||
572 | sys_network: |
||
573 | |||
1196 | hidnplayr | 574 | cmp ebx, -1 |
575 | jne @f |
||
576 | |||
1514 | hidnplayr | 577 | mov eax, [NET_RUNNING] |
1196 | hidnplayr | 578 | jmp .return |
579 | |||
580 | @@: |
||
2220 | hidnplayr | 581 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
2300 | hidnplayr | 582 | jae .doesnt_exist |
1159 | hidnplayr | 583 | |
584 | mov esi, ebx |
||
585 | and esi, 0x0000ff00 |
||
586 | shr esi, 6 |
||
587 | |||
2220 | hidnplayr | 588 | cmp dword [esi + NET_DRV_LIST], 0 ; check if driver is running |
1159 | hidnplayr | 589 | je .doesnt_exist |
590 | |||
2220 | hidnplayr | 591 | |
592 | |||
593 | test bl, bl ; 0 = Get device type (ethernet/token ring/...) |
||
1159 | hidnplayr | 594 | jnz @f |
1514 | hidnplayr | 595 | |
1192 | hidnplayr | 596 | xor eax, eax |
597 | jmp .return |
||
1159 | hidnplayr | 598 | |
599 | |||
600 | @@: |
||
601 | dec bl ; 1 = Get device name |
||
602 | jnz @f |
||
603 | |||
1514 | hidnplayr | 604 | mov esi, [esi + NET_DRV_LIST] |
1519 | hidnplayr | 605 | mov esi, [esi + NET_DEVICE.name] |
1159 | hidnplayr | 606 | mov edi, ecx |
607 | |||
608 | mov ecx, 64 ; max length |
||
609 | repnz movsb |
||
610 | |||
1192 | hidnplayr | 611 | xor eax, eax |
612 | jmp .return |
||
1159 | hidnplayr | 613 | |
1192 | hidnplayr | 614 | @@: |
1159 | hidnplayr | 615 | |
1192 | hidnplayr | 616 | dec bl ; 2 = Reset the device |
617 | jnz @f |
||
618 | |||
1514 | hidnplayr | 619 | mov esi, [esi + NET_DRV_LIST] |
1519 | hidnplayr | 620 | call [esi + NET_DEVICE.reset] |
1192 | hidnplayr | 621 | jmp .return |
622 | |||
1159 | hidnplayr | 623 | @@: |
1192 | hidnplayr | 624 | |
625 | dec bl ; 3 = Stop driver for this device |
||
626 | jnz @f |
||
627 | |||
1514 | hidnplayr | 628 | mov esi, [esi + NET_DRV_LIST] |
1519 | hidnplayr | 629 | call [esi + NET_DEVICE.unload] |
1192 | hidnplayr | 630 | jmp .return |
631 | |||
632 | @@: |
||
1249 | hidnplayr | 633 | dec bl ; 4 = Get driver pointer |
634 | jnz @f |
||
1192 | hidnplayr | 635 | |
1249 | hidnplayr | 636 | ; ..; |
2220 | hidnplayr | 637 | xor eax, eax |
638 | jmp .return |
||
1249 | hidnplayr | 639 | |
640 | |||
641 | @@: |
||
2220 | hidnplayr | 642 | dec bl ; 5 = Get driver name |
643 | jnz @f |
||
1249 | hidnplayr | 644 | |
2220 | hidnplayr | 645 | ; ..; |
646 | xor eax, eax |
||
647 | jmp .return |
||
648 | |||
649 | |||
650 | @@: |
||
651 | dec bl ; 6 = Set default device |
||
652 | jnz @f |
||
653 | |||
654 | mov eax, esi |
||
655 | call NET_set_default |
||
656 | jmp .return |
||
657 | |||
658 | |||
659 | @@: |
||
660 | |||
1159 | hidnplayr | 661 | .doesnt_exist: |
662 | DEBUGF 1,"sys_network: invalid device/function specified!\n" |
||
663 | mov eax, -1 |
||
664 | |||
1192 | hidnplayr | 665 | .return: |
666 | mov [esp+28+4], eax |
||
1159 | hidnplayr | 667 | ret |
668 | |||
669 | |||
670 | ;---------------------------------------------------------------- |
||
671 | ; |
||
1514 | hidnplayr | 672 | ; System function to work with protocols (75) |
1159 | hidnplayr | 673 | ; |
674 | ;---------------------------------------------------------------- |
||
675 | align 4 |
||
676 | sys_protocols: |
||
677 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
||
2300 | hidnplayr | 678 | jae .doesnt_exist |
1159 | hidnplayr | 679 | |
680 | mov esi, ebx |
||
681 | and esi, 0x0000ff00 |
||
1514 | hidnplayr | 682 | shr esi, 6 ; now we have the device num * 4 in esi |
2305 | hidnplayr | 683 | cmp [esi + NET_DRV_LIST], 0 ; check if driver is running |
1159 | hidnplayr | 684 | je .doesnt_exist |
685 | |||
686 | push .return ; return address (we will be using jumps instead of calls) |
||
687 | |||
688 | mov eax, ebx ; set ax to protocol number |
||
689 | shr eax, 16 ; |
||
690 | |||
691 | cmp ax , IP_PROTO_IP |
||
692 | je IPv4_API |
||
693 | |||
694 | cmp ax , IP_PROTO_ICMP |
||
695 | je ICMP_API |
||
696 | |||
697 | cmp ax , IP_PROTO_UDP |
||
698 | je UDP_API |
||
699 | |||
1171 | hidnplayr | 700 | cmp ax , IP_PROTO_TCP |
1254 | hidnplayr | 701 | je TCP_API |
1159 | hidnplayr | 702 | |
1171 | hidnplayr | 703 | cmp ax , ETHER_ARP |
1159 | hidnplayr | 704 | je ARP_API |
705 | |||
1519 | hidnplayr | 706 | cmp ax , 1337 ;;;;; |
1159 | hidnplayr | 707 | je ETH_API |
708 | |||
1171 | hidnplayr | 709 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
1159 | hidnplayr | 710 | |
711 | .doesnt_exist: |
||
1519 | hidnplayr | 712 | DEBUGF 1,"sys_protocols: protocol %u doesnt exist on device %u!\n", ax, bh |
1159 | hidnplayr | 713 | mov eax, -1 |
714 | |||
715 | .return: |
||
1171 | hidnplayr | 716 | mov [esp+28+4], eax |
1257 | hidnplayr | 717 | ret |
1473 | hidnplayr | 718 | |
719 | |||
720 | __DEBUG_LEVEL__ equ __DEBUG_LEVEL_OLD__ |