xref: /netbsd-src/sys/kern/kern_uuid.c (revision 3e23190f1c82a9f6bc326cc87b33fd5df5408f72)
1*3e23190fSriastradh /*	$NetBSD: kern_uuid.c,v 1.20 2014/10/05 10:00:03 riastradh Exp $	*/
272489e1eStsarna 
372489e1eStsarna /*
472489e1eStsarna  * Copyright (c) 2002 Marcel Moolenaar
572489e1eStsarna  * All rights reserved.
672489e1eStsarna  *
772489e1eStsarna  * Redistribution and use in source and binary forms, with or without
872489e1eStsarna  * modification, are permitted provided that the following conditions
972489e1eStsarna  * are met:
1072489e1eStsarna  *
1172489e1eStsarna  * 1. Redistributions of source code must retain the above copyright
1272489e1eStsarna  *    notice, this list of conditions and the following disclaimer.
1372489e1eStsarna  * 2. Redistributions in binary form must reproduce the above copyright
1472489e1eStsarna  *    notice, this list of conditions and the following disclaimer in the
1572489e1eStsarna  *    documentation and/or other materials provided with the distribution.
1672489e1eStsarna  *
1772489e1eStsarna  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1872489e1eStsarna  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1972489e1eStsarna  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2072489e1eStsarna  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2172489e1eStsarna  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2272489e1eStsarna  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2372489e1eStsarna  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2472489e1eStsarna  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2572489e1eStsarna  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2672489e1eStsarna  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27e910f908Sthorpej  *
28e910f908Sthorpej  * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12 13:34:11 rse Exp $
2972489e1eStsarna  */
3072489e1eStsarna 
3172489e1eStsarna #include <sys/cdefs.h>
32*3e23190fSriastradh __KERNEL_RCSID(0, "$NetBSD: kern_uuid.c,v 1.20 2014/10/05 10:00:03 riastradh Exp $");
3372489e1eStsarna 
3472489e1eStsarna #include <sys/param.h>
3566f47404Sriastradh #include <sys/cprng.h>
3672489e1eStsarna #include <sys/endian.h>
3766f47404Sriastradh #include <sys/syscallargs.h>
3872489e1eStsarna #include <sys/systm.h>
3972489e1eStsarna #include <sys/uuid.h>
4072489e1eStsarna 
4172489e1eStsarna /*
4272489e1eStsarna  * See also:
4372489e1eStsarna  *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
4472489e1eStsarna  *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
4572489e1eStsarna  */
4672489e1eStsarna 
4772489e1eStsarna CTASSERT(sizeof(struct uuid) == 16);
4872489e1eStsarna 
4972489e1eStsarna static void
uuid_generate(struct uuid * uuid)5066f47404Sriastradh uuid_generate(struct uuid *uuid)
5172489e1eStsarna {
5272489e1eStsarna 
5366f47404Sriastradh 	/* Randomly generate the content.  */
5466f47404Sriastradh 	cprng_fast(uuid, sizeof(*uuid));
5572489e1eStsarna 
5666f47404Sriastradh 	/* Set the version number to 4.  */
5766f47404Sriastradh 	uuid->time_hi_and_version &= ~(uint32_t)0xf000;
5866f47404Sriastradh 	uuid->time_hi_and_version |= 0x4000;
5972489e1eStsarna 
6066f47404Sriastradh 	/* Fix the reserved bits.  */
6166f47404Sriastradh 	uuid->clock_seq_hi_and_reserved &= ~(uint8_t)0x40;
6266f47404Sriastradh 	uuid->clock_seq_hi_and_reserved |= 0x80;
63fb6f7962Sjoerg }
64fb6f7962Sjoerg 
65fb6f7962Sjoerg int
sys_uuidgen(struct lwp * l,const struct sys_uuidgen_args * uap,register_t * retval)66fb6f7962Sjoerg sys_uuidgen(struct lwp *l, const struct sys_uuidgen_args *uap, register_t *retval)
67fb6f7962Sjoerg {
6866f47404Sriastradh 	struct uuid *store, tmp;
6966f47404Sriastradh 	unsigned count;
7066f47404Sriastradh 	int error;
7166f47404Sriastradh 
7272489e1eStsarna 	/*
7372489e1eStsarna 	 * Limit the number of UUIDs that can be created at the same time
7472489e1eStsarna 	 * to some arbitrary number. This isn't really necessary, but I
7572489e1eStsarna 	 * like to have some sort of upper-bound that's less than 2G :-)
7672489e1eStsarna 	 * XXX needs to be tunable.
7772489e1eStsarna 	 */
7872489e1eStsarna 	if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
7972489e1eStsarna 		return (EINVAL);
8072489e1eStsarna 
8166f47404Sriastradh 	for (store = SCARG(uap,store), count = SCARG(uap,count);
8266f47404Sriastradh 	     count > 0;
8366f47404Sriastradh 	     store++, count--) {
8466f47404Sriastradh 		uuid_generate(&tmp);
85*3e23190fSriastradh 		error = copyout(&tmp, store, sizeof tmp);
8666f47404Sriastradh 		if (error)
8766f47404Sriastradh 			return error;
8866f47404Sriastradh 	}
8966f47404Sriastradh 
9066f47404Sriastradh 	return 0;
917e2790cfSdsl }
9272489e1eStsarna 
93fb6f7962Sjoerg int
uuidgen(struct uuid * store,int count)94fb6f7962Sjoerg uuidgen(struct uuid *store, int count)
95fb6f7962Sjoerg {
9666f47404Sriastradh 
9766f47404Sriastradh 	while (--count > 0)
9866f47404Sriastradh 		uuid_generate(store++);
9966f47404Sriastradh 	return 0;
10072489e1eStsarna }
10172489e1eStsarna 
10272489e1eStsarna int
uuid_snprintf(char * buf,size_t sz,const struct uuid * uuid)103e910f908Sthorpej uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
10472489e1eStsarna {
10572489e1eStsarna 
10666f47404Sriastradh 	return snprintf(buf, sz,
10766f47404Sriastradh 	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
10866f47404Sriastradh 	    uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
10966f47404Sriastradh 	    uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
11066f47404Sriastradh 	    uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3],
11166f47404Sriastradh 	    uuid->node[4], uuid->node[5]);
11272489e1eStsarna }
11372489e1eStsarna 
11472489e1eStsarna int
uuid_printf(const struct uuid * uuid)115e910f908Sthorpej uuid_printf(const struct uuid *uuid)
11672489e1eStsarna {
117e910f908Sthorpej 	char buf[UUID_STR_LEN];
11872489e1eStsarna 
119e910f908Sthorpej 	(void) uuid_snprintf(buf, sizeof(buf), uuid);
12072489e1eStsarna 	printf("%s", buf);
121e910f908Sthorpej 	return (0);
12272489e1eStsarna }
12372489e1eStsarna 
12472489e1eStsarna /*
125e910f908Sthorpej  * Encode/Decode UUID into octet-stream.
12672489e1eStsarna  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
12772489e1eStsarna  *
12872489e1eStsarna  * 0                   1                   2                   3
12972489e1eStsarna  *   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
13072489e1eStsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13172489e1eStsarna  *  |                          time_low                             |
13272489e1eStsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13372489e1eStsarna  *  |       time_mid                |         time_hi_and_version   |
13472489e1eStsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13572489e1eStsarna  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
13672489e1eStsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13772489e1eStsarna  *  |                         node (2-5)                            |
13872489e1eStsarna  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13972489e1eStsarna  */
14072489e1eStsarna 
141e910f908Sthorpej void
uuid_enc_le(void * buf,const struct uuid * uuid)142e910f908Sthorpej uuid_enc_le(void *buf, const struct uuid *uuid)
143e910f908Sthorpej {
144e910f908Sthorpej 	uint8_t *p = buf;
14572489e1eStsarna 	int i;
14672489e1eStsarna 
14772489e1eStsarna 	le32enc(p, uuid->time_low);
14872489e1eStsarna 	le16enc(p + 4, uuid->time_mid);
14972489e1eStsarna 	le16enc(p + 6, uuid->time_hi_and_version);
15072489e1eStsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
15172489e1eStsarna 	p[9] = uuid->clock_seq_low;
15272489e1eStsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
15372489e1eStsarna 		p[10 + i] = uuid->node[i];
15472489e1eStsarna }
15572489e1eStsarna 
15672489e1eStsarna void
uuid_dec_le(void const * buf,struct uuid * uuid)157e910f908Sthorpej uuid_dec_le(void const *buf, struct uuid *uuid)
15872489e1eStsarna {
159e910f908Sthorpej 	const uint8_t *p = buf;
16072489e1eStsarna 	int i;
16172489e1eStsarna 
16272489e1eStsarna 	uuid->time_low = le32dec(p);
16372489e1eStsarna 	uuid->time_mid = le16dec(p + 4);
16472489e1eStsarna 	uuid->time_hi_and_version = le16dec(p + 6);
16572489e1eStsarna 	uuid->clock_seq_hi_and_reserved = p[8];
16672489e1eStsarna 	uuid->clock_seq_low = p[9];
16772489e1eStsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
16872489e1eStsarna 		uuid->node[i] = p[10 + i];
16972489e1eStsarna }
170e910f908Sthorpej 
17172489e1eStsarna void
uuid_enc_be(void * buf,const struct uuid * uuid)172e910f908Sthorpej uuid_enc_be(void *buf, const struct uuid *uuid)
17372489e1eStsarna {
174e910f908Sthorpej 	uint8_t *p = buf;
17572489e1eStsarna 	int i;
17672489e1eStsarna 
17772489e1eStsarna 	be32enc(p, uuid->time_low);
17872489e1eStsarna 	be16enc(p + 4, uuid->time_mid);
17972489e1eStsarna 	be16enc(p + 6, uuid->time_hi_and_version);
18072489e1eStsarna 	p[8] = uuid->clock_seq_hi_and_reserved;
18172489e1eStsarna 	p[9] = uuid->clock_seq_low;
18272489e1eStsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
18372489e1eStsarna 		p[10 + i] = uuid->node[i];
18472489e1eStsarna }
18572489e1eStsarna 
18672489e1eStsarna void
uuid_dec_be(void const * buf,struct uuid * uuid)187e910f908Sthorpej uuid_dec_be(void const *buf, struct uuid *uuid)
18872489e1eStsarna {
189e910f908Sthorpej 	const uint8_t *p = buf;
19072489e1eStsarna 	int i;
19172489e1eStsarna 
19272489e1eStsarna 	uuid->time_low = be32dec(p);
1937c3f3854Splunky 	uuid->time_mid = be16dec(p + 4);
19472489e1eStsarna 	uuid->time_hi_and_version = be16dec(p + 6);
19572489e1eStsarna 	uuid->clock_seq_hi_and_reserved = p[8];
19672489e1eStsarna 	uuid->clock_seq_low = p[9];
19772489e1eStsarna 	for (i = 0; i < _UUID_NODE_LEN; i++)
19872489e1eStsarna 		uuid->node[i] = p[10 + i];
19972489e1eStsarna }
200