ctld.h revision 265513
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: stable/10/usr.sbin/ctld/ctld.h 265513 2014-05-07 07:37:55Z trasz $
30 */
31
32#ifndef CTLD_H
33#define	CTLD_H
34
35#include <sys/queue.h>
36#ifdef ICL_KERNEL_PROXY
37#include <sys/types.h>
38#include <sys/socket.h>
39#endif
40#include <stdbool.h>
41#include <libutil.h>
42
43#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
44#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
45#define	DEFAULT_BLOCKSIZE		512
46
47#define	MAX_NAME_LEN			223
48#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
49#define	MAX_BURST_LENGTH		16776192
50
51struct auth {
52	TAILQ_ENTRY(auth)		a_next;
53	struct auth_group		*a_auth_group;
54	char				*a_user;
55	char				*a_secret;
56	char				*a_mutual_user;
57	char				*a_mutual_secret;
58};
59
60struct auth_name {
61	TAILQ_ENTRY(auth_name)		an_next;
62	struct auth_group		*an_auth_group;
63	char				*an_initator_name;
64};
65
66struct auth_portal {
67	TAILQ_ENTRY(auth_portal)	ap_next;
68	struct auth_group		*ap_auth_group;
69	char				*ap_initator_portal;
70};
71
72#define	AG_TYPE_UNKNOWN			0
73#define	AG_TYPE_DENY			1
74#define	AG_TYPE_NO_AUTHENTICATION	2
75#define	AG_TYPE_CHAP			3
76#define	AG_TYPE_CHAP_MUTUAL		4
77
78struct auth_group {
79	TAILQ_ENTRY(auth_group)		ag_next;
80	struct conf			*ag_conf;
81	char				*ag_name;
82	struct target			*ag_target;
83	int				ag_type;
84	TAILQ_HEAD(, auth)		ag_auths;
85	TAILQ_HEAD(, auth_name)		ag_names;
86	TAILQ_HEAD(, auth_portal)	ag_portals;
87};
88
89struct portal {
90	TAILQ_ENTRY(portal)		p_next;
91	struct portal_group		*p_portal_group;
92	bool				p_iser;
93	char				*p_listen;
94	struct addrinfo			*p_ai;
95#ifdef ICL_KERNEL_PROXY
96	int				p_id;
97#endif
98
99	TAILQ_HEAD(, target)		p_targets;
100	int				p_socket;
101};
102
103struct portal_group {
104	TAILQ_ENTRY(portal_group)	pg_next;
105	struct conf			*pg_conf;
106	char				*pg_name;
107	struct auth_group		*pg_discovery_auth_group;
108	bool				pg_unassigned;
109	TAILQ_HEAD(, portal)		pg_portals;
110
111	uint16_t			pg_tag;
112};
113
114struct lun_option {
115	TAILQ_ENTRY(lun_option)		lo_next;
116	struct lun			*lo_lun;
117	char				*lo_name;
118	char				*lo_value;
119};
120
121struct lun {
122	TAILQ_ENTRY(lun)		l_next;
123	TAILQ_HEAD(, lun_option)	l_options;
124	struct target			*l_target;
125	int				l_lun;
126	char				*l_backend;
127	int				l_blocksize;
128	char				*l_device_id;
129	char				*l_path;
130	char				*l_serial;
131	int64_t				l_size;
132
133	int				l_ctl_lun;
134};
135
136struct target {
137	TAILQ_ENTRY(target)		t_next;
138	TAILQ_HEAD(, lun)		t_luns;
139	struct conf			*t_conf;
140	struct auth_group		*t_auth_group;
141	struct portal_group		*t_portal_group;
142	char				*t_name;
143	char				*t_alias;
144};
145
146struct conf {
147	char				*conf_pidfile_path;
148	TAILQ_HEAD(, target)		conf_targets;
149	TAILQ_HEAD(, auth_group)	conf_auth_groups;
150	TAILQ_HEAD(, portal_group)	conf_portal_groups;
151	int				conf_debug;
152	int				conf_timeout;
153	int				conf_maxproc;
154
155	uint16_t			conf_last_portal_group_tag;
156#ifdef ICL_KERNEL_PROXY
157	int				conf_portal_id;
158#endif
159	struct pidfh			*conf_pidfh;
160
161	bool				conf_default_pg_defined;
162	bool				conf_default_ag_defined;
163	bool				conf_kernel_port_on;
164};
165
166#define	CONN_SESSION_TYPE_NONE		0
167#define	CONN_SESSION_TYPE_DISCOVERY	1
168#define	CONN_SESSION_TYPE_NORMAL	2
169
170#define	CONN_DIGEST_NONE		0
171#define	CONN_DIGEST_CRC32C		1
172
173struct connection {
174	struct portal		*conn_portal;
175	struct target		*conn_target;
176	int			conn_socket;
177	int			conn_session_type;
178	char			*conn_initiator_name;
179	char			*conn_initiator_addr;
180	char			*conn_initiator_alias;
181	uint32_t		conn_cmdsn;
182	uint32_t		conn_statsn;
183	size_t			conn_max_data_segment_length;
184	size_t			conn_max_burst_length;
185	int			conn_immediate_data;
186	int			conn_header_digest;
187	int			conn_data_digest;
188};
189
190struct pdu {
191	struct connection	*pdu_connection;
192	struct iscsi_bhs	*pdu_bhs;
193	char			*pdu_data;
194	size_t			pdu_data_len;
195};
196
197#define	KEYS_MAX	1024
198
199struct keys {
200	char		*keys_names[KEYS_MAX];
201	char		*keys_values[KEYS_MAX];
202	char		*keys_data;
203	size_t		keys_data_len;
204};
205
206struct conf		*conf_new(void);
207struct conf		*conf_new_from_file(const char *path);
208struct conf		*conf_new_from_kernel(void);
209void			conf_delete(struct conf *conf);
210int			conf_verify(struct conf *conf);
211
212struct auth_group	*auth_group_new(struct conf *conf, const char *name);
213void			auth_group_delete(struct auth_group *ag);
214struct auth_group	*auth_group_find(struct conf *conf, const char *name);
215int			auth_group_set_type_str(struct auth_group *ag,
216			    const char *type);
217
218const struct auth	*auth_new_chap(struct auth_group *ag,
219			    const char *user, const char *secret);
220const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
221			    const char *user, const char *secret,
222			    const char *user2, const char *secret2);
223const struct auth	*auth_find(struct auth_group *ag,
224			    const char *user);
225
226const struct auth_name	*auth_name_new(struct auth_group *ag,
227			    const char *initiator_name);
228bool			auth_name_defined(const struct auth_group *ag);
229const struct auth_name	*auth_name_find(const struct auth_group *ag,
230			    const char *initiator_name);
231
232const struct auth_portal	*auth_portal_new(struct auth_group *ag,
233				    const char *initiator_portal);
234bool			auth_portal_defined(const struct auth_group *ag);
235const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
236				    const char *initiator_portal);
237
238struct portal_group	*portal_group_new(struct conf *conf, const char *name);
239void			portal_group_delete(struct portal_group *pg);
240struct portal_group	*portal_group_find(struct conf *conf, const char *name);
241int			portal_group_add_listen(struct portal_group *pg,
242			    const char *listen, bool iser);
243
244struct target		*target_new(struct conf *conf, const char *name);
245void			target_delete(struct target *target);
246struct target		*target_find(struct conf *conf,
247			    const char *name);
248
249struct lun		*lun_new(struct target *target, int lun_id);
250void			lun_delete(struct lun *lun);
251struct lun		*lun_find(struct target *target, int lun_id);
252void			lun_set_backend(struct lun *lun, const char *value);
253void			lun_set_blocksize(struct lun *lun, size_t value);
254void			lun_set_device_id(struct lun *lun, const char *value);
255void			lun_set_path(struct lun *lun, const char *value);
256void			lun_set_serial(struct lun *lun, const char *value);
257void			lun_set_size(struct lun *lun, size_t value);
258void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
259
260struct lun_option	*lun_option_new(struct lun *lun,
261			    const char *name, const char *value);
262void			lun_option_delete(struct lun_option *clo);
263struct lun_option	*lun_option_find(struct lun *lun, const char *name);
264void			lun_option_set(struct lun_option *clo,
265			    const char *value);
266
267void			kernel_init(void);
268int			kernel_lun_add(struct lun *lun);
269int			kernel_lun_resize(struct lun *lun);
270int			kernel_lun_remove(struct lun *lun);
271void			kernel_handoff(struct connection *conn);
272int			kernel_port_on(void);
273int			kernel_port_off(void);
274void			kernel_capsicate(void);
275
276#ifdef ICL_KERNEL_PROXY
277void			kernel_listen(struct addrinfo *ai, bool iser,
278			    int portal_id);
279void			kernel_accept(int *connection_id, int *portal_id,
280			    struct sockaddr *client_sa,
281			    socklen_t *client_salen);
282void			kernel_send(struct pdu *pdu);
283void			kernel_receive(struct pdu *pdu);
284#endif
285
286struct keys		*keys_new(void);
287void			keys_delete(struct keys *keys);
288void			keys_load(struct keys *keys, const struct pdu *pdu);
289void			keys_save(struct keys *keys, struct pdu *pdu);
290const char		*keys_find(struct keys *keys, const char *name);
291int			keys_find_int(struct keys *keys, const char *name);
292void			keys_add(struct keys *keys,
293			    const char *name, const char *value);
294void			keys_add_int(struct keys *keys,
295			    const char *name, int value);
296
297struct pdu		*pdu_new(struct connection *conn);
298struct pdu		*pdu_new_response(struct pdu *request);
299void			pdu_delete(struct pdu *pdu);
300void			pdu_receive(struct pdu *request);
301void			pdu_send(struct pdu *response);
302
303void			login(struct connection *conn);
304
305void			discovery(struct connection *conn);
306
307void			log_init(int level);
308void			log_set_peer_name(const char *name);
309void			log_set_peer_addr(const char *addr);
310void			log_err(int, const char *, ...)
311			    __dead2 __printflike(2, 3);
312void			log_errx(int, const char *, ...)
313			    __dead2 __printflike(2, 3);
314void			log_warn(const char *, ...) __printflike(1, 2);
315void			log_warnx(const char *, ...) __printflike(1, 2);
316void			log_debugx(const char *, ...) __printflike(1, 2);
317
318char			*checked_strdup(const char *);
319bool			valid_iscsi_name(const char *name);
320bool			timed_out(void);
321
322#endif /* !CTLD_H */
323