1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #define THIRTY_YEARS_IN_SECONDS 946707779
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 #include "common.h"
38 #include "duid.h"
39 #include "net.h"
40
41 size_t
get_duid(unsigned char * duid,const struct interface * iface)42 get_duid(unsigned char *duid, const struct interface *iface)
43 {
44 FILE *f;
45 uint16_t type = 0;
46 uint16_t hw = 0;
47 uint32_t ul;
48 time_t t;
49 int x = 0;
50 unsigned char *p = duid;
51 size_t len = 0;
52 char *line;
53
54 /* If we already have a DUID then use it as it's never supposed
55 * to change once we have one even if the interfaces do */
56 if ((f = fopen(DUID, "r"))) {
57 while ((line = get_line(f))) {
58 len = hwaddr_aton(NULL, line);
59 if (len && len <= DUID_LEN) {
60 hwaddr_aton(duid, line);
61 break;
62 }
63 len = 0;
64 }
65 fclose(f);
66 if (len)
67 return len;
68 } else {
69 if (errno != ENOENT)
70 return 0;
71 }
72
73 /* No file? OK, lets make one based on our interface */
74 if (!(f = fopen(DUID, "w")))
75 return 0;
76 type = htons(1); /* DUI-D-LLT */
77 memcpy(p, &type, 2);
78 p += 2;
79 hw = htons(iface->family);
80 memcpy(p, &hw, 2);
81 p += 2;
82 /* time returns seconds from jan 1 1970, but DUID-LLT is
83 * seconds from jan 1 2000 modulo 2^32 */
84 t = time(NULL) - THIRTY_YEARS_IN_SECONDS;
85 ul = htonl(t & 0xffffffff);
86 memcpy(p, &ul, 4);
87 p += 4;
88 /* Finally, add the MAC address of the interface */
89 memcpy(p, iface->hwaddr, iface->hwlen);
90 p += iface->hwlen;
91 len = p - duid;
92 x = fprintf(f, "%s\n", hwaddr_ntoa(duid, len));
93 fclose(f);
94 /* Failed to write the duid? scrub it, we cannot use it */
95 if (x < 1) {
96 len = 0;
97 unlink(DUID);
98 }
99 return len;
100 }
101