C4TG1RL5_UN1T3D.DN42/base/bird/bird/config/custom_filters.conf
2022-06-11 00:59:24 +02:00

110 lines
2.9 KiB
Plaintext

# Based on jlu5's custom filters
# https://github.com/jlu5/ansible-dn42
# Adapted for AS4242421411 - C4TG1RL5
#
function lower_pref(int x) {
if (bgp_local_pref > x) then {
bgp_local_pref = bgp_local_pref - x;
} else {
bgp_local_pref = 0;
}
}
function get_region_tag(int region_tag) {
if (region_tag = 44) then {
return 1; # North America - West
} else if (region_tag ~ [42..43]) then {
return 2; # North America - Central/East
} else if (region_tag = 41) then {
return 3; # Europe
} else if (region_tag ~ [51..53]) then {
return 4; # Asia E/SE + Oceania
}
return 0;
}
function prefer_same_region_origin(int base_weight)
int region_tag;
int incoming_tag;
{
region_tag = get_region_tag(DN42_REGION);
incoming_tag = 0;
if ((64511, 41) ~ bgp_community) then {
incoming_tag = get_region_tag(41);
}
else if ((64511, 42) ~ bgp_community) then {
incoming_tag = get_region_tag(42);
}
else if ((64511, 43) ~ bgp_community) then {
incoming_tag = get_region_tag(43);
}
else if ((64511, 44) ~ bgp_community) then {
incoming_tag = get_region_tag(44);
}
else if ((64511, 50) ~ bgp_community) then {
incoming_tag = get_region_tag(50);
}
else if ((64511, 51) ~ bgp_community) then {
incoming_tag = get_region_tag(51);
}
else if ((64511, 52) ~ bgp_community) then {
incoming_tag = get_region_tag(52);
}
else if ((64511, 53) ~ bgp_community) then {
incoming_tag = get_region_tag(53);
}
if (incoming_tag = 0 || incoming_tag = region_tag) then {
# print "Route ", net, " has incoming tag ", incoming_tag, " matching ours ", region_tag;
bgp_local_pref = bgp_local_pref + 50;
}
}
function bgp_import_filter() {
# Reject routes with long path lengths
if (bgp_path.len > 12) then {
reject;
}
bgp_local_pref = bgp_local_pref + 1400;
lower_pref(bgp_path.len * 100);
prefer_same_region_origin(200);
if (source = RTS_BGP && (65535, 666) ~ bgp_community) then {
dest = RTD_BLACKHOLE;
}
};
function ibgp_import_filter() {
if (source != RTS_BGP) then {
reject;
}
if (!is_valid_network() && !is_valid_network_v6()) then {
reject;
}
bgp_local_pref = bgp_local_pref + 1400;
lower_pref(bgp_path.len * 100);
if (bgp_path.len = 0) then {
bgp_local_pref = bgp_local_pref + 2000;
}
accept;
}
# TODO: implement exports with the bgp_med attribute
function ibgp_export_filter() {
if (source != RTS_BGP && !is_ibgp_network()) then {
reject;
}
# Don't export device routes for dn42 anycast networks. These will be filled in via a static route config
# triggered by service start/stop
if (is_ibgp_network() && source = RTS_DEVICE) then {
reject;
}
accept;
}