/* * Copyright (c) 2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include typedef struct ip6_link_delegate_t_ { u32 ild_sw_if_index; ip6_link_delegate_id_t ild_type; index_t ild_index; } ip6_link_delegate_t; const static ip6_link_delegate_t ip6_link_delegate_uninit = { .ild_sw_if_index = ~0, }; typedef struct ip6_link_t_ { /** interface ip6 is enabled on */ u32 il_sw_if_index; /** link-local address - if unset that IP6 is disabled*/ ip6_address_t il_ll_addr; /** list of delegates */ ip6_link_delegate_t *il_delegates; /** multicast adjacency for this link */ adj_index_t il_mcast_adj; /** number of references to IP6 enabled on this link */ u32 il_locks; } ip6_link_t; #define FOREACH_IP6_LINK_DELEGATE(_ild, _il, body) \ { \ if (NULL != _il) { \ vec_foreach (_ild, _il->il_delegates) { \ if (ip6_link_delegate_is_init(_ild)) \ body; \ } \ } \ } #define FOREACH_IP6_LINK_DELEGATE_ID(_id) \ for (_id = 0; _id < il_delegate_id; _id++) /** last used delegate ID */ static ip6_link_delegate_id_t il_delegate_id; /** VFT registered per-delegate type */ static ip6_link_delegate_vft_t *il_delegate_vfts; /** Per interface configs */ static ip6_link_t *ip6_links; /** Randomizer */ static u64 il_randomizer; /** Logging */ static vlib_log_class_t ip6_link_logger; #define IP6_LINK_DBG(...) \ vlib_log_debug (ip6_link_logger, __VA_ARGS__); #define IP6_LINK_INFO(...) \ vlib_log_notice (ip6_link_logger, __VA_ARGS__); static bool ip6_link_delegate_is_init (const ip6_link_delegate_t * ild) { return (~0 != ild->ild_sw_if_index); } static bool ip6_link_is_enabled_i (const ip6_link_t * il) { return (!ip6_address_is_zero (&il->il_ll_addr)); } static void ip6_link_local_address_from_mac (ip6_address_t * ip, const u8 * mac) { ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL); /* Invert the "u" bit */ ip->as_u8[8] = mac[0] ^ (1 << 1); ip->as_u8[9] = mac[1]; ip->as_u8[10] = mac[2]; ip->as_u8[11] = 0xFF; ip->as_u8[12] = 0xFE; ip->as_u8[13] = mac[3]; ip->as_u8[14] = mac[4]; ip->as_u8[15] = mac[5]; } static void ip6_mac_address_from_link_local (u8 * mac, const ip6_address_t * ip) { /* Invert the previously inverted "u" bit */ mac[0] = ip->as_u8[8] ^ (1 << 1); mac[1] = ip->as_u8[9]; mac[2] = ip->as_u8[10]; mac[3] = ip->as_u8[13]; mac[4] = ip->as_u8[14]; mac[5] = ip->as_u8[15]; } static ip6_link_t * ip6_link_get (u32 sw_if_index) { ip6_link_t *il; if (sw_if_index >= vec_len (ip6_links)) return (NULL); il = &ip6_links[sw_if_index]; if (!ip6_link_is_enabled_i (il)) return (NULL); return (il); } bool ip6_link_is_enabled (u32 sw_if_index) { return (NULL != ip6_link_get (sw_if_index)); } int ip6_link_enable (u32 sw_if_index, const ip6_address_t * link_local_addr) { ip6_link_t *il; int rv; il = ip6_link_get (sw_if_index); if (NULL == il) { const vnet_sw_interface_t *sw, *sw_sup; const ethernet_interface_t *eth; vnet_main_t *vnm; vnm = vnet_get_main (); IP6_LINK_INFO ("enable: %U", format_vnet_sw_if_index_name, vnm, sw_if_index); sw_sup = vnet_get_sup_sw_interface (vnm, sw_if_index); if (sw_sup->type != VNET_SW_INTERFACE_TYPE_HARDWARE) { rv = VNET_API_ERROR_UNSUPPORTED; goto out; } eth = ethernet_get_interface (ðernet_main, sw_sup->hw_if_index); if (NULL == eth) { rv = VNET_API_ERROR_UNSUPPORTED; goto out; } vec_validate (ip6_links, sw_if_index); il = &ip6_links[sw_if_index]; il->il_locks = 0; il->il_sw_if_index = sw_if_index; sw = vnet_get_sup_sw_interface (vnm, sw_if_index); if (NULL != link_local_addr) ip6_address_copy (&il->il_ll_addr, link_local_addr); else if (sw->type == VNET_SW_INTERFACE_TYPE_SUB || sw->type == VNET_SW_INTERFACE_TYPE_PIPE || sw->type == VNET_SW_INTERFACE_TYPE_P2P) { il->il_ll_addr.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL); /* make up an interface id */ il->il_ll_addr.as_u64[1] = random_u64 (&il_randomizer); /* clear u bit */ il->il_ll_addr.as_u8[8] &= 0xfd; } else { ip6_link_local_address_from_mac (&il->il_ll_addr, eth->address.mac.bytes); } { ip6_ll_prefix_t ilp = { .ilp_addr = il->il_ll_addr, .ilp_sw_if_index = sw_if_index, }; ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL); } /* essentially "enables" ipv6 on this interface */ ip6_mfib_interface_enable_disable (sw_if_index, 1); ip6_sw_interface_enable_disable (sw_if_index, 1); il->il_mcast_adj = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6, sw_if_index); /* inform all register clients */ ip6_link_delegate_id_t id; FOREACH_IP6_LINK_DELEGATE_ID (id) { if (NULL != il_delegate_vfts[id].ildv_enable) il_delegate_vfts[id].ildv_enable (il->il_sw_if_index); } rv = 0; } else { rv = VNET_API_ERROR_VALUE_EXIST; } il->il_locks++; out: return (rv); } static void ip6_link_delegate_flush (ip6_link_t * il) { ip6_link_delegate_t *ild; /* *INDENT-OFF* */ FOREACH_IP6_LINK_DELEGATE (ild, il, ({ il_delegate_vfts[ild->ild_type].ildv_disable(ild->ild_index); })); /* *INDENT-ON* */ vec_free (il->il_delegates); il->il_delegates = NULL; } static void ip6_link_last_lock_gone (ip6_link_t * il) { ip6_ll_prefix_t ilp = { .ilp_addr = il->il_ll_addr, .ilp_sw_if_index = il->il_sw_if_index, }; IP6_LINK_INFO ("last-lock: %U", format_vnet_sw_if_index_name, vnet_get_main (), il->il_sw_if_index); ip6_link_delegate_flush (il); ip6_ll_table_entry_delete (&ilp); ip6_mfib_interface_enable_disable (il->il_sw_if_index, 0); ip6_sw_interface_enable_disable (il->il_sw_if_index, 0); ip6_address_set_zero (&il->il_ll_addr); adj_unlock (il->il_mcast_adj); il->il_mcast_adj = ADJ_INDEX_INVALID; } static void ip6_link_unlock (ip6_link_t * il) { if (NULL == il) return; il->il_locks--; if (0 == il->il_locks) ip6_link_last_lock_gone (il); } int ip6_link_disable (u32 sw_if_index) { ip6_link_t *il; il = ip6_link_get (sw_if_index); if (NULL == il) return (VNET_API_ERROR_IP6_NOT_ENABLED); IP6_LINK_INFO ("disable: %U", format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index); ip6_link_unlock (il); return (0); } const ip6_address_t * ip6_get_link_local_address (u32 sw_if_index) { const ip6_link_t *il; vec_validate (ip6_links, sw_if_index); il = &ip6_links[sw_if_index]; return (&il->il_ll_addr); } adj_index_t ip6_link_get_mcast_adj (u32 sw_if_index) { const ip6_link_t *il; il = &ip6_links[sw_if_index]; return (il->il_mcast_adj); } int ip6_src_address_for_packet (u32 sw_if_index, const ip6_address_t * dst, ip6_address_t * src) { ip_lookup_main_t *lm; lm = &ip6_main.lookup_main; if (ip6_address_is_link_local_unicast (dst)) { ip6_address_copy (src, ip6_get_link_local_address (sw_if_index)); return (!0); } else { u32 if_add_index = lm->if_address_pool_index_by_sw_if_index[sw_if_index]; if (PREDICT_TRUE (if_add_index != ~0)) { ip_interface_address_t *if_add = pool_elt_at_index (lm->if_address_pool, if_add_index); ip6_address_t *if_ip = ip_interface_address_get_address (lm, if_add); *src = *if_ip; return (!0); } } src->as_u64[0] = 0; src->as_u64[1] = 0; return (0); } int ip6_link_set_local_address (u32 sw_if_index, const ip6_address_t * address) { ip6_link_delegate_t *ild; ip6_link_t *il; il = ip6_link_get (sw_if_index); if (NULL == il) return ip6_link_enable (sw_if_index, address); ip6_ll_prefix_t ilp = { .ilp_addr = il->il_ll_addr, .ilp_sw_if_index = sw_if_index, }; IP6_LINK_INFO ("set-ll: %U -> %U", format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index, format_ip6_address, address); ip6_ll_table_entry_delete (&ilp); ip6_address_copy (&il->il_ll_addr, address); ip6_address_copy (&ilp.ilp_addr, address); ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL); /* *INDENT-OFF* */ FOREACH_IP6_LINK_DELEGATE (ild, il, ({ if (NULL != il_delegate_vfts[ild->ild_type].ildv_ll_change) il_delegate_vfts[ild->ild_type].ildv_ll_change(ild->ild_index, &il->il_ll_addr); })); /* *INDENT-ON* */ return (0); } ip6_link_delegate_id_t ip6_link_delegate_register (const ip6_l
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#ifndef _IGMP_H_
#define _IGMP_H_

#include <vlib/vlib.h>
#include <vnet/ip/ip.h>
#include <vlibapi/api_helper_macros.h>
#include <vnet/ip/igmp_packet.h>
#include <vnet/adj/adj_mcast.h>
#include <igmp/igmp_types.h>
#include <igmp/igmp_format.h>
#include <igmp/igmp_timer.h>
#include <igmp/igmp_group.h>
#include <igmp/igmp_config.h>

/**
 * RFC 3376 Section 8.1
 */
#define IGMP_DEFAULT_ROBUSTNESS_VARIABLE	(2)

#define IGMP_DBG(...) \
    vlib_log_debug (igmp_main.logger, __VA_ARGS__);

/**
 * General Query address - 224.0.0.1
 * Membership Report address - 224.0.0.22
 * SSM defau