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