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