Skip to content

Commit

Permalink
Add ldpd, a RFC 3036 compatible LDP speaker.
Browse files Browse the repository at this point in the history
  • Loading branch information
kefren committed Dec 8, 2010
1 parent 0b18eab commit 10a10e5
Show file tree
Hide file tree
Showing 31 changed files with 5,890 additions and 2 deletions.
4 changes: 2 additions & 2 deletions usr.sbin/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.248 2010/12/05 05:59:17 christos Exp $
# $NetBSD: Makefile,v 1.249 2010/12/08 07:20:14 kefren Exp $
# from: @(#)Makefile 5.20 (Berkeley) 6/12/93

.include <bsd.own.mk>
Expand All @@ -11,7 +11,7 @@ SUBDIR= ac accton acpitools altq apm apmd arp bad144 bootp \
gpioctl grfconfig grfinfo gspa hdaudioctl hilinfo ifwatchd inetd \
installboot \
iopctl iostat ipwctl irdaattach isdn iteconfig iwictl\
kgmon lastlogin link lmcconfig lockstat lpr mailwrapper makefs \
kgmon lastlogin ldpd link lmcconfig lockstat lpr mailwrapper makefs \
map-mbone mdconfig memswitch mlxctl mmcformat mopd mountd moused \
mrinfo mrouted mscdlabel mtrace \
mtree ndbootd ndiscvt netgroup_mkdb nfsd ofctl paxctl pcictl \
Expand Down
26 changes: 26 additions & 0 deletions usr.sbin/ldpd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# $NetBSD: Makefile,v 1.1 2010/12/08 07:20:14 kefren Exp $

.include <bsd.own.mk>

PROG= ldpd
MAN= ldpd.8

SRCS= fsm.c \
label.c \
ldp_command.c \
ldp_errors.c \
ldp_peer.c \
main.c \
mpls_interface.c \
mpls_routes.c \
notifications.c \
pdu.c \
socketops.c \
tlv.c \
tlv_stack.c

CFLAGS= -Wall -g

LDADD+= -lcrypt

.include <bsd.prog.mk>
12 changes: 12 additions & 0 deletions usr.sbin/ldpd/TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# $NetBSD: TODO,v 1.1 2010/12/08 07:20:14 kefren Exp $

TODO
====

* send notifications for every error I encounter - kefren
* document better Label Distribution (downstream on demand or
unsolicited downstream), distribution control (independent or
ordered) and retention mode (liberal or conservative) - kefren
* config/options file
* future: IPv6 support. Have no infrastructure to test right
now - kefren
220 changes: 220 additions & 0 deletions usr.sbin/ldpd/fsm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/* $NetBSD: fsm.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */

/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Mihai Chelaru <[email protected]>
*
* 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 <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>

#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include "ldp.h"
#include "ldp_peer.h"
#include "socketops.h"
#include "ldp_errors.h"
#include "fsm.h"

char my_ldp_id[20];
struct sockaddr mplssockaddr;

/* Processing a hello */
void
run_ldp_hello(struct ldp_pdu * pduid, struct hello_tlv * ht,
struct in_addr * padd, struct in_addr * ladd, int mysock)
{
struct ldp_peer *peer = NULL;
struct in_addr peer_addr;
struct transport_address_tlv *trtlv;
struct hello_info *hi;

if ((!pduid) || (!ht))
return;

debugp("Received it on address: %s\n", inet_ntoa(*ladd));
debugp("Hello: Type: 0x%.4X Length: %.2d ID: %.8X\n", ht->type,
ht->length, ht->messageid);

/* Add it to hello list or just update timer */
SLIST_FOREACH(hi, &hello_info_head, infos)
if (hi->ldp_id.s_addr == pduid->ldp_id.s_addr)
break;
if (hi == NULL) {
hi = (struct hello_info *)malloc(sizeof(struct hello_info));
if (!hi) {
fatalp("Cannot alloc a hello info");
return;
}
hi->ldp_id.s_addr = pduid->ldp_id.s_addr;
SLIST_INSERT_HEAD(&hello_info_head, hi, infos);
} else
/* Just update timer */
hi->keepalive = LDP_HELLO_KEEP;

if (ht->length > 4) { /* Common hello parameters */
ht->ch.type = ntohs(ht->ch.type);
ht->ch.length = ntohs(ht->ch.length);
ht->ch.holdtime = ntohs(ht->ch.holdtime);
ht->ch.res = ntohs(ht->ch.res);
debugp("Common hello Type: 0x%.4X Length: %.2d R:%d T:%d"
"Hold time: %d\n", ht->ch.type, ht->ch.length,
ht->ch.tr / 2, ht->ch.tr % 2, ht->ch.holdtime);
if (ht->ch.holdtime)
hi->keepalive = ht->ch.holdtime;
if (!get_ldp_peer(&pduid->ldp_id)) {
/* First of all set peer_addr to announced LDP_ID */
memcpy(&peer_addr, &pduid->ldp_id,
sizeof(struct in_addr));
/*
* Now let's see if there is any transport TLV in
* there
*/
if (pduid->length - PDU_PAYLOAD_LENGTH -
sizeof(struct hello_tlv) > 3) {
trtlv = (struct transport_address_tlv *) &ht[1];
if (trtlv->type == TLV_IPV4_TRANSPORT)
memcpy(&peer_addr, &trtlv->address,
sizeof(struct in_addr));
}
/*
* RFC says: If A1 > A2, LSR1 plays the active role;
* otherwise it is passive.
*/
if (ntohl(peer_addr.s_addr) < ntohl(ladd->s_addr)) {
#define TRADDR (trtlv && trtlv->type == TLV_IPV4_TRANSPORT) ? &peer_addr : NULL
peer = ldp_peer_new(&pduid->ldp_id, padd,
TRADDR, ht->ch.holdtime, 0);
if (!peer)
return;
if (peer && peer->state == LDP_PEER_CONNECTED)
send_initialize(peer);
}
}
}
}

struct address_list_tlv *
build_address_list_tlv(void)
{
struct address_list_tlv *t;
struct ifaddrs *ifa, *ifb;
struct sockaddr_in *sa;
struct in_addr *ia;
uint16_t adrcount = 0;

if (getifaddrs(&ifa) == -1)
return NULL;

/* Find out the number of addresses */
/* Ignore loopback */
for (ifb = ifa; ifb; ifb = ifb->ifa_next)
if ((ifb->ifa_addr->sa_family == AF_INET) &&
(ifb->ifa_flags & IFF_UP)) {
sa = (struct sockaddr_in *) ifb->ifa_addr;
if (sa->sin_addr.s_addr << 24 >> 24 != 127)
adrcount++;
}
t = (struct address_list_tlv *) malloc(sizeof(struct address_list_tlv)
+ (adrcount - 1) * sizeof(struct in_addr));

if (!t) {
fatalp("build_address_list_tlv: malloc problem\n");
return NULL;
}

t->type = htons(LDP_ADDRESS);
t->length = htons(sizeof(struct address_list_tlv) - TLV_TYPE_LENGTH
+ (adrcount - 1) * sizeof(struct in_addr));
t->messageid = htonl(get_message_id());

t->a_type = htons(TLV_ADDRESS_LIST);
t->a_length = htons(sizeof(t->a_af) +
adrcount * sizeof(struct in_addr));
t->a_af = htons(LDP_AF_INET);

ia = &t->a_address;
for (adrcount = 0, ifb = ifa; ifb; ifb = ifb->ifa_next) {
if ((ifb->ifa_addr->sa_family != AF_INET) ||
(!(ifb->ifa_flags & IFF_UP)) ||
(ifb->ifa_flags & IFF_LOOPBACK))
continue;
sa = (struct sockaddr_in *) ifb->ifa_addr;
memcpy(&ia[adrcount], &sa->sin_addr, sizeof(struct in_addr));
adrcount++;
}
freeifaddrs(ifa);

add_my_if_addrs(ia, adrcount);
return t;
}

/*
* Calculate LDP ID
* Get also mpls pseudo-interface address
*/
int
set_my_ldp_id()
{
struct ifaddrs *ifa, *ifb;
struct in_addr a;
struct sockaddr_in *sa;

a.s_addr = 0;
my_ldp_id[0] = 0;
mplssockaddr.sa_len = 0;

if (getifaddrs(&ifa) == -1)
return LDP_E_GENERIC;

for (ifb = ifa; ifb; ifb = ifb->ifa_next)
if(ifb->ifa_flags & IFF_UP) {
if (strncmp("mpls", ifb->ifa_name, 4) == 0 &&
ifb->ifa_addr->sa_family == AF_LINK)
memcpy(&mplssockaddr, ifb->ifa_addr,
ifb->ifa_addr->sa_len);

if (ifb->ifa_addr->sa_family != AF_INET)
continue;

sa = (struct sockaddr_in *) ifb->ifa_addr;
if (ntohl(sa->sin_addr.s_addr) >> 24 == 127)
continue; /* No 127/8 */
if (ntohl(sa->sin_addr.s_addr) > ntohl(a.s_addr))
a.s_addr = sa->sin_addr.s_addr;
}
freeifaddrs(ifa);
debugp("LDP ID: %s\n", inet_ntoa(a));
strlcpy(my_ldp_id, inet_ntoa(a), INET_ADDRSTRLEN);
return LDP_E_OK;
}
43 changes: 43 additions & 0 deletions usr.sbin/ldpd/fsm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* $NetBSD: fsm.h,v 1.1 2010/12/08 07:20:14 kefren Exp $ */

/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Mihai Chelaru <[email protected]>
*
* 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.
*/

#ifndef _FSM_H_
#define _FSM_H_

#include "tlv.h"
#include "pdu.h"

void run_ldp_hello(struct ldp_pdu *, struct hello_tlv *,
struct in_addr *, struct in_addr *, int);
struct address_list_tlv * build_address_list_tlv(void);
int set_my_ldp_id(void);

#endif /* !_FSM_H_ */
Loading

0 comments on commit 10a10e5

Please sign in to comment.