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