blob: a8e085126d4fa27c9837323e5ea3c2e81ac6cdaa [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <net.h>
wdenkf602aa02004-03-13 23:29:43 +000011#include "nfs.h"
wdenkaffae2b2002-08-17 09:36:01 +000012#include "bootp.h"
13#include "rarp.h"
14#include "tftp.h"
15
Joe Hershberger61b4de62012-05-23 07:58:03 +000016#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenkaffae2b2002-08-17 09:36:01 +000017#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger61b4de62012-05-23 07:58:03 +000018#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenkaffae2b2002-08-17 09:36:01 +000019#else
Joe Hershberger61b4de62012-05-23 07:58:03 +000020#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenkaffae2b2002-08-17 09:36:01 +000021#endif
22
Joe Hershberger61b4de62012-05-23 07:58:03 +000023int RarpTry;
wdenkaffae2b2002-08-17 09:36:01 +000024
25/*
26 * Handle a RARP received packet.
27 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +000028void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000029{
Joe Hershbergerde15d0932012-05-23 07:58:08 +000030 struct arp_hdr *arp;
Joe Hershberger61b4de62012-05-23 07:58:03 +000031
Joe Hershberger05a377b2012-05-23 08:01:04 +000032 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershbergerde15d0932012-05-23 07:58:08 +000033 arp = (struct arp_hdr *)ip;
Joe Hershberger61b4de62012-05-23 07:58:03 +000034 if (len < ARP_HDR_SIZE) {
35 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
36 return;
37 }
38
39 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
40 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
41 (ntohs(arp->ar_pro) != PROT_IP) ||
42 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
43
44 puts("invalid RARP header\n");
45 } else {
46 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
47 if (NetServerIP == 0)
48 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
49 memcpy(NetServerEther, &arp->ar_data[0], 6);
Joe Hershberger05a377b2012-05-23 08:01:04 +000050 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger61b4de62012-05-23 07:58:03 +000051 net_auto_load();
52 }
wdenkaffae2b2002-08-17 09:36:01 +000053}
54
55
56/*
57 * Timeout on BOOTP request.
58 */
Joe Hershberger61b4de62012-05-23 07:58:03 +000059static void RarpTimeout(void)
wdenkaffae2b2002-08-17 09:36:01 +000060{
61 if (RarpTry >= TIMEOUT_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000062 puts("\nRetry count exceeded; starting again\n");
63 NetStartAgain();
wdenkaffae2b2002-08-17 09:36:01 +000064 } else {
Joe Hershbergerc7684462012-05-15 08:59:10 +000065 NetSetTimeout(TIMEOUT, RarpTimeout);
66 RarpRequest();
wdenkaffae2b2002-08-17 09:36:01 +000067 }
68}
69
70
Joe Hershberger61b4de62012-05-23 07:58:03 +000071void RarpRequest(void)
wdenkaffae2b2002-08-17 09:36:01 +000072{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000073 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000074 struct arp_hdr *rarp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000075 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000076
77 printf("RARP broadcast %d\n", ++RarpTry);
78 pkt = NetTxPacket;
79
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000080 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP);
81 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000082
Joe Hershbergerde15d0932012-05-23 07:58:08 +000083 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000084
Joe Hershbergerc7684462012-05-15 08:59:10 +000085 rarp->ar_hrd = htons(ARP_ETHER);
86 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000087 rarp->ar_hln = 6;
88 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +000089 rarp->ar_op = htons(RARPOP_REQUEST);
90 memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
91 memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
92 /* dest ET addr = source ET addr ??*/
93 memcpy(&rarp->ar_data[10], NetOurEther, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +000094 /* dest IP addr set to broadcast */
95 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000096
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000097 NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000098
Bartlomiej Sieka56668462008-10-01 15:26:28 +020099 NetSetTimeout(TIMEOUT, RarpTimeout);
wdenkaffae2b2002-08-17 09:36:01 +0000100}