2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/ioctl.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
48 #include <iscsi_ioctl.h>
56 conf = calloc(1, sizeof(*conf));
60 TAILQ_INIT(&conf->conf_targets);
66 target_find(struct conf *conf, const char *nickname)
70 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
71 if (targ->t_nickname != NULL &&
72 strcasecmp(targ->t_nickname, nickname) == 0)
80 target_new(struct conf *conf)
84 targ = calloc(1, sizeof(*targ));
88 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
94 target_delete(struct target *targ)
97 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
102 default_initiator_name(void)
108 namelen = _POSIX_HOST_NAME_MAX + strlen(DEFAULT_IQN);
110 name = calloc(1, namelen + 1);
113 strcpy(name, DEFAULT_IQN);
114 error = gethostname(name + strlen(DEFAULT_IQN),
115 namelen - strlen(DEFAULT_IQN));
117 xo_err(1, "gethostname");
123 valid_hex(const char ch)
155 parse_enable(const char *enable)
158 return (ENABLE_UNSPECIFIED);
160 if (strcasecmp(enable, "on") == 0 ||
161 strcasecmp(enable, "yes") == 0)
164 if (strcasecmp(enable, "off") == 0 ||
165 strcasecmp(enable, "no") == 0)
168 return (ENABLE_UNSPECIFIED);
172 valid_iscsi_name(const char *name)
176 if (strlen(name) >= MAX_NAME_LEN) {
177 xo_warnx("overlong name for \"%s\"; max length allowed "
178 "by iSCSI specification is %d characters",
184 * In the cases below, we don't return an error, just in case the admin
185 * was right, and we're wrong.
187 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
188 for (i = strlen("iqn."); name[i] != '\0'; i++) {
190 * XXX: We should verify UTF-8 normalisation, as defined
191 * by 3.2.6.2: iSCSI Name Encoding.
193 if (isalnum(name[i]))
195 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
197 xo_warnx("invalid character \"%c\" in iSCSI name "
198 "\"%s\"; allowed characters are letters, digits, "
199 "'-', '.', and ':'", name[i], name);
203 * XXX: Check more stuff: valid date and a valid reversed domain.
205 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
206 if (strlen(name) != strlen("eui.") + 16)
207 xo_warnx("invalid iSCSI name \"%s\"; the \"eui.\" "
208 "should be followed by exactly 16 hexadecimal "
210 for (i = strlen("eui."); name[i] != '\0'; i++) {
211 if (!valid_hex(name[i])) {
212 xo_warnx("invalid character \"%c\" in iSCSI "
213 "name \"%s\"; allowed characters are 1-9 "
214 "and A-F", name[i], name);
218 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
219 if (strlen(name) > strlen("naa.") + 32)
220 xo_warnx("invalid iSCSI name \"%s\"; the \"naa.\" "
221 "should be followed by at most 32 hexadecimal "
223 for (i = strlen("naa."); name[i] != '\0'; i++) {
224 if (!valid_hex(name[i])) {
225 xo_warnx("invalid character \"%c\" in ISCSI "
226 "name \"%s\"; allowed characters are 1-9 "
227 "and A-F", name[i], name);
232 xo_warnx("invalid iSCSI name \"%s\"; should start with "
233 "either \".iqn\", \"eui.\", or \"naa.\"",
240 conf_verify(struct conf *conf)
244 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
245 assert(targ->t_nickname != NULL);
246 if (targ->t_session_type == SESSION_TYPE_UNSPECIFIED)
247 targ->t_session_type = SESSION_TYPE_NORMAL;
248 if (targ->t_session_type == SESSION_TYPE_NORMAL &&
249 targ->t_name == NULL)
250 xo_errx(1, "missing TargetName for target \"%s\"",
252 if (targ->t_session_type == SESSION_TYPE_DISCOVERY &&
253 targ->t_name != NULL)
254 xo_errx(1, "cannot specify TargetName for discovery "
255 "sessions for target \"%s\"", targ->t_nickname);
256 if (targ->t_name != NULL) {
257 if (valid_iscsi_name(targ->t_name) == false)
258 xo_errx(1, "invalid target name \"%s\"",
261 if (targ->t_protocol == PROTOCOL_UNSPECIFIED)
262 targ->t_protocol = PROTOCOL_ISCSI;
263 if (targ->t_address == NULL)
264 xo_errx(1, "missing TargetAddress for target \"%s\"",
266 if (targ->t_initiator_name == NULL)
267 targ->t_initiator_name = default_initiator_name();
268 if (valid_iscsi_name(targ->t_initiator_name) == false)
269 xo_errx(1, "invalid initiator name \"%s\"",
270 targ->t_initiator_name);
271 if (targ->t_header_digest == DIGEST_UNSPECIFIED)
272 targ->t_header_digest = DIGEST_NONE;
273 if (targ->t_data_digest == DIGEST_UNSPECIFIED)
274 targ->t_data_digest = DIGEST_NONE;
275 if (targ->t_auth_method == AUTH_METHOD_UNSPECIFIED) {
276 if (targ->t_user != NULL || targ->t_secret != NULL ||
277 targ->t_mutual_user != NULL ||
278 targ->t_mutual_secret != NULL)
279 targ->t_auth_method =
282 targ->t_auth_method =
285 if (targ->t_auth_method == AUTH_METHOD_CHAP) {
286 if (targ->t_user == NULL) {
287 xo_errx(1, "missing chapIName for target \"%s\"",
290 if (targ->t_secret == NULL)
291 xo_errx(1, "missing chapSecret for target \"%s\"",
293 if (targ->t_mutual_user != NULL ||
294 targ->t_mutual_secret != NULL) {
295 if (targ->t_mutual_user == NULL)
296 xo_errx(1, "missing tgtChapName for "
297 "target \"%s\"", targ->t_nickname);
298 if (targ->t_mutual_secret == NULL)
299 xo_errx(1, "missing tgtChapSecret for "
300 "target \"%s\"", targ->t_nickname);
307 conf_from_target(struct iscsi_session_conf *conf,
308 const struct target *targ)
310 memset(conf, 0, sizeof(*conf));
313 * XXX: Check bounds and return error instead of silently truncating.
315 if (targ->t_initiator_name != NULL)
316 strlcpy(conf->isc_initiator, targ->t_initiator_name,
317 sizeof(conf->isc_initiator));
318 if (targ->t_initiator_address != NULL)
319 strlcpy(conf->isc_initiator_addr, targ->t_initiator_address,
320 sizeof(conf->isc_initiator_addr));
321 if (targ->t_initiator_alias != NULL)
322 strlcpy(conf->isc_initiator_alias, targ->t_initiator_alias,
323 sizeof(conf->isc_initiator_alias));
324 if (targ->t_name != NULL)
325 strlcpy(conf->isc_target, targ->t_name,
326 sizeof(conf->isc_target));
327 if (targ->t_address != NULL)
328 strlcpy(conf->isc_target_addr, targ->t_address,
329 sizeof(conf->isc_target_addr));
330 if (targ->t_user != NULL)
331 strlcpy(conf->isc_user, targ->t_user,
332 sizeof(conf->isc_user));
333 if (targ->t_secret != NULL)
334 strlcpy(conf->isc_secret, targ->t_secret,
335 sizeof(conf->isc_secret));
336 if (targ->t_mutual_user != NULL)
337 strlcpy(conf->isc_mutual_user, targ->t_mutual_user,
338 sizeof(conf->isc_mutual_user));
339 if (targ->t_mutual_secret != NULL)
340 strlcpy(conf->isc_mutual_secret, targ->t_mutual_secret,
341 sizeof(conf->isc_mutual_secret));
342 if (targ->t_session_type == SESSION_TYPE_DISCOVERY)
343 conf->isc_discovery = 1;
344 if (targ->t_enable != ENABLE_OFF)
345 conf->isc_enable = 1;
346 if (targ->t_protocol == PROTOCOL_ISER)
348 if (targ->t_offload != NULL)
349 strlcpy(conf->isc_offload, targ->t_offload,
350 sizeof(conf->isc_offload));
351 if (targ->t_header_digest == DIGEST_CRC32C)
352 conf->isc_header_digest = ISCSI_DIGEST_CRC32C;
354 conf->isc_header_digest = ISCSI_DIGEST_NONE;
355 if (targ->t_data_digest == DIGEST_CRC32C)
356 conf->isc_data_digest = ISCSI_DIGEST_CRC32C;
358 conf->isc_data_digest = ISCSI_DIGEST_NONE;
362 kernel_add(int iscsi_fd, const struct target *targ)
364 struct iscsi_session_add isa;
367 memset(&isa, 0, sizeof(isa));
368 conf_from_target(&isa.isa_conf, targ);
369 error = ioctl(iscsi_fd, ISCSISADD, &isa);
371 xo_warn("ISCSISADD");
376 kernel_modify(int iscsi_fd, unsigned int session_id, const struct target *targ)
378 struct iscsi_session_modify ism;
381 memset(&ism, 0, sizeof(ism));
382 ism.ism_session_id = session_id;
383 conf_from_target(&ism.ism_conf, targ);
384 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
386 xo_warn("ISCSISMODIFY");
391 kernel_modify_some(int iscsi_fd, unsigned int session_id, const char *target,
392 const char *target_addr, const char *user, const char *secret, int enable)
394 struct iscsi_session_state *states = NULL;
395 struct iscsi_session_state *state;
396 struct iscsi_session_conf *conf;
397 struct iscsi_session_list isl;
398 struct iscsi_session_modify ism;
399 unsigned int i, nentries = 1;
403 states = realloc(states,
404 nentries * sizeof(struct iscsi_session_state));
406 xo_err(1, "realloc");
408 memset(&isl, 0, sizeof(isl));
409 isl.isl_nentries = nentries;
410 isl.isl_pstates = states;
412 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
413 if (error != 0 && errno == EMSGSIZE) {
420 xo_errx(1, "ISCSISLIST");
422 for (i = 0; i < isl.isl_nentries; i++) {
425 if (state->iss_id == session_id)
428 if (i == isl.isl_nentries)
429 xo_errx(1, "session-id %u not found", session_id);
431 conf = &state->iss_conf;
434 strlcpy(conf->isc_target, target, sizeof(conf->isc_target));
435 if (target_addr != NULL)
436 strlcpy(conf->isc_target_addr, target_addr,
437 sizeof(conf->isc_target_addr));
439 strlcpy(conf->isc_user, user, sizeof(conf->isc_user));
441 strlcpy(conf->isc_secret, secret, sizeof(conf->isc_secret));
442 if (enable == ENABLE_ON)
443 conf->isc_enable = 1;
444 else if (enable == ENABLE_OFF)
445 conf->isc_enable = 0;
447 memset(&ism, 0, sizeof(ism));
448 ism.ism_session_id = session_id;
449 memcpy(&ism.ism_conf, conf, sizeof(ism.ism_conf));
450 error = ioctl(iscsi_fd, ISCSISMODIFY, &ism);
452 xo_warn("ISCSISMODIFY");
456 kernel_remove(int iscsi_fd, const struct target *targ)
458 struct iscsi_session_remove isr;
461 memset(&isr, 0, sizeof(isr));
462 conf_from_target(&isr.isr_conf, targ);
463 error = ioctl(iscsi_fd, ISCSISREMOVE, &isr);
465 xo_warn("ISCSISREMOVE");
470 * XXX: Add filtering.
473 kernel_list(int iscsi_fd, const struct target *targ __unused,
476 struct iscsi_session_state *states = NULL;
477 const struct iscsi_session_state *state;
478 const struct iscsi_session_conf *conf;
479 struct iscsi_session_list isl;
480 unsigned int i, nentries = 1;
484 states = realloc(states,
485 nentries * sizeof(struct iscsi_session_state));
487 xo_err(1, "realloc");
489 memset(&isl, 0, sizeof(isl));
490 isl.isl_nentries = nentries;
491 isl.isl_pstates = states;
493 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
494 if (error != 0 && errno == EMSGSIZE) {
501 xo_warn("ISCSISLIST");
506 xo_open_list("session");
507 for (i = 0; i < isl.isl_nentries; i++) {
509 conf = &state->iss_conf;
511 xo_open_instance("session");
514 * Display-only modifier as this information
515 * is also present within the 'session' container
517 xo_emit("{L:/%-18s}{V:sessionId/%u}\n",
518 "Session ID:", state->iss_id);
520 xo_open_container("initiator");
521 xo_emit("{L:/%-18s}{V:name/%s}\n",
522 "Initiator name:", conf->isc_initiator);
523 xo_emit("{L:/%-18s}{V:portal/%s}\n",
524 "Initiator portal:", conf->isc_initiator_addr);
525 xo_emit("{L:/%-18s}{V:alias/%s}\n",
526 "Initiator alias:", conf->isc_initiator_alias);
527 xo_close_container("initiator");
529 xo_open_container("target");
530 xo_emit("{L:/%-18s}{V:name/%s}\n",
531 "Target name:", conf->isc_target);
532 xo_emit("{L:/%-18s}{V:portal/%s}\n",
533 "Target portal:", conf->isc_target_addr);
534 xo_emit("{L:/%-18s}{V:alias/%s}\n",
535 "Target alias:", state->iss_target_alias);
536 xo_close_container("target");
538 xo_open_container("auth");
539 xo_emit("{L:/%-18s}{V:user/%s}\n",
540 "User:", conf->isc_user);
541 xo_emit("{L:/%-18s}{V:secret/%s}\n",
542 "Secret:", conf->isc_secret);
543 xo_emit("{L:/%-18s}{V:mutualUser/%s}\n",
544 "Mutual user:", conf->isc_mutual_user);
545 xo_emit("{L:/%-18s}{V:mutualSecret/%s}\n",
546 "Mutual secret:", conf->isc_mutual_secret);
547 xo_close_container("auth");
549 xo_emit("{L:/%-18s}{V:type/%s}\n",
551 conf->isc_discovery ? "Discovery" : "Normal");
552 xo_emit("{L:/%-18s}{V:enable/%s}\n",
554 conf->isc_enable ? "Yes" : "No");
555 xo_emit("{L:/%-18s}{V:state/%s}\n",
557 state->iss_connected ? "Connected" : "Disconnected");
558 xo_emit("{L:/%-18s}{V:failureReason/%s}\n",
559 "Failure reason:", state->iss_reason);
560 xo_emit("{L:/%-18s}{V:headerDigest/%s}\n",
562 state->iss_header_digest == ISCSI_DIGEST_CRC32C ?
564 xo_emit("{L:/%-18s}{V:dataDigest/%s}\n",
566 state->iss_data_digest == ISCSI_DIGEST_CRC32C ?
568 xo_emit("{L:/%-18s}{V:dataSegmentLen/%d}\n",
569 "DataSegmentLen:", state->iss_max_data_segment_length);
570 xo_emit("{L:/%-18s}{V:maxBurstLen/%d}\n",
571 "MaxBurstLen:", state->iss_max_burst_length);
572 xo_emit("{L:/%-18s}{V:firstBurstLen/%d}\n",
573 "FirstBurstLen:", state->iss_first_burst_length);
574 xo_emit("{L:/%-18s}{V:immediateData/%s}\n",
575 "ImmediateData:", state->iss_immediate_data ? "Yes" : "No");
576 xo_emit("{L:/%-18s}{V:iSER/%s}\n",
577 "iSER (RDMA):", conf->isc_iser ? "Yes" : "No");
578 xo_emit("{L:/%-18s}{V:offloadDriver/%s}\n",
579 "Offload driver:", state->iss_offload);
580 xo_emit("{L:/%-18s}",
582 print_periphs(state->iss_id);
584 xo_close_instance("session");
586 xo_close_list("session");
588 xo_emit("{T:/%-36s} {T:/%-16s} {T:/%s}\n",
589 "Target name", "Target portal", "State");
591 if (isl.isl_nentries != 0)
592 xo_open_list("session");
593 for (i = 0; i < isl.isl_nentries; i++) {
596 conf = &state->iss_conf;
598 xo_open_instance("session");
599 xo_emit("{V:name/%-36s/%s} {V:portal/%-16s/%s} ",
600 conf->isc_target, conf->isc_target_addr);
602 if (state->iss_reason[0] != '\0') {
603 xo_emit("{V:state/%s}\n", state->iss_reason);
605 if (conf->isc_discovery) {
606 xo_emit("{V:state}\n", "Discovery");
607 } else if (conf->isc_enable == 0) {
608 xo_emit("{V:state}\n", "Disabled");
609 } else if (state->iss_connected) {
610 xo_emit("{V:state}: ", "Connected");
611 print_periphs(state->iss_id);
614 xo_emit("{V:state}\n", "Disconnected");
617 xo_close_instance("session");
619 if (isl.isl_nentries != 0)
620 xo_close_list("session");
627 kernel_wait(int iscsi_fd, int timeout)
629 struct iscsi_session_state *states = NULL;
630 const struct iscsi_session_state *state;
631 struct iscsi_session_list isl;
632 unsigned int i, nentries = 1;
638 states = realloc(states,
639 nentries * sizeof(struct iscsi_session_state));
641 xo_err(1, "realloc");
643 memset(&isl, 0, sizeof(isl));
644 isl.isl_nentries = nentries;
645 isl.isl_pstates = states;
647 error = ioctl(iscsi_fd, ISCSISLIST, &isl);
648 if (error != 0 && errno == EMSGSIZE) {
655 xo_warn("ISCSISLIST");
659 all_connected = true;
660 for (i = 0; i < isl.isl_nentries; i++) {
663 if (!state->iss_connected) {
664 all_connected = false;
686 fprintf(stderr, "usage: iscsictl -A -p portal -t target "
687 "[-u user -s secret] [-w timeout] [-e on | off]\n");
688 fprintf(stderr, " iscsictl -A -d discovery-host "
689 "[-u user -s secret] [-e on | off]\n");
690 fprintf(stderr, " iscsictl -A -a [-c path]\n");
691 fprintf(stderr, " iscsictl -A -n nickname [-c path]\n");
692 fprintf(stderr, " iscsictl -M -i session-id [-p portal] "
693 "[-t target] [-u user] [-s secret] [-e on | off]\n");
694 fprintf(stderr, " iscsictl -M -i session-id -n nickname "
696 fprintf(stderr, " iscsictl -R [-p portal] [-t target]\n");
697 fprintf(stderr, " iscsictl -R -a\n");
698 fprintf(stderr, " iscsictl -R -n nickname [-c path]\n");
699 fprintf(stderr, " iscsictl -L [-v] [-w timeout]\n");
704 checked_strdup(const char *s)
715 main(int argc, char **argv)
717 int Aflag = 0, Mflag = 0, Rflag = 0, Lflag = 0, aflag = 0,
718 rflag = 0, vflag = 0;
719 const char *conf_path = DEFAULT_CONFIG_PATH;
720 char *nickname = NULL, *discovery_host = NULL, *portal = NULL,
721 *target = NULL, *user = NULL, *secret = NULL;
722 int timeout = -1, enable = ENABLE_UNSPECIFIED;
723 long long session_id = -1;
725 int ch, error, iscsi_fd, retval, saved_errno;
730 argc = xo_parse_args(argc, argv);
731 xo_open_container("iscsictl");
733 while ((ch = getopt(argc, argv, "AMRLac:d:e:i:n:p:rt:u:s:vw:")) != -1) {
754 discovery_host = optarg;
757 enable = parse_enable(optarg);
758 if (enable == ENABLE_UNSPECIFIED) {
759 xo_errx(1, "invalid argument to -e, "
760 "must be either \"on\" or \"off\"");
764 session_id = strtol(optarg, &end, 10);
765 if ((size_t)(end - optarg) != strlen(optarg))
766 xo_errx(1, "trailing characters after session-id");
768 xo_errx(1, "session-id cannot be negative");
769 if (session_id > UINT_MAX)
770 xo_errx(1, "session-id cannot be greater than %u",
795 timeout = strtol(optarg, &end, 10);
796 if ((size_t)(end - optarg) != strlen(optarg))
797 xo_errx(1, "trailing characters after timeout");
799 xo_errx(1, "timeout cannot be negative");
810 if (Aflag + Mflag + Rflag + Lflag == 0)
812 if (Aflag + Mflag + Rflag + Lflag > 1)
813 xo_errx(1, "at most one of -A, -M, -R, or -L may be specified");
816 * Note that we ignore unnecessary/inapplicable "-c" flag; so that
817 * people can do something like "alias ISCSICTL="iscsictl -c path"
822 if (enable != ENABLE_UNSPECIFIED)
823 xo_errx(1, "-a and -e and mutually exclusive");
825 xo_errx(1, "-a and -p and mutually exclusive");
827 xo_errx(1, "-a and -t and mutually exclusive");
829 xo_errx(1, "-a and -u and mutually exclusive");
831 xo_errx(1, "-a and -s and mutually exclusive");
832 if (nickname != NULL)
833 xo_errx(1, "-a and -n and mutually exclusive");
834 if (discovery_host != NULL)
835 xo_errx(1, "-a and -d and mutually exclusive");
837 xo_errx(1, "-a and -r and mutually exclusive");
838 } else if (nickname != NULL) {
839 if (enable != ENABLE_UNSPECIFIED)
840 xo_errx(1, "-n and -e and mutually exclusive");
842 xo_errx(1, "-n and -p and mutually exclusive");
844 xo_errx(1, "-n and -t and mutually exclusive");
846 xo_errx(1, "-n and -u and mutually exclusive");
848 xo_errx(1, "-n and -s and mutually exclusive");
849 if (discovery_host != NULL)
850 xo_errx(1, "-n and -d and mutually exclusive");
852 xo_errx(1, "-n and -r and mutually exclusive");
853 } else if (discovery_host != NULL) {
855 xo_errx(1, "-d and -p and mutually exclusive");
857 xo_errx(1, "-d and -t and mutually exclusive");
859 if (target == NULL && portal == NULL)
860 xo_errx(1, "must specify -a, -n or -t/-p");
862 if (target != NULL && portal == NULL)
863 xo_errx(1, "-t must always be used with -p");
864 if (portal != NULL && target == NULL)
865 xo_errx(1, "-p must always be used with -t");
868 if (user != NULL && secret == NULL)
869 xo_errx(1, "-u must always be used with -s");
870 if (secret != NULL && user == NULL)
871 xo_errx(1, "-s must always be used with -u");
873 if (session_id != -1)
874 xo_errx(1, "-i cannot be used with -A");
876 xo_errx(1, "-v cannot be used with -A");
878 } else if (Mflag != 0) {
879 if (session_id == -1)
880 xo_errx(1, "-M requires -i");
882 if (nickname != NULL) {
883 if (enable != ENABLE_UNSPECIFIED)
884 xo_errx(1, "-n and -e and mutually exclusive");
886 xo_errx(1, "-n and -p and mutually exclusive");
888 xo_errx(1, "-n and -t and mutually exclusive");
890 xo_errx(1, "-n and -u and mutually exclusive");
892 xo_errx(1, "-n and -s and mutually exclusive");
896 xo_errx(1, "-a cannot be used with -M");
897 if (discovery_host != NULL)
898 xo_errx(1, "-d cannot be used with -M");
900 xo_errx(1, "-r cannot be used with -M");
902 xo_errx(1, "-v cannot be used with -M");
904 xo_errx(1, "-w cannot be used with -M");
906 } else if (Rflag != 0) {
909 xo_errx(1, "-a and -p and mutually exclusive");
911 xo_errx(1, "-a and -t and mutually exclusive");
912 if (nickname != NULL)
913 xo_errx(1, "-a and -n and mutually exclusive");
914 } else if (nickname != NULL) {
916 xo_errx(1, "-n and -p and mutually exclusive");
918 xo_errx(1, "-n and -t and mutually exclusive");
919 } else if (target == NULL && portal == NULL) {
920 xo_errx(1, "must specify either -a, -n, -t, or -p");
923 if (discovery_host != NULL)
924 xo_errx(1, "-d cannot be used with -R");
925 if (enable != ENABLE_UNSPECIFIED)
926 xo_errx(1, "-e cannot be used with -R");
927 if (session_id != -1)
928 xo_errx(1, "-i cannot be used with -R");
930 xo_errx(1, "-r cannot be used with -R");
932 xo_errx(1, "-u cannot be used with -R");
934 xo_errx(1, "-s cannot be used with -R");
936 xo_errx(1, "-v cannot be used with -R");
938 xo_errx(1, "-w cannot be used with -R");
943 if (discovery_host != NULL)
944 xo_errx(1, "-d cannot be used with -L");
945 if (session_id != -1)
946 xo_errx(1, "-i cannot be used with -L");
947 if (nickname != NULL)
948 xo_errx(1, "-n cannot be used with -L");
950 xo_errx(1, "-p cannot be used with -L");
952 xo_errx(1, "-r cannot be used with -L");
954 xo_errx(1, "-t cannot be used with -L");
956 xo_errx(1, "-u cannot be used with -L");
958 xo_errx(1, "-s cannot be used with -L");
961 iscsi_fd = open(ISCSI_PATH, O_RDWR);
962 if (iscsi_fd < 0 && errno == ENOENT) {
964 retval = kldload("iscsi");
966 iscsi_fd = open(ISCSI_PATH, O_RDWR);
971 xo_err(1, "failed to open %s", ISCSI_PATH);
973 if (Aflag != 0 && aflag != 0) {
974 conf = conf_new_from_file(conf_path);
976 TAILQ_FOREACH(targ, &conf->conf_targets, t_next)
977 failed += kernel_add(iscsi_fd, targ);
978 } else if (nickname != NULL) {
979 conf = conf_new_from_file(conf_path);
980 targ = target_find(conf, nickname);
982 xo_errx(1, "target %s not found in %s",
983 nickname, conf_path);
986 failed += kernel_add(iscsi_fd, targ);
988 failed += kernel_modify(iscsi_fd, session_id, targ);
990 failed += kernel_remove(iscsi_fd, targ);
992 failed += kernel_list(iscsi_fd, targ, vflag);
993 } else if (Mflag != 0) {
994 kernel_modify_some(iscsi_fd, session_id, target, portal,
995 user, secret, enable);
997 if (Aflag != 0 && target != NULL) {
998 if (valid_iscsi_name(target) == false)
999 xo_errx(1, "invalid target name \"%s\"", target);
1002 targ = target_new(conf);
1003 targ->t_initiator_name = default_initiator_name();
1004 targ->t_header_digest = DIGEST_NONE;
1005 targ->t_data_digest = DIGEST_NONE;
1006 targ->t_name = target;
1007 if (discovery_host != NULL) {
1008 targ->t_session_type = SESSION_TYPE_DISCOVERY;
1009 targ->t_address = discovery_host;
1011 targ->t_session_type = SESSION_TYPE_NORMAL;
1012 targ->t_address = portal;
1014 targ->t_enable = enable;
1016 targ->t_protocol = PROTOCOL_ISER;
1017 targ->t_user = user;
1018 targ->t_secret = secret;
1021 failed += kernel_add(iscsi_fd, targ);
1022 else if (Rflag != 0)
1023 failed += kernel_remove(iscsi_fd, targ);
1025 failed += kernel_list(iscsi_fd, targ, vflag);
1029 failed += kernel_wait(iscsi_fd, timeout);
1031 error = close(iscsi_fd);
1035 xo_close_container("iscsictl");