2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/queue.h>
39 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf"
40 #define DEFAULT_PIDFILE "/var/run/ctld.pid"
41 #define DEFAULT_BLOCKSIZE 512
43 #define MAX_NAME_LEN 223
44 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024)
45 #define MAX_BURST_LENGTH 16776192
48 TAILQ_ENTRY(auth) a_next;
49 struct auth_group *a_auth_group;
53 char *a_mutual_secret;
57 TAILQ_ENTRY(auth_name) an_next;
58 struct auth_group *an_auth_group;
59 char *an_initator_name;
63 TAILQ_ENTRY(auth_portal) ap_next;
64 struct auth_group *ap_auth_group;
65 char *ap_initator_portal;
68 #define AG_TYPE_UNKNOWN 0
69 #define AG_TYPE_DENY 1
70 #define AG_TYPE_NO_AUTHENTICATION 2
71 #define AG_TYPE_CHAP 3
72 #define AG_TYPE_CHAP_MUTUAL 4
75 TAILQ_ENTRY(auth_group) ag_next;
78 struct target *ag_target;
80 TAILQ_HEAD(, auth) ag_auths;
81 TAILQ_HEAD(, auth_name) ag_names;
82 TAILQ_HEAD(, auth_portal) ag_portals;
86 TAILQ_ENTRY(portal) p_next;
87 struct portal_group *p_portal_group;
90 struct addrinfo *p_ai;
91 #ifdef ICL_KERNEL_PROXY
95 TAILQ_HEAD(, target) p_targets;
100 TAILQ_ENTRY(portal_group) pg_next;
101 struct conf *pg_conf;
103 struct auth_group *pg_discovery_auth_group;
105 TAILQ_HEAD(, portal) pg_portals;
111 TAILQ_ENTRY(lun_option) lo_next;
118 TAILQ_ENTRY(lun) l_next;
119 TAILQ_HEAD(, lun_option) l_options;
120 struct target *l_target;
133 TAILQ_ENTRY(target) t_next;
134 TAILQ_HEAD(, lun) t_luns;
136 struct auth_group *t_auth_group;
137 struct portal_group *t_portal_group;
143 char *conf_pidfile_path;
144 TAILQ_HEAD(, target) conf_targets;
145 TAILQ_HEAD(, auth_group) conf_auth_groups;
146 TAILQ_HEAD(, portal_group) conf_portal_groups;
151 uint16_t conf_last_portal_group_tag;
152 #ifdef ICL_KERNEL_PROXY
155 struct pidfh *conf_pidfh;
157 bool conf_default_pg_defined;
158 bool conf_default_ag_defined;
161 #define CONN_SESSION_TYPE_NONE 0
162 #define CONN_SESSION_TYPE_DISCOVERY 1
163 #define CONN_SESSION_TYPE_NORMAL 2
165 #define CONN_DIGEST_NONE 0
166 #define CONN_DIGEST_CRC32C 1
169 struct portal *conn_portal;
170 struct target *conn_target;
172 int conn_session_type;
173 char *conn_initiator_name;
174 char *conn_initiator_addr;
175 char *conn_initiator_alias;
177 uint32_t conn_statsn;
178 size_t conn_max_data_segment_length;
179 size_t conn_max_burst_length;
180 int conn_immediate_data;
181 int conn_header_digest;
182 int conn_data_digest;
186 struct connection *pdu_connection;
187 struct iscsi_bhs *pdu_bhs;
192 #define KEYS_MAX 1024
195 char *keys_names[KEYS_MAX];
196 char *keys_values[KEYS_MAX];
198 size_t keys_data_len;
201 struct conf *conf_new(void);
202 struct conf *conf_new_from_file(const char *path);
203 struct conf *conf_new_from_kernel(void);
204 void conf_delete(struct conf *conf);
205 int conf_verify(struct conf *conf);
207 struct auth_group *auth_group_new(struct conf *conf, const char *name);
208 void auth_group_delete(struct auth_group *ag);
209 struct auth_group *auth_group_find(struct conf *conf, const char *name);
210 int auth_group_set_type_str(struct auth_group *ag,
213 const struct auth *auth_new_chap(struct auth_group *ag,
214 const char *user, const char *secret);
215 const struct auth *auth_new_chap_mutual(struct auth_group *ag,
216 const char *user, const char *secret,
217 const char *user2, const char *secret2);
218 const struct auth *auth_find(struct auth_group *ag,
221 const struct auth_name *auth_name_new(struct auth_group *ag,
222 const char *initiator_name);
223 bool auth_name_defined(const struct auth_group *ag);
224 const struct auth_name *auth_name_find(const struct auth_group *ag,
225 const char *initiator_name);
227 const struct auth_portal *auth_portal_new(struct auth_group *ag,
228 const char *initiator_portal);
229 bool auth_portal_defined(const struct auth_group *ag);
230 const struct auth_portal *auth_portal_find(const struct auth_group *ag,
231 const char *initiator_portal);
233 struct portal_group *portal_group_new(struct conf *conf, const char *name);
234 void portal_group_delete(struct portal_group *pg);
235 struct portal_group *portal_group_find(struct conf *conf, const char *name);
236 int portal_group_add_listen(struct portal_group *pg,
237 const char *listen, bool iser);
239 struct target *target_new(struct conf *conf, const char *name);
240 void target_delete(struct target *target);
241 struct target *target_find(struct conf *conf,
244 struct lun *lun_new(struct target *target, int lun_id);
245 void lun_delete(struct lun *lun);
246 struct lun *lun_find(struct target *target, int lun_id);
247 void lun_set_backend(struct lun *lun, const char *value);
248 void lun_set_blocksize(struct lun *lun, size_t value);
249 void lun_set_device_id(struct lun *lun, const char *value);
250 void lun_set_path(struct lun *lun, const char *value);
251 void lun_set_serial(struct lun *lun, const char *value);
252 void lun_set_size(struct lun *lun, size_t value);
253 void lun_set_ctl_lun(struct lun *lun, uint32_t value);
255 struct lun_option *lun_option_new(struct lun *lun,
256 const char *name, const char *value);
257 void lun_option_delete(struct lun_option *clo);
258 struct lun_option *lun_option_find(struct lun *lun, const char *name);
259 void lun_option_set(struct lun_option *clo,
262 void kernel_init(void);
263 int kernel_lun_add(struct lun *lun);
264 int kernel_lun_resize(struct lun *lun);
265 int kernel_lun_remove(struct lun *lun);
266 void kernel_handoff(struct connection *conn);
267 int kernel_port_on(void);
268 int kernel_port_off(void);
269 void kernel_capsicate(void);
274 void kernel_listen(struct addrinfo *ai, bool iser,
276 void kernel_accept(int *connection_id, int *portal_id);
277 void kernel_send(struct pdu *pdu);
278 void kernel_receive(struct pdu *pdu);
280 struct keys *keys_new(void);
281 void keys_delete(struct keys *keys);
282 void keys_load(struct keys *keys, const struct pdu *pdu);
283 void keys_save(struct keys *keys, struct pdu *pdu);
284 const char *keys_find(struct keys *keys, const char *name);
285 int keys_find_int(struct keys *keys, const char *name);
286 void keys_add(struct keys *keys,
287 const char *name, const char *value);
288 void keys_add_int(struct keys *keys,
289 const char *name, int value);
291 struct pdu *pdu_new(struct connection *conn);
292 struct pdu *pdu_new_response(struct pdu *request);
293 void pdu_delete(struct pdu *pdu);
294 void pdu_receive(struct pdu *request);
295 void pdu_send(struct pdu *response);
297 void login(struct connection *conn);
299 void discovery(struct connection *conn);
301 void log_init(int level);
302 void log_set_peer_name(const char *name);
303 void log_set_peer_addr(const char *addr);
304 void log_err(int, const char *, ...)
305 __dead2 __printflike(2, 3);
306 void log_errx(int, const char *, ...)
307 __dead2 __printflike(2, 3);
308 void log_warn(const char *, ...) __printflike(1, 2);
309 void log_warnx(const char *, ...) __printflike(1, 2);
310 void log_debugx(const char *, ...) __printflike(1, 2);
312 char *checked_strdup(const char *);
313 bool valid_iscsi_name(const char *name);
314 bool timed_out(void);