Home | History | Annotate | Line # | Download | only in ldpd
fsm.c revision 1.6
      1 /* $NetBSD: fsm.c,v 1.6 2012/11/12 18:39:00 kefren Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/socket.h>
     34 #include <arpa/inet.h>
     35 #include <netinet/in.h>
     36 #include <net/if.h>
     37 
     38 #include <ifaddrs.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <strings.h>
     42 
     43 #include "ldp.h"
     44 #include "ldp_peer.h"
     45 #include "socketops.h"
     46 #include "ldp_errors.h"
     47 #include "fsm.h"
     48 
     49 char            my_ldp_id[20];
     50 struct sockaddr	mplssockaddr;
     51 
     52 /* Processing a hello */
     53 void
     54 run_ldp_hello(struct ldp_pdu * pduid, struct hello_tlv * ht,
     55     struct in_addr * padd, struct in_addr * ladd, int mysock)
     56 {
     57 	struct ldp_peer *peer = NULL;
     58 	struct in_addr  peer_addr;
     59 	struct in6_addr	peer_addr6;
     60 	struct transport_address_tlv *trtlv;
     61 	struct hello_info *hi;
     62 
     63 	if ((!pduid) || (!ht))
     64 		return;
     65 
     66 	debugp("Hello received for address: %s\n", inet_ntoa(*ladd));
     67 	debugp("Hello: Type: 0x%.4X Length: %.2d ID: %.8X\n", ht->type,
     68 	    ht->length, ht->messageid);
     69 
     70 	/* Add it to hello list or just update timer */
     71 	SLIST_FOREACH(hi, &hello_info_head, infos)
     72 		if (hi->ldp_id.s_addr == pduid->ldp_id.s_addr)
     73 			break;
     74 	if (hi == NULL) {
     75 		hi = malloc(sizeof(*hi));
     76 		if (!hi) {
     77 			fatalp("Cannot alloc a hello info structure");
     78 			return;
     79 		}
     80 		hi->ldp_id.s_addr = pduid->ldp_id.s_addr;
     81 		SLIST_INSERT_HEAD(&hello_info_head, hi, infos);
     82 	} else
     83 		/* Just update timer */
     84 		hi->keepalive = LDP_HELLO_KEEP;
     85 
     86 	if (ht->length <= 4)	/* Common hello parameters */
     87 		return;
     88 	ht->ch.type = ntohs(ht->ch.type);
     89 	ht->ch.length = ntohs(ht->ch.length);
     90 	ht->ch.holdtime = ntohs(ht->ch.holdtime);
     91 	ht->ch.res = ntohs(ht->ch.res);
     92 	debugp("Common hello Type: 0x%.4X Length: %.2d R:%d T:%d"
     93 	    "Hold time: %d\n", ht->ch.type, ht->ch.length,
     94 	    ht->ch.tr / 2, ht->ch.tr % 2, ht->ch.holdtime);
     95 	if (ht->ch.holdtime != 0)
     96 		hi->keepalive = ht->ch.holdtime;
     97 	else {
     98 		if (ht->ch.res >> 15 == 0)
     99 			hi->keepalive = LDP_HELLO_KEEP;
    100 		else
    101 			hi->keepalive = LDP_THELLO_KEEP;
    102 	}
    103 	if (!get_ldp_peer(&pduid->ldp_id)) {
    104 		/* First of all set peer_addr to announced LDP_ID */
    105 		memcpy(&peer_addr, &pduid->ldp_id,
    106 		    sizeof(struct in_addr));
    107 		/*
    108 		 * Now let's see if there is any transport TLV in
    109 		 * there
    110 		 */
    111 		if (pduid->length - PDU_PAYLOAD_LENGTH -
    112 		    sizeof(struct hello_tlv) > 3) {
    113 			trtlv = (struct transport_address_tlv *) &ht[1];
    114 			if (trtlv->type == TLV_IPV4_TRANSPORT)
    115 				memcpy(&peer_addr, &trtlv->address,
    116 				    sizeof(struct in_addr));
    117 			else if (trtlv->type == TLV_IPV6_TRANSPORT)
    118 				memcpy(&peer_addr6, &trtlv->address,
    119 				    sizeof(struct in6_addr));
    120 		} else
    121 			trtlv = NULL;
    122 		/*
    123 		 * RFC 5036 2.5.2: If A1 > A2, LSR1 plays the active role;
    124 		 * otherwise it is passive.
    125 		 */
    126 		if (ntohl(peer_addr.s_addr) < ntohl(ladd->s_addr)) {
    127 #define	TR_INET4_ADDR (trtlv && trtlv->type == TLV_IPV4_TRANSPORT) ? &peer_addr : NULL
    128 #define TR_INET6_ADDR NULL
    129 			peer = ldp_peer_new(&pduid->ldp_id, padd,
    130 				TR_INET4_ADDR, TR_INET6_ADDR, ht->ch.holdtime, 0);
    131 			if (!peer)
    132 				return;
    133 			if (peer && peer->state == LDP_PEER_CONNECTED)
    134 				send_initialize(peer);
    135 		}
    136 	}
    137 }
    138 
    139 struct address_list_tlv *
    140 build_address_list_tlv(void)
    141 {
    142 	struct address_list_tlv *t;
    143 	struct ifaddrs *ifa, *ifb;
    144 	struct sockaddr_in *sa;
    145 	struct in_addr *ia;
    146 	uint16_t       adrcount = 0;
    147 
    148 	if (getifaddrs(&ifa) == -1)
    149 		return NULL;
    150 
    151 	/* Find out the number of addresses */
    152 	/* Ignore loopback */
    153 	for (ifb = ifa; ifb; ifb = ifb->ifa_next)
    154 		if ((ifb->ifa_addr->sa_family == AF_INET) &&
    155 		    (ifb->ifa_flags & IFF_UP)) {
    156 			sa = (struct sockaddr_in *) ifb->ifa_addr;
    157 			if (ntohl(sa->sin_addr.s_addr) >> 24 != IN_LOOPBACKNET)
    158 				adrcount++;
    159 		}
    160 	t = malloc(sizeof(*t) + (adrcount - 1) * sizeof(struct in_addr));
    161 
    162 	if (!t) {
    163 		fatalp("build_address_list_tlv: malloc problem\n");
    164 		freeifaddrs(ifa);
    165 		return NULL;
    166 	}
    167 
    168 	t->type = htons(LDP_ADDRESS);
    169 	t->length = htons(sizeof(struct address_list_tlv) - TLV_TYPE_LENGTH
    170 			  + (adrcount - 1) * sizeof(struct in_addr));
    171 	t->messageid = htonl(get_message_id());
    172 
    173 	t->a_type = htons(TLV_ADDRESS_LIST);
    174 	t->a_length = htons(sizeof(t->a_af) +
    175 	    adrcount * sizeof(struct in_addr));
    176 	t->a_af = htons(LDP_AF_INET);
    177 
    178 	ia = &t->a_address;
    179 	for (adrcount = 0, ifb = ifa; ifb; ifb = ifb->ifa_next) {
    180 		if ((ifb->ifa_addr->sa_family != AF_INET) ||
    181 		    (!(ifb->ifa_flags & IFF_UP)) ||
    182 		    (ifb->ifa_flags & IFF_LOOPBACK))
    183 			continue;
    184 		sa = (struct sockaddr_in *) ifb->ifa_addr;
    185 		memcpy(&ia[adrcount], &sa->sin_addr, sizeof(struct in_addr));
    186 		adrcount++;
    187 	}
    188 	freeifaddrs(ifa);
    189 
    190 	add_my_if_addrs(ia, adrcount);
    191 	return t;
    192 }
    193 
    194 /*
    195  * Calculate LDP ID
    196  * Get also mpls pseudo-interface address
    197  */
    198 int
    199 set_my_ldp_id()
    200 {
    201 	struct ifaddrs *ifa, *ifb;
    202 	struct in_addr  a;
    203 	struct sockaddr_in *sa;
    204 
    205 	a.s_addr = 0;
    206 	my_ldp_id[0] = '\0';
    207 	mplssockaddr.sa_len = 0;
    208 
    209 	if (getifaddrs(&ifa) == -1)
    210 		return LDP_E_GENERIC;
    211 
    212 	for (ifb = ifa; ifb; ifb = ifb->ifa_next)
    213 		if(ifb->ifa_flags & IFF_UP) {
    214 			if (strncmp("mpls", ifb->ifa_name, 4) == 0 &&
    215 			    ifb->ifa_addr->sa_family == AF_LINK)
    216 				memcpy(&mplssockaddr, ifb->ifa_addr,
    217 				    ifb->ifa_addr->sa_len);
    218 
    219 			if (ifb->ifa_addr->sa_family != AF_INET)
    220 				continue;
    221 
    222 			sa = (struct sockaddr_in *) ifb->ifa_addr;
    223 			if (ntohl(sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET)
    224 				continue;	/* No 127/8 */
    225 			if (ntohl(sa->sin_addr.s_addr) > ntohl(a.s_addr))
    226 				a.s_addr = sa->sin_addr.s_addr;
    227 		}
    228 	freeifaddrs(ifa);
    229 	debugp("LDP ID: %s\n", inet_ntoa(a));
    230 	strlcpy(my_ldp_id, inet_ntoa(a), INET_ADDRSTRLEN);
    231 	return LDP_E_OK;
    232 }
    233