socketops.c revision 1.14 1 /* $NetBSD: socketops.c,v 1.14 2012/11/13 01:08:51 pgoyette 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/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 /* Send only once per interface, using primary address */
473 if (strcmp(ifb->ifa_name, lastifname) == 0)
474 continue;
475 if_index = if_nametoindex(ifb->ifa_name);
476 if (if_index == 0)
477 continue;
478 if (setsockopt(hello6_socket, IPPROTO_IPV6, IPV6_MULTICAST_IF,
479 &if_index, sizeof(int)) == -1) {
480 warnp("setsockopt6 failed in sendhello(): %s\n",
481 strerror(errno));
482 continue;
483 }
484 memcpy(&trtlv->address.ip6addr, &if_sa6->sin6_addr,
485 sizeof(struct in6_addr));
486
487 strlcpy(lastifname, ifb->ifa_name, sizeof(lastifname));
488
489 /* Put it on the wire */
490 sb = sendto(hello6_socket, v, IPV6_HELLO_MSG_SIZE,
491 0, (struct sockaddr *)&sadest6, sizeof(sadest6));
492 if (sb < (ssize_t)(IPV6_HELLO_MSG_SIZE))
493 fatalp("send6 on %s: %s", ifb->ifa_name, strerror(errno));
494 else
495 debugp("Sent (IPv6) %zd bytes on %s"
496 "(PDU: %d, Hello TLV: %d, CH: %d TR: %d)\n",
497 sb, ifb->ifa_name,
498 htons(spdu->length), htons(t->length),
499 htons(cht->length), htons(trtlv->length));
500
501 }
502 #endif
503 freeifaddrs(ifa);
504 free(v);
505 }
506
507 int
508 get_message_id(void)
509 {
510 current_msg_id++;
511 return current_msg_id;
512 }
513
514 static int
515 get_local_addr(struct sockaddr_dl *sdl, struct in_addr *sin)
516 {
517 struct ifaddrs *ifa, *ifb;
518 struct sockaddr_in *sinet;
519
520 if (sdl == NULL)
521 return -1;
522
523 if (getifaddrs(&ifa) == -1)
524 return -1;
525 for (ifb = ifa; ifb; ifb = ifb->ifa_next)
526 if (ifb->ifa_addr->sa_family == AF_INET) {
527 if (if_nametoindex(ifb->ifa_name) != sdl->sdl_index)
528 continue;
529 sinet = (struct sockaddr_in*) ifb->ifa_addr;
530 sin->s_addr = sinet->sin_addr.s_addr;
531 freeifaddrs(ifa);
532 return 0;
533 }
534 freeifaddrs(ifa);
535 return -1;
536 }
537
538 /* Receive PDUs on Multicast UDP socket */
539 void
540 recv_pdu(int sock)
541 {
542 struct ldp_pdu rpdu;
543 int c, i;
544 struct msghdr msg;
545 struct iovec iov[1];
546 unsigned char recvspace[MAX_PDU_SIZE];
547 struct hello_tlv *t;
548 struct sockaddr_in fromsa;
549 struct sockaddr_dl *sdl = NULL;
550 struct in_addr my_ldp_addr, local_addr;
551 struct cmsghdr *cmptr;
552 union {
553 struct cmsghdr cm;
554 char control[1024];
555 } control_un;
556
557 debugp("Entering RECV_PDU\n");
558
559 memset(&msg, 0, sizeof(msg));
560 msg.msg_control = control_un.control;
561 msg.msg_controllen = sizeof(control_un.control);
562 msg.msg_flags = 0;
563 msg.msg_name = &fromsa;
564 msg.msg_namelen = sizeof(fromsa);
565 iov[0].iov_base = recvspace;
566 iov[0].iov_len = sizeof(recvspace);
567 msg.msg_iov = iov;
568 msg.msg_iovlen = 1;
569
570 c = recvmsg(sock, &msg, MSG_WAITALL);
571 debugp("Incoming PDU size: %d\n", c);
572
573 debugp("PDU from: %s\n", inet_ntoa(fromsa.sin_addr));
574
575 /* Check to see if this is larger than MIN_PDU_SIZE */
576 if (c < MIN_PDU_SIZE)
577 return;
578
579 /* Read the PDU */
580 i = get_pdu(recvspace, &rpdu);
581
582 /* We currently understand Version 1 */
583 if (rpdu.version != LDP_VERSION) {
584 fatalp("recv_pdu: Version mismatch\n");
585 return;
586 }
587
588 /* Maybe it's our hello */
589 inet_aton(LDP_ID, &my_ldp_addr);
590 if (rpdu.ldp_id.s_addr == my_ldp_addr.s_addr) {
591 fatalp("Received our PDU..\n"); /* it should be not looped */
592 return;
593 }
594
595 if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) ||
596 (msg.msg_flags & MSG_CTRUNC))
597 local_addr.s_addr = my_ldp_addr.s_addr;
598 else {
599 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
600 cmptr = CMSG_NXTHDR(&msg, cmptr))
601 if (cmptr->cmsg_level == IPPROTO_IP &&
602 cmptr->cmsg_type == IP_RECVIF) {
603 sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr);
604 break;
605 }
606 if (get_local_addr(sdl, &local_addr) != 0)
607 local_addr.s_addr = my_ldp_addr.s_addr;
608 }
609
610
611 debugp("Read %d bytes from address %s Length: %.4d Version: %d\n",
612 c, inet_ntoa(rpdu.ldp_id), rpdu.length, rpdu.version);
613
614 /* Fill the TLV messages */
615 t = get_hello_tlv(recvspace + i, c - i);
616 run_ldp_hello(&rpdu, t, &fromsa.sin_addr, &local_addr, sock);
617 }
618
619 void
620 send_hello_alarm(int unused)
621 {
622 struct ldp_peer *p, *ptmp;
623 struct hello_info *hi, *hinext;
624 time_t t = time(NULL);
625 int olderrno = errno;
626
627 /* Send hellos */
628 if (!(t % ldp_hello_time))
629 send_hello();
630
631 /* Timeout -- */
632 SLIST_FOREACH(p, &ldp_peer_head, peers)
633 p->timeout--;
634
635 /* Check for timeout */
636 SLIST_FOREACH_SAFE(p, &ldp_peer_head, peers, ptmp)
637 if (p->timeout < 1)
638 switch (p->state) {
639 case LDP_PEER_HOLDDOWN:
640 debugp("LDP holddown expired for peer %s\n",
641 inet_ntoa(p->ldp_id));
642 ldp_peer_delete(p);
643 break;
644 case LDP_PEER_ESTABLISHED:
645 case LDP_PEER_CONNECTED:
646 send_notification(p, 0,
647 NOTIF_KEEP_ALIVE_TIMER_EXPIRED);
648 warnp("Keepalive expired for %s\n",
649 inet_ntoa(p->ldp_id));
650 ldp_peer_holddown(p);
651 break;
652 } /* switch */
653
654 /* send keepalives */
655 if (!(t % ldp_keepalive_time)) {
656 SLIST_FOREACH(p, &ldp_peer_head, peers)
657 if (p->state == LDP_PEER_ESTABLISHED) {
658 debugp("Sending KeepAlive to %s\n",
659 inet_ntoa(p->ldp_id));
660 keep_alive(p);
661 }
662 }
663
664 /* Decrement hello info keepalives */
665 SLIST_FOREACH(hi, &hello_info_head, infos)
666 if (hi->keepalive != 0xFFFF)
667 hi->keepalive--;
668
669 /* Check hello keepalives */
670 SLIST_FOREACH_SAFE(hi, &hello_info_head, infos, hinext)
671 if (hi->keepalive < 1)
672 SLIST_REMOVE(&hello_info_head, hi, hello_info, infos);
673
674 /* Set the alarm again and bail out */
675 alarm(1);
676 errno = olderrno;
677 }
678
679 static void
680 bail_out(int x)
681 {
682 ldp_peer_holddown_all();
683 flush_mpls_routes();
684 exit(0);
685 }
686
687 /*
688 * The big poll that catches every single event
689 * on every socket.
690 */
691 int
692 the_big_loop(void)
693 {
694 int sock_error;
695 uint32_t i;
696 socklen_t sock_error_size = sizeof(int);
697 struct ldp_peer *p;
698 struct com_sock *cs;
699 struct pollfd pfd[MAX_POLL_FDS];
700
701 assert(MAX_POLL_FDS > 3);
702
703 SLIST_INIT(&hello_info_head);
704
705 signal(SIGALRM, send_hello_alarm);
706 signal(SIGPIPE, SIG_IGN);
707 signal(SIGINT, bail_out);
708 signal(SIGTERM, bail_out);
709 send_hello_alarm(1);
710
711 route_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
712
713 sock_error = bind_current_routes();
714 if (sock_error != LDP_E_OK) {
715 fatalp("Cannot get current routes\n");
716 return sock_error;
717 }
718
719 for (;;) {
720 nfds_t pollsum = 4;
721
722 pfd[0].fd = ls;
723 pfd[0].events = POLLRDNORM;
724 pfd[0].revents = 0;
725
726 pfd[1].fd = route_socket;
727 pfd[1].events = POLLRDNORM;
728 pfd[1].revents = 0;
729
730 pfd[2].fd = command_socket;
731 pfd[2].events = POLLRDNORM;
732 pfd[2].revents = 0;
733
734 /* Hello socket */
735 pfd[3].fd = hello_socket;
736 pfd[3].events = POLLIN;
737 pfd[3].revents = 0;
738
739 /* Command sockets */
740 for (i=0; i < MAX_COMMAND_SOCKETS; i++)
741 if (csockets[i].socket != -1) {
742 if (pollsum >= MAX_POLL_FDS)
743 break;
744 pfd[pollsum].fd = csockets[i].socket;
745 pfd[pollsum].events = POLLIN;
746 pfd[pollsum].revents = 0;
747 pollsum++;
748 }
749
750 /* LDP Peer sockets */
751 SLIST_FOREACH(p, &ldp_peer_head, peers) {
752 if (p->socket < 1)
753 continue;
754 switch (p->state) {
755 case LDP_PEER_CONNECTED:
756 case LDP_PEER_ESTABLISHED:
757 if (pollsum >= MAX_POLL_FDS)
758 break;
759 pfd[pollsum].fd = p->socket;
760 pfd[pollsum].events = POLLRDNORM;
761 pfd[pollsum].revents = 0;
762 pollsum++;
763 break;
764 case LDP_PEER_CONNECTING:
765 if (pollsum >= MAX_POLL_FDS)
766 break;
767 pfd[pollsum].fd = p->socket;
768 pfd[pollsum].events = POLLWRNORM;
769 pfd[pollsum].revents = 0;
770 pollsum++;
771 break;
772 }
773 }
774
775 if (pollsum >= MAX_POLL_FDS) {
776 fatalp("Too many sockets. Increase MAX_POLL_FDS\n");
777 return LDP_E_TOO_MANY_FDS;
778 }
779 if (poll(pfd, pollsum, INFTIM) < 0) {
780 if (errno != EINTR)
781 fatalp("poll: %s", strerror(errno));
782 continue;
783 }
784
785 for (i = 0; i < pollsum; i++) {
786 if ((pfd[i].revents & POLLRDNORM) ||
787 (pfd[i].revents & POLLIN)) {
788 if(pfd[i].fd == ls)
789 new_peer_connection();
790 else if (pfd[i].fd == route_socket) {
791 struct rt_msg xbuf;
792 int l;
793 do {
794 l = read(route_socket, &xbuf,
795 sizeof(xbuf));
796 } while ((l == -1) && (errno == EINTR));
797
798 if (l == -1)
799 break;
800
801 check_route(&xbuf, l);
802
803 } else if (pfd[i].fd == hello_socket) {
804 /* Receiving hello socket */
805 recv_pdu(pfd[i].fd);
806 } else if (pfd[i].fd == command_socket) {
807 command_accept(command_socket);
808 } else if ((cs = is_command_socket(pfd[i].fd))
809 != NULL) {
810 command_dispatch(cs);
811 } else {
812 /* ldp peer socket */
813 p = get_ldp_peer_by_socket(pfd[i].fd);
814 if (p)
815 recv_session_pdu(p);
816 }
817 } else if(pfd[i].revents & POLLWRNORM) {
818 p = get_ldp_peer_by_socket(pfd[i].fd);
819 if (!p)
820 continue;
821 if (getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR,
822 &sock_error, &sock_error_size) != 0 ||
823 sock_error != 0) {
824 ldp_peer_holddown(p);
825 sock_error = 0;
826 } else {
827 p->state = LDP_PEER_CONNECTED;
828 send_initialize(p);
829 }
830 }
831 }
832
833 for (int ri = 0; ri < replay_index; ri++) {
834 debugp("Replaying: PID %d, SEQ %d\n",
835 replay_rt[ri].m_rtm.rtm_pid,
836 replay_rt[ri].m_rtm.rtm_seq);
837 check_route(&replay_rt[ri], sizeof(struct rt_msg));
838 }
839 replay_index = 0;
840 } /* for (;;) */
841 }
842
843 void
844 new_peer_connection()
845 {
846 struct sockaddr_in sa, sin_me;
847 int s;
848
849 s = accept(ls, (struct sockaddr *) & sa,
850 & (socklen_t) { sizeof(struct sockaddr_in) } );
851 if (s < 0) {
852 fatalp("accept: %s", strerror(errno));
853 return;
854 }
855
856 if (get_ldp_peer(&sa.sin_addr) != NULL) {
857 close(s);
858 return;
859 }
860
861 warnp("Accepted a connection from %s\n", inet_ntoa(sa.sin_addr));
862
863 if (getsockname(s, (struct sockaddr *)&sin_me,
864 & (socklen_t) { sizeof(struct sockaddr_in) } )) {
865 fatalp("new_peer_connection(): cannot getsockname\n");
866 close(s);
867 return;
868 }
869
870 if (ntohl(sa.sin_addr.s_addr) < ntohl(sin_me.sin_addr.s_addr)) {
871 fatalp("Peer %s: connect from lower ID\n",
872 inet_ntoa(sa.sin_addr));
873 close(s);
874 return;
875 }
876 /* XXX: sa.sin_addr is not peer LDP ID ... */
877 ldp_peer_new(&sa.sin_addr, &sa.sin_addr, NULL, NULL, ldp_holddown_time, s);
878
879 }
880
881 void
882 send_initialize(struct ldp_peer * p)
883 {
884 struct init_tlv ti;
885
886 ti.type = htons(LDP_INITIALIZE);
887 ti.length = htons(sizeof(struct init_tlv) - TLV_TYPE_LENGTH);
888 ti.messageid = htonl(get_message_id());
889 ti.cs_type = htons(TLV_COMMON_SESSION);
890 ti.cs_len = htons(CS_LEN);
891 ti.cs_version = htons(LDP_VERSION);
892 ti.cs_keepalive = htons(2 * ldp_keepalive_time);
893 ti.cs_adpvlim = 0;
894 ti.cs_maxpdulen = htons(MAX_PDU_SIZE);
895 ti.cs_peeraddress.s_addr = p->ldp_id.s_addr;
896 ti.cs_peeraddrspace = 0;
897
898 send_tlv(p, (struct tlv *) (void *) &ti);
899 }
900
901 void
902 keep_alive(struct ldp_peer * p)
903 {
904 struct ka_tlv kt;
905
906 kt.type = htons(LDP_KEEPALIVE);
907 kt.length = htons(sizeof(kt.messageid));
908 kt.messageid = htonl(get_message_id());
909
910 send_tlv(p, (struct tlv *) (void *) &kt);
911
912 }
913
914 void
915 recv_session_pdu(struct ldp_peer * p)
916 {
917 struct ldp_pdu *rpdu;
918 struct address_tlv *atlv;
919 struct al_tlv *altlv;
920 struct init_tlv *itlv;
921 struct label_map_tlv *lmtlv;
922 struct fec_tlv *fectlv;
923 struct label_tlv *labeltlv;
924 struct notification_tlv *nottlv;
925 struct hello_info *hi;
926
927 int c;
928 int32_t wo = 0;
929 struct tlv *ttmp;
930 unsigned char recvspace[MAX_PDU_SIZE];
931
932 memset(recvspace, 0, MAX_PDU_SIZE);
933
934 c = recv(p->socket, (void *) recvspace, MAX_PDU_SIZE, MSG_PEEK);
935
936 debugp("Ready to read %d bytes\n", c);
937
938 if (c < 1) { /* Session closed */
939 warnp("Error in connection with %s\n", inet_ntoa(p->ldp_id));
940 ldp_peer_holddown(p);
941 return;
942 }
943 if (c > MAX_PDU_SIZE) {
944 debugp("Incoming PDU size exceeds MAX_PDU_SIZE !\n");
945 return;
946 }
947 if (c < MIN_PDU_SIZE) {
948 debugp("PDU too small received from peer %s\n", inet_ntoa(p->ldp_id));
949 return;
950 }
951 rpdu = (struct ldp_pdu *) recvspace;
952 /* XXX: buggy messages may crash the whole thing */
953 c = recv(p->socket, (void *) recvspace,
954 ntohs(rpdu->length) + PDU_VER_LENGTH, MSG_WAITALL);
955 rpdu = (struct ldp_pdu *) recvspace;
956
957 /* Check if it's somehow OK... */
958 if (check_recv_pdu(p, rpdu, c) != 0)
959 return;
960
961 debugp("Read %d bytes, PDU size: %d bytes\n", c, ntohs(rpdu->length));
962 wo = sizeof(struct ldp_pdu);
963
964 while (wo + TLV_TYPE_LENGTH < (uint)c) {
965
966 ttmp = (struct tlv *) (&recvspace[wo]);
967
968 if ((ntohs(ttmp->type) != LDP_KEEPALIVE) &&
969 (ntohs(ttmp->type) != LDP_LABEL_MAPPING)) {
970 debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
971 ntohs(ttmp->type), ntohs(ttmp->length),
972 inet_ntoa(p->ldp_id));
973 } else
974 debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
975 ntohs(ttmp->type), ntohs(ttmp->length),
976 inet_ntoa(p->ldp_id));
977
978 /* Should we get the message ? */
979 if (p->state != LDP_PEER_ESTABLISHED &&
980 ntohs(ttmp->type) != LDP_INITIALIZE &&
981 ntohs(ttmp->type) != LDP_KEEPALIVE)
982 break;
983 /* The big switch */
984 switch (ntohs(ttmp->type)) {
985 case LDP_INITIALIZE:
986 itlv = (struct init_tlv *)ttmp;
987 /* Check size */
988 if (ntohs(itlv->length) <
989 sizeof(struct init_tlv) - TLV_TYPE_LENGTH) {
990 send_notification(p, 0,
991 NOTIF_BAD_PDU_LEN | NOTIF_FATAL);
992 ldp_peer_holddown(p);
993 break;
994 }
995 /* Check version */
996 if (ntohs(itlv->cs_version) != LDP_VERSION) {
997 send_notification(p, ntohl(itlv->messageid),
998 NOTIF_BAD_LDP_VER | NOTIF_FATAL);
999 ldp_peer_holddown(p);
1000 break;
1001 }
1002 /* Check if we got any hello from this one */
1003 SLIST_FOREACH(hi, &hello_info_head, infos)
1004 if (hi->ldp_id.s_addr == rpdu->ldp_id.s_addr)
1005 break;
1006 if (hi == NULL) {
1007 send_notification(p, ntohl(itlv->messageid),
1008 NOTIF_SESSION_REJECTED_NO_HELLO | NOTIF_FATAL);
1009 ldp_peer_holddown(p);
1010 break;
1011 }
1012
1013 if (!p->master) {
1014 keep_alive(p);
1015 send_initialize(p);
1016 } else {
1017 p->state = LDP_PEER_ESTABLISHED;
1018 p->established_t = time(NULL);
1019 keep_alive(p);
1020
1021 /*
1022 * Recheck here ldp id because we accepted
1023 * connection without knowing who is it for sure
1024 */
1025 p->ldp_id.s_addr = rpdu->ldp_id.s_addr;
1026
1027 fatalp("LDP neighbour %s is UP\n",
1028 inet_ntoa(p->ldp_id));
1029 mpls_add_ldp_peer(p);
1030 send_addresses(p);
1031 send_all_bindings(p);
1032 }
1033 break;
1034 case LDP_KEEPALIVE:
1035 if ((p->state == LDP_PEER_CONNECTED) && (!p->master)) {
1036 p->state = LDP_PEER_ESTABLISHED;
1037 p->established_t = time(NULL);
1038 fatalp("LDP neighbour %s is UP\n",
1039 inet_ntoa(p->ldp_id));
1040 mpls_add_ldp_peer(p);
1041 send_addresses(p);
1042 send_all_bindings(p);
1043 }
1044 p->timeout = p->holdtime;
1045 break;
1046 case LDP_ADDRESS:
1047 /* Add peer addresses */
1048 atlv = (struct address_tlv *) ttmp;
1049 altlv = (struct al_tlv *) (&atlv[1]);
1050 add_ifaddresses(p, altlv);
1051 print_bounded_addresses(p);
1052 break;
1053 case LDP_ADDRESS_WITHDRAW:
1054 atlv = (struct address_tlv *) ttmp;
1055 altlv = (struct al_tlv *) (&atlv[1]);
1056 del_ifaddresses(p, altlv);
1057 break;
1058 case LDP_LABEL_MAPPING:
1059 lmtlv = (struct label_map_tlv *) ttmp;
1060 fectlv = (struct fec_tlv *) (&lmtlv[1]);
1061 labeltlv = (struct label_tlv *)((unsigned char *)fectlv
1062 + ntohs(fectlv->length) + TLV_TYPE_LENGTH);
1063 map_label(p, fectlv, labeltlv);
1064 break;
1065 case LDP_LABEL_REQUEST:
1066 lmtlv = (struct label_map_tlv *) ttmp;
1067 fectlv = (struct fec_tlv *) (&lmtlv[1]);
1068 switch (request_respond(p, lmtlv, fectlv)) {
1069 case LDP_E_BAD_FEC:
1070 send_notification(p, ntohl(lmtlv->messageid),
1071 NOTIF_UNKNOWN_TLV);
1072 break;
1073 case LDP_E_BAD_AF:
1074 send_notification(p, ntohl(lmtlv->messageid),
1075 NOTIF_UNSUPPORTED_AF);
1076 break;
1077 case LDP_E_NO_SUCH_ROUTE:
1078 send_notification(p, ntohl(lmtlv->messageid),
1079 NOTIF_NO_ROUTE);
1080 break;
1081 }
1082 break;
1083 case LDP_LABEL_WITHDRAW:
1084 lmtlv = (struct label_map_tlv *) ttmp;
1085 fectlv = (struct fec_tlv *) (&lmtlv[1]);
1086 if (withdraw_label(p, fectlv) == LDP_E_OK) {
1087 /* Send RELEASE */
1088 prepare_release(ttmp);
1089 send_tlv(p, ttmp);
1090 }
1091 break;
1092 case LDP_LABEL_RELEASE:
1093 /*
1094 * XXX: we need to make a timed queue...
1095 * For now I just assume peers are processing messages
1096 * correctly so I just ignore confirmations
1097 */
1098 wo = -1; /* Ignore rest of message */
1099 break;
1100 case LDP_LABEL_ABORT:
1101 /* XXX: For now I pretend I can process everything
1102 * RFC 5036, Section 3.5.9.1
1103 * If an LSR receives a Label Abort Request Message after it
1104 * has responded to the Label Request in question with a Label
1105 * Mapping message or a Notification message, it ignores the
1106 * abort request.
1107 */
1108 wo = -1;
1109 break;
1110 case LDP_NOTIFICATION:
1111 nottlv = (struct notification_tlv *) ttmp;
1112 nottlv->st_code = ntohl(nottlv->st_code);
1113 fatalp("Got notification 0x%X from peer %s\n",
1114 nottlv->st_code, inet_ntoa(p->ldp_id));
1115 if (nottlv->st_code >> 31) {
1116 fatalp("LDP peer %s signalized %s\n",
1117 inet_ntoa(p->ldp_id),
1118 NOTIF_STR[(nottlv->st_code << 1) >> 1]);
1119 ldp_peer_holddown(p);
1120 wo = -1;
1121 }
1122 break;
1123 case LDP_HELLO:
1124 /* No hellos should came on tcp session */
1125 wo = -1;
1126 break;
1127 default:
1128 warnp("Unknown TLV received from %s\n",
1129 inet_ntoa(p->ldp_id));
1130 debug_tlv(ttmp);
1131 wo = -1;/* discard the rest of the message */
1132 break;
1133 }
1134 if (wo < 0) {
1135 debugp("Discarding the rest of the message\n");
1136 break;
1137 } else {
1138 wo += ntohs(ttmp->length) + TLV_TYPE_LENGTH;
1139 debugp("WORKED ON %u bytes (Left %d)\n", wo, c - wo);
1140 }
1141 } /* while */
1142
1143 }
1144
1145 /* Sends a pdu, tlv pair to a connected peer */
1146 int
1147 send_message(struct ldp_peer * p, struct ldp_pdu * pdu, struct tlv * t)
1148 {
1149 unsigned char sendspace[MAX_PDU_SIZE];
1150
1151 /* Check if peer is connected */
1152 switch (p->state) {
1153 case LDP_PEER_CONNECTED:
1154 case LDP_PEER_ESTABLISHED:
1155 break;
1156 default:
1157 return -1;
1158 }
1159
1160 /* Check length validity first */
1161 if (ntohs(pdu->length) !=
1162 ntohs(t->length) + TLV_TYPE_LENGTH + PDU_PAYLOAD_LENGTH) {
1163 fatalp("LDP: TLV - PDU incompability. Message discarded\n");
1164 fatalp("LDP: TLV len %d - PDU len %d\n", ntohs(t->length),
1165 ntohs(pdu->length));
1166 return -1;
1167 }
1168 if (ntohs(t->length) + PDU_VER_LENGTH > MAX_PDU_SIZE) {
1169 fatalp("Message to large discarded\n");
1170 return -1;
1171 }
1172 /* Arrange them in a buffer and send */
1173 memcpy(sendspace, pdu, sizeof(struct ldp_pdu));
1174 memcpy(sendspace + sizeof(struct ldp_pdu), t,
1175 ntohs(t->length) + TLV_TYPE_LENGTH);
1176
1177 /* Report keepalives only for DEBUG */
1178 if ((ntohs(t->type) != 0x201) && (ntohs(t->type) != 0x400)) {
1179 debugp("Sending message type 0x%.4X to %s (size: %d)\n",
1180 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
1181 } else
1182 /* downgraded from warnp to debugp for now */
1183 debugp("Sending message type 0x%.4X to %s (size: %d)\n",
1184 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
1185
1186 /* Send it finally */
1187 return send(p->socket, sendspace,
1188 ntohs(pdu->length) + PDU_VER_LENGTH, 0);
1189 }
1190
1191 /*
1192 * Encapsulates TLV into a PDU and sends it to a peer
1193 */
1194 int
1195 send_tlv(struct ldp_peer * p, struct tlv * t)
1196 {
1197 struct ldp_pdu pdu;
1198
1199 pdu.version = htons(LDP_VERSION);
1200 inet_aton(LDP_ID, &pdu.ldp_id);
1201 pdu.label_space = 0;
1202 pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH +
1203 PDU_PAYLOAD_LENGTH);
1204
1205 return send_message(p, &pdu, t);
1206 }
1207
1208
1209 int
1210 send_addresses(struct ldp_peer * p)
1211 {
1212 struct address_list_tlv *t;
1213 int ret;
1214
1215 t = build_address_list_tlv();
1216
1217 ret = send_tlv(p, (struct tlv *) t);
1218 free(t);
1219 return ret;
1220
1221 }
1222