Debian Codegraph @

src/network/networkd-json.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/nexthop.h>
4
5 #include "sd-device.h"
6 #include "sd-dhcp-client.h"
7 #include "sd-dhcp6-client.h"
8
9 #include "dhcp-lease-internal.h" /* IWYU pragma: keep */
10 #include "dhcp-server-internal.h"
11 #include "dhcp-server-lease-internal.h"
12 #include "dhcp6-lease-internal.h"
13 #include "extract-word.h"
14 #include "in-addr-util.h"
15 #include "iovec-util.h"
16 #include "ip-protocol-list.h"
17 #include "json-util.h"
18 #include "netif-util.h"
19 #include "networkd-address.h"
20 #include "networkd-dhcp-common.h"
21 #include "networkd-json.h"
22 #include "networkd-link.h"
23 #include "networkd-manager.h"
24 #include "networkd-neighbor.h"
25 #include "networkd-network.h"
26 #include "networkd-nexthop.h"
27 #include "networkd-ntp.h"
28 #include "networkd-route.h"
29 #include "networkd-route-util.h"
30 #include "networkd-routing-policy-rule.h"
31 #include "networkd-speed-meter.h"
32 #include "networkd-wwan.h"
33 #include "ordered-set.h"
34 #include "set.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "udev-util.h"
38 #include "wifi-util.h"
39
40 static int address_append_json(Address *address, bool serializing, sd_json_variant **array) {
41 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
42 int r;
43
44 assert(address);
45 assert(array);
46
47 r = sd_json_buildo(
48 &v,
49 SD_JSON_BUILD_PAIR_INTEGER("Family", address->family),
50 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Address", address->family, &address->in_addr),
51 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Peer", address->family, &address->in_addr_peer),
52 SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", address->prefixlen),
53 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(address->source)),
54 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", address->family, &address->provider));
55 if (r < 0)
56 return r;
57
58 if (!serializing) {
59 _cleanup_free_ char *scope = NULL, *flags = NULL, *state = NULL;
60
61 r = route_scope_to_string_alloc(address->scope, &scope);
62 if (r < 0)
63 return r;
64
65 r = address_flags_to_string_alloc(address->flags, address->family, &flags);
66 if (r < 0)
67 return r;
68
69 r = network_config_state_to_string_alloc(address->state, &state);
70 if (r < 0)
71 return r;
72
73 r = sd_json_variant_merge_objectbo(
74 &v,
75 JSON_BUILD_PAIR_IN4_ADDR_WITH_STRING_NON_NULL("Broadcast", &address->broadcast),
76 SD_JSON_BUILD_PAIR_UNSIGNED("Scope", address->scope),
77 SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
78 SD_JSON_BUILD_PAIR_UNSIGNED("Flags", address->flags),
79 SD_JSON_BUILD_PAIR_STRING("FlagsString", flags),
80 JSON_BUILD_PAIR_STRING_NON_EMPTY("Label", address->label),
81 JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", address->lifetime_preferred_usec),
82 JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUsec", address->lifetime_preferred_usec), /* for backward compat */
83 JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", address->lifetime_valid_usec),
84 JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUsec", address->lifetime_valid_usec), /* for backward compat */
85 SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
86 if (r < 0)
87 return r;
88 }
89
90 return sd_json_variant_append_array(array, v);
91 }
92
93 int addresses_append_json(Link *link, bool serializing, sd_json_variant **v) {
94 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
95 Address *address;
96 int r;
97
98 assert(link);
99 assert(v);
100
101 SET_FOREACH(address, link->addresses) {
102 if (serializing) {
103 if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
104 continue;
105 if (!address_is_ready(address))
106 continue;
107
108 log_address_debug(address, "Serializing", link);
109 }
110
111 r = address_append_json(address, serializing, &array);
112 if (r < 0)
113 return r;
114 }
115
116 return json_variant_set_field_non_null(v, "Addresses", array);
117 }
118
119 static int neighbor_append_json(Neighbor *n, sd_json_variant **array) {
120 _cleanup_free_ char *state = NULL;
121 int r;
122
123 assert(n);
124 assert(array);
125
126 r = network_config_state_to_string_alloc(n->state, &state);
127 if (r < 0)
128 return r;
129
130 return sd_json_variant_append_arraybo(
131 array,
132 SD_JSON_BUILD_PAIR_INTEGER("Family", n->dst_addr.family),
133 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Destination", n->dst_addr.family, &n->dst_addr.address),
134 JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr),
135 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
136 SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
137 }
138
139 static int neighbors_append_json(Set *neighbors, sd_json_variant **v) {
140 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
141 Neighbor *neighbor;
142 int r;
143
144 assert(v);
145
146 SET_FOREACH(neighbor, neighbors) {
147 r = neighbor_append_json(neighbor, &array);
148 if (r < 0)
149 return r;
150 }
151
152 return json_variant_set_field_non_null(v, "Neighbors", array);
153 }
154
155 static int nexthop_group_build_json(NextHop *nexthop, sd_json_variant **ret) {
156 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
157 struct nexthop_grp *g;
158 int r;
159
160 assert(nexthop);
161 assert(ret);
162
163 HASHMAP_FOREACH(g, nexthop->group) {
164 r = sd_json_variant_append_arraybo(
165 &array,
166 SD_JSON_BUILD_PAIR_UNSIGNED("ID", g->id),
167 SD_JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1));
168 if (r < 0)
169 return r;
170 }
171
172 *ret = TAKE_PTR(array);
173 return 0;
174 }
175
176 static int nexthop_append_json(NextHop *n, bool serializing, sd_json_variant **array) {
177 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
178 int r;
179
180 assert(n);
181 assert(array);
182
183 r = sd_json_buildo(
184 &v,
185 SD_JSON_BUILD_PAIR_UNSIGNED("ID", n->id),
186 SD_JSON_BUILD_PAIR_INTEGER("Family", n->family),
187 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
188 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", n->family, &n->provider));
189 if (r < 0)
190 return r;
191
192 if (!serializing) {
193 _cleanup_(sd_json_variant_unrefp) sd_json_variant *group = NULL;
194 _cleanup_free_ char *flags = NULL, *protocol = NULL, *state = NULL;
195
196 r = route_flags_to_string_alloc(n->flags, &flags);
197 if (r < 0)
198 return r;
199
200 r = route_protocol_to_string_alloc(n->protocol, &protocol);
201 if (r < 0)
202 return r;
203
204 r = network_config_state_to_string_alloc(n->state, &state);
205 if (r < 0)
206 return r;
207
208 r = nexthop_group_build_json(n, &group);
209 if (r < 0)
210 return r;
211
212 r = sd_json_variant_merge_objectbo(
213 &v,
214 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Gateway", n->family, &n->gw.address),
215 SD_JSON_BUILD_PAIR_UNSIGNED("Flags", n->flags),
216 SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
217 SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", n->protocol),
218 SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
219 SD_JSON_BUILD_PAIR_BOOLEAN("Blackhole", n->blackhole),
220 JSON_BUILD_PAIR_VARIANT_NON_NULL("Group", group),
221 SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
222 }
223
224 return sd_json_variant_append_array(array, v);
225 }
226
227 int nexthops_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
228 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
229 NextHop *nexthop;
230 int r;
231
232 assert(manager);
233 assert(v);
234
235 HASHMAP_FOREACH(nexthop, manager->nexthops_by_id) {
236 if (ifindex >= 0) {
237 if (nexthop->ifindex != ifindex)
238 continue;
239 } else {
240 /* negative ifindex means we are serializing now. */
241
242 if (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN)
243 continue;
244 if (!nexthop_exists(nexthop))
245 continue;
246
247 log_nexthop_debug(nexthop, "Serializing", manager);
248 }
249
250 r = nexthop_append_json(nexthop, /* serializing= */ ifindex < 0, &array);
251 if (r < 0)
252 return r;
253 }
254
255 return json_variant_set_field_non_null(v, "NextHops", array);
256 }
257
258 static int route_append_json(Route *route, bool serializing, sd_json_variant **array) {
259 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
260 int r;
261
262 assert(route);
263 assert(array);
264
265 r = sd_json_buildo(
266 &v,
267 SD_JSON_BUILD_PAIR_INTEGER("Family", route->family),
268 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Destination", route->family, &route->dst),
269 SD_JSON_BUILD_PAIR_UNSIGNED("DestinationPrefixLength", route->dst_prefixlen),
270 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Gateway", route->nexthop.family, &route->nexthop.gw),
271 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("Source", route->family, route->src_prefixlen > 0 ? &route->src : NULL),
272 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("SourcePrefixLength", route->src_prefixlen),
273 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("PreferredSource", route->family, &route->prefsrc),
274 SD_JSON_BUILD_PAIR_UNSIGNED("TOS", route->tos),
275 SD_JSON_BUILD_PAIR_UNSIGNED("Scope", route->scope),
276 SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", route->protocol),
277 SD_JSON_BUILD_PAIR_UNSIGNED("Type", route->type),
278 SD_JSON_BUILD_PAIR_UNSIGNED("Priority", route->priority),
279 SD_JSON_BUILD_PAIR_UNSIGNED("Table", route->table),
280 SD_JSON_BUILD_PAIR_UNSIGNED("Flags", route->flags),
281 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("NextHopID", route->nexthop_id),
282 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(route->source)),
283 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", route->family, &route->provider));
284 if (r < 0)
285 return r;
286
287 if (serializing) {
288 r = sd_json_variant_merge_objectbo(
289 &v,
290 SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", route->nexthop.ifindex),
291 JSON_BUILD_PAIR_BYTE_ARRAY_NON_EMPTY("Metrics", route->metric.metrics, route->metric.n_metrics),
292 JSON_BUILD_PAIR_STRING_NON_EMPTY("TCPCongestionControlAlgorithm", route->metric.tcp_congestion_control_algo));
293 if (r < 0)
294 return r;
295 } else {
296 _cleanup_free_ char *scope = NULL, *protocol = NULL, *table = NULL, *flags = NULL, *state = NULL;
297
298 r = route_scope_to_string_alloc(route->scope, &scope);
299 if (r < 0)
300 return r;
301
302 r = route_protocol_to_string_alloc(route->protocol, &protocol);
303 if (r < 0)
304 return r;
305
306 r = manager_get_route_table_to_string(route->manager, route->table, /* append_num= */ false, &table);
307 if (r < 0)
308 return r;
309
310 r = route_flags_to_string_alloc(route->flags, &flags);
311 if (r < 0)
312 return r;
313
314 r = network_config_state_to_string_alloc(route->state, &state);
315 if (r < 0)
316 return r;
317
318 r = sd_json_variant_merge_objectbo(
319 &v,
320 SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
321 SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
322 SD_JSON_BUILD_PAIR_STRING("TypeString", route_type_to_string(route->type)),
323 SD_JSON_BUILD_PAIR_STRING("TableString", table),
324 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("MTU", route_metric_get(&route->metric, RTAX_MTU)),
325 SD_JSON_BUILD_PAIR_UNSIGNED("Preference", route->pref),
326 SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
327 JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", route->lifetime_usec),
328 SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
329 if (r < 0)
330 return r;
331 }
332
333 return sd_json_variant_append_array(array, v);
334 }
335
336 int routes_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
337 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
338 Route *route;
339 int r;
340
341 assert(manager);
342 assert(v);
343
344 SET_FOREACH(route, manager->routes) {
345 if (ifindex >= 0) {
346 if (route->nexthop.ifindex != ifindex)
347 continue;
348 } else {
349 /* negative ifindex means we are serializing now. */
350
351 if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN)
352 continue;
353 if (!route_exists(route))
354 continue;
355
356 log_route_debug(route, "Serializing", manager);
357 }
358
359 r = route_append_json(route, /* serializing= */ ifindex < 0, &array);
360 if (r < 0)
361 return r;
362 }
363
364 return json_variant_set_field_non_null(v, "Routes", array);
365 }
366
367 static int routing_policy_rule_append_json(RoutingPolicyRule *rule, sd_json_variant **array) {
368 _cleanup_free_ char *table = NULL, *protocol = NULL, *state = NULL;
369 int r;
370
371 assert(rule);
372 assert(rule->manager);
373 assert(array);
374
375 r = manager_get_route_table_to_string(rule->manager, rule->table, /* append_num= */ false, &table);
376 if (r < 0 && r != -EINVAL)
377 return r;
378
379 r = route_protocol_to_string_alloc(rule->protocol, &protocol);
380 if (r < 0)
381 return r;
382
383 r = network_config_state_to_string_alloc(rule->state, &state);
384 if (r < 0)
385 return r;
386
387 return sd_json_variant_append_arraybo(
388 array,
389 SD_JSON_BUILD_PAIR_INTEGER("Family", rule->family),
390 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("FromPrefix", rule->family, &rule->from.address),
391 SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->from.address),
392 "FromPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->from.prefixlen)),
393 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ToPrefix", rule->family, &rule->to.address),
394 SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->to.address),
395 "ToPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->to.prefixlen)),
396 SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", rule->protocol),
397 SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
398 SD_JSON_BUILD_PAIR_UNSIGNED("TOS", rule->tos),
399 SD_JSON_BUILD_PAIR_UNSIGNED("Type", rule->action),
400 SD_JSON_BUILD_PAIR_STRING("TypeString", fr_act_type_to_string(rule->action)),
401 SD_JSON_BUILD_PAIR_UNSIGNED("IPProtocol", rule->ipproto),
402 SD_JSON_BUILD_PAIR_STRING("IPProtocolString", ip_protocol_to_name(rule->ipproto)),
403 SD_JSON_BUILD_PAIR_UNSIGNED("Priority", rule->priority),
404 SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMark", rule->fwmark),
405 SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMask", rule->fwmask),
406 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Table", rule->table),
407 JSON_BUILD_PAIR_STRING_NON_EMPTY("TableString", table),
408 SD_JSON_BUILD_PAIR_BOOLEAN("Invert", FLAGS_SET(rule->flags, FIB_RULE_INVERT)),
409 SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_prefixlen >= 0,
410 "SuppressPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->suppress_prefixlen)),
411 SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_ifgroup >= 0,
412 "SuppressInterfaceGroup", SD_JSON_BUILD_UNSIGNED(rule->suppress_ifgroup)),
413 SD_JSON_BUILD_PAIR_CONDITION(rule->sport.start != 0 || rule->sport.end != 0, "SourcePort",
414 SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->sport.start), SD_JSON_BUILD_UNSIGNED(rule->sport.end))),
415 SD_JSON_BUILD_PAIR_CONDITION(rule->dport.start != 0 || rule->dport.end != 0, "DestinationPort",
416 SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->dport.start), SD_JSON_BUILD_UNSIGNED(rule->dport.end))),
417 SD_JSON_BUILD_PAIR_CONDITION(rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID, "User",
418 SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->uid_range.start), SD_JSON_BUILD_UNSIGNED(rule->uid_range.end))),
419 JSON_BUILD_PAIR_STRING_NON_EMPTY("IncomingInterface", rule->iif),
420 JSON_BUILD_PAIR_STRING_NON_EMPTY("OutgoingInterface", rule->oif),
421 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(rule->source)),
422 SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
423 }
424
425 static int routing_policy_rules_append_json(Set *rules, sd_json_variant **v) {
426 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
427 RoutingPolicyRule *rule;
428 int r;
429
430 assert(v);
431
432 SET_FOREACH(rule, rules) {
433 r = routing_policy_rule_append_json(rule, &array);
434 if (r < 0)
435 return r;
436 }
437
438 return json_variant_set_field_non_null(v, "RoutingPolicyRules", array);
439 }
440
441 static int network_append_json(Network *network, sd_json_variant **v) {
442 assert(v);
443
444 if (!network)
445 return 0;
446
447 return sd_json_variant_merge_objectbo(
448 v,
449 SD_JSON_BUILD_PAIR_STRING("NetworkFile", network->filename),
450 SD_JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins),
451 SD_JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online > 0),
452 SD_JSON_BUILD_PAIR_CONDITION(
453 operational_state_range_is_valid(&network->required_operstate_for_online),
454 "RequiredOperationalStateForOnline",
455 SD_JSON_BUILD_ARRAY(
456 SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.min)),
457 SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.max)))),
458 SD_JSON_BUILD_PAIR_STRING("RequiredFamilyForOnline", link_required_address_family_to_string(network->required_family_for_online)),
459 SD_JSON_BUILD_PAIR_STRING("ActivationPolicy", activation_policy_to_string(network->activation_policy)));
460 }
461
462 static int netdev_append_json(NetDev *netdev, sd_json_variant **v) {
463 assert(v);
464
465 if (!netdev)
466 return 0;
467
468 return sd_json_variant_merge_objectbo(
469 v,
470 SD_JSON_BUILD_PAIR_STRING("NetDevFile", netdev->filename),
471 SD_JSON_BUILD_PAIR_STRV("NetDevFileDropins", netdev->dropins));
472 }
473
474 static int device_append_json(sd_device *device, sd_json_variant **v) {
475 _cleanup_strv_free_ char **link_dropins = NULL;
476 const char *link = NULL, *path = NULL, *vendor = NULL, *model = NULL, *joined;
477 int r;
478
479 assert(v);
480
481 if (!device)
482 return 0;
483
484 (void) sd_device_get_property_value(device, "ID_NET_LINK_FILE", &link);
485
486 if (sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) {
487 r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE);
488 if (r < 0)
489 return r;
490 }
491
492 (void) sd_device_get_property_value(device, "ID_PATH", &path);
493
494 (void) device_get_vendor_string(device, &vendor);
495 (void) device_get_model_string(device, &model);
496
497 return sd_json_variant_merge_objectbo(
498 v,
499 JSON_BUILD_PAIR_STRING_NON_EMPTY("LinkFile", link),
500 JSON_BUILD_PAIR_STRV_NON_EMPTY("LinkFileDropins", link_dropins),
501 JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", path),
502 JSON_BUILD_PAIR_STRING_NON_EMPTY("Vendor", vendor),
503 JSON_BUILD_PAIR_STRING_NON_EMPTY("Model", model));
504 }
505
506 static int dns_append_json_one(Link *link, const struct in_addr_full *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
507 assert(link);
508 assert(a);
509 assert(array);
510
511 if (a->ifindex != 0 && a->ifindex != link->ifindex)
512 return 0;
513
514 return sd_json_variant_append_arraybo(
515 array,
516 SD_JSON_BUILD_PAIR_INTEGER("Family", a->family),
517 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING("Address", a->family, &a->address),
518 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port),
519 SD_JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", SD_JSON_BUILD_INTEGER(a->ifindex)),
520 JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name),
521 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
522 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", a->family, p));
523 }
524
525 static int dns_append_json(Link *link, sd_json_variant **v) {
526 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
527 int r;
528
529 assert(link);
530 assert(v);
531
532 if (!link->network)
533 return 0;
534
535 if (link->n_dns != UINT_MAX)
536 for (unsigned i = 0; i < link->n_dns; i++) {
537 r = dns_append_json_one(link, link->dns[i], NETWORK_CONFIG_SOURCE_RUNTIME, NULL, &array);
538 if (r < 0)
539 return r;
540 }
541 else {
542 for (unsigned i = 0; i < link->network->n_dns; i++) {
543 r = dns_append_json_one(link, link->network->dns[i], NETWORK_CONFIG_SOURCE_STATIC, NULL, &array);
544 if (r < 0)
545 return r;
546 }
547
548 Bearer *b;
549
550 if (link_get_bearer(link, &b) >= 0)
551 FOREACH_ARRAY(dns, b->dns, b->n_dns) {
552 r = dns_append_json_one(link, *dns, NETWORK_CONFIG_SOURCE_MODEM_MANAGER, NULL, &array);
553 if (r < 0)
554 return r;
555 }
556
557 if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
558 const struct in_addr *dns;
559 union in_addr_union s;
560 int n_dns;
561
562 r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
563 if (r < 0)
564 return r;
565
566 n_dns = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
567 for (int i = 0; i < n_dns; i++) {
568 r = dns_append_json_one(link,
569 &(struct in_addr_full) { .family = AF_INET, .address.in = dns[i], },
570 NETWORK_CONFIG_SOURCE_DHCP4,
571 &s,
572 &array);
573 if (r < 0)
574 return r;
575 }
576 }
577
578 if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
579 const struct in6_addr *dns;
580 union in_addr_union s;
581 int n_dns;
582
583 r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
584 if (r < 0)
585 return r;
586
587 n_dns = sd_dhcp6_lease_get_dns(link->dhcp6_lease, &dns);
588 for (int i = 0; i < n_dns; i++) {
589 r = dns_append_json_one(link,
590 &(struct in_addr_full) { .family = AF_INET6, .address.in6 = dns[i], },
591 NETWORK_CONFIG_SOURCE_DHCP6,
592 &s,
593 &array);
594 if (r < 0)
595 return r;
596 }
597 }
598
599 if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
600 NDiscRDNSS *a;
601
602 SET_FOREACH(a, link->ndisc_rdnss) {
603 r = dns_append_json_one(link,
604 &(struct in_addr_full) { .family = AF_INET6, .address.in6 = a->address, },
605 NETWORK_CONFIG_SOURCE_NDISC,
606 &(union in_addr_union) { .in6 = a->router },
607 &array);
608 if (r < 0)
609 return r;
610 }
611 }
612 }
613
614 return json_variant_set_field_non_null(v, "DNS", array);
615 }
616
617 static int dnr_append_json_one(Link *link, const struct sd_dns_resolver *res, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
618 _cleanup_(sd_json_variant_unrefp) sd_json_variant *addrs_array = NULL;
619 _cleanup_strv_free_ char **transports = NULL;
620 int r;
621
622 assert(link);
623 assert(res);
624 assert(array);
625
626 FOREACH_ARRAY(addr, res->addrs, res->n_addrs) {
627 r = sd_json_variant_append_arrayb(
628 &addrs_array,
629 JSON_BUILD_IN_ADDR(res->family, addr));
630 if (r < 0)
631 return r;
632 }
633
634 r = dns_resolver_transports_to_strv(res->transports, &transports);
635 if (r < 0)
636 return r;
637
638 //FIXME ifindex?
639 return sd_json_variant_append_arrayb(
640 array,
641 SD_JSON_BUILD_OBJECT(
642 SD_JSON_BUILD_PAIR_INTEGER("Family", res->family),
643 SD_JSON_BUILD_PAIR_INTEGER("Priority", res->priority),
644 JSON_BUILD_PAIR_VARIANT_NON_NULL("Addresses", addrs_array),
645 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", res->port),
646 JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", res->auth_name),
647 JSON_BUILD_PAIR_STRING_NON_EMPTY("DoHPath", res->dohpath),
648 JSON_BUILD_PAIR_STRV_NON_EMPTY("Transports", transports),
649 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
650 JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", res->family, p)));
651 }
652
653 static int dnr_append_json(Link *link, sd_json_variant **v) {
654 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
655 int r;
656
657 assert(link);
658 assert(v);
659
660 if (!link->network)
661 return 0;
662
663 if (link->dhcp_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
664 struct sd_dns_resolver *dnr;
665 union in_addr_union s;
666 int n_dnr;
667
668 r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
669 if (r < 0)
670 return r;
671
672 n_dnr = sd_dhcp_lease_get_dnr(link->dhcp_lease, &dnr);
673 if (n_dnr > 0)
674 FOREACH_ARRAY(res, dnr, n_dnr) {
675 r = dnr_append_json_one(link,
676 res,
677 NETWORK_CONFIG_SOURCE_DHCP4,
678 &s,
679 &array);
680 if (r < 0)
681 return r;
682 }
683 }
684
685 if (link->dhcp6_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
686 struct sd_dns_resolver *dnr;
687 union in_addr_union s;
688 int n_dnr;
689
690 r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
691 if (r < 0)
692 return r;
693
694 n_dnr = sd_dhcp6_lease_get_dnr(link->dhcp6_lease, &dnr);
695 if (n_dnr > 0)
696 FOREACH_ARRAY(res, dnr, n_dnr) {
697 r = dnr_append_json_one(link,
698 res,
699 NETWORK_CONFIG_SOURCE_DHCP6,
700 &s,
701 &array);
702 if (r < 0)
703 return r;
704 }
705 }
706
707 if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_NDISC)) {
708 NDiscDNR *a;
709
710 SET_FOREACH(a, link->ndisc_dnr) {
711 r = dnr_append_json_one(link,
712 &a->resolver,
713 NETWORK_CONFIG_SOURCE_NDISC,
714 &(union in_addr_union) { .in6 = a->router },
715 &array);
716 if (r < 0)
717 return r;
718 }
719 }
720
721 return json_variant_set_field_non_null(v, "DNR", array);
722 }
723
724 static int server_append_json_one_addr(int family, const union in_addr_union *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
725 _cleanup_free_ char *address_str = NULL;
726 int r;
727
728 assert(IN_SET(family, AF_INET, AF_INET6));
729 assert(a);
730 assert(array);
731
732 r = in_addr_to_string(family, a, &address_str);
733 if (r < 0)
734 return r;
735
736 return sd_json_variant_append_arraybo(
737 array,
738 SD_JSON_BUILD_PAIR_INTEGER("Family", family),
739 JSON_BUILD_PAIR_IN_ADDR("Address", family, a),
740 SD_JSON_BUILD_PAIR_STRING("AddressString", address_str),
741 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
742 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
743 }
744
745 static int server_append_json_one_fqdn(int family, const char *fqdn, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
746 assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
747 assert(fqdn);
748 assert(array);
749
750 return sd_json_variant_append_arraybo(
751 array,
752 SD_JSON_BUILD_PAIR_STRING("Server", fqdn),
753 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
754 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
755 }
756
757 static int server_append_json_one_string(const char *str, NetworkConfigSource s, sd_json_variant **array) {
758 union in_addr_union a;
759 int family;
760
761 assert(str);
762
763 if (in_addr_from_string_auto(str, &family, &a) >= 0)
764 return server_append_json_one_addr(family, &a, s, NULL, array);
765
766 return server_append_json_one_fqdn(AF_UNSPEC, str, s, NULL, array);
767 }
768
769 static int ntp_append_json(Link *link, sd_json_variant **v) {
770 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
771 int r;
772
773 assert(link);
774 assert(v);
775
776 if (!link->network)
777 return 0;
778
779 STRV_FOREACH(p, link->ntp ?: link->network->ntp) {
780 r = server_append_json_one_string(*p, NETWORK_CONFIG_SOURCE_RUNTIME, &array);
781 if (r < 0)
782 return r;
783 }
784
785 if (!link->ntp) {
786 if (link->dhcp_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
787 const struct in_addr *ntp;
788 union in_addr_union s;
789 int n_ntp;
790
791 r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
792 if (r < 0)
793 return r;
794
795 n_ntp = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
796 for (int i = 0; i < n_ntp; i++) {
797 r = server_append_json_one_addr(AF_INET,
798 &(union in_addr_union) { .in = ntp[i], },
799 NETWORK_CONFIG_SOURCE_DHCP4,
800 &s,
801 &array);
802 if (r < 0)
803 return r;
804 }
805 }
806
807 if (link->dhcp6_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
808 const struct in6_addr *ntp_addr;
809 union in_addr_union s;
810 char **ntp_fqdn;
811 int n_ntp;
812
813 r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
814 if (r < 0)
815 return r;
816
817 n_ntp = sd_dhcp6_lease_get_ntp_addrs(link->dhcp6_lease, &ntp_addr);
818 for (int i = 0; i < n_ntp; i++) {
819 r = server_append_json_one_addr(AF_INET6,
820 &(union in_addr_union) { .in6 = ntp_addr[i], },
821 NETWORK_CONFIG_SOURCE_DHCP6,
822 &s,
823 &array);
824 if (r < 0)
825 return r;
826 }
827
828 n_ntp = sd_dhcp6_lease_get_ntp_fqdn(link->dhcp6_lease, &ntp_fqdn);
829 for (int i = 0; i < n_ntp; i++) {
830 r = server_append_json_one_fqdn(AF_INET6,
831 ntp_fqdn[i],
832 NETWORK_CONFIG_SOURCE_DHCP6,
833 &s,
834 &array);
835 if (r < 0)
836 return r;
837 }
838 }
839 }
840
841 return json_variant_set_field_non_null(v, "NTP", array);
842 }
843
844 static int domain_append_json(int family, const char *domain, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
845 assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
846 assert(domain);
847 assert(array);
848
849 return sd_json_variant_append_arraybo(
850 array,
851 SD_JSON_BUILD_PAIR_STRING("Domain", domain),
852 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
853 JSON_BUILD_PAIR_IN_ADDR_WITH_STRING_NON_NULL("ConfigProvider", family, p));
854 }
855
856 static int sip_append_json(Link *link, sd_json_variant **v) {
857 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
858 int r;
859
860 assert(link);
861 assert(v);
862
863 if (!link->network)
864 return 0;
865
866 if (link->dhcp_lease && link->network->dhcp_use_sip) {
867 const struct in_addr *sip;
868 union in_addr_union s;
869 int n_sip;
870
871 r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
872 if (r < 0)
873 return r;
874
875 n_sip = sd_dhcp_lease_get_sip(link->dhcp_lease, &sip);
876 for (int i = 0; i < n_sip; i++) {
877 r = server_append_json_one_addr(AF_INET,
878 &(union in_addr_union) { .in = sip[i], },
879 NETWORK_CONFIG_SOURCE_DHCP4,
880 &s,
881 &array);
882 if (r < 0)
883 return r;
884 }
885
886 }
887
888 if (link->dhcp6_lease && link->network->dhcp6_use_sip) {
889 const struct in6_addr *sip_addr;
890 union in_addr_union s;
891 char **domains;
892 int n_sip;
893
894 r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
895 if (r < 0)
896 return r;
897
898 n_sip = sd_dhcp6_lease_get_sip_addrs(link->dhcp6_lease, &sip_addr);
899 for (int i = 0; i < n_sip; i++) {
900 r = server_append_json_one_addr(AF_INET6,
901 &(union in_addr_union) { .in6 = sip_addr[i], },
902 NETWORK_CONFIG_SOURCE_DHCP6,
903 &s,
904 &array);
905 if (r < 0)
906 return r;
907 }
908
909 if (sd_dhcp6_lease_get_sip_domains(link->dhcp6_lease, &domains) >= 0)
910 STRV_FOREACH(p, domains) {
911 r = domain_append_json(AF_INET6,
912 *p,
913 NETWORK_CONFIG_SOURCE_DHCP6,
914 &s,
915 &array);
916 if (r < 0)
917 return r;
918 }
919
920 }
921
922 return json_variant_set_field_non_null(v, "SIP", array);
923 }
924
925 static int domains_append_json(Link *link, bool is_route, sd_json_variant **v) {
926 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
927 OrderedSet *link_domains, *network_domains;
928 UseDomains use_domains;
929 union in_addr_union s;
930 char **domains;
931 const char *domain;
932 int r;
933
934 assert(link);
935 assert(v);
936
937 if (!link->network)
938 return 0;
939
940 link_domains = is_route ? link->route_domains : link->search_domains;
941 network_domains = is_route ? link->network->route_domains : link->network->search_domains;
942 use_domains = is_route ? USE_DOMAINS_ROUTE : USE_DOMAINS_YES;
943
944 ORDERED_SET_FOREACH(domain, link_domains ?: network_domains) {
945 r = domain_append_json(AF_UNSPEC, domain,
946 link_domains ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC,
947 NULL, &array);
948 if (r < 0)
949 return r;
950 }
951
952 if (!link_domains) {
953 if (link->dhcp_lease &&
954 link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) == use_domains) {
955 r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
956 if (r < 0)
957 return r;
958
959 if (sd_dhcp_lease_get_domainname(link->dhcp_lease, &domain) >= 0) {
960 r = domain_append_json(AF_INET, domain, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
961 if (r < 0)
962 return r;
963 }
964
965 if (sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains) >= 0)
966 STRV_FOREACH(p, domains) {
967 r = domain_append_json(AF_INET, *p, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
968 if (r < 0)
969 return r;
970 }
971 }
972
973 if (link->dhcp6_lease &&
974 link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP6) == use_domains) {
975 r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
976 if (r < 0)
977 return r;
978
979 if (sd_dhcp6_lease_get_domains(link->dhcp6_lease, &domains) >= 0)
980 STRV_FOREACH(p, domains) {
981 r = domain_append_json(AF_INET6, *p, NETWORK_CONFIG_SOURCE_DHCP6, &s, &array);
982 if (r < 0)
983 return r;
984 }
985 }
986
987 if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_NDISC) == use_domains) {
988 NDiscDNSSL *a;
989
990 SET_FOREACH(a, link->ndisc_dnssl) {
991 r = domain_append_json(AF_INET6, ndisc_dnssl_domain(a), NETWORK_CONFIG_SOURCE_NDISC,
992 &(union in_addr_union) { .in6 = a->router },
993 &array);
994 if (r < 0)
995 return r;
996 }
997 }
998 }
999
1000 return json_variant_set_field_non_null(v, is_route ? "RouteDomains" : "SearchDomains", array);
1001 }
1002
1003 static int nta_append_json(const char *nta, NetworkConfigSource s, sd_json_variant **array) {
1004 assert(nta);
1005 assert(array);
1006
1007 return sd_json_variant_append_arraybo(
1008 array,
1009 SD_JSON_BUILD_PAIR_STRING("DNSSECNegativeTrustAnchor", nta),
1010 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)));
1011 }
1012
1013 static int ntas_append_json(Link *link, sd_json_variant **v) {
1014 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1015 const char *nta;
1016 int r;
1017
1018 assert(link);
1019 assert(v);
1020
1021 if (!link->network)
1022 return 0;
1023
1024 SET_FOREACH(nta, link->dnssec_negative_trust_anchors ?: link->network->dnssec_negative_trust_anchors) {
1025 r = nta_append_json(nta,
1026 link->dnssec_negative_trust_anchors ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC,
1027 &array);
1028 if (r < 0)
1029 return r;
1030 }
1031
1032 return json_variant_set_field_non_null(v, "DNSSECNegativeTrustAnchors", array);
1033 }
1034
1035 static int dns_misc_append_json(Link *link, sd_json_variant **v) {
1036 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1037 ResolveSupport resolve_support;
1038 NetworkConfigSource source;
1039 DnsOverTlsMode mode;
1040 int t, r;
1041
1042 assert(link);
1043 assert(v);
1044
1045 if (!link->network)
1046 return 0;
1047
1048 resolve_support = link->llmnr >= 0 ? link->llmnr : link->network->llmnr;
1049 if (resolve_support >= 0) {
1050 source = link->llmnr >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
1051
1052 r = sd_json_variant_append_arraybo(
1053 &array,
1054 SD_JSON_BUILD_PAIR_STRING("LLMNR", resolve_support_to_string(resolve_support)),
1055 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1056 if (r < 0)
1057 return r;
1058 }
1059
1060 resolve_support = link->mdns >= 0 ? link->mdns : link->network->mdns;
1061 if (resolve_support >= 0) {
1062 source = link->mdns >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
1063
1064 r = sd_json_variant_append_arraybo(
1065 &array,
1066 SD_JSON_BUILD_PAIR_STRING("MDNS", resolve_support_to_string(resolve_support)),
1067 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1068 if (r < 0)
1069 return r;
1070 }
1071
1072 t = link->dns_default_route >= 0 ? link->dns_default_route : link->network->dns_default_route;
1073 if (t >= 0) {
1074 source = link->dns_default_route >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
1075
1076 r = sd_json_variant_append_arraybo(
1077 &array,
1078 SD_JSON_BUILD_PAIR_BOOLEAN("DNSDefaultRoute", t),
1079 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1080 if (r < 0)
1081 return r;
1082 }
1083
1084 mode = link->dns_over_tls_mode >= 0 ? link->dns_over_tls_mode : link->network->dns_over_tls_mode;
1085 if (mode >= 0) {
1086 source = link->dns_over_tls_mode >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
1087
1088 r = sd_json_variant_append_arraybo(
1089 &array,
1090 SD_JSON_BUILD_PAIR_STRING("DNSOverTLS", dns_over_tls_mode_to_string(mode)),
1091 SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1092 if (r < 0)
1093 return r;
1094 }
1095
1096 return json_variant_set_field_non_null(v, "DNSSettings", array);
1097 }
1098
1099 static int captive_portal_append_json(Link *link, sd_json_variant **v) {
1100 const char *captive_portal;
1101 int r;
1102
1103 assert(link);
1104 assert(v);
1105
1106 r = link_get_captive_portal(link, &captive_portal);
1107 if (r <= 0)
1108 return r;
1109
1110 return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal));
1111 }
1112
1113 static int pref64_append_json(Link *link, sd_json_variant **v) {
1114 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *w = NULL;
1115 NDiscPREF64 *i;
1116 int r;
1117
1118 assert(link);
1119 assert(v);
1120
1121 if (!link->network || !link->network->ndisc_use_pref64)
1122 return 0;
1123
1124 SET_FOREACH(i, link->ndisc_pref64) {
1125 r = sd_json_variant_append_arraybo(
1126 &array,
1127 JSON_BUILD_PAIR_IN6_ADDR_WITH_STRING("Prefix", &i->prefix),
1128 SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", i->prefix_len),
1129 JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", i->lifetime_usec),
1130 JSON_BUILD_PAIR_IN6_ADDR_WITH_STRING_NON_NULL("ConfigProvider", &i->router));
1131 if (r < 0)
1132 return r;
1133 }
1134
1135 r = json_variant_set_field_non_null(&w, "PREF64", array);
1136 if (r < 0)
1137 return r;
1138
1139 return json_variant_set_field_non_null(v, "NDisc", w);
1140 }
1141
1142 static int dhcp_server_append_json(Link *link, sd_json_variant **v) {
1143 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1144 int r;
1145
1146 assert(link);
1147 assert(v);
1148
1149 if (!link->dhcp_server)
1150 return 0;
1151
1152 r = sd_json_buildo(
1153 &w,
1154 SD_JSON_BUILD_PAIR_UNSIGNED("PoolOffset", link->dhcp_server->pool_offset),
1155 SD_JSON_BUILD_PAIR_UNSIGNED("PoolSize", link->dhcp_server->pool_size));
1156 if (r < 0)
1157 return r;
1158
1159 r = dhcp_server_bound_leases_append_json(link->dhcp_server, &w);
1160 if (r < 0)
1161 return r;
1162
1163 r = dhcp_server_static_leases_append_json(link->dhcp_server, &w);
1164 if (r < 0)
1165 return r;
1166
1167 return json_variant_set_field_non_null(v, "DHCPServer", w);
1168 }
1169
1170 static int dhcp6_client_vendor_options_append_json(Link *link, sd_json_variant **v) {
1171 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1172 sd_dhcp6_option **options = NULL;
1173 int r, n_vendor_options;
1174
1175 assert(link);
1176 assert(v);
1177
1178 if (!link->dhcp6_lease)
1179 return 0;
1180
1181 n_vendor_options = sd_dhcp6_lease_get_vendor_options(link->dhcp6_lease, &options);
1182
1183 FOREACH_ARRAY(option, options, n_vendor_options) {
1184 r = sd_json_variant_append_arraybo(
1185 &array,
1186 SD_JSON_BUILD_PAIR_UNSIGNED("EnterpriseId", (*option)->enterprise_identifier),
1187 SD_JSON_BUILD_PAIR_UNSIGNED("SubOptionCode", (*option)->option),
1188 SD_JSON_BUILD_PAIR_HEX("SubOptionData", (*option)->data, (*option)->length));
1189 if (r < 0)
1190 return r;
1191 }
1192
1193 return json_variant_set_field_non_null(v, "VendorSpecificOptions", array);
1194 }
1195
1196 static int dhcp6_client_lease_append_json(Link *link, sd_json_variant **v) {
1197 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1198 usec_t ts = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
1199 int r;
1200
1201 assert(link);
1202 assert(v);
1203
1204 if (!link->dhcp6_lease)
1205 return 0;
1206
1207 r = sd_dhcp6_lease_get_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &ts);
1208 if (r < 0 && r != -ENODATA)
1209 return r;
1210
1211 r = sd_dhcp6_lease_get_t1_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t1);
1212 if (r < 0 && r != -ENODATA)
1213 return r;
1214
1215 r = sd_dhcp6_lease_get_t2_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t2);
1216 if (r < 0 && r != -ENODATA)
1217 return r;
1218
1219 r = sd_json_buildo(
1220 &w,
1221 JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1),
1222 JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2),
1223 JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", ts));
1224 if (r < 0)
1225 return r;
1226
1227 return json_variant_set_field_non_null(v, "Lease", w);
1228 }
1229
1230 static int dhcp6_client_pd_append_json(Link *link, sd_json_variant **v) {
1231 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1232 int r;
1233
1234 assert(link);
1235 assert(link->network);
1236 assert(v);
1237
1238 if (!link->network->dhcp6_use_pd_prefix ||
1239 !sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease))
1240 return 0;
1241
1242 FOREACH_DHCP6_PD_PREFIX(link->dhcp6_lease) {
1243 usec_t lifetime_preferred_usec, lifetime_valid_usec;
1244 struct in6_addr prefix;
1245 uint8_t prefix_len;
1246 _cleanup_free_ char *prefix_str = NULL;
1247
1248 r = sd_dhcp6_lease_get_pd_prefix(link->dhcp6_lease, &prefix, &prefix_len);
1249 if (r < 0)
1250 return r;
1251
1252 r = sd_dhcp6_lease_get_pd_lifetime_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME,
1253 &lifetime_preferred_usec, &lifetime_valid_usec);
1254 if (r < 0)
1255 return r;
1256
1257 if (in6_addr_is_set(&prefix)) {
1258 r = in6_addr_to_string(&prefix, &prefix_str);
1259 if (r < 0)
1260 return r;
1261 }
1262
1263 r = sd_json_variant_append_arraybo(
1264 &array,
1265 JSON_BUILD_PAIR_IN6_ADDR("Prefix", &prefix),
1266 JSON_BUILD_PAIR_STRING_NON_EMPTY("PrefixString", prefix_str),
1267 SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", prefix_len),
1268 JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", lifetime_preferred_usec),
1269 JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", lifetime_valid_usec));
1270 if (r < 0)
1271 return r;
1272 }
1273
1274 return json_variant_set_field_non_null(v, "Prefixes", array);
1275 }
1276
1277 static int dhcp6_client_duid_append_json(Link *link, sd_json_variant **v) {
1278 const sd_dhcp_duid *duid;
1279 const void *data;
1280 size_t data_size;
1281 int r;
1282
1283 assert(link);
1284 assert(v);
1285
1286 if (!link->dhcp6_client)
1287 return 0;
1288
1289 r = sd_dhcp6_client_get_duid(link->dhcp6_client, &duid);
1290 if (r < 0)
1291 return 0;
1292
1293 r = sd_dhcp_duid_get_raw(duid, &data, &data_size);
1294 if (r < 0)
1295 return 0;
1296
1297 return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_BYTE_ARRAY("DUID", data, data_size));
1298 }
1299
1300 static int dhcp6_client_append_json(Link *link, sd_json_variant **v) {
1301 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1302 int r;
1303
1304 assert(link);
1305 assert(v);
1306
1307 if (!link->dhcp6_client)
1308 return 0;
1309
1310 r = dhcp6_client_lease_append_json(link, &w);
1311 if (r < 0)
1312 return r;
1313
1314 r = dhcp6_client_pd_append_json(link, &w);
1315 if (r < 0)
1316 return r;
1317
1318 r = dhcp6_client_vendor_options_append_json(link, &w);
1319 if (r < 0)
1320 return r;
1321
1322 r = dhcp6_client_duid_append_json(link, &w);
1323 if (r < 0)
1324 return r;
1325
1326 return json_variant_set_field_non_null(v, "DHCPv6Client", w);
1327 }
1328
1329 static int dhcp_client_lease_append_json(Link *link, sd_json_variant **v) {
1330 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1331 usec_t lease_timestamp_usec = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
1332 const char *hostname = NULL;
1333 int r;
1334
1335 assert(link);
1336 assert(v);
1337
1338 if (!link->dhcp_client || !link->dhcp_lease)
1339 return 0;
1340
1341 r = sd_dhcp_lease_get_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lease_timestamp_usec);
1342 if (r < 0 && r != -ENODATA)
1343 return r;
1344
1345 r = sd_dhcp_lease_get_t1_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t1);
1346 if (r < 0 && r != -ENODATA)
1347 return r;
1348
1349 r = sd_dhcp_lease_get_t2_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t2);
1350 if (r < 0 && r != -ENODATA)
1351 return r;
1352
1353 r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
1354 if (r < 0 && r != -ENODATA)
1355 return r;
1356
1357 _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
1358 r = dhcp_message_build_json(link->dhcp_lease->message, &m);
1359 if (r < 0)
1360 return r;
1361
1362 r = sd_json_buildo(
1363 &w,
1364 JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec),
1365 JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1),
1366 JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2),
1367 JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", hostname),
1368 JSON_BUILD_PAIR_VARIANT_NON_NULL("Message", m));
1369 if (r < 0)
1370 return r;
1371
1372 return json_variant_set_field_non_null(v, "Lease", w);
1373 }
1374
1375 static int dhcp_client_pd_append_json(Link *link, sd_json_variant **v) {
1376 _cleanup_(sd_json_variant_unrefp) sd_json_variant *addresses = NULL, *array = NULL;
1377 uint8_t ipv4masklen, sixrd_prefixlen;
1378 struct in6_addr sixrd_prefix;
1379 const struct in_addr *br_addresses;
1380 size_t n_br_addresses = 0;
1381 _cleanup_free_ char *prefix_str = NULL;
1382 int r;
1383
1384 assert(link);
1385 assert(link->network);
1386 assert(v);
1387
1388 if (!link->network->dhcp_use_6rd || !sd_dhcp_lease_has_6rd(link->dhcp_lease))
1389 return 0;
1390
1391 r = sd_dhcp_lease_get_6rd(link->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, &n_br_addresses);
1392 if (r < 0)
1393 return r;
1394
1395 if (in6_addr_is_set(&sixrd_prefix)) {
1396 r = in6_addr_to_string(&sixrd_prefix, &prefix_str);
1397 if (r < 0)
1398 return r;
1399 }
1400
1401 FOREACH_ARRAY(br_address, br_addresses, n_br_addresses) {
1402 r = sd_json_variant_append_arrayb(&addresses, JSON_BUILD_IN4_ADDR(br_address));
1403 if (r < 0)
1404 return r;
1405 }
1406
1407 r = sd_json_buildo(
1408 &array,
1409 JSON_BUILD_PAIR_IN6_ADDR("Prefix", &sixrd_prefix),
1410 JSON_BUILD_PAIR_STRING_NON_EMPTY("PrefixString", prefix_str),
1411 SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", sixrd_prefixlen),
1412 SD_JSON_BUILD_PAIR_UNSIGNED("IPv4MaskLength", ipv4masklen),
1413 JSON_BUILD_PAIR_VARIANT_NON_NULL("BorderRouters", addresses));
1414 if (r < 0)
1415 return r;
1416
1417 return json_variant_set_field_non_null(v, "6rdPrefix", array);
1418 }
1419
1420 static int dhcp_client_private_options_append_json(Link *link, sd_json_variant **v) {
1421 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1422 int r;
1423
1424 assert(link);
1425 assert(v);
1426
1427 if (!link->dhcp_lease)
1428 return 0;
1429
1430 for (uint8_t i = SD_DHCP_OPTION_PRIVATE_BASE; i <= SD_DHCP_OPTION_PRIVATE_LAST; i++) {
1431 _cleanup_(iovec_done) struct iovec iov = {};
1432 r = dhcp_message_get_option_alloc(link->dhcp_lease->message, i, &iov);
1433 if (r == -ENODATA)
1434 continue;
1435 if (r < 0)
1436 return r;
1437
1438 r = sd_json_variant_append_arraybo(
1439 &array,
1440 SD_JSON_BUILD_PAIR_UNSIGNED("Option", i),
1441 SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", iov.iov_base, iov.iov_len));
1442 if (r < 0)
1443 return r;
1444 }
1445
1446 return json_variant_set_field_non_null(v, "PrivateOptions", array);
1447 }
1448
1449 static int dhcp_client_id_append_json(Link *link, sd_json_variant **v) {
1450 assert(link);
1451 assert(v);
1452
1453 if (!link->dhcp_client)
1454 return 0;
1455
1456 const sd_dhcp_client_id *client_id;
1457 if (sd_dhcp_client_get_client_id(link->dhcp_client, &client_id) < 0)
1458 return 0;
1459
1460 const void *data;
1461 size_t l;
1462 if (sd_dhcp_client_id_get_raw(client_id, &data, &l) < 0)
1463 return 0;
1464
1465 return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_BYTE_ARRAY("ClientIdentifier", data, l));
1466 }
1467
1468 static int dhcp_client_append_json(Link *link, sd_json_variant **v) {
1469 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1470 int r;
1471
1472 assert(link);
1473 assert(v);
1474
1475 if (!link->dhcp_client)
1476 return 0;
1477
1478 r = dhcp_client_lease_append_json(link, &w);
1479 if (r < 0)
1480 return r;
1481
1482 r = dhcp_client_pd_append_json(link, &w);
1483 if (r < 0)
1484 return r;
1485
1486 r = dhcp_client_private_options_append_json(link, &w);
1487 if (r < 0)
1488 return r;
1489
1490 r = dhcp_client_id_append_json(link, &w);
1491 if (r < 0)
1492 return r;
1493
1494 return json_variant_set_field_non_null(v, "DHCPv4Client", w);
1495 }
1496
1497 static int lldp_tx_append_json(Link *link, sd_json_variant **v) {
1498 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1499 int r;
1500
1501 assert(link);
1502 assert(v);
1503
1504 if (!link->lldp_tx)
1505 return 0;
1506
1507 r = sd_lldp_tx_describe(link->lldp_tx, &w);
1508 if (r < 0)
1509 return r;
1510
1511 return json_variant_set_field_non_null(v, "LLDP", w);
1512 }
1513
1514 int link_build_json(Link *link, sd_json_variant **ret) {
1515 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
1516 _cleanup_free_ char *type = NULL, *flags = NULL;
1517 int r;
1518
1519 assert(link);
1520 assert(ret);
1521
1522 r = net_get_type_string(link->dev, link->iftype, &type);
1523 if (r == -ENOMEM)
1524 return r;
1525
1526 r = link_flags_to_string_alloc(link->flags, &flags);
1527 if (r < 0)
1528 return r;
1529
1530 r = sd_json_buildo(
1531 &v,
1532 /* basic information */
1533 SD_JSON_BUILD_PAIR_INTEGER("Index", link->ifindex),
1534 SD_JSON_BUILD_PAIR_STRING("Name", link->ifname),
1535 JSON_BUILD_PAIR_STRV_NON_EMPTY("AlternativeNames", link->alternative_names),
1536 SD_JSON_BUILD_PAIR_CONDITION(link->master_ifindex > 0,
1537 "MasterInterfaceIndex", SD_JSON_BUILD_INTEGER(link->master_ifindex)),
1538 JSON_BUILD_PAIR_STRING_NON_EMPTY("Kind", link->kind),
1539 SD_JSON_BUILD_PAIR_STRING("Type", type),
1540 JSON_BUILD_PAIR_STRING_NON_EMPTY("Driver", link->driver),
1541 SD_JSON_BUILD_PAIR_UNSIGNED("Flags", link->flags),
1542 SD_JSON_BUILD_PAIR_STRING("FlagsString", flags),
1543 SD_JSON_BUILD_PAIR_UNSIGNED("KernelOperationalState", link->kernel_operstate),
1544 SD_JSON_BUILD_PAIR_STRING("KernelOperationalStateString", kernel_operstate_to_string(link->kernel_operstate)),
1545 SD_JSON_BUILD_PAIR_UNSIGNED("MTU", link->mtu),
1546 SD_JSON_BUILD_PAIR_UNSIGNED("MinimumMTU", link->min_mtu),
1547 SD_JSON_BUILD_PAIR_UNSIGNED("MaximumMTU", link->max_mtu),
1548 JSON_BUILD_PAIR_HW_ADDR_NON_NULL("HardwareAddress", &link->hw_addr),
1549 JSON_BUILD_PAIR_HW_ADDR_NON_NULL("PermanentHardwareAddress", &link->permanent_hw_addr),
1550 JSON_BUILD_PAIR_HW_ADDR_NON_NULL("BroadcastAddress", &link->bcast_addr),
1551 JSON_BUILD_PAIR_IN6_ADDR_WITH_STRING_NON_NULL("IPv6LinkLocalAddress", &link->ipv6ll_address),
1552 /* wlan information */
1553 SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceType",
1554 SD_JSON_BUILD_UNSIGNED(link->wlan_iftype)),
1555 SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceTypeString",
1556 SD_JSON_BUILD_STRING(nl80211_iftype_to_string(link->wlan_iftype))),
1557 JSON_BUILD_PAIR_STRING_NON_EMPTY("SSID", link->ssid),
1558 JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL("BSSID", &link->bssid),
1559 /* link state */
1560 SD_JSON_BUILD_PAIR_STRING("AdministrativeState", link_state_to_string(link->state)),
1561 SD_JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(link->operstate)),
1562 SD_JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(link->carrier_state)),
1563 JSON_BUILD_PAIR_ENUM("AddressState", link_address_state_to_string(link->address_state)),
1564 JSON_BUILD_PAIR_ENUM("IPv4AddressState", link_address_state_to_string(link->ipv4_address_state)),
1565 JSON_BUILD_PAIR_ENUM("IPv6AddressState", link_address_state_to_string(link->ipv6_address_state)),
1566 JSON_BUILD_PAIR_ENUM("OnlineState", link_online_state_to_string(link->online_state)));
1567 if (r < 0)
1568 return r;
1569
1570 r = network_append_json(link->network, &v);
1571 if (r < 0)
1572 return r;
1573
1574 r = netdev_append_json(link->netdev, &v);
1575 if (r < 0)
1576 return r;
1577
1578 r = device_append_json(link->dev, &v);
1579 if (r < 0)
1580 return r;
1581
1582 r = dns_append_json(link, &v);
1583 if (r < 0)
1584 return r;
1585
1586 r = dnr_append_json(link, &v);
1587 if (r < 0)
1588 return r;
1589
1590 r = ntp_append_json(link, &v);
1591 if (r < 0)
1592 return r;
1593
1594 r = sip_append_json(link, &v);
1595 if (r < 0)
1596 return r;
1597
1598 r = domains_append_json(link, /* is_route= */ false, &v);
1599 if (r < 0)
1600 return r;
1601
1602 r = domains_append_json(link, /* is_route= */ true, &v);
1603 if (r < 0)
1604 return r;
1605
1606 r = ntas_append_json(link, &v);
1607 if (r < 0)
1608 return r;
1609
1610 r = dns_misc_append_json(link, &v);
1611 if (r < 0)
1612 return r;
1613
1614 r = captive_portal_append_json(link, &v);
1615 if (r < 0)
1616 return r;
1617
1618 r = pref64_append_json(link, &v);
1619 if (r < 0)
1620 return r;
1621
1622 r = addresses_append_json(link, /* serializing= */ false, &v);
1623 if (r < 0)
1624 return r;
1625
1626 r = neighbors_append_json(link->neighbors, &v);
1627 if (r < 0)
1628 return r;
1629
1630 r = nexthops_append_json(link->manager, link->ifindex, &v);
1631 if (r < 0)
1632 return r;
1633
1634 r = routes_append_json(link->manager, link->ifindex, &v);
1635 if (r < 0)
1636 return r;
1637
1638 r = dhcp_server_append_json(link, &v);
1639 if (r < 0)
1640 return r;
1641
1642 r = dhcp_client_append_json(link, &v);
1643 if (r < 0)
1644 return r;
1645
1646 r = dhcp6_client_append_json(link, &v);
1647 if (r < 0)
1648 return r;
1649
1650 r = lldp_tx_append_json(link, &v);
1651 if (r < 0)
1652 return r;
1653
1654 /* Append BitRates if speed meter is active */
1655 uint64_t tx, rx;
1656 link_get_bit_rates(link, &tx, &rx);
1657 if (tx != UINT64_MAX && rx != UINT64_MAX) {
1658 r = sd_json_variant_merge_objectbo(
1659 &v,
1660 SD_JSON_BUILD_PAIR("BitRates",
1661 SD_JSON_BUILD_OBJECT(
1662 SD_JSON_BUILD_PAIR_UNSIGNED("TxBitRate", tx),
1663 SD_JSON_BUILD_PAIR_UNSIGNED("RxBitRate", rx))));
1664 if (r < 0)
1665 return r;
1666 }
1667
1668 *ret = TAKE_PTR(v);
1669 return 0;
1670 }
1671
1672 static int links_append_json(Manager *manager, sd_json_variant **v) {
1673 _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1674 _cleanup_free_ Link **links = NULL;
1675 size_t n_links = 0;
1676 int r;
1677
1678 assert(manager);
1679 assert(v);
1680
1681 r = hashmap_dump_sorted(manager->links_by_index, (void***) &links, &n_links);
1682 if (r < 0)
1683 return r;
1684
1685 FOREACH_ARRAY(link, links, n_links) {
1686 _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL;
1687
1688 r = link_build_json(*link, &e);
1689 if (r < 0)
1690 return r;
1691
1692 r = sd_json_variant_append_array(&array, e);
1693 if (r < 0)
1694 return r;
1695 }
1696
1697 return json_variant_set_field_non_null(v, "Interfaces", array);
1698 }
1699
1700 int manager_build_json(Manager *manager, sd_json_variant **ret) {
1701 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
1702 int r;
1703
1704 assert(manager);
1705 assert(ret);
1706
1707 r = links_append_json(manager, &v);
1708 if (r < 0)
1709 return r;
1710
1711 r = nexthops_append_json(manager, /* ifindex= */ 0, &v);
1712 if (r < 0)
1713 return r;
1714
1715 r = routes_append_json(manager, /* ifindex= */ 0, &v);
1716 if (r < 0)
1717 return r;
1718
1719 r = routing_policy_rules_append_json(manager->rules, &v);
1720 if (r < 0)
1721 return r;
1722
1723 *ret = TAKE_PTR(v);
1724 return 0;
1725 }