Home | History | Annotate | Line # | Download | only in kern
kern_uuid.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: kern_uuid.c,v 1.2 2004/08/30 02:56:03 thorpej Exp $	*/
      2  1.1   tsarna 
      3  1.1   tsarna /*
      4  1.1   tsarna  * Copyright (c) 2002 Marcel Moolenaar
      5  1.1   tsarna  * All rights reserved.
      6  1.1   tsarna  *
      7  1.1   tsarna  * Redistribution and use in source and binary forms, with or without
      8  1.1   tsarna  * modification, are permitted provided that the following conditions
      9  1.1   tsarna  * are met:
     10  1.1   tsarna  *
     11  1.1   tsarna  * 1. Redistributions of source code must retain the above copyright
     12  1.1   tsarna  *    notice, this list of conditions and the following disclaimer.
     13  1.1   tsarna  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1   tsarna  *    notice, this list of conditions and the following disclaimer in the
     15  1.1   tsarna  *    documentation and/or other materials provided with the distribution.
     16  1.1   tsarna  *
     17  1.1   tsarna  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1   tsarna  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1   tsarna  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   tsarna  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1   tsarna  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1   tsarna  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1   tsarna  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1   tsarna  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1   tsarna  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1   tsarna  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.2  thorpej  *
     28  1.2  thorpej  * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12 13:34:11 rse Exp $
     29  1.1   tsarna  */
     30  1.1   tsarna 
     31  1.1   tsarna #include <sys/cdefs.h>
     32  1.2  thorpej __KERNEL_RCSID(0, "$NetBSD: kern_uuid.c,v 1.2 2004/08/30 02:56:03 thorpej Exp $");
     33  1.1   tsarna 
     34  1.1   tsarna #include <sys/param.h>
     35  1.1   tsarna #include <sys/endian.h>
     36  1.1   tsarna #include <sys/kernel.h>
     37  1.1   tsarna #include <sys/lock.h>
     38  1.1   tsarna #include <sys/socket.h>
     39  1.1   tsarna #include <sys/systm.h>
     40  1.1   tsarna #include <sys/uuid.h>
     41  1.1   tsarna 
     42  1.1   tsarna /* NetBSD */
     43  1.1   tsarna #include <sys/proc.h>
     44  1.1   tsarna #include <sys/sa.h>
     45  1.1   tsarna #include <sys/mount.h>
     46  1.1   tsarna #include <sys/syscallargs.h>
     47  1.1   tsarna #include <sys/uio.h>
     48  1.1   tsarna 
     49  1.1   tsarna #include <net/if.h>
     50  1.1   tsarna #include <net/if_dl.h>
     51  1.1   tsarna #include <net/if_types.h>
     52  1.1   tsarna 
     53  1.1   tsarna /*
     54  1.1   tsarna  * See also:
     55  1.1   tsarna  *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
     56  1.1   tsarna  *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
     57  1.1   tsarna  *
     58  1.1   tsarna  * Note that the generator state is itself an UUID, but the time and clock
     59  1.1   tsarna  * sequence fields are written in the native byte order.
     60  1.1   tsarna  */
     61  1.1   tsarna 
     62  1.1   tsarna /* XXX Do we have a similar ASSERT()? */
     63  1.1   tsarna #define CTASSERT(x)
     64  1.1   tsarna 
     65  1.1   tsarna CTASSERT(sizeof(struct uuid) == 16);
     66  1.1   tsarna 
     67  1.1   tsarna /* We use an alternative, more convenient representation in the generator. */
     68  1.1   tsarna struct uuid_private {
     69  1.1   tsarna 	union {
     70  1.1   tsarna 		uint64_t	ll;		/* internal. */
     71  1.1   tsarna 		struct {
     72  1.1   tsarna 			uint32_t	low;
     73  1.1   tsarna 			uint16_t	mid;
     74  1.1   tsarna 			uint16_t	hi;
     75  1.1   tsarna 		} x;
     76  1.1   tsarna 	} time;
     77  1.1   tsarna 	uint16_t	seq;			/* Big-endian. */
     78  1.1   tsarna 	uint16_t	node[UUID_NODE_LEN>>1];
     79  1.1   tsarna };
     80  1.1   tsarna 
     81  1.1   tsarna CTASSERT(sizeof(struct uuid_private) == 16);
     82  1.1   tsarna 
     83  1.1   tsarna static struct uuid_private uuid_last;
     84  1.1   tsarna 
     85  1.1   tsarna /* "UUID generator mutex lock" */
     86  1.1   tsarna static struct simplelock uuid_mutex = SIMPLELOCK_INITIALIZER;
     87  1.1   tsarna 
     88  1.1   tsarna /*
     89  1.1   tsarna  * Return the first MAC address we encounter or, if none was found,
     90  1.1   tsarna  * construct a sufficiently random multicast address. We don't try
     91  1.1   tsarna  * to return the same MAC address as previously returned. We always
     92  1.1   tsarna  * generate a new multicast address if no MAC address exists in the
     93  1.1   tsarna  * system.
     94  1.1   tsarna  * It would be nice to know if 'ifnet' or any of its sub-structures
     95  1.1   tsarna  * has been changed in any way. If not, we could simply skip the
     96  1.1   tsarna  * scan and safely return the MAC address we returned before.
     97  1.1   tsarna  */
     98  1.1   tsarna static void
     99  1.1   tsarna uuid_node(uint16_t *node)
    100  1.1   tsarna {
    101  1.1   tsarna 	struct ifnet *ifp;
    102  1.1   tsarna 	struct ifaddr *ifa;
    103  1.1   tsarna 	struct sockaddr_dl *sdl;
    104  1.1   tsarna 	int i, s;
    105  1.1   tsarna 
    106  1.1   tsarna 	s = splnet();
    107  1.1   tsarna 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
    108  1.1   tsarna 		/* Walk the address list */
    109  1.1   tsarna 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    110  1.1   tsarna 			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
    111  1.1   tsarna 			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
    112  1.1   tsarna 			    sdl->sdl_type == IFT_ETHER) {
    113  1.1   tsarna 				/* Got a MAC address. */
    114  1.1   tsarna 				memcpy(node, LLADDR(sdl), UUID_NODE_LEN);
    115  1.1   tsarna 				splx(s);
    116  1.1   tsarna 				return;
    117  1.1   tsarna 			}
    118  1.1   tsarna 		}
    119  1.1   tsarna 	}
    120  1.1   tsarna 	splx(s);
    121  1.1   tsarna 
    122  1.1   tsarna 	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
    123  1.1   tsarna 		node[i] = (uint16_t)arc4random();
    124  1.1   tsarna 	*((uint8_t*)node) |= 0x01;
    125  1.1   tsarna }
    126  1.1   tsarna 
    127  1.1   tsarna /*
    128  1.1   tsarna  * Get the current time as a 60 bit count of 100-nanosecond intervals
    129  1.1   tsarna  * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
    130  1.1   tsarna  * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
    131  1.1   tsarna  * Gregorian reform to the Christian calendar.
    132  1.1   tsarna  */
    133  1.1   tsarna /*
    134  1.1   tsarna  * At present, NetBSD has no timespec source, only timeval sources.  So,
    135  1.1   tsarna  * we use timeval.
    136  1.1   tsarna  */
    137  1.1   tsarna static uint64_t
    138  1.1   tsarna uuid_time(void)
    139  1.1   tsarna {
    140  1.1   tsarna 	struct timeval tv;
    141  1.1   tsarna 	uint64_t time = 0x01B21DD213814000LL;
    142  1.1   tsarna 
    143  1.1   tsarna 	microtime(&tv);
    144  1.1   tsarna 	time += (uint64_t)tv.tv_sec * 10000000LL;
    145  1.1   tsarna 	time += (uint64_t)(10 * tv.tv_usec);
    146  1.1   tsarna 	return (time & ((1LL << 60) - 1LL));
    147  1.1   tsarna }
    148  1.1   tsarna 
    149  1.2  thorpej /*
    150  1.2  thorpej  * Internal routine to actually generate the UUID.
    151  1.2  thorpej  */
    152  1.2  thorpej static void
    153  1.2  thorpej uuid_generate(struct uuid_private *uuid, uint64_t *timep, int count)
    154  1.2  thorpej {
    155  1.2  thorpej 	uint64_t time;
    156  1.2  thorpej 
    157  1.2  thorpej 	simple_lock(&uuid_mutex);
    158  1.2  thorpej 
    159  1.2  thorpej 	uuid_node(uuid->node);
    160  1.2  thorpej 	time = uuid_time();
    161  1.2  thorpej 	*timep = time;
    162  1.2  thorpej 
    163  1.2  thorpej 	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid->node[0] ||
    164  1.2  thorpej 	    uuid_last.node[1] != uuid->node[1] ||
    165  1.2  thorpej 	    uuid_last.node[2] != uuid->node[2])
    166  1.2  thorpej 		uuid->seq = (uint16_t)arc4random() & 0x3fff;
    167  1.2  thorpej 	else if (uuid_last.time.ll >= time)
    168  1.2  thorpej 		uuid->seq = (uuid_last.seq + 1) & 0x3fff;
    169  1.2  thorpej 	else
    170  1.2  thorpej 		uuid->seq = uuid_last.seq;
    171  1.2  thorpej 
    172  1.2  thorpej 	uuid_last = *uuid;
    173  1.2  thorpej 	uuid_last.time.ll = (time + count - 1) & ((1LL << 60) - 1LL);
    174  1.2  thorpej 
    175  1.2  thorpej 	simple_unlock(&uuid_mutex);
    176  1.2  thorpej }
    177  1.2  thorpej 
    178  1.1   tsarna int
    179  1.1   tsarna sys_uuidgen(struct lwp *l, void *v, register_t *retval)
    180  1.1   tsarna {
    181  1.1   tsarna 	struct sys_uuidgen_args *uap = v;
    182  1.1   tsarna 	struct uuid_private uuid;
    183  1.1   tsarna 	uint64_t time;
    184  1.1   tsarna 	int error;
    185  1.1   tsarna 
    186  1.1   tsarna 	/*
    187  1.1   tsarna 	 * Limit the number of UUIDs that can be created at the same time
    188  1.1   tsarna 	 * to some arbitrary number. This isn't really necessary, but I
    189  1.1   tsarna 	 * like to have some sort of upper-bound that's less than 2G :-)
    190  1.1   tsarna 	 * XXX needs to be tunable.
    191  1.1   tsarna 	 */
    192  1.1   tsarna 	if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
    193  1.1   tsarna 		return (EINVAL);
    194  1.1   tsarna 
    195  1.1   tsarna 	/* XXX: pre-validate accessibility to the whole of the UUID store? */
    196  1.1   tsarna 
    197  1.2  thorpej 	/* Generate the base UUID. */
    198  1.2  thorpej 	uuid_generate(&uuid, &time, SCARG(uap, count));
    199  1.1   tsarna 
    200  1.1   tsarna 	/* Set sequence and variant and deal with byte order. */
    201  1.1   tsarna 	uuid.seq = htobe16(uuid.seq | 0x8000);
    202  1.1   tsarna 
    203  1.1   tsarna 	/* XXX: this should copyout larger chunks at a time. */
    204  1.1   tsarna 	do {
    205  1.1   tsarna 		/* Set time and version (=1) and deal with byte order. */
    206  1.1   tsarna 		uuid.time.x.low = (uint32_t)time;
    207  1.1   tsarna 		uuid.time.x.mid = (uint16_t)(time >> 32);
    208  1.1   tsarna 		uuid.time.x.hi = ((uint16_t)(time >> 48) & 0xfff) | (1 << 12);
    209  1.1   tsarna 		error = copyout(&uuid, SCARG(uap,store), sizeof(uuid));
    210  1.2  thorpej 		SCARG(uap, store)++;
    211  1.2  thorpej 		SCARG(uap, count)--;
    212  1.1   tsarna 		time++;
    213  1.2  thorpej 	} while (SCARG(uap, count) > 0 && error == 0);
    214  1.1   tsarna 
    215  1.1   tsarna 	return (error);
    216  1.1   tsarna }
    217  1.1   tsarna 
    218  1.1   tsarna int
    219  1.2  thorpej uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
    220  1.1   tsarna {
    221  1.2  thorpej 	const struct uuid_private *id;
    222  1.1   tsarna 	int cnt;
    223  1.1   tsarna 
    224  1.2  thorpej 	id = (const struct uuid_private *)uuid;
    225  1.1   tsarna 	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
    226  1.1   tsarna 	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
    227  1.1   tsarna 	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
    228  1.1   tsarna 	return (cnt);
    229  1.1   tsarna }
    230  1.1   tsarna 
    231  1.1   tsarna int
    232  1.2  thorpej uuid_printf(const struct uuid *uuid)
    233  1.1   tsarna {
    234  1.2  thorpej 	char buf[UUID_STR_LEN];
    235  1.1   tsarna 
    236  1.2  thorpej 	(void) uuid_snprintf(buf, sizeof(buf), uuid);
    237  1.1   tsarna 	printf("%s", buf);
    238  1.2  thorpej 	return (0);
    239  1.1   tsarna }
    240  1.1   tsarna 
    241  1.1   tsarna /*
    242  1.2  thorpej  * Encode/Decode UUID into octet-stream.
    243  1.1   tsarna  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
    244  1.1   tsarna  *
    245  1.1   tsarna  * 0                   1                   2                   3
    246  1.1   tsarna  *   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
    247  1.1   tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    248  1.1   tsarna  *  |                          time_low                             |
    249  1.1   tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    250  1.1   tsarna  *  |       time_mid                |         time_hi_and_version   |
    251  1.1   tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    252  1.1   tsarna  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
    253  1.1   tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    254  1.1   tsarna  *  |                         node (2-5)                            |
    255  1.1   tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    256  1.1   tsarna  */
    257  1.1   tsarna 
    258  1.2  thorpej static void
    259  1.2  thorpej be16enc(void *buf, uint16_t u)
    260  1.2  thorpej {
    261  1.2  thorpej 	uint8_t *p = buf;
    262  1.2  thorpej 
    263  1.2  thorpej 	p[0] = (u >> 8) & 0xff;
    264  1.2  thorpej 	p[1] = u & 0xff;
    265  1.2  thorpej }
    266  1.2  thorpej 
    267  1.2  thorpej static void
    268  1.2  thorpej le16enc(void *buf, uint16_t u)
    269  1.2  thorpej {
    270  1.2  thorpej 	uint8_t *p = buf;
    271  1.2  thorpej 
    272  1.2  thorpej 	p[0] = u & 0xff;
    273  1.2  thorpej 	p[1] = (u >> 8) & 0xff;
    274  1.2  thorpej }
    275  1.2  thorpej 
    276  1.2  thorpej static uint16_t
    277  1.2  thorpej be16dec(const void *buf)
    278  1.2  thorpej {
    279  1.2  thorpej 	const uint8_t *p = buf;
    280  1.2  thorpej 
    281  1.2  thorpej 	return ((p[0] << 8) | p[1]);
    282  1.2  thorpej }
    283  1.2  thorpej 
    284  1.2  thorpej static uint16_t
    285  1.2  thorpej le16dec(const void *buf)
    286  1.2  thorpej {
    287  1.2  thorpej 	const uint8_t *p = buf;
    288  1.2  thorpej 
    289  1.2  thorpej 	return ((p[1] << 8) | p[0]);
    290  1.2  thorpej }
    291  1.2  thorpej 
    292  1.2  thorpej static void
    293  1.2  thorpej be32enc(void *buf, uint32_t u)
    294  1.2  thorpej {
    295  1.2  thorpej 	uint8_t *p = buf;
    296  1.2  thorpej 
    297  1.2  thorpej 	p[0] = (u >> 24) & 0xff;
    298  1.2  thorpej 	p[1] = (u >> 16) & 0xff;
    299  1.2  thorpej 	p[2] = (u >> 8) & 0xff;
    300  1.2  thorpej 	p[3] = u & 0xff;
    301  1.2  thorpej }
    302  1.2  thorpej 
    303  1.2  thorpej static void
    304  1.2  thorpej le32enc(void *buf, uint32_t u)
    305  1.2  thorpej {
    306  1.2  thorpej 	uint8_t *p = buf;
    307  1.2  thorpej 
    308  1.2  thorpej 	p[0] = u & 0xff;
    309  1.2  thorpej 	p[1] = (u >> 8) & 0xff;
    310  1.2  thorpej 	p[2] = (u >> 16) & 0xff;
    311  1.2  thorpej 	p[3] = (u >> 24) & 0xff;
    312  1.2  thorpej }
    313  1.2  thorpej 
    314  1.2  thorpej static uint32_t
    315  1.2  thorpej be32dec(const void *buf)
    316  1.2  thorpej {
    317  1.2  thorpej 	const uint8_t *p = buf;
    318  1.2  thorpej 
    319  1.2  thorpej 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
    320  1.2  thorpej }
    321  1.2  thorpej 
    322  1.2  thorpej static uint32_t
    323  1.2  thorpej le32dec(const void *buf)
    324  1.2  thorpej {
    325  1.2  thorpej 	const uint8_t *p = buf;
    326  1.2  thorpej 
    327  1.2  thorpej 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
    328  1.2  thorpej }
    329  1.2  thorpej 
    330  1.1   tsarna void
    331  1.2  thorpej uuid_enc_le(void *buf, const struct uuid *uuid)
    332  1.1   tsarna {
    333  1.2  thorpej 	uint8_t *p = buf;
    334  1.1   tsarna 	int i;
    335  1.1   tsarna 
    336  1.1   tsarna 	le32enc(p, uuid->time_low);
    337  1.1   tsarna 	le16enc(p + 4, uuid->time_mid);
    338  1.1   tsarna 	le16enc(p + 6, uuid->time_hi_and_version);
    339  1.1   tsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
    340  1.1   tsarna 	p[9] = uuid->clock_seq_low;
    341  1.1   tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    342  1.1   tsarna 		p[10 + i] = uuid->node[i];
    343  1.1   tsarna }
    344  1.1   tsarna 
    345  1.1   tsarna void
    346  1.2  thorpej uuid_dec_le(void const *buf, struct uuid *uuid)
    347  1.1   tsarna {
    348  1.2  thorpej 	const uint8_t *p = buf;
    349  1.1   tsarna 	int i;
    350  1.1   tsarna 
    351  1.1   tsarna 	uuid->time_low = le32dec(p);
    352  1.1   tsarna 	uuid->time_mid = le16dec(p + 4);
    353  1.1   tsarna 	uuid->time_hi_and_version = le16dec(p + 6);
    354  1.1   tsarna 	uuid->clock_seq_hi_and_reserved = p[8];
    355  1.1   tsarna 	uuid->clock_seq_low = p[9];
    356  1.1   tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    357  1.1   tsarna 		uuid->node[i] = p[10 + i];
    358  1.1   tsarna }
    359  1.2  thorpej 
    360  1.1   tsarna void
    361  1.2  thorpej uuid_enc_be(void *buf, const struct uuid *uuid)
    362  1.1   tsarna {
    363  1.2  thorpej 	uint8_t *p = buf;
    364  1.1   tsarna 	int i;
    365  1.1   tsarna 
    366  1.1   tsarna 	be32enc(p, uuid->time_low);
    367  1.1   tsarna 	be16enc(p + 4, uuid->time_mid);
    368  1.1   tsarna 	be16enc(p + 6, uuid->time_hi_and_version);
    369  1.1   tsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
    370  1.1   tsarna 	p[9] = uuid->clock_seq_low;
    371  1.1   tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    372  1.1   tsarna 		p[10 + i] = uuid->node[i];
    373  1.1   tsarna }
    374  1.1   tsarna 
    375  1.1   tsarna void
    376  1.2  thorpej uuid_dec_be(void const *buf, struct uuid *uuid)
    377  1.1   tsarna {
    378  1.2  thorpej 	const uint8_t *p = buf;
    379  1.1   tsarna 	int i;
    380  1.1   tsarna 
    381  1.1   tsarna 	uuid->time_low = be32dec(p);
    382  1.1   tsarna 	uuid->time_mid = le16dec(p + 4);
    383  1.1   tsarna 	uuid->time_hi_and_version = be16dec(p + 6);
    384  1.1   tsarna 	uuid->clock_seq_hi_and_reserved = p[8];
    385  1.1   tsarna 	uuid->clock_seq_low = p[9];
    386  1.1   tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    387  1.1   tsarna 		uuid->node[i] = p[10 + i];
    388  1.1   tsarna }
    389