]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/config.c
Merge pull request #10848 from pguibert6WIND/ospf_srlb_config
[mirror_frr.git] / bfdd / config.c
1 /*********************************************************************
2 * Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * config.c: implements the BFD daemon configuration handling.
19 *
20 * Authors
21 * -------
22 * Rafael Zalamena <rzalamena@opensourcerouting.org>
23 */
24
25 #include <zebra.h>
26
27 #include <string.h>
28
29 #include "lib/json.h"
30
31 #include "bfd.h"
32
33 DEFINE_MTYPE_STATIC(BFDD, BFDD_LABEL, "long-lived label memory");
34
35 /*
36 * Definitions
37 */
38 enum peer_list_type {
39 PLT_IPV4,
40 PLT_IPV6,
41 PLT_LABEL,
42 };
43
44
45 /*
46 * Prototypes
47 */
48 static int parse_config_json(struct json_object *jo, bpc_handle h, void *arg);
49 static int parse_list(struct json_object *jo, enum peer_list_type plt,
50 bpc_handle h, void *arg);
51 static int parse_peer_config(struct json_object *jo, struct bfd_peer_cfg *bpc);
52 static int parse_peer_label_config(struct json_object *jo,
53 struct bfd_peer_cfg *bpc);
54
55 static int config_add(struct bfd_peer_cfg *bpc, void *arg);
56 static int config_del(struct bfd_peer_cfg *bpc, void *arg);
57
58 static int json_object_add_peer(struct json_object *jo, struct bfd_session *bs);
59
60
61 /*
62 * Implementation
63 */
64 static int config_add(struct bfd_peer_cfg *bpc,
65 void *arg __attribute__((unused)))
66 {
67 return ptm_bfd_sess_new(bpc) == NULL;
68 }
69
70 static int config_del(struct bfd_peer_cfg *bpc,
71 void *arg __attribute__((unused)))
72 {
73 return ptm_bfd_sess_del(bpc) != 0;
74 }
75
76 static int parse_config_json(struct json_object *jo, bpc_handle h, void *arg)
77 {
78 const char *key, *sval;
79 struct json_object *jo_val;
80 struct json_object_iterator joi, join;
81 int error = 0;
82
83 JSON_FOREACH (jo, joi, join) {
84 key = json_object_iter_peek_name(&joi);
85 jo_val = json_object_iter_peek_value(&joi);
86
87 if (strcmp(key, "ipv4") == 0) {
88 error += parse_list(jo_val, PLT_IPV4, h, arg);
89 } else if (strcmp(key, "ipv6") == 0) {
90 error += parse_list(jo_val, PLT_IPV6, h, arg);
91 } else if (strcmp(key, "label") == 0) {
92 error += parse_list(jo_val, PLT_LABEL, h, arg);
93 } else {
94 sval = json_object_get_string(jo_val);
95 zlog_warn("%s:%d invalid configuration: %s", __func__,
96 __LINE__, sval);
97 error++;
98 }
99 }
100
101 /*
102 * Our callers never call free() on json_object and only expect
103 * the return value, so lets free() it here.
104 */
105 json_object_put(jo);
106
107 return error;
108 }
109
110 int parse_config(const char *fname)
111 {
112 struct json_object *jo;
113
114 jo = json_object_from_file(fname);
115 if (jo == NULL)
116 return -1;
117
118 return parse_config_json(jo, config_add, NULL);
119 }
120
121 static int parse_list(struct json_object *jo, enum peer_list_type plt,
122 bpc_handle h, void *arg)
123 {
124 struct json_object *jo_val;
125 struct bfd_peer_cfg bpc;
126 int allen, idx;
127 int error = 0, result;
128
129 allen = json_object_array_length(jo);
130 for (idx = 0; idx < allen; idx++) {
131 jo_val = json_object_array_get_idx(jo, idx);
132
133 /* Set defaults. */
134 memset(&bpc, 0, sizeof(bpc));
135 bpc.bpc_detectmultiplier = BFD_DEFDETECTMULT;
136 bpc.bpc_recvinterval = BFD_DEFREQUIREDMINRX;
137 bpc.bpc_txinterval = BFD_DEFDESIREDMINTX;
138 bpc.bpc_echorecvinterval = BFD_DEF_REQ_MIN_ECHO_RX;
139 bpc.bpc_echotxinterval = BFD_DEF_DES_MIN_ECHO_TX;
140
141 switch (plt) {
142 case PLT_IPV4:
143 zlog_debug("ipv4 peers %d:", allen);
144 bpc.bpc_ipv4 = true;
145 break;
146 case PLT_IPV6:
147 zlog_debug("ipv6 peers %d:", allen);
148 bpc.bpc_ipv4 = false;
149 break;
150 case PLT_LABEL:
151 zlog_debug("label peers %d:", allen);
152 if (parse_peer_label_config(jo_val, &bpc) != 0) {
153 error++;
154 continue;
155 }
156 break;
157
158 default:
159 error++;
160 zlog_err("%s:%d: unsupported peer type", __func__,
161 __LINE__);
162 break;
163 }
164
165 result = parse_peer_config(jo_val, &bpc);
166 error += result;
167 if (result == 0)
168 error += (h(&bpc, arg) != 0);
169 }
170
171 return error;
172 }
173
174 static int parse_peer_config(struct json_object *jo, struct bfd_peer_cfg *bpc)
175 {
176 const char *key, *sval;
177 struct json_object *jo_val;
178 struct json_object_iterator joi, join;
179 int family_type = (bpc->bpc_ipv4) ? AF_INET : AF_INET6;
180 int error = 0;
181
182 zlog_debug(" peer: %s", bpc->bpc_ipv4 ? "ipv4" : "ipv6");
183
184 JSON_FOREACH (jo, joi, join) {
185 key = json_object_iter_peek_name(&joi);
186 jo_val = json_object_iter_peek_value(&joi);
187
188 if (strcmp(key, "multihop") == 0) {
189 bpc->bpc_mhop = json_object_get_boolean(jo_val);
190 zlog_debug(" multihop: %s",
191 bpc->bpc_mhop ? "true" : "false");
192 } else if (strcmp(key, "peer-address") == 0) {
193 sval = json_object_get_string(jo_val);
194 if (strtosa(sval, &bpc->bpc_peer) != 0
195 || bpc->bpc_peer.sa_sin.sin_family != family_type) {
196 zlog_debug(
197 "%s:%d failed to parse peer-address '%s'",
198 __func__, __LINE__, sval);
199 error++;
200 }
201 zlog_debug(" peer-address: %s", sval);
202 } else if (strcmp(key, "local-address") == 0) {
203 sval = json_object_get_string(jo_val);
204 if (strtosa(sval, &bpc->bpc_local) != 0
205 || bpc->bpc_local.sa_sin.sin_family
206 != family_type) {
207 zlog_debug(
208 "%s:%d failed to parse local-address '%s'",
209 __func__, __LINE__, sval);
210 error++;
211 }
212 zlog_debug(" local-address: %s", sval);
213 } else if (strcmp(key, "local-interface") == 0) {
214 bpc->bpc_has_localif = true;
215 sval = json_object_get_string(jo_val);
216 if (strlcpy(bpc->bpc_localif, sval,
217 sizeof(bpc->bpc_localif))
218 > sizeof(bpc->bpc_localif)) {
219 zlog_debug(
220 " local-interface: %s (truncated)",
221 sval);
222 error++;
223 } else {
224 zlog_debug(" local-interface: %s", sval);
225 }
226 } else if (strcmp(key, "vrf-name") == 0) {
227 bpc->bpc_has_vrfname = true;
228 sval = json_object_get_string(jo_val);
229 if (strlcpy(bpc->bpc_vrfname, sval,
230 sizeof(bpc->bpc_vrfname))
231 > sizeof(bpc->bpc_vrfname)) {
232 zlog_debug(" vrf-name: %s (truncated)",
233 sval);
234 error++;
235 } else {
236 zlog_debug(" vrf-name: %s", sval);
237 }
238 } else if (strcmp(key, "detect-multiplier") == 0) {
239 bpc->bpc_detectmultiplier =
240 json_object_get_int64(jo_val);
241 bpc->bpc_has_detectmultiplier = true;
242 zlog_debug(" detect-multiplier: %u",
243 bpc->bpc_detectmultiplier);
244 } else if (strcmp(key, "receive-interval") == 0) {
245 bpc->bpc_recvinterval = json_object_get_int64(jo_val);
246 bpc->bpc_has_recvinterval = true;
247 zlog_debug(" receive-interval: %" PRIu64,
248 bpc->bpc_recvinterval);
249 } else if (strcmp(key, "transmit-interval") == 0) {
250 bpc->bpc_txinterval = json_object_get_int64(jo_val);
251 bpc->bpc_has_txinterval = true;
252 zlog_debug(" transmit-interval: %" PRIu64,
253 bpc->bpc_txinterval);
254 } else if (strcmp(key, "echo-receive-interval") == 0) {
255 bpc->bpc_echorecvinterval = json_object_get_int64(jo_val);
256 bpc->bpc_has_echorecvinterval = true;
257 zlog_debug(" echo-receive-interval: %" PRIu64,
258 bpc->bpc_echorecvinterval);
259 } else if (strcmp(key, "echo-transmit-interval") == 0) {
260 bpc->bpc_echotxinterval = json_object_get_int64(jo_val);
261 bpc->bpc_has_echotxinterval = true;
262 zlog_debug(" echo-transmit-interval: %" PRIu64,
263 bpc->bpc_echotxinterval);
264 } else if (strcmp(key, "create-only") == 0) {
265 bpc->bpc_createonly = json_object_get_boolean(jo_val);
266 zlog_debug(" create-only: %s",
267 bpc->bpc_createonly ? "true" : "false");
268 } else if (strcmp(key, "shutdown") == 0) {
269 bpc->bpc_shutdown = json_object_get_boolean(jo_val);
270 zlog_debug(" shutdown: %s",
271 bpc->bpc_shutdown ? "true" : "false");
272 } else if (strcmp(key, "echo-mode") == 0) {
273 bpc->bpc_echo = json_object_get_boolean(jo_val);
274 zlog_debug(" echo-mode: %s",
275 bpc->bpc_echo ? "true" : "false");
276 } else if (strcmp(key, "label") == 0) {
277 bpc->bpc_has_label = true;
278 sval = json_object_get_string(jo_val);
279 if (strlcpy(bpc->bpc_label, sval,
280 sizeof(bpc->bpc_label))
281 > sizeof(bpc->bpc_label)) {
282 zlog_debug(" label: %s (truncated)",
283 sval);
284 error++;
285 } else {
286 zlog_debug(" label: %s", sval);
287 }
288 } else {
289 sval = json_object_get_string(jo_val);
290 zlog_warn("%s:%d invalid configuration: '%s: %s'",
291 __func__, __LINE__, key, sval);
292 error++;
293 }
294 }
295
296 if (bpc->bpc_peer.sa_sin.sin_family == 0) {
297 zlog_debug("%s:%d no peer address provided", __func__,
298 __LINE__);
299 error++;
300 }
301
302 return error;
303 }
304
305 static int parse_peer_label_config(struct json_object *jo,
306 struct bfd_peer_cfg *bpc)
307 {
308 struct peer_label *pl;
309 struct json_object *label;
310 const char *sval;
311
312 /* Get label and translate it to BFD daemon key. */
313 if (!json_object_object_get_ex(jo, "label", &label))
314 return 1;
315
316 sval = json_object_get_string(label);
317
318 pl = pl_find(sval);
319 if (pl == NULL)
320 return 1;
321
322 zlog_debug(" peer-label: %s", sval);
323
324 /* Translate the label into BFD address keys. */
325 bs_to_bpc(pl->pl_bs, bpc);
326
327 return 0;
328 }
329
330
331 /*
332 * Control socket JSON parsing.
333 */
334 int config_request_add(const char *jsonstr)
335 {
336 struct json_object *jo;
337
338 jo = json_tokener_parse(jsonstr);
339 if (jo == NULL)
340 return -1;
341
342 return parse_config_json(jo, config_add, NULL);
343 }
344
345 int config_request_del(const char *jsonstr)
346 {
347 struct json_object *jo;
348
349 jo = json_tokener_parse(jsonstr);
350 if (jo == NULL)
351 return -1;
352
353 return parse_config_json(jo, config_del, NULL);
354 }
355
356 char *config_response(const char *status, const char *error)
357 {
358 struct json_object *resp, *jo;
359 char *jsonstr;
360
361 resp = json_object_new_object();
362 if (resp == NULL)
363 return NULL;
364
365 /* Add 'status' response key. */
366 jo = json_object_new_string(status);
367 if (jo == NULL) {
368 json_object_put(resp);
369 return NULL;
370 }
371
372 json_object_object_add(resp, "status", jo);
373
374 /* Add 'error' response key. */
375 if (error != NULL) {
376 jo = json_object_new_string(error);
377 if (jo == NULL) {
378 json_object_put(resp);
379 return NULL;
380 }
381
382 json_object_object_add(resp, "error", jo);
383 }
384
385 /* Generate JSON response. */
386 jsonstr = XSTRDUP(
387 MTYPE_BFDD_NOTIFICATION,
388 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
389 json_object_put(resp);
390
391 return jsonstr;
392 }
393
394 char *config_notify(struct bfd_session *bs)
395 {
396 struct json_object *resp;
397 char *jsonstr;
398 time_t now;
399
400 resp = json_object_new_object();
401 if (resp == NULL)
402 return NULL;
403
404 json_object_string_add(resp, "op", BCM_NOTIFY_PEER_STATUS);
405
406 json_object_add_peer(resp, bs);
407
408 /* Add status information */
409 json_object_int_add(resp, "id", bs->discrs.my_discr);
410 json_object_int_add(resp, "remote-id", bs->discrs.my_discr);
411
412 switch (bs->ses_state) {
413 case PTM_BFD_UP:
414 json_object_string_add(resp, "state", "up");
415
416 now = monotime(NULL);
417 json_object_int_add(resp, "uptime", now - bs->uptime.tv_sec);
418 break;
419 case PTM_BFD_ADM_DOWN:
420 json_object_string_add(resp, "state", "adm-down");
421 break;
422 case PTM_BFD_DOWN:
423 json_object_string_add(resp, "state", "down");
424
425 now = monotime(NULL);
426 json_object_int_add(resp, "downtime",
427 now - bs->downtime.tv_sec);
428 break;
429 case PTM_BFD_INIT:
430 json_object_string_add(resp, "state", "init");
431 break;
432
433 default:
434 json_object_string_add(resp, "state", "unknown");
435 break;
436 }
437
438 json_object_int_add(resp, "diagnostics", bs->local_diag);
439 json_object_int_add(resp, "remote-diagnostics", bs->remote_diag);
440
441 /* Generate JSON response. */
442 jsonstr = XSTRDUP(
443 MTYPE_BFDD_NOTIFICATION,
444 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
445 json_object_put(resp);
446
447 return jsonstr;
448 }
449
450 char *config_notify_config(const char *op, struct bfd_session *bs)
451 {
452 struct json_object *resp;
453 char *jsonstr;
454
455 resp = json_object_new_object();
456 if (resp == NULL)
457 return NULL;
458
459 json_object_string_add(resp, "op", op);
460
461 json_object_add_peer(resp, bs);
462
463 /* On peer deletion we don't need to add any additional information. */
464 if (strcmp(op, BCM_NOTIFY_CONFIG_DELETE) == 0)
465 goto skip_config;
466
467 json_object_int_add(resp, "detect-multiplier", bs->detect_mult);
468 json_object_int_add(resp, "receive-interval",
469 bs->timers.required_min_rx / 1000);
470 json_object_int_add(resp, "transmit-interval",
471 bs->timers.desired_min_tx / 1000);
472 json_object_int_add(resp, "echo-receive-interval",
473 bs->timers.required_min_echo_rx / 1000);
474 json_object_int_add(resp, "echo-transmit-interval",
475 bs->timers.desired_min_echo_tx / 1000);
476
477 json_object_int_add(resp, "remote-detect-multiplier",
478 bs->remote_detect_mult);
479 json_object_int_add(resp, "remote-receive-interval",
480 bs->remote_timers.required_min_rx / 1000);
481 json_object_int_add(resp, "remote-transmit-interval",
482 bs->remote_timers.desired_min_tx / 1000);
483 json_object_int_add(resp, "remote-echo-receive-interval",
484 bs->remote_timers.required_min_echo / 1000);
485
486 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
487 json_object_boolean_true_add(resp, "echo-mode");
488 else
489 json_object_boolean_false_add(resp, "echo-mode");
490
491 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
492 json_object_boolean_true_add(resp, "shutdown");
493 else
494 json_object_boolean_false_add(resp, "shutdown");
495
496 skip_config:
497 /* Generate JSON response. */
498 jsonstr = XSTRDUP(
499 MTYPE_BFDD_NOTIFICATION,
500 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
501 json_object_put(resp);
502
503 return jsonstr;
504 }
505
506 int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
507 bpc_handle bh)
508 {
509 struct json_object *jo;
510
511 jo = json_tokener_parse(jsonstr);
512 if (jo == NULL)
513 return -1;
514
515 return parse_config_json(jo, bh, bcs);
516 }
517
518 static int json_object_add_peer(struct json_object *jo, struct bfd_session *bs)
519 {
520 char addr_buf[INET6_ADDRSTRLEN];
521
522 /* Add peer 'key' information. */
523 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6))
524 json_object_boolean_true_add(jo, "ipv6");
525 else
526 json_object_boolean_false_add(jo, "ipv6");
527
528 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
529 json_object_boolean_true_add(jo, "multihop");
530 json_object_string_add(jo, "peer-address",
531 inet_ntop(bs->key.family, &bs->key.peer,
532 addr_buf, sizeof(addr_buf)));
533 json_object_string_add(jo, "local-address",
534 inet_ntop(bs->key.family, &bs->key.local,
535 addr_buf, sizeof(addr_buf)));
536 if (bs->key.vrfname[0])
537 json_object_string_add(jo, "vrf-name", bs->key.vrfname);
538 } else {
539 json_object_boolean_false_add(jo, "multihop");
540 json_object_string_add(jo, "peer-address",
541 inet_ntop(bs->key.family, &bs->key.peer,
542 addr_buf, sizeof(addr_buf)));
543 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
544 json_object_string_add(
545 jo, "local-address",
546 inet_ntop(bs->key.family, &bs->key.local,
547 addr_buf, sizeof(addr_buf)));
548 if (bs->key.ifname[0])
549 json_object_string_add(jo, "local-interface",
550 bs->key.ifname);
551 }
552
553 if (bs->pl)
554 json_object_string_add(jo, "label", bs->pl->pl_label);
555
556 return 0;
557 }
558
559
560 /*
561 * Label handling
562 */
563 struct peer_label *pl_find(const char *label)
564 {
565 struct peer_label *pl;
566
567 TAILQ_FOREACH (pl, &bglobal.bg_pllist, pl_entry) {
568 if (strcmp(pl->pl_label, label) != 0)
569 continue;
570
571 return pl;
572 }
573
574 return NULL;
575 }
576
577 struct peer_label *pl_new(const char *label, struct bfd_session *bs)
578 {
579 struct peer_label *pl;
580
581 pl = XCALLOC(MTYPE_BFDD_LABEL, sizeof(*pl));
582
583 if (strlcpy(pl->pl_label, label, sizeof(pl->pl_label))
584 > sizeof(pl->pl_label))
585 zlog_warn("%s:%d: label was truncated", __func__, __LINE__);
586
587 pl->pl_bs = bs;
588 bs->pl = pl;
589
590 TAILQ_INSERT_HEAD(&bglobal.bg_pllist, pl, pl_entry);
591
592 return pl;
593 }
594
595 void pl_free(struct peer_label *pl)
596 {
597 if (pl == NULL)
598 return;
599
600 /* Remove the pointer back. */
601 pl->pl_bs->pl = NULL;
602
603 TAILQ_REMOVE(&bglobal.bg_pllist, pl, pl_entry);
604 XFREE(MTYPE_BFDD_LABEL, pl);
605 }