Joe Hershberger | aae0508 | 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 |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | |
| 13 | #include "arp.h" |
| 14 | |
| 15 | #ifndef CONFIG_ARP_TIMEOUT |
| 16 | /* Milliseconds before trying ARP again */ |
| 17 | # define ARP_TIMEOUT 5000UL |
| 18 | #else |
| 19 | # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT |
| 20 | #endif |
| 21 | |
| 22 | |
| 23 | #ifndef CONFIG_NET_RETRY_COUNT |
| 24 | # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 25 | #else |
| 26 | # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT |
| 27 | #endif |
| 28 | |
| 29 | IPaddr_t NetArpWaitPacketIP; |
| 30 | IPaddr_t NetArpWaitReplyIP; |
| 31 | /* MAC address of waiting packet's destination */ |
| 32 | uchar *NetArpWaitPacketMAC; |
| 33 | /* THE transmit packet */ |
| 34 | uchar *NetArpWaitTxPacket; |
| 35 | int NetArpWaitTxPacketSize; |
| 36 | uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; |
| 37 | ulong NetArpWaitTimerStart; |
| 38 | int NetArpWaitTry; |
| 39 | |
| 40 | void ArpInit(void) |
| 41 | { |
| 42 | /* XXX problem with bss workaround */ |
| 43 | NetArpWaitPacketMAC = NULL; |
| 44 | NetArpWaitPacketIP = 0; |
| 45 | NetArpWaitReplyIP = 0; |
| 46 | NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); |
| 47 | NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; |
| 48 | NetArpWaitTxPacketSize = 0; |
| 49 | } |
| 50 | |
| 51 | void ArpRequest(void) |
| 52 | { |
| 53 | uchar *pkt; |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 54 | struct arp_hdr *arp; |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 55 | int eth_hdr_size; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 56 | |
| 57 | debug("ARP broadcast %d\n", NetArpWaitTry); |
| 58 | |
| 59 | pkt = NetTxPacket; |
| 60 | |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 61 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); |
| 62 | pkt += eth_hdr_size; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 63 | |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 64 | arp = (struct arp_hdr *) pkt; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 65 | |
| 66 | arp->ar_hrd = htons(ARP_ETHER); |
| 67 | arp->ar_pro = htons(PROT_IP); |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 68 | arp->ar_hln = ARP_HLEN; |
| 69 | arp->ar_pln = ARP_PLEN; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 70 | arp->ar_op = htons(ARPOP_REQUEST); |
| 71 | |
| 72 | /* source ET addr */ |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 73 | memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 74 | /* source IP addr */ |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 75 | NetWriteIP(&arp->ar_spa, NetOurIP); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 76 | /* dest ET addr = 0 */ |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 77 | memset(&arp->ar_tha, 0, ARP_HLEN); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 78 | if ((NetArpWaitPacketIP & NetOurSubnetMask) != |
| 79 | (NetOurIP & NetOurSubnetMask)) { |
| 80 | if (NetOurGatewayIP == 0) { |
| 81 | puts("## Warning: gatewayip needed but not set\n"); |
| 82 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 83 | } else { |
| 84 | NetArpWaitReplyIP = NetOurGatewayIP; |
| 85 | } |
| 86 | } else { |
| 87 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 88 | } |
| 89 | |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 90 | NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); |
Joe Hershberger | ebe3ca8 | 2012-05-23 07:59:13 +0000 | [diff] [blame] | 91 | NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void ArpTimeoutCheck(void) |
| 95 | { |
| 96 | ulong t; |
| 97 | |
| 98 | if (!NetArpWaitPacketIP) |
| 99 | return; |
| 100 | |
| 101 | t = get_timer(0); |
| 102 | |
| 103 | /* check for arp timeout */ |
| 104 | if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { |
| 105 | NetArpWaitTry++; |
| 106 | |
| 107 | if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { |
| 108 | puts("\nARP Retry count exceeded; starting again\n"); |
| 109 | NetArpWaitTry = 0; |
| 110 | NetStartAgain(); |
| 111 | } else { |
| 112 | NetArpWaitTimerStart = t; |
| 113 | ArpRequest(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 118 | void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 119 | { |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 120 | struct arp_hdr *arp; |
Joe Hershberger | 555efa0 | 2012-05-23 07:58:16 +0000 | [diff] [blame] | 121 | IPaddr_t reply_ip_addr; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 122 | uchar *pkt; |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 123 | int eth_hdr_size; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * We have to deal with two types of ARP packets: |
| 127 | * - REQUEST packets will be answered by sending our |
| 128 | * IP address - if we know it. |
| 129 | * - REPLY packates are expected only after we asked |
| 130 | * for the TFTP server's or the gateway's ethernet |
| 131 | * address; so if we receive such a packet, we set |
| 132 | * the server ethernet address |
| 133 | */ |
| 134 | debug("Got ARP\n"); |
| 135 | |
Joe Hershberger | de15d093 | 2012-05-23 07:58:08 +0000 | [diff] [blame] | 136 | arp = (struct arp_hdr *)ip; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 137 | if (len < ARP_HDR_SIZE) { |
| 138 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 139 | return; |
| 140 | } |
| 141 | if (ntohs(arp->ar_hrd) != ARP_ETHER) |
| 142 | return; |
| 143 | if (ntohs(arp->ar_pro) != PROT_IP) |
| 144 | return; |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 145 | if (arp->ar_hln != ARP_HLEN) |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 146 | return; |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 147 | if (arp->ar_pln != ARP_PLEN) |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | if (NetOurIP == 0) |
| 151 | return; |
| 152 | |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 153 | if (NetReadIP(&arp->ar_tpa) != NetOurIP) |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 154 | return; |
| 155 | |
| 156 | switch (ntohs(arp->ar_op)) { |
| 157 | case ARPOP_REQUEST: |
| 158 | /* reply with our IP address */ |
| 159 | debug("Got ARP REQUEST, return our IP\n"); |
| 160 | pkt = (uchar *)et; |
Joe Hershberger | f7ea805 | 2012-05-23 07:59:09 +0000 | [diff] [blame] | 161 | eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP); |
| 162 | pkt += eth_hdr_size; |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 163 | arp->ar_op = htons(ARPOP_REPLY); |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 164 | memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); |
| 165 | NetCopyIP(&arp->ar_tpa, &arp->ar_spa); |
| 166 | memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); |
| 167 | NetCopyIP(&arp->ar_spa, &NetOurIP); |
Joe Hershberger | ebe3ca8 | 2012-05-23 07:59:13 +0000 | [diff] [blame] | 168 | NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 169 | return; |
| 170 | |
| 171 | case ARPOP_REPLY: /* arp reply */ |
| 172 | /* are we waiting for a reply */ |
| 173 | if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) |
| 174 | break; |
| 175 | |
| 176 | #ifdef CONFIG_KEEP_SERVERADDR |
| 177 | if (NetServerIP == NetArpWaitPacketIP) { |
| 178 | char buf[20]; |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 179 | sprintf(buf, "%pM", arp->ar_sha); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 180 | setenv("serveraddr", buf); |
| 181 | } |
| 182 | #endif |
| 183 | |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 184 | reply_ip_addr = NetReadIP(&arp->ar_spa); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 185 | |
| 186 | /* matched waiting packet's address */ |
Joe Hershberger | 555efa0 | 2012-05-23 07:58:16 +0000 | [diff] [blame] | 187 | if (reply_ip_addr == NetArpWaitReplyIP) { |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 188 | debug("Got ARP REPLY, set eth addr (%pM)\n", |
| 189 | arp->ar_data); |
| 190 | |
| 191 | /* save address for later use */ |
| 192 | memcpy(NetArpWaitPacketMAC, |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 193 | &arp->ar_sha, ARP_HLEN); |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 194 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame^] | 195 | net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, |
| 196 | 0, len); |
| 197 | |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 198 | /* modify header, and transmit it */ |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 199 | memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> |
Joe Hershberger | 2f78749 | 2012-05-23 07:58:17 +0000 | [diff] [blame] | 200 | et_dest, NetArpWaitPacketMAC, ARP_HLEN); |
Joe Hershberger | ebe3ca8 | 2012-05-23 07:59:13 +0000 | [diff] [blame] | 201 | NetSendPacket(NetArpWaitTxPacket, |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 202 | NetArpWaitTxPacketSize); |
| 203 | |
| 204 | /* no arp request pending now */ |
| 205 | NetArpWaitPacketIP = 0; |
| 206 | NetArpWaitTxPacketSize = 0; |
| 207 | NetArpWaitPacketMAC = NULL; |
| 208 | |
| 209 | } |
| 210 | return; |
| 211 | default: |
| 212 | debug("Unexpected ARP opcode 0x%x\n", |
| 213 | ntohs(arp->ar_op)); |
| 214 | return; |
| 215 | } |
| 216 | } |