]> git.ipfire.org Git - thirdparty/u-boot.git/blame - net/arp.c
malloc: Enable assertions if UNIT_TEST is enabled
[thirdparty/u-boot.git] / net / arp.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0
d280d3f4
JH
2/*
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
12#include <common.h>
09140113 13#include <env.h>
f7ae49fc 14#include <log.h>
90526e9f 15#include <net.h>
c05ed00a 16#include <linux/delay.h>
d280d3f4
JH
17
18#include "arp.h"
19
049a95a7
JH
20struct in_addr net_arp_wait_packet_ip;
21static struct in_addr net_arp_wait_reply_ip;
d280d3f4 22/* MAC address of waiting packet's destination */
85d25e0e
JH
23uchar *arp_wait_packet_ethaddr;
24int arp_wait_tx_packet_size;
25ulong arp_wait_timer_start;
26int arp_wait_try;
ac3f26cc 27uchar *arp_tx_packet; /* THE ARP transmit packet */
85d25e0e 28static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
e94070c4 29
85d25e0e 30void arp_init(void)
d280d3f4
JH
31{
32 /* XXX problem with bss workaround */
85d25e0e 33 arp_wait_packet_ethaddr = NULL;
049a95a7
JH
34 net_arp_wait_packet_ip.s_addr = 0;
35 net_arp_wait_reply_ip.s_addr = 0;
85d25e0e
JH
36 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;
d280d3f4
JH
39}
40
85d25e0e 41void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
049a95a7 42 struct in_addr target_ip)
d280d3f4
JH
43{
44 uchar *pkt;
738853bb 45 struct arp_hdr *arp;
00f33268 46 int eth_hdr_size;
d280d3f4 47
85d25e0e 48 debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
d280d3f4 49
85d25e0e 50 pkt = arp_tx_packet;
d280d3f4 51
1203fcce 52 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
00f33268 53 pkt += eth_hdr_size;
d280d3f4 54
85d25e0e 55 arp = (struct arp_hdr *)pkt;
d280d3f4
JH
56
57 arp->ar_hrd = htons(ARP_ETHER);
58 arp->ar_pro = htons(PROT_IP);
674bb249
JH
59 arp->ar_hln = ARP_HLEN;
60 arp->ar_pln = ARP_PLEN;
d280d3f4
JH
61 arp->ar_op = htons(ARPOP_REQUEST);
62
0adb5b76 63 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
049a95a7 64 net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
85d25e0e 65 memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
049a95a7 66 net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
22804189 67
85d25e0e 68 net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
22804189
JH
69}
70
85d25e0e 71void arp_request(void)
22804189 72{
049a95a7
JH
73 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) {
d280d3f4 76 puts("## Warning: gatewayip needed but not set\n");
049a95a7 77 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
d280d3f4 78 } else {
049a95a7 79 net_arp_wait_reply_ip = net_gateway;
d280d3f4
JH
80 }
81 } else {
049a95a7 82 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
d280d3f4
JH
83 }
84
0adb5b76 85 arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
d280d3f4
JH
86}
87
45b47734 88int arp_timeout_check(void)
d280d3f4
JH
89{
90 ulong t;
91
3f02c98b 92 if (!arp_is_waiting())
45b47734 93 return 0;
d280d3f4
JH
94
95 t = get_timer(0);
96
97 /* check for arp timeout */
5d4e863b 98 if ((t - arp_wait_timer_start) > CONFIG_ARP_TIMEOUT) {
85d25e0e 99 arp_wait_try++;
d280d3f4 100
01d1b99c 101 if (arp_wait_try >= CONFIG_NET_RETRY_COUNT) {
d280d3f4 102 puts("\nARP Retry count exceeded; starting again\n");
85d25e0e 103 arp_wait_try = 0;
94323111 104 net_set_state(NETLOOP_FAIL);
d280d3f4 105 } else {
85d25e0e
JH
106 arp_wait_timer_start = t;
107 arp_request();
d280d3f4
JH
108 }
109 }
45b47734 110 return 1;
d280d3f4
JH
111}
112
85d25e0e 113void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
d280d3f4 114{
738853bb 115 struct arp_hdr *arp;
049a95a7 116 struct in_addr reply_ip_addr;
00f33268 117 int eth_hdr_size;
ac3f26cc 118 uchar *tx_packet;
d280d3f4
JH
119
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 */
4ef8d53c 129 debug_cond(DEBUG_NET_PKT, "Got ARP\n");
d280d3f4 130
738853bb 131 arp = (struct arp_hdr *)ip;
d280d3f4
JH
132 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;
674bb249 140 if (arp->ar_hln != ARP_HLEN)
d280d3f4 141 return;
674bb249 142 if (arp->ar_pln != ARP_PLEN)
d280d3f4
JH
143 return;
144
049a95a7 145 if (net_ip.s_addr == 0)
d280d3f4
JH
146 return;
147
049a95a7 148 if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
d280d3f4
JH
149 return;
150
151 switch (ntohs(arp->ar_op)) {
152 case ARPOP_REQUEST:
153 /* reply with our IP address */
4ef8d53c 154 debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
e7111015 155 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
d280d3f4 156 arp->ar_op = htons(ARPOP_REPLY);
674bb249 157 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
049a95a7 158 net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
0adb5b76 159 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
049a95a7 160 net_copy_ip(&arp->ar_spa, &net_ip);
c6975762
JH
161
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 */
049a95a7
JH
171 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))
c6975762
JH
173 udelay(5000);
174#endif
ac3f26cc
JH
175 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);
d280d3f4
JH
178 return;
179
180 case ARPOP_REPLY: /* arp reply */
3f02c98b
JH
181 /* are we waiting for a reply? */
182 if (!arp_is_waiting())
d280d3f4
JH
183 break;
184
3df6cd4d
SG
185 if (IS_ENABLED(CONFIG_KEEP_SERVERADDR) &&
186 net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
d280d3f4 187 char buf[20];
a0bc44e6 188 sprintf(buf, "%pM", &arp->ar_sha);
382bee57 189 env_set("serveraddr", buf);
d280d3f4 190 }
d280d3f4 191
049a95a7 192 reply_ip_addr = net_read_ip(&arp->ar_spa);
d280d3f4
JH
193
194 /* matched waiting packet's address */
049a95a7 195 if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
4ef8d53c 196 debug_cond(DEBUG_DEV_PKT,
85d25e0e
JH
197 "Got ARP REPLY, set eth addr (%pM)\n",
198 arp->ar_data);
d280d3f4
JH
199
200 /* save address for later use */
85d25e0e
JH
201 if (arp_wait_packet_ethaddr != NULL)
202 memcpy(arp_wait_packet_ethaddr,
f1d2d284 203 &arp->ar_sha, ARP_HLEN);
d280d3f4 204
ece223b5 205 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
85d25e0e 206 0, len);
ece223b5 207
e94070c4
JH
208 /* set the mac address in the waiting packet's header
209 and transmit it */
1203fcce
JH
210 memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
211 &arp->ar_sha, ARP_HLEN);
85d25e0e 212 net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
d280d3f4
JH
213
214 /* no arp request pending now */
049a95a7 215 net_arp_wait_packet_ip.s_addr = 0;
85d25e0e
JH
216 arp_wait_tx_packet_size = 0;
217 arp_wait_packet_ethaddr = NULL;
d280d3f4
JH
218 }
219 return;
220 default:
221 debug("Unexpected ARP opcode 0x%x\n",
222 ntohs(arp->ar_op));
223 return;
224 }
225}
3f02c98b
JH
226
227bool arp_is_waiting(void)
228{
229 return !!net_arp_wait_packet_ip.s_addr;
230}