]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ptm.c
Merge pull request #12920 from sri-mohan1/sri-mohan-ldp
[mirror_frr.git] / zebra / zebra_ptm.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel routing table updates using netlink over GNU/Linux system.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include <sys/un.h> /* for sockaddr_un */
9 #include <net/if.h>
10
11 #include "bfd.h"
12 #include "buffer.h"
13 #include "command.h"
14 #include "if.h"
15 #include "network.h"
16 #include "ptm_lib.h"
17 #include "rib.h"
18 #include "stream.h"
19 #include "vrf.h"
20 #include "vty.h"
21 #include "lib_errors.h"
22
23 #include "zebra/debug.h"
24 #include "zebra/interface.h"
25 #include "zebra/zebra_errors.h"
26 #include "zebra/zebra_ptm.h"
27 #include "zebra/zebra_ptm_redistribute.h"
28 #include "zebra/zebra_router.h"
29 #include "zebra_vrf.h"
30
31 /*
32 * Choose the BFD implementation that we'll use.
33 *
34 * There are two implementations:
35 * - PTM BFD: which uses an external daemon;
36 * - bfdd: FRR's own BFD daemon;
37 */
38 #if HAVE_BFDD == 0
39
40 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
41 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
42
43 #define PTM_MSG_LEN 4
44 #define PTM_HEADER_LEN 37
45
46 const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status";
47 const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess";
48 const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess";
49 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client";
50 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client";
51
52 const char ZEBRA_PTM_CMD_STR[] = "cmd";
53 const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status";
54 const char ZEBRA_PTM_PORT_STR[] = "port";
55 const char ZEBRA_PTM_CBL_STR[] = "cbl status";
56 const char ZEBRA_PTM_PASS_STR[] = "pass";
57 const char ZEBRA_PTM_FAIL_STR[] = "fail";
58 const char ZEBRA_PTM_BFDSTATUS_STR[] = "state";
59 const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up";
60 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down";
61 const char ZEBRA_PTM_BFDDEST_STR[] = "peer";
62 const char ZEBRA_PTM_BFDSRC_STR[] = "local";
63 const char ZEBRA_PTM_BFDVRF_STR[] = "vrf";
64 const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A";
65 const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A";
66 const char ZEBRA_PTM_INVALID_VRF[] = "N/A";
67
68 const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr";
69 const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr";
70 const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx";
71 const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx";
72 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult";
73 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop";
74 const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client";
75 const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid";
76 const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName";
77 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt";
78 const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent";
79 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName";
80 const char ZEBRA_PTM_BFD_CBIT_FIELD[] = "bfdcbit";
81
82 static ptm_lib_handle_t *ptm_hdl;
83
84 struct zebra_ptm_cb ptm_cb;
85
86 static int zebra_ptm_socket_init(void);
87 void zebra_ptm_sock_read(struct thread *thread);
88 static void zebra_ptm_install_commands(void);
89 static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt);
90 void zebra_bfd_peer_replay_req(void);
91 void zebra_ptm_send_status_req(void);
92 void zebra_ptm_reset_status(int ptm_disable);
93 static int zebra_ptm_bfd_client_deregister(struct zserv *client);
94
95 const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket";
96
97 void zebra_ptm_init(void)
98 {
99 char buf[64];
100
101 memset(&ptm_cb, 0, sizeof(ptm_cb));
102
103 ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
104 if (!ptm_cb.out_data) {
105 zlog_debug("%s: Allocation of send data failed", __func__);
106 return;
107 }
108
109 ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
110 if (!ptm_cb.in_data) {
111 zlog_debug("%s: Allocation of recv data failed", __func__);
112 free(ptm_cb.out_data);
113 return;
114 }
115
116 ptm_cb.pid = getpid();
117 zebra_ptm_install_commands();
118
119 snprintf(buf, sizeof(buf), "%s", FRR_PTM_NAME);
120 ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb,
121 zebra_ptm_handle_msg_cb);
122 ptm_cb.wb = buffer_new(0);
123
124 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
125
126 ptm_cb.ptm_sock = -1;
127
128 hook_register(zserv_client_close, zebra_ptm_bfd_client_deregister);
129 }
130
131 void zebra_ptm_finish(void)
132 {
133 buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock);
134
135 free(ptm_hdl);
136
137 if (ptm_cb.out_data)
138 free(ptm_cb.out_data);
139
140 if (ptm_cb.in_data)
141 free(ptm_cb.in_data);
142
143 /* Cancel events. */
144 THREAD_OFF(ptm_cb.t_read);
145 THREAD_OFF(ptm_cb.t_write);
146 THREAD_OFF(ptm_cb.t_timer);
147
148 if (ptm_cb.wb)
149 buffer_free(ptm_cb.wb);
150
151 if (ptm_cb.ptm_sock >= 0)
152 close(ptm_cb.ptm_sock);
153 }
154
155 static void zebra_ptm_flush_messages(struct thread *thread)
156 {
157 ptm_cb.t_write = NULL;
158
159 if (ptm_cb.ptm_sock == -1)
160 return;
161
162 errno = 0;
163
164 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
165 case BUFFER_ERROR:
166 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
167 safe_strerror(errno));
168 close(ptm_cb.ptm_sock);
169 ptm_cb.ptm_sock = -1;
170 zebra_ptm_reset_status(0);
171 ptm_cb.t_timer = NULL;
172 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
173 ptm_cb.reconnect_time, &ptm_cb.t_timer);
174 return;
175 case BUFFER_PENDING:
176 ptm_cb.t_write = NULL;
177 thread_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
178 ptm_cb.ptm_sock, &ptm_cb.t_write);
179 break;
180 case BUFFER_EMPTY:
181 break;
182 }
183 }
184
185 static int zebra_ptm_send_message(char *data, int size)
186 {
187 errno = 0;
188 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
189 case BUFFER_ERROR:
190 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
191 safe_strerror(errno));
192 close(ptm_cb.ptm_sock);
193 ptm_cb.ptm_sock = -1;
194 zebra_ptm_reset_status(0);
195 ptm_cb.t_timer = NULL;
196 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
197 ptm_cb.reconnect_time, &ptm_cb.t_timer);
198 return -1;
199 case BUFFER_EMPTY:
200 THREAD_OFF(ptm_cb.t_write);
201 break;
202 case BUFFER_PENDING:
203 thread_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
204 ptm_cb.ptm_sock, &ptm_cb.t_write);
205 break;
206 }
207
208 return 0;
209 }
210
211 void zebra_ptm_connect(struct thread *t)
212 {
213 int init = 0;
214
215 if (ptm_cb.ptm_sock == -1) {
216 zebra_ptm_socket_init();
217 init = 1;
218 }
219
220 if (ptm_cb.ptm_sock != -1) {
221 if (init) {
222 ptm_cb.t_read = NULL;
223 thread_add_read(zrouter.master, zebra_ptm_sock_read,
224 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
225 zebra_bfd_peer_replay_req();
226 }
227 zebra_ptm_send_status_req();
228 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
229 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
230 ptm_cb.reconnect_time *= 2;
231 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
232 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
233
234 ptm_cb.t_timer = NULL;
235 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
236 ptm_cb.reconnect_time, &ptm_cb.t_timer);
237 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
238 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
239 }
240 }
241
242 DEFUN (zebra_ptm_enable,
243 zebra_ptm_enable_cmd,
244 "ptm-enable",
245 "Enable neighbor check with specified topology\n")
246 {
247 struct vrf *vrf;
248 struct interface *ifp;
249 struct zebra_if *if_data;
250
251 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
252
253 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
254 FOR_ALL_INTERFACES (vrf, ifp)
255 if (!ifp->ptm_enable) {
256 if_data = (struct zebra_if *)ifp->info;
257 if (if_data
258 && (if_data->ptm_enable
259 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
260 ifp->ptm_enable =
261 ZEBRA_IF_PTM_ENABLE_ON;
262 }
263 /* Assign a default unknown status */
264 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
265 }
266
267 zebra_ptm_connect(NULL);
268
269 return CMD_SUCCESS;
270 }
271
272 DEFUN (no_zebra_ptm_enable,
273 no_zebra_ptm_enable_cmd,
274 "no ptm-enable",
275 NO_STR
276 "Enable neighbor check with specified topology\n")
277 {
278 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
279 zebra_ptm_reset_status(1);
280 return CMD_SUCCESS;
281 }
282
283 DEFUN (zebra_ptm_enable_if,
284 zebra_ptm_enable_if_cmd,
285 "ptm-enable",
286 "Enable neighbor check with specified topology\n")
287 {
288 VTY_DECLVAR_CONTEXT(interface, ifp);
289 struct zebra_if *if_data;
290 int old_ptm_enable;
291 int send_linkdown = 0;
292
293 if_data = ifp->info;
294 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
295
296 if (ifp->ifindex == IFINDEX_INTERNAL) {
297 return CMD_SUCCESS;
298 }
299
300 old_ptm_enable = ifp->ptm_enable;
301 ifp->ptm_enable = ptm_cb.ptm_enable;
302
303 if (if_is_no_ptm_operative(ifp))
304 send_linkdown = 1;
305
306 if (!old_ptm_enable && ptm_cb.ptm_enable) {
307 if (!if_is_operative(ifp) && send_linkdown) {
308 if (IS_ZEBRA_DEBUG_EVENT)
309 zlog_debug("%s: Bringing down interface %s",
310 __func__, ifp->name);
311 if_down(ifp);
312 }
313 }
314
315 return CMD_SUCCESS;
316 }
317
318 DEFUN (no_zebra_ptm_enable_if,
319 no_zebra_ptm_enable_if_cmd,
320 "no ptm-enable",
321 NO_STR
322 "Enable neighbor check with specified topology\n")
323 {
324 VTY_DECLVAR_CONTEXT(interface, ifp);
325 int send_linkup = 0;
326 struct zebra_if *if_data;
327
328 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
329 if (!if_is_operative(ifp))
330 send_linkup = 1;
331
332 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
333 if (if_is_no_ptm_operative(ifp) && send_linkup) {
334 if (IS_ZEBRA_DEBUG_EVENT)
335 zlog_debug("%s: Bringing up interface %s",
336 __func__, ifp->name);
337 if_up(ifp, true);
338 }
339 }
340
341 if_data = ifp->info;
342 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
343
344 return CMD_SUCCESS;
345 }
346
347
348 void zebra_ptm_write(struct vty *vty)
349 {
350 if (ptm_cb.ptm_enable)
351 vty_out(vty, "ptm-enable\n");
352
353 return;
354 }
355
356 static int zebra_ptm_socket_init(void)
357 {
358 int ret;
359 int sock;
360 struct sockaddr_un addr;
361
362 ptm_cb.ptm_sock = -1;
363
364 sock = socket(PF_UNIX, SOCK_STREAM, 0);
365 if (sock < 0)
366 return -1;
367 if (set_nonblocking(sock) < 0) {
368 if (IS_ZEBRA_DEBUG_EVENT)
369 zlog_debug("%s: Unable to set socket non blocking[%s]",
370 __func__, safe_strerror(errno));
371 close(sock);
372 return -1;
373 }
374
375 /* Make server socket. */
376 memset(&addr, 0, sizeof(addr));
377 addr.sun_family = AF_UNIX;
378 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
379 sizeof(ZEBRA_PTM_SOCK_NAME));
380
381 ret = connect(sock, (struct sockaddr *)&addr,
382 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
383 - 1);
384 if (ret < 0) {
385 if (IS_ZEBRA_DEBUG_EVENT)
386 zlog_debug("%s: Unable to connect to socket %s [%s]",
387 __func__, ZEBRA_PTM_SOCK_NAME,
388 safe_strerror(errno));
389 close(sock);
390 return -1;
391 }
392 ptm_cb.ptm_sock = sock;
393 return sock;
394 }
395
396 static void zebra_ptm_install_commands(void)
397 {
398 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
399 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
400 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
401 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
402 }
403
404 /* BFD session goes down, send message to the protocols. */
405 static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
406 struct prefix *sp, int status,
407 vrf_id_t vrf_id)
408 {
409 if (IS_ZEBRA_DEBUG_EVENT) {
410 char buf[2][INET6_ADDRSTRLEN];
411
412 if (ifp) {
413 zlog_debug(
414 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event",
415 inet_ntop(dp->family, &dp->u.prefix, buf[0],
416 INET6_ADDRSTRLEN),
417 dp->prefixlen, ifp->name,
418 bfd_get_status_str(status));
419 } else {
420 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
421
422 zlog_debug(
423 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
424 inet_ntop(dp->family, &dp->u.prefix, buf[0],
425 INET6_ADDRSTRLEN),
426 dp->prefixlen,
427 inet_ntop(sp->family, &sp->u.prefix, buf[1],
428 INET6_ADDRSTRLEN),
429 sp->prefixlen, VRF_LOGNAME(vrf), vrf_id,
430 bfd_get_status_str(status));
431 }
432 }
433
434 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
435 }
436
437 static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
438 struct interface *ifp)
439 {
440 char bfdst_str[32];
441 char dest_str[64];
442 char src_str[64];
443 char vrf_str[64];
444 struct prefix dest_prefix;
445 struct prefix src_prefix;
446 vrf_id_t vrf_id;
447
448 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
449
450 if (bfdst_str[0] == '\0') {
451 return -1;
452 }
453
454 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
455
456 if (dest_str[0] == '\0') {
457 zlog_debug("%s: Key %s not found in PTM msg", __func__,
458 ZEBRA_PTM_BFDDEST_STR);
459 return -1;
460 }
461
462 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
463
464 if (src_str[0] == '\0') {
465 zlog_debug("%s: Key %s not found in PTM msg", __func__,
466 ZEBRA_PTM_BFDSRC_STR);
467 return -1;
468 }
469
470 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
471
472 if (vrf_str[0] == '\0') {
473 zlog_debug("%s: Key %s not found in PTM msg", __func__,
474 ZEBRA_PTM_BFDVRF_STR);
475 return -1;
476 }
477
478 if (IS_ZEBRA_DEBUG_EVENT)
479 zlog_debug(
480 "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
481 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
482 dest_str, src_str);
483
484 if (str2prefix(dest_str, &dest_prefix) == 0) {
485 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
486 "%s: Peer addr %s not found", __func__, dest_str);
487 return -1;
488 }
489
490 memset(&src_prefix, 0, sizeof(src_prefix));
491 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
492 if (str2prefix(src_str, &src_prefix) == 0) {
493 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
494 "%s: Local addr %s not found", __func__,
495 src_str);
496 return -1;
497 }
498 }
499
500 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
501 vrf_id = ifp->vrf->vrf_id;
502 } else {
503 struct vrf *pVrf;
504
505 pVrf = vrf_lookup_by_name(vrf_str);
506 if (pVrf)
507 vrf_id = pVrf->vrf_id;
508 else
509 vrf_id = VRF_DEFAULT;
510 }
511
512 if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) {
513 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
514 BFD_STATUS_DOWN, vrf_id);
515 } else {
516 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
517 BFD_STATUS_UP, vrf_id);
518 }
519
520 return 0;
521 }
522
523 static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt,
524 struct interface *ifp, char *cbl_str)
525 {
526 int send_linkup = 0;
527
528 if (IS_ZEBRA_DEBUG_EVENT)
529 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__,
530 ifp->name, cbl_str);
531
532 if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR)
533 && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) {
534
535 if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)
536 send_linkup = 1;
537 ifp->ptm_status = ZEBRA_PTM_STATUS_UP;
538 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)
539 && send_linkup)
540 if_up(ifp, true);
541 } else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR)
542 && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) {
543 ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN;
544 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp))
545 if_down(ifp);
546 }
547
548 return 0;
549 }
550
551 /*
552 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
553 * all the command responses and notifications received from PTM.
554 *
555 * Command responses: Upon establishing connection with PTM, Zebra requests
556 * status of all interfaces using 'get-status' command if global ptm-enable
557 * knob is enabled. As a response to the get-status command PTM sends status
558 * of all the interfaces as command responses. All other type of command
559 * responses with cmd_status key word are dropped. The sole purpose of
560 * registering this function as callback for the command responses is to
561 * handle the responses to get-status command.
562 *
563 * Notifications: Cable status and BFD session status changes are sent as
564 * notifications by PTM. So, this function is also the callback function for
565 * processing all the notifications from the PTM.
566 *
567 */
568 static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt)
569 {
570 struct interface *ifp = NULL;
571 char port_str[128];
572 char cbl_str[32];
573 char cmd_status_str[32];
574
575 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR,
576 cmd_status_str);
577
578 /* Drop command response messages */
579 if (cmd_status_str[0] != '\0') {
580 return 0;
581 }
582
583 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str);
584
585 if (port_str[0] == '\0') {
586 zlog_debug("%s: Key %s not found in PTM msg", __func__,
587 ZEBRA_PTM_PORT_STR);
588 return -1;
589 }
590
591 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) {
592 struct vrf *vrf;
593 int count = 0;
594
595 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
596 ifp = if_lookup_by_name_vrf(port_str, vrf);
597 if (ifp) {
598 count++;
599 if (!vrf_is_backend_netns())
600 break;
601 }
602 }
603
604 if (!ifp) {
605 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
606 "%s: %s not found in interface list",
607 __func__, port_str);
608 return -1;
609 }
610 if (count > 1) {
611 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
612 "%s: multiple interface with name %s",
613 __func__, port_str);
614 return -1;
615 }
616 }
617
618 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
619
620 if (cbl_str[0] == '\0') {
621 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
622 } else {
623 if (ifp) {
624 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
625 cbl_str);
626 } else {
627 return -1;
628 }
629 }
630 }
631
632 void zebra_ptm_sock_read(struct thread *thread)
633 {
634 int sock;
635 int rc;
636
637 errno = 0;
638 sock = THREAD_FD(thread);
639
640 if (sock == -1)
641 return;
642
643 /* PTM communicates in CSV format */
644 do {
645 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
646 ZEBRA_PTM_MAX_SOCKBUF, NULL);
647 } while (rc > 0);
648
649 if (((rc == 0) && !errno)
650 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
651 flog_err_sys(EC_LIB_SOCKET,
652 "%s routing socket error: %s(%d) bytes %d",
653 __func__, safe_strerror(errno), errno, rc);
654
655 close(ptm_cb.ptm_sock);
656 ptm_cb.ptm_sock = -1;
657 zebra_ptm_reset_status(0);
658 ptm_cb.t_timer = NULL;
659 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
660 ptm_cb.reconnect_time,
661 &ptm_cb.t_timer);
662 return;
663 }
664
665 ptm_cb.t_read = NULL;
666 thread_add_read(zrouter.master, zebra_ptm_sock_read, NULL,
667 ptm_cb.ptm_sock, &ptm_cb.t_read);
668 }
669
670 /* BFD peer/dst register/update */
671 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
672 {
673 struct stream *s;
674 struct prefix src_p;
675 struct prefix dst_p;
676 uint8_t multi_hop;
677 uint8_t multi_hop_cnt;
678 uint8_t detect_mul;
679 unsigned int min_rx_timer;
680 unsigned int min_tx_timer;
681 char if_name[INTERFACE_NAMSIZ];
682 uint8_t len;
683 void *out_ctxt;
684 char buf[INET6_ADDRSTRLEN];
685 char tmp_buf[64];
686 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
687 unsigned int pid;
688 uint8_t cbit_set;
689
690 if (hdr->command == ZEBRA_BFD_DEST_UPDATE)
691 client->bfd_peer_upd8_cnt++;
692 else
693 client->bfd_peer_add_cnt++;
694
695 if (IS_ZEBRA_DEBUG_EVENT)
696 zlog_debug("bfd_dst_register msg from client %s: length=%d",
697 zebra_route_string(client->proto), hdr->length);
698
699 if (ptm_cb.ptm_sock == -1) {
700 ptm_cb.t_timer = NULL;
701 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
702 ptm_cb.reconnect_time, &ptm_cb.t_timer);
703 return;
704 }
705
706 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
707 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_START_CMD);
708 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
709 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
710 zebra_route_string(client->proto));
711 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
712 tmp_buf);
713
714 s = msg;
715
716 STREAM_GETL(s, pid);
717 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
718 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
719 tmp_buf);
720
721 STREAM_GETW(s, dst_p.family);
722
723 if (dst_p.family == AF_INET)
724 dst_p.prefixlen = IPV4_MAX_BYTELEN;
725 else
726 dst_p.prefixlen = IPV6_MAX_BYTELEN;
727
728 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
729 if (dst_p.family == AF_INET) {
730 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
731 ptm_lib_append_msg(ptm_hdl, out_ctxt,
732 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
733 } else {
734 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
735 ptm_lib_append_msg(ptm_hdl, out_ctxt,
736 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
737 }
738
739 STREAM_GETL(s, min_rx_timer);
740 snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_rx_timer);
741 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
742 tmp_buf);
743 STREAM_GETL(s, min_tx_timer);
744 snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_tx_timer);
745 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
746 tmp_buf);
747 STREAM_GETC(s, detect_mul);
748 snprintf(tmp_buf, sizeof(tmp_buf), "%d", detect_mul);
749 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
750 tmp_buf);
751
752 STREAM_GETC(s, multi_hop);
753 if (multi_hop) {
754 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
755 ptm_lib_append_msg(ptm_hdl, out_ctxt,
756 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
757 STREAM_GETW(s, src_p.family);
758
759 if (src_p.family == AF_INET)
760 src_p.prefixlen = IPV4_MAX_BYTELEN;
761 else
762 src_p.prefixlen = IPV6_MAX_BYTELEN;
763
764 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
765 if (src_p.family == AF_INET) {
766 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
767 ptm_lib_append_msg(ptm_hdl, out_ctxt,
768 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
769 } else {
770 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
771 ptm_lib_append_msg(ptm_hdl, out_ctxt,
772 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
773 }
774
775 STREAM_GETC(s, multi_hop_cnt);
776 snprintf(tmp_buf, sizeof(tmp_buf), "%d", multi_hop_cnt);
777 ptm_lib_append_msg(ptm_hdl, out_ctxt,
778 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
779
780 if (zvrf_id(zvrf) != VRF_DEFAULT)
781 ptm_lib_append_msg(ptm_hdl, out_ctxt,
782 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
783 zvrf_name(zvrf));
784 } else {
785 if (dst_p.family == AF_INET6) {
786 STREAM_GETW(s, src_p.family);
787
788 if (src_p.family == AF_INET)
789 src_p.prefixlen = IPV4_MAX_BYTELEN;
790 else
791 src_p.prefixlen = IPV6_MAX_BYTELEN;
792
793 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
794 if (src_p.family == AF_INET) {
795 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
796 sizeof(buf));
797 ptm_lib_append_msg(ptm_hdl, out_ctxt,
798 ZEBRA_PTM_BFD_SRC_IP_FIELD,
799 buf);
800 } else {
801 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
802 sizeof(buf));
803 ptm_lib_append_msg(ptm_hdl, out_ctxt,
804 ZEBRA_PTM_BFD_SRC_IP_FIELD,
805 buf);
806 }
807 }
808 STREAM_GETC(s, len);
809 STREAM_GET(if_name, s, len);
810 if_name[len] = '\0';
811
812 ptm_lib_append_msg(ptm_hdl, out_ctxt,
813 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
814 }
815 STREAM_GETC(s, cbit_set);
816 snprintf(tmp_buf, sizeof(tmp_buf), "%d", cbit_set);
817 ptm_lib_append_msg(ptm_hdl, out_ctxt,
818 ZEBRA_PTM_BFD_CBIT_FIELD, tmp_buf);
819
820 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
821 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
822 tmp_buf);
823
824 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
825
826 if (IS_ZEBRA_DEBUG_SEND)
827 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
828 ptm_cb.out_data);
829 zebra_ptm_send_message(ptm_cb.out_data, data_len);
830
831 return;
832
833 stream_failure:
834 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
835 }
836
837 /* BFD peer/dst deregister */
838 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
839 {
840 struct stream *s;
841 struct prefix src_p;
842 struct prefix dst_p;
843 uint8_t multi_hop;
844 char if_name[INTERFACE_NAMSIZ];
845 uint8_t len;
846 char buf[INET6_ADDRSTRLEN];
847 char tmp_buf[64];
848 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
849 void *out_ctxt;
850 unsigned int pid;
851
852 client->bfd_peer_del_cnt++;
853
854 if (IS_ZEBRA_DEBUG_EVENT)
855 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
856 zebra_route_string(client->proto), hdr->length);
857
858 if (ptm_cb.ptm_sock == -1) {
859 ptm_cb.t_timer = NULL;
860 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
861 ptm_cb.reconnect_time, &ptm_cb.t_timer);
862 return;
863 }
864
865 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
866
867 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_STOP_CMD);
868 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
869
870 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
871 zebra_route_string(client->proto));
872 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
873 tmp_buf);
874
875 s = msg;
876
877 STREAM_GETL(s, pid);
878 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
879 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
880 tmp_buf);
881
882 STREAM_GETW(s, dst_p.family);
883
884 if (dst_p.family == AF_INET)
885 dst_p.prefixlen = IPV4_MAX_BYTELEN;
886 else
887 dst_p.prefixlen = IPV6_MAX_BYTELEN;
888
889 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
890 if (dst_p.family == AF_INET)
891 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
892 else
893 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
894 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
895
896
897 STREAM_GETC(s, multi_hop);
898 if (multi_hop) {
899 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
900 ptm_lib_append_msg(ptm_hdl, out_ctxt,
901 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
902
903 STREAM_GETW(s, src_p.family);
904
905 if (src_p.family == AF_INET)
906 src_p.prefixlen = IPV4_MAX_BYTELEN;
907 else
908 src_p.prefixlen = IPV6_MAX_BYTELEN;
909
910 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
911 if (src_p.family == AF_INET)
912 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
913 else
914 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
915 ptm_lib_append_msg(ptm_hdl, out_ctxt,
916 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
917
918 if (zvrf_id(zvrf) != VRF_DEFAULT)
919 ptm_lib_append_msg(ptm_hdl, out_ctxt,
920 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
921 zvrf_name(zvrf));
922 } else {
923 if (dst_p.family == AF_INET6) {
924 STREAM_GETW(s, src_p.family);
925
926 if (src_p.family == AF_INET)
927 src_p.prefixlen = IPV4_MAX_BYTELEN;
928 else
929 src_p.prefixlen = IPV6_MAX_BYTELEN;
930
931 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
932 if (src_p.family == AF_INET) {
933 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
934 sizeof(buf));
935 ptm_lib_append_msg(ptm_hdl, out_ctxt,
936 ZEBRA_PTM_BFD_SRC_IP_FIELD,
937 buf);
938 } else {
939 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
940 sizeof(buf));
941 ptm_lib_append_msg(ptm_hdl, out_ctxt,
942 ZEBRA_PTM_BFD_SRC_IP_FIELD,
943 buf);
944 }
945 }
946
947 STREAM_GETC(s, len);
948 STREAM_GET(if_name, s, len);
949 if_name[len] = '\0';
950
951 ptm_lib_append_msg(ptm_hdl, out_ctxt,
952 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
953 }
954
955 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
956 if (IS_ZEBRA_DEBUG_SEND)
957 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
958 ptm_cb.out_data);
959
960 zebra_ptm_send_message(ptm_cb.out_data, data_len);
961
962 return;
963
964 stream_failure:
965 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
966 }
967
968 /* BFD client register */
969 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
970 {
971 struct stream *s;
972 unsigned int pid;
973 void *out_ctxt = NULL;
974 char tmp_buf[64];
975 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
976
977 client->bfd_client_reg_cnt++;
978
979 if (IS_ZEBRA_DEBUG_EVENT)
980 zlog_debug("bfd_client_register msg from client %s: length=%d",
981 zebra_route_string(client->proto), hdr->length);
982
983 s = msg;
984 STREAM_GETL(s, pid);
985
986 if (ptm_cb.ptm_sock == -1) {
987 ptm_cb.t_timer = NULL;
988 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
989 ptm_cb.reconnect_time, &ptm_cb.t_timer);
990 return;
991 }
992
993 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
994
995 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
996 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
997
998 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
999 zebra_route_string(client->proto));
1000 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1001 tmp_buf);
1002
1003 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
1004 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
1005 tmp_buf);
1006
1007 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1008
1009 if (IS_ZEBRA_DEBUG_SEND)
1010 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1011 ptm_cb.out_data);
1012 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1013
1014 SET_FLAG(ptm_cb.client_flags[client->proto],
1015 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1016
1017 return;
1018
1019 stream_failure:
1020 /*
1021 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1022 * is allocated then we need to add this code back in
1023 *
1024 * if (out_ctxt)
1025 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1026 */
1027 return;
1028 }
1029
1030 /* BFD client deregister */
1031 int zebra_ptm_bfd_client_deregister(struct zserv *client)
1032 {
1033 uint8_t proto = client->proto;
1034 void *out_ctxt;
1035 char tmp_buf[64];
1036 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1037
1038 if (!IS_BFD_ENABLED_PROTOCOL(proto))
1039 return 0;
1040
1041 if (IS_ZEBRA_DEBUG_EVENT)
1042 zlog_debug("bfd_client_deregister msg for client %s",
1043 zebra_route_string(proto));
1044
1045 if (ptm_cb.ptm_sock == -1) {
1046 ptm_cb.t_timer = NULL;
1047 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
1048 ptm_cb.reconnect_time, &ptm_cb.t_timer);
1049 return 0;
1050 }
1051
1052 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
1053
1054 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
1055 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1056 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1057
1058 snprintf(tmp_buf, sizeof(tmp_buf), "%s", zebra_route_string(proto));
1059 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1060 tmp_buf);
1061
1062 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1063
1064 if (IS_ZEBRA_DEBUG_SEND)
1065 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1066 ptm_cb.out_data);
1067
1068 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1069 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1070
1071 return 0;
1072 }
1073
1074 int zebra_ptm_get_enable_state(void)
1075 {
1076 return ptm_cb.ptm_enable;
1077 }
1078
1079 /*
1080 * zebra_ptm_get_status_str - Convert status to a display string.
1081 */
1082 static const char *zebra_ptm_get_status_str(int status)
1083 {
1084 switch (status) {
1085 case ZEBRA_PTM_STATUS_DOWN:
1086 return "fail";
1087 case ZEBRA_PTM_STATUS_UP:
1088 return "pass";
1089 case ZEBRA_PTM_STATUS_UNKNOWN:
1090 default:
1091 return "n/a";
1092 }
1093 }
1094
1095 void zebra_ptm_show_status(struct vty *vty, json_object *json,
1096 struct interface *ifp)
1097 {
1098 const char *status;
1099
1100 if (ifp->ptm_enable)
1101 status = zebra_ptm_get_status_str(ifp->ptm_status);
1102 else
1103 status = "disabled";
1104
1105 if (json)
1106 json_object_string_add(json, "ptmStatus", status);
1107 else
1108 vty_out(vty, " PTM status: %s\n", status);
1109 }
1110
1111 void zebra_ptm_send_status_req(void)
1112 {
1113 void *out_ctxt;
1114 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1115
1116 if (ptm_cb.ptm_enable) {
1117 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1118 &out_ctxt);
1119 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1120 ZEBRA_PTM_GET_STATUS_CMD);
1121 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1122
1123 zebra_ptm_send_message(ptm_cb.out_data, len);
1124 }
1125 }
1126
1127 void zebra_ptm_reset_status(int ptm_disable)
1128 {
1129 struct vrf *vrf;
1130 struct interface *ifp;
1131 int send_linkup;
1132
1133 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
1134 FOR_ALL_INTERFACES (vrf, ifp) {
1135 send_linkup = 0;
1136 if (ifp->ptm_enable) {
1137 if (!if_is_operative(ifp))
1138 send_linkup = 1;
1139
1140 if (ptm_disable)
1141 ifp->ptm_enable =
1142 ZEBRA_IF_PTM_ENABLE_OFF;
1143 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1144
1145 if (if_is_operative(ifp) && send_linkup) {
1146 if (IS_ZEBRA_DEBUG_EVENT)
1147 zlog_debug(
1148 "%s: Bringing up interface %s",
1149 __func__, ifp->name);
1150 if_up(ifp, true);
1151 }
1152 }
1153 }
1154 }
1155
1156 void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
1157 {
1158 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
1159 }
1160
1161 void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1162 struct zebra_if *zebra_ifp)
1163 {
1164 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1165 ifp->ptm_enable = zebra_ifp->ptm_enable;
1166 }
1167
1168 void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
1169 {
1170 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1171 vty_out(vty, " no ptm-enable\n");
1172 }
1173
1174 #else /* HAVE_BFDD */
1175
1176 /*
1177 * Data structures.
1178 */
1179 struct ptm_process {
1180 struct zserv *pp_zs;
1181 pid_t pp_pid;
1182
1183 TAILQ_ENTRY(ptm_process) pp_entry;
1184 };
1185 TAILQ_HEAD(ppqueue, ptm_process) ppqueue;
1186
1187 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS,
1188 "PTM BFD process registration table.");
1189
1190 /*
1191 * Prototypes.
1192 */
1193 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs);
1194 static struct ptm_process *pp_lookup_byzs(struct zserv *zs);
1195 static void pp_free(struct ptm_process *pp);
1196 static void pp_free_all(void);
1197
1198 static void zebra_ptm_send_bfdd(struct stream *msg);
1199 static void zebra_ptm_send_clients(struct stream *msg);
1200 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs);
1201 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1202 struct stream *msg, uint32_t command);
1203
1204
1205 /*
1206 * Process PID registration.
1207 */
1208 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs)
1209 {
1210 struct ptm_process *pp;
1211
1212 #ifdef PTM_DEBUG
1213 /* Sanity check: more than one client can't have the same PID. */
1214 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1215 if (pp->pp_pid == pid && pp->pp_zs != zs)
1216 zlog_err("%s:%d pid and client pointer doesn't match",
1217 __FILE__, __LINE__);
1218 }
1219 #endif /* PTM_DEBUG */
1220
1221 /* Lookup for duplicates. */
1222 pp = pp_lookup_byzs(zs);
1223 if (pp != NULL)
1224 return pp;
1225
1226 /* Allocate and register new process. */
1227 pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp));
1228
1229 pp->pp_pid = pid;
1230 pp->pp_zs = zs;
1231 TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry);
1232
1233 return pp;
1234 }
1235
1236 static struct ptm_process *pp_lookup_byzs(struct zserv *zs)
1237 {
1238 struct ptm_process *pp;
1239
1240 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1241 if (pp->pp_zs != zs)
1242 continue;
1243
1244 break;
1245 }
1246
1247 return pp;
1248 }
1249
1250 static void pp_free(struct ptm_process *pp)
1251 {
1252 if (pp == NULL)
1253 return;
1254
1255 TAILQ_REMOVE(&ppqueue, pp, pp_entry);
1256 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp);
1257 }
1258
1259 static void pp_free_all(void)
1260 {
1261 struct ptm_process *pp;
1262
1263 while (!TAILQ_EMPTY(&ppqueue)) {
1264 pp = TAILQ_FIRST(&ppqueue);
1265 pp_free(pp);
1266 }
1267 }
1268
1269
1270 /*
1271 * Use the FRR's internal daemon implementation.
1272 */
1273 static void zebra_ptm_send_bfdd(struct stream *msg)
1274 {
1275 struct listnode *node;
1276 struct zserv *client;
1277 struct stream *msgc;
1278
1279 /* Create copy for replication. */
1280 msgc = stream_dup(msg);
1281
1282 /* Send message to all running BFDd daemons. */
1283 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1284 if (client->proto != ZEBRA_ROUTE_BFD)
1285 continue;
1286
1287 zserv_send_message(client, msg);
1288
1289 /* Allocate more messages. */
1290 msg = stream_dup(msgc);
1291 }
1292
1293 stream_free(msgc);
1294 stream_free(msg);
1295 }
1296
1297 static void zebra_ptm_send_clients(struct stream *msg)
1298 {
1299 struct listnode *node;
1300 struct zserv *client;
1301 struct stream *msgc;
1302
1303 /* Create copy for replication. */
1304 msgc = stream_dup(msg);
1305
1306 /* Send message to all running client daemons. */
1307 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1308 if (!IS_BFD_ENABLED_PROTOCOL(client->proto))
1309 continue;
1310
1311 zserv_send_message(client, msg);
1312
1313 /* Allocate more messages. */
1314 msg = stream_dup(msgc);
1315 }
1316
1317 stream_free(msgc);
1318 stream_free(msg);
1319 }
1320
1321 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs)
1322 {
1323 struct stream *msg;
1324 struct ptm_process *pp;
1325
1326 if (!IS_BFD_ENABLED_PROTOCOL(zs->proto))
1327 return 0;
1328
1329 /* Find daemon pid by zebra connection pointer. */
1330 pp = pp_lookup_byzs(zs);
1331 if (pp == NULL) {
1332 zlog_err("%s:%d failed to find process pid registration",
1333 __FILE__, __LINE__);
1334 return -1;
1335 }
1336
1337 /* Generate, send message and free() daemon related data. */
1338 msg = stream_new(ZEBRA_MAX_PACKET_SIZ);
1339 if (msg == NULL) {
1340 zlog_debug("%s: not enough memory", __func__);
1341 return 0;
1342 }
1343
1344 /*
1345 * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only
1346 * one callback at the `bfdd` side, however the real command
1347 * number will be included right after the zebra header.
1348 */
1349 zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0);
1350 stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER);
1351
1352 /* Put process PID. */
1353 stream_putl(msg, pp->pp_pid);
1354
1355 /* Update the data pointers. */
1356 stream_putw_at(msg, 0, stream_get_endp(msg));
1357
1358 zebra_ptm_send_bfdd(msg);
1359
1360 pp_free(pp);
1361
1362 return 0;
1363 }
1364
1365 void zebra_ptm_init(void)
1366 {
1367 /* Initialize the ptm process information list. */
1368 TAILQ_INIT(&ppqueue);
1369
1370 /*
1371 * Send deregistration messages to BFD daemon when some other
1372 * daemon closes. This will help avoid sending daemons
1373 * unnecessary notification messages.
1374 */
1375 hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1376 }
1377
1378 void zebra_ptm_finish(void)
1379 {
1380 /* Remove the client disconnect hook and free all memory. */
1381 hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1382 pp_free_all();
1383 }
1384
1385
1386 /*
1387 * Message handling.
1388 */
1389 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1390 struct stream *msg, uint32_t command)
1391 {
1392 struct stream *msgc;
1393 char buf[ZEBRA_MAX_PACKET_SIZ];
1394 pid_t ppid;
1395
1396 /* Create BFD header */
1397 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1398 zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, zvrf->vrf->vrf_id);
1399 stream_putl(msgc, command);
1400
1401 if (STREAM_READABLE(msg) > STREAM_WRITEABLE(msgc)) {
1402 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1403 goto stream_failure;
1404 }
1405
1406 /* Copy original message, excluding header, into new message */
1407 stream_get_from(buf, msg, stream_get_getp(msg), STREAM_READABLE(msg));
1408 stream_put(msgc, buf, STREAM_READABLE(msg));
1409
1410 /* Update length field */
1411 stream_putw_at(msgc, 0, STREAM_READABLE(msgc));
1412
1413 zebra_ptm_send_bfdd(msgc);
1414 msgc = NULL;
1415
1416 /* Registrate process PID for shutdown hook. */
1417 STREAM_GETL(msg, ppid);
1418 pp_new(ppid, zs);
1419
1420 return;
1421
1422 stream_failure:
1423 if (msgc)
1424 stream_free(msgc);
1425 zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__);
1426 }
1427
1428 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
1429 {
1430 if (IS_ZEBRA_DEBUG_EVENT)
1431 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1432 zebra_route_string(client->proto), hdr->length);
1433
1434 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REGISTER);
1435 }
1436
1437 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
1438 {
1439 if (IS_ZEBRA_DEBUG_EVENT)
1440 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1441 zebra_route_string(client->proto), hdr->length);
1442
1443 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_DEREGISTER);
1444 }
1445
1446 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
1447 {
1448 if (IS_ZEBRA_DEBUG_EVENT)
1449 zlog_debug("bfd_client_register msg from client %s: length=%d",
1450 zebra_route_string(client->proto), hdr->length);
1451
1452 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_CLIENT_REGISTER);
1453 }
1454
1455 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)
1456 {
1457 struct stream *msgc;
1458 size_t zmsglen, zhdrlen;
1459 uint32_t cmd;
1460
1461 /*
1462 * NOTE:
1463 * Replay messages with HAVE_BFDD are meant to be replayed to
1464 * the client daemons. These messages are composed and
1465 * originated from the `bfdd` daemon.
1466 */
1467 if (IS_ZEBRA_DEBUG_EVENT)
1468 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1469 zebra_route_string(client->proto), hdr->length);
1470
1471 /*
1472 * Client messages must be re-routed, otherwise do the `bfdd`
1473 * special treatment.
1474 */
1475 if (client->proto != ZEBRA_ROUTE_BFD) {
1476 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REPLAY);
1477 return;
1478 }
1479
1480 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1481 if (stream_getl2(msg, &cmd) == false) {
1482 zlog_err("%s: expected at least 4 bytes (command)", __func__);
1483 return;
1484 }
1485
1486 /*
1487 * Don't modify message in the zebra API. In order to do that we
1488 * need to allocate a new message stream and copy the message
1489 * provided by zebra.
1490 */
1491 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1492 if (msgc == NULL) {
1493 zlog_debug("%s: not enough memory", __func__);
1494 return;
1495 }
1496
1497 /* Calculate our header size plus the message contents. */
1498 if (cmd != ZEBRA_BFD_DEST_REPLAY) {
1499 zhdrlen = ZEBRA_HEADER_SIZE;
1500 zmsglen = msg->endp - msg->getp;
1501 memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
1502
1503 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1504
1505 msgc->getp = 0;
1506 msgc->endp = zhdrlen + zmsglen;
1507 } else
1508 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1509
1510 /* Update the data pointers. */
1511 stream_putw_at(msgc, 0, stream_get_endp(msgc));
1512
1513 zebra_ptm_send_clients(msgc);
1514 }
1515
1516 /*
1517 * Unused functions.
1518 */
1519 void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__)))
1520 {
1521 /* NOTHING */
1522 }
1523
1524 int zebra_ptm_get_enable_state(void)
1525 {
1526 return 0;
1527 }
1528
1529 void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)),
1530 json_object *json __attribute__((__unused__)),
1531 struct interface *ifp __attribute__((__unused__)))
1532 {
1533 /* NOTHING */
1534 }
1535
1536 void zebra_ptm_write(struct vty *vty __attribute__((__unused__)))
1537 {
1538 /* NOTHING */
1539 }
1540
1541 void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)),
1542 struct zebra_if *zifp __attribute__((__unused__)))
1543 {
1544 /* NOTHING */
1545 }
1546 void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)),
1547 struct zebra_if *zi __attribute__((__unused__)))
1548 {
1549 /* NOTHING */
1550 }
1551
1552 #endif /* HAVE_BFDD */