]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ptm.c
*: use clang's 'ForEachMacros' format style option
[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 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
43 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
44
45 #define PTM_MSG_LEN 4
46 #define PTM_HEADER_LEN 37
47
48 const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status";
49 const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess";
50 const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess";
51 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client";
52 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client";
53
54 const char ZEBRA_PTM_CMD_STR[] = "cmd";
55 const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status";
56 const char ZEBRA_PTM_PORT_STR[] = "port";
57 const char ZEBRA_PTM_CBL_STR[] = "cbl status";
58 const char ZEBRA_PTM_PASS_STR[] = "pass";
59 const char ZEBRA_PTM_FAIL_STR[] = "fail";
60 const char ZEBRA_PTM_BFDSTATUS_STR[] = "state";
61 const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up";
62 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down";
63 const char ZEBRA_PTM_BFDDEST_STR[] = "peer";
64 const char ZEBRA_PTM_BFDSRC_STR[] = "local";
65 const char ZEBRA_PTM_BFDVRF_STR[] = "vrf";
66 const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A";
67 const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A";
68 const char ZEBRA_PTM_INVALID_VRF[] = "N/A";
69
70 const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr";
71 const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr";
72 const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx";
73 const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx";
74 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult";
75 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop";
76 const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client";
77 const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid";
78 const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName";
79 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt";
80 const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent";
81 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName";
82
83 static ptm_lib_handle_t *ptm_hdl;
84
85 struct zebra_ptm_cb ptm_cb;
86
87 static int zebra_ptm_socket_init(void);
88 int zebra_ptm_sock_read(struct thread *);
89 static void zebra_ptm_install_commands(void);
90 static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt);
91 void zebra_bfd_peer_replay_req(void);
92 void zebra_ptm_send_status_req(void);
93 void zebra_ptm_reset_status(int ptm_disable);
94
95 const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket";
96
97 void zebra_ptm_init(void)
98 {
99 char buf[64];
100
101 memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb));
102
103 ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
104 if (!ptm_cb.out_data) {
105 zlog_warn("%s: Allocation of send data failed", __func__);
106 return;
107 }
108
109 ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
110 if (!ptm_cb.in_data) {
111 zlog_warn("%s: Allocation of recv data failed", __func__);
112 free(ptm_cb.out_data);
113 return;
114 }
115
116 ptm_cb.pid = getpid();
117 zebra_ptm_install_commands();
118
119 sprintf(buf, "%s", FRR_PTM_NAME);
120 ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb,
121 zebra_ptm_handle_msg_cb);
122 ptm_cb.wb = buffer_new(0);
123
124 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
125
126 ptm_cb.ptm_sock = -1;
127 }
128
129 void zebra_ptm_finish(void)
130 {
131 int proto;
132
133 for (proto = 0; proto < ZEBRA_ROUTE_MAX; proto++)
134 if (CHECK_FLAG(ptm_cb.client_flags[proto],
135 ZEBRA_PTM_BFD_CLIENT_FLAG_REG))
136 zebra_ptm_bfd_client_deregister(proto);
137
138 buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock);
139
140 free(ptm_hdl);
141
142 if (ptm_cb.out_data)
143 free(ptm_cb.out_data);
144
145 if (ptm_cb.in_data)
146 free(ptm_cb.in_data);
147
148 /* Release threads. */
149 if (ptm_cb.t_read)
150 thread_cancel(ptm_cb.t_read);
151 if (ptm_cb.t_write)
152 thread_cancel(ptm_cb.t_write);
153 if (ptm_cb.t_timer)
154 thread_cancel(ptm_cb.t_timer);
155
156 if (ptm_cb.wb)
157 buffer_free(ptm_cb.wb);
158
159 if (ptm_cb.ptm_sock >= 0)
160 close(ptm_cb.ptm_sock);
161 }
162
163 static int zebra_ptm_flush_messages(struct thread *thread)
164 {
165 ptm_cb.t_write = NULL;
166
167 if (ptm_cb.ptm_sock == -1)
168 return -1;
169
170 errno = 0;
171
172 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
173 case BUFFER_ERROR:
174 zlog_warn("%s ptm socket error: %s", __func__,
175 safe_strerror(errno));
176 close(ptm_cb.ptm_sock);
177 ptm_cb.ptm_sock = -1;
178 zebra_ptm_reset_status(0);
179 ptm_cb.t_timer = NULL;
180 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
181 ptm_cb.reconnect_time, &ptm_cb.t_timer);
182 return (-1);
183 case BUFFER_PENDING:
184 ptm_cb.t_write = NULL;
185 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
186 ptm_cb.ptm_sock, &ptm_cb.t_write);
187 break;
188 case BUFFER_EMPTY:
189 break;
190 }
191
192 return (0);
193 }
194
195 static int zebra_ptm_send_message(char *data, int size)
196 {
197 errno = 0;
198 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
199 case BUFFER_ERROR:
200 zlog_warn("%s ptm socket error: %s", __func__,
201 safe_strerror(errno));
202 close(ptm_cb.ptm_sock);
203 ptm_cb.ptm_sock = -1;
204 zebra_ptm_reset_status(0);
205 ptm_cb.t_timer = NULL;
206 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
207 ptm_cb.reconnect_time, &ptm_cb.t_timer);
208 return -1;
209 case BUFFER_EMPTY:
210 THREAD_OFF(ptm_cb.t_write);
211 break;
212 case BUFFER_PENDING:
213 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
214 ptm_cb.ptm_sock, &ptm_cb.t_write);
215 break;
216 }
217
218 return 0;
219 }
220
221 int zebra_ptm_connect(struct thread *t)
222 {
223 int init = 0;
224
225 if (ptm_cb.ptm_sock == -1) {
226 zebra_ptm_socket_init();
227 init = 1;
228 }
229
230 if (ptm_cb.ptm_sock != -1) {
231 if (init) {
232 ptm_cb.t_read = NULL;
233 thread_add_read(zebrad.master, zebra_ptm_sock_read,
234 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
235 zebra_bfd_peer_replay_req();
236 }
237 zebra_ptm_send_status_req();
238 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
239 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
240 ptm_cb.reconnect_time *= 2;
241 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
242 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
243
244 ptm_cb.t_timer = NULL;
245 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
246 ptm_cb.reconnect_time, &ptm_cb.t_timer);
247 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
248 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
249 }
250
251 return (errno);
252 }
253
254 DEFUN (zebra_ptm_enable,
255 zebra_ptm_enable_cmd,
256 "ptm-enable",
257 "Enable neighbor check with specified topology\n")
258 {
259 struct vrf *vrf;
260 struct listnode *i;
261 struct interface *ifp;
262 struct zebra_if *if_data;
263
264 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
265
266 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
267 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, i, ifp))
268 if (!ifp->ptm_enable) {
269 if_data = (struct zebra_if *)ifp->info;
270 if (if_data
271 && (if_data->ptm_enable
272 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
273 ifp->ptm_enable =
274 ZEBRA_IF_PTM_ENABLE_ON;
275 }
276 /* Assign a default unknown status */
277 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
278 }
279
280 zebra_ptm_connect(NULL);
281
282 return CMD_SUCCESS;
283 }
284
285 DEFUN (no_zebra_ptm_enable,
286 no_zebra_ptm_enable_cmd,
287 "no ptm-enable",
288 NO_STR
289 "Enable neighbor check with specified topology\n")
290 {
291 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
292 zebra_ptm_reset_status(1);
293 return CMD_SUCCESS;
294 }
295
296 DEFUN (zebra_ptm_enable_if,
297 zebra_ptm_enable_if_cmd,
298 "ptm-enable",
299 "Enable neighbor check with specified topology\n")
300 {
301 VTY_DECLVAR_CONTEXT(interface, ifp);
302 struct zebra_if *if_data;
303 int old_ptm_enable;
304 int send_linkdown = 0;
305
306 if (ifp->ifindex == IFINDEX_INTERNAL) {
307 return CMD_SUCCESS;
308 }
309
310 old_ptm_enable = ifp->ptm_enable;
311 ifp->ptm_enable = ptm_cb.ptm_enable;
312
313 if (if_is_no_ptm_operative(ifp))
314 send_linkdown = 1;
315
316 if (!old_ptm_enable && ptm_cb.ptm_enable) {
317 if (!if_is_operative(ifp) && send_linkdown) {
318 if (IS_ZEBRA_DEBUG_EVENT)
319 zlog_debug("%s: Bringing down interface %s\n",
320 __func__, ifp->name);
321 if_down(ifp);
322 }
323 }
324
325 if_data = ifp->info;
326 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
327
328 return CMD_SUCCESS;
329 }
330
331 DEFUN (no_zebra_ptm_enable_if,
332 no_zebra_ptm_enable_if_cmd,
333 "no ptm-enable",
334 NO_STR
335 "Enable neighbor check with specified topology\n")
336 {
337 VTY_DECLVAR_CONTEXT(interface, ifp);
338 int send_linkup = 0;
339 struct zebra_if *if_data;
340
341 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
342 if (!if_is_operative(ifp))
343 send_linkup = 1;
344
345 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
346 if (if_is_no_ptm_operative(ifp) && send_linkup) {
347 if (IS_ZEBRA_DEBUG_EVENT)
348 zlog_debug("%s: Bringing up interface %s\n",
349 __func__, ifp->name);
350 if_up(ifp);
351 }
352 }
353
354 if_data = ifp->info;
355 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
356
357 return CMD_SUCCESS;
358 }
359
360
361 void zebra_ptm_write(struct vty *vty)
362 {
363 if (ptm_cb.ptm_enable)
364 vty_out(vty, "ptm-enable\n");
365
366 return;
367 }
368
369 static int zebra_ptm_socket_init(void)
370 {
371 int ret;
372 int sock;
373 struct sockaddr_un addr;
374
375 ptm_cb.ptm_sock = -1;
376
377 sock = socket(PF_UNIX, SOCK_STREAM, 0);
378 if (sock < 0)
379 return -1;
380 if (set_nonblocking(sock) < 0) {
381 if (IS_ZEBRA_DEBUG_EVENT)
382 zlog_debug("%s: Unable to set socket non blocking[%s]",
383 __PRETTY_FUNCTION__, safe_strerror(errno));
384 close(sock);
385 return -1;
386 }
387
388 /* Make server socket. */
389 memset(&addr, 0, sizeof(struct sockaddr_un));
390 addr.sun_family = AF_UNIX;
391 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
392 sizeof(ZEBRA_PTM_SOCK_NAME));
393
394 ret = connect(sock, (struct sockaddr *)&addr,
395 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
396 - 1);
397 if (ret < 0) {
398 if (IS_ZEBRA_DEBUG_EVENT)
399 zlog_debug("%s: Unable to connect to socket %s [%s]",
400 __func__, ZEBRA_PTM_SOCK_NAME,
401 safe_strerror(errno));
402 close(sock);
403 return -1;
404 }
405 ptm_cb.ptm_sock = sock;
406 return sock;
407 }
408
409 static void zebra_ptm_install_commands(void)
410 {
411 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
412 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
413 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
414 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
415 }
416
417 /* BFD session goes down, send message to the protocols. */
418 static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
419 struct prefix *sp, int status,
420 vrf_id_t vrf_id)
421 {
422 if (IS_ZEBRA_DEBUG_EVENT) {
423 char buf[2][INET6_ADDRSTRLEN];
424
425 if (ifp) {
426 zlog_debug(
427 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
428 " %s event",
429 inet_ntop(dp->family, &dp->u.prefix, buf[0],
430 INET6_ADDRSTRLEN),
431 dp->prefixlen, ifp->name,
432 bfd_get_status_str(status));
433 } else {
434 zlog_debug(
435 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
436 "with src %s/%d and vrf %d %s event",
437 inet_ntop(dp->family, &dp->u.prefix, buf[0],
438 INET6_ADDRSTRLEN),
439 dp->prefixlen,
440 inet_ntop(sp->family, &sp->u.prefix, buf[1],
441 INET6_ADDRSTRLEN),
442 sp->prefixlen, vrf_id,
443 bfd_get_status_str(status));
444 }
445 }
446
447 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
448 }
449
450 static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
451 struct interface *ifp)
452 {
453 char bfdst_str[32];
454 char dest_str[64];
455 char src_str[64];
456 char vrf_str[64];
457 struct prefix dest_prefix;
458 struct prefix src_prefix;
459 vrf_id_t vrf_id;
460
461 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
462
463 if (bfdst_str[0] == '\0') {
464 return -1;
465 }
466
467 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
468
469 if (dest_str[0] == '\0') {
470 zlog_debug("%s: Key %s not found in PTM msg", __func__,
471 ZEBRA_PTM_BFDDEST_STR);
472 return -1;
473 }
474
475 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
476
477 if (src_str[0] == '\0') {
478 zlog_debug("%s: Key %s not found in PTM msg", __func__,
479 ZEBRA_PTM_BFDSRC_STR);
480 return -1;
481 }
482
483 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
484
485 if (vrf_str[0] == '\0') {
486 zlog_debug("%s: Key %s not found in PTM msg", __func__,
487 ZEBRA_PTM_BFDVRF_STR);
488 return -1;
489 }
490
491 if (IS_ZEBRA_DEBUG_EVENT)
492 zlog_debug(
493 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
494 " peer [%s] local [%s]",
495 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
496 dest_str, src_str);
497
498 if (str2prefix(dest_str, &dest_prefix) == 0) {
499 zlog_err("%s: Peer addr %s not found", __func__, dest_str);
500 return -1;
501 }
502
503 memset(&src_prefix, 0, sizeof(struct prefix));
504 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
505 if (str2prefix(src_str, &src_prefix) == 0) {
506 zlog_err("%s: Local addr %s not found", __func__,
507 src_str);
508 return -1;
509 }
510 }
511
512 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
513 vrf_id = ifp->vrf_id;
514 } else {
515 vrf_id = vrf_name_to_id(vrf_str);
516 }
517
518 if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) {
519 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
520 BFD_STATUS_DOWN, vrf_id);
521 } else {
522 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
523 BFD_STATUS_UP, vrf_id);
524 }
525
526 return 0;
527 }
528
529 static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt,
530 struct interface *ifp, char *cbl_str)
531 {
532 int send_linkup = 0;
533
534 if (IS_ZEBRA_DEBUG_EVENT)
535 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__,
536 ifp->name, cbl_str);
537
538 if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR)
539 && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) {
540
541 if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)
542 send_linkup = 1;
543 ifp->ptm_status = ZEBRA_PTM_STATUS_UP;
544 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)
545 && send_linkup)
546 if_up(ifp);
547 } else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR)
548 && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) {
549 ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN;
550 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp))
551 if_down(ifp);
552 }
553
554 return 0;
555 }
556
557 /*
558 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
559 * all the command responses and notifications received from PTM.
560 *
561 * Command responses: Upon establishing connection with PTM, Zebra requests
562 * status of all interfaces using 'get-status' command if global ptm-enable
563 * knob is enabled. As a response to the get-status command PTM sends status
564 * of all the interfaces as command responses. All other type of command
565 * responses with cmd_status key word are dropped. The sole purpose of
566 * registering this function as callback for the command responses is to
567 * handle the responses to get-status command.
568 *
569 * Notifications: Cable status and BFD session status changes are sent as
570 * notifications by PTM. So, this function is also the callback function for
571 * processing all the notifications from the PTM.
572 *
573 */
574 static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt)
575 {
576 struct interface *ifp = NULL;
577 char port_str[128];
578 char cbl_str[32];
579 char cmd_status_str[32];
580
581 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR,
582 cmd_status_str);
583
584 /* Drop command response messages */
585 if (cmd_status_str[0] != '\0') {
586 return 0;
587 }
588
589 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str);
590
591 if (port_str[0] == '\0') {
592 zlog_debug("%s: Key %s not found in PTM msg", __func__,
593 ZEBRA_PTM_PORT_STR);
594 return -1;
595 }
596
597 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) {
598 ifp = if_lookup_by_name_all_vrf(port_str);
599
600 if (!ifp) {
601 zlog_err("%s: %s not found in interface list", __func__,
602 port_str);
603 return -1;
604 }
605 }
606
607 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
608
609 if (cbl_str[0] == '\0') {
610 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
611 } else {
612 if (ifp) {
613 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
614 cbl_str);
615 } else {
616 return -1;
617 }
618 }
619 }
620
621 int zebra_ptm_sock_read(struct thread *thread)
622 {
623 int sock, done = 0;
624 int rc;
625
626 errno = 0;
627 sock = THREAD_FD(thread);
628
629 if (sock == -1)
630 return -1;
631
632 /* PTM communicates in CSV format */
633 while (!done) {
634 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
635 ZEBRA_PTM_MAX_SOCKBUF, NULL);
636 if (rc <= 0)
637 break;
638 }
639
640 if (rc <= 0) {
641 if (((rc == 0) && !errno)
642 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
643 zlog_warn("%s routing socket error: %s(%d) bytes %d",
644 __func__, safe_strerror(errno), errno, rc);
645
646 close(ptm_cb.ptm_sock);
647 ptm_cb.ptm_sock = -1;
648 zebra_ptm_reset_status(0);
649 ptm_cb.t_timer = NULL;
650 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
651 ptm_cb.reconnect_time,
652 &ptm_cb.t_timer);
653 return (-1);
654 }
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 int zebra_ptm_bfd_dst_register(struct zserv *client, int sock, u_short length,
666 int command, struct zebra_vrf *zvrf)
667 {
668 struct stream *s;
669 struct prefix src_p;
670 struct prefix dst_p;
671 u_char multi_hop;
672 u_char multi_hop_cnt;
673 u_char detect_mul;
674 unsigned int min_rx_timer;
675 unsigned int min_tx_timer;
676 char if_name[INTERFACE_NAMSIZ];
677 u_char len;
678 void *out_ctxt;
679 char buf[INET6_ADDRSTRLEN];
680 char tmp_buf[64];
681 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
682 unsigned int pid;
683
684 if (command == ZEBRA_BFD_DEST_UPDATE)
685 client->bfd_peer_upd8_cnt++;
686 else
687 client->bfd_peer_add_cnt++;
688
689 if (IS_ZEBRA_DEBUG_EVENT)
690 zlog_debug("bfd_dst_register msg from client %s: length=%d",
691 zebra_route_string(client->proto), length);
692
693 if (ptm_cb.ptm_sock == -1) {
694 ptm_cb.t_timer = NULL;
695 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
696 ptm_cb.reconnect_time, &ptm_cb.t_timer);
697 return -1;
698 }
699
700 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
701 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD);
702 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
703 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
704 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
705 tmp_buf);
706
707 s = client->ibuf;
708
709 pid = stream_getl(s);
710 sprintf(tmp_buf, "%d", pid);
711 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
712 tmp_buf);
713
714 dst_p.family = stream_getw(s);
715
716 if (dst_p.family == AF_INET)
717 dst_p.prefixlen = IPV4_MAX_BYTELEN;
718 else
719 dst_p.prefixlen = IPV6_MAX_BYTELEN;
720
721 stream_get(&dst_p.u.prefix, s, dst_p.prefixlen);
722 if (dst_p.family == AF_INET) {
723 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
724 ptm_lib_append_msg(ptm_hdl, out_ctxt,
725 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
726 } else {
727 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
728 ptm_lib_append_msg(ptm_hdl, out_ctxt,
729 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
730 }
731
732 min_rx_timer = stream_getl(s);
733 sprintf(tmp_buf, "%d", min_rx_timer);
734 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
735 tmp_buf);
736 min_tx_timer = stream_getl(s);
737 sprintf(tmp_buf, "%d", min_tx_timer);
738 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
739 tmp_buf);
740 detect_mul = stream_getc(s);
741 sprintf(tmp_buf, "%d", detect_mul);
742 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
743 tmp_buf);
744
745 multi_hop = stream_getc(s);
746 if (multi_hop) {
747 sprintf(tmp_buf, "%d", 1);
748 ptm_lib_append_msg(ptm_hdl, out_ctxt,
749 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
750 src_p.family = stream_getw(s);
751
752 if (src_p.family == AF_INET)
753 src_p.prefixlen = IPV4_MAX_BYTELEN;
754 else
755 src_p.prefixlen = IPV6_MAX_BYTELEN;
756
757 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
758 if (src_p.family == AF_INET) {
759 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
760 ptm_lib_append_msg(ptm_hdl, out_ctxt,
761 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
762 } else {
763 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
764 ptm_lib_append_msg(ptm_hdl, out_ctxt,
765 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
766 }
767
768 multi_hop_cnt = stream_getc(s);
769 sprintf(tmp_buf, "%d", multi_hop_cnt);
770 ptm_lib_append_msg(ptm_hdl, out_ctxt,
771 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
772
773 if (zvrf_id(zvrf) != VRF_DEFAULT)
774 ptm_lib_append_msg(ptm_hdl, out_ctxt,
775 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
776 zvrf_name(zvrf));
777 } else {
778 if (dst_p.family == AF_INET6) {
779 src_p.family = stream_getw(s);
780
781 if (src_p.family == AF_INET)
782 src_p.prefixlen = IPV4_MAX_BYTELEN;
783 else
784 src_p.prefixlen = IPV6_MAX_BYTELEN;
785
786 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
787 if (src_p.family == AF_INET) {
788 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
789 sizeof(buf));
790 ptm_lib_append_msg(ptm_hdl, out_ctxt,
791 ZEBRA_PTM_BFD_SRC_IP_FIELD,
792 buf);
793 } else {
794 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
795 sizeof(buf));
796 ptm_lib_append_msg(ptm_hdl, out_ctxt,
797 ZEBRA_PTM_BFD_SRC_IP_FIELD,
798 buf);
799 }
800 }
801 len = stream_getc(s);
802 stream_get(if_name, s, len);
803 if_name[len] = '\0';
804
805 ptm_lib_append_msg(ptm_hdl, out_ctxt,
806 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
807 }
808
809 sprintf(tmp_buf, "%d", 1);
810 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
811 tmp_buf);
812
813 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
814
815 if (IS_ZEBRA_DEBUG_SEND)
816 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
817 ptm_cb.out_data);
818 zebra_ptm_send_message(ptm_cb.out_data, data_len);
819 return 0;
820 }
821
822 /* BFD peer/dst deregister */
823 int zebra_ptm_bfd_dst_deregister(struct zserv *client, int sock, u_short length,
824 struct zebra_vrf *zvrf)
825 {
826 struct stream *s;
827 struct prefix src_p;
828 struct prefix dst_p;
829 u_char multi_hop;
830 char if_name[INTERFACE_NAMSIZ];
831 u_char len;
832 char buf[INET6_ADDRSTRLEN];
833 char tmp_buf[64];
834 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
835 void *out_ctxt;
836 unsigned int pid;
837
838 client->bfd_peer_del_cnt++;
839
840 if (IS_ZEBRA_DEBUG_EVENT)
841 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
842 zebra_route_string(client->proto), length);
843
844 if (ptm_cb.ptm_sock == -1) {
845 ptm_cb.t_timer = NULL;
846 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
847 ptm_cb.reconnect_time, &ptm_cb.t_timer);
848 return -1;
849 }
850
851 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
852
853 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD);
854 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
855
856 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
857 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
858 tmp_buf);
859
860 s = client->ibuf;
861
862 pid = stream_getl(s);
863 sprintf(tmp_buf, "%d", pid);
864 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
865 tmp_buf);
866
867 dst_p.family = stream_getw(s);
868
869 if (dst_p.family == AF_INET)
870 dst_p.prefixlen = IPV4_MAX_BYTELEN;
871 else
872 dst_p.prefixlen = IPV6_MAX_BYTELEN;
873
874 stream_get(&dst_p.u.prefix, s, dst_p.prefixlen);
875 if (dst_p.family == AF_INET)
876 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
877 else
878 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
879 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
880
881
882 multi_hop = stream_getc(s);
883 if (multi_hop) {
884 sprintf(tmp_buf, "%d", 1);
885 ptm_lib_append_msg(ptm_hdl, out_ctxt,
886 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
887
888 src_p.family = stream_getw(s);
889
890 if (src_p.family == AF_INET)
891 src_p.prefixlen = IPV4_MAX_BYTELEN;
892 else
893 src_p.prefixlen = IPV6_MAX_BYTELEN;
894
895 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
896 if (src_p.family == AF_INET)
897 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
898 else
899 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
900 ptm_lib_append_msg(ptm_hdl, out_ctxt,
901 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
902
903 if (zvrf_id(zvrf) != VRF_DEFAULT)
904 ptm_lib_append_msg(ptm_hdl, out_ctxt,
905 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
906 zvrf_name(zvrf));
907 } else {
908 if (dst_p.family == AF_INET6) {
909 src_p.family = stream_getw(s);
910
911 if (src_p.family == AF_INET)
912 src_p.prefixlen = IPV4_MAX_BYTELEN;
913 else
914 src_p.prefixlen = IPV6_MAX_BYTELEN;
915
916 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
917 if (src_p.family == AF_INET) {
918 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
919 sizeof(buf));
920 ptm_lib_append_msg(ptm_hdl, out_ctxt,
921 ZEBRA_PTM_BFD_SRC_IP_FIELD,
922 buf);
923 } else {
924 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
925 sizeof(buf));
926 ptm_lib_append_msg(ptm_hdl, out_ctxt,
927 ZEBRA_PTM_BFD_SRC_IP_FIELD,
928 buf);
929 }
930 }
931
932 len = stream_getc(s);
933 stream_get(if_name, s, len);
934 if_name[len] = '\0';
935
936 ptm_lib_append_msg(ptm_hdl, out_ctxt,
937 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
938 }
939
940 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
941 if (IS_ZEBRA_DEBUG_SEND)
942 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
943 ptm_cb.out_data);
944
945 zebra_ptm_send_message(ptm_cb.out_data, data_len);
946 return 0;
947 }
948
949 /* BFD client register */
950 int zebra_ptm_bfd_client_register(struct zserv *client, int sock,
951 u_short length)
952 {
953 struct stream *s;
954 unsigned int pid;
955 void *out_ctxt;
956 char tmp_buf[64];
957 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
958
959 client->bfd_client_reg_cnt++;
960
961 if (IS_ZEBRA_DEBUG_EVENT)
962 zlog_debug("bfd_client_register msg from client %s: length=%d",
963 zebra_route_string(client->proto), length);
964
965 if (ptm_cb.ptm_sock == -1) {
966 ptm_cb.t_timer = NULL;
967 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
968 ptm_cb.reconnect_time, &ptm_cb.t_timer);
969 return -1;
970 }
971
972 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
973
974 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
975 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
976
977 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
978 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
979 tmp_buf);
980
981 s = client->ibuf;
982
983 pid = stream_getl(s);
984 sprintf(tmp_buf, "%d", pid);
985 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
986 tmp_buf);
987
988 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
989
990 if (IS_ZEBRA_DEBUG_SEND)
991 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
992 ptm_cb.out_data);
993 zebra_ptm_send_message(ptm_cb.out_data, data_len);
994
995 SET_FLAG(ptm_cb.client_flags[client->proto],
996 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
997 return 0;
998 }
999
1000 /* BFD client deregister */
1001 void zebra_ptm_bfd_client_deregister(int proto)
1002 {
1003 void *out_ctxt;
1004 char tmp_buf[64];
1005 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1006
1007 if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP
1008 && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM)
1009 return;
1010
1011 if (IS_ZEBRA_DEBUG_EVENT)
1012 zlog_err("bfd_client_deregister msg for client %s",
1013 zebra_route_string(proto));
1014
1015 if (ptm_cb.ptm_sock == -1) {
1016 ptm_cb.t_timer = NULL;
1017 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
1018 ptm_cb.reconnect_time, &ptm_cb.t_timer);
1019 return;
1020 }
1021
1022 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
1023
1024 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1025 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
1026
1027 sprintf(tmp_buf, "%s", zebra_route_string(proto));
1028 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1029 tmp_buf);
1030
1031 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
1032
1033 if (IS_ZEBRA_DEBUG_SEND)
1034 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1035 ptm_cb.out_data);
1036
1037 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1038 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
1039 }
1040
1041 int zebra_ptm_get_enable_state(void)
1042 {
1043 return ptm_cb.ptm_enable;
1044 }
1045
1046 /*
1047 * zebra_ptm_get_status_str - Convert status to a display string.
1048 */
1049 static const char *zebra_ptm_get_status_str(int status)
1050 {
1051 switch (status) {
1052 case ZEBRA_PTM_STATUS_DOWN:
1053 return "fail";
1054 case ZEBRA_PTM_STATUS_UP:
1055 return "pass";
1056 case ZEBRA_PTM_STATUS_UNKNOWN:
1057 default:
1058 return "n/a";
1059 }
1060 }
1061
1062 void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
1063 {
1064 vty_out(vty, " PTM status: ");
1065 if (ifp->ptm_enable) {
1066 vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1067 } else {
1068 vty_out(vty, "disabled\n");
1069 }
1070 }
1071
1072 void zebra_ptm_send_status_req(void)
1073 {
1074 void *out_ctxt;
1075 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1076
1077 if (ptm_cb.ptm_enable) {
1078 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1079 &out_ctxt);
1080 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1081 ZEBRA_PTM_GET_STATUS_CMD);
1082 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1083
1084 zebra_ptm_send_message(ptm_cb.out_data, len);
1085 }
1086 }
1087
1088 void zebra_ptm_reset_status(int ptm_disable)
1089 {
1090 struct vrf *vrf;
1091 struct listnode *i;
1092 struct interface *ifp;
1093 int send_linkup;
1094
1095 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
1096 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, i, ifp)) {
1097 send_linkup = 0;
1098 if (ifp->ptm_enable) {
1099 if (!if_is_operative(ifp))
1100 send_linkup = 1;
1101
1102 if (ptm_disable)
1103 ifp->ptm_enable =
1104 ZEBRA_IF_PTM_ENABLE_OFF;
1105 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1106
1107 if (if_is_operative(ifp) && send_linkup) {
1108 if (IS_ZEBRA_DEBUG_EVENT)
1109 zlog_debug(
1110 "%s: Bringing up interface %s",
1111 __func__, ifp->name);
1112 if_up(ifp);
1113 }
1114 }
1115 }
1116 }
1117
1118 void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
1119 {
1120 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
1121 }
1122
1123 void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1124 struct zebra_if *zebra_ifp)
1125 {
1126 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1127 ifp->ptm_enable = zebra_ifp->ptm_enable;
1128 }
1129
1130 void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
1131 {
1132 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1133 vty_out(vty, " no ptm-enable\n");
1134 }