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
51 #define SOCKBUF_SIZE 1048576
54 TAILQ_ENTRY(auth) a_next;
55 struct auth_group *a_auth_group;
59 char *a_mutual_secret;
63 TAILQ_ENTRY(auth_name) an_next;
64 struct auth_group *an_auth_group;
65 char *an_initator_name;
69 TAILQ_ENTRY(auth_portal) ap_next;
70 struct auth_group *ap_auth_group;
71 char *ap_initator_portal;
72 struct sockaddr_storage ap_sa;
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
83 TAILQ_ENTRY(auth_group) ag_next;
86 struct target *ag_target;
88 TAILQ_HEAD(, auth) ag_auths;
89 TAILQ_HEAD(, auth_name) ag_names;
90 TAILQ_HEAD(, auth_portal) ag_portals;
94 TAILQ_ENTRY(portal) p_next;
95 struct portal_group *p_portal_group;
98 struct addrinfo *p_ai;
99 #ifdef ICL_KERNEL_PROXY
103 TAILQ_HEAD(, target) p_targets;
107 #define PG_FILTER_UNKNOWN 0
108 #define PG_FILTER_NONE 1
109 #define PG_FILTER_PORTAL 2
110 #define PG_FILTER_PORTAL_NAME 3
111 #define PG_FILTER_PORTAL_NAME_AUTH 4
113 struct portal_group {
114 TAILQ_ENTRY(portal_group) pg_next;
115 struct conf *pg_conf;
117 struct auth_group *pg_discovery_auth_group;
118 int pg_discovery_filter;
120 TAILQ_HEAD(, portal) pg_portals;
121 char *pg_redirection;
127 TAILQ_ENTRY(lun_option) lo_next;
134 TAILQ_ENTRY(lun) l_next;
135 TAILQ_HEAD(, lun_option) l_options;
136 struct target *l_target;
149 TAILQ_ENTRY(target) t_next;
150 TAILQ_HEAD(, lun) t_luns;
152 struct auth_group *t_auth_group;
153 struct portal_group *t_portal_group;
160 TAILQ_ENTRY(isns) i_next;
163 struct addrinfo *i_ai;
167 char *conf_pidfile_path;
168 TAILQ_HEAD(, target) conf_targets;
169 TAILQ_HEAD(, auth_group) conf_auth_groups;
170 TAILQ_HEAD(, portal_group) conf_portal_groups;
171 TAILQ_HEAD(, isns) conf_isns;
172 int conf_isns_period;
173 int conf_isns_timeout;
178 uint16_t conf_last_portal_group_tag;
179 #ifdef ICL_KERNEL_PROXY
182 struct pidfh *conf_pidfh;
184 bool conf_default_pg_defined;
185 bool conf_default_ag_defined;
186 bool conf_kernel_port_on;
189 #define CONN_SESSION_TYPE_NONE 0
190 #define CONN_SESSION_TYPE_DISCOVERY 1
191 #define CONN_SESSION_TYPE_NORMAL 2
193 #define CONN_DIGEST_NONE 0
194 #define CONN_DIGEST_CRC32C 1
197 struct portal *conn_portal;
198 struct target *conn_target;
200 int conn_session_type;
201 char *conn_initiator_name;
202 char *conn_initiator_addr;
203 char *conn_initiator_alias;
204 uint8_t conn_initiator_isid[6];
205 struct sockaddr_storage conn_initiator_sa;
207 uint32_t conn_statsn;
208 size_t conn_max_data_segment_length;
209 size_t conn_max_burst_length;
210 int conn_immediate_data;
211 int conn_header_digest;
212 int conn_data_digest;
213 const char *conn_user;
214 struct chap *conn_chap;
218 struct connection *pdu_connection;
219 struct iscsi_bhs *pdu_bhs;
224 #define KEYS_MAX 1024
227 char *keys_names[KEYS_MAX];
228 char *keys_values[KEYS_MAX];
230 size_t keys_data_len;
233 #define CHAP_CHALLENGE_LEN 1024
236 unsigned char chap_id;
237 char chap_challenge[CHAP_CHALLENGE_LEN];
238 char chap_response[MD5_DIGEST_LENGTH];
243 unsigned char rchap_id;
244 void *rchap_challenge;
245 size_t rchap_challenge_len;
248 struct chap *chap_new(void);
249 char *chap_get_id(const struct chap *chap);
250 char *chap_get_challenge(const struct chap *chap);
251 int chap_receive(struct chap *chap, const char *response);
252 int chap_authenticate(struct chap *chap,
254 void chap_delete(struct chap *chap);
256 struct rchap *rchap_new(const char *secret);
257 int rchap_receive(struct rchap *rchap,
258 const char *id, const char *challenge);
259 char *rchap_get_response(struct rchap *rchap);
260 void rchap_delete(struct rchap *rchap);
262 struct conf *conf_new(void);
263 struct conf *conf_new_from_file(const char *path);
264 struct conf *conf_new_from_kernel(void);
265 void conf_delete(struct conf *conf);
266 int conf_verify(struct conf *conf);
268 struct auth_group *auth_group_new(struct conf *conf, const char *name);
269 void auth_group_delete(struct auth_group *ag);
270 struct auth_group *auth_group_find(const struct conf *conf,
272 int auth_group_set_type(struct auth_group *ag,
275 const struct auth *auth_new_chap(struct auth_group *ag,
276 const char *user, const char *secret);
277 const struct auth *auth_new_chap_mutual(struct auth_group *ag,
278 const char *user, const char *secret,
279 const char *user2, const char *secret2);
280 const struct auth *auth_find(const struct auth_group *ag,
283 const struct auth_name *auth_name_new(struct auth_group *ag,
284 const char *initiator_name);
285 bool auth_name_defined(const struct auth_group *ag);
286 const struct auth_name *auth_name_find(const struct auth_group *ag,
287 const char *initiator_name);
288 int auth_name_check(const struct auth_group *ag,
289 const char *initiator_name);
291 const struct auth_portal *auth_portal_new(struct auth_group *ag,
292 const char *initiator_portal);
293 bool auth_portal_defined(const struct auth_group *ag);
294 const struct auth_portal *auth_portal_find(const struct auth_group *ag,
295 const struct sockaddr_storage *sa);
296 int auth_portal_check(const struct auth_group *ag,
297 const struct sockaddr_storage *sa);
299 struct portal_group *portal_group_new(struct conf *conf, const char *name);
300 void portal_group_delete(struct portal_group *pg);
301 struct portal_group *portal_group_find(const struct conf *conf,
303 int portal_group_add_listen(struct portal_group *pg,
304 const char *listen, bool iser);
305 int portal_group_set_filter(struct portal_group *pg,
307 int portal_group_set_redirection(struct portal_group *pg,
310 int isns_new(struct conf *conf, const char *addr);
311 void isns_delete(struct isns *is);
312 void isns_register(struct isns *isns, struct isns *oldisns);
313 void isns_check(struct isns *isns);
314 void isns_deregister(struct isns *isns);
316 struct target *target_new(struct conf *conf, const char *name);
317 void target_delete(struct target *target);
318 struct target *target_find(struct conf *conf,
320 int target_set_redirection(struct target *target,
323 struct lun *lun_new(struct target *target, int lun_id);
324 void lun_delete(struct lun *lun);
325 struct lun *lun_find(const struct target *target, int lun_id);
326 void lun_set_backend(struct lun *lun, const char *value);
327 void lun_set_blocksize(struct lun *lun, size_t value);
328 void lun_set_device_id(struct lun *lun, const char *value);
329 void lun_set_path(struct lun *lun, const char *value);
330 void lun_set_serial(struct lun *lun, const char *value);
331 void lun_set_size(struct lun *lun, size_t value);
332 void lun_set_ctl_lun(struct lun *lun, uint32_t value);
334 struct lun_option *lun_option_new(struct lun *lun,
335 const char *name, const char *value);
336 void lun_option_delete(struct lun_option *clo);
337 struct lun_option *lun_option_find(const struct lun *lun,
339 void lun_option_set(struct lun_option *clo,
342 void kernel_init(void);
343 int kernel_lun_add(struct lun *lun);
344 int kernel_lun_resize(struct lun *lun);
345 int kernel_lun_remove(struct lun *lun);
346 void kernel_handoff(struct connection *conn);
347 int kernel_port_add(struct target *targ);
348 int kernel_port_remove(struct target *targ);
349 void kernel_capsicate(void);
351 #ifdef ICL_KERNEL_PROXY
352 void kernel_listen(struct addrinfo *ai, bool iser,
354 void kernel_accept(int *connection_id, int *portal_id,
355 struct sockaddr *client_sa,
356 socklen_t *client_salen);
357 void kernel_send(struct pdu *pdu);
358 void kernel_receive(struct pdu *pdu);
361 struct keys *keys_new(void);
362 void keys_delete(struct keys *keys);
363 void keys_load(struct keys *keys, const struct pdu *pdu);
364 void keys_save(struct keys *keys, struct pdu *pdu);
365 const char *keys_find(struct keys *keys, const char *name);
366 int keys_find_int(struct keys *keys, const char *name);
367 void keys_add(struct keys *keys,
368 const char *name, const char *value);
369 void keys_add_int(struct keys *keys,
370 const char *name, int value);
372 struct pdu *pdu_new(struct connection *conn);
373 struct pdu *pdu_new_response(struct pdu *request);
374 void pdu_delete(struct pdu *pdu);
375 void pdu_receive(struct pdu *request);
376 void pdu_send(struct pdu *response);
378 void login(struct connection *conn);
380 void discovery(struct connection *conn);
382 void log_init(int level);
383 void log_set_peer_name(const char *name);
384 void log_set_peer_addr(const char *addr);
385 void log_err(int, const char *, ...)
386 __dead2 __printflike(2, 3);
387 void log_errx(int, const char *, ...)
388 __dead2 __printflike(2, 3);
389 void log_warn(const char *, ...) __printflike(1, 2);
390 void log_warnx(const char *, ...) __printflike(1, 2);
391 void log_debugx(const char *, ...) __printflike(1, 2);
393 char *checked_strdup(const char *);
394 bool valid_iscsi_name(const char *name);
395 void set_timeout(int timeout, int fatal);
396 bool timed_out(void);