17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d62bc4baSyz147064 * Common Development and Distribution License (the "License"). 6d62bc4baSyz147064 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22d62bc4baSyz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 2433f5ff17SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved. 25*84615402SJerry Jelinek * Copyright 2013 Joyent, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * The copyright in this file is taken from the original Leach & Salz 307c478bd9Sstevel@tonic-gate * UUID specification, from which this implementation is derived. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc. 357c478bd9Sstevel@tonic-gate * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. & 367c478bd9Sstevel@tonic-gate * Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998 377c478bd9Sstevel@tonic-gate * Microsoft. To anyone who acknowledges that this file is provided 387c478bd9Sstevel@tonic-gate * "AS IS" without any express or implied warranty: permission to use, 397c478bd9Sstevel@tonic-gate * copy, modify, and distribute this file for any purpose is hereby 407c478bd9Sstevel@tonic-gate * granted without fee, provided that the above copyright notices and 417c478bd9Sstevel@tonic-gate * this notice appears in all source code copies, and that none of the 427c478bd9Sstevel@tonic-gate * names of Open Software Foundation, Inc., Hewlett-Packard Company, 437c478bd9Sstevel@tonic-gate * or Digital Equipment Corporation be used in advertising or 447c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the software without 457c478bd9Sstevel@tonic-gate * specific, written prior permission. Neither Open Software 467c478bd9Sstevel@tonic-gate * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital 477c478bd9Sstevel@tonic-gate * Equipment Corporation makes any representations about the 487c478bd9Sstevel@tonic-gate * suitability of this software for any purpose. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 52cc1a9a89SRafael Vanoni Polanczyk * This module is the workhorse for generating abstract 537c478bd9Sstevel@tonic-gate * UUIDs. It delegates system-specific tasks (such 547c478bd9Sstevel@tonic-gate * as obtaining the node identifier or system time) 557c478bd9Sstevel@tonic-gate * to the sysdep module. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include <ctype.h> 597c478bd9Sstevel@tonic-gate #include <sys/param.h> 607c478bd9Sstevel@tonic-gate #include <sys/stat.h> 617c478bd9Sstevel@tonic-gate #include <errno.h> 627c478bd9Sstevel@tonic-gate #include <stdio.h> 637c478bd9Sstevel@tonic-gate #include <stdlib.h> 647c478bd9Sstevel@tonic-gate #include <strings.h> 657c478bd9Sstevel@tonic-gate #include <fcntl.h> 667c478bd9Sstevel@tonic-gate #include <unistd.h> 677c478bd9Sstevel@tonic-gate #include <synch.h> 68cc1a9a89SRafael Vanoni Polanczyk #include <sys/mman.h> 697c478bd9Sstevel@tonic-gate #include "uuid_misc.h" 707c478bd9Sstevel@tonic-gate 71cc1a9a89SRafael Vanoni Polanczyk shared_buffer_t *data; 727c478bd9Sstevel@tonic-gate 73cc1a9a89SRafael Vanoni Polanczyk static uuid_node_t node_id_cache; 74cc1a9a89SRafael Vanoni Polanczyk static int node_init; 75cc1a9a89SRafael Vanoni Polanczyk static int file_type; 76cc1a9a89SRafael Vanoni Polanczyk static int fd; 777c478bd9Sstevel@tonic-gate 78cc1a9a89SRafael Vanoni Polanczyk /* 79*84615402SJerry Jelinek * The urandmtx mutex prevents multiple opens of /dev/urandom and protects the 80*84615402SJerry Jelinek * cache. 81*84615402SJerry Jelinek */ 82*84615402SJerry Jelinek #define RCACHE_SIZE 65535 83*84615402SJerry Jelinek static mutex_t urandmtx; 84*84615402SJerry Jelinek static int fd_urand = -1; 85*84615402SJerry Jelinek static char rcache[RCACHE_SIZE]; 86*84615402SJerry Jelinek static char *rcachep = rcache; 87*84615402SJerry Jelinek 88*84615402SJerry Jelinek /* 89cc1a9a89SRafael Vanoni Polanczyk * misc routines 90cc1a9a89SRafael Vanoni Polanczyk */ 91cc1a9a89SRafael Vanoni Polanczyk uint16_t get_random(void); 92cc1a9a89SRafael Vanoni Polanczyk void get_current_time(uuid_time_t *); 93cc1a9a89SRafael Vanoni Polanczyk 947c478bd9Sstevel@tonic-gate void struct_to_string(uuid_t, struct uuid *); 957c478bd9Sstevel@tonic-gate void string_to_struct(struct uuid *, uuid_t); 967c478bd9Sstevel@tonic-gate int get_ethernet_address(uuid_node_t *); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * local functions 1007c478bd9Sstevel@tonic-gate */ 101cc1a9a89SRafael Vanoni Polanczyk static int map_state(); 102cc1a9a89SRafael Vanoni Polanczyk static void format_uuid(struct uuid *, uint16_t, uuid_time_t, 1037c478bd9Sstevel@tonic-gate uuid_node_t); 1047c478bd9Sstevel@tonic-gate static void fill_random_bytes(uchar_t *, int); 1057c478bd9Sstevel@tonic-gate static int uuid_create(struct uuid *); 1067c478bd9Sstevel@tonic-gate static void gen_ethernet_address(uuid_node_t *); 107cc1a9a89SRafael Vanoni Polanczyk static void revalidate_data(uuid_node_t *); 108cc1a9a89SRafael Vanoni Polanczyk 1097c478bd9Sstevel@tonic-gate /* 110cc1a9a89SRafael Vanoni Polanczyk * Generates a uuid based on version 1 format. 111cc1a9a89SRafael Vanoni Polanczyk * Returns 0 on success and -1 on failure. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate static int 1147c478bd9Sstevel@tonic-gate uuid_create(struct uuid *uuid) 1157c478bd9Sstevel@tonic-gate { 116cc1a9a89SRafael Vanoni Polanczyk uuid_time_t timestamp; 1177c478bd9Sstevel@tonic-gate uuid_node_t system_node; 118cc1a9a89SRafael Vanoni Polanczyk int ret, non_unique = 0; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /* 121cc1a9a89SRafael Vanoni Polanczyk * Get the system MAC address and/or cache it 1227c478bd9Sstevel@tonic-gate */ 123cc1a9a89SRafael Vanoni Polanczyk if (node_init) { 124cc1a9a89SRafael Vanoni Polanczyk bcopy(&node_id_cache, &system_node, sizeof (uuid_node_t)); 1257c478bd9Sstevel@tonic-gate } else { 126cc1a9a89SRafael Vanoni Polanczyk gen_ethernet_address(&system_node); 127cc1a9a89SRafael Vanoni Polanczyk bcopy(&system_node, &node_id_cache, sizeof (uuid_node_t)); 128cc1a9a89SRafael Vanoni Polanczyk node_init = 1; 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 131cc1a9a89SRafael Vanoni Polanczyk /* 132cc1a9a89SRafael Vanoni Polanczyk * Access the state file, mmap it and initialize the shared lock. 133cc1a9a89SRafael Vanoni Polanczyk * file_type tells us whether we had access to the state file or 134cc1a9a89SRafael Vanoni Polanczyk * created a temporary one. 135cc1a9a89SRafael Vanoni Polanczyk */ 136ad2de435SRichard Lowe if (map_state() == -1) 137ad2de435SRichard Lowe return (-1); 138cc1a9a89SRafael Vanoni Polanczyk 139cc1a9a89SRafael Vanoni Polanczyk /* 140cc1a9a89SRafael Vanoni Polanczyk * Acquire the lock 141cc1a9a89SRafael Vanoni Polanczyk */ 142cc1a9a89SRafael Vanoni Polanczyk for (;;) { 143cc1a9a89SRafael Vanoni Polanczyk if ((ret = mutex_lock(&data->lock)) == 0) 144cc1a9a89SRafael Vanoni Polanczyk break; 145cc1a9a89SRafael Vanoni Polanczyk else 146cc1a9a89SRafael Vanoni Polanczyk switch (ret) { 147cc1a9a89SRafael Vanoni Polanczyk case EOWNERDEAD: 148cc1a9a89SRafael Vanoni Polanczyk revalidate_data(&system_node); 149cc1a9a89SRafael Vanoni Polanczyk (void) mutex_consistent(&data->lock); 150cc1a9a89SRafael Vanoni Polanczyk (void) mutex_unlock(&data->lock); 151cc1a9a89SRafael Vanoni Polanczyk break; 152cc1a9a89SRafael Vanoni Polanczyk case ENOTRECOVERABLE: 153cc1a9a89SRafael Vanoni Polanczyk return (ret); 154cc1a9a89SRafael Vanoni Polanczyk } 155cc1a9a89SRafael Vanoni Polanczyk } 156cc1a9a89SRafael Vanoni Polanczyk 157cc1a9a89SRafael Vanoni Polanczyk /* State file is either new or is temporary, get a random clock seq */ 158cc1a9a89SRafael Vanoni Polanczyk if (data->state.clock == 0) { 159cc1a9a89SRafael Vanoni Polanczyk data->state.clock = get_random(); 1607c478bd9Sstevel@tonic-gate non_unique++; 1617c478bd9Sstevel@tonic-gate } 162cc1a9a89SRafael Vanoni Polanczyk 163cc1a9a89SRafael Vanoni Polanczyk if (memcmp(&system_node, &data->state.node, sizeof (uuid_node_t)) != 0) 164cc1a9a89SRafael Vanoni Polanczyk data->state.clock++; 165cc1a9a89SRafael Vanoni Polanczyk 166cc1a9a89SRafael Vanoni Polanczyk get_current_time(×tamp); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 169cc1a9a89SRafael Vanoni Polanczyk * If timestamp is not set or is not in the past, bump 170cc1a9a89SRafael Vanoni Polanczyk * data->state.clock 1717c478bd9Sstevel@tonic-gate */ 172cc1a9a89SRafael Vanoni Polanczyk if ((data->state.ts == 0) || (data->state.ts >= timestamp)) { 173cc1a9a89SRafael Vanoni Polanczyk data->state.clock++; 174cc1a9a89SRafael Vanoni Polanczyk data->state.ts = timestamp; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if (non_unique) 1787c478bd9Sstevel@tonic-gate system_node.nodeID[0] |= 0x80; 179cc1a9a89SRafael Vanoni Polanczyk 180cc1a9a89SRafael Vanoni Polanczyk /* Stuff fields into the UUID struct */ 181cc1a9a89SRafael Vanoni Polanczyk format_uuid(uuid, data->state.clock, timestamp, system_node); 182cc1a9a89SRafael Vanoni Polanczyk 183cc1a9a89SRafael Vanoni Polanczyk (void) mutex_unlock(&data->lock); 184cc1a9a89SRafael Vanoni Polanczyk 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 189cc1a9a89SRafael Vanoni Polanczyk * Fills system_node with Ethernet address if available, 1907c478bd9Sstevel@tonic-gate * else fills random numbers 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate static void 1937c478bd9Sstevel@tonic-gate gen_ethernet_address(uuid_node_t *system_node) 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate uchar_t node[6]; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (get_ethernet_address(system_node) != 0) { 1987c478bd9Sstevel@tonic-gate fill_random_bytes(node, 6); 1997c478bd9Sstevel@tonic-gate (void) memcpy(system_node->nodeID, node, 6); 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * use 8:0:20 with the multicast bit set 2027c478bd9Sstevel@tonic-gate * to avoid namespace collisions. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate system_node->nodeID[0] = 0x88; 2057c478bd9Sstevel@tonic-gate system_node->nodeID[1] = 0x00; 2067c478bd9Sstevel@tonic-gate system_node->nodeID[2] = 0x20; 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* 211cc1a9a89SRafael Vanoni Polanczyk * Formats a UUID, given the clock_seq timestamp, and node address. 212cc1a9a89SRafael Vanoni Polanczyk * Fills in passed-in pointer with the resulting uuid. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate static void 215cc1a9a89SRafael Vanoni Polanczyk format_uuid(struct uuid *uuid, uint16_t clock_seq, 2167c478bd9Sstevel@tonic-gate uuid_time_t timestamp, uuid_node_t node) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * First set up the first 60 bits from the timestamp 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate uuid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF); 2237c478bd9Sstevel@tonic-gate uuid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF); 224cc1a9a89SRafael Vanoni Polanczyk uuid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate /* 2277c478bd9Sstevel@tonic-gate * This is version 1, so say so in the UUID version field (4 bits) 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate uuid->time_hi_and_version |= (1 << 12); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Now do the clock sequence 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate uuid->clock_seq_low = clock_seq & 0xFF; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * We must save the most-significant 2 bits for the reserved field 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * The variant for this format is the 2 high bits set to 10, 2437c478bd9Sstevel@tonic-gate * so here it is 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved |= 0x80; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /* 2487c478bd9Sstevel@tonic-gate * write result to passed-in pointer 2497c478bd9Sstevel@tonic-gate */ 2507c478bd9Sstevel@tonic-gate (void) memcpy(&uuid->node_addr, &node, sizeof (uuid->node_addr)); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate /* 254cc1a9a89SRafael Vanoni Polanczyk * Opens/creates the state file, falling back to a tmp 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate static int 257cc1a9a89SRafael Vanoni Polanczyk map_state() 2587c478bd9Sstevel@tonic-gate { 259cc1a9a89SRafael Vanoni Polanczyk FILE *tmp; 2607c478bd9Sstevel@tonic-gate 261cc1a9a89SRafael Vanoni Polanczyk /* If file's mapped, return */ 262cc1a9a89SRafael Vanoni Polanczyk if (file_type != 0) 263cc1a9a89SRafael Vanoni Polanczyk return (1); 264cc1a9a89SRafael Vanoni Polanczyk 265cc1a9a89SRafael Vanoni Polanczyk if ((fd = open(STATE_LOCATION, O_RDWR)) < 0) { 266cc1a9a89SRafael Vanoni Polanczyk file_type = TEMP_FILE; 267cc1a9a89SRafael Vanoni Polanczyk 268cc1a9a89SRafael Vanoni Polanczyk if ((tmp = tmpfile()) == NULL) 2697c478bd9Sstevel@tonic-gate return (-1); 270cc1a9a89SRafael Vanoni Polanczyk else 271cc1a9a89SRafael Vanoni Polanczyk fd = fileno(tmp); 272cc1a9a89SRafael Vanoni Polanczyk } else { 273cc1a9a89SRafael Vanoni Polanczyk file_type = STATE_FILE; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 276cc1a9a89SRafael Vanoni Polanczyk (void) ftruncate(fd, (off_t)sizeof (shared_buffer_t)); 2777c478bd9Sstevel@tonic-gate 278cc1a9a89SRafael Vanoni Polanczyk /* LINTED - alignment */ 279cc1a9a89SRafael Vanoni Polanczyk data = (shared_buffer_t *)mmap(NULL, sizeof (shared_buffer_t), 280cc1a9a89SRafael Vanoni Polanczyk PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 281cc1a9a89SRafael Vanoni Polanczyk 282cc1a9a89SRafael Vanoni Polanczyk if (data == MAP_FAILED) 283cc1a9a89SRafael Vanoni Polanczyk return (-1); 284cc1a9a89SRafael Vanoni Polanczyk 285cc1a9a89SRafael Vanoni Polanczyk (void) mutex_init(&data->lock, USYNC_PROCESS|LOCK_ROBUST, 0); 286cc1a9a89SRafael Vanoni Polanczyk 287cc1a9a89SRafael Vanoni Polanczyk (void) close(fd); 288cc1a9a89SRafael Vanoni Polanczyk 289cc1a9a89SRafael Vanoni Polanczyk return (1); 290cc1a9a89SRafael Vanoni Polanczyk } 291cc1a9a89SRafael Vanoni Polanczyk 292cc1a9a89SRafael Vanoni Polanczyk static void 293cc1a9a89SRafael Vanoni Polanczyk revalidate_data(uuid_node_t *node) 294cc1a9a89SRafael Vanoni Polanczyk { 295cc1a9a89SRafael Vanoni Polanczyk int i; 296cc1a9a89SRafael Vanoni Polanczyk 297cc1a9a89SRafael Vanoni Polanczyk data->state.ts = 0; 298cc1a9a89SRafael Vanoni Polanczyk 299cc1a9a89SRafael Vanoni Polanczyk for (i = 0; i < sizeof (data->state.node.nodeID); i++) 300cc1a9a89SRafael Vanoni Polanczyk data->state.node.nodeID[i] = 0; 301cc1a9a89SRafael Vanoni Polanczyk 302cc1a9a89SRafael Vanoni Polanczyk data->state.clock = 0; 303cc1a9a89SRafael Vanoni Polanczyk 304cc1a9a89SRafael Vanoni Polanczyk gen_ethernet_address(node); 305cc1a9a89SRafael Vanoni Polanczyk bcopy(node, &node_id_cache, sizeof (uuid_node_t)); 306cc1a9a89SRafael Vanoni Polanczyk node_init = 1; 307cc1a9a89SRafael Vanoni Polanczyk } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* 310cc1a9a89SRafael Vanoni Polanczyk * Prints a nicely-formatted uuid to stdout. 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate void 3137c478bd9Sstevel@tonic-gate uuid_print(struct uuid u) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate int i; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate (void) printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid, 3187c478bd9Sstevel@tonic-gate u.time_hi_and_version, u.clock_seq_hi_and_reserved, 3197c478bd9Sstevel@tonic-gate u.clock_seq_low); 3207c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 3217c478bd9Sstevel@tonic-gate (void) printf("%2.2x", u.node_addr[i]); 3227c478bd9Sstevel@tonic-gate (void) printf("\n"); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 326*84615402SJerry Jelinek * Only called with urandmtx held. 327*84615402SJerry Jelinek * Fills/refills the cache of randomness. We know that our allocations of 328*84615402SJerry Jelinek * randomness are always much less than the total size of the cache. 329*84615402SJerry Jelinek * Tries to use /dev/urandom random number generator - if that fails for some 330*84615402SJerry Jelinek * reason, it retries MAX_RETRY times then sets rcachep to NULL so we no 331*84615402SJerry Jelinek * longer use the cache. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate static void 334*84615402SJerry Jelinek load_cache() 3357c478bd9Sstevel@tonic-gate { 336*84615402SJerry Jelinek int i, retries = 0; 337*84615402SJerry Jelinek int nbytes = RCACHE_SIZE; 338*84615402SJerry Jelinek char *buf = rcache; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate while (nbytes > 0) { 341*84615402SJerry Jelinek i = read(fd_urand, buf, nbytes); 3427c478bd9Sstevel@tonic-gate if ((i < 0) && (errno == EINTR)) { 3437c478bd9Sstevel@tonic-gate continue; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate if (i <= 0) { 3467c478bd9Sstevel@tonic-gate if (retries++ == MAX_RETRY) 3477c478bd9Sstevel@tonic-gate break; 3487c478bd9Sstevel@tonic-gate continue; 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate nbytes -= i; 3517c478bd9Sstevel@tonic-gate buf += i; 3527c478bd9Sstevel@tonic-gate retries = 0; 3537c478bd9Sstevel@tonic-gate } 354*84615402SJerry Jelinek if (nbytes == 0) 355*84615402SJerry Jelinek rcachep = rcache; 356*84615402SJerry Jelinek else 357*84615402SJerry Jelinek rcachep = NULL; 358*84615402SJerry Jelinek } 359*84615402SJerry Jelinek 360*84615402SJerry Jelinek /* 361*84615402SJerry Jelinek * Fills buf with random numbers - nbytes is the number of bytes 362*84615402SJerry Jelinek * to fill-in. Tries to use cached data from the /dev/urandom random number 363*84615402SJerry Jelinek * generator - if that fails for some reason, it uses srand48(3C) 364*84615402SJerry Jelinek */ 365*84615402SJerry Jelinek static void 366*84615402SJerry Jelinek fill_random_bytes(uchar_t *buf, int nbytes) 367*84615402SJerry Jelinek { 368*84615402SJerry Jelinek int i; 369*84615402SJerry Jelinek 370*84615402SJerry Jelinek if (fd_urand == -1) { 371*84615402SJerry Jelinek (void) mutex_lock(&urandmtx); 372*84615402SJerry Jelinek /* check again now that we have the mutex */ 373*84615402SJerry Jelinek if (fd_urand == -1) { 374*84615402SJerry Jelinek if ((fd_urand = open(URANDOM_PATH, O_RDONLY)) >= 0) 375*84615402SJerry Jelinek load_cache(); 376*84615402SJerry Jelinek } 377*84615402SJerry Jelinek (void) mutex_unlock(&urandmtx); 378*84615402SJerry Jelinek } 379*84615402SJerry Jelinek if (fd_urand >= 0 && rcachep != NULL) { 380*84615402SJerry Jelinek int cnt; 381*84615402SJerry Jelinek 382*84615402SJerry Jelinek (void) mutex_lock(&urandmtx); 383*84615402SJerry Jelinek if (rcachep != NULL && 384*84615402SJerry Jelinek (rcachep + nbytes) >= (rcache + RCACHE_SIZE)) 385*84615402SJerry Jelinek load_cache(); 386*84615402SJerry Jelinek 387*84615402SJerry Jelinek if (rcachep != NULL) { 388*84615402SJerry Jelinek for (cnt = 0; cnt < nbytes; cnt++) 389*84615402SJerry Jelinek *buf++ = *rcachep++; 390*84615402SJerry Jelinek (void) mutex_unlock(&urandmtx); 3917c478bd9Sstevel@tonic-gate return; 3927c478bd9Sstevel@tonic-gate } 393*84615402SJerry Jelinek (void) mutex_unlock(&urandmtx); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate for (i = 0; i < nbytes; i++) { 396cc1a9a89SRafael Vanoni Polanczyk *buf++ = get_random() & 0xFF; 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /* 401cc1a9a89SRafael Vanoni Polanczyk * Unpacks the structure members in "struct uuid" to a char string "uuid_t". 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate void 4047c478bd9Sstevel@tonic-gate struct_to_string(uuid_t ptr, struct uuid *uu) 4057c478bd9Sstevel@tonic-gate { 4067c478bd9Sstevel@tonic-gate uint_t tmp; 4077c478bd9Sstevel@tonic-gate uchar_t *out = ptr; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate tmp = uu->time_low; 4107c478bd9Sstevel@tonic-gate out[3] = (uchar_t)tmp; 4117c478bd9Sstevel@tonic-gate tmp >>= 8; 4127c478bd9Sstevel@tonic-gate out[2] = (uchar_t)tmp; 4137c478bd9Sstevel@tonic-gate tmp >>= 8; 4147c478bd9Sstevel@tonic-gate out[1] = (uchar_t)tmp; 4157c478bd9Sstevel@tonic-gate tmp >>= 8; 4167c478bd9Sstevel@tonic-gate out[0] = (uchar_t)tmp; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate tmp = uu->time_mid; 4197c478bd9Sstevel@tonic-gate out[5] = (uchar_t)tmp; 4207c478bd9Sstevel@tonic-gate tmp >>= 8; 4217c478bd9Sstevel@tonic-gate out[4] = (uchar_t)tmp; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate tmp = uu->time_hi_and_version; 4247c478bd9Sstevel@tonic-gate out[7] = (uchar_t)tmp; 4257c478bd9Sstevel@tonic-gate tmp >>= 8; 4267c478bd9Sstevel@tonic-gate out[6] = (uchar_t)tmp; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate tmp = uu->clock_seq_hi_and_reserved; 4297c478bd9Sstevel@tonic-gate out[8] = (uchar_t)tmp; 4307c478bd9Sstevel@tonic-gate tmp = uu->clock_seq_low; 4317c478bd9Sstevel@tonic-gate out[9] = (uchar_t)tmp; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate (void) memcpy(out+10, uu->node_addr, 6); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 438cc1a9a89SRafael Vanoni Polanczyk * Packs the values in the "uuid_t" string into "struct uuid". 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate void 4417c478bd9Sstevel@tonic-gate string_to_struct(struct uuid *uuid, uuid_t in) 4427c478bd9Sstevel@tonic-gate { 4437c478bd9Sstevel@tonic-gate uchar_t *ptr; 4447c478bd9Sstevel@tonic-gate uint_t tmp; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate ptr = in; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate tmp = *ptr++; 4497c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4507c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4517c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4527c478bd9Sstevel@tonic-gate uuid->time_low = tmp; 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate tmp = *ptr++; 4557c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4567c478bd9Sstevel@tonic-gate uuid->time_mid = tmp; 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate tmp = *ptr++; 4597c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4607c478bd9Sstevel@tonic-gate uuid->time_hi_and_version = tmp; 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate tmp = *ptr++; 4637c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved = tmp; 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate tmp = *ptr++; 4667c478bd9Sstevel@tonic-gate uuid->clock_seq_low = tmp; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate (void) memcpy(uuid->node_addr, ptr, 6); 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate /* 473cc1a9a89SRafael Vanoni Polanczyk * Generates UUID based on DCE Version 4 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate void 4767c478bd9Sstevel@tonic-gate uuid_generate_random(uuid_t uu) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate struct uuid uuid; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate if (uu == NULL) 4817c478bd9Sstevel@tonic-gate return; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate (void) memset(uu, 0, sizeof (uuid_t)); 4847c478bd9Sstevel@tonic-gate (void) memset(&uuid, 0, sizeof (struct uuid)); 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate fill_random_bytes(uu, sizeof (uuid_t)); 4877c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 4887c478bd9Sstevel@tonic-gate /* 4897c478bd9Sstevel@tonic-gate * This is version 4, so say so in the UUID version field (4 bits) 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate uuid.time_hi_and_version |= (1 << 14); 4927c478bd9Sstevel@tonic-gate /* 4937c478bd9Sstevel@tonic-gate * we don't want the bit 1 to be set also which is for version 1 4947c478bd9Sstevel@tonic-gate */ 4957c478bd9Sstevel@tonic-gate uuid.time_hi_and_version &= VER1_MASK; 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate /* 4987c478bd9Sstevel@tonic-gate * The variant for this format is the 2 high bits set to 10, 4997c478bd9Sstevel@tonic-gate * so here it is 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate uuid.clock_seq_hi_and_reserved |= 0x80; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate /* 5047c478bd9Sstevel@tonic-gate * Set MSB of Ethernet address to 1 to indicate that it was generated 5057c478bd9Sstevel@tonic-gate * randomly 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate uuid.node_addr[0] |= 0x80; 5087c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate /* 512cc1a9a89SRafael Vanoni Polanczyk * Generates UUID based on DCE Version 1. 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate void 5157c478bd9Sstevel@tonic-gate uuid_generate_time(uuid_t uu) 5167c478bd9Sstevel@tonic-gate { 5177c478bd9Sstevel@tonic-gate struct uuid uuid; 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate if (uu == NULL) 5207c478bd9Sstevel@tonic-gate return; 5217c478bd9Sstevel@tonic-gate 522cc1a9a89SRafael Vanoni Polanczyk if (uuid_create(&uuid) < 0) { 5237c478bd9Sstevel@tonic-gate uuid_generate_random(uu); 5247c478bd9Sstevel@tonic-gate return; 5257c478bd9Sstevel@tonic-gate } 526cc1a9a89SRafael Vanoni Polanczyk 5277c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* 531cc1a9a89SRafael Vanoni Polanczyk * Creates a new UUID. The uuid will be generated based on high-quality 532cc1a9a89SRafael Vanoni Polanczyk * randomness from /dev/urandom, if available by calling uuid_generate_random. 533cc1a9a89SRafael Vanoni Polanczyk * If it failed to generate UUID then uuid_generate will call 534cc1a9a89SRafael Vanoni Polanczyk * uuid_generate_time. 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate void 5377c478bd9Sstevel@tonic-gate uuid_generate(uuid_t uu) 5387c478bd9Sstevel@tonic-gate { 5397c478bd9Sstevel@tonic-gate if (uu == NULL) { 5407c478bd9Sstevel@tonic-gate return; 5417c478bd9Sstevel@tonic-gate } 542*84615402SJerry Jelinek if (fd_urand == -1) { 543*84615402SJerry Jelinek (void) mutex_lock(&urandmtx); 544*84615402SJerry Jelinek /* check again now that we have the mutex */ 545*84615402SJerry Jelinek if (fd_urand == -1) { 546*84615402SJerry Jelinek if ((fd_urand = open(URANDOM_PATH, O_RDONLY)) >= 0) 547*84615402SJerry Jelinek load_cache(); 548*84615402SJerry Jelinek } 549*84615402SJerry Jelinek (void) mutex_unlock(&urandmtx); 550*84615402SJerry Jelinek } 551*84615402SJerry Jelinek if (fd_urand >= 0) { 5527c478bd9Sstevel@tonic-gate uuid_generate_random(uu); 5537c478bd9Sstevel@tonic-gate } else { 5547c478bd9Sstevel@tonic-gate (void) uuid_generate_time(uu); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 559cc1a9a89SRafael Vanoni Polanczyk * Copies the UUID variable src to dst. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate void 5627c478bd9Sstevel@tonic-gate uuid_copy(uuid_t dst, uuid_t src) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate (void) memcpy(dst, src, UUID_LEN); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate /* 568cc1a9a89SRafael Vanoni Polanczyk * Sets the value of the supplied uuid variable uu, to the NULL value. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate void 5717c478bd9Sstevel@tonic-gate uuid_clear(uuid_t uu) 5727c478bd9Sstevel@tonic-gate { 5737c478bd9Sstevel@tonic-gate (void) memset(uu, 0, UUID_LEN); 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate /* 577cc1a9a89SRafael Vanoni Polanczyk * This function converts the supplied UUID uu from the internal 5787c478bd9Sstevel@tonic-gate * binary format into a 36-byte string (plus trailing null char) 579cc1a9a89SRafael Vanoni Polanczyk * and stores this value in the character string pointed to by out. 5807c478bd9Sstevel@tonic-gate */ 5817c478bd9Sstevel@tonic-gate void 5827c478bd9Sstevel@tonic-gate uuid_unparse(uuid_t uu, char *out) 5837c478bd9Sstevel@tonic-gate { 5847c478bd9Sstevel@tonic-gate struct uuid uuid; 5857c478bd9Sstevel@tonic-gate uint16_t clock_seq; 5867c478bd9Sstevel@tonic-gate char etheraddr[13]; 5877c478bd9Sstevel@tonic-gate int index = 0, i; 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate /* basic sanity checking */ 5907c478bd9Sstevel@tonic-gate if (uu == NULL) { 5917c478bd9Sstevel@tonic-gate return; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate /* XXX user should have allocated enough memory */ 5957c478bd9Sstevel@tonic-gate /* 5966be356c5Sgz161490 * if (strlen(out) < UUID_PRINTABLE_STRING_LENGTH) { 5977c478bd9Sstevel@tonic-gate * return; 5987c478bd9Sstevel@tonic-gate * } 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 6017c478bd9Sstevel@tonic-gate clock_seq = uuid.clock_seq_hi_and_reserved; 6027c478bd9Sstevel@tonic-gate clock_seq = (clock_seq << 8) | uuid.clock_seq_low; 6037c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 6047c478bd9Sstevel@tonic-gate (void) sprintf(ðeraddr[index++], "%.2x", uuid.node_addr[i]); 6057c478bd9Sstevel@tonic-gate index++; 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate etheraddr[index] = '\0'; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate (void) snprintf(out, 25, "%08x-%04x-%04x-%04x-", 610cc1a9a89SRafael Vanoni Polanczyk uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq); 6116be356c5Sgz161490 (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH); 6127c478bd9Sstevel@tonic-gate } 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 615cc1a9a89SRafael Vanoni Polanczyk * The uuid_is_null function compares the value of the supplied 6167c478bd9Sstevel@tonic-gate * UUID variable uu to the NULL value. If the value is equal 6177c478bd9Sstevel@tonic-gate * to the NULL UUID, 1 is returned, otherwise 0 is returned. 6187c478bd9Sstevel@tonic-gate */ 6197c478bd9Sstevel@tonic-gate int 6207c478bd9Sstevel@tonic-gate uuid_is_null(uuid_t uu) 6217c478bd9Sstevel@tonic-gate { 6227c478bd9Sstevel@tonic-gate int i; 6237c478bd9Sstevel@tonic-gate uuid_t null_uu; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate (void) memset(null_uu, 0, sizeof (uuid_t)); 6267c478bd9Sstevel@tonic-gate i = memcmp(uu, null_uu, sizeof (uuid_t)); 6277c478bd9Sstevel@tonic-gate if (i == 0) { 6287c478bd9Sstevel@tonic-gate /* uu is NULL uuid */ 6297c478bd9Sstevel@tonic-gate return (1); 6307c478bd9Sstevel@tonic-gate } else { 6317c478bd9Sstevel@tonic-gate return (0); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate /* 636cc1a9a89SRafael Vanoni Polanczyk * uuid_parse converts the UUID string given by 'in' into the 6377c478bd9Sstevel@tonic-gate * internal uuid_t format. The input UUID is a string of the form 6387c478bd9Sstevel@tonic-gate * cefa7a9c-1dd2-11b2-8350-880020adbeef in printf(3C) format. 6397c478bd9Sstevel@tonic-gate * Upon successfully parsing the input string, UUID is stored 6407c478bd9Sstevel@tonic-gate * in the location pointed to by uu 6417c478bd9Sstevel@tonic-gate */ 6427c478bd9Sstevel@tonic-gate int 6437c478bd9Sstevel@tonic-gate uuid_parse(char *in, uuid_t uu) 6447c478bd9Sstevel@tonic-gate { 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate char *ptr, buf[3]; 6477c478bd9Sstevel@tonic-gate int i; 6487c478bd9Sstevel@tonic-gate struct uuid uuid; 6497c478bd9Sstevel@tonic-gate uint16_t clock_seq; 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate /* do some sanity checking */ 6527c478bd9Sstevel@tonic-gate if ((strlen(in) != 36) || (uu == NULL) || (in[36] != '\0')) { 6537c478bd9Sstevel@tonic-gate return (-1); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate ptr = in; 6577c478bd9Sstevel@tonic-gate for (i = 0; i < 36; i++, ptr++) { 6587c478bd9Sstevel@tonic-gate if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) { 6597c478bd9Sstevel@tonic-gate if (*ptr != '-') { 6607c478bd9Sstevel@tonic-gate return (-1); 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate } else { 6637c478bd9Sstevel@tonic-gate if (!isxdigit(*ptr)) { 6647c478bd9Sstevel@tonic-gate return (-1); 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate uuid.time_low = strtoul(in, NULL, 16); 6707c478bd9Sstevel@tonic-gate uuid.time_mid = strtoul(in+9, NULL, 16); 6717c478bd9Sstevel@tonic-gate uuid.time_hi_and_version = strtoul(in+14, NULL, 16); 6727c478bd9Sstevel@tonic-gate clock_seq = strtoul(in+19, NULL, 16); 6737c478bd9Sstevel@tonic-gate uuid.clock_seq_hi_and_reserved = (clock_seq & 0xFF00) >> 8; 6747c478bd9Sstevel@tonic-gate uuid.clock_seq_low = (clock_seq & 0xFF); 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate ptr = in+24; 6777c478bd9Sstevel@tonic-gate buf[2] = '\0'; 6787c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 6797c478bd9Sstevel@tonic-gate buf[0] = *ptr++; 6807c478bd9Sstevel@tonic-gate buf[1] = *ptr++; 6817c478bd9Sstevel@tonic-gate uuid.node_addr[i] = strtoul(buf, NULL, 16); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 6847c478bd9Sstevel@tonic-gate return (0); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* 688cc1a9a89SRafael Vanoni Polanczyk * uuid_time extracts the time at which the supplied UUID uu 6897c478bd9Sstevel@tonic-gate * was created. This function can only extract the creation 6907c478bd9Sstevel@tonic-gate * time for UUIDs created with the uuid_generate_time function. 6917c478bd9Sstevel@tonic-gate * The time at which the UUID was created, in seconds and 6927c478bd9Sstevel@tonic-gate * microseconds since the epoch is stored in the location 6937c478bd9Sstevel@tonic-gate * pointed to by ret_tv. 6947c478bd9Sstevel@tonic-gate */ 6957c478bd9Sstevel@tonic-gate time_t 6967c478bd9Sstevel@tonic-gate uuid_time(uuid_t uu, struct timeval *ret_tv) 6977c478bd9Sstevel@tonic-gate { 6987c478bd9Sstevel@tonic-gate struct uuid uuid; 6997c478bd9Sstevel@tonic-gate uint_t high; 7007c478bd9Sstevel@tonic-gate struct timeval tv; 7017c478bd9Sstevel@tonic-gate u_longlong_t clock_reg; 7027c478bd9Sstevel@tonic-gate uint_t tmp; 7037c478bd9Sstevel@tonic-gate uint8_t clk; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 7067c478bd9Sstevel@tonic-gate tmp = (uuid.time_hi_and_version & 0xF000) >> 12; 7077c478bd9Sstevel@tonic-gate clk = uuid.clock_seq_hi_and_reserved; 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */ 7107c478bd9Sstevel@tonic-gate if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) { 7117c478bd9Sstevel@tonic-gate return (-1); 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); 7147c478bd9Sstevel@tonic-gate clock_reg = uuid.time_low | ((u_longlong_t)high << 32); 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000; 7177c478bd9Sstevel@tonic-gate tv.tv_sec = clock_reg / 10000000; 7187c478bd9Sstevel@tonic-gate tv.tv_usec = (clock_reg % 10000000) / 10; 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate if (ret_tv) { 7217c478bd9Sstevel@tonic-gate *ret_tv = tv; 7227c478bd9Sstevel@tonic-gate } 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate return (tv.tv_sec); 7257c478bd9Sstevel@tonic-gate } 726