d01ec7f2c038139611fda109a77f1be07ff5b271
[cascardo/ovs.git] / lib / tnl-neigh-cache.c
1 /*
2  * Copyright (c) 2014, 2015, 2016 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-neigh-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 "openvswitch/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 NEIGH_ENTRY_DEFAULT_IDLE_TIME  (15 * 60)
49
50 struct tnl_neigh_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 = CMAP_INITIALIZER;
59 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
60
61 static uint32_t
62 tnl_neigh_hash(const struct in6_addr *ip)
63 {
64     return hash_bytes(ip->s6_addr, 16, 0);
65 }
66
67 static struct tnl_neigh_entry *
68 tnl_neigh_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
69 {
70     struct tnl_neigh_entry *neigh;
71     uint32_t hash;
72
73     hash = tnl_neigh_hash(dst);
74     CMAP_FOR_EACH_WITH_HASH (neigh, cmap_node, hash, &table) {
75         if (ipv6_addr_equals(&neigh->ip, dst) && !strcmp(neigh->br_name, br_name)) {
76             neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
77             return neigh;
78         }
79     }
80     return NULL;
81 }
82
83 int
84 tnl_neigh_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
85                  struct eth_addr *mac)
86 {
87     struct tnl_neigh_entry *neigh;
88     int res = ENOENT;
89
90     neigh = tnl_neigh_lookup__(br_name, dst);
91     if (neigh) {
92         *mac = neigh->mac;
93         res = 0;
94     }
95     return res;
96 }
97
98 static void
99 neigh_entry_free(struct tnl_neigh_entry *neigh)
100 {
101     free(neigh);
102 }
103
104 static void
105 tnl_neigh_delete(struct tnl_neigh_entry *neigh)
106 {
107     uint32_t hash = tnl_neigh_hash(&neigh->ip);
108     cmap_remove(&table, &neigh->cmap_node, hash);
109     ovsrcu_postpone(neigh_entry_free, neigh);
110 }
111
112 static void
113 tnl_neigh_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
114               const struct eth_addr mac)
115 {
116     ovs_mutex_lock(&mutex);
117     struct tnl_neigh_entry *neigh = tnl_neigh_lookup__(name, dst);
118     if (neigh) {
119         if (eth_addr_equals(neigh->mac, mac)) {
120             neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
121             ovs_mutex_unlock(&mutex);
122             return;
123         }
124         tnl_neigh_delete(neigh);
125         seq_change(tnl_conf_seq);
126     }
127
128     neigh = xmalloc(sizeof *neigh);
129
130     neigh->ip = *dst;
131     neigh->mac = mac;
132     neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
133     ovs_strlcpy(neigh->br_name, name, sizeof neigh->br_name);
134     cmap_insert(&table, &neigh->cmap_node, tnl_neigh_hash(&neigh->ip));
135     ovs_mutex_unlock(&mutex);
136 }
137
138 static void
139 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
140             const struct eth_addr mac)
141 {
142     struct in6_addr dst6 = in6_addr_mapped_ipv4(dst);
143     tnl_neigh_set__(name, &dst6, mac);
144 }
145
146 static int
147 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
148               const char name[IFNAMSIZ])
149 {
150     if (flow->dl_type != htons(ETH_TYPE_ARP) ||
151         flow->nw_proto != ARP_OP_REPLY ||
152         eth_addr_is_zero(flow->arp_sha)) {
153         return EINVAL;
154     }
155
156     /* Exact Match on all ARP flows. */
157     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
158     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
159     memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
160
161     tnl_arp_set(name, flow->nw_src, flow->arp_sha);
162     return 0;
163 }
164
165 static int
166 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
167               const char name[IFNAMSIZ])
168 {
169     if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
170         flow->nw_proto != IPPROTO_ICMPV6 ||
171         flow->tp_dst != htons(0) ||
172         flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
173         return EINVAL;
174     }
175     /* - RFC4861 says Neighbor Advertisements sent in response to unicast Neighbor
176      *   Solicitations SHOULD include the Target link-layer address. However, Linux
177      *   doesn't. So, the response to Solicitations sent by OVS will include the
178      *   TLL address and other Advertisements not including it can be ignored.
179      * - OVS flow extract can set this field to zero in case of packet parsing errors.
180      *   For details refer miniflow_extract()*/
181     if (eth_addr_is_zero(flow->arp_tha)) {
182         return EINVAL;
183     }
184
185     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
186     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
187     memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
188     memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
189
190     tnl_neigh_set__(name, &flow->nd_target, flow->arp_tha);
191     return 0;
192 }
193
194 int
195 tnl_neigh_snoop(const struct flow *flow, struct flow_wildcards *wc,
196                 const char name[IFNAMSIZ])
197 {
198     int res;
199     res = tnl_arp_snoop(flow, wc, name);
200     if (res != EINVAL) {
201         return res;
202     }
203     return tnl_nd_snoop(flow, wc, name);
204 }
205
206 void
207 tnl_neigh_cache_run(void)
208 {
209     struct tnl_neigh_entry *neigh;
210     bool changed = false;
211
212     ovs_mutex_lock(&mutex);
213     CMAP_FOR_EACH(neigh, cmap_node, &table) {
214         if (neigh->expires <= time_now()) {
215             tnl_neigh_delete(neigh);
216             changed = true;
217         }
218     }
219     ovs_mutex_unlock(&mutex);
220
221     if (changed) {
222         seq_change(tnl_conf_seq);
223     }
224 }
225
226 static void
227 tnl_neigh_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
228                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
229 {
230     struct tnl_neigh_entry *neigh;
231     bool changed = false;
232
233     ovs_mutex_lock(&mutex);
234     CMAP_FOR_EACH(neigh, cmap_node, &table) {
235         tnl_neigh_delete(neigh);
236         changed = true;
237     }
238     ovs_mutex_unlock(&mutex);
239     if (changed) {
240         seq_change(tnl_conf_seq);
241     }
242     unixctl_command_reply(conn, "OK");
243 }
244
245 static int
246 lookup_any(const char *host_name, struct in6_addr *address)
247 {
248     if (addr_is_ipv6(host_name)) {
249         return lookup_ipv6(host_name, address);
250     } else {
251         int r;
252         struct in_addr ip;
253         r = lookup_ip(host_name, &ip);
254         if (r == 0) {
255             in6_addr_set_mapped_ipv4(address, ip.s_addr);
256         }
257         return r;
258     }
259     return ENOENT;
260 }
261
262 static void
263 tnl_neigh_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
264                     const char *argv[], void *aux OVS_UNUSED)
265 {
266     const char *br_name = argv[1];
267     struct eth_addr mac;
268     struct in6_addr ip6;
269
270     if (lookup_any(argv[2], &ip6) != 0) {
271         unixctl_command_reply_error(conn, "bad IP address");
272         return;
273     }
274
275     if (!eth_addr_from_string(argv[3], &mac)) {
276         unixctl_command_reply_error(conn, "bad MAC address");
277         return;
278     }
279
280     tnl_neigh_set__(br_name, &ip6, mac);
281     unixctl_command_reply(conn, "OK");
282 }
283
284 static void
285 tnl_neigh_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
286                      const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
287 {
288     struct ds ds = DS_EMPTY_INITIALIZER;
289     struct tnl_neigh_entry *neigh;
290
291     ds_put_cstr(&ds, "IP                                            MAC                 Bridge\n");
292     ds_put_cstr(&ds, "==========================================================================\n");
293     ovs_mutex_lock(&mutex);
294     CMAP_FOR_EACH(neigh, cmap_node, &table) {
295         int start_len, need_ws;
296
297         start_len = ds.length;
298         ipv6_format_mapped(&neigh->ip, &ds);
299
300         need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
301         ds_put_char_multiple(&ds, ' ', need_ws);
302
303         ds_put_format(&ds, ETH_ADDR_FMT"   %s\n",
304                       ETH_ADDR_ARGS(neigh->mac), neigh->br_name);
305
306     }
307     ovs_mutex_unlock(&mutex);
308     unixctl_command_reply(conn, ds_cstr(&ds));
309     ds_destroy(&ds);
310 }
311
312 void
313 tnl_neigh_cache_init(void)
314 {
315     unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_neigh_cache_show, NULL);
316     unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
317     unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
318     unixctl_command_register("tnl/neigh/show", "", 0, 0, tnl_neigh_cache_show, NULL);
319     unixctl_command_register("tnl/neigh/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
320     unixctl_command_register("tnl/neigh/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
321 }