]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.c
Merge pull request #9939 from idryzhov/fix-ptm-build
[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 #include "lib/network.h"
32
33 #include "bfd.h"
34
35 DEFINE_MTYPE_STATIC(BFDD, BFDD_CONFIG, "long-lived configuration memory");
36 DEFINE_MTYPE_STATIC(BFDD, BFDD_PROFILE, "long-lived profile memory");
37 DEFINE_MTYPE_STATIC(BFDD, BFDD_SESSION_OBSERVER, "Session observer");
38 DEFINE_MTYPE_STATIC(BFDD, BFDD_VRF, "BFD VRF");
39
40 /*
41 * Prototypes
42 */
43 static uint32_t ptm_bfd_gen_ID(void);
44 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
45 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
46 uint32_t ldisc);
47 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
48 static const char *get_diag_str(int diag);
49
50 static void bs_admin_down_handler(struct bfd_session *bs, int nstate);
51 static void bs_down_handler(struct bfd_session *bs, int nstate);
52 static void bs_init_handler(struct bfd_session *bs, int nstate);
53 static void bs_up_handler(struct bfd_session *bs, int nstate);
54 static void bs_neighbour_admin_down_handler(struct bfd_session *bfd,
55 uint8_t diag);
56
57 /**
58 * Remove BFD profile from all BFD sessions so we don't leave dangling
59 * pointers.
60 */
61 static void bfd_profile_detach(struct bfd_profile *bp);
62
63 /* Zeroed array with the size of an IPv6 address. */
64 struct in6_addr zero_addr;
65
66 /** BFD profiles list. */
67 struct bfdproflist bplist;
68
69 /*
70 * Functions
71 */
72 struct bfd_profile *bfd_profile_lookup(const char *name)
73 {
74 struct bfd_profile *bp;
75
76 TAILQ_FOREACH (bp, &bplist, entry) {
77 if (strcmp(name, bp->name))
78 continue;
79
80 return bp;
81 }
82
83 return NULL;
84 }
85
86 static void bfd_profile_set_default(struct bfd_profile *bp)
87 {
88 bp->admin_shutdown = false;
89 bp->detection_multiplier = BFD_DEFDETECTMULT;
90 bp->echo_mode = false;
91 bp->passive = false;
92 bp->minimum_ttl = BFD_DEF_MHOP_TTL;
93 bp->min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
94 bp->min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
95 bp->min_rx = BFD_DEFREQUIREDMINRX;
96 bp->min_tx = BFD_DEFDESIREDMINTX;
97 }
98
99 struct bfd_profile *bfd_profile_new(const char *name)
100 {
101 struct bfd_profile *bp;
102
103 /* Search for duplicates. */
104 if (bfd_profile_lookup(name) != NULL)
105 return NULL;
106
107 /* Allocate, name it and put into list. */
108 bp = XCALLOC(MTYPE_BFDD_PROFILE, sizeof(*bp));
109 strlcpy(bp->name, name, sizeof(bp->name));
110 TAILQ_INSERT_TAIL(&bplist, bp, entry);
111
112 /* Set default values. */
113 bfd_profile_set_default(bp);
114
115 return bp;
116 }
117
118 void bfd_profile_free(struct bfd_profile *bp)
119 {
120 /* Detach from any session. */
121 if (bglobal.bg_shutdown == false)
122 bfd_profile_detach(bp);
123
124 /* Remove from global list. */
125 TAILQ_REMOVE(&bplist, bp, entry);
126
127 XFREE(MTYPE_BFDD_PROFILE, bp);
128 }
129
130 void bfd_profile_apply(const char *profname, struct bfd_session *bs)
131 {
132 struct bfd_profile *bp;
133
134 /* Remove previous profile if any. */
135 if (bs->profile_name) {
136 /* We are changing profiles. */
137 if (strcmp(bs->profile_name, profname)) {
138 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
139 bs->profile_name =
140 XSTRDUP(MTYPE_BFDD_PROFILE, profname);
141 }
142 } else /* Save the current profile name (in case it doesn't exist). */
143 bs->profile_name = XSTRDUP(MTYPE_BFDD_PROFILE, profname);
144
145 /* Look up new profile to apply. */
146 bp = bfd_profile_lookup(profname);
147
148 /* Point to profile if it exists. */
149 bs->profile = bp;
150
151 /* Apply configuration. */
152 bfd_session_apply(bs);
153 }
154
155 void bfd_session_apply(struct bfd_session *bs)
156 {
157 struct bfd_profile *bp;
158 uint32_t min_tx = bs->timers.desired_min_tx;
159 uint32_t min_rx = bs->timers.required_min_rx;
160
161 /* Pick the source of configuration. */
162 bp = bs->profile ? bs->profile : &bs->peer_profile;
163
164 /* Set multiplier if not the default. */
165 if (bs->peer_profile.detection_multiplier == BFD_DEFDETECTMULT)
166 bs->detect_mult = bp->detection_multiplier;
167 else
168 bs->detect_mult = bs->peer_profile.detection_multiplier;
169
170 /* Set timers if not the default. */
171 if (bs->peer_profile.min_tx == BFD_DEFDESIREDMINTX)
172 bs->timers.desired_min_tx = bp->min_tx;
173 else
174 bs->timers.desired_min_tx = bs->peer_profile.min_tx;
175
176 if (bs->peer_profile.min_rx == BFD_DEFREQUIREDMINRX)
177 bs->timers.required_min_rx = bp->min_rx;
178 else
179 bs->timers.required_min_rx = bs->peer_profile.min_rx;
180
181 /* We can only apply echo options on single hop sessions. */
182 if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
183 /* Configure echo timers if they were default. */
184 if (bs->peer_profile.min_echo_rx == BFD_DEF_REQ_MIN_ECHO_RX)
185 bs->timers.required_min_echo_rx = bp->min_echo_rx;
186 else
187 bs->timers.required_min_echo_rx =
188 bs->peer_profile.min_echo_rx;
189
190 if (bs->peer_profile.min_echo_tx == BFD_DEF_DES_MIN_ECHO_TX)
191 bs->timers.desired_min_echo_tx = bp->min_echo_tx;
192 else
193 bs->timers.desired_min_echo_tx =
194 bs->peer_profile.min_echo_tx;
195
196 /* Toggle echo if default value. */
197 if (bs->peer_profile.echo_mode == false)
198 bfd_set_echo(bs, bp->echo_mode);
199 else
200 bfd_set_echo(bs, bs->peer_profile.echo_mode);
201 } else {
202 /* Configure the TTL packet filter. */
203 if (bs->peer_profile.minimum_ttl == BFD_DEF_MHOP_TTL)
204 bs->mh_ttl = bp->minimum_ttl;
205 else
206 bs->mh_ttl = bs->peer_profile.minimum_ttl;
207 }
208
209 /* Toggle 'passive-mode' if default value. */
210 if (bs->peer_profile.passive == false)
211 bfd_set_passive_mode(bs, bp->passive);
212 else
213 bfd_set_passive_mode(bs, bs->peer_profile.passive);
214
215 /* Toggle 'no shutdown' if default value. */
216 if (bs->peer_profile.admin_shutdown == false)
217 bfd_set_shutdown(bs, bp->admin_shutdown);
218 else
219 bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown);
220
221 /* If session interval changed negotiate new timers. */
222 if (bs->ses_state == PTM_BFD_UP
223 && (bs->timers.desired_min_tx != min_tx
224 || bs->timers.required_min_rx != min_rx))
225 bfd_set_polling(bs);
226
227 /* Send updated information to data plane. */
228 bfd_dplane_update_session(bs);
229 }
230
231 void bfd_profile_remove(struct bfd_session *bs)
232 {
233 /* Remove any previous set profile name. */
234 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
235 bs->profile = NULL;
236
237 bfd_session_apply(bs);
238 }
239
240 void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
241 struct sockaddr_any *local, bool mhop, const char *ifname,
242 const char *vrfname)
243 {
244 memset(key, 0, sizeof(*key));
245
246 switch (peer->sa_sin.sin_family) {
247 case AF_INET:
248 key->family = AF_INET;
249 memcpy(&key->peer, &peer->sa_sin.sin_addr,
250 sizeof(peer->sa_sin.sin_addr));
251 memcpy(&key->local, &local->sa_sin.sin_addr,
252 sizeof(local->sa_sin.sin_addr));
253 break;
254 case AF_INET6:
255 key->family = AF_INET6;
256 memcpy(&key->peer, &peer->sa_sin6.sin6_addr,
257 sizeof(peer->sa_sin6.sin6_addr));
258 memcpy(&key->local, &local->sa_sin6.sin6_addr,
259 sizeof(local->sa_sin6.sin6_addr));
260 break;
261 }
262
263 key->mhop = mhop;
264 if (ifname && ifname[0])
265 strlcpy(key->ifname, ifname, sizeof(key->ifname));
266 if (vrfname && vrfname[0])
267 strlcpy(key->vrfname, vrfname, sizeof(key->vrfname));
268 else
269 strlcpy(key->vrfname, VRF_DEFAULT_NAME, sizeof(key->vrfname));
270 }
271
272 struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
273 {
274 struct bfd_session *bs;
275 struct peer_label *pl;
276 struct bfd_key key;
277
278 /* Try to find label first. */
279 if (bpc->bpc_has_label) {
280 pl = pl_find(bpc->bpc_label);
281 if (pl != NULL) {
282 bs = pl->pl_bs;
283 return bs;
284 }
285 }
286
287 /* Otherwise fallback to peer/local hash lookup. */
288 gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop,
289 bpc->bpc_localif, bpc->bpc_vrfname);
290
291 return bfd_key_lookup(key);
292 }
293
294 /*
295 * Starts a disabled BFD session.
296 *
297 * A session is disabled when the specified interface/VRF doesn't exist
298 * yet. It might happen on FRR boot or with virtual interfaces.
299 */
300 int bfd_session_enable(struct bfd_session *bs)
301 {
302 struct interface *ifp = NULL;
303 struct vrf *vrf = NULL;
304 int psock;
305
306 /* We are using data plane, we don't need software. */
307 if (bs->bdc)
308 return 0;
309
310 /*
311 * If the interface or VRF doesn't exist, then we must register
312 * the session but delay its start.
313 */
314 if (bs->key.vrfname[0]) {
315 vrf = vrf_lookup_by_name(bs->key.vrfname);
316 if (vrf == NULL) {
317 zlog_err(
318 "session-enable: specified VRF %s doesn't exists.",
319 bs->key.vrfname);
320 return 0;
321 }
322 } else {
323 vrf = vrf_lookup_by_id(VRF_DEFAULT);
324 }
325
326 assert(vrf);
327
328 if (bs->key.ifname[0]) {
329 ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id);
330 if (ifp == NULL) {
331 zlog_err(
332 "session-enable: specified interface %s (VRF %s) doesn't exist.",
333 bs->key.ifname, vrf->name);
334 return 0;
335 }
336 }
337
338 /* Assign interface/VRF pointers. */
339 bs->vrf = vrf;
340
341 /* Assign interface pointer (if any). */
342 bs->ifp = ifp;
343
344 /* Attempt to use data plane. */
345 if (bglobal.bg_use_dplane && bfd_dplane_add_session(bs) == 0) {
346 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bs);
347 return 0;
348 }
349
350 /* Sanity check: don't leak open sockets. */
351 if (bs->sock != -1) {
352 if (bglobal.debug_peer_event)
353 zlog_debug("session-enable: previous socket open");
354
355 close(bs->sock);
356 bs->sock = -1;
357 }
358
359 /*
360 * Get socket for transmitting control packets. Note that if we
361 * could use the destination port (3784) for the source
362 * port we wouldn't need a socket per session.
363 */
364 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) {
365 psock = bp_peer_socket(bs);
366 if (psock == -1)
367 return 0;
368 } else {
369 psock = bp_peer_socketv6(bs);
370 if (psock == -1)
371 return 0;
372 }
373
374 /*
375 * We've got a valid socket, lets start the timers and the
376 * protocol.
377 */
378 bs->sock = psock;
379
380 /* Only start timers if we are using active mode. */
381 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE) == 0) {
382 bfd_recvtimer_update(bs);
383 ptm_bfd_start_xmt_timer(bs, false);
384 }
385
386 return 0;
387 }
388
389 /*
390 * Disabled a running BFD session.
391 *
392 * A session is disabled when the specified interface/VRF gets removed
393 * (e.g. virtual interfaces).
394 */
395 void bfd_session_disable(struct bfd_session *bs)
396 {
397 /* We are using data plane, we don't need software. */
398 if (bs->bdc)
399 return;
400
401 /* Free up socket resources. */
402 if (bs->sock != -1) {
403 close(bs->sock);
404 bs->sock = -1;
405 }
406
407 /* Disable all timers. */
408 bfd_recvtimer_delete(bs);
409 bfd_xmttimer_delete(bs);
410 ptm_bfd_echo_stop(bs);
411
412 /* Set session down so it doesn't report UP and disabled. */
413 ptm_bfd_sess_dn(bs, BD_PATH_DOWN);
414 }
415
416 static uint32_t ptm_bfd_gen_ID(void)
417 {
418 uint32_t session_id;
419
420 /*
421 * RFC 5880, Section 6.8.1. recommends that we should generate
422 * random session identification numbers.
423 */
424 do {
425 session_id = ((frr_weak_random() << 16) & 0xFFFF0000)
426 | (frr_weak_random() & 0x0000FFFF);
427 } while (session_id == 0 || bfd_id_lookup(session_id) != NULL);
428
429 return session_id;
430 }
431
432 void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
433 {
434 uint64_t jitter, xmt_TO;
435 int maxpercent;
436
437 xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
438
439 /*
440 * From section 6.5.2: trasmit interval should be randomly jittered
441 * between
442 * 75% and 100% of nominal value, unless detect_mult is 1, then should
443 * be
444 * between 75% and 90%.
445 */
446 maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
447 jitter = (xmt_TO * (75 + (frr_weak_random() % maxpercent))) / 100;
448 /* XXX remove that division above */
449
450 if (is_echo)
451 bfd_echo_xmttimer_update(bfd, jitter);
452 else
453 bfd_xmttimer_update(bfd, jitter);
454 }
455
456 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
457 {
458 /* Send the scheduled echo packet */
459 ptm_bfd_echo_snd(bfd);
460
461 /* Restart the timer for next time */
462 ptm_bfd_start_xmt_timer(bfd, true);
463 }
464
465 void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
466 {
467 /* Send the scheduled control packet */
468 ptm_bfd_snd(bfd, fbit);
469
470 /* Restart the timer for next time */
471 ptm_bfd_start_xmt_timer(bfd, false);
472 }
473
474 void ptm_bfd_echo_stop(struct bfd_session *bfd)
475 {
476 bfd->echo_xmt_TO = 0;
477 bfd->echo_detect_TO = 0;
478 UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
479
480 bfd_echo_xmttimer_delete(bfd);
481 bfd_echo_recvtimer_delete(bfd);
482 }
483
484 void ptm_bfd_echo_start(struct bfd_session *bfd)
485 {
486 bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
487 if (bfd->echo_detect_TO > 0) {
488 bfd_echo_recvtimer_update(bfd);
489 ptm_bfd_echo_xmt_TO(bfd);
490 }
491 }
492
493 void ptm_bfd_sess_up(struct bfd_session *bfd)
494 {
495 int old_state = bfd->ses_state;
496
497 bfd->local_diag = 0;
498 bfd->ses_state = PTM_BFD_UP;
499 monotime(&bfd->uptime);
500
501 /* Connection is up, lets negotiate timers. */
502 bfd_set_polling(bfd);
503
504 /* Start sending control packets with poll bit immediately. */
505 ptm_bfd_snd(bfd, 0);
506
507 control_notify(bfd, bfd->ses_state);
508
509 if (old_state != bfd->ses_state) {
510 bfd->stats.session_up++;
511 if (bglobal.debug_peer_event)
512 zlog_debug("state-change: [%s] %s -> %s",
513 bs_to_string(bfd), state_list[old_state].str,
514 state_list[bfd->ses_state].str);
515 }
516 }
517
518 void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag)
519 {
520 int old_state = bfd->ses_state;
521
522 bfd->local_diag = diag;
523 bfd->discrs.remote_discr = 0;
524 bfd->ses_state = PTM_BFD_DOWN;
525 bfd->polling = 0;
526 bfd->demand_mode = 0;
527 monotime(&bfd->downtime);
528
529 /*
530 * Only attempt to send if we have a valid socket:
531 * this function might be called by session disablers and in
532 * this case we won't have a valid socket (i.e. interface was
533 * removed or VRF doesn't exist anymore).
534 */
535 if (bfd->sock != -1)
536 ptm_bfd_snd(bfd, 0);
537
538 /* Slow down the control packets, the connection is down. */
539 bs_set_slow_timers(bfd);
540
541 /* only signal clients when going from up->down state */
542 if (old_state == PTM_BFD_UP)
543 control_notify(bfd, PTM_BFD_DOWN);
544
545 /* Stop echo packet transmission if they are active */
546 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
547 ptm_bfd_echo_stop(bfd);
548
549 /* Stop attempting to transmit or expect control packets if passive. */
550 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_PASSIVE)) {
551 bfd_recvtimer_delete(bfd);
552 bfd_xmttimer_delete(bfd);
553 }
554
555 if (old_state != bfd->ses_state) {
556 bfd->stats.session_down++;
557 if (bglobal.debug_peer_event)
558 zlog_debug("state-change: [%s] %s -> %s reason:%s",
559 bs_to_string(bfd), state_list[old_state].str,
560 state_list[bfd->ses_state].str,
561 get_diag_str(bfd->local_diag));
562 }
563 }
564
565 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
566 uint32_t ldisc)
567 {
568 struct bfd_session *bs;
569
570 bs = bfd_id_lookup(ldisc);
571 if (bs == NULL)
572 return NULL;
573
574 switch (bs->key.family) {
575 case AF_INET:
576 if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer,
577 sizeof(sa->sa_sin.sin_addr)))
578 return NULL;
579 break;
580 case AF_INET6:
581 if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer,
582 sizeof(sa->sa_sin6.sin6_addr)))
583 return NULL;
584 break;
585 }
586
587 return bs;
588 }
589
590 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
591 struct sockaddr_any *peer,
592 struct sockaddr_any *local,
593 struct interface *ifp,
594 vrf_id_t vrfid,
595 bool is_mhop)
596 {
597 struct vrf *vrf;
598 struct bfd_key key;
599
600 /* Find our session using the ID signaled by the remote end. */
601 if (cp->discrs.remote_discr)
602 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
603
604 /* Search for session without using discriminator. */
605 vrf = vrf_lookup_by_id(vrfid);
606
607 gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL,
608 vrf ? vrf->name : VRF_DEFAULT_NAME);
609
610 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
611 return bfd_key_lookup(key);
612 }
613
614 int bfd_xmt_cb(struct thread *t)
615 {
616 struct bfd_session *bs = THREAD_ARG(t);
617
618 ptm_bfd_xmt_TO(bs, 0);
619
620 return 0;
621 }
622
623 int bfd_echo_xmt_cb(struct thread *t)
624 {
625 struct bfd_session *bs = THREAD_ARG(t);
626
627 if (bs->echo_xmt_TO > 0)
628 ptm_bfd_echo_xmt_TO(bs);
629
630 return 0;
631 }
632
633 /* Was ptm_bfd_detect_TO() */
634 int bfd_recvtimer_cb(struct thread *t)
635 {
636 struct bfd_session *bs = THREAD_ARG(t);
637
638 switch (bs->ses_state) {
639 case PTM_BFD_INIT:
640 case PTM_BFD_UP:
641 ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED);
642 bfd_recvtimer_update(bs);
643 break;
644
645 default:
646 /* Second detect time expiration, zero remote discr (section
647 * 6.5.1)
648 */
649 bs->discrs.remote_discr = 0;
650 break;
651 }
652
653 return 0;
654 }
655
656 /* Was ptm_bfd_echo_detect_TO() */
657 int bfd_echo_recvtimer_cb(struct thread *t)
658 {
659 struct bfd_session *bs = THREAD_ARG(t);
660
661 switch (bs->ses_state) {
662 case PTM_BFD_INIT:
663 case PTM_BFD_UP:
664 ptm_bfd_sess_dn(bs, BD_ECHO_FAILED);
665 break;
666 }
667
668 return 0;
669 }
670
671 struct bfd_session *bfd_session_new(void)
672 {
673 struct bfd_session *bs;
674
675 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
676
677 /* Set peer session defaults. */
678 bfd_profile_set_default(&bs->peer_profile);
679
680 bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX;
681 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
682 bs->timers.required_min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
683 bs->timers.desired_min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
684 bs->detect_mult = BFD_DEFDETECTMULT;
685 bs->mh_ttl = BFD_DEF_MHOP_TTL;
686 bs->ses_state = PTM_BFD_DOWN;
687
688 /* Initiate connection with slow timers. */
689 bs_set_slow_timers(bs);
690
691 /* Initiate remote settings as well. */
692 bs->remote_timers = bs->cur_timers;
693 bs->remote_detect_mult = BFD_DEFDETECTMULT;
694
695 bs->sock = -1;
696 monotime(&bs->uptime);
697 bs->downtime = bs->uptime;
698
699 return bs;
700 }
701
702 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
703 {
704 /* New label treatment:
705 * - Check if the label is taken;
706 * - Try to allocate the memory for it and register;
707 */
708 if (bs->pl == NULL) {
709 if (pl_find(nlabel) != NULL) {
710 /* Someone is already using it. */
711 return -1;
712 }
713
714 pl_new(nlabel, bs);
715
716 return 0;
717 }
718
719 /*
720 * Test label change consistency:
721 * - Do nothing if it's the same label;
722 * - Check if the future label is already taken;
723 * - Change label;
724 */
725 if (strcmp(nlabel, bs->pl->pl_label) == 0)
726 return -1;
727 if (pl_find(nlabel) != NULL)
728 return -1;
729
730 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
731 return 0;
732 }
733
734 static void _bfd_session_update(struct bfd_session *bs,
735 struct bfd_peer_cfg *bpc)
736 {
737 if (bpc->bpc_has_txinterval) {
738 bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000;
739 bs->peer_profile.min_tx = bs->timers.desired_min_tx;
740 }
741
742 if (bpc->bpc_has_recvinterval) {
743 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
744 bs->peer_profile.min_rx = bs->timers.required_min_rx;
745 }
746
747 if (bpc->bpc_has_detectmultiplier) {
748 bs->detect_mult = bpc->bpc_detectmultiplier;
749 bs->peer_profile.detection_multiplier = bs->detect_mult;
750 }
751
752 if (bpc->bpc_has_echorecvinterval) {
753 bs->timers.required_min_echo_rx = bpc->bpc_echorecvinterval * 1000;
754 bs->peer_profile.min_echo_rx = bs->timers.required_min_echo_rx;
755 }
756
757 if (bpc->bpc_has_echotxinterval) {
758 bs->timers.desired_min_echo_tx = bpc->bpc_echotxinterval * 1000;
759 bs->peer_profile.min_echo_tx = bs->timers.desired_min_echo_tx;
760 }
761
762 if (bpc->bpc_has_label)
763 bfd_session_update_label(bs, bpc->bpc_label);
764
765 if (bpc->bpc_cbit)
766 SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
767 else
768 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
769
770 if (bpc->bpc_has_minimum_ttl) {
771 bs->mh_ttl = bpc->bpc_minimum_ttl;
772 bs->peer_profile.minimum_ttl = bpc->bpc_minimum_ttl;
773 }
774
775 bs->peer_profile.echo_mode = bpc->bpc_echo;
776 bfd_set_echo(bs, bpc->bpc_echo);
777
778 /*
779 * Shutdown needs to be the last in order to avoid timers enable when
780 * the session is disabled.
781 */
782 bs->peer_profile.admin_shutdown = bpc->bpc_shutdown;
783 bfd_set_passive_mode(bs, bpc->bpc_passive);
784 bfd_set_shutdown(bs, bpc->bpc_shutdown);
785
786 /*
787 * Apply profile last: it also calls `bfd_set_shutdown`.
788 *
789 * There is no problem calling `shutdown` twice if the value doesn't
790 * change or if it is overriden by peer specific configuration.
791 */
792 if (bpc->bpc_has_profile)
793 bfd_profile_apply(bpc->bpc_profile, bs);
794 }
795
796 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
797 {
798 /* User didn't want to update, return failure. */
799 if (bpc->bpc_createonly)
800 return -1;
801
802 _bfd_session_update(bs, bpc);
803
804 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
805
806 return 0;
807 }
808
809 void bfd_session_free(struct bfd_session *bs)
810 {
811 struct bfd_session_observer *bso;
812
813 bfd_session_disable(bs);
814
815 /* Remove session from data plane if any. */
816 bfd_dplane_delete_session(bs);
817
818 bfd_key_delete(bs->key);
819 bfd_id_delete(bs->discrs.my_discr);
820
821 /* Remove observer if any. */
822 TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) {
823 if (bso->bso_bs != bs)
824 continue;
825
826 break;
827 }
828 if (bso != NULL)
829 bs_observer_del(bso);
830
831 pl_free(bs->pl);
832
833 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
834 XFREE(MTYPE_BFDD_CONFIG, bs);
835 }
836
837 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
838 {
839 struct bfd_session *bfd, *l_bfd;
840
841 /* check to see if this needs a new session */
842 l_bfd = bs_peer_find(bpc);
843 if (l_bfd) {
844 /* Requesting a duplicated peer means update configuration. */
845 if (bfd_session_update(l_bfd, bpc) == 0)
846 return l_bfd;
847 else
848 return NULL;
849 }
850
851 /* Get BFD session storage with its defaults. */
852 bfd = bfd_session_new();
853
854 /*
855 * Store interface/VRF name in case we need to delay session
856 * start. See `bfd_session_enable` for more information.
857 */
858 if (bpc->bpc_has_localif)
859 strlcpy(bfd->key.ifname, bpc->bpc_localif,
860 sizeof(bfd->key.ifname));
861
862 if (bpc->bpc_has_vrfname)
863 strlcpy(bfd->key.vrfname, bpc->bpc_vrfname,
864 sizeof(bfd->key.vrfname));
865 else
866 strlcpy(bfd->key.vrfname, VRF_DEFAULT_NAME,
867 sizeof(bfd->key.vrfname));
868
869 /* Copy remaining data. */
870 if (bpc->bpc_ipv4 == false)
871 SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
872
873 bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6;
874 switch (bfd->key.family) {
875 case AF_INET:
876 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr,
877 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
878 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr,
879 sizeof(bpc->bpc_local.sa_sin.sin_addr));
880 break;
881
882 case AF_INET6:
883 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr,
884 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
885 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr,
886 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
887 break;
888
889 default:
890 assert(1);
891 break;
892 }
893
894 if (bpc->bpc_mhop)
895 SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
896
897 bfd->key.mhop = bpc->bpc_mhop;
898
899 if (bs_registrate(bfd) == NULL)
900 return NULL;
901
902 /* Apply other configurations. */
903 _bfd_session_update(bfd, bpc);
904
905 return bfd;
906 }
907
908 struct bfd_session *bs_registrate(struct bfd_session *bfd)
909 {
910 /* Registrate session into data structures. */
911 bfd_key_insert(bfd);
912 bfd->discrs.my_discr = ptm_bfd_gen_ID();
913 bfd_id_insert(bfd);
914
915 /* Try to enable session and schedule for packet receive/send. */
916 if (bfd_session_enable(bfd) == -1) {
917 /* Unrecoverable failure, remove the session/peer. */
918 bfd_session_free(bfd);
919 return NULL;
920 }
921
922 /* Add observer if we have moving parts. */
923 if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1)
924 bs_observer_add(bfd);
925
926 if (bglobal.debug_peer_event)
927 zlog_debug("session-new: %s", bs_to_string(bfd));
928
929 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
930
931 return bfd;
932 }
933
934 int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc)
935 {
936 struct bfd_session *bs;
937
938 /* Find session and call free(). */
939 bs = bs_peer_find(bpc);
940 if (bs == NULL)
941 return -1;
942
943 /* This pointer is being referenced, don't let it be deleted. */
944 if (bs->refcount > 0) {
945 zlog_err("session-delete: refcount failure: %" PRIu64" references",
946 bs->refcount);
947 return -1;
948 }
949
950 if (bglobal.debug_peer_event)
951 zlog_debug("session-delete: %s", bs_to_string(bs));
952
953 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
954
955 bfd_session_free(bs);
956
957 return 0;
958 }
959
960 void bfd_set_polling(struct bfd_session *bs)
961 {
962 /*
963 * Start polling procedure: the only timers that require polling
964 * to change value without losing connection are:
965 *
966 * - Desired minimum transmission interval;
967 * - Required minimum receive interval;
968 *
969 * RFC 5880, Section 6.8.3.
970 */
971 bs->polling = 1;
972 }
973
974 /*
975 * bs_<state>_handler() functions implement the BFD state machine
976 * transition mechanism. `<state>` is the current session state and
977 * the parameter `nstate` is the peer new state.
978 */
979 static void bs_admin_down_handler(struct bfd_session *bs
980 __attribute__((__unused__)),
981 int nstate __attribute__((__unused__)))
982 {
983 /*
984 * We are administratively down, there is no state machine
985 * handling.
986 */
987 }
988
989 static void bs_down_handler(struct bfd_session *bs, int nstate)
990 {
991 switch (nstate) {
992 case PTM_BFD_ADM_DOWN:
993 /*
994 * Remote peer doesn't want to talk, so lets keep the
995 * connection down.
996 */
997 case PTM_BFD_UP:
998 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
999 break;
1000
1001 case PTM_BFD_DOWN:
1002 /*
1003 * Remote peer agreed that the path is down, lets try to
1004 * bring it up.
1005 */
1006 bs->ses_state = PTM_BFD_INIT;
1007
1008 /* Answer peer with INIT immediately in passive mode. */
1009 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE))
1010 ptm_bfd_snd(bs, 0);
1011 break;
1012
1013 case PTM_BFD_INIT:
1014 /*
1015 * Remote peer told us his path is up, lets turn
1016 * activate the session.
1017 */
1018 ptm_bfd_sess_up(bs);
1019 break;
1020
1021 default:
1022 if (bglobal.debug_peer_event)
1023 zlog_debug("state-change: unhandled neighbor state: %d",
1024 nstate);
1025 break;
1026 }
1027 }
1028
1029 static void bs_init_handler(struct bfd_session *bs, int nstate)
1030 {
1031 switch (nstate) {
1032 case PTM_BFD_ADM_DOWN:
1033 /*
1034 * Remote peer doesn't want to talk, so lets make the
1035 * connection down.
1036 */
1037 bs->ses_state = PTM_BFD_DOWN;
1038 break;
1039
1040 case PTM_BFD_DOWN:
1041 /* Remote peer hasn't moved to first stage yet. */
1042 break;
1043
1044 case PTM_BFD_INIT:
1045 case PTM_BFD_UP:
1046 /* We agreed on the settings and the path is up. */
1047 ptm_bfd_sess_up(bs);
1048 break;
1049
1050 default:
1051 if (bglobal.debug_peer_event)
1052 zlog_debug("state-change: unhandled neighbor state: %d",
1053 nstate);
1054 break;
1055 }
1056 }
1057
1058 static void bs_neighbour_admin_down_handler(struct bfd_session *bfd,
1059 uint8_t diag)
1060 {
1061 int old_state = bfd->ses_state;
1062
1063 bfd->local_diag = diag;
1064 bfd->discrs.remote_discr = 0;
1065 bfd->ses_state = PTM_BFD_DOWN;
1066 bfd->polling = 0;
1067 bfd->demand_mode = 0;
1068 monotime(&bfd->downtime);
1069
1070 /* Slow down the control packets, the connection is down. */
1071 bs_set_slow_timers(bfd);
1072
1073 /* only signal clients when going from up->down state */
1074 if (old_state == PTM_BFD_UP)
1075 control_notify(bfd, PTM_BFD_ADM_DOWN);
1076
1077 /* Stop echo packet transmission if they are active */
1078 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
1079 ptm_bfd_echo_stop(bfd);
1080
1081 if (old_state != bfd->ses_state) {
1082 bfd->stats.session_down++;
1083 if (bglobal.debug_peer_event)
1084 zlog_debug("state-change: [%s] %s -> %s reason:%s",
1085 bs_to_string(bfd), state_list[old_state].str,
1086 state_list[bfd->ses_state].str,
1087 get_diag_str(bfd->local_diag));
1088 }
1089 }
1090
1091 static void bs_up_handler(struct bfd_session *bs, int nstate)
1092 {
1093 switch (nstate) {
1094 case PTM_BFD_ADM_DOWN:
1095 bs_neighbour_admin_down_handler(bs, BD_ADMIN_DOWN);
1096 break;
1097
1098 case PTM_BFD_DOWN:
1099 /* Peer lost or asked to shutdown connection. */
1100 ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN);
1101 break;
1102
1103 case PTM_BFD_INIT:
1104 case PTM_BFD_UP:
1105 /* Path is up and working. */
1106 break;
1107
1108 default:
1109 if (bglobal.debug_peer_event)
1110 zlog_debug("state-change: unhandled neighbor state: %d",
1111 nstate);
1112 break;
1113 }
1114 }
1115
1116 void bs_state_handler(struct bfd_session *bs, int nstate)
1117 {
1118 switch (bs->ses_state) {
1119 case PTM_BFD_ADM_DOWN:
1120 bs_admin_down_handler(bs, nstate);
1121 break;
1122 case PTM_BFD_DOWN:
1123 bs_down_handler(bs, nstate);
1124 break;
1125 case PTM_BFD_INIT:
1126 bs_init_handler(bs, nstate);
1127 break;
1128 case PTM_BFD_UP:
1129 bs_up_handler(bs, nstate);
1130 break;
1131
1132 default:
1133 if (bglobal.debug_peer_event)
1134 zlog_debug("state-change: [%s] is in invalid state: %d",
1135 bs_to_string(bs), nstate);
1136 break;
1137 }
1138 }
1139
1140 /*
1141 * Handles echo timer manipulation after updating timer.
1142 */
1143 void bs_echo_timer_handler(struct bfd_session *bs)
1144 {
1145 uint32_t old_timer;
1146
1147 /*
1148 * Before doing any echo handling, check if it is possible to
1149 * use it.
1150 *
1151 * - Check for `echo-mode` configuration.
1152 * - Check that we are not using multi hop (RFC 5883,
1153 * Section 3).
1154 * - Check that we are already at the up state.
1155 */
1156 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0
1157 || CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
1158 || bs->ses_state != PTM_BFD_UP)
1159 return;
1160
1161 /* Remote peer asked to stop echo. */
1162 if (bs->remote_timers.required_min_echo == 0) {
1163 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
1164 ptm_bfd_echo_stop(bs);
1165
1166 return;
1167 }
1168
1169 /*
1170 * Calculate the echo transmission timer: we must not send
1171 * echo packets faster than the minimum required time
1172 * announced by the remote system.
1173 *
1174 * RFC 5880, Section 6.8.9.
1175 */
1176 old_timer = bs->echo_xmt_TO;
1177 if (bs->remote_timers.required_min_echo > bs->timers.desired_min_echo_tx)
1178 bs->echo_xmt_TO = bs->remote_timers.required_min_echo;
1179 else
1180 bs->echo_xmt_TO = bs->timers.desired_min_echo_tx;
1181
1182 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0
1183 || old_timer != bs->echo_xmt_TO)
1184 ptm_bfd_echo_start(bs);
1185 }
1186
1187 /*
1188 * RFC 5880 Section 6.5.
1189 *
1190 * When a BFD control packet with the final bit is received, we must
1191 * update the session parameters.
1192 */
1193 void bs_final_handler(struct bfd_session *bs)
1194 {
1195 /* Start using our new timers. */
1196 bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx;
1197 bs->cur_timers.required_min_rx = bs->timers.required_min_rx;
1198
1199 /*
1200 * TODO: demand mode. See RFC 5880 Section 6.1.
1201 *
1202 * When using demand mode we must disable the detection timer
1203 * for lost control packets.
1204 */
1205 if (bs->demand_mode) {
1206 /* Notify watchers about changed timers. */
1207 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
1208 return;
1209 }
1210
1211 /*
1212 * Calculate transmission time based on new timers.
1213 *
1214 * Transmission calculation:
1215 * Unless specified by exceptions at the end of Section 6.8.7, the
1216 * transmission time will be determined by the system with the
1217 * slowest rate.
1218 *
1219 * RFC 5880, Section 6.8.7.
1220 */
1221 if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx)
1222 bs->xmt_TO = bs->timers.desired_min_tx;
1223 else
1224 bs->xmt_TO = bs->remote_timers.required_min_rx;
1225
1226 /* Apply new transmission timer immediately. */
1227 ptm_bfd_start_xmt_timer(bs, false);
1228
1229 /*
1230 * Detection timeout calculation:
1231 * The minimum detection timeout is the remote detection
1232 * multipler (number of packets to be missed) times the agreed
1233 * transmission interval.
1234 *
1235 * RFC 5880, Section 6.8.4.
1236 *
1237 * TODO: support sending/counting more packets inside detection
1238 * timeout.
1239 */
1240 if (bs->timers.required_min_rx > bs->remote_timers.desired_min_tx)
1241 bs->detect_TO = bs->remote_detect_mult
1242 * bs->timers.required_min_rx;
1243 else
1244 bs->detect_TO = bs->remote_detect_mult
1245 * bs->remote_timers.desired_min_tx;
1246
1247 /* Apply new receive timer immediately. */
1248 bfd_recvtimer_update(bs);
1249
1250 /* Notify watchers about changed timers. */
1251 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
1252 }
1253
1254 void bs_set_slow_timers(struct bfd_session *bs)
1255 {
1256 /*
1257 * BFD connection must use slow timers before going up or after
1258 * losing connectivity to avoid wasting bandwidth.
1259 *
1260 * RFC 5880, Section 6.8.3.
1261 */
1262 bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX;
1263 bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX;
1264 bs->cur_timers.required_min_echo = 0;
1265
1266 /* Set the appropriated timeouts for slow connection. */
1267 bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX);
1268 bs->xmt_TO = BFD_DEF_SLOWTX;
1269 }
1270
1271 void bfd_set_echo(struct bfd_session *bs, bool echo)
1272 {
1273 if (echo) {
1274 /* Check if echo mode is already active. */
1275 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
1276 return;
1277
1278 SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
1279
1280 /* Activate/update echo receive timeout timer. */
1281 if (bs->bdc == NULL)
1282 bs_echo_timer_handler(bs);
1283 } else {
1284 /* Check if echo mode is already disabled. */
1285 if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
1286 return;
1287
1288 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
1289
1290 /* Deactivate timeout timer. */
1291 if (bs->bdc == NULL)
1292 ptm_bfd_echo_stop(bs);
1293 }
1294 }
1295
1296 void bfd_set_shutdown(struct bfd_session *bs, bool shutdown)
1297 {
1298 bool is_shutdown;
1299
1300 /*
1301 * Special case: we are batching changes and the previous state was
1302 * not shutdown. Instead of potentially disconnect a running peer,
1303 * we'll get the current status to validate we were really down.
1304 */
1305 if (bs->ses_state == PTM_BFD_UP)
1306 is_shutdown = false;
1307 else
1308 is_shutdown = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1309
1310 if (shutdown) {
1311 /* Already shutdown. */
1312 if (is_shutdown)
1313 return;
1314
1315 SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1316
1317 /* Handle data plane shutdown case. */
1318 if (bs->bdc) {
1319 bs->ses_state = PTM_BFD_ADM_DOWN;
1320 bfd_dplane_update_session(bs);
1321 control_notify(bs, bs->ses_state);
1322 return;
1323 }
1324
1325 /* Disable all events. */
1326 bfd_recvtimer_delete(bs);
1327 bfd_echo_recvtimer_delete(bs);
1328 bfd_xmttimer_delete(bs);
1329 bfd_echo_xmttimer_delete(bs);
1330
1331 /* Change and notify state change. */
1332 bs->ses_state = PTM_BFD_ADM_DOWN;
1333 control_notify(bs, bs->ses_state);
1334
1335 /* Don't try to send packets with a disabled session. */
1336 if (bs->sock != -1)
1337 ptm_bfd_snd(bs, 0);
1338 } else {
1339 /* Already working. */
1340 if (!is_shutdown)
1341 return;
1342
1343 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1344
1345 /* Handle data plane shutdown case. */
1346 if (bs->bdc) {
1347 bs->ses_state = PTM_BFD_DOWN;
1348 bfd_dplane_update_session(bs);
1349 control_notify(bs, bs->ses_state);
1350 return;
1351 }
1352
1353 /* Change and notify state change. */
1354 bs->ses_state = PTM_BFD_DOWN;
1355 control_notify(bs, bs->ses_state);
1356
1357 /* Enable timers if non passive, otherwise stop them. */
1358 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE)) {
1359 bfd_recvtimer_delete(bs);
1360 bfd_xmttimer_delete(bs);
1361 } else {
1362 bfd_recvtimer_update(bs);
1363 bfd_xmttimer_update(bs, bs->xmt_TO);
1364 }
1365 }
1366 }
1367
1368 void bfd_set_passive_mode(struct bfd_session *bs, bool passive)
1369 {
1370 if (passive) {
1371 SET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE);
1372
1373 /* Session is already up and running, nothing to do now. */
1374 if (bs->ses_state != PTM_BFD_DOWN)
1375 return;
1376
1377 /* Lets disable the timers since we are now passive. */
1378 bfd_recvtimer_delete(bs);
1379 bfd_xmttimer_delete(bs);
1380 } else {
1381 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE);
1382
1383 /* Session is already up and running, nothing to do now. */
1384 if (bs->ses_state != PTM_BFD_DOWN)
1385 return;
1386
1387 /* Session is down, let it attempt to start the connection. */
1388 bfd_xmttimer_update(bs, bs->xmt_TO);
1389 bfd_recvtimer_update(bs);
1390 }
1391 }
1392
1393 /*
1394 * Helper functions.
1395 */
1396 static const char *get_diag_str(int diag)
1397 {
1398 for (int i = 0; diag_list[i].str; i++) {
1399 if (diag_list[i].type == diag)
1400 return diag_list[i].str;
1401 }
1402 return "N/A";
1403 }
1404
1405 const char *satostr(const struct sockaddr_any *sa)
1406 {
1407 #define INETSTR_BUFCOUNT 8
1408 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
1409 static int bufidx;
1410 const struct sockaddr_in *sin = &sa->sa_sin;
1411 const struct sockaddr_in6 *sin6 = &sa->sa_sin6;
1412
1413 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
1414 buf[bufidx][0] = 0;
1415
1416 switch (sin->sin_family) {
1417 case AF_INET:
1418 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
1419 sizeof(buf[bufidx]));
1420 break;
1421 case AF_INET6:
1422 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
1423 sizeof(buf[bufidx]));
1424 break;
1425
1426 default:
1427 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
1428 break;
1429 }
1430
1431 return buf[bufidx];
1432 }
1433
1434 const char *diag2str(uint8_t diag)
1435 {
1436 switch (diag) {
1437 case 0:
1438 return "ok";
1439 case 1:
1440 return "control detection time expired";
1441 case 2:
1442 return "echo function failed";
1443 case 3:
1444 return "neighbor signaled session down";
1445 case 4:
1446 return "forwarding plane reset";
1447 case 5:
1448 return "path down";
1449 case 6:
1450 return "concatenated path down";
1451 case 7:
1452 return "administratively down";
1453 case 8:
1454 return "reverse concatenated path down";
1455 default:
1456 return "unknown";
1457 }
1458 }
1459
1460 int strtosa(const char *addr, struct sockaddr_any *sa)
1461 {
1462 memset(sa, 0, sizeof(*sa));
1463
1464 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
1465 sa->sa_sin.sin_family = AF_INET;
1466 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1467 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
1468 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1469 return 0;
1470 }
1471
1472 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
1473 sa->sa_sin6.sin6_family = AF_INET6;
1474 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1475 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
1476 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1477 return 0;
1478 }
1479
1480 return -1;
1481 }
1482
1483 void integer2timestr(uint64_t time, char *buf, size_t buflen)
1484 {
1485 unsigned int year, month, day, hour, minute, second;
1486 int rv;
1487
1488 #define MINUTES (60)
1489 #define HOURS (60 * MINUTES)
1490 #define DAYS (24 * HOURS)
1491 #define MONTHS (30 * DAYS)
1492 #define YEARS (12 * MONTHS)
1493 if (time >= YEARS) {
1494 year = time / YEARS;
1495 time -= year * YEARS;
1496
1497 rv = snprintf(buf, buflen, "%u year(s), ", year);
1498 buf += rv;
1499 buflen -= rv;
1500 }
1501 if (time >= MONTHS) {
1502 month = time / MONTHS;
1503 time -= month * MONTHS;
1504
1505 rv = snprintf(buf, buflen, "%u month(s), ", month);
1506 buf += rv;
1507 buflen -= rv;
1508 }
1509 if (time >= DAYS) {
1510 day = time / DAYS;
1511 time -= day * DAYS;
1512
1513 rv = snprintf(buf, buflen, "%u day(s), ", day);
1514 buf += rv;
1515 buflen -= rv;
1516 }
1517 if (time >= HOURS) {
1518 hour = time / HOURS;
1519 time -= hour * HOURS;
1520
1521 rv = snprintf(buf, buflen, "%u hour(s), ", hour);
1522 buf += rv;
1523 buflen -= rv;
1524 }
1525 if (time >= MINUTES) {
1526 minute = time / MINUTES;
1527 time -= minute * MINUTES;
1528
1529 rv = snprintf(buf, buflen, "%u minute(s), ", minute);
1530 buf += rv;
1531 buflen -= rv;
1532 }
1533 second = time % MINUTES;
1534 snprintf(buf, buflen, "%u second(s)", second);
1535 }
1536
1537 const char *bs_to_string(const struct bfd_session *bs)
1538 {
1539 static char buf[256];
1540 char addr_buf[INET6_ADDRSTRLEN];
1541 int pos;
1542 bool is_mhop = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
1543
1544 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
1545 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
1546 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
1547 sizeof(addr_buf)));
1548 pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s",
1549 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
1550 sizeof(addr_buf)));
1551 if (bs->key.vrfname[0])
1552 pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
1553 bs->key.vrfname);
1554 if (bs->key.ifname[0])
1555 pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s",
1556 bs->key.ifname);
1557
1558 (void)pos;
1559
1560 return buf;
1561 }
1562
1563 int bs_observer_add(struct bfd_session *bs)
1564 {
1565 struct bfd_session_observer *bso;
1566
1567 bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso));
1568 bso->bso_bs = bs;
1569 bso->bso_addr.family = bs->key.family;
1570 memcpy(&bso->bso_addr.u.prefix, &bs->key.local,
1571 sizeof(bs->key.local));
1572
1573 TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry);
1574
1575 return 0;
1576 }
1577
1578 void bs_observer_del(struct bfd_session_observer *bso)
1579 {
1580 TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry);
1581 XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso);
1582 }
1583
1584 void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
1585 {
1586 memset(bpc, 0, sizeof(*bpc));
1587
1588 bpc->bpc_ipv4 = (bs->key.family == AF_INET);
1589 bpc->bpc_mhop = bs->key.mhop;
1590
1591 switch (bs->key.family) {
1592 case AF_INET:
1593 bpc->bpc_peer.sa_sin.sin_family = AF_INET;
1594 memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer,
1595 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
1596
1597 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) {
1598 bpc->bpc_local.sa_sin.sin_family = AF_INET6;
1599 memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local,
1600 sizeof(bpc->bpc_local.sa_sin.sin_addr));
1601 }
1602 break;
1603
1604 case AF_INET6:
1605 bpc->bpc_peer.sa_sin.sin_family = AF_INET6;
1606 memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer,
1607 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
1608
1609 bpc->bpc_local.sa_sin6.sin6_family = AF_INET6;
1610 memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local,
1611 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
1612 break;
1613 }
1614
1615 if (bs->key.ifname[0]) {
1616 bpc->bpc_has_localif = true;
1617 strlcpy(bpc->bpc_localif, bs->key.ifname,
1618 sizeof(bpc->bpc_localif));
1619 }
1620
1621 if (bs->key.vrfname[0]) {
1622 bpc->bpc_has_vrfname = true;
1623 strlcpy(bpc->bpc_vrfname, bs->key.vrfname,
1624 sizeof(bpc->bpc_vrfname));
1625 }
1626 }
1627
1628
1629 /*
1630 * BFD hash data structures to find sessions.
1631 */
1632 static struct hash *bfd_id_hash;
1633 static struct hash *bfd_key_hash;
1634
1635 static unsigned int bfd_id_hash_do(const void *p);
1636 static unsigned int bfd_key_hash_do(const void *p);
1637
1638 static void _bfd_free(struct hash_bucket *hb,
1639 void *arg __attribute__((__unused__)));
1640
1641 /* BFD hash for our discriminator. */
1642 static unsigned int bfd_id_hash_do(const void *p)
1643 {
1644 const struct bfd_session *bs = p;
1645
1646 return jhash_1word(bs->discrs.my_discr, 0);
1647 }
1648
1649 static bool bfd_id_hash_cmp(const void *n1, const void *n2)
1650 {
1651 const struct bfd_session *bs1 = n1, *bs2 = n2;
1652
1653 return bs1->discrs.my_discr == bs2->discrs.my_discr;
1654 }
1655
1656 /* BFD hash for single hop. */
1657 static unsigned int bfd_key_hash_do(const void *p)
1658 {
1659 const struct bfd_session *bs = p;
1660 struct bfd_key key = bs->key;
1661
1662 /*
1663 * Local address and interface name are optional and
1664 * can be filled any time after session creation.
1665 * Hash key should not depend on these fields.
1666 */
1667 memset(&key.local, 0, sizeof(key.local));
1668 memset(key.ifname, 0, sizeof(key.ifname));
1669
1670 return jhash(&key, sizeof(key), 0);
1671 }
1672
1673 static bool bfd_key_hash_cmp(const void *n1, const void *n2)
1674 {
1675 const struct bfd_session *bs1 = n1, *bs2 = n2;
1676
1677 if (bs1->key.family != bs2->key.family)
1678 return false;
1679 if (bs1->key.mhop != bs2->key.mhop)
1680 return false;
1681 if (memcmp(&bs1->key.peer, &bs2->key.peer, sizeof(bs1->key.peer)))
1682 return false;
1683 if (memcmp(bs1->key.vrfname, bs2->key.vrfname,
1684 sizeof(bs1->key.vrfname)))
1685 return false;
1686
1687 /*
1688 * Local address is optional and can be empty.
1689 * If both addresses are not empty and different,
1690 * then the keys are different.
1691 */
1692 if (memcmp(&bs1->key.local, &zero_addr, sizeof(bs1->key.local))
1693 && memcmp(&bs2->key.local, &zero_addr, sizeof(bs2->key.local))
1694 && memcmp(&bs1->key.local, &bs2->key.local, sizeof(bs1->key.local)))
1695 return false;
1696
1697 /*
1698 * Interface name is optional and can be empty.
1699 * If both names are not empty and different,
1700 * then the keys are different.
1701 */
1702 if (bs1->key.ifname[0] && bs2->key.ifname[0]
1703 && memcmp(bs1->key.ifname, bs2->key.ifname,
1704 sizeof(bs1->key.ifname)))
1705 return false;
1706
1707 return true;
1708 }
1709
1710
1711 /*
1712 * Hash public interface / exported functions.
1713 */
1714
1715 /* Lookup functions. */
1716 struct bfd_session *bfd_id_lookup(uint32_t id)
1717 {
1718 struct bfd_session bs;
1719
1720 bs.discrs.my_discr = id;
1721
1722 return hash_lookup(bfd_id_hash, &bs);
1723 }
1724
1725 struct bfd_session *bfd_key_lookup(struct bfd_key key)
1726 {
1727 struct bfd_session bs;
1728
1729 bs.key = key;
1730
1731 return hash_lookup(bfd_key_hash, &bs);
1732 }
1733
1734 /*
1735 * Delete functions.
1736 *
1737 * Delete functions searches and remove the item from the hash and
1738 * returns a pointer to the removed item data. If the item was not found
1739 * then it returns NULL.
1740 *
1741 * The data stored inside the hash is not free()ed, so you must do it
1742 * manually after getting the pointer back.
1743 */
1744 struct bfd_session *bfd_id_delete(uint32_t id)
1745 {
1746 struct bfd_session bs;
1747
1748 bs.discrs.my_discr = id;
1749
1750 return hash_release(bfd_id_hash, &bs);
1751 }
1752
1753 struct bfd_session *bfd_key_delete(struct bfd_key key)
1754 {
1755 struct bfd_session bs;
1756
1757 bs.key = key;
1758
1759 return hash_release(bfd_key_hash, &bs);
1760 }
1761
1762 /* Iteration functions. */
1763 void bfd_id_iterate(hash_iter_func hif, void *arg)
1764 {
1765 hash_iterate(bfd_id_hash, hif, arg);
1766 }
1767
1768 void bfd_key_iterate(hash_iter_func hif, void *arg)
1769 {
1770 hash_iterate(bfd_key_hash, hif, arg);
1771 }
1772
1773 /*
1774 * Insert functions.
1775 *
1776 * Inserts session into hash and returns `true` on success, otherwise
1777 * `false`.
1778 */
1779 bool bfd_id_insert(struct bfd_session *bs)
1780 {
1781 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1782 }
1783
1784 bool bfd_key_insert(struct bfd_session *bs)
1785 {
1786 return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs);
1787 }
1788
1789 void bfd_initialize(void)
1790 {
1791 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1792 "BFD session discriminator hash");
1793 bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp,
1794 "BFD session hash");
1795 TAILQ_INIT(&bplist);
1796 }
1797
1798 static void _bfd_free(struct hash_bucket *hb,
1799 void *arg __attribute__((__unused__)))
1800 {
1801 struct bfd_session *bs = hb->data;
1802
1803 bfd_session_free(bs);
1804 }
1805
1806 void bfd_shutdown(void)
1807 {
1808 struct bfd_profile *bp;
1809
1810 /*
1811 * Close and free all BFD sessions.
1812 *
1813 * _bfd_free() will call bfd_session_free() which will take care
1814 * of removing the session from all hashes, so we just run an
1815 * assert() here to make sure it really happened.
1816 */
1817 bfd_id_iterate(_bfd_free, NULL);
1818 assert(bfd_key_hash->count == 0);
1819
1820 /* Now free the hashes themselves. */
1821 hash_free(bfd_id_hash);
1822 hash_free(bfd_key_hash);
1823
1824 /* Free all profile allocations. */
1825 while ((bp = TAILQ_FIRST(&bplist)) != NULL)
1826 bfd_profile_free(bp);
1827 }
1828
1829 struct bfd_session_iterator {
1830 int bsi_stop;
1831 bool bsi_mhop;
1832 const struct bfd_session *bsi_bs;
1833 };
1834
1835 static int _bfd_session_next(struct hash_bucket *hb, void *arg)
1836 {
1837 struct bfd_session_iterator *bsi = arg;
1838 struct bfd_session *bs = hb->data;
1839
1840 /* Previous entry signaled stop. */
1841 if (bsi->bsi_stop == 1) {
1842 /* Match the single/multi hop sessions. */
1843 if (bs->key.mhop != bsi->bsi_mhop)
1844 return HASHWALK_CONTINUE;
1845
1846 bsi->bsi_bs = bs;
1847 return HASHWALK_ABORT;
1848 }
1849
1850 /* We found the current item, stop in the next one. */
1851 if (bsi->bsi_bs == hb->data) {
1852 bsi->bsi_stop = 1;
1853 /* Set entry to NULL to signal end of list. */
1854 bsi->bsi_bs = NULL;
1855 } else if (bsi->bsi_bs == NULL && bsi->bsi_mhop == bs->key.mhop) {
1856 /* We want the first list item. */
1857 bsi->bsi_stop = 1;
1858 bsi->bsi_bs = hb->data;
1859 return HASHWALK_ABORT;
1860 }
1861
1862 return HASHWALK_CONTINUE;
1863 }
1864
1865 /*
1866 * bfd_session_next: uses the current session to find the next.
1867 *
1868 * `bs` might point to NULL to get the first item of the data structure.
1869 */
1870 const struct bfd_session *bfd_session_next(const struct bfd_session *bs,
1871 bool mhop)
1872 {
1873 struct bfd_session_iterator bsi;
1874
1875 bsi.bsi_stop = 0;
1876 bsi.bsi_bs = bs;
1877 bsi.bsi_mhop = mhop;
1878 hash_walk(bfd_key_hash, _bfd_session_next, &bsi);
1879 if (bsi.bsi_stop == 0)
1880 return NULL;
1881
1882 return bsi.bsi_bs;
1883 }
1884
1885 static void _bfd_session_remove_manual(struct hash_bucket *hb,
1886 void *arg __attribute__((__unused__)))
1887 {
1888 struct bfd_session *bs = hb->data;
1889
1890 /* Delete only manually configured sessions. */
1891 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG) == 0)
1892 return;
1893
1894 bs->refcount--;
1895 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG);
1896
1897 /* Don't delete sessions still in use. */
1898 if (bs->refcount != 0)
1899 return;
1900
1901 bfd_session_free(bs);
1902 }
1903
1904 /*
1905 * bfd_sessions_remove_manual: remove all manually configured sessions.
1906 *
1907 * NOTE: this function doesn't remove automatically created sessions.
1908 */
1909 void bfd_sessions_remove_manual(void)
1910 {
1911 hash_iterate(bfd_key_hash, _bfd_session_remove_manual, NULL);
1912 }
1913
1914 void bfd_profiles_remove(void)
1915 {
1916 struct bfd_profile *bp;
1917
1918 while ((bp = TAILQ_FIRST(&bplist)) != NULL)
1919 bfd_profile_free(bp);
1920 }
1921
1922 /*
1923 * Profile related hash functions.
1924 */
1925 static void _bfd_profile_update(struct hash_bucket *hb, void *arg)
1926 {
1927 struct bfd_profile *bp = arg;
1928 struct bfd_session *bs = hb->data;
1929
1930 /* This session is not using the profile. */
1931 if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0)
1932 return;
1933
1934 bfd_profile_apply(bp->name, bs);
1935 }
1936
1937 void bfd_profile_update(struct bfd_profile *bp)
1938 {
1939 hash_iterate(bfd_key_hash, _bfd_profile_update, bp);
1940 }
1941
1942 static void _bfd_profile_detach(struct hash_bucket *hb, void *arg)
1943 {
1944 struct bfd_profile *bp = arg;
1945 struct bfd_session *bs = hb->data;
1946
1947 /* This session is not using the profile. */
1948 if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0)
1949 return;
1950
1951 bfd_profile_remove(bs);
1952 }
1953
1954 static void bfd_profile_detach(struct bfd_profile *bp)
1955 {
1956 hash_iterate(bfd_key_hash, _bfd_profile_detach, bp);
1957 }
1958
1959 /*
1960 * VRF related functions.
1961 */
1962 static int bfd_vrf_new(struct vrf *vrf)
1963 {
1964 if (bglobal.debug_zebra)
1965 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
1966
1967 return 0;
1968 }
1969
1970 static int bfd_vrf_delete(struct vrf *vrf)
1971 {
1972 if (bglobal.debug_zebra)
1973 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
1974
1975 return 0;
1976 }
1977
1978 static int bfd_vrf_update(struct vrf *vrf)
1979 {
1980 if (!vrf_is_enabled(vrf))
1981 return 0;
1982
1983 if (bglobal.debug_zebra)
1984 zlog_debug("VRF update: %s(%u)", vrf->name, vrf->vrf_id);
1985
1986 /* a different name is given; update bfd list */
1987 bfdd_sessions_enable_vrf(vrf);
1988 return 0;
1989 }
1990
1991 static int bfd_vrf_enable(struct vrf *vrf)
1992 {
1993 struct bfd_vrf_global *bvrf;
1994
1995 /* a different name */
1996 if (!vrf->info) {
1997 bvrf = XCALLOC(MTYPE_BFDD_VRF, sizeof(struct bfd_vrf_global));
1998 bvrf->vrf = vrf;
1999 vrf->info = (void *)bvrf;
2000
2001 /* Disable sockets if using data plane. */
2002 if (bglobal.bg_use_dplane) {
2003 bvrf->bg_shop = -1;
2004 bvrf->bg_mhop = -1;
2005 bvrf->bg_shop6 = -1;
2006 bvrf->bg_mhop6 = -1;
2007 bvrf->bg_echo = -1;
2008 bvrf->bg_echov6 = -1;
2009 }
2010 } else
2011 bvrf = vrf->info;
2012
2013 if (bglobal.debug_zebra)
2014 zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
2015
2016 if (vrf->vrf_id == VRF_DEFAULT ||
2017 vrf_get_backend() == VRF_BACKEND_NETNS) {
2018 if (!bvrf->bg_shop)
2019 bvrf->bg_shop = bp_udp_shop(vrf);
2020 if (!bvrf->bg_mhop)
2021 bvrf->bg_mhop = bp_udp_mhop(vrf);
2022 if (!bvrf->bg_shop6)
2023 bvrf->bg_shop6 = bp_udp6_shop(vrf);
2024 if (!bvrf->bg_mhop6)
2025 bvrf->bg_mhop6 = bp_udp6_mhop(vrf);
2026 if (!bvrf->bg_echo)
2027 bvrf->bg_echo = bp_echo_socket(vrf);
2028 if (!bvrf->bg_echov6)
2029 bvrf->bg_echov6 = bp_echov6_socket(vrf);
2030
2031 if (!bvrf->bg_ev[0] && bvrf->bg_shop != -1)
2032 thread_add_read(master, bfd_recv_cb, bvrf,
2033 bvrf->bg_shop, &bvrf->bg_ev[0]);
2034 if (!bvrf->bg_ev[1] && bvrf->bg_mhop != -1)
2035 thread_add_read(master, bfd_recv_cb, bvrf,
2036 bvrf->bg_mhop, &bvrf->bg_ev[1]);
2037 if (!bvrf->bg_ev[2] && bvrf->bg_shop6 != -1)
2038 thread_add_read(master, bfd_recv_cb, bvrf,
2039 bvrf->bg_shop6, &bvrf->bg_ev[2]);
2040 if (!bvrf->bg_ev[3] && bvrf->bg_mhop6 != -1)
2041 thread_add_read(master, bfd_recv_cb, bvrf,
2042 bvrf->bg_mhop6, &bvrf->bg_ev[3]);
2043 if (!bvrf->bg_ev[4] && bvrf->bg_echo != -1)
2044 thread_add_read(master, bfd_recv_cb, bvrf,
2045 bvrf->bg_echo, &bvrf->bg_ev[4]);
2046 if (!bvrf->bg_ev[5] && bvrf->bg_echov6 != -1)
2047 thread_add_read(master, bfd_recv_cb, bvrf,
2048 bvrf->bg_echov6, &bvrf->bg_ev[5]);
2049 }
2050 if (vrf->vrf_id != VRF_DEFAULT) {
2051 bfdd_zclient_register(vrf->vrf_id);
2052 bfdd_sessions_enable_vrf(vrf);
2053 }
2054 return 0;
2055 }
2056
2057 static int bfd_vrf_disable(struct vrf *vrf)
2058 {
2059 struct bfd_vrf_global *bvrf;
2060
2061 if (!vrf->info)
2062 return 0;
2063 bvrf = vrf->info;
2064
2065 if (vrf->vrf_id != VRF_DEFAULT) {
2066 bfdd_sessions_disable_vrf(vrf);
2067 bfdd_zclient_unregister(vrf->vrf_id);
2068 }
2069
2070 if (bglobal.debug_zebra)
2071 zlog_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id);
2072
2073 /* Disable read/write poll triggering. */
2074 THREAD_OFF(bvrf->bg_ev[0]);
2075 THREAD_OFF(bvrf->bg_ev[1]);
2076 THREAD_OFF(bvrf->bg_ev[2]);
2077 THREAD_OFF(bvrf->bg_ev[3]);
2078 THREAD_OFF(bvrf->bg_ev[4]);
2079 THREAD_OFF(bvrf->bg_ev[5]);
2080
2081 /* Close all descriptors. */
2082 socket_close(&bvrf->bg_echo);
2083 socket_close(&bvrf->bg_shop);
2084 socket_close(&bvrf->bg_mhop);
2085 if (bvrf->bg_shop6 != -1)
2086 socket_close(&bvrf->bg_shop6);
2087 if (bvrf->bg_mhop6 != -1)
2088 socket_close(&bvrf->bg_mhop6);
2089 socket_close(&bvrf->bg_echo);
2090 if (bvrf->bg_echov6 != -1)
2091 socket_close(&bvrf->bg_echov6);
2092
2093 /* free context */
2094 XFREE(MTYPE_BFDD_VRF, bvrf);
2095 vrf->info = NULL;
2096
2097 return 0;
2098 }
2099
2100 void bfd_vrf_init(void)
2101 {
2102 vrf_init(bfd_vrf_new, bfd_vrf_enable, bfd_vrf_disable,
2103 bfd_vrf_delete, bfd_vrf_update);
2104 }
2105
2106 void bfd_vrf_terminate(void)
2107 {
2108 vrf_terminate();
2109 }
2110
2111 struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd)
2112 {
2113 struct vrf *vrf;
2114
2115 if (!vrf_is_backend_netns()) {
2116 vrf = vrf_lookup_by_id(VRF_DEFAULT);
2117 if (vrf)
2118 return (struct bfd_vrf_global *)vrf->info;
2119 return NULL;
2120 }
2121 if (!bfd)
2122 return NULL;
2123 if (!bfd->vrf)
2124 return NULL;
2125 return bfd->vrf->info;
2126 }
2127
2128 void bfd_session_update_vrf_name(struct bfd_session *bs, struct vrf *vrf)
2129 {
2130 if (!vrf || !bs)
2131 return;
2132 /* update key */
2133 hash_release(bfd_key_hash, bs);
2134 /*
2135 * HACK: Change the BFD VRF in the running configuration directly,
2136 * bypassing the northbound layer. This is necessary to avoid deleting
2137 * the BFD and readding it in the new VRF, which would have
2138 * several implications.
2139 */
2140 if (yang_module_find("frr-bfdd") && bs->key.vrfname[0]) {
2141 struct lyd_node *bfd_dnode;
2142 char xpath[XPATH_MAXLEN], xpath_srcaddr[XPATH_MAXLEN + 32];
2143 char oldpath[XPATH_MAXLEN], newpath[XPATH_MAXLEN];
2144 char addr_buf[INET6_ADDRSTRLEN];
2145 int slen;
2146
2147 /* build xpath */
2148 if (bs->key.mhop) {
2149 inet_ntop(bs->key.family, &bs->key.local, addr_buf, sizeof(addr_buf));
2150 snprintf(xpath_srcaddr, sizeof(xpath_srcaddr), "[source-addr='%s']",
2151 addr_buf);
2152 } else
2153 xpath_srcaddr[0] = 0;
2154 inet_ntop(bs->key.family, &bs->key.peer, addr_buf, sizeof(addr_buf));
2155 slen = snprintf(xpath, sizeof(xpath),
2156 "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']",
2157 bs->key.mhop ? "multi-hop" : "single-hop", xpath_srcaddr,
2158 addr_buf);
2159 if (bs->key.ifname[0])
2160 slen += snprintf(xpath + slen, sizeof(xpath) - slen,
2161 "[interface='%s']", bs->key.ifname);
2162 else
2163 slen += snprintf(xpath + slen, sizeof(xpath) - slen,
2164 "[interface='*']");
2165 snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']/vrf",
2166 bs->key.vrfname);
2167
2168 bfd_dnode = yang_dnode_getf(running_config->dnode, xpath,
2169 bs->key.vrfname);
2170 if (bfd_dnode) {
2171 yang_dnode_get_path(lyd_parent(bfd_dnode), oldpath,
2172 sizeof(oldpath));
2173 yang_dnode_change_leaf(bfd_dnode, vrf->name);
2174 yang_dnode_get_path(lyd_parent(bfd_dnode), newpath,
2175 sizeof(newpath));
2176 nb_running_move_tree(oldpath, newpath);
2177 running_config->version++;
2178 }
2179 }
2180 memset(bs->key.vrfname, 0, sizeof(bs->key.vrfname));
2181 strlcpy(bs->key.vrfname, vrf->name, sizeof(bs->key.vrfname));
2182 hash_get(bfd_key_hash, bs, hash_alloc_intern);
2183 }
2184
2185 unsigned long bfd_get_session_count(void)
2186 {
2187 return bfd_key_hash->count;
2188 }