ctld.h revision 275642
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 275642 2014-12-09 11:50:50Z 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#endif
39#include <sys/socket.h>
40#include <stdbool.h>
41#include <libutil.h>
42#include <openssl/md5.h>
43
44#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
45#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
46#define	DEFAULT_BLOCKSIZE		512
47
48#define	MAX_NAME_LEN			223
49#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
50#define	MAX_BURST_LENGTH		16776192
51
52struct auth {
53	TAILQ_ENTRY(auth)		a_next;
54	struct auth_group		*a_auth_group;
55	char				*a_user;
56	char				*a_secret;
57	char				*a_mutual_user;
58	char				*a_mutual_secret;
59};
60
61struct auth_name {
62	TAILQ_ENTRY(auth_name)		an_next;
63	struct auth_group		*an_auth_group;
64	char				*an_initator_name;
65};
66
67struct auth_portal {
68	TAILQ_ENTRY(auth_portal)	ap_next;
69	struct auth_group		*ap_auth_group;
70	char				*ap_initator_portal;
71	struct sockaddr_storage		ap_sa;
72	int				ap_mask;
73};
74
75#define	AG_TYPE_UNKNOWN			0
76#define	AG_TYPE_DENY			1
77#define	AG_TYPE_NO_AUTHENTICATION	2
78#define	AG_TYPE_CHAP			3
79#define	AG_TYPE_CHAP_MUTUAL		4
80
81struct auth_group {
82	TAILQ_ENTRY(auth_group)		ag_next;
83	struct conf			*ag_conf;
84	char				*ag_name;
85	struct target			*ag_target;
86	int				ag_type;
87	TAILQ_HEAD(, auth)		ag_auths;
88	TAILQ_HEAD(, auth_name)		ag_names;
89	TAILQ_HEAD(, auth_portal)	ag_portals;
90};
91
92struct portal {
93	TAILQ_ENTRY(portal)		p_next;
94	struct portal_group		*p_portal_group;
95	bool				p_iser;
96	char				*p_listen;
97	struct addrinfo			*p_ai;
98#ifdef ICL_KERNEL_PROXY
99	int				p_id;
100#endif
101
102	TAILQ_HEAD(, target)		p_targets;
103	int				p_socket;
104};
105
106#define	PG_FILTER_UNKNOWN		0
107#define	PG_FILTER_NONE			1
108#define	PG_FILTER_PORTAL		2
109#define	PG_FILTER_PORTAL_NAME		3
110#define	PG_FILTER_PORTAL_NAME_AUTH	4
111
112struct portal_group {
113	TAILQ_ENTRY(portal_group)	pg_next;
114	struct conf			*pg_conf;
115	char				*pg_name;
116	struct auth_group		*pg_discovery_auth_group;
117	int				pg_discovery_filter;
118	bool				pg_unassigned;
119	TAILQ_HEAD(, portal)		pg_portals;
120	char				*pg_redirection;
121
122	uint16_t			pg_tag;
123};
124
125struct lun_option {
126	TAILQ_ENTRY(lun_option)		lo_next;
127	struct lun			*lo_lun;
128	char				*lo_name;
129	char				*lo_value;
130};
131
132struct lun {
133	TAILQ_ENTRY(lun)		l_next;
134	TAILQ_HEAD(, lun_option)	l_options;
135	struct target			*l_target;
136	int				l_lun;
137	char				*l_backend;
138	int				l_blocksize;
139	char				*l_device_id;
140	char				*l_path;
141	char				*l_serial;
142	int64_t				l_size;
143
144	int				l_ctl_lun;
145};
146
147struct target {
148	TAILQ_ENTRY(target)		t_next;
149	TAILQ_HEAD(, lun)		t_luns;
150	struct conf			*t_conf;
151	struct auth_group		*t_auth_group;
152	struct portal_group		*t_portal_group;
153	char				*t_name;
154	char				*t_alias;
155	char				*t_redirection;
156};
157
158struct isns {
159	TAILQ_ENTRY(isns)		i_next;
160	struct conf			*i_conf;
161	char				*i_addr;
162	struct addrinfo			*i_ai;
163};
164
165struct conf {
166	char				*conf_pidfile_path;
167	TAILQ_HEAD(, target)		conf_targets;
168	TAILQ_HEAD(, auth_group)	conf_auth_groups;
169	TAILQ_HEAD(, portal_group)	conf_portal_groups;
170	TAILQ_HEAD(, isns)		conf_isns;
171	int				conf_isns_period;
172	int				conf_isns_timeout;
173	int				conf_debug;
174	int				conf_timeout;
175	int				conf_maxproc;
176
177	uint16_t			conf_last_portal_group_tag;
178#ifdef ICL_KERNEL_PROXY
179	int				conf_portal_id;
180#endif
181	struct pidfh			*conf_pidfh;
182
183	bool				conf_default_pg_defined;
184	bool				conf_default_ag_defined;
185	bool				conf_kernel_port_on;
186};
187
188#define	CONN_SESSION_TYPE_NONE		0
189#define	CONN_SESSION_TYPE_DISCOVERY	1
190#define	CONN_SESSION_TYPE_NORMAL	2
191
192#define	CONN_DIGEST_NONE		0
193#define	CONN_DIGEST_CRC32C		1
194
195struct connection {
196	struct portal		*conn_portal;
197	struct target		*conn_target;
198	int			conn_socket;
199	int			conn_session_type;
200	char			*conn_initiator_name;
201	char			*conn_initiator_addr;
202	char			*conn_initiator_alias;
203	uint8_t			conn_initiator_isid[6];
204	struct sockaddr_storage	conn_initiator_sa;
205	uint32_t		conn_cmdsn;
206	uint32_t		conn_statsn;
207	size_t			conn_max_data_segment_length;
208	size_t			conn_max_burst_length;
209	int			conn_immediate_data;
210	int			conn_header_digest;
211	int			conn_data_digest;
212	const char		*conn_user;
213	struct chap		*conn_chap;
214};
215
216struct pdu {
217	struct connection	*pdu_connection;
218	struct iscsi_bhs	*pdu_bhs;
219	char			*pdu_data;
220	size_t			pdu_data_len;
221};
222
223#define	KEYS_MAX	1024
224
225struct keys {
226	char		*keys_names[KEYS_MAX];
227	char		*keys_values[KEYS_MAX];
228	char		*keys_data;
229	size_t		keys_data_len;
230};
231
232#define	CHAP_CHALLENGE_LEN	1024
233
234struct chap {
235	unsigned char	chap_id;
236	char		chap_challenge[CHAP_CHALLENGE_LEN];
237	char		chap_response[MD5_DIGEST_LENGTH];
238};
239
240struct rchap {
241	char		*rchap_secret;
242	unsigned char	rchap_id;
243	void		*rchap_challenge;
244	size_t		rchap_challenge_len;
245};
246
247struct chap		*chap_new(void);
248char			*chap_get_id(const struct chap *chap);
249char			*chap_get_challenge(const struct chap *chap);
250int			chap_receive(struct chap *chap, const char *response);
251int			chap_authenticate(struct chap *chap,
252			    const char *secret);
253void			chap_delete(struct chap *chap);
254
255struct rchap		*rchap_new(const char *secret);
256int			rchap_receive(struct rchap *rchap,
257			    const char *id, const char *challenge);
258char			*rchap_get_response(struct rchap *rchap);
259void			rchap_delete(struct rchap *rchap);
260
261struct conf		*conf_new(void);
262struct conf		*conf_new_from_file(const char *path);
263struct conf		*conf_new_from_kernel(void);
264void			conf_delete(struct conf *conf);
265int			conf_verify(struct conf *conf);
266
267struct auth_group	*auth_group_new(struct conf *conf, const char *name);
268void			auth_group_delete(struct auth_group *ag);
269struct auth_group	*auth_group_find(const struct conf *conf,
270			    const char *name);
271int			auth_group_set_type(struct auth_group *ag,
272			    const char *type);
273
274const struct auth	*auth_new_chap(struct auth_group *ag,
275			    const char *user, const char *secret);
276const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
277			    const char *user, const char *secret,
278			    const char *user2, const char *secret2);
279const struct auth	*auth_find(const struct auth_group *ag,
280			    const char *user);
281
282const struct auth_name	*auth_name_new(struct auth_group *ag,
283			    const char *initiator_name);
284bool			auth_name_defined(const struct auth_group *ag);
285const struct auth_name	*auth_name_find(const struct auth_group *ag,
286			    const char *initiator_name);
287int			auth_name_check(const struct auth_group *ag,
288			    const char *initiator_name);
289
290const struct auth_portal	*auth_portal_new(struct auth_group *ag,
291				    const char *initiator_portal);
292bool			auth_portal_defined(const struct auth_group *ag);
293const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
294				    const struct sockaddr_storage *sa);
295int				auth_portal_check(const struct auth_group *ag,
296				    const struct sockaddr_storage *sa);
297
298struct portal_group	*portal_group_new(struct conf *conf, const char *name);
299void			portal_group_delete(struct portal_group *pg);
300struct portal_group	*portal_group_find(const struct conf *conf,
301			    const char *name);
302int			portal_group_add_listen(struct portal_group *pg,
303			    const char *listen, bool iser);
304int			portal_group_set_filter(struct portal_group *pg,
305			    const char *filter);
306int			portal_group_set_redirection(struct portal_group *pg,
307			    const char *addr);
308
309int			isns_new(struct conf *conf, const char *addr);
310void			isns_delete(struct isns *is);
311void			isns_register(struct isns *isns, struct isns *oldisns);
312void			isns_check(struct isns *isns);
313void			isns_deregister(struct isns *isns);
314
315struct target		*target_new(struct conf *conf, const char *name);
316void			target_delete(struct target *target);
317struct target		*target_find(struct conf *conf,
318			    const char *name);
319int			target_set_redirection(struct target *target,
320			    const char *addr);
321
322struct lun		*lun_new(struct target *target, int lun_id);
323void			lun_delete(struct lun *lun);
324struct lun		*lun_find(const struct target *target, int lun_id);
325void			lun_set_backend(struct lun *lun, const char *value);
326void			lun_set_blocksize(struct lun *lun, size_t value);
327void			lun_set_device_id(struct lun *lun, const char *value);
328void			lun_set_path(struct lun *lun, const char *value);
329void			lun_set_serial(struct lun *lun, const char *value);
330void			lun_set_size(struct lun *lun, size_t value);
331void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
332
333struct lun_option	*lun_option_new(struct lun *lun,
334			    const char *name, const char *value);
335void			lun_option_delete(struct lun_option *clo);
336struct lun_option	*lun_option_find(const struct lun *lun,
337			    const char *name);
338void			lun_option_set(struct lun_option *clo,
339			    const char *value);
340
341void			kernel_init(void);
342int			kernel_lun_add(struct lun *lun);
343int			kernel_lun_resize(struct lun *lun);
344int			kernel_lun_remove(struct lun *lun);
345void			kernel_handoff(struct connection *conn);
346int			kernel_port_add(struct target *targ);
347int			kernel_port_remove(struct target *targ);
348void			kernel_capsicate(void);
349
350#ifdef ICL_KERNEL_PROXY
351void			kernel_listen(struct addrinfo *ai, bool iser,
352			    int portal_id);
353void			kernel_accept(int *connection_id, int *portal_id,
354			    struct sockaddr *client_sa,
355			    socklen_t *client_salen);
356void			kernel_send(struct pdu *pdu);
357void			kernel_receive(struct pdu *pdu);
358#endif
359
360struct keys		*keys_new(void);
361void			keys_delete(struct keys *keys);
362void			keys_load(struct keys *keys, const struct pdu *pdu);
363void			keys_save(struct keys *keys, struct pdu *pdu);
364const char		*keys_find(struct keys *keys, const char *name);
365int			keys_find_int(struct keys *keys, const char *name);
366void			keys_add(struct keys *keys,
367			    const char *name, const char *value);
368void			keys_add_int(struct keys *keys,
369			    const char *name, int value);
370
371struct pdu		*pdu_new(struct connection *conn);
372struct pdu		*pdu_new_response(struct pdu *request);
373void			pdu_delete(struct pdu *pdu);
374void			pdu_receive(struct pdu *request);
375void			pdu_send(struct pdu *response);
376
377void			login(struct connection *conn);
378
379void			discovery(struct connection *conn);
380
381void			log_init(int level);
382void			log_set_peer_name(const char *name);
383void			log_set_peer_addr(const char *addr);
384void			log_err(int, const char *, ...)
385			    __dead2 __printflike(2, 3);
386void			log_errx(int, const char *, ...)
387			    __dead2 __printflike(2, 3);
388void			log_warn(const char *, ...) __printflike(1, 2);
389void			log_warnx(const char *, ...) __printflike(1, 2);
390void			log_debugx(const char *, ...) __printflike(1, 2);
391
392char			*checked_strdup(const char *);
393bool			valid_iscsi_name(const char *name);
394void			set_timeout(int timeout, int fatal);
395bool			timed_out(void);
396
397#endif /* !CTLD_H */
398