ban4jp -
/
uIP-1-0_webserver
uIP 1.0 based webserver for LPC1114 + ENC28J60
uip/uip_arp.c@0:685224d2f66d, 2014-06-14 (annotated)
- Committer:
- ban4jp
- Date:
- Sat Jun 14 16:02:21 2014 +0000
- Revision:
- 0:685224d2f66d
- Child:
- 3:a2715e9c7737
initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ban4jp | 0:685224d2f66d | 1 | /** |
ban4jp | 0:685224d2f66d | 2 | * \addtogroup uip |
ban4jp | 0:685224d2f66d | 3 | * @{ |
ban4jp | 0:685224d2f66d | 4 | */ |
ban4jp | 0:685224d2f66d | 5 | |
ban4jp | 0:685224d2f66d | 6 | /** |
ban4jp | 0:685224d2f66d | 7 | * \defgroup uiparp uIP Address Resolution Protocol |
ban4jp | 0:685224d2f66d | 8 | * @{ |
ban4jp | 0:685224d2f66d | 9 | * |
ban4jp | 0:685224d2f66d | 10 | * The Address Resolution Protocol ARP is used for mapping between IP |
ban4jp | 0:685224d2f66d | 11 | * addresses and link level addresses such as the Ethernet MAC |
ban4jp | 0:685224d2f66d | 12 | * addresses. ARP uses broadcast queries to ask for the link level |
ban4jp | 0:685224d2f66d | 13 | * address of a known IP address and the host which is configured with |
ban4jp | 0:685224d2f66d | 14 | * the IP address for which the query was meant, will respond with its |
ban4jp | 0:685224d2f66d | 15 | * link level address. |
ban4jp | 0:685224d2f66d | 16 | * |
ban4jp | 0:685224d2f66d | 17 | * \note This ARP implementation only supports Ethernet. |
ban4jp | 0:685224d2f66d | 18 | */ |
ban4jp | 0:685224d2f66d | 19 | |
ban4jp | 0:685224d2f66d | 20 | /** |
ban4jp | 0:685224d2f66d | 21 | * \file |
ban4jp | 0:685224d2f66d | 22 | * Implementation of the ARP Address Resolution Protocol. |
ban4jp | 0:685224d2f66d | 23 | * \author Adam Dunkels <adam@dunkels.com> |
ban4jp | 0:685224d2f66d | 24 | * |
ban4jp | 0:685224d2f66d | 25 | */ |
ban4jp | 0:685224d2f66d | 26 | |
ban4jp | 0:685224d2f66d | 27 | /* |
ban4jp | 0:685224d2f66d | 28 | * Copyright (c) 2001-2003, Adam Dunkels. |
ban4jp | 0:685224d2f66d | 29 | * All rights reserved. |
ban4jp | 0:685224d2f66d | 30 | * |
ban4jp | 0:685224d2f66d | 31 | * Redistribution and use in source and binary forms, with or without |
ban4jp | 0:685224d2f66d | 32 | * modification, are permitted provided that the following conditions |
ban4jp | 0:685224d2f66d | 33 | * are met: |
ban4jp | 0:685224d2f66d | 34 | * 1. Redistributions of source code must retain the above copyright |
ban4jp | 0:685224d2f66d | 35 | * notice, this list of conditions and the following disclaimer. |
ban4jp | 0:685224d2f66d | 36 | * 2. Redistributions in binary form must reproduce the above copyright |
ban4jp | 0:685224d2f66d | 37 | * notice, this list of conditions and the following disclaimer in the |
ban4jp | 0:685224d2f66d | 38 | * documentation and/or other materials provided with the distribution. |
ban4jp | 0:685224d2f66d | 39 | * 3. The name of the author may not be used to endorse or promote |
ban4jp | 0:685224d2f66d | 40 | * products derived from this software without specific prior |
ban4jp | 0:685224d2f66d | 41 | * written permission. |
ban4jp | 0:685224d2f66d | 42 | * |
ban4jp | 0:685224d2f66d | 43 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
ban4jp | 0:685224d2f66d | 44 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
ban4jp | 0:685224d2f66d | 45 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
ban4jp | 0:685224d2f66d | 46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
ban4jp | 0:685224d2f66d | 47 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
ban4jp | 0:685224d2f66d | 48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
ban4jp | 0:685224d2f66d | 49 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
ban4jp | 0:685224d2f66d | 50 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
ban4jp | 0:685224d2f66d | 51 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
ban4jp | 0:685224d2f66d | 52 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
ban4jp | 0:685224d2f66d | 53 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ban4jp | 0:685224d2f66d | 54 | * |
ban4jp | 0:685224d2f66d | 55 | * This file is part of the uIP TCP/IP stack. |
ban4jp | 0:685224d2f66d | 56 | * |
ban4jp | 0:685224d2f66d | 57 | * $Id: uip_arp.c,v 1.8 2006/06/02 23:36:21 adam Exp $ |
ban4jp | 0:685224d2f66d | 58 | * |
ban4jp | 0:685224d2f66d | 59 | */ |
ban4jp | 0:685224d2f66d | 60 | |
ban4jp | 0:685224d2f66d | 61 | |
ban4jp | 0:685224d2f66d | 62 | #include "uip_arp.h" |
ban4jp | 0:685224d2f66d | 63 | |
ban4jp | 0:685224d2f66d | 64 | #include <string.h> |
ban4jp | 0:685224d2f66d | 65 | |
ban4jp | 0:685224d2f66d | 66 | struct arp_hdr { |
ban4jp | 0:685224d2f66d | 67 | struct uip_eth_hdr ethhdr; |
ban4jp | 0:685224d2f66d | 68 | u16_t hwtype; |
ban4jp | 0:685224d2f66d | 69 | u16_t protocol; |
ban4jp | 0:685224d2f66d | 70 | u8_t hwlen; |
ban4jp | 0:685224d2f66d | 71 | u8_t protolen; |
ban4jp | 0:685224d2f66d | 72 | u16_t opcode; |
ban4jp | 0:685224d2f66d | 73 | struct uip_eth_addr shwaddr; |
ban4jp | 0:685224d2f66d | 74 | u16_t sipaddr[2]; |
ban4jp | 0:685224d2f66d | 75 | struct uip_eth_addr dhwaddr; |
ban4jp | 0:685224d2f66d | 76 | u16_t dipaddr[2]; |
ban4jp | 0:685224d2f66d | 77 | }; |
ban4jp | 0:685224d2f66d | 78 | |
ban4jp | 0:685224d2f66d | 79 | struct ethip_hdr { |
ban4jp | 0:685224d2f66d | 80 | struct uip_eth_hdr ethhdr; |
ban4jp | 0:685224d2f66d | 81 | /* IP header. */ |
ban4jp | 0:685224d2f66d | 82 | u8_t vhl, |
ban4jp | 0:685224d2f66d | 83 | tos, |
ban4jp | 0:685224d2f66d | 84 | len[2], |
ban4jp | 0:685224d2f66d | 85 | ipid[2], |
ban4jp | 0:685224d2f66d | 86 | ipoffset[2], |
ban4jp | 0:685224d2f66d | 87 | ttl, |
ban4jp | 0:685224d2f66d | 88 | proto; |
ban4jp | 0:685224d2f66d | 89 | u16_t ipchksum; |
ban4jp | 0:685224d2f66d | 90 | u16_t srcipaddr[2], |
ban4jp | 0:685224d2f66d | 91 | destipaddr[2]; |
ban4jp | 0:685224d2f66d | 92 | }; |
ban4jp | 0:685224d2f66d | 93 | |
ban4jp | 0:685224d2f66d | 94 | #define ARP_REQUEST 1 |
ban4jp | 0:685224d2f66d | 95 | #define ARP_REPLY 2 |
ban4jp | 0:685224d2f66d | 96 | |
ban4jp | 0:685224d2f66d | 97 | #define ARP_HWTYPE_ETH 1 |
ban4jp | 0:685224d2f66d | 98 | |
ban4jp | 0:685224d2f66d | 99 | struct arp_entry { |
ban4jp | 0:685224d2f66d | 100 | u16_t ipaddr[2]; |
ban4jp | 0:685224d2f66d | 101 | struct uip_eth_addr ethaddr; |
ban4jp | 0:685224d2f66d | 102 | u8_t time; |
ban4jp | 0:685224d2f66d | 103 | }; |
ban4jp | 0:685224d2f66d | 104 | |
ban4jp | 0:685224d2f66d | 105 | static const struct uip_eth_addr broadcast_ethaddr = |
ban4jp | 0:685224d2f66d | 106 | {{0xff,0xff,0xff,0xff,0xff,0xff}}; |
ban4jp | 0:685224d2f66d | 107 | static const u16_t broadcast_ipaddr[2] = {0xffff,0xffff}; |
ban4jp | 0:685224d2f66d | 108 | |
ban4jp | 0:685224d2f66d | 109 | static struct arp_entry arp_table[UIP_ARPTAB_SIZE]; |
ban4jp | 0:685224d2f66d | 110 | static u16_t ipaddr[2]; |
ban4jp | 0:685224d2f66d | 111 | static u8_t i, c; |
ban4jp | 0:685224d2f66d | 112 | |
ban4jp | 0:685224d2f66d | 113 | static u8_t arptime; |
ban4jp | 0:685224d2f66d | 114 | static u8_t tmpage; |
ban4jp | 0:685224d2f66d | 115 | |
ban4jp | 0:685224d2f66d | 116 | #define BUF ((struct arp_hdr *)&uip_buf[0]) |
ban4jp | 0:685224d2f66d | 117 | #define IPBUF ((struct ethip_hdr *)&uip_buf[0]) |
ban4jp | 0:685224d2f66d | 118 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 119 | /** |
ban4jp | 0:685224d2f66d | 120 | * Initialize the ARP module. |
ban4jp | 0:685224d2f66d | 121 | * |
ban4jp | 0:685224d2f66d | 122 | */ |
ban4jp | 0:685224d2f66d | 123 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 124 | void |
ban4jp | 0:685224d2f66d | 125 | uip_arp_init(void) |
ban4jp | 0:685224d2f66d | 126 | { |
ban4jp | 0:685224d2f66d | 127 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 128 | memset(arp_table[i].ipaddr, 0, 4); |
ban4jp | 0:685224d2f66d | 129 | } |
ban4jp | 0:685224d2f66d | 130 | } |
ban4jp | 0:685224d2f66d | 131 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 132 | /** |
ban4jp | 0:685224d2f66d | 133 | * Periodic ARP processing function. |
ban4jp | 0:685224d2f66d | 134 | * |
ban4jp | 0:685224d2f66d | 135 | * This function performs periodic timer processing in the ARP module |
ban4jp | 0:685224d2f66d | 136 | * and should be called at regular intervals. The recommended interval |
ban4jp | 0:685224d2f66d | 137 | * is 10 seconds between the calls. |
ban4jp | 0:685224d2f66d | 138 | * |
ban4jp | 0:685224d2f66d | 139 | */ |
ban4jp | 0:685224d2f66d | 140 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 141 | void |
ban4jp | 0:685224d2f66d | 142 | uip_arp_timer(void) |
ban4jp | 0:685224d2f66d | 143 | { |
ban4jp | 0:685224d2f66d | 144 | struct arp_entry *tabptr; |
ban4jp | 0:685224d2f66d | 145 | |
ban4jp | 0:685224d2f66d | 146 | ++arptime; |
ban4jp | 0:685224d2f66d | 147 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 148 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 149 | if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 && |
ban4jp | 0:685224d2f66d | 150 | arptime - tabptr->time >= UIP_ARP_MAXAGE) { |
ban4jp | 0:685224d2f66d | 151 | memset(tabptr->ipaddr, 0, 4); |
ban4jp | 0:685224d2f66d | 152 | } |
ban4jp | 0:685224d2f66d | 153 | } |
ban4jp | 0:685224d2f66d | 154 | |
ban4jp | 0:685224d2f66d | 155 | } |
ban4jp | 0:685224d2f66d | 156 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 157 | static void |
ban4jp | 0:685224d2f66d | 158 | uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr) |
ban4jp | 0:685224d2f66d | 159 | { |
ban4jp | 0:685224d2f66d | 160 | register struct arp_entry *tabptr; |
ban4jp | 0:685224d2f66d | 161 | /* Walk through the ARP mapping table and try to find an entry to |
ban4jp | 0:685224d2f66d | 162 | update. If none is found, the IP -> MAC address mapping is |
ban4jp | 0:685224d2f66d | 163 | inserted in the ARP table. */ |
ban4jp | 0:685224d2f66d | 164 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 165 | |
ban4jp | 0:685224d2f66d | 166 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 167 | /* Only check those entries that are actually in use. */ |
ban4jp | 0:685224d2f66d | 168 | if(tabptr->ipaddr[0] != 0 && |
ban4jp | 0:685224d2f66d | 169 | tabptr->ipaddr[1] != 0) { |
ban4jp | 0:685224d2f66d | 170 | |
ban4jp | 0:685224d2f66d | 171 | /* Check if the source IP address of the incoming packet matches |
ban4jp | 0:685224d2f66d | 172 | the IP address in this ARP table entry. */ |
ban4jp | 0:685224d2f66d | 173 | if(ipaddr[0] == tabptr->ipaddr[0] && |
ban4jp | 0:685224d2f66d | 174 | ipaddr[1] == tabptr->ipaddr[1]) { |
ban4jp | 0:685224d2f66d | 175 | |
ban4jp | 0:685224d2f66d | 176 | /* An old entry found, update this and return. */ |
ban4jp | 0:685224d2f66d | 177 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); |
ban4jp | 0:685224d2f66d | 178 | tabptr->time = arptime; |
ban4jp | 0:685224d2f66d | 179 | |
ban4jp | 0:685224d2f66d | 180 | return; |
ban4jp | 0:685224d2f66d | 181 | } |
ban4jp | 0:685224d2f66d | 182 | } |
ban4jp | 0:685224d2f66d | 183 | } |
ban4jp | 0:685224d2f66d | 184 | |
ban4jp | 0:685224d2f66d | 185 | /* If we get here, no existing ARP table entry was found, so we |
ban4jp | 0:685224d2f66d | 186 | create one. */ |
ban4jp | 0:685224d2f66d | 187 | |
ban4jp | 0:685224d2f66d | 188 | /* First, we try to find an unused entry in the ARP table. */ |
ban4jp | 0:685224d2f66d | 189 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 190 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 191 | if(tabptr->ipaddr[0] == 0 && |
ban4jp | 0:685224d2f66d | 192 | tabptr->ipaddr[1] == 0) { |
ban4jp | 0:685224d2f66d | 193 | break; |
ban4jp | 0:685224d2f66d | 194 | } |
ban4jp | 0:685224d2f66d | 195 | } |
ban4jp | 0:685224d2f66d | 196 | |
ban4jp | 0:685224d2f66d | 197 | /* If no unused entry is found, we try to find the oldest entry and |
ban4jp | 0:685224d2f66d | 198 | throw it away. */ |
ban4jp | 0:685224d2f66d | 199 | if(i == UIP_ARPTAB_SIZE) { |
ban4jp | 0:685224d2f66d | 200 | tmpage = 0; |
ban4jp | 0:685224d2f66d | 201 | c = 0; |
ban4jp | 0:685224d2f66d | 202 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 203 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 204 | if(arptime - tabptr->time > tmpage) { |
ban4jp | 0:685224d2f66d | 205 | tmpage = arptime - tabptr->time; |
ban4jp | 0:685224d2f66d | 206 | c = i; |
ban4jp | 0:685224d2f66d | 207 | } |
ban4jp | 0:685224d2f66d | 208 | } |
ban4jp | 0:685224d2f66d | 209 | i = c; |
ban4jp | 0:685224d2f66d | 210 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 211 | } |
ban4jp | 0:685224d2f66d | 212 | |
ban4jp | 0:685224d2f66d | 213 | /* Now, i is the ARP table entry which we will fill with the new |
ban4jp | 0:685224d2f66d | 214 | information. */ |
ban4jp | 0:685224d2f66d | 215 | memcpy(tabptr->ipaddr, ipaddr, 4); |
ban4jp | 0:685224d2f66d | 216 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); |
ban4jp | 0:685224d2f66d | 217 | tabptr->time = arptime; |
ban4jp | 0:685224d2f66d | 218 | } |
ban4jp | 0:685224d2f66d | 219 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 220 | /** |
ban4jp | 0:685224d2f66d | 221 | * ARP processing for incoming IP packets |
ban4jp | 0:685224d2f66d | 222 | * |
ban4jp | 0:685224d2f66d | 223 | * This function should be called by the device driver when an IP |
ban4jp | 0:685224d2f66d | 224 | * packet has been received. The function will check if the address is |
ban4jp | 0:685224d2f66d | 225 | * in the ARP cache, and if so the ARP cache entry will be |
ban4jp | 0:685224d2f66d | 226 | * refreshed. If no ARP cache entry was found, a new one is created. |
ban4jp | 0:685224d2f66d | 227 | * |
ban4jp | 0:685224d2f66d | 228 | * This function expects an IP packet with a prepended Ethernet header |
ban4jp | 0:685224d2f66d | 229 | * in the uip_buf[] buffer, and the length of the packet in the global |
ban4jp | 0:685224d2f66d | 230 | * variable uip_len. |
ban4jp | 0:685224d2f66d | 231 | */ |
ban4jp | 0:685224d2f66d | 232 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 233 | #if 0 |
ban4jp | 0:685224d2f66d | 234 | void |
ban4jp | 0:685224d2f66d | 235 | uip_arp_ipin(void) |
ban4jp | 0:685224d2f66d | 236 | { |
ban4jp | 0:685224d2f66d | 237 | uip_len -= sizeof(struct uip_eth_hdr); |
ban4jp | 0:685224d2f66d | 238 | |
ban4jp | 0:685224d2f66d | 239 | /* Only insert/update an entry if the source IP address of the |
ban4jp | 0:685224d2f66d | 240 | incoming IP packet comes from a host on the local network. */ |
ban4jp | 0:685224d2f66d | 241 | if((IPBUF->srcipaddr[0] & uip_netmask[0]) != |
ban4jp | 0:685224d2f66d | 242 | (uip_hostaddr[0] & uip_netmask[0])) { |
ban4jp | 0:685224d2f66d | 243 | return; |
ban4jp | 0:685224d2f66d | 244 | } |
ban4jp | 0:685224d2f66d | 245 | if((IPBUF->srcipaddr[1] & uip_netmask[1]) != |
ban4jp | 0:685224d2f66d | 246 | (uip_hostaddr[1] & uip_netmask[1])) { |
ban4jp | 0:685224d2f66d | 247 | return; |
ban4jp | 0:685224d2f66d | 248 | } |
ban4jp | 0:685224d2f66d | 249 | uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src)); |
ban4jp | 0:685224d2f66d | 250 | |
ban4jp | 0:685224d2f66d | 251 | return; |
ban4jp | 0:685224d2f66d | 252 | } |
ban4jp | 0:685224d2f66d | 253 | #endif /* 0 */ |
ban4jp | 0:685224d2f66d | 254 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 255 | /** |
ban4jp | 0:685224d2f66d | 256 | * ARP processing for incoming ARP packets. |
ban4jp | 0:685224d2f66d | 257 | * |
ban4jp | 0:685224d2f66d | 258 | * This function should be called by the device driver when an ARP |
ban4jp | 0:685224d2f66d | 259 | * packet has been received. The function will act differently |
ban4jp | 0:685224d2f66d | 260 | * depending on the ARP packet type: if it is a reply for a request |
ban4jp | 0:685224d2f66d | 261 | * that we previously sent out, the ARP cache will be filled in with |
ban4jp | 0:685224d2f66d | 262 | * the values from the ARP reply. If the incoming ARP packet is an ARP |
ban4jp | 0:685224d2f66d | 263 | * request for our IP address, an ARP reply packet is created and put |
ban4jp | 0:685224d2f66d | 264 | * into the uip_buf[] buffer. |
ban4jp | 0:685224d2f66d | 265 | * |
ban4jp | 0:685224d2f66d | 266 | * When the function returns, the value of the global variable uip_len |
ban4jp | 0:685224d2f66d | 267 | * indicates whether the device driver should send out a packet or |
ban4jp | 0:685224d2f66d | 268 | * not. If uip_len is zero, no packet should be sent. If uip_len is |
ban4jp | 0:685224d2f66d | 269 | * non-zero, it contains the length of the outbound packet that is |
ban4jp | 0:685224d2f66d | 270 | * present in the uip_buf[] buffer. |
ban4jp | 0:685224d2f66d | 271 | * |
ban4jp | 0:685224d2f66d | 272 | * This function expects an ARP packet with a prepended Ethernet |
ban4jp | 0:685224d2f66d | 273 | * header in the uip_buf[] buffer, and the length of the packet in the |
ban4jp | 0:685224d2f66d | 274 | * global variable uip_len. |
ban4jp | 0:685224d2f66d | 275 | */ |
ban4jp | 0:685224d2f66d | 276 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 277 | void |
ban4jp | 0:685224d2f66d | 278 | uip_arp_arpin(void) |
ban4jp | 0:685224d2f66d | 279 | { |
ban4jp | 0:685224d2f66d | 280 | |
ban4jp | 0:685224d2f66d | 281 | if(uip_len < sizeof(struct arp_hdr)) { |
ban4jp | 0:685224d2f66d | 282 | uip_len = 0; |
ban4jp | 0:685224d2f66d | 283 | return; |
ban4jp | 0:685224d2f66d | 284 | } |
ban4jp | 0:685224d2f66d | 285 | uip_len = 0; |
ban4jp | 0:685224d2f66d | 286 | |
ban4jp | 0:685224d2f66d | 287 | switch(BUF->opcode) { |
ban4jp | 0:685224d2f66d | 288 | case HTONS(ARP_REQUEST): |
ban4jp | 0:685224d2f66d | 289 | /* ARP request. If it asked for our address, we send out a |
ban4jp | 0:685224d2f66d | 290 | reply. */ |
ban4jp | 0:685224d2f66d | 291 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { |
ban4jp | 0:685224d2f66d | 292 | /* First, we register the one who made the request in our ARP |
ban4jp | 0:685224d2f66d | 293 | table, since it is likely that we will do more communication |
ban4jp | 0:685224d2f66d | 294 | with this host in the future. */ |
ban4jp | 0:685224d2f66d | 295 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr); |
ban4jp | 0:685224d2f66d | 296 | |
ban4jp | 0:685224d2f66d | 297 | /* The reply opcode is 2. */ |
ban4jp | 0:685224d2f66d | 298 | BUF->opcode = HTONS(2); |
ban4jp | 0:685224d2f66d | 299 | |
ban4jp | 0:685224d2f66d | 300 | memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 301 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 302 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 303 | memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 304 | |
ban4jp | 0:685224d2f66d | 305 | BUF->dipaddr[0] = BUF->sipaddr[0]; |
ban4jp | 0:685224d2f66d | 306 | BUF->dipaddr[1] = BUF->sipaddr[1]; |
ban4jp | 0:685224d2f66d | 307 | BUF->sipaddr[0] = uip_hostaddr[0]; |
ban4jp | 0:685224d2f66d | 308 | BUF->sipaddr[1] = uip_hostaddr[1]; |
ban4jp | 0:685224d2f66d | 309 | |
ban4jp | 0:685224d2f66d | 310 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); |
ban4jp | 0:685224d2f66d | 311 | uip_len = sizeof(struct arp_hdr); |
ban4jp | 0:685224d2f66d | 312 | } |
ban4jp | 0:685224d2f66d | 313 | break; |
ban4jp | 0:685224d2f66d | 314 | case HTONS(ARP_REPLY): |
ban4jp | 0:685224d2f66d | 315 | /* ARP reply. We insert or update the ARP table if it was meant |
ban4jp | 0:685224d2f66d | 316 | for us. */ |
ban4jp | 0:685224d2f66d | 317 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { |
ban4jp | 0:685224d2f66d | 318 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr); |
ban4jp | 0:685224d2f66d | 319 | } |
ban4jp | 0:685224d2f66d | 320 | break; |
ban4jp | 0:685224d2f66d | 321 | } |
ban4jp | 0:685224d2f66d | 322 | |
ban4jp | 0:685224d2f66d | 323 | return; |
ban4jp | 0:685224d2f66d | 324 | } |
ban4jp | 0:685224d2f66d | 325 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 326 | /** |
ban4jp | 0:685224d2f66d | 327 | * Prepend Ethernet header to an outbound IP packet and see if we need |
ban4jp | 0:685224d2f66d | 328 | * to send out an ARP request. |
ban4jp | 0:685224d2f66d | 329 | * |
ban4jp | 0:685224d2f66d | 330 | * This function should be called before sending out an IP packet. The |
ban4jp | 0:685224d2f66d | 331 | * function checks the destination IP address of the IP packet to see |
ban4jp | 0:685224d2f66d | 332 | * what Ethernet MAC address that should be used as a destination MAC |
ban4jp | 0:685224d2f66d | 333 | * address on the Ethernet. |
ban4jp | 0:685224d2f66d | 334 | * |
ban4jp | 0:685224d2f66d | 335 | * If the destination IP address is in the local network (determined |
ban4jp | 0:685224d2f66d | 336 | * by logical ANDing of netmask and our IP address), the function |
ban4jp | 0:685224d2f66d | 337 | * checks the ARP cache to see if an entry for the destination IP |
ban4jp | 0:685224d2f66d | 338 | * address is found. If so, an Ethernet header is prepended and the |
ban4jp | 0:685224d2f66d | 339 | * function returns. If no ARP cache entry is found for the |
ban4jp | 0:685224d2f66d | 340 | * destination IP address, the packet in the uip_buf[] is replaced by |
ban4jp | 0:685224d2f66d | 341 | * an ARP request packet for the IP address. The IP packet is dropped |
ban4jp | 0:685224d2f66d | 342 | * and it is assumed that they higher level protocols (e.g., TCP) |
ban4jp | 0:685224d2f66d | 343 | * eventually will retransmit the dropped packet. |
ban4jp | 0:685224d2f66d | 344 | * |
ban4jp | 0:685224d2f66d | 345 | * If the destination IP address is not on the local network, the IP |
ban4jp | 0:685224d2f66d | 346 | * address of the default router is used instead. |
ban4jp | 0:685224d2f66d | 347 | * |
ban4jp | 0:685224d2f66d | 348 | * When the function returns, a packet is present in the uip_buf[] |
ban4jp | 0:685224d2f66d | 349 | * buffer, and the length of the packet is in the global variable |
ban4jp | 0:685224d2f66d | 350 | * uip_len. |
ban4jp | 0:685224d2f66d | 351 | */ |
ban4jp | 0:685224d2f66d | 352 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 353 | void |
ban4jp | 0:685224d2f66d | 354 | uip_arp_out(void) |
ban4jp | 0:685224d2f66d | 355 | { |
ban4jp | 0:685224d2f66d | 356 | struct arp_entry *tabptr; |
ban4jp | 0:685224d2f66d | 357 | |
ban4jp | 0:685224d2f66d | 358 | /* Find the destination IP address in the ARP table and construct |
ban4jp | 0:685224d2f66d | 359 | the Ethernet header. If the destination IP addres isn't on the |
ban4jp | 0:685224d2f66d | 360 | local network, we use the default router's IP address instead. |
ban4jp | 0:685224d2f66d | 361 | |
ban4jp | 0:685224d2f66d | 362 | If not ARP table entry is found, we overwrite the original IP |
ban4jp | 0:685224d2f66d | 363 | packet with an ARP request for the IP address. */ |
ban4jp | 0:685224d2f66d | 364 | |
ban4jp | 0:685224d2f66d | 365 | /* First check if destination is a local broadcast. */ |
ban4jp | 0:685224d2f66d | 366 | if(uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) { |
ban4jp | 0:685224d2f66d | 367 | memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 368 | } else { |
ban4jp | 0:685224d2f66d | 369 | /* Check if the destination address is on the local network. */ |
ban4jp | 0:685224d2f66d | 370 | if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) { |
ban4jp | 0:685224d2f66d | 371 | /* Destination address was not on the local network, so we need to |
ban4jp | 0:685224d2f66d | 372 | use the default router's IP address instead of the destination |
ban4jp | 0:685224d2f66d | 373 | address when determining the MAC address. */ |
ban4jp | 0:685224d2f66d | 374 | uip_ipaddr_copy(ipaddr, uip_draddr); |
ban4jp | 0:685224d2f66d | 375 | } else { |
ban4jp | 0:685224d2f66d | 376 | /* Else, we use the destination IP address. */ |
ban4jp | 0:685224d2f66d | 377 | uip_ipaddr_copy(ipaddr, IPBUF->destipaddr); |
ban4jp | 0:685224d2f66d | 378 | } |
ban4jp | 0:685224d2f66d | 379 | |
ban4jp | 0:685224d2f66d | 380 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
ban4jp | 0:685224d2f66d | 381 | tabptr = &arp_table[i]; |
ban4jp | 0:685224d2f66d | 382 | if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) { |
ban4jp | 0:685224d2f66d | 383 | break; |
ban4jp | 0:685224d2f66d | 384 | } |
ban4jp | 0:685224d2f66d | 385 | } |
ban4jp | 0:685224d2f66d | 386 | |
ban4jp | 0:685224d2f66d | 387 | if(i == UIP_ARPTAB_SIZE) { |
ban4jp | 0:685224d2f66d | 388 | /* The destination address was not in our ARP table, so we |
ban4jp | 0:685224d2f66d | 389 | overwrite the IP packet with an ARP request. */ |
ban4jp | 0:685224d2f66d | 390 | |
ban4jp | 0:685224d2f66d | 391 | memset(BUF->ethhdr.dest.addr, 0xff, 6); |
ban4jp | 0:685224d2f66d | 392 | memset(BUF->dhwaddr.addr, 0x00, 6); |
ban4jp | 0:685224d2f66d | 393 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 394 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 395 | |
ban4jp | 0:685224d2f66d | 396 | uip_ipaddr_copy(BUF->dipaddr, ipaddr); |
ban4jp | 0:685224d2f66d | 397 | uip_ipaddr_copy(BUF->sipaddr, uip_hostaddr); |
ban4jp | 0:685224d2f66d | 398 | BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */ |
ban4jp | 0:685224d2f66d | 399 | BUF->hwtype = HTONS(ARP_HWTYPE_ETH); |
ban4jp | 0:685224d2f66d | 400 | BUF->protocol = HTONS(UIP_ETHTYPE_IP); |
ban4jp | 0:685224d2f66d | 401 | BUF->hwlen = 6; |
ban4jp | 0:685224d2f66d | 402 | BUF->protolen = 4; |
ban4jp | 0:685224d2f66d | 403 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); |
ban4jp | 0:685224d2f66d | 404 | |
ban4jp | 0:685224d2f66d | 405 | uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; |
ban4jp | 0:685224d2f66d | 406 | |
ban4jp | 0:685224d2f66d | 407 | uip_len = sizeof(struct arp_hdr); |
ban4jp | 0:685224d2f66d | 408 | return; |
ban4jp | 0:685224d2f66d | 409 | } |
ban4jp | 0:685224d2f66d | 410 | |
ban4jp | 0:685224d2f66d | 411 | /* Build an ethernet header. */ |
ban4jp | 0:685224d2f66d | 412 | memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 413 | } |
ban4jp | 0:685224d2f66d | 414 | memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
ban4jp | 0:685224d2f66d | 415 | |
ban4jp | 0:685224d2f66d | 416 | IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP); |
ban4jp | 0:685224d2f66d | 417 | |
ban4jp | 0:685224d2f66d | 418 | uip_len += sizeof(struct uip_eth_hdr); |
ban4jp | 0:685224d2f66d | 419 | } |
ban4jp | 0:685224d2f66d | 420 | /*-----------------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 421 | |
ban4jp | 0:685224d2f66d | 422 | /** @} */ |
ban4jp | 0:685224d2f66d | 423 | /** @} */ |