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