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