xref: /freebsd/usr.sbin/ctld/ctld.h (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef CTLD_H
34 #define	CTLD_H
35 
36 #include <sys/queue.h>
37 #ifdef ICL_KERNEL_PROXY
38 #include <sys/types.h>
39 #endif
40 #include <sys/socket.h>
41 #include <stdbool.h>
42 #include <libiscsiutil.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_initiator_name;
68 };
69 
70 struct auth_portal {
71 	TAILQ_ENTRY(auth_portal)	ap_next;
72 	struct auth_group		*ap_auth_group;
73 	char				*ap_initiator_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 	int				pg_dscp;
131 	int				pg_pcp;
132 
133 	uint16_t			pg_tag;
134 };
135 
136 struct pport {
137 	TAILQ_ENTRY(pport)		pp_next;
138 	TAILQ_HEAD(, port)		pp_ports;
139 	struct conf			*pp_conf;
140 	char				*pp_name;
141 
142 	uint32_t			pp_ctl_port;
143 };
144 
145 struct port {
146 	TAILQ_ENTRY(port)		p_next;
147 	TAILQ_ENTRY(port)		p_pgs;
148 	TAILQ_ENTRY(port)		p_pps;
149 	TAILQ_ENTRY(port)		p_ts;
150 	struct conf			*p_conf;
151 	char				*p_name;
152 	struct auth_group		*p_auth_group;
153 	struct portal_group		*p_portal_group;
154 	struct pport			*p_pport;
155 	struct target			*p_target;
156 
157 	int				p_ioctl_port;
158 	int				p_ioctl_pp;
159 	int				p_ioctl_vp;
160 	uint32_t			p_ctl_port;
161 };
162 
163 struct option {
164 	TAILQ_ENTRY(option)		o_next;
165 	char				*o_name;
166 	char				*o_value;
167 };
168 
169 struct lun {
170 	TAILQ_ENTRY(lun)		l_next;
171 	struct conf			*l_conf;
172 	struct options			l_options;
173 	char				*l_name;
174 	char				*l_backend;
175 	uint8_t				l_device_type;
176 	int				l_blocksize;
177 	char				*l_device_id;
178 	char				*l_path;
179 	char				*l_scsiname;
180 	char				*l_serial;
181 	int64_t				l_size;
182 
183 	int				l_ctl_lun;
184 };
185 
186 struct target {
187 	TAILQ_ENTRY(target)		t_next;
188 	struct conf			*t_conf;
189 	struct lun			*t_luns[MAX_LUNS];
190 	struct auth_group		*t_auth_group;
191 	TAILQ_HEAD(, port)		t_ports;
192 	char				*t_name;
193 	char				*t_alias;
194 	char				*t_redirection;
195 };
196 
197 struct isns {
198 	TAILQ_ENTRY(isns)		i_next;
199 	struct conf			*i_conf;
200 	char				*i_addr;
201 	struct addrinfo			*i_ai;
202 };
203 
204 struct conf {
205 	char				*conf_pidfile_path;
206 	TAILQ_HEAD(, lun)		conf_luns;
207 	TAILQ_HEAD(, target)		conf_targets;
208 	TAILQ_HEAD(, auth_group)	conf_auth_groups;
209 	TAILQ_HEAD(, port)		conf_ports;
210 	TAILQ_HEAD(, portal_group)	conf_portal_groups;
211 	TAILQ_HEAD(, pport)		conf_pports;
212 	TAILQ_HEAD(, isns)		conf_isns;
213 	int				conf_isns_period;
214 	int				conf_isns_timeout;
215 	int				conf_debug;
216 	int				conf_timeout;
217 	int				conf_maxproc;
218 
219 #ifdef ICL_KERNEL_PROXY
220 	int				conf_portal_id;
221 #endif
222 	struct pidfh			*conf_pidfh;
223 
224 	bool				conf_default_pg_defined;
225 	bool				conf_default_ag_defined;
226 	bool				conf_kernel_port_on;
227 };
228 
229 #define	CONN_SESSION_TYPE_NONE		0
230 #define	CONN_SESSION_TYPE_DISCOVERY	1
231 #define	CONN_SESSION_TYPE_NORMAL	2
232 
233 struct ctld_connection {
234 	struct connection	conn;
235 	struct portal		*conn_portal;
236 	struct port		*conn_port;
237 	struct target		*conn_target;
238 	int			conn_session_type;
239 	char			*conn_initiator_name;
240 	char			*conn_initiator_addr;
241 	char			*conn_initiator_alias;
242 	uint8_t			conn_initiator_isid[6];
243 	struct sockaddr_storage	conn_initiator_sa;
244 	int			conn_max_recv_data_segment_limit;
245 	int			conn_max_send_data_segment_limit;
246 	int			conn_max_burst_limit;
247 	int			conn_first_burst_limit;
248 	const char		*conn_user;
249 	struct chap		*conn_chap;
250 };
251 
252 int			parse_conf(struct conf *conf, const char *path);
253 int			uclparse_conf(struct conf *conf, const char *path);
254 
255 struct conf		*conf_new(void);
256 struct conf		*conf_new_from_kernel(void);
257 void			conf_delete(struct conf *conf);
258 int			conf_verify(struct conf *conf);
259 
260 struct auth_group	*auth_group_new(struct conf *conf, const char *name);
261 void			auth_group_delete(struct auth_group *ag);
262 struct auth_group	*auth_group_find(const struct conf *conf,
263 			    const char *name);
264 int			auth_group_set_type(struct auth_group *ag,
265 			    const char *type);
266 
267 const struct auth	*auth_new_chap(struct auth_group *ag,
268 			    const char *user, const char *secret);
269 const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
270 			    const char *user, const char *secret,
271 			    const char *user2, const char *secret2);
272 const struct auth	*auth_find(const struct auth_group *ag,
273 			    const char *user);
274 
275 const struct auth_name	*auth_name_new(struct auth_group *ag,
276 			    const char *initiator_name);
277 bool			auth_name_defined(const struct auth_group *ag);
278 const struct auth_name	*auth_name_find(const struct auth_group *ag,
279 			    const char *initiator_name);
280 int			auth_name_check(const struct auth_group *ag,
281 			    const char *initiator_name);
282 
283 const struct auth_portal	*auth_portal_new(struct auth_group *ag,
284 				    const char *initiator_portal);
285 bool			auth_portal_defined(const struct auth_group *ag);
286 const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
287 				    const struct sockaddr_storage *sa);
288 int				auth_portal_check(const struct auth_group *ag,
289 				    const struct sockaddr_storage *sa);
290 
291 struct portal_group	*portal_group_new(struct conf *conf, const char *name);
292 void			portal_group_delete(struct portal_group *pg);
293 struct portal_group	*portal_group_find(const struct conf *conf,
294 			    const char *name);
295 int			portal_group_add_listen(struct portal_group *pg,
296 			    const char *listen, bool iser);
297 int			portal_group_set_filter(struct portal_group *pg,
298 			    const char *filter);
299 int			portal_group_set_offload(struct portal_group *pg,
300 			    const char *offload);
301 int			portal_group_set_redirection(struct portal_group *pg,
302 			    const char *addr);
303 
304 int			isns_new(struct conf *conf, const char *addr);
305 void			isns_delete(struct isns *is);
306 void			isns_register(struct isns *isns, struct isns *oldisns);
307 void			isns_check(struct isns *isns);
308 void			isns_deregister(struct isns *isns);
309 
310 struct pport		*pport_new(struct conf *conf, const char *name,
311 			    uint32_t ctl_port);
312 struct pport		*pport_find(const struct conf *conf, const char *name);
313 struct pport		*pport_copy(struct pport *pport, struct conf *conf);
314 void			pport_delete(struct pport *pport);
315 
316 struct port		*port_new(struct conf *conf, struct target *target,
317 			    struct portal_group *pg);
318 struct port		*port_new_ioctl(struct conf *conf, struct target *target,
319 			    int pp, int vp);
320 struct port		*port_new_pp(struct conf *conf, struct target *target,
321 			    struct pport *pp);
322 struct port		*port_find(const struct conf *conf, const char *name);
323 struct port		*port_find_in_pg(const struct portal_group *pg,
324 			    const char *target);
325 void			port_delete(struct port *port);
326 int			port_is_dummy(struct port *port);
327 
328 struct target		*target_new(struct conf *conf, const char *name);
329 void			target_delete(struct target *target);
330 struct target		*target_find(struct conf *conf,
331 			    const char *name);
332 int			target_set_redirection(struct target *target,
333 			    const char *addr);
334 
335 struct lun		*lun_new(struct conf *conf, const char *name);
336 void			lun_delete(struct lun *lun);
337 struct lun		*lun_find(const struct conf *conf, const char *name);
338 void			lun_set_backend(struct lun *lun, const char *value);
339 void			lun_set_device_type(struct lun *lun, uint8_t value);
340 void			lun_set_blocksize(struct lun *lun, size_t value);
341 void			lun_set_device_id(struct lun *lun, const char *value);
342 void			lun_set_path(struct lun *lun, const char *value);
343 void			lun_set_scsiname(struct lun *lun, const char *value);
344 void			lun_set_serial(struct lun *lun, const char *value);
345 void			lun_set_size(struct lun *lun, size_t value);
346 void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
347 
348 struct option		*option_new(struct options *os,
349 			    const char *name, const char *value);
350 void			option_delete(struct options *os, struct option *co);
351 struct option		*option_find(const struct options *os, const char *name);
352 void			option_set(struct option *o, const char *value);
353 
354 void			kernel_init(void);
355 int			kernel_lun_add(struct lun *lun);
356 int			kernel_lun_modify(struct lun *lun);
357 int			kernel_lun_remove(struct lun *lun);
358 void			kernel_handoff(struct ctld_connection *conn);
359 void			kernel_limits(const char *offload, int s,
360 			    int *max_recv_data_segment_length,
361 			    int *max_send_data_segment_length,
362 			    int *max_burst_length,
363 			    int *first_burst_length);
364 int			kernel_port_add(struct port *port);
365 int			kernel_port_update(struct port *port, struct port *old);
366 int			kernel_port_remove(struct port *port);
367 void			kernel_capsicate(void);
368 
369 #ifdef ICL_KERNEL_PROXY
370 void			kernel_listen(struct addrinfo *ai, bool iser,
371 			    int portal_id);
372 void			kernel_accept(int *connection_id, int *portal_id,
373 			    struct sockaddr *client_sa,
374 			    socklen_t *client_salen);
375 void			kernel_send(struct pdu *pdu);
376 void			kernel_receive(struct pdu *pdu);
377 #endif
378 
379 void			login(struct ctld_connection *conn);
380 
381 void			discovery(struct ctld_connection *conn);
382 
383 bool			valid_iscsi_name(const char *name);
384 void			set_timeout(int timeout, int fatal);
385 
386 #endif /* !CTLD_H */
387