]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/config.c
sharpd: Allow sharpd to accept nexthop group as part of route install
[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 /*
34 * Definitions
35 */
36 enum peer_list_type {
37 PLT_IPV4,
38 PLT_IPV6,
39 PLT_LABEL,
40 };
41
42
43 /*
44 * Prototypes
45 */
46 static int parse_config_json(struct json_object *jo, bpc_handle h, void *arg);
47 static int parse_list(struct json_object *jo, enum peer_list_type plt,
48 bpc_handle h, void *arg);
49 static int parse_peer_config(struct json_object *jo, struct bfd_peer_cfg *bpc);
50 static int parse_peer_label_config(struct json_object *jo,
51 struct bfd_peer_cfg *bpc);
52
53 static int config_add(struct bfd_peer_cfg *bpc, void *arg);
54 static int config_del(struct bfd_peer_cfg *bpc, void *arg);
55
56 static int json_object_add_peer(struct json_object *jo, struct bfd_session *bs);
57
58
59 /*
60 * Implementation
61 */
62 static int config_add(struct bfd_peer_cfg *bpc,
63 void *arg __attribute__((unused)))
64 {
65 return ptm_bfd_sess_new(bpc) == NULL;
66 }
67
68 static int config_del(struct bfd_peer_cfg *bpc,
69 void *arg __attribute__((unused)))
70 {
71 return ptm_bfd_ses_del(bpc) != 0;
72 }
73
74 static 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
108 int 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
119 static 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
171 static 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 }
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
292 static 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. */
312 bpc->bpc_ipv4 = !BFD_CHECK_FLAG(pl->pl_bs->flags, BFD_SESS_FLAG_IPV6);
313 bpc->bpc_mhop = BFD_CHECK_FLAG(pl->pl_bs->flags, BFD_SESS_FLAG_MH);
314 if (bpc->bpc_mhop) {
315 bpc->bpc_peer = pl->pl_bs->mhop.peer;
316 bpc->bpc_local = pl->pl_bs->mhop.local;
317 if (pl->pl_bs->mhop.vrf_name[0]) {
318 bpc->bpc_has_vrfname = true;
319 strlcpy(bpc->bpc_vrfname, pl->pl_bs->mhop.vrf_name,
320 sizeof(bpc->bpc_vrfname));
321 }
322 } else {
323 bpc->bpc_peer = pl->pl_bs->shop.peer;
324 if (pl->pl_bs->shop.port_name[0]) {
325 bpc->bpc_has_localif = true;
326 strlcpy(bpc->bpc_localif, pl->pl_bs->shop.port_name,
327 sizeof(bpc->bpc_localif));
328 }
329 }
330
331 return 0;
332 }
333
334
335 /*
336 * Control socket JSON parsing.
337 */
338 int config_request_add(const char *jsonstr)
339 {
340 struct json_object *jo;
341
342 jo = json_tokener_parse(jsonstr);
343 if (jo == NULL)
344 return -1;
345
346 return parse_config_json(jo, config_add, NULL);
347 }
348
349 int config_request_del(const char *jsonstr)
350 {
351 struct json_object *jo;
352
353 jo = json_tokener_parse(jsonstr);
354 if (jo == NULL)
355 return -1;
356
357 return parse_config_json(jo, config_del, NULL);
358 }
359
360 char *config_response(const char *status, const char *error)
361 {
362 struct json_object *resp, *jo;
363 char *jsonstr;
364
365 resp = json_object_new_object();
366 if (resp == NULL)
367 return NULL;
368
369 /* Add 'status' response key. */
370 jo = json_object_new_string(status);
371 if (jo == NULL) {
372 json_object_put(resp);
373 return NULL;
374 }
375
376 json_object_object_add(resp, "status", jo);
377
378 /* Add 'error' response key. */
379 if (error != NULL) {
380 jo = json_object_new_string(error);
381 if (jo == NULL) {
382 json_object_put(resp);
383 return NULL;
384 }
385
386 json_object_object_add(resp, "error", jo);
387 }
388
389 /* Generate JSON response. */
390 jsonstr = XSTRDUP(
391 MTYPE_BFDD_NOTIFICATION,
392 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
393 json_object_put(resp);
394
395 return jsonstr;
396 }
397
398 char *config_notify(struct bfd_session *bs)
399 {
400 struct json_object *resp;
401 char *jsonstr;
402 time_t now;
403
404 resp = json_object_new_object();
405 if (resp == NULL)
406 return NULL;
407
408 json_object_string_add(resp, "op", BCM_NOTIFY_PEER_STATUS);
409
410 json_object_add_peer(resp, bs);
411
412 /* Add status information */
413 json_object_int_add(resp, "id", bs->discrs.my_discr);
414 json_object_int_add(resp, "remote-id", bs->discrs.my_discr);
415
416 switch (bs->ses_state) {
417 case PTM_BFD_UP:
418 json_object_string_add(resp, "state", "up");
419
420 now = monotime(NULL);
421 json_object_int_add(resp, "uptime", now - bs->uptime.tv_sec);
422 break;
423 case PTM_BFD_ADM_DOWN:
424 json_object_string_add(resp, "state", "adm-down");
425 break;
426 case PTM_BFD_DOWN:
427 json_object_string_add(resp, "state", "down");
428
429 now = monotime(NULL);
430 json_object_int_add(resp, "downtime",
431 now - bs->downtime.tv_sec);
432 break;
433 case PTM_BFD_INIT:
434 json_object_string_add(resp, "state", "init");
435 break;
436
437 default:
438 json_object_string_add(resp, "state", "unknown");
439 break;
440 }
441
442 json_object_int_add(resp, "diagnostics", bs->local_diag);
443 json_object_int_add(resp, "remote-diagnostics", bs->remote_diag);
444
445 /* Generate JSON response. */
446 jsonstr = XSTRDUP(
447 MTYPE_BFDD_NOTIFICATION,
448 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
449 json_object_put(resp);
450
451 return jsonstr;
452 }
453
454 char *config_notify_config(const char *op, struct bfd_session *bs)
455 {
456 struct json_object *resp;
457 char *jsonstr;
458
459 resp = json_object_new_object();
460 if (resp == NULL)
461 return NULL;
462
463 json_object_string_add(resp, "op", op);
464
465 json_object_add_peer(resp, bs);
466
467 /* On peer deletion we don't need to add any additional information. */
468 if (strcmp(op, BCM_NOTIFY_CONFIG_DELETE) == 0)
469 goto skip_config;
470
471 json_object_int_add(resp, "detect-multiplier", bs->detect_mult);
472 json_object_int_add(resp, "receive-interval",
473 bs->timers.required_min_rx / 1000);
474 json_object_int_add(resp, "transmit-interval", bs->up_min_tx / 1000);
475 json_object_int_add(resp, "echo-interval",
476 bs->timers.required_min_echo / 1000);
477
478 json_object_int_add(resp, "remote-detect-multiplier",
479 bs->remote_detect_mult);
480 json_object_int_add(resp, "remote-receive-interval",
481 bs->remote_timers.required_min_rx / 1000);
482 json_object_int_add(resp, "remote-transmit-interval",
483 bs->remote_timers.desired_min_tx / 1000);
484 json_object_int_add(resp, "remote-echo-interval",
485 bs->remote_timers.required_min_echo / 1000);
486
487 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
488 json_object_boolean_true_add(resp, "echo-mode");
489 else
490 json_object_boolean_false_add(resp, "echo-mode");
491
492 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
493 json_object_boolean_true_add(resp, "shutdown");
494 else
495 json_object_boolean_false_add(resp, "shutdown");
496
497 skip_config:
498 /* Generate JSON response. */
499 jsonstr = XSTRDUP(
500 MTYPE_BFDD_NOTIFICATION,
501 json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
502 json_object_put(resp);
503
504 return jsonstr;
505 }
506
507 int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
508 bpc_handle bh)
509 {
510 struct json_object *jo;
511
512 jo = json_tokener_parse(jsonstr);
513 if (jo == NULL)
514 return -1;
515
516 return parse_config_json(jo, bh, bcs);
517 }
518
519 static int json_object_add_peer(struct json_object *jo, struct bfd_session *bs)
520 {
521 /* Add peer 'key' information. */
522 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6))
523 json_object_boolean_true_add(jo, "ipv6");
524 else
525 json_object_boolean_false_add(jo, "ipv6");
526
527 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
528 json_object_boolean_true_add(jo, "multihop");
529 json_object_string_add(jo, "peer-address",
530 satostr(&bs->mhop.peer));
531 json_object_string_add(jo, "local-address",
532 satostr(&bs->mhop.local));
533 if (strlen(bs->mhop.vrf_name) > 0)
534 json_object_string_add(jo, "vrf-name",
535 bs->mhop.vrf_name);
536 } else {
537 json_object_boolean_false_add(jo, "multihop");
538 json_object_string_add(jo, "peer-address",
539 satostr(&bs->shop.peer));
540 if (bs->local_address.sa_sin.sin_family != AF_UNSPEC)
541 json_object_string_add(jo, "local-address",
542 satostr(&bs->local_address));
543 if (strlen(bs->shop.port_name) > 0)
544 json_object_string_add(jo, "local-interface",
545 bs->shop.port_name);
546 }
547
548 if (bs->pl)
549 json_object_string_add(jo, "label", bs->pl->pl_label);
550
551 return 0;
552 }
553
554
555 /*
556 * Label handling
557 */
558 struct peer_label *pl_find(const char *label)
559 {
560 struct peer_label *pl;
561
562 TAILQ_FOREACH (pl, &bglobal.bg_pllist, pl_entry) {
563 if (strcmp(pl->pl_label, label) != 0)
564 continue;
565
566 return pl;
567 }
568
569 return NULL;
570 }
571
572 struct peer_label *pl_new(const char *label, struct bfd_session *bs)
573 {
574 struct peer_label *pl;
575
576 pl = XCALLOC(MTYPE_BFDD_LABEL, sizeof(*pl));
577 if (pl == NULL)
578 return NULL;
579
580 if (strlcpy(pl->pl_label, label, sizeof(pl->pl_label))
581 > sizeof(pl->pl_label))
582 log_warning("%s:%d: label was truncated", __func__, __LINE__);
583
584 pl->pl_bs = bs;
585 bs->pl = pl;
586
587 TAILQ_INSERT_HEAD(&bglobal.bg_pllist, pl, pl_entry);
588
589 return pl;
590 }
591
592 void pl_free(struct peer_label *pl)
593 {
594 if (pl == NULL)
595 return;
596
597 /* Remove the pointer back. */
598 pl->pl_bs->pl = NULL;
599
600 TAILQ_REMOVE(&bglobal.bg_pllist, pl, pl_entry);
601 XFREE(MTYPE_BFDD_LABEL, pl);
602 }