]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.c
tests: more datastructure tests
[mirror_frr.git] / bfdd / bfd.c
1 /*********************************************************************
2 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * bfd.c: implements the BFD protocol.
20 *
21 * Authors
22 * -------
23 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
24 * Kanna Rajagopal [kanna@cumulusnetworks.com]
25 * Radhika Mahankali [Radhika@cumulusnetworks.com]
26 */
27
28 #include <zebra.h>
29
30 #include "lib/jhash.h"
31
32 #include "bfd.h"
33
34 DEFINE_QOBJ_TYPE(bfd_session);
35
36 /*
37 * Prototypes
38 */
39 void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
40 struct sockaddr_any *local, bool mhop, const char *ifname,
41 const char *vrfname);
42
43 static uint32_t ptm_bfd_gen_ID(void);
44 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
45 static void bfd_session_free(struct bfd_session *bs);
46 static struct bfd_session *bfd_session_new(void);
47 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
48 uint32_t ldisc);
49 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
50 static const char *get_diag_str(int diag);
51
52 static void bs_admin_down_handler(struct bfd_session *bs, int nstate);
53 static void bs_down_handler(struct bfd_session *bs, int nstate);
54 static void bs_init_handler(struct bfd_session *bs, int nstate);
55 static void bs_up_handler(struct bfd_session *bs, int nstate);
56
57 /* Zeroed array with the size of an IPv6 address. */
58 struct in6_addr zero_addr;
59
60 /*
61 * Functions
62 */
63 void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
64 struct sockaddr_any *local, bool mhop, const char *ifname,
65 const char *vrfname)
66 {
67 memset(key, 0, sizeof(*key));
68
69 switch (peer->sa_sin.sin_family) {
70 case AF_INET:
71 key->family = AF_INET;
72 memcpy(&key->peer, &peer->sa_sin.sin_addr,
73 sizeof(peer->sa_sin.sin_addr));
74 memcpy(&key->local, &local->sa_sin.sin_addr,
75 sizeof(local->sa_sin.sin_addr));
76 break;
77 case AF_INET6:
78 key->family = AF_INET6;
79 memcpy(&key->peer, &peer->sa_sin6.sin6_addr,
80 sizeof(peer->sa_sin6.sin6_addr));
81 memcpy(&key->local, &local->sa_sin6.sin6_addr,
82 sizeof(local->sa_sin6.sin6_addr));
83 break;
84 }
85
86 key->mhop = mhop;
87 if (ifname && ifname[0])
88 strlcpy(key->ifname, ifname, sizeof(key->ifname));
89 if (vrfname && vrfname[0])
90 strlcpy(key->vrfname, vrfname, sizeof(key->vrfname));
91 }
92
93 struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
94 {
95 struct bfd_session *bs;
96 struct peer_label *pl;
97 struct bfd_key key;
98
99 /* Try to find label first. */
100 if (bpc->bpc_has_label) {
101 pl = pl_find(bpc->bpc_label);
102 if (pl != NULL) {
103 bs = pl->pl_bs;
104 return bs;
105 }
106 }
107
108 /* Otherwise fallback to peer/local hash lookup. */
109 gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop,
110 bpc->bpc_localif, bpc->bpc_vrfname);
111
112 return bfd_key_lookup(key);
113 }
114
115 /*
116 * Starts a disabled BFD session.
117 *
118 * A session is disabled when the specified interface/VRF doesn't exist
119 * yet. It might happen on FRR boot or with virtual interfaces.
120 */
121 int bfd_session_enable(struct bfd_session *bs)
122 {
123 struct interface *ifp = NULL;
124 struct vrf *vrf = NULL;
125 int psock;
126
127 /*
128 * If the interface or VRF doesn't exist, then we must register
129 * the session but delay its start.
130 */
131 if (bs->key.vrfname[0]) {
132 vrf = vrf_lookup_by_name(bs->key.vrfname);
133 if (vrf == NULL) {
134 log_error(
135 "session-enable: specified VRF doesn't exists.");
136 return 0;
137 }
138 }
139
140 if (bs->key.ifname[0]) {
141 if (vrf)
142 ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id);
143 else
144 ifp = if_lookup_by_name_all_vrf(bs->key.ifname);
145 if (ifp == NULL) {
146 log_error(
147 "session-enable: specified interface doesn't exists.");
148 return 0;
149 }
150 if (bs->key.ifname[0] && !vrf) {
151 vrf = vrf_lookup_by_id(ifp->vrf_id);
152 if (vrf == NULL) {
153 log_error(
154 "session-enable: specified VRF doesn't exists.");
155 return 0;
156 }
157 }
158 }
159
160 /* Assign interface/VRF pointers. */
161 bs->vrf = vrf;
162 if (bs->vrf == NULL)
163 bs->vrf = vrf_lookup_by_id(VRF_DEFAULT);
164
165 if (bs->key.ifname[0]
166 && BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) == 0)
167 bs->ifp = ifp;
168
169 /* Sanity check: don't leak open sockets. */
170 if (bs->sock != -1) {
171 log_debug("session-enable: previous socket open");
172 close(bs->sock);
173 bs->sock = -1;
174 }
175
176 /*
177 * Get socket for transmitting control packets. Note that if we
178 * could use the destination port (3784) for the source
179 * port we wouldn't need a socket per session.
180 */
181 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) {
182 psock = bp_peer_socket(bs);
183 if (psock == -1)
184 return 0;
185 } else {
186 psock = bp_peer_socketv6(bs);
187 if (psock == -1)
188 return 0;
189 }
190
191 /*
192 * We've got a valid socket, lets start the timers and the
193 * protocol.
194 */
195 bs->sock = psock;
196 bfd_recvtimer_update(bs);
197 ptm_bfd_start_xmt_timer(bs, false);
198
199 return 0;
200 }
201
202 /*
203 * Disabled a running BFD session.
204 *
205 * A session is disabled when the specified interface/VRF gets removed
206 * (e.g. virtual interfaces).
207 */
208 void bfd_session_disable(struct bfd_session *bs)
209 {
210 /* Free up socket resources. */
211 if (bs->sock != -1) {
212 close(bs->sock);
213 bs->sock = -1;
214 }
215
216 /* Disable all timers. */
217 bfd_recvtimer_delete(bs);
218 bfd_echo_recvtimer_delete(bs);
219 bfd_xmttimer_delete(bs);
220 bfd_echo_xmttimer_delete(bs);
221 }
222
223 static uint32_t ptm_bfd_gen_ID(void)
224 {
225 uint32_t session_id;
226
227 /*
228 * RFC 5880, Section 6.8.1. recommends that we should generate
229 * random session identification numbers.
230 */
231 do {
232 session_id = ((random() << 16) & 0xFFFF0000)
233 | (random() & 0x0000FFFF);
234 } while (session_id == 0 || bfd_id_lookup(session_id) != NULL);
235
236 return session_id;
237 }
238
239 void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
240 {
241 uint64_t jitter, xmt_TO;
242 int maxpercent;
243
244 xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
245
246 /*
247 * From section 6.5.2: trasmit interval should be randomly jittered
248 * between
249 * 75% and 100% of nominal value, unless detect_mult is 1, then should
250 * be
251 * between 75% and 90%.
252 */
253 maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
254 jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100;
255 /* XXX remove that division above */
256
257 if (is_echo)
258 bfd_echo_xmttimer_update(bfd, jitter);
259 else
260 bfd_xmttimer_update(bfd, jitter);
261 }
262
263 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
264 {
265 /* Send the scheduled echo packet */
266 ptm_bfd_echo_snd(bfd);
267
268 /* Restart the timer for next time */
269 ptm_bfd_start_xmt_timer(bfd, true);
270 }
271
272 void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
273 {
274 /* Send the scheduled control packet */
275 ptm_bfd_snd(bfd, fbit);
276
277 /* Restart the timer for next time */
278 ptm_bfd_start_xmt_timer(bfd, false);
279 }
280
281 void ptm_bfd_echo_stop(struct bfd_session *bfd)
282 {
283 bfd->echo_xmt_TO = 0;
284 bfd->echo_detect_TO = 0;
285 BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
286
287 bfd_echo_xmttimer_delete(bfd);
288 bfd_echo_recvtimer_delete(bfd);
289 }
290
291 void ptm_bfd_echo_start(struct bfd_session *bfd)
292 {
293 bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
294 if (bfd->echo_detect_TO > 0)
295 ptm_bfd_echo_xmt_TO(bfd);
296 }
297
298 void ptm_bfd_sess_up(struct bfd_session *bfd)
299 {
300 int old_state = bfd->ses_state;
301
302 bfd->local_diag = 0;
303 bfd->ses_state = PTM_BFD_UP;
304 monotime(&bfd->uptime);
305
306 /* Connection is up, lets negotiate timers. */
307 bfd_set_polling(bfd);
308
309 /* Start sending control packets with poll bit immediately. */
310 ptm_bfd_snd(bfd, 0);
311
312 control_notify(bfd);
313
314 if (old_state != bfd->ses_state) {
315 bfd->stats.session_up++;
316 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd),
317 state_list[old_state].str,
318 state_list[bfd->ses_state].str);
319 }
320 }
321
322 void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag)
323 {
324 int old_state = bfd->ses_state;
325
326 bfd->local_diag = diag;
327 bfd->discrs.remote_discr = 0;
328 bfd->ses_state = PTM_BFD_DOWN;
329 bfd->polling = 0;
330 bfd->demand_mode = 0;
331 monotime(&bfd->downtime);
332
333 ptm_bfd_snd(bfd, 0);
334
335 /* Slow down the control packets, the connection is down. */
336 bs_set_slow_timers(bfd);
337
338 /* only signal clients when going from up->down state */
339 if (old_state == PTM_BFD_UP)
340 control_notify(bfd);
341
342 /* Stop echo packet transmission if they are active */
343 if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
344 ptm_bfd_echo_stop(bfd);
345
346 if (old_state != bfd->ses_state) {
347 bfd->stats.session_down++;
348 log_info("state-change: [%s] %s -> %s reason:%s",
349 bs_to_string(bfd), state_list[old_state].str,
350 state_list[bfd->ses_state].str,
351 get_diag_str(bfd->local_diag));
352 }
353 }
354
355 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
356 uint32_t ldisc)
357 {
358 struct bfd_session *bs;
359
360 bs = bfd_id_lookup(ldisc);
361 if (bs == NULL)
362 return NULL;
363
364 switch (bs->key.family) {
365 case AF_INET:
366 if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer,
367 sizeof(sa->sa_sin.sin_addr)))
368 return NULL;
369 break;
370 case AF_INET6:
371 if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer,
372 sizeof(sa->sa_sin6.sin6_addr)))
373 return NULL;
374 break;
375 }
376
377 return bs;
378 }
379
380 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
381 struct sockaddr_any *peer,
382 struct sockaddr_any *local,
383 ifindex_t ifindex, vrf_id_t vrfid,
384 bool is_mhop)
385 {
386 struct interface *ifp;
387 struct vrf *vrf;
388 struct bfd_key key;
389
390 /* Find our session using the ID signaled by the remote end. */
391 if (cp->discrs.remote_discr)
392 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
393
394 /* Search for session without using discriminator. */
395 ifp = if_lookup_by_index(ifindex, vrfid);
396 if (vrfid == VRF_DEFAULT) {
397 /*
398 * Don't use the default vrf, otherwise we won't find
399 * sessions that doesn't specify it.
400 */
401 vrf = NULL;
402 } else
403 vrf = vrf_lookup_by_id(vrfid);
404
405 gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL,
406 vrf ? vrf->name : NULL);
407
408 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
409 return bfd_key_lookup(key);
410 }
411
412 int bfd_xmt_cb(struct thread *t)
413 {
414 struct bfd_session *bs = THREAD_ARG(t);
415
416 ptm_bfd_xmt_TO(bs, 0);
417
418 return 0;
419 }
420
421 int bfd_echo_xmt_cb(struct thread *t)
422 {
423 struct bfd_session *bs = THREAD_ARG(t);
424
425 if (bs->echo_xmt_TO > 0)
426 ptm_bfd_echo_xmt_TO(bs);
427
428 return 0;
429 }
430
431 /* Was ptm_bfd_detect_TO() */
432 int bfd_recvtimer_cb(struct thread *t)
433 {
434 struct bfd_session *bs = THREAD_ARG(t);
435
436 switch (bs->ses_state) {
437 case PTM_BFD_INIT:
438 case PTM_BFD_UP:
439 ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED);
440 bfd_recvtimer_update(bs);
441 break;
442
443 default:
444 /* Second detect time expiration, zero remote discr (section
445 * 6.5.1)
446 */
447 bs->discrs.remote_discr = 0;
448 break;
449 }
450
451 return 0;
452 }
453
454 /* Was ptm_bfd_echo_detect_TO() */
455 int bfd_echo_recvtimer_cb(struct thread *t)
456 {
457 struct bfd_session *bs = THREAD_ARG(t);
458
459 switch (bs->ses_state) {
460 case PTM_BFD_INIT:
461 case PTM_BFD_UP:
462 ptm_bfd_sess_dn(bs, BD_ECHO_FAILED);
463 break;
464 }
465
466 return 0;
467 }
468
469 static struct bfd_session *bfd_session_new(void)
470 {
471 struct bfd_session *bs;
472
473 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
474
475 QOBJ_REG(bs, bfd_session);
476
477 bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX;
478 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
479 bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO;
480 bs->detect_mult = BFD_DEFDETECTMULT;
481 bs->mh_ttl = BFD_DEF_MHOP_TTL;
482 bs->ses_state = PTM_BFD_DOWN;
483
484 /* Initiate connection with slow timers. */
485 bs_set_slow_timers(bs);
486
487 /* Initiate remote settings as well. */
488 bs->remote_timers = bs->cur_timers;
489 bs->remote_detect_mult = BFD_DEFDETECTMULT;
490
491 bs->sock = -1;
492 monotime(&bs->uptime);
493 bs->downtime = bs->uptime;
494
495 return bs;
496 }
497
498 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
499 {
500 /* New label treatment:
501 * - Check if the label is taken;
502 * - Try to allocate the memory for it and register;
503 */
504 if (bs->pl == NULL) {
505 if (pl_find(nlabel) != NULL) {
506 /* Someone is already using it. */
507 return -1;
508 }
509
510 if (pl_new(nlabel, bs) == NULL)
511 return -1;
512
513 return 0;
514 }
515
516 /*
517 * Test label change consistency:
518 * - Do nothing if it's the same label;
519 * - Check if the future label is already taken;
520 * - Change label;
521 */
522 if (strcmp(nlabel, bs->pl->pl_label) == 0)
523 return -1;
524 if (pl_find(nlabel) != NULL)
525 return -1;
526
527 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
528 return 0;
529 }
530
531 static void _bfd_session_update(struct bfd_session *bs,
532 struct bfd_peer_cfg *bpc)
533 {
534 if (bpc->bpc_echo) {
535 /* Check if echo mode is already active. */
536 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
537 goto skip_echo;
538
539 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
540
541 /* Activate/update echo receive timeout timer. */
542 bs_echo_timer_handler(bs);
543 } else {
544 /* Check if echo mode is already disabled. */
545 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
546 goto skip_echo;
547
548 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
549 ptm_bfd_echo_stop(bs);
550 }
551
552 skip_echo:
553 if (bpc->bpc_has_txinterval)
554 bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000;
555
556 if (bpc->bpc_has_recvinterval)
557 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
558
559 if (bpc->bpc_has_detectmultiplier)
560 bs->detect_mult = bpc->bpc_detectmultiplier;
561
562 if (bpc->bpc_has_echointerval)
563 bs->timers.required_min_echo = bpc->bpc_echointerval * 1000;
564
565 if (bpc->bpc_has_label)
566 bfd_session_update_label(bs, bpc->bpc_label);
567
568 if (bpc->bpc_shutdown) {
569 /* Check if already shutdown. */
570 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
571 return;
572
573 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
574
575 /* Disable all events. */
576 bfd_recvtimer_delete(bs);
577 bfd_echo_recvtimer_delete(bs);
578 bfd_xmttimer_delete(bs);
579 bfd_echo_xmttimer_delete(bs);
580
581 /* Change and notify state change. */
582 bs->ses_state = PTM_BFD_ADM_DOWN;
583 control_notify(bs);
584
585 /* Don't try to send packets with a disabled session. */
586 if (bs->sock != -1)
587 ptm_bfd_snd(bs, 0);
588 } else {
589 /* Check if already working. */
590 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
591 return;
592
593 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
594
595 /* Change and notify state change. */
596 bs->ses_state = PTM_BFD_DOWN;
597 control_notify(bs);
598
599 /* Enable all timers. */
600 bfd_recvtimer_update(bs);
601 bfd_xmttimer_update(bs, bs->xmt_TO);
602 }
603 if (bpc->bpc_cbit) {
604 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CBIT))
605 return;
606
607 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
608 } else {
609 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CBIT))
610 return;
611
612 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
613 }
614 }
615
616 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
617 {
618 /* User didn't want to update, return failure. */
619 if (bpc->bpc_createonly)
620 return -1;
621
622 _bfd_session_update(bs, bpc);
623
624 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
625
626 return 0;
627 }
628
629 static void bfd_session_free(struct bfd_session *bs)
630 {
631 struct bfd_session_observer *bso;
632
633 bfd_session_disable(bs);
634
635 bfd_key_delete(bs->key);
636 bfd_id_delete(bs->discrs.my_discr);
637
638 /* Remove observer if any. */
639 TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) {
640 if (bso->bso_bs != bs)
641 continue;
642
643 break;
644 }
645 if (bso != NULL)
646 bs_observer_del(bso);
647
648 pl_free(bs->pl);
649
650 QOBJ_UNREG(bs);
651 XFREE(MTYPE_BFDD_CONFIG, bs);
652 }
653
654 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
655 {
656 struct bfd_session *bfd, *l_bfd;
657
658 /* check to see if this needs a new session */
659 l_bfd = bs_peer_find(bpc);
660 if (l_bfd) {
661 /* Requesting a duplicated peer means update configuration. */
662 if (bfd_session_update(l_bfd, bpc) == 0)
663 return l_bfd;
664 else
665 return NULL;
666 }
667
668 /* Get BFD session storage with its defaults. */
669 bfd = bfd_session_new();
670 if (bfd == NULL) {
671 log_error("session-new: allocation failed");
672 return NULL;
673 }
674
675 /*
676 * Store interface/VRF name in case we need to delay session
677 * start. See `bfd_session_enable` for more information.
678 */
679 if (bpc->bpc_has_localif)
680 strlcpy(bfd->key.ifname, bpc->bpc_localif,
681 sizeof(bfd->key.ifname));
682
683 if (bpc->bpc_has_vrfname)
684 strlcpy(bfd->key.vrfname, bpc->bpc_vrfname,
685 sizeof(bfd->key.vrfname));
686
687 /* Copy remaining data. */
688 if (bpc->bpc_ipv4 == false)
689 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
690
691 bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6;
692 switch (bfd->key.family) {
693 case AF_INET:
694 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr,
695 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
696 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr,
697 sizeof(bpc->bpc_local.sa_sin.sin_addr));
698 break;
699
700 case AF_INET6:
701 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr,
702 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
703 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr,
704 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
705 break;
706
707 default:
708 assert(1);
709 break;
710 }
711
712 if (bpc->bpc_mhop)
713 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
714
715 bfd->key.mhop = bpc->bpc_mhop;
716
717 /* Registrate session into data structures. */
718 bfd_key_insert(bfd);
719 bfd->discrs.my_discr = ptm_bfd_gen_ID();
720 bfd_id_insert(bfd);
721
722 /* Try to enable session and schedule for packet receive/send. */
723 if (bfd_session_enable(bfd) == -1) {
724 /* Unrecoverable failure, remove the session/peer. */
725 bfd_session_free(bfd);
726 return NULL;
727 }
728
729 /* Add observer if we have moving parts. */
730 if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1)
731 bs_observer_add(bfd);
732
733 /* Apply other configurations. */
734 _bfd_session_update(bfd, bpc);
735
736 log_info("session-new: %s", bs_to_string(bfd));
737
738 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
739
740 return bfd;
741 }
742
743 int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc)
744 {
745 struct bfd_session *bs;
746
747 /* Find session and call free(). */
748 bs = bs_peer_find(bpc);
749 if (bs == NULL)
750 return -1;
751
752 /* This pointer is being referenced, don't let it be deleted. */
753 if (bs->refcount > 0) {
754 log_error("session-delete: refcount failure: %" PRIu64
755 " references",
756 bs->refcount);
757 return -1;
758 }
759
760 log_info("session-delete: %s", bs_to_string(bs));
761
762 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
763
764 bfd_session_free(bs);
765
766 return 0;
767 }
768
769 void bfd_set_polling(struct bfd_session *bs)
770 {
771 /*
772 * Start polling procedure: the only timers that require polling
773 * to change value without losing connection are:
774 *
775 * - Desired minimum transmission interval;
776 * - Required minimum receive interval;
777 *
778 * RFC 5880, Section 6.8.3.
779 */
780 bs->polling = 1;
781 }
782
783 /*
784 * bs_<state>_handler() functions implement the BFD state machine
785 * transition mechanism. `<state>` is the current session state and
786 * the parameter `nstate` is the peer new state.
787 */
788 static void bs_admin_down_handler(struct bfd_session *bs
789 __attribute__((__unused__)),
790 int nstate __attribute__((__unused__)))
791 {
792 /*
793 * We are administratively down, there is no state machine
794 * handling.
795 */
796 }
797
798 static void bs_down_handler(struct bfd_session *bs, int nstate)
799 {
800 switch (nstate) {
801 case PTM_BFD_ADM_DOWN:
802 /*
803 * Remote peer doesn't want to talk, so lets keep the
804 * connection down.
805 */
806 case PTM_BFD_UP:
807 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
808 break;
809
810 case PTM_BFD_DOWN:
811 /*
812 * Remote peer agreed that the path is down, lets try to
813 * bring it up.
814 */
815 bs->ses_state = PTM_BFD_INIT;
816 break;
817
818 case PTM_BFD_INIT:
819 /*
820 * Remote peer told us his path is up, lets turn
821 * activate the session.
822 */
823 ptm_bfd_sess_up(bs);
824 break;
825
826 default:
827 log_debug("state-change: unhandled neighbor state: %d", nstate);
828 break;
829 }
830 }
831
832 static void bs_init_handler(struct bfd_session *bs, int nstate)
833 {
834 switch (nstate) {
835 case PTM_BFD_ADM_DOWN:
836 /*
837 * Remote peer doesn't want to talk, so lets make the
838 * connection down.
839 */
840 bs->ses_state = PTM_BFD_DOWN;
841 break;
842
843 case PTM_BFD_DOWN:
844 /* Remote peer hasn't moved to first stage yet. */
845 break;
846
847 case PTM_BFD_INIT:
848 case PTM_BFD_UP:
849 /* We agreed on the settings and the path is up. */
850 ptm_bfd_sess_up(bs);
851 break;
852
853 default:
854 log_debug("state-change: unhandled neighbor state: %d", nstate);
855 break;
856 }
857 }
858
859 static void bs_up_handler(struct bfd_session *bs, int nstate)
860 {
861 switch (nstate) {
862 case PTM_BFD_ADM_DOWN:
863 case PTM_BFD_DOWN:
864 /* Peer lost or asked to shutdown connection. */
865 ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN);
866 break;
867
868 case PTM_BFD_INIT:
869 case PTM_BFD_UP:
870 /* Path is up and working. */
871 break;
872
873 default:
874 log_debug("state-change: unhandled neighbor state: %d", nstate);
875 break;
876 }
877 }
878
879 void bs_state_handler(struct bfd_session *bs, int nstate)
880 {
881 switch (bs->ses_state) {
882 case PTM_BFD_ADM_DOWN:
883 bs_admin_down_handler(bs, nstate);
884 break;
885 case PTM_BFD_DOWN:
886 bs_down_handler(bs, nstate);
887 break;
888 case PTM_BFD_INIT:
889 bs_init_handler(bs, nstate);
890 break;
891 case PTM_BFD_UP:
892 bs_up_handler(bs, nstate);
893 break;
894
895 default:
896 log_debug("state-change: [%s] is in invalid state: %d",
897 bs_to_string(bs), nstate);
898 break;
899 }
900 }
901
902 /*
903 * Handles echo timer manipulation after updating timer.
904 */
905 void bs_echo_timer_handler(struct bfd_session *bs)
906 {
907 uint32_t old_timer;
908
909 /*
910 * Before doing any echo handling, check if it is possible to
911 * use it.
912 *
913 * - Check for `echo-mode` configuration.
914 * - Check that we are not using multi hop (RFC 5883,
915 * Section 3).
916 * - Check that we are already at the up state.
917 */
918 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0
919 || BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
920 || bs->ses_state != PTM_BFD_UP)
921 return;
922
923 /* Remote peer asked to stop echo. */
924 if (bs->remote_timers.required_min_echo == 0) {
925 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
926 ptm_bfd_echo_stop(bs);
927
928 return;
929 }
930
931 /*
932 * Calculate the echo transmission timer: we must not send
933 * echo packets faster than the minimum required time
934 * announced by the remote system.
935 *
936 * RFC 5880, Section 6.8.9.
937 */
938 old_timer = bs->echo_xmt_TO;
939 if (bs->remote_timers.required_min_echo > bs->timers.required_min_echo)
940 bs->echo_xmt_TO = bs->remote_timers.required_min_echo;
941 else
942 bs->echo_xmt_TO = bs->timers.required_min_echo;
943
944 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0
945 || old_timer != bs->echo_xmt_TO)
946 ptm_bfd_echo_start(bs);
947 }
948
949 /*
950 * RFC 5880 Section 6.5.
951 *
952 * When a BFD control packet with the final bit is received, we must
953 * update the session parameters.
954 */
955 void bs_final_handler(struct bfd_session *bs)
956 {
957 /* Start using our new timers. */
958 bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx;
959 bs->cur_timers.required_min_rx = bs->timers.required_min_rx;
960
961 /*
962 * TODO: demand mode. See RFC 5880 Section 6.1.
963 *
964 * When using demand mode we must disable the detection timer
965 * for lost control packets.
966 */
967 if (bs->demand_mode) {
968 /* Notify watchers about changed timers. */
969 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
970 return;
971 }
972
973 /*
974 * Calculate detection time based on new timers.
975 *
976 * Transmission calculation:
977 * We must respect the RequiredMinRxInterval from the remote
978 * system: if our desired transmission timer is more than the
979 * minimum receive rate, then we must lower it to at least the
980 * minimum receive interval.
981 *
982 * RFC 5880, Section 6.8.3.
983 */
984 if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx)
985 bs->xmt_TO = bs->remote_timers.required_min_rx;
986 else
987 bs->xmt_TO = bs->timers.desired_min_tx;
988
989 /* Apply new transmission timer immediately. */
990 ptm_bfd_start_xmt_timer(bs, false);
991
992 /*
993 * Detection timeout calculation:
994 * The minimum detection timeout is the remote detection
995 * multipler (number of packets to be missed) times the agreed
996 * transmission interval.
997 *
998 * RFC 5880, Section 6.8.4.
999 *
1000 * TODO: support sending/counting more packets inside detection
1001 * timeout.
1002 */
1003 if (bs->remote_timers.required_min_rx > bs->timers.desired_min_tx)
1004 bs->detect_TO = bs->remote_detect_mult
1005 * bs->remote_timers.required_min_rx;
1006 else
1007 bs->detect_TO = bs->remote_detect_mult
1008 * bs->timers.desired_min_tx;
1009
1010 /* Apply new receive timer immediately. */
1011 bfd_recvtimer_update(bs);
1012
1013 /* Notify watchers about changed timers. */
1014 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
1015 }
1016
1017 void bs_set_slow_timers(struct bfd_session *bs)
1018 {
1019 /*
1020 * BFD connection must use slow timers before going up or after
1021 * losing connectivity to avoid wasting bandwidth.
1022 *
1023 * RFC 5880, Section 6.8.3.
1024 */
1025 bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX;
1026 bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX;
1027 bs->cur_timers.required_min_echo = 0;
1028
1029 /* Set the appropriated timeouts for slow connection. */
1030 bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX);
1031 bs->xmt_TO = BFD_DEF_SLOWTX;
1032 }
1033
1034 /*
1035 * Helper functions.
1036 */
1037 static const char *get_diag_str(int diag)
1038 {
1039 for (int i = 0; diag_list[i].str; i++) {
1040 if (diag_list[i].type == diag)
1041 return diag_list[i].str;
1042 }
1043 return "N/A";
1044 }
1045
1046 const char *satostr(struct sockaddr_any *sa)
1047 {
1048 #define INETSTR_BUFCOUNT 8
1049 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
1050 static int bufidx;
1051 struct sockaddr_in *sin = &sa->sa_sin;
1052 struct sockaddr_in6 *sin6 = &sa->sa_sin6;
1053
1054 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
1055 buf[bufidx][0] = 0;
1056
1057 switch (sin->sin_family) {
1058 case AF_INET:
1059 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
1060 sizeof(buf[bufidx]));
1061 break;
1062 case AF_INET6:
1063 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
1064 sizeof(buf[bufidx]));
1065 break;
1066
1067 default:
1068 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
1069 break;
1070 }
1071
1072 return buf[bufidx];
1073 }
1074
1075 const char *diag2str(uint8_t diag)
1076 {
1077 switch (diag) {
1078 case 0:
1079 return "ok";
1080 case 1:
1081 return "control detection time expired";
1082 case 2:
1083 return "echo function failed";
1084 case 3:
1085 return "neighbor signaled session down";
1086 case 4:
1087 return "forwarding plane reset";
1088 case 5:
1089 return "path down";
1090 case 6:
1091 return "concatenated path down";
1092 case 7:
1093 return "administratively down";
1094 case 8:
1095 return "reverse concatenated path down";
1096 default:
1097 return "unknown";
1098 }
1099 }
1100
1101 int strtosa(const char *addr, struct sockaddr_any *sa)
1102 {
1103 memset(sa, 0, sizeof(*sa));
1104
1105 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
1106 sa->sa_sin.sin_family = AF_INET;
1107 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1108 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
1109 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1110 return 0;
1111 }
1112
1113 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
1114 sa->sa_sin6.sin6_family = AF_INET6;
1115 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1116 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
1117 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1118 return 0;
1119 }
1120
1121 return -1;
1122 }
1123
1124 void integer2timestr(uint64_t time, char *buf, size_t buflen)
1125 {
1126 unsigned int year, month, day, hour, minute, second;
1127 int rv;
1128
1129 #define MINUTES (60)
1130 #define HOURS (60 * MINUTES)
1131 #define DAYS (24 * HOURS)
1132 #define MONTHS (30 * DAYS)
1133 #define YEARS (12 * MONTHS)
1134 if (time >= YEARS) {
1135 year = time / YEARS;
1136 time -= year * YEARS;
1137
1138 rv = snprintf(buf, buflen, "%u year(s), ", year);
1139 buf += rv;
1140 buflen -= rv;
1141 }
1142 if (time >= MONTHS) {
1143 month = time / MONTHS;
1144 time -= month * MONTHS;
1145
1146 rv = snprintf(buf, buflen, "%u month(s), ", month);
1147 buf += rv;
1148 buflen -= rv;
1149 }
1150 if (time >= DAYS) {
1151 day = time / DAYS;
1152 time -= day * DAYS;
1153
1154 rv = snprintf(buf, buflen, "%u day(s), ", day);
1155 buf += rv;
1156 buflen -= rv;
1157 }
1158 if (time >= HOURS) {
1159 hour = time / HOURS;
1160 time -= hour * HOURS;
1161
1162 rv = snprintf(buf, buflen, "%u hour(s), ", hour);
1163 buf += rv;
1164 buflen -= rv;
1165 }
1166 if (time >= MINUTES) {
1167 minute = time / MINUTES;
1168 time -= minute * MINUTES;
1169
1170 rv = snprintf(buf, buflen, "%u minute(s), ", minute);
1171 buf += rv;
1172 buflen -= rv;
1173 }
1174 second = time % MINUTES;
1175 snprintf(buf, buflen, "%u second(s)", second);
1176 }
1177
1178 const char *bs_to_string(const struct bfd_session *bs)
1179 {
1180 static char buf[256];
1181 char addr_buf[INET6_ADDRSTRLEN];
1182 int pos;
1183 bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
1184
1185 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
1186 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
1187 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
1188 sizeof(addr_buf)));
1189 pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s",
1190 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
1191 sizeof(addr_buf)));
1192 if (bs->key.vrfname[0])
1193 pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
1194 bs->key.vrfname);
1195 if (bs->key.ifname[0])
1196 pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s",
1197 bs->key.ifname);
1198
1199 (void)pos;
1200
1201 return buf;
1202 }
1203
1204 int bs_observer_add(struct bfd_session *bs)
1205 {
1206 struct bfd_session_observer *bso;
1207
1208 bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso));
1209 bso->bso_isaddress = false;
1210 bso->bso_bs = bs;
1211 bso->bso_isinterface = !BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
1212 if (bso->bso_isinterface)
1213 strlcpy(bso->bso_entryname, bs->key.ifname,
1214 sizeof(bso->bso_entryname));
1215 /* Handle socket binding failures caused by missing local addresses. */
1216 if (bs->sock == -1) {
1217 bso->bso_isaddress = true;
1218 bso->bso_addr.family = bs->key.family;
1219 memcpy(&bso->bso_addr.u.prefix, &bs->key.local,
1220 sizeof(bs->key.local));
1221 }
1222
1223 TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry);
1224
1225 return 0;
1226 }
1227
1228 void bs_observer_del(struct bfd_session_observer *bso)
1229 {
1230 TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry);
1231 XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso);
1232 }
1233
1234 void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
1235 {
1236 memset(bpc, 0, sizeof(*bpc));
1237
1238 bpc->bpc_ipv4 = (bs->key.family == AF_INET);
1239 bpc->bpc_mhop = bs->key.mhop;
1240
1241 switch (bs->key.family) {
1242 case AF_INET:
1243 bpc->bpc_peer.sa_sin.sin_family = AF_INET;
1244 memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer,
1245 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
1246
1247 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) {
1248 bpc->bpc_local.sa_sin.sin_family = AF_INET6;
1249 memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local,
1250 sizeof(bpc->bpc_local.sa_sin.sin_addr));
1251 }
1252 break;
1253
1254 case AF_INET6:
1255 bpc->bpc_peer.sa_sin.sin_family = AF_INET6;
1256 memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer,
1257 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
1258
1259 bpc->bpc_local.sa_sin6.sin6_family = AF_INET6;
1260 memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local,
1261 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
1262 break;
1263 }
1264
1265 if (bs->key.ifname[0]) {
1266 bpc->bpc_has_localif = true;
1267 strlcpy(bpc->bpc_localif, bs->key.ifname,
1268 sizeof(bpc->bpc_localif));
1269 }
1270
1271 if (bs->key.vrfname[0]) {
1272 bpc->bpc_has_vrfname = true;
1273 strlcpy(bpc->bpc_vrfname, bs->key.vrfname,
1274 sizeof(bpc->bpc_vrfname));
1275 }
1276 }
1277
1278
1279 /*
1280 * BFD hash data structures to find sessions.
1281 */
1282 static struct hash *bfd_id_hash;
1283 static struct hash *bfd_key_hash;
1284
1285 static unsigned int bfd_id_hash_do(const void *p);
1286 static unsigned int bfd_key_hash_do(const void *p);
1287
1288 static void _bfd_free(struct hash_bucket *hb,
1289 void *arg __attribute__((__unused__)));
1290
1291 /* BFD hash for our discriminator. */
1292 static unsigned int bfd_id_hash_do(const void *p)
1293 {
1294 const struct bfd_session *bs = p;
1295
1296 return jhash_1word(bs->discrs.my_discr, 0);
1297 }
1298
1299 static bool bfd_id_hash_cmp(const void *n1, const void *n2)
1300 {
1301 const struct bfd_session *bs1 = n1, *bs2 = n2;
1302
1303 return bs1->discrs.my_discr == bs2->discrs.my_discr;
1304 }
1305
1306 /* BFD hash for single hop. */
1307 static unsigned int bfd_key_hash_do(const void *p)
1308 {
1309 const struct bfd_session *bs = p;
1310
1311 return jhash(&bs->key, sizeof(bs->key), 0);
1312 }
1313
1314 static bool bfd_key_hash_cmp(const void *n1, const void *n2)
1315 {
1316 const struct bfd_session *bs1 = n1, *bs2 = n2;
1317
1318 return memcmp(&bs1->key, &bs2->key, sizeof(bs1->key)) == 0;
1319 }
1320
1321
1322 /*
1323 * Hash public interface / exported functions.
1324 */
1325
1326 /* Lookup functions. */
1327 struct bfd_session *bfd_id_lookup(uint32_t id)
1328 {
1329 struct bfd_session bs;
1330
1331 bs.discrs.my_discr = id;
1332
1333 return hash_lookup(bfd_id_hash, &bs);
1334 }
1335
1336 struct bfd_key_walk_partial_lookup {
1337 struct bfd_session *given;
1338 struct bfd_session *result;
1339 };
1340
1341 /* ignore some parameters */
1342 static int bfd_key_lookup_ignore_partial_walker(struct hash_bucket *b, void *data)
1343 {
1344 struct bfd_key_walk_partial_lookup *ctx =
1345 (struct bfd_key_walk_partial_lookup *)data;
1346 struct bfd_session *given = ctx->given;
1347 struct bfd_session *parsed = b->data;
1348
1349 if (given->key.family != parsed->key.family)
1350 return HASHWALK_CONTINUE;
1351 if (given->key.mhop != parsed->key.mhop)
1352 return HASHWALK_CONTINUE;
1353 if (memcmp(&given->key.peer, &parsed->key.peer, sizeof(struct in6_addr)))
1354 return HASHWALK_CONTINUE;
1355 if (memcmp(given->key.vrfname, parsed->key.vrfname, MAXNAMELEN))
1356 return HASHWALK_CONTINUE;
1357 ctx->result = parsed;
1358 /* ignore localaddr or interface */
1359 return HASHWALK_ABORT;
1360 }
1361
1362 struct bfd_session *bfd_key_lookup(struct bfd_key key)
1363 {
1364 struct bfd_session bs, *bsp;
1365 struct bfd_key_walk_partial_lookup ctx;
1366 char peer_buf[INET6_ADDRSTRLEN];
1367
1368 bs.key = key;
1369 bsp = hash_lookup(bfd_key_hash, &bs);
1370 if (bsp)
1371 return bsp;
1372
1373 inet_ntop(bs.key.family, &bs.key.peer, peer_buf,
1374 sizeof(peer_buf));
1375 /* Handle cases where local-address is optional. */
1376 if (bs.key.family == AF_INET) {
1377 memset(&bs.key.local, 0, sizeof(bs.key.local));
1378 bsp = hash_lookup(bfd_key_hash, &bs);
1379 if (bsp) {
1380 char addr_buf[INET6_ADDRSTRLEN];
1381
1382 inet_ntop(bs.key.family, &key.local, addr_buf,
1383 sizeof(addr_buf));
1384 log_debug(" peer %s found, but loc-addr %s ignored",
1385 peer_buf, addr_buf);
1386 return bsp;
1387 }
1388 }
1389
1390 bs.key = key;
1391 /* Handle cases where ifname is optional. */
1392 if (bs.key.ifname[0]) {
1393 memset(bs.key.ifname, 0, sizeof(bs.key.ifname));
1394 bsp = hash_lookup(bfd_key_hash, &bs);
1395 if (bsp) {
1396 log_debug(" peer %s found, but ifp %s ignored",
1397 peer_buf, key.ifname);
1398 return bsp;
1399 }
1400 }
1401
1402 /* Handle cases where local-address and ifname are optional. */
1403 if (bs.key.family == AF_INET) {
1404 memset(&bs.key.local, 0, sizeof(bs.key.local));
1405 bsp = hash_lookup(bfd_key_hash, &bs);
1406 if (bsp) {
1407 char addr_buf[INET6_ADDRSTRLEN];
1408
1409 inet_ntop(bs.key.family, &bs.key.local, addr_buf,
1410 sizeof(addr_buf));
1411 log_debug(" peer %s found, but ifp %s"
1412 " and loc-addr %s ignored",
1413 peer_buf, key.ifname,
1414 addr_buf);
1415 return bsp;
1416 }
1417 }
1418 bs.key = key;
1419
1420 /* Handle case where a context more complex ctx is present.
1421 * input has no iface nor local-address, but a context may
1422 * exist
1423 */
1424 ctx.result = NULL;
1425 ctx.given = &bs;
1426 hash_walk(bfd_key_hash,
1427 &bfd_key_lookup_ignore_partial_walker,
1428 &ctx);
1429 /* change key */
1430 if (ctx.result) {
1431 bsp = ctx.result;
1432 log_debug(" peer %s found, but ifp"
1433 " and/or loc-addr params ignored");
1434 }
1435 return bsp;
1436 }
1437
1438 /*
1439 * Delete functions.
1440 *
1441 * Delete functions searches and remove the item from the hash and
1442 * returns a pointer to the removed item data. If the item was not found
1443 * then it returns NULL.
1444 *
1445 * The data stored inside the hash is not free()ed, so you must do it
1446 * manually after getting the pointer back.
1447 */
1448 struct bfd_session *bfd_id_delete(uint32_t id)
1449 {
1450 struct bfd_session bs;
1451
1452 bs.discrs.my_discr = id;
1453
1454 return hash_release(bfd_id_hash, &bs);
1455 }
1456
1457 struct bfd_session *bfd_key_delete(struct bfd_key key)
1458 {
1459 struct bfd_session bs, *bsp;
1460
1461 bs.key = key;
1462 bsp = hash_lookup(bfd_key_hash, &bs);
1463 if (bsp == NULL && key.ifname[0]) {
1464 memset(bs.key.ifname, 0, sizeof(bs.key.ifname));
1465 bsp = hash_lookup(bfd_key_hash, &bs);
1466 }
1467
1468 return hash_release(bfd_key_hash, bsp);
1469 }
1470
1471 /* Iteration functions. */
1472 void bfd_id_iterate(hash_iter_func hif, void *arg)
1473 {
1474 hash_iterate(bfd_id_hash, hif, arg);
1475 }
1476
1477 void bfd_key_iterate(hash_iter_func hif, void *arg)
1478 {
1479 hash_iterate(bfd_key_hash, hif, arg);
1480 }
1481
1482 /*
1483 * Insert functions.
1484 *
1485 * Inserts session into hash and returns `true` on success, otherwise
1486 * `false`.
1487 */
1488 bool bfd_id_insert(struct bfd_session *bs)
1489 {
1490 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1491 }
1492
1493 bool bfd_key_insert(struct bfd_session *bs)
1494 {
1495 return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs);
1496 }
1497
1498 void bfd_initialize(void)
1499 {
1500 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1501 "BFD session discriminator hash");
1502 bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp,
1503 "BFD session hash");
1504 }
1505
1506 static void _bfd_free(struct hash_bucket *hb,
1507 void *arg __attribute__((__unused__)))
1508 {
1509 struct bfd_session *bs = hb->data;
1510
1511 bfd_session_free(bs);
1512 }
1513
1514 void bfd_shutdown(void)
1515 {
1516 /*
1517 * Close and free all BFD sessions.
1518 *
1519 * _bfd_free() will call bfd_session_free() which will take care
1520 * of removing the session from all hashes, so we just run an
1521 * assert() here to make sure it really happened.
1522 */
1523 bfd_id_iterate(_bfd_free, NULL);
1524 assert(bfd_key_hash->count == 0);
1525
1526 /* Now free the hashes themselves. */
1527 hash_free(bfd_id_hash);
1528 hash_free(bfd_key_hash);
1529 }
1530
1531 static int bfd_vrf_new(struct vrf *vrf)
1532 {
1533 log_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
1534 return 0;
1535 }
1536
1537 static int bfd_vrf_delete(struct vrf *vrf)
1538 {
1539 log_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
1540 return 0;
1541 }
1542
1543 static int bfd_vrf_enable(struct vrf *vrf)
1544 {
1545 struct bfd_vrf_global *bvrf;
1546
1547 /* a different name */
1548 if (!vrf->info) {
1549 bvrf = XCALLOC(MTYPE_BFDD_VRF, sizeof(struct bfd_vrf_global));
1550 bvrf->vrf = vrf;
1551 vrf->info = (void *)bvrf;
1552 } else
1553 bvrf = vrf->info;
1554 log_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
1555
1556 /* create sockets if needed */
1557 if (!bvrf->bg_shop)
1558 bvrf->bg_shop = bp_udp_shop(vrf->vrf_id);
1559 if (!bvrf->bg_mhop)
1560 bvrf->bg_mhop = bp_udp_mhop(vrf->vrf_id);
1561 if (!bvrf->bg_shop6)
1562 bvrf->bg_shop6 = bp_udp6_shop(vrf->vrf_id);
1563 if (!bvrf->bg_mhop6)
1564 bvrf->bg_mhop6 = bp_udp6_mhop(vrf->vrf_id);
1565 if (!bvrf->bg_echo)
1566 bvrf->bg_echo = bp_echo_socket(vrf->vrf_id);
1567 if (!bvrf->bg_echov6)
1568 bvrf->bg_echov6 = bp_echov6_socket(vrf->vrf_id);
1569
1570 /* Add descriptors to the event loop. */
1571 if (!bvrf->bg_ev[0])
1572 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop,
1573 &bvrf->bg_ev[0]);
1574 if (!bvrf->bg_ev[1])
1575 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop,
1576 &bvrf->bg_ev[1]);
1577 if (!bvrf->bg_ev[2])
1578 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop6,
1579 &bvrf->bg_ev[2]);
1580 if (!bvrf->bg_ev[3])
1581 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop6,
1582 &bvrf->bg_ev[3]);
1583 if (!bvrf->bg_ev[4])
1584 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echo,
1585 &bvrf->bg_ev[4]);
1586 if (!bvrf->bg_ev[5])
1587 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echov6,
1588 &bvrf->bg_ev[5]);
1589
1590 if (vrf->vrf_id != VRF_DEFAULT) {
1591 bfdd_zclient_register(vrf->vrf_id);
1592 bfdd_sessions_enable_vrf(vrf);
1593 }
1594 return 0;
1595 }
1596
1597 static int bfd_vrf_disable(struct vrf *vrf)
1598 {
1599 struct bfd_vrf_global *bvrf;
1600
1601 if (!vrf->info)
1602 return 0;
1603 bvrf = vrf->info;
1604
1605 if (vrf->vrf_id != VRF_DEFAULT) {
1606 bfdd_sessions_disable_vrf(vrf);
1607 bfdd_zclient_unregister(vrf->vrf_id);
1608 }
1609
1610 log_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id);
1611 /* Close all descriptors. */
1612 socket_close(&bvrf->bg_echo);
1613 socket_close(&bvrf->bg_shop);
1614 socket_close(&bvrf->bg_mhop);
1615 socket_close(&bvrf->bg_shop6);
1616 socket_close(&bvrf->bg_mhop6);
1617
1618 /* free context */
1619 XFREE(MTYPE_BFDD_VRF, bvrf);
1620 vrf->info = NULL;
1621
1622 return 0;
1623 }
1624
1625 void bfd_vrf_init(void)
1626 {
1627 vrf_init(bfd_vrf_new, bfd_vrf_enable, bfd_vrf_disable,
1628 bfd_vrf_delete, NULL);
1629 }
1630
1631 void bfd_vrf_terminate(void)
1632 {
1633 vrf_terminate();
1634 }
1635
1636 struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd)
1637 {
1638 struct vrf *vrf;
1639
1640 if (!vrf_is_backend_netns()) {
1641 vrf = vrf_lookup_by_id(VRF_DEFAULT);
1642 if (vrf)
1643 return (struct bfd_vrf_global *)vrf->info;
1644 return NULL;
1645 }
1646 if (!bfd)
1647 return NULL;
1648 if (!bfd->vrf)
1649 return NULL;
1650 return bfd->vrf->info;
1651 }