xref: /dragonfly/contrib/dhcpcd/src/duid.c (revision 6a3cbbc2)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #define	UUID_LEN	36
30 #define	DUID_TIME_EPOCH 946684800
31 
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #ifdef BSD
36 #  include <sys/sysctl.h>
37 #endif
38 
39 #include <arpa/inet.h>
40 
41 #include <net/if.h>
42 #include <net/if_arp.h>
43 
44 #include <errno.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 
52 #include "common.h"
53 #include "dhcpcd.h"
54 #include "duid.h"
55 #include "logerr.h"
56 
57 static size_t
58 duid_machineuuid(char *uuid, size_t uuid_len)
59 {
60 	int r;
61 	size_t len = uuid_len;
62 
63 #if defined(HW_UUID) /* OpenBSD */
64 	int mib[] = { CTL_HW, HW_UUID };
65 
66 	r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
67 #elif defined(KERN_HOSTUUID) /* FreeBSD */
68 	int mib[] = { CTL_KERN, KERN_HOSTUUID };
69 
70 	r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
71 #elif defined(__NetBSD__)
72 	r = sysctlbyname("machdep.dmi.system-uuid", uuid, &len, NULL, 0);
73 #elif defined(__linux__)
74 	FILE *fp;
75 
76 	fp = fopen("/sys/class/dmi/id/product_uuid", "r");
77 	if (fp == NULL)
78 		return 0;
79 	if (fgets(uuid, (int)uuid_len, fp) == NULL) {
80 		fclose(fp);
81 		return 0;
82 	}
83 	len = strlen(uuid) + 1;
84 	fclose(fp);
85 	r = len == 1 ? -1 : 0;
86 #else
87 	UNUSED(uuid);
88 	r = -1;
89 	errno = ENOSYS;
90 #endif
91 
92 	if (r == -1)
93 		return 0;
94 	return len;
95 }
96 
97 static size_t
98 duid_make_uuid(uint8_t *d)
99 {
100 	uint16_t type = htons(DUID_UUID);
101 	char uuid[UUID_LEN + 1];
102 	size_t l;
103 
104 	if (duid_machineuuid(uuid, sizeof(uuid)) != sizeof(uuid))
105 		return 0;
106 
107 	/* All zeros UUID is not valid */
108 	if (strcmp("00000000-0000-0000-0000-000000000000", uuid) == 0)
109 		return 0;
110 
111 	memcpy(d, &type, sizeof(type));
112 	l = sizeof(type);
113 	d += sizeof(type);
114 	l += hwaddr_aton(d, uuid);
115 	return l;
116 }
117 
118 size_t
119 duid_make(void *d, const struct interface *ifp, uint16_t type)
120 {
121 	uint8_t *p;
122 	uint16_t u16;
123 	time_t t;
124 	uint32_t u32;
125 
126 	if (ifp->hwlen == 0)
127 		return 0;
128 
129 	p = d;
130 	u16 = htons(type);
131 	memcpy(p, &u16, sizeof(u16));
132 	p += sizeof(u16);
133 	u16 = htons(ifp->family);
134 	memcpy(p, &u16, sizeof(u16));
135 	p += sizeof(u16);
136 	if (type == DUID_LLT) {
137 		/* time returns seconds from jan 1 1970, but DUID-LLT is
138 		 * seconds from jan 1 2000 modulo 2^32 */
139 		t = time(NULL) - DUID_TIME_EPOCH;
140 		u32 = htonl((uint32_t)t & 0xffffffff);
141 		memcpy(p, &u32, sizeof(u32));
142 		p += sizeof(u32);
143 	}
144 	/* Finally, add the MAC address of the interface */
145 	memcpy(p, ifp->hwaddr, ifp->hwlen);
146 	p += ifp->hwlen;
147 	return (size_t)(p - (uint8_t *)d);
148 }
149 
150 #define DUID_STRLEN DUID_LEN * 3
151 static size_t
152 duid_get(uint8_t **d, const struct interface *ifp)
153 {
154 	FILE *fp;
155 	uint8_t *data;
156 	size_t len;
157 	int x = 0;
158 	char line[DUID_STRLEN];
159 	const struct interface *ifp2;
160 
161 	/* If we already have a DUID then use it as it's never supposed
162 	 * to change once we have one even if the interfaces do */
163 	if ((len = read_hwaddr_aton(&data, DUID)) != 0) {
164 		if (len <= DUID_LEN) {
165 			*d = data;
166 			return len;
167 		}
168 		logerrx("DUID too big (max %u): %s", DUID_LEN, DUID);
169 		/* Keep the buffer, will assign below. */
170 	} else {
171 		if (errno != ENOENT)
172 			logerr("%s", DUID);
173 		if ((data = malloc(DUID_LEN)) == NULL) {
174 			logerr(__func__);
175 			return 0;
176 		}
177 	}
178 
179 	/* Regardless of what happens we will create a DUID to use. */
180 	*d = data;
181 
182 	/* No file? OK, lets make one based the machines UUID */
183 	len = duid_make_uuid(data);
184 	if (len > 0)
185 		return len;
186 
187 	/* No UUID? OK, lets make one based on our interface */
188 	if (ifp->hwlen == 0) {
189 		logwarnx("%s: does not have hardware address", ifp->name);
190 		TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
191 			if (ifp2->hwlen != 0)
192 				break;
193 		}
194 		if (ifp2) {
195 			ifp = ifp2;
196 			logwarnx("picked interface %s to generate a DUID",
197 			    ifp->name);
198 		} else {
199 			logwarnx("no interfaces have a fixed hardware "
200 			    "address");
201 			return duid_make(data, ifp, DUID_LL);
202 		}
203 	}
204 
205 	if (!(fp = fopen(DUID, "w"))) {
206 		logerr("%s", DUID);
207 		return duid_make(data, ifp, DUID_LL);
208 	}
209 	len = duid_make(data, ifp, DUID_LLT);
210 	x = fprintf(fp, "%s\n", hwaddr_ntoa(data, len, line, sizeof(line)));
211 	if (fclose(fp) == EOF)
212 		x = -1;
213 	/* Failed to write the duid? scrub it, we cannot use it */
214 	if (x < 1) {
215 		logerr("%s", DUID);
216 		unlink(DUID);
217 		return duid_make(data, ifp, DUID_LL);
218 	}
219 	return len;
220 }
221 
222 size_t duid_init(const struct interface *ifp)
223 {
224 
225 	if (ifp->ctx->duid == NULL)
226 		ifp->ctx->duid_len = duid_get(&ifp->ctx->duid, ifp);
227 	return ifp->ctx->duid_len;
228 }
229