Subversion Repositories Kolibri OS

Rev

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