1 /*- 2 * Copyright (c) 2002 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/endian.h> 32 #include <sys/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/sbuf.h> 36 #include <sys/socket.h> 37 #include <sys/sysproto.h> 38 #include <sys/systm.h> 39 #include <sys/uuid.h> 40 41 #include <net/if.h> 42 #include <net/if_dl.h> 43 #include <net/if_types.h> 44 45 /* 46 * See also: 47 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt 48 * http://www.opengroup.org/onlinepubs/009629399/apdxa.htm 49 * 50 * Note that the generator state is itself an UUID, but the time and clock 51 * sequence fields are written in the native byte order. 52 */ 53 54 CTASSERT(sizeof(struct uuid) == 16); 55 56 /* We use an alternative, more convenient representation in the generator. */ 57 struct uuid_private { 58 union { 59 uint64_t ll; /* internal. */ 60 struct { 61 uint32_t low; 62 uint16_t mid; 63 uint16_t hi; 64 } x; 65 } time; 66 uint16_t seq; /* Big-endian. */ 67 uint16_t node[UUID_NODE_LEN>>1]; 68 }; 69 70 CTASSERT(sizeof(struct uuid_private) == 16); 71 72 static struct uuid_private uuid_last; 73 74 static struct mtx uuid_mutex; 75 MTX_SYSINIT(uuid_lock, &uuid_mutex, "UUID generator mutex lock", MTX_DEF); 76 77 /* 78 * Return the first MAC address we encounter or, if none was found, 79 * construct a sufficiently random multicast address. We don't try 80 * to return the same MAC address as previously returned. We always 81 * generate a new multicast address if no MAC address exists in the 82 * system. 83 * It would be nice to know if 'ifnet' or any of its sub-structures 84 * has been changed in any way. If not, we could simply skip the 85 * scan and safely return the MAC address we returned before. 86 */ 87 static void 88 uuid_node(uint16_t *node) 89 { 90 struct ifnet *ifp; 91 struct ifaddr *ifa; 92 struct sockaddr_dl *sdl; 93 int i; 94 95 IFNET_RLOCK(); 96 TAILQ_FOREACH(ifp, &ifnet, if_link) { 97 /* Walk the address list */ 98 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 99 sdl = (struct sockaddr_dl*)ifa->ifa_addr; 100 if (sdl != NULL && sdl->sdl_family == AF_LINK && 101 sdl->sdl_type == IFT_ETHER) { 102 /* Got a MAC address. */ 103 bcopy(LLADDR(sdl), node, UUID_NODE_LEN); 104 IFNET_RUNLOCK(); 105 return; 106 } 107 } 108 } 109 IFNET_RUNLOCK(); 110 111 for (i = 0; i < (UUID_NODE_LEN>>1); i++) 112 node[i] = (uint16_t)arc4random(); 113 *((uint8_t*)node) |= 0x01; 114 } 115 116 /* 117 * Get the current time as a 60 bit count of 100-nanosecond intervals 118 * since 00:00:00.00, October 15,1582. We apply a magic offset to convert 119 * the Unix time since 00:00:00.00, January 1, 1970 to the date of the 120 * Gregorian reform to the Christian calendar. 121 */ 122 static uint64_t 123 uuid_time(void) 124 { 125 struct bintime bt; 126 uint64_t time = 0x01B21DD213814000LL; 127 128 bintime(&bt); 129 time += (uint64_t)bt.sec * 10000000LL; 130 time += (10000000LL * (uint32_t)(bt.frac >> 32)) >> 32; 131 return (time & ((1LL << 60) - 1LL)); 132 } 133 134 struct uuid * 135 kern_uuidgen(struct uuid *store, size_t count) 136 { 137 struct uuid_private uuid; 138 uint64_t time; 139 size_t n; 140 141 mtx_lock(&uuid_mutex); 142 143 uuid_node(uuid.node); 144 time = uuid_time(); 145 146 if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid.node[0] || 147 uuid_last.node[1] != uuid.node[1] || 148 uuid_last.node[2] != uuid.node[2]) 149 uuid.seq = (uint16_t)arc4random() & 0x3fff; 150 else if (uuid_last.time.ll >= time) 151 uuid.seq = (uuid_last.seq + 1) & 0x3fff; 152 else 153 uuid.seq = uuid_last.seq; 154 155 uuid_last = uuid; 156 uuid_last.time.ll = (time + count - 1) & ((1LL << 60) - 1LL); 157 158 mtx_unlock(&uuid_mutex); 159 160 /* Set sequence and variant and deal with byte order. */ 161 uuid.seq = htobe16(uuid.seq | 0x8000); 162 163 for (n = 0; n < count; n++) { 164 /* Set time and version (=1). */ 165 uuid.time.x.low = (uint32_t)time; 166 uuid.time.x.mid = (uint16_t)(time >> 32); 167 uuid.time.x.hi = ((uint16_t)(time >> 48) & 0xfff) | (1 << 12); 168 store[n] = *(struct uuid *)&uuid; 169 time++; 170 } 171 172 return (store); 173 } 174 175 #ifndef _SYS_SYSPROTO_H_ 176 struct uuidgen_args { 177 struct uuid *store; 178 int count; 179 }; 180 #endif 181 int 182 uuidgen(struct thread *td, struct uuidgen_args *uap) 183 { 184 struct uuid *store; 185 size_t count; 186 int error; 187 188 /* 189 * Limit the number of UUIDs that can be created at the same time 190 * to some arbitrary number. This isn't really necessary, but I 191 * like to have some sort of upper-bound that's less than 2G :-) 192 * XXX probably needs to be tunable. 193 */ 194 if (uap->count < 1 || uap->count > 2048) 195 return (EINVAL); 196 197 count = uap->count; 198 store = malloc(count * sizeof(struct uuid), M_TEMP, M_WAITOK); 199 kern_uuidgen(store, count); 200 error = copyout(store, uap->store, count * sizeof(struct uuid)); 201 free(store, M_TEMP); 202 return (error); 203 } 204 205 int 206 snprintf_uuid(char *buf, size_t sz, struct uuid *uuid) 207 { 208 struct uuid_private *id; 209 int cnt; 210 211 id = (struct uuid_private *)uuid; 212 cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x", 213 id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq), 214 be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2])); 215 return (cnt); 216 } 217 218 int 219 printf_uuid(struct uuid *uuid) 220 { 221 char buf[38]; 222 223 snprintf_uuid(buf, sizeof(buf), uuid); 224 return (printf("%s", buf)); 225 } 226 227 int 228 sbuf_printf_uuid(struct sbuf *sb, struct uuid *uuid) 229 { 230 char buf[38]; 231 232 snprintf_uuid(buf, sizeof(buf), uuid); 233 return (sbuf_printf(sb, "%s", buf)); 234 } 235 236 /* 237 * Encode/Decode UUID into byte-stream. 238 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt 239 * 240 * 0 1 2 3 241 * 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 242 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 243 * | time_low | 244 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 245 * | time_mid | time_hi_and_version | 246 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 247 * |clk_seq_hi_res | clk_seq_low | node (0-1) | 248 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 249 * | node (2-5) | 250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 251 */ 252 253 void 254 le_uuid_enc(void *buf, struct uuid const *uuid) 255 { 256 u_char *p; 257 int i; 258 259 p = buf; 260 le32enc(p, uuid->time_low); 261 le16enc(p + 4, uuid->time_mid); 262 le16enc(p + 6, uuid->time_hi_and_version); 263 p[8] = uuid->clock_seq_hi_and_reserved; 264 p[9] = uuid->clock_seq_low; 265 for (i = 0; i < _UUID_NODE_LEN; i++) 266 p[10 + i] = uuid->node[i]; 267 } 268 269 void 270 le_uuid_dec(void const *buf, struct uuid *uuid) 271 { 272 u_char const *p; 273 int i; 274 275 p = buf; 276 uuid->time_low = le32dec(p); 277 uuid->time_mid = le16dec(p + 4); 278 uuid->time_hi_and_version = le16dec(p + 6); 279 uuid->clock_seq_hi_and_reserved = p[8]; 280 uuid->clock_seq_low = p[9]; 281 for (i = 0; i < _UUID_NODE_LEN; i++) 282 uuid->node[i] = p[10 + i]; 283 } 284 285 void 286 be_uuid_enc(void *buf, struct uuid const *uuid) 287 { 288 u_char *p; 289 int i; 290 291 p = buf; 292 be32enc(p, uuid->time_low); 293 be16enc(p + 4, uuid->time_mid); 294 be16enc(p + 6, uuid->time_hi_and_version); 295 p[8] = uuid->clock_seq_hi_and_reserved; 296 p[9] = uuid->clock_seq_low; 297 for (i = 0; i < _UUID_NODE_LEN; i++) 298 p[10 + i] = uuid->node[i]; 299 } 300 301 void 302 be_uuid_dec(void const *buf, struct uuid *uuid) 303 { 304 u_char const *p; 305 int i; 306 307 p = buf; 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 317 int 318 parse_uuid(const char *str, struct uuid *uuid) 319 { 320 u_int c[11]; 321 int n; 322 323 /* An empty string represents a nil UUID. */ 324 if (*str == '\0') { 325 bzero(uuid, sizeof(*uuid)); 326 return (0); 327 } 328 329 /* The UUID string representation has a fixed length. */ 330 if (strlen(str) != 36) 331 return (EINVAL); 332 333 /* 334 * We only work with "new" UUIDs. New UUIDs have the form: 335 * 01234567-89ab-cdef-0123-456789abcdef 336 * The so called "old" UUIDs, which we don't support, have the form: 337 * 0123456789ab.cd.ef.01.23.45.67.89.ab 338 */ 339 if (str[8] != '-') 340 return (EINVAL); 341 342 n = sscanf(str, "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x", c + 0, c + 1, 343 c + 2, c + 3, c + 4, c + 5, c + 6, c + 7, c + 8, c + 9, c + 10); 344 /* Make sure we have all conversions. */ 345 if (n != 11) 346 return (EINVAL); 347 348 /* Successful scan. Build the UUID. */ 349 uuid->time_low = c[0]; 350 uuid->time_mid = c[1]; 351 uuid->time_hi_and_version = c[2]; 352 uuid->clock_seq_hi_and_reserved = c[3]; 353 uuid->clock_seq_low = c[4]; 354 for (n = 0; n < 6; n++) 355 uuid->node[n] = c[n + 5]; 356 357 /* Check semantics... */ 358 return (((c[3] & 0x80) != 0x00 && /* variant 0? */ 359 (c[3] & 0xc0) != 0x80 && /* variant 1? */ 360 (c[3] & 0xe0) != 0xc0) ? EINVAL : 0); /* variant 2? */ 361 } 362