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