2 * Copyright (c) 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <netinet/icmp6.h>
25 #include "dpif-netdev.h"
26 #include "dynamic-string.h"
30 #include "ovs-thread.h"
32 #include "poll-loop.h"
34 #include "socket-util.h"
36 #include "tnl-arp-cache.h"
37 #include "unaligned.h"
40 #include "openvswitch/vlog.h"
44 #define ARP_ENTRY_DEFAULT_IDLE_TIME (15 * 60)
46 struct tnl_arp_entry {
47 struct cmap_node cmap_node;
50 time_t expires; /* Expiration time. */
51 char br_name[IFNAMSIZ];
54 static struct cmap table;
55 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
58 tnl_arp_hash(const struct in6_addr *ip)
60 return hash_bytes(ip->s6_addr, 16, 0);
63 static struct tnl_arp_entry *
64 tnl_arp_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
66 struct tnl_arp_entry *arp;
69 hash = tnl_arp_hash(dst);
70 CMAP_FOR_EACH_WITH_HASH (arp, cmap_node, hash, &table) {
71 if (ipv6_addr_equals(&arp->ip, dst) && !strcmp(arp->br_name, br_name)) {
72 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
80 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
83 struct tnl_arp_entry *arp;
87 in6_addr_set_mapped_ipv4(&dst6, dst);
89 arp = tnl_arp_lookup__(br_name, &dst6);
99 tnl_nd_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
100 struct eth_addr *mac)
102 struct tnl_arp_entry *arp;
105 arp = tnl_arp_lookup__(br_name, dst);
114 arp_entry_free(struct tnl_arp_entry *arp)
120 tnl_arp_delete(struct tnl_arp_entry *arp)
122 uint32_t hash = tnl_arp_hash(&arp->ip);
123 cmap_remove(&table, &arp->cmap_node, hash);
124 ovsrcu_postpone(arp_entry_free, arp);
128 tnl_arp_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
129 const struct eth_addr mac)
131 ovs_mutex_lock(&mutex);
132 struct tnl_arp_entry *arp = tnl_arp_lookup__(name, dst);
134 if (eth_addr_equals(arp->mac, mac)) {
135 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
136 ovs_mutex_unlock(&mutex);
140 seq_change(tnl_conf_seq);
143 arp = xmalloc(sizeof *arp);
147 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
148 ovs_strlcpy(arp->br_name, name, sizeof arp->br_name);
149 cmap_insert(&table, &arp->cmap_node, tnl_arp_hash(&arp->ip));
150 ovs_mutex_unlock(&mutex);
154 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
155 const struct eth_addr mac)
157 struct in6_addr dst6;
159 in6_addr_set_mapped_ipv4(&dst6, dst);
160 tnl_arp_set__(name, &dst6, mac);
164 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
165 const char name[IFNAMSIZ])
167 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
171 /* Exact Match on all ARP flows. */
172 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
173 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
174 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
176 tnl_arp_set(name, flow->nw_src, flow->arp_sha);
181 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
182 const char name[IFNAMSIZ])
184 if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
185 flow->nw_proto != IPPROTO_ICMPV6 ||
186 flow->tp_dst != htons(0) ||
187 flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
191 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
192 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
193 memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
194 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
196 tnl_arp_set__(name, &flow->nd_target, flow->arp_tha);
201 tnl_arp_cache_run(void)
203 struct tnl_arp_entry *arp;
204 bool changed = false;
206 ovs_mutex_lock(&mutex);
207 CMAP_FOR_EACH(arp, cmap_node, &table) {
208 if (arp->expires <= time_now()) {
213 ovs_mutex_unlock(&mutex);
216 seq_change(tnl_conf_seq);
221 tnl_arp_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
222 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
224 struct tnl_arp_entry *arp;
225 bool changed = false;
227 ovs_mutex_lock(&mutex);
228 CMAP_FOR_EACH(arp, cmap_node, &table) {
232 ovs_mutex_unlock(&mutex);
234 seq_change(tnl_conf_seq);
236 unixctl_command_reply(conn, "OK");
240 tnl_arp_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
241 const char *argv[], void *aux OVS_UNUSED)
243 const char *br_name = argv[1];
248 if (lookup_ip(argv[2], &ip) == 0) {
249 in6_addr_set_mapped_ipv4(&ip6, ip.s_addr);
250 } else if (lookup_ipv6(argv[2], &ip6) != 0) {
251 unixctl_command_reply_error(conn, "bad IP address");
255 if (!eth_addr_from_string(argv[3], &mac)) {
256 unixctl_command_reply_error(conn, "bad MAC address");
260 tnl_arp_set__(br_name, &ip6, mac);
261 unixctl_command_reply(conn, "OK");
265 tnl_arp_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
266 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
268 struct ds ds = DS_EMPTY_INITIALIZER;
269 struct tnl_arp_entry *arp;
271 ds_put_cstr(&ds, "IP MAC Bridge\n");
272 ds_put_cstr(&ds, "==========================================================================\n");
273 ovs_mutex_lock(&mutex);
274 CMAP_FOR_EACH(arp, cmap_node, &table) {
275 int start_len, need_ws;
277 start_len = ds.length;
278 print_ipv6_mapped(&ds, &arp->ip);
280 need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
281 ds_put_char_multiple(&ds, ' ', need_ws);
283 ds_put_format(&ds, ETH_ADDR_FMT" %s\n",
284 ETH_ADDR_ARGS(arp->mac), arp->br_name);
287 ovs_mutex_unlock(&mutex);
288 unixctl_command_reply(conn, ds_cstr(&ds));
293 tnl_arp_cache_init(void)
297 unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_arp_cache_show, NULL);
298 unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_arp_cache_add, NULL);
299 unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_arp_cache_flush, NULL);