2013-08-02 11:29:56 +04:00
|
|
|
/* $NetBSD: ldp_peer.c,v 1.16 2013/08/02 07:29:56 kefren Exp $ */
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/*
|
2010-12-08 10:20:14 +03:00
|
|
|
* Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Mihai Chelaru <kefren@NetBSD.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2010-12-30 14:29:21 +03:00
|
|
|
#include <netinet/tcp.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <netmpls/mpls.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2013-07-20 09:16:08 +04:00
|
|
|
#include <fcntl.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
2013-08-02 11:29:56 +04:00
|
|
|
#include <stddef.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2010-12-30 14:29:21 +03:00
|
|
|
|
|
|
|
#include "conffile.h"
|
2010-12-08 10:20:14 +03:00
|
|
|
#include "socketops.h"
|
|
|
|
#include "ldp_errors.h"
|
|
|
|
#include "ldp.h"
|
|
|
|
#include "tlv_stack.h"
|
|
|
|
#include "mpls_interface.h"
|
|
|
|
#include "notifications.h"
|
|
|
|
#include "ldp_peer.h"
|
|
|
|
|
2010-12-30 14:29:21 +03:00
|
|
|
extern int ldp_holddown_time;
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
static struct label_mapping *ldp_peer_get_lm(struct ldp_peer *,
|
|
|
|
const struct sockaddr *, uint);
|
|
|
|
|
|
|
|
static int mappings_compare(void *, const void *, const void *);
|
|
|
|
static rb_tree_ops_t mappings_tree_ops = {
|
|
|
|
.rbto_compare_nodes = mappings_compare,
|
|
|
|
.rbto_compare_key = mappings_compare,
|
|
|
|
.rbto_node_offset = offsetof(struct label_mapping, mappings_node),
|
|
|
|
.rbto_context = NULL
|
|
|
|
};
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
void
|
|
|
|
ldp_peer_init(void)
|
|
|
|
{
|
|
|
|
SLIST_INIT(&ldp_peer_head);
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:14:31 +04:00
|
|
|
int
|
2013-01-26 21:29:55 +04:00
|
|
|
sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
|
|
|
|
{
|
2013-02-05 00:28:24 +04:00
|
|
|
if (a == NULL || b == NULL || a->sa_len != b->sa_len ||
|
|
|
|
a->sa_family != b->sa_family)
|
2013-01-26 21:29:55 +04:00
|
|
|
return -1;
|
2013-02-04 13:52:43 +04:00
|
|
|
return memcmp(a, b, a->sa_len);
|
2013-01-26 21:29:55 +04:00
|
|
|
}
|
2013-08-02 11:29:56 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
mappings_compare(void *context, const void *node1, const void *node2)
|
|
|
|
{
|
|
|
|
const struct label_mapping *l1 = node1, *l2 = node2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (__predict_false(l1->address.sa.sa_family !=
|
|
|
|
l2->address.sa.sa_family))
|
|
|
|
return l1->address.sa.sa_family > l2->address.sa.sa_family ?
|
|
|
|
1 : -1;
|
|
|
|
|
|
|
|
assert(l1->address.sa.sa_len == l2->address.sa.sa_len);
|
|
|
|
if ((ret = memcmp(&l1->address.sa, &l2->address.sa, l1->address.sa.sa_len)) != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (__predict_false(l1->prefix != l2->prefix))
|
|
|
|
return l1->prefix > l2->prefix ? 1 : -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/*
|
|
|
|
* soc should be > 1 if there is already a TCP socket for this else we'll
|
|
|
|
* initiate a new one
|
|
|
|
*/
|
|
|
|
struct ldp_peer *
|
2013-07-11 09:45:23 +04:00
|
|
|
ldp_peer_new(const struct in_addr * ldp_id, const struct sockaddr * padd,
|
|
|
|
const struct sockaddr * tradd, uint16_t holdtime, int soc)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer *p;
|
2013-07-20 09:16:08 +04:00
|
|
|
int s = soc, sopts;
|
|
|
|
union sockunion connecting_su;
|
2010-12-30 14:29:21 +03:00
|
|
|
struct conf_neighbour *cn;
|
2013-01-27 09:53:21 +04:00
|
|
|
|
2013-07-20 09:16:08 +04:00
|
|
|
assert(tradd == NULL || tradd->sa_family == padd->sa_family);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
if (soc < 1) {
|
2010-12-08 10:20:14 +03:00
|
|
|
s = socket(PF_INET, SOCK_STREAM, 0);
|
2013-01-26 21:29:55 +04:00
|
|
|
if (s < 0) {
|
|
|
|
fatalp("ldp_peer_new: cannot create socket\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-11 09:45:23 +04:00
|
|
|
if (tradd != NULL) {
|
2013-07-20 09:16:08 +04:00
|
|
|
assert(tradd->sa_len <= sizeof(connecting_su));
|
|
|
|
memcpy(&connecting_su, tradd, tradd->sa_len);
|
2013-07-11 09:45:23 +04:00
|
|
|
} else {
|
2013-07-20 09:16:08 +04:00
|
|
|
assert(padd->sa_len <= sizeof(connecting_su));
|
|
|
|
memcpy(&connecting_su, padd, padd->sa_len);
|
2013-07-11 09:45:23 +04:00
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
|
2013-07-20 09:16:08 +04:00
|
|
|
assert(connecting_su.sa.sa_family == AF_INET ||
|
|
|
|
connecting_su.sa.sa_family == AF_INET6);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-07-20 09:16:08 +04:00
|
|
|
if (connecting_su.sa.sa_family == AF_INET)
|
|
|
|
connecting_su.sin.sin_port = htons(LDP_PORT);
|
2010-12-08 10:20:14 +03:00
|
|
|
else
|
2013-07-20 09:16:08 +04:00
|
|
|
connecting_su.sin6.sin6_port = htons(LDP_PORT);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
set_ttl(s);
|
|
|
|
}
|
|
|
|
|
2010-12-30 14:29:21 +03:00
|
|
|
/* MD5 authentication needed ? */
|
|
|
|
SLIST_FOREACH(cn, &conei_head, neilist)
|
2013-01-27 09:53:21 +04:00
|
|
|
if (cn->authenticate != 0 &&
|
|
|
|
ldp_id->s_addr == cn->address.s_addr) {
|
2010-12-30 14:29:21 +03:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG, &(int){1},
|
|
|
|
sizeof(int)) != 0)
|
|
|
|
fatalp("setsockopt TCP_MD5SIG: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2013-01-27 09:53:21 +04:00
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* Set the peer in CONNECTING/CONNECTED state */
|
2010-12-09 03:10:59 +03:00
|
|
|
p = calloc(1, sizeof(*p));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (!p) {
|
2010-12-30 14:29:21 +03:00
|
|
|
fatalp("ldp_peer_new: calloc problem\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return NULL;
|
2010-12-09 03:10:59 +03:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
SLIST_INSERT_HEAD(&ldp_peer_head, p, peers);
|
2013-01-26 21:29:55 +04:00
|
|
|
p->address = (struct sockaddr *)malloc(padd->sa_len);
|
|
|
|
memcpy(p->address, padd, padd->sa_len);
|
2010-12-08 10:20:14 +03:00
|
|
|
memcpy(&p->ldp_id, ldp_id, sizeof(struct in_addr));
|
2013-01-29 01:08:14 +04:00
|
|
|
if (tradd != NULL) {
|
|
|
|
p->transport_address = (struct sockaddr *)malloc(tradd->sa_len);
|
2013-01-26 21:29:55 +04:00
|
|
|
memcpy(p->transport_address, tradd, tradd->sa_len);
|
2013-01-29 01:08:14 +04:00
|
|
|
} else {
|
|
|
|
p->transport_address = (struct sockaddr *)malloc(padd->sa_len);
|
2013-01-26 21:29:55 +04:00
|
|
|
memcpy(p->transport_address, padd, padd->sa_len);
|
2013-01-29 01:08:14 +04:00
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
p->holdtime=holdtime > ldp_holddown_time ? holdtime : ldp_holddown_time;
|
2010-12-08 10:20:14 +03:00
|
|
|
p->socket = s;
|
|
|
|
if (soc < 1) {
|
|
|
|
p->state = LDP_PEER_CONNECTING;
|
|
|
|
p->master = 1;
|
|
|
|
} else {
|
|
|
|
p->state = LDP_PEER_CONNECTED;
|
|
|
|
p->master = 0;
|
|
|
|
set_ttl(p->socket);
|
|
|
|
}
|
|
|
|
SLIST_INIT(&p->ldp_peer_address_head);
|
2013-08-02 11:29:56 +04:00
|
|
|
rb_tree_init(&p->label_mapping_tree, &mappings_tree_ops);
|
2010-12-08 10:20:14 +03:00
|
|
|
p->timeout = p->holdtime;
|
|
|
|
|
2013-07-20 09:16:08 +04:00
|
|
|
sopts = fcntl(p->socket, F_GETFL);
|
|
|
|
if (sopts >= 0) {
|
|
|
|
sopts |= O_NONBLOCK;
|
|
|
|
fcntl(p->socket, F_SETFL, &sopts);
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* And connect to peer */
|
2013-07-20 09:16:08 +04:00
|
|
|
if (soc < 1 &&
|
|
|
|
connect(s, &connecting_su.sa, connecting_su.sa.sa_len) == -1) {
|
|
|
|
if (errno == EINTR || errno == EINPROGRESS)
|
|
|
|
/* We take care of this in big_loop */
|
|
|
|
return p;
|
|
|
|
warnp("connect to %s failed: %s\n",
|
|
|
|
satos(&connecting_su.sa), strerror(errno));
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
p->state = LDP_PEER_CONNECTED;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldp_peer_holddown(struct ldp_peer * p)
|
|
|
|
{
|
2013-07-20 09:16:08 +04:00
|
|
|
|
|
|
|
if (!p || p->state == LDP_PEER_HOLDDOWN)
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
2013-07-20 09:16:08 +04:00
|
|
|
if (p->state == LDP_PEER_ESTABLISHED) {
|
|
|
|
p->state = LDP_PEER_HOLDDOWN;
|
2010-12-08 10:20:14 +03:00
|
|
|
mpls_delete_ldp_peer(p);
|
2013-07-20 09:16:08 +04:00
|
|
|
} else
|
|
|
|
p->state = LDP_PEER_HOLDDOWN;
|
|
|
|
p->timeout = p->holdtime;
|
2010-12-08 10:20:14 +03:00
|
|
|
shutdown(p->socket, SHUT_RDWR);
|
|
|
|
ldp_peer_delete_all_mappings(p);
|
|
|
|
del_all_ifaddr(p);
|
|
|
|
fatalp("LDP Neighbour %s is DOWN\n", inet_ntoa(p->ldp_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldp_peer_holddown_all()
|
|
|
|
{
|
|
|
|
struct ldp_peer *p;
|
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers) {
|
|
|
|
if ((p->state == LDP_PEER_ESTABLISHED) ||
|
|
|
|
(p->state == LDP_PEER_CONNECTED))
|
2013-01-29 01:35:34 +04:00
|
|
|
send_notification(p, get_message_id(),
|
|
|
|
NOTIF_FATAL | NOTIF_SHUTDOWN);
|
2010-12-08 10:20:14 +03:00
|
|
|
ldp_peer_holddown(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldp_peer_delete(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SLIST_REMOVE(&ldp_peer_head, p, ldp_peer, peers);
|
|
|
|
close(p->socket);
|
|
|
|
warnp("LDP Neighbor %s holddown timer expired\n", inet_ntoa(p->ldp_id));
|
2013-01-26 21:29:55 +04:00
|
|
|
free(p->address);
|
|
|
|
free(p->transport_address);
|
2010-12-08 10:20:14 +03:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ldp_peer *
|
2013-01-26 21:29:55 +04:00
|
|
|
get_ldp_peer(const struct sockaddr * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer *p;
|
2013-01-26 21:29:55 +04:00
|
|
|
const struct sockaddr_in *a_inet = (const struct sockaddr_in *)a;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers) {
|
2013-01-26 21:29:55 +04:00
|
|
|
if (a->sa_family == AF_INET &&
|
|
|
|
memcmp((const void *) &a_inet->sin_addr,
|
|
|
|
(const void *) &p->ldp_id,
|
|
|
|
sizeof(struct in_addr)) == 0)
|
2010-12-08 10:20:14 +03:00
|
|
|
return p;
|
2013-02-05 00:28:24 +04:00
|
|
|
if (sockaddr_cmp(a, p->address) == 0 ||
|
|
|
|
sockaddr_cmp(a, p->transport_address) == 0 ||
|
|
|
|
check_ifaddr(p, a))
|
2010-12-08 10:20:14 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
struct ldp_peer *
|
|
|
|
get_ldp_peer_by_id(const struct in_addr *a)
|
|
|
|
{
|
|
|
|
struct ldp_peer *p;
|
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers)
|
|
|
|
if (memcmp((const void*)a,
|
|
|
|
(const void*)&p->ldp_id, sizeof(*a)) == 0)
|
|
|
|
return p;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
struct ldp_peer *
|
|
|
|
get_ldp_peer_by_socket(int s)
|
|
|
|
{
|
|
|
|
struct ldp_peer *p;
|
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers)
|
|
|
|
if (p->socket == s)
|
|
|
|
return p;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds address list bounded to a specific peer
|
|
|
|
* Returns the number of addresses inserted successfuly
|
|
|
|
*/
|
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
add_ifaddresses(struct ldp_peer * p, const struct al_tlv * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
int i, c, n;
|
2013-07-11 09:45:23 +04:00
|
|
|
const struct in_addr *ia;
|
2013-01-26 21:29:55 +04:00
|
|
|
struct sockaddr_in ipa;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
memset(&ipa, 0, sizeof(ipa));
|
|
|
|
ipa.sin_len = sizeof(ipa);
|
|
|
|
ipa.sin_family = AF_INET;
|
2010-12-08 10:20:14 +03:00
|
|
|
/*
|
|
|
|
* Check if tlv is Address type, if it's correct size (at least one
|
|
|
|
* address) and if it's IPv4
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((ntohs(a->type) != TLV_ADDRESS_LIST) ||
|
|
|
|
(ntohs(a->length) < sizeof(a->af) + sizeof(struct in_addr)) ||
|
|
|
|
(ntohs(a->af) != LDP_AF_INET))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Number of addresses to insert */
|
|
|
|
n = (ntohs(a->length) - sizeof(a->af)) / sizeof(struct in_addr);
|
|
|
|
|
|
|
|
debugp("Trying to add %d addresses to peer %s ... \n", n,
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
|
2013-07-11 09:45:23 +04:00
|
|
|
for (ia = (const struct in_addr *) & a->address,c = 0,i = 0; i<n; i++) {
|
2013-01-26 21:29:55 +04:00
|
|
|
memcpy(&ipa.sin_addr, &ia[i], sizeof(ipa.sin_addr));
|
|
|
|
if (add_ifaddr(p, (struct sockaddr *)&ipa) == LDP_E_OK)
|
2010-12-08 10:20:14 +03:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugp("Added %d addresses\n", c);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
del_ifaddresses(struct ldp_peer * p, const struct al_tlv * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
int i, c, n;
|
2013-07-11 09:45:23 +04:00
|
|
|
const struct in_addr *ia;
|
2013-01-26 21:29:55 +04:00
|
|
|
struct sockaddr_in ipa;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
memset(&ipa, 0, sizeof(ipa));
|
|
|
|
ipa.sin_len = sizeof(ipa);
|
|
|
|
ipa.sin_family = AF_INET;
|
2010-12-08 10:20:14 +03:00
|
|
|
/*
|
|
|
|
* Check if tlv is Address type, if it's correct size (at least one
|
|
|
|
* address) and if it's IPv4
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ntohs(a->type) != TLV_ADDRESS_LIST ||
|
|
|
|
ntohs(a->length) > sizeof(a->af) + sizeof(struct in_addr) ||
|
|
|
|
ntohs(a->af) != LDP_AF_INET)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = (ntohs(a->length) - sizeof(a->af)) / sizeof(struct in_addr);
|
|
|
|
|
|
|
|
debugp("Trying to delete %d addresses from peer %s ... \n", n,
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
|
2013-07-11 09:45:23 +04:00
|
|
|
for (ia = (const struct in_addr *) & a[1], c = 0, i = 0; i < n; i++) {
|
2013-01-26 21:29:55 +04:00
|
|
|
memcpy(&ipa.sin_addr, &ia[i], sizeof(ipa.sin_addr));
|
|
|
|
if (del_ifaddr(p, (struct sockaddr *)&ipa) == LDP_E_OK)
|
2010-12-08 10:20:14 +03:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugp("Deleted %d addresses\n", c);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Adds a _SINGLE_ INET address to a specific peer */
|
2010-12-08 10:20:14 +03:00
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
add_ifaddr(struct ldp_peer * p, const struct sockaddr * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer_address *lpa;
|
|
|
|
|
|
|
|
/* Is it already there ? */
|
|
|
|
if (check_ifaddr(p, a))
|
|
|
|
return LDP_E_ALREADY_DONE;
|
|
|
|
|
2010-12-09 03:10:59 +03:00
|
|
|
lpa = calloc(1, sizeof(*lpa));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (!lpa) {
|
|
|
|
fatalp("add_ifaddr: malloc problem\n");
|
|
|
|
return LDP_E_MEMORY;
|
2010-12-09 03:10:59 +03:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
assert(a->sa_len <= sizeof(union sockunion));
|
|
|
|
|
|
|
|
memcpy(&lpa->address.sa, a, a->sa_len);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
SLIST_INSERT_HEAD(&p->ldp_peer_address_head, lpa, addresses);
|
|
|
|
return LDP_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deletes an address bounded to a specific peer */
|
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
del_ifaddr(struct ldp_peer * p, const struct sockaddr * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer_address *wp;
|
|
|
|
|
|
|
|
wp = check_ifaddr(p, a);
|
|
|
|
if (!wp)
|
|
|
|
return LDP_E_NOENT;
|
|
|
|
|
|
|
|
SLIST_REMOVE(&p->ldp_peer_address_head, wp, ldp_peer_address,
|
|
|
|
addresses);
|
|
|
|
free(wp);
|
|
|
|
return LDP_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks if an address is already bounded */
|
|
|
|
struct ldp_peer_address *
|
2013-07-11 09:45:23 +04:00
|
|
|
check_ifaddr(const struct ldp_peer * p, const struct sockaddr * a)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer_address *wp;
|
|
|
|
|
|
|
|
SLIST_FOREACH(wp, &p->ldp_peer_address_head, addresses)
|
2013-01-26 21:29:55 +04:00
|
|
|
if (sockaddr_cmp(a, &wp->address.sa) == 0)
|
2010-12-08 10:20:14 +03:00
|
|
|
return wp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
del_all_ifaddr(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct ldp_peer_address *wp;
|
|
|
|
|
|
|
|
while (!SLIST_EMPTY(&p->ldp_peer_address_head)) {
|
|
|
|
wp = SLIST_FIRST(&p->ldp_peer_address_head);
|
|
|
|
SLIST_REMOVE_HEAD(&p->ldp_peer_address_head, addresses);
|
|
|
|
free(wp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-11 09:45:23 +04:00
|
|
|
print_bounded_addresses(const struct ldp_peer * p)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer_address *wp;
|
2013-01-29 01:08:14 +04:00
|
|
|
char abuf[512];
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
snprintf(abuf, sizeof(abuf), "Addresses bounded to peer %s: ",
|
2013-01-29 01:08:14 +04:00
|
|
|
satos(p->address));
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_FOREACH(wp, &p->ldp_peer_address_head, addresses) {
|
2013-01-26 21:29:55 +04:00
|
|
|
strncat(abuf, satos(&wp->address.sa),
|
|
|
|
sizeof(abuf) -1);
|
2010-12-08 10:20:14 +03:00
|
|
|
strncat(abuf, " ", sizeof(abuf) -1);
|
|
|
|
}
|
|
|
|
warnp("%s\n", abuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adds a label and a prefix to a specific peer */
|
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
ldp_peer_add_mapping(struct ldp_peer * p, const struct sockaddr * a,
|
|
|
|
int prefix, int label)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct label_mapping *lma;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
2013-07-18 10:07:45 +04:00
|
|
|
if ((lma = ldp_peer_get_lm(p, a, prefix)) != NULL) {
|
|
|
|
/* Change the current label */
|
|
|
|
lma->label = label;
|
|
|
|
return LDP_E_OK;
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2010-12-09 03:10:59 +03:00
|
|
|
lma = malloc(sizeof(*lma));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (!lma) {
|
|
|
|
fatalp("ldp_peer_add_mapping: malloc problem\n");
|
|
|
|
return LDP_E_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
memcpy(&lma->address, a, a->sa_len);
|
2010-12-08 10:20:14 +03:00
|
|
|
lma->prefix = prefix;
|
|
|
|
lma->label = label;
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
rb_tree_insert_node(&p->label_mapping_tree, lma);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
return LDP_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-07-11 09:45:23 +04:00
|
|
|
ldp_peer_delete_mapping(struct ldp_peer * p, const struct sockaddr * a,
|
|
|
|
int prefix)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct label_mapping *lma;
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
if (a == NULL || (lma = ldp_peer_get_lm(p, a, prefix)) == NULL)
|
2013-07-20 09:16:08 +04:00
|
|
|
return LDP_E_NOENT;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
rb_tree_remove_node(&p->label_mapping_tree, lma);
|
2010-12-08 10:20:14 +03:00
|
|
|
free(lma);
|
|
|
|
|
|
|
|
return LDP_E_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
static struct label_mapping *
|
|
|
|
ldp_peer_get_lm(struct ldp_peer * p, const struct sockaddr * a,
|
2013-07-11 09:45:23 +04:00
|
|
|
uint prefix)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
2013-08-02 11:29:56 +04:00
|
|
|
struct label_mapping rv;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
assert(a->sa_len <= sizeof(union sockunion));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
memset(&rv, 0, sizeof(rv));
|
|
|
|
memcpy(&rv.address.sa, a, a->sa_len);
|
|
|
|
rv.prefix = prefix;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
return rb_tree_find_node(&p->label_mapping_tree, &rv);
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
|
2013-07-20 09:16:08 +04:00
|
|
|
void
|
2010-12-08 10:20:14 +03:00
|
|
|
ldp_peer_delete_all_mappings(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct label_mapping *lma;
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
while((lma = RB_TREE_MIN(&p->label_mapping_tree)) != NULL) {
|
|
|
|
rb_tree_remove_node(&p->label_mapping_tree, lma);
|
2010-12-08 10:20:14 +03:00
|
|
|
free(lma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a mapping and its peer */
|
|
|
|
struct peer_map *
|
2013-07-11 09:45:23 +04:00
|
|
|
ldp_test_mapping(const struct sockaddr * a, int prefix,
|
|
|
|
const struct sockaddr * gate)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ldp_peer *lpeer;
|
|
|
|
struct peer_map *rv = NULL;
|
|
|
|
struct label_mapping *lm = NULL;
|
|
|
|
|
|
|
|
/* Checks if it's LPDID, else checks if it's an interface */
|
|
|
|
|
|
|
|
lpeer = get_ldp_peer(gate);
|
|
|
|
if (!lpeer) {
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("ldp_test_mapping: Gateway is not an LDP peer\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (lpeer->state != LDP_PEER_ESTABLISHED) {
|
2013-02-04 13:52:43 +04:00
|
|
|
fatalp("ldp_test_mapping: peer is down ?!\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lm = ldp_peer_get_lm(lpeer, a, prefix);
|
|
|
|
|
|
|
|
if (!lm) {
|
2013-02-04 13:52:43 +04:00
|
|
|
debugp("Cannot match prefix %s/%d to the specified peer\n",
|
|
|
|
satos(a), prefix);
|
2010-12-08 10:20:14 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-09 03:10:59 +03:00
|
|
|
rv = malloc(sizeof(*rv));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (!rv) {
|
|
|
|
fatalp("ldp_test_mapping: malloc problem\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv->lm = lm;
|
|
|
|
rv->peer = lpeer;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:29:56 +04:00
|
|
|
struct label_mapping * ldp_peer_lm_right(struct ldp_peer *p,
|
|
|
|
struct label_mapping * map)
|
|
|
|
{
|
|
|
|
if (map == NULL)
|
|
|
|
return RB_TREE_MIN(&p->label_mapping_tree);
|
|
|
|
else
|
|
|
|
return rb_tree_iterate(&p->label_mapping_tree, map,
|
|
|
|
RB_DIR_RIGHT);
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* Name from state */
|
|
|
|
const char * ldp_state_to_name(int state)
|
|
|
|
{
|
|
|
|
switch(state) {
|
|
|
|
case LDP_PEER_CONNECTING:
|
|
|
|
return "CONNECTING";
|
|
|
|
case LDP_PEER_CONNECTED:
|
|
|
|
return "CONNECTED";
|
|
|
|
case LDP_PEER_ESTABLISHED:
|
|
|
|
return "ESTABLISHED";
|
|
|
|
case LDP_PEER_HOLDDOWN:
|
|
|
|
return "HOLDDOWN";
|
|
|
|
}
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|