]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ptm.c
Merge pull request #5451 from opensourcerouting/rcu-log
[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 #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 sprintf(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 /* Release threads. */
160 if (ptm_cb.t_read)
161 thread_cancel(ptm_cb.t_read);
162 if (ptm_cb.t_write)
163 thread_cancel(ptm_cb.t_write);
164 if (ptm_cb.t_timer)
165 thread_cancel(ptm_cb.t_timer);
166
167 if (ptm_cb.wb)
168 buffer_free(ptm_cb.wb);
169
170 if (ptm_cb.ptm_sock >= 0)
171 close(ptm_cb.ptm_sock);
172 }
173
174 static int zebra_ptm_flush_messages(struct thread *thread)
175 {
176 ptm_cb.t_write = NULL;
177
178 if (ptm_cb.ptm_sock == -1)
179 return -1;
180
181 errno = 0;
182
183 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
184 case BUFFER_ERROR:
185 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
186 safe_strerror(errno));
187 close(ptm_cb.ptm_sock);
188 ptm_cb.ptm_sock = -1;
189 zebra_ptm_reset_status(0);
190 ptm_cb.t_timer = NULL;
191 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
192 ptm_cb.reconnect_time, &ptm_cb.t_timer);
193 return -1;
194 case BUFFER_PENDING:
195 ptm_cb.t_write = NULL;
196 thread_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
197 ptm_cb.ptm_sock, &ptm_cb.t_write);
198 break;
199 case BUFFER_EMPTY:
200 break;
201 }
202
203 return 0;
204 }
205
206 static int zebra_ptm_send_message(char *data, int size)
207 {
208 errno = 0;
209 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
210 case BUFFER_ERROR:
211 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
212 safe_strerror(errno));
213 close(ptm_cb.ptm_sock);
214 ptm_cb.ptm_sock = -1;
215 zebra_ptm_reset_status(0);
216 ptm_cb.t_timer = NULL;
217 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
218 ptm_cb.reconnect_time, &ptm_cb.t_timer);
219 return -1;
220 case BUFFER_EMPTY:
221 THREAD_OFF(ptm_cb.t_write);
222 break;
223 case BUFFER_PENDING:
224 thread_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
225 ptm_cb.ptm_sock, &ptm_cb.t_write);
226 break;
227 }
228
229 return 0;
230 }
231
232 int zebra_ptm_connect(struct thread *t)
233 {
234 int init = 0;
235
236 if (ptm_cb.ptm_sock == -1) {
237 zebra_ptm_socket_init();
238 init = 1;
239 }
240
241 if (ptm_cb.ptm_sock != -1) {
242 if (init) {
243 ptm_cb.t_read = NULL;
244 thread_add_read(zrouter.master, zebra_ptm_sock_read,
245 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
246 zebra_bfd_peer_replay_req();
247 }
248 zebra_ptm_send_status_req();
249 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
250 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
251 ptm_cb.reconnect_time *= 2;
252 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
253 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
254
255 ptm_cb.t_timer = NULL;
256 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
257 ptm_cb.reconnect_time, &ptm_cb.t_timer);
258 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
259 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
260 }
261
262 return (errno);
263 }
264
265 DEFUN (zebra_ptm_enable,
266 zebra_ptm_enable_cmd,
267 "ptm-enable",
268 "Enable neighbor check with specified topology\n")
269 {
270 struct vrf *vrf;
271 struct interface *ifp;
272 struct zebra_if *if_data;
273
274 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
275
276 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
277 FOR_ALL_INTERFACES (vrf, ifp)
278 if (!ifp->ptm_enable) {
279 if_data = (struct zebra_if *)ifp->info;
280 if (if_data
281 && (if_data->ptm_enable
282 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
283 ifp->ptm_enable =
284 ZEBRA_IF_PTM_ENABLE_ON;
285 }
286 /* Assign a default unknown status */
287 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
288 }
289
290 zebra_ptm_connect(NULL);
291
292 return CMD_SUCCESS;
293 }
294
295 DEFUN (no_zebra_ptm_enable,
296 no_zebra_ptm_enable_cmd,
297 "no ptm-enable",
298 NO_STR
299 "Enable neighbor check with specified topology\n")
300 {
301 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
302 zebra_ptm_reset_status(1);
303 return CMD_SUCCESS;
304 }
305
306 DEFUN (zebra_ptm_enable_if,
307 zebra_ptm_enable_if_cmd,
308 "ptm-enable",
309 "Enable neighbor check with specified topology\n")
310 {
311 VTY_DECLVAR_CONTEXT(interface, ifp);
312 struct zebra_if *if_data;
313 int old_ptm_enable;
314 int send_linkdown = 0;
315
316 if_data = ifp->info;
317 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
318
319 if (ifp->ifindex == IFINDEX_INTERNAL) {
320 return CMD_SUCCESS;
321 }
322
323 old_ptm_enable = ifp->ptm_enable;
324 ifp->ptm_enable = ptm_cb.ptm_enable;
325
326 if (if_is_no_ptm_operative(ifp))
327 send_linkdown = 1;
328
329 if (!old_ptm_enable && ptm_cb.ptm_enable) {
330 if (!if_is_operative(ifp) && send_linkdown) {
331 if (IS_ZEBRA_DEBUG_EVENT)
332 zlog_debug("%s: Bringing down interface %s",
333 __func__, ifp->name);
334 if_down(ifp);
335 }
336 }
337
338 return CMD_SUCCESS;
339 }
340
341 DEFUN (no_zebra_ptm_enable_if,
342 no_zebra_ptm_enable_if_cmd,
343 "no ptm-enable",
344 NO_STR
345 "Enable neighbor check with specified topology\n")
346 {
347 VTY_DECLVAR_CONTEXT(interface, ifp);
348 int send_linkup = 0;
349 struct zebra_if *if_data;
350
351 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
352 if (!if_is_operative(ifp))
353 send_linkup = 1;
354
355 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
356 if (if_is_no_ptm_operative(ifp) && send_linkup) {
357 if (IS_ZEBRA_DEBUG_EVENT)
358 zlog_debug("%s: Bringing up interface %s",
359 __func__, ifp->name);
360 if_up(ifp);
361 }
362 }
363
364 if_data = ifp->info;
365 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
366
367 return CMD_SUCCESS;
368 }
369
370
371 void zebra_ptm_write(struct vty *vty)
372 {
373 if (ptm_cb.ptm_enable)
374 vty_out(vty, "ptm-enable\n");
375
376 return;
377 }
378
379 static int zebra_ptm_socket_init(void)
380 {
381 int ret;
382 int sock;
383 struct sockaddr_un addr;
384
385 ptm_cb.ptm_sock = -1;
386
387 sock = socket(PF_UNIX, SOCK_STREAM, 0);
388 if (sock < 0)
389 return -1;
390 if (set_nonblocking(sock) < 0) {
391 if (IS_ZEBRA_DEBUG_EVENT)
392 zlog_debug("%s: Unable to set socket non blocking[%s]",
393 __func__, safe_strerror(errno));
394 close(sock);
395 return -1;
396 }
397
398 /* Make server socket. */
399 memset(&addr, 0, sizeof(struct sockaddr_un));
400 addr.sun_family = AF_UNIX;
401 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
402 sizeof(ZEBRA_PTM_SOCK_NAME));
403
404 ret = connect(sock, (struct sockaddr *)&addr,
405 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
406 - 1);
407 if (ret < 0) {
408 if (IS_ZEBRA_DEBUG_EVENT)
409 zlog_debug("%s: Unable to connect to socket %s [%s]",
410 __func__, ZEBRA_PTM_SOCK_NAME,
411 safe_strerror(errno));
412 close(sock);
413 return -1;
414 }
415 ptm_cb.ptm_sock = sock;
416 return sock;
417 }
418
419 static void zebra_ptm_install_commands(void)
420 {
421 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
422 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
423 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
424 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
425 }
426
427 /* BFD session goes down, send message to the protocols. */
428 static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
429 struct prefix *sp, int status,
430 vrf_id_t vrf_id)
431 {
432 if (IS_ZEBRA_DEBUG_EVENT) {
433 char buf[2][INET6_ADDRSTRLEN];
434
435 if (ifp) {
436 zlog_debug(
437 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
438 " %s event",
439 inet_ntop(dp->family, &dp->u.prefix, buf[0],
440 INET6_ADDRSTRLEN),
441 dp->prefixlen, ifp->name,
442 bfd_get_status_str(status));
443 } else {
444 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
445
446 zlog_debug(
447 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
448 "with src %s/%d and vrf %s(%u) %s event",
449 inet_ntop(dp->family, &dp->u.prefix, buf[0],
450 INET6_ADDRSTRLEN),
451 dp->prefixlen,
452 inet_ntop(sp->family, &sp->u.prefix, buf[1],
453 INET6_ADDRSTRLEN),
454 sp->prefixlen, VRF_LOGNAME(vrf), vrf_id,
455 bfd_get_status_str(status));
456 }
457 }
458
459 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
460 }
461
462 static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
463 struct interface *ifp)
464 {
465 char bfdst_str[32];
466 char dest_str[64];
467 char src_str[64];
468 char vrf_str[64];
469 struct prefix dest_prefix;
470 struct prefix src_prefix;
471 vrf_id_t vrf_id;
472
473 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
474
475 if (bfdst_str[0] == '\0') {
476 return -1;
477 }
478
479 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
480
481 if (dest_str[0] == '\0') {
482 zlog_debug("%s: Key %s not found in PTM msg", __func__,
483 ZEBRA_PTM_BFDDEST_STR);
484 return -1;
485 }
486
487 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
488
489 if (src_str[0] == '\0') {
490 zlog_debug("%s: Key %s not found in PTM msg", __func__,
491 ZEBRA_PTM_BFDSRC_STR);
492 return -1;
493 }
494
495 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
496
497 if (vrf_str[0] == '\0') {
498 zlog_debug("%s: Key %s not found in PTM msg", __func__,
499 ZEBRA_PTM_BFDVRF_STR);
500 return -1;
501 }
502
503 if (IS_ZEBRA_DEBUG_EVENT)
504 zlog_debug(
505 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
506 " peer [%s] local [%s]",
507 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
508 dest_str, src_str);
509
510 if (str2prefix(dest_str, &dest_prefix) == 0) {
511 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
512 "%s: Peer addr %s not found", __func__, dest_str);
513 return -1;
514 }
515
516 memset(&src_prefix, 0, sizeof(struct prefix));
517 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
518 if (str2prefix(src_str, &src_prefix) == 0) {
519 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
520 "%s: Local addr %s not found", __func__,
521 src_str);
522 return -1;
523 }
524 }
525
526 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
527 vrf_id = ifp->vrf_id;
528 } else {
529 vrf_id = vrf_name_to_id(vrf_str);
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 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD);
714 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
715 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
716 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
717 tmp_buf);
718
719 s = msg;
720
721 STREAM_GETL(s, pid);
722 sprintf(tmp_buf, "%d", pid);
723 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
724 tmp_buf);
725
726 STREAM_GETW(s, dst_p.family);
727
728 if (dst_p.family == AF_INET)
729 dst_p.prefixlen = IPV4_MAX_BYTELEN;
730 else
731 dst_p.prefixlen = IPV6_MAX_BYTELEN;
732
733 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
734 if (dst_p.family == AF_INET) {
735 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
736 ptm_lib_append_msg(ptm_hdl, out_ctxt,
737 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
738 } else {
739 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
740 ptm_lib_append_msg(ptm_hdl, out_ctxt,
741 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
742 }
743
744 STREAM_GETL(s, min_rx_timer);
745 sprintf(tmp_buf, "%d", min_rx_timer);
746 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
747 tmp_buf);
748 STREAM_GETL(s, min_tx_timer);
749 sprintf(tmp_buf, "%d", min_tx_timer);
750 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
751 tmp_buf);
752 STREAM_GETC(s, detect_mul);
753 sprintf(tmp_buf, "%d", detect_mul);
754 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
755 tmp_buf);
756
757 STREAM_GETC(s, multi_hop);
758 if (multi_hop) {
759 sprintf(tmp_buf, "%d", 1);
760 ptm_lib_append_msg(ptm_hdl, out_ctxt,
761 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
762 STREAM_GETW(s, src_p.family);
763
764 if (src_p.family == AF_INET)
765 src_p.prefixlen = IPV4_MAX_BYTELEN;
766 else
767 src_p.prefixlen = IPV6_MAX_BYTELEN;
768
769 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
770 if (src_p.family == AF_INET) {
771 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
772 ptm_lib_append_msg(ptm_hdl, out_ctxt,
773 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
774 } else {
775 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
776 ptm_lib_append_msg(ptm_hdl, out_ctxt,
777 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
778 }
779
780 STREAM_GETC(s, multi_hop_cnt);
781 sprintf(tmp_buf, "%d", multi_hop_cnt);
782 ptm_lib_append_msg(ptm_hdl, out_ctxt,
783 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
784
785 if (zvrf_id(zvrf) != VRF_DEFAULT)
786 ptm_lib_append_msg(ptm_hdl, out_ctxt,
787 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
788 zvrf_name(zvrf));
789 } else {
790 if (dst_p.family == AF_INET6) {
791 STREAM_GETW(s, src_p.family);
792
793 if (src_p.family == AF_INET)
794 src_p.prefixlen = IPV4_MAX_BYTELEN;
795 else
796 src_p.prefixlen = IPV6_MAX_BYTELEN;
797
798 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
799 if (src_p.family == AF_INET) {
800 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
801 sizeof(buf));
802 ptm_lib_append_msg(ptm_hdl, out_ctxt,
803 ZEBRA_PTM_BFD_SRC_IP_FIELD,
804 buf);
805 } else {
806 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
807 sizeof(buf));
808 ptm_lib_append_msg(ptm_hdl, out_ctxt,
809 ZEBRA_PTM_BFD_SRC_IP_FIELD,
810 buf);
811 }
812 }
813 STREAM_GETC(s, len);
814 STREAM_GET(if_name, s, len);
815 if_name[len] = '\0';
816
817 ptm_lib_append_msg(ptm_hdl, out_ctxt,
818 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
819 }
820 STREAM_GETC(s, cbit_set);
821 sprintf(tmp_buf, "%d", cbit_set);
822 ptm_lib_append_msg(ptm_hdl, out_ctxt,
823 ZEBRA_PTM_BFD_CBIT_FIELD, tmp_buf);
824
825 sprintf(tmp_buf, "%d", 1);
826 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
827 tmp_buf);
828
829 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
830
831 if (IS_ZEBRA_DEBUG_SEND)
832 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
833 ptm_cb.out_data);
834 zebra_ptm_send_message(ptm_cb.out_data, data_len);
835
836 return;
837
838 stream_failure:
839 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
840 }
841
842 /* BFD peer/dst deregister */
843 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
844 {
845 struct stream *s;
846 struct prefix src_p;
847 struct prefix dst_p;
848 uint8_t multi_hop;
849 char if_name[INTERFACE_NAMSIZ];
850 uint8_t len;
851 char buf[INET6_ADDRSTRLEN];
852 char tmp_buf[64];
853 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
854 void *out_ctxt;
855 unsigned int pid;
856
857 client->bfd_peer_del_cnt++;
858
859 if (IS_ZEBRA_DEBUG_EVENT)
860 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
861 zebra_route_string(client->proto), hdr->length);
862
863 if (ptm_cb.ptm_sock == -1) {
864 ptm_cb.t_timer = NULL;
865 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
866 ptm_cb.reconnect_time, &ptm_cb.t_timer);
867 return;
868 }
869
870 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
871
872 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD);
873 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
874
875 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
876 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
877 tmp_buf);
878
879 s = msg;
880
881 STREAM_GETL(s, pid);
882 sprintf(tmp_buf, "%d", pid);
883 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
884 tmp_buf);
885
886 STREAM_GETW(s, dst_p.family);
887
888 if (dst_p.family == AF_INET)
889 dst_p.prefixlen = IPV4_MAX_BYTELEN;
890 else
891 dst_p.prefixlen = IPV6_MAX_BYTELEN;
892
893 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
894 if (dst_p.family == AF_INET)
895 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
896 else
897 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
898 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
899
900
901 STREAM_GETC(s, multi_hop);
902 if (multi_hop) {
903 sprintf(tmp_buf, "%d", 1);
904 ptm_lib_append_msg(ptm_hdl, out_ctxt,
905 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
906
907 STREAM_GETW(s, src_p.family);
908
909 if (src_p.family == AF_INET)
910 src_p.prefixlen = IPV4_MAX_BYTELEN;
911 else
912 src_p.prefixlen = IPV6_MAX_BYTELEN;
913
914 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
915 if (src_p.family == AF_INET)
916 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
917 else
918 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
919 ptm_lib_append_msg(ptm_hdl, out_ctxt,
920 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
921
922 if (zvrf_id(zvrf) != VRF_DEFAULT)
923 ptm_lib_append_msg(ptm_hdl, out_ctxt,
924 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
925 zvrf_name(zvrf));
926 } else {
927 if (dst_p.family == AF_INET6) {
928 STREAM_GETW(s, src_p.family);
929
930 if (src_p.family == AF_INET)
931 src_p.prefixlen = IPV4_MAX_BYTELEN;
932 else
933 src_p.prefixlen = IPV6_MAX_BYTELEN;
934
935 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
936 if (src_p.family == AF_INET) {
937 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
938 sizeof(buf));
939 ptm_lib_append_msg(ptm_hdl, out_ctxt,
940 ZEBRA_PTM_BFD_SRC_IP_FIELD,
941 buf);
942 } else {
943 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
944 sizeof(buf));
945 ptm_lib_append_msg(ptm_hdl, out_ctxt,
946 ZEBRA_PTM_BFD_SRC_IP_FIELD,
947 buf);
948 }
949 }
950
951 STREAM_GETC(s, len);
952 STREAM_GET(if_name, s, len);
953 if_name[len] = '\0';
954
955 ptm_lib_append_msg(ptm_hdl, out_ctxt,
956 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
957 }
958
959 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
960 if (IS_ZEBRA_DEBUG_SEND)
961 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
962 ptm_cb.out_data);
963
964 zebra_ptm_send_message(ptm_cb.out_data, data_len);
965
966 return;
967
968 stream_failure:
969 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
970 }
971
972 /* BFD client register */
973 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
974 {
975 struct stream *s;
976 unsigned int pid;
977 void *out_ctxt = NULL;
978 char tmp_buf[64];
979 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
980
981 client->bfd_client_reg_cnt++;
982
983 if (IS_ZEBRA_DEBUG_EVENT)
984 zlog_debug("bfd_client_register msg from client %s: length=%d",
985 zebra_route_string(client->proto), hdr->length);
986
987 s = msg;
988 STREAM_GETL(s, pid);
989
990 if (ptm_cb.ptm_sock == -1) {
991 ptm_cb.t_timer = NULL;
992 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
993 ptm_cb.reconnect_time, &ptm_cb.t_timer);
994 return;
995 }
996
997 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
998
999 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
1000 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1001
1002 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
1003 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1004 tmp_buf);
1005
1006 sprintf(tmp_buf, "%d", pid);
1007 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
1008 tmp_buf);
1009
1010 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1011
1012 if (IS_ZEBRA_DEBUG_SEND)
1013 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1014 ptm_cb.out_data);
1015 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1016
1017 SET_FLAG(ptm_cb.client_flags[client->proto],
1018 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1019
1020 return;
1021
1022 stream_failure:
1023 /*
1024 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1025 * is allocated then we need to add this code back in
1026 *
1027 * if (out_ctxt)
1028 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1029 */
1030 return;
1031 }
1032
1033 /* BFD client deregister */
1034 int zebra_ptm_bfd_client_deregister(struct zserv *client)
1035 {
1036 uint8_t proto = client->proto;
1037 void *out_ctxt;
1038 char tmp_buf[64];
1039 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1040
1041 if (!IS_BFD_ENABLED_PROTOCOL(proto))
1042 return 0;
1043
1044 if (IS_ZEBRA_DEBUG_EVENT)
1045 zlog_debug("bfd_client_deregister msg for client %s",
1046 zebra_route_string(proto));
1047
1048 if (ptm_cb.ptm_sock == -1) {
1049 ptm_cb.t_timer = NULL;
1050 thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
1051 ptm_cb.reconnect_time, &ptm_cb.t_timer);
1052 return 0;
1053 }
1054
1055 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
1056
1057 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1058 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1059
1060 sprintf(tmp_buf, "%s", zebra_route_string(proto));
1061 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1062 tmp_buf);
1063
1064 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1065
1066 if (IS_ZEBRA_DEBUG_SEND)
1067 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1068 ptm_cb.out_data);
1069
1070 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1071 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1072
1073 return 0;
1074 }
1075
1076 int zebra_ptm_get_enable_state(void)
1077 {
1078 return ptm_cb.ptm_enable;
1079 }
1080
1081 /*
1082 * zebra_ptm_get_status_str - Convert status to a display string.
1083 */
1084 static const char *zebra_ptm_get_status_str(int status)
1085 {
1086 switch (status) {
1087 case ZEBRA_PTM_STATUS_DOWN:
1088 return "fail";
1089 case ZEBRA_PTM_STATUS_UP:
1090 return "pass";
1091 case ZEBRA_PTM_STATUS_UNKNOWN:
1092 default:
1093 return "n/a";
1094 }
1095 }
1096
1097 void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
1098 {
1099 vty_out(vty, " PTM status: ");
1100 if (ifp->ptm_enable) {
1101 vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1102 } else {
1103 vty_out(vty, "disabled\n");
1104 }
1105 }
1106
1107 void zebra_ptm_send_status_req(void)
1108 {
1109 void *out_ctxt;
1110 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1111
1112 if (ptm_cb.ptm_enable) {
1113 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1114 &out_ctxt);
1115 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1116 ZEBRA_PTM_GET_STATUS_CMD);
1117 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1118
1119 zebra_ptm_send_message(ptm_cb.out_data, len);
1120 }
1121 }
1122
1123 void zebra_ptm_reset_status(int ptm_disable)
1124 {
1125 struct vrf *vrf;
1126 struct interface *ifp;
1127 int send_linkup;
1128
1129 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
1130 FOR_ALL_INTERFACES (vrf, ifp) {
1131 send_linkup = 0;
1132 if (ifp->ptm_enable) {
1133 if (!if_is_operative(ifp))
1134 send_linkup = 1;
1135
1136 if (ptm_disable)
1137 ifp->ptm_enable =
1138 ZEBRA_IF_PTM_ENABLE_OFF;
1139 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1140
1141 if (if_is_operative(ifp) && send_linkup) {
1142 if (IS_ZEBRA_DEBUG_EVENT)
1143 zlog_debug(
1144 "%s: Bringing up interface %s",
1145 __func__, ifp->name);
1146 if_up(ifp);
1147 }
1148 }
1149 }
1150 }
1151
1152 void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
1153 {
1154 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
1155 }
1156
1157 void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1158 struct zebra_if *zebra_ifp)
1159 {
1160 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1161 ifp->ptm_enable = zebra_ifp->ptm_enable;
1162 }
1163
1164 void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
1165 {
1166 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1167 vty_out(vty, " no ptm-enable\n");
1168 }
1169
1170 #else /* HAVE_BFDD */
1171
1172 #include "zebra/zebra_memory.h"
1173
1174 /*
1175 * Data structures.
1176 */
1177 struct ptm_process {
1178 struct zserv *pp_zs;
1179 pid_t pp_pid;
1180
1181 TAILQ_ENTRY(ptm_process) pp_entry;
1182 };
1183 TAILQ_HEAD(ppqueue, ptm_process) ppqueue;
1184
1185 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS,
1186 "PTM BFD process registration table.");
1187
1188 /*
1189 * Prototypes.
1190 */
1191 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs);
1192 static struct ptm_process *pp_lookup_byzs(struct zserv *zs);
1193 static void pp_free(struct ptm_process *pp);
1194 static void pp_free_all(void);
1195
1196 static void zebra_ptm_send_bfdd(struct stream *msg);
1197 static void zebra_ptm_send_clients(struct stream *msg);
1198 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs);
1199 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1200 struct stream *msg, uint32_t command);
1201
1202
1203 /*
1204 * Process PID registration.
1205 */
1206 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs)
1207 {
1208 struct ptm_process *pp;
1209
1210 #ifdef PTM_DEBUG
1211 /* Sanity check: more than one client can't have the same PID. */
1212 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1213 if (pp->pp_pid == pid && pp->pp_zs != zs)
1214 zlog_err("%s:%d pid and client pointer doesn't match",
1215 __FILE__, __LINE__);
1216 }
1217 #endif /* PTM_DEBUG */
1218
1219 /* Lookup for duplicates. */
1220 pp = pp_lookup_byzs(zs);
1221 if (pp != NULL)
1222 return pp;
1223
1224 /* Allocate and register new process. */
1225 pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp));
1226
1227 pp->pp_pid = pid;
1228 pp->pp_zs = zs;
1229 TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry);
1230
1231 return pp;
1232 }
1233
1234 static struct ptm_process *pp_lookup_byzs(struct zserv *zs)
1235 {
1236 struct ptm_process *pp;
1237
1238 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1239 if (pp->pp_zs != zs)
1240 continue;
1241
1242 break;
1243 }
1244
1245 return pp;
1246 }
1247
1248 static void pp_free(struct ptm_process *pp)
1249 {
1250 if (pp == NULL)
1251 return;
1252
1253 TAILQ_REMOVE(&ppqueue, pp, pp_entry);
1254 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp);
1255 }
1256
1257 static void pp_free_all(void)
1258 {
1259 struct ptm_process *pp;
1260
1261 while (!TAILQ_EMPTY(&ppqueue)) {
1262 pp = TAILQ_FIRST(&ppqueue);
1263 pp_free(pp);
1264 }
1265 }
1266
1267
1268 /*
1269 * Use the FRR's internal daemon implementation.
1270 */
1271 static void zebra_ptm_send_bfdd(struct stream *msg)
1272 {
1273 struct listnode *node;
1274 struct zserv *client;
1275 struct stream *msgc;
1276
1277 /* Create copy for replication. */
1278 msgc = stream_dup(msg);
1279 if (msgc == NULL) {
1280 zlog_debug("%s: not enough memory", __func__);
1281 return;
1282 }
1283
1284 /* Send message to all running BFDd daemons. */
1285 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1286 if (client->proto != ZEBRA_ROUTE_BFD)
1287 continue;
1288
1289 zserv_send_message(client, msg);
1290
1291 /* Allocate more messages. */
1292 msg = stream_dup(msgc);
1293 if (msg == NULL) {
1294 zlog_debug("%s: not enough memory", __func__);
1295 return;
1296 }
1297 }
1298
1299 stream_free(msgc);
1300 stream_free(msg);
1301 }
1302
1303 static void zebra_ptm_send_clients(struct stream *msg)
1304 {
1305 struct listnode *node;
1306 struct zserv *client;
1307 struct stream *msgc;
1308
1309 /* Create copy for replication. */
1310 msgc = stream_dup(msg);
1311 if (msgc == NULL) {
1312 zlog_debug("%s: not enough memory", __func__);
1313 return;
1314 }
1315
1316 /* Send message to all running client daemons. */
1317 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1318 if (!IS_BFD_ENABLED_PROTOCOL(client->proto))
1319 continue;
1320
1321 zserv_send_message(client, msg);
1322
1323 /* Allocate more messages. */
1324 msg = stream_dup(msgc);
1325 if (msg == NULL) {
1326 zlog_debug("%s: not enough memory", __func__);
1327 return;
1328 }
1329 }
1330
1331 stream_free(msgc);
1332 stream_free(msg);
1333 }
1334
1335 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs)
1336 {
1337 struct stream *msg;
1338 struct ptm_process *pp;
1339
1340 if (!IS_BFD_ENABLED_PROTOCOL(zs->proto))
1341 return 0;
1342
1343 /* Find daemon pid by zebra connection pointer. */
1344 pp = pp_lookup_byzs(zs);
1345 if (pp == NULL) {
1346 zlog_err("%s:%d failed to find process pid registration",
1347 __FILE__, __LINE__);
1348 return -1;
1349 }
1350
1351 /* Generate, send message and free() daemon related data. */
1352 msg = stream_new(ZEBRA_MAX_PACKET_SIZ);
1353 if (msg == NULL) {
1354 zlog_debug("%s: not enough memory", __func__);
1355 return 0;
1356 }
1357
1358 /*
1359 * The message type will be BFD_DEST_REPLY so we can use only
1360 * one callback at the `bfdd` side, however the real command
1361 * number will be included right after the zebra header.
1362 */
1363 zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0);
1364 stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER);
1365
1366 /* Put process PID. */
1367 stream_putl(msg, pp->pp_pid);
1368
1369 /* Update the data pointers. */
1370 stream_putw_at(msg, 0, stream_get_endp(msg));
1371
1372 zebra_ptm_send_bfdd(msg);
1373
1374 pp_free(pp);
1375
1376 return 0;
1377 }
1378
1379 void zebra_ptm_init(void)
1380 {
1381 /* Initialize the ptm process information list. */
1382 TAILQ_INIT(&ppqueue);
1383
1384 /*
1385 * Send deregistration messages to BFD daemon when some other
1386 * daemon closes. This will help avoid sending daemons
1387 * unnecessary notification messages.
1388 */
1389 hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1390 }
1391
1392 void zebra_ptm_finish(void)
1393 {
1394 /* Remove the client disconnect hook and free all memory. */
1395 hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1396 pp_free_all();
1397 }
1398
1399
1400 /*
1401 * Message handling.
1402 */
1403 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1404 struct stream *msg, uint32_t command)
1405 {
1406 struct stream *msgc;
1407 char buf[ZEBRA_MAX_PACKET_SIZ];
1408 pid_t ppid;
1409
1410 /* Create BFD header */
1411 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1412 zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, zvrf->vrf->vrf_id);
1413 stream_putl(msgc, command);
1414
1415 if (STREAM_READABLE(msg) > STREAM_WRITEABLE(msgc)) {
1416 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1417 goto stream_failure;
1418 }
1419
1420 /* Copy original message, excluding header, into new message */
1421 stream_get_from(buf, msg, stream_get_getp(msg), STREAM_READABLE(msg));
1422 stream_put(msgc, buf, STREAM_READABLE(msg));
1423
1424 /* Update length field */
1425 stream_putw_at(msgc, 0, STREAM_READABLE(msgc));
1426
1427 zebra_ptm_send_bfdd(msgc);
1428 msgc = NULL;
1429
1430 /* Registrate process PID for shutdown hook. */
1431 STREAM_GETL(msg, ppid);
1432 pp_new(ppid, zs);
1433
1434 return;
1435
1436 stream_failure:
1437 if (msgc)
1438 stream_free(msgc);
1439 zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__);
1440 }
1441
1442 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
1443 {
1444 if (IS_ZEBRA_DEBUG_EVENT)
1445 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1446 zebra_route_string(client->proto), hdr->length);
1447
1448 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REGISTER);
1449 }
1450
1451 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
1452 {
1453 if (IS_ZEBRA_DEBUG_EVENT)
1454 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1455 zebra_route_string(client->proto), hdr->length);
1456
1457 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_DEREGISTER);
1458 }
1459
1460 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
1461 {
1462 if (IS_ZEBRA_DEBUG_EVENT)
1463 zlog_debug("bfd_client_register msg from client %s: length=%d",
1464 zebra_route_string(client->proto), hdr->length);
1465
1466 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_CLIENT_REGISTER);
1467 }
1468
1469 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)
1470 {
1471 struct stream *msgc;
1472 size_t zmsglen, zhdrlen;
1473 uint32_t cmd;
1474
1475 /*
1476 * NOTE:
1477 * Replay messages with HAVE_BFDD are meant to be replayed to
1478 * the client daemons. These messages are composed and
1479 * originated from the `bfdd` daemon.
1480 */
1481 if (IS_ZEBRA_DEBUG_EVENT)
1482 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1483 zebra_route_string(client->proto), hdr->length);
1484
1485 /*
1486 * Client messages must be re-routed, otherwise do the `bfdd`
1487 * special treatment.
1488 */
1489 if (client->proto != ZEBRA_ROUTE_BFD) {
1490 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REPLAY);
1491 return;
1492 }
1493
1494 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1495 if (stream_getl2(msg, &cmd) == false) {
1496 zlog_err("%s: expected at least 4 bytes (command)", __func__);
1497 return;
1498 }
1499
1500 /*
1501 * Don't modify message in the zebra API. In order to do that we
1502 * need to allocate a new message stream and copy the message
1503 * provided by zebra.
1504 */
1505 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1506 if (msgc == NULL) {
1507 zlog_debug("%s: not enough memory", __func__);
1508 return;
1509 }
1510
1511 /* Calculate our header size plus the message contents. */
1512 if (cmd != ZEBRA_BFD_DEST_REPLAY) {
1513 zhdrlen = ZEBRA_HEADER_SIZE;
1514 zmsglen = msg->endp - msg->getp;
1515 memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
1516
1517 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1518
1519 msgc->getp = 0;
1520 msgc->endp = zhdrlen + zmsglen;
1521 } else
1522 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1523
1524 /* Update the data pointers. */
1525 stream_putw_at(msgc, 0, stream_get_endp(msgc));
1526
1527 zebra_ptm_send_clients(msgc);
1528 }
1529
1530 /*
1531 * Unused functions.
1532 */
1533 void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__)))
1534 {
1535 /* NOTHING */
1536 }
1537
1538 int zebra_ptm_get_enable_state(void)
1539 {
1540 return 0;
1541 }
1542
1543 void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)),
1544 struct interface *ifp __attribute__((__unused__)))
1545 {
1546 /* NOTHING */
1547 }
1548
1549 void zebra_ptm_write(struct vty *vty __attribute__((__unused__)))
1550 {
1551 /* NOTHING */
1552 }
1553
1554 void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)),
1555 struct zebra_if *zifp __attribute__((__unused__)))
1556 {
1557 /* NOTHING */
1558 }
1559 void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)),
1560 struct zebra_if *zi __attribute__((__unused__)))
1561 {
1562 /* NOTHING */
1563 }
1564
1565 #endif /* HAVE_BFDD */