blob: a6b564e314d47d2c5b55e2c3b4c5a3f1130b471f [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
48
49/*
50 * Timeout on BOOTP request.
51 */
Joe Hershberger8e805bb2015-04-08 01:41:11 -050052static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000053{
Tom Rini26011a32022-03-11 09:12:02 -050054 if (rarp_try >= CONFIG_NET_RETRY_COUNT) {
Joe Hershbergerc7684462012-05-15 08:59:10 +000055 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050056 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000057 } else {
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050058 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger8e805bb2015-04-08 01:41:11 -050059 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000060 }
61}
62
63
Joe Hershberger8e805bb2015-04-08 01:41:11 -050064void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000065{
Joe Hershberger4b7747e2012-05-15 08:59:04 +000066 uchar *pkt;
Joe Hershbergerde15d0932012-05-23 07:58:08 +000067 struct arp_hdr *rarp;
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000068 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000069
Joe Hershberger8e805bb2015-04-08 01:41:11 -050070 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050071 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000072
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050073 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershbergerf7ea8052012-05-23 07:59:09 +000074 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000075
Joe Hershbergerde15d0932012-05-23 07:58:08 +000076 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000077
Joe Hershbergerc7684462012-05-15 08:59:10 +000078 rarp->ar_hrd = htons(ARP_ETHER);
79 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000080 rarp->ar_hln = 6;
81 rarp->ar_pln = 4;
Joe Hershbergerc7684462012-05-15 08:59:10 +000082 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050083 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger5874dec2015-04-08 01:41:01 -050084 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc7684462012-05-15 08:59:10 +000085 /* dest ET addr = source ET addr ??*/
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050086 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger61b4de62012-05-23 07:58:03 +000087 /* dest IP addr set to broadcast */
88 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000089
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050090 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000091
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050092 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000093}