]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_bfd.c
pimd: Prevent Null string %s issues
[mirror_frr.git] / pimd / pim_bfd.c
CommitLineData
ba4eb1bc
CS
1/*
2 * pim_bfd.c: PIM BFD handling routines
3 *
4 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * Chirag Shah
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23#include <zebra.h>
24
25#include "lib/json.h"
26#include "command.h"
27#include "vty.h"
28#include "zclient.h"
29
9b29ea95 30#include "pim_instance.h"
ba4eb1bc
CS
31#include "pim_cmd.h"
32#include "pim_vty.h"
33#include "pim_iface.h"
34#include "pim_bfd.h"
35#include "bfd.h"
36#include "pimd.h"
37#include "pim_zebra.h"
38
39/*
40 * pim_bfd_write_config - Write the interface BFD configuration.
41 */
d62a17ae 42void pim_bfd_write_config(struct vty *vty, struct interface *ifp)
ba4eb1bc 43{
d62a17ae 44 struct pim_interface *pim_ifp = ifp->info;
45 struct bfd_info *bfd_info = NULL;
46
47 if (!pim_ifp)
48 return;
49
c4efd0f4 50 bfd_info = pim_ifp->bfd_info;
d62a17ae 51 if (!bfd_info)
52 return;
53
a0841732 54#if HAVE_BFDD == 0
d62a17ae 55 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
56 vty_out(vty, " ip pim bfd %d %d %d\n", bfd_info->detect_mult,
57 bfd_info->required_min_rx, bfd_info->desired_min_tx);
58 else
a0841732 59#endif /* ! HAVE_BFDD */
d62a17ae 60 vty_out(vty, " ip pim bfd\n");
ba4eb1bc
CS
61}
62
63/*
64 * pim_bfd_show_info - Show BFD info structure
65 */
d62a17ae 66void pim_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
9f049418 67 bool use_json, int param_only)
ba4eb1bc 68{
d62a17ae 69 if (param_only)
70 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
71 json_obj);
72 else
73 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
74 json_obj);
ba4eb1bc
CS
75}
76
77/*
78 * pim_bfd_info_nbr_create - Create/update BFD information for a neighbor.
79 */
d62a17ae 80void pim_bfd_info_nbr_create(struct pim_interface *pim_ifp,
81 struct pim_neighbor *neigh)
ba4eb1bc 82{
d62a17ae 83 struct bfd_info *nbr_bfd_info = NULL;
ba4eb1bc 84
d62a17ae 85 /* Check if Pim Interface BFD is enabled */
86 if (!pim_ifp || !pim_ifp->bfd_info)
87 return;
ba4eb1bc 88
d62a17ae 89 if (!neigh->bfd_info)
90 neigh->bfd_info = bfd_info_create();
ba4eb1bc 91
d62a17ae 92 if (!neigh->bfd_info)
93 return;
ba4eb1bc 94
c4efd0f4 95 nbr_bfd_info = neigh->bfd_info;
d62a17ae 96 nbr_bfd_info->detect_mult = pim_ifp->bfd_info->detect_mult;
97 nbr_bfd_info->desired_min_tx = pim_ifp->bfd_info->desired_min_tx;
98 nbr_bfd_info->required_min_rx = pim_ifp->bfd_info->required_min_rx;
ba4eb1bc
CS
99}
100
101/*
102 * pim_bfd_info_free - Free BFD info structure
103 */
662ba9e6 104void pim_bfd_info_free(struct bfd_info **bfd_info)
ba4eb1bc 105{
662ba9e6 106 bfd_info_free(bfd_info);
ba4eb1bc
CS
107}
108
d62a17ae 109static void pim_bfd_reg_dereg_nbr(struct pim_neighbor *nbr, int command)
ba4eb1bc 110{
d62a17ae 111 struct pim_interface *pim_ifp = NULL;
112 struct bfd_info *bfd_info = NULL;
113 struct zclient *zclient = NULL;
9beff0bd 114 int cbit;
d62a17ae 115
116 zclient = pim_zebra_zclient_get();
117
118 if (!nbr)
119 return;
120 pim_ifp = nbr->interface->info;
c4efd0f4 121 bfd_info = pim_ifp->bfd_info;
d62a17ae 122 if (!bfd_info)
123 return;
124 if (PIM_DEBUG_PIM_TRACE) {
125 char str[INET_ADDRSTRLEN];
126 pim_inet4_dump("<bfd_nbr?>", nbr->source_addr, str,
127 sizeof(str));
15569c58 128 zlog_debug("%s Nbr %s %s with BFD", __func__, str,
d62a17ae 129 bfd_get_command_dbg_str(command));
130 }
9beff0bd
PG
131
132 cbit = CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON);
133
d62a17ae 134 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->source_addr, NULL,
9beff0bd
PG
135 nbr->interface->name, 0, 0, cbit,
136 command, 0, VRF_DEFAULT);
ba4eb1bc
CS
137}
138
139/*
140 * pim_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
141 * with a interface with BFD through
142 * zebra for starting/stopping the monitoring of
143 * the neighbor rechahability.
144 */
d62a17ae 145int pim_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
ba4eb1bc 146{
d62a17ae 147 struct pim_interface *pim_ifp = NULL;
148 struct listnode *node = NULL;
149 struct pim_neighbor *neigh = NULL;
150
151 pim_ifp = ifp->info;
152 if (!pim_ifp)
153 return -1;
154 if (!pim_ifp->bfd_info)
155 return -1;
156
157 for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_neighbor_list, node, neigh)) {
158 if (command != ZEBRA_BFD_DEST_DEREGISTER)
159 pim_bfd_info_nbr_create(pim_ifp, neigh);
160 else
662ba9e6 161 pim_bfd_info_free((struct bfd_info **)&neigh->bfd_info);
d62a17ae 162
163 pim_bfd_reg_dereg_nbr(neigh, command);
164 }
165
166 return 0;
ba4eb1bc
CS
167}
168
169/*
170 * pim_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
171 * neighbor state is changed to/from 2way.
172 */
d62a17ae 173void pim_bfd_trigger_event(struct pim_interface *pim_ifp,
174 struct pim_neighbor *nbr, uint8_t nbr_up)
ba4eb1bc 175{
d62a17ae 176 if (nbr_up) {
177 pim_bfd_info_nbr_create(pim_ifp, nbr);
178 pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_REGISTER);
179 } else {
662ba9e6 180 pim_bfd_info_free(&nbr->bfd_info);
d62a17ae 181 pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_DEREGISTER);
182 }
ba4eb1bc
CS
183}
184
185/*
186 * pim_bfd_if_param_set - Set the configured BFD paramter values for
187 * interface.
188 */
d7c0a89a
QY
189void pim_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
190 uint32_t min_tx, uint8_t detect_mult, int defaults)
ba4eb1bc 191{
d62a17ae 192 struct pim_interface *pim_ifp = ifp->info;
193 int command = 0;
194
195 if (!pim_ifp)
196 return;
c4efd0f4
DA
197 bfd_set_param(&(pim_ifp->bfd_info), min_rx, min_tx, detect_mult,
198 defaults, &command);
d62a17ae 199
200 if (pim_ifp->bfd_info) {
201 if (PIM_DEBUG_PIM_TRACE)
15569c58
DA
202 zlog_debug("%s: interface %s has bfd_info", __func__,
203 ifp->name);
d62a17ae 204 }
205 if (command)
206 pim_bfd_reg_dereg_all_nbr(ifp, command);
ba4eb1bc
CS
207}
208
209
210/*
211 * pim_bfd_interface_dest_update - Find the neighbor for which the BFD status
212 * has changed and bring down the neighbor
213 * connectivity if the BFD status changed to
214 * down.
215 */
121f9dee 216static int pim_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
ba4eb1bc 217{
d62a17ae 218 struct interface *ifp = NULL;
219 struct pim_interface *pim_ifp = NULL;
220 struct prefix p;
221 int status;
222 char msg[100];
223 int old_status;
224 struct bfd_info *bfd_info = NULL;
225 struct timeval tv;
226 struct listnode *neigh_node = NULL;
227 struct listnode *neigh_nextnode = NULL;
228 struct pim_neighbor *neigh = NULL;
229
9beff0bd
PG
230 ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status,
231 NULL, vrf_id);
d62a17ae 232
233 if ((ifp == NULL) || (p.family != AF_INET))
234 return 0;
235
236 pim_ifp = ifp->info;
237 if (!pim_ifp)
238 return 0;
239
240 if (!pim_ifp->bfd_info) {
241 if (PIM_DEBUG_PIM_TRACE)
242 zlog_debug("%s: pim interface %s BFD is disabled ",
5e81f5dd 243 __func__, ifp->name);
d62a17ae 244 return 0;
245 }
246
247 if (PIM_DEBUG_PIM_TRACE) {
248 char buf[PREFIX2STR_BUFFER];
249 prefix2str(&p, buf, sizeof(buf));
5e81f5dd
DS
250 zlog_debug("%s: interface %s bfd destination %s %s", __func__,
251 ifp->name, buf, bfd_get_status_str(status));
d62a17ae 252 }
253
254 for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list, neigh_node,
255 neigh_nextnode, neigh)) {
256 /* Check neigh address matches with BFD address */
257 if (neigh->source_addr.s_addr != p.u.prefix4.s_addr)
258 continue;
259
260 bfd_info = (struct bfd_info *)neigh->bfd_info;
261 if (bfd_info->status == status) {
262 if (PIM_DEBUG_PIM_TRACE) {
263 char str[INET_ADDRSTRLEN];
264 pim_inet4_dump("<nht_nbr?>", neigh->source_addr,
265 str, sizeof(str));
266 zlog_debug("%s: bfd status is same for nbr %s",
5e81f5dd 267 __func__, str);
d62a17ae 268 }
269 continue;
270 }
271 old_status = bfd_info->status;
7555dc61 272 BFD_SET_CLIENT_STATUS(bfd_info->status, status);
d62a17ae 273 monotime(&tv);
274 bfd_info->last_update = tv.tv_sec;
275
276 if (PIM_DEBUG_PIM_TRACE) {
5e81f5dd 277 zlog_debug("%s: status %s old_status %s", __func__,
d62a17ae 278 bfd_get_status_str(status),
279 bfd_get_status_str(old_status));
280 }
281 if ((status == BFD_STATUS_DOWN)
282 && (old_status == BFD_STATUS_UP)) {
283 snprintf(msg, sizeof(msg), "BFD Session Expired");
284 pim_neighbor_delete(ifp, neigh, msg);
285 }
286 }
287 return 0;
ba4eb1bc
CS
288}
289
290/*
291 * pim_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
292 * to zebra
293 */
121f9dee 294static int pim_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
ba4eb1bc 295{
d62a17ae 296 struct interface *ifp = NULL;
297 struct pim_interface *pim_ifp = NULL;
298 struct pim_neighbor *neigh = NULL;
d62a17ae 299 struct listnode *neigh_node;
300 struct listnode *neigh_nextnode;
df83d4e1 301 struct vrf *vrf = NULL;
d62a17ae 302
303 /* Send the client registration */
0945d5ed 304 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
d62a17ae 305
a2addae8 306 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
451fda4f 307 FOR_ALL_INTERFACES (vrf, ifp) {
df83d4e1 308 pim_ifp = ifp->info;
d62a17ae 309
df83d4e1
CS
310 if (!pim_ifp)
311 continue;
d62a17ae 312
df83d4e1 313 if (pim_ifp->pim_sock_fd < 0)
d62a17ae 314 continue;
df83d4e1
CS
315
316 for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list,
317 neigh_node, neigh_nextnode,
318 neigh)) {
319 if (!neigh->bfd_info)
320 continue;
321 if (PIM_DEBUG_PIM_TRACE) {
322 char str[INET_ADDRSTRLEN];
323
324 pim_inet4_dump("<bfd_nbr?>",
996c9314
LB
325 neigh->source_addr, str,
326 sizeof(str));
327 zlog_debug(
328 "%s: Replaying Pim Neigh %s to BFD vrf_id %u",
5e81f5dd 329 __func__, str, vrf->vrf_id);
df83d4e1
CS
330 }
331 pim_bfd_reg_dereg_nbr(neigh,
332 ZEBRA_BFD_DEST_UPDATE);
d62a17ae 333 }
d62a17ae 334 }
335 }
336 return 0;
ba4eb1bc
CS
337}
338
d62a17ae 339void pim_bfd_init(void)
ba4eb1bc 340{
d62a17ae 341 struct zclient *zclient = NULL;
ba4eb1bc 342
d62a17ae 343 zclient = pim_zebra_zclient_get();
ba4eb1bc 344
d62a17ae 345 bfd_gbl_init();
ba4eb1bc 346
d62a17ae 347 zclient->interface_bfd_dest_update = pim_bfd_interface_dest_update;
348 zclient->bfd_dest_replay = pim_bfd_nbr_replay;
ba4eb1bc 349}