-static int
-lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
-{
- u8 *rw = 0;
- lisp_gpe_header_t * lisp0;
- int len;
-
- if (ip_addr_version(&t->src) == IP4)
- {
- ip4_header_t * ip0;
- ip4_udp_lisp_gpe_header_t * h0;
- len = sizeof(*h0);
-
- vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
-
- h0 = (ip4_udp_lisp_gpe_header_t *) rw;
-
- /* Fixed portion of the (outer) ip4 header */
- ip0 = &h0->ip4;
- ip0->ip_version_and_header_length = 0x45;
- ip0->ttl = 254;
- ip0->protocol = IP_PROTOCOL_UDP;
-
- /* we fix up the ip4 header length and checksum after-the-fact */
- ip_address_copy_addr(&ip0->src_address, &t->src);
- ip_address_copy_addr(&ip0->dst_address, &t->dst);
- ip0->checksum = ip4_header_checksum (ip0);
-
- /* UDP header, randomize src port on something, maybe? */
- h0->udp.src_port = clib_host_to_net_u16 (4341);
- h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
-
- /* LISP-gpe header */
- lisp0 = &h0->lisp;
- }
- else
- {
- ip6_header_t * ip0;
- ip6_udp_lisp_gpe_header_t * h0;
- len = sizeof(*h0);
-
- vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
-
- h0 = (ip6_udp_lisp_gpe_header_t *) rw;
-
- /* Fixed portion of the (outer) ip6 header */
- ip0 = &h0->ip6;
- ip0->ip_version_traffic_class_and_flow_label =
- clib_host_to_net_u32 (0x6 << 28);
- ip0->hop_limit = 254;
- ip0->protocol = IP_PROTOCOL_UDP;
-
- /* we fix up the ip6 header length after-the-fact */
- ip_address_copy_addr(&ip0->src_address, &t->src);
- ip_address_copy_addr(&ip0->dst_address, &t->dst);
-
- /* UDP header, randomize src port on something, maybe? */
- h0->udp.src_port = clib_host_to_net_u16 (4341);
- h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
-
- /* LISP-gpe header */
- lisp0 = &h0->lisp;
- }
-
- lisp0->flags = t->flags;
- lisp0->ver_res = t->ver_res;
- lisp0->res = t->res;
- lisp0->next_protocol = t->next_protocol;
- lisp0->iid = clib_host_to_net_u32 (t->vni);
-
- t->rewrite = rw;
- return 0;
-}
-
-#define foreach_copy_field \
-_(encap_fib_index) \
-_(decap_fib_index) \
-_(decap_next_index) \
-_(vni) \
-_(action)
-
-static int
-add_del_ip_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u8 is_l2,
- u32 * tun_index_res)
-{
- lisp_gpe_main_t * lgm = &lisp_gpe_main;
- lisp_gpe_tunnel_t *t = 0;
- lisp_gpe_tunnel_key_t key;
- uword * p;
- int rv;
-
- /* prepare tunnel key */
- memset(&key, 0, sizeof(key));
-
- /* fill in the key's remote eid */
- if (!is_l2)
- ip_prefix_copy (&key.rmt_ippref, &gid_address_ippref(&a->rmt_eid));
- else
- mac_copy (&key.rmt_mac, &gid_address_mac(&a->rmt_eid));
-
-
- ip_address_copy(&key.rmt_loc, &a->rmt_loc);
- key.vni = clib_host_to_net_u32 (a->vni);
-
- p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
-
- if (a->is_add)
- {
- /* adding a tunnel: tunnel must not already exist */
- if (p)
- return VNET_API_ERROR_INVALID_VALUE;
-
- if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
- return VNET_API_ERROR_INVALID_DECAP_NEXT;
-
- pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
- memset (t, 0, sizeof (*t));
-
- /* copy from arg structure */
-#define _(x) t->x = a->x;
- foreach_copy_field;
-#undef _
-
- ip_address_copy(&t->src, &a->lcl_loc);
- ip_address_copy(&t->dst, &a->rmt_loc);
-
- /* if vni is non-default */
- if (a->vni)
- t->flags = LISP_GPE_FLAGS_I;
-
- /* work in lisp-gpe not legacy mode */
- t->flags |= LISP_GPE_FLAGS_P;
-
- /* next proto */
- if (!is_l2)
- t->next_protocol = ip_prefix_version(&key.rmt_ippref) == IP4 ?
- LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
- else
- t->next_protocol = LISP_GPE_NEXT_PROTO_ETHERNET;
-
- /* compute rewrite */
- rv = lisp_gpe_rewrite (t);
-
- if (rv)
- {
- pool_put(lgm->tunnels, t);
- return rv;
- }
-
- mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
-
- /* return tunnel index */
- if (tun_index_res)
- tun_index_res[0] = t - lgm->tunnels;
- }
- else
- {
- /* deleting a tunnel: tunnel must exist */
- if (!p)
- {
- clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
- &a->rmt_eid);
- return VNET_API_ERROR_NO_SUCH_ENTRY;
- }
-
- t = pool_elt_at_index(lgm->tunnels, p[0]);
-
- mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
-
- vec_free(t->rewrite);
- pool_put(lgm->tunnels, t);
- }
-
- return 0;
-}
-
-static int
-add_del_negative_ip_fwd_entry (lisp_gpe_main_t * lgm,
- vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
-{
- ip_adjacency_t adj;
- ip_prefix_t * dpref = &gid_address_ippref(&a->rmt_eid);
- ip_prefix_t * spref = &gid_address_ippref(&a->lcl_eid);
-
- /* setup adjacency for eid */
- memset (&adj, 0, sizeof(adj));
- adj.n_adj = 1;
-
- /* fill in 'legal' data to avoid issues */
- adj.lookup_next_index = (ip_prefix_version(dpref) == IP4) ?
- lgm->ip4_lookup_next_lgpe_ip4_lookup :
- lgm->ip6_lookup_next_lgpe_ip6_lookup;
-
- adj.rewrite_header.sw_if_index = ~0;
- adj.rewrite_header.next_index = ~0;
-
- switch (a->action)
- {
- case LISP_NO_ACTION:
- /* TODO update timers? */
- case LISP_FORWARD_NATIVE:
- /* TODO check if route/next-hop for eid exists in fib and add
- * more specific for the eid with the next-hop found */
- case LISP_SEND_MAP_REQUEST:
- /* insert tunnel that always sends map-request */
- adj.explicit_fib_index = (ip_prefix_version(dpref) == IP4) ?
- LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP:
- LGPE_IP6_LOOKUP_NEXT_LISP_CP_LOOKUP;
- /* add/delete route for prefix */
- return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
- a->is_add);
- case LISP_DROP:
- /* for drop fwd entries, just add route, no need to add encap tunnel */
- adj.explicit_fib_index = (ip_prefix_version(dpref) == IP4 ?
- LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP);
-
- /* add/delete route for prefix */
- return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
- a->is_add);
- default:
- return -1;
- }
-}
-
-static int
-add_del_ip_fwd_entry (lisp_gpe_main_t * lgm,
- vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
-{
- ip_adjacency_t adj, * adjp;
- u32 adj_index, rv, tun_index = ~0;
- ip_prefix_t * dpref, * spref;
- uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
- u8 ip_ver;
-
- /* treat negative fwd entries separately */
- if (a->is_negative)
- return add_del_negative_ip_fwd_entry (lgm, a);
-
- /* add/del tunnel to tunnels pool and prepares rewrite */
- rv = add_del_ip_tunnel (a, 0 /* is_l2 */, &tun_index);
- if (rv)
- return rv;
-
- dpref = &gid_address_ippref(&a->rmt_eid);
- spref = &gid_address_ippref(&a->lcl_eid);
- ip_ver = ip_prefix_version(dpref);
-
- /* setup adjacency for eid */
- memset (&adj, 0, sizeof(adj));
- adj.n_adj = 1;
-
- /* fill in lookup_next_index with a 'legal' value to avoid problems */
- adj.lookup_next_index = (ip_ver == IP4) ?
- lgm->ip4_lookup_next_lgpe_ip4_lookup :
- lgm->ip6_lookup_next_lgpe_ip6_lookup;
-
- if (a->is_add)
- {
- /* send packets that hit this adj to lisp-gpe interface output node in
- * requested vrf. */
- lnip = (ip_ver == IP4) ?
- lgm->lgpe_ip4_lookup_next_index_by_table_id :
- lgm->lgpe_ip6_lookup_next_index_by_table_id;
- lookup_next_index = hash_get(lnip, a->table_id);
- lgpe_sw_if_index = hash_get(lgm->l3_ifaces.sw_if_index_by_vni, a->vni);
-
- /* the assumption is that the interface must've been created before
- * programming the dp */
- ASSERT(lookup_next_index != 0);
- ASSERT(lgpe_sw_if_index != 0);
-
- /* hijack explicit fib index to store lisp interface node index and
- * if_address_index for the tunnel index */
- adj.explicit_fib_index = lookup_next_index[0];
- adj.if_address_index = tun_index;
- adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
- }
-
- /* add/delete route for prefix */
- rv = ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
- a->is_add);
-
- /* check that everything worked */
- if (CLIB_DEBUG && a->is_add)
- {
- adj_index = ip_sd_fib_get_route (lgm, dpref, spref, a->table_id);
- ASSERT(adj_index != 0);
-
- adjp = ip_get_adjacency ((ip_ver == IP4) ? lgm->lm4 : lgm->lm6,
- adj_index);
-
- ASSERT(adjp != 0);
- ASSERT(adjp->if_address_index == tun_index);
- }
-
- return rv;
-}
-
-static void
-make_mac_fib_key (BVT(clib_bihash_kv) *kv, u16 bd_index, u8 src_mac[6],
- u8 dst_mac[6])
-{
- kv->key[0] = (((u64) bd_index) << 48) | (((u64 *)dst_mac)[0] & MAC_BIT_MASK);
- kv->key[1] = ((u64 *)src_mac)[0] & MAC_BIT_MASK;
- kv->key[2] = 0;
-}
-
-u32
-lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
- u8 dst_mac[6])
-{
- int rv;
- BVT(clib_bihash_kv) kv, value;
-
- make_mac_fib_key(&kv, bd_index, src_mac, dst_mac);
- rv = BV(clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
-
- /* no match, try with src 0, catch all for dst */
- if (rv != 0)
- {
- kv.key[1] = 0;
- rv = BV(clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
- if (rv == 0)
- return value.value;
- }
-
- return ~0;
-}
-
-u32
-lisp_l2_fib_add_del_entry (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
- u8 dst_mac[6], u32 val, u8 is_add)
-{
- BVT(clib_bihash_kv) kv, value;
- u32 old_val = ~0;
-
- make_mac_fib_key(&kv, bd_index, src_mac, dst_mac);
-
- if (BV(clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
- old_val = value.value;
-
- if (!is_add)
- BV(clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */);
- else
- {
- kv.value = val;
- BV(clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */);
- }
- return old_val;
-}
-
-static void
-l2_fib_init (lisp_gpe_main_t * lgm)
-{
- BV(clib_bihash_init) (&lgm->l2_fib, "l2 fib",
- 1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
- L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
-}
-
-static int
-add_del_l2_fwd_entry (lisp_gpe_main_t * lgm,
- vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
-{
- int rv;
- u32 tun_index;
- bd_main_t * bdm = &bd_main;
- uword * bd_indexp;
-
- /* create tunnel */
- rv = add_del_ip_tunnel (a, 1 /* is_l2 */, &tun_index);
- if (rv)
- return rv;
-
- bd_indexp = hash_get(bdm->bd_index_by_bd_id, a->bd_id);
- if (!bd_indexp)
- {
- clib_warning("bridge domain %d doesn't exist", a->bd_id);
- return -1;
- }
-
- /* add entry to l2 lisp fib */
- lisp_l2_fib_add_del_entry (lgm, bd_indexp[0], gid_address_mac(&a->lcl_eid),
- gid_address_mac(&a->rmt_eid), tun_index,
- a->is_add);
- return 0;
-}
-
-
-int
-vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
- u32 * hw_if_indexp)
-{
- lisp_gpe_main_t * lgm = &lisp_gpe_main;
- u8 type;
-
- if (vnet_lisp_gpe_enable_disable_status () == 0)
- {
- clib_warning ("LISP is disabled!");
- return VNET_API_ERROR_LISP_DISABLED;
- }
-
- type = gid_address_type(&a->rmt_eid);
- switch (type)
- {
- case GID_ADDR_IP_PREFIX:
- return add_del_ip_fwd_entry (lgm, a);
- case GID_ADDR_MAC:
- return add_del_l2_fwd_entry (lgm, a);
- default:
- clib_warning("Forwarding entries for type %d not supported!", type);
- return -1;
- }
-}