xref: /illumos-gate/usr/src/uts/common/os/iscsiboot_prop.c (revision 4eaa471005973e11a6110b69fe990530b3b95a38)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Commmon routines, handling iscsi boot props
29  */
30 
31 #include <sys/types.h>
32 #include <sys/bootprops.h>
33 #include <sys/cmn_err.h>
34 #include <sys/socket.h>
35 #include <sys/kmem.h>
36 #include <netinet/in.h>
37 
38 extern void *memset(void *s, int c, size_t n);
39 extern int memcmp(const void *s1, const void *s2, size_t n);
40 extern void bcopy(const void *s1, void *s2, size_t n);
41 extern size_t strlen(const char *s);
42 static void kinet_ntoa(char *buf, void *in, int af);
43 extern ib_boot_prop_t *iscsiboot_prop;
44 
45 int  iscsi_print_bootprop	=	0;
46 
47 #define	ISCSI_BOOTPROP_BUFLEN	256
48 
49 #ifndef	NULL
50 #define	NULL	0
51 #endif
52 
53 static void
54 iscsi_bootprop_print(int level, char *str)
55 {
56 	if (str == NULL) {
57 		return;
58 	}
59 	if (iscsi_print_bootprop == 1) {
60 		cmn_err(level, "%s", str);
61 	}
62 }
63 
64 static void
65 iscsi_print_initiator_property(ib_ini_prop_t *ibinitp)
66 {
67 	char	outbuf[ISCSI_BOOTPROP_BUFLEN] = {0};
68 
69 	if (ibinitp == NULL) {
70 		return;
71 	}
72 
73 	if (ibinitp->ini_name != NULL) {
74 		(void) sprintf(outbuf,
75 		    "Initiator Name : %s\n",
76 		    ibinitp->ini_name);
77 		iscsi_bootprop_print(CE_CONT, outbuf);
78 	}
79 
80 	if (ibinitp->ini_chap_name != NULL) {
81 		(void) memset(outbuf, 0, ISCSI_BOOTPROP_BUFLEN);
82 		(void) sprintf(outbuf,
83 		    "Initiator CHAP Name  : %s\n",
84 		    ibinitp->ini_chap_name);
85 
86 		iscsi_bootprop_print(CE_CONT, outbuf);
87 	}
88 }
89 
90 static void
91 iscsi_print_nic_property(ib_nic_prop_t *nicp)
92 {
93 	char	outbuf[ISCSI_BOOTPROP_BUFLEN] = {0};
94 	char	ipaddr[50]  =	{0};
95 	int	n	    =	0;
96 
97 	if (nicp == NULL) {
98 		return;
99 	}
100 
101 	kinet_ntoa(ipaddr, &nicp->nic_ip_u, nicp->sin_family);
102 	n = snprintf(outbuf, ISCSI_BOOTPROP_BUFLEN,
103 	    "Local IP addr  : %s\n", ipaddr);
104 
105 	(void) memset(ipaddr, 0, 50);
106 	kinet_ntoa(ipaddr, &nicp->nic_gw_u, nicp->sin_family);
107 	n = n + snprintf(outbuf + n, ISCSI_BOOTPROP_BUFLEN - n,
108 	    "Local gateway  : %s\n", ipaddr);
109 
110 	(void) memset(ipaddr, 0, 50);
111 	kinet_ntoa(ipaddr, &nicp->nic_dhcp_u, nicp->sin_family);
112 	n = n + snprintf(outbuf + n, ISCSI_BOOTPROP_BUFLEN - n,
113 	    "Local DHCP     : %s\n", ipaddr);
114 
115 	(void) snprintf(outbuf + n, ISCSI_BOOTPROP_BUFLEN - n,
116 	    "Local MAC      : %02x:%02x:%02x:%02x:%02x:%02x\n",
117 	    nicp->nic_mac[0],
118 	    nicp->nic_mac[1],
119 	    nicp->nic_mac[2],
120 	    nicp->nic_mac[3],
121 	    nicp->nic_mac[4],
122 	    nicp->nic_mac[5]);
123 
124 	iscsi_bootprop_print(CE_CONT, outbuf);
125 }
126 
127 static void
128 iscsi_print_tgt_property(ib_tgt_prop_t *itgtp)
129 {
130 	char	outbuf[ISCSI_BOOTPROP_BUFLEN] = {0};
131 	char	ipaddr[50]  =	{0};
132 
133 	if (itgtp == NULL) {
134 		return;
135 	}
136 
137 	if (itgtp->tgt_name != NULL) {
138 		(void) memset(outbuf, 0, ISCSI_BOOTPROP_BUFLEN);
139 		(void) sprintf(outbuf,
140 		    "Target Name    : %s\n",
141 		    itgtp->tgt_name);
142 		iscsi_bootprop_print(CE_CONT, outbuf);
143 	}
144 
145 	kinet_ntoa(ipaddr, &itgtp->tgt_ip_u, itgtp->sin_family);
146 	(void) sprintf(outbuf,
147 	    "Target IP      : %s\n"
148 	    "Target Port    : %d\n"
149 	    "Boot LUN       : %02x%02x-%02x%02x-%02x%02x-%02x%02x\n",
150 	    ipaddr,
151 	    itgtp->tgt_port,
152 	    itgtp->tgt_boot_lun[0],
153 	    itgtp->tgt_boot_lun[1],
154 	    itgtp->tgt_boot_lun[2],
155 	    itgtp->tgt_boot_lun[3],
156 	    itgtp->tgt_boot_lun[4],
157 	    itgtp->tgt_boot_lun[5],
158 	    itgtp->tgt_boot_lun[6],
159 	    itgtp->tgt_boot_lun[7]);
160 	iscsi_bootprop_print(CE_CONT, outbuf);
161 
162 	if (itgtp->tgt_chap_name != NULL) {
163 		(void) memset(outbuf, 0, ISCSI_BOOTPROP_BUFLEN);
164 		(void) sprintf(outbuf,
165 		    "CHAP Name      : %s\n",
166 		    itgtp->tgt_chap_name);
167 		iscsi_bootprop_print(CE_CONT, outbuf);
168 	}
169 }
170 
171 void
172 iscsi_print_boot_property()
173 {
174 	if (iscsiboot_prop == NULL) {
175 		return;
176 	}
177 
178 	iscsi_print_initiator_property(
179 	    &iscsiboot_prop->boot_init);
180 
181 	iscsi_print_nic_property(&iscsiboot_prop->boot_nic);
182 
183 	iscsi_print_tgt_property(&iscsiboot_prop->boot_tgt);
184 }
185 
186 void
187 iscsi_boot_free_ini(ib_ini_prop_t *init)
188 {
189 	if (init == NULL) {
190 		return;
191 	}
192 
193 	if (init->ini_name != NULL) {
194 		kmem_free(init->ini_name, strlen((char *)init->ini_name) + 1);
195 		init->ini_name = NULL;
196 	}
197 	if (init->ini_chap_name != NULL) {
198 		kmem_free(init->ini_chap_name,
199 		    strlen((char *)init->ini_chap_name) + 1);
200 		init->ini_chap_name = NULL;
201 	}
202 	if (init->ini_chap_sec != NULL) {
203 		kmem_free(init->ini_chap_sec,
204 		    strlen((char *)init->ini_chap_sec) + 1);
205 		init->ini_chap_sec = NULL;
206 	}
207 }
208 
209 void
210 iscsi_boot_free_tgt(ib_tgt_prop_t *target)
211 {
212 	if (target == NULL) {
213 		return;
214 	}
215 
216 	if (target->tgt_name != NULL) {
217 		kmem_free(target->tgt_name,
218 		    strlen((char *)target->tgt_name) + 1);
219 		target->tgt_name = NULL;
220 	}
221 	if (target->tgt_chap_name != NULL) {
222 		kmem_free(target->tgt_chap_name,
223 		    strlen((char *)target->tgt_chap_name) + 1);
224 		target->tgt_chap_name = NULL;
225 	}
226 	if (target->tgt_chap_sec != NULL) {
227 		kmem_free(target->tgt_chap_sec,
228 		    strlen((char *)target->tgt_chap_sec) + 1);
229 		target->tgt_chap_sec = NULL;
230 	}
231 }
232 
233 /*
234  * Free the memory used by boot property.
235  */
236 void
237 iscsi_boot_prop_free()
238 {
239 	ib_boot_prop_t	*tmp;
240 
241 	if (iscsiboot_prop == NULL) {
242 		return;
243 	}
244 	tmp = iscsiboot_prop;
245 	iscsiboot_prop = NULL;
246 	iscsi_boot_free_ini(&(tmp->boot_init));
247 	iscsi_boot_free_tgt(&(tmp->boot_tgt));
248 }
249 
250 static void
251 kinet_ntoa(char *buf, void *in, int af)
252 {
253 	unsigned char   *p =    NULL;
254 	int	i = 0;
255 
256 	if (buf == NULL || in == NULL) {
257 		return;
258 	}
259 	p = (unsigned char *)in;
260 	if (af == AF_INET) {
261 		(void) sprintf(buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
262 	} else {
263 		for (i = 0; i < 14; i = i + 2) {
264 			(void) sprintf(buf, "%02x%02x:", p[i], p[i+1]);
265 			buf = buf + 5;
266 		}
267 		(void) sprintf(buf, "%02x%02x", p[i], p[i+1]);
268 	}
269 }
270