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_NAME_LEN			223
50#define	MAX_DATA_SEGMENT_LENGTH		(128 * 1024)
51#define	SOCKBUF_SIZE			1048576
52
53struct 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
62struct auth_name {
63	TAILQ_ENTRY(auth_name)		an_next;
64	struct auth_group		*an_auth_group;
65	char				*an_initiator_name;
66};
67
68struct 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
82struct 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
93struct 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
107TAILQ_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
115struct 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	int				pg_dscp;
129	int				pg_pcp;
130
131	uint16_t			pg_tag;
132};
133
134struct pport {
135	TAILQ_ENTRY(pport)		pp_next;
136	TAILQ_HEAD(, port)		pp_ports;
137	struct conf			*pp_conf;
138	char				*pp_name;
139
140	uint32_t			pp_ctl_port;
141};
142
143struct 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
161struct option {
162	TAILQ_ENTRY(option)		o_next;
163	char				*o_name;
164	char				*o_value;
165};
166
167struct 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
184struct 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};
194
195struct isns {
196	TAILQ_ENTRY(isns)		i_next;
197	struct conf			*i_conf;
198	char				*i_addr;
199	struct addrinfo			*i_ai;
200};
201
202struct conf {
203	char				*conf_pidfile_path;
204	TAILQ_HEAD(, lun)		conf_luns;
205	TAILQ_HEAD(, target)		conf_targets;
206	TAILQ_HEAD(, auth_group)	conf_auth_groups;
207	TAILQ_HEAD(, port)		conf_ports;
208	TAILQ_HEAD(, portal_group)	conf_portal_groups;
209	TAILQ_HEAD(, pport)		conf_pports;
210	TAILQ_HEAD(, isns)		conf_isns;
211	int				conf_isns_period;
212	int				conf_isns_timeout;
213	int				conf_debug;
214	int				conf_timeout;
215	int				conf_maxproc;
216
217#ifdef ICL_KERNEL_PROXY
218	int				conf_portal_id;
219#endif
220	struct pidfh			*conf_pidfh;
221
222	bool				conf_default_pg_defined;
223	bool				conf_default_ag_defined;
224	bool				conf_kernel_port_on;
225};
226
227#define	CONN_SESSION_TYPE_NONE		0
228#define	CONN_SESSION_TYPE_DISCOVERY	1
229#define	CONN_SESSION_TYPE_NORMAL	2
230
231struct ctld_connection {
232	struct connection	conn;
233	struct portal		*conn_portal;
234	struct port		*conn_port;
235	struct target		*conn_target;
236	int			conn_session_type;
237	char			*conn_initiator_name;
238	char			*conn_initiator_addr;
239	char			*conn_initiator_alias;
240	uint8_t			conn_initiator_isid[6];
241	struct sockaddr_storage	conn_initiator_sa;
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	const char		*conn_user;
247	struct chap		*conn_chap;
248};
249
250int			parse_conf(struct conf *conf, const char *path);
251int			uclparse_conf(struct conf *conf, const char *path);
252
253struct conf		*conf_new(void);
254struct conf		*conf_new_from_kernel(void);
255void			conf_delete(struct conf *conf);
256int			conf_verify(struct conf *conf);
257
258struct auth_group	*auth_group_new(struct conf *conf, const char *name);
259void			auth_group_delete(struct auth_group *ag);
260struct auth_group	*auth_group_find(const struct conf *conf,
261			    const char *name);
262int			auth_group_set_type(struct auth_group *ag,
263			    const char *type);
264
265const struct auth	*auth_new_chap(struct auth_group *ag,
266			    const char *user, const char *secret);
267const struct auth	*auth_new_chap_mutual(struct auth_group *ag,
268			    const char *user, const char *secret,
269			    const char *user2, const char *secret2);
270const struct auth	*auth_find(const struct auth_group *ag,
271			    const char *user);
272
273const struct auth_name	*auth_name_new(struct auth_group *ag,
274			    const char *initiator_name);
275bool			auth_name_defined(const struct auth_group *ag);
276const struct auth_name	*auth_name_find(const struct auth_group *ag,
277			    const char *initiator_name);
278int			auth_name_check(const struct auth_group *ag,
279			    const char *initiator_name);
280
281const struct auth_portal	*auth_portal_new(struct auth_group *ag,
282				    const char *initiator_portal);
283bool			auth_portal_defined(const struct auth_group *ag);
284const struct auth_portal	*auth_portal_find(const struct auth_group *ag,
285				    const struct sockaddr_storage *sa);
286int				auth_portal_check(const struct auth_group *ag,
287				    const struct sockaddr_storage *sa);
288
289struct portal_group	*portal_group_new(struct conf *conf, const char *name);
290void			portal_group_delete(struct portal_group *pg);
291struct portal_group	*portal_group_find(const struct conf *conf,
292			    const char *name);
293int			portal_group_add_listen(struct portal_group *pg,
294			    const char *listen, bool iser);
295int			portal_group_set_filter(struct portal_group *pg,
296			    const char *filter);
297int			portal_group_set_offload(struct portal_group *pg,
298			    const char *offload);
299int			portal_group_set_redirection(struct portal_group *pg,
300			    const char *addr);
301
302int			isns_new(struct conf *conf, const char *addr);
303void			isns_delete(struct isns *is);
304void			isns_register(struct isns *isns, struct isns *oldisns);
305void			isns_check(struct isns *isns);
306void			isns_deregister(struct isns *isns);
307
308struct pport		*pport_new(struct conf *conf, const char *name,
309			    uint32_t ctl_port);
310struct pport		*pport_find(const struct conf *conf, const char *name);
311struct pport		*pport_copy(struct pport *pport, struct conf *conf);
312void			pport_delete(struct pport *pport);
313
314struct port		*port_new(struct conf *conf, struct target *target,
315			    struct portal_group *pg);
316struct port		*port_new_ioctl(struct conf *conf, struct target *target,
317			    int pp, int vp);
318struct port		*port_new_pp(struct conf *conf, struct target *target,
319			    struct pport *pp);
320struct port		*port_find(const struct conf *conf, const char *name);
321struct port		*port_find_in_pg(const struct portal_group *pg,
322			    const char *target);
323void			port_delete(struct port *port);
324int			port_is_dummy(struct port *port);
325
326struct target		*target_new(struct conf *conf, const char *name);
327void			target_delete(struct target *target);
328struct target		*target_find(struct conf *conf,
329			    const char *name);
330int			target_set_redirection(struct target *target,
331			    const char *addr);
332
333struct lun		*lun_new(struct conf *conf, const char *name);
334void			lun_delete(struct lun *lun);
335struct lun		*lun_find(const struct conf *conf, const char *name);
336void			lun_set_backend(struct lun *lun, const char *value);
337void			lun_set_device_type(struct lun *lun, uint8_t value);
338void			lun_set_blocksize(struct lun *lun, size_t value);
339void			lun_set_device_id(struct lun *lun, const char *value);
340void			lun_set_path(struct lun *lun, const char *value);
341void			lun_set_scsiname(struct lun *lun, const char *value);
342void			lun_set_serial(struct lun *lun, const char *value);
343void			lun_set_size(struct lun *lun, size_t value);
344void			lun_set_ctl_lun(struct lun *lun, uint32_t value);
345
346struct option		*option_new(struct options *os,
347			    const char *name, const char *value);
348void			option_delete(struct options *os, struct option *co);
349struct option		*option_find(const struct options *os, const char *name);
350void			option_set(struct option *o, const char *value);
351
352void			kernel_init(void);
353int			kernel_lun_add(struct lun *lun);
354int			kernel_lun_modify(struct lun *lun);
355int			kernel_lun_remove(struct lun *lun);
356void			kernel_handoff(struct ctld_connection *conn);
357void			kernel_limits(const char *offload, int s,
358			    int *max_recv_data_segment_length,
359			    int *max_send_data_segment_length,
360			    int *max_burst_length,
361			    int *first_burst_length);
362int			kernel_port_add(struct port *port);
363int			kernel_port_update(struct port *port, struct port *old);
364int			kernel_port_remove(struct port *port);
365void			kernel_capsicate(void);
366
367#ifdef ICL_KERNEL_PROXY
368void			kernel_listen(struct addrinfo *ai, bool iser,
369			    int portal_id);
370void			kernel_accept(int *connection_id, int *portal_id,
371			    struct sockaddr *client_sa,
372			    socklen_t *client_salen);
373void			kernel_send(struct pdu *pdu);
374void			kernel_receive(struct pdu *pdu);
375#endif
376
377void			login(struct ctld_connection *conn);
378
379void			discovery(struct ctld_connection *conn);
380
381bool			valid_iscsi_name(const char *name);
382void			set_timeout(int timeout, int fatal);
383
384#endif /* !CTLD_H */
385