ctld.h revision 265511
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 265511 2014-05-07 07:35:21Z trasz $
30 */
31
32#ifndef CTLD_H
33#define	CTLD_H
34
35#include <sys/queue.h>
36#include <stdbool.h>
37#include <libutil.h>
38
39#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
40#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
41#define	DEFAULT_BLOCKSIZE		512
42
43#define	MAX_NAME_LEN			223
44#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
45#define	MAX_BURST_LENGTH		16776192
46
47struct auth {
48	TAILQ_ENTRY(auth)		a_next;
49	struct auth_group		*a_auth_group;
50	char				*a_user;
51	char				*a_secret;
52	char				*a_mutual_user;
53	char				*a_mutual_secret;
54};
55
56struct auth_name {
57	TAILQ_ENTRY(auth_name)		an_next;
58	struct auth_group		*an_auth_group;
59	char				*an_initator_name;
60};
61
62struct auth_portal {
63	TAILQ_ENTRY(auth_portal)	ap_next;
64	struct auth_group		*ap_auth_group;
65	char				*ap_initator_portal;
66};
67
68#define	AG_TYPE_UNKNOWN			0
69#define	AG_TYPE_DENY			1
70#define	AG_TYPE_NO_AUTHENTICATION	2
71#define	AG_TYPE_CHAP			3
72#define	AG_TYPE_CHAP_MUTUAL		4
73
74struct auth_group {
75	TAILQ_ENTRY(auth_group)		ag_next;
76	struct conf			*ag_conf;
77	char				*ag_name;
78	struct target			*ag_target;
79	int				ag_type;
80	TAILQ_HEAD(, auth)		ag_auths;
81	TAILQ_HEAD(, auth_name)		ag_names;
82	TAILQ_HEAD(, auth_portal)	ag_portals;
83};
84
85struct portal {
86	TAILQ_ENTRY(portal)		p_next;
87	struct portal_group		*p_portal_group;
88	bool				p_iser;
89	char				*p_listen;
90	struct addrinfo			*p_ai;
91#ifdef ICL_KERNEL_PROXY
92	int				p_id;
93#endif
94
95	TAILQ_HEAD(, target)		p_targets;
96	int				p_socket;
97};
98
99struct portal_group {
100	TAILQ_ENTRY(portal_group)	pg_next;
101	struct conf			*pg_conf;
102	char				*pg_name;
103	struct auth_group		*pg_discovery_auth_group;
104	bool				pg_unassigned;
105	TAILQ_HEAD(, portal)		pg_portals;
106
107	uint16_t			pg_tag;
108};
109
110struct lun_option {
111	TAILQ_ENTRY(lun_option)		lo_next;
112	struct lun			*lo_lun;
113	char				*lo_name;
114	char				*lo_value;
115};
116
117struct lun {
118	TAILQ_ENTRY(lun)		l_next;
119	TAILQ_HEAD(, lun_option)	l_options;
120	struct target			*l_target;
121	int				l_lun;
122	char				*l_backend;
123	int				l_blocksize;
124	char				*l_device_id;
125	char				*l_path;
126	char				*l_serial;
127	int64_t				l_size;
128
129	int				l_ctl_lun;
130};
131
132struct target {
133	TAILQ_ENTRY(target)		t_next;
134	TAILQ_HEAD(, lun)		t_luns;
135	struct conf			*t_conf;
136	struct auth_group		*t_auth_group;
137	struct portal_group		*t_portal_group;
138	char				*t_name;
139	char				*t_alias;
140};
141
142struct conf {
143	char				*conf_pidfile_path;
144	TAILQ_HEAD(, target)		conf_targets;
145	TAILQ_HEAD(, auth_group)	conf_auth_groups;
146	TAILQ_HEAD(, portal_group)	conf_portal_groups;
147	int				conf_debug;
148	int				conf_timeout;
149	int				conf_maxproc;
150
151	uint16_t			conf_last_portal_group_tag;
152#ifdef ICL_KERNEL_PROXY
153	int				conf_portal_id;
154#endif
155	struct pidfh			*conf_pidfh;
156
157	bool				conf_default_pg_defined;
158	bool				conf_default_ag_defined;
159	bool				conf_kernel_port_on;
160};
161
162#define	CONN_SESSION_TYPE_NONE		0
163#define	CONN_SESSION_TYPE_DISCOVERY	1
164#define	CONN_SESSION_TYPE_NORMAL	2
165
166#define	CONN_DIGEST_NONE		0
167#define	CONN_DIGEST_CRC32C		1
168
169struct connection {
170	struct portal		*conn_portal;
171	struct target		*conn_target;
172	int			conn_socket;
173	int			conn_session_type;
174	char			*conn_initiator_name;
175	char			*conn_initiator_addr;
176	char			*conn_initiator_alias;
177	uint32_t		conn_cmdsn;
178	uint32_t		conn_statsn;
179	size_t			conn_max_data_segment_length;
180	size_t			conn_max_burst_length;
181	int			conn_immediate_data;
182	int			conn_header_digest;
183	int			conn_data_digest;
184};
185
186struct pdu {
187	struct connection	*pdu_connection;
188	struct iscsi_bhs	*pdu_bhs;
189	char			*pdu_data;
190	size_t			pdu_data_len;
191};
192
193#define	KEYS_MAX	1024
194
195struct keys {
196	char		*keys_names[KEYS_MAX];
197	char		*keys_values[KEYS_MAX];
198	char		*keys_data;
199	size_t		keys_data_len;
200};
201
202struct conf		*conf_new(void);
203struct conf		*conf_new_from_file(const char *path);
204struct conf		*conf_new_from_kernel(void);
205void			conf_delete(struct conf *conf);
206int			conf_verify(struct conf *conf);
207
208struct auth_group	*auth_group_new(struct conf *conf, const char *name);
209void			auth_group_delete(struct auth_group *ag);
210struct auth_group	*auth_group_find(struct conf *conf, const char *name);
211int			auth_group_set_type_str(struct auth_group *ag,
212			    const char *type);
213
214const struct auth	*auth_new_chap(struct auth_group *ag,
215			    const char *user, const char *secret);
216const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
217			    const char *user, const char *secret,
218			    const char *user2, const char *secret2);
219const struct auth	*auth_find(struct auth_group *ag,
220			    const char *user);
221
222const struct auth_name	*auth_name_new(struct auth_group *ag,
223			    const char *initiator_name);
224bool			auth_name_defined(const struct auth_group *ag);
225const struct auth_name	*auth_name_find(const struct auth_group *ag,
226			    const char *initiator_name);
227
228const struct auth_portal	*auth_portal_new(struct auth_group *ag,
229				    const char *initiator_portal);
230bool			auth_portal_defined(const struct auth_group *ag);
231const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
232				    const char *initiator_portal);
233
234struct portal_group	*portal_group_new(struct conf *conf, const char *name);
235void			portal_group_delete(struct portal_group *pg);
236struct portal_group	*portal_group_find(struct conf *conf, const char *name);
237int			portal_group_add_listen(struct portal_group *pg,
238			    const char *listen, bool iser);
239
240struct target		*target_new(struct conf *conf, const char *name);
241void			target_delete(struct target *target);
242struct target		*target_find(struct conf *conf,
243			    const char *name);
244
245struct lun		*lun_new(struct target *target, int lun_id);
246void			lun_delete(struct lun *lun);
247struct lun		*lun_find(struct target *target, int lun_id);
248void			lun_set_backend(struct lun *lun, const char *value);
249void			lun_set_blocksize(struct lun *lun, size_t value);
250void			lun_set_device_id(struct lun *lun, const char *value);
251void			lun_set_path(struct lun *lun, const char *value);
252void			lun_set_serial(struct lun *lun, const char *value);
253void			lun_set_size(struct lun *lun, size_t value);
254void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
255
256struct lun_option	*lun_option_new(struct lun *lun,
257			    const char *name, const char *value);
258void			lun_option_delete(struct lun_option *clo);
259struct lun_option	*lun_option_find(struct lun *lun, const char *name);
260void			lun_option_set(struct lun_option *clo,
261			    const char *value);
262
263void			kernel_init(void);
264int			kernel_lun_add(struct lun *lun);
265int			kernel_lun_resize(struct lun *lun);
266int			kernel_lun_remove(struct lun *lun);
267void			kernel_handoff(struct connection *conn);
268int			kernel_port_on(void);
269int			kernel_port_off(void);
270void			kernel_capsicate(void);
271
272/*
273 * ICL_KERNEL_PROXY
274 */
275void			kernel_listen(struct addrinfo *ai, bool iser,
276			    int portal_id);
277void			kernel_accept(int *connection_id, int *portal_id);
278void			kernel_send(struct pdu *pdu);
279void			kernel_receive(struct pdu *pdu);
280
281struct keys		*keys_new(void);
282void			keys_delete(struct keys *keys);
283void			keys_load(struct keys *keys, const struct pdu *pdu);
284void			keys_save(struct keys *keys, struct pdu *pdu);
285const char		*keys_find(struct keys *keys, const char *name);
286int			keys_find_int(struct keys *keys, const char *name);
287void			keys_add(struct keys *keys,
288			    const char *name, const char *value);
289void			keys_add_int(struct keys *keys,
290			    const char *name, int value);
291
292struct pdu		*pdu_new(struct connection *conn);
293struct pdu		*pdu_new_response(struct pdu *request);
294void			pdu_delete(struct pdu *pdu);
295void			pdu_receive(struct pdu *request);
296void			pdu_send(struct pdu *response);
297
298void			login(struct connection *conn);
299
300void			discovery(struct connection *conn);
301
302void			log_init(int level);
303void			log_set_peer_name(const char *name);
304void			log_set_peer_addr(const char *addr);
305void			log_err(int, const char *, ...)
306			    __dead2 __printflike(2, 3);
307void			log_errx(int, const char *, ...)
308			    __dead2 __printflike(2, 3);
309void			log_warn(const char *, ...) __printflike(1, 2);
310void			log_warnx(const char *, ...) __printflike(1, 2);
311void			log_debugx(const char *, ...) __printflike(1, 2);
312
313char			*checked_strdup(const char *);
314bool			valid_iscsi_name(const char *name);
315bool			timed_out(void);
316
317#endif /* !CTLD_H */
318