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 Denk1a459662013-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>
wdenkc40b2952004-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 Hershberger8b9c5322012-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 Hershberger8b9c5322012-05-23 07:58:03 +000018#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenkaffae2b2002-08-17 09:36:01 +000019#else
Joe Hershberger8b9c5322012-05-23 07:58:03 +000020#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenkaffae2b2002-08-17 09:36:01 +000021#endif
22
Joe Hershberger8b9c5322012-05-23 07:58:03 +000023int RarpTry;
wdenkaffae2b2002-08-17 09:36:01 +000024
25/*
26 * Handle a RARP received packet.
27 */
Joe Hershberger594c26f2012-05-23 07:58:04 +000028void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000029{
Joe Hershberger738853b2012-05-23 07:58:08 +000030 struct arp_hdr *arp;
Joe Hershberger8b9c5322012-05-23 07:58:03 +000031
Joe Hershberger4ef8d532012-05-23 08:01:04 +000032 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershberger738853b2012-05-23 07:58:08 +000033 arp = (struct arp_hdr *)ip;
Joe Hershberger8b9c5322012-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 Hershberger4ef8d532012-05-23 08:01:04 +000050 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger8b9c5322012-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 Hershberger8b9c5322012-05-23 07:58:03 +000059static void RarpTimeout(void)
wdenkaffae2b2002-08-17 09:36:01 +000060{
61 if (RarpTry >= TIMEOUT_COUNT) {
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000062 puts("\nRetry count exceeded; starting again\n");
63 NetStartAgain();
wdenkaffae2b2002-08-17 09:36:01 +000064 } else {
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000065 NetSetTimeout(TIMEOUT, RarpTimeout);
66 RarpRequest();
wdenkaffae2b2002-08-17 09:36:01 +000067 }
68}
69
70
Joe Hershberger8b9c5322012-05-23 07:58:03 +000071void RarpRequest(void)
wdenkaffae2b2002-08-17 09:36:01 +000072{
Joe Hershbergerdb288a92012-05-15 08:59:04 +000073 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000074 struct arp_hdr *rarp;
Joe Hershberger00f33262012-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 Hershberger00f33262012-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 Hershberger738853b2012-05-23 07:58:08 +000083 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000084
Joe Hershbergerc2faf4f2012-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 Hershbergerc2faf4f2012-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 Hershberger8b9c5322012-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 Hershberger00f33262012-05-23 07:59:09 +000097 NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000098
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020099 NetSetTimeout(TIMEOUT, RarpTimeout);
wdenkaffae2b2002-08-17 09:36:01 +0000100}