1/* SPDX-License-Identifier: BSD-3-Clause */
2/*
3 * Copyright (c) 2013
4 * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
5 */
6
7#include "slirp.h"
8
9void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
10 uint8_t ethaddr[ETH_ALEN])
11{
12 char addrstr[INET6_ADDRSTRLEN];
13 NdpTable *ndp_table = &slirp->ndp_table;
14 int i;
15
16 inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
17
18 DEBUG_CALL("ndp_table_add");
19 DEBUG_ARG("ip = %s", addrstr);
20 DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x", ethaddr[0], ethaddr[1],
21 ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]);
22
23 if (IN6_IS_ADDR_MULTICAST(&ip_addr) || in6_zero(&ip_addr)) {
24 /* Do not register multicast or unspecified addresses */
25 DEBUG_CALL(" abort: do not register multicast or unspecified address");
26 return;
27 }
28
29 /* Search for an entry */
30 for (i = 0; i < NDP_TABLE_SIZE; i++) {
31 if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
32 DEBUG_CALL(" already in table: update the entry");
33 /* Update the entry */
34 memcpy(ndp_table->table[i].eth_addr, ethaddr, ETH_ALEN);
35 return;
36 }
37 }
38
39 /* No entry found, create a new one */
40 DEBUG_CALL(" create new entry");
41 ndp_table->table[ndp_table->next_victim].ip_addr = ip_addr;
42 memcpy(ndp_table->table[ndp_table->next_victim].eth_addr, ethaddr,
43 ETH_ALEN);
44 ndp_table->next_victim = (ndp_table->next_victim + 1) % NDP_TABLE_SIZE;
45}
46
47bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
48 uint8_t out_ethaddr[ETH_ALEN])
49{
50 char addrstr[INET6_ADDRSTRLEN];
51 NdpTable *ndp_table = &slirp->ndp_table;
52 int i;
53
54 inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
55
56 DEBUG_CALL("ndp_table_search");
57 DEBUG_ARG("ip = %s", addrstr);
58
59 assert(!in6_zero(&ip_addr));
60
61 /* Multicast address: fec0::abcd:efgh/8 -> 33:33:ab:cd:ef:gh */
62 if (IN6_IS_ADDR_MULTICAST(&ip_addr)) {
63 out_ethaddr[0] = 0x33;
64 out_ethaddr[1] = 0x33;
65 out_ethaddr[2] = ip_addr.s6_addr[12];
66 out_ethaddr[3] = ip_addr.s6_addr[13];
67 out_ethaddr[4] = ip_addr.s6_addr[14];
68 out_ethaddr[5] = ip_addr.s6_addr[15];
69 DEBUG_ARG("multicast addr = %02x:%02x:%02x:%02x:%02x:%02x",
70 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
71 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
72 return 1;
73 }
74
75 for (i = 0; i < NDP_TABLE_SIZE; i++) {
76 if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
77 memcpy(out_ethaddr, ndp_table->table[i].eth_addr, ETH_ALEN);
78 DEBUG_ARG("found hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
79 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
80 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
81 return 1;
82 }
83 }
84
85 DEBUG_CALL(" ip not found in table");
86 return 0;
87}
88