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_HH__ 32 #define __CTLD_HH__ 33 34 #include <sys/_nv.h> 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 <libiscsiutil.h> 42 #include <libutil.h> 43 44 #include <array> 45 #include <list> 46 #include <memory> 47 #include <string> 48 #include <string_view> 49 #include <unordered_map> 50 #include <unordered_set> 51 #include <libutil++.hh> 52 53 #define DEFAULT_CONFIG_PATH "/etc/ctl.conf" 54 #define DEFAULT_PIDFILE "/var/run/ctld.pid" 55 #define DEFAULT_BLOCKSIZE 512 56 #define DEFAULT_CD_BLOCKSIZE 2048 57 58 #define MAX_LUNS 1024 59 60 struct isns_req; 61 struct port; 62 63 struct auth { authauth64 auth(std::string_view secret) : a_secret(secret) {} authauth65 auth(std::string_view secret, std::string_view mutual_user, 66 std::string_view mutual_secret) : 67 a_secret(secret), a_mutual_user(mutual_user), 68 a_mutual_secret(mutual_secret) {} 69 mutualauth70 bool mutual() const { return !a_mutual_user.empty(); } 71 secretauth72 const char *secret() const { return a_secret.c_str(); } mutual_userauth73 const char *mutual_user() const { return a_mutual_user.c_str(); } mutual_secretauth74 const char *mutual_secret() const { return a_mutual_secret.c_str(); } 75 76 private: 77 std::string a_secret; 78 std::string a_mutual_user; 79 std::string a_mutual_secret; 80 }; 81 82 struct auth_portal { 83 bool matches(const struct sockaddr *sa) const; 84 bool parse(const char *portal); 85 86 private: 87 struct sockaddr_storage ap_sa; 88 int ap_mask = 0; 89 }; 90 91 enum class auth_type { 92 UNKNOWN, 93 DENY, 94 NO_AUTHENTICATION, 95 CHAP, 96 CHAP_MUTUAL 97 }; 98 99 struct auth_group { auth_groupauth_group100 auth_group(std::string label) : ag_label(label) {} 101 typeauth_group102 auth_type type() const { return ag_type; } 103 bool set_type(const char *str); 104 void set_type(auth_type type); 105 labelauth_group106 const char *label() const { return ag_label.c_str(); } 107 108 bool add_chap(const char *user, const char *secret); 109 bool add_chap_mutual(const char *user, const char *secret, 110 const char *user2, const char *secret2); 111 const struct auth *find_auth(std::string_view user) const; 112 113 bool add_host_nqn(std::string_view nqn); 114 bool host_permitted(std::string_view nqn) const; 115 116 bool add_host_address(const char *address); 117 bool host_permitted(const struct sockaddr *sa) const; 118 119 bool add_initiator_name(std::string_view initiator_name); 120 bool initiator_permitted(std::string_view initiator_name) const; 121 122 bool add_initiator_portal(const char *initiator_portal); 123 bool initiator_permitted(const struct sockaddr *sa) const; 124 125 private: 126 void check_secret_length(const char *user, const char *secret, 127 const char *secret_type); 128 129 std::string ag_label; 130 auth_type ag_type = auth_type::UNKNOWN; 131 std::unordered_map<std::string, auth> ag_auths; 132 std::unordered_set<std::string> ag_host_names; 133 std::list<auth_portal> ag_host_addresses; 134 std::unordered_set<std::string> ag_initiator_names; 135 std::list<auth_portal> ag_initiator_portals; 136 }; 137 138 using auth_group_sp = std::shared_ptr<auth_group>; 139 140 enum class portal_protocol { 141 ISCSI, 142 ISER, 143 NVME_TCP, 144 NVME_DISCOVERY_TCP, 145 }; 146 147 struct portal { portalportal148 portal(struct portal_group *pg, std::string_view listen, 149 portal_protocol protocol, freebsd::addrinfo_up ai) : 150 p_portal_group(pg), p_listen(listen), p_ai(std::move(ai)), 151 p_protocol(protocol) {} 152 virtual ~portal() = default; 153 154 bool reuse_socket(portal &oldp); 155 bool init_socket(); init_socket_optionsportal156 virtual bool init_socket_options(int s __unused) { return true; } 157 virtual void handle_connection(freebsd::fd_up fd, const char *host, 158 const struct sockaddr *client_sa) = 0; 159 portal_groupportal160 struct portal_group *portal_group() const { return p_portal_group; } listenportal161 const char *listen() const { return p_listen.c_str(); } aiportal162 const addrinfo *ai() const { return p_ai.get(); } protocolportal163 portal_protocol protocol() const { return p_protocol; } socketportal164 int socket() const { return p_socket; } closeportal165 void close() { p_socket.reset(); } 166 167 private: 168 struct portal_group *p_portal_group; 169 std::string p_listen; 170 freebsd::addrinfo_up p_ai; 171 portal_protocol p_protocol; 172 173 freebsd::fd_up p_socket; 174 }; 175 176 using portal_up = std::unique_ptr<portal>; 177 using port_up = std::unique_ptr<port>; 178 179 enum class discovery_filter { 180 UNKNOWN, 181 NONE, 182 PORTAL, 183 PORTAL_NAME, 184 PORTAL_NAME_AUTH 185 }; 186 187 struct portal_group { 188 portal_group(struct conf *conf, std::string_view name); 189 virtual ~portal_group() = default; 190 confportal_group191 struct conf *conf() const { return pg_conf; } 192 virtual const char *keyword() const = 0; nameportal_group193 const char *name() const { return pg_name.c_str(); } assignedportal_group194 bool assigned() const { return pg_assigned; } 195 bool is_dummy() const; is_redirectingportal_group196 bool is_redirecting() const { return !pg_redirection.empty(); } discovery_auth_groupportal_group197 struct auth_group *discovery_auth_group() const 198 { return pg_discovery_auth_group.get(); } discovery_filterportal_group199 enum discovery_filter discovery_filter() const 200 { return pg_discovery_filter; } dscpportal_group201 int dscp() const { return pg_dscp; } offloadportal_group202 const char *offload() const { return pg_offload.c_str(); } redirectionportal_group203 const char *redirection() const { return pg_redirection.c_str(); } pcpportal_group204 int pcp() const { return pg_pcp; } tagportal_group205 uint16_t tag() const { return pg_tag; } 206 207 freebsd::nvlist_up options() const; 208 portalsportal_group209 const std::list<portal_up> &portals() const { return pg_portals; } portsportal_group210 const std::unordered_map<std::string, port *> &ports() const 211 { return pg_ports; } 212 213 virtual void allocate_tag() = 0; 214 virtual bool add_portal(const char *value, 215 portal_protocol protocol) = 0; 216 virtual void add_default_portals() = 0; 217 bool add_option(const char *name, const char *value); 218 bool set_discovery_auth_group(const char *name); 219 bool set_dscp(u_int dscp); 220 virtual bool set_filter(const char *str) = 0; 221 void set_foreign(); 222 bool set_offload(const char *offload); 223 bool set_pcp(u_int pcp); 224 bool set_redirection(const char *addr); 225 void set_tag(uint16_t tag); 226 227 virtual port_up create_port(struct target *target, auth_group_sp ag) = 228 0; 229 virtual port_up create_port(struct target *target, uint32_t ctl_port) = 230 0; 231 232 void add_port(struct portal_group_port *port); 233 const struct port *find_port(std::string_view target) const; 234 void remove_port(struct portal_group_port *port); 235 void verify(struct conf *conf); 236 237 bool reuse_socket(struct portal &newp); 238 int open_sockets(struct conf &oldconf); 239 void close_sockets(); 240 241 protected: 242 struct conf *pg_conf; 243 freebsd::nvlist_up pg_options; 244 const char *pg_keyword; 245 std::string pg_name; 246 auth_group_sp pg_discovery_auth_group; 247 enum discovery_filter pg_discovery_filter = 248 discovery_filter::UNKNOWN; 249 bool pg_foreign = false; 250 bool pg_assigned = false; 251 std::list<portal_up> pg_portals; 252 std::unordered_map<std::string, port *> pg_ports; 253 std::string pg_offload; 254 std::string pg_redirection; 255 int pg_dscp = -1; 256 int pg_pcp = -1; 257 258 uint16_t pg_tag = 0; 259 }; 260 261 using portal_group_up = std::unique_ptr<portal_group>; 262 263 struct port { 264 port(struct target *target); 265 virtual ~port() = default; 266 targetport267 struct target *target() const { return p_target; } auth_groupport268 virtual struct auth_group *auth_group() const { return nullptr; } portal_groupport269 virtual struct portal_group *portal_group() const { return nullptr; } 270 is_dummyport271 virtual bool is_dummy() const { return true; } 272 273 virtual void clear_references(); 274 275 bool kernel_add(); 276 bool kernel_update(const port *oport); 277 bool kernel_remove(); 278 279 virtual bool kernel_create_port() = 0; 280 virtual bool kernel_remove_port() = 0; 281 282 protected: 283 struct target *p_target; 284 285 uint32_t p_ctl_port = 0; 286 }; 287 288 struct portal_group_port : public port { 289 portal_group_port(struct target *target, struct portal_group *pg, 290 auth_group_sp ag); 291 portal_group_port(struct target *target, struct portal_group *pg, 292 uint32_t ctl_port); 293 ~portal_group_port() override = default; 294 auth_groupportal_group_port295 struct auth_group *auth_group() const override 296 { return p_auth_group.get(); } portal_groupportal_group_port297 struct portal_group *portal_group() const override 298 { return p_portal_group; } 299 300 bool is_dummy() const override; 301 302 void clear_references() override; 303 304 protected: 305 auth_group_sp p_auth_group; 306 struct portal_group *p_portal_group; 307 }; 308 309 struct ioctl_port final : public port { ioctl_portioctl_port310 ioctl_port(struct target *target, int pp, int vp) : 311 port(target), p_ioctl_pp(pp), p_ioctl_vp(vp) {} 312 ~ioctl_port() override = default; 313 314 bool kernel_create_port() override; 315 bool kernel_remove_port() override; 316 317 private: 318 int p_ioctl_pp; 319 int p_ioctl_vp; 320 }; 321 322 struct kernel_port final : public port { kernel_portkernel_port323 kernel_port(struct target *target, struct pport *pp) : 324 port(target), p_pport(pp) {} 325 ~kernel_port() override = default; 326 327 bool kernel_create_port() override; 328 bool kernel_remove_port() override; 329 330 private: 331 struct pport *p_pport; 332 }; 333 334 struct lun { 335 lun(struct conf *conf, std::string_view name); 336 namelun337 const char *name() const { return l_name.c_str(); } pathlun338 const std::string &path() const { return l_path; } ctl_lunlun339 int ctl_lun() const { return l_ctl_lun; } 340 341 freebsd::nvlist_up options() const; 342 343 bool add_option(const char *name, const char *value); 344 bool set_backend(std::string_view value); 345 bool set_blocksize(size_t value); 346 bool set_ctl_lun(uint32_t value); 347 bool set_device_type(uint8_t device_type); 348 bool set_device_type(const char *value); 349 bool set_device_id(std::string_view value); 350 bool set_path(std::string_view value); 351 void set_scsiname(std::string_view value); 352 bool set_serial(std::string_view value); 353 bool set_size(uint64_t value); 354 355 bool changed(const struct lun &old) const; 356 bool verify(); 357 358 bool kernel_add(); 359 bool kernel_modify() const; 360 bool kernel_remove() const; 361 362 private: 363 struct conf *l_conf; 364 freebsd::nvlist_up l_options; 365 std::string l_name; 366 std::string l_backend; 367 uint8_t l_device_type = 0; 368 int l_blocksize = 0; 369 std::string l_device_id; 370 std::string l_path; 371 std::string l_scsiname; 372 std::string l_serial; 373 uint64_t l_size = 0; 374 375 int l_ctl_lun = -1; 376 }; 377 378 struct target { 379 target(struct conf *conf, const char *keyword, std::string_view name); 380 virtual ~target() = default; 381 has_aliastarget382 bool has_alias() const { return !t_alias.empty(); } has_pporttarget383 bool has_pport() const { return !t_pport.empty(); } has_redirectiontarget384 bool has_redirection() const { return !t_redirection.empty(); } aliastarget385 const char *alias() const { return t_alias.c_str(); } nametarget386 const char *name() const { return t_name.c_str(); } labeltarget387 const char *label() const { return t_label.c_str(); } pporttarget388 const char *pport() const { return t_pport.c_str(); } private_authtarget389 bool private_auth() const { return t_private_auth; } redirectiontarget390 const char *redirection() const { return t_redirection.c_str(); } 391 auth_grouptarget392 struct auth_group *auth_group() const { return t_auth_group.get(); } portstarget393 const std::list<port *> &ports() const { return t_ports; } luntarget394 const struct lun *lun(int idx) const { return t_luns[idx]; } 395 396 bool add_chap(const char *user, const char *secret); 397 bool add_chap_mutual(const char *user, const char *secret, 398 const char *user2, const char *secret2); add_host_addresstarget399 virtual bool add_host_address(const char *) { return false; } add_host_nqntarget400 virtual bool add_host_nqn(std::string_view) { return false; } add_initiator_nametarget401 virtual bool add_initiator_name(std::string_view) { return false; } add_initiator_portaltarget402 virtual bool add_initiator_portal(const char *) { return false; } add_luntarget403 virtual bool add_lun(u_int, const char *) { return false; } add_namespacetarget404 virtual bool add_namespace(u_int, const char *) { return false; } 405 virtual bool add_portal_group(const char *pg_name, 406 const char *ag_name) = 0; 407 bool set_alias(std::string_view alias); 408 bool set_auth_group(const char *ag_name); 409 bool set_auth_type(const char *type); 410 bool set_physical_port(std::string_view pport); 411 bool set_redirection(const char *addr); start_luntarget412 virtual struct lun *start_lun(u_int) { return nullptr; } start_namespacetarget413 virtual struct lun *start_namespace(u_int) { return nullptr; } 414 415 void add_port(struct port *port); 416 void remove_lun(struct lun *lun); 417 void remove_port(struct port *port); 418 void verify(); 419 420 protected: 421 bool use_private_auth(const char *keyword); 422 bool add_lun(u_int id, const char *lun_label, const char *lun_name); 423 struct lun *start_lun(u_int id, const char *lun_label, 424 const char *lun_name); 425 virtual struct portal_group *default_portal_group() = 0; 426 427 struct conf *t_conf; 428 std::array<struct lun *, MAX_LUNS> t_luns; 429 auth_group_sp t_auth_group; 430 std::list<port *> t_ports; 431 std::string t_name; 432 std::string t_label; 433 std::string t_alias; 434 std::string t_redirection; 435 /* Name of this target's physical port, if any, i.e. "isp0" */ 436 std::string t_pport; 437 bool t_private_auth; 438 }; 439 440 using target_up = std::unique_ptr<target>; 441 442 struct isns { isnsisns443 isns(std::string_view addr, freebsd::addrinfo_up ai) : 444 i_addr(addr), i_ai(std::move(ai)) {} 445 addrisns446 const char *addr() const { return i_addr.c_str(); } 447 448 freebsd::fd_up connect(); 449 bool send_request(int s, struct isns_req req); 450 451 private: 452 std::string i_addr; 453 freebsd::addrinfo_up i_ai; 454 }; 455 456 struct conf { 457 conf(); 458 maxprocconf459 int maxproc() const { return conf_maxproc; } timeoutconf460 int timeout() const { return conf_timeout; } genctrconf461 uint32_t genctr() const { return conf_genctr; } 462 default_auth_group_definedconf463 bool default_auth_group_defined() const 464 { return conf_default_ag_defined; } default_portal_group_definedconf465 bool default_portal_group_defined() const 466 { return conf_default_pg_defined; } default_transport_group_definedconf467 bool default_transport_group_defined() const 468 { return conf_default_tg_defined; } 469 470 struct auth_group *add_auth_group(const char *ag_name); 471 struct auth_group *define_default_auth_group(); 472 auth_group_sp find_auth_group(std::string_view ag_name); 473 474 struct portal_group *add_portal_group(const char *name); 475 struct portal_group *define_default_portal_group(); 476 struct portal_group *find_portal_group(std::string_view name); 477 478 struct portal_group *add_transport_group(const char *name); 479 struct portal_group *define_default_transport_group(); 480 struct portal_group *find_transport_group(std::string_view name); 481 482 bool add_port(struct target *target, struct portal_group *pg, 483 auth_group_sp ag); 484 bool add_port(struct target *target, struct portal_group *pg, 485 uint32_t ctl_port); 486 bool add_port(struct target *target, struct pport *pp); 487 bool add_port(struct kports &kports, struct target *target, int pp, 488 int vp); 489 bool add_pports(struct kports &kports); 490 491 struct target *add_controller(const char *name); 492 struct target *find_controller(std::string_view name); 493 494 struct target *add_target(const char *name); 495 struct target *find_target(std::string_view name); 496 497 struct lun *add_lun(const char *name); 498 struct lun *find_lun(std::string_view name); 499 500 void set_debug(int debug); 501 void set_isns_period(int period); 502 void set_isns_timeout(int timeout); 503 void set_maxproc(int maxproc); 504 bool set_pidfile_path(std::string_view path); 505 void set_timeout(int timeout); 506 507 void open_pidfile(); 508 void write_pidfile(); 509 void close_pidfile(); 510 511 bool add_isns(const char *addr); 512 void isns_register_targets(struct isns *isns, struct conf *oldconf); 513 void isns_deregister_targets(struct isns *isns); 514 void isns_schedule_update(); 515 void isns_update(); 516 517 int apply(struct conf *oldconf); 518 void delete_target_luns(struct lun *lun); 519 bool reuse_portal_group_socket(struct portal &newp); 520 bool verify(); 521 522 private: 523 struct isns_req isns_register_request(const char *hostname); 524 struct isns_req isns_check_request(const char *hostname); 525 struct isns_req isns_deregister_request(const char *hostname); 526 void isns_check(struct isns *isns); 527 528 std::string conf_pidfile_path; 529 std::unordered_map<std::string, std::unique_ptr<lun>> conf_luns; 530 std::unordered_map<std::string, target_up> conf_targets; 531 std::unordered_map<std::string, target_up> conf_controllers; 532 std::unordered_map<std::string, auth_group_sp> conf_auth_groups; 533 std::unordered_map<std::string, std::unique_ptr<port>> conf_ports; 534 std::unordered_map<std::string, portal_group_up> conf_portal_groups; 535 std::unordered_map<std::string, portal_group_up> conf_transport_groups; 536 std::unordered_map<std::string, isns> conf_isns; 537 struct target *conf_first_target = nullptr; 538 int conf_isns_period = 900; 539 int conf_isns_timeout = 5; 540 int conf_debug = 0; 541 int conf_timeout = 60; 542 int conf_maxproc = 30; 543 uint32_t conf_genctr = 0; 544 545 freebsd::pidfile conf_pidfile; 546 547 bool conf_default_pg_defined = false; 548 bool conf_default_tg_defined = false; 549 bool conf_default_ag_defined = false; 550 551 static uint32_t global_genctr; 552 553 #ifdef ICL_KERNEL_PROXY 554 public: 555 int add_proxy_portal(portal *); 556 portal *proxy_portal(int); 557 private: 558 std::vector<portal *> conf_proxy_portals; 559 #endif 560 }; 561 562 using conf_up = std::unique_ptr<conf>; 563 564 /* Physical ports exposed by the kernel */ 565 struct pport { pportpport566 pport(std::string_view name, uint32_t ctl_port) : pp_name(name), 567 pp_ctl_port(ctl_port) {} 568 namepport569 const char *name() const { return pp_name.c_str(); } ctl_portpport570 uint32_t ctl_port() const { return pp_ctl_port; } 571 linkedpport572 bool linked() const { return pp_linked; } linkpport573 void link() { pp_linked = true; } 574 575 private: 576 std::string pp_name; 577 uint32_t pp_ctl_port; 578 bool pp_linked; 579 }; 580 581 struct kports { 582 bool add_port(std::string &name, uint32_t ctl_port); 583 bool has_port(std::string_view name); 584 struct pport *find_port(std::string_view name); 585 586 private: 587 std::unordered_map<std::string, struct pport> pports; 588 }; 589 590 extern bool proxy_mode; 591 extern int ctl_fd; 592 593 bool parse_conf(const char *path); 594 bool uclparse_conf(const char *path); 595 596 conf_up conf_new_from_kernel(struct kports &kports); 597 void conf_finish(void); 598 void conf_start(struct conf *new_conf); 599 600 bool option_new(nvlist_t *nvl, 601 const char *name, const char *value); 602 603 freebsd::addrinfo_up parse_addr_port(const char *address, 604 const char *def_port); 605 606 void kernel_init(void); 607 void kernel_capsicate(void); 608 609 #ifdef ICL_KERNEL_PROXY 610 void kernel_listen(struct addrinfo *ai, bool iser, 611 int portal_id); 612 void kernel_accept(int *connection_id, int *portal_id, 613 struct sockaddr *client_sa, 614 socklen_t *client_salen); 615 void kernel_send(struct pdu *pdu); 616 void kernel_receive(struct pdu *pdu); 617 #endif 618 619 bool ctl_create_port(const char *driver, 620 const nvlist_t *nvl, uint32_t *ctl_port); 621 bool ctl_remove_port(const char *driver, nvlist_t *nvl); 622 623 portal_group_up iscsi_make_portal_group(struct conf *conf, 624 std::string_view name); 625 target_up iscsi_make_target(struct conf *conf, 626 std::string_view name); 627 628 portal_group_up nvmf_make_transport_group(struct conf *conf, 629 std::string_view name); 630 target_up nvmf_make_controller(struct conf *conf, 631 std::string_view name); 632 633 void start_timer(int timeout, bool fatal = false); 634 void stop_timer(); 635 bool timed_out(); 636 637 #endif /* !__CTLD_HH__ */ 638