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