3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
5 * FRR 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
7 * Free Software Foundation; either version 2, or (at your option) any
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with FRR; see the file COPYING. If not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "lib/libfrr.h"
24 #include "lib/queue.h"
25 #include "lib/stream.h"
26 #include "lib/zclient.h"
35 struct ptm_client_notification
{
36 struct bfd_session
*pcn_bs
;
37 struct ptm_client
*pcn_pc
;
39 TAILQ_ENTRY(ptm_client_notification
) pcn_entry
;
41 TAILQ_HEAD(pcnqueue
, ptm_client_notification
);
45 struct pcnqueue pc_pcnqueue
;
47 TAILQ_ENTRY(ptm_client
) pc_entry
;
49 TAILQ_HEAD(pcqueue
, ptm_client
);
51 static struct pcqueue pcqueue
;
52 static struct zclient
*zclient
;
58 static int _ptm_msg_address(struct stream
*msg
, struct sockaddr_any
*sa
);
60 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
);
61 static int _ptm_msg_read(struct stream
*msg
, int command
,
62 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
);
64 static struct ptm_client
*pc_lookup(uint32_t pid
);
65 static struct ptm_client
*pc_new(uint32_t pid
);
66 static void pc_free(struct ptm_client
*pc
);
67 static void pc_free_all(void);
68 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
69 struct bfd_session
*bs
);
70 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
71 struct bfd_session
*bs
);
72 static void pcn_free(struct ptm_client_notification
*pcn
);
75 static void bfdd_dest_register(struct stream
*msg
);
76 static void bfdd_dest_deregister(struct stream
*msg
);
77 static void bfdd_client_register(struct stream
*msg
);
78 static void bfdd_client_deregister(struct stream
*msg
);
84 static void debug_printbpc(const char *func
, unsigned int line
,
85 struct bfd_peer_cfg
*bpc
);
87 static void debug_printbpc(const char *func
, unsigned int line
,
88 struct bfd_peer_cfg
*bpc
)
93 addr
[0][0] = addr
[1][0] = addr
[2][0] = timers
[0][0] = timers
[1][0] =
96 snprintf(addr
[0], sizeof(addr
[0]), "peer:%s", satostr(&bpc
->bpc_peer
));
97 if (bpc
->bpc_local
.sa_sin
.sin_family
)
98 snprintf(addr
[1], sizeof(addr
[1]), " local:%s",
99 satostr(&bpc
->bpc_local
));
101 if (bpc
->bpc_has_localif
)
102 snprintf(addr
[2], sizeof(addr
[2]), " ifname:%s",
105 if (bpc
->bpc_has_vrfname
)
106 snprintf(addr
[2], sizeof(addr
[2]), " vrf:%s", bpc
->bpc_vrfname
);
108 if (bpc
->bpc_has_recvinterval
)
109 snprintf(timers
[0], sizeof(timers
[0]), " rx:%lu",
110 bpc
->bpc_recvinterval
);
112 if (bpc
->bpc_has_txinterval
)
113 snprintf(timers
[1], sizeof(timers
[1]), " tx:%lu",
114 bpc
->bpc_recvinterval
);
116 if (bpc
->bpc_has_detectmultiplier
)
117 snprintf(timers
[2], sizeof(timers
[2]), " detect-multiplier:%d",
118 bpc
->bpc_detectmultiplier
);
120 log_debug("%s:%d: %s %s%s%s%s%s%s", func
, line
,
121 bpc
->bpc_mhop
? "multi-hop" : "single-hop", addr
[0], addr
[1],
122 addr
[2], timers
[0], timers
[1], timers
[2]);
125 #define DEBUG_PRINTBPC(bpc) debug_printbpc(__FILE__, __LINE__, (bpc))
127 #define DEBUG_PRINTBPC(bpc)
128 #endif /* BFD_DEBUG */
130 static int _ptm_msg_address(struct stream
*msg
, struct sockaddr_any
*sa
)
132 switch (sa
->sa_sin
.sin_family
) {
134 stream_putc(msg
, sa
->sa_sin
.sin_family
);
135 stream_put_in_addr(msg
, &sa
->sa_sin
.sin_addr
);
136 stream_putc(msg
, 32);
140 stream_putc(msg
, sa
->sa_sin6
.sin6_family
);
141 stream_put(msg
, &sa
->sa_sin6
.sin6_addr
,
142 sizeof(sa
->sa_sin6
.sin6_addr
));
143 stream_putc(msg
, 128);
153 int ptm_bfd_notify(struct bfd_session
*bs
)
156 struct sockaddr_any sac
;
158 bs
->stats
.znotification
++;
162 * - header: command, vrf
163 * - l: interface index
178 * Commands: ZEBRA_BFD_DEST_REPLAY
180 * q(64), l(32), w(16), c(8)
185 /* TODO: VRF handling */
186 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
188 /* This header will be handled by `zebra_ptm.c`. */
189 stream_putl(msg
, ZEBRA_INTERFACE_BFD_DEST_UPDATE
);
191 /* NOTE: Interface is a shortcut to avoid comparing source address. */
192 stream_putl(msg
, bs
->ifindex
);
194 /* BFD destination prefix information. */
195 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
))
196 _ptm_msg_address(msg
, &bs
->mhop
.peer
);
198 _ptm_msg_address(msg
, &bs
->shop
.peer
);
201 switch (bs
->ses_state
) {
203 stream_putl(msg
, BFD_STATUS_UP
);
206 case PTM_BFD_ADM_DOWN
:
209 stream_putl(msg
, BFD_STATUS_DOWN
);
213 stream_putl(msg
, BFD_STATUS_UNKNOWN
);
217 /* BFD source prefix information. */
218 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
219 _ptm_msg_address(msg
, &bs
->mhop
.local
);
221 if (bs
->local_address
.sa_sin
.sin_family
)
222 _ptm_msg_address(msg
, &bs
->local_address
);
223 else if (bs
->local_address
.sa_sin
.sin_family
)
224 _ptm_msg_address(msg
, &bs
->local_ip
);
227 switch (sac
.sa_sin
.sin_family
) {
229 memset(&sac
.sa_sin
.sin_addr
, 0,
230 sizeof(sac
.sa_sin
.sin_family
));
233 memset(&sac
.sa_sin6
.sin6_addr
, 0,
234 sizeof(sac
.sa_sin6
.sin6_family
));
242 /* No local address found yet, so send zeroes. */
243 _ptm_msg_address(msg
, &sac
);
247 /* Write packet size. */
248 stream_putw_at(msg
, 0, stream_get_endp(msg
));
250 return zclient_send_message(zclient
);
253 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
)
257 STREAM_GETW(msg
, family
);
261 sa
->sa_sin
.sin_family
= family
;
262 STREAM_GET(&sa
->sa_sin
.sin_addr
, msg
,
263 sizeof(sa
->sa_sin
.sin_addr
));
264 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
265 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
266 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
270 sa
->sa_sin6
.sin6_family
= family
;
271 STREAM_GET(&sa
->sa_sin6
.sin6_addr
, msg
,
272 sizeof(sa
->sa_sin6
.sin6_addr
));
273 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
274 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
275 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
279 log_warning("ptm-read-address: invalid family: %d", family
);
284 memset(sa
, 0, sizeof(*sa
));
287 static int _ptm_msg_read(struct stream
*msg
, int command
,
288 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
)
291 uint8_t ttl
__attribute__((unused
));
295 * Register/Deregister/Update Message format:
296 * - header: Command, VRF
300 * - l: destination ipv4
302 * - 16 bytes: destination IPv6
303 * - command != ZEBRA_BFD_DEST_DEREGISTER
306 * - c: detect multiplier
311 * - l: destination ipv4
313 * - 16 bytes: destination IPv6
318 * - 16 bytes: ipv6 address
320 * - X bytes: interface name
322 * q(64), l(32), w(16), c(8)
325 /* Initialize parameters return values. */
326 memset(bpc
, 0, sizeof(*bpc
));
329 /* Find or allocate process context data. */
330 STREAM_GETL(msg
, pid
);
334 log_debug("ptm-read: failed to allocate memory");
338 /* Register/update peer information. */
339 _ptm_msg_read_address(msg
, &bpc
->bpc_peer
);
341 /* Determine IP type from peer destination. */
342 bpc
->bpc_ipv4
= (bpc
->bpc_peer
.sa_sin
.sin_family
== AF_INET
);
344 /* Get peer configuration. */
345 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
346 STREAM_GETL(msg
, bpc
->bpc_recvinterval
);
347 bpc
->bpc_has_recvinterval
=
348 (bpc
->bpc_recvinterval
!= BPC_DEF_RECEIVEINTERVAL
);
350 STREAM_GETL(msg
, bpc
->bpc_txinterval
);
351 bpc
->bpc_has_txinterval
=
352 (bpc
->bpc_txinterval
!= BPC_DEF_TRANSMITINTERVAL
);
354 STREAM_GETC(msg
, bpc
->bpc_detectmultiplier
);
355 bpc
->bpc_has_detectmultiplier
=
356 (bpc
->bpc_detectmultiplier
!= BPC_DEF_DETECTMULTIPLIER
);
359 /* Read (single|multi)hop and its options. */
360 STREAM_GETC(msg
, bpc
->bpc_mhop
);
362 /* Read multihop source address and TTL. */
363 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
364 STREAM_GETC(msg
, ttl
);
366 /* If target is IPv6, then we must obtain local address. */
367 if (bpc
->bpc_ipv4
== false)
368 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
371 * Read interface name and make sure it fits our data
372 * structure, otherwise fail.
374 STREAM_GETC(msg
, ifnamelen
);
375 if (ifnamelen
>= sizeof(bpc
->bpc_localif
)) {
376 log_error("ptm-read: interface name is too big");
380 bpc
->bpc_has_localif
= ifnamelen
> 0;
381 if (bpc
->bpc_has_localif
) {
382 STREAM_GET(bpc
->bpc_localif
, msg
, ifnamelen
);
383 bpc
->bpc_localif
[ifnamelen
] = 0;
387 /* Sanity check: peer and local address must match IP types. */
388 if (bpc
->bpc_local
.sa_sin
.sin_family
!= 0
389 && (bpc
->bpc_local
.sa_sin
.sin_family
390 != bpc
->bpc_peer
.sa_sin
.sin_family
)) {
391 log_warning("ptm-read: peer family doesn't match local type");
401 static void bfdd_dest_register(struct stream
*msg
)
403 struct ptm_client
*pc
;
404 struct ptm_client_notification
*pcn
;
405 struct bfd_session
*bs
;
406 struct bfd_peer_cfg bpc
;
408 /* Read the client context and peer data. */
409 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_REGISTER
, &bpc
, &pc
) == -1)
412 DEBUG_PRINTBPC(&bpc
);
414 /* Find or start new BFD session. */
415 bs
= bs_peer_find(&bpc
);
417 bs
= ptm_bfd_sess_new(&bpc
);
419 log_debug("ptm-add-dest: failed to create BFD session");
423 /* Don't try to change echo/shutdown state. */
424 bpc
.bpc_echo
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
426 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
429 /* Create client peer notification register. */
430 pcn
= pcn_new(pc
, bs
);
432 log_error("ptm-add-dest: failed to registrate notifications");
439 static void bfdd_dest_deregister(struct stream
*msg
)
441 struct ptm_client
*pc
;
442 struct ptm_client_notification
*pcn
;
443 struct bfd_session
*bs
;
444 struct bfd_peer_cfg bpc
;
446 /* Read the client context and peer data. */
447 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_DEREGISTER
, &bpc
, &pc
) == -1)
450 DEBUG_PRINTBPC(&bpc
);
452 /* Find or start new BFD session. */
453 bs
= bs_peer_find(&bpc
);
455 log_debug("ptm-del-dest: failed to find BFD session");
459 /* Unregister client peer notification. */
460 pcn
= pcn_lookup(pc
, bs
);
465 * header: command, VRF
468 static void bfdd_client_register(struct stream
*msg
)
470 struct ptm_client
*pc
;
473 /* Find or allocate process context data. */
474 STREAM_GETL(msg
, pid
);
478 log_error("ptm-add-client: failed to register client: %u", pid
);
485 log_error("ptm-add-client: failed to register client");
489 * header: command, VRF
492 static void bfdd_client_deregister(struct stream
*msg
)
494 struct ptm_client
*pc
;
497 /* Find or allocate process context data. */
498 STREAM_GETL(msg
, pid
);
502 log_debug("ptm-del-client: failed to find client: %u", pid
);
511 log_error("ptm-del-client: failed to deregister client");
514 static int bfdd_replay(int cmd
, struct zclient
*zc
, uint16_t len
, vrf_id_t vid
)
516 struct stream
*msg
= zc
->ibuf
;
519 STREAM_GETL(msg
, rcmd
);
522 case ZEBRA_BFD_DEST_REGISTER
:
523 case ZEBRA_BFD_DEST_UPDATE
:
524 bfdd_dest_register(msg
);
526 case ZEBRA_BFD_DEST_DEREGISTER
:
527 bfdd_dest_deregister(msg
);
529 case ZEBRA_BFD_CLIENT_REGISTER
:
530 bfdd_client_register(msg
);
532 case ZEBRA_BFD_CLIENT_DEREGISTER
:
533 bfdd_client_deregister(msg
);
537 log_debug("ptm-replay: invalid message type %u", rcmd
);
544 log_error("ptm-replay: failed to find command");
548 static void bfdd_zebra_connected(struct zclient
*zc
)
550 struct stream
*msg
= zc
->obuf
;
552 /* Clean-up and free ptm clients data memory. */
556 * The replay is an empty message just to trigger client daemons
557 * configuration replay.
560 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
561 stream_putl(msg
, ZEBRA_BFD_DEST_REPLAY
);
562 stream_putw_at(msg
, 0, stream_get_endp(msg
));
564 zclient_send_message(zclient
);
567 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
)
569 zclient
= zclient_new_notify(master
, &zclient_options_default
);
570 assert(zclient
!= NULL
);
571 zclient_init(zclient
, ZEBRA_ROUTE_BFD
, 0, bfdd_priv
);
574 * We'll receive all messages through replay, however it will
575 * contain a special field with the real command inside so we
576 * avoid having to create too many handlers.
578 zclient
->bfd_dest_replay
= bfdd_replay
;
580 /* Send replay request on zebra connect. */
581 zclient
->zebra_connected
= bfdd_zebra_connected
;
584 void bfdd_zclient_stop(void)
586 zclient_stop(zclient
);
588 /* Clean-up and free ptm clients data memory. */
596 static struct ptm_client
*pc_lookup(uint32_t pid
)
598 struct ptm_client
*pc
;
600 TAILQ_FOREACH (pc
, &pcqueue
, pc_entry
) {
601 if (pc
->pc_pid
!= pid
)
610 static struct ptm_client
*pc_new(uint32_t pid
)
612 struct ptm_client
*pc
;
614 /* Look up first, if not found create the client. */
619 /* Allocate the client data and save it. */
620 pc
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*pc
));
625 TAILQ_INSERT_HEAD(&pcqueue
, pc
, pc_entry
);
629 static void pc_free(struct ptm_client
*pc
)
631 struct ptm_client_notification
*pcn
;
636 TAILQ_REMOVE(&pcqueue
, pc
, pc_entry
);
638 while (!TAILQ_EMPTY(&pc
->pc_pcnqueue
)) {
639 pcn
= TAILQ_FIRST(&pc
->pc_pcnqueue
);
643 XFREE(MTYPE_BFDD_CONTROL
, pc
);
646 static void pc_free_all(void)
648 struct ptm_client
*pc
;
650 while (!TAILQ_EMPTY(&pcqueue
)) {
651 pc
= TAILQ_FIRST(&pcqueue
);
656 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
657 struct bfd_session
*bs
)
659 struct ptm_client_notification
*pcn
;
661 /* Try to find an existing pcn fist. */
662 pcn
= pcn_lookup(pc
, bs
);
666 /* Save the client notification data. */
667 pcn
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*pcn
));
671 TAILQ_INSERT_HEAD(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
679 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
680 struct bfd_session
*bs
)
682 struct ptm_client_notification
*pcn
;
684 TAILQ_FOREACH (pcn
, &pc
->pc_pcnqueue
, pcn_entry
) {
685 if (pcn
->pcn_bs
!= bs
)
694 static void pcn_free(struct ptm_client_notification
*pcn
)
696 struct ptm_client
*pc
;
697 struct bfd_session
*bs
;
702 /* Handle session de-registration. */
707 /* Handle ptm_client deregistration. */
710 TAILQ_REMOVE(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
712 XFREE(MTYPE_BFDD_NOTIFICATION
, pcn
);