2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/ioctl.h>
36 #include <sys/param.h>
37 #include <sys/linker.h>
49 #include <iscsi_ioctl.h>
57 conf = calloc(1, sizeof(*conf));
61 TAILQ_INIT(&conf->conf_targets);
67 target_find(struct conf *conf, const char *nickname)
71 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
72 if (targ->t_nickname != NULL &&
73 strcasecmp(targ->t_nickname, nickname) == 0)
81 target_new(struct conf *conf)
85 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;
363 conf->isc_pcp = targ->t_pcp;
367 kernel_add(int iscsi_fd, const struct target *targ)
369 struct iscsi_session_add isa;
372 memset(&isa, 0, sizeof(isa));
373 conf_from_target(&isa.isa_conf, targ);
374 error = ioctl(iscsi_fd, ISCSISADD, &isa);
376 xo_warn("ISCSISADD");
381 kernel_modify(int iscsi_fd, unsigned int session_id, const struct target *targ)
383 struct iscsi_session_modify ism;
386 memset(&ism, 0, sizeof(ism));
387 ism.ism_session_id = session_id;
388 conf_from_target(&ism.ism_conf, targ);
389 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
391 xo_warn("ISCSISMODIFY");
396 kernel_modify_some(int iscsi_fd, unsigned int session_id, const char *target,
397 const char *target_addr, const char *user, const char *secret, int enable)
399 struct iscsi_session_state *states = NULL;
400 struct iscsi_session_state *state;
401 struct iscsi_session_conf *conf;
402 struct iscsi_session_list isl;
403 struct iscsi_session_modify ism;
404 unsigned int i, nentries = 1;
408 states = realloc(states,
409 nentries * sizeof(struct iscsi_session_state));
411 xo_err(1, "realloc");
413 memset(&isl, 0, sizeof(isl));
414 isl.isl_nentries = nentries;
415 isl.isl_pstates = states;
417 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
418 if (error != 0 && errno == EMSGSIZE) {
425 xo_errx(1, "ISCSISLIST");
427 for (i = 0; i < isl.isl_nentries; i++) {
430 if (state->iss_id == session_id)
433 if (i == isl.isl_nentries)
434 xo_errx(1, "session-id %u not found", session_id);
436 conf = &state->iss_conf;
439 strlcpy(conf->isc_target, target, sizeof(conf->isc_target));
440 if (target_addr != NULL)
441 strlcpy(conf->isc_target_addr, target_addr,
442 sizeof(conf->isc_target_addr));
444 strlcpy(conf->isc_user, user, sizeof(conf->isc_user));
446 strlcpy(conf->isc_secret, secret, sizeof(conf->isc_secret));
447 if (enable == ENABLE_ON)
448 conf->isc_enable = 1;
449 else if (enable == ENABLE_OFF)
450 conf->isc_enable = 0;
452 memset(&ism, 0, sizeof(ism));
453 ism.ism_session_id = session_id;
454 memcpy(&ism.ism_conf, conf, sizeof(ism.ism_conf));
455 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
457 xo_warn("ISCSISMODIFY");
461 kernel_remove(int iscsi_fd, const struct target *targ)
463 struct iscsi_session_remove isr;
466 memset(&isr, 0, sizeof(isr));
467 conf_from_target(&isr.isr_conf, targ);
468 error = ioctl(iscsi_fd, ISCSISREMOVE, &isr);
470 xo_warn("ISCSISREMOVE");
475 * XXX: Add filtering.
478 kernel_list(int iscsi_fd, const struct target *targ __unused,
481 struct iscsi_session_state *states = NULL;
482 const struct iscsi_session_state *state;
483 const struct iscsi_session_conf *conf;
484 struct iscsi_session_list isl;
485 unsigned int i, nentries = 1;
489 states = realloc(states,
490 nentries * sizeof(struct iscsi_session_state));
492 xo_err(1, "realloc");
494 memset(&isl, 0, sizeof(isl));
495 isl.isl_nentries = nentries;
496 isl.isl_pstates = states;
498 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
499 if (error != 0 && errno == EMSGSIZE) {
506 xo_warn("ISCSISLIST");
511 xo_open_list("session");
512 for (i = 0; i < isl.isl_nentries; i++) {
514 conf = &state->iss_conf;
516 xo_open_instance("session");
519 * Display-only modifier as this information
520 * is also present within the 'session' container
522 xo_emit("{L:/%-26s}{V:sessionId/%u}\n",
523 "Session ID:", state->iss_id);
525 xo_open_container("initiator");
526 xo_emit("{L:/%-26s}{V:name/%s}\n",
527 "Initiator name:", conf->isc_initiator);
528 xo_emit("{L:/%-26s}{V:portal/%s}\n",
529 "Initiator portal:", conf->isc_initiator_addr);
530 xo_emit("{L:/%-26s}{V:alias/%s}\n",
531 "Initiator alias:", conf->isc_initiator_alias);
532 xo_close_container("initiator");
534 xo_open_container("target");
535 xo_emit("{L:/%-26s}{V:name/%s}\n",
536 "Target name:", conf->isc_target);
537 xo_emit("{L:/%-26s}{V:portal/%s}\n",
538 "Target portal:", conf->isc_target_addr);
539 xo_emit("{L:/%-26s}{V:alias/%s}\n",
540 "Target alias:", state->iss_target_alias);
541 if (conf->isc_dscp != -1)
542 xo_emit("{L:/%-26s}{V:dscp/0x%02x}\n",
543 "Target DSCP:", conf->isc_dscp);
544 if (conf->isc_pcp != -1)
545 xo_emit("{L:/%-26s}{V:pcp/0x%02x}\n",
546 "Target PCP:", conf->isc_pcp);
547 xo_close_container("target");
549 xo_open_container("auth");
550 xo_emit("{L:/%-26s}{V:user/%s}\n",
551 "User:", conf->isc_user);
552 xo_emit("{L:/%-26s}{V:secret/%s}\n",
553 "Secret:", conf->isc_secret);
554 xo_emit("{L:/%-26s}{V:mutualUser/%s}\n",
555 "Mutual user:", conf->isc_mutual_user);
556 xo_emit("{L:/%-26s}{V:mutualSecret/%s}\n",
557 "Mutual secret:", conf->isc_mutual_secret);
558 xo_close_container("auth");
560 xo_emit("{L:/%-26s}{V:type/%s}\n",
562 conf->isc_discovery ? "Discovery" : "Normal");
563 xo_emit("{L:/%-26s}{V:enable/%s}\n",
565 conf->isc_enable ? "Yes" : "No");
566 xo_emit("{L:/%-26s}{V:state/%s}\n",
568 state->iss_connected ? "Connected" : "Disconnected");
569 xo_emit("{L:/%-26s}{V:failureReason/%s}\n",
570 "Failure reason:", state->iss_reason);
571 xo_emit("{L:/%-26s}{V:headerDigest/%s}\n",
573 state->iss_header_digest == ISCSI_DIGEST_CRC32C ?
575 xo_emit("{L:/%-26s}{V:dataDigest/%s}\n",
577 state->iss_data_digest == ISCSI_DIGEST_CRC32C ?
579 xo_emit("{L:/%-26s}{V:recvDataSegmentLen/%d}\n",
580 "MaxRecvDataSegmentLength:",
581 state->iss_max_recv_data_segment_length);
582 xo_emit("{L:/%-26s}{V:sendDataSegmentLen/%d}\n",
583 "MaxSendDataSegmentLength:",
584 state->iss_max_send_data_segment_length);
585 xo_emit("{L:/%-26s}{V:maxBurstLen/%d}\n",
586 "MaxBurstLen:", state->iss_max_burst_length);
587 xo_emit("{L:/%-26s}{V:firstBurstLen/%d}\n",
588 "FirstBurstLen:", state->iss_first_burst_length);
589 xo_emit("{L:/%-26s}{V:immediateData/%s}\n",
590 "ImmediateData:", state->iss_immediate_data ? "Yes" : "No");
591 xo_emit("{L:/%-26s}{V:iSER/%s}\n",
592 "iSER (RDMA):", conf->isc_iser ? "Yes" : "No");
593 xo_emit("{L:/%-26s}{V:offloadDriver/%s}\n",
594 "Offload driver:", state->iss_offload);
595 xo_emit("{L:/%-26s}",
597 print_periphs(state->iss_id);
599 xo_close_instance("session");
601 xo_close_list("session");
603 xo_emit("{T:/%-36s} {T:/%-16s} {T:/%s}\n",
604 "Target name", "Target portal", "State");
606 if (isl.isl_nentries != 0)
607 xo_open_list("session");
608 for (i = 0; i < isl.isl_nentries; i++) {
611 conf = &state->iss_conf;
613 xo_open_instance("session");
614 xo_emit("{V:name/%-36s/%s} {V:portal/%-16s/%s} ",
615 conf->isc_target, conf->isc_target_addr);
617 if (state->iss_reason[0] != '\0' &&
618 conf->isc_enable != 0) {
619 xo_emit("{V:state/%s}\n", state->iss_reason);
621 if (conf->isc_discovery) {
622 xo_emit("{V:state}\n", "Discovery");
623 } else if (conf->isc_enable == 0) {
624 xo_emit("{V:state}\n", "Disabled");
625 } else if (state->iss_connected) {
626 xo_emit("{V:state}: ", "Connected");
627 print_periphs(state->iss_id);
630 xo_emit("{V:state}\n", "Disconnected");
633 xo_close_instance("session");
635 if (isl.isl_nentries != 0)
636 xo_close_list("session");
643 kernel_wait(int iscsi_fd, int timeout)
645 struct iscsi_session_state *states = NULL;
646 const struct iscsi_session_state *state;
647 struct iscsi_session_list isl;
648 unsigned int i, nentries = 1;
654 states = realloc(states,
655 nentries * sizeof(struct iscsi_session_state));
657 xo_err(1, "realloc");
659 memset(&isl, 0, sizeof(isl));
660 isl.isl_nentries = nentries;
661 isl.isl_pstates = states;
663 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
664 if (error != 0 && errno == EMSGSIZE) {
671 xo_warn("ISCSISLIST");
675 all_connected = true;
676 for (i = 0; i < isl.isl_nentries; i++) {
679 if (!state->iss_connected) {
680 all_connected = false;
702 fprintf(stderr, "usage: iscsictl -A -p portal -t target "
703 "[-u user -s secret] [-w timeout] [-e on | off]\n");
704 fprintf(stderr, " iscsictl -A -d discovery-host "
705 "[-u user -s secret] [-e on | off]\n");
706 fprintf(stderr, " iscsictl -A -a [-c path]\n");
707 fprintf(stderr, " iscsictl -A -n nickname [-c path]\n");
708 fprintf(stderr, " iscsictl -M -i session-id [-p portal] "
709 "[-t target] [-u user] [-s secret] [-e on | off]\n");
710 fprintf(stderr, " iscsictl -M -i session-id -n nickname "
712 fprintf(stderr, " iscsictl -R [-p portal] [-t target]\n");
713 fprintf(stderr, " iscsictl -R -a\n");
714 fprintf(stderr, " iscsictl -R -n nickname [-c path]\n");
715 fprintf(stderr, " iscsictl -L [-v] [-w timeout]\n");
720 main(int argc, char **argv)
722 int Aflag = 0, Mflag = 0, Rflag = 0, Lflag = 0, aflag = 0,
723 rflag = 0, vflag = 0;
724 const char *conf_path = DEFAULT_CONFIG_PATH;
725 char *nickname = NULL, *discovery_host = NULL, *portal = NULL,
726 *target = NULL, *user = NULL, *secret = NULL;
727 int timeout = -1, enable = ENABLE_UNSPECIFIED;
728 long long session_id = -1;
730 int ch, error, iscsi_fd, retval, saved_errno;
735 argc = xo_parse_args(argc, argv);
736 xo_open_container("iscsictl");
738 while ((ch = getopt(argc, argv, "AMRLac:d:e:i:n:p:rt:u:s:vw:")) != -1) {
759 discovery_host = optarg;
762 enable = parse_enable(optarg);
763 if (enable == ENABLE_UNSPECIFIED) {
764 xo_errx(1, "invalid argument to -e, "
765 "must be either \"on\" or \"off\"");
769 session_id = strtol(optarg, &end, 10);
770 if ((size_t)(end - optarg) != strlen(optarg))
771 xo_errx(1, "trailing characters after session-id");
773 xo_errx(1, "session-id cannot be negative");
774 if (session_id > UINT_MAX)
775 xo_errx(1, "session-id cannot be greater than %u",
800 timeout = strtol(optarg, &end, 10);
801 if ((size_t)(end - optarg) != strlen(optarg))
802 xo_errx(1, "trailing characters after timeout");
804 xo_errx(1, "timeout cannot be negative");
815 if (Aflag + Mflag + Rflag + Lflag == 0)
817 if (Aflag + Mflag + Rflag + Lflag > 1)
818 xo_errx(1, "at most one of -A, -M, -R, or -L may be specified");
821 * Note that we ignore unnecessary/inapplicable "-c" flag; so that
822 * people can do something like "alias ISCSICTL="iscsictl -c path"
827 if (enable != ENABLE_UNSPECIFIED)
828 xo_errx(1, "-a and -e are mutually exclusive");
830 xo_errx(1, "-a and -p are mutually exclusive");
832 xo_errx(1, "-a and -t are mutually exclusive");
834 xo_errx(1, "-a and -u are mutually exclusive");
836 xo_errx(1, "-a and -s are mutually exclusive");
837 if (nickname != NULL)
838 xo_errx(1, "-a and -n are mutually exclusive");
839 if (discovery_host != NULL)
840 xo_errx(1, "-a and -d are mutually exclusive");
842 xo_errx(1, "-a and -r are mutually exclusive");
843 } else if (nickname != NULL) {
844 if (enable != ENABLE_UNSPECIFIED)
845 xo_errx(1, "-n and -e are mutually exclusive");
847 xo_errx(1, "-n and -p are mutually exclusive");
849 xo_errx(1, "-n and -t are mutually exclusive");
851 xo_errx(1, "-n and -u are mutually exclusive");
853 xo_errx(1, "-n and -s are mutually exclusive");
854 if (discovery_host != NULL)
855 xo_errx(1, "-n and -d are mutually exclusive");
857 xo_errx(1, "-n and -r are mutually exclusive");
858 } else if (discovery_host != NULL) {
860 xo_errx(1, "-d and -p are mutually exclusive");
862 xo_errx(1, "-d and -t are mutually exclusive");
864 if (target == NULL && portal == NULL)
865 xo_errx(1, "must specify -a, -n or -t/-p");
867 if (target != NULL && portal == NULL)
868 xo_errx(1, "-t must always be used with -p");
869 if (portal != NULL && target == NULL)
870 xo_errx(1, "-p must always be used with -t");
873 if (user != NULL && secret == NULL)
874 xo_errx(1, "-u must always be used with -s");
875 if (secret != NULL && user == NULL)
876 xo_errx(1, "-s must always be used with -u");
878 if (session_id != -1)
879 xo_errx(1, "-i cannot be used with -A");
881 xo_errx(1, "-v cannot be used with -A");
883 } else if (Mflag != 0) {
884 if (session_id == -1)
885 xo_errx(1, "-M requires -i");
887 if (nickname != NULL) {
888 if (enable != ENABLE_UNSPECIFIED)
889 xo_errx(1, "-n and -e are mutually exclusive");
891 xo_errx(1, "-n and -p are mutually exclusive");
893 xo_errx(1, "-n and -t are mutually exclusive");
895 xo_errx(1, "-n and -u are mutually exclusive");
897 xo_errx(1, "-n and -s are mutually exclusive");
901 xo_errx(1, "-a cannot be used with -M");
902 if (discovery_host != NULL)
903 xo_errx(1, "-d cannot be used with -M");
905 xo_errx(1, "-r cannot be used with -M");
907 xo_errx(1, "-v cannot be used with -M");
909 xo_errx(1, "-w cannot be used with -M");
911 } else if (Rflag != 0) {
914 xo_errx(1, "-a and -p are mutually exclusive");
916 xo_errx(1, "-a and -t are mutually exclusive");
917 if (nickname != NULL)
918 xo_errx(1, "-a and -n are mutually exclusive");
919 } else if (nickname != NULL) {
921 xo_errx(1, "-n and -p are mutually exclusive");
923 xo_errx(1, "-n and -t are mutually exclusive");
924 } else if (target == NULL && portal == NULL) {
925 xo_errx(1, "must specify either -a, -n, -t, or -p");
928 if (discovery_host != NULL)
929 xo_errx(1, "-d cannot be used with -R");
930 if (enable != ENABLE_UNSPECIFIED)
931 xo_errx(1, "-e cannot be used with -R");
932 if (session_id != -1)
933 xo_errx(1, "-i cannot be used with -R");
935 xo_errx(1, "-r cannot be used with -R");
937 xo_errx(1, "-u cannot be used with -R");
939 xo_errx(1, "-s cannot be used with -R");
941 xo_errx(1, "-v cannot be used with -R");
943 xo_errx(1, "-w cannot be used with -R");
948 if (discovery_host != NULL)
949 xo_errx(1, "-d cannot be used with -L");
950 if (session_id != -1)
951 xo_errx(1, "-i cannot be used with -L");
952 if (nickname != NULL)
953 xo_errx(1, "-n cannot be used with -L");
955 xo_errx(1, "-p cannot be used with -L");
957 xo_errx(1, "-r cannot be used with -L");
959 xo_errx(1, "-t cannot be used with -L");
961 xo_errx(1, "-u cannot be used with -L");
963 xo_errx(1, "-s cannot be used with -L");
966 iscsi_fd = open(ISCSI_PATH, O_RDWR);
967 if (iscsi_fd < 0 && errno == ENOENT) {
969 retval = kldload("iscsi");
971 iscsi_fd = open(ISCSI_PATH, O_RDWR);
976 xo_err(1, "failed to open %s", ISCSI_PATH);
978 if (Aflag != 0 && aflag != 0) {
979 conf = conf_new_from_file(conf_path);
981 TAILQ_FOREACH(targ, &conf->conf_targets, t_next)
982 failed += kernel_add(iscsi_fd, targ);
983 } else if (nickname != NULL) {
984 conf = conf_new_from_file(conf_path);
985 targ = target_find(conf, nickname);
987 xo_errx(1, "target %s not found in %s",
988 nickname, conf_path);
991 failed += kernel_add(iscsi_fd, targ);
993 failed += kernel_modify(iscsi_fd, session_id, targ);
995 failed += kernel_remove(iscsi_fd, targ);
997 failed += kernel_list(iscsi_fd, targ, vflag);
998 } else if (Mflag != 0) {
999 kernel_modify_some(iscsi_fd, session_id, target, portal,
1000 user, secret, enable);
1002 if (Aflag != 0 && target != NULL) {
1003 if (valid_iscsi_name(target) == false)
1004 xo_errx(1, "invalid target name \"%s\"", target);
1007 targ = target_new(conf);
1008 targ->t_initiator_name = default_initiator_name();
1009 targ->t_header_digest = DIGEST_NONE;
1010 targ->t_data_digest = DIGEST_NONE;
1011 targ->t_name = target;
1012 if (discovery_host != NULL) {
1013 targ->t_session_type = SESSION_TYPE_DISCOVERY;
1014 targ->t_address = discovery_host;
1016 targ->t_session_type = SESSION_TYPE_NORMAL;
1017 targ->t_address = portal;
1019 targ->t_enable = enable;
1021 targ->t_protocol = PROTOCOL_ISER;
1022 targ->t_user = user;
1023 targ->t_secret = secret;
1026 failed += kernel_add(iscsi_fd, targ);
1027 else if (Rflag != 0)
1028 failed += kernel_remove(iscsi_fd, targ);
1030 failed += kernel_list(iscsi_fd, targ, vflag);
1034 failed += kernel_wait(iscsi_fd, timeout);
1036 error = close(iscsi_fd);
1040 xo_close_container("iscsictl");