Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin Add ldpd, a RFC 3036 compatible LDP speaker.



details:   https://anonhg.NetBSD.org/src/rev/f095d393b8a7
branches:  trunk
changeset: 759400:f095d393b8a7
user:      kefren <kefren%NetBSD.org@localhost>
date:      Wed Dec 08 07:20:14 2010 +0000

description:
Add ldpd, a RFC 3036 compatible LDP speaker.

diffstat:

 usr.sbin/Makefile              |     4 +-
 usr.sbin/ldpd/Makefile         |    26 +
 usr.sbin/ldpd/TODO             |    12 +
 usr.sbin/ldpd/fsm.c            |   220 ++++++++
 usr.sbin/ldpd/fsm.h            |    43 +
 usr.sbin/ldpd/label.c          |   279 ++++++++++
 usr.sbin/ldpd/label.h          |    73 ++
 usr.sbin/ldpd/ldp.d            |    50 +
 usr.sbin/ldpd/ldp.h            |    79 ++
 usr.sbin/ldpd/ldp_command.c    |   564 +++++++++++++++++++++
 usr.sbin/ldpd/ldp_command.h    |    79 ++
 usr.sbin/ldpd/ldp_errors.c     |   110 ++++
 usr.sbin/ldpd/ldp_errors.h     |    58 ++
 usr.sbin/ldpd/ldp_peer.c       |   498 ++++++++++++++++++
 usr.sbin/ldpd/ldp_peer.h       |   118 ++++
 usr.sbin/ldpd/ldpd.8           |    94 +++
 usr.sbin/ldpd/main.c           |   146 +++++
 usr.sbin/ldpd/mpls_interface.c |   226 ++++++++
 usr.sbin/ldpd/mpls_interface.h |    43 +
 usr.sbin/ldpd/mpls_routes.c    |   893 +++++++++++++++++++++++++++++++++
 usr.sbin/ldpd/mpls_routes.h    |    78 ++
 usr.sbin/ldpd/notifications.c  |    79 ++
 usr.sbin/ldpd/notifications.h  |    98 +++
 usr.sbin/ldpd/pdu.c            |    90 +++
 usr.sbin/ldpd/pdu.h            |    58 ++
 usr.sbin/ldpd/socketops.c      |  1055 ++++++++++++++++++++++++++++++++++++++++
 usr.sbin/ldpd/socketops.h      |    69 ++
 usr.sbin/ldpd/tlv.c            |    75 ++
 usr.sbin/ldpd/tlv.h            |   206 +++++++
 usr.sbin/ldpd/tlv_stack.c      |   415 +++++++++++++++
 usr.sbin/ldpd/tlv_stack.h      |    54 ++
 31 files changed, 5890 insertions(+), 2 deletions(-)

diffs (truncated from 6026 to 300 lines):

diff -r 6394586bf4a7 -r f095d393b8a7 usr.sbin/Makefile
--- a/usr.sbin/Makefile Wed Dec 08 03:57:24 2010 +0000
+++ b/usr.sbin/Makefile Wed Dec 08 07:20:14 2010 +0000
@@ -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>
@@ -11,7 +11,7 @@
        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 \
diff -r 6394586bf4a7 -r f095d393b8a7 usr.sbin/ldpd/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ldpd/Makefile    Wed Dec 08 07:20:14 2010 +0000
@@ -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>
diff -r 6394586bf4a7 -r f095d393b8a7 usr.sbin/ldpd/TODO
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ldpd/TODO        Wed Dec 08 07:20:14 2010 +0000
@@ -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
diff -r 6394586bf4a7 -r f095d393b8a7 usr.sbin/ldpd/fsm.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ldpd/fsm.c       Wed Dec 08 07:20:14 2010 +0000
@@ -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 <kefren%NetBSD.org@localhost>
+ *
+ * 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;
+}
diff -r 6394586bf4a7 -r f095d393b8a7 usr.sbin/ldpd/fsm.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ldpd/fsm.h       Wed Dec 08 07:20:14 2010 +0000
@@ -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 <kefren%NetBSD.org@localhost>



Home | Main Index | Thread Index | Old Index