1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copied from Linux Monitor (LiMon) - Networking.
4 *
5 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11 #include <env.h>
12 #include <log.h>
13 #include <net.h>
14 #include <vsprintf.h>
15 #include <linux/delay.h>
16
17 #include "arp.h"
18
19 struct in_addr net_arp_wait_packet_ip;
20 static struct in_addr net_arp_wait_reply_ip;
21 /* MAC address of waiting packet's destination */
22 uchar *arp_wait_packet_ethaddr;
23 int arp_wait_tx_packet_size;
24 ulong arp_wait_timer_start;
25 int arp_wait_try;
26 uchar *arp_tx_packet; /* THE ARP transmit packet */
27 static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
28
arp_init(void)29 void arp_init(void)
30 {
31 /* XXX problem with bss workaround */
32 arp_wait_packet_ethaddr = NULL;
33 net_arp_wait_packet_ip.s_addr = 0;
34 net_arp_wait_reply_ip.s_addr = 0;
35 arp_wait_tx_packet_size = 0;
36 arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
37 arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
38 }
39
arp_raw_request(struct in_addr source_ip,const uchar * target_ethaddr,struct in_addr target_ip)40 void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
41 struct in_addr target_ip)
42 {
43 uchar *pkt;
44 struct arp_hdr *arp;
45 int eth_hdr_size;
46
47 debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
48
49 pkt = arp_tx_packet;
50
51 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
52 pkt += eth_hdr_size;
53
54 arp = (struct arp_hdr *)pkt;
55
56 arp->ar_hrd = htons(ARP_ETHER);
57 arp->ar_pro = htons(PROT_IP);
58 arp->ar_hln = ARP_HLEN;
59 arp->ar_pln = ARP_PLEN;
60 arp->ar_op = htons(ARPOP_REQUEST);
61
62 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
63 net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
64 memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
65 net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
66
67 net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
68 }
69
arp_request(void)70 void arp_request(void)
71 {
72 if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
73 (net_ip.s_addr & net_netmask.s_addr)) {
74 if (net_gateway.s_addr == 0) {
75 puts("## Warning: gatewayip needed but not set\n");
76 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
77 } else {
78 net_arp_wait_reply_ip = net_gateway;
79 }
80 } else {
81 net_arp_wait_reply_ip = net_arp_wait_packet_ip;
82 }
83
84 arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
85 }
86
arp_timeout_check(void)87 int arp_timeout_check(void)
88 {
89 ulong t;
90
91 if (!arp_is_waiting())
92 return 0;
93
94 t = get_timer(0);
95
96 /* check for arp timeout */
97 if ((t - arp_wait_timer_start) > CONFIG_ARP_TIMEOUT) {
98 arp_wait_try++;
99
100 if (arp_wait_try >= CONFIG_NET_RETRY_COUNT) {
101 puts("\nARP Retry count exceeded; starting again\n");
102 arp_wait_try = 0;
103 net_set_state(NETLOOP_FAIL);
104 } else {
105 arp_wait_timer_start = t;
106 arp_request();
107 }
108 }
109 return 1;
110 }
111
arp_receive(struct ethernet_hdr * et,struct ip_udp_hdr * ip,int len)112 void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
113 {
114 struct arp_hdr *arp;
115 struct in_addr reply_ip_addr;
116 int eth_hdr_size;
117 uchar *tx_packet;
118
119 /*
120 * We have to deal with two types of ARP packets:
121 * - REQUEST packets will be answered by sending our
122 * IP address - if we know it.
123 * - REPLY packates are expected only after we asked
124 * for the TFTP server's or the gateway's ethernet
125 * address; so if we receive such a packet, we set
126 * the server ethernet address
127 */
128 debug_cond(DEBUG_NET_PKT, "Got ARP\n");
129
130 arp = (struct arp_hdr *)ip;
131 if (len < ARP_HDR_SIZE) {
132 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
133 return;
134 }
135 if (ntohs(arp->ar_hrd) != ARP_ETHER)
136 return;
137 if (ntohs(arp->ar_pro) != PROT_IP)
138 return;
139 if (arp->ar_hln != ARP_HLEN)
140 return;
141 if (arp->ar_pln != ARP_PLEN)
142 return;
143
144 if (net_ip.s_addr == 0)
145 return;
146
147 if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
148 return;
149
150 switch (ntohs(arp->ar_op)) {
151 case ARPOP_REQUEST:
152 /* reply with our IP address */
153 debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
154 eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
155 arp->ar_op = htons(ARPOP_REPLY);
156 memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
157 net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
158 memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
159 net_copy_ip(&arp->ar_spa, &net_ip);
160
161 #ifdef CONFIG_CMD_LINK_LOCAL
162 /*
163 * Work-around for brain-damaged Cisco equipment with
164 * arp-proxy enabled.
165 *
166 * If the requesting IP is not on our subnet, wait 5ms to
167 * reply to ARP request so that our reply will overwrite
168 * the arp-proxy's instead of the other way around.
169 */
170 if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
171 (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
172 udelay(5000);
173 #endif
174 tx_packet = net_get_async_tx_pkt_buf();
175 memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
176 net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE);
177 return;
178
179 case ARPOP_REPLY: /* arp reply */
180 /* are we waiting for a reply? */
181 if (!arp_is_waiting())
182 break;
183
184 if (IS_ENABLED(CONFIG_KEEP_SERVERADDR) &&
185 net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
186 char buf[20];
187 sprintf(buf, "%pM", &arp->ar_sha);
188 env_set("serveraddr", buf);
189 }
190
191 reply_ip_addr = net_read_ip(&arp->ar_spa);
192
193 /* matched waiting packet's address */
194 if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
195 debug_cond(DEBUG_DEV_PKT,
196 "Got ARP REPLY, set eth addr (%pM)\n",
197 arp->ar_data);
198
199 /* save address for later use */
200 if (arp_wait_packet_ethaddr != NULL)
201 memcpy(arp_wait_packet_ethaddr,
202 &arp->ar_sha, ARP_HLEN);
203
204 net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
205 0, len);
206
207 /* set the mac address in the waiting packet's header
208 and transmit it */
209 memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
210 &arp->ar_sha, ARP_HLEN);
211 net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
212
213 /* no arp request pending now */
214 net_arp_wait_packet_ip.s_addr = 0;
215 arp_wait_tx_packet_size = 0;
216 arp_wait_packet_ethaddr = NULL;
217 }
218 return;
219 default:
220 debug("Unexpected ARP opcode 0x%x\n",
221 ntohs(arp->ar_op));
222 return;
223 }
224 }
225
arp_is_waiting(void)226 bool arp_is_waiting(void)
227 {
228 return !!net_arp_wait_packet_ip.s_addr;
229 }
230