Home | History | Annotate | Line # | Download | only in kern
kern_uuid.c revision 1.11.10.1
      1  1.11.10.1        ad /*	$NetBSD: kern_uuid.c,v 1.11.10.1 2007/12/26 19:57:12 ad 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.11.10.1        ad __KERNEL_RCSID(0, "$NetBSD: kern_uuid.c,v 1.11.10.1 2007/12/26 19:57:12 ad 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.10        ad #include <sys/mutex.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/mount.h>
     45        1.1    tsarna #include <sys/syscallargs.h>
     46        1.1    tsarna #include <sys/uio.h>
     47        1.1    tsarna 
     48        1.1    tsarna #include <net/if.h>
     49        1.1    tsarna #include <net/if_dl.h>
     50        1.1    tsarna #include <net/if_types.h>
     51        1.1    tsarna 
     52        1.1    tsarna /*
     53        1.1    tsarna  * See also:
     54        1.1    tsarna  *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
     55        1.1    tsarna  *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
     56        1.1    tsarna  *
     57        1.1    tsarna  * Note that the generator state is itself an UUID, but the time and clock
     58        1.1    tsarna  * sequence fields are written in the native byte order.
     59        1.1    tsarna  */
     60        1.1    tsarna 
     61        1.1    tsarna /* XXX Do we have a similar ASSERT()? */
     62        1.1    tsarna #define CTASSERT(x)
     63        1.1    tsarna 
     64        1.1    tsarna CTASSERT(sizeof(struct uuid) == 16);
     65        1.1    tsarna 
     66        1.1    tsarna /* We use an alternative, more convenient representation in the generator. */
     67        1.1    tsarna struct uuid_private {
     68        1.1    tsarna 	union {
     69        1.1    tsarna 		uint64_t	ll;		/* internal. */
     70        1.1    tsarna 		struct {
     71        1.1    tsarna 			uint32_t	low;
     72        1.1    tsarna 			uint16_t	mid;
     73        1.1    tsarna 			uint16_t	hi;
     74        1.1    tsarna 		} x;
     75        1.1    tsarna 	} time;
     76        1.1    tsarna 	uint16_t	seq;			/* Big-endian. */
     77        1.1    tsarna 	uint16_t	node[UUID_NODE_LEN>>1];
     78        1.1    tsarna };
     79        1.1    tsarna 
     80        1.1    tsarna CTASSERT(sizeof(struct uuid_private) == 16);
     81        1.1    tsarna 
     82        1.1    tsarna static struct uuid_private uuid_last;
     83        1.1    tsarna 
     84        1.1    tsarna /* "UUID generator mutex lock" */
     85       1.10        ad static kmutex_t uuid_mutex;
     86       1.10        ad 
     87       1.10        ad void
     88       1.10        ad uuid_init(void)
     89       1.10        ad {
     90       1.10        ad 
     91       1.10        ad 	mutex_init(&uuid_mutex, MUTEX_DEFAULT, IPL_NONE);
     92       1.10        ad }
     93        1.1    tsarna 
     94        1.1    tsarna /*
     95        1.1    tsarna  * Return the first MAC address we encounter or, if none was found,
     96        1.1    tsarna  * construct a sufficiently random multicast address. We don't try
     97        1.1    tsarna  * to return the same MAC address as previously returned. We always
     98        1.1    tsarna  * generate a new multicast address if no MAC address exists in the
     99        1.1    tsarna  * system.
    100        1.1    tsarna  * It would be nice to know if 'ifnet' or any of its sub-structures
    101        1.1    tsarna  * has been changed in any way. If not, we could simply skip the
    102        1.1    tsarna  * scan and safely return the MAC address we returned before.
    103        1.1    tsarna  */
    104        1.1    tsarna static void
    105        1.1    tsarna uuid_node(uint16_t *node)
    106        1.1    tsarna {
    107        1.1    tsarna 	struct ifnet *ifp;
    108        1.1    tsarna 	struct ifaddr *ifa;
    109        1.1    tsarna 	struct sockaddr_dl *sdl;
    110        1.1    tsarna 	int i, s;
    111        1.1    tsarna 
    112        1.1    tsarna 	s = splnet();
    113        1.4      matt 	IFNET_FOREACH(ifp) {
    114        1.1    tsarna 		/* Walk the address list */
    115        1.4      matt 		IFADDR_FOREACH(ifa, ifp) {
    116        1.1    tsarna 			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
    117        1.1    tsarna 			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
    118        1.1    tsarna 			    sdl->sdl_type == IFT_ETHER) {
    119        1.1    tsarna 				/* Got a MAC address. */
    120       1.11    dyoung 				memcpy(node, CLLADDR(sdl), UUID_NODE_LEN);
    121        1.1    tsarna 				splx(s);
    122        1.1    tsarna 				return;
    123        1.1    tsarna 			}
    124        1.1    tsarna 		}
    125        1.1    tsarna 	}
    126        1.1    tsarna 	splx(s);
    127        1.1    tsarna 
    128        1.1    tsarna 	for (i = 0; i < (UUID_NODE_LEN>>1); i++)
    129        1.1    tsarna 		node[i] = (uint16_t)arc4random();
    130        1.1    tsarna 	*((uint8_t*)node) |= 0x01;
    131        1.1    tsarna }
    132        1.1    tsarna 
    133        1.1    tsarna /*
    134        1.1    tsarna  * Get the current time as a 60 bit count of 100-nanosecond intervals
    135        1.1    tsarna  * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
    136        1.1    tsarna  * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
    137        1.1    tsarna  * Gregorian reform to the Christian calendar.
    138        1.1    tsarna  */
    139        1.1    tsarna /*
    140        1.1    tsarna  * At present, NetBSD has no timespec source, only timeval sources.  So,
    141        1.1    tsarna  * we use timeval.
    142        1.1    tsarna  */
    143        1.1    tsarna static uint64_t
    144        1.1    tsarna uuid_time(void)
    145        1.1    tsarna {
    146        1.1    tsarna 	struct timeval tv;
    147        1.5  christos 	uint64_t xtime = 0x01B21DD213814000LL;
    148        1.1    tsarna 
    149        1.1    tsarna 	microtime(&tv);
    150        1.5  christos 	xtime += (uint64_t)tv.tv_sec * 10000000LL;
    151        1.5  christos 	xtime += (uint64_t)(10 * tv.tv_usec);
    152        1.5  christos 	return (xtime & ((1LL << 60) - 1LL));
    153        1.1    tsarna }
    154        1.1    tsarna 
    155        1.2   thorpej /*
    156        1.2   thorpej  * Internal routine to actually generate the UUID.
    157        1.2   thorpej  */
    158        1.2   thorpej static void
    159        1.2   thorpej uuid_generate(struct uuid_private *uuid, uint64_t *timep, int count)
    160        1.2   thorpej {
    161        1.5  christos 	uint64_t xtime;
    162        1.2   thorpej 
    163       1.10        ad 	mutex_enter(&uuid_mutex);
    164        1.2   thorpej 
    165        1.2   thorpej 	uuid_node(uuid->node);
    166        1.5  christos 	xtime = uuid_time();
    167        1.5  christos 	*timep = xtime;
    168        1.2   thorpej 
    169        1.2   thorpej 	if (uuid_last.time.ll == 0LL || uuid_last.node[0] != uuid->node[0] ||
    170        1.2   thorpej 	    uuid_last.node[1] != uuid->node[1] ||
    171        1.2   thorpej 	    uuid_last.node[2] != uuid->node[2])
    172        1.2   thorpej 		uuid->seq = (uint16_t)arc4random() & 0x3fff;
    173        1.5  christos 	else if (uuid_last.time.ll >= xtime)
    174        1.2   thorpej 		uuid->seq = (uuid_last.seq + 1) & 0x3fff;
    175        1.2   thorpej 	else
    176        1.2   thorpej 		uuid->seq = uuid_last.seq;
    177        1.2   thorpej 
    178        1.2   thorpej 	uuid_last = *uuid;
    179        1.5  christos 	uuid_last.time.ll = (xtime + count - 1) & ((1LL << 60) - 1LL);
    180        1.2   thorpej 
    181       1.10        ad 	mutex_exit(&uuid_mutex);
    182        1.2   thorpej }
    183        1.2   thorpej 
    184        1.1    tsarna int
    185  1.11.10.1        ad sys_uuidgen(struct lwp *l, const struct sys_uuidgen_args *uap, register_t *retval)
    186        1.1    tsarna {
    187        1.1    tsarna 	struct uuid_private uuid;
    188        1.5  christos 	uint64_t xtime;
    189        1.1    tsarna 	int error;
    190  1.11.10.1        ad 	int i;
    191        1.1    tsarna 
    192        1.1    tsarna 	/*
    193        1.1    tsarna 	 * Limit the number of UUIDs that can be created at the same time
    194        1.1    tsarna 	 * to some arbitrary number. This isn't really necessary, but I
    195        1.1    tsarna 	 * like to have some sort of upper-bound that's less than 2G :-)
    196        1.1    tsarna 	 * XXX needs to be tunable.
    197        1.1    tsarna 	 */
    198        1.1    tsarna 	if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
    199        1.1    tsarna 		return (EINVAL);
    200        1.1    tsarna 
    201        1.1    tsarna 	/* XXX: pre-validate accessibility to the whole of the UUID store? */
    202        1.1    tsarna 
    203        1.2   thorpej 	/* Generate the base UUID. */
    204        1.5  christos 	uuid_generate(&uuid, &xtime, SCARG(uap, count));
    205        1.1    tsarna 
    206        1.1    tsarna 	/* Set sequence and variant and deal with byte order. */
    207        1.1    tsarna 	uuid.seq = htobe16(uuid.seq | 0x8000);
    208        1.1    tsarna 
    209        1.1    tsarna 	/* XXX: this should copyout larger chunks at a time. */
    210  1.11.10.1        ad 	for (i = 0; i < SCARG(uap, count); xtime++, i++) {
    211        1.1    tsarna 		/* Set time and version (=1) and deal with byte order. */
    212        1.5  christos 		uuid.time.x.low = (uint32_t)xtime;
    213        1.5  christos 		uuid.time.x.mid = (uint16_t)(xtime >> 32);
    214        1.5  christos 		uuid.time.x.hi = ((uint16_t)(xtime >> 48) & 0xfff) | (1 << 12);
    215  1.11.10.1        ad 		error = copyout(&uuid, SCARG(uap,store) + i, sizeof(uuid));
    216  1.11.10.1        ad 		if (error != 0)
    217  1.11.10.1        ad 			return error;
    218  1.11.10.1        ad 	}
    219        1.1    tsarna 
    220  1.11.10.1        ad 	return 0;
    221        1.1    tsarna }
    222        1.1    tsarna 
    223        1.1    tsarna int
    224        1.2   thorpej uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
    225        1.1    tsarna {
    226        1.2   thorpej 	const struct uuid_private *id;
    227        1.1    tsarna 	int cnt;
    228        1.1    tsarna 
    229        1.2   thorpej 	id = (const struct uuid_private *)uuid;
    230        1.1    tsarna 	cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
    231        1.1    tsarna 	    id->time.x.low, id->time.x.mid, id->time.x.hi, be16toh(id->seq),
    232        1.1    tsarna 	    be16toh(id->node[0]), be16toh(id->node[1]), be16toh(id->node[2]));
    233        1.1    tsarna 	return (cnt);
    234        1.1    tsarna }
    235        1.1    tsarna 
    236        1.1    tsarna int
    237        1.2   thorpej uuid_printf(const struct uuid *uuid)
    238        1.1    tsarna {
    239        1.2   thorpej 	char buf[UUID_STR_LEN];
    240        1.1    tsarna 
    241        1.2   thorpej 	(void) uuid_snprintf(buf, sizeof(buf), uuid);
    242        1.1    tsarna 	printf("%s", buf);
    243        1.2   thorpej 	return (0);
    244        1.1    tsarna }
    245        1.1    tsarna 
    246        1.1    tsarna /*
    247        1.2   thorpej  * Encode/Decode UUID into octet-stream.
    248        1.1    tsarna  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
    249        1.1    tsarna  *
    250        1.1    tsarna  * 0                   1                   2                   3
    251        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
    252        1.1    tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    253        1.1    tsarna  *  |                          time_low                             |
    254        1.1    tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    255        1.1    tsarna  *  |       time_mid                |         time_hi_and_version   |
    256        1.1    tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    257        1.1    tsarna  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
    258        1.1    tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    259        1.1    tsarna  *  |                         node (2-5)                            |
    260        1.1    tsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    261        1.1    tsarna  */
    262        1.1    tsarna 
    263        1.1    tsarna void
    264        1.2   thorpej uuid_enc_le(void *buf, const struct uuid *uuid)
    265        1.1    tsarna {
    266        1.2   thorpej 	uint8_t *p = buf;
    267        1.1    tsarna 	int i;
    268        1.1    tsarna 
    269        1.1    tsarna 	le32enc(p, uuid->time_low);
    270        1.1    tsarna 	le16enc(p + 4, uuid->time_mid);
    271        1.1    tsarna 	le16enc(p + 6, uuid->time_hi_and_version);
    272        1.1    tsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
    273        1.1    tsarna 	p[9] = uuid->clock_seq_low;
    274        1.1    tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    275        1.1    tsarna 		p[10 + i] = uuid->node[i];
    276        1.1    tsarna }
    277        1.1    tsarna 
    278        1.1    tsarna void
    279        1.2   thorpej uuid_dec_le(void const *buf, struct uuid *uuid)
    280        1.1    tsarna {
    281        1.2   thorpej 	const uint8_t *p = buf;
    282        1.1    tsarna 	int i;
    283        1.1    tsarna 
    284        1.1    tsarna 	uuid->time_low = le32dec(p);
    285        1.1    tsarna 	uuid->time_mid = le16dec(p + 4);
    286        1.1    tsarna 	uuid->time_hi_and_version = le16dec(p + 6);
    287        1.1    tsarna 	uuid->clock_seq_hi_and_reserved = p[8];
    288        1.1    tsarna 	uuid->clock_seq_low = p[9];
    289        1.1    tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    290        1.1    tsarna 		uuid->node[i] = p[10 + i];
    291        1.1    tsarna }
    292        1.2   thorpej 
    293        1.1    tsarna void
    294        1.2   thorpej uuid_enc_be(void *buf, const struct uuid *uuid)
    295        1.1    tsarna {
    296        1.2   thorpej 	uint8_t *p = buf;
    297        1.1    tsarna 	int i;
    298        1.1    tsarna 
    299        1.1    tsarna 	be32enc(p, uuid->time_low);
    300        1.1    tsarna 	be16enc(p + 4, uuid->time_mid);
    301        1.1    tsarna 	be16enc(p + 6, uuid->time_hi_and_version);
    302        1.1    tsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
    303        1.1    tsarna 	p[9] = uuid->clock_seq_low;
    304        1.1    tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    305        1.1    tsarna 		p[10 + i] = uuid->node[i];
    306        1.1    tsarna }
    307        1.1    tsarna 
    308        1.1    tsarna void
    309        1.2   thorpej uuid_dec_be(void const *buf, struct uuid *uuid)
    310        1.1    tsarna {
    311        1.2   thorpej 	const uint8_t *p = buf;
    312        1.1    tsarna 	int i;
    313        1.1    tsarna 
    314        1.1    tsarna 	uuid->time_low = be32dec(p);
    315        1.1    tsarna 	uuid->time_mid = le16dec(p + 4);
    316        1.1    tsarna 	uuid->time_hi_and_version = be16dec(p + 6);
    317        1.1    tsarna 	uuid->clock_seq_hi_and_reserved = p[8];
    318        1.1    tsarna 	uuid->clock_seq_low = p[9];
    319        1.1    tsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
    320        1.1    tsarna 		uuid->node[i] = p[10 + i];
    321        1.1    tsarna }
    322