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