Subversion Repositories Kolibri OS

Rev

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