wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <net.h> |
wdenk | c40b295 | 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" |
| 14 | #include "tftp.h" |
| 15 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 16 | #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 17 | #ifndef CONFIG_NET_RETRY_COUNT |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 18 | #define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 19 | #else |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 20 | #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 23 | int RarpTry; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Handle a RARP received packet. |
| 27 | */ |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 28 | void rarp_receive(struct ip_udp_hdr *ip, unsigned len) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 29 | { |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 30 | struct arp_hdr *arp; |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 31 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 32 | debug_cond(DEBUG_NET_PKT, "Got RARP\n"); |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 33 | arp = (struct arp_hdr *)ip; |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 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 { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 46 | net_copy_ip(&net_ip, &arp->ar_data[16]); |
| 47 | if (net_server_ip.s_addr == 0) |
| 48 | net_copy_ip(&net_server_ip, &arp->ar_data[6]); |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 49 | memcpy(NetServerEther, &arp->ar_data[0], 6); |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 50 | debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 51 | net_auto_load(); |
| 52 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * Timeout on BOOTP request. |
| 58 | */ |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 59 | static void RarpTimeout(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 60 | { |
| 61 | if (RarpTry >= TIMEOUT_COUNT) { |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 62 | puts("\nRetry count exceeded; starting again\n"); |
| 63 | NetStartAgain(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 64 | } else { |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 65 | NetSetTimeout(TIMEOUT, RarpTimeout); |
| 66 | RarpRequest(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 71 | void RarpRequest(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 72 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 73 | uchar *pkt; |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 74 | struct arp_hdr *rarp; |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 75 | int eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 76 | |
| 77 | printf("RARP broadcast %d\n", ++RarpTry); |
| 78 | pkt = NetTxPacket; |
| 79 | |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 80 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); |
| 81 | pkt += eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 82 | |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 83 | rarp = (struct arp_hdr *)pkt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 84 | |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 85 | rarp->ar_hrd = htons(ARP_ETHER); |
| 86 | rarp->ar_pro = htons(PROT_IP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 87 | rarp->ar_hln = 6; |
| 88 | rarp->ar_pln = 4; |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 89 | rarp->ar_op = htons(RARPOP_REQUEST); |
| 90 | memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 91 | memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */ |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 92 | /* dest ET addr = source ET addr ??*/ |
| 93 | memcpy(&rarp->ar_data[10], NetOurEther, 6); |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 94 | /* dest IP addr set to broadcast */ |
| 95 | memset(&rarp->ar_data[16], 0xff, 4); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 96 | |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 97 | NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 98 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 99 | NetSetTimeout(TIMEOUT, RarpTimeout); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 100 | } |