0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00
bird/proto/rpki/transport.c
Pavel Tvrdík 65d2a88dd2 RPKI protocol with one cache server per protocol
The RPKI protocol (RFC 6810) using the RTRLib
(http://rpki.realmv6.org/) that is integrated inside
the BIRD's code.

Implemeted transports are:
 - unprotected transport over TCP
 - secure transport over SSHv2

Example configuration of bird.conf:
  ...
  roa4 table r4;
  roa6 table r6;

  protocol rpki {
    debug all;

    # Import both IPv4 and IPv6 ROAs
    roa4 { table r4; };
    roa6 { table r6; };

    # Set cache server (validator) address,
    # overwrite default port 323
    remote "rpki-validator.realmv6.org" port 8282;

    # Overwrite default time intervals
    retry   10;         # Default 600 seconds
    refresh 60;         # Default 3600 seconds
    expire 600;         # Default 7200 seconds
  }

  protocol rpki {
    debug all;

    # Import only IPv4 routes
    roa4 { table r4; };

    # Set cache server address to localhost,
    # use default ports tcp => 323 or ssh => 22
    remote 127.0.0.1;

    # Use SSH transport instead of unprotected transport over TCP
    ssh encryption {
      bird private key "/home/birdgeek/.ssh/id_rsa";
      remote public key "/home/birdgeek/.ssh/known_hosts";
      user "birdgeek";
    };
  }
  ...
2016-12-07 09:35:24 +01:00

136 lines
3.1 KiB
C

/*
* BIRD -- The Resource Public Key Infrastructure (RPKI) to Router Protocol
*
* (c) 2015 CZ.NIC
* (c) 2015 Pavel Tvrdik <pawel.tvrdik@gmail.com>
*
* This file was a part of RTRlib: http://rpki.realmv6.org/
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <sys/socket.h>
#include <netdb.h>
#include "rpki.h"
#include "transport.h"
#include "sysdep/unix/unix.h"
/**
* rpki_hostname_autoresolv - auto-resolve an IP address from a hostname
* @host: domain name of host, e.g. "rpki-validator.realmv6.org"
*
* This function resolves an IP address from a hostname.
* Returns &ip_addr structure with IP address or |IPA_NONE|.
*/
static ip_addr
rpki_hostname_autoresolv(const char *host)
{
ip_addr addr = {};
struct addrinfo *res;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_ADDRCONFIG,
};
if (!host)
return IPA_NONE;
int err_code = getaddrinfo(host, NULL, &hints, &res);
if (err_code != 0)
{
log(L_DEBUG "getaddrinfo failed: %s", gai_strerror(err_code));
return IPA_NONE;
}
sockaddr sa = {
.sa = *res->ai_addr,
};
uint unused;
sockaddr_read(&sa, res->ai_family, &addr, NULL, &unused);
freeaddrinfo(res);
return addr;
}
/**
* rpki_tr_open - prepare and open a socket connection
* @tr: initialized transport socket
*
* Prepare and open a socket connection specified by @tr that must be initialized before.
* This function ends with a calling the sk_open() function.
* Returns RPKI_TR_SUCCESS or RPKI_TR_ERROR.
*/
int
rpki_tr_open(struct rpki_tr_sock *tr)
{
struct rpki_cache *cache = tr->cache;
struct rpki_config *cf = (void *) cache->p->p.cf;
ASSERT(tr->sk == NULL);
tr->sk = sk_new(cache->pool);
sock *sk = tr->sk;
/* sk->type -1 is invalid value, a correct value MUST be set in the specific transport layer in open_fp() hook */
sk->type = -1;
sk->tx_hook = rpki_connected_hook;
sk->err_hook = rpki_err_hook;
sk->data = cache;
sk->daddr = cf->ip;
sk->dport = cf->port;
sk->host = cf->hostname;
sk->rbsize = RPKI_RX_BUFFER_SIZE;
sk->tbsize = RPKI_TX_BUFFER_SIZE;
sk->tos = IP_PREC_INTERNET_CONTROL;
if (ipa_zero2(sk->daddr) && sk->host)
{
sk->daddr = rpki_hostname_autoresolv(sk->host);
if (ipa_zero(sk->daddr))
{
CACHE_TRACE(D_EVENTS, cache, "Cannot resolve the hostname '%s'", sk->host);
return RPKI_TR_ERROR;
}
}
return tr->open_fp(tr);
}
/**
* rpki_tr_close - close socket and prepare it for possible next open
* @tr: successfully opened transport socket
*
* Close socket and free resources.
*/
void
rpki_tr_close(struct rpki_tr_sock *tr)
{
if (tr->ident)
{
mb_free((char *) tr->ident);
tr->ident = NULL;
}
if (tr->sk)
{
rfree(tr->sk);
tr->sk = NULL;
}
}
/**
* rpki_tr_ident - Returns a string identifier for the rpki transport socket
* @tr: successfully opened transport socket
*
* Returns a \0 terminated string identifier for the socket endpoint, e.g. "<host>:<port>".
* Memory is allocated inside @tr structure.
*/
inline const char *
rpki_tr_ident(struct rpki_tr_sock *tr)
{
return tr->ident_fp(tr);
}