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