blob: e58a074512d62d2e6f602fee18d729daab453697 [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;
33/* THE transmit packet */
34uchar *NetArpWaitTxPacket;
35int NetArpWaitTxPacketSize;
36uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
37ulong NetArpWaitTimerStart;
38int NetArpWaitTry;
39
40void ArpInit(void)
41{
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;
49}
50
51void ArpRequest(void)
52{
53 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000054 struct arp_hdr *arp;
Joe Hershberger00f33262012-05-23 07:59:09 +000055 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000056
57 debug("ARP broadcast %d\n", NetArpWaitTry);
58
59 pkt = NetTxPacket;
60
Joe Hershberger00f33262012-05-23 07:59:09 +000061 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
62 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000063
Joe Hershberger738853b2012-05-23 07:58:08 +000064 arp = (struct arp_hdr *) pkt;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000065
66 arp->ar_hrd = htons(ARP_ETHER);
67 arp->ar_pro = htons(PROT_IP);
Joe Hershberger674bb242012-05-23 07:58:17 +000068 arp->ar_hln = ARP_HLEN;
69 arp->ar_pln = ARP_PLEN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000070 arp->ar_op = htons(ARPOP_REQUEST);
71
72 /* source ET addr */
Joe Hershberger674bb242012-05-23 07:58:17 +000073 memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000074 /* source IP addr */
Joe Hershberger674bb242012-05-23 07:58:17 +000075 NetWriteIP(&arp->ar_spa, NetOurIP);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000076 /* dest ET addr = 0 */
Joe Hershberger674bb242012-05-23 07:58:17 +000077 memset(&arp->ar_tha, 0, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000078 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
79 (NetOurIP & NetOurSubnetMask)) {
80 if (NetOurGatewayIP == 0) {
81 puts("## Warning: gatewayip needed but not set\n");
82 NetArpWaitReplyIP = NetArpWaitPacketIP;
83 } else {
84 NetArpWaitReplyIP = NetOurGatewayIP;
85 }
86 } else {
87 NetArpWaitReplyIP = NetArpWaitPacketIP;
88 }
89
Joe Hershberger674bb242012-05-23 07:58:17 +000090 NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
Joe Hershberger00f33262012-05-23 07:59:09 +000091 (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000092}
93
94void ArpTimeoutCheck(void)
95{
96 ulong t;
97
98 if (!NetArpWaitPacketIP)
99 return;
100
101 t = get_timer(0);
102
103 /* check for arp timeout */
104 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
105 NetArpWaitTry++;
106
107 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
108 puts("\nARP Retry count exceeded; starting again\n");
109 NetArpWaitTry = 0;
110 NetStartAgain();
111 } else {
112 NetArpWaitTimerStart = t;
113 ArpRequest();
114 }
115 }
116}
117
Joe Hershbergercb487f52012-05-23 07:58:06 +0000118void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000119{
Joe Hershberger738853b2012-05-23 07:58:08 +0000120 struct arp_hdr *arp;
Joe Hershberger12567932012-05-23 07:58:16 +0000121 IPaddr_t reply_ip_addr;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000122 uchar *pkt;
Joe Hershberger00f33262012-05-23 07:59:09 +0000123 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000124
125 /*
126 * We have to deal with two types of ARP packets:
127 * - REQUEST packets will be answered by sending our
128 * IP address - if we know it.
129 * - REPLY packates are expected only after we asked
130 * for the TFTP server's or the gateway's ethernet
131 * address; so if we receive such a packet, we set
132 * the server ethernet address
133 */
134 debug("Got ARP\n");
135
Joe Hershberger738853b2012-05-23 07:58:08 +0000136 arp = (struct arp_hdr *)ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000137 if (len < ARP_HDR_SIZE) {
138 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
139 return;
140 }
141 if (ntohs(arp->ar_hrd) != ARP_ETHER)
142 return;
143 if (ntohs(arp->ar_pro) != PROT_IP)
144 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000145 if (arp->ar_hln != ARP_HLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000146 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000147 if (arp->ar_pln != ARP_PLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000148 return;
149
150 if (NetOurIP == 0)
151 return;
152
Joe Hershberger674bb242012-05-23 07:58:17 +0000153 if (NetReadIP(&arp->ar_tpa) != NetOurIP)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000154 return;
155
156 switch (ntohs(arp->ar_op)) {
157 case ARPOP_REQUEST:
158 /* reply with our IP address */
159 debug("Got ARP REQUEST, return our IP\n");
160 pkt = (uchar *)et;
Joe Hershberger00f33262012-05-23 07:59:09 +0000161 eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP);
162 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000163 arp->ar_op = htons(ARPOP_REPLY);
Joe Hershberger674bb242012-05-23 07:58:17 +0000164 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
165 NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
166 memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
167 NetCopyIP(&arp->ar_spa, &NetOurIP);
Joe Hershberger00f33262012-05-23 07:59:09 +0000168 (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000169 return;
170
171 case ARPOP_REPLY: /* arp reply */
172 /* are we waiting for a reply */
173 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
174 break;
175
176#ifdef CONFIG_KEEP_SERVERADDR
177 if (NetServerIP == NetArpWaitPacketIP) {
178 char buf[20];
Joe Hershberger674bb242012-05-23 07:58:17 +0000179 sprintf(buf, "%pM", arp->ar_sha);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000180 setenv("serveraddr", buf);
181 }
182#endif
183
Joe Hershberger674bb242012-05-23 07:58:17 +0000184 reply_ip_addr = NetReadIP(&arp->ar_spa);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000185
186 /* matched waiting packet's address */
Joe Hershberger12567932012-05-23 07:58:16 +0000187 if (reply_ip_addr == NetArpWaitReplyIP) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000188 debug("Got ARP REPLY, set eth addr (%pM)\n",
189 arp->ar_data);
190
191 /* save address for later use */
192 memcpy(NetArpWaitPacketMAC,
Joe Hershberger674bb242012-05-23 07:58:17 +0000193 &arp->ar_sha, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000194
195#ifdef CONFIG_NETCONSOLE
196 NetGetHandler()(0, 0, 0, 0, 0);
197#endif
198 /* modify header, and transmit it */
Joe Hershbergercb487f52012-05-23 07:58:06 +0000199 memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)->
Joe Hershberger674bb242012-05-23 07:58:17 +0000200 et_dest, NetArpWaitPacketMAC, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000201 (void) eth_send(NetArpWaitTxPacket,
202 NetArpWaitTxPacketSize);
203
204 /* no arp request pending now */
205 NetArpWaitPacketIP = 0;
206 NetArpWaitTxPacketSize = 0;
207 NetArpWaitPacketMAC = NULL;
208
209 }
210 return;
211 default:
212 debug("Unexpected ARP opcode 0x%x\n",
213 ntohs(arp->ar_op));
214 return;
215 }
216}