예제 #1
0
    def create_ipv6_inner_pkt_only(self, src_ip, dst_ip, tc, encap=False, hlim=64):
        """Creates an IPv6 only packet for the test
        @param src_ip: source ip
        @param dst_ip: destination ip
        @param tc: traffic class
        @param encap: build encapsulated packet.
                      If @encap is True the return packet would be:
                      IP(@src_ip, @dst_ip, @tc) / IP(dst_ip=4::4, src_ip=3::3) / TCP()
        """

        # no ptf function to build simple ipv6 only packet
        # so use simple_tcpv6_packet function which builds the same packet
        # with TCP header as simple_ip_only_packet but extract away Ethernet

        inner_pkt = simple_tcpv6_packet(ipv6_dst=dst_ip, ipv6_src=src_ip, ipv6_hlim=hlim, ipv6_tc=tc).getlayer(scapy.IPv6)
        if encap:
            inner_pkt2 = self.create_ipv6_inner_pkt_only(self.DEFAULT_INNER2_V6_PKT_SRC_IP,
                                                         self.DEFAULT_INNER2_V6_PKT_DST_IP,
                                                         0)
            inner_pkt = simple_ipv6ip_packet(ipv6_src=src_ip,
                                             ipv6_dst=dst_ip,
                                             ipv6_tc=tc,
                                             ipv6_hlim=hlim,
                                             inner_frame=inner_pkt2).getlayer(scapy.IPv6) # get only the IP layer

        return inner_pkt
예제 #2
0
 def runTest(self):
     pktlen = 400
     pkt = testutils.simple_ipv6ip_packet(pktlen=pktlen)
     self.assertEqual(len(pkt), pktlen)
     testutils.send_packet(self, (0, 1), pkt)
     print("packet sent")
     testutils.verify_packet(self, pkt, (1, 1))
     testutils.verify_no_other_packets(self, 1)
예제 #3
0
    def runTest(self):
        pktlen = 1000
        udp = testutils.simple_udp_packet()
        ipv6 = testutils.simple_ipv6ip_packet(inner_frame=udp['UDP'])
        gre = testutils.simple_grev6_packet(pktlen=pktlen,
                                            inner_frame=ipv6["IPv6"])

        self.assertEqual(gre['GRE'].proto, 0x86DD)
        testutils.send_packet(self, (0, 1), gre)
        print("packet sent")
        testutils.verify_packet(self, gre, (1, 1))
        testutils.verify_no_other_packets(self, 1)
예제 #4
0
    def create_encap_packet(self,
                            dst_ip,
                            outer_pkt='ipv4',
                            triple_encap=False,
                            outer_ttl=None,
                            inner_ttl=None):
        """Creates an IPv4/IPv6 encapsulated packet in @outer_pkt packet
        @param dst_ip: Destination IP for inner packet. Depending @dst_ip IPv4 or IPv6 packet will be created
        @param outer_pkt: Outer packet type to encapsulate inner packet in (ipv4/ipv6)
        @param triple_encap: Whether to build triple encapsulated packet
        @outer_ttl: TTL for the outer layer
        @inner_ttl: TTL for the inner layer
        @return: built packet and expected packet to match after decapsulation
        """

        src_mac = self.dataplane.get_mac(0, 0)
        dst_mac = '00:11:22:33:44:55'
        router_mac = self.test_params['router_mac']

        # Set DSCP value for the inner layer
        dscp_in = self.DSCP_RANGE[self.dscp_in_idx]
        self.dscp_in_idx = (self.dscp_in_idx + 1) % len(
            self.DSCP_RANGE)  # Next packet will use a different DSCP

        # TC for IPv6, ToS for IPv4
        tc_in = tos_in = dscp_in << 2

        # Set DSCP value for the outer layer
        dscp_out = self.DSCP_RANGE[self.dscp_out_idx]
        self.dscp_out_idx = (self.dscp_out_idx + 1) % len(
            self.DSCP_RANGE)  # Next packet will use a different DSCP

        # TC for IPv6, ToS for IPv4
        tc_out = tos_out = dscp_out << 2

        if "pipe" == self.test_params['dscp_mode']:
            exp_tc = exp_tos = tc_in
        elif "uniform" == self.test_params['dscp_mode']:
            exp_tc = exp_tos = tc_out
        else:
            print("ERROR: no dscp is configured")
            exit()

        # Set TTL value for the outer layer
        if outer_ttl is None:
            outer_ttl = self.TTL_RANGE[self.ttl_out_idx]
            self.ttl_out_idx = (self.ttl_out_idx + 1) % len(
                self.TTL_RANGE)  # Next packet will use a different TTL

        # Set TTL value for the inner layer
        if inner_ttl is None:
            inner_ttl = self.TTL_RANGE[self.ttl_in_idx]
            self.ttl_in_idx = (self.ttl_in_idx + 1) % len(
                self.TTL_RANGE)  # Next packet will use a different TTL

        if "pipe" == self.test_params['ttl_mode']:
            exp_ttl = inner_ttl - 1
        elif "uniform" == self.test_params["ttl_mode"]:
            exp_ttl = outer_ttl - 1
        else:
            print("ERROR: unexpected ttl_mode is configured")
            exit()

        if ipaddress.ip_address(unicode(dst_ip)).version == 6:
            inner_src_ip = self.DEFAULT_INNER_V6_PKT_SRC_IP
            # build inner packet, if triple_encap is True inner_pkt would be double encapsulated
            inner_pkt = self.create_ipv6_inner_pkt_only(inner_src_ip,
                                                        dst_ip,
                                                        tos_in,
                                                        triple_encap,
                                                        hlim=inner_ttl)

            # build expected packet based on inner packet
            # set the correct L2 fields
            exp_pkt = scapy.Ether(dst=dst_mac, src=router_mac) / inner_pkt

            # set expected TC value
            exp_pkt['IPv6'].tc = exp_tc
            # decrement TTL
            exp_pkt['IPv6'].hlim = exp_ttl
        else:
            inner_src_ip = self.DEFAULT_INNER_V4_PKT_SRC_IP
            # build inner packet, if triple_encap is True inner_pkt would be double encapsulated
            inner_pkt = self.create_ipv4_inner_pkt_only(inner_src_ip,
                                                        dst_ip,
                                                        tos_in,
                                                        triple_encap,
                                                        ttl=inner_ttl)

            # build expected packet based on inner packet
            # set the correct L2 fields
            exp_pkt = scapy.Ether(dst=dst_mac, src=router_mac) / inner_pkt

            # set expected ToS value
            exp_pkt['IP'].tos = exp_tos
            # decrement TTL
            exp_pkt['IP'].ttl = exp_ttl

        if outer_pkt == 'ipv4':
            pkt = simple_ipv4ip_packet(eth_dst=router_mac,
                                       eth_src=src_mac,
                                       ip_src='1.1.1.1',
                                       ip_dst=self.test_params['lo_ip'],
                                       ip_tos=tos_out,
                                       ip_ttl=outer_ttl,
                                       inner_frame=inner_pkt)
        elif outer_pkt == 'ipv6':
            pkt = simple_ipv6ip_packet(eth_dst=router_mac,
                                       eth_src=src_mac,
                                       ipv6_src='1::1',
                                       ipv6_dst=self.test_params['lo_ipv6'],
                                       ipv6_tc=tc_out,
                                       ipv6_hlim=outer_ttl,
                                       inner_frame=inner_pkt)
        else:
            raise Exception("ERROR: invalid outer packet type ", outer_pkt)

        return pkt, exp_pkt