blob: 231b6233c07a7f07e41d1a1842ee8780f556c063 [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
Tom Riniabb9a042024-05-18 20:20:43 -06007#include <common.h>
wdenkaffae2b2002-08-17 09:36:01 +00008#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
wdenkaffae2b2002-08-17 09:36:01 +000010#include <net.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020011#include <net/tftp.h>
wdenkf602aa02004-03-13 23:29:43 +000012#include "nfs.h"
wdenkaffae2b2002-08-17 09:36:01 +000013#include "bootp.h"
14#include "rarp.h"
wdenkaffae2b2002-08-17 09:36:01 +000015
Joe Hershberger61b4de62012-05-23 07:58:03 +000016#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenkaffae2b2002-08-17 09:36:01 +000017
Joe Hershberger8e805bb2015-04-08 01:41:11 -050018int rarp_try;
wdenkaffae2b2002-08-17 09:36:01 +000019
20/*
21 * Handle a RARP received packet.
22 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +000023void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000024{
Joe Hershbergerde15d0932012-05-23 07:58:08 +000025 struct arp_hdr *arp;
Joe Hershberger61b4de62012-05-23 07:58:03 +000026
Joe Hershberger05a377b2012-05-23 08:01:04 +000027 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershbergerde15d0932012-05-23 07:58:08 +000028 arp = (struct arp_hdr *)ip;
Joe Hershberger61b4de62012-05-23 07:58:03 +000029 if (len < ARP_HDR_SIZE) {
30 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
31 return;
32 }
33
34 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
Joe Hershberger8e805bb2015-04-08 01:41:11 -050035 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
36 (ntohs(arp->ar_pro) != PROT_IP) ||
37 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
Joe Hershberger61b4de62012-05-23 07:58:03 +000038 puts("invalid RARP header\n");
39 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050040 net_copy_ip(&net_ip, &arp->ar_data[16]);
41 if (net_server_ip.s_addr == 0)
42 net_copy_ip(&net_server_ip, &arp->ar_data[6]);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050043 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
Joe Hershberger05a377b2012-05-23 08:01:04 +000044 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger61b4de62012-05-23 07:58:03 +000045 net_auto_load();
46 }
wdenkaffae2b2002-08-17 09:36:01 +000047}
48
49
50/*
51 * Timeout on BOOTP request.
52 */
Joe Hershberger8e805bb2015-04-08 01:41:11 -050053static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000054{
Tom Rini26011a32022-03-11 09:12:02 -050055 if (rarp_try >= CONFIG_NET_RETRY_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000056 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050057 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000058 } else {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050059 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger8e805bb2015-04-08 01:41:11 -050060 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000061 }
62}
63
64
Joe Hershberger8e805bb2015-04-08 01:41:11 -050065void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000066{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000067 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000068 struct arp_hdr *rarp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000069 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000070
Joe Hershberger8e805bb2015-04-08 01:41:11 -050071 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050072 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000073
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050074 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000075 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000076
Joe Hershbergerde15d0932012-05-23 07:58:08 +000077 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000078
Joe Hershbergerc7684462012-05-15 08:59:10 +000079 rarp->ar_hrd = htons(ARP_ETHER);
80 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000081 rarp->ar_hln = 6;
82 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +000083 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050084 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050085 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc7684462012-05-15 08:59:10 +000086 /* dest ET addr = source ET addr ??*/
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050087 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +000088 /* dest IP addr set to broadcast */
89 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000090
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050091 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000092
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050093 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000094}