Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/net/arp.c
Line
Count
Source
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
29
void arp_init(void)
30
0
{
31
  /* XXX problem with bss workaround */
32
0
  arp_wait_packet_ethaddr = NULL;
33
0
  net_arp_wait_packet_ip.s_addr = 0;
34
0
  net_arp_wait_reply_ip.s_addr = 0;
35
0
  arp_wait_tx_packet_size = 0;
36
0
  arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
37
0
  arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
38
0
}
39
40
void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
41
  struct in_addr target_ip)
42
0
{
43
0
  uchar *pkt;
44
0
  struct arp_hdr *arp;
45
0
  int eth_hdr_size;
46
47
0
  debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
48
49
0
  pkt = arp_tx_packet;
50
51
0
  eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
52
0
  pkt += eth_hdr_size;
53
54
0
  arp = (struct arp_hdr *)pkt;
55
56
0
  arp->ar_hrd = htons(ARP_ETHER);
57
0
  arp->ar_pro = htons(PROT_IP);
58
0
  arp->ar_hln = ARP_HLEN;
59
0
  arp->ar_pln = ARP_PLEN;
60
0
  arp->ar_op = htons(ARPOP_REQUEST);
61
62
0
  memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);  /* source ET addr */
63
0
  net_write_ip(&arp->ar_spa, source_ip);   /* source IP addr */
64
0
  memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN);  /* target ET addr */
65
0
  net_write_ip(&arp->ar_tpa, target_ip);   /* target IP addr */
66
67
0
  net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
68
0
}
69
70
void arp_request(void)
71
0
{
72
0
  if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
73
0
      (net_ip.s_addr & net_netmask.s_addr)) {
74
0
    if (net_gateway.s_addr == 0) {
75
0
      puts("## Warning: gatewayip needed but not set\n");
76
0
      net_arp_wait_reply_ip = net_arp_wait_packet_ip;
77
0
    } else {
78
0
      net_arp_wait_reply_ip = net_gateway;
79
0
    }
80
0
  } else {
81
0
    net_arp_wait_reply_ip = net_arp_wait_packet_ip;
82
0
  }
83
84
0
  arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
85
0
}
86
87
int arp_timeout_check(void)
88
0
{
89
0
  ulong t;
90
91
0
  if (!arp_is_waiting())
92
0
    return 0;
93
94
0
  t = get_timer(0);
95
96
  /* check for arp timeout */
97
0
  if ((t - arp_wait_timer_start) > CONFIG_ARP_TIMEOUT) {
98
0
    arp_wait_try++;
99
100
0
    if (arp_wait_try >= CONFIG_NET_RETRY_COUNT) {
101
0
      puts("\nARP Retry count exceeded; starting again\n");
102
0
      arp_wait_try = 0;
103
0
      net_set_state(NETLOOP_FAIL);
104
0
    } else {
105
0
      arp_wait_timer_start = t;
106
0
      arp_request();
107
0
    }
108
0
  }
109
0
  return 1;
110
0
}
111
112
void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
113
0
{
114
0
  struct arp_hdr *arp;
115
0
  struct in_addr reply_ip_addr;
116
0
  int eth_hdr_size;
117
0
  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
0
  debug_cond(DEBUG_NET_PKT, "Got ARP\n");
129
130
0
  arp = (struct arp_hdr *)ip;
131
0
  if (len < ARP_HDR_SIZE) {
132
0
    printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
133
0
    return;
134
0
  }
135
0
  if (ntohs(arp->ar_hrd) != ARP_ETHER)
136
0
    return;
137
0
  if (ntohs(arp->ar_pro) != PROT_IP)
138
0
    return;
139
0
  if (arp->ar_hln != ARP_HLEN)
140
0
    return;
141
0
  if (arp->ar_pln != ARP_PLEN)
142
0
    return;
143
144
0
  if (net_ip.s_addr == 0)
145
0
    return;
146
147
0
  if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
148
0
    return;
149
150
0
  switch (ntohs(arp->ar_op)) {
151
0
  case ARPOP_REQUEST:
152
    /* reply with our IP address */
153
0
    debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
154
0
    eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
155
0
    arp->ar_op = htons(ARPOP_REPLY);
156
0
    memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
157
0
    net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
158
0
    memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
159
0
    net_copy_ip(&arp->ar_spa, &net_ip);
160
161
0
#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
0
    if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
171
0
        (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
172
0
      udelay(5000);
173
0
#endif
174
0
    tx_packet = net_get_async_tx_pkt_buf();
175
0
    memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
176
0
    net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE);
177
0
    return;
178
179
0
  case ARPOP_REPLY:   /* arp reply */
180
    /* are we waiting for a reply? */
181
0
    if (!arp_is_waiting())
182
0
      break;
183
184
0
    if (IS_ENABLED(CONFIG_KEEP_SERVERADDR) &&
185
0
        net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
186
0
      char buf[20];
187
0
      sprintf(buf, "%pM", &arp->ar_sha);
188
0
      env_set("serveraddr", buf);
189
0
    }
190
191
0
    reply_ip_addr = net_read_ip(&arp->ar_spa);
192
193
    /* matched waiting packet's address */
194
0
    if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
195
0
      debug_cond(DEBUG_DEV_PKT,
196
0
           "Got ARP REPLY, set eth addr (%pM)\n",
197
0
           arp->ar_data);
198
199
      /* save address for later use */
200
0
      if (arp_wait_packet_ethaddr != NULL)
201
0
        memcpy(arp_wait_packet_ethaddr,
202
0
               &arp->ar_sha, ARP_HLEN);
203
204
0
      net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
205
0
                0, len);
206
207
      /* set the mac address in the waiting packet's header
208
         and transmit it */
209
0
      memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
210
0
             &arp->ar_sha, ARP_HLEN);
211
0
      net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
212
213
      /* no arp request pending now */
214
0
      net_arp_wait_packet_ip.s_addr = 0;
215
0
      arp_wait_tx_packet_size = 0;
216
0
      arp_wait_packet_ethaddr = NULL;
217
0
    }
218
0
    return;
219
0
  default:
220
0
    debug("Unexpected ARP opcode 0x%x\n",
221
0
          ntohs(arp->ar_op));
222
0
    return;
223
0
  }
224
0
}
225
226
bool arp_is_waiting(void)
227
0
{
228
0
  return !!net_arp_wait_packet_ip.s_addr;
229
0
}