• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 18052125394

26 Sep 2025 11:00PM UTC coverage: 72.224% (+0.02%) from 72.205%
18052125394

push

github

YHNdnzj
pam_systemd: correct alignment

Follow-up for cf2630aca

303350 of 420010 relevant lines covered (72.22%)

1058085.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.55
/src/network/networkd-json.c
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"
10
#include "dhcp-server-lease-internal.h"
11
#include "dhcp6-lease-internal.h"
12
#include "extract-word.h"
13
#include "ip-protocol-list.h"
14
#include "json-util.h"
15
#include "netif-util.h"
16
#include "networkd-address.h"
17
#include "networkd-dhcp-common.h"
18
#include "networkd-json.h"
19
#include "networkd-link.h"
20
#include "networkd-manager.h"
21
#include "networkd-neighbor.h"
22
#include "networkd-network.h"
23
#include "networkd-nexthop.h"
24
#include "networkd-ntp.h"
25
#include "networkd-route.h"
26
#include "networkd-route-util.h"
27
#include "networkd-routing-policy-rule.h"
28
#include "ordered-set.h"
29
#include "set.h"
30
#include "string-util.h"
31
#include "strv.h"
32
#include "udev-util.h"
33
#include "wifi-util.h"
34

35
static int address_append_json(Address *address, bool serializing, sd_json_variant **array) {
897✔
36
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
897✔
37
        int r;
897✔
38

39
        assert(address);
897✔
40
        assert(array);
897✔
41

42
        r = sd_json_buildo(
897✔
43
                        &v,
44
                        SD_JSON_BUILD_PAIR_INTEGER("Family", address->family),
45
                        JSON_BUILD_PAIR_IN_ADDR("Address", &address->in_addr, address->family),
46
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Peer", &address->in_addr_peer, address->family),
47
                        SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", address->prefixlen),
48
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(address->source)),
49
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", &address->provider, address->family));
50
        if (r < 0)
897✔
51
                return r;
52

53
        if (!serializing) {
897✔
54
                _cleanup_free_ char *scope = NULL, *flags = NULL, *state = NULL;
804✔
55

56
                r = route_scope_to_string_alloc(address->scope, &scope);
804✔
57
                if (r < 0)
804✔
58
                        return r;
59

60
                r = address_flags_to_string_alloc(address->flags, address->family, &flags);
804✔
61
                if (r < 0)
804✔
62
                        return r;
63

64
                r = network_config_state_to_string_alloc(address->state, &state);
804✔
65
                if (r < 0)
804✔
66
                        return r;
67

68
                r = sd_json_variant_merge_objectbo(
804✔
69
                                &v,
70
                                JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Broadcast", &address->broadcast),
71
                                SD_JSON_BUILD_PAIR_UNSIGNED("Scope", address->scope),
72
                                SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
73
                                SD_JSON_BUILD_PAIR_UNSIGNED("Flags", address->flags),
74
                                SD_JSON_BUILD_PAIR_STRING("FlagsString", flags),
75
                                JSON_BUILD_PAIR_STRING_NON_EMPTY("Label", address->label),
76
                                JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", address->lifetime_preferred_usec),
77
                                JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUsec", address->lifetime_preferred_usec), /* for backward compat */
78
                                JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", address->lifetime_valid_usec),
79
                                JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUsec", address->lifetime_valid_usec), /* for backward compat */
80
                                SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
81
                if (r < 0)
804✔
82
                        return r;
83
        }
84

85
        return sd_json_variant_append_array(array, v);
897✔
86
}
87

88
int addresses_append_json(Link *link, bool serializing, sd_json_variant **v) {
637✔
89
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
637✔
90
        Address *address;
637✔
91
        int r;
637✔
92

93
        assert(link);
637✔
94
        assert(v);
637✔
95

96
        SET_FOREACH(address, link->addresses) {
1,590✔
97
                if (serializing) {
953✔
98
                        if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
149✔
99
                                continue;
56✔
100
                        if (!address_is_ready(address))
93✔
101
                                continue;
×
102

103
                        log_address_debug(address, "Serializing", link);
93✔
104
                }
105

106
                r = address_append_json(address, serializing, &array);
897✔
107
                if (r < 0)
897✔
108
                        return r;
×
109
        }
110

111
        return json_variant_set_field_non_null(v, "Addresses", array);
637✔
112
}
113

114
static int neighbor_append_json(Neighbor *n, sd_json_variant **array) {
8✔
115
        _cleanup_free_ char *state = NULL;
8✔
116
        int r;
8✔
117

118
        assert(n);
8✔
119
        assert(array);
8✔
120

121
        r = network_config_state_to_string_alloc(n->state, &state);
8✔
122
        if (r < 0)
8✔
123
                return r;
124

125
        return sd_json_variant_append_arraybo(
8✔
126
                        array,
127
                        SD_JSON_BUILD_PAIR_INTEGER("Family", n->dst_addr.family),
128
                        JSON_BUILD_PAIR_IN_ADDR("Destination", &n->dst_addr.address, n->dst_addr.family),
129
                        JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr),
130
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
131
                        SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
132
}
133

134
static int neighbors_append_json(Set *neighbors, sd_json_variant **v) {
580✔
135
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
136
        Neighbor *neighbor;
580✔
137
        int r;
580✔
138

139
        assert(v);
580✔
140

141
        SET_FOREACH(neighbor, neighbors) {
588✔
142
                r = neighbor_append_json(neighbor, &array);
8✔
143
                if (r < 0)
8✔
144
                        return r;
×
145
        }
146

147
        return json_variant_set_field_non_null(v, "Neighbors", array);
580✔
148
}
149

150
static int nexthop_group_build_json(NextHop *nexthop, sd_json_variant **ret) {
113✔
151
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
113✔
152
        struct nexthop_grp *g;
113✔
153
        int r;
113✔
154

155
        assert(nexthop);
113✔
156
        assert(ret);
113✔
157

158
        HASHMAP_FOREACH(g, nexthop->group) {
125✔
159
                r = sd_json_variant_append_arraybo(
12✔
160
                                &array,
161
                                SD_JSON_BUILD_PAIR_UNSIGNED("ID", g->id),
162
                                SD_JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1));
163
                if (r < 0)
12✔
164
                        return r;
×
165
        }
166

167
        *ret = TAKE_PTR(array);
113✔
168
        return 0;
113✔
169
}
170

171
static int nexthop_append_json(NextHop *n, bool serializing, sd_json_variant **array) {
118✔
172
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
118✔
173
        int r;
118✔
174

175
        assert(n);
118✔
176
        assert(array);
118✔
177

178
        r = sd_json_buildo(
118✔
179
                        &v,
180
                        SD_JSON_BUILD_PAIR_UNSIGNED("ID", n->id),
181
                        SD_JSON_BUILD_PAIR_INTEGER("Family", n->family),
182
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
183
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", &n->provider, n->family));
184
        if (r < 0)
118✔
185
                return r;
186

187
        if (!serializing) {
118✔
188
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *group = NULL;
113✔
189
                _cleanup_free_ char *flags = NULL, *protocol = NULL, *state = NULL;
113✔
190

191
                r = route_flags_to_string_alloc(n->flags, &flags);
113✔
192
                if (r < 0)
113✔
193
                        return r;
194

195
                r = route_protocol_to_string_alloc(n->protocol, &protocol);
113✔
196
                if (r < 0)
113✔
197
                        return r;
198

199
                r = network_config_state_to_string_alloc(n->state, &state);
113✔
200
                if (r < 0)
113✔
201
                        return r;
202

203
                r = nexthop_group_build_json(n, &group);
113✔
204
                if (r < 0)
113✔
205
                        return r;
206

207
                r = sd_json_variant_merge_objectbo(
220✔
208
                                &v,
209
                                JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Gateway", &n->gw.address, n->family),
210
                                SD_JSON_BUILD_PAIR_UNSIGNED("Flags", n->flags),
211
                                SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
212
                                SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", n->protocol),
213
                                SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
214
                                SD_JSON_BUILD_PAIR_BOOLEAN("Blackhole", n->blackhole),
215
                                JSON_BUILD_PAIR_VARIANT_NON_NULL("Group", group),
216
                                SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
217
        }
218

219
        return sd_json_variant_append_array(array, v);
118✔
220
}
221

222
int nexthops_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
1,097✔
223
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1,097✔
224
        NextHop *nexthop;
1,097✔
225
        int r;
1,097✔
226

227
        assert(manager);
1,097✔
228
        assert(v);
1,097✔
229

230
        HASHMAP_FOREACH(nexthop, manager->nexthops_by_id) {
2,207✔
231
                if (ifindex >= 0) {
1,110✔
232
                        if (nexthop->ifindex != ifindex)
1,105✔
233
                                continue;
992✔
234
                } else {
235
                        /* negative ifindex means we are serializing now. */
236

237
                        if (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN)
5✔
238
                                continue;
×
239
                        if (!nexthop_exists(nexthop))
5✔
240
                                continue;
×
241

242
                        log_nexthop_debug(nexthop, "Serializing", manager);
5✔
243
                }
244

245
                r = nexthop_append_json(nexthop, /* serializing = */ ifindex < 0, &array);
118✔
246
                if (r < 0)
118✔
247
                        return r;
×
248
        }
249

250
        return json_variant_set_field_non_null(v, "NextHops", array);
1,097✔
251
}
252

253
static int route_append_json(Route *route, bool serializing, sd_json_variant **array) {
2,292✔
254
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
2,292✔
255
        int r;
2,292✔
256

257
        assert(route);
2,292✔
258
        assert(array);
2,292✔
259

260
        r = sd_json_buildo(
2,292✔
261
                        &v,
262
                        SD_JSON_BUILD_PAIR_INTEGER("Family", route->family),
263
                        JSON_BUILD_PAIR_IN_ADDR("Destination", &route->dst, route->family),
264
                        SD_JSON_BUILD_PAIR_UNSIGNED("DestinationPrefixLength", route->dst_prefixlen),
265
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Gateway", &route->nexthop.gw, route->nexthop.family),
266
                        SD_JSON_BUILD_PAIR_CONDITION(route->src_prefixlen > 0,
267
                                                     "Source", JSON_BUILD_IN_ADDR(&route->src, route->family)),
268
                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("SourcePrefixLength", route->src_prefixlen),
269
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("PreferredSource", &route->prefsrc, route->family),
270
                        SD_JSON_BUILD_PAIR_UNSIGNED("TOS", route->tos),
271
                        SD_JSON_BUILD_PAIR_UNSIGNED("Scope", route->scope),
272
                        SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", route->protocol),
273
                        SD_JSON_BUILD_PAIR_UNSIGNED("Type", route->type),
274
                        SD_JSON_BUILD_PAIR_UNSIGNED("Priority", route->priority),
275
                        SD_JSON_BUILD_PAIR_UNSIGNED("Table", route->table),
276
                        SD_JSON_BUILD_PAIR_UNSIGNED("Flags", route->flags),
277
                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("NextHopID", route->nexthop_id),
278
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(route->source)),
279
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", &route->provider, route->family));
280
        if (r < 0)
2,292✔
281
                return r;
282

283
        if (serializing) {
2,292✔
284
                r = sd_json_variant_merge_objectbo(
41✔
285
                                &v,
286
                                SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", route->nexthop.ifindex),
287
                                JSON_BUILD_PAIR_BYTE_ARRAY_NON_EMPTY("Metrics", route->metric.metrics, route->metric.n_metrics),
288
                                JSON_BUILD_PAIR_STRING_NON_EMPTY("TCPCongestionControlAlgorithm", route->metric.tcp_congestion_control_algo));
289
                if (r < 0)
41✔
290
                        return r;
291
        } else {
292
                _cleanup_free_ char *scope = NULL, *protocol = NULL, *table = NULL, *flags = NULL, *state = NULL;
2,251✔
293

294
                r = route_scope_to_string_alloc(route->scope, &scope);
2,251✔
295
                if (r < 0)
2,251✔
296
                        return r;
297

298
                r = route_protocol_to_string_alloc(route->protocol, &protocol);
2,251✔
299
                if (r < 0)
2,251✔
300
                        return r;
301

302
                r = manager_get_route_table_to_string(route->manager, route->table, /* append_num = */ false, &table);
2,251✔
303
                if (r < 0)
2,251✔
304
                        return r;
305

306
                r = route_flags_to_string_alloc(route->flags, &flags);
2,251✔
307
                if (r < 0)
2,251✔
308
                        return r;
309

310
                r = network_config_state_to_string_alloc(route->state, &state);
2,251✔
311
                if (r < 0)
2,251✔
312
                        return r;
313

314
                r = sd_json_variant_merge_objectbo(
4,486✔
315
                                &v,
316
                                SD_JSON_BUILD_PAIR_STRING("ScopeString", scope),
317
                                SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
318
                                SD_JSON_BUILD_PAIR_STRING("TypeString", route_type_to_string(route->type)),
319
                                SD_JSON_BUILD_PAIR_STRING("TableString", table),
320
                                JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("MTU", route_metric_get(&route->metric, RTAX_MTU)),
321
                                SD_JSON_BUILD_PAIR_UNSIGNED("Preference", route->pref),
322
                                SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)),
323
                                JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", route->lifetime_usec),
324
                                SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
325
                if (r < 0)
2,251✔
326
                        return r;
327
        }
328

329
        return sd_json_variant_append_array(array, v);
2,292✔
330
}
331

332
int routes_append_json(Manager *manager, int ifindex, sd_json_variant **v) {
1,097✔
333
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1,097✔
334
        Route *route;
1,097✔
335
        int r;
1,097✔
336

337
        assert(manager);
1,097✔
338
        assert(v);
1,097✔
339

340
        SET_FOREACH(route, manager->routes) {
21,862✔
341
                if (ifindex >= 0) {
20,765✔
342
                        if (route->nexthop.ifindex != ifindex)
18,541✔
343
                                continue;
16,290✔
344
                } else {
345
                        /* negative ifindex means we are serializing now. */
346

347
                        if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN)
2,224✔
348
                                continue;
2,183✔
349
                        if (!route_exists(route))
41✔
350
                                continue;
×
351

352
                        log_route_debug(route, "Serializing", manager);
41✔
353
                }
354

355
                r = route_append_json(route, /* serializing = */ ifindex < 0, &array);
2,292✔
356
                if (r < 0)
2,292✔
357
                        return r;
×
358
        }
359

360
        return json_variant_set_field_non_null(v, "Routes", array);
1,097✔
361
}
362

363
static int routing_policy_rule_append_json(RoutingPolicyRule *rule, sd_json_variant **array) {
413✔
364
        _cleanup_free_ char *table = NULL, *protocol = NULL, *state = NULL;
413✔
365
        int r;
413✔
366

367
        assert(rule);
413✔
368
        assert(rule->manager);
413✔
369
        assert(array);
413✔
370

371
        r = manager_get_route_table_to_string(rule->manager, rule->table, /* append_num = */ false, &table);
413✔
372
        if (r < 0 && r != -EINVAL)
413✔
373
                return r;
374

375
        r = route_protocol_to_string_alloc(rule->protocol, &protocol);
413✔
376
        if (r < 0)
413✔
377
                return r;
378

379
        r = network_config_state_to_string_alloc(rule->state, &state);
413✔
380
        if (r < 0)
413✔
381
                return r;
382

383
        return sd_json_variant_append_arraybo(
1,652✔
384
                        array,
385
                        SD_JSON_BUILD_PAIR_INTEGER("Family", rule->family),
386
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("FromPrefix", &rule->from.address, rule->family),
387
                        SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->from.address),
388
                                                     "FromPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->from.prefixlen)),
389
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ToPrefix", &rule->to.address, rule->family),
390
                        SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->to.address),
391
                                                     "ToPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->to.prefixlen)),
392
                        SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", rule->protocol),
393
                        SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol),
394
                        SD_JSON_BUILD_PAIR_UNSIGNED("TOS", rule->tos),
395
                        SD_JSON_BUILD_PAIR_UNSIGNED("Type", rule->action),
396
                        SD_JSON_BUILD_PAIR_STRING("TypeString", fr_act_type_to_string(rule->action)),
397
                        SD_JSON_BUILD_PAIR_UNSIGNED("IPProtocol", rule->ipproto),
398
                        SD_JSON_BUILD_PAIR_STRING("IPProtocolString", ip_protocol_to_name(rule->ipproto)),
399
                        SD_JSON_BUILD_PAIR_UNSIGNED("Priority", rule->priority),
400
                        SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMark", rule->fwmark),
401
                        SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMask", rule->fwmask),
402
                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Table", rule->table),
403
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("TableString", table),
404
                        SD_JSON_BUILD_PAIR_BOOLEAN("Invert", FLAGS_SET(rule->flags, FIB_RULE_INVERT)),
405
                        SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_prefixlen >= 0,
406
                                                     "SuppressPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->suppress_prefixlen)),
407
                        SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_ifgroup >= 0,
408
                                                     "SuppressInterfaceGroup", SD_JSON_BUILD_UNSIGNED(rule->suppress_ifgroup)),
409
                        SD_JSON_BUILD_PAIR_CONDITION(rule->sport.start != 0 || rule->sport.end != 0, "SourcePort",
410
                                                     SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->sport.start), SD_JSON_BUILD_UNSIGNED(rule->sport.end))),
411
                        SD_JSON_BUILD_PAIR_CONDITION(rule->dport.start != 0 || rule->dport.end != 0, "DestinationPort",
412
                                                     SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->dport.start), SD_JSON_BUILD_UNSIGNED(rule->dport.end))),
413
                        SD_JSON_BUILD_PAIR_CONDITION(rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID, "User",
414
                                                     SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->uid_range.start), SD_JSON_BUILD_UNSIGNED(rule->uid_range.end))),
415
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("IncomingInterface", rule->iif),
416
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("OutgoingInterface", rule->oif),
417
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(rule->source)),
418
                        SD_JSON_BUILD_PAIR_STRING("ConfigState", state));
419
}
420

421
static int routing_policy_rules_append_json(Set *rules, sd_json_variant **v) {
78✔
422
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
78✔
423
        RoutingPolicyRule *rule;
78✔
424
        int r;
78✔
425

426
        assert(v);
78✔
427

428
        SET_FOREACH(rule, rules) {
491✔
429
                r = routing_policy_rule_append_json(rule, &array);
413✔
430
                if (r < 0)
413✔
431
                        return r;
×
432
        }
433

434
        return json_variant_set_field_non_null(v, "RoutingPolicyRules", array);
78✔
435
}
436

437
static int network_append_json(Network *network, sd_json_variant **v) {
580✔
438
        assert(v);
580✔
439

440
        if (!network)
580✔
441
                return 0;
442

443
        return sd_json_variant_merge_objectbo(
326✔
444
                        v,
445
                        SD_JSON_BUILD_PAIR_STRING("NetworkFile", network->filename),
446
                        SD_JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins),
447
                        SD_JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online > 0),
448
                        SD_JSON_BUILD_PAIR_CONDITION(
449
                                        operational_state_range_is_valid(&network->required_operstate_for_online),
450
                                        "RequiredOperationalStateForOnline",
451
                                        SD_JSON_BUILD_ARRAY(
452
                                                SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.min)),
453
                                                SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.max)))),
454
                        SD_JSON_BUILD_PAIR_STRING("RequiredFamilyForOnline", link_required_address_family_to_string(network->required_family_for_online)),
455
                        SD_JSON_BUILD_PAIR_STRING("ActivationPolicy", activation_policy_to_string(network->activation_policy)));
456
}
457

458
static int netdev_append_json(NetDev *netdev, sd_json_variant **v) {
580✔
459
        assert(v);
580✔
460

461
        if (!netdev)
580✔
462
                return 0;
463

464
        return sd_json_variant_merge_objectbo(
165✔
465
                        v,
466
                        SD_JSON_BUILD_PAIR_STRING("NetDevFile", netdev->filename),
467
                        SD_JSON_BUILD_PAIR_STRV("NetDevFileDropins", netdev->dropins));
468
}
469

470
static int device_append_json(sd_device *device, sd_json_variant **v) {
580✔
471
        _cleanup_strv_free_ char **link_dropins = NULL;
580✔
472
        const char *link = NULL, *path = NULL, *vendor = NULL, *model = NULL, *joined;
580✔
473
        int r;
580✔
474

475
        assert(v);
580✔
476

477
        if (!device)
580✔
478
                return 0;
479

480
        (void) sd_device_get_property_value(device, "ID_NET_LINK_FILE", &link);
580✔
481

482
        if (sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) {
580✔
483
                 r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE);
×
484
                 if (r < 0)
×
485
                        return r;
486
        }
487

488
        (void) sd_device_get_property_value(device, "ID_PATH", &path);
580✔
489

490
        (void) device_get_vendor_string(device, &vendor);
580✔
491
        (void) device_get_model_string(device, &model);
580✔
492

493
        return sd_json_variant_merge_objectbo(
580✔
494
                        v,
495
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("LinkFile", link),
496
                        JSON_BUILD_PAIR_STRV_NON_EMPTY("LinkFileDropins", link_dropins),
497
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", path),
498
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("Vendor", vendor),
499
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("Model", model));
500
}
501

502
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) {
65✔
503
        assert(link);
65✔
504
        assert(a);
65✔
505
        assert(array);
65✔
506

507
        if (a->ifindex != 0 && a->ifindex != link->ifindex)
65✔
508
                return 0;
509

510
        return sd_json_variant_append_arraybo(
65✔
511
                        array,
512
                        SD_JSON_BUILD_PAIR_INTEGER("Family", a->family),
513
                        JSON_BUILD_PAIR_IN_ADDR("Address", &a->address, a->family),
514
                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port),
515
                        SD_JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", SD_JSON_BUILD_INTEGER(a->ifindex)),
516
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name),
517
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
518
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, a->family));
519
}
520

521
static int dns_append_json(Link *link, sd_json_variant **v) {
580✔
522
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
523
        int r;
580✔
524

525
        assert(link);
580✔
526
        assert(v);
580✔
527

528
        if (!link->network)
580✔
529
                return 0;
530

531
        if (link->n_dns != UINT_MAX)
163✔
532
                for (unsigned i = 0; i < link->n_dns; i++) {
8✔
533
                        r = dns_append_json_one(link, link->dns[i], NETWORK_CONFIG_SOURCE_RUNTIME, NULL, &array);
6✔
534
                        if (r < 0)
6✔
535
                                return r;
536
                }
537
        else {
538
                for (unsigned i = 0; i < link->network->n_dns; i++) {
169✔
539
                        r = dns_append_json_one(link, link->network->dns[i], NETWORK_CONFIG_SOURCE_STATIC, NULL, &array);
8✔
540
                        if (r < 0)
8✔
541
                                return r;
542
                }
543

544
                if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
161✔
545
                        const struct in_addr *dns;
36✔
546
                        union in_addr_union s;
36✔
547
                        int n_dns;
36✔
548

549
                        r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
36✔
550
                        if (r < 0)
36✔
551
                                return r;
×
552

553
                        n_dns = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
36✔
554
                        for (int i = 0; i < n_dns; i++) {
77✔
555
                                r = dns_append_json_one(link,
82✔
556
                                                        &(struct in_addr_full) { .family = AF_INET, .address.in = dns[i], },
41✔
557
                                                        NETWORK_CONFIG_SOURCE_DHCP4,
558
                                                        &s,
559
                                                        &array);
560
                                if (r < 0)
41✔
561
                                        return r;
×
562
                        }
563
                }
564

565
                if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
161✔
566
                        const struct in6_addr *dns;
22✔
567
                        union in_addr_union s;
22✔
568
                        int n_dns;
22✔
569

570
                        r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
22✔
571
                        if (r < 0)
22✔
572
                                return r;
×
573

574
                        n_dns = sd_dhcp6_lease_get_dns(link->dhcp6_lease, &dns);
22✔
575
                        for (int i = 0; i < n_dns; i++) {
30✔
576
                                r = dns_append_json_one(link,
16✔
577
                                                        &(struct in_addr_full) { .family = AF_INET6, .address.in6 = dns[i], },
8✔
578
                                                        NETWORK_CONFIG_SOURCE_DHCP6,
579
                                                        &s,
580
                                                        &array);
581
                                if (r < 0)
8✔
582
                                        return r;
×
583
                        }
584
                }
585

586
                if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
161✔
587
                        NDiscRDNSS *a;
150✔
588

589
                        SET_FOREACH(a, link->ndisc_rdnss) {
152✔
590
                                r = dns_append_json_one(link,
4✔
591
                                                        &(struct in_addr_full) { .family = AF_INET6, .address.in6 = a->address, },
2✔
592
                                                        NETWORK_CONFIG_SOURCE_NDISC,
593
                                                        &(union in_addr_union) { .in6 = a->router },
2✔
594
                                                        &array);
595
                                if (r < 0)
2✔
596
                                        return r;
×
597
                        }
598
                }
599
        }
600

601
        return json_variant_set_field_non_null(v, "DNS", array);
163✔
602
}
603

604
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) {
6✔
605
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *addrs_array = NULL;
6✔
606
        _cleanup_strv_free_ char **transports = NULL;
6✔
607
        int r;
6✔
608

609
        assert(link);
6✔
610
        assert(res);
6✔
611
        assert(array);
6✔
612

613
        FOREACH_ARRAY(addr, res->addrs, res->n_addrs) {
16✔
614
                r = sd_json_variant_append_arrayb(
30✔
615
                                &addrs_array,
616
                                JSON_BUILD_IN_ADDR(addr, res->family));
10✔
617
                if (r < 0)
10✔
618
                        return r;
619
        }
620

621
        r = dns_resolver_transports_to_strv(res->transports, &transports);
6✔
622
        if (r < 0)
6✔
623
                return r;
624

625
        //FIXME ifindex?
626
        return sd_json_variant_append_arrayb(
12✔
627
                        array,
628
                        SD_JSON_BUILD_OBJECT(
6✔
629
                                        SD_JSON_BUILD_PAIR_INTEGER("Family", res->family),
630
                                        SD_JSON_BUILD_PAIR_INTEGER("Priority", res->priority),
631
                                        JSON_BUILD_PAIR_VARIANT_NON_NULL("Addresses", addrs_array),
632
                                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", res->port),
633
                                        JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", res->auth_name),
634
                                        JSON_BUILD_PAIR_STRING_NON_EMPTY("DoHPath", res->dohpath),
635
                                        JSON_BUILD_PAIR_STRV_NON_EMPTY("Transports", transports),
636
                                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
637
                                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, res->family)));
638
}
639

640
static int dnr_append_json(Link *link, sd_json_variant **v) {
580✔
641
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
642
        int r;
580✔
643

644
        assert(link);
580✔
645
        assert(v);
580✔
646

647
        if (!link->network)
580✔
648
                return 0;
649

650
        if (link->dhcp_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
163✔
651
                struct sd_dns_resolver *dnr;
36✔
652
                union in_addr_union s;
36✔
653
                int n_dnr;
36✔
654

655
                r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
36✔
656
                if (r < 0)
36✔
657
                        return r;
×
658

659
                n_dnr = sd_dhcp_lease_get_dnr(link->dhcp_lease, &dnr);
36✔
660
                if (n_dnr > 0)
36✔
661
                        FOREACH_ARRAY(res, dnr, n_dnr) {
6✔
662
                                r = dnr_append_json_one(link,
4✔
663
                                                        res,
664
                                                        NETWORK_CONFIG_SOURCE_DHCP4,
665
                                                        &s,
666
                                                        &array);
667
                                if (r < 0)
4✔
668
                                        return r;
669
                        }
670
        }
671

672
        if (link->dhcp6_lease && link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
163✔
673
                struct sd_dns_resolver *dnr;
22✔
674
                union in_addr_union s;
22✔
675
                int n_dnr;
22✔
676

677
                r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
22✔
678
                if (r < 0)
22✔
679
                        return r;
×
680

681
                n_dnr = sd_dhcp6_lease_get_dnr(link->dhcp6_lease, &dnr);
22✔
682
                if (n_dnr > 0)
22✔
683
                        FOREACH_ARRAY(res, dnr, n_dnr) {
4✔
684
                                r = dnr_append_json_one(link,
2✔
685
                                                        res,
686
                                                        NETWORK_CONFIG_SOURCE_DHCP6,
687
                                                        &s,
688
                                                        &array);
689
                                if (r < 0)
2✔
690
                                        return r;
691
                        }
692
        }
693

694
        if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_NDISC)) {
163✔
695
                NDiscDNR *a;
152✔
696

697
                SET_FOREACH(a, link->ndisc_dnr) {
152✔
698
                        r = dnr_append_json_one(link,
×
699
                                                &a->resolver,
×
700
                                                NETWORK_CONFIG_SOURCE_NDISC,
701
                                                &(union in_addr_union) { .in6 = a->router },
×
702
                                                &array);
703
                        if (r < 0)
×
704
                                return r;
×
705
                }
706
        }
707

708
        return json_variant_set_field_non_null(v, "DNR", array);
163✔
709
}
710

711
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) {
45✔
712
        assert(IN_SET(family, AF_INET, AF_INET6));
45✔
713
        assert(a);
45✔
714
        assert(array);
45✔
715

716
        return sd_json_variant_append_arraybo(
45✔
717
                        array,
718
                        SD_JSON_BUILD_PAIR_INTEGER("Family", family),
719
                        JSON_BUILD_PAIR_IN_ADDR("Address", a, family),
720
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
721
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family));
722
}
723

724
static int server_append_json_one_fqdn(int family, const char *fqdn, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
8✔
725
        assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
8✔
726
        assert(fqdn);
8✔
727
        assert(array);
8✔
728

729
        return sd_json_variant_append_arraybo(
8✔
730
                        array,
731
                        SD_JSON_BUILD_PAIR_STRING("Server", fqdn),
732
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
733
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family));
734
}
735

736
static int server_append_json_one_string(const char *str, NetworkConfigSource s, sd_json_variant **array) {
8✔
737
        union in_addr_union a;
8✔
738
        int family;
8✔
739

740
        assert(str);
8✔
741

742
        if (in_addr_from_string_auto(str, &family, &a) >= 0)
8✔
743
                return server_append_json_one_addr(family, &a, s, NULL, array);
×
744

745
        return server_append_json_one_fqdn(AF_UNSPEC, str, s, NULL, array);
8✔
746
}
747

748
static int ntp_append_json(Link *link, sd_json_variant **v) {
580✔
749
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
750
        int r;
580✔
751

752
        assert(link);
580✔
753
        assert(v);
580✔
754

755
        if (!link->network)
580✔
756
                return 0;
757

758
        STRV_FOREACH(p, link->ntp ?: link->network->ntp) {
334✔
759
                r = server_append_json_one_string(*p, NETWORK_CONFIG_SOURCE_RUNTIME, &array);
8✔
760
                if (r < 0)
8✔
761
                        return r;
762
        }
763

764
        if (!link->ntp) {
163✔
765
                if (link->dhcp_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
162✔
766
                        const struct in_addr *ntp;
40✔
767
                        union in_addr_union s;
40✔
768
                        int n_ntp;
40✔
769

770
                        r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
40✔
771
                        if (r < 0)
40✔
772
                                return r;
×
773

774
                        n_ntp = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
40✔
775
                        for (int i = 0; i < n_ntp; i++) {
74✔
776
                                r = server_append_json_one_addr(AF_INET,
68✔
777
                                                                &(union in_addr_union) { .in = ntp[i], },
34✔
778
                                                                NETWORK_CONFIG_SOURCE_DHCP4,
779
                                                                &s,
780
                                                                &array);
781
                                if (r < 0)
34✔
782
                                        return r;
×
783
                        }
784
                }
785

786
                if (link->dhcp6_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
162✔
787
                        const struct in6_addr *ntp_addr;
26✔
788
                        union in_addr_union s;
26✔
789
                        char **ntp_fqdn;
26✔
790
                        int n_ntp;
26✔
791

792
                        r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
26✔
793
                        if (r < 0)
26✔
794
                                return r;
×
795

796
                        n_ntp = sd_dhcp6_lease_get_ntp_addrs(link->dhcp6_lease, &ntp_addr);
26✔
797
                        for (int i = 0; i < n_ntp; i++) {
29✔
798
                                r = server_append_json_one_addr(AF_INET6,
6✔
799
                                                                &(union in_addr_union) { .in6 = ntp_addr[i], },
3✔
800
                                                                NETWORK_CONFIG_SOURCE_DHCP6,
801
                                                                &s,
802
                                                                &array);
803
                                if (r < 0)
3✔
804
                                        return r;
×
805
                        }
806

807
                        n_ntp = sd_dhcp6_lease_get_ntp_fqdn(link->dhcp6_lease, &ntp_fqdn);
26✔
808
                        for (int i = 0; i < n_ntp; i++) {
26✔
809
                                r = server_append_json_one_fqdn(AF_INET6,
×
810
                                                                ntp_fqdn[i],
×
811
                                                                NETWORK_CONFIG_SOURCE_DHCP6,
812
                                                                &s,
813
                                                                &array);
814
                                if (r < 0)
×
815
                                        return r;
816
                        }
817
                }
818
        }
819

820
        return json_variant_set_field_non_null(v, "NTP", array);
163✔
821
}
822

823
static int domain_append_json(int family, const char *domain, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) {
16✔
824
        assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6));
16✔
825
        assert(domain);
16✔
826
        assert(array);
16✔
827

828
        return sd_json_variant_append_arraybo(
16✔
829
                        array,
830
                        SD_JSON_BUILD_PAIR_STRING("Domain", domain),
831
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
832
                        JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family));
833
}
834

835
static int sip_append_json(Link *link, sd_json_variant **v) {
580✔
836
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
837
        int r;
580✔
838

839
        assert(link);
580✔
840
        assert(v);
580✔
841

842
        if (!link->network)
580✔
843
                return 0;
844

845
        if (link->dhcp_lease && link->network->dhcp_use_sip) {
163✔
846
                const struct in_addr *sip;
38✔
847
                union in_addr_union s;
38✔
848
                int n_sip;
38✔
849

850
                r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
38✔
851
                if (r < 0)
38✔
852
                        return r;
×
853

854
                n_sip = sd_dhcp_lease_get_sip(link->dhcp_lease, &sip);
38✔
855
                for (int i = 0; i < n_sip; i++) {
44✔
856
                        r = server_append_json_one_addr(AF_INET,
12✔
857
                                                        &(union in_addr_union) { .in = sip[i], },
6✔
858
                                                        NETWORK_CONFIG_SOURCE_DHCP4,
859
                                                        &s,
860
                                                        &array);
861
                        if (r < 0)
6✔
862
                                return r;
×
863
                }
864

865
        }
866

867
        if (link->dhcp6_lease && link->network->dhcp6_use_sip) {
163✔
868
                const struct in6_addr *sip_addr;
24✔
869
                union in_addr_union s;
24✔
870
                char **domains;
24✔
871
                int n_sip;
24✔
872

873
                r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
24✔
874
                if (r < 0)
24✔
875
                        return r;
×
876

877
                n_sip = sd_dhcp6_lease_get_sip_addrs(link->dhcp6_lease, &sip_addr);
24✔
878
                for (int i = 0; i < n_sip; i++) {
26✔
879
                        r = server_append_json_one_addr(AF_INET6,
4✔
880
                                                        &(union in_addr_union) { .in6 = sip_addr[i], },
2✔
881
                                                        NETWORK_CONFIG_SOURCE_DHCP6,
882
                                                        &s,
883
                                                        &array);
884
                        if (r < 0)
2✔
885
                                return r;
×
886
                }
887

888
                if (sd_dhcp6_lease_get_sip_domains(link->dhcp6_lease, &domains) >= 0)
24✔
889
                        STRV_FOREACH(p, domains) {
4✔
890
                                r = domain_append_json(AF_INET6,
2✔
891
                                                       *p,
892
                                                       NETWORK_CONFIG_SOURCE_DHCP6,
893
                                                       &s,
894
                                                       &array);
895
                                if (r < 0)
2✔
896
                                        return r;
897
                        }
898

899
        }
900

901
        return json_variant_set_field_non_null(v, "SIP", array);
163✔
902
}
903

904
static int domains_append_json(Link *link, bool is_route, sd_json_variant **v) {
1,160✔
905
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
1,160✔
906
        OrderedSet *link_domains, *network_domains;
1,160✔
907
        UseDomains use_domains;
1,160✔
908
        union in_addr_union s;
1,160✔
909
        char **domains;
1,160✔
910
        const char *domain;
1,160✔
911
        int r;
1,160✔
912

913
        assert(link);
1,160✔
914
        assert(v);
1,160✔
915

916
        if (!link->network)
1,160✔
917
                return 0;
918

919
        link_domains = is_route ? link->route_domains : link->search_domains;
326✔
920
        network_domains = is_route ? link->network->route_domains : link->network->search_domains;
326✔
921
        use_domains = is_route ? USE_DOMAINS_ROUTE : USE_DOMAINS_YES;
326✔
922

923
        ORDERED_SET_FOREACH(domain, link_domains ?: network_domains) {
672✔
924
                r = domain_append_json(AF_UNSPEC, domain,
16✔
925
                                       link_domains ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC,
926
                                       NULL, &array);
927
                if (r < 0)
10✔
928
                        return r;
×
929
        }
930

931
        if (!link_domains) {
326✔
932
                if (link->dhcp_lease &&
402✔
933
                    link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) == use_domains) {
80✔
934
                        r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &s.in);
2✔
935
                        if (r < 0)
2✔
936
                                return r;
937

938
                        if (sd_dhcp_lease_get_domainname(link->dhcp_lease, &domain) >= 0) {
2✔
939
                                r = domain_append_json(AF_INET, domain, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
×
940
                                if (r < 0)
×
941
                                        return r;
942
                        }
943

944
                        if (sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains) >= 0)
2✔
945
                                STRV_FOREACH(p, domains) {
4✔
946
                                        r = domain_append_json(AF_INET, *p, NETWORK_CONFIG_SOURCE_DHCP4, &s, &array);
2✔
947
                                        if (r < 0)
2✔
948
                                                return r;
949
                                }
950
                }
951

952
                if (link->dhcp6_lease &&
374✔
953
                    link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP6) == use_domains) {
52✔
954
                        r = sd_dhcp6_lease_get_server_address(link->dhcp6_lease, &s.in6);
×
955
                        if (r < 0)
×
956
                                return r;
957

958
                        if (sd_dhcp6_lease_get_domains(link->dhcp6_lease, &domains) >= 0)
×
959
                                STRV_FOREACH(p, domains) {
×
960
                                        r = domain_append_json(AF_INET6, *p, NETWORK_CONFIG_SOURCE_DHCP6, &s, &array);
×
961
                                        if (r < 0)
×
962
                                                return r;
963
                                }
964
                }
965

966
                if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_NDISC) == use_domains) {
322✔
967
                        NDiscDNSSL *a;
2✔
968

969
                        SET_FOREACH(a, link->ndisc_dnssl) {
4✔
970
                                r = domain_append_json(AF_INET6, ndisc_dnssl_domain(a), NETWORK_CONFIG_SOURCE_NDISC,
2✔
971
                                                       &(union in_addr_union) { .in6 = a->router },
2✔
972
                                                       &array);
973
                                if (r < 0)
2✔
974
                                        return r;
×
975
                        }
976
                }
977
        }
978

979
        return json_variant_set_field_non_null(v, is_route ? "RouteDomains" : "SearchDomains", array);
489✔
980
}
981

982
static int nta_append_json(const char *nta, NetworkConfigSource s, sd_json_variant **array) {
×
983
        assert(nta);
×
984
        assert(array);
×
985

986
        return sd_json_variant_append_arraybo(
×
987
                        array,
988
                        SD_JSON_BUILD_PAIR_STRING("DNSSECNegativeTrustAnchor", nta),
989
                        SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)));
990
}
991

992
static int ntas_append_json(Link *link, sd_json_variant **v) {
580✔
993
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
994
        const char *nta;
580✔
995
        int r;
580✔
996

997
        assert(link);
580✔
998
        assert(v);
580✔
999

1000
        if (!link->network)
580✔
1001
                return 0;
1002

1003
        SET_FOREACH(nta, link->dnssec_negative_trust_anchors ?: link->network->dnssec_negative_trust_anchors) {
326✔
1004
                r = nta_append_json(nta,
×
1005
                                   link->dnssec_negative_trust_anchors ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC,
×
1006
                                   &array);
1007
                if (r < 0)
×
1008
                        return r;
×
1009
        }
1010

1011
        return json_variant_set_field_non_null(v, "DNSSECNegativeTrustAnchors", array);
163✔
1012
}
1013

1014
static int dns_misc_append_json(Link *link, sd_json_variant **v) {
580✔
1015
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
580✔
1016
        ResolveSupport resolve_support;
580✔
1017
        NetworkConfigSource source;
580✔
1018
        DnsOverTlsMode mode;
580✔
1019
        int t, r;
580✔
1020

1021
        assert(link);
580✔
1022
        assert(v);
580✔
1023

1024
        if (!link->network)
580✔
1025
                return 0;
1026

1027
        resolve_support = link->llmnr >= 0 ? link->llmnr : link->network->llmnr;
163✔
1028
        if (resolve_support >= 0) {
163✔
1029
                source = link->llmnr >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
163✔
1030

1031
                r = sd_json_variant_append_arraybo(
163✔
1032
                                &array,
1033
                                SD_JSON_BUILD_PAIR_STRING("LLMNR", resolve_support_to_string(resolve_support)),
1034
                                SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1035
                if (r < 0)
163✔
1036
                        return r;
1037
        }
1038

1039
        resolve_support = link->mdns >= 0 ? link->mdns : link->network->mdns;
163✔
1040
        if (resolve_support >= 0) {
163✔
1041
                source = link->mdns >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
163✔
1042

1043
                r = sd_json_variant_append_arraybo(
163✔
1044
                                &array,
1045
                                SD_JSON_BUILD_PAIR_STRING("MDNS", resolve_support_to_string(resolve_support)),
1046
                                SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1047
                if (r < 0)
163✔
1048
                        return r;
1049
        }
1050

1051
        t = link->dns_default_route >= 0 ? link->dns_default_route : link->network->dns_default_route;
163✔
1052
        if (t >= 0) {
163✔
1053
                source = link->dns_default_route >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
×
1054

1055
                r = sd_json_variant_append_arraybo(
×
1056
                                &array,
1057
                                SD_JSON_BUILD_PAIR_BOOLEAN("DNSDefaultRoute", t),
1058
                                SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1059
                if (r < 0)
×
1060
                        return r;
1061
        }
1062

1063
        mode = link->dns_over_tls_mode >= 0 ? link->dns_over_tls_mode : link->network->dns_over_tls_mode;
163✔
1064
        if (mode >= 0) {
163✔
1065
                source = link->dns_over_tls_mode >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC;
×
1066

1067
                r = sd_json_variant_append_arraybo(
×
1068
                                &array,
1069
                                SD_JSON_BUILD_PAIR_STRING("DNSOverTLS", dns_over_tls_mode_to_string(mode)),
1070
                                SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)));
1071
                if (r < 0)
×
1072
                        return r;
1073
        }
1074

1075
        return json_variant_set_field_non_null(v, "DNSSettings", array);
163✔
1076
}
1077

1078
static int captive_portal_append_json(Link *link, sd_json_variant **v) {
580✔
1079
        const char *captive_portal;
580✔
1080
        int r;
580✔
1081

1082
        assert(link);
580✔
1083
        assert(v);
580✔
1084

1085
        r = link_get_captive_portal(link, &captive_portal);
580✔
1086
        if (r <= 0)
580✔
1087
                return r;
580✔
1088

1089
        return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal));
3✔
1090
}
1091

1092
static int pref64_append_json(Link *link, sd_json_variant **v) {
580✔
1093
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *w = NULL;
582✔
1094
        NDiscPREF64 *i;
580✔
1095
        int r;
580✔
1096

1097
        assert(link);
580✔
1098
        assert(v);
580✔
1099

1100
        if (!link->network || !link->network->ndisc_use_pref64)
580✔
1101
                return 0;
1102

1103
        SET_FOREACH(i, link->ndisc_pref64) {
4✔
1104
                r = sd_json_variant_append_arraybo(
2✔
1105
                                &array,
1106
                                JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("Prefix", &i->prefix),
1107
                                SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", i->prefix_len),
1108
                                JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", i->lifetime_usec),
1109
                                JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("ConfigProvider", &i->router));
1110
                if (r < 0)
2✔
1111
                        return r;
×
1112
        }
1113

1114
        r = json_variant_set_field_non_null(&w, "PREF64", array);
2✔
1115
        if (r < 0)
2✔
1116
                return r;
1117

1118
        return json_variant_set_field_non_null(v, "NDisc", w);
2✔
1119
}
1120

1121
static int dhcp_server_append_json(Link *link, sd_json_variant **v) {
580✔
1122
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
580✔
1123
        int r;
580✔
1124

1125
        assert(link);
580✔
1126
        assert(v);
580✔
1127

1128
        if (!link->dhcp_server)
580✔
1129
                return 0;
1130

1131
        r = sd_json_buildo(
1✔
1132
                        &w,
1133
                        SD_JSON_BUILD_PAIR_UNSIGNED("PoolOffset", link->dhcp_server->pool_offset),
1134
                        SD_JSON_BUILD_PAIR_UNSIGNED("PoolSize", link->dhcp_server->pool_size));
1135
        if (r < 0)
1✔
1136
                return r;
1137

1138
        r = dhcp_server_bound_leases_append_json(link->dhcp_server, &w);
1✔
1139
        if (r < 0)
1✔
1140
                return r;
1141

1142
        r = dhcp_server_static_leases_append_json(link->dhcp_server, &w);
1✔
1143
        if (r < 0)
1✔
1144
                return r;
1145

1146
        return json_variant_set_field_non_null(v, "DHCPServer", w);
1✔
1147
}
1148

1149
static int dhcp6_client_vendor_options_append_json(Link *link, sd_json_variant **v) {
29✔
1150
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
29✔
1151
        sd_dhcp6_option **options = NULL;
29✔
1152
        int r, n_vendor_options;
29✔
1153

1154
        assert(link);
29✔
1155
        assert(v);
29✔
1156

1157
        if (!link->dhcp6_lease)
29✔
1158
                return 0;
1159

1160
        n_vendor_options = sd_dhcp6_lease_get_vendor_options(link->dhcp6_lease, &options);
26✔
1161

1162
        FOREACH_ARRAY(option, options, n_vendor_options) {
26✔
1163
                r = sd_json_variant_append_arraybo(
×
1164
                                &array,
1165
                                SD_JSON_BUILD_PAIR_UNSIGNED("EnterpriseId", (*option)->enterprise_identifier),
1166
                                SD_JSON_BUILD_PAIR_UNSIGNED("SubOptionCode", (*option)->option),
1167
                                SD_JSON_BUILD_PAIR_HEX("SubOptionData", (*option)->data, (*option)->length));
1168
                if (r < 0)
×
1169
                        return r;
1170
        }
1171

1172
        return json_variant_set_field_non_null(v, "VendorSpecificOptions", array);
26✔
1173
}
1174

1175
static int dhcp6_client_lease_append_json(Link *link, sd_json_variant **v) {
29✔
1176
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
29✔
1177
        usec_t ts = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
29✔
1178
        int r;
29✔
1179

1180
        assert(link);
29✔
1181
        assert(v);
29✔
1182

1183
        if (!link->dhcp6_lease)
29✔
1184
                return 0;
1185

1186
        r = sd_dhcp6_lease_get_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &ts);
26✔
1187
        if (r < 0 && r != -ENODATA)
26✔
1188
                return r;
1189

1190
        r = sd_dhcp6_lease_get_t1_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t1);
26✔
1191
        if (r < 0 && r != -ENODATA)
26✔
1192
                return r;
1193

1194
        r = sd_dhcp6_lease_get_t2_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t2);
26✔
1195
        if (r < 0 && r != -ENODATA)
26✔
1196
                return r;
1197

1198
        r = sd_json_buildo(
26✔
1199
                        &w,
1200
                        JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1),
1201
                        JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2),
1202
                        JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", ts));
1203
        if (r < 0)
26✔
1204
                return r;
1205

1206
        return json_variant_set_field_non_null(v, "Lease", w);
26✔
1207
}
1208

1209
static int dhcp6_client_pd_append_json(Link *link, sd_json_variant **v) {
29✔
1210
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
29✔
1211
        int r;
29✔
1212

1213
        assert(link);
29✔
1214
        assert(link->network);
29✔
1215
        assert(v);
29✔
1216

1217
        if (!link->network->dhcp6_use_pd_prefix ||
58✔
1218
            !sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease))
29✔
1219
                return 0;
26✔
1220

1221
        FOREACH_DHCP6_PD_PREFIX(link->dhcp6_lease) {
6✔
1222
                usec_t lifetime_preferred_usec, lifetime_valid_usec;
3✔
1223
                struct in6_addr prefix;
3✔
1224
                uint8_t prefix_len;
3✔
1225

1226
                r = sd_dhcp6_lease_get_pd_prefix(link->dhcp6_lease, &prefix, &prefix_len);
3✔
1227
                if (r < 0)
3✔
1228
                        return r;
×
1229

1230
                r = sd_dhcp6_lease_get_pd_lifetime_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME,
3✔
1231
                                                             &lifetime_preferred_usec, &lifetime_valid_usec);
1232
                if (r < 0)
3✔
1233
                        return r;
1234

1235
                r = sd_json_variant_append_arraybo(
3✔
1236
                                &array,
1237
                                JSON_BUILD_PAIR_IN6_ADDR("Prefix", &prefix),
1238
                                SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", prefix_len),
1239
                                JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", lifetime_preferred_usec),
1240
                                JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", lifetime_valid_usec));
1241
                if (r < 0)
3✔
1242
                        return r;
1243
        }
1244

1245
        return json_variant_set_field_non_null(v, "Prefixes", array);
3✔
1246
}
1247

1248
static int dhcp6_client_duid_append_json(Link *link, sd_json_variant **v) {
29✔
1249
        const sd_dhcp_duid *duid;
29✔
1250
        const void *data;
29✔
1251
        size_t data_size;
29✔
1252
        int r;
29✔
1253

1254
        assert(link);
29✔
1255
        assert(v);
29✔
1256

1257
        if (!link->dhcp6_client)
29✔
1258
                return 0;
29✔
1259

1260
        r = sd_dhcp6_client_get_duid(link->dhcp6_client, &duid);
29✔
1261
        if (r < 0)
29✔
1262
                return 0;
1263

1264
        r = sd_dhcp_duid_get_raw(duid, &data, &data_size);
29✔
1265
        if (r < 0)
29✔
1266
                return 0;
1267

1268
        return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_BYTE_ARRAY("DUID", data, data_size));
29✔
1269
}
1270

1271
static int dhcp6_client_append_json(Link *link, sd_json_variant **v) {
580✔
1272
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
580✔
1273
        int r;
580✔
1274

1275
        assert(link);
580✔
1276
        assert(v);
580✔
1277

1278
        if (!link->dhcp6_client)
580✔
1279
                return 0;
1280

1281
        r = dhcp6_client_lease_append_json(link, &w);
29✔
1282
        if (r < 0)
29✔
1283
                return r;
1284

1285
        r = dhcp6_client_pd_append_json(link, &w);
29✔
1286
        if (r < 0)
29✔
1287
                return r;
1288

1289
        r = dhcp6_client_vendor_options_append_json(link, &w);
29✔
1290
        if (r < 0)
29✔
1291
                return r;
1292

1293
        r = dhcp6_client_duid_append_json(link, &w);
29✔
1294
        if (r < 0)
29✔
1295
                return r;
1296

1297
        return json_variant_set_field_non_null(v, "DHCPv6Client", w);
29✔
1298
}
1299

1300
static int dhcp_client_lease_append_json(Link *link, sd_json_variant **v) {
43✔
1301
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
43✔
1302
        usec_t lease_timestamp_usec = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
43✔
1303
        int r;
43✔
1304

1305
        assert(link);
43✔
1306
        assert(v);
43✔
1307

1308
        if (!link->dhcp_client || !link->dhcp_lease)
43✔
1309
                return 0;
1310

1311
        r = sd_dhcp_lease_get_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lease_timestamp_usec);
40✔
1312
        if (r < 0 && r != -ENODATA)
40✔
1313
                return r;
1314

1315
        r = sd_dhcp_lease_get_t1_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t1);
40✔
1316
        if (r < 0 && r != -ENODATA)
40✔
1317
                return r;
1318

1319
        r = sd_dhcp_lease_get_t2_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t2);
40✔
1320
        if (r < 0 && r != -ENODATA)
40✔
1321
                return r;
1322

1323
        r = sd_json_buildo(&w,
40✔
1324
                           JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec),
1325
                           JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1),
1326
                           JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2));
1327
        if (r < 0)
40✔
1328
                return r;
1329

1330
        return json_variant_set_field_non_null(v, "Lease", w);
40✔
1331
}
1332

1333
static int dhcp_client_pd_append_json(Link *link, sd_json_variant **v) {
43✔
1334
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *addresses = NULL, *array = NULL;
44✔
1335
        uint8_t ipv4masklen, sixrd_prefixlen;
43✔
1336
        struct in6_addr sixrd_prefix;
43✔
1337
        const struct in_addr *br_addresses;
43✔
1338
        size_t n_br_addresses = 0;
43✔
1339
        int r;
43✔
1340

1341
        assert(link);
43✔
1342
        assert(link->network);
43✔
1343
        assert(v);
43✔
1344

1345
        if (!link->network->dhcp_use_6rd || !sd_dhcp_lease_has_6rd(link->dhcp_lease))
43✔
1346
                return 0;
42✔
1347

1348
        r = sd_dhcp_lease_get_6rd(link->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, &br_addresses, &n_br_addresses);
1✔
1349
        if (r < 0)
1✔
1350
                return r;
1351

1352
        FOREACH_ARRAY(br_address, br_addresses, n_br_addresses) {
2✔
1353
                r = sd_json_variant_append_arrayb(&addresses, JSON_BUILD_IN4_ADDR(br_address));
1✔
1354
                if (r < 0)
1✔
1355
                        return r;
1356
        }
1357

1358
        r = sd_json_buildo(
1✔
1359
                        &array,
1360
                        JSON_BUILD_PAIR_IN6_ADDR("Prefix", &sixrd_prefix),
1361
                        SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", sixrd_prefixlen),
1362
                        SD_JSON_BUILD_PAIR_UNSIGNED("IPv4MaskLength", ipv4masklen),
1363
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("BorderRouters", addresses));
1364
        if (r < 0)
1✔
1365
                return r;
1366

1367
        return json_variant_set_field_non_null(v, "6rdPrefix", array);
1✔
1368
}
1369

1370
static int dhcp_client_private_options_append_json(Link *link, sd_json_variant **v) {
43✔
1371
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
43✔
1372
        int r;
43✔
1373

1374
        assert(link);
43✔
1375
        assert(v);
43✔
1376

1377
        if (!link->dhcp_lease)
43✔
1378
                return 0;
1379

1380
        LIST_FOREACH(options, option, link->dhcp_lease->private_options) {
40✔
1381

1382
                r = sd_json_variant_append_arraybo(
×
1383
                                &array,
1384
                                SD_JSON_BUILD_PAIR_UNSIGNED("Option", option->tag),
1385
                                SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", option->data, option->length));
1386
                if (r < 0)
×
1387
                        return 0;
1388
        }
1389
        return json_variant_set_field_non_null(v, "PrivateOptions", array);
40✔
1390
}
1391

1392
static int dhcp_client_id_append_json(Link *link, sd_json_variant **v) {
43✔
1393
        const sd_dhcp_client_id *client_id;
43✔
1394
        const void *data;
43✔
1395
        size_t l;
43✔
1396
        int r;
43✔
1397

1398
        assert(link);
43✔
1399
        assert(v);
43✔
1400

1401
        if (!link->dhcp_client)
43✔
1402
                return 0;
43✔
1403

1404
        r = sd_dhcp_client_get_client_id(link->dhcp_client, &client_id);
43✔
1405
        if (r < 0)
43✔
1406
                return 0;
1407

1408
        r = sd_dhcp_client_id_get_raw(client_id, &data, &l);
43✔
1409
        if (r < 0)
43✔
1410
                return 0;
1411

1412
        return sd_json_variant_merge_objectbo(v, SD_JSON_BUILD_PAIR_BYTE_ARRAY("ClientIdentifier", data, l));
43✔
1413
}
1414

1415
static int dhcp_client_append_json(Link *link, sd_json_variant **v) {
580✔
1416
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
580✔
1417
        int r;
580✔
1418

1419
        assert(link);
580✔
1420
        assert(v);
580✔
1421

1422
        if (!link->dhcp_client)
580✔
1423
                return 0;
1424

1425
        r = dhcp_client_lease_append_json(link, &w);
43✔
1426
        if (r < 0)
43✔
1427
                return r;
1428

1429
        r = dhcp_client_pd_append_json(link, &w);
43✔
1430
        if (r < 0)
43✔
1431
                return r;
1432

1433
        r = dhcp_client_private_options_append_json(link, &w);
43✔
1434
        if (r < 0)
43✔
1435
                return r;
1436

1437
        r = dhcp_client_id_append_json(link, &w);
43✔
1438
        if (r < 0)
43✔
1439
                return r;
1440

1441
        return json_variant_set_field_non_null(v, "DHCPv4Client", w);
43✔
1442
}
1443

1444
static int lldp_tx_append_json(Link *link, sd_json_variant **v) {
580✔
1445
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
580✔
1446
        int r;
580✔
1447

1448
        assert(link);
580✔
1449
        assert(v);
580✔
1450

1451
        if (!link->lldp_tx)
580✔
1452
                return 0;
1453

1454
        r = sd_lldp_tx_describe(link->lldp_tx, &w);
1✔
1455
        if (r < 0)
1✔
1456
                return r;
1457

1458
        return json_variant_set_field_non_null(v, "LLDP", w);
1✔
1459
}
1460

1461
int link_build_json(Link *link, sd_json_variant **ret) {
580✔
1462
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
580✔
1463
        _cleanup_free_ char *type = NULL, *flags = NULL;
580✔
1464
        int r;
580✔
1465

1466
        assert(link);
580✔
1467
        assert(ret);
580✔
1468

1469
        r = net_get_type_string(link->dev, link->iftype, &type);
580✔
1470
        if (r == -ENOMEM)
580✔
1471
                return r;
1472

1473
        r = link_flags_to_string_alloc(link->flags, &flags);
580✔
1474
        if (r < 0)
580✔
1475
                return r;
1476

1477
        r = sd_json_buildo(
580✔
1478
                        &v,
1479
                        /* basic information */
1480
                        SD_JSON_BUILD_PAIR_INTEGER("Index", link->ifindex),
1481
                        SD_JSON_BUILD_PAIR_STRING("Name", link->ifname),
1482
                        JSON_BUILD_PAIR_STRV_NON_EMPTY("AlternativeNames", link->alternative_names),
1483
                        SD_JSON_BUILD_PAIR_CONDITION(link->master_ifindex > 0,
1484
                                                     "MasterInterfaceIndex", SD_JSON_BUILD_INTEGER(link->master_ifindex)),
1485
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("Kind", link->kind),
1486
                        SD_JSON_BUILD_PAIR_STRING("Type", type),
1487
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("Driver", link->driver),
1488
                        SD_JSON_BUILD_PAIR_UNSIGNED("Flags", link->flags),
1489
                        SD_JSON_BUILD_PAIR_STRING("FlagsString", flags),
1490
                        SD_JSON_BUILD_PAIR_UNSIGNED("KernelOperationalState", link->kernel_operstate),
1491
                        SD_JSON_BUILD_PAIR_STRING("KernelOperationalStateString", kernel_operstate_to_string(link->kernel_operstate)),
1492
                        SD_JSON_BUILD_PAIR_UNSIGNED("MTU", link->mtu),
1493
                        SD_JSON_BUILD_PAIR_UNSIGNED("MinimumMTU", link->min_mtu),
1494
                        SD_JSON_BUILD_PAIR_UNSIGNED("MaximumMTU", link->max_mtu),
1495
                        JSON_BUILD_PAIR_HW_ADDR_NON_NULL("HardwareAddress", &link->hw_addr),
1496
                        JSON_BUILD_PAIR_HW_ADDR_NON_NULL("PermanentHardwareAddress", &link->permanent_hw_addr),
1497
                        JSON_BUILD_PAIR_HW_ADDR_NON_NULL("BroadcastAddress", &link->bcast_addr),
1498
                        JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("IPv6LinkLocalAddress", &link->ipv6ll_address),
1499
                        /* wlan information */
1500
                        SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceType",
1501
                                                     SD_JSON_BUILD_UNSIGNED(link->wlan_iftype)),
1502
                        SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceTypeString",
1503
                                                     SD_JSON_BUILD_STRING(nl80211_iftype_to_string(link->wlan_iftype))),
1504
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("SSID", link->ssid),
1505
                        JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL("BSSID", &link->bssid),
1506
                        /* link state */
1507
                        SD_JSON_BUILD_PAIR_STRING("AdministrativeState", link_state_to_string(link->state)),
1508
                        SD_JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(link->operstate)),
1509
                        SD_JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(link->carrier_state)),
1510
                        SD_JSON_BUILD_PAIR_STRING("AddressState", link_address_state_to_string(link->address_state)),
1511
                        SD_JSON_BUILD_PAIR_STRING("IPv4AddressState", link_address_state_to_string(link->ipv4_address_state)),
1512
                        SD_JSON_BUILD_PAIR_STRING("IPv6AddressState", link_address_state_to_string(link->ipv6_address_state)),
1513
                        SD_JSON_BUILD_PAIR_STRING("OnlineState", link_online_state_to_string(link->online_state)));
1514
        if (r < 0)
580✔
1515
                return r;
1516

1517
        r = network_append_json(link->network, &v);
580✔
1518
        if (r < 0)
580✔
1519
                return r;
1520

1521
        r = netdev_append_json(link->netdev, &v);
580✔
1522
        if (r < 0)
580✔
1523
                return r;
1524

1525
        r = device_append_json(link->dev, &v);
580✔
1526
        if (r < 0)
580✔
1527
                return r;
1528

1529
        r = dns_append_json(link, &v);
580✔
1530
        if (r < 0)
580✔
1531
                return r;
1532

1533
        r = dnr_append_json(link, &v);
580✔
1534
        if (r < 0)
580✔
1535
                return r;
1536

1537
        r = ntp_append_json(link, &v);
580✔
1538
        if (r < 0)
580✔
1539
                return r;
1540

1541
        r = sip_append_json(link, &v);
580✔
1542
        if (r < 0)
580✔
1543
                return r;
1544

1545
        r = domains_append_json(link, /* is_route = */ false, &v);
580✔
1546
        if (r < 0)
580✔
1547
                return r;
1548

1549
        r = domains_append_json(link, /* is_route = */ true, &v);
580✔
1550
        if (r < 0)
580✔
1551
                return r;
1552

1553
        r = ntas_append_json(link, &v);
580✔
1554
        if (r < 0)
580✔
1555
                return r;
1556

1557
        r = dns_misc_append_json(link, &v);
580✔
1558
        if (r < 0)
580✔
1559
                return r;
1560

1561
        r = captive_portal_append_json(link, &v);
580✔
1562
        if (r < 0)
580✔
1563
                return r;
1564

1565
        r = pref64_append_json(link, &v);
580✔
1566
        if (r < 0)
580✔
1567
                return r;
1568

1569
        r = addresses_append_json(link, /* serializing = */ false, &v);
580✔
1570
        if (r < 0)
580✔
1571
                return r;
1572

1573
        r = neighbors_append_json(link->neighbors, &v);
580✔
1574
        if (r < 0)
580✔
1575
                return r;
1576

1577
        r = nexthops_append_json(link->manager, link->ifindex, &v);
580✔
1578
        if (r < 0)
580✔
1579
                return r;
1580

1581
        r = routes_append_json(link->manager, link->ifindex, &v);
580✔
1582
        if (r < 0)
580✔
1583
                return r;
1584

1585
        r = dhcp_server_append_json(link, &v);
580✔
1586
        if (r < 0)
580✔
1587
                return r;
1588

1589
        r = dhcp_client_append_json(link, &v);
580✔
1590
        if (r < 0)
580✔
1591
                return r;
1592

1593
        r = dhcp6_client_append_json(link, &v);
580✔
1594
        if (r < 0)
580✔
1595
                return r;
1596

1597
        r = lldp_tx_append_json(link, &v);
580✔
1598
        if (r < 0)
580✔
1599
                return r;
1600

1601
        *ret = TAKE_PTR(v);
580✔
1602
        return 0;
580✔
1603
}
1604

1605
static int links_append_json(Manager *manager, sd_json_variant **v) {
78✔
1606
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
78✔
1607
        _cleanup_free_ Link **links = NULL;
78✔
1608
        size_t n_links = 0;
78✔
1609
        int r;
78✔
1610

1611
        assert(manager);
78✔
1612
        assert(v);
78✔
1613

1614
        r = hashmap_dump_sorted(manager->links_by_index, (void***) &links, &n_links);
78✔
1615
        if (r < 0)
78✔
1616
                return r;
1617

1618
        FOREACH_ARRAY(link, links, n_links) {
653✔
1619
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL;
575✔
1620

1621
                r = link_build_json(*link, &e);
575✔
1622
                if (r < 0)
575✔
1623
                        return r;
1624

1625
                r = sd_json_variant_append_array(&array, e);
575✔
1626
                if (r < 0)
575✔
1627
                        return r;
1628
        }
1629

1630
        return json_variant_set_field_non_null(v, "Interfaces", array);
78✔
1631
}
1632

1633
int manager_build_json(Manager *manager, sd_json_variant **ret) {
78✔
1634
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
78✔
1635
        int r;
78✔
1636

1637
        assert(manager);
78✔
1638
        assert(ret);
78✔
1639

1640
        r = links_append_json(manager, &v);
78✔
1641
        if (r < 0)
78✔
1642
                return r;
1643

1644
        r = nexthops_append_json(manager, /* ifindex = */ 0, &v);
78✔
1645
        if (r < 0)
78✔
1646
                return r;
1647

1648
        r = routes_append_json(manager, /* ifindex = */ 0, &v);
78✔
1649
        if (r < 0)
78✔
1650
                return r;
1651

1652
        r = routing_policy_rules_append_json(manager->rules, &v);
78✔
1653
        if (r < 0)
78✔
1654
                return r;
1655

1656
        *ret = TAKE_PTR(v);
78✔
1657
        return 0;
78✔
1658
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc