filters.conf.j2 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. include "community_filters.conf";
  2. include "crxn_filters.conf";
  3. define DNET_BOGON_ASNS = [
  4. 0, # RFC 7607
  5. 23456, # RFC 4893 AS_TRANS
  6. 64496..64511, # RFC 5398 and documentation/example ASNs
  7. 65535, # RFC 7300 Last 16 bit ASN
  8. 65536..65551, # RFC 5398 and documentation/example ASNs
  9. 65552..76099, # RFC IANA reserved ASNs
  10. 76200..131071, # Excluding 76100 To 76199
  11. 4200000000..4201269999, # RFC 6996 Private ASNs
  12. 4201280000..4242419999, # Excluding 4201270000 To 4201279999
  13. 4242430000..4294967294, # Excluding 4242420000 To 4242429999
  14. 4294967295 # RFC 7300 Last 32 bit ASN
  15. ];
  16. define ASN_BLACKLIST = [
  17. {% set comma = joiner(",") %}
  18. {% for asn in dn42.asn_blacklist %}
  19. {{ comma() }}{{ asn }}
  20. {% endfor %}
  21. ];
  22. function strip_too_many_communities() {
  23. if ( ( bgp_community.len + bgp_ext_community.len + bgp_large_community.len ) > 99 ) then {
  24. bgp_community.filter([(64511, *)]);
  25. bgp_ext_community.empty;
  26. bgp_large_community.filter([(DN42AS, *, *), (NEOAS, *, *)]);
  27. }
  28. }
  29. function reject_bogon_asns() {
  30. int set bogon_asns = DNET_BOGON_ASNS;
  31. if ( bgp_path ~ bogon_asns ) then {
  32. print "Reject: bogon AS_PATH ", net, " ", bgp_path;
  33. reject;
  34. }
  35. }
  36. function reject_long_aspaths()
  37. {
  38. if ( bgp_path.len > {{ dn42.very_long_as_path }} ) then {
  39. print "Reject: Too long AS path: ", net, " ", bgp_path;
  40. reject;
  41. }
  42. }
  43. function honor_graceful_shutdown() {
  44. if (65535, 0) ~ bgp_community then {
  45. bgp_local_pref = 0;
  46. }
  47. }
  48. function is_blackholed() {
  49. return ((65535, 666) ~ bgp_community);
  50. }
  51. function reject_blacklisted()
  52. int set blacklist;
  53. {
  54. blacklist = ASN_BLACKLIST;
  55. if ( bgp_path ~ blacklist ) then {
  56. if ( bgp_path !~ [DN42AS, NEOAS] ) then
  57. print "Reject: Found blacklisted ASN ", bgp_path;
  58. reject;
  59. }
  60. }
  61. function delete_invalid_communities() {
  62. bgp_community.delete([(64511, 0)]);
  63. bgp_community.delete([(64511, 71..999)]);
  64. bgp_community.delete([(64511, 2000..65535)]);
  65. }
  66. function dn42_import_filter(int link_latency; int link_bandwidth; int link_crypto) {
  67. if (! is_valid_network()) then {
  68. print "Received invalid network: ", net, " ", bgp_path;
  69. reject;
  70. }
  71. if ( is_self_net() ) then reject;
  72. if net.type = NET_IP4 then {
  73. if (roa_check(dn42_roa, net, bgp_path.last) != ROA_VALID) then {
  74. # print "[dn42] ROA check failed for ", net, " - AS ", bgp_path;
  75. reject;
  76. }
  77. } else if net.type = NET_IP6 then {
  78. if (roa_check(dn42_roa_v6, net, bgp_path.last) != ROA_VALID) then {
  79. # print "[dn42] ROA check failed for ", net, " - AS ", bgp_path;
  80. reject;
  81. }
  82. } else {
  83. reject;
  84. }
  85. reject_long_aspaths();
  86. reject_blacklisted();
  87. reject_bogon_asns();
  88. strip_too_many_communities();
  89. delete_invalid_communities();
  90. update_flags(link_latency, link_bandwidth, link_crypto);
  91. unset(bgp_otc);
  92. if (bgp_path.len = 1) then
  93. bgp_local_pref = bgp_local_pref + 700;
  94. else if ( bgp_path.len > {{ dn42.long_as_path }} ) then
  95. bgp_local_pref = bgp_local_pref - 20;
  96. honor_graceful_shutdown();
  97. # Avoid netid hjacking
  98. del_netid();
  99. if ( is_neonetwork() ) then
  100. add_netid(4);
  101. else
  102. add_netid(1);
  103. accept;
  104. }
  105. function dn42_export_filter(int link_latency; int link_bandwidth; int link_crypto) {
  106. if ( ! is_valid_network() ) then reject;
  107. if ((has_netid(1) || has_netid(4) || has_netid(2)) || is_notsub_self_net() || is_blackholed()) then {
  108. update_geo_flags();
  109. update_flags(link_latency, link_bandwidth, link_crypto);
  110. if (has_netid(2) && is_crxn_net()) then
  111. crxn_bgp_include();
  112. del_netid();
  113. bgp_med = 0;
  114. bgp_med = bgp_med + ( ( 4 - ( link_crypto - 30 ) ) * 600 );
  115. bgp_med = bgp_med + ( ( 9 - ( link_bandwidth - 20 ) ) * 100);
  116. bgp_med = bgp_med + ( ( link_latency - 1) * 300);
  117. accept;
  118. }
  119. reject;
  120. }
  121. function dn42_ibgp_import(int link_latency; int link_bandwidth; int link_crypto; int link_connection) {
  122. if (! is_self_net() ) then {
  123. update_flags(link_latency, link_bandwidth, link_crypto);
  124. accept;
  125. }
  126. reject;
  127. }
  128. function dn42_ibgp_export(int link_latency; int link_bandwidth; int link_crypto; int link_connection) {
  129. if ( has_netid(2) ) then reject;
  130. if (! is_self_net() ) then {
  131. update_flags(link_latency, link_bandwidth, link_crypto);
  132. if (bgp_path.len = 1 && link_connection !~ [1, 2]) then {
  133. bgp_local_pref = bgp_local_pref - 200;
  134. }
  135. accept;
  136. }
  137. reject;
  138. }
  139. function collector_export_filter() {
  140. if ( ! is_valid_network() ) then reject;
  141. if ( (source ~ [RTS_STATIC, RTS_BABEL, RTS_BGP] && (has_netid(1) || has_netid(4) || has_netid(2))) || is_notsub_self_net()) then {
  142. update_geo_flags();
  143. # "||" is to the fact that babel does not transfer netids
  144. # maybe it need a bit reworking... transfering igp and crxn
  145. # over one babel is not the best idea ever...
  146. if (has_netid(2) || is_crxn_net()) then
  147. crxn_bgp_include();
  148. del_netid();
  149. accept;
  150. }
  151. reject;
  152. }