1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright 2015 Joyent, Inc. All rights reserved.
  26  * Copyright 2014 Andrew Stormont.
  27  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  28  */
  29 
  30 /*
  31  * The copyright in this file is taken from the original Leach & Salz
  32  * UUID specification, from which this implementation is derived.
  33  */
  34 
  35 /*
  36  * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
  37  * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
  38  * Digital Equipment Corporation, Maynard, Mass.  Copyright (c) 1998
  39  * Microsoft.  To anyone who acknowledges that this file is provided
  40  * "AS IS" without any express or implied warranty: permission to use,
  41  * copy, modify, and distribute this file for any purpose is hereby
  42  * granted without fee, provided that the above copyright notices and
  43  * this notice appears in all source code copies, and that none of the
  44  * names of Open Software Foundation, Inc., Hewlett-Packard Company,
  45  * or Digital Equipment Corporation be used in advertising or
  46  * publicity pertaining to distribution of the software without
  47  * specific, written prior permission.  Neither Open Software
  48  * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
  49  * Equipment Corporation makes any representations about the
  50  * suitability of this software for any purpose.
  51  */
  52 
  53 /*
  54  * This module is the workhorse for generating abstract
  55  * UUIDs.  It delegates system-specific tasks (such
  56  * as obtaining the node identifier or system time)
  57  * to the sysdep module.
  58  */
  59 
  60 #include <ctype.h>
  61 #include <sys/param.h>
  62 #include <sys/stat.h>
  63 #include <errno.h>
  64 #include <stdio.h>
  65 #include <stdlib.h>
  66 #include <strings.h>
  67 #include <fcntl.h>
  68 #include <unistd.h>
  69 #include <synch.h>
  70 #include <sys/mman.h>
  71 #include "uuid_misc.h"
  72 
  73 shared_buffer_t         *data;
  74 
  75 static  uuid_node_t     node_id_cache;
  76 static  int             node_init;
  77 static  int             file_type;
  78 static  int             fd;
  79 
  80 /*
  81  * misc routines
  82  */
  83 uint16_t                get_random(void);
  84 void                    get_current_time(uuid_time_t *);
  85 
  86 void                    struct_to_string(uuid_t, struct uuid *);
  87 void                    string_to_struct(struct uuid *, uuid_t);
  88 int                     get_ethernet_address(uuid_node_t *);
  89 
  90 /*
  91  * local functions
  92  */
  93 static  int             map_state();
  94 static  void            format_uuid(struct uuid *, uint16_t, uuid_time_t,
  95     uuid_node_t);
  96 static  int             uuid_create(struct uuid *);
  97 static  void            gen_ethernet_address(uuid_node_t *);
  98 static  void            revalidate_data(uuid_node_t *);
  99 
 100 /*
 101  * Generates a uuid based on version 1 format.
 102  * Returns 0 on success and -1 on failure.
 103  */
 104 static int
 105 uuid_create(struct uuid *uuid)
 106 {
 107         uuid_time_t     timestamp;
 108         uuid_node_t     system_node;
 109         int             ret, non_unique = 0;
 110 
 111         /*
 112          * Get the system MAC address and/or cache it
 113          */
 114         if (node_init) {
 115                 bcopy(&node_id_cache, &system_node, sizeof (uuid_node_t));
 116         } else {
 117                 gen_ethernet_address(&system_node);
 118                 bcopy(&system_node, &node_id_cache, sizeof (uuid_node_t));
 119                 node_init = 1;
 120         }
 121 
 122         /*
 123          * Access the state file, mmap it and initialize the shared lock.
 124          * file_type tells us whether we had access to the state file or
 125          * created a temporary one.
 126          */
 127         if (map_state() == -1)
 128                 return (-1);
 129 
 130         /*
 131          * Acquire the lock
 132          */
 133         for (;;) {
 134                 if ((ret = mutex_lock(&data->lock)) == 0)
 135                         break;
 136                 else
 137                         switch (ret) {
 138                                 case EOWNERDEAD:
 139                                         revalidate_data(&system_node);
 140                                         (void) mutex_consistent(&data->lock);
 141                                         (void) mutex_unlock(&data->lock);
 142                                         break;
 143                                 case ENOTRECOVERABLE:
 144                                         return (ret);
 145                         }
 146         }
 147 
 148         /* State file is either new or is temporary, get a random clock seq */
 149         if (data->state.clock == 0) {
 150                 data->state.clock = get_random();
 151                 non_unique++;
 152         }
 153 
 154         if (memcmp(&system_node, &data->state.node, sizeof (uuid_node_t)) != 0)
 155                 data->state.clock++;
 156 
 157         get_current_time(&timestamp);
 158 
 159         /*
 160          * If timestamp is not set or is not in the past, bump
 161          * data->state.clock
 162          */
 163         if ((data->state.ts == 0) || (data->state.ts >= timestamp)) {
 164                 data->state.clock++;
 165                 data->state.ts = timestamp;
 166         }
 167 
 168         if (non_unique)
 169                 system_node.nodeID[0] |= 0x80;
 170 
 171         /* Stuff fields into the UUID struct */
 172         format_uuid(uuid, data->state.clock, timestamp, system_node);
 173 
 174         (void) mutex_unlock(&data->lock);
 175 
 176         return (0);
 177 }
 178 
 179 /*
 180  * Fills system_node with Ethernet address if available,
 181  * else fills random numbers
 182  */
 183 static void
 184 gen_ethernet_address(uuid_node_t *system_node)
 185 {
 186         uchar_t         node[6];
 187 
 188         if (get_ethernet_address(system_node) != 0) {
 189                 arc4random_buf(node, 6);
 190                 (void) memcpy(system_node->nodeID, node, 6);
 191                 /*
 192                  * use 8:.. with the multicast bit set
 193                  * to reduce namespace collisions.
 194                  */
 195                 system_node->nodeID[0] = 0x88;
 196         }
 197 }
 198 
 199 /*
 200  * Formats a UUID, given the clock_seq timestamp, and node address.
 201  * Fills in passed-in pointer with the resulting uuid.
 202  */
 203 static void
 204 format_uuid(struct uuid *uuid, uint16_t clock_seq,
 205     uuid_time_t timestamp, uuid_node_t node)
 206 {
 207 
 208         /*
 209          * First set up the first 60 bits from the timestamp
 210          */
 211         uuid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF);
 212         uuid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF);
 213         uuid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF);
 214 
 215         /*
 216          * This is version 1, so say so in the UUID version field (4 bits)
 217          */
 218         uuid->time_hi_and_version |= (1 << 12);
 219 
 220         /*
 221          * Now do the clock sequence
 222          */
 223         uuid->clock_seq_low = clock_seq & 0xFF;
 224 
 225         /*
 226          * We must save the most-significant 2 bits for the reserved field
 227          */
 228         uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
 229 
 230         /*
 231          * The variant for this format is the 2 high bits set to 10,
 232          * so here it is
 233          */
 234         uuid->clock_seq_hi_and_reserved |= 0x80;
 235 
 236         /*
 237          * write result to passed-in pointer
 238          */
 239         (void) memcpy(&uuid->node_addr, &node, sizeof (uuid->node_addr));
 240 }
 241 
 242 /*
 243  * Opens/creates the state file, falling back to a tmp
 244  */
 245 static int
 246 map_state()
 247 {
 248         FILE    *tmp;
 249 
 250         /* If file's mapped, return */
 251         if (file_type != 0)
 252                 return (1);
 253 
 254         if ((fd = open(STATE_LOCATION, O_RDWR)) < 0) {
 255                 file_type = TEMP_FILE;
 256 
 257                 if ((tmp = tmpfile()) == NULL)
 258                         return (-1);
 259                 else
 260                         fd = fileno(tmp);
 261         } else {
 262                 file_type = STATE_FILE;
 263         }
 264 
 265         (void) ftruncate(fd, (off_t)sizeof (shared_buffer_t));
 266 
 267         /* LINTED - alignment */
 268         data = (shared_buffer_t *)mmap(NULL, sizeof (shared_buffer_t),
 269             PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
 270 
 271         if (data == MAP_FAILED)
 272                 return (-1);
 273 
 274         (void) mutex_init(&data->lock, USYNC_PROCESS|LOCK_ROBUST, 0);
 275 
 276         (void) close(fd);
 277 
 278         return (1);
 279 }
 280 
 281 static void
 282 revalidate_data(uuid_node_t *node)
 283 {
 284         int i;
 285 
 286         data->state.ts = 0;
 287 
 288         for (i = 0; i < sizeof (data->state.node.nodeID); i++)
 289                 data->state.node.nodeID[i] = 0;
 290 
 291         data->state.clock = 0;
 292 
 293         gen_ethernet_address(node);
 294         bcopy(node, &node_id_cache, sizeof (uuid_node_t));
 295         node_init = 1;
 296 }
 297 
 298 /*
 299  * Prints a nicely-formatted uuid to stdout.
 300  */
 301 void
 302 uuid_print(struct uuid u)
 303 {
 304         int i;
 305 
 306         (void) printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
 307             u.time_hi_and_version, u.clock_seq_hi_and_reserved,
 308             u.clock_seq_low);
 309         for (i = 0; i < 6; i++)
 310                 (void) printf("%2.2x", u.node_addr[i]);
 311         (void) printf("\n");
 312 }
 313 
 314 /*
 315  * Unpacks the structure members in "struct uuid" to a char string "uuid_t".
 316  */
 317 void
 318 struct_to_string(uuid_t ptr, struct uuid *uu)
 319 {
 320         uint_t          tmp;
 321         uchar_t         *out = ptr;
 322 
 323         tmp = uu->time_low;
 324         out[3] = (uchar_t)tmp;
 325         tmp >>= 8;
 326         out[2] = (uchar_t)tmp;
 327         tmp >>= 8;
 328         out[1] = (uchar_t)tmp;
 329         tmp >>= 8;
 330         out[0] = (uchar_t)tmp;
 331 
 332         tmp = uu->time_mid;
 333         out[5] = (uchar_t)tmp;
 334         tmp >>= 8;
 335         out[4] = (uchar_t)tmp;
 336 
 337         tmp = uu->time_hi_and_version;
 338         out[7] = (uchar_t)tmp;
 339         tmp >>= 8;
 340         out[6] = (uchar_t)tmp;
 341 
 342         tmp = uu->clock_seq_hi_and_reserved;
 343         out[8] = (uchar_t)tmp;
 344         tmp = uu->clock_seq_low;
 345         out[9] = (uchar_t)tmp;
 346 
 347         (void) memcpy(out+10, uu->node_addr, 6);
 348 
 349 }
 350 
 351 /*
 352  * Packs the values in the "uuid_t" string into "struct uuid".
 353  */
 354 void
 355 string_to_struct(struct uuid *uuid, uuid_t in)
 356 {
 357         uchar_t *ptr;
 358         uint_t  tmp;
 359 
 360         ptr = in;
 361 
 362         tmp = *ptr++;
 363         tmp = (tmp << 8) | *ptr++;
 364         tmp = (tmp << 8) | *ptr++;
 365         tmp = (tmp << 8) | *ptr++;
 366         uuid->time_low = tmp;
 367 
 368         tmp = *ptr++;
 369         tmp = (tmp << 8) | *ptr++;
 370         uuid->time_mid = tmp;
 371 
 372         tmp = *ptr++;
 373         tmp = (tmp << 8) | *ptr++;
 374         uuid->time_hi_and_version = tmp;
 375 
 376         tmp = *ptr++;
 377         uuid->clock_seq_hi_and_reserved = tmp;
 378 
 379         tmp = *ptr++;
 380         uuid->clock_seq_low = tmp;
 381 
 382         (void) memcpy(uuid->node_addr, ptr, 6);
 383 
 384 }
 385 
 386 /*
 387  * Generates UUID based on DCE Version 4
 388  */
 389 void
 390 uuid_generate_random(uuid_t uu)
 391 {
 392         struct uuid     uuid;
 393 
 394         if (uu == NULL)
 395                 return;
 396 
 397         (void) memset(uu, 0, sizeof (uuid_t));
 398         (void) memset(&uuid, 0, sizeof (struct uuid));
 399 
 400         arc4random_buf(uu, sizeof (uuid_t));
 401         string_to_struct(&uuid, uu);
 402         /*
 403          * This is version 4, so say so in the UUID version field (4 bits)
 404          */
 405         uuid.time_hi_and_version |= (1 << 14);
 406         /*
 407          * we don't want the bit 1 to be set also which is for version 1
 408          */
 409         uuid.time_hi_and_version &= VER1_MASK;
 410 
 411         /*
 412          * The variant for this format is the 2 high bits set to 10,
 413          * so here it is
 414          */
 415         uuid.clock_seq_hi_and_reserved |= 0x80;
 416 
 417         /*
 418          * Set MSB of Ethernet address to 1 to indicate that it was generated
 419          * randomly
 420          */
 421         uuid.node_addr[0] |= 0x80;
 422         struct_to_string(uu, &uuid);
 423 }
 424 
 425 /*
 426  * Generates UUID based on DCE Version 1.
 427  */
 428 void
 429 uuid_generate_time(uuid_t uu)
 430 {
 431         struct  uuid uuid;
 432 
 433         if (uu == NULL)
 434                 return;
 435 
 436         if (uuid_create(&uuid) < 0) {
 437                 uuid_generate_random(uu);
 438                 return;
 439         }
 440 
 441         struct_to_string(uu, &uuid);
 442 }
 443 
 444 /*
 445  * Creates a new UUID. The uuid will be generated based on high-quality
 446  * randomness from arc4random(3C).
 447  */
 448 void
 449 uuid_generate(uuid_t uu)
 450 {
 451         uuid_generate_random(uu);
 452 }
 453 
 454 /*
 455  * Copies the UUID variable src to dst.
 456  */
 457 void
 458 uuid_copy(uuid_t dst, uuid_t src)
 459 {
 460         (void) memcpy(dst, src, UUID_LEN);
 461 }
 462 
 463 /*
 464  * Sets the value of the supplied uuid variable uu, to the NULL value.
 465  */
 466 void
 467 uuid_clear(uuid_t uu)
 468 {
 469         (void) memset(uu, 0, UUID_LEN);
 470 }
 471 
 472 /*
 473  * This function converts the supplied UUID uu from the internal
 474  * binary format into a 36-byte string (plus trailing null char)
 475  * and stores this value in the character string pointed to by out.
 476  */
 477 static void
 478 uuid_unparse_common(uuid_t uu, char *out, boolean_t upper)
 479 {
 480         struct uuid     uuid;
 481         uint16_t        clock_seq;
 482         char            etheraddr[13];
 483         int             index = 0, i;
 484 
 485         /* basic sanity checking */
 486         if (uu == NULL) {
 487                 return;
 488         }
 489 
 490         string_to_struct(&uuid, uu);
 491         clock_seq = uuid.clock_seq_hi_and_reserved;
 492         clock_seq = (clock_seq  << 8) | uuid.clock_seq_low;
 493         for (i = 0; i < 6; i++) {
 494                 (void) sprintf(&etheraddr[index++], upper ? "%.2X" : "%.2x",
 495                     uuid.node_addr[i]);
 496                 index++;
 497         }
 498         etheraddr[index] = '\0';
 499 
 500         (void) snprintf(out, 25,
 501             upper ? "%08X-%04X-%04X-%04X-" : "%08x-%04x-%04x-%04x-",
 502             uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq);
 503         (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH);
 504 }
 505 
 506 void
 507 uuid_unparse_upper(uuid_t uu, char *out)
 508 {
 509         uuid_unparse_common(uu, out, B_TRUE);
 510 }
 511 
 512 void
 513 uuid_unparse_lower(uuid_t uu, char *out)
 514 {
 515         uuid_unparse_common(uu, out, B_FALSE);
 516 }
 517 
 518 void
 519 uuid_unparse(uuid_t uu, char *out)
 520 {
 521         /*
 522          * Historically uuid_unparse on Solaris returns lower case,
 523          * for compatibility we preserve this behaviour.
 524          */
 525         uuid_unparse_common(uu, out, B_FALSE);
 526 }
 527 
 528 /*
 529  * The uuid_is_null function compares the value of the supplied
 530  * UUID variable uu to the NULL value. If the value is equal
 531  * to the NULL UUID, 1 is returned, otherwise 0 is returned.
 532  */
 533 int
 534 uuid_is_null(uuid_t uu)
 535 {
 536         int             i;
 537         uuid_t          null_uu;
 538 
 539         (void) memset(null_uu, 0, sizeof (uuid_t));
 540         i = memcmp(uu, null_uu, sizeof (uuid_t));
 541         if (i == 0) {
 542                 /* uu is NULL uuid */
 543                 return (1);
 544         } else {
 545                 return (0);
 546         }
 547 }
 548 
 549 /*
 550  * uuid_parse converts the UUID string given by 'in' into the
 551  * internal uuid_t format. The input UUID is a string of the form
 552  * cefa7a9c-1dd2-11b2-8350-880020adbeef in printf(3C) format.
 553  * Upon successfully parsing the input string, UUID is stored
 554  * in the location pointed to by uu
 555  */
 556 int
 557 uuid_parse(char *in, uuid_t uu)
 558 {
 559 
 560         char            *ptr, buf[3];
 561         int             i;
 562         struct uuid     uuid;
 563         uint16_t        clock_seq;
 564 
 565         /* do some sanity checking */
 566         if ((strlen(in) != 36) || (uu == NULL) || (in[36] != '\0')) {
 567                 return (-1);
 568         }
 569 
 570         ptr = in;
 571         for (i = 0; i < 36; i++, ptr++) {
 572                 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
 573                         if (*ptr != '-') {
 574                                 return (-1);
 575                         }
 576                 } else {
 577                         if (!isxdigit(*ptr)) {
 578                                 return (-1);
 579                         }
 580                 }
 581         }
 582 
 583         uuid.time_low = strtoul(in, NULL, 16);
 584         uuid.time_mid = strtoul(in+9, NULL, 16);
 585         uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
 586         clock_seq = strtoul(in+19, NULL, 16);
 587         uuid.clock_seq_hi_and_reserved = (clock_seq & 0xFF00) >> 8;
 588         uuid.clock_seq_low = (clock_seq & 0xFF);
 589 
 590         ptr = in+24;
 591         buf[2] = '\0';
 592         for (i = 0; i < 6; i++) {
 593                 buf[0] = *ptr++;
 594                 buf[1] = *ptr++;
 595                 uuid.node_addr[i] = strtoul(buf, NULL, 16);
 596         }
 597         struct_to_string(uu, &uuid);
 598         return (0);
 599 }
 600 
 601 /*
 602  * uuid_time extracts the time at which the supplied UUID uu
 603  * was created. This function can only extract the creation
 604  * time for UUIDs created with the uuid_generate_time function.
 605  * The time at which the UUID was created, in seconds and
 606  * microseconds since the epoch is stored in the location
 607  * pointed to by ret_tv.
 608  */
 609 time_t
 610 uuid_time(uuid_t uu, struct timeval *ret_tv)
 611 {
 612         struct uuid     uuid;
 613         uint_t          high;
 614         struct timeval  tv;
 615         u_longlong_t    clock_reg;
 616         uint_t          tmp;
 617         uint8_t         clk;
 618 
 619         string_to_struct(&uuid, uu);
 620         tmp = (uuid.time_hi_and_version & 0xF000) >> 12;
 621         clk = uuid.clock_seq_hi_and_reserved;
 622 
 623         /* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */
 624         if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) {
 625                 return (-1);
 626         }
 627         high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
 628         clock_reg = uuid.time_low | ((u_longlong_t)high << 32);
 629 
 630         clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000;
 631         tv.tv_sec = clock_reg / 10000000;
 632         tv.tv_usec = (clock_reg % 10000000) / 10;
 633 
 634         if (ret_tv) {
 635                 *ret_tv = tv;
 636         }
 637 
 638         return (tv.tv_sec);
 639 }