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