xref: /freebsd/usr.sbin/ctld/ctld.h (revision 9e425a8a7ef075706c901b27091a79e2911b7595)
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	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 #define	PG_FILTER_UNKNOWN		0
107 #define	PG_FILTER_NONE			1
108 #define	PG_FILTER_PORTAL		2
109 #define	PG_FILTER_PORTAL_NAME		3
110 #define	PG_FILTER_PORTAL_NAME_AUTH	4
111 
112 struct portal_group {
113 	TAILQ_ENTRY(portal_group)	pg_next;
114 	struct conf			*pg_conf;
115 	nvlist_t			*pg_options;
116 	char				*pg_name;
117 	struct auth_group		*pg_discovery_auth_group;
118 	int				pg_discovery_filter;
119 	int				pg_foreign;
120 	bool				pg_unassigned;
121 	TAILQ_HEAD(, portal)		pg_portals;
122 	TAILQ_HEAD(, port)		pg_ports;
123 	char				*pg_offload;
124 	char				*pg_redirection;
125 	int				pg_dscp;
126 	int				pg_pcp;
127 
128 	uint16_t			pg_tag;
129 };
130 
131 /* Ports created by the kernel.  Perhaps the "p" means "physical" ? */
132 struct pport {
133 	TAILQ_ENTRY(pport)		pp_next;
134 	TAILQ_HEAD(, port)		pp_ports;
135 	struct kports			*pp_kports;
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 	int				p_ioctl_port;
154 	int				p_ioctl_pp;
155 	int				p_ioctl_vp;
156 	uint32_t			p_ctl_port;
157 };
158 
159 struct lun {
160 	TAILQ_ENTRY(lun)		l_next;
161 	struct conf			*l_conf;
162 	nvlist_t			*l_options;
163 	char				*l_name;
164 	char				*l_backend;
165 	uint8_t				l_device_type;
166 	int				l_blocksize;
167 	char				*l_device_id;
168 	char				*l_path;
169 	char				*l_scsiname;
170 	char				*l_serial;
171 	int64_t				l_size;
172 
173 	int				l_ctl_lun;
174 };
175 
176 struct target {
177 	TAILQ_ENTRY(target)		t_next;
178 	struct conf			*t_conf;
179 	struct lun			*t_luns[MAX_LUNS];
180 	struct auth_group		*t_auth_group;
181 	TAILQ_HEAD(, port)		t_ports;
182 	char				*t_name;
183 	char				*t_alias;
184 	char				*t_redirection;
185 	/* Name of this target's physical port, if any, i.e. "isp0" */
186 	char				*t_pport;
187 };
188 
189 struct isns {
190 	TAILQ_ENTRY(isns)		i_next;
191 	struct conf			*i_conf;
192 	char				*i_addr;
193 	struct addrinfo			*i_ai;
194 };
195 
196 struct conf {
197 	char				*conf_pidfile_path;
198 	TAILQ_HEAD(, lun)		conf_luns;
199 	TAILQ_HEAD(, target)		conf_targets;
200 	TAILQ_HEAD(, auth_group)	conf_auth_groups;
201 	TAILQ_HEAD(, port)		conf_ports;
202 	TAILQ_HEAD(, portal_group)	conf_portal_groups;
203 	TAILQ_HEAD(, isns)		conf_isns;
204 	int				conf_isns_period;
205 	int				conf_isns_timeout;
206 	int				conf_debug;
207 	int				conf_timeout;
208 	int				conf_maxproc;
209 
210 #ifdef ICL_KERNEL_PROXY
211 	int				conf_portal_id;
212 #endif
213 	struct pidfh			*conf_pidfh;
214 
215 	bool				conf_default_pg_defined;
216 	bool				conf_default_ag_defined;
217 	bool				conf_kernel_port_on;
218 };
219 
220 /* Physical ports exposed by the kernel */
221 struct kports {
222 	TAILQ_HEAD(, pport)		pports;
223 };
224 
225 #define	CONN_SESSION_TYPE_NONE		0
226 #define	CONN_SESSION_TYPE_DISCOVERY	1
227 #define	CONN_SESSION_TYPE_NORMAL	2
228 
229 struct ctld_connection {
230 	struct connection	conn;
231 	struct portal		*conn_portal;
232 	struct port		*conn_port;
233 	struct target		*conn_target;
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 	int			conn_max_recv_data_segment_limit;
241 	int			conn_max_send_data_segment_limit;
242 	int			conn_max_burst_limit;
243 	int			conn_first_burst_limit;
244 	const char		*conn_user;
245 	struct chap		*conn_chap;
246 };
247 
248 extern int ctl_fd;
249 
250 int			parse_conf(struct conf *newconf, const char *path);
251 int			uclparse_conf(struct conf *conf, const char *path);
252 
253 struct conf		*conf_new(void);
254 struct conf		*conf_new_from_kernel(struct kports *kports);
255 void			conf_delete(struct conf *conf);
256 int			conf_verify(struct conf *conf);
257 
258 struct auth_group	*auth_group_new(struct conf *conf, const char *name);
259 void			auth_group_delete(struct auth_group *ag);
260 struct auth_group	*auth_group_find(const struct conf *conf,
261 			    const char *name);
262 int			auth_group_set_type(struct auth_group *ag,
263 			    const char *type);
264 
265 const struct auth	*auth_new_chap(struct auth_group *ag,
266 			    const char *user, const char *secret);
267 const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
268 			    const char *user, const char *secret,
269 			    const char *user2, const char *secret2);
270 const struct auth	*auth_find(const struct auth_group *ag,
271 			    const char *user);
272 
273 const struct auth_name	*auth_name_new(struct auth_group *ag,
274 			    const char *initiator_name);
275 bool			auth_name_defined(const struct auth_group *ag);
276 const struct auth_name	*auth_name_find(const struct auth_group *ag,
277 			    const char *initiator_name);
278 int			auth_name_check(const struct auth_group *ag,
279 			    const char *initiator_name);
280 
281 const struct auth_portal	*auth_portal_new(struct auth_group *ag,
282 				    const char *initiator_portal);
283 bool			auth_portal_defined(const struct auth_group *ag);
284 const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
285 				    const struct sockaddr_storage *sa);
286 int				auth_portal_check(const struct auth_group *ag,
287 				    const struct sockaddr_storage *sa);
288 
289 struct portal_group	*portal_group_new(struct conf *conf, const char *name);
290 void			portal_group_delete(struct portal_group *pg);
291 struct portal_group	*portal_group_find(const struct conf *conf,
292 			    const char *name);
293 int			portal_group_add_listen(struct portal_group *pg,
294 			    const char *listen, bool iser);
295 int			portal_group_set_filter(struct portal_group *pg,
296 			    const char *filter);
297 int			portal_group_set_offload(struct portal_group *pg,
298 			    const char *offload);
299 int			portal_group_set_redirection(struct portal_group *pg,
300 			    const char *addr);
301 
302 int			isns_new(struct conf *conf, const char *addr);
303 void			isns_delete(struct isns *is);
304 void			isns_register(struct isns *isns, struct isns *oldisns);
305 void			isns_check(struct isns *isns);
306 void			isns_deregister(struct isns *isns);
307 
308 struct pport		*pport_new(struct kports *kports, const char *name,
309 			    uint32_t ctl_port);
310 struct pport		*pport_find(const struct kports *kports,
311 			    const char *name);
312 struct pport		*pport_copy(struct pport *pp, struct kports *kports);
313 void			pport_delete(struct pport *pport);
314 
315 struct port		*port_new(struct conf *conf, struct target *target,
316 			    struct portal_group *pg);
317 struct port		*port_new_ioctl(struct conf *conf,
318 			    struct kports *kports, 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, int64_t value);
346 void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
347 
348 bool			option_new(nvlist_t *nvl,
349 			    const char *name, const char *value);
350 
351 void			kernel_init(void);
352 int			kernel_lun_add(struct lun *lun);
353 int			kernel_lun_modify(struct lun *lun);
354 int			kernel_lun_remove(struct lun *lun);
355 void			kernel_handoff(struct ctld_connection *conn);
356 int			kernel_port_add(struct port *port);
357 int			kernel_port_update(struct port *port, struct port *old);
358 int			kernel_port_remove(struct port *port);
359 void			kernel_capsicate(void);
360 
361 #ifdef ICL_KERNEL_PROXY
362 void			kernel_listen(struct addrinfo *ai, bool iser,
363 			    int portal_id);
364 void			kernel_accept(int *connection_id, int *portal_id,
365 			    struct sockaddr *client_sa,
366 			    socklen_t *client_salen);
367 void			kernel_send(struct pdu *pdu);
368 void			kernel_receive(struct pdu *pdu);
369 #endif
370 
371 void			login(struct ctld_connection *conn);
372 
373 void			discovery(struct ctld_connection *conn);
374 
375 void			set_timeout(int timeout, int fatal);
376 
377 #endif /* !CTLD_H */
378