Ejemplo n.º 1
0
def process_packet(ethernet):
    """Navigates through the Ethernet payload looking for the
    TraceMsg(). TraceMsg is the payload after all protocols of
    the TCP/IP stack.

    Args:
        ethernet: ethernet frame

    Returns:
        TraceMsg in the binary format
    """
    offset = 0

    trace_msg = ethernet.data.value

    if ethernet.ether_type == constants.IPV4:
        ip_pkt = IPv4()
        ip_pkt.unpack(ethernet.data.value, offset)
        offset += ip_pkt.length
        trace_msg = ip_pkt.data

        if ip_pkt.protocol == constants.TCP:
            # TCP and UDP are not yet supported
            # transport = TCP()
            # transport.parse(ethernet.data.value, offset)
            # offset += transport.length
            # trace_msg = transport.data
            trace_msg = ip_pkt.data

        if ip_pkt.protocol == constants.UDP:
            # transport = UDP()
            # transport.parse(ethernet.data.value, offset)
            # offset += transport.length
            # trace_msg = transport.data
            trace_msg = ip_pkt.data

    return trace_msg
Ejemplo n.º 2
0
    def packet_in(self, event):
        self.lock.acquire()

        from pyof.v0x04.asynchronous.packet_in import PacketIn
        msg = event.content['message']
        assert isinstance(msg, PacketIn)
        eth = Ethernet()
        eth.unpack(msg.data.value)
        #log.info('ethernet type=%s'%str(eth.ether_type))
        switch = event.source.switch
        in_port = msg.in_port
        # if eth.ether_type != 35020:
        #     log.info('ethernet type=%s'%str(eth.ether_type))
        if eth.ether_type == EtherType.IPV4:
            ipv4 = IPv4()
            ipv4.unpack(eth.data.value)

            if ipv4.destination == '10.0.0.254':
                pass
                #only for host locate
                # role = HOST_ROLE[ipv4.source]
                # self.nodes[ipv4.source] = {'role': role}
                #
                # h_id = ipv4.source
                # s_id = str(switch.dpid) + ":" + str(in_port)
                #
                # h2s_id = h_id + "+" + s_id
                # s2h_id = s_id + "+" + h_id
                # self.edges[h2s_id] = {'src': h_id, 'dst': s_id}
                # self.edges[s2h_id] = {'src': s_id, 'dst': h_id}
                # #TODO: need to handle host mobility
                # print(self.nodes)
                # print(self.edges)
                # if not self.debug:
                #     pass # self.trident.set_topology(self.nodes, self.edges)
            else:
                sip = ipv4.source
                dip = ipv4.destination
                ipproto = ipv4.protocol
                sport = None
                dport = None
                # allow_ip = ["10.0.0.1","10.0.0.2","10.0.0.3"]
                # if sip not in allow_ip:
                #     return
                # if dip not in allow_ip:
                #     return
                allow_pair = [("10.0.0.2", "10.0.0.3"),
                              ("10.0.0.2", "10.0.0.1")]
                if (sip, dip) not in allow_pair:
                    self.lock.release()
                    return
                if ipproto == 6:
                    ipproto = "tcp"
                elif ipproto == 17:
                    ipproto = "udp"
                else:
                    ipproto = "unknown"

                if ipproto == "tcp":
                    tcph = unpack('!HHLLBBHHH', eth.data.value[20:40])
                    sport = tcph[0]
                    dport = tcph[1]
                elif ipproto == "udp":
                    udph = unpack('!HHHH', eth.data.value[20:28])
                    sport = udph[0]
                    dport = udph[1]
                else:
                    self.lock.release()
                    return

                log.info("sip:" + str(sip) + "dip:" + str(dip) + str(ipproto) +
                         "sport" + str(sport) + "dport" + str(dport))
                pkt = TridentPacket(sip, dip, sport, dport, ipproto)
                if not self.debug:
                    log.info('start to call trident new_pkt')
                    self.trident.new_pkt(pkt)
        self.lock.release()
Ejemplo n.º 3
0
def generate_trace_pkt(entries, color, r_id, my_domain):
    """ Receives the REST/PUT to generate a PacketOut
    data needs to be serialized.

    Args:
        entries:
        color: result from Coloring Napp for a specific DPID
        r_id:
        my_domain:

    Returns:
        in_port:
        pkt:
    """

    trace = {}
    switch = {}
    eth = {}
    ip = {}
    tp = {}

    # TODO Validate for dl_vlan. If empty, return error.

    # Default values
    dpid, in_port = 0, 65532
    if color['color_field'] == 'dl_src':
        dl_src = color['color_value']
    else:
        dl_src = '0e:55:05:0e:55:05'
    dl_dst = "ca:fe:ca:fe:ca:fe"
    dl_vlan, dl_type, dl_vlan_pcp = 100, 2048, 0
    nw_src, nw_dst, nw_tos = '127.0.0.1', '127.0.0.1', 0
    tp_src, tp_dst = 1, 1

    try:
        trace = entries['trace']
        switch = trace['switch']
        eth = trace['eth']
    except:
        pass

    try:
        ip = trace['ip']
    except:
        pass

    try:
        tp = trace['tp']
    except:
        pass

    if len(switch) > 0:
        dpid, in_port = prepare_switch(switch, dpid, in_port)

    if len(eth) > 0:
        dl_src, dl_dst, dl_vlan, dl_type, dl_vlan_pcp = prepare_ethernet(eth, dl_src, dl_dst,
                                                                         dl_vlan, dl_type,
                                                                         dl_vlan_pcp)
    # if len(ip) > 0:
    nw_src, nw_dst, nw_tos = prepare_ip(ip, nw_src, nw_dst, nw_tos)

    # if len(tp) > 0:
    tp_src, tp_dst = prepare_tp(tp, tp_src, tp_dst)

    kethernet = Ethernet()
    kethernet.ether_type = constants.VLAN
    kethernet.source = dl_src
    kethernet.destination = dl_dst

    kvlan = VLAN(vid=int(dl_vlan), pcp=dl_vlan_pcp, ether_type=int(dl_type))

    msg = TraceMsg(r_id, my_domain)

    if int(dl_type) == constants.IPv4:

        kip = IPv4()
        kip.destination = nw_dst
        kip.source = nw_src
        # ip.protocol = 6
        kip.data = dill.dumps(msg)
        kvlan.data = kip.pack()
    else:
        kvlan.data = dill.dumps(msg)

    kethernet.data = kvlan.pack()
    pkt = kethernet.pack()
    return in_port, pkt