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