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