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