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  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #ifndef _ISCSIT_COMMON_H_
26 #define	_ISCSIT_COMMON_H_
27 
28 #ifdef	_KERNEL
29 #include <sys/nvpair.h>
30 #else
31 #include <libnvpair.h>
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define	ISCSIT_API_VERS0		0
39 
40 #define	ISCSIT_MODNAME		"iscsit"
41 #define	ISCSIT_NODE		"/devices/pseudo/iscsit@0:iscsit"
42 
43 typedef enum {
44 	ITCFG_SUCCESS = 0,
45 	ITCFG_INVALID,
46 	ITCFG_TGT_CREATE_ERR,
47 	ITCFG_MISC_ERR
48 } it_cfg_status_t;
49 
50 /*
51  * This structure is passed back to the driver during ISCSIT_IOC_ENABLE_SVC
52  * in order to provide the fully qualified hostname for use as the EID
53  * by iSNS.
54  */
55 
56 #define	ISCSIT_MAX_HOSTNAME_LEN	256
57 
58 typedef struct iscsit_hostinfo_s {
59 	uint32_t	length;
60 	char		fqhn[ISCSIT_MAX_HOSTNAME_LEN];
61 } iscsit_hostinfo_t;
62 
63 #define	ISCSIT_IOC_SET_CONFIG		1
64 #define	ISCSIT_IOC_GET_STATE		2
65 #define	ISCSIT_IOC_ENABLE_SVC		101
66 #define	ISCSIT_IOC_DISABLE_SVC		102
67 
68 /* XXX Rationalize these with other error values (used in it_smf.c */
69 #define	ITADM_SUCCESS		0
70 #define	ITADM_FATAL_ERROR	0x1
71 #define	ITADM_NO_MEM		0x2
72 #define	ITADM_INVALID		0x4
73 #define	ITADM_NODATA		0x8
74 #define	ITADM_PERM		0x10
75 
76 
77 #define	PROP_AUTH		"auth"
78 #define	PROP_ALIAS		"alias"
79 #define	PROP_CHAP_USER		"chapuser"
80 #define	PROP_CHAP_SECRET	"chapsecret"
81 #define	PROP_TARGET_CHAP_USER	"targetchapuser"
82 #define	PROP_TARGET_CHAP_SECRET	"targetchapsecret"
83 #define	PROP_RADIUS_SERVER	"radiusserver"
84 #define	PROP_RADIUS_SECRET	"radiussecret"
85 #define	PROP_ISNS_ENABLED	"isns"
86 #define	PROP_ISNS_SERVER	"isnsserver"
87 #define	PROP_OLD_TARGET_NAME	"oldtargetname"
88 
89 #define	PA_AUTH_RADIUS		"radius"
90 #define	PA_AUTH_CHAP		"chap"
91 #define	PA_AUTH_NONE		"none"
92 
93 typedef struct {
94 	int		set_cfg_vers;
95 	int		set_cfg_pnvlist_len;
96 	caddr_t		set_cfg_pnvlist;
97 } iscsit_ioc_set_config_t;
98 
99 typedef struct {
100 	int		getst_vers;
101 	int		getst_pnvlist_len;
102 	char		*getst_pnvlist;
103 } iscsit_ioc_getstate_t;
104 
105 #ifdef _SYSCALL32
106 typedef struct {
107 	int		set_cfg_vers;
108 	int		set_cfg_pnvlist_len;
109 	caddr32_t	set_cfg_pnvlist;
110 } iscsit_ioc_set_config32_t;
111 
112 typedef struct {
113 	int		getst_vers;
114 	int		getst_pnvlist_len;
115 	caddr32_t	getst_pnvlist;
116 } iscsit_ioc_getstate32_t;
117 #endif /* _SYSCALL32 */
118 
119 /* Shared user/kernel structures */
120 /*  Maximum size of a Target Portal Group name */
121 #define	MAX_TPG_NAMELEN		256		/* XXX */
122 
123 /* Maximum size of an iSCSI Target Node name */
124 #define	MAX_ISCSI_NODENAMELEN	256		/* XXX */
125 
126 /*
127  * A target portal group tag is a binding between a target and a target
128  * portal group along with a numerical value associated with that binding.
129  * The numerical identifier is used as the 'target portal group tag' defined
130  * in RFC3720.
131  *
132  *  tpgt_tpg_name	The name of the target portal group associated with
133  *			this target portal group tag.
134  *  tpgt_generation	Generation number which is incremented each time the
135  *			structure changes.
136  *  tpgt_next		Next target portal group tag in th list of target portal
137  *			group tags.  If tpgt_next is NUL, then this is the last
138  *			target portal group in the list.
139  *  tpgt_tag		A numerical identifier that uniquely identifies a
140  *			target portal group within the associated target node.
141  */
142 typedef struct it_tpgt_s {
143 	char			tpgt_tpg_name[MAX_TPG_NAMELEN];
144 	uint64_t		tpgt_generation;
145 	struct it_tpgt_s	*tpgt_next;
146 	uint16_t		tpgt_tag;
147 } it_tpgt_t;
148 
149 /*
150  * An iSCSI target node is represented by an it_tgt_structure.  Each
151  * target node includes a list of associated target portal group tags
152  * and a list of properties.
153  *
154  *  tgt_name		The iSCSI target node name in either IQN or EUI
155  *			format (see RFC3720).
156  *  tgt_generation	Generation number which is incremented each time
157  *			the structure changes.
158  *  tgt_next		Next target in the list of targets.  If tgt_next
159  *			is NULL, then this is the last target in the list.
160  *  tgt_tpgt_list	A linked list representing the current target
161  *			portal group tags associated with this target.
162  *  tgt_tpgt_count	The number of currently defined target portal
163  *			group tags.
164  *  tgt_properties	An nvlist representation of the properties
165  *			associated with this target.  This list can be
166  *			manipulated using libnvpair(3lib), and should be
167  *			validated and stored using it_tgt_setprop().
168  *
169  * Target nvlist Properties:
170  *
171  *  nvlist Key		Type		Valid Values
172  *  --------------------------------------------------------------------
173  *  targetchapuser	string		any string or "none" to remove
174  *  targetchapsecret	string		string of at least 12 characters
175  *					but not more than 255 characters.
176  *					secret will be base64 encoded when
177  *					stored.
178  *  alias		string		any string or "none" to remove
179  *  auth		string		"radius", "chap", or "none"
180  *
181  */
182 typedef struct it_tgt_s {
183 	char			tgt_name[MAX_ISCSI_NODENAMELEN];
184 	uint64_t		tgt_generation;
185 	struct it_tgt_s		*tgt_next;
186 	it_tpgt_t		*tgt_tpgt_list;
187 	uint32_t		tgt_tpgt_count;
188 	nvlist_t		*tgt_properties;
189 } it_tgt_t;
190 
191 /*
192  * A target portal is represented by an IP address and a listening
193  * TCP port.
194  *
195  *  portal_addr		sockaddr_storage structure representing the
196  *			IPv4 or IPv6 address and TCP port associated
197  *			with the portal.
198  *  portal_next		Next portal in the list of portals.  If
199  *			portal_next is NULL, this is the last portal
200  *			in the list.
201  */
202 typedef struct it_portal_s {
203 	struct sockaddr_storage portal_addr;
204 	struct it_portal_s	*portal_next;
205 } it_portal_t;
206 
207 /*
208  * A portal is an IP address and TCP port and a portal group is a set
209  * of portals.  Each defined portal belongs to exactly one portal group.
210  * Applications can associate a target portal group with a particular
211  * target using a target portal group name.  Initiators can only connect
212  * to targets through the portals associated with the target's target
213  * portal group tags.
214  *
215  *  tpg_name		Identifier for the target portal group.
216  *  tpg_generation	Generation number which is incremented each
217  *			time this structure changes.
218  *  tpg_next		Next target portal group in the list of target
219  *			portal groups.  If tpg_next is NULL, this is the
220  *			last target portal group in the list.
221  *  tpg_portal_count	Number of it_portal_t structures in the list.
222  *  tpg_portal_list	Linked list of it_portal_t structures.
223  */
224 typedef struct it_tpg_s {
225 	char			tpg_name[MAX_TPG_NAMELEN];
226 	uint64_t		tpg_generation;
227 	struct it_tpg_s		*tpg_next;
228 	uint32_t		tpg_portal_count;
229 	it_portal_t		*tpg_portal_list;
230 } it_tpg_t;
231 
232 /*
233  * A context representing a remote iSCSI initiator node.  The purpose
234  * of this structure is to maintain information specific to a remote
235  * initiator such as the CHAP username and CHAP secret.
236  *
237  *  ini_name		the iSCSI node name of the remote initiator.
238  *  ini_generation	Generation number which is incremented each
239  *			time this structure changes.
240  *  ini_next		Next initiator in the list of initiators.
241  *			If ini_next is NULL, this is the last initiator
242  *			in the list.
243  *  ini_properties	Name/Value list containing the properties
244  *			associated with the initiator context.  This list
245  *			can be manipulated using libnvpair(3lib), and should
246  *			be validated and stored using it_ini_setprop().
247  *
248  * Initiator nvlist Properties:
249  *
250  *  nvlist Key		Type		Valid Values
251  *  --------------------------------------------------------------------
252  *  chapuser		string		any string
253  *  chapsecret		string		string of at least 12 characters
254  *					but not more than 255 characters.
255  *					secret will be base64 encoded when
256  *					stored.
257  */
258 typedef struct it_ini_s {
259 	char		ini_name[MAX_ISCSI_NODENAMELEN];
260 	uint64_t	ini_generation;
261 	struct it_ini_s	*ini_next;
262 	nvlist_t	*ini_properties;
263 } it_ini_t;
264 
265 
266 /*
267  * This structure represents a complete configuration for the iscsit
268  * port provider.  In addition to the global configuration, it_config_t
269  * includes lists of child objects including targets, target portal
270  * groups and initiator contexts.  Each object includes a "generation"
271  * value which is used by the iscsit kernel driver to identify changes
272  * from one configuration update to the next.
273  *
274  *  stmf_token		A uint64_t that contains the value returned from a
275  *			successful call to stmfGetProviderDataProt(3STMF).
276  *			This token is used to verify that the configuration
277  *			data persistently stored in COMSTAR has not been
278  *			modified since this version was loaded.
279  *  config_version	Version number for this configuration structure
280  *  config_tgt_list	Linked list of target contexts representing the
281  *			currently defined targets.  Applications can add
282  *			targets to or remove targets from this list using
283  *			the it_tgt_create and it_tgt_delete functions.
284  *  config_tgt_count	The number of currently defined targets.
285  *  config_tpg_list	Linked list of target portal group contexts.
286  *			Applications can add or remove target portal groups
287  *			to/from this list using the it_tpg_create and
288  *			it_tpg_delete functions.
289  *  config_tpg_count	The number of currently defined target portal groups
290  *  config_ini_list	Linked list of initiator contexts.  Applications
291  *			can add initiator contexts or remove initiator
292  *			contexts from this list using the it_ini_create
293  *			and it_ini_delete functions.
294  *  config_ini_count	The number of currently defined initiator contexts.
295  *  config_global_properties
296  *			Name/Value list representing the current global
297  *			property settings.  This list can be manipulated
298  *			using libnvpair(3lib), and should be validated
299  *			and stored using it_config_setprop().
300  *  config_isns_svr_list
301  *			Linked list of currently defined iSNS servers.
302  *			Applications can add or remove iSNS servers by
303  *			using the it_config_setprop() function and changing
304  *			the array of iSNS servers stored in the "isnsserver"
305  *			property.
306  *  config_isns_svr_count
307  *			The number of currently defined iSNS servers.
308  *
309  * Global nvlist Properties:
310  *
311  *  nvlist Key		Type		Valid Values
312  *  --------------------------------------------------------------------
313  *  alias		string		any string
314  *  auth		string		"radius", "chap", or "none"
315  *  isns		boolean		B_TRUE, B_FALSE
316  *  isnsserver		string array	Array of portal specifications of
317  *					the form IPaddress:port.  Port
318  *					is optional; if not specified, the
319  *					default iSNS port number of 3205 will
320  *					be used.  IPv6 addresses should
321  *					be enclosed in square brackets '[' ']'.
322  *					If "none" is specified, all defined
323  *					iSNS servers will be removed from the
324  *					configuration.
325  *  radiusserver	string		IPaddress:port specification as
326  *					described for 'isnsserver'.
327  *  radiussecret	string		string of at least 12 characters
328  *					but not more than 255 characters.
329  *					secret will be base64 encoded when
330  *					stored.
331  */
332 typedef struct it_config_s {
333 	uint64_t		stmf_token;
334 	uint32_t		config_version;
335 	it_tgt_t		*config_tgt_list;
336 	uint32_t		config_tgt_count;
337 	it_tpg_t		*config_tpg_list;
338 	uint32_t		config_tpg_count;
339 	it_ini_t		*config_ini_list;
340 	uint32_t		config_ini_count;
341 	it_portal_t		*config_isns_svr_list;
342 	uint32_t		config_isns_svr_count;
343 	nvlist_t		*config_global_properties;
344 } it_config_t;
345 
346 
347 /*  Functions to convert iSCSI target structures to/from nvlists. */
348 int
349 it_config_to_nv(it_config_t *cfg, nvlist_t **nvl);
350 
351 /*
352  * nvlist version of config is 3 list-of-list, + 1 proplist.  arrays
353  * are interesting, but lists-of-lists are more useful when doing
354  * individual lookups when we later add support for it.  Also, no
355  * need to store name in individual struct representation.
356  */
357 int
358 it_nv_to_config(nvlist_t *nvl, it_config_t **cfg);
359 
360 int
361 it_nv_to_tgtlist(nvlist_t *nvl, uint32_t *count, it_tgt_t **tgtlist);
362 
363 int
364 it_tgtlist_to_nv(it_tgt_t *tgtlist, nvlist_t **nvl);
365 
366 int
367 it_tgt_to_nv(it_tgt_t *tgt, nvlist_t **nvl);
368 
369 int
370 it_nv_to_tgt(nvlist_t *nvl, char *name, it_tgt_t **tgt);
371 
372 int
373 it_tpgt_to_nv(it_tpgt_t *tpgt, nvlist_t **nvl);
374 
375 int
376 it_nv_to_tpgt(nvlist_t *nvl, char *name, it_tpgt_t **tpgt);
377 
378 int
379 it_tpgtlist_to_nv(it_tpgt_t *tpgtlist, nvlist_t **nvl);
380 
381 int
382 it_nv_to_tpgtlist(nvlist_t *nvl, uint32_t *count, it_tpgt_t **tpgtlist);
383 
384 int
385 it_tpg_to_nv(it_tpg_t *tpg, nvlist_t **nvl);
386 
387 int
388 it_nv_to_tpg(nvlist_t *nvl, char *name, it_tpg_t **tpg);
389 
390 int
391 it_tpglist_to_nv(it_tpg_t *tpglist, nvlist_t **nvl);
392 
393 int
394 it_nv_to_tpglist(nvlist_t *nvl, uint32_t *count, it_tpg_t **tpglist);
395 
396 int
397 it_ini_to_nv(it_ini_t *ini, nvlist_t **nvl);
398 
399 int
400 it_nv_to_ini(nvlist_t *nvl, char *name, it_ini_t **ini);
401 
402 int
403 it_inilist_to_nv(it_ini_t *inilist, nvlist_t **nvl);
404 
405 int
406 it_nv_to_inilist(nvlist_t *nvl, uint32_t *count, it_ini_t **inilist);
407 
408 it_tgt_t *
409 it_tgt_lookup(it_config_t *cfg, char *tgt_name);
410 
411 it_tpg_t *
412 it_tpg_lookup(it_config_t *cfg, char *tpg_name);
413 
414 it_portal_t *
415 it_sns_svr_lookup(it_config_t *cfg, struct sockaddr_storage *sa);
416 
417 it_portal_t *
418 it_portal_lookup(it_tpg_t *cfg_tpg, struct sockaddr_storage *sa);
419 
420 int
421 it_sa_compare(struct sockaddr_storage *sa1, struct sockaddr_storage *sa2);
422 
423 /*
424  * Convert a sockaddr to the string representation, suitable for
425  * storing in an nvlist or printing out in a list.
426  */
427 int
428 sockaddr_to_str(struct sockaddr_storage *sa, char **addr);
429 
430 /*
431  * Convert a char string to a sockaddr structure
432  *
433  * default_port should be the port to be used, if not specified
434  * as part of the supplied string 'arg'.
435  */
436 struct sockaddr_storage *
437 it_common_convert_sa(char *arg, struct sockaddr_storage *buf,
438     uint32_t default_port);
439 
440 /*
441  * Convert an string array of IP-addr:port to a portal list
442  */
443 int
444 it_array_to_portallist(char **arr, uint32_t count, uint32_t default_port,
445     it_portal_t **portallist, uint32_t *list_count);
446 
447 /*
448  * Function:  it_config_free_cmn()
449  *
450  * Free any resources associated with the it_config_t structure.
451  *
452  * Parameters:
453  *    cfg       A C representation of the current iSCSI configuration
454  */
455 void
456 it_config_free_cmn(it_config_t *cfg);
457 
458 /*
459  * Function:  it_tgt_free_cmn()
460  *
461  * Frees an it_tgt_t structure.  If tgt_next is not NULL, frees
462  * all structures in the list.
463  */
464 void
465 it_tgt_free_cmn(it_tgt_t *tgt);
466 
467 /*
468  * Function:  it_tpgt_free_cmn()
469  *
470  * Deallocates resources of an it_tpgt_t structure.  If tpgt->next
471  * is not NULL, frees all members of the list.
472  */
473 void
474 it_tpgt_free_cmn(it_tpgt_t *tpgt);
475 
476 /*
477  * Function:  it_tpg_free_cmn()
478  *
479  * Deallocates resources associated with an it_tpg_t structure.
480  * If tpg->next is not NULL, frees all members of the list.
481  */
482 void
483 it_tpg_free_cmn(it_tpg_t *tpg);
484 
485 /*
486  * Function:  it_ini_free_cmn()
487  *
488  * Deallocates resources of an it_ini_t structure. If ini->next is
489  * not NULL, frees all members of the list.
490  */
491 void
492 it_ini_free_cmn(it_ini_t *ini);
493 
494 /*
495  * Function:  iscsi_binary_to_base64_str()
496  *
497  * Encodes a byte array into a base64 string.
498  */
499 int
500 iscsi_binary_to_base64_str(uint8_t *in_buf, int in_buf_len,
501     char *base64_str_buf, int base64_buf_len);
502 
503 /*
504  * Function:  iscsi_base64_str_to_binary()
505  *
506  * Decodes a base64 string into a byte array
507  */
508 int
509 iscsi_base64_str_to_binary(char *hstr, int hstr_len,
510     uint8_t *binary, int binary_buf_len, int *out_len);
511 
512 #ifdef __cplusplus
513 }
514 #endif
515 
516 #endif /* _ISCSIT_COMMON_H_ */
517