#!/usr/bin/env python """ACL IRB Test Case HLD: **config** - L2 MAC learning enabled in l2bd - 2 routed interfaces untagged, bvi (Bridge Virtual Interface) - 2 bridged interfaces in l2bd with bvi **test** - sending ip4 eth pkts between routed interfaces - 2 routed interfaces - 2 bridged interfaces - 64B, 512B, 1518B, 9200B (ether_size) - burst of pkts per interface - 257pkts per burst - routed pkts hitting different FIB entries - bridged pkts hitting different MAC entries **verify** - all packets received correctly """ import unittest from socket import inet_pton, AF_INET, AF_INET6 from random import choice, shuffle from pprint import pprint import scapy.compat from scapy.packet import Raw from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP, ICMP, TCP from scapy.layers.inet6 import IPv6, ICMPv6Unknown, ICMPv6EchoRequest from scapy.layers.inet6 import ICMPv6EchoReply, IPv6ExtHdrRouting from scapy.layers.inet6 import IPv6ExtHdrFragment from framework import VppTestCase, VppTestRunner from vpp_l2 import L2_PORT_TYPE import time class TestACLpluginL2L3(VppTestCase): """TestACLpluginL2L3 Test Case""" @classmethod def setUpClass(cls): """ #. Create BD with MAC learning enabled and put interfaces to this BD. #. Configure IPv4 addresses on loopback interface and routed interface. #. Configure MAC address binding to IPv4 neighbors on loop0. #. Configure MAC address on pg2. #. Loopback BVI interface has remote hosts, one half of hosts are behind pg0 second behind pg1. """ super(TestACLpluginL2L3, cls).setUpClass() cls.pg_if_packet_sizes = [64, 512, 1518, 9018] # packet sizes cls.bd_id = 10 cls.remote_hosts_count = 250 # create 3 pg interfaces, 1 loopback interface cls.create_pg_interfaces(range(3)) cls.create_loopback_interfaces(1) cls.interfaces = list(cls.pg_interfaces) cls.interfaces.extend(cls.lo_interfaces) for i in cls.interfaces: i.admin_up() # Create BD with MAC learning enabled and put interfaces to this BD cls.vapi.sw_interface_set_l2_bridge( rx_sw_if_index=cls.loop0.sw_if_index, bd_id=cls.bd_id, port_type=L2_PORT_TYPE.BVI) cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=cls.pg0.sw_if_index, bd_id=cls.bd_id) cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.bd_id) # Configure IPv4 addresses on loopback interface and routed interface cls.loop0.config_ip4() cls.loop0.config_ip6() cls.pg2.config_ip4() cls.pg2.config_ip6() # Configure MAC address binding to IPv4 neighbors on loop0 cls.loop0.generate_remote_hosts(cls.remote_hosts_count) cls.loop0.configure_ipv4_neighbors() cls.loop0.configure_ipv6_neighbors() # configure MAC address on pg2 cls.pg2.resolve_arp() cls.pg2.resolve_ndp() cls.WITHOUT_EH = False cls.WITH_EH = True cls.STATELESS_ICMP = False cls.STATEFUL_ICMP = True # Loopback BVI interface has remote hosts, one half of hosts are behind # pg0 second behind pg1 half = cls.remote_hosts_count // 2 cls.pg0.remote_hosts = cls.loop0.remote_hosts[:half] cls.pg1.remote_hosts = cls.loop0.remote_hosts[half:] @classmethod def tearDownClass(cls): super(TestACLpluginL2L3, cls).tearDownClass() def tearDown(self): """Run standard test teardown and log ``show l2patch``, ``show l2fib verbose``,``show bridge-domain detail``, ``show ip arp``. """ super(TestACLpluginL2L3, self).tearDown() def show_commands_at_teardown(self): self.logger.info(self.vapi.cli("show l2patch")) self.logger.info(self.vapi.cli("show classify tables")) self.logger.info(self.vapi.cli("show l2fib verbose")) self.logger.info(self.vapi.cli("show bridge-domain %s detail" % self.bd_id)) self.logger.info(self.vapi.cli("show ip arp")) self.logger.info(self.vapi.cli("show ip6 neighbors")) cmd = "show acl-plugin sessions verbose 1" self.logger.info(self.vapi.cli(cmd)) self.logger.info(self.vapi.cli("show acl-plugin acl")) self.logger.info(self.vapi.cli("show acl-plugin interface")) self.logger.info(self.vapi.cli("show acl-plugin tables")) def create_stream(self, src_ip_if, dst_ip_if, reverse, packet_sizes, is_ip6, expect_blocked, expect_established, add_extension_header, icmp_stateful=False): pkts = [] rules = [] permit_rules = [] permit_and_reflect_rules = [] total_packet_count = 8 for i in range(0, total_packet_count): modulo = (i//2) % 2 icmp_type_delta = i % 2 icmp_code = i is_udp_packet = (modulo == 0) if is_udp_packet and icmp_stateful: continue is_reflectable_icmp = (icmp_stateful and icmp_type_delta == 0 and not is_udp_packet) is_reflected_icmp = is_reflectable_icmp and expect_established can_reflect_this_packet = is_udp_packet or is_reflectable_icmp is_permit = i % 2 remote_dst_index = i % len(dst_ip_if.remote_hosts) remote_dst_host = dst_ip_if.remote_hosts[remote_dst_index] if is_permit == 1: info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) else: to_be_blocked = False if (expect_blocked and not expect_established): to_be_blocked = True if (not can_reflect_this_packet): to_be_blocked = True if to_be_blocked: payload = "to be blocked" else: info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) if reverse: dst_mac = 'de:ad:00:00:00:00' src_mac = remote_dst_host._mac dst_ip6 = src_ip_if.remote_ip6 src_ip6 = remote_dst_host.ip6 dst_ip4 = src_ip_if.remote_ip4 src_ip4 = remote_dst_host.ip4 dst_l4 = 1234 + i src_l4 = 4321 + i else: dst_mac = src_ip_if.local_mac src_mac = src_ip_if.remote_mac src_ip6 = src_ip_if.remote_ip6 dst_ip6 = remote_dst_host.ip6 src_ip4 = src_ip_if.remote_ip4 dst_ip4 = remote_dst_host.ip4 src_l4 = 1234 + i dst_l4 = 4321 + i if is_reflected_icmp: icmp_type_delta = 1 # default ULP should be something we do not use in tests ulp_l4 = TCP(sport=src_l4, dport=dst_l4) # potentially a chain of protocols leading to ULP ulp = ulp_l4 if is_udp_packet: if is_ip6: ulp_l4 = UDP(sport=src_l4, dport=dst_l4) if add_extension_header: # prepend some extension headers ulp = (IPv6ExtHdrRouting() / IPv6ExtHdrRouting() / IPv6ExtHdrFragment(offset=0, m=1) / ulp_l4) # uncomment below to test invalid ones # u
/*
 * Copyright (c) 2020 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.
 */

/**
 * @file
 * @brief IP prefix management on interfaces
 */

#ifndef included_ip_interface_h
#define included_ip_interface_h

#include <vnet/ip/lookup.h>

clib_error_t *ip_interface_address_add (ip_lookup_main_t * lm,
					u32 sw_if_index,
					void *address,
					u32 address_length,
					u32 * result_index);
clib_error_t *ip_interface_address_del (ip_lookup_main_t * lm,
					vnet_main_t * vnm,
					u32 addr_index, void *address,
					u32 address_length, u32 sw_if_index);
void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
void ip_interface_address_mark (void);
void ip_interface_address_sweep (void);
u32 ip_interface_address_find (ip_lookup_main_t * lm,
			       void *addr_fib, u32 address_length);
u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);

always_inline void *
ip_interface_address_get_address (ip_lookup_main_t * lm,
				  ip_interface_address_t * a)
{
  return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
}

always_inline ip_interface_prefix_t *
ip_get_interface_prefix (ip_lookup_main_t * lm, ip_interface_prefix_key_t * k)
{
  uword *p = mhash_get (&lm->prefix_to_if_prefix_index, k);
  return p ? pool_elt_at_index (lm->if_prefix_pool, p[0]) : 0;
}

/* *INDENT-OFF* */
#define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
do {                                                                    \
    vnet_main_t *_vnm = vnet_get_main();                                \
    u32 _sw_if_index = sw_if_index;                                     \
    vnet_sw_interface_t *_swif;                                         \
    _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
                                                                        \
    /*                                                                  \
     * Loop => honor unnumbered interface addressing.                   \
     */                                                                 \
    if (_swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)               \
      {                                                                 \
        if (loop)                                                       \
          _sw_if_index = _swif->unnumbered_sw_if_index;                 \
        else                                                            \
          /* the interface is unnumbered, by the caller does not want   \
           * unnumbered interfaces considered/honoured */               \
          break;                                                        \
      }                                                                 \
    u32 _ia = ((vec_len((lm)->if_address_pool_index_by_sw_if_index)     \
                > (_sw_if_index)) ?                                     \
               vec_elt ((lm)->if_address_pool_index_by_sw_if_index,     \
                        (_sw_if_index)) :                               \
               (u32)~0);                                                \
    ip_interface_address_t * _a;                                        \
    while (_ia != ~0)                                                   \
    {                                                                   \
        _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
        _ia = _a->next_this_sw_interface;                               \
        (a) = _a;                                                       \
        body;                                                           \
    }                                                                   \
} while (0)
/* *INDENT-ON* */

#endif /* included_ip_interface_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
is_ip6, add_eh, stateful_icmp) self.run_traffic_ip46_bridged_to_routed(test_l2_action, is_ip6, True, False, add_eh, stateful_icmp) self.run_traffic_ip46_routed_to_bridged(test_l2_action, is_ip6, False, True, add_eh, stateful_icmp) def test_0000_ip6_irb_1(self): """ ACL plugin prepare""" if not self.vpp_dead: cmd = "set acl-plugin session timeout udp idle 2000" self.logger.info(self.vapi.ppcli(cmd)) # uncomment to not skip past the routing header # and watch the EH tests fail # self.logger.info(self.vapi.ppcli( # "set acl-plugin skip-ipv6-extension-header 43 0")) # uncomment to test the session limit (stateful tests will fail) # self.logger.info(self.vapi.ppcli( # "set acl-plugin session table max-entries 1")) # new datapath is the default, but just in case # self.logger.info(self.vapi.ppcli( # "set acl-plugin l2-datapath new")) # If you want to see some tests fail, uncomment the next line # self.logger.info(self.vapi.ppcli( # "set acl-plugin l2-datapath old")) def test_0001_ip6_irb_1(self): """ ACL IPv6 routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, True, False, self.WITHOUT_EH) def test_0002_ip6_irb_1(self): """ ACL IPv6 routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, True, False, self.WITHOUT_EH) def test_0003_ip4_irb_1(self): """ ACL IPv4 routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, False, False, self.WITHOUT_EH) def test_0004_ip4_irb_1(self): """ ACL IPv4 routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, False, False, self.WITHOUT_EH) def test_0005_ip6_irb_1(self): """ ACL IPv6 bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, True, False, self.WITHOUT_EH) def test_0006_ip6_irb_1(self): """ ACL IPv6 bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, True, False, self.WITHOUT_EH) def test_0007_ip6_irb_1(self): """ ACL IPv4 bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, False, False, self.WITHOUT_EH) def test_0008_ip6_irb_1(self): """ ACL IPv4 bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, False, False, self.WITHOUT_EH) # Stateful ACL tests def test_0101_ip6_irb_1(self): """ ACL IPv6 routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, True, self.WITHOUT_EH) def test_0102_ip6_irb_1(self): """ ACL IPv6 bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, True, self.WITHOUT_EH) def test_0103_ip6_irb_1(self): """ ACL IPv4 routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, False, self.WITHOUT_EH) def test_0104_ip6_irb_1(self): """ ACL IPv4 bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, False, self.WITHOUT_EH) def test_0111_ip6_irb_1(self): """ ACL IPv6 routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, True, self.WITHOUT_EH) def test_0112_ip6_irb_1(self): """ ACL IPv6 bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, True, self.WITHOUT_EH) def test_0113_ip6_irb_1(self): """ ACL IPv4 routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, False, self.WITHOUT_EH) def test_0114_ip6_irb_1(self): """ ACL IPv4 bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, False, self.WITHOUT_EH) # A block of tests with extension headers def test_1001_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, True, False, self.WITH_EH) def test_1002_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, True, False, self.WITH_EH) def test_1005_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, True, False, self.WITH_EH) def test_1006_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, True, False, self.WITH_EH) def test_1101_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, True, self.WITH_EH) def test_1102_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, True, self.WITH_EH) def test_1111_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, True, self.WITH_EH) def test_1112_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, True, self.WITH_EH) # IPv4 with "MF" bit set def test_1201_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, False, False, self.WITH_EH) def test_1202_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, False, False, self.WITH_EH) def test_1205_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, False, False, self.WITH_EH) def test_1206_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, False, False, self.WITH_EH) def test_1301_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, False, self.WITH_EH) def test_1302_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, False, self.WITH_EH) def test_1311_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, False, self.WITH_EH) def test_1312_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, False, self.WITH_EH) # Stateful ACL tests with stateful ICMP def test_1401_ip6_irb_1(self): """ IPv6 routed -> bridged, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1402_ip6_irb_1(self): """ IPv6 bridged -> routed, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1403_ip4_irb_1(self): """ IPv4 routed -> bridged, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1404_ip4_irb_1(self): """ IPv4 bridged -> routed, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1411_ip6_irb_1(self): """ IPv6 routed -> bridged, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1412_ip6_irb_1(self): """ IPv6 bridged -> routed, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1413_ip4_irb_1(self): """ IPv4 routed -> bridged, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1414_ip4_irb_1(self): """ IPv4 bridged -> routed, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, False, self.WITHOUT_EH, self.STATEFUL_ICMP) if __name__ == '__main__': unittest.main(testRunner=VppTestRunner)