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