nbd: Permit simple error to NBD_CMD_BLOCK_STATUS
[qemu/ericb.git] / slirp / src / ndp_table.c
blob78324877e26f4b64e5efac4582810e824c8c2c34
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3 * Copyright (c) 2013
4 * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
5 */
7 #include "slirp.h"
9 void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
10 uint8_t ethaddr[ETH_ALEN])
12 char addrstr[INET6_ADDRSTRLEN];
13 NdpTable *ndp_table = &slirp->ndp_table;
14 int i;
16 inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
18 DEBUG_CALL("ndp_table_add");
19 DEBUG_ARG("ip = %s", addrstr);
20 DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
21 ethaddr[0], ethaddr[1], ethaddr[2],
22 ethaddr[3], ethaddr[4], ethaddr[5]);
24 if (IN6_IS_ADDR_MULTICAST(&ip_addr) || in6_zero(&ip_addr)) {
25 /* Do not register multicast or unspecified addresses */
26 DEBUG_CALL(" abort: do not register multicast or unspecified address");
27 return;
30 /* Search for an entry */
31 for (i = 0; i < NDP_TABLE_SIZE; i++) {
32 if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
33 DEBUG_CALL(" already in table: update the entry");
34 /* Update the entry */
35 memcpy(ndp_table->table[i].eth_addr, ethaddr, ETH_ALEN);
36 return;
40 /* No entry found, create a new one */
41 DEBUG_CALL(" create new entry");
42 ndp_table->table[ndp_table->next_victim].ip_addr = ip_addr;
43 memcpy(ndp_table->table[ndp_table->next_victim].eth_addr,
44 ethaddr, ETH_ALEN);
45 ndp_table->next_victim = (ndp_table->next_victim + 1) % NDP_TABLE_SIZE;
48 bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
49 uint8_t out_ethaddr[ETH_ALEN])
51 char addrstr[INET6_ADDRSTRLEN];
52 NdpTable *ndp_table = &slirp->ndp_table;
53 int i;
55 inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
57 DEBUG_CALL("ndp_table_search");
58 DEBUG_ARG("ip = %s", addrstr);
60 assert(!in6_zero(&ip_addr));
62 /* Multicast address: fec0::abcd:efgh/8 -> 33:33:ab:cd:ef:gh */
63 if (IN6_IS_ADDR_MULTICAST(&ip_addr)) {
64 out_ethaddr[0] = 0x33; 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;
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;
85 DEBUG_CALL(" ip not found in table");
86 return 0;