# Set to true if this peer is directly connected to a dn42 peer
define is_dn42_peer = true;
# If we are connected directly to dn42, we don't want the dn42 routes from others
define import_dn42 = !is_dn42_peer;
define export_dn42 = is_dn42_peer;

template bgp tnet_tpl {
  local    as 4242423538;
  neighbor internal;

  direct;

  password "trygvis";

  ipv6 {
    next hop self;
    import filter {
      if is_tnet() then {
        accept proto, ": (tnet) import accept, net=", net, ", from=", from, ", gw=", gw;
      } else if import_dn42 && dn42_is_valid_network() then {
        accept proto, ": (dn42) import accept, net=", net, ", from=", from, ", gw=", gw;
      } else {
        reject proto, ": import reject, reason=not tnet";
      }
    };
    # newer bird's only
    # import keep filtered;
    export filter {
      if is_tnet() then {
        accept proto, ": (tnet) export accept, net=", net, ", from=", from, ", gw=", gw;
      } else if export_dn42 && dn42_is_valid_network() then {
        accept proto, ": (dn42) import accept, net=", net, ", from=", from, ", gw=", gw;
      } else {
        reject proto, ": export reject, reason=not tnet";
      }
    };
  };
}

protocol bgp tnet_conflatorio from tnet_tpl {
  neighbor fe80:47fc:660:b91f:1063:a6ae:46bb:7588;
  interface "tnet-confltrio";

  rr client;
}

protocol bgp tnet_hash from tnet_tpl {
  neighbor fe80:3b20:4cb0:5315:22a:c7de:a45b:8a7d;
  interface "tnet-hash";

  rr client;
}

protocol bgp tnet_lhn2pi from tnet_tpl {
  neighbor fdb1:4242:3538:ffff:374e:2c7d:319e:e527;
  interface "tnet-lhn2pi";

  rr client;
}

protocol bgp tnet_node1 from tnet_tpl {
  neighbor fe80:58eb:3930:1815:2a6d:8918:70c9:96f3;
  interface "tnet-node1";

  rr client;
}

protocol bgp tnet_node2 from tnet_tpl {
  neighbor fe80:9dd8:abac:cf05:aea3:dc03:4c74:32db;
  interface "tnet-node2";

  rr client;
}

protocol bgp routedbits_lon1 {
  local    as 4242423538;
  neighbor fe80::207;
  neighbor as 4242420207;
  interface "tnet-rtdbts_l1";

  ipv6 {
    import filter {
      if dn42_is_valid_network() && (net !~ tnet) then {
        # Check when unknown or invalid according to ROA
        if (roa_check(dn42_roa, net, bgp_path.last) = ROA_VALID) then {
          accept;
        } else {
          reject proto, "[dn42] ROA check failed for ", net, " ASN ", bgp_path.last;
        }
      } else {
        reject proto, "[dn42] invalid dn42 network ", net, " ASN ", bgp_path.last;
      }
    };
    export filter {
      if net ~ tnet then {
        accept proto, "[dn42] accepting export tnet: ", net;
      } else {
        reject proto, "[dn42] rejecting export: ", net;
      }
    };
  };
}