blob: db78678d9c6886b11d9bc3244bfc115d98406766 [file] [log] [blame]
Florin Corase127a7e2016-02-18 22:20:01 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Filip Tehlara5abdeb2016-07-18 17:35:40 +020016#include <vlibmemory/api.h>
Florin Corase127a7e2016-02-18 22:20:01 +010017#include <vnet/lisp-cp/control.h>
18#include <vnet/lisp-cp/packets.h>
19#include <vnet/lisp-cp/lisp_msg_serdes.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010020#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
21#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Filip Tehlar4868ff62017-03-09 16:48:39 +010022#include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_table.h>
Filip Tehlard5a65db2017-05-17 17:21:10 +020025#include <vnet/ethernet/arp_packet.h>
26#include <vnet/ethernet/packet.h>
Florin Corase127a7e2016-02-18 22:20:01 +010027
Filip Tehlar397fd7d2016-10-26 14:31:24 +020028#include <openssl/evp.h>
29#include <openssl/hmac.h>
30
Filip Tehlaref2a5bf2017-05-30 07:14:46 +020031#define MAX_VALUE_U24 0xffffff
32
Florin Corasf3dc11a2017-01-24 03:13:43 -080033lisp_cp_main_t lisp_control_main;
34
Filip Tehlarcda70662017-01-16 10:30:03 +010035u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
36
37typedef enum
38{
39 LISP_CP_INPUT_NEXT_DROP,
40 LISP_CP_INPUT_N_NEXT,
41} lisp_cp_input_next_t;
42
Filip Tehlara5abdeb2016-07-18 17:35:40 +020043typedef struct
44{
45 u8 is_resend;
46 gid_address_t seid;
47 gid_address_t deid;
48 u8 smr_invoked;
49} map_request_args_t;
50
Florin Corasdca88042016-09-14 16:01:38 +020051u8
52vnet_lisp_get_map_request_mode (void)
53{
54 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
55 return lcm->map_request_mode;
56}
57
Filip Tehlar397fd7d2016-10-26 14:31:24 +020058static u16
59auth_data_len_by_key_id (lisp_key_type_t key_id)
60{
61 switch (key_id)
62 {
63 case HMAC_SHA_1_96:
64 return SHA1_AUTH_DATA_LEN;
65 case HMAC_SHA_256_128:
66 return SHA256_AUTH_DATA_LEN;
67 default:
68 clib_warning ("unsupported key type: %d!", key_id);
69 return (u16) ~ 0;
70 }
71 return (u16) ~ 0;
72}
73
74static const EVP_MD *
75get_encrypt_fcn (lisp_key_type_t key_id)
76{
77 switch (key_id)
78 {
79 case HMAC_SHA_1_96:
80 return EVP_sha1 ();
81 case HMAC_SHA_256_128:
82 return EVP_sha256 ();
83 default:
84 clib_warning ("unsupported encryption key type: %d!", key_id);
85 break;
86 }
87 return 0;
88}
89
Filip Tehlara5abdeb2016-07-18 17:35:40 +020090static int
91queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020092 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020093
Florin Corasf727db92016-06-23 15:01:58 +020094ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020095ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
96 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +020097{
98 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +020099 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +0200100 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
101 sw_if_index = swif->unnumbered_sw_if_index;
102 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +0200103 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
104 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
105 (u32) ~ 0;
106 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +0200107}
Filip Tehlar215104e2016-05-10 16:58:29 +0200108
Florin Corasf727db92016-06-23 15:01:58 +0200109void *
110ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200111 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200112{
Florin Corasa2157cf2016-08-16 21:09:14 +0200113 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200114
Florin Corasf727db92016-06-23 15:01:58 +0200115 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
116 if (!ia)
117 return 0;
118 return ip_interface_address_get_address (lm, ia);
119}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200120
Florin Corase127a7e2016-02-18 22:20:01 +0100121int
Florin Corasf727db92016-06-23 15:01:58 +0200122ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200123 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200124{
Florin Corasa2157cf2016-08-16 21:09:14 +0200125 ip_lookup_main_t *lm;
126 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200127
128 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
129 addr = ip_interface_get_first_address (lm, sw_if_index, version);
130 if (!addr)
131 return 0;
132
133 ip_address_set (result, addr, version);
134 return 1;
135}
136
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100137/**
138 * convert from a LISP address to a FIB prefix
139 */
140void
141ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200142{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100143 if (addr->version == IP4)
144 {
145 prefix->fp_len = 32;
146 prefix->fp_proto = FIB_PROTOCOL_IP4;
147 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
148 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
149 }
Florin Corasf727db92016-06-23 15:01:58 +0200150 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100151 {
152 prefix->fp_len = 128;
153 prefix->fp_proto = FIB_PROTOCOL_IP6;
154 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
155 }
Florin Corasf727db92016-06-23 15:01:58 +0200156}
157
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100158/**
159 * convert from a LISP to a FIB prefix
160 */
161void
162ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
163 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200164{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
166 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200167}
168
169/**
170 * Find the sw_if_index of the interface that would be used to egress towards
171 * dst.
172 */
173u32
174ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
175{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176 fib_node_index_t fei;
177 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200178
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200180
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100181 fei = fib_table_lookup (0, &prefix);
182
183 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200184}
185
186/**
187 * Find first IP of the interface that would be used to egress towards dst.
188 * Returns 1 if the address is found 0 otherwise.
189 */
190int
191ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200192 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200193{
194 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200195 ip_lookup_main_t *lm;
196 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200197 u8 ipver;
198
Florin Corasa2157cf2016-08-16 21:09:14 +0200199 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200200
Florin Corasa2157cf2016-08-16 21:09:14 +0200201 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200202
203 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100204 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200205
Florin Corasa2157cf2016-08-16 21:09:14 +0200206 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200207 return 0;
208
209 /* find the first ip address */
210 addr = ip_interface_get_first_address (lm, si, ipver);
211 if (0 == addr)
212 return 0;
213
214 ip_address_set (result, addr, ipver);
215 return 1;
216}
217
218static int
Florin Coras1a1adc72016-07-22 01:45:30 +0200219dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +0200220{
Neale Ranns5e575b12016-10-03 09:40:25 +0100221 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200222
Florin Coras1a1adc72016-07-22 01:45:30 +0200223 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200224 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200225 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200226
227 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200228 {
229 clib_warning ("vni %d not associated to a vrf!", vni);
230 return VNET_API_ERROR_INVALID_VALUE;
231 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200232 }
233 else
234 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200235 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200236 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200237 {
238 clib_warning ("vni %d not associated to a bridge domain!", vni);
239 return VNET_API_ERROR_INVALID_VALUE;
240 }
Florin Corasf727db92016-06-23 15:01:58 +0200241 }
242
Florin Corasf727db92016-06-23 15:01:58 +0200243 /* enable/disable data-plane interface */
244 if (is_add)
245 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100246 if (is_l2)
247 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
248 else
249 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200250 }
251 else
252 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100253 if (is_l2)
254 lisp_gpe_tenant_l2_iface_unlock (vni);
255 else
256 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200257 }
258
259 return 0;
260}
261
262static void
263dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
264{
Florin Corasa2157cf2016-08-16 21:09:14 +0200265 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
266 fwd_entry_t *fe = 0;
267 uword *feip = 0;
268 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200269
Florin Corasa2157cf2016-08-16 21:09:14 +0200270 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200271 if (!feip)
272 return;
273
Florin Corasa2157cf2016-08-16 21:09:14 +0200274 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200275
276 /* delete dp fwd entry */
277 u32 sw_if_index;
278 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200279 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200280 a->vni = gid_address_vni (&fe->reid);
281 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200282 if (fe->is_src_dst)
283 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200284
Filip Tehlar21511912017-04-07 10:41:42 +0200285 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200286 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
287
288 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200289 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
290 vec_free (fe->locator_pairs);
291 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200292}
293
294/**
295 * Finds first remote locator with best (lowest) priority that has a local
296 * peer locator with an underlying route to it.
297 *
298 */
299static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200300get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
301 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200302{
Florin Coras3590ac52016-08-08 16:04:26 +0200303 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200304 locator_set_t *rmt_ls, *lcl_ls;
305 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
306 locator_t *lp, *rmt = 0;
307 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200308 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200309
Florin Corasa2157cf2016-08-16 21:09:14 +0200310 rmt_ls =
311 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
312 lcl_ls =
313 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200314
Florin Corasa2157cf2016-08-16 21:09:14 +0200315 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200316 return 0;
317
Florin Coras3590ac52016-08-08 16:04:26 +0200318 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200319 {
320 rmt = 0;
321
322 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200323 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
324 {
325 if (0 != hash_get (checked, i))
326 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200327
Florin Corasa2157cf2016-08-16 21:09:14 +0200328 li = vec_elt (rmt_ls->locator_indices, i);
329 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200330
Florin Corasa2157cf2016-08-16 21:09:14 +0200331 /* we don't support non-IP locators for now */
332 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
333 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200334
Florin Corasa2157cf2016-08-16 21:09:14 +0200335 if ((found && lp->priority == limitp)
336 || (!found && lp->priority >= limitp))
337 {
338 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200339
Florin Corasa2157cf2016-08-16 21:09:14 +0200340 /* don't search for locators with lower priority and don't
341 * check this locator again*/
342 limitp = lp->priority;
343 hash_set (checked, i, 1);
344 break;
345 }
346 }
Florin Corasf727db92016-06-23 15:01:58 +0200347 /* check if a local locator with a route to remote locator exists */
348 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200349 {
350 /* find egress sw_if_index for rmt locator */
351 esi =
352 ip_fib_get_egress_iface_for_dst (lcm,
353 &gid_address_ip (&rmt->address));
354 if ((u32) ~ 0 == esi)
355 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200356
Florin Corasa2157cf2016-08-16 21:09:14 +0200357 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
358 {
359 li = vec_elt (lcl_ls->locator_indices, i);
360 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200361
Florin Corasa2157cf2016-08-16 21:09:14 +0200362 /* found local locator with the needed sw_if_index */
363 if (sl->sw_if_index == esi)
364 {
365 /* and it has an address */
366 if (0 == ip_interface_get_first_ip_address (lcm,
367 sl->sw_if_index,
368 gid_address_ip_version
369 (&rmt->address),
370 lcl_addr))
371 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200372
Florin Corasa2157cf2016-08-16 21:09:14 +0200373 memset (&pair, 0, sizeof (pair));
374 ip_address_copy (&pair.rmt_loc,
375 &gid_address_ip (&rmt->address));
376 ip_address_copy (&pair.lcl_loc, lcl_addr);
377 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100378 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200379 vec_add1 (locator_pairs[0], pair);
380 found = 1;
381 }
382 }
383 }
Florin Corasf727db92016-06-23 15:01:58 +0200384 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200385 break;
Florin Corasf727db92016-06-23 15:01:58 +0200386 }
Florin Coras3590ac52016-08-08 16:04:26 +0200387
Florin Corasa2157cf2016-08-16 21:09:14 +0200388 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200389 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200390}
391
392static void
Florin Corasdca88042016-09-14 16:01:38 +0200393gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
394 fid_address_t * fid)
395{
396 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
397
398 dst[0] = src[0];
399
400 switch (fid_addr_type (fid))
401 {
402 case FID_ADDR_IP_PREF:
403 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
404 gid_address_ippref (dst) = fid_addr_ippref (fid);
405 break;
406 case FID_ADDR_MAC:
407 gid_address_type (dst) = GID_ADDR_MAC;
408 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
409 break;
410 default:
411 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
412 break;
413 }
414}
415
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200416u8
417vnet_lisp_map_register_state_get (void)
418{
419 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
420 return lcm->map_registering;
421}
422
423u8
424vnet_lisp_rloc_probe_state_get (void)
425{
426 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
427 return lcm->rloc_probing;
428}
429
Florin Corasdca88042016-09-14 16:01:38 +0200430static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200431dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200432{
Florin Corasa2157cf2016-08-16 21:09:14 +0200433 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
Florin Corasba888e42017-01-24 11:38:18 -0800434 gid_address_t *rmt_eid, *lcl_eid;
435 mapping_t *lcl_map, *rmt_map;
Florin Corasf727db92016-06-23 15:01:58 +0200436 u32 sw_if_index;
Florin Corasa2157cf2016-08-16 21:09:14 +0200437 uword *feip = 0, *dpid;
438 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200439 u8 type, is_src_dst = 0;
Florin Corasba888e42017-01-24 11:38:18 -0800440 int rv;
Florin Corasf727db92016-06-23 15:01:58 +0200441
Florin Corasa2157cf2016-08-16 21:09:14 +0200442 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200443
444 /* remove entry if it already exists */
445 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
446 if (feip)
447 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
448
Florin Corasba888e42017-01-24 11:38:18 -0800449 /*
450 * Determine local mapping and eid
451 */
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200452 if (lcm->lisp_pitr)
Florin Corasba888e42017-01-24 11:38:18 -0800453 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200454 else
Florin Corasba888e42017-01-24 11:38:18 -0800455 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
456 lcl_eid = &lcl_map->eid;
Florin Corasf727db92016-06-23 15:01:58 +0200457
Florin Corasba888e42017-01-24 11:38:18 -0800458 /*
459 * Determine remote mapping and eid
460 */
461 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
462 rmt_eid = &rmt_map->eid;
463
464 /*
465 * Build and insert data plane forwarding entry
466 */
Florin Corasf727db92016-06-23 15:01:58 +0200467 a->is_add = 1;
468
Filip Tehlard5fcc462016-10-17 16:20:18 +0200469 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200470 {
Florin Corasba888e42017-01-24 11:38:18 -0800471 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
Filip Tehlard5fcc462016-10-17 16:20:18 +0200472 {
Florin Corasba888e42017-01-24 11:38:18 -0800473 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
474 &gid_address_sd_dst (rmt_eid));
475 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
476 &gid_address_sd_src (rmt_eid));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200477 }
478 else
479 {
Florin Corasba888e42017-01-24 11:38:18 -0800480 gid_address_copy (&a->rmt_eid, rmt_eid);
481 gid_address_copy (&a->lcl_eid, lcl_eid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200482 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200483 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200484 }
485 else
Florin Corasba888e42017-01-24 11:38:18 -0800486 gid_address_copy (&a->rmt_eid, rmt_eid);
Florin Corasdca88042016-09-14 16:01:38 +0200487
Florin Corasa2157cf2016-08-16 21:09:14 +0200488 a->vni = gid_address_vni (&a->rmt_eid);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100489 a->is_src_dst = is_src_dst;
Florin Coras1a1adc72016-07-22 01:45:30 +0200490
491 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200492 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200493 if (GID_ADDR_IP_PREFIX == type)
494 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200495 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200496 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200497 {
498 clib_warning ("vni %d not associated to a vrf!", a->vni);
499 return;
500 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200501 a->table_id = dpid[0];
502 }
503 else if (GID_ADDR_MAC == type)
504 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200505 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200506 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200507 {
508 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
509 return;
510 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200511 a->bd_id = dpid[0];
512 }
513
Florin Corasf727db92016-06-23 15:01:58 +0200514 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Corasba888e42017-01-24 11:38:18 -0800515 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
516
517 /* Either rmt mapping is negative or we can't find underlay path.
518 * Try again with petr if configured */
519 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
Florin Corasf727db92016-06-23 15:01:58 +0200520 {
Florin Corasba888e42017-01-24 11:38:18 -0800521 rmt_map = lisp_get_petr_mapping (lcm);
522 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
Florin Corasf727db92016-06-23 15:01:58 +0200523 }
524
Florin Corasba888e42017-01-24 11:38:18 -0800525 /* negative entry */
526 if (rv == 0)
527 {
528 a->is_negative = 1;
529 a->action = rmt_map->action;
530 }
Florin Corasf727db92016-06-23 15:01:58 +0200531
Filip Tehlar21511912017-04-07 10:41:42 +0200532 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
533 if (rv)
534 {
535 if (a->locator_pairs)
536 vec_free (a->locator_pairs);
537 return;
538 }
Florin Corasf727db92016-06-23 15:01:58 +0200539
Filip Tehlar21511912017-04-07 10:41:42 +0200540 /* add tunnel to fwd entry table */
Florin Corasf727db92016-06-23 15:01:58 +0200541 pool_get (lcm->fwd_entry_pool, fe);
Filip Tehlar21511912017-04-07 10:41:42 +0200542 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
543
Florin Corasbb5c22f2016-08-02 02:31:03 +0200544 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200545 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100546
547 if (is_src_dst)
548 gid_address_copy (&fe->leid, &a->lcl_eid);
549 else
Florin Corasba888e42017-01-24 11:38:18 -0800550 gid_address_copy (&fe->leid, lcl_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100551
Filip Tehlar69a9b762016-09-23 10:00:52 +0200552 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200553 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200554 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200555}
556
Filip Tehlarce1aae42017-01-04 10:42:25 +0100557typedef struct
558{
559 u32 si;
560 u32 di;
561} fwd_entry_mt_arg_t;
562
563static void *
564dp_add_fwd_entry_thread_fn (void *arg)
565{
566 fwd_entry_mt_arg_t *a = arg;
567 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
568 dp_add_fwd_entry (lcm, a->si, a->di);
569 return 0;
570}
571
572static int
573dp_add_fwd_entry_from_mt (u32 si, u32 di)
574{
575 fwd_entry_mt_arg_t a;
576
577 memset (&a, 0, sizeof (a));
578 a.si = si;
579 a.di = di;
580
581 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
582 (u8 *) & a, sizeof (a));
583 return 0;
584}
585
Florin Corasf727db92016-06-23 15:01:58 +0200586/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200587 * Returns vector of adjacencies.
588 *
589 * The caller must free the vector returned by this function.
590 *
591 * @param vni virtual network identifier
592 * @return vector of adjacencies
593 */
594lisp_adjacency_t *
595vnet_lisp_adjacencies_get_by_vni (u32 vni)
596{
597 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
598 fwd_entry_t *fwd;
599 lisp_adjacency_t *adjs = 0, adj;
600
601 /* *INDENT-OFF* */
602 pool_foreach(fwd, lcm->fwd_entry_pool,
603 ({
604 if (gid_address_vni (&fwd->reid) != vni)
605 continue;
606
607 gid_address_copy (&adj.reid, &fwd->reid);
608 gid_address_copy (&adj.leid, &fwd->leid);
609 vec_add1 (adjs, adj);
610 }));
611 /* *INDENT-ON* */
612
613 return adjs;
614}
615
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200616static lisp_msmr_t *
617get_map_server (ip_address_t * a)
618{
619 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
620 lisp_msmr_t *m;
621
622 vec_foreach (m, lcm->map_servers)
623 {
624 if (!ip_address_cmp (&m->address, a))
625 {
626 return m;
627 }
628 }
629 return 0;
630}
631
632static lisp_msmr_t *
633get_map_resolver (ip_address_t * a)
634{
635 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
636 lisp_msmr_t *m;
637
638 vec_foreach (m, lcm->map_resolvers)
639 {
640 if (!ip_address_cmp (&m->address, a))
641 {
642 return m;
643 }
644 }
645 return 0;
646}
647
648int
649vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
650{
651 u32 i;
652 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
653 lisp_msmr_t _ms, *ms = &_ms;
654
655 if (vnet_lisp_enable_disable_status () == 0)
656 {
657 clib_warning ("LISP is disabled!");
658 return VNET_API_ERROR_LISP_DISABLED;
659 }
660
661 if (is_add)
662 {
663 if (get_map_server (addr))
664 {
665 clib_warning ("map-server %U already exists!", format_ip_address,
666 addr);
667 return -1;
668 }
669
670 memset (ms, 0, sizeof (*ms));
671 ip_address_copy (&ms->address, addr);
672 vec_add1 (lcm->map_servers, ms[0]);
673 }
674 else
675 {
676 for (i = 0; i < vec_len (lcm->map_servers); i++)
677 {
678 ms = vec_elt_at_index (lcm->map_servers, i);
679 if (!ip_address_cmp (&ms->address, addr))
680 {
681 vec_del1 (lcm->map_servers, i);
682 break;
683 }
684 }
685 }
686
687 return 0;
688}
689
Filip Tehlar69a9b762016-09-23 10:00:52 +0200690/**
Florin Corasf727db92016-06-23 15:01:58 +0200691 * Add/remove mapping to/from map-cache. Overwriting not allowed.
692 */
693int
694vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200695 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100696{
Florin Corasa2157cf2016-08-16 21:09:14 +0200697 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
698 u32 mi, *map_indexp, map_index, i;
699 mapping_t *m, *old_map;
700 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100701
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200702 if (gid_address_type (&a->eid) == GID_ADDR_NSH)
703 {
704 if (gid_address_vni (&a->eid) != 0)
705 {
706 clib_warning ("Supported only default VNI for NSH!");
707 return VNET_API_ERROR_INVALID_ARGUMENT;
708 }
709 if (gid_address_nsh_spi (&a->eid) > MAX_VALUE_U24)
710 {
711 clib_warning ("SPI is greater than 24bit!");
712 return VNET_API_ERROR_INVALID_ARGUMENT;
713 }
714 }
715
Florin Corasf727db92016-06-23 15:01:58 +0200716 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200717 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100718 if (a->is_add)
719 {
720 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200721 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
722 {
723 clib_warning ("eid %U found in the eid-table", format_gid_address,
724 &a->eid);
725 return VNET_API_ERROR_VALUE_EXIST;
726 }
Florin Corase127a7e2016-02-18 22:20:01 +0100727
Florin Corasa2157cf2016-08-16 21:09:14 +0200728 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200729 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100730 m->locator_set_index = a->locator_set_index;
731 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200732 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100733 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200734 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200735 m->key = vec_dup (a->key);
736 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100737
738 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200739 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200740 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100741
Florin Corasa2157cf2016-08-16 21:09:14 +0200742 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
743 {
744 clib_warning ("Locator set with index %d doesn't exist",
745 a->locator_set_index);
746 return VNET_API_ERROR_INVALID_VALUE;
747 }
Florin Corase127a7e2016-02-18 22:20:01 +0100748
749 /* add eid to list of eids supported by locator-set */
750 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200751 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
752 a->locator_set_index);
753 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100754
755 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200756 {
757 /* mark as local */
758 vec_add1 (lcm->local_mappings_indexes, map_index);
759 }
Florin Corase127a7e2016-02-18 22:20:01 +0100760 map_index_result[0] = map_index;
761 }
762 else
763 {
Florin Coras577c3552016-04-21 00:45:40 +0200764 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200765 {
766 clib_warning ("eid %U not found in the eid-table",
767 format_gid_address, &a->eid);
768 return VNET_API_ERROR_INVALID_VALUE;
769 }
Florin Corase127a7e2016-02-18 22:20:01 +0100770
771 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200772 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
773 a->locator_set_index);
774 for (i = 0; i < vec_len (eid_indexes[0]); i++)
775 {
776 map_indexp = vec_elt_at_index (eid_indexes[0], i);
777 if (map_indexp[0] == mi)
778 break;
779 }
780 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100781
782 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200783 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100784 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200785 {
786 u32 k, *lm_indexp;
787 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
788 {
789 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
790 if (lm_indexp[0] == mi)
791 break;
792 }
793 vec_del1 (lcm->local_mappings_indexes, k);
794 }
Florin Corase127a7e2016-02-18 22:20:01 +0100795
796 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200797 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200798 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100799 pool_put_index (lcm->mapping_pool, mi);
800 }
801
802 return 0;
803}
804
Florin Corasf727db92016-06-23 15:01:58 +0200805/**
806 * Add/update/delete mapping to/in/from map-cache.
807 */
Florin Coras577c3552016-04-21 00:45:40 +0200808int
809vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200810 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200811{
Florin Corasa2157cf2016-08-16 21:09:14 +0200812 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200813 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200814 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200815
Florin Corasa2157cf2016-08-16 21:09:14 +0200816 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200817
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200818 if (vnet_lisp_enable_disable_status () == 0)
819 {
820 clib_warning ("LISP is disabled!");
821 return VNET_API_ERROR_LISP_DISABLED;
822 }
823
Florin Corasa2157cf2016-08-16 21:09:14 +0200824 vni = gid_address_vni (&a->eid);
825 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200826 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200827 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200828 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200829 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200830
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200831 if (!dp_table && GID_ADDR_NSH != type)
Florin Coras577c3552016-04-21 00:45:40 +0200832 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200833 clib_warning ("vni %d not associated to a %s!", vni,
834 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200835 return VNET_API_ERROR_INVALID_VALUE;
836 }
837
Florin Corasf727db92016-06-23 15:01:58 +0200838 /* store/remove mapping from map-cache */
839 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200840}
841
Filip Tehlard5a65db2017-05-17 17:21:10 +0200842static void
843add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
844{
845 u32 **ht = arg;
846 u32 bd = (u32) kvp->key[0];
847 hash_set (ht[0], bd, 0);
848}
849
850u32 *
851vnet_lisp_l2_arp_bds_get (void)
852{
853 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
854 u32 *bds = 0;
855
856 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
857 add_l2_arp_bd, &bds);
858 return bds;
859}
860
861typedef struct
862{
863 void *vector;
864 u32 bd;
865} lisp_add_l2_arp_args_t;
866
867static void
868add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
869{
870 lisp_add_l2_arp_args_t *a = arg;
871 lisp_api_l2_arp_entry_t **vector = a->vector, e;
872
873 if ((u32) kvp->key[0] == a->bd)
874 {
875 mac_copy (e.mac, (void *) &kvp->value);
876 e.ip4 = (u32) kvp->key[1];
877 vec_add1 (vector[0], e);
878 }
879}
880
881lisp_api_l2_arp_entry_t *
882vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
883{
884 lisp_api_l2_arp_entry_t *entries = 0;
885 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
886 lisp_add_l2_arp_args_t a;
887
888 a.vector = &entries;
889 a.bd = bd;
890
891 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
892 add_l2_arp_entry, &a);
893 return entries;
894}
895
896int
897vnet_lisp_add_del_l2_arp_entry (gid_address_t * key, u8 * mac, u8 is_add)
898{
899 if (vnet_lisp_enable_disable_status () == 0)
900 {
901 clib_warning ("LISP is disabled!");
902 return VNET_API_ERROR_LISP_DISABLED;
903 }
904
905 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
906 int rc = 0;
907
908 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
909 if (is_add)
910 {
911 if (res != GID_LOOKUP_MISS_L2)
912 {
913 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
914 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
915 }
916 u64 val = mac_to_u64 (mac);
917 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
918 1 /* is_add */ );
919 }
920 else
921 {
922 if (res == GID_LOOKUP_MISS_L2)
923 {
924 clib_warning ("ONE ARP entry %U not found - cannot delete!",
925 format_gid_address, key);
926 return -1;
927 }
928 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
929 0 /* is_add */ );
930 }
931
932 return rc;
933}
934
Filip Tehlar324112f2016-06-02 16:07:38 +0200935int
Florin Coras1a1adc72016-07-22 01:45:30 +0200936vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +0200937{
Florin Corasa2157cf2016-08-16 21:09:14 +0200938 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
939 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +0200940
941 if (vnet_lisp_enable_disable_status () == 0)
942 {
943 clib_warning ("LISP is disabled!");
Filip Tehlard5a65db2017-05-17 17:21:10 +0200944 return VNET_API_ERROR_LISP_DISABLED;
Filip Tehlar324112f2016-06-02 16:07:38 +0200945 }
946
Florin Coras1a1adc72016-07-22 01:45:30 +0200947 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
948 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
949
950 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +0200951 {
952 clib_warning ("can't add/del default vni-vrf mapping!");
953 return -1;
954 }
955
Florin Coras1a1adc72016-07-22 01:45:30 +0200956 dp_idp = hash_get (dp_table_by_vni[0], vni);
957 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200958
959 if (is_add)
960 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200961 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200962 {
963 clib_warning ("vni %d or vrf %d already used in vrf/vni "
964 "mapping!", vni, dp_id);
965 return -1;
966 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200967 hash_set (dp_table_by_vni[0], vni, dp_id);
968 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +0200969
970 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200971 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +0200972 }
973 else
974 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200975 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200976 {
977 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
978 "mapping!", vni, dp_id);
979 return -1;
980 }
Florin Corasf727db92016-06-23 15:01:58 +0200981 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200982 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlarfc568412017-04-05 10:06:03 +0200983
984 hash_unset (dp_table_by_vni[0], vni);
985 hash_unset (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200986 }
987 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +0200988
Filip Tehlar324112f2016-06-02 16:07:38 +0200989}
990
Florin Corasf727db92016-06-23 15:01:58 +0200991/* return 0 if the two locator sets are identical 1 otherwise */
992static u8
Florin Corasa2157cf2016-08-16 21:09:14 +0200993compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
994 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200995{
Florin Corasf727db92016-06-23 15:01:58 +0200996 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +0200997 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200998
Florin Corasa2157cf2016-08-16 21:09:14 +0200999 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +02001000 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001001
Florin Corasa2157cf2016-08-16 21:09:14 +02001002 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001003 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001004 old_li = vec_elt (old_ls_indexes, i);
1005 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +02001006
Florin Corasa2157cf2016-08-16 21:09:14 +02001007 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +02001008
1009 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +02001010 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001011 }
Florin Corasf727db92016-06-23 15:01:58 +02001012 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001013}
1014
Filip Tehlard5fcc462016-10-17 16:20:18 +02001015typedef struct
1016{
1017 u8 is_negative;
1018 void *lcm;
1019 gid_address_t *eids_to_be_deleted;
1020} remove_mapping_args_t;
1021
1022/**
1023 * Callback invoked when a sub-prefix is found
1024 */
1025static void
1026remove_mapping_if_needed (u32 mi, void *arg)
1027{
1028 u8 delete = 0;
1029 remove_mapping_args_t *a = arg;
1030 lisp_cp_main_t *lcm = a->lcm;
1031 mapping_t *m;
1032 locator_set_t *ls;
1033
1034 m = pool_elt_at_index (lcm->mapping_pool, mi);
1035 if (!m)
1036 return;
1037
1038 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1039
1040 if (a->is_negative)
1041 {
1042 if (0 != vec_len (ls->locator_indices))
1043 delete = 1;
1044 }
1045 else
1046 {
1047 if (0 == vec_len (ls->locator_indices))
1048 delete = 1;
1049 }
1050
1051 if (delete)
1052 vec_add1 (a->eids_to_be_deleted, m->eid);
1053}
1054
1055/**
1056 * This function searches map cache and looks for IP prefixes that are subset
1057 * of the provided one. If such prefix is found depending on 'is_negative'
1058 * it does follows:
1059 *
1060 * 1) if is_negative is true and found prefix points to positive mapping,
1061 * then the mapping is removed
1062 * 2) if is_negative is false and found prefix points to negative mapping,
1063 * then the mapping is removed
1064 */
1065static void
1066remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1067 u8 is_negative)
1068{
1069 gid_address_t *e;
1070 remove_mapping_args_t a;
Florin Corasf3dc11a2017-01-24 03:13:43 -08001071
Filip Tehlard5fcc462016-10-17 16:20:18 +02001072 memset (&a, 0, sizeof (a));
1073
1074 /* do this only in src/dst mode ... */
1075 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1076 return;
1077
1078 /* ... and only for IP prefix */
1079 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1080 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1081 return;
1082
1083 a.is_negative = is_negative;
1084 a.lcm = lcm;
1085
1086 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1087 remove_mapping_if_needed, &a);
1088
1089 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001090 {
Florin Corasf3dc11a2017-01-24 03:13:43 -08001091 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1092
1093 memset (adj_args, 0, sizeof (adj_args[0]));
1094 gid_address_copy (&adj_args->reid, e);
1095 adj_args->is_add = 0;
1096 if (vnet_lisp_add_del_adjacency (adj_args))
1097 clib_warning ("failed to del adjacency!");
1098
Filip Tehlard5fcc462016-10-17 16:20:18 +02001099 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001100 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001101
1102 vec_free (a.eids_to_be_deleted);
1103}
1104
Filip Tehlar9677a942016-11-28 10:23:31 +01001105static void
1106mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1107{
1108 timing_wheel_delete (&lcm->wheel, mi);
1109}
1110
Filip Tehlara6bce492017-02-03 10:17:49 +01001111static int
1112is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1113{
1114 fib_node_index_t fei;
1115 fib_prefix_t prefix;
1116 fib_entry_flag_t flags;
1117
1118 ip_address_to_fib_prefix (addr, &prefix);
1119
1120 fei = fib_table_lookup (0, &prefix);
1121 flags = fib_entry_get_flags (fei);
1122 return (FIB_ENTRY_FLAG_LOCAL & flags);
1123}
1124
Filip Tehlar195bcee2016-05-13 17:37:35 +02001125/**
Florin Corasf727db92016-06-23 15:01:58 +02001126 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001127 *
Florin Coras71893ac2016-07-10 20:09:32 +02001128 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001129 * @param rlocs vector of remote locators
1130 * @param action action for negative map-reply
1131 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001132 * @param res_map_index the map-index that was created/updated/removed. It is
1133 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001134 * @param is_static used for distinguishing between statically learned
1135 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001136 * @return return code
1137 */
1138int
Florin Coras71893ac2016-07-10 20:09:32 +02001139vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001140 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001141 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001142{
Florin Corasa2157cf2016-08-16 21:09:14 +02001143 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1144 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1145 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001146 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001147 mapping_t *old_map;
Filip Tehlara6bce492017-02-03 10:17:49 +01001148 locator_t *loc;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001149
Florin Corasa2157cf2016-08-16 21:09:14 +02001150 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001151 {
1152 clib_warning ("LISP is disabled!");
1153 return VNET_API_ERROR_LISP_DISABLED;
1154 }
1155
Florin Corasf727db92016-06-23 15:01:58 +02001156 if (res_map_index)
1157 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001158
Florin Corasf727db92016-06-23 15:01:58 +02001159 memset (m_args, 0, sizeof (m_args[0]));
1160 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001161
Florin Corasf727db92016-06-23 15:01:58 +02001162 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001163
Florin Coras71893ac2016-07-10 20:09:32 +02001164 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001165 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001166
1167 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001168 {
Filip Tehlar272c69e2017-02-15 16:40:35 +01001169 /* check if none of the locators match localy configured address */
1170 vec_foreach (loc, rlocs)
1171 {
1172 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1173 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1174 {
1175 clib_warning ("RLOC %U matches a local address!",
1176 format_gid_address, &loc->address);
1177 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1178 }
1179 }
1180
Florin Corasf727db92016-06-23 15:01:58 +02001181 /* overwrite: if mapping already exists, decide if locators should be
1182 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001183 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001184 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001185 if (!is_static && (old_map->is_static || old_map->local))
1186 {
1187 /* do not overwrite local or static remote mappings */
1188 clib_warning ("mapping %U rejected due to collision with local "
1189 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001190 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001191 return 0;
1192 }
1193
Florin Corasa2157cf2016-08-16 21:09:14 +02001194 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001195
Florin Corasa2157cf2016-08-16 21:09:14 +02001196 /* update mapping attributes */
1197 old_map->action = action;
1198 old_map->authoritative = authoritative;
1199 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001200
Florin Corasa2157cf2016-08-16 21:09:14 +02001201 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1202 old_map->locator_set_index);
1203 if (compare_locators (lcm, old_ls->locator_indices,
1204 ls_args->locators))
1205 {
1206 /* set locator-set index to overwrite */
1207 ls_args->is_add = 1;
1208 ls_args->index = old_map->locator_set_index;
1209 vnet_lisp_add_del_locator_set (ls_args, 0);
1210 if (res_map_index)
1211 res_map_index[0] = mi;
1212 }
1213 }
Florin Corasf727db92016-06-23 15:01:58 +02001214 /* new mapping */
1215 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001216 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001217 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1218
Florin Corasa2157cf2016-08-16 21:09:14 +02001219 ls_args->is_add = 1;
1220 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001221
Florin Corasa2157cf2016-08-16 21:09:14 +02001222 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001223
Florin Corasa2157cf2016-08-16 21:09:14 +02001224 /* add mapping */
1225 gid_address_copy (&m_args->eid, eid);
1226 m_args->is_add = 1;
1227 m_args->action = action;
1228 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001229 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001230 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001231 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001232
Florin Corasa2157cf2016-08-16 21:09:14 +02001233 if (res_map_index)
1234 res_map_index[0] = dst_map_index;
1235 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001236 }
Florin Corasf727db92016-06-23 15:01:58 +02001237 else
1238 {
Florin Coras71893ac2016-07-10 20:09:32 +02001239 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001240 {
1241 clib_warning ("cannot delete mapping for eid %U",
1242 format_gid_address, eid);
1243 return -1;
1244 }
Florin Corasf727db92016-06-23 15:01:58 +02001245
1246 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001247 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001248 m_args->locator_set_index = old_map->locator_set_index;
1249
1250 /* delete mapping associated from map-cache */
1251 vnet_lisp_map_cache_add_del (m_args, 0);
1252
1253 ls_args->is_add = 0;
1254 ls_args->index = old_map->locator_set_index;
1255 /* delete locator set */
1256 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001257
Filip Tehlar9677a942016-11-28 10:23:31 +01001258 /* delete timer associated to the mapping if any */
1259 if (old_map->timer_set)
1260 mapping_delete_timer (lcm, mi);
1261
Filip Tehlarb93222f2016-07-07 09:58:08 +02001262 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001263 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001264 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001265 }
1266
Filip Tehlar195bcee2016-05-13 17:37:35 +02001267 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001268 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001269}
1270
Filip Tehlar58f886a2016-05-30 15:57:40 +02001271int
Florin Corasf727db92016-06-23 15:01:58 +02001272vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001273{
1274 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001275 u32 mi, *map_indices = 0, *map_indexp;
1276 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1277 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1278 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001279
Florin Corasa2157cf2016-08-16 21:09:14 +02001280 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001281 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001282 ({
1283 vec_add1 (map_indices, mi);
1284 }));
1285 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001286
1287 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001288 {
1289 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1290 if (!map->local)
1291 {
1292 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001293
Florin Corasa2157cf2016-08-16 21:09:14 +02001294 dm_args->is_add = 0;
1295 gid_address_copy (&dm_args->eid, &map->eid);
1296 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001297
Florin Corasa2157cf2016-08-16 21:09:14 +02001298 /* delete mapping associated to fwd entry */
1299 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001300
Florin Corasa2157cf2016-08-16 21:09:14 +02001301 ls->is_add = 0;
1302 ls->local = 0;
1303 ls->index = map->locator_set_index;
1304 /* delete locator set */
1305 rv = vnet_lisp_add_del_locator_set (ls, 0);
1306 if (rv != 0)
1307 goto cleanup;
1308 }
1309 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001310
1311cleanup:
1312 if (map_indices)
1313 vec_free (map_indices);
1314 return rv;
1315}
1316
Filip Tehlar195bcee2016-05-13 17:37:35 +02001317/**
Florin Coras71893ac2016-07-10 20:09:32 +02001318 * Adds adjacency or removes forwarding entry associated to remote mapping.
1319 * Note that adjacencies are not stored, they only result in forwarding entries
1320 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001321 */
Florin Corasf3dc11a2017-01-24 03:13:43 -08001322int
1323vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
Florin Corasf727db92016-06-23 15:01:58 +02001324{
Florin Corasf3dc11a2017-01-24 03:13:43 -08001325 lisp_cp_main_t *lcm = &lisp_control_main;
Florin Coras71893ac2016-07-10 20:09:32 +02001326 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001327
1328 if (vnet_lisp_enable_disable_status () == 0)
1329 {
1330 clib_warning ("LISP is disabled!");
1331 return VNET_API_ERROR_LISP_DISABLED;
1332 }
1333
Filip Tehlar69a9b762016-09-23 10:00:52 +02001334 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
Florin Corasf3dc11a2017-01-24 03:13:43 -08001335 &a->reid, &a->leid);
Florin Coras71893ac2016-07-10 20:09:32 +02001336 if (GID_LOOKUP_MISS == remote_mi)
1337 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001338 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001339 format_gid_address, &a->reid);
Florin Corasf727db92016-06-23 15:01:58 +02001340
Florin Coras71893ac2016-07-10 20:09:32 +02001341 return -1;
1342 }
1343
Florin Corasf3dc11a2017-01-24 03:13:43 -08001344 if (a->is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001345 {
Florin Corasf727db92016-06-23 15:01:58 +02001346 /* check if source eid has an associated mapping. If pitr mode is on,
1347 * just use the pitr's mapping */
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001348 if (lcm->lisp_pitr)
1349 local_mi = lcm->pitr_map_index;
1350 else
1351 {
1352 if (gid_address_type (&a->reid) == GID_ADDR_NSH)
1353 {
1354 if (lcm->nsh_map_index == ~0)
1355 local_mi = GID_LOOKUP_MISS;
1356 else
1357 local_mi = lcm->nsh_map_index;
1358 }
1359 else
1360 {
1361 local_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1362 &a->leid);
1363 }
1364 }
Florin Corasf727db92016-06-23 15:01:58 +02001365
Florin Coras71893ac2016-07-10 20:09:32 +02001366 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001367 {
1368 clib_warning ("Local eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001369 format_gid_address, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001370
Florin Corasa2157cf2016-08-16 21:09:14 +02001371 return -1;
1372 }
Florin Corasf727db92016-06-23 15:01:58 +02001373
Florin Coras71893ac2016-07-10 20:09:32 +02001374 /* update forwarding */
1375 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001376 }
1377 else
Florin Coras71893ac2016-07-10 20:09:32 +02001378 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001379
1380 return 0;
1381}
1382
1383int
Florin Corasdca88042016-09-14 16:01:38 +02001384vnet_lisp_set_map_request_mode (u8 mode)
1385{
1386 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1387
1388 if (vnet_lisp_enable_disable_status () == 0)
1389 {
1390 clib_warning ("LISP is disabled!");
1391 return VNET_API_ERROR_LISP_DISABLED;
1392 }
1393
1394 if (mode >= _MR_MODE_MAX)
1395 {
1396 clib_warning ("Invalid LISP map request mode %d!", mode);
1397 return VNET_API_ERROR_INVALID_ARGUMENT;
1398 }
1399
1400 lcm->map_request_mode = mode;
1401 return 0;
1402}
1403
Filip Tehlar53f09e32016-05-19 14:25:44 +02001404int
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001405vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add)
1406{
1407 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1408 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1409 u32 locator_set_index = ~0;
1410 mapping_t *m;
1411 uword *p;
1412
1413 if (vnet_lisp_enable_disable_status () == 0)
1414 {
1415 clib_warning ("LISP is disabled!");
1416 return VNET_API_ERROR_LISP_DISABLED;
1417 }
1418
1419 if (is_add)
1420 {
1421 if (lcm->nsh_map_index == (u32) ~ 0)
1422 {
1423 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1424 if (!p)
1425 {
1426 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1427 return -1;
1428 }
1429 locator_set_index = p[0];
1430
1431 pool_get (lcm->mapping_pool, m);
1432 memset (m, 0, sizeof *m);
1433 m->locator_set_index = locator_set_index;
1434 m->local = 1;
1435 m->nsh_set = 1;
1436 lcm->nsh_map_index = m - lcm->mapping_pool;
1437
1438 if (~0 == vnet_lisp_gpe_add_nsh_iface (lgm))
1439 return -1;
1440 }
1441 }
1442 else
1443 {
1444 if (lcm->nsh_map_index != (u32) ~ 0)
1445 {
1446 /* remove NSH mapping */
1447 pool_put_index (lcm->mapping_pool, lcm->nsh_map_index);
1448 lcm->nsh_map_index = ~0;
1449 vnet_lisp_gpe_del_nsh_iface (lgm);
1450 }
1451 }
1452 return 0;
1453}
1454
1455int
Filip Tehlar53f09e32016-05-19 14:25:44 +02001456vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1457{
Florin Corasa2157cf2016-08-16 21:09:14 +02001458 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001459 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001460 mapping_t *m;
1461 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001462
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001463 if (vnet_lisp_enable_disable_status () == 0)
1464 {
1465 clib_warning ("LISP is disabled!");
1466 return VNET_API_ERROR_LISP_DISABLED;
1467 }
1468
Filip Tehlar53f09e32016-05-19 14:25:44 +02001469 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1470 if (!p)
1471 {
1472 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1473 return -1;
1474 }
1475 locator_set_index = p[0];
1476
1477 if (is_add)
1478 {
1479 pool_get (lcm->mapping_pool, m);
1480 m->locator_set_index = locator_set_index;
1481 m->local = 1;
Filip Tehlar38206ee2017-02-20 17:31:57 +01001482 m->pitr_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001483 lcm->pitr_map_index = m - lcm->mapping_pool;
1484
1485 /* enable pitr mode */
1486 lcm->lisp_pitr = 1;
1487 }
1488 else
1489 {
1490 /* remove pitr mapping */
1491 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1492
1493 /* disable pitr mode */
1494 lcm->lisp_pitr = 0;
1495 }
1496 return 0;
1497}
1498
Florin Corasba888e42017-01-24 11:38:18 -08001499/**
1500 * Configure Proxy-ETR
1501 *
1502 * @param ip PETR's IP address
1503 * @param is_add Flag that indicates if this is an addition or removal
1504 *
1505 * return 0 on success
1506 */
1507int
1508vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1509{
1510 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1511 u32 ls_index = ~0;
1512 mapping_t *m;
1513 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1514 locator_t loc;
1515
1516 if (vnet_lisp_enable_disable_status () == 0)
1517 {
1518 clib_warning ("LISP is disabled!");
1519 return VNET_API_ERROR_LISP_DISABLED;
1520 }
1521
1522 memset (ls_args, 0, sizeof (*ls_args));
1523
1524 if (is_add)
1525 {
1526 /* Create dummy petr locator-set */
Florin Corasb69259a2017-03-09 14:39:54 -08001527 memset (&loc, 0, sizeof (loc));
Florin Corasba888e42017-01-24 11:38:18 -08001528 gid_address_from_ip (&loc.address, ip);
1529 loc.priority = 1;
1530 loc.state = loc.weight = 1;
Florin Coras2743cc42017-01-29 16:42:20 -08001531 loc.local = 0;
Florin Corasba888e42017-01-24 11:38:18 -08001532
1533 ls_args->is_add = 1;
1534 ls_args->index = ~0;
1535 vec_add1 (ls_args->locators, loc);
1536 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1537
1538 /* Add petr mapping */
1539 pool_get (lcm->mapping_pool, m);
1540 m->locator_set_index = ls_index;
1541 lcm->petr_map_index = m - lcm->mapping_pool;
1542
1543 /* Enable use-petr */
1544 lcm->flags |= LISP_FLAG_USE_PETR;
1545 }
1546 else
1547 {
1548 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1549
1550 /* Remove petr locator */
1551 ls_args->is_add = 0;
1552 ls_args->index = m->locator_set_index;
1553 vnet_lisp_add_del_locator_set (ls_args, 0);
1554
1555 /* Remove petr mapping */
1556 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1557
1558 /* Disable use-petr */
1559 lcm->flags &= ~LISP_FLAG_USE_PETR;
1560 }
1561 return 0;
1562}
1563
Florin Corase127a7e2016-02-18 22:20:01 +01001564/* cleans locator to locator-set data and removes locators not part of
1565 * any locator-set */
1566static void
1567clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1568{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001569 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001570 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1571 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01001572 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001573 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1574 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1575 loc_indexp[0]);
1576 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1577 {
1578 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1579 if (ls_indexp[0] == lsi)
1580 break;
1581 }
Florin Corase127a7e2016-02-18 22:20:01 +01001582
Florin Corasa2157cf2016-08-16 21:09:14 +02001583 /* delete index for removed locator-set */
1584 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01001585
1586 /* delete locator if it's part of no locator-set */
1587 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001588 {
1589 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1590 vec_add1 (to_be_deleted, i);
1591 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001592 }
1593
1594 if (to_be_deleted)
1595 {
1596 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02001597 {
1598 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1599 vec_del1 (ls->locator_indices, loc_indexp[0]);
1600 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001601 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01001602 }
1603}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001604
Florin Corasf727db92016-06-23 15:01:58 +02001605static inline uword *
1606get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001607{
Florin Corasa2157cf2016-08-16 21:09:14 +02001608 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001609
Florin Corasa2157cf2016-08-16 21:09:14 +02001610 ASSERT (a != NULL);
1611 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001612
1613 /* find locator-set */
1614 if (a->local)
1615 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001616 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001617 }
1618 else
1619 {
1620 *p = a->index;
1621 }
1622
1623 return p;
1624}
1625
Florin Corasf727db92016-06-23 15:01:58 +02001626static inline int
1627is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02001628 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001629{
Florin Corasa2157cf2016-08-16 21:09:14 +02001630 locator_t *itloc;
1631 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001632
Florin Corasa2157cf2016-08-16 21:09:14 +02001633 ASSERT (ls != NULL);
1634 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001635
Florin Corasa2157cf2016-08-16 21:09:14 +02001636 vec_foreach (locit, ls->locator_indices)
1637 {
1638 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1639 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1640 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1641 {
1642 clib_warning ("Duplicate locator");
1643 return VNET_API_ERROR_VALUE_EXIST;
1644 }
1645 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001646
1647 return 0;
1648}
1649
Filip Tehlar68d2e242017-04-21 12:05:58 +02001650static void
1651update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1652 u32 mapping_index, u8 remove_only)
1653{
1654 fwd_entry_t *fwd;
1655 mapping_t *map;
1656 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1657
1658 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1659
1660 /* *INDENT-OFF* */
1661 pool_foreach(fwd, lcm->fwd_entry_pool,
1662 ({
1663 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1664 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1665 {
1666 a->is_add = 0;
1667 gid_address_copy (&a->leid, &fwd->leid);
1668 gid_address_copy (&a->reid, &fwd->reid);
1669
1670 vnet_lisp_add_del_adjacency (a);
1671
1672 if (!remove_only)
1673 {
1674 a->is_add = 1;
1675 vnet_lisp_add_del_adjacency (a);
1676 }
1677 }
1678 }));
1679 /* *INDENT-ON* */
1680}
1681
1682static void
1683update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1684 u32 ls_index, u8 remove_only)
1685{
1686 u32 i, *map_indexp;
1687 u32 **eid_indexes;
Filip Tehlarfacee282017-04-27 14:29:27 +02001688
1689 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1690 return;
1691
Filip Tehlar68d2e242017-04-21 12:05:58 +02001692 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1693
1694 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1695 {
1696 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1697 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1698 remove_only);
1699 }
1700}
1701
Florin Corasf727db92016-06-23 15:01:58 +02001702static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02001703remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1704 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001705{
Florin Corasa2157cf2016-08-16 21:09:14 +02001706 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1707 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001708
Florin Corasa2157cf2016-08-16 21:09:14 +02001709 ASSERT (ls != NULL);
1710 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001711
Florin Corasa2157cf2016-08-16 21:09:14 +02001712 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1713 pool_put_index (lcm->locator_pool, locit[0]);
1714 vec_del1 (ls->locator_indices, loc_id);
1715 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001716}
1717
1718int
1719vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001720 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001721{
Florin Corasa2157cf2016-08-16 21:09:14 +02001722 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1723 locator_t *loc = NULL, *itloc = NULL;
1724 uword _p = (u32) ~ 0, *p = &_p;
1725 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001726 u32 loc_id = ~0;
1727 int ret = 0;
1728
Florin Corasa2157cf2016-08-16 21:09:14 +02001729 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001730
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001731 if (vnet_lisp_enable_disable_status () == 0)
1732 {
1733 clib_warning ("LISP is disabled!");
1734 return VNET_API_ERROR_LISP_DISABLED;
1735 }
1736
Florin Corasa2157cf2016-08-16 21:09:14 +02001737 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001738 if (!p)
1739 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001740 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001741 return VNET_API_ERROR_INVALID_ARGUMENT;
1742 }
1743
1744 if (ls == 0)
1745 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001746 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001747 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001748 {
1749 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1750 p[0]);
1751 return VNET_API_ERROR_INVALID_ARGUMENT;
1752 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001753 }
1754
1755 if (a->is_add)
1756 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001757 if (ls_result)
1758 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001759
Florin Corasa2157cf2016-08-16 21:09:14 +02001760 /* allocate locators */
1761 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001762 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001763 ret = is_locator_in_locator_set (lcm, ls, itloc);
1764 if (0 != ret)
1765 {
1766 return ret;
1767 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001768
Florin Corasa2157cf2016-08-16 21:09:14 +02001769 pool_get (lcm->locator_pool, loc);
1770 loc[0] = itloc[0];
1771 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001772
Florin Corasa2157cf2016-08-16 21:09:14 +02001773 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001774
Florin Corasa2157cf2016-08-16 21:09:14 +02001775 vec_validate (lcm->locator_to_locator_sets, loc_index);
1776 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1777 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001778 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001779 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001780 }
1781 else
1782 {
1783 ls_index = p[0];
Filip Tehlar68d2e242017-04-21 12:05:58 +02001784 u8 removed;
Florin Corasa2157cf2016-08-16 21:09:14 +02001785
Filip Tehlar68d2e242017-04-21 12:05:58 +02001786 vec_foreach (itloc, a->locators)
Florin Corasa2157cf2016-08-16 21:09:14 +02001787 {
Filip Tehlar68d2e242017-04-21 12:05:58 +02001788 removed = 0;
1789 loc_id = 0;
1790 vec_foreach (locit, ls->locator_indices)
1791 {
1792 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
Florin Corasa2157cf2016-08-16 21:09:14 +02001793
Filip Tehlar68d2e242017-04-21 12:05:58 +02001794 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1795 {
1796 removed = 1;
1797 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1798 }
1799 if (0 == loc->local &&
1800 !gid_address_cmp (&loc->address, &itloc->address))
1801 {
1802 removed = 1;
1803 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1804 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001805
Filip Tehlar68d2e242017-04-21 12:05:58 +02001806 if (removed)
1807 {
1808 /* update fwd entries using this locator in DP */
1809 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1810 vec_len (ls->locator_indices)
1811 == 0);
1812 }
1813
1814 loc_id++;
1815 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001816 }
1817 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001818
1819 return 0;
1820}
1821
Florin Corase127a7e2016-02-18 22:20:01 +01001822int
1823vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001824 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01001825{
Florin Corasa2157cf2016-08-16 21:09:14 +02001826 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1827 locator_set_t *ls;
1828 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001829 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001830 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001831 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001832
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001833 if (vnet_lisp_enable_disable_status () == 0)
1834 {
1835 clib_warning ("LISP is disabled!");
1836 return VNET_API_ERROR_LISP_DISABLED;
1837 }
1838
Florin Corase127a7e2016-02-18 22:20:01 +01001839 if (a->is_add)
1840 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001841 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01001842
1843 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02001844 if (p && p[0] != (u32) ~ 0)
1845 {
1846 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1847 if (!ls)
1848 {
1849 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1850 p[0]);
1851 return -1;
1852 }
Florin Corase127a7e2016-02-18 22:20:01 +01001853
Florin Corasa2157cf2016-08-16 21:09:14 +02001854 /* clean locator to locator-set vectors and remove locators if
1855 * they're not part of another locator-set */
1856 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001857
Florin Corasa2157cf2016-08-16 21:09:14 +02001858 /* remove locator indices from locator set */
1859 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01001860
Florin Corasa2157cf2016-08-16 21:09:14 +02001861 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01001862
Florin Corasa2157cf2016-08-16 21:09:14 +02001863 if (ls_result)
1864 ls_result[0] = p[0];
1865 }
Florin Corase127a7e2016-02-18 22:20:01 +01001866 /* new locator-set */
1867 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001868 {
1869 pool_get (lcm->locator_set_pool, ls);
1870 memset (ls, 0, sizeof (*ls));
1871 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01001872
Florin Corasa2157cf2016-08-16 21:09:14 +02001873 if (a->local)
1874 {
1875 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01001876
Florin Corasa2157cf2016-08-16 21:09:14 +02001877 if (!lcm->locator_set_index_by_name)
1878 lcm->locator_set_index_by_name = hash_create_vec (
1879 /* size */
1880 0,
1881 sizeof
1882 (ls->name
1883 [0]),
1884 sizeof
1885 (uword));
1886 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1887 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01001888
Florin Corasa2157cf2016-08-16 21:09:14 +02001889 /* mark as local locator-set */
1890 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1891 }
1892 ls->local = a->local;
1893 if (ls_result)
1894 ls_result[0] = ls_index;
1895 }
Florin Corase127a7e2016-02-18 22:20:01 +01001896
Florin Corasa2157cf2016-08-16 21:09:14 +02001897 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001898 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02001899 {
1900 return ret;
1901 }
Florin Corase127a7e2016-02-18 22:20:01 +01001902 }
1903 else
1904 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001905 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001906 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001907 {
1908 clib_warning ("locator-set %v doesn't exists", a->name);
1909 return -1;
1910 }
Florin Corase127a7e2016-02-18 22:20:01 +01001911
Florin Corasa2157cf2016-08-16 21:09:14 +02001912 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001913 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001914 {
1915 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1916 return -1;
1917 }
Florin Corase127a7e2016-02-18 22:20:01 +01001918
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001919 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02001920 {
1921 clib_warning ("Can't delete the locator-set used to constrain "
1922 "the itr-rlocs in map-requests!");
1923 return -1;
1924 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001925
Florin Corasa2157cf2016-08-16 21:09:14 +02001926 if (vec_len (lcm->locator_set_to_eids) != 0)
1927 {
1928 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1929 if (vec_len (eid_indexes[0]) != 0)
1930 {
1931 clib_warning
1932 ("Can't delete a locator that supports a mapping!");
1933 return -1;
1934 }
1935 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001936
1937 /* clean locator to locator-sets data */
1938 clean_locator_to_locator_set (lcm, p[0]);
1939
1940 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02001941 {
1942 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001943
Florin Corasa2157cf2016-08-16 21:09:14 +02001944 vec_foreach_index (it, lcm->local_locator_set_indexes)
1945 {
1946 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1947 if (lsi == p[0])
1948 {
1949 vec_del1 (lcm->local_locator_set_indexes, it);
1950 break;
1951 }
1952 }
1953 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1954 }
1955 vec_free (ls->name);
1956 vec_free (ls->locator_indices);
1957 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001958 }
1959 return 0;
1960}
1961
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001962int
1963vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1964{
1965 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1966
1967 lcm->rloc_probing = is_enable;
1968 return 0;
1969}
1970
1971int
1972vnet_lisp_map_register_enable_disable (u8 is_enable)
1973{
1974 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1975
1976 lcm->map_registering = is_enable;
1977 return 0;
1978}
1979
Filip Tehlar46d4e362016-05-09 09:39:26 +02001980clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02001981vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001982{
Florin Coras1a1adc72016-07-22 01:45:30 +02001983 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02001984 clib_error_t *error = 0;
1985 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1986 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001987
Florin Corasf727db92016-06-23 15:01:58 +02001988 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001989 error = vnet_lisp_gpe_enable_disable (a);
1990 if (error)
1991 {
1992 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02001993 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02001994 }
1995
Florin Corasf727db92016-06-23 15:01:58 +02001996 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001997 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001998 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02001999
2000 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02002001 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2002 dp_add_del_iface(lcm, vni, 0, 1);
2003 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02002004 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2005 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02002006 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002007 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002008 }
2009 else
2010 {
Florin Corasf727db92016-06-23 15:01:58 +02002011 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002012 hash_free (lcm->fwd_entry_by_mapping_index);
2013 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002014 }
2015
2016 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02002017 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002018
2019 return 0;
2020}
2021
Filip Tehlar46d4e362016-05-09 09:39:26 +02002022u8
2023vnet_lisp_enable_disable_status (void)
2024{
Florin Corasa2157cf2016-08-16 21:09:14 +02002025 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002026 return lcm->is_enabled;
2027}
2028
Florin Corase127a7e2016-02-18 22:20:01 +01002029int
2030vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2031{
Florin Corasa2157cf2016-08-16 21:09:14 +02002032 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002033 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002034 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01002035
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002036 if (vnet_lisp_enable_disable_status () == 0)
2037 {
2038 clib_warning ("LISP is disabled!");
2039 return VNET_API_ERROR_LISP_DISABLED;
2040 }
2041
Florin Corase127a7e2016-02-18 22:20:01 +01002042 if (a->is_add)
2043 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002044
2045 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02002046 {
2047 clib_warning ("map-resolver %U already exists!", format_ip_address,
2048 &a->address);
2049 return -1;
2050 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002051
2052 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02002053 ip_address_copy (&mr->address, &a->address);
2054 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002055
2056 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02002057 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002058 }
2059 else
2060 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002061 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2062 {
2063 mr = vec_elt_at_index (lcm->map_resolvers, i);
2064 if (!ip_address_cmp (&mr->address, &a->address))
2065 {
2066 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2067 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002068
Florin Corasa2157cf2016-08-16 21:09:14 +02002069 vec_del1 (lcm->map_resolvers, i);
2070 break;
2071 }
2072 }
Florin Corase127a7e2016-02-18 22:20:01 +01002073 }
2074 return 0;
2075}
2076
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002077int
2078vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2079{
Florin Corasa2157cf2016-08-16 21:09:14 +02002080 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2081 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002082
Florin Corasf727db92016-06-23 15:01:58 +02002083 if (vnet_lisp_enable_disable_status () == 0)
2084 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002085 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02002086 return VNET_API_ERROR_LISP_DISABLED;
2087 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002088
2089 if (a->is_add)
2090 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002091 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002092 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002093 {
2094 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2095 return VNET_API_ERROR_INVALID_ARGUMENT;
2096 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002097
2098 lcm->mreq_itr_rlocs = p[0];
2099 }
2100 else
2101 {
2102 lcm->mreq_itr_rlocs = ~0;
2103 }
2104
2105 return 0;
2106}
2107
Florin Corase127a7e2016-02-18 22:20:01 +01002108/* Statistics (not really errors) */
2109#define foreach_lisp_cp_lookup_error \
2110_(DROP, "drop") \
Filip Tehlard5a65db2017-05-17 17:21:10 +02002111_(MAP_REQUESTS_SENT, "map-request sent") \
2112_(ARP_REPLY_TX, "ARP replies sent")
Florin Corase127a7e2016-02-18 22:20:01 +01002113
Florin Corasa2157cf2016-08-16 21:09:14 +02002114static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01002115#define _(sym,string) string,
2116 foreach_lisp_cp_lookup_error
2117#undef _
2118};
2119
2120typedef enum
2121{
2122#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02002123 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01002124#undef _
2125 LISP_CP_LOOKUP_N_ERROR,
2126} lisp_cp_lookup_error_t;
2127
2128typedef enum
2129{
2130 LISP_CP_LOOKUP_NEXT_DROP,
Filip Tehlard5a65db2017-05-17 17:21:10 +02002131 LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX,
Florin Corase127a7e2016-02-18 22:20:01 +01002132 LISP_CP_LOOKUP_N_NEXT,
2133} lisp_cp_lookup_next_t;
2134
2135typedef struct
2136{
2137 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02002138 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01002139} lisp_cp_lookup_trace_t;
2140
2141u8 *
2142format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2143{
2144 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2145 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02002146 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01002147
2148 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02002149 format_ip_address, &t->map_resolver_ip, format_gid_address,
2150 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01002151 return s;
2152}
2153
Florin Coras1c494842016-06-16 21:08:56 +02002154int
2155get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02002156 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02002157{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002158 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02002159 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01002160
Florin Corasa2157cf2016-08-16 21:09:14 +02002161 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01002162 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002163 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02002164 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002165 }
2166
Florin Corasddfafb82016-05-13 18:09:56 +02002167 /* find the first mr ip we have a route to and the ip of the
2168 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002169 vec_foreach (mrit, lcm->map_resolvers)
2170 {
2171 a = &mrit->address;
2172 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2173 {
2174 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002175
Florin Corasa2157cf2016-08-16 21:09:14 +02002176 /* also update globals */
2177 return 1;
2178 }
2179 }
Florin Corasddfafb82016-05-13 18:09:56 +02002180
Florin Corasa2157cf2016-08-16 21:09:14 +02002181 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02002182 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02002183}
Filip Tehlar254b0362016-04-07 10:04:34 +02002184
Filip Tehlarbeceab92016-04-20 17:21:55 +02002185static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02002186build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2187{
Florin Corasa2157cf2016-08-16 21:09:14 +02002188 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02002189 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02002190 locator_t *loc;
2191 u32 *loc_indexp;
2192 ip_interface_address_t *ia = 0;
2193 gid_address_t gid_data, *gid = &gid_data;
2194 gid_address_t *rlocs = 0;
2195 ip_prefix_t *ippref = &gid_address_ippref (gid);
2196 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02002197
Filip Tehlar324112f2016-06-02 16:07:38 +02002198 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02002199 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02002200 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02002201 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002202 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02002203 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2204
Filip Tehlar254b0362016-04-07 10:04:34 +02002205 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02002206
2207 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02002208 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2209 loc->sw_if_index, 1 /* unnumbered */,
2210 ({
Florin Coras1c494842016-06-16 21:08:56 +02002211 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2212 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02002213 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002214 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002215 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002216 }));
2217
Filip Tehlar254b0362016-04-07 10:04:34 +02002218 /* Add ipv6 locators */
2219 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2220 loc->sw_if_index, 1 /* unnumbered */,
2221 ({
Florin Coras1c494842016-06-16 21:08:56 +02002222 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2223 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02002224 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002225 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002226 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002227 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002228 /* *INDENT-ON* */
2229
Filip Tehlar254b0362016-04-07 10:04:34 +02002230 }
2231 return rlocs;
2232}
2233
Florin Corase127a7e2016-02-18 22:20:01 +01002234static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002235build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2236 ip_address_t * sloc, ip_address_t * rloc,
2237 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2238{
2239 vlib_buffer_t *b;
2240 u32 bi;
2241 vlib_main_t *vm = lcm->vlib_main;
2242
2243 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2244 {
2245 clib_warning ("Can't allocate buffer for Map-Request!");
2246 return 0;
2247 }
2248
2249 b = vlib_get_buffer (vm, bi);
2250
2251 /* leave some space for the encap headers */
2252 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2253
2254 /* put lisp msg */
2255 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2256 1 /* rloc probe */ , nonce_res);
2257
2258 /* push outer ip header */
2259 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2260 rloc);
2261
2262 bi_res[0] = bi;
2263
2264 return b;
2265}
2266
2267static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002268build_encapsulated_map_request (lisp_cp_main_t * lcm,
2269 gid_address_t * seid, gid_address_t * deid,
2270 locator_set_t * loc_set, ip_address_t * mr_ip,
2271 ip_address_t * sloc, u8 is_smr_invoked,
2272 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01002273{
Florin Corasa2157cf2016-08-16 21:09:14 +02002274 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01002275 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02002276 gid_address_t *rlocs = 0;
2277 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01002278
2279 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2280 {
2281 clib_warning ("Can't allocate buffer for Map-Request!");
2282 return 0;
2283 }
2284
2285 b = vlib_get_buffer (vm, bi);
2286
2287 /* leave some space for the encap headers */
2288 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2289
Filip Tehlar254b0362016-04-07 10:04:34 +02002290 /* get rlocs */
2291 rlocs = build_itr_rloc_list (lcm, loc_set);
2292
Filip Tehlard5fcc462016-10-17 16:20:18 +02002293 if (MR_MODE_SRC_DST == lcm->map_request_mode
2294 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02002295 {
2296 gid_address_t sd;
2297 memset (&sd, 0, sizeof (sd));
2298 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002299 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2300 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002301 }
2302 else
2303 {
2304 /* put lisp msg */
2305 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002306 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002307 }
Florin Corase127a7e2016-02-18 22:20:01 +01002308
2309 /* push ecm: udp-ip-lisp */
2310 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2311
Florin Corase127a7e2016-02-18 22:20:01 +01002312 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02002313 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02002314 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01002315
2316 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02002317
Florin Corasa2157cf2016-08-16 21:09:14 +02002318 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01002319 return b;
2320}
2321
2322static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002323reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01002324{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002325 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2326 r->retries_num = 0;
2327}
2328
2329static int
2330elect_map_resolver (lisp_cp_main_t * lcm)
2331{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002332 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002333
2334 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02002335 {
2336 if (!mr->is_down)
2337 {
2338 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2339 lcm->do_map_resolver_election = 0;
2340 return 1;
2341 }
2342 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002343 return 0;
2344}
2345
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002346static void
2347free_map_register_records (mapping_t * maps)
2348{
2349 mapping_t *map;
2350 vec_foreach (map, maps) vec_free (map->locators);
2351
2352 vec_free (maps);
2353}
2354
2355static void
2356add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2357 ip_address_t * probed_loc)
2358{
2359 u32 *li;
2360 locator_t *loc, new;
2361 ip_interface_address_t *ia = 0;
2362 void *addr;
2363 ip_address_t *new_ip = &gid_address_ip (&new.address);
2364
2365 m->locators = 0;
2366 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2367 locator_set_index);
2368 vec_foreach (li, ls->locator_indices)
2369 {
2370 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2371 new = loc[0];
2372 if (loc->local)
2373 {
2374 /* *INDENT-OFF* */
2375 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2376 loc->sw_if_index, 1 /* unnumbered */,
2377 ({
2378 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2379 ia);
2380 ip_address_set (new_ip, addr, IP4);
2381 }));
2382
2383 /* Add ipv6 locators */
2384 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2385 loc->sw_if_index, 1 /* unnumbered */,
2386 ({
2387 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2388 ia);
2389 ip_address_set (new_ip, addr, IP6);
2390 }));
2391 /* *INDENT-ON* */
2392
2393 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2394 new.probed = 1;
2395 }
2396 vec_add1 (m->locators, new);
2397 }
2398}
2399
2400static mapping_t *
2401build_map_register_record_list (lisp_cp_main_t * lcm)
2402{
2403 mapping_t *recs = 0, rec, *m;
2404
2405 /* *INDENT-OFF* */
2406 pool_foreach(m, lcm->mapping_pool,
2407 {
2408 /* for now build only local mappings */
2409 if (!m->local)
2410 continue;
2411
2412 rec = m[0];
2413 add_locators (lcm, &rec, m->locator_set_index, NULL);
2414 vec_add1 (recs, rec);
2415 });
2416 /* *INDENT-ON* */
2417
2418 return recs;
2419}
2420
2421static int
2422update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2423 lisp_key_type_t key_id, u8 * key,
2424 u16 auth_data_len, u32 msg_len)
2425{
2426 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2427 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2428
2429 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2430 (unsigned char *) map_reg_hdr, msg_len, NULL,
2431 NULL);
2432 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2433
2434 return 0;
2435}
2436
2437static vlib_buffer_t *
2438build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2439 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2440 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2441 u32 * bi_res)
2442{
2443 void *map_reg_hdr;
2444 vlib_buffer_t *b;
2445 u32 bi, auth_data_len = 0, msg_len = 0;
2446 vlib_main_t *vm = lcm->vlib_main;
2447
2448 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2449 {
2450 clib_warning ("Can't allocate buffer for Map-Register!");
2451 return 0;
2452 }
2453
2454 b = vlib_get_buffer (vm, bi);
2455
2456 /* leave some space for the encap headers */
2457 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2458
2459 auth_data_len = auth_data_len_by_key_id (key_id);
2460 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2461 auth_data_len, nonce_res,
2462 &msg_len);
2463
2464 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2465 msg_len);
2466
2467 /* push outer ip header */
2468 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2469 ms_ip);
2470
2471 bi_res[0] = bi;
2472 return b;
2473}
2474
2475static int
2476get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2477{
2478 lisp_msmr_t *mr;
2479 while (lcm->do_map_resolver_election
2480 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2481 &lcm->active_map_resolver,
2482 ip)))
2483 {
2484 if (0 == elect_map_resolver (lcm))
2485 /* all map resolvers are down */
2486 {
2487 /* restart MR checking by marking all of them up */
2488 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2489 return -1;
2490 }
2491 }
2492 return 0;
2493}
2494
Filip Tehlarcda70662017-01-16 10:30:03 +01002495/* CP output statistics */
2496#define foreach_lisp_cp_output_error \
2497_(MAP_REGISTERS_SENT, "map-registers sent") \
2498_(RLOC_PROBES_SENT, "rloc-probes sent")
2499
2500static char *lisp_cp_output_error_strings[] = {
2501#define _(sym,string) string,
2502 foreach_lisp_cp_output_error
2503#undef _
2504};
2505
2506typedef enum
2507{
2508#define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2509 foreach_lisp_cp_output_error
2510#undef _
2511 LISP_CP_OUTPUT_N_ERROR,
2512} lisp_cp_output_error_t;
2513
2514static uword
2515lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2516 vlib_frame_t * from_frame)
2517{
2518 return 0;
2519}
2520
2521/* dummy node used only for statistics */
2522/* *INDENT-OFF* */
2523VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2524 .function = lisp_cp_output,
2525 .name = "lisp-cp-output",
2526 .vector_size = sizeof (u32),
2527 .format_trace = format_lisp_cp_input_trace,
2528 .type = VLIB_NODE_TYPE_INTERNAL,
2529
2530 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2531 .error_strings = lisp_cp_output_error_strings,
2532
2533 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2534
2535 .next_nodes = {
2536 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2537 },
2538};
2539/* *INDENT-ON* */
2540
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002541static int
2542send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2543 u32 local_locator_set_index, ip_address_t * sloc,
2544 ip_address_t * rloc)
2545{
2546 locator_set_t *ls;
2547 u32 bi;
2548 vlib_buffer_t *b;
2549 vlib_frame_t *f;
2550 u64 nonce = 0;
2551 u32 next_index, *to_next;
2552 gid_address_t *itr_rlocs;
2553
2554 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2555 itr_rlocs = build_itr_rloc_list (lcm, ls);
2556
2557 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2558 vec_free (itr_rlocs);
2559 if (!b)
2560 return -1;
2561
2562 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2563
Filip Tehlar272c69e2017-02-15 16:40:35 +01002564 next_index = (ip_addr_version (rloc) == IP4) ?
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002565 ip4_lookup_node.index : ip6_lookup_node.index;
2566
2567 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2568
2569 /* Enqueue the packet */
2570 to_next = vlib_frame_vector_args (f);
2571 to_next[0] = bi;
2572 f->n_vectors = 1;
2573 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2574
2575 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2576 return 0;
2577}
2578
2579static int
2580send_rloc_probes (lisp_cp_main_t * lcm)
2581{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002582 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002583 mapping_t *lm;
2584 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002585 locator_pair_t *lp;
Filip Tehlarcda70662017-01-16 10:30:03 +01002586 u32 si, rloc_probes_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002587
2588 /* *INDENT-OFF* */
2589 pool_foreach (e, lcm->fwd_entry_pool,
2590 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002591 if (vec_len (e->locator_pairs) == 0)
2592 continue;
2593
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002594 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2595 if (~0 == si)
2596 {
2597 clib_warning ("internal error: cannot find local eid %U in "
2598 "map-cache!", format_gid_address, &e->leid);
2599 continue;
2600 }
2601 lm = pool_elt_at_index (lcm->mapping_pool, si);
2602
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002603 /* get the best (lowest) priority */
2604 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002605
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002606 /* send rloc-probe for pair(s) with the best remote locator priority */
2607 vec_foreach (lp, e->locator_pairs)
2608 {
2609 if (lp->priority != lprio)
2610 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002611
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002612 /* get first remote locator */
2613 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2614 &lp->rmt_loc);
Filip Tehlarcda70662017-01-16 10:30:03 +01002615 rloc_probes_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002616 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002617 });
2618 /* *INDENT-ON* */
2619
Filip Tehlarcda70662017-01-16 10:30:03 +01002620 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2621 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2622 rloc_probes_sent);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002623 return 0;
2624}
2625
2626static int
2627send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2628{
Filip Tehlarcda70662017-01-16 10:30:03 +01002629 u32 bi, map_registers_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002630 vlib_buffer_t *b;
2631 ip_address_t sloc;
2632 vlib_frame_t *f;
2633 u64 nonce = 0;
2634 u32 next_index, *to_next;
2635 ip_address_t *ms = 0;
Filip Tehlarcf121c82017-05-03 10:12:44 +02002636 mapping_t *records, *r, *group, *k;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002637
2638 // TODO: support multiple map servers and do election
2639 if (0 == vec_len (lcm->map_servers))
2640 return -1;
2641
2642 ms = &lcm->map_servers[0].address;
2643
2644 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2645 {
2646 clib_warning ("no eligible interface address found for %U!",
2647 format_ip_address, &lcm->map_servers[0]);
2648 return -1;
2649 }
2650
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002651 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002652 if (!records)
2653 return -1;
2654
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002655 vec_foreach (r, records)
2656 {
2657 u8 *key = r->key;
2658 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002659
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002660 if (!key)
2661 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002662
Filip Tehlarcf121c82017-05-03 10:12:44 +02002663 group = 0;
2664 vec_add1 (group, r[0]);
2665
2666 /* group mappings that share common key */
2667 for (k = r + 1; k < vec_end (records); k++)
2668 {
2669 if (k->key_id != r->key_id)
2670 continue;
2671
2672 if (vec_is_equal (k->key, r->key))
2673 {
2674 vec_add1 (group, k[0]);
2675 k->key = 0; /* don't process this mapping again */
2676 }
2677 }
2678
2679 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, group,
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002680 key_id, key, &bi);
Filip Tehlarcf121c82017-05-03 10:12:44 +02002681 vec_free (group);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002682 if (!b)
2683 continue;
2684
2685 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2686
2687 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2688 ip4_lookup_node.index : ip6_lookup_node.index;
2689
2690 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2691
2692 /* Enqueue the packet */
2693 to_next = vlib_frame_vector_args (f);
2694 to_next[0] = bi;
2695 f->n_vectors = 1;
2696 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Filip Tehlarcda70662017-01-16 10:30:03 +01002697 map_registers_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002698
2699 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2700 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002701 free_map_register_records (records);
2702
Filip Tehlarcda70662017-01-16 10:30:03 +01002703 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2704 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2705 map_registers_sent);
2706
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002707 return 0;
2708}
2709
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002710#define send_encapsulated_map_request(lcm, seid, deid, smr) \
2711 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2712
2713#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2714 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2715
2716static int
Florin Corasa2157cf2016-08-16 21:09:14 +02002717_send_encapsulated_map_request (lisp_cp_main_t * lcm,
2718 gid_address_t * seid, gid_address_t * deid,
2719 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002720{
Florin Corasa2157cf2016-08-16 21:09:14 +02002721 u32 next_index, bi = 0, *to_next, map_index;
2722 vlib_buffer_t *b;
2723 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01002724 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002725 locator_set_t *loc_set;
2726 mapping_t *map;
2727 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002728 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002729 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002730
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002731 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002732
2733 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002734 pool_foreach(pmr, lcm->pending_map_requests_pool,
2735 ({
2736 if (!gid_address_cmp (&pmr->src, seid)
2737 && !gid_address_cmp (&pmr->dst, deid))
2738 {
2739 duplicate_pmr = pmr;
2740 break;
2741 }
2742 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002743 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002744
2745 if (!is_resend && duplicate_pmr)
2746 {
2747 /* don't send the request if there is a pending map request already */
2748 return 0;
2749 }
2750
Florin Corase127a7e2016-02-18 22:20:01 +01002751 /* get locator-set for seid */
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02002752 if (!lcm->lisp_pitr && gid_address_type (deid) != GID_ADDR_NSH)
Florin Corase127a7e2016-02-18 22:20:01 +01002753 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02002754 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2755 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002756 {
2757 clib_warning ("No local mapping found in eid-table for %U!",
2758 format_gid_address, seid);
2759 return -1;
2760 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02002761
2762 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2763
2764 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002765 {
2766 clib_warning
2767 ("Mapping found for src eid %U is not marked as local!",
2768 format_gid_address, seid);
2769 return -1;
2770 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002771 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02002772 }
2773 else
2774 {
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02002775 if (lcm->lisp_pitr)
2776 {
2777 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2778 ls_index = map->locator_set_index;
2779 }
2780 else
2781 {
2782 if (lcm->nsh_map_index == (u32) ~ 0)
2783 {
2784 clib_warning ("No locator-set defined for NSH!");
2785 return -1;
2786 }
2787 else
2788 {
2789 map = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index);
2790 ls_index = map->locator_set_index;
2791 }
2792 }
Florin Corase127a7e2016-02-18 22:20:01 +01002793 }
2794
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002795 /* overwrite locator set if map-request itr-rlocs configured */
2796 if (~0 != lcm->mreq_itr_rlocs)
2797 {
2798 ls_index = lcm->mreq_itr_rlocs;
2799 }
2800
2801 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002802
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002803 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002804 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002805 if (duplicate_pmr)
2806 duplicate_pmr->to_be_removed = 1;
2807 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002808 }
Florin Corasddfafb82016-05-13 18:09:56 +02002809
Florin Corase127a7e2016-02-18 22:20:01 +01002810 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002811 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02002812 &lcm->active_map_resolver,
2813 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01002814
2815 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002816 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01002817
Florin Corasf727db92016-06-23 15:01:58 +02002818 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02002819 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2820 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2821 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01002822
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002823 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002824
2825 /* Enqueue the packet */
2826 to_next = vlib_frame_vector_args (f);
2827 to_next[0] = bi;
2828 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002829 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01002830
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002831 if (duplicate_pmr)
2832 /* if there is a pending request already update it */
2833 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002834 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2835 {
2836 /* remove the oldest nonce */
2837 u64 CLIB_UNUSED (tmp), *nonce_del;
2838 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2839 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2840 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2841 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002842
Florin Corasf4691cd2016-08-15 19:16:32 +02002843 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002844 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002845 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002846 }
2847 else
2848 {
2849 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002850 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002851 memset (pmr, 0, sizeof (*pmr));
2852 gid_address_copy (&pmr->src, seid);
2853 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02002854 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002855 pmr->is_smr_invoked = is_smr_invoked;
2856 reset_pending_mr_counters (pmr);
2857 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002858 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002859 }
2860
2861 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002862}
2863
2864static void
Florin Corasa2157cf2016-08-16 21:09:14 +02002865get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01002866{
Florin Corasa2157cf2016-08-16 21:09:14 +02002867 ip4_header_t *ip4 = hdr;
2868 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01002869
2870 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2871 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002872 ip_address_set (src, &ip4->src_address, IP4);
2873 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01002874 }
2875 else
2876 {
2877 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002878 ip_address_set (src, &ip6->src_address, IP6);
2879 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01002880 }
2881}
2882
Filip Tehlar324112f2016-06-02 16:07:38 +02002883static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02002884lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02002885 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02002886{
Florin Corasa2157cf2016-08-16 21:09:14 +02002887 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002888 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002889
Florin Coras87e40692016-09-22 18:02:17 +02002890 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2891 IP4 ? FIB_PROTOCOL_IP4 :
2892 FIB_PROTOCOL_IP6),
2893 vnet_buffer
2894 (b)->sw_if_index
2895 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02002896
2897 vnip = hash_get (lcm->vni_by_table_id, table_id);
2898 if (vnip)
2899 vni = vnip[0];
2900 else
2901 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2902
2903 return vni;
2904}
2905
Florin Coras1a1adc72016-07-22 01:45:30 +02002906always_inline u32
Filip Tehlard5a65db2017-05-17 17:21:10 +02002907lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
Florin Coras1a1adc72016-07-22 01:45:30 +02002908{
Florin Coras1a1adc72016-07-22 01:45:30 +02002909 u32 sw_if_index0;
2910
Florin Corasa2157cf2016-08-16 21:09:14 +02002911 l2input_main_t *l2im = &l2input_main;
2912 l2_input_config_t *config;
2913 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02002914
Florin Corasa2157cf2016-08-16 21:09:14 +02002915 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2916 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02002917 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2918
Filip Tehlard5a65db2017-05-17 17:21:10 +02002919 return bd_config->bd_id;
2920}
2921
2922always_inline u32
2923lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2924{
2925 uword *vnip;
2926 u32 vni = ~0;
2927 u32 bd = lisp_get_bd_from_buffer_eth (b);
2928
2929 vnip = hash_get (lcm->vni_by_bd_id, bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02002930 if (vnip)
2931 vni = vnip[0];
2932 else
Filip Tehlard5a65db2017-05-17 17:21:10 +02002933 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02002934
2935 return vni;
2936}
2937
Filip Tehlar4868ff62017-03-09 16:48:39 +01002938void
Florin Corasa2157cf2016-08-16 21:09:14 +02002939get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Filip Tehlar4868ff62017-03-09 16:48:39 +01002940 gid_address_t * src, gid_address_t * dst,
2941 u16 type)
Florin Coras1a1adc72016-07-22 01:45:30 +02002942{
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02002943 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02002944 u32 vni = 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02002945
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002946 memset (src, 0, sizeof (*src));
2947 memset (dst, 0, sizeof (*dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002948
Filip Tehlard5a65db2017-05-17 17:21:10 +02002949 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
2950 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
2951
Florin Coras1a1adc72016-07-22 01:45:30 +02002952 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2953 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002954 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002955 u8 version, preflen;
2956
Florin Corasa2157cf2016-08-16 21:09:14 +02002957 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2958 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02002959
2960 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02002961 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002962
Florin Corasa2157cf2016-08-16 21:09:14 +02002963 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02002964 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02002965 gid_address_ippref_len (src) = preflen;
2966 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02002967
2968 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2969 gid_address_vni (dst) = vni;
2970 gid_address_vni (src) = vni;
2971 }
2972 else if (LISP_AFI_MAC == type)
2973 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02002974 ethernet_arp_header_t *ah;
Florin Coras1a1adc72016-07-22 01:45:30 +02002975
2976 eh = vlib_buffer_get_current (b);
2977
Filip Tehlard5a65db2017-05-17 17:21:10 +02002978 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
2979 {
2980 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
2981 if (clib_net_to_host_u16 (ah->opcode)
2982 != ETHERNET_ARP_OPCODE_request)
2983 return;
Florin Coras1a1adc72016-07-22 01:45:30 +02002984
Filip Tehlard5a65db2017-05-17 17:21:10 +02002985 gid_address_type (dst) = GID_ADDR_ARP;
2986 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
2987 clib_memcpy (&gid_address_arp_ip4 (dst),
2988 &ah->ip4_over_ethernet[1].ip4, 4);
2989 }
2990 else
2991 {
2992 gid_address_type (src) = GID_ADDR_MAC;
2993 gid_address_type (dst) = GID_ADDR_MAC;
2994 mac_copy (&gid_address_mac (src), eh->src_address);
2995 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02002996
Filip Tehlard5a65db2017-05-17 17:21:10 +02002997 /* get vni */
2998 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2999
3000 gid_address_vni (dst) = vni;
3001 gid_address_vni (src) = vni;
3002 }
Florin Coras1a1adc72016-07-22 01:45:30 +02003003 }
Florin Corasce1b4c72017-01-26 14:25:34 -08003004 else if (LISP_AFI_LCAF == type)
3005 {
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003006 lisp_nsh_hdr_t *nh;
3007 eh = vlib_buffer_get_current (b);
3008
3009 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_NSH)
3010 {
3011 nh = (lisp_nsh_hdr_t *) (((u8 *) eh) + sizeof (*eh));
3012 u32 spi = clib_net_to_host_u32 (nh->spi_si << 8);
3013 u8 si = (u8) clib_net_to_host_u32 (nh->spi_si);
3014 gid_address_nsh_spi (dst) = spi;
3015 gid_address_nsh_si (dst) = si;
3016
3017 gid_address_type (dst) = GID_ADDR_NSH;
3018 }
Florin Corasce1b4c72017-01-26 14:25:34 -08003019 }
Florin Coras1a1adc72016-07-22 01:45:30 +02003020}
3021
Florin Corase127a7e2016-02-18 22:20:01 +01003022static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003023lisp_cp_lookup_inline (vlib_main_t * vm,
3024 vlib_node_runtime_t * node,
3025 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01003026{
Filip Tehlard5a65db2017-05-17 17:21:10 +02003027 u32 *from, *to_next, di, si;
Florin Corasa2157cf2016-08-16 21:09:14 +02003028 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlard5a65db2017-05-17 17:21:10 +02003029 u32 pkts_mapped = 0, next_index;
3030 uword n_left_from, n_left_to_next;
3031 vnet_main_t *vnm = vnet_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01003032
3033 from = vlib_frame_vector_args (from_frame);
3034 n_left_from = from_frame->n_vectors;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003035 next_index = node->cached_next_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003036
3037 while (n_left_from > 0)
3038 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02003039 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01003040
Filip Tehlard5a65db2017-05-17 17:21:10 +02003041 while (n_left_from > 0 && n_left_to_next > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003042 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02003043 u32 pi0, sw_if_index0, next0;
3044 u64 mac0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003045 vlib_buffer_t *b0;
3046 gid_address_t src, dst;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003047 ethernet_arp_header_t *arp0;
3048 ethernet_header_t *eth0;
3049 vnet_hw_interface_t *hw_if0;
Florin Corase127a7e2016-02-18 22:20:01 +01003050
Florin Corasa2157cf2016-08-16 21:09:14 +02003051 pi0 = from[0];
3052 from += 1;
3053 n_left_from -= 1;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003054 to_next[0] = pi0;
3055 to_next += 1;
3056 n_left_to_next -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003057
Florin Corasa2157cf2016-08-16 21:09:14 +02003058 b0 = vlib_get_buffer (vm, pi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003059
Florin Corasa2157cf2016-08-16 21:09:14 +02003060 /* src/dst eid pair */
Filip Tehlar4868ff62017-03-09 16:48:39 +01003061 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
Filip Tehlar324112f2016-06-02 16:07:38 +02003062
Filip Tehlard5a65db2017-05-17 17:21:10 +02003063 if (gid_address_type (&dst) == GID_ADDR_ARP)
3064 {
3065 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3066 if (GID_LOOKUP_MISS_L2 != mac0)
3067 {
3068 /* send ARP reply */
3069
3070 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3071 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
3072
3073 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3074
3075 eth0 = vlib_buffer_get_current (b0);
3076 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
3077 + sizeof (*eth0));
3078 arp0->opcode =
3079 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
3080 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
3081 clib_memcpy (arp0->ip4_over_ethernet[0].ethernet,
3082 (u8 *) & mac0, 6);
3083 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
3084 &gid_address_arp_ip4 (&dst), 4);
3085
3086 /* Hardware must be ethernet-like. */
3087 ASSERT (vec_len (hw_if0->hw_address) == 6);
3088
3089 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
3090 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
3091
3092 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
3093 next0 = LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX;
3094 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3095 to_next,
3096 n_left_to_next, pi0,
3097 next0);
3098 }
3099 continue;
3100 }
3101
Florin Corasa2157cf2016-08-16 21:09:14 +02003102 /* if we have remote mapping for destination already in map-chache
3103 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02003104 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3105 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02003106 if (~0 != di)
3107 {
3108 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3109 /* send a map-request also in case of negative mapping entry
3110 with corresponding action */
3111 if (m->action == LISP_SEND_MAP_REQUEST)
3112 {
3113 /* send map-request */
3114 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3115 0 /* is_resend */ );
3116 pkts_mapped++;
3117 }
3118 else
3119 {
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003120 if (GID_ADDR_NSH != gid_address_type (&dst))
3121 {
3122 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3123 &src);
3124 }
3125 else
3126 si = lcm->nsh_map_index;
3127
Florin Corasa2157cf2016-08-16 21:09:14 +02003128 if (~0 != si)
3129 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01003130 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02003131 }
3132 }
3133 }
3134 else
3135 {
3136 /* send map-request */
3137 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3138 0 /* is_resend */ );
3139 pkts_mapped++;
3140 }
Florin Corase127a7e2016-02-18 22:20:01 +01003141
Filip Tehlard5a65db2017-05-17 17:21:10 +02003142 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Florin Corasa2157cf2016-08-16 21:09:14 +02003143 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3144 {
3145 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3146 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003147
Florin Corasa2157cf2016-08-16 21:09:14 +02003148 memset (tr, 0, sizeof (*tr));
3149 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02003150 ip_address_copy (&tr->map_resolver_ip,
3151 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02003152 }
3153 gid_address_free (&dst);
3154 gid_address_free (&src);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003155 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3156 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3157 to_next,
3158 n_left_to_next, pi0, next0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003159 }
Florin Corase127a7e2016-02-18 22:20:01 +01003160
Filip Tehlard5a65db2017-05-17 17:21:10 +02003161 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01003162 }
3163 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02003164 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3165 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01003166 return from_frame->n_vectors;
3167}
3168
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003169static uword
3170lisp_cp_lookup_ip4 (vlib_main_t * vm,
3171 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3172{
3173 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3174}
3175
3176static uword
3177lisp_cp_lookup_ip6 (vlib_main_t * vm,
3178 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3179{
3180 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3181}
3182
Neale Ranns5e575b12016-10-03 09:40:25 +01003183static uword
3184lisp_cp_lookup_l2 (vlib_main_t * vm,
3185 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3186{
3187 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3188}
3189
Florin Corasce1b4c72017-01-26 14:25:34 -08003190static uword
3191lisp_cp_lookup_nsh (vlib_main_t * vm,
3192 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3193{
3194 /* TODO decide if NSH should be propagated as LCAF or not */
3195 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3196}
3197
Florin Corasa2157cf2016-08-16 21:09:14 +02003198/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003199VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3200 .function = lisp_cp_lookup_ip4,
3201 .name = "lisp-cp-lookup-ip4",
3202 .vector_size = sizeof (u32),
3203 .format_trace = format_lisp_cp_lookup_trace,
3204 .type = VLIB_NODE_TYPE_INTERNAL,
3205
3206 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3207 .error_strings = lisp_cp_lookup_error_strings,
3208
3209 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3210
3211 .next_nodes = {
3212 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003213 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003214 },
3215};
3216/* *INDENT-ON* */
3217
3218/* *INDENT-OFF* */
3219VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3220 .function = lisp_cp_lookup_ip6,
3221 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01003222 .vector_size = sizeof (u32),
3223 .format_trace = format_lisp_cp_lookup_trace,
3224 .type = VLIB_NODE_TYPE_INTERNAL,
3225
3226 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3227 .error_strings = lisp_cp_lookup_error_strings,
3228
3229 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3230
3231 .next_nodes = {
3232 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003233 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Neale Ranns5e575b12016-10-03 09:40:25 +01003234 },
3235};
3236/* *INDENT-ON* */
3237
3238/* *INDENT-OFF* */
3239VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3240 .function = lisp_cp_lookup_l2,
3241 .name = "lisp-cp-lookup-l2",
3242 .vector_size = sizeof (u32),
3243 .format_trace = format_lisp_cp_lookup_trace,
3244 .type = VLIB_NODE_TYPE_INTERNAL,
3245
3246 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3247 .error_strings = lisp_cp_lookup_error_strings,
3248
3249 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3250
3251 .next_nodes = {
3252 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003253 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Florin Corase127a7e2016-02-18 22:20:01 +01003254 },
3255};
Florin Corasa2157cf2016-08-16 21:09:14 +02003256/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003257
Florin Corasce1b4c72017-01-26 14:25:34 -08003258/* *INDENT-OFF* */
3259VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3260 .function = lisp_cp_lookup_nsh,
3261 .name = "lisp-cp-lookup-nsh",
3262 .vector_size = sizeof (u32),
3263 .format_trace = format_lisp_cp_lookup_trace,
3264 .type = VLIB_NODE_TYPE_INTERNAL,
3265
3266 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3267 .error_strings = lisp_cp_lookup_error_strings,
3268
3269 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3270
3271 .next_nodes = {
3272 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003273 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Florin Corasce1b4c72017-01-26 14:25:34 -08003274 },
3275};
3276/* *INDENT-ON* */
3277
Florin Corase127a7e2016-02-18 22:20:01 +01003278/* lisp_cp_input statistics */
Filip Tehlarcda70662017-01-16 10:30:03 +01003279#define foreach_lisp_cp_input_error \
3280_(DROP, "drop") \
3281_(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3282_(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3283_(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
Florin Corase127a7e2016-02-18 22:20:01 +01003284_(MAP_REPLIES_RECEIVED, "map-replies received")
3285
Florin Corasa2157cf2016-08-16 21:09:14 +02003286static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003287#define _(sym,string) string,
3288 foreach_lisp_cp_input_error
3289#undef _
3290};
3291
3292typedef enum
3293{
3294#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003295 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01003296#undef _
3297 LISP_CP_INPUT_N_ERROR,
3298} lisp_cp_input_error_t;
3299
Florin Corase127a7e2016-02-18 22:20:01 +01003300typedef struct
3301{
3302 gid_address_t dst_eid;
3303 ip4_address_t map_resolver_ip;
3304} lisp_cp_input_trace_t;
3305
3306u8 *
3307format_lisp_cp_input_trace (u8 * s, va_list * args)
3308{
3309 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3310 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003311 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3312 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003313
3314 s = format (s, "LISP-CP-INPUT: TODO");
3315 return s;
3316}
3317
Filip Tehlar9677a942016-11-28 10:23:31 +01003318static void
3319remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3320{
3321 mapping_t *m;
Florin Corasf3dc11a2017-01-24 03:13:43 -08003322 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3323 memset (adj_args, 0, sizeof (adj_args[0]));
Filip Tehlar9677a942016-11-28 10:23:31 +01003324
3325 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasf3dc11a2017-01-24 03:13:43 -08003326
3327 gid_address_copy (&adj_args->reid, &m->eid);
3328 adj_args->is_add = 0;
3329 if (vnet_lisp_add_del_adjacency (adj_args))
3330 clib_warning ("failed to del adjacency!");
3331
Filip Tehlar9677a942016-11-28 10:23:31 +01003332 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3333 0 /* is_static */ , 0);
3334 mapping_delete_timer (lcm, mi);
3335}
3336
3337static void
3338mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3339 f64 expiration_time)
3340{
3341 mapping_t *m;
3342 u64 now = clib_cpu_time_now ();
3343 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3344 u64 exp_clock_time = now + expiration_time * cpu_cps;
3345
3346 m = pool_elt_at_index (lcm->mapping_pool, mi);
3347
3348 m->timer_set = 1;
3349 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3350}
3351
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003352static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003353map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01003354{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003355 mapping_t *m;
3356 vec_foreach (m, a->mappings)
3357 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003358 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003359 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003360 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003361
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003362 clib_mem_free (a);
3363}
3364
3365void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003366process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003367{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003368 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003369 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3370 u32 dst_map_index = 0;
3371 pending_map_request_t *pmr;
3372 u64 *noncep;
3373 uword *pmr_index;
3374
3375 if (a->is_rloc_probe)
3376 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003377
Florin Corase127a7e2016-02-18 22:20:01 +01003378 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003379 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01003380 if (!pmr_index)
3381 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003382 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003383 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003384 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003385 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01003386
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003387 vec_foreach (m, a->mappings)
3388 {
3389 /* insert/update mappings cache */
3390 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3391 m->authoritative, m->ttl,
3392 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003393
Florin Corasba888e42017-01-24 11:38:18 -08003394 if (dst_map_index == (u32) ~ 0)
3395 continue;
3396
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003397 /* try to program forwarding only if mapping saved or updated */
Florin Corasba888e42017-01-24 11:38:18 -08003398 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3399 memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08003400
Florin Corasba888e42017-01-24 11:38:18 -08003401 gid_address_copy (&adj_args->leid, &pmr->src);
3402 gid_address_copy (&adj_args->reid, &m->eid);
3403 adj_args->is_add = 1;
3404 if (vnet_lisp_add_del_adjacency (adj_args))
3405 clib_warning ("failed to add adjacency!");
Florin Corasf3dc11a2017-01-24 03:13:43 -08003406
Florin Corasba888e42017-01-24 11:38:18 -08003407 if ((u32) ~ 0 != m->ttl)
3408 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003409 }
Florin Corase127a7e2016-02-18 22:20:01 +01003410
3411 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02003412
3413 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02003414 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003415 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02003416 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003417 /* *INDENT-ON* */
3418
3419 clib_fifo_free (pmr->nonces);
3420 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003421
3422done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003423 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003424 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003425}
3426
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003427static int
3428is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3429 lisp_key_type_t key_id, u8 * key)
3430{
3431 u8 *auth_data = 0;
3432 u16 auth_data_len;
3433 int result;
3434
3435 auth_data_len = auth_data_len_by_key_id (key_id);
3436 if ((u16) ~ 0 == auth_data_len)
3437 {
3438 clib_warning ("invalid length for key_id %d!", key_id);
3439 return 0;
3440 }
3441
3442 /* save auth data */
3443 vec_validate (auth_data, auth_data_len - 1);
3444 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3445
3446 /* clear auth data */
3447 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3448
3449 /* get hash of the message */
3450 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3451 (unsigned char *) h, msg_len, NULL, NULL);
3452
3453 result = memcmp (code, auth_data, auth_data_len);
3454
3455 vec_free (auth_data);
3456
3457 return !result;
3458}
3459
3460static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003461process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003462{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003463 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003464 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003465
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003466 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003467 if (!pmr_index)
3468 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003469 clib_warning ("No pending map-register entry with nonce %lu!",
3470 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003471 return;
3472 }
3473
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003474 map_records_arg_free (a);
3475 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3476}
3477
3478static mapping_t *
3479get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3480{
3481 u32 mi;
3482
3483 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3484 if (~0 == mi)
3485 {
3486 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3487 e);
3488 return 0;
3489 }
3490 return pool_elt_at_index (lcm->mapping_pool, mi);
3491}
3492
3493/**
3494 * When map-notify is received it is necessary that all EIDs in the record
3495 * list share common key. The key is then used to verify authentication
3496 * data in map-notify message.
3497 */
3498static int
3499map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3500 u32 key_id, u8 ** key_out)
3501{
3502 u32 i, len = vec_len (maps);
3503 mapping_t *m;
3504
3505 /* get key of the first mapping */
3506 m = get_mapping (lcm, &maps[0].eid);
3507 if (!m || !m->key)
3508 return -1;
3509
3510 key_out[0] = m->key;
3511
3512 for (i = 1; i < len; i++)
3513 {
3514 m = get_mapping (lcm, &maps[i].eid);
3515 if (!m || !m->key)
3516 return -1;
3517
3518 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3519 {
3520 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3521 return -1;
3522 }
3523 }
3524 return 0;
3525}
3526
3527static int
3528parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3529{
3530 locator_t *locators = 0;
3531 u32 i, len;
3532 gid_address_t deid;
3533 mapping_t m;
3534 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003535
Filip Tehlar68c74fc2017-05-04 14:52:42 +02003536 memset (&m, 0, sizeof (m));
3537
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003538 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003539 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003540 {
Florin Corase68de8c2017-06-05 15:38:50 -07003541 locators = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003542 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3543 if (len == ~0)
3544 {
3545 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003546 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003547 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003548 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003549 }
3550
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003551 m.locators = locators;
3552 gid_address_copy (&m.eid, &deid);
3553 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003554 }
3555
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003556 return 0;
3557}
3558
3559static map_records_arg_t *
3560parse_map_notify (vlib_buffer_t * b)
3561{
3562 int rc = 0;
3563 map_notify_hdr_t *mnotif_hdr;
3564 lisp_key_type_t key_id;
3565 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3566 u8 *key = 0;
3567 gid_address_t deid;
3568 u16 auth_data_len = 0;
3569 u8 record_count;
3570 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3571
3572 memset (a, 0, sizeof (*a));
3573 mnotif_hdr = vlib_buffer_get_current (b);
3574 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3575 memset (&deid, 0, sizeof (deid));
3576
3577 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3578 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3579 auth_data_len = auth_data_len_by_key_id (key_id);
3580
3581 /* advance buffer by authentication data */
3582 vlib_buffer_pull (b, auth_data_len);
3583
3584 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3585 rc = parse_map_records (b, a, record_count);
3586 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003587 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003588 map_records_arg_free (a);
3589 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003590 }
3591
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003592 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3593 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003594 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003595 map_records_arg_free (a);
3596 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003597 }
3598
3599 /* verify authentication data */
3600 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003601 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003602 {
3603 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003604 a->nonce);
3605 map_records_arg_free (a);
3606 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003607 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003608 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003609}
3610
3611static vlib_buffer_t *
3612build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3613 ip_address_t * dst, u64 nonce, u8 probe_bit,
3614 mapping_t * records, u16 dst_port, u32 * bi_res)
3615{
3616 vlib_buffer_t *b;
3617 u32 bi;
3618 vlib_main_t *vm = lcm->vlib_main;
3619
3620 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3621 {
3622 clib_warning ("Can't allocate buffer for Map-Register!");
3623 return 0;
3624 }
3625
3626 b = vlib_get_buffer (vm, bi);
3627
3628 /* leave some space for the encap headers */
3629 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3630
3631 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3632
3633 /* push outer ip header */
3634 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3635
3636 bi_res[0] = bi;
3637 return b;
3638}
3639
3640static int
3641send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3642 u8 probe_bit, u64 nonce, u16 dst_port,
3643 ip_address_t * probed_loc)
3644{
3645 ip_address_t src;
3646 u32 bi;
3647 vlib_buffer_t *b;
3648 vlib_frame_t *f;
3649 u32 next_index, *to_next;
3650 mapping_t *records = 0, *m;
3651
3652 m = pool_elt_at_index (lcm->mapping_pool, mi);
3653 if (!m)
3654 return -1;
3655
3656 vec_add1 (records, m[0]);
3657 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3658 memset (&src, 0, sizeof (src));
3659
3660 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3661 {
3662 clib_warning ("can't find inteface address for %U", format_ip_address,
3663 dst);
3664 return -1;
3665 }
3666
3667 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3668 &bi);
3669 if (!b)
3670 return -1;
3671 free_map_register_records (records);
3672
3673 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3674 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3675 ip4_lookup_node.index : ip6_lookup_node.index;
3676
3677 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3678
3679 /* Enqueue the packet */
3680 to_next = vlib_frame_vector_args (f);
3681 to_next[0] = bi;
3682 f->n_vectors = 1;
3683 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3684 return 0;
3685}
3686
Filip Tehlarb601f222017-01-02 10:22:56 +01003687static void
3688find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3689{
3690 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3691 if (start < 0 && start < -sizeof (b->pre_data))
3692 {
3693 *ip_hdr = 0;
3694 return;
3695 }
3696
3697 *ip_hdr = b->data + start;
3698 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3699 *ip_hdr = 0;
3700}
3701
Florin Corase127a7e2016-02-18 22:20:01 +01003702void
Filip Tehlarcda70662017-01-16 10:30:03 +01003703process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3704 lisp_cp_main_t * lcm, vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01003705{
Filip Tehlarb601f222017-01-02 10:22:56 +01003706 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003707 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3708 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02003709 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01003710 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003711 u64 nonce;
Filip Tehlarcda70662017-01-16 10:30:03 +01003712 u32 i, len = 0, rloc_probe_recv = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003713 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003714
3715 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003716 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02003717 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003718 clib_warning
3719 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01003720 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02003721 }
Florin Corase127a7e2016-02-18 22:20:01 +01003722
Filip Tehlarb601f222017-01-02 10:22:56 +01003723 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3724 nonce = MREQ_NONCE (mreq_hdr);
3725
Florin Corase127a7e2016-02-18 22:20:01 +01003726 /* parse src eid */
3727 len = lisp_msg_parse_addr (b, &src);
3728 if (len == ~0)
3729 return;
3730
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003731 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3732 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01003733 if (len == ~0)
Filip Tehlarcda70662017-01-16 10:30:03 +01003734 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003735
3736 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02003737 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01003738 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003739 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01003740 len = lisp_msg_parse_eid_rec (b, &dst);
3741 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003742 {
3743 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003744 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02003745 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003746
3747 if (MREQ_SMR (mreq_hdr))
3748 {
3749 /* send SMR-invoked map-requests */
3750 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3751 }
3752 else if (MREQ_RLOC_PROBE (mreq_hdr))
3753 {
Filip Tehlarb601f222017-01-02 10:22:56 +01003754 find_ip_header (b, &ip_hdr);
3755 if (!ip_hdr)
3756 {
3757 clib_warning ("Cannot find the IP header!");
Filip Tehlarcda70662017-01-16 10:30:03 +01003758 goto done;
Filip Tehlarb601f222017-01-02 10:22:56 +01003759 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003760 rloc_probe_recv++;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003761 memset (&m, 0, sizeof (m));
3762 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3763
3764 // TODO: select best locator; for now use the first one
3765 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3766
3767 /* get src/dst IP addresses */
3768 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3769
3770 // TODO get source port from buffer
3771 u16 src_port = LISP_CONTROL_PORT;
3772
3773 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3774 src_port, &probed_loc);
3775 }
Florin Corase127a7e2016-02-18 22:20:01 +01003776 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003777
3778done:
Filip Tehlarcda70662017-01-16 10:30:03 +01003779 vlib_node_increment_counter (vm, node->node_index,
3780 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3781 rloc_probe_recv);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003782 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003783}
3784
Filip Tehlar816f4372017-04-26 16:09:06 +02003785map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003786parse_map_reply (vlib_buffer_t * b)
3787{
3788 locator_t probed;
3789 gid_address_t deid;
3790 void *h;
3791 u32 i, len = 0;
3792 mapping_t m;
3793 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003794 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003795 memset (a, 0, sizeof (*a));
3796 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003797
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003798 mrep_hdr = vlib_buffer_get_current (b);
3799 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003800 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlar816f4372017-04-26 16:09:06 +02003801 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
3802 {
3803 clib_mem_free (a);
3804 return 0;
3805 }
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003806 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3807
3808 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3809 {
3810 memset (&m, 0, sizeof (m));
3811 locators = 0;
3812 h = vlib_buffer_get_current (b);
3813
3814 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3815 m.action = MAP_REC_ACTION (h);
3816 m.authoritative = MAP_REC_AUTH (h);
3817
3818 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3819 if (len == ~0)
3820 {
3821 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003822 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003823 return 0;
3824 }
3825
3826 m.locators = locators;
3827 gid_address_copy (&m.eid, &deid);
3828 vec_add1 (a->mappings, m);
3829 }
3830 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003831}
3832
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003833static void
3834queue_map_reply_for_processing (map_records_arg_t * a)
3835{
Florin Coras655fcc42017-01-10 08:57:54 -08003836 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003837}
3838
3839static void
3840queue_map_notify_for_processing (map_records_arg_t * a)
3841{
3842 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3843}
3844
Florin Corase127a7e2016-02-18 22:20:01 +01003845static uword
3846lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02003847 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01003848{
Filip Tehlarcda70662017-01-16 10:30:03 +01003849 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3850 map_notifies_recv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003851 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02003852 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003853 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003854
3855 from = vlib_frame_vector_args (from_frame);
3856 n_left_from = from_frame->n_vectors;
3857
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003858
Florin Corase127a7e2016-02-18 22:20:01 +01003859 while (n_left_from > 0)
3860 {
3861 u32 n_left_to_next_drop;
3862
3863 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003864 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003865 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003866 {
3867 u32 bi0;
3868 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01003869
Florin Corasa2157cf2016-08-16 21:09:14 +02003870 bi0 = from[0];
3871 from += 1;
3872 n_left_from -= 1;
3873 to_next_drop[0] = bi0;
3874 to_next_drop += 1;
3875 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003876
Florin Corasa2157cf2016-08-16 21:09:14 +02003877 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003878
Florin Corasa2157cf2016-08-16 21:09:14 +02003879 type = lisp_msg_type (vlib_buffer_get_current (b0));
3880 switch (type)
3881 {
3882 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003883 a = parse_map_reply (b0);
3884 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003885 {
3886 if (a->is_rloc_probe)
3887 rloc_probe_rep_recv++;
3888 queue_map_reply_for_processing (a);
3889 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003890 break;
3891 case LISP_MAP_REQUEST:
Filip Tehlarcda70662017-01-16 10:30:03 +01003892 process_map_request (vm, node, lcm, b0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003893 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003894 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003895 a = parse_map_notify (b0);
3896 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003897 {
3898 map_notifies_recv++;
3899 queue_map_notify_for_processing (a);
3900 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003901 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02003902 default:
3903 clib_warning ("Unsupported LISP message type %d", type);
3904 break;
3905 }
Florin Corase127a7e2016-02-18 22:20:01 +01003906
Florin Corasa2157cf2016-08-16 21:09:14 +02003907 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01003908
Florin Corasa2157cf2016-08-16 21:09:14 +02003909 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3910 {
Florin Corase127a7e2016-02-18 22:20:01 +01003911
Florin Corasa2157cf2016-08-16 21:09:14 +02003912 }
3913 }
Florin Corase127a7e2016-02-18 22:20:01 +01003914
Florin Corasa2157cf2016-08-16 21:09:14 +02003915 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3916 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003917 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003918 vlib_node_increment_counter (vm, node->node_index,
3919 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3920 rloc_probe_rep_recv);
3921 vlib_node_increment_counter (vm, node->node_index,
3922 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3923 map_notifies_recv);
Florin Corase127a7e2016-02-18 22:20:01 +01003924 return from_frame->n_vectors;
3925}
3926
Florin Corasa2157cf2016-08-16 21:09:14 +02003927/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003928VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3929 .function = lisp_cp_input,
3930 .name = "lisp-cp-input",
3931 .vector_size = sizeof (u32),
3932 .format_trace = format_lisp_cp_input_trace,
3933 .type = VLIB_NODE_TYPE_INTERNAL,
3934
3935 .n_errors = LISP_CP_INPUT_N_ERROR,
3936 .error_strings = lisp_cp_input_error_strings,
3937
3938 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3939
3940 .next_nodes = {
3941 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3942 },
3943};
Florin Corasa2157cf2016-08-16 21:09:14 +02003944/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003945
3946clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003947lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01003948{
Florin Corasa2157cf2016-08-16 21:09:14 +02003949 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3950 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003951
3952 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3953 return error;
3954
3955 lcm->im4 = &ip4_main;
3956 lcm->im6 = &ip6_main;
3957 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02003958 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003959 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02003960 lcm->lisp_pitr = 0;
Florin Corasba888e42017-01-24 11:38:18 -08003961 lcm->flags = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02003962 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01003963
3964 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003965 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02003966 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01003967
Florin Coras577c3552016-04-21 00:45:40 +02003968 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02003969 hash_set (lcm->table_id_by_vni, 0, 0);
3970 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02003971
Florin Corase127a7e2016-02-18 22:20:01 +01003972 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02003973 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003974 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02003975 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003976
Filip Tehlar9677a942016-11-28 10:23:31 +01003977 u64 now = clib_cpu_time_now ();
3978 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003979 lcm->nsh_map_index = ~0;
Florin Corase127a7e2016-02-18 22:20:01 +01003980 return 0;
3981}
3982
Filip Tehlar4868ff62017-03-09 16:48:39 +01003983static int
3984lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3985 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3986 u32 stats_index)
3987{
Filip Tehlar21511912017-04-07 10:41:42 +02003988 vlib_counter_t v;
3989 vlib_combined_counter_main_t *cm = &lgm->counters;
Filip Tehlar4868ff62017-03-09 16:48:39 +01003990 lisp_gpe_fwd_entry_key_t fwd_key;
3991 const lisp_gpe_tunnel_t *lgt;
3992 fwd_entry_t *fe;
3993
3994 memset (stat, 0, sizeof (*stat));
3995 memset (&fwd_key, 0, sizeof (fwd_key));
3996
3997 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3998 ASSERT (fe != 0);
3999
4000 gid_to_dp_address (&fe->reid, &stat->deid);
4001 gid_to_dp_address (&fe->leid, &stat->seid);
4002 stat->vni = gid_address_vni (&fe->reid);
4003
4004 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
4005 stat->loc_rloc = lgt->key->lcl;
4006 stat->rmt_rloc = lgt->key->rmt;
4007
Filip Tehlar21511912017-04-07 10:41:42 +02004008 vlib_get_combined_counter (cm, stats_index, &v);
4009 stat->counters = v;
Filip Tehlar4868ff62017-03-09 16:48:39 +01004010 return 1;
4011}
4012
4013lisp_api_stats_t *
4014vnet_lisp_get_stats (void)
4015{
4016 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
4017 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4018 lisp_api_stats_t *stats = 0, stat;
4019 lisp_stats_key_t *key;
4020 u32 index;
4021
4022 /* *INDENT-OFF* */
4023 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
4024 {
4025 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
4026 vec_add1 (stats, stat);
4027 });
4028 /* *INDENT-ON* */
4029
4030 return stats;
4031}
4032
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004033static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02004034send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004035{
Florin Corasa2157cf2016-08-16 21:09:14 +02004036 map_request_args_t *a = arg;
4037 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004038
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004039 if (a->is_resend)
4040 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4041 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004042 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004043
4044 return 0;
4045}
4046
4047static int
4048queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02004049 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004050{
4051 map_request_args_t a;
4052
4053 a.is_resend = is_resend;
4054 gid_address_copy (&a.seid, seid);
4055 gid_address_copy (&a.deid, deid);
4056 a.smr_invoked = smr_invoked;
4057
4058 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02004059 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004060 return 0;
4061}
4062
4063/**
4064 * Take an action with a pending map request depending on expiration time
4065 * and re-try counters.
4066 */
4067static void
4068update_pending_request (pending_map_request_t * r, f64 dt)
4069{
Florin Corasa2157cf2016-08-16 21:09:14 +02004070 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004071 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004072
4073 if (r->time_to_expire - dt < 0)
4074 /* it's time to decide what to do with this pending request */
4075 {
4076 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02004077 /* too many retries -> assume current map resolver is not available */
4078 {
4079 mr = get_map_resolver (&lcm->active_map_resolver);
4080 if (!mr)
4081 {
4082 clib_warning ("Map resolver %U not found - probably deleted "
4083 "by the user recently.", format_ip_address,
4084 &lcm->active_map_resolver);
4085 }
4086 else
4087 {
4088 clib_warning ("map resolver %U is unreachable, ignoring",
4089 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004090
Florin Corasa2157cf2016-08-16 21:09:14 +02004091 /* mark current map resolver unavailable so it won't be
4092 * selected next time */
4093 mr->is_down = 1;
4094 mr->last_update = vlib_time_now (lcm->vlib_main);
4095 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004096
Florin Corasa2157cf2016-08-16 21:09:14 +02004097 reset_pending_mr_counters (r);
4098 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004099
Florin Corasa2157cf2016-08-16 21:09:14 +02004100 /* try to find a next eligible map resolver and re-send */
4101 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4102 1 /* resend */ );
4103 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004104 else
Florin Corasa2157cf2016-08-16 21:09:14 +02004105 {
4106 /* try again */
4107 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4108 1 /* resend */ );
4109 r->retries_num++;
4110 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4111 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004112 }
4113 else
4114 r->time_to_expire -= dt;
4115}
4116
4117static void
4118remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4119{
Florin Corasa2157cf2016-08-16 21:09:14 +02004120 u64 *nonce;
4121 pending_map_request_t *pmr;
4122 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004123
Florin Corasa2157cf2016-08-16 21:09:14 +02004124 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004125 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004126 ({
4127 if (pmr->to_be_removed)
4128 {
4129 clib_fifo_foreach (nonce, pmr->nonces, ({
4130 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4131 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004132
Florin Corasa2157cf2016-08-16 21:09:14 +02004133 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4134 }
4135 }));
4136 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004137
4138 vec_foreach (pmr_index, to_be_removed)
4139 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4140
4141 vec_free (to_be_removed);
4142}
4143
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004144static void
4145update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4146{
4147 static f64 time_left = RLOC_PROBING_INTERVAL;
4148
4149 if (!lcm->is_enabled || !lcm->rloc_probing)
4150 return;
4151
4152 time_left -= dt;
4153 if (time_left <= 0)
4154 {
4155 time_left = RLOC_PROBING_INTERVAL;
4156 send_rloc_probes (lcm);
4157 }
4158}
4159
4160static void
4161update_map_register (lisp_cp_main_t * lcm, f64 dt)
4162{
4163 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4164 static u64 mreg_sent_counter = 0;
4165
4166 if (!lcm->is_enabled || !lcm->map_registering)
4167 return;
4168
4169 time_left -= dt;
4170 if (time_left <= 0)
4171 {
4172 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4173 time_left = MAP_REGISTER_INTERVAL;
4174 else
4175 {
4176 mreg_sent_counter++;
4177 time_left = QUICK_MAP_REGISTER_INTERVAL;
4178 }
4179 send_map_register (lcm, 1 /* want map notify */ );
4180 }
4181}
4182
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004183static uword
4184send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004185 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004186{
Filip Tehlar9677a942016-11-28 10:23:31 +01004187 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004188 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004189 pending_map_request_t *pmr;
4190 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004191
4192 while (1)
4193 {
4194 vlib_process_wait_for_event_or_clock (vm, period);
4195
4196 /* currently no signals are expected - just wait for clock */
4197 (void) vlib_process_get_events (vm, 0);
4198
Florin Corasa2157cf2016-08-16 21:09:14 +02004199 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004200 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004201 ({
4202 if (!pmr->to_be_removed)
4203 update_pending_request (pmr, period);
4204 }));
4205 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004206
4207 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004208
4209 update_map_register (lcm, period);
4210 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004211
4212 u64 now = clib_cpu_time_now ();
4213
4214 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4215 if (vec_len (expired) > 0)
4216 {
4217 u32 *mi = 0;
4218 vec_foreach (mi, expired)
4219 {
4220 remove_expired_mapping (lcm, mi[0]);
4221 }
4222 _vec_len (expired) = 0;
4223 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004224 }
4225
4226 /* unreachable */
4227 return 0;
4228}
4229
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004230vnet_api_error_t
4231vnet_lisp_stats_enable_disable (u8 enable)
4232{
4233 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4234
4235 if (vnet_lisp_enable_disable_status () == 0)
4236 return VNET_API_ERROR_LISP_DISABLED;
4237
Filip Tehlar4868ff62017-03-09 16:48:39 +01004238 if (enable)
4239 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4240 else
4241 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4242
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004243 return 0;
4244}
4245
4246u8
4247vnet_lisp_stats_enable_disable_state (void)
4248{
4249 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4250
4251 if (vnet_lisp_enable_disable_status () == 0)
4252 return VNET_API_ERROR_LISP_DISABLED;
4253
Filip Tehlar4868ff62017-03-09 16:48:39 +01004254 return lcm->flags & LISP_FLAG_STATS_ENABLED;
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004255}
4256
Florin Corasa2157cf2016-08-16 21:09:14 +02004257/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004258VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4259 .function = send_map_resolver_service,
4260 .type = VLIB_NODE_TYPE_PROCESS,
4261 .name = "lisp-retry-service",
4262 .process_log2_n_stack_bytes = 16,
4263};
Florin Corasa2157cf2016-08-16 21:09:14 +02004264/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004265
Florin Corasa2157cf2016-08-16 21:09:14 +02004266VLIB_INIT_FUNCTION (lisp_cp_init);
4267
4268/*
4269 * fd.io coding-style-patch-verification: ON
4270 *
4271 * Local Variables:
4272 * eval: (c-set-style "gnu")
4273 * End:
4274 */