1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz * $FreeBSD: releng/10.3/usr.sbin/ctld/ctld.h 291387 2015-11-27 15:19:36Z mav $
30255570Strasz */
31255570Strasz
32255570Strasz#ifndef CTLD_H
33255570Strasz#define	CTLD_H
34255570Strasz
35255570Strasz#include <sys/queue.h>
36265513Strasz#ifdef ICL_KERNEL_PROXY
37265513Strasz#include <sys/types.h>
38270137Smav#endif
39265513Strasz#include <sys/socket.h>
40255570Strasz#include <stdbool.h>
41255570Strasz#include <libutil.h>
42255570Strasz
43255570Strasz#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
44255570Strasz#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
45255570Strasz#define	DEFAULT_BLOCKSIZE		512
46288823Smav#define	DEFAULT_CD_BLOCKSIZE		2048
47255570Strasz
48279002Smav#define	MAX_LUNS			1024
49255570Strasz#define	MAX_NAME_LEN			223
50255570Strasz#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
51255570Strasz#define	MAX_BURST_LENGTH		16776192
52279001Smav#define	SOCKBUF_SIZE			1048576
53255570Strasz
54255570Straszstruct auth {
55255570Strasz	TAILQ_ENTRY(auth)		a_next;
56255570Strasz	struct auth_group		*a_auth_group;
57255570Strasz	char				*a_user;
58255570Strasz	char				*a_secret;
59255570Strasz	char				*a_mutual_user;
60255570Strasz	char				*a_mutual_secret;
61255570Strasz};
62255570Strasz
63263720Straszstruct auth_name {
64263720Strasz	TAILQ_ENTRY(auth_name)		an_next;
65263720Strasz	struct auth_group		*an_auth_group;
66263720Strasz	char				*an_initator_name;
67263720Strasz};
68263720Strasz
69263720Straszstruct auth_portal {
70263720Strasz	TAILQ_ENTRY(auth_portal)	ap_next;
71263720Strasz	struct auth_group		*ap_auth_group;
72263720Strasz	char				*ap_initator_portal;
73270137Smav	struct sockaddr_storage		ap_sa;
74270137Smav	int				ap_mask;
75263720Strasz};
76263720Strasz
77255570Strasz#define	AG_TYPE_UNKNOWN			0
78263729Strasz#define	AG_TYPE_DENY			1
79263729Strasz#define	AG_TYPE_NO_AUTHENTICATION	2
80263729Strasz#define	AG_TYPE_CHAP			3
81263729Strasz#define	AG_TYPE_CHAP_MUTUAL		4
82255570Strasz
83255570Straszstruct auth_group {
84255570Strasz	TAILQ_ENTRY(auth_group)		ag_next;
85255570Strasz	struct conf			*ag_conf;
86255570Strasz	char				*ag_name;
87255570Strasz	struct target			*ag_target;
88255570Strasz	int				ag_type;
89255570Strasz	TAILQ_HEAD(, auth)		ag_auths;
90263720Strasz	TAILQ_HEAD(, auth_name)		ag_names;
91263720Strasz	TAILQ_HEAD(, auth_portal)	ag_portals;
92255570Strasz};
93255570Strasz
94255570Straszstruct portal {
95255570Strasz	TAILQ_ENTRY(portal)		p_next;
96255570Strasz	struct portal_group		*p_portal_group;
97255570Strasz	bool				p_iser;
98255570Strasz	char				*p_listen;
99255570Strasz	struct addrinfo			*p_ai;
100265509Strasz#ifdef ICL_KERNEL_PROXY
101265509Strasz	int				p_id;
102265509Strasz#endif
103255570Strasz
104255570Strasz	TAILQ_HEAD(, target)		p_targets;
105255570Strasz	int				p_socket;
106255570Strasz};
107255570Strasz
108291387SmavTAILQ_HEAD(options, option);
109291387Smav
110275244Strasz#define	PG_FILTER_UNKNOWN		0
111275244Strasz#define	PG_FILTER_NONE			1
112275244Strasz#define	PG_FILTER_PORTAL		2
113275244Strasz#define	PG_FILTER_PORTAL_NAME		3
114275244Strasz#define	PG_FILTER_PORTAL_NAME_AUTH	4
115275244Strasz
116255570Straszstruct portal_group {
117255570Strasz	TAILQ_ENTRY(portal_group)	pg_next;
118255570Strasz	struct conf			*pg_conf;
119291387Smav	struct options			pg_options;
120255570Strasz	char				*pg_name;
121255570Strasz	struct auth_group		*pg_discovery_auth_group;
122275244Strasz	int				pg_discovery_filter;
123288729Smav	int				pg_foreign;
124255570Strasz	bool				pg_unassigned;
125255570Strasz	TAILQ_HEAD(, portal)		pg_portals;
126279006Smav	TAILQ_HEAD(, port)		pg_ports;
127275642Strasz	char				*pg_redirection;
128255570Strasz
129255570Strasz	uint16_t			pg_tag;
130255570Strasz};
131255570Strasz
132279055Smavstruct pport {
133279055Smav	TAILQ_ENTRY(pport)		pp_next;
134279055Smav	TAILQ_HEAD(, port)		pp_ports;
135279055Smav	struct conf			*pp_conf;
136279055Smav	char				*pp_name;
137279055Smav
138279055Smav	uint32_t			pp_ctl_port;
139279055Smav};
140279055Smav
141279006Smavstruct port {
142279006Smav	TAILQ_ENTRY(port)		p_next;
143279006Smav	TAILQ_ENTRY(port)		p_pgs;
144279055Smav	TAILQ_ENTRY(port)		p_pps;
145279006Smav	TAILQ_ENTRY(port)		p_ts;
146279006Smav	struct conf			*p_conf;
147279006Smav	char				*p_name;
148279006Smav	struct auth_group		*p_auth_group;
149279006Smav	struct portal_group		*p_portal_group;
150279055Smav	struct pport			*p_pport;
151279006Smav	struct target			*p_target;
152288729Smav	int				p_foreign;
153279006Smav
154279006Smav	uint32_t			p_ctl_port;
155279006Smav};
156279006Smav
157291387Smavstruct option {
158291387Smav	TAILQ_ENTRY(option)		o_next;
159291387Smav	char				*o_name;
160291387Smav	char				*o_value;
161255570Strasz};
162255570Strasz
163255570Straszstruct lun {
164255570Strasz	TAILQ_ENTRY(lun)		l_next;
165279002Smav	struct conf			*l_conf;
166291387Smav	struct options			l_options;
167279002Smav	char				*l_name;
168255570Strasz	char				*l_backend;
169288810Smav	uint8_t				l_device_type;
170255570Strasz	int				l_blocksize;
171255570Strasz	char				*l_device_id;
172255570Strasz	char				*l_path;
173279002Smav	char				*l_scsiname;
174255570Strasz	char				*l_serial;
175255570Strasz	int64_t				l_size;
176255570Strasz
177255570Strasz	int				l_ctl_lun;
178255570Strasz};
179255570Strasz
180255570Straszstruct target {
181255570Strasz	TAILQ_ENTRY(target)		t_next;
182255570Strasz	struct conf			*t_conf;
183279002Smav	struct lun			*t_luns[MAX_LUNS];
184255570Strasz	struct auth_group		*t_auth_group;
185279006Smav	TAILQ_HEAD(, port)		t_ports;
186263723Strasz	char				*t_name;
187255570Strasz	char				*t_alias;
188275642Strasz	char				*t_redirection;
189255570Strasz};
190255570Strasz
191274939Smavstruct isns {
192274939Smav	TAILQ_ENTRY(isns)		i_next;
193274939Smav	struct conf			*i_conf;
194274939Smav	char				*i_addr;
195274939Smav	struct addrinfo			*i_ai;
196274939Smav};
197274939Smav
198255570Straszstruct conf {
199255570Strasz	char				*conf_pidfile_path;
200279002Smav	TAILQ_HEAD(, lun)		conf_luns;
201255570Strasz	TAILQ_HEAD(, target)		conf_targets;
202255570Strasz	TAILQ_HEAD(, auth_group)	conf_auth_groups;
203279006Smav	TAILQ_HEAD(, port)		conf_ports;
204255570Strasz	TAILQ_HEAD(, portal_group)	conf_portal_groups;
205279055Smav	TAILQ_HEAD(, pport)		conf_pports;
206274939Smav	TAILQ_HEAD(, isns)		conf_isns;
207274939Smav	int				conf_isns_period;
208274939Smav	int				conf_isns_timeout;
209255570Strasz	int				conf_debug;
210255570Strasz	int				conf_timeout;
211255570Strasz	int				conf_maxproc;
212255570Strasz
213265509Strasz#ifdef ICL_KERNEL_PROXY
214265509Strasz	int				conf_portal_id;
215265509Strasz#endif
216255570Strasz	struct pidfh			*conf_pidfh;
217263725Strasz
218263725Strasz	bool				conf_default_pg_defined;
219263725Strasz	bool				conf_default_ag_defined;
220265511Strasz	bool				conf_kernel_port_on;
221255570Strasz};
222255570Strasz
223255570Strasz#define	CONN_SESSION_TYPE_NONE		0
224255570Strasz#define	CONN_SESSION_TYPE_DISCOVERY	1
225255570Strasz#define	CONN_SESSION_TYPE_NORMAL	2
226255570Strasz
227255570Strasz#define	CONN_DIGEST_NONE		0
228255570Strasz#define	CONN_DIGEST_CRC32C		1
229255570Strasz
230255570Straszstruct connection {
231255570Strasz	struct portal		*conn_portal;
232279006Smav	struct port		*conn_port;
233255570Strasz	struct target		*conn_target;
234255570Strasz	int			conn_socket;
235255570Strasz	int			conn_session_type;
236255570Strasz	char			*conn_initiator_name;
237255570Strasz	char			*conn_initiator_addr;
238255570Strasz	char			*conn_initiator_alias;
239268684Smav	uint8_t			conn_initiator_isid[6];
240270137Smav	struct sockaddr_storage	conn_initiator_sa;
241255570Strasz	uint32_t		conn_cmdsn;
242255570Strasz	uint32_t		conn_statsn;
243255570Strasz	size_t			conn_max_data_segment_length;
244255570Strasz	size_t			conn_max_burst_length;
245255570Strasz	int			conn_immediate_data;
246255570Strasz	int			conn_header_digest;
247255570Strasz	int			conn_data_digest;
248275244Strasz	const char		*conn_user;
249275244Strasz	struct chap		*conn_chap;
250255570Strasz};
251255570Strasz
252255570Straszstruct pdu {
253255570Strasz	struct connection	*pdu_connection;
254255570Strasz	struct iscsi_bhs	*pdu_bhs;
255255570Strasz	char			*pdu_data;
256255570Strasz	size_t			pdu_data_len;
257255570Strasz};
258255570Strasz
259255570Strasz#define	KEYS_MAX	1024
260255570Strasz
261255570Straszstruct keys {
262255570Strasz	char		*keys_names[KEYS_MAX];
263255570Strasz	char		*keys_values[KEYS_MAX];
264255570Strasz	char		*keys_data;
265255570Strasz	size_t		keys_data_len;
266255570Strasz};
267255570Strasz
268274866Strasz#define	CHAP_CHALLENGE_LEN	1024
269286219Strasz#define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
270274866Strasz
271274866Straszstruct chap {
272274866Strasz	unsigned char	chap_id;
273274866Strasz	char		chap_challenge[CHAP_CHALLENGE_LEN];
274286219Strasz	char		chap_response[CHAP_DIGEST_LEN];
275274866Strasz};
276274866Strasz
277274866Straszstruct rchap {
278274866Strasz	char		*rchap_secret;
279274866Strasz	unsigned char	rchap_id;
280274866Strasz	void		*rchap_challenge;
281274866Strasz	size_t		rchap_challenge_len;
282274866Strasz};
283274866Strasz
284274866Straszstruct chap		*chap_new(void);
285274866Straszchar			*chap_get_id(const struct chap *chap);
286274866Straszchar			*chap_get_challenge(const struct chap *chap);
287274866Straszint			chap_receive(struct chap *chap, const char *response);
288274866Straszint			chap_authenticate(struct chap *chap,
289274866Strasz			    const char *secret);
290274866Straszvoid			chap_delete(struct chap *chap);
291274866Strasz
292274866Straszstruct rchap		*rchap_new(const char *secret);
293274866Straszint			rchap_receive(struct rchap *rchap,
294274866Strasz			    const char *id, const char *challenge);
295274866Straszchar			*rchap_get_response(struct rchap *rchap);
296274866Straszvoid			rchap_delete(struct rchap *rchap);
297274866Strasz
298255570Straszstruct conf		*conf_new(void);
299279055Smavstruct conf		*conf_new_from_file(const char *path, struct conf *old);
300255570Straszstruct conf		*conf_new_from_kernel(void);
301255570Straszvoid			conf_delete(struct conf *conf);
302255570Straszint			conf_verify(struct conf *conf);
303255570Strasz
304255570Straszstruct auth_group	*auth_group_new(struct conf *conf, const char *name);
305255570Straszvoid			auth_group_delete(struct auth_group *ag);
306265514Straszstruct auth_group	*auth_group_find(const struct conf *conf,
307265514Strasz			    const char *name);
308275245Straszint			auth_group_set_type(struct auth_group *ag,
309263724Strasz			    const char *type);
310255570Strasz
311255570Straszconst struct auth	*auth_new_chap(struct auth_group *ag,
312255570Strasz			    const char *user, const char *secret);
313255570Straszconst struct auth	*auth_new_chap_mutual(struct auth_group *ag,
314255570Strasz			    const char *user, const char *secret,
315255570Strasz			    const char *user2, const char *secret2);
316265514Straszconst struct auth	*auth_find(const struct auth_group *ag,
317255570Strasz			    const char *user);
318255570Strasz
319263720Straszconst struct auth_name	*auth_name_new(struct auth_group *ag,
320263720Strasz			    const char *initiator_name);
321263720Straszbool			auth_name_defined(const struct auth_group *ag);
322263720Straszconst struct auth_name	*auth_name_find(const struct auth_group *ag,
323263720Strasz			    const char *initiator_name);
324274949Straszint			auth_name_check(const struct auth_group *ag,
325274949Strasz			    const char *initiator_name);
326263720Strasz
327263720Straszconst struct auth_portal	*auth_portal_new(struct auth_group *ag,
328263720Strasz				    const char *initiator_portal);
329263720Straszbool			auth_portal_defined(const struct auth_group *ag);
330263720Straszconst struct auth_portal	*auth_portal_find(const struct auth_group *ag,
331270137Smav				    const struct sockaddr_storage *sa);
332274949Straszint				auth_portal_check(const struct auth_group *ag,
333274949Strasz				    const struct sockaddr_storage *sa);
334263720Strasz
335255570Straszstruct portal_group	*portal_group_new(struct conf *conf, const char *name);
336255570Straszvoid			portal_group_delete(struct portal_group *pg);
337265514Straszstruct portal_group	*portal_group_find(const struct conf *conf,
338265514Strasz			    const char *name);
339255570Straszint			portal_group_add_listen(struct portal_group *pg,
340255570Strasz			    const char *listen, bool iser);
341275245Straszint			portal_group_set_filter(struct portal_group *pg,
342275244Strasz			    const char *filter);
343275642Straszint			portal_group_set_redirection(struct portal_group *pg,
344275642Strasz			    const char *addr);
345255570Strasz
346274939Smavint			isns_new(struct conf *conf, const char *addr);
347274939Smavvoid			isns_delete(struct isns *is);
348274939Smavvoid			isns_register(struct isns *isns, struct isns *oldisns);
349274939Smavvoid			isns_check(struct isns *isns);
350274939Smavvoid			isns_deregister(struct isns *isns);
351274939Smav
352279055Smavstruct pport		*pport_new(struct conf *conf, const char *name,
353279055Smav			    uint32_t ctl_port);
354279055Smavstruct pport		*pport_find(const struct conf *conf, const char *name);
355279055Smavstruct pport		*pport_copy(struct pport *pport, struct conf *conf);
356279055Smavvoid			pport_delete(struct pport *pport);
357279055Smav
358279006Smavstruct port		*port_new(struct conf *conf, struct target *target,
359279006Smav			    struct portal_group *pg);
360279055Smavstruct port		*port_new_pp(struct conf *conf, struct target *target,
361279055Smav			    struct pport *pp);
362279006Smavstruct port		*port_find(const struct conf *conf, const char *name);
363279006Smavstruct port		*port_find_in_pg(const struct portal_group *pg,
364279006Smav			    const char *target);
365279006Smavvoid			port_delete(struct port *port);
366279006Smav
367263723Straszstruct target		*target_new(struct conf *conf, const char *name);
368255570Straszvoid			target_delete(struct target *target);
369255570Straszstruct target		*target_find(struct conf *conf,
370263723Strasz			    const char *name);
371275642Straszint			target_set_redirection(struct target *target,
372275642Strasz			    const char *addr);
373255570Strasz
374279002Smavstruct lun		*lun_new(struct conf *conf, const char *name);
375255570Straszvoid			lun_delete(struct lun *lun);
376279002Smavstruct lun		*lun_find(const struct conf *conf, const char *name);
377255570Straszvoid			lun_set_backend(struct lun *lun, const char *value);
378288810Smavvoid			lun_set_device_type(struct lun *lun, uint8_t value);
379255570Straszvoid			lun_set_blocksize(struct lun *lun, size_t value);
380255570Straszvoid			lun_set_device_id(struct lun *lun, const char *value);
381255570Straszvoid			lun_set_path(struct lun *lun, const char *value);
382279002Smavvoid			lun_set_scsiname(struct lun *lun, const char *value);
383255570Straszvoid			lun_set_serial(struct lun *lun, const char *value);
384255570Straszvoid			lun_set_size(struct lun *lun, size_t value);
385255570Straszvoid			lun_set_ctl_lun(struct lun *lun, uint32_t value);
386255570Strasz
387291387Smavstruct option		*option_new(struct options *os,
388255570Strasz			    const char *name, const char *value);
389291387Smavvoid			option_delete(struct options *os, struct option *co);
390291387Smavstruct option		*option_find(const struct options *os, const char *name);
391291387Smavvoid			option_set(struct option *o, const char *value);
392255570Strasz
393255570Straszvoid			kernel_init(void);
394255570Straszint			kernel_lun_add(struct lun *lun);
395288728Smavint			kernel_lun_modify(struct lun *lun);
396255570Straszint			kernel_lun_remove(struct lun *lun);
397255570Straszvoid			kernel_handoff(struct connection *conn);
398279006Smavint			kernel_port_add(struct port *port);
399288748Smavint			kernel_port_update(struct port *port, struct port *old);
400279006Smavint			kernel_port_remove(struct port *port);
401255570Straszvoid			kernel_capsicate(void);
402255570Strasz
403265513Strasz#ifdef ICL_KERNEL_PROXY
404265509Straszvoid			kernel_listen(struct addrinfo *ai, bool iser,
405265509Strasz			    int portal_id);
406265513Straszvoid			kernel_accept(int *connection_id, int *portal_id,
407265513Strasz			    struct sockaddr *client_sa,
408265513Strasz			    socklen_t *client_salen);
409255570Straszvoid			kernel_send(struct pdu *pdu);
410255570Straszvoid			kernel_receive(struct pdu *pdu);
411265513Strasz#endif
412255570Strasz
413255570Straszstruct keys		*keys_new(void);
414255570Straszvoid			keys_delete(struct keys *keys);
415255570Straszvoid			keys_load(struct keys *keys, const struct pdu *pdu);
416255570Straszvoid			keys_save(struct keys *keys, struct pdu *pdu);
417255570Straszconst char		*keys_find(struct keys *keys, const char *name);
418255570Straszvoid			keys_add(struct keys *keys,
419255570Strasz			    const char *name, const char *value);
420255570Straszvoid			keys_add_int(struct keys *keys,
421255570Strasz			    const char *name, int value);
422255570Strasz
423255570Straszstruct pdu		*pdu_new(struct connection *conn);
424255570Straszstruct pdu		*pdu_new_response(struct pdu *request);
425255570Straszvoid			pdu_delete(struct pdu *pdu);
426255570Straszvoid			pdu_receive(struct pdu *request);
427255570Straszvoid			pdu_send(struct pdu *response);
428255570Strasz
429255570Straszvoid			login(struct connection *conn);
430255570Strasz
431255570Straszvoid			discovery(struct connection *conn);
432255570Strasz
433255570Straszvoid			log_init(int level);
434255570Straszvoid			log_set_peer_name(const char *name);
435255570Straszvoid			log_set_peer_addr(const char *addr);
436255570Straszvoid			log_err(int, const char *, ...)
437263732Strasz			    __dead2 __printflike(2, 3);
438255570Straszvoid			log_errx(int, const char *, ...)
439263732Strasz			    __dead2 __printflike(2, 3);
440263732Straszvoid			log_warn(const char *, ...) __printflike(1, 2);
441255570Straszvoid			log_warnx(const char *, ...) __printflike(1, 2);
442263732Straszvoid			log_debugx(const char *, ...) __printflike(1, 2);
443255570Strasz
444255570Straszchar			*checked_strdup(const char *);
445255570Straszbool			valid_iscsi_name(const char *name);
446274939Smavvoid			set_timeout(int timeout, int fatal);
447255570Straszbool			timed_out(void);
448255570Strasz
449255570Strasz#endif /* !CTLD_H */
450