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 
zebra_ptm_init(void)113 void zebra_ptm_init(void)
114 {
115 	char buf[64];
116 
117 	memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb));
118 
119 	ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
120 	if (!ptm_cb.out_data) {
121 		zlog_debug("%s: Allocation of send data failed", __func__);
122 		return;
123 	}
124 
125 	ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
126 	if (!ptm_cb.in_data) {
127 		zlog_debug("%s: Allocation of recv data failed", __func__);
128 		free(ptm_cb.out_data);
129 		return;
130 	}
131 
132 	ptm_cb.pid = getpid();
133 	zebra_ptm_install_commands();
134 
135 	snprintf(buf, sizeof(buf), "%s", FRR_PTM_NAME);
136 	ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb,
137 				   zebra_ptm_handle_msg_cb);
138 	ptm_cb.wb = buffer_new(0);
139 
140 	ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
141 
142 	ptm_cb.ptm_sock = -1;
143 
144 	hook_register(zserv_client_close, zebra_ptm_bfd_client_deregister);
145 }
146 
zebra_ptm_finish(void)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 
zebra_ptm_flush_messages(struct thread * thread)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 
zebra_ptm_send_message(char * data,int size)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 
zebra_ptm_connect(struct thread * t)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 
zebra_ptm_write(struct vty * vty)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 
zebra_ptm_socket_init(void)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 
zebra_ptm_install_commands(void)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. */
if_bfd_session_update(struct interface * ifp,struct prefix * dp,struct prefix * sp,int status,vrf_id_t vrf_id)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 %s event",
438 				inet_ntop(dp->family, &dp->u.prefix, buf[0],
439 					  INET6_ADDRSTRLEN),
440 				dp->prefixlen, ifp->name,
441 				bfd_get_status_str(status));
442 		} else {
443 			struct vrf *vrf = vrf_lookup_by_id(vrf_id);
444 
445 			zlog_debug(
446 				"MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
447 				inet_ntop(dp->family, &dp->u.prefix, buf[0],
448 					  INET6_ADDRSTRLEN),
449 				dp->prefixlen,
450 				inet_ntop(sp->family, &sp->u.prefix, buf[1],
451 					  INET6_ADDRSTRLEN),
452 				sp->prefixlen, VRF_LOGNAME(vrf), vrf_id,
453 				bfd_get_status_str(status));
454 		}
455 	}
456 
457 	zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
458 }
459 
zebra_ptm_handle_bfd_msg(void * arg,void * in_ctxt,struct interface * ifp)460 static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
461 				    struct interface *ifp)
462 {
463 	char bfdst_str[32];
464 	char dest_str[64];
465 	char src_str[64];
466 	char vrf_str[64];
467 	struct prefix dest_prefix;
468 	struct prefix src_prefix;
469 	vrf_id_t vrf_id;
470 
471 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
472 
473 	if (bfdst_str[0] == '\0') {
474 		return -1;
475 	}
476 
477 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
478 
479 	if (dest_str[0] == '\0') {
480 		zlog_debug("%s: Key %s not found in PTM msg", __func__,
481 			   ZEBRA_PTM_BFDDEST_STR);
482 		return -1;
483 	}
484 
485 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
486 
487 	if (src_str[0] == '\0') {
488 		zlog_debug("%s: Key %s not found in PTM msg", __func__,
489 			   ZEBRA_PTM_BFDSRC_STR);
490 		return -1;
491 	}
492 
493 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
494 
495 	if (vrf_str[0] == '\0') {
496 		zlog_debug("%s: Key %s not found in PTM msg", __func__,
497 			   ZEBRA_PTM_BFDVRF_STR);
498 		return -1;
499 	}
500 
501 	if (IS_ZEBRA_DEBUG_EVENT)
502 		zlog_debug(
503 			"%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
504 			__func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
505 			dest_str, src_str);
506 
507 	if (str2prefix(dest_str, &dest_prefix) == 0) {
508 		flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
509 			 "%s: Peer addr %s not found", __func__, dest_str);
510 		return -1;
511 	}
512 
513 	memset(&src_prefix, 0, sizeof(struct prefix));
514 	if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
515 		if (str2prefix(src_str, &src_prefix) == 0) {
516 			flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
517 				 "%s: Local addr %s not found", __func__,
518 				 src_str);
519 			return -1;
520 		}
521 	}
522 
523 	if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
524 		vrf_id = ifp->vrf_id;
525 	} else {
526 		vrf_id = vrf_name_to_id(vrf_str);
527 	}
528 
529 	if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) {
530 		if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
531 				      BFD_STATUS_DOWN, vrf_id);
532 	} else {
533 		if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
534 				      BFD_STATUS_UP, vrf_id);
535 	}
536 
537 	return 0;
538 }
539 
zebra_ptm_handle_cbl_msg(void * arg,void * in_ctxt,struct interface * ifp,char * cbl_str)540 static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt,
541 				    struct interface *ifp, char *cbl_str)
542 {
543 	int send_linkup = 0;
544 
545 	if (IS_ZEBRA_DEBUG_EVENT)
546 		zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__,
547 			   ifp->name, cbl_str);
548 
549 	if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR)
550 	    && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) {
551 
552 		if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)
553 			send_linkup = 1;
554 		ifp->ptm_status = ZEBRA_PTM_STATUS_UP;
555 		if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)
556 		    && send_linkup)
557 			if_up(ifp);
558 	} else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR)
559 		   && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) {
560 		ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN;
561 		if (ifp->ptm_enable && if_is_no_ptm_operative(ifp))
562 			if_down(ifp);
563 	}
564 
565 	return 0;
566 }
567 
568 /*
569  * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
570  *  all the command responses and notifications received from PTM.
571  *
572  * Command responses: Upon establishing connection with PTM, Zebra requests
573  *  status of all interfaces using 'get-status' command if global ptm-enable
574  *  knob is enabled. As a response to the get-status command PTM sends status
575  *  of all the interfaces as command responses. All other type of command
576  *  responses with cmd_status key word  are dropped. The sole purpose of
577  *  registering this function as callback for the command responses is to
578  *  handle the responses to get-status command.
579  *
580  * Notifications: Cable status and BFD session status changes are sent as
581  *  notifications by PTM. So, this function is also the callback function for
582  *  processing all the notifications from the PTM.
583  *
584  */
zebra_ptm_handle_msg_cb(void * arg,void * in_ctxt)585 static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt)
586 {
587 	struct interface *ifp = NULL;
588 	char port_str[128];
589 	char cbl_str[32];
590 	char cmd_status_str[32];
591 
592 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR,
593 				cmd_status_str);
594 
595 	/* Drop command response messages */
596 	if (cmd_status_str[0] != '\0') {
597 		return 0;
598 	}
599 
600 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str);
601 
602 	if (port_str[0] == '\0') {
603 		zlog_debug("%s: Key %s not found in PTM msg", __func__,
604 			   ZEBRA_PTM_PORT_STR);
605 		return -1;
606 	}
607 
608 	if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) {
609 		ifp = if_lookup_by_name_all_vrf(port_str);
610 
611 		if (!ifp) {
612 			flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
613 				  "%s: %s not found in interface list",
614 				  __func__, port_str);
615 			return -1;
616 		}
617 	}
618 
619 	ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
620 
621 	if (cbl_str[0] == '\0') {
622 		return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
623 	} else {
624 		if (ifp) {
625 			return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
626 							cbl_str);
627 		} else {
628 			return -1;
629 		}
630 	}
631 }
632 
zebra_ptm_sock_read(struct thread * thread)633 int zebra_ptm_sock_read(struct thread *thread)
634 {
635 	int sock;
636 	int rc;
637 
638 	errno = 0;
639 	sock = THREAD_FD(thread);
640 
641 	if (sock == -1)
642 		return -1;
643 
644 	/* PTM communicates in CSV format */
645 	do {
646 		rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
647 					 ZEBRA_PTM_MAX_SOCKBUF, NULL);
648 	} while (rc > 0);
649 
650 	if (((rc == 0) && !errno)
651 	    || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
652 		flog_err_sys(EC_LIB_SOCKET,
653 			     "%s routing socket error: %s(%d) bytes %d",
654 			     __func__, safe_strerror(errno), errno, rc);
655 
656 		close(ptm_cb.ptm_sock);
657 		ptm_cb.ptm_sock = -1;
658 		zebra_ptm_reset_status(0);
659 		ptm_cb.t_timer = NULL;
660 		thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
661 				 ptm_cb.reconnect_time,
662 				 &ptm_cb.t_timer);
663 		return -1;
664 	}
665 
666 	ptm_cb.t_read = NULL;
667 	thread_add_read(zrouter.master, zebra_ptm_sock_read, NULL,
668 			ptm_cb.ptm_sock, &ptm_cb.t_read);
669 
670 	return 0;
671 }
672 
673 /* BFD peer/dst register/update */
zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)674 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
675 {
676 	struct stream *s;
677 	struct prefix src_p;
678 	struct prefix dst_p;
679 	uint8_t multi_hop;
680 	uint8_t multi_hop_cnt;
681 	uint8_t detect_mul;
682 	unsigned int min_rx_timer;
683 	unsigned int min_tx_timer;
684 	char if_name[INTERFACE_NAMSIZ];
685 	uint8_t len;
686 	void *out_ctxt;
687 	char buf[INET6_ADDRSTRLEN];
688 	char tmp_buf[64];
689 	int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
690 	unsigned int pid;
691 	uint8_t cbit_set;
692 
693 	if (hdr->command == ZEBRA_BFD_DEST_UPDATE)
694 		client->bfd_peer_upd8_cnt++;
695 	else
696 		client->bfd_peer_add_cnt++;
697 
698 	if (IS_ZEBRA_DEBUG_EVENT)
699 		zlog_debug("bfd_dst_register msg from client %s: length=%d",
700 			   zebra_route_string(client->proto), hdr->length);
701 
702 	if (ptm_cb.ptm_sock == -1) {
703 		ptm_cb.t_timer = NULL;
704 		thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
705 				 ptm_cb.reconnect_time, &ptm_cb.t_timer);
706 		return;
707 	}
708 
709 	ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
710 	snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_START_CMD);
711 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
712 	snprintf(tmp_buf, sizeof(tmp_buf), "%s",
713 		 zebra_route_string(client->proto));
714 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
715 			   tmp_buf);
716 
717 	s = msg;
718 
719 	STREAM_GETL(s, pid);
720 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
721 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
722 			   tmp_buf);
723 
724 	STREAM_GETW(s, dst_p.family);
725 
726 	if (dst_p.family == AF_INET)
727 		dst_p.prefixlen = IPV4_MAX_BYTELEN;
728 	else
729 		dst_p.prefixlen = IPV6_MAX_BYTELEN;
730 
731 	STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
732 	if (dst_p.family == AF_INET) {
733 		inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
734 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
735 				   ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
736 	} else {
737 		inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
738 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
739 				   ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
740 	}
741 
742 	STREAM_GETL(s, min_rx_timer);
743 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_rx_timer);
744 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
745 			   tmp_buf);
746 	STREAM_GETL(s, min_tx_timer);
747 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_tx_timer);
748 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
749 			   tmp_buf);
750 	STREAM_GETC(s, detect_mul);
751 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", detect_mul);
752 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
753 			   tmp_buf);
754 
755 	STREAM_GETC(s, multi_hop);
756 	if (multi_hop) {
757 		snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
758 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
759 				   ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
760 		STREAM_GETW(s, src_p.family);
761 
762 		if (src_p.family == AF_INET)
763 			src_p.prefixlen = IPV4_MAX_BYTELEN;
764 		else
765 			src_p.prefixlen = IPV6_MAX_BYTELEN;
766 
767 		STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
768 		if (src_p.family == AF_INET) {
769 			inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
770 			ptm_lib_append_msg(ptm_hdl, out_ctxt,
771 					   ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
772 		} else {
773 			inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
774 			ptm_lib_append_msg(ptm_hdl, out_ctxt,
775 					   ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
776 		}
777 
778 		STREAM_GETC(s, multi_hop_cnt);
779 		snprintf(tmp_buf, sizeof(tmp_buf), "%d", multi_hop_cnt);
780 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
781 				   ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
782 
783 		if (zvrf_id(zvrf) != VRF_DEFAULT)
784 			ptm_lib_append_msg(ptm_hdl, out_ctxt,
785 					   ZEBRA_PTM_BFD_VRF_NAME_FIELD,
786 					   zvrf_name(zvrf));
787 	} else {
788 		if (dst_p.family == AF_INET6) {
789 			STREAM_GETW(s, src_p.family);
790 
791 			if (src_p.family == AF_INET)
792 				src_p.prefixlen = IPV4_MAX_BYTELEN;
793 			else
794 				src_p.prefixlen = IPV6_MAX_BYTELEN;
795 
796 			STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
797 			if (src_p.family == AF_INET) {
798 				inet_ntop(AF_INET, &src_p.u.prefix4, buf,
799 					  sizeof(buf));
800 				ptm_lib_append_msg(ptm_hdl, out_ctxt,
801 						   ZEBRA_PTM_BFD_SRC_IP_FIELD,
802 						   buf);
803 			} else {
804 				inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
805 					  sizeof(buf));
806 				ptm_lib_append_msg(ptm_hdl, out_ctxt,
807 						   ZEBRA_PTM_BFD_SRC_IP_FIELD,
808 						   buf);
809 			}
810 		}
811 		STREAM_GETC(s, len);
812 		STREAM_GET(if_name, s, len);
813 		if_name[len] = '\0';
814 
815 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
816 				   ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
817 	}
818 	STREAM_GETC(s, cbit_set);
819 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", cbit_set);
820 	ptm_lib_append_msg(ptm_hdl, out_ctxt,
821 			   ZEBRA_PTM_BFD_CBIT_FIELD, tmp_buf);
822 
823 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
824 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
825 			   tmp_buf);
826 
827 	ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
828 
829 	if (IS_ZEBRA_DEBUG_SEND)
830 		zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
831 			   ptm_cb.out_data);
832 	zebra_ptm_send_message(ptm_cb.out_data, data_len);
833 
834 	return;
835 
836 stream_failure:
837 	ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
838 }
839 
840 /* BFD peer/dst deregister */
zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)841 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
842 {
843 	struct stream *s;
844 	struct prefix src_p;
845 	struct prefix dst_p;
846 	uint8_t multi_hop;
847 	char if_name[INTERFACE_NAMSIZ];
848 	uint8_t len;
849 	char buf[INET6_ADDRSTRLEN];
850 	char tmp_buf[64];
851 	int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
852 	void *out_ctxt;
853 	unsigned int pid;
854 
855 	client->bfd_peer_del_cnt++;
856 
857 	if (IS_ZEBRA_DEBUG_EVENT)
858 		zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
859 			   zebra_route_string(client->proto), hdr->length);
860 
861 	if (ptm_cb.ptm_sock == -1) {
862 		ptm_cb.t_timer = NULL;
863 		thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
864 				 ptm_cb.reconnect_time, &ptm_cb.t_timer);
865 		return;
866 	}
867 
868 	ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
869 
870 	snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_STOP_CMD);
871 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
872 
873 	snprintf(tmp_buf, sizeof(tmp_buf), "%s",
874 		 zebra_route_string(client->proto));
875 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
876 			   tmp_buf);
877 
878 	s = msg;
879 
880 	STREAM_GETL(s, pid);
881 	snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
882 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
883 			   tmp_buf);
884 
885 	STREAM_GETW(s, dst_p.family);
886 
887 	if (dst_p.family == AF_INET)
888 		dst_p.prefixlen = IPV4_MAX_BYTELEN;
889 	else
890 		dst_p.prefixlen = IPV6_MAX_BYTELEN;
891 
892 	STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
893 	if (dst_p.family == AF_INET)
894 		inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
895 	else
896 		inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
897 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
898 
899 
900 	STREAM_GETC(s, multi_hop);
901 	if (multi_hop) {
902 		snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
903 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
904 				   ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
905 
906 		STREAM_GETW(s, src_p.family);
907 
908 		if (src_p.family == AF_INET)
909 			src_p.prefixlen = IPV4_MAX_BYTELEN;
910 		else
911 			src_p.prefixlen = IPV6_MAX_BYTELEN;
912 
913 		STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
914 		if (src_p.family == AF_INET)
915 			inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
916 		else
917 			inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
918 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
919 				   ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
920 
921 		if (zvrf_id(zvrf) != VRF_DEFAULT)
922 			ptm_lib_append_msg(ptm_hdl, out_ctxt,
923 					   ZEBRA_PTM_BFD_VRF_NAME_FIELD,
924 					   zvrf_name(zvrf));
925 	} else {
926 		if (dst_p.family == AF_INET6) {
927 			STREAM_GETW(s, src_p.family);
928 
929 			if (src_p.family == AF_INET)
930 				src_p.prefixlen = IPV4_MAX_BYTELEN;
931 			else
932 				src_p.prefixlen = IPV6_MAX_BYTELEN;
933 
934 			STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
935 			if (src_p.family == AF_INET) {
936 				inet_ntop(AF_INET, &src_p.u.prefix4, buf,
937 					  sizeof(buf));
938 				ptm_lib_append_msg(ptm_hdl, out_ctxt,
939 						   ZEBRA_PTM_BFD_SRC_IP_FIELD,
940 						   buf);
941 			} else {
942 				inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
943 					  sizeof(buf));
944 				ptm_lib_append_msg(ptm_hdl, out_ctxt,
945 						   ZEBRA_PTM_BFD_SRC_IP_FIELD,
946 						   buf);
947 			}
948 		}
949 
950 		STREAM_GETC(s, len);
951 		STREAM_GET(if_name, s, len);
952 		if_name[len] = '\0';
953 
954 		ptm_lib_append_msg(ptm_hdl, out_ctxt,
955 				   ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
956 	}
957 
958 	ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
959 	if (IS_ZEBRA_DEBUG_SEND)
960 		zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
961 			   ptm_cb.out_data);
962 
963 	zebra_ptm_send_message(ptm_cb.out_data, data_len);
964 
965 	return;
966 
967 stream_failure:
968 	ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
969 }
970 
971 /* BFD client register */
zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)972 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
973 {
974 	struct stream *s;
975 	unsigned int pid;
976 	void *out_ctxt = NULL;
977 	char tmp_buf[64];
978 	int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
979 
980 	client->bfd_client_reg_cnt++;
981 
982 	if (IS_ZEBRA_DEBUG_EVENT)
983 		zlog_debug("bfd_client_register msg from client %s: length=%d",
984 			   zebra_route_string(client->proto), hdr->length);
985 
986 	s = msg;
987 	STREAM_GETL(s, pid);
988 
989 	if (ptm_cb.ptm_sock == -1) {
990 		ptm_cb.t_timer = NULL;
991 		thread_add_timer(zrouter.master, zebra_ptm_connect, NULL,
992 				 ptm_cb.reconnect_time, &ptm_cb.t_timer);
993 		return;
994 	}
995 
996 	ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
997 
998 	snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
999 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1000 
1001 	snprintf(tmp_buf, sizeof(tmp_buf), "%s",
1002 		 zebra_route_string(client->proto));
1003 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1004 			   tmp_buf);
1005 
1006 	snprintf(tmp_buf, sizeof(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 */
zebra_ptm_bfd_client_deregister(struct zserv * client)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 	snprintf(tmp_buf, sizeof(tmp_buf), "%s",
1058 		 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1059 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1060 
1061 	snprintf(tmp_buf, sizeof(tmp_buf), "%s", zebra_route_string(proto));
1062 	ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1063 			   tmp_buf);
1064 
1065 	ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1066 
1067 	if (IS_ZEBRA_DEBUG_SEND)
1068 		zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1069 			   ptm_cb.out_data);
1070 
1071 	zebra_ptm_send_message(ptm_cb.out_data, data_len);
1072 	UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1073 
1074 	return 0;
1075 }
1076 
zebra_ptm_get_enable_state(void)1077 int zebra_ptm_get_enable_state(void)
1078 {
1079 	return ptm_cb.ptm_enable;
1080 }
1081 
1082 /*
1083  * zebra_ptm_get_status_str - Convert status to a display string.
1084  */
zebra_ptm_get_status_str(int status)1085 static const char *zebra_ptm_get_status_str(int status)
1086 {
1087 	switch (status) {
1088 	case ZEBRA_PTM_STATUS_DOWN:
1089 		return "fail";
1090 	case ZEBRA_PTM_STATUS_UP:
1091 		return "pass";
1092 	case ZEBRA_PTM_STATUS_UNKNOWN:
1093 	default:
1094 		return "n/a";
1095 	}
1096 }
1097 
zebra_ptm_show_status(struct vty * vty,struct interface * ifp)1098 void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
1099 {
1100 	vty_out(vty, "  PTM status: ");
1101 	if (ifp->ptm_enable) {
1102 		vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1103 	} else {
1104 		vty_out(vty, "disabled\n");
1105 	}
1106 }
1107 
zebra_ptm_send_status_req(void)1108 void zebra_ptm_send_status_req(void)
1109 {
1110 	void *out_ctxt;
1111 	int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1112 
1113 	if (ptm_cb.ptm_enable) {
1114 		ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1115 				 &out_ctxt);
1116 		ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1117 				   ZEBRA_PTM_GET_STATUS_CMD);
1118 		ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1119 
1120 		zebra_ptm_send_message(ptm_cb.out_data, len);
1121 	}
1122 }
1123 
zebra_ptm_reset_status(int ptm_disable)1124 void zebra_ptm_reset_status(int ptm_disable)
1125 {
1126 	struct vrf *vrf;
1127 	struct interface *ifp;
1128 	int send_linkup;
1129 
1130 	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
1131 		FOR_ALL_INTERFACES (vrf, ifp) {
1132 			send_linkup = 0;
1133 			if (ifp->ptm_enable) {
1134 				if (!if_is_operative(ifp))
1135 					send_linkup = 1;
1136 
1137 				if (ptm_disable)
1138 					ifp->ptm_enable =
1139 						ZEBRA_IF_PTM_ENABLE_OFF;
1140 				ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1141 
1142 				if (if_is_operative(ifp) && send_linkup) {
1143 					if (IS_ZEBRA_DEBUG_EVENT)
1144 						zlog_debug(
1145 							"%s: Bringing up interface %s",
1146 							__func__, ifp->name);
1147 					if_up(ifp);
1148 				}
1149 			}
1150 		}
1151 }
1152 
zebra_ptm_if_init(struct zebra_if * zebra_ifp)1153 void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
1154 {
1155 	zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
1156 }
1157 
zebra_ptm_if_set_ptm_state(struct interface * ifp,struct zebra_if * zebra_ifp)1158 void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1159 				struct zebra_if *zebra_ifp)
1160 {
1161 	if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1162 		ifp->ptm_enable = zebra_ifp->ptm_enable;
1163 }
1164 
zebra_ptm_if_write(struct vty * vty,struct zebra_if * zebra_ifp)1165 void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
1166 {
1167 	if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1168 		vty_out(vty, " no ptm-enable\n");
1169 }
1170 
1171 #else /* HAVE_BFDD */
1172 
1173 #include "zebra/zebra_memory.h"
1174 
1175 /*
1176  * Data structures.
1177  */
1178 struct ptm_process {
1179 	struct zserv *pp_zs;
1180 	pid_t pp_pid;
1181 
1182 	TAILQ_ENTRY(ptm_process) pp_entry;
1183 };
1184 TAILQ_HEAD(ppqueue, ptm_process) ppqueue;
1185 
1186 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS,
1187 		    "PTM BFD process registration table.");
1188 
1189 /*
1190  * Prototypes.
1191  */
1192 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs);
1193 static struct ptm_process *pp_lookup_byzs(struct zserv *zs);
1194 static void pp_free(struct ptm_process *pp);
1195 static void pp_free_all(void);
1196 
1197 static void zebra_ptm_send_bfdd(struct stream *msg);
1198 static void zebra_ptm_send_clients(struct stream *msg);
1199 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs);
1200 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1201 			       struct stream *msg, uint32_t command);
1202 
1203 
1204 /*
1205  * Process PID registration.
1206  */
pp_new(pid_t pid,struct zserv * zs)1207 static struct ptm_process *pp_new(pid_t pid, struct zserv *zs)
1208 {
1209 	struct ptm_process *pp;
1210 
1211 #ifdef PTM_DEBUG
1212 	/* Sanity check: more than one client can't have the same PID. */
1213 	TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1214 		if (pp->pp_pid == pid && pp->pp_zs != zs)
1215 			zlog_err("%s:%d pid and client pointer doesn't match",
1216 				 __FILE__, __LINE__);
1217 	}
1218 #endif /* PTM_DEBUG */
1219 
1220 	/* Lookup for duplicates. */
1221 	pp = pp_lookup_byzs(zs);
1222 	if (pp != NULL)
1223 		return pp;
1224 
1225 	/* Allocate and register new process. */
1226 	pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp));
1227 
1228 	pp->pp_pid = pid;
1229 	pp->pp_zs = zs;
1230 	TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry);
1231 
1232 	return pp;
1233 }
1234 
pp_lookup_byzs(struct zserv * zs)1235 static struct ptm_process *pp_lookup_byzs(struct zserv *zs)
1236 {
1237 	struct ptm_process *pp;
1238 
1239 	TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1240 		if (pp->pp_zs != zs)
1241 			continue;
1242 
1243 		break;
1244 	}
1245 
1246 	return pp;
1247 }
1248 
pp_free(struct ptm_process * pp)1249 static void pp_free(struct ptm_process *pp)
1250 {
1251 	if (pp == NULL)
1252 		return;
1253 
1254 	TAILQ_REMOVE(&ppqueue, pp, pp_entry);
1255 	XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp);
1256 }
1257 
pp_free_all(void)1258 static void pp_free_all(void)
1259 {
1260 	struct ptm_process *pp;
1261 
1262 	while (!TAILQ_EMPTY(&ppqueue)) {
1263 		pp = TAILQ_FIRST(&ppqueue);
1264 		pp_free(pp);
1265 	}
1266 }
1267 
1268 
1269 /*
1270  * Use the FRR's internal daemon implementation.
1271  */
zebra_ptm_send_bfdd(struct stream * msg)1272 static void zebra_ptm_send_bfdd(struct stream *msg)
1273 {
1274 	struct listnode *node;
1275 	struct zserv *client;
1276 	struct stream *msgc;
1277 
1278 	/* Create copy for replication. */
1279 	msgc = stream_dup(msg);
1280 	if (msgc == NULL) {
1281 		zlog_debug("%s: not enough memory", __func__);
1282 		return;
1283 	}
1284 
1285 	/* Send message to all running BFDd daemons. */
1286 	for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1287 		if (client->proto != ZEBRA_ROUTE_BFD)
1288 			continue;
1289 
1290 		zserv_send_message(client, msg);
1291 
1292 		/* Allocate more messages. */
1293 		msg = stream_dup(msgc);
1294 		if (msg == NULL) {
1295 			zlog_debug("%s: not enough memory", __func__);
1296 			return;
1297 		}
1298 	}
1299 
1300 	stream_free(msgc);
1301 	stream_free(msg);
1302 }
1303 
zebra_ptm_send_clients(struct stream * msg)1304 static void zebra_ptm_send_clients(struct stream *msg)
1305 {
1306 	struct listnode *node;
1307 	struct zserv *client;
1308 	struct stream *msgc;
1309 
1310 	/* Create copy for replication. */
1311 	msgc = stream_dup(msg);
1312 	if (msgc == NULL) {
1313 		zlog_debug("%s: not enough memory", __func__);
1314 		return;
1315 	}
1316 
1317 	/* Send message to all running client daemons. */
1318 	for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
1319 		if (!IS_BFD_ENABLED_PROTOCOL(client->proto))
1320 			continue;
1321 
1322 		zserv_send_message(client, msg);
1323 
1324 		/* Allocate more messages. */
1325 		msg = stream_dup(msgc);
1326 		if (msg == NULL) {
1327 			zlog_debug("%s: not enough memory", __func__);
1328 			return;
1329 		}
1330 	}
1331 
1332 	stream_free(msgc);
1333 	stream_free(msg);
1334 }
1335 
_zebra_ptm_bfd_client_deregister(struct zserv * zs)1336 static int _zebra_ptm_bfd_client_deregister(struct zserv *zs)
1337 {
1338 	struct stream *msg;
1339 	struct ptm_process *pp;
1340 
1341 	if (!IS_BFD_ENABLED_PROTOCOL(zs->proto))
1342 		return 0;
1343 
1344 	/* Find daemon pid by zebra connection pointer. */
1345 	pp = pp_lookup_byzs(zs);
1346 	if (pp == NULL) {
1347 		zlog_err("%s:%d failed to find process pid registration",
1348 			 __FILE__, __LINE__);
1349 		return -1;
1350 	}
1351 
1352 	/* Generate, send message and free() daemon related data. */
1353 	msg = stream_new(ZEBRA_MAX_PACKET_SIZ);
1354 	if (msg == NULL) {
1355 		zlog_debug("%s: not enough memory", __func__);
1356 		return 0;
1357 	}
1358 
1359 	/*
1360 	 * The message type will be BFD_DEST_REPLY so we can use only
1361 	 * one callback at the `bfdd` side, however the real command
1362 	 * number will be included right after the zebra header.
1363 	 */
1364 	zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0);
1365 	stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER);
1366 
1367 	/* Put process PID. */
1368 	stream_putl(msg, pp->pp_pid);
1369 
1370 	/* Update the data pointers. */
1371 	stream_putw_at(msg, 0, stream_get_endp(msg));
1372 
1373 	zebra_ptm_send_bfdd(msg);
1374 
1375 	pp_free(pp);
1376 
1377 	return 0;
1378 }
1379 
zebra_ptm_init(void)1380 void zebra_ptm_init(void)
1381 {
1382 	/* Initialize the ptm process information list. */
1383 	TAILQ_INIT(&ppqueue);
1384 
1385 	/*
1386 	 * Send deregistration messages to BFD daemon when some other
1387 	 * daemon closes. This will help avoid sending daemons
1388 	 * unnecessary notification messages.
1389 	 */
1390 	hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1391 }
1392 
zebra_ptm_finish(void)1393 void zebra_ptm_finish(void)
1394 {
1395 	/* Remove the client disconnect hook and free all memory. */
1396 	hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1397 	pp_free_all();
1398 }
1399 
1400 
1401 /*
1402  * Message handling.
1403  */
_zebra_ptm_reroute(struct zserv * zs,struct zebra_vrf * zvrf,struct stream * msg,uint32_t command)1404 static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1405 			       struct stream *msg, uint32_t command)
1406 {
1407 	struct stream *msgc;
1408 	char buf[ZEBRA_MAX_PACKET_SIZ];
1409 	pid_t ppid;
1410 
1411 	/* Create BFD header */
1412 	msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1413 	zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, zvrf->vrf->vrf_id);
1414 	stream_putl(msgc, command);
1415 
1416 	if (STREAM_READABLE(msg) > STREAM_WRITEABLE(msgc)) {
1417 		zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1418 		goto stream_failure;
1419 	}
1420 
1421 	/* Copy original message, excluding header, into new message */
1422 	stream_get_from(buf, msg, stream_get_getp(msg), STREAM_READABLE(msg));
1423 	stream_put(msgc, buf, STREAM_READABLE(msg));
1424 
1425 	/* Update length field */
1426 	stream_putw_at(msgc, 0, STREAM_READABLE(msgc));
1427 
1428 	zebra_ptm_send_bfdd(msgc);
1429 	msgc = NULL;
1430 
1431 	/* Registrate process PID for shutdown hook. */
1432 	STREAM_GETL(msg, ppid);
1433 	pp_new(ppid, zs);
1434 
1435 	return;
1436 
1437 stream_failure:
1438 	if (msgc)
1439 		stream_free(msgc);
1440 	zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__);
1441 }
1442 
zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)1443 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
1444 {
1445 	if (IS_ZEBRA_DEBUG_EVENT)
1446 		zlog_debug("bfd_dst_register msg from client %s: length=%d",
1447 			   zebra_route_string(client->proto), hdr->length);
1448 
1449 	_zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REGISTER);
1450 }
1451 
zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)1452 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
1453 {
1454 	if (IS_ZEBRA_DEBUG_EVENT)
1455 		zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1456 			   zebra_route_string(client->proto), hdr->length);
1457 
1458 	_zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_DEREGISTER);
1459 }
1460 
zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)1461 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
1462 {
1463 	if (IS_ZEBRA_DEBUG_EVENT)
1464 		zlog_debug("bfd_client_register msg from client %s: length=%d",
1465 			   zebra_route_string(client->proto), hdr->length);
1466 
1467 	_zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_CLIENT_REGISTER);
1468 }
1469 
zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)1470 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)
1471 {
1472 	struct stream *msgc;
1473 	size_t zmsglen, zhdrlen;
1474 	uint32_t cmd;
1475 
1476 	/*
1477 	 * NOTE:
1478 	 * Replay messages with HAVE_BFDD are meant to be replayed to
1479 	 * the client daemons. These messages are composed and
1480 	 * originated from the `bfdd` daemon.
1481 	 */
1482 	if (IS_ZEBRA_DEBUG_EVENT)
1483 		zlog_debug("bfd_dst_update msg from client %s: length=%d",
1484 			   zebra_route_string(client->proto), hdr->length);
1485 
1486 	/*
1487 	 * Client messages must be re-routed, otherwise do the `bfdd`
1488 	 * special treatment.
1489 	 */
1490 	if (client->proto != ZEBRA_ROUTE_BFD) {
1491 		_zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REPLAY);
1492 		return;
1493 	}
1494 
1495 	/* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1496 	if (stream_getl2(msg, &cmd) == false) {
1497 		zlog_err("%s: expected at least 4 bytes (command)", __func__);
1498 		return;
1499 	}
1500 
1501 	/*
1502 	 * Don't modify message in the zebra API. In order to do that we
1503 	 * need to allocate a new message stream and copy the message
1504 	 * provided by zebra.
1505 	 */
1506 	msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1507 	if (msgc == NULL) {
1508 		zlog_debug("%s: not enough memory", __func__);
1509 		return;
1510 	}
1511 
1512 	/* Calculate our header size plus the message contents. */
1513 	if (cmd != ZEBRA_BFD_DEST_REPLAY) {
1514 		zhdrlen = ZEBRA_HEADER_SIZE;
1515 		zmsglen = msg->endp - msg->getp;
1516 		memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
1517 
1518 		zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1519 
1520 		msgc->getp = 0;
1521 		msgc->endp = zhdrlen + zmsglen;
1522 	} else
1523 		zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1524 
1525 	/* Update the data pointers. */
1526 	stream_putw_at(msgc, 0, stream_get_endp(msgc));
1527 
1528 	zebra_ptm_send_clients(msgc);
1529 }
1530 
1531 /*
1532  * Unused functions.
1533  */
zebra_ptm_if_init(struct zebra_if * zifp)1534 void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__)))
1535 {
1536 	/* NOTHING */
1537 }
1538 
zebra_ptm_get_enable_state(void)1539 int zebra_ptm_get_enable_state(void)
1540 {
1541 	return 0;
1542 }
1543 
zebra_ptm_show_status(struct vty * vty,struct interface * ifp)1544 void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)),
1545 			   struct interface *ifp __attribute__((__unused__)))
1546 {
1547 	/* NOTHING */
1548 }
1549 
zebra_ptm_write(struct vty * vty)1550 void zebra_ptm_write(struct vty *vty __attribute__((__unused__)))
1551 {
1552 	/* NOTHING */
1553 }
1554 
zebra_ptm_if_write(struct vty * vty,struct zebra_if * zifp)1555 void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)),
1556 			struct zebra_if *zifp __attribute__((__unused__)))
1557 {
1558 	/* NOTHING */
1559 }
zebra_ptm_if_set_ptm_state(struct interface * i,struct zebra_if * zi)1560 void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)),
1561 				struct zebra_if *zi __attribute__((__unused__)))
1562 {
1563 	/* NOTHING */
1564 }
1565 
1566 #endif /* HAVE_BFDD */
1567