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