kern_uuid.c revision 97372
1/*
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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 ``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 * $FreeBSD: head/sys/kern/kern_uuid.c 97372 2002-05-28 06:16:08Z marcel $
27 */
28
29#include <sys/param.h>
30#include <sys/endian.h>
31#include <sys/kernel.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/sbuf.h>
35#include <sys/socket.h>
36#include <sys/sysproto.h>
37#include <sys/uuid.h>
38
39#include <net/if.h>
40#include <net/if_dl.h>
41#include <net/if_types.h>
42
43/*
44 * See also:
45 *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
46 *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
47 *
48 * Note that the generator state is itself an UUID, but the time and clock
49 * sequence fields are written in the native byte order.
50 */
51
52CTASSERT(sizeof(struct uuid) == 16);
53
54/* We use an alternative, more convenient representation in the generator. */
55struct uuid_private {
56	union {
57		uint64_t	ll;		/* internal. */
58		struct {
59			uint32_t	low;
60			uint16_t	mid;
61			uint16_t	hi;
62		} x;
63	} time;
64	uint16_t	seq;			/* Big-endian. */
65	uint16_t	node[UUID_NODE_LEN>>1];
66};
67
68CTASSERT(sizeof(struct uuid_private) == 16);
69
70static struct uuid_private uuid_last;
71
72static struct mtx uuid_mutex;
73MTX_SYSINIT(uuid_lock, &uuid_mutex, "UUID generator mutex lock", MTX_DEF);
74
75/*
76 * Return the first MAC address we encounter or, if none was found,
77 * construct a sufficiently random multicast address. We don't try
78 * to return the same MAC address as previously returned. We always
79 * generate a new multicast address if no MAC address exists in the
80 * system.
81 * It would be nice to know if 'ifnet' or any of its sub-structures
82 * has been changed in any way. If not, we could simply skip the
83 * scan and safely return the MAC address we returned before.
84 */
85static void
86uuid_node(uint16_t *node)
87{
88	struct ifnet *ifp;
89	struct ifaddr *ifa;
90	struct sockaddr_dl *sdl;
91	int i;
92
93	/* XXX: lock ifnet. */
94	TAILQ_FOREACH(ifp, &ifnet, if_link) {
95		/* Walk the address list */
96		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
97			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
98			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
99			    sdl->sdl_type == IFT_ETHER) {
100				/* Got a MAC address. */
101				bcopy(LLADDR(sdl), node, UUID_NODE_LEN);
102				/* XXX: unlock ifnet. */
103				return;
104			}
105		}
106	}
107	/* XXX: unlock ifnet. */
108
109	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
110		node[i] = (uint16_t)arc4random();
111	*((uint8_t*)node) |= 0x80;
112}
113
114/*
115 * Get the current time as a 60 bit count of 100-nanosecond intervals
116 * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
117 * the Unix time since 00:00:00.00, Januari 1, 1970 to the date of the
118 * Gregorian reform to the Christian calendar.
119 */
120static uint64_t
121uuid_time(void)
122{
123	struct bintime bt;
124	uint64_t time = 0x01B21DD213814000LL;
125
126	bintime(&bt);
127	time += (uint64_t)bt.sec * 10000000LL;
128	time += (10000000LL * (uint32_t)(bt.frac >> 32)) >> 32;
129	return (time & ((1LL << 60) - 1LL));
130}
131
132#ifndef _SYS_SYSPROTO_H_
133struct uuidgen_args {
134	struct uuid *store;
135	int	count;
136};
137#endif
138
139int uuidgen(struct thread *td, struct uuidgen_args *uap)
140{
141	struct uuid_private uuid;
142	uint64_t time;
143	int error;
144
145	/*
146	 * Limit the number of UUIDs that can be created at the same time
147	 * to some arbitrary number. This isn't really necessary, but I
148	 * like to have some sort of upper-bound that's less than 2G :-)
149	 * XXX needs to be tunable.
150	 */
151	if (uap->count < 1 || uap->count > 2048)
152		return (EINVAL);
153
154	/* XXX: pre-validate accessibility to the whole of the UUID store? */
155
156	mtx_lock(&uuid_mutex);
157
158	uuid_node(uuid.node);
159	time = uuid_time();
160
161	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid.node[0] ||
162	    uuid_last.node[1] != uuid.node[1] ||
163	    uuid_last.node[2] != uuid.node[2])
164		uuid.seq = (uint16_t)arc4random() & 0x3fff;
165	else if (uuid_last.time.ll >= time)
166		uuid.seq = (uuid_last.seq + 1) & 0x3fff;
167	else
168		uuid.seq = uuid_last.seq;
169
170	uuid_last = uuid;
171	uuid_last.time.ll = (time + uap->count - 1) & ((1LL << 60) - 1LL);
172
173	mtx_unlock(&uuid_mutex);
174
175	/* Set sequence and variant and deal with byte order. */
176	uuid.seq = htobe16(uuid.seq | 0x8000);
177
178	/* XXX: this should copyout larger chunks at a time. */
179	do {
180		/* Set time and version (=1) and deal with byte order. */
181		uuid.time.x.low = (uint32_t)time;
182		uuid.time.x.mid = (uint16_t)(time >> 32);
183		uuid.time.x.hi = ((uint16_t)(time >> 48) & 0xfff) | (1 << 12);
184		error = copyout(&uuid, uap->store, sizeof(uuid));
185		uap->store++;
186		uap->count--;
187		time++;
188	} while (uap->count > 0 && !error);
189
190	return (error);
191}
192
193int
194snprintf_uuid(char *buf, size_t sz, struct uuid *uuid)
195{
196	struct uuid_private *id;
197	int cnt;
198
199	id = (struct uuid_private *)uuid;
200	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
201	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
202	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
203	return (cnt);
204}
205
206int
207printf_uuid(struct uuid *uuid)
208{
209	char buf[38];
210
211	snprintf_uuid(buf, sizeof(buf), uuid);
212	return (printf("%s", buf));
213}
214
215int
216sbuf_printf_uuid(struct sbuf *sb, struct uuid *uuid)
217{
218	char buf[38];
219
220	snprintf_uuid(buf, sizeof(buf), uuid);
221	return (sbuf_printf(sb, "%s", buf));
222}
223