xref: /freebsd/usr.sbin/ctld/ctld.h (revision 703b03a8e325101f85610bf09f098442697f2f23)
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/_nv.h>
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 <libiscsiutil.h>
42 #include <libutil.h>
43 
44 #define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
45 #define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
46 #define	DEFAULT_BLOCKSIZE		512
47 #define	DEFAULT_CD_BLOCKSIZE		2048
48 
49 #define	MAX_LUNS			1024
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_initiator_name;
66 };
67 
68 struct auth_portal {
69 	TAILQ_ENTRY(auth_portal)	ap_next;
70 	struct auth_group		*ap_auth_group;
71 	char				*ap_initiator_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 #define	PG_FILTER_UNKNOWN		0
108 #define	PG_FILTER_NONE			1
109 #define	PG_FILTER_PORTAL		2
110 #define	PG_FILTER_PORTAL_NAME		3
111 #define	PG_FILTER_PORTAL_NAME_AUTH	4
112 
113 struct portal_group {
114 	TAILQ_ENTRY(portal_group)	pg_next;
115 	struct conf			*pg_conf;
116 	nvlist_t			*pg_options;
117 	char				*pg_name;
118 	struct auth_group		*pg_discovery_auth_group;
119 	int				pg_discovery_filter;
120 	int				pg_foreign;
121 	bool				pg_unassigned;
122 	TAILQ_HEAD(, portal)		pg_portals;
123 	TAILQ_HEAD(, port)		pg_ports;
124 	char				*pg_offload;
125 	char				*pg_redirection;
126 	int				pg_dscp;
127 	int				pg_pcp;
128 
129 	uint16_t			pg_tag;
130 };
131 
132 /* Ports created by the kernel.  Perhaps the "p" means "physical" ? */
133 struct pport {
134 	TAILQ_ENTRY(pport)		pp_next;
135 	TAILQ_HEAD(, port)		pp_ports;
136 	struct kports			*pp_kports;
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 
154 	int				p_ioctl_port;
155 	int				p_ioctl_pp;
156 	int				p_ioctl_vp;
157 	uint32_t			p_ctl_port;
158 };
159 
160 struct lun {
161 	TAILQ_ENTRY(lun)		l_next;
162 	struct conf			*l_conf;
163 	nvlist_t			*l_options;
164 	char				*l_name;
165 	char				*l_backend;
166 	uint8_t				l_device_type;
167 	int				l_blocksize;
168 	char				*l_device_id;
169 	char				*l_path;
170 	char				*l_scsiname;
171 	char				*l_serial;
172 	int64_t				l_size;
173 
174 	int				l_ctl_lun;
175 };
176 
177 struct target {
178 	TAILQ_ENTRY(target)		t_next;
179 	struct conf			*t_conf;
180 	struct lun			*t_luns[MAX_LUNS];
181 	struct auth_group		*t_auth_group;
182 	TAILQ_HEAD(, port)		t_ports;
183 	char				*t_name;
184 	char				*t_alias;
185 	char				*t_redirection;
186 	/* Name of this target's physical port, if any, i.e. "isp0" */
187 	char				*t_pport;
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(, isns)		conf_isns;
205 	int				conf_isns_period;
206 	int				conf_isns_timeout;
207 	int				conf_debug;
208 	int				conf_timeout;
209 	int				conf_maxproc;
210 
211 #ifdef ICL_KERNEL_PROXY
212 	int				conf_portal_id;
213 #endif
214 	struct pidfh			*conf_pidfh;
215 
216 	bool				conf_default_pg_defined;
217 	bool				conf_default_ag_defined;
218 	bool				conf_kernel_port_on;
219 };
220 
221 /* Physical ports exposed by the kernel */
222 struct kports {
223 	TAILQ_HEAD(, pport)		pports;
224 };
225 
226 #define	CONN_SESSION_TYPE_NONE		0
227 #define	CONN_SESSION_TYPE_DISCOVERY	1
228 #define	CONN_SESSION_TYPE_NORMAL	2
229 
230 struct ctld_connection {
231 	struct connection	conn;
232 	struct portal		*conn_portal;
233 	struct port		*conn_port;
234 	struct target		*conn_target;
235 	int			conn_session_type;
236 	char			*conn_initiator_name;
237 	char			*conn_initiator_addr;
238 	char			*conn_initiator_alias;
239 	uint8_t			conn_initiator_isid[6];
240 	struct sockaddr_storage	conn_initiator_sa;
241 	int			conn_max_recv_data_segment_limit;
242 	int			conn_max_send_data_segment_limit;
243 	int			conn_max_burst_limit;
244 	int			conn_first_burst_limit;
245 	const char		*conn_user;
246 	struct chap		*conn_chap;
247 };
248 
249 int			parse_conf(struct conf *newconf, const char *path);
250 int			uclparse_conf(struct conf *conf, const char *path);
251 
252 struct conf		*conf_new(void);
253 struct conf		*conf_new_from_kernel(struct kports *kports);
254 void			conf_delete(struct conf *conf);
255 int			conf_verify(struct conf *conf);
256 
257 struct auth_group	*auth_group_new(struct conf *conf, const char *name);
258 void			auth_group_delete(struct auth_group *ag);
259 struct auth_group	*auth_group_find(const struct conf *conf,
260 			    const char *name);
261 int			auth_group_set_type(struct auth_group *ag,
262 			    const char *type);
263 
264 const struct auth	*auth_new_chap(struct auth_group *ag,
265 			    const char *user, const char *secret);
266 const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
267 			    const char *user, const char *secret,
268 			    const char *user2, const char *secret2);
269 const struct auth	*auth_find(const struct auth_group *ag,
270 			    const char *user);
271 
272 const struct auth_name	*auth_name_new(struct auth_group *ag,
273 			    const char *initiator_name);
274 bool			auth_name_defined(const struct auth_group *ag);
275 const struct auth_name	*auth_name_find(const struct auth_group *ag,
276 			    const char *initiator_name);
277 int			auth_name_check(const struct auth_group *ag,
278 			    const char *initiator_name);
279 
280 const struct auth_portal	*auth_portal_new(struct auth_group *ag,
281 				    const char *initiator_portal);
282 bool			auth_portal_defined(const struct auth_group *ag);
283 const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
284 				    const struct sockaddr_storage *sa);
285 int				auth_portal_check(const struct auth_group *ag,
286 				    const struct sockaddr_storage *sa);
287 
288 struct portal_group	*portal_group_new(struct conf *conf, const char *name);
289 void			portal_group_delete(struct portal_group *pg);
290 struct portal_group	*portal_group_find(const struct conf *conf,
291 			    const char *name);
292 int			portal_group_add_listen(struct portal_group *pg,
293 			    const char *listen, bool iser);
294 int			portal_group_set_filter(struct portal_group *pg,
295 			    const char *filter);
296 int			portal_group_set_offload(struct portal_group *pg,
297 			    const char *offload);
298 int			portal_group_set_redirection(struct portal_group *pg,
299 			    const char *addr);
300 
301 int			isns_new(struct conf *conf, const char *addr);
302 void			isns_delete(struct isns *is);
303 void			isns_register(struct isns *isns, struct isns *oldisns);
304 void			isns_check(struct isns *isns);
305 void			isns_deregister(struct isns *isns);
306 
307 struct pport		*pport_new(struct kports *kports, const char *name,
308 			    uint32_t ctl_port);
309 struct pport		*pport_find(const struct kports *kports,
310 			    const char *name);
311 struct pport		*pport_copy(struct pport *pp, struct kports *kports);
312 void			pport_delete(struct pport *pport);
313 
314 struct port		*port_new(struct conf *conf, struct target *target,
315 			    struct portal_group *pg);
316 struct port		*port_new_ioctl(struct conf *conf,
317 			    struct kports *kports, struct target *target,
318 			    int pp, int vp);
319 struct port		*port_new_pp(struct conf *conf, struct target *target,
320 			    struct pport *pp);
321 struct port		*port_find(const struct conf *conf, const char *name);
322 struct port		*port_find_in_pg(const struct portal_group *pg,
323 			    const char *target);
324 void			port_delete(struct port *port);
325 int			port_is_dummy(struct port *port);
326 
327 struct target		*target_new(struct conf *conf, const char *name);
328 void			target_delete(struct target *target);
329 struct target		*target_find(struct conf *conf,
330 			    const char *name);
331 int			target_set_redirection(struct target *target,
332 			    const char *addr);
333 
334 struct lun		*lun_new(struct conf *conf, const char *name);
335 void			lun_delete(struct lun *lun);
336 struct lun		*lun_find(const struct conf *conf, const char *name);
337 void			lun_set_backend(struct lun *lun, const char *value);
338 void			lun_set_device_type(struct lun *lun, uint8_t value);
339 void			lun_set_blocksize(struct lun *lun, size_t value);
340 void			lun_set_device_id(struct lun *lun, const char *value);
341 void			lun_set_path(struct lun *lun, const char *value);
342 void			lun_set_scsiname(struct lun *lun, const char *value);
343 void			lun_set_serial(struct lun *lun, const char *value);
344 void			lun_set_size(struct lun *lun, int64_t value);
345 void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
346 
347 bool			option_new(nvlist_t *nvl,
348 			    const char *name, const char *value);
349 
350 void			kernel_init(void);
351 int			kernel_lun_add(struct lun *lun);
352 int			kernel_lun_modify(struct lun *lun);
353 int			kernel_lun_remove(struct lun *lun);
354 void			kernel_handoff(struct ctld_connection *conn);
355 void			kernel_limits(const char *offload, int s,
356 			    int *max_recv_data_segment_length,
357 			    int *max_send_data_segment_length,
358 			    int *max_burst_length,
359 			    int *first_burst_length);
360 int			kernel_port_add(struct port *port);
361 int			kernel_port_update(struct port *port, struct port *old);
362 int			kernel_port_remove(struct port *port);
363 void			kernel_capsicate(void);
364 
365 #ifdef ICL_KERNEL_PROXY
366 void			kernel_listen(struct addrinfo *ai, bool iser,
367 			    int portal_id);
368 void			kernel_accept(int *connection_id, int *portal_id,
369 			    struct sockaddr *client_sa,
370 			    socklen_t *client_salen);
371 void			kernel_send(struct pdu *pdu);
372 void			kernel_receive(struct pdu *pdu);
373 #endif
374 
375 void			login(struct ctld_connection *conn);
376 
377 void			discovery(struct ctld_connection *conn);
378 
379 void			set_timeout(int timeout, int fatal);
380 
381 #endif /* !CTLD_H */
382