Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5015 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2014. All rights reserved.    ;;
3545 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  ARP.INC                                                        ;;
7
;;                                                                 ;;
8
;;  Part of the tcp/ip network stack for KolibriOS                 ;;
9
;;                                                                 ;;
10
;;  Based on the work of [Johnny_B] and [smb]                      ;;
11
;;                                                                 ;;
12
;;    Written by hidnplayr@kolibrios.org                           ;;
13
;;                                                                 ;;
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
15
;;             Version 2, June- 1991                               ;;
16
;;                                                                 ;;
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
 
4850 mario79 19
$Revision: 5015 $
3545 hidnplayr 20
 
21
ARP_NO_ENTRY            = 0
22
ARP_VALID_MAPPING       = 1
23
ARP_AWAITING_RESPONSE   = 2
24
ARP_RESPONSE_TIMEOUT    = 3
25
 
26
ARP_REQUEST_TTL         = 31          ; 20 s
27
ARP_ENTRY_TTL           = 937         ; 600 s
28
ARP_STATIC_ENTRY        = -1
29
 
30
ARP_REQ_OPCODE          = 0x0100      ; request
31
ARP_REP_OPCODE          = 0x0200      ; reply
32
 
33
ARP_TABLE_SIZE          = 20          ; Size of table
34
 
3601 hidnplayr 35
struct  ARP_entry
3545 hidnplayr 36
 
3601 hidnplayr 37
        IP              dd ?
38
        MAC             dp ?
39
        Status          dw ?
40
        TTL             dw ?
3545 hidnplayr 41
 
42
ends
43
 
3601 hidnplayr 44
struct  ARP_header
3545 hidnplayr 45
 
3601 hidnplayr 46
        HardwareType    dw ?
47
        ProtocolType    dw ?
48
        HardwareSize    db ?
49
        ProtocolSize    db ?
50
        Opcode          dw ?
51
        SenderMAC       dp ?
52
        SenderIP        dd ?
53
        TargetMAC       dp ?
54
        TargetIP        dd ?
3545 hidnplayr 55
 
56
ends
57
 
3698 hidnplayr 58
uglobal
3545 hidnplayr 59
align 4
60
 
3601 hidnplayr 61
        ARP_table       rb NET_DEVICES_MAX*(ARP_TABLE_SIZE * sizeof.ARP_entry)
3545 hidnplayr 62
 
3601 hidnplayr 63
        ARP_entries_num rd NET_DEVICES_MAX
3600 hidnplayr 64
        ARP_PACKETS_TX  rd NET_DEVICES_MAX
65
        ARP_PACKETS_RX  rd NET_DEVICES_MAX
66
        ARP_CONFLICTS   rd NET_DEVICES_MAX
3545 hidnplayr 67
 
68
 
69
endg
70
 
71
 
72
 
73
;-----------------------------------------------------------------
74
;
75
; ARP_init
76
;
77
;  This function resets all ARP variables
78
;
79
;-----------------------------------------------------------------
80
macro ARP_init {
81
 
82
        xor     eax, eax
3601 hidnplayr 83
        mov     edi, ARP_entries_num
84
        mov     ecx, 4*NET_DEVICES_MAX
3711 clevermous 85
        rep stosd
3545 hidnplayr 86
 
87
}
88
 
89
;---------------------------------------------------------------------------
90
;
91
; ARP_decrease_entry_ttls
92
;
93
;---------------------------------------------------------------------------
94
 
95
macro ARP_decrease_entry_ttls {
96
 
97
local   .loop
98
local   .exit
99
 
100
; The TTL field is decremented every second, and is deleted when it reaches 0.
101
; It is refreshed every time a packet is received.
102
; If the TTL field is 0xFFFF it is a static entry and is never deleted.
103
; The status field can be the following values:
104
; 0x0000  entry not used
105
; 0x0001  entry holds a valid mapping
106
; 0x0002  entry contains an IP address, awaiting ARP response
107
; 0x0003  No response received to ARP request.
108
; The last status value is provided to allow the network layer to delete
109
; a packet that is queued awaiting an ARP response
110
 
3601 hidnplayr 111
        xor     edi, edi
112
  .loop_outer:
113
        mov     ecx, [ARP_entries_num + 4*edi]
3545 hidnplayr 114
        test    ecx, ecx
115
        jz      .exit
116
 
3601 hidnplayr 117
        mov     esi, (ARP_TABLE_SIZE * sizeof.ARP_entry)
118
        imul    esi, edi
119
        add     esi, ARP_table
3545 hidnplayr 120
  .loop:
121
        cmp     [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
122
        je      .next
123
 
124
        dec     [esi + ARP_entry.TTL]
125
        jz      .time_out
126
 
127
  .next:
128
        add     esi, sizeof.ARP_entry
129
        dec     ecx
130
        jnz     .loop
131
        jmp     .exit
132
 
133
  .time_out:
134
        cmp     [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE
135
        je      .response_timeout
136
 
3601 hidnplayr 137
        push    esi edi ecx
3545 hidnplayr 138
        call    ARP_del_entry
3601 hidnplayr 139
        pop     ecx edi esi
3545 hidnplayr 140
 
141
        jmp     .next
142
 
143
  .response_timeout:
144
        mov     [esi + ARP_entry.Status], ARP_RESPONSE_TIMEOUT
145
        mov     [esi + ARP_entry.TTL], 10
146
 
147
        jmp     .next
148
 
149
  .exit:
3601 hidnplayr 150
        inc     edi
151
        cmp     edi, NET_DEVICES_MAX
152
        jb      .loop_outer
3545 hidnplayr 153
 
154
}
155
 
156
 
157
;-----------------------------------------------------------------
158
;
159
; ARP_input
160
;
161
;  IN:  Pointer to buffer in [esp]
162
;       size of buffer in [esp+4]
163
;       packet size (without ethernet header) in ecx
164
;       packet ptr in edx
165
;       device ptr in ebx
166
;  OUT: /
167
;
168
;-----------------------------------------------------------------
169
align 4
170
ARP_input:
171
 
172
;-----------------------------------------
173
; Check validity and print some debug info
174
 
175
        cmp     ecx, sizeof.ARP_header
176
        jb      .exit
177
 
3643 hidnplayr 178
        call    NET_ptr_to_num4
3545 hidnplayr 179
        cmp     edi, -1
180
        jz      .exit
181
 
3643 hidnplayr 182
        inc     [ARP_PACKETS_RX + edi]          ; update stats
3545 hidnplayr 183
 
3643 hidnplayr 184
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: got packet from %u.%u.%u.%u (device*4=%u)\n",\
3545 hidnplayr 185
        [edx + ARP_header.SenderIP]:1, [edx + ARP_header.SenderIP + 1]:1,\
186
        [edx + ARP_header.SenderIP + 2]:1, [edx + ARP_header.SenderIP + 3]:1, edi
187
 
188
;------------------------------
189
; First, check for IP collision
190
 
191
        mov     eax, [edx + ARP_header.SenderIP]
3643 hidnplayr 192
        cmp     eax, [IP_LIST + edi]
3545 hidnplayr 193
        je      .collision
194
 
195
;---------------------
196
; Handle reply packets
197
 
198
        cmp     [edx + ARP_header.Opcode], ARP_REP_OPCODE
199
        jne     .maybe_request
200
 
3556 hidnplayr 201
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: It's a reply\n"
3545 hidnplayr 202
 
3643 hidnplayr 203
        mov     ecx, [ARP_entries_num + edi]
3545 hidnplayr 204
        test    ecx, ecx
205
        jz      .exit
206
 
3643 hidnplayr 207
        mov     esi, edi
208
        imul    esi, (ARP_TABLE_SIZE * sizeof.ARP_entry)/4
3601 hidnplayr 209
        add     esi, ARP_table
3545 hidnplayr 210
  .loop:
211
        cmp     [esi + ARP_entry.IP], eax
212
        je      .gotit
213
        add     esi, sizeof.ARP_entry
214
        dec     ecx
215
        jnz     .loop
216
 
3556 hidnplayr 217
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: no matching entry found\n"
3545 hidnplayr 218
        jmp     .exit
219
 
220
  .gotit:
3556 hidnplayr 221
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: found matching entry\n"
3545 hidnplayr 222
 
223
        cmp     [esi + ARP_entry.TTL], ARP_STATIC_ENTRY         ; if it is a static entry, dont touch it
224
        je      .exit
225
 
3556 hidnplayr 226
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: updating entry\n"
3545 hidnplayr 227
 
228
        mov     [esi + ARP_entry.Status], ARP_VALID_MAPPING
229
        mov     [esi + ARP_entry.TTL], ARP_ENTRY_TTL
230
 
231
        mov     eax, dword [edx + ARP_header.SenderMAC]
232
        mov     dword [esi + ARP_entry.MAC], eax
233
        mov     cx, word [edx + ARP_header.SenderMAC + 4]
234
        mov     word [esi + ARP_entry.MAC + 4], cx
235
 
236
        jmp     .exit
237
 
238
;-----------------------
239
; Handle request packets
240
 
241
  .maybe_request:
242
        cmp     [edx + ARP_header.Opcode], ARP_REQ_OPCODE
243
        jne     .exit
244
 
3556 hidnplayr 245
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: its a request\n"
3545 hidnplayr 246
 
3643 hidnplayr 247
        mov     eax, [IP_LIST + edi]
3545 hidnplayr 248
        cmp     eax, [edx + ARP_header.TargetIP]                ; Is it looking for my IP address?
249
        jne     .exit
250
 
251
        push    eax
252
        push    edi
253
 
254
; OK, it is a request for one of our MAC addresses.
255
; Build the frame and send it. We can reuse the buffer.  (faster then using ARP_create_packet)
256
 
257
        lea     esi, [edx + ARP_header.SenderMAC]
258
        lea     edi, [edx + ARP_header.TargetMAC]
259
        movsd                                                   ; Move Sender Mac to Dest MAC
260
        movsw                                                   ;
261
        movsd                                                   ; Move sender IP to Dest IP
262
 
263
        pop     esi
3643 hidnplayr 264
        mov     esi, [NET_DRV_LIST + esi]
3545 hidnplayr 265
        lea     esi, [esi + ETH_DEVICE.mac]
266
        lea     edi, [edx + ARP_header.SenderMAC]
267
        movsd                                                   ; Copy MAC address from in MAC_LIST
268
        movsw                                                   ;
269
        pop     eax
270
        stosd                                                   ; Write our IP
271
 
272
        mov     [edx + ARP_header.Opcode], ARP_REP_OPCODE
273
 
274
; Now, Fill in ETHERNET header
275
 
276
        mov     edi, [esp]
277
        lea     esi, [edx + ARP_header.TargetMAC]
278
        movsd
279
        movsw
280
        lea     esi, [edx + ARP_header.SenderMAC]
281
        movsd
282
        movsw
283
;        mov     ax , ETHER_ARP                                 ; It's already there, I'm sure of it!
284
;        stosw
285
 
3556 hidnplayr 286
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: Sending reply\n"
3545 hidnplayr 287
 
288
        call    [ebx + NET_DEVICE.transmit]
289
        ret
290
 
291
  .collision:
3643 hidnplayr 292
        inc     [ARP_CONFLICTS + edi]
3556 hidnplayr 293
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: IP address conflict detected!\n"
3545 hidnplayr 294
 
295
  .exit:
3861 hidnplayr 296
        call    NET_packet_free
3545 hidnplayr 297
        add     esp, 4                                          ; pop (balance stack)
298
 
3556 hidnplayr 299
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_input: exiting\n"
3545 hidnplayr 300
        ret
301
 
302
 
303
;---------------------------------------------------------------------------
304
;
305
; ARP_output_request
306
;
3601 hidnplayr 307
; IN:   ebx = device ptr
308
;       eax = IP
3545 hidnplayr 309
; OUT: /
3601 hidnplayr 310
;       scratched: probably everything
3545 hidnplayr 311
;
312
;---------------------------------------------------------------------------
313
align 4
314
ARP_output_request:
315
 
3601 hidnplayr 316
        push    eax
3545 hidnplayr 317
 
3601 hidnplayr 318
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_output_request: ip=%u.%u.%u.%u device=0x%x\n",\
319
        [esp]:1, [esp + 1]:1, [esp + 2]:1, [esp + 3]:1, ebx
3545 hidnplayr 320
 
5015 hidnplayr 321
        mov     ax, ETHER_PROTO_ARP
322
        mov     ecx, sizeof.ARP_header
3545 hidnplayr 323
        mov     edx, ETH_BROADCAST              ; broadcast mac
324
        call    ETH_output
325
        jz      .exit
326
 
327
        mov     [edi + ARP_header.HardwareType], 0x0100         ; Ethernet
328
        mov     [edi + ARP_header.ProtocolType], 0x0008         ; IP
329
        mov     [edi + ARP_header.HardwareSize], 6              ; MAC-addr length
330
        mov     [edi + ARP_header.ProtocolSize], 4              ; IP-addr length
331
        mov     [edi + ARP_header.Opcode], ARP_REQ_OPCODE       ; Request
332
 
333
        add     edi, ARP_header.SenderMAC
334
        lea     esi, [ebx + ETH_DEVICE.mac]     ; SenderMac
335
        movsw                                   ;
336
        movsd                                   ;
337
 
3644 hidnplayr 338
        push    edi
339
        call    NET_ptr_to_num4
340
        inc     [ARP_PACKETS_TX + edi]          ; assume we will succeed
341
        lea     esi, [IP_LIST + edi]            ; SenderIP
342
        pop     edi
3601 hidnplayr 343
        movsd
3545 hidnplayr 344
 
3601 hidnplayr 345
        mov     esi, ETH_BROADCAST              ; DestMac
346
        movsw                                   ;
347
        movsd                                   ;
348
        popd    [edi]                           ; DestIP
3545 hidnplayr 349
 
3601 hidnplayr 350
        push    edx eax
3545 hidnplayr 351
        call    [ebx + NET_DEVICE.transmit]
352
        ret
353
 
354
  .exit:
3601 hidnplayr 355
        add     esp, 4
356
        DEBUGF  DEBUG_NETWORK_ERROR, "ARP_output_request: send failed\n"
3545 hidnplayr 357
        ret
358
 
359
 
360
;-----------------------------------------------------------------
361
;
362
; ARP_add_entry (or update)
363
;
364
; IN:  esi = ptr to entry (can easily be made on the stack)
3638 hidnplayr 365
;      edi = device num*4
3545 hidnplayr 366
; OUT: eax = entry #, -1 on error
3601 hidnplayr 367
;      esi = ptr to newly created entry
3545 hidnplayr 368
;
369
;-----------------------------------------------------------------      ; TODO: use a mutex
370
align 4
371
ARP_add_entry:
372
 
3601 hidnplayr 373
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_add_entry: device=%u\n", edi
3545 hidnplayr 374
 
3638 hidnplayr 375
        mov     ecx, [ARP_entries_num + edi]
3545 hidnplayr 376
        cmp     ecx, ARP_TABLE_SIZE                                     ; list full ?
3601 hidnplayr 377
        jae     .full
3545 hidnplayr 378
 
3601 hidnplayr 379
; From this point on, we can only fail if IP has a static entry, or if table is corrupt.
380
 
3638 hidnplayr 381
        inc     [ARP_entries_num + edi]                                 ; assume we will succeed
3601 hidnplayr 382
 
383
        push    edi
384
        xor     ecx, ecx
3638 hidnplayr 385
        imul    edi, ARP_TABLE_SIZE*sizeof.ARP_entry/4
3601 hidnplayr 386
        add     edi, ARP_table
3609 hidnplayr 387
        mov     eax, [esi + ARP_entry.IP]
3545 hidnplayr 388
  .loop:
389
        cmp     [edi + ARP_entry.Status], ARP_NO_ENTRY                  ; is this slot empty?
390
        je      .add
391
 
3601 hidnplayr 392
        cmp     [edi + ARP_entry.IP], eax                               ; if not, check if it doesnt collide
3545 hidnplayr 393
        jne     .maybe_next
394
 
395
        cmp     [edi + ARP_entry.TTL], ARP_STATIC_ENTRY                 ; ok, its the same IP, update it if not static
396
        jne     .add
397
 
3601 hidnplayr 398
        DEBUGF  DEBUG_NETWORK_ERROR, "ARP_add_entry: failed, IP already has a static entry\n"
399
        jmp     .error
400
 
3545 hidnplayr 401
  .maybe_next:                                                          ; try the next slot
402
        add     edi, sizeof.ARP_entry
3601 hidnplayr 403
        inc     ecx
404
        cmp     ecx, ARP_TABLE_SIZE
405
        jb      .loop
3545 hidnplayr 406
 
407
  .add:
3601 hidnplayr 408
        push    ecx
3545 hidnplayr 409
        mov     ecx, sizeof.ARP_entry/2
3711 clevermous 410
        rep movsw
3601 hidnplayr 411
        pop     ecx
412
        lea     esi, [edi - sizeof.ARP_entry]
413
        pop     edi
414
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_add_entry: entry=%u\n", ecx
3545 hidnplayr 415
 
416
        ret
417
 
418
  .error:
3601 hidnplayr 419
        pop     edi
3638 hidnplayr 420
        dec     [ARP_entries_num + edi]
3601 hidnplayr 421
        DEBUGF  DEBUG_NETWORK_ERROR, "ARP_add_entry_failed\n"
422
  .full:
3545 hidnplayr 423
        mov     eax, -1
424
        ret
425
 
426
 
427
;-----------------------------------------------------------------
428
;
429
; ARP_del_entry
430
;
3601 hidnplayr 431
; IN:   esi = ptr to arp entry
432
;       edi = device number
433
; OUT:  /
3545 hidnplayr 434
;
435
;-----------------------------------------------------------------
436
align 4
437
ARP_del_entry:
438
 
3601 hidnplayr 439
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_del_entry: entry=%x entrys=%u\n", esi, [ARP_entries_num + 4*edi]
3556 hidnplayr 440
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_del_entry: IP=%u.%u.%u.%u\n", \
3545 hidnplayr 441
        [esi + ARP_entry.IP]:1, [esi + ARP_entry.IP + 1]:1, [esi + ARP_entry.IP + 2]:1, [esi + ARP_entry.IP + 3]:1
442
 
3601 hidnplayr 443
        push    edi
444
        imul    edi, (ARP_TABLE_SIZE) * sizeof.ARP_entry
445
        lea     ecx, [ARP_table + (ARP_TABLE_SIZE - 1) * sizeof.ARP_entry + edi]
3545 hidnplayr 446
        sub     ecx, esi
447
        shr     ecx, 1
448
 
3601 hidnplayr 449
; move all trailing entries, sizeof.ARP_entry bytes to left.
3545 hidnplayr 450
        mov     edi, esi
451
        add     esi, sizeof.ARP_entry
3711 clevermous 452
        rep movsw
3545 hidnplayr 453
 
3601 hidnplayr 454
; now add an empty entry to the end (erasing previous one)
3545 hidnplayr 455
        xor     eax, eax
456
        mov     ecx, sizeof.ARP_entry/2
3711 clevermous 457
        rep stosw
3545 hidnplayr 458
 
3601 hidnplayr 459
        pop     edi
460
        dec     [ARP_entries_num + 4*edi]
3556 hidnplayr 461
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_del_entry: success\n"
3545 hidnplayr 462
 
463
        ret
464
 
465
 
466
 
467
 
468
 
469
;-----------------------------------------------------------------
470
;
471
; ARP_IP_to_MAC
472
;
473
;  This function translates an IP address to a MAC address
474
;
475
;  IN:  eax = IPv4 address
3638 hidnplayr 476
;       edi = device number * 4
3545 hidnplayr 477
;  OUT: eax = -1 on error, -2 means request send
478
;      else, ax = first two bytes of mac (high 16 bits of eax will be 0)
479
;       ebx = last four bytes of mac
480
;       edi = unchanged
481
;
482
;-----------------------------------------------------------------
483
align 4
484
ARP_IP_to_MAC:
485
 
3556 hidnplayr 486
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: %u.%u", al, ah
3545 hidnplayr 487
        rol     eax, 16
3638 hidnplayr 488
        DEBUGF  DEBUG_NETWORK_VERBOSE, ".%u.%u device*4: %u\n", al, ah, edi
3545 hidnplayr 489
        rol     eax, 16
490
 
491
        cmp     eax, 0xffffffff
492
        je      .broadcast
493
 
494
;--------------------------------
495
; Try to find the IP in ARP_table
496
 
3640 hidnplayr 497
        mov     ecx, [ARP_entries_num + edi]
3545 hidnplayr 498
        test    ecx, ecx
499
        jz      .not_in_list
3609 hidnplayr 500
        mov     esi, edi
3640 hidnplayr 501
        imul    esi, (sizeof.ARP_entry * ARP_TABLE_SIZE)/4
3609 hidnplayr 502
        add     esi, ARP_table + ARP_entry.IP
3545 hidnplayr 503
  .scan_loop:
504
        cmp     [esi], eax
505
        je      .found_it
506
        add     esi, sizeof.ARP_entry
3601 hidnplayr 507
        dec     ecx
508
        jnz     .scan_loop
3545 hidnplayr 509
 
510
  .not_in_list:
3556 hidnplayr 511
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: preparing for ARP request\n"
3545 hidnplayr 512
 
513
        push    eax edi                 ; save IP for ARP_output_request
3601 hidnplayr 514
; Now craft the ARP entry on the stack
3545 hidnplayr 515
        pushw   ARP_REQUEST_TTL         ; TTL
516
        pushw   ARP_AWAITING_RESPONSE   ; status
517
        pushd   0                       ; mac
518
        pushw   0
519
        pushd   eax                     ; ip
520
        mov     esi, esp
3601 hidnplayr 521
 
522
; Add it to the list
3545 hidnplayr 523
        call    ARP_add_entry
3601 hidnplayr 524
 
525
; Delete the temporary entry
3545 hidnplayr 526
        add     esp, sizeof.ARP_entry   ; clear the entry from stack
527
 
3601 hidnplayr 528
; If we could not add it to the list, give up
3545 hidnplayr 529
        cmp     eax, -1                 ; did ARP_add_entry fail?
530
        je      .full
531
 
3601 hidnplayr 532
;-----------------------------------------------
533
; At this point, we got an ARP entry in the list
3545 hidnplayr 534
 
3601 hidnplayr 535
; Now send a request packet on the network
536
        pop     edi eax                 ; IP in eax, device number in ebx, for ARP_output_request
537
 
3545 hidnplayr 538
        push    esi edi
3638 hidnplayr 539
        mov     ebx, [NET_DRV_LIST + edi]
3601 hidnplayr 540
        call    ARP_output_request
3545 hidnplayr 541
        pop     edi esi
542
  .found_it:
543
        cmp     [esi + ARP_entry.Status], ARP_VALID_MAPPING             ; Does it have a MAC assigned?
544
        je      .valid
545
 
546
if ARP_BLOCK
547
 
548
        cmp     [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE         ; Are we waiting for reply from remote end?
549
        jne     .give_up
3602 hidnplayr 550
        push    esi
3545 hidnplayr 551
        mov     esi, 10                 ; wait 10 ms
552
        call    delay_ms
3602 hidnplayr 553
        pop     esi
3545 hidnplayr 554
        jmp     .found_it               ; now check again
555
 
556
else
557
 
558
        jmp     .give_up
559
 
560
end if
561
 
562
  .valid:
3556 hidnplayr 563
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: found MAC\n"
3602 hidnplayr 564
        movzx   eax, word[esi + ARP_entry.MAC]
565
        mov     ebx, dword[esi + ARP_entry.MAC + 2]
3545 hidnplayr 566
        ret
567
 
568
  .full:
3556 hidnplayr 569
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: table is full!\n"
3545 hidnplayr 570
        add     esp, 8
571
  .give_up:
3556 hidnplayr 572
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: entry has no valid mapping!\n"
3545 hidnplayr 573
        mov     eax, -1
574
        ret
575
 
576
  .broadcast:
577
        mov     eax, 0x0000ffff
578
        mov     ebx, 0xffffffff
579
        ret
580
 
581
 
582
;-----------------------------------------------------------------
583
;
584
; ARP_API
585
;
3609 hidnplayr 586
; This function is called by system function 76
3545 hidnplayr 587
;
588
; IN:  subfunction number in bl
589
;      device number in bh
590
;      ecx, edx, .. depends on subfunction
591
;
592
; OUT:  ?
593
;
594
;-----------------------------------------------------------------
595
align 4
596
ARP_api:
597
 
598
        movzx   eax, bh
599
        shl     eax, 2
600
 
601
        and     ebx, 0xff
602
        cmp     ebx, .number
603
        ja      .error
604
        jmp     dword [.table + 4*ebx]
605
 
606
  .table:
607
        dd      .packets_tx     ; 0
608
        dd      .packets_rx     ; 1
609
        dd      .entries        ; 2
610
        dd      .read           ; 3
611
        dd      .write          ; 4
612
        dd      .remove         ; 5
613
        dd      .send_announce  ; 6
614
        dd      .conflicts      ; 7
615
  .number = ($ - .table) / 4 - 1
616
 
617
  .error:
618
        mov     eax, -1
619
        ret
620
 
621
  .packets_tx:
622
        mov     eax, [ARP_PACKETS_TX + eax]
623
        ret
624
 
625
  .packets_rx:
626
        mov     eax, [ARP_PACKETS_RX + eax]
627
        ret
628
 
629
  .conflicts:
630
        mov     eax, [ARP_CONFLICTS + eax]
631
        ret
632
 
633
  .entries:
3601 hidnplayr 634
        mov     eax, [ARP_entries_num + eax]
3545 hidnplayr 635
        ret
636
 
637
  .read:
3601 hidnplayr 638
        cmp     ecx, [ARP_entries_num + eax]
3545 hidnplayr 639
        jae     .error
3601 hidnplayr 640
        shr     eax, 2
641
        imul    eax, sizeof.ARP_entry*ARP_TABLE_SIZE
642
        add     eax, ARP_table
3545 hidnplayr 643
        ; edi = pointer to buffer
644
        ; ecx = # entry
645
        imul    ecx, sizeof.ARP_entry
3601 hidnplayr 646
        lea     esi, [eax + ecx]
3545 hidnplayr 647
        mov     ecx, sizeof.ARP_entry/2
3711 clevermous 648
        rep movsw
3545 hidnplayr 649
 
650
        xor     eax, eax
651
        ret
652
 
653
  .write:
654
        ; esi = pointer to buffer
3601 hidnplayr 655
        mov     edi, eax
3545 hidnplayr 656
        call    ARP_add_entry           ; out: eax = entry number, -1 on error
657
        ret
658
 
659
  .remove:
660
        ; ecx = # entry
3601 hidnplayr 661
        cmp     ecx, [ARP_entries_num + eax]
3545 hidnplayr 662
        jae     .error
663
        imul    ecx, sizeof.ARP_entry
664
        lea     esi, [ARP_table + ecx]
3601 hidnplayr 665
        mov     edi, eax
666
        shr     edi, 2
3545 hidnplayr 667
        call    ARP_del_entry
668
        ret
669
 
670
  .send_announce:
3601 hidnplayr 671
        mov     ebx, [NET_DRV_LIST + eax]
3545 hidnplayr 672
        mov     eax, [IP_LIST + eax]
673
        call    ARP_output_request      ; now send a gratuitous ARP
674
        ret
675