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
49 #define MAX_NAME_LEN 223
50 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024)
51 #define MAX_BURST_LENGTH 16776192
52 #define SOCKBUF_SIZE 1048576
55 TAILQ_ENTRY(auth) a_next;
56 struct auth_group *a_auth_group;
60 char *a_mutual_secret;
64 TAILQ_ENTRY(auth_name) an_next;
65 struct auth_group *an_auth_group;
66 char *an_initator_name;
70 TAILQ_ENTRY(auth_portal) ap_next;
71 struct auth_group *ap_auth_group;
72 char *ap_initator_portal;
73 struct sockaddr_storage ap_sa;
77 #define AG_TYPE_UNKNOWN 0
78 #define AG_TYPE_DENY 1
79 #define AG_TYPE_NO_AUTHENTICATION 2
80 #define AG_TYPE_CHAP 3
81 #define AG_TYPE_CHAP_MUTUAL 4
84 TAILQ_ENTRY(auth_group) ag_next;
87 struct target *ag_target;
89 TAILQ_HEAD(, auth) ag_auths;
90 TAILQ_HEAD(, auth_name) ag_names;
91 TAILQ_HEAD(, auth_portal) ag_portals;
95 TAILQ_ENTRY(portal) p_next;
96 struct portal_group *p_portal_group;
99 struct addrinfo *p_ai;
100 #ifdef ICL_KERNEL_PROXY
104 TAILQ_HEAD(, target) p_targets;
108 #define PG_FILTER_UNKNOWN 0
109 #define PG_FILTER_NONE 1
110 #define PG_FILTER_PORTAL 2
111 #define PG_FILTER_PORTAL_NAME 3
112 #define PG_FILTER_PORTAL_NAME_AUTH 4
114 struct portal_group {
115 TAILQ_ENTRY(portal_group) pg_next;
116 struct conf *pg_conf;
118 struct auth_group *pg_discovery_auth_group;
119 int pg_discovery_filter;
121 TAILQ_HEAD(, portal) pg_portals;
122 TAILQ_HEAD(, port) pg_ports;
123 char *pg_redirection;
129 TAILQ_ENTRY(port) p_next;
130 TAILQ_ENTRY(port) p_pgs;
131 TAILQ_ENTRY(port) p_ts;
134 struct auth_group *p_auth_group;
135 struct portal_group *p_portal_group;
136 struct target *p_target;
142 TAILQ_ENTRY(lun_option) lo_next;
149 TAILQ_ENTRY(lun) l_next;
151 TAILQ_HEAD(, lun_option) l_options;
165 TAILQ_ENTRY(target) t_next;
167 struct lun *t_luns[MAX_LUNS];
168 struct auth_group *t_auth_group;
169 TAILQ_HEAD(, port) t_ports;
176 TAILQ_ENTRY(isns) i_next;
179 struct addrinfo *i_ai;
183 char *conf_pidfile_path;
184 TAILQ_HEAD(, lun) conf_luns;
185 TAILQ_HEAD(, target) conf_targets;
186 TAILQ_HEAD(, auth_group) conf_auth_groups;
187 TAILQ_HEAD(, port) conf_ports;
188 TAILQ_HEAD(, portal_group) conf_portal_groups;
189 TAILQ_HEAD(, isns) conf_isns;
190 int conf_isns_period;
191 int conf_isns_timeout;
196 #ifdef ICL_KERNEL_PROXY
199 struct pidfh *conf_pidfh;
201 bool conf_default_pg_defined;
202 bool conf_default_ag_defined;
203 bool conf_kernel_port_on;
206 #define CONN_SESSION_TYPE_NONE 0
207 #define CONN_SESSION_TYPE_DISCOVERY 1
208 #define CONN_SESSION_TYPE_NORMAL 2
210 #define CONN_DIGEST_NONE 0
211 #define CONN_DIGEST_CRC32C 1
214 struct portal *conn_portal;
215 struct port *conn_port;
216 struct target *conn_target;
218 int conn_session_type;
219 char *conn_initiator_name;
220 char *conn_initiator_addr;
221 char *conn_initiator_alias;
222 uint8_t conn_initiator_isid[6];
223 struct sockaddr_storage conn_initiator_sa;
225 uint32_t conn_statsn;
226 size_t conn_max_data_segment_length;
227 size_t conn_max_burst_length;
228 int conn_immediate_data;
229 int conn_header_digest;
230 int conn_data_digest;
231 const char *conn_user;
232 struct chap *conn_chap;
236 struct connection *pdu_connection;
237 struct iscsi_bhs *pdu_bhs;
242 #define KEYS_MAX 1024
245 char *keys_names[KEYS_MAX];
246 char *keys_values[KEYS_MAX];
248 size_t keys_data_len;
251 #define CHAP_CHALLENGE_LEN 1024
254 unsigned char chap_id;
255 char chap_challenge[CHAP_CHALLENGE_LEN];
256 char chap_response[MD5_DIGEST_LENGTH];
261 unsigned char rchap_id;
262 void *rchap_challenge;
263 size_t rchap_challenge_len;
266 struct chap *chap_new(void);
267 char *chap_get_id(const struct chap *chap);
268 char *chap_get_challenge(const struct chap *chap);
269 int chap_receive(struct chap *chap, const char *response);
270 int chap_authenticate(struct chap *chap,
272 void chap_delete(struct chap *chap);
274 struct rchap *rchap_new(const char *secret);
275 int rchap_receive(struct rchap *rchap,
276 const char *id, const char *challenge);
277 char *rchap_get_response(struct rchap *rchap);
278 void rchap_delete(struct rchap *rchap);
280 struct conf *conf_new(void);
281 struct conf *conf_new_from_file(const char *path);
282 struct conf *conf_new_from_kernel(void);
283 void conf_delete(struct conf *conf);
284 int conf_verify(struct conf *conf);
286 struct auth_group *auth_group_new(struct conf *conf, const char *name);
287 void auth_group_delete(struct auth_group *ag);
288 struct auth_group *auth_group_find(const struct conf *conf,
290 int auth_group_set_type(struct auth_group *ag,
293 const struct auth *auth_new_chap(struct auth_group *ag,
294 const char *user, const char *secret);
295 const struct auth *auth_new_chap_mutual(struct auth_group *ag,
296 const char *user, const char *secret,
297 const char *user2, const char *secret2);
298 const struct auth *auth_find(const struct auth_group *ag,
301 const struct auth_name *auth_name_new(struct auth_group *ag,
302 const char *initiator_name);
303 bool auth_name_defined(const struct auth_group *ag);
304 const struct auth_name *auth_name_find(const struct auth_group *ag,
305 const char *initiator_name);
306 int auth_name_check(const struct auth_group *ag,
307 const char *initiator_name);
309 const struct auth_portal *auth_portal_new(struct auth_group *ag,
310 const char *initiator_portal);
311 bool auth_portal_defined(const struct auth_group *ag);
312 const struct auth_portal *auth_portal_find(const struct auth_group *ag,
313 const struct sockaddr_storage *sa);
314 int auth_portal_check(const struct auth_group *ag,
315 const struct sockaddr_storage *sa);
317 struct portal_group *portal_group_new(struct conf *conf, const char *name);
318 void portal_group_delete(struct portal_group *pg);
319 struct portal_group *portal_group_find(const struct conf *conf,
321 int portal_group_add_listen(struct portal_group *pg,
322 const char *listen, bool iser);
323 int portal_group_set_filter(struct portal_group *pg,
325 int portal_group_set_redirection(struct portal_group *pg,
328 int isns_new(struct conf *conf, const char *addr);
329 void isns_delete(struct isns *is);
330 void isns_register(struct isns *isns, struct isns *oldisns);
331 void isns_check(struct isns *isns);
332 void isns_deregister(struct isns *isns);
334 struct port *port_new(struct conf *conf, struct target *target,
335 struct portal_group *pg);
336 struct port *port_find(const struct conf *conf, const char *name);
337 struct port *port_find_in_pg(const struct portal_group *pg,
339 void port_delete(struct port *port);
341 struct target *target_new(struct conf *conf, const char *name);
342 void target_delete(struct target *target);
343 struct target *target_find(struct conf *conf,
345 int target_set_redirection(struct target *target,
348 struct lun *lun_new(struct conf *conf, const char *name);
349 void lun_delete(struct lun *lun);
350 struct lun *lun_find(const struct conf *conf, const char *name);
351 void lun_set_backend(struct lun *lun, const char *value);
352 void lun_set_blocksize(struct lun *lun, size_t value);
353 void lun_set_device_id(struct lun *lun, const char *value);
354 void lun_set_path(struct lun *lun, const char *value);
355 void lun_set_scsiname(struct lun *lun, const char *value);
356 void lun_set_serial(struct lun *lun, const char *value);
357 void lun_set_size(struct lun *lun, size_t value);
358 void lun_set_ctl_lun(struct lun *lun, uint32_t value);
360 struct lun_option *lun_option_new(struct lun *lun,
361 const char *name, const char *value);
362 void lun_option_delete(struct lun_option *clo);
363 struct lun_option *lun_option_find(const struct lun *lun,
365 void lun_option_set(struct lun_option *clo,
368 void kernel_init(void);
369 int kernel_lun_add(struct lun *lun);
370 int kernel_lun_resize(struct lun *lun);
371 int kernel_lun_remove(struct lun *lun);
372 void kernel_handoff(struct connection *conn);
373 int kernel_port_add(struct port *port);
374 int kernel_port_update(struct port *port);
375 int kernel_port_remove(struct port *port);
376 void kernel_capsicate(void);
378 #ifdef ICL_KERNEL_PROXY
379 void kernel_listen(struct addrinfo *ai, bool iser,
381 void kernel_accept(int *connection_id, int *portal_id,
382 struct sockaddr *client_sa,
383 socklen_t *client_salen);
384 void kernel_send(struct pdu *pdu);
385 void kernel_receive(struct pdu *pdu);
388 struct keys *keys_new(void);
389 void keys_delete(struct keys *keys);
390 void keys_load(struct keys *keys, const struct pdu *pdu);
391 void keys_save(struct keys *keys, struct pdu *pdu);
392 const char *keys_find(struct keys *keys, const char *name);
393 int keys_find_int(struct keys *keys, const char *name);
394 void keys_add(struct keys *keys,
395 const char *name, const char *value);
396 void keys_add_int(struct keys *keys,
397 const char *name, int value);
399 struct pdu *pdu_new(struct connection *conn);
400 struct pdu *pdu_new_response(struct pdu *request);
401 void pdu_delete(struct pdu *pdu);
402 void pdu_receive(struct pdu *request);
403 void pdu_send(struct pdu *response);
405 void login(struct connection *conn);
407 void discovery(struct connection *conn);
409 void log_init(int level);
410 void log_set_peer_name(const char *name);
411 void log_set_peer_addr(const char *addr);
412 void log_err(int, const char *, ...)
413 __dead2 __printflike(2, 3);
414 void log_errx(int, const char *, ...)
415 __dead2 __printflike(2, 3);
416 void log_warn(const char *, ...) __printflike(1, 2);
417 void log_warnx(const char *, ...) __printflike(1, 2);
418 void log_debugx(const char *, ...) __printflike(1, 2);
420 char *checked_strdup(const char *);
421 bool valid_iscsi_name(const char *name);
422 void set_timeout(int timeout, int fatal);
423 bool timed_out(void);