1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #ifndef CTLD_H 34 #define CTLD_H 35 36 #include <sys/queue.h> 37 #ifdef ICL_KERNEL_PROXY 38 #include <sys/types.h> 39 #endif 40 #include <sys/socket.h> 41 #include <stdbool.h> 42 #include <libutil.h> 43 44 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf" 45 #define DEFAULT_PIDFILE "/var/run/ctld.pid" 46 #define DEFAULT_BLOCKSIZE 512 47 #define DEFAULT_CD_BLOCKSIZE 2048 48 49 #define MAX_LUNS 1024 50 #define MAX_NAME_LEN 223 51 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024) 52 #define SOCKBUF_SIZE 1048576 53 54 struct auth { 55 TAILQ_ENTRY(auth) a_next; 56 struct auth_group *a_auth_group; 57 char *a_user; 58 char *a_secret; 59 char *a_mutual_user; 60 char *a_mutual_secret; 61 }; 62 63 struct auth_name { 64 TAILQ_ENTRY(auth_name) an_next; 65 struct auth_group *an_auth_group; 66 char *an_initiator_name; 67 }; 68 69 struct auth_portal { 70 TAILQ_ENTRY(auth_portal) ap_next; 71 struct auth_group *ap_auth_group; 72 char *ap_initiator_portal; 73 struct sockaddr_storage ap_sa; 74 int ap_mask; 75 }; 76 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 82 83 struct auth_group { 84 TAILQ_ENTRY(auth_group) ag_next; 85 struct conf *ag_conf; 86 char *ag_name; 87 struct target *ag_target; 88 int ag_type; 89 TAILQ_HEAD(, auth) ag_auths; 90 TAILQ_HEAD(, auth_name) ag_names; 91 TAILQ_HEAD(, auth_portal) ag_portals; 92 }; 93 94 struct portal { 95 TAILQ_ENTRY(portal) p_next; 96 struct portal_group *p_portal_group; 97 bool p_iser; 98 char *p_listen; 99 struct addrinfo *p_ai; 100 #ifdef ICL_KERNEL_PROXY 101 int p_id; 102 #endif 103 104 TAILQ_HEAD(, target) p_targets; 105 int p_socket; 106 }; 107 108 TAILQ_HEAD(options, option); 109 110 #define PG_FILTER_UNKNOWN 0 111 #define PG_FILTER_NONE 1 112 #define PG_FILTER_PORTAL 2 113 #define PG_FILTER_PORTAL_NAME 3 114 #define PG_FILTER_PORTAL_NAME_AUTH 4 115 116 struct portal_group { 117 TAILQ_ENTRY(portal_group) pg_next; 118 struct conf *pg_conf; 119 struct options pg_options; 120 char *pg_name; 121 struct auth_group *pg_discovery_auth_group; 122 int pg_discovery_filter; 123 int pg_foreign; 124 bool pg_unassigned; 125 TAILQ_HEAD(, portal) pg_portals; 126 TAILQ_HEAD(, port) pg_ports; 127 char *pg_offload; 128 char *pg_redirection; 129 int pg_dscp; 130 int pg_pcp; 131 132 uint16_t pg_tag; 133 }; 134 135 struct pport { 136 TAILQ_ENTRY(pport) pp_next; 137 TAILQ_HEAD(, port) pp_ports; 138 struct conf *pp_conf; 139 char *pp_name; 140 141 uint32_t pp_ctl_port; 142 }; 143 144 struct port { 145 TAILQ_ENTRY(port) p_next; 146 TAILQ_ENTRY(port) p_pgs; 147 TAILQ_ENTRY(port) p_pps; 148 TAILQ_ENTRY(port) p_ts; 149 struct conf *p_conf; 150 char *p_name; 151 struct auth_group *p_auth_group; 152 struct portal_group *p_portal_group; 153 struct pport *p_pport; 154 struct target *p_target; 155 156 int p_ioctl_port; 157 int p_ioctl_pp; 158 int p_ioctl_vp; 159 uint32_t p_ctl_port; 160 }; 161 162 struct option { 163 TAILQ_ENTRY(option) o_next; 164 char *o_name; 165 char *o_value; 166 }; 167 168 struct lun { 169 TAILQ_ENTRY(lun) l_next; 170 struct conf *l_conf; 171 struct options l_options; 172 char *l_name; 173 char *l_backend; 174 uint8_t l_device_type; 175 int l_blocksize; 176 char *l_device_id; 177 char *l_path; 178 char *l_scsiname; 179 char *l_serial; 180 int64_t l_size; 181 182 int l_ctl_lun; 183 }; 184 185 struct target { 186 TAILQ_ENTRY(target) t_next; 187 struct conf *t_conf; 188 struct lun *t_luns[MAX_LUNS]; 189 struct auth_group *t_auth_group; 190 TAILQ_HEAD(, port) t_ports; 191 char *t_name; 192 char *t_alias; 193 char *t_redirection; 194 }; 195 196 struct isns { 197 TAILQ_ENTRY(isns) i_next; 198 struct conf *i_conf; 199 char *i_addr; 200 struct addrinfo *i_ai; 201 }; 202 203 struct conf { 204 char *conf_pidfile_path; 205 TAILQ_HEAD(, lun) conf_luns; 206 TAILQ_HEAD(, target) conf_targets; 207 TAILQ_HEAD(, auth_group) conf_auth_groups; 208 TAILQ_HEAD(, port) conf_ports; 209 TAILQ_HEAD(, portal_group) conf_portal_groups; 210 TAILQ_HEAD(, pport) conf_pports; 211 TAILQ_HEAD(, isns) conf_isns; 212 int conf_isns_period; 213 int conf_isns_timeout; 214 int conf_debug; 215 int conf_timeout; 216 int conf_maxproc; 217 218 #ifdef ICL_KERNEL_PROXY 219 int conf_portal_id; 220 #endif 221 struct pidfh *conf_pidfh; 222 223 bool conf_default_pg_defined; 224 bool conf_default_ag_defined; 225 bool conf_kernel_port_on; 226 }; 227 228 #define CONN_SESSION_TYPE_NONE 0 229 #define CONN_SESSION_TYPE_DISCOVERY 1 230 #define CONN_SESSION_TYPE_NORMAL 2 231 232 #define CONN_DIGEST_NONE 0 233 #define CONN_DIGEST_CRC32C 1 234 235 struct connection { 236 struct portal *conn_portal; 237 struct port *conn_port; 238 struct target *conn_target; 239 int conn_socket; 240 int conn_session_type; 241 char *conn_initiator_name; 242 char *conn_initiator_addr; 243 char *conn_initiator_alias; 244 uint8_t conn_initiator_isid[6]; 245 struct sockaddr_storage conn_initiator_sa; 246 uint32_t conn_cmdsn; 247 uint32_t conn_statsn; 248 int conn_max_recv_data_segment_limit; 249 int conn_max_send_data_segment_limit; 250 int conn_max_burst_limit; 251 int conn_first_burst_limit; 252 int conn_max_recv_data_segment_length; 253 int conn_max_send_data_segment_length; 254 int conn_max_burst_length; 255 int conn_first_burst_length; 256 int conn_immediate_data; 257 int conn_header_digest; 258 int conn_data_digest; 259 const char *conn_user; 260 struct chap *conn_chap; 261 }; 262 263 struct pdu { 264 struct connection *pdu_connection; 265 struct iscsi_bhs *pdu_bhs; 266 char *pdu_data; 267 size_t pdu_data_len; 268 }; 269 270 #define KEYS_MAX 1024 271 272 struct keys { 273 char *keys_names[KEYS_MAX]; 274 char *keys_values[KEYS_MAX]; 275 char *keys_data; 276 size_t keys_data_len; 277 }; 278 279 #define CHAP_CHALLENGE_LEN 1024 280 #define CHAP_DIGEST_LEN 16 /* Equal to MD5 digest size. */ 281 282 struct chap { 283 unsigned char chap_id; 284 char chap_challenge[CHAP_CHALLENGE_LEN]; 285 char chap_response[CHAP_DIGEST_LEN]; 286 }; 287 288 struct rchap { 289 char *rchap_secret; 290 unsigned char rchap_id; 291 void *rchap_challenge; 292 size_t rchap_challenge_len; 293 }; 294 295 struct chap *chap_new(void); 296 char *chap_get_id(const struct chap *chap); 297 char *chap_get_challenge(const struct chap *chap); 298 int chap_receive(struct chap *chap, const char *response); 299 int chap_authenticate(struct chap *chap, 300 const char *secret); 301 void chap_delete(struct chap *chap); 302 303 struct rchap *rchap_new(const char *secret); 304 int rchap_receive(struct rchap *rchap, 305 const char *id, const char *challenge); 306 char *rchap_get_response(struct rchap *rchap); 307 void rchap_delete(struct rchap *rchap); 308 309 int parse_conf(struct conf *conf, const char *path); 310 int uclparse_conf(struct conf *conf, const char *path); 311 312 struct conf *conf_new(void); 313 struct conf *conf_new_from_kernel(void); 314 void conf_delete(struct conf *conf); 315 int conf_verify(struct conf *conf); 316 317 struct auth_group *auth_group_new(struct conf *conf, const char *name); 318 void auth_group_delete(struct auth_group *ag); 319 struct auth_group *auth_group_find(const struct conf *conf, 320 const char *name); 321 int auth_group_set_type(struct auth_group *ag, 322 const char *type); 323 324 const struct auth *auth_new_chap(struct auth_group *ag, 325 const char *user, const char *secret); 326 const struct auth *auth_new_chap_mutual(struct auth_group *ag, 327 const char *user, const char *secret, 328 const char *user2, const char *secret2); 329 const struct auth *auth_find(const struct auth_group *ag, 330 const char *user); 331 332 const struct auth_name *auth_name_new(struct auth_group *ag, 333 const char *initiator_name); 334 bool auth_name_defined(const struct auth_group *ag); 335 const struct auth_name *auth_name_find(const struct auth_group *ag, 336 const char *initiator_name); 337 int auth_name_check(const struct auth_group *ag, 338 const char *initiator_name); 339 340 const struct auth_portal *auth_portal_new(struct auth_group *ag, 341 const char *initiator_portal); 342 bool auth_portal_defined(const struct auth_group *ag); 343 const struct auth_portal *auth_portal_find(const struct auth_group *ag, 344 const struct sockaddr_storage *sa); 345 int auth_portal_check(const struct auth_group *ag, 346 const struct sockaddr_storage *sa); 347 348 struct portal_group *portal_group_new(struct conf *conf, const char *name); 349 void portal_group_delete(struct portal_group *pg); 350 struct portal_group *portal_group_find(const struct conf *conf, 351 const char *name); 352 int portal_group_add_listen(struct portal_group *pg, 353 const char *listen, bool iser); 354 int portal_group_set_filter(struct portal_group *pg, 355 const char *filter); 356 int portal_group_set_offload(struct portal_group *pg, 357 const char *offload); 358 int portal_group_set_redirection(struct portal_group *pg, 359 const char *addr); 360 361 int isns_new(struct conf *conf, const char *addr); 362 void isns_delete(struct isns *is); 363 void isns_register(struct isns *isns, struct isns *oldisns); 364 void isns_check(struct isns *isns); 365 void isns_deregister(struct isns *isns); 366 367 struct pport *pport_new(struct conf *conf, const char *name, 368 uint32_t ctl_port); 369 struct pport *pport_find(const struct conf *conf, const char *name); 370 struct pport *pport_copy(struct pport *pport, struct conf *conf); 371 void pport_delete(struct pport *pport); 372 373 struct port *port_new(struct conf *conf, struct target *target, 374 struct portal_group *pg); 375 struct port *port_new_ioctl(struct conf *conf, struct target *target, 376 int pp, int vp); 377 struct port *port_new_pp(struct conf *conf, struct target *target, 378 struct pport *pp); 379 struct port *port_find(const struct conf *conf, const char *name); 380 struct port *port_find_in_pg(const struct portal_group *pg, 381 const char *target); 382 void port_delete(struct port *port); 383 int port_is_dummy(struct port *port); 384 385 struct target *target_new(struct conf *conf, const char *name); 386 void target_delete(struct target *target); 387 struct target *target_find(struct conf *conf, 388 const char *name); 389 int target_set_redirection(struct target *target, 390 const char *addr); 391 392 struct lun *lun_new(struct conf *conf, const char *name); 393 void lun_delete(struct lun *lun); 394 struct lun *lun_find(const struct conf *conf, const char *name); 395 void lun_set_backend(struct lun *lun, const char *value); 396 void lun_set_device_type(struct lun *lun, uint8_t value); 397 void lun_set_blocksize(struct lun *lun, size_t value); 398 void lun_set_device_id(struct lun *lun, const char *value); 399 void lun_set_path(struct lun *lun, const char *value); 400 void lun_set_scsiname(struct lun *lun, const char *value); 401 void lun_set_serial(struct lun *lun, const char *value); 402 void lun_set_size(struct lun *lun, size_t value); 403 void lun_set_ctl_lun(struct lun *lun, uint32_t value); 404 405 struct option *option_new(struct options *os, 406 const char *name, const char *value); 407 void option_delete(struct options *os, struct option *co); 408 struct option *option_find(const struct options *os, const char *name); 409 void option_set(struct option *o, const char *value); 410 411 void kernel_init(void); 412 int kernel_lun_add(struct lun *lun); 413 int kernel_lun_modify(struct lun *lun); 414 int kernel_lun_remove(struct lun *lun); 415 void kernel_handoff(struct connection *conn); 416 void kernel_limits(const char *offload, 417 int *max_recv_data_segment_length, 418 int *max_send_data_segment_length, 419 int *max_burst_length, 420 int *first_burst_length); 421 int kernel_port_add(struct port *port); 422 int kernel_port_update(struct port *port, struct port *old); 423 int kernel_port_remove(struct port *port); 424 void kernel_capsicate(void); 425 426 #ifdef ICL_KERNEL_PROXY 427 void kernel_listen(struct addrinfo *ai, bool iser, 428 int portal_id); 429 void kernel_accept(int *connection_id, int *portal_id, 430 struct sockaddr *client_sa, 431 socklen_t *client_salen); 432 void kernel_send(struct pdu *pdu); 433 void kernel_receive(struct pdu *pdu); 434 #endif 435 436 struct keys *keys_new(void); 437 void keys_delete(struct keys *keys); 438 void keys_load(struct keys *keys, const struct pdu *pdu); 439 void keys_save(struct keys *keys, struct pdu *pdu); 440 const char *keys_find(struct keys *keys, const char *name); 441 void keys_add(struct keys *keys, 442 const char *name, const char *value); 443 void keys_add_int(struct keys *keys, 444 const char *name, int value); 445 446 struct pdu *pdu_new(struct connection *conn); 447 struct pdu *pdu_new_response(struct pdu *request); 448 void pdu_delete(struct pdu *pdu); 449 void pdu_receive(struct pdu *request); 450 void pdu_send(struct pdu *response); 451 452 void login(struct connection *conn); 453 454 void discovery(struct connection *conn); 455 456 void log_init(int level); 457 void log_set_peer_name(const char *name); 458 void log_set_peer_addr(const char *addr); 459 void log_err(int, const char *, ...) 460 __dead2 __printflike(2, 3); 461 void log_errx(int, const char *, ...) 462 __dead2 __printflike(2, 3); 463 void log_warn(const char *, ...) __printflike(1, 2); 464 void log_warnx(const char *, ...) __printflike(1, 2); 465 void log_debugx(const char *, ...) __printflike(1, 2); 466 467 char *checked_strdup(const char *); 468 bool valid_iscsi_name(const char *name); 469 void set_timeout(int timeout, int fatal); 470 bool timed_out(void); 471 472 #endif /* !CTLD_H */ 473