1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* SPDX-License-Identifier: LGPL-2.1+ */
/*
* Copyright (C) 2019 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-initrd-generator.h"
#include <arpa/inet.h>
#include <linux/if_ether.h>
#include "nm-core-internal.h"
/*****************************************************************************/
#define _NMLOG(level, domain, ...) \
nm_log((level), \
(domain), \
NULL, \
NULL, \
"dt-reader: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__) _NM_UTILS_MACRO_REST(__VA_ARGS__))
/*****************************************************************************/
static gboolean
dt_get_property(const char *base,
const char *dev,
const char *prop,
char ** contents,
size_t * length)
{
gs_free char *filename = g_build_filename(base, dev, prop, NULL);
gs_free_error GError *error = NULL;
if (!g_file_test(filename, G_FILE_TEST_EXISTS))
return FALSE;
if (!contents)
return TRUE;
if (!g_file_get_contents(filename, contents, length, &error)) {
_LOGW(LOGD_CORE, "%s: Can not read the %s property: %s", dev, prop, error->message);
return FALSE;
}
return TRUE;
}
static NMIPAddress *
dt_get_ipaddr_property(const char *base, const char *dev, const char *prop, int *family)
{
gs_free char *buf = NULL;
size_t len;
int f;
if (!dt_get_property(base, dev, prop, &buf, &len))
return NULL;
f = nm_utils_addr_family_from_size(len);
if (f == AF_UNSPEC || (*family != AF_UNSPEC && *family != f)) {
_LOGW(LOGD_CORE, "%s: Address %s has unrecognized length (%zd)", dev, prop, len);
return NULL;
}
*family = f;
return nm_ip_address_new_binary(f, buf, 0, NULL);
}
static char *
dt_get_hwaddr_property(const char *base, const char *dev, const char *prop)
{
gs_free guint8 *buf = NULL;
size_t len;
if (!dt_get_property(base, dev, prop, (char **) &buf, &len))
return NULL;
if (len != ETH_ALEN) {
_LOGW(LOGD_CORE, "%s: MAC address %s has unrecognized length (%zd)", dev, prop, len);
return NULL;
}
return g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
buf[0],
buf[1],
buf[2],
buf[3],
buf[4],
buf[4]);
}
static NMIPAddress *
str_addr(const char *str, int *family)
{
NMIPAddr addr_bin;
if (!nm_utils_parse_inaddr_bin_full(*family, TRUE, str, family, &addr_bin)) {
_LOGW(LOGD_CORE, "Malformed IP address: '%s'", str);
return NULL;
}
return nm_ip_address_new_binary(*family, &addr_bin, 0, NULL);
}
NMConnection *
nmi_dt_reader_parse(const char *sysfs_dir)
{
gs_unref_object NMConnection *connection = NULL;
gs_free char * base = NULL;
gs_free char * bootpath = NULL;
gs_strfreev char ** tokens = NULL;
char * path = NULL;
gboolean bootp = FALSE;
const char * s_ipaddr = NULL;
const char * s_netmask = NULL;
const char * s_gateway = NULL;
nm_auto_unref_ip_address NMIPAddress *ipaddr = NULL;
nm_auto_unref_ip_address NMIPAddress *gateway = NULL;
const char * duplex = NULL;
gs_free char * hwaddr = NULL;
gs_free char * local_hwaddr = NULL;
gs_free char * hostname = NULL;
guint32 speed = 0;
int prefix = -1;
NMSettingIPConfig * s_ip = NULL;
NMSetting * s_ip4 = NULL;
NMSetting * s_ip6 = NULL;
NMSetting * s_wired = NULL;
int family = AF_UNSPEC;
int i = 0;
char * c;
gs_free_error GError *error = NULL;
base = g_build_filename(sysfs_dir, "firmware", "devicetree", "base", NULL);
if (!dt_get_property(base, "chosen", "bootpath", &bootpath, NULL))
return NULL;
c = strchr(bootpath, ':');
if (c) {
*c = '\0';
path = c + 1;
} else {
path = "";
}
dt_get_property(base, "chosen", "client-name", &hostname, NULL);
local_hwaddr = dt_get_hwaddr_property(base, bootpath, "local-mac-address");
hwaddr = dt_get_hwaddr_property(base, bootpath, "mac-address");
if (nm_streq0(local_hwaddr, hwaddr))
nm_clear_g_free(&local_hwaddr);
tokens = g_strsplit(path, ",", 0);
/*
* Ethernet device settings. Defined by "Open Firmware,
* Recommended Practice: Device Support Extensions, Version 1.0 [1]
* [1] https://www.devicetree.org/open-firmware/practice/devicex/dse1_0a.ps
*/
for (i = 0; tokens[i]; i++) {
/* Skip these. They have magical meaning for OpenFirmware. */
if (NM_IN_STRSET(tokens[i], "nfs", "last"))
continue;
if (nm_streq(tokens[i], "promiscuous")) {
/* Ignore. */
continue;
}
if (g_str_has_prefix(tokens[i], "speed=")) {
speed = _nm_utils_ascii_str_to_int64(tokens[i] + 6, 10, 0, G_MAXUINT32, 0);
continue;
}
if (g_str_has_prefix(tokens[i], "duplex=auto")) {
continue;
} else if (g_str_has_prefix(tokens[i], "duplex=half")
|| g_str_has_prefix(tokens[i], "duplex=full")) {
duplex = tokens[i] + 7;
continue;
}
break;
}
/*
* Network boot configuration. Defined by "Open Firmware,
* Recommended Practice: TFTP Booting Extension, Version 1.0 [1]
* [1] https://www.devicetree.org/open-firmware/practice/obp-tftp/tftp1_0.pdf
*/
for (; tokens[i]; i++) {
if (NM_IN_STRSET(tokens[i], "bootp", "dhcp", "rarp")) {
bootp = TRUE;
continue;
}
break;
}
/* s-iaddr, or perhaps a raw absolute filename */
if (tokens[i] && tokens[i][0] != '/')
i++;
/* filename */
if (tokens[i])
i++;
/* c-iaddr */
if (tokens[i]) {
s_ipaddr = tokens[i];
i++;
}
/* g-iaddr */
if (tokens[i]) {
s_gateway = tokens[i];
i++;
}
if (tokens[i] && (strchr(tokens[i], '.') || strchr(tokens[i], ':'))) {
/* yaboot claims the mask can be specified here,
* though it doesn't support it. */
s_netmask = tokens[i];
i++;
}
/* bootp-retries */
if (tokens[i])
i++;
/* tftp-retries */
if (tokens[i])
i++;
if (tokens[i]) {
/* yaboot accepts a mask here */
s_netmask = tokens[i];
i++;
}
connection = nm_simple_connection_new();
nm_connection_add_setting(connection,
g_object_new(NM_TYPE_SETTING_CONNECTION,
NM_SETTING_CONNECTION_TYPE,
NM_SETTING_WIRED_SETTING_NAME,
NM_SETTING_CONNECTION_ID,
"OpenFirmware Connection",
NULL));
s_ip4 = nm_setting_ip4_config_new();
nm_connection_add_setting(connection, s_ip4);
s_ip6 = nm_setting_ip6_config_new();
nm_connection_add_setting(connection, s_ip6);
g_object_set(s_ip6,
NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE,
(int) NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64,
NULL);
if (!bootp && dt_get_property(base, "chosen", "bootp-response", NULL, NULL))
bootp = TRUE;
if (!bootp) {
nm_auto_unref_ip_address NMIPAddress *netmask = NULL;
netmask = dt_get_ipaddr_property(base, "chosen", "netmask-ip", &family);
gateway = dt_get_ipaddr_property(base, "chosen", "gateway-ip", &family);
if (gateway)
s_gateway = nm_ip_address_get_address(gateway);
ipaddr = dt_get_ipaddr_property(base, "chosen", "client-ip", &family);
if (family == AF_UNSPEC) {
nm_assert(netmask == NULL);
nm_assert(gateway == NULL);
nm_assert(ipaddr == NULL);
netmask = str_addr(s_netmask, &family);
ipaddr = str_addr(s_ipaddr, &family);
prefix = _nm_utils_ascii_str_to_int64(s_netmask, 10, 0, 128, -1);
}
if (prefix == -1 && family == AF_INET && netmask) {
guint32 netmask_v4;
nm_ip_address_get_address_binary(netmask, &netmask_v4);
prefix = nm_utils_ip4_netmask_to_prefix(netmask_v4);
}
if (prefix == -1)
_LOGW(LOGD_CORE, "Unable to determine the network prefix");
else
nm_ip_address_set_prefix(ipaddr, prefix);
}
if (!ipaddr) {
family = AF_UNSPEC;
bootp = TRUE;
}
if (bootp) {
g_object_set(s_ip4,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NM_SETTING_IP_CONFIG_DHCP_HOSTNAME,
hostname,
NULL);
g_object_set(s_ip6,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP_CONFIG_DHCP_HOSTNAME,
hostname,
NULL);
} else {
switch (family) {
case AF_INET:
s_ip = (NMSettingIPConfig *) s_ip4;
g_object_set(s_ip4,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
NULL);
g_object_set(s_ip6,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP6_CONFIG_METHOD_DISABLED,
NULL);
break;
case AF_INET6:
s_ip = (NMSettingIPConfig *) s_ip6;
g_object_set(s_ip4,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP4_CONFIG_METHOD_DISABLED,
NULL);
g_object_set(s_ip6,
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP6_CONFIG_METHOD_MANUAL,
NULL);
break;
default:
g_return_val_if_reached(NULL);
}
nm_setting_ip_config_add_address(s_ip, ipaddr);
g_object_set(s_ip, NM_SETTING_IP_CONFIG_GATEWAY, s_gateway, NULL);
}
if (duplex || speed || hwaddr || local_hwaddr) {
s_wired = nm_setting_wired_new();
nm_connection_add_setting(connection, s_wired);
g_object_set(s_wired,
NM_SETTING_WIRED_SPEED,
speed,
NM_SETTING_WIRED_DUPLEX,
duplex,
NM_SETTING_WIRED_MAC_ADDRESS,
hwaddr,
NM_SETTING_WIRED_CLONED_MAC_ADDRESS,
local_hwaddr,
NULL);
}
if (!nm_connection_normalize(connection, NULL, NULL, &error)) {
_LOGW(LOGD_CORE, "Generated an invalid connection: %s", error->message);
nm_clear_pointer(&connection, g_object_unref);
}
return g_steal_pointer(&connection);
}
|