Rev 3249 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1171 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3251 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2013. 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: 3251 $ |
1159 | hidnplayr | 25 | |
26 | uglobal |
||
2366 | hidnplayr | 27 | net_10ms dd ? |
28 | net_tmr_count dw ? |
||
1159 | hidnplayr | 29 | endg |
30 | |||
2731 | hidnplayr | 31 | MAX_NET_DEVICES = 16 |
1159 | hidnplayr | 32 | |
2731 | hidnplayr | 33 | MIN_EPHEMERAL_PORT = 49152 |
2995 | hidnplayr | 34 | MIN_EPHEMERAL_PORT_N = 0x00C0 ; same in Network byte order (FIXME) |
2731 | hidnplayr | 35 | MAX_EPHEMERAL_PORT = 61000 |
2995 | hidnplayr | 36 | MAX_EPHEMERAL_PORT_N = 0x48EE ; same in Network byte order (FIXME) |
1159 | hidnplayr | 37 | |
1514 | hidnplayr | 38 | ; Ethernet protocol numbers |
2731 | hidnplayr | 39 | ETHER_ARP = 0x0608 |
40 | ETHER_IPv4 = 0x0008 |
||
41 | ETHER_IPv6 = 0xDD86 |
||
42 | ETHER_PPP_DISCOVERY = 0x6388 |
||
43 | ETHER_PPP_SESSION = 0x6488 |
||
1185 | hidnplayr | 44 | |
2931 | hidnplayr | 45 | ; PPP protocol numbers |
46 | PPP_IPv4 = 0x2100 |
||
2964 | hidnplayr | 47 | PPP_IPV6 = 0x5780 |
2931 | hidnplayr | 48 | |
1514 | hidnplayr | 49 | ;Protocol family |
2731 | hidnplayr | 50 | AF_UNSPEC = 0 |
3251 | hidnplayr | 51 | AF_LOCAL = 1 |
2731 | hidnplayr | 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 | |
2994 | hidnplayr | 82 | SO_BLOCK = 1 shl 10 |
83 | |||
2877 | hidnplayr | 84 | ; Socket level |
85 | SOL_SOCKET = 0 |
||
1885 | hidnplayr | 86 | |
2877 | hidnplayr | 87 | |
1773 | hidnplayr | 88 | ; Socket States |
2731 | hidnplayr | 89 | SS_NOFDREF = 0x001 ; no file table ref any more |
90 | SS_ISCONNECTED = 0x002 ; socket connected to a peer |
||
91 | SS_ISCONNECTING = 0x004 ; in process of connecting to peer |
||
92 | SS_ISDISCONNECTING = 0x008 ; in process of disconnecting |
||
93 | SS_CANTSENDMORE = 0x010 ; can't send more data to peer |
||
94 | SS_CANTRCVMORE = 0x020 ; can't receive more data from peer |
||
95 | SS_RCVATMARK = 0x040 ; at mark on input |
||
96 | SS_ISABORTING = 0x080 ; aborting fd references - close() |
||
97 | SS_RESTARTSYS = 0x100 ; restart blocked system calls |
||
98 | SS_ISDISCONNECTED = 0x800 ; socket disconnected from peer |
||
1773 | hidnplayr | 99 | |
2731 | hidnplayr | 100 | SS_ASYNC = 0x100 ; async i/o notify |
101 | SS_ISCONFIRMING = 0x200 ; deciding to accept connection req |
||
102 | SS_MORETOCOME = 0x400 |
||
1773 | hidnplayr | 103 | |
104 | |||
2731 | hidnplayr | 105 | SOCKET_MAXDATA = 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8 |
1254 | hidnplayr | 106 | |
1514 | hidnplayr | 107 | ; Network driver types |
2914 | hidnplayr | 108 | NET_TYPE_LOOPBACK = 0 |
2731 | hidnplayr | 109 | NET_TYPE_ETH = 1 |
110 | NET_TYPE_SLIP = 2 |
||
1254 | hidnplayr | 111 | |
2731 | hidnplayr | 112 | MAX_backlog = 20 ; maximum backlog for stream sockets |
1254 | hidnplayr | 113 | |
1716 | hidnplayr | 114 | ; Error Codes |
2731 | hidnplayr | 115 | ENOBUFS = 55 |
116 | ECONNREFUSED = 61 |
||
117 | ECONNRESET = 52 |
||
118 | ETIMEDOUT = 60 |
||
119 | ECONNABORTED = 53 |
||
1529 | hidnplayr | 120 | |
2731 | hidnplayr | 121 | ; Api protocol numbers |
122 | API_ETH = 0 |
||
123 | API_IPv4 = 1 |
||
124 | API_ICMP = 2 |
||
125 | API_UDP = 3 |
||
126 | API_TCP = 4 |
||
127 | API_ARP = 5 |
||
128 | API_PPPOE = 6 |
||
3185 | hidnplayr | 129 | API_IPv6 = 7 |
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 | |
3249 | hidnplayr | 164 | ; Network to Hardware byte order (dword) |
1543 | hidnplayr | 165 | macro ntohd reg { |
1318 | hidnplayr | 166 | |
2366 | hidnplayr | 167 | rol word reg, 8 |
168 | rol dword reg, 16 |
||
169 | rol word reg , 8 |
||
1514 | hidnplayr | 170 | |
1318 | hidnplayr | 171 | } |
172 | |||
3249 | hidnplayr | 173 | ; Network to Hardware byte order (word) |
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" |
||
3185 | 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 | |
3251 | hidnplayr | 262 | call TCP_process_input ; de-queue TCP ragments and process them |
263 | |||
2366 | hidnplayr | 264 | ; Test for 10ms tick |
265 | mov eax, [timer_ticks] |
||
266 | cmp eax, [net_10ms] |
||
267 | je .exit |
||
268 | mov [net_10ms], eax |
||
1159 | hidnplayr | 269 | |
2366 | hidnplayr | 270 | test [net_10ms], 0x0f ; 160ms |
271 | jnz .exit |
||
1249 | hidnplayr | 272 | |
2366 | hidnplayr | 273 | TCP_timer_160ms |
1159 | hidnplayr | 274 | |
2366 | hidnplayr | 275 | test [net_10ms], 0x3f ; 640ms |
276 | jnz .exit |
||
1519 | hidnplayr | 277 | |
2366 | hidnplayr | 278 | TCP_timer_640ms |
279 | ARP_decrease_entry_ttls |
||
280 | IPv4_decrease_fragment_ttls |
||
1159 | hidnplayr | 281 | |
282 | .exit: |
||
2366 | hidnplayr | 283 | ret |
1159 | hidnplayr | 284 | |
285 | |||
1514 | hidnplayr | 286 | |
1249 | hidnplayr | 287 | ;----------------------------------------------------------------- |
288 | ; |
||
2220 | hidnplayr | 289 | ; NET_add_device: |
1514 | hidnplayr | 290 | ; |
291 | ; This function is called by the network drivers, |
||
292 | ; to register each running NIC to the kernel |
||
293 | ; |
||
294 | ; IN: Pointer to device structure in ebx |
||
295 | ; OUT: Device num in eax, -1 on error |
||
296 | ; |
||
297 | ;----------------------------------------------------------------- |
||
298 | align 4 |
||
299 | NET_add_device: |
||
300 | |||
2366 | hidnplayr | 301 | DEBUGF 1,"NET_Add_Device: %x\n", ebx ;;; TODO: use mutex to lock net device list |
1514 | hidnplayr | 302 | |
2366 | hidnplayr | 303 | mov eax, [NET_RUNNING] |
304 | cmp eax, MAX_NET_DEVICES |
||
305 | jae .error |
||
1514 | hidnplayr | 306 | |
307 | ;---------------------------------- |
||
308 | ; Check if device is already listed |
||
2366 | hidnplayr | 309 | mov eax, ebx |
310 | mov ecx, MAX_NET_DEVICES ; We need to check whole list because a device may be removed without re-organizing list |
||
311 | mov edi, NET_DRV_LIST |
||
1514 | hidnplayr | 312 | |
2366 | hidnplayr | 313 | repne scasd ; See if device is already in the list |
314 | jz .error |
||
1514 | hidnplayr | 315 | |
316 | ;---------------------------- |
||
317 | ; Find empty slot in the list |
||
2366 | hidnplayr | 318 | xor eax, eax |
319 | mov ecx, MAX_NET_DEVICES |
||
320 | mov edi, NET_DRV_LIST |
||
1514 | hidnplayr | 321 | |
2366 | hidnplayr | 322 | repne scasd |
323 | jnz .error |
||
1514 | hidnplayr | 324 | |
2366 | hidnplayr | 325 | sub edi, 4 |
1514 | hidnplayr | 326 | |
2220 | hidnplayr | 327 | ;----------------------------- |
328 | ; Add device to the found slot |
||
2366 | hidnplayr | 329 | mov [edi], ebx ; add device to list |
1514 | hidnplayr | 330 | |
2366 | hidnplayr | 331 | mov eax, edi ; Calculate device number in eax |
332 | sub eax, NET_DRV_LIST |
||
333 | shr eax, 2 |
||
1514 | hidnplayr | 334 | |
2366 | hidnplayr | 335 | inc [NET_RUNNING] ; Indicate that one more network device is up and running |
1514 | hidnplayr | 336 | |
2366 | hidnplayr | 337 | cmp eax, 1 ; If it's the first network device, try to set it as default |
338 | jne @f |
||
339 | push eax |
||
340 | call NET_set_default |
||
341 | pop eax |
||
2220 | hidnplayr | 342 | @@: |
1514 | hidnplayr | 343 | |
2366 | hidnplayr | 344 | DEBUGF 1,"Device number: %u\n", eax |
345 | ret |
||
1514 | hidnplayr | 346 | |
2220 | hidnplayr | 347 | .error: |
2366 | hidnplayr | 348 | or eax, -1 |
349 | DEBUGF 2,"Adding network device failed\n" |
||
350 | ret |
||
1514 | hidnplayr | 351 | |
352 | |||
353 | |||
2220 | hidnplayr | 354 | ;----------------------------------------------------------------- |
355 | ; |
||
356 | ; NET_set_default |
||
357 | ; |
||
358 | ; API to set the default interface |
||
359 | ; |
||
360 | ; IN: Device num in eax |
||
361 | ; OUT: Device num in eax, -1 on error |
||
362 | ; |
||
363 | ;----------------------------------------------------------------- |
||
364 | align 4 |
||
365 | NET_set_default: |
||
1514 | hidnplayr | 366 | |
2891 | hidnplayr | 367 | DEBUGF 1,"NET_set_default: device=%x\n", eax |
1514 | hidnplayr | 368 | |
2366 | hidnplayr | 369 | cmp eax, MAX_NET_DEVICES |
370 | jae .error |
||
1514 | hidnplayr | 371 | |
2366 | hidnplayr | 372 | cmp [NET_DRV_LIST+eax*4], 0 |
373 | je .error |
||
2220 | hidnplayr | 374 | |
2366 | hidnplayr | 375 | mov [NET_DEFAULT], eax |
2220 | hidnplayr | 376 | |
2891 | hidnplayr | 377 | DEBUGF 1,"NET_set_default: succes\n" |
2366 | hidnplayr | 378 | ret |
1514 | hidnplayr | 379 | |
380 | .error: |
||
2366 | hidnplayr | 381 | or eax, -1 |
2891 | hidnplayr | 382 | DEBUGF 1,"NET_set_default: failed\n" |
2366 | hidnplayr | 383 | ret |
1514 | hidnplayr | 384 | |
385 | |||
386 | ;----------------------------------------------------------------- |
||
387 | ; |
||
388 | ; NET_Remove_Device: |
||
389 | ; |
||
390 | ; This function is called by etwork drivers, |
||
391 | ; to unregister network devices from the kernel |
||
2891 | hidnplayr | 392 | ; |
1514 | hidnplayr | 393 | ; IN: Pointer to device structure in ebx |
394 | ; OUT: eax: -1 on error |
||
395 | ; |
||
396 | ;----------------------------------------------------------------- |
||
397 | align 4 |
||
398 | NET_remove_device: |
||
399 | |||
2366 | hidnplayr | 400 | cmp [NET_RUNNING], 0 |
401 | je .error |
||
1514 | hidnplayr | 402 | |
2366 | hidnplayr | 403 | cmp [NET_DRV_LIST], ebx |
404 | jne @f |
||
405 | mov [NET_DRV_LIST], 0 |
||
406 | cmp [NET_RUNNING], 1 |
||
407 | je @f |
||
408 | ; there are still active devices, find one and make it default |
||
409 | xor eax, eax |
||
410 | mov ecx, MAX_NET_DEVICES |
||
411 | mov edi, NET_DRV_LIST |
||
412 | repe scasd |
||
413 | je @f |
||
414 | shr edi, 2 |
||
415 | dec edi |
||
416 | mov [NET_DEFAULT], edi |
||
2220 | hidnplayr | 417 | @@: |
418 | |||
1514 | hidnplayr | 419 | ;---------------------------- |
420 | ; Find the driver in the list |
||
421 | |||
2366 | hidnplayr | 422 | mov eax, ebx |
423 | mov ecx, MAX_NET_DEVICES |
||
424 | mov edi, NET_DRV_LIST+4 |
||
1514 | hidnplayr | 425 | |
2366 | hidnplayr | 426 | repne scasd |
427 | jnz .error |
||
1514 | hidnplayr | 428 | |
429 | ;------------------------ |
||
430 | ; Remove it from the list |
||
431 | |||
2366 | hidnplayr | 432 | xor eax, eax |
433 | mov dword [edi-4], eax |
||
1514 | hidnplayr | 434 | |
2366 | hidnplayr | 435 | dec [NET_RUNNING] |
436 | ret |
||
1514 | hidnplayr | 437 | |
438 | .error: |
||
2366 | hidnplayr | 439 | or eax, -1 |
440 | ret |
||
1514 | hidnplayr | 441 | |
442 | |||
443 | |||
444 | ;----------------------------------------------------------------- |
||
445 | ; |
||
446 | ; NET_ptr_to_num |
||
447 | ; |
||
448 | ; IN: ebx = ptr to device struct |
||
449 | ; OUT: edi = -1 on error, device number otherwise |
||
450 | ; |
||
451 | ;----------------------------------------------------------------- |
||
452 | align 4 |
||
453 | NET_ptr_to_num: |
||
2366 | hidnplayr | 454 | push ecx |
1514 | hidnplayr | 455 | |
2366 | hidnplayr | 456 | mov ecx, MAX_NET_DEVICES |
457 | mov edi, NET_DRV_LIST |
||
1514 | hidnplayr | 458 | |
459 | .loop: |
||
2366 | hidnplayr | 460 | cmp ebx, [edi] |
461 | jz .found |
||
462 | add edi, 4 |
||
463 | dec ecx |
||
464 | jnz .loop |
||
1514 | hidnplayr | 465 | |
2366 | hidnplayr | 466 | ; repnz scasd could work too if eax is used instead of ebx! |
1514 | hidnplayr | 467 | |
2366 | hidnplayr | 468 | or edi, -1 |
1514 | hidnplayr | 469 | |
2366 | hidnplayr | 470 | pop ecx |
471 | ret |
||
1514 | hidnplayr | 472 | |
473 | .found: |
||
2366 | hidnplayr | 474 | sub edi, NET_DRV_LIST |
475 | shr edi, 2 |
||
1514 | hidnplayr | 476 | |
2366 | hidnplayr | 477 | pop ecx |
478 | ret |
||
1514 | hidnplayr | 479 | |
480 | ;----------------------------------------------------------------- |
||
481 | ; |
||
1249 | hidnplayr | 482 | ; checksum_1 |
483 | ; |
||
1482 | hidnplayr | 484 | ; This is the first of two functions needed to calculate a checksum. |
1249 | hidnplayr | 485 | ; |
1473 | hidnplayr | 486 | ; IN: edx = start offset for semi-checksum |
1249 | hidnplayr | 487 | ; esi = pointer to data |
488 | ; ecx = data size |
||
489 | ; OUT: edx = semi-checksum |
||
490 | ; |
||
1473 | hidnplayr | 491 | ; |
492 | ; Code was optimized by diamond |
||
493 | ; |
||
1249 | hidnplayr | 494 | ;----------------------------------------------------------------- |
495 | align 4 |
||
496 | checksum_1: |
||
1159 | hidnplayr | 497 | |
2366 | hidnplayr | 498 | shr ecx, 1 |
499 | pushf |
||
500 | jz .no_2 |
||
1159 | hidnplayr | 501 | |
2366 | hidnplayr | 502 | shr ecx, 1 |
503 | pushf |
||
504 | jz .no_4 |
||
1473 | hidnplayr | 505 | |
2366 | hidnplayr | 506 | shr ecx, 1 |
507 | pushf |
||
508 | jz .no_8 |
||
1473 | hidnplayr | 509 | |
510 | .loop: |
||
2366 | hidnplayr | 511 | add dl, [esi+1] |
512 | adc dh, [esi+0] |
||
1473 | hidnplayr | 513 | |
2366 | hidnplayr | 514 | adc dl, [esi+3] |
515 | adc dh, [esi+2] |
||
1473 | hidnplayr | 516 | |
2366 | hidnplayr | 517 | adc dl, [esi+5] |
518 | adc dh, [esi+4] |
||
1473 | hidnplayr | 519 | |
2366 | hidnplayr | 520 | adc dl, [esi+7] |
521 | adc dh, [esi+6] |
||
1473 | hidnplayr | 522 | |
2366 | hidnplayr | 523 | adc edx, 0 |
524 | add esi, 8 |
||
1473 | hidnplayr | 525 | |
2366 | hidnplayr | 526 | dec ecx |
527 | jnz .loop |
||
1473 | hidnplayr | 528 | |
2366 | hidnplayr | 529 | adc edx, 0 |
1473 | hidnplayr | 530 | |
531 | .no_8: |
||
2366 | hidnplayr | 532 | popf |
533 | jnc .no_4 |
||
1473 | hidnplayr | 534 | |
2366 | hidnplayr | 535 | add dl, [esi+1] |
536 | adc dh, [esi+0] |
||
1473 | hidnplayr | 537 | |
2366 | hidnplayr | 538 | adc dl, [esi+3] |
539 | adc dh, [esi+2] |
||
1473 | hidnplayr | 540 | |
2366 | hidnplayr | 541 | adc edx, 0 |
542 | add esi, 4 |
||
1473 | hidnplayr | 543 | |
544 | .no_4: |
||
2366 | hidnplayr | 545 | popf |
546 | jnc .no_2 |
||
1473 | hidnplayr | 547 | |
2366 | hidnplayr | 548 | add dl, [esi+1] |
549 | adc dh, [esi+0] |
||
1473 | hidnplayr | 550 | |
2366 | hidnplayr | 551 | adc edx, 0 |
552 | inc esi |
||
553 | inc esi |
||
1473 | hidnplayr | 554 | |
555 | .no_2: |
||
2366 | hidnplayr | 556 | popf |
557 | jnc .end |
||
1159 | hidnplayr | 558 | |
2366 | hidnplayr | 559 | add dh, [esi+0] |
560 | adc edx, 0 |
||
1473 | hidnplayr | 561 | .end: |
2366 | hidnplayr | 562 | ret |
1159 | hidnplayr | 563 | |
1249 | hidnplayr | 564 | ;----------------------------------------------------------------- |
565 | ; |
||
566 | ; checksum_2 |
||
567 | ; |
||
568 | ; This function calculates the final ip/tcp/udp checksum for you |
||
569 | ; |
||
570 | ; IN: edx = semi-checksum |
||
571 | ; OUT: dx = checksum (in INET byte order) |
||
572 | ; |
||
573 | ;----------------------------------------------------------------- |
||
574 | align 4 |
||
575 | checksum_2: |
||
576 | |||
2366 | hidnplayr | 577 | mov ecx, edx |
578 | shr ecx, 16 |
||
579 | and edx, 0xffff |
||
580 | add edx, ecx |
||
1249 | hidnplayr | 581 | |
2366 | hidnplayr | 582 | mov ecx, edx |
583 | shr ecx, 16 |
||
584 | add dx, cx |
||
585 | test dx, dx ; it seems that ZF is not set when CF is set :( |
||
586 | not dx |
||
587 | jnz .not_zero |
||
588 | dec dx |
||
1249 | hidnplayr | 589 | .not_zero: |
2366 | hidnplayr | 590 | xchg dl, dh |
1249 | hidnplayr | 591 | |
3251 | hidnplayr | 592 | DEBUGF 1,"Checksum: %x\n", dx |
1249 | hidnplayr | 593 | |
2366 | hidnplayr | 594 | ret |
1249 | hidnplayr | 595 | |
596 | |||
597 | |||
1159 | hidnplayr | 598 | ;---------------------------------------------------------------- |
599 | ; |
||
3146 | hidnplayr | 600 | ; System function to work with network devices (75) |
1159 | hidnplayr | 601 | ; |
602 | ;---------------------------------------------------------------- |
||
603 | align 4 |
||
2366 | hidnplayr | 604 | sys_network: ; FIXME: make default device easily accessible |
1159 | hidnplayr | 605 | |
2366 | hidnplayr | 606 | cmp ebx, -1 |
607 | jne @f |
||
1196 | hidnplayr | 608 | |
2366 | hidnplayr | 609 | mov eax, [NET_RUNNING] |
610 | jmp .return |
||
1196 | hidnplayr | 611 | |
612 | @@: |
||
2366 | hidnplayr | 613 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
614 | jae .doesnt_exist |
||
1159 | hidnplayr | 615 | |
2366 | hidnplayr | 616 | mov esi, ebx |
617 | and esi, 0x0000ff00 |
||
618 | shr esi, 6 |
||
1159 | hidnplayr | 619 | |
2366 | hidnplayr | 620 | cmp dword [esi + NET_DRV_LIST], 0 ; check if driver is running |
621 | je .doesnt_exist |
||
1159 | hidnplayr | 622 | |
2621 | hidnplayr | 623 | mov eax, [esi + NET_DRV_LIST] |
2220 | hidnplayr | 624 | |
2621 | hidnplayr | 625 | and ebx, 0x000000ff |
626 | cmp ebx, .number |
||
627 | ja .doesnt_exist |
||
628 | jmp dword [.table + 4*ebx] |
||
2220 | hidnplayr | 629 | |
2621 | hidnplayr | 630 | .table: |
631 | dd .get_type ; 0 |
||
632 | dd .get_dev_name ; 1 |
||
633 | dd .reset ; 2 |
||
634 | dd .stop ; 3 |
||
635 | dd .get_ptr ; 4 |
||
636 | dd .get_drv_name ; 5 |
||
637 | dd .set_default ; 6 |
||
638 | .number = ($ - .table) / 4 - 1 |
||
1514 | hidnplayr | 639 | |
2621 | hidnplayr | 640 | .get_type: ; 0 = Get device type (ethernet/token ring/...) |
641 | |||
642 | mov eax, [eax + NET_DEVICE.type] |
||
2366 | hidnplayr | 643 | jmp .return |
1159 | hidnplayr | 644 | |
645 | |||
2621 | hidnplayr | 646 | .get_dev_name: ; 1 = Get device name |
1159 | hidnplayr | 647 | |
2621 | hidnplayr | 648 | mov esi, [eax + NET_DEVICE.name] |
2366 | hidnplayr | 649 | mov edi, ecx |
1159 | hidnplayr | 650 | |
2621 | hidnplayr | 651 | mov ecx, 64/4 ; max length |
652 | rep movsd |
||
1159 | hidnplayr | 653 | |
2366 | hidnplayr | 654 | xor eax, eax |
655 | jmp .return |
||
1159 | hidnplayr | 656 | |
2621 | hidnplayr | 657 | .reset: ; 2 = Reset the device |
1159 | hidnplayr | 658 | |
2621 | hidnplayr | 659 | call [eax + NET_DEVICE.reset] |
2366 | hidnplayr | 660 | jmp .return |
1192 | hidnplayr | 661 | |
2621 | hidnplayr | 662 | .stop: ; 3 = Stop driver for this device |
1192 | hidnplayr | 663 | |
2621 | hidnplayr | 664 | call [eax + NET_DEVICE.unload] |
2366 | hidnplayr | 665 | jmp .return |
1192 | hidnplayr | 666 | |
667 | |||
2621 | hidnplayr | 668 | .get_ptr: ; 4 = Get driver pointer |
669 | |||
2366 | hidnplayr | 670 | jmp .return |
1249 | hidnplayr | 671 | |
672 | |||
2621 | hidnplayr | 673 | .get_drv_name: ; 5 = Get driver name |
1249 | hidnplayr | 674 | |
2366 | hidnplayr | 675 | xor eax, eax |
676 | jmp .return |
||
2220 | hidnplayr | 677 | |
678 | |||
2621 | hidnplayr | 679 | .set_default: ; 6 = Set default device |
2220 | hidnplayr | 680 | |
2366 | hidnplayr | 681 | call NET_set_default |
682 | jmp .return |
||
2220 | hidnplayr | 683 | |
1159 | hidnplayr | 684 | .doesnt_exist: |
2366 | hidnplayr | 685 | mov eax, -1 |
1159 | hidnplayr | 686 | |
1192 | hidnplayr | 687 | .return: |
2621 | hidnplayr | 688 | mov [esp+32], eax |
2366 | hidnplayr | 689 | ret |
1159 | hidnplayr | 690 | |
691 | |||
692 | ;---------------------------------------------------------------- |
||
693 | ; |
||
2731 | hidnplayr | 694 | ; System function to work with protocols (76) |
1159 | hidnplayr | 695 | ; |
696 | ;---------------------------------------------------------------- |
||
697 | align 4 |
||
698 | sys_protocols: |
||
2366 | hidnplayr | 699 | cmp bh, MAX_NET_DEVICES ; Check if device number exists |
700 | jae .doesnt_exist |
||
1159 | hidnplayr | 701 | |
2366 | hidnplayr | 702 | mov esi, ebx |
703 | and esi, 0x0000ff00 |
||
704 | shr esi, 6 ; now we have the device num * 4 in esi |
||
705 | cmp [esi + NET_DRV_LIST], 0 ; check if driver is running |
||
706 | je .doesnt_exist |
||
1159 | hidnplayr | 707 | |
2366 | hidnplayr | 708 | push .return ; return address (we will be using jumps instead of calls) |
1159 | hidnplayr | 709 | |
2366 | hidnplayr | 710 | mov eax, ebx ; set ax to protocol number |
711 | shr eax, 16 ; |
||
1159 | hidnplayr | 712 | |
2731 | hidnplayr | 713 | cmp ax, API_ETH |
714 | je ETH_api |
||
715 | |||
716 | cmp ax, API_IPv4 |
||
2614 | hidnplayr | 717 | je IPv4_api |
1159 | hidnplayr | 718 | |
2731 | hidnplayr | 719 | cmp ax, API_ICMP |
2614 | hidnplayr | 720 | je ICMP_api |
1159 | hidnplayr | 721 | |
2731 | hidnplayr | 722 | cmp ax, API_UDP |
2614 | hidnplayr | 723 | je UDP_api |
1159 | hidnplayr | 724 | |
2731 | hidnplayr | 725 | cmp ax, API_TCP |
2614 | hidnplayr | 726 | je TCP_api |
1159 | hidnplayr | 727 | |
2731 | hidnplayr | 728 | cmp ax, API_ARP |
2614 | hidnplayr | 729 | je ARP_api |
1159 | hidnplayr | 730 | |
2931 | hidnplayr | 731 | cmp ax, API_PPPOE |
732 | je PPPoE_api |
||
2614 | hidnplayr | 733 | |
3185 | hidnplayr | 734 | cmp ax, API_IPv6 |
735 | je IPv6_api |
||
736 | |||
2366 | hidnplayr | 737 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
1159 | hidnplayr | 738 | |
739 | .doesnt_exist: |
||
2366 | hidnplayr | 740 | mov eax, -1 |
1159 | hidnplayr | 741 | |
742 | .return: |
||
2614 | hidnplayr | 743 | mov [esp+28+4], eax ; return eax value to the program |
2366 | hidnplayr | 744 | ret |