blob: cc73dfc5f0278e5487adf77557024178d3685d13 [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>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/fib/fib_entry.h>
23#include <vnet/fib/fib_table.h>
Florin Corase127a7e2016-02-18 22:20:01 +010024
Filip Tehlar397fd7d2016-10-26 14:31:24 +020025#include <openssl/evp.h>
26#include <openssl/hmac.h>
27
Florin Corasf3dc11a2017-01-24 03:13:43 -080028lisp_cp_main_t lisp_control_main;
29
Filip Tehlarcda70662017-01-16 10:30:03 +010030u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
31
32typedef enum
33{
34 LISP_CP_INPUT_NEXT_DROP,
35 LISP_CP_INPUT_N_NEXT,
36} lisp_cp_input_next_t;
37
Filip Tehlara5abdeb2016-07-18 17:35:40 +020038typedef struct
39{
40 u8 is_resend;
41 gid_address_t seid;
42 gid_address_t deid;
43 u8 smr_invoked;
44} map_request_args_t;
45
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010046typedef struct
47{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010048 u64 nonce;
Filip Tehlarfb9931f2016-12-09 13:52:38 +010049 u8 is_rloc_probe;
50 mapping_t *mappings;
51} map_records_arg_t;
52
Florin Corasdca88042016-09-14 16:01:38 +020053u8
54vnet_lisp_get_map_request_mode (void)
55{
56 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
57 return lcm->map_request_mode;
58}
59
Filip Tehlar397fd7d2016-10-26 14:31:24 +020060static u16
61auth_data_len_by_key_id (lisp_key_type_t key_id)
62{
63 switch (key_id)
64 {
65 case HMAC_SHA_1_96:
66 return SHA1_AUTH_DATA_LEN;
67 case HMAC_SHA_256_128:
68 return SHA256_AUTH_DATA_LEN;
69 default:
70 clib_warning ("unsupported key type: %d!", key_id);
71 return (u16) ~ 0;
72 }
73 return (u16) ~ 0;
74}
75
76static const EVP_MD *
77get_encrypt_fcn (lisp_key_type_t key_id)
78{
79 switch (key_id)
80 {
81 case HMAC_SHA_1_96:
82 return EVP_sha1 ();
83 case HMAC_SHA_256_128:
84 return EVP_sha256 ();
85 default:
86 clib_warning ("unsupported encryption key type: %d!", key_id);
87 break;
88 }
89 return 0;
90}
91
Filip Tehlara5abdeb2016-07-18 17:35:40 +020092static int
93queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020094 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020095
Florin Corasf727db92016-06-23 15:01:58 +020096ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020097ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
98 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +020099{
100 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +0200101 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +0200102 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
103 sw_if_index = swif->unnumbered_sw_if_index;
104 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +0200105 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
106 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
107 (u32) ~ 0;
108 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +0200109}
Filip Tehlar215104e2016-05-10 16:58:29 +0200110
Florin Corasf727db92016-06-23 15:01:58 +0200111void *
112ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200113 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200114{
Florin Corasa2157cf2016-08-16 21:09:14 +0200115 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200116
Florin Corasf727db92016-06-23 15:01:58 +0200117 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
118 if (!ia)
119 return 0;
120 return ip_interface_address_get_address (lm, ia);
121}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200122
Florin Corase127a7e2016-02-18 22:20:01 +0100123int
Florin Corasf727db92016-06-23 15:01:58 +0200124ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200125 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200126{
Florin Corasa2157cf2016-08-16 21:09:14 +0200127 ip_lookup_main_t *lm;
128 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200129
130 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
131 addr = ip_interface_get_first_address (lm, sw_if_index, version);
132 if (!addr)
133 return 0;
134
135 ip_address_set (result, addr, version);
136 return 1;
137}
138
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100139/**
140 * convert from a LISP address to a FIB prefix
141 */
142void
143ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200144{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145 if (addr->version == IP4)
146 {
147 prefix->fp_len = 32;
148 prefix->fp_proto = FIB_PROTOCOL_IP4;
149 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
150 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
151 }
Florin Corasf727db92016-06-23 15:01:58 +0200152 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 {
154 prefix->fp_len = 128;
155 prefix->fp_proto = FIB_PROTOCOL_IP6;
156 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
157 }
Florin Corasf727db92016-06-23 15:01:58 +0200158}
159
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100160/**
161 * convert from a LISP to a FIB prefix
162 */
163void
164ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
165 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200166{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100167 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
168 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200169}
170
171/**
172 * Find the sw_if_index of the interface that would be used to egress towards
173 * dst.
174 */
175u32
176ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
177{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178 fib_node_index_t fei;
179 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200180
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100181 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200182
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100183 fei = fib_table_lookup (0, &prefix);
184
185 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200186}
187
188/**
189 * Find first IP of the interface that would be used to egress towards dst.
190 * Returns 1 if the address is found 0 otherwise.
191 */
192int
193ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200194 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200195{
196 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200197 ip_lookup_main_t *lm;
198 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200199 u8 ipver;
200
Florin Corasa2157cf2016-08-16 21:09:14 +0200201 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200202
Florin Corasa2157cf2016-08-16 21:09:14 +0200203 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200204
205 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100206 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200207
Florin Corasa2157cf2016-08-16 21:09:14 +0200208 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200209 return 0;
210
211 /* find the first ip address */
212 addr = ip_interface_get_first_address (lm, si, ipver);
213 if (0 == addr)
214 return 0;
215
216 ip_address_set (result, addr, ipver);
217 return 1;
218}
219
220static int
Florin Coras1a1adc72016-07-22 01:45:30 +0200221dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +0200222{
Neale Ranns5e575b12016-10-03 09:40:25 +0100223 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200224
Florin Coras1a1adc72016-07-22 01:45:30 +0200225 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200226 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200227 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200228
229 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200230 {
231 clib_warning ("vni %d not associated to a vrf!", vni);
232 return VNET_API_ERROR_INVALID_VALUE;
233 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200234 }
235 else
236 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200237 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200238 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200239 {
240 clib_warning ("vni %d not associated to a bridge domain!", vni);
241 return VNET_API_ERROR_INVALID_VALUE;
242 }
Florin Corasf727db92016-06-23 15:01:58 +0200243 }
244
Florin Corasf727db92016-06-23 15:01:58 +0200245 /* enable/disable data-plane interface */
246 if (is_add)
247 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100248 if (is_l2)
249 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
250 else
251 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200252 }
253 else
254 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100255 if (is_l2)
256 lisp_gpe_tenant_l2_iface_unlock (vni);
257 else
258 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200259 }
260
261 return 0;
262}
263
264static void
265dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
266{
Florin Corasa2157cf2016-08-16 21:09:14 +0200267 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
268 fwd_entry_t *fe = 0;
269 uword *feip = 0;
270 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200271
Florin Corasa2157cf2016-08-16 21:09:14 +0200272 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200273 if (!feip)
274 return;
275
Florin Corasa2157cf2016-08-16 21:09:14 +0200276 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200277
278 /* delete dp fwd entry */
279 u32 sw_if_index;
280 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200281 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200282 a->vni = gid_address_vni (&fe->reid);
283 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200284 if (fe->is_src_dst)
285 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200286
287 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
288
289 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200290 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
291 vec_free (fe->locator_pairs);
292 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200293}
294
295/**
296 * Finds first remote locator with best (lowest) priority that has a local
297 * peer locator with an underlying route to it.
298 *
299 */
300static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200301get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
302 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200303{
Florin Coras3590ac52016-08-08 16:04:26 +0200304 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200305 locator_set_t *rmt_ls, *lcl_ls;
306 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
307 locator_t *lp, *rmt = 0;
308 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200309 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200310
Florin Corasa2157cf2016-08-16 21:09:14 +0200311 rmt_ls =
312 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
313 lcl_ls =
314 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200315
Florin Corasa2157cf2016-08-16 21:09:14 +0200316 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200317 return 0;
318
Florin Coras3590ac52016-08-08 16:04:26 +0200319 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200320 {
321 rmt = 0;
322
323 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200324 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
325 {
326 if (0 != hash_get (checked, i))
327 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200328
Florin Corasa2157cf2016-08-16 21:09:14 +0200329 li = vec_elt (rmt_ls->locator_indices, i);
330 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200331
Florin Corasa2157cf2016-08-16 21:09:14 +0200332 /* we don't support non-IP locators for now */
333 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
334 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200335
Florin Corasa2157cf2016-08-16 21:09:14 +0200336 if ((found && lp->priority == limitp)
337 || (!found && lp->priority >= limitp))
338 {
339 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200340
Florin Corasa2157cf2016-08-16 21:09:14 +0200341 /* don't search for locators with lower priority and don't
342 * check this locator again*/
343 limitp = lp->priority;
344 hash_set (checked, i, 1);
345 break;
346 }
347 }
Florin Corasf727db92016-06-23 15:01:58 +0200348 /* check if a local locator with a route to remote locator exists */
349 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200350 {
351 /* find egress sw_if_index for rmt locator */
352 esi =
353 ip_fib_get_egress_iface_for_dst (lcm,
354 &gid_address_ip (&rmt->address));
355 if ((u32) ~ 0 == esi)
356 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200357
Florin Corasa2157cf2016-08-16 21:09:14 +0200358 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
359 {
360 li = vec_elt (lcl_ls->locator_indices, i);
361 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200362
Florin Corasa2157cf2016-08-16 21:09:14 +0200363 /* found local locator with the needed sw_if_index */
364 if (sl->sw_if_index == esi)
365 {
366 /* and it has an address */
367 if (0 == ip_interface_get_first_ip_address (lcm,
368 sl->sw_if_index,
369 gid_address_ip_version
370 (&rmt->address),
371 lcl_addr))
372 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200373
Florin Corasa2157cf2016-08-16 21:09:14 +0200374 memset (&pair, 0, sizeof (pair));
375 ip_address_copy (&pair.rmt_loc,
376 &gid_address_ip (&rmt->address));
377 ip_address_copy (&pair.lcl_loc, lcl_addr);
378 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100379 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200380 vec_add1 (locator_pairs[0], pair);
381 found = 1;
382 }
383 }
384 }
Florin Corasf727db92016-06-23 15:01:58 +0200385 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200386 break;
Florin Corasf727db92016-06-23 15:01:58 +0200387 }
Florin Coras3590ac52016-08-08 16:04:26 +0200388
Florin Corasa2157cf2016-08-16 21:09:14 +0200389 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200390 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200391}
392
393static void
Florin Corasdca88042016-09-14 16:01:38 +0200394gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
395 fid_address_t * fid)
396{
397 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
398
399 dst[0] = src[0];
400
401 switch (fid_addr_type (fid))
402 {
403 case FID_ADDR_IP_PREF:
404 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
405 gid_address_ippref (dst) = fid_addr_ippref (fid);
406 break;
407 case FID_ADDR_MAC:
408 gid_address_type (dst) = GID_ADDR_MAC;
409 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
410 break;
411 default:
412 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
413 break;
414 }
415}
416
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200417u8
418vnet_lisp_map_register_state_get (void)
419{
420 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
421 return lcm->map_registering;
422}
423
424u8
425vnet_lisp_rloc_probe_state_get (void)
426{
427 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
428 return lcm->rloc_probing;
429}
430
Florin Corasdca88042016-09-14 16:01:38 +0200431static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200432dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200433{
Florin Corasa2157cf2016-08-16 21:09:14 +0200434 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
Florin Corasba888e42017-01-24 11:38:18 -0800435 gid_address_t *rmt_eid, *lcl_eid;
436 mapping_t *lcl_map, *rmt_map;
Florin Corasf727db92016-06-23 15:01:58 +0200437 u32 sw_if_index;
Florin Corasa2157cf2016-08-16 21:09:14 +0200438 uword *feip = 0, *dpid;
439 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200440 u8 type, is_src_dst = 0;
Florin Corasba888e42017-01-24 11:38:18 -0800441 int rv;
Florin Corasf727db92016-06-23 15:01:58 +0200442
Florin Corasa2157cf2016-08-16 21:09:14 +0200443 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200444
445 /* remove entry if it already exists */
446 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
447 if (feip)
448 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
449
Florin Corasba888e42017-01-24 11:38:18 -0800450 /*
451 * Determine local mapping and eid
452 */
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200453 if (lcm->lisp_pitr)
Florin Corasba888e42017-01-24 11:38:18 -0800454 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200455 else
Florin Corasba888e42017-01-24 11:38:18 -0800456 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
457 lcl_eid = &lcl_map->eid;
Florin Corasf727db92016-06-23 15:01:58 +0200458
Florin Corasba888e42017-01-24 11:38:18 -0800459 /*
460 * Determine remote mapping and eid
461 */
462 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
463 rmt_eid = &rmt_map->eid;
464
465 /*
466 * Build and insert data plane forwarding entry
467 */
Florin Corasf727db92016-06-23 15:01:58 +0200468 a->is_add = 1;
469
Filip Tehlard5fcc462016-10-17 16:20:18 +0200470 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200471 {
Florin Corasba888e42017-01-24 11:38:18 -0800472 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
Filip Tehlard5fcc462016-10-17 16:20:18 +0200473 {
Florin Corasba888e42017-01-24 11:38:18 -0800474 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
475 &gid_address_sd_dst (rmt_eid));
476 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
477 &gid_address_sd_src (rmt_eid));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200478 }
479 else
480 {
Florin Corasba888e42017-01-24 11:38:18 -0800481 gid_address_copy (&a->rmt_eid, rmt_eid);
482 gid_address_copy (&a->lcl_eid, lcl_eid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200483 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200484 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200485 }
486 else
Florin Corasba888e42017-01-24 11:38:18 -0800487 gid_address_copy (&a->rmt_eid, rmt_eid);
Florin Corasdca88042016-09-14 16:01:38 +0200488
Florin Corasa2157cf2016-08-16 21:09:14 +0200489 a->vni = gid_address_vni (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200490
491 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200492 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200493 if (GID_ADDR_IP_PREFIX == type)
494 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200495 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200496 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200497 {
498 clib_warning ("vni %d not associated to a vrf!", a->vni);
499 return;
500 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200501 a->table_id = dpid[0];
502 }
503 else if (GID_ADDR_MAC == type)
504 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200505 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200506 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200507 {
508 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
509 return;
510 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200511 a->bd_id = dpid[0];
512 }
513
Florin Corasf727db92016-06-23 15:01:58 +0200514 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Corasba888e42017-01-24 11:38:18 -0800515 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
516
517 /* Either rmt mapping is negative or we can't find underlay path.
518 * Try again with petr if configured */
519 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
Florin Corasf727db92016-06-23 15:01:58 +0200520 {
Florin Corasba888e42017-01-24 11:38:18 -0800521 rmt_map = lisp_get_petr_mapping (lcm);
522 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
Florin Corasf727db92016-06-23 15:01:58 +0200523 }
524
Florin Corasba888e42017-01-24 11:38:18 -0800525 /* negative entry */
526 if (rv == 0)
527 {
528 a->is_negative = 1;
529 a->action = rmt_map->action;
530 }
Florin Corasf727db92016-06-23 15:01:58 +0200531
532 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
533
534 /* add tunnel to fwd entry table XXX check return value from DP insertion */
535 pool_get (lcm->fwd_entry_pool, fe);
Florin Corasbb5c22f2016-08-02 02:31:03 +0200536 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200537 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100538
539 if (is_src_dst)
540 gid_address_copy (&fe->leid, &a->lcl_eid);
541 else
Florin Corasba888e42017-01-24 11:38:18 -0800542 gid_address_copy (&fe->leid, lcl_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100543
Filip Tehlar69a9b762016-09-23 10:00:52 +0200544 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200545 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200546 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200547}
548
Filip Tehlarce1aae42017-01-04 10:42:25 +0100549typedef struct
550{
551 u32 si;
552 u32 di;
553} fwd_entry_mt_arg_t;
554
555static void *
556dp_add_fwd_entry_thread_fn (void *arg)
557{
558 fwd_entry_mt_arg_t *a = arg;
559 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
560 dp_add_fwd_entry (lcm, a->si, a->di);
561 return 0;
562}
563
564static int
565dp_add_fwd_entry_from_mt (u32 si, u32 di)
566{
567 fwd_entry_mt_arg_t a;
568
569 memset (&a, 0, sizeof (a));
570 a.si = si;
571 a.di = di;
572
573 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
574 (u8 *) & a, sizeof (a));
575 return 0;
576}
577
Florin Corasf727db92016-06-23 15:01:58 +0200578/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200579 * Returns vector of adjacencies.
580 *
581 * The caller must free the vector returned by this function.
582 *
583 * @param vni virtual network identifier
584 * @return vector of adjacencies
585 */
586lisp_adjacency_t *
587vnet_lisp_adjacencies_get_by_vni (u32 vni)
588{
589 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
590 fwd_entry_t *fwd;
591 lisp_adjacency_t *adjs = 0, adj;
592
593 /* *INDENT-OFF* */
594 pool_foreach(fwd, lcm->fwd_entry_pool,
595 ({
596 if (gid_address_vni (&fwd->reid) != vni)
597 continue;
598
599 gid_address_copy (&adj.reid, &fwd->reid);
600 gid_address_copy (&adj.leid, &fwd->leid);
601 vec_add1 (adjs, adj);
602 }));
603 /* *INDENT-ON* */
604
605 return adjs;
606}
607
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200608static lisp_msmr_t *
609get_map_server (ip_address_t * a)
610{
611 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
612 lisp_msmr_t *m;
613
614 vec_foreach (m, lcm->map_servers)
615 {
616 if (!ip_address_cmp (&m->address, a))
617 {
618 return m;
619 }
620 }
621 return 0;
622}
623
624static lisp_msmr_t *
625get_map_resolver (ip_address_t * a)
626{
627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
628 lisp_msmr_t *m;
629
630 vec_foreach (m, lcm->map_resolvers)
631 {
632 if (!ip_address_cmp (&m->address, a))
633 {
634 return m;
635 }
636 }
637 return 0;
638}
639
640int
641vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
642{
643 u32 i;
644 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
645 lisp_msmr_t _ms, *ms = &_ms;
646
647 if (vnet_lisp_enable_disable_status () == 0)
648 {
649 clib_warning ("LISP is disabled!");
650 return VNET_API_ERROR_LISP_DISABLED;
651 }
652
653 if (is_add)
654 {
655 if (get_map_server (addr))
656 {
657 clib_warning ("map-server %U already exists!", format_ip_address,
658 addr);
659 return -1;
660 }
661
662 memset (ms, 0, sizeof (*ms));
663 ip_address_copy (&ms->address, addr);
664 vec_add1 (lcm->map_servers, ms[0]);
665 }
666 else
667 {
668 for (i = 0; i < vec_len (lcm->map_servers); i++)
669 {
670 ms = vec_elt_at_index (lcm->map_servers, i);
671 if (!ip_address_cmp (&ms->address, addr))
672 {
673 vec_del1 (lcm->map_servers, i);
674 break;
675 }
676 }
677 }
678
679 return 0;
680}
681
Filip Tehlar69a9b762016-09-23 10:00:52 +0200682/**
Florin Corasf727db92016-06-23 15:01:58 +0200683 * Add/remove mapping to/from map-cache. Overwriting not allowed.
684 */
685int
686vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200687 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100688{
Florin Corasa2157cf2016-08-16 21:09:14 +0200689 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
690 u32 mi, *map_indexp, map_index, i;
691 mapping_t *m, *old_map;
692 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100693
Florin Corasf727db92016-06-23 15:01:58 +0200694 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200695 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100696 if (a->is_add)
697 {
698 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200699 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
700 {
701 clib_warning ("eid %U found in the eid-table", format_gid_address,
702 &a->eid);
703 return VNET_API_ERROR_VALUE_EXIST;
704 }
Florin Corase127a7e2016-02-18 22:20:01 +0100705
Florin Corasa2157cf2016-08-16 21:09:14 +0200706 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200707 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100708 m->locator_set_index = a->locator_set_index;
709 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200710 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100711 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200712 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200713 m->key = vec_dup (a->key);
714 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100715
716 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200717 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200718 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100719
Florin Corasa2157cf2016-08-16 21:09:14 +0200720 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
721 {
722 clib_warning ("Locator set with index %d doesn't exist",
723 a->locator_set_index);
724 return VNET_API_ERROR_INVALID_VALUE;
725 }
Florin Corase127a7e2016-02-18 22:20:01 +0100726
727 /* add eid to list of eids supported by locator-set */
728 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200729 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
730 a->locator_set_index);
731 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100732
733 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200734 {
735 /* mark as local */
736 vec_add1 (lcm->local_mappings_indexes, map_index);
737 }
Florin Corase127a7e2016-02-18 22:20:01 +0100738 map_index_result[0] = map_index;
739 }
740 else
741 {
Florin Coras577c3552016-04-21 00:45:40 +0200742 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200743 {
744 clib_warning ("eid %U not found in the eid-table",
745 format_gid_address, &a->eid);
746 return VNET_API_ERROR_INVALID_VALUE;
747 }
Florin Corase127a7e2016-02-18 22:20:01 +0100748
749 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200750 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
751 a->locator_set_index);
752 for (i = 0; i < vec_len (eid_indexes[0]); i++)
753 {
754 map_indexp = vec_elt_at_index (eid_indexes[0], i);
755 if (map_indexp[0] == mi)
756 break;
757 }
758 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100759
760 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200761 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100762 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200763 {
764 u32 k, *lm_indexp;
765 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
766 {
767 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
768 if (lm_indexp[0] == mi)
769 break;
770 }
771 vec_del1 (lcm->local_mappings_indexes, k);
772 }
Florin Corase127a7e2016-02-18 22:20:01 +0100773
774 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200775 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200776 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100777 pool_put_index (lcm->mapping_pool, mi);
778 }
779
780 return 0;
781}
782
Florin Corasf727db92016-06-23 15:01:58 +0200783/**
784 * Add/update/delete mapping to/in/from map-cache.
785 */
Florin Coras577c3552016-04-21 00:45:40 +0200786int
787vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200788 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200789{
Florin Corasa2157cf2016-08-16 21:09:14 +0200790 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200791 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200792 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200793
Florin Corasa2157cf2016-08-16 21:09:14 +0200794 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200795
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200796 if (vnet_lisp_enable_disable_status () == 0)
797 {
798 clib_warning ("LISP is disabled!");
799 return VNET_API_ERROR_LISP_DISABLED;
800 }
801
Florin Corasa2157cf2016-08-16 21:09:14 +0200802 vni = gid_address_vni (&a->eid);
803 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200804 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200805 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200806 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200807 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200808
Florin Coras1a1adc72016-07-22 01:45:30 +0200809 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200810 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200811 clib_warning ("vni %d not associated to a %s!", vni,
812 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200813 return VNET_API_ERROR_INVALID_VALUE;
814 }
815
Florin Corasf727db92016-06-23 15:01:58 +0200816 /* store/remove mapping from map-cache */
817 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200818}
819
Filip Tehlar324112f2016-06-02 16:07:38 +0200820int
Florin Coras1a1adc72016-07-22 01:45:30 +0200821vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +0200822{
Florin Corasa2157cf2016-08-16 21:09:14 +0200823 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
824 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +0200825
826 if (vnet_lisp_enable_disable_status () == 0)
827 {
828 clib_warning ("LISP is disabled!");
829 return -1;
830 }
831
Florin Coras1a1adc72016-07-22 01:45:30 +0200832 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
833 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
834
835 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +0200836 {
837 clib_warning ("can't add/del default vni-vrf mapping!");
838 return -1;
839 }
840
Florin Coras1a1adc72016-07-22 01:45:30 +0200841 dp_idp = hash_get (dp_table_by_vni[0], vni);
842 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200843
844 if (is_add)
845 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200846 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200847 {
848 clib_warning ("vni %d or vrf %d already used in vrf/vni "
849 "mapping!", vni, dp_id);
850 return -1;
851 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200852 hash_set (dp_table_by_vni[0], vni, dp_id);
853 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +0200854
855 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200856 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +0200857 }
858 else
859 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200860 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200861 {
862 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
863 "mapping!", vni, dp_id);
864 return -1;
865 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200866 hash_unset (dp_table_by_vni[0], vni);
867 hash_unset (vni_by_dp_table[0], dp_id);
Florin Corasf727db92016-06-23 15:01:58 +0200868
869 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200870 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200871 }
872 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +0200873
Filip Tehlar324112f2016-06-02 16:07:38 +0200874}
875
Florin Corasf727db92016-06-23 15:01:58 +0200876/* return 0 if the two locator sets are identical 1 otherwise */
877static u8
Florin Corasa2157cf2016-08-16 21:09:14 +0200878compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
879 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200880{
Florin Corasf727db92016-06-23 15:01:58 +0200881 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +0200882 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200883
Florin Corasa2157cf2016-08-16 21:09:14 +0200884 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +0200885 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200886
Florin Corasa2157cf2016-08-16 21:09:14 +0200887 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200888 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200889 old_li = vec_elt (old_ls_indexes, i);
890 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +0200891
Florin Corasa2157cf2016-08-16 21:09:14 +0200892 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +0200893
894 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +0200895 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200896 }
Florin Corasf727db92016-06-23 15:01:58 +0200897 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200898}
899
Filip Tehlard5fcc462016-10-17 16:20:18 +0200900typedef struct
901{
902 u8 is_negative;
903 void *lcm;
904 gid_address_t *eids_to_be_deleted;
905} remove_mapping_args_t;
906
907/**
908 * Callback invoked when a sub-prefix is found
909 */
910static void
911remove_mapping_if_needed (u32 mi, void *arg)
912{
913 u8 delete = 0;
914 remove_mapping_args_t *a = arg;
915 lisp_cp_main_t *lcm = a->lcm;
916 mapping_t *m;
917 locator_set_t *ls;
918
919 m = pool_elt_at_index (lcm->mapping_pool, mi);
920 if (!m)
921 return;
922
923 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
924
925 if (a->is_negative)
926 {
927 if (0 != vec_len (ls->locator_indices))
928 delete = 1;
929 }
930 else
931 {
932 if (0 == vec_len (ls->locator_indices))
933 delete = 1;
934 }
935
936 if (delete)
937 vec_add1 (a->eids_to_be_deleted, m->eid);
938}
939
940/**
941 * This function searches map cache and looks for IP prefixes that are subset
942 * of the provided one. If such prefix is found depending on 'is_negative'
943 * it does follows:
944 *
945 * 1) if is_negative is true and found prefix points to positive mapping,
946 * then the mapping is removed
947 * 2) if is_negative is false and found prefix points to negative mapping,
948 * then the mapping is removed
949 */
950static void
951remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
952 u8 is_negative)
953{
954 gid_address_t *e;
955 remove_mapping_args_t a;
Florin Corasf3dc11a2017-01-24 03:13:43 -0800956
Filip Tehlard5fcc462016-10-17 16:20:18 +0200957 memset (&a, 0, sizeof (a));
958
959 /* do this only in src/dst mode ... */
960 if (MR_MODE_SRC_DST != lcm->map_request_mode)
961 return;
962
963 /* ... and only for IP prefix */
964 if (GID_ADDR_SRC_DST != gid_address_type (eid)
965 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
966 return;
967
968 a.is_negative = is_negative;
969 a.lcm = lcm;
970
971 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
972 remove_mapping_if_needed, &a);
973
974 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100975 {
Florin Corasf3dc11a2017-01-24 03:13:43 -0800976 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
977
978 memset (adj_args, 0, sizeof (adj_args[0]));
979 gid_address_copy (&adj_args->reid, e);
980 adj_args->is_add = 0;
981 if (vnet_lisp_add_del_adjacency (adj_args))
982 clib_warning ("failed to del adjacency!");
983
Filip Tehlard5fcc462016-10-17 16:20:18 +0200984 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100985 }
Filip Tehlard5fcc462016-10-17 16:20:18 +0200986
987 vec_free (a.eids_to_be_deleted);
988}
989
Filip Tehlar9677a942016-11-28 10:23:31 +0100990static void
991mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
992{
993 timing_wheel_delete (&lcm->wheel, mi);
994}
995
Filip Tehlara6bce492017-02-03 10:17:49 +0100996static int
997is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
998{
999 fib_node_index_t fei;
1000 fib_prefix_t prefix;
1001 fib_entry_flag_t flags;
1002
1003 ip_address_to_fib_prefix (addr, &prefix);
1004
1005 fei = fib_table_lookup (0, &prefix);
1006 flags = fib_entry_get_flags (fei);
1007 return (FIB_ENTRY_FLAG_LOCAL & flags);
1008}
1009
Filip Tehlar195bcee2016-05-13 17:37:35 +02001010/**
Florin Corasf727db92016-06-23 15:01:58 +02001011 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001012 *
Florin Coras71893ac2016-07-10 20:09:32 +02001013 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001014 * @param rlocs vector of remote locators
1015 * @param action action for negative map-reply
1016 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001017 * @param res_map_index the map-index that was created/updated/removed. It is
1018 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001019 * @param is_static used for distinguishing between statically learned
1020 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001021 * @return return code
1022 */
1023int
Florin Coras71893ac2016-07-10 20:09:32 +02001024vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001025 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001026 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001027{
Florin Corasa2157cf2016-08-16 21:09:14 +02001028 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1029 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1030 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001031 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001032 mapping_t *old_map;
Filip Tehlara6bce492017-02-03 10:17:49 +01001033 locator_t *loc;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001034
Florin Corasa2157cf2016-08-16 21:09:14 +02001035 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001036 {
1037 clib_warning ("LISP is disabled!");
1038 return VNET_API_ERROR_LISP_DISABLED;
1039 }
1040
Filip Tehlara6bce492017-02-03 10:17:49 +01001041 /* check if none of the locators match localy configured address */
1042 vec_foreach (loc, rlocs)
1043 {
1044 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1045 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1046 {
1047 clib_warning ("RLOC %U matches a local address!",
1048 format_gid_address, &loc->address);
1049 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1050 }
1051 }
1052
Florin Corasf727db92016-06-23 15:01:58 +02001053 if (res_map_index)
1054 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001055
Florin Corasf727db92016-06-23 15:01:58 +02001056 memset (m_args, 0, sizeof (m_args[0]));
1057 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001058
Florin Corasf727db92016-06-23 15:01:58 +02001059 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001060
Florin Coras71893ac2016-07-10 20:09:32 +02001061 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001062 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001063
1064 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001065 {
Florin Corasf727db92016-06-23 15:01:58 +02001066 /* overwrite: if mapping already exists, decide if locators should be
1067 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001068 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001069 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001070 if (!is_static && (old_map->is_static || old_map->local))
1071 {
1072 /* do not overwrite local or static remote mappings */
1073 clib_warning ("mapping %U rejected due to collision with local "
1074 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001075 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001076 return 0;
1077 }
1078
Florin Corasa2157cf2016-08-16 21:09:14 +02001079 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001080
Florin Corasa2157cf2016-08-16 21:09:14 +02001081 /* update mapping attributes */
1082 old_map->action = action;
1083 old_map->authoritative = authoritative;
1084 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001085
Florin Corasa2157cf2016-08-16 21:09:14 +02001086 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1087 old_map->locator_set_index);
1088 if (compare_locators (lcm, old_ls->locator_indices,
1089 ls_args->locators))
1090 {
1091 /* set locator-set index to overwrite */
1092 ls_args->is_add = 1;
1093 ls_args->index = old_map->locator_set_index;
1094 vnet_lisp_add_del_locator_set (ls_args, 0);
1095 if (res_map_index)
1096 res_map_index[0] = mi;
1097 }
1098 }
Florin Corasf727db92016-06-23 15:01:58 +02001099 /* new mapping */
1100 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001101 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001102 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1103
Florin Corasa2157cf2016-08-16 21:09:14 +02001104 ls_args->is_add = 1;
1105 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001106
Florin Corasa2157cf2016-08-16 21:09:14 +02001107 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001108
Florin Corasa2157cf2016-08-16 21:09:14 +02001109 /* add mapping */
1110 gid_address_copy (&m_args->eid, eid);
1111 m_args->is_add = 1;
1112 m_args->action = action;
1113 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001114 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001115 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001116 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001117
Florin Corasa2157cf2016-08-16 21:09:14 +02001118 if (res_map_index)
1119 res_map_index[0] = dst_map_index;
1120 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001121 }
Florin Corasf727db92016-06-23 15:01:58 +02001122 else
1123 {
Florin Coras71893ac2016-07-10 20:09:32 +02001124 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001125 {
1126 clib_warning ("cannot delete mapping for eid %U",
1127 format_gid_address, eid);
1128 return -1;
1129 }
Florin Corasf727db92016-06-23 15:01:58 +02001130
1131 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001132 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001133 m_args->locator_set_index = old_map->locator_set_index;
1134
1135 /* delete mapping associated from map-cache */
1136 vnet_lisp_map_cache_add_del (m_args, 0);
1137
1138 ls_args->is_add = 0;
1139 ls_args->index = old_map->locator_set_index;
1140 /* delete locator set */
1141 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001142
Filip Tehlar9677a942016-11-28 10:23:31 +01001143 /* delete timer associated to the mapping if any */
1144 if (old_map->timer_set)
1145 mapping_delete_timer (lcm, mi);
1146
Filip Tehlarb93222f2016-07-07 09:58:08 +02001147 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001148 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001149 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001150 }
1151
Filip Tehlar195bcee2016-05-13 17:37:35 +02001152 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001153 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001154}
1155
Filip Tehlar58f886a2016-05-30 15:57:40 +02001156int
Florin Corasf727db92016-06-23 15:01:58 +02001157vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001158{
1159 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001160 u32 mi, *map_indices = 0, *map_indexp;
1161 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1162 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1163 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001164
Florin Corasa2157cf2016-08-16 21:09:14 +02001165 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001166 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001167 ({
1168 vec_add1 (map_indices, mi);
1169 }));
1170 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001171
1172 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001173 {
1174 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1175 if (!map->local)
1176 {
1177 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001178
Florin Corasa2157cf2016-08-16 21:09:14 +02001179 dm_args->is_add = 0;
1180 gid_address_copy (&dm_args->eid, &map->eid);
1181 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001182
Florin Corasa2157cf2016-08-16 21:09:14 +02001183 /* delete mapping associated to fwd entry */
1184 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001185
Florin Corasa2157cf2016-08-16 21:09:14 +02001186 ls->is_add = 0;
1187 ls->local = 0;
1188 ls->index = map->locator_set_index;
1189 /* delete locator set */
1190 rv = vnet_lisp_add_del_locator_set (ls, 0);
1191 if (rv != 0)
1192 goto cleanup;
1193 }
1194 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001195
1196cleanup:
1197 if (map_indices)
1198 vec_free (map_indices);
1199 return rv;
1200}
1201
Filip Tehlar195bcee2016-05-13 17:37:35 +02001202/**
Florin Coras71893ac2016-07-10 20:09:32 +02001203 * Adds adjacency or removes forwarding entry associated to remote mapping.
1204 * Note that adjacencies are not stored, they only result in forwarding entries
1205 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001206 */
Florin Corasf3dc11a2017-01-24 03:13:43 -08001207int
1208vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
Florin Corasf727db92016-06-23 15:01:58 +02001209{
Florin Corasf3dc11a2017-01-24 03:13:43 -08001210 lisp_cp_main_t *lcm = &lisp_control_main;
Florin Coras71893ac2016-07-10 20:09:32 +02001211 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001212
1213 if (vnet_lisp_enable_disable_status () == 0)
1214 {
1215 clib_warning ("LISP is disabled!");
1216 return VNET_API_ERROR_LISP_DISABLED;
1217 }
1218
Filip Tehlar69a9b762016-09-23 10:00:52 +02001219 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
Florin Corasf3dc11a2017-01-24 03:13:43 -08001220 &a->reid, &a->leid);
Florin Coras71893ac2016-07-10 20:09:32 +02001221 if (GID_LOOKUP_MISS == remote_mi)
1222 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001223 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001224 format_gid_address, &a->reid);
Florin Corasf727db92016-06-23 15:01:58 +02001225
Florin Coras71893ac2016-07-10 20:09:32 +02001226 return -1;
1227 }
1228
Florin Corasf3dc11a2017-01-24 03:13:43 -08001229 if (a->is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001230 {
1231 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1232 * delete*/
1233
1234 /* check if source eid has an associated mapping. If pitr mode is on,
1235 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001236 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasf3dc11a2017-01-24 03:13:43 -08001237 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001238
Florin Coras71893ac2016-07-10 20:09:32 +02001239 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001240 {
1241 clib_warning ("Local eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001242 format_gid_address, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001243
Florin Corasa2157cf2016-08-16 21:09:14 +02001244 return -1;
1245 }
Florin Corasf727db92016-06-23 15:01:58 +02001246
Florin Coras71893ac2016-07-10 20:09:32 +02001247 /* update forwarding */
1248 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001249 }
1250 else
Florin Coras71893ac2016-07-10 20:09:32 +02001251 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001252
1253 return 0;
1254}
1255
1256int
Florin Corasdca88042016-09-14 16:01:38 +02001257vnet_lisp_set_map_request_mode (u8 mode)
1258{
1259 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1260
1261 if (vnet_lisp_enable_disable_status () == 0)
1262 {
1263 clib_warning ("LISP is disabled!");
1264 return VNET_API_ERROR_LISP_DISABLED;
1265 }
1266
1267 if (mode >= _MR_MODE_MAX)
1268 {
1269 clib_warning ("Invalid LISP map request mode %d!", mode);
1270 return VNET_API_ERROR_INVALID_ARGUMENT;
1271 }
1272
1273 lcm->map_request_mode = mode;
1274 return 0;
1275}
1276
Filip Tehlar53f09e32016-05-19 14:25:44 +02001277int
1278vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1279{
Florin Corasa2157cf2016-08-16 21:09:14 +02001280 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001281 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001282 mapping_t *m;
1283 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001284
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001285 if (vnet_lisp_enable_disable_status () == 0)
1286 {
1287 clib_warning ("LISP is disabled!");
1288 return VNET_API_ERROR_LISP_DISABLED;
1289 }
1290
Filip Tehlar53f09e32016-05-19 14:25:44 +02001291 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1292 if (!p)
1293 {
1294 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1295 return -1;
1296 }
1297 locator_set_index = p[0];
1298
1299 if (is_add)
1300 {
1301 pool_get (lcm->mapping_pool, m);
1302 m->locator_set_index = locator_set_index;
1303 m->local = 1;
1304 lcm->pitr_map_index = m - lcm->mapping_pool;
1305
1306 /* enable pitr mode */
1307 lcm->lisp_pitr = 1;
1308 }
1309 else
1310 {
1311 /* remove pitr mapping */
1312 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1313
1314 /* disable pitr mode */
1315 lcm->lisp_pitr = 0;
1316 }
1317 return 0;
1318}
1319
Florin Corasba888e42017-01-24 11:38:18 -08001320/**
1321 * Configure Proxy-ETR
1322 *
1323 * @param ip PETR's IP address
1324 * @param is_add Flag that indicates if this is an addition or removal
1325 *
1326 * return 0 on success
1327 */
1328int
1329vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1330{
1331 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1332 u32 ls_index = ~0;
1333 mapping_t *m;
1334 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1335 locator_t loc;
1336
1337 if (vnet_lisp_enable_disable_status () == 0)
1338 {
1339 clib_warning ("LISP is disabled!");
1340 return VNET_API_ERROR_LISP_DISABLED;
1341 }
1342
1343 memset (ls_args, 0, sizeof (*ls_args));
1344
1345 if (is_add)
1346 {
1347 /* Create dummy petr locator-set */
1348 gid_address_from_ip (&loc.address, ip);
1349 loc.priority = 1;
1350 loc.state = loc.weight = 1;
Florin Coras2743cc42017-01-29 16:42:20 -08001351 loc.local = 0;
Florin Corasba888e42017-01-24 11:38:18 -08001352
1353 ls_args->is_add = 1;
1354 ls_args->index = ~0;
1355 vec_add1 (ls_args->locators, loc);
1356 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1357
1358 /* Add petr mapping */
1359 pool_get (lcm->mapping_pool, m);
1360 m->locator_set_index = ls_index;
1361 lcm->petr_map_index = m - lcm->mapping_pool;
1362
1363 /* Enable use-petr */
1364 lcm->flags |= LISP_FLAG_USE_PETR;
1365 }
1366 else
1367 {
1368 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1369
1370 /* Remove petr locator */
1371 ls_args->is_add = 0;
1372 ls_args->index = m->locator_set_index;
1373 vnet_lisp_add_del_locator_set (ls_args, 0);
1374
1375 /* Remove petr mapping */
1376 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1377
1378 /* Disable use-petr */
1379 lcm->flags &= ~LISP_FLAG_USE_PETR;
1380 }
1381 return 0;
1382}
1383
Florin Corase127a7e2016-02-18 22:20:01 +01001384/* cleans locator to locator-set data and removes locators not part of
1385 * any locator-set */
1386static void
1387clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1388{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001389 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001390 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1391 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01001392 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001393 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1394 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1395 loc_indexp[0]);
1396 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1397 {
1398 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1399 if (ls_indexp[0] == lsi)
1400 break;
1401 }
Florin Corase127a7e2016-02-18 22:20:01 +01001402
Florin Corasa2157cf2016-08-16 21:09:14 +02001403 /* delete index for removed locator-set */
1404 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01001405
1406 /* delete locator if it's part of no locator-set */
1407 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001408 {
1409 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1410 vec_add1 (to_be_deleted, i);
1411 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001412 }
1413
1414 if (to_be_deleted)
1415 {
1416 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02001417 {
1418 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1419 vec_del1 (ls->locator_indices, loc_indexp[0]);
1420 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001421 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01001422 }
1423}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001424
Florin Corasf727db92016-06-23 15:01:58 +02001425static inline uword *
1426get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001427{
Florin Corasa2157cf2016-08-16 21:09:14 +02001428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001429
Florin Corasa2157cf2016-08-16 21:09:14 +02001430 ASSERT (a != NULL);
1431 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001432
1433 /* find locator-set */
1434 if (a->local)
1435 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001436 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001437 }
1438 else
1439 {
1440 *p = a->index;
1441 }
1442
1443 return p;
1444}
1445
Florin Corasf727db92016-06-23 15:01:58 +02001446static inline int
1447is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02001448 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001449{
Florin Corasa2157cf2016-08-16 21:09:14 +02001450 locator_t *itloc;
1451 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001452
Florin Corasa2157cf2016-08-16 21:09:14 +02001453 ASSERT (ls != NULL);
1454 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001455
Florin Corasa2157cf2016-08-16 21:09:14 +02001456 vec_foreach (locit, ls->locator_indices)
1457 {
1458 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1459 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1460 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1461 {
1462 clib_warning ("Duplicate locator");
1463 return VNET_API_ERROR_VALUE_EXIST;
1464 }
1465 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001466
1467 return 0;
1468}
1469
Florin Corasf727db92016-06-23 15:01:58 +02001470static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02001471remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1472 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001473{
Florin Corasa2157cf2016-08-16 21:09:14 +02001474 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1475 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001476
Florin Corasa2157cf2016-08-16 21:09:14 +02001477 ASSERT (ls != NULL);
1478 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001479
Florin Corasa2157cf2016-08-16 21:09:14 +02001480 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1481 pool_put_index (lcm->locator_pool, locit[0]);
1482 vec_del1 (ls->locator_indices, loc_id);
1483 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001484}
1485
1486int
1487vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001488 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001489{
Florin Corasa2157cf2016-08-16 21:09:14 +02001490 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1491 locator_t *loc = NULL, *itloc = NULL;
1492 uword _p = (u32) ~ 0, *p = &_p;
1493 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001494 u32 loc_id = ~0;
1495 int ret = 0;
1496
Florin Corasa2157cf2016-08-16 21:09:14 +02001497 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001498
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001499 if (vnet_lisp_enable_disable_status () == 0)
1500 {
1501 clib_warning ("LISP is disabled!");
1502 return VNET_API_ERROR_LISP_DISABLED;
1503 }
1504
Florin Corasa2157cf2016-08-16 21:09:14 +02001505 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001506 if (!p)
1507 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001508 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001509 return VNET_API_ERROR_INVALID_ARGUMENT;
1510 }
1511
1512 if (ls == 0)
1513 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001514 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001515 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001516 {
1517 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1518 p[0]);
1519 return VNET_API_ERROR_INVALID_ARGUMENT;
1520 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001521 }
1522
1523 if (a->is_add)
1524 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001525 if (ls_result)
1526 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001527
Florin Corasa2157cf2016-08-16 21:09:14 +02001528 /* allocate locators */
1529 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001530 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001531 ret = is_locator_in_locator_set (lcm, ls, itloc);
1532 if (0 != ret)
1533 {
1534 return ret;
1535 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001536
Florin Corasa2157cf2016-08-16 21:09:14 +02001537 pool_get (lcm->locator_pool, loc);
1538 loc[0] = itloc[0];
1539 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001540
Florin Corasa2157cf2016-08-16 21:09:14 +02001541 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001542
Florin Corasa2157cf2016-08-16 21:09:14 +02001543 vec_validate (lcm->locator_to_locator_sets, loc_index);
1544 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1545 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001546 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001547 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001548 }
1549 else
1550 {
1551 ls_index = p[0];
1552
1553 itloc = a->locators;
1554 loc_id = 0;
1555 vec_foreach (locit, ls->locator_indices)
1556 {
1557 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1558
1559 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1560 {
1561 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1562 }
1563 if (0 == loc->local &&
1564 !gid_address_cmp (&loc->address, &itloc->address))
1565 {
1566 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1567 }
1568
1569 loc_id++;
1570 }
1571 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001572
1573 return 0;
1574}
1575
Florin Corase127a7e2016-02-18 22:20:01 +01001576int
1577vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001578 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01001579{
Florin Corasa2157cf2016-08-16 21:09:14 +02001580 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1581 locator_set_t *ls;
1582 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001583 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001584 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001585 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001586
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001587 if (vnet_lisp_enable_disable_status () == 0)
1588 {
1589 clib_warning ("LISP is disabled!");
1590 return VNET_API_ERROR_LISP_DISABLED;
1591 }
1592
Florin Corase127a7e2016-02-18 22:20:01 +01001593 if (a->is_add)
1594 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001595 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01001596
1597 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02001598 if (p && p[0] != (u32) ~ 0)
1599 {
1600 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1601 if (!ls)
1602 {
1603 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1604 p[0]);
1605 return -1;
1606 }
Florin Corase127a7e2016-02-18 22:20:01 +01001607
Florin Corasa2157cf2016-08-16 21:09:14 +02001608 /* clean locator to locator-set vectors and remove locators if
1609 * they're not part of another locator-set */
1610 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001611
Florin Corasa2157cf2016-08-16 21:09:14 +02001612 /* remove locator indices from locator set */
1613 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01001614
Florin Corasa2157cf2016-08-16 21:09:14 +02001615 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01001616
Florin Corasa2157cf2016-08-16 21:09:14 +02001617 if (ls_result)
1618 ls_result[0] = p[0];
1619 }
Florin Corase127a7e2016-02-18 22:20:01 +01001620 /* new locator-set */
1621 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001622 {
1623 pool_get (lcm->locator_set_pool, ls);
1624 memset (ls, 0, sizeof (*ls));
1625 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01001626
Florin Corasa2157cf2016-08-16 21:09:14 +02001627 if (a->local)
1628 {
1629 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01001630
Florin Corasa2157cf2016-08-16 21:09:14 +02001631 if (!lcm->locator_set_index_by_name)
1632 lcm->locator_set_index_by_name = hash_create_vec (
1633 /* size */
1634 0,
1635 sizeof
1636 (ls->name
1637 [0]),
1638 sizeof
1639 (uword));
1640 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1641 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01001642
Florin Corasa2157cf2016-08-16 21:09:14 +02001643 /* mark as local locator-set */
1644 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1645 }
1646 ls->local = a->local;
1647 if (ls_result)
1648 ls_result[0] = ls_index;
1649 }
Florin Corase127a7e2016-02-18 22:20:01 +01001650
Florin Corasa2157cf2016-08-16 21:09:14 +02001651 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001652 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02001653 {
1654 return ret;
1655 }
Florin Corase127a7e2016-02-18 22:20:01 +01001656 }
1657 else
1658 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001659 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001660 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001661 {
1662 clib_warning ("locator-set %v doesn't exists", a->name);
1663 return -1;
1664 }
Florin Corase127a7e2016-02-18 22:20:01 +01001665
Florin Corasa2157cf2016-08-16 21:09:14 +02001666 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001667 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001668 {
1669 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1670 return -1;
1671 }
Florin Corase127a7e2016-02-18 22:20:01 +01001672
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001673 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02001674 {
1675 clib_warning ("Can't delete the locator-set used to constrain "
1676 "the itr-rlocs in map-requests!");
1677 return -1;
1678 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001679
Florin Corasa2157cf2016-08-16 21:09:14 +02001680 if (vec_len (lcm->locator_set_to_eids) != 0)
1681 {
1682 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1683 if (vec_len (eid_indexes[0]) != 0)
1684 {
1685 clib_warning
1686 ("Can't delete a locator that supports a mapping!");
1687 return -1;
1688 }
1689 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001690
1691 /* clean locator to locator-sets data */
1692 clean_locator_to_locator_set (lcm, p[0]);
1693
1694 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02001695 {
1696 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001697
Florin Corasa2157cf2016-08-16 21:09:14 +02001698 vec_foreach_index (it, lcm->local_locator_set_indexes)
1699 {
1700 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1701 if (lsi == p[0])
1702 {
1703 vec_del1 (lcm->local_locator_set_indexes, it);
1704 break;
1705 }
1706 }
1707 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1708 }
1709 vec_free (ls->name);
1710 vec_free (ls->locator_indices);
1711 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001712 }
1713 return 0;
1714}
1715
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001716int
1717vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1718{
1719 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1720
1721 lcm->rloc_probing = is_enable;
1722 return 0;
1723}
1724
1725int
1726vnet_lisp_map_register_enable_disable (u8 is_enable)
1727{
1728 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1729
1730 lcm->map_registering = is_enable;
1731 return 0;
1732}
1733
Filip Tehlar46d4e362016-05-09 09:39:26 +02001734clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02001735vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001736{
Florin Coras1a1adc72016-07-22 01:45:30 +02001737 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02001738 clib_error_t *error = 0;
1739 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1740 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001741
Florin Corasf727db92016-06-23 15:01:58 +02001742 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001743 error = vnet_lisp_gpe_enable_disable (a);
1744 if (error)
1745 {
1746 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02001747 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02001748 }
1749
Florin Corasf727db92016-06-23 15:01:58 +02001750 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001751 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001752 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02001753
1754 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02001755 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1756 dp_add_del_iface(lcm, vni, 0, 1);
1757 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02001758 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1759 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02001760 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02001761 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02001762 }
1763 else
1764 {
Florin Corasf727db92016-06-23 15:01:58 +02001765 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02001766 hash_free (lcm->fwd_entry_by_mapping_index);
1767 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02001768 }
1769
1770 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02001771 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001772
1773 return 0;
1774}
1775
Filip Tehlar46d4e362016-05-09 09:39:26 +02001776u8
1777vnet_lisp_enable_disable_status (void)
1778{
Florin Corasa2157cf2016-08-16 21:09:14 +02001779 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02001780 return lcm->is_enabled;
1781}
1782
Florin Corase127a7e2016-02-18 22:20:01 +01001783int
1784vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1785{
Florin Corasa2157cf2016-08-16 21:09:14 +02001786 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01001787 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001788 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01001789
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001790 if (vnet_lisp_enable_disable_status () == 0)
1791 {
1792 clib_warning ("LISP is disabled!");
1793 return VNET_API_ERROR_LISP_DISABLED;
1794 }
1795
Florin Corase127a7e2016-02-18 22:20:01 +01001796 if (a->is_add)
1797 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001798
1799 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02001800 {
1801 clib_warning ("map-resolver %U already exists!", format_ip_address,
1802 &a->address);
1803 return -1;
1804 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001805
1806 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02001807 ip_address_copy (&mr->address, &a->address);
1808 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001809
1810 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02001811 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01001812 }
1813 else
1814 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001815 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1816 {
1817 mr = vec_elt_at_index (lcm->map_resolvers, i);
1818 if (!ip_address_cmp (&mr->address, &a->address))
1819 {
1820 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1821 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001822
Florin Corasa2157cf2016-08-16 21:09:14 +02001823 vec_del1 (lcm->map_resolvers, i);
1824 break;
1825 }
1826 }
Florin Corase127a7e2016-02-18 22:20:01 +01001827 }
1828 return 0;
1829}
1830
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001831int
1832vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1833{
Florin Corasa2157cf2016-08-16 21:09:14 +02001834 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1835 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001836
Florin Corasf727db92016-06-23 15:01:58 +02001837 if (vnet_lisp_enable_disable_status () == 0)
1838 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001839 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02001840 return VNET_API_ERROR_LISP_DISABLED;
1841 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001842
1843 if (a->is_add)
1844 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001845 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001846 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001847 {
1848 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1849 return VNET_API_ERROR_INVALID_ARGUMENT;
1850 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001851
1852 lcm->mreq_itr_rlocs = p[0];
1853 }
1854 else
1855 {
1856 lcm->mreq_itr_rlocs = ~0;
1857 }
1858
1859 return 0;
1860}
1861
Florin Corase127a7e2016-02-18 22:20:01 +01001862/* Statistics (not really errors) */
1863#define foreach_lisp_cp_lookup_error \
1864_(DROP, "drop") \
1865_(MAP_REQUESTS_SENT, "map-request sent")
1866
Florin Corasa2157cf2016-08-16 21:09:14 +02001867static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01001868#define _(sym,string) string,
1869 foreach_lisp_cp_lookup_error
1870#undef _
1871};
1872
1873typedef enum
1874{
1875#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02001876 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01001877#undef _
1878 LISP_CP_LOOKUP_N_ERROR,
1879} lisp_cp_lookup_error_t;
1880
1881typedef enum
1882{
1883 LISP_CP_LOOKUP_NEXT_DROP,
Florin Corase127a7e2016-02-18 22:20:01 +01001884 LISP_CP_LOOKUP_N_NEXT,
1885} lisp_cp_lookup_next_t;
1886
1887typedef struct
1888{
1889 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02001890 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01001891} lisp_cp_lookup_trace_t;
1892
1893u8 *
1894format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1895{
1896 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1897 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02001898 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01001899
1900 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02001901 format_ip_address, &t->map_resolver_ip, format_gid_address,
1902 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01001903 return s;
1904}
1905
Florin Coras1c494842016-06-16 21:08:56 +02001906int
1907get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02001908 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02001909{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001910 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02001911 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01001912
Florin Corasa2157cf2016-08-16 21:09:14 +02001913 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01001914 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001915 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02001916 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001917 }
1918
Florin Corasddfafb82016-05-13 18:09:56 +02001919 /* find the first mr ip we have a route to and the ip of the
1920 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02001921 vec_foreach (mrit, lcm->map_resolvers)
1922 {
1923 a = &mrit->address;
1924 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1925 {
1926 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001927
Florin Corasa2157cf2016-08-16 21:09:14 +02001928 /* also update globals */
1929 return 1;
1930 }
1931 }
Florin Corasddfafb82016-05-13 18:09:56 +02001932
Florin Corasa2157cf2016-08-16 21:09:14 +02001933 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02001934 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02001935}
Filip Tehlar254b0362016-04-07 10:04:34 +02001936
Filip Tehlarbeceab92016-04-20 17:21:55 +02001937static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02001938build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1939{
Florin Corasa2157cf2016-08-16 21:09:14 +02001940 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02001941 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02001942 locator_t *loc;
1943 u32 *loc_indexp;
1944 ip_interface_address_t *ia = 0;
1945 gid_address_t gid_data, *gid = &gid_data;
1946 gid_address_t *rlocs = 0;
1947 ip_prefix_t *ippref = &gid_address_ippref (gid);
1948 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02001949
Filip Tehlar324112f2016-06-02 16:07:38 +02001950 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02001951 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02001952 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02001953 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001954 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02001955 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1956
Filip Tehlar254b0362016-04-07 10:04:34 +02001957 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02001958
1959 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02001960 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1961 loc->sw_if_index, 1 /* unnumbered */,
1962 ({
Florin Coras1c494842016-06-16 21:08:56 +02001963 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1964 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02001965 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001966 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02001967 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02001968 }));
1969
Filip Tehlar254b0362016-04-07 10:04:34 +02001970 /* Add ipv6 locators */
1971 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1972 loc->sw_if_index, 1 /* unnumbered */,
1973 ({
Florin Coras1c494842016-06-16 21:08:56 +02001974 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1975 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02001976 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001977 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02001978 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02001979 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02001980 /* *INDENT-ON* */
1981
Filip Tehlar254b0362016-04-07 10:04:34 +02001982 }
1983 return rlocs;
1984}
1985
Florin Corase127a7e2016-02-18 22:20:01 +01001986static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001987build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
1988 ip_address_t * sloc, ip_address_t * rloc,
1989 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
1990{
1991 vlib_buffer_t *b;
1992 u32 bi;
1993 vlib_main_t *vm = lcm->vlib_main;
1994
1995 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
1996 {
1997 clib_warning ("Can't allocate buffer for Map-Request!");
1998 return 0;
1999 }
2000
2001 b = vlib_get_buffer (vm, bi);
2002
2003 /* leave some space for the encap headers */
2004 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2005
2006 /* put lisp msg */
2007 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2008 1 /* rloc probe */ , nonce_res);
2009
2010 /* push outer ip header */
2011 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2012 rloc);
2013
2014 bi_res[0] = bi;
2015
2016 return b;
2017}
2018
2019static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002020build_encapsulated_map_request (lisp_cp_main_t * lcm,
2021 gid_address_t * seid, gid_address_t * deid,
2022 locator_set_t * loc_set, ip_address_t * mr_ip,
2023 ip_address_t * sloc, u8 is_smr_invoked,
2024 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01002025{
Florin Corasa2157cf2016-08-16 21:09:14 +02002026 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01002027 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02002028 gid_address_t *rlocs = 0;
2029 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01002030
2031 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2032 {
2033 clib_warning ("Can't allocate buffer for Map-Request!");
2034 return 0;
2035 }
2036
2037 b = vlib_get_buffer (vm, bi);
2038
2039 /* leave some space for the encap headers */
2040 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2041
Filip Tehlar254b0362016-04-07 10:04:34 +02002042 /* get rlocs */
2043 rlocs = build_itr_rloc_list (lcm, loc_set);
2044
Filip Tehlard5fcc462016-10-17 16:20:18 +02002045 if (MR_MODE_SRC_DST == lcm->map_request_mode
2046 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02002047 {
2048 gid_address_t sd;
2049 memset (&sd, 0, sizeof (sd));
2050 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002051 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2052 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002053 }
2054 else
2055 {
2056 /* put lisp msg */
2057 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002058 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002059 }
Florin Corase127a7e2016-02-18 22:20:01 +01002060
2061 /* push ecm: udp-ip-lisp */
2062 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2063
Florin Corase127a7e2016-02-18 22:20:01 +01002064 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02002065 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02002066 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01002067
2068 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02002069
Florin Corasa2157cf2016-08-16 21:09:14 +02002070 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01002071 return b;
2072}
2073
2074static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002075reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01002076{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002077 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2078 r->retries_num = 0;
2079}
2080
2081static int
2082elect_map_resolver (lisp_cp_main_t * lcm)
2083{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002084 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002085
2086 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02002087 {
2088 if (!mr->is_down)
2089 {
2090 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2091 lcm->do_map_resolver_election = 0;
2092 return 1;
2093 }
2094 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002095 return 0;
2096}
2097
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002098static void
2099free_map_register_records (mapping_t * maps)
2100{
2101 mapping_t *map;
2102 vec_foreach (map, maps) vec_free (map->locators);
2103
2104 vec_free (maps);
2105}
2106
2107static void
2108add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2109 ip_address_t * probed_loc)
2110{
2111 u32 *li;
2112 locator_t *loc, new;
2113 ip_interface_address_t *ia = 0;
2114 void *addr;
2115 ip_address_t *new_ip = &gid_address_ip (&new.address);
2116
2117 m->locators = 0;
2118 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2119 locator_set_index);
2120 vec_foreach (li, ls->locator_indices)
2121 {
2122 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2123 new = loc[0];
2124 if (loc->local)
2125 {
2126 /* *INDENT-OFF* */
2127 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2128 loc->sw_if_index, 1 /* unnumbered */,
2129 ({
2130 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2131 ia);
2132 ip_address_set (new_ip, addr, IP4);
2133 }));
2134
2135 /* Add ipv6 locators */
2136 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2137 loc->sw_if_index, 1 /* unnumbered */,
2138 ({
2139 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2140 ia);
2141 ip_address_set (new_ip, addr, IP6);
2142 }));
2143 /* *INDENT-ON* */
2144
2145 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2146 new.probed = 1;
2147 }
2148 vec_add1 (m->locators, new);
2149 }
2150}
2151
2152static mapping_t *
2153build_map_register_record_list (lisp_cp_main_t * lcm)
2154{
2155 mapping_t *recs = 0, rec, *m;
2156
2157 /* *INDENT-OFF* */
2158 pool_foreach(m, lcm->mapping_pool,
2159 {
2160 /* for now build only local mappings */
2161 if (!m->local)
2162 continue;
2163
2164 rec = m[0];
2165 add_locators (lcm, &rec, m->locator_set_index, NULL);
2166 vec_add1 (recs, rec);
2167 });
2168 /* *INDENT-ON* */
2169
2170 return recs;
2171}
2172
2173static int
2174update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2175 lisp_key_type_t key_id, u8 * key,
2176 u16 auth_data_len, u32 msg_len)
2177{
2178 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2179 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2180
2181 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2182 (unsigned char *) map_reg_hdr, msg_len, NULL,
2183 NULL);
2184 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2185
2186 return 0;
2187}
2188
2189static vlib_buffer_t *
2190build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2191 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2192 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2193 u32 * bi_res)
2194{
2195 void *map_reg_hdr;
2196 vlib_buffer_t *b;
2197 u32 bi, auth_data_len = 0, msg_len = 0;
2198 vlib_main_t *vm = lcm->vlib_main;
2199
2200 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2201 {
2202 clib_warning ("Can't allocate buffer for Map-Register!");
2203 return 0;
2204 }
2205
2206 b = vlib_get_buffer (vm, bi);
2207
2208 /* leave some space for the encap headers */
2209 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2210
2211 auth_data_len = auth_data_len_by_key_id (key_id);
2212 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2213 auth_data_len, nonce_res,
2214 &msg_len);
2215
2216 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2217 msg_len);
2218
2219 /* push outer ip header */
2220 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2221 ms_ip);
2222
2223 bi_res[0] = bi;
2224 return b;
2225}
2226
2227static int
2228get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2229{
2230 lisp_msmr_t *mr;
2231 while (lcm->do_map_resolver_election
2232 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2233 &lcm->active_map_resolver,
2234 ip)))
2235 {
2236 if (0 == elect_map_resolver (lcm))
2237 /* all map resolvers are down */
2238 {
2239 /* restart MR checking by marking all of them up */
2240 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2241 return -1;
2242 }
2243 }
2244 return 0;
2245}
2246
Filip Tehlarcda70662017-01-16 10:30:03 +01002247/* CP output statistics */
2248#define foreach_lisp_cp_output_error \
2249_(MAP_REGISTERS_SENT, "map-registers sent") \
2250_(RLOC_PROBES_SENT, "rloc-probes sent")
2251
2252static char *lisp_cp_output_error_strings[] = {
2253#define _(sym,string) string,
2254 foreach_lisp_cp_output_error
2255#undef _
2256};
2257
2258typedef enum
2259{
2260#define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2261 foreach_lisp_cp_output_error
2262#undef _
2263 LISP_CP_OUTPUT_N_ERROR,
2264} lisp_cp_output_error_t;
2265
2266static uword
2267lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2268 vlib_frame_t * from_frame)
2269{
2270 return 0;
2271}
2272
2273/* dummy node used only for statistics */
2274/* *INDENT-OFF* */
2275VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2276 .function = lisp_cp_output,
2277 .name = "lisp-cp-output",
2278 .vector_size = sizeof (u32),
2279 .format_trace = format_lisp_cp_input_trace,
2280 .type = VLIB_NODE_TYPE_INTERNAL,
2281
2282 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2283 .error_strings = lisp_cp_output_error_strings,
2284
2285 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2286
2287 .next_nodes = {
2288 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2289 },
2290};
2291/* *INDENT-ON* */
2292
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002293static int
2294send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2295 u32 local_locator_set_index, ip_address_t * sloc,
2296 ip_address_t * rloc)
2297{
2298 locator_set_t *ls;
2299 u32 bi;
2300 vlib_buffer_t *b;
2301 vlib_frame_t *f;
2302 u64 nonce = 0;
2303 u32 next_index, *to_next;
2304 gid_address_t *itr_rlocs;
2305
2306 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2307 itr_rlocs = build_itr_rloc_list (lcm, ls);
2308
2309 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2310 vec_free (itr_rlocs);
2311 if (!b)
2312 return -1;
2313
2314 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2315
2316 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2317 ip4_lookup_node.index : ip6_lookup_node.index;
2318
2319 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2320
2321 /* Enqueue the packet */
2322 to_next = vlib_frame_vector_args (f);
2323 to_next[0] = bi;
2324 f->n_vectors = 1;
2325 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2326
2327 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2328 return 0;
2329}
2330
2331static int
2332send_rloc_probes (lisp_cp_main_t * lcm)
2333{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002334 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002335 mapping_t *lm;
2336 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002337 locator_pair_t *lp;
Filip Tehlarcda70662017-01-16 10:30:03 +01002338 u32 si, rloc_probes_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002339
2340 /* *INDENT-OFF* */
2341 pool_foreach (e, lcm->fwd_entry_pool,
2342 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002343 if (vec_len (e->locator_pairs) == 0)
2344 continue;
2345
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002346 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2347 if (~0 == si)
2348 {
2349 clib_warning ("internal error: cannot find local eid %U in "
2350 "map-cache!", format_gid_address, &e->leid);
2351 continue;
2352 }
2353 lm = pool_elt_at_index (lcm->mapping_pool, si);
2354
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002355 /* get the best (lowest) priority */
2356 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002357
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002358 /* send rloc-probe for pair(s) with the best remote locator priority */
2359 vec_foreach (lp, e->locator_pairs)
2360 {
2361 if (lp->priority != lprio)
2362 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002363
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002364 /* get first remote locator */
2365 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2366 &lp->rmt_loc);
Filip Tehlarcda70662017-01-16 10:30:03 +01002367 rloc_probes_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002368 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002369 });
2370 /* *INDENT-ON* */
2371
Filip Tehlarcda70662017-01-16 10:30:03 +01002372 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2373 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2374 rloc_probes_sent);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002375 return 0;
2376}
2377
2378static int
2379send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2380{
Filip Tehlarcda70662017-01-16 10:30:03 +01002381 u32 bi, map_registers_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002382 vlib_buffer_t *b;
2383 ip_address_t sloc;
2384 vlib_frame_t *f;
2385 u64 nonce = 0;
2386 u32 next_index, *to_next;
2387 ip_address_t *ms = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002388 mapping_t *records, *r, *g;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002389
2390 // TODO: support multiple map servers and do election
2391 if (0 == vec_len (lcm->map_servers))
2392 return -1;
2393
2394 ms = &lcm->map_servers[0].address;
2395
2396 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2397 {
2398 clib_warning ("no eligible interface address found for %U!",
2399 format_ip_address, &lcm->map_servers[0]);
2400 return -1;
2401 }
2402
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002403 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002404 if (!records)
2405 return -1;
2406
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002407 vec_foreach (r, records)
2408 {
2409 u8 *key = r->key;
2410 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002411
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002412 if (!key)
2413 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002414
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002415 g = 0;
2416 // TODO: group mappings that share common key
2417 vec_add1 (g, r[0]);
2418 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
2419 key_id, key, &bi);
2420 vec_free (g);
2421 if (!b)
2422 continue;
2423
2424 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2425
2426 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2427 ip4_lookup_node.index : ip6_lookup_node.index;
2428
2429 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2430
2431 /* Enqueue the packet */
2432 to_next = vlib_frame_vector_args (f);
2433 to_next[0] = bi;
2434 f->n_vectors = 1;
2435 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Filip Tehlarcda70662017-01-16 10:30:03 +01002436 map_registers_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002437
2438 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2439 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002440 free_map_register_records (records);
2441
Filip Tehlarcda70662017-01-16 10:30:03 +01002442 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2443 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2444 map_registers_sent);
2445
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002446 return 0;
2447}
2448
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002449#define send_encapsulated_map_request(lcm, seid, deid, smr) \
2450 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2451
2452#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2453 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2454
2455static int
Florin Corasa2157cf2016-08-16 21:09:14 +02002456_send_encapsulated_map_request (lisp_cp_main_t * lcm,
2457 gid_address_t * seid, gid_address_t * deid,
2458 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002459{
Florin Corasa2157cf2016-08-16 21:09:14 +02002460 u32 next_index, bi = 0, *to_next, map_index;
2461 vlib_buffer_t *b;
2462 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01002463 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002464 locator_set_t *loc_set;
2465 mapping_t *map;
2466 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002467 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002468 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002469
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002470 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002471
2472 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002473 pool_foreach(pmr, lcm->pending_map_requests_pool,
2474 ({
2475 if (!gid_address_cmp (&pmr->src, seid)
2476 && !gid_address_cmp (&pmr->dst, deid))
2477 {
2478 duplicate_pmr = pmr;
2479 break;
2480 }
2481 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002482 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002483
2484 if (!is_resend && duplicate_pmr)
2485 {
2486 /* don't send the request if there is a pending map request already */
2487 return 0;
2488 }
2489
Florin Corase127a7e2016-02-18 22:20:01 +01002490 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02002491 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01002492 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02002493 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2494 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002495 {
2496 clib_warning ("No local mapping found in eid-table for %U!",
2497 format_gid_address, seid);
2498 return -1;
2499 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02002500
2501 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2502
2503 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002504 {
2505 clib_warning
2506 ("Mapping found for src eid %U is not marked as local!",
2507 format_gid_address, seid);
2508 return -1;
2509 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002510 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02002511 }
2512 else
2513 {
2514 map_index = lcm->pitr_map_index;
2515 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002516 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002517 }
2518
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002519 /* overwrite locator set if map-request itr-rlocs configured */
2520 if (~0 != lcm->mreq_itr_rlocs)
2521 {
2522 ls_index = lcm->mreq_itr_rlocs;
2523 }
2524
2525 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002526
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002527 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002528 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002529 if (duplicate_pmr)
2530 duplicate_pmr->to_be_removed = 1;
2531 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002532 }
Florin Corasddfafb82016-05-13 18:09:56 +02002533
Florin Corase127a7e2016-02-18 22:20:01 +01002534 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002535 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02002536 &lcm->active_map_resolver,
2537 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01002538
2539 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002540 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01002541
Florin Corasf727db92016-06-23 15:01:58 +02002542 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02002543 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2544 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2545 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01002546
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002547 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002548
2549 /* Enqueue the packet */
2550 to_next = vlib_frame_vector_args (f);
2551 to_next[0] = bi;
2552 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002553 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01002554
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002555 if (duplicate_pmr)
2556 /* if there is a pending request already update it */
2557 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002558 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2559 {
2560 /* remove the oldest nonce */
2561 u64 CLIB_UNUSED (tmp), *nonce_del;
2562 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2563 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2564 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2565 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002566
Florin Corasf4691cd2016-08-15 19:16:32 +02002567 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002568 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002569 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002570 }
2571 else
2572 {
2573 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002574 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002575 memset (pmr, 0, sizeof (*pmr));
2576 gid_address_copy (&pmr->src, seid);
2577 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02002578 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002579 pmr->is_smr_invoked = is_smr_invoked;
2580 reset_pending_mr_counters (pmr);
2581 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002582 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002583 }
2584
2585 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002586}
2587
2588static void
Florin Corasa2157cf2016-08-16 21:09:14 +02002589get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01002590{
Florin Corasa2157cf2016-08-16 21:09:14 +02002591 ip4_header_t *ip4 = hdr;
2592 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01002593
2594 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2595 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002596 ip_address_set (src, &ip4->src_address, IP4);
2597 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01002598 }
2599 else
2600 {
2601 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002602 ip_address_set (src, &ip6->src_address, IP6);
2603 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01002604 }
2605}
2606
Filip Tehlar324112f2016-06-02 16:07:38 +02002607static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02002608lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02002609 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02002610{
Florin Corasa2157cf2016-08-16 21:09:14 +02002611 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002612 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002613
Florin Coras87e40692016-09-22 18:02:17 +02002614 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2615 IP4 ? FIB_PROTOCOL_IP4 :
2616 FIB_PROTOCOL_IP6),
2617 vnet_buffer
2618 (b)->sw_if_index
2619 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02002620
2621 vnip = hash_get (lcm->vni_by_table_id, table_id);
2622 if (vnip)
2623 vni = vnip[0];
2624 else
2625 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2626
2627 return vni;
2628}
2629
Florin Coras1a1adc72016-07-22 01:45:30 +02002630always_inline u32
2631lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2632{
Florin Corasa2157cf2016-08-16 21:09:14 +02002633 uword *vnip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002634 u32 vni = ~0;
2635 u32 sw_if_index0;
2636
Florin Corasa2157cf2016-08-16 21:09:14 +02002637 l2input_main_t *l2im = &l2input_main;
2638 l2_input_config_t *config;
2639 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02002640
Florin Corasa2157cf2016-08-16 21:09:14 +02002641 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2642 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02002643 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2644
2645 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2646 if (vnip)
2647 vni = vnip[0];
2648 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002649 clib_warning ("bridge domain %d is not mapped to any vni!",
2650 config->bd_index);
Florin Coras1a1adc72016-07-22 01:45:30 +02002651
2652 return vni;
2653}
2654
2655always_inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02002656get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2657 gid_address_t * src, gid_address_t * dst)
Florin Coras1a1adc72016-07-22 01:45:30 +02002658{
2659 u32 vni = 0;
2660 u16 type;
2661
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002662 memset (src, 0, sizeof (*src));
2663 memset (dst, 0, sizeof (*dst));
Florin Corasa2157cf2016-08-16 21:09:14 +02002664 type = vnet_buffer (b)->lisp.overlay_afi;
Florin Coras1a1adc72016-07-22 01:45:30 +02002665
2666 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2667 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002668 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002669 u8 version, preflen;
2670
Florin Corasa2157cf2016-08-16 21:09:14 +02002671 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2672 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02002673
2674 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02002675 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002676
Florin Corasa2157cf2016-08-16 21:09:14 +02002677 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02002678 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02002679 gid_address_ippref_len (src) = preflen;
2680 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02002681
2682 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2683 gid_address_vni (dst) = vni;
2684 gid_address_vni (src) = vni;
2685 }
2686 else if (LISP_AFI_MAC == type)
2687 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002688 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02002689
2690 eh = vlib_buffer_get_current (b);
2691
Florin Corasa2157cf2016-08-16 21:09:14 +02002692 gid_address_type (src) = GID_ADDR_MAC;
2693 gid_address_type (dst) = GID_ADDR_MAC;
2694 mac_copy (&gid_address_mac (src), eh->src_address);
2695 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02002696
2697 /* get vni */
2698 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2699
2700 gid_address_vni (dst) = vni;
2701 gid_address_vni (src) = vni;
2702 }
2703}
2704
Florin Corase127a7e2016-02-18 22:20:01 +01002705static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002706lisp_cp_lookup_inline (vlib_main_t * vm,
2707 vlib_node_runtime_t * node,
2708 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01002709{
Florin Corasa2157cf2016-08-16 21:09:14 +02002710 u32 *from, *to_next_drop, di, si;
2711 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002712 u32 pkts_mapped = 0;
2713 uword n_left_from, n_left_to_next_drop;
2714
2715 from = vlib_frame_vector_args (from_frame);
2716 n_left_from = from_frame->n_vectors;
2717
2718 while (n_left_from > 0)
2719 {
2720 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02002721 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01002722
2723 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002724 {
2725 u32 pi0;
2726 vlib_buffer_t *b0;
2727 gid_address_t src, dst;
Florin Corase127a7e2016-02-18 22:20:01 +01002728
Florin Corasa2157cf2016-08-16 21:09:14 +02002729 pi0 = from[0];
2730 from += 1;
2731 n_left_from -= 1;
2732 to_next_drop[0] = pi0;
2733 to_next_drop += 1;
2734 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002735
Florin Corasa2157cf2016-08-16 21:09:14 +02002736 b0 = vlib_get_buffer (vm, pi0);
2737 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002738 vnet_buffer (b0)->lisp.overlay_afi = overlay;
Florin Corase127a7e2016-02-18 22:20:01 +01002739
Florin Corasa2157cf2016-08-16 21:09:14 +02002740 /* src/dst eid pair */
2741 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
Filip Tehlar324112f2016-06-02 16:07:38 +02002742
Florin Corasa2157cf2016-08-16 21:09:14 +02002743 /* if we have remote mapping for destination already in map-chache
2744 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02002745 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2746 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02002747 if (~0 != di)
2748 {
2749 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2750 /* send a map-request also in case of negative mapping entry
2751 with corresponding action */
2752 if (m->action == LISP_SEND_MAP_REQUEST)
2753 {
2754 /* send map-request */
2755 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2756 0 /* is_resend */ );
2757 pkts_mapped++;
2758 }
2759 else
2760 {
2761 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2762 &src);
2763 if (~0 != si)
2764 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01002765 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02002766 }
2767 }
2768 }
2769 else
2770 {
2771 /* send map-request */
2772 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2773 0 /* is_resend */ );
2774 pkts_mapped++;
2775 }
Florin Corase127a7e2016-02-18 22:20:01 +01002776
Florin Corasa2157cf2016-08-16 21:09:14 +02002777 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2778 {
2779 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2780 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02002781
Florin Corasa2157cf2016-08-16 21:09:14 +02002782 memset (tr, 0, sizeof (*tr));
2783 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02002784 ip_address_copy (&tr->map_resolver_ip,
2785 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02002786 }
2787 gid_address_free (&dst);
2788 gid_address_free (&src);
2789 }
Florin Corase127a7e2016-02-18 22:20:01 +01002790
Florin Corasa2157cf2016-08-16 21:09:14 +02002791 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2792 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01002793 }
2794 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02002795 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2796 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01002797 return from_frame->n_vectors;
2798}
2799
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002800static uword
2801lisp_cp_lookup_ip4 (vlib_main_t * vm,
2802 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2803{
2804 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2805}
2806
2807static uword
2808lisp_cp_lookup_ip6 (vlib_main_t * vm,
2809 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2810{
2811 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2812}
2813
Neale Ranns5e575b12016-10-03 09:40:25 +01002814static uword
2815lisp_cp_lookup_l2 (vlib_main_t * vm,
2816 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2817{
2818 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2819}
2820
Florin Corasa2157cf2016-08-16 21:09:14 +02002821/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002822VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2823 .function = lisp_cp_lookup_ip4,
2824 .name = "lisp-cp-lookup-ip4",
2825 .vector_size = sizeof (u32),
2826 .format_trace = format_lisp_cp_lookup_trace,
2827 .type = VLIB_NODE_TYPE_INTERNAL,
2828
2829 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2830 .error_strings = lisp_cp_lookup_error_strings,
2831
2832 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2833
2834 .next_nodes = {
2835 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002836 },
2837};
2838/* *INDENT-ON* */
2839
2840/* *INDENT-OFF* */
2841VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2842 .function = lisp_cp_lookup_ip6,
2843 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01002844 .vector_size = sizeof (u32),
2845 .format_trace = format_lisp_cp_lookup_trace,
2846 .type = VLIB_NODE_TYPE_INTERNAL,
2847
2848 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2849 .error_strings = lisp_cp_lookup_error_strings,
2850
2851 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2852
2853 .next_nodes = {
2854 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns5e575b12016-10-03 09:40:25 +01002855 },
2856};
2857/* *INDENT-ON* */
2858
2859/* *INDENT-OFF* */
2860VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2861 .function = lisp_cp_lookup_l2,
2862 .name = "lisp-cp-lookup-l2",
2863 .vector_size = sizeof (u32),
2864 .format_trace = format_lisp_cp_lookup_trace,
2865 .type = VLIB_NODE_TYPE_INTERNAL,
2866
2867 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2868 .error_strings = lisp_cp_lookup_error_strings,
2869
2870 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2871
2872 .next_nodes = {
2873 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Florin Corase127a7e2016-02-18 22:20:01 +01002874 },
2875};
Florin Corasa2157cf2016-08-16 21:09:14 +02002876/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002877
2878/* lisp_cp_input statistics */
Filip Tehlarcda70662017-01-16 10:30:03 +01002879#define foreach_lisp_cp_input_error \
2880_(DROP, "drop") \
2881_(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2882_(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2883_(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
Florin Corase127a7e2016-02-18 22:20:01 +01002884_(MAP_REPLIES_RECEIVED, "map-replies received")
2885
Florin Corasa2157cf2016-08-16 21:09:14 +02002886static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01002887#define _(sym,string) string,
2888 foreach_lisp_cp_input_error
2889#undef _
2890};
2891
2892typedef enum
2893{
2894#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02002895 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01002896#undef _
2897 LISP_CP_INPUT_N_ERROR,
2898} lisp_cp_input_error_t;
2899
Florin Corase127a7e2016-02-18 22:20:01 +01002900typedef struct
2901{
2902 gid_address_t dst_eid;
2903 ip4_address_t map_resolver_ip;
2904} lisp_cp_input_trace_t;
2905
2906u8 *
2907format_lisp_cp_input_trace (u8 * s, va_list * args)
2908{
2909 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2910 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02002911 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
2912 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01002913
2914 s = format (s, "LISP-CP-INPUT: TODO");
2915 return s;
2916}
2917
Filip Tehlar9677a942016-11-28 10:23:31 +01002918static void
2919remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
2920{
2921 mapping_t *m;
Florin Corasf3dc11a2017-01-24 03:13:43 -08002922 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
2923 memset (adj_args, 0, sizeof (adj_args[0]));
Filip Tehlar9677a942016-11-28 10:23:31 +01002924
2925 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasf3dc11a2017-01-24 03:13:43 -08002926
2927 gid_address_copy (&adj_args->reid, &m->eid);
2928 adj_args->is_add = 0;
2929 if (vnet_lisp_add_del_adjacency (adj_args))
2930 clib_warning ("failed to del adjacency!");
2931
Filip Tehlar9677a942016-11-28 10:23:31 +01002932 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
2933 0 /* is_static */ , 0);
2934 mapping_delete_timer (lcm, mi);
2935}
2936
2937static void
2938mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
2939 f64 expiration_time)
2940{
2941 mapping_t *m;
2942 u64 now = clib_cpu_time_now ();
2943 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
2944 u64 exp_clock_time = now + expiration_time * cpu_cps;
2945
2946 m = pool_elt_at_index (lcm->mapping_pool, mi);
2947
2948 m->timer_set = 1;
2949 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
2950}
2951
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002952static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002953map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01002954{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002955 mapping_t *m;
2956 vec_foreach (m, a->mappings)
2957 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002958 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002959 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002960 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002961
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002962 clib_mem_free (a);
2963}
2964
2965void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002966process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002967{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002968 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002969 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2970 u32 dst_map_index = 0;
2971 pending_map_request_t *pmr;
2972 u64 *noncep;
2973 uword *pmr_index;
2974
2975 if (a->is_rloc_probe)
2976 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002977
Florin Corase127a7e2016-02-18 22:20:01 +01002978 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002979 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01002980 if (!pmr_index)
2981 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002982 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002983 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01002984 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002985 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002986
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002987 vec_foreach (m, a->mappings)
2988 {
2989 /* insert/update mappings cache */
2990 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
2991 m->authoritative, m->ttl,
2992 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002993
Florin Corasba888e42017-01-24 11:38:18 -08002994 if (dst_map_index == (u32) ~ 0)
2995 continue;
2996
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002997 /* try to program forwarding only if mapping saved or updated */
Florin Corasba888e42017-01-24 11:38:18 -08002998 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
2999 memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08003000
Florin Corasba888e42017-01-24 11:38:18 -08003001 gid_address_copy (&adj_args->leid, &pmr->src);
3002 gid_address_copy (&adj_args->reid, &m->eid);
3003 adj_args->is_add = 1;
3004 if (vnet_lisp_add_del_adjacency (adj_args))
3005 clib_warning ("failed to add adjacency!");
Florin Corasf3dc11a2017-01-24 03:13:43 -08003006
Florin Corasba888e42017-01-24 11:38:18 -08003007 if ((u32) ~ 0 != m->ttl)
3008 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003009 }
Florin Corase127a7e2016-02-18 22:20:01 +01003010
3011 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02003012
3013 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02003014 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003015 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02003016 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003017 /* *INDENT-ON* */
3018
3019 clib_fifo_free (pmr->nonces);
3020 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003021
3022done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003023 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003024 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003025}
3026
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003027static int
3028is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3029 lisp_key_type_t key_id, u8 * key)
3030{
3031 u8 *auth_data = 0;
3032 u16 auth_data_len;
3033 int result;
3034
3035 auth_data_len = auth_data_len_by_key_id (key_id);
3036 if ((u16) ~ 0 == auth_data_len)
3037 {
3038 clib_warning ("invalid length for key_id %d!", key_id);
3039 return 0;
3040 }
3041
3042 /* save auth data */
3043 vec_validate (auth_data, auth_data_len - 1);
3044 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3045
3046 /* clear auth data */
3047 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3048
3049 /* get hash of the message */
3050 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3051 (unsigned char *) h, msg_len, NULL, NULL);
3052
3053 result = memcmp (code, auth_data, auth_data_len);
3054
3055 vec_free (auth_data);
3056
3057 return !result;
3058}
3059
3060static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003061process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003062{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003063 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003064 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003065
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003066 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003067 if (!pmr_index)
3068 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003069 clib_warning ("No pending map-register entry with nonce %lu!",
3070 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003071 return;
3072 }
3073
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003074 map_records_arg_free (a);
3075 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3076}
3077
3078static mapping_t *
3079get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3080{
3081 u32 mi;
3082
3083 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3084 if (~0 == mi)
3085 {
3086 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3087 e);
3088 return 0;
3089 }
3090 return pool_elt_at_index (lcm->mapping_pool, mi);
3091}
3092
3093/**
3094 * When map-notify is received it is necessary that all EIDs in the record
3095 * list share common key. The key is then used to verify authentication
3096 * data in map-notify message.
3097 */
3098static int
3099map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3100 u32 key_id, u8 ** key_out)
3101{
3102 u32 i, len = vec_len (maps);
3103 mapping_t *m;
3104
3105 /* get key of the first mapping */
3106 m = get_mapping (lcm, &maps[0].eid);
3107 if (!m || !m->key)
3108 return -1;
3109
3110 key_out[0] = m->key;
3111
3112 for (i = 1; i < len; i++)
3113 {
3114 m = get_mapping (lcm, &maps[i].eid);
3115 if (!m || !m->key)
3116 return -1;
3117
3118 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3119 {
3120 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3121 return -1;
3122 }
3123 }
3124 return 0;
3125}
3126
3127static int
3128parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3129{
3130 locator_t *locators = 0;
3131 u32 i, len;
3132 gid_address_t deid;
3133 mapping_t m;
3134 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003135
3136 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003137 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003138 {
3139 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3140 if (len == ~0)
3141 {
3142 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003143 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003144 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003145 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003146 }
3147
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003148 m.locators = locators;
3149 gid_address_copy (&m.eid, &deid);
3150 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003151 }
3152
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003153 return 0;
3154}
3155
3156static map_records_arg_t *
3157parse_map_notify (vlib_buffer_t * b)
3158{
3159 int rc = 0;
3160 map_notify_hdr_t *mnotif_hdr;
3161 lisp_key_type_t key_id;
3162 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3163 u8 *key = 0;
3164 gid_address_t deid;
3165 u16 auth_data_len = 0;
3166 u8 record_count;
3167 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3168
3169 memset (a, 0, sizeof (*a));
3170 mnotif_hdr = vlib_buffer_get_current (b);
3171 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3172 memset (&deid, 0, sizeof (deid));
3173
3174 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3175 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3176 auth_data_len = auth_data_len_by_key_id (key_id);
3177
3178 /* advance buffer by authentication data */
3179 vlib_buffer_pull (b, auth_data_len);
3180
3181 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3182 rc = parse_map_records (b, a, record_count);
3183 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003184 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003185 map_records_arg_free (a);
3186 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003187 }
3188
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003189 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3190 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003191 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003192 map_records_arg_free (a);
3193 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003194 }
3195
3196 /* verify authentication data */
3197 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003198 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003199 {
3200 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003201 a->nonce);
3202 map_records_arg_free (a);
3203 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003204 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003205 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003206}
3207
3208static vlib_buffer_t *
3209build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3210 ip_address_t * dst, u64 nonce, u8 probe_bit,
3211 mapping_t * records, u16 dst_port, u32 * bi_res)
3212{
3213 vlib_buffer_t *b;
3214 u32 bi;
3215 vlib_main_t *vm = lcm->vlib_main;
3216
3217 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3218 {
3219 clib_warning ("Can't allocate buffer for Map-Register!");
3220 return 0;
3221 }
3222
3223 b = vlib_get_buffer (vm, bi);
3224
3225 /* leave some space for the encap headers */
3226 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3227
3228 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3229
3230 /* push outer ip header */
3231 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3232
3233 bi_res[0] = bi;
3234 return b;
3235}
3236
3237static int
3238send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3239 u8 probe_bit, u64 nonce, u16 dst_port,
3240 ip_address_t * probed_loc)
3241{
3242 ip_address_t src;
3243 u32 bi;
3244 vlib_buffer_t *b;
3245 vlib_frame_t *f;
3246 u32 next_index, *to_next;
3247 mapping_t *records = 0, *m;
3248
3249 m = pool_elt_at_index (lcm->mapping_pool, mi);
3250 if (!m)
3251 return -1;
3252
3253 vec_add1 (records, m[0]);
3254 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3255 memset (&src, 0, sizeof (src));
3256
3257 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3258 {
3259 clib_warning ("can't find inteface address for %U", format_ip_address,
3260 dst);
3261 return -1;
3262 }
3263
3264 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3265 &bi);
3266 if (!b)
3267 return -1;
3268 free_map_register_records (records);
3269
3270 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3271 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3272 ip4_lookup_node.index : ip6_lookup_node.index;
3273
3274 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3275
3276 /* Enqueue the packet */
3277 to_next = vlib_frame_vector_args (f);
3278 to_next[0] = bi;
3279 f->n_vectors = 1;
3280 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3281 return 0;
3282}
3283
Filip Tehlarb601f222017-01-02 10:22:56 +01003284static void
3285find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3286{
3287 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3288 if (start < 0 && start < -sizeof (b->pre_data))
3289 {
3290 *ip_hdr = 0;
3291 return;
3292 }
3293
3294 *ip_hdr = b->data + start;
3295 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3296 *ip_hdr = 0;
3297}
3298
Florin Corase127a7e2016-02-18 22:20:01 +01003299void
Filip Tehlarcda70662017-01-16 10:30:03 +01003300process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3301 lisp_cp_main_t * lcm, vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01003302{
Filip Tehlarb601f222017-01-02 10:22:56 +01003303 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003304 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3305 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02003306 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01003307 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003308 u64 nonce;
Filip Tehlarcda70662017-01-16 10:30:03 +01003309 u32 i, len = 0, rloc_probe_recv = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003310 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003311
3312 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003313 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02003314 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003315 clib_warning
3316 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01003317 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02003318 }
Florin Corase127a7e2016-02-18 22:20:01 +01003319
Filip Tehlarb601f222017-01-02 10:22:56 +01003320 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3321 nonce = MREQ_NONCE (mreq_hdr);
3322
Florin Corase127a7e2016-02-18 22:20:01 +01003323 /* parse src eid */
3324 len = lisp_msg_parse_addr (b, &src);
3325 if (len == ~0)
3326 return;
3327
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003328 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3329 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01003330 if (len == ~0)
Filip Tehlarcda70662017-01-16 10:30:03 +01003331 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003332
3333 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02003334 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01003335 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003336 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01003337 len = lisp_msg_parse_eid_rec (b, &dst);
3338 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003339 {
3340 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003341 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02003342 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003343
3344 if (MREQ_SMR (mreq_hdr))
3345 {
3346 /* send SMR-invoked map-requests */
3347 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3348 }
3349 else if (MREQ_RLOC_PROBE (mreq_hdr))
3350 {
Filip Tehlarb601f222017-01-02 10:22:56 +01003351 find_ip_header (b, &ip_hdr);
3352 if (!ip_hdr)
3353 {
3354 clib_warning ("Cannot find the IP header!");
Filip Tehlarcda70662017-01-16 10:30:03 +01003355 goto done;
Filip Tehlarb601f222017-01-02 10:22:56 +01003356 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003357 rloc_probe_recv++;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003358 memset (&m, 0, sizeof (m));
3359 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3360
3361 // TODO: select best locator; for now use the first one
3362 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3363
3364 /* get src/dst IP addresses */
3365 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3366
3367 // TODO get source port from buffer
3368 u16 src_port = LISP_CONTROL_PORT;
3369
3370 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3371 src_port, &probed_loc);
3372 }
Florin Corase127a7e2016-02-18 22:20:01 +01003373 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003374
3375done:
Filip Tehlarcda70662017-01-16 10:30:03 +01003376 vlib_node_increment_counter (vm, node->node_index,
3377 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3378 rloc_probe_recv);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003379 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003380}
3381
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003382static map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003383parse_map_reply (vlib_buffer_t * b)
3384{
3385 locator_t probed;
3386 gid_address_t deid;
3387 void *h;
3388 u32 i, len = 0;
3389 mapping_t m;
3390 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003391 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003392 memset (a, 0, sizeof (*a));
3393 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003394
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003395 mrep_hdr = vlib_buffer_get_current (b);
3396 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003397 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003398 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3399
3400 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3401 {
3402 memset (&m, 0, sizeof (m));
3403 locators = 0;
3404 h = vlib_buffer_get_current (b);
3405
3406 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3407 m.action = MAP_REC_ACTION (h);
3408 m.authoritative = MAP_REC_AUTH (h);
3409
3410 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3411 if (len == ~0)
3412 {
3413 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003414 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003415 return 0;
3416 }
3417
3418 m.locators = locators;
3419 gid_address_copy (&m.eid, &deid);
3420 vec_add1 (a->mappings, m);
3421 }
3422 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003423}
3424
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003425static void
3426queue_map_reply_for_processing (map_records_arg_t * a)
3427{
Florin Coras655fcc42017-01-10 08:57:54 -08003428 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003429}
3430
3431static void
3432queue_map_notify_for_processing (map_records_arg_t * a)
3433{
3434 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3435}
3436
Florin Corase127a7e2016-02-18 22:20:01 +01003437static uword
3438lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02003439 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01003440{
Filip Tehlarcda70662017-01-16 10:30:03 +01003441 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3442 map_notifies_recv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003443 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02003444 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003445 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003446
3447 from = vlib_frame_vector_args (from_frame);
3448 n_left_from = from_frame->n_vectors;
3449
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003450
Florin Corase127a7e2016-02-18 22:20:01 +01003451 while (n_left_from > 0)
3452 {
3453 u32 n_left_to_next_drop;
3454
3455 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003456 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003457 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003458 {
3459 u32 bi0;
3460 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01003461
Florin Corasa2157cf2016-08-16 21:09:14 +02003462 bi0 = from[0];
3463 from += 1;
3464 n_left_from -= 1;
3465 to_next_drop[0] = bi0;
3466 to_next_drop += 1;
3467 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003468
Florin Corasa2157cf2016-08-16 21:09:14 +02003469 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003470
Florin Corasa2157cf2016-08-16 21:09:14 +02003471 type = lisp_msg_type (vlib_buffer_get_current (b0));
3472 switch (type)
3473 {
3474 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003475 a = parse_map_reply (b0);
3476 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003477 {
3478 if (a->is_rloc_probe)
3479 rloc_probe_rep_recv++;
3480 queue_map_reply_for_processing (a);
3481 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003482 break;
3483 case LISP_MAP_REQUEST:
Filip Tehlarcda70662017-01-16 10:30:03 +01003484 process_map_request (vm, node, lcm, b0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003485 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003486 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003487 a = parse_map_notify (b0);
3488 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003489 {
3490 map_notifies_recv++;
3491 queue_map_notify_for_processing (a);
3492 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003493 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02003494 default:
3495 clib_warning ("Unsupported LISP message type %d", type);
3496 break;
3497 }
Florin Corase127a7e2016-02-18 22:20:01 +01003498
Florin Corasa2157cf2016-08-16 21:09:14 +02003499 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01003500
Florin Corasa2157cf2016-08-16 21:09:14 +02003501 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3502 {
Florin Corase127a7e2016-02-18 22:20:01 +01003503
Florin Corasa2157cf2016-08-16 21:09:14 +02003504 }
3505 }
Florin Corase127a7e2016-02-18 22:20:01 +01003506
Florin Corasa2157cf2016-08-16 21:09:14 +02003507 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3508 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003509 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003510 vlib_node_increment_counter (vm, node->node_index,
3511 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3512 rloc_probe_rep_recv);
3513 vlib_node_increment_counter (vm, node->node_index,
3514 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3515 map_notifies_recv);
Florin Corase127a7e2016-02-18 22:20:01 +01003516 return from_frame->n_vectors;
3517}
3518
Florin Corasa2157cf2016-08-16 21:09:14 +02003519/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003520VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3521 .function = lisp_cp_input,
3522 .name = "lisp-cp-input",
3523 .vector_size = sizeof (u32),
3524 .format_trace = format_lisp_cp_input_trace,
3525 .type = VLIB_NODE_TYPE_INTERNAL,
3526
3527 .n_errors = LISP_CP_INPUT_N_ERROR,
3528 .error_strings = lisp_cp_input_error_strings,
3529
3530 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3531
3532 .next_nodes = {
3533 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3534 },
3535};
Florin Corasa2157cf2016-08-16 21:09:14 +02003536/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003537
3538clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003539lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01003540{
Florin Corasa2157cf2016-08-16 21:09:14 +02003541 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3542 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003543
3544 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3545 return error;
3546
3547 lcm->im4 = &ip4_main;
3548 lcm->im6 = &ip6_main;
3549 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02003550 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003551 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02003552 lcm->lisp_pitr = 0;
Florin Corasba888e42017-01-24 11:38:18 -08003553 lcm->flags = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02003554 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01003555
3556 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003557 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02003558 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01003559
Florin Coras577c3552016-04-21 00:45:40 +02003560 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02003561 hash_set (lcm->table_id_by_vni, 0, 0);
3562 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02003563
Florin Corase127a7e2016-02-18 22:20:01 +01003564 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02003565 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003566 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02003567 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003568
Filip Tehlar9677a942016-11-28 10:23:31 +01003569 u64 now = clib_cpu_time_now ();
3570 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01003571 return 0;
3572}
3573
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003574static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02003575send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003576{
Florin Corasa2157cf2016-08-16 21:09:14 +02003577 map_request_args_t *a = arg;
3578 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003579
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003580 if (a->is_resend)
3581 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3582 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003583 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003584
3585 return 0;
3586}
3587
3588static int
3589queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02003590 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003591{
3592 map_request_args_t a;
3593
3594 a.is_resend = is_resend;
3595 gid_address_copy (&a.seid, seid);
3596 gid_address_copy (&a.deid, deid);
3597 a.smr_invoked = smr_invoked;
3598
3599 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02003600 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003601 return 0;
3602}
3603
3604/**
3605 * Take an action with a pending map request depending on expiration time
3606 * and re-try counters.
3607 */
3608static void
3609update_pending_request (pending_map_request_t * r, f64 dt)
3610{
Florin Corasa2157cf2016-08-16 21:09:14 +02003611 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003612 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003613
3614 if (r->time_to_expire - dt < 0)
3615 /* it's time to decide what to do with this pending request */
3616 {
3617 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02003618 /* too many retries -> assume current map resolver is not available */
3619 {
3620 mr = get_map_resolver (&lcm->active_map_resolver);
3621 if (!mr)
3622 {
3623 clib_warning ("Map resolver %U not found - probably deleted "
3624 "by the user recently.", format_ip_address,
3625 &lcm->active_map_resolver);
3626 }
3627 else
3628 {
3629 clib_warning ("map resolver %U is unreachable, ignoring",
3630 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003631
Florin Corasa2157cf2016-08-16 21:09:14 +02003632 /* mark current map resolver unavailable so it won't be
3633 * selected next time */
3634 mr->is_down = 1;
3635 mr->last_update = vlib_time_now (lcm->vlib_main);
3636 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003637
Florin Corasa2157cf2016-08-16 21:09:14 +02003638 reset_pending_mr_counters (r);
3639 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003640
Florin Corasa2157cf2016-08-16 21:09:14 +02003641 /* try to find a next eligible map resolver and re-send */
3642 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3643 1 /* resend */ );
3644 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003645 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003646 {
3647 /* try again */
3648 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3649 1 /* resend */ );
3650 r->retries_num++;
3651 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3652 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003653 }
3654 else
3655 r->time_to_expire -= dt;
3656}
3657
3658static void
3659remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3660{
Florin Corasa2157cf2016-08-16 21:09:14 +02003661 u64 *nonce;
3662 pending_map_request_t *pmr;
3663 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003664
Florin Corasa2157cf2016-08-16 21:09:14 +02003665 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003666 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02003667 ({
3668 if (pmr->to_be_removed)
3669 {
3670 clib_fifo_foreach (nonce, pmr->nonces, ({
3671 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3672 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003673
Florin Corasa2157cf2016-08-16 21:09:14 +02003674 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3675 }
3676 }));
3677 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003678
3679 vec_foreach (pmr_index, to_be_removed)
3680 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3681
3682 vec_free (to_be_removed);
3683}
3684
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003685static void
3686update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3687{
3688 static f64 time_left = RLOC_PROBING_INTERVAL;
3689
3690 if (!lcm->is_enabled || !lcm->rloc_probing)
3691 return;
3692
3693 time_left -= dt;
3694 if (time_left <= 0)
3695 {
3696 time_left = RLOC_PROBING_INTERVAL;
3697 send_rloc_probes (lcm);
3698 }
3699}
3700
3701static void
3702update_map_register (lisp_cp_main_t * lcm, f64 dt)
3703{
3704 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3705 static u64 mreg_sent_counter = 0;
3706
3707 if (!lcm->is_enabled || !lcm->map_registering)
3708 return;
3709
3710 time_left -= dt;
3711 if (time_left <= 0)
3712 {
3713 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3714 time_left = MAP_REGISTER_INTERVAL;
3715 else
3716 {
3717 mreg_sent_counter++;
3718 time_left = QUICK_MAP_REGISTER_INTERVAL;
3719 }
3720 send_map_register (lcm, 1 /* want map notify */ );
3721 }
3722}
3723
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003724static uword
3725send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003726 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003727{
Filip Tehlar9677a942016-11-28 10:23:31 +01003728 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003729 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003730 pending_map_request_t *pmr;
3731 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003732
3733 while (1)
3734 {
3735 vlib_process_wait_for_event_or_clock (vm, period);
3736
3737 /* currently no signals are expected - just wait for clock */
3738 (void) vlib_process_get_events (vm, 0);
3739
Florin Corasa2157cf2016-08-16 21:09:14 +02003740 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003741 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02003742 ({
3743 if (!pmr->to_be_removed)
3744 update_pending_request (pmr, period);
3745 }));
3746 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003747
3748 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003749
3750 update_map_register (lcm, period);
3751 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01003752
3753 u64 now = clib_cpu_time_now ();
3754
3755 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3756 if (vec_len (expired) > 0)
3757 {
3758 u32 *mi = 0;
3759 vec_foreach (mi, expired)
3760 {
3761 remove_expired_mapping (lcm, mi[0]);
3762 }
3763 _vec_len (expired) = 0;
3764 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003765 }
3766
3767 /* unreachable */
3768 return 0;
3769}
3770
Florin Corasa2157cf2016-08-16 21:09:14 +02003771/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003772VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3773 .function = send_map_resolver_service,
3774 .type = VLIB_NODE_TYPE_PROCESS,
3775 .name = "lisp-retry-service",
3776 .process_log2_n_stack_bytes = 16,
3777};
Florin Corasa2157cf2016-08-16 21:09:14 +02003778/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003779
Florin Corasa2157cf2016-08-16 21:09:14 +02003780VLIB_INIT_FUNCTION (lisp_cp_init);
3781
3782/*
3783 * fd.io coding-style-patch-verification: ON
3784 *
3785 * Local Variables:
3786 * eval: (c-set-style "gnu")
3787 * End:
3788 */