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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlibmemory/api.h>
17 #include <lisp/lisp-cp/control.h>
18 #include <lisp/lisp-cp/packets.h>
19 #include <lisp/lisp-cp/lisp_msg_serdes.h>
20 #include <lisp/lisp-gpe/lisp_gpe_fwd_entry.h>
21 #include <lisp/lisp-gpe/lisp_gpe_tenant.h>
22 #include <lisp/lisp-gpe/lisp_gpe_tunnel.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/ethernet/arp_packet.h>
26 #include <vnet/ethernet/packet.h>
28 #include <openssl/evp.h>
29 #include <vnet/crypto/crypto.h>
31 #define MAX_VALUE_U24 0xffffff
33 /* mapping timer control constants (in seconds) */
34 #define TIME_UNTIL_REFETCH_OR_DELETE 20
35 #define MAPPING_TIMEOUT (((m->ttl) * 60) - TIME_UNTIL_REFETCH_OR_DELETE)
37 u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
38 static void *send_map_request_thread_fn (void *arg);
42 LISP_CP_INPUT_NEXT_DROP,
44 } lisp_cp_input_next_t;
55 vnet_lisp_get_map_request_mode (void)
57 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
58 return lcm->map_request_mode;
62 auth_data_len_by_key_id (lisp_key_type_t key_id)
67 return SHA1_AUTH_DATA_LEN;
68 case HMAC_SHA_256_128:
69 return SHA256_AUTH_DATA_LEN;
71 clib_warning ("unsupported key type: %d!", key_id);
78 queue_map_request (gid_address_t * seid, gid_address_t * deid,
79 u8 smr_invoked, u8 is_resend);
81 ip_interface_address_t *
82 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
83 u32 sw_if_index, u8 loop)
85 vnet_main_t *vnm = vnet_get_main ();
86 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
87 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
88 sw_if_index = swif->unnumbered_sw_if_index;
90 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
91 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
93 return pool_elt_at_index ((lm)->if_address_pool, ia);
97 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
100 ip_interface_address_t *ia;
102 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
105 return ip_interface_address_get_address (lm, ia);
109 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
110 u8 version, ip_address_t * result)
112 ip_lookup_main_t *lm;
115 lm = (version == AF_IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
116 addr = ip_interface_get_first_address (lm, sw_if_index, version);
120 ip_address_set (result, addr, version);
125 * Find the sw_if_index of the interface that would be used to egress towards
129 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
131 fib_node_index_t fei;
134 ip_address_to_fib_prefix (dst, &prefix);
136 fei = fib_table_lookup (0, &prefix);
138 return (fib_entry_get_resolving_interface (fei));
142 * Find first IP of the interface that would be used to egress towards dst.
143 * Returns 1 if the address is found 0 otherwise.
146 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
147 ip_address_t * result)
150 ip_lookup_main_t *lm;
154 ASSERT (result != 0);
156 ipver = ip_addr_version (dst);
158 lm = (ipver == AF_IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
159 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
164 /* find the first ip address */
165 addr = ip_interface_get_first_address (lm, si, ipver);
169 ip_address_set (result, addr, ipver);
174 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add,
175 u8 with_default_route)
181 dp_table = hash_get (lcm->table_id_by_vni, vni);
185 clib_warning ("vni %d not associated to a vrf!", vni);
186 return VNET_API_ERROR_INVALID_VALUE;
191 dp_table = hash_get (lcm->bd_id_by_vni, vni);
194 clib_warning ("vni %d not associated to a bridge domain!", vni);
195 return VNET_API_ERROR_INVALID_VALUE;
199 /* enable/disable data-plane interface */
203 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
205 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0],
211 lisp_gpe_tenant_l2_iface_unlock (vni);
213 lisp_gpe_tenant_l3_iface_unlock (vni);
220 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 dst_map_index)
222 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
225 clib_memset (a, 0, sizeof (*a));
227 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
231 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
233 /* delete dp fwd entry */
236 a->locator_pairs = fe->locator_pairs;
237 a->vni = gid_address_vni (&fe->reid);
238 gid_address_copy (&a->rmt_eid, &fe->reid);
240 gid_address_copy (&a->lcl_eid, &fe->leid);
242 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
243 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
245 /* delete entry in fwd table */
246 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
247 vec_free (fe->locator_pairs);
248 pool_put (lcm->fwd_entry_pool, fe);
252 * Finds first remote locator with best (lowest) priority that has a local
253 * peer locator with an underlying route to it.
257 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
258 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
260 u32 i, limitp = 0, li, found = 0, esi;
261 locator_set_t *rmt_ls, *lcl_ls;
262 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
263 locator_t *lp, *rmt = 0;
268 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
270 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
272 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
279 /* find unvisited remote locator with best priority */
280 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
282 if (0 != hash_get (checked, i))
285 li = vec_elt (rmt_ls->locator_indices, i);
286 lp = pool_elt_at_index (lcm->locator_pool, li);
288 /* we don't support non-IP locators for now */
289 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
292 if ((found && lp->priority == limitp)
293 || (!found && lp->priority >= limitp))
297 /* don't search for locators with lower priority and don't
298 * check this locator again*/
299 limitp = lp->priority;
300 hash_set (checked, i, 1);
304 /* check if a local locator with a route to remote locator exists */
307 /* find egress sw_if_index for rmt locator */
309 ip_fib_get_egress_iface_for_dst (lcm,
310 &gid_address_ip (&rmt->address));
311 if ((u32) ~ 0 == esi)
314 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
316 li = vec_elt (lcl_ls->locator_indices, i);
317 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
319 /* found local locator with the needed sw_if_index */
320 if (sl->sw_if_index == esi)
322 /* and it has an address */
323 if (0 == ip_interface_get_first_ip_address (lcm,
325 gid_address_ip_version
330 clib_memset (&pair, 0, sizeof (pair));
331 ip_address_copy (&pair.rmt_loc,
332 &gid_address_ip (&rmt->address));
333 ip_address_copy (&pair.lcl_loc, lcl_addr);
334 pair.weight = rmt->weight;
335 pair.priority = rmt->priority;
336 vec_add1 (locator_pairs[0], pair);
350 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
353 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
357 switch (fid_addr_type (fid))
359 case FID_ADDR_IP_PREF:
360 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
361 gid_address_ippref (dst) = fid_addr_ippref (fid);
364 gid_address_type (dst) = GID_ADDR_MAC;
365 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
368 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
374 vnet_lisp_map_register_state_get (void)
376 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
377 return lcm->map_registering;
381 vnet_lisp_rloc_probe_state_get (void)
383 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
384 return lcm->rloc_probing;
388 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
390 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
391 gid_address_t *rmt_eid, *lcl_eid;
392 mapping_t *lcl_map, *rmt_map;
393 u32 sw_if_index, **rmts, rmts_idx;
394 uword *feip = 0, *dpid, *rmts_stored_idxp = 0;
396 u8 type, is_src_dst = 0;
399 clib_memset (a, 0, sizeof (*a));
401 /* remove entry if it already exists */
402 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
404 dp_del_fwd_entry (lcm, dst_map_index);
407 * Determine local mapping and eid
409 if (lcm->flags & LISP_FLAG_PITR_MODE)
411 if (lcm->pitr_map_index != ~0)
412 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
415 clib_warning ("no PITR mapping configured!");
420 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
421 lcl_eid = &lcl_map->eid;
424 * Determine remote mapping and eid
426 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
427 rmt_eid = &rmt_map->eid;
430 * Build and insert data plane forwarding entry
434 if (MR_MODE_SRC_DST == lcm->map_request_mode)
436 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
438 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
439 &gid_address_sd_dst (rmt_eid));
440 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
441 &gid_address_sd_src (rmt_eid));
445 gid_address_copy (&a->rmt_eid, rmt_eid);
446 gid_address_copy (&a->lcl_eid, lcl_eid);
451 gid_address_copy (&a->rmt_eid, rmt_eid);
453 a->vni = gid_address_vni (&a->rmt_eid);
454 a->is_src_dst = is_src_dst;
456 /* get vrf or bd_index associated to vni */
457 type = gid_address_type (&a->rmt_eid);
458 if (GID_ADDR_IP_PREFIX == type)
460 dpid = hash_get (lcm->table_id_by_vni, a->vni);
463 clib_warning ("vni %d not associated to a vrf!", a->vni);
466 a->table_id = dpid[0];
468 else if (GID_ADDR_MAC == type)
470 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
473 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
479 /* find best locator pair that 1) verifies LISP policy 2) are connected */
480 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
482 /* Either rmt mapping is negative or we can't find underlay path.
483 * Try again with petr if configured */
484 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
486 rmt_map = lisp_get_petr_mapping (lcm);
487 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
494 a->action = rmt_map->action;
497 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
500 if (a->locator_pairs)
501 vec_free (a->locator_pairs);
505 /* add tunnel to fwd entry table */
506 pool_get (lcm->fwd_entry_pool, fe);
507 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
509 fe->locator_pairs = a->locator_pairs;
510 gid_address_copy (&fe->reid, &a->rmt_eid);
513 gid_address_copy (&fe->leid, &a->lcl_eid);
515 gid_address_copy (&fe->leid, lcl_eid);
517 fe->is_src_dst = is_src_dst;
518 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
519 fe - lcm->fwd_entry_pool);
521 /* Add rmt mapping to the vector of adjacent mappings to lcl mapping */
523 hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, src_map_index);
524 if (!rmts_stored_idxp)
526 pool_get (lcm->lcl_to_rmt_adjacencies, rmts);
527 clib_memset (rmts, 0, sizeof (*rmts));
528 rmts_idx = rmts - lcm->lcl_to_rmt_adjacencies;
529 hash_set (lcm->lcl_to_rmt_adjs_by_lcl_idx, src_map_index, rmts_idx);
533 rmts_idx = (u32) (*rmts_stored_idxp);
534 rmts = pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idx);
536 vec_add1 (rmts[0], dst_map_index);
543 } fwd_entry_mt_arg_t;
546 dp_add_fwd_entry_thread_fn (void *arg)
548 fwd_entry_mt_arg_t *a = arg;
549 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
550 dp_add_fwd_entry (lcm, a->si, a->di);
555 dp_add_fwd_entry_from_mt (u32 si, u32 di)
557 fwd_entry_mt_arg_t a;
559 clib_memset (&a, 0, sizeof (a));
563 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
564 (u8 *) & a, sizeof (a));
569 * Returns vector of adjacencies.
571 * The caller must free the vector returned by this function.
573 * @param vni virtual network identifier
574 * @return vector of adjacencies
577 vnet_lisp_adjacencies_get_by_vni (u32 vni)
579 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
581 lisp_adjacency_t *adjs = 0, adj;
584 pool_foreach(fwd, lcm->fwd_entry_pool,
586 if (gid_address_vni (&fwd->reid) != vni)
589 gid_address_copy (&adj.reid, &fwd->reid);
590 gid_address_copy (&adj.leid, &fwd->leid);
591 vec_add1 (adjs, adj);
599 get_map_server (ip_address_t * a)
601 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
604 vec_foreach (m, lcm->map_servers)
606 if (!ip_address_cmp (&m->address, a))
615 get_map_resolver (ip_address_t * a)
617 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
620 vec_foreach (m, lcm->map_resolvers)
622 if (!ip_address_cmp (&m->address, a))
631 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
634 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
635 lisp_msmr_t _ms, *ms = &_ms;
637 if (vnet_lisp_enable_disable_status () == 0)
639 clib_warning ("LISP is disabled!");
640 return VNET_API_ERROR_LISP_DISABLED;
645 if (get_map_server (addr))
647 clib_warning ("map-server %U already exists!", format_ip_address,
652 clib_memset (ms, 0, sizeof (*ms));
653 ip_address_copy (&ms->address, addr);
654 vec_add1 (lcm->map_servers, ms[0]);
656 if (vec_len (lcm->map_servers) == 1)
657 lcm->do_map_server_election = 1;
661 for (i = 0; i < vec_len (lcm->map_servers); i++)
663 ms = vec_elt_at_index (lcm->map_servers, i);
664 if (!ip_address_cmp (&ms->address, addr))
666 if (!ip_address_cmp (&ms->address, &lcm->active_map_server))
667 lcm->do_map_server_election = 1;
669 vec_del1 (lcm->map_servers, i);
679 * Add/remove mapping to/from map-cache. Overwriting not allowed.
682 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
683 u32 * map_index_result)
685 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
686 u32 mi, *map_indexp, map_index, i;
687 u32 **rmts = 0, *remote_idxp, rmts_itr, remote_idx;
689 mapping_t *m, *old_map;
692 if (gid_address_type (&a->eid) == GID_ADDR_NSH)
694 if (gid_address_vni (&a->eid) != 0)
696 clib_warning ("Supported only default VNI for NSH!");
697 return VNET_API_ERROR_INVALID_ARGUMENT;
699 if (gid_address_nsh_spi (&a->eid) > MAX_VALUE_U24)
701 clib_warning ("SPI is greater than 24bit!");
702 return VNET_API_ERROR_INVALID_ARGUMENT;
706 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
707 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
710 /* TODO check if overwriting and take appropriate actions */
711 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
713 clib_warning ("eid %U found in the eid-table", format_gid_address,
715 return VNET_API_ERROR_VALUE_EXIST;
718 pool_get (lcm->mapping_pool, m);
719 gid_address_copy (&m->eid, &a->eid);
720 m->locator_set_index = a->locator_set_index;
722 m->action = a->action;
724 m->is_static = a->is_static;
725 m->key = vec_dup (a->key);
726 m->key_id = a->key_id;
727 m->authoritative = a->authoritative;
729 map_index = m - lcm->mapping_pool;
730 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
733 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
735 clib_warning ("Locator set with index %d doesn't exist",
736 a->locator_set_index);
737 return VNET_API_ERROR_INVALID_VALUE;
740 /* add eid to list of eids supported by locator-set */
741 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
742 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
743 a->locator_set_index);
744 vec_add1 (eid_indexes[0], map_index);
749 vec_add1 (lcm->local_mappings_indexes, map_index);
751 map_index_result[0] = map_index;
755 if (mi == GID_LOOKUP_MISS)
757 clib_warning ("eid %U not found in the eid-table",
758 format_gid_address, &a->eid);
759 return VNET_API_ERROR_INVALID_VALUE;
762 /* clear locator-set to eids binding */
763 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
764 a->locator_set_index);
765 for (i = 0; i < vec_len (eid_indexes[0]); i++)
767 map_indexp = vec_elt_at_index (eid_indexes[0], i);
768 if (map_indexp[0] == mi)
771 vec_del1 (eid_indexes[0], i);
773 /* remove local mark if needed */
774 m = pool_elt_at_index (lcm->mapping_pool, mi);
777 /* Remove adjacencies associated with the local mapping */
778 rmts_idxp = hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, mi);
782 pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idxp[0]);
783 vec_foreach (remote_idxp, rmts[0])
785 dp_del_fwd_entry (lcm, remote_idxp[0]);
788 pool_put (lcm->lcl_to_rmt_adjacencies, rmts);
789 hash_unset (lcm->lcl_to_rmt_adjs_by_lcl_idx, mi);
793 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
795 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
796 if (lm_indexp[0] == mi)
799 vec_del1 (lcm->local_mappings_indexes, k);
803 /* Remove remote (if present) from the vectors of lcl-to-rmts
804 * TODO: Address this in a more efficient way.
807 pool_foreach (rmts, lcm->lcl_to_rmt_adjacencies,
809 vec_foreach_index (rmts_itr, rmts[0])
811 remote_idx = vec_elt (rmts[0], rmts_itr);
812 if (mi == remote_idx)
814 vec_del1 (rmts[0], rmts_itr);
822 /* remove mapping from dictionary */
823 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
824 gid_address_free (&m->eid);
825 pool_put_index (lcm->mapping_pool, mi);
832 * Add/update/delete mapping to/in/from map-cache.
835 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
836 u32 * map_index_result)
842 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
844 if (vnet_lisp_enable_disable_status () == 0)
846 clib_warning ("LISP is disabled!");
847 return VNET_API_ERROR_LISP_DISABLED;
850 vni = gid_address_vni (&a->eid);
851 type = gid_address_type (&a->eid);
852 if (GID_ADDR_IP_PREFIX == type)
853 dp_table = hash_get (lcm->table_id_by_vni, vni);
854 else if (GID_ADDR_MAC == type)
855 dp_table = hash_get (lcm->bd_id_by_vni, vni);
857 if (!dp_table && GID_ADDR_NSH != type)
859 clib_warning ("vni %d not associated to a %s!", vni,
860 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
861 return VNET_API_ERROR_INVALID_VALUE;
864 /* store/remove mapping from map-cache */
865 return vnet_lisp_map_cache_add_del (a, map_index_result);
869 add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
872 u32 version = (u32) kvp->key[0];
873 if (AF_IP6 == version)
874 return (BIHASH_WALK_CONTINUE);
876 u32 bd = (u32) (kvp->key[0] >> 32);
877 hash_set (ht[0], bd, 0);
878 return (BIHASH_WALK_CONTINUE);
882 vnet_lisp_l2_arp_bds_get (void)
884 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
887 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
888 add_l2_arp_bd, &bds);
893 add_ndp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
896 u32 version = (u32) kvp->key[0];
897 if (AF_IP4 == version)
898 return (BIHASH_WALK_CONTINUE);
900 u32 bd = (u32) (kvp->key[0] >> 32);
901 hash_set (ht[0], bd, 0);
902 return (BIHASH_WALK_CONTINUE);
906 vnet_lisp_ndp_bds_get (void)
908 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
911 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
920 } lisp_add_l2_arp_ndp_args_t;
923 add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
925 lisp_add_l2_arp_ndp_args_t *a = arg;
926 lisp_api_l2_arp_entry_t **vector = a->vector, e;
928 u32 version = (u32) kvp->key[0];
929 if (AF_IP6 == version)
930 return (BIHASH_WALK_CONTINUE);
932 u32 bd = (u32) (kvp->key[0] >> 32);
936 mac_copy (e.mac, (void *) &kvp->value);
937 e.ip4 = (u32) kvp->key[1];
938 vec_add1 (vector[0], e);
940 return (BIHASH_WALK_CONTINUE);
943 lisp_api_l2_arp_entry_t *
944 vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
946 lisp_api_l2_arp_entry_t *entries = 0;
947 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
948 lisp_add_l2_arp_ndp_args_t a;
953 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
954 add_l2_arp_entry, &a);
959 add_ndp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
961 lisp_add_l2_arp_ndp_args_t *a = arg;
962 lisp_api_ndp_entry_t **vector = a->vector, e;
964 u32 version = (u32) kvp->key[0];
965 if (AF_IP4 == version)
966 return (BIHASH_WALK_CONTINUE);
968 u32 bd = (u32) (kvp->key[0] >> 32);
972 mac_copy (e.mac, (void *) &kvp->value);
973 clib_memcpy (e.ip6, &kvp->key[1], 16);
974 vec_add1 (vector[0], e);
976 return (BIHASH_WALK_CONTINUE);
979 lisp_api_ndp_entry_t *
980 vnet_lisp_ndp_entries_get_by_bd (u32 bd)
982 lisp_api_ndp_entry_t *entries = 0;
983 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
984 lisp_add_l2_arp_ndp_args_t a;
989 gid_dict_foreach_l2_arp_ndp_entry (&lcm->mapping_index_by_gid,
995 vnet_lisp_add_del_l2_arp_ndp_entry (gid_address_t * key, u8 * mac, u8 is_add)
997 if (vnet_lisp_enable_disable_status () == 0)
999 clib_warning ("LISP is disabled!");
1000 return VNET_API_ERROR_LISP_DISABLED;
1003 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1006 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
1009 if (res != GID_LOOKUP_MISS_L2)
1011 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
1012 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1014 u64 val = mac_to_u64 (mac);
1015 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
1020 if (res == GID_LOOKUP_MISS_L2)
1022 clib_warning ("ONE entry %U not found - cannot delete!",
1023 format_gid_address, key);
1026 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
1034 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
1036 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1037 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
1039 if (vnet_lisp_enable_disable_status () == 0)
1041 clib_warning ("LISP is disabled!");
1042 return VNET_API_ERROR_LISP_DISABLED;
1045 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1046 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1048 if (!is_l2 && (vni == 0 || dp_id == 0))
1050 clib_warning ("can't add/del default vni-vrf mapping!");
1054 dp_idp = hash_get (dp_table_by_vni[0], vni);
1055 vnip = hash_get (vni_by_dp_table[0], dp_id);
1061 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1062 "mapping!", vni, dp_id);
1065 hash_set (dp_table_by_vni[0], vni, dp_id);
1066 hash_set (vni_by_dp_table[0], dp_id, vni);
1068 /* create dp iface */
1069 dp_add_del_iface (lcm, vni, is_l2, 1 /* is_add */ ,
1070 1 /* with_default_route */ );
1074 if (!dp_idp || !vnip)
1076 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1077 "mapping!", vni, dp_id);
1080 /* remove dp iface */
1081 dp_add_del_iface (lcm, vni, is_l2, 0 /* is_add */ , 0 /* unused */ );
1083 hash_unset (dp_table_by_vni[0], vni);
1084 hash_unset (vni_by_dp_table[0], dp_id);
1090 /* return 0 if the two locator sets are identical 1 otherwise */
1092 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1093 locator_t * new_locators)
1096 locator_t *old_loc, *new_loc;
1098 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1101 for (i = 0; i < vec_len (new_locators); i++)
1103 old_li = vec_elt (old_ls_indexes, i);
1104 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1106 new_loc = vec_elt_at_index (new_locators, i);
1108 if (locator_cmp (old_loc, new_loc))
1118 gid_address_t *eids_to_be_deleted;
1119 } remove_mapping_args_t;
1122 * Callback invoked when a sub-prefix is found
1125 remove_mapping_if_needed (u32 mi, void *arg)
1128 remove_mapping_args_t *a = arg;
1129 lisp_cp_main_t *lcm = a->lcm;
1133 m = pool_elt_at_index (lcm->mapping_pool, mi);
1137 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1141 if (0 != vec_len (ls->locator_indices))
1146 if (0 == vec_len (ls->locator_indices))
1151 vec_add1 (a->eids_to_be_deleted, m->eid);
1155 * This function searches map cache and looks for IP prefixes that are subset
1156 * of the provided one. If such prefix is found depending on 'is_negative'
1159 * 1) if is_negative is true and found prefix points to positive mapping,
1160 * then the mapping is removed
1161 * 2) if is_negative is false and found prefix points to negative mapping,
1162 * then the mapping is removed
1165 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1169 remove_mapping_args_t a;
1171 clib_memset (&a, 0, sizeof (a));
1173 /* do this only in src/dst mode ... */
1174 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1177 /* ... and only for IP prefix */
1178 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1179 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1182 a.is_negative = is_negative;
1185 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1186 remove_mapping_if_needed, &a);
1188 vec_foreach (e, a.eids_to_be_deleted)
1190 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1192 clib_memset (adj_args, 0, sizeof (adj_args[0]));
1193 gid_address_copy (&adj_args->reid, e);
1194 adj_args->is_add = 0;
1195 if (vnet_lisp_add_del_adjacency (adj_args))
1196 clib_warning ("failed to del adjacency!");
1198 vnet_lisp_del_mapping (e, NULL);
1201 vec_free (a.eids_to_be_deleted);
1205 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1207 fib_node_index_t fei;
1208 fib_prefix_t prefix;
1209 fib_entry_flag_t flags;
1211 ip_address_to_fib_prefix (addr, &prefix);
1213 fei = fib_table_lookup (0, &prefix);
1214 flags = fib_entry_get_flags (fei);
1215 return (FIB_ENTRY_FLAG_LOCAL & flags);
1219 * Adds/updates mapping. Does not program forwarding.
1221 * @param a parameters of the new mapping
1222 * @param rlocs vector of remote locators
1223 * @param res_map_index index of the newly created mapping
1224 * @param locators_changed indicator if locators were updated in the mapping
1225 * @return return code
1228 vnet_lisp_add_mapping (vnet_lisp_add_del_mapping_args_t * a,
1230 u32 * res_map_index, u8 * is_updated)
1232 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1233 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1234 u32 mi, ls_index = 0, dst_map_index;
1238 if (vnet_lisp_enable_disable_status () == 0)
1240 clib_warning ("LISP is disabled!");
1241 return VNET_API_ERROR_LISP_DISABLED;
1245 res_map_index[0] = ~0;
1249 clib_memset (ls_args, 0, sizeof (ls_args[0]));
1251 ls_args->locators = rlocs;
1252 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
1253 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1255 /* check if none of the locators match locally configured address */
1256 vec_foreach (loc, rlocs)
1258 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1259 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1261 clib_warning ("RLOC %U matches a local address!",
1262 format_gid_address, &loc->address);
1263 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1267 /* overwrite: if mapping already exists, decide if locators should be
1268 * updated and be done */
1269 if (old_map && gid_address_cmp (&old_map->eid, &a->eid) == 0)
1271 if (!a->is_static && (old_map->is_static || old_map->local))
1273 /* do not overwrite local or static remote mappings */
1274 clib_warning ("mapping %U rejected due to collision with local "
1275 "or static remote mapping!", format_gid_address,
1280 locator_set_t *old_ls;
1282 /* update mapping attributes */
1283 old_map->action = a->action;
1284 if (old_map->action != a->action && NULL != is_updated)
1287 old_map->authoritative = a->authoritative;
1288 old_map->ttl = a->ttl;
1290 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1291 old_map->locator_set_index);
1292 if (compare_locators (lcm, old_ls->locator_indices, ls_args->locators))
1294 /* set locator-set index to overwrite */
1295 ls_args->is_add = 1;
1296 ls_args->index = old_map->locator_set_index;
1297 vnet_lisp_add_del_locator_set (ls_args, 0);
1302 res_map_index[0] = mi;
1309 remove_overlapping_sub_prefixes (lcm, &a->eid, 0 == ls_args->locators);
1311 ls_args->is_add = 1;
1312 ls_args->index = ~0;
1314 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1318 a->locator_set_index = ls_index;
1319 vnet_lisp_map_cache_add_del (a, &dst_map_index);
1322 res_map_index[0] = dst_map_index;
1330 * Removes a mapping. Does not program forwarding.
1332 * @param eid end-host identifier
1333 * @param res_map_index index of the removed mapping
1334 * @return return code
1337 vnet_lisp_del_mapping (gid_address_t * eid, u32 * res_map_index)
1339 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1340 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1341 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1345 clib_memset (ls_args, 0, sizeof (ls_args[0]));
1346 clib_memset (m_args, 0, sizeof (m_args[0]));
1348 res_map_index[0] = ~0;
1350 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1351 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1353 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1355 clib_warning ("cannot delete mapping for eid %U",
1356 format_gid_address, eid);
1361 gid_address_copy (&m_args->eid, eid);
1362 m_args->locator_set_index = old_map->locator_set_index;
1364 ls_args->is_add = 0;
1365 ls_args->index = old_map->locator_set_index;
1367 /* delete timer associated to the mapping if any */
1368 if (old_map->timer_set)
1369 TW (tw_timer_stop) (&lcm->wheel, old_map->timer_handle);
1371 /* delete locator set */
1372 vnet_lisp_add_del_locator_set (ls_args, 0);
1374 /* delete mapping associated from map-cache */
1375 vnet_lisp_map_cache_add_del (m_args, 0);
1377 /* return old mapping index */
1379 res_map_index[0] = mi;
1386 vnet_lisp_clear_all_remote_adjacencies (void)
1389 u32 mi, *map_indices = 0, *map_indexp;
1390 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1391 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1392 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1395 pool_foreach_index (mi, lcm->mapping_pool,
1397 vec_add1 (map_indices, mi);
1401 vec_foreach (map_indexp, map_indices)
1403 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1406 dp_del_fwd_entry (lcm, map_indexp[0]);
1408 dm_args->is_add = 0;
1409 gid_address_copy (&dm_args->eid, &map->eid);
1410 dm_args->locator_set_index = map->locator_set_index;
1412 /* delete mapping associated to fwd entry */
1413 vnet_lisp_map_cache_add_del (dm_args, 0);
1417 ls->index = map->locator_set_index;
1418 /* delete locator set */
1419 rv = vnet_lisp_add_del_locator_set (ls, 0);
1427 vec_free (map_indices);
1432 * Adds adjacency or removes forwarding entry associated to remote mapping.
1433 * Note that adjacencies are not stored, they only result in forwarding entries
1437 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1439 lisp_cp_main_t *lcm = &lisp_control_main;
1440 u32 local_mi, remote_mi = ~0;
1442 if (vnet_lisp_enable_disable_status () == 0)
1444 clib_warning ("LISP is disabled!");
1445 return VNET_API_ERROR_LISP_DISABLED;
1448 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1449 &a->reid, &a->leid);
1450 if (GID_LOOKUP_MISS == remote_mi)
1452 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1453 format_gid_address, &a->reid);
1460 /* check if source eid has an associated mapping. If pitr mode is on,
1461 * just use the pitr's mapping */
1462 if (lcm->flags & LISP_FLAG_PITR_MODE)
1464 if (lcm->pitr_map_index != ~0)
1466 local_mi = lcm->pitr_map_index;
1470 /* PITR mode is on, but no mapping is configured */
1476 if (gid_address_type (&a->reid) == GID_ADDR_NSH)
1478 if (lcm->nsh_map_index == ~0)
1479 local_mi = GID_LOOKUP_MISS;
1481 local_mi = lcm->nsh_map_index;
1485 local_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1490 if (GID_LOOKUP_MISS == local_mi)
1492 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1493 format_gid_address, &a->leid);
1498 /* update forwarding */
1499 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1502 dp_del_fwd_entry (lcm, remote_mi);
1508 vnet_lisp_set_map_request_mode (u8 mode)
1510 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1512 if (vnet_lisp_enable_disable_status () == 0)
1514 clib_warning ("LISP is disabled!");
1515 return VNET_API_ERROR_LISP_DISABLED;
1518 if (mode >= _MR_MODE_MAX)
1520 clib_warning ("Invalid LISP map request mode %d!", mode);
1521 return VNET_API_ERROR_INVALID_ARGUMENT;
1524 lcm->map_request_mode = mode;
1529 vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add)
1531 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1532 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1533 u32 locator_set_index = ~0;
1537 if (vnet_lisp_enable_disable_status () == 0)
1539 clib_warning ("LISP is disabled!");
1540 return VNET_API_ERROR_LISP_DISABLED;
1545 if (lcm->nsh_map_index == (u32) ~ 0)
1547 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1550 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1553 locator_set_index = p[0];
1555 pool_get (lcm->mapping_pool, m);
1556 clib_memset (m, 0, sizeof *m);
1557 m->locator_set_index = locator_set_index;
1560 lcm->nsh_map_index = m - lcm->mapping_pool;
1562 if (~0 == vnet_lisp_gpe_add_nsh_iface (lgm))
1568 if (lcm->nsh_map_index != (u32) ~ 0)
1570 /* remove NSH mapping */
1571 pool_put_index (lcm->mapping_pool, lcm->nsh_map_index);
1572 lcm->nsh_map_index = ~0;
1573 vnet_lisp_gpe_del_nsh_iface (lgm);
1580 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1582 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1583 u32 locator_set_index = ~0;
1587 if (vnet_lisp_enable_disable_status () == 0)
1589 clib_warning ("LISP is disabled!");
1590 return VNET_API_ERROR_LISP_DISABLED;
1593 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1596 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1599 locator_set_index = p[0];
1603 pool_get (lcm->mapping_pool, m);
1604 m->locator_set_index = locator_set_index;
1607 lcm->pitr_map_index = m - lcm->mapping_pool;
1611 /* remove pitr mapping */
1612 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1613 lcm->pitr_map_index = ~0;
1619 vnet_lisp_map_register_fallback_threshold_set (u32 value)
1621 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1624 return VNET_API_ERROR_INVALID_ARGUMENT;
1627 lcm->max_expired_map_registers = value;
1632 vnet_lisp_map_register_fallback_threshold_get (void)
1634 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1635 return lcm->max_expired_map_registers;
1639 * Configure Proxy-ETR
1641 * @param ip PETR's IP address
1642 * @param is_add Flag that indicates if this is an addition or removal
1644 * return 0 on success
1647 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1649 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1652 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1655 if (vnet_lisp_enable_disable_status () == 0)
1657 clib_warning ("LISP is disabled!");
1658 return VNET_API_ERROR_LISP_DISABLED;
1661 clib_memset (ls_args, 0, sizeof (*ls_args));
1665 /* Create placeholder petr locator-set */
1666 clib_memset (&loc, 0, sizeof (loc));
1667 gid_address_from_ip (&loc.address, ip);
1669 loc.state = loc.weight = 1;
1672 ls_args->is_add = 1;
1673 ls_args->index = ~0;
1674 vec_add1 (ls_args->locators, loc);
1675 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1677 /* Add petr mapping */
1678 pool_get (lcm->mapping_pool, m);
1679 m->locator_set_index = ls_index;
1680 lcm->petr_map_index = m - lcm->mapping_pool;
1682 /* Enable use-petr */
1683 lcm->flags |= LISP_FLAG_USE_PETR;
1687 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1689 /* Remove petr locator */
1690 ls_args->is_add = 0;
1691 ls_args->index = m->locator_set_index;
1692 vnet_lisp_add_del_locator_set (ls_args, 0);
1694 /* Remove petr mapping */
1695 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1697 /* Disable use-petr */
1698 lcm->flags &= ~LISP_FLAG_USE_PETR;
1699 lcm->petr_map_index = ~0;
1704 /* cleans locator to locator-set data and removes locators not part of
1705 * any locator-set */
1707 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1709 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1710 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1711 for (i = 0; i < vec_len (ls->locator_indices); i++)
1713 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1714 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1716 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1718 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1719 if (ls_indexp[0] == lsi)
1723 /* delete index for removed locator-set */
1724 vec_del1 (ls_indexes[0], j);
1726 /* delete locator if it's part of no locator-set */
1727 if (vec_len (ls_indexes[0]) == 0)
1729 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1730 vec_add1 (to_be_deleted, i);
1736 for (i = 0; i < vec_len (to_be_deleted); i++)
1738 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1739 vec_del1 (ls->locator_indices, loc_indexp[0]);
1741 vec_free (to_be_deleted);
1745 static inline uword *
1746 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1748 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1753 /* find locator-set */
1757 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1768 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1774 ASSERT (ls != NULL);
1775 ASSERT (loc != NULL);
1777 vec_foreach (locit, ls->locator_indices)
1779 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1780 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1781 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1783 clib_warning ("Duplicate locator");
1784 return VNET_API_ERROR_VALUE_EXIST;
1792 update_adjacencies_by_map_index (lisp_cp_main_t * lcm,
1793 u32 mapping_index, u8 remove_only)
1797 uword *fei = 0, *rmts_idxp = 0;
1798 u32 **rmts = 0, *remote_idxp = 0, *rmts_copy = 0;
1799 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1800 clib_memset (a, 0, sizeof (*a));
1802 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1806 rmts_idxp = hash_get (lcm->lcl_to_rmt_adjs_by_lcl_idx, mapping_index);
1810 pool_elt_at_index (lcm->lcl_to_rmt_adjacencies, rmts_idxp[0]);
1811 rmts_copy = vec_dup (rmts[0]);
1813 vec_foreach (remote_idxp, rmts_copy)
1815 fei = hash_get (lcm->fwd_entry_by_mapping_index, remote_idxp[0]);
1819 fwd = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
1821 gid_address_copy (&a->leid, &fwd->leid);
1822 gid_address_copy (&a->reid, &fwd->reid);
1823 vnet_lisp_add_del_adjacency (a);
1828 vnet_lisp_add_del_adjacency (a);
1831 vec_free (rmts_copy);
1836 fei = hash_get (lcm->fwd_entry_by_mapping_index, mapping_index);
1840 fwd = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
1842 gid_address_copy (&a->leid, &fwd->leid);
1843 gid_address_copy (&a->reid, &fwd->reid);
1844 vnet_lisp_add_del_adjacency (a);
1849 vnet_lisp_add_del_adjacency (a);
1855 update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm,
1856 u32 ls_index, u8 remove_only)
1861 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1864 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1866 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1868 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1869 update_adjacencies_by_map_index (lcm, map_indexp[0], remove_only);
1874 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1875 u32 ls_index, u32 loc_id)
1877 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1878 u32 **ls_indexes = NULL;
1880 ASSERT (ls != NULL);
1881 ASSERT (locit != NULL);
1883 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1884 pool_put_index (lcm->locator_pool, locit[0]);
1885 vec_del1 (ls->locator_indices, loc_id);
1886 vec_del1 (ls_indexes[0], ls_index);
1890 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1891 locator_set_t * ls, u32 * ls_result)
1893 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1894 locator_t *loc = NULL, *itloc = NULL;
1895 uword _p = (u32) ~ 0, *p = &_p;
1896 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1902 if (vnet_lisp_enable_disable_status () == 0)
1904 clib_warning ("LISP is disabled!");
1905 return VNET_API_ERROR_LISP_DISABLED;
1908 p = get_locator_set_index (a, p);
1911 clib_warning ("locator-set %v doesn't exist", a->name);
1912 return VNET_API_ERROR_INVALID_ARGUMENT;
1917 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1920 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1922 return VNET_API_ERROR_INVALID_ARGUMENT;
1929 ls_result[0] = p[0];
1931 /* allocate locators */
1932 vec_foreach (itloc, a->locators)
1934 ret = is_locator_in_locator_set (lcm, ls, itloc);
1940 pool_get (lcm->locator_pool, loc);
1942 loc_index = loc - lcm->locator_pool;
1944 vec_add1 (ls->locator_indices, loc_index);
1946 vec_validate (lcm->locator_to_locator_sets, loc_index);
1947 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1949 vec_add1 (ls_indexes[0], p[0]);
1957 vec_foreach (itloc, a->locators)
1961 vec_foreach (locit, ls->locator_indices)
1963 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1965 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1968 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1970 else if (0 == loc->local &&
1971 !gid_address_cmp (&loc->address, &itloc->address))
1974 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1979 /* update fwd entries using this locator in DP */
1980 update_fwd_entries_by_locator_set (lcm, ls_index,
1981 vec_len (ls->locator_indices)
1994 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1997 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1999 uword _p = (u32) ~ 0, *p = &_p;
2004 if (vnet_lisp_enable_disable_status () == 0)
2006 clib_warning ("LISP is disabled!");
2007 return VNET_API_ERROR_LISP_DISABLED;
2012 p = get_locator_set_index (a, p);
2015 if (p && p[0] != (u32) ~ 0)
2017 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2020 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2025 /* clean locator to locator-set vectors and remove locators if
2026 * they're not part of another locator-set */
2027 clean_locator_to_locator_set (lcm, p[0]);
2029 /* remove locator indices from locator set */
2030 vec_free (ls->locator_indices);
2035 ls_result[0] = p[0];
2037 /* new locator-set */
2040 pool_get (lcm->locator_set_pool, ls);
2041 clib_memset (ls, 0, sizeof (*ls));
2042 ls_index = ls - lcm->locator_set_pool;
2046 ls->name = vec_dup (a->name);
2048 if (!lcm->locator_set_index_by_name)
2049 lcm->locator_set_index_by_name =
2050 hash_create_vec ( /* size */ 0, sizeof (ls->name[0]),
2052 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2055 /* mark as local locator-set */
2056 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2058 ls->local = a->local;
2060 ls_result[0] = ls_index;
2063 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2071 p = get_locator_set_index (a, p);
2074 clib_warning ("locator-set %v doesn't exists", a->name);
2078 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2081 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2085 if (lcm->mreq_itr_rlocs == p[0])
2087 clib_warning ("Can't delete the locator-set used to constrain "
2088 "the itr-rlocs in map-requests!");
2092 if (vec_len (lcm->locator_set_to_eids) != 0)
2094 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2095 if (vec_len (eid_indexes[0]) != 0)
2098 ("Can't delete a locator that supports a mapping!");
2103 /* clean locator to locator-sets data */
2104 clean_locator_to_locator_set (lcm, p[0]);
2110 vec_foreach_index (it, lcm->local_locator_set_indexes)
2112 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2115 vec_del1 (lcm->local_locator_set_indexes, it);
2119 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2121 vec_free (ls->name);
2122 vec_free (ls->locator_indices);
2123 pool_put (lcm->locator_set_pool, ls);
2129 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2131 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2133 lcm->rloc_probing = is_enable;
2138 vnet_lisp_map_register_enable_disable (u8 is_enable)
2140 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2142 lcm->map_registering = is_enable;
2147 lisp_cp_register_dst_port (vlib_main_t * vm)
2149 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2150 lisp_cp_input_node.index, 1 /* is_ip4 */ );
2151 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2152 lisp_cp_input_node.index, 0 /* is_ip4 */ );
2156 lisp_cp_unregister_dst_port (vlib_main_t * vm)
2158 udp_unregister_dst_port (vm, UDP_DST_PORT_lisp_cp, 0 /* is_ip4 */ );
2159 udp_unregister_dst_port (vm, UDP_DST_PORT_lisp_cp6, 1 /* is_ip4 */ );
2163 * lisp_cp_enable_l2_l3_ifaces
2165 * Enable all l2 and l3 ifaces
2168 lisp_cp_enable_l2_l3_ifaces (lisp_cp_main_t * lcm, u8 with_default_route)
2173 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2174 dp_add_del_iface(lcm, vni, /* is_l2 */ 0, /* is_add */1,
2175 with_default_route);
2177 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2178 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1,
2179 with_default_route);
2185 lisp_cp_disable_l2_l3_ifaces (lisp_cp_main_t * lcm)
2189 /* clear interface table */
2190 hash_free (lcm->fwd_entry_by_mapping_index);
2191 pool_free (lcm->fwd_entry_pool);
2192 /* Clear state tracking rmt-lcl fwd entries */
2194 pool_foreach(rmts, lcm->lcl_to_rmt_adjacencies,
2199 hash_free (lcm->lcl_to_rmt_adjs_by_lcl_idx);
2200 pool_free (lcm->lcl_to_rmt_adjacencies);
2204 vnet_lisp_enable_disable (u8 is_enable)
2206 clib_error_t *error = 0;
2207 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2208 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2210 a->is_en = is_enable;
2211 error = vnet_lisp_gpe_enable_disable (a);
2214 return clib_error_return (0, "failed to %s data-plane!",
2215 a->is_en ? "enable" : "disable");
2218 /* decide what to do based on mode */
2220 if (lcm->flags & LISP_FLAG_XTR_MODE)
2224 lisp_cp_register_dst_port (lcm->vlib_main);
2225 lisp_cp_enable_l2_l3_ifaces (lcm, 1 /* with_default_route */ );
2229 lisp_cp_unregister_dst_port (lcm->vlib_main);
2230 lisp_cp_disable_l2_l3_ifaces (lcm);
2234 if (lcm->flags & LISP_FLAG_PETR_MODE)
2236 /* if in xTR mode, the LISP ports were already (un)registered above */
2237 if (!(lcm->flags & LISP_FLAG_XTR_MODE))
2240 lisp_cp_register_dst_port (lcm->vlib_main);
2242 lisp_cp_unregister_dst_port (lcm->vlib_main);
2246 if (lcm->flags & LISP_FLAG_PITR_MODE)
2250 /* install interfaces, but no default routes */
2251 lisp_cp_enable_l2_l3_ifaces (lcm, 0 /* with_default_route */ );
2255 lisp_cp_disable_l2_l3_ifaces (lcm);
2260 vnet_lisp_create_retry_process (lcm);
2262 /* update global flag */
2263 lcm->is_enabled = is_enable;
2269 vnet_lisp_enable_disable_status (void)
2271 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2272 return lcm->is_enabled;
2276 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2278 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2280 lisp_msmr_t _mr, *mr = &_mr;
2282 if (vnet_lisp_enable_disable_status () == 0)
2284 clib_warning ("LISP is disabled!");
2285 return VNET_API_ERROR_LISP_DISABLED;
2291 if (get_map_resolver (&a->address))
2293 clib_warning ("map-resolver %U already exists!", format_ip_address,
2298 clib_memset (mr, 0, sizeof (*mr));
2299 ip_address_copy (&mr->address, &a->address);
2300 vec_add1 (lcm->map_resolvers, *mr);
2302 if (vec_len (lcm->map_resolvers) == 1)
2303 lcm->do_map_resolver_election = 1;
2307 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2309 mr = vec_elt_at_index (lcm->map_resolvers, i);
2310 if (!ip_address_cmp (&mr->address, &a->address))
2312 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2313 lcm->do_map_resolver_election = 1;
2315 vec_del1 (lcm->map_resolvers, i);
2324 vnet_lisp_map_register_set_ttl (u32 ttl)
2326 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2327 lcm->map_register_ttl = ttl;
2332 vnet_lisp_map_register_get_ttl (void)
2334 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2335 return lcm->map_register_ttl;
2339 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2341 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2344 if (vnet_lisp_enable_disable_status () == 0)
2346 clib_warning ("LISP is disabled!");
2347 return VNET_API_ERROR_LISP_DISABLED;
2352 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2355 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2356 return VNET_API_ERROR_INVALID_ARGUMENT;
2359 lcm->mreq_itr_rlocs = p[0];
2363 lcm->mreq_itr_rlocs = ~0;
2369 /* Statistics (not really errors) */
2370 #define foreach_lisp_cp_lookup_error \
2372 _(MAP_REQUESTS_SENT, "map-request sent") \
2373 _(ARP_REPLY_TX, "ARP replies sent") \
2374 _(NDP_NEIGHBOR_ADVERTISEMENT_TX, \
2375 "neighbor advertisement sent")
2377 static char *lisp_cp_lookup_error_strings[] = {
2378 #define _(sym,string) string,
2379 foreach_lisp_cp_lookup_error
2385 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2386 foreach_lisp_cp_lookup_error
2388 LISP_CP_LOOKUP_N_ERROR,
2389 } lisp_cp_lookup_error_t;
2393 LISP_CP_LOOKUP_NEXT_DROP,
2394 LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX,
2395 LISP_CP_LOOKUP_N_NEXT,
2396 } lisp_cp_lookup_next_t;
2400 gid_address_t dst_eid;
2401 ip_address_t map_resolver_ip;
2402 } lisp_cp_lookup_trace_t;
2405 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2407 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2408 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2409 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2411 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2412 format_ip_address, &t->map_resolver_ip, format_gid_address,
2418 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2419 ip_address_t * sloc)
2424 if (vec_len (lcm->map_resolvers) == 0)
2426 clib_warning ("No map-resolver configured");
2430 /* find the first mr ip we have a route to and the ip of the
2431 * iface that has a route to it */
2432 vec_foreach (mrit, lcm->map_resolvers)
2435 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2437 ip_address_copy (mr_ip, a);
2439 /* also update globals */
2444 clib_warning ("Can't find map-resolver and local interface ip!");
2448 static gid_address_t *
2449 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2455 ip_interface_address_t *ia = 0;
2456 gid_address_t gid_data, *gid = &gid_data;
2457 gid_address_t *rlocs = 0;
2458 ip_prefix_t *ippref = &gid_address_ippref (gid);
2459 ip_address_t *rloc = &ip_prefix_addr (ippref);
2461 clib_memset (gid, 0, sizeof (gid[0]));
2462 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2463 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2465 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2466 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2468 /* Add ipv4 locators first TODO sort them */
2471 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2472 loc->sw_if_index, 1 /* unnumbered */,
2474 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2475 ip_address_set (rloc, addr, AF_IP4);
2476 ip_prefix_len (ippref) = 32;
2477 ip_prefix_normalize (ippref);
2478 vec_add1 (rlocs, gid[0]);
2481 /* Add ipv6 locators */
2482 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2483 loc->sw_if_index, 1 /* unnumbered */,
2485 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2486 ip_address_set (rloc, addr, AF_IP6);
2487 ip_prefix_len (ippref) = 128;
2488 ip_prefix_normalize (ippref);
2489 vec_add1 (rlocs, gid[0]);
2497 static vlib_buffer_t *
2498 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2499 ip_address_t * sloc, ip_address_t * rloc,
2500 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2504 vlib_main_t *vm = lcm->vlib_main;
2506 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2508 clib_warning ("Can't allocate buffer for Map-Request!");
2512 b = vlib_get_buffer (vm, bi);
2514 /* leave some space for the encap headers */
2515 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2518 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2519 1 /* rloc probe */ , nonce_res);
2521 /* push outer ip header */
2522 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2530 static vlib_buffer_t *
2531 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2532 gid_address_t * seid, gid_address_t * deid,
2533 locator_set_t * loc_set, ip_address_t * mr_ip,
2534 ip_address_t * sloc, u8 is_smr_invoked,
2535 u64 * nonce_res, u32 * bi_res)
2539 gid_address_t *rlocs = 0;
2540 vlib_main_t *vm = lcm->vlib_main;
2542 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2544 clib_warning ("Can't allocate buffer for Map-Request!");
2548 b = vlib_get_buffer (vm, bi);
2551 /* leave some space for the encap headers */
2552 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2555 rlocs = build_itr_rloc_list (lcm, loc_set);
2557 if (MR_MODE_SRC_DST == lcm->map_request_mode
2558 && GID_ADDR_SRC_DST != gid_address_type (deid))
2561 clib_memset (&sd, 0, sizeof (sd));
2562 build_src_dst (&sd, seid, deid);
2563 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2564 0 /* rloc probe */ , nonce_res);
2569 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2570 0 /* rloc probe */ , nonce_res);
2573 /* push ecm: udp-ip-lisp */
2574 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2576 /* push outer ip header */
2577 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2587 reset_pending_mr_counters (pending_map_request_t * r)
2589 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2593 #define foreach_msmr \
2599 elect_map_ ## name (lisp_cp_main_t * lcm) \
2602 vec_foreach (mr, lcm->map_ ## name ## s) \
2606 ip_address_copy (&lcm->active_map_ ##name, &mr->address); \
2607 lcm->do_map_ ## name ## _election = 0; \
2616 free_map_register_records (mapping_t * maps)
2619 vec_foreach (map, maps) vec_free (map->locators);
2625 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2626 ip_address_t * probed_loc)
2629 locator_t *loc, new;
2630 ip_interface_address_t *ia = 0;
2632 ip_address_t *new_ip = &gid_address_ip (&new.address);
2635 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2637 vec_foreach (li, ls->locator_indices)
2639 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2644 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2645 loc->sw_if_index, 1 /* unnumbered */,
2647 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2649 ip_address_set (new_ip, addr, AF_IP4);
2652 /* Add ipv6 locators */
2653 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2654 loc->sw_if_index, 1 /* unnumbered */,
2656 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2658 ip_address_set (new_ip, addr, AF_IP6);
2662 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2665 vec_add1 (m->locators, new);
2670 build_map_register_record_list (lisp_cp_main_t * lcm)
2672 mapping_t *recs = 0, rec, *m;
2675 pool_foreach(m, lcm->mapping_pool,
2677 /* for now build only local mappings */
2682 add_locators (lcm, &rec, m->locator_set_index, NULL);
2683 vec_add1 (recs, rec);
2690 static vnet_crypto_alg_t
2691 lisp_key_type_to_crypto_alg (lisp_key_type_t key_id)
2696 return VNET_CRYPTO_ALG_HMAC_SHA1;
2697 case HMAC_SHA_256_128:
2698 return VNET_CRYPTO_ALG_HMAC_SHA256;
2700 clib_warning ("unsupported encryption key type: %d!", key_id);
2703 return VNET_CRYPTO_ALG_NONE;
2706 static vnet_crypto_op_id_t
2707 lisp_key_type_to_crypto_op (lisp_key_type_t key_id)
2712 return VNET_CRYPTO_OP_SHA1_HMAC;
2713 case HMAC_SHA_256_128:
2714 return VNET_CRYPTO_OP_SHA256_HMAC;
2716 clib_warning ("unsupported encryption key type: %d!", key_id);
2719 return VNET_CRYPTO_OP_NONE;
2723 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2724 lisp_key_type_t key_id, u8 * key,
2725 u16 auth_data_len, u32 msg_len)
2727 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2728 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2729 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2730 vnet_crypto_op_t _op, *op = &_op;
2731 vnet_crypto_key_index_t ki;
2733 vnet_crypto_op_init (op, lisp_key_type_to_crypto_op (key_id));
2735 op->digest = MREG_DATA (map_reg_hdr);
2736 op->src = (u8 *) map_reg_hdr;
2740 ki = vnet_crypto_key_add (lcm->vlib_main,
2741 lisp_key_type_to_crypto_alg (key_id), key,
2746 vnet_crypto_process_ops (lcm->vlib_main, op, 1);
2747 vnet_crypto_key_del (lcm->vlib_main, ki);
2752 static vlib_buffer_t *
2753 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2754 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2755 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2760 u32 bi, auth_data_len = 0, msg_len = 0;
2761 vlib_main_t *vm = lcm->vlib_main;
2763 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2765 clib_warning ("Can't allocate buffer for Map-Register!");
2769 b = vlib_get_buffer (vm, bi);
2771 /* leave some space for the encap headers */
2772 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2774 auth_data_len = auth_data_len_by_key_id (key_id);
2775 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2776 auth_data_len, nonce_res,
2779 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2782 /* push outer ip header */
2783 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2792 get_egress_map_ ##name## _ip (lisp_cp_main_t * lcm, ip_address_t * ip) \
2795 while (lcm->do_map_ ## name ## _election \
2796 | (0 == ip_fib_get_first_egress_ip_for_dst \
2797 (lcm, &lcm->active_map_ ##name, ip))) \
2799 if (0 == elect_map_ ## name (lcm)) \
2800 /* all map resolvers/servers are down */ \
2802 /* restart MR/MS checking by marking all of them up */ \
2803 vec_foreach (mr, lcm->map_ ## name ## s) mr->is_down = 0; \
2812 /* CP output statistics */
2813 #define foreach_lisp_cp_output_error \
2814 _(MAP_REGISTERS_SENT, "map-registers sent") \
2815 _(MAP_REQUESTS_SENT, "map-requests sent") \
2816 _(RLOC_PROBES_SENT, "rloc-probes sent")
2817 static char *lisp_cp_output_error_strings[] = {
2818 #define _(sym,string) string,
2819 foreach_lisp_cp_output_error
2825 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2826 foreach_lisp_cp_output_error
2828 LISP_CP_OUTPUT_N_ERROR,
2829 } lisp_cp_output_error_t;
2832 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2833 vlib_frame_t * from_frame)
2838 /* placeholder node used only for statistics */
2840 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2841 .function = lisp_cp_output,
2842 .name = "lisp-cp-output",
2843 .vector_size = sizeof (u32),
2844 .format_trace = format_lisp_cp_input_trace,
2845 .type = VLIB_NODE_TYPE_INTERNAL,
2847 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2848 .error_strings = lisp_cp_output_error_strings,
2850 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2853 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2859 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2860 u32 local_locator_set_index, ip_address_t * sloc,
2861 ip_address_t * rloc)
2868 u32 next_index, *to_next;
2869 gid_address_t *itr_rlocs;
2871 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2872 itr_rlocs = build_itr_rloc_list (lcm, ls);
2874 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2875 vec_free (itr_rlocs);
2879 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2881 next_index = (ip_addr_version (rloc) == AF_IP4) ?
2882 ip4_lookup_node.index : ip6_lookup_node.index;
2884 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2886 /* Enqueue the packet */
2887 to_next = vlib_frame_vector_args (f);
2890 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2896 send_rloc_probes (lisp_cp_main_t * lcm)
2902 u32 si, rloc_probes_sent = 0;
2905 pool_foreach (e, lcm->fwd_entry_pool,
2907 if (vec_len (e->locator_pairs) == 0)
2910 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2913 clib_warning ("internal error: cannot find local eid %U in "
2914 "map-cache!", format_gid_address, &e->leid);
2917 lm = pool_elt_at_index (lcm->mapping_pool, si);
2919 /* get the best (lowest) priority */
2920 lprio = e->locator_pairs[0].priority;
2922 /* send rloc-probe for pair(s) with the best remote locator priority */
2923 vec_foreach (lp, e->locator_pairs)
2925 if (lp->priority != lprio)
2928 /* get first remote locator */
2929 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2936 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2937 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2943 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2945 pending_map_register_t *pmr;
2946 u32 bi, map_registers_sent = 0;
2951 u32 next_index, *to_next;
2952 mapping_t *records, *r, *group, *k;
2954 if (get_egress_map_server_ip (lcm, &sloc) < 0)
2957 records = build_map_register_record_list (lcm);
2961 vec_foreach (r, records)
2964 u8 key_id = r->key_id;
2967 continue; /* no secret key -> map-register cannot be sent */
2970 vec_add1 (group, r[0]);
2972 /* group mappings that share common key */
2973 for (k = r + 1; k < vec_end (records); k++)
2975 if (k->key_id != r->key_id)
2978 if (vec_is_equal (k->key, r->key))
2980 vec_add1 (group, k[0]);
2981 k->key = 0; /* don't process this mapping again */
2985 b = build_map_register (lcm, &sloc, &lcm->active_map_server, &nonce,
2986 want_map_notif, group, key_id, key, &bi);
2991 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2993 next_index = (ip_addr_version (&lcm->active_map_server) == AF_IP4) ?
2994 ip4_lookup_node.index : ip6_lookup_node.index;
2996 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2998 /* Enqueue the packet */
2999 to_next = vlib_frame_vector_args (f);
3002 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3003 map_registers_sent++;
3005 pool_get (lcm->pending_map_registers_pool, pmr);
3006 clib_memset (pmr, 0, sizeof (*pmr));
3007 pmr->time_to_expire = PENDING_MREG_EXPIRATION_TIME;
3008 hash_set (lcm->map_register_messages_by_nonce, nonce,
3009 pmr - lcm->pending_map_registers_pool);
3011 free_map_register_records (records);
3013 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
3014 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
3015 map_registers_sent);
3020 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3021 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3023 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3024 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3027 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3028 gid_address_t * seid, gid_address_t * deid,
3029 u8 is_smr_invoked, u8 is_resend)
3031 u32 next_index, bi = 0, *to_next, map_index;
3035 locator_set_t *loc_set;
3037 pending_map_request_t *pmr, *duplicate_pmr = 0;
3041 /* if there is already a pending request remember it */
3044 pool_foreach(pmr, lcm->pending_map_requests_pool,
3046 if (!gid_address_cmp (&pmr->src, seid)
3047 && !gid_address_cmp (&pmr->dst, deid))
3049 duplicate_pmr = pmr;
3055 if (!is_resend && duplicate_pmr)
3057 /* don't send the request if there is a pending map request already */
3061 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
3063 /* get locator-set for seid */
3064 if (!pitr_mode && gid_address_type (deid) != GID_ADDR_NSH)
3066 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3067 if (map_index == ~0)
3069 clib_warning ("No local mapping found in eid-table for %U!",
3070 format_gid_address, seid);
3074 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3079 ("Mapping found for src eid %U is not marked as local!",
3080 format_gid_address, seid);
3083 ls_index = map->locator_set_index;
3089 if (lcm->pitr_map_index != ~0)
3092 pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3093 ls_index = map->locator_set_index;
3102 if (lcm->nsh_map_index == (u32) ~ 0)
3104 clib_warning ("No locator-set defined for NSH!");
3109 map = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index);
3110 ls_index = map->locator_set_index;
3115 /* overwrite locator set if map-request itr-rlocs configured */
3116 if (~0 != lcm->mreq_itr_rlocs)
3118 ls_index = lcm->mreq_itr_rlocs;
3121 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3123 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3126 duplicate_pmr->to_be_removed = 1;
3130 /* build the encapsulated map request */
3131 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3132 &lcm->active_map_resolver,
3133 &sloc, is_smr_invoked, &nonce, &bi);
3138 /* set fib index to default and lookup node */
3139 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3140 next_index = (ip_addr_version (&lcm->active_map_resolver) == AF_IP4) ?
3141 ip4_lookup_node.index : ip6_lookup_node.index;
3143 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3145 /* Enqueue the packet */
3146 to_next = vlib_frame_vector_args (f);
3149 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3151 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
3152 LISP_CP_OUTPUT_ERROR_MAP_REQUESTS_SENT, 1);
3155 /* if there is a pending request already update it */
3157 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3159 /* remove the oldest nonce */
3160 u64 CLIB_UNUSED (tmp), *nonce_del;
3161 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3162 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3163 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3166 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3167 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3168 duplicate_pmr - lcm->pending_map_requests_pool);
3172 /* add map-request to pending requests table */
3173 pool_get (lcm->pending_map_requests_pool, pmr);
3174 clib_memset (pmr, 0, sizeof (*pmr));
3175 gid_address_copy (&pmr->src, seid);
3176 gid_address_copy (&pmr->dst, deid);
3177 clib_fifo_add1 (pmr->nonces, nonce);
3178 pmr->is_smr_invoked = is_smr_invoked;
3179 reset_pending_mr_counters (pmr);
3180 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3181 pmr - lcm->pending_map_requests_pool);
3188 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3190 ip4_header_t *ip4 = hdr;
3193 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3195 ip_address_set (src, &ip4->src_address, AF_IP4);
3196 ip_address_set (dst, &ip4->dst_address, AF_IP4);
3201 ip_address_set (src, &ip6->src_address, AF_IP6);
3202 ip_address_set (dst, &ip6->dst_address, AF_IP6);
3207 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3211 u32 vni = ~0, table_id = ~0;
3213 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3221 vnip = hash_get (lcm->vni_by_table_id, table_id);
3225 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3231 lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
3235 l2input_main_t *l2im = &l2input_main;
3236 l2_input_config_t *config;
3237 l2_bridge_domain_t *bd_config;
3239 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3240 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3241 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3243 return bd_config->bd_id;
3247 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3251 u32 bd = lisp_get_bd_from_buffer_eth (b);
3253 vnip = hash_get (lcm->vni_by_bd_id, bd);
3257 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
3263 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3264 gid_address_t * src, gid_address_t * dst,
3267 ethernet_header_t *eh;
3269 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
3271 clib_memset (src, 0, sizeof (*src));
3272 clib_memset (dst, 0, sizeof (*dst));
3274 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
3275 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
3277 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3280 u8 version, preflen;
3282 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3283 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3285 ip = vlib_buffer_get_current (b);
3286 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3288 version = gid_address_ip_version (src);
3289 preflen = ip_address_max_len (version);
3290 gid_address_ippref_len (src) = preflen;
3291 gid_address_ippref_len (dst) = preflen;
3293 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3294 gid_address_vni (dst) = vni;
3295 gid_address_vni (src) = vni;
3297 else if (LISP_AFI_MAC == type)
3299 ethernet_arp_header_t *ah;
3301 eh = vlib_buffer_get_current (b);
3303 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
3305 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
3306 gid_address_type (dst) = GID_ADDR_ARP;
3308 if (clib_net_to_host_u16 (ah->opcode)
3309 != ETHERNET_ARP_OPCODE_request)
3311 clib_memset (&gid_address_arp_ndp_ip (dst), 0,
3312 sizeof (ip_address_t));
3313 ip_addr_version (&gid_address_arp_ndp_ip (dst)) = AF_IP4;
3314 gid_address_arp_ndp_bd (dst) = ~0;
3318 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
3319 clib_memcpy (&gid_address_arp_ip4 (dst),
3320 &ah->ip4_over_ethernet[1].ip4, 4);
3324 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_IP6)
3327 ip = (ip6_header_t *) (eh + 1);
3329 if (IP_PROTOCOL_ICMP6 == ip->protocol)
3331 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3332 ndh = ip6_next_header (ip);
3333 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3335 gid_address_type (dst) = GID_ADDR_NDP;
3337 /* check that source link layer address option is present */
3338 opt = (void *) (ndh + 1);
3339 if ((opt->header.type !=
3340 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address)
3341 || (opt->header.n_data_u64s != 1))
3343 clib_memset (&gid_address_arp_ndp_ip (dst), 0,
3344 sizeof (ip_address_t));
3345 ip_addr_version (&gid_address_arp_ndp_ip (dst)) =
3347 gid_address_arp_ndp_bd (dst) = ~0;
3348 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
3352 gid_address_ndp_bd (dst) =
3353 lisp_get_bd_from_buffer_eth (b);
3354 ip_address_set (&gid_address_arp_ndp_ip (dst),
3355 &ndh->target_address, AF_IP6);
3361 gid_address_type (src) = GID_ADDR_MAC;
3362 gid_address_type (dst) = GID_ADDR_MAC;
3363 mac_copy (&gid_address_mac (src), eh->src_address);
3364 mac_copy (&gid_address_mac (dst), eh->dst_address);
3367 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3369 gid_address_vni (dst) = vni;
3370 gid_address_vni (src) = vni;
3373 else if (LISP_AFI_LCAF == type)
3376 eh = vlib_buffer_get_current (b);
3378 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_NSH)
3380 nh = (lisp_nsh_hdr_t *) (((u8 *) eh) + sizeof (*eh));
3381 u32 spi = clib_net_to_host_u32 (nh->spi_si << 8);
3382 u8 si = (u8) clib_net_to_host_u32 (nh->spi_si);
3383 gid_address_nsh_spi (dst) = spi;
3384 gid_address_nsh_si (dst) = si;
3386 gid_address_type (dst) = GID_ADDR_NSH;
3387 gid_address_type (src) = GID_ADDR_NSH;
3393 lisp_cp_lookup_inline (vlib_main_t * vm,
3394 vlib_node_runtime_t * node,
3395 vlib_frame_t * from_frame, int overlay)
3397 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
3398 u32 *from, *to_next, di, si;
3399 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3401 uword n_left_from, n_left_to_next;
3402 vnet_main_t *vnm = vnet_get_main ();
3404 from = vlib_frame_vector_args (from_frame);
3405 n_left_from = from_frame->n_vectors;
3406 next_index = node->cached_next_index;
3408 while (n_left_from > 0)
3410 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3412 while (n_left_from > 0 && n_left_to_next > 0)
3414 u32 pi0, sw_if_index0, next0;
3417 gid_address_t src, dst;
3418 ethernet_arp_header_t *arp0;
3419 ethernet_header_t *eth0;
3420 vnet_hw_interface_t *hw_if0;
3421 ethernet_header_t *eh0;
3422 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3430 n_left_to_next -= 1;
3432 b0 = vlib_get_buffer (vm, pi0);
3434 /* src/dst eid pair */
3435 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
3437 if (gid_address_type (&dst) == GID_ADDR_ARP)
3439 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3440 if (GID_LOOKUP_MISS_L2 == mac0)
3443 /* send ARP reply */
3444 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3445 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
3447 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3449 eth0 = vlib_buffer_get_current (b0);
3450 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
3452 arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
3453 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
3454 mac_address_from_u64 (&arp0->ip4_over_ethernet[0].mac, mac0);
3455 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
3456 &gid_address_arp_ip4 (&dst), 4);
3458 /* Hardware must be ethernet-like. */
3459 ASSERT (vec_len (hw_if0->hw_address) == 6);
3461 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
3462 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
3464 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
3465 next0 = LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX;
3468 else if (gid_address_type (&dst) == GID_ADDR_NDP)
3470 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3471 if (GID_LOOKUP_MISS_L2 == mac0)
3474 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3475 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
3477 eh0 = vlib_buffer_get_current (b0);
3478 ip0 = (ip6_header_t *) (eh0 + 1);
3479 ndh = ip6_next_header (ip0);
3481 ip0->dst_address = ip0->src_address;
3482 ip0->src_address = ndh->target_address;
3483 ip0->hop_limit = 255;
3484 opt = (void *) (ndh + 1);
3486 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3487 clib_memcpy (opt->ethernet_address, (u8 *) & mac0, 6);
3488 ndh->icmp.type = ICMP6_neighbor_advertisement;
3489 ndh->advertisement_flags = clib_host_to_net_u32
3490 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3491 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3492 ndh->icmp.checksum = 0;
3493 ndh->icmp.checksum =
3494 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
3496 clib_memcpy (eh0->dst_address, eh0->src_address, 6);
3497 clib_memcpy (eh0->src_address, (u8 *) & mac0, 6);
3500 [LISP_CP_LOOKUP_ERROR_NDP_NEIGHBOR_ADVERTISEMENT_TX];
3501 next0 = LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX;
3505 /* if we have remote mapping for destination already in map-cache
3506 add forwarding tunnel directly. If not send a map-request */
3507 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3511 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3512 /* send a map-request also in case of negative mapping entry
3513 with corresponding action */
3514 if (m->action == LISP_SEND_MAP_REQUEST)
3516 /* send map-request */
3517 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3518 0 /* is_resend */ );
3522 if (GID_ADDR_NSH != gid_address_type (&dst))
3524 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3528 si = lcm->nsh_map_index;
3532 dp_add_fwd_entry_from_mt (si, di);
3538 /* send map-request */
3539 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3540 0 /* is_resend */ );
3544 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3545 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3547 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3549 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3552 clib_memset (tr, 0, sizeof (*tr));
3553 if ((gid_address_type (&dst) == GID_ADDR_NDP) ||
3554 (gid_address_type (&dst) == GID_ADDR_ARP))
3555 clib_memcpy (&tr->dst_eid, &dst, sizeof (gid_address_t));
3557 gid_address_copy (&tr->dst_eid, &dst);
3558 ip_address_copy (&tr->map_resolver_ip,
3559 &lcm->active_map_resolver);
3561 gid_address_free (&dst);
3562 gid_address_free (&src);
3563 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3565 n_left_to_next, pi0, next0);
3568 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3570 return from_frame->n_vectors;
3574 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3575 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3577 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3581 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3582 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3584 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3588 lisp_cp_lookup_l2 (vlib_main_t * vm,
3589 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3591 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3595 lisp_cp_lookup_nsh (vlib_main_t * vm,
3596 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3598 /* TODO decide if NSH should be propagated as LCAF or not */
3599 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3603 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3604 .function = lisp_cp_lookup_ip4,
3605 .name = "lisp-cp-lookup-ip4",
3606 .vector_size = sizeof (u32),
3607 .format_trace = format_lisp_cp_lookup_trace,
3608 .type = VLIB_NODE_TYPE_INTERNAL,
3610 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3611 .error_strings = lisp_cp_lookup_error_strings,
3613 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3616 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3617 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
3623 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3624 .function = lisp_cp_lookup_ip6,
3625 .name = "lisp-cp-lookup-ip6",
3626 .vector_size = sizeof (u32),
3627 .format_trace = format_lisp_cp_lookup_trace,
3628 .type = VLIB_NODE_TYPE_INTERNAL,
3630 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3631 .error_strings = lisp_cp_lookup_error_strings,
3633 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3636 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3637 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
3643 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3644 .function = lisp_cp_lookup_l2,
3645 .name = "lisp-cp-lookup-l2",
3646 .vector_size = sizeof (u32),
3647 .format_trace = format_lisp_cp_lookup_trace,
3648 .type = VLIB_NODE_TYPE_INTERNAL,
3650 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3651 .error_strings = lisp_cp_lookup_error_strings,
3653 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3656 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3657 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
3663 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3664 .function = lisp_cp_lookup_nsh,
3665 .name = "lisp-cp-lookup-nsh",
3666 .vector_size = sizeof (u32),
3667 .format_trace = format_lisp_cp_lookup_trace,
3668 .type = VLIB_NODE_TYPE_INTERNAL,
3670 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3671 .error_strings = lisp_cp_lookup_error_strings,
3673 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3676 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3677 [LISP_CP_LOOKUP_NEXT_ARP_NDP_REPLY_TX] = "interface-output",
3682 /* lisp_cp_input statistics */
3683 #define foreach_lisp_cp_input_error \
3685 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3686 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3687 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
3688 _(MAP_REPLIES_RECEIVED, "map-replies received")
3690 static char *lisp_cp_input_error_strings[] = {
3691 #define _(sym,string) string,
3692 foreach_lisp_cp_input_error
3698 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3699 foreach_lisp_cp_input_error
3701 LISP_CP_INPUT_N_ERROR,
3702 } lisp_cp_input_error_t;
3706 gid_address_t dst_eid;
3707 ip4_address_t map_resolver_ip;
3708 } lisp_cp_input_trace_t;
3711 format_lisp_cp_input_trace (u8 * s, va_list * args)
3713 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3714 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3715 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3716 va_arg (*args, lisp_cp_input_trace_t *);
3718 s = format (s, "LISP-CP-INPUT: TODO");
3723 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3726 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3727 clib_memset (adj_args, 0, sizeof (adj_args[0]));
3729 m = pool_elt_at_index (lcm->mapping_pool, mi);
3731 gid_address_copy (&adj_args->reid, &m->eid);
3732 adj_args->is_add = 0;
3733 if (vnet_lisp_add_del_adjacency (adj_args))
3734 clib_warning ("failed to del adjacency!");
3736 TW (tw_timer_stop) (&lcm->wheel, m->timer_handle);
3737 vnet_lisp_del_mapping (&m->eid, NULL);
3741 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3742 f64 expiration_time)
3745 u64 now = clib_cpu_time_now ();
3746 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3747 u64 exp_clock_time = now + expiration_time * cpu_cps;
3749 m = pool_elt_at_index (lcm->mapping_pool, mi);
3752 m->timer_handle = TW (tw_timer_start) (&lcm->wheel, mi, 0, exp_clock_time);
3756 process_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3759 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
3760 mapping_t *m = pool_elt_at_index (lcm->mapping_pool, mi);
3766 if (m->delete_after_expiration)
3768 remove_expired_mapping (lcm, mi);
3772 fei = hash_get (lcm->fwd_entry_by_mapping_index, mi);
3776 fe = pool_elt_at_index (lcm->fwd_entry_pool, fei[0]);
3778 clib_memset (a, 0, sizeof (*a));
3779 a->rmt_eid = fe->reid;
3781 a->lcl_eid = fe->leid;
3782 a->vni = gid_address_vni (&fe->reid);
3784 rv = vnet_lisp_gpe_get_fwd_stats (a, &c);
3788 if (m->almost_expired)
3790 m->almost_expired = 0; /* reset flag */
3793 if (m->packets != c.packets)
3795 /* mapping is in use, re-fetch */
3796 map_request_args_t mr_args;
3797 clib_memset (&mr_args, 0, sizeof (mr_args));
3798 mr_args.seid = fe->leid;
3799 mr_args.deid = fe->reid;
3801 send_map_request_thread_fn (&mr_args);
3804 remove_expired_mapping (lcm, mi);
3807 remove_expired_mapping (lcm, mi);
3811 m->almost_expired = 1;
3812 mapping_start_expiration_timer (lcm, mi, TIME_UNTIL_REFETCH_OR_DELETE);
3816 m->packets = c.packets;
3818 m->delete_after_expiration = 1;
3823 map_records_arg_free (map_records_arg_t * a)
3825 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3827 vec_foreach (m, a->mappings)
3829 vec_free (m->locators);
3830 gid_address_free (&m->eid);
3832 pool_put (lcm->map_records_args_pool[vlib_get_thread_index ()], a);
3836 process_map_reply (map_records_arg_t * a)
3839 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3840 u32 dst_map_index = 0;
3841 pending_map_request_t *pmr;
3846 if (a->is_rloc_probe)
3849 /* Check pending requests table and nonce */
3850 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3853 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3856 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3858 vec_foreach (m, a->mappings)
3860 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
3861 clib_memset (m_args, 0, sizeof (m_args[0]));
3862 gid_address_copy (&m_args->eid, &m->eid);
3863 m_args->action = m->action;
3864 m_args->authoritative = m->authoritative;
3865 m_args->ttl = m->ttl;
3866 m_args->is_static = 0;
3868 /* insert/update mappings cache */
3869 vnet_lisp_add_mapping (m_args, m->locators, &dst_map_index, &is_changed);
3871 if (dst_map_index == (u32) ~ 0)
3876 /* try to program forwarding only if mapping saved or updated */
3877 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3878 clib_memset (adj_args, 0, sizeof (adj_args[0]));
3880 gid_address_copy (&adj_args->leid, &pmr->src);
3881 gid_address_copy (&adj_args->reid, &m->eid);
3882 adj_args->is_add = 1;
3884 if (vnet_lisp_add_del_adjacency (adj_args))
3885 clib_warning ("failed to add adjacency!");
3888 if ((u32) ~ 0 != m->ttl)
3889 mapping_start_expiration_timer (lcm, dst_map_index,
3890 (m->ttl == 0) ? 0 : MAPPING_TIMEOUT);
3893 /* remove pending map request entry */
3896 clib_fifo_foreach (noncep, pmr->nonces, ({
3897 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3901 clib_fifo_free (pmr->nonces);
3902 pool_put (lcm->pending_map_requests_pool, pmr);
3910 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3911 lisp_key_type_t key_id, u8 * key)
3913 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3917 vnet_crypto_op_t _op, *op = &_op;
3918 vnet_crypto_key_index_t ki;
3919 u8 out[EVP_MAX_MD_SIZE] = { 0, };
3921 auth_data_len = auth_data_len_by_key_id (key_id);
3922 if ((u16) ~ 0 == auth_data_len)
3924 clib_warning ("invalid length for key_id %d!", key_id);
3928 /* save auth data */
3929 vec_validate (auth_data, auth_data_len - 1);
3930 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3932 /* clear auth data */
3933 clib_memset (MNOTIFY_DATA (h), 0, auth_data_len);
3935 vnet_crypto_op_init (op, lisp_key_type_to_crypto_op (key_id));
3942 ki = vnet_crypto_key_add (lcm->vlib_main,
3943 lisp_key_type_to_crypto_alg (key_id), key,
3948 vnet_crypto_process_ops (lcm->vlib_main, op, 1);
3949 vnet_crypto_key_del (lcm->vlib_main, ki);
3951 result = memcmp (out, auth_data, auth_data_len);
3953 vec_free (auth_data);
3959 process_map_notify (map_records_arg_t * a)
3961 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3964 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3967 clib_warning ("No pending map-register entry with nonce %lu!",
3973 pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]);
3974 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3976 /* reset map-notify counter */
3977 lcm->expired_map_registers = 0;
3981 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3985 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3988 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3992 return pool_elt_at_index (lcm->mapping_pool, mi);
3996 * When map-notify is received it is necessary that all EIDs in the record
3997 * list share common key. The key is then used to verify authentication
3998 * data in map-notify message.
4001 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4002 u32 key_id, u8 ** key_out)
4004 u32 i, len = vec_len (maps);
4007 /* get key of the first mapping */
4008 m = get_mapping (lcm, &maps[0].eid);
4012 key_out[0] = m->key;
4014 for (i = 1; i < len; i++)
4016 m = get_mapping (lcm, &maps[i].eid);
4020 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4022 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4030 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4032 locator_t *locators = 0;
4038 clib_memset (&m, 0, sizeof (m));
4040 /* parse record eid */
4041 for (i = 0; i < count; i++)
4044 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4047 clib_warning ("Failed to parse mapping record!");
4048 vec_foreach (loc, locators) locator_free (loc);
4049 vec_free (locators);
4053 m.locators = locators;
4054 gid_address_copy (&m.eid, &deid);
4055 vec_add1 (a->mappings, m);
4061 static map_records_arg_t *
4062 map_record_args_get ()
4064 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4065 map_records_arg_t *rec;
4069 pool_foreach (rec, lcm->map_records_args_pool[vlib_get_thread_index()], ({
4071 map_records_arg_free (rec);
4075 pool_get (lcm->map_records_args_pool[vlib_get_thread_index ()], rec);
4079 static map_records_arg_t *
4080 parse_map_notify (vlib_buffer_t * b)
4083 map_notify_hdr_t *mnotif_hdr;
4084 lisp_key_type_t key_id;
4085 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4088 u16 auth_data_len = 0;
4090 map_records_arg_t *a;
4092 a = map_record_args_get ();
4093 clib_memset (a, 0, sizeof (*a));
4094 mnotif_hdr = vlib_buffer_get_current (b);
4095 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4096 clib_memset (&deid, 0, sizeof (deid));
4098 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4099 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4100 auth_data_len = auth_data_len_by_key_id (key_id);
4102 /* advance buffer by authentication data */
4103 vlib_buffer_pull (b, auth_data_len);
4105 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4106 rc = parse_map_records (b, a, record_count);
4109 map_records_arg_free (a);
4113 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4116 map_records_arg_free (a);
4120 /* verify authentication data */
4121 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4122 - (u8 *) mnotif_hdr, key_id, key))
4124 clib_warning ("Map-notify auth data verification failed for nonce "
4125 "0x%lx!", a->nonce);
4126 map_records_arg_free (a);
4132 static vlib_buffer_t *
4133 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4134 ip_address_t * dst, u64 nonce, u8 probe_bit,
4135 mapping_t * records, u16 dst_port, u32 * bi_res)
4139 vlib_main_t *vm = lcm->vlib_main;
4141 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4143 clib_warning ("Can't allocate buffer for Map-Register!");
4147 b = vlib_get_buffer (vm, bi);
4149 /* leave some space for the encap headers */
4150 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4152 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4154 /* push outer ip header */
4155 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst, 1);
4162 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4163 u8 probe_bit, u64 nonce, u16 dst_port,
4164 ip_address_t * probed_loc)
4170 u32 next_index, *to_next;
4171 mapping_t *records = 0, *m;
4173 m = pool_elt_at_index (lcm->mapping_pool, mi);
4177 vec_add1 (records, m[0]);
4178 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4179 clib_memset (&src, 0, sizeof (src));
4181 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4183 clib_warning ("can't find interface address for %U", format_ip_address,
4188 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4192 free_map_register_records (records);
4194 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4195 next_index = (ip_addr_version (&lcm->active_map_resolver) == AF_IP4) ?
4196 ip4_lookup_node.index : ip6_lookup_node.index;
4198 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4200 /* Enqueue the packet */
4201 to_next = vlib_frame_vector_args (f);
4204 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4209 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
4211 const i32 start = vnet_buffer (b)->l3_hdr_offset;
4212 if (start < 0 && start < -sizeof (b->pre_data))
4218 *ip_hdr = b->data + start;
4219 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
4224 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
4225 lisp_cp_main_t * lcm, vlib_buffer_t * b)
4228 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4230 map_request_hdr_t *mreq_hdr;
4231 gid_address_t src, dst;
4233 u32 i, len = 0, rloc_probe_recv = 0;
4234 gid_address_t *itr_rlocs = 0;
4236 mreq_hdr = vlib_buffer_get_current (b);
4237 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4240 ("Only SMR Map-Requests and RLOC probe supported for now!");
4244 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4245 nonce = MREQ_NONCE (mreq_hdr);
4248 len = lisp_msg_parse_addr (b, &src);
4252 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4253 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4257 /* parse eid records and send SMR-invoked map-requests */
4258 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4260 clib_memset (&dst, 0, sizeof (dst));
4261 len = lisp_msg_parse_eid_rec (b, &dst);
4264 clib_warning ("Can't parse map-request EID-record");
4268 if (MREQ_SMR (mreq_hdr))
4270 /* send SMR-invoked map-requests */
4271 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4273 else if (MREQ_RLOC_PROBE (mreq_hdr))
4275 find_ip_header (b, &ip_hdr);
4278 clib_warning ("Cannot find the IP header!");
4282 clib_memset (&m, 0, sizeof (m));
4283 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4285 // TODO: select best locator; for now use the first one
4286 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4288 /* get src/dst IP addresses */
4289 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4291 // TODO get source port from buffer
4292 u16 src_port = LISP_CONTROL_PORT;
4294 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4295 src_port, &probed_loc);
4300 vlib_node_increment_counter (vm, node->node_index,
4301 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
4303 vec_free (itr_rlocs);
4307 parse_map_reply (vlib_buffer_t * b)
4314 map_reply_hdr_t *mrep_hdr;
4315 map_records_arg_t *a;
4317 a = map_record_args_get ();
4318 clib_memset (a, 0, sizeof (*a));
4320 locator_t *locators;
4322 mrep_hdr = vlib_buffer_get_current (b);
4323 a->nonce = MREP_NONCE (mrep_hdr);
4324 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
4325 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
4327 map_records_arg_free (a);
4330 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4332 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4334 clib_memset (&m, 0, sizeof (m));
4336 h = vlib_buffer_get_current (b);
4338 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4339 m.action = MAP_REC_ACTION (h);
4340 m.authoritative = MAP_REC_AUTH (h);
4342 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4345 clib_warning ("Failed to parse mapping record!");
4346 map_records_arg_free (a);
4350 m.locators = locators;
4351 gid_address_copy (&m.eid, &deid);
4352 vec_add1 (a->mappings, m);
4358 queue_map_reply_for_processing (map_records_arg_t * a)
4360 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
4364 queue_map_notify_for_processing (map_records_arg_t * a)
4366 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4370 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4371 vlib_frame_t * from_frame)
4373 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
4374 map_notifies_recv = 0;
4375 lisp_msg_type_e type;
4376 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4377 map_records_arg_t *a;
4379 from = vlib_frame_vector_args (from_frame);
4380 n_left_from = from_frame->n_vectors;
4383 while (n_left_from > 0)
4385 u32 n_left_to_next_drop;
4387 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4388 to_next_drop, n_left_to_next_drop);
4389 while (n_left_from > 0 && n_left_to_next_drop > 0)
4397 to_next_drop[0] = bi0;
4399 n_left_to_next_drop -= 1;
4401 b0 = vlib_get_buffer (vm, bi0);
4403 type = lisp_msg_type (vlib_buffer_get_current (b0));
4406 case LISP_MAP_REPLY:
4407 a = parse_map_reply (b0);
4410 if (a->is_rloc_probe)
4411 rloc_probe_rep_recv++;
4412 queue_map_reply_for_processing (a);
4415 case LISP_MAP_REQUEST:
4416 process_map_request (vm, node, lcm, b0);
4418 case LISP_MAP_NOTIFY:
4419 a = parse_map_notify (b0);
4422 map_notifies_recv++;
4423 queue_map_notify_for_processing (a);
4427 clib_warning ("Unsupported LISP message type %d", type);
4431 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4433 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4439 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4440 n_left_to_next_drop);
4442 vlib_node_increment_counter (vm, node->node_index,
4443 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
4444 rloc_probe_rep_recv);
4445 vlib_node_increment_counter (vm, node->node_index,
4446 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
4448 return from_frame->n_vectors;
4452 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4453 .function = lisp_cp_input,
4454 .name = "lisp-cp-input",
4455 .vector_size = sizeof (u32),
4456 .format_trace = format_lisp_cp_input_trace,
4457 .type = VLIB_NODE_TYPE_INTERNAL,
4459 .n_errors = LISP_CP_INPUT_N_ERROR,
4460 .error_strings = lisp_cp_input_error_strings,
4462 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4465 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4471 lisp_cp_init (vlib_main_t * vm)
4473 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4474 clib_error_t *error = 0;
4475 vlib_thread_main_t *vtm = vlib_get_thread_main ();
4478 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4481 lcm->im4 = &ip4_main;
4482 lcm->im6 = &ip6_main;
4483 lcm->vlib_main = vm;
4484 lcm->vnet_main = vnet_get_main ();
4485 lcm->mreq_itr_rlocs = ~0;
4487 lcm->pitr_map_index = ~0;
4488 lcm->petr_map_index = ~0;
4489 clib_memset (&lcm->active_map_resolver, 0,
4490 sizeof (lcm->active_map_resolver));
4491 clib_memset (&lcm->active_map_server, 0, sizeof (lcm->active_map_server));
4493 gid_dictionary_init (&lcm->mapping_index_by_gid);
4494 lcm->do_map_resolver_election = 1;
4495 lcm->do_map_server_election = 1;
4496 lcm->map_request_mode = MR_MODE_DST_ONLY;
4498 num_threads = 1 /* main thread */ + vtm->n_threads;
4499 vec_validate (lcm->map_records_args_pool, num_threads - 1);
4501 /* default vrf mapped to vni 0 */
4502 hash_set (lcm->table_id_by_vni, 0, 0);
4503 hash_set (lcm->vni_by_table_id, 0, 0);
4505 TW (tw_timer_wheel_init) (&lcm->wheel, 0 /* no callback */ ,
4506 1e-3 /* timer period 1ms */ ,
4507 ~0 /* max expirations per call */ );
4508 lcm->nsh_map_index = ~0;
4509 lcm->map_register_ttl = MAP_REGISTER_DEFAULT_TTL;
4510 lcm->max_expired_map_registers = MAX_EXPIRED_MAP_REGISTERS_DEFAULT;
4511 lcm->expired_map_registers = 0;
4512 lcm->transport_protocol = LISP_TRANSPORT_PROTOCOL_UDP;
4513 lcm->flags |= LISP_FLAG_XTR_MODE;
4518 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
4519 lisp_api_stats_t * stat, lisp_stats_key_t * key,
4523 vlib_combined_counter_main_t *cm = &lgm->counters;
4524 lisp_gpe_fwd_entry_key_t fwd_key;
4525 const lisp_gpe_tunnel_t *lgt;
4528 clib_memset (stat, 0, sizeof (*stat));
4529 clib_memset (&fwd_key, 0, sizeof (fwd_key));
4531 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
4534 gid_to_dp_address (&fe->reid, &stat->deid);
4535 gid_to_dp_address (&fe->leid, &stat->seid);
4536 stat->vni = gid_address_vni (&fe->reid);
4538 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
4539 stat->loc_rloc = lgt->key->lcl;
4540 stat->rmt_rloc = lgt->key->rmt;
4542 vlib_get_combined_counter (cm, stats_index, &v);
4548 vnet_lisp_get_stats (void)
4550 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
4551 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4552 lisp_api_stats_t *stats = 0, stat;
4553 lisp_stats_key_t *key;
4557 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
4559 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
4560 vec_add1 (stats, stat);
4568 send_map_request_thread_fn (void *arg)
4570 map_request_args_t *a = arg;
4571 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4574 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4576 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4582 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4583 u8 smr_invoked, u8 is_resend)
4585 map_request_args_t a;
4587 a.is_resend = is_resend;
4588 gid_address_copy (&a.seid, seid);
4589 gid_address_copy (&a.deid, deid);
4590 a.smr_invoked = smr_invoked;
4592 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4593 (u8 *) & a, sizeof (a));
4598 * Take an action with a pending map request depending on expiration time
4599 * and re-try counters.
4602 update_pending_request (pending_map_request_t * r, f64 dt)
4604 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4607 if (r->time_to_expire - dt < 0)
4608 /* it's time to decide what to do with this pending request */
4610 if (r->retries_num >= NUMBER_OF_RETRIES)
4611 /* too many retries -> assume current map resolver is not available */
4613 mr = get_map_resolver (&lcm->active_map_resolver);
4616 clib_warning ("Map resolver %U not found - probably deleted "
4617 "by the user recently.", format_ip_address,
4618 &lcm->active_map_resolver);
4622 clib_warning ("map resolver %U is unreachable, ignoring",
4623 format_ip_address, &lcm->active_map_resolver);
4625 /* mark current map resolver unavailable so it won't be
4626 * selected next time */
4628 mr->last_update = vlib_time_now (lcm->vlib_main);
4631 reset_pending_mr_counters (r);
4632 elect_map_resolver (lcm);
4634 /* try to find a next eligible map resolver and re-send */
4635 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4641 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4644 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4648 r->time_to_expire -= dt;
4652 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4655 pending_map_request_t *pmr;
4656 u32 *to_be_removed = 0, *pmr_index;
4659 pool_foreach (pmr, lcm->pending_map_requests_pool,
4661 if (pmr->to_be_removed)
4663 clib_fifo_foreach (nonce, pmr->nonces, ({
4664 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4667 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4672 vec_foreach (pmr_index, to_be_removed)
4673 pool_put_index (lcm->pending_map_requests_pool, pmr_index[0]);
4675 vec_free (to_be_removed);
4679 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4681 static f64 time_left = RLOC_PROBING_INTERVAL;
4683 if (!lcm->is_enabled || !lcm->rloc_probing)
4689 time_left = RLOC_PROBING_INTERVAL;
4690 send_rloc_probes (lcm);
4695 update_pending_map_register (pending_map_register_t * r, f64 dt, u8 * del_all)
4697 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4701 r->time_to_expire -= dt;
4703 if (r->time_to_expire < 0)
4705 lcm->expired_map_registers++;
4707 if (lcm->expired_map_registers >= lcm->max_expired_map_registers)
4709 ms = get_map_server (&lcm->active_map_server);
4712 clib_warning ("Map server %U not found - probably deleted "
4713 "by the user recently.", format_ip_address,
4714 &lcm->active_map_server);
4718 clib_warning ("map server %U is unreachable, ignoring",
4719 format_ip_address, &lcm->active_map_server);
4721 /* mark current map server unavailable so it won't be
4722 * elected next time */
4724 ms->last_update = vlib_time_now (lcm->vlib_main);
4727 elect_map_server (lcm);
4729 /* indication for deleting all pending map registers */
4731 lcm->expired_map_registers = 0;
4736 /* delete pending map register */
4744 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4746 u32 *to_be_removed = 0, *pmr_index;
4747 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4748 static u64 mreg_sent_counter = 0;
4750 pending_map_register_t *pmr;
4753 if (!lcm->is_enabled || !lcm->map_registering)
4757 pool_foreach (pmr, lcm->pending_map_registers_pool,
4759 if (!update_pending_map_register (pmr, dt, &del_all))
4763 vec_add1 (to_be_removed, pmr - lcm->pending_map_registers_pool);
4770 /* delete all pending map register messages so they won't
4771 * trigger another map server election.. */
4772 pool_free (lcm->pending_map_registers_pool);
4773 hash_free (lcm->map_register_messages_by_nonce);
4775 /* ..and trigger registration against next map server (if any) */
4780 vec_foreach (pmr_index, to_be_removed)
4781 pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]);
4784 vec_free (to_be_removed);
4789 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4790 time_left = MAP_REGISTER_INTERVAL;
4793 mreg_sent_counter++;
4794 time_left = QUICK_MAP_REGISTER_INTERVAL;
4796 send_map_register (lcm, 1 /* want map notify */ );
4801 send_map_resolver_service (vlib_main_t * vm,
4802 vlib_node_runtime_t * rt, vlib_frame_t * f)
4806 pending_map_request_t *pmr;
4807 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4811 vlib_process_wait_for_event_or_clock (vm, period);
4813 /* currently no signals are expected - just wait for clock */
4814 (void) vlib_process_get_events (vm, 0);
4817 pool_foreach (pmr, lcm->pending_map_requests_pool,
4819 if (!pmr->to_be_removed)
4820 update_pending_request (pmr, period);
4824 remove_dead_pending_map_requests (lcm);
4826 update_map_register (lcm, period);
4827 update_rloc_probing (lcm, period);
4829 expired = TW (tw_timer_expire_timers_vec) (&lcm->wheel,
4830 vlib_time_now (vm), expired);
4831 if (vec_len (expired) > 0)
4834 vec_foreach (mi, expired)
4836 process_expired_mapping (lcm, mi[0]);
4838 _vec_len (expired) = 0;
4847 vnet_lisp_stats_enable_disable (u8 enable)
4849 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4851 if (vnet_lisp_enable_disable_status () == 0)
4852 return VNET_API_ERROR_LISP_DISABLED;
4855 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4857 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4863 vnet_lisp_stats_enable_disable_state (void)
4865 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4867 if (vnet_lisp_enable_disable_status () == 0)
4868 return VNET_API_ERROR_LISP_DISABLED;
4870 return lcm->flags & LISP_FLAG_STATS_ENABLED;
4874 vnet_lisp_create_retry_process (lisp_cp_main_t * lcm)
4876 if (lcm->retry_service_index)
4879 lcm->retry_service_index = vlib_process_create (vlib_get_main (),
4880 "lisp-retry-service",
4881 send_map_resolver_service,
4882 16 /* stack_bytes */ );
4886 vnet_lisp_set_transport_protocol (u8 protocol)
4888 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4890 if (protocol < LISP_TRANSPORT_PROTOCOL_UDP ||
4891 protocol > LISP_TRANSPORT_PROTOCOL_API)
4892 return VNET_API_ERROR_INVALID_ARGUMENT;
4894 lcm->transport_protocol = protocol;
4898 lisp_transport_protocol_t
4899 vnet_lisp_get_transport_protocol (void)
4901 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4902 return lcm->transport_protocol;
4906 vnet_lisp_enable_disable_xtr_mode (u8 is_enabled)
4908 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4909 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
4910 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
4911 u8 petr_mode = lcm->flags & LISP_FLAG_PETR_MODE;
4913 if (pitr_mode && is_enabled)
4914 return VNET_API_ERROR_INVALID_ARGUMENT;
4916 if (is_enabled && xtr_mode)
4918 if (!is_enabled && !xtr_mode)
4925 lisp_cp_register_dst_port (lcm->vlib_main);
4927 lisp_cp_enable_l2_l3_ifaces (lcm, 1 /* with_default_route */ );
4928 lcm->flags |= LISP_FLAG_XTR_MODE;
4934 lisp_cp_unregister_dst_port (lcm->vlib_main);
4936 lisp_cp_disable_l2_l3_ifaces (lcm);
4937 lcm->flags &= ~LISP_FLAG_XTR_MODE;
4943 vnet_lisp_enable_disable_pitr_mode (u8 is_enabled)
4945 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4946 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
4947 u8 pitr_mode = lcm->flags & LISP_FLAG_PITR_MODE;
4949 if (xtr_mode && is_enabled)
4950 return VNET_API_ERROR_INVALID_VALUE;
4952 if (is_enabled && pitr_mode)
4954 if (!is_enabled && !pitr_mode)
4959 /* create iface, no default route */
4960 lisp_cp_enable_l2_l3_ifaces (lcm, 0 /* with_default_route */ );
4961 lcm->flags |= LISP_FLAG_PITR_MODE;
4965 lisp_cp_disable_l2_l3_ifaces (lcm);
4966 lcm->flags &= ~LISP_FLAG_PITR_MODE;
4972 vnet_lisp_enable_disable_petr_mode (u8 is_enabled)
4974 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4975 u8 xtr_mode = lcm->flags & LISP_FLAG_XTR_MODE;
4976 u8 petr_mode = lcm->flags & LISP_FLAG_PETR_MODE;
4978 if (is_enabled && petr_mode)
4980 if (!is_enabled && !petr_mode)
4987 lisp_cp_register_dst_port (lcm->vlib_main);
4989 lcm->flags |= LISP_FLAG_PETR_MODE;
4995 lisp_cp_unregister_dst_port (lcm->vlib_main);
4997 lcm->flags &= ~LISP_FLAG_PETR_MODE;
5003 vnet_lisp_get_xtr_mode (void)
5005 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5006 return (lcm->flags & LISP_FLAG_XTR_MODE);
5010 vnet_lisp_get_pitr_mode (void)
5012 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5013 return (lcm->flags & LISP_FLAG_PITR_MODE);
5017 vnet_lisp_get_petr_mode (void)
5019 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5020 return (lcm->flags & LISP_FLAG_PETR_MODE);
5023 VLIB_INIT_FUNCTION (lisp_cp_init);
5026 * fd.io coding-style-patch-verification: ON
5029 * eval: (c-set-style "gnu")