]>
Commit | Line | Data |
---|---|---|
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, [email protected] | |
10 | */ | |
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 | ||
049a95a7 JH |
30 | struct in_addr net_arp_wait_packet_ip; |
31 | static struct in_addr net_arp_wait_reply_ip; | |
d280d3f4 | 32 | /* MAC address of waiting packet's destination */ |
85d25e0e JH |
33 | uchar *arp_wait_packet_ethaddr; |
34 | int arp_wait_tx_packet_size; | |
35 | ulong arp_wait_timer_start; | |
36 | int arp_wait_try; | |
ac3f26cc | 37 | uchar *arp_tx_packet; /* THE ARP transmit packet */ |
85d25e0e | 38 | static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN]; |
e94070c4 | 39 | |
85d25e0e | 40 | void arp_init(void) |
d280d3f4 JH |
41 | { |
42 | /* XXX problem with bss workaround */ | |
85d25e0e | 43 | arp_wait_packet_ethaddr = NULL; |
049a95a7 JH |
44 | net_arp_wait_packet_ip.s_addr = 0; |
45 | net_arp_wait_reply_ip.s_addr = 0; | |
85d25e0e JH |
46 | arp_wait_tx_packet_size = 0; |
47 | arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1); | |
48 | arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN; | |
d280d3f4 JH |
49 | } |
50 | ||
85d25e0e | 51 | void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr, |
049a95a7 | 52 | struct in_addr target_ip) |
d280d3f4 JH |
53 | { |
54 | uchar *pkt; | |
738853bb | 55 | struct arp_hdr *arp; |
00f33268 | 56 | int eth_hdr_size; |
d280d3f4 | 57 | |
85d25e0e | 58 | debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try); |
d280d3f4 | 59 | |
85d25e0e | 60 | pkt = arp_tx_packet; |
d280d3f4 | 61 | |
1203fcce | 62 | eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP); |
00f33268 | 63 | pkt += eth_hdr_size; |
d280d3f4 | 64 | |
85d25e0e | 65 | arp = (struct arp_hdr *)pkt; |
d280d3f4 JH |
66 | |
67 | arp->ar_hrd = htons(ARP_ETHER); | |
68 | arp->ar_pro = htons(PROT_IP); | |
674bb249 JH |
69 | arp->ar_hln = ARP_HLEN; |
70 | arp->ar_pln = ARP_PLEN; | |
d280d3f4 JH |
71 | arp->ar_op = htons(ARPOP_REQUEST); |
72 | ||
0adb5b76 | 73 | memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */ |
049a95a7 | 74 | net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */ |
85d25e0e | 75 | memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */ |
049a95a7 | 76 | net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */ |
22804189 | 77 | |
85d25e0e | 78 | net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE); |
22804189 JH |
79 | } |
80 | ||
85d25e0e | 81 | void arp_request(void) |
22804189 | 82 | { |
049a95a7 JH |
83 | if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) != |
84 | (net_ip.s_addr & net_netmask.s_addr)) { | |
85 | if (net_gateway.s_addr == 0) { | |
d280d3f4 | 86 | puts("## Warning: gatewayip needed but not set\n"); |
049a95a7 | 87 | net_arp_wait_reply_ip = net_arp_wait_packet_ip; |
d280d3f4 | 88 | } else { |
049a95a7 | 89 | net_arp_wait_reply_ip = net_gateway; |
d280d3f4 JH |
90 | } |
91 | } else { | |
049a95a7 | 92 | net_arp_wait_reply_ip = net_arp_wait_packet_ip; |
d280d3f4 JH |
93 | } |
94 | ||
0adb5b76 | 95 | arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip); |
d280d3f4 JH |
96 | } |
97 | ||
45b47734 | 98 | int arp_timeout_check(void) |
d280d3f4 JH |
99 | { |
100 | ulong t; | |
101 | ||
3f02c98b | 102 | if (!arp_is_waiting()) |
45b47734 | 103 | return 0; |
d280d3f4 JH |
104 | |
105 | t = get_timer(0); | |
106 | ||
107 | /* check for arp timeout */ | |
85d25e0e JH |
108 | if ((t - arp_wait_timer_start) > ARP_TIMEOUT) { |
109 | arp_wait_try++; | |
d280d3f4 | 110 | |
85d25e0e | 111 | if (arp_wait_try >= ARP_TIMEOUT_COUNT) { |
d280d3f4 | 112 | puts("\nARP Retry count exceeded; starting again\n"); |
85d25e0e | 113 | arp_wait_try = 0; |
94323111 | 114 | net_set_state(NETLOOP_FAIL); |
d280d3f4 | 115 | } else { |
85d25e0e JH |
116 | arp_wait_timer_start = t; |
117 | arp_request(); | |
d280d3f4 JH |
118 | } |
119 | } | |
45b47734 | 120 | return 1; |
d280d3f4 JH |
121 | } |
122 | ||
85d25e0e | 123 | void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) |
d280d3f4 | 124 | { |
738853bb | 125 | struct arp_hdr *arp; |
049a95a7 | 126 | struct in_addr reply_ip_addr; |
00f33268 | 127 | int eth_hdr_size; |
ac3f26cc | 128 | uchar *tx_packet; |
d280d3f4 JH |
129 | |
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 | */ | |
4ef8d53c | 139 | debug_cond(DEBUG_NET_PKT, "Got ARP\n"); |
d280d3f4 | 140 | |
738853bb | 141 | arp = (struct arp_hdr *)ip; |
d280d3f4 JH |
142 | 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; | |
674bb249 | 150 | if (arp->ar_hln != ARP_HLEN) |
d280d3f4 | 151 | return; |
674bb249 | 152 | if (arp->ar_pln != ARP_PLEN) |
d280d3f4 JH |
153 | return; |
154 | ||
049a95a7 | 155 | if (net_ip.s_addr == 0) |
d280d3f4 JH |
156 | return; |
157 | ||
049a95a7 | 158 | if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr) |
d280d3f4 JH |
159 | return; |
160 | ||
161 | switch (ntohs(arp->ar_op)) { | |
162 | case ARPOP_REQUEST: | |
163 | /* reply with our IP address */ | |
4ef8d53c | 164 | debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); |
e7111015 | 165 | eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); |
d280d3f4 | 166 | arp->ar_op = htons(ARPOP_REPLY); |
674bb249 | 167 | memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); |
049a95a7 | 168 | net_copy_ip(&arp->ar_tpa, &arp->ar_spa); |
0adb5b76 | 169 | memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); |
049a95a7 | 170 | net_copy_ip(&arp->ar_spa, &net_ip); |
c6975762 JH |
171 | |
172 | #ifdef CONFIG_CMD_LINK_LOCAL | |
173 | /* | |
174 | * Work-around for brain-damaged Cisco equipment with | |
175 | * arp-proxy enabled. | |
176 | * | |
177 | * If the requesting IP is not on our subnet, wait 5ms to | |
178 | * reply to ARP request so that our reply will overwrite | |
179 | * the arp-proxy's instead of the other way around. | |
180 | */ | |
049a95a7 JH |
181 | if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) != |
182 | (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr)) | |
c6975762 JH |
183 | udelay(5000); |
184 | #endif | |
ac3f26cc JH |
185 | tx_packet = net_get_async_tx_pkt_buf(); |
186 | memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE); | |
187 | net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE); | |
d280d3f4 JH |
188 | return; |
189 | ||
190 | case ARPOP_REPLY: /* arp reply */ | |
3f02c98b JH |
191 | /* are we waiting for a reply? */ |
192 | if (!arp_is_waiting()) | |
d280d3f4 JH |
193 | break; |
194 | ||
195 | #ifdef CONFIG_KEEP_SERVERADDR | |
049a95a7 | 196 | if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) { |
d280d3f4 | 197 | char buf[20]; |
a0bc44e6 | 198 | sprintf(buf, "%pM", &arp->ar_sha); |
382bee57 | 199 | env_set("serveraddr", buf); |
d280d3f4 JH |
200 | } |
201 | #endif | |
202 | ||
049a95a7 | 203 | reply_ip_addr = net_read_ip(&arp->ar_spa); |
d280d3f4 JH |
204 | |
205 | /* matched waiting packet's address */ | |
049a95a7 | 206 | if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) { |
4ef8d53c | 207 | debug_cond(DEBUG_DEV_PKT, |
85d25e0e JH |
208 | "Got ARP REPLY, set eth addr (%pM)\n", |
209 | arp->ar_data); | |
d280d3f4 JH |
210 | |
211 | /* save address for later use */ | |
85d25e0e JH |
212 | if (arp_wait_packet_ethaddr != NULL) |
213 | memcpy(arp_wait_packet_ethaddr, | |
f1d2d284 | 214 | &arp->ar_sha, ARP_HLEN); |
d280d3f4 | 215 | |
ece223b5 | 216 | net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, |
85d25e0e | 217 | 0, len); |
ece223b5 | 218 | |
e94070c4 JH |
219 | /* set the mac address in the waiting packet's header |
220 | and transmit it */ | |
1203fcce JH |
221 | memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest, |
222 | &arp->ar_sha, ARP_HLEN); | |
85d25e0e | 223 | net_send_packet(net_tx_packet, arp_wait_tx_packet_size); |
d280d3f4 JH |
224 | |
225 | /* no arp request pending now */ | |
049a95a7 | 226 | net_arp_wait_packet_ip.s_addr = 0; |
85d25e0e JH |
227 | arp_wait_tx_packet_size = 0; |
228 | arp_wait_packet_ethaddr = NULL; | |
d280d3f4 JH |
229 | } |
230 | return; | |
231 | default: | |
232 | debug("Unexpected ARP opcode 0x%x\n", | |
233 | ntohs(arp->ar_op)); | |
234 | return; | |
235 | } | |
236 | } | |
3f02c98b JH |
237 | |
238 | bool arp_is_waiting(void) | |
239 | { | |
240 | return !!net_arp_wait_packet_ip.s_addr; | |
241 | } |