Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copied from Linux Monitor (LiMon) - Networking. |
| 3 | * |
| 4 | * Copyright 1994 - 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2002 Wolfgang Denk, wd@denx.de |
Wolfgang Denk | 2ea9103 | 2014-09-30 10:44:01 +0200 | [diff] [blame^] | 9 | * SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | |
| 14 | #include "arp.h" |
| 15 | |
| 16 | #ifndef CONFIG_ARP_TIMEOUT |
| 17 | /* Milliseconds before trying ARP again */ |
| 18 | # define ARP_TIMEOUT 5000UL |
| 19 | #else |
| 20 | # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT |
| 21 | #endif |
| 22 | |
| 23 | |
| 24 | #ifndef CONFIG_NET_RETRY_COUNT |
| 25 | # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 26 | #else |
| 27 | # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT |
| 28 | #endif |
| 29 | |
| 30 | IPaddr_t NetArpWaitPacketIP; |
Kim Phillips | 0637059 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 31 | static IPaddr_t NetArpWaitReplyIP; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 32 | /* MAC address of waiting packet's destination */ |
| 33 | uchar *NetArpWaitPacketMAC; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 34 | int NetArpWaitTxPacketSize; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 35 | ulong NetArpWaitTimerStart; |
| 36 | int NetArpWaitTry; |
| 37 | |
Kim Phillips | 0637059 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 38 | static uchar *NetArpTxPacket; /* THE ARP transmit packet */ |
| 39 | static uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; |
Joe Hershberger | e94070c | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 40 | |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 41 | void ArpInit(void) |
| 42 | { |
| 43 | /* XXX problem with bss workaround */ |
| 44 | NetArpWaitPacketMAC = NULL; |
| 45 | NetArpWaitPacketIP = 0; |
| 46 | NetArpWaitReplyIP = 0; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 47 | NetArpWaitTxPacketSize = 0; |
Joe Hershberger | e94070c | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 48 | NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1); |
| 49 | NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Joe Hershberger | 2280418 | 2012-05-23 08:00:11 +0000 | [diff] [blame] | 52 | void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, |
| 53 | IPaddr_t targetIP) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 54 | { |
| 55 | uchar *pkt; |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 56 | struct arp_hdr *arp; |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 57 | int eth_hdr_size; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 58 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 59 | debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 60 | |
Joe Hershberger | e94070c | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 61 | pkt = NetArpTxPacket; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 62 | |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 63 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); |
| 64 | pkt += eth_hdr_size; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 65 | |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 66 | arp = (struct arp_hdr *) pkt; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 67 | |
| 68 | arp->ar_hrd = htons(ARP_ETHER); |
| 69 | arp->ar_pro = htons(PROT_IP); |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 70 | arp->ar_hln = ARP_HLEN; |
| 71 | arp->ar_pln = ARP_PLEN; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 72 | arp->ar_op = htons(ARPOP_REQUEST); |
| 73 | |
Joe Hershberger | 2280418 | 2012-05-23 08:00:11 +0000 | [diff] [blame] | 74 | memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */ |
| 75 | NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */ |
| 76 | memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */ |
| 77 | NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */ |
| 78 | |
| 79 | NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); |
| 80 | } |
| 81 | |
| 82 | void ArpRequest(void) |
| 83 | { |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 84 | if ((NetArpWaitPacketIP & NetOurSubnetMask) != |
| 85 | (NetOurIP & NetOurSubnetMask)) { |
| 86 | if (NetOurGatewayIP == 0) { |
| 87 | puts("## Warning: gatewayip needed but not set\n"); |
| 88 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 89 | } else { |
| 90 | NetArpWaitReplyIP = NetOurGatewayIP; |
| 91 | } |
| 92 | } else { |
| 93 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 94 | } |
| 95 | |
Joe Hershberger | 2280418 | 2012-05-23 08:00:11 +0000 | [diff] [blame] | 96 | arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void ArpTimeoutCheck(void) |
| 100 | { |
| 101 | ulong t; |
| 102 | |
| 103 | if (!NetArpWaitPacketIP) |
| 104 | return; |
| 105 | |
| 106 | t = get_timer(0); |
| 107 | |
| 108 | /* check for arp timeout */ |
| 109 | if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { |
| 110 | NetArpWaitTry++; |
| 111 | |
| 112 | if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { |
| 113 | puts("\nARP Retry count exceeded; starting again\n"); |
| 114 | NetArpWaitTry = 0; |
| 115 | NetStartAgain(); |
| 116 | } else { |
| 117 | NetArpWaitTimerStart = t; |
| 118 | ArpRequest(); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Joe Hershberger | cb487f5 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 123 | void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 124 | { |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 125 | struct arp_hdr *arp; |
Joe Hershberger | 1256793 | 2012-05-23 07:58:16 +0000 | [diff] [blame] | 126 | IPaddr_t reply_ip_addr; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 127 | uchar *pkt; |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 128 | int eth_hdr_size; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * We have to deal with two types of ARP packets: |
| 132 | * - REQUEST packets will be answered by sending our |
| 133 | * IP address - if we know it. |
| 134 | * - REPLY packates are expected only after we asked |
| 135 | * for the TFTP server's or the gateway's ethernet |
| 136 | * address; so if we receive such a packet, we set |
| 137 | * the server ethernet address |
| 138 | */ |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 139 | debug_cond(DEBUG_NET_PKT, "Got ARP\n"); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 140 | |
Joe Hershberger | 738853b | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 141 | arp = (struct arp_hdr *)ip; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 142 | if (len < ARP_HDR_SIZE) { |
| 143 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 144 | return; |
| 145 | } |
| 146 | if (ntohs(arp->ar_hrd) != ARP_ETHER) |
| 147 | return; |
| 148 | if (ntohs(arp->ar_pro) != PROT_IP) |
| 149 | return; |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 150 | if (arp->ar_hln != ARP_HLEN) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 151 | return; |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 152 | if (arp->ar_pln != ARP_PLEN) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
| 155 | if (NetOurIP == 0) |
| 156 | return; |
| 157 | |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 158 | if (NetReadIP(&arp->ar_tpa) != NetOurIP) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 159 | return; |
| 160 | |
| 161 | switch (ntohs(arp->ar_op)) { |
| 162 | case ARPOP_REQUEST: |
| 163 | /* reply with our IP address */ |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 164 | debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 165 | pkt = (uchar *)et; |
Joe Hershberger | e711101 | 2012-05-23 07:59:16 +0000 | [diff] [blame] | 166 | eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); |
Joe Hershberger | 00f3326 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 167 | pkt += eth_hdr_size; |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 168 | arp->ar_op = htons(ARPOP_REPLY); |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 169 | memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); |
| 170 | NetCopyIP(&arp->ar_tpa, &arp->ar_spa); |
| 171 | memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); |
| 172 | NetCopyIP(&arp->ar_spa, &NetOurIP); |
Joe Hershberger | c697576 | 2012-05-23 08:00:13 +0000 | [diff] [blame] | 173 | |
| 174 | #ifdef CONFIG_CMD_LINK_LOCAL |
| 175 | /* |
| 176 | * Work-around for brain-damaged Cisco equipment with |
| 177 | * arp-proxy enabled. |
| 178 | * |
| 179 | * If the requesting IP is not on our subnet, wait 5ms to |
| 180 | * reply to ARP request so that our reply will overwrite |
| 181 | * the arp-proxy's instead of the other way around. |
| 182 | */ |
| 183 | if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) != |
| 184 | (NetReadIP(&arp->ar_spa) & NetOurSubnetMask)) |
| 185 | udelay(5000); |
| 186 | #endif |
Joe Hershberger | adf5d93 | 2012-05-23 07:59:13 +0000 | [diff] [blame] | 187 | NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 188 | return; |
| 189 | |
| 190 | case ARPOP_REPLY: /* arp reply */ |
| 191 | /* are we waiting for a reply */ |
Joe Hershberger | f1d2d28 | 2012-05-23 07:59:20 +0000 | [diff] [blame] | 192 | if (!NetArpWaitPacketIP) |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 193 | break; |
| 194 | |
| 195 | #ifdef CONFIG_KEEP_SERVERADDR |
| 196 | if (NetServerIP == NetArpWaitPacketIP) { |
| 197 | char buf[20]; |
Mike Frysinger | a0bc44e | 2012-07-10 00:41:50 +0000 | [diff] [blame] | 198 | sprintf(buf, "%pM", &arp->ar_sha); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 199 | setenv("serveraddr", buf); |
| 200 | } |
| 201 | #endif |
| 202 | |
Joe Hershberger | 674bb24 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 203 | reply_ip_addr = NetReadIP(&arp->ar_spa); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 204 | |
| 205 | /* matched waiting packet's address */ |
Joe Hershberger | 1256793 | 2012-05-23 07:58:16 +0000 | [diff] [blame] | 206 | if (reply_ip_addr == NetArpWaitReplyIP) { |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 207 | debug_cond(DEBUG_DEV_PKT, |
| 208 | "Got ARP REPLY, set eth addr (%pM)\n", |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 209 | arp->ar_data); |
| 210 | |
| 211 | /* save address for later use */ |
Joe Hershberger | f1d2d28 | 2012-05-23 07:59:20 +0000 | [diff] [blame] | 212 | if (NetArpWaitPacketMAC != NULL) |
| 213 | memcpy(NetArpWaitPacketMAC, |
| 214 | &arp->ar_sha, ARP_HLEN); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 215 | |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 216 | net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, |
| 217 | 0, len); |
| 218 | |
Joe Hershberger | e94070c | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 219 | /* set the mac address in the waiting packet's header |
| 220 | and transmit it */ |
| 221 | memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest, |
| 222 | &arp->ar_sha, ARP_HLEN); |
| 223 | NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize); |
Joe Hershberger | d280d3f | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 224 | |
| 225 | /* no arp request pending now */ |
| 226 | NetArpWaitPacketIP = 0; |
| 227 | NetArpWaitTxPacketSize = 0; |
| 228 | NetArpWaitPacketMAC = NULL; |
| 229 | |
| 230 | } |
| 231 | return; |
| 232 | default: |
| 233 | debug("Unexpected ARP opcode 0x%x\n", |
| 234 | ntohs(arp->ar_op)); |
| 235 | return; |
| 236 | } |
| 237 | } |