def start_protocol_patched(coreservice, socket): """When a neighbor connects that does not exist, quickly add it.""" peer_addr, peer_port = coreservice.get_remotename(socket) peer = coreservice._peer_manager.get_by_addr(peer_addr) if not peer: # Use AS 1 for the peer, it will be fixed in validate_open_msg log.info(f'Adding peer {peer_addr}') s.neighbor_add(peer_addr, 1, is_route_server_client=True) s.in_filter_set(peer_addr, [PrefixFilter('0.0.0.0/0', PrefixFilter.POLICY_DENY)]) start_protocol_orig(coreservice, socket)
def valid_prefix_filter(filter_): policy = filter_.get('policy', None) if policy == 'permit': policy = PrefixFilter.POLICY_PERMIT else: policy = PrefixFilter.POLICY_DENY prefix = filter_['prefix'] ge = filter_.get('ge', None) le = filter_.get('le', None) return PrefixFilter(prefix, policy, ge=ge, le=le)
s.neighbor_add(peer_addr, 1, is_route_server_client=True) s.in_filter_set(peer_addr, [PrefixFilter('0.0.0.0/0', PrefixFilter.POLICY_DENY)]) start_protocol_orig(coreservice, socket) ryu.services.protocols.bgp.core.CoreService.start_protocol = start_protocol_patched validate_open_msg_orig = ryu.services.protocols.bgp.speaker.BgpProtocol._validate_open_msg def validate_open_msg_patched(bgpprotocol, open_msg): """When the OPEN message is received, quickly change the peer AS.""" opt_param_cap_map = open_msg.opt_param_cap_map cap4as = opt_param_cap_map.get(BGP_CAP_FOUR_OCTET_AS_NUMBER, None) if cap4as is None: log.info(f'fixing peer from {bgpprotocol._peer.remote_as} to {open_msg.my_as}') bgpprotocol._peer._neigh_conf._settings['remote_as'] = open_msg.my_as else: log.info(f'fixing peer from {bgpprotocol._peer.remote_as} to {cap4as.as_number}') bgpprotocol._peer._neigh_conf._settings['remote_as'] = cap4as.as_number validate_open_msg_orig(bgpprotocol, open_msg) ryu.services.protocols.bgp.speaker.BgpProtocol._validate_open_msg = validate_open_msg_patched s = ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker(as_number=131072, router_id='10.0.0.3') s.neighbor_add(sys.argv[1], int(sys.argv[2])) s.out_filter_set(sys.argv[1], [PrefixFilter('0.0.0.0/0', PrefixFilter.POLICY_DENY)]) while 1: eventlet.sleep(30)