Web 51 - ARP |
|
ARP - Address Resolution Protocol is described in RFC826 (An Ethernet Address Resolution Protocol). ARP is used in Ethernet networks to determine the MAC address from a known IP address. The method is very simple and truly distributed - each node responds only to requests for its own MAC address. Whenever a node needs to find a MAC address from an IP address, it sends a broadcast message to all nodes (address 0xFFFFFFFFFFFF) containing the IP address in question and a request for the respective MAC address. The other node which detects its own IP address in such a message replies with a packet containing its MAC address. For correct operation, Web51 needs to implement both parts - it must be able to receive and reply to ARP requests, as well as ask for another station's MAC address and process the reply. Algorithms implementing ARP are detailed below.
void ProcessARP (void) { if(rx_eth_pkt.pkt.arp.TIPA == flash_my_ip) { switch (rx_eth_pkt.pkt.arp.Op) { case ARPT_RQ:ARP request received; send a reply.
tx_eth_pkt.hdr.pktDest = EthRcvHdr.pktSrc; tx_eth_pkt.hdr.pktSrc = flash_my_ether; tx_eth_pkt.hdr.pktType = 0x0806; tx_eth_pkt.pkt.arp.HwType = 0x0001; tx_eth_pkt.pkt.arp.PrType = 0x0800; tx_eth_pkt.pkt.arp.HwLen = 6; tx_eth_pkt.pkt.arp.PrLen = 4; tx_eth_pkt.pkt.arp.Op = ARPT_REPLY; //ARP 0x0002=reply tx_eth_pkt.pkt.arp.SHwA = flash_my_ether; //senders hardware address tx_eth_pkt.pkt.arp.SIPA = flash_my_ip; //senders IP address tx_eth_pkt.pkt.arp.THwA = rx_eth_pkt.pkt.arp.SHwA; //target hardware address tx_eth_pkt.pkt.arp.TIPA = rx_eth_pkt.pkt.arp.SIPA; //target IP address xmit_frame(sizeof(rx_eth_pkt.hdr) + sizeof(rx_eth_pkt.pkt.arp)); return; case ARPT_REPLY:Reply to an ARP request was received, extract and store the MAC address.
if (rx_eth_pkt.pkt.arp.SIPA == ip_gateway) {Reply with gateway MAC address was received.
mac_gateway = rx_eth_pkt.pkt.arp.SHwA; return; if (rx_eth_pkt.pkt.arp.SIPA == IPsrcAddr23) {Reply with MAC address of "telnet" target was received.
MacSrcAddr23 = rx_eth_pkt.pkt.arp.SHwA; return; } default: return; } } return;
Construct and send a request for gateway MAC address.
void MakeArpGwRq (void) { tx_eth_pkt.hdr.pktDest = eth_brdcast; //Broadcast Address tx_eth_pkt.hdr.pktSrc = flash_my_ether; //packet source address tx_eth_pkt.hdr.pktType = 0x0806; //ARP tx_eth_pkt.pkt.arp.HwType = 0x0001; //hardware type tx_eth_pkt.pkt.arp.PrType = 0x0800; //protocol type tx_eth_pkt.pkt.arp.HwLen = 6; //hardware address length tx_eth_pkt.pkt.arp.PrLen = 4; //protocol address length tx_eth_pkt.pkt.arp.Op = ARPT_RQ; //ARP operation (1=request, 2=reply) tx_eth_pkt.pkt.arp.SHwA = flash_my_ether; //senders hardware address tx_eth_pkt.pkt.arp.SIPA = flash_my_ip; //senders IP address tx_eth_pkt.pkt.arp.THwA = eth_zero; //target hardware address tx_eth_pkt.pkt.arp.TIPA = flash_ip_gateway; //target IP address xmit_frame(sizeof(rx_eth_pkt.hdr) + sizeof(rx_eth_pkt.pkt.arp)); return; }Construct and send a request for MAC of a "telnet" target.
void MakeArpPoiRq (void) { tx_eth_pkt.hdr.pktDest = eth_brdcast; //Broadcast Address tx_eth_pkt.hdr.pktSrc = flash_my_ether; //packet source address tx_eth_pkt.hdr.pktType = 0x0806; //ARP tx_eth_pkt.pkt.arp.HwType = 0x0001; //hardware type tx_eth_pkt.pkt.arp.PrType = 0x0800; //protocol type tx_eth_pkt.pkt.arp.HwLen = 6; //hardware address length tx_eth_pkt.pkt.arp.PrLen = 4; //protocol address length tx_eth_pkt.pkt.arp.Op = ARPT_RQ; //ARP operation (1=request, 2=reply) tx_eth_pkt.pkt.arp.SHwA = flash_my_ether; //senders hardware address tx_eth_pkt.pkt.arp.SIPA = flash_my_ip; //senders IP address tx_eth_pkt.pkt.arp.THwA = eth_zero; //target hardware address tx_eth_pkt.pkt.arp.TIPA = flash_ip_point; //target IP address xmit_frame(sizeof(rx_eth_pkt.hdr) + sizeof(rx_eth_pkt.pkt.arp)); return; }
Previous algorithms may look complicated at the first glance; however, in most cases they simply fill the transmitted packet in with constant, or "almost" constant values. To take advantage of this, the packet is pre-constructed in the processor's EEPROM. Constant portions are initialized at compile-time and then downloaded into the processor together with firmware; "almost" constant parts are initialized whenever the processor is reset or configuration is cloned -- required parameters (IP/MAC addresses) are copied into respective fields of pre-constructed packets. Following example details the transmission of an ARP request for gateway MAC address.First, a peek into the processor's EEPROM.
arpgwreq:.word 0xFFFF,0xFFFF,0xFFFF ;PKT_DEST (Broadcast Address) my3eth: .ascii "??????" ;PKT_SRC = myether .word 0x0806 ;PKT_TYPE ;0806 = ARP .word 0x0001 ;ARP_HWTYPE ;hardware type 0001 .word 0x0800 ;ARP_PRTYPE ;protocol type 0800 .byte 6 ;ARP_HWLEN ;hardware address length 06 .byte 4 ;ARP_PRLEN ;protocol address length 04 .word ARPT_RQ ;ARP operation (1=request, 2=reply) my4eth: .ascii "??????" ;senders hardware address = myether my2ip: .ascii "????" ;senders IP address = myip .word 0x0000,0x0000,0x0000 ;target hardware address gw1ip: .ascii "????" ;target IP address sizeofarpgwreq equ ($ - arpgwreq)All '?'s are filled in during cloning with the real MAC address of Web 51 (my3eth, my4eth), IP address of Web 51 (my2ip), or gateway IP address (gwlip).
The actual implementation of gateway MAC address request is very simple - all that is needed is to transmit the pre-constructed packet.
ProcessArpGwRq: mov r7,#0 ; use service stack lcall changeStack clr flagArpGwRq LCALL pcode .pcode pe2x PKT_DEST, arpgwreq, #BYTE sizeofarppointreq .pcode xmit_frame #SIZE_OF_ETH_PKT_HDR+SIZE_OF_ARP .byte 0 RETConsistent use of pre-constructed packets greatly improves processing speed and therefore shortens the response time of a Web 51 server.
| Web51 description | News | FAQ | ORDER FORM | DOWNLOAD | Links |
| (c)Copyright 2000 - 2002, HW server & Radek Benedikt
Web51@HW.cz, Web51.HW.cz |
|