blob: 908ebf53165a334e25096be1719241db138700e0 [file] [log] [blame]
Joe Hershbergerd280d3f2012-05-23 07:58:01 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11#include <common.h>
12
13#include "arp.h"
14
15#ifndef CONFIG_ARP_TIMEOUT
16/* Milliseconds before trying ARP again */
17# define ARP_TIMEOUT 5000UL
18#else
19# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
20#endif
21
22
23#ifndef CONFIG_NET_RETRY_COUNT
24# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
25#else
26# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
27#endif
28
29IPaddr_t NetArpWaitPacketIP;
30IPaddr_t NetArpWaitReplyIP;
31/* MAC address of waiting packet's destination */
32uchar *NetArpWaitPacketMAC;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000033int NetArpWaitTxPacketSize;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000034ulong NetArpWaitTimerStart;
35int NetArpWaitTry;
36
Joe Hershbergere94070c2012-05-23 07:59:24 +000037uchar *NetArpTxPacket; /* THE ARP transmit packet */
38uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
39
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000040void ArpInit(void)
41{
42 /* XXX problem with bss workaround */
43 NetArpWaitPacketMAC = NULL;
44 NetArpWaitPacketIP = 0;
45 NetArpWaitReplyIP = 0;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000046 NetArpWaitTxPacketSize = 0;
Joe Hershbergere94070c2012-05-23 07:59:24 +000047 NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
48 NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000049}
50
Joe Hershberger22804182012-05-23 08:00:11 +000051void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther,
52 IPaddr_t targetIP)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000053{
54 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000055 struct arp_hdr *arp;
Joe Hershberger00f33262012-05-23 07:59:09 +000056 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000057
58 debug("ARP broadcast %d\n", NetArpWaitTry);
59
Joe Hershbergere94070c2012-05-23 07:59:24 +000060 pkt = NetArpTxPacket;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000061
Joe Hershberger00f33262012-05-23 07:59:09 +000062 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
63 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000064
Joe Hershberger738853b2012-05-23 07:58:08 +000065 arp = (struct arp_hdr *) pkt;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000066
67 arp->ar_hrd = htons(ARP_ETHER);
68 arp->ar_pro = htons(PROT_IP);
Joe Hershberger674bb242012-05-23 07:58:17 +000069 arp->ar_hln = ARP_HLEN;
70 arp->ar_pln = ARP_PLEN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000071 arp->ar_op = htons(ARPOP_REQUEST);
72
Joe Hershberger22804182012-05-23 08:00:11 +000073 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 */
77
78 NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
79}
80
81void ArpRequest(void)
82{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000083 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
84 (NetOurIP & NetOurSubnetMask)) {
85 if (NetOurGatewayIP == 0) {
86 puts("## Warning: gatewayip needed but not set\n");
87 NetArpWaitReplyIP = NetArpWaitPacketIP;
88 } else {
89 NetArpWaitReplyIP = NetOurGatewayIP;
90 }
91 } else {
92 NetArpWaitReplyIP = NetArpWaitPacketIP;
93 }
94
Joe Hershberger22804182012-05-23 08:00:11 +000095 arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000096}
97
98void ArpTimeoutCheck(void)
99{
100 ulong t;
101
102 if (!NetArpWaitPacketIP)
103 return;
104
105 t = get_timer(0);
106
107 /* check for arp timeout */
108 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
109 NetArpWaitTry++;
110
111 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
112 puts("\nARP Retry count exceeded; starting again\n");
113 NetArpWaitTry = 0;
114 NetStartAgain();
115 } else {
116 NetArpWaitTimerStart = t;
117 ArpRequest();
118 }
119 }
120}
121
Joe Hershbergercb487f52012-05-23 07:58:06 +0000122void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000123{
Joe Hershberger738853b2012-05-23 07:58:08 +0000124 struct arp_hdr *arp;
Joe Hershberger12567932012-05-23 07:58:16 +0000125 IPaddr_t reply_ip_addr;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000126 uchar *pkt;
Joe Hershberger00f33262012-05-23 07:59:09 +0000127 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000128
129 /*
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
137 */
138 debug("Got ARP\n");
139
Joe Hershberger738853b2012-05-23 07:58:08 +0000140 arp = (struct arp_hdr *)ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000141 if (len < ARP_HDR_SIZE) {
142 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
143 return;
144 }
145 if (ntohs(arp->ar_hrd) != ARP_ETHER)
146 return;
147 if (ntohs(arp->ar_pro) != PROT_IP)
148 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000149 if (arp->ar_hln != ARP_HLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000150 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000151 if (arp->ar_pln != ARP_PLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000152 return;
153
154 if (NetOurIP == 0)
155 return;
156
Joe Hershberger674bb242012-05-23 07:58:17 +0000157 if (NetReadIP(&arp->ar_tpa) != NetOurIP)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000158 return;
159
160 switch (ntohs(arp->ar_op)) {
161 case ARPOP_REQUEST:
162 /* reply with our IP address */
163 debug("Got ARP REQUEST, return our IP\n");
164 pkt = (uchar *)et;
Joe Hershbergere7111012012-05-23 07:59:16 +0000165 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
Joe Hershberger00f33262012-05-23 07:59:09 +0000166 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000167 arp->ar_op = htons(ARPOP_REPLY);
Joe Hershberger674bb242012-05-23 07:58:17 +0000168 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);
Joe Hershbergeradf5d932012-05-23 07:59:13 +0000172 NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000173 return;
174
175 case ARPOP_REPLY: /* arp reply */
176 /* are we waiting for a reply */
Joe Hershbergerf1d2d282012-05-23 07:59:20 +0000177 if (!NetArpWaitPacketIP)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000178 break;
179
180#ifdef CONFIG_KEEP_SERVERADDR
181 if (NetServerIP == NetArpWaitPacketIP) {
182 char buf[20];
Joe Hershberger674bb242012-05-23 07:58:17 +0000183 sprintf(buf, "%pM", arp->ar_sha);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000184 setenv("serveraddr", buf);
185 }
186#endif
187
Joe Hershberger674bb242012-05-23 07:58:17 +0000188 reply_ip_addr = NetReadIP(&arp->ar_spa);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000189
190 /* matched waiting packet's address */
Joe Hershberger12567932012-05-23 07:58:16 +0000191 if (reply_ip_addr == NetArpWaitReplyIP) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000192 debug("Got ARP REPLY, set eth addr (%pM)\n",
193 arp->ar_data);
194
195 /* save address for later use */
Joe Hershbergerf1d2d282012-05-23 07:59:20 +0000196 if (NetArpWaitPacketMAC != NULL)
197 memcpy(NetArpWaitPacketMAC,
198 &arp->ar_sha, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000199
Joe Hershbergerece223b2012-05-23 07:59:15 +0000200 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
201 0, len);
202
Joe Hershbergere94070c2012-05-23 07:59:24 +0000203 /* set the mac address in the waiting packet's header
204 and transmit it */
205 memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
206 &arp->ar_sha, ARP_HLEN);
207 NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000208
209 /* no arp request pending now */
210 NetArpWaitPacketIP = 0;
211 NetArpWaitTxPacketSize = 0;
212 NetArpWaitPacketMAC = NULL;
213
214 }
215 return;
216 default:
217 debug("Unexpected ARP opcode 0x%x\n",
218 ntohs(arp->ar_op));
219 return;
220 }
221}