1 /* $NetBSD: socketops.c,v 1.15 2012/11/13 06:58:58 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@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/stat.h> 34 #include <sys/socket.h> 35 #include <sys/ioctl.h> 36 #include <net/if.h> 37 #include <netinet/in.h> 38 #include <arpa/inet.h> 39 40 #include <assert.h> 41 #include <errno.h> 42 #include <ifaddrs.h> 43 #include <poll.h> 44 #include <signal.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <strings.h> 48 #include <unistd.h> 49 50 #include "fsm.h" 51 #include "ldp.h" 52 #include "ldp_command.h" 53 #include "tlv.h" 54 #include "ldp_peer.h" 55 #include "notifications.h" 56 #include "tlv_stack.h" 57 #include "mpls_interface.h" 58 #include "label.h" 59 #include "mpls_routes.h" 60 #include "ldp_errors.h" 61 #include "socketops.h" 62 63 int ls; /* TCP listening socket on port 646 */ 64 int route_socket; /* used to see when a route is added/deleted */ 65 int hello_socket; /* hello RX/TX multicast sockets */ 66 #ifdef INET6 67 int hello6_socket; /* same as above */ 68 #endif 69 int command_socket; /* Listening socket for interface command */ 70 int current_msg_id = 0x233; 71 int command_port = LDP_COMMAND_PORT; 72 extern int replay_index; 73 extern struct rt_msg replay_rt[REPLAY_MAX]; 74 extern struct com_sock csockets[MAX_COMMAND_SOCKETS]; 75 76 int ldp_hello_time = LDP_HELLO_TIME; 77 int ldp_keepalive_time = LDP_KEEPALIVE_TIME; 78 int ldp_holddown_time = LDP_HOLDTIME; 79 int no_default_route = 1; 80 int loop_detection = 0; 81 82 void recv_pdu(int); 83 void send_hello_alarm(int); 84 __dead static void bail_out(int); 85 static int bind_socket(int s, uint8_t stype); 86 static int set_mcast_ttl(int, int); 87 static int set_tos(int); 88 static int socket_reuse_port(int); 89 static int get_local_addr(struct sockaddr_dl *, struct in_addr *); 90 91 int 92 create_hello_sockets() 93 { 94 struct ip_mreq mcast_addr; 95 int s; 96 #ifdef INET6 97 struct ipv6_mreq mcast_addr6; 98 int s6; 99 #endif 100 101 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 102 if (s < 0) 103 return s; 104 #ifdef INET6 105 s6 = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); 106 if (s6 < 0) { 107 close(s); 108 return s6; 109 } 110 #endif 111 112 /* 113 * RFC5036 specifies we should listen to all subnet routers multicast 114 * group 115 */ 116 assert(inet_pton(AF_INET, ALL_ROUTERS, &mcast_addr.imr_multiaddr) == 1); 117 mcast_addr.imr_interface.s_addr = htonl(INADDR_ANY); 118 119 #ifdef INET6 120 assert(inet_pton(AF_INET6, ALL_ROUTERS6, 121 &mcast_addr6.ipv6mr_multiaddr) == 1); 122 /* 123 * XXXXX: kefren. This should be 0, else I should create a socket 124 * for every interface. Need to investigate why is not working 125 * as documented in ip6(4) 126 */ 127 mcast_addr6.ipv6mr_interface = 1; 128 #endif 129 130 if (socket_reuse_port(s) < 0) 131 goto chs_error; 132 #ifdef INET6 133 if (socket_reuse_port(s6) < 0) 134 goto chs_error; 135 #endif 136 137 /* Bind it to port 646 on specific address */ 138 if (bind_socket(s, 4) == -1) { 139 warnp("Cannot bind INET hello socket\n"); 140 goto chs_error; 141 } 142 #ifdef INET6 143 if (bind_socket(s6, 6) == -1) { 144 fatalp("Cannot bind INET6 hello socket\n"); 145 goto chs_error; 146 } 147 #endif 148 149 /* We don't need to receive back our messages */ 150 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &(uint8_t){0}, 151 sizeof(uint8_t)) == -1) { 152 fatalp("INET setsockopt IP_MCAST_LOOP: %s\n", strerror(errno)); 153 goto chs_error; 154 } 155 #ifdef INET6 156 if (setsockopt(s6, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &(uint32_t){0}, 157 sizeof(uint32_t)) == -1) { 158 fatalp("INET6 setsocketopt IP_MCAST_LOOP: %s\n", 159 strerror(errno)); 160 goto chs_error; 161 } 162 #endif 163 164 /* Finally join the group */ 165 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mcast_addr, 166 sizeof(mcast_addr)) == -1) { 167 fatalp("setsockopt ADD_MEMBER: %s\n", strerror(errno)); 168 goto chs_error; 169 } 170 #ifdef INET6 171 if (setsockopt(s6, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mcast_addr6, 172 sizeof(mcast_addr6)) == -1) { 173 fatalp("INET6 setsockopt JOIN: %s\n", strerror(errno)); 174 goto chs_error; 175 } 176 #endif 177 /* TTL:1, TOS: 0xc0 */ 178 if (set_mcast_ttl(s, 4) == -1) 179 goto chs_error; 180 #ifdef INET6 181 if (set_mcast_ttl(s6, 6) == -1) 182 goto chs_error; 183 #endif 184 if (set_tos(s) == -1) { 185 fatalp("set_tos: %s", strerror(errno)); 186 goto chs_error; 187 } 188 189 /* we need to get the input interface for message processing */ 190 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &(uint32_t){1}, sizeof(uint32_t)) == -1) { 191 fatalp("Cannot set IP_RECVIF\n"); 192 goto chs_error; 193 } 194 #ifdef INET6 195 if (setsockopt(s6, IPPROTO_IPV6, IPV6_RECVPKTINFO, &(uint32_t){1}, sizeof(uint32_t)) == -1) 196 goto chs_error; 197 #endif 198 199 hello_socket = s; 200 #ifdef INET6 201 hello6_socket = s6; 202 #endif 203 204 return 0; 205 chs_error: 206 close(s); 207 #ifdef INET6 208 close(s6); 209 #endif 210 return -1; 211 } 212 213 /* Sets the TTL to 1 as we don't want to transmit outside this subnet */ 214 int 215 set_ttl(int s) 216 { 217 int ret; 218 if ((ret = setsockopt(s, IPPROTO_IP, IP_TTL, &(int){1}, sizeof(int))) 219 == -1) 220 fatalp("set_ttl: %s", strerror(errno)); 221 return ret; 222 } 223 224 /* Sets multicast TTL to 1 */ 225 static int 226 set_mcast_ttl(int s, int stype) 227 { 228 int ret; 229 230 assert(stype == 4 || stype == 6); 231 if ((ret = setsockopt(s, stype == 4 ? IPPROTO_IP : IPPROTO_IPV6, 232 stype == 4 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS, &(int){1}, 233 sizeof(int))) == -1) 234 fatalp("set_mcast_ttl: %s", strerror(errno)); 235 return ret; 236 } 237 238 /* Sets TOS to 0xc0 aka IP Precedence 6 */ 239 static int 240 set_tos(int s) 241 { 242 int ret; 243 if ((ret = setsockopt(s, IPPROTO_IP, IP_TOS, &(int){0xc0}, 244 sizeof(int))) == -1) 245 fatalp("set_tos: %s", strerror(errno)); 246 return ret; 247 } 248 249 static int 250 socket_reuse_port(int s) 251 { 252 int ret; 253 if ((ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &(int){1}, 254 sizeof(int))) == -1) 255 fatalp("socket_reuse_port: %s", strerror(errno)); 256 return ret; 257 } 258 259 /* binds an UDP socket */ 260 static int 261 bind_socket(int s, uint8_t stype) 262 { 263 struct sockaddr sa; 264 265 assert (stype == 4 || stype == 6); 266 267 if (stype == 4) { 268 struct sockaddr_in *sa_inet = (struct sockaddr_in *)(&sa); 269 sa_inet->sin_len = sizeof(*sa_inet); 270 sa_inet->sin_family = AF_INET; 271 sa_inet->sin_addr.s_addr = INADDR_ANY; 272 sa_inet->sin_port = htons(LDP_PORT); 273 } 274 #ifdef INET6 275 else if (stype == 6) { 276 struct sockaddr_in6 *sa_inet6 = (struct sockaddr_in6 *)(&sa); 277 sa_inet6->sin6_len = sizeof(*sa_inet6); 278 sa_inet6->sin6_family = AF_INET6; 279 sa_inet6->sin6_addr = in6addr_any; 280 sa_inet6->sin6_port = htons(LDP_PORT); 281 } 282 #endif 283 if (bind(s, &sa, sa.sa_len)) { 284 fatalp("bind_socket: %s\n", strerror(errno)); 285 return -1; 286 } 287 return 0; 288 } 289 290 /* Create / bind the TCP socket */ 291 int 292 create_listening_socket(void) 293 { 294 struct sockaddr_in sa; 295 int s; 296 297 sa.sin_len = sizeof(sa); 298 sa.sin_family = AF_INET; 299 sa.sin_port = htons(LDP_PORT); 300 sa.sin_addr.s_addr = htonl(INADDR_ANY); 301 302 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 303 if (s < 0) 304 return s; 305 if (bind(s, (struct sockaddr *) & sa, sizeof(sa))) { 306 fatalp("bind: %s", strerror(errno)); 307 close(s); 308 return -1; 309 } 310 if (listen(s, 10) == -1) { 311 fatalp("listen: %s", strerror(errno)); 312 close(s); 313 return -1; 314 } 315 /* if (set_tos(s) == -1) { 316 fatalp("set_tos: %s", strerror(errno)); 317 close(s); 318 return -1; 319 } 320 */ return s; 321 } 322 323 /* 324 * It's ugly. We need a function to pass all tlvs and create pdu but since I 325 * use UDP socket only to send hellos, I didn't bother 326 */ 327 void 328 send_hello(void) 329 { 330 struct hello_tlv *t; 331 struct common_hello_tlv *cht; 332 struct ldp_pdu *spdu; 333 struct transport_address_tlv *trtlv; 334 void *v; 335 struct sockaddr_in sadest; /* Destination ALL_ROUTERS */ 336 ssize_t sb = 0; /* sent bytes */ 337 struct ifaddrs *ifa, *ifb; 338 struct sockaddr_in *if_sa; 339 #ifdef INET6 340 struct sockaddr_in6 sadest6; 341 struct sockaddr_in6 *if_sa6; 342 #endif 343 char lastifname[20]; 344 345 #define BASIC_HELLO_MSG_SIZE (sizeof(struct ldp_pdu) + /* PDU */ \ 346 TLV_TYPE_LENGTH + MSGID_SIZE + /* Hello TLV */ \ 347 /* Common Hello TLV */ \ 348 sizeof(struct common_hello_tlv)) 349 #define GENERAL_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + \ 350 /* Transport Address */ \ 351 sizeof(struct transport_address_tlv) 352 #define IPV4_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + 4 + sizeof(struct in_addr) 353 #define IPV6_HELLO_MSG_SIZE BASIC_HELLO_MSG_SIZE + 4 + sizeof(struct in6_addr) 354 355 if ((v = calloc(1, GENERAL_HELLO_MSG_SIZE)) == NULL) { 356 fatalp("alloc problem in send_hello()\n"); 357 return; 358 } 359 360 spdu = (struct ldp_pdu *)((char *)v); 361 t = (struct hello_tlv *)(spdu + 1); 362 cht = &t->ch; /* Hello tlv struct includes CHT */ 363 trtlv = (struct transport_address_tlv *)(t + 1); 364 365 /* Prepare PDU envelope */ 366 spdu->version = htons(LDP_VERSION); 367 spdu->length = htons(IPV4_HELLO_MSG_SIZE - PDU_VER_LENGTH); 368 inet_aton(LDP_ID, &spdu->ldp_id); 369 370 /* Prepare Hello TLV */ 371 t->type = htons(LDP_HELLO); 372 t->length = htons(MSGID_SIZE + 373 sizeof(struct common_hello_tlv) + 374 IPV4_HELLO_MSG_SIZE - BASIC_HELLO_MSG_SIZE); 375 /* 376 * kefren: 377 * I used ID 0 instead of htonl(get_message_id()) because I've 378 * seen hellos from Cisco routers doing the same thing 379 */ 380 t->messageid = 0; 381 382 /* Prepare Common Hello attributes */ 383 cht->type = htons(TLV_COMMON_HELLO); 384 cht->length = htons(sizeof(cht->holdtime) + sizeof(cht->res)); 385 cht->holdtime = htons(ldp_holddown_time); 386 cht->res = 0; 387 388 /* 389 * Prepare Transport Address TLV RFC5036 says: "If this optional TLV 390 * is not present the IPv4 source address for the UDP packet carrying 391 * the Hello should be used." But we send it because everybody seems 392 * to do so 393 */ 394 trtlv->type = htons(TLV_IPV4_TRANSPORT); 395 trtlv->length = htons(sizeof(struct in_addr)); 396 /* trtlv->address will be set for each socket */ 397 398 /* Destination sockaddr */ 399 memset(&sadest, 0, sizeof(sadest)); 400 sadest.sin_len = sizeof(sadest); 401 sadest.sin_family = AF_INET; 402 sadest.sin_port = htons(LDP_PORT); 403 inet_aton(ALL_ROUTERS, &sadest.sin_addr); 404 405 if (getifaddrs(&ifa) == -1) { 406 free(v); 407 return; 408 } 409 410 lastifname[0] = '\0'; 411 /* Loop all interfaces in order to send IPv4 hellos */ 412 for (ifb = ifa; ifb; ifb = ifb->ifa_next) { 413 if_sa = (struct sockaddr_in *) ifb->ifa_addr; 414 if (if_sa->sin_family != AF_INET) 415 continue; 416 if (ntohl(if_sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET || 417 ntohl(if_sa->sin_addr.s_addr) >> 24 == 0) 418 continue; 419 420 /* Send only once per interface, using primary address */ 421 if (strcmp(ifb->ifa_name, lastifname) == 0) 422 continue; 423 debugp("Sending hello on %s\n", ifb->ifa_name); 424 if (setsockopt(hello_socket, IPPROTO_IP, IP_MULTICAST_IF, 425 &if_sa->sin_addr, sizeof(struct in_addr)) == -1) { 426 warnp("setsockopt failed: %s\n", strerror(errno)); 427 continue; 428 } 429 trtlv->address.ip4addr.s_addr = if_sa->sin_addr.s_addr; 430 431 strlcpy(lastifname, ifb->ifa_name, sizeof(lastifname)); 432 433 /* Send to the wire */ 434 sb = sendto(hello_socket, v, IPV4_HELLO_MSG_SIZE, 435 0, (struct sockaddr *) & sadest, sizeof(sadest)); 436 if (sb < (ssize_t)(IPV4_HELLO_MSG_SIZE)) 437 fatalp("send: %s", strerror(errno)); 438 else 439 debugp("Sent (IP4) %zd bytes on %s" 440 "(PDU: %d, Hello TLV: %d, CH: %d, TR: %d)\n", 441 sb, ifb->ifa_name, 442 ntohs(spdu->length), ntohs(t->length), 443 ntohs(cht->length), ntohs(trtlv->length)); 444 } 445 #ifdef INET6 446 /* Let's do the same thing for IPv6 */ 447 lastifname[0]='\0'; 448 449 /* Adjust lengths */ 450 spdu->length = htons(IPV6_HELLO_MSG_SIZE - PDU_VER_LENGTH); 451 t->length = htons(MSGID_SIZE + 452 sizeof(struct common_hello_tlv) + 453 IPV6_HELLO_MSG_SIZE - BASIC_HELLO_MSG_SIZE); 454 trtlv->length = htons(sizeof(struct in6_addr)); 455 trtlv->type = htons(TLV_IPV6_TRANSPORT); 456 457 /* Prepare destination sockaddr */ 458 memset(&sadest6, 0, sizeof(sadest6)); 459 sadest6.sin6_len = sizeof(sadest6); 460 sadest6.sin6_family = AF_INET6; 461 sadest6.sin6_port = htons(LDP_PORT); 462 assert(inet_pton(AF_INET6, ALL_ROUTERS6, &sadest6.sin6_addr) == 1); 463 464 for (ifb = ifa; ifb; ifb = ifb->ifa_next) { 465 unsigned int if_index; 466 if_sa6 = (struct sockaddr_in6 *) ifb->ifa_addr; 467 if (if_sa6->sin6_family != AF_INET6) 468 continue; 469 if (IN6_IS_ADDR_LOOPBACK(&if_sa6->sin6_addr)) 470 continue; 471 /* 472 * draft-ietf-mpls-ldp-ipv6-07 Section 5.1: 473 * Additionally, the link-local 474 * IPv6 address MUST be used as the source IP address in IPv6 475 * LDP Link Hellos. 476 */ 477 if (IN6_IS_ADDR_LINKLOCAL(&if_sa6->sin6_addr) == 0) 478 continue; 479 480 /* Send only once per interface, using primary address */ 481 if (strcmp(ifb->ifa_name, lastifname) == 0) 482 continue; 483 if_index = if_nametoindex(ifb->ifa_name); 484 if (if_index == 0) 485 continue; 486 if (setsockopt(hello6_socket, IPPROTO_IPV6, IPV6_MULTICAST_IF, 487 &if_index, sizeof(int)) == -1) { 488 warnp("setsockopt6 failed in sendhello(): %s\n", 489 strerror(errno)); 490 continue; 491 } 492 memcpy(&trtlv->address.ip6addr, &if_sa6->sin6_addr, 493 sizeof(struct in6_addr)); 494 495 strlcpy(lastifname, ifb->ifa_name, sizeof(lastifname)); 496 497 /* Put it on the wire */ 498 sb = sendto(hello6_socket, v, IPV6_HELLO_MSG_SIZE, 499 0, (struct sockaddr *)&sadest6, sizeof(sadest6)); 500 if (sb < (ssize_t)(IPV6_HELLO_MSG_SIZE)) 501 fatalp("send6 on %s: %s", ifb->ifa_name, strerror(errno)); 502 else 503 debugp("Sent (IPv6) %zd bytes on %s" 504 "(PDU: %d, Hello TLV: %d, CH: %d TR: %d)\n", 505 sb, ifb->ifa_name, 506 htons(spdu->length), htons(t->length), 507 htons(cht->length), htons(trtlv->length)); 508 509 } 510 #endif 511 freeifaddrs(ifa); 512 free(v); 513 } 514 515 int 516 get_message_id(void) 517 { 518 current_msg_id++; 519 return current_msg_id; 520 } 521 522 static int 523 get_local_addr(struct sockaddr_dl *sdl, struct in_addr *sin) 524 { 525 struct ifaddrs *ifa, *ifb; 526 struct sockaddr_in *sinet; 527 528 if (sdl == NULL) 529 return -1; 530 531 if (getifaddrs(&ifa) == -1) 532 return -1; 533 for (ifb = ifa; ifb; ifb = ifb->ifa_next) 534 if (ifb->ifa_addr->sa_family == AF_INET) { 535 if (if_nametoindex(ifb->ifa_name) != sdl->sdl_index) 536 continue; 537 sinet = (struct sockaddr_in*) ifb->ifa_addr; 538 sin->s_addr = sinet->sin_addr.s_addr; 539 freeifaddrs(ifa); 540 return 0; 541 } 542 freeifaddrs(ifa); 543 return -1; 544 } 545 546 /* Receive PDUs on Multicast UDP socket */ 547 void 548 recv_pdu(int sock) 549 { 550 struct ldp_pdu rpdu; 551 int c, i; 552 struct msghdr msg; 553 struct iovec iov[1]; 554 unsigned char recvspace[MAX_PDU_SIZE]; 555 struct hello_tlv *t; 556 struct sockaddr_in fromsa; 557 struct sockaddr_dl *sdl = NULL; 558 struct in_addr my_ldp_addr, local_addr; 559 struct cmsghdr *cmptr; 560 union { 561 struct cmsghdr cm; 562 char control[1024]; 563 } control_un; 564 565 debugp("Entering RECV_PDU\n"); 566 567 memset(&msg, 0, sizeof(msg)); 568 msg.msg_control = control_un.control; 569 msg.msg_controllen = sizeof(control_un.control); 570 msg.msg_flags = 0; 571 msg.msg_name = &fromsa; 572 msg.msg_namelen = sizeof(fromsa); 573 iov[0].iov_base = recvspace; 574 iov[0].iov_len = sizeof(recvspace); 575 msg.msg_iov = iov; 576 msg.msg_iovlen = 1; 577 578 c = recvmsg(sock, &msg, MSG_WAITALL); 579 debugp("Incoming PDU size: %d\n", c); 580 581 debugp("PDU from: %s\n", inet_ntoa(fromsa.sin_addr)); 582 583 /* Check to see if this is larger than MIN_PDU_SIZE */ 584 if (c < MIN_PDU_SIZE) 585 return; 586 587 /* Read the PDU */ 588 i = get_pdu(recvspace, &rpdu); 589 590 /* We currently understand Version 1 */ 591 if (rpdu.version != LDP_VERSION) { 592 fatalp("recv_pdu: Version mismatch\n"); 593 return; 594 } 595 596 /* Maybe it's our hello */ 597 inet_aton(LDP_ID, &my_ldp_addr); 598 if (rpdu.ldp_id.s_addr == my_ldp_addr.s_addr) { 599 fatalp("Received our PDU..\n"); /* it should be not looped */ 600 return; 601 } 602 603 if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) || 604 (msg.msg_flags & MSG_CTRUNC)) 605 local_addr.s_addr = my_ldp_addr.s_addr; 606 else { 607 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL; 608 cmptr = CMSG_NXTHDR(&msg, cmptr)) 609 if (cmptr->cmsg_level == IPPROTO_IP && 610 cmptr->cmsg_type == IP_RECVIF) { 611 sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr); 612 break; 613 } 614 if (get_local_addr(sdl, &local_addr) != 0) 615 local_addr.s_addr = my_ldp_addr.s_addr; 616 } 617 618 619 debugp("Read %d bytes from address %s Length: %.4d Version: %d\n", 620 c, inet_ntoa(rpdu.ldp_id), rpdu.length, rpdu.version); 621 622 /* Fill the TLV messages */ 623 t = get_hello_tlv(recvspace + i, c - i); 624 run_ldp_hello(&rpdu, t, &fromsa.sin_addr, &local_addr, sock); 625 } 626 627 void 628 send_hello_alarm(int unused) 629 { 630 struct ldp_peer *p, *ptmp; 631 struct hello_info *hi, *hinext; 632 time_t t = time(NULL); 633 int olderrno = errno; 634 635 /* Send hellos */ 636 if (!(t % ldp_hello_time)) 637 send_hello(); 638 639 /* Timeout -- */ 640 SLIST_FOREACH(p, &ldp_peer_head, peers) 641 p->timeout--; 642 643 /* Check for timeout */ 644 SLIST_FOREACH_SAFE(p, &ldp_peer_head, peers, ptmp) 645 if (p->timeout < 1) 646 switch (p->state) { 647 case LDP_PEER_HOLDDOWN: 648 debugp("LDP holddown expired for peer %s\n", 649 inet_ntoa(p->ldp_id)); 650 ldp_peer_delete(p); 651 break; 652 case LDP_PEER_ESTABLISHED: 653 case LDP_PEER_CONNECTED: 654 send_notification(p, 0, 655 NOTIF_KEEP_ALIVE_TIMER_EXPIRED); 656 warnp("Keepalive expired for %s\n", 657 inet_ntoa(p->ldp_id)); 658 ldp_peer_holddown(p); 659 break; 660 } /* switch */ 661 662 /* send keepalives */ 663 if (!(t % ldp_keepalive_time)) { 664 SLIST_FOREACH(p, &ldp_peer_head, peers) 665 if (p->state == LDP_PEER_ESTABLISHED) { 666 debugp("Sending KeepAlive to %s\n", 667 inet_ntoa(p->ldp_id)); 668 keep_alive(p); 669 } 670 } 671 672 /* Decrement hello info keepalives */ 673 SLIST_FOREACH(hi, &hello_info_head, infos) 674 if (hi->keepalive != 0xFFFF) 675 hi->keepalive--; 676 677 /* Check hello keepalives */ 678 SLIST_FOREACH_SAFE(hi, &hello_info_head, infos, hinext) 679 if (hi->keepalive < 1) 680 SLIST_REMOVE(&hello_info_head, hi, hello_info, infos); 681 682 /* Set the alarm again and bail out */ 683 alarm(1); 684 errno = olderrno; 685 } 686 687 static void 688 bail_out(int x) 689 { 690 ldp_peer_holddown_all(); 691 flush_mpls_routes(); 692 exit(0); 693 } 694 695 /* 696 * The big poll that catches every single event 697 * on every socket. 698 */ 699 int 700 the_big_loop(void) 701 { 702 int sock_error; 703 uint32_t i; 704 socklen_t sock_error_size = sizeof(int); 705 struct ldp_peer *p; 706 struct com_sock *cs; 707 struct pollfd pfd[MAX_POLL_FDS]; 708 709 assert(MAX_POLL_FDS > 3); 710 711 SLIST_INIT(&hello_info_head); 712 713 signal(SIGALRM, send_hello_alarm); 714 signal(SIGPIPE, SIG_IGN); 715 signal(SIGINT, bail_out); 716 signal(SIGTERM, bail_out); 717 send_hello_alarm(1); 718 719 route_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); 720 721 sock_error = bind_current_routes(); 722 if (sock_error != LDP_E_OK) { 723 fatalp("Cannot get current routes\n"); 724 return sock_error; 725 } 726 727 for (;;) { 728 nfds_t pollsum = 4; 729 730 pfd[0].fd = ls; 731 pfd[0].events = POLLRDNORM; 732 pfd[0].revents = 0; 733 734 pfd[1].fd = route_socket; 735 pfd[1].events = POLLRDNORM; 736 pfd[1].revents = 0; 737 738 pfd[2].fd = command_socket; 739 pfd[2].events = POLLRDNORM; 740 pfd[2].revents = 0; 741 742 /* Hello socket */ 743 pfd[3].fd = hello_socket; 744 pfd[3].events = POLLIN; 745 pfd[3].revents = 0; 746 747 /* Command sockets */ 748 for (i=0; i < MAX_COMMAND_SOCKETS; i++) 749 if (csockets[i].socket != -1) { 750 if (pollsum >= MAX_POLL_FDS) 751 break; 752 pfd[pollsum].fd = csockets[i].socket; 753 pfd[pollsum].events = POLLIN; 754 pfd[pollsum].revents = 0; 755 pollsum++; 756 } 757 758 /* LDP Peer sockets */ 759 SLIST_FOREACH(p, &ldp_peer_head, peers) { 760 if (p->socket < 1) 761 continue; 762 switch (p->state) { 763 case LDP_PEER_CONNECTED: 764 case LDP_PEER_ESTABLISHED: 765 if (pollsum >= MAX_POLL_FDS) 766 break; 767 pfd[pollsum].fd = p->socket; 768 pfd[pollsum].events = POLLRDNORM; 769 pfd[pollsum].revents = 0; 770 pollsum++; 771 break; 772 case LDP_PEER_CONNECTING: 773 if (pollsum >= MAX_POLL_FDS) 774 break; 775 pfd[pollsum].fd = p->socket; 776 pfd[pollsum].events = POLLWRNORM; 777 pfd[pollsum].revents = 0; 778 pollsum++; 779 break; 780 } 781 } 782 783 if (pollsum >= MAX_POLL_FDS) { 784 fatalp("Too many sockets. Increase MAX_POLL_FDS\n"); 785 return LDP_E_TOO_MANY_FDS; 786 } 787 if (poll(pfd, pollsum, INFTIM) < 0) { 788 if (errno != EINTR) 789 fatalp("poll: %s", strerror(errno)); 790 continue; 791 } 792 793 for (i = 0; i < pollsum; i++) { 794 if ((pfd[i].revents & POLLRDNORM) || 795 (pfd[i].revents & POLLIN)) { 796 if(pfd[i].fd == ls) 797 new_peer_connection(); 798 else if (pfd[i].fd == route_socket) { 799 struct rt_msg xbuf; 800 int l; 801 do { 802 l = read(route_socket, &xbuf, 803 sizeof(xbuf)); 804 } while ((l == -1) && (errno == EINTR)); 805 806 if (l == -1) 807 break; 808 809 check_route(&xbuf, l); 810 811 } else if (pfd[i].fd == hello_socket) { 812 /* Receiving hello socket */ 813 recv_pdu(pfd[i].fd); 814 } else if (pfd[i].fd == command_socket) { 815 command_accept(command_socket); 816 } else if ((cs = is_command_socket(pfd[i].fd)) 817 != NULL) { 818 command_dispatch(cs); 819 } else { 820 /* ldp peer socket */ 821 p = get_ldp_peer_by_socket(pfd[i].fd); 822 if (p) 823 recv_session_pdu(p); 824 } 825 } else if(pfd[i].revents & POLLWRNORM) { 826 p = get_ldp_peer_by_socket(pfd[i].fd); 827 if (!p) 828 continue; 829 if (getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR, 830 &sock_error, &sock_error_size) != 0 || 831 sock_error != 0) { 832 ldp_peer_holddown(p); 833 sock_error = 0; 834 } else { 835 p->state = LDP_PEER_CONNECTED; 836 send_initialize(p); 837 } 838 } 839 } 840 841 for (int ri = 0; ri < replay_index; ri++) { 842 debugp("Replaying: PID %d, SEQ %d\n", 843 replay_rt[ri].m_rtm.rtm_pid, 844 replay_rt[ri].m_rtm.rtm_seq); 845 check_route(&replay_rt[ri], sizeof(struct rt_msg)); 846 } 847 replay_index = 0; 848 } /* for (;;) */ 849 } 850 851 void 852 new_peer_connection() 853 { 854 struct sockaddr_in sa, sin_me; 855 int s; 856 857 s = accept(ls, (struct sockaddr *) & sa, 858 & (socklen_t) { sizeof(struct sockaddr_in) } ); 859 if (s < 0) { 860 fatalp("accept: %s", strerror(errno)); 861 return; 862 } 863 864 if (get_ldp_peer(&sa.sin_addr) != NULL) { 865 close(s); 866 return; 867 } 868 869 warnp("Accepted a connection from %s\n", inet_ntoa(sa.sin_addr)); 870 871 if (getsockname(s, (struct sockaddr *)&sin_me, 872 & (socklen_t) { sizeof(struct sockaddr_in) } )) { 873 fatalp("new_peer_connection(): cannot getsockname\n"); 874 close(s); 875 return; 876 } 877 878 if (ntohl(sa.sin_addr.s_addr) < ntohl(sin_me.sin_addr.s_addr)) { 879 fatalp("Peer %s: connect from lower ID\n", 880 inet_ntoa(sa.sin_addr)); 881 close(s); 882 return; 883 } 884 /* XXX: sa.sin_addr is not peer LDP ID ... */ 885 ldp_peer_new(&sa.sin_addr, &sa.sin_addr, NULL, NULL, ldp_holddown_time, s); 886 887 } 888 889 void 890 send_initialize(struct ldp_peer * p) 891 { 892 struct init_tlv ti; 893 894 ti.type = htons(LDP_INITIALIZE); 895 ti.length = htons(sizeof(struct init_tlv) - TLV_TYPE_LENGTH); 896 ti.messageid = htonl(get_message_id()); 897 ti.cs_type = htons(TLV_COMMON_SESSION); 898 ti.cs_len = htons(CS_LEN); 899 ti.cs_version = htons(LDP_VERSION); 900 ti.cs_keepalive = htons(2 * ldp_keepalive_time); 901 ti.cs_adpvlim = 0; 902 ti.cs_maxpdulen = htons(MAX_PDU_SIZE); 903 ti.cs_peeraddress.s_addr = p->ldp_id.s_addr; 904 ti.cs_peeraddrspace = 0; 905 906 send_tlv(p, (struct tlv *) (void *) &ti); 907 } 908 909 void 910 keep_alive(struct ldp_peer * p) 911 { 912 struct ka_tlv kt; 913 914 kt.type = htons(LDP_KEEPALIVE); 915 kt.length = htons(sizeof(kt.messageid)); 916 kt.messageid = htonl(get_message_id()); 917 918 send_tlv(p, (struct tlv *) (void *) &kt); 919 920 } 921 922 void 923 recv_session_pdu(struct ldp_peer * p) 924 { 925 struct ldp_pdu *rpdu; 926 struct address_tlv *atlv; 927 struct al_tlv *altlv; 928 struct init_tlv *itlv; 929 struct label_map_tlv *lmtlv; 930 struct fec_tlv *fectlv; 931 struct label_tlv *labeltlv; 932 struct notification_tlv *nottlv; 933 struct hello_info *hi; 934 935 int c; 936 int32_t wo = 0; 937 struct tlv *ttmp; 938 unsigned char recvspace[MAX_PDU_SIZE]; 939 940 memset(recvspace, 0, MAX_PDU_SIZE); 941 942 c = recv(p->socket, (void *) recvspace, MAX_PDU_SIZE, MSG_PEEK); 943 944 debugp("Ready to read %d bytes\n", c); 945 946 if (c < 1) { /* Session closed */ 947 warnp("Error in connection with %s\n", inet_ntoa(p->ldp_id)); 948 ldp_peer_holddown(p); 949 return; 950 } 951 if (c > MAX_PDU_SIZE) { 952 debugp("Incoming PDU size exceeds MAX_PDU_SIZE !\n"); 953 return; 954 } 955 if (c < MIN_PDU_SIZE) { 956 debugp("PDU too small received from peer %s\n", inet_ntoa(p->ldp_id)); 957 return; 958 } 959 rpdu = (struct ldp_pdu *) recvspace; 960 /* XXX: buggy messages may crash the whole thing */ 961 c = recv(p->socket, (void *) recvspace, 962 ntohs(rpdu->length) + PDU_VER_LENGTH, MSG_WAITALL); 963 rpdu = (struct ldp_pdu *) recvspace; 964 965 /* Check if it's somehow OK... */ 966 if (check_recv_pdu(p, rpdu, c) != 0) 967 return; 968 969 debugp("Read %d bytes, PDU size: %d bytes\n", c, ntohs(rpdu->length)); 970 wo = sizeof(struct ldp_pdu); 971 972 while (wo + TLV_TYPE_LENGTH < (uint)c) { 973 974 ttmp = (struct tlv *) (&recvspace[wo]); 975 976 if ((ntohs(ttmp->type) != LDP_KEEPALIVE) && 977 (ntohs(ttmp->type) != LDP_LABEL_MAPPING)) { 978 debugp("Got Type: 0x%.4X (Length: %d) from %s\n", 979 ntohs(ttmp->type), ntohs(ttmp->length), 980 inet_ntoa(p->ldp_id)); 981 } else 982 debugp("Got Type: 0x%.4X (Length: %d) from %s\n", 983 ntohs(ttmp->type), ntohs(ttmp->length), 984 inet_ntoa(p->ldp_id)); 985 986 /* Should we get the message ? */ 987 if (p->state != LDP_PEER_ESTABLISHED && 988 ntohs(ttmp->type) != LDP_INITIALIZE && 989 ntohs(ttmp->type) != LDP_KEEPALIVE) 990 break; 991 /* The big switch */ 992 switch (ntohs(ttmp->type)) { 993 case LDP_INITIALIZE: 994 itlv = (struct init_tlv *)ttmp; 995 /* Check size */ 996 if (ntohs(itlv->length) < 997 sizeof(struct init_tlv) - TLV_TYPE_LENGTH) { 998 send_notification(p, 0, 999 NOTIF_BAD_PDU_LEN | NOTIF_FATAL); 1000 ldp_peer_holddown(p); 1001 break; 1002 } 1003 /* Check version */ 1004 if (ntohs(itlv->cs_version) != LDP_VERSION) { 1005 send_notification(p, ntohl(itlv->messageid), 1006 NOTIF_BAD_LDP_VER | NOTIF_FATAL); 1007 ldp_peer_holddown(p); 1008 break; 1009 } 1010 /* Check if we got any hello from this one */ 1011 SLIST_FOREACH(hi, &hello_info_head, infos) 1012 if (hi->ldp_id.s_addr == rpdu->ldp_id.s_addr) 1013 break; 1014 if (hi == NULL) { 1015 send_notification(p, ntohl(itlv->messageid), 1016 NOTIF_SESSION_REJECTED_NO_HELLO | NOTIF_FATAL); 1017 ldp_peer_holddown(p); 1018 break; 1019 } 1020 1021 if (!p->master) { 1022 keep_alive(p); 1023 send_initialize(p); 1024 } else { 1025 p->state = LDP_PEER_ESTABLISHED; 1026 p->established_t = time(NULL); 1027 keep_alive(p); 1028 1029 /* 1030 * Recheck here ldp id because we accepted 1031 * connection without knowing who is it for sure 1032 */ 1033 p->ldp_id.s_addr = rpdu->ldp_id.s_addr; 1034 1035 fatalp("LDP neighbour %s is UP\n", 1036 inet_ntoa(p->ldp_id)); 1037 mpls_add_ldp_peer(p); 1038 send_addresses(p); 1039 send_all_bindings(p); 1040 } 1041 break; 1042 case LDP_KEEPALIVE: 1043 if ((p->state == LDP_PEER_CONNECTED) && (!p->master)) { 1044 p->state = LDP_PEER_ESTABLISHED; 1045 p->established_t = time(NULL); 1046 fatalp("LDP neighbour %s is UP\n", 1047 inet_ntoa(p->ldp_id)); 1048 mpls_add_ldp_peer(p); 1049 send_addresses(p); 1050 send_all_bindings(p); 1051 } 1052 p->timeout = p->holdtime; 1053 break; 1054 case LDP_ADDRESS: 1055 /* Add peer addresses */ 1056 atlv = (struct address_tlv *) ttmp; 1057 altlv = (struct al_tlv *) (&atlv[1]); 1058 add_ifaddresses(p, altlv); 1059 print_bounded_addresses(p); 1060 break; 1061 case LDP_ADDRESS_WITHDRAW: 1062 atlv = (struct address_tlv *) ttmp; 1063 altlv = (struct al_tlv *) (&atlv[1]); 1064 del_ifaddresses(p, altlv); 1065 break; 1066 case LDP_LABEL_MAPPING: 1067 lmtlv = (struct label_map_tlv *) ttmp; 1068 fectlv = (struct fec_tlv *) (&lmtlv[1]); 1069 labeltlv = (struct label_tlv *)((unsigned char *)fectlv 1070 + ntohs(fectlv->length) + TLV_TYPE_LENGTH); 1071 map_label(p, fectlv, labeltlv); 1072 break; 1073 case LDP_LABEL_REQUEST: 1074 lmtlv = (struct label_map_tlv *) ttmp; 1075 fectlv = (struct fec_tlv *) (&lmtlv[1]); 1076 switch (request_respond(p, lmtlv, fectlv)) { 1077 case LDP_E_BAD_FEC: 1078 send_notification(p, ntohl(lmtlv->messageid), 1079 NOTIF_UNKNOWN_TLV); 1080 break; 1081 case LDP_E_BAD_AF: 1082 send_notification(p, ntohl(lmtlv->messageid), 1083 NOTIF_UNSUPPORTED_AF); 1084 break; 1085 case LDP_E_NO_SUCH_ROUTE: 1086 send_notification(p, ntohl(lmtlv->messageid), 1087 NOTIF_NO_ROUTE); 1088 break; 1089 } 1090 break; 1091 case LDP_LABEL_WITHDRAW: 1092 lmtlv = (struct label_map_tlv *) ttmp; 1093 fectlv = (struct fec_tlv *) (&lmtlv[1]); 1094 if (withdraw_label(p, fectlv) == LDP_E_OK) { 1095 /* Send RELEASE */ 1096 prepare_release(ttmp); 1097 send_tlv(p, ttmp); 1098 } 1099 break; 1100 case LDP_LABEL_RELEASE: 1101 /* 1102 * XXX: we need to make a timed queue... 1103 * For now I just assume peers are processing messages 1104 * correctly so I just ignore confirmations 1105 */ 1106 wo = -1; /* Ignore rest of message */ 1107 break; 1108 case LDP_LABEL_ABORT: 1109 /* XXX: For now I pretend I can process everything 1110 * RFC 5036, Section 3.5.9.1 1111 * If an LSR receives a Label Abort Request Message after it 1112 * has responded to the Label Request in question with a Label 1113 * Mapping message or a Notification message, it ignores the 1114 * abort request. 1115 */ 1116 wo = -1; 1117 break; 1118 case LDP_NOTIFICATION: 1119 nottlv = (struct notification_tlv *) ttmp; 1120 nottlv->st_code = ntohl(nottlv->st_code); 1121 fatalp("Got notification 0x%X from peer %s\n", 1122 nottlv->st_code, inet_ntoa(p->ldp_id)); 1123 if (nottlv->st_code >> 31) { 1124 fatalp("LDP peer %s signalized %s\n", 1125 inet_ntoa(p->ldp_id), 1126 NOTIF_STR[(nottlv->st_code << 1) >> 1]); 1127 ldp_peer_holddown(p); 1128 wo = -1; 1129 } 1130 break; 1131 case LDP_HELLO: 1132 /* No hellos should came on tcp session */ 1133 wo = -1; 1134 break; 1135 default: 1136 warnp("Unknown TLV received from %s\n", 1137 inet_ntoa(p->ldp_id)); 1138 debug_tlv(ttmp); 1139 wo = -1;/* discard the rest of the message */ 1140 break; 1141 } 1142 if (wo < 0) { 1143 debugp("Discarding the rest of the message\n"); 1144 break; 1145 } else { 1146 wo += ntohs(ttmp->length) + TLV_TYPE_LENGTH; 1147 debugp("WORKED ON %u bytes (Left %d)\n", wo, c - wo); 1148 } 1149 } /* while */ 1150 1151 } 1152 1153 /* Sends a pdu, tlv pair to a connected peer */ 1154 int 1155 send_message(struct ldp_peer * p, struct ldp_pdu * pdu, struct tlv * t) 1156 { 1157 unsigned char sendspace[MAX_PDU_SIZE]; 1158 1159 /* Check if peer is connected */ 1160 switch (p->state) { 1161 case LDP_PEER_CONNECTED: 1162 case LDP_PEER_ESTABLISHED: 1163 break; 1164 default: 1165 return -1; 1166 } 1167 1168 /* Check length validity first */ 1169 if (ntohs(pdu->length) != 1170 ntohs(t->length) + TLV_TYPE_LENGTH + PDU_PAYLOAD_LENGTH) { 1171 fatalp("LDP: TLV - PDU incompability. Message discarded\n"); 1172 fatalp("LDP: TLV len %d - PDU len %d\n", ntohs(t->length), 1173 ntohs(pdu->length)); 1174 return -1; 1175 } 1176 if (ntohs(t->length) + PDU_VER_LENGTH > MAX_PDU_SIZE) { 1177 fatalp("Message to large discarded\n"); 1178 return -1; 1179 } 1180 /* Arrange them in a buffer and send */ 1181 memcpy(sendspace, pdu, sizeof(struct ldp_pdu)); 1182 memcpy(sendspace + sizeof(struct ldp_pdu), t, 1183 ntohs(t->length) + TLV_TYPE_LENGTH); 1184 1185 /* Report keepalives only for DEBUG */ 1186 if ((ntohs(t->type) != 0x201) && (ntohs(t->type) != 0x400)) { 1187 debugp("Sending message type 0x%.4X to %s (size: %d)\n", 1188 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length)); 1189 } else 1190 /* downgraded from warnp to debugp for now */ 1191 debugp("Sending message type 0x%.4X to %s (size: %d)\n", 1192 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length)); 1193 1194 /* Send it finally */ 1195 return send(p->socket, sendspace, 1196 ntohs(pdu->length) + PDU_VER_LENGTH, 0); 1197 } 1198 1199 /* 1200 * Encapsulates TLV into a PDU and sends it to a peer 1201 */ 1202 int 1203 send_tlv(struct ldp_peer * p, struct tlv * t) 1204 { 1205 struct ldp_pdu pdu; 1206 1207 pdu.version = htons(LDP_VERSION); 1208 inet_aton(LDP_ID, &pdu.ldp_id); 1209 pdu.label_space = 0; 1210 pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH + 1211 PDU_PAYLOAD_LENGTH); 1212 1213 return send_message(p, &pdu, t); 1214 } 1215 1216 1217 int 1218 send_addresses(struct ldp_peer * p) 1219 { 1220 struct address_list_tlv *t; 1221 int ret; 1222 1223 t = build_address_list_tlv(); 1224 1225 ret = send_tlv(p, (struct tlv *) t); 1226 free(t); 1227 return ret; 1228 1229 } 1230