]> git.ipfire.org Git - thirdparty/u-boot.git/blame - net/rarp.c
Merge patch series "k3-j721e: beagleboneai: Fix USB"
[thirdparty/u-boot.git] / net / rarp.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
affae2bf
WD
2/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
affae2bf
WD
5 */
6
7#include <common.h>
8#include <command.h>
f7ae49fc 9#include <log.h>
affae2bf 10#include <net.h>
34696958 11#include <net/tftp.h>
c40b2956 12#include "nfs.h"
affae2bf
WD
13#include "bootp.h"
14#include "rarp.h"
affae2bf 15
8b9c5322 16#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
affae2bf 17
698d78e5 18int rarp_try;
affae2bf
WD
19
20/*
21 * Handle a RARP received packet.
22 */
594c26f8 23void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
affae2bf 24{
738853bb 25 struct arp_hdr *arp;
8b9c5322 26
4ef8d53c 27 debug_cond(DEBUG_NET_PKT, "Got RARP\n");
738853bb 28 arp = (struct arp_hdr *)ip;
8b9c5322
JH
29 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) ||
698d78e5
JH
35 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
36 (ntohs(arp->ar_pro) != PROT_IP) ||
37 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
8b9c5322
JH
38 puts("invalid RARP header\n");
39 } else {
049a95a7
JH
40 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]);
0adb5b76 43 memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
4ef8d53c 44 debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
8b9c5322
JH
45 net_auto_load();
46 }
affae2bf
WD
47}
48
49
50/*
51 * Timeout on BOOTP request.
52 */
698d78e5 53static void rarp_timeout_handler(void)
affae2bf 54{
01d1b99c 55 if (rarp_try >= CONFIG_NET_RETRY_COUNT) {
c2faf4f9 56 puts("\nRetry count exceeded; starting again\n");
bc0571fc 57 net_start_again();
affae2bf 58 } else {
bc0571fc 59 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
698d78e5 60 rarp_request();
affae2bf
WD
61 }
62}
63
64
698d78e5 65void rarp_request(void)
affae2bf 66{
db288a96 67 uchar *pkt;
738853bb 68 struct arp_hdr *rarp;
00f33268 69 int eth_hdr_size;
affae2bf 70
698d78e5 71 printf("RARP broadcast %d\n", ++rarp_try);
1203fcce 72 pkt = net_tx_packet;
affae2bf 73
1203fcce 74 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
00f33268 75 pkt += eth_hdr_size;
affae2bf 76
738853bb 77 rarp = (struct arp_hdr *)pkt;
affae2bf 78
c2faf4f9
JH
79 rarp->ar_hrd = htons(ARP_ETHER);
80 rarp->ar_pro = htons(PROT_IP);
affae2bf
WD
81 rarp->ar_hln = 6;
82 rarp->ar_pln = 4;
c2faf4f9 83 rarp->ar_op = htons(RARPOP_REQUEST);
0adb5b76 84 memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
049a95a7 85 memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
c2faf4f9 86 /* dest ET addr = source ET addr ??*/
0adb5b76 87 memcpy(&rarp->ar_data[10], net_ethaddr, 6);
8b9c5322
JH
88 /* dest IP addr set to broadcast */
89 memset(&rarp->ar_data[16], 0xff, 4);
affae2bf 90
1203fcce 91 net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
affae2bf 92
bc0571fc 93 net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
affae2bf 94}