def handle_leaked_rt_event(): _leaked_rt_evt_handle = cps.event_connect() cps.event_register(_leaked_rt_evt_handle, _leaked_rt_key) while True: leaked_rt_evt = cps.event_wait(_leaked_rt_evt_handle) obj = cps_object.CPSObject(obj=leaked_rt_evt) if obj is None: continue if not 'operation' in leaked_rt_evt.keys(): continue op = None if leaked_rt_evt['operation'] == 'create': op = 'add' if leaked_rt_evt['operation'] == 'delete': op = 'del' src_vrf_name = None af = None prefix = None prefix_len = 0 dst_vrf_name = None try: dst_vrf_name = obj.get_attr_data( 'os-re/os-leak-route-config/vrf-name') af = obj.get_attr_data('os-re/os-leak-route-config/af') prefix = obj.get_attr_data( 'os-re/os-leak-route-config/route-prefix') prefix = binascii.unhexlify(prefix) prefix = get_ip_str(af, prefix) prefix_len = obj.get_attr_data( 'os-re/os-leak-route-config/prefix-len') src_vrf_name = obj.get_attr_data( 'os-re/os-leak-route-config/src-vrf-name') except ValueError as e: log_err( 'Error - Mandatatory object attributes are not present for route leak configuration!' ) continue global _vrf_mutex _vrf_mutex.acquire() ret_val, nexthop_ip, nexthop_ip6 = _veth_ip_get( dst_vrf_name, src_vrf_name, True) _vrf_mutex.release() if ret_val is False: log_err('veth info. not found while handling src-vrf:%s af:%s prefix:%s/%s dst-vrd:%s'\ % (src_vrf_name, str(af), prefix, str(prefix_len), dst_vrf_name)) continue if af == socket.AF_INET6: nexthop_ip = nexthop_ip6 if dn_base_vrf_tool.process_leaked_rt_config(op, af, dst_vrf_name, prefix,\ prefix_len, nexthop_ip) != True: log_err('VRF route leaking configuration failed op:%s src-vrf:%s af:%s prefix:%s/%s dst-vrd:%s'\ % (op, src_vrf_name, str(af), prefix, str(prefix_len), dst_vrf_name)) continue
def config_outgoing_ip_svcs_int(methods, params): obj = cps_object.CPSObject(obj=params['change']) in_param_list = {} log_info('Callback for outgoing IP service configuration') def get_svcs_attr_val(attr_name, dft_val=None): attr_id = outgoing_ip_svcs_attr(attr_name) try: attr_val = obj.get_attr_data(attr_id) except ValueError: attr_val = None if attr_val is None: attr_val = dft_val in_param_list[attr_name] = attr_val return attr_val operation = params['operation'] vrf_name = get_svcs_attr_val('ni-name') af = get_svcs_attr_val('af') public_ip_attr = get_svcs_attr_val('public-ip') outgoing_source_ip_attr = get_svcs_attr_val('outgoing-source-ip') protocol = get_svcs_attr_val('protocol') public_port = get_svcs_attr_val('public-port') private_ip_attr = get_svcs_attr_val('private-ip') private_port = get_svcs_attr_val('private-port') rule_id = get_svcs_attr_val('id') orig_af = af public_ip = None # check public IP if public_ip_attr is not None: af_ip = normalize_ip_with_prefix(orig_af, public_ip_attr) if af_ip is None: log_err('Invalid public IP %s for rule configuration' % public_ip_attr) return False af, public_ip, _ = af_ip if orig_af is None: log_info('Address family %d is deducted from input public IP %s' % (af, public_ip_attr)) in_param_list['af'] = af in_param_list['public-ip'] = public_ip orig_af = af outgoing_source_ip = None # check outgoing source IP if outgoing_source_ip_attr is not None: af_ip = normalize_ip_with_prefix(orig_af, outgoing_source_ip_attr) if af_ip is None: log_err('Invalid outgoing source IP %s for rule configuration' % outgoing_source_ip_attr) return False af, outgoing_source_ip, _ = af_ip if orig_af is None: log_info('Address family %d is deducted from input source IP %s' % (af, outgoing_source_ip_attr)) in_param_list['af'] = af in_param_list['outgoing-source-ip'] = outgoing_source_ip orig_af = af private_ip = None # check private IP if private_ip_attr is not None: af_ip = normalize_ip_with_prefix(orig_af, private_ip_attr) if af_ip is None: log_err('Invalid private IP %s for rule configuration' % private_ip_attr) return False af, private_ip, _ = af_ip if orig_af is None: log_info('Address family %d is deducted from input private IP %s' % (af, private_ip_attr)) in_param_list['af'] = af in_param_list['private-ip'] = private_ip """ The input object stands for Source IP configuration request if: 1. Outgoing source IP attribute is given For all other cases, input object stands for VRF outgoing IP service binding request. """ def is_egress_source_ip_config(): if outgoing_source_ip is not None: return True return False log_info('Input parameters:') for name, val in in_param_list.items(): if val is not None: if name == 'public-ip' or name == 'outgoing-source-ip' or name == 'private-ip': log_info(' %-10s - %s' % (name, binascii.hexlify(val))) else: log_info(' %-10s - %s' % (name, str(val))) if is_egress_source_ip_config(): log_info('Handle Outgoing Source IP configuration, operation: %s' % operation) rule_type = VrfSvcsRuleType.RULE_TYPE_SNAT action = VrfSvcsRuleAction.RULE_ACTION_SNAT reqd_input = [ 'ni-name', 'af', 'public-ip', 'protocol', 'public-port', 'outgoing-source-ip' ] else: log_info( 'Handle Outgoing IP service binding configuration, operation: %s' % operation) rule_type = VrfSvcsRuleType.RULE_TYPE_OUT_IP action = VrfSvcsRuleAction.RULE_ACTION_DNAT reqd_input = ['ni-name', 'af', 'public-ip', 'protocol', 'public-port'] def check_rule_input(missed_attr_list=None): """ The keys to identify a rule """ if operation == 'delete': """ For delete operation, either rule ID or rule keys need to be given to find rule to be deleted """ if rule_id is not None: return True else: if check_attr_list(reqd_input, in_param_list, missed_attr_list): return True elif operation == 'set': """ set operation, not supported on outgoing IP service configuration """ return False elif operation == 'create': """ For create operation, all key attributes need to be given """ if check_attr_list(reqd_input, in_param_list, missed_attr_list): return True return False not_found_list = [] if not check_rule_input(not_found_list): log_err( 'Operation not support or Mandatory attributes %s not found for operation %s' % (str(not_found_list), operation)) return False log_info( 'Outgoing IP service rule: Operation %s VRF %s AF %s%s%s%s%s%s' % (operation, vrf_name, (_af[af] if af is not None else ' '), (' public-ip %s' % (get_ip_str(af, public_ip)) if public_ip is not None else ''), (' protocol %s' % _protocol[protocol] if protocol is not None else ''), (' public-port %d' % public_port if public_port is not None else ''), (' outgoing-source-ip %s' % (get_ip_str(af, outgoing_source_ip)) if outgoing_source_ip is not None else ''), (' ID %d' % rule_id if rule_id is not None else ''))) try: if operation == 'set': return False elif operation == 'create': ret_val, private_ip, private_port = process_vrf_outgoing_svcs_rule_add( rule_type, vrf_name, action, af, dst_ip=public_ip, protocol=protocol, dst_port=public_port, out_src_ip=outgoing_source_ip, private_ip=private_ip, private_port=private_port, rule_id=rule_id) log_info( '%s in adding outgoing %s: VRF %s AF %s%s%s%s%s%s%s%s%s' % (('Success' if ret_val is not None else 'Failure'), ('%s rule' % 'SNAT' if rule_type == VrfSvcsRuleType.RULE_TYPE_SNAT else 'IP'), vrf_name, (_af[af] if af is not None else ' '), (' PROTO %s' % _protocol[protocol] if protocol is not None else ''), (' DST IP %s' % get_ip_str(af, public_ip) if public_ip is not None else ''), (' PORT %d' % public_port if public_port is not None else ''), (' ACTION %s' % _action[action] if action is not None else ''), (' SRC IP %s' % get_ip_str(af, outgoing_source_ip) if outgoing_source_ip is not None else ''), (' PRIVATE IP %s' % get_ip_str(af, private_ip) if private_ip is not None else ''), (' PRIVATE PORT %d' % private_port if private_port is not None else ''), (' ID %d' % ret_val if ret_val is not None else ''))) if ret_val is not None: if rule_type == VrfSvcsRuleType.RULE_TYPE_SNAT: #SNAT flow obj.add_attr(outgoing_ip_svcs_attr('id'), ret_val) params['change'] = obj.get() else: #DNAT - service binding flow private_ip = binascii.hexlify(private_ip) cps_obj = cps_object.CPSObject( module='vrf-firewall/ns-outgoing-service', qual='target', data={ outgoing_ip_svcs_attr('id'): ret_val, outgoing_ip_svcs_attr('ni-name'): vrf_name, outgoing_ip_svcs_attr('af'): af, outgoing_ip_svcs_attr('public-ip'): public_ip_attr, outgoing_ip_svcs_attr('protocol'): protocol, outgoing_ip_svcs_attr('public-port'): public_port, outgoing_ip_svcs_attr('private-ip'): private_ip, outgoing_ip_svcs_attr('private-port'): private_port }) params['change'] = cps_obj.get() return True elif operation == 'delete': if rule_id is None: ret_val = process_vrf_outgoing_svcs_rule_del( rule_type, vrf_name, action, af, dst_ip=public_ip, protocol=protocol, dst_port=public_port, out_src_ip=outgoing_source_ip, private_ip=private_ip, private_port=private_port) else: ret_val = process_vrf_outgoing_svcs_rule_del_by_id(rule_id) if ret_val is True: #deleted successfully return True except Exception as e: log_msg = ('%s %s %s' % ('Failed to commit operation.', e, params)) log_err(log_msg) return False log_err( 'Outgoing IP service rule: Operation %s Failed VRF %s AF %s%s%s%s%s%s' % (operation, vrf_name, (_af[af] if af is not None else ' '), (' public-ip %s' % (get_ip_str(af, public_ip)) if public_ip is not None else ''), (' protocol %s' % _protocol[protocol] if protocol is not None else ''), (' public-port %d' % public_port if public_port is not None else ''), (' outgoing-source-ip %s' % (get_ip_str(af, outgoing_source_ip)) if outgoing_source_ip is not None else ''), (' ID %d' % rule_id if rule_id is not None else ''))) return False
def config_incoming_ip_svcs_int(methods, params): obj = cps_object.CPSObject(obj=params['change']) in_param_list = {} log_info('Callback for incoming IP service configuration') def get_svcs_attr_val(attr_name, dft_val=None): attr_id = incoming_ip_svcs_attr(attr_name) try: attr_val = obj.get_attr_data(attr_id) except ValueError: attr_val = None if attr_val is None: attr_val = dft_val in_param_list[attr_name] = attr_val return attr_val operation = params['operation'] af = get_svcs_attr_val('af') vrf_name = get_svcs_attr_val('ni-name') protocol = get_svcs_attr_val('protocol') dst_port = get_svcs_attr_val('dst-port') low_dst_port = get_svcs_attr_val('lower-dst-port') high_dst_port = get_svcs_attr_val('upper-dst-port') action = get_svcs_attr_val('action') src_ip = get_svcs_attr_val('src-ip') src_prefix_len = get_svcs_attr_val('src-prefix-len') dst_ip = get_svcs_attr_val('dst-ip') dst_prefix_len = get_svcs_attr_val('dst-prefix-len') in_intf = get_svcs_attr_val('ifname') if operation == 'create': # set default seq number to 0 for create seq_num = get_svcs_attr_val('seq-num', 0) else: seq_num = get_svcs_attr_val('seq-num') rule_id = get_svcs_attr_val('id') # check source and destination IP if src_ip is not None: ip_tuple = normalize_ip_with_prefix(af, src_ip, src_prefix_len) if ip_tuple is None: log_err('Failed to normalize source IP with prefix') return False orig_af = af af, src_ip, src_prefix_len = ip_tuple if orig_af is None: log_info('Address family %d is deduced from input source IP %s' % (af, src_ip)) in_param_list['af'] = af in_param_list['src-ip'] = src_ip in_param_list['src-prefix-len'] = src_prefix_len if dst_ip is not None: ip_tuple = normalize_ip_with_prefix(af, dst_ip, dst_prefix_len) if ip_tuple is None: log_err('Failed to normalize destination IP with prefix') return False orig_af = af af, dst_ip, dst_prefix_len = ip_tuple if orig_af is None: log_info( 'Address family %d is deduced from input destination IP %s' % (af, dst_ip)) in_param_list['af'] = af in_param_list['dst-ip'] = dst_ip in_param_list['dst-prefix-len'] = dst_prefix_len # check lower & upper destination ports if low_dst_port is not None and high_dst_port is None: log_err('Missing upper-dst-port for VTY ACL rule configuration') return False if low_dst_port is None and high_dst_port is not None: log_err('Missing lower-dst-port for VTY ACL rule configuration') return False if (low_dst_port is not None or high_dst_port is not None) and dst_port is not None: log_err('Invalid VTY ACL rule configuration, \ dst port attribute cannot be configured along with dst port range attributes' ) return False """ The input object stands for VTY ACL configuration request if: 1. Destination L4 port attribute is not given or given value is 0, 2. Or Source IP address attribute is given, 3. Or lower & upper destination L4 port attributes are given. For all other cases, input object stands for VRF incoming IP service request. """ def is_vty_acl_config(): if dst_port is None or dst_port == 0: return True if src_ip is not None: return True if low_dst_port is not None and high_dst_port is not None: return True return False log_info('Input parameters:') for name, val in in_param_list.items(): if val is not None: if name == 'src-ip' or name == 'dst-ip': log_info(' %-10s - %s' % (name, binascii.hexlify(val))) else: log_info(' %-10s - %s' % (name, str(val))) if is_vty_acl_config(): log_info('Handle VTY ACL configuration, operation: %s' % operation) rule_type = VrfSvcsRuleType.RULE_TYPE_ACL reqd_input = ['ni-name', 'af', 'src-ip', 'src-prefix-len', 'action'] else: log_info('Handle incoming IP configuration, operation: %s' % operation) rule_type = VrfSvcsRuleType.RULE_TYPE_IP reqd_input = ['af', 'ni-name', 'protocol', 'dst-port'] if vrf_name == 'default': reqd_input.append('action') def check_rule_input(missed_attr_list=None): """ The keys to identify a rule """ if operation == 'delete': """ For delete operation, either rule ID or rule keys need to be given to find rule to be deleted """ if rule_id is not None: return True else: if check_attr_list(reqd_input, in_param_list, missed_attr_list): return True elif operation == 'set': """ For set operation, rule ID is the only required input to find the rule to be updated. And the attributes of the rule will be changed to attribute value given by input """ if rule_id is not None: return True else: if missed_attr_list is not None: missed_attr_list.append('id') elif operation == 'create': """ For create operation, all key attributes plus sequence number need to be given by input """ if check_attr_list(reqd_input + ['seq-num'], in_param_list, missed_attr_list): return True return False not_found_list = [] if not check_rule_input(not_found_list): log_err('Mandatory attributes %s not found for operation %s' % (str(not_found_list), operation)) return False if operation == 'create': if rule_type == VrfSvcsRuleType.RULE_TYPE_IP and vrf_name != 'default': """ If it is IP forwarding rule and the name space is not default, we set its action type as DNAT """ dst_ip = dn_base_vrf_tool.get_veth_ip(af, vrf_name) action = VrfSvcsRuleAction.RULE_ACTION_DNAT else: if (dst_port is not None and protocol != VrfSvcsRuleProto.RULE_PROTO_TCP and protocol != VrfSvcsRuleProto.RULE_PROTO_UDP): log_err( 'L4 destination port filter must with protocol type TCP or UDP' ) return False if (low_dst_port is not None and protocol != VrfSvcsRuleProto.RULE_PROTO_TCP and protocol != VrfSvcsRuleProto.RULE_PROTO_UDP): log_err( 'L4 destination port range filter must with protocol type TCP or UDP' ) return False ret_val = process_vrf_svcs_rule_add(rule_type, vrf_name, action, af, src_ip=src_ip, src_prefix_len=src_prefix_len, dst_ip=dst_ip, dst_prefix_len=dst_prefix_len, seq_num=seq_num, protocol=protocol, dst_port=dst_port, low_dst_port=low_dst_port, high_dst_port=high_dst_port, in_intf=in_intf, rule_id=rule_id) if ret_val is None: log_err( 'Failed to add incoming IP rule: VRF %s AF %s%s%s%s%s%s%s%s%s%s%s' % (vrf_name, _af[af], (' IIF %s' % in_intf if in_intf is not None else ' '), (' SRC %s/%d' % (get_ip_str(af, src_ip), src_prefix_len) if src_ip is not None and src_prefix_len is not None else ''), (' DST %s/%d' % (get_ip_str(af, dst_ip), dst_prefix_len) if dst_ip is not None and dst_prefix_len is not None else ''), (' PROTO %s' % _protocol[protocol] if protocol is not None else ''), (' PORT %d' % dst_port if dst_port is not None else ''), (' PORT RANGE %d-%d' % (low_dst_port, high_dst_port) if low_dst_port is not None else ''), (' SEQ %d' % seq_num if seq_num is not None else ''), (' ACTION %s' % _action[action] if action is not None else ''), (' DST %s' % get_ip_str(af, dst_ip) if dst_ip is not None else ''), (' ID %d' % rule_id if rule_id is not None else ''))) return False if rule_id is not None and ret_val != rule_id: log_err('Given rule id %d is not equal to actually used id %d' % (rule_id, ret_val)) obj.add_attr(incoming_ip_svcs_attr('id'), ret_val) params['change'] = obj.get() elif operation == 'set': ret_val = process_vrf_svcs_rule_set(rule_id, src_ip=src_ip, src_prefix_len=src_prefix_len, protocol=protocol, dst_ip=dst_ip, dst_prefix_len=dst_prefix_len, dst_port=dst_port, action=action, low_dst_port=low_dst_port, high_dst_port=high_dst_port, seq_num=seq_num, in_intf=in_intf) if not ret_val: log_err('Failed to update rule with ID %d' % rule_id) return False elif operation == 'delete': if rule_id is None: if rule_type == VrfSvcsRuleType.RULE_TYPE_IP and vrf_name != 'default': """ If it is IP forwarding rule and the name space is not default, we set its action type as DNAT """ dst_ip = dn_base_vrf_tool.get_veth_ip(af, vrf_name) action = VrfSvcsRuleAction.RULE_ACTION_DNAT ret_val = process_vrf_svcs_rule_del(rule_type, vrf_name, action, af, src_ip=src_ip, src_prefix_len=src_prefix_len, dst_ip=dst_ip, dst_prefix_len=dst_prefix_len, protocol=protocol, dst_port=dst_port, low_dst_port=low_dst_port, high_dst_port=high_dst_port, in_intf=in_intf) else: ret_val = process_vrf_svcs_rule_del_by_id(rule_id) if not ret_val: log_err('Failed to delete rule') return False else: log_err('Invalid operation type %s' % operation) return False return True