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