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