1 /////////////////////////////////////////////////////////////////////////
2 // $Id: arp_table.cc 12269 2014-04-02 17:38:09Z vruppert $
3 /////////////////////////////////////////////////////////////////////////
4 /*
5 * ARP table
6 *
7 * Copyright (c) 2011 AdaCore
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include "slirp.h"
29
30 #if BX_NETWORKING && BX_NETMOD_SLIRP
31
arp_table_add(Slirp * slirp,uint32_t ip_addr,uint8_t ethaddr[ETH_ALEN])32 void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
33 {
34 const uint32_t broadcast_addr =
35 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
36 ArpTable *arptbl = &slirp->arp_table;
37 int i;
38
39 DEBUG_CALL("arp_table_add");
40 DEBUG_ARG("ip = 0x%x", ip_addr);
41 DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
42 ethaddr[0], ethaddr[1], ethaddr[2],
43 ethaddr[3], ethaddr[4], ethaddr[5]));
44
45 /* Check 0.0.0.0/8 invalid source-only addresses */
46 if ((ip_addr & htonl(~(0xfU << 28))) == 0) {
47 return;
48 }
49
50 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
51 /* Do not register broadcast addresses */
52 return;
53 }
54
55 /* Search for an entry */
56 for (i = 0; i < ARP_TABLE_SIZE; i++) {
57 if (arptbl->table[i].ar_sip == ip_addr) {
58 /* Update the entry */
59 memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
60 return;
61 }
62 }
63
64 /* No entry found, create a new one */
65 arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
66 memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN);
67 arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
68 }
69
arp_table_search(Slirp * slirp,uint32_t ip_addr,uint8_t out_ethaddr[ETH_ALEN])70 bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
71 uint8_t out_ethaddr[ETH_ALEN])
72 {
73 const uint32_t broadcast_addr =
74 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
75 ArpTable *arptbl = &slirp->arp_table;
76 int i;
77
78 DEBUG_CALL("arp_table_search");
79 DEBUG_ARG("ip = 0x%x", ip_addr);
80
81 /* Check 0.0.0.0/8 invalid source-only addresses */
82 assert((ip_addr & htonl(~(0xfU << 28))) != 0);
83
84 /* If broadcast address */
85 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
86 /* return Ethernet broadcast address */
87 memset(out_ethaddr, 0xff, ETH_ALEN);
88 return 1;
89 }
90
91 for (i = 0; i < ARP_TABLE_SIZE; i++) {
92 if (arptbl->table[i].ar_sip == ip_addr) {
93 memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN);
94 DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
95 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
96 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]));
97 return 1;
98 }
99 }
100
101 return 0;
102 }
103
104 #endif
105