blob: 231b6233c07a7f07e41d1a1842ee8780f556c063 [file] [log] [blame]
Tom Rini83d290c2018-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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
wdenkaffae2b2002-08-17 09:36:01 +000010#include <net.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020011#include <net/tftp.h>
wdenkc40b2952004-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 Hershberger8b9c5322012-05-23 07:58:03 +000016#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenkaffae2b2002-08-17 09:36:01 +000017
Joe Hershberger698d78e2015-04-08 01:41:11 -050018int rarp_try;
wdenkaffae2b2002-08-17 09:36:01 +000019
20/*
21 * Handle a RARP received packet.
22 */
Joe Hershberger594c26f2012-05-23 07:58:04 +000023void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000024{
Joe Hershberger738853b2012-05-23 07:58:08 +000025 struct arp_hdr *arp;
Joe Hershberger8b9c5322012-05-23 07:58:03 +000026
Joe Hershberger4ef8d532012-05-23 08:01:04 +000027 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershberger738853b2012-05-23 07:58:08 +000028 arp = (struct arp_hdr *)ip;
Joe Hershberger8b9c5322012-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 Hershberger698d78e2015-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 Hershberger8b9c5322012-05-23 07:58:03 +000038 puts("invalid RARP header\n");
39 } else {
Joe Hershberger049a95a2015-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 Hershberger0adb5b72015-04-08 01:41:04 -050043 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
Joe Hershberger4ef8d532012-05-23 08:01:04 +000044 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger8b9c5322012-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 Hershberger698d78e2015-04-08 01:41:11 -050053static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000054{
Tom Rini01d1b992022-03-11 09:12:02 -050055 if (rarp_try >= CONFIG_NET_RETRY_COUNT) {
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000056 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050057 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000058 } else {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050059 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger698d78e2015-04-08 01:41:11 -050060 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000061 }
62}
63
64
Joe Hershberger698d78e2015-04-08 01:41:11 -050065void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000066{
Joe Hershbergerdb288a92012-05-15 08:59:04 +000067 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000068 struct arp_hdr *rarp;
Joe Hershberger00f33262012-05-23 07:59:09 +000069 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000070
Joe Hershberger698d78e2015-04-08 01:41:11 -050071 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -050072 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000073
Joe Hershberger1203fcc2015-04-08 01:41:05 -050074 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershberger00f33262012-05-23 07:59:09 +000075 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000076
Joe Hershberger738853b2012-05-23 07:58:08 +000077 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000078
Joe Hershbergerc2faf4f2012-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 Hershbergerc2faf4f2012-05-15 08:59:10 +000083 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050084 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050085 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000086 /* dest ET addr = source ET addr ??*/
Joe Hershberger0adb5b72015-04-08 01:41:04 -050087 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger8b9c5322012-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 Hershberger1203fcc2015-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 Hershbergerbc0571f2015-04-08 01:41:21 -050093 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000094}