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