xref: /dragonfly/contrib/dhcpcd/src/duid.c (revision 6e5c5008)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2019 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 #define	DUID_LLT	1
32 #define	DUID_LL		3
33 #define	DUID_UUID	4
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #ifdef BSD
39 #  include <sys/sysctl.h>
40 #endif
41 
42 #include <arpa/inet.h>
43 
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 
47 #include <errno.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 #include "common.h"
56 #include "dhcpcd.h"
57 #include "duid.h"
58 #include "logerr.h"
59 
60 static size_t
61 duid_machineuuid(char *uuid, size_t uuid_len)
62 {
63 	int r;
64 	size_t len = uuid_len;
65 
66 #if defined(HW_UUID) /* OpenBSD */
67 	int mib[] = { CTL_HW, HW_UUID };
68 
69 	r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
70 #elif defined(KERN_HOSTUUID) /* FreeBSD */
71 	int mib[] = { CTL_KERN, KERN_HOSTUUID };
72 
73 	r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
74 #elif defined(__NetBSD__)
75 	r = sysctlbyname("machdep.dmi.system-uuid", uuid, &len, NULL, 0);
76 #elif defined(__linux__)
77 	FILE *fp;
78 
79 	fp = fopen("/sys/class/dmi/id/product_uuid", "r");
80 	if (fp == NULL)
81 		return 0;
82 	if (fgets(uuid, (int)uuid_len, fp) == NULL) {
83 		fclose(fp);
84 		return 0;
85 	}
86 	len = strlen(uuid) + 1;
87 	fclose(fp);
88 	r = len == 1 ? -1 : 0;
89 #else
90 	UNUSED(uuid);
91 	r = -1;
92 	errno = ENOSYS;
93 #endif
94 
95 	if (r == -1)
96 		return 0;
97 	return len;
98 }
99 
100 static size_t
101 duid_make_uuid(uint8_t *d)
102 {
103 	uint16_t type = htons(DUID_UUID);
104 	char uuid[UUID_LEN + 1];
105 	size_t l;
106 
107 	if (duid_machineuuid(uuid, sizeof(uuid)) != sizeof(uuid))
108 		return 0;
109 
110 	/* All zeros UUID is not valid */
111 	if (strcmp("00000000-0000-0000-0000-000000000000", uuid) == 0)
112 		return 0;
113 
114 	memcpy(d, &type, sizeof(type));
115 	l = sizeof(type);
116 	d += sizeof(type);
117 	l += hwaddr_aton(d, uuid);
118 	return l;
119 }
120 
121 static size_t
122 duid_make(uint8_t *d, const struct interface *ifp, uint16_t type)
123 {
124 	uint8_t *p;
125 	uint16_t u16;
126 	time_t t;
127 	uint32_t u32;
128 
129 	p = d;
130 	u16 = htons(type);
131 	memcpy(p, &u16, 2);
132 	p += 2;
133 	u16 = htons(ifp->family);
134 	memcpy(p, &u16, 2);
135 	p += 2;
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, 4);
142 		p += 4;
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 - 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