blob: c613609b2d0162e3745ca6e57ce86dcaab3436d3 [file] [log] [blame]
Joe Hershbergeraae05082012-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
Wolfgang Denkdb065922014-09-30 10:44:01 +02009 * SPDX-License-Identifier: GPL-2.0
Joe Hershbergeraae05082012-05-23 07:58:01 +000010 */
11
12#include <common.h>
13
14#include "arp.h"
15
16#ifndef CONFIG_ARP_TIMEOUT
17/* Milliseconds before trying ARP again */
18# define ARP_TIMEOUT 5000UL
19#else
20# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
21#endif
22
23
24#ifndef CONFIG_NET_RETRY_COUNT
25# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
26#else
27# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
28#endif
29
Joe Hershberger5874dec2015-04-08 01:41:01 -050030struct in_addr net_arp_wait_packet_ip;
31static struct in_addr net_arp_wait_reply_ip;
Joe Hershbergeraae05082012-05-23 07:58:01 +000032/* MAC address of waiting packet's destination */
33uchar *NetArpWaitPacketMAC;
Joe Hershbergeraae05082012-05-23 07:58:01 +000034int NetArpWaitTxPacketSize;
Joe Hershbergeraae05082012-05-23 07:58:01 +000035ulong NetArpWaitTimerStart;
36int NetArpWaitTry;
37
Kim Phillips40c2c032012-10-29 13:34:33 +000038static uchar *NetArpTxPacket; /* THE ARP transmit packet */
39static uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
Joe Hershbergerde8205a2012-05-23 07:59:24 +000040
Joe Hershbergeraae05082012-05-23 07:58:01 +000041void ArpInit(void)
42{
43 /* XXX problem with bss workaround */
44 NetArpWaitPacketMAC = NULL;
Joe Hershberger5874dec2015-04-08 01:41:01 -050045 net_arp_wait_packet_ip.s_addr = 0;
46 net_arp_wait_reply_ip.s_addr = 0;
Joe Hershbergeraae05082012-05-23 07:58:01 +000047 NetArpWaitTxPacketSize = 0;
Joe Hershbergerde8205a2012-05-23 07:59:24 +000048 NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
49 NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
Joe Hershbergeraae05082012-05-23 07:58:01 +000050}
51
Joe Hershberger5874dec2015-04-08 01:41:01 -050052void arp_raw_request(struct in_addr source_ip, const uchar *targetEther,
53 struct in_addr target_ip)
Joe Hershbergeraae05082012-05-23 07:58:01 +000054{
55 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000056 struct arp_hdr *arp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000057 int eth_hdr_size;
Joe Hershbergeraae05082012-05-23 07:58:01 +000058
Joe Hershberger05a377b2012-05-23 08:01:04 +000059 debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry);
Joe Hershbergeraae05082012-05-23 07:58:01 +000060
Joe Hershbergerde8205a2012-05-23 07:59:24 +000061 pkt = NetArpTxPacket;
Joe Hershbergeraae05082012-05-23 07:58:01 +000062
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050063 eth_hdr_size = NetSetEther(pkt, net_bcast_ethaddr, PROT_ARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000064 pkt += eth_hdr_size;
Joe Hershbergeraae05082012-05-23 07:58:01 +000065
Joe Hershbergerde15d0932012-05-23 07:58:08 +000066 arp = (struct arp_hdr *) pkt;
Joe Hershbergeraae05082012-05-23 07:58:01 +000067
68 arp->ar_hrd = htons(ARP_ETHER);
69 arp->ar_pro = htons(PROT_IP);
Joe Hershberger2f787492012-05-23 07:58:17 +000070 arp->ar_hln = ARP_HLEN;
71 arp->ar_pln = ARP_PLEN;
Joe Hershbergeraae05082012-05-23 07:58:01 +000072 arp->ar_op = htons(ARPOP_REQUEST);
73
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050074 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050075 net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
Joe Hershbergerc07e8102012-05-23 08:00:11 +000076 memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050077 net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
Joe Hershbergerc07e8102012-05-23 08:00:11 +000078
79 NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
80}
81
82void ArpRequest(void)
83{
Joe Hershberger5874dec2015-04-08 01:41:01 -050084 if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
85 (net_ip.s_addr & net_netmask.s_addr)) {
86 if (net_gateway.s_addr == 0) {
Joe Hershbergeraae05082012-05-23 07:58:01 +000087 puts("## Warning: gatewayip needed but not set\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -050088 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergeraae05082012-05-23 07:58:01 +000089 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050090 net_arp_wait_reply_ip = net_gateway;
Joe Hershbergeraae05082012-05-23 07:58:01 +000091 }
92 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050093 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergeraae05082012-05-23 07:58:01 +000094 }
95
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050096 arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
Joe Hershbergeraae05082012-05-23 07:58:01 +000097}
98
99void ArpTimeoutCheck(void)
100{
101 ulong t;
102
Joe Hershberger5874dec2015-04-08 01:41:01 -0500103 if (!net_arp_wait_packet_ip.s_addr)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000104 return;
105
106 t = get_timer(0);
107
108 /* check for arp timeout */
109 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
110 NetArpWaitTry++;
111
112 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
113 puts("\nARP Retry count exceeded; starting again\n");
114 NetArpWaitTry = 0;
115 NetStartAgain();
116 } else {
117 NetArpWaitTimerStart = t;
118 ArpRequest();
119 }
120 }
121}
122
Joe Hershberger1178f412012-05-23 07:58:06 +0000123void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000124{
Joe Hershbergerde15d0932012-05-23 07:58:08 +0000125 struct arp_hdr *arp;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500126 struct in_addr reply_ip_addr;
Joe Hershbergeraae05082012-05-23 07:58:01 +0000127 uchar *pkt;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +0000128 int eth_hdr_size;
Joe Hershbergeraae05082012-05-23 07:58:01 +0000129
130 /*
131 * We have to deal with two types of ARP packets:
132 * - REQUEST packets will be answered by sending our
133 * IP address - if we know it.
134 * - REPLY packates are expected only after we asked
135 * for the TFTP server's or the gateway's ethernet
136 * address; so if we receive such a packet, we set
137 * the server ethernet address
138 */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000139 debug_cond(DEBUG_NET_PKT, "Got ARP\n");
Joe Hershbergeraae05082012-05-23 07:58:01 +0000140
Joe Hershbergerde15d0932012-05-23 07:58:08 +0000141 arp = (struct arp_hdr *)ip;
Joe Hershbergeraae05082012-05-23 07:58:01 +0000142 if (len < ARP_HDR_SIZE) {
143 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
144 return;
145 }
146 if (ntohs(arp->ar_hrd) != ARP_ETHER)
147 return;
148 if (ntohs(arp->ar_pro) != PROT_IP)
149 return;
Joe Hershberger2f787492012-05-23 07:58:17 +0000150 if (arp->ar_hln != ARP_HLEN)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000151 return;
Joe Hershberger2f787492012-05-23 07:58:17 +0000152 if (arp->ar_pln != ARP_PLEN)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000153 return;
154
Joe Hershberger5874dec2015-04-08 01:41:01 -0500155 if (net_ip.s_addr == 0)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000156 return;
157
Joe Hershberger5874dec2015-04-08 01:41:01 -0500158 if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000159 return;
160
161 switch (ntohs(arp->ar_op)) {
162 case ARPOP_REQUEST:
163 /* reply with our IP address */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000164 debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
Joe Hershbergeraae05082012-05-23 07:58:01 +0000165 pkt = (uchar *)et;
Joe Hershberger530cd6b2012-05-23 07:59:16 +0000166 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +0000167 pkt += eth_hdr_size;
Joe Hershbergeraae05082012-05-23 07:58:01 +0000168 arp->ar_op = htons(ARPOP_REPLY);
Joe Hershberger2f787492012-05-23 07:58:17 +0000169 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500170 net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500171 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500172 net_copy_ip(&arp->ar_spa, &net_ip);
Joe Hershberger39048f62012-05-23 08:00:13 +0000173
174#ifdef CONFIG_CMD_LINK_LOCAL
175 /*
176 * Work-around for brain-damaged Cisco equipment with
177 * arp-proxy enabled.
178 *
179 * If the requesting IP is not on our subnet, wait 5ms to
180 * reply to ARP request so that our reply will overwrite
181 * the arp-proxy's instead of the other way around.
182 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500183 if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
184 (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
Joe Hershberger39048f62012-05-23 08:00:13 +0000185 udelay(5000);
186#endif
Joe Hershbergerebe3ca82012-05-23 07:59:13 +0000187 NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergeraae05082012-05-23 07:58:01 +0000188 return;
189
190 case ARPOP_REPLY: /* arp reply */
191 /* are we waiting for a reply */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500192 if (!net_arp_wait_packet_ip.s_addr)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000193 break;
194
195#ifdef CONFIG_KEEP_SERVERADDR
Joe Hershberger5874dec2015-04-08 01:41:01 -0500196 if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
Joe Hershbergeraae05082012-05-23 07:58:01 +0000197 char buf[20];
Mike Frysingerdc36a6a2012-07-10 00:41:50 +0000198 sprintf(buf, "%pM", &arp->ar_sha);
Joe Hershbergeraae05082012-05-23 07:58:01 +0000199 setenv("serveraddr", buf);
200 }
201#endif
202
Joe Hershberger5874dec2015-04-08 01:41:01 -0500203 reply_ip_addr = net_read_ip(&arp->ar_spa);
Joe Hershbergeraae05082012-05-23 07:58:01 +0000204
205 /* matched waiting packet's address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500206 if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000207 debug_cond(DEBUG_DEV_PKT,
208 "Got ARP REPLY, set eth addr (%pM)\n",
Joe Hershbergeraae05082012-05-23 07:58:01 +0000209 arp->ar_data);
210
211 /* save address for later use */
Joe Hershbergere96624a2012-05-23 07:59:20 +0000212 if (NetArpWaitPacketMAC != NULL)
213 memcpy(NetArpWaitPacketMAC,
214 &arp->ar_sha, ARP_HLEN);
Joe Hershbergeraae05082012-05-23 07:58:01 +0000215
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000216 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
217 0, len);
218
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000219 /* set the mac address in the waiting packet's header
220 and transmit it */
221 memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
222 &arp->ar_sha, ARP_HLEN);
223 NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
Joe Hershbergeraae05082012-05-23 07:58:01 +0000224
225 /* no arp request pending now */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500226 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergeraae05082012-05-23 07:58:01 +0000227 NetArpWaitTxPacketSize = 0;
228 NetArpWaitPacketMAC = NULL;
229
230 }
231 return;
232 default:
233 debug("Unexpected ARP opcode 0x%x\n",
234 ntohs(arp->ar_op));
235 return;
236 }
237}