Subversion Repositories Kolibri OS

Rev

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