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