]>
Commit | Line | Data |
---|---|---|
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 | ||
1b88c3cb DL |
33 | DEFINE_MTYPE_STATIC(BFDD, BFDD_LABEL, "long-lived label memory") |
34 | ||
e9e2c950 RZ |
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 | { | |
bc50bcc8 | 73 | return ptm_bfd_sess_del(bpc) != 0; |
e9e2c950 RZ |
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); | |
259b64eb RZ |
95 | zlog_warn("%s:%d invalid configuration: %s", __func__, |
96 | __LINE__, sval); | |
e9e2c950 RZ |
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_echointerval = BFD_DEF_REQ_MIN_ECHO; | |
139 | ||
140 | switch (plt) { | |
141 | case PLT_IPV4: | |
259b64eb | 142 | zlog_debug("ipv4 peers %d:", allen); |
e9e2c950 RZ |
143 | bpc.bpc_ipv4 = true; |
144 | break; | |
145 | case PLT_IPV6: | |
259b64eb | 146 | zlog_debug("ipv6 peers %d:", allen); |
e9e2c950 RZ |
147 | bpc.bpc_ipv4 = false; |
148 | break; | |
149 | case PLT_LABEL: | |
259b64eb | 150 | zlog_debug("label peers %d:", allen); |
e9e2c950 RZ |
151 | if (parse_peer_label_config(jo_val, &bpc) != 0) { |
152 | error++; | |
153 | continue; | |
154 | } | |
155 | break; | |
156 | ||
157 | default: | |
158 | error++; | |
259b64eb RZ |
159 | zlog_err("%s:%d: unsupported peer type", __func__, |
160 | __LINE__); | |
e9e2c950 RZ |
161 | break; |
162 | } | |
163 | ||
164 | result = parse_peer_config(jo_val, &bpc); | |
165 | error += result; | |
166 | if (result == 0) | |
167 | error += (h(&bpc, arg) != 0); | |
168 | } | |
169 | ||
170 | return error; | |
171 | } | |
172 | ||
173 | static int parse_peer_config(struct json_object *jo, struct bfd_peer_cfg *bpc) | |
174 | { | |
175 | const char *key, *sval; | |
176 | struct json_object *jo_val; | |
177 | struct json_object_iterator joi, join; | |
178 | int family_type = (bpc->bpc_ipv4) ? AF_INET : AF_INET6; | |
179 | int error = 0; | |
180 | ||
259b64eb | 181 | zlog_debug(" peer: %s", bpc->bpc_ipv4 ? "ipv4" : "ipv6"); |
e9e2c950 RZ |
182 | |
183 | JSON_FOREACH (jo, joi, join) { | |
184 | key = json_object_iter_peek_name(&joi); | |
185 | jo_val = json_object_iter_peek_value(&joi); | |
186 | ||
187 | if (strcmp(key, "multihop") == 0) { | |
188 | bpc->bpc_mhop = json_object_get_boolean(jo_val); | |
259b64eb RZ |
189 | zlog_debug(" multihop: %s", |
190 | bpc->bpc_mhop ? "true" : "false"); | |
e9e2c950 RZ |
191 | } else if (strcmp(key, "peer-address") == 0) { |
192 | sval = json_object_get_string(jo_val); | |
193 | if (strtosa(sval, &bpc->bpc_peer) != 0 | |
194 | || bpc->bpc_peer.sa_sin.sin_family != family_type) { | |
259b64eb | 195 | zlog_debug( |
e9e2c950 RZ |
196 | "%s:%d failed to parse peer-address '%s'", |
197 | __func__, __LINE__, sval); | |
198 | error++; | |
199 | } | |
259b64eb | 200 | zlog_debug(" peer-address: %s", sval); |
e9e2c950 RZ |
201 | } else if (strcmp(key, "local-address") == 0) { |
202 | sval = json_object_get_string(jo_val); | |
203 | if (strtosa(sval, &bpc->bpc_local) != 0 | |
204 | || bpc->bpc_local.sa_sin.sin_family | |
205 | != family_type) { | |
259b64eb | 206 | zlog_debug( |
e9e2c950 RZ |
207 | "%s:%d failed to parse local-address '%s'", |
208 | __func__, __LINE__, sval); | |
209 | error++; | |
210 | } | |
259b64eb | 211 | zlog_debug(" local-address: %s", sval); |
e9e2c950 RZ |
212 | } else if (strcmp(key, "local-interface") == 0) { |
213 | bpc->bpc_has_localif = true; | |
214 | sval = json_object_get_string(jo_val); | |
215 | if (strlcpy(bpc->bpc_localif, sval, | |
216 | sizeof(bpc->bpc_localif)) | |
217 | > sizeof(bpc->bpc_localif)) { | |
259b64eb | 218 | zlog_debug( |
d6951e5e DL |
219 | " local-interface: %s (truncated)", |
220 | sval); | |
e9e2c950 RZ |
221 | error++; |
222 | } else { | |
259b64eb | 223 | zlog_debug(" local-interface: %s", sval); |
e9e2c950 | 224 | } |
e9e2c950 RZ |
225 | } else if (strcmp(key, "vrf-name") == 0) { |
226 | bpc->bpc_has_vrfname = true; | |
227 | sval = json_object_get_string(jo_val); | |
228 | if (strlcpy(bpc->bpc_vrfname, sval, | |
229 | sizeof(bpc->bpc_vrfname)) | |
230 | > sizeof(bpc->bpc_vrfname)) { | |
259b64eb RZ |
231 | zlog_debug(" vrf-name: %s (truncated)", |
232 | sval); | |
e9e2c950 RZ |
233 | error++; |
234 | } else { | |
259b64eb | 235 | zlog_debug(" vrf-name: %s", sval); |
e9e2c950 RZ |
236 | } |
237 | } else if (strcmp(key, "detect-multiplier") == 0) { | |
238 | bpc->bpc_detectmultiplier = | |
239 | json_object_get_int64(jo_val); | |
240 | bpc->bpc_has_detectmultiplier = true; | |
259b64eb RZ |
241 | zlog_debug(" detect-multiplier: %u", |
242 | bpc->bpc_detectmultiplier); | |
e9e2c950 RZ |
243 | } else if (strcmp(key, "receive-interval") == 0) { |
244 | bpc->bpc_recvinterval = json_object_get_int64(jo_val); | |
245 | bpc->bpc_has_recvinterval = true; | |
259b64eb RZ |
246 | zlog_debug(" receive-interval: %" PRIu64, |
247 | bpc->bpc_recvinterval); | |
e9e2c950 RZ |
248 | } else if (strcmp(key, "transmit-interval") == 0) { |
249 | bpc->bpc_txinterval = json_object_get_int64(jo_val); | |
250 | bpc->bpc_has_txinterval = true; | |
259b64eb RZ |
251 | zlog_debug(" transmit-interval: %" PRIu64, |
252 | bpc->bpc_txinterval); | |
e9e2c950 RZ |
253 | } else if (strcmp(key, "echo-interval") == 0) { |
254 | bpc->bpc_echointerval = json_object_get_int64(jo_val); | |
255 | bpc->bpc_has_echointerval = true; | |
259b64eb RZ |
256 | zlog_debug(" echo-interval: %" PRIu64, |
257 | bpc->bpc_echointerval); | |
e9e2c950 RZ |
258 | } else if (strcmp(key, "create-only") == 0) { |
259 | bpc->bpc_createonly = json_object_get_boolean(jo_val); | |
259b64eb RZ |
260 | zlog_debug(" create-only: %s", |
261 | bpc->bpc_createonly ? "true" : "false"); | |
e9e2c950 RZ |
262 | } else if (strcmp(key, "shutdown") == 0) { |
263 | bpc->bpc_shutdown = json_object_get_boolean(jo_val); | |
259b64eb RZ |
264 | zlog_debug(" shutdown: %s", |
265 | bpc->bpc_shutdown ? "true" : "false"); | |
e9e2c950 RZ |
266 | } else if (strcmp(key, "echo-mode") == 0) { |
267 | bpc->bpc_echo = json_object_get_boolean(jo_val); | |
259b64eb RZ |
268 | zlog_debug(" echo-mode: %s", |
269 | bpc->bpc_echo ? "true" : "false"); | |
e9e2c950 RZ |
270 | } else if (strcmp(key, "label") == 0) { |
271 | bpc->bpc_has_label = true; | |
272 | sval = json_object_get_string(jo_val); | |
273 | if (strlcpy(bpc->bpc_label, sval, | |
274 | sizeof(bpc->bpc_label)) | |
275 | > sizeof(bpc->bpc_label)) { | |
259b64eb RZ |
276 | zlog_debug(" label: %s (truncated)", |
277 | sval); | |
e9e2c950 RZ |
278 | error++; |
279 | } else { | |
259b64eb | 280 | zlog_debug(" label: %s", sval); |
e9e2c950 RZ |
281 | } |
282 | } else { | |
283 | sval = json_object_get_string(jo_val); | |
259b64eb RZ |
284 | zlog_warn("%s:%d invalid configuration: '%s: %s'", |
285 | __func__, __LINE__, key, sval); | |
e9e2c950 RZ |
286 | error++; |
287 | } | |
288 | } | |
289 | ||
290 | if (bpc->bpc_peer.sa_sin.sin_family == 0) { | |
259b64eb RZ |
291 | zlog_debug("%s:%d no peer address provided", __func__, |
292 | __LINE__); | |
e9e2c950 RZ |
293 | error++; |
294 | } | |
295 | ||
296 | return error; | |
297 | } | |
298 | ||
299 | static int parse_peer_label_config(struct json_object *jo, | |
300 | struct bfd_peer_cfg *bpc) | |
301 | { | |
302 | struct peer_label *pl; | |
303 | struct json_object *label; | |
304 | const char *sval; | |
305 | ||
306 | /* Get label and translate it to BFD daemon key. */ | |
307 | if (!json_object_object_get_ex(jo, "label", &label)) | |
308 | return 1; | |
309 | ||
310 | sval = json_object_get_string(label); | |
311 | ||
312 | pl = pl_find(sval); | |
313 | if (pl == NULL) | |
314 | return 1; | |
315 | ||
259b64eb | 316 | zlog_debug(" peer-label: %s", sval); |
e9e2c950 RZ |
317 | |
318 | /* Translate the label into BFD address keys. */ | |
79b4a6fc | 319 | bs_to_bpc(pl->pl_bs, bpc); |
e9e2c950 RZ |
320 | |
321 | return 0; | |
322 | } | |
323 | ||
324 | ||
325 | /* | |
326 | * Control socket JSON parsing. | |
327 | */ | |
328 | int config_request_add(const char *jsonstr) | |
329 | { | |
330 | struct json_object *jo; | |
331 | ||
332 | jo = json_tokener_parse(jsonstr); | |
333 | if (jo == NULL) | |
334 | return -1; | |
335 | ||
336 | return parse_config_json(jo, config_add, NULL); | |
337 | } | |
338 | ||
339 | int config_request_del(const char *jsonstr) | |
340 | { | |
341 | struct json_object *jo; | |
342 | ||
343 | jo = json_tokener_parse(jsonstr); | |
344 | if (jo == NULL) | |
345 | return -1; | |
346 | ||
347 | return parse_config_json(jo, config_del, NULL); | |
348 | } | |
349 | ||
350 | char *config_response(const char *status, const char *error) | |
351 | { | |
352 | struct json_object *resp, *jo; | |
353 | char *jsonstr; | |
354 | ||
355 | resp = json_object_new_object(); | |
356 | if (resp == NULL) | |
357 | return NULL; | |
358 | ||
359 | /* Add 'status' response key. */ | |
360 | jo = json_object_new_string(status); | |
361 | if (jo == NULL) { | |
362 | json_object_put(resp); | |
363 | return NULL; | |
364 | } | |
365 | ||
366 | json_object_object_add(resp, "status", jo); | |
367 | ||
368 | /* Add 'error' response key. */ | |
369 | if (error != NULL) { | |
370 | jo = json_object_new_string(error); | |
371 | if (jo == NULL) { | |
372 | json_object_put(resp); | |
373 | return NULL; | |
374 | } | |
375 | ||
376 | json_object_object_add(resp, "error", jo); | |
377 | } | |
378 | ||
379 | /* Generate JSON response. */ | |
380 | jsonstr = XSTRDUP( | |
381 | MTYPE_BFDD_NOTIFICATION, | |
382 | json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS)); | |
383 | json_object_put(resp); | |
384 | ||
385 | return jsonstr; | |
386 | } | |
387 | ||
388 | char *config_notify(struct bfd_session *bs) | |
389 | { | |
390 | struct json_object *resp; | |
391 | char *jsonstr; | |
392 | time_t now; | |
393 | ||
394 | resp = json_object_new_object(); | |
395 | if (resp == NULL) | |
396 | return NULL; | |
397 | ||
398 | json_object_string_add(resp, "op", BCM_NOTIFY_PEER_STATUS); | |
399 | ||
400 | json_object_add_peer(resp, bs); | |
401 | ||
402 | /* Add status information */ | |
403 | json_object_int_add(resp, "id", bs->discrs.my_discr); | |
404 | json_object_int_add(resp, "remote-id", bs->discrs.my_discr); | |
405 | ||
406 | switch (bs->ses_state) { | |
407 | case PTM_BFD_UP: | |
408 | json_object_string_add(resp, "state", "up"); | |
409 | ||
410 | now = monotime(NULL); | |
411 | json_object_int_add(resp, "uptime", now - bs->uptime.tv_sec); | |
412 | break; | |
413 | case PTM_BFD_ADM_DOWN: | |
414 | json_object_string_add(resp, "state", "adm-down"); | |
415 | break; | |
416 | case PTM_BFD_DOWN: | |
417 | json_object_string_add(resp, "state", "down"); | |
418 | ||
419 | now = monotime(NULL); | |
420 | json_object_int_add(resp, "downtime", | |
421 | now - bs->downtime.tv_sec); | |
422 | break; | |
423 | case PTM_BFD_INIT: | |
424 | json_object_string_add(resp, "state", "init"); | |
425 | break; | |
426 | ||
427 | default: | |
428 | json_object_string_add(resp, "state", "unknown"); | |
429 | break; | |
430 | } | |
431 | ||
432 | json_object_int_add(resp, "diagnostics", bs->local_diag); | |
433 | json_object_int_add(resp, "remote-diagnostics", bs->remote_diag); | |
434 | ||
435 | /* Generate JSON response. */ | |
436 | jsonstr = XSTRDUP( | |
437 | MTYPE_BFDD_NOTIFICATION, | |
438 | json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS)); | |
439 | json_object_put(resp); | |
440 | ||
441 | return jsonstr; | |
442 | } | |
443 | ||
444 | char *config_notify_config(const char *op, struct bfd_session *bs) | |
445 | { | |
446 | struct json_object *resp; | |
447 | char *jsonstr; | |
448 | ||
449 | resp = json_object_new_object(); | |
450 | if (resp == NULL) | |
451 | return NULL; | |
452 | ||
453 | json_object_string_add(resp, "op", op); | |
454 | ||
455 | json_object_add_peer(resp, bs); | |
456 | ||
457 | /* On peer deletion we don't need to add any additional information. */ | |
458 | if (strcmp(op, BCM_NOTIFY_CONFIG_DELETE) == 0) | |
459 | goto skip_config; | |
460 | ||
461 | json_object_int_add(resp, "detect-multiplier", bs->detect_mult); | |
462 | json_object_int_add(resp, "receive-interval", | |
463 | bs->timers.required_min_rx / 1000); | |
f43b9368 RZ |
464 | json_object_int_add(resp, "transmit-interval", |
465 | bs->timers.desired_min_tx / 1000); | |
e9e2c950 RZ |
466 | json_object_int_add(resp, "echo-interval", |
467 | bs->timers.required_min_echo / 1000); | |
468 | ||
469 | json_object_int_add(resp, "remote-detect-multiplier", | |
470 | bs->remote_detect_mult); | |
471 | json_object_int_add(resp, "remote-receive-interval", | |
472 | bs->remote_timers.required_min_rx / 1000); | |
473 | json_object_int_add(resp, "remote-transmit-interval", | |
474 | bs->remote_timers.desired_min_tx / 1000); | |
475 | json_object_int_add(resp, "remote-echo-interval", | |
476 | bs->remote_timers.required_min_echo / 1000); | |
477 | ||
b88113ef | 478 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) |
e9e2c950 RZ |
479 | json_object_boolean_true_add(resp, "echo-mode"); |
480 | else | |
481 | json_object_boolean_false_add(resp, "echo-mode"); | |
482 | ||
b88113ef | 483 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) |
e9e2c950 RZ |
484 | json_object_boolean_true_add(resp, "shutdown"); |
485 | else | |
486 | json_object_boolean_false_add(resp, "shutdown"); | |
487 | ||
488 | skip_config: | |
489 | /* Generate JSON response. */ | |
490 | jsonstr = XSTRDUP( | |
491 | MTYPE_BFDD_NOTIFICATION, | |
492 | json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS)); | |
493 | json_object_put(resp); | |
494 | ||
495 | return jsonstr; | |
496 | } | |
497 | ||
498 | int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr, | |
499 | bpc_handle bh) | |
500 | { | |
501 | struct json_object *jo; | |
502 | ||
503 | jo = json_tokener_parse(jsonstr); | |
504 | if (jo == NULL) | |
505 | return -1; | |
506 | ||
507 | return parse_config_json(jo, bh, bcs); | |
508 | } | |
509 | ||
510 | static int json_object_add_peer(struct json_object *jo, struct bfd_session *bs) | |
511 | { | |
79b4a6fc RZ |
512 | char addr_buf[INET6_ADDRSTRLEN]; |
513 | ||
e9e2c950 | 514 | /* Add peer 'key' information. */ |
b88113ef | 515 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6)) |
e9e2c950 RZ |
516 | json_object_boolean_true_add(jo, "ipv6"); |
517 | else | |
518 | json_object_boolean_false_add(jo, "ipv6"); | |
519 | ||
b88113ef | 520 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) { |
e9e2c950 RZ |
521 | json_object_boolean_true_add(jo, "multihop"); |
522 | json_object_string_add(jo, "peer-address", | |
79b4a6fc RZ |
523 | inet_ntop(bs->key.family, &bs->key.peer, |
524 | addr_buf, sizeof(addr_buf))); | |
e9e2c950 | 525 | json_object_string_add(jo, "local-address", |
79b4a6fc RZ |
526 | inet_ntop(bs->key.family, &bs->key.local, |
527 | addr_buf, sizeof(addr_buf))); | |
528 | if (bs->key.vrfname[0]) | |
529 | json_object_string_add(jo, "vrf-name", bs->key.vrfname); | |
e9e2c950 RZ |
530 | } else { |
531 | json_object_boolean_false_add(jo, "multihop"); | |
532 | json_object_string_add(jo, "peer-address", | |
79b4a6fc RZ |
533 | inet_ntop(bs->key.family, &bs->key.peer, |
534 | addr_buf, sizeof(addr_buf))); | |
535 | if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) | |
536 | json_object_string_add( | |
537 | jo, "local-address", | |
538 | inet_ntop(bs->key.family, &bs->key.local, | |
539 | addr_buf, sizeof(addr_buf))); | |
540 | if (bs->key.ifname[0]) | |
e9e2c950 | 541 | json_object_string_add(jo, "local-interface", |
79b4a6fc | 542 | bs->key.ifname); |
e9e2c950 RZ |
543 | } |
544 | ||
545 | if (bs->pl) | |
546 | json_object_string_add(jo, "label", bs->pl->pl_label); | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | ||
552 | /* | |
553 | * Label handling | |
554 | */ | |
555 | struct peer_label *pl_find(const char *label) | |
556 | { | |
557 | struct peer_label *pl; | |
558 | ||
559 | TAILQ_FOREACH (pl, &bglobal.bg_pllist, pl_entry) { | |
560 | if (strcmp(pl->pl_label, label) != 0) | |
561 | continue; | |
562 | ||
563 | return pl; | |
564 | } | |
565 | ||
566 | return NULL; | |
567 | } | |
568 | ||
569 | struct peer_label *pl_new(const char *label, struct bfd_session *bs) | |
570 | { | |
571 | struct peer_label *pl; | |
572 | ||
573 | pl = XCALLOC(MTYPE_BFDD_LABEL, sizeof(*pl)); | |
e9e2c950 RZ |
574 | |
575 | if (strlcpy(pl->pl_label, label, sizeof(pl->pl_label)) | |
576 | > sizeof(pl->pl_label)) | |
259b64eb | 577 | zlog_warn("%s:%d: label was truncated", __func__, __LINE__); |
e9e2c950 RZ |
578 | |
579 | pl->pl_bs = bs; | |
580 | bs->pl = pl; | |
581 | ||
582 | TAILQ_INSERT_HEAD(&bglobal.bg_pllist, pl, pl_entry); | |
583 | ||
584 | return pl; | |
585 | } | |
586 | ||
587 | void pl_free(struct peer_label *pl) | |
588 | { | |
589 | if (pl == NULL) | |
590 | return; | |
591 | ||
592 | /* Remove the pointer back. */ | |
593 | pl->pl_bs->pl = NULL; | |
594 | ||
595 | TAILQ_REMOVE(&bglobal.bg_pllist, pl, pl_entry); | |
596 | XFREE(MTYPE_BFDD_LABEL, pl); | |
597 | } |