blob: bc1e25f941f721d0d165cc185c62a49137885a9a [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershbergerd280d3f2012-05-23 07:58:01 +00002/*
3 * Copied from Linux Monitor (LiMon) - Networking.
4 *
5 * Copyright 1994 - 2000 Neil Russell.
6 * (See License)
7 * Copyright 2000 Roland Borde
8 * Copyright 2000 Paolo Scaffardi
9 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
10 */
11
Simon Glass09140112020-05-10 11:40:03 -060012#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <net.h>
Tom Rini03de3052024-05-20 13:35:03 -060015#include <vsprintf.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000017
18#include "arp.h"
19
Joe Hershberger049a95a2015-04-08 01:41:01 -050020struct in_addr net_arp_wait_packet_ip;
21static struct in_addr net_arp_wait_reply_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000022/* MAC address of waiting packet's destination */
Joe Hershberger85d25e02015-04-08 01:41:08 -050023uchar *arp_wait_packet_ethaddr;
24int arp_wait_tx_packet_size;
25ulong arp_wait_timer_start;
26int arp_wait_try;
Joe Hershbergerac3f26c2018-09-26 16:49:02 -050027uchar *arp_tx_packet; /* THE ARP transmit packet */
Joe Hershberger85d25e02015-04-08 01:41:08 -050028static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
Joe Hershbergere94070c2012-05-23 07:59:24 +000029
Joe Hershberger85d25e02015-04-08 01:41:08 -050030void arp_init(void)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000031{
32 /* XXX problem with bss workaround */
Joe Hershberger85d25e02015-04-08 01:41:08 -050033 arp_wait_packet_ethaddr = NULL;
Joe Hershberger049a95a2015-04-08 01:41:01 -050034 net_arp_wait_packet_ip.s_addr = 0;
35 net_arp_wait_reply_ip.s_addr = 0;
Joe Hershberger85d25e02015-04-08 01:41:08 -050036 arp_wait_tx_packet_size = 0;
37 arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
38 arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000039}
40
Joe Hershberger85d25e02015-04-08 01:41:08 -050041void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
Joe Hershberger049a95a2015-04-08 01:41:01 -050042 struct in_addr target_ip)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000043{
44 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000045 struct arp_hdr *arp;
Joe Hershberger00f33262012-05-23 07:59:09 +000046 int eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000047
Joe Hershberger85d25e02015-04-08 01:41:08 -050048 debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000049
Joe Hershberger85d25e02015-04-08 01:41:08 -050050 pkt = arp_tx_packet;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000051
Joe Hershberger1203fcc2015-04-08 01:41:05 -050052 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
Joe Hershberger00f33262012-05-23 07:59:09 +000053 pkt += eth_hdr_size;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000054
Joe Hershberger85d25e02015-04-08 01:41:08 -050055 arp = (struct arp_hdr *)pkt;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000056
57 arp->ar_hrd = htons(ARP_ETHER);
58 arp->ar_pro = htons(PROT_IP);
Joe Hershberger674bb242012-05-23 07:58:17 +000059 arp->ar_hln = ARP_HLEN;
60 arp->ar_pln = ARP_PLEN;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000061 arp->ar_op = htons(ARPOP_REQUEST);
62
Joe Hershberger0adb5b72015-04-08 01:41:04 -050063 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050064 net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
Joe Hershberger85d25e02015-04-08 01:41:08 -050065 memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050066 net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
Joe Hershberger22804182012-05-23 08:00:11 +000067
Joe Hershberger85d25e02015-04-08 01:41:08 -050068 net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershberger22804182012-05-23 08:00:11 +000069}
70
Joe Hershberger85d25e02015-04-08 01:41:08 -050071void arp_request(void)
Joe Hershberger22804182012-05-23 08:00:11 +000072{
Joe Hershberger049a95a2015-04-08 01:41:01 -050073 if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
74 (net_ip.s_addr & net_netmask.s_addr)) {
75 if (net_gateway.s_addr == 0) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000076 puts("## Warning: gatewayip needed but not set\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -050077 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000078 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050079 net_arp_wait_reply_ip = net_gateway;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000080 }
81 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050082 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000083 }
84
Joe Hershberger0adb5b72015-04-08 01:41:04 -050085 arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000086}
87
Stefan Brüns45b47732015-08-30 17:46:54 +020088int arp_timeout_check(void)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000089{
90 ulong t;
91
Joe Hershberger3f02c982018-09-26 16:48:58 -050092 if (!arp_is_waiting())
Stefan Brüns45b47732015-08-30 17:46:54 +020093 return 0;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000094
95 t = get_timer(0);
96
97 /* check for arp timeout */
Tom Rini5d4e8632022-03-11 09:12:01 -050098 if ((t - arp_wait_timer_start) > CONFIG_ARP_TIMEOUT) {
Joe Hershberger85d25e02015-04-08 01:41:08 -050099 arp_wait_try++;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000100
Tom Rini01d1b992022-03-11 09:12:02 -0500101 if (arp_wait_try >= CONFIG_NET_RETRY_COUNT) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000102 puts("\nARP Retry count exceeded; starting again\n");
Joe Hershberger85d25e02015-04-08 01:41:08 -0500103 arp_wait_try = 0;
Stefan Brüns94323112015-08-30 17:46:43 +0200104 net_set_state(NETLOOP_FAIL);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000105 } else {
Joe Hershberger85d25e02015-04-08 01:41:08 -0500106 arp_wait_timer_start = t;
107 arp_request();
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000108 }
109 }
Stefan Brüns45b47732015-08-30 17:46:54 +0200110 return 1;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000111}
112
Joe Hershberger85d25e02015-04-08 01:41:08 -0500113void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000114{
Joe Hershberger738853b2012-05-23 07:58:08 +0000115 struct arp_hdr *arp;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500116 struct in_addr reply_ip_addr;
Joe Hershberger00f33262012-05-23 07:59:09 +0000117 int eth_hdr_size;
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500118 uchar *tx_packet;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000119
120 /*
121 * We have to deal with two types of ARP packets:
122 * - REQUEST packets will be answered by sending our
123 * IP address - if we know it.
124 * - REPLY packates are expected only after we asked
125 * for the TFTP server's or the gateway's ethernet
126 * address; so if we receive such a packet, we set
127 * the server ethernet address
128 */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000129 debug_cond(DEBUG_NET_PKT, "Got ARP\n");
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000130
Joe Hershberger738853b2012-05-23 07:58:08 +0000131 arp = (struct arp_hdr *)ip;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000132 if (len < ARP_HDR_SIZE) {
133 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
134 return;
135 }
136 if (ntohs(arp->ar_hrd) != ARP_ETHER)
137 return;
138 if (ntohs(arp->ar_pro) != PROT_IP)
139 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000140 if (arp->ar_hln != ARP_HLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000141 return;
Joe Hershberger674bb242012-05-23 07:58:17 +0000142 if (arp->ar_pln != ARP_PLEN)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000143 return;
144
Joe Hershberger049a95a2015-04-08 01:41:01 -0500145 if (net_ip.s_addr == 0)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000146 return;
147
Joe Hershberger049a95a2015-04-08 01:41:01 -0500148 if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000149 return;
150
151 switch (ntohs(arp->ar_op)) {
152 case ARPOP_REQUEST:
153 /* reply with our IP address */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000154 debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
Joe Hershbergere7111012012-05-23 07:59:16 +0000155 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000156 arp->ar_op = htons(ARPOP_REPLY);
Joe Hershberger674bb242012-05-23 07:58:17 +0000157 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500158 net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500159 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500160 net_copy_ip(&arp->ar_spa, &net_ip);
Joe Hershbergerc6975762012-05-23 08:00:13 +0000161
162#ifdef CONFIG_CMD_LINK_LOCAL
163 /*
164 * Work-around for brain-damaged Cisco equipment with
165 * arp-proxy enabled.
166 *
167 * If the requesting IP is not on our subnet, wait 5ms to
168 * reply to ARP request so that our reply will overwrite
169 * the arp-proxy's instead of the other way around.
170 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500171 if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
172 (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
Joe Hershbergerc6975762012-05-23 08:00:13 +0000173 udelay(5000);
174#endif
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500175 tx_packet = net_get_async_tx_pkt_buf();
176 memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
177 net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000178 return;
179
180 case ARPOP_REPLY: /* arp reply */
Joe Hershberger3f02c982018-09-26 16:48:58 -0500181 /* are we waiting for a reply? */
182 if (!arp_is_waiting())
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000183 break;
184
Simon Glass3df6cd42021-12-18 11:27:48 -0700185 if (IS_ENABLED(CONFIG_KEEP_SERVERADDR) &&
186 net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000187 char buf[20];
Mike Frysingera0bc44e2012-07-10 00:41:50 +0000188 sprintf(buf, "%pM", &arp->ar_sha);
Simon Glass382bee52017-08-03 12:22:09 -0600189 env_set("serveraddr", buf);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000190 }
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000191
Joe Hershberger049a95a2015-04-08 01:41:01 -0500192 reply_ip_addr = net_read_ip(&arp->ar_spa);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000193
194 /* matched waiting packet's address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500195 if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000196 debug_cond(DEBUG_DEV_PKT,
Joe Hershberger85d25e02015-04-08 01:41:08 -0500197 "Got ARP REPLY, set eth addr (%pM)\n",
198 arp->ar_data);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000199
200 /* save address for later use */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500201 if (arp_wait_packet_ethaddr != NULL)
202 memcpy(arp_wait_packet_ethaddr,
Joe Hershbergerf1d2d282012-05-23 07:59:20 +0000203 &arp->ar_sha, ARP_HLEN);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000204
Joe Hershbergerece223b2012-05-23 07:59:15 +0000205 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
Joe Hershberger85d25e02015-04-08 01:41:08 -0500206 0, len);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000207
Joe Hershbergere94070c2012-05-23 07:59:24 +0000208 /* set the mac address in the waiting packet's header
209 and transmit it */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500210 memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
211 &arp->ar_sha, ARP_HLEN);
Joe Hershberger85d25e02015-04-08 01:41:08 -0500212 net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000213
214 /* no arp request pending now */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500215 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershberger85d25e02015-04-08 01:41:08 -0500216 arp_wait_tx_packet_size = 0;
217 arp_wait_packet_ethaddr = NULL;
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000218 }
219 return;
220 default:
221 debug("Unexpected ARP opcode 0x%x\n",
222 ntohs(arp->ar_op));
223 return;
224 }
225}
Joe Hershberger3f02c982018-09-26 16:48:58 -0500226
227bool arp_is_waiting(void)
228{
229 return !!net_arp_wait_packet_ip.s_addr;
230}