]>
Commit | Line | Data |
---|---|---|
affae2bf WD |
1 | /* |
2 | * (C) Copyright 2000-2002 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
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> | |
c40b2956 | 11 | #include "nfs.h" |
affae2bf WD |
12 | #include "bootp.h" |
13 | #include "rarp.h" | |
14 | #include "tftp.h" | |
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 | ||
8b9c5322 | 23 | int RarpTry; |
affae2bf WD |
24 | |
25 | /* | |
26 | * Handle a RARP received packet. | |
27 | */ | |
594c26f8 | 28 | void 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) || | |
40 | (ntohs(arp->ar_hrd) != ARP_ETHER) || | |
41 | (ntohs(arp->ar_pro) != PROT_IP) || | |
42 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { | |
43 | ||
44 | puts("invalid RARP header\n"); | |
45 | } else { | |
46 | NetCopyIP(&NetOurIP, &arp->ar_data[16]); | |
47 | if (NetServerIP == 0) | |
48 | NetCopyIP(&NetServerIP, &arp->ar_data[6]); | |
49 | memcpy(NetServerEther, &arp->ar_data[0], 6); | |
4ef8d53c | 50 | debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); |
8b9c5322 JH |
51 | net_auto_load(); |
52 | } | |
affae2bf WD |
53 | } |
54 | ||
55 | ||
56 | /* | |
57 | * Timeout on BOOTP request. | |
58 | */ | |
8b9c5322 | 59 | static void RarpTimeout(void) |
affae2bf WD |
60 | { |
61 | if (RarpTry >= TIMEOUT_COUNT) { | |
c2faf4f9 JH |
62 | puts("\nRetry count exceeded; starting again\n"); |
63 | NetStartAgain(); | |
affae2bf | 64 | } else { |
c2faf4f9 JH |
65 | NetSetTimeout(TIMEOUT, RarpTimeout); |
66 | RarpRequest(); | |
affae2bf WD |
67 | } |
68 | } | |
69 | ||
70 | ||
8b9c5322 | 71 | void RarpRequest(void) |
affae2bf | 72 | { |
db288a96 | 73 | uchar *pkt; |
738853bb | 74 | struct arp_hdr *rarp; |
00f33268 | 75 | int eth_hdr_size; |
affae2bf WD |
76 | |
77 | printf("RARP broadcast %d\n", ++RarpTry); | |
78 | pkt = NetTxPacket; | |
79 | ||
00f33268 JH |
80 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); |
81 | pkt += eth_hdr_size; | |
affae2bf | 82 | |
738853bb | 83 | rarp = (struct arp_hdr *)pkt; |
affae2bf | 84 | |
c2faf4f9 JH |
85 | rarp->ar_hrd = htons(ARP_ETHER); |
86 | rarp->ar_pro = htons(PROT_IP); | |
affae2bf WD |
87 | rarp->ar_hln = 6; |
88 | rarp->ar_pln = 4; | |
c2faf4f9 JH |
89 | rarp->ar_op = htons(RARPOP_REQUEST); |
90 | memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ | |
91 | memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ | |
92 | /* dest ET addr = source ET addr ??*/ | |
93 | memcpy(&rarp->ar_data[10], NetOurEther, 6); | |
8b9c5322 JH |
94 | /* dest IP addr set to broadcast */ |
95 | memset(&rarp->ar_data[16], 0xff, 4); | |
affae2bf | 96 | |
00f33268 | 97 | NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); |
affae2bf | 98 | |
49f3bdbb | 99 | NetSetTimeout(TIMEOUT, RarpTimeout); |
affae2bf | 100 | } |