Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 9 | #include <net.h> |
Lukasz Majewski | 2118592 | 2015-08-24 00:21:43 +0200 | [diff] [blame] | 10 | #include <net/tftp.h> |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 11 | #include "nfs.h" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 12 | #include "bootp.h" |
| 13 | #include "rarp.h" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 14 | |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 15 | #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 16 | |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 17 | int rarp_try; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Handle a RARP received packet. |
| 21 | */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 22 | void rarp_receive(struct ip_udp_hdr *ip, unsigned len) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 23 | { |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 24 | struct arp_hdr *arp; |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 25 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 26 | debug_cond(DEBUG_NET_PKT, "Got RARP\n"); |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 27 | arp = (struct arp_hdr *)ip; |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 28 | if (len < ARP_HDR_SIZE) { |
| 29 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if ((ntohs(arp->ar_op) != RARPOP_REPLY) || |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 34 | (ntohs(arp->ar_hrd) != ARP_ETHER) || |
| 35 | (ntohs(arp->ar_pro) != PROT_IP) || |
| 36 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 37 | puts("invalid RARP header\n"); |
| 38 | } else { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 39 | net_copy_ip(&net_ip, &arp->ar_data[16]); |
| 40 | if (net_server_ip.s_addr == 0) |
| 41 | net_copy_ip(&net_server_ip, &arp->ar_data[6]); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 42 | memcpy(net_server_ethaddr, &arp->ar_data[0], 6); |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 43 | debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 44 | net_auto_load(); |
| 45 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Timeout on BOOTP request. |
| 51 | */ |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 52 | static void rarp_timeout_handler(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 53 | { |
Tom Rini | 26011a3 | 2022-03-11 09:12:02 -0500 | [diff] [blame] | 54 | if (rarp_try >= CONFIG_NET_RETRY_COUNT) { |
Joe Hershberger | c768446 | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 55 | puts("\nRetry count exceeded; starting again\n"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 56 | net_start_again(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 57 | } else { |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 58 | net_set_timeout_handler(TIMEOUT, rarp_timeout_handler); |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 59 | rarp_request(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 64 | void rarp_request(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 65 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 66 | uchar *pkt; |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 67 | struct arp_hdr *rarp; |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 68 | int eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 69 | |
Joe Hershberger | 8e805bb | 2015-04-08 01:41:11 -0500 | [diff] [blame] | 70 | printf("RARP broadcast %d\n", ++rarp_try); |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 71 | pkt = net_tx_packet; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 72 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 73 | eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP); |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 74 | pkt += eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 75 | |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 76 | rarp = (struct arp_hdr *)pkt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 77 | |
Joe Hershberger | c768446 | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 78 | rarp->ar_hrd = htons(ARP_ETHER); |
| 79 | rarp->ar_pro = htons(PROT_IP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 80 | rarp->ar_hln = 6; |
| 81 | rarp->ar_pln = 4; |
Joe Hershberger | c768446 | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 82 | rarp->ar_op = htons(RARPOP_REQUEST); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 83 | memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 84 | memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */ |
Joe Hershberger | c768446 | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 85 | /* dest ET addr = source ET addr ??*/ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 86 | memcpy(&rarp->ar_data[10], net_ethaddr, 6); |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 87 | /* dest IP addr set to broadcast */ |
| 88 | memset(&rarp->ar_data[16], 0xff, 4); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 89 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 90 | net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 91 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 92 | net_set_timeout_handler(TIMEOUT, rarp_timeout_handler); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 93 | } |