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>
36 #ifdef ICL_KERNEL_PROXY
37 #include <sys/types.h>
39 #include <sys/socket.h>
42 #include <openssl/md5.h>
44 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf"
45 #define DEFAULT_PIDFILE "/var/run/ctld.pid"
46 #define DEFAULT_BLOCKSIZE 512
48 #define MAX_NAME_LEN 223
49 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024)
50 #define MAX_BURST_LENGTH 16776192
53 TAILQ_ENTRY(auth) a_next;
54 struct auth_group *a_auth_group;
58 char *a_mutual_secret;
62 TAILQ_ENTRY(auth_name) an_next;
63 struct auth_group *an_auth_group;
64 char *an_initator_name;
68 TAILQ_ENTRY(auth_portal) ap_next;
69 struct auth_group *ap_auth_group;
70 char *ap_initator_portal;
71 struct sockaddr_storage ap_sa;
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
82 TAILQ_ENTRY(auth_group) ag_next;
85 struct target *ag_target;
87 TAILQ_HEAD(, auth) ag_auths;
88 TAILQ_HEAD(, auth_name) ag_names;
89 TAILQ_HEAD(, auth_portal) ag_portals;
93 TAILQ_ENTRY(portal) p_next;
94 struct portal_group *p_portal_group;
97 struct addrinfo *p_ai;
98 #ifdef ICL_KERNEL_PROXY
102 TAILQ_HEAD(, target) p_targets;
106 struct portal_group {
107 TAILQ_ENTRY(portal_group) pg_next;
108 struct conf *pg_conf;
110 struct auth_group *pg_discovery_auth_group;
112 TAILQ_HEAD(, portal) pg_portals;
118 TAILQ_ENTRY(lun_option) lo_next;
125 TAILQ_ENTRY(lun) l_next;
126 TAILQ_HEAD(, lun_option) l_options;
127 struct target *l_target;
140 TAILQ_ENTRY(target) t_next;
141 TAILQ_HEAD(, lun) t_luns;
143 struct auth_group *t_auth_group;
144 struct portal_group *t_portal_group;
150 char *conf_pidfile_path;
151 TAILQ_HEAD(, target) conf_targets;
152 TAILQ_HEAD(, auth_group) conf_auth_groups;
153 TAILQ_HEAD(, portal_group) conf_portal_groups;
158 uint16_t conf_last_portal_group_tag;
159 #ifdef ICL_KERNEL_PROXY
162 struct pidfh *conf_pidfh;
164 bool conf_default_pg_defined;
165 bool conf_default_ag_defined;
166 bool conf_kernel_port_on;
169 #define CONN_SESSION_TYPE_NONE 0
170 #define CONN_SESSION_TYPE_DISCOVERY 1
171 #define CONN_SESSION_TYPE_NORMAL 2
173 #define CONN_DIGEST_NONE 0
174 #define CONN_DIGEST_CRC32C 1
177 struct portal *conn_portal;
178 struct target *conn_target;
180 int conn_session_type;
181 char *conn_initiator_name;
182 char *conn_initiator_addr;
183 char *conn_initiator_alias;
184 uint8_t conn_initiator_isid[6];
185 struct sockaddr_storage conn_initiator_sa;
187 uint32_t conn_statsn;
188 size_t conn_max_data_segment_length;
189 size_t conn_max_burst_length;
190 int conn_immediate_data;
191 int conn_header_digest;
192 int conn_data_digest;
196 struct connection *pdu_connection;
197 struct iscsi_bhs *pdu_bhs;
202 #define KEYS_MAX 1024
205 char *keys_names[KEYS_MAX];
206 char *keys_values[KEYS_MAX];
208 size_t keys_data_len;
211 #define CHAP_CHALLENGE_LEN 1024
214 unsigned char chap_id;
215 char chap_challenge[CHAP_CHALLENGE_LEN];
216 char chap_response[MD5_DIGEST_LENGTH];
221 unsigned char rchap_id;
222 void *rchap_challenge;
223 size_t rchap_challenge_len;
226 struct chap *chap_new(void);
227 char *chap_get_id(const struct chap *chap);
228 char *chap_get_challenge(const struct chap *chap);
229 int chap_receive(struct chap *chap, const char *response);
230 int chap_authenticate(struct chap *chap,
232 void chap_delete(struct chap *chap);
234 struct rchap *rchap_new(const char *secret);
235 int rchap_receive(struct rchap *rchap,
236 const char *id, const char *challenge);
237 char *rchap_get_response(struct rchap *rchap);
238 void rchap_delete(struct rchap *rchap);
240 struct conf *conf_new(void);
241 struct conf *conf_new_from_file(const char *path);
242 struct conf *conf_new_from_kernel(void);
243 void conf_delete(struct conf *conf);
244 int conf_verify(struct conf *conf);
246 struct auth_group *auth_group_new(struct conf *conf, const char *name);
247 void auth_group_delete(struct auth_group *ag);
248 struct auth_group *auth_group_find(const struct conf *conf,
250 int auth_group_set_type_str(struct auth_group *ag,
253 const struct auth *auth_new_chap(struct auth_group *ag,
254 const char *user, const char *secret);
255 const struct auth *auth_new_chap_mutual(struct auth_group *ag,
256 const char *user, const char *secret,
257 const char *user2, const char *secret2);
258 const struct auth *auth_find(const struct auth_group *ag,
261 const struct auth_name *auth_name_new(struct auth_group *ag,
262 const char *initiator_name);
263 bool auth_name_defined(const struct auth_group *ag);
264 const struct auth_name *auth_name_find(const struct auth_group *ag,
265 const char *initiator_name);
267 const struct auth_portal *auth_portal_new(struct auth_group *ag,
268 const char *initiator_portal);
269 bool auth_portal_defined(const struct auth_group *ag);
270 const struct auth_portal *auth_portal_find(const struct auth_group *ag,
271 const struct sockaddr_storage *sa);
273 struct portal_group *portal_group_new(struct conf *conf, const char *name);
274 void portal_group_delete(struct portal_group *pg);
275 struct portal_group *portal_group_find(const struct conf *conf,
277 int portal_group_add_listen(struct portal_group *pg,
278 const char *listen, bool iser);
280 struct target *target_new(struct conf *conf, const char *name);
281 void target_delete(struct target *target);
282 struct target *target_find(struct conf *conf,
285 struct lun *lun_new(struct target *target, int lun_id);
286 void lun_delete(struct lun *lun);
287 struct lun *lun_find(const struct target *target, int lun_id);
288 void lun_set_backend(struct lun *lun, const char *value);
289 void lun_set_blocksize(struct lun *lun, size_t value);
290 void lun_set_device_id(struct lun *lun, const char *value);
291 void lun_set_path(struct lun *lun, const char *value);
292 void lun_set_serial(struct lun *lun, const char *value);
293 void lun_set_size(struct lun *lun, size_t value);
294 void lun_set_ctl_lun(struct lun *lun, uint32_t value);
296 struct lun_option *lun_option_new(struct lun *lun,
297 const char *name, const char *value);
298 void lun_option_delete(struct lun_option *clo);
299 struct lun_option *lun_option_find(const struct lun *lun,
301 void lun_option_set(struct lun_option *clo,
304 void kernel_init(void);
305 int kernel_lun_add(struct lun *lun);
306 int kernel_lun_resize(struct lun *lun);
307 int kernel_lun_remove(struct lun *lun);
308 void kernel_handoff(struct connection *conn);
309 int kernel_port_add(struct target *targ);
310 int kernel_port_remove(struct target *targ);
311 void kernel_capsicate(void);
313 #ifdef ICL_KERNEL_PROXY
314 void kernel_listen(struct addrinfo *ai, bool iser,
316 void kernel_accept(int *connection_id, int *portal_id,
317 struct sockaddr *client_sa,
318 socklen_t *client_salen);
319 void kernel_send(struct pdu *pdu);
320 void kernel_receive(struct pdu *pdu);
323 struct keys *keys_new(void);
324 void keys_delete(struct keys *keys);
325 void keys_load(struct keys *keys, const struct pdu *pdu);
326 void keys_save(struct keys *keys, struct pdu *pdu);
327 const char *keys_find(struct keys *keys, const char *name);
328 int keys_find_int(struct keys *keys, const char *name);
329 void keys_add(struct keys *keys,
330 const char *name, const char *value);
331 void keys_add_int(struct keys *keys,
332 const char *name, int value);
334 struct pdu *pdu_new(struct connection *conn);
335 struct pdu *pdu_new_response(struct pdu *request);
336 void pdu_delete(struct pdu *pdu);
337 void pdu_receive(struct pdu *request);
338 void pdu_send(struct pdu *response);
340 void login(struct connection *conn);
342 void discovery(struct connection *conn);
344 void log_init(int level);
345 void log_set_peer_name(const char *name);
346 void log_set_peer_addr(const char *addr);
347 void log_err(int, const char *, ...)
348 __dead2 __printflike(2, 3);
349 void log_errx(int, const char *, ...)
350 __dead2 __printflike(2, 3);
351 void log_warn(const char *, ...) __printflike(1, 2);
352 void log_warnx(const char *, ...) __printflike(1, 2);
353 void log_debugx(const char *, ...) __printflike(1, 2);
355 char *checked_strdup(const char *);
356 bool valid_iscsi_name(const char *name);
357 bool timed_out(void);