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