2012-05-23 07:58:01 +00:00
|
|
|
/*
|
|
|
|
* Copied from Linux Monitor (LiMon) - Networking.
|
|
|
|
*
|
|
|
|
* Copyright 1994 - 2000 Neil Russell.
|
|
|
|
* (See License)
|
|
|
|
* Copyright 2000 Roland Borde
|
|
|
|
* Copyright 2000 Paolo Scaffardi
|
|
|
|
* Copyright 2000-2002 Wolfgang Denk, wd@denx.de
|
2014-09-30 08:44:01 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0
|
2012-05-23 07:58:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
|
|
|
#include "arp.h"
|
|
|
|
|
|
|
|
#ifndef CONFIG_ARP_TIMEOUT
|
|
|
|
/* Milliseconds before trying ARP again */
|
|
|
|
# define ARP_TIMEOUT 5000UL
|
|
|
|
#else
|
|
|
|
# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_RETRY_COUNT
|
|
|
|
# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
|
|
|
|
#else
|
|
|
|
# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 06:41:01 +00:00
|
|
|
struct in_addr net_arp_wait_packet_ip;
|
|
|
|
static struct in_addr net_arp_wait_reply_ip;
|
2012-05-23 07:58:01 +00:00
|
|
|
/* MAC address of waiting packet's destination */
|
2015-04-08 06:41:08 +00:00
|
|
|
uchar *arp_wait_packet_ethaddr;
|
|
|
|
int arp_wait_tx_packet_size;
|
|
|
|
ulong arp_wait_timer_start;
|
|
|
|
int arp_wait_try;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
static uchar *arp_tx_packet; /* THE ARP transmit packet */
|
|
|
|
static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
|
2012-05-23 07:59:24 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
void arp_init(void)
|
2012-05-23 07:58:01 +00:00
|
|
|
{
|
|
|
|
/* XXX problem with bss workaround */
|
2015-04-08 06:41:08 +00:00
|
|
|
arp_wait_packet_ethaddr = NULL;
|
2015-04-08 06:41:01 +00:00
|
|
|
net_arp_wait_packet_ip.s_addr = 0;
|
|
|
|
net_arp_wait_reply_ip.s_addr = 0;
|
2015-04-08 06:41:08 +00:00
|
|
|
arp_wait_tx_packet_size = 0;
|
|
|
|
arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
|
|
|
|
arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
|
2015-04-08 06:41:01 +00:00
|
|
|
struct in_addr target_ip)
|
2012-05-23 07:58:01 +00:00
|
|
|
{
|
|
|
|
uchar *pkt;
|
2012-05-23 07:58:08 +00:00
|
|
|
struct arp_hdr *arp;
|
2012-05-23 07:59:09 +00:00
|
|
|
int eth_hdr_size;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
pkt = arp_tx_packet;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:05 +00:00
|
|
|
eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
|
2012-05-23 07:59:09 +00:00
|
|
|
pkt += eth_hdr_size;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
arp = (struct arp_hdr *)pkt;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
|
|
|
arp->ar_hrd = htons(ARP_ETHER);
|
|
|
|
arp->ar_pro = htons(PROT_IP);
|
2012-05-23 07:58:17 +00:00
|
|
|
arp->ar_hln = ARP_HLEN;
|
|
|
|
arp->ar_pln = ARP_PLEN;
|
2012-05-23 07:58:01 +00:00
|
|
|
arp->ar_op = htons(ARPOP_REQUEST);
|
|
|
|
|
2015-04-08 06:41:04 +00:00
|
|
|
memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
|
2015-04-08 06:41:01 +00:00
|
|
|
net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
|
2015-04-08 06:41:08 +00:00
|
|
|
memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
|
2015-04-08 06:41:01 +00:00
|
|
|
net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
|
2012-05-23 08:00:11 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
|
2012-05-23 08:00:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
void arp_request(void)
|
2012-05-23 08:00:11 +00:00
|
|
|
{
|
2015-04-08 06:41:01 +00:00
|
|
|
if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
|
|
|
|
(net_ip.s_addr & net_netmask.s_addr)) {
|
|
|
|
if (net_gateway.s_addr == 0) {
|
2012-05-23 07:58:01 +00:00
|
|
|
puts("## Warning: gatewayip needed but not set\n");
|
2015-04-08 06:41:01 +00:00
|
|
|
net_arp_wait_reply_ip = net_arp_wait_packet_ip;
|
2012-05-23 07:58:01 +00:00
|
|
|
} else {
|
2015-04-08 06:41:01 +00:00
|
|
|
net_arp_wait_reply_ip = net_gateway;
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-08 06:41:01 +00:00
|
|
|
net_arp_wait_reply_ip = net_arp_wait_packet_ip;
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 06:41:04 +00:00
|
|
|
arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
void arp_timeout_check(void)
|
2012-05-23 07:58:01 +00:00
|
|
|
{
|
|
|
|
ulong t;
|
|
|
|
|
2015-04-08 06:41:01 +00:00
|
|
|
if (!net_arp_wait_packet_ip.s_addr)
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
t = get_timer(0);
|
|
|
|
|
|
|
|
/* check for arp timeout */
|
2015-04-08 06:41:08 +00:00
|
|
|
if ((t - arp_wait_timer_start) > ARP_TIMEOUT) {
|
|
|
|
arp_wait_try++;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
if (arp_wait_try >= ARP_TIMEOUT_COUNT) {
|
2012-05-23 07:58:01 +00:00
|
|
|
puts("\nARP Retry count exceeded; starting again\n");
|
2015-04-08 06:41:08 +00:00
|
|
|
arp_wait_try = 0;
|
2015-04-08 06:41:21 +00:00
|
|
|
net_start_again();
|
2012-05-23 07:58:01 +00:00
|
|
|
} else {
|
2015-04-08 06:41:08 +00:00
|
|
|
arp_wait_timer_start = t;
|
|
|
|
arp_request();
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 06:41:08 +00:00
|
|
|
void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
|
2012-05-23 07:58:01 +00:00
|
|
|
{
|
2012-05-23 07:58:08 +00:00
|
|
|
struct arp_hdr *arp;
|
2015-04-08 06:41:01 +00:00
|
|
|
struct in_addr reply_ip_addr;
|
2012-05-23 07:58:01 +00:00
|
|
|
uchar *pkt;
|
2012-05-23 07:59:09 +00:00
|
|
|
int eth_hdr_size;
|
2012-05-23 07:58:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to deal with two types of ARP packets:
|
|
|
|
* - REQUEST packets will be answered by sending our
|
|
|
|
* IP address - if we know it.
|
|
|
|
* - REPLY packates are expected only after we asked
|
|
|
|
* for the TFTP server's or the gateway's ethernet
|
|
|
|
* address; so if we receive such a packet, we set
|
|
|
|
* the server ethernet address
|
|
|
|
*/
|
2012-05-23 08:01:04 +00:00
|
|
|
debug_cond(DEBUG_NET_PKT, "Got ARP\n");
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2012-05-23 07:58:08 +00:00
|
|
|
arp = (struct arp_hdr *)ip;
|
2012-05-23 07:58:01 +00:00
|
|
|
if (len < ARP_HDR_SIZE) {
|
|
|
|
printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ntohs(arp->ar_hrd) != ARP_ETHER)
|
|
|
|
return;
|
|
|
|
if (ntohs(arp->ar_pro) != PROT_IP)
|
|
|
|
return;
|
2012-05-23 07:58:17 +00:00
|
|
|
if (arp->ar_hln != ARP_HLEN)
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
2012-05-23 07:58:17 +00:00
|
|
|
if (arp->ar_pln != ARP_PLEN)
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-08 06:41:01 +00:00
|
|
|
if (net_ip.s_addr == 0)
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-08 06:41:01 +00:00
|
|
|
if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (ntohs(arp->ar_op)) {
|
|
|
|
case ARPOP_REQUEST:
|
|
|
|
/* reply with our IP address */
|
2012-05-23 08:01:04 +00:00
|
|
|
debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
|
2012-05-23 07:58:01 +00:00
|
|
|
pkt = (uchar *)et;
|
2012-05-23 07:59:16 +00:00
|
|
|
eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
|
2012-05-23 07:59:09 +00:00
|
|
|
pkt += eth_hdr_size;
|
2012-05-23 07:58:01 +00:00
|
|
|
arp->ar_op = htons(ARPOP_REPLY);
|
2012-05-23 07:58:17 +00:00
|
|
|
memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
|
2015-04-08 06:41:01 +00:00
|
|
|
net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
|
2015-04-08 06:41:04 +00:00
|
|
|
memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
|
2015-04-08 06:41:01 +00:00
|
|
|
net_copy_ip(&arp->ar_spa, &net_ip);
|
2012-05-23 08:00:13 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_CMD_LINK_LOCAL
|
|
|
|
/*
|
|
|
|
* Work-around for brain-damaged Cisco equipment with
|
|
|
|
* arp-proxy enabled.
|
|
|
|
*
|
|
|
|
* If the requesting IP is not on our subnet, wait 5ms to
|
|
|
|
* reply to ARP request so that our reply will overwrite
|
|
|
|
* the arp-proxy's instead of the other way around.
|
|
|
|
*/
|
2015-04-08 06:41:01 +00:00
|
|
|
if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
|
|
|
|
(net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
|
2012-05-23 08:00:13 +00:00
|
|
|
udelay(5000);
|
|
|
|
#endif
|
2015-04-08 06:41:05 +00:00
|
|
|
net_send_packet((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
|
2012-05-23 07:58:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ARPOP_REPLY: /* arp reply */
|
|
|
|
/* are we waiting for a reply */
|
2015-04-08 06:41:01 +00:00
|
|
|
if (!net_arp_wait_packet_ip.s_addr)
|
2012-05-23 07:58:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef CONFIG_KEEP_SERVERADDR
|
2015-04-08 06:41:01 +00:00
|
|
|
if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
|
2012-05-23 07:58:01 +00:00
|
|
|
char buf[20];
|
2012-07-10 00:41:50 +00:00
|
|
|
sprintf(buf, "%pM", &arp->ar_sha);
|
2012-05-23 07:58:01 +00:00
|
|
|
setenv("serveraddr", buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 06:41:01 +00:00
|
|
|
reply_ip_addr = net_read_ip(&arp->ar_spa);
|
2012-05-23 07:58:01 +00:00
|
|
|
|
|
|
|
/* matched waiting packet's address */
|
2015-04-08 06:41:01 +00:00
|
|
|
if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
|
2012-05-23 08:01:04 +00:00
|
|
|
debug_cond(DEBUG_DEV_PKT,
|
2015-04-08 06:41:08 +00:00
|
|
|
"Got ARP REPLY, set eth addr (%pM)\n",
|
|
|
|
arp->ar_data);
|
2012-05-23 07:58:01 +00:00
|
|
|
|
|
|
|
/* save address for later use */
|
2015-04-08 06:41:08 +00:00
|
|
|
if (arp_wait_packet_ethaddr != NULL)
|
|
|
|
memcpy(arp_wait_packet_ethaddr,
|
2012-05-23 07:59:20 +00:00
|
|
|
&arp->ar_sha, ARP_HLEN);
|
2012-05-23 07:58:01 +00:00
|
|
|
|
2012-05-23 07:59:15 +00:00
|
|
|
net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
|
2015-04-08 06:41:08 +00:00
|
|
|
0, len);
|
2012-05-23 07:59:15 +00:00
|
|
|
|
2012-05-23 07:59:24 +00:00
|
|
|
/* set the mac address in the waiting packet's header
|
|
|
|
and transmit it */
|
2015-04-08 06:41:05 +00:00
|
|
|
memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
|
|
|
|
&arp->ar_sha, ARP_HLEN);
|
2015-04-08 06:41:08 +00:00
|
|
|
net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
|
2012-05-23 07:58:01 +00:00
|
|
|
|
|
|
|
/* no arp request pending now */
|
2015-04-08 06:41:01 +00:00
|
|
|
net_arp_wait_packet_ip.s_addr = 0;
|
2015-04-08 06:41:08 +00:00
|
|
|
arp_wait_tx_packet_size = 0;
|
|
|
|
arp_wait_packet_ethaddr = NULL;
|
2012-05-23 07:58:01 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
debug("Unexpected ARP opcode 0x%x\n",
|
|
|
|
ntohs(arp->ar_op));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|