1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 31 #ifndef CTLD_H 32 #define CTLD_H 33 34 #include <sys/queue.h> 35 #ifdef ICL_KERNEL_PROXY 36 #include <sys/types.h> 37 #endif 38 #include <sys/socket.h> 39 #include <stdbool.h> 40 #include <libiscsiutil.h> 41 #include <libutil.h> 42 43 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf" 44 #define DEFAULT_PIDFILE "/var/run/ctld.pid" 45 #define DEFAULT_BLOCKSIZE 512 46 #define DEFAULT_CD_BLOCKSIZE 2048 47 48 #define MAX_LUNS 1024 49 #define MAX_NAME_LEN 223 50 #define MAX_DATA_SEGMENT_LENGTH (128 * 1024) 51 #define SOCKBUF_SIZE 1048576 52 53 struct auth { 54 TAILQ_ENTRY(auth) a_next; 55 struct auth_group *a_auth_group; 56 char *a_user; 57 char *a_secret; 58 char *a_mutual_user; 59 char *a_mutual_secret; 60 }; 61 62 struct auth_name { 63 TAILQ_ENTRY(auth_name) an_next; 64 struct auth_group *an_auth_group; 65 char *an_initiator_name; 66 }; 67 68 struct auth_portal { 69 TAILQ_ENTRY(auth_portal) ap_next; 70 struct auth_group *ap_auth_group; 71 char *ap_initiator_portal; 72 struct sockaddr_storage ap_sa; 73 int ap_mask; 74 }; 75 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 81 82 struct auth_group { 83 TAILQ_ENTRY(auth_group) ag_next; 84 struct conf *ag_conf; 85 char *ag_name; 86 struct target *ag_target; 87 int ag_type; 88 TAILQ_HEAD(, auth) ag_auths; 89 TAILQ_HEAD(, auth_name) ag_names; 90 TAILQ_HEAD(, auth_portal) ag_portals; 91 }; 92 93 struct portal { 94 TAILQ_ENTRY(portal) p_next; 95 struct portal_group *p_portal_group; 96 bool p_iser; 97 char *p_listen; 98 struct addrinfo *p_ai; 99 #ifdef ICL_KERNEL_PROXY 100 int p_id; 101 #endif 102 103 TAILQ_HEAD(, target) p_targets; 104 int p_socket; 105 }; 106 107 TAILQ_HEAD(options, option); 108 109 #define PG_FILTER_UNKNOWN 0 110 #define PG_FILTER_NONE 1 111 #define PG_FILTER_PORTAL 2 112 #define PG_FILTER_PORTAL_NAME 3 113 #define PG_FILTER_PORTAL_NAME_AUTH 4 114 115 struct portal_group { 116 TAILQ_ENTRY(portal_group) pg_next; 117 struct conf *pg_conf; 118 struct options pg_options; 119 char *pg_name; 120 struct auth_group *pg_discovery_auth_group; 121 int pg_discovery_filter; 122 int pg_foreign; 123 bool pg_unassigned; 124 TAILQ_HEAD(, portal) pg_portals; 125 TAILQ_HEAD(, port) pg_ports; 126 char *pg_offload; 127 char *pg_redirection; 128 int pg_dscp; 129 int pg_pcp; 130 131 uint16_t pg_tag; 132 }; 133 134 /* Ports created by the kernel. Perhaps the "p" means "physical" ? */ 135 struct pport { 136 TAILQ_ENTRY(pport) pp_next; 137 TAILQ_HEAD(, port) pp_ports; 138 struct kports *pp_kports; 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 /* Name of this target's physical port, if any, i.e. "isp0" */ 195 char *t_pport; 196 }; 197 198 struct isns { 199 TAILQ_ENTRY(isns) i_next; 200 struct conf *i_conf; 201 char *i_addr; 202 struct addrinfo *i_ai; 203 }; 204 205 struct conf { 206 char *conf_pidfile_path; 207 TAILQ_HEAD(, lun) conf_luns; 208 TAILQ_HEAD(, target) conf_targets; 209 TAILQ_HEAD(, auth_group) conf_auth_groups; 210 TAILQ_HEAD(, port) conf_ports; 211 TAILQ_HEAD(, portal_group) conf_portal_groups; 212 TAILQ_HEAD(, isns) conf_isns; 213 int conf_isns_period; 214 int conf_isns_timeout; 215 int conf_debug; 216 int conf_timeout; 217 int conf_maxproc; 218 219 #ifdef ICL_KERNEL_PROXY 220 int conf_portal_id; 221 #endif 222 struct pidfh *conf_pidfh; 223 224 bool conf_default_pg_defined; 225 bool conf_default_ag_defined; 226 bool conf_kernel_port_on; 227 }; 228 229 /* Physical ports exposed by the kernel */ 230 struct kports { 231 TAILQ_HEAD(, pport) pports; 232 }; 233 234 #define CONN_SESSION_TYPE_NONE 0 235 #define CONN_SESSION_TYPE_DISCOVERY 1 236 #define CONN_SESSION_TYPE_NORMAL 2 237 238 struct ctld_connection { 239 struct connection conn; 240 struct portal *conn_portal; 241 struct port *conn_port; 242 struct target *conn_target; 243 int conn_session_type; 244 char *conn_initiator_name; 245 char *conn_initiator_addr; 246 char *conn_initiator_alias; 247 uint8_t conn_initiator_isid[6]; 248 struct sockaddr_storage conn_initiator_sa; 249 int conn_max_recv_data_segment_limit; 250 int conn_max_send_data_segment_limit; 251 int conn_max_burst_limit; 252 int conn_first_burst_limit; 253 const char *conn_user; 254 struct chap *conn_chap; 255 }; 256 257 int parse_conf(struct conf *newconf, const char *path); 258 int uclparse_conf(struct conf *conf, const char *path); 259 260 struct conf *conf_new(void); 261 struct conf *conf_new_from_kernel(struct kports *kports); 262 void conf_delete(struct conf *conf); 263 int conf_verify(struct conf *conf); 264 265 struct auth_group *auth_group_new(struct conf *conf, const char *name); 266 void auth_group_delete(struct auth_group *ag); 267 struct auth_group *auth_group_find(const struct conf *conf, 268 const char *name); 269 int auth_group_set_type(struct auth_group *ag, 270 const char *type); 271 272 const struct auth *auth_new_chap(struct auth_group *ag, 273 const char *user, const char *secret); 274 const struct auth *auth_new_chap_mutual(struct auth_group *ag, 275 const char *user, const char *secret, 276 const char *user2, const char *secret2); 277 const struct auth *auth_find(const struct auth_group *ag, 278 const char *user); 279 280 const struct auth_name *auth_name_new(struct auth_group *ag, 281 const char *initiator_name); 282 bool auth_name_defined(const struct auth_group *ag); 283 const struct auth_name *auth_name_find(const struct auth_group *ag, 284 const char *initiator_name); 285 int auth_name_check(const struct auth_group *ag, 286 const char *initiator_name); 287 288 const struct auth_portal *auth_portal_new(struct auth_group *ag, 289 const char *initiator_portal); 290 bool auth_portal_defined(const struct auth_group *ag); 291 const struct auth_portal *auth_portal_find(const struct auth_group *ag, 292 const struct sockaddr_storage *sa); 293 int auth_portal_check(const struct auth_group *ag, 294 const struct sockaddr_storage *sa); 295 296 struct portal_group *portal_group_new(struct conf *conf, const char *name); 297 void portal_group_delete(struct portal_group *pg); 298 struct portal_group *portal_group_find(const struct conf *conf, 299 const char *name); 300 int portal_group_add_listen(struct portal_group *pg, 301 const char *listen, bool iser); 302 int portal_group_set_filter(struct portal_group *pg, 303 const char *filter); 304 int portal_group_set_offload(struct portal_group *pg, 305 const char *offload); 306 int portal_group_set_redirection(struct portal_group *pg, 307 const char *addr); 308 309 int isns_new(struct conf *conf, const char *addr); 310 void isns_delete(struct isns *is); 311 void isns_register(struct isns *isns, struct isns *oldisns); 312 void isns_check(struct isns *isns); 313 void isns_deregister(struct isns *isns); 314 315 struct pport *pport_new(struct kports *kports, const char *name, 316 uint32_t ctl_port); 317 struct pport *pport_find(const struct kports *kports, 318 const char *name); 319 struct pport *pport_copy(struct pport *pp, struct kports *kports); 320 void pport_delete(struct pport *pport); 321 322 struct port *port_new(struct conf *conf, struct target *target, 323 struct portal_group *pg); 324 struct port *port_new_ioctl(struct conf *conf, 325 struct kports *kports, struct target *target, 326 int pp, int vp); 327 struct port *port_new_pp(struct conf *conf, struct target *target, 328 struct pport *pp); 329 struct port *port_find(const struct conf *conf, const char *name); 330 struct port *port_find_in_pg(const struct portal_group *pg, 331 const char *target); 332 void port_delete(struct port *port); 333 int port_is_dummy(struct port *port); 334 335 struct target *target_new(struct conf *conf, const char *name); 336 void target_delete(struct target *target); 337 struct target *target_find(struct conf *conf, 338 const char *name); 339 int target_set_redirection(struct target *target, 340 const char *addr); 341 342 struct lun *lun_new(struct conf *conf, const char *name); 343 void lun_delete(struct lun *lun); 344 struct lun *lun_find(const struct conf *conf, const char *name); 345 void lun_set_backend(struct lun *lun, const char *value); 346 void lun_set_device_type(struct lun *lun, uint8_t value); 347 void lun_set_blocksize(struct lun *lun, size_t value); 348 void lun_set_device_id(struct lun *lun, const char *value); 349 void lun_set_path(struct lun *lun, const char *value); 350 void lun_set_scsiname(struct lun *lun, const char *value); 351 void lun_set_serial(struct lun *lun, const char *value); 352 void lun_set_size(struct lun *lun, size_t value); 353 void lun_set_ctl_lun(struct lun *lun, uint32_t value); 354 355 struct option *option_new(struct options *os, 356 const char *name, const char *value); 357 void option_delete(struct options *os, struct option *co); 358 struct option *option_find(const struct options *os, const char *name); 359 void option_set(struct option *o, const char *value); 360 361 void kernel_init(void); 362 int kernel_lun_add(struct lun *lun); 363 int kernel_lun_modify(struct lun *lun); 364 int kernel_lun_remove(struct lun *lun); 365 void kernel_handoff(struct ctld_connection *conn); 366 void kernel_limits(const char *offload, int s, 367 int *max_recv_data_segment_length, 368 int *max_send_data_segment_length, 369 int *max_burst_length, 370 int *first_burst_length); 371 int kernel_port_add(struct port *port); 372 int kernel_port_update(struct port *port, struct port *old); 373 int kernel_port_remove(struct port *port); 374 void kernel_capsicate(void); 375 376 #ifdef ICL_KERNEL_PROXY 377 void kernel_listen(struct addrinfo *ai, bool iser, 378 int portal_id); 379 void kernel_accept(int *connection_id, int *portal_id, 380 struct sockaddr *client_sa, 381 socklen_t *client_salen); 382 void kernel_send(struct pdu *pdu); 383 void kernel_receive(struct pdu *pdu); 384 #endif 385 386 void login(struct ctld_connection *conn); 387 388 void discovery(struct ctld_connection *conn); 389 390 bool valid_iscsi_name(const char *name); 391 void set_timeout(int timeout, int fatal); 392 393 #endif /* !CTLD_H */ 394