rarp.c 2.32 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4
/*
 * (C) Copyright 2000-2002
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6 7 8 9 10
 */

#include <common.h>
#include <command.h>
#include <net.h>
11
#include <net/tftp.h>
12
#include "nfs.h"
wdenk's avatar
wdenk committed
13 14 15
#include "bootp.h"
#include "rarp.h"

16
#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk's avatar
wdenk committed
17
#ifndef	CONFIG_NET_RETRY_COUNT
18
#define TIMEOUT_COUNT 5 /* # of timeouts before giving up  */
wdenk's avatar
wdenk committed
19
#else
20
#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk's avatar
wdenk committed
21 22
#endif

23
int rarp_try;
wdenk's avatar
wdenk committed
24 25 26 27

/*
 *	Handle a RARP received packet.
 */
28
void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenk's avatar
wdenk committed
29
{
30
	struct arp_hdr *arp;
31

32
	debug_cond(DEBUG_NET_PKT, "Got RARP\n");
33
	arp = (struct arp_hdr *)ip;
34 35 36 37 38 39
	if (len < ARP_HDR_SIZE) {
		printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
		return;
	}

	if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
40 41 42
	    (ntohs(arp->ar_hrd) != ARP_ETHER)   ||
	    (ntohs(arp->ar_pro) != PROT_IP)     ||
	    (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
43 44
		puts("invalid RARP header\n");
	} else {
45 46 47
		net_copy_ip(&net_ip, &arp->ar_data[16]);
		if (net_server_ip.s_addr == 0)
			net_copy_ip(&net_server_ip, &arp->ar_data[6]);
48
		memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
49
		debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
50 51
		net_auto_load();
	}
wdenk's avatar
wdenk committed
52 53 54 55 56 57
}


/*
 *	Timeout on BOOTP request.
 */
58
static void rarp_timeout_handler(void)
wdenk's avatar
wdenk committed
59
{
60
	if (rarp_try >= TIMEOUT_COUNT) {
61
		puts("\nRetry count exceeded; starting again\n");
62
		net_start_again();
wdenk's avatar
wdenk committed
63
	} else {
64
		net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
65
		rarp_request();
wdenk's avatar
wdenk committed
66 67 68 69
	}
}


70
void rarp_request(void)
wdenk's avatar
wdenk committed
71
{
72
	uchar *pkt;
73
	struct arp_hdr *rarp;
74
	int eth_hdr_size;
wdenk's avatar
wdenk committed
75

76
	printf("RARP broadcast %d\n", ++rarp_try);
77
	pkt = net_tx_packet;
wdenk's avatar
wdenk committed
78

79
	eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
80
	pkt += eth_hdr_size;
wdenk's avatar
wdenk committed
81

82
	rarp = (struct arp_hdr *)pkt;
wdenk's avatar
wdenk committed
83

84 85
	rarp->ar_hrd = htons(ARP_ETHER);
	rarp->ar_pro = htons(PROT_IP);
wdenk's avatar
wdenk committed
86 87
	rarp->ar_hln = 6;
	rarp->ar_pln = 4;
88
	rarp->ar_op  = htons(RARPOP_REQUEST);
89
	memcpy(&rarp->ar_data[0],  net_ethaddr, 6);	/* source ET addr */
90
	memcpy(&rarp->ar_data[6],  &net_ip,   4);	/* source IP addr */
91
	/* dest ET addr = source ET addr ??*/
92
	memcpy(&rarp->ar_data[10], net_ethaddr, 6);
93 94
	/* dest IP addr set to broadcast */
	memset(&rarp->ar_data[16], 0xff,        4);
wdenk's avatar
wdenk committed
95

96
	net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenk's avatar
wdenk committed
97

98
	net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenk's avatar
wdenk committed
99
}