blob: a346e067cb948a3a69a5f0eaacb4dc63fbc3f9ab [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
wdenkaffae2b2002-08-17 09:36:01 +00007#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
wdenkaffae2b2002-08-17 09:36:01 +00009#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
Joe Hershberger8e805bb2015-04-08 01:41:11 -050017int rarp_try;
wdenkaffae2b2002-08-17 09:36:01 +000018
19/*
20 * Handle a RARP received packet.
21 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +000022void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000023{
Joe Hershbergerde15d0932012-05-23 07:58:08 +000024 struct arp_hdr *arp;
Joe Hershberger61b4de62012-05-23 07:58:03 +000025
Joe Hershberger05a377b2012-05-23 08:01:04 +000026 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershbergerde15d0932012-05-23 07:58:08 +000027 arp = (struct arp_hdr *)ip;
Joe Hershberger61b4de62012-05-23 07:58:03 +000028 if (len < ARP_HDR_SIZE) {
29 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
30 return;
31 }
32
33 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
Joe Hershberger8e805bb2015-04-08 01:41:11 -050034 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
35 (ntohs(arp->ar_pro) != PROT_IP) ||
36 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
Joe Hershberger61b4de62012-05-23 07:58:03 +000037 puts("invalid RARP header\n");
38 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -050039 net_copy_ip(&net_ip, &arp->ar_data[16]);
40 if (net_server_ip.s_addr == 0)
41 net_copy_ip(&net_server_ip, &arp->ar_data[6]);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050042 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
Joe Hershberger05a377b2012-05-23 08:01:04 +000043 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger61b4de62012-05-23 07:58:03 +000044 net_auto_load();
45 }
wdenkaffae2b2002-08-17 09:36:01 +000046}
47
wdenkaffae2b2002-08-17 09:36:01 +000048/*
49 * Timeout on BOOTP request.
50 */
Joe Hershberger8e805bb2015-04-08 01:41:11 -050051static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000052{
Tom Rini26011a32022-03-11 09:12:02 -050053 if (rarp_try >= CONFIG_NET_RETRY_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000054 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050055 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000056 } else {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050057 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger8e805bb2015-04-08 01:41:11 -050058 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000059 }
60}
61
Joe Hershberger8e805bb2015-04-08 01:41:11 -050062void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000063{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000064 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000065 struct arp_hdr *rarp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000066 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000067
Joe Hershberger8e805bb2015-04-08 01:41:11 -050068 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050069 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000070
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050071 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000072 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000073
Joe Hershbergerde15d0932012-05-23 07:58:08 +000074 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000075
Joe Hershbergerc7684462012-05-15 08:59:10 +000076 rarp->ar_hrd = htons(ARP_ETHER);
77 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000078 rarp->ar_hln = 6;
79 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +000080 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050081 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050082 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc7684462012-05-15 08:59:10 +000083 /* dest ET addr = source ET addr ??*/
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050084 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +000085 /* dest IP addr set to broadcast */
86 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000087
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050088 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000089
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050090 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000091}