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