kern_uuid.c revision 1.5.2.1 1 /* $NetBSD: kern_uuid.c,v 1.5.2.1 2007/02/26 09:11:13 yamt 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.5.2.1 2007/02/26 09:11:13 yamt Exp $");
33
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.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 /* XXX Do we have a similar ASSERT()? */
62 #define CTASSERT(x)
63
64 CTASSERT(sizeof(struct uuid) == 16);
65
66 /* We use an alternative, more convenient representation in the generator. */
67 struct uuid_private {
68 union {
69 uint64_t ll; /* internal. */
70 struct {
71 uint32_t low;
72 uint16_t mid;
73 uint16_t hi;
74 } x;
75 } time;
76 uint16_t seq; /* Big-endian. */
77 uint16_t node[UUID_NODE_LEN>>1];
78 };
79
80 CTASSERT(sizeof(struct uuid_private) == 16);
81
82 static struct uuid_private uuid_last;
83
84 /* "UUID generator mutex lock" */
85 static struct simplelock uuid_mutex = SIMPLELOCK_INITIALIZER;
86
87 /*
88 * Return the first MAC address we encounter or, if none was found,
89 * construct a sufficiently random multicast address. We don't try
90 * to return the same MAC address as previously returned. We always
91 * generate a new multicast address if no MAC address exists in the
92 * system.
93 * It would be nice to know if 'ifnet' or any of its sub-structures
94 * has been changed in any way. If not, we could simply skip the
95 * scan and safely return the MAC address we returned before.
96 */
97 static void
98 uuid_node(uint16_t *node)
99 {
100 struct ifnet *ifp;
101 struct ifaddr *ifa;
102 struct sockaddr_dl *sdl;
103 int i, s;
104
105 s = splnet();
106 IFNET_FOREACH(ifp) {
107 /* Walk the address list */
108 IFADDR_FOREACH(ifa, ifp) {
109 sdl = (struct sockaddr_dl*)ifa->ifa_addr;
110 if (sdl != NULL && sdl->sdl_family == AF_LINK &&
111 sdl->sdl_type == IFT_ETHER) {
112 /* Got a MAC address. */
113 memcpy(node, LLADDR(sdl), UUID_NODE_LEN);
114 splx(s);
115 return;
116 }
117 }
118 }
119 splx(s);
120
121 for (i = 0; i < (UUID_NODE_LEN>>1); i++)
122 node[i] = (uint16_t)arc4random();
123 *((uint8_t*)node) |= 0x01;
124 }
125
126 /*
127 * Get the current time as a 60 bit count of 100-nanosecond intervals
128 * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
129 * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
130 * Gregorian reform to the Christian calendar.
131 */
132 /*
133 * At present, NetBSD has no timespec source, only timeval sources. So,
134 * we use timeval.
135 */
136 static uint64_t
137 uuid_time(void)
138 {
139 struct timeval tv;
140 uint64_t xtime = 0x01B21DD213814000LL;
141
142 microtime(&tv);
143 xtime += (uint64_t)tv.tv_sec * 10000000LL;
144 xtime += (uint64_t)(10 * tv.tv_usec);
145 return (xtime & ((1LL << 60) - 1LL));
146 }
147
148 /*
149 * Internal routine to actually generate the UUID.
150 */
151 static void
152 uuid_generate(struct uuid_private *uuid, uint64_t *timep, int count)
153 {
154 uint64_t xtime;
155
156 simple_lock(&uuid_mutex);
157
158 uuid_node(uuid->node);
159 xtime = uuid_time();
160 *timep = xtime;
161
162 if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid->node[0] ||
163 uuid_last.node[1] != uuid->node[1] ||
164 uuid_last.node[2] != uuid->node[2])
165 uuid->seq = (uint16_t)arc4random() & 0x3fff;
166 else if (uuid_last.time.ll >= xtime)
167 uuid->seq = (uuid_last.seq + 1) & 0x3fff;
168 else
169 uuid->seq = uuid_last.seq;
170
171 uuid_last = *uuid;
172 uuid_last.time.ll = (xtime + count - 1) & ((1LL << 60) - 1LL);
173
174 simple_unlock(&uuid_mutex);
175 }
176
177 int
178 sys_uuidgen(struct lwp *l, void *v, register_t *retval)
179 {
180 struct sys_uuidgen_args *uap = v;
181 struct uuid_private uuid;
182 uint64_t xtime;
183 int error;
184
185 /*
186 * Limit the number of UUIDs that can be created at the same time
187 * to some arbitrary number. This isn't really necessary, but I
188 * like to have some sort of upper-bound that's less than 2G :-)
189 * XXX needs to be tunable.
190 */
191 if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
192 return (EINVAL);
193
194 /* XXX: pre-validate accessibility to the whole of the UUID store? */
195
196 /* Generate the base UUID. */
197 uuid_generate(&uuid, &xtime, SCARG(uap, count));
198
199 /* Set sequence and variant and deal with byte order. */
200 uuid.seq = htobe16(uuid.seq | 0x8000);
201
202 /* XXX: this should copyout larger chunks at a time. */
203 do {
204 /* Set time and version (=1) and deal with byte order. */
205 uuid.time.x.low = (uint32_t)xtime;
206 uuid.time.x.mid = (uint16_t)(xtime >> 32);
207 uuid.time.x.hi = ((uint16_t)(xtime >> 48) & 0xfff) | (1 << 12);
208 error = copyout(&uuid, SCARG(uap,store), sizeof(uuid));
209 SCARG(uap, store)++;
210 SCARG(uap, count)--;
211 xtime++;
212 } while (SCARG(uap, count) > 0 && error == 0);
213
214 return (error);
215 }
216
217 int
218 uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
219 {
220 const struct uuid_private *id;
221 int cnt;
222
223 id = (const struct uuid_private *)uuid;
224 cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
225 id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
226 be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
227 return (cnt);
228 }
229
230 int
231 uuid_printf(const struct uuid *uuid)
232 {
233 char buf[UUID_STR_LEN];
234
235 (void) uuid_snprintf(buf, sizeof(buf), uuid);
236 printf("%s", buf);
237 return (0);
238 }
239
240 /*
241 * Encode/Decode UUID into octet-stream.
242 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
243 *
244 * 0 1 2 3
245 * 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
246 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
247 * | time_low |
248 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
249 * | time_mid | time_hi_and_version |
250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
251 * |clk_seq_hi_res | clk_seq_low | node (0-1) |
252 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
253 * | node (2-5) |
254 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
255 */
256
257 void
258 uuid_enc_le(void *buf, const struct uuid *uuid)
259 {
260 uint8_t *p = buf;
261 int i;
262
263 le32enc(p, uuid->time_low);
264 le16enc(p + 4, uuid->time_mid);
265 le16enc(p + 6, uuid->time_hi_and_version);
266 p[8] = uuid->clock_seq_hi_and_reserved;
267 p[9] = uuid->clock_seq_low;
268 for (i = 0; i < _UUID_NODE_LEN; i++)
269 p[10 + i] = uuid->node[i];
270 }
271
272 void
273 uuid_dec_le(void const *buf, struct uuid *uuid)
274 {
275 const uint8_t *p = buf;
276 int i;
277
278 uuid->time_low = le32dec(p);
279 uuid->time_mid = le16dec(p + 4);
280 uuid->time_hi_and_version = le16dec(p + 6);
281 uuid->clock_seq_hi_and_reserved = p[8];
282 uuid->clock_seq_low = p[9];
283 for (i = 0; i < _UUID_NODE_LEN; i++)
284 uuid->node[i] = p[10 + i];
285 }
286
287 void
288 uuid_enc_be(void *buf, const struct uuid *uuid)
289 {
290 uint8_t *p = buf;
291 int i;
292
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
302 void
303 uuid_dec_be(void const *buf, struct uuid *uuid)
304 {
305 const uint8_t *p = buf;
306 int i;
307
308 uuid->time_low = be32dec(p);
309 uuid->time_mid = le16dec(p + 4);
310 uuid->time_hi_and_version = be16dec(p + 6);
311 uuid->clock_seq_hi_and_reserved = p[8];
312 uuid->clock_seq_low = p[9];
313 for (i = 0; i < _UUID_NODE_LEN; i++)
314 uuid->node[i] = p[10 + i];
315 }
316