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