Rev 3626 | Rev 3908 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3555 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; STACK.INC ;; |
||
7 | ;; ;; |
||
8 | ;; TCP/IP stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
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) ;; |
||
16 | ;; ;; |
||
17 | ;; TCP part is based on 4.4BSD ;; |
||
18 | ;; ;; |
||
19 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
20 | ;; Version 2, June 1991 ;; |
||
21 | ;; ;; |
||
22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
1 | ha | 23 | |
593 | mikedld | 24 | $Revision: 3725 $ |
1 | ha | 25 | |
261 | hidnplayr | 26 | uglobal |
3555 | Serge | 27 | net_10ms dd ? |
28 | net_tmr_count dw ? |
||
261 | hidnplayr | 29 | endg |
1 | ha | 30 | |
3589 | Serge | 31 | DEBUG_NETWORK_ERROR = 1 |
32 | DEBUG_NETWORK_VERBOSE = 0 |
||
33 | |||
3626 | Serge | 34 | NET_DEVICES_MAX = 16 |
3555 | Serge | 35 | ARP_BLOCK = 1 ; true or false |
1 | ha | 36 | |
3626 | Serge | 37 | EPHEMERAL_PORT_MIN = 49152 |
38 | EPHEMERAL_PORT_MAX = 61000 |
||
3555 | Serge | 39 | MIN_EPHEMERAL_PORT_N = 0x00C0 ; same in Network byte order (FIXME) |
40 | MAX_EPHEMERAL_PORT_N = 0x48EE ; same in Network byte order (FIXME) |
||
1 | ha | 41 | |
3555 | Serge | 42 | ; Ethernet protocol numbers |
3626 | Serge | 43 | ETHER_PROTO_ARP = 0x0608 |
44 | ETHER_PROTO_IPv4 = 0x0008 |
||
45 | ETHER_PROTO_IPv6 = 0xDD86 |
||
46 | ETHER_PROTO_PPP_DISCOVERY = 0x6388 |
||
47 | ETHER_PROTO_PPP_SESSION = 0x6488 |
||
1 | ha | 48 | |
3626 | Serge | 49 | ; Internet protocol numbers |
50 | IP_PROTO_IP = 0 |
||
51 | IP_PROTO_ICMP = 1 |
||
52 | IP_PROTO_TCP = 6 |
||
53 | IP_PROTO_UDP = 17 |
||
54 | |||
3555 | Serge | 55 | ; PPP protocol numbers |
3626 | Serge | 56 | PPP_PROTO_IPv4 = 0x2100 |
57 | PPP_PROTO_IPV6 = 0x5780 |
||
58 | PPP_PROTO_ETHERNET = 666 ; FIXME |
||
1 | ha | 59 | |
3555 | Serge | 60 | ;Protocol family |
61 | AF_UNSPEC = 0 |
||
62 | AF_LOCAL = 1 |
||
63 | AF_INET4 = 2 |
||
64 | AF_INET6 = 10 |
||
3626 | Serge | 65 | AF_PPP = 777 ; FIXME |
907 | mikedld | 66 | |
3555 | Serge | 67 | ; Socket types |
68 | SOCK_STREAM = 1 |
||
69 | SOCK_DGRAM = 2 |
||
70 | SOCK_RAW = 3 |
||
1 | ha | 71 | |
3555 | Serge | 72 | ; Socket options |
73 | SO_ACCEPTCON = 1 shl 0 |
||
74 | SO_BROADCAST = 1 shl 1 |
||
75 | SO_DEBUG = 1 shl 2 |
||
76 | SO_DONTROUTE = 1 shl 3 |
||
77 | SO_KEEPALIVE = 1 shl 4 |
||
78 | SO_OOBINLINE = 1 shl 5 |
||
79 | SO_REUSEADDR = 1 shl 6 |
||
80 | SO_REUSEPORT = 1 shl 7 |
||
81 | SO_USELOOPBACK = 1 shl 8 |
||
82 | SO_BINDTODEVICE = 1 shl 9 |
||
1 | ha | 83 | |
3555 | Serge | 84 | SO_NONBLOCK = 1 shl 31 |
1 | ha | 85 | |
3555 | Serge | 86 | ; Socket flags for user calls |
87 | MSG_PEEK = 0x02 |
||
88 | MSG_DONTWAIT = 0x40 |
||
261 | hidnplayr | 89 | |
3555 | Serge | 90 | ; Socket level |
91 | SOL_SOCKET = 0 |
||
261 | hidnplayr | 92 | |
93 | |||
3555 | Serge | 94 | ; Socket States |
3626 | Serge | 95 | SS_NOFDREF = 0x0001 ; no file table ref any more |
96 | SS_ISCONNECTED = 0x0002 ; socket connected to a peer |
||
97 | SS_ISCONNECTING = 0x0004 ; in process of connecting to peer |
||
98 | SS_ISDISCONNECTING = 0x0008 ; in process of disconnecting |
||
99 | SS_CANTSENDMORE = 0x0010 ; can't send more data to peer |
||
100 | SS_CANTRCVMORE = 0x0020 ; can't receive more data from peer |
||
101 | SS_RCVATMARK = 0x0040 ; at mark on input |
||
102 | SS_ISABORTING = 0x0080 ; aborting fd references - close() |
||
103 | SS_RESTARTSYS = 0x0100 ; restart blocked system calls |
||
104 | SS_ISDISCONNECTED = 0x0800 ; socket disconnected from peer |
||
261 | hidnplayr | 105 | |
3725 | Serge | 106 | SS_ASYNC = 0x1000 ; async i/o notify |
107 | SS_ISCONFIRMING = 0x2000 ; deciding to accept connection req |
||
108 | SS_MORETOCOME = 0x4000 |
||
261 | hidnplayr | 109 | |
3555 | Serge | 110 | SS_BLOCKED = 0x8000 |
1 | ha | 111 | |
112 | |||
3626 | Serge | 113 | SOCKET_MAXDATA = 4096*32 ; must be 4096*(power of 2) where 'power of 2' is at least 8 |
114 | MAX_backlog = 20 ; maximum backlog for stream sockets |
||
1 | ha | 115 | |
3555 | Serge | 116 | ; Error Codes |
3725 | Serge | 117 | ENOBUFS = 1 |
118 | EOPNOTSUPP = 4 |
||
119 | EWOULDBLOCK = 6 |
||
120 | ENOTCONN = 9 |
||
121 | EALREADY = 10 |
||
122 | EINVAL = 11 |
||
123 | EMSGSIZE = 12 |
||
124 | ENOMEM = 18 |
||
125 | EADDRINUSE = 20 |
||
3555 | Serge | 126 | ECONNREFUSED = 61 |
127 | ECONNRESET = 52 |
||
128 | ETIMEDOUT = 60 |
||
129 | ECONNABORTED = 53 |
||
1 | ha | 130 | |
3555 | Serge | 131 | ; Api protocol numbers |
132 | API_ETH = 0 |
||
133 | API_IPv4 = 1 |
||
134 | API_ICMP = 2 |
||
135 | API_UDP = 3 |
||
136 | API_TCP = 4 |
||
137 | API_ARP = 5 |
||
138 | API_PPPOE = 6 |
||
139 | API_IPv6 = 7 |
||
1 | ha | 140 | |
3626 | Serge | 141 | ; Network device types |
142 | NET_DEVICE_LOOPBACK = 0 |
||
143 | NET_DEVICE_ETH = 1 |
||
144 | NET_DEVICE_SLIP = 2 |
||
145 | |||
146 | ; Network link types (link protocols) |
||
147 | NET_LINK_LOOPBACK = 0 ;;; Really a link type? |
||
148 | NET_LINK_MAC = 1 ; Media access control (ethernet, isdn, ...) |
||
149 | NET_LINK_PPP = 2 ; Point to Point Protocol (PPPoE, ...) |
||
150 | NET_LINK_IEEE802.11 = 3 ; IEEE 802.11 (WiFi) |
||
151 | |||
152 | ; Hardware acceleration bits |
||
3555 | Serge | 153 | HWACC_TCP_IPv4 = 1 shl 0 |
1 | ha | 154 | |
3555 | Serge | 155 | struct NET_DEVICE |
907 | mikedld | 156 | |
3626 | Serge | 157 | device_type dd ? ; Type field |
3555 | Serge | 158 | mtu dd ? ; Maximal Transmission Unit |
159 | name dd ? ; Ptr to 0 terminated string |
||
160 | |||
161 | unload dd ? ; Ptrs to driver functions |
||
162 | reset dd ? ; |
||
163 | transmit dd ? ; |
||
164 | |||
165 | bytes_tx dq ? ; Statistics, updated by the driver |
||
166 | bytes_rx dq ? ; |
||
167 | packets_tx dd ? ; |
||
168 | packets_rx dd ? ; |
||
169 | |||
3626 | Serge | 170 | link_state dd ? ; link state (0 = no link) |
3555 | Serge | 171 | hwacc dd ? ; bitmask stating enabled HW accelerations (offload engines) |
172 | |||
173 | ends |
||
174 | |||
175 | |||
176 | ; Exactly as it says.. |
||
177 | macro pseudo_random reg { |
||
178 | add reg, [esp] |
||
179 | rol reg, 5 |
||
180 | xor reg, [timer_ticks] |
||
181 | ; add reg, [CPU_FREQ] |
||
182 | imul reg, 214013 |
||
183 | xor reg, 0xdeadbeef |
||
184 | rol reg, 9 |
||
261 | hidnplayr | 185 | } |
186 | |||
3555 | Serge | 187 | ; Network to Hardware byte order (dword) |
188 | macro ntohd reg { |
||
261 | hidnplayr | 189 | |
3555 | Serge | 190 | rol word reg, 8 |
191 | rol dword reg, 16 |
||
192 | rol word reg , 8 |
||
193 | |||
194 | } |
||
195 | |||
196 | ; Network to Hardware byte order (word) |
||
197 | macro ntohw reg { |
||
198 | |||
199 | rol word reg, 8 |
||
200 | |||
201 | } |
||
202 | |||
203 | |||
261 | hidnplayr | 204 | include "queue.inc" |
3555 | Serge | 205 | |
206 | include "loopback.inc" |
||
207 | include "ethernet.inc" |
||
208 | |||
209 | include "PPPoE.inc" |
||
210 | |||
211 | include "ARP.inc" |
||
212 | include "IPv4.inc" |
||
213 | include "IPv6.inc" |
||
214 | |||
215 | include "icmp.inc" |
||
216 | include "udp.inc" |
||
217 | include "tcp.inc" |
||
218 | |||
261 | hidnplayr | 219 | include "socket.inc" |
220 | |||
221 | |||
1 | ha | 222 | |
3725 | Serge | 223 | uglobal |
3555 | Serge | 224 | align 4 |
909 | mikedld | 225 | |
3555 | Serge | 226 | NET_RUNNING dd ? |
3626 | Serge | 227 | NET_DRV_LIST rd NET_DEVICES_MAX |
1 | ha | 228 | |
3555 | Serge | 229 | endg |
1 | ha | 230 | |
231 | |||
3555 | Serge | 232 | ;----------------------------------------------------------------- |
1 | ha | 233 | ; |
3555 | Serge | 234 | ; stack_init |
1 | ha | 235 | ; |
3555 | Serge | 236 | ; This function calls all network init procedures |
237 | ; |
||
238 | ; IN: / |
||
239 | ; OUT: / |
||
240 | ; |
||
241 | ;----------------------------------------------------------------- |
||
1168 | Lrz | 242 | align 4 |
3555 | Serge | 243 | stack_init: |
1 | ha | 244 | |
3555 | Serge | 245 | ; Init the network drivers list |
246 | xor eax, eax |
||
247 | mov edi, NET_RUNNING |
||
3626 | Serge | 248 | mov ecx, (NET_DEVICES_MAX + 2) |
3725 | Serge | 249 | rep stosd |
1 | ha | 250 | |
3555 | Serge | 251 | PPPoE_init |
1 | ha | 252 | |
3555 | Serge | 253 | IPv4_init |
254 | ; IPv6_init |
||
255 | ICMP_init |
||
1 | ha | 256 | |
3555 | Serge | 257 | ARP_init |
258 | UDP_init |
||
259 | TCP_init |
||
1 | ha | 260 | |
3555 | Serge | 261 | SOCKET_init |
1 | ha | 262 | |
3626 | Serge | 263 | LOOP_init |
264 | |||
3555 | Serge | 265 | mov [net_tmr_count], 0 |
1 | ha | 266 | |
2434 | Serge | 267 | ret |
1 | ha | 268 | |
269 | |||
270 | |||
3555 | Serge | 271 | ; Wakeup every tick. |
272 | proc stack_handler_has_work? |
||
1 | ha | 273 | |
3725 | Serge | 274 | mov eax, [timer_ticks] |
275 | cmp eax, [net_10ms] |
||
1 | ha | 276 | |
2434 | Serge | 277 | ret |
261 | hidnplayr | 278 | endp |
1 | ha | 279 | |
3555 | Serge | 280 | |
281 | ;----------------------------------------------------------------- |
||
1 | ha | 282 | ; |
3555 | Serge | 283 | ; stack_handler |
1 | ha | 284 | ; |
3555 | Serge | 285 | ; This function is called in kernel loop |
286 | ; |
||
287 | ; IN: / |
||
288 | ; OUT: / |
||
289 | ; |
||
290 | ;----------------------------------------------------------------- |
||
291 | align 4 |
||
292 | stack_handler: |
||
261 | hidnplayr | 293 | |
3555 | Serge | 294 | ; Test for 10ms tick |
295 | mov eax, [timer_ticks] |
||
296 | cmp eax, [net_10ms] |
||
297 | je .exit |
||
298 | mov [net_10ms], eax |
||
261 | hidnplayr | 299 | |
3555 | Serge | 300 | cmp [NET_RUNNING], 0 |
301 | je .exit |
||
1 | ha | 302 | |
3555 | Serge | 303 | test [net_10ms], 0x0f ; 160ms |
304 | jnz .exit |
||
1 | ha | 305 | |
3555 | Serge | 306 | TCP_timer_160ms |
1 | ha | 307 | |
3555 | Serge | 308 | test [net_10ms], 0x3f ; 640ms |
309 | jnz .exit |
||
1 | ha | 310 | |
3555 | Serge | 311 | TCP_timer_640ms |
312 | ARP_decrease_entry_ttls |
||
313 | IPv4_decrease_fragment_ttls |
||
1 | ha | 314 | |
3555 | Serge | 315 | .exit: |
316 | ret |
||
1 | ha | 317 | |
318 | |||
319 | |||
3555 | Serge | 320 | align 4 |
321 | NET_link_changed: |
||
323 | hidnplayr | 322 | |
3626 | Serge | 323 | DEBUGF DEBUG_NETWORK_VERBOSE, "NET_link_changed device=0x%x status=0x%x\n", ebx, [ebx + NET_DEVICE.link_state] |
1 | ha | 324 | |
3555 | Serge | 325 | align 4 |
326 | NET_send_event: |
||
1 | ha | 327 | |
3589 | Serge | 328 | DEBUGF DEBUG_NETWORK_VERBOSE, "NET_send_event\n" |
1 | ha | 329 | |
3555 | Serge | 330 | ; Send event to all applications |
331 | push edi ecx |
||
332 | mov edi, SLOT_BASE |
||
333 | mov ecx, [TASK_COUNT] |
||
334 | .loop: |
||
335 | add edi, 256 |
||
336 | or [edi + APPDATA.event_mask], EVENT_NETWORK2 |
||
337 | loop .loop |
||
338 | pop ecx edi |
||
1 | ha | 339 | |
3555 | Serge | 340 | ret |
1 | ha | 341 | |
342 | |||
343 | |||
3555 | Serge | 344 | ;----------------------------------------------------------------- |
345 | ; |
||
346 | ; NET_add_device: |
||
347 | ; |
||
348 | ; This function is called by the network drivers, |
||
349 | ; to register each running NIC to the kernel |
||
350 | ; |
||
351 | ; IN: Pointer to device structure in ebx |
||
352 | ; OUT: Device num in eax, -1 on error |
||
353 | ; |
||
354 | ;----------------------------------------------------------------- |
||
355 | align 4 |
||
356 | NET_add_device: |
||
1 | ha | 357 | |
3589 | Serge | 358 | DEBUGF DEBUG_NETWORK_VERBOSE, "NET_Add_Device: %x\n", ebx ;;; TODO: use mutex to lock net device list |
1 | ha | 359 | |
3626 | Serge | 360 | cmp [NET_RUNNING], NET_DEVICES_MAX |
3555 | Serge | 361 | jae .error |
1 | ha | 362 | |
3555 | Serge | 363 | ;---------------------------------- |
364 | ; Check if device is already listed |
||
365 | mov eax, ebx |
||
3626 | Serge | 366 | mov ecx, NET_DEVICES_MAX ; We need to check whole list because a device may be removed without re-organizing list |
3555 | Serge | 367 | mov edi, NET_DRV_LIST |
1 | ha | 368 | |
3725 | Serge | 369 | repne scasd ; See if device is already in the list |
3555 | Serge | 370 | jz .error |
1 | ha | 371 | |
3555 | Serge | 372 | ;---------------------------- |
373 | ; Find empty slot in the list |
||
374 | xor eax, eax |
||
3626 | Serge | 375 | mov ecx, NET_DEVICES_MAX |
3555 | Serge | 376 | mov edi, NET_DRV_LIST |
1 | ha | 377 | |
3725 | Serge | 378 | repne scasd |
3555 | Serge | 379 | jnz .error |
1 | ha | 380 | |
3555 | Serge | 381 | sub edi, 4 |
1 | ha | 382 | |
3555 | Serge | 383 | ;----------------------------- |
384 | ; Add device to the found slot |
||
385 | mov [edi], ebx ; add device to list |
||
1 | ha | 386 | |
3555 | Serge | 387 | mov eax, edi ; Calculate device number in eax |
388 | sub eax, NET_DRV_LIST |
||
389 | shr eax, 2 |
||
1 | ha | 390 | |
3555 | Serge | 391 | inc [NET_RUNNING] ; Indicate that one more network device is up and running |
1 | ha | 392 | |
3555 | Serge | 393 | call NET_send_event |
1 | ha | 394 | |
3589 | Serge | 395 | DEBUGF DEBUG_NETWORK_VERBOSE, "Device number: %u\n", eax |
2434 | Serge | 396 | ret |
1 | ha | 397 | |
3555 | Serge | 398 | .error: |
399 | or eax, -1 |
||
3589 | Serge | 400 | DEBUGF DEBUG_NETWORK_ERROR, "Adding network device failed\n" |
2434 | Serge | 401 | ret |
261 | hidnplayr | 402 | |
1 | ha | 403 | |
404 | |||
3555 | Serge | 405 | ;----------------------------------------------------------------- |
406 | ; |
||
407 | ; NET_Remove_Device: |
||
408 | ; |
||
3626 | Serge | 409 | ; This function is called by network drivers, |
3555 | Serge | 410 | ; to unregister network devices from the kernel |
411 | ; |
||
412 | ; IN: Pointer to device structure in ebx |
||
413 | ; OUT: eax: -1 on error |
||
414 | ; |
||
415 | ;----------------------------------------------------------------- |
||
416 | align 4 |
||
417 | NET_remove_device: |
||
379 | serge | 418 | |
3555 | Serge | 419 | cmp [NET_RUNNING], 0 |
420 | je .error |
||
261 | hidnplayr | 421 | |
3555 | Serge | 422 | ;---------------------------- |
423 | ; Find the driver in the list |
||
379 | serge | 424 | |
3555 | Serge | 425 | mov eax, ebx |
3626 | Serge | 426 | mov ecx, NET_DEVICES_MAX |
427 | mov edi, NET_DRV_LIST |
||
261 | hidnplayr | 428 | |
3725 | Serge | 429 | repne scasd |
3555 | Serge | 430 | jnz .error |
379 | serge | 431 | |
3555 | Serge | 432 | ;------------------------ |
433 | ; Remove it from the list |
||
379 | serge | 434 | |
3555 | Serge | 435 | xor eax, eax |
436 | mov dword [edi-4], eax |
||
3626 | Serge | 437 | dec [NET_RUNNING] |
3555 | Serge | 438 | |
439 | call NET_send_event |
||
440 | |||
3626 | Serge | 441 | xor eax, eax |
1369 | Lrz | 442 | ret |
379 | serge | 443 | |
3555 | Serge | 444 | .error: |
445 | or eax, -1 |
||
1369 | Lrz | 446 | ret |
379 | serge | 447 | |
261 | hidnplayr | 448 | |
1 | ha | 449 | |
3555 | Serge | 450 | ;----------------------------------------------------------------- |
1 | ha | 451 | ; |
3555 | Serge | 452 | ; NET_ptr_to_num |
1 | ha | 453 | ; |
3555 | Serge | 454 | ; IN: ebx = ptr to device struct |
455 | ; OUT: edi = -1 on error, device number otherwise |
||
456 | ; |
||
457 | ;----------------------------------------------------------------- |
||
1369 | Lrz | 458 | align 4 |
3555 | Serge | 459 | NET_ptr_to_num: |
3725 | Serge | 460 | |
461 | call NET_ptr_to_num4 |
||
462 | ror edi, 2 ; If -1, stay -1 |
||
463 | ; valid device numbers have last two bits 0, so do just shr |
||
464 | |||
465 | ret |
||
466 | |||
467 | align 4 |
||
468 | NET_ptr_to_num4: ; Todo, place number in device structure so we only need to verify? |
||
469 | |||
3555 | Serge | 470 | push ecx |
1369 | Lrz | 471 | |
3626 | Serge | 472 | mov ecx, NET_DEVICES_MAX |
3555 | Serge | 473 | mov edi, NET_DRV_LIST |
474 | .loop: |
||
475 | cmp ebx, [edi] |
||
3725 | Serge | 476 | je .found |
3555 | Serge | 477 | add edi, 4 |
478 | dec ecx |
||
479 | jnz .loop |
||
1 | ha | 480 | |
3555 | Serge | 481 | or edi, -1 |
482 | pop ecx |
||
2434 | Serge | 483 | ret |
1 | ha | 484 | |
3555 | Serge | 485 | .found: |
486 | sub edi, NET_DRV_LIST |
||
487 | pop ecx |
||
2434 | Serge | 488 | ret |
1 | ha | 489 | |
3555 | Serge | 490 | ;----------------------------------------------------------------- |
1 | ha | 491 | ; |
3555 | Serge | 492 | ; checksum_1 |
1 | ha | 493 | ; |
3555 | Serge | 494 | ; This is the first of two functions needed to calculate a checksum. |
495 | ; |
||
496 | ; IN: edx = start offset for semi-checksum |
||
497 | ; esi = pointer to data |
||
498 | ; ecx = data size |
||
499 | ; OUT: edx = semi-checksum |
||
500 | ; |
||
501 | ; |
||
502 | ; Code was optimized by diamond |
||
503 | ; |
||
504 | ;----------------------------------------------------------------- |
||
505 | align 4 |
||
506 | checksum_1: |
||
1369 | Lrz | 507 | |
3555 | Serge | 508 | shr ecx, 1 |
509 | pushf |
||
510 | jz .no_2 |
||
1369 | Lrz | 511 | |
3555 | Serge | 512 | shr ecx, 1 |
513 | pushf |
||
514 | jz .no_4 |
||
1369 | Lrz | 515 | |
3555 | Serge | 516 | shr ecx, 1 |
517 | pushf |
||
518 | jz .no_8 |
||
1369 | Lrz | 519 | |
3555 | Serge | 520 | .loop: |
521 | add dl, [esi+1] |
||
522 | adc dh, [esi+0] |
||
1 | ha | 523 | |
3555 | Serge | 524 | adc dl, [esi+3] |
525 | adc dh, [esi+2] |
||
1 | ha | 526 | |
3555 | Serge | 527 | adc dl, [esi+5] |
528 | adc dh, [esi+4] |
||
1 | ha | 529 | |
3555 | Serge | 530 | adc dl, [esi+7] |
531 | adc dh, [esi+6] |
||
1 | ha | 532 | |
3555 | Serge | 533 | adc edx, 0 |
534 | add esi, 8 |
||
1 | ha | 535 | |
3555 | Serge | 536 | dec ecx |
537 | jnz .loop |
||
1 | ha | 538 | |
3555 | Serge | 539 | adc edx, 0 |
1 | ha | 540 | |
3555 | Serge | 541 | .no_8: |
542 | popf |
||
543 | jnc .no_4 |
||
1 | ha | 544 | |
3555 | Serge | 545 | add dl, [esi+1] |
546 | adc dh, [esi+0] |
||
1 | ha | 547 | |
3555 | Serge | 548 | adc dl, [esi+3] |
549 | adc dh, [esi+2] |
||
1 | ha | 550 | |
3555 | Serge | 551 | adc edx, 0 |
552 | add esi, 4 |
||
1 | ha | 553 | |
3555 | Serge | 554 | .no_4: |
555 | popf |
||
556 | jnc .no_2 |
||
1 | ha | 557 | |
3555 | Serge | 558 | add dl, [esi+1] |
559 | adc dh, [esi+0] |
||
1 | ha | 560 | |
3555 | Serge | 561 | adc edx, 0 |
562 | inc esi |
||
563 | inc esi |
||
1 | ha | 564 | |
3555 | Serge | 565 | .no_2: |
566 | popf |
||
567 | jnc .end |
||
1 | ha | 568 | |
3555 | Serge | 569 | add dh, [esi+0] |
570 | adc edx, 0 |
||
571 | .end: |
||
2434 | Serge | 572 | ret |
1 | ha | 573 | |
3555 | Serge | 574 | ;----------------------------------------------------------------- |
575 | ; |
||
576 | ; checksum_2 |
||
577 | ; |
||
578 | ; This function calculates the final ip/tcp/udp checksum for you |
||
579 | ; |
||
580 | ; IN: edx = semi-checksum |
||
581 | ; OUT: dx = checksum (in INET byte order) |
||
582 | ; |
||
583 | ;----------------------------------------------------------------- |
||
584 | align 4 |
||
585 | checksum_2: |
||
1 | ha | 586 | |
3555 | Serge | 587 | mov ecx, edx |
588 | shr ecx, 16 |
||
589 | and edx, 0xffff |
||
590 | add edx, ecx |
||
1 | ha | 591 | |
3555 | Serge | 592 | mov ecx, edx |
593 | shr ecx, 16 |
||
594 | add dx, cx |
||
595 | test dx, dx ; it seems that ZF is not set when CF is set :( |
||
596 | not dx |
||
597 | jnz .not_zero |
||
598 | dec dx |
||
599 | .not_zero: |
||
600 | xchg dl, dh |
||
1 | ha | 601 | |
3589 | Serge | 602 | DEBUGF DEBUG_NETWORK_VERBOSE, "Checksum: %x\n", dx |
3555 | Serge | 603 | |
2434 | Serge | 604 | ret |
1 | ha | 605 | |
606 | |||
607 | |||
3555 | Serge | 608 | ;---------------------------------------------------------------- |
609 | ; |
||
3626 | Serge | 610 | ; System function to work with network devices (74) |
3555 | Serge | 611 | ; |
612 | ;---------------------------------------------------------------- |
||
613 | align 4 |
||
3626 | Serge | 614 | sys_network: |
1 | ha | 615 | |
3555 | Serge | 616 | cmp ebx, -1 |
617 | jne @f |
||
1 | ha | 618 | |
3555 | Serge | 619 | mov eax, [NET_RUNNING] |
3626 | Serge | 620 | mov [esp+32], eax |
621 | ret |
||
1 | ha | 622 | |
3555 | Serge | 623 | @@: |
3626 | Serge | 624 | cmp bh, NET_DEVICES_MAX ; Check if device number exists |
3555 | Serge | 625 | jae .doesnt_exist |
1 | ha | 626 | |
3555 | Serge | 627 | mov esi, ebx |
628 | and esi, 0x0000ff00 |
||
629 | shr esi, 6 |
||
1 | ha | 630 | |
3555 | Serge | 631 | cmp dword [esi + NET_DRV_LIST], 0 ; check if driver is running |
632 | je .doesnt_exist |
||
1 | ha | 633 | |
3555 | Serge | 634 | mov eax, [esi + NET_DRV_LIST] |
1 | ha | 635 | |
3555 | Serge | 636 | and ebx, 0x000000ff |
637 | cmp ebx, .number |
||
638 | ja .doesnt_exist |
||
639 | jmp dword [.table + 4*ebx] |
||
1 | ha | 640 | |
3555 | Serge | 641 | .table: |
642 | dd .get_type ; 0 |
||
643 | dd .get_dev_name ; 1 |
||
644 | dd .reset ; 2 |
||
645 | dd .stop ; 3 |
||
646 | dd .get_ptr ; 4 |
||
647 | dd .get_drv_name ; 5 |
||
3626 | Serge | 648 | |
649 | dd .packets_tx ; 6 |
||
650 | dd .packets_rx ; 7 |
||
651 | dd .bytes_tx ; 8 |
||
652 | dd .bytes_rx ; 9 |
||
653 | dd .state ; 10 |
||
3555 | Serge | 654 | .number = ($ - .table) / 4 - 1 |
1 | ha | 655 | |
3626 | Serge | 656 | .get_type: |
657 | mov eax, [eax + NET_DEVICE.device_type] |
||
658 | mov [esp+32], eax |
||
659 | ret |
||
1 | ha | 660 | |
3626 | Serge | 661 | .get_dev_name: |
3555 | Serge | 662 | mov esi, [eax + NET_DEVICE.name] |
663 | mov edi, ecx |
||
1 | ha | 664 | |
3555 | Serge | 665 | mov ecx, 64/4 ; max length |
3725 | Serge | 666 | rep movsd |
3555 | Serge | 667 | |
2434 | Serge | 668 | xor eax, eax |
3626 | Serge | 669 | mov [esp+32], eax |
670 | ret |
||
1 | ha | 671 | |
3626 | Serge | 672 | .reset: |
3555 | Serge | 673 | call [eax + NET_DEVICE.reset] |
3626 | Serge | 674 | mov [esp+32], eax |
675 | ret |
||
1 | ha | 676 | |
3626 | Serge | 677 | .stop: |
3555 | Serge | 678 | call [eax + NET_DEVICE.unload] |
3626 | Serge | 679 | mov [esp+32], eax |
680 | ret |
||
1 | ha | 681 | |
682 | |||
3626 | Serge | 683 | .get_ptr: |
684 | mov [esp+32], eax |
||
685 | ret |
||
1 | ha | 686 | |
687 | |||
3626 | Serge | 688 | .get_drv_name: |
2434 | Serge | 689 | xor eax, eax |
3626 | Serge | 690 | mov [esp+32], eax |
691 | ret |
||
3555 | Serge | 692 | |
3626 | Serge | 693 | .packets_tx: |
694 | mov eax, [eax + NET_DEVICE.packets_tx] |
||
695 | mov [esp+32], eax |
||
696 | ret |
||
3555 | Serge | 697 | |
3626 | Serge | 698 | .packets_rx: |
699 | mov eax, [eax + NET_DEVICE.packets_rx] |
||
700 | mov [esp+32], eax |
||
701 | ret |
||
3555 | Serge | 702 | |
3626 | Serge | 703 | .bytes_tx: |
704 | mov ebx, dword [eax + NET_DEVICE.bytes_tx + 4] |
||
705 | mov [esp+20], ebx |
||
706 | mov eax, dword [eax + NET_DEVICE.bytes_tx] |
||
707 | mov [esp+32], eax |
||
708 | ret |
||
3555 | Serge | 709 | |
3626 | Serge | 710 | .bytes_rx: |
711 | mov ebx, dword [eax + NET_DEVICE.bytes_rx + 4] |
||
712 | mov [esp+20], ebx |
||
713 | mov eax, dword [eax + NET_DEVICE.bytes_rx] |
||
714 | mov [esp+32], eax |
||
715 | ret |
||
3555 | Serge | 716 | |
3626 | Serge | 717 | .state: |
718 | mov eax, [eax + NET_DEVICE.link_state] |
||
3555 | Serge | 719 | mov [esp+32], eax |
2434 | Serge | 720 | ret |
1 | ha | 721 | |
722 | |||
3626 | Serge | 723 | .doesnt_exist: |
724 | mov dword[esp+32], -1 |
||
725 | ret |
||
726 | |||
727 | |||
728 | |||
3555 | Serge | 729 | ;---------------------------------------------------------------- |
1 | ha | 730 | ; |
3555 | Serge | 731 | ; System function to work with protocols (76) |
1 | ha | 732 | ; |
3555 | Serge | 733 | ;---------------------------------------------------------------- |
734 | align 4 |
||
735 | sys_protocols: |
||
3626 | Serge | 736 | cmp bh, NET_DEVICES_MAX ; Check if device number exists |
3555 | Serge | 737 | jae .doesnt_exist |
1 | ha | 738 | |
3555 | Serge | 739 | mov esi, ebx |
740 | and esi, 0x0000ff00 |
||
741 | shr esi, 6 ; now we have the device num * 4 in esi |
||
742 | cmp [esi + NET_DRV_LIST], 0 ; check if driver is running |
||
743 | je .doesnt_exist |
||
1 | ha | 744 | |
3555 | Serge | 745 | push .return ; return address (we will be using jumps instead of calls) |
1 | ha | 746 | |
3555 | Serge | 747 | mov eax, ebx ; set ax to protocol number |
748 | shr eax, 16 ; |
||
1 | ha | 749 | |
3555 | Serge | 750 | cmp ax, API_ETH |
751 | je ETH_api |
||
1 | ha | 752 | |
3555 | Serge | 753 | cmp ax, API_IPv4 |
754 | je IPv4_api |
||
1 | ha | 755 | |
3555 | Serge | 756 | cmp ax, API_ICMP |
757 | je ICMP_api |
||
1 | ha | 758 | |
3555 | Serge | 759 | cmp ax, API_UDP |
760 | je UDP_api |
||
1 | ha | 761 | |
3555 | Serge | 762 | cmp ax, API_TCP |
763 | je TCP_api |
||
1 | ha | 764 | |
3555 | Serge | 765 | cmp ax, API_ARP |
766 | je ARP_api |
||
1 | ha | 767 | |
3555 | Serge | 768 | cmp ax, API_PPPOE |
769 | je PPPoE_api |
||
1 | ha | 770 | |
3555 | Serge | 771 | cmp ax, API_IPv6 |
772 | je IPv6_api |
||
1 | ha | 773 | |
3555 | Serge | 774 | add esp, 4 ; if we reached here, no function was called, so we need to balance stack |
1 | ha | 775 | |
3555 | Serge | 776 | .doesnt_exist: |
777 | mov eax, -1 |
||
1 | ha | 778 | |
3555 | Serge | 779 | .return: |
780 | mov [esp+28+4], eax ; return eax value to the program |
||
2434 | Serge | 781 | ret |