Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ndp_table.c
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1/*2* Copyright (c) 20133* Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.4*/56#include "slirp.h"78void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,9uint8_t ethaddr[ETH_ALEN])10{11char addrstr[INET6_ADDRSTRLEN];12NdpTable *ndp_table = &slirp->ndp_table;13int i;14char ethaddr_str[ETH_ADDRSTRLEN];1516inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);1718DEBUG_CALL("ndp_table_add");19DEBUG_ARG("ip = %s", addrstr);20DEBUG_ARG("hw addr = %s", slirp_ether_ntoa(ethaddr, ethaddr_str,21sizeof(ethaddr_str)));2223if (IN6_IS_ADDR_MULTICAST(&ip_addr) || in6_zero(&ip_addr)) {24/* Do not register multicast or unspecified addresses */25DEBUG_CALL(" abort: do not register multicast or unspecified address");26return;27}2829/* Search for an entry */30for (i = 0; i < NDP_TABLE_SIZE; i++) {31if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {32DEBUG_CALL(" already in table: update the entry");33/* Update the entry */34memcpy(ndp_table->table[i].eth_addr, ethaddr, ETH_ALEN);35return;36}37}3839/* No entry found, create a new one */40DEBUG_CALL(" create new entry");41/* Save the first entry, it is the guest. */42if (in6_zero(&ndp_table->guest_in6_addr)) {43ndp_table->guest_in6_addr = ip_addr;44}45ndp_table->table[ndp_table->next_victim].ip_addr = ip_addr;46memcpy(ndp_table->table[ndp_table->next_victim].eth_addr, ethaddr,47ETH_ALEN);48ndp_table->next_victim = (ndp_table->next_victim + 1) % NDP_TABLE_SIZE;49}5051bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,52uint8_t out_ethaddr[ETH_ALEN])53{54char addrstr[INET6_ADDRSTRLEN];55NdpTable *ndp_table = &slirp->ndp_table;56int i;57char ethaddr_str[ETH_ADDRSTRLEN];5859inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);6061DEBUG_CALL("ndp_table_search");62DEBUG_ARG("ip = %s", addrstr);6364/* If unspecified address */65if (in6_zero(&ip_addr)) {66/* return Ethernet broadcast address */67memset(out_ethaddr, 0xff, ETH_ALEN);68return 1;69}7071/* Multicast address: fec0::abcd:efgh/8 -> 33:33:ab:cd:ef:gh */72if (IN6_IS_ADDR_MULTICAST(&ip_addr)) {73out_ethaddr[0] = 0x33;74out_ethaddr[1] = 0x33;75out_ethaddr[2] = ip_addr.s6_addr[12];76out_ethaddr[3] = ip_addr.s6_addr[13];77out_ethaddr[4] = ip_addr.s6_addr[14];78out_ethaddr[5] = ip_addr.s6_addr[15];79DEBUG_ARG("multicast addr = %s",80slirp_ether_ntoa(out_ethaddr, ethaddr_str,81sizeof(ethaddr_str)));82return 1;83}8485for (i = 0; i < NDP_TABLE_SIZE; i++) {86if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {87memcpy(out_ethaddr, ndp_table->table[i].eth_addr, ETH_ALEN);88DEBUG_ARG("found hw addr = %s",89slirp_ether_ntoa(out_ethaddr, ethaddr_str,90sizeof(ethaddr_str)));91return 1;92}93}9495DEBUG_CALL(" ip not found in table");96return 0;97}9899100