xref: /spdk/lib/iscsi/tgt_node.c (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/conf.h"
38 #include "spdk/sock.h"
39 #include "spdk/scsi.h"
40 
41 #include "spdk_internal/log.h"
42 
43 #include "iscsi/iscsi.h"
44 #include "iscsi/conn.h"
45 #include "iscsi/tgt_node.h"
46 #include "iscsi/portal_grp.h"
47 #include "iscsi/init_grp.h"
48 #include "iscsi/task.h"
49 
50 #define MAX_TMPBUF 4096
51 #define MAX_MASKBUF 128
52 
53 static bool
54 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
55 {
56 	struct in6_addr in6_mask;
57 	struct in6_addr in6_addr;
58 	char mask[MAX_MASKBUF];
59 	const char *p;
60 	size_t n;
61 	int bits, bmask;
62 	int i;
63 
64 	if (netmask[0] != '[') {
65 		return false;
66 	}
67 	p = strchr(netmask, ']');
68 	if (p == NULL) {
69 		return false;
70 	}
71 	n = p - (netmask + 1);
72 	if (n + 1 > sizeof mask) {
73 		return false;
74 	}
75 
76 	memcpy(mask, netmask + 1, n);
77 	mask[n] = '\0';
78 	p++;
79 
80 	if (p[0] == '/') {
81 		bits = (int) strtol(p + 1, NULL, 10);
82 		if (bits <= 0 || bits > 128) {
83 			return false;
84 		}
85 	} else {
86 		bits = 128;
87 	}
88 
89 #if 0
90 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "input %s\n", addr);
91 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "mask  %s / %d\n", mask, bits);
92 #endif
93 
94 	/* presentation to network order binary */
95 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
96 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
97 		return false;
98 	}
99 
100 	/* check 128bits */
101 	for (i = 0; i < (bits / 8); i++) {
102 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
103 			return false;
104 		}
105 	}
106 	if (bits % 8) {
107 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
108 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
109 			return false;
110 		}
111 	}
112 
113 	/* match */
114 	return true;
115 }
116 
117 static bool
118 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
119 {
120 	struct in_addr in4_mask;
121 	struct in_addr in4_addr;
122 	char mask[MAX_MASKBUF];
123 	const char *p;
124 	uint32_t bmask;
125 	size_t n;
126 	int bits;
127 
128 	p = strchr(netmask, '/');
129 	if (p == NULL) {
130 		p = netmask + strlen(netmask);
131 	}
132 	n = p - netmask;
133 	if (n + 1 > sizeof mask) {
134 		return false;
135 	}
136 
137 	memcpy(mask, netmask, n);
138 	mask[n] = '\0';
139 
140 	if (p[0] == '/') {
141 		bits = (int) strtol(p + 1, NULL, 10);
142 		if (bits <= 0 || bits > 32) {
143 			return false;
144 		}
145 	} else {
146 		bits = 32;
147 	}
148 
149 	/* presentation to network order binary */
150 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
151 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
152 		return false;
153 	}
154 
155 	/* check 32bits */
156 	bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
157 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
158 		return false;
159 	}
160 
161 	/* match */
162 	return true;
163 }
164 
165 static bool
166 iscsi_netmask_allow_addr(const char *netmask, const char *addr)
167 {
168 	if (netmask == NULL || addr == NULL) {
169 		return false;
170 	}
171 	if (strcasecmp(netmask, "ANY") == 0) {
172 		return true;
173 	}
174 	if (netmask[0] == '[') {
175 		/* IPv6 */
176 		if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
177 			return true;
178 		}
179 	} else {
180 		/* IPv4 */
181 		if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
182 			return true;
183 		}
184 	}
185 	return false;
186 }
187 
188 static bool
189 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
190 			  const char *addr)
191 {
192 	struct spdk_iscsi_initiator_netmask *imask;
193 
194 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
195 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "netmask=%s, addr=%s\n",
196 			      imask->mask, addr);
197 		if (iscsi_netmask_allow_addr(imask->mask, addr)) {
198 			return true;
199 		}
200 	}
201 	return false;
202 }
203 
204 static int
205 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
206 				const char *iqn, bool *result)
207 {
208 	struct spdk_iscsi_initiator_name *iname;
209 
210 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
211 		/* denied if iqn is matched */
212 		if ((iname->name[0] == '!')
213 		    && (strcasecmp(&iname->name[1], "ANY") == 0
214 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
215 			*result = false;
216 			return 0;
217 		}
218 		/* allowed if iqn is matched */
219 		if (strcasecmp(iname->name, "ANY") == 0
220 		    || strcasecmp(iname->name, iqn) == 0) {
221 			*result = true;
222 			return 0;
223 		}
224 	}
225 	return -1;
226 }
227 
228 static struct spdk_iscsi_pg_map *
229 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
230 			   struct spdk_iscsi_portal_grp *pg);
231 
232 bool
233 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
234 		      struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
235 {
236 	struct spdk_iscsi_portal_grp *pg;
237 	struct spdk_iscsi_pg_map *pg_map;
238 	struct spdk_iscsi_ig_map *ig_map;
239 	int rc;
240 	bool allowed = false;
241 
242 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
243 		return false;
244 	}
245 	pg = conn->portal->group;
246 
247 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "pg=%d, iqn=%s, addr=%s\n",
248 		      pg->tag, iqn, addr);
249 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
250 	if (pg_map == NULL) {
251 		return false;
252 	}
253 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
254 		rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
255 		if (rc == 0) {
256 			if (allowed == false) {
257 				goto denied;
258 			} else {
259 				if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
260 					return true;
261 				}
262 			}
263 		} else {
264 			/* netmask is denied in this initiator group */
265 		}
266 	}
267 
268 denied:
269 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "access denied from %s (%s) to %s (%s:%s,%d)\n",
270 		      iqn, addr, target->name, conn->portal_host,
271 		      conn->portal_port, conn->pg_tag);
272 	return false;
273 }
274 
275 static bool
276 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
277 {
278 	struct spdk_iscsi_pg_map *pg_map;
279 	struct spdk_iscsi_ig_map *ig_map;
280 	int rc;
281 	bool result = false;
282 
283 	if (target == NULL || iqn == NULL) {
284 		return false;
285 	}
286 
287 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
288 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
289 			rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
290 			if (rc == 0) {
291 				return result;
292 			}
293 		}
294 	}
295 
296 	return false;
297 }
298 
299 static int
300 iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn,
301 		       struct spdk_iscsi_tgt_node *target,
302 		       uint8_t *data, int alloc_len, int total)
303 {
304 	char buf[MAX_TMPBUF];
305 	struct spdk_iscsi_portal_grp *pg;
306 	struct spdk_iscsi_pg_map *pg_map;
307 	struct spdk_iscsi_portal *p;
308 	char *host;
309 	int len;
310 
311 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
312 		pg = pg_map->pg;
313 
314 		if (pg->is_private) {
315 			/* Skip the private portal group. Portals in the private portal group
316 			 * will be returned only by temporary login redirection responses.
317 			 */
318 			continue;
319 		}
320 
321 		TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
322 			if (alloc_len - total < 1) {
323 				/* TODO: long text responses support */
324 				SPDK_ERRLOG("SPDK doesn't support long text responses now, "
325 					    "you can use larger MaxRecvDataSegmentLength"
326 					    "value in initiator\n");
327 				return alloc_len;
328 			}
329 			host = p->host;
330 			/* wildcard? */
331 			if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) {
332 				if (spdk_sock_is_ipv6(conn->sock)) {
333 					snprintf(buf, sizeof buf, "[%s]", conn->target_addr);
334 					host = buf;
335 				} else if (spdk_sock_is_ipv4(conn->sock)) {
336 					snprintf(buf, sizeof buf, "%s", conn->target_addr);
337 					host = buf;
338 				} else {
339 					/* skip portal for the family */
340 					continue;
341 				}
342 			}
343 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "TargetAddress=%s:%s,%d\n",
344 				      host, p->port, pg->tag);
345 			len = snprintf((char *)data + total, alloc_len - total,
346 				       "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
347 			total += len + 1;
348 		}
349 	}
350 
351 	return total;
352 }
353 
354 int
355 iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
356 		const char *tiqn, uint8_t *data, int alloc_len, int data_len)
357 {
358 	struct spdk_iscsi_tgt_node *target;
359 	int total;
360 	int len;
361 	int rc;
362 
363 	if (conn == NULL) {
364 		return 0;
365 	}
366 
367 	total = data_len;
368 	if (alloc_len < 1) {
369 		return 0;
370 	}
371 	if (total >= alloc_len) {
372 		total = alloc_len;
373 		data[total - 1] = '\0';
374 		return total;
375 	}
376 
377 	pthread_mutex_lock(&g_iscsi.mutex);
378 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
379 		if (strcasecmp(tiqn, "ALL") != 0
380 		    && strcasecmp(tiqn, target->name) != 0) {
381 			continue;
382 		}
383 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
384 		if (rc == 0) {
385 			continue;
386 		}
387 
388 		len = snprintf((char *)data + total, alloc_len - total, "TargetName=%s",
389 			       target->name);
390 		total += len + 1;
391 
392 		total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total);
393 		if (alloc_len - total < 1) {
394 			break;
395 		}
396 	}
397 	pthread_mutex_unlock(&g_iscsi.mutex);
398 
399 	return total;
400 }
401 
402 struct spdk_iscsi_tgt_node *
403 iscsi_find_tgt_node(const char *target_name)
404 {
405 	struct spdk_iscsi_tgt_node *target;
406 
407 	if (target_name == NULL) {
408 		return NULL;
409 	}
410 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
411 		if (strcasecmp(target_name, target->name) == 0) {
412 			return target;
413 		}
414 	}
415 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name);
416 	return NULL;
417 }
418 
419 static int
420 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
421 {
422 	pthread_mutex_lock(&g_iscsi.mutex);
423 
424 	if (iscsi_find_tgt_node(target->name) != NULL) {
425 		pthread_mutex_unlock(&g_iscsi.mutex);
426 		return -EEXIST;
427 	}
428 
429 	TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
430 
431 	pthread_mutex_unlock(&g_iscsi.mutex);
432 	return 0;
433 }
434 
435 static int
436 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
437 {
438 	struct spdk_iscsi_tgt_node *t;
439 
440 	TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
441 		if (t == target) {
442 			TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
443 			return 0;
444 		}
445 	}
446 
447 	return -1;
448 }
449 
450 static struct spdk_iscsi_ig_map *
451 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
452 			 struct spdk_iscsi_init_grp *ig)
453 {
454 	struct spdk_iscsi_ig_map *ig_map;
455 
456 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
457 		if (ig_map->ig == ig) {
458 			return ig_map;
459 		}
460 	}
461 
462 	return NULL;
463 }
464 
465 static struct spdk_iscsi_ig_map *
466 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
467 			struct spdk_iscsi_init_grp *ig)
468 {
469 	struct spdk_iscsi_ig_map *ig_map;
470 
471 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
472 		return NULL;
473 	}
474 
475 	ig_map = malloc(sizeof(*ig_map));
476 	if (ig_map == NULL) {
477 		return NULL;
478 	}
479 
480 	ig_map->ig = ig;
481 	ig->ref++;
482 	pg_map->num_ig_maps++;
483 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
484 
485 	return ig_map;
486 }
487 
488 static void
489 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
490 			    struct spdk_iscsi_ig_map *ig_map)
491 {
492 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
493 	pg_map->num_ig_maps--;
494 	ig_map->ig->ref--;
495 	free(ig_map);
496 }
497 
498 static int
499 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
500 			   struct spdk_iscsi_init_grp *ig)
501 {
502 	struct spdk_iscsi_ig_map *ig_map;
503 
504 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
505 	if (ig_map == NULL) {
506 		return -ENOENT;
507 	}
508 
509 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
510 	return 0;
511 }
512 
513 static void
514 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
515 {
516 	struct spdk_iscsi_ig_map *ig_map, *tmp;
517 
518 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
519 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
520 	}
521 }
522 
523 static struct spdk_iscsi_pg_map *
524 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
525 			   struct spdk_iscsi_portal_grp *pg)
526 {
527 	struct spdk_iscsi_pg_map *pg_map;
528 
529 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
530 		if (pg_map->pg == pg) {
531 			return pg_map;
532 		}
533 	}
534 
535 	return NULL;
536 }
537 
538 static struct spdk_iscsi_pg_map *
539 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
540 			  struct spdk_iscsi_portal_grp *pg)
541 {
542 	struct spdk_iscsi_pg_map *pg_map;
543 	char port_name[MAX_TMPBUF];
544 	int rc;
545 
546 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
547 		return NULL;
548 	}
549 
550 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
551 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
552 			    SPDK_SCSI_DEV_MAX_PORTS);
553 		return NULL;
554 	}
555 
556 	pg_map = calloc(1, sizeof(*pg_map));
557 	if (pg_map == NULL) {
558 		return NULL;
559 	}
560 
561 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
562 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
563 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
564 	if (rc != 0) {
565 		free(pg_map);
566 		return NULL;
567 	}
568 
569 	TAILQ_INIT(&pg_map->ig_map_head);
570 	pg_map->num_ig_maps = 0;
571 	pg->ref++;
572 	pg_map->pg = pg;
573 	target->num_pg_maps++;
574 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
575 
576 	return pg_map;
577 }
578 
579 static void
580 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
581 			      struct spdk_iscsi_pg_map *pg_map)
582 {
583 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
584 	target->num_pg_maps--;
585 	pg_map->pg->ref--;
586 
587 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
588 
589 	free(pg_map);
590 }
591 
592 static int
593 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
594 			     struct spdk_iscsi_portal_grp *pg)
595 {
596 	struct spdk_iscsi_pg_map *pg_map;
597 
598 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
599 	if (pg_map == NULL) {
600 		return -ENOENT;
601 	}
602 
603 	if (pg_map->num_ig_maps > 0) {
604 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n",
605 			      pg_map->num_ig_maps);
606 	}
607 
608 	iscsi_pg_map_delete_all_ig_maps(pg_map);
609 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
610 	return 0;
611 }
612 
613 static void
614 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
615 			      struct spdk_iscsi_init_grp *ig)
616 {
617 	struct spdk_iscsi_pg_map *pg_map, *tmp;
618 
619 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
620 		iscsi_pg_map_delete_ig_map(pg_map, ig);
621 		if (pg_map->num_ig_maps == 0) {
622 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
623 		}
624 	}
625 }
626 
627 static void
628 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
629 {
630 	struct spdk_iscsi_pg_map *pg_map, *tmp;
631 
632 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
633 		iscsi_pg_map_delete_all_ig_maps(pg_map);
634 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
635 	}
636 }
637 
638 static void
639 _iscsi_tgt_node_destruct(void *cb_arg, int rc)
640 {
641 	struct spdk_iscsi_tgt_node *target = cb_arg;
642 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
643 	void *destruct_cb_arg = target->destruct_cb_arg;
644 
645 	if (rc != 0) {
646 		if (destruct_cb_fn) {
647 			destruct_cb_fn(destruct_cb_arg, rc);
648 		}
649 		return;
650 	}
651 
652 	pthread_mutex_lock(&g_iscsi.mutex);
653 	iscsi_tgt_node_delete_all_pg_maps(target);
654 	pthread_mutex_unlock(&g_iscsi.mutex);
655 
656 	pthread_mutex_destroy(&target->mutex);
657 	free(target);
658 
659 	if (destruct_cb_fn) {
660 		destruct_cb_fn(destruct_cb_arg, 0);
661 	}
662 }
663 
664 static int
665 iscsi_tgt_node_check_active_conns(void *arg)
666 {
667 	struct spdk_iscsi_tgt_node *target = arg;
668 
669 	if (iscsi_get_active_conns(target) != 0) {
670 		return SPDK_POLLER_BUSY;
671 	}
672 
673 	spdk_poller_unregister(&target->destruct_poller);
674 
675 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
676 
677 	return SPDK_POLLER_BUSY;
678 }
679 
680 static void
681 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
682 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
683 {
684 	if (target == NULL) {
685 		if (cb_fn) {
686 			cb_fn(cb_arg, -ENOENT);
687 		}
688 		return;
689 	}
690 
691 	if (target->destructed) {
692 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
693 		if (cb_fn) {
694 			cb_fn(cb_arg, -EBUSY);
695 		}
696 		return;
697 	}
698 
699 	target->destructed = true;
700 	target->destruct_cb_fn = cb_fn;
701 	target->destruct_cb_arg = cb_arg;
702 
703 	iscsi_conns_request_logout(target, -1);
704 
705 	if (iscsi_get_active_conns(target) != 0) {
706 		target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
707 					  target, 10);
708 	} else {
709 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
710 	}
711 
712 }
713 
714 static int
715 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
716 				int pg_tag, int ig_tag)
717 {
718 	struct spdk_iscsi_portal_grp	*pg;
719 	struct spdk_iscsi_init_grp	*ig;
720 	struct spdk_iscsi_pg_map	*pg_map;
721 	struct spdk_iscsi_ig_map	*ig_map;
722 
723 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
724 	if (pg == NULL) {
725 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
726 		return -ENOENT;
727 	}
728 	ig = iscsi_init_grp_find_by_tag(ig_tag);
729 	if (ig == NULL) {
730 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
731 		return -ENOENT;
732 	}
733 
734 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
735 	if (pg_map == NULL) {
736 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
737 		return -ENOENT;
738 	}
739 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
740 	if (ig_map == NULL) {
741 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
742 		return -ENOENT;
743 	}
744 
745 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
746 	if (pg_map->num_ig_maps == 0) {
747 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
748 	}
749 
750 	return 0;
751 }
752 
753 static int
754 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
755 			     int pg_tag, int ig_tag)
756 {
757 	struct spdk_iscsi_portal_grp	*pg;
758 	struct spdk_iscsi_pg_map	*pg_map;
759 	struct spdk_iscsi_init_grp	*ig;
760 	struct spdk_iscsi_ig_map	*ig_map;
761 	bool				new_pg_map = false;
762 
763 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
764 	if (pg == NULL) {
765 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
766 		return -ENOENT;
767 	}
768 	ig = iscsi_init_grp_find_by_tag(ig_tag);
769 	if (ig == NULL) {
770 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
771 		return -ENOENT;
772 	}
773 
774 	/* get existing pg_map or create new pg_map and add it to target */
775 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
776 	if (pg_map == NULL) {
777 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
778 		if (pg_map == NULL) {
779 			goto failed;
780 		}
781 		new_pg_map = true;
782 	}
783 
784 	/* create new ig_map and add it to pg_map */
785 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
786 	if (ig_map == NULL) {
787 		goto failed;
788 	}
789 
790 	return 0;
791 
792 failed:
793 	if (new_pg_map) {
794 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
795 	}
796 
797 	return -1;
798 }
799 
800 int
801 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
802 				 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
803 {
804 	uint16_t i;
805 	int rc;
806 
807 	pthread_mutex_lock(&g_iscsi.mutex);
808 	for (i = 0; i < num_maps; i++) {
809 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
810 						  ig_tag_list[i]);
811 		if (rc != 0) {
812 			SPDK_ERRLOG("could not add map to target\n");
813 			goto invalid;
814 		}
815 	}
816 	pthread_mutex_unlock(&g_iscsi.mutex);
817 	return 0;
818 
819 invalid:
820 	for (; i > 0; --i) {
821 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
822 						ig_tag_list[i - 1]);
823 	}
824 	pthread_mutex_unlock(&g_iscsi.mutex);
825 	return -1;
826 }
827 
828 int
829 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
830 				    int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
831 {
832 	uint16_t i;
833 	int rc;
834 
835 	pthread_mutex_lock(&g_iscsi.mutex);
836 	for (i = 0; i < num_maps; i++) {
837 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
838 						     ig_tag_list[i]);
839 		if (rc != 0) {
840 			SPDK_ERRLOG("could not delete map from target\n");
841 			goto invalid;
842 		}
843 	}
844 	pthread_mutex_unlock(&g_iscsi.mutex);
845 	return 0;
846 
847 invalid:
848 	for (; i > 0; --i) {
849 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
850 						  ig_tag_list[i - 1]);
851 		if (rc != 0) {
852 			iscsi_tgt_node_delete_all_pg_maps(target);
853 			break;
854 		}
855 	}
856 	pthread_mutex_unlock(&g_iscsi.mutex);
857 	return -1;
858 }
859 
860 int
861 iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
862 			const char *host, const char *port)
863 {
864 	struct spdk_iscsi_portal_grp *pg;
865 	struct spdk_iscsi_pg_map *pg_map;
866 	struct sockaddr_storage sa;
867 
868 	if (target == NULL) {
869 		return -EINVAL;
870 	}
871 
872 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
873 	if (pg == NULL) {
874 		SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
875 		return -EINVAL;
876 	}
877 
878 	if (pg->is_private) {
879 		SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
880 		return -EINVAL;
881 	}
882 
883 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
884 	if (pg_map == NULL) {
885 		SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
886 		return -EINVAL;
887 	}
888 
889 	if (host == NULL && port == NULL) {
890 		/* Clear redirect setting. */
891 		memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
892 		memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
893 	} else {
894 		if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
895 			SPDK_ERRLOG("IP address-port pair is not valid.\n");
896 			return -EINVAL;
897 		}
898 
899 		if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
900 			SPDK_ERRLOG("IP address-port pair must be chosen from a "
901 				    "different private portal group\n");
902 			return -EINVAL;
903 		}
904 
905 		snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
906 		snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
907 	}
908 
909 	return 0;
910 }
911 
912 bool
913 iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
914 			     struct spdk_iscsi_tgt_node *target,
915 			     char *buf, int buf_len)
916 {
917 	struct spdk_iscsi_pg_map *pg_map;
918 
919 	if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
920 		return false;
921 	}
922 
923 	pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
924 	if (pg_map == NULL) {
925 		return false;
926 	}
927 
928 	if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
929 		return false;
930 	}
931 
932 	snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
933 
934 	return true;
935 }
936 
937 static int
938 check_iscsi_name(const char *name)
939 {
940 	const unsigned char *up = (const unsigned char *) name;
941 	size_t n;
942 
943 	/* valid iSCSI name no larger than 223 bytes */
944 	if (strlen(name) > MAX_TARGET_NAME) {
945 		return -1;
946 	}
947 
948 	/* valid iSCSI name? */
949 	for (n = 0; up[n] != 0; n++) {
950 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
951 			return -1;
952 		}
953 		if (up[n] == 0x2fU) {
954 			return -1;
955 		}
956 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
957 			return -1;
958 		}
959 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
960 			return -1;
961 		}
962 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
963 			return -1;
964 		}
965 		if (isspace(up[n])) {
966 			return -1;
967 		}
968 	}
969 	/* valid format? */
970 	if (strncasecmp(name, "iqn.", 4) == 0) {
971 		/* iqn.YYYY-MM.reversed.domain.name */
972 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
973 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
974 		    || !isdigit(up[10]) || up[11] != '.') {
975 			SPDK_ERRLOG("invalid iqn format. "
976 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
977 			return -1;
978 		}
979 	} else if (strncasecmp(name, "eui.", 4) == 0) {
980 		/* EUI-64 -> 16bytes */
981 		/* XXX */
982 	} else if (strncasecmp(name, "naa.", 4) == 0) {
983 		/* 64bit -> 16bytes, 128bit -> 32bytes */
984 		/* XXX */
985 	}
986 	/* OK */
987 	return 0;
988 }
989 
990 bool
991 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
992 {
993 	if (group < 0) {
994 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
995 		return false;
996 	}
997 	if ((!disable && !require && !mutual) ||	/* Auto */
998 	    (disable && !require && !mutual) ||	/* None */
999 	    (!disable && require && !mutual) ||	/* CHAP */
1000 	    (!disable && require && mutual)) {	/* CHAP Mutual */
1001 		return true;
1002 	}
1003 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1004 		    disable, require, mutual);
1005 	return false;
1006 }
1007 
1008 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1009 		const char *name, const char *alias,
1010 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1011 		const char *bdev_name_list[], int *lun_id_list, int num_luns,
1012 		int queue_depth,
1013 		bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1014 		bool header_digest, bool data_digest)
1015 {
1016 	char				fullname[MAX_TMPBUF];
1017 	struct spdk_iscsi_tgt_node	*target;
1018 	int				rc;
1019 
1020 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1021 				     mutual_chap, chap_group)) {
1022 		return NULL;
1023 	}
1024 
1025 	if (num_maps == 0) {
1026 		SPDK_ERRLOG("num_maps = 0\n");
1027 		return NULL;
1028 	}
1029 
1030 	if (name == NULL) {
1031 		SPDK_ERRLOG("TargetName not found\n");
1032 		return NULL;
1033 	}
1034 
1035 	if (strncasecmp(name, "iqn.", 4) != 0
1036 	    && strncasecmp(name, "eui.", 4) != 0
1037 	    && strncasecmp(name, "naa.", 4) != 0) {
1038 		snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
1039 	} else {
1040 		snprintf(fullname, sizeof(fullname), "%s", name);
1041 	}
1042 
1043 	if (check_iscsi_name(fullname) != 0) {
1044 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1045 			    name);
1046 		return NULL;
1047 	}
1048 
1049 	target = calloc(1, sizeof(*target));
1050 	if (!target) {
1051 		SPDK_ERRLOG("could not allocate target\n");
1052 		return NULL;
1053 	}
1054 
1055 	rc = pthread_mutex_init(&target->mutex, NULL);
1056 	if (rc != 0) {
1057 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1058 		iscsi_tgt_node_destruct(target, NULL, NULL);
1059 		return NULL;
1060 	}
1061 
1062 	target->num = target_index;
1063 
1064 	memcpy(target->name, fullname, strlen(fullname));
1065 
1066 	if (alias != NULL) {
1067 		if (strlen(alias) > MAX_TARGET_NAME) {
1068 			iscsi_tgt_node_destruct(target, NULL, NULL);
1069 			return NULL;
1070 		}
1071 		memcpy(target->alias, alias, strlen(alias));
1072 	}
1073 
1074 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1075 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1076 	if (!target->dev) {
1077 		SPDK_ERRLOG("Could not construct SCSI device\n");
1078 		iscsi_tgt_node_destruct(target, NULL, NULL);
1079 		return NULL;
1080 	}
1081 
1082 	TAILQ_INIT(&target->pg_map_head);
1083 	rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1084 					      ig_tag_list, num_maps);
1085 	if (rc != 0) {
1086 		SPDK_ERRLOG("could not add map to target\n");
1087 		iscsi_tgt_node_destruct(target, NULL, NULL);
1088 		return NULL;
1089 	}
1090 
1091 	target->disable_chap = disable_chap;
1092 	target->require_chap = require_chap;
1093 	target->mutual_chap = mutual_chap;
1094 	target->chap_group = chap_group;
1095 	target->header_digest = header_digest;
1096 	target->data_digest = data_digest;
1097 
1098 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1099 		target->queue_depth = queue_depth;
1100 	} else {
1101 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
1102 			      queue_depth, g_iscsi.MaxQueueDepth);
1103 		target->queue_depth = g_iscsi.MaxQueueDepth;
1104 	}
1105 
1106 	rc = iscsi_tgt_node_register(target);
1107 	if (rc != 0) {
1108 		SPDK_ERRLOG("register target is failed\n");
1109 		iscsi_tgt_node_destruct(target, NULL, NULL);
1110 		return NULL;
1111 	}
1112 
1113 	return target;
1114 }
1115 
1116 static int
1117 iscsi_parse_tgt_node(struct spdk_conf_section *sp)
1118 {
1119 	char buf[MAX_TMPBUF];
1120 	struct spdk_iscsi_tgt_node *target;
1121 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
1122 	int num_target_maps;
1123 	const char *alias, *pg_tag, *ig_tag;
1124 	const char *ag_tag;
1125 	const char *val, *name;
1126 	int target_num, chap_group, pg_tag_i, ig_tag_i;
1127 	bool header_digest, data_digest;
1128 	bool disable_chap, require_chap, mutual_chap;
1129 	int i;
1130 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
1131 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
1132 	int num_luns, queue_depth;
1133 
1134 	target_num = spdk_conf_section_get_num(sp);
1135 
1136 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
1137 
1138 	data_digest = false;
1139 	header_digest = false;
1140 
1141 	name = spdk_conf_section_get_val(sp, "TargetName");
1142 
1143 	if (name == NULL) {
1144 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1145 		return -1;
1146 	}
1147 
1148 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1149 
1150 	/* Setup initiator and portal group mapping */
1151 	val = spdk_conf_section_get_val(sp, "Mapping");
1152 	if (val == NULL) {
1153 		/* no map */
1154 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1155 		return -1;
1156 	}
1157 
1158 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1159 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1160 		if (val == NULL) {
1161 			break;
1162 		}
1163 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1164 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1165 		if (pg_tag == NULL || ig_tag == NULL) {
1166 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1167 			return -1;
1168 		}
1169 		if (strncasecmp(pg_tag, "PortalGroup",
1170 				strlen("PortalGroup")) != 0
1171 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1172 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1173 			return -1;
1174 		}
1175 		if (strncasecmp(ig_tag, "InitiatorGroup",
1176 				strlen("InitiatorGroup")) != 0
1177 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1178 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1179 			return -1;
1180 		}
1181 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1182 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1183 			return -1;
1184 		}
1185 		pg_tag_list[i] = pg_tag_i;
1186 		ig_tag_list[i] = ig_tag_i;
1187 	}
1188 
1189 	num_target_maps = i;
1190 
1191 	/* Setup AuthMethod */
1192 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1193 	disable_chap = false;
1194 	require_chap = false;
1195 	mutual_chap = false;
1196 	if (val != NULL) {
1197 		for (i = 0; ; i++) {
1198 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1199 			if (val == NULL) {
1200 				break;
1201 			}
1202 			if (strcasecmp(val, "CHAP") == 0) {
1203 				require_chap = true;
1204 			} else if (strcasecmp(val, "Mutual") == 0) {
1205 				mutual_chap = true;
1206 			} else if (strcasecmp(val, "Auto") == 0) {
1207 				disable_chap = false;
1208 				require_chap = false;
1209 				mutual_chap = false;
1210 			} else if (strcasecmp(val, "None") == 0) {
1211 				disable_chap = true;
1212 				require_chap = false;
1213 				mutual_chap = false;
1214 			} else {
1215 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1216 				return -1;
1217 			}
1218 		}
1219 		if (mutual_chap && !require_chap) {
1220 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1221 			return -1;
1222 		}
1223 	}
1224 	if (disable_chap) {
1225 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1226 	} else if (!require_chap) {
1227 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1228 	} else {
1229 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1230 			      mutual_chap ? "Mutual" : "");
1231 	}
1232 
1233 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1234 	if (val == NULL) {
1235 		chap_group = 0;
1236 	} else {
1237 		ag_tag = val;
1238 		if (strcasecmp(ag_tag, "None") == 0) {
1239 			chap_group = 0;
1240 		} else {
1241 			if (strncasecmp(ag_tag, "AuthGroup",
1242 					strlen("AuthGroup")) != 0
1243 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1244 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1245 				return -1;
1246 			}
1247 			if (chap_group == 0) {
1248 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1249 				return -1;
1250 			}
1251 		}
1252 	}
1253 	if (chap_group == 0) {
1254 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1255 	} else {
1256 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1257 	}
1258 
1259 	val = spdk_conf_section_get_val(sp, "UseDigest");
1260 	if (val != NULL) {
1261 		for (i = 0; ; i++) {
1262 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1263 			if (val == NULL) {
1264 				break;
1265 			}
1266 			if (strcasecmp(val, "Header") == 0) {
1267 				header_digest = true;
1268 			} else if (strcasecmp(val, "Data") == 0) {
1269 				data_digest = true;
1270 			} else if (strcasecmp(val, "Auto") == 0) {
1271 				header_digest = false;
1272 				data_digest = false;
1273 			} else {
1274 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1275 				return -1;
1276 			}
1277 		}
1278 	}
1279 	if (!header_digest && !data_digest) {
1280 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1281 	} else {
1282 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1283 			      header_digest ? "Header" : "",
1284 			      data_digest ? "Data" : "");
1285 	}
1286 
1287 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1288 	if (val == NULL) {
1289 		queue_depth = g_iscsi.MaxQueueDepth;
1290 	} else {
1291 		queue_depth = (int) strtol(val, NULL, 10);
1292 	}
1293 
1294 	num_luns = 0;
1295 
1296 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1297 		snprintf(buf, sizeof(buf), "LUN%d", i);
1298 		val = spdk_conf_section_get_val(sp, buf);
1299 		if (val == NULL) {
1300 			continue;
1301 		}
1302 
1303 		bdev_name_list[num_luns] = val;
1304 		lun_id_list[num_luns] = i;
1305 		num_luns++;
1306 	}
1307 
1308 	if (num_luns == 0) {
1309 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1310 		return -1;
1311 	}
1312 
1313 	target = iscsi_tgt_node_construct(target_num, name, alias,
1314 					  pg_tag_list, ig_tag_list, num_target_maps,
1315 					  bdev_name_list, lun_id_list, num_luns, queue_depth,
1316 					  disable_chap, require_chap, mutual_chap, chap_group,
1317 					  header_digest, data_digest);
1318 
1319 	if (target == NULL) {
1320 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1321 		return -1;
1322 	}
1323 
1324 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1325 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1326 
1327 		if (lun) {
1328 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1329 				     spdk_scsi_dev_get_id(target->dev),
1330 				     spdk_scsi_lun_get_id(lun),
1331 				     spdk_scsi_lun_get_bdev_name(lun));
1332 		}
1333 	}
1334 
1335 	return 0;
1336 }
1337 
1338 int iscsi_parse_tgt_nodes(void)
1339 {
1340 	struct spdk_conf_section *sp;
1341 	int rc;
1342 
1343 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "iscsi_parse_tgt_nodes\n");
1344 
1345 	sp = spdk_conf_first_section(NULL);
1346 	while (sp != NULL) {
1347 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1348 			int tag = spdk_conf_section_get_num(sp);
1349 
1350 			if (tag > SPDK_TN_TAG_MAX) {
1351 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1352 				return -1;
1353 			}
1354 			rc = iscsi_parse_tgt_node(sp);
1355 			if (rc < 0) {
1356 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1357 				return -1;
1358 			}
1359 		}
1360 		sp = spdk_conf_next_section(sp);
1361 	}
1362 	return 0;
1363 }
1364 
1365 void
1366 iscsi_shutdown_tgt_nodes(void)
1367 {
1368 	struct spdk_iscsi_tgt_node *target;
1369 
1370 	pthread_mutex_lock(&g_iscsi.mutex);
1371 	while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1372 		target = TAILQ_FIRST(&g_iscsi.target_head);
1373 		TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1374 
1375 		pthread_mutex_unlock(&g_iscsi.mutex);
1376 
1377 		iscsi_tgt_node_destruct(target, NULL, NULL);
1378 
1379 		pthread_mutex_lock(&g_iscsi.mutex);
1380 	}
1381 	pthread_mutex_unlock(&g_iscsi.mutex);
1382 }
1383 
1384 void
1385 iscsi_shutdown_tgt_node_by_name(const char *target_name,
1386 				iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1387 {
1388 	struct spdk_iscsi_tgt_node *target;
1389 
1390 	pthread_mutex_lock(&g_iscsi.mutex);
1391 	target = iscsi_find_tgt_node(target_name);
1392 	if (target != NULL) {
1393 		iscsi_tgt_node_unregister(target);
1394 		pthread_mutex_unlock(&g_iscsi.mutex);
1395 
1396 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1397 
1398 		return;
1399 	}
1400 	pthread_mutex_unlock(&g_iscsi.mutex);
1401 
1402 	if (cb_fn) {
1403 		cb_fn(cb_arg, -ENOENT);
1404 	}
1405 }
1406 
1407 bool
1408 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1409 {
1410 	return target->destructed;
1411 }
1412 
1413 int
1414 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1415 			    struct spdk_iscsi_tgt_node *target)
1416 {
1417 	int i;
1418 	struct spdk_iscsi_task *task;
1419 
1420 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1421 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1422 
1423 		if (!lun) {
1424 			continue;
1425 		}
1426 
1427 		/* we create a fake management task per LUN to cleanup */
1428 		task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1429 		if (!task) {
1430 			SPDK_ERRLOG("Unable to acquire task\n");
1431 			return -1;
1432 		}
1433 
1434 		task->scsi.target_port = conn->target_port;
1435 		task->scsi.initiator_port = conn->initiator_port;
1436 		task->scsi.lun = lun;
1437 
1438 		iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1439 	}
1440 
1441 	return 0;
1442 }
1443 
1444 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1445 			       struct spdk_iscsi_init_grp *initiator_group)
1446 {
1447 	struct spdk_iscsi_tgt_node *target;
1448 
1449 	pthread_mutex_lock(&g_iscsi.mutex);
1450 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1451 		if (portal_group) {
1452 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1453 		}
1454 		if (initiator_group) {
1455 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1456 		}
1457 	}
1458 	pthread_mutex_unlock(&g_iscsi.mutex);
1459 }
1460 
1461 int
1462 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1463 		       const char *bdev_name, int lun_id)
1464 {
1465 	struct spdk_scsi_dev *dev;
1466 	int rc;
1467 
1468 	if (target->num_active_conns > 0) {
1469 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1470 			    target->num_active_conns);
1471 		return -1;
1472 	}
1473 
1474 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1475 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1476 		return -1;
1477 	}
1478 
1479 	dev = target->dev;
1480 	if (dev == NULL) {
1481 		SPDK_ERRLOG("SCSI device is not found\n");
1482 		return -1;
1483 	}
1484 
1485 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1486 	if (rc != 0) {
1487 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1488 		return -1;
1489 	}
1490 
1491 	return 0;
1492 }
1493 
1494 int
1495 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1496 			       bool disable_chap, bool require_chap,
1497 			       bool mutual_chap, int32_t chap_group)
1498 {
1499 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1500 				     mutual_chap, chap_group)) {
1501 		return -EINVAL;
1502 	}
1503 
1504 	pthread_mutex_lock(&target->mutex);
1505 	target->disable_chap = disable_chap;
1506 	target->require_chap = require_chap;
1507 	target->mutual_chap = mutual_chap;
1508 	target->chap_group = chap_group;
1509 	pthread_mutex_unlock(&target->mutex);
1510 
1511 	return 0;
1512 }
1513 
1514 static const char *target_nodes_section = \
1515 		"\n"
1516 		"# Users should change the TargetNode section(s) below to match the\n"
1517 		"#  desired iSCSI target node configuration.\n"
1518 		"# TargetName, Mapping, LUN0 are minimum required\n";
1519 
1520 #define TARGET_NODE_TMPL \
1521 "[TargetNode%d]\n" \
1522 "  Comment \"Target%d\"\n" \
1523 "  TargetName %s\n" \
1524 "  TargetAlias \"%s\"\n"
1525 
1526 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1527 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1528 
1529 #define TARGET_NODE_AUTH_TMPL \
1530 "  AuthMethod %s\n" \
1531 "  AuthGroup %s\n" \
1532 "  UseDigest %s\n"
1533 
1534 #define TARGET_NODE_QD_TMPL \
1535 "  QueueDepth %d\n\n"
1536 
1537 #define TARGET_NODE_LUN_TMPL \
1538 "  LUN%d %s\n"
1539 
1540 void
1541 iscsi_tgt_nodes_config_text(FILE *fp)
1542 {
1543 	int l = 0;
1544 	struct spdk_scsi_dev *dev = NULL;
1545 	struct spdk_iscsi_tgt_node *target = NULL;
1546 	struct spdk_iscsi_pg_map *pg_map;
1547 	struct spdk_iscsi_ig_map *ig_map;
1548 
1549 	/* Create target nodes section */
1550 	fprintf(fp, "%s", target_nodes_section);
1551 
1552 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1553 		int idx;
1554 		const char *authmethod = "None";
1555 		char authgroup[32] = "None";
1556 		const char *usedigest = "Auto";
1557 
1558 		dev = target->dev;
1559 		if (NULL == dev) { continue; }
1560 
1561 		idx = target->num;
1562 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1563 
1564 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1565 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1566 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1567 					pg_map->pg->tag,
1568 					ig_map->ig->tag);
1569 			}
1570 		}
1571 
1572 		if (target->disable_chap) {
1573 			authmethod = "None";
1574 		} else if (!target->require_chap) {
1575 			authmethod = "Auto";
1576 		} else if (target->mutual_chap) {
1577 			authmethod = "CHAP Mutual";
1578 		} else {
1579 			authmethod = "CHAP";
1580 		}
1581 
1582 		if (target->chap_group > 0) {
1583 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1584 		}
1585 
1586 		if (target->header_digest) {
1587 			usedigest = "Header";
1588 		} else if (target->data_digest) {
1589 			usedigest = "Data";
1590 		}
1591 
1592 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1593 			authmethod, authgroup, usedigest);
1594 
1595 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1596 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1597 
1598 			if (!lun) {
1599 				continue;
1600 			}
1601 
1602 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1603 				spdk_scsi_lun_get_id(lun),
1604 				spdk_scsi_lun_get_bdev_name(lun));
1605 		}
1606 
1607 		fprintf(fp, TARGET_NODE_QD_TMPL,
1608 			target->queue_depth);
1609 	}
1610 }
1611 
1612 static void
1613 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1614 			 struct spdk_json_write_ctx *w)
1615 {
1616 	struct spdk_iscsi_pg_map *pg_map;
1617 	struct spdk_iscsi_ig_map *ig_map;
1618 	int i;
1619 
1620 	spdk_json_write_object_begin(w);
1621 
1622 	spdk_json_write_named_string(w, "name", target->name);
1623 
1624 	if (target->alias[0] != '\0') {
1625 		spdk_json_write_named_string(w, "alias_name", target->alias);
1626 	}
1627 
1628 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1629 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1630 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1631 			spdk_json_write_object_begin(w);
1632 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1633 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1634 			spdk_json_write_object_end(w);
1635 		}
1636 	}
1637 	spdk_json_write_array_end(w);
1638 
1639 	spdk_json_write_named_array_begin(w, "luns");
1640 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1641 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1642 
1643 		if (lun) {
1644 			spdk_json_write_object_begin(w);
1645 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1646 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1647 			spdk_json_write_object_end(w);
1648 		}
1649 	}
1650 	spdk_json_write_array_end(w);
1651 
1652 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1653 
1654 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1655 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1656 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1657 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1658 
1659 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1660 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1661 
1662 	spdk_json_write_object_end(w);
1663 }
1664 
1665 static void
1666 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1667 			   struct spdk_json_write_ctx *w)
1668 {
1669 	spdk_json_write_object_begin(w);
1670 
1671 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1672 
1673 	spdk_json_write_name(w, "params");
1674 	iscsi_tgt_node_info_json(target, w);
1675 
1676 	spdk_json_write_object_end(w);
1677 }
1678 
1679 void
1680 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1681 {
1682 	struct spdk_iscsi_tgt_node *target;
1683 
1684 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1685 		iscsi_tgt_node_info_json(target, w);
1686 	}
1687 }
1688 
1689 void
1690 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1691 {
1692 	struct spdk_iscsi_tgt_node *target;
1693 
1694 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1695 		iscsi_tgt_node_config_json(target, w);
1696 	}
1697 }
1698