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