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