blob: c1d9fcaf251849ff164f0eb9ece3af3368d8d80d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkaffae2b2002-08-17 09:36:01 +00005 */
6
7#include <common.h>
8#include <command.h>
9#include <net.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020010#include <net/tftp.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"
wdenkaffae2b2002-08-17 09:36:01 +000014
Joe Hershberger61b4de62012-05-23 07:58:03 +000015#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenkaffae2b2002-08-17 09:36:01 +000016#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger61b4de62012-05-23 07:58:03 +000017#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenkaffae2b2002-08-17 09:36:01 +000018#else
Joe Hershberger61b4de62012-05-23 07:58:03 +000019#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenkaffae2b2002-08-17 09:36:01 +000020#endif
21
Joe Hershberger8e805bb2015-04-08 01:41:11 -050022int rarp_try;
wdenkaffae2b2002-08-17 09:36:01 +000023
24/*
25 * Handle a RARP received packet.
26 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +000027void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000028{
Joe Hershbergerde15d0932012-05-23 07:58:08 +000029 struct arp_hdr *arp;
Joe Hershberger61b4de62012-05-23 07:58:03 +000030
Joe Hershberger05a377b2012-05-23 08:01:04 +000031 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershbergerde15d0932012-05-23 07:58:08 +000032 arp = (struct arp_hdr *)ip;
Joe Hershberger61b4de62012-05-23 07:58:03 +000033 if (len < ARP_HDR_SIZE) {
34 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
35 return;
36 }
37
38 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
Joe Hershberger8e805bb2015-04-08 01:41:11 -050039 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
40 (ntohs(arp->ar_pro) != PROT_IP) ||
41 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
Joe Hershberger61b4de62012-05-23 07:58:03 +000042 puts("invalid RARP header\n");
43 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050044 net_copy_ip(&net_ip, &arp->ar_data[16]);
45 if (net_server_ip.s_addr == 0)
46 net_copy_ip(&net_server_ip, &arp->ar_data[6]);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050047 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
Joe Hershberger05a377b2012-05-23 08:01:04 +000048 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger61b4de62012-05-23 07:58:03 +000049 net_auto_load();
50 }
wdenkaffae2b2002-08-17 09:36:01 +000051}
52
53
54/*
55 * Timeout on BOOTP request.
56 */
Joe Hershberger8e805bb2015-04-08 01:41:11 -050057static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000058{
Joe Hershberger8e805bb2015-04-08 01:41:11 -050059 if (rarp_try >= TIMEOUT_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000060 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050061 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000062 } else {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050063 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger8e805bb2015-04-08 01:41:11 -050064 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000065 }
66}
67
68
Joe Hershberger8e805bb2015-04-08 01:41:11 -050069void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000070{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000071 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000072 struct arp_hdr *rarp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000073 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000074
Joe Hershberger8e805bb2015-04-08 01:41:11 -050075 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050076 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000077
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050078 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000079 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000080
Joe Hershbergerde15d0932012-05-23 07:58:08 +000081 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000082
Joe Hershbergerc7684462012-05-15 08:59:10 +000083 rarp->ar_hrd = htons(ARP_ETHER);
84 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000085 rarp->ar_hln = 6;
86 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +000087 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050088 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050089 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc7684462012-05-15 08:59:10 +000090 /* dest ET addr = source ET addr ??*/
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050091 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +000092 /* dest IP addr set to broadcast */
93 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000094
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050095 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000096
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050097 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000098}