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