Ejemplo n.º 1
0
Archivo: bmp.py Proyecto: jumeng/yabmp
 def parse_peer_up_notification(msg, peer_flag):
     """
     The Peer Up message is used to indicate that a peering session has
     come up (i.e., has transitioned into ESTABLISHED state). Following
     the common BMP header and per-peer header is the following:
     :param msg:
     :param peer_flag: see parse_per_peer_header
     :return:
     """
     # 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
     # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     # |                 Local Address (16 bytes)                      |
     # ~                                                               ~
     # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     # |          Local Port           |           Remote Port         |
     # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     # |                    Sent OPEN Message                         #|
     # ~                                                               ~
     # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     # |                  Received OPEN Message                        |
     # ~                                                               ~
     # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     LOG.info('decode peer up notification')
     ip_value = int(binascii.b2a_hex(msg[0:16]), 16)
     if int(peer_flag['V']):
         # ipv6 address
         ip_address = str(netaddr.IPAddress(ip_value, version=6))
     else:
         ip_address = str(netaddr.IPAddress(ip_value, version=4))
     LOG.info('local address: %s' % ip_address)
     local_port = int(binascii.b2a_hex(msg[16:18]), 16)
     LOG.info('local port: %s' % local_port)
     remote_port = int(binascii.b2a_hex(msg[18:20]), 16)
     LOG.info('remote port: %s' % remote_port)
     # decode sent and received open message
     open_msg_data = msg[20:]
     length = struct.unpack('!H', open_msg_data[16:18])[0]
     sent_open_msg = Open().parse(open_msg_data[bgp_cons.HDR_LEN:length])
     open_msg_data = open_msg_data[length:]
     received_open_msg = Open().parse(open_msg_data[bgp_cons.HDR_LEN:])
     LOG.info('sent open: %s' % sent_open_msg)
     LOG.info('received open: %s' % received_open_msg)
     return {
         'local_address': ip_address,
         'local_port': local_port,
         'remote_port': remote_port,
         'sent_open_msg': sent_open_msg,
         'received_open_msg': received_open_msg
     }
Ejemplo n.º 2
0
    def send_open(self):
        """
        send open message

        :return:
        """
        # construct Open message
        self.capability_negotiate()
        open_msg = Open(
            version=bgp_cons.VERSION, asn=self.factory.my_asn, hold_time=self.fsm.hold_time,
            bgp_id=self.factory.bgp_id). \
            construct(cfg.CONF.bgp.running_config['capability']['local'])
        if 'add_path' in cfg.CONF.bgp.running_config['capability']['local']:
            # check add path feature, send add path condition:
            # local support send or both
            # remote support receive or both
            if cfg.CONF.bgp.running_config['capability']['local']['add_path'] in \
                    ['ipv4_send', 'ipv4_both']:
                if cfg.CONF.bgp.running_config['capability']['remote'].get('add_path') in \
                        ['ipv4_receive', 'ipv4_both']:
                    self.add_path_ipv4_send = True
        # send message
        self.transport.write(open_msg)
        self.msg_sent_stat['Opens'] += 1
        LOG.info("[%s]Send a BGP Open message to the peer.",
                 self.factory.peer_addr)
        LOG.info("[%s]Probe's Capabilities:", self.factory.peer_addr)
        for key in cfg.CONF.bgp.running_config['capability']['local']:
            LOG.info("--%s = %s", key,
                     cfg.CONF.bgp.running_config['capability']['local'][key])
Ejemplo n.º 3
0
    def open_received(self, timestamp, msg):
        """
        porcess open message

        :param timestamp: timestamp that received this message
        :param msg: binary raw message data
        :return:
        """

        self.msg_recv_stat['Opens'] += 1
        open_msg = Open()
        parse_result = open_msg.parse(msg)
        if self.fsm.bgp_peering.peer_asn != open_msg.asn:
            raise excep.OpenMessageError(
                sub_error=bgp_cons.ERR_MSG_OPEN_BAD_PEER_AS)

        # Open message Capabilities negotiation
        cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability'][
            'remote'] = open_msg.capa_dict
        LOG.info("[%s]A BGP Open message was received", self.factory.peer_addr)
        LOG.info('--version = %s', open_msg.version)
        LOG.info('--ASN = %s', open_msg.asn)
        LOG.info('--hold time = %s', open_msg.hold_time)
        LOG.info('--id = %s', open_msg.bgp_id)
        LOG.info("[%s]Neighbor's Capabilities:", self.factory.peer_addr)
        for key in cfg.CONF.bgp.running_config[
                self.factory.peer_addr]['capability']['remote']:
            if key == 'four_bytes_as':
                self.fourbytesas = True
            elif key == 'add_path':
                if cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability']['remote']['add_path'] in \
                        ['ipv4_send', 'ipv4_both']:
                    if cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability']['local']['add_path'] in \
                            ['ipv4_receive', 'ipv4_both']:
                        self.add_path_ipv4_receive = True

            LOG.info(
                "--%s = %s", key, cfg.CONF.bgp.running_config[
                    self.factory.peer_addr]['capability']['remote'][key])

        # write bgp message
        self.factory.write_msg(timestamp=timestamp,
                               msg_type=1,
                               msg=parse_result,
                               flush=True)
        self.peer_id = open_msg.bgp_id
        self.bgp_peering.set_peer_id(open_msg.bgp_id)

        self.negotiate_hold_time(open_msg.hold_time)
        self.fsm.open_received()
Ejemplo n.º 4
0
    def send_open(self):
        """
        send open message

        :return:
        """
        # construct Open message
        self.capability_negotiate()
        open_msg = Open(version=bgp_cons.VERSION, asn=self.factory.my_asn, hold_time=self.fsm.hold_time,
                        bgp_id=self.factory.bgp_id). \
            construct(cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability']['local'])
        # send message
        self.transport.write(open_msg)
        self.msg_sent_stat['Opens'] += 1
        LOG.info("[%s]Send a BGP Open message to the peer.", self.factory.peer_addr)
        LOG.info("[%s]Probe's Capabilities:", self.factory.peer_addr)
        for key in cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability']['local']:
            LOG.info("--%s = %s", key, cfg.CONF.bgp.running_config[self.factory.peer_addr]['capability']['local'][key])
Ejemplo n.º 5
0
 def setUp(self):
     self.open = Open()
     self.maxDiff = None
Ejemplo n.º 6
0
Archivo: bmp.py Proyecto: jumeng/yabmp
    def parse_route_mirroring_msg(msg):
        """
            Route Mirroring messages are used for verbatim duplication of
        messages as received. Following the common BMP header and per-peer
        header is a set of TLVs that contain information about a message
        or set of messages.
        :param msg:
        :return:
        """
        LOG.debug('decode route mirroring message')

        msg_dict = {}
        open_l = []
        update = []
        notification = []
        route_refresh = []
        while msg:
            mirror_type, length = struct.unpack('!HH', msg[0:4])
            mirror_value = msg[4:4 + length]
            msg = msg[4 + length:]
            if mirror_type == 0:
                # BGP message type
                bgp_msg_type = struct.unpack('!B', mirror_value[18])[0]
                LOG.debug('bgp message type=%s' % bgp_msg_type)
                bgp_msg_body = mirror_value[bgp_cons.HDR_LEN:]
                if bgp_msg_type == 2:
                    # Update message
                    bgp_update_msg = Update().parse(None,
                                                    bgp_msg_body,
                                                    asn4=True)
                    if bgp_update_msg['sub_error']:
                        LOG.error(
                            'error: decode update message error!, error code: %s'
                            % bgp_update_msg['sub_error'])
                        LOG.error('Raw data: %s' % repr(bgp_update_msg['hex']))
                    else:
                        update.append(bgp_update_msg)
                elif bgp_msg_type == 5:
                    # Route Refresh message
                    bgp_route_refresh_msg = RouteRefresh().parse(
                        msg=bgp_msg_body)
                    LOG.debug(
                        'bgp route refresh message: afi=%s,res=%s,safi=%s' %
                        (bgp_route_refresh_msg[0], bgp_route_refresh_msg[1],
                         bgp_route_refresh_msg[2]))
                    route_refresh.append(bgp_route_refresh_msg)
                elif bgp_msg_type == 1:
                    # Open message
                    open_msg = Open().parse(bgp_msg_body)
                    open_l.append(open_msg)
                elif bgp_msg_type == 3:
                    # Notification message
                    notification_msg = Notification().parse(bgp_msg_body)
                    notification.append(notification_msg)
            elif mirror_type == 1:
                # Information type.
                # Amount of this TLV is not specified but we can assume
                # only one per mirroring message is present.
                info_code_type = struct.unpack('!H', mirror_value)[0]
                msg_dict['1'] = info_code_type
            else:
                msg_dict[mirror_type] = binascii.unhexlify(
                    binascii.hexlify(mirror_value))
                LOG.info('unknow mirroring type, type = %s' % mirror_type)

        msg_dict['0'] = {
            'update': update,
            'route_refresh': route_refresh,
            'open': open_l,
            'notification': notification
        }
        return msg_dict