Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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