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