blob: 824d2e9393550ea250f357b2762cf2f465e5fe66 [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
Wolfgang Denk2ea91032014-09-30 10:44:01 +02009 * SPDX-License-Identifier: GPL-2.0
Joe Hershbergerd280d3f2012-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 Hershberger049a95a2015-04-08 01:41:01 -050030struct in_addr net_arp_wait_packet_ip;
31static struct in_addr net_arp_wait_reply_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000032/* MAC address of waiting packet's destination */
Joe Hershberger85d25e02015-04-08 01:41:08 -050033uchar *arp_wait_packet_ethaddr;
34int arp_wait_tx_packet_size;
35ulong arp_wait_timer_start;
36int arp_wait_try;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000037
Joe Hershberger85d25e02015-04-08 01:41:08 -050038static uchar *arp_tx_packet; /* THE ARP transmit packet */
39static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
Joe Hershbergere94070c2012-05-23 07:59:24 +000040
Joe Hershberger85d25e02015-04-08 01:41:08 -050041void arp_init(void)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000042{
43 /* XXX problem with bss workaround */
Joe Hershberger85d25e02015-04-08 01:41:08 -050044 arp_wait_packet_ethaddr = NULL;
Joe Hershberger049a95a2015-04-08 01:41:01 -050045 net_arp_wait_packet_ip.s_addr = 0;
46 net_arp_wait_reply_ip.s_addr = 0;
Joe Hershberger85d25e02015-04-08 01:41:08 -050047 arp_wait_tx_packet_size = 0;
48 arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
49 arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000050}
51
Joe Hershberger85d25e02015-04-08 01:41:08 -050052void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
Joe Hershberger049a95a2015-04-08 01:41:01 -050053 struct in_addr target_ip)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000054{
55 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000056 struct arp_hdr *arp;
Joe Hershberger00f33262012-05-23 07:59:09 +000057 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000058
Joe Hershberger85d25e02015-04-08 01:41:08 -050059 debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000060
Joe Hershberger85d25e02015-04-08 01:41:08 -050061 pkt = arp_tx_packet;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000062
Joe Hershberger1203fcc2015-04-08 01:41:05 -050063 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
Joe Hershberger00f33262012-05-23 07:59:09 +000064 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000065
Joe Hershberger85d25e02015-04-08 01:41:08 -050066 arp = (struct arp_hdr *)pkt;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000067
68 arp->ar_hrd = htons(ARP_ETHER);
69 arp->ar_pro = htons(PROT_IP);
Joe Hershberger674bb242012-05-23 07:58:17 +000070 arp->ar_hln = ARP_HLEN;
71 arp->ar_pln = ARP_PLEN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000072 arp->ar_op = htons(ARPOP_REQUEST);
73
Joe Hershberger0adb5b72015-04-08 01:41:04 -050074 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050075 net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
Joe Hershberger85d25e02015-04-08 01:41:08 -050076 memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050077 net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
Joe Hershberger22804182012-05-23 08:00:11 +000078
Joe Hershberger85d25e02015-04-08 01:41:08 -050079 net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershberger22804182012-05-23 08:00:11 +000080}
81
Joe Hershberger85d25e02015-04-08 01:41:08 -050082void arp_request(void)
Joe Hershberger22804182012-05-23 08:00:11 +000083{
Joe Hershberger049a95a2015-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 Hershbergerd280d3f2012-05-23 07:58:01 +000087 puts("## Warning: gatewayip needed but not set\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -050088 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000089 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050090 net_arp_wait_reply_ip = net_gateway;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000091 }
92 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050093 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000094 }
95
Joe Hershberger0adb5b72015-04-08 01:41:04 -050096 arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000097}
98
Stefan BrĂ¼ns45b47732015-08-30 17:46:54 +020099int arp_timeout_check(void)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000100{
101 ulong t;
102
Joe Hershberger049a95a2015-04-08 01:41:01 -0500103 if (!net_arp_wait_packet_ip.s_addr)
Stefan BrĂ¼ns45b47732015-08-30 17:46:54 +0200104 return 0;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000105
106 t = get_timer(0);
107
108 /* check for arp timeout */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500109 if ((t - arp_wait_timer_start) > ARP_TIMEOUT) {
110 arp_wait_try++;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000111
Joe Hershberger85d25e02015-04-08 01:41:08 -0500112 if (arp_wait_try >= ARP_TIMEOUT_COUNT) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000113 puts("\nARP Retry count exceeded; starting again\n");
Joe Hershberger85d25e02015-04-08 01:41:08 -0500114 arp_wait_try = 0;
Stefan BrĂ¼ns94323112015-08-30 17:46:43 +0200115 net_set_state(NETLOOP_FAIL);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000116 } else {
Joe Hershberger85d25e02015-04-08 01:41:08 -0500117 arp_wait_timer_start = t;
118 arp_request();
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000119 }
120 }
Stefan BrĂ¼ns45b47732015-08-30 17:46:54 +0200121 return 1;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000122}
123
Joe Hershberger85d25e02015-04-08 01:41:08 -0500124void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000125{
Joe Hershberger738853b2012-05-23 07:58:08 +0000126 struct arp_hdr *arp;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500127 struct in_addr reply_ip_addr;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000128 uchar *pkt;
Joe Hershberger00f33262012-05-23 07:59:09 +0000129 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000130
131 /*
132 * We have to deal with two types of ARP packets:
133 * - REQUEST packets will be answered by sending our
134 * IP address - if we know it.
135 * - REPLY packates are expected only after we asked
136 * for the TFTP server's or the gateway's ethernet
137 * address; so if we receive such a packet, we set
138 * the server ethernet address
139 */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000140 debug_cond(DEBUG_NET_PKT, "Got ARP\n");
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000141
Joe Hershberger738853b2012-05-23 07:58:08 +0000142 arp = (struct arp_hdr *)ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000143 if (len < ARP_HDR_SIZE) {
144 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
145 return;
146 }
147 if (ntohs(arp->ar_hrd) != ARP_ETHER)
148 return;
149 if (ntohs(arp->ar_pro) != PROT_IP)
150 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000151 if (arp->ar_hln != ARP_HLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000152 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000153 if (arp->ar_pln != ARP_PLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000154 return;
155
Joe Hershberger049a95a2015-04-08 01:41:01 -0500156 if (net_ip.s_addr == 0)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000157 return;
158
Joe Hershberger049a95a2015-04-08 01:41:01 -0500159 if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000160 return;
161
162 switch (ntohs(arp->ar_op)) {
163 case ARPOP_REQUEST:
164 /* reply with our IP address */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000165 debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000166 pkt = (uchar *)et;
Joe Hershbergere7111012012-05-23 07:59:16 +0000167 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
Joe Hershberger00f33262012-05-23 07:59:09 +0000168 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000169 arp->ar_op = htons(ARPOP_REPLY);
Joe Hershberger674bb242012-05-23 07:58:17 +0000170 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500171 net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500172 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500173 net_copy_ip(&arp->ar_spa, &net_ip);
Joe Hershbergerc6975762012-05-23 08:00:13 +0000174
175#ifdef CONFIG_CMD_LINK_LOCAL
176 /*
177 * Work-around for brain-damaged Cisco equipment with
178 * arp-proxy enabled.
179 *
180 * If the requesting IP is not on our subnet, wait 5ms to
181 * reply to ARP request so that our reply will overwrite
182 * the arp-proxy's instead of the other way around.
183 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500184 if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
185 (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
Joe Hershbergerc6975762012-05-23 08:00:13 +0000186 udelay(5000);
187#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500188 net_send_packet((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000189 return;
190
191 case ARPOP_REPLY: /* arp reply */
192 /* are we waiting for a reply */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500193 if (!net_arp_wait_packet_ip.s_addr)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000194 break;
195
196#ifdef CONFIG_KEEP_SERVERADDR
Joe Hershberger049a95a2015-04-08 01:41:01 -0500197 if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000198 char buf[20];
Mike Frysingera0bc44e2012-07-10 00:41:50 +0000199 sprintf(buf, "%pM", &arp->ar_sha);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000200 setenv("serveraddr", buf);
201 }
202#endif
203
Joe Hershberger049a95a2015-04-08 01:41:01 -0500204 reply_ip_addr = net_read_ip(&arp->ar_spa);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000205
206 /* matched waiting packet's address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500207 if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000208 debug_cond(DEBUG_DEV_PKT,
Joe Hershberger85d25e02015-04-08 01:41:08 -0500209 "Got ARP REPLY, set eth addr (%pM)\n",
210 arp->ar_data);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000211
212 /* save address for later use */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500213 if (arp_wait_packet_ethaddr != NULL)
214 memcpy(arp_wait_packet_ethaddr,
Joe Hershbergerf1d2d282012-05-23 07:59:20 +0000215 &arp->ar_sha, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000216
Joe Hershbergerece223b2012-05-23 07:59:15 +0000217 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
Joe Hershberger85d25e02015-04-08 01:41:08 -0500218 0, len);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000219
Joe Hershbergere94070c2012-05-23 07:59:24 +0000220 /* set the mac address in the waiting packet's header
221 and transmit it */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500222 memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
223 &arp->ar_sha, ARP_HLEN);
Joe Hershberger85d25e02015-04-08 01:41:08 -0500224 net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000225
226 /* no arp request pending now */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500227 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershberger85d25e02015-04-08 01:41:08 -0500228 arp_wait_tx_packet_size = 0;
229 arp_wait_packet_ethaddr = NULL;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000230 }
231 return;
232 default:
233 debug("Unexpected ARP opcode 0x%x\n",
234 ntohs(arp->ar_op));
235 return;
236 }
237}