d817d663959097c5d3f2af161a3b0524ba071738
[cascardo/ovs.git] / lib / tnl-arp-cache.c
1 /*
2  * Copyright (c) 2014, 2015 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include "tnl-arp-cache.h"
20
21 #include <inttypes.h>
22 #include <netinet/icmp6.h>
23 #include <stdlib.h>
24
25 #include "bitmap.h"
26 #include "cmap.h"
27 #include "coverage.h"
28 #include "dpif-netdev.h"
29 #include "dynamic-string.h"
30 #include "errno.h"
31 #include "flow.h"
32 #include "netdev.h"
33 #include "ovs-thread.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "seq.h"
37 #include "socket-util.h"
38 #include "timeval.h"
39 #include "unaligned.h"
40 #include "unixctl.h"
41 #include "util.h"
42 #include "openvswitch/vlog.h"
43
44
45 /* In seconds */
46 #define ARP_ENTRY_DEFAULT_IDLE_TIME  (15 * 60)
47
48 struct tnl_arp_entry {
49     struct cmap_node cmap_node;
50     struct in6_addr ip;
51     struct eth_addr mac;
52     time_t expires;             /* Expiration time. */
53     char br_name[IFNAMSIZ];
54 };
55
56 static struct cmap table;
57 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
58
59 static uint32_t
60 tnl_arp_hash(const struct in6_addr *ip)
61 {
62     return hash_bytes(ip->s6_addr, 16, 0);
63 }
64
65 static struct tnl_arp_entry *
66 tnl_arp_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
67 {
68     struct tnl_arp_entry *arp;
69     uint32_t hash;
70
71     hash = tnl_arp_hash(dst);
72     CMAP_FOR_EACH_WITH_HASH (arp, cmap_node, hash, &table) {
73         if (ipv6_addr_equals(&arp->ip, dst) && !strcmp(arp->br_name, br_name)) {
74             arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
75             return arp;
76         }
77     }
78     return NULL;
79 }
80
81 int
82 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
83                struct eth_addr *mac)
84 {
85     struct tnl_arp_entry *arp;
86     int res = ENOENT;
87     struct in6_addr dst6;
88
89     in6_addr_set_mapped_ipv4(&dst6, dst);
90
91     arp = tnl_arp_lookup__(br_name, &dst6);
92     if (arp) {
93         *mac = arp->mac;
94         res = 0;
95     }
96
97     return res;
98 }
99
100 int
101 tnl_nd_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
102               struct eth_addr *mac)
103 {
104     struct tnl_arp_entry *arp;
105     int res = ENOENT;
106
107     arp = tnl_arp_lookup__(br_name, dst);
108     if (arp) {
109         *mac = arp->mac;
110         res = 0;
111     }
112     return res;
113 }
114
115 static void
116 arp_entry_free(struct tnl_arp_entry *arp)
117 {
118     free(arp);
119 }
120
121 static void
122 tnl_arp_delete(struct tnl_arp_entry *arp)
123 {
124     uint32_t hash = tnl_arp_hash(&arp->ip);
125     cmap_remove(&table, &arp->cmap_node, hash);
126     ovsrcu_postpone(arp_entry_free, arp);
127 }
128
129 static void
130 tnl_arp_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
131               const struct eth_addr mac)
132 {
133     ovs_mutex_lock(&mutex);
134     struct tnl_arp_entry *arp = tnl_arp_lookup__(name, dst);
135     if (arp) {
136         if (eth_addr_equals(arp->mac, mac)) {
137             arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
138             ovs_mutex_unlock(&mutex);
139             return;
140         }
141         tnl_arp_delete(arp);
142         seq_change(tnl_conf_seq);
143     }
144
145     arp = xmalloc(sizeof *arp);
146
147     arp->ip = *dst;
148     arp->mac = mac;
149     arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
150     ovs_strlcpy(arp->br_name, name, sizeof arp->br_name);
151     cmap_insert(&table, &arp->cmap_node, tnl_arp_hash(&arp->ip));
152     ovs_mutex_unlock(&mutex);
153 }
154
155 static void
156 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
157             const struct eth_addr mac)
158 {
159     struct in6_addr dst6;
160
161     in6_addr_set_mapped_ipv4(&dst6, dst);
162     tnl_arp_set__(name, &dst6, mac);
163 }
164
165 int
166 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
167               const char name[IFNAMSIZ])
168 {
169     if (flow->dl_type != htons(ETH_TYPE_ARP)) {
170         return EINVAL;
171     }
172
173     /* Exact Match on all ARP flows. */
174     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
175     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
176     memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
177
178     tnl_arp_set(name, flow->nw_src, flow->arp_sha);
179     return 0;
180 }
181
182 int
183 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
184               const char name[IFNAMSIZ])
185 {
186     if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
187         flow->nw_proto != IPPROTO_ICMPV6 ||
188         flow->tp_dst != htons(0) ||
189         flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
190         return EINVAL;
191     }
192
193     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
194     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
195     memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
196     memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
197
198     tnl_arp_set__(name, &flow->nd_target, flow->arp_tha);
199     return 0;
200 }
201
202 void
203 tnl_arp_cache_run(void)
204 {
205     struct tnl_arp_entry *arp;
206     bool changed = false;
207
208     ovs_mutex_lock(&mutex);
209     CMAP_FOR_EACH(arp, cmap_node, &table) {
210         if (arp->expires <= time_now()) {
211             tnl_arp_delete(arp);
212             changed = true;
213         }
214     }
215     ovs_mutex_unlock(&mutex);
216
217     if (changed) {
218         seq_change(tnl_conf_seq);
219     }
220 }
221
222 static void
223 tnl_arp_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
224                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
225 {
226     struct tnl_arp_entry *arp;
227     bool changed = false;
228
229     ovs_mutex_lock(&mutex);
230     CMAP_FOR_EACH(arp, cmap_node, &table) {
231         tnl_arp_delete(arp);
232         changed = true;
233     }
234     ovs_mutex_unlock(&mutex);
235     if (changed) {
236         seq_change(tnl_conf_seq);
237     }
238     unixctl_command_reply(conn, "OK");
239 }
240
241 static int
242 lookup_any(const char *host_name, struct in6_addr *address)
243 {
244     if (addr_is_ipv6(host_name)) {
245         return lookup_ipv6(host_name, address);
246     } else {
247         int r;
248         struct in_addr ip;
249         r = lookup_ip(host_name, &ip);
250         if (r == 0) {
251             in6_addr_set_mapped_ipv4(address, ip.s_addr);
252         }
253         return r;
254     }
255     return ENOENT;
256 }
257
258 static void
259 tnl_arp_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
260                   const char *argv[], void *aux OVS_UNUSED)
261 {
262     const char *br_name = argv[1];
263     struct eth_addr mac;
264     struct in6_addr ip6;
265
266     if (lookup_any(argv[2], &ip6) != 0) {
267         unixctl_command_reply_error(conn, "bad IP address");
268         return;
269     }
270
271     if (!eth_addr_from_string(argv[3], &mac)) {
272         unixctl_command_reply_error(conn, "bad MAC address");
273         return;
274     }
275
276     tnl_arp_set__(br_name, &ip6, mac);
277     unixctl_command_reply(conn, "OK");
278 }
279
280 static void
281 tnl_arp_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
282                    const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
283 {
284     struct ds ds = DS_EMPTY_INITIALIZER;
285     struct tnl_arp_entry *arp;
286
287     ds_put_cstr(&ds, "IP                                            MAC                 Bridge\n");
288     ds_put_cstr(&ds, "==========================================================================\n");
289     ovs_mutex_lock(&mutex);
290     CMAP_FOR_EACH(arp, cmap_node, &table) {
291         int start_len, need_ws;
292
293         start_len = ds.length;
294         print_ipv6_mapped(&ds, &arp->ip);
295
296         need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
297         ds_put_char_multiple(&ds, ' ', need_ws);
298
299         ds_put_format(&ds, ETH_ADDR_FMT"   %s\n",
300                       ETH_ADDR_ARGS(arp->mac), arp->br_name);
301
302     }
303     ovs_mutex_unlock(&mutex);
304     unixctl_command_reply(conn, ds_cstr(&ds));
305     ds_destroy(&ds);
306 }
307
308 void
309 tnl_arp_cache_init(void)
310 {
311     cmap_init(&table);
312
313     unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_arp_cache_show, NULL);
314     unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_arp_cache_add, NULL);
315     unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_arp_cache_flush, NULL);
316 }