xref: /freebsd/usr.sbin/ctld/ctld.h (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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$
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	SOCKBUF_SIZE			1048576
54 
55 struct 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 
64 struct auth_name {
65 	TAILQ_ENTRY(auth_name)		an_next;
66 	struct auth_group		*an_auth_group;
67 	char				*an_initator_name;
68 };
69 
70 struct 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 
84 struct 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 
95 struct 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 
109 TAILQ_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 
117 struct 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 
134 struct 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 
143 struct 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 
155 	int				p_ioctl_port;
156 	int				p_ioctl_pp;
157 	int				p_ioctl_vp;
158 	uint32_t			p_ctl_port;
159 };
160 
161 struct option {
162 	TAILQ_ENTRY(option)		o_next;
163 	char				*o_name;
164 	char				*o_value;
165 };
166 
167 struct lun {
168 	TAILQ_ENTRY(lun)		l_next;
169 	struct conf			*l_conf;
170 	struct options			l_options;
171 	char				*l_name;
172 	char				*l_backend;
173 	uint8_t				l_device_type;
174 	int				l_blocksize;
175 	char				*l_device_id;
176 	char				*l_path;
177 	char				*l_scsiname;
178 	char				*l_serial;
179 	int64_t				l_size;
180 
181 	int				l_ctl_lun;
182 };
183 
184 struct target {
185 	TAILQ_ENTRY(target)		t_next;
186 	struct conf			*t_conf;
187 	struct lun			*t_luns[MAX_LUNS];
188 	struct auth_group		*t_auth_group;
189 	TAILQ_HEAD(, port)		t_ports;
190 	char				*t_name;
191 	char				*t_alias;
192 	char				*t_redirection;
193 };
194 
195 struct isns {
196 	TAILQ_ENTRY(isns)		i_next;
197 	struct conf			*i_conf;
198 	char				*i_addr;
199 	struct addrinfo			*i_ai;
200 };
201 
202 struct conf {
203 	char				*conf_pidfile_path;
204 	TAILQ_HEAD(, lun)		conf_luns;
205 	TAILQ_HEAD(, target)		conf_targets;
206 	TAILQ_HEAD(, auth_group)	conf_auth_groups;
207 	TAILQ_HEAD(, port)		conf_ports;
208 	TAILQ_HEAD(, portal_group)	conf_portal_groups;
209 	TAILQ_HEAD(, pport)		conf_pports;
210 	TAILQ_HEAD(, isns)		conf_isns;
211 	int				conf_isns_period;
212 	int				conf_isns_timeout;
213 	int				conf_debug;
214 	int				conf_timeout;
215 	int				conf_maxproc;
216 
217 #ifdef ICL_KERNEL_PROXY
218 	int				conf_portal_id;
219 #endif
220 	struct pidfh			*conf_pidfh;
221 
222 	bool				conf_default_pg_defined;
223 	bool				conf_default_ag_defined;
224 	bool				conf_kernel_port_on;
225 };
226 
227 #define	CONN_SESSION_TYPE_NONE		0
228 #define	CONN_SESSION_TYPE_DISCOVERY	1
229 #define	CONN_SESSION_TYPE_NORMAL	2
230 
231 #define	CONN_DIGEST_NONE		0
232 #define	CONN_DIGEST_CRC32C		1
233 
234 struct connection {
235 	struct portal		*conn_portal;
236 	struct port		*conn_port;
237 	struct target		*conn_target;
238 	int			conn_socket;
239 	int			conn_session_type;
240 	char			*conn_initiator_name;
241 	char			*conn_initiator_addr;
242 	char			*conn_initiator_alias;
243 	uint8_t			conn_initiator_isid[6];
244 	struct sockaddr_storage	conn_initiator_sa;
245 	uint32_t		conn_cmdsn;
246 	uint32_t		conn_statsn;
247 	int			conn_max_recv_data_segment_limit;
248 	int			conn_max_send_data_segment_limit;
249 	int			conn_max_burst_limit;
250 	int			conn_first_burst_limit;
251 	int			conn_max_recv_data_segment_length;
252 	int			conn_max_send_data_segment_length;
253 	int			conn_max_burst_length;
254 	int			conn_first_burst_length;
255 	int			conn_immediate_data;
256 	int			conn_header_digest;
257 	int			conn_data_digest;
258 	const char		*conn_user;
259 	struct chap		*conn_chap;
260 };
261 
262 struct pdu {
263 	struct connection	*pdu_connection;
264 	struct iscsi_bhs	*pdu_bhs;
265 	char			*pdu_data;
266 	size_t			pdu_data_len;
267 };
268 
269 #define	KEYS_MAX	1024
270 
271 struct keys {
272 	char		*keys_names[KEYS_MAX];
273 	char		*keys_values[KEYS_MAX];
274 	char		*keys_data;
275 	size_t		keys_data_len;
276 };
277 
278 #define	CHAP_CHALLENGE_LEN	1024
279 #define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
280 
281 struct chap {
282 	unsigned char	chap_id;
283 	char		chap_challenge[CHAP_CHALLENGE_LEN];
284 	char		chap_response[CHAP_DIGEST_LEN];
285 };
286 
287 struct rchap {
288 	char		*rchap_secret;
289 	unsigned char	rchap_id;
290 	void		*rchap_challenge;
291 	size_t		rchap_challenge_len;
292 };
293 
294 struct chap		*chap_new(void);
295 char			*chap_get_id(const struct chap *chap);
296 char			*chap_get_challenge(const struct chap *chap);
297 int			chap_receive(struct chap *chap, const char *response);
298 int			chap_authenticate(struct chap *chap,
299 			    const char *secret);
300 void			chap_delete(struct chap *chap);
301 
302 struct rchap		*rchap_new(const char *secret);
303 int			rchap_receive(struct rchap *rchap,
304 			    const char *id, const char *challenge);
305 char			*rchap_get_response(struct rchap *rchap);
306 void			rchap_delete(struct rchap *rchap);
307 
308 int			parse_conf(struct conf *conf, const char *path);
309 int			uclparse_conf(struct conf *conf, const char *path);
310 
311 struct conf		*conf_new(void);
312 struct conf		*conf_new_from_kernel(void);
313 void			conf_delete(struct conf *conf);
314 int			conf_verify(struct conf *conf);
315 
316 struct auth_group	*auth_group_new(struct conf *conf, const char *name);
317 void			auth_group_delete(struct auth_group *ag);
318 struct auth_group	*auth_group_find(const struct conf *conf,
319 			    const char *name);
320 int			auth_group_set_type(struct auth_group *ag,
321 			    const char *type);
322 
323 const struct auth	*auth_new_chap(struct auth_group *ag,
324 			    const char *user, const char *secret);
325 const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
326 			    const char *user, const char *secret,
327 			    const char *user2, const char *secret2);
328 const struct auth	*auth_find(const struct auth_group *ag,
329 			    const char *user);
330 
331 const struct auth_name	*auth_name_new(struct auth_group *ag,
332 			    const char *initiator_name);
333 bool			auth_name_defined(const struct auth_group *ag);
334 const struct auth_name	*auth_name_find(const struct auth_group *ag,
335 			    const char *initiator_name);
336 int			auth_name_check(const struct auth_group *ag,
337 			    const char *initiator_name);
338 
339 const struct auth_portal	*auth_portal_new(struct auth_group *ag,
340 				    const char *initiator_portal);
341 bool			auth_portal_defined(const struct auth_group *ag);
342 const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
343 				    const struct sockaddr_storage *sa);
344 int				auth_portal_check(const struct auth_group *ag,
345 				    const struct sockaddr_storage *sa);
346 
347 struct portal_group	*portal_group_new(struct conf *conf, const char *name);
348 void			portal_group_delete(struct portal_group *pg);
349 struct portal_group	*portal_group_find(const struct conf *conf,
350 			    const char *name);
351 int			portal_group_add_listen(struct portal_group *pg,
352 			    const char *listen, bool iser);
353 int			portal_group_set_filter(struct portal_group *pg,
354 			    const char *filter);
355 int			portal_group_set_offload(struct portal_group *pg,
356 			    const char *offload);
357 int			portal_group_set_redirection(struct portal_group *pg,
358 			    const char *addr);
359 
360 int			isns_new(struct conf *conf, const char *addr);
361 void			isns_delete(struct isns *is);
362 void			isns_register(struct isns *isns, struct isns *oldisns);
363 void			isns_check(struct isns *isns);
364 void			isns_deregister(struct isns *isns);
365 
366 struct pport		*pport_new(struct conf *conf, const char *name,
367 			    uint32_t ctl_port);
368 struct pport		*pport_find(const struct conf *conf, const char *name);
369 struct pport		*pport_copy(struct pport *pport, struct conf *conf);
370 void			pport_delete(struct pport *pport);
371 
372 struct port		*port_new(struct conf *conf, struct target *target,
373 			    struct portal_group *pg);
374 struct port		*port_new_ioctl(struct conf *conf, struct target *target,
375 			    int pp, int vp);
376 struct port		*port_new_pp(struct conf *conf, struct target *target,
377 			    struct pport *pp);
378 struct port		*port_find(const struct conf *conf, const char *name);
379 struct port		*port_find_in_pg(const struct portal_group *pg,
380 			    const char *target);
381 void			port_delete(struct port *port);
382 int			port_is_dummy(struct port *port);
383 
384 struct target		*target_new(struct conf *conf, const char *name);
385 void			target_delete(struct target *target);
386 struct target		*target_find(struct conf *conf,
387 			    const char *name);
388 int			target_set_redirection(struct target *target,
389 			    const char *addr);
390 
391 struct lun		*lun_new(struct conf *conf, const char *name);
392 void			lun_delete(struct lun *lun);
393 struct lun		*lun_find(const struct conf *conf, const char *name);
394 void			lun_set_backend(struct lun *lun, const char *value);
395 void			lun_set_device_type(struct lun *lun, uint8_t value);
396 void			lun_set_blocksize(struct lun *lun, size_t value);
397 void			lun_set_device_id(struct lun *lun, const char *value);
398 void			lun_set_path(struct lun *lun, const char *value);
399 void			lun_set_scsiname(struct lun *lun, const char *value);
400 void			lun_set_serial(struct lun *lun, const char *value);
401 void			lun_set_size(struct lun *lun, size_t value);
402 void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
403 
404 struct option		*option_new(struct options *os,
405 			    const char *name, const char *value);
406 void			option_delete(struct options *os, struct option *co);
407 struct option		*option_find(const struct options *os, const char *name);
408 void			option_set(struct option *o, const char *value);
409 
410 void			kernel_init(void);
411 int			kernel_lun_add(struct lun *lun);
412 int			kernel_lun_modify(struct lun *lun);
413 int			kernel_lun_remove(struct lun *lun);
414 void			kernel_handoff(struct connection *conn);
415 void			kernel_limits(const char *offload,
416 			    int *max_recv_data_segment_length,
417 			    int *max_send_data_segment_length,
418 			    int *max_burst_length,
419 			    int *first_burst_length);
420 int			kernel_port_add(struct port *port);
421 int			kernel_port_update(struct port *port, struct port *old);
422 int			kernel_port_remove(struct port *port);
423 void			kernel_capsicate(void);
424 
425 #ifdef ICL_KERNEL_PROXY
426 void			kernel_listen(struct addrinfo *ai, bool iser,
427 			    int portal_id);
428 void			kernel_accept(int *connection_id, int *portal_id,
429 			    struct sockaddr *client_sa,
430 			    socklen_t *client_salen);
431 void			kernel_send(struct pdu *pdu);
432 void			kernel_receive(struct pdu *pdu);
433 #endif
434 
435 struct keys		*keys_new(void);
436 void			keys_delete(struct keys *keys);
437 void			keys_load(struct keys *keys, const struct pdu *pdu);
438 void			keys_save(struct keys *keys, struct pdu *pdu);
439 const char		*keys_find(struct keys *keys, const char *name);
440 void			keys_add(struct keys *keys,
441 			    const char *name, const char *value);
442 void			keys_add_int(struct keys *keys,
443 			    const char *name, int value);
444 
445 struct pdu		*pdu_new(struct connection *conn);
446 struct pdu		*pdu_new_response(struct pdu *request);
447 void			pdu_delete(struct pdu *pdu);
448 void			pdu_receive(struct pdu *request);
449 void			pdu_send(struct pdu *response);
450 
451 void			login(struct connection *conn);
452 
453 void			discovery(struct connection *conn);
454 
455 void			log_init(int level);
456 void			log_set_peer_name(const char *name);
457 void			log_set_peer_addr(const char *addr);
458 void			log_err(int, const char *, ...)
459 			    __dead2 __printflike(2, 3);
460 void			log_errx(int, const char *, ...)
461 			    __dead2 __printflike(2, 3);
462 void			log_warn(const char *, ...) __printflike(1, 2);
463 void			log_warnx(const char *, ...) __printflike(1, 2);
464 void			log_debugx(const char *, ...) __printflike(1, 2);
465 
466 char			*checked_strdup(const char *);
467 bool			valid_iscsi_name(const char *name);
468 void			set_timeout(int timeout, int fatal);
469 bool			timed_out(void);
470 
471 #endif /* !CTLD_H */
472