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