1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
22fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte 
27fcf3ce44SJohn Forte 
28fcf3ce44SJohn Forte #include <fstream>
29fcf3ce44SJohn Forte #include "fcntl.h"
30fcf3ce44SJohn Forte #include "Handle.h"
31fcf3ce44SJohn Forte #include "Trace.h"
32fcf3ce44SJohn Forte #include "Exceptions.h"
33fcf3ce44SJohn Forte #include "sun_fc.h"
34*8edd08a7SToomas Soome #include <unistd.h>
35fcf3ce44SJohn Forte 
36fcf3ce44SJohn Forte #ifdef  __cplusplus
37fcf3ce44SJohn Forte extern "C" {
38fcf3ce44SJohn Forte #endif
39fcf3ce44SJohn Forte 
get_random_bytes(HBA_UINT8 * ptr,size_t len)40fcf3ce44SJohn Forte void get_random_bytes(HBA_UINT8 *ptr, size_t len) {
41fcf3ce44SJohn Forte 	int fd = open("/dev/urandom", O_RDONLY);
42fcf3ce44SJohn Forte 	size_t resid = len;
43fcf3ce44SJohn Forte 	ssize_t bytes;
44fcf3ce44SJohn Forte 
45fcf3ce44SJohn Forte 	while (resid != 0) {
46fcf3ce44SJohn Forte 		bytes = read(fd, ptr, resid);
47fcf3ce44SJohn Forte 		ptr += bytes;
48fcf3ce44SJohn Forte 		resid -= bytes;
49fcf3ce44SJohn Forte 	}
50fcf3ce44SJohn Forte 	close (fd);
51fcf3ce44SJohn Forte 	return;
52fcf3ce44SJohn Forte }
53fcf3ce44SJohn Forte 
Sun_fcAdapterCreateWWN(HBA_HANDLE handle,HBA_UINT32 portindex,HBA_WWN * nwwn,HBA_WWN * pwwn,HBA_WWN * OUI,HBA_INT32 method)54fcf3ce44SJohn Forte HBA_STATUS Sun_fcAdapterCreateWWN(HBA_HANDLE handle,
55fcf3ce44SJohn Forte     HBA_UINT32 portindex, HBA_WWN *nwwn, HBA_WWN *pwwn,
56fcf3ce44SJohn Forte     HBA_WWN *OUI, HBA_INT32 method) {
57fcf3ce44SJohn Forte 	HBA_UINT8	randombyte[5] = {0};
58fcf3ce44SJohn Forte 	HBA_WWN		randomwwn = {0};
59fcf3ce44SJohn Forte 	int		index = 0;
60fcf3ce44SJohn Forte 
61fcf3ce44SJohn Forte         Trace log("Sun_fcAdapterCreateWWN");
62fcf3ce44SJohn Forte 
63fcf3ce44SJohn Forte         if ((nwwn == NULL) || (pwwn == NULL)) {
64fcf3ce44SJohn Forte                 log.userError(
65fcf3ce44SJohn Forte                     "NULL WWN pointer");
66fcf3ce44SJohn Forte                 return (HBA_STATUS_ERROR_ARG);
67fcf3ce44SJohn Forte         }
68fcf3ce44SJohn Forte 	if (method == HBA_CREATE_WWN_FACTORY) {
69fcf3ce44SJohn Forte 		return (HBA_STATUS_ERROR_NOT_SUPPORTED);
70fcf3ce44SJohn Forte 	}
71fcf3ce44SJohn Forte 
72fcf3ce44SJohn Forte         try {
73fcf3ce44SJohn Forte 		/* create EUI-64 Mapped WWN */
74fcf3ce44SJohn Forte 		if (OUI == NULL) {
75fcf3ce44SJohn Forte 			/* if no OUI spec'd, used one of Sun's */
76fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x0;
77fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x0;
78fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x7D;
79fcf3ce44SJohn Forte 		} else {
80fcf3ce44SJohn Forte 			memcpy(randomwwn.wwn, OUI->wwn, sizeof(HBA_WWN));
81fcf3ce44SJohn Forte 			index += 3;
82fcf3ce44SJohn Forte 		}
83fcf3ce44SJohn Forte 		/*
84fcf3ce44SJohn Forte 		 * for EUI-64 mapped, shift OUI first byte right two bits
85fcf3ce44SJohn Forte 		 * then set top two bits to 11
86fcf3ce44SJohn Forte 		 */
87fcf3ce44SJohn Forte 		randomwwn.wwn[0] = randomwwn.wwn[0] >> 2;
88fcf3ce44SJohn Forte 		randomwwn.wwn[0] = randomwwn.wwn[0] | 0xc0;
89fcf3ce44SJohn Forte 
90fcf3ce44SJohn Forte 		/* now create and add 40 random bits */
91fcf3ce44SJohn Forte 		get_random_bytes(randombyte, 5);
92fcf3ce44SJohn Forte 		memcpy(randomwwn.wwn+index, randombyte, 5);
93fcf3ce44SJohn Forte 
94fcf3ce44SJohn Forte 		memcpy(nwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
95fcf3ce44SJohn Forte 
96fcf3ce44SJohn Forte 		/* toggle lowest bit, to make NWWN and PWWN unique */
97fcf3ce44SJohn Forte 		randomwwn.wwn[7] = randomwwn.wwn[7] ^ 1;
98fcf3ce44SJohn Forte 		memcpy(pwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
99fcf3ce44SJohn Forte 
100fcf3ce44SJohn Forte                 return (HBA_STATUS_OK);
101fcf3ce44SJohn Forte         } catch (HBAException &e) {
102fcf3ce44SJohn Forte                 return (e.getErrorCode());
103fcf3ce44SJohn Forte         } catch (...) {
104fcf3ce44SJohn Forte                 log.internalError(
105fcf3ce44SJohn Forte                     "Uncaught exception");
106fcf3ce44SJohn Forte                 return (HBA_STATUS_ERROR);
107fcf3ce44SJohn Forte         }
108fcf3ce44SJohn Forte }
109fcf3ce44SJohn Forte #ifdef  __cplusplus
110fcf3ce44SJohn Forte }
111fcf3ce44SJohn Forte #endif
112