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