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