]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.c
Merge pull request #2875 from opensourcerouting/fabricd
[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 ptm_bfd_echo_xmt_TO(bfd);
161
162 bfd->polling = 1;
163 bfd->new_timers.desired_min_tx = bfd->up_min_tx;
164 bfd->new_timers.required_min_rx = bfd->timers.required_min_rx;
165 ptm_bfd_snd(bfd, 0);
166 }
167
168 void ptm_bfd_ses_up(struct bfd_session *bfd)
169 {
170 int old_state = bfd->ses_state;
171
172 bfd->local_diag = 0;
173 bfd->ses_state = PTM_BFD_UP;
174 bfd->polling = 1;
175 monotime(&bfd->uptime);
176
177 /* If the peer is capable to receiving Echo pkts */
178 if (bfd->echo_xmt_TO && !BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MH)) {
179 ptm_bfd_echo_start(bfd);
180 } else {
181 bfd->new_timers.desired_min_tx = bfd->up_min_tx;
182 bfd->new_timers.required_min_rx = bfd->timers.required_min_rx;
183 ptm_bfd_snd(bfd, 0);
184 }
185
186 control_notify(bfd);
187
188 if (old_state != bfd->ses_state) {
189 bfd->stats.session_up++;
190 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd),
191 state_list[old_state].str,
192 state_list[bfd->ses_state].str);
193 }
194 }
195
196 void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag)
197 {
198 int old_state = bfd->ses_state;
199
200 bfd->local_diag = diag;
201 bfd->discrs.remote_discr = 0;
202 bfd->ses_state = PTM_BFD_DOWN;
203 bfd->polling = 0;
204 bfd->demand_mode = 0;
205 monotime(&bfd->downtime);
206
207 ptm_bfd_snd(bfd, 0);
208
209 /* only signal clients when going from up->down state */
210 if (old_state == PTM_BFD_UP)
211 control_notify(bfd);
212
213 /* Stop echo packet transmission if they are active */
214 if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
215 ptm_bfd_echo_stop(bfd, 0);
216
217 if (old_state != bfd->ses_state) {
218 bfd->stats.session_down++;
219 log_info("state-change: [%s] %s -> %s reason:%s",
220 bs_to_string(bfd), state_list[old_state].str,
221 state_list[bfd->ses_state].str,
222 get_diag_str(bfd->local_diag));
223 }
224 }
225
226 static int ptm_bfd_get_vrf_name(char *port_name, char *vrf_name)
227 {
228 struct bfd_iface *iface;
229 struct bfd_vrf *vrf;
230
231 if ((port_name == NULL) || (vrf_name == NULL))
232 return -1;
233
234 iface = bfd_iface_lookup(port_name);
235 if (iface) {
236 vrf = bfd_vrf_lookup(iface->vrf_id);
237 if (vrf) {
238 strlcpy(vrf_name, vrf->name, sizeof(vrf->name));
239 return 0;
240 }
241 }
242 return -1;
243 }
244
245 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
246 uint32_t ldisc)
247 {
248 struct bfd_session *bs;
249
250 bs = bfd_id_lookup(ldisc);
251 if (bs == NULL)
252 return NULL;
253
254 /* Remove unused fields. */
255 switch (sa->sa_sin.sin_family) {
256 case AF_INET:
257 sa->sa_sin.sin_port = 0;
258 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin)) == 0)
259 return bs;
260 break;
261 case AF_INET6:
262 sa->sa_sin6.sin6_port = 0;
263 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin6)) == 0)
264 return bs;
265 break;
266 }
267
268 return NULL;
269 }
270
271 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name,
272 struct sockaddr_any *peer,
273 struct sockaddr_any *local,
274 char *vrf_name, bool is_mhop)
275 {
276 struct bfd_session *l_bfd = NULL;
277 struct bfd_mhop_key mhop;
278 struct bfd_shop_key shop;
279 char vrf_buf[MAXNAMELEN];
280
281 /* Find our session using the ID signaled by the remote end. */
282 if (cp->discrs.remote_discr)
283 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
284
285 /* Search for session without using discriminator. */
286 if (is_mhop) {
287 memset(&mhop, 0, sizeof(mhop));
288 mhop.peer = *peer;
289 mhop.local = *local;
290 if (vrf_name && vrf_name[0]) {
291 strlcpy(mhop.vrf_name, vrf_name, sizeof(mhop.vrf_name));
292 } else if (port_name && port_name[0]) {
293 memset(vrf_buf, 0, sizeof(vrf_buf));
294 if (ptm_bfd_get_vrf_name(port_name, vrf_buf) != -1)
295 strlcpy(mhop.vrf_name, vrf_buf,
296 sizeof(mhop.vrf_name));
297 }
298
299 l_bfd = bfd_mhop_lookup(mhop);
300 } else {
301 memset(&shop, 0, sizeof(shop));
302 shop.peer = *peer;
303 if (port_name && port_name[0])
304 strlcpy(shop.port_name, port_name,
305 sizeof(shop.port_name));
306
307 l_bfd = bfd_shop_lookup(shop);
308 }
309
310 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
311 return l_bfd;
312 }
313
314 int bfd_xmt_cb(struct thread *t)
315 {
316 struct bfd_session *bs = THREAD_ARG(t);
317
318 ptm_bfd_xmt_TO(bs, 0);
319
320 return 0;
321 }
322
323 int bfd_echo_xmt_cb(struct thread *t)
324 {
325 struct bfd_session *bs = THREAD_ARG(t);
326
327 ptm_bfd_echo_xmt_TO(bs);
328
329 return 0;
330 }
331
332 /* Was ptm_bfd_detect_TO() */
333 int bfd_recvtimer_cb(struct thread *t)
334 {
335 struct bfd_session *bs = THREAD_ARG(t);
336
337 switch (bs->ses_state) {
338 case PTM_BFD_INIT:
339 case PTM_BFD_UP:
340 ptm_bfd_ses_dn(bs, BD_CONTROL_EXPIRED);
341 bfd_recvtimer_update(bs);
342 break;
343
344 default:
345 /* Second detect time expiration, zero remote discr (section
346 * 6.5.1)
347 */
348 bs->discrs.remote_discr = 0;
349 break;
350 }
351
352 return 0;
353 }
354
355 /* Was ptm_bfd_echo_detect_TO() */
356 int bfd_echo_recvtimer_cb(struct thread *t)
357 {
358 struct bfd_session *bs = THREAD_ARG(t);
359
360 switch (bs->ses_state) {
361 case PTM_BFD_INIT:
362 case PTM_BFD_UP:
363 ptm_bfd_ses_dn(bs, BD_ECHO_FAILED);
364 break;
365 }
366
367 return 0;
368 }
369
370 static struct bfd_session *bfd_session_new(int sd)
371 {
372 struct bfd_session *bs;
373
374 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
375 if (bs == NULL)
376 return NULL;
377
378 QOBJ_REG(bs, bfd_session);
379
380 bs->up_min_tx = BFD_DEFDESIREDMINTX;
381 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
382 bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO;
383 bs->detect_mult = BFD_DEFDETECTMULT;
384 bs->mh_ttl = BFD_DEF_MHOP_TTL;
385
386 bs->sock = sd;
387 monotime(&bs->uptime);
388 bs->downtime = bs->uptime;
389
390 return bs;
391 }
392
393 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
394 {
395 /* New label treatment:
396 * - Check if the label is taken;
397 * - Try to allocate the memory for it and register;
398 */
399 if (bs->pl == NULL) {
400 if (pl_find(nlabel) != NULL) {
401 /* Someone is already using it. */
402 return -1;
403 }
404
405 if (pl_new(nlabel, bs) == NULL)
406 return -1;
407
408 return 0;
409 }
410
411 /*
412 * Test label change consistency:
413 * - Do nothing if it's the same label;
414 * - Check if the future label is already taken;
415 * - Change label;
416 */
417 if (strcmp(nlabel, bs->pl->pl_label) == 0)
418 return -1;
419 if (pl_find(nlabel) != NULL)
420 return -1;
421
422 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
423 return 0;
424 }
425
426 static void _bfd_session_update(struct bfd_session *bs,
427 struct bfd_peer_cfg *bpc)
428 {
429 if (bpc->bpc_echo) {
430 /* Check if echo mode is already active. */
431 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
432 goto skip_echo;
433
434 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
435 ptm_bfd_echo_start(bs);
436
437 /* Activate/update echo receive timeout timer. */
438 bfd_echo_recvtimer_update(bs);
439 } else {
440 /* Check if echo mode is already disabled. */
441 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
442 goto skip_echo;
443
444 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
445 ptm_bfd_echo_stop(bs, 0);
446 }
447
448 skip_echo:
449 if (bpc->bpc_has_txinterval)
450 bs->up_min_tx = bpc->bpc_txinterval * 1000;
451
452 if (bpc->bpc_has_recvinterval)
453 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
454
455 if (bpc->bpc_has_detectmultiplier)
456 bs->detect_mult = bpc->bpc_detectmultiplier;
457
458 if (bpc->bpc_has_echointerval)
459 bs->timers.required_min_echo = bpc->bpc_echointerval * 1000;
460
461 if (bpc->bpc_has_label)
462 bfd_session_update_label(bs, bpc->bpc_label);
463
464 if (bpc->bpc_shutdown) {
465 /* Check if already shutdown. */
466 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
467 return;
468
469 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
470
471 /* Disable all events. */
472 bfd_recvtimer_delete(bs);
473 bfd_echo_recvtimer_delete(bs);
474 bfd_xmttimer_delete(bs);
475 bfd_echo_xmttimer_delete(bs);
476
477 /* Change and notify state change. */
478 bs->ses_state = PTM_BFD_ADM_DOWN;
479 control_notify(bs);
480
481 ptm_bfd_snd(bs, 0);
482 } else {
483 /* Check if already working. */
484 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
485 return;
486
487 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
488
489 /* Change and notify state change. */
490 bs->ses_state = PTM_BFD_DOWN;
491 control_notify(bs);
492
493 /* Enable all timers. */
494 bfd_recvtimer_update(bs);
495 bfd_xmttimer_update(bs, bs->xmt_TO);
496 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) {
497 bfd_echo_recvtimer_update(bs);
498 bfd_echo_xmttimer_update(bs, bs->echo_xmt_TO);
499 }
500 }
501 }
502
503 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
504 {
505 /* User didn't want to update, return failure. */
506 if (bpc->bpc_createonly)
507 return -1;
508
509 _bfd_session_update(bs, bpc);
510
511 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
512
513 return 0;
514 }
515
516 static void bfd_session_free(struct bfd_session *bs)
517 {
518 if (bs->sock != -1)
519 close(bs->sock);
520
521 bfd_recvtimer_delete(bs);
522 bfd_echo_recvtimer_delete(bs);
523 bfd_xmttimer_delete(bs);
524 bfd_echo_xmttimer_delete(bs);
525
526 bfd_id_delete(bs->discrs.my_discr);
527 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
528 bfd_mhop_delete(bs->mhop);
529 else
530 bfd_shop_delete(bs->shop);
531
532 pl_free(bs->pl);
533
534 QOBJ_UNREG(bs);
535 XFREE(MTYPE_BFDD_CONFIG, bs);
536 }
537
538 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
539 {
540 struct bfd_session *bfd, *l_bfd;
541 int psock;
542
543 /* check to see if this needs a new session */
544 l_bfd = bs_peer_find(bpc);
545 if (l_bfd) {
546 /* Requesting a duplicated peer means update configuration. */
547 if (bfd_session_update(l_bfd, bpc) == 0)
548 return l_bfd;
549 else
550 return NULL;
551 }
552
553 /*
554 * Get socket for transmitting control packets. Note that if we
555 * could use the destination port (3784) for the source
556 * port we wouldn't need a socket per session.
557 */
558 if (bpc->bpc_ipv4) {
559 psock = bp_peer_socket(bpc);
560 if (psock == -1)
561 return NULL;
562 } else {
563 psock = bp_peer_socketv6(bpc);
564 if (psock == -1)
565 return NULL;
566 }
567
568 /* Get memory */
569 bfd = bfd_session_new(psock);
570 if (bfd == NULL) {
571 log_error("session-new: allocation failed");
572 return NULL;
573 }
574
575 if (bpc->bpc_has_localif && !bpc->bpc_mhop) {
576 bfd->ifindex = ptm_bfd_fetch_ifindex(bpc->bpc_localif);
577 ptm_bfd_fetch_local_mac(bpc->bpc_localif, bfd->local_mac);
578 }
579
580 if (bpc->bpc_ipv4 == false) {
581 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
582
583 /* Set the IPv6 scope id for link-local addresses. */
584 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_local.sa_sin6.sin6_addr))
585 bpc->bpc_local.sa_sin6.sin6_scope_id = bfd->ifindex;
586 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_peer.sa_sin6.sin6_addr))
587 bpc->bpc_peer.sa_sin6.sin6_scope_id = bfd->ifindex;
588 }
589
590 /* Initialize the session */
591 bfd->ses_state = PTM_BFD_DOWN;
592 bfd->discrs.my_discr = ptm_bfd_gen_ID();
593 bfd->discrs.remote_discr = 0;
594 bfd->local_ip = bpc->bpc_local;
595 bfd->local_address = bpc->bpc_local;
596 bfd->timers.desired_min_tx = bfd->up_min_tx;
597 bfd->detect_TO = (bfd->detect_mult * BFD_DEF_SLOWTX);
598
599 /* Use detect_TO first for slow detection, then use recvtimer_update. */
600 bfd_recvtimer_update(bfd);
601
602 bfd_id_insert(bfd);
603
604 if (bpc->bpc_mhop) {
605 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
606 bfd->mhop.peer = bpc->bpc_peer;
607 bfd->mhop.local = bpc->bpc_local;
608 if (bpc->bpc_has_vrfname)
609 strlcpy(bfd->mhop.vrf_name, bpc->bpc_vrfname,
610 sizeof(bfd->mhop.vrf_name));
611
612 bfd_mhop_insert(bfd);
613 } else {
614 bfd->shop.peer = bpc->bpc_peer;
615 if (bpc->bpc_has_localif)
616 strlcpy(bfd->shop.port_name, bpc->bpc_localif,
617 sizeof(bfd->shop.port_name));
618
619 bfd_shop_insert(bfd);
620 }
621
622 /*
623 * XXX: session update triggers echo start, so we must have our
624 * discriminator ID set first.
625 */
626 _bfd_session_update(bfd, bpc);
627
628 /* Start transmitting with slow interval until peer responds */
629 bfd->xmt_TO = BFD_DEF_SLOWTX;
630
631 ptm_bfd_xmt_TO(bfd, 0);
632
633 log_info("session-new: %s", bs_to_string(bfd));
634
635 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
636
637 return bfd;
638 }
639
640 int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc)
641 {
642 struct bfd_session *bs;
643
644 /* Find session and call free(). */
645 bs = bs_peer_find(bpc);
646 if (bs == NULL)
647 return -1;
648
649 /* This pointer is being referenced, don't let it be deleted. */
650 if (bs->refcount > 0) {
651 log_error("session-delete: refcount failure: %" PRIu64
652 " references",
653 bs->refcount);
654 return -1;
655 }
656
657 log_info("session-delete: %s", bs_to_string(bs));
658
659 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
660
661 bfd_session_free(bs);
662
663 return 0;
664 }
665
666 void bfd_set_polling(struct bfd_session *bs)
667 {
668 bs->new_timers.desired_min_tx = bs->up_min_tx;
669 bs->new_timers.required_min_rx = bs->timers.required_min_rx;
670 bs->new_timers.required_min_echo = bs->timers.required_min_echo;
671 bs->polling = 1;
672 }
673
674
675 /*
676 * Helper functions.
677 */
678 static const char *get_diag_str(int diag)
679 {
680 for (int i = 0; diag_list[i].str; i++) {
681 if (diag_list[i].type == diag)
682 return diag_list[i].str;
683 }
684 return "N/A";
685 }
686
687 const char *satostr(struct sockaddr_any *sa)
688 {
689 #define INETSTR_BUFCOUNT 8
690 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
691 static int bufidx;
692 struct sockaddr_in *sin = &sa->sa_sin;
693 struct sockaddr_in6 *sin6 = &sa->sa_sin6;
694
695 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
696 buf[bufidx][0] = 0;
697
698 switch (sin->sin_family) {
699 case AF_INET:
700 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
701 sizeof(buf[bufidx]));
702 break;
703 case AF_INET6:
704 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
705 sizeof(buf[bufidx]));
706 break;
707
708 default:
709 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
710 break;
711 }
712
713 return buf[bufidx];
714 }
715
716 const char *diag2str(uint8_t diag)
717 {
718 switch (diag) {
719 case 0:
720 return "ok";
721 case 1:
722 return "control detection time expired";
723 case 2:
724 return "echo function failed";
725 case 3:
726 return "neighbor signaled session down";
727 case 4:
728 return "forwarding plane reset";
729 case 5:
730 return "path down";
731 case 6:
732 return "concatenated path down";
733 case 7:
734 return "administratively down";
735 case 8:
736 return "reverse concatenated path down";
737 default:
738 return "unknown";
739 }
740 }
741
742 int strtosa(const char *addr, struct sockaddr_any *sa)
743 {
744 memset(sa, 0, sizeof(*sa));
745
746 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
747 sa->sa_sin.sin_family = AF_INET;
748 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
749 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
750 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
751 return 0;
752 }
753
754 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
755 sa->sa_sin6.sin6_family = AF_INET6;
756 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
757 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
758 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
759 return 0;
760 }
761
762 return -1;
763 }
764
765 void integer2timestr(uint64_t time, char *buf, size_t buflen)
766 {
767 unsigned int year, month, day, hour, minute, second;
768 int rv;
769
770 #define MINUTES (60)
771 #define HOURS (24 * MINUTES)
772 #define DAYS (30 * HOURS)
773 #define MONTHS (12 * DAYS)
774 #define YEARS (MONTHS)
775 if (time >= YEARS) {
776 year = time / YEARS;
777 time -= year * YEARS;
778
779 rv = snprintf(buf, buflen, "%u year(s), ", year);
780 buf += rv;
781 buflen -= rv;
782 }
783 if (time >= MONTHS) {
784 month = time / MONTHS;
785 time -= month * MONTHS;
786
787 rv = snprintf(buf, buflen, "%u month(s), ", month);
788 buf += rv;
789 buflen -= rv;
790 }
791 if (time >= DAYS) {
792 day = time / DAYS;
793 time -= day * DAYS;
794
795 rv = snprintf(buf, buflen, "%u day(s), ", day);
796 buf += rv;
797 buflen -= rv;
798 }
799 if (time >= HOURS) {
800 hour = time / HOURS;
801 time -= hour * HOURS;
802
803 rv = snprintf(buf, buflen, "%u hour(s), ", hour);
804 buf += rv;
805 buflen -= rv;
806 }
807 if (time >= MINUTES) {
808 minute = time / MINUTES;
809 time -= minute * MINUTES;
810
811 rv = snprintf(buf, buflen, "%u minute(s), ", minute);
812 buf += rv;
813 buflen -= rv;
814 }
815 second = time % MINUTES;
816 snprintf(buf, buflen, "%u second(s)", second);
817 }
818
819 const char *bs_to_string(struct bfd_session *bs)
820 {
821 static char buf[256];
822 int pos;
823 bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
824
825 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
826 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
827 pos += snprintf(buf + pos, sizeof(buf) - pos,
828 " peer:%s local:%s", satostr(&bs->mhop.peer),
829 satostr(&bs->mhop.local));
830
831 if (bs->mhop.vrf_name[0])
832 snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
833 bs->mhop.vrf_name);
834 } else {
835 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
836 satostr(&bs->shop.peer));
837
838 if (bs->local_address.sa_sin.sin_family)
839 pos += snprintf(buf + pos, sizeof(buf) - pos,
840 " local:%s",
841 satostr(&bs->local_address));
842
843 if (bs->shop.port_name[0])
844 snprintf(buf + pos, sizeof(buf) - pos, " interface:%s",
845 bs->shop.port_name);
846 }
847
848 return buf;
849 }
850
851
852 /*
853 * BFD hash data structures to find sessions.
854 */
855 static struct hash *bfd_id_hash;
856 static struct hash *bfd_shop_hash;
857 static struct hash *bfd_mhop_hash;
858 static struct hash *bfd_vrf_hash;
859 static struct hash *bfd_iface_hash;
860
861 static unsigned int bfd_id_hash_do(void *p);
862 static int bfd_id_hash_cmp(const void *n1, const void *n2);
863 static unsigned int bfd_shop_hash_do(void *p);
864 static int bfd_shop_hash_cmp(const void *n1, const void *n2);
865 static unsigned int bfd_mhop_hash_do(void *p);
866 static int bfd_mhop_hash_cmp(const void *n1, const void *n2);
867 static unsigned int bfd_vrf_hash_do(void *p);
868 static int bfd_vrf_hash_cmp(const void *n1, const void *n2);
869 static unsigned int bfd_iface_hash_do(void *p);
870 static int bfd_iface_hash_cmp(const void *n1, const void *n2);
871
872 static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop);
873 static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop);
874 static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop);
875 static int _iface_key(struct bfd_iface *iface, const char *ifname);
876
877 static void _bfd_free(struct hash_backet *hb,
878 void *arg __attribute__((__unused__)));
879 static void _vrf_free(void *arg);
880 static void _iface_free(void *arg);
881
882 /* BFD hash for our discriminator. */
883 static unsigned int bfd_id_hash_do(void *p)
884 {
885 struct bfd_session *bs = p;
886
887 return jhash_1word(bs->discrs.my_discr, 0);
888 }
889
890 static int bfd_id_hash_cmp(const void *n1, const void *n2)
891 {
892 const struct bfd_session *bs1 = n1, *bs2 = n2;
893
894 return bs1->discrs.my_discr == bs2->discrs.my_discr;
895 }
896
897 /* BFD hash for single hop. */
898 static unsigned int bfd_shop_hash_do(void *p)
899 {
900 struct bfd_session *bs = p;
901
902 return jhash(&bs->shop, sizeof(bs->shop), 0);
903 }
904
905 static int bfd_shop_hash_cmp(const void *n1, const void *n2)
906 {
907 const struct bfd_session *bs1 = n1, *bs2 = n2;
908
909 return memcmp(&bs1->shop, &bs2->shop, sizeof(bs1->shop)) == 0;
910 }
911
912 /* BFD hash for multi hop. */
913 static unsigned int bfd_mhop_hash_do(void *p)
914 {
915 struct bfd_session *bs = p;
916
917 return jhash(&bs->mhop, sizeof(bs->mhop), 0);
918 }
919
920 static int bfd_mhop_hash_cmp(const void *n1, const void *n2)
921 {
922 const struct bfd_session *bs1 = n1, *bs2 = n2;
923
924 return memcmp(&bs1->mhop, &bs2->mhop, sizeof(bs1->mhop)) == 0;
925 }
926
927 /* BFD hash for VRFs. */
928 static unsigned int bfd_vrf_hash_do(void *p)
929 {
930 struct bfd_vrf *vrf = p;
931
932 return jhash_1word(vrf->vrf_id, 0);
933 }
934
935 static int bfd_vrf_hash_cmp(const void *n1, const void *n2)
936 {
937 const struct bfd_vrf *v1 = n1, *v2 = n2;
938
939 return v1->vrf_id == v2->vrf_id;
940 }
941
942 /* BFD hash for interfaces. */
943 static unsigned int bfd_iface_hash_do(void *p)
944 {
945 struct bfd_iface *iface = p;
946
947 return string_hash_make(iface->ifname);
948 }
949
950 static int bfd_iface_hash_cmp(const void *n1, const void *n2)
951 {
952 const struct bfd_iface *i1 = n1, *i2 = n2;
953
954 return strcmp(i1->ifname, i2->ifname) == 0;
955 }
956
957 /* Helper functions */
958 static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop)
959 {
960 bs->shop = *shop;
961
962 /* Remove unused fields. */
963 switch (bs->shop.peer.sa_sin.sin_family) {
964 case AF_INET:
965 bs->shop.peer.sa_sin.sin_port = 0;
966 break;
967 case AF_INET6:
968 bs->shop.peer.sa_sin6.sin6_port = 0;
969 break;
970 }
971 }
972
973 static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop)
974 {
975 _shop_key(bs, shop);
976 memset(bs->shop.port_name, 0, sizeof(bs->shop.port_name));
977 }
978
979 static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop)
980 {
981 bs->mhop = *mhop;
982
983 /* Remove unused fields. */
984 switch (bs->mhop.peer.sa_sin.sin_family) {
985 case AF_INET:
986 bs->mhop.peer.sa_sin.sin_port = 0;
987 bs->mhop.local.sa_sin.sin_port = 0;
988 break;
989 case AF_INET6:
990 bs->mhop.peer.sa_sin6.sin6_port = 0;
991 bs->mhop.local.sa_sin6.sin6_port = 0;
992 break;
993 }
994 }
995
996 static int _iface_key(struct bfd_iface *iface, const char *ifname)
997 {
998 size_t slen = sizeof(iface->ifname);
999
1000 memset(iface->ifname, 0, slen);
1001 if (strlcpy(iface->ifname, ifname, slen) >= slen)
1002 return -1;
1003
1004 return 0;
1005 }
1006
1007 /*
1008 * Hash public interface / exported functions.
1009 */
1010
1011 /* Lookup functions. */
1012 struct bfd_session *bfd_id_lookup(uint32_t id)
1013 {
1014 struct bfd_session bs;
1015
1016 bs.discrs.my_discr = id;
1017
1018 return hash_lookup(bfd_id_hash, &bs);
1019 }
1020
1021 struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop)
1022 {
1023 struct bfd_session bs, *bsp;
1024
1025 _shop_key(&bs, &shop);
1026
1027 bsp = hash_lookup(bfd_shop_hash, &bs);
1028 if (bsp == NULL && bs.shop.port_name[0] != 0) {
1029 /*
1030 * Since the local interface spec is optional, try
1031 * searching the key without it as well.
1032 */
1033 _shop_key2(&bs, &shop);
1034 bsp = hash_lookup(bfd_shop_hash, &bs);
1035 }
1036
1037 return bsp;
1038 }
1039
1040 struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop)
1041 {
1042 struct bfd_session bs;
1043
1044 _mhop_key(&bs, &mhop);
1045
1046 return hash_lookup(bfd_shop_hash, &bs);
1047 }
1048
1049 struct bfd_vrf *bfd_vrf_lookup(int vrf_id)
1050 {
1051 struct bfd_vrf vrf;
1052
1053 vrf.vrf_id = vrf_id;
1054
1055 return hash_lookup(bfd_vrf_hash, &vrf);
1056 }
1057
1058 struct bfd_iface *bfd_iface_lookup(const char *ifname)
1059 {
1060 struct bfd_iface iface;
1061
1062 if (_iface_key(&iface, ifname) != 0)
1063 return NULL;
1064
1065 return hash_lookup(bfd_iface_hash, &iface);
1066 }
1067
1068 /*
1069 * Delete functions.
1070 *
1071 * Delete functions searches and remove the item from the hash and
1072 * returns a pointer to the removed item data. If the item was not found
1073 * then it returns NULL.
1074 *
1075 * The data stored inside the hash is not free()ed, so you must do it
1076 * manually after getting the pointer back.
1077 */
1078 struct bfd_session *bfd_id_delete(uint32_t id)
1079 {
1080 struct bfd_session bs;
1081
1082 bs.discrs.my_discr = id;
1083
1084 return hash_release(bfd_id_hash, &bs);
1085 }
1086
1087 struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop)
1088 {
1089 struct bfd_session bs, *bsp;
1090
1091 _shop_key(&bs, &shop);
1092 bsp = hash_release(bfd_shop_hash, &bs);
1093 if (bsp == NULL && shop.port_name[0] != 0) {
1094 /*
1095 * Since the local interface spec is optional, try
1096 * searching the key without it as well.
1097 */
1098 _shop_key2(&bs, &shop);
1099 bsp = hash_release(bfd_shop_hash, &bs);
1100 }
1101
1102 return bsp;
1103 }
1104
1105 struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop)
1106 {
1107 struct bfd_session bs;
1108
1109 _mhop_key(&bs, &mhop);
1110
1111 return hash_release(bfd_mhop_hash, &bs);
1112 }
1113
1114 struct bfd_vrf *bfd_vrf_delete(int vrf_id)
1115 {
1116 struct bfd_vrf vrf;
1117
1118 vrf.vrf_id = vrf_id;
1119
1120 return hash_release(bfd_vrf_hash, &vrf);
1121 }
1122
1123 struct bfd_iface *bfd_iface_delete(const char *ifname)
1124 {
1125 struct bfd_iface iface;
1126
1127 if (_iface_key(&iface, ifname) != 0)
1128 return NULL;
1129
1130 return hash_release(bfd_iface_hash, &iface);
1131 }
1132
1133 /* Iteration functions. */
1134 void bfd_id_iterate(hash_iter_func hif, void *arg)
1135 {
1136 hash_iterate(bfd_id_hash, hif, arg);
1137 }
1138
1139 void bfd_shop_iterate(hash_iter_func hif, void *arg)
1140 {
1141 hash_iterate(bfd_shop_hash, hif, arg);
1142 }
1143
1144 void bfd_mhop_iterate(hash_iter_func hif, void *arg)
1145 {
1146 hash_iterate(bfd_mhop_hash, hif, arg);
1147 }
1148
1149 void bfd_vrf_iterate(hash_iter_func hif, void *arg)
1150 {
1151 hash_iterate(bfd_vrf_hash, hif, arg);
1152 }
1153
1154 void bfd_iface_iterate(hash_iter_func hif, void *arg)
1155 {
1156 hash_iterate(bfd_iface_hash, hif, arg);
1157 }
1158
1159 /*
1160 * Insert functions.
1161 *
1162 * Inserts session into hash and returns `true` on success, otherwise
1163 * `false`.
1164 */
1165 bool bfd_id_insert(struct bfd_session *bs)
1166 {
1167 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1168 }
1169
1170 bool bfd_shop_insert(struct bfd_session *bs)
1171 {
1172 return (hash_get(bfd_shop_hash, bs, hash_alloc_intern) == bs);
1173 }
1174
1175 bool bfd_mhop_insert(struct bfd_session *bs)
1176 {
1177 return (hash_get(bfd_mhop_hash, bs, hash_alloc_intern) == bs);
1178 }
1179
1180 bool bfd_vrf_insert(struct bfd_vrf *vrf)
1181 {
1182 return (hash_get(bfd_vrf_hash, vrf, hash_alloc_intern) == vrf);
1183 }
1184
1185 bool bfd_iface_insert(struct bfd_iface *iface)
1186 {
1187 return (hash_get(bfd_iface_hash, iface, hash_alloc_intern) == iface);
1188 }
1189
1190 void bfd_initialize(void)
1191 {
1192 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1193 "BFD discriminator hash");
1194 bfd_shop_hash = hash_create(bfd_shop_hash_do, bfd_shop_hash_cmp,
1195 "BFD single hop hash");
1196 bfd_mhop_hash = hash_create(bfd_mhop_hash_do, bfd_mhop_hash_cmp,
1197 "BFD multihop hop hash");
1198 bfd_vrf_hash =
1199 hash_create(bfd_vrf_hash_do, bfd_vrf_hash_cmp, "BFD VRF hash");
1200 bfd_iface_hash = hash_create(bfd_iface_hash_do, bfd_iface_hash_cmp,
1201 "BFD interface hash");
1202 }
1203
1204 static void _bfd_free(struct hash_backet *hb,
1205 void *arg __attribute__((__unused__)))
1206 {
1207 struct bfd_session *bs = hb->data;
1208
1209 bfd_session_free(bs);
1210 }
1211
1212 static void _vrf_free(void *arg)
1213 {
1214 struct bfd_vrf *vrf = arg;
1215
1216 XFREE(MTYPE_BFDD_CONFIG, vrf);
1217 }
1218
1219 static void _iface_free(void *arg)
1220 {
1221 struct bfd_iface *iface = arg;
1222
1223 XFREE(MTYPE_BFDD_CONFIG, iface);
1224 }
1225
1226 void bfd_shutdown(void)
1227 {
1228 /*
1229 * Close and free all BFD sessions.
1230 *
1231 * _bfd_free() will call bfd_session_free() which will take care
1232 * of removing the session from all hashes, so we just run an
1233 * assert() here to make sure it really happened.
1234 */
1235 bfd_id_iterate(_bfd_free, NULL);
1236 assert(bfd_shop_hash->count == 0);
1237 assert(bfd_mhop_hash->count == 0);
1238
1239 /* Clean the VRF and interface hashes. */
1240 hash_clean(bfd_vrf_hash, _vrf_free);
1241 hash_clean(bfd_iface_hash, _iface_free);
1242
1243 /* Now free the hashes themselves. */
1244 hash_free(bfd_id_hash);
1245 hash_free(bfd_shop_hash);
1246 hash_free(bfd_mhop_hash);
1247 hash_free(bfd_vrf_hash);
1248 hash_free(bfd_iface_hash);
1249 }