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
)
94 addr
[0][0] = addr
[1][0] = addr
[2][0] = timers
[0][0] = timers
[1][0] =
97 snprintf(addr
[0], sizeof(addr
[0]), "peer:%s", satostr(&bpc
->bpc_peer
));
98 if (bpc
->bpc_local
.sa_sin
.sin_family
)
99 snprintf(addr
[1], sizeof(addr
[1]), " local:%s",
100 satostr(&bpc
->bpc_local
));
102 if (bpc
->bpc_has_localif
)
103 snprintf(addr
[2], sizeof(addr
[2]), " ifname:%s",
106 if (bpc
->bpc_has_vrfname
)
107 snprintf(addr
[2], sizeof(addr
[2]), " vrf:%s", bpc
->bpc_vrfname
);
109 if (bpc
->bpc_has_recvinterval
)
110 snprintf(timers
[0], sizeof(timers
[0]), " rx:%lu",
111 bpc
->bpc_recvinterval
);
113 if (bpc
->bpc_has_txinterval
)
114 snprintf(timers
[1], sizeof(timers
[1]), " tx:%lu",
115 bpc
->bpc_recvinterval
);
117 if (bpc
->bpc_has_detectmultiplier
)
118 snprintf(timers
[2], sizeof(timers
[2]), " detect-multiplier:%d",
119 bpc
->bpc_detectmultiplier
);
121 sprintf(cbit_str
, "CB %x", bpc
->bpc_cbit
);
123 log_debug("%s:%d: %s %s%s%s%s%s%s %s", func
, line
,
124 bpc
->bpc_mhop
? "multi-hop" : "single-hop", addr
[0], addr
[1],
125 addr
[2], timers
[0], timers
[1], timers
[2], cbit_str
);
128 #define DEBUG_PRINTBPC(bpc) debug_printbpc(__FILE__, __LINE__, (bpc))
130 #define DEBUG_PRINTBPC(bpc)
131 #endif /* BFD_DEBUG */
133 static int _ptm_msg_address(struct stream
*msg
, int family
, const void *addr
)
135 stream_putc(msg
, family
);
139 stream_put(msg
, addr
, sizeof(struct in_addr
));
140 stream_putc(msg
, 32);
144 stream_put(msg
, addr
, sizeof(struct in6_addr
));
145 stream_putc(msg
, 128);
156 int ptm_bfd_notify(struct bfd_session
*bs
)
160 bs
->stats
.znotification
++;
164 * - header: command, vrf
165 * - l: interface index
181 * Commands: ZEBRA_BFD_DEST_REPLAY
183 * q(64), l(32), w(16), c(8)
188 /* TODO: VRF handling */
190 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, bs
->vrf
->vrf_id
);
192 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
194 /* This header will be handled by `zebra_ptm.c`. */
195 stream_putl(msg
, ZEBRA_INTERFACE_BFD_DEST_UPDATE
);
197 /* NOTE: Interface is a shortcut to avoid comparing source address. */
199 stream_putl(msg
, bs
->ifp
->ifindex
);
201 stream_putl(msg
, IFINDEX_INTERNAL
);
203 /* BFD destination prefix information. */
204 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.peer
);
207 switch (bs
->ses_state
) {
209 stream_putl(msg
, BFD_STATUS_UP
);
212 case PTM_BFD_ADM_DOWN
:
215 stream_putl(msg
, BFD_STATUS_DOWN
);
219 stream_putl(msg
, BFD_STATUS_UNKNOWN
);
223 /* BFD source prefix information. */
224 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.local
);
226 stream_putc(msg
, bs
->remote_cbit
);
228 /* Write packet size. */
229 stream_putw_at(msg
, 0, stream_get_endp(msg
));
231 return zclient_send_message(zclient
);
234 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
)
238 STREAM_GETW(msg
, family
);
242 sa
->sa_sin
.sin_family
= family
;
243 STREAM_GET(&sa
->sa_sin
.sin_addr
, msg
,
244 sizeof(sa
->sa_sin
.sin_addr
));
245 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
246 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
247 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
251 sa
->sa_sin6
.sin6_family
= family
;
252 STREAM_GET(&sa
->sa_sin6
.sin6_addr
, msg
,
253 sizeof(sa
->sa_sin6
.sin6_addr
));
254 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
255 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
256 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
260 log_warning("ptm-read-address: invalid family: %d", family
);
265 memset(sa
, 0, sizeof(*sa
));
268 static int _ptm_msg_read(struct stream
*msg
, int command
, vrf_id_t vrf_id
,
269 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
)
272 uint8_t ttl
__attribute__((unused
));
276 * Register/Deregister/Update Message format:
277 * - header: Command, VRF
281 * - l: destination ipv4
283 * - 16 bytes: destination IPv6
284 * - command != ZEBRA_BFD_DEST_DEREGISTER
287 * - c: detect multiplier
292 * - l: destination ipv4
294 * - 16 bytes: destination IPv6
299 * - 16 bytes: ipv6 address
301 * - X bytes: interface name
304 * q(64), l(32), w(16), c(8)
307 /* Initialize parameters return values. */
308 memset(bpc
, 0, sizeof(*bpc
));
311 /* Find or allocate process context data. */
312 STREAM_GETL(msg
, pid
);
316 log_debug("ptm-read: failed to allocate memory");
320 /* Register/update peer information. */
321 _ptm_msg_read_address(msg
, &bpc
->bpc_peer
);
323 /* Determine IP type from peer destination. */
324 bpc
->bpc_ipv4
= (bpc
->bpc_peer
.sa_sin
.sin_family
== AF_INET
);
326 /* Get peer configuration. */
327 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
328 STREAM_GETL(msg
, bpc
->bpc_recvinterval
);
329 bpc
->bpc_has_recvinterval
=
330 (bpc
->bpc_recvinterval
!= BPC_DEF_RECEIVEINTERVAL
);
332 STREAM_GETL(msg
, bpc
->bpc_txinterval
);
333 bpc
->bpc_has_txinterval
=
334 (bpc
->bpc_txinterval
!= BPC_DEF_TRANSMITINTERVAL
);
336 STREAM_GETC(msg
, bpc
->bpc_detectmultiplier
);
337 bpc
->bpc_has_detectmultiplier
=
338 (bpc
->bpc_detectmultiplier
!= BPC_DEF_DETECTMULTIPLIER
);
341 /* Read (single|multi)hop and its options. */
342 STREAM_GETC(msg
, bpc
->bpc_mhop
);
344 /* Read multihop source address and TTL. */
345 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
346 STREAM_GETC(msg
, ttl
);
348 /* If target is IPv6, then we must obtain local address. */
349 if (bpc
->bpc_ipv4
== false)
350 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
353 * Read interface name and make sure it fits our data
354 * structure, otherwise fail.
356 STREAM_GETC(msg
, ifnamelen
);
357 if (ifnamelen
>= sizeof(bpc
->bpc_localif
)) {
358 log_error("ptm-read: interface name is too big");
362 bpc
->bpc_has_localif
= ifnamelen
> 0;
363 if (bpc
->bpc_has_localif
) {
364 STREAM_GET(bpc
->bpc_localif
, msg
, ifnamelen
);
365 bpc
->bpc_localif
[ifnamelen
] = 0;
368 if (vrf_id
!= VRF_DEFAULT
) {
371 vrf
= vrf_lookup_by_id(vrf_id
);
373 bpc
->bpc_has_vrfname
= true;
374 strlcpy(bpc
->bpc_vrfname
, vrf
->name
, sizeof(bpc
->bpc_vrfname
));
376 log_error("ptm-read: vrf id %u could not be identified", vrf_id
);
381 STREAM_GETC(msg
, bpc
->bpc_cbit
);
383 /* Sanity check: peer and local address must match IP types. */
384 if (bpc
->bpc_local
.sa_sin
.sin_family
!= 0
385 && (bpc
->bpc_local
.sa_sin
.sin_family
386 != bpc
->bpc_peer
.sa_sin
.sin_family
)) {
387 log_warning("ptm-read: peer family doesn't match local type");
397 static void bfdd_dest_register(struct stream
*msg
, vrf_id_t vrf_id
)
399 struct ptm_client
*pc
;
400 struct ptm_client_notification
*pcn
;
401 struct bfd_session
*bs
;
402 struct bfd_peer_cfg bpc
;
404 /* Read the client context and peer data. */
405 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_REGISTER
, vrf_id
, &bpc
, &pc
) == -1)
408 DEBUG_PRINTBPC(&bpc
);
410 /* Find or start new BFD session. */
411 bs
= bs_peer_find(&bpc
);
413 bs
= ptm_bfd_sess_new(&bpc
);
415 log_debug("ptm-add-dest: failed to create BFD session");
419 /* Don't try to change echo/shutdown state. */
420 bpc
.bpc_echo
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
422 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
425 /* Create client peer notification register. */
426 pcn
= pcn_new(pc
, bs
);
428 log_error("ptm-add-dest: failed to registrate notifications");
435 static void bfdd_dest_deregister(struct stream
*msg
, vrf_id_t vrf_id
)
437 struct ptm_client
*pc
;
438 struct ptm_client_notification
*pcn
;
439 struct bfd_session
*bs
;
440 struct bfd_peer_cfg bpc
;
442 /* Read the client context and peer data. */
443 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_DEREGISTER
, vrf_id
, &bpc
, &pc
) == -1)
446 DEBUG_PRINTBPC(&bpc
);
448 /* Find or start new BFD session. */
449 bs
= bs_peer_find(&bpc
);
451 log_debug("ptm-del-dest: failed to find BFD session");
455 /* Unregister client peer notification. */
456 pcn
= pcn_lookup(pc
, bs
);
459 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
))
461 ptm_bfd_sess_del(&bpc
);
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(ZAPI_CALLBACK_ARGS
)
516 struct stream
*msg
= zclient
->ibuf
;
519 STREAM_GETL(msg
, rcmd
);
522 case ZEBRA_BFD_DEST_REGISTER
:
523 case ZEBRA_BFD_DEST_UPDATE
:
524 bfdd_dest_register(msg
, vrf_id
);
526 case ZEBRA_BFD_DEST_DEREGISTER
:
527 bfdd_dest_deregister(msg
, vrf_id
);
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 /* Ask for interfaces information. */
565 zclient_create_header(msg
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
568 zclient_send_message(zclient
);
571 static void bfdd_sessions_enable_interface(struct interface
*ifp
)
573 struct bfd_session_observer
*bso
;
574 struct bfd_session
*bs
;
575 struct vrf
*vrf
= ifp
->vrf
;
580 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
582 if (bso
->bso_isinterface
== false)
584 /* Interface name mismatch. */
585 if (strcmp(ifp
->name
, bs
->key
.ifname
))
587 if (bs
->key
.vrfname
[0] &&
588 strcmp(vrf
->name
, bs
->key
.vrfname
))
590 /* Skip enabled sessions. */
594 /* Try to enable it. */
595 bfd_session_enable(bs
);
599 static void bfdd_sessions_disable_interface(struct interface
*ifp
)
601 struct bfd_session_observer
*bso
;
602 struct bfd_session
*bs
;
604 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
605 if (bso
->bso_isinterface
== false)
608 /* Interface name mismatch. */
610 if (strcmp(ifp
->name
, bs
->key
.ifname
))
612 /* Skip disabled sessions. */
616 /* Try to enable it. */
617 bfd_session_disable(bs
);
622 void bfdd_sessions_enable_vrf(struct vrf
*vrf
)
624 struct bfd_session_observer
*bso
;
625 struct bfd_session
*bs
;
627 /* it may affect configs without interfaces */
628 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
632 if (bs
->key
.vrfname
[0] &&
633 strcmp(vrf
->name
, bs
->key
.vrfname
))
635 /* need to update the vrf information on
636 * bs so that callbacks are handled
639 /* Skip enabled sessions. */
642 /* Try to enable it. */
643 bfd_session_enable(bs
);
647 void bfdd_sessions_disable_vrf(struct vrf
*vrf
)
649 struct bfd_session_observer
*bso
;
650 struct bfd_session
*bs
;
652 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
653 if (bso
->bso_isinterface
)
656 if (bs
->key
.vrfname
[0] &&
657 strcmp(vrf
->name
, bs
->key
.vrfname
))
659 /* Skip disabled sessions. */
663 /* Try to enable it. */
664 bfd_session_disable(bs
);
668 static int bfdd_interface_update(ZAPI_CALLBACK_ARGS
)
670 struct interface
*ifp
;
673 * `zebra_interface_add_read` will handle the interface creation
674 * on `lib/if.c`. We'll use that data structure instead of
677 if (cmd
== ZEBRA_INTERFACE_ADD
) {
678 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
682 bfdd_sessions_enable_interface(ifp
);
686 /* Update interface information. */
687 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
691 bfdd_sessions_disable_interface(ifp
);
693 if_set_index(ifp
, IFINDEX_INTERNAL
);
698 static int bfdd_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
700 struct interface
*ifp
;
704 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
, &nvrfid
);
707 nvrf
= vrf_lookup_by_id(nvrfid
);
708 if_update_to_new_vrf(ifp
, nvrf
);
713 static void bfdd_sessions_enable_address(struct connected
*ifc
)
715 struct bfd_session_observer
*bso
;
716 struct bfd_session
*bs
;
717 struct prefix prefix
;
719 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
720 if (bso
->bso_isaddress
== false)
723 /* Skip enabled sessions. */
729 prefix
= bso
->bso_addr
;
730 prefix
.prefixlen
= ifc
->address
->prefixlen
;
731 if (prefix_cmp(&prefix
, ifc
->address
))
734 /* Try to enable it. */
735 bfd_session_enable(bs
);
739 static int bfdd_interface_address_update(ZAPI_CALLBACK_ARGS
)
741 struct connected
*ifc
;
743 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
747 bfdd_sessions_enable_address(ifc
);
752 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
)
754 zclient
= zclient_new(master
, &zclient_options_default
);
755 assert(zclient
!= NULL
);
756 zclient_init(zclient
, ZEBRA_ROUTE_BFD
, 0, bfdd_priv
);
759 * We'll receive all messages through replay, however it will
760 * contain a special field with the real command inside so we
761 * avoid having to create too many handlers.
763 zclient
->bfd_dest_replay
= bfdd_replay
;
765 /* Send replay request on zebra connect. */
766 zclient
->zebra_connected
= bfdd_zebra_connected
;
768 /* Learn interfaces from zebra instead of the OS. */
769 zclient
->interface_add
= bfdd_interface_update
;
770 zclient
->interface_delete
= bfdd_interface_update
;
772 /* Learn about interface VRF. */
773 zclient
->interface_vrf_update
= bfdd_interface_vrf_update
;
775 /* Learn about new addresses being registered. */
776 zclient
->interface_address_add
= bfdd_interface_address_update
;
777 zclient
->interface_address_delete
= bfdd_interface_address_update
;
780 void bfdd_zclient_register(vrf_id_t vrf_id
)
782 if (!zclient
|| zclient
->sock
< 0)
784 zclient_send_reg_requests(zclient
, vrf_id
);
787 void bfdd_zclient_unregister(vrf_id_t vrf_id
)
789 if (!zclient
|| zclient
->sock
< 0)
791 zclient_send_dereg_requests(zclient
, vrf_id
);
794 void bfdd_zclient_stop(void)
796 zclient_stop(zclient
);
798 /* Clean-up and free ptm clients data memory. */
806 static struct ptm_client
*pc_lookup(uint32_t pid
)
808 struct ptm_client
*pc
;
810 TAILQ_FOREACH (pc
, &pcqueue
, pc_entry
) {
811 if (pc
->pc_pid
!= pid
)
820 static struct ptm_client
*pc_new(uint32_t pid
)
822 struct ptm_client
*pc
;
824 /* Look up first, if not found create the client. */
829 /* Allocate the client data and save it. */
830 pc
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*pc
));
833 TAILQ_INSERT_HEAD(&pcqueue
, pc
, pc_entry
);
837 static void pc_free(struct ptm_client
*pc
)
839 struct ptm_client_notification
*pcn
;
844 TAILQ_REMOVE(&pcqueue
, pc
, pc_entry
);
846 while (!TAILQ_EMPTY(&pc
->pc_pcnqueue
)) {
847 pcn
= TAILQ_FIRST(&pc
->pc_pcnqueue
);
851 XFREE(MTYPE_BFDD_CONTROL
, pc
);
854 static void pc_free_all(void)
856 struct ptm_client
*pc
;
858 while (!TAILQ_EMPTY(&pcqueue
)) {
859 pc
= TAILQ_FIRST(&pcqueue
);
864 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
865 struct bfd_session
*bs
)
867 struct ptm_client_notification
*pcn
;
869 /* Try to find an existing pcn fist. */
870 pcn
= pcn_lookup(pc
, bs
);
874 /* Save the client notification data. */
875 pcn
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*pcn
));
877 TAILQ_INSERT_HEAD(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
885 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
886 struct bfd_session
*bs
)
888 struct ptm_client_notification
*pcn
;
890 TAILQ_FOREACH (pcn
, &pc
->pc_pcnqueue
, pcn_entry
) {
891 if (pcn
->pcn_bs
!= bs
)
900 static void pcn_free(struct ptm_client_notification
*pcn
)
902 struct ptm_client
*pc
;
903 struct bfd_session
*bs
;
908 /* Handle session de-registration. */
913 /* Handle ptm_client deregistration. */
916 TAILQ_REMOVE(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
918 XFREE(MTYPE_BFDD_NOTIFICATION
, pcn
);