tnl-arp-cache: add IPv6 Neighbor Discovery support
[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 #include <inttypes.h>
19 #include <netinet/icmp6.h>
20 #include <stdlib.h>
21
22 #include "bitmap.h"
23 #include "cmap.h"
24 #include "coverage.h"
25 #include "dpif-netdev.h"
26 #include "dynamic-string.h"
27 #include "errno.h"
28 #include "flow.h"
29 #include "netdev.h"
30 #include "ovs-thread.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "seq.h"
34 #include "socket-util.h"
35 #include "timeval.h"
36 #include "tnl-arp-cache.h"
37 #include "unaligned.h"
38 #include "unixctl.h"
39 #include "util.h"
40 #include "openvswitch/vlog.h"
41
42
43 /* In seconds */
44 #define ARP_ENTRY_DEFAULT_IDLE_TIME  (15 * 60)
45
46 struct tnl_arp_entry {
47     struct cmap_node cmap_node;
48     struct in6_addr ip;
49     struct eth_addr mac;
50     time_t expires;             /* Expiration time. */
51     char br_name[IFNAMSIZ];
52 };
53
54 static struct cmap table;
55 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
56
57 static uint32_t
58 tnl_arp_hash(const struct in6_addr *ip)
59 {
60     return hash_bytes(ip->s6_addr, 16, 0);
61 }
62
63 static struct tnl_arp_entry *
64 tnl_arp_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
65 {
66     struct tnl_arp_entry *arp;
67     uint32_t hash;
68
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;
73             return arp;
74         }
75     }
76     return NULL;
77 }
78
79 int
80 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
81                struct eth_addr *mac)
82 {
83     struct tnl_arp_entry *arp;
84     int res = ENOENT;
85     struct in6_addr dst6;
86
87     in6_addr_set_mapped_ipv4(&dst6, dst);
88
89     arp = tnl_arp_lookup__(br_name, &dst6);
90     if (arp) {
91         *mac = arp->mac;
92         res = 0;
93     }
94
95     return res;
96 }
97
98 int
99 tnl_nd_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
100               struct eth_addr *mac)
101 {
102     struct tnl_arp_entry *arp;
103     int res = ENOENT;
104
105     arp = tnl_arp_lookup__(br_name, dst);
106     if (arp) {
107         *mac = arp->mac;
108         res = 0;
109     }
110     return res;
111 }
112
113 static void
114 arp_entry_free(struct tnl_arp_entry *arp)
115 {
116     free(arp);
117 }
118
119 static void
120 tnl_arp_delete(struct tnl_arp_entry *arp)
121 {
122     uint32_t hash = tnl_arp_hash(&arp->ip);
123     cmap_remove(&table, &arp->cmap_node, hash);
124     ovsrcu_postpone(arp_entry_free, arp);
125 }
126
127 static void
128 tnl_arp_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
129               const struct eth_addr mac)
130 {
131     ovs_mutex_lock(&mutex);
132     struct tnl_arp_entry *arp = tnl_arp_lookup__(name, dst);
133     if (arp) {
134         if (eth_addr_equals(arp->mac, mac)) {
135             arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
136             ovs_mutex_unlock(&mutex);
137             return;
138         }
139         tnl_arp_delete(arp);
140         seq_change(tnl_conf_seq);
141     }
142
143     arp = xmalloc(sizeof *arp);
144
145     arp->ip = *dst;
146     arp->mac = mac;
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);
151 }
152
153 static void
154 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
155             const struct eth_addr mac)
156 {
157     struct in6_addr dst6;
158
159     in6_addr_set_mapped_ipv4(&dst6, dst);
160     tnl_arp_set__(name, &dst6, mac);
161 }
162
163 int
164 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
165               const char name[IFNAMSIZ])
166 {
167     if (flow->dl_type != htons(ETH_TYPE_ARP)) {
168         return EINVAL;
169     }
170
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);
175
176     tnl_arp_set(name, flow->nw_src, flow->arp_sha);
177     return 0;
178 }
179
180 int
181 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
182               const char name[IFNAMSIZ])
183 {
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)) {
188         return EINVAL;
189     }
190
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);
195
196     tnl_arp_set__(name, &flow->nd_target, flow->arp_tha);
197     return 0;
198 }
199
200 void
201 tnl_arp_cache_run(void)
202 {
203     struct tnl_arp_entry *arp;
204     bool changed = false;
205
206     ovs_mutex_lock(&mutex);
207     CMAP_FOR_EACH(arp, cmap_node, &table) {
208         if (arp->expires <= time_now()) {
209             tnl_arp_delete(arp);
210             changed = true;
211         }
212     }
213     ovs_mutex_unlock(&mutex);
214
215     if (changed) {
216         seq_change(tnl_conf_seq);
217     }
218 }
219
220 static void
221 tnl_arp_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
222                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
223 {
224     struct tnl_arp_entry *arp;
225     bool changed = false;
226
227     ovs_mutex_lock(&mutex);
228     CMAP_FOR_EACH(arp, cmap_node, &table) {
229         tnl_arp_delete(arp);
230         changed = true;
231     }
232     ovs_mutex_unlock(&mutex);
233     if (changed) {
234         seq_change(tnl_conf_seq);
235     }
236     unixctl_command_reply(conn, "OK");
237 }
238
239 static void
240 tnl_arp_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
241                   const char *argv[], void *aux OVS_UNUSED)
242 {
243     const char *br_name = argv[1];
244     struct eth_addr mac;
245     struct in_addr ip;
246     struct in6_addr ip6;
247
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");
252         return;
253     }
254
255     if (!eth_addr_from_string(argv[3], &mac)) {
256         unixctl_command_reply_error(conn, "bad MAC address");
257         return;
258     }
259
260     tnl_arp_set__(br_name, &ip6, mac);
261     unixctl_command_reply(conn, "OK");
262 }
263
264 static void
265 tnl_arp_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
266                    const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
267 {
268     struct ds ds = DS_EMPTY_INITIALIZER;
269     struct tnl_arp_entry *arp;
270
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;
276
277         start_len = ds.length;
278         print_ipv6_mapped(&ds, &arp->ip);
279
280         need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
281         ds_put_char_multiple(&ds, ' ', need_ws);
282
283         ds_put_format(&ds, ETH_ADDR_FMT"   %s\n",
284                       ETH_ADDR_ARGS(arp->mac), arp->br_name);
285
286     }
287     ovs_mutex_unlock(&mutex);
288     unixctl_command_reply(conn, ds_cstr(&ds));
289     ds_destroy(&ds);
290 }
291
292 void
293 tnl_arp_cache_init(void)
294 {
295     cmap_init(&table);
296
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);
300 }