]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ptm.c
zebra, lib: error references for zebra
[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_warn("%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_warn("%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 zlog_warn("%s ptm socket error: %s", __func__,
184 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 zlog_warn("%s ptm socket error: %s", __func__,
210 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 zlog_ferr(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 zlog_ferr(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 zlog_warn("%s: %s not found in interface list",
612 __func__, port_str);
613 return -1;
614 }
615 }
616
617 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
618
619 if (cbl_str[0] == '\0') {
620 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
621 } else {
622 if (ifp) {
623 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
624 cbl_str);
625 } else {
626 return -1;
627 }
628 }
629 }
630
631 int zebra_ptm_sock_read(struct thread *thread)
632 {
633 int sock;
634 int rc;
635
636 errno = 0;
637 sock = THREAD_FD(thread);
638
639 if (sock == -1)
640 return -1;
641
642 /* PTM communicates in CSV format */
643 do {
644 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
645 ZEBRA_PTM_MAX_SOCKBUF, NULL);
646 } while (rc > 0);
647
648 if (((rc == 0) && !errno)
649 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
650 zlog_warn("%s routing socket error: %s(%d) bytes %d",
651 __func__, safe_strerror(errno), errno, rc);
652
653 close(ptm_cb.ptm_sock);
654 ptm_cb.ptm_sock = -1;
655 zebra_ptm_reset_status(0);
656 ptm_cb.t_timer = NULL;
657 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
658 ptm_cb.reconnect_time,
659 &ptm_cb.t_timer);
660 return (-1);
661 }
662
663 ptm_cb.t_read = NULL;
664 thread_add_read(zebrad.master, zebra_ptm_sock_read, NULL,
665 ptm_cb.ptm_sock, &ptm_cb.t_read);
666
667 return 0;
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
689 if (hdr->command == ZEBRA_BFD_DEST_UPDATE)
690 client->bfd_peer_upd8_cnt++;
691 else
692 client->bfd_peer_add_cnt++;
693
694 if (IS_ZEBRA_DEBUG_EVENT)
695 zlog_debug("bfd_dst_register msg from client %s: length=%d",
696 zebra_route_string(client->proto), hdr->length);
697
698 if (ptm_cb.ptm_sock == -1) {
699 ptm_cb.t_timer = NULL;
700 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
701 ptm_cb.reconnect_time, &ptm_cb.t_timer);
702 return;
703 }
704
705 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
706 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD);
707 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
708 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
709 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
710 tmp_buf);
711
712 s = msg;
713
714 STREAM_GETL(s, pid);
715 sprintf(tmp_buf, "%d", pid);
716 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
717 tmp_buf);
718
719 STREAM_GETW(s, dst_p.family);
720
721 if (dst_p.family == AF_INET)
722 dst_p.prefixlen = IPV4_MAX_BYTELEN;
723 else
724 dst_p.prefixlen = IPV6_MAX_BYTELEN;
725
726 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
727 if (dst_p.family == AF_INET) {
728 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
729 ptm_lib_append_msg(ptm_hdl, out_ctxt,
730 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
731 } else {
732 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
733 ptm_lib_append_msg(ptm_hdl, out_ctxt,
734 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
735 }
736
737 STREAM_GETL(s, min_rx_timer);
738 sprintf(tmp_buf, "%d", min_rx_timer);
739 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
740 tmp_buf);
741 STREAM_GETL(s, min_tx_timer);
742 sprintf(tmp_buf, "%d", min_tx_timer);
743 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
744 tmp_buf);
745 STREAM_GETC(s, detect_mul);
746 sprintf(tmp_buf, "%d", detect_mul);
747 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
748 tmp_buf);
749
750 STREAM_GETC(s, multi_hop);
751 if (multi_hop) {
752 sprintf(tmp_buf, "%d", 1);
753 ptm_lib_append_msg(ptm_hdl, out_ctxt,
754 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
755 STREAM_GETW(s, src_p.family);
756
757 if (src_p.family == AF_INET)
758 src_p.prefixlen = IPV4_MAX_BYTELEN;
759 else
760 src_p.prefixlen = IPV6_MAX_BYTELEN;
761
762 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
763 if (src_p.family == AF_INET) {
764 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
765 ptm_lib_append_msg(ptm_hdl, out_ctxt,
766 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
767 } else {
768 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
769 ptm_lib_append_msg(ptm_hdl, out_ctxt,
770 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
771 }
772
773 STREAM_GETC(s, multi_hop_cnt);
774 sprintf(tmp_buf, "%d", multi_hop_cnt);
775 ptm_lib_append_msg(ptm_hdl, out_ctxt,
776 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
777
778 if (zvrf_id(zvrf) != VRF_DEFAULT)
779 ptm_lib_append_msg(ptm_hdl, out_ctxt,
780 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
781 zvrf_name(zvrf));
782 } else {
783 if (dst_p.family == AF_INET6) {
784 STREAM_GETW(s, src_p.family);
785
786 if (src_p.family == AF_INET)
787 src_p.prefixlen = IPV4_MAX_BYTELEN;
788 else
789 src_p.prefixlen = IPV6_MAX_BYTELEN;
790
791 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
792 if (src_p.family == AF_INET) {
793 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
794 sizeof(buf));
795 ptm_lib_append_msg(ptm_hdl, out_ctxt,
796 ZEBRA_PTM_BFD_SRC_IP_FIELD,
797 buf);
798 } else {
799 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
800 sizeof(buf));
801 ptm_lib_append_msg(ptm_hdl, out_ctxt,
802 ZEBRA_PTM_BFD_SRC_IP_FIELD,
803 buf);
804 }
805 }
806 STREAM_GETC(s, len);
807 STREAM_GET(if_name, s, len);
808 if_name[len] = '\0';
809
810 ptm_lib_append_msg(ptm_hdl, out_ctxt,
811 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
812 }
813
814 sprintf(tmp_buf, "%d", 1);
815 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
816 tmp_buf);
817
818 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
819
820 if (IS_ZEBRA_DEBUG_SEND)
821 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
822 ptm_cb.out_data);
823 zebra_ptm_send_message(ptm_cb.out_data, data_len);
824
825 return;
826
827 stream_failure:
828 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
829 }
830
831 /* BFD peer/dst deregister */
832 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
833 {
834 struct stream *s;
835 struct prefix src_p;
836 struct prefix dst_p;
837 uint8_t multi_hop;
838 char if_name[INTERFACE_NAMSIZ];
839 uint8_t len;
840 char buf[INET6_ADDRSTRLEN];
841 char tmp_buf[64];
842 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
843 void *out_ctxt;
844 unsigned int pid;
845
846 client->bfd_peer_del_cnt++;
847
848 if (IS_ZEBRA_DEBUG_EVENT)
849 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
850 zebra_route_string(client->proto), hdr->length);
851
852 if (ptm_cb.ptm_sock == -1) {
853 ptm_cb.t_timer = NULL;
854 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
855 ptm_cb.reconnect_time, &ptm_cb.t_timer);
856 return;
857 }
858
859 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
860
861 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD);
862 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
863
864 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
865 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
866 tmp_buf);
867
868 s = msg;
869
870 STREAM_GETL(s, pid);
871 sprintf(tmp_buf, "%d", pid);
872 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
873 tmp_buf);
874
875 STREAM_GETW(s, dst_p.family);
876
877 if (dst_p.family == AF_INET)
878 dst_p.prefixlen = IPV4_MAX_BYTELEN;
879 else
880 dst_p.prefixlen = IPV6_MAX_BYTELEN;
881
882 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
883 if (dst_p.family == AF_INET)
884 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
885 else
886 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
887 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
888
889
890 STREAM_GETC(s, multi_hop);
891 if (multi_hop) {
892 sprintf(tmp_buf, "%d", 1);
893 ptm_lib_append_msg(ptm_hdl, out_ctxt,
894 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
895
896 STREAM_GETW(s, src_p.family);
897
898 if (src_p.family == AF_INET)
899 src_p.prefixlen = IPV4_MAX_BYTELEN;
900 else
901 src_p.prefixlen = IPV6_MAX_BYTELEN;
902
903 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
904 if (src_p.family == AF_INET)
905 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
906 else
907 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
908 ptm_lib_append_msg(ptm_hdl, out_ctxt,
909 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
910
911 if (zvrf_id(zvrf) != VRF_DEFAULT)
912 ptm_lib_append_msg(ptm_hdl, out_ctxt,
913 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
914 zvrf_name(zvrf));
915 } else {
916 if (dst_p.family == AF_INET6) {
917 STREAM_GETW(s, src_p.family);
918
919 if (src_p.family == AF_INET)
920 src_p.prefixlen = IPV4_MAX_BYTELEN;
921 else
922 src_p.prefixlen = IPV6_MAX_BYTELEN;
923
924 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
925 if (src_p.family == AF_INET) {
926 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
927 sizeof(buf));
928 ptm_lib_append_msg(ptm_hdl, out_ctxt,
929 ZEBRA_PTM_BFD_SRC_IP_FIELD,
930 buf);
931 } else {
932 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
933 sizeof(buf));
934 ptm_lib_append_msg(ptm_hdl, out_ctxt,
935 ZEBRA_PTM_BFD_SRC_IP_FIELD,
936 buf);
937 }
938 }
939
940 STREAM_GETC(s, len);
941 STREAM_GET(if_name, s, len);
942 if_name[len] = '\0';
943
944 ptm_lib_append_msg(ptm_hdl, out_ctxt,
945 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
946 }
947
948 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
949 if (IS_ZEBRA_DEBUG_SEND)
950 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
951 ptm_cb.out_data);
952
953 zebra_ptm_send_message(ptm_cb.out_data, data_len);
954
955 return;
956
957 stream_failure:
958 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
959 }
960
961 /* BFD client register */
962 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
963 {
964 struct stream *s;
965 unsigned int pid;
966 void *out_ctxt = NULL;
967 char tmp_buf[64];
968 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
969
970 client->bfd_client_reg_cnt++;
971
972 if (IS_ZEBRA_DEBUG_EVENT)
973 zlog_debug("bfd_client_register msg from client %s: length=%d",
974 zebra_route_string(client->proto), hdr->length);
975
976 s = msg;
977 STREAM_GETL(s, pid);
978
979 if (ptm_cb.ptm_sock == -1) {
980 ptm_cb.t_timer = NULL;
981 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
982 ptm_cb.reconnect_time, &ptm_cb.t_timer);
983 return;
984 }
985
986 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
987
988 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
989 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
990
991 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
992 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
993 tmp_buf);
994
995 sprintf(tmp_buf, "%d", pid);
996 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
997 tmp_buf);
998
999 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1000
1001 if (IS_ZEBRA_DEBUG_SEND)
1002 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1003 ptm_cb.out_data);
1004 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1005
1006 SET_FLAG(ptm_cb.client_flags[client->proto],
1007 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1008
1009 return;
1010
1011 stream_failure:
1012 /*
1013 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1014 * is allocated then we need to add this code back in
1015 *
1016 * if (out_ctxt)
1017 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1018 */
1019 return;
1020 }
1021
1022 /* BFD client deregister */
1023 int zebra_ptm_bfd_client_deregister(struct zserv *client)
1024 {
1025 uint8_t proto = client->proto;
1026 void *out_ctxt;
1027 char tmp_buf[64];
1028 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1029
1030 if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP
1031 && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM)
1032 return 0;
1033
1034 if (IS_ZEBRA_DEBUG_EVENT)
1035 zlog_warn("bfd_client_deregister msg for client %s",
1036 zebra_route_string(proto));
1037
1038 if (ptm_cb.ptm_sock == -1) {
1039 ptm_cb.t_timer = NULL;
1040 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
1041 ptm_cb.reconnect_time, &ptm_cb.t_timer);
1042 return 0;
1043 }
1044
1045 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
1046
1047 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1048 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1049
1050 sprintf(tmp_buf, "%s", zebra_route_string(proto));
1051 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1052 tmp_buf);
1053
1054 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1055
1056 if (IS_ZEBRA_DEBUG_SEND)
1057 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1058 ptm_cb.out_data);
1059
1060 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1061 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1062
1063 return 0;
1064 }
1065
1066 int zebra_ptm_get_enable_state(void)
1067 {
1068 return ptm_cb.ptm_enable;
1069 }
1070
1071 /*
1072 * zebra_ptm_get_status_str - Convert status to a display string.
1073 */
1074 static const char *zebra_ptm_get_status_str(int status)
1075 {
1076 switch (status) {
1077 case ZEBRA_PTM_STATUS_DOWN:
1078 return "fail";
1079 case ZEBRA_PTM_STATUS_UP:
1080 return "pass";
1081 case ZEBRA_PTM_STATUS_UNKNOWN:
1082 default:
1083 return "n/a";
1084 }
1085 }
1086
1087 void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
1088 {
1089 vty_out(vty, " PTM status: ");
1090 if (ifp->ptm_enable) {
1091 vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1092 } else {
1093 vty_out(vty, "disabled\n");
1094 }
1095 }
1096
1097 void zebra_ptm_send_status_req(void)
1098 {
1099 void *out_ctxt;
1100 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1101
1102 if (ptm_cb.ptm_enable) {
1103 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1104 &out_ctxt);
1105 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1106 ZEBRA_PTM_GET_STATUS_CMD);
1107 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1108
1109 zebra_ptm_send_message(ptm_cb.out_data, len);
1110 }
1111 }
1112
1113 void zebra_ptm_reset_status(int ptm_disable)
1114 {
1115 struct vrf *vrf;
1116 struct interface *ifp;
1117 int send_linkup;
1118
1119 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
1120 FOR_ALL_INTERFACES (vrf, ifp) {
1121 send_linkup = 0;
1122 if (ifp->ptm_enable) {
1123 if (!if_is_operative(ifp))
1124 send_linkup = 1;
1125
1126 if (ptm_disable)
1127 ifp->ptm_enable =
1128 ZEBRA_IF_PTM_ENABLE_OFF;
1129 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1130
1131 if (if_is_operative(ifp) && send_linkup) {
1132 if (IS_ZEBRA_DEBUG_EVENT)
1133 zlog_debug(
1134 "%s: Bringing up interface %s",
1135 __func__, ifp->name);
1136 if_up(ifp);
1137 }
1138 }
1139 }
1140 }
1141
1142 void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
1143 {
1144 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
1145 }
1146
1147 void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1148 struct zebra_if *zebra_ifp)
1149 {
1150 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1151 ifp->ptm_enable = zebra_ifp->ptm_enable;
1152 }
1153
1154 void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
1155 {
1156 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1157 vty_out(vty, " no ptm-enable\n");
1158 }
1159
1160 #else /* HAVE_BFDD */
1161
1162 #include "zebra/zebra_memory.h"
1163
1164 /*
1165 * Data structures.
1166 */
1167 struct ptm_process {
1168 struct zserv *pp_zs;
1169 pid_t pp_pid;
1170
1171 TAILQ_ENTRY(ptm_process) pp_entry;
1172 };
1173 TAILQ_HEAD(ppqueue, ptm_process) ppqueue;
1174
1175 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS,
1176 "PTM BFD process registration table.");
1177
1178 /*
1179 * Prototypes.
1180 */
1181 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs);
1182 static struct ptm_process *pp_lookup_byzs(struct zserv *zs);
1183 static void pp_free(struct ptm_process *pp);
1184 static void pp_free_all(void);
1185
1186 static void zebra_ptm_send_bfdd(struct stream *msg);
1187 static void zebra_ptm_send_clients(struct stream *msg);
1188 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs);
1189 static void _zebra_ptm_reroute(struct zserv *zs, struct stream *msg,
1190 uint32_t command);
1191
1192
1193 /*
1194 * Process PID registration.
1195 */
1196 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs)
1197 {
1198 struct ptm_process *pp;
1199
1200 #ifdef PTM_DEBUG
1201 /* Sanity check: more than one client can't have the same PID. */
1202 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1203 if (pp->pp_pid == pid && pp->pp_zs != zs)
1204 zlog_err("%s:%d pid and client pointer doesn't match",
1205 __FILE__, __LINE__);
1206 }
1207 #endif /* PTM_DEBUG */
1208
1209 /* Lookup for duplicates. */
1210 pp = pp_lookup_byzs(zs);
1211 if (pp != NULL)
1212 return pp;
1213
1214 /* Allocate and register new process. */
1215 pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp));
1216 if (pp == NULL)
1217 return NULL;
1218
1219 pp->pp_pid = pid;
1220 pp->pp_zs = zs;
1221 TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry);
1222
1223 return pp;
1224 }
1225
1226 static struct ptm_process *pp_lookup_byzs(struct zserv *zs)
1227 {
1228 struct ptm_process *pp;
1229
1230 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1231 if (pp->pp_zs != zs)
1232 continue;
1233
1234 break;
1235 }
1236
1237 return pp;
1238 }
1239
1240 static void pp_free(struct ptm_process *pp)
1241 {
1242 if (pp == NULL)
1243 return;
1244
1245 TAILQ_REMOVE(&ppqueue, pp, pp_entry);
1246 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp);
1247 }
1248
1249 static void pp_free_all(void)
1250 {
1251 struct ptm_process *pp;
1252
1253 while (!TAILQ_EMPTY(&ppqueue)) {
1254 pp = TAILQ_FIRST(&ppqueue);
1255 pp_free(pp);
1256 }
1257 }
1258
1259
1260 /*
1261 * Use the FRR's internal daemon implementation.
1262 */
1263 static void zebra_ptm_send_bfdd(struct stream *msg)
1264 {
1265 struct listnode *node;
1266 struct zserv *client;
1267 struct stream *msgc;
1268
1269 /* Create copy for replication. */
1270 msgc = stream_dup(msg);
1271 if (msgc == NULL) {
1272 zlog_warn("%s: not enough memory", __func__);
1273 return;
1274 }
1275
1276 /* Send message to all running BFDd daemons. */
1277 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
1278 if (client->proto != ZEBRA_ROUTE_BFD)
1279 continue;
1280
1281 zserv_send_message(client, msg);
1282
1283 /* Allocate more messages. */
1284 msg = stream_dup(msgc);
1285 if (msg == NULL) {
1286 zlog_warn("%s: not enough memory", __func__);
1287 return;
1288 }
1289 }
1290
1291 stream_free(msgc);
1292 }
1293
1294 static void zebra_ptm_send_clients(struct stream *msg)
1295 {
1296 struct listnode *node;
1297 struct zserv *client;
1298 struct stream *msgc;
1299
1300 /* Create copy for replication. */
1301 msgc = stream_dup(msg);
1302 if (msgc == NULL) {
1303 zlog_warn("%s: not enough memory", __func__);
1304 return;
1305 }
1306
1307 /* Send message to all running client daemons. */
1308 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) {
1309 switch (client->proto) {
1310 case ZEBRA_ROUTE_BGP:
1311 case ZEBRA_ROUTE_OSPF:
1312 case ZEBRA_ROUTE_OSPF6:
1313 case ZEBRA_ROUTE_PIM:
1314 break;
1315
1316 default:
1317 /* NOTHING: skip this daemon. */
1318 continue;
1319 }
1320
1321 zserv_send_message(client, msg);
1322
1323 /* Allocate more messages. */
1324 msg = stream_dup(msgc);
1325 if (msg == NULL) {
1326 zlog_warn("%s: not enough memory", __func__);
1327 return;
1328 }
1329 }
1330
1331 stream_free(msgc);
1332 }
1333
1334 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs)
1335 {
1336 struct stream *msg;
1337 struct ptm_process *pp;
1338
1339 /* Filter daemons that must receive this treatment. */
1340 switch (zs->proto) {
1341 case ZEBRA_ROUTE_BGP:
1342 case ZEBRA_ROUTE_OSPF:
1343 case ZEBRA_ROUTE_OSPF6:
1344 case ZEBRA_ROUTE_PIM:
1345 break;
1346
1347 case ZEBRA_ROUTE_BFD:
1348 /* Don't try to send BFDd messages to itself. */
1349 return 0;
1350
1351 default:
1352 /* Unsupported daemon. */
1353 return 0;
1354 }
1355
1356 /* Find daemon pid by zebra connection pointer. */
1357 pp = pp_lookup_byzs(zs);
1358 if (pp == NULL) {
1359 zlog_err("%s:%d failed to find process pid registration",
1360 __FILE__, __LINE__);
1361 return -1;
1362 }
1363
1364 /* Generate, send message and free() daemon related data. */
1365 msg = stream_new(ZEBRA_MAX_PACKET_SIZ);
1366 if (msg == NULL) {
1367 zlog_warn("%s: not enough memory", __func__);
1368 return 0;
1369 }
1370
1371 /*
1372 * The message type will be BFD_DEST_REPLY so we can use only
1373 * one callback at the `bfdd` side, however the real command
1374 * number will be included right after the zebra header.
1375 */
1376 zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0);
1377 stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER);
1378
1379 /* Put process PID. */
1380 stream_putl(msg, pp->pp_pid);
1381
1382 /* Update the data pointers. */
1383 stream_putw_at(msg, 0, stream_get_endp(msg));
1384
1385 zebra_ptm_send_bfdd(msg);
1386
1387 pp_free(pp);
1388
1389 return 0;
1390 }
1391
1392 void zebra_ptm_init(void)
1393 {
1394 /* Initialize the ptm process information list. */
1395 TAILQ_INIT(&ppqueue);
1396
1397 /*
1398 * Send deregistration messages to BFD daemon when some other
1399 * daemon closes. This will help avoid sending daemons
1400 * unnecessary notification messages.
1401 */
1402 hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1403 }
1404
1405 void zebra_ptm_finish(void)
1406 {
1407 /* Remove the client disconnect hook and free all memory. */
1408 hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1409 pp_free_all();
1410 }
1411
1412
1413 /*
1414 * Message handling.
1415 */
1416 static void _zebra_ptm_reroute(struct zserv *zs, struct stream *msg,
1417 uint32_t command)
1418 {
1419 struct stream *msgc;
1420 size_t zmsglen, zhdrlen;
1421 pid_t ppid;
1422
1423 /*
1424 * Don't modify message in the zebra API. In order to do that we
1425 * need to allocate a new message stream and copy the message
1426 * provided by zebra.
1427 */
1428 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1429 if (msgc == NULL) {
1430 zlog_warn("%s: not enough memory", __func__);
1431 return;
1432 }
1433
1434 /* Calculate our header size plus the message contents. */
1435 zhdrlen = ZEBRA_HEADER_SIZE + sizeof(uint32_t);
1436 zmsglen = msg->endp - msg->getp;
1437 memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
1438
1439 /*
1440 * The message type will be BFD_DEST_REPLY so we can use only
1441 * one callback at the `bfdd` side, however the real command
1442 * number will be included right after the zebra header.
1443 */
1444 zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, 0);
1445 stream_putl(msgc, command);
1446
1447 /* Update the data pointers. */
1448 msgc->getp = 0;
1449 msgc->endp = zhdrlen + zmsglen;
1450 stream_putw_at(msgc, 0, stream_get_endp(msgc));
1451
1452 zebra_ptm_send_bfdd(msgc);
1453
1454 /* Registrate process PID for shutdown hook. */
1455 STREAM_GETL(msg, ppid);
1456 pp_new(ppid, zs);
1457
1458 return;
1459
1460 stream_failure:
1461 zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__);
1462 }
1463
1464 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
1465 {
1466 if (IS_ZEBRA_DEBUG_EVENT)
1467 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1468 zebra_route_string(client->proto), hdr->length);
1469
1470 _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_REGISTER);
1471 }
1472
1473 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
1474 {
1475 if (IS_ZEBRA_DEBUG_EVENT)
1476 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1477 zebra_route_string(client->proto), hdr->length);
1478
1479 _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_DEREGISTER);
1480 }
1481
1482 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
1483 {
1484 if (IS_ZEBRA_DEBUG_EVENT)
1485 zlog_debug("bfd_client_register msg from client %s: length=%d",
1486 zebra_route_string(client->proto), hdr->length);
1487
1488 _zebra_ptm_reroute(client, msg, ZEBRA_BFD_CLIENT_REGISTER);
1489 }
1490
1491 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)
1492 {
1493 struct stream *msgc;
1494 size_t zmsglen, zhdrlen;
1495 uint32_t cmd;
1496
1497 /*
1498 * NOTE:
1499 * Replay messages with HAVE_BFDD are meant to be replayed to
1500 * the client daemons. These messages are composed and
1501 * originated from the `bfdd` daemon.
1502 */
1503 if (IS_ZEBRA_DEBUG_EVENT)
1504 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1505 zebra_route_string(client->proto), hdr->length);
1506
1507 /*
1508 * Client messages must be re-routed, otherwise do the `bfdd`
1509 * special treatment.
1510 */
1511 if (client->proto != ZEBRA_ROUTE_BFD) {
1512 _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_REPLAY);
1513 return;
1514 }
1515
1516 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1517 if (stream_getl2(msg, &cmd) == false) {
1518 zlog_err("%s: expected at least 4 bytes (command)", __func__);
1519 return;
1520 }
1521
1522 /*
1523 * Don't modify message in the zebra API. In order to do that we
1524 * need to allocate a new message stream and copy the message
1525 * provided by zebra.
1526 */
1527 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1528 if (msgc == NULL) {
1529 zlog_warn("%s: not enough memory", __func__);
1530 return;
1531 }
1532
1533 /* Calculate our header size plus the message contents. */
1534 if (cmd != ZEBRA_BFD_DEST_REPLAY) {
1535 zhdrlen = ZEBRA_HEADER_SIZE;
1536 zmsglen = msg->endp - msg->getp;
1537 memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
1538
1539 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1540
1541 msgc->getp = 0;
1542 msgc->endp = zhdrlen + zmsglen;
1543 } else
1544 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1545
1546 /* Update the data pointers. */
1547 stream_putw_at(msgc, 0, stream_get_endp(msgc));
1548
1549 zebra_ptm_send_clients(msgc);
1550 }
1551
1552 /*
1553 * Unused functions.
1554 */
1555 void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__)))
1556 {
1557 /* NOTHING */
1558 }
1559
1560 int zebra_ptm_get_enable_state(void)
1561 {
1562 return 1;
1563 }
1564
1565 void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)),
1566 struct interface *ifp __attribute__((__unused__)))
1567 {
1568 /* NOTHING */
1569 }
1570
1571 void zebra_ptm_write(struct vty *vty __attribute__((__unused__)))
1572 {
1573 /* NOTHING */
1574 }
1575
1576 void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)),
1577 struct zebra_if *zifp __attribute__((__unused__)))
1578 {
1579 /* NOTHING */
1580 }
1581 void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)),
1582 struct zebra_if *zi __attribute__((__unused__)))
1583 {
1584 /* NOTHING */
1585 }
1586
1587 #endif /* HAVE_BFDD */