Add an option to set source address for a BGP session.

When adding a BGP neighbour, one can set "update-source" (quagga syntax) to the
source IP address that will be used for that session.

Signed-off-by: Benjamin Cama <benoar@dolka.fr>
This commit is contained in:
Benjamin Cama 2011-07-22 02:11:33 +02:00
parent 11ec3c4a24
commit b36141c0c7
5 changed files with 51 additions and 3 deletions

17
bgp.c
View file

@ -95,7 +95,7 @@ int bgp_setup(int as)
/* start connection with a peer */
int bgp_start(struct bgp_peer *peer, char *name, int as, int keepalive,
int hold, int enable)
int hold, struct in_addr update_source, int enable)
{
struct hostent *h;
int ibgp;
@ -124,6 +124,7 @@ int bgp_start(struct bgp_peer *peer, char *name, int as, int keepalive,
}
memcpy(&peer->addr, h->h_addr, sizeof(peer->addr));
peer->source_addr = update_source.s_addr;
peer->as = as > 0 ? as : our_as;
ibgp = peer->as == our_as;
@ -696,6 +697,7 @@ static int bgp_connect(struct bgp_peer *peer)
{
static int bgp_port = 0;
struct sockaddr_in addr;
struct sockaddr_in source_addr;
struct epoll_event ev;
if (!bgp_port)
@ -727,6 +729,19 @@ static int bgp_connect(struct bgp_peer *peer)
/* set to non-blocking */
fcntl(peer->sock, F_SETFL, fcntl(peer->sock, F_GETFL, 0) | O_NONBLOCK);
/* set source address */
memset(&source_addr, 0, sizeof(source_addr));
source_addr.sin_family = AF_INET;
source_addr.sin_addr.s_addr = peer->source_addr; /* defaults to INADDR_ANY */
if (bind(peer->sock, (struct sockaddr *) &source_addr, sizeof(source_addr)) < 0)
{
LOG(1, 0, 0, "Can't set source address to %s: %s\n",
inet_ntoa(source_addr.sin_addr), strerror(errno));
bgp_set_retry(peer);
return 0;
}
/* try connect */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;