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$
30 */
31
32#ifndef CTLD_H
33#define	CTLD_H
34
35#include <sys/queue.h>
36#include <stdbool.h>
37#include <libutil.h>
38
39#define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
40#define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
41#define	DEFAULT_BLOCKSIZE		512
42
43#define	MAX_NAME_LEN			223
44#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
45#define	MAX_BURST_LENGTH		16776192
46
47struct auth {
48	TAILQ_ENTRY(auth)		a_next;
49	struct auth_group		*a_auth_group;
50	char				*a_user;
51	char				*a_secret;
52	char				*a_mutual_user;
53	char				*a_mutual_secret;
54};
55
56#define	AG_TYPE_UNKNOWN			0
57#define	AG_TYPE_NO_AUTHENTICATION	1
58#define	AG_TYPE_CHAP			2
59#define	AG_TYPE_CHAP_MUTUAL		3
60
61struct auth_group {
62	TAILQ_ENTRY(auth_group)		ag_next;
63	struct conf			*ag_conf;
64	char				*ag_name;
65	struct target			*ag_target;
66	int				ag_type;
67	TAILQ_HEAD(, auth)		ag_auths;
68};
69
70struct portal {
71	TAILQ_ENTRY(portal)		p_next;
72	struct portal_group		*p_portal_group;
73	bool				p_iser;
74	char				*p_listen;
75	struct addrinfo			*p_ai;
76
77	TAILQ_HEAD(, target)		p_targets;
78	int				p_socket;
79};
80
81struct portal_group {
82	TAILQ_ENTRY(portal_group)	pg_next;
83	struct conf			*pg_conf;
84	char				*pg_name;
85	struct auth_group		*pg_discovery_auth_group;
86	bool				pg_unassigned;
87	TAILQ_HEAD(, portal)		pg_portals;
88
89	uint16_t			pg_tag;
90};
91
92struct lun_option {
93	TAILQ_ENTRY(lun_option)		lo_next;
94	struct lun			*lo_lun;
95	char				*lo_name;
96	char				*lo_value;
97};
98
99struct lun {
100	TAILQ_ENTRY(lun)		l_next;
101	TAILQ_HEAD(, lun_option)	l_options;
102	struct target			*l_target;
103	int				l_lun;
104	char				*l_backend;
105	int				l_blocksize;
106	char				*l_device_id;
107	char				*l_path;
108	char				*l_serial;
109	int64_t				l_size;
110
111	int				l_ctl_lun;
112};
113
114struct target {
115	TAILQ_ENTRY(target)		t_next;
116	TAILQ_HEAD(, lun)		t_luns;
117	struct conf			*t_conf;
118	struct auth_group		*t_auth_group;
119	struct portal_group		*t_portal_group;
120	char				*t_iqn;
121	char				*t_alias;
122};
123
124struct conf {
125	char				*conf_pidfile_path;
126	TAILQ_HEAD(, target)		conf_targets;
127	TAILQ_HEAD(, auth_group)	conf_auth_groups;
128	TAILQ_HEAD(, portal_group)	conf_portal_groups;
129	int				conf_debug;
130	int				conf_timeout;
131	int				conf_maxproc;
132
133	uint16_t			conf_last_portal_group_tag;
134	struct pidfh			*conf_pidfh;
135};
136
137#define	CONN_SESSION_TYPE_NONE		0
138#define	CONN_SESSION_TYPE_DISCOVERY	1
139#define	CONN_SESSION_TYPE_NORMAL	2
140
141#define	CONN_DIGEST_NONE		0
142#define	CONN_DIGEST_CRC32C		1
143
144struct connection {
145	struct portal		*conn_portal;
146	struct target		*conn_target;
147	int			conn_socket;
148	int			conn_session_type;
149	char			*conn_initiator_name;
150	char			*conn_initiator_addr;
151	char			*conn_initiator_alias;
152	uint32_t		conn_cmdsn;
153	uint32_t		conn_statsn;
154	size_t			conn_max_data_segment_length;
155	size_t			conn_max_burst_length;
156	int			conn_immediate_data;
157	int			conn_header_digest;
158	int			conn_data_digest;
159};
160
161struct pdu {
162	struct connection	*pdu_connection;
163	struct iscsi_bhs	*pdu_bhs;
164	char			*pdu_data;
165	size_t			pdu_data_len;
166};
167
168#define	KEYS_MAX	1024
169
170struct keys {
171	char		*keys_names[KEYS_MAX];
172	char		*keys_values[KEYS_MAX];
173	char		*keys_data;
174	size_t		keys_data_len;
175};
176
177struct conf		*conf_new(void);
178struct conf		*conf_new_from_file(const char *path);
179struct conf		*conf_new_from_kernel(void);
180void			conf_delete(struct conf *conf);
181int			conf_verify(struct conf *conf);
182
183struct auth_group	*auth_group_new(struct conf *conf, const char *name);
184void			auth_group_delete(struct auth_group *ag);
185struct auth_group	*auth_group_find(struct conf *conf, const char *name);
186
187const struct auth	*auth_new_chap(struct auth_group *ag,
188			    const char *user, const char *secret);
189const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
190			    const char *user, const char *secret,
191			    const char *user2, const char *secret2);
192const struct auth	*auth_find(struct auth_group *ag,
193			    const char *user);
194
195struct portal_group	*portal_group_new(struct conf *conf, const char *name);
196void			portal_group_delete(struct portal_group *pg);
197struct portal_group	*portal_group_find(struct conf *conf, const char *name);
198int			portal_group_add_listen(struct portal_group *pg,
199			    const char *listen, bool iser);
200
201struct target		*target_new(struct conf *conf, const char *iqn);
202void			target_delete(struct target *target);
203struct target		*target_find(struct conf *conf,
204			    const char *iqn);
205
206struct lun		*lun_new(struct target *target, int lun_id);
207void			lun_delete(struct lun *lun);
208struct lun		*lun_find(struct target *target, int lun_id);
209void			lun_set_backend(struct lun *lun, const char *value);
210void			lun_set_blocksize(struct lun *lun, size_t value);
211void			lun_set_device_id(struct lun *lun, const char *value);
212void			lun_set_path(struct lun *lun, const char *value);
213void			lun_set_serial(struct lun *lun, const char *value);
214void			lun_set_size(struct lun *lun, size_t value);
215void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
216
217struct lun_option	*lun_option_new(struct lun *lun,
218			    const char *name, const char *value);
219void			lun_option_delete(struct lun_option *clo);
220struct lun_option	*lun_option_find(struct lun *lun, const char *name);
221void			lun_option_set(struct lun_option *clo,
222			    const char *value);
223
224void			kernel_init(void);
225int			kernel_lun_add(struct lun *lun);
226int			kernel_lun_resize(struct lun *lun);
227int			kernel_lun_remove(struct lun *lun);
228void			kernel_handoff(struct connection *conn);
229int			kernel_port_on(void);
230int			kernel_port_off(void);
231void			kernel_capsicate(void);
232
233/*
234 * ICL_KERNEL_PROXY
235 */
236void			kernel_listen(struct addrinfo *ai, bool iser);
237int			kernel_accept(void);
238void			kernel_send(struct pdu *pdu);
239void			kernel_receive(struct pdu *pdu);
240
241struct keys		*keys_new(void);
242void			keys_delete(struct keys *keys);
243void			keys_load(struct keys *keys, const struct pdu *pdu);
244void			keys_save(struct keys *keys, struct pdu *pdu);
245const char		*keys_find(struct keys *keys, const char *name);
246int			keys_find_int(struct keys *keys, const char *name);
247void			keys_add(struct keys *keys,
248			    const char *name, const char *value);
249void			keys_add_int(struct keys *keys,
250			    const char *name, int value);
251
252struct pdu		*pdu_new(struct connection *conn);
253struct pdu		*pdu_new_response(struct pdu *request);
254void			pdu_delete(struct pdu *pdu);
255void			pdu_receive(struct pdu *request);
256void			pdu_send(struct pdu *response);
257
258void			login(struct connection *conn);
259
260void			discovery(struct connection *conn);
261
262void			log_init(int level);
263void			log_set_peer_name(const char *name);
264void			log_set_peer_addr(const char *addr);
265void			log_err(int, const char *, ...)
266			    __dead2 __printf0like(2, 3);
267void			log_errx(int, const char *, ...)
268			    __dead2 __printf0like(2, 3);
269void			log_warn(const char *, ...) __printf0like(1, 2);
270void			log_warnx(const char *, ...) __printflike(1, 2);
271void			log_debugx(const char *, ...) __printf0like(1, 2);
272
273char			*checked_strdup(const char *);
274bool			valid_iscsi_name(const char *name);
275bool			timed_out(void);
276
277#endif /* !CTLD_H */
278