Subversion Repositories Kolibri OS

Rev

Rev 2366 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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