]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.c
235f9c06a8f242c49a6d49e84406644a49c87271
[mirror_frr.git] / bfdd / bfd.c
1 /*********************************************************************
2 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * bfd.c: implements the BFD protocol.
20 *
21 * Authors
22 * -------
23 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
24 * Kanna Rajagopal [kanna@cumulusnetworks.com]
25 * Radhika Mahankali [Radhika@cumulusnetworks.com]
26 */
27
28 #include <zebra.h>
29
30 #include "lib/jhash.h"
31
32 #include "bfd.h"
33
34 DEFINE_QOBJ_TYPE(bfd_session);
35
36 /*
37 * Prototypes
38 */
39 static uint32_t ptm_bfd_gen_ID(void);
40 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
41 static void bfd_session_free(struct bfd_session *bs);
42 static struct bfd_session *bfd_session_new(int sd);
43 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
44 uint32_t ldisc);
45 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
46 static const char *get_diag_str(int diag);
47
48
49 /*
50 * Functions
51 */
52 struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
53 {
54 struct bfd_session *bs;
55 struct peer_label *pl;
56 struct bfd_mhop_key mhop;
57 struct bfd_shop_key shop;
58
59 /* Try to find label first. */
60 if (bpc->bpc_has_label) {
61 pl = pl_find(bpc->bpc_label);
62 if (pl != NULL) {
63 bs = pl->pl_bs;
64 return bs;
65 }
66 }
67
68 /* Otherwise fallback to peer/local hash lookup. */
69 if (bpc->bpc_mhop) {
70 memset(&mhop, 0, sizeof(mhop));
71 mhop.peer = bpc->bpc_peer;
72 mhop.local = bpc->bpc_local;
73 if (bpc->bpc_has_vrfname)
74 strlcpy(mhop.vrf_name, bpc->bpc_vrfname,
75 sizeof(mhop.vrf_name));
76
77 bs = bfd_mhop_lookup(mhop);
78 } else {
79 memset(&shop, 0, sizeof(shop));
80 shop.peer = bpc->bpc_peer;
81 if (bpc->bpc_has_localif)
82 strlcpy(shop.port_name, bpc->bpc_localif,
83 sizeof(shop.port_name));
84
85 bs = bfd_shop_lookup(shop);
86 }
87
88 return bs;
89 }
90
91 static uint32_t ptm_bfd_gen_ID(void)
92 {
93 static uint32_t sessionID = 1;
94
95 return (sessionID++);
96 }
97
98 void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
99 {
100 uint64_t jitter, xmt_TO;
101 int maxpercent;
102
103 xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
104
105 /*
106 * From section 6.5.2: trasmit interval should be randomly jittered
107 * between
108 * 75% and 100% of nominal value, unless detect_mult is 1, then should
109 * be
110 * between 75% and 90%.
111 */
112 maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
113 jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100;
114 /* XXX remove that division above */
115
116 if (is_echo)
117 bfd_echo_xmttimer_update(bfd, jitter);
118 else
119 bfd_xmttimer_update(bfd, jitter);
120 }
121
122 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
123 {
124 /* Send the scheduled echo packet */
125 ptm_bfd_echo_snd(bfd);
126
127 /* Restart the timer for next time */
128 ptm_bfd_start_xmt_timer(bfd, true);
129 }
130
131 void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
132 {
133 /* Send the scheduled control packet */
134 ptm_bfd_snd(bfd, fbit);
135
136 /* Restart the timer for next time */
137 ptm_bfd_start_xmt_timer(bfd, false);
138 }
139
140 void ptm_bfd_echo_stop(struct bfd_session *bfd, int polling)
141 {
142 bfd->echo_xmt_TO = 0;
143 bfd->echo_detect_TO = 0;
144 BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
145
146 bfd_echo_xmttimer_delete(bfd);
147 bfd_echo_recvtimer_delete(bfd);
148
149 if (polling) {
150 bfd->polling = polling;
151 bfd->new_timers.desired_min_tx = bfd->up_min_tx;
152 bfd->new_timers.required_min_rx = bfd->timers.required_min_rx;
153 ptm_bfd_snd(bfd, 0);
154 }
155 }
156
157 void ptm_bfd_echo_start(struct bfd_session *bfd)
158 {
159 bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
160 if (bfd->echo_detect_TO > 0)
161 ptm_bfd_echo_xmt_TO(bfd);
162
163 bfd->polling = 1;
164 bfd->new_timers.desired_min_tx = bfd->up_min_tx;
165 bfd->new_timers.required_min_rx = bfd->timers.required_min_rx;
166 ptm_bfd_snd(bfd, 0);
167 }
168
169 void ptm_bfd_ses_up(struct bfd_session *bfd)
170 {
171 int old_state = bfd->ses_state;
172
173 bfd->local_diag = 0;
174 bfd->ses_state = PTM_BFD_UP;
175 bfd->polling = 1;
176 monotime(&bfd->uptime);
177
178 /* If the peer is capable to receiving Echo pkts */
179 if (bfd->echo_xmt_TO && !BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MH)) {
180 ptm_bfd_echo_start(bfd);
181 } else {
182 bfd->new_timers.desired_min_tx = bfd->up_min_tx;
183 bfd->new_timers.required_min_rx = bfd->timers.required_min_rx;
184 ptm_bfd_snd(bfd, 0);
185 }
186
187 control_notify(bfd);
188
189 if (old_state != bfd->ses_state) {
190 bfd->stats.session_up++;
191 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd),
192 state_list[old_state].str,
193 state_list[bfd->ses_state].str);
194 }
195 }
196
197 void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag)
198 {
199 int old_state = bfd->ses_state;
200
201 bfd->local_diag = diag;
202 bfd->discrs.remote_discr = 0;
203 bfd->ses_state = PTM_BFD_DOWN;
204 bfd->polling = 0;
205 bfd->demand_mode = 0;
206 monotime(&bfd->downtime);
207
208 ptm_bfd_snd(bfd, 0);
209
210 /* only signal clients when going from up->down state */
211 if (old_state == PTM_BFD_UP)
212 control_notify(bfd);
213
214 /* Stop echo packet transmission if they are active */
215 if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
216 ptm_bfd_echo_stop(bfd, 0);
217
218 if (old_state != bfd->ses_state) {
219 bfd->stats.session_down++;
220 log_info("state-change: [%s] %s -> %s reason:%s",
221 bs_to_string(bfd), state_list[old_state].str,
222 state_list[bfd->ses_state].str,
223 get_diag_str(bfd->local_diag));
224 }
225 }
226
227 static int ptm_bfd_get_vrf_name(char *port_name, char *vrf_name)
228 {
229 struct bfd_iface *iface;
230 struct bfd_vrf *vrf;
231
232 if ((port_name == NULL) || (vrf_name == NULL))
233 return -1;
234
235 iface = bfd_iface_lookup(port_name);
236 if (iface) {
237 vrf = bfd_vrf_lookup(iface->vrf_id);
238 if (vrf) {
239 strlcpy(vrf_name, vrf->name, sizeof(vrf->name));
240 return 0;
241 }
242 }
243 return -1;
244 }
245
246 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
247 uint32_t ldisc)
248 {
249 struct bfd_session *bs;
250
251 bs = bfd_id_lookup(ldisc);
252 if (bs == NULL)
253 return NULL;
254
255 /* Remove unused fields. */
256 switch (sa->sa_sin.sin_family) {
257 case AF_INET:
258 sa->sa_sin.sin_port = 0;
259 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin)) == 0)
260 return bs;
261 break;
262 case AF_INET6:
263 sa->sa_sin6.sin6_port = 0;
264 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin6)) == 0)
265 return bs;
266 break;
267 }
268
269 return NULL;
270 }
271
272 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name,
273 struct sockaddr_any *peer,
274 struct sockaddr_any *local,
275 char *vrf_name, bool is_mhop)
276 {
277 struct bfd_session *l_bfd = NULL;
278 struct bfd_mhop_key mhop;
279 struct bfd_shop_key shop;
280 char vrf_buf[MAXNAMELEN];
281
282 /* Find our session using the ID signaled by the remote end. */
283 if (cp->discrs.remote_discr)
284 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
285
286 /* Search for session without using discriminator. */
287 if (is_mhop) {
288 memset(&mhop, 0, sizeof(mhop));
289 mhop.peer = *peer;
290 mhop.local = *local;
291 if (vrf_name && vrf_name[0]) {
292 strlcpy(mhop.vrf_name, vrf_name, sizeof(mhop.vrf_name));
293 } else if (port_name && port_name[0]) {
294 memset(vrf_buf, 0, sizeof(vrf_buf));
295 if (ptm_bfd_get_vrf_name(port_name, vrf_buf) != -1)
296 strlcpy(mhop.vrf_name, vrf_buf,
297 sizeof(mhop.vrf_name));
298 }
299
300 l_bfd = bfd_mhop_lookup(mhop);
301 } else {
302 memset(&shop, 0, sizeof(shop));
303 shop.peer = *peer;
304 if (port_name && port_name[0])
305 strlcpy(shop.port_name, port_name,
306 sizeof(shop.port_name));
307
308 l_bfd = bfd_shop_lookup(shop);
309 }
310
311 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
312 return l_bfd;
313 }
314
315 int bfd_xmt_cb(struct thread *t)
316 {
317 struct bfd_session *bs = THREAD_ARG(t);
318
319 ptm_bfd_xmt_TO(bs, 0);
320
321 return 0;
322 }
323
324 int bfd_echo_xmt_cb(struct thread *t)
325 {
326 struct bfd_session *bs = THREAD_ARG(t);
327
328 if (bs->echo_xmt_TO > 0)
329 ptm_bfd_echo_xmt_TO(bs);
330
331 return 0;
332 }
333
334 /* Was ptm_bfd_detect_TO() */
335 int bfd_recvtimer_cb(struct thread *t)
336 {
337 struct bfd_session *bs = THREAD_ARG(t);
338
339 switch (bs->ses_state) {
340 case PTM_BFD_INIT:
341 case PTM_BFD_UP:
342 ptm_bfd_ses_dn(bs, BD_CONTROL_EXPIRED);
343 bfd_recvtimer_update(bs);
344 break;
345
346 default:
347 /* Second detect time expiration, zero remote discr (section
348 * 6.5.1)
349 */
350 bs->discrs.remote_discr = 0;
351 break;
352 }
353
354 return 0;
355 }
356
357 /* Was ptm_bfd_echo_detect_TO() */
358 int bfd_echo_recvtimer_cb(struct thread *t)
359 {
360 struct bfd_session *bs = THREAD_ARG(t);
361
362 switch (bs->ses_state) {
363 case PTM_BFD_INIT:
364 case PTM_BFD_UP:
365 ptm_bfd_ses_dn(bs, BD_ECHO_FAILED);
366 break;
367 }
368
369 return 0;
370 }
371
372 static struct bfd_session *bfd_session_new(int sd)
373 {
374 struct bfd_session *bs;
375
376 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
377 if (bs == NULL)
378 return NULL;
379
380 QOBJ_REG(bs, bfd_session);
381
382 bs->up_min_tx = BFD_DEFDESIREDMINTX;
383 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
384 bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO;
385 bs->detect_mult = BFD_DEFDETECTMULT;
386 bs->mh_ttl = BFD_DEF_MHOP_TTL;
387
388 bs->sock = sd;
389 monotime(&bs->uptime);
390 bs->downtime = bs->uptime;
391
392 return bs;
393 }
394
395 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
396 {
397 /* New label treatment:
398 * - Check if the label is taken;
399 * - Try to allocate the memory for it and register;
400 */
401 if (bs->pl == NULL) {
402 if (pl_find(nlabel) != NULL) {
403 /* Someone is already using it. */
404 return -1;
405 }
406
407 if (pl_new(nlabel, bs) == NULL)
408 return -1;
409
410 return 0;
411 }
412
413 /*
414 * Test label change consistency:
415 * - Do nothing if it's the same label;
416 * - Check if the future label is already taken;
417 * - Change label;
418 */
419 if (strcmp(nlabel, bs->pl->pl_label) == 0)
420 return -1;
421 if (pl_find(nlabel) != NULL)
422 return -1;
423
424 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
425 return 0;
426 }
427
428 static void _bfd_session_update(struct bfd_session *bs,
429 struct bfd_peer_cfg *bpc)
430 {
431 if (bpc->bpc_echo) {
432 /* Check if echo mode is already active. */
433 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
434 goto skip_echo;
435
436 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
437 ptm_bfd_echo_start(bs);
438
439 /* Activate/update echo receive timeout timer. */
440 bfd_echo_recvtimer_update(bs);
441 } else {
442 /* Check if echo mode is already disabled. */
443 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
444 goto skip_echo;
445
446 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
447 ptm_bfd_echo_stop(bs, 0);
448 }
449
450 skip_echo:
451 if (bpc->bpc_has_txinterval)
452 bs->up_min_tx = bpc->bpc_txinterval * 1000;
453
454 if (bpc->bpc_has_recvinterval)
455 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
456
457 if (bpc->bpc_has_detectmultiplier)
458 bs->detect_mult = bpc->bpc_detectmultiplier;
459
460 if (bpc->bpc_has_echointerval)
461 bs->timers.required_min_echo = bpc->bpc_echointerval * 1000;
462
463 if (bpc->bpc_has_label)
464 bfd_session_update_label(bs, bpc->bpc_label);
465
466 if (bpc->bpc_shutdown) {
467 /* Check if already shutdown. */
468 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
469 return;
470
471 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
472
473 /* Disable all events. */
474 bfd_recvtimer_delete(bs);
475 bfd_echo_recvtimer_delete(bs);
476 bfd_xmttimer_delete(bs);
477 bfd_echo_xmttimer_delete(bs);
478
479 /* Change and notify state change. */
480 bs->ses_state = PTM_BFD_ADM_DOWN;
481 control_notify(bs);
482
483 ptm_bfd_snd(bs, 0);
484 } else {
485 /* Check if already working. */
486 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
487 return;
488
489 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
490
491 /* Change and notify state change. */
492 bs->ses_state = PTM_BFD_DOWN;
493 control_notify(bs);
494
495 /* Enable all timers. */
496 bfd_recvtimer_update(bs);
497 bfd_xmttimer_update(bs, bs->xmt_TO);
498 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) {
499 bfd_echo_recvtimer_update(bs);
500 bfd_echo_xmttimer_update(bs, bs->echo_xmt_TO);
501 }
502 }
503 }
504
505 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
506 {
507 /* User didn't want to update, return failure. */
508 if (bpc->bpc_createonly)
509 return -1;
510
511 _bfd_session_update(bs, bpc);
512
513 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
514
515 return 0;
516 }
517
518 static void bfd_session_free(struct bfd_session *bs)
519 {
520 if (bs->sock != -1)
521 close(bs->sock);
522
523 bfd_recvtimer_delete(bs);
524 bfd_echo_recvtimer_delete(bs);
525 bfd_xmttimer_delete(bs);
526 bfd_echo_xmttimer_delete(bs);
527
528 bfd_id_delete(bs->discrs.my_discr);
529 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
530 bfd_mhop_delete(bs->mhop);
531 else
532 bfd_shop_delete(bs->shop);
533
534 pl_free(bs->pl);
535
536 QOBJ_UNREG(bs);
537 XFREE(MTYPE_BFDD_CONFIG, bs);
538 }
539
540 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
541 {
542 struct bfd_session *bfd, *l_bfd;
543 struct interface *ifp = NULL;
544 int psock;
545
546 /* check to see if this needs a new session */
547 l_bfd = bs_peer_find(bpc);
548 if (l_bfd) {
549 /* Requesting a duplicated peer means update configuration. */
550 if (bfd_session_update(l_bfd, bpc) == 0)
551 return l_bfd;
552 else
553 return NULL;
554 }
555
556 /*
557 * No session found, we have to allocate a new one.
558 *
559 * First a few critical checks:
560 *
561 * * Check that the specified interface exists.
562 * * Attempt to create the UDP socket (might fail if we exceed
563 * our limits).
564 */
565 if (bpc->bpc_has_localif) {
566 ifp = if_lookup_by_name(bpc->bpc_localif, VRF_DEFAULT);
567 if (ifp == NULL) {
568 log_error(
569 "session-new: specified interface doesn't exists.");
570 return NULL;
571 }
572 }
573
574 /*
575 * Get socket for transmitting control packets. Note that if we
576 * could use the destination port (3784) for the source
577 * port we wouldn't need a socket per session.
578 */
579 if (bpc->bpc_ipv4) {
580 psock = bp_peer_socket(bpc);
581 if (psock == -1)
582 return NULL;
583 } else {
584 psock = bp_peer_socketv6(bpc);
585 if (psock == -1)
586 return NULL;
587 }
588
589 /* Get memory */
590 bfd = bfd_session_new(psock);
591 if (bfd == NULL) {
592 log_error("session-new: allocation failed");
593 return NULL;
594 }
595
596 if (bpc->bpc_has_localif && !bpc->bpc_mhop)
597 bfd->ifp = ifp;
598
599 if (bpc->bpc_ipv4 == false) {
600 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
601
602 /* Set the IPv6 scope id for link-local addresses. */
603 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_local.sa_sin6.sin6_addr))
604 bpc->bpc_local.sa_sin6.sin6_scope_id =
605 bfd->ifp != NULL ? bfd->ifp->ifindex
606 : IFINDEX_INTERNAL;
607 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_peer.sa_sin6.sin6_addr))
608 bpc->bpc_peer.sa_sin6.sin6_scope_id =
609 bfd->ifp != NULL ? bfd->ifp->ifindex
610 : IFINDEX_INTERNAL;
611 }
612
613 /* Initialize the session */
614 bfd->ses_state = PTM_BFD_DOWN;
615 bfd->discrs.my_discr = ptm_bfd_gen_ID();
616 bfd->discrs.remote_discr = 0;
617 bfd->local_ip = bpc->bpc_local;
618 bfd->local_address = bpc->bpc_local;
619 bfd->timers.desired_min_tx = bfd->up_min_tx;
620 bfd->detect_TO = (bfd->detect_mult * BFD_DEF_SLOWTX);
621
622 /* Use detect_TO first for slow detection, then use recvtimer_update. */
623 bfd_recvtimer_update(bfd);
624
625 bfd_id_insert(bfd);
626
627 if (bpc->bpc_mhop) {
628 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
629 bfd->mhop.peer = bpc->bpc_peer;
630 bfd->mhop.local = bpc->bpc_local;
631 if (bpc->bpc_has_vrfname)
632 strlcpy(bfd->mhop.vrf_name, bpc->bpc_vrfname,
633 sizeof(bfd->mhop.vrf_name));
634
635 bfd_mhop_insert(bfd);
636 } else {
637 bfd->shop.peer = bpc->bpc_peer;
638 if (bpc->bpc_has_localif)
639 strlcpy(bfd->shop.port_name, bpc->bpc_localif,
640 sizeof(bfd->shop.port_name));
641
642 bfd_shop_insert(bfd);
643 }
644
645 /*
646 * XXX: session update triggers echo start, so we must have our
647 * discriminator ID set first.
648 */
649 _bfd_session_update(bfd, bpc);
650
651 /* Start transmitting with slow interval until peer responds */
652 bfd->xmt_TO = BFD_DEF_SLOWTX;
653
654 ptm_bfd_xmt_TO(bfd, 0);
655
656 log_info("session-new: %s", bs_to_string(bfd));
657
658 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
659
660 return bfd;
661 }
662
663 int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc)
664 {
665 struct bfd_session *bs;
666
667 /* Find session and call free(). */
668 bs = bs_peer_find(bpc);
669 if (bs == NULL)
670 return -1;
671
672 /* This pointer is being referenced, don't let it be deleted. */
673 if (bs->refcount > 0) {
674 log_error("session-delete: refcount failure: %" PRIu64
675 " references",
676 bs->refcount);
677 return -1;
678 }
679
680 log_info("session-delete: %s", bs_to_string(bs));
681
682 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
683
684 bfd_session_free(bs);
685
686 return 0;
687 }
688
689 void bfd_set_polling(struct bfd_session *bs)
690 {
691 bs->new_timers.desired_min_tx = bs->up_min_tx;
692 bs->new_timers.required_min_rx = bs->timers.required_min_rx;
693 bs->new_timers.required_min_echo = bs->timers.required_min_echo;
694 bs->polling = 1;
695 }
696
697 /*
698 * bs_<state>_handler() functions implement the BFD state machine
699 * transition mechanism. `<state>` is the current session state and
700 * the parameter `nstate` is the peer new state.
701 */
702 void bs_admin_down_handler(struct bfd_session *bs, int nstate);
703 void bs_down_handler(struct bfd_session *bs, int nstate);
704 void bs_init_handler(struct bfd_session *bs, int nstate);
705 void bs_up_handler(struct bfd_session *bs, int nstate);
706
707 void bs_admin_down_handler(struct bfd_session *bs __attribute__((__unused__)),
708 int nstate __attribute__((__unused__)))
709 {
710 /*
711 * We are administratively down, there is no state machine
712 * handling.
713 */
714 }
715
716 void bs_down_handler(struct bfd_session *bs, int nstate)
717 {
718 switch (nstate) {
719 case PTM_BFD_ADM_DOWN:
720 /*
721 * Remote peer doesn't want to talk, so lets keep the
722 * connection down.
723 */
724 case PTM_BFD_UP:
725 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
726 break;
727
728 case PTM_BFD_DOWN:
729 /*
730 * Remote peer agreed that the path is down, lets try to
731 * bring it up.
732 */
733 bs->ses_state = PTM_BFD_INIT;
734 break;
735
736 case PTM_BFD_INIT:
737 /*
738 * Remote peer told us his path is up, lets turn
739 * activate the session.
740 */
741 ptm_bfd_ses_up(bs);
742 break;
743
744 default:
745 log_debug("state-change: unhandled neighbor state: %d", nstate);
746 break;
747 }
748 }
749
750 void bs_init_handler(struct bfd_session *bs, int nstate)
751 {
752 switch (nstate) {
753 case PTM_BFD_ADM_DOWN:
754 /*
755 * Remote peer doesn't want to talk, so lets make the
756 * connection down.
757 */
758 bs->ses_state = PTM_BFD_DOWN;
759 break;
760
761 case PTM_BFD_DOWN:
762 /* Remote peer hasn't moved to first stage yet. */
763 break;
764
765 case PTM_BFD_INIT:
766 case PTM_BFD_UP:
767 /* We agreed on the settings and the path is up. */
768 ptm_bfd_ses_up(bs);
769 break;
770
771 default:
772 log_debug("state-change: unhandled neighbor state: %d", nstate);
773 break;
774 }
775 }
776
777 void bs_up_handler(struct bfd_session *bs, int nstate)
778 {
779 switch (nstate) {
780 case PTM_BFD_ADM_DOWN:
781 case PTM_BFD_DOWN:
782 /* Peer lost or asked to shutdown connection. */
783 ptm_bfd_ses_dn(bs, BD_NEIGHBOR_DOWN);
784 break;
785
786 case PTM_BFD_INIT:
787 case PTM_BFD_UP:
788 /* Path is up and working. */
789 break;
790
791 default:
792 log_debug("state-change: unhandled neighbor state: %d", nstate);
793 break;
794 }
795 }
796
797 void bs_state_handler(struct bfd_session *bs, int nstate)
798 {
799 switch (bs->ses_state) {
800 case PTM_BFD_ADM_DOWN:
801 bs_admin_down_handler(bs, nstate);
802 break;
803 case PTM_BFD_DOWN:
804 bs_down_handler(bs, nstate);
805 break;
806 case PTM_BFD_INIT:
807 bs_init_handler(bs, nstate);
808 break;
809 case PTM_BFD_UP:
810 bs_up_handler(bs, nstate);
811 break;
812
813 default:
814 log_debug("state-change: [%s] is in invalid state: %d",
815 bs_to_string(bs), nstate);
816 break;
817 }
818 }
819
820
821 /*
822 * Helper functions.
823 */
824 static const char *get_diag_str(int diag)
825 {
826 for (int i = 0; diag_list[i].str; i++) {
827 if (diag_list[i].type == diag)
828 return diag_list[i].str;
829 }
830 return "N/A";
831 }
832
833 const char *satostr(struct sockaddr_any *sa)
834 {
835 #define INETSTR_BUFCOUNT 8
836 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
837 static int bufidx;
838 struct sockaddr_in *sin = &sa->sa_sin;
839 struct sockaddr_in6 *sin6 = &sa->sa_sin6;
840
841 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
842 buf[bufidx][0] = 0;
843
844 switch (sin->sin_family) {
845 case AF_INET:
846 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
847 sizeof(buf[bufidx]));
848 break;
849 case AF_INET6:
850 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
851 sizeof(buf[bufidx]));
852 break;
853
854 default:
855 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
856 break;
857 }
858
859 return buf[bufidx];
860 }
861
862 const char *diag2str(uint8_t diag)
863 {
864 switch (diag) {
865 case 0:
866 return "ok";
867 case 1:
868 return "control detection time expired";
869 case 2:
870 return "echo function failed";
871 case 3:
872 return "neighbor signaled session down";
873 case 4:
874 return "forwarding plane reset";
875 case 5:
876 return "path down";
877 case 6:
878 return "concatenated path down";
879 case 7:
880 return "administratively down";
881 case 8:
882 return "reverse concatenated path down";
883 default:
884 return "unknown";
885 }
886 }
887
888 int strtosa(const char *addr, struct sockaddr_any *sa)
889 {
890 memset(sa, 0, sizeof(*sa));
891
892 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
893 sa->sa_sin.sin_family = AF_INET;
894 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
895 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
896 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
897 return 0;
898 }
899
900 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
901 sa->sa_sin6.sin6_family = AF_INET6;
902 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
903 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
904 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
905 return 0;
906 }
907
908 return -1;
909 }
910
911 void integer2timestr(uint64_t time, char *buf, size_t buflen)
912 {
913 unsigned int year, month, day, hour, minute, second;
914 int rv;
915
916 #define MINUTES (60)
917 #define HOURS (60 * MINUTES)
918 #define DAYS (24 * HOURS)
919 #define MONTHS (30 * DAYS)
920 #define YEARS (12 * MONTHS)
921 if (time >= YEARS) {
922 year = time / YEARS;
923 time -= year * YEARS;
924
925 rv = snprintf(buf, buflen, "%u year(s), ", year);
926 buf += rv;
927 buflen -= rv;
928 }
929 if (time >= MONTHS) {
930 month = time / MONTHS;
931 time -= month * MONTHS;
932
933 rv = snprintf(buf, buflen, "%u month(s), ", month);
934 buf += rv;
935 buflen -= rv;
936 }
937 if (time >= DAYS) {
938 day = time / DAYS;
939 time -= day * DAYS;
940
941 rv = snprintf(buf, buflen, "%u day(s), ", day);
942 buf += rv;
943 buflen -= rv;
944 }
945 if (time >= HOURS) {
946 hour = time / HOURS;
947 time -= hour * HOURS;
948
949 rv = snprintf(buf, buflen, "%u hour(s), ", hour);
950 buf += rv;
951 buflen -= rv;
952 }
953 if (time >= MINUTES) {
954 minute = time / MINUTES;
955 time -= minute * MINUTES;
956
957 rv = snprintf(buf, buflen, "%u minute(s), ", minute);
958 buf += rv;
959 buflen -= rv;
960 }
961 second = time % MINUTES;
962 snprintf(buf, buflen, "%u second(s)", second);
963 }
964
965 const char *bs_to_string(struct bfd_session *bs)
966 {
967 static char buf[256];
968 int pos;
969 bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
970
971 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
972 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
973 pos += snprintf(buf + pos, sizeof(buf) - pos,
974 " peer:%s local:%s", satostr(&bs->mhop.peer),
975 satostr(&bs->mhop.local));
976
977 if (bs->mhop.vrf_name[0])
978 snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
979 bs->mhop.vrf_name);
980 } else {
981 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
982 satostr(&bs->shop.peer));
983
984 if (bs->local_address.sa_sin.sin_family)
985 pos += snprintf(buf + pos, sizeof(buf) - pos,
986 " local:%s",
987 satostr(&bs->local_address));
988
989 if (bs->shop.port_name[0])
990 snprintf(buf + pos, sizeof(buf) - pos, " interface:%s",
991 bs->shop.port_name);
992 }
993
994 return buf;
995 }
996
997
998 /*
999 * BFD hash data structures to find sessions.
1000 */
1001 static struct hash *bfd_id_hash;
1002 static struct hash *bfd_shop_hash;
1003 static struct hash *bfd_mhop_hash;
1004 static struct hash *bfd_vrf_hash;
1005 static struct hash *bfd_iface_hash;
1006
1007 static unsigned int bfd_id_hash_do(void *p);
1008 static unsigned int bfd_shop_hash_do(void *p);
1009 static unsigned int bfd_mhop_hash_do(void *p);
1010 static unsigned int bfd_vrf_hash_do(void *p);
1011 static unsigned int bfd_iface_hash_do(void *p);
1012
1013 static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop);
1014 static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop);
1015 static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop);
1016 static int _iface_key(struct bfd_iface *iface, const char *ifname);
1017
1018 static void _bfd_free(struct hash_backet *hb,
1019 void *arg __attribute__((__unused__)));
1020 static void _vrf_free(void *arg);
1021 static void _iface_free(void *arg);
1022
1023 /* BFD hash for our discriminator. */
1024 static unsigned int bfd_id_hash_do(void *p)
1025 {
1026 struct bfd_session *bs = p;
1027
1028 return jhash_1word(bs->discrs.my_discr, 0);
1029 }
1030
1031 static bool bfd_id_hash_cmp(const void *n1, const void *n2)
1032 {
1033 const struct bfd_session *bs1 = n1, *bs2 = n2;
1034
1035 return bs1->discrs.my_discr == bs2->discrs.my_discr;
1036 }
1037
1038 /* BFD hash for single hop. */
1039 static unsigned int bfd_shop_hash_do(void *p)
1040 {
1041 struct bfd_session *bs = p;
1042
1043 return jhash(&bs->shop, sizeof(bs->shop), 0);
1044 }
1045
1046 static bool bfd_shop_hash_cmp(const void *n1, const void *n2)
1047 {
1048 const struct bfd_session *bs1 = n1, *bs2 = n2;
1049
1050 return memcmp(&bs1->shop, &bs2->shop, sizeof(bs1->shop)) == 0;
1051 }
1052
1053 /* BFD hash for multi hop. */
1054 static unsigned int bfd_mhop_hash_do(void *p)
1055 {
1056 struct bfd_session *bs = p;
1057
1058 return jhash(&bs->mhop, sizeof(bs->mhop), 0);
1059 }
1060
1061 static bool bfd_mhop_hash_cmp(const void *n1, const void *n2)
1062 {
1063 const struct bfd_session *bs1 = n1, *bs2 = n2;
1064
1065 return memcmp(&bs1->mhop, &bs2->mhop, sizeof(bs1->mhop)) == 0;
1066 }
1067
1068 /* BFD hash for VRFs. */
1069 static unsigned int bfd_vrf_hash_do(void *p)
1070 {
1071 struct bfd_vrf *vrf = p;
1072
1073 return jhash_1word(vrf->vrf_id, 0);
1074 }
1075
1076 static bool bfd_vrf_hash_cmp(const void *n1, const void *n2)
1077 {
1078 const struct bfd_vrf *v1 = n1, *v2 = n2;
1079
1080 return v1->vrf_id == v2->vrf_id;
1081 }
1082
1083 /* BFD hash for interfaces. */
1084 static unsigned int bfd_iface_hash_do(void *p)
1085 {
1086 struct bfd_iface *iface = p;
1087
1088 return string_hash_make(iface->ifname);
1089 }
1090
1091 static bool bfd_iface_hash_cmp(const void *n1, const void *n2)
1092 {
1093 const struct bfd_iface *i1 = n1, *i2 = n2;
1094
1095 return strcmp(i1->ifname, i2->ifname) == 0;
1096 }
1097
1098 /* Helper functions */
1099 static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop)
1100 {
1101 bs->shop = *shop;
1102
1103 /* Remove unused fields. */
1104 switch (bs->shop.peer.sa_sin.sin_family) {
1105 case AF_INET:
1106 bs->shop.peer.sa_sin.sin_port = 0;
1107 break;
1108 case AF_INET6:
1109 bs->shop.peer.sa_sin6.sin6_port = 0;
1110 break;
1111 }
1112 }
1113
1114 static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop)
1115 {
1116 _shop_key(bs, shop);
1117 memset(bs->shop.port_name, 0, sizeof(bs->shop.port_name));
1118 }
1119
1120 static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop)
1121 {
1122 bs->mhop = *mhop;
1123
1124 /* Remove unused fields. */
1125 switch (bs->mhop.peer.sa_sin.sin_family) {
1126 case AF_INET:
1127 bs->mhop.peer.sa_sin.sin_port = 0;
1128 bs->mhop.local.sa_sin.sin_port = 0;
1129 break;
1130 case AF_INET6:
1131 bs->mhop.peer.sa_sin6.sin6_port = 0;
1132 bs->mhop.local.sa_sin6.sin6_port = 0;
1133 break;
1134 }
1135 }
1136
1137 static int _iface_key(struct bfd_iface *iface, const char *ifname)
1138 {
1139 size_t slen = sizeof(iface->ifname);
1140
1141 memset(iface->ifname, 0, slen);
1142 if (strlcpy(iface->ifname, ifname, slen) >= slen)
1143 return -1;
1144
1145 return 0;
1146 }
1147
1148 /*
1149 * Hash public interface / exported functions.
1150 */
1151
1152 /* Lookup functions. */
1153 struct bfd_session *bfd_id_lookup(uint32_t id)
1154 {
1155 struct bfd_session bs;
1156
1157 bs.discrs.my_discr = id;
1158
1159 return hash_lookup(bfd_id_hash, &bs);
1160 }
1161
1162 struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop)
1163 {
1164 struct bfd_session bs, *bsp;
1165
1166 _shop_key(&bs, &shop);
1167
1168 bsp = hash_lookup(bfd_shop_hash, &bs);
1169 if (bsp == NULL && bs.shop.port_name[0] != 0) {
1170 /*
1171 * Since the local interface spec is optional, try
1172 * searching the key without it as well.
1173 */
1174 _shop_key2(&bs, &shop);
1175 bsp = hash_lookup(bfd_shop_hash, &bs);
1176 }
1177
1178 return bsp;
1179 }
1180
1181 struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop)
1182 {
1183 struct bfd_session bs;
1184
1185 _mhop_key(&bs, &mhop);
1186
1187 return hash_lookup(bfd_mhop_hash, &bs);
1188 }
1189
1190 struct bfd_vrf *bfd_vrf_lookup(int vrf_id)
1191 {
1192 struct bfd_vrf vrf;
1193
1194 vrf.vrf_id = vrf_id;
1195
1196 return hash_lookup(bfd_vrf_hash, &vrf);
1197 }
1198
1199 struct bfd_iface *bfd_iface_lookup(const char *ifname)
1200 {
1201 struct bfd_iface iface;
1202
1203 if (_iface_key(&iface, ifname) != 0)
1204 return NULL;
1205
1206 return hash_lookup(bfd_iface_hash, &iface);
1207 }
1208
1209 /*
1210 * Delete functions.
1211 *
1212 * Delete functions searches and remove the item from the hash and
1213 * returns a pointer to the removed item data. If the item was not found
1214 * then it returns NULL.
1215 *
1216 * The data stored inside the hash is not free()ed, so you must do it
1217 * manually after getting the pointer back.
1218 */
1219 struct bfd_session *bfd_id_delete(uint32_t id)
1220 {
1221 struct bfd_session bs;
1222
1223 bs.discrs.my_discr = id;
1224
1225 return hash_release(bfd_id_hash, &bs);
1226 }
1227
1228 struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop)
1229 {
1230 struct bfd_session bs, *bsp;
1231
1232 _shop_key(&bs, &shop);
1233 bsp = hash_release(bfd_shop_hash, &bs);
1234 if (bsp == NULL && shop.port_name[0] != 0) {
1235 /*
1236 * Since the local interface spec is optional, try
1237 * searching the key without it as well.
1238 */
1239 _shop_key2(&bs, &shop);
1240 bsp = hash_release(bfd_shop_hash, &bs);
1241 }
1242
1243 return bsp;
1244 }
1245
1246 struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop)
1247 {
1248 struct bfd_session bs;
1249
1250 _mhop_key(&bs, &mhop);
1251
1252 return hash_release(bfd_mhop_hash, &bs);
1253 }
1254
1255 struct bfd_vrf *bfd_vrf_delete(int vrf_id)
1256 {
1257 struct bfd_vrf vrf;
1258
1259 vrf.vrf_id = vrf_id;
1260
1261 return hash_release(bfd_vrf_hash, &vrf);
1262 }
1263
1264 struct bfd_iface *bfd_iface_delete(const char *ifname)
1265 {
1266 struct bfd_iface iface;
1267
1268 if (_iface_key(&iface, ifname) != 0)
1269 return NULL;
1270
1271 return hash_release(bfd_iface_hash, &iface);
1272 }
1273
1274 /* Iteration functions. */
1275 void bfd_id_iterate(hash_iter_func hif, void *arg)
1276 {
1277 hash_iterate(bfd_id_hash, hif, arg);
1278 }
1279
1280 void bfd_shop_iterate(hash_iter_func hif, void *arg)
1281 {
1282 hash_iterate(bfd_shop_hash, hif, arg);
1283 }
1284
1285 void bfd_mhop_iterate(hash_iter_func hif, void *arg)
1286 {
1287 hash_iterate(bfd_mhop_hash, hif, arg);
1288 }
1289
1290 void bfd_vrf_iterate(hash_iter_func hif, void *arg)
1291 {
1292 hash_iterate(bfd_vrf_hash, hif, arg);
1293 }
1294
1295 void bfd_iface_iterate(hash_iter_func hif, void *arg)
1296 {
1297 hash_iterate(bfd_iface_hash, hif, arg);
1298 }
1299
1300 /*
1301 * Insert functions.
1302 *
1303 * Inserts session into hash and returns `true` on success, otherwise
1304 * `false`.
1305 */
1306 bool bfd_id_insert(struct bfd_session *bs)
1307 {
1308 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1309 }
1310
1311 bool bfd_shop_insert(struct bfd_session *bs)
1312 {
1313 return (hash_get(bfd_shop_hash, bs, hash_alloc_intern) == bs);
1314 }
1315
1316 bool bfd_mhop_insert(struct bfd_session *bs)
1317 {
1318 return (hash_get(bfd_mhop_hash, bs, hash_alloc_intern) == bs);
1319 }
1320
1321 bool bfd_vrf_insert(struct bfd_vrf *vrf)
1322 {
1323 return (hash_get(bfd_vrf_hash, vrf, hash_alloc_intern) == vrf);
1324 }
1325
1326 bool bfd_iface_insert(struct bfd_iface *iface)
1327 {
1328 return (hash_get(bfd_iface_hash, iface, hash_alloc_intern) == iface);
1329 }
1330
1331 void bfd_initialize(void)
1332 {
1333 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1334 "BFD discriminator hash");
1335 bfd_shop_hash = hash_create(bfd_shop_hash_do, bfd_shop_hash_cmp,
1336 "BFD single hop hash");
1337 bfd_mhop_hash = hash_create(bfd_mhop_hash_do, bfd_mhop_hash_cmp,
1338 "BFD multihop hop hash");
1339 bfd_vrf_hash =
1340 hash_create(bfd_vrf_hash_do, bfd_vrf_hash_cmp, "BFD VRF hash");
1341 bfd_iface_hash = hash_create(bfd_iface_hash_do, bfd_iface_hash_cmp,
1342 "BFD interface hash");
1343 }
1344
1345 static void _bfd_free(struct hash_backet *hb,
1346 void *arg __attribute__((__unused__)))
1347 {
1348 struct bfd_session *bs = hb->data;
1349
1350 bfd_session_free(bs);
1351 }
1352
1353 static void _vrf_free(void *arg)
1354 {
1355 struct bfd_vrf *vrf = arg;
1356
1357 XFREE(MTYPE_BFDD_CONFIG, vrf);
1358 }
1359
1360 static void _iface_free(void *arg)
1361 {
1362 struct bfd_iface *iface = arg;
1363
1364 XFREE(MTYPE_BFDD_CONFIG, iface);
1365 }
1366
1367 void bfd_shutdown(void)
1368 {
1369 /*
1370 * Close and free all BFD sessions.
1371 *
1372 * _bfd_free() will call bfd_session_free() which will take care
1373 * of removing the session from all hashes, so we just run an
1374 * assert() here to make sure it really happened.
1375 */
1376 bfd_id_iterate(_bfd_free, NULL);
1377 assert(bfd_shop_hash->count == 0);
1378 assert(bfd_mhop_hash->count == 0);
1379
1380 /* Clean the VRF and interface hashes. */
1381 hash_clean(bfd_vrf_hash, _vrf_free);
1382 hash_clean(bfd_iface_hash, _iface_free);
1383
1384 /* Now free the hashes themselves. */
1385 hash_free(bfd_id_hash);
1386 hash_free(bfd_shop_hash);
1387 hash_free(bfd_mhop_hash);
1388 hash_free(bfd_vrf_hash);
1389 hash_free(bfd_iface_hash);
1390 }