ctld.h revision 330449
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: stable/11/usr.sbin/ctld/ctld.h 330449 2018-03-05 07:26:05Z eadler $
32 */
33
34#ifndef CTLD_H
35#define	CTLD_H
36
37#include <sys/queue.h>
38#ifdef ICL_KERNEL_PROXY
39#include <sys/types.h>
40#endif
41#include <sys/socket.h>
42#include <stdbool.h>
43#include <libutil.h>
44
45#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
46#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
47#define	DEFAULT_BLOCKSIZE		512
48#define	DEFAULT_CD_BLOCKSIZE		2048
49
50#define	MAX_LUNS			1024
51#define	MAX_NAME_LEN			223
52#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
53#define	MAX_BURST_LENGTH		16776192
54#define	FIRST_BURST_LENGTH		(128 * 1024)
55#define	SOCKBUF_SIZE			1048576
56
57struct auth {
58	TAILQ_ENTRY(auth)		a_next;
59	struct auth_group		*a_auth_group;
60	char				*a_user;
61	char				*a_secret;
62	char				*a_mutual_user;
63	char				*a_mutual_secret;
64};
65
66struct auth_name {
67	TAILQ_ENTRY(auth_name)		an_next;
68	struct auth_group		*an_auth_group;
69	char				*an_initator_name;
70};
71
72struct auth_portal {
73	TAILQ_ENTRY(auth_portal)	ap_next;
74	struct auth_group		*ap_auth_group;
75	char				*ap_initator_portal;
76	struct sockaddr_storage		ap_sa;
77	int				ap_mask;
78};
79
80#define	AG_TYPE_UNKNOWN			0
81#define	AG_TYPE_DENY			1
82#define	AG_TYPE_NO_AUTHENTICATION	2
83#define	AG_TYPE_CHAP			3
84#define	AG_TYPE_CHAP_MUTUAL		4
85
86struct auth_group {
87	TAILQ_ENTRY(auth_group)		ag_next;
88	struct conf			*ag_conf;
89	char				*ag_name;
90	struct target			*ag_target;
91	int				ag_type;
92	TAILQ_HEAD(, auth)		ag_auths;
93	TAILQ_HEAD(, auth_name)		ag_names;
94	TAILQ_HEAD(, auth_portal)	ag_portals;
95};
96
97struct portal {
98	TAILQ_ENTRY(portal)		p_next;
99	struct portal_group		*p_portal_group;
100	bool				p_iser;
101	char				*p_listen;
102	struct addrinfo			*p_ai;
103#ifdef ICL_KERNEL_PROXY
104	int				p_id;
105#endif
106
107	TAILQ_HEAD(, target)		p_targets;
108	int				p_socket;
109};
110
111TAILQ_HEAD(options, option);
112
113#define	PG_FILTER_UNKNOWN		0
114#define	PG_FILTER_NONE			1
115#define	PG_FILTER_PORTAL		2
116#define	PG_FILTER_PORTAL_NAME		3
117#define	PG_FILTER_PORTAL_NAME_AUTH	4
118
119struct portal_group {
120	TAILQ_ENTRY(portal_group)	pg_next;
121	struct conf			*pg_conf;
122	struct options			pg_options;
123	char				*pg_name;
124	struct auth_group		*pg_discovery_auth_group;
125	int				pg_discovery_filter;
126	int				pg_foreign;
127	bool				pg_unassigned;
128	TAILQ_HEAD(, portal)		pg_portals;
129	TAILQ_HEAD(, port)		pg_ports;
130	char				*pg_offload;
131	char				*pg_redirection;
132
133	uint16_t			pg_tag;
134};
135
136struct pport {
137	TAILQ_ENTRY(pport)		pp_next;
138	TAILQ_HEAD(, port)		pp_ports;
139	struct conf			*pp_conf;
140	char				*pp_name;
141
142	uint32_t			pp_ctl_port;
143};
144
145struct port {
146	TAILQ_ENTRY(port)		p_next;
147	TAILQ_ENTRY(port)		p_pgs;
148	TAILQ_ENTRY(port)		p_pps;
149	TAILQ_ENTRY(port)		p_ts;
150	struct conf			*p_conf;
151	char				*p_name;
152	struct auth_group		*p_auth_group;
153	struct portal_group		*p_portal_group;
154	struct pport			*p_pport;
155	struct target			*p_target;
156
157	uint32_t			p_ctl_port;
158};
159
160struct option {
161	TAILQ_ENTRY(option)		o_next;
162	char				*o_name;
163	char				*o_value;
164};
165
166struct lun {
167	TAILQ_ENTRY(lun)		l_next;
168	struct conf			*l_conf;
169	struct options			l_options;
170	char				*l_name;
171	char				*l_backend;
172	uint8_t				l_device_type;
173	int				l_blocksize;
174	char				*l_device_id;
175	char				*l_path;
176	char				*l_scsiname;
177	char				*l_serial;
178	int64_t				l_size;
179
180	int				l_ctl_lun;
181};
182
183struct target {
184	TAILQ_ENTRY(target)		t_next;
185	struct conf			*t_conf;
186	struct lun			*t_luns[MAX_LUNS];
187	struct auth_group		*t_auth_group;
188	TAILQ_HEAD(, port)		t_ports;
189	char				*t_name;
190	char				*t_alias;
191	char				*t_redirection;
192};
193
194struct isns {
195	TAILQ_ENTRY(isns)		i_next;
196	struct conf			*i_conf;
197	char				*i_addr;
198	struct addrinfo			*i_ai;
199};
200
201struct conf {
202	char				*conf_pidfile_path;
203	TAILQ_HEAD(, lun)		conf_luns;
204	TAILQ_HEAD(, target)		conf_targets;
205	TAILQ_HEAD(, auth_group)	conf_auth_groups;
206	TAILQ_HEAD(, port)		conf_ports;
207	TAILQ_HEAD(, portal_group)	conf_portal_groups;
208	TAILQ_HEAD(, pport)		conf_pports;
209	TAILQ_HEAD(, isns)		conf_isns;
210	int				conf_isns_period;
211	int				conf_isns_timeout;
212	int				conf_debug;
213	int				conf_timeout;
214	int				conf_maxproc;
215
216#ifdef ICL_KERNEL_PROXY
217	int				conf_portal_id;
218#endif
219	struct pidfh			*conf_pidfh;
220
221	bool				conf_default_pg_defined;
222	bool				conf_default_ag_defined;
223	bool				conf_kernel_port_on;
224};
225
226#define	CONN_SESSION_TYPE_NONE		0
227#define	CONN_SESSION_TYPE_DISCOVERY	1
228#define	CONN_SESSION_TYPE_NORMAL	2
229
230#define	CONN_DIGEST_NONE		0
231#define	CONN_DIGEST_CRC32C		1
232
233struct connection {
234	struct portal		*conn_portal;
235	struct port		*conn_port;
236	struct target		*conn_target;
237	int			conn_socket;
238	int			conn_session_type;
239	char			*conn_initiator_name;
240	char			*conn_initiator_addr;
241	char			*conn_initiator_alias;
242	uint8_t			conn_initiator_isid[6];
243	struct sockaddr_storage	conn_initiator_sa;
244	uint32_t		conn_cmdsn;
245	uint32_t		conn_statsn;
246	size_t			conn_data_segment_limit;
247	size_t			conn_max_data_segment_length;
248	size_t			conn_max_burst_length;
249	size_t			conn_first_burst_length;
250	int			conn_immediate_data;
251	int			conn_header_digest;
252	int			conn_data_digest;
253	const char		*conn_user;
254	struct chap		*conn_chap;
255};
256
257struct pdu {
258	struct connection	*pdu_connection;
259	struct iscsi_bhs	*pdu_bhs;
260	char			*pdu_data;
261	size_t			pdu_data_len;
262};
263
264#define	KEYS_MAX	1024
265
266struct keys {
267	char		*keys_names[KEYS_MAX];
268	char		*keys_values[KEYS_MAX];
269	char		*keys_data;
270	size_t		keys_data_len;
271};
272
273#define	CHAP_CHALLENGE_LEN	1024
274#define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
275
276struct chap {
277	unsigned char	chap_id;
278	char		chap_challenge[CHAP_CHALLENGE_LEN];
279	char		chap_response[CHAP_DIGEST_LEN];
280};
281
282struct rchap {
283	char		*rchap_secret;
284	unsigned char	rchap_id;
285	void		*rchap_challenge;
286	size_t		rchap_challenge_len;
287};
288
289struct chap		*chap_new(void);
290char			*chap_get_id(const struct chap *chap);
291char			*chap_get_challenge(const struct chap *chap);
292int			chap_receive(struct chap *chap, const char *response);
293int			chap_authenticate(struct chap *chap,
294			    const char *secret);
295void			chap_delete(struct chap *chap);
296
297struct rchap		*rchap_new(const char *secret);
298int			rchap_receive(struct rchap *rchap,
299			    const char *id, const char *challenge);
300char			*rchap_get_response(struct rchap *rchap);
301void			rchap_delete(struct rchap *rchap);
302
303int			parse_conf(struct conf *conf, const char *path);
304int			uclparse_conf(struct conf *conf, const char *path);
305
306struct conf		*conf_new(void);
307struct conf		*conf_new_from_kernel(void);
308void			conf_delete(struct conf *conf);
309int			conf_verify(struct conf *conf);
310
311struct auth_group	*auth_group_new(struct conf *conf, const char *name);
312void			auth_group_delete(struct auth_group *ag);
313struct auth_group	*auth_group_find(const struct conf *conf,
314			    const char *name);
315int			auth_group_set_type(struct auth_group *ag,
316			    const char *type);
317
318const struct auth	*auth_new_chap(struct auth_group *ag,
319			    const char *user, const char *secret);
320const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
321			    const char *user, const char *secret,
322			    const char *user2, const char *secret2);
323const struct auth	*auth_find(const struct auth_group *ag,
324			    const char *user);
325
326const struct auth_name	*auth_name_new(struct auth_group *ag,
327			    const char *initiator_name);
328bool			auth_name_defined(const struct auth_group *ag);
329const struct auth_name	*auth_name_find(const struct auth_group *ag,
330			    const char *initiator_name);
331int			auth_name_check(const struct auth_group *ag,
332			    const char *initiator_name);
333
334const struct auth_portal	*auth_portal_new(struct auth_group *ag,
335				    const char *initiator_portal);
336bool			auth_portal_defined(const struct auth_group *ag);
337const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
338				    const struct sockaddr_storage *sa);
339int				auth_portal_check(const struct auth_group *ag,
340				    const struct sockaddr_storage *sa);
341
342struct portal_group	*portal_group_new(struct conf *conf, const char *name);
343void			portal_group_delete(struct portal_group *pg);
344struct portal_group	*portal_group_find(const struct conf *conf,
345			    const char *name);
346int			portal_group_add_listen(struct portal_group *pg,
347			    const char *listen, bool iser);
348int			portal_group_set_filter(struct portal_group *pg,
349			    const char *filter);
350int			portal_group_set_offload(struct portal_group *pg,
351			    const char *offload);
352int			portal_group_set_redirection(struct portal_group *pg,
353			    const char *addr);
354
355int			isns_new(struct conf *conf, const char *addr);
356void			isns_delete(struct isns *is);
357void			isns_register(struct isns *isns, struct isns *oldisns);
358void			isns_check(struct isns *isns);
359void			isns_deregister(struct isns *isns);
360
361struct pport		*pport_new(struct conf *conf, const char *name,
362			    uint32_t ctl_port);
363struct pport		*pport_find(const struct conf *conf, const char *name);
364struct pport		*pport_copy(struct pport *pport, struct conf *conf);
365void			pport_delete(struct pport *pport);
366
367struct port		*port_new(struct conf *conf, struct target *target,
368			    struct portal_group *pg);
369struct port		*port_new_pp(struct conf *conf, struct target *target,
370			    struct pport *pp);
371struct port		*port_find(const struct conf *conf, const char *name);
372struct port		*port_find_in_pg(const struct portal_group *pg,
373			    const char *target);
374void			port_delete(struct port *port);
375int			port_is_dummy(struct port *port);
376
377struct target		*target_new(struct conf *conf, const char *name);
378void			target_delete(struct target *target);
379struct target		*target_find(struct conf *conf,
380			    const char *name);
381int			target_set_redirection(struct target *target,
382			    const char *addr);
383
384struct lun		*lun_new(struct conf *conf, const char *name);
385void			lun_delete(struct lun *lun);
386struct lun		*lun_find(const struct conf *conf, const char *name);
387void			lun_set_backend(struct lun *lun, const char *value);
388void			lun_set_device_type(struct lun *lun, uint8_t value);
389void			lun_set_blocksize(struct lun *lun, size_t value);
390void			lun_set_device_id(struct lun *lun, const char *value);
391void			lun_set_path(struct lun *lun, const char *value);
392void			lun_set_scsiname(struct lun *lun, const char *value);
393void			lun_set_serial(struct lun *lun, const char *value);
394void			lun_set_size(struct lun *lun, size_t value);
395void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
396
397struct option		*option_new(struct options *os,
398			    const char *name, const char *value);
399void			option_delete(struct options *os, struct option *co);
400struct option		*option_find(const struct options *os, const char *name);
401void			option_set(struct option *o, const char *value);
402
403void			kernel_init(void);
404int			kernel_lun_add(struct lun *lun);
405int			kernel_lun_modify(struct lun *lun);
406int			kernel_lun_remove(struct lun *lun);
407void			kernel_handoff(struct connection *conn);
408void			kernel_limits(const char *offload,
409			    size_t *max_data_segment_length);
410int			kernel_port_add(struct port *port);
411int			kernel_port_update(struct port *port, struct port *old);
412int			kernel_port_remove(struct port *port);
413void			kernel_capsicate(void);
414
415#ifdef ICL_KERNEL_PROXY
416void			kernel_listen(struct addrinfo *ai, bool iser,
417			    int portal_id);
418void			kernel_accept(int *connection_id, int *portal_id,
419			    struct sockaddr *client_sa,
420			    socklen_t *client_salen);
421void			kernel_send(struct pdu *pdu);
422void			kernel_receive(struct pdu *pdu);
423#endif
424
425struct keys		*keys_new(void);
426void			keys_delete(struct keys *keys);
427void			keys_load(struct keys *keys, const struct pdu *pdu);
428void			keys_save(struct keys *keys, struct pdu *pdu);
429const char		*keys_find(struct keys *keys, const char *name);
430void			keys_add(struct keys *keys,
431			    const char *name, const char *value);
432void			keys_add_int(struct keys *keys,
433			    const char *name, int value);
434
435struct pdu		*pdu_new(struct connection *conn);
436struct pdu		*pdu_new_response(struct pdu *request);
437void			pdu_delete(struct pdu *pdu);
438void			pdu_receive(struct pdu *request);
439void			pdu_send(struct pdu *response);
440
441void			login(struct connection *conn);
442
443void			discovery(struct connection *conn);
444
445void			log_init(int level);
446void			log_set_peer_name(const char *name);
447void			log_set_peer_addr(const char *addr);
448void			log_err(int, const char *, ...)
449			    __dead2 __printflike(2, 3);
450void			log_errx(int, const char *, ...)
451			    __dead2 __printflike(2, 3);
452void			log_warn(const char *, ...) __printflike(1, 2);
453void			log_warnx(const char *, ...) __printflike(1, 2);
454void			log_debugx(const char *, ...) __printflike(1, 2);
455
456char			*checked_strdup(const char *);
457bool			valid_iscsi_name(const char *name);
458void			set_timeout(int timeout, int fatal);
459bool			timed_out(void);
460
461#endif /* !CTLD_H */
462