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