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