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