xref: /freebsd/usr.sbin/ctld/ctld.h (revision dd3603749cb7f20a628f04d595b105962b21a3d2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
31 #ifndef CTLD_H
32 #define	CTLD_H
33 
34 #include <sys/queue.h>
35 #ifdef ICL_KERNEL_PROXY
36 #include <sys/types.h>
37 #endif
38 #include <sys/socket.h>
39 #include <stdbool.h>
40 #include <libiscsiutil.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_DATA_SEGMENT_LENGTH		(128 * 1024)
50 #define	SOCKBUF_SIZE			1048576
51 
52 struct auth {
53 	TAILQ_ENTRY(auth)		a_next;
54 	struct auth_group		*a_auth_group;
55 	char				*a_user;
56 	char				*a_secret;
57 	char				*a_mutual_user;
58 	char				*a_mutual_secret;
59 };
60 
61 struct auth_name {
62 	TAILQ_ENTRY(auth_name)		an_next;
63 	struct auth_group		*an_auth_group;
64 	char				*an_initiator_name;
65 };
66 
67 struct auth_portal {
68 	TAILQ_ENTRY(auth_portal)	ap_next;
69 	struct auth_group		*ap_auth_group;
70 	char				*ap_initiator_portal;
71 	struct sockaddr_storage		ap_sa;
72 	int				ap_mask;
73 };
74 
75 #define	AG_TYPE_UNKNOWN			0
76 #define	AG_TYPE_DENY			1
77 #define	AG_TYPE_NO_AUTHENTICATION	2
78 #define	AG_TYPE_CHAP			3
79 #define	AG_TYPE_CHAP_MUTUAL		4
80 
81 struct auth_group {
82 	TAILQ_ENTRY(auth_group)		ag_next;
83 	struct conf			*ag_conf;
84 	char				*ag_name;
85 	struct target			*ag_target;
86 	int				ag_type;
87 	TAILQ_HEAD(, auth)		ag_auths;
88 	TAILQ_HEAD(, auth_name)		ag_names;
89 	TAILQ_HEAD(, auth_portal)	ag_portals;
90 };
91 
92 struct portal {
93 	TAILQ_ENTRY(portal)		p_next;
94 	struct portal_group		*p_portal_group;
95 	bool				p_iser;
96 	char				*p_listen;
97 	struct addrinfo			*p_ai;
98 #ifdef ICL_KERNEL_PROXY
99 	int				p_id;
100 #endif
101 
102 	TAILQ_HEAD(, target)		p_targets;
103 	int				p_socket;
104 };
105 
106 TAILQ_HEAD(options, option);
107 
108 #define	PG_FILTER_UNKNOWN		0
109 #define	PG_FILTER_NONE			1
110 #define	PG_FILTER_PORTAL		2
111 #define	PG_FILTER_PORTAL_NAME		3
112 #define	PG_FILTER_PORTAL_NAME_AUTH	4
113 
114 struct portal_group {
115 	TAILQ_ENTRY(portal_group)	pg_next;
116 	struct conf			*pg_conf;
117 	struct options			pg_options;
118 	char				*pg_name;
119 	struct auth_group		*pg_discovery_auth_group;
120 	int				pg_discovery_filter;
121 	int				pg_foreign;
122 	bool				pg_unassigned;
123 	TAILQ_HEAD(, portal)		pg_portals;
124 	TAILQ_HEAD(, port)		pg_ports;
125 	char				*pg_offload;
126 	char				*pg_redirection;
127 	int				pg_dscp;
128 	int				pg_pcp;
129 
130 	uint16_t			pg_tag;
131 };
132 
133 /* Ports created by the kernel.  Perhaps the "p" means "physical" ? */
134 struct pport {
135 	TAILQ_ENTRY(pport)		pp_next;
136 	TAILQ_HEAD(, port)		pp_ports;
137 	struct kports			*pp_kports;
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 	/* Name of this target's physical port, if any, i.e. "isp0" */
194 	char				*t_pport;
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(, isns)		conf_isns;
212 	int				conf_isns_period;
213 	int				conf_isns_timeout;
214 	int				conf_debug;
215 	int				conf_timeout;
216 	int				conf_maxproc;
217 
218 #ifdef ICL_KERNEL_PROXY
219 	int				conf_portal_id;
220 #endif
221 	struct pidfh			*conf_pidfh;
222 
223 	bool				conf_default_pg_defined;
224 	bool				conf_default_ag_defined;
225 	bool				conf_kernel_port_on;
226 };
227 
228 /* Physical ports exposed by the kernel */
229 struct kports {
230 	TAILQ_HEAD(, pport)		pports;
231 };
232 
233 #define	CONN_SESSION_TYPE_NONE		0
234 #define	CONN_SESSION_TYPE_DISCOVERY	1
235 #define	CONN_SESSION_TYPE_NORMAL	2
236 
237 struct ctld_connection {
238 	struct connection	conn;
239 	struct portal		*conn_portal;
240 	struct port		*conn_port;
241 	struct target		*conn_target;
242 	int			conn_session_type;
243 	char			*conn_initiator_name;
244 	char			*conn_initiator_addr;
245 	char			*conn_initiator_alias;
246 	uint8_t			conn_initiator_isid[6];
247 	struct sockaddr_storage	conn_initiator_sa;
248 	int			conn_max_recv_data_segment_limit;
249 	int			conn_max_send_data_segment_limit;
250 	int			conn_max_burst_limit;
251 	int			conn_first_burst_limit;
252 	const char		*conn_user;
253 	struct chap		*conn_chap;
254 };
255 
256 int			parse_conf(struct conf *newconf, const char *path);
257 int			uclparse_conf(struct conf *conf, const char *path);
258 
259 struct conf		*conf_new(void);
260 struct conf		*conf_new_from_kernel(struct kports *kports);
261 void			conf_delete(struct conf *conf);
262 int			conf_verify(struct conf *conf);
263 
264 struct auth_group	*auth_group_new(struct conf *conf, const char *name);
265 void			auth_group_delete(struct auth_group *ag);
266 struct auth_group	*auth_group_find(const struct conf *conf,
267 			    const char *name);
268 int			auth_group_set_type(struct auth_group *ag,
269 			    const char *type);
270 
271 const struct auth	*auth_new_chap(struct auth_group *ag,
272 			    const char *user, const char *secret);
273 const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
274 			    const char *user, const char *secret,
275 			    const char *user2, const char *secret2);
276 const struct auth	*auth_find(const struct auth_group *ag,
277 			    const char *user);
278 
279 const struct auth_name	*auth_name_new(struct auth_group *ag,
280 			    const char *initiator_name);
281 bool			auth_name_defined(const struct auth_group *ag);
282 const struct auth_name	*auth_name_find(const struct auth_group *ag,
283 			    const char *initiator_name);
284 int			auth_name_check(const struct auth_group *ag,
285 			    const char *initiator_name);
286 
287 const struct auth_portal	*auth_portal_new(struct auth_group *ag,
288 				    const char *initiator_portal);
289 bool			auth_portal_defined(const struct auth_group *ag);
290 const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
291 				    const struct sockaddr_storage *sa);
292 int				auth_portal_check(const struct auth_group *ag,
293 				    const struct sockaddr_storage *sa);
294 
295 struct portal_group	*portal_group_new(struct conf *conf, const char *name);
296 void			portal_group_delete(struct portal_group *pg);
297 struct portal_group	*portal_group_find(const struct conf *conf,
298 			    const char *name);
299 int			portal_group_add_listen(struct portal_group *pg,
300 			    const char *listen, bool iser);
301 int			portal_group_set_filter(struct portal_group *pg,
302 			    const char *filter);
303 int			portal_group_set_offload(struct portal_group *pg,
304 			    const char *offload);
305 int			portal_group_set_redirection(struct portal_group *pg,
306 			    const char *addr);
307 
308 int			isns_new(struct conf *conf, const char *addr);
309 void			isns_delete(struct isns *is);
310 void			isns_register(struct isns *isns, struct isns *oldisns);
311 void			isns_check(struct isns *isns);
312 void			isns_deregister(struct isns *isns);
313 
314 struct pport		*pport_new(struct kports *kports, const char *name,
315 			    uint32_t ctl_port);
316 struct pport		*pport_find(const struct kports *kports,
317 			    const char *name);
318 struct pport		*pport_copy(struct pport *pp, struct kports *kports);
319 void			pport_delete(struct pport *pport);
320 
321 struct port		*port_new(struct conf *conf, struct target *target,
322 			    struct portal_group *pg);
323 struct port		*port_new_ioctl(struct conf *conf,
324 			    struct kports *kports, struct target *target,
325 			    int pp, int vp);
326 struct port		*port_new_pp(struct conf *conf, struct target *target,
327 			    struct pport *pp);
328 struct port		*port_find(const struct conf *conf, const char *name);
329 struct port		*port_find_in_pg(const struct portal_group *pg,
330 			    const char *target);
331 void			port_delete(struct port *port);
332 int			port_is_dummy(struct port *port);
333 
334 struct target		*target_new(struct conf *conf, const char *name);
335 void			target_delete(struct target *target);
336 struct target		*target_find(struct conf *conf,
337 			    const char *name);
338 int			target_set_redirection(struct target *target,
339 			    const char *addr);
340 
341 struct lun		*lun_new(struct conf *conf, const char *name);
342 void			lun_delete(struct lun *lun);
343 struct lun		*lun_find(const struct conf *conf, const char *name);
344 void			lun_set_backend(struct lun *lun, const char *value);
345 void			lun_set_device_type(struct lun *lun, uint8_t value);
346 void			lun_set_blocksize(struct lun *lun, size_t value);
347 void			lun_set_device_id(struct lun *lun, const char *value);
348 void			lun_set_path(struct lun *lun, const char *value);
349 void			lun_set_scsiname(struct lun *lun, const char *value);
350 void			lun_set_serial(struct lun *lun, const char *value);
351 void			lun_set_size(struct lun *lun, int64_t value);
352 void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
353 
354 struct option		*option_new(struct options *os,
355 			    const char *name, const char *value);
356 void			option_delete(struct options *os, struct option *co);
357 struct option		*option_find(const struct options *os, const char *name);
358 void			option_set(struct option *o, const char *value);
359 
360 void			kernel_init(void);
361 int			kernel_lun_add(struct lun *lun);
362 int			kernel_lun_modify(struct lun *lun);
363 int			kernel_lun_remove(struct lun *lun);
364 void			kernel_handoff(struct ctld_connection *conn);
365 void			kernel_limits(const char *offload, int s,
366 			    int *max_recv_data_segment_length,
367 			    int *max_send_data_segment_length,
368 			    int *max_burst_length,
369 			    int *first_burst_length);
370 int			kernel_port_add(struct port *port);
371 int			kernel_port_update(struct port *port, struct port *old);
372 int			kernel_port_remove(struct port *port);
373 void			kernel_capsicate(void);
374 
375 #ifdef ICL_KERNEL_PROXY
376 void			kernel_listen(struct addrinfo *ai, bool iser,
377 			    int portal_id);
378 void			kernel_accept(int *connection_id, int *portal_id,
379 			    struct sockaddr *client_sa,
380 			    socklen_t *client_salen);
381 void			kernel_send(struct pdu *pdu);
382 void			kernel_receive(struct pdu *pdu);
383 #endif
384 
385 void			login(struct ctld_connection *conn);
386 
387 void			discovery(struct ctld_connection *conn);
388 
389 void			set_timeout(int timeout, int fatal);
390 
391 #endif /* !CTLD_H */
392