1 /* $NetBSD: kern_uuid.c,v 1.20 2014/10/05 10:00:03 riastradh 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.20 2014/10/05 10:00:03 riastradh Exp $");
33
34 #include <sys/param.h>
35 #include <sys/cprng.h>
36 #include <sys/endian.h>
37 #include <sys/syscallargs.h>
38 #include <sys/systm.h>
39 #include <sys/uuid.h>
40
41 /*
42 * See also:
43 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
44 * http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
45 */
46
47 CTASSERT(sizeof(struct uuid) == 16);
48
49 static void
uuid_generate(struct uuid * uuid)50 uuid_generate(struct uuid *uuid)
51 {
52
53 /* Randomly generate the content. */
54 cprng_fast(uuid, sizeof(*uuid));
55
56 /* Set the version number to 4. */
57 uuid->time_hi_and_version &= ~(uint32_t)0xf000;
58 uuid->time_hi_and_version |= 0x4000;
59
60 /* Fix the reserved bits. */
61 uuid->clock_seq_hi_and_reserved &= ~(uint8_t)0x40;
62 uuid->clock_seq_hi_and_reserved |= 0x80;
63 }
64
65 int
sys_uuidgen(struct lwp * l,const struct sys_uuidgen_args * uap,register_t * retval)66 sys_uuidgen(struct lwp *l, const struct sys_uuidgen_args *uap, register_t *retval)
67 {
68 struct uuid *store, tmp;
69 unsigned count;
70 int error;
71
72 /*
73 * Limit the number of UUIDs that can be created at the same time
74 * to some arbitrary number. This isn't really necessary, but I
75 * like to have some sort of upper-bound that's less than 2G :-)
76 * XXX needs to be tunable.
77 */
78 if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
79 return (EINVAL);
80
81 for (store = SCARG(uap,store), count = SCARG(uap,count);
82 count > 0;
83 store++, count--) {
84 uuid_generate(&tmp);
85 error = copyout(&tmp, store, sizeof tmp);
86 if (error)
87 return error;
88 }
89
90 return 0;
91 }
92
93 int
uuidgen(struct uuid * store,int count)94 uuidgen(struct uuid *store, int count)
95 {
96
97 while (--count > 0)
98 uuid_generate(store++);
99 return 0;
100 }
101
102 int
uuid_snprintf(char * buf,size_t sz,const struct uuid * uuid)103 uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
104 {
105
106 return snprintf(buf, sz,
107 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
108 uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
109 uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
110 uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3],
111 uuid->node[4], uuid->node[5]);
112 }
113
114 int
uuid_printf(const struct uuid * uuid)115 uuid_printf(const struct uuid *uuid)
116 {
117 char buf[UUID_STR_LEN];
118
119 (void) uuid_snprintf(buf, sizeof(buf), uuid);
120 printf("%s", buf);
121 return (0);
122 }
123
124 /*
125 * Encode/Decode UUID into octet-stream.
126 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
127 *
128 * 0 1 2 3
129 * 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
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 * | time_low |
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | time_mid | time_hi_and_version |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * |clk_seq_hi_res | clk_seq_low | node (0-1) |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | node (2-5) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 */
140
141 void
uuid_enc_le(void * buf,const struct uuid * uuid)142 uuid_enc_le(void *buf, const struct uuid *uuid)
143 {
144 uint8_t *p = buf;
145 int i;
146
147 le32enc(p, uuid->time_low);
148 le16enc(p + 4, uuid->time_mid);
149 le16enc(p + 6, uuid->time_hi_and_version);
150 p[8] = uuid->clock_seq_hi_and_reserved;
151 p[9] = uuid->clock_seq_low;
152 for (i = 0; i < _UUID_NODE_LEN; i++)
153 p[10 + i] = uuid->node[i];
154 }
155
156 void
uuid_dec_le(void const * buf,struct uuid * uuid)157 uuid_dec_le(void const *buf, struct uuid *uuid)
158 {
159 const uint8_t *p = buf;
160 int i;
161
162 uuid->time_low = le32dec(p);
163 uuid->time_mid = le16dec(p + 4);
164 uuid->time_hi_and_version = le16dec(p + 6);
165 uuid->clock_seq_hi_and_reserved = p[8];
166 uuid->clock_seq_low = p[9];
167 for (i = 0; i < _UUID_NODE_LEN; i++)
168 uuid->node[i] = p[10 + i];
169 }
170
171 void
uuid_enc_be(void * buf,const struct uuid * uuid)172 uuid_enc_be(void *buf, const struct uuid *uuid)
173 {
174 uint8_t *p = buf;
175 int i;
176
177 be32enc(p, uuid->time_low);
178 be16enc(p + 4, uuid->time_mid);
179 be16enc(p + 6, uuid->time_hi_and_version);
180 p[8] = uuid->clock_seq_hi_and_reserved;
181 p[9] = uuid->clock_seq_low;
182 for (i = 0; i < _UUID_NODE_LEN; i++)
183 p[10 + i] = uuid->node[i];
184 }
185
186 void
uuid_dec_be(void const * buf,struct uuid * uuid)187 uuid_dec_be(void const *buf, struct uuid *uuid)
188 {
189 const uint8_t *p = buf;
190 int i;
191
192 uuid->time_low = be32dec(p);
193 uuid->time_mid = be16dec(p + 4);
194 uuid->time_hi_and_version = be16dec(p + 6);
195 uuid->clock_seq_hi_and_reserved = p[8];
196 uuid->clock_seq_low = p[9];
197 for (i = 0; i < _UUID_NODE_LEN; i++)
198 uuid->node[i] = p[10 + i];
199 }
200