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