Browse code

Query DHCP DUID, IAID

Change-Id: I9185ee8d4b903283e7527539aa97d095a85aaa90
Reviewed-on: http://photon-jenkins.eng.vmware.com/682
Tested-by: jenkins-photon <wangnan2015@hotmail.com>
Reviewed-by: suezzelur <anishs@vmware.com>

Vinay Kulkarni authored on 2016/04/01 16:50:49
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,460 @@
0
+diff -uNr systemd-228/src/libsystemd/sd-network/sd-network.c systemd-228-query-duid/src/libsystemd/sd-network/sd-network.c
1
+--- systemd-228/src/libsystemd/sd-network/sd-network.c	2015-11-18 07:59:06.000000000 +0000
2
+@@ -297,6 +297,51 @@
3
+         return network_get_link_strv("CARRIER_BOUND_BY", ifindex, ret);
4
+ }
5
+ 
6
++static int network_get_file_str(const char *key, const char *fpath, int ifindex, char **ret) {
7
++        _cleanup_free_ char *p = NULL, *s = NULL;
8
++        int r;
9
++
10
++        assert_return(ifindex > 0, -EINVAL);
11
++        assert_return(fpath, -EINVAL);
12
++        assert_return(ret, -EINVAL);
13
++
14
++        if (asprintf(&p, "%s/%d", fpath, ifindex) < 0)
15
++                return -ENOMEM;
16
++
17
++        r = parse_env_file(p, NEWLINE, key, &s, NULL);
18
++        if (r == -ENOENT)
19
++                return -ENODATA;
20
++        if (r < 0)
21
++                return r;
22
++        if (isempty(s))
23
++                return -ENODATA;
24
++
25
++        *ret = s;
26
++        s = NULL;
27
++        return 0;
28
++}
29
++
30
++_public_ int sd_network_link_get_clientid(int ifindex, char **clientid) {
31
++        return network_get_file_str("CLIENTID",
32
++                                    "/run/systemd/netif/leases",
33
++                                    ifindex,
34
++                                    clientid);
35
++}
36
++
37
++_public_ int sd_network_link_get_iaid(int ifindex, char **iaid) {
38
++        return network_get_file_str("IAID",
39
++                                    "/run/systemd/netif/leases6",
40
++                                    ifindex,
41
++                                    iaid);
42
++}
43
++
44
++_public_ int sd_network_link_get_duid(int ifindex, char **duid) {
45
++        return network_get_file_str("DUID",
46
++                                    "/run/systemd/netif/leases6",
47
++                                    ifindex,
48
++                                    duid);
49
++}
50
++
51
+ _public_ int sd_network_link_get_wildcard_domain(int ifindex) {
52
+         int r;
53
+         _cleanup_free_ char *p = NULL, *s = NULL;
54
+diff -uNr systemd-228/src/libsystemd-network/dhcp6-lease-internal.h systemd-228-query-duid/src/libsystemd-network/dhcp6-lease-internal.h
55
+--- systemd-228/src/libsystemd-network/dhcp6-lease-internal.h	2015-11-18 07:59:06.000000000 +0000
56
+@@ -25,6 +25,7 @@
57
+ #include <stdint.h>
58
+ 
59
+ #include "sd-dhcp6-lease.h"
60
++#include "dhcp-identifier.h"
61
+ #include "dhcp6-internal.h"
62
+ 
63
+ struct sd_dhcp6_lease {
64
+@@ -36,6 +37,8 @@
65
+         bool rapid_commit;
66
+ 
67
+         DHCP6IA ia;
68
++        struct duid *duid;
69
++        size_t duid_len;
70
+ 
71
+         DHCP6Address *addr_iter;
72
+ 
73
+@@ -64,6 +67,7 @@
74
+ int dhcp6_lease_get_rapid_commit(sd_dhcp6_lease *lease, bool *rapid_commit);
75
+ 
76
+ int dhcp6_lease_get_iaid(sd_dhcp6_lease *lease, be32_t *iaid);
77
++int dhcp6_lease_set_duid(sd_dhcp6_lease *lease, struct duid *duid, size_t duid_len);
78
+ 
79
+ int dhcp6_lease_set_dns(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen);
80
+ int dhcp6_lease_set_domains(sd_dhcp6_lease *lease, uint8_t *optval,
81
+@@ -74,5 +78,7 @@
82
+ 
83
+ int dhcp6_lease_new(sd_dhcp6_lease **ret);
84
+ 
85
++int dhcp6_lease_save(sd_dhcp6_lease *lease, const char *lease_file);
86
++
87
+ DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp6_lease*, sd_dhcp6_lease_unref);
88
+ #define _cleanup_dhcp6_lease_free_ _cleanup_(sd_dhcp6_lease_unrefp)
89
+diff -uNr systemd-228/src/libsystemd-network/sd-dhcp6-client.c systemd-228-query-duid/src/libsystemd-network/sd-dhcp6-client.c
90
+--- systemd-228/src/libsystemd-network/sd-dhcp6-client.c	2016-04-01 00:10:28.311819290 +0000
91
+@@ -202,6 +202,23 @@
92
+         return 0;
93
+ }
94
+ 
95
++int sd_dhcp6_client_get_duid(sd_dhcp6_client *client, uint16_t *duid_type,
96
++                             const uint8_t **duid, size_t *duid_len) {
97
++        assert_return(client, -EINVAL);
98
++        assert_return(IN_SET(client->state, DHCP6_STATE_STOPPED), -EBUSY);
99
++
100
++        *duid_type = 0;
101
++        *duid_len = 0;
102
++        *duid = NULL;
103
++        if (client->duid_len > 0) {
104
++                *duid_type = be16toh(client->duid.type);
105
++                *duid_len = client->duid_len - sizeof(client->duid.type);
106
++                *duid = client->duid.raw.data;
107
++        }
108
++
109
++        return 0;
110
++}
111
++
112
+ int sd_dhcp6_client_set_iaid(sd_dhcp6_client *client, uint32_t iaid) {
113
+         assert_return(client, -EINVAL);
114
+         assert_return(IN_SET(client->state, DHCP6_STATE_STOPPED), -EBUSY);
115
+@@ -211,6 +228,14 @@
116
+         return 0;
117
+ }
118
+ 
119
++int sd_dhcp6_client_get_iaid(sd_dhcp6_client *client, uint32_t *iaid) {
120
++        assert_return(client, -EINVAL);
121
++
122
++        *iaid = be32toh(client->ia_na.id);
123
++
124
++        return 0;
125
++}
126
++
127
+ int sd_dhcp6_client_set_information_request(sd_dhcp6_client *client, int enabled) {
128
+         assert_return(client, -EINVAL);
129
+         assert_return(IN_SET(client->state, DHCP6_STATE_STOPPED), -EBUSY);
130
+@@ -746,6 +771,10 @@
131
+                         if (r < 0 && r != -ENOMSG)
132
+                                 return r;
133
+ 
134
++                        r = dhcp6_lease_set_duid(lease, &client->duid, client->duid_len);
135
++                        if (r < 0)
136
++                                return r;
137
++
138
+                         r = dhcp6_lease_get_iaid(lease, &iaid_lease);
139
+                         if (r < 0)
140
+                                 return r;
141
+diff -uNr systemd-228/src/libsystemd-network/sd-dhcp6-lease.c systemd-228-query-duid/src/libsystemd-network/sd-dhcp6-lease.c
142
+--- systemd-228/src/libsystemd-network/sd-dhcp6-lease.c	2015-11-18 07:59:06.000000000 +0000
143
+@@ -20,14 +20,19 @@
144
+   along with systemd; If not, see <http://www.gnu.org/licenses/>.
145
+ ***/
146
+ 
147
++#include <arpa/inet.h>
148
+ #include <errno.h>
149
+ 
150
+ #include "alloc-util.h"
151
+ #include "dhcp6-lease-internal.h"
152
+ #include "dhcp6-protocol.h"
153
++#include "fd-util.h"
154
++#include "fileio.h"
155
++#include "hexdecoct.h"
156
+ #include "strv.h"
157
+ #include "util.h"
158
+ 
159
++
160
+ int dhcp6_lease_clear_timers(DHCP6IA *ia) {
161
+         assert_return(ia, -EINVAL);
162
+ 
163
+@@ -150,6 +155,15 @@
164
+         return 0;
165
+ }
166
+ 
167
++int dhcp6_lease_set_duid(sd_dhcp6_lease *lease, struct duid *duid, size_t duid_len) {
168
++        assert_return(lease, -EINVAL);
169
++        assert_return(duid, -EINVAL);
170
++
171
++        lease->duid = duid;
172
++        lease->duid_len = duid_len;
173
++        return 0;
174
++}
175
++
176
+ int sd_dhcp6_lease_get_address(sd_dhcp6_lease *lease, struct in6_addr *addr,
177
+                                uint32_t *lifetime_preferred,
178
+                                uint32_t *lifetime_valid) {
179
+@@ -410,3 +424,74 @@
180
+         *ret = lease;
181
+         return 0;
182
+ }
183
++
184
++int dhcp6_lease_save(sd_dhcp6_lease *lease, const char *lease_file) {
185
++        _cleanup_free_ char *temp_path = NULL;
186
++        _cleanup_fclose_ FILE *f = NULL;
187
++        char addr6[INET6_ADDRSTRLEN];
188
++        const char *paddr6;
189
++        struct in6_addr ip6_addr;
190
++        uint32_t lifetime_preferred, lifetime_valid;
191
++        be32_t iaid_lease;
192
++        const struct in6_addr *addresses;
193
++        int r;
194
++
195
++        assert(lease);
196
++        assert(lease_file);
197
++
198
++        r = fopen_temporary(lease_file, &f, &temp_path);
199
++        if (r < 0)
200
++                goto fail;
201
++
202
++        fchmod(fileno(f), 0644);
203
++
204
++        fprintf(f, "# This is private data. Do not parse.\n");
205
++
206
++        sd_dhcp6_lease_reset_address_iter(lease);
207
++        do {
208
++                r = sd_dhcp6_lease_get_address(lease, &ip6_addr,
209
++                                               &lifetime_preferred,
210
++                                               &lifetime_valid);
211
++                if (r >= 0) {
212
++                        paddr6 = inet_ntop(AF_INET6, &ip6_addr, addr6,
213
++                                           INET6_ADDRSTRLEN);
214
++                        if (paddr6 != NULL) {
215
++                                fprintf(f, "ADDRESS=%s\n", paddr6);
216
++                                fprintf(f, "LIFETIME_PREFERRED=%u\n", lifetime_preferred);
217
++                                fprintf(f, "LIFETIME_VALID=%u\n", lifetime_valid);
218
++                        }
219
++                }
220
++        } while (r >=0);
221
++
222
++        r = dhcp6_lease_get_iaid(lease, &iaid_lease);
223
++        if (r == 0)
224
++                fprintf(f, "IAID=%u\n", be32toh(iaid_lease));
225
++
226
++        if (lease->duid_len > 0) {
227
++                _cleanup_free_ char *duid_hex;
228
++
229
++                duid_hex = hexmem(lease->duid, lease->duid_len);
230
++                if (duid_hex == NULL) {
231
++                        r = -ENOMEM;
232
++                        goto fail;
233
++                }
234
++                fprintf(f, "DUID=%s\n", duid_hex);
235
++        }
236
++
237
++        r = fflush_and_check(f);
238
++        if (r < 0)
239
++                goto fail;
240
++
241
++        if (rename(temp_path, lease_file) < 0) {
242
++                r = -errno;
243
++                goto fail;
244
++        }
245
++
246
++        return 0;
247
++
248
++fail:
249
++        if (temp_path)
250
++                (void) unlink(temp_path);
251
++
252
++        return log_error_errno(r, "Failed to save lease data %s: %m", lease_file);
253
++}
254
+diff -uNr systemd-228/src/network/networkctl.c systemd-228-query-duid/src/network/networkctl.c
255
+--- systemd-228/src/network/networkctl.c	2015-11-18 07:59:06.000000000 +0000
256
+@@ -502,6 +502,7 @@
257
+                 const char *name) {
258
+         _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **domains = NULL;
259
+         _cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL;
260
++        _cleanup_free_ char *clientid = NULL, *duid = NULL, *iaid = NULL;
261
+         _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
262
+         _cleanup_device_unref_ sd_device *d = NULL;
263
+         char devid[2 + DECIMAL_STR_MAX(int)];
264
+@@ -668,7 +669,19 @@
265
+ 
266
+         (void) sd_network_link_get_timezone(ifindex, &tz);
267
+         if (tz)
268
+-                printf("       Time Zone: %s", tz);
269
++                printf("       Time Zone: %s\n", tz);
270
++
271
++        (void) sd_network_link_get_clientid(ifindex, &clientid);
272
++        if (clientid)
273
++                printf("        CLIENTID: %s\n", clientid);
274
++
275
++        (void) sd_network_link_get_iaid(ifindex, &iaid);
276
++        if (iaid)
277
++                printf("            IAID: %s\n", iaid);
278
++
279
++        (void) sd_network_link_get_duid(ifindex, &duid);
280
++        if (duid)
281
++                printf("            DUID: %s", duid);
282
+ 
283
+         return 0;
284
+ }
285
+diff -uNr systemd-228/src/network/networkd-dhcp6.c systemd-228-query-duid/src/network/networkd-dhcp6.c
286
+--- systemd-228/src/network/networkd-dhcp6.c	2016-04-01 00:10:28.311819290 +0000
287
+@@ -113,6 +113,8 @@
288
+                         return r;
289
+         }
290
+ 
291
++        link->dhcp6_lease = sd_dhcp6_lease_ref(lease);
292
++
293
+         return 0;
294
+ }
295
+ 
296
+@@ -134,7 +136,9 @@
297
+                 if (sd_dhcp6_client_get_lease(client, NULL) >= 0)
298
+                         log_link_warning(link, "DHCPv6 lease lost");
299
+ 
300
++                link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
301
+                 link->dhcp6_configured = false;
302
++                link_dirty(link);
303
+                 break;
304
+ 
305
+         case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
306
+@@ -153,6 +157,7 @@
307
+                 }
308
+ 
309
+                 link->dhcp6_configured = true;
310
++                link_dirty(link);
311
+                 break;
312
+ 
313
+         default:
314
+diff -uNr systemd-228/src/network/networkd-link.c systemd-228-query-duid/src/network/networkd-link.c
315
+--- systemd-228/src/network/networkd-link.c	2016-04-01 00:10:28.311819290 +0000
316
+@@ -26,6 +26,7 @@
317
+ #include "alloc-util.h"
318
+ #include "bus-util.h"
319
+ #include "dhcp-lease-internal.h"
320
++#include "dhcp6-lease-internal.h"
321
+ #include "event-util.h"
322
+ #include "fd-util.h"
323
+ #include "fileio.h"
324
+@@ -371,6 +372,11 @@
325
+         if (r < 0)
326
+                 return -ENOMEM;
327
+ 
328
++        r = asprintf(&link->lease6_file, "/run/systemd/netif/leases6/%d",
329
++                     link->ifindex);
330
++        if (r < 0)
331
++                return -ENOMEM;
332
++
333
+         r = asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d",
334
+                      link->ifindex);
335
+         if (r < 0)
336
+@@ -421,8 +427,10 @@
337
+         sd_dhcp_server_unref(link->dhcp_server);
338
+         sd_dhcp_client_unref(link->dhcp_client);
339
+         sd_dhcp_lease_unref(link->dhcp_lease);
340
++        sd_dhcp_lease_unref(link->dhcp6_lease);
341
+ 
342
+         free(link->lease_file);
343
++        free(link->lease6_file);
344
+ 
345
+         sd_lldp_free(link->lldp);
346
+ 
347
+@@ -2749,6 +2757,7 @@
348
+         assert(link);
349
+         assert(link->state_file);
350
+         assert(link->lease_file);
351
++        assert(link->lease6_file);
352
+         assert(link->manager);
353
+ 
354
+         if (link->state == LINK_STATE_LINGER) {
355
+@@ -3004,6 +3013,31 @@
356
+         } else
357
+                 unlink(link->lease_file);
358
+ 
359
++        if (link->dhcp6_lease) {
360
++                struct in6_addr addr6;
361
++                uint32_t lp, lv;
362
++                assert(link->network);
363
++
364
++                fputs("DHCP6_ADDRESS=", f);
365
++
366
++                sd_dhcp6_lease_reset_address_iter(link->dhcp6_lease);
367
++                while (sd_dhcp6_lease_get_address(link->dhcp6_lease,
368
++                                                  &addr6, &lp, &lv) >= 0) {
369
++                        serialize_in6_addrs(f, &addr6, 1);
370
++                        fputc(' ', f);
371
++                }
372
++                fputc('\n', f);
373
++
374
++                r = dhcp6_lease_save(link->dhcp6_lease, link->lease6_file);
375
++                if (r < 0)
376
++                        goto fail;
377
++
378
++                fprintf(f,
379
++                        "DHCP6_LEASE=%s\n",
380
++                        link->lease6_file);
381
++        } else
382
++                unlink(link->lease6_file);
383
++
384
+         if (link->ipv4ll) {
385
+                 struct in_addr address;
386
+ 
387
+diff -uNr systemd-228/src/network/networkd-link.h systemd-228-query-duid/src/network/networkd-link.h
388
+--- systemd-228/src/network/networkd-link.h	2015-11-18 07:59:06.000000000 +0000
389
+@@ -92,6 +92,7 @@
390
+         sd_dhcp_client *dhcp_client;
391
+         sd_dhcp_lease *dhcp_lease;
392
+         char *lease_file;
393
++        char *lease6_file;
394
+         uint16_t original_mtu;
395
+         unsigned dhcp4_messages;
396
+         bool dhcp4_configured;
397
+@@ -111,6 +112,7 @@
398
+ 
399
+         sd_ndisc *ndisc_router_discovery;
400
+         sd_dhcp6_client *dhcp6_client;
401
++        sd_dhcp6_lease *dhcp6_lease;
402
+         bool rtnl_extended_attrs;
403
+ 
404
+         sd_lldp *lldp;
405
+diff -uNr systemd-228/src/network/networkd.c systemd-228-query-duid/src/network/networkd.c
406
+--- systemd-228/src/network/networkd.c	2016-04-01 00:10:28.311819290 +0000
407
+@@ -66,6 +66,10 @@
408
+         if (r < 0)
409
+                 log_warning_errno(r, "Could not create runtime directory 'leases': %m");
410
+ 
411
++        r = mkdir_safe_label("/run/systemd/netif/leases6", 0755, uid, gid);
412
++        if (r < 0)
413
++                log_warning_errno(r, "Could not create runtime directory 'leases6': %m");
414
++
415
+         r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid);
416
+         if (r < 0)
417
+                 log_warning_errno(r, "Could not create runtime directory 'lldp': %m");
418
+diff -uNr systemd-228/src/systemd/sd-dhcp6-client.h systemd-228-query-duid/src/systemd/sd-dhcp6-client.h
419
+--- systemd-228/src/systemd/sd-dhcp6-client.h	2016-04-01 00:10:28.311819290 +0000
420
+@@ -54,7 +54,10 @@
421
+                             size_t addr_len, uint16_t arp_type);
422
+ int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint16_t duid_type,
423
+                              uint8_t *duid, size_t duid_len);
424
++int sd_dhcp6_client_get_duid(sd_dhcp6_client *client, uint16_t *duid_type,
425
++                             const uint8_t **duid, size_t *duid_len);
426
+ int sd_dhcp6_client_set_iaid(sd_dhcp6_client *client, uint32_t iaid);
427
++int sd_dhcp6_client_get_iaid(sd_dhcp6_client *client, uint32_t *iaid);
428
+ int sd_dhcp6_client_set_information_request(sd_dhcp6_client *client, int enabled);
429
+ int sd_dhcp6_client_get_information_request(sd_dhcp6_client *client, int *enabled);
430
+ int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client,
431
+diff -uNr systemd-228/src/systemd/sd-network.h systemd-228-query-duid/src/systemd/sd-network.h
432
+--- systemd-228/src/systemd/sd-network.h	2015-11-18 07:59:06.000000000 +0000
433
+@@ -122,6 +122,15 @@
434
+ /* Get the CARRIERS that are bound to current link. */
435
+ int sd_network_link_get_carrier_bound_by(int ifindex, char ***carriers);
436
+ 
437
++/* Get the CLIENTID if the link has a IPv4 DHCP address. */
438
++int sd_network_link_get_clientid(int ifindex, char **clientid);
439
++
440
++/* Get the IAID if the link has a IPv6 DHCP address. */
441
++int sd_network_link_get_iaid(int ifindex, char **iaid);
442
++
443
++/* Get the DUID if the link has a IPv6 DHCP address. */
444
++int sd_network_link_get_duid(int ifindex, char **duid);
445
++
446
+ /* Get the timezone that was learnt on a specific link. */
447
+ int sd_network_link_get_timezone(int ifindex, char **timezone);
448
+ 
... ...
@@ -1,7 +1,7 @@
1 1
 Summary:	Systemd-228
2 2
 Name:		systemd
3 3
 Version:	228
4
-Release:	14%{?dist}
4
+Release:	15%{?dist}
5 5
 License:	LGPLv2+ and GPLv2+ and MIT
6 6
 URL:		http://www.freedesktop.org/wiki/Software/systemd/
7 7
 Group:		System Environment/Security
... ...
@@ -24,6 +24,7 @@ Patch8:         systemd-228-ipv6-disabled-fix.patch
24 24
 Patch9:         systemd-228-swap-disconnect-order-fix.patch
25 25
 Patch10:        systemd-228-duid-iaid-dhcp-preserve.patch
26 26
 Patch11:        systemd-228-timedatectl-PR2749.patch
27
+Patch12:        systemd-228-query-duid.patch
27 28
 Requires:	Linux-PAM
28 29
 Requires:	libcap
29 30
 Requires:	xz
... ...
@@ -67,6 +68,7 @@ sed -i "s:blkid/::" $(grep -rl "blkid/blkid.h")
67 67
 %patch9 -p1
68 68
 %patch10 -p1
69 69
 %patch11 -p1
70
+%patch12 -p1
70 71
 
71 72
 %build
72 73
 ./autogen.sh
... ...
@@ -129,8 +131,9 @@ rm -rf %{buildroot}/*
129 129
 %{_datadir}/*
130 130
 %dir %{_localstatedir}/log/journal
131 131
 
132
-
133 132
 %changelog
133
+*       Wed Mar 31 2016 Vinay Kulkarni <kulkarniv@vmware.com>  228-15
134
+-       Patch to query DHCP DUID, IAID.
134 135
 *       Wed Mar 30 2016 Vinay Kulkarni <kulkarniv@vmware.com>  228-14
135 136
 -       Update DHCP DUID, IAID configuration patch.
136 137
 *       Wed Mar 30 2016 Kumar Kaushik <kaushikk@vmware.com>  228-13