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 /* THE transmit packet */
34 uchar *NetArpWaitTxPacket;
35 int NetArpWaitTxPacketSize;
36 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
37 ulong NetArpWaitTimerStart;
42 /* XXX problem with bss workaround */
43 NetArpWaitPacketMAC = NULL;
44 NetArpWaitPacketIP = 0;
45 NetArpWaitReplyIP = 0;
46 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
47 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
48 NetArpWaitTxPacketSize = 0;
56 debug("ARP broadcast %d\n", NetArpWaitTry);
60 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
62 arp = (struct arp_hdr *) pkt;
64 arp->ar_hrd = htons(ARP_ETHER);
65 arp->ar_pro = htons(PROT_IP);
68 arp->ar_op = htons(ARPOP_REQUEST);
71 memcpy(&arp->ar_data[0], NetOurEther, 6);
73 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
74 /* dest ET addr = 0 */
75 memset(&arp->ar_data[10], '\0', 6);
76 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
77 (NetOurIP & NetOurSubnetMask)) {
78 if (NetOurGatewayIP == 0) {
79 puts("## Warning: gatewayip needed but not set\n");
80 NetArpWaitReplyIP = NetArpWaitPacketIP;
82 NetArpWaitReplyIP = NetOurGatewayIP;
85 NetArpWaitReplyIP = NetArpWaitPacketIP;
88 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
89 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
92 void ArpTimeoutCheck(void)
96 if (!NetArpWaitPacketIP)
101 /* check for arp timeout */
102 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
105 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
106 puts("\nARP Retry count exceeded; starting again\n");
110 NetArpWaitTimerStart = t;
116 void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
123 * We have to deal with two types of ARP packets:
124 * - REQUEST packets will be answered by sending our
125 * IP address - if we know it.
126 * - REPLY packates are expected only after we asked
127 * for the TFTP server's or the gateway's ethernet
128 * address; so if we receive such a packet, we set
129 * the server ethernet address
133 arp = (struct arp_hdr *)ip;
134 if (len < ARP_HDR_SIZE) {
135 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
138 if (ntohs(arp->ar_hrd) != ARP_ETHER)
140 if (ntohs(arp->ar_pro) != PROT_IP)
142 if (arp->ar_hln != 6)
144 if (arp->ar_pln != 4)
150 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
153 switch (ntohs(arp->ar_op)) {
155 /* reply with our IP address */
156 debug("Got ARP REQUEST, return our IP\n");
158 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
159 arp->ar_op = htons(ARPOP_REPLY);
160 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
161 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
162 memcpy(&arp->ar_data[0], NetOurEther, 6);
163 NetCopyIP(&arp->ar_data[6], &NetOurIP);
164 (void) eth_send((uchar *)et,
165 (pkt - (uchar *)et) + ARP_HDR_SIZE);
168 case ARPOP_REPLY: /* arp reply */
169 /* are we waiting for a reply */
170 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
173 #ifdef CONFIG_KEEP_SERVERADDR
174 if (NetServerIP == NetArpWaitPacketIP) {
176 sprintf(buf, "%pM", arp->ar_data);
177 setenv("serveraddr", buf);
181 tmp = NetReadIP(&arp->ar_data[6]);
183 /* matched waiting packet's address */
184 if (tmp == NetArpWaitReplyIP) {
185 debug("Got ARP REPLY, set eth addr (%pM)\n",
188 /* save address for later use */
189 memcpy(NetArpWaitPacketMAC,
190 &arp->ar_data[0], 6);
192 #ifdef CONFIG_NETCONSOLE
193 NetGetHandler()(0, 0, 0, 0, 0);
195 /* modify header, and transmit it */
196 memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)->
197 et_dest, NetArpWaitPacketMAC, 6);
198 (void) eth_send(NetArpWaitTxPacket,
199 NetArpWaitTxPacketSize);
201 /* no arp request pending now */
202 NetArpWaitPacketIP = 0;
203 NetArpWaitTxPacketSize = 0;
204 NetArpWaitPacketMAC = NULL;
209 debug("Unexpected ARP opcode 0x%x\n",