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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
| 26 | #include <net.h> |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 27 | #include "nfs.h" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | #include "bootp.h" |
| 29 | #include "rarp.h" |
| 30 | #include "tftp.h" |
| 31 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 32 | #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 33 | #ifndef CONFIG_NET_RETRY_COUNT |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 34 | #define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 35 | #else |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 36 | #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 39 | int RarpTry; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Handle a RARP received packet. |
| 43 | */ |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 44 | void rarp_receive(struct ip_udp_hdr *ip, unsigned len) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 45 | { |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 46 | struct arp_hdr *arp; |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 47 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 48 | debug_cond(DEBUG_NET_PKT, "Got RARP\n"); |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 49 | arp = (struct arp_hdr *)ip; |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 50 | if (len < ARP_HDR_SIZE) { |
| 51 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if ((ntohs(arp->ar_op) != RARPOP_REPLY) || |
| 56 | (ntohs(arp->ar_hrd) != ARP_ETHER) || |
| 57 | (ntohs(arp->ar_pro) != PROT_IP) || |
| 58 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { |
| 59 | |
| 60 | puts("invalid RARP header\n"); |
| 61 | } else { |
| 62 | NetCopyIP(&NetOurIP, &arp->ar_data[16]); |
| 63 | if (NetServerIP == 0) |
| 64 | NetCopyIP(&NetServerIP, &arp->ar_data[6]); |
| 65 | memcpy(NetServerEther, &arp->ar_data[0], 6); |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 66 | debug_cond(DEBUG_DEV_PKT, "Got good RARP\n"); |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 67 | net_auto_load(); |
| 68 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * Timeout on BOOTP request. |
| 74 | */ |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 75 | static void RarpTimeout(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 76 | { |
| 77 | if (RarpTry >= TIMEOUT_COUNT) { |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 78 | puts("\nRetry count exceeded; starting again\n"); |
| 79 | NetStartAgain(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 80 | } else { |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 81 | NetSetTimeout(TIMEOUT, RarpTimeout); |
| 82 | RarpRequest(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 87 | void RarpRequest(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 88 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 89 | uchar *pkt; |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 90 | struct arp_hdr *rarp; |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 91 | int eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 92 | |
| 93 | printf("RARP broadcast %d\n", ++RarpTry); |
| 94 | pkt = NetTxPacket; |
| 95 | |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 96 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP); |
| 97 | pkt += eth_hdr_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 98 | |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 99 | rarp = (struct arp_hdr *)pkt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 100 | |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 101 | rarp->ar_hrd = htons(ARP_ETHER); |
| 102 | rarp->ar_pro = htons(PROT_IP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 103 | rarp->ar_hln = 6; |
| 104 | rarp->ar_pln = 4; |
Joe Hershberger | c2faf4f | 2012-05-15 08:59:10 +0000 | [diff] [blame] | 105 | rarp->ar_op = htons(RARPOP_REQUEST); |
| 106 | memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ |
| 107 | memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ |
| 108 | /* dest ET addr = source ET addr ??*/ |
| 109 | memcpy(&rarp->ar_data[10], NetOurEther, 6); |
Joe Hershberger | 8b9c532 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 110 | /* dest IP addr set to broadcast */ |
| 111 | memset(&rarp->ar_data[16], 0xff, 4); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 112 | |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 113 | NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 114 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 115 | NetSetTimeout(TIMEOUT, RarpTimeout); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 116 | } |