2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
15 #ifndef CONFIG_ARP_TIMEOUT
16 /* Milliseconds before trying ARP again */
17 # define ARP_TIMEOUT 5000UL
19 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
23 #ifndef CONFIG_NET_RETRY_COUNT
24 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
26 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
29 IPaddr_t NetArpWaitPacketIP;
30 IPaddr_t NetArpWaitReplyIP;
31 /* MAC address of waiting packet's destination */
32 uchar *NetArpWaitPacketMAC;
33 int NetArpWaitTxPacketSize;
34 ulong NetArpWaitTimerStart;
37 uchar *NetArpTxPacket; /* THE ARP transmit packet */
38 uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
42 /* XXX problem with bss workaround */
43 NetArpWaitPacketMAC = NULL;
44 NetArpWaitPacketIP = 0;
45 NetArpWaitReplyIP = 0;
46 NetArpWaitTxPacketSize = 0;
47 NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
48 NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
51 void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther,
58 debug("ARP broadcast %d\n", NetArpWaitTry);
62 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
65 arp = (struct arp_hdr *) pkt;
67 arp->ar_hrd = htons(ARP_ETHER);
68 arp->ar_pro = htons(PROT_IP);
69 arp->ar_hln = ARP_HLEN;
70 arp->ar_pln = ARP_PLEN;
71 arp->ar_op = htons(ARPOP_REQUEST);
73 memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */
74 NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */
75 memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */
76 NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */
78 NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
83 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
84 (NetOurIP & NetOurSubnetMask)) {
85 if (NetOurGatewayIP == 0) {
86 puts("## Warning: gatewayip needed but not set\n");
87 NetArpWaitReplyIP = NetArpWaitPacketIP;
89 NetArpWaitReplyIP = NetOurGatewayIP;
92 NetArpWaitReplyIP = NetArpWaitPacketIP;
95 arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP);
98 void ArpTimeoutCheck(void)
102 if (!NetArpWaitPacketIP)
107 /* check for arp timeout */
108 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
111 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
112 puts("\nARP Retry count exceeded; starting again\n");
116 NetArpWaitTimerStart = t;
122 void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
125 IPaddr_t reply_ip_addr;
130 * We have to deal with two types of ARP packets:
131 * - REQUEST packets will be answered by sending our
132 * IP address - if we know it.
133 * - REPLY packates are expected only after we asked
134 * for the TFTP server's or the gateway's ethernet
135 * address; so if we receive such a packet, we set
136 * the server ethernet address
140 arp = (struct arp_hdr *)ip;
141 if (len < ARP_HDR_SIZE) {
142 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
145 if (ntohs(arp->ar_hrd) != ARP_ETHER)
147 if (ntohs(arp->ar_pro) != PROT_IP)
149 if (arp->ar_hln != ARP_HLEN)
151 if (arp->ar_pln != ARP_PLEN)
157 if (NetReadIP(&arp->ar_tpa) != NetOurIP)
160 switch (ntohs(arp->ar_op)) {
162 /* reply with our IP address */
163 debug("Got ARP REQUEST, return our IP\n");
165 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
167 arp->ar_op = htons(ARPOP_REPLY);
168 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
169 NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
170 memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
171 NetCopyIP(&arp->ar_spa, &NetOurIP);
172 NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
175 case ARPOP_REPLY: /* arp reply */
176 /* are we waiting for a reply */
177 if (!NetArpWaitPacketIP)
180 #ifdef CONFIG_KEEP_SERVERADDR
181 if (NetServerIP == NetArpWaitPacketIP) {
183 sprintf(buf, "%pM", arp->ar_sha);
184 setenv("serveraddr", buf);
188 reply_ip_addr = NetReadIP(&arp->ar_spa);
190 /* matched waiting packet's address */
191 if (reply_ip_addr == NetArpWaitReplyIP) {
192 debug("Got ARP REPLY, set eth addr (%pM)\n",
195 /* save address for later use */
196 if (NetArpWaitPacketMAC != NULL)
197 memcpy(NetArpWaitPacketMAC,
198 &arp->ar_sha, ARP_HLEN);
200 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
203 /* set the mac address in the waiting packet's header
205 memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
206 &arp->ar_sha, ARP_HLEN);
207 NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
209 /* no arp request pending now */
210 NetArpWaitPacketIP = 0;
211 NetArpWaitTxPacketSize = 0;
212 NetArpWaitPacketMAC = NULL;
217 debug("Unexpected ARP opcode 0x%x\n",