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