Ejemplo n.º 1
0
    def _build_itag(self):
        b_src_mac = '00:07:0d:af:f4:54'
        b_dst_mac = '00:00:00:00:00:00'
        b_ethertype = ether.ETH_TYPE_8021AD
        e1 = ethernet.ethernet(b_dst_mac, b_src_mac, b_ethertype)

        b_pcp = 0
        b_cfi = 0
        b_vid = 32
        b_ethertype = ether.ETH_TYPE_8021Q
        bt = vlan.svlan(b_pcp, b_cfi, b_vid, b_ethertype)

        c_src_mac = '11:11:11:11:11:11'
        c_dst_mac = 'aa:aa:aa:aa:aa:aa'
        c_ethertype = ether.ETH_TYPE_8021AD
        e2 = ethernet.ethernet(c_dst_mac, c_src_mac, c_ethertype)

        s_pcp = 0
        s_cfi = 0
        s_vid = 32
        s_ethertype = ether.ETH_TYPE_8021Q
        st = vlan.svlan(s_pcp, s_cfi, s_vid, s_ethertype)

        c_pcp = 0
        c_cfi = 0
        c_vid = 32
        c_ethertype = ether.ETH_TYPE_IP
        ct = vlan.vlan(c_pcp, c_cfi, c_vid, c_ethertype)

        version = 4
        header_length = 20
        tos = 0
        total_length = 24
        identification = 0x8a5d
        flags = 0
        offset = 1480
        ttl = 64
        proto = inet.IPPROTO_ICMP
        csum = 0xa7f2
        src = '131.151.32.21'
        dst = '131.151.32.129'
        option = b'TEST'
        ip = ipv4.ipv4(version, header_length, tos, total_length,
                       identification, flags, offset, ttl, proto, csum, src,
                       dst, option)

        p = packet.Packet()

        p.add_protocol(e1)
        p.add_protocol(bt)
        p.add_protocol(self.it)
        p.add_protocol(e2)
        p.add_protocol(st)
        p.add_protocol(ct)
        p.add_protocol(ip)
        p.serialize()

        return p
Ejemplo n.º 2
0
    def create_packet(self, primary_ip_address, vlan_id=None):
        """Prepare a VRRP packet.

        Returns a newly created os_ken.lib.packet.packet.Packet object
        with appropriate protocol header objects added by add_protocol().
        It's caller's responsibility to serialize().
        The serialized packet would looks like the ones described in
        the following sections.

        * RFC 3768 5.1. VRRP Packet Format
        * RFC 5798 5.1. VRRP Packet Format

        ================== ====================
        Argument           Description
        ================== ====================
        primary_ip_address Source IP address
        vlan_id            VLAN ID.  None for no VLAN.
        ================== ====================
        """
        if self.is_ipv6:
            traffic_class = 0xc0        # set tos to internetwork control
            flow_label = 0
            payload_length = ipv6.ipv6._MIN_LEN + len(self)     # XXX _MIN_LEN
            e = ethernet.ethernet(VRRP_IPV6_DST_MAC_ADDRESS,
                                  vrrp_ipv6_src_mac_address(self.vrid),
                                  ether.ETH_TYPE_IPV6)
            ip = ipv6.ipv6(6, traffic_class, flow_label, payload_length,
                           inet.IPPROTO_VRRP, VRRP_IPV6_HOP_LIMIT,
                           primary_ip_address, VRRP_IPV6_DST_ADDRESS)
        else:
            header_length = ipv4.ipv4._MIN_LEN // 4      # XXX _MIN_LEN
            total_length = 0
            tos = 0xc0  # set tos to internetwork control
            identification = self.get_identification()
            e = ethernet.ethernet(VRRP_IPV4_DST_MAC_ADDRESS,
                                  vrrp_ipv4_src_mac_address(self.vrid),
                                  ether.ETH_TYPE_IP)
            ip = ipv4.ipv4(4, header_length, tos, total_length, identification,
                           0, 0, VRRP_IPV4_TTL, inet.IPPROTO_VRRP, 0,
                           primary_ip_address, VRRP_IPV4_DST_ADDRESS)

        p = packet.Packet()
        p.add_protocol(e)
        if vlan_id is not None:
            vlan_ = vlan.vlan(0, 0, vlan_id, e.ethertype)
            e.ethertype = ether.ETH_TYPE_8021Q
            p.add_protocol(vlan_)
        p.add_protocol(ip)
        p.add_protocol(self)
        return p
Ejemplo n.º 3
0
def may_add_vlan(packet, vlan_id):
    """
    :type packet: os_ken.lib.packet.packet.Packet
    :param packet:
    :type vlan_id: int (0 <= vlan_id <= 4095) or None (= No VLAN)
    :param vlan_id:
    """
    if vlan_id is None:
        return

    e = packet.protocols[0]
    assert isinstance(e, ethernet.ethernet)
    v = vlan.vlan(0, 0, vlan_id, e.ethertype)
    e.ethertype = ether.ETH_TYPE_8021Q
    packet.add_protocol(v)
Ejemplo n.º 4
0
    def _build_arp(self, vlan_enabled):
        if vlan_enabled is True:
            ethertype = ether.ETH_TYPE_8021Q
            v = vlan(1, 1, 3, ether.ETH_TYPE_ARP)
        else:
            ethertype = ether.ETH_TYPE_ARP
        e = ethernet(self.dst_mac, self.src_mac, ethertype)
        p = Packet()

        p.add_protocol(e)
        if vlan_enabled is True:
            p.add_protocol(v)
        p.add_protocol(self.a)
        p.serialize()
        return p
Ejemplo n.º 5
0
 def packet_in_handler(self, event):
     if event.msg.match['in_port'] != FAKEPORT:
         return
     pkt = packet.Packet(event.msg.data)
     eth_protocol = pkt.get_protocol(ethernet.ethernet)
     vlan_protocol = pkt.get_protocol(vlan.vlan)
     ipv6_protocol = pkt.get_protocol(ipv6.ipv6)
     icmpv6_protocol = pkt.get_protocol(icmpv6.icmpv6)
     if not (eth_protocol and vlan_protocol and ipv6_protocol
             and icmpv6_protocol):
         return
     if icmpv6_protocol.type_ != icmpv6.ND_NEIGHBOR_SOLICIT:
         return
     if int(ipaddress.ip_address(ipv6_protocol.src)) == 0:
         return
     src_ip = ipaddress.ip_address(icmpv6_protocol.data.dst)
     if src_ip.is_reserved:
         return
     eth_dst = eth_protocol.src
     dst_ip = ipv6_protocol.src
     eth_src = FAKECLIENTMAC
     vid = vlan_protocol.vid
     reply = packet.Packet()
     for protocol in (ethernet.ethernet(eth_dst, eth_src,
                                        ether.ETH_TYPE_8021Q),
                      vlan.vlan(vid=vid, ethertype=ether.ETH_TYPE_IPV6),
                      ipv6.ipv6(src=src_ip,
                                dst=dst_ip,
                                nxt=socket.IPPROTO_ICMPV6,
                                hop_limit=255),
                      icmpv6.icmpv6(
                          type_=icmpv6.ND_NEIGHBOR_ADVERT,
                          data=icmpv6.nd_neighbor(
                              dst=src_ip,
                              option=icmpv6.nd_option_tla(hw_src=eth_src),
                              res=7))):
         reply.add_protocol(protocol)
     reply.serialize()
     out = parser.OFPPacketOut(datapath=event.msg.datapath,
                               buffer_id=ofp.OFP_NO_BUFFER,
                               in_port=ofp.OFPP_CONTROLLER,
                               actions=[parser.OFPActionOutput(FAKEPORT)],
                               data=reply.data)
     self.send_mods(event.msg.datapath, [out])
Ejemplo n.º 6
0
    def test_smoke_packet_in(self):
        nd_solicit = packet.Packet()
        eth_src = '01:02:03:04:05:06'
        eth_dst = 'ff:ff:ff:ff:ff:ff'
        src_ip = 'fc00::1'
        dst_ip = 'fc00::2'
        vid = 2
        for protocol in (ethernet.ethernet(eth_dst, eth_src,
                                           ether.ETH_TYPE_8021Q),
                         vlan.vlan(vid=vid, ethertype=ether.ETH_TYPE_IPV6),
                         ipv6.ipv6(src=src_ip,
                                   dst=dst_ip,
                                   nxt=socket.IPPROTO_ICMPV6,
                                   hop_limit=255),
                         icmpv6.icmpv6(
                             type_=icmpv6.ND_NEIGHBOR_SOLICIT,
                             data=icmpv6.nd_neighbor(
                                 dst=src_ip,
                                 option=icmpv6.nd_option_tla(hw_src=eth_src),
                                 res=7))):
            nd_solicit.add_protocol(protocol)
        nd_solicit.serialize()

        fake_dp = FakeDP()
        fake_pipette = Pipette(dpset={})

        class FakeMsg:
            def __init__(self):
                self.datapath = fake_dp
                self.match = {'in_port': FAKEPORT}
                self.data = nd_solicit.data

        class FakePiEv:
            def __init__(self):
                self.msg = FakeMsg()

        fake_pipette = Pipette(dpset={})
        fake_pipette.packet_in_handler(FakePiEv())
        assert fake_dp.msgs
Ejemplo n.º 7
0
def build_pkt_header(vid, eth_src, eth_dst, dl_type):
    """Return an Ethernet packet header.

    Args:
        vid (int or None): VLAN VID to use (or None).
        eth_src (str): source Ethernet MAC address.
        eth_dst (str): destination Ethernet MAC address.
        dl_type (int): EtherType.
    Returns:
        ryu.lib.packet.ethernet: Ethernet packet with header.
    """
    pkt_header = packet.Packet()
    if vid is None:
        eth_header = ethernet.ethernet(eth_dst, eth_src, dl_type)
        pkt_header.add_protocol(eth_header)
    else:
        eth_header = ethernet.ethernet(eth_dst, eth_src,
                                       valve_of.ether.ETH_TYPE_8021Q)
        pkt_header.add_protocol(eth_header)
        vlan_header = vlan.vlan(vid=vid, ethertype=dl_type)
        pkt_header.add_protocol(vlan_header)
    return pkt_header
Ejemplo n.º 8
0
    def _build_svlan(self):
        src_mac = '00:07:0d:af:f4:54'
        dst_mac = '00:00:00:00:00:00'
        ethertype = ether.ETH_TYPE_8021AD
        e = ethernet(dst_mac, src_mac, ethertype)

        pcp = 0
        cfi = 0
        vid = 32
        tci = pcp << 15 | cfi << 12 | vid
        ethertype = ether.ETH_TYPE_IP
        v = vlan(pcp, cfi, vid, ethertype)

        version = 4
        header_length = 20
        tos = 0
        total_length = 24
        identification = 0x8a5d
        flags = 0
        offset = 1480
        ttl = 64
        proto = inet.IPPROTO_ICMP
        csum = 0xa7f2
        src = '131.151.32.21'
        dst = '131.151.32.129'
        option = b'TEST'
        ip = ipv4(version, header_length, tos, total_length, identification,
                  flags, offset, ttl, proto, csum, src, dst, option)

        p = Packet()

        p.add_protocol(e)
        p.add_protocol(self.sv)
        p.add_protocol(v)
        p.add_protocol(ip)
        p.serialize()

        return p
Ejemplo n.º 9
0
class Test_vlan(unittest.TestCase):
    """ Test case for vlan
    """

    pcp = 0
    cfi = 0
    vid = 32
    tci = pcp << 15 | cfi << 12 | vid
    ethertype = ether.ETH_TYPE_IP

    buf = pack(vlan._PACK_STR, tci, ethertype)

    v = vlan(pcp, cfi, vid, ethertype)

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def find_protocol(self, pkt, name):
        for p in pkt.protocols:
            if p.protocol_name == name:
                return p

    def test_init(self):
        eq_(self.pcp, self.v.pcp)
        eq_(self.cfi, self.v.cfi)
        eq_(self.vid, self.v.vid)
        eq_(self.ethertype, self.v.ethertype)

    def test_parser(self):
        res, ptype, _ = self.v.parser(self.buf)

        eq_(res.pcp, self.pcp)
        eq_(res.cfi, self.cfi)
        eq_(res.vid, self.vid)
        eq_(res.ethertype, self.ethertype)
        eq_(ptype, ipv4)

    def test_serialize(self):
        data = bytearray()
        prev = None
        buf = self.v.serialize(data, prev)

        fmt = vlan._PACK_STR
        res = struct.unpack(fmt, buf)

        eq_(res[0], self.tci)
        eq_(res[1], self.ethertype)

    def _build_vlan(self):
        src_mac = '00:07:0d:af:f4:54'
        dst_mac = '00:00:00:00:00:00'
        ethertype = ether.ETH_TYPE_8021Q
        e = ethernet(dst_mac, src_mac, ethertype)

        version = 4
        header_length = 20
        tos = 0
        total_length = 24
        identification = 0x8a5d
        flags = 0
        offset = 1480
        ttl = 64
        proto = inet.IPPROTO_ICMP
        csum = 0xa7f2
        src = '131.151.32.21'
        dst = '131.151.32.129'
        option = b'TEST'
        ip = ipv4(version, header_length, tos, total_length, identification,
                  flags, offset, ttl, proto, csum, src, dst, option)

        p = Packet()

        p.add_protocol(e)
        p.add_protocol(self.v)
        p.add_protocol(ip)
        p.serialize()

        return p

    def test_build_vlan(self):
        p = self._build_vlan()

        e = self.find_protocol(p, "ethernet")
        ok_(e)
        eq_(e.ethertype, ether.ETH_TYPE_8021Q)

        v = self.find_protocol(p, "vlan")
        ok_(v)
        eq_(v.ethertype, ether.ETH_TYPE_IP)

        ip = self.find_protocol(p, "ipv4")
        ok_(ip)

        eq_(v.pcp, self.pcp)
        eq_(v.cfi, self.cfi)
        eq_(v.vid, self.vid)
        eq_(v.ethertype, self.ethertype)

    @raises(Exception)
    def test_malformed_vlan(self):
        m_short_buf = self.buf[1:vlan._MIN_LEN]
        vlan.parser(m_short_buf)

    def test_json(self):
        jsondict = self.v.to_jsondict()
        v = vlan.from_jsondict(jsondict['vlan'])
        eq_(str(self.v), str(v))