kern_uuid.c revision 181803
1140089Sdas/*-
2140089Sdas * Copyright (c) 2002 Marcel Moolenaar
3140089Sdas * All rights reserved.
4140089Sdas *
5140089Sdas * Redistribution and use in source and binary forms, with or without
6140089Sdas * modification, are permitted provided that the following conditions
7140089Sdas * are met:
8140089Sdas *
9140089Sdas * 1. Redistributions of source code must retain the above copyright
10140089Sdas *    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 ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_uuid.c 181803 2008-08-17 23:27:27Z bz $");
29
30#include <sys/param.h>
31#include <sys/endian.h>
32#include <sys/kernel.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/sbuf.h>
36#include <sys/socket.h>
37#include <sys/sysproto.h>
38#include <sys/systm.h>
39#include <sys/uuid.h>
40#include <sys/vimage.h>
41
42#include <net/if.h>
43#include <net/if_dl.h>
44#include <net/if_types.h>
45
46/*
47 * See also:
48 *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
49 *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
50 *
51 * Note that the generator state is itself an UUID, but the time and clock
52 * sequence fields are written in the native byte order.
53 */
54
55CTASSERT(sizeof(struct uuid) == 16);
56
57/* We use an alternative, more convenient representation in the generator. */
58struct uuid_private {
59	union {
60		uint64_t	ll;		/* internal. */
61		struct {
62			uint32_t	low;
63			uint16_t	mid;
64			uint16_t	hi;
65		} x;
66	} time;
67	uint16_t	seq;			/* Big-endian. */
68	uint16_t	node[UUID_NODE_LEN>>1];
69};
70
71CTASSERT(sizeof(struct uuid_private) == 16);
72
73static struct uuid_private uuid_last;
74
75static struct mtx uuid_mutex;
76MTX_SYSINIT(uuid_lock, &uuid_mutex, "UUID generator mutex lock", MTX_DEF);
77
78/*
79 * Return the first MAC address we encounter or, if none was found,
80 * construct a sufficiently random multicast address. We don't try
81 * to return the same MAC address as previously returned. We always
82 * generate a new multicast address if no MAC address exists in the
83 * system.
84 * It would be nice to know if 'ifnet' or any of its sub-structures
85 * has been changed in any way. If not, we could simply skip the
86 * scan and safely return the MAC address we returned before.
87 */
88static void
89uuid_node(uint16_t *node)
90{
91	struct ifnet *ifp;
92	struct ifaddr *ifa;
93	struct sockaddr_dl *sdl;
94	int i;
95
96	IFNET_RLOCK();
97	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
98		/* Walk the address list */
99		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
100			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
101			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
102			    sdl->sdl_type == IFT_ETHER) {
103				/* Got a MAC address. */
104				bcopy(LLADDR(sdl), node, UUID_NODE_LEN);
105				IFNET_RUNLOCK();
106				return;
107			}
108		}
109	}
110	IFNET_RUNLOCK();
111
112	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
113		node[i] = (uint16_t)arc4random();
114	*((uint8_t*)node) |= 0x01;
115}
116
117/*
118 * Get the current time as a 60 bit count of 100-nanosecond intervals
119 * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
120 * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
121 * Gregorian reform to the Christian calendar.
122 */
123static uint64_t
124uuid_time(void)
125{
126	struct bintime bt;
127	uint64_t time = 0x01B21DD213814000LL;
128
129	bintime(&bt);
130	time += (uint64_t)bt.sec * 10000000LL;
131	time += (10000000LL * (uint32_t)(bt.frac >> 32)) >> 32;
132	return (time & ((1LL << 60) - 1LL));
133}
134
135struct uuid *
136kern_uuidgen(struct uuid *store, size_t count)
137{
138	struct uuid_private uuid;
139	uint64_t time;
140	size_t n;
141
142	mtx_lock(&uuid_mutex);
143
144	uuid_node(uuid.node);
145	time = uuid_time();
146
147	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid.node[0] ||
148	    uuid_last.node[1] != uuid.node[1] ||
149	    uuid_last.node[2] != uuid.node[2])
150		uuid.seq = (uint16_t)arc4random() & 0x3fff;
151	else if (uuid_last.time.ll >= time)
152		uuid.seq = (uuid_last.seq + 1) & 0x3fff;
153	else
154		uuid.seq = uuid_last.seq;
155
156	uuid_last = uuid;
157	uuid_last.time.ll = (time + count - 1) & ((1LL << 60) - 1LL);
158
159	mtx_unlock(&uuid_mutex);
160
161	/* Set sequence and variant and deal with byte order. */
162	uuid.seq = htobe16(uuid.seq | 0x8000);
163
164	for (n = 0; n < count; n++) {
165		/* Set time and version (=1). */
166		uuid.time.x.low = (uint32_t)time;
167		uuid.time.x.mid = (uint16_t)(time >> 32);
168		uuid.time.x.hi = ((uint16_t)(time >> 48) & 0xfff) | (1 << 12);
169		store[n] = *(struct uuid *)&uuid;
170		time++;
171	}
172
173	return (store);
174}
175
176#ifndef _SYS_SYSPROTO_H_
177struct uuidgen_args {
178	struct uuid *store;
179	int	count;
180};
181#endif
182int
183uuidgen(struct thread *td, struct uuidgen_args *uap)
184{
185	struct uuid *store;
186	size_t count;
187	int error;
188
189	/*
190	 * Limit the number of UUIDs that can be created at the same time
191	 * to some arbitrary number. This isn't really necessary, but I
192	 * like to have some sort of upper-bound that's less than 2G :-)
193	 * XXX probably needs to be tunable.
194	 */
195	if (uap->count < 1 || uap->count > 2048)
196		return (EINVAL);
197
198	count = uap->count;
199	store = malloc(count * sizeof(struct uuid), M_TEMP, M_WAITOK);
200	kern_uuidgen(store, count);
201	error = copyout(store, uap->store, count * sizeof(struct uuid));
202	free(store, M_TEMP);
203	return (error);
204}
205
206int
207snprintf_uuid(char *buf, size_t sz, struct uuid *uuid)
208{
209	struct uuid_private *id;
210	int cnt;
211
212	id = (struct uuid_private *)uuid;
213	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
214	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
215	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
216	return (cnt);
217}
218
219int
220printf_uuid(struct uuid *uuid)
221{
222	char buf[38];
223
224	snprintf_uuid(buf, sizeof(buf), uuid);
225	return (printf("%s", buf));
226}
227
228int
229sbuf_printf_uuid(struct sbuf *sb, struct uuid *uuid)
230{
231	char buf[38];
232
233	snprintf_uuid(buf, sizeof(buf), uuid);
234	return (sbuf_printf(sb, "%s", buf));
235}
236
237/*
238 * Encode/Decode UUID into byte-stream.
239 *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
240 *
241 * 0                   1                   2                   3
242 *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
243 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
244 *  |                          time_low                             |
245 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
246 *  |       time_mid                |         time_hi_and_version   |
247 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
248 *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
249 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250 *  |                         node (2-5)                            |
251 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252 */
253
254void
255le_uuid_enc(void *buf, struct uuid const *uuid)
256{
257	u_char *p;
258	int i;
259
260	p = buf;
261	le32enc(p, uuid->time_low);
262	le16enc(p + 4, uuid->time_mid);
263	le16enc(p + 6, uuid->time_hi_and_version);
264	p[8] = uuid->clock_seq_hi_and_reserved;
265	p[9] = uuid->clock_seq_low;
266	for (i = 0; i < _UUID_NODE_LEN; i++)
267		p[10 + i] = uuid->node[i];
268}
269
270void
271le_uuid_dec(void const *buf, struct uuid *uuid)
272{
273	u_char const *p;
274	int i;
275
276	p = buf;
277	uuid->time_low = le32dec(p);
278	uuid->time_mid = le16dec(p + 4);
279	uuid->time_hi_and_version = le16dec(p + 6);
280	uuid->clock_seq_hi_and_reserved = p[8];
281	uuid->clock_seq_low = p[9];
282	for (i = 0; i < _UUID_NODE_LEN; i++)
283		uuid->node[i] = p[10 + i];
284}
285
286void
287be_uuid_enc(void *buf, struct uuid const *uuid)
288{
289	u_char *p;
290	int i;
291
292	p = buf;
293	be32enc(p, uuid->time_low);
294	be16enc(p + 4, uuid->time_mid);
295	be16enc(p + 6, uuid->time_hi_and_version);
296	p[8] = uuid->clock_seq_hi_and_reserved;
297	p[9] = uuid->clock_seq_low;
298	for (i = 0; i < _UUID_NODE_LEN; i++)
299		p[10 + i] = uuid->node[i];
300}
301
302void
303be_uuid_dec(void const *buf, struct uuid *uuid)
304{
305	u_char const *p;
306	int i;
307
308	p = buf;
309	uuid->time_low = be32dec(p);
310	uuid->time_mid = le16dec(p + 4);
311	uuid->time_hi_and_version = be16dec(p + 6);
312	uuid->clock_seq_hi_and_reserved = p[8];
313	uuid->clock_seq_low = p[9];
314	for (i = 0; i < _UUID_NODE_LEN; i++)
315		uuid->node[i] = p[10 + i];
316}
317
318int
319parse_uuid(const char *str, struct uuid *uuid)
320{
321	u_int c[11];
322	int n;
323
324	/* An empty string represents a nil UUID. */
325	if (*str == '\0') {
326		bzero(uuid, sizeof(*uuid));
327		return (0);
328	}
329
330	/* The UUID string representation has a fixed length. */
331	if (strlen(str) != 36)
332		return (EINVAL);
333
334	/*
335	 * We only work with "new" UUIDs. New UUIDs have the form:
336	 *      01234567-89ab-cdef-0123-456789abcdef
337	 * The so called "old" UUIDs, which we don't support, have the form:
338	 *      0123456789ab.cd.ef.01.23.45.67.89.ab
339	 */
340	if (str[8] != '-')
341		return (EINVAL);
342
343	n = sscanf(str, "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x", c + 0, c + 1,
344	    c + 2, c + 3, c + 4, c + 5, c + 6, c + 7, c + 8, c + 9, c + 10);
345	/* Make sure we have all conversions. */
346	if (n != 11)
347		return (EINVAL);
348
349	/* Successful scan. Build the UUID. */
350	uuid->time_low = c[0];
351	uuid->time_mid = c[1];
352	uuid->time_hi_and_version = c[2];
353	uuid->clock_seq_hi_and_reserved = c[3];
354	uuid->clock_seq_low = c[4];
355	for (n = 0; n < 6; n++)
356		uuid->node[n] = c[n + 5];
357
358	/* Check semantics... */
359	return (((c[3] & 0x80) != 0x00 &&		/* variant 0? */
360	    (c[3] & 0xc0) != 0x80 &&			/* variant 1? */
361	    (c[3] & 0xe0) != 0xc0) ? EINVAL : 0);	/* variant 2? */
362}
363