xref: /netbsd-src/sys/kern/kern_uuid.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: kern_uuid.c,v 1.16 2008/11/18 14:01:03 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Marcel Moolenaar
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12 13:34:11 rse Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: kern_uuid.c,v 1.16 2008/11/18 14:01:03 joerg Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/uuid.h>
41 
42 /* NetBSD */
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/syscallargs.h>
46 #include <sys/uio.h>
47 
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 
52 /*
53  * See also:
54  *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
55  *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
56  *
57  * Note that the generator state is itself an UUID, but the time and clock
58  * sequence fields are written in the native byte order.
59  */
60 
61 CTASSERT(sizeof(struct uuid) == 16);
62 
63 /* We use an alternative, more convenient representation in the generator. */
64 struct uuid_private {
65 	union {
66 		uint64_t	ll;		/* internal. */
67 		struct {
68 			uint32_t	low;
69 			uint16_t	mid;
70 			uint16_t	hi;
71 		} x;
72 	} time;
73 	uint16_t	seq;			/* Big-endian. */
74 	uint16_t	node[UUID_NODE_LEN>>1];
75 };
76 
77 CTASSERT(sizeof(struct uuid_private) == 16);
78 
79 static struct uuid_private uuid_last;
80 
81 /* "UUID generator mutex lock" */
82 static kmutex_t uuid_mutex;
83 
84 void
85 uuid_init(void)
86 {
87 
88 	mutex_init(&uuid_mutex, MUTEX_DEFAULT, IPL_NONE);
89 }
90 
91 /*
92  * Return the first MAC address we encounter or, if none was found,
93  * construct a sufficiently random multicast address. We don't try
94  * to return the same MAC address as previously returned. We always
95  * generate a new multicast address if no MAC address exists in the
96  * system.
97  * It would be nice to know if 'ifnet' or any of its sub-structures
98  * has been changed in any way. If not, we could simply skip the
99  * scan and safely return the MAC address we returned before.
100  */
101 static void
102 uuid_node(uint16_t *node)
103 {
104 	struct ifnet *ifp;
105 	struct ifaddr *ifa;
106 	struct sockaddr_dl *sdl;
107 	int i, s;
108 
109 	s = splnet();
110 	KERNEL_LOCK(1, NULL);
111 	IFNET_FOREACH(ifp) {
112 		/* Walk the address list */
113 		IFADDR_FOREACH(ifa, ifp) {
114 			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
115 			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
116 			    sdl->sdl_type == IFT_ETHER) {
117 				/* Got a MAC address. */
118 				memcpy(node, CLLADDR(sdl), UUID_NODE_LEN);
119 				KERNEL_UNLOCK_ONE(NULL);
120 				splx(s);
121 				return;
122 			}
123 		}
124 	}
125 	KERNEL_UNLOCK_ONE(NULL);
126 	splx(s);
127 
128 	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
129 		node[i] = (uint16_t)arc4random();
130 	*((uint8_t*)node) |= 0x01;
131 }
132 
133 /*
134  * Get the current time as a 60 bit count of 100-nanosecond intervals
135  * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
136  * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
137  * Gregorian reform to the Christian calendar.
138  */
139 /*
140  * At present, NetBSD has no timespec source, only timeval sources.  So,
141  * we use timeval.
142  */
143 static uint64_t
144 uuid_time(void)
145 {
146 	struct timeval tv;
147 	uint64_t xtime = 0x01B21DD213814000LL;
148 
149 	microtime(&tv);
150 	xtime += (uint64_t)tv.tv_sec * 10000000LL;
151 	xtime += (uint64_t)(10 * tv.tv_usec);
152 	return (xtime & ((1LL << 60) - 1LL));
153 }
154 
155 /*
156  * Internal routine to actually generate the UUID.
157  */
158 static void
159 uuid_generate(struct uuid_private *uuid, uint64_t *timep, int count)
160 {
161 	uint64_t xtime;
162 
163 	mutex_enter(&uuid_mutex);
164 
165 	uuid_node(uuid->node);
166 	xtime = uuid_time();
167 	*timep = xtime;
168 
169 	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid->node[0] ||
170 	    uuid_last.node[1] != uuid->node[1] ||
171 	    uuid_last.node[2] != uuid->node[2])
172 		uuid->seq = (uint16_t)arc4random() & 0x3fff;
173 	else if (uuid_last.time.ll >= xtime)
174 		uuid->seq = (uuid_last.seq + 1) & 0x3fff;
175 	else
176 		uuid->seq = uuid_last.seq;
177 
178 	uuid_last = *uuid;
179 	uuid_last.time.ll = (xtime + count - 1) & ((1LL << 60) - 1LL);
180 
181 	mutex_exit(&uuid_mutex);
182 }
183 
184 static int
185 kern_uuidgen(struct uuid *store, int count, bool to_user)
186 {
187 	struct uuid_private uuid;
188 	uint64_t xtime;
189 	int error = 0, i;
190 
191 	KASSERT(count >= 1);
192 
193 	/* Generate the base UUID. */
194 	uuid_generate(&uuid, &xtime, count);
195 
196 	/* Set sequence and variant and deal with byte order. */
197 	uuid.seq = htobe16(uuid.seq | 0x8000);
198 
199 	for (i = 0; i < count; xtime++, i++) {
200 		/* Set time and version (=1) and deal with byte order. */
201 		uuid.time.x.low = (uint32_t)xtime;
202 		uuid.time.x.mid = (uint16_t)(xtime >> 32);
203 		uuid.time.x.hi = ((uint16_t)(xtime >> 48) & 0xfff) | (1 << 12);
204 		if (to_user) {
205 			error = copyout(&uuid, store + i, sizeof(uuid));
206 			if (error != 0)
207 				break;
208 		} else {
209 			memcpy(store + i, &uuid, sizeof(uuid));
210 		}
211 	}
212 
213 	return error;
214 }
215 
216 int
217 sys_uuidgen(struct lwp *l, const struct sys_uuidgen_args *uap, register_t *retval)
218 {
219 	/*
220 	 * Limit the number of UUIDs that can be created at the same time
221 	 * to some arbitrary number. This isn't really necessary, but I
222 	 * like to have some sort of upper-bound that's less than 2G :-)
223 	 * XXX needs to be tunable.
224 	 */
225 	if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
226 		return (EINVAL);
227 
228 	return kern_uuidgen(SCARG(uap, store), SCARG(uap,count), true);
229 }
230 
231 int
232 uuidgen(struct uuid *store, int count)
233 {
234 	return kern_uuidgen(store,count, false);
235 }
236 
237 int
238 uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
239 {
240 	const struct uuid_private *id;
241 	int cnt;
242 
243 	id = (const struct uuid_private *)uuid;
244 	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
245 	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
246 	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
247 	return (cnt);
248 }
249 
250 int
251 uuid_printf(const struct uuid *uuid)
252 {
253 	char buf[UUID_STR_LEN];
254 
255 	(void) uuid_snprintf(buf, sizeof(buf), uuid);
256 	printf("%s", buf);
257 	return (0);
258 }
259 
260 /*
261  * Encode/Decode UUID into octet-stream.
262  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
263  *
264  * 0                   1                   2                   3
265  *   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
266  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267  *  |                          time_low                             |
268  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
269  *  |       time_mid                |         time_hi_and_version   |
270  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
271  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
272  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
273  *  |                         node (2-5)                            |
274  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
275  */
276 
277 void
278 uuid_enc_le(void *buf, const struct uuid *uuid)
279 {
280 	uint8_t *p = buf;
281 	int i;
282 
283 	le32enc(p, uuid->time_low);
284 	le16enc(p + 4, uuid->time_mid);
285 	le16enc(p + 6, uuid->time_hi_and_version);
286 	p[8] = uuid->clock_seq_hi_and_reserved;
287 	p[9] = uuid->clock_seq_low;
288 	for (i = 0; i < _UUID_NODE_LEN; i++)
289 		p[10 + i] = uuid->node[i];
290 }
291 
292 void
293 uuid_dec_le(void const *buf, struct uuid *uuid)
294 {
295 	const uint8_t *p = buf;
296 	int i;
297 
298 	uuid->time_low = le32dec(p);
299 	uuid->time_mid = le16dec(p + 4);
300 	uuid->time_hi_and_version = le16dec(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 
307 void
308 uuid_enc_be(void *buf, const struct uuid *uuid)
309 {
310 	uint8_t *p = buf;
311 	int i;
312 
313 	be32enc(p, uuid->time_low);
314 	be16enc(p + 4, uuid->time_mid);
315 	be16enc(p + 6, uuid->time_hi_and_version);
316 	p[8] = uuid->clock_seq_hi_and_reserved;
317 	p[9] = uuid->clock_seq_low;
318 	for (i = 0; i < _UUID_NODE_LEN; i++)
319 		p[10 + i] = uuid->node[i];
320 }
321 
322 void
323 uuid_dec_be(void const *buf, struct uuid *uuid)
324 {
325 	const uint8_t *p = buf;
326 	int i;
327 
328 	uuid->time_low = be32dec(p);
329 	uuid->time_mid = be16dec(p + 4);
330 	uuid->time_hi_and_version = be16dec(p + 6);
331 	uuid->clock_seq_hi_and_reserved = p[8];
332 	uuid->clock_seq_low = p[9];
333 	for (i = 0; i < _UUID_NODE_LEN; i++)
334 		uuid->node[i] = p[10 + i];
335 }
336