2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
50 #include <iscsi_ioctl.h>
58 conf = calloc(1, sizeof(*conf));
62 TAILQ_INIT(&conf->conf_targets);
68 target_find(struct conf *conf, const char *nickname)
72 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
73 if (targ->t_nickname != NULL &&
74 strcasecmp(targ->t_nickname, nickname) == 0)
82 target_new(struct conf *conf)
86 targ = calloc(1, sizeof(*targ));
91 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
97 target_delete(struct target *targ)
100 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
105 default_initiator_name(void)
111 namelen = _POSIX_HOST_NAME_MAX + strlen(DEFAULT_IQN);
113 name = calloc(1, namelen + 1);
116 strcpy(name, DEFAULT_IQN);
117 error = gethostname(name + strlen(DEFAULT_IQN),
118 namelen - strlen(DEFAULT_IQN));
120 xo_err(1, "gethostname");
126 valid_hex(const char ch)
158 parse_enable(const char *enable)
161 return (ENABLE_UNSPECIFIED);
163 if (strcasecmp(enable, "on") == 0 ||
164 strcasecmp(enable, "yes") == 0)
167 if (strcasecmp(enable, "off") == 0 ||
168 strcasecmp(enable, "no") == 0)
171 return (ENABLE_UNSPECIFIED);
175 valid_iscsi_name(const char *name)
179 if (strlen(name) >= MAX_NAME_LEN) {
180 xo_warnx("overlong name for \"%s\"; max length allowed "
181 "by iSCSI specification is %d characters",
187 * In the cases below, we don't return an error, just in case the admin
188 * was right, and we're wrong.
190 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
191 for (i = strlen("iqn."); name[i] != '\0'; i++) {
193 * XXX: We should verify UTF-8 normalisation, as defined
194 * by 3.2.6.2: iSCSI Name Encoding.
196 if (isalnum(name[i]))
198 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
200 xo_warnx("invalid character \"%c\" in iSCSI name "
201 "\"%s\"; allowed characters are letters, digits, "
202 "'-', '.', and ':'", name[i], name);
206 * XXX: Check more stuff: valid date and a valid reversed domain.
208 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
209 if (strlen(name) != strlen("eui.") + 16)
210 xo_warnx("invalid iSCSI name \"%s\"; the \"eui.\" "
211 "should be followed by exactly 16 hexadecimal "
213 for (i = strlen("eui."); name[i] != '\0'; i++) {
214 if (!valid_hex(name[i])) {
215 xo_warnx("invalid character \"%c\" in iSCSI "
216 "name \"%s\"; allowed characters are 1-9 "
217 "and A-F", name[i], name);
221 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
222 if (strlen(name) > strlen("naa.") + 32)
223 xo_warnx("invalid iSCSI name \"%s\"; the \"naa.\" "
224 "should be followed by at most 32 hexadecimal "
226 for (i = strlen("naa."); name[i] != '\0'; i++) {
227 if (!valid_hex(name[i])) {
228 xo_warnx("invalid character \"%c\" in ISCSI "
229 "name \"%s\"; allowed characters are 1-9 "
230 "and A-F", name[i], name);
235 xo_warnx("invalid iSCSI name \"%s\"; should start with "
236 "either \".iqn\", \"eui.\", or \"naa.\"",
243 conf_verify(struct conf *conf)
247 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
248 assert(targ->t_nickname != NULL);
249 if (targ->t_session_type == SESSION_TYPE_UNSPECIFIED)
250 targ->t_session_type = SESSION_TYPE_NORMAL;
251 if (targ->t_session_type == SESSION_TYPE_NORMAL &&
252 targ->t_name == NULL)
253 xo_errx(1, "missing TargetName for target \"%s\"",
255 if (targ->t_session_type == SESSION_TYPE_DISCOVERY &&
256 targ->t_name != NULL)
257 xo_errx(1, "cannot specify TargetName for discovery "
258 "sessions for target \"%s\"", targ->t_nickname);
259 if (targ->t_name != NULL) {
260 if (valid_iscsi_name(targ->t_name) == false)
261 xo_errx(1, "invalid target name \"%s\"",
264 if (targ->t_protocol == PROTOCOL_UNSPECIFIED)
265 targ->t_protocol = PROTOCOL_ISCSI;
266 if (targ->t_address == NULL)
267 xo_errx(1, "missing TargetAddress for target \"%s\"",
269 if (targ->t_initiator_name == NULL)
270 targ->t_initiator_name = default_initiator_name();
271 if (valid_iscsi_name(targ->t_initiator_name) == false)
272 xo_errx(1, "invalid initiator name \"%s\"",
273 targ->t_initiator_name);
274 if (targ->t_header_digest == DIGEST_UNSPECIFIED)
275 targ->t_header_digest = DIGEST_NONE;
276 if (targ->t_data_digest == DIGEST_UNSPECIFIED)
277 targ->t_data_digest = DIGEST_NONE;
278 if (targ->t_auth_method == AUTH_METHOD_UNSPECIFIED) {
279 if (targ->t_user != NULL || targ->t_secret != NULL ||
280 targ->t_mutual_user != NULL ||
281 targ->t_mutual_secret != NULL)
282 targ->t_auth_method =
285 targ->t_auth_method =
288 if (targ->t_auth_method == AUTH_METHOD_CHAP) {
289 if (targ->t_user == NULL) {
290 xo_errx(1, "missing chapIName for target \"%s\"",
293 if (targ->t_secret == NULL)
294 xo_errx(1, "missing chapSecret for target \"%s\"",
296 if (targ->t_mutual_user != NULL ||
297 targ->t_mutual_secret != NULL) {
298 if (targ->t_mutual_user == NULL)
299 xo_errx(1, "missing tgtChapName for "
300 "target \"%s\"", targ->t_nickname);
301 if (targ->t_mutual_secret == NULL)
302 xo_errx(1, "missing tgtChapSecret for "
303 "target \"%s\"", targ->t_nickname);
310 conf_from_target(struct iscsi_session_conf *conf,
311 const struct target *targ)
313 memset(conf, 0, sizeof(*conf));
316 * XXX: Check bounds and return error instead of silently truncating.
318 if (targ->t_initiator_name != NULL)
319 strlcpy(conf->isc_initiator, targ->t_initiator_name,
320 sizeof(conf->isc_initiator));
321 if (targ->t_initiator_address != NULL)
322 strlcpy(conf->isc_initiator_addr, targ->t_initiator_address,
323 sizeof(conf->isc_initiator_addr));
324 if (targ->t_initiator_alias != NULL)
325 strlcpy(conf->isc_initiator_alias, targ->t_initiator_alias,
326 sizeof(conf->isc_initiator_alias));
327 if (targ->t_name != NULL)
328 strlcpy(conf->isc_target, targ->t_name,
329 sizeof(conf->isc_target));
330 if (targ->t_address != NULL)
331 strlcpy(conf->isc_target_addr, targ->t_address,
332 sizeof(conf->isc_target_addr));
333 if (targ->t_user != NULL)
334 strlcpy(conf->isc_user, targ->t_user,
335 sizeof(conf->isc_user));
336 if (targ->t_secret != NULL)
337 strlcpy(conf->isc_secret, targ->t_secret,
338 sizeof(conf->isc_secret));
339 if (targ->t_mutual_user != NULL)
340 strlcpy(conf->isc_mutual_user, targ->t_mutual_user,
341 sizeof(conf->isc_mutual_user));
342 if (targ->t_mutual_secret != NULL)
343 strlcpy(conf->isc_mutual_secret, targ->t_mutual_secret,
344 sizeof(conf->isc_mutual_secret));
345 if (targ->t_session_type == SESSION_TYPE_DISCOVERY)
346 conf->isc_discovery = 1;
347 if (targ->t_enable != ENABLE_OFF)
348 conf->isc_enable = 1;
349 if (targ->t_protocol == PROTOCOL_ISER)
351 if (targ->t_offload != NULL)
352 strlcpy(conf->isc_offload, targ->t_offload,
353 sizeof(conf->isc_offload));
354 if (targ->t_header_digest == DIGEST_CRC32C)
355 conf->isc_header_digest = ISCSI_DIGEST_CRC32C;
357 conf->isc_header_digest = ISCSI_DIGEST_NONE;
358 if (targ->t_data_digest == DIGEST_CRC32C)
359 conf->isc_data_digest = ISCSI_DIGEST_CRC32C;
361 conf->isc_data_digest = ISCSI_DIGEST_NONE;
362 conf->isc_dscp = targ->t_dscp;
366 kernel_add(int iscsi_fd, const struct target *targ)
368 struct iscsi_session_add isa;
371 memset(&isa, 0, sizeof(isa));
372 conf_from_target(&isa.isa_conf, targ);
373 error = ioctl(iscsi_fd, ISCSISADD, &isa);
375 xo_warn("ISCSISADD");
380 kernel_modify(int iscsi_fd, unsigned int session_id, const struct target *targ)
382 struct iscsi_session_modify ism;
385 memset(&ism, 0, sizeof(ism));
386 ism.ism_session_id = session_id;
387 conf_from_target(&ism.ism_conf, targ);
388 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
390 xo_warn("ISCSISMODIFY");
395 kernel_modify_some(int iscsi_fd, unsigned int session_id, const char *target,
396 const char *target_addr, const char *user, const char *secret, int enable)
398 struct iscsi_session_state *states = NULL;
399 struct iscsi_session_state *state;
400 struct iscsi_session_conf *conf;
401 struct iscsi_session_list isl;
402 struct iscsi_session_modify ism;
403 unsigned int i, nentries = 1;
407 states = realloc(states,
408 nentries * sizeof(struct iscsi_session_state));
410 xo_err(1, "realloc");
412 memset(&isl, 0, sizeof(isl));
413 isl.isl_nentries = nentries;
414 isl.isl_pstates = states;
416 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
417 if (error != 0 && errno == EMSGSIZE) {
424 xo_errx(1, "ISCSISLIST");
426 for (i = 0; i < isl.isl_nentries; i++) {
429 if (state->iss_id == session_id)
432 if (i == isl.isl_nentries)
433 xo_errx(1, "session-id %u not found", session_id);
435 conf = &state->iss_conf;
438 strlcpy(conf->isc_target, target, sizeof(conf->isc_target));
439 if (target_addr != NULL)
440 strlcpy(conf->isc_target_addr, target_addr,
441 sizeof(conf->isc_target_addr));
443 strlcpy(conf->isc_user, user, sizeof(conf->isc_user));
445 strlcpy(conf->isc_secret, secret, sizeof(conf->isc_secret));
446 if (enable == ENABLE_ON)
447 conf->isc_enable = 1;
448 else if (enable == ENABLE_OFF)
449 conf->isc_enable = 0;
451 memset(&ism, 0, sizeof(ism));
452 ism.ism_session_id = session_id;
453 memcpy(&ism.ism_conf, conf, sizeof(ism.ism_conf));
454 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
456 xo_warn("ISCSISMODIFY");
460 kernel_remove(int iscsi_fd, const struct target *targ)
462 struct iscsi_session_remove isr;
465 memset(&isr, 0, sizeof(isr));
466 conf_from_target(&isr.isr_conf, targ);
467 error = ioctl(iscsi_fd, ISCSISREMOVE, &isr);
469 xo_warn("ISCSISREMOVE");
474 * XXX: Add filtering.
477 kernel_list(int iscsi_fd, const struct target *targ __unused,
480 struct iscsi_session_state *states = NULL;
481 const struct iscsi_session_state *state;
482 const struct iscsi_session_conf *conf;
483 struct iscsi_session_list isl;
484 unsigned int i, nentries = 1;
488 states = realloc(states,
489 nentries * sizeof(struct iscsi_session_state));
491 xo_err(1, "realloc");
493 memset(&isl, 0, sizeof(isl));
494 isl.isl_nentries = nentries;
495 isl.isl_pstates = states;
497 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
498 if (error != 0 && errno == EMSGSIZE) {
505 xo_warn("ISCSISLIST");
510 xo_open_list("session");
511 for (i = 0; i < isl.isl_nentries; i++) {
513 conf = &state->iss_conf;
515 xo_open_instance("session");
518 * Display-only modifier as this information
519 * is also present within the 'session' container
521 xo_emit("{L:/%-26s}{V:sessionId/%u}\n",
522 "Session ID:", state->iss_id);
524 xo_open_container("initiator");
525 xo_emit("{L:/%-26s}{V:name/%s}\n",
526 "Initiator name:", conf->isc_initiator);
527 xo_emit("{L:/%-26s}{V:portal/%s}\n",
528 "Initiator portal:", conf->isc_initiator_addr);
529 xo_emit("{L:/%-26s}{V:alias/%s}\n",
530 "Initiator alias:", conf->isc_initiator_alias);
531 xo_close_container("initiator");
533 xo_open_container("target");
534 xo_emit("{L:/%-26s}{V:name/%s}\n",
535 "Target name:", conf->isc_target);
536 xo_emit("{L:/%-26s}{V:portal/%s}\n",
537 "Target portal:", conf->isc_target_addr);
538 xo_emit("{L:/%-26s}{V:alias/%s}\n",
539 "Target alias:", state->iss_target_alias);
540 if (conf->isc_dscp != -1)
541 xo_emit("{L:/%-26s}{V:dscp/0x%02x}\n",
542 "Target DSCP:", conf->isc_dscp);
543 xo_close_container("target");
545 xo_open_container("auth");
546 xo_emit("{L:/%-26s}{V:user/%s}\n",
547 "User:", conf->isc_user);
548 xo_emit("{L:/%-26s}{V:secret/%s}\n",
549 "Secret:", conf->isc_secret);
550 xo_emit("{L:/%-26s}{V:mutualUser/%s}\n",
551 "Mutual user:", conf->isc_mutual_user);
552 xo_emit("{L:/%-26s}{V:mutualSecret/%s}\n",
553 "Mutual secret:", conf->isc_mutual_secret);
554 xo_close_container("auth");
556 xo_emit("{L:/%-26s}{V:type/%s}\n",
558 conf->isc_discovery ? "Discovery" : "Normal");
559 xo_emit("{L:/%-26s}{V:enable/%s}\n",
561 conf->isc_enable ? "Yes" : "No");
562 xo_emit("{L:/%-26s}{V:state/%s}\n",
564 state->iss_connected ? "Connected" : "Disconnected");
565 xo_emit("{L:/%-26s}{V:failureReason/%s}\n",
566 "Failure reason:", state->iss_reason);
567 xo_emit("{L:/%-26s}{V:headerDigest/%s}\n",
569 state->iss_header_digest == ISCSI_DIGEST_CRC32C ?
571 xo_emit("{L:/%-26s}{V:dataDigest/%s}\n",
573 state->iss_data_digest == ISCSI_DIGEST_CRC32C ?
575 xo_emit("{L:/%-26s}{V:recvDataSegmentLen/%d}\n",
576 "MaxRecvDataSegmentLength:",
577 state->iss_max_recv_data_segment_length);
578 xo_emit("{L:/%-26s}{V:sendDataSegmentLen/%d}\n",
579 "MaxSendDataSegmentLength:",
580 state->iss_max_send_data_segment_length);
581 xo_emit("{L:/%-26s}{V:maxBurstLen/%d}\n",
582 "MaxBurstLen:", state->iss_max_burst_length);
583 xo_emit("{L:/%-26s}{V:firstBurstLen/%d}\n",
584 "FirstBurstLen:", state->iss_first_burst_length);
585 xo_emit("{L:/%-26s}{V:immediateData/%s}\n",
586 "ImmediateData:", state->iss_immediate_data ? "Yes" : "No");
587 xo_emit("{L:/%-26s}{V:iSER/%s}\n",
588 "iSER (RDMA):", conf->isc_iser ? "Yes" : "No");
589 xo_emit("{L:/%-26s}{V:offloadDriver/%s}\n",
590 "Offload driver:", state->iss_offload);
591 xo_emit("{L:/%-26s}",
593 print_periphs(state->iss_id);
595 xo_close_instance("session");
597 xo_close_list("session");
599 xo_emit("{T:/%-36s} {T:/%-16s} {T:/%s}\n",
600 "Target name", "Target portal", "State");
602 if (isl.isl_nentries != 0)
603 xo_open_list("session");
604 for (i = 0; i < isl.isl_nentries; i++) {
607 conf = &state->iss_conf;
609 xo_open_instance("session");
610 xo_emit("{V:name/%-36s/%s} {V:portal/%-16s/%s} ",
611 conf->isc_target, conf->isc_target_addr);
613 if (state->iss_reason[0] != '\0' &&
614 conf->isc_enable != 0) {
615 xo_emit("{V:state/%s}\n", state->iss_reason);
617 if (conf->isc_discovery) {
618 xo_emit("{V:state}\n", "Discovery");
619 } else if (conf->isc_enable == 0) {
620 xo_emit("{V:state}\n", "Disabled");
621 } else if (state->iss_connected) {
622 xo_emit("{V:state}: ", "Connected");
623 print_periphs(state->iss_id);
626 xo_emit("{V:state}\n", "Disconnected");
629 xo_close_instance("session");
631 if (isl.isl_nentries != 0)
632 xo_close_list("session");
639 kernel_wait(int iscsi_fd, int timeout)
641 struct iscsi_session_state *states = NULL;
642 const struct iscsi_session_state *state;
643 struct iscsi_session_list isl;
644 unsigned int i, nentries = 1;
650 states = realloc(states,
651 nentries * sizeof(struct iscsi_session_state));
653 xo_err(1, "realloc");
655 memset(&isl, 0, sizeof(isl));
656 isl.isl_nentries = nentries;
657 isl.isl_pstates = states;
659 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
660 if (error != 0 && errno == EMSGSIZE) {
667 xo_warn("ISCSISLIST");
671 all_connected = true;
672 for (i = 0; i < isl.isl_nentries; i++) {
675 if (!state->iss_connected) {
676 all_connected = false;
698 fprintf(stderr, "usage: iscsictl -A -p portal -t target "
699 "[-u user -s secret] [-w timeout] [-e on | off]\n");
700 fprintf(stderr, " iscsictl -A -d discovery-host "
701 "[-u user -s secret] [-e on | off]\n");
702 fprintf(stderr, " iscsictl -A -a [-c path]\n");
703 fprintf(stderr, " iscsictl -A -n nickname [-c path]\n");
704 fprintf(stderr, " iscsictl -M -i session-id [-p portal] "
705 "[-t target] [-u user] [-s secret] [-e on | off]\n");
706 fprintf(stderr, " iscsictl -M -i session-id -n nickname "
708 fprintf(stderr, " iscsictl -R [-p portal] [-t target]\n");
709 fprintf(stderr, " iscsictl -R -a\n");
710 fprintf(stderr, " iscsictl -R -n nickname [-c path]\n");
711 fprintf(stderr, " iscsictl -L [-v] [-w timeout]\n");
716 main(int argc, char **argv)
718 int Aflag = 0, Mflag = 0, Rflag = 0, Lflag = 0, aflag = 0,
719 rflag = 0, vflag = 0;
720 const char *conf_path = DEFAULT_CONFIG_PATH;
721 char *nickname = NULL, *discovery_host = NULL, *portal = NULL,
722 *target = NULL, *user = NULL, *secret = NULL;
723 int timeout = -1, enable = ENABLE_UNSPECIFIED;
724 long long session_id = -1;
726 int ch, error, iscsi_fd, retval, saved_errno;
731 argc = xo_parse_args(argc, argv);
732 xo_open_container("iscsictl");
734 while ((ch = getopt(argc, argv, "AMRLac:d:e:i:n:p:rt:u:s:vw:")) != -1) {
755 discovery_host = optarg;
758 enable = parse_enable(optarg);
759 if (enable == ENABLE_UNSPECIFIED) {
760 xo_errx(1, "invalid argument to -e, "
761 "must be either \"on\" or \"off\"");
765 session_id = strtol(optarg, &end, 10);
766 if ((size_t)(end - optarg) != strlen(optarg))
767 xo_errx(1, "trailing characters after session-id");
769 xo_errx(1, "session-id cannot be negative");
770 if (session_id > UINT_MAX)
771 xo_errx(1, "session-id cannot be greater than %u",
796 timeout = strtol(optarg, &end, 10);
797 if ((size_t)(end - optarg) != strlen(optarg))
798 xo_errx(1, "trailing characters after timeout");
800 xo_errx(1, "timeout cannot be negative");
811 if (Aflag + Mflag + Rflag + Lflag == 0)
813 if (Aflag + Mflag + Rflag + Lflag > 1)
814 xo_errx(1, "at most one of -A, -M, -R, or -L may be specified");
817 * Note that we ignore unnecessary/inapplicable "-c" flag; so that
818 * people can do something like "alias ISCSICTL="iscsictl -c path"
823 if (enable != ENABLE_UNSPECIFIED)
824 xo_errx(1, "-a and -e are mutually exclusive");
826 xo_errx(1, "-a and -p are mutually exclusive");
828 xo_errx(1, "-a and -t are mutually exclusive");
830 xo_errx(1, "-a and -u are mutually exclusive");
832 xo_errx(1, "-a and -s are mutually exclusive");
833 if (nickname != NULL)
834 xo_errx(1, "-a and -n are mutually exclusive");
835 if (discovery_host != NULL)
836 xo_errx(1, "-a and -d are mutually exclusive");
838 xo_errx(1, "-a and -r are mutually exclusive");
839 } else if (nickname != NULL) {
840 if (enable != ENABLE_UNSPECIFIED)
841 xo_errx(1, "-n and -e are mutually exclusive");
843 xo_errx(1, "-n and -p are mutually exclusive");
845 xo_errx(1, "-n and -t are mutually exclusive");
847 xo_errx(1, "-n and -u are mutually exclusive");
849 xo_errx(1, "-n and -s are mutually exclusive");
850 if (discovery_host != NULL)
851 xo_errx(1, "-n and -d are mutually exclusive");
853 xo_errx(1, "-n and -r are mutually exclusive");
854 } else if (discovery_host != NULL) {
856 xo_errx(1, "-d and -p are mutually exclusive");
858 xo_errx(1, "-d and -t are mutually exclusive");
860 if (target == NULL && portal == NULL)
861 xo_errx(1, "must specify -a, -n or -t/-p");
863 if (target != NULL && portal == NULL)
864 xo_errx(1, "-t must always be used with -p");
865 if (portal != NULL && target == NULL)
866 xo_errx(1, "-p must always be used with -t");
869 if (user != NULL && secret == NULL)
870 xo_errx(1, "-u must always be used with -s");
871 if (secret != NULL && user == NULL)
872 xo_errx(1, "-s must always be used with -u");
874 if (session_id != -1)
875 xo_errx(1, "-i cannot be used with -A");
877 xo_errx(1, "-v cannot be used with -A");
879 } else if (Mflag != 0) {
880 if (session_id == -1)
881 xo_errx(1, "-M requires -i");
883 if (nickname != NULL) {
884 if (enable != ENABLE_UNSPECIFIED)
885 xo_errx(1, "-n and -e are mutually exclusive");
887 xo_errx(1, "-n and -p are mutually exclusive");
889 xo_errx(1, "-n and -t are mutually exclusive");
891 xo_errx(1, "-n and -u are mutually exclusive");
893 xo_errx(1, "-n and -s are mutually exclusive");
897 xo_errx(1, "-a cannot be used with -M");
898 if (discovery_host != NULL)
899 xo_errx(1, "-d cannot be used with -M");
901 xo_errx(1, "-r cannot be used with -M");
903 xo_errx(1, "-v cannot be used with -M");
905 xo_errx(1, "-w cannot be used with -M");
907 } else if (Rflag != 0) {
910 xo_errx(1, "-a and -p are mutually exclusive");
912 xo_errx(1, "-a and -t are mutually exclusive");
913 if (nickname != NULL)
914 xo_errx(1, "-a and -n are mutually exclusive");
915 } else if (nickname != NULL) {
917 xo_errx(1, "-n and -p are mutually exclusive");
919 xo_errx(1, "-n and -t are mutually exclusive");
920 } else if (target == NULL && portal == NULL) {
921 xo_errx(1, "must specify either -a, -n, -t, or -p");
924 if (discovery_host != NULL)
925 xo_errx(1, "-d cannot be used with -R");
926 if (enable != ENABLE_UNSPECIFIED)
927 xo_errx(1, "-e cannot be used with -R");
928 if (session_id != -1)
929 xo_errx(1, "-i cannot be used with -R");
931 xo_errx(1, "-r cannot be used with -R");
933 xo_errx(1, "-u cannot be used with -R");
935 xo_errx(1, "-s cannot be used with -R");
937 xo_errx(1, "-v cannot be used with -R");
939 xo_errx(1, "-w cannot be used with -R");
944 if (discovery_host != NULL)
945 xo_errx(1, "-d cannot be used with -L");
946 if (session_id != -1)
947 xo_errx(1, "-i cannot be used with -L");
948 if (nickname != NULL)
949 xo_errx(1, "-n cannot be used with -L");
951 xo_errx(1, "-p cannot be used with -L");
953 xo_errx(1, "-r cannot be used with -L");
955 xo_errx(1, "-t cannot be used with -L");
957 xo_errx(1, "-u cannot be used with -L");
959 xo_errx(1, "-s cannot be used with -L");
962 iscsi_fd = open(ISCSI_PATH, O_RDWR);
963 if (iscsi_fd < 0 && errno == ENOENT) {
965 retval = kldload("iscsi");
967 iscsi_fd = open(ISCSI_PATH, O_RDWR);
972 xo_err(1, "failed to open %s", ISCSI_PATH);
974 if (Aflag != 0 && aflag != 0) {
975 conf = conf_new_from_file(conf_path);
977 TAILQ_FOREACH(targ, &conf->conf_targets, t_next)
978 failed += kernel_add(iscsi_fd, targ);
979 } else if (nickname != NULL) {
980 conf = conf_new_from_file(conf_path);
981 targ = target_find(conf, nickname);
983 xo_errx(1, "target %s not found in %s",
984 nickname, conf_path);
987 failed += kernel_add(iscsi_fd, targ);
989 failed += kernel_modify(iscsi_fd, session_id, targ);
991 failed += kernel_remove(iscsi_fd, targ);
993 failed += kernel_list(iscsi_fd, targ, vflag);
994 } else if (Mflag != 0) {
995 kernel_modify_some(iscsi_fd, session_id, target, portal,
996 user, secret, enable);
998 if (Aflag != 0 && target != NULL) {
999 if (valid_iscsi_name(target) == false)
1000 xo_errx(1, "invalid target name \"%s\"", target);
1003 targ = target_new(conf);
1004 targ->t_initiator_name = default_initiator_name();
1005 targ->t_header_digest = DIGEST_NONE;
1006 targ->t_data_digest = DIGEST_NONE;
1007 targ->t_name = target;
1008 if (discovery_host != NULL) {
1009 targ->t_session_type = SESSION_TYPE_DISCOVERY;
1010 targ->t_address = discovery_host;
1012 targ->t_session_type = SESSION_TYPE_NORMAL;
1013 targ->t_address = portal;
1015 targ->t_enable = enable;
1017 targ->t_protocol = PROTOCOL_ISER;
1018 targ->t_user = user;
1019 targ->t_secret = secret;
1022 failed += kernel_add(iscsi_fd, targ);
1023 else if (Rflag != 0)
1024 failed += kernel_remove(iscsi_fd, targ);
1026 failed += kernel_list(iscsi_fd, targ, vflag);
1030 failed += kernel_wait(iscsi_fd, timeout);
1032 error = close(iscsi_fd);
1036 xo_close_container("iscsictl");