Subversion Repositories Kolibri OS

Rev

Rev 5596 | Details | Compare with Previous | Last modification | View Log | RSS feed

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