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