1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #ifndef CTLD_H 33 #define CTLD_H 34 35 #include <sys/queue.h> 36 #ifdef ICL_KERNEL_PROXY 37 #include <sys/types.h> 38 #endif 39 #include <sys/socket.h> 40 #include <stdbool.h> 41 #include <libutil.h> 42 #include <openssl/md5.h> 43 44 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf" 45 #define DEFAULT_PIDFILE "/var/run/ctld.pid" 46 #define DEFAULT_BLOCKSIZE 512 47 48 #define MAX_NAME_LEN 223 49 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024) 50 #define MAX_BURST_LENGTH 16776192 51 52 struct auth { 53 TAILQ_ENTRY(auth) a_next; 54 struct auth_group *a_auth_group; 55 char *a_user; 56 char *a_secret; 57 char *a_mutual_user; 58 char *a_mutual_secret; 59 }; 60 61 struct auth_name { 62 TAILQ_ENTRY(auth_name) an_next; 63 struct auth_group *an_auth_group; 64 char *an_initator_name; 65 }; 66 67 struct auth_portal { 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; 72 int ap_mask; 73 }; 74 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 80 81 struct auth_group { 82 TAILQ_ENTRY(auth_group) ag_next; 83 struct conf *ag_conf; 84 char *ag_name; 85 struct target *ag_target; 86 int ag_type; 87 TAILQ_HEAD(, auth) ag_auths; 88 TAILQ_HEAD(, auth_name) ag_names; 89 TAILQ_HEAD(, auth_portal) ag_portals; 90 }; 91 92 struct portal { 93 TAILQ_ENTRY(portal) p_next; 94 struct portal_group *p_portal_group; 95 bool p_iser; 96 char *p_listen; 97 struct addrinfo *p_ai; 98 #ifdef ICL_KERNEL_PROXY 99 int p_id; 100 #endif 101 102 TAILQ_HEAD(, target) p_targets; 103 int p_socket; 104 }; 105 106 struct portal_group { 107 TAILQ_ENTRY(portal_group) pg_next; 108 struct conf *pg_conf; 109 char *pg_name; 110 struct auth_group *pg_discovery_auth_group; 111 bool pg_unassigned; 112 TAILQ_HEAD(, portal) pg_portals; 113 114 uint16_t pg_tag; 115 }; 116 117 struct lun_option { 118 TAILQ_ENTRY(lun_option) lo_next; 119 struct lun *lo_lun; 120 char *lo_name; 121 char *lo_value; 122 }; 123 124 struct lun { 125 TAILQ_ENTRY(lun) l_next; 126 TAILQ_HEAD(, lun_option) l_options; 127 struct target *l_target; 128 int l_lun; 129 char *l_backend; 130 int l_blocksize; 131 char *l_device_id; 132 char *l_path; 133 char *l_serial; 134 int64_t l_size; 135 136 int l_ctl_lun; 137 }; 138 139 struct target { 140 TAILQ_ENTRY(target) t_next; 141 TAILQ_HEAD(, lun) t_luns; 142 struct conf *t_conf; 143 struct auth_group *t_auth_group; 144 struct portal_group *t_portal_group; 145 char *t_name; 146 char *t_alias; 147 }; 148 149 struct isns { 150 TAILQ_ENTRY(isns) i_next; 151 struct conf *i_conf; 152 char *i_addr; 153 struct addrinfo *i_ai; 154 }; 155 156 struct conf { 157 char *conf_pidfile_path; 158 TAILQ_HEAD(, target) conf_targets; 159 TAILQ_HEAD(, auth_group) conf_auth_groups; 160 TAILQ_HEAD(, portal_group) conf_portal_groups; 161 TAILQ_HEAD(, isns) conf_isns; 162 int conf_isns_period; 163 int conf_isns_timeout; 164 int conf_debug; 165 int conf_timeout; 166 int conf_maxproc; 167 168 uint16_t conf_last_portal_group_tag; 169 #ifdef ICL_KERNEL_PROXY 170 int conf_portal_id; 171 #endif 172 struct pidfh *conf_pidfh; 173 174 bool conf_default_pg_defined; 175 bool conf_default_ag_defined; 176 bool conf_kernel_port_on; 177 }; 178 179 #define CONN_SESSION_TYPE_NONE 0 180 #define CONN_SESSION_TYPE_DISCOVERY 1 181 #define CONN_SESSION_TYPE_NORMAL 2 182 183 #define CONN_DIGEST_NONE 0 184 #define CONN_DIGEST_CRC32C 1 185 186 struct connection { 187 struct portal *conn_portal; 188 struct target *conn_target; 189 int conn_socket; 190 int conn_session_type; 191 char *conn_initiator_name; 192 char *conn_initiator_addr; 193 char *conn_initiator_alias; 194 uint8_t conn_initiator_isid[6]; 195 struct sockaddr_storage conn_initiator_sa; 196 uint32_t conn_cmdsn; 197 uint32_t conn_statsn; 198 size_t conn_max_data_segment_length; 199 size_t conn_max_burst_length; 200 int conn_immediate_data; 201 int conn_header_digest; 202 int conn_data_digest; 203 }; 204 205 struct pdu { 206 struct connection *pdu_connection; 207 struct iscsi_bhs *pdu_bhs; 208 char *pdu_data; 209 size_t pdu_data_len; 210 }; 211 212 #define KEYS_MAX 1024 213 214 struct keys { 215 char *keys_names[KEYS_MAX]; 216 char *keys_values[KEYS_MAX]; 217 char *keys_data; 218 size_t keys_data_len; 219 }; 220 221 #define CHAP_CHALLENGE_LEN 1024 222 223 struct chap { 224 unsigned char chap_id; 225 char chap_challenge[CHAP_CHALLENGE_LEN]; 226 char chap_response[MD5_DIGEST_LENGTH]; 227 }; 228 229 struct rchap { 230 char *rchap_secret; 231 unsigned char rchap_id; 232 void *rchap_challenge; 233 size_t rchap_challenge_len; 234 }; 235 236 struct chap *chap_new(void); 237 char *chap_get_id(const struct chap *chap); 238 char *chap_get_challenge(const struct chap *chap); 239 int chap_receive(struct chap *chap, const char *response); 240 int chap_authenticate(struct chap *chap, 241 const char *secret); 242 void chap_delete(struct chap *chap); 243 244 struct rchap *rchap_new(const char *secret); 245 int rchap_receive(struct rchap *rchap, 246 const char *id, const char *challenge); 247 char *rchap_get_response(struct rchap *rchap); 248 void rchap_delete(struct rchap *rchap); 249 250 struct conf *conf_new(void); 251 struct conf *conf_new_from_file(const char *path); 252 struct conf *conf_new_from_kernel(void); 253 void conf_delete(struct conf *conf); 254 int conf_verify(struct conf *conf); 255 256 struct auth_group *auth_group_new(struct conf *conf, const char *name); 257 void auth_group_delete(struct auth_group *ag); 258 struct auth_group *auth_group_find(const struct conf *conf, 259 const char *name); 260 int auth_group_set_type_str(struct auth_group *ag, 261 const char *type); 262 263 const struct auth *auth_new_chap(struct auth_group *ag, 264 const char *user, const char *secret); 265 const struct auth *auth_new_chap_mutual(struct auth_group *ag, 266 const char *user, const char *secret, 267 const char *user2, const char *secret2); 268 const struct auth *auth_find(const struct auth_group *ag, 269 const char *user); 270 271 const struct auth_name *auth_name_new(struct auth_group *ag, 272 const char *initiator_name); 273 bool auth_name_defined(const struct auth_group *ag); 274 const struct auth_name *auth_name_find(const struct auth_group *ag, 275 const char *initiator_name); 276 int auth_name_check(const struct auth_group *ag, 277 const char *initiator_name); 278 279 const struct auth_portal *auth_portal_new(struct auth_group *ag, 280 const char *initiator_portal); 281 bool auth_portal_defined(const struct auth_group *ag); 282 const struct auth_portal *auth_portal_find(const struct auth_group *ag, 283 const struct sockaddr_storage *sa); 284 int auth_portal_check(const struct auth_group *ag, 285 const struct sockaddr_storage *sa); 286 287 struct portal_group *portal_group_new(struct conf *conf, const char *name); 288 void portal_group_delete(struct portal_group *pg); 289 struct portal_group *portal_group_find(const struct conf *conf, 290 const char *name); 291 int portal_group_add_listen(struct portal_group *pg, 292 const char *listen, bool iser); 293 294 int isns_new(struct conf *conf, const char *addr); 295 void isns_delete(struct isns *is); 296 void isns_register(struct isns *isns, struct isns *oldisns); 297 void isns_check(struct isns *isns); 298 void isns_deregister(struct isns *isns); 299 300 struct target *target_new(struct conf *conf, const char *name); 301 void target_delete(struct target *target); 302 struct target *target_find(struct conf *conf, 303 const char *name); 304 305 struct lun *lun_new(struct target *target, int lun_id); 306 void lun_delete(struct lun *lun); 307 struct lun *lun_find(const struct target *target, int lun_id); 308 void lun_set_backend(struct lun *lun, const char *value); 309 void lun_set_blocksize(struct lun *lun, size_t value); 310 void lun_set_device_id(struct lun *lun, const char *value); 311 void lun_set_path(struct lun *lun, const char *value); 312 void lun_set_serial(struct lun *lun, const char *value); 313 void lun_set_size(struct lun *lun, size_t value); 314 void lun_set_ctl_lun(struct lun *lun, uint32_t value); 315 316 struct lun_option *lun_option_new(struct lun *lun, 317 const char *name, const char *value); 318 void lun_option_delete(struct lun_option *clo); 319 struct lun_option *lun_option_find(const struct lun *lun, 320 const char *name); 321 void lun_option_set(struct lun_option *clo, 322 const char *value); 323 324 void kernel_init(void); 325 int kernel_lun_add(struct lun *lun); 326 int kernel_lun_resize(struct lun *lun); 327 int kernel_lun_remove(struct lun *lun); 328 void kernel_handoff(struct connection *conn); 329 int kernel_port_add(struct target *targ); 330 int kernel_port_remove(struct target *targ); 331 void kernel_capsicate(void); 332 333 #ifdef ICL_KERNEL_PROXY 334 void kernel_listen(struct addrinfo *ai, bool iser, 335 int portal_id); 336 void kernel_accept(int *connection_id, int *portal_id, 337 struct sockaddr *client_sa, 338 socklen_t *client_salen); 339 void kernel_send(struct pdu *pdu); 340 void kernel_receive(struct pdu *pdu); 341 #endif 342 343 struct keys *keys_new(void); 344 void keys_delete(struct keys *keys); 345 void keys_load(struct keys *keys, const struct pdu *pdu); 346 void keys_save(struct keys *keys, struct pdu *pdu); 347 const char *keys_find(struct keys *keys, const char *name); 348 int keys_find_int(struct keys *keys, const char *name); 349 void keys_add(struct keys *keys, 350 const char *name, const char *value); 351 void keys_add_int(struct keys *keys, 352 const char *name, int value); 353 354 struct pdu *pdu_new(struct connection *conn); 355 struct pdu *pdu_new_response(struct pdu *request); 356 void pdu_delete(struct pdu *pdu); 357 void pdu_receive(struct pdu *request); 358 void pdu_send(struct pdu *response); 359 360 void login(struct connection *conn); 361 362 void discovery(struct connection *conn); 363 364 void log_init(int level); 365 void log_set_peer_name(const char *name); 366 void log_set_peer_addr(const char *addr); 367 void log_err(int, const char *, ...) 368 __dead2 __printflike(2, 3); 369 void log_errx(int, const char *, ...) 370 __dead2 __printflike(2, 3); 371 void log_warn(const char *, ...) __printflike(1, 2); 372 void log_warnx(const char *, ...) __printflike(1, 2); 373 void log_debugx(const char *, ...) __printflike(1, 2); 374 375 char *checked_strdup(const char *); 376 bool valid_iscsi_name(const char *name); 377 void set_timeout(int timeout, int fatal); 378 bool timed_out(void); 379 380 #endif /* !CTLD_H */ 381