]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/neighbor.c
Merge pull request #735 from qlyoung/fix-routemap
[mirror_frr.git] / ldpd / neighbor.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
7 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22 #include <zebra.h>
23
24 #include "ldpd.h"
25 #include "ldpe.h"
26 #include "lde.h"
27 #include "log.h"
28
29 static __inline int nbr_id_compare(const struct nbr *, const struct nbr *);
30 static __inline int nbr_addr_compare(const struct nbr *,
31 const struct nbr *);
32 static __inline int nbr_pid_compare(const struct nbr *,
33 const struct nbr *);
34 static void nbr_update_peerid(struct nbr *);
35 static int nbr_ktimer(struct thread *);
36 static void nbr_start_ktimer(struct nbr *);
37 static int nbr_ktimeout(struct thread *);
38 static void nbr_start_ktimeout(struct nbr *);
39 static int nbr_itimeout(struct thread *);
40 static void nbr_start_itimeout(struct nbr *);
41 static int nbr_idtimer(struct thread *);
42 static int nbr_act_session_operational(struct nbr *);
43 static void nbr_send_labelmappings(struct nbr *);
44 static __inline int nbr_params_compare(const struct nbr_params *,
45 const struct nbr_params *);
46
47 RB_GENERATE(nbr_id_head, nbr, id_tree, nbr_id_compare)
48 RB_GENERATE(nbr_addr_head, nbr, addr_tree, nbr_addr_compare)
49 RB_GENERATE(nbr_pid_head, nbr, pid_tree, nbr_pid_compare)
50 RB_GENERATE(nbrp_head, nbr_params, entry, nbr_params_compare)
51
52 struct {
53 int state;
54 enum nbr_event event;
55 enum nbr_action action;
56 int new_state;
57 } nbr_fsm_tbl[] = {
58 /* current state event that happened action to take resulting state */
59 /* Passive Role */
60 {NBR_STA_PRESENT, NBR_EVT_MATCH_ADJ, NBR_ACT_NOTHING, NBR_STA_INITIAL},
61 {NBR_STA_INITIAL, NBR_EVT_INIT_RCVD, NBR_ACT_PASSIVE_INIT, NBR_STA_OPENREC},
62 {NBR_STA_OPENREC, NBR_EVT_KEEPALIVE_RCVD, NBR_ACT_SESSION_EST, NBR_STA_OPER},
63 /* Active Role */
64 {NBR_STA_PRESENT, NBR_EVT_CONNECT_UP, NBR_ACT_CONNECT_SETUP, NBR_STA_INITIAL},
65 {NBR_STA_INITIAL, NBR_EVT_INIT_SENT, NBR_ACT_NOTHING, NBR_STA_OPENSENT},
66 {NBR_STA_OPENSENT, NBR_EVT_INIT_RCVD, NBR_ACT_KEEPALIVE_SEND, NBR_STA_OPENREC},
67 /* Session Maintenance */
68 {NBR_STA_OPER, NBR_EVT_PDU_RCVD, NBR_ACT_RST_KTIMEOUT, 0},
69 {NBR_STA_SESSION, NBR_EVT_PDU_RCVD, NBR_ACT_NOTHING, 0},
70 {NBR_STA_OPER, NBR_EVT_PDU_SENT, NBR_ACT_RST_KTIMER, 0},
71 {NBR_STA_SESSION, NBR_EVT_PDU_SENT, NBR_ACT_NOTHING, 0},
72 /* Session Close */
73 {NBR_STA_PRESENT, NBR_EVT_CLOSE_SESSION, NBR_ACT_NOTHING, 0},
74 {NBR_STA_SESSION, NBR_EVT_CLOSE_SESSION, NBR_ACT_CLOSE_SESSION, NBR_STA_PRESENT},
75 {-1, NBR_EVT_NOTHING, NBR_ACT_NOTHING, 0},
76 };
77
78 const char * const nbr_event_names[] = {
79 "NOTHING",
80 "ADJACENCY MATCHED",
81 "CONNECTION UP",
82 "SESSION CLOSE",
83 "INIT RECEIVED",
84 "KEEPALIVE RECEIVED",
85 "PDU RECEIVED",
86 "PDU SENT",
87 "INIT SENT"
88 };
89
90 const char * const nbr_action_names[] = {
91 "NOTHING",
92 "RESET KEEPALIVE TIMEOUT",
93 "START NEIGHBOR SESSION",
94 "RESET KEEPALIVE TIMER",
95 "SETUP NEIGHBOR CONNECTION",
96 "SEND INIT AND KEEPALIVE",
97 "SEND KEEPALIVE",
98 "CLOSE SESSION"
99 };
100
101 struct nbr_id_head nbrs_by_id = RB_INITIALIZER(&nbrs_by_id);
102 struct nbr_addr_head nbrs_by_addr = RB_INITIALIZER(&nbrs_by_addr);
103 struct nbr_pid_head nbrs_by_pid = RB_INITIALIZER(&nbrs_by_pid);
104
105 static __inline int
106 nbr_id_compare(const struct nbr *a, const struct nbr *b)
107 {
108 return (ntohl(a->id.s_addr) - ntohl(b->id.s_addr));
109 }
110
111 static __inline int
112 nbr_addr_compare(const struct nbr *a, const struct nbr *b)
113 {
114 if (a->af < b->af)
115 return (-1);
116 if (a->af > b->af)
117 return (1);
118
119 return (ldp_addrcmp(a->af, &a->raddr, &b->raddr));
120 }
121
122 static __inline int
123 nbr_pid_compare(const struct nbr *a, const struct nbr *b)
124 {
125 return (a->peerid - b->peerid);
126 }
127
128 int
129 nbr_fsm(struct nbr *nbr, enum nbr_event event)
130 {
131 struct timeval now;
132 int old_state;
133 int new_state = 0;
134 int i;
135
136 old_state = nbr->state;
137 for (i = 0; nbr_fsm_tbl[i].state != -1; i++)
138 if ((nbr_fsm_tbl[i].state & old_state) &&
139 (nbr_fsm_tbl[i].event == event)) {
140 new_state = nbr_fsm_tbl[i].new_state;
141 break;
142 }
143
144 if (nbr_fsm_tbl[i].state == -1) {
145 /* event outside of the defined fsm, ignore it. */
146 log_warnx("%s: lsr-id %s, event %s not expected in "
147 "state %s", __func__, inet_ntoa(nbr->id),
148 nbr_event_names[event], nbr_state_name(old_state));
149 return (0);
150 }
151
152 if (new_state != 0)
153 nbr->state = new_state;
154
155 if (old_state != nbr->state) {
156 log_debug("%s: event %s resulted in action %s and "
157 "changing state for lsr-id %s from %s to %s",
158 __func__, nbr_event_names[event],
159 nbr_action_names[nbr_fsm_tbl[i].action],
160 inet_ntoa(nbr->id), nbr_state_name(old_state),
161 nbr_state_name(nbr->state));
162
163 if (nbr->state == NBR_STA_OPER) {
164 gettimeofday(&now, NULL);
165 nbr->uptime = now.tv_sec;
166 }
167 }
168
169 if (nbr->state == NBR_STA_OPER || nbr->state == NBR_STA_PRESENT)
170 nbr_stop_itimeout(nbr);
171 else
172 nbr_start_itimeout(nbr);
173
174 switch (nbr_fsm_tbl[i].action) {
175 case NBR_ACT_RST_KTIMEOUT:
176 nbr_start_ktimeout(nbr);
177 break;
178 case NBR_ACT_RST_KTIMER:
179 nbr_start_ktimer(nbr);
180 break;
181 case NBR_ACT_SESSION_EST:
182 nbr_act_session_operational(nbr);
183 nbr_start_ktimer(nbr);
184 nbr_start_ktimeout(nbr);
185 if (nbr->v4_enabled)
186 send_address_all(nbr, AF_INET);
187 if (nbr->v6_enabled)
188 send_address_all(nbr, AF_INET6);
189 nbr_send_labelmappings(nbr);
190 break;
191 case NBR_ACT_CONNECT_SETUP:
192 nbr->tcp = tcp_new(nbr->fd, nbr);
193
194 /* trigger next state */
195 send_init(nbr);
196 nbr_fsm(nbr, NBR_EVT_INIT_SENT);
197 break;
198 case NBR_ACT_PASSIVE_INIT:
199 send_init(nbr);
200 send_keepalive(nbr);
201 break;
202 case NBR_ACT_KEEPALIVE_SEND:
203 nbr_start_ktimeout(nbr);
204 send_keepalive(nbr);
205 break;
206 case NBR_ACT_CLOSE_SESSION:
207 ldpe_imsg_compose_lde(IMSG_NEIGHBOR_DOWN, nbr->peerid, 0,
208 NULL, 0);
209 session_close(nbr);
210 break;
211 case NBR_ACT_NOTHING:
212 /* do nothing */
213 break;
214 }
215
216 return (0);
217 }
218
219 struct nbr *
220 nbr_new(struct in_addr id, int af, int ds_tlv, union ldpd_addr *addr,
221 uint32_t scope_id)
222 {
223 struct nbr *nbr;
224 struct nbr_params *nbrp;
225 struct adj *adj;
226 struct pending_conn *pconn;
227
228 log_debug("%s: lsr-id %s transport-address %s", __func__,
229 inet_ntoa(id), log_addr(af, addr));
230
231 if ((nbr = calloc(1, sizeof(*nbr))) == NULL)
232 fatal(__func__);
233
234 RB_INIT(nbr_adj_head, &nbr->adj_tree);
235 nbr->state = NBR_STA_PRESENT;
236 nbr->peerid = 0;
237 nbr->af = af;
238 nbr->ds_tlv = ds_tlv;
239 if (af == AF_INET || ds_tlv)
240 nbr->v4_enabled = 1;
241 if (af == AF_INET6 || ds_tlv)
242 nbr->v6_enabled = 1;
243 nbr->id = id;
244 nbr->laddr = (ldp_af_conf_get(leconf, af))->trans_addr;
245 nbr->raddr = *addr;
246 nbr->raddr_scope = scope_id;
247 nbr->conf_seqnum = 0;
248
249 RB_FOREACH(adj, global_adj_head, &global.adj_tree) {
250 if (adj->lsr_id.s_addr == nbr->id.s_addr) {
251 adj->nbr = nbr;
252 RB_INSERT(nbr_adj_head, &nbr->adj_tree, adj);
253 }
254 }
255
256 if (RB_INSERT(nbr_id_head, &nbrs_by_id, nbr) != NULL)
257 fatalx("nbr_new: RB_INSERT(nbrs_by_id) failed");
258 if (RB_INSERT(nbr_addr_head, &nbrs_by_addr, nbr) != NULL)
259 fatalx("nbr_new: RB_INSERT(nbrs_by_addr) failed");
260
261 TAILQ_INIT(&nbr->mapping_list);
262 TAILQ_INIT(&nbr->withdraw_list);
263 TAILQ_INIT(&nbr->request_list);
264 TAILQ_INIT(&nbr->release_list);
265 TAILQ_INIT(&nbr->abortreq_list);
266
267 nbrp = nbr_params_find(leconf, nbr->id);
268 if (nbrp) {
269 nbr->auth.method = nbrp->auth.method;
270 #ifdef __OpenBSD__
271 if (pfkey_establish(nbr, nbrp) == -1)
272 fatalx("pfkey setup failed");
273 #else
274 sock_set_md5sig(
275 (ldp_af_global_get(&global, nbr->af))->ldp_session_socket,
276 nbr->af, &nbr->raddr, nbrp->auth.md5key);
277 #endif
278 }
279
280 pconn = pending_conn_find(nbr->af, &nbr->raddr);
281 if (pconn) {
282 session_accept_nbr(nbr, pconn->fd);
283 pending_conn_del(pconn);
284 }
285
286 return (nbr);
287 }
288
289 void
290 nbr_del(struct nbr *nbr)
291 {
292 log_debug("%s: lsr-id %s", __func__, inet_ntoa(nbr->id));
293
294 nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION);
295 #ifdef __OpenBSD__
296 pfkey_remove(nbr);
297 #else
298 sock_set_md5sig(
299 (ldp_af_global_get(&global, nbr->af))->ldp_session_socket,
300 nbr->af, &nbr->raddr, NULL);
301 #endif
302 nbr->auth.method = AUTH_NONE;
303
304 if (nbr_pending_connect(nbr))
305 THREAD_WRITE_OFF(nbr->ev_connect);
306 nbr_stop_ktimer(nbr);
307 nbr_stop_ktimeout(nbr);
308 nbr_stop_itimeout(nbr);
309 nbr_stop_idtimer(nbr);
310
311 mapping_list_clr(&nbr->mapping_list);
312 mapping_list_clr(&nbr->withdraw_list);
313 mapping_list_clr(&nbr->request_list);
314 mapping_list_clr(&nbr->release_list);
315 mapping_list_clr(&nbr->abortreq_list);
316
317 if (nbr->peerid)
318 RB_REMOVE(nbr_pid_head, &nbrs_by_pid, nbr);
319 RB_REMOVE(nbr_id_head, &nbrs_by_id, nbr);
320 RB_REMOVE(nbr_addr_head, &nbrs_by_addr, nbr);
321
322 free(nbr);
323 }
324
325 static void
326 nbr_update_peerid(struct nbr *nbr)
327 {
328 static uint32_t peercnt = 1;
329
330 if (nbr->peerid)
331 RB_REMOVE(nbr_pid_head, &nbrs_by_pid, nbr);
332
333 /* get next unused peerid */
334 while (nbr_find_peerid(++peercnt))
335 ;
336 nbr->peerid = peercnt;
337
338 if (RB_INSERT(nbr_pid_head, &nbrs_by_pid, nbr) != NULL)
339 fatalx("nbr_update_peerid: RB_INSERT(nbrs_by_pid) failed");
340 }
341
342 struct nbr *
343 nbr_find_ldpid(uint32_t lsr_id)
344 {
345 struct nbr n;
346 n.id.s_addr = lsr_id;
347 return (RB_FIND(nbr_id_head, &nbrs_by_id, &n));
348 }
349
350 struct nbr *
351 nbr_find_addr(int af, union ldpd_addr *addr)
352 {
353 struct nbr n;
354 n.af = af;
355 n.raddr = *addr;
356 return (RB_FIND(nbr_addr_head, &nbrs_by_addr, &n));
357 }
358
359 struct nbr *
360 nbr_find_peerid(uint32_t peerid)
361 {
362 struct nbr n;
363 n.peerid = peerid;
364 return (RB_FIND(nbr_pid_head, &nbrs_by_pid, &n));
365 }
366
367 int
368 nbr_adj_count(struct nbr *nbr, int af)
369 {
370 struct adj *adj;
371 int total = 0;
372
373 RB_FOREACH(adj, nbr_adj_head, &nbr->adj_tree)
374 if (adj_get_af(adj) == af)
375 total++;
376
377 return (total);
378 }
379
380 int
381 nbr_session_active_role(struct nbr *nbr)
382 {
383 if (ldp_addrcmp(nbr->af, &nbr->laddr, &nbr->raddr) > 0)
384 return (1);
385
386 return (0);
387 }
388
389 /* timers */
390
391 /* Keepalive timer: timer to send keepalive message to neighbors */
392
393 static int
394 nbr_ktimer(struct thread *thread)
395 {
396 struct nbr *nbr = THREAD_ARG(thread);
397
398 nbr->keepalive_timer = NULL;
399 send_keepalive(nbr);
400 nbr_start_ktimer(nbr);
401
402 return (0);
403 }
404
405 static void
406 nbr_start_ktimer(struct nbr *nbr)
407 {
408 int secs;
409
410 /* send three keepalives per period */
411 secs = nbr->keepalive / KEEPALIVE_PER_PERIOD;
412 THREAD_TIMER_OFF(nbr->keepalive_timer);
413 nbr->keepalive_timer = NULL;
414 thread_add_timer(master, nbr_ktimer, nbr, secs, &nbr->keepalive_timer);
415 }
416
417 void
418 nbr_stop_ktimer(struct nbr *nbr)
419 {
420 THREAD_TIMER_OFF(nbr->keepalive_timer);
421 }
422
423 /* Keepalive timeout: if the nbr hasn't sent keepalive */
424
425 static int
426 nbr_ktimeout(struct thread *thread)
427 {
428 struct nbr *nbr = THREAD_ARG(thread);
429
430 nbr->keepalive_timeout = NULL;
431
432 log_debug("%s: lsr-id %s", __func__, inet_ntoa(nbr->id));
433
434 session_shutdown(nbr, S_KEEPALIVE_TMR, 0, 0);
435
436 return (0);
437 }
438
439 static void
440 nbr_start_ktimeout(struct nbr *nbr)
441 {
442 THREAD_TIMER_OFF(nbr->keepalive_timeout);
443 nbr->keepalive_timeout = NULL;
444 thread_add_timer(master, nbr_ktimeout, nbr, nbr->keepalive,
445 &nbr->keepalive_timeout);
446 }
447
448 void
449 nbr_stop_ktimeout(struct nbr *nbr)
450 {
451 THREAD_TIMER_OFF(nbr->keepalive_timeout);
452 }
453
454 /* Session initialization timeout: if nbr got stuck in the initialization FSM */
455
456 static int
457 nbr_itimeout(struct thread *thread)
458 {
459 struct nbr *nbr = THREAD_ARG(thread);
460
461 log_debug("%s: lsr-id %s", __func__, inet_ntoa(nbr->id));
462
463 nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION);
464
465 return (0);
466 }
467
468 static void
469 nbr_start_itimeout(struct nbr *nbr)
470 {
471 int secs;
472
473 secs = INIT_FSM_TIMEOUT;
474 THREAD_TIMER_OFF(nbr->init_timeout);
475 nbr->init_timeout = NULL;
476 thread_add_timer(master, nbr_itimeout, nbr, secs, &nbr->init_timeout);
477 }
478
479 void
480 nbr_stop_itimeout(struct nbr *nbr)
481 {
482 THREAD_TIMER_OFF(nbr->init_timeout);
483 }
484
485 /* Init delay timer: timer to retry to iniziatize session */
486
487 static int
488 nbr_idtimer(struct thread *thread)
489 {
490 struct nbr *nbr = THREAD_ARG(thread);
491
492 nbr->initdelay_timer = NULL;
493
494 log_debug("%s: lsr-id %s", __func__, inet_ntoa(nbr->id));
495
496 nbr_establish_connection(nbr);
497
498 return (0);
499 }
500
501 void
502 nbr_start_idtimer(struct nbr *nbr)
503 {
504 int secs;
505
506 secs = INIT_DELAY_TMR;
507 switch(nbr->idtimer_cnt) {
508 default:
509 /* do not further increase the counter */
510 secs = MAX_DELAY_TMR;
511 break;
512 case 2:
513 secs *= 2;
514 /* FALLTHROUGH */
515 case 1:
516 secs *= 2;
517 /* FALLTHROUGH */
518 case 0:
519 nbr->idtimer_cnt++;
520 break;
521 }
522
523 THREAD_TIMER_OFF(nbr->initdelay_timer);
524 nbr->initdelay_timer = NULL;
525 thread_add_timer(master, nbr_idtimer, nbr, secs,
526 &nbr->initdelay_timer);
527 }
528
529 void
530 nbr_stop_idtimer(struct nbr *nbr)
531 {
532 THREAD_TIMER_OFF(nbr->initdelay_timer);
533 }
534
535 int
536 nbr_pending_idtimer(struct nbr *nbr)
537 {
538 return (nbr->initdelay_timer != NULL);
539 }
540
541 int
542 nbr_pending_connect(struct nbr *nbr)
543 {
544 return (nbr->ev_connect != NULL);
545 }
546
547 static int
548 nbr_connect_cb(struct thread *thread)
549 {
550 struct nbr *nbr = THREAD_ARG(thread);
551 int error;
552 socklen_t len;
553
554 nbr->ev_connect = NULL;
555
556 len = sizeof(error);
557 if (getsockopt(nbr->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
558 log_warn("%s: getsockopt SOL_SOCKET SO_ERROR", __func__);
559 return (0);
560 }
561
562 if (error) {
563 close(nbr->fd);
564 errno = error;
565 log_debug("%s: error while connecting to %s: %s", __func__,
566 log_addr(nbr->af, &nbr->raddr), strerror(errno));
567 return (0);
568 }
569
570 nbr_fsm(nbr, NBR_EVT_CONNECT_UP);
571
572 return (0);
573 }
574
575 int
576 nbr_establish_connection(struct nbr *nbr)
577 {
578 struct sockaddr_storage local_sa;
579 struct sockaddr_storage remote_sa;
580 struct adj *adj;
581 struct nbr_params *nbrp;
582 #ifdef __OpenBSD__
583 int opt = 1;
584 #endif
585
586 nbr->fd = socket(nbr->af, SOCK_STREAM, 0);
587 if (nbr->fd == -1) {
588 log_warn("%s: error while creating socket", __func__);
589 return (-1);
590 }
591 sock_set_nonblock(nbr->fd);
592
593 nbrp = nbr_params_find(leconf, nbr->id);
594 if (nbrp && nbrp->auth.method == AUTH_MD5SIG) {
595 #ifdef __OpenBSD__
596 if (sysdep.no_pfkey || sysdep.no_md5sig) {
597 log_warnx("md5sig configured but not available");
598 close(nbr->fd);
599 return (-1);
600 }
601 if (setsockopt(nbr->fd, IPPROTO_TCP, TCP_MD5SIG,
602 &opt, sizeof(opt)) == -1) {
603 log_warn("setsockopt md5sig");
604 close(nbr->fd);
605 return (-1);
606 }
607 #else
608 sock_set_md5sig(nbr->fd, nbr->af, &nbr->raddr,
609 nbrp->auth.md5key);
610 #endif
611 }
612
613 memcpy(&local_sa, addr2sa(nbr->af, &nbr->laddr, 0), sizeof(local_sa));
614 memcpy(&remote_sa, addr2sa(nbr->af, &nbr->raddr, LDP_PORT),
615 sizeof(local_sa));
616 if (nbr->af == AF_INET6 && nbr->raddr_scope)
617 addscope((struct sockaddr_in6 *)&remote_sa, nbr->raddr_scope);
618
619 if (bind(nbr->fd, (struct sockaddr *)&local_sa,
620 sockaddr_len((struct sockaddr *)&local_sa)) == -1) {
621 log_warn("%s: error while binding socket to %s", __func__,
622 log_sockaddr((struct sockaddr *)&local_sa));
623 close(nbr->fd);
624 return (-1);
625 }
626
627 if (nbr_gtsm_check(nbr->fd, nbr, nbrp)) {
628 close(nbr->fd);
629 return (-1);
630 }
631
632 /*
633 * Send an extra hello to guarantee that the remote peer has formed
634 * an adjacency as well.
635 */
636 RB_FOREACH(adj, nbr_adj_head, &nbr->adj_tree)
637 send_hello(adj->source.type, adj->source.link.ia,
638 adj->source.target);
639
640 if (connect(nbr->fd, (struct sockaddr *)&remote_sa,
641 sockaddr_len((struct sockaddr *)&remote_sa)) == -1) {
642 if (errno == EINPROGRESS) {
643 thread_add_write(master, nbr_connect_cb, nbr, nbr->fd,
644 &nbr->ev_connect);
645 return (0);
646 }
647 log_warn("%s: error while connecting to %s", __func__,
648 log_sockaddr((struct sockaddr *)&remote_sa));
649 close(nbr->fd);
650 return (-1);
651 }
652
653 /* connection completed immediately */
654 nbr_fsm(nbr, NBR_EVT_CONNECT_UP);
655
656 return (0);
657 }
658
659 int
660 nbr_gtsm_enabled(struct nbr *nbr, struct nbr_params *nbrp)
661 {
662 /*
663 * RFC 6720 - Section 3:
664 * "This document allows for the implementation to provide an option to
665 * statically (e.g., via configuration) and/or dynamically override the
666 * default behavior and enable/disable GTSM on a per-peer basis".
667 */
668 if (nbrp && (nbrp->flags & F_NBRP_GTSM))
669 return (nbrp->gtsm_enabled);
670
671 if ((ldp_af_conf_get(leconf, nbr->af))->flags & F_LDPD_AF_NO_GTSM)
672 return (0);
673
674 /* By default, GTSM support has to be negotiated for LDPv4 */
675 if (nbr->af == AF_INET && !(nbr->flags & F_NBR_GTSM_NEGOTIATED))
676 return (0);
677
678 return (1);
679 }
680
681 int
682 nbr_gtsm_setup(int fd, int af, struct nbr_params *nbrp)
683 {
684 int ttl = 255;
685
686 if (nbrp && (nbrp->flags & F_NBRP_GTSM_HOPS))
687 ttl = 256 - nbrp->gtsm_hops;
688
689 switch (af) {
690 case AF_INET:
691 if (sock_set_ipv4_minttl(fd, ttl) == -1)
692 return (-1);
693 ttl = 255;
694 if (sock_set_ipv4_ucast_ttl(fd, ttl) == -1)
695 return (-1);
696 break;
697 case AF_INET6:
698 /* ignore any possible error */
699 sock_set_ipv6_minhopcount(fd, ttl);
700 ttl = 255;
701 if (sock_set_ipv6_ucast_hops(fd, ttl) == -1)
702 return (-1);
703 break;
704 default:
705 fatalx("nbr_gtsm_setup: unknown af");
706 }
707
708 return (0);
709 }
710
711 int
712 nbr_gtsm_check(int fd, struct nbr *nbr, struct nbr_params *nbrp)
713 {
714 if (!nbr_gtsm_enabled(nbr, nbrp)) {
715 switch (nbr->af) {
716 case AF_INET:
717 sock_set_ipv4_ucast_ttl(fd, -1);
718 break;
719 case AF_INET6:
720 /*
721 * Send packets with a Hop Limit of 255 even when GSTM
722 * is disabled to guarantee interoperability.
723 */
724 sock_set_ipv6_ucast_hops(fd, 255);
725 break;
726 default:
727 fatalx("nbr_gtsm_check: unknown af");
728 break;
729 }
730 return (0);
731 }
732
733 if (nbr_gtsm_setup(fd, nbr->af, nbrp) == -1) {
734 log_warnx("%s: error enabling GTSM for lsr-id %s", __func__,
735 inet_ntoa(nbr->id));
736 return (-1);
737 }
738
739 return (0);
740 }
741
742 static int
743 nbr_act_session_operational(struct nbr *nbr)
744 {
745 struct lde_nbr lde_nbr;
746
747 nbr->idtimer_cnt = 0;
748
749 /* this is necessary to avoid ipc synchronization issues */
750 nbr_update_peerid(nbr);
751
752 memset(&lde_nbr, 0, sizeof(lde_nbr));
753 lde_nbr.id = nbr->id;
754 lde_nbr.v4_enabled = nbr->v4_enabled;
755 lde_nbr.v6_enabled = nbr->v6_enabled;
756 lde_nbr.flags = nbr->flags;
757 return (ldpe_imsg_compose_lde(IMSG_NEIGHBOR_UP, nbr->peerid, 0,
758 &lde_nbr, sizeof(lde_nbr)));
759 }
760
761 static void
762 nbr_send_labelmappings(struct nbr *nbr)
763 {
764 ldpe_imsg_compose_lde(IMSG_LABEL_MAPPING_FULL, nbr->peerid, 0,
765 NULL, 0);
766 }
767
768 static __inline int
769 nbr_params_compare(const struct nbr_params *a, const struct nbr_params *b)
770 {
771 return (ntohl(a->lsr_id.s_addr) - ntohl(b->lsr_id.s_addr));
772 }
773
774 struct nbr_params *
775 nbr_params_new(struct in_addr lsr_id)
776 {
777 struct nbr_params *nbrp;
778
779 if ((nbrp = calloc(1, sizeof(*nbrp))) == NULL)
780 fatal(__func__);
781
782 nbrp->lsr_id = lsr_id;
783 nbrp->auth.method = AUTH_NONE;
784
785 return (nbrp);
786 }
787
788 struct nbr_params *
789 nbr_params_find(struct ldpd_conf *xconf, struct in_addr lsr_id)
790 {
791 struct nbr_params nbrp;
792 nbrp.lsr_id = lsr_id;
793 return (RB_FIND(nbrp_head, &xconf->nbrp_tree, &nbrp));
794 }
795
796 uint16_t
797 nbr_get_keepalive(int af, struct in_addr lsr_id)
798 {
799 struct nbr_params *nbrp;
800
801 nbrp = nbr_params_find(leconf, lsr_id);
802 if (nbrp && (nbrp->flags & F_NBRP_KEEPALIVE))
803 return (nbrp->keepalive);
804
805 return ((ldp_af_conf_get(leconf, af))->keepalive);
806 }
807
808 struct ctl_nbr *
809 nbr_to_ctl(struct nbr *nbr)
810 {
811 static struct ctl_nbr nctl;
812 struct timeval now;
813
814 nctl.af = nbr->af;
815 nctl.id = nbr->id;
816 nctl.laddr = nbr->laddr;
817 nctl.lport = nbr->tcp->lport;
818 nctl.raddr = nbr->raddr;
819 nctl.rport = nbr->tcp->rport;
820 nctl.auth_method = nbr->auth.method;
821 nctl.holdtime = nbr->keepalive;
822 nctl.nbr_state = nbr->state;
823 nctl.stats = nbr->stats;
824 nctl.flags = nbr->flags;
825
826 gettimeofday(&now, NULL);
827 if (nbr->state == NBR_STA_OPER) {
828 nctl.uptime = now.tv_sec - nbr->uptime;
829 } else
830 nctl.uptime = 0;
831
832 return (&nctl);
833 }
834
835 void
836 nbr_clear_ctl(struct ctl_nbr *nctl)
837 {
838 struct nbr *nbr;
839
840 RB_FOREACH(nbr, nbr_addr_head, &nbrs_by_addr) {
841 if (ldp_addrisset(nctl->af, &nctl->raddr) &&
842 ldp_addrcmp(nctl->af, &nctl->raddr, &nbr->raddr))
843 continue;
844
845 log_debug("%s: neighbor %s manually cleared", __func__,
846 log_addr(nbr->af, &nbr->raddr));
847 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
848 }
849 }