+static void
+gre_nhrp_mk_key (const gre_tunnel_t * t,
+ const nhrp_entry_t * ne, gre_tunnel_key_t * key)
+{
+ const fib_prefix_t *nh;
+
+ nh = nhrp_entry_get_nh (ne);
+
+ /* construct the key using mode P2P so it can be found in the DP */
+ if (FIB_PROTOCOL_IP4 == nh->fp_proto)
+ gre_mk_key4 (t->tunnel_src.ip4,
+ nh->fp_addr.ip4,
+ nhrp_entry_get_fib_index (ne),
+ t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v4);
+ else
+ gre_mk_key6 (&t->tunnel_src.ip6,
+ &nh->fp_addr.ip6,
+ nhrp_entry_get_fib_index (ne),
+ t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v6);
+}
+
+/**
+ * An NHRP entry has been added
+ */
+static void
+gre_nhrp_entry_added (const nhrp_entry_t * ne)
+{
+ gre_main_t *gm = &gre_main;
+ const ip46_address_t *nh;
+ gre_tunnel_key_t key;
+ gre_tunnel_t *t;
+ u32 sw_if_index;
+ u32 t_idx;
+
+ sw_if_index = nhrp_entry_get_sw_if_index (ne);
+ if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
+ return;
+
+ t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
+
+ if (INDEX_INVALID == t_idx)
+ return;
+
+ /* entry has been added on an interface for which there is a GRE tunnel */
+ t = pool_elt_at_index (gm->tunnels, t_idx);
+
+ if (t->mode != TUNNEL_MODE_MP)
+ return;
+
+ /* the next-hop (underlay) of the NHRP entry will form part of the key for
+ * ingress lookup to match packets to this interface */
+ gre_nhrp_mk_key (t, ne, &key);
+ gre_tunnel_db_add (t, &key);
+
+ /* update the rewrites for each of the adjacencies for this peer (overlay)
+ * using the next-hop (underlay) */
+ mgre_walk_ctx_t ctx = {
+ .t = t,
+ .ne = ne
+ };
+ nh = nhrp_entry_get_peer (ne);
+ adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
+ (ip46_address_is_ip4 (nh) ?
+ FIB_PROTOCOL_IP4 :
+ FIB_PROTOCOL_IP6), nh, mgre_mk_complete_walk, &ctx);
+}
+
+static void
+gre_nhrp_entry_deleted (const nhrp_entry_t * ne)
+{
+ gre_main_t *gm = &gre_main;
+ const ip46_address_t *nh;
+ gre_tunnel_key_t key;
+ gre_tunnel_t *t;
+ u32 sw_if_index;
+ u32 t_idx;
+
+ sw_if_index = nhrp_entry_get_sw_if_index (ne);
+ if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
+ return;
+
+ t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
+
+ if (INDEX_INVALID == t_idx)
+ return;
+
+ t = pool_elt_at_index (gm->tunnels, t_idx);
+
+ /* remove the next-hop as an ingress lookup key */
+ gre_nhrp_mk_key (t, ne, &key);
+ gre_tunnel_db_remove (t, &key);
+
+ nh = nhrp_entry_get_peer (ne);
+
+ /* make all the adjacencies incomplete */
+ adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
+ (ip46_address_is_ip4 (nh) ?
+ FIB_PROTOCOL_IP4 :
+ FIB_PROTOCOL_IP6), nh, mgre_mk_incomplete_walk, t);
+}
+
+static walk_rc_t
+gre_tunnel_delete_nhrp_walk (index_t nei, void *ctx)
+{
+ gre_tunnel_t *t = ctx;
+ gre_tunnel_key_t key;
+
+ gre_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
+ gre_tunnel_db_remove (t, &key);
+
+ return (WALK_CONTINUE);
+}
+
+static walk_rc_t
+gre_tunnel_add_nhrp_walk (index_t nei, void *ctx)
+{
+ gre_tunnel_t *t = ctx;
+ gre_tunnel_key_t key;
+
+ gre_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
+ gre_tunnel_db_add (t, &key);
+
+ return (WALK_CONTINUE);
+}
+