blob: 1fa11b690d218fbda9fd80d26d99746acae10bf9 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <command.h>
10#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#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger8b9c5322012-05-23 07:58:03 +000018#define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenkaffae2b2002-08-17 09:36:01 +000019#else
Joe Hershberger8b9c5322012-05-23 07:58:03 +000020#define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenkaffae2b2002-08-17 09:36:01 +000021#endif
22
Joe Hershberger698d78e2015-04-08 01:41:11 -050023int rarp_try;
wdenkaffae2b2002-08-17 09:36:01 +000024
25/*
26 * Handle a RARP received packet.
27 */
Joe Hershberger594c26f2012-05-23 07:58:04 +000028void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
wdenkaffae2b2002-08-17 09:36:01 +000029{
Joe Hershberger738853b2012-05-23 07:58:08 +000030 struct arp_hdr *arp;
Joe Hershberger8b9c5322012-05-23 07:58:03 +000031
Joe Hershberger4ef8d532012-05-23 08:01:04 +000032 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
Joe Hershberger738853b2012-05-23 07:58:08 +000033 arp = (struct arp_hdr *)ip;
Joe Hershberger8b9c5322012-05-23 07:58:03 +000034 if (len < ARP_HDR_SIZE) {
35 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
36 return;
37 }
38
39 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
Joe Hershberger698d78e2015-04-08 01:41:11 -050040 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
41 (ntohs(arp->ar_pro) != PROT_IP) ||
42 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
Joe Hershberger8b9c5322012-05-23 07:58:03 +000043 puts("invalid RARP header\n");
44 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -050045 net_copy_ip(&net_ip, &arp->ar_data[16]);
46 if (net_server_ip.s_addr == 0)
47 net_copy_ip(&net_server_ip, &arp->ar_data[6]);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050048 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
Joe Hershberger4ef8d532012-05-23 08:01:04 +000049 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
Joe Hershberger8b9c5322012-05-23 07:58:03 +000050 net_auto_load();
51 }
wdenkaffae2b2002-08-17 09:36:01 +000052}
53
54
55/*
56 * Timeout on BOOTP request.
57 */
Joe Hershberger698d78e2015-04-08 01:41:11 -050058static void rarp_timeout_handler(void)
wdenkaffae2b2002-08-17 09:36:01 +000059{
Joe Hershberger698d78e2015-04-08 01:41:11 -050060 if (rarp_try >= TIMEOUT_COUNT) {
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000061 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050062 net_start_again();
wdenkaffae2b2002-08-17 09:36:01 +000063 } else {
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050064 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
Joe Hershberger698d78e2015-04-08 01:41:11 -050065 rarp_request();
wdenkaffae2b2002-08-17 09:36:01 +000066 }
67}
68
69
Joe Hershberger698d78e2015-04-08 01:41:11 -050070void rarp_request(void)
wdenkaffae2b2002-08-17 09:36:01 +000071{
Joe Hershbergerdb288a92012-05-15 08:59:04 +000072 uchar *pkt;
Joe Hershberger738853b2012-05-23 07:58:08 +000073 struct arp_hdr *rarp;
Joe Hershberger00f33262012-05-23 07:59:09 +000074 int eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000075
Joe Hershberger698d78e2015-04-08 01:41:11 -050076 printf("RARP broadcast %d\n", ++rarp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -050077 pkt = net_tx_packet;
wdenkaffae2b2002-08-17 09:36:01 +000078
Joe Hershberger1203fcc2015-04-08 01:41:05 -050079 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
Joe Hershberger00f33262012-05-23 07:59:09 +000080 pkt += eth_hdr_size;
wdenkaffae2b2002-08-17 09:36:01 +000081
Joe Hershberger738853b2012-05-23 07:58:08 +000082 rarp = (struct arp_hdr *)pkt;
wdenkaffae2b2002-08-17 09:36:01 +000083
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000084 rarp->ar_hrd = htons(ARP_ETHER);
85 rarp->ar_pro = htons(PROT_IP);
wdenkaffae2b2002-08-17 09:36:01 +000086 rarp->ar_hln = 6;
87 rarp->ar_pln = 4;
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000088 rarp->ar_op = htons(RARPOP_REQUEST);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050089 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
Joe Hershberger049a95a2015-04-08 01:41:01 -050090 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
Joe Hershbergerc2faf4f2012-05-15 08:59:10 +000091 /* dest ET addr = source ET addr ??*/
Joe Hershberger0adb5b72015-04-08 01:41:04 -050092 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
Joe Hershberger8b9c5322012-05-23 07:58:03 +000093 /* dest IP addr set to broadcast */
94 memset(&rarp->ar_data[16], 0xff, 4);
wdenkaffae2b2002-08-17 09:36:01 +000095
Joe Hershberger1203fcc2015-04-08 01:41:05 -050096 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +000097
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050098 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
wdenkaffae2b2002-08-17 09:36:01 +000099}