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