#!/usr/bin/env python3 import unittest from scapy.layers.l2 import Ether from scapy.packet import Raw from scapy.layers.inet import IP, IPOption from scapy.contrib.igmpv3 import IGMPv3, IGMPv3gr, IGMPv3mq, IGMPv3mr from framework import tag_fixme_vpp_workers from framework import VppTestCase, VppTestRunner from vpp_igmp import find_igmp_state, IGMP_FILTER, IgmpRecord, IGMP_MODE, \ IgmpSG, VppHostState, wait_for_igmp_event from vpp_ip_route import find_mroute, VppIpTable class IgmpMode: HOST = 1 ROUTER = 0 @tag_fixme_vpp_workers class TestIgmp(VppTestCase): """ IGMP Test Case """ @classmethod def setUpClass(cls): super(TestIgmp, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIgmp, cls).tearDownClass() def setUp(self): super(TestIgmp, self).setUp() self.create_pg_interfaces(range(4)) self.sg_list = [] self.config_list = [] self.ip_addr = [] self.ip_table = VppIpTable(self, 1) self.ip_table.add_vpp_config() for pg in self.pg_interfaces[2:]: pg.set_table_ip4(1) for pg in self.pg_interfaces: pg.admin_up() pg.config_ip4() pg.resolve_arp() def tearDown(self): for pg in self.pg_interfaces: self.vapi.igmp_clear_interface(pg.sw_if_index) pg.unconfig_ip4() pg.set_table_ip4(0) pg.admin_down() super(TestIgmp, self).tearDown() def send(self, ti, pkts): ti.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() def test_igmp_flush(self): """ IGMP Link Up/down and Flush """ # # FIX THIS. Link down. # def test_igmp_enable(self): """ IGMP enable/disable on an interface check for the addition/removal of the IGMP mroutes """ self.vapi.igmp_enable_disable(self.pg0.sw_if_index, 1, IGMP_MODE.HOST) self.vapi.igmp_enable_disable(self.pg1.sw_if_index, 1, IGMP_MODE.HOST) self.assertTrue(find_mroute(self, "224.0.0.1", "0.0.0.0", 32)) self.assertTrue(find_mroute(self, "224.0.0.22", "0.0.0.0", 32)) self.vapi.igmp_enable_disable(self.pg2.sw_if_index, 1, IGMP_MODE.HOST) self.vapi.igmp_enable_disable(self.pg3.sw_if_index, 1, IGMP_MODE.HOST) self.assertTrue(find_mroute(self, "224.0.0.1", "0.0.0.0", 32, table_id=1)) self.assertTrue(find_mroute(self, "224.0.0.22", "0.0.0.0", 32, table_id=1)) self.vapi.igmp_enable_disable(self.pg0.sw_if_index, 0, IGMP_MODE.HOST) self.vapi.igmp_enable_disable(self.pg1.sw_if_index, 0, IGMP_MODE.HOST) self.vapi.igmp_enable_disable(self.pg2.sw_if_index, 0, IGMP_MODE.HOST) self.vapi.igmp_enable_disable(self.pg3.sw_if_index, 0, IGMP_MODE.HOST) self.assertTrue(find_mroute(self, "224.0.0.1", "0.0.0.0", 32)) self.assertFalse(find_mroute(self, "224.0.0.22", "0.0.0.0", 32)) self.assertTrue(find_mroute(self, "224.0.0.1", "0.0.0.0", 32, table_id=1)) self.assertFalse(find_mroute(self, "224.0.0.22", "0.0.0.0", 32, table_id=1)) def verify_general_query(self, p): ip = p[IP] self.assertEqual(len(ip.options), 1) self.assertEqual(ip.options[0].option, 20) self.assertEqual(ip.dst, "224.0.0.1") self.assertEqual(ip.proto, 2) igmp = p[IGMPv3] self.assertEqual(igmp.type, 0x11) self.assertEqual(igmp.gaddr, "0.0.0.0") def verify_group_query(self, p, grp, srcs): ip = p[IP] self.assertEqual(ip.dst, grp) self.assertEqual(ip.proto, 2) self.assertEqual(len(ip.options), 1) self.assertEqual(ip.options[0].option, 20) self.assertEqual(ip.proto, 2) igmp = p[IGMPv3] self.assertEqual(igmp.type, 0x11) self.assertEqual(igmp.gaddr, grp) def verify_report(self, rx, records): ip = rx[IP] self.assertEqual(rx[IP].dst, "224.0.0.22") self.assertEqual(len(ip.options), 1) self.assertEqual(ip.options[0].option, 20) self.assertEqual(ip.proto, 2) self.assertEqual(IGMPv3.igmpv3types[rx[IGMPv3].type], "Version 3 Membership Report") self.assertEqual(rx[IGMPv3mr].numgrp, len(records)) received = rx[IGMPv3mr].records for ii in range(len(records)): gr = received[ii] r = records[ii] self.assertEqual(IGMPv3gr.igmpv3grtypes[gr.rtype], r.type) self.assertEqual(gr.numsrc, len(r.sg.saddrs)) self.assertEqual(gr.maddr, r.sg.gaddr) self.assertEqual(len(gr.srcaddrs), len(r.sg.saddrs)) self.assertEqual(sorted(gr.srcaddrs), sorted(r.sg.saddrs)) def add_group(self, itf, sg, n_pkts=2): self.pg_enable_capture(self.pg_interfaces) self.pg_start() hs = VppHostState(self, IGMP_FILTER.INCLUDE, itf.sw_if_index, sg) hs.add_vpp_config() capture = itf.get_capture(n_pkts, timeout=10) # reports are transmitted twice due to default rebostness value=2 self.verify_report(capture[0], [IgmpRecord(sg, "Allow New Sources")]), self.verify_report(capture[1], [IgmpRecord(sg, "Allow New Sources")]), return hs def remove_group(self, hs): self.pg_enable_capture(self.pg_interfaces) self.pg_start() hs.remove_vpp_config() capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(hs.sg, "Block Old Sources")]) def test_igmp_host(self): """ IGMP Host functions """ # # Enable interface for host functions # self.vapi.igmp_enable_disable(self.pg0.sw_if_index, 1, IGMP_MODE.HOST) # # Add one S,G of state and expect a state-change event report # indicating the addition of the S,G # h1 = self.add_group(self.pg0, IgmpSG("239.1.1.1", ["1.1.1.1"])) # search for the corresponding state created in VPP dump = self.vapi.igmp_dump(self.pg0.sw_if_index) self.assertEqual(len(dump), 1) self.assertTrue(find_igmp_state(dump, self.pg0, "239.1.1.1", "1.1.1.1")) # # Send a general query (to the all router's address) # expect VPP to respond with a membership report. # Pad the query with 0 - some devices in the big wild # internet are prone to this. # p_g = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='224.0.0.1', tos=0xc0) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="0.0.0.0") / Raw(b'\x00' * 10)) self.send(self.pg0, p_g) capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(h1.sg, "Mode Is Include")]) # # Group specific query # p_gs = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='239.1.1.1', tos=0xc0, options=[IPOption(copy_flag=1, optclass="control", option="router_alert")]) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="239.1.1.1")) self.send(self.pg0, p_gs) capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(h1.sg, "Mode Is Include")]) # # A group and source specific query, with the source matching # the source VPP has # p_gs1 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='239.1.1.1', tos=0xc0, options=[IPOption(copy_flag=1, optclass="control", option="router_alert")]) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="239.1.1.1", srcaddrs=["1.1.1.1"])) self.send(self.pg0, p_gs1) capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(h1.sg, "Mode Is Include")]) # # A group and source specific query that reports more sources # than the packet actually has. # p_gs2 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='239.1.1.1', tos=0xc0, options=[IPOption(copy_flag=1, optclass="control", option="router_alert")]) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="239.1.1.1", numsrc=4, srcaddrs=["1.1.1.1"])) self.send_and_assert_no_replies(self.pg0, p_gs2, timeout=10) # # A group and source specific query, with the source NOT matching # the source VPP has. There should be no response. # p_gs2 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='239.1.1.1', tos=0xc0, options=[IPOption(copy_flag=1, optclass="control", option="router_alert")]) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="239.1.1.1", srcaddrs=["1.1.1.2"])) self.send_and_assert_no_replies(self.pg0, p_gs2, timeout=10) # # A group and source specific query, with the multiple sources # one of which matches the source VPP has. # The report should contain only the source VPP has. # p_gs3 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst='239.1.1.1', tos=0xc0, options=[IPOption(copy_flag=1, optclass="control", option="router_alert")]) / IGMPv3(type="Membership Query", mrcode=100) / IGMPv3mq(gaddr="239.1.1.1", srcaddrs=["1.1.1.1", "1.1.1.2", "1.1.1.3"])) self.send(self.pg0, p_gs3) capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(h1.sg, "Mode Is Include")]) # # Two source and group specific queries in quick succession, the # first does not have VPPs source the second does. then vice-versa # self.send(self.pg0, [p_gs2, p_gs1]) capture = self.pg0.get_capture(1, timeout=10) self.verify_report(capture[0], [IgmpRecord(h1.sg, "Mode Is Include")]) self.send(self.pg0, [p_gs1, p_gs
/*
 * Copyright (c) 2015 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.
 */
/*
 * srp/packet.h: srp packet format.
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef included_srp_packet_h
#define included_srp_packet_h

#include <vppinfra/byte_order.h>
#include <vppinfra/bitops.h>
#include <vnet/ethernet/packet.h>

/* SRP version 2. */

#define foreach_srp_mode			\
  _ (reserved0)					\
  _ (reserved1)					\
  _ (reserved2)					\
  _ (reserved3)					\
  _ (control_pass_to_host)			\
  _ (control_locally_buffered_for_host)		\
  _ (keep_alive)				\
  _ (data)

typedef enum {
#define _(f) SRP_MODE_##f,
  foreach_srp_mode
#undef _
  SRP_N_MODE,
} srp_mode_t;

typedef union {
  /* For computing parity bit. */
  u16 as_u16;

  struct {
    u8 ttl;

#if CLIB_ARCH_IS_BIG_ENDIAN
    u8 is_inner_ring : 1;
    u8 mode : 3;
    u8 priority : 3;
    u8 parity : 1;
#endif
#if CLIB_ARCH_IS_LITTLE_ENDIAN
    u8 parity : 1;
    u8 priority : 3;
    u8 mode : 3;
    u8 is_inner_ring : 1;
#endif
  };
} srp_header_t;

always_inline void
srp_header_compute_parity (srp_header_t * h)
{
  h->parity = 0;
  h->parity = count_set_bits (h->as_u16) ^ 1; /* odd parity */
}

typedef struct {
  srp_header_t srp;
  ethernet_header_t ethernet;
} srp_and_ethernet_header_t;

#define foreach_srp_control_packet_type		\
  _ (reserved)					\
  _ (topology)					\
  _ (ips)

typedef enum {
#define _(f) SRP_CONTROL_PACKET_TYPE_##f,
  foreach_srp_control_packet_type
#undef _
  SRP_N_CONTROL_PACKET_TYPE,
} srp_control_packet_type_t;

typedef CLIB_PACKED (struct {
  /* Set to 0. */
  u8 version;

  srp_control_packet_type_t type : 8;

  /* IP4-like checksum of packet starting with start of control header. */
  u16 checksum;

  u16 ttl;
}) srp_control_header_t;

typedef struct {
  srp_header_t srp;
  ethernet_header_t ethernet;
  srp_control_header_t control;
} srp_generic_control_header_t;

typedef struct {
  u8 flags;
#define SRP_TOPOLOGY_MAC_BINDING_FLAG_IS_INNER_RING (1 << 6)
#define SRP_TOPOLOGY_MAC_BINDING_FLAG_IS_WRAPPED (1 << 5)

  /* MAC address. */
  u8 address[6];
} srp_topology_mac_binding_t;

typedef CLIB_PACKED (struct {
  srp_header_t srp;
  ethernet_header_t ethernet;
  srp_control_header_t control;

  /* Length in bytes of data that follows. */
  u16 n_bytes_of_data_that_follows;

  /* MAC address of originator of this topology request. */
  u8 originator_address[6];

  /* Bindings follow. */
  srp_topology_mac_binding_t bindings[0];
}) srp_topology_header_t;

#define foreach_srp_ips_request_type		\
  _ (idle, 0x0)					\
  _ (wait_to_restore, 0x5)			\
  _ (manual_switch, 0x6)			\
  _ (signal_degrade, 0x8)			\
  _ (signal_fail, 0xb)				\
  _ (forced_switch, 0xd)

typedef enum {
#define _(f,n) SRP_IPS_REQUEST_##f = n,
  foreach_srp_ips_request_type
#undef _
} srp_ips_request_type_t;

#define foreach_srp_ips_status			\
  _ (idle, 0x0)					\
  _ (wrapped, 0x2)

typedef enum {
#define _(f,n) SRP_IPS_STATUS_##f = n,
  foreach_srp_ips_status
#undef _
} srp_ips_status_t;

typedef struct {
  srp_header_t srp;
  ethernet_header_t ethernet;
  srp_control_header_t control;
  u8 originator_address[6];

  union {
    u8 ips_octet;

    struct {
#if CLIB_ARCH_IS_BIG_ENDIAN
      u8 request_type : 4;
      u8 is_long_path : 1;
      u8 status : 3;
#endif
#if CLIB_ARCH_IS_LITTLE_ENDIAN
      u8 status : 3;
      u8 is_long_path : 1;
      u8 request_type : 4;
#endif
    };
  };

  u8 reserved;
} srp_ips_header_t;

#endif /* included_srp_packet_h */