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
, int family
, const void *addr
);
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
, vrf_id_t vrf_id
,
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
, vrf_id_t vrf_id
);
76 static void bfdd_dest_deregister(struct stream
*msg
, vrf_id_t vrf_id
);
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
, int family
, const void *addr
)
132 stream_putc(msg
, family
);
136 stream_put(msg
, addr
, sizeof(struct in_addr
));
137 stream_putc(msg
, 32);
141 stream_put(msg
, addr
, sizeof(struct in6_addr
));
142 stream_putc(msg
, 128);
153 int ptm_bfd_notify(struct bfd_session
*bs
)
157 bs
->stats
.znotification
++;
161 * - header: command, vrf
162 * - l: interface index
177 * Commands: ZEBRA_BFD_DEST_REPLAY
179 * q(64), l(32), w(16), c(8)
184 /* TODO: VRF handling */
186 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, bs
->vrf
->vrf_id
);
188 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
190 /* This header will be handled by `zebra_ptm.c`. */
191 stream_putl(msg
, ZEBRA_INTERFACE_BFD_DEST_UPDATE
);
193 /* NOTE: Interface is a shortcut to avoid comparing source address. */
195 stream_putl(msg
, bs
->ifp
->ifindex
);
197 stream_putl(msg
, IFINDEX_INTERNAL
);
199 /* BFD destination prefix information. */
200 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.peer
);
203 switch (bs
->ses_state
) {
205 stream_putl(msg
, BFD_STATUS_UP
);
208 case PTM_BFD_ADM_DOWN
:
211 stream_putl(msg
, BFD_STATUS_DOWN
);
215 stream_putl(msg
, BFD_STATUS_UNKNOWN
);
219 /* BFD source prefix information. */
220 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.local
);
222 /* Write packet size. */
223 stream_putw_at(msg
, 0, stream_get_endp(msg
));
225 return zclient_send_message(zclient
);
228 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
)
232 STREAM_GETW(msg
, family
);
236 sa
->sa_sin
.sin_family
= family
;
237 STREAM_GET(&sa
->sa_sin
.sin_addr
, msg
,
238 sizeof(sa
->sa_sin
.sin_addr
));
239 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
240 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
241 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
245 sa
->sa_sin6
.sin6_family
= family
;
246 STREAM_GET(&sa
->sa_sin6
.sin6_addr
, msg
,
247 sizeof(sa
->sa_sin6
.sin6_addr
));
248 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
249 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
250 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
254 log_warning("ptm-read-address: invalid family: %d", family
);
259 memset(sa
, 0, sizeof(*sa
));
262 static int _ptm_msg_read(struct stream
*msg
, int command
, vrf_id_t vrf_id
,
263 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
)
266 uint8_t ttl
__attribute__((unused
));
270 * Register/Deregister/Update Message format:
271 * - header: Command, VRF
275 * - l: destination ipv4
277 * - 16 bytes: destination IPv6
278 * - command != ZEBRA_BFD_DEST_DEREGISTER
281 * - c: detect multiplier
286 * - l: destination ipv4
288 * - 16 bytes: destination IPv6
293 * - 16 bytes: ipv6 address
295 * - X bytes: interface name
297 * q(64), l(32), w(16), c(8)
300 /* Initialize parameters return values. */
301 memset(bpc
, 0, sizeof(*bpc
));
304 /* Find or allocate process context data. */
305 STREAM_GETL(msg
, pid
);
309 log_debug("ptm-read: failed to allocate memory");
313 /* Register/update peer information. */
314 _ptm_msg_read_address(msg
, &bpc
->bpc_peer
);
316 /* Determine IP type from peer destination. */
317 bpc
->bpc_ipv4
= (bpc
->bpc_peer
.sa_sin
.sin_family
== AF_INET
);
319 /* Get peer configuration. */
320 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
321 STREAM_GETL(msg
, bpc
->bpc_recvinterval
);
322 bpc
->bpc_has_recvinterval
=
323 (bpc
->bpc_recvinterval
!= BPC_DEF_RECEIVEINTERVAL
);
325 STREAM_GETL(msg
, bpc
->bpc_txinterval
);
326 bpc
->bpc_has_txinterval
=
327 (bpc
->bpc_txinterval
!= BPC_DEF_TRANSMITINTERVAL
);
329 STREAM_GETC(msg
, bpc
->bpc_detectmultiplier
);
330 bpc
->bpc_has_detectmultiplier
=
331 (bpc
->bpc_detectmultiplier
!= BPC_DEF_DETECTMULTIPLIER
);
334 /* Read (single|multi)hop and its options. */
335 STREAM_GETC(msg
, bpc
->bpc_mhop
);
337 /* Read multihop source address and TTL. */
338 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
339 STREAM_GETC(msg
, ttl
);
341 /* If target is IPv6, then we must obtain local address. */
342 if (bpc
->bpc_ipv4
== false)
343 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
346 * Read interface name and make sure it fits our data
347 * structure, otherwise fail.
349 STREAM_GETC(msg
, ifnamelen
);
350 if (ifnamelen
>= sizeof(bpc
->bpc_localif
)) {
351 log_error("ptm-read: interface name is too big");
355 bpc
->bpc_has_localif
= ifnamelen
> 0;
356 if (bpc
->bpc_has_localif
) {
357 STREAM_GET(bpc
->bpc_localif
, msg
, ifnamelen
);
358 bpc
->bpc_localif
[ifnamelen
] = 0;
361 if (vrf_id
!= VRF_DEFAULT
) {
364 vrf
= vrf_lookup_by_id(vrf_id
);
366 bpc
->bpc_has_vrfname
= true;
367 strlcpy(bpc
->bpc_vrfname
, vrf
->name
, sizeof(bpc
->bpc_vrfname
));
369 log_error("ptm-read: vrf id %u could not be identified", vrf_id
);
374 /* Sanity check: peer and local address must match IP types. */
375 if (bpc
->bpc_local
.sa_sin
.sin_family
!= 0
376 && (bpc
->bpc_local
.sa_sin
.sin_family
377 != bpc
->bpc_peer
.sa_sin
.sin_family
)) {
378 log_warning("ptm-read: peer family doesn't match local type");
388 static void bfdd_dest_register(struct stream
*msg
, vrf_id_t vrf_id
)
390 struct ptm_client
*pc
;
391 struct ptm_client_notification
*pcn
;
392 struct bfd_session
*bs
;
393 struct bfd_peer_cfg bpc
;
395 /* Read the client context and peer data. */
396 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_REGISTER
, vrf_id
, &bpc
, &pc
) == -1)
399 DEBUG_PRINTBPC(&bpc
);
401 /* Find or start new BFD session. */
402 bs
= bs_peer_find(&bpc
);
404 bs
= ptm_bfd_sess_new(&bpc
);
406 log_debug("ptm-add-dest: failed to create BFD session");
410 /* Don't try to change echo/shutdown state. */
411 bpc
.bpc_echo
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
413 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
416 /* Create client peer notification register. */
417 pcn
= pcn_new(pc
, bs
);
419 log_error("ptm-add-dest: failed to registrate notifications");
426 static void bfdd_dest_deregister(struct stream
*msg
, vrf_id_t vrf_id
)
428 struct ptm_client
*pc
;
429 struct ptm_client_notification
*pcn
;
430 struct bfd_session
*bs
;
431 struct bfd_peer_cfg bpc
;
433 /* Read the client context and peer data. */
434 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_DEREGISTER
, vrf_id
, &bpc
, &pc
) == -1)
437 DEBUG_PRINTBPC(&bpc
);
439 /* Find or start new BFD session. */
440 bs
= bs_peer_find(&bpc
);
442 log_debug("ptm-del-dest: failed to find BFD session");
446 /* Unregister client peer notification. */
447 pcn
= pcn_lookup(pc
, bs
);
450 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
))
452 ptm_bfd_sess_del(&bpc
);
456 * header: command, VRF
459 static void bfdd_client_register(struct stream
*msg
)
461 struct ptm_client
*pc
;
464 /* Find or allocate process context data. */
465 STREAM_GETL(msg
, pid
);
469 log_error("ptm-add-client: failed to register client: %u", pid
);
476 log_error("ptm-add-client: failed to register client");
480 * header: command, VRF
483 static void bfdd_client_deregister(struct stream
*msg
)
485 struct ptm_client
*pc
;
488 /* Find or allocate process context data. */
489 STREAM_GETL(msg
, pid
);
493 log_debug("ptm-del-client: failed to find client: %u", pid
);
502 log_error("ptm-del-client: failed to deregister client");
505 static int bfdd_replay(ZAPI_CALLBACK_ARGS
)
507 struct stream
*msg
= zclient
->ibuf
;
510 STREAM_GETL(msg
, rcmd
);
513 case ZEBRA_BFD_DEST_REGISTER
:
514 case ZEBRA_BFD_DEST_UPDATE
:
515 bfdd_dest_register(msg
, vrf_id
);
517 case ZEBRA_BFD_DEST_DEREGISTER
:
518 bfdd_dest_deregister(msg
, vrf_id
);
520 case ZEBRA_BFD_CLIENT_REGISTER
:
521 bfdd_client_register(msg
);
523 case ZEBRA_BFD_CLIENT_DEREGISTER
:
524 bfdd_client_deregister(msg
);
528 log_debug("ptm-replay: invalid message type %u", rcmd
);
535 log_error("ptm-replay: failed to find command");
539 static void bfdd_zebra_connected(struct zclient
*zc
)
541 struct stream
*msg
= zc
->obuf
;
543 /* Clean-up and free ptm clients data memory. */
547 * The replay is an empty message just to trigger client daemons
548 * configuration replay.
551 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
552 stream_putl(msg
, ZEBRA_BFD_DEST_REPLAY
);
553 stream_putw_at(msg
, 0, stream_get_endp(msg
));
555 /* Ask for interfaces information. */
556 zclient_create_header(msg
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
559 zclient_send_message(zclient
);
562 static void bfdd_sessions_enable_interface(struct interface
*ifp
)
564 struct bfd_session_observer
*bso
;
565 struct bfd_session
*bs
;
568 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
570 if (bso
->bso_isinterface
== false)
572 /* Interface name mismatch. */
573 if (strcmp(ifp
->name
, bs
->key
.ifname
))
575 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
578 if (bs
->key
.vrfname
[0] &&
579 strcmp(vrf
->name
, bs
->key
.vrfname
))
581 /* Skip enabled sessions. */
585 /* Try to enable it. */
586 bfd_session_enable(bs
);
590 static void bfdd_sessions_disable_interface(struct interface
*ifp
)
592 struct bfd_session_observer
*bso
;
593 struct bfd_session
*bs
;
595 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
596 if (bso
->bso_isinterface
== false)
599 /* Interface name mismatch. */
601 if (strcmp(ifp
->name
, bs
->key
.ifname
))
603 /* Skip disabled sessions. */
607 /* Try to enable it. */
608 bfd_session_disable(bs
);
610 TAILQ_INSERT_HEAD(&bglobal
.bg_obslist
, bso
, bso_entry
);
614 void bfdd_sessions_enable_vrf(struct vrf
*vrf
)
616 struct bfd_session_observer
*bso
;
617 struct bfd_session
*bs
;
619 /* it may affect configs without interfaces */
620 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
624 if (bs
->key
.vrfname
[0] &&
625 strcmp(vrf
->name
, bs
->key
.vrfname
))
627 /* need to update the vrf information on
628 * bs so that callbacks are handled
631 /* Skip enabled sessions. */
634 /* Try to enable it. */
635 bfd_session_enable(bs
);
639 void bfdd_sessions_disable_vrf(struct vrf
*vrf
)
641 struct bfd_session_observer
*bso
;
642 struct bfd_session
*bs
;
644 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
645 if (bso
->bso_isinterface
)
648 if (bs
->key
.vrfname
[0] &&
649 strcmp(vrf
->name
, bs
->key
.vrfname
))
651 /* Skip disabled sessions. */
655 /* Try to enable it. */
656 bfd_session_disable(bs
);
660 static int bfdd_interface_update(ZAPI_CALLBACK_ARGS
)
662 struct interface
*ifp
;
665 * `zebra_interface_add_read` will handle the interface creation
666 * on `lib/if.c`. We'll use that data structure instead of
669 if (cmd
== ZEBRA_INTERFACE_ADD
) {
670 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
674 bfdd_sessions_enable_interface(ifp
);
678 /* Update interface information. */
679 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
683 bfdd_sessions_disable_interface(ifp
);
685 if_set_index(ifp
, IFINDEX_INTERNAL
);
690 static int bfdd_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
692 struct interface
*ifp
;
695 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
, &nvrfid
);
699 if_update_to_new_vrf(ifp
, nvrfid
);
704 static void bfdd_sessions_enable_address(struct connected
*ifc
)
706 struct bfd_session_observer
*bso
;
707 struct bfd_session
*bs
;
708 struct prefix prefix
;
710 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
711 if (bso
->bso_isaddress
== false)
714 /* Skip enabled sessions. */
720 prefix
= bso
->bso_addr
;
721 prefix
.prefixlen
= ifc
->address
->prefixlen
;
722 if (prefix_cmp(&prefix
, ifc
->address
))
725 /* Try to enable it. */
726 bfd_session_enable(bs
);
730 static int bfdd_interface_address_update(ZAPI_CALLBACK_ARGS
)
732 struct connected
*ifc
;
734 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
738 bfdd_sessions_enable_address(ifc
);
743 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
)
745 zclient
= zclient_new(master
, &zclient_options_default
);
746 assert(zclient
!= NULL
);
747 zclient_init(zclient
, ZEBRA_ROUTE_BFD
, 0, bfdd_priv
);
750 * We'll receive all messages through replay, however it will
751 * contain a special field with the real command inside so we
752 * avoid having to create too many handlers.
754 zclient
->bfd_dest_replay
= bfdd_replay
;
756 /* Send replay request on zebra connect. */
757 zclient
->zebra_connected
= bfdd_zebra_connected
;
759 /* Learn interfaces from zebra instead of the OS. */
760 zclient
->interface_add
= bfdd_interface_update
;
761 zclient
->interface_delete
= bfdd_interface_update
;
763 /* Learn about interface VRF. */
764 zclient
->interface_vrf_update
= bfdd_interface_vrf_update
;
766 /* Learn about new addresses being registered. */
767 zclient
->interface_address_add
= bfdd_interface_address_update
;
768 zclient
->interface_address_delete
= bfdd_interface_address_update
;
771 void bfdd_zclient_register(vrf_id_t vrf_id
)
773 if (!zclient
|| zclient
->sock
< 0)
775 zclient_send_reg_requests(zclient
, vrf_id
);
778 void bfdd_zclient_unregister(vrf_id_t vrf_id
)
780 if (!zclient
|| zclient
->sock
< 0)
782 zclient_send_dereg_requests(zclient
, vrf_id
);
785 void bfdd_zclient_stop(void)
787 zclient_stop(zclient
);
789 /* Clean-up and free ptm clients data memory. */
797 static struct ptm_client
*pc_lookup(uint32_t pid
)
799 struct ptm_client
*pc
;
801 TAILQ_FOREACH (pc
, &pcqueue
, pc_entry
) {
802 if (pc
->pc_pid
!= pid
)
811 static struct ptm_client
*pc_new(uint32_t pid
)
813 struct ptm_client
*pc
;
815 /* Look up first, if not found create the client. */
820 /* Allocate the client data and save it. */
821 pc
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*pc
));
824 TAILQ_INSERT_HEAD(&pcqueue
, pc
, pc_entry
);
828 static void pc_free(struct ptm_client
*pc
)
830 struct ptm_client_notification
*pcn
;
835 TAILQ_REMOVE(&pcqueue
, pc
, pc_entry
);
837 while (!TAILQ_EMPTY(&pc
->pc_pcnqueue
)) {
838 pcn
= TAILQ_FIRST(&pc
->pc_pcnqueue
);
842 XFREE(MTYPE_BFDD_CONTROL
, pc
);
845 static void pc_free_all(void)
847 struct ptm_client
*pc
;
849 while (!TAILQ_EMPTY(&pcqueue
)) {
850 pc
= TAILQ_FIRST(&pcqueue
);
855 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
856 struct bfd_session
*bs
)
858 struct ptm_client_notification
*pcn
;
860 /* Try to find an existing pcn fist. */
861 pcn
= pcn_lookup(pc
, bs
);
865 /* Save the client notification data. */
866 pcn
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*pcn
));
868 TAILQ_INSERT_HEAD(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
876 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
877 struct bfd_session
*bs
)
879 struct ptm_client_notification
*pcn
;
881 TAILQ_FOREACH (pcn
, &pc
->pc_pcnqueue
, pcn_entry
) {
882 if (pcn
->pcn_bs
!= bs
)
891 static void pcn_free(struct ptm_client_notification
*pcn
)
893 struct ptm_client
*pc
;
894 struct bfd_session
*bs
;
899 /* Handle session de-registration. */
904 /* Handle ptm_client deregistration. */
907 TAILQ_REMOVE(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
909 XFREE(MTYPE_BFDD_NOTIFICATION
, pcn
);