Subversion Repositories Kolibri OS

Rev

Rev 2614 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
  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.  
  19. $Revision: 2629 $
  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.  
  35. struct ARP_entry
  36.  
  37.        IP               dd ?
  38.        MAC              dp ?
  39.        Status           dw ?
  40.        TTL              dw ?
  41.  
  42. ends
  43.  
  44. struct ARP_header
  45.  
  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 ?
  55.  
  56. ends
  57.  
  58. align 4
  59. uglobal
  60.  
  61.         NumARP          dd ?
  62.  
  63.         ARP_table       rb ARP_TABLE_SIZE * sizeof.ARP_entry
  64.  
  65.         ARP_PACKETS_TX  rd MAX_NET_DEVICES
  66.         ARP_PACKETS_RX  rd MAX_NET_DEVICES
  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
  83.         mov     [NumARP], eax
  84.  
  85.         mov     edi, ARP_PACKETS_TX
  86.         mov     ecx, 2*MAX_NET_DEVICES
  87.         rep     stosd
  88.  
  89. }
  90.  
  91. ;---------------------------------------------------------------------------
  92. ;
  93. ; ARP_decrease_entry_ttls
  94. ;
  95. ;---------------------------------------------------------------------------
  96.  
  97. macro ARP_decrease_entry_ttls {
  98.  
  99. local   .loop
  100. local   .exit
  101.  
  102. ; The TTL field is decremented every second, and is deleted when it reaches 0.
  103. ; It is refreshed every time a packet is received.
  104. ; If the TTL field is 0xFFFF it is a static entry and is never deleted.
  105. ; The status field can be the following values:
  106. ; 0x0000  entry not used
  107. ; 0x0001  entry holds a valid mapping
  108. ; 0x0002  entry contains an IP address, awaiting ARP response
  109. ; 0x0003  No response received to ARP request.
  110. ; The last status value is provided to allow the network layer to delete
  111. ; a packet that is queued awaiting an ARP response
  112.  
  113.         mov     ecx, [NumARP]
  114.         test    ecx, ecx
  115.         jz      .exit
  116.  
  117.         mov     esi, ARP_table
  118.   .loop:
  119.         cmp     [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
  120.         je      .next
  121.  
  122.         dec     [esi + ARP_entry.TTL]
  123.         jz      .time_out
  124.  
  125.   .next:
  126.         add     esi, sizeof.ARP_entry
  127.         dec     ecx
  128.         jnz     .loop
  129.         jmp     .exit
  130.  
  131.   .time_out:
  132.         cmp     [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE
  133.         je      .response_timeout
  134.  
  135.         push    esi ecx
  136.         call    ARP_del_entry
  137.         pop     ecx esi
  138.  
  139.         jmp     .next
  140.  
  141.   .response_timeout:
  142.         mov     [esi + ARP_entry.Status], ARP_RESPONSE_TIMEOUT
  143.         mov     [esi + ARP_entry.TTL], 10
  144.  
  145.         jmp     .next
  146.  
  147.   .exit:
  148.  
  149. }
  150.  
  151.  
  152. ;-----------------------------------------------------------------
  153. ;
  154. ; ARP_input
  155. ;
  156. ;  IN:  Pointer to buffer in [esp]
  157. ;       size of buffer in [esp+4]
  158. ;       packet size (without ethernet header) in ecx
  159. ;       packet ptr in edx
  160. ;  OUT: /
  161. ;
  162. ;-----------------------------------------------------------------
  163. align 4
  164. ARP_input:
  165.  
  166.         DEBUGF  1,"ARP_input - start\n"
  167.         cmp     ecx, sizeof.ARP_header
  168.         jb      .exit
  169.  
  170. ;---------------------
  171. ; Handle Reply packets
  172.  
  173.         cmp     [edx + ARP_header.Opcode], ARP_REP_OPCODE
  174.         jne     .maybe_request
  175.  
  176.         DEBUGF  1,"ARP_input - it's a reply packet from %u.%u.%u.%u\n",\
  177.         [edx + ARP_header.SenderIP]:1, [edx + ARP_header.SenderIP+1]:1,\
  178.         [edx + ARP_header.SenderIP+2]:1, [edx + ARP_header.SenderIP+3]:1
  179.  
  180.         mov     ecx, [NumARP]
  181.         test    ecx, ecx
  182.         jz      .exit
  183.  
  184.         mov     eax, [edx + ARP_header.SenderIP]
  185.         mov     esi, ARP_table
  186.  
  187.   .loop:
  188.         cmp     [esi + ARP_entry.IP], eax
  189.         je      .gotit
  190.         add     esi, sizeof.ARP_entry
  191.         dec     ecx
  192.         jnz     .loop
  193.  
  194.         jmp     .exit
  195.  
  196.   .gotit:
  197.         DEBUGF  1,"ARP_input - found matching entry\n"
  198.  
  199.         cmp     [esi + ARP_entry.TTL], ARP_STATIC_ENTRY         ; if it is a static entry, dont touch it
  200.         je      .exit
  201.  
  202.         DEBUGF  1,"ARP_input - updating entry\n"
  203.  
  204.         mov     [esi + ARP_entry.Status], ARP_VALID_MAPPING
  205.         mov     [esi + ARP_entry.TTL], ARP_ENTRY_TTL
  206.  
  207.         mov     eax, dword [edx + ARP_header.SenderMAC]
  208.         mov     dword [esi+ARP_entry.MAC], eax
  209.         mov     cx, word [edx + ARP_header.SenderMAC + 4]
  210.         mov     word [esi+ARP_entry.MAC+4], cx
  211.  
  212.         jmp     .exit
  213.  
  214.  
  215. ;-----------------------
  216. ; Handle Request packets
  217.  
  218.   .maybe_request:
  219.         cmp     [edx + ARP_header.Opcode], ARP_REQ_OPCODE
  220.         jne     .exit
  221.  
  222.         call    NET_ptr_to_num
  223.         cmp     edi, -1
  224.         jz      .exit
  225.         DEBUGF  1,"ARP Request packet through device: %u\n", edi
  226.         inc     [ARP_PACKETS_RX+4*edi]
  227.  
  228.         mov     eax, [IP_LIST+4*edi]
  229.         cmp     eax, [edx + ARP_header.TargetIP]                ; Is it looking for my IP address?
  230.         jne     .exit
  231.  
  232.         push    eax
  233.         push    edi
  234.  
  235. ; OK, it is a request for one of our MAC addresses.
  236. ; Build the frame and send it. We can reuse the buffer.  (faster then using ARP_create_packet)
  237.  
  238.         lea     esi, [edx + ARP_header.SenderMAC]
  239.         lea     edi, [edx + ARP_header.TargetMAC]
  240.         movsd                                                   ; Move Sender Mac to Dest MAC
  241.         movsw                                                   ;
  242.         movsd                                                   ; Move sender IP to Dest IP
  243.  
  244.         pop     esi
  245.         mov     esi, [NET_DRV_LIST + 4*esi]
  246.         lea     esi, [esi + ETH_DEVICE.mac]
  247.         lea     edi, [edx + ARP_header.SenderMAC]
  248.         movsd                                                   ; Copy MAC address from in MAC_LIST
  249.         movsw                                                   ;
  250.         pop     eax
  251.         stosd                                                   ; Write our IP
  252.  
  253.         mov     [edx + ARP_header.Opcode], ARP_REP_OPCODE
  254.  
  255. ; Now, Fill in ETHERNET header
  256.  
  257.         mov     edi, [esp]
  258.         lea     esi, [edx + ARP_header.TargetMAC]
  259.         movsd
  260.         movsw
  261.         lea     esi, [edx + ARP_header.SenderMAC]
  262.         movsd
  263.         movsw
  264. ;        mov     ax , ETHER_ARP                                 ; It's already there, I'm sure of it!
  265. ;        stosw
  266.  
  267.         DEBUGF  1,"ARP_input - Sending reply \n"
  268.  
  269.         call    [ebx + NET_DEVICE.transmit]
  270.         ret
  271.  
  272.   .exit:
  273.         call    kernel_free
  274.         add     esp, 4                                          ; pop (balance stack)
  275.  
  276.         DEBUGF  1,"ARP_input - exiting\n"
  277.         ret
  278.  
  279.  
  280. ;---------------------------------------------------------------------------
  281. ;
  282. ; ARP_output_request
  283. ;
  284. ; IN:  ip in eax
  285. ; OUT: /
  286. ;
  287. ;---------------------------------------------------------------------------
  288. align 4
  289. ARP_output_request:
  290.  
  291.         DEBUGF 1,"Create ARP Packet\n"
  292.  
  293.         call    IPv4_dest_to_dev
  294.         push    eax                             ; DestIP
  295.         pushd   [IP_LIST+edi]                   ; SenderIP
  296.  
  297.         mov     ebx, [NET_DRV_LIST+edi]         ; device ptr
  298.  
  299.         lea     eax, [ebx + ETH_DEVICE.mac]     ; local device mac
  300.         mov     edx, ETH_BROADCAST              ; broadcast mac
  301.         mov     ecx, sizeof.ARP_header
  302.         mov     di, ETHER_ARP
  303.         call    ETH_output
  304.         jz      .exit
  305.  
  306.         mov     ecx, eax
  307.  
  308.         mov     [edi + ARP_header.HardwareType], 0x0100         ; Ethernet
  309.         mov     [edi + ARP_header.ProtocolType], 0x0008         ; IP
  310.         mov     [edi + ARP_header.HardwareSize], 6              ; MAC-addr length
  311.         mov     [edi + ARP_header.ProtocolSize], 4              ; IP-addr length
  312.         mov     [edi + ARP_header.Opcode], ARP_REQ_OPCODE       ; Request
  313.  
  314.         add     edi, ARP_header.SenderMAC
  315.  
  316.         lea     esi, [ebx + ETH_DEVICE.mac]     ; SenderMac
  317.         movsw                                   ;
  318.         movsd                                   ;
  319.         pop     eax                             ; SenderIP
  320.         stosd                                   ;
  321.  
  322.         mov     eax, -1                         ; DestMac
  323.         stosd                                   ;
  324.         stosw                                   ;
  325.         pop     eax                             ; DestIP
  326.         stosd                                   ;
  327.  
  328.         DEBUGF 1,"ARP Packet for device %x created successfully\n", ebx
  329.  
  330.         push    edx ecx
  331.         call    [ebx + NET_DEVICE.transmit]
  332.         ret
  333.  
  334.   .exit:
  335.         add     esp, 4+4
  336.         DEBUGF  1,"Create ARP Packet - failed\n"
  337.         sub     eax, eax
  338.         ret
  339.  
  340.  
  341. ;-----------------------------------------------------------------
  342. ;
  343. ; ARP_add_entry (or update)
  344. ;
  345. ; IN:  esi = ptr to entry (can easily be made on the stack)
  346. ; OUT: eax = entry #, -1 on error
  347. ;
  348. ;-----------------------------------------------------------------   ; TODO: use a mutex
  349. align 4
  350. ARP_add_entry:
  351.  
  352.         DEBUGF 1,"ARP add entry: "
  353.  
  354.         mov     ecx, [NumARP]
  355.         test    ecx, ecx                ; first entry?
  356.         jz      .add
  357.         cmp     ecx, ARP_TABLE_SIZE     ; list full ?
  358.         jae     .error
  359.  
  360.         mov     eax, dword [esi + ARP_entry.MAC]
  361.         mov     bx , word [esi + ARP_entry.MAC + 4]
  362.         mov     edi, ARP_table
  363.  
  364.   .loop:
  365.         cmp     dword [edi + ARP_entry.MAC], eax        ; Check for duplicate MAC's
  366.         jne     .maybe_next                             ;
  367.         cmp     word [edi + ARP_entry.MAC + 4], bx      ;
  368.         jne     .maybe_next                             ;
  369.  
  370.         cmp     [edi + ARP_entry.TTL], ARP_STATIC_ENTRY
  371.         jne     .notstatic
  372.         cmp     [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
  373.         jne     .error
  374.   .notstatic:
  375.  
  376.         neg     ecx
  377.         add     ecx, [NumARP]
  378.         jmp     .add
  379.  
  380.   .maybe_next:
  381.         add     esi, sizeof.ARP_entry
  382.         loop    .loop
  383.  
  384.         mov     ecx, [NumARP]
  385.   .add:
  386.         push    ecx
  387.         imul    ecx, sizeof.ARP_entry
  388.         lea     edi, [ecx + ARP_table]
  389.         mov     ecx, sizeof.ARP_entry/2
  390.         rep     movsw
  391.  
  392.         lea     esi, [edi - sizeof.ARP_entry]
  393.         inc     [NumARP]
  394.         pop     eax
  395.         DEBUGF 1,"New entry created: %u\n", eax
  396.  
  397.   .exit:
  398.         DEBUGF 1,"Exiting\n"
  399.         ret
  400.  
  401.   .error:
  402.         DEBUGF 1,"error! \n"
  403.         mov     eax, -1
  404.         ret
  405.  
  406.  
  407. ;-----------------------------------------------------------------
  408. ;
  409. ; ARP_del_entry
  410. ;
  411. ; IN:  esi = ptr to arp entry
  412. ; OUT: /
  413. ;
  414. ;-----------------------------------------------------------------
  415. align 4
  416. ARP_del_entry:
  417.  
  418.         DEBUGF 1,"ARP del entry %x, total entrys: %u\n", esi, [NumARP]
  419.  
  420.         mov     ecx, ARP_table + (ARP_TABLE_SIZE - 1) * sizeof.ARP_entry
  421.         sub     ecx, esi
  422.         shr     ecx, 1
  423.  
  424.         mov     edi, esi
  425.         lea     esi, [edi + sizeof.ARP_entry]
  426.         rep     movsw
  427.  
  428.         dec     [NumARP]
  429.         DEBUGF 1,"ARP entry deleted\n"
  430.  
  431.         ret
  432.  
  433.  
  434.  
  435.  
  436.  
  437. ;-----------------------------------------------------------------
  438. ;
  439. ; ARP_IP_to_MAC
  440. ;
  441. ;  This function translates an IP address to a MAC address
  442. ;
  443. ;  IN:  eax = IPv4 address
  444. ;  OUT: eax = -1 on error, -2 means request send
  445. ;      else, ax = first two bytes of mac (high 16 bits of eax will be 0)
  446. ;       ebx = last four bytes of mac
  447. ;
  448. ;-----------------------------------------------------------------
  449. align 4
  450. ARP_IP_to_MAC:
  451.  
  452.         DEBUGF 1,"ARP_IP_to_MAC: %u.%u", al, ah
  453.         rol     eax, 16
  454.         DEBUGF 1,".%u.%u\n", al, ah
  455.         rol     eax, 16
  456.  
  457.         cmp     eax, 0xffffffff
  458.         je      .broadcast
  459.  
  460. ; if ((Remote IP & subnet_mask) == (local IP & subnet_mask ))
  461. ; destination is on same subnet
  462. ; else, destination is remote and must use a gateway
  463.  
  464.         call    IPv4_dest_to_dev
  465.         mov     ebx, [IP_LIST + edi]
  466.         and     ebx, [SUBNET_LIST + edi]
  467.  
  468.         mov     ecx, eax
  469.         and     ecx, [SUBNET_LIST + edi]
  470.  
  471.         cmp     ecx, ebx
  472.         je      .local
  473.  
  474.         mov     eax, [GATEWAY_LIST + edi]
  475.         DEBUGF  1,"requested IP is not on subnet, using default gateway\n"
  476.  
  477. ;--------------------------------
  478. ; Try to find the IP in ARP_table
  479.  
  480.   .local:
  481.         mov     ecx, [NumARP]
  482.         test    ecx, ecx
  483.         jz      .not_in_list
  484.         mov     esi, ARP_table + ARP_entry.IP
  485.   .scan_loop:
  486.         cmp     [esi], eax
  487.         je      .found_it
  488.         add     esi, sizeof.ARP_entry
  489.         loop    .scan_loop
  490.  
  491.   .not_in_list:
  492.         DEBUGF 1,"IP not found on list, preparing for ARP request\n"
  493.  
  494. ;--------------------
  495. ; Send an ARP request
  496.  
  497.         push    eax                     ; save IP for ARP_output_request
  498.  
  499. ; Now create the ARP entry
  500.         pushw   ARP_REQUEST_TTL         ; TTL
  501.         pushw   ARP_AWAITING_RESPONSE   ; status
  502.         pushd   0                       ; mac
  503.         pushw   0
  504.         pushd   eax                     ; ip
  505.         mov     esi, esp
  506.         call    ARP_add_entry
  507.         add     esp, sizeof.ARP_entry
  508.  
  509.         cmp     eax, -1
  510.         je      .full
  511.  
  512. ; And send a request
  513.         pop     eax
  514.         call    ARP_output_request      ; IP in eax
  515. ;; TODO: check if driver could transmit packet
  516.  
  517.         mov     eax, -2                 ; request send
  518.         ret
  519.  
  520.   .found_it:
  521.         DEBUGF  1,"found IP in ARPTable\n"
  522.         cmp     [esi + ARP_entry.Status], ARP_VALID_MAPPING
  523.         jne     .invalid
  524.  
  525.         movzx   eax, word [esi + ARP_entry.MAC]
  526.         mov     ebx, dword[esi + ARP_entry.MAC+2]
  527.         ret
  528.  
  529.   .invalid:
  530.         mov     eax, -1
  531.         ret
  532.  
  533.   .full:
  534.         DEBUGF  1,"ARP table is full!\n"
  535.         pop     eax
  536.         mov     eax, -1
  537.         ret
  538.  
  539.   .broadcast:
  540.         mov     eax, 0x0000ffff
  541.         mov     ebx, 0xffffffff
  542.         ret
  543.  
  544.  
  545. ;-----------------------------------------------------------------
  546. ;
  547. ; ARP_API
  548. ;
  549. ; This function is called by system function 75
  550. ;
  551. ; IN:  subfunction number in bl
  552. ;      device number in bh
  553. ;      ecx, edx, .. depends on subfunction
  554. ;
  555. ; OUT:  ?
  556. ;
  557. ;-----------------------------------------------------------------
  558. align 4
  559. ARP_api:
  560.  
  561.         movzx   eax, bh
  562.         shl     eax, 2
  563.  
  564.         and     ebx, 0xff
  565.         cmp     ebx, .number
  566.         ja      .error
  567.         jmp     dword [.table + 4*ebx]
  568.  
  569.   .table:
  570.         dd      .packets_tx     ; 0
  571.         dd      .packets_rx     ; 1
  572.         dd      .entries        ; 2
  573.         dd      .read           ; 3
  574.         dd      .write          ; 4
  575.         dd      .remove         ; 5
  576.   .number = ($ - .table) / 4 - 1
  577.  
  578.   .error:
  579.         mov     eax, -1
  580.         ret
  581.  
  582.   .packets_tx:
  583.         mov     eax, [ARP_PACKETS_TX + eax]
  584.         ret
  585.  
  586.   .packets_rx:
  587.         mov     eax, [ARP_PACKETS_RX + eax]
  588.         ret
  589.  
  590.   .entries:
  591.         mov     eax, [NumARP]
  592.         ret
  593.  
  594.   .read:
  595.         cmp     ecx, [NumARP]
  596.         jae     .error
  597.         ; edi = pointer to buffer
  598.         ; ecx = # entry
  599.         imul    ecx, sizeof.ARP_entry
  600.         add     ecx, ARP_table
  601.         mov     esi, ecx
  602.         mov     ecx, sizeof.ARP_entry/2
  603.         rep     movsw
  604.  
  605.         xor     eax, eax
  606.         ret
  607.  
  608.   .write:
  609.         ; esi = pointer to buffer
  610.         call    ARP_add_entry        ;out: eax = entry number, -1 on error
  611.         ret
  612.  
  613.   .remove:
  614.         ; ecx = # entry
  615.         cmp     ecx, [NumARP]
  616.         jae     .error
  617.         imul    ecx, sizeof.ARP_entry
  618.         lea     esi, [ARP_table + ecx]
  619.         call    ARP_del_entry
  620.         ret
  621.  
  622.