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