kern_uuid.c revision 116182
1218065Sadrian/*
2218065Sadrian * Copyright (c) 2002 Marcel Moolenaar
3218065Sadrian * All rights reserved.
4218065Sadrian *
5218065Sadrian * Redistribution and use in source and binary forms, with or without
6218065Sadrian * modification, are permitted provided that the following conditions
7218065Sadrian * are met:
8218065Sadrian *
9218065Sadrian * 1. Redistributions of source code must retain the above copyright
10218065Sadrian *    notice, this list of conditions and the following disclaimer.
11218065Sadrian * 2. Redistributions in binary form must reproduce the above copyright
12218065Sadrian *    notice, this list of conditions and the following disclaimer in the
13218065Sadrian *    documentation and/or other materials provided with the distribution.
14218065Sadrian *
15218065Sadrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16218065Sadrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17218065Sadrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18218065Sadrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19218065Sadrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20218065Sadrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21218065Sadrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22218065Sadrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23218065Sadrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24218065Sadrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25218065Sadrian */
26218065Sadrian
27218065Sadrian#include <sys/cdefs.h>
28218065Sadrian__FBSDID("$FreeBSD: head/sys/kern/kern_uuid.c 116182 2003-06-11 00:56:59Z obrien $");
29218065Sadrian
30218065Sadrian#include <sys/param.h>
31218065Sadrian#include <sys/endian.h>
32218065Sadrian#include <sys/kernel.h>
33218065Sadrian#include <sys/lock.h>
34218065Sadrian#include <sys/mutex.h>
35218065Sadrian#include <sys/sbuf.h>
36218065Sadrian#include <sys/socket.h>
37218065Sadrian#include <sys/sysproto.h>
38218065Sadrian#include <sys/systm.h>
39218065Sadrian#include <sys/uuid.h>
40218065Sadrian
41218065Sadrian#include <net/if.h>
42238284Sadrian#include <net/if_dl.h>
43238284Sadrian#include <net/if_types.h>
44238284Sadrian
45238284Sadrian/*
46218065Sadrian * See also:
47218065Sadrian *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
48237000Sadrian *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
49237000Sadrian *
50237000Sadrian * Note that the generator state is itself an UUID, but the time and clock
51237000Sadrian * sequence fields are written in the native byte order.
52227359Sadrian */
53248988Sadrian
54237000SadrianCTASSERT(sizeof(struct uuid) == 16);
55227361Sadrian
56236993Sadrian/* We use an alternative, more convenient representation in the generator. */
57236993Sadrianstruct uuid_private {
58218065Sadrian	union {
59287197Sglebius		uint64_t	ll;		/* internal. */
60227361Sadrian		struct {
61227361Sadrian			uint32_t	low;
62227364Sadrian			uint16_t	mid;
63227364Sadrian			uint16_t	hi;
64227364Sadrian		} x;
65219180Sadrian	} time;
66248671Sadrian	uint16_t	seq;			/* Big-endian. */
67248671Sadrian	uint16_t	node[UUID_NODE_LEN>>1];
68248671Sadrian};
69227361Sadrian
70227361SadrianCTASSERT(sizeof(struct uuid_private) == 16);
71248745Sadrian
72248745Sadrianstatic struct uuid_private uuid_last;
73227361Sadrian
74235680Sadrianstatic struct mtx uuid_mutex;
75235680SadrianMTX_SYSINIT(uuid_lock, &uuid_mutex, "UUID generator mutex lock", MTX_DEF);
76235676Sadrian
77235676Sadrian/*
78235676Sadrian * Return the first MAC address we encounter or, if none was found,
79235676Sadrian * construct a sufficiently random multicast address. We don't try
80235680Sadrian * to return the same MAC address as previously returned. We always
81235680Sadrian * generate a new multicast address if no MAC address exists in the
82238931Sadrian * system.
83239204Sadrian * It would be nice to know if 'ifnet' or any of its sub-structures
84239205Sadrian * has been changed in any way. If not, we could simply skip the
85239205Sadrian * scan and safely return the MAC address we returned before.
86239204Sadrian */
87239204Sadrianstatic void
88238931Sadrianuuid_node(uint16_t *node)
89239262Sadrian{
90239262Sadrian	struct ifnet *ifp;
91239262Sadrian	struct ifaddr *ifa;
92239262Sadrian	struct sockaddr_dl *sdl;
93239262Sadrian	int i;
94242271Sadrian
95242271Sadrian	IFNET_RLOCK();
96242271Sadrian	TAILQ_FOREACH(ifp, &ifnet, if_link) {
97235676Sadrian		/* Walk the address list */
98235676Sadrian		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
99235676Sadrian			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
100235676Sadrian			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
101235676Sadrian			    sdl->sdl_type == IFT_ETHER) {
102235676Sadrian				/* Got a MAC address. */
103235676Sadrian				bcopy(LLADDR(sdl), node, UUID_NODE_LEN);
104235676Sadrian				IFNET_RUNLOCK();
105241559Sadrian				return;
106235676Sadrian			}
107250783Sadrian		}
108250783Sadrian	}
109246450Sadrian	IFNET_RUNLOCK();
110265115Sadrian
111265115Sadrian	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
112265115Sadrian		node[i] = (uint16_t)arc4random();
113265205Sadrian	*((uint8_t*)node) |= 0x80;
114265115Sadrian}
115265115Sadrian
116265115Sadrian/*
117265115Sadrian * Get the current time as a 60 bit count of 100-nanosecond intervals
118265205Sadrian * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
119265115Sadrian * the Unix time since 00:00:00.00, Januari 1, 1970 to the date of the
120265115Sadrian * Gregorian reform to the Christian calendar.
121265115Sadrian */
122265115Sadrianstatic uint64_t
123246450Sadrianuuid_time(void)
124246450Sadrian{
125236597Sadrian	struct bintime bt;
126236597Sadrian	uint64_t time = 0x01B21DD213814000LL;
127236597Sadrian
128235680Sadrian	bintime(&bt);
129251014Sadrian	time += (uint64_t)bt.sec * 10000000LL;
130236597Sadrian	time += (10000000LL * (uint32_t)(bt.frac >> 32)) >> 32;
131236597Sadrian	return (time & ((1LL << 60) - 1LL));
132246450Sadrian}
133246450Sadrian
134246450Sadrian#ifndef _SYS_SYSPROTO_H_
135246450Sadrianstruct uuidgen_args {
136246450Sadrian	struct uuid *store;
137246450Sadrian	int	count;
138246450Sadrian};
139246652Sadrian#endif
140246450Sadrian
141246450Sadrianint
142218065Sadrianuuidgen(struct thread *td, struct uuidgen_args *uap)
143{
144	struct uuid_private uuid;
145	uint64_t time;
146	int error;
147
148	/*
149	 * Limit the number of UUIDs that can be created at the same time
150	 * to some arbitrary number. This isn't really necessary, but I
151	 * like to have some sort of upper-bound that's less than 2G :-)
152	 * XXX needs to be tunable.
153	 */
154	if (uap->count < 1 || uap->count > 2048)
155		return (EINVAL);
156
157	/* XXX: pre-validate accessibility to the whole of the UUID store? */
158
159	mtx_lock(&uuid_mutex);
160
161	uuid_node(uuid.node);
162	time = uuid_time();
163
164	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid.node[0] ||
165	    uuid_last.node[1] != uuid.node[1] ||
166	    uuid_last.node[2] != uuid.node[2])
167		uuid.seq = (uint16_t)arc4random() & 0x3fff;
168	else if (uuid_last.time.ll >= time)
169		uuid.seq = (uuid_last.seq + 1) & 0x3fff;
170	else
171		uuid.seq = uuid_last.seq;
172
173	uuid_last = uuid;
174	uuid_last.time.ll = (time + uap->count - 1) & ((1LL << 60) - 1LL);
175
176	mtx_unlock(&uuid_mutex);
177
178	/* Set sequence and variant and deal with byte order. */
179	uuid.seq = htobe16(uuid.seq | 0x8000);
180
181	/* XXX: this should copyout larger chunks at a time. */
182	do {
183		/* Set time and version (=1) and deal with byte order. */
184		uuid.time.x.low = (uint32_t)time;
185		uuid.time.x.mid = (uint16_t)(time >> 32);
186		uuid.time.x.hi = ((uint16_t)(time >> 48) & 0xfff) | (1 << 12);
187		error = copyout(&uuid, uap->store, sizeof(uuid));
188		uap->store++;
189		uap->count--;
190		time++;
191	} while (uap->count > 0 && !error);
192
193	return (error);
194}
195
196int
197snprintf_uuid(char *buf, size_t sz, struct uuid *uuid)
198{
199	struct uuid_private *id;
200	int cnt;
201
202	id = (struct uuid_private *)uuid;
203	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
204	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
205	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
206	return (cnt);
207}
208
209int
210printf_uuid(struct uuid *uuid)
211{
212	char buf[38];
213
214	snprintf_uuid(buf, sizeof(buf), uuid);
215	return (printf("%s", buf));
216}
217
218int
219sbuf_printf_uuid(struct sbuf *sb, struct uuid *uuid)
220{
221	char buf[38];
222
223	snprintf_uuid(buf, sizeof(buf), uuid);
224	return (sbuf_printf(sb, "%s", buf));
225}
226
227/*
228 * Encode/Decode UUID into byte-stream.
229 *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
230 *
231 * 0                   1                   2                   3
232 *   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
233 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234 *  |                          time_low                             |
235 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 *  |       time_mid                |         time_hi_and_version   |
237 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
239 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 *  |                         node (2-5)                            |
241 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242 */
243
244void
245le_uuid_enc(void *buf, struct uuid const *uuid)
246{
247	u_char *p;
248	int i;
249
250	p = buf;
251	le32enc(p, uuid->time_low);
252	le16enc(p + 4, uuid->time_mid);
253	le16enc(p + 6, uuid->time_hi_and_version);
254	p[8] = uuid->clock_seq_hi_and_reserved;
255	p[9] = uuid->clock_seq_low;
256	for (i = 0; i < _UUID_NODE_LEN; i++)
257		p[10 + i] = uuid->node[i];
258}
259
260void
261le_uuid_dec(void const *buf, struct uuid *uuid)
262{
263	u_char const *p;
264	int i;
265
266	p = buf;
267	uuid->time_low = le32dec(p);
268	uuid->time_mid = le16dec(p + 4);
269	uuid->time_hi_and_version = le16dec(p + 6);
270	uuid->clock_seq_hi_and_reserved = p[8];
271	uuid->clock_seq_low = p[9];
272	for (i = 0; i < _UUID_NODE_LEN; i++)
273		uuid->node[i] = p[10 + i];
274}
275void
276be_uuid_enc(void *buf, struct uuid const *uuid)
277{
278	u_char *p;
279	int i;
280
281	p = buf;
282	be32enc(p, uuid->time_low);
283	be16enc(p + 4, uuid->time_mid);
284	be16enc(p + 6, uuid->time_hi_and_version);
285	p[8] = uuid->clock_seq_hi_and_reserved;
286	p[9] = uuid->clock_seq_low;
287	for (i = 0; i < _UUID_NODE_LEN; i++)
288		p[10 + i] = uuid->node[i];
289}
290
291void
292be_uuid_dec(void const *buf, struct uuid *uuid)
293{
294	u_char const *p;
295	int i;
296
297	p = buf;
298	uuid->time_low = be32dec(p);
299	uuid->time_mid = le16dec(p + 4);
300	uuid->time_hi_and_version = be16dec(p + 6);
301	uuid->clock_seq_hi_and_reserved = p[8];
302	uuid->clock_seq_low = p[9];
303	for (i = 0; i < _UUID_NODE_LEN; i++)
304		uuid->node[i] = p[10 + i];
305}
306