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