blob: 09f5c0a652dc4ab4bf30c51afc7b457df7b7efb5 [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>
Filip Tehlarc6c43942019-03-21 05:56:59 -070029#include <vnet/crypto/crypto.h>
Filip Tehlar397fd7d2016-10-26 14:31:24 +020030
Filip Tehlaref2a5bf2017-05-30 07:14:46 +020031#define MAX_VALUE_U24 0xffffff
32
Filip Tehlar809bc742017-08-14 19:15:36 +020033/* mapping timer control constants (in seconds) */
34#define TIME_UNTIL_REFETCH_OR_DELETE 20
35#define MAPPING_TIMEOUT (((m->ttl) * 60) - TIME_UNTIL_REFETCH_OR_DELETE)
36
Florin Corasf3dc11a2017-01-24 03:13:43 -080037lisp_cp_main_t lisp_control_main;
38
Filip Tehlarcda70662017-01-16 10:30:03 +010039u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
Filip Tehlar809bc742017-08-14 19:15:36 +020040static void *send_map_request_thread_fn (void *arg);
Filip Tehlarcda70662017-01-16 10:30:03 +010041
42typedef enum
43{
44 LISP_CP_INPUT_NEXT_DROP,
45 LISP_CP_INPUT_N_NEXT,
46} lisp_cp_input_next_t;
47
Filip Tehlara5abdeb2016-07-18 17:35:40 +020048typedef struct
49{
50 u8 is_resend;
51 gid_address_t seid;
52 gid_address_t deid;
53 u8 smr_invoked;
54} map_request_args_t;
55
Florin Corasdca88042016-09-14 16:01:38 +020056u8
57vnet_lisp_get_map_request_mode (void)
58{
59 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
60 return lcm->map_request_mode;
61}
62
Filip Tehlar397fd7d2016-10-26 14:31:24 +020063static u16
64auth_data_len_by_key_id (lisp_key_type_t key_id)
65{
66 switch (key_id)
67 {
68 case HMAC_SHA_1_96:
69 return SHA1_AUTH_DATA_LEN;
70 case HMAC_SHA_256_128:
71 return SHA256_AUTH_DATA_LEN;
72 default:
73 clib_warning ("unsupported key type: %d!", key_id);
74 return (u16) ~ 0;
75 }
76 return (u16) ~ 0;
77}
78
Filip Tehlara5abdeb2016-07-18 17:35:40 +020079static int
80queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020081 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020082
Florin Corasf727db92016-06-23 15:01:58 +020083ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020084ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
85 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +020086{
87 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +020088 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +020089 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
90 sw_if_index = swif->unnumbered_sw_if_index;
91 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +020092 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
93 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
94 (u32) ~ 0;
95 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +020096}
Filip Tehlar215104e2016-05-10 16:58:29 +020097
Florin Corasf727db92016-06-23 15:01:58 +020098void *
99ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200100 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200101{
Florin Corasa2157cf2016-08-16 21:09:14 +0200102 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200103
Florin Corasf727db92016-06-23 15:01:58 +0200104 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
105 if (!ia)
106 return 0;
107 return ip_interface_address_get_address (lm, ia);
108}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200109
Florin Corase127a7e2016-02-18 22:20:01 +0100110int
Florin Corasf727db92016-06-23 15:01:58 +0200111ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200112 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200113{
Florin Corasa2157cf2016-08-16 21:09:14 +0200114 ip_lookup_main_t *lm;
115 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200116
Neale Rannsea93e482019-11-12 17:16:47 +0000117 lm = (version == AF_IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Florin Corasf727db92016-06-23 15:01:58 +0200118 addr = ip_interface_get_first_address (lm, sw_if_index, version);
119 if (!addr)
120 return 0;
121
122 ip_address_set (result, addr, version);
123 return 1;
124}
125
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126/**
127 * convert from a LISP address to a FIB prefix
128 */
129void
130ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200131{
Neale Rannsea93e482019-11-12 17:16:47 +0000132 if (addr->version == AF_IP4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133 {
134 prefix->fp_len = 32;
135 prefix->fp_proto = FIB_PROTOCOL_IP4;
Dave Barachb7b92992018-10-17 10:38:51 -0400136 clib_memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
Neale Ranns3ec09e92020-02-24 13:32:30 +0000137 memcpy (&prefix->fp_addr.ip4, &addr->ip.ip4,
138 sizeof (prefix->fp_addr.ip4));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100139 }
Florin Corasf727db92016-06-23 15:01:58 +0200140 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100141 {
142 prefix->fp_len = 128;
143 prefix->fp_proto = FIB_PROTOCOL_IP6;
Neale Ranns3ec09e92020-02-24 13:32:30 +0000144 memcpy (&prefix->fp_addr.ip6, &addr->ip.ip6,
145 sizeof (prefix->fp_addr.ip6));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100146 }
Florin Coras6ce954f2020-02-20 16:11:23 +0000147 prefix->___fp___pad = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200148}
149
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100150/**
151 * convert from a LISP to a FIB prefix
152 */
153void
154ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
155 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200156{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100157 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
158 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200159}
160
161/**
162 * Find the sw_if_index of the interface that would be used to egress towards
163 * dst.
164 */
165u32
166ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
167{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100168 fib_node_index_t fei;
169 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200170
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100171 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200172
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100173 fei = fib_table_lookup (0, &prefix);
174
175 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200176}
177
178/**
179 * Find first IP of the interface that would be used to egress towards dst.
180 * Returns 1 if the address is found 0 otherwise.
181 */
182int
183ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200184 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200185{
186 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200187 ip_lookup_main_t *lm;
188 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200189 u8 ipver;
190
Florin Corasa2157cf2016-08-16 21:09:14 +0200191 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200192
Florin Corasa2157cf2016-08-16 21:09:14 +0200193 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200194
Neale Rannsea93e482019-11-12 17:16:47 +0000195 lm = (ipver == AF_IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100196 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200197
Florin Corasa2157cf2016-08-16 21:09:14 +0200198 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200199 return 0;
200
201 /* find the first ip address */
202 addr = ip_interface_get_first_address (lm, si, ipver);
203 if (0 == addr)
204 return 0;
205
206 ip_address_set (result, addr, ipver);
207 return 1;
208}
209
210static int
Filip Tehlar0a8840d2017-10-16 05:48:23 -0700211dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add,
212 u8 with_default_route)
Florin Corasf727db92016-06-23 15:01:58 +0200213{
Neale Ranns5e575b12016-10-03 09:40:25 +0100214 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200215
Florin Coras1a1adc72016-07-22 01:45:30 +0200216 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200217 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200218 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200219
220 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200221 {
222 clib_warning ("vni %d not associated to a vrf!", vni);
223 return VNET_API_ERROR_INVALID_VALUE;
224 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200225 }
226 else
227 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200228 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200229 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200230 {
231 clib_warning ("vni %d not associated to a bridge domain!", vni);
232 return VNET_API_ERROR_INVALID_VALUE;
233 }
Florin Corasf727db92016-06-23 15:01:58 +0200234 }
235
Florin Corasf727db92016-06-23 15:01:58 +0200236 /* enable/disable data-plane interface */
237 if (is_add)
238 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100239 if (is_l2)
240 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
241 else
Filip Tehlar0a8840d2017-10-16 05:48:23 -0700242 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0],
243 with_default_route);
Florin Corasf727db92016-06-23 15:01:58 +0200244 }
245 else
246 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100247 if (is_l2)
248 lisp_gpe_tenant_l2_iface_unlock (vni);
249 else
250 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200251 }
252
253 return 0;
254}
255
256static void
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700257dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200258{
Florin Corasa2157cf2016-08-16 21:09:14 +0200259 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
260 fwd_entry_t *fe = 0;
261 uword *feip = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400262 clib_memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200263
Florin Corasa2157cf2016-08-16 21:09:14 +0200264 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200265 if (!feip)
266 return;
267
Florin Corasa2157cf2016-08-16 21:09:14 +0200268 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200269
270 /* delete dp fwd entry */
271 u32 sw_if_index;
272 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200273 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200274 a->vni = gid_address_vni (&fe->reid);
275 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200276 if (fe->is_src_dst)
277 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200278
Filip Tehlar21511912017-04-07 10:41:42 +0200279 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200280 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
281
282 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200283 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
284 vec_free (fe->locator_pairs);
285 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200286}
287
288/**
289 * Finds first remote locator with best (lowest) priority that has a local
290 * peer locator with an underlying route to it.
291 *
292 */
293static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200294get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
295 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200296{
Florin Coras3590ac52016-08-08 16:04:26 +0200297 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200298 locator_set_t *rmt_ls, *lcl_ls;
299 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
300 locator_t *lp, *rmt = 0;
301 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200302 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200303
Florin Corasa2157cf2016-08-16 21:09:14 +0200304 rmt_ls =
305 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
306 lcl_ls =
307 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200308
Florin Corasa2157cf2016-08-16 21:09:14 +0200309 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200310 return 0;
311
Florin Coras3590ac52016-08-08 16:04:26 +0200312 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200313 {
314 rmt = 0;
315
316 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200317 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
318 {
319 if (0 != hash_get (checked, i))
320 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200321
Florin Corasa2157cf2016-08-16 21:09:14 +0200322 li = vec_elt (rmt_ls->locator_indices, i);
323 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200324
Florin Corasa2157cf2016-08-16 21:09:14 +0200325 /* we don't support non-IP locators for now */
326 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
327 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200328
Florin Corasa2157cf2016-08-16 21:09:14 +0200329 if ((found && lp->priority == limitp)
330 || (!found && lp->priority >= limitp))
331 {
332 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200333
Florin Corasa2157cf2016-08-16 21:09:14 +0200334 /* don't search for locators with lower priority and don't
335 * check this locator again*/
336 limitp = lp->priority;
337 hash_set (checked, i, 1);
338 break;
339 }
340 }
Florin Corasf727db92016-06-23 15:01:58 +0200341 /* check if a local locator with a route to remote locator exists */
342 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200343 {
344 /* find egress sw_if_index for rmt locator */
345 esi =
346 ip_fib_get_egress_iface_for_dst (lcm,
347 &gid_address_ip (&rmt->address));
348 if ((u32) ~ 0 == esi)
349 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200350
Florin Corasa2157cf2016-08-16 21:09:14 +0200351 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
352 {
353 li = vec_elt (lcl_ls->locator_indices, i);
354 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200355
Florin Corasa2157cf2016-08-16 21:09:14 +0200356 /* found local locator with the needed sw_if_index */
357 if (sl->sw_if_index == esi)
358 {
359 /* and it has an address */
360 if (0 == ip_interface_get_first_ip_address (lcm,
361 sl->sw_if_index,
362 gid_address_ip_version
363 (&rmt->address),
364 lcl_addr))
365 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200366
Dave Barachb7b92992018-10-17 10:38:51 -0400367 clib_memset (&pair, 0, sizeof (pair));
Florin Corasa2157cf2016-08-16 21:09:14 +0200368 ip_address_copy (&pair.rmt_loc,
369 &gid_address_ip (&rmt->address));
370 ip_address_copy (&pair.lcl_loc, lcl_addr);
371 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100372 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200373 vec_add1 (locator_pairs[0], pair);
374 found = 1;
375 }
376 }
377 }
Florin Corasf727db92016-06-23 15:01:58 +0200378 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200379 break;
Florin Corasf727db92016-06-23 15:01:58 +0200380 }
Florin Coras3590ac52016-08-08 16:04:26 +0200381
Florin Corasa2157cf2016-08-16 21:09:14 +0200382 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200383 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200384}
385
386static void
Florin Corasdca88042016-09-14 16:01:38 +0200387gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
388 fid_address_t * fid)
389{
390 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
391
392 dst[0] = src[0];
393
394 switch (fid_addr_type (fid))
395 {
396 case FID_ADDR_IP_PREF:
397 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
398 gid_address_ippref (dst) = fid_addr_ippref (fid);
399 break;
400 case FID_ADDR_MAC:
401 gid_address_type (dst) = GID_ADDR_MAC;
402 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
403 break;
404 default:
405 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
406 break;
407 }
408}
409
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200410u8
411vnet_lisp_map_register_state_get (void)
412{
413 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
414 return lcm->map_registering;
415}
416
417u8
418vnet_lisp_rloc_probe_state_get (void)
419{
420 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
421 return lcm->rloc_probing;
422}
423
Florin Corasdca88042016-09-14 16:01:38 +0200424static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200425dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200426{
Florin Corasa2157cf2016-08-16 21:09:14 +0200427 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
Florin Corasba888e42017-01-24 11:38:18 -0800428 gid_address_t *rmt_eid, *lcl_eid;
429 mapping_t *lcl_map, *rmt_map;
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700430 u32 sw_if_index, **rmts, rmts_idx;
431 uword *feip = 0, *dpid, *rmts_stored_idxp = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +0200432 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200433 u8 type, is_src_dst = 0;
Florin Corasba888e42017-01-24 11:38:18 -0800434 int rv;
Florin Corasf727db92016-06-23 15:01:58 +0200435
Dave Barachb7b92992018-10-17 10:38:51 -0400436 clib_memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200437
438 /* remove entry if it already exists */
439 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
440 if (feip)
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700441 dp_del_fwd_entry (lcm, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200442
Florin Corasba888e42017-01-24 11:38:18 -0800443 /*
444 * Determine local mapping and eid
445 */
Filip Tehlar0a8840d2017-10-16 05:48:23 -0700446 if (lcm->flags & LISP_FLAG_PITR_MODE)
447 {
448 if (lcm->pitr_map_index != ~0)
449 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
450 else
451 {
452 clib_warning ("no PITR mapping configured!");
453 return;
454 }
455 }
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200456 else
Florin Corasba888e42017-01-24 11:38:18 -0800457 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
458 lcl_eid = &lcl_map->eid;
Florin Corasf727db92016-06-23 15:01:58 +0200459
Florin Corasba888e42017-01-24 11:38:18 -0800460 /*
461 * Determine remote mapping and eid
462 */
463 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
464 rmt_eid = &rmt_map->eid;
465
466 /*
467 * Build and insert data plane forwarding entry
468 */
Florin Corasf727db92016-06-23 15:01:58 +0200469 a->is_add = 1;
470
Filip Tehlard5fcc462016-10-17 16:20:18 +0200471 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200472 {
Florin Corasba888e42017-01-24 11:38:18 -0800473 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
Filip Tehlard5fcc462016-10-17 16:20:18 +0200474 {
Florin Corasba888e42017-01-24 11:38:18 -0800475 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
476 &gid_address_sd_dst (rmt_eid));
477 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
478 &gid_address_sd_src (rmt_eid));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200479 }
480 else
481 {
Florin Corasba888e42017-01-24 11:38:18 -0800482 gid_address_copy (&a->rmt_eid, rmt_eid);
483 gid_address_copy (&a->lcl_eid, lcl_eid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200484 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200485 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200486 }
487 else
Florin Corasba888e42017-01-24 11:38:18 -0800488 gid_address_copy (&a->rmt_eid, rmt_eid);
Florin Corasdca88042016-09-14 16:01:38 +0200489
Florin Corasa2157cf2016-08-16 21:09:14 +0200490 a->vni = gid_address_vni (&a->rmt_eid);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100491 a->is_src_dst = is_src_dst;
Florin Coras1a1adc72016-07-22 01:45:30 +0200492
493 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200494 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200495 if (GID_ADDR_IP_PREFIX == type)
496 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200497 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200498 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200499 {
500 clib_warning ("vni %d not associated to a vrf!", a->vni);
501 return;
502 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200503 a->table_id = dpid[0];
504 }
505 else if (GID_ADDR_MAC == type)
506 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200507 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200508 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200509 {
510 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
511 return;
512 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200513 a->bd_id = dpid[0];
514 }
515
Florin Corasf727db92016-06-23 15:01:58 +0200516 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Corasba888e42017-01-24 11:38:18 -0800517 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
518
519 /* Either rmt mapping is negative or we can't find underlay path.
520 * Try again with petr if configured */
521 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
Florin Corasf727db92016-06-23 15:01:58 +0200522 {
Florin Corasba888e42017-01-24 11:38:18 -0800523 rmt_map = lisp_get_petr_mapping (lcm);
524 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
Florin Corasf727db92016-06-23 15:01:58 +0200525 }
526
Florin Corasba888e42017-01-24 11:38:18 -0800527 /* negative entry */
528 if (rv == 0)
529 {
530 a->is_negative = 1;
531 a->action = rmt_map->action;
532 }
Florin Corasf727db92016-06-23 15:01:58 +0200533
Filip Tehlar21511912017-04-07 10:41:42 +0200534 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
535 if (rv)
536 {
537 if (a->locator_pairs)
538 vec_free (a->locator_pairs);
539 return;
540 }
Florin Corasf727db92016-06-23 15:01:58 +0200541
Filip Tehlar21511912017-04-07 10:41:42 +0200542 /* add tunnel to fwd entry table */
Florin Corasf727db92016-06-23 15:01:58 +0200543 pool_get (lcm->fwd_entry_pool, fe);
Filip Tehlar21511912017-04-07 10:41:42 +0200544 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
545
Florin Corasbb5c22f2016-08-02 02:31:03 +0200546 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200547 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100548
549 if (is_src_dst)
550 gid_address_copy (&fe->leid, &a->lcl_eid);
551 else
Florin Corasba888e42017-01-24 11:38:18 -0800552 gid_address_copy (&fe->leid, lcl_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100553
Filip Tehlar69a9b762016-09-23 10:00:52 +0200554 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200555 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200556 fe - lcm->fwd_entry_pool);
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700557
558 /* Add rmt mapping to the vector of adjacent mappings to lcl mapping */
559 rmts_stored_idxp =
560 hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, src_map_index);
561 if (!rmts_stored_idxp)
562 {
563 pool_get (lcm->lcl_to_rmt_adjacencies, rmts);
Dave Barachb7b92992018-10-17 10:38:51 -0400564 clib_memset (rmts, 0, sizeof (*rmts));
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700565 rmts_idx = rmts - lcm->lcl_to_rmt_adjacencies;
566 hash_set (lcm->lcl_to_rmt_adjs_by_lcl_idx, src_map_index, rmts_idx);
567 }
568 else
569 {
570 rmts_idx = (u32) (*rmts_stored_idxp);
571 rmts = pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idx);
572 }
573 vec_add1 (rmts[0], dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200574}
575
Filip Tehlarce1aae42017-01-04 10:42:25 +0100576typedef struct
577{
578 u32 si;
579 u32 di;
580} fwd_entry_mt_arg_t;
581
582static void *
583dp_add_fwd_entry_thread_fn (void *arg)
584{
585 fwd_entry_mt_arg_t *a = arg;
586 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
587 dp_add_fwd_entry (lcm, a->si, a->di);
588 return 0;
589}
590
591static int
592dp_add_fwd_entry_from_mt (u32 si, u32 di)
593{
594 fwd_entry_mt_arg_t a;
595
Dave Barachb7b92992018-10-17 10:38:51 -0400596 clib_memset (&a, 0, sizeof (a));
Filip Tehlarce1aae42017-01-04 10:42:25 +0100597 a.si = si;
598 a.di = di;
599
600 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
601 (u8 *) & a, sizeof (a));
602 return 0;
603}
604
Florin Corasf727db92016-06-23 15:01:58 +0200605/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200606 * Returns vector of adjacencies.
607 *
608 * The caller must free the vector returned by this function.
609 *
610 * @param vni virtual network identifier
611 * @return vector of adjacencies
612 */
613lisp_adjacency_t *
614vnet_lisp_adjacencies_get_by_vni (u32 vni)
615{
616 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
617 fwd_entry_t *fwd;
618 lisp_adjacency_t *adjs = 0, adj;
619
620 /* *INDENT-OFF* */
621 pool_foreach(fwd, lcm->fwd_entry_pool,
622 ({
623 if (gid_address_vni (&fwd->reid) != vni)
624 continue;
625
626 gid_address_copy (&adj.reid, &fwd->reid);
627 gid_address_copy (&adj.leid, &fwd->leid);
628 vec_add1 (adjs, adj);
629 }));
630 /* *INDENT-ON* */
631
632 return adjs;
633}
634
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200635static lisp_msmr_t *
636get_map_server (ip_address_t * a)
637{
638 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
639 lisp_msmr_t *m;
640
641 vec_foreach (m, lcm->map_servers)
642 {
643 if (!ip_address_cmp (&m->address, a))
644 {
645 return m;
646 }
647 }
648 return 0;
649}
650
651static lisp_msmr_t *
652get_map_resolver (ip_address_t * a)
653{
654 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
655 lisp_msmr_t *m;
656
657 vec_foreach (m, lcm->map_resolvers)
658 {
659 if (!ip_address_cmp (&m->address, a))
660 {
661 return m;
662 }
663 }
664 return 0;
665}
666
667int
668vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
669{
670 u32 i;
671 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
672 lisp_msmr_t _ms, *ms = &_ms;
673
674 if (vnet_lisp_enable_disable_status () == 0)
675 {
676 clib_warning ("LISP is disabled!");
677 return VNET_API_ERROR_LISP_DISABLED;
678 }
679
680 if (is_add)
681 {
682 if (get_map_server (addr))
683 {
684 clib_warning ("map-server %U already exists!", format_ip_address,
685 addr);
686 return -1;
687 }
688
Dave Barachb7b92992018-10-17 10:38:51 -0400689 clib_memset (ms, 0, sizeof (*ms));
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200690 ip_address_copy (&ms->address, addr);
691 vec_add1 (lcm->map_servers, ms[0]);
Filip Tehlar7048ff12017-07-27 08:09:14 +0200692
693 if (vec_len (lcm->map_servers) == 1)
694 lcm->do_map_server_election = 1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200695 }
696 else
697 {
698 for (i = 0; i < vec_len (lcm->map_servers); i++)
699 {
700 ms = vec_elt_at_index (lcm->map_servers, i);
701 if (!ip_address_cmp (&ms->address, addr))
702 {
Filip Tehlar7048ff12017-07-27 08:09:14 +0200703 if (!ip_address_cmp (&ms->address, &lcm->active_map_server))
704 lcm->do_map_server_election = 1;
705
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200706 vec_del1 (lcm->map_servers, i);
707 break;
708 }
709 }
710 }
711
712 return 0;
713}
714
Filip Tehlar69a9b762016-09-23 10:00:52 +0200715/**
Florin Corasf727db92016-06-23 15:01:58 +0200716 * Add/remove mapping to/from map-cache. Overwriting not allowed.
717 */
718int
719vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200720 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100721{
Florin Corasa2157cf2016-08-16 21:09:14 +0200722 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
723 u32 mi, *map_indexp, map_index, i;
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700724 u32 **rmts = 0, *remote_idxp, rmts_itr, remote_idx;
725 uword *rmts_idxp;
Florin Corasa2157cf2016-08-16 21:09:14 +0200726 mapping_t *m, *old_map;
727 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100728
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200729 if (gid_address_type (&a->eid) == GID_ADDR_NSH)
730 {
731 if (gid_address_vni (&a->eid) != 0)
732 {
733 clib_warning ("Supported only default VNI for NSH!");
734 return VNET_API_ERROR_INVALID_ARGUMENT;
735 }
736 if (gid_address_nsh_spi (&a->eid) > MAX_VALUE_U24)
737 {
738 clib_warning ("SPI is greater than 24bit!");
739 return VNET_API_ERROR_INVALID_ARGUMENT;
740 }
741 }
742
Florin Corasf727db92016-06-23 15:01:58 +0200743 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200744 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100745 if (a->is_add)
746 {
747 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200748 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
749 {
750 clib_warning ("eid %U found in the eid-table", format_gid_address,
751 &a->eid);
752 return VNET_API_ERROR_VALUE_EXIST;
753 }
Florin Corase127a7e2016-02-18 22:20:01 +0100754
Florin Corasa2157cf2016-08-16 21:09:14 +0200755 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200756 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100757 m->locator_set_index = a->locator_set_index;
758 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200759 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100760 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200761 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200762 m->key = vec_dup (a->key);
763 m->key_id = a->key_id;
Florin Corase51a7a02019-01-21 17:36:28 -0800764 m->authoritative = a->authoritative;
Florin Corase127a7e2016-02-18 22:20:01 +0100765
766 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200767 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200768 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100769
Florin Corasa2157cf2016-08-16 21:09:14 +0200770 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
771 {
772 clib_warning ("Locator set with index %d doesn't exist",
773 a->locator_set_index);
774 return VNET_API_ERROR_INVALID_VALUE;
775 }
Florin Corase127a7e2016-02-18 22:20:01 +0100776
777 /* add eid to list of eids supported by locator-set */
778 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200779 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
780 a->locator_set_index);
781 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100782
783 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200784 {
785 /* mark as local */
786 vec_add1 (lcm->local_mappings_indexes, map_index);
787 }
Florin Corase127a7e2016-02-18 22:20:01 +0100788 map_index_result[0] = map_index;
789 }
790 else
791 {
Florin Coras577c3552016-04-21 00:45:40 +0200792 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200793 {
794 clib_warning ("eid %U not found in the eid-table",
795 format_gid_address, &a->eid);
796 return VNET_API_ERROR_INVALID_VALUE;
797 }
Florin Corase127a7e2016-02-18 22:20:01 +0100798
799 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200800 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
801 a->locator_set_index);
802 for (i = 0; i < vec_len (eid_indexes[0]); i++)
803 {
804 map_indexp = vec_elt_at_index (eid_indexes[0], i);
805 if (map_indexp[0] == mi)
806 break;
807 }
808 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100809
810 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200811 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100812 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200813 {
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700814 /* Remove adjacencies associated with the local mapping */
815 rmts_idxp = hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, mi);
816 if (rmts_idxp)
817 {
818 rmts =
819 pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idxp[0]);
820 vec_foreach (remote_idxp, rmts[0])
821 {
822 dp_del_fwd_entry (lcm, remote_idxp[0]);
823 }
824 vec_free (rmts[0]);
825 pool_put (lcm->lcl_to_rmt_adjacencies, rmts);
826 hash_unset (lcm->lcl_to_rmt_adjs_by_lcl_idx, mi);
827 }
828
Florin Corasa2157cf2016-08-16 21:09:14 +0200829 u32 k, *lm_indexp;
830 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
831 {
832 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
833 if (lm_indexp[0] == mi)
834 break;
835 }
836 vec_del1 (lcm->local_mappings_indexes, k);
837 }
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -0700838 else
839 {
840 /* Remove remote (if present) from the vectors of lcl-to-rmts
841 * TODO: Address this in a more efficient way.
842 */
843 /* *INDENT-OFF* */
844 pool_foreach (rmts, lcm->lcl_to_rmt_adjacencies,
845 ({
846 vec_foreach_index (rmts_itr, rmts[0])
847 {
848 remote_idx = vec_elt (rmts[0], rmts_itr);
849 if (mi == remote_idx)
850 {
851 vec_del1 (rmts[0], rmts_itr);
852 break;
853 }
854 }
855 }));
856 /* *INDENT-ON* */
857 }
Florin Corase127a7e2016-02-18 22:20:01 +0100858
859 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200860 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200861 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100862 pool_put_index (lcm->mapping_pool, mi);
863 }
864
865 return 0;
866}
867
Florin Corasf727db92016-06-23 15:01:58 +0200868/**
869 * Add/update/delete mapping to/in/from map-cache.
870 */
Florin Coras577c3552016-04-21 00:45:40 +0200871int
872vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200873 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200874{
Florin Corasa2157cf2016-08-16 21:09:14 +0200875 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200876 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200877 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200878
Florin Corasa2157cf2016-08-16 21:09:14 +0200879 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200880
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200881 if (vnet_lisp_enable_disable_status () == 0)
882 {
883 clib_warning ("LISP is disabled!");
884 return VNET_API_ERROR_LISP_DISABLED;
885 }
886
Florin Corasa2157cf2016-08-16 21:09:14 +0200887 vni = gid_address_vni (&a->eid);
888 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200889 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200890 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200891 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200892 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200893
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200894 if (!dp_table && GID_ADDR_NSH != type)
Florin Coras577c3552016-04-21 00:45:40 +0200895 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200896 clib_warning ("vni %d not associated to a %s!", vni,
897 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200898 return VNET_API_ERROR_INVALID_VALUE;
899 }
900
Florin Corasf727db92016-06-23 15:01:58 +0200901 /* store/remove mapping from map-cache */
902 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200903}
904
Neale Rannsf50bac12019-12-06 05:53:17 +0000905static int
Filip Tehlard5a65db2017-05-17 17:21:10 +0200906add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
907{
908 u32 **ht = arg;
Filip Tehlar05879992017-09-05 15:46:09 +0200909 u32 version = (u32) kvp->key[0];
Neale Rannsea93e482019-11-12 17:16:47 +0000910 if (AF_IP6 == version)
Neale Rannsf50bac12019-12-06 05:53:17 +0000911 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +0200912
913 u32 bd = (u32) (kvp->key[0] >> 32);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200914 hash_set (ht[0], bd, 0);
Neale Rannsf50bac12019-12-06 05:53:17 +0000915 return (BIHASH_WALK_CONTINUE);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200916}
917
918u32 *
919vnet_lisp_l2_arp_bds_get (void)
920{
921 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
922 u32 *bds = 0;
923
Filip Tehlar05879992017-09-05 15:46:09 +0200924 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
925 add_l2_arp_bd, &bds);
926 return bds;
927}
928
Neale Rannsf50bac12019-12-06 05:53:17 +0000929static int
Filip Tehlar05879992017-09-05 15:46:09 +0200930add_ndp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
931{
932 u32 **ht = arg;
933 u32 version = (u32) kvp->key[0];
Neale Rannsea93e482019-11-12 17:16:47 +0000934 if (AF_IP4 == version)
Neale Rannsf50bac12019-12-06 05:53:17 +0000935 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +0200936
937 u32 bd = (u32) (kvp->key[0] >> 32);
938 hash_set (ht[0], bd, 0);
Neale Rannsf50bac12019-12-06 05:53:17 +0000939 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +0200940}
941
942u32 *
943vnet_lisp_ndp_bds_get (void)
944{
945 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
946 u32 *bds = 0;
947
948 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
949 add_ndp_bd, &bds);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200950 return bds;
951}
952
953typedef struct
954{
955 void *vector;
956 u32 bd;
Filip Tehlar05879992017-09-05 15:46:09 +0200957} lisp_add_l2_arp_ndp_args_t;
Filip Tehlard5a65db2017-05-17 17:21:10 +0200958
Neale Rannsf50bac12019-12-06 05:53:17 +0000959static int
Filip Tehlard5a65db2017-05-17 17:21:10 +0200960add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
961{
Filip Tehlar05879992017-09-05 15:46:09 +0200962 lisp_add_l2_arp_ndp_args_t *a = arg;
Filip Tehlard5a65db2017-05-17 17:21:10 +0200963 lisp_api_l2_arp_entry_t **vector = a->vector, e;
964
Filip Tehlar05879992017-09-05 15:46:09 +0200965 u32 version = (u32) kvp->key[0];
Neale Rannsea93e482019-11-12 17:16:47 +0000966 if (AF_IP6 == version)
Neale Rannsf50bac12019-12-06 05:53:17 +0000967 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +0200968
969 u32 bd = (u32) (kvp->key[0] >> 32);
970
971 if (bd == a->bd)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200972 {
973 mac_copy (e.mac, (void *) &kvp->value);
974 e.ip4 = (u32) kvp->key[1];
975 vec_add1 (vector[0], e);
976 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000977 return (BIHASH_WALK_CONTINUE);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200978}
979
980lisp_api_l2_arp_entry_t *
981vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
982{
983 lisp_api_l2_arp_entry_t *entries = 0;
984 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar05879992017-09-05 15:46:09 +0200985 lisp_add_l2_arp_ndp_args_t a;
Filip Tehlard5a65db2017-05-17 17:21:10 +0200986
987 a.vector = &entries;
988 a.bd = bd;
989
Filip Tehlar05879992017-09-05 15:46:09 +0200990 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
991 add_l2_arp_entry, &a);
992 return entries;
993}
994
Neale Rannsf50bac12019-12-06 05:53:17 +0000995static int
Filip Tehlar05879992017-09-05 15:46:09 +0200996add_ndp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
997{
998 lisp_add_l2_arp_ndp_args_t *a = arg;
999 lisp_api_ndp_entry_t **vector = a->vector, e;
1000
1001 u32 version = (u32) kvp->key[0];
Neale Rannsea93e482019-11-12 17:16:47 +00001002 if (AF_IP4 == version)
Neale Rannsf50bac12019-12-06 05:53:17 +00001003 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +02001004
1005 u32 bd = (u32) (kvp->key[0] >> 32);
1006
1007 if (bd == a->bd)
1008 {
1009 mac_copy (e.mac, (void *) &kvp->value);
1010 clib_memcpy (e.ip6, &kvp->key[1], 16);
1011 vec_add1 (vector[0], e);
1012 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001013 return (BIHASH_WALK_CONTINUE);
Filip Tehlar05879992017-09-05 15:46:09 +02001014}
1015
1016lisp_api_ndp_entry_t *
1017vnet_lisp_ndp_entries_get_by_bd (u32 bd)
1018{
1019 lisp_api_ndp_entry_t *entries = 0;
1020 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1021 lisp_add_l2_arp_ndp_args_t a;
1022
1023 a.vector = &entries;
1024 a.bd = bd;
1025
1026 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
1027 add_ndp_entry, &a);
Filip Tehlard5a65db2017-05-17 17:21:10 +02001028 return entries;
1029}
1030
1031int
Filip Tehlar64929642017-09-20 08:41:23 +02001032vnet_lisp_add_del_l2_arp_ndp_entry (gid_address_t * key, u8 * mac, u8 is_add)
Filip Tehlard5a65db2017-05-17 17:21:10 +02001033{
1034 if (vnet_lisp_enable_disable_status () == 0)
1035 {
1036 clib_warning ("LISP is disabled!");
1037 return VNET_API_ERROR_LISP_DISABLED;
1038 }
1039
1040 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1041 int rc = 0;
1042
1043 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
1044 if (is_add)
1045 {
1046 if (res != GID_LOOKUP_MISS_L2)
1047 {
1048 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
1049 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1050 }
1051 u64 val = mac_to_u64 (mac);
1052 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
1053 1 /* is_add */ );
1054 }
1055 else
1056 {
1057 if (res == GID_LOOKUP_MISS_L2)
1058 {
Filip Tehlar64929642017-09-20 08:41:23 +02001059 clib_warning ("ONE entry %U not found - cannot delete!",
Filip Tehlard5a65db2017-05-17 17:21:10 +02001060 format_gid_address, key);
1061 return -1;
1062 }
1063 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
1064 0 /* is_add */ );
1065 }
1066
1067 return rc;
1068}
1069
Filip Tehlar324112f2016-06-02 16:07:38 +02001070int
Florin Coras1a1adc72016-07-22 01:45:30 +02001071vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +02001072{
Florin Corasa2157cf2016-08-16 21:09:14 +02001073 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1074 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +02001075
1076 if (vnet_lisp_enable_disable_status () == 0)
1077 {
1078 clib_warning ("LISP is disabled!");
Filip Tehlard5a65db2017-05-17 17:21:10 +02001079 return VNET_API_ERROR_LISP_DISABLED;
Filip Tehlar324112f2016-06-02 16:07:38 +02001080 }
1081
Florin Coras1a1adc72016-07-22 01:45:30 +02001082 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1083 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1084
1085 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +02001086 {
1087 clib_warning ("can't add/del default vni-vrf mapping!");
1088 return -1;
1089 }
1090
Florin Coras1a1adc72016-07-22 01:45:30 +02001091 dp_idp = hash_get (dp_table_by_vni[0], vni);
1092 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +02001093
1094 if (is_add)
1095 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001096 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001097 {
1098 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1099 "mapping!", vni, dp_id);
1100 return -1;
1101 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001102 hash_set (dp_table_by_vni[0], vni, dp_id);
1103 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +02001104
1105 /* create dp iface */
Filip Tehlar0a8840d2017-10-16 05:48:23 -07001106 dp_add_del_iface (lcm, vni, is_l2, 1 /* is_add */ ,
1107 1 /* with_default_route */ );
Filip Tehlar324112f2016-06-02 16:07:38 +02001108 }
1109 else
1110 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001111 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001112 {
1113 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1114 "mapping!", vni, dp_id);
1115 return -1;
1116 }
Florin Corasf727db92016-06-23 15:01:58 +02001117 /* remove dp iface */
Filip Tehlar0a8840d2017-10-16 05:48:23 -07001118 dp_add_del_iface (lcm, vni, is_l2, 0 /* is_add */ , 0 /* unused */ );
Filip Tehlarfc568412017-04-05 10:06:03 +02001119
1120 hash_unset (dp_table_by_vni[0], vni);
1121 hash_unset (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +02001122 }
1123 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02001124
Filip Tehlar324112f2016-06-02 16:07:38 +02001125}
1126
Florin Corasf727db92016-06-23 15:01:58 +02001127/* return 0 if the two locator sets are identical 1 otherwise */
1128static u8
Florin Corasa2157cf2016-08-16 21:09:14 +02001129compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1130 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001131{
Florin Corasf727db92016-06-23 15:01:58 +02001132 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +02001133 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001134
Florin Corasa2157cf2016-08-16 21:09:14 +02001135 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +02001136 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001137
Florin Corasa2157cf2016-08-16 21:09:14 +02001138 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001139 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001140 old_li = vec_elt (old_ls_indexes, i);
1141 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +02001142
Florin Corasa2157cf2016-08-16 21:09:14 +02001143 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +02001144
1145 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +02001146 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001147 }
Florin Corasf727db92016-06-23 15:01:58 +02001148 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001149}
1150
Filip Tehlard5fcc462016-10-17 16:20:18 +02001151typedef struct
1152{
1153 u8 is_negative;
1154 void *lcm;
1155 gid_address_t *eids_to_be_deleted;
1156} remove_mapping_args_t;
1157
1158/**
1159 * Callback invoked when a sub-prefix is found
1160 */
1161static void
1162remove_mapping_if_needed (u32 mi, void *arg)
1163{
1164 u8 delete = 0;
1165 remove_mapping_args_t *a = arg;
1166 lisp_cp_main_t *lcm = a->lcm;
1167 mapping_t *m;
1168 locator_set_t *ls;
1169
1170 m = pool_elt_at_index (lcm->mapping_pool, mi);
1171 if (!m)
1172 return;
1173
1174 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1175
1176 if (a->is_negative)
1177 {
1178 if (0 != vec_len (ls->locator_indices))
1179 delete = 1;
1180 }
1181 else
1182 {
1183 if (0 == vec_len (ls->locator_indices))
1184 delete = 1;
1185 }
1186
1187 if (delete)
1188 vec_add1 (a->eids_to_be_deleted, m->eid);
1189}
1190
1191/**
1192 * This function searches map cache and looks for IP prefixes that are subset
1193 * of the provided one. If such prefix is found depending on 'is_negative'
1194 * it does follows:
1195 *
1196 * 1) if is_negative is true and found prefix points to positive mapping,
1197 * then the mapping is removed
1198 * 2) if is_negative is false and found prefix points to negative mapping,
1199 * then the mapping is removed
1200 */
1201static void
1202remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1203 u8 is_negative)
1204{
1205 gid_address_t *e;
1206 remove_mapping_args_t a;
Florin Corasf3dc11a2017-01-24 03:13:43 -08001207
Dave Barachb7b92992018-10-17 10:38:51 -04001208 clib_memset (&a, 0, sizeof (a));
Filip Tehlard5fcc462016-10-17 16:20:18 +02001209
1210 /* do this only in src/dst mode ... */
1211 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1212 return;
1213
1214 /* ... and only for IP prefix */
1215 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1216 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1217 return;
1218
1219 a.is_negative = is_negative;
1220 a.lcm = lcm;
1221
1222 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1223 remove_mapping_if_needed, &a);
1224
1225 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001226 {
Florin Corasf3dc11a2017-01-24 03:13:43 -08001227 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1228
Dave Barachb7b92992018-10-17 10:38:51 -04001229 clib_memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08001230 gid_address_copy (&adj_args->reid, e);
1231 adj_args->is_add = 0;
1232 if (vnet_lisp_add_del_adjacency (adj_args))
1233 clib_warning ("failed to del adjacency!");
1234
Filip Tehlar809bc742017-08-14 19:15:36 +02001235 vnet_lisp_del_mapping (e, NULL);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001236 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001237
1238 vec_free (a.eids_to_be_deleted);
1239}
1240
Filip Tehlar9677a942016-11-28 10:23:31 +01001241static void
1242mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1243{
1244 timing_wheel_delete (&lcm->wheel, mi);
1245}
1246
Filip Tehlara6bce492017-02-03 10:17:49 +01001247static int
1248is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1249{
1250 fib_node_index_t fei;
1251 fib_prefix_t prefix;
1252 fib_entry_flag_t flags;
1253
1254 ip_address_to_fib_prefix (addr, &prefix);
1255
1256 fei = fib_table_lookup (0, &prefix);
1257 flags = fib_entry_get_flags (fei);
1258 return (FIB_ENTRY_FLAG_LOCAL & flags);
1259}
1260
Filip Tehlar195bcee2016-05-13 17:37:35 +02001261/**
Filip Tehlar809bc742017-08-14 19:15:36 +02001262 * Adds/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001263 *
Filip Tehlar809bc742017-08-14 19:15:36 +02001264 * @param a parameters of the new mapping
Filip Tehlar195bcee2016-05-13 17:37:35 +02001265 * @param rlocs vector of remote locators
Filip Tehlar809bc742017-08-14 19:15:36 +02001266 * @param res_map_index index of the newly created mapping
1267 * @param locators_changed indicator if locators were updated in the mapping
Filip Tehlar195bcee2016-05-13 17:37:35 +02001268 * @return return code
1269 */
1270int
Filip Tehlar809bc742017-08-14 19:15:36 +02001271vnet_lisp_add_mapping (vnet_lisp_add_del_mapping_args_t * a,
1272 locator_t * rlocs,
1273 u32 * res_map_index, u8 * is_updated)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001274{
Florin Corasa2157cf2016-08-16 21:09:14 +02001275 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1276 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001277 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001278 mapping_t *old_map;
Filip Tehlara6bce492017-02-03 10:17:49 +01001279 locator_t *loc;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001280
Florin Corasa2157cf2016-08-16 21:09:14 +02001281 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001282 {
1283 clib_warning ("LISP is disabled!");
1284 return VNET_API_ERROR_LISP_DISABLED;
1285 }
1286
Florin Corasf727db92016-06-23 15:01:58 +02001287 if (res_map_index)
1288 res_map_index[0] = ~0;
Filip Tehlar809bc742017-08-14 19:15:36 +02001289 if (is_updated)
1290 is_updated[0] = 0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001291
Dave Barachb7b92992018-10-17 10:38:51 -04001292 clib_memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001293
Florin Corasf727db92016-06-23 15:01:58 +02001294 ls_args->locators = rlocs;
Filip Tehlar809bc742017-08-14 19:15:36 +02001295 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
1296 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1297
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001298 /* check if none of the locators match locally configured address */
Filip Tehlar809bc742017-08-14 19:15:36 +02001299 vec_foreach (loc, rlocs)
1300 {
1301 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1302 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1303 {
1304 clib_warning ("RLOC %U matches a local address!",
1305 format_gid_address, &loc->address);
1306 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1307 }
1308 }
1309
1310 /* overwrite: if mapping already exists, decide if locators should be
1311 * updated and be done */
1312 if (old_map && gid_address_cmp (&old_map->eid, &a->eid) == 0)
1313 {
1314 if (!a->is_static && (old_map->is_static || old_map->local))
1315 {
1316 /* do not overwrite local or static remote mappings */
1317 clib_warning ("mapping %U rejected due to collision with local "
1318 "or static remote mapping!", format_gid_address,
1319 &a->eid);
1320 return 0;
1321 }
1322
1323 locator_set_t *old_ls;
1324
1325 /* update mapping attributes */
1326 old_map->action = a->action;
1327 if (old_map->action != a->action && NULL != is_updated)
1328 is_updated[0] = 1;
1329
1330 old_map->authoritative = a->authoritative;
1331 old_map->ttl = a->ttl;
1332
1333 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1334 old_map->locator_set_index);
1335 if (compare_locators (lcm, old_ls->locator_indices, ls_args->locators))
1336 {
1337 /* set locator-set index to overwrite */
1338 ls_args->is_add = 1;
1339 ls_args->index = old_map->locator_set_index;
1340 vnet_lisp_add_del_locator_set (ls_args, 0);
1341 if (is_updated)
1342 is_updated[0] = 1;
1343 }
1344 if (res_map_index)
1345 res_map_index[0] = mi;
1346 }
1347 /* new mapping */
1348 else
1349 {
Filip Tehlar8d7a0b92017-10-18 07:10:25 -07001350 if (is_updated)
1351 is_updated[0] = 1;
Filip Tehlar809bc742017-08-14 19:15:36 +02001352 remove_overlapping_sub_prefixes (lcm, &a->eid, 0 == ls_args->locators);
1353
1354 ls_args->is_add = 1;
1355 ls_args->index = ~0;
1356
1357 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1358
1359 /* add mapping */
1360 a->is_add = 1;
1361 a->locator_set_index = ls_index;
1362 vnet_lisp_map_cache_add_del (a, &dst_map_index);
1363
1364 if (res_map_index)
1365 res_map_index[0] = dst_map_index;
1366 }
1367
1368 /* success */
1369 return 0;
1370}
1371
1372/**
1373 * Removes a mapping. Does not program forwarding.
1374 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001375 * @param eid end-host identifier
Filip Tehlar809bc742017-08-14 19:15:36 +02001376 * @param res_map_index index of the removed mapping
1377 * @return return code
1378 */
1379int
1380vnet_lisp_del_mapping (gid_address_t * eid, u32 * res_map_index)
1381{
1382 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1383 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1384 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1385 mapping_t *old_map;
1386 u32 mi;
1387
Dave Barachb7b92992018-10-17 10:38:51 -04001388 clib_memset (ls_args, 0, sizeof (ls_args[0]));
1389 clib_memset (m_args, 0, sizeof (m_args[0]));
Filip Tehlar809bc742017-08-14 19:15:36 +02001390 if (res_map_index)
1391 res_map_index[0] = ~0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001392
Florin Coras71893ac2016-07-10 20:09:32 +02001393 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001394 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001395
Filip Tehlar809bc742017-08-14 19:15:36 +02001396 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001397 {
Filip Tehlar809bc742017-08-14 19:15:36 +02001398 clib_warning ("cannot delete mapping for eid %U",
1399 format_gid_address, eid);
1400 return -1;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001401 }
Florin Corasf727db92016-06-23 15:01:58 +02001402
Filip Tehlar809bc742017-08-14 19:15:36 +02001403 m_args->is_add = 0;
1404 gid_address_copy (&m_args->eid, eid);
1405 m_args->locator_set_index = old_map->locator_set_index;
Florin Corasf727db92016-06-23 15:01:58 +02001406
Filip Tehlar809bc742017-08-14 19:15:36 +02001407 /* delete mapping associated from map-cache */
1408 vnet_lisp_map_cache_add_del (m_args, 0);
Florin Corasf727db92016-06-23 15:01:58 +02001409
Filip Tehlar809bc742017-08-14 19:15:36 +02001410 ls_args->is_add = 0;
1411 ls_args->index = old_map->locator_set_index;
Filip Tehlarb93222f2016-07-07 09:58:08 +02001412
Filip Tehlar809bc742017-08-14 19:15:36 +02001413 /* delete locator set */
1414 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlar9677a942016-11-28 10:23:31 +01001415
Filip Tehlar809bc742017-08-14 19:15:36 +02001416 /* delete timer associated to the mapping if any */
1417 if (old_map->timer_set)
1418 mapping_delete_timer (lcm, mi);
1419
1420 /* return old mapping index */
1421 if (res_map_index)
1422 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001423
Filip Tehlar195bcee2016-05-13 17:37:35 +02001424 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001425 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001426}
1427
Filip Tehlar58f886a2016-05-30 15:57:40 +02001428int
Florin Corasf727db92016-06-23 15:01:58 +02001429vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001430{
1431 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001432 u32 mi, *map_indices = 0, *map_indexp;
1433 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1434 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1435 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001436
Florin Corasa2157cf2016-08-16 21:09:14 +02001437 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001438 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001439 ({
1440 vec_add1 (map_indices, mi);
1441 }));
1442 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001443
1444 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001445 {
1446 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1447 if (!map->local)
1448 {
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -07001449 dp_del_fwd_entry (lcm, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001450
Florin Corasa2157cf2016-08-16 21:09:14 +02001451 dm_args->is_add = 0;
1452 gid_address_copy (&dm_args->eid, &map->eid);
1453 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001454
Florin Corasa2157cf2016-08-16 21:09:14 +02001455 /* delete mapping associated to fwd entry */
1456 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001457
Florin Corasa2157cf2016-08-16 21:09:14 +02001458 ls->is_add = 0;
1459 ls->local = 0;
1460 ls->index = map->locator_set_index;
1461 /* delete locator set */
1462 rv = vnet_lisp_add_del_locator_set (ls, 0);
1463 if (rv != 0)
1464 goto cleanup;
1465 }
1466 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001467
1468cleanup:
1469 if (map_indices)
1470 vec_free (map_indices);
1471 return rv;
1472}
1473
Filip Tehlar195bcee2016-05-13 17:37:35 +02001474/**
Florin Coras71893ac2016-07-10 20:09:32 +02001475 * Adds adjacency or removes forwarding entry associated to remote mapping.
1476 * Note that adjacencies are not stored, they only result in forwarding entries
1477 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001478 */
Florin Corasf3dc11a2017-01-24 03:13:43 -08001479int
1480vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
Florin Corasf727db92016-06-23 15:01:58 +02001481{
Florin Corasf3dc11a2017-01-24 03:13:43 -08001482 lisp_cp_main_t *lcm = &lisp_control_main;
Florin Coras71893ac2016-07-10 20:09:32 +02001483 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001484
1485 if (vnet_lisp_enable_disable_status () == 0)
1486 {
1487 clib_warning ("LISP is disabled!");
1488 return VNET_API_ERROR_LISP_DISABLED;
1489 }
1490
Filip Tehlar69a9b762016-09-23 10:00:52 +02001491 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
Florin Corasf3dc11a2017-01-24 03:13:43 -08001492 &a->reid, &a->leid);
Florin Coras71893ac2016-07-10 20:09:32 +02001493 if (GID_LOOKUP_MISS == remote_mi)
1494 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001495 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001496 format_gid_address, &a->reid);
Florin Corasf727db92016-06-23 15:01:58 +02001497
Florin Coras71893ac2016-07-10 20:09:32 +02001498 return -1;
1499 }
1500
Florin Corasf3dc11a2017-01-24 03:13:43 -08001501 if (a->is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001502 {
Florin Corasf727db92016-06-23 15:01:58 +02001503 /* check if source eid has an associated mapping. If pitr mode is on,
1504 * just use the pitr's mapping */
Filip Tehlar0a8840d2017-10-16 05:48:23 -07001505 if (lcm->flags & LISP_FLAG_PITR_MODE)
1506 {
1507 if (lcm->pitr_map_index != ~0)
1508 {
1509 local_mi = lcm->pitr_map_index;
1510 }
1511 else
1512 {
1513 /* PITR mode is on, but no mapping is configured */
1514 return -1;
1515 }
1516 }
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001517 else
1518 {
1519 if (gid_address_type (&a->reid) == GID_ADDR_NSH)
1520 {
1521 if (lcm->nsh_map_index == ~0)
1522 local_mi = GID_LOOKUP_MISS;
1523 else
1524 local_mi = lcm->nsh_map_index;
1525 }
1526 else
1527 {
1528 local_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1529 &a->leid);
1530 }
1531 }
Florin Corasf727db92016-06-23 15:01:58 +02001532
Florin Coras71893ac2016-07-10 20:09:32 +02001533 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001534 {
1535 clib_warning ("Local eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001536 format_gid_address, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001537
Florin Corasa2157cf2016-08-16 21:09:14 +02001538 return -1;
1539 }
Florin Corasf727db92016-06-23 15:01:58 +02001540
Florin Coras71893ac2016-07-10 20:09:32 +02001541 /* update forwarding */
1542 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001543 }
1544 else
Alberto Rodriguez-Natal8d66f9d2017-09-09 14:15:15 -07001545 dp_del_fwd_entry (lcm, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001546
1547 return 0;
1548}
1549
1550int
Florin Corasdca88042016-09-14 16:01:38 +02001551vnet_lisp_set_map_request_mode (u8 mode)
1552{
1553 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1554
1555 if (vnet_lisp_enable_disable_status () == 0)
1556 {
1557 clib_warning ("LISP is disabled!");
1558 return VNET_API_ERROR_LISP_DISABLED;
1559 }
1560
1561 if (mode >= _MR_MODE_MAX)
1562 {
1563 clib_warning ("Invalid LISP map request mode %d!", mode);
1564 return VNET_API_ERROR_INVALID_ARGUMENT;
1565 }
1566
1567 lcm->map_request_mode = mode;
1568 return 0;
1569}
1570
Filip Tehlar53f09e32016-05-19 14:25:44 +02001571int
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001572vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add)
1573{
1574 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1575 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1576 u32 locator_set_index = ~0;
1577 mapping_t *m;
1578 uword *p;
1579
1580 if (vnet_lisp_enable_disable_status () == 0)
1581 {
1582 clib_warning ("LISP is disabled!");
1583 return VNET_API_ERROR_LISP_DISABLED;
1584 }
1585
1586 if (is_add)
1587 {
1588 if (lcm->nsh_map_index == (u32) ~ 0)
1589 {
1590 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1591 if (!p)
1592 {
1593 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1594 return -1;
1595 }
1596 locator_set_index = p[0];
1597
1598 pool_get (lcm->mapping_pool, m);
Dave Barachb7b92992018-10-17 10:38:51 -04001599 clib_memset (m, 0, sizeof *m);
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001600 m->locator_set_index = locator_set_index;
1601 m->local = 1;
1602 m->nsh_set = 1;
1603 lcm->nsh_map_index = m - lcm->mapping_pool;
1604
1605 if (~0 == vnet_lisp_gpe_add_nsh_iface (lgm))
1606 return -1;
1607 }
1608 }
1609 else
1610 {
1611 if (lcm->nsh_map_index != (u32) ~ 0)
1612 {
1613 /* remove NSH mapping */
1614 pool_put_index (lcm->mapping_pool, lcm->nsh_map_index);
1615 lcm->nsh_map_index = ~0;
1616 vnet_lisp_gpe_del_nsh_iface (lgm);
1617 }
1618 }
1619 return 0;
1620}
1621
1622int
Filip Tehlar53f09e32016-05-19 14:25:44 +02001623vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1624{
Florin Corasa2157cf2016-08-16 21:09:14 +02001625 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001626 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001627 mapping_t *m;
1628 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001629
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001630 if (vnet_lisp_enable_disable_status () == 0)
1631 {
1632 clib_warning ("LISP is disabled!");
1633 return VNET_API_ERROR_LISP_DISABLED;
1634 }
1635
Filip Tehlar53f09e32016-05-19 14:25:44 +02001636 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1637 if (!p)
1638 {
1639 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1640 return -1;
1641 }
1642 locator_set_index = p[0];
1643
1644 if (is_add)
1645 {
1646 pool_get (lcm->mapping_pool, m);
1647 m->locator_set_index = locator_set_index;
1648 m->local = 1;
Filip Tehlar38206ee2017-02-20 17:31:57 +01001649 m->pitr_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001650 lcm->pitr_map_index = m - lcm->mapping_pool;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001651 }
1652 else
1653 {
1654 /* remove pitr mapping */
1655 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
Filip Tehlar0a8840d2017-10-16 05:48:23 -07001656 lcm->pitr_map_index = ~0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001657 }
1658 return 0;
1659}
1660
Filip Tehlar7048ff12017-07-27 08:09:14 +02001661int
1662vnet_lisp_map_register_fallback_threshold_set (u32 value)
1663{
1664 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1665 if (0 == value)
1666 {
1667 return VNET_API_ERROR_INVALID_ARGUMENT;
1668 }
1669
1670 lcm->max_expired_map_registers = value;
1671 return 0;
1672}
1673
1674u32
1675vnet_lisp_map_register_fallback_threshold_get (void)
1676{
1677 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1678 return lcm->max_expired_map_registers;
1679}
1680
Florin Corasba888e42017-01-24 11:38:18 -08001681/**
1682 * Configure Proxy-ETR
1683 *
1684 * @param ip PETR's IP address
1685 * @param is_add Flag that indicates if this is an addition or removal
1686 *
1687 * return 0 on success
1688 */
1689int
1690vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1691{
1692 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1693 u32 ls_index = ~0;
1694 mapping_t *m;
1695 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1696 locator_t loc;
1697
1698 if (vnet_lisp_enable_disable_status () == 0)
1699 {
1700 clib_warning ("LISP is disabled!");
1701 return VNET_API_ERROR_LISP_DISABLED;
1702 }
1703
Dave Barachb7b92992018-10-17 10:38:51 -04001704 clib_memset (ls_args, 0, sizeof (*ls_args));
Florin Corasba888e42017-01-24 11:38:18 -08001705
1706 if (is_add)
1707 {
1708 /* Create dummy petr locator-set */
Dave Barachb7b92992018-10-17 10:38:51 -04001709 clib_memset (&loc, 0, sizeof (loc));
Florin Corasba888e42017-01-24 11:38:18 -08001710 gid_address_from_ip (&loc.address, ip);
1711 loc.priority = 1;
1712 loc.state = loc.weight = 1;
Florin Coras2743cc42017-01-29 16:42:20 -08001713 loc.local = 0;
Florin Corasba888e42017-01-24 11:38:18 -08001714
1715 ls_args->is_add = 1;
1716 ls_args->index = ~0;
1717 vec_add1 (ls_args->locators, loc);
1718 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1719
1720 /* Add petr mapping */
1721 pool_get (lcm->mapping_pool, m);
1722 m->locator_set_index = ls_index;
1723 lcm->petr_map_index = m - lcm->mapping_pool;
1724
1725 /* Enable use-petr */
1726 lcm->flags |= LISP_FLAG_USE_PETR;
1727 }
1728 else
1729 {
1730 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1731
1732 /* Remove petr locator */
1733 ls_args->is_add = 0;
1734 ls_args->index = m->locator_set_index;
1735 vnet_lisp_add_del_locator_set (ls_args, 0);
1736
1737 /* Remove petr mapping */
1738 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1739
1740 /* Disable use-petr */
1741 lcm->flags &= ~LISP_FLAG_USE_PETR;
Filip Tehlar0a8840d2017-10-16 05:48:23 -07001742 lcm->petr_map_index = ~0;
Florin Corasba888e42017-01-24 11:38:18 -08001743 }
1744 return 0;
1745}
1746
Florin Corase127a7e2016-02-18 22:20:01 +01001747/* cleans locator to locator-set data and removes locators not part of
1748 * any locator-set */
1749static void
1750clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1751{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001752 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001753 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1754 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01001755 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001756 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1757 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1758 loc_indexp[0]);
1759 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1760 {
1761 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1762 if (ls_indexp[0] == lsi)
1763 break;
1764 }
Florin Corase127a7e2016-02-18 22:20:01 +01001765
Florin Corasa2157cf2016-08-16 21:09:14 +02001766 /* delete index for removed locator-set */
1767 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01001768
1769 /* delete locator if it's part of no locator-set */
1770 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001771 {
1772 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1773 vec_add1 (to_be_deleted, i);
1774 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001775 }
1776
1777 if (to_be_deleted)
1778 {
1779 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02001780 {
1781 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1782 vec_del1 (ls->locator_indices, loc_indexp[0]);
1783 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001784 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01001785 }
1786}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001787
Florin Corasf727db92016-06-23 15:01:58 +02001788static inline uword *
1789get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001790{
Florin Corasa2157cf2016-08-16 21:09:14 +02001791 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001792
Florin Corasa2157cf2016-08-16 21:09:14 +02001793 ASSERT (a != NULL);
1794 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001795
1796 /* find locator-set */
1797 if (a->local)
1798 {
Dave Barach59b25652017-09-10 15:04:27 -04001799 ASSERT (a->name);
Florin Corasa2157cf2016-08-16 21:09:14 +02001800 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001801 }
1802 else
1803 {
1804 *p = a->index;
1805 }
1806
1807 return p;
1808}
1809
Florin Corasf727db92016-06-23 15:01:58 +02001810static inline int
1811is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02001812 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001813{
Florin Corasa2157cf2016-08-16 21:09:14 +02001814 locator_t *itloc;
1815 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001816
Florin Corasa2157cf2016-08-16 21:09:14 +02001817 ASSERT (ls != NULL);
1818 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001819
Florin Corasa2157cf2016-08-16 21:09:14 +02001820 vec_foreach (locit, ls->locator_indices)
1821 {
1822 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1823 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1824 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1825 {
1826 clib_warning ("Duplicate locator");
1827 return VNET_API_ERROR_VALUE_EXIST;
1828 }
1829 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001830
1831 return 0;
1832}
1833
Filip Tehlar68d2e242017-04-21 12:05:58 +02001834static void
Filip Tehlar9d286a42017-10-26 23:57:09 -07001835update_adjacencies_by_map_index (lisp_cp_main_t * lcm,
Filip Tehlar68d2e242017-04-21 12:05:58 +02001836 u32 mapping_index, u8 remove_only)
1837{
1838 fwd_entry_t *fwd;
1839 mapping_t *map;
Filip Tehlar9d286a42017-10-26 23:57:09 -07001840 uword *fei = 0, *rmts_idxp = 0;
1841 u32 **rmts = 0, *remote_idxp = 0, *rmts_copy = 0;
Filip Tehlar68d2e242017-04-21 12:05:58 +02001842 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -04001843 clib_memset (a, 0, sizeof (*a));
Filip Tehlar68d2e242017-04-21 12:05:58 +02001844
1845 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1846
Filip Tehlar9d286a42017-10-26 23:57:09 -07001847 if (map->local)
1848 {
1849 rmts_idxp = hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, mapping_index);
1850 if (rmts_idxp)
1851 {
1852 rmts =
1853 pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idxp[0]);
1854 rmts_copy = vec_dup (rmts[0]);
Filip Tehlar68d2e242017-04-21 12:05:58 +02001855
Filip Tehlar9d286a42017-10-26 23:57:09 -07001856 vec_foreach (remote_idxp, rmts_copy)
1857 {
1858 fei = hash_get (lcm->fwd_entry_by_mapping_index, remote_idxp[0]);
1859 if (!fei)
1860 continue;
Filip Tehlar68d2e242017-04-21 12:05:58 +02001861
Filip Tehlar9d286a42017-10-26 23:57:09 -07001862 fwd = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
1863 a->is_add = 0;
1864 gid_address_copy (&a->leid, &fwd->leid);
1865 gid_address_copy (&a->reid, &fwd->reid);
1866 vnet_lisp_add_del_adjacency (a);
1867
1868 if (!remove_only)
1869 {
1870 a->is_add = 1;
1871 vnet_lisp_add_del_adjacency (a);
1872 }
1873 }
1874 vec_free (rmts_copy);
1875 }
1876 }
1877 else
1878 {
1879 fei = hash_get (lcm->fwd_entry_by_mapping_index, mapping_index);
1880 if (!fei)
1881 return;
1882
1883 fwd = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
1884 a->is_add = 0;
1885 gid_address_copy (&a->leid, &fwd->leid);
1886 gid_address_copy (&a->reid, &fwd->reid);
1887 vnet_lisp_add_del_adjacency (a);
1888
1889 if (!remove_only)
1890 {
1891 a->is_add = 1;
1892 vnet_lisp_add_del_adjacency (a);
1893 }
1894 }
Filip Tehlar68d2e242017-04-21 12:05:58 +02001895}
1896
1897static void
Filip Tehlar9d286a42017-10-26 23:57:09 -07001898update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm,
Filip Tehlar68d2e242017-04-21 12:05:58 +02001899 u32 ls_index, u8 remove_only)
1900{
1901 u32 i, *map_indexp;
1902 u32 **eid_indexes;
Filip Tehlarfacee282017-04-27 14:29:27 +02001903
1904 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1905 return;
1906
Filip Tehlar68d2e242017-04-21 12:05:58 +02001907 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1908
1909 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1910 {
1911 map_indexp = vec_elt_at_index (eid_indexes[0], i);
Filip Tehlar9d286a42017-10-26 23:57:09 -07001912 update_adjacencies_by_map_index (lcm, map_indexp[0], remove_only);
Filip Tehlar68d2e242017-04-21 12:05:58 +02001913 }
1914}
1915
Florin Corasf727db92016-06-23 15:01:58 +02001916static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02001917remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1918 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001919{
Florin Corasa2157cf2016-08-16 21:09:14 +02001920 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1921 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001922
Florin Corasa2157cf2016-08-16 21:09:14 +02001923 ASSERT (ls != NULL);
1924 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001925
Florin Corasa2157cf2016-08-16 21:09:14 +02001926 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1927 pool_put_index (lcm->locator_pool, locit[0]);
1928 vec_del1 (ls->locator_indices, loc_id);
1929 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001930}
1931
1932int
1933vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001934 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001935{
Florin Corasa2157cf2016-08-16 21:09:14 +02001936 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1937 locator_t *loc = NULL, *itloc = NULL;
1938 uword _p = (u32) ~ 0, *p = &_p;
1939 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001940 u32 loc_id = ~0;
1941 int ret = 0;
1942
Florin Corasa2157cf2016-08-16 21:09:14 +02001943 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001944
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001945 if (vnet_lisp_enable_disable_status () == 0)
1946 {
1947 clib_warning ("LISP is disabled!");
1948 return VNET_API_ERROR_LISP_DISABLED;
1949 }
1950
Florin Corasa2157cf2016-08-16 21:09:14 +02001951 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001952 if (!p)
1953 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001954 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001955 return VNET_API_ERROR_INVALID_ARGUMENT;
1956 }
1957
1958 if (ls == 0)
1959 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001960 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001961 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001962 {
1963 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1964 p[0]);
1965 return VNET_API_ERROR_INVALID_ARGUMENT;
1966 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001967 }
1968
1969 if (a->is_add)
1970 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001971 if (ls_result)
1972 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001973
Florin Corasa2157cf2016-08-16 21:09:14 +02001974 /* allocate locators */
1975 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001976 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001977 ret = is_locator_in_locator_set (lcm, ls, itloc);
1978 if (0 != ret)
1979 {
1980 return ret;
1981 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001982
Florin Corasa2157cf2016-08-16 21:09:14 +02001983 pool_get (lcm->locator_pool, loc);
1984 loc[0] = itloc[0];
1985 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001986
Florin Corasa2157cf2016-08-16 21:09:14 +02001987 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001988
Florin Corasa2157cf2016-08-16 21:09:14 +02001989 vec_validate (lcm->locator_to_locator_sets, loc_index);
1990 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1991 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001992 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001993 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001994 }
1995 else
1996 {
1997 ls_index = p[0];
Filip Tehlar68d2e242017-04-21 12:05:58 +02001998 u8 removed;
Florin Corasa2157cf2016-08-16 21:09:14 +02001999
Filip Tehlar68d2e242017-04-21 12:05:58 +02002000 vec_foreach (itloc, a->locators)
Florin Corasa2157cf2016-08-16 21:09:14 +02002001 {
Filip Tehlar68d2e242017-04-21 12:05:58 +02002002 removed = 0;
2003 loc_id = 0;
2004 vec_foreach (locit, ls->locator_indices)
2005 {
2006 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
Florin Corasa2157cf2016-08-16 21:09:14 +02002007
Filip Tehlar68d2e242017-04-21 12:05:58 +02002008 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2009 {
2010 removed = 1;
2011 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2012 }
2013 if (0 == loc->local &&
2014 !gid_address_cmp (&loc->address, &itloc->address))
2015 {
2016 removed = 1;
2017 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2018 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002019
Filip Tehlar68d2e242017-04-21 12:05:58 +02002020 if (removed)
2021 {
2022 /* update fwd entries using this locator in DP */
Filip Tehlar9d286a42017-10-26 23:57:09 -07002023 update_fwd_entries_by_locator_set (lcm, ls_index,
Filip Tehlar68d2e242017-04-21 12:05:58 +02002024 vec_len (ls->locator_indices)
2025 == 0);
2026 }
2027
2028 loc_id++;
2029 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002030 }
2031 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002032
2033 return 0;
2034}
2035
Florin Corase127a7e2016-02-18 22:20:01 +01002036int
2037vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002038 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01002039{
Florin Corasa2157cf2016-08-16 21:09:14 +02002040 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2041 locator_set_t *ls;
2042 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002043 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02002044 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002045 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002046
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002047 if (vnet_lisp_enable_disable_status () == 0)
2048 {
2049 clib_warning ("LISP is disabled!");
2050 return VNET_API_ERROR_LISP_DISABLED;
2051 }
2052
Florin Corase127a7e2016-02-18 22:20:01 +01002053 if (a->is_add)
2054 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002055 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01002056
2057 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02002058 if (p && p[0] != (u32) ~ 0)
2059 {
2060 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2061 if (!ls)
2062 {
2063 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2064 p[0]);
2065 return -1;
2066 }
Florin Corase127a7e2016-02-18 22:20:01 +01002067
Florin Corasa2157cf2016-08-16 21:09:14 +02002068 /* clean locator to locator-set vectors and remove locators if
2069 * they're not part of another locator-set */
2070 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002071
Florin Corasa2157cf2016-08-16 21:09:14 +02002072 /* remove locator indices from locator set */
2073 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01002074
Florin Corasa2157cf2016-08-16 21:09:14 +02002075 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01002076
Florin Corasa2157cf2016-08-16 21:09:14 +02002077 if (ls_result)
2078 ls_result[0] = p[0];
2079 }
Florin Corase127a7e2016-02-18 22:20:01 +01002080 /* new locator-set */
2081 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002082 {
2083 pool_get (lcm->locator_set_pool, ls);
Dave Barachb7b92992018-10-17 10:38:51 -04002084 clib_memset (ls, 0, sizeof (*ls));
Florin Corasa2157cf2016-08-16 21:09:14 +02002085 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01002086
Florin Corasa2157cf2016-08-16 21:09:14 +02002087 if (a->local)
2088 {
2089 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01002090
Florin Corasa2157cf2016-08-16 21:09:14 +02002091 if (!lcm->locator_set_index_by_name)
2092 lcm->locator_set_index_by_name = hash_create_vec (
2093 /* size */
2094 0,
2095 sizeof
2096 (ls->name
2097 [0]),
2098 sizeof
2099 (uword));
2100 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2101 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002102
Florin Corasa2157cf2016-08-16 21:09:14 +02002103 /* mark as local locator-set */
2104 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2105 }
2106 ls->local = a->local;
2107 if (ls_result)
2108 ls_result[0] = ls_index;
2109 }
Florin Corase127a7e2016-02-18 22:20:01 +01002110
Florin Corasa2157cf2016-08-16 21:09:14 +02002111 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002112 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02002113 {
2114 return ret;
2115 }
Florin Corase127a7e2016-02-18 22:20:01 +01002116 }
2117 else
2118 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002119 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002120 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002121 {
2122 clib_warning ("locator-set %v doesn't exists", a->name);
2123 return -1;
2124 }
Florin Corase127a7e2016-02-18 22:20:01 +01002125
Florin Corasa2157cf2016-08-16 21:09:14 +02002126 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002127 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002128 {
2129 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2130 return -1;
2131 }
Florin Corase127a7e2016-02-18 22:20:01 +01002132
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002133 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02002134 {
2135 clib_warning ("Can't delete the locator-set used to constrain "
2136 "the itr-rlocs in map-requests!");
2137 return -1;
2138 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002139
Florin Corasa2157cf2016-08-16 21:09:14 +02002140 if (vec_len (lcm->locator_set_to_eids) != 0)
2141 {
2142 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2143 if (vec_len (eid_indexes[0]) != 0)
2144 {
2145 clib_warning
2146 ("Can't delete a locator that supports a mapping!");
2147 return -1;
2148 }
2149 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002150
2151 /* clean locator to locator-sets data */
2152 clean_locator_to_locator_set (lcm, p[0]);
2153
2154 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002155 {
2156 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002157
Florin Corasa2157cf2016-08-16 21:09:14 +02002158 vec_foreach_index (it, lcm->local_locator_set_indexes)
2159 {
2160 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2161 if (lsi == p[0])
2162 {
2163 vec_del1 (lcm->local_locator_set_indexes, it);
2164 break;
2165 }
2166 }
2167 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2168 }
2169 vec_free (ls->name);
2170 vec_free (ls->locator_indices);
2171 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002172 }
2173 return 0;
2174}
2175
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002176int
2177vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2178{
2179 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2180
2181 lcm->rloc_probing = is_enable;
2182 return 0;
2183}
2184
2185int
2186vnet_lisp_map_register_enable_disable (u8 is_enable)
2187{
2188 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2189
2190 lcm->map_registering = is_enable;
2191 return 0;
2192}
2193
Filip Tehlar0a8840d2017-10-16 05:48:23 -07002194static void
2195lisp_cp_register_dst_port (vlib_main_t * vm)
2196{
2197 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2198 lisp_cp_input_node.index, 1 /* is_ip4 */ );
2199 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2200 lisp_cp_input_node.index, 0 /* is_ip4 */ );
2201}
2202
2203static void
2204lisp_cp_unregister_dst_port (vlib_main_t * vm)
2205{
2206 udp_unregister_dst_port (vm, UDP_DST_PORT_lisp_cp, 0 /* is_ip4 */ );
2207 udp_unregister_dst_port (vm, UDP_DST_PORT_lisp_cp6, 1 /* is_ip4 */ );
2208}
2209
2210/**
2211 * lisp_cp_enable_l2_l3_ifaces
2212 *
2213 * Enable all l2 and l3 ifaces
2214 */
2215static void
2216lisp_cp_enable_l2_l3_ifaces (lisp_cp_main_t * lcm, u8 with_default_route)
2217{
2218 u32 vni, dp_table;
2219
2220 /* *INDENT-OFF* */
2221 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2222 dp_add_del_iface(lcm, vni, /* is_l2 */ 0, /* is_add */1,
2223 with_default_route);
2224 }));
2225 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2226 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1,
2227 with_default_route);
2228 }));
2229 /* *INDENT-ON* */
2230}
2231
2232static void
2233lisp_cp_disable_l2_l3_ifaces (lisp_cp_main_t * lcm)
2234{
2235 u32 **rmts;
2236
2237 /* clear interface table */
2238 hash_free (lcm->fwd_entry_by_mapping_index);
2239 pool_free (lcm->fwd_entry_pool);
2240 /* Clear state tracking rmt-lcl fwd entries */
2241 /* *INDENT-OFF* */
2242 pool_foreach(rmts, lcm->lcl_to_rmt_adjacencies,
2243 {
2244 vec_free(rmts[0]);
2245 });
2246 /* *INDENT-ON* */
2247 hash_free (lcm->lcl_to_rmt_adjs_by_lcl_idx);
2248 pool_free (lcm->lcl_to_rmt_adjacencies);
2249}
2250
Filip Tehlar46d4e362016-05-09 09:39:26 +02002251clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02002252vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002253{
Florin Corasa2157cf2016-08-16 21:09:14 +02002254 clib_error_t *error = 0;
2255 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2256 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002257
Florin Corasf727db92016-06-23 15:01:58 +02002258 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002259 error = vnet_lisp_gpe_enable_disable (a);
2260 if (error)
2261 {
2262 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02002263 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002264 }
2265
Filip Tehlar0a8840d2017-10-16 05:48:23 -07002266 /* decide what to do based on mode */
Florin Corasa2157cf2016-08-16 21:09:14 +02002267
Filip Tehlar0a8840d2017-10-16 05:48:23 -07002268 if (lcm->flags & LISP_FLAG_XTR_MODE)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002269 {
Filip Tehlar0a8840d2017-10-16 05:48:23 -07002270 if (is_enable)
2271 {
2272 lisp_cp_register_dst_port (lcm->vlib_main);
2273 lisp_cp_enable_l2_l3_ifaces (lcm, 1 /* with_default_route */ );
2274 }
2275 else
2276 {
2277 lisp_cp_unregister_dst_port (lcm->vlib_main);
2278 lisp_cp_disable_l2_l3_ifaces (lcm);
2279 }
2280 }
2281
2282 if (lcm->flags & LISP_FLAG_PETR_MODE)
2283 {
2284 /* if in xTR mode, the LISP ports were already (un)registered above */
2285 if (!(lcm->flags & LISP_FLAG_XTR_MODE))
2286 {
2287 if (is_enable)
2288 lisp_cp_register_dst_port (lcm->vlib_main);
2289 else
2290 lisp_cp_unregister_dst_port (lcm->vlib_main);
2291 }
2292 }
2293
2294 if (lcm->flags & LISP_FLAG_PITR_MODE)
2295 {
2296 if (is_enable)
2297 {
2298 /* install interfaces, but no default routes */
2299 lisp_cp_enable_l2_l3_ifaces (lcm, 0 /* with_default_route */ );
2300 }
2301 else
2302 {
2303 lisp_cp_disable_l2_l3_ifaces (lcm);
2304 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002305 }
2306
Florin Corasd37da902019-05-30 02:01:55 -07002307 if (is_enable)
2308 vnet_lisp_create_retry_process (lcm);
2309
Filip Tehlar46d4e362016-05-09 09:39:26 +02002310 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02002311 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002312
2313 return 0;
2314}
2315
Filip Tehlar46d4e362016-05-09 09:39:26 +02002316u8
2317vnet_lisp_enable_disable_status (void)
2318{
Florin Corasa2157cf2016-08-16 21:09:14 +02002319 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002320 return lcm->is_enabled;
2321}
2322
Florin Corase127a7e2016-02-18 22:20:01 +01002323int
2324vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2325{
Florin Corasa2157cf2016-08-16 21:09:14 +02002326 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002327 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002328 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01002329
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002330 if (vnet_lisp_enable_disable_status () == 0)
2331 {
2332 clib_warning ("LISP is disabled!");
2333 return VNET_API_ERROR_LISP_DISABLED;
2334 }
2335
Florin Corase127a7e2016-02-18 22:20:01 +01002336 if (a->is_add)
2337 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002338
2339 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02002340 {
2341 clib_warning ("map-resolver %U already exists!", format_ip_address,
2342 &a->address);
2343 return -1;
2344 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002345
Dave Barachb7b92992018-10-17 10:38:51 -04002346 clib_memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02002347 ip_address_copy (&mr->address, &a->address);
2348 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002349
2350 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02002351 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002352 }
2353 else
2354 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002355 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2356 {
2357 mr = vec_elt_at_index (lcm->map_resolvers, i);
2358 if (!ip_address_cmp (&mr->address, &a->address))
2359 {
2360 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2361 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002362
Florin Corasa2157cf2016-08-16 21:09:14 +02002363 vec_del1 (lcm->map_resolvers, i);
2364 break;
2365 }
2366 }
Florin Corase127a7e2016-02-18 22:20:01 +01002367 }
2368 return 0;
2369}
2370
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002371int
Filip Tehlar1e553a02017-08-02 12:45:07 +02002372vnet_lisp_map_register_set_ttl (u32 ttl)
2373{
2374 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2375 lcm->map_register_ttl = ttl;
2376 return 0;
2377}
2378
2379u32
2380vnet_lisp_map_register_get_ttl (void)
2381{
2382 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2383 return lcm->map_register_ttl;
2384}
2385
2386int
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002387vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2388{
Florin Corasa2157cf2016-08-16 21:09:14 +02002389 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2390 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002391
Florin Corasf727db92016-06-23 15:01:58 +02002392 if (vnet_lisp_enable_disable_status () == 0)
2393 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002394 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02002395 return VNET_API_ERROR_LISP_DISABLED;
2396 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002397
2398 if (a->is_add)
2399 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002400 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002401 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002402 {
2403 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2404 return VNET_API_ERROR_INVALID_ARGUMENT;
2405 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002406
2407 lcm->mreq_itr_rlocs = p[0];
2408 }
2409 else
2410 {
2411 lcm->mreq_itr_rlocs = ~0;
2412 }
2413
2414 return 0;
2415}
2416
Florin Corase127a7e2016-02-18 22:20:01 +01002417/* Statistics (not really errors) */
2418#define foreach_lisp_cp_lookup_error \
2419_(DROP, "drop") \
Filip Tehlard5a65db2017-05-17 17:21:10 +02002420_(MAP_REQUESTS_SENT, "map-request sent") \
Filip Tehlar05879992017-09-05 15:46:09 +02002421_(ARP_REPLY_TX, "ARP replies sent") \
2422_(NDP_NEIGHBOR_ADVERTISEMENT_TX, \
2423 "neighbor advertisement sent")
Florin Corase127a7e2016-02-18 22:20:01 +01002424
Florin Corasa2157cf2016-08-16 21:09:14 +02002425static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01002426#define _(sym,string) string,
2427 foreach_lisp_cp_lookup_error
2428#undef _
2429};
2430
2431typedef enum
2432{
2433#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02002434 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01002435#undef _
2436 LISP_CP_LOOKUP_N_ERROR,
2437} lisp_cp_lookup_error_t;
2438
2439typedef enum
2440{
2441 LISP_CP_LOOKUP_NEXT_DROP,
Filip Tehlar05879992017-09-05 15:46:09 +02002442 LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX,
Florin Corase127a7e2016-02-18 22:20:01 +01002443 LISP_CP_LOOKUP_N_NEXT,
2444} lisp_cp_lookup_next_t;
2445
2446typedef struct
2447{
2448 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02002449 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01002450} lisp_cp_lookup_trace_t;
2451
2452u8 *
2453format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2454{
2455 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2456 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02002457 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01002458
2459 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02002460 format_ip_address, &t->map_resolver_ip, format_gid_address,
2461 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01002462 return s;
2463}
2464
Florin Coras1c494842016-06-16 21:08:56 +02002465int
2466get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02002467 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02002468{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002469 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02002470 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01002471
Florin Corasa2157cf2016-08-16 21:09:14 +02002472 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01002473 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002474 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02002475 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002476 }
2477
Florin Corasddfafb82016-05-13 18:09:56 +02002478 /* find the first mr ip we have a route to and the ip of the
2479 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002480 vec_foreach (mrit, lcm->map_resolvers)
2481 {
2482 a = &mrit->address;
2483 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2484 {
2485 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002486
Florin Corasa2157cf2016-08-16 21:09:14 +02002487 /* also update globals */
2488 return 1;
2489 }
2490 }
Florin Corasddfafb82016-05-13 18:09:56 +02002491
Florin Corasa2157cf2016-08-16 21:09:14 +02002492 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02002493 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02002494}
Filip Tehlar254b0362016-04-07 10:04:34 +02002495
Filip Tehlarbeceab92016-04-20 17:21:55 +02002496static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02002497build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2498{
Florin Corasa2157cf2016-08-16 21:09:14 +02002499 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02002500 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02002501 locator_t *loc;
2502 u32 *loc_indexp;
2503 ip_interface_address_t *ia = 0;
2504 gid_address_t gid_data, *gid = &gid_data;
2505 gid_address_t *rlocs = 0;
2506 ip_prefix_t *ippref = &gid_address_ippref (gid);
2507 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02002508
Dave Barachb7b92992018-10-17 10:38:51 -04002509 clib_memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02002510 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02002511 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02002512 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002513 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02002514 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2515
Filip Tehlar254b0362016-04-07 10:04:34 +02002516 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02002517
2518 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02002519 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2520 loc->sw_if_index, 1 /* unnumbered */,
2521 ({
Florin Coras1c494842016-06-16 21:08:56 +02002522 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
Neale Rannsea93e482019-11-12 17:16:47 +00002523 ip_address_set (rloc, addr, AF_IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02002524 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002525 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002526 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002527 }));
2528
Filip Tehlar254b0362016-04-07 10:04:34 +02002529 /* Add ipv6 locators */
2530 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2531 loc->sw_if_index, 1 /* unnumbered */,
2532 ({
Florin Coras1c494842016-06-16 21:08:56 +02002533 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
Neale Rannsea93e482019-11-12 17:16:47 +00002534 ip_address_set (rloc, addr, AF_IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02002535 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002536 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002537 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002538 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002539 /* *INDENT-ON* */
2540
Filip Tehlar254b0362016-04-07 10:04:34 +02002541 }
2542 return rlocs;
2543}
2544
Florin Corase127a7e2016-02-18 22:20:01 +01002545static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002546build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2547 ip_address_t * sloc, ip_address_t * rloc,
2548 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2549{
2550 vlib_buffer_t *b;
2551 u32 bi;
2552 vlib_main_t *vm = lcm->vlib_main;
2553
2554 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2555 {
2556 clib_warning ("Can't allocate buffer for Map-Request!");
2557 return 0;
2558 }
2559
2560 b = vlib_get_buffer (vm, bi);
2561
2562 /* leave some space for the encap headers */
2563 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2564
2565 /* put lisp msg */
2566 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2567 1 /* rloc probe */ , nonce_res);
2568
2569 /* push outer ip header */
2570 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasfdbc3822017-07-27 00:34:12 -07002571 rloc, 1);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002572
2573 bi_res[0] = bi;
2574
2575 return b;
2576}
2577
2578static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002579build_encapsulated_map_request (lisp_cp_main_t * lcm,
2580 gid_address_t * seid, gid_address_t * deid,
2581 locator_set_t * loc_set, ip_address_t * mr_ip,
2582 ip_address_t * sloc, u8 is_smr_invoked,
2583 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01002584{
Florin Corasa2157cf2016-08-16 21:09:14 +02002585 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01002586 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02002587 gid_address_t *rlocs = 0;
2588 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01002589
2590 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2591 {
2592 clib_warning ("Can't allocate buffer for Map-Request!");
2593 return 0;
2594 }
2595
2596 b = vlib_get_buffer (vm, bi);
Florin Corasfdbc3822017-07-27 00:34:12 -07002597 b->flags = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002598
2599 /* leave some space for the encap headers */
2600 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2601
Filip Tehlar254b0362016-04-07 10:04:34 +02002602 /* get rlocs */
2603 rlocs = build_itr_rloc_list (lcm, loc_set);
2604
Filip Tehlard5fcc462016-10-17 16:20:18 +02002605 if (MR_MODE_SRC_DST == lcm->map_request_mode
2606 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02002607 {
2608 gid_address_t sd;
Dave Barachb7b92992018-10-17 10:38:51 -04002609 clib_memset (&sd, 0, sizeof (sd));
Florin Corasdca88042016-09-14 16:01:38 +02002610 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002611 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2612 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002613 }
2614 else
2615 {
2616 /* put lisp msg */
2617 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002618 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002619 }
Florin Corase127a7e2016-02-18 22:20:01 +01002620
2621 /* push ecm: udp-ip-lisp */
2622 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2623
Florin Corase127a7e2016-02-18 22:20:01 +01002624 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02002625 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasfdbc3822017-07-27 00:34:12 -07002626 mr_ip, 1);
Florin Corase127a7e2016-02-18 22:20:01 +01002627
2628 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02002629
Florin Corasa2157cf2016-08-16 21:09:14 +02002630 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01002631 return b;
2632}
2633
2634static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002635reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01002636{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002637 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2638 r->retries_num = 0;
2639}
2640
Filip Tehlar7048ff12017-07-27 08:09:14 +02002641#define foreach_msmr \
2642 _(server) \
2643 _(resolver)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002644
Filip Tehlar7048ff12017-07-27 08:09:14 +02002645#define _(name) \
2646static int \
2647elect_map_ ## name (lisp_cp_main_t * lcm) \
2648{ \
2649 lisp_msmr_t *mr; \
2650 vec_foreach (mr, lcm->map_ ## name ## s) \
2651 { \
2652 if (!mr->is_down) \
2653 { \
2654 ip_address_copy (&lcm->active_map_ ##name, &mr->address); \
2655 lcm->do_map_ ## name ## _election = 0; \
2656 return 1; \
2657 } \
2658 } \
2659 return 0; \
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002660}
Filip Tehlar7048ff12017-07-27 08:09:14 +02002661foreach_msmr
2662#undef _
2663 static void
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002664free_map_register_records (mapping_t * maps)
2665{
2666 mapping_t *map;
2667 vec_foreach (map, maps) vec_free (map->locators);
2668
2669 vec_free (maps);
2670}
2671
2672static void
2673add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2674 ip_address_t * probed_loc)
2675{
2676 u32 *li;
2677 locator_t *loc, new;
2678 ip_interface_address_t *ia = 0;
2679 void *addr;
2680 ip_address_t *new_ip = &gid_address_ip (&new.address);
2681
2682 m->locators = 0;
2683 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2684 locator_set_index);
2685 vec_foreach (li, ls->locator_indices)
2686 {
2687 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2688 new = loc[0];
2689 if (loc->local)
2690 {
2691 /* *INDENT-OFF* */
2692 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2693 loc->sw_if_index, 1 /* unnumbered */,
2694 ({
2695 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2696 ia);
Neale Rannsea93e482019-11-12 17:16:47 +00002697 ip_address_set (new_ip, addr, AF_IP4);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002698 }));
2699
2700 /* Add ipv6 locators */
2701 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2702 loc->sw_if_index, 1 /* unnumbered */,
2703 ({
2704 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2705 ia);
Neale Rannsea93e482019-11-12 17:16:47 +00002706 ip_address_set (new_ip, addr, AF_IP6);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002707 }));
2708 /* *INDENT-ON* */
2709
2710 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2711 new.probed = 1;
2712 }
2713 vec_add1 (m->locators, new);
2714 }
2715}
2716
2717static mapping_t *
2718build_map_register_record_list (lisp_cp_main_t * lcm)
2719{
2720 mapping_t *recs = 0, rec, *m;
2721
2722 /* *INDENT-OFF* */
2723 pool_foreach(m, lcm->mapping_pool,
2724 {
2725 /* for now build only local mappings */
2726 if (!m->local)
2727 continue;
2728
2729 rec = m[0];
2730 add_locators (lcm, &rec, m->locator_set_index, NULL);
2731 vec_add1 (recs, rec);
2732 });
2733 /* *INDENT-ON* */
2734
2735 return recs;
2736}
2737
Damjan Mariond1bed682019-04-24 15:20:35 +02002738static vnet_crypto_alg_t
2739lisp_key_type_to_crypto_alg (lisp_key_type_t key_id)
2740{
2741 switch (key_id)
2742 {
2743 case HMAC_SHA_1_96:
2744 return VNET_CRYPTO_ALG_HMAC_SHA1;
2745 case HMAC_SHA_256_128:
2746 return VNET_CRYPTO_ALG_HMAC_SHA256;
2747 default:
2748 clib_warning ("unsupported encryption key type: %d!", key_id);
2749 break;
2750 }
2751 return VNET_CRYPTO_ALG_NONE;
2752}
2753
Damjan Marion060bfb92019-03-29 13:47:54 +01002754static vnet_crypto_op_id_t
Filip Tehlarc6c43942019-03-21 05:56:59 -07002755lisp_key_type_to_crypto_op (lisp_key_type_t key_id)
2756{
2757 switch (key_id)
2758 {
2759 case HMAC_SHA_1_96:
2760 return VNET_CRYPTO_OP_SHA1_HMAC;
2761 case HMAC_SHA_256_128:
2762 return VNET_CRYPTO_OP_SHA256_HMAC;
2763 default:
2764 clib_warning ("unsupported encryption key type: %d!", key_id);
2765 break;
2766 }
2767 return VNET_CRYPTO_OP_NONE;
2768}
2769
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002770static int
2771update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2772 lisp_key_type_t key_id, u8 * key,
2773 u16 auth_data_len, u32 msg_len)
2774{
Filip Tehlarc6c43942019-03-21 05:56:59 -07002775 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002776 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2777 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
Filip Tehlarc6c43942019-03-21 05:56:59 -07002778 vnet_crypto_op_t _op, *op = &_op;
Damjan Mariond1bed682019-04-24 15:20:35 +02002779 vnet_crypto_key_index_t ki;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002780
Filip Tehlarc6c43942019-03-21 05:56:59 -07002781 vnet_crypto_op_init (op, lisp_key_type_to_crypto_op (key_id));
Filip Tehlarc6c43942019-03-21 05:56:59 -07002782 op->len = msg_len;
Damjan Marion060bfb92019-03-29 13:47:54 +01002783 op->digest = MREG_DATA (map_reg_hdr);
Filip Tehlarc6c43942019-03-21 05:56:59 -07002784 op->src = (u8 *) map_reg_hdr;
Damjan Marion060bfb92019-03-29 13:47:54 +01002785 op->digest_len = 0;
Filip Tehlarc6c43942019-03-21 05:56:59 -07002786 op->iv = 0;
2787
Damjan Mariond1bed682019-04-24 15:20:35 +02002788 ki = vnet_crypto_key_add (lcm->vlib_main,
2789 lisp_key_type_to_crypto_alg (key_id), key,
2790 vec_len (key));
2791
2792 op->key_index = ki;
2793
Filip Tehlarc6c43942019-03-21 05:56:59 -07002794 vnet_crypto_process_ops (lcm->vlib_main, op, 1);
Damjan Mariond1bed682019-04-24 15:20:35 +02002795 vnet_crypto_key_del (lcm->vlib_main, ki);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002796
2797 return 0;
2798}
2799
2800static vlib_buffer_t *
2801build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2802 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2803 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2804 u32 * bi_res)
2805{
2806 void *map_reg_hdr;
2807 vlib_buffer_t *b;
2808 u32 bi, auth_data_len = 0, msg_len = 0;
2809 vlib_main_t *vm = lcm->vlib_main;
2810
2811 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2812 {
2813 clib_warning ("Can't allocate buffer for Map-Register!");
2814 return 0;
2815 }
2816
2817 b = vlib_get_buffer (vm, bi);
2818
2819 /* leave some space for the encap headers */
2820 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2821
2822 auth_data_len = auth_data_len_by_key_id (key_id);
2823 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2824 auth_data_len, nonce_res,
2825 &msg_len);
2826
2827 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2828 msg_len);
2829
2830 /* push outer ip header */
2831 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasfdbc3822017-07-27 00:34:12 -07002832 ms_ip, 1);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002833
2834 bi_res[0] = bi;
2835 return b;
2836}
2837
Filip Tehlar7048ff12017-07-27 08:09:14 +02002838#define _(name) \
2839static int \
2840get_egress_map_ ##name## _ip (lisp_cp_main_t * lcm, ip_address_t * ip) \
2841{ \
2842 lisp_msmr_t *mr; \
2843 while (lcm->do_map_ ## name ## _election \
2844 | (0 == ip_fib_get_first_egress_ip_for_dst \
2845 (lcm, &lcm->active_map_ ##name, ip))) \
2846 { \
2847 if (0 == elect_map_ ## name (lcm)) \
2848 /* all map resolvers/servers are down */ \
2849 { \
2850 /* restart MR/MS checking by marking all of them up */ \
2851 vec_foreach (mr, lcm->map_ ## name ## s) mr->is_down = 0; \
2852 return -1; \
2853 } \
2854 } \
2855 return 0; \
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002856}
2857
Filip Tehlar7048ff12017-07-27 08:09:14 +02002858foreach_msmr
2859#undef _
Filip Tehlarcda70662017-01-16 10:30:03 +01002860/* CP output statistics */
2861#define foreach_lisp_cp_output_error \
2862_(MAP_REGISTERS_SENT, "map-registers sent") \
Filip Tehlar5908e182017-10-16 06:51:11 -07002863_(MAP_REQUESTS_SENT, "map-requests sent") \
Filip Tehlarcda70662017-01-16 10:30:03 +01002864_(RLOC_PROBES_SENT, "rloc-probes sent")
Filip Tehlarcda70662017-01-16 10:30:03 +01002865static char *lisp_cp_output_error_strings[] = {
2866#define _(sym,string) string,
2867 foreach_lisp_cp_output_error
2868#undef _
2869};
2870
2871typedef enum
2872{
2873#define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2874 foreach_lisp_cp_output_error
2875#undef _
2876 LISP_CP_OUTPUT_N_ERROR,
2877} lisp_cp_output_error_t;
2878
2879static uword
2880lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2881 vlib_frame_t * from_frame)
2882{
2883 return 0;
2884}
2885
2886/* dummy node used only for statistics */
2887/* *INDENT-OFF* */
2888VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2889 .function = lisp_cp_output,
2890 .name = "lisp-cp-output",
2891 .vector_size = sizeof (u32),
2892 .format_trace = format_lisp_cp_input_trace,
2893 .type = VLIB_NODE_TYPE_INTERNAL,
2894
2895 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2896 .error_strings = lisp_cp_output_error_strings,
2897
2898 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2899
2900 .next_nodes = {
2901 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2902 },
2903};
2904/* *INDENT-ON* */
2905
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002906static int
2907send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2908 u32 local_locator_set_index, ip_address_t * sloc,
2909 ip_address_t * rloc)
2910{
2911 locator_set_t *ls;
2912 u32 bi;
2913 vlib_buffer_t *b;
2914 vlib_frame_t *f;
2915 u64 nonce = 0;
2916 u32 next_index, *to_next;
2917 gid_address_t *itr_rlocs;
2918
2919 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2920 itr_rlocs = build_itr_rloc_list (lcm, ls);
2921
2922 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2923 vec_free (itr_rlocs);
2924 if (!b)
2925 return -1;
2926
2927 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2928
Neale Rannsea93e482019-11-12 17:16:47 +00002929 next_index = (ip_addr_version (rloc) == AF_IP4) ?
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002930 ip4_lookup_node.index : ip6_lookup_node.index;
2931
2932 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2933
2934 /* Enqueue the packet */
2935 to_next = vlib_frame_vector_args (f);
2936 to_next[0] = bi;
2937 f->n_vectors = 1;
2938 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2939
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002940 return 0;
2941}
2942
2943static int
2944send_rloc_probes (lisp_cp_main_t * lcm)
2945{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002946 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002947 mapping_t *lm;
2948 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002949 locator_pair_t *lp;
Filip Tehlarcda70662017-01-16 10:30:03 +01002950 u32 si, rloc_probes_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002951
2952 /* *INDENT-OFF* */
2953 pool_foreach (e, lcm->fwd_entry_pool,
2954 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002955 if (vec_len (e->locator_pairs) == 0)
2956 continue;
2957
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002958 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2959 if (~0 == si)
2960 {
2961 clib_warning ("internal error: cannot find local eid %U in "
2962 "map-cache!", format_gid_address, &e->leid);
2963 continue;
2964 }
2965 lm = pool_elt_at_index (lcm->mapping_pool, si);
2966
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002967 /* get the best (lowest) priority */
2968 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002969
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002970 /* send rloc-probe for pair(s) with the best remote locator priority */
2971 vec_foreach (lp, e->locator_pairs)
2972 {
2973 if (lp->priority != lprio)
2974 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002975
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002976 /* get first remote locator */
2977 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2978 &lp->rmt_loc);
Filip Tehlarcda70662017-01-16 10:30:03 +01002979 rloc_probes_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002980 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002981 });
2982 /* *INDENT-ON* */
2983
Filip Tehlarcda70662017-01-16 10:30:03 +01002984 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2985 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2986 rloc_probes_sent);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002987 return 0;
2988}
2989
2990static int
2991send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2992{
Filip Tehlar7048ff12017-07-27 08:09:14 +02002993 pending_map_register_t *pmr;
Filip Tehlarcda70662017-01-16 10:30:03 +01002994 u32 bi, map_registers_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002995 vlib_buffer_t *b;
2996 ip_address_t sloc;
2997 vlib_frame_t *f;
2998 u64 nonce = 0;
2999 u32 next_index, *to_next;
Filip Tehlarcf121c82017-05-03 10:12:44 +02003000 mapping_t *records, *r, *group, *k;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003001
Filip Tehlar7048ff12017-07-27 08:09:14 +02003002 if (get_egress_map_server_ip (lcm, &sloc) < 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003003 return -1;
3004
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003005 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003006 if (!records)
3007 return -1;
3008
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003009 vec_foreach (r, records)
3010 {
3011 u8 *key = r->key;
3012 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003013
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003014 if (!key)
3015 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003016
Filip Tehlarcf121c82017-05-03 10:12:44 +02003017 group = 0;
3018 vec_add1 (group, r[0]);
3019
3020 /* group mappings that share common key */
3021 for (k = r + 1; k < vec_end (records); k++)
3022 {
3023 if (k->key_id != r->key_id)
3024 continue;
3025
3026 if (vec_is_equal (k->key, r->key))
3027 {
3028 vec_add1 (group, k[0]);
3029 k->key = 0; /* don't process this mapping again */
3030 }
3031 }
3032
Filip Tehlar7048ff12017-07-27 08:09:14 +02003033 b = build_map_register (lcm, &sloc, &lcm->active_map_server, &nonce,
3034 want_map_notif, group, key_id, key, &bi);
Filip Tehlarcf121c82017-05-03 10:12:44 +02003035 vec_free (group);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003036 if (!b)
3037 continue;
3038
3039 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3040
Neale Rannsea93e482019-11-12 17:16:47 +00003041 next_index = (ip_addr_version (&lcm->active_map_server) == AF_IP4) ?
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003042 ip4_lookup_node.index : ip6_lookup_node.index;
3043
3044 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3045
3046 /* Enqueue the packet */
3047 to_next = vlib_frame_vector_args (f);
3048 to_next[0] = bi;
3049 f->n_vectors = 1;
3050 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Filip Tehlarcda70662017-01-16 10:30:03 +01003051 map_registers_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003052
Filip Tehlar7048ff12017-07-27 08:09:14 +02003053 pool_get (lcm->pending_map_registers_pool, pmr);
Dave Barachb7b92992018-10-17 10:38:51 -04003054 clib_memset (pmr, 0, sizeof (*pmr));
Filip Tehlar7048ff12017-07-27 08:09:14 +02003055 pmr->time_to_expire = PENDING_MREG_EXPIRATION_TIME;
3056 hash_set (lcm->map_register_messages_by_nonce, nonce,
3057 pmr - lcm->pending_map_registers_pool);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003058 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003059 free_map_register_records (records);
3060
Filip Tehlarcda70662017-01-16 10:30:03 +01003061 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
3062 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
3063 map_registers_sent);
3064
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003065 return 0;
3066}
3067
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003068#define send_encapsulated_map_request(lcm, seid, deid, smr) \
3069 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3070
3071#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3072 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3073
3074static int
Florin Corasa2157cf2016-08-16 21:09:14 +02003075_send_encapsulated_map_request (lisp_cp_main_t * lcm,
3076 gid_address_t * seid, gid_address_t * deid,
3077 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003078{
Florin Corasa2157cf2016-08-16 21:09:14 +02003079 u32 next_index, bi = 0, *to_next, map_index;
3080 vlib_buffer_t *b;
3081 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01003082 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003083 locator_set_t *loc_set;
3084 mapping_t *map;
3085 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003086 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003087 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003088
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003089 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003090
3091 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003092 pool_foreach(pmr, lcm->pending_map_requests_pool,
3093 ({
3094 if (!gid_address_cmp (&pmr->src, seid)
3095 && !gid_address_cmp (&pmr->dst, deid))
3096 {
3097 duplicate_pmr = pmr;
3098 break;
3099 }
3100 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003101 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003102
3103 if (!is_resend && duplicate_pmr)
3104 {
3105 /* don't send the request if there is a pending map request already */
3106 return 0;
3107 }
3108
Filip Tehlar0a8840d2017-10-16 05:48:23 -07003109 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
3110
Florin Corase127a7e2016-02-18 22:20:01 +01003111 /* get locator-set for seid */
Filip Tehlar0a8840d2017-10-16 05:48:23 -07003112 if (!pitr_mode && gid_address_type (deid) != GID_ADDR_NSH)
Florin Corase127a7e2016-02-18 22:20:01 +01003113 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02003114 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3115 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003116 {
3117 clib_warning ("No local mapping found in eid-table for %U!",
3118 format_gid_address, seid);
3119 return -1;
3120 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02003121
3122 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3123
3124 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02003125 {
3126 clib_warning
3127 ("Mapping found for src eid %U is not marked as local!",
3128 format_gid_address, seid);
3129 return -1;
3130 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003131 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02003132 }
3133 else
3134 {
Filip Tehlar0a8840d2017-10-16 05:48:23 -07003135 if (pitr_mode)
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003136 {
Filip Tehlar0a8840d2017-10-16 05:48:23 -07003137 if (lcm->pitr_map_index != ~0)
3138 {
3139 map =
3140 pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3141 ls_index = map->locator_set_index;
3142 }
3143 else
3144 {
3145 return -1;
3146 }
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003147 }
3148 else
3149 {
3150 if (lcm->nsh_map_index == (u32) ~ 0)
3151 {
3152 clib_warning ("No locator-set defined for NSH!");
3153 return -1;
3154 }
3155 else
3156 {
3157 map = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index);
3158 ls_index = map->locator_set_index;
3159 }
3160 }
Florin Corase127a7e2016-02-18 22:20:01 +01003161 }
3162
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003163 /* overwrite locator set if map-request itr-rlocs configured */
3164 if (~0 != lcm->mreq_itr_rlocs)
3165 {
3166 ls_index = lcm->mreq_itr_rlocs;
3167 }
3168
3169 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003170
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003171 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003172 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003173 if (duplicate_pmr)
3174 duplicate_pmr->to_be_removed = 1;
3175 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003176 }
Florin Corasddfafb82016-05-13 18:09:56 +02003177
Florin Corase127a7e2016-02-18 22:20:01 +01003178 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003179 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02003180 &lcm->active_map_resolver,
3181 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01003182
3183 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003184 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01003185
Florin Corasf727db92016-06-23 15:01:58 +02003186 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02003187 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
Neale Rannsea93e482019-11-12 17:16:47 +00003188 next_index = (ip_addr_version (&lcm->active_map_resolver) == AF_IP4) ?
Florin Corasa2157cf2016-08-16 21:09:14 +02003189 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01003190
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003191 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003192
3193 /* Enqueue the packet */
3194 to_next = vlib_frame_vector_args (f);
3195 to_next[0] = bi;
3196 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003197 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01003198
Filip Tehlar5908e182017-10-16 06:51:11 -07003199 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
3200 LISP_CP_OUTPUT_ERROR_MAP_REQUESTS_SENT, 1);
3201
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003202 if (duplicate_pmr)
3203 /* if there is a pending request already update it */
3204 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003205 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3206 {
3207 /* remove the oldest nonce */
3208 u64 CLIB_UNUSED (tmp), *nonce_del;
3209 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3210 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3211 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3212 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003213
Florin Corasf4691cd2016-08-15 19:16:32 +02003214 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003215 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003216 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003217 }
3218 else
3219 {
3220 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02003221 pool_get (lcm->pending_map_requests_pool, pmr);
Dave Barachb7b92992018-10-17 10:38:51 -04003222 clib_memset (pmr, 0, sizeof (*pmr));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003223 gid_address_copy (&pmr->src, seid);
3224 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02003225 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003226 pmr->is_smr_invoked = is_smr_invoked;
3227 reset_pending_mr_counters (pmr);
3228 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003229 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003230 }
3231
3232 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003233}
3234
3235static void
Florin Corasa2157cf2016-08-16 21:09:14 +02003236get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01003237{
Florin Corasa2157cf2016-08-16 21:09:14 +02003238 ip4_header_t *ip4 = hdr;
3239 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01003240
3241 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3242 {
Neale Rannsea93e482019-11-12 17:16:47 +00003243 ip_address_set (src, &ip4->src_address, AF_IP4);
3244 ip_address_set (dst, &ip4->dst_address, AF_IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01003245 }
3246 else
3247 {
3248 ip6 = hdr;
Neale Rannsea93e482019-11-12 17:16:47 +00003249 ip_address_set (src, &ip6->src_address, AF_IP6);
3250 ip_address_set (dst, &ip6->dst_address, AF_IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01003251 }
3252}
3253
Filip Tehlar324112f2016-06-02 16:07:38 +02003254static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02003255lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02003256 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02003257{
Florin Corasa2157cf2016-08-16 21:09:14 +02003258 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003259 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02003260
Florin Coras87e40692016-09-22 18:02:17 +02003261 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
Neale Rannsea93e482019-11-12 17:16:47 +00003262 AF_IP4 ?
3263 FIB_PROTOCOL_IP4 :
Florin Coras87e40692016-09-22 18:02:17 +02003264 FIB_PROTOCOL_IP6),
3265 vnet_buffer
3266 (b)->sw_if_index
3267 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02003268
3269 vnip = hash_get (lcm->vni_by_table_id, table_id);
3270 if (vnip)
3271 vni = vnip[0];
3272 else
3273 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3274
3275 return vni;
3276}
3277
Florin Coras1a1adc72016-07-22 01:45:30 +02003278always_inline u32
Filip Tehlard5a65db2017-05-17 17:21:10 +02003279lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
Florin Coras1a1adc72016-07-22 01:45:30 +02003280{
Florin Coras1a1adc72016-07-22 01:45:30 +02003281 u32 sw_if_index0;
3282
Florin Corasa2157cf2016-08-16 21:09:14 +02003283 l2input_main_t *l2im = &l2input_main;
3284 l2_input_config_t *config;
3285 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02003286
Florin Corasa2157cf2016-08-16 21:09:14 +02003287 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3288 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02003289 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3290
Filip Tehlard5a65db2017-05-17 17:21:10 +02003291 return bd_config->bd_id;
3292}
3293
3294always_inline u32
3295lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3296{
3297 uword *vnip;
3298 u32 vni = ~0;
3299 u32 bd = lisp_get_bd_from_buffer_eth (b);
3300
3301 vnip = hash_get (lcm->vni_by_bd_id, bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02003302 if (vnip)
3303 vni = vnip[0];
3304 else
Filip Tehlard5a65db2017-05-17 17:21:10 +02003305 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02003306
3307 return vni;
3308}
3309
Filip Tehlar4868ff62017-03-09 16:48:39 +01003310void
Florin Corasa2157cf2016-08-16 21:09:14 +02003311get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Filip Tehlar4868ff62017-03-09 16:48:39 +01003312 gid_address_t * src, gid_address_t * dst,
3313 u16 type)
Florin Coras1a1adc72016-07-22 01:45:30 +02003314{
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003315 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02003316 u32 vni = 0;
Filip Tehlar05879992017-09-05 15:46:09 +02003317 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
Florin Coras1a1adc72016-07-22 01:45:30 +02003318
Dave Barachb7b92992018-10-17 10:38:51 -04003319 clib_memset (src, 0, sizeof (*src));
3320 clib_memset (dst, 0, sizeof (*dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02003321
Filip Tehlard5a65db2017-05-17 17:21:10 +02003322 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
3323 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
3324
Florin Coras1a1adc72016-07-22 01:45:30 +02003325 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3326 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003327 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003328 u8 version, preflen;
3329
Florin Corasa2157cf2016-08-16 21:09:14 +02003330 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3331 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02003332
3333 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02003334 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02003335
Florin Corasa2157cf2016-08-16 21:09:14 +02003336 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02003337 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02003338 gid_address_ippref_len (src) = preflen;
3339 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02003340
3341 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3342 gid_address_vni (dst) = vni;
3343 gid_address_vni (src) = vni;
3344 }
3345 else if (LISP_AFI_MAC == type)
3346 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02003347 ethernet_arp_header_t *ah;
Florin Coras1a1adc72016-07-22 01:45:30 +02003348
3349 eh = vlib_buffer_get_current (b);
3350
Filip Tehlard5a65db2017-05-17 17:21:10 +02003351 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
3352 {
3353 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
Florin Corasc1c09762018-01-30 03:21:32 -08003354 gid_address_type (dst) = GID_ADDR_ARP;
3355
Filip Tehlard5a65db2017-05-17 17:21:10 +02003356 if (clib_net_to_host_u16 (ah->opcode)
3357 != ETHERNET_ARP_OPCODE_request)
Florin Corasc1c09762018-01-30 03:21:32 -08003358 {
Dave Barachb7b92992018-10-17 10:38:51 -04003359 clib_memset (&gid_address_arp_ndp_ip (dst), 0,
3360 sizeof (ip_address_t));
Neale Rannsea93e482019-11-12 17:16:47 +00003361 ip_addr_version (&gid_address_arp_ndp_ip (dst)) = AF_IP4;
Florin Corasc1c09762018-01-30 03:21:32 -08003362 gid_address_arp_ndp_bd (dst) = ~0;
3363 return;
3364 }
Florin Coras1a1adc72016-07-22 01:45:30 +02003365
Filip Tehlard5a65db2017-05-17 17:21:10 +02003366 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
3367 clib_memcpy (&gid_address_arp_ip4 (dst),
3368 &ah->ip4_over_ethernet[1].ip4, 4);
3369 }
3370 else
3371 {
Filip Tehlar05879992017-09-05 15:46:09 +02003372 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_IP6)
3373 {
3374 ip6_header_t *ip;
3375 ip = (ip6_header_t *) (eh + 1);
3376
3377 if (IP_PROTOCOL_ICMP6 == ip->protocol)
3378 {
3379 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3380 ndh = ip6_next_header (ip);
3381 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3382 {
Florin Corasc1c09762018-01-30 03:21:32 -08003383 gid_address_type (dst) = GID_ADDR_NDP;
3384
3385 /* check that source link layer address option is present */
Filip Tehlar05879992017-09-05 15:46:09 +02003386 opt = (void *) (ndh + 1);
3387 if ((opt->header.type !=
3388 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address)
3389 || (opt->header.n_data_u64s != 1))
Florin Corasc1c09762018-01-30 03:21:32 -08003390 {
Dave Barachb7b92992018-10-17 10:38:51 -04003391 clib_memset (&gid_address_arp_ndp_ip (dst), 0,
3392 sizeof (ip_address_t));
Florin Corasc1c09762018-01-30 03:21:32 -08003393 ip_addr_version (&gid_address_arp_ndp_ip (dst)) =
Neale Rannsea93e482019-11-12 17:16:47 +00003394 AF_IP6;
Florin Corasc1c09762018-01-30 03:21:32 -08003395 gid_address_arp_ndp_bd (dst) = ~0;
3396 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
3397 return;
3398 }
Filip Tehlar05879992017-09-05 15:46:09 +02003399
Filip Tehlar05879992017-09-05 15:46:09 +02003400 gid_address_ndp_bd (dst) =
3401 lisp_get_bd_from_buffer_eth (b);
3402 ip_address_set (&gid_address_arp_ndp_ip (dst),
Neale Rannsea93e482019-11-12 17:16:47 +00003403 &ndh->target_address, AF_IP6);
Filip Tehlar05879992017-09-05 15:46:09 +02003404 return;
3405 }
3406 }
3407 }
3408
Filip Tehlard5a65db2017-05-17 17:21:10 +02003409 gid_address_type (src) = GID_ADDR_MAC;
3410 gid_address_type (dst) = GID_ADDR_MAC;
3411 mac_copy (&gid_address_mac (src), eh->src_address);
3412 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02003413
Filip Tehlard5a65db2017-05-17 17:21:10 +02003414 /* get vni */
3415 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3416
3417 gid_address_vni (dst) = vni;
3418 gid_address_vni (src) = vni;
3419 }
Florin Coras1a1adc72016-07-22 01:45:30 +02003420 }
Florin Corasce1b4c72017-01-26 14:25:34 -08003421 else if (LISP_AFI_LCAF == type)
3422 {
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003423 lisp_nsh_hdr_t *nh;
3424 eh = vlib_buffer_get_current (b);
3425
3426 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_NSH)
3427 {
3428 nh = (lisp_nsh_hdr_t *) (((u8 *) eh) + sizeof (*eh));
3429 u32 spi = clib_net_to_host_u32 (nh->spi_si << 8);
3430 u8 si = (u8) clib_net_to_host_u32 (nh->spi_si);
3431 gid_address_nsh_spi (dst) = spi;
3432 gid_address_nsh_si (dst) = si;
3433
3434 gid_address_type (dst) = GID_ADDR_NSH;
Filip Tehlar8d7a0b92017-10-18 07:10:25 -07003435 gid_address_type (src) = GID_ADDR_NSH;
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003436 }
Florin Corasce1b4c72017-01-26 14:25:34 -08003437 }
Florin Coras1a1adc72016-07-22 01:45:30 +02003438}
3439
Florin Corase127a7e2016-02-18 22:20:01 +01003440static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003441lisp_cp_lookup_inline (vlib_main_t * vm,
3442 vlib_node_runtime_t * node,
3443 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01003444{
Filip Tehlar05879992017-09-05 15:46:09 +02003445 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003446 u32 *from, *to_next, di, si;
Florin Corasa2157cf2016-08-16 21:09:14 +02003447 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar5908e182017-10-16 06:51:11 -07003448 u32 next_index;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003449 uword n_left_from, n_left_to_next;
3450 vnet_main_t *vnm = vnet_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01003451
3452 from = vlib_frame_vector_args (from_frame);
3453 n_left_from = from_frame->n_vectors;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003454 next_index = node->cached_next_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003455
3456 while (n_left_from > 0)
3457 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02003458 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01003459
Filip Tehlard5a65db2017-05-17 17:21:10 +02003460 while (n_left_from > 0 && n_left_to_next > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003461 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02003462 u32 pi0, sw_if_index0, next0;
3463 u64 mac0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003464 vlib_buffer_t *b0;
3465 gid_address_t src, dst;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003466 ethernet_arp_header_t *arp0;
3467 ethernet_header_t *eth0;
3468 vnet_hw_interface_t *hw_if0;
Filip Tehlar05879992017-09-05 15:46:09 +02003469 ethernet_header_t *eh0;
3470 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3471 ip6_header_t *ip0;
Florin Corase127a7e2016-02-18 22:20:01 +01003472
Florin Corasa2157cf2016-08-16 21:09:14 +02003473 pi0 = from[0];
3474 from += 1;
3475 n_left_from -= 1;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003476 to_next[0] = pi0;
3477 to_next += 1;
3478 n_left_to_next -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003479
Florin Corasa2157cf2016-08-16 21:09:14 +02003480 b0 = vlib_get_buffer (vm, pi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003481
Florin Corasa2157cf2016-08-16 21:09:14 +02003482 /* src/dst eid pair */
Filip Tehlar4868ff62017-03-09 16:48:39 +01003483 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
Filip Tehlar324112f2016-06-02 16:07:38 +02003484
Filip Tehlard5a65db2017-05-17 17:21:10 +02003485 if (gid_address_type (&dst) == GID_ADDR_ARP)
3486 {
3487 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
Filip Tehlar05879992017-09-05 15:46:09 +02003488 if (GID_LOOKUP_MISS_L2 == mac0)
3489 goto drop;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003490
Filip Tehlar05879992017-09-05 15:46:09 +02003491 /* send ARP reply */
3492 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3493 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003494
Filip Tehlar05879992017-09-05 15:46:09 +02003495 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003496
Filip Tehlar05879992017-09-05 15:46:09 +02003497 eth0 = vlib_buffer_get_current (b0);
3498 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
3499 + sizeof (*eth0));
3500 arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
3501 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
Neale Ranns37029302018-08-10 05:30:06 -07003502 mac_address_from_u64 (&arp0->ip4_over_ethernet[0].mac, mac0);
Filip Tehlar05879992017-09-05 15:46:09 +02003503 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
3504 &gid_address_arp_ip4 (&dst), 4);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003505
Filip Tehlar05879992017-09-05 15:46:09 +02003506 /* Hardware must be ethernet-like. */
3507 ASSERT (vec_len (hw_if0->hw_address) == 6);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003508
Filip Tehlar05879992017-09-05 15:46:09 +02003509 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
3510 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003511
Filip Tehlar05879992017-09-05 15:46:09 +02003512 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
3513 next0 = LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX;
3514 goto enqueue;
3515 }
3516 else if (gid_address_type (&dst) == GID_ADDR_NDP)
3517 {
3518 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3519 if (GID_LOOKUP_MISS_L2 == mac0)
3520 goto drop;
3521
3522 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3523 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
3524
3525 eh0 = vlib_buffer_get_current (b0);
3526 ip0 = (ip6_header_t *) (eh0 + 1);
3527 ndh = ip6_next_header (ip0);
3528 int bogus_length;
3529 ip0->dst_address = ip0->src_address;
3530 ip0->src_address = ndh->target_address;
3531 ip0->hop_limit = 255;
3532 opt = (void *) (ndh + 1);
3533 opt->header.type =
3534 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3535 clib_memcpy (opt->ethernet_address, (u8 *) & mac0, 6);
3536 ndh->icmp.type = ICMP6_neighbor_advertisement;
3537 ndh->advertisement_flags = clib_host_to_net_u32
3538 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3539 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3540 ndh->icmp.checksum = 0;
3541 ndh->icmp.checksum =
3542 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
3543 &bogus_length);
3544 clib_memcpy (eh0->dst_address, eh0->src_address, 6);
3545 clib_memcpy (eh0->src_address, (u8 *) & mac0, 6);
3546 b0->error =
3547 node->errors
3548 [LISP_CP_LOOKUP_ERROR_NDP_NEIGHBOR_ADVERTISEMENT_TX];
3549 next0 = LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX;
3550 goto enqueue;
Filip Tehlard5a65db2017-05-17 17:21:10 +02003551 }
3552
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07003553 /* if we have remote mapping for destination already in map-cache
Florin Corasa2157cf2016-08-16 21:09:14 +02003554 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02003555 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3556 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02003557 if (~0 != di)
3558 {
3559 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3560 /* send a map-request also in case of negative mapping entry
3561 with corresponding action */
3562 if (m->action == LISP_SEND_MAP_REQUEST)
3563 {
3564 /* send map-request */
3565 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3566 0 /* is_resend */ );
Florin Corasa2157cf2016-08-16 21:09:14 +02003567 }
3568 else
3569 {
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02003570 if (GID_ADDR_NSH != gid_address_type (&dst))
3571 {
3572 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3573 &src);
3574 }
3575 else
3576 si = lcm->nsh_map_index;
3577
Florin Corasa2157cf2016-08-16 21:09:14 +02003578 if (~0 != si)
3579 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01003580 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02003581 }
3582 }
3583 }
3584 else
3585 {
3586 /* send map-request */
3587 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3588 0 /* is_resend */ );
Florin Corasa2157cf2016-08-16 21:09:14 +02003589 }
Florin Corase127a7e2016-02-18 22:20:01 +01003590
Filip Tehlar05879992017-09-05 15:46:09 +02003591 drop:
Filip Tehlard5a65db2017-05-17 17:21:10 +02003592 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Filip Tehlar05879992017-09-05 15:46:09 +02003593 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3594 enqueue:
Florin Corasa2157cf2016-08-16 21:09:14 +02003595 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3596 {
3597 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3598 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003599
Dave Barachb7b92992018-10-17 10:38:51 -04003600 clib_memset (tr, 0, sizeof (*tr));
Florin Corasa2157cf2016-08-16 21:09:14 +02003601 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02003602 ip_address_copy (&tr->map_resolver_ip,
3603 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02003604 }
3605 gid_address_free (&dst);
3606 gid_address_free (&src);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003607 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3608 to_next,
3609 n_left_to_next, pi0, next0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003610 }
Florin Corase127a7e2016-02-18 22:20:01 +01003611
Filip Tehlard5a65db2017-05-17 17:21:10 +02003612 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01003613 }
Florin Corase127a7e2016-02-18 22:20:01 +01003614 return from_frame->n_vectors;
3615}
3616
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003617static uword
3618lisp_cp_lookup_ip4 (vlib_main_t * vm,
3619 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3620{
3621 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3622}
3623
3624static uword
3625lisp_cp_lookup_ip6 (vlib_main_t * vm,
3626 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3627{
3628 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3629}
3630
Neale Ranns5e575b12016-10-03 09:40:25 +01003631static uword
3632lisp_cp_lookup_l2 (vlib_main_t * vm,
3633 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3634{
3635 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3636}
3637
Florin Corasce1b4c72017-01-26 14:25:34 -08003638static uword
3639lisp_cp_lookup_nsh (vlib_main_t * vm,
3640 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3641{
3642 /* TODO decide if NSH should be propagated as LCAF or not */
3643 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3644}
3645
Florin Corasa2157cf2016-08-16 21:09:14 +02003646/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003647VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3648 .function = lisp_cp_lookup_ip4,
3649 .name = "lisp-cp-lookup-ip4",
3650 .vector_size = sizeof (u32),
3651 .format_trace = format_lisp_cp_lookup_trace,
3652 .type = VLIB_NODE_TYPE_INTERNAL,
3653
3654 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3655 .error_strings = lisp_cp_lookup_error_strings,
3656
3657 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3658
3659 .next_nodes = {
3660 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlar05879992017-09-05 15:46:09 +02003661 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003662 },
3663};
3664/* *INDENT-ON* */
3665
3666/* *INDENT-OFF* */
3667VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3668 .function = lisp_cp_lookup_ip6,
3669 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01003670 .vector_size = sizeof (u32),
3671 .format_trace = format_lisp_cp_lookup_trace,
3672 .type = VLIB_NODE_TYPE_INTERNAL,
3673
3674 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3675 .error_strings = lisp_cp_lookup_error_strings,
3676
3677 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3678
3679 .next_nodes = {
3680 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlar05879992017-09-05 15:46:09 +02003681 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
Neale Ranns5e575b12016-10-03 09:40:25 +01003682 },
3683};
3684/* *INDENT-ON* */
3685
3686/* *INDENT-OFF* */
3687VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3688 .function = lisp_cp_lookup_l2,
3689 .name = "lisp-cp-lookup-l2",
3690 .vector_size = sizeof (u32),
3691 .format_trace = format_lisp_cp_lookup_trace,
3692 .type = VLIB_NODE_TYPE_INTERNAL,
3693
3694 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3695 .error_strings = lisp_cp_lookup_error_strings,
3696
3697 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3698
3699 .next_nodes = {
3700 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlar05879992017-09-05 15:46:09 +02003701 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
Florin Corase127a7e2016-02-18 22:20:01 +01003702 },
3703};
Florin Corasa2157cf2016-08-16 21:09:14 +02003704/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003705
Florin Corasce1b4c72017-01-26 14:25:34 -08003706/* *INDENT-OFF* */
3707VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3708 .function = lisp_cp_lookup_nsh,
3709 .name = "lisp-cp-lookup-nsh",
3710 .vector_size = sizeof (u32),
3711 .format_trace = format_lisp_cp_lookup_trace,
3712 .type = VLIB_NODE_TYPE_INTERNAL,
3713
3714 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3715 .error_strings = lisp_cp_lookup_error_strings,
3716
3717 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3718
3719 .next_nodes = {
3720 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlar05879992017-09-05 15:46:09 +02003721 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
Florin Corasce1b4c72017-01-26 14:25:34 -08003722 },
3723};
3724/* *INDENT-ON* */
3725
Florin Corase127a7e2016-02-18 22:20:01 +01003726/* lisp_cp_input statistics */
Filip Tehlarcda70662017-01-16 10:30:03 +01003727#define foreach_lisp_cp_input_error \
3728_(DROP, "drop") \
3729_(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3730_(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3731_(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
Florin Corase127a7e2016-02-18 22:20:01 +01003732_(MAP_REPLIES_RECEIVED, "map-replies received")
3733
Florin Corasa2157cf2016-08-16 21:09:14 +02003734static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003735#define _(sym,string) string,
3736 foreach_lisp_cp_input_error
3737#undef _
3738};
3739
3740typedef enum
3741{
3742#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003743 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01003744#undef _
3745 LISP_CP_INPUT_N_ERROR,
3746} lisp_cp_input_error_t;
3747
Florin Corase127a7e2016-02-18 22:20:01 +01003748typedef struct
3749{
3750 gid_address_t dst_eid;
3751 ip4_address_t map_resolver_ip;
3752} lisp_cp_input_trace_t;
3753
3754u8 *
3755format_lisp_cp_input_trace (u8 * s, va_list * args)
3756{
3757 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3758 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003759 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3760 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003761
3762 s = format (s, "LISP-CP-INPUT: TODO");
3763 return s;
3764}
3765
Filip Tehlar9677a942016-11-28 10:23:31 +01003766static void
3767remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3768{
3769 mapping_t *m;
Florin Corasf3dc11a2017-01-24 03:13:43 -08003770 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
Dave Barachb7b92992018-10-17 10:38:51 -04003771 clib_memset (adj_args, 0, sizeof (adj_args[0]));
Filip Tehlar9677a942016-11-28 10:23:31 +01003772
3773 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasf3dc11a2017-01-24 03:13:43 -08003774
3775 gid_address_copy (&adj_args->reid, &m->eid);
3776 adj_args->is_add = 0;
3777 if (vnet_lisp_add_del_adjacency (adj_args))
3778 clib_warning ("failed to del adjacency!");
3779
Filip Tehlar809bc742017-08-14 19:15:36 +02003780 vnet_lisp_del_mapping (&m->eid, NULL);
Filip Tehlar9677a942016-11-28 10:23:31 +01003781 mapping_delete_timer (lcm, mi);
3782}
3783
3784static void
3785mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3786 f64 expiration_time)
3787{
3788 mapping_t *m;
3789 u64 now = clib_cpu_time_now ();
3790 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3791 u64 exp_clock_time = now + expiration_time * cpu_cps;
3792
3793 m = pool_elt_at_index (lcm->mapping_pool, mi);
3794
3795 m->timer_set = 1;
3796 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3797}
3798
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003799static void
Filip Tehlar809bc742017-08-14 19:15:36 +02003800process_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3801{
3802 int rv;
3803 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
3804 mapping_t *m = pool_elt_at_index (lcm->mapping_pool, mi);
3805 uword *fei;
3806 fwd_entry_t *fe;
3807 vlib_counter_t c;
3808 u8 have_stats = 0;
3809
3810 if (m->delete_after_expiration)
3811 {
3812 remove_expired_mapping (lcm, mi);
3813 return;
3814 }
3815
3816 fei = hash_get (lcm->fwd_entry_by_mapping_index, mi);
3817 if (!fei)
3818 return;
3819
3820 fe = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
3821
Dave Barachb7b92992018-10-17 10:38:51 -04003822 clib_memset (a, 0, sizeof (*a));
Filip Tehlar809bc742017-08-14 19:15:36 +02003823 a->rmt_eid = fe->reid;
3824 if (fe->is_src_dst)
3825 a->lcl_eid = fe->leid;
3826 a->vni = gid_address_vni (&fe->reid);
3827
3828 rv = vnet_lisp_gpe_get_fwd_stats (a, &c);
3829 if (0 == rv)
3830 have_stats = 1;
3831
3832 if (m->almost_expired)
3833 {
3834 m->almost_expired = 0; /* reset flag */
3835 if (have_stats)
3836 {
3837 if (m->packets != c.packets)
3838 {
3839 /* mapping is in use, re-fetch */
3840 map_request_args_t mr_args;
Dave Barachb7b92992018-10-17 10:38:51 -04003841 clib_memset (&mr_args, 0, sizeof (mr_args));
Filip Tehlar809bc742017-08-14 19:15:36 +02003842 mr_args.seid = fe->leid;
3843 mr_args.deid = fe->reid;
3844
3845 send_map_request_thread_fn (&mr_args);
3846 }
3847 else
3848 remove_expired_mapping (lcm, mi);
3849 }
3850 else
3851 remove_expired_mapping (lcm, mi);
3852 }
3853 else
3854 {
3855 m->almost_expired = 1;
3856 mapping_start_expiration_timer (lcm, mi, TIME_UNTIL_REFETCH_OR_DELETE);
3857
3858 if (have_stats)
3859 /* save counter */
3860 m->packets = c.packets;
3861 else
3862 m->delete_after_expiration = 1;
3863 }
3864}
3865
3866static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003867map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01003868{
Florin Corasacd4c632017-06-15 14:33:48 -07003869 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003870 mapping_t *m;
3871 vec_foreach (m, a->mappings)
3872 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003873 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003874 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003875 }
Florin Corasacd4c632017-06-15 14:33:48 -07003876 pool_put (lcm->map_records_args_pool[vlib_get_thread_index ()], a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003877}
3878
3879void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003880process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003881{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003882 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003883 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3884 u32 dst_map_index = 0;
3885 pending_map_request_t *pmr;
3886 u64 *noncep;
3887 uword *pmr_index;
Filip Tehlar809bc742017-08-14 19:15:36 +02003888 u8 is_changed = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003889
3890 if (a->is_rloc_probe)
3891 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003892
Florin Corase127a7e2016-02-18 22:20:01 +01003893 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003894 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01003895 if (!pmr_index)
3896 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003897 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003898 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003899 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003900 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01003901
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003902 vec_foreach (m, a->mappings)
3903 {
Filip Tehlar809bc742017-08-14 19:15:36 +02003904 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
Dave Barachb7b92992018-10-17 10:38:51 -04003905 clib_memset (m_args, 0, sizeof (m_args[0]));
Filip Tehlar809bc742017-08-14 19:15:36 +02003906 gid_address_copy (&m_args->eid, &m->eid);
3907 m_args->action = m->action;
3908 m_args->authoritative = m->authoritative;
3909 m_args->ttl = m->ttl;
3910 m_args->is_static = 0;
3911
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003912 /* insert/update mappings cache */
Filip Tehlar809bc742017-08-14 19:15:36 +02003913 vnet_lisp_add_mapping (m_args, m->locators, &dst_map_index, &is_changed);
Florin Corase127a7e2016-02-18 22:20:01 +01003914
Florin Corasba888e42017-01-24 11:38:18 -08003915 if (dst_map_index == (u32) ~ 0)
3916 continue;
3917
Filip Tehlar809bc742017-08-14 19:15:36 +02003918 if (is_changed)
3919 {
3920 /* try to program forwarding only if mapping saved or updated */
3921 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
Dave Barachb7b92992018-10-17 10:38:51 -04003922 clib_memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08003923
Filip Tehlar809bc742017-08-14 19:15:36 +02003924 gid_address_copy (&adj_args->leid, &pmr->src);
3925 gid_address_copy (&adj_args->reid, &m->eid);
3926 adj_args->is_add = 1;
3927
3928 if (vnet_lisp_add_del_adjacency (adj_args))
3929 clib_warning ("failed to add adjacency!");
3930 }
Florin Corasf3dc11a2017-01-24 03:13:43 -08003931
Florin Corasba888e42017-01-24 11:38:18 -08003932 if ((u32) ~ 0 != m->ttl)
Filip Tehlar0a62e5a2017-11-02 01:38:49 -07003933 mapping_start_expiration_timer (lcm, dst_map_index,
3934 (m->ttl == 0) ? 0 : MAPPING_TIMEOUT);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003935 }
Florin Corase127a7e2016-02-18 22:20:01 +01003936
3937 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02003938
3939 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02003940 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003941 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02003942 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003943 /* *INDENT-ON* */
3944
3945 clib_fifo_free (pmr->nonces);
3946 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003947
3948done:
Florin Corasacd4c632017-06-15 14:33:48 -07003949 a->is_free = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003950 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003951}
3952
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003953static int
3954is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3955 lisp_key_type_t key_id, u8 * key)
3956{
Filip Tehlarc6c43942019-03-21 05:56:59 -07003957 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003958 u8 *auth_data = 0;
3959 u16 auth_data_len;
3960 int result;
Filip Tehlarc6c43942019-03-21 05:56:59 -07003961 vnet_crypto_op_t _op, *op = &_op;
Damjan Mariond1bed682019-04-24 15:20:35 +02003962 vnet_crypto_key_index_t ki;
Filip Tehlarc6c43942019-03-21 05:56:59 -07003963 u8 out[EVP_MAX_MD_SIZE] = { 0, };
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003964
3965 auth_data_len = auth_data_len_by_key_id (key_id);
3966 if ((u16) ~ 0 == auth_data_len)
3967 {
3968 clib_warning ("invalid length for key_id %d!", key_id);
3969 return 0;
3970 }
3971
3972 /* save auth data */
3973 vec_validate (auth_data, auth_data_len - 1);
3974 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3975
3976 /* clear auth data */
Dave Barachb7b92992018-10-17 10:38:51 -04003977 clib_memset (MNOTIFY_DATA (h), 0, auth_data_len);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003978
Filip Tehlarc6c43942019-03-21 05:56:59 -07003979 vnet_crypto_op_init (op, lisp_key_type_to_crypto_op (key_id));
Filip Tehlarc6c43942019-03-21 05:56:59 -07003980 op->len = msg_len;
Damjan Marion060bfb92019-03-29 13:47:54 +01003981 op->digest = out;
Filip Tehlarc6c43942019-03-21 05:56:59 -07003982 op->src = (u8 *) h;
Damjan Marion060bfb92019-03-29 13:47:54 +01003983 op->digest_len = 0;
Filip Tehlarc6c43942019-03-21 05:56:59 -07003984 op->iv = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003985
Damjan Mariond1bed682019-04-24 15:20:35 +02003986 ki = vnet_crypto_key_add (lcm->vlib_main,
3987 lisp_key_type_to_crypto_alg (key_id), key,
3988 vec_len (key));
3989
3990 op->key_index = ki;
3991
Filip Tehlarc6c43942019-03-21 05:56:59 -07003992 vnet_crypto_process_ops (lcm->vlib_main, op, 1);
Damjan Mariond1bed682019-04-24 15:20:35 +02003993 vnet_crypto_key_del (lcm->vlib_main, ki);
Filip Tehlarc6c43942019-03-21 05:56:59 -07003994
3995 result = memcmp (out, auth_data, auth_data_len);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003996
3997 vec_free (auth_data);
3998
3999 return !result;
4000}
4001
4002static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004003process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004004{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004005 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004006 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004007
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004008 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004009 if (!pmr_index)
4010 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004011 clib_warning ("No pending map-register entry with nonce %lu!",
4012 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004013 return;
4014 }
4015
Florin Corasacd4c632017-06-15 14:33:48 -07004016 a->is_free = 1;
Filip Tehlar7048ff12017-07-27 08:09:14 +02004017 pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004018 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar7048ff12017-07-27 08:09:14 +02004019
4020 /* reset map-notify counter */
4021 lcm->expired_map_registers = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004022}
4023
4024static mapping_t *
4025get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4026{
4027 u32 mi;
4028
4029 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4030 if (~0 == mi)
4031 {
4032 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4033 e);
4034 return 0;
4035 }
4036 return pool_elt_at_index (lcm->mapping_pool, mi);
4037}
4038
4039/**
4040 * When map-notify is received it is necessary that all EIDs in the record
4041 * list share common key. The key is then used to verify authentication
4042 * data in map-notify message.
4043 */
4044static int
4045map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4046 u32 key_id, u8 ** key_out)
4047{
4048 u32 i, len = vec_len (maps);
4049 mapping_t *m;
4050
4051 /* get key of the first mapping */
4052 m = get_mapping (lcm, &maps[0].eid);
4053 if (!m || !m->key)
4054 return -1;
4055
4056 key_out[0] = m->key;
4057
4058 for (i = 1; i < len; i++)
4059 {
4060 m = get_mapping (lcm, &maps[i].eid);
4061 if (!m || !m->key)
4062 return -1;
4063
4064 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4065 {
4066 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4067 return -1;
4068 }
4069 }
4070 return 0;
4071}
4072
4073static int
4074parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4075{
4076 locator_t *locators = 0;
4077 u32 i, len;
4078 gid_address_t deid;
4079 mapping_t m;
4080 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004081
Dave Barachb7b92992018-10-17 10:38:51 -04004082 clib_memset (&m, 0, sizeof (m));
Filip Tehlar68c74fc2017-05-04 14:52:42 +02004083
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004084 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004085 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004086 {
Florin Corase68de8c2017-06-05 15:38:50 -07004087 locators = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004088 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4089 if (len == ~0)
4090 {
4091 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004092 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004093 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004094 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004095 }
4096
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004097 m.locators = locators;
4098 gid_address_copy (&m.eid, &deid);
4099 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004100 }
4101
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004102 return 0;
4103}
4104
4105static map_records_arg_t *
Florin Corasacd4c632017-06-15 14:33:48 -07004106map_record_args_get ()
4107{
4108 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4109 map_records_arg_t *rec;
4110
4111 /* Cleanup first */
4112 /* *INDENT-OFF* */
4113 pool_foreach (rec, lcm->map_records_args_pool[vlib_get_thread_index()], ({
4114 if (rec->is_free)
4115 map_records_arg_free (rec);
4116 }));
4117 /* *INDENT-ON* */
4118
4119 pool_get (lcm->map_records_args_pool[vlib_get_thread_index ()], rec);
4120 return rec;
4121}
4122
4123static map_records_arg_t *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004124parse_map_notify (vlib_buffer_t * b)
4125{
4126 int rc = 0;
4127 map_notify_hdr_t *mnotif_hdr;
4128 lisp_key_type_t key_id;
4129 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4130 u8 *key = 0;
4131 gid_address_t deid;
4132 u16 auth_data_len = 0;
4133 u8 record_count;
Florin Corasacd4c632017-06-15 14:33:48 -07004134 map_records_arg_t *a;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004135
Florin Corasacd4c632017-06-15 14:33:48 -07004136 a = map_record_args_get ();
Dave Barachb7b92992018-10-17 10:38:51 -04004137 clib_memset (a, 0, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004138 mnotif_hdr = vlib_buffer_get_current (b);
4139 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
Dave Barachb7b92992018-10-17 10:38:51 -04004140 clib_memset (&deid, 0, sizeof (deid));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004141
4142 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4143 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4144 auth_data_len = auth_data_len_by_key_id (key_id);
4145
4146 /* advance buffer by authentication data */
4147 vlib_buffer_pull (b, auth_data_len);
4148
4149 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4150 rc = parse_map_records (b, a, record_count);
4151 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004152 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004153 map_records_arg_free (a);
4154 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004155 }
4156
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004157 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4158 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004159 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004160 map_records_arg_free (a);
4161 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004162 }
4163
4164 /* verify authentication data */
4165 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004166 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004167 {
Filip Tehlar7048ff12017-07-27 08:09:14 +02004168 clib_warning ("Map-notify auth data verification failed for nonce "
4169 "0x%lx!", a->nonce);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004170 map_records_arg_free (a);
4171 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004172 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004173 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004174}
4175
4176static vlib_buffer_t *
4177build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4178 ip_address_t * dst, u64 nonce, u8 probe_bit,
4179 mapping_t * records, u16 dst_port, u32 * bi_res)
4180{
4181 vlib_buffer_t *b;
4182 u32 bi;
4183 vlib_main_t *vm = lcm->vlib_main;
4184
4185 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4186 {
4187 clib_warning ("Can't allocate buffer for Map-Register!");
4188 return 0;
4189 }
4190
4191 b = vlib_get_buffer (vm, bi);
4192
4193 /* leave some space for the encap headers */
4194 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4195
4196 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4197
4198 /* push outer ip header */
Florin Corasfdbc3822017-07-27 00:34:12 -07004199 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst, 1);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004200
4201 bi_res[0] = bi;
4202 return b;
4203}
4204
4205static int
4206send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4207 u8 probe_bit, u64 nonce, u16 dst_port,
4208 ip_address_t * probed_loc)
4209{
4210 ip_address_t src;
4211 u32 bi;
4212 vlib_buffer_t *b;
4213 vlib_frame_t *f;
4214 u32 next_index, *to_next;
4215 mapping_t *records = 0, *m;
4216
4217 m = pool_elt_at_index (lcm->mapping_pool, mi);
4218 if (!m)
4219 return -1;
4220
4221 vec_add1 (records, m[0]);
4222 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
Dave Barachb7b92992018-10-17 10:38:51 -04004223 clib_memset (&src, 0, sizeof (src));
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004224
4225 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4226 {
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07004227 clib_warning ("can't find interface address for %U", format_ip_address,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004228 dst);
4229 return -1;
4230 }
4231
4232 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4233 &bi);
4234 if (!b)
4235 return -1;
4236 free_map_register_records (records);
4237
4238 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
Neale Rannsea93e482019-11-12 17:16:47 +00004239 next_index = (ip_addr_version (&lcm->active_map_resolver) == AF_IP4) ?
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004240 ip4_lookup_node.index : ip6_lookup_node.index;
4241
4242 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4243
4244 /* Enqueue the packet */
4245 to_next = vlib_frame_vector_args (f);
4246 to_next[0] = bi;
4247 f->n_vectors = 1;
4248 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4249 return 0;
4250}
4251
Filip Tehlarb601f222017-01-02 10:22:56 +01004252static void
4253find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
4254{
Damjan Marion072401e2017-07-13 18:53:27 +02004255 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Filip Tehlarb601f222017-01-02 10:22:56 +01004256 if (start < 0 && start < -sizeof (b->pre_data))
4257 {
4258 *ip_hdr = 0;
4259 return;
4260 }
4261
4262 *ip_hdr = b->data + start;
4263 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
4264 *ip_hdr = 0;
4265}
4266
Florin Corase127a7e2016-02-18 22:20:01 +01004267void
Filip Tehlarcda70662017-01-16 10:30:03 +01004268process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
4269 lisp_cp_main_t * lcm, vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01004270{
Filip Tehlarb601f222017-01-02 10:22:56 +01004271 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004272 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4273 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02004274 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01004275 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004276 u64 nonce;
Filip Tehlarcda70662017-01-16 10:30:03 +01004277 u32 i, len = 0, rloc_probe_recv = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004278 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004279
4280 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004281 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02004282 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004283 clib_warning
4284 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01004285 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02004286 }
Florin Corase127a7e2016-02-18 22:20:01 +01004287
Filip Tehlarb601f222017-01-02 10:22:56 +01004288 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4289 nonce = MREQ_NONCE (mreq_hdr);
4290
Florin Corase127a7e2016-02-18 22:20:01 +01004291 /* parse src eid */
4292 len = lisp_msg_parse_addr (b, &src);
4293 if (len == ~0)
4294 return;
4295
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004296 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4297 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01004298 if (len == ~0)
Filip Tehlarcda70662017-01-16 10:30:03 +01004299 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01004300
4301 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02004302 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01004303 {
Dave Barachb7b92992018-10-17 10:38:51 -04004304 clib_memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01004305 len = lisp_msg_parse_eid_rec (b, &dst);
4306 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004307 {
4308 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004309 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02004310 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004311
4312 if (MREQ_SMR (mreq_hdr))
4313 {
4314 /* send SMR-invoked map-requests */
4315 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4316 }
4317 else if (MREQ_RLOC_PROBE (mreq_hdr))
4318 {
Filip Tehlarb601f222017-01-02 10:22:56 +01004319 find_ip_header (b, &ip_hdr);
4320 if (!ip_hdr)
4321 {
4322 clib_warning ("Cannot find the IP header!");
Filip Tehlarcda70662017-01-16 10:30:03 +01004323 goto done;
Filip Tehlarb601f222017-01-02 10:22:56 +01004324 }
Filip Tehlarcda70662017-01-16 10:30:03 +01004325 rloc_probe_recv++;
Dave Barachb7b92992018-10-17 10:38:51 -04004326 clib_memset (&m, 0, sizeof (m));
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004327 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4328
4329 // TODO: select best locator; for now use the first one
4330 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4331
4332 /* get src/dst IP addresses */
4333 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4334
4335 // TODO get source port from buffer
4336 u16 src_port = LISP_CONTROL_PORT;
4337
4338 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4339 src_port, &probed_loc);
4340 }
Florin Corase127a7e2016-02-18 22:20:01 +01004341 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004342
4343done:
Filip Tehlarcda70662017-01-16 10:30:03 +01004344 vlib_node_increment_counter (vm, node->node_index,
4345 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
4346 rloc_probe_recv);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004347 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01004348}
4349
Filip Tehlar816f4372017-04-26 16:09:06 +02004350map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004351parse_map_reply (vlib_buffer_t * b)
4352{
4353 locator_t probed;
4354 gid_address_t deid;
4355 void *h;
4356 u32 i, len = 0;
4357 mapping_t m;
4358 map_reply_hdr_t *mrep_hdr;
Florin Corasacd4c632017-06-15 14:33:48 -07004359 map_records_arg_t *a;
4360
4361 a = map_record_args_get ();
Dave Barachb7b92992018-10-17 10:38:51 -04004362 clib_memset (a, 0, sizeof (*a));
Florin Corasacd4c632017-06-15 14:33:48 -07004363
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004364 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004365
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004366 mrep_hdr = vlib_buffer_get_current (b);
4367 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004368 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlar816f4372017-04-26 16:09:06 +02004369 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
4370 {
Florin Coras0bcb9312019-06-24 19:33:40 -07004371 map_records_arg_free (a);
Filip Tehlar816f4372017-04-26 16:09:06 +02004372 return 0;
4373 }
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004374 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4375
4376 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4377 {
Dave Barachb7b92992018-10-17 10:38:51 -04004378 clib_memset (&m, 0, sizeof (m));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004379 locators = 0;
4380 h = vlib_buffer_get_current (b);
4381
4382 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4383 m.action = MAP_REC_ACTION (h);
4384 m.authoritative = MAP_REC_AUTH (h);
4385
4386 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4387 if (len == ~0)
4388 {
4389 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004390 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004391 return 0;
4392 }
4393
4394 m.locators = locators;
4395 gid_address_copy (&m.eid, &deid);
4396 vec_add1 (a->mappings, m);
4397 }
4398 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004399}
4400
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004401static void
4402queue_map_reply_for_processing (map_records_arg_t * a)
4403{
Florin Coras655fcc42017-01-10 08:57:54 -08004404 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004405}
4406
4407static void
4408queue_map_notify_for_processing (map_records_arg_t * a)
4409{
4410 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4411}
4412
Florin Corase127a7e2016-02-18 22:20:01 +01004413static uword
4414lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02004415 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01004416{
Filip Tehlarcda70662017-01-16 10:30:03 +01004417 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
4418 map_notifies_recv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004419 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02004420 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004421 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01004422
4423 from = vlib_frame_vector_args (from_frame);
4424 n_left_from = from_frame->n_vectors;
4425
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004426
Florin Corase127a7e2016-02-18 22:20:01 +01004427 while (n_left_from > 0)
4428 {
4429 u32 n_left_to_next_drop;
4430
4431 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02004432 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004433 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004434 {
4435 u32 bi0;
4436 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01004437
Florin Corasa2157cf2016-08-16 21:09:14 +02004438 bi0 = from[0];
4439 from += 1;
4440 n_left_from -= 1;
4441 to_next_drop[0] = bi0;
4442 to_next_drop += 1;
4443 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01004444
Florin Corasa2157cf2016-08-16 21:09:14 +02004445 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01004446
Florin Corasa2157cf2016-08-16 21:09:14 +02004447 type = lisp_msg_type (vlib_buffer_get_current (b0));
4448 switch (type)
4449 {
4450 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004451 a = parse_map_reply (b0);
4452 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01004453 {
4454 if (a->is_rloc_probe)
4455 rloc_probe_rep_recv++;
4456 queue_map_reply_for_processing (a);
4457 }
Florin Corasa2157cf2016-08-16 21:09:14 +02004458 break;
4459 case LISP_MAP_REQUEST:
Filip Tehlarcda70662017-01-16 10:30:03 +01004460 process_map_request (vm, node, lcm, b0);
Florin Corasa2157cf2016-08-16 21:09:14 +02004461 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004462 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004463 a = parse_map_notify (b0);
4464 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01004465 {
4466 map_notifies_recv++;
4467 queue_map_notify_for_processing (a);
4468 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004469 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02004470 default:
4471 clib_warning ("Unsupported LISP message type %d", type);
4472 break;
4473 }
Florin Corase127a7e2016-02-18 22:20:01 +01004474
Florin Corasa2157cf2016-08-16 21:09:14 +02004475 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01004476
Florin Corasa2157cf2016-08-16 21:09:14 +02004477 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4478 {
Florin Corase127a7e2016-02-18 22:20:01 +01004479
Florin Corasa2157cf2016-08-16 21:09:14 +02004480 }
4481 }
Florin Corase127a7e2016-02-18 22:20:01 +01004482
Florin Corasa2157cf2016-08-16 21:09:14 +02004483 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4484 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004485 }
Filip Tehlarcda70662017-01-16 10:30:03 +01004486 vlib_node_increment_counter (vm, node->node_index,
4487 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
4488 rloc_probe_rep_recv);
4489 vlib_node_increment_counter (vm, node->node_index,
4490 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
4491 map_notifies_recv);
Florin Corase127a7e2016-02-18 22:20:01 +01004492 return from_frame->n_vectors;
4493}
4494
Florin Corasa2157cf2016-08-16 21:09:14 +02004495/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01004496VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4497 .function = lisp_cp_input,
4498 .name = "lisp-cp-input",
4499 .vector_size = sizeof (u32),
4500 .format_trace = format_lisp_cp_input_trace,
4501 .type = VLIB_NODE_TYPE_INTERNAL,
4502
4503 .n_errors = LISP_CP_INPUT_N_ERROR,
4504 .error_strings = lisp_cp_input_error_strings,
4505
4506 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4507
4508 .next_nodes = {
4509 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4510 },
4511};
Florin Corasa2157cf2016-08-16 21:09:14 +02004512/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004513
4514clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02004515lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01004516{
Florin Corasa2157cf2016-08-16 21:09:14 +02004517 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4518 clib_error_t *error = 0;
Florin Corasacd4c632017-06-15 14:33:48 -07004519 vlib_thread_main_t *vtm = vlib_get_thread_main ();
4520 u32 num_threads;
Florin Corase127a7e2016-02-18 22:20:01 +01004521
4522 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4523 return error;
4524
4525 lcm->im4 = &ip4_main;
4526 lcm->im6 = &ip6_main;
4527 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02004528 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02004529 lcm->mreq_itr_rlocs = ~0;
Florin Corasba888e42017-01-24 11:38:18 -08004530 lcm->flags = 0;
Filip Tehlar0a8840d2017-10-16 05:48:23 -07004531 lcm->pitr_map_index = ~0;
4532 lcm->petr_map_index = ~0;
Dave Barachb7b92992018-10-17 10:38:51 -04004533 clib_memset (&lcm->active_map_resolver, 0,
4534 sizeof (lcm->active_map_resolver));
4535 clib_memset (&lcm->active_map_server, 0, sizeof (lcm->active_map_server));
Florin Corase127a7e2016-02-18 22:20:01 +01004536
4537 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004538 lcm->do_map_resolver_election = 1;
Filip Tehlar7048ff12017-07-27 08:09:14 +02004539 lcm->do_map_server_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02004540 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01004541
Florin Corasacd4c632017-06-15 14:33:48 -07004542 num_threads = 1 /* main thread */ + vtm->n_threads;
4543 vec_validate (lcm->map_records_args_pool, num_threads - 1);
4544
Florin Coras577c3552016-04-21 00:45:40 +02004545 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02004546 hash_set (lcm->table_id_by_vni, 0, 0);
4547 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02004548
Filip Tehlar9677a942016-11-28 10:23:31 +01004549 u64 now = clib_cpu_time_now ();
4550 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02004551 lcm->nsh_map_index = ~0;
Filip Tehlar1e553a02017-08-02 12:45:07 +02004552 lcm->map_register_ttl = MAP_REGISTER_DEFAULT_TTL;
Filip Tehlar7048ff12017-07-27 08:09:14 +02004553 lcm->max_expired_map_registers = MAX_EXPIRED_MAP_REGISTERS_DEFAULT;
4554 lcm->expired_map_registers = 0;
Filip Tehlara4980b82017-09-27 14:32:02 +02004555 lcm->transport_protocol = LISP_TRANSPORT_PROTOCOL_UDP;
Filip Tehlar0a8840d2017-10-16 05:48:23 -07004556 lcm->flags |= LISP_FLAG_XTR_MODE;
Florin Corase127a7e2016-02-18 22:20:01 +01004557 return 0;
4558}
4559
Filip Tehlar4868ff62017-03-09 16:48:39 +01004560static int
4561lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
4562 lisp_api_stats_t * stat, lisp_stats_key_t * key,
4563 u32 stats_index)
4564{
Filip Tehlar21511912017-04-07 10:41:42 +02004565 vlib_counter_t v;
4566 vlib_combined_counter_main_t *cm = &lgm->counters;
Filip Tehlar4868ff62017-03-09 16:48:39 +01004567 lisp_gpe_fwd_entry_key_t fwd_key;
4568 const lisp_gpe_tunnel_t *lgt;
4569 fwd_entry_t *fe;
4570
Dave Barachb7b92992018-10-17 10:38:51 -04004571 clib_memset (stat, 0, sizeof (*stat));
4572 clib_memset (&fwd_key, 0, sizeof (fwd_key));
Filip Tehlar4868ff62017-03-09 16:48:39 +01004573
4574 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
4575 ASSERT (fe != 0);
4576
4577 gid_to_dp_address (&fe->reid, &stat->deid);
4578 gid_to_dp_address (&fe->leid, &stat->seid);
4579 stat->vni = gid_address_vni (&fe->reid);
4580
4581 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
4582 stat->loc_rloc = lgt->key->lcl;
4583 stat->rmt_rloc = lgt->key->rmt;
4584
Filip Tehlar21511912017-04-07 10:41:42 +02004585 vlib_get_combined_counter (cm, stats_index, &v);
4586 stat->counters = v;
Filip Tehlar4868ff62017-03-09 16:48:39 +01004587 return 1;
4588}
4589
4590lisp_api_stats_t *
4591vnet_lisp_get_stats (void)
4592{
4593 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
4594 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4595 lisp_api_stats_t *stats = 0, stat;
4596 lisp_stats_key_t *key;
4597 u32 index;
4598
4599 /* *INDENT-OFF* */
4600 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
4601 {
4602 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
4603 vec_add1 (stats, stat);
4604 });
4605 /* *INDENT-ON* */
4606
4607 return stats;
4608}
4609
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004610static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02004611send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004612{
Florin Corasa2157cf2016-08-16 21:09:14 +02004613 map_request_args_t *a = arg;
4614 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004615
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004616 if (a->is_resend)
4617 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4618 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004619 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004620
4621 return 0;
4622}
4623
4624static int
4625queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02004626 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004627{
4628 map_request_args_t a;
4629
4630 a.is_resend = is_resend;
4631 gid_address_copy (&a.seid, seid);
4632 gid_address_copy (&a.deid, deid);
4633 a.smr_invoked = smr_invoked;
4634
4635 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02004636 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004637 return 0;
4638}
4639
4640/**
4641 * Take an action with a pending map request depending on expiration time
4642 * and re-try counters.
4643 */
4644static void
4645update_pending_request (pending_map_request_t * r, f64 dt)
4646{
Florin Corasa2157cf2016-08-16 21:09:14 +02004647 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004648 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004649
4650 if (r->time_to_expire - dt < 0)
4651 /* it's time to decide what to do with this pending request */
4652 {
4653 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02004654 /* too many retries -> assume current map resolver is not available */
4655 {
4656 mr = get_map_resolver (&lcm->active_map_resolver);
4657 if (!mr)
4658 {
4659 clib_warning ("Map resolver %U not found - probably deleted "
4660 "by the user recently.", format_ip_address,
4661 &lcm->active_map_resolver);
4662 }
4663 else
4664 {
4665 clib_warning ("map resolver %U is unreachable, ignoring",
4666 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004667
Florin Corasa2157cf2016-08-16 21:09:14 +02004668 /* mark current map resolver unavailable so it won't be
4669 * selected next time */
4670 mr->is_down = 1;
4671 mr->last_update = vlib_time_now (lcm->vlib_main);
4672 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004673
Florin Corasa2157cf2016-08-16 21:09:14 +02004674 reset_pending_mr_counters (r);
4675 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004676
Florin Corasa2157cf2016-08-16 21:09:14 +02004677 /* try to find a next eligible map resolver and re-send */
4678 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4679 1 /* resend */ );
4680 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004681 else
Florin Corasa2157cf2016-08-16 21:09:14 +02004682 {
4683 /* try again */
4684 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4685 1 /* resend */ );
4686 r->retries_num++;
4687 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4688 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004689 }
4690 else
4691 r->time_to_expire -= dt;
4692}
4693
4694static void
4695remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4696{
Florin Corasa2157cf2016-08-16 21:09:14 +02004697 u64 *nonce;
4698 pending_map_request_t *pmr;
4699 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004700
Florin Corasa2157cf2016-08-16 21:09:14 +02004701 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004702 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004703 ({
4704 if (pmr->to_be_removed)
4705 {
4706 clib_fifo_foreach (nonce, pmr->nonces, ({
4707 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4708 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004709
Florin Corasa2157cf2016-08-16 21:09:14 +02004710 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4711 }
4712 }));
4713 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004714
4715 vec_foreach (pmr_index, to_be_removed)
Filip Tehlar7048ff12017-07-27 08:09:14 +02004716 pool_put_index (lcm->pending_map_requests_pool, pmr_index[0]);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004717
4718 vec_free (to_be_removed);
4719}
4720
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004721static void
4722update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4723{
4724 static f64 time_left = RLOC_PROBING_INTERVAL;
4725
4726 if (!lcm->is_enabled || !lcm->rloc_probing)
4727 return;
4728
4729 time_left -= dt;
4730 if (time_left <= 0)
4731 {
4732 time_left = RLOC_PROBING_INTERVAL;
4733 send_rloc_probes (lcm);
4734 }
4735}
4736
Filip Tehlar7048ff12017-07-27 08:09:14 +02004737static int
4738update_pending_map_register (pending_map_register_t * r, f64 dt, u8 * del_all)
4739{
4740 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4741 lisp_msmr_t *ms;
4742 del_all[0] = 0;
4743
4744 r->time_to_expire -= dt;
4745
4746 if (r->time_to_expire < 0)
4747 {
4748 lcm->expired_map_registers++;
4749
4750 if (lcm->expired_map_registers >= lcm->max_expired_map_registers)
4751 {
4752 ms = get_map_server (&lcm->active_map_server);
4753 if (!ms)
4754 {
4755 clib_warning ("Map server %U not found - probably deleted "
4756 "by the user recently.", format_ip_address,
4757 &lcm->active_map_server);
4758 }
4759 else
4760 {
4761 clib_warning ("map server %U is unreachable, ignoring",
4762 format_ip_address, &lcm->active_map_server);
4763
4764 /* mark current map server unavailable so it won't be
4765 * elected next time */
4766 ms->is_down = 1;
4767 ms->last_update = vlib_time_now (lcm->vlib_main);
4768 }
4769
4770 elect_map_server (lcm);
4771
4772 /* indication for deleting all pending map registers */
4773 del_all[0] = 1;
4774 lcm->expired_map_registers = 0;
4775 return 0;
4776 }
4777 else
4778 {
4779 /* delete pending map register */
4780 return 0;
4781 }
4782 }
4783 return 1;
4784}
4785
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004786static void
4787update_map_register (lisp_cp_main_t * lcm, f64 dt)
4788{
Filip Tehlar7048ff12017-07-27 08:09:14 +02004789 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004790 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4791 static u64 mreg_sent_counter = 0;
4792
Filip Tehlar7048ff12017-07-27 08:09:14 +02004793 pending_map_register_t *pmr;
4794 u8 del_all = 0;
4795
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004796 if (!lcm->is_enabled || !lcm->map_registering)
4797 return;
4798
Filip Tehlar7048ff12017-07-27 08:09:14 +02004799 /* *INDENT-OFF* */
4800 pool_foreach (pmr, lcm->pending_map_registers_pool,
4801 ({
4802 if (!update_pending_map_register (pmr, dt, &del_all))
4803 {
4804 if (del_all)
4805 break;
4806 vec_add1 (to_be_removed, pmr - lcm->pending_map_registers_pool);
4807 }
4808 }));
4809 /* *INDENT-ON* */
4810
4811 if (del_all)
4812 {
4813 /* delete all pending map register messages so they won't
4814 * trigger another map server election.. */
4815 pool_free (lcm->pending_map_registers_pool);
4816 hash_free (lcm->map_register_messages_by_nonce);
4817
4818 /* ..and trigger registration against next map server (if any) */
4819 time_left = 0;
4820 }
4821 else
4822 {
4823 vec_foreach (pmr_index, to_be_removed)
4824 pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]);
4825 }
4826
4827 vec_free (to_be_removed);
4828
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004829 time_left -= dt;
4830 if (time_left <= 0)
4831 {
4832 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4833 time_left = MAP_REGISTER_INTERVAL;
4834 else
4835 {
4836 mreg_sent_counter++;
4837 time_left = QUICK_MAP_REGISTER_INTERVAL;
4838 }
4839 send_map_register (lcm, 1 /* want map notify */ );
4840 }
4841}
4842
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004843static uword
4844send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004845 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004846{
Filip Tehlar9677a942016-11-28 10:23:31 +01004847 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004848 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004849 pending_map_request_t *pmr;
4850 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004851
4852 while (1)
4853 {
4854 vlib_process_wait_for_event_or_clock (vm, period);
4855
4856 /* currently no signals are expected - just wait for clock */
4857 (void) vlib_process_get_events (vm, 0);
4858
Florin Corasa2157cf2016-08-16 21:09:14 +02004859 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004860 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004861 ({
4862 if (!pmr->to_be_removed)
4863 update_pending_request (pmr, period);
4864 }));
4865 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004866
4867 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004868
4869 update_map_register (lcm, period);
4870 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004871
4872 u64 now = clib_cpu_time_now ();
4873
4874 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4875 if (vec_len (expired) > 0)
4876 {
4877 u32 *mi = 0;
4878 vec_foreach (mi, expired)
4879 {
Filip Tehlar809bc742017-08-14 19:15:36 +02004880 process_expired_mapping (lcm, mi[0]);
Filip Tehlar9677a942016-11-28 10:23:31 +01004881 }
4882 _vec_len (expired) = 0;
4883 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004884 }
4885
4886 /* unreachable */
4887 return 0;
4888}
4889
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004890vnet_api_error_t
4891vnet_lisp_stats_enable_disable (u8 enable)
4892{
4893 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4894
4895 if (vnet_lisp_enable_disable_status () == 0)
4896 return VNET_API_ERROR_LISP_DISABLED;
4897
Filip Tehlar4868ff62017-03-09 16:48:39 +01004898 if (enable)
4899 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4900 else
4901 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4902
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004903 return 0;
4904}
4905
4906u8
4907vnet_lisp_stats_enable_disable_state (void)
4908{
4909 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4910
4911 if (vnet_lisp_enable_disable_status () == 0)
4912 return VNET_API_ERROR_LISP_DISABLED;
4913
Filip Tehlar4868ff62017-03-09 16:48:39 +01004914 return lcm->flags & LISP_FLAG_STATS_ENABLED;
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004915}
4916
Florin Corasd37da902019-05-30 02:01:55 -07004917void
4918vnet_lisp_create_retry_process (lisp_cp_main_t * lcm)
4919{
4920 if (lcm->retry_service_index)
4921 return;
4922
4923 lcm->retry_service_index = vlib_process_create (vlib_get_main (),
4924 "lisp-retry-service",
4925 send_map_resolver_service,
4926 16 /* stack_bytes */ );
4927}
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004928
Filip Tehlara4980b82017-09-27 14:32:02 +02004929u32
4930vnet_lisp_set_transport_protocol (u8 protocol)
4931{
4932 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4933
4934 if (protocol < LISP_TRANSPORT_PROTOCOL_UDP ||
4935 protocol > LISP_TRANSPORT_PROTOCOL_API)
4936 return VNET_API_ERROR_INVALID_ARGUMENT;
4937
4938 lcm->transport_protocol = protocol;
4939 return 0;
4940}
4941
4942lisp_transport_protocol_t
4943vnet_lisp_get_transport_protocol (void)
4944{
4945 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4946 return lcm->transport_protocol;
4947}
4948
Filip Tehlar0a8840d2017-10-16 05:48:23 -07004949int
4950vnet_lisp_enable_disable_xtr_mode (u8 is_enabled)
4951{
4952 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4953 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
4954 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
4955 u8 petr_mode = lcm->flags & LISP_FLAG_PETR_MODE;
4956
4957 if (pitr_mode && is_enabled)
4958 return VNET_API_ERROR_INVALID_ARGUMENT;
4959
4960 if (is_enabled && xtr_mode)
4961 return 0;
4962 if (!is_enabled && !xtr_mode)
4963 return 0;
4964
4965 if (is_enabled)
4966 {
4967 if (!petr_mode)
4968 {
4969 lisp_cp_register_dst_port (lcm->vlib_main);
4970 }
4971 lisp_cp_enable_l2_l3_ifaces (lcm, 1 /* with_default_route */ );
4972 lcm->flags |= LISP_FLAG_XTR_MODE;
4973 }
4974 else
4975 {
4976 if (!petr_mode)
4977 {
4978 lisp_cp_unregister_dst_port (lcm->vlib_main);
4979 }
4980 lisp_cp_disable_l2_l3_ifaces (lcm);
4981 lcm->flags &= ~LISP_FLAG_XTR_MODE;
4982 }
4983 return 0;
4984}
4985
4986int
4987vnet_lisp_enable_disable_pitr_mode (u8 is_enabled)
4988{
4989 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4990 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
4991 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
4992
4993 if (xtr_mode && is_enabled)
4994 return VNET_API_ERROR_INVALID_VALUE;
4995
4996 if (is_enabled && pitr_mode)
4997 return 0;
4998 if (!is_enabled && !pitr_mode)
4999 return 0;
5000
5001 if (is_enabled)
5002 {
5003 /* create iface, no default route */
5004 lisp_cp_enable_l2_l3_ifaces (lcm, 0 /* with_default_route */ );
5005 lcm->flags |= LISP_FLAG_PITR_MODE;
5006 }
5007 else
5008 {
5009 lisp_cp_disable_l2_l3_ifaces (lcm);
5010 lcm->flags &= ~LISP_FLAG_PITR_MODE;
5011 }
5012 return 0;
5013}
5014
5015int
5016vnet_lisp_enable_disable_petr_mode (u8 is_enabled)
5017{
5018 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5019 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
5020 u8 petr_mode = lcm->flags & LISP_FLAG_PETR_MODE;
5021
5022 if (is_enabled && petr_mode)
5023 return 0;
5024 if (!is_enabled && !petr_mode)
5025 return 0;
5026
5027 if (is_enabled)
5028 {
5029 if (!xtr_mode)
5030 {
5031 lisp_cp_register_dst_port (lcm->vlib_main);
5032 }
5033 lcm->flags |= LISP_FLAG_PETR_MODE;
5034 }
5035 else
5036 {
5037 if (!xtr_mode)
5038 {
5039 lisp_cp_unregister_dst_port (lcm->vlib_main);
5040 }
5041 lcm->flags &= ~LISP_FLAG_PETR_MODE;
5042 }
5043 return 0;
5044}
5045
5046u8
5047vnet_lisp_get_xtr_mode (void)
5048{
5049 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5050 return (lcm->flags & LISP_FLAG_XTR_MODE);
5051}
5052
5053u8
5054vnet_lisp_get_pitr_mode (void)
5055{
5056 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5057 return (lcm->flags & LISP_FLAG_PITR_MODE);
5058}
5059
5060u8
5061vnet_lisp_get_petr_mode (void)
5062{
5063 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5064 return (lcm->flags & LISP_FLAG_PETR_MODE);
5065}
5066
Florin Corasa2157cf2016-08-16 21:09:14 +02005067VLIB_INIT_FUNCTION (lisp_cp_init);
5068
5069/*
5070 * fd.io coding-style-patch-verification: ON
5071 *
5072 * Local Variables:
5073 * eval: (c-set-style "gnu")
5074 * End:
5075 */