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