blob: 660a02c5770282d9c7a4ab79ed9a7384fd9175e6 [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <command.h>
26#include <net.h>
wdenkf602aa02004-03-13 23:29:43 +000027#include "nfs.h"
wdenkaffae2b2002-08-17 09:36:01 +000028#include "bootp.h"
29#include "rarp.h"
30#include "tftp.h"
31
Joe Hershberger61b4de62012-05-23 07:58:03 +000032#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenkaffae2b2002-08-17 09:36:01 +000033#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger61b4de62012-05-23 07:58:03 +000034#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenkaffae2b2002-08-17 09:36:01 +000035#else
Joe Hershberger61b4de62012-05-23 07:58:03 +000036#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenkaffae2b2002-08-17 09:36:01 +000037#endif
38
Joe Hershberger61b4de62012-05-23 07:58:03 +000039int RarpTry;
wdenkaffae2b2002-08-17 09:36:01 +000040
41/*
42 * Handle a RARP received packet.
43 */
Joe Hershberger61b4de62012-05-23 07:58:03 +000044void rarp_receive(IP_t *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000045{
Joe Hershberger61b4de62012-05-23 07:58:03 +000046 ARP_t *arp;
47
48 debug("Got RARP\n");
49 arp = (ARP_t *)ip;
50 if (len < ARP_HDR_SIZE) {
51 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
52 return;
53 }
54
55 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
56 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
57 (ntohs(arp->ar_pro) != PROT_IP) ||
58 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
59
60 puts("invalid RARP header\n");
61 } else {
62 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
63 if (NetServerIP == 0)
64 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
65 memcpy(NetServerEther, &arp->ar_data[0], 6);
66 debug("Got good RARP\n");
67 net_auto_load();
68 }
wdenkaffae2b2002-08-17 09:36:01 +000069}
70
71
72/*
73 * Timeout on BOOTP request.
74 */
Joe Hershberger61b4de62012-05-23 07:58:03 +000075static void RarpTimeout(void)
wdenkaffae2b2002-08-17 09:36:01 +000076{
77 if (RarpTry >= TIMEOUT_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000078 puts("\nRetry count exceeded; starting again\n");
79 NetStartAgain();
wdenkaffae2b2002-08-17 09:36:01 +000080 } else {
Joe Hershbergerc7684462012-05-15 08:59:10 +000081 NetSetTimeout(TIMEOUT, RarpTimeout);
82 RarpRequest();
wdenkaffae2b2002-08-17 09:36:01 +000083 }
84}
85
86
Joe Hershberger61b4de62012-05-23 07:58:03 +000087void RarpRequest(void)
wdenkaffae2b2002-08-17 09:36:01 +000088{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000089 uchar *pkt;
Joe Hershbergerc7684462012-05-15 08:59:10 +000090 ARP_t *rarp;
wdenkaffae2b2002-08-17 09:36:01 +000091
92 printf("RARP broadcast %d\n", ++RarpTry);
93 pkt = NetTxPacket;
94
wdenk145d2c12004-04-15 21:48:45 +000095 pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
wdenkaffae2b2002-08-17 09:36:01 +000096
97 rarp = (ARP_t *)pkt;
98
Joe Hershbergerc7684462012-05-15 08:59:10 +000099 rarp->ar_hrd = htons(ARP_ETHER);
100 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +0000101 rarp->ar_hln = 6;
102 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +0000103 rarp->ar_op = htons(RARPOP_REQUEST);
104 memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
105 memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
106 /* dest ET addr = source ET addr ??*/
107 memcpy(&rarp->ar_data[10], NetOurEther, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +0000108 /* dest IP addr set to broadcast */
109 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +0000110
wdenk145d2c12004-04-15 21:48:45 +0000111 NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +0000112
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200113 NetSetTimeout(TIMEOUT, RarpTimeout);
wdenkaffae2b2002-08-17 09:36:01 +0000114}