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