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