blob: ebbd1be2e37b3dc1a07c0b1ae4c9e4e970b831f5 [file] [log] [blame]
Florin Corase127a7e2016-02-18 22:20:01 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Filip Tehlara5abdeb2016-07-18 17:35:40 +020016#include <vlibmemory/api.h>
Florin Corase127a7e2016-02-18 22:20:01 +010017#include <vnet/lisp-cp/control.h>
18#include <vnet/lisp-cp/packets.h>
19#include <vnet/lisp-cp/lisp_msg_serdes.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010020#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
21#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Filip Tehlar4868ff62017-03-09 16:48:39 +010022#include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_table.h>
Florin Corase127a7e2016-02-18 22:20:01 +010025
Filip Tehlar397fd7d2016-10-26 14:31:24 +020026#include <openssl/evp.h>
27#include <openssl/hmac.h>
28
Florin Corasf3dc11a2017-01-24 03:13:43 -080029lisp_cp_main_t lisp_control_main;
30
Filip Tehlarcda70662017-01-16 10:30:03 +010031u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
32
33typedef enum
34{
35 LISP_CP_INPUT_NEXT_DROP,
36 LISP_CP_INPUT_N_NEXT,
37} lisp_cp_input_next_t;
38
Filip Tehlara5abdeb2016-07-18 17:35:40 +020039typedef struct
40{
41 u8 is_resend;
42 gid_address_t seid;
43 gid_address_t deid;
44 u8 smr_invoked;
45} map_request_args_t;
46
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010047typedef struct
48{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010049 u64 nonce;
Filip Tehlarfb9931f2016-12-09 13:52:38 +010050 u8 is_rloc_probe;
51 mapping_t *mappings;
52} map_records_arg_t;
53
Florin Corasdca88042016-09-14 16:01:38 +020054u8
55vnet_lisp_get_map_request_mode (void)
56{
57 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
58 return lcm->map_request_mode;
59}
60
Filip Tehlar397fd7d2016-10-26 14:31:24 +020061static u16
62auth_data_len_by_key_id (lisp_key_type_t key_id)
63{
64 switch (key_id)
65 {
66 case HMAC_SHA_1_96:
67 return SHA1_AUTH_DATA_LEN;
68 case HMAC_SHA_256_128:
69 return SHA256_AUTH_DATA_LEN;
70 default:
71 clib_warning ("unsupported key type: %d!", key_id);
72 return (u16) ~ 0;
73 }
74 return (u16) ~ 0;
75}
76
77static const EVP_MD *
78get_encrypt_fcn (lisp_key_type_t key_id)
79{
80 switch (key_id)
81 {
82 case HMAC_SHA_1_96:
83 return EVP_sha1 ();
84 case HMAC_SHA_256_128:
85 return EVP_sha256 ();
86 default:
87 clib_warning ("unsupported encryption key type: %d!", key_id);
88 break;
89 }
90 return 0;
91}
92
Filip Tehlara5abdeb2016-07-18 17:35:40 +020093static int
94queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020095 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020096
Florin Corasf727db92016-06-23 15:01:58 +020097ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020098ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
99 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +0200100{
101 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +0200102 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +0200103 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
104 sw_if_index = swif->unnumbered_sw_if_index;
105 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +0200106 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
107 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
108 (u32) ~ 0;
109 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +0200110}
Filip Tehlar215104e2016-05-10 16:58:29 +0200111
Florin Corasf727db92016-06-23 15:01:58 +0200112void *
113ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200114 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200115{
Florin Corasa2157cf2016-08-16 21:09:14 +0200116 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200117
Florin Corasf727db92016-06-23 15:01:58 +0200118 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
119 if (!ia)
120 return 0;
121 return ip_interface_address_get_address (lm, ia);
122}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200123
Florin Corase127a7e2016-02-18 22:20:01 +0100124int
Florin Corasf727db92016-06-23 15:01:58 +0200125ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200126 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200127{
Florin Corasa2157cf2016-08-16 21:09:14 +0200128 ip_lookup_main_t *lm;
129 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200130
131 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
132 addr = ip_interface_get_first_address (lm, sw_if_index, version);
133 if (!addr)
134 return 0;
135
136 ip_address_set (result, addr, version);
137 return 1;
138}
139
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100140/**
141 * convert from a LISP address to a FIB prefix
142 */
143void
144ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200145{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100146 if (addr->version == IP4)
147 {
148 prefix->fp_len = 32;
149 prefix->fp_proto = FIB_PROTOCOL_IP4;
150 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
151 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
152 }
Florin Corasf727db92016-06-23 15:01:58 +0200153 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100154 {
155 prefix->fp_len = 128;
156 prefix->fp_proto = FIB_PROTOCOL_IP6;
157 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
158 }
Florin Corasf727db92016-06-23 15:01:58 +0200159}
160
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161/**
162 * convert from a LISP to a FIB prefix
163 */
164void
165ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
166 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200167{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100168 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
169 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200170}
171
172/**
173 * Find the sw_if_index of the interface that would be used to egress towards
174 * dst.
175 */
176u32
177ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
178{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 fib_node_index_t fei;
180 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200181
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200183
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184 fei = fib_table_lookup (0, &prefix);
185
186 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200187}
188
189/**
190 * Find first IP of the interface that would be used to egress towards dst.
191 * Returns 1 if the address is found 0 otherwise.
192 */
193int
194ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200195 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200196{
197 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200198 ip_lookup_main_t *lm;
199 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200200 u8 ipver;
201
Florin Corasa2157cf2016-08-16 21:09:14 +0200202 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200203
Florin Corasa2157cf2016-08-16 21:09:14 +0200204 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200205
206 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100207 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200208
Florin Corasa2157cf2016-08-16 21:09:14 +0200209 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200210 return 0;
211
212 /* find the first ip address */
213 addr = ip_interface_get_first_address (lm, si, ipver);
214 if (0 == addr)
215 return 0;
216
217 ip_address_set (result, addr, ipver);
218 return 1;
219}
220
221static int
Florin Coras1a1adc72016-07-22 01:45:30 +0200222dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +0200223{
Neale Ranns5e575b12016-10-03 09:40:25 +0100224 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200225
Florin Coras1a1adc72016-07-22 01:45:30 +0200226 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200227 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200228 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200229
230 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200231 {
232 clib_warning ("vni %d not associated to a vrf!", vni);
233 return VNET_API_ERROR_INVALID_VALUE;
234 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200235 }
236 else
237 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200238 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200239 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200240 {
241 clib_warning ("vni %d not associated to a bridge domain!", vni);
242 return VNET_API_ERROR_INVALID_VALUE;
243 }
Florin Corasf727db92016-06-23 15:01:58 +0200244 }
245
Florin Corasf727db92016-06-23 15:01:58 +0200246 /* enable/disable data-plane interface */
247 if (is_add)
248 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100249 if (is_l2)
250 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
251 else
252 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200253 }
254 else
255 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100256 if (is_l2)
257 lisp_gpe_tenant_l2_iface_unlock (vni);
258 else
259 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200260 }
261
262 return 0;
263}
264
265static void
266dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
267{
Florin Corasa2157cf2016-08-16 21:09:14 +0200268 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
269 fwd_entry_t *fe = 0;
270 uword *feip = 0;
271 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200272
Florin Corasa2157cf2016-08-16 21:09:14 +0200273 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200274 if (!feip)
275 return;
276
Florin Corasa2157cf2016-08-16 21:09:14 +0200277 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200278
279 /* delete dp fwd entry */
280 u32 sw_if_index;
281 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200282 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200283 a->vni = gid_address_vni (&fe->reid);
284 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200285 if (fe->is_src_dst)
286 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200287
Filip Tehlar21511912017-04-07 10:41:42 +0200288 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200289 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
290
291 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200292 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
293 vec_free (fe->locator_pairs);
294 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200295}
296
297/**
298 * Finds first remote locator with best (lowest) priority that has a local
299 * peer locator with an underlying route to it.
300 *
301 */
302static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200303get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
304 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200305{
Florin Coras3590ac52016-08-08 16:04:26 +0200306 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200307 locator_set_t *rmt_ls, *lcl_ls;
308 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
309 locator_t *lp, *rmt = 0;
310 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200311 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200312
Florin Corasa2157cf2016-08-16 21:09:14 +0200313 rmt_ls =
314 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
315 lcl_ls =
316 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200317
Florin Corasa2157cf2016-08-16 21:09:14 +0200318 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200319 return 0;
320
Florin Coras3590ac52016-08-08 16:04:26 +0200321 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200322 {
323 rmt = 0;
324
325 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200326 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
327 {
328 if (0 != hash_get (checked, i))
329 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200330
Florin Corasa2157cf2016-08-16 21:09:14 +0200331 li = vec_elt (rmt_ls->locator_indices, i);
332 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200333
Florin Corasa2157cf2016-08-16 21:09:14 +0200334 /* we don't support non-IP locators for now */
335 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
336 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200337
Florin Corasa2157cf2016-08-16 21:09:14 +0200338 if ((found && lp->priority == limitp)
339 || (!found && lp->priority >= limitp))
340 {
341 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200342
Florin Corasa2157cf2016-08-16 21:09:14 +0200343 /* don't search for locators with lower priority and don't
344 * check this locator again*/
345 limitp = lp->priority;
346 hash_set (checked, i, 1);
347 break;
348 }
349 }
Florin Corasf727db92016-06-23 15:01:58 +0200350 /* check if a local locator with a route to remote locator exists */
351 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200352 {
353 /* find egress sw_if_index for rmt locator */
354 esi =
355 ip_fib_get_egress_iface_for_dst (lcm,
356 &gid_address_ip (&rmt->address));
357 if ((u32) ~ 0 == esi)
358 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200359
Florin Corasa2157cf2016-08-16 21:09:14 +0200360 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
361 {
362 li = vec_elt (lcl_ls->locator_indices, i);
363 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200364
Florin Corasa2157cf2016-08-16 21:09:14 +0200365 /* found local locator with the needed sw_if_index */
366 if (sl->sw_if_index == esi)
367 {
368 /* and it has an address */
369 if (0 == ip_interface_get_first_ip_address (lcm,
370 sl->sw_if_index,
371 gid_address_ip_version
372 (&rmt->address),
373 lcl_addr))
374 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200375
Florin Corasa2157cf2016-08-16 21:09:14 +0200376 memset (&pair, 0, sizeof (pair));
377 ip_address_copy (&pair.rmt_loc,
378 &gid_address_ip (&rmt->address));
379 ip_address_copy (&pair.lcl_loc, lcl_addr);
380 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100381 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200382 vec_add1 (locator_pairs[0], pair);
383 found = 1;
384 }
385 }
386 }
Florin Corasf727db92016-06-23 15:01:58 +0200387 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200388 break;
Florin Corasf727db92016-06-23 15:01:58 +0200389 }
Florin Coras3590ac52016-08-08 16:04:26 +0200390
Florin Corasa2157cf2016-08-16 21:09:14 +0200391 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200392 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200393}
394
395static void
Florin Corasdca88042016-09-14 16:01:38 +0200396gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
397 fid_address_t * fid)
398{
399 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
400
401 dst[0] = src[0];
402
403 switch (fid_addr_type (fid))
404 {
405 case FID_ADDR_IP_PREF:
406 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
407 gid_address_ippref (dst) = fid_addr_ippref (fid);
408 break;
409 case FID_ADDR_MAC:
410 gid_address_type (dst) = GID_ADDR_MAC;
411 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
412 break;
413 default:
414 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
415 break;
416 }
417}
418
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200419u8
420vnet_lisp_map_register_state_get (void)
421{
422 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
423 return lcm->map_registering;
424}
425
426u8
427vnet_lisp_rloc_probe_state_get (void)
428{
429 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
430 return lcm->rloc_probing;
431}
432
Florin Corasdca88042016-09-14 16:01:38 +0200433static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200434dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200435{
Florin Corasa2157cf2016-08-16 21:09:14 +0200436 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
Florin Corasba888e42017-01-24 11:38:18 -0800437 gid_address_t *rmt_eid, *lcl_eid;
438 mapping_t *lcl_map, *rmt_map;
Florin Corasf727db92016-06-23 15:01:58 +0200439 u32 sw_if_index;
Florin Corasa2157cf2016-08-16 21:09:14 +0200440 uword *feip = 0, *dpid;
441 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200442 u8 type, is_src_dst = 0;
Florin Corasba888e42017-01-24 11:38:18 -0800443 int rv;
Florin Corasf727db92016-06-23 15:01:58 +0200444
Florin Corasa2157cf2016-08-16 21:09:14 +0200445 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200446
447 /* remove entry if it already exists */
448 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
449 if (feip)
450 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
451
Florin Corasba888e42017-01-24 11:38:18 -0800452 /*
453 * Determine local mapping and eid
454 */
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200455 if (lcm->lisp_pitr)
Florin Corasba888e42017-01-24 11:38:18 -0800456 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200457 else
Florin Corasba888e42017-01-24 11:38:18 -0800458 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
459 lcl_eid = &lcl_map->eid;
Florin Corasf727db92016-06-23 15:01:58 +0200460
Florin Corasba888e42017-01-24 11:38:18 -0800461 /*
462 * Determine remote mapping and eid
463 */
464 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
465 rmt_eid = &rmt_map->eid;
466
467 /*
468 * Build and insert data plane forwarding entry
469 */
Florin Corasf727db92016-06-23 15:01:58 +0200470 a->is_add = 1;
471
Filip Tehlard5fcc462016-10-17 16:20:18 +0200472 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200473 {
Florin Corasba888e42017-01-24 11:38:18 -0800474 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
Filip Tehlard5fcc462016-10-17 16:20:18 +0200475 {
Florin Corasba888e42017-01-24 11:38:18 -0800476 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
477 &gid_address_sd_dst (rmt_eid));
478 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
479 &gid_address_sd_src (rmt_eid));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200480 }
481 else
482 {
Florin Corasba888e42017-01-24 11:38:18 -0800483 gid_address_copy (&a->rmt_eid, rmt_eid);
484 gid_address_copy (&a->lcl_eid, lcl_eid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200485 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200486 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200487 }
488 else
Florin Corasba888e42017-01-24 11:38:18 -0800489 gid_address_copy (&a->rmt_eid, rmt_eid);
Florin Corasdca88042016-09-14 16:01:38 +0200490
Florin Corasa2157cf2016-08-16 21:09:14 +0200491 a->vni = gid_address_vni (&a->rmt_eid);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100492 a->is_src_dst = is_src_dst;
Florin Coras1a1adc72016-07-22 01:45:30 +0200493
494 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200495 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200496 if (GID_ADDR_IP_PREFIX == type)
497 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200498 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200499 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200500 {
501 clib_warning ("vni %d not associated to a vrf!", a->vni);
502 return;
503 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200504 a->table_id = dpid[0];
505 }
506 else if (GID_ADDR_MAC == type)
507 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200508 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200509 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200510 {
511 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
512 return;
513 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200514 a->bd_id = dpid[0];
515 }
516
Florin Corasf727db92016-06-23 15:01:58 +0200517 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Corasba888e42017-01-24 11:38:18 -0800518 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
519
520 /* Either rmt mapping is negative or we can't find underlay path.
521 * Try again with petr if configured */
522 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
Florin Corasf727db92016-06-23 15:01:58 +0200523 {
Florin Corasba888e42017-01-24 11:38:18 -0800524 rmt_map = lisp_get_petr_mapping (lcm);
525 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
Florin Corasf727db92016-06-23 15:01:58 +0200526 }
527
Florin Corasba888e42017-01-24 11:38:18 -0800528 /* negative entry */
529 if (rv == 0)
530 {
531 a->is_negative = 1;
532 a->action = rmt_map->action;
533 }
Florin Corasf727db92016-06-23 15:01:58 +0200534
Filip Tehlar21511912017-04-07 10:41:42 +0200535 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
536 if (rv)
537 {
538 if (a->locator_pairs)
539 vec_free (a->locator_pairs);
540 return;
541 }
Florin Corasf727db92016-06-23 15:01:58 +0200542
Filip Tehlar21511912017-04-07 10:41:42 +0200543 /* add tunnel to fwd entry table */
Florin Corasf727db92016-06-23 15:01:58 +0200544 pool_get (lcm->fwd_entry_pool, fe);
Filip Tehlar21511912017-04-07 10:41:42 +0200545 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
546
Florin Corasbb5c22f2016-08-02 02:31:03 +0200547 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200548 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100549
550 if (is_src_dst)
551 gid_address_copy (&fe->leid, &a->lcl_eid);
552 else
Florin Corasba888e42017-01-24 11:38:18 -0800553 gid_address_copy (&fe->leid, lcl_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100554
Filip Tehlar69a9b762016-09-23 10:00:52 +0200555 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200556 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200557 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200558}
559
Filip Tehlarce1aae42017-01-04 10:42:25 +0100560typedef struct
561{
562 u32 si;
563 u32 di;
564} fwd_entry_mt_arg_t;
565
566static void *
567dp_add_fwd_entry_thread_fn (void *arg)
568{
569 fwd_entry_mt_arg_t *a = arg;
570 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
571 dp_add_fwd_entry (lcm, a->si, a->di);
572 return 0;
573}
574
575static int
576dp_add_fwd_entry_from_mt (u32 si, u32 di)
577{
578 fwd_entry_mt_arg_t a;
579
580 memset (&a, 0, sizeof (a));
581 a.si = si;
582 a.di = di;
583
584 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
585 (u8 *) & a, sizeof (a));
586 return 0;
587}
588
Florin Corasf727db92016-06-23 15:01:58 +0200589/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200590 * Returns vector of adjacencies.
591 *
592 * The caller must free the vector returned by this function.
593 *
594 * @param vni virtual network identifier
595 * @return vector of adjacencies
596 */
597lisp_adjacency_t *
598vnet_lisp_adjacencies_get_by_vni (u32 vni)
599{
600 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
601 fwd_entry_t *fwd;
602 lisp_adjacency_t *adjs = 0, adj;
603
604 /* *INDENT-OFF* */
605 pool_foreach(fwd, lcm->fwd_entry_pool,
606 ({
607 if (gid_address_vni (&fwd->reid) != vni)
608 continue;
609
610 gid_address_copy (&adj.reid, &fwd->reid);
611 gid_address_copy (&adj.leid, &fwd->leid);
612 vec_add1 (adjs, adj);
613 }));
614 /* *INDENT-ON* */
615
616 return adjs;
617}
618
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200619static lisp_msmr_t *
620get_map_server (ip_address_t * a)
621{
622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
623 lisp_msmr_t *m;
624
625 vec_foreach (m, lcm->map_servers)
626 {
627 if (!ip_address_cmp (&m->address, a))
628 {
629 return m;
630 }
631 }
632 return 0;
633}
634
635static lisp_msmr_t *
636get_map_resolver (ip_address_t * a)
637{
638 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
639 lisp_msmr_t *m;
640
641 vec_foreach (m, lcm->map_resolvers)
642 {
643 if (!ip_address_cmp (&m->address, a))
644 {
645 return m;
646 }
647 }
648 return 0;
649}
650
651int
652vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
653{
654 u32 i;
655 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
656 lisp_msmr_t _ms, *ms = &_ms;
657
658 if (vnet_lisp_enable_disable_status () == 0)
659 {
660 clib_warning ("LISP is disabled!");
661 return VNET_API_ERROR_LISP_DISABLED;
662 }
663
664 if (is_add)
665 {
666 if (get_map_server (addr))
667 {
668 clib_warning ("map-server %U already exists!", format_ip_address,
669 addr);
670 return -1;
671 }
672
673 memset (ms, 0, sizeof (*ms));
674 ip_address_copy (&ms->address, addr);
675 vec_add1 (lcm->map_servers, ms[0]);
676 }
677 else
678 {
679 for (i = 0; i < vec_len (lcm->map_servers); i++)
680 {
681 ms = vec_elt_at_index (lcm->map_servers, i);
682 if (!ip_address_cmp (&ms->address, addr))
683 {
684 vec_del1 (lcm->map_servers, i);
685 break;
686 }
687 }
688 }
689
690 return 0;
691}
692
Filip Tehlar69a9b762016-09-23 10:00:52 +0200693/**
Florin Corasf727db92016-06-23 15:01:58 +0200694 * Add/remove mapping to/from map-cache. Overwriting not allowed.
695 */
696int
697vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200698 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100699{
Florin Corasa2157cf2016-08-16 21:09:14 +0200700 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
701 u32 mi, *map_indexp, map_index, i;
702 mapping_t *m, *old_map;
703 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100704
Florin Corasf727db92016-06-23 15:01:58 +0200705 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200706 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100707 if (a->is_add)
708 {
709 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200710 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
711 {
712 clib_warning ("eid %U found in the eid-table", format_gid_address,
713 &a->eid);
714 return VNET_API_ERROR_VALUE_EXIST;
715 }
Florin Corase127a7e2016-02-18 22:20:01 +0100716
Florin Corasa2157cf2016-08-16 21:09:14 +0200717 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200718 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100719 m->locator_set_index = a->locator_set_index;
720 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200721 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100722 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200723 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200724 m->key = vec_dup (a->key);
725 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100726
727 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200728 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200729 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100730
Florin Corasa2157cf2016-08-16 21:09:14 +0200731 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
732 {
733 clib_warning ("Locator set with index %d doesn't exist",
734 a->locator_set_index);
735 return VNET_API_ERROR_INVALID_VALUE;
736 }
Florin Corase127a7e2016-02-18 22:20:01 +0100737
738 /* add eid to list of eids supported by locator-set */
739 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200740 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
741 a->locator_set_index);
742 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100743
744 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200745 {
746 /* mark as local */
747 vec_add1 (lcm->local_mappings_indexes, map_index);
748 }
Florin Corase127a7e2016-02-18 22:20:01 +0100749 map_index_result[0] = map_index;
750 }
751 else
752 {
Florin Coras577c3552016-04-21 00:45:40 +0200753 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200754 {
755 clib_warning ("eid %U not found in the eid-table",
756 format_gid_address, &a->eid);
757 return VNET_API_ERROR_INVALID_VALUE;
758 }
Florin Corase127a7e2016-02-18 22:20:01 +0100759
760 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200761 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
762 a->locator_set_index);
763 for (i = 0; i < vec_len (eid_indexes[0]); i++)
764 {
765 map_indexp = vec_elt_at_index (eid_indexes[0], i);
766 if (map_indexp[0] == mi)
767 break;
768 }
769 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100770
771 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200772 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100773 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200774 {
775 u32 k, *lm_indexp;
776 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
777 {
778 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
779 if (lm_indexp[0] == mi)
780 break;
781 }
782 vec_del1 (lcm->local_mappings_indexes, k);
783 }
Florin Corase127a7e2016-02-18 22:20:01 +0100784
785 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200786 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200787 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100788 pool_put_index (lcm->mapping_pool, mi);
789 }
790
791 return 0;
792}
793
Florin Corasf727db92016-06-23 15:01:58 +0200794/**
795 * Add/update/delete mapping to/in/from map-cache.
796 */
Florin Coras577c3552016-04-21 00:45:40 +0200797int
798vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200799 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200800{
Florin Corasa2157cf2016-08-16 21:09:14 +0200801 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200802 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200803 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200804
Florin Corasa2157cf2016-08-16 21:09:14 +0200805 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200806
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200807 if (vnet_lisp_enable_disable_status () == 0)
808 {
809 clib_warning ("LISP is disabled!");
810 return VNET_API_ERROR_LISP_DISABLED;
811 }
812
Florin Corasa2157cf2016-08-16 21:09:14 +0200813 vni = gid_address_vni (&a->eid);
814 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200815 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200816 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200817 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200818 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200819
Florin Coras1a1adc72016-07-22 01:45:30 +0200820 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200821 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200822 clib_warning ("vni %d not associated to a %s!", vni,
823 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200824 return VNET_API_ERROR_INVALID_VALUE;
825 }
826
Florin Corasf727db92016-06-23 15:01:58 +0200827 /* store/remove mapping from map-cache */
828 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200829}
830
Filip Tehlar324112f2016-06-02 16:07:38 +0200831int
Florin Coras1a1adc72016-07-22 01:45:30 +0200832vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +0200833{
Florin Corasa2157cf2016-08-16 21:09:14 +0200834 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
835 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +0200836
837 if (vnet_lisp_enable_disable_status () == 0)
838 {
839 clib_warning ("LISP is disabled!");
840 return -1;
841 }
842
Florin Coras1a1adc72016-07-22 01:45:30 +0200843 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
844 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
845
846 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +0200847 {
848 clib_warning ("can't add/del default vni-vrf mapping!");
849 return -1;
850 }
851
Florin Coras1a1adc72016-07-22 01:45:30 +0200852 dp_idp = hash_get (dp_table_by_vni[0], vni);
853 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200854
855 if (is_add)
856 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200857 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200858 {
859 clib_warning ("vni %d or vrf %d already used in vrf/vni "
860 "mapping!", vni, dp_id);
861 return -1;
862 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200863 hash_set (dp_table_by_vni[0], vni, dp_id);
864 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +0200865
866 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200867 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +0200868 }
869 else
870 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200871 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200872 {
873 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
874 "mapping!", vni, dp_id);
875 return -1;
876 }
Florin Corasf727db92016-06-23 15:01:58 +0200877 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200878 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlarfc568412017-04-05 10:06:03 +0200879
880 hash_unset (dp_table_by_vni[0], vni);
881 hash_unset (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200882 }
883 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +0200884
Filip Tehlar324112f2016-06-02 16:07:38 +0200885}
886
Florin Corasf727db92016-06-23 15:01:58 +0200887/* return 0 if the two locator sets are identical 1 otherwise */
888static u8
Florin Corasa2157cf2016-08-16 21:09:14 +0200889compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
890 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200891{
Florin Corasf727db92016-06-23 15:01:58 +0200892 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +0200893 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200894
Florin Corasa2157cf2016-08-16 21:09:14 +0200895 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +0200896 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200897
Florin Corasa2157cf2016-08-16 21:09:14 +0200898 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200899 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200900 old_li = vec_elt (old_ls_indexes, i);
901 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +0200902
Florin Corasa2157cf2016-08-16 21:09:14 +0200903 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +0200904
905 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +0200906 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200907 }
Florin Corasf727db92016-06-23 15:01:58 +0200908 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200909}
910
Filip Tehlard5fcc462016-10-17 16:20:18 +0200911typedef struct
912{
913 u8 is_negative;
914 void *lcm;
915 gid_address_t *eids_to_be_deleted;
916} remove_mapping_args_t;
917
918/**
919 * Callback invoked when a sub-prefix is found
920 */
921static void
922remove_mapping_if_needed (u32 mi, void *arg)
923{
924 u8 delete = 0;
925 remove_mapping_args_t *a = arg;
926 lisp_cp_main_t *lcm = a->lcm;
927 mapping_t *m;
928 locator_set_t *ls;
929
930 m = pool_elt_at_index (lcm->mapping_pool, mi);
931 if (!m)
932 return;
933
934 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
935
936 if (a->is_negative)
937 {
938 if (0 != vec_len (ls->locator_indices))
939 delete = 1;
940 }
941 else
942 {
943 if (0 == vec_len (ls->locator_indices))
944 delete = 1;
945 }
946
947 if (delete)
948 vec_add1 (a->eids_to_be_deleted, m->eid);
949}
950
951/**
952 * This function searches map cache and looks for IP prefixes that are subset
953 * of the provided one. If such prefix is found depending on 'is_negative'
954 * it does follows:
955 *
956 * 1) if is_negative is true and found prefix points to positive mapping,
957 * then the mapping is removed
958 * 2) if is_negative is false and found prefix points to negative mapping,
959 * then the mapping is removed
960 */
961static void
962remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
963 u8 is_negative)
964{
965 gid_address_t *e;
966 remove_mapping_args_t a;
Florin Corasf3dc11a2017-01-24 03:13:43 -0800967
Filip Tehlard5fcc462016-10-17 16:20:18 +0200968 memset (&a, 0, sizeof (a));
969
970 /* do this only in src/dst mode ... */
971 if (MR_MODE_SRC_DST != lcm->map_request_mode)
972 return;
973
974 /* ... and only for IP prefix */
975 if (GID_ADDR_SRC_DST != gid_address_type (eid)
976 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
977 return;
978
979 a.is_negative = is_negative;
980 a.lcm = lcm;
981
982 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
983 remove_mapping_if_needed, &a);
984
985 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100986 {
Florin Corasf3dc11a2017-01-24 03:13:43 -0800987 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
988
989 memset (adj_args, 0, sizeof (adj_args[0]));
990 gid_address_copy (&adj_args->reid, e);
991 adj_args->is_add = 0;
992 if (vnet_lisp_add_del_adjacency (adj_args))
993 clib_warning ("failed to del adjacency!");
994
Filip Tehlard5fcc462016-10-17 16:20:18 +0200995 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100996 }
Filip Tehlard5fcc462016-10-17 16:20:18 +0200997
998 vec_free (a.eids_to_be_deleted);
999}
1000
Filip Tehlar9677a942016-11-28 10:23:31 +01001001static void
1002mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1003{
1004 timing_wheel_delete (&lcm->wheel, mi);
1005}
1006
Filip Tehlara6bce492017-02-03 10:17:49 +01001007static int
1008is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1009{
1010 fib_node_index_t fei;
1011 fib_prefix_t prefix;
1012 fib_entry_flag_t flags;
1013
1014 ip_address_to_fib_prefix (addr, &prefix);
1015
1016 fei = fib_table_lookup (0, &prefix);
1017 flags = fib_entry_get_flags (fei);
1018 return (FIB_ENTRY_FLAG_LOCAL & flags);
1019}
1020
Filip Tehlar195bcee2016-05-13 17:37:35 +02001021/**
Florin Corasf727db92016-06-23 15:01:58 +02001022 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001023 *
Florin Coras71893ac2016-07-10 20:09:32 +02001024 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001025 * @param rlocs vector of remote locators
1026 * @param action action for negative map-reply
1027 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001028 * @param res_map_index the map-index that was created/updated/removed. It is
1029 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001030 * @param is_static used for distinguishing between statically learned
1031 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001032 * @return return code
1033 */
1034int
Florin Coras71893ac2016-07-10 20:09:32 +02001035vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001036 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001037 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001038{
Florin Corasa2157cf2016-08-16 21:09:14 +02001039 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1040 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1041 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001042 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001043 mapping_t *old_map;
Filip Tehlara6bce492017-02-03 10:17:49 +01001044 locator_t *loc;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001045
Florin Corasa2157cf2016-08-16 21:09:14 +02001046 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001047 {
1048 clib_warning ("LISP is disabled!");
1049 return VNET_API_ERROR_LISP_DISABLED;
1050 }
1051
Florin Corasf727db92016-06-23 15:01:58 +02001052 if (res_map_index)
1053 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001054
Florin Corasf727db92016-06-23 15:01:58 +02001055 memset (m_args, 0, sizeof (m_args[0]));
1056 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001057
Florin Corasf727db92016-06-23 15:01:58 +02001058 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001059
Florin Coras71893ac2016-07-10 20:09:32 +02001060 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001061 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001062
1063 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001064 {
Filip Tehlar272c69e2017-02-15 16:40:35 +01001065 /* check if none of the locators match localy configured address */
1066 vec_foreach (loc, rlocs)
1067 {
1068 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1069 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1070 {
1071 clib_warning ("RLOC %U matches a local address!",
1072 format_gid_address, &loc->address);
1073 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1074 }
1075 }
1076
Florin Corasf727db92016-06-23 15:01:58 +02001077 /* overwrite: if mapping already exists, decide if locators should be
1078 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001079 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001080 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001081 if (!is_static && (old_map->is_static || old_map->local))
1082 {
1083 /* do not overwrite local or static remote mappings */
1084 clib_warning ("mapping %U rejected due to collision with local "
1085 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001086 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001087 return 0;
1088 }
1089
Florin Corasa2157cf2016-08-16 21:09:14 +02001090 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001091
Florin Corasa2157cf2016-08-16 21:09:14 +02001092 /* update mapping attributes */
1093 old_map->action = action;
1094 old_map->authoritative = authoritative;
1095 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001096
Florin Corasa2157cf2016-08-16 21:09:14 +02001097 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1098 old_map->locator_set_index);
1099 if (compare_locators (lcm, old_ls->locator_indices,
1100 ls_args->locators))
1101 {
1102 /* set locator-set index to overwrite */
1103 ls_args->is_add = 1;
1104 ls_args->index = old_map->locator_set_index;
1105 vnet_lisp_add_del_locator_set (ls_args, 0);
1106 if (res_map_index)
1107 res_map_index[0] = mi;
1108 }
1109 }
Florin Corasf727db92016-06-23 15:01:58 +02001110 /* new mapping */
1111 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001112 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001113 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1114
Florin Corasa2157cf2016-08-16 21:09:14 +02001115 ls_args->is_add = 1;
1116 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001117
Florin Corasa2157cf2016-08-16 21:09:14 +02001118 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001119
Florin Corasa2157cf2016-08-16 21:09:14 +02001120 /* add mapping */
1121 gid_address_copy (&m_args->eid, eid);
1122 m_args->is_add = 1;
1123 m_args->action = action;
1124 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001125 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001126 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001127 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001128
Florin Corasa2157cf2016-08-16 21:09:14 +02001129 if (res_map_index)
1130 res_map_index[0] = dst_map_index;
1131 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001132 }
Florin Corasf727db92016-06-23 15:01:58 +02001133 else
1134 {
Florin Coras71893ac2016-07-10 20:09:32 +02001135 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001136 {
1137 clib_warning ("cannot delete mapping for eid %U",
1138 format_gid_address, eid);
1139 return -1;
1140 }
Florin Corasf727db92016-06-23 15:01:58 +02001141
1142 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001143 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001144 m_args->locator_set_index = old_map->locator_set_index;
1145
1146 /* delete mapping associated from map-cache */
1147 vnet_lisp_map_cache_add_del (m_args, 0);
1148
1149 ls_args->is_add = 0;
1150 ls_args->index = old_map->locator_set_index;
1151 /* delete locator set */
1152 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001153
Filip Tehlar9677a942016-11-28 10:23:31 +01001154 /* delete timer associated to the mapping if any */
1155 if (old_map->timer_set)
1156 mapping_delete_timer (lcm, mi);
1157
Filip Tehlarb93222f2016-07-07 09:58:08 +02001158 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001159 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001160 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001161 }
1162
Filip Tehlar195bcee2016-05-13 17:37:35 +02001163 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001164 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001165}
1166
Filip Tehlar58f886a2016-05-30 15:57:40 +02001167int
Florin Corasf727db92016-06-23 15:01:58 +02001168vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001169{
1170 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001171 u32 mi, *map_indices = 0, *map_indexp;
1172 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1173 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1174 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001175
Florin Corasa2157cf2016-08-16 21:09:14 +02001176 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001177 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001178 ({
1179 vec_add1 (map_indices, mi);
1180 }));
1181 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001182
1183 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001184 {
1185 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1186 if (!map->local)
1187 {
1188 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001189
Florin Corasa2157cf2016-08-16 21:09:14 +02001190 dm_args->is_add = 0;
1191 gid_address_copy (&dm_args->eid, &map->eid);
1192 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001193
Florin Corasa2157cf2016-08-16 21:09:14 +02001194 /* delete mapping associated to fwd entry */
1195 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001196
Florin Corasa2157cf2016-08-16 21:09:14 +02001197 ls->is_add = 0;
1198 ls->local = 0;
1199 ls->index = map->locator_set_index;
1200 /* delete locator set */
1201 rv = vnet_lisp_add_del_locator_set (ls, 0);
1202 if (rv != 0)
1203 goto cleanup;
1204 }
1205 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001206
1207cleanup:
1208 if (map_indices)
1209 vec_free (map_indices);
1210 return rv;
1211}
1212
Filip Tehlar195bcee2016-05-13 17:37:35 +02001213/**
Florin Coras71893ac2016-07-10 20:09:32 +02001214 * Adds adjacency or removes forwarding entry associated to remote mapping.
1215 * Note that adjacencies are not stored, they only result in forwarding entries
1216 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001217 */
Florin Corasf3dc11a2017-01-24 03:13:43 -08001218int
1219vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
Florin Corasf727db92016-06-23 15:01:58 +02001220{
Florin Corasf3dc11a2017-01-24 03:13:43 -08001221 lisp_cp_main_t *lcm = &lisp_control_main;
Florin Coras71893ac2016-07-10 20:09:32 +02001222 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001223
1224 if (vnet_lisp_enable_disable_status () == 0)
1225 {
1226 clib_warning ("LISP is disabled!");
1227 return VNET_API_ERROR_LISP_DISABLED;
1228 }
1229
Filip Tehlar69a9b762016-09-23 10:00:52 +02001230 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
Florin Corasf3dc11a2017-01-24 03:13:43 -08001231 &a->reid, &a->leid);
Florin Coras71893ac2016-07-10 20:09:32 +02001232 if (GID_LOOKUP_MISS == remote_mi)
1233 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001234 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001235 format_gid_address, &a->reid);
Florin Corasf727db92016-06-23 15:01:58 +02001236
Florin Coras71893ac2016-07-10 20:09:32 +02001237 return -1;
1238 }
1239
Florin Corasf3dc11a2017-01-24 03:13:43 -08001240 if (a->is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001241 {
Florin Corasf727db92016-06-23 15:01:58 +02001242 /* check if source eid has an associated mapping. If pitr mode is on,
1243 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001244 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasf3dc11a2017-01-24 03:13:43 -08001245 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001246
Florin Coras71893ac2016-07-10 20:09:32 +02001247 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001248 {
1249 clib_warning ("Local eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001250 format_gid_address, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001251
Florin Corasa2157cf2016-08-16 21:09:14 +02001252 return -1;
1253 }
Florin Corasf727db92016-06-23 15:01:58 +02001254
Florin Coras71893ac2016-07-10 20:09:32 +02001255 /* update forwarding */
1256 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001257 }
1258 else
Florin Coras71893ac2016-07-10 20:09:32 +02001259 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001260
1261 return 0;
1262}
1263
1264int
Florin Corasdca88042016-09-14 16:01:38 +02001265vnet_lisp_set_map_request_mode (u8 mode)
1266{
1267 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1268
1269 if (vnet_lisp_enable_disable_status () == 0)
1270 {
1271 clib_warning ("LISP is disabled!");
1272 return VNET_API_ERROR_LISP_DISABLED;
1273 }
1274
1275 if (mode >= _MR_MODE_MAX)
1276 {
1277 clib_warning ("Invalid LISP map request mode %d!", mode);
1278 return VNET_API_ERROR_INVALID_ARGUMENT;
1279 }
1280
1281 lcm->map_request_mode = mode;
1282 return 0;
1283}
1284
Filip Tehlar53f09e32016-05-19 14:25:44 +02001285int
1286vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1287{
Florin Corasa2157cf2016-08-16 21:09:14 +02001288 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001289 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001290 mapping_t *m;
1291 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001292
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001293 if (vnet_lisp_enable_disable_status () == 0)
1294 {
1295 clib_warning ("LISP is disabled!");
1296 return VNET_API_ERROR_LISP_DISABLED;
1297 }
1298
Filip Tehlar53f09e32016-05-19 14:25:44 +02001299 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1300 if (!p)
1301 {
1302 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1303 return -1;
1304 }
1305 locator_set_index = p[0];
1306
1307 if (is_add)
1308 {
1309 pool_get (lcm->mapping_pool, m);
1310 m->locator_set_index = locator_set_index;
1311 m->local = 1;
Filip Tehlar38206ee2017-02-20 17:31:57 +01001312 m->pitr_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001313 lcm->pitr_map_index = m - lcm->mapping_pool;
1314
1315 /* enable pitr mode */
1316 lcm->lisp_pitr = 1;
1317 }
1318 else
1319 {
1320 /* remove pitr mapping */
1321 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1322
1323 /* disable pitr mode */
1324 lcm->lisp_pitr = 0;
1325 }
1326 return 0;
1327}
1328
Florin Corasba888e42017-01-24 11:38:18 -08001329/**
1330 * Configure Proxy-ETR
1331 *
1332 * @param ip PETR's IP address
1333 * @param is_add Flag that indicates if this is an addition or removal
1334 *
1335 * return 0 on success
1336 */
1337int
1338vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1339{
1340 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1341 u32 ls_index = ~0;
1342 mapping_t *m;
1343 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1344 locator_t loc;
1345
1346 if (vnet_lisp_enable_disable_status () == 0)
1347 {
1348 clib_warning ("LISP is disabled!");
1349 return VNET_API_ERROR_LISP_DISABLED;
1350 }
1351
1352 memset (ls_args, 0, sizeof (*ls_args));
1353
1354 if (is_add)
1355 {
1356 /* Create dummy petr locator-set */
Florin Corasb69259a2017-03-09 14:39:54 -08001357 memset (&loc, 0, sizeof (loc));
Florin Corasba888e42017-01-24 11:38:18 -08001358 gid_address_from_ip (&loc.address, ip);
1359 loc.priority = 1;
1360 loc.state = loc.weight = 1;
Florin Coras2743cc42017-01-29 16:42:20 -08001361 loc.local = 0;
Florin Corasba888e42017-01-24 11:38:18 -08001362
1363 ls_args->is_add = 1;
1364 ls_args->index = ~0;
1365 vec_add1 (ls_args->locators, loc);
1366 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1367
1368 /* Add petr mapping */
1369 pool_get (lcm->mapping_pool, m);
1370 m->locator_set_index = ls_index;
1371 lcm->petr_map_index = m - lcm->mapping_pool;
1372
1373 /* Enable use-petr */
1374 lcm->flags |= LISP_FLAG_USE_PETR;
1375 }
1376 else
1377 {
1378 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1379
1380 /* Remove petr locator */
1381 ls_args->is_add = 0;
1382 ls_args->index = m->locator_set_index;
1383 vnet_lisp_add_del_locator_set (ls_args, 0);
1384
1385 /* Remove petr mapping */
1386 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1387
1388 /* Disable use-petr */
1389 lcm->flags &= ~LISP_FLAG_USE_PETR;
1390 }
1391 return 0;
1392}
1393
Florin Corase127a7e2016-02-18 22:20:01 +01001394/* cleans locator to locator-set data and removes locators not part of
1395 * any locator-set */
1396static void
1397clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1398{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001399 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001400 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1401 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01001402 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001403 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1404 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1405 loc_indexp[0]);
1406 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1407 {
1408 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1409 if (ls_indexp[0] == lsi)
1410 break;
1411 }
Florin Corase127a7e2016-02-18 22:20:01 +01001412
Florin Corasa2157cf2016-08-16 21:09:14 +02001413 /* delete index for removed locator-set */
1414 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01001415
1416 /* delete locator if it's part of no locator-set */
1417 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001418 {
1419 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1420 vec_add1 (to_be_deleted, i);
1421 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001422 }
1423
1424 if (to_be_deleted)
1425 {
1426 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02001427 {
1428 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1429 vec_del1 (ls->locator_indices, loc_indexp[0]);
1430 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001431 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01001432 }
1433}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001434
Florin Corasf727db92016-06-23 15:01:58 +02001435static inline uword *
1436get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001437{
Florin Corasa2157cf2016-08-16 21:09:14 +02001438 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001439
Florin Corasa2157cf2016-08-16 21:09:14 +02001440 ASSERT (a != NULL);
1441 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001442
1443 /* find locator-set */
1444 if (a->local)
1445 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001446 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001447 }
1448 else
1449 {
1450 *p = a->index;
1451 }
1452
1453 return p;
1454}
1455
Florin Corasf727db92016-06-23 15:01:58 +02001456static inline int
1457is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02001458 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001459{
Florin Corasa2157cf2016-08-16 21:09:14 +02001460 locator_t *itloc;
1461 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001462
Florin Corasa2157cf2016-08-16 21:09:14 +02001463 ASSERT (ls != NULL);
1464 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001465
Florin Corasa2157cf2016-08-16 21:09:14 +02001466 vec_foreach (locit, ls->locator_indices)
1467 {
1468 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1469 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1470 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1471 {
1472 clib_warning ("Duplicate locator");
1473 return VNET_API_ERROR_VALUE_EXIST;
1474 }
1475 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001476
1477 return 0;
1478}
1479
Florin Corasf727db92016-06-23 15:01:58 +02001480static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02001481remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1482 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001483{
Florin Corasa2157cf2016-08-16 21:09:14 +02001484 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1485 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001486
Florin Corasa2157cf2016-08-16 21:09:14 +02001487 ASSERT (ls != NULL);
1488 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001489
Florin Corasa2157cf2016-08-16 21:09:14 +02001490 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1491 pool_put_index (lcm->locator_pool, locit[0]);
1492 vec_del1 (ls->locator_indices, loc_id);
1493 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001494}
1495
1496int
1497vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001498 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001499{
Florin Corasa2157cf2016-08-16 21:09:14 +02001500 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1501 locator_t *loc = NULL, *itloc = NULL;
1502 uword _p = (u32) ~ 0, *p = &_p;
1503 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001504 u32 loc_id = ~0;
1505 int ret = 0;
1506
Florin Corasa2157cf2016-08-16 21:09:14 +02001507 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001508
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001509 if (vnet_lisp_enable_disable_status () == 0)
1510 {
1511 clib_warning ("LISP is disabled!");
1512 return VNET_API_ERROR_LISP_DISABLED;
1513 }
1514
Florin Corasa2157cf2016-08-16 21:09:14 +02001515 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001516 if (!p)
1517 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001518 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001519 return VNET_API_ERROR_INVALID_ARGUMENT;
1520 }
1521
1522 if (ls == 0)
1523 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001524 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001525 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001526 {
1527 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1528 p[0]);
1529 return VNET_API_ERROR_INVALID_ARGUMENT;
1530 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001531 }
1532
1533 if (a->is_add)
1534 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001535 if (ls_result)
1536 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001537
Florin Corasa2157cf2016-08-16 21:09:14 +02001538 /* allocate locators */
1539 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001540 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001541 ret = is_locator_in_locator_set (lcm, ls, itloc);
1542 if (0 != ret)
1543 {
1544 return ret;
1545 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001546
Florin Corasa2157cf2016-08-16 21:09:14 +02001547 pool_get (lcm->locator_pool, loc);
1548 loc[0] = itloc[0];
1549 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001550
Florin Corasa2157cf2016-08-16 21:09:14 +02001551 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001552
Florin Corasa2157cf2016-08-16 21:09:14 +02001553 vec_validate (lcm->locator_to_locator_sets, loc_index);
1554 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1555 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001556 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001557 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001558 }
1559 else
1560 {
1561 ls_index = p[0];
1562
1563 itloc = a->locators;
1564 loc_id = 0;
1565 vec_foreach (locit, ls->locator_indices)
1566 {
1567 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1568
1569 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1570 {
1571 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1572 }
1573 if (0 == loc->local &&
1574 !gid_address_cmp (&loc->address, &itloc->address))
1575 {
1576 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1577 }
1578
1579 loc_id++;
1580 }
1581 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001582
1583 return 0;
1584}
1585
Florin Corase127a7e2016-02-18 22:20:01 +01001586int
1587vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001588 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01001589{
Florin Corasa2157cf2016-08-16 21:09:14 +02001590 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1591 locator_set_t *ls;
1592 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001593 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001594 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001595 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001596
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001597 if (vnet_lisp_enable_disable_status () == 0)
1598 {
1599 clib_warning ("LISP is disabled!");
1600 return VNET_API_ERROR_LISP_DISABLED;
1601 }
1602
Florin Corase127a7e2016-02-18 22:20:01 +01001603 if (a->is_add)
1604 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001605 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01001606
1607 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02001608 if (p && p[0] != (u32) ~ 0)
1609 {
1610 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1611 if (!ls)
1612 {
1613 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1614 p[0]);
1615 return -1;
1616 }
Florin Corase127a7e2016-02-18 22:20:01 +01001617
Florin Corasa2157cf2016-08-16 21:09:14 +02001618 /* clean locator to locator-set vectors and remove locators if
1619 * they're not part of another locator-set */
1620 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001621
Florin Corasa2157cf2016-08-16 21:09:14 +02001622 /* remove locator indices from locator set */
1623 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01001624
Florin Corasa2157cf2016-08-16 21:09:14 +02001625 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01001626
Florin Corasa2157cf2016-08-16 21:09:14 +02001627 if (ls_result)
1628 ls_result[0] = p[0];
1629 }
Florin Corase127a7e2016-02-18 22:20:01 +01001630 /* new locator-set */
1631 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001632 {
1633 pool_get (lcm->locator_set_pool, ls);
1634 memset (ls, 0, sizeof (*ls));
1635 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01001636
Florin Corasa2157cf2016-08-16 21:09:14 +02001637 if (a->local)
1638 {
1639 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01001640
Florin Corasa2157cf2016-08-16 21:09:14 +02001641 if (!lcm->locator_set_index_by_name)
1642 lcm->locator_set_index_by_name = hash_create_vec (
1643 /* size */
1644 0,
1645 sizeof
1646 (ls->name
1647 [0]),
1648 sizeof
1649 (uword));
1650 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1651 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01001652
Florin Corasa2157cf2016-08-16 21:09:14 +02001653 /* mark as local locator-set */
1654 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1655 }
1656 ls->local = a->local;
1657 if (ls_result)
1658 ls_result[0] = ls_index;
1659 }
Florin Corase127a7e2016-02-18 22:20:01 +01001660
Florin Corasa2157cf2016-08-16 21:09:14 +02001661 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001662 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02001663 {
1664 return ret;
1665 }
Florin Corase127a7e2016-02-18 22:20:01 +01001666 }
1667 else
1668 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001669 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001670 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001671 {
1672 clib_warning ("locator-set %v doesn't exists", a->name);
1673 return -1;
1674 }
Florin Corase127a7e2016-02-18 22:20:01 +01001675
Florin Corasa2157cf2016-08-16 21:09:14 +02001676 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001677 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001678 {
1679 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1680 return -1;
1681 }
Florin Corase127a7e2016-02-18 22:20:01 +01001682
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001683 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02001684 {
1685 clib_warning ("Can't delete the locator-set used to constrain "
1686 "the itr-rlocs in map-requests!");
1687 return -1;
1688 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001689
Florin Corasa2157cf2016-08-16 21:09:14 +02001690 if (vec_len (lcm->locator_set_to_eids) != 0)
1691 {
1692 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1693 if (vec_len (eid_indexes[0]) != 0)
1694 {
1695 clib_warning
1696 ("Can't delete a locator that supports a mapping!");
1697 return -1;
1698 }
1699 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001700
1701 /* clean locator to locator-sets data */
1702 clean_locator_to_locator_set (lcm, p[0]);
1703
1704 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02001705 {
1706 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001707
Florin Corasa2157cf2016-08-16 21:09:14 +02001708 vec_foreach_index (it, lcm->local_locator_set_indexes)
1709 {
1710 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1711 if (lsi == p[0])
1712 {
1713 vec_del1 (lcm->local_locator_set_indexes, it);
1714 break;
1715 }
1716 }
1717 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1718 }
1719 vec_free (ls->name);
1720 vec_free (ls->locator_indices);
1721 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001722 }
1723 return 0;
1724}
1725
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001726int
1727vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1728{
1729 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1730
1731 lcm->rloc_probing = is_enable;
1732 return 0;
1733}
1734
1735int
1736vnet_lisp_map_register_enable_disable (u8 is_enable)
1737{
1738 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1739
1740 lcm->map_registering = is_enable;
1741 return 0;
1742}
1743
Filip Tehlar46d4e362016-05-09 09:39:26 +02001744clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02001745vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001746{
Florin Coras1a1adc72016-07-22 01:45:30 +02001747 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02001748 clib_error_t *error = 0;
1749 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1750 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001751
Florin Corasf727db92016-06-23 15:01:58 +02001752 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001753 error = vnet_lisp_gpe_enable_disable (a);
1754 if (error)
1755 {
1756 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02001757 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02001758 }
1759
Florin Corasf727db92016-06-23 15:01:58 +02001760 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001761 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001762 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02001763
1764 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02001765 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1766 dp_add_del_iface(lcm, vni, 0, 1);
1767 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02001768 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1769 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02001770 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02001771 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02001772 }
1773 else
1774 {
Florin Corasf727db92016-06-23 15:01:58 +02001775 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02001776 hash_free (lcm->fwd_entry_by_mapping_index);
1777 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02001778 }
1779
1780 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02001781 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001782
1783 return 0;
1784}
1785
Filip Tehlar46d4e362016-05-09 09:39:26 +02001786u8
1787vnet_lisp_enable_disable_status (void)
1788{
Florin Corasa2157cf2016-08-16 21:09:14 +02001789 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02001790 return lcm->is_enabled;
1791}
1792
Florin Corase127a7e2016-02-18 22:20:01 +01001793int
1794vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1795{
Florin Corasa2157cf2016-08-16 21:09:14 +02001796 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01001797 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001798 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01001799
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001800 if (vnet_lisp_enable_disable_status () == 0)
1801 {
1802 clib_warning ("LISP is disabled!");
1803 return VNET_API_ERROR_LISP_DISABLED;
1804 }
1805
Florin Corase127a7e2016-02-18 22:20:01 +01001806 if (a->is_add)
1807 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001808
1809 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02001810 {
1811 clib_warning ("map-resolver %U already exists!", format_ip_address,
1812 &a->address);
1813 return -1;
1814 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001815
1816 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02001817 ip_address_copy (&mr->address, &a->address);
1818 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001819
1820 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02001821 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01001822 }
1823 else
1824 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001825 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1826 {
1827 mr = vec_elt_at_index (lcm->map_resolvers, i);
1828 if (!ip_address_cmp (&mr->address, &a->address))
1829 {
1830 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1831 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001832
Florin Corasa2157cf2016-08-16 21:09:14 +02001833 vec_del1 (lcm->map_resolvers, i);
1834 break;
1835 }
1836 }
Florin Corase127a7e2016-02-18 22:20:01 +01001837 }
1838 return 0;
1839}
1840
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001841int
1842vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1843{
Florin Corasa2157cf2016-08-16 21:09:14 +02001844 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1845 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001846
Florin Corasf727db92016-06-23 15:01:58 +02001847 if (vnet_lisp_enable_disable_status () == 0)
1848 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001849 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02001850 return VNET_API_ERROR_LISP_DISABLED;
1851 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001852
1853 if (a->is_add)
1854 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001855 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001856 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001857 {
1858 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1859 return VNET_API_ERROR_INVALID_ARGUMENT;
1860 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001861
1862 lcm->mreq_itr_rlocs = p[0];
1863 }
1864 else
1865 {
1866 lcm->mreq_itr_rlocs = ~0;
1867 }
1868
1869 return 0;
1870}
1871
Florin Corase127a7e2016-02-18 22:20:01 +01001872/* Statistics (not really errors) */
1873#define foreach_lisp_cp_lookup_error \
1874_(DROP, "drop") \
1875_(MAP_REQUESTS_SENT, "map-request sent")
1876
Florin Corasa2157cf2016-08-16 21:09:14 +02001877static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01001878#define _(sym,string) string,
1879 foreach_lisp_cp_lookup_error
1880#undef _
1881};
1882
1883typedef enum
1884{
1885#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02001886 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01001887#undef _
1888 LISP_CP_LOOKUP_N_ERROR,
1889} lisp_cp_lookup_error_t;
1890
1891typedef enum
1892{
1893 LISP_CP_LOOKUP_NEXT_DROP,
Florin Corase127a7e2016-02-18 22:20:01 +01001894 LISP_CP_LOOKUP_N_NEXT,
1895} lisp_cp_lookup_next_t;
1896
1897typedef struct
1898{
1899 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02001900 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01001901} lisp_cp_lookup_trace_t;
1902
1903u8 *
1904format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1905{
1906 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1907 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02001908 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01001909
1910 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02001911 format_ip_address, &t->map_resolver_ip, format_gid_address,
1912 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01001913 return s;
1914}
1915
Florin Coras1c494842016-06-16 21:08:56 +02001916int
1917get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02001918 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02001919{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001920 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02001921 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01001922
Florin Corasa2157cf2016-08-16 21:09:14 +02001923 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01001924 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001925 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02001926 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001927 }
1928
Florin Corasddfafb82016-05-13 18:09:56 +02001929 /* find the first mr ip we have a route to and the ip of the
1930 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02001931 vec_foreach (mrit, lcm->map_resolvers)
1932 {
1933 a = &mrit->address;
1934 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1935 {
1936 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001937
Florin Corasa2157cf2016-08-16 21:09:14 +02001938 /* also update globals */
1939 return 1;
1940 }
1941 }
Florin Corasddfafb82016-05-13 18:09:56 +02001942
Florin Corasa2157cf2016-08-16 21:09:14 +02001943 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02001944 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02001945}
Filip Tehlar254b0362016-04-07 10:04:34 +02001946
Filip Tehlarbeceab92016-04-20 17:21:55 +02001947static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02001948build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1949{
Florin Corasa2157cf2016-08-16 21:09:14 +02001950 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02001951 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02001952 locator_t *loc;
1953 u32 *loc_indexp;
1954 ip_interface_address_t *ia = 0;
1955 gid_address_t gid_data, *gid = &gid_data;
1956 gid_address_t *rlocs = 0;
1957 ip_prefix_t *ippref = &gid_address_ippref (gid);
1958 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02001959
Filip Tehlar324112f2016-06-02 16:07:38 +02001960 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02001961 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02001962 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02001963 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001964 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02001965 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1966
Filip Tehlar254b0362016-04-07 10:04:34 +02001967 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02001968
1969 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02001970 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1971 loc->sw_if_index, 1 /* unnumbered */,
1972 ({
Florin Coras1c494842016-06-16 21:08:56 +02001973 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1974 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02001975 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001976 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02001977 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02001978 }));
1979
Filip Tehlar254b0362016-04-07 10:04:34 +02001980 /* Add ipv6 locators */
1981 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1982 loc->sw_if_index, 1 /* unnumbered */,
1983 ({
Florin Coras1c494842016-06-16 21:08:56 +02001984 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1985 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02001986 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001987 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02001988 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02001989 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02001990 /* *INDENT-ON* */
1991
Filip Tehlar254b0362016-04-07 10:04:34 +02001992 }
1993 return rlocs;
1994}
1995
Florin Corase127a7e2016-02-18 22:20:01 +01001996static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001997build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
1998 ip_address_t * sloc, ip_address_t * rloc,
1999 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2000{
2001 vlib_buffer_t *b;
2002 u32 bi;
2003 vlib_main_t *vm = lcm->vlib_main;
2004
2005 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2006 {
2007 clib_warning ("Can't allocate buffer for Map-Request!");
2008 return 0;
2009 }
2010
2011 b = vlib_get_buffer (vm, bi);
2012
2013 /* leave some space for the encap headers */
2014 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2015
2016 /* put lisp msg */
2017 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2018 1 /* rloc probe */ , nonce_res);
2019
2020 /* push outer ip header */
2021 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2022 rloc);
2023
2024 bi_res[0] = bi;
2025
2026 return b;
2027}
2028
2029static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002030build_encapsulated_map_request (lisp_cp_main_t * lcm,
2031 gid_address_t * seid, gid_address_t * deid,
2032 locator_set_t * loc_set, ip_address_t * mr_ip,
2033 ip_address_t * sloc, u8 is_smr_invoked,
2034 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01002035{
Florin Corasa2157cf2016-08-16 21:09:14 +02002036 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01002037 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02002038 gid_address_t *rlocs = 0;
2039 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01002040
2041 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2042 {
2043 clib_warning ("Can't allocate buffer for Map-Request!");
2044 return 0;
2045 }
2046
2047 b = vlib_get_buffer (vm, bi);
2048
2049 /* leave some space for the encap headers */
2050 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2051
Filip Tehlar254b0362016-04-07 10:04:34 +02002052 /* get rlocs */
2053 rlocs = build_itr_rloc_list (lcm, loc_set);
2054
Filip Tehlard5fcc462016-10-17 16:20:18 +02002055 if (MR_MODE_SRC_DST == lcm->map_request_mode
2056 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02002057 {
2058 gid_address_t sd;
2059 memset (&sd, 0, sizeof (sd));
2060 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002061 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2062 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002063 }
2064 else
2065 {
2066 /* put lisp msg */
2067 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002068 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002069 }
Florin Corase127a7e2016-02-18 22:20:01 +01002070
2071 /* push ecm: udp-ip-lisp */
2072 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2073
Florin Corase127a7e2016-02-18 22:20:01 +01002074 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02002075 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02002076 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01002077
2078 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02002079
Florin Corasa2157cf2016-08-16 21:09:14 +02002080 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01002081 return b;
2082}
2083
2084static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002085reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01002086{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002087 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2088 r->retries_num = 0;
2089}
2090
2091static int
2092elect_map_resolver (lisp_cp_main_t * lcm)
2093{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002094 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002095
2096 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02002097 {
2098 if (!mr->is_down)
2099 {
2100 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2101 lcm->do_map_resolver_election = 0;
2102 return 1;
2103 }
2104 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002105 return 0;
2106}
2107
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002108static void
2109free_map_register_records (mapping_t * maps)
2110{
2111 mapping_t *map;
2112 vec_foreach (map, maps) vec_free (map->locators);
2113
2114 vec_free (maps);
2115}
2116
2117static void
2118add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2119 ip_address_t * probed_loc)
2120{
2121 u32 *li;
2122 locator_t *loc, new;
2123 ip_interface_address_t *ia = 0;
2124 void *addr;
2125 ip_address_t *new_ip = &gid_address_ip (&new.address);
2126
2127 m->locators = 0;
2128 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2129 locator_set_index);
2130 vec_foreach (li, ls->locator_indices)
2131 {
2132 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2133 new = loc[0];
2134 if (loc->local)
2135 {
2136 /* *INDENT-OFF* */
2137 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2138 loc->sw_if_index, 1 /* unnumbered */,
2139 ({
2140 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2141 ia);
2142 ip_address_set (new_ip, addr, IP4);
2143 }));
2144
2145 /* Add ipv6 locators */
2146 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2147 loc->sw_if_index, 1 /* unnumbered */,
2148 ({
2149 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2150 ia);
2151 ip_address_set (new_ip, addr, IP6);
2152 }));
2153 /* *INDENT-ON* */
2154
2155 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2156 new.probed = 1;
2157 }
2158 vec_add1 (m->locators, new);
2159 }
2160}
2161
2162static mapping_t *
2163build_map_register_record_list (lisp_cp_main_t * lcm)
2164{
2165 mapping_t *recs = 0, rec, *m;
2166
2167 /* *INDENT-OFF* */
2168 pool_foreach(m, lcm->mapping_pool,
2169 {
2170 /* for now build only local mappings */
2171 if (!m->local)
2172 continue;
2173
2174 rec = m[0];
2175 add_locators (lcm, &rec, m->locator_set_index, NULL);
2176 vec_add1 (recs, rec);
2177 });
2178 /* *INDENT-ON* */
2179
2180 return recs;
2181}
2182
2183static int
2184update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2185 lisp_key_type_t key_id, u8 * key,
2186 u16 auth_data_len, u32 msg_len)
2187{
2188 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2189 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2190
2191 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2192 (unsigned char *) map_reg_hdr, msg_len, NULL,
2193 NULL);
2194 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2195
2196 return 0;
2197}
2198
2199static vlib_buffer_t *
2200build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2201 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2202 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2203 u32 * bi_res)
2204{
2205 void *map_reg_hdr;
2206 vlib_buffer_t *b;
2207 u32 bi, auth_data_len = 0, msg_len = 0;
2208 vlib_main_t *vm = lcm->vlib_main;
2209
2210 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2211 {
2212 clib_warning ("Can't allocate buffer for Map-Register!");
2213 return 0;
2214 }
2215
2216 b = vlib_get_buffer (vm, bi);
2217
2218 /* leave some space for the encap headers */
2219 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2220
2221 auth_data_len = auth_data_len_by_key_id (key_id);
2222 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2223 auth_data_len, nonce_res,
2224 &msg_len);
2225
2226 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2227 msg_len);
2228
2229 /* push outer ip header */
2230 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2231 ms_ip);
2232
2233 bi_res[0] = bi;
2234 return b;
2235}
2236
2237static int
2238get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2239{
2240 lisp_msmr_t *mr;
2241 while (lcm->do_map_resolver_election
2242 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2243 &lcm->active_map_resolver,
2244 ip)))
2245 {
2246 if (0 == elect_map_resolver (lcm))
2247 /* all map resolvers are down */
2248 {
2249 /* restart MR checking by marking all of them up */
2250 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2251 return -1;
2252 }
2253 }
2254 return 0;
2255}
2256
Filip Tehlarcda70662017-01-16 10:30:03 +01002257/* CP output statistics */
2258#define foreach_lisp_cp_output_error \
2259_(MAP_REGISTERS_SENT, "map-registers sent") \
2260_(RLOC_PROBES_SENT, "rloc-probes sent")
2261
2262static char *lisp_cp_output_error_strings[] = {
2263#define _(sym,string) string,
2264 foreach_lisp_cp_output_error
2265#undef _
2266};
2267
2268typedef enum
2269{
2270#define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2271 foreach_lisp_cp_output_error
2272#undef _
2273 LISP_CP_OUTPUT_N_ERROR,
2274} lisp_cp_output_error_t;
2275
2276static uword
2277lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2278 vlib_frame_t * from_frame)
2279{
2280 return 0;
2281}
2282
2283/* dummy node used only for statistics */
2284/* *INDENT-OFF* */
2285VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2286 .function = lisp_cp_output,
2287 .name = "lisp-cp-output",
2288 .vector_size = sizeof (u32),
2289 .format_trace = format_lisp_cp_input_trace,
2290 .type = VLIB_NODE_TYPE_INTERNAL,
2291
2292 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2293 .error_strings = lisp_cp_output_error_strings,
2294
2295 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2296
2297 .next_nodes = {
2298 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2299 },
2300};
2301/* *INDENT-ON* */
2302
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002303static int
2304send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2305 u32 local_locator_set_index, ip_address_t * sloc,
2306 ip_address_t * rloc)
2307{
2308 locator_set_t *ls;
2309 u32 bi;
2310 vlib_buffer_t *b;
2311 vlib_frame_t *f;
2312 u64 nonce = 0;
2313 u32 next_index, *to_next;
2314 gid_address_t *itr_rlocs;
2315
2316 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2317 itr_rlocs = build_itr_rloc_list (lcm, ls);
2318
2319 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2320 vec_free (itr_rlocs);
2321 if (!b)
2322 return -1;
2323
2324 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2325
Filip Tehlar272c69e2017-02-15 16:40:35 +01002326 next_index = (ip_addr_version (rloc) == IP4) ?
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002327 ip4_lookup_node.index : ip6_lookup_node.index;
2328
2329 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2330
2331 /* Enqueue the packet */
2332 to_next = vlib_frame_vector_args (f);
2333 to_next[0] = bi;
2334 f->n_vectors = 1;
2335 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2336
2337 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2338 return 0;
2339}
2340
2341static int
2342send_rloc_probes (lisp_cp_main_t * lcm)
2343{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002344 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002345 mapping_t *lm;
2346 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002347 locator_pair_t *lp;
Filip Tehlarcda70662017-01-16 10:30:03 +01002348 u32 si, rloc_probes_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002349
2350 /* *INDENT-OFF* */
2351 pool_foreach (e, lcm->fwd_entry_pool,
2352 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002353 if (vec_len (e->locator_pairs) == 0)
2354 continue;
2355
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002356 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2357 if (~0 == si)
2358 {
2359 clib_warning ("internal error: cannot find local eid %U in "
2360 "map-cache!", format_gid_address, &e->leid);
2361 continue;
2362 }
2363 lm = pool_elt_at_index (lcm->mapping_pool, si);
2364
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002365 /* get the best (lowest) priority */
2366 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002367
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002368 /* send rloc-probe for pair(s) with the best remote locator priority */
2369 vec_foreach (lp, e->locator_pairs)
2370 {
2371 if (lp->priority != lprio)
2372 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002373
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002374 /* get first remote locator */
2375 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2376 &lp->rmt_loc);
Filip Tehlarcda70662017-01-16 10:30:03 +01002377 rloc_probes_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002378 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002379 });
2380 /* *INDENT-ON* */
2381
Filip Tehlarcda70662017-01-16 10:30:03 +01002382 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2383 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2384 rloc_probes_sent);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002385 return 0;
2386}
2387
2388static int
2389send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2390{
Filip Tehlarcda70662017-01-16 10:30:03 +01002391 u32 bi, map_registers_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002392 vlib_buffer_t *b;
2393 ip_address_t sloc;
2394 vlib_frame_t *f;
2395 u64 nonce = 0;
2396 u32 next_index, *to_next;
2397 ip_address_t *ms = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002398 mapping_t *records, *r, *g;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002399
2400 // TODO: support multiple map servers and do election
2401 if (0 == vec_len (lcm->map_servers))
2402 return -1;
2403
2404 ms = &lcm->map_servers[0].address;
2405
2406 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2407 {
2408 clib_warning ("no eligible interface address found for %U!",
2409 format_ip_address, &lcm->map_servers[0]);
2410 return -1;
2411 }
2412
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002413 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002414 if (!records)
2415 return -1;
2416
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002417 vec_foreach (r, records)
2418 {
2419 u8 *key = r->key;
2420 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002421
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002422 if (!key)
2423 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002424
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002425 g = 0;
2426 // TODO: group mappings that share common key
2427 vec_add1 (g, r[0]);
2428 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
2429 key_id, key, &bi);
2430 vec_free (g);
2431 if (!b)
2432 continue;
2433
2434 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2435
2436 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2437 ip4_lookup_node.index : ip6_lookup_node.index;
2438
2439 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2440
2441 /* Enqueue the packet */
2442 to_next = vlib_frame_vector_args (f);
2443 to_next[0] = bi;
2444 f->n_vectors = 1;
2445 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Filip Tehlarcda70662017-01-16 10:30:03 +01002446 map_registers_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002447
2448 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2449 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002450 free_map_register_records (records);
2451
Filip Tehlarcda70662017-01-16 10:30:03 +01002452 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2453 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2454 map_registers_sent);
2455
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002456 return 0;
2457}
2458
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002459#define send_encapsulated_map_request(lcm, seid, deid, smr) \
2460 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2461
2462#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2463 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2464
2465static int
Florin Corasa2157cf2016-08-16 21:09:14 +02002466_send_encapsulated_map_request (lisp_cp_main_t * lcm,
2467 gid_address_t * seid, gid_address_t * deid,
2468 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002469{
Florin Corasa2157cf2016-08-16 21:09:14 +02002470 u32 next_index, bi = 0, *to_next, map_index;
2471 vlib_buffer_t *b;
2472 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01002473 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002474 locator_set_t *loc_set;
2475 mapping_t *map;
2476 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002477 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002478 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002479
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002480 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002481
2482 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002483 pool_foreach(pmr, lcm->pending_map_requests_pool,
2484 ({
2485 if (!gid_address_cmp (&pmr->src, seid)
2486 && !gid_address_cmp (&pmr->dst, deid))
2487 {
2488 duplicate_pmr = pmr;
2489 break;
2490 }
2491 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002492 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002493
2494 if (!is_resend && duplicate_pmr)
2495 {
2496 /* don't send the request if there is a pending map request already */
2497 return 0;
2498 }
2499
Florin Corase127a7e2016-02-18 22:20:01 +01002500 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02002501 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01002502 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02002503 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2504 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002505 {
2506 clib_warning ("No local mapping found in eid-table for %U!",
2507 format_gid_address, seid);
2508 return -1;
2509 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02002510
2511 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2512
2513 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002514 {
2515 clib_warning
2516 ("Mapping found for src eid %U is not marked as local!",
2517 format_gid_address, seid);
2518 return -1;
2519 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002520 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02002521 }
2522 else
2523 {
2524 map_index = lcm->pitr_map_index;
2525 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002526 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002527 }
2528
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002529 /* overwrite locator set if map-request itr-rlocs configured */
2530 if (~0 != lcm->mreq_itr_rlocs)
2531 {
2532 ls_index = lcm->mreq_itr_rlocs;
2533 }
2534
2535 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002536
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002537 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002538 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002539 if (duplicate_pmr)
2540 duplicate_pmr->to_be_removed = 1;
2541 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002542 }
Florin Corasddfafb82016-05-13 18:09:56 +02002543
Florin Corase127a7e2016-02-18 22:20:01 +01002544 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002545 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02002546 &lcm->active_map_resolver,
2547 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01002548
2549 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002550 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01002551
Florin Corasf727db92016-06-23 15:01:58 +02002552 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02002553 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2554 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2555 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01002556
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002557 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002558
2559 /* Enqueue the packet */
2560 to_next = vlib_frame_vector_args (f);
2561 to_next[0] = bi;
2562 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002563 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01002564
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002565 if (duplicate_pmr)
2566 /* if there is a pending request already update it */
2567 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002568 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2569 {
2570 /* remove the oldest nonce */
2571 u64 CLIB_UNUSED (tmp), *nonce_del;
2572 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2573 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2574 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2575 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002576
Florin Corasf4691cd2016-08-15 19:16:32 +02002577 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002578 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002579 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002580 }
2581 else
2582 {
2583 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002584 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002585 memset (pmr, 0, sizeof (*pmr));
2586 gid_address_copy (&pmr->src, seid);
2587 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02002588 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002589 pmr->is_smr_invoked = is_smr_invoked;
2590 reset_pending_mr_counters (pmr);
2591 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002592 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002593 }
2594
2595 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002596}
2597
2598static void
Florin Corasa2157cf2016-08-16 21:09:14 +02002599get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01002600{
Florin Corasa2157cf2016-08-16 21:09:14 +02002601 ip4_header_t *ip4 = hdr;
2602 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01002603
2604 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2605 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002606 ip_address_set (src, &ip4->src_address, IP4);
2607 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01002608 }
2609 else
2610 {
2611 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002612 ip_address_set (src, &ip6->src_address, IP6);
2613 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01002614 }
2615}
2616
Filip Tehlar324112f2016-06-02 16:07:38 +02002617static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02002618lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02002619 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02002620{
Florin Corasa2157cf2016-08-16 21:09:14 +02002621 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002622 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002623
Florin Coras87e40692016-09-22 18:02:17 +02002624 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2625 IP4 ? FIB_PROTOCOL_IP4 :
2626 FIB_PROTOCOL_IP6),
2627 vnet_buffer
2628 (b)->sw_if_index
2629 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02002630
2631 vnip = hash_get (lcm->vni_by_table_id, table_id);
2632 if (vnip)
2633 vni = vnip[0];
2634 else
2635 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2636
2637 return vni;
2638}
2639
Florin Coras1a1adc72016-07-22 01:45:30 +02002640always_inline u32
2641lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2642{
Florin Corasa2157cf2016-08-16 21:09:14 +02002643 uword *vnip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002644 u32 vni = ~0;
2645 u32 sw_if_index0;
2646
Florin Corasa2157cf2016-08-16 21:09:14 +02002647 l2input_main_t *l2im = &l2input_main;
2648 l2_input_config_t *config;
2649 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02002650
Florin Corasa2157cf2016-08-16 21:09:14 +02002651 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2652 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02002653 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2654
2655 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2656 if (vnip)
2657 vni = vnip[0];
2658 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002659 clib_warning ("bridge domain %d is not mapped to any vni!",
2660 config->bd_index);
Florin Coras1a1adc72016-07-22 01:45:30 +02002661
2662 return vni;
2663}
2664
Filip Tehlar4868ff62017-03-09 16:48:39 +01002665void
Florin Corasa2157cf2016-08-16 21:09:14 +02002666get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Filip Tehlar4868ff62017-03-09 16:48:39 +01002667 gid_address_t * src, gid_address_t * dst,
2668 u16 type)
Florin Coras1a1adc72016-07-22 01:45:30 +02002669{
2670 u32 vni = 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02002671
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002672 memset (src, 0, sizeof (*src));
2673 memset (dst, 0, sizeof (*dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002674
2675 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2676 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002677 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002678 u8 version, preflen;
2679
Florin Corasa2157cf2016-08-16 21:09:14 +02002680 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2681 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02002682
2683 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02002684 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002685
Florin Corasa2157cf2016-08-16 21:09:14 +02002686 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02002687 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02002688 gid_address_ippref_len (src) = preflen;
2689 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02002690
2691 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2692 gid_address_vni (dst) = vni;
2693 gid_address_vni (src) = vni;
2694 }
2695 else if (LISP_AFI_MAC == type)
2696 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002697 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02002698
2699 eh = vlib_buffer_get_current (b);
2700
Florin Corasa2157cf2016-08-16 21:09:14 +02002701 gid_address_type (src) = GID_ADDR_MAC;
2702 gid_address_type (dst) = GID_ADDR_MAC;
2703 mac_copy (&gid_address_mac (src), eh->src_address);
2704 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02002705
2706 /* get vni */
2707 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2708
2709 gid_address_vni (dst) = vni;
2710 gid_address_vni (src) = vni;
2711 }
Florin Corasce1b4c72017-01-26 14:25:34 -08002712 else if (LISP_AFI_LCAF == type)
2713 {
2714 /* Eventually extend this to support NSH and other */
2715 ASSERT (0);
2716 }
Florin Coras1a1adc72016-07-22 01:45:30 +02002717}
2718
Florin Corase127a7e2016-02-18 22:20:01 +01002719static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002720lisp_cp_lookup_inline (vlib_main_t * vm,
2721 vlib_node_runtime_t * node,
2722 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01002723{
Florin Corasa2157cf2016-08-16 21:09:14 +02002724 u32 *from, *to_next_drop, di, si;
2725 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002726 u32 pkts_mapped = 0;
2727 uword n_left_from, n_left_to_next_drop;
2728
2729 from = vlib_frame_vector_args (from_frame);
2730 n_left_from = from_frame->n_vectors;
2731
2732 while (n_left_from > 0)
2733 {
2734 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02002735 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01002736
2737 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002738 {
2739 u32 pi0;
2740 vlib_buffer_t *b0;
2741 gid_address_t src, dst;
Florin Corase127a7e2016-02-18 22:20:01 +01002742
Florin Corasa2157cf2016-08-16 21:09:14 +02002743 pi0 = from[0];
2744 from += 1;
2745 n_left_from -= 1;
2746 to_next_drop[0] = pi0;
2747 to_next_drop += 1;
2748 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002749
Florin Corasa2157cf2016-08-16 21:09:14 +02002750 b0 = vlib_get_buffer (vm, pi0);
2751 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01002752
Florin Corasa2157cf2016-08-16 21:09:14 +02002753 /* src/dst eid pair */
Filip Tehlar4868ff62017-03-09 16:48:39 +01002754 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
Filip Tehlar324112f2016-06-02 16:07:38 +02002755
Florin Corasa2157cf2016-08-16 21:09:14 +02002756 /* if we have remote mapping for destination already in map-chache
2757 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02002758 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2759 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02002760 if (~0 != di)
2761 {
2762 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2763 /* send a map-request also in case of negative mapping entry
2764 with corresponding action */
2765 if (m->action == LISP_SEND_MAP_REQUEST)
2766 {
2767 /* send map-request */
2768 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2769 0 /* is_resend */ );
2770 pkts_mapped++;
2771 }
2772 else
2773 {
2774 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2775 &src);
2776 if (~0 != si)
2777 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01002778 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02002779 }
2780 }
2781 }
2782 else
2783 {
2784 /* send map-request */
2785 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2786 0 /* is_resend */ );
2787 pkts_mapped++;
2788 }
Florin Corase127a7e2016-02-18 22:20:01 +01002789
Florin Corasa2157cf2016-08-16 21:09:14 +02002790 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2791 {
2792 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2793 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02002794
Florin Corasa2157cf2016-08-16 21:09:14 +02002795 memset (tr, 0, sizeof (*tr));
2796 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02002797 ip_address_copy (&tr->map_resolver_ip,
2798 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02002799 }
2800 gid_address_free (&dst);
2801 gid_address_free (&src);
2802 }
Florin Corase127a7e2016-02-18 22:20:01 +01002803
Florin Corasa2157cf2016-08-16 21:09:14 +02002804 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2805 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01002806 }
2807 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02002808 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2809 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01002810 return from_frame->n_vectors;
2811}
2812
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002813static uword
2814lisp_cp_lookup_ip4 (vlib_main_t * vm,
2815 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2816{
2817 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2818}
2819
2820static uword
2821lisp_cp_lookup_ip6 (vlib_main_t * vm,
2822 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2823{
2824 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2825}
2826
Neale Ranns5e575b12016-10-03 09:40:25 +01002827static uword
2828lisp_cp_lookup_l2 (vlib_main_t * vm,
2829 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2830{
2831 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2832}
2833
Florin Corasce1b4c72017-01-26 14:25:34 -08002834static uword
2835lisp_cp_lookup_nsh (vlib_main_t * vm,
2836 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2837{
2838 /* TODO decide if NSH should be propagated as LCAF or not */
2839 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
2840}
2841
Florin Corasa2157cf2016-08-16 21:09:14 +02002842/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002843VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2844 .function = lisp_cp_lookup_ip4,
2845 .name = "lisp-cp-lookup-ip4",
2846 .vector_size = sizeof (u32),
2847 .format_trace = format_lisp_cp_lookup_trace,
2848 .type = VLIB_NODE_TYPE_INTERNAL,
2849
2850 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2851 .error_strings = lisp_cp_lookup_error_strings,
2852
2853 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2854
2855 .next_nodes = {
2856 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002857 },
2858};
2859/* *INDENT-ON* */
2860
2861/* *INDENT-OFF* */
2862VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2863 .function = lisp_cp_lookup_ip6,
2864 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01002865 .vector_size = sizeof (u32),
2866 .format_trace = format_lisp_cp_lookup_trace,
2867 .type = VLIB_NODE_TYPE_INTERNAL,
2868
2869 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2870 .error_strings = lisp_cp_lookup_error_strings,
2871
2872 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2873
2874 .next_nodes = {
2875 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns5e575b12016-10-03 09:40:25 +01002876 },
2877};
2878/* *INDENT-ON* */
2879
2880/* *INDENT-OFF* */
2881VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2882 .function = lisp_cp_lookup_l2,
2883 .name = "lisp-cp-lookup-l2",
2884 .vector_size = sizeof (u32),
2885 .format_trace = format_lisp_cp_lookup_trace,
2886 .type = VLIB_NODE_TYPE_INTERNAL,
2887
2888 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2889 .error_strings = lisp_cp_lookup_error_strings,
2890
2891 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2892
2893 .next_nodes = {
2894 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Florin Corase127a7e2016-02-18 22:20:01 +01002895 },
2896};
Florin Corasa2157cf2016-08-16 21:09:14 +02002897/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002898
Florin Corasce1b4c72017-01-26 14:25:34 -08002899/* *INDENT-OFF* */
2900VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
2901 .function = lisp_cp_lookup_nsh,
2902 .name = "lisp-cp-lookup-nsh",
2903 .vector_size = sizeof (u32),
2904 .format_trace = format_lisp_cp_lookup_trace,
2905 .type = VLIB_NODE_TYPE_INTERNAL,
2906
2907 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2908 .error_strings = lisp_cp_lookup_error_strings,
2909
2910 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2911
2912 .next_nodes = {
2913 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2914 },
2915};
2916/* *INDENT-ON* */
2917
Florin Corase127a7e2016-02-18 22:20:01 +01002918/* lisp_cp_input statistics */
Filip Tehlarcda70662017-01-16 10:30:03 +01002919#define foreach_lisp_cp_input_error \
2920_(DROP, "drop") \
2921_(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2922_(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2923_(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
Florin Corase127a7e2016-02-18 22:20:01 +01002924_(MAP_REPLIES_RECEIVED, "map-replies received")
2925
Florin Corasa2157cf2016-08-16 21:09:14 +02002926static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01002927#define _(sym,string) string,
2928 foreach_lisp_cp_input_error
2929#undef _
2930};
2931
2932typedef enum
2933{
2934#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02002935 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01002936#undef _
2937 LISP_CP_INPUT_N_ERROR,
2938} lisp_cp_input_error_t;
2939
Florin Corase127a7e2016-02-18 22:20:01 +01002940typedef struct
2941{
2942 gid_address_t dst_eid;
2943 ip4_address_t map_resolver_ip;
2944} lisp_cp_input_trace_t;
2945
2946u8 *
2947format_lisp_cp_input_trace (u8 * s, va_list * args)
2948{
2949 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2950 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02002951 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
2952 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01002953
2954 s = format (s, "LISP-CP-INPUT: TODO");
2955 return s;
2956}
2957
Filip Tehlar9677a942016-11-28 10:23:31 +01002958static void
2959remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
2960{
2961 mapping_t *m;
Florin Corasf3dc11a2017-01-24 03:13:43 -08002962 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
2963 memset (adj_args, 0, sizeof (adj_args[0]));
Filip Tehlar9677a942016-11-28 10:23:31 +01002964
2965 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasf3dc11a2017-01-24 03:13:43 -08002966
2967 gid_address_copy (&adj_args->reid, &m->eid);
2968 adj_args->is_add = 0;
2969 if (vnet_lisp_add_del_adjacency (adj_args))
2970 clib_warning ("failed to del adjacency!");
2971
Filip Tehlar9677a942016-11-28 10:23:31 +01002972 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
2973 0 /* is_static */ , 0);
2974 mapping_delete_timer (lcm, mi);
2975}
2976
2977static void
2978mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
2979 f64 expiration_time)
2980{
2981 mapping_t *m;
2982 u64 now = clib_cpu_time_now ();
2983 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
2984 u64 exp_clock_time = now + expiration_time * cpu_cps;
2985
2986 m = pool_elt_at_index (lcm->mapping_pool, mi);
2987
2988 m->timer_set = 1;
2989 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
2990}
2991
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002992static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002993map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01002994{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002995 mapping_t *m;
2996 vec_foreach (m, a->mappings)
2997 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01002998 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002999 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003000 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003001
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003002 clib_mem_free (a);
3003}
3004
3005void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003006process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003007{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003008 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003009 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3010 u32 dst_map_index = 0;
3011 pending_map_request_t *pmr;
3012 u64 *noncep;
3013 uword *pmr_index;
3014
3015 if (a->is_rloc_probe)
3016 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003017
Florin Corase127a7e2016-02-18 22:20:01 +01003018 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003019 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01003020 if (!pmr_index)
3021 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003022 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003023 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003024 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003025 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01003026
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003027 vec_foreach (m, a->mappings)
3028 {
3029 /* insert/update mappings cache */
3030 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3031 m->authoritative, m->ttl,
3032 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003033
Florin Corasba888e42017-01-24 11:38:18 -08003034 if (dst_map_index == (u32) ~ 0)
3035 continue;
3036
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003037 /* try to program forwarding only if mapping saved or updated */
Florin Corasba888e42017-01-24 11:38:18 -08003038 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3039 memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08003040
Florin Corasba888e42017-01-24 11:38:18 -08003041 gid_address_copy (&adj_args->leid, &pmr->src);
3042 gid_address_copy (&adj_args->reid, &m->eid);
3043 adj_args->is_add = 1;
3044 if (vnet_lisp_add_del_adjacency (adj_args))
3045 clib_warning ("failed to add adjacency!");
Florin Corasf3dc11a2017-01-24 03:13:43 -08003046
Florin Corasba888e42017-01-24 11:38:18 -08003047 if ((u32) ~ 0 != m->ttl)
3048 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003049 }
Florin Corase127a7e2016-02-18 22:20:01 +01003050
3051 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02003052
3053 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02003054 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003055 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02003056 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003057 /* *INDENT-ON* */
3058
3059 clib_fifo_free (pmr->nonces);
3060 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003061
3062done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003063 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003064 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003065}
3066
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003067static int
3068is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3069 lisp_key_type_t key_id, u8 * key)
3070{
3071 u8 *auth_data = 0;
3072 u16 auth_data_len;
3073 int result;
3074
3075 auth_data_len = auth_data_len_by_key_id (key_id);
3076 if ((u16) ~ 0 == auth_data_len)
3077 {
3078 clib_warning ("invalid length for key_id %d!", key_id);
3079 return 0;
3080 }
3081
3082 /* save auth data */
3083 vec_validate (auth_data, auth_data_len - 1);
3084 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3085
3086 /* clear auth data */
3087 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3088
3089 /* get hash of the message */
3090 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3091 (unsigned char *) h, msg_len, NULL, NULL);
3092
3093 result = memcmp (code, auth_data, auth_data_len);
3094
3095 vec_free (auth_data);
3096
3097 return !result;
3098}
3099
3100static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003101process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003102{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003103 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003104 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003105
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003106 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003107 if (!pmr_index)
3108 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003109 clib_warning ("No pending map-register entry with nonce %lu!",
3110 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003111 return;
3112 }
3113
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003114 map_records_arg_free (a);
3115 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3116}
3117
3118static mapping_t *
3119get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3120{
3121 u32 mi;
3122
3123 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3124 if (~0 == mi)
3125 {
3126 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3127 e);
3128 return 0;
3129 }
3130 return pool_elt_at_index (lcm->mapping_pool, mi);
3131}
3132
3133/**
3134 * When map-notify is received it is necessary that all EIDs in the record
3135 * list share common key. The key is then used to verify authentication
3136 * data in map-notify message.
3137 */
3138static int
3139map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3140 u32 key_id, u8 ** key_out)
3141{
3142 u32 i, len = vec_len (maps);
3143 mapping_t *m;
3144
3145 /* get key of the first mapping */
3146 m = get_mapping (lcm, &maps[0].eid);
3147 if (!m || !m->key)
3148 return -1;
3149
3150 key_out[0] = m->key;
3151
3152 for (i = 1; i < len; i++)
3153 {
3154 m = get_mapping (lcm, &maps[i].eid);
3155 if (!m || !m->key)
3156 return -1;
3157
3158 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3159 {
3160 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3161 return -1;
3162 }
3163 }
3164 return 0;
3165}
3166
3167static int
3168parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3169{
3170 locator_t *locators = 0;
3171 u32 i, len;
3172 gid_address_t deid;
3173 mapping_t m;
3174 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003175
3176 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003177 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003178 {
3179 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3180 if (len == ~0)
3181 {
3182 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003183 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003184 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003185 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003186 }
3187
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003188 m.locators = locators;
3189 gid_address_copy (&m.eid, &deid);
3190 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003191 }
3192
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003193 return 0;
3194}
3195
3196static map_records_arg_t *
3197parse_map_notify (vlib_buffer_t * b)
3198{
3199 int rc = 0;
3200 map_notify_hdr_t *mnotif_hdr;
3201 lisp_key_type_t key_id;
3202 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3203 u8 *key = 0;
3204 gid_address_t deid;
3205 u16 auth_data_len = 0;
3206 u8 record_count;
3207 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3208
3209 memset (a, 0, sizeof (*a));
3210 mnotif_hdr = vlib_buffer_get_current (b);
3211 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3212 memset (&deid, 0, sizeof (deid));
3213
3214 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3215 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3216 auth_data_len = auth_data_len_by_key_id (key_id);
3217
3218 /* advance buffer by authentication data */
3219 vlib_buffer_pull (b, auth_data_len);
3220
3221 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3222 rc = parse_map_records (b, a, record_count);
3223 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003224 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003225 map_records_arg_free (a);
3226 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003227 }
3228
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003229 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3230 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003231 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003232 map_records_arg_free (a);
3233 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003234 }
3235
3236 /* verify authentication data */
3237 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003238 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003239 {
3240 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003241 a->nonce);
3242 map_records_arg_free (a);
3243 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003244 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003245 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003246}
3247
3248static vlib_buffer_t *
3249build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3250 ip_address_t * dst, u64 nonce, u8 probe_bit,
3251 mapping_t * records, u16 dst_port, u32 * bi_res)
3252{
3253 vlib_buffer_t *b;
3254 u32 bi;
3255 vlib_main_t *vm = lcm->vlib_main;
3256
3257 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3258 {
3259 clib_warning ("Can't allocate buffer for Map-Register!");
3260 return 0;
3261 }
3262
3263 b = vlib_get_buffer (vm, bi);
3264
3265 /* leave some space for the encap headers */
3266 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3267
3268 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3269
3270 /* push outer ip header */
3271 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3272
3273 bi_res[0] = bi;
3274 return b;
3275}
3276
3277static int
3278send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3279 u8 probe_bit, u64 nonce, u16 dst_port,
3280 ip_address_t * probed_loc)
3281{
3282 ip_address_t src;
3283 u32 bi;
3284 vlib_buffer_t *b;
3285 vlib_frame_t *f;
3286 u32 next_index, *to_next;
3287 mapping_t *records = 0, *m;
3288
3289 m = pool_elt_at_index (lcm->mapping_pool, mi);
3290 if (!m)
3291 return -1;
3292
3293 vec_add1 (records, m[0]);
3294 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3295 memset (&src, 0, sizeof (src));
3296
3297 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3298 {
3299 clib_warning ("can't find inteface address for %U", format_ip_address,
3300 dst);
3301 return -1;
3302 }
3303
3304 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3305 &bi);
3306 if (!b)
3307 return -1;
3308 free_map_register_records (records);
3309
3310 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3311 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3312 ip4_lookup_node.index : ip6_lookup_node.index;
3313
3314 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3315
3316 /* Enqueue the packet */
3317 to_next = vlib_frame_vector_args (f);
3318 to_next[0] = bi;
3319 f->n_vectors = 1;
3320 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3321 return 0;
3322}
3323
Filip Tehlarb601f222017-01-02 10:22:56 +01003324static void
3325find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3326{
3327 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3328 if (start < 0 && start < -sizeof (b->pre_data))
3329 {
3330 *ip_hdr = 0;
3331 return;
3332 }
3333
3334 *ip_hdr = b->data + start;
3335 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3336 *ip_hdr = 0;
3337}
3338
Florin Corase127a7e2016-02-18 22:20:01 +01003339void
Filip Tehlarcda70662017-01-16 10:30:03 +01003340process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3341 lisp_cp_main_t * lcm, vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01003342{
Filip Tehlarb601f222017-01-02 10:22:56 +01003343 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003344 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3345 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02003346 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01003347 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003348 u64 nonce;
Filip Tehlarcda70662017-01-16 10:30:03 +01003349 u32 i, len = 0, rloc_probe_recv = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003350 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003351
3352 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003353 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02003354 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003355 clib_warning
3356 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01003357 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02003358 }
Florin Corase127a7e2016-02-18 22:20:01 +01003359
Filip Tehlarb601f222017-01-02 10:22:56 +01003360 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3361 nonce = MREQ_NONCE (mreq_hdr);
3362
Florin Corase127a7e2016-02-18 22:20:01 +01003363 /* parse src eid */
3364 len = lisp_msg_parse_addr (b, &src);
3365 if (len == ~0)
3366 return;
3367
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003368 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3369 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01003370 if (len == ~0)
Filip Tehlarcda70662017-01-16 10:30:03 +01003371 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003372
3373 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02003374 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01003375 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003376 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01003377 len = lisp_msg_parse_eid_rec (b, &dst);
3378 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003379 {
3380 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003381 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02003382 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003383
3384 if (MREQ_SMR (mreq_hdr))
3385 {
3386 /* send SMR-invoked map-requests */
3387 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3388 }
3389 else if (MREQ_RLOC_PROBE (mreq_hdr))
3390 {
Filip Tehlarb601f222017-01-02 10:22:56 +01003391 find_ip_header (b, &ip_hdr);
3392 if (!ip_hdr)
3393 {
3394 clib_warning ("Cannot find the IP header!");
Filip Tehlarcda70662017-01-16 10:30:03 +01003395 goto done;
Filip Tehlarb601f222017-01-02 10:22:56 +01003396 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003397 rloc_probe_recv++;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003398 memset (&m, 0, sizeof (m));
3399 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3400
3401 // TODO: select best locator; for now use the first one
3402 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3403
3404 /* get src/dst IP addresses */
3405 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3406
3407 // TODO get source port from buffer
3408 u16 src_port = LISP_CONTROL_PORT;
3409
3410 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3411 src_port, &probed_loc);
3412 }
Florin Corase127a7e2016-02-18 22:20:01 +01003413 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003414
3415done:
Filip Tehlarcda70662017-01-16 10:30:03 +01003416 vlib_node_increment_counter (vm, node->node_index,
3417 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3418 rloc_probe_recv);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003419 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003420}
3421
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003422static map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003423parse_map_reply (vlib_buffer_t * b)
3424{
3425 locator_t probed;
3426 gid_address_t deid;
3427 void *h;
3428 u32 i, len = 0;
3429 mapping_t m;
3430 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003431 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003432 memset (a, 0, sizeof (*a));
3433 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003434
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003435 mrep_hdr = vlib_buffer_get_current (b);
3436 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003437 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003438 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3439
3440 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3441 {
3442 memset (&m, 0, sizeof (m));
3443 locators = 0;
3444 h = vlib_buffer_get_current (b);
3445
3446 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3447 m.action = MAP_REC_ACTION (h);
3448 m.authoritative = MAP_REC_AUTH (h);
3449
3450 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3451 if (len == ~0)
3452 {
3453 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003454 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003455 return 0;
3456 }
3457
3458 m.locators = locators;
3459 gid_address_copy (&m.eid, &deid);
3460 vec_add1 (a->mappings, m);
3461 }
3462 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003463}
3464
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003465static void
3466queue_map_reply_for_processing (map_records_arg_t * a)
3467{
Florin Coras655fcc42017-01-10 08:57:54 -08003468 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003469}
3470
3471static void
3472queue_map_notify_for_processing (map_records_arg_t * a)
3473{
3474 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3475}
3476
Florin Corase127a7e2016-02-18 22:20:01 +01003477static uword
3478lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02003479 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01003480{
Filip Tehlarcda70662017-01-16 10:30:03 +01003481 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3482 map_notifies_recv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003483 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02003484 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003485 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003486
3487 from = vlib_frame_vector_args (from_frame);
3488 n_left_from = from_frame->n_vectors;
3489
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003490
Florin Corase127a7e2016-02-18 22:20:01 +01003491 while (n_left_from > 0)
3492 {
3493 u32 n_left_to_next_drop;
3494
3495 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003496 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003497 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003498 {
3499 u32 bi0;
3500 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01003501
Florin Corasa2157cf2016-08-16 21:09:14 +02003502 bi0 = from[0];
3503 from += 1;
3504 n_left_from -= 1;
3505 to_next_drop[0] = bi0;
3506 to_next_drop += 1;
3507 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003508
Florin Corasa2157cf2016-08-16 21:09:14 +02003509 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003510
Florin Corasa2157cf2016-08-16 21:09:14 +02003511 type = lisp_msg_type (vlib_buffer_get_current (b0));
3512 switch (type)
3513 {
3514 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003515 a = parse_map_reply (b0);
3516 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003517 {
3518 if (a->is_rloc_probe)
3519 rloc_probe_rep_recv++;
3520 queue_map_reply_for_processing (a);
3521 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003522 break;
3523 case LISP_MAP_REQUEST:
Filip Tehlarcda70662017-01-16 10:30:03 +01003524 process_map_request (vm, node, lcm, b0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003525 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003526 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003527 a = parse_map_notify (b0);
3528 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003529 {
3530 map_notifies_recv++;
3531 queue_map_notify_for_processing (a);
3532 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003533 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02003534 default:
3535 clib_warning ("Unsupported LISP message type %d", type);
3536 break;
3537 }
Florin Corase127a7e2016-02-18 22:20:01 +01003538
Florin Corasa2157cf2016-08-16 21:09:14 +02003539 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01003540
Florin Corasa2157cf2016-08-16 21:09:14 +02003541 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3542 {
Florin Corase127a7e2016-02-18 22:20:01 +01003543
Florin Corasa2157cf2016-08-16 21:09:14 +02003544 }
3545 }
Florin Corase127a7e2016-02-18 22:20:01 +01003546
Florin Corasa2157cf2016-08-16 21:09:14 +02003547 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3548 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003549 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003550 vlib_node_increment_counter (vm, node->node_index,
3551 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3552 rloc_probe_rep_recv);
3553 vlib_node_increment_counter (vm, node->node_index,
3554 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3555 map_notifies_recv);
Florin Corase127a7e2016-02-18 22:20:01 +01003556 return from_frame->n_vectors;
3557}
3558
Florin Corasa2157cf2016-08-16 21:09:14 +02003559/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003560VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3561 .function = lisp_cp_input,
3562 .name = "lisp-cp-input",
3563 .vector_size = sizeof (u32),
3564 .format_trace = format_lisp_cp_input_trace,
3565 .type = VLIB_NODE_TYPE_INTERNAL,
3566
3567 .n_errors = LISP_CP_INPUT_N_ERROR,
3568 .error_strings = lisp_cp_input_error_strings,
3569
3570 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3571
3572 .next_nodes = {
3573 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3574 },
3575};
Florin Corasa2157cf2016-08-16 21:09:14 +02003576/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003577
3578clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003579lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01003580{
Florin Corasa2157cf2016-08-16 21:09:14 +02003581 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3582 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003583
3584 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3585 return error;
3586
3587 lcm->im4 = &ip4_main;
3588 lcm->im6 = &ip6_main;
3589 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02003590 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003591 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02003592 lcm->lisp_pitr = 0;
Florin Corasba888e42017-01-24 11:38:18 -08003593 lcm->flags = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02003594 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01003595
3596 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003597 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02003598 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01003599
Florin Coras577c3552016-04-21 00:45:40 +02003600 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02003601 hash_set (lcm->table_id_by_vni, 0, 0);
3602 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02003603
Florin Corase127a7e2016-02-18 22:20:01 +01003604 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02003605 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003606 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02003607 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003608
Filip Tehlar9677a942016-11-28 10:23:31 +01003609 u64 now = clib_cpu_time_now ();
3610 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01003611 return 0;
3612}
3613
Filip Tehlar4868ff62017-03-09 16:48:39 +01003614static int
3615lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3616 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3617 u32 stats_index)
3618{
Filip Tehlar21511912017-04-07 10:41:42 +02003619 vlib_counter_t v;
3620 vlib_combined_counter_main_t *cm = &lgm->counters;
Filip Tehlar4868ff62017-03-09 16:48:39 +01003621 lisp_gpe_fwd_entry_key_t fwd_key;
3622 const lisp_gpe_tunnel_t *lgt;
3623 fwd_entry_t *fe;
3624
3625 memset (stat, 0, sizeof (*stat));
3626 memset (&fwd_key, 0, sizeof (fwd_key));
3627
3628 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3629 ASSERT (fe != 0);
3630
3631 gid_to_dp_address (&fe->reid, &stat->deid);
3632 gid_to_dp_address (&fe->leid, &stat->seid);
3633 stat->vni = gid_address_vni (&fe->reid);
3634
3635 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3636 stat->loc_rloc = lgt->key->lcl;
3637 stat->rmt_rloc = lgt->key->rmt;
3638
Filip Tehlar21511912017-04-07 10:41:42 +02003639 vlib_get_combined_counter (cm, stats_index, &v);
3640 stat->counters = v;
Filip Tehlar4868ff62017-03-09 16:48:39 +01003641 return 1;
3642}
3643
3644lisp_api_stats_t *
3645vnet_lisp_get_stats (void)
3646{
3647 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3648 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3649 lisp_api_stats_t *stats = 0, stat;
3650 lisp_stats_key_t *key;
3651 u32 index;
3652
3653 /* *INDENT-OFF* */
3654 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3655 {
3656 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3657 vec_add1 (stats, stat);
3658 });
3659 /* *INDENT-ON* */
3660
3661 return stats;
3662}
3663
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003664static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02003665send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003666{
Florin Corasa2157cf2016-08-16 21:09:14 +02003667 map_request_args_t *a = arg;
3668 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003669
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003670 if (a->is_resend)
3671 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3672 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003673 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003674
3675 return 0;
3676}
3677
3678static int
3679queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02003680 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003681{
3682 map_request_args_t a;
3683
3684 a.is_resend = is_resend;
3685 gid_address_copy (&a.seid, seid);
3686 gid_address_copy (&a.deid, deid);
3687 a.smr_invoked = smr_invoked;
3688
3689 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02003690 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003691 return 0;
3692}
3693
3694/**
3695 * Take an action with a pending map request depending on expiration time
3696 * and re-try counters.
3697 */
3698static void
3699update_pending_request (pending_map_request_t * r, f64 dt)
3700{
Florin Corasa2157cf2016-08-16 21:09:14 +02003701 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003702 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003703
3704 if (r->time_to_expire - dt < 0)
3705 /* it's time to decide what to do with this pending request */
3706 {
3707 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02003708 /* too many retries -> assume current map resolver is not available */
3709 {
3710 mr = get_map_resolver (&lcm->active_map_resolver);
3711 if (!mr)
3712 {
3713 clib_warning ("Map resolver %U not found - probably deleted "
3714 "by the user recently.", format_ip_address,
3715 &lcm->active_map_resolver);
3716 }
3717 else
3718 {
3719 clib_warning ("map resolver %U is unreachable, ignoring",
3720 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003721
Florin Corasa2157cf2016-08-16 21:09:14 +02003722 /* mark current map resolver unavailable so it won't be
3723 * selected next time */
3724 mr->is_down = 1;
3725 mr->last_update = vlib_time_now (lcm->vlib_main);
3726 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003727
Florin Corasa2157cf2016-08-16 21:09:14 +02003728 reset_pending_mr_counters (r);
3729 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003730
Florin Corasa2157cf2016-08-16 21:09:14 +02003731 /* try to find a next eligible map resolver and re-send */
3732 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3733 1 /* resend */ );
3734 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003735 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003736 {
3737 /* try again */
3738 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3739 1 /* resend */ );
3740 r->retries_num++;
3741 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3742 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003743 }
3744 else
3745 r->time_to_expire -= dt;
3746}
3747
3748static void
3749remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3750{
Florin Corasa2157cf2016-08-16 21:09:14 +02003751 u64 *nonce;
3752 pending_map_request_t *pmr;
3753 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003754
Florin Corasa2157cf2016-08-16 21:09:14 +02003755 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003756 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02003757 ({
3758 if (pmr->to_be_removed)
3759 {
3760 clib_fifo_foreach (nonce, pmr->nonces, ({
3761 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3762 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003763
Florin Corasa2157cf2016-08-16 21:09:14 +02003764 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3765 }
3766 }));
3767 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003768
3769 vec_foreach (pmr_index, to_be_removed)
3770 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3771
3772 vec_free (to_be_removed);
3773}
3774
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003775static void
3776update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3777{
3778 static f64 time_left = RLOC_PROBING_INTERVAL;
3779
3780 if (!lcm->is_enabled || !lcm->rloc_probing)
3781 return;
3782
3783 time_left -= dt;
3784 if (time_left <= 0)
3785 {
3786 time_left = RLOC_PROBING_INTERVAL;
3787 send_rloc_probes (lcm);
3788 }
3789}
3790
3791static void
3792update_map_register (lisp_cp_main_t * lcm, f64 dt)
3793{
3794 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3795 static u64 mreg_sent_counter = 0;
3796
3797 if (!lcm->is_enabled || !lcm->map_registering)
3798 return;
3799
3800 time_left -= dt;
3801 if (time_left <= 0)
3802 {
3803 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3804 time_left = MAP_REGISTER_INTERVAL;
3805 else
3806 {
3807 mreg_sent_counter++;
3808 time_left = QUICK_MAP_REGISTER_INTERVAL;
3809 }
3810 send_map_register (lcm, 1 /* want map notify */ );
3811 }
3812}
3813
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003814static uword
3815send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003816 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003817{
Filip Tehlar9677a942016-11-28 10:23:31 +01003818 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003819 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003820 pending_map_request_t *pmr;
3821 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003822
3823 while (1)
3824 {
3825 vlib_process_wait_for_event_or_clock (vm, period);
3826
3827 /* currently no signals are expected - just wait for clock */
3828 (void) vlib_process_get_events (vm, 0);
3829
Florin Corasa2157cf2016-08-16 21:09:14 +02003830 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003831 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02003832 ({
3833 if (!pmr->to_be_removed)
3834 update_pending_request (pmr, period);
3835 }));
3836 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003837
3838 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003839
3840 update_map_register (lcm, period);
3841 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01003842
3843 u64 now = clib_cpu_time_now ();
3844
3845 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3846 if (vec_len (expired) > 0)
3847 {
3848 u32 *mi = 0;
3849 vec_foreach (mi, expired)
3850 {
3851 remove_expired_mapping (lcm, mi[0]);
3852 }
3853 _vec_len (expired) = 0;
3854 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003855 }
3856
3857 /* unreachable */
3858 return 0;
3859}
3860
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01003861vnet_api_error_t
3862vnet_lisp_stats_enable_disable (u8 enable)
3863{
3864 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3865
3866 if (vnet_lisp_enable_disable_status () == 0)
3867 return VNET_API_ERROR_LISP_DISABLED;
3868
Filip Tehlar4868ff62017-03-09 16:48:39 +01003869 if (enable)
3870 lcm->flags |= LISP_FLAG_STATS_ENABLED;
3871 else
3872 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
3873
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01003874 return 0;
3875}
3876
3877u8
3878vnet_lisp_stats_enable_disable_state (void)
3879{
3880 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3881
3882 if (vnet_lisp_enable_disable_status () == 0)
3883 return VNET_API_ERROR_LISP_DISABLED;
3884
Filip Tehlar4868ff62017-03-09 16:48:39 +01003885 return lcm->flags & LISP_FLAG_STATS_ENABLED;
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01003886}
3887
Florin Corasa2157cf2016-08-16 21:09:14 +02003888/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003889VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3890 .function = send_map_resolver_service,
3891 .type = VLIB_NODE_TYPE_PROCESS,
3892 .name = "lisp-retry-service",
3893 .process_log2_n_stack_bytes = 16,
3894};
Florin Corasa2157cf2016-08-16 21:09:14 +02003895/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003896
Florin Corasa2157cf2016-08-16 21:09:14 +02003897VLIB_INIT_FUNCTION (lisp_cp_init);
3898
3899/*
3900 * fd.io coding-style-patch-verification: ON
3901 *
3902 * Local Variables:
3903 * eval: (c-set-style "gnu")
3904 * End:
3905 */