2013-02-05 00:28:24 +04:00
|
|
|
/* $NetBSD: socketops.c,v 1.26 2013/02/04 20:28:24 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/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2011-06-14 15:28:51 +04:00
|
|
|
#include <assert.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <errno.h>
|
2011-06-14 15:28:51 +04:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <poll.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <signal.h>
|
2011-06-14 15:28:51 +04:00
|
|
|
#include <stdio.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
2011-06-14 15:28:51 +04:00
|
|
|
#include <unistd.h>
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-27 01:07:49 +04:00
|
|
|
#include "conffile.h"
|
2010-12-08 10:20:14 +03:00
|
|
|
#include "fsm.h"
|
|
|
|
#include "ldp.h"
|
|
|
|
#include "ldp_command.h"
|
|
|
|
#include "tlv.h"
|
|
|
|
#include "ldp_peer.h"
|
|
|
|
#include "notifications.h"
|
|
|
|
#include "tlv_stack.h"
|
|
|
|
#include "mpls_interface.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "mpls_routes.h"
|
|
|
|
#include "ldp_errors.h"
|
|
|
|
#include "socketops.h"
|
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
int ls; /* TCP listening socket on port 646 */
|
|
|
|
int route_socket; /* used to see when a route is added/deleted */
|
|
|
|
int command_socket; /* Listening socket for interface command */
|
|
|
|
int current_msg_id = 0x233;
|
|
|
|
int command_port = LDP_COMMAND_PORT;
|
2010-12-08 10:20:14 +03:00
|
|
|
extern int replay_index;
|
|
|
|
extern struct rt_msg replay_rt[REPLAY_MAX];
|
|
|
|
extern struct com_sock csockets[MAX_COMMAND_SOCKETS];
|
|
|
|
|
|
|
|
int ldp_hello_time = LDP_HELLO_TIME;
|
2010-12-30 14:29:21 +03:00
|
|
|
int ldp_keepalive_time = LDP_KEEPALIVE_TIME;
|
|
|
|
int ldp_holddown_time = LDP_HOLDTIME;
|
2011-06-14 15:28:51 +04:00
|
|
|
int no_default_route = 1;
|
2012-11-12 22:39:00 +04:00
|
|
|
int loop_detection = 0;
|
2013-02-03 23:41:59 +04:00
|
|
|
bool may_connect;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
void recv_pdu(int);
|
|
|
|
void send_hello_alarm(int);
|
2011-08-31 17:32:36 +04:00
|
|
|
__dead static void bail_out(int);
|
2013-01-26 21:29:55 +04:00
|
|
|
static int bind_socket(int s, int stype);
|
2012-11-12 22:39:00 +04:00
|
|
|
static int set_tos(int);
|
|
|
|
static int socket_reuse_port(int);
|
2010-12-08 10:20:14 +03:00
|
|
|
static int get_local_addr(struct sockaddr_dl *, struct in_addr *);
|
2013-01-26 21:29:55 +04:00
|
|
|
static int is_hello_socket(int);
|
2013-01-27 01:07:49 +04:00
|
|
|
static int is_passive_if(char *if_name);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
int
|
2012-11-12 22:39:00 +04:00
|
|
|
create_hello_sockets()
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct ip_mreq mcast_addr;
|
2013-01-26 21:29:55 +04:00
|
|
|
int s, joined_groups;
|
|
|
|
struct ifaddrs *ifa, *ifb;
|
|
|
|
uint lastifindex;
|
2013-01-26 21:46:50 +04:00
|
|
|
#ifdef INET6
|
|
|
|
struct ipv6_mreq mcast_addr6;
|
2013-01-26 21:29:55 +04:00
|
|
|
struct sockaddr_in6 *if_sa6;
|
2012-11-12 22:39:00 +04:00
|
|
|
#endif
|
2013-01-26 21:29:55 +04:00
|
|
|
struct hello_socket *hs;
|
|
|
|
|
|
|
|
SLIST_INIT(&hello_socket_head);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2010-12-08 10:20:14 +03:00
|
|
|
if (s < 0)
|
|
|
|
return s;
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("INET4 socket created (%d)\n", s);
|
2010-12-08 10:20:14 +03:00
|
|
|
/*
|
2012-11-12 22:39:00 +04:00
|
|
|
* RFC5036 specifies we should listen to all subnet routers multicast
|
2010-12-08 10:20:14 +03:00
|
|
|
* group
|
|
|
|
*/
|
2013-01-26 23:44:52 +04:00
|
|
|
mcast_addr.imr_multiaddr.s_addr = htonl(INADDR_ALLRTRS_GROUP);
|
2012-11-12 22:39:00 +04:00
|
|
|
|
|
|
|
if (socket_reuse_port(s) < 0)
|
|
|
|
goto chs_error;
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Bind it to port 646 */
|
|
|
|
if (bind_socket(s, AF_INET) == -1) {
|
2012-11-12 22:39:00 +04:00
|
|
|
warnp("Cannot bind INET hello socket\n");
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* We don't need to receive back our messages */
|
2013-01-16 12:28:44 +04:00
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &(u_char){0},
|
|
|
|
sizeof(u_char)) == -1) {
|
2012-11-12 22:39:00 +04:00
|
|
|
fatalp("INET setsockopt IP_MCAST_LOOP: %s\n", strerror(errno));
|
|
|
|
goto chs_error;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Finally join the group on all interfaces */
|
|
|
|
if (getifaddrs(&ifa) == -1) {
|
|
|
|
fatalp("Cannot iterate interfaces\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lastifindex = UINT_MAX;
|
|
|
|
joined_groups = 0;
|
|
|
|
for (ifb = ifa; ifb; ifb = ifb->ifa_next) {
|
|
|
|
struct sockaddr_in *if_sa = (struct sockaddr_in *) ifb->ifa_addr;
|
|
|
|
if (if_sa->sin_family != AF_INET || (!(ifb->ifa_flags & IFF_UP)) ||
|
|
|
|
(ifb->ifa_flags & IFF_LOOPBACK) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
(!(ifb->ifa_flags & IFF_MULTICAST)) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
(ntohl(if_sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
is_passive_if(ifb->ifa_name) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
lastifindex == if_nametoindex(ifb->ifa_name))
|
|
|
|
continue;
|
|
|
|
lastifindex = if_nametoindex(ifb->ifa_name);
|
|
|
|
|
|
|
|
mcast_addr.imr_interface.s_addr = if_sa->sin_addr.s_addr;
|
|
|
|
debugp("Join IPv4 mcast on %s\n", ifb->ifa_name);
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mcast_addr,
|
|
|
|
sizeof(mcast_addr)) == -1) {
|
|
|
|
fatalp("setsockopt ADD_MEMBER: %s\n", strerror(errno));
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
joined_groups++;
|
|
|
|
if (joined_groups == IP_MAX_MEMBERSHIPS) {
|
|
|
|
warnp("Maximum group memberships reached for INET socket\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* TTL:1 for IPv4 */
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &(int){1},
|
|
|
|
sizeof(int)) == -1) {
|
|
|
|
fatalp("set mcast ttl: %s\n", strerror(errno));
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
/* TOS :0xc0 for IPv4 */
|
|
|
|
if (set_tos(s) == -1) {
|
|
|
|
fatalp("set_tos: %s", strerror(errno));
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
/* we need to get the input interface for message processing */
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &(uint32_t){1},
|
2012-11-12 22:39:00 +04:00
|
|
|
sizeof(uint32_t)) == -1) {
|
2013-01-26 21:29:55 +04:00
|
|
|
fatalp("Cannot set IP_RECVIF\n");
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
hs = (struct hello_socket *)malloc(sizeof(*hs));
|
|
|
|
if (hs == NULL) {
|
|
|
|
fatalp("Cannot alloc hello_socket structure\n");
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
2013-01-26 21:29:55 +04:00
|
|
|
}
|
|
|
|
hs->type = AF_INET;
|
|
|
|
hs->socket = s;
|
|
|
|
SLIST_INSERT_HEAD(&hello_socket_head, hs, listentry);
|
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
#ifdef INET6
|
2013-01-26 21:29:55 +04:00
|
|
|
/*
|
|
|
|
* Now we do the same for IPv6
|
|
|
|
*/
|
|
|
|
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (s < 0) {
|
|
|
|
fatalp("Cannot create INET6 socket\n");
|
|
|
|
return -1;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("INET6 socket created (%d)\n", s);
|
|
|
|
|
|
|
|
if (socket_reuse_port(s) < 0)
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
2013-01-26 21:29:55 +04:00
|
|
|
|
|
|
|
if (bind_socket(s, AF_INET6) == -1) {
|
|
|
|
fatalp("Cannot bind INET6 hello socket\n");
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
2013-01-26 21:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
|
|
|
|
&(uint){0}, sizeof(uint)) == -1) {
|
|
|
|
fatalp("INET6 setsocketopt IP_MCAST_LOOP: %s\n",
|
|
|
|
strerror(errno));
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
2012-11-12 22:39:00 +04:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
lastifindex = UINT_MAX;
|
2013-01-26 23:44:52 +04:00
|
|
|
mcast_addr6.ipv6mr_multiaddr = in6addr_linklocal_allrouters;
|
2013-01-26 21:29:55 +04:00
|
|
|
for (ifb = ifa; ifb; ifb = ifb->ifa_next) {
|
|
|
|
if_sa6 = (struct sockaddr_in6 *) ifb->ifa_addr;
|
|
|
|
if (if_sa6->sin6_family != AF_INET6 ||
|
|
|
|
(!(ifb->ifa_flags & IFF_UP)) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
(!(ifb->ifa_flags & IFF_MULTICAST)) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
(ifb->ifa_flags & IFF_LOOPBACK) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
is_passive_if(ifb->ifa_name) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
IN6_IS_ADDR_LOOPBACK(&if_sa6->sin6_addr))
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* draft-ietf-mpls-ldp-ipv6-07 Section 5.1:
|
|
|
|
* Additionally, the link-local
|
|
|
|
* IPv6 address MUST be used as the source IP address in IPv6
|
|
|
|
* LDP Link Hellos.
|
|
|
|
*/
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&if_sa6->sin6_addr) == 0)
|
|
|
|
continue;
|
|
|
|
/* We should have only one LLADDR per interface, but... */
|
|
|
|
if (lastifindex == if_nametoindex(ifb->ifa_name))
|
|
|
|
continue;
|
|
|
|
mcast_addr6.ipv6mr_interface = lastifindex =
|
|
|
|
if_nametoindex(ifb->ifa_name);
|
|
|
|
|
|
|
|
debugp("Join IPv6 mcast on %s\n", ifb->ifa_name);
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
|
|
|
(char *)&mcast_addr6, sizeof(mcast_addr6)) == -1) {
|
|
|
|
fatalp("INET6 setsockopt JOIN: %s\n", strerror(errno));
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
|
|
|
|
/* TTL: 255 for IPv6 - draft-ietf-mpls-ldp-ipv6-07 Section 9 */
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
|
|
|
|
&(int){255}, sizeof(int)) == -1) {
|
|
|
|
fatalp("set mcast hops: %s\n", strerror(errno));
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
|
|
|
&(uint32_t){1}, sizeof(uint32_t)) == -1)
|
2012-11-12 22:39:00 +04:00
|
|
|
goto chs_error;
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
hs = (struct hello_socket *)malloc(sizeof(*hs));
|
|
|
|
if (hs == NULL) {
|
|
|
|
fatalp("Memory alloc problem: hs\n");
|
|
|
|
goto chs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->type = AF_INET6;
|
|
|
|
hs->socket = s;
|
|
|
|
SLIST_INSERT_HEAD(&hello_socket_head, hs, listentry);
|
|
|
|
#endif
|
2012-11-12 22:39:00 +04:00
|
|
|
return 0;
|
|
|
|
chs_error:
|
|
|
|
close(s);
|
|
|
|
return -1;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Check if parameter is a hello socket */
|
|
|
|
int
|
|
|
|
is_hello_socket(int s)
|
|
|
|
{
|
|
|
|
struct hello_socket *hs;
|
|
|
|
|
|
|
|
SLIST_FOREACH(hs, &hello_socket_head, listentry)
|
|
|
|
if (hs->socket == s)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-27 01:07:49 +04:00
|
|
|
/* Check if interface is passive */
|
|
|
|
static int
|
|
|
|
is_passive_if(char *if_name)
|
|
|
|
{
|
|
|
|
struct passive_if *pif;
|
|
|
|
|
|
|
|
SLIST_FOREACH(pif, &passifs_head, listentry)
|
|
|
|
if (strncasecmp(if_name, pif->if_name, IF_NAMESIZE) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* Sets the TTL to 1 as we don't want to transmit outside this subnet */
|
2012-11-12 22:39:00 +04:00
|
|
|
int
|
2010-12-08 10:20:14 +03:00
|
|
|
set_ttl(int s)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if ((ret = setsockopt(s, IPPROTO_IP, IP_TTL, &(int){1}, sizeof(int)))
|
|
|
|
== -1)
|
|
|
|
fatalp("set_ttl: %s", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets TOS to 0xc0 aka IP Precedence 6 */
|
2012-11-12 22:39:00 +04:00
|
|
|
static int
|
2010-12-08 10:20:14 +03:00
|
|
|
set_tos(int s)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if ((ret = setsockopt(s, IPPROTO_IP, IP_TOS, &(int){0xc0},
|
|
|
|
sizeof(int))) == -1)
|
|
|
|
fatalp("set_tos: %s", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
static int
|
2010-12-08 10:20:14 +03:00
|
|
|
socket_reuse_port(int s)
|
|
|
|
{
|
2012-11-12 22:39:00 +04:00
|
|
|
int ret;
|
2010-12-08 10:20:14 +03:00
|
|
|
if ((ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &(int){1},
|
|
|
|
sizeof(int))) == -1)
|
|
|
|
fatalp("socket_reuse_port: %s", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* binds an UDP socket */
|
2012-11-12 22:39:00 +04:00
|
|
|
static int
|
2013-01-26 21:29:55 +04:00
|
|
|
bind_socket(int s, int stype)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
2013-01-16 12:28:44 +04:00
|
|
|
union sockunion su;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
assert (stype == AF_INET || stype == AF_INET6);
|
2012-11-12 22:39:00 +04:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
if (stype == AF_INET) {
|
2013-01-16 12:28:44 +04:00
|
|
|
su.sin.sin_len = sizeof(su.sin);
|
|
|
|
su.sin.sin_family = AF_INET;
|
2013-01-26 21:29:55 +04:00
|
|
|
su.sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
2013-01-16 12:28:44 +04:00
|
|
|
su.sin.sin_port = htons(LDP_PORT);
|
2012-11-12 22:39:00 +04:00
|
|
|
}
|
|
|
|
#ifdef INET6
|
2013-01-26 21:29:55 +04:00
|
|
|
else if (stype == AF_INET6) {
|
2013-01-16 12:28:44 +04:00
|
|
|
su.sin6.sin6_len = sizeof(su.sin6);
|
|
|
|
su.sin6.sin6_family = AF_INET6;
|
|
|
|
su.sin6.sin6_addr = in6addr_any;
|
|
|
|
su.sin6.sin6_port = htons(LDP_PORT);
|
2012-11-12 22:39:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-01-16 12:28:44 +04:00
|
|
|
if (bind(s, &su.sa, su.sa.sa_len)) {
|
2012-11-12 22:39:00 +04:00
|
|
|
fatalp("bind_socket: %s\n", strerror(errno));
|
2010-12-08 10:20:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create / bind the TCP socket */
|
2012-11-12 22:39:00 +04:00
|
|
|
int
|
2010-12-08 10:20:14 +03:00
|
|
|
create_listening_socket(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
sa.sin_len = sizeof(sa);
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(LDP_PORT);
|
|
|
|
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2010-12-08 10:20:14 +03:00
|
|
|
if (s < 0)
|
|
|
|
return s;
|
|
|
|
if (bind(s, (struct sockaddr *) & sa, sizeof(sa))) {
|
|
|
|
fatalp("bind: %s", strerror(errno));
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (listen(s, 10) == -1) {
|
|
|
|
fatalp("listen: %s", strerror(errno));
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* if (set_tos(s) == -1) {
|
|
|
|
fatalp("set_tos: %s", strerror(errno));
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/ return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's ugly. We need a function to pass all tlvs and create pdu but since I
|
|
|
|
* use UDP socket only to send hellos, I didn't bother
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
send_hello(void)
|
|
|
|
{
|
|
|
|
struct hello_tlv *t;
|
|
|
|
struct common_hello_tlv *cht;
|
|
|
|
struct ldp_pdu *spdu;
|
|
|
|
struct transport_address_tlv *trtlv;
|
|
|
|
void *v;
|
|
|
|
struct sockaddr_in sadest; /* Destination ALL_ROUTERS */
|
2012-11-12 22:39:00 +04:00
|
|
|
ssize_t sb = 0; /* sent bytes */
|
2010-12-08 10:20:14 +03:00
|
|
|
struct ifaddrs *ifa, *ifb;
|
|
|
|
struct sockaddr_in *if_sa;
|
2013-01-26 21:29:55 +04:00
|
|
|
int ip4socket = -1;
|
|
|
|
uint lastifindex;
|
|
|
|
struct hello_socket *hs;
|
2012-11-12 22:39:00 +04:00
|
|
|
#ifdef INET6
|
|
|
|
struct sockaddr_in6 sadest6;
|
2013-01-26 21:29:55 +04:00
|
|
|
int ip6socket = -1;
|
2012-11-12 22:39:00 +04:00
|
|
|
#endif
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
#define BASIC_HELLO_MSG_SIZE (sizeof(struct ldp_pdu) + /* PDU */ \
|
2010-12-08 10:20:14 +03:00
|
|
|
TLV_TYPE_LENGTH + MSGID_SIZE + /* Hello TLV */ \
|
|
|
|
/* Common Hello TLV */ \
|
2012-11-12 22:39:00 +04:00
|
|
|
sizeof(struct common_hello_tlv))
|
|
|
|
#define GENERAL_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + \
|
|
|
|
/* Transport Address */ \
|
|
|
|
sizeof(struct transport_address_tlv)
|
|
|
|
#define IPV4_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + 4 + sizeof(struct in_addr)
|
|
|
|
#define IPV6_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + 4 + sizeof(struct in6_addr)
|
|
|
|
|
|
|
|
if ((v = calloc(1, GENERAL_HELLO_MSG_SIZE)) == NULL) {
|
|
|
|
fatalp("alloc problem in send_hello()\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spdu = (struct ldp_pdu *)((char *)v);
|
|
|
|
t = (struct hello_tlv *)(spdu + 1);
|
|
|
|
cht = &t->ch; /* Hello tlv struct includes CHT */
|
|
|
|
trtlv = (struct transport_address_tlv *)(t + 1);
|
|
|
|
|
|
|
|
/* Prepare PDU envelope */
|
|
|
|
spdu->version = htons(LDP_VERSION);
|
2012-11-12 22:39:00 +04:00
|
|
|
spdu->length = htons(IPV4_HELLO_MSG_SIZE - PDU_VER_LENGTH);
|
2010-12-08 10:20:14 +03:00
|
|
|
inet_aton(LDP_ID, &spdu->ldp_id);
|
|
|
|
|
|
|
|
/* Prepare Hello TLV */
|
|
|
|
t->type = htons(LDP_HELLO);
|
|
|
|
t->length = htons(MSGID_SIZE +
|
|
|
|
sizeof(struct common_hello_tlv) +
|
2012-11-12 22:39:00 +04:00
|
|
|
IPV4_HELLO_MSG_SIZE - BASIC_HELLO_MSG_SIZE);
|
2010-12-08 10:20:14 +03:00
|
|
|
/*
|
2012-11-12 22:39:00 +04:00
|
|
|
* kefren:
|
2010-12-08 10:20:14 +03:00
|
|
|
* I used ID 0 instead of htonl(get_message_id()) because I've
|
2012-11-12 22:39:00 +04:00
|
|
|
* seen hellos from Cisco routers doing the same thing
|
2010-12-08 10:20:14 +03:00
|
|
|
*/
|
|
|
|
t->messageid = 0;
|
|
|
|
|
|
|
|
/* Prepare Common Hello attributes */
|
|
|
|
cht->type = htons(TLV_COMMON_HELLO);
|
|
|
|
cht->length = htons(sizeof(cht->holdtime) + sizeof(cht->res));
|
2010-12-30 14:29:21 +03:00
|
|
|
cht->holdtime = htons(ldp_holddown_time);
|
2010-12-08 10:20:14 +03:00
|
|
|
cht->res = 0;
|
|
|
|
|
|
|
|
/*
|
2012-11-12 22:39:00 +04:00
|
|
|
* Prepare Transport Address TLV RFC5036 says: "If this optional TLV
|
2010-12-08 10:20:14 +03:00
|
|
|
* is not present the IPv4 source address for the UDP packet carrying
|
|
|
|
* the Hello should be used." But we send it because everybody seems
|
|
|
|
* to do so
|
|
|
|
*/
|
|
|
|
trtlv->type = htons(TLV_IPV4_TRANSPORT);
|
|
|
|
trtlv->length = htons(sizeof(struct in_addr));
|
|
|
|
/* trtlv->address will be set for each socket */
|
|
|
|
|
|
|
|
/* Destination sockaddr */
|
|
|
|
memset(&sadest, 0, sizeof(sadest));
|
|
|
|
sadest.sin_len = sizeof(sadest);
|
|
|
|
sadest.sin_family = AF_INET;
|
|
|
|
sadest.sin_port = htons(LDP_PORT);
|
2013-01-26 23:44:52 +04:00
|
|
|
sadest.sin_addr.s_addr = htonl(INADDR_ALLRTRS_GROUP);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Find our socket */
|
|
|
|
SLIST_FOREACH(hs, &hello_socket_head, listentry)
|
|
|
|
if (hs->type == AF_INET) {
|
|
|
|
ip4socket = hs->socket;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(ip4socket >= 0);
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
if (getifaddrs(&ifa) == -1) {
|
|
|
|
free(v);
|
2013-01-26 21:29:55 +04:00
|
|
|
fatalp("Cannot enumerate interfaces\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
lastifindex = UINT_MAX;
|
2012-11-12 22:39:00 +04:00
|
|
|
/* Loop all interfaces in order to send IPv4 hellos */
|
2010-12-08 10:20:14 +03:00
|
|
|
for (ifb = ifa; ifb; ifb = ifb->ifa_next) {
|
|
|
|
if_sa = (struct sockaddr_in *) ifb->ifa_addr;
|
2013-01-27 01:07:49 +04:00
|
|
|
if (if_sa->sin_family != AF_INET ||
|
|
|
|
(!(ifb->ifa_flags & IFF_UP)) ||
|
|
|
|
(ifb->ifa_flags & IFF_LOOPBACK) ||
|
|
|
|
(!(ifb->ifa_flags & IFF_MULTICAST)) ||
|
|
|
|
is_passive_if(ifb->ifa_name) ||
|
|
|
|
(ntohl(if_sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET) ||
|
|
|
|
lastifindex == if_nametoindex(ifb->ifa_name))
|
2010-12-08 10:20:14 +03:00
|
|
|
continue;
|
2012-11-12 22:39:00 +04:00
|
|
|
|
|
|
|
/* Send only once per interface, using primary address */
|
2013-01-26 21:29:55 +04:00
|
|
|
if (lastifindex == if_nametoindex(ifb->ifa_name))
|
2010-12-08 10:20:14 +03:00
|
|
|
continue;
|
2013-01-26 21:29:55 +04:00
|
|
|
lastifindex = if_nametoindex(ifb->ifa_name);
|
|
|
|
|
|
|
|
if (setsockopt(ip4socket, IPPROTO_IP, IP_MULTICAST_IF,
|
2010-12-08 10:20:14 +03:00
|
|
|
&if_sa->sin_addr, sizeof(struct in_addr)) == -1) {
|
|
|
|
warnp("setsockopt failed: %s\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-12 22:39:00 +04:00
|
|
|
trtlv->address.ip4addr.s_addr = if_sa->sin_addr.s_addr;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Put it on the wire */
|
|
|
|
sb = sendto(ip4socket, v, IPV4_HELLO_MSG_SIZE, 0,
|
|
|
|
(struct sockaddr *) & sadest, sizeof(sadest));
|
2012-11-12 22:39:00 +04:00
|
|
|
if (sb < (ssize_t)(IPV4_HELLO_MSG_SIZE))
|
2010-12-08 10:20:14 +03:00
|
|
|
fatalp("send: %s", strerror(errno));
|
|
|
|
else
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("Sent (IPv4) %zd bytes on %s"
|
|
|
|
" (PDU: %d, Hello TLV: %d, CH: %d, TR: %d)\n",
|
2012-11-12 22:39:00 +04:00
|
|
|
sb, ifb->ifa_name,
|
|
|
|
ntohs(spdu->length), ntohs(t->length),
|
|
|
|
ntohs(cht->length), ntohs(trtlv->length));
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
/* Adjust lengths */
|
|
|
|
spdu->length = htons(IPV6_HELLO_MSG_SIZE - PDU_VER_LENGTH);
|
|
|
|
t->length = htons(MSGID_SIZE +
|
|
|
|
sizeof(struct common_hello_tlv) +
|
|
|
|
IPV6_HELLO_MSG_SIZE - BASIC_HELLO_MSG_SIZE);
|
|
|
|
trtlv->length = htons(sizeof(struct in6_addr));
|
|
|
|
trtlv->type = htons(TLV_IPV6_TRANSPORT);
|
|
|
|
|
|
|
|
/* Prepare destination sockaddr */
|
|
|
|
memset(&sadest6, 0, sizeof(sadest6));
|
|
|
|
sadest6.sin6_len = sizeof(sadest6);
|
|
|
|
sadest6.sin6_family = AF_INET6;
|
|
|
|
sadest6.sin6_port = htons(LDP_PORT);
|
2013-01-26 23:44:52 +04:00
|
|
|
sadest6.sin6_addr = in6addr_linklocal_allrouters;
|
2012-11-12 22:39:00 +04:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
SLIST_FOREACH(hs, &hello_socket_head, listentry)
|
|
|
|
if (hs->type == AF_INET6) {
|
|
|
|
ip6socket = hs->socket;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastifindex = UINT_MAX;
|
2012-11-12 22:39:00 +04:00
|
|
|
for (ifb = ifa; ifb; ifb = ifb->ifa_next) {
|
2013-01-26 21:29:55 +04:00
|
|
|
struct sockaddr_in6 * if_sa6 =
|
|
|
|
(struct sockaddr_in6 *) ifb->ifa_addr;
|
|
|
|
if (if_sa6->sin6_family != AF_INET6 ||
|
|
|
|
(!(ifb->ifa_flags & IFF_UP)) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
(!(ifb->ifa_flags & IFF_MULTICAST)) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
(ifb->ifa_flags & IFF_LOOPBACK) ||
|
2013-01-27 01:07:49 +04:00
|
|
|
is_passive_if(ifb->ifa_name) ||
|
2013-01-26 21:29:55 +04:00
|
|
|
IN6_IS_ADDR_LOOPBACK(&if_sa6->sin6_addr))
|
2012-11-12 22:39:00 +04:00
|
|
|
continue;
|
2012-11-13 10:58:58 +04:00
|
|
|
/*
|
|
|
|
* draft-ietf-mpls-ldp-ipv6-07 Section 5.1:
|
|
|
|
* Additionally, the link-local
|
|
|
|
* IPv6 address MUST be used as the source IP address in IPv6
|
|
|
|
* LDP Link Hellos.
|
|
|
|
*/
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&if_sa6->sin6_addr) == 0)
|
|
|
|
continue;
|
2013-01-26 21:29:55 +04:00
|
|
|
/* We should have only one LLADDR per interface, but... */
|
|
|
|
if (lastifindex == if_nametoindex(ifb->ifa_name))
|
2012-11-12 22:39:00 +04:00
|
|
|
continue;
|
2013-01-26 21:29:55 +04:00
|
|
|
lastifindex = if_nametoindex(ifb->ifa_name);
|
|
|
|
|
|
|
|
if (setsockopt(ip6socket, IPPROTO_IPV6, IPV6_MULTICAST_IF,
|
|
|
|
&lastifindex, sizeof(int)) == -1) {
|
|
|
|
fatalp("ssopt6 IPV6_MULTICAST_IF failed: %s for %s\n",
|
|
|
|
strerror(errno), ifb->ifa_name);
|
2012-11-12 22:39:00 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-01-26 21:29:55 +04:00
|
|
|
|
2012-11-12 22:39:00 +04:00
|
|
|
memcpy(&trtlv->address.ip6addr, &if_sa6->sin6_addr,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
|
|
|
|
/* Put it on the wire */
|
2013-01-26 21:29:55 +04:00
|
|
|
sb = sendto(ip6socket, v, IPV6_HELLO_MSG_SIZE,
|
2012-11-12 22:39:00 +04:00
|
|
|
0, (struct sockaddr *)&sadest6, sizeof(sadest6));
|
|
|
|
if (sb < (ssize_t)(IPV6_HELLO_MSG_SIZE))
|
2013-01-26 21:29:55 +04:00
|
|
|
fatalp("send6: %s", strerror(errno));
|
2012-11-12 22:39:00 +04:00
|
|
|
else
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("Sent (IPv6) %zd bytes on %s "
|
2012-11-12 22:39:00 +04:00
|
|
|
"(PDU: %d, Hello TLV: %d, CH: %d TR: %d)\n",
|
2013-01-26 21:29:55 +04:00
|
|
|
sb, ifb->ifa_name, htons(spdu->length),
|
|
|
|
htons(t->length), htons(cht->length),
|
|
|
|
htons(trtlv->length));
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
2012-11-12 22:39:00 +04:00
|
|
|
#endif
|
2010-12-08 10:20:14 +03:00
|
|
|
freeifaddrs(ifa);
|
|
|
|
free(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_message_id(void)
|
|
|
|
{
|
|
|
|
current_msg_id++;
|
|
|
|
return current_msg_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_local_addr(struct sockaddr_dl *sdl, struct in_addr *sin)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifa, *ifb;
|
|
|
|
struct sockaddr_in *sinet;
|
|
|
|
|
|
|
|
if (sdl == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifa) == -1)
|
|
|
|
return -1;
|
|
|
|
for (ifb = ifa; ifb; ifb = ifb->ifa_next)
|
|
|
|
if (ifb->ifa_addr->sa_family == AF_INET) {
|
|
|
|
if (if_nametoindex(ifb->ifa_name) != sdl->sdl_index)
|
|
|
|
continue;
|
|
|
|
sinet = (struct sockaddr_in*) ifb->ifa_addr;
|
|
|
|
sin->s_addr = sinet->sin_addr.s_addr;
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive PDUs on Multicast UDP socket */
|
|
|
|
void
|
|
|
|
recv_pdu(int sock)
|
|
|
|
{
|
|
|
|
struct ldp_pdu rpdu;
|
|
|
|
int c, i;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov[1];
|
|
|
|
unsigned char recvspace[MAX_PDU_SIZE];
|
|
|
|
struct hello_tlv *t;
|
2013-01-26 21:29:55 +04:00
|
|
|
union sockunion sender;
|
2010-12-08 10:20:14 +03:00
|
|
|
struct sockaddr_dl *sdl = NULL;
|
|
|
|
struct in_addr my_ldp_addr, local_addr;
|
|
|
|
struct cmsghdr *cmptr;
|
|
|
|
union {
|
|
|
|
struct cmsghdr cm;
|
|
|
|
char control[1024];
|
|
|
|
} control_un;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_control = control_un.control;
|
|
|
|
msg.msg_controllen = sizeof(control_un.control);
|
|
|
|
msg.msg_flags = 0;
|
2013-01-26 21:29:55 +04:00
|
|
|
msg.msg_name = &sender;
|
|
|
|
msg.msg_namelen = sizeof(sender);
|
2010-12-08 10:20:14 +03:00
|
|
|
iov[0].iov_base = recvspace;
|
|
|
|
iov[0].iov_len = sizeof(recvspace);
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
c = recvmsg(sock, &msg, MSG_WAITALL);
|
|
|
|
|
|
|
|
/* Check to see if this is larger than MIN_PDU_SIZE */
|
|
|
|
if (c < MIN_PDU_SIZE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Read the PDU */
|
|
|
|
i = get_pdu(recvspace, &rpdu);
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("recv_pdu(%d): PDU(size: %d) from: %s\n", sock,
|
|
|
|
c, satos(&sender.sa));
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
/* We currently understand Version 1 */
|
|
|
|
if (rpdu.version != LDP_VERSION) {
|
2013-01-26 21:29:55 +04:00
|
|
|
warnp("recv_pdu: Version mismatch\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Check if it's our hello */
|
2010-12-08 10:20:14 +03:00
|
|
|
inet_aton(LDP_ID, &my_ldp_addr);
|
|
|
|
if (rpdu.ldp_id.s_addr == my_ldp_addr.s_addr) {
|
2013-01-26 21:29:55 +04:00
|
|
|
/* It should not be looped. We set MULTICAST_LOOP 0 */
|
|
|
|
fatalp("Received our PDU. Ignoring it\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) ||
|
|
|
|
(msg.msg_flags & MSG_CTRUNC))
|
|
|
|
local_addr.s_addr = my_ldp_addr.s_addr;
|
|
|
|
else {
|
|
|
|
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
|
|
|
|
cmptr = CMSG_NXTHDR(&msg, cmptr))
|
|
|
|
if (cmptr->cmsg_level == IPPROTO_IP &&
|
|
|
|
cmptr->cmsg_type == IP_RECVIF) {
|
|
|
|
sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (get_local_addr(sdl, &local_addr) != 0)
|
|
|
|
local_addr.s_addr = my_ldp_addr.s_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
debugp("Read %d bytes from address %s Length: %.4d Version: %d\n",
|
|
|
|
c, inet_ntoa(rpdu.ldp_id), rpdu.length, rpdu.version);
|
|
|
|
|
|
|
|
/* Fill the TLV messages */
|
|
|
|
t = get_hello_tlv(recvspace + i, c - i);
|
2013-02-03 23:41:59 +04:00
|
|
|
run_ldp_hello(&rpdu, t, &sender.sa, &local_addr, sock, may_connect);
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send_hello_alarm(int unused)
|
|
|
|
{
|
2011-01-22 22:35:00 +03:00
|
|
|
struct ldp_peer *p, *ptmp;
|
2010-12-30 14:29:21 +03:00
|
|
|
struct hello_info *hi, *hinext;
|
2010-12-08 10:20:14 +03:00
|
|
|
time_t t = time(NULL);
|
|
|
|
int olderrno = errno;
|
|
|
|
|
2013-02-03 23:41:59 +04:00
|
|
|
if (may_connect == false)
|
|
|
|
may_connect = true;
|
2010-12-08 10:20:14 +03:00
|
|
|
/* Send hellos */
|
|
|
|
if (!(t % ldp_hello_time))
|
|
|
|
send_hello();
|
|
|
|
|
|
|
|
/* Timeout -- */
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers)
|
|
|
|
p->timeout--;
|
|
|
|
|
|
|
|
/* Check for timeout */
|
2011-01-22 22:35:00 +03:00
|
|
|
SLIST_FOREACH_SAFE(p, &ldp_peer_head, peers, ptmp)
|
2010-12-08 10:20:14 +03:00
|
|
|
if (p->timeout < 1)
|
|
|
|
switch (p->state) {
|
|
|
|
case LDP_PEER_HOLDDOWN:
|
|
|
|
debugp("LDP holddown expired for peer %s\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
ldp_peer_delete(p);
|
2011-01-22 22:35:00 +03:00
|
|
|
break;
|
2010-12-08 10:20:14 +03:00
|
|
|
case LDP_PEER_ESTABLISHED:
|
|
|
|
case LDP_PEER_CONNECTED:
|
|
|
|
send_notification(p, 0,
|
2013-01-29 01:35:34 +04:00
|
|
|
NOTIF_FATAL|NOTIF_KEEP_ALIVE_TIMER_EXPIRED);
|
2010-12-08 10:20:14 +03:00
|
|
|
warnp("Keepalive expired for %s\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
break;
|
|
|
|
} /* switch */
|
|
|
|
|
|
|
|
/* send keepalives */
|
2010-12-30 14:29:21 +03:00
|
|
|
if (!(t % ldp_keepalive_time)) {
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers)
|
|
|
|
if (p->state == LDP_PEER_ESTABLISHED) {
|
|
|
|
debugp("Sending KeepAlive to %s\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
keep_alive(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 00:06:52 +04:00
|
|
|
/* Decrement and Check hello keepalives */
|
|
|
|
SLIST_FOREACH_SAFE(hi, &hello_info_head, infos, hinext) {
|
2011-06-16 18:48:30 +04:00
|
|
|
if (hi->keepalive != 0xFFFF)
|
|
|
|
hi->keepalive--;
|
2010-12-30 14:29:21 +03:00
|
|
|
if (hi->keepalive < 1)
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_REMOVE(&hello_info_head, hi, hello_info, infos);
|
2013-01-29 00:06:52 +04:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
/* Set the alarm again and bail out */
|
|
|
|
alarm(1);
|
|
|
|
errno = olderrno;
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:32:36 +04:00
|
|
|
static void
|
2010-12-08 10:20:14 +03:00
|
|
|
bail_out(int x)
|
|
|
|
{
|
|
|
|
ldp_peer_holddown_all();
|
|
|
|
flush_mpls_routes();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The big poll that catches every single event
|
|
|
|
* on every socket.
|
|
|
|
*/
|
2011-06-14 15:28:51 +04:00
|
|
|
int
|
2010-12-08 10:20:14 +03:00
|
|
|
the_big_loop(void)
|
|
|
|
{
|
|
|
|
int sock_error;
|
|
|
|
uint32_t i;
|
|
|
|
socklen_t sock_error_size = sizeof(int);
|
|
|
|
struct ldp_peer *p;
|
|
|
|
struct com_sock *cs;
|
|
|
|
struct pollfd pfd[MAX_POLL_FDS];
|
2013-01-26 21:29:55 +04:00
|
|
|
struct hello_socket *hs;
|
|
|
|
nfds_t pollsum;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
assert(MAX_POLL_FDS > 5);
|
2011-06-14 15:28:51 +04:00
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_INIT(&hello_info_head);
|
|
|
|
|
|
|
|
signal(SIGALRM, send_hello_alarm);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2011-06-16 18:48:30 +04:00
|
|
|
signal(SIGINT, bail_out);
|
2010-12-08 10:20:14 +03:00
|
|
|
signal(SIGTERM, bail_out);
|
2013-01-26 21:29:55 +04:00
|
|
|
|
|
|
|
/* Send first hellos in 5 seconds. Avoid No hello notifications */
|
2013-02-03 23:41:59 +04:00
|
|
|
may_connect = false;
|
2013-01-26 21:29:55 +04:00
|
|
|
alarm(5);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
route_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
|
|
|
|
|
2011-06-14 15:28:51 +04:00
|
|
|
sock_error = bind_current_routes();
|
|
|
|
if (sock_error != LDP_E_OK) {
|
2010-12-08 10:20:14 +03:00
|
|
|
fatalp("Cannot get current routes\n");
|
2011-06-14 15:28:51 +04:00
|
|
|
return sock_error;
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pfd[0].fd = ls;
|
|
|
|
pfd[0].events = POLLRDNORM;
|
|
|
|
pfd[0].revents = 0;
|
|
|
|
|
|
|
|
pfd[1].fd = route_socket;
|
|
|
|
pfd[1].events = POLLRDNORM;
|
|
|
|
pfd[1].revents = 0;
|
|
|
|
|
|
|
|
pfd[2].fd = command_socket;
|
|
|
|
pfd[2].events = POLLRDNORM;
|
|
|
|
pfd[2].revents = 0;
|
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
/* Hello sockets */
|
|
|
|
pollsum = 3;
|
|
|
|
SLIST_FOREACH(hs, &hello_socket_head, listentry) {
|
|
|
|
pfd[pollsum].fd = hs->socket;
|
|
|
|
pfd[pollsum].events = POLLIN;
|
|
|
|
pfd[pollsum].revents = 0;
|
|
|
|
pollsum++;
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
/* Command sockets */
|
|
|
|
for (i=0; i < MAX_COMMAND_SOCKETS; i++)
|
|
|
|
if (csockets[i].socket != -1) {
|
2011-06-14 15:28:51 +04:00
|
|
|
if (pollsum >= MAX_POLL_FDS)
|
|
|
|
break;
|
2010-12-08 10:20:14 +03:00
|
|
|
pfd[pollsum].fd = csockets[i].socket;
|
|
|
|
pfd[pollsum].events = POLLIN;
|
|
|
|
pfd[pollsum].revents = 0;
|
|
|
|
pollsum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* LDP Peer sockets */
|
|
|
|
SLIST_FOREACH(p, &ldp_peer_head, peers) {
|
|
|
|
if (p->socket < 1)
|
|
|
|
continue;
|
|
|
|
switch (p->state) {
|
|
|
|
case LDP_PEER_CONNECTED:
|
|
|
|
case LDP_PEER_ESTABLISHED:
|
2011-06-14 15:28:51 +04:00
|
|
|
if (pollsum >= MAX_POLL_FDS)
|
|
|
|
break;
|
2010-12-08 10:20:14 +03:00
|
|
|
pfd[pollsum].fd = p->socket;
|
|
|
|
pfd[pollsum].events = POLLRDNORM;
|
|
|
|
pfd[pollsum].revents = 0;
|
|
|
|
pollsum++;
|
|
|
|
break;
|
|
|
|
case LDP_PEER_CONNECTING:
|
2011-06-14 15:28:51 +04:00
|
|
|
if (pollsum >= MAX_POLL_FDS)
|
|
|
|
break;
|
2010-12-08 10:20:14 +03:00
|
|
|
pfd[pollsum].fd = p->socket;
|
|
|
|
pfd[pollsum].events = POLLWRNORM;
|
|
|
|
pfd[pollsum].revents = 0;
|
|
|
|
pollsum++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pollsum >= MAX_POLL_FDS) {
|
|
|
|
fatalp("Too many sockets. Increase MAX_POLL_FDS\n");
|
2011-06-14 15:28:51 +04:00
|
|
|
return LDP_E_TOO_MANY_FDS;
|
2011-06-15 17:24:48 +04:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
if (poll(pfd, pollsum, INFTIM) < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
fatalp("poll: %s", strerror(errno));
|
|
|
|
continue;
|
2011-06-15 17:24:48 +04:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
for (i = 0; i < pollsum; i++) {
|
|
|
|
if ((pfd[i].revents & POLLRDNORM) ||
|
|
|
|
(pfd[i].revents & POLLIN)) {
|
2011-06-15 17:24:48 +04:00
|
|
|
if(pfd[i].fd == ls)
|
2010-12-08 10:20:14 +03:00
|
|
|
new_peer_connection();
|
2011-06-15 17:24:48 +04:00
|
|
|
else if (pfd[i].fd == route_socket) {
|
2010-12-08 10:20:14 +03:00
|
|
|
struct rt_msg xbuf;
|
2011-06-15 17:24:48 +04:00
|
|
|
int l;
|
2010-12-08 10:20:14 +03:00
|
|
|
do {
|
2011-06-15 17:24:48 +04:00
|
|
|
l = read(route_socket, &xbuf,
|
|
|
|
sizeof(xbuf));
|
2010-12-08 10:20:14 +03:00
|
|
|
} while ((l == -1) && (errno == EINTR));
|
|
|
|
|
|
|
|
if (l == -1)
|
|
|
|
break;
|
|
|
|
|
2011-06-15 17:24:48 +04:00
|
|
|
check_route(&xbuf, l);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
} else if (is_hello_socket(pfd[i].fd) == 1) {
|
2010-12-08 10:20:14 +03:00
|
|
|
/* Receiving hello socket */
|
|
|
|
recv_pdu(pfd[i].fd);
|
|
|
|
} else if (pfd[i].fd == command_socket) {
|
|
|
|
command_accept(command_socket);
|
|
|
|
} else if ((cs = is_command_socket(pfd[i].fd))
|
|
|
|
!= NULL) {
|
|
|
|
command_dispatch(cs);
|
|
|
|
} else {
|
|
|
|
/* ldp peer socket */
|
|
|
|
p = get_ldp_peer_by_socket(pfd[i].fd);
|
|
|
|
if (p)
|
|
|
|
recv_session_pdu(p);
|
|
|
|
}
|
|
|
|
} else if(pfd[i].revents & POLLWRNORM) {
|
|
|
|
p = get_ldp_peer_by_socket(pfd[i].fd);
|
|
|
|
if (!p)
|
|
|
|
continue;
|
2011-06-16 10:05:47 +04:00
|
|
|
if (getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR,
|
|
|
|
&sock_error, &sock_error_size) != 0 ||
|
|
|
|
sock_error != 0) {
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
sock_error = 0;
|
|
|
|
} else {
|
|
|
|
p->state = LDP_PEER_CONNECTED;
|
|
|
|
send_initialize(p);
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ri = 0; ri < replay_index; ri++) {
|
|
|
|
debugp("Replaying: PID %d, SEQ %d\n",
|
|
|
|
replay_rt[ri].m_rtm.rtm_pid,
|
|
|
|
replay_rt[ri].m_rtm.rtm_seq);
|
|
|
|
check_route(&replay_rt[ri], sizeof(struct rt_msg));
|
|
|
|
}
|
|
|
|
replay_index = 0;
|
|
|
|
} /* for (;;) */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
new_peer_connection()
|
|
|
|
{
|
2013-02-04 21:14:31 +04:00
|
|
|
union sockunion peer_address, my_address;
|
|
|
|
struct in_addr *peer_ldp_id = NULL;
|
|
|
|
struct hello_info *hi;
|
2010-12-08 10:20:14 +03:00
|
|
|
int s;
|
|
|
|
|
2013-02-04 21:14:31 +04:00
|
|
|
s = accept(ls, &peer_address.sa,
|
|
|
|
& (socklen_t) { sizeof(union sockunion) } );
|
2010-12-08 10:20:14 +03:00
|
|
|
if (s < 0) {
|
|
|
|
fatalp("accept: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:14:31 +04:00
|
|
|
if (getsockname(s, &my_address.sa,
|
|
|
|
& (socklen_t) { sizeof(union sockunion) } )) {
|
2010-12-08 10:20:14 +03:00
|
|
|
fatalp("new_peer_connection(): cannot getsockname\n");
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-05 00:28:24 +04:00
|
|
|
|
2013-02-04 21:14:31 +04:00
|
|
|
if (peer_address.sa.sa_family == AF_INET)
|
|
|
|
peer_address.sin.sin_port = 0;
|
|
|
|
else if (peer_address.sa.sa_family == AF_INET6)
|
|
|
|
peer_address.sin6.sin6_port = 0;
|
|
|
|
else {
|
|
|
|
fatalp("Unknown peer address family\n");
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-02-05 00:28:24 +04:00
|
|
|
/* Already peered or in holddown ? */
|
|
|
|
if (get_ldp_peer(&peer_address.sa) != NULL) {
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
warnp("Accepted a connection from %s\n", satos(&peer_address.sa));
|
|
|
|
|
2013-02-04 21:14:31 +04:00
|
|
|
/* Verify if it should connect - XXX: no check for INET6 */
|
|
|
|
if (peer_address.sa.sa_family == AF_INET &&
|
|
|
|
ntohl(peer_address.sin.sin_addr.s_addr) <
|
|
|
|
ntohl(my_address.sin.sin_addr.s_addr)) {
|
2010-12-08 10:20:14 +03:00
|
|
|
fatalp("Peer %s: connect from lower ID\n",
|
2013-02-04 21:14:31 +04:00
|
|
|
satos(&peer_address.sa));
|
2010-12-08 10:20:14 +03:00
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-04 21:14:31 +04:00
|
|
|
|
|
|
|
/* Match hello info in order to get ldp_id */
|
|
|
|
SLIST_FOREACH(hi, &hello_info_head, infos) {
|
|
|
|
if (sockaddr_cmp(&peer_address.sa,
|
|
|
|
&hi->transport_address.sa) == 0) {
|
|
|
|
peer_ldp_id = &hi->ldp_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (peer_ldp_id == NULL) {
|
|
|
|
fatalp("Got connection from %s, but no hello info exists\n",
|
|
|
|
satos(&peer_address.sa));
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
ldp_peer_new(peer_ldp_id, &peer_address.sa, NULL,
|
|
|
|
ldp_holddown_time, s);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send_initialize(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct init_tlv ti;
|
|
|
|
|
|
|
|
ti.type = htons(LDP_INITIALIZE);
|
|
|
|
ti.length = htons(sizeof(struct init_tlv) - TLV_TYPE_LENGTH);
|
|
|
|
ti.messageid = htonl(get_message_id());
|
|
|
|
ti.cs_type = htons(TLV_COMMON_SESSION);
|
|
|
|
ti.cs_len = htons(CS_LEN);
|
|
|
|
ti.cs_version = htons(LDP_VERSION);
|
2010-12-30 14:29:21 +03:00
|
|
|
ti.cs_keepalive = htons(2 * ldp_keepalive_time);
|
2010-12-08 10:20:14 +03:00
|
|
|
ti.cs_adpvlim = 0;
|
|
|
|
ti.cs_maxpdulen = htons(MAX_PDU_SIZE);
|
|
|
|
ti.cs_peeraddress.s_addr = p->ldp_id.s_addr;
|
|
|
|
ti.cs_peeraddrspace = 0;
|
|
|
|
|
|
|
|
send_tlv(p, (struct tlv *) (void *) &ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
keep_alive(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct ka_tlv kt;
|
|
|
|
|
|
|
|
kt.type = htons(LDP_KEEPALIVE);
|
|
|
|
kt.length = htons(sizeof(kt.messageid));
|
|
|
|
kt.messageid = htonl(get_message_id());
|
|
|
|
|
|
|
|
send_tlv(p, (struct tlv *) (void *) &kt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_session_pdu(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct ldp_pdu *rpdu;
|
|
|
|
struct address_tlv *atlv;
|
|
|
|
struct al_tlv *altlv;
|
|
|
|
struct init_tlv *itlv;
|
|
|
|
struct label_map_tlv *lmtlv;
|
|
|
|
struct fec_tlv *fectlv;
|
2011-05-24 17:03:19 +04:00
|
|
|
struct label_tlv *labeltlv;
|
2010-12-08 10:20:14 +03:00
|
|
|
struct notification_tlv *nottlv;
|
|
|
|
struct hello_info *hi;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
int32_t wo = 0;
|
|
|
|
struct tlv *ttmp;
|
|
|
|
unsigned char recvspace[MAX_PDU_SIZE];
|
|
|
|
|
|
|
|
memset(recvspace, 0, MAX_PDU_SIZE);
|
|
|
|
|
|
|
|
c = recv(p->socket, (void *) recvspace, MAX_PDU_SIZE, MSG_PEEK);
|
|
|
|
|
|
|
|
debugp("Ready to read %d bytes\n", c);
|
|
|
|
|
|
|
|
if (c < 1) { /* Session closed */
|
|
|
|
warnp("Error in connection with %s\n", inet_ntoa(p->ldp_id));
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c > MAX_PDU_SIZE) {
|
|
|
|
debugp("Incoming PDU size exceeds MAX_PDU_SIZE !\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c < MIN_PDU_SIZE) {
|
|
|
|
debugp("PDU too small received from peer %s\n", inet_ntoa(p->ldp_id));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rpdu = (struct ldp_pdu *) recvspace;
|
|
|
|
/* XXX: buggy messages may crash the whole thing */
|
|
|
|
c = recv(p->socket, (void *) recvspace,
|
|
|
|
ntohs(rpdu->length) + PDU_VER_LENGTH, MSG_WAITALL);
|
|
|
|
rpdu = (struct ldp_pdu *) recvspace;
|
|
|
|
|
|
|
|
/* Check if it's somehow OK... */
|
|
|
|
if (check_recv_pdu(p, rpdu, c) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
debugp("Read %d bytes, PDU size: %d bytes\n", c, ntohs(rpdu->length));
|
|
|
|
wo = sizeof(struct ldp_pdu);
|
|
|
|
|
|
|
|
while (wo + TLV_TYPE_LENGTH < (uint)c) {
|
|
|
|
|
|
|
|
ttmp = (struct tlv *) (&recvspace[wo]);
|
|
|
|
|
|
|
|
if ((ntohs(ttmp->type) != LDP_KEEPALIVE) &&
|
|
|
|
(ntohs(ttmp->type) != LDP_LABEL_MAPPING)) {
|
|
|
|
debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
|
|
|
|
ntohs(ttmp->type), ntohs(ttmp->length),
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
} else
|
|
|
|
debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
|
|
|
|
ntohs(ttmp->type), ntohs(ttmp->length),
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
|
|
|
|
/* Should we get the message ? */
|
|
|
|
if (p->state != LDP_PEER_ESTABLISHED &&
|
|
|
|
ntohs(ttmp->type) != LDP_INITIALIZE &&
|
2013-01-29 00:32:04 +04:00
|
|
|
ntohs(ttmp->type) != LDP_KEEPALIVE &&
|
|
|
|
ntohs(ttmp->type) != LDP_NOTIFICATION)
|
2010-12-08 10:20:14 +03:00
|
|
|
break;
|
|
|
|
/* The big switch */
|
|
|
|
switch (ntohs(ttmp->type)) {
|
|
|
|
case LDP_INITIALIZE:
|
|
|
|
itlv = (struct init_tlv *)ttmp;
|
|
|
|
/* Check size */
|
|
|
|
if (ntohs(itlv->length) <
|
|
|
|
sizeof(struct init_tlv) - TLV_TYPE_LENGTH) {
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("Bad size\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
send_notification(p, 0,
|
|
|
|
NOTIF_BAD_PDU_LEN | NOTIF_FATAL);
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Check version */
|
|
|
|
if (ntohs(itlv->cs_version) != LDP_VERSION) {
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("Bad version");
|
2010-12-08 10:20:14 +03:00
|
|
|
send_notification(p, ntohl(itlv->messageid),
|
|
|
|
NOTIF_BAD_LDP_VER | NOTIF_FATAL);
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Check if we got any hello from this one */
|
|
|
|
SLIST_FOREACH(hi, &hello_info_head, infos)
|
|
|
|
if (hi->ldp_id.s_addr == rpdu->ldp_id.s_addr)
|
|
|
|
break;
|
|
|
|
if (hi == NULL) {
|
2013-01-26 21:29:55 +04:00
|
|
|
debugp("No hello. Moving peer to holddown\n");
|
2010-12-08 10:20:14 +03:00
|
|
|
send_notification(p, ntohl(itlv->messageid),
|
|
|
|
NOTIF_SESSION_REJECTED_NO_HELLO | NOTIF_FATAL);
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->master) {
|
|
|
|
keep_alive(p);
|
|
|
|
send_initialize(p);
|
|
|
|
} else {
|
|
|
|
p->state = LDP_PEER_ESTABLISHED;
|
|
|
|
p->established_t = time(NULL);
|
|
|
|
keep_alive(p);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recheck here ldp id because we accepted
|
|
|
|
* connection without knowing who is it for sure
|
|
|
|
*/
|
|
|
|
p->ldp_id.s_addr = rpdu->ldp_id.s_addr;
|
|
|
|
|
|
|
|
fatalp("LDP neighbour %s is UP\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
mpls_add_ldp_peer(p);
|
|
|
|
send_addresses(p);
|
|
|
|
send_all_bindings(p);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDP_KEEPALIVE:
|
|
|
|
if ((p->state == LDP_PEER_CONNECTED) && (!p->master)) {
|
|
|
|
p->state = LDP_PEER_ESTABLISHED;
|
|
|
|
p->established_t = time(NULL);
|
|
|
|
fatalp("LDP neighbour %s is UP\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
mpls_add_ldp_peer(p);
|
|
|
|
send_addresses(p);
|
|
|
|
send_all_bindings(p);
|
|
|
|
}
|
|
|
|
p->timeout = p->holdtime;
|
|
|
|
break;
|
|
|
|
case LDP_ADDRESS:
|
|
|
|
/* Add peer addresses */
|
|
|
|
atlv = (struct address_tlv *) ttmp;
|
|
|
|
altlv = (struct al_tlv *) (&atlv[1]);
|
|
|
|
add_ifaddresses(p, altlv);
|
|
|
|
print_bounded_addresses(p);
|
|
|
|
break;
|
|
|
|
case LDP_ADDRESS_WITHDRAW:
|
|
|
|
atlv = (struct address_tlv *) ttmp;
|
|
|
|
altlv = (struct al_tlv *) (&atlv[1]);
|
|
|
|
del_ifaddresses(p, altlv);
|
|
|
|
break;
|
|
|
|
case LDP_LABEL_MAPPING:
|
|
|
|
lmtlv = (struct label_map_tlv *) ttmp;
|
|
|
|
fectlv = (struct fec_tlv *) (&lmtlv[1]);
|
|
|
|
labeltlv = (struct label_tlv *)((unsigned char *)fectlv
|
|
|
|
+ ntohs(fectlv->length) + TLV_TYPE_LENGTH);
|
|
|
|
map_label(p, fectlv, labeltlv);
|
|
|
|
break;
|
|
|
|
case LDP_LABEL_REQUEST:
|
|
|
|
lmtlv = (struct label_map_tlv *) ttmp;
|
|
|
|
fectlv = (struct fec_tlv *) (&lmtlv[1]);
|
|
|
|
switch (request_respond(p, lmtlv, fectlv)) {
|
|
|
|
case LDP_E_BAD_FEC:
|
|
|
|
send_notification(p, ntohl(lmtlv->messageid),
|
|
|
|
NOTIF_UNKNOWN_TLV);
|
|
|
|
break;
|
|
|
|
case LDP_E_BAD_AF:
|
|
|
|
send_notification(p, ntohl(lmtlv->messageid),
|
|
|
|
NOTIF_UNSUPPORTED_AF);
|
|
|
|
break;
|
|
|
|
case LDP_E_NO_SUCH_ROUTE:
|
|
|
|
send_notification(p, ntohl(lmtlv->messageid),
|
|
|
|
NOTIF_NO_ROUTE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDP_LABEL_WITHDRAW:
|
|
|
|
lmtlv = (struct label_map_tlv *) ttmp;
|
|
|
|
fectlv = (struct fec_tlv *) (&lmtlv[1]);
|
|
|
|
if (withdraw_label(p, fectlv) == LDP_E_OK) {
|
|
|
|
/* Send RELEASE */
|
|
|
|
prepare_release(ttmp);
|
|
|
|
send_tlv(p, ttmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDP_LABEL_RELEASE:
|
|
|
|
/*
|
|
|
|
* XXX: we need to make a timed queue...
|
|
|
|
* For now I just assume peers are processing messages
|
|
|
|
* correctly so I just ignore confirmations
|
|
|
|
*/
|
|
|
|
wo = -1; /* Ignore rest of message */
|
|
|
|
break;
|
|
|
|
case LDP_LABEL_ABORT:
|
|
|
|
/* XXX: For now I pretend I can process everything
|
2012-11-12 22:39:00 +04:00
|
|
|
* RFC 5036, Section 3.5.9.1
|
2010-12-08 10:20:14 +03:00
|
|
|
* If an LSR receives a Label Abort Request Message after it
|
|
|
|
* has responded to the Label Request in question with a Label
|
|
|
|
* Mapping message or a Notification message, it ignores the
|
|
|
|
* abort request.
|
|
|
|
*/
|
|
|
|
wo = -1;
|
|
|
|
break;
|
|
|
|
case LDP_NOTIFICATION:
|
|
|
|
nottlv = (struct notification_tlv *) ttmp;
|
|
|
|
nottlv->st_code = ntohl(nottlv->st_code);
|
|
|
|
fatalp("Got notification 0x%X from peer %s\n",
|
|
|
|
nottlv->st_code, inet_ntoa(p->ldp_id));
|
|
|
|
if (nottlv->st_code >> 31) {
|
|
|
|
fatalp("LDP peer %s signalized %s\n",
|
|
|
|
inet_ntoa(p->ldp_id),
|
|
|
|
NOTIF_STR[(nottlv->st_code << 1) >> 1]);
|
|
|
|
ldp_peer_holddown(p);
|
|
|
|
wo = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDP_HELLO:
|
|
|
|
/* No hellos should came on tcp session */
|
|
|
|
wo = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnp("Unknown TLV received from %s\n",
|
|
|
|
inet_ntoa(p->ldp_id));
|
|
|
|
debug_tlv(ttmp);
|
|
|
|
wo = -1;/* discard the rest of the message */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wo < 0) {
|
|
|
|
debugp("Discarding the rest of the message\n");
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
wo += ntohs(ttmp->length) + TLV_TYPE_LENGTH;
|
|
|
|
debugp("WORKED ON %u bytes (Left %d)\n", wo, c - wo);
|
|
|
|
}
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends a pdu, tlv pair to a connected peer */
|
|
|
|
int
|
|
|
|
send_message(struct ldp_peer * p, struct ldp_pdu * pdu, struct tlv * t)
|
|
|
|
{
|
|
|
|
unsigned char sendspace[MAX_PDU_SIZE];
|
|
|
|
|
|
|
|
/* Check if peer is connected */
|
|
|
|
switch (p->state) {
|
|
|
|
case LDP_PEER_CONNECTED:
|
|
|
|
case LDP_PEER_ESTABLISHED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check length validity first */
|
|
|
|
if (ntohs(pdu->length) !=
|
|
|
|
ntohs(t->length) + TLV_TYPE_LENGTH + PDU_PAYLOAD_LENGTH) {
|
|
|
|
fatalp("LDP: TLV - PDU incompability. Message discarded\n");
|
|
|
|
fatalp("LDP: TLV len %d - PDU len %d\n", ntohs(t->length),
|
|
|
|
ntohs(pdu->length));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ntohs(t->length) + PDU_VER_LENGTH > MAX_PDU_SIZE) {
|
|
|
|
fatalp("Message to large discarded\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Arrange them in a buffer and send */
|
|
|
|
memcpy(sendspace, pdu, sizeof(struct ldp_pdu));
|
|
|
|
memcpy(sendspace + sizeof(struct ldp_pdu), t,
|
|
|
|
ntohs(t->length) + TLV_TYPE_LENGTH);
|
|
|
|
|
|
|
|
/* Report keepalives only for DEBUG */
|
|
|
|
if ((ntohs(t->type) != 0x201) && (ntohs(t->type) != 0x400)) {
|
|
|
|
debugp("Sending message type 0x%.4X to %s (size: %d)\n",
|
|
|
|
ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
|
|
|
|
} else
|
|
|
|
/* downgraded from warnp to debugp for now */
|
|
|
|
debugp("Sending message type 0x%.4X to %s (size: %d)\n",
|
|
|
|
ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
|
|
|
|
|
|
|
|
/* Send it finally */
|
|
|
|
return send(p->socket, sendspace,
|
|
|
|
ntohs(pdu->length) + PDU_VER_LENGTH, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encapsulates TLV into a PDU and sends it to a peer
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
send_tlv(struct ldp_peer * p, struct tlv * t)
|
|
|
|
{
|
|
|
|
struct ldp_pdu pdu;
|
|
|
|
|
|
|
|
pdu.version = htons(LDP_VERSION);
|
|
|
|
inet_aton(LDP_ID, &pdu.ldp_id);
|
|
|
|
pdu.label_space = 0;
|
|
|
|
pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH +
|
|
|
|
PDU_PAYLOAD_LENGTH);
|
|
|
|
|
|
|
|
return send_message(p, &pdu, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
send_addresses(struct ldp_peer * p)
|
|
|
|
{
|
|
|
|
struct address_list_tlv *t;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
t = build_address_list_tlv();
|
|
|
|
|
|
|
|
ret = send_tlv(p, (struct tlv *) t);
|
|
|
|
free(t);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|