From eb2d6e317709d0890f23c8597c2e492b7af7dcee Mon Sep 17 00:00:00 2001 From: mav Date: Thu, 24 Jul 2014 15:31:45 +0000 Subject: [PATCH] MFC r267613 (by trasz): Implement redirection handling in initiator. git-svn-id: svn://svn.freebsd.org/base/stable/10@269065 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/dev/iscsi/iscsi.c | 77 ++++++++++++++++++++++++++++++------- sys/dev/iscsi/iscsi_ioctl.h | 7 ++++ usr.sbin/iscsid/iscsid.c | 4 +- usr.sbin/iscsid/login.c | 60 +++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 15 deletions(-) diff --git a/sys/dev/iscsi/iscsi.c b/sys/dev/iscsi/iscsi.c index 188b3781d..d8685fded 100644 --- a/sys/dev/iscsi/iscsi.c +++ b/sys/dev/iscsi/iscsi.c @@ -1635,6 +1635,33 @@ iscsi_sanitize_session_conf(struct iscsi_session_conf *isc) isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0'; } +static bool +iscsi_valid_session_conf(const struct iscsi_session_conf *isc) +{ + + if (isc->isc_initiator[0] == '\0') { + ISCSI_DEBUG("empty isc_initiator"); + return (false); + } + + if (isc->isc_target_addr[0] == '\0') { + ISCSI_DEBUG("empty isc_target_addr"); + return (false); + } + + if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) { + ISCSI_DEBUG("non-empty isc_target for discovery session"); + return (false); + } + + if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) { + ISCSI_DEBUG("empty isc_target for non-discovery session"); + return (false); + } + + return (true); +} + static int iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) { @@ -1643,22 +1670,12 @@ iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) int error; iscsi_sanitize_session_conf(&isa->isa_conf); + if (iscsi_valid_session_conf(&isa->isa_conf) == false) + return (EINVAL); is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK); memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf)); - if (is->is_conf.isc_initiator[0] == '\0' || - is->is_conf.isc_target_addr[0] == '\0') { - free(is, M_ISCSI); - return (EINVAL); - } - - if ((is->is_conf.isc_discovery != 0 && is->is_conf.isc_target[0] != 0) || - (is->is_conf.isc_discovery == 0 && is->is_conf.isc_target[0] == 0)) { - free(is, M_ISCSI); - return (EINVAL); - } - sx_xlock(&sc->sc_lock); /* @@ -1818,7 +1835,38 @@ iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl) return (0); } - + +static int +iscsi_ioctl_session_modify(struct iscsi_softc *sc, + struct iscsi_session_modify *ism) +{ + struct iscsi_session *is; + + iscsi_sanitize_session_conf(&ism->ism_conf); + if (iscsi_valid_session_conf(&ism->ism_conf) == false) + return (EINVAL); + + sx_xlock(&sc->sc_lock); + TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { + ISCSI_SESSION_LOCK(is); + if (is->is_id == ism->ism_session_id) + break; + ISCSI_SESSION_UNLOCK(is); + } + if (is == NULL) { + sx_xunlock(&sc->sc_lock); + return (ESRCH); + } + sx_xunlock(&sc->sc_lock); + + memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf)); + ISCSI_SESSION_UNLOCK(is); + + iscsi_session_reconnect(is); + + return (0); +} + static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td) @@ -1857,6 +1905,9 @@ iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, case ISCSISLIST: return (iscsi_ioctl_session_list(sc, (struct iscsi_session_list *)arg)); + case ISCSISMODIFY: + return (iscsi_ioctl_session_modify(sc, + (struct iscsi_session_modify *)arg)); default: return (EINVAL); } diff --git a/sys/dev/iscsi/iscsi_ioctl.h b/sys/dev/iscsi/iscsi_ioctl.h index 4e06b8753..b7cb47d4f 100644 --- a/sys/dev/iscsi/iscsi_ioctl.h +++ b/sys/dev/iscsi/iscsi_ioctl.h @@ -202,8 +202,15 @@ struct iscsi_session_list { int isl_spare[4]; }; +struct iscsi_session_modify { + unsigned int ism_session_id; + struct iscsi_session_conf ism_conf; + int ism_spare[4]; +}; + #define ISCSISADD _IOW('I', 0x11, struct iscsi_session_add) #define ISCSISREMOVE _IOW('I', 0x12, struct iscsi_session_remove) #define ISCSISLIST _IOWR('I', 0x13, struct iscsi_session_list) +#define ISCSISMODIFY _IOWR('I', 0x14, struct iscsi_session_modify) #endif /* !ISCSI_IOCTL_H */ diff --git a/usr.sbin/iscsid/iscsid.c b/usr.sbin/iscsid/iscsid.c index 1693d30ea..1297ccdf4 100644 --- a/usr.sbin/iscsid/iscsid.c +++ b/usr.sbin/iscsid/iscsid.c @@ -306,10 +306,10 @@ capsicate(struct connection *conn) cap_rights_t rights; #ifdef ICL_KERNEL_PROXY const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, - ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE }; + ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; #else const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, - ISCSISREMOVE }; + ISCSISREMOVE, ISCSISMODIFY }; #endif cap_rights_init(&rights, CAP_IOCTL); diff --git a/usr.sbin/iscsid/login.c b/usr.sbin/iscsid/login.c index 81a965d12..0bdb40676 100644 --- a/usr.sbin/iscsid/login.c +++ b/usr.sbin/iscsid/login.c @@ -30,6 +30,7 @@ */ #include +#include #include #include #include @@ -158,6 +159,60 @@ login_target_error_str(int class, int detail) } } +static void +kernel_modify(const struct connection *conn, const char *target_address) +{ + struct iscsi_session_modify ism; + int error; + + memset(&ism, 0, sizeof(ism)); + ism.ism_session_id = conn->conn_session_id; + memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf)); + strlcpy(ism.ism_conf.isc_target_addr, target_address, + sizeof(ism.ism_conf.isc_target)); + error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism); + if (error != 0) { + log_err(1, "failed to redirect to %s: ISCSISMODIFY", + target_address); + } +} + +/* + * XXX: The way it works is suboptimal; what should happen is described + * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would + * be much more complicated: we would need to keep "dependencies" + * for sessions, so that, in case described in draft and using draft + * terminology, we would have three sessions: one for discovery, + * one for initial target portal, and one for redirect portal. + * This would allow us to "backtrack" on connection failure, + * as described in draft. + */ +static void +login_handle_redirection(struct connection *conn, struct pdu *response) +{ + struct iscsi_bhs_login_response *bhslr; + struct keys *response_keys; + const char *target_address; + + bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; + assert (bhslr->bhslr_status_class == 1); + + response_keys = keys_new(); + keys_load(response_keys, response); + + target_address = keys_find(response_keys, "TargetAddress"); + if (target_address == NULL) + log_errx(1, "received redirection without TargetAddress"); + if (target_address[0] == '\0') + log_errx(1, "received redirection with empty TargetAddress"); + if (strlen(target_address) >= + sizeof(conn->conn_conf.isc_target_addr) - 1) + log_errx(1, "received TargetAddress is too long"); + + log_debugx("received redirection to \"%s\"", target_address); + kernel_modify(conn, target_address); +} + static struct pdu * login_receive(struct connection *conn, bool initial) { @@ -183,6 +238,11 @@ login_receive(struct connection *conn, bool initial) if (bhslr->bhslr_version_active != 0x00) log_errx(1, "received Login PDU with unsupported " "Version-active 0x%x", bhslr->bhslr_version_active); + if (bhslr->bhslr_status_class == 1) { + login_handle_redirection(conn, response); + log_debugx("redirection handled; exiting"); + exit(0); + } if (bhslr->bhslr_status_class != 0) { errorstr = login_target_error_str(bhslr->bhslr_status_class, bhslr->bhslr_status_detail); -- 2.45.0