Home | History | Annotate | Line # | Download | only in ldpd
      1 /* $NetBSD: fsm.c,v 1.16 2022/06/26 17:55:38 riastradh 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 /* Process a hello */
     53 void
     54 run_ldp_hello(const struct ldp_pdu * pduid, const struct hello_tlv * ht,
     55     const struct sockaddr * padd, const struct in_addr * ladd, int mysock,
     56     bool may_connect)
     57 {
     58 	struct ldp_peer *peer = NULL;
     59 	const struct transport_address_tlv *trtlv;
     60 	struct hello_info *hi = NULL;
     61 	union sockunion traddr;
     62 	struct in_addr ldp_id;
     63 
     64 	if ((!pduid) || (!ht))
     65 		return;
     66 
     67 	debugp("Hello received for address: %s\n", inet_ntoa(*ladd));
     68 	debugp("Hello: Type: 0x%.4X Length: %.2d ID: %.8X\n", ht->type,
     69 	    ht->length, ht->messageid);
     70 
     71 	if (ht->length <= 4)	/* Common hello parameters */
     72 		return;
     73 	debugp("Common hello Type: 0x%.4X Length: %.2d"
     74 	    " Hold time: %d\n", ntohs(ht->ch.type), ntohs(ht->ch.length),
     75 	    ht->ch.holdtime);
     76 
     77 	memset(&traddr, 0, sizeof(traddr));
     78 	/* Check transport TLV */
     79 	if (pduid->length - PDU_PAYLOAD_LENGTH -
     80 	    sizeof(struct hello_tlv) >= 8) {
     81 		trtlv = (const struct transport_address_tlv *)(ht + 1);
     82 		if (trtlv->type == htons(TLV_IPV4_TRANSPORT)) {
     83 			traddr.sin.sin_family = AF_INET;
     84 			traddr.sin.sin_len = sizeof(struct sockaddr_in);
     85 			memcpy(&traddr.sin.sin_addr,
     86 			    &trtlv->address, sizeof(struct in_addr));
     87 		} else if (trtlv->type == htons(TLV_IPV6_TRANSPORT)) {
     88 			traddr.sin6.sin6_family = AF_INET6;
     89 			traddr.sin6.sin6_len = sizeof(struct sockaddr_in6);
     90 			memcpy(&traddr.sin6.sin6_addr,
     91 			    &trtlv->address, sizeof(struct in6_addr));
     92 		} else
     93 			warnp("Unknown AF %x for transport address\n",
     94 			    ntohs(trtlv->type));
     95 	} else {
     96 		/* Use LDP ID as transport address */
     97 		traddr.sin.sin_family = AF_INET;
     98 		traddr.sin.sin_len = sizeof(struct sockaddr_in);
     99 		memcpy(&traddr.sin.sin_addr,
    100 		    &pduid->ldp_id, sizeof(struct in_addr));
    101 	}
    102 	/* Add it to hello list or just update timer */
    103 	SLIST_FOREACH(hi, &hello_info_head, infos)
    104 		if (hi->ldp_id.s_addr == pduid->ldp_id.s_addr &&
    105 		    sockaddr_cmp(&hi->transport_address.sa, &traddr.sa) == 0)
    106 			break;
    107 	if (hi == NULL) {
    108 		hi = calloc(1, sizeof(*hi));
    109 		if (!hi) {
    110 			fatalp("Cannot alloc a hello info structure");
    111 			return;
    112 		}
    113 		hi->ldp_id.s_addr = pduid->ldp_id.s_addr;
    114 		memcpy(&hi->transport_address, &traddr, traddr.sa.sa_len);
    115 		SLIST_INSERT_HEAD(&hello_info_head, hi, infos);
    116 		may_connect = false;
    117 	}
    118 
    119 	/* Update expire timer */
    120 	if (ht->ch.holdtime != 0)
    121 		hi->keepalive = ntohs(ht->ch.holdtime);
    122 	else {
    123 		if (ntohs(ht->ch.res) >> 15 == 0)
    124 			hi->keepalive = LDP_HELLO_KEEP;
    125 		else
    126 			hi->keepalive = LDP_THELLO_KEEP;
    127 	}
    128 
    129 	ldp_id = pduid->ldp_id;
    130 	if (!get_ldp_peer_by_id(&ldp_id)) {
    131 		/*
    132 		 * RFC 5036 2.5.2: If A1 > A2, LSR1 plays the active role;
    133 		 * otherwise it is passive.
    134 		 */
    135 		if (may_connect == true &&
    136 		    (hi->transport_address.sa.sa_family == AF_INET &&
    137 		    ntohl(hi->transport_address.sin.sin_addr.s_addr) <
    138 		    ntohl(ladd->s_addr))) {
    139 			peer = ldp_peer_new(&ldp_id, padd,
    140 				&hi->transport_address.sa,
    141 				ntohs(ht->ch.holdtime), 0);
    142 			if (peer == NULL)
    143 				return;
    144 			if (peer->state == LDP_PEER_CONNECTED)
    145 				send_initialize(peer);
    146 		}
    147 	}
    148 }
    149 
    150 struct address_list_tlv *
    151 build_address_list_tlv(void)
    152 {
    153 	struct address_list_tlv *t;
    154 	struct ifaddrs *ifa, *ifb;
    155 	struct sockaddr_in *sa;
    156 	char *ia;
    157 	uint16_t       adrcount = 0;
    158 
    159 	if (getifaddrs(&ifa) == -1)
    160 		return NULL;
    161 
    162 	/* Find out the number of addresses */
    163 	/* Ignore loopback */
    164 	for (ifb = ifa; ifb; ifb = ifb->ifa_next)
    165 		if ((ifb->ifa_addr->sa_family == AF_INET) &&
    166 		    (ifb->ifa_flags & IFF_UP)) {
    167 			sa = (struct sockaddr_in *) ifb->ifa_addr;
    168 			if (ntohl(sa->sin_addr.s_addr) >> 24 != IN_LOOPBACKNET)
    169 				adrcount++;
    170 		}
    171 	t = malloc(sizeof(*t) + (adrcount - 1) * sizeof(struct in_addr));
    172 
    173 	if (!t) {
    174 		fatalp("build_address_list_tlv: malloc problem\n");
    175 		freeifaddrs(ifa);
    176 		return NULL;
    177 	}
    178 
    179 	t->type = htons(LDP_ADDRESS);
    180 	t->length = htons(sizeof(struct address_list_tlv) - TLV_TYPE_LENGTH
    181 			  + (adrcount - 1) * sizeof(struct in_addr));
    182 	t->messageid = htonl(get_message_id());
    183 
    184 	t->a_type = htons(TLV_ADDRESS_LIST);
    185 	t->a_length = htons(sizeof(t->a_af) +
    186 	    adrcount * sizeof(struct in_addr));
    187 	t->a_af = htons(LDP_AF_INET);
    188 
    189 	ia = (void *)&t->a_address;
    190 	for (adrcount = 0, ifb = ifa; ifb; ifb = ifb->ifa_next) {
    191 		if ((ifb->ifa_addr->sa_family != AF_INET) ||
    192 		    (!(ifb->ifa_flags & IFF_UP)))
    193 			continue;
    194 		sa = (struct sockaddr_in *) ifb->ifa_addr;
    195 		if (ntohl(sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET)
    196 			continue;
    197 		memcpy(ia + adrcount*sizeof(struct in_addr), &sa->sin_addr,
    198 		    sizeof(struct in_addr));
    199 		adrcount++;
    200 	}
    201 	freeifaddrs(ifa);
    202 
    203 	return t;
    204 }
    205 
    206 /*
    207  * Calculate LDP ID
    208  * Get also mpls pseudo-interface address
    209  */
    210 int
    211 set_my_ldp_id()
    212 {
    213 	struct ifaddrs *ifa, *ifb;
    214 	struct in_addr  a;
    215 	struct sockaddr_in *sa;
    216 
    217 	a.s_addr = 0;
    218 	my_ldp_id[0] = '\0';
    219 	mplssockaddr.sa_len = 0;
    220 
    221 	if (getifaddrs(&ifa) == -1)
    222 		return LDP_E_GENERIC;
    223 
    224 	for (ifb = ifa; ifb; ifb = ifb->ifa_next)
    225 		if(ifb->ifa_flags & IFF_UP) {
    226 			if (strncmp("mpls", ifb->ifa_name, 4) == 0 &&
    227 			    ifb->ifa_addr->sa_family == AF_LINK)
    228 				memcpy(&mplssockaddr, ifb->ifa_addr,
    229 				    ifb->ifa_addr->sa_len);
    230 
    231 			if (ifb->ifa_addr->sa_family != AF_INET)
    232 				continue;
    233 
    234 			sa = (struct sockaddr_in *) ifb->ifa_addr;
    235 			if (ntohl(sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET)
    236 				continue;	/* No 127/8 */
    237 			if (ntohl(sa->sin_addr.s_addr) > ntohl(a.s_addr))
    238 				a.s_addr = sa->sin_addr.s_addr;
    239 		}
    240 	freeifaddrs(ifa);
    241 	debugp("LDP ID: %s\n", inet_ntoa(a));
    242 	strlcpy(my_ldp_id, inet_ntoa(a), INET_ADDRSTRLEN);
    243 	setproctitle("LDP ID: %s", my_ldp_id);
    244 	return LDP_E_OK;
    245 }
    246