xref: /spdk/lib/iscsi/tgt_node.c (revision bb488d2829a9b7863daab45917dd2174905cc0ae)
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 1024
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 spdk_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->portal->group->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 int
300 spdk_iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
301 		     const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len,
302 		     int data_len)
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 	struct spdk_iscsi_tgt_node	*target;
309 	char *host;
310 	int total;
311 	int len;
312 	int rc;
313 
314 	if (conn == NULL) {
315 		return 0;
316 	}
317 
318 	total = data_len;
319 	if (alloc_len < 1) {
320 		return 0;
321 	}
322 	if (total > alloc_len) {
323 		total = alloc_len;
324 		data[total - 1] = '\0';
325 		return total;
326 	}
327 
328 	if (alloc_len - total < 1) {
329 		SPDK_ERRLOG("data space small %d\n", alloc_len);
330 		return total;
331 	}
332 
333 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
334 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
335 		if (strcasecmp(tiqn, "ALL") != 0
336 		    && strcasecmp(tiqn, target->name) != 0) {
337 			continue;
338 		}
339 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
340 		if (rc == 0) {
341 			continue;
342 		}
343 
344 		/* DO SENDTARGETS */
345 		len = snprintf((char *) data + total, alloc_len - total,
346 			       "TargetName=%s", target->name);
347 		total += len + 1;
348 
349 		/* write to data */
350 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
351 			pg = pg_map->pg;
352 			TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
353 				if (alloc_len - total < 1) {
354 					pthread_mutex_unlock(&g_spdk_iscsi.mutex);
355 					SPDK_ERRLOG("data space small %d\n", alloc_len);
356 					return total;
357 				}
358 				host = p->host;
359 				/* wildcard? */
360 				if (strcasecmp(host, "[::]") == 0
361 				    || strcasecmp(host, "0.0.0.0") == 0) {
362 					if (spdk_sock_is_ipv6(conn->sock)) {
363 						snprintf(buf, sizeof buf, "[%s]",
364 							 conn->target_addr);
365 						host = buf;
366 					} else if (spdk_sock_is_ipv4(conn->sock)) {
367 						snprintf(buf, sizeof buf, "%s",
368 							 conn->target_addr);
369 						host = buf;
370 					} else {
371 						/* skip portal for the family */
372 						continue;
373 					}
374 				}
375 				SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
376 					      "TargetAddress=%s:%s,%d\n",
377 					      host, p->port, pg->tag);
378 				len = snprintf((char *) data + total,
379 					       alloc_len - total,
380 					       "TargetAddress=%s:%s,%d",
381 					       host, p->port, pg->tag);
382 				total += len + 1;
383 			}
384 		}
385 	}
386 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
387 
388 	return total;
389 }
390 
391 struct spdk_iscsi_tgt_node *
392 spdk_iscsi_find_tgt_node(const char *target_name)
393 {
394 	struct spdk_iscsi_tgt_node *target;
395 
396 	if (target_name == NULL) {
397 		return NULL;
398 	}
399 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
400 		if (strcasecmp(target_name, target->name) == 0) {
401 			return target;
402 		}
403 	}
404 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name);
405 	return NULL;
406 }
407 
408 static int
409 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
410 {
411 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
412 
413 	if (spdk_iscsi_find_tgt_node(target->name) != NULL) {
414 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
415 		return -EEXIST;
416 	}
417 
418 	TAILQ_INSERT_TAIL(&g_spdk_iscsi.target_head, target, tailq);
419 
420 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
421 	return 0;
422 }
423 
424 static int
425 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
426 {
427 	struct spdk_iscsi_tgt_node *t;
428 
429 	TAILQ_FOREACH(t, &g_spdk_iscsi.target_head, tailq) {
430 		if (t == target) {
431 			TAILQ_REMOVE(&g_spdk_iscsi.target_head, t, tailq);
432 			return 0;
433 		}
434 	}
435 
436 	return -1;
437 }
438 
439 static struct spdk_iscsi_ig_map *
440 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
441 			 struct spdk_iscsi_init_grp *ig)
442 {
443 	struct spdk_iscsi_ig_map *ig_map;
444 
445 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
446 		if (ig_map->ig == ig) {
447 			return ig_map;
448 		}
449 	}
450 
451 	return NULL;
452 }
453 
454 static struct spdk_iscsi_ig_map *
455 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
456 			struct spdk_iscsi_init_grp *ig)
457 {
458 	struct spdk_iscsi_ig_map *ig_map;
459 
460 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
461 		return NULL;
462 	}
463 
464 	ig_map = malloc(sizeof(*ig_map));
465 	if (ig_map == NULL) {
466 		return NULL;
467 	}
468 
469 	ig_map->ig = ig;
470 	ig->ref++;
471 	pg_map->num_ig_maps++;
472 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
473 
474 	return ig_map;
475 }
476 
477 static void
478 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
479 			    struct spdk_iscsi_ig_map *ig_map)
480 {
481 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
482 	pg_map->num_ig_maps--;
483 	ig_map->ig->ref--;
484 	free(ig_map);
485 }
486 
487 static int
488 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
489 			   struct spdk_iscsi_init_grp *ig)
490 {
491 	struct spdk_iscsi_ig_map *ig_map;
492 
493 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
494 	if (ig_map == NULL) {
495 		return -ENOENT;
496 	}
497 
498 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
499 	return 0;
500 }
501 
502 static void
503 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
504 {
505 	struct spdk_iscsi_ig_map *ig_map, *tmp;
506 
507 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
508 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
509 	}
510 }
511 
512 static struct spdk_iscsi_pg_map *
513 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
514 			   struct spdk_iscsi_portal_grp *pg)
515 {
516 	struct spdk_iscsi_pg_map *pg_map;
517 
518 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
519 		if (pg_map->pg == pg) {
520 			return pg_map;
521 		}
522 	}
523 
524 	return NULL;
525 }
526 
527 static struct spdk_iscsi_pg_map *
528 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
529 			  struct spdk_iscsi_portal_grp *pg)
530 {
531 	struct spdk_iscsi_pg_map *pg_map;
532 	char port_name[MAX_TMPBUF];
533 	int rc;
534 
535 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
536 		return NULL;
537 	}
538 
539 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
540 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
541 			    SPDK_SCSI_DEV_MAX_PORTS);
542 		return NULL;
543 	}
544 
545 	pg_map = malloc(sizeof(*pg_map));
546 	if (pg_map == NULL) {
547 		return NULL;
548 	}
549 
550 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
551 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
552 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
553 	if (rc != 0) {
554 		free(pg_map);
555 		return NULL;
556 	}
557 
558 	TAILQ_INIT(&pg_map->ig_map_head);
559 	pg_map->num_ig_maps = 0;
560 	pg->ref++;
561 	pg_map->pg = pg;
562 	target->num_pg_maps++;
563 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
564 
565 	return pg_map;
566 }
567 
568 static void
569 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
570 			      struct spdk_iscsi_pg_map *pg_map)
571 {
572 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
573 	target->num_pg_maps--;
574 	pg_map->pg->ref--;
575 
576 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
577 
578 	free(pg_map);
579 }
580 
581 static int
582 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
583 			     struct spdk_iscsi_portal_grp *pg)
584 {
585 	struct spdk_iscsi_pg_map *pg_map;
586 
587 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
588 	if (pg_map == NULL) {
589 		return -ENOENT;
590 	}
591 
592 	if (pg_map->num_ig_maps > 0) {
593 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n",
594 			      pg_map->num_ig_maps);
595 	}
596 
597 	iscsi_pg_map_delete_all_ig_maps(pg_map);
598 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
599 	return 0;
600 }
601 
602 static void
603 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
604 			      struct spdk_iscsi_init_grp *ig)
605 {
606 	struct spdk_iscsi_pg_map *pg_map, *tmp;
607 
608 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
609 		iscsi_pg_map_delete_ig_map(pg_map, ig);
610 		if (pg_map->num_ig_maps == 0) {
611 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
612 		}
613 	}
614 }
615 
616 static void
617 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
618 {
619 	struct spdk_iscsi_pg_map *pg_map, *tmp;
620 
621 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
622 		iscsi_pg_map_delete_all_ig_maps(pg_map);
623 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
624 	}
625 }
626 
627 static void
628 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target)
629 {
630 	if (target == NULL) {
631 		return;
632 	}
633 
634 	if (target->destructed) {
635 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
636 		return;
637 	}
638 
639 	target->destructed = true;
640 
641 	spdk_scsi_dev_destruct(target->dev);
642 
643 	free(target->name);
644 	free(target->alias);
645 
646 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
647 	iscsi_tgt_node_delete_all_pg_maps(target);
648 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
649 
650 	pthread_mutex_destroy(&target->mutex);
651 	free(target);
652 }
653 
654 static int
655 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
656 				int pg_tag, int ig_tag)
657 {
658 	struct spdk_iscsi_portal_grp	*pg;
659 	struct spdk_iscsi_init_grp	*ig;
660 	struct spdk_iscsi_pg_map	*pg_map;
661 	struct spdk_iscsi_ig_map	*ig_map;
662 
663 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
664 	if (pg == NULL) {
665 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
666 		return -ENOENT;
667 	}
668 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
669 	if (ig == NULL) {
670 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
671 		return -ENOENT;
672 	}
673 
674 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
675 	if (pg_map == NULL) {
676 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
677 		return -ENOENT;
678 	}
679 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
680 	if (ig_map == NULL) {
681 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
682 		return -ENOENT;
683 	}
684 
685 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
686 	if (pg_map->num_ig_maps == 0) {
687 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
688 	}
689 
690 	return 0;
691 }
692 
693 static int
694 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
695 			     int pg_tag, int ig_tag)
696 {
697 	struct spdk_iscsi_portal_grp	*pg;
698 	struct spdk_iscsi_pg_map	*pg_map;
699 	struct spdk_iscsi_init_grp	*ig;
700 	struct spdk_iscsi_ig_map	*ig_map;
701 	bool				new_pg_map = false;
702 
703 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
704 	if (pg == NULL) {
705 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
706 		return -ENOENT;
707 	}
708 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
709 	if (ig == NULL) {
710 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
711 		return -ENOENT;
712 	}
713 
714 	/* get existing pg_map or create new pg_map and add it to target */
715 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
716 	if (pg_map == NULL) {
717 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
718 		if (pg_map == NULL) {
719 			goto failed;
720 		}
721 		new_pg_map = true;
722 	}
723 
724 	/* create new ig_map and add it to pg_map */
725 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
726 	if (ig_map == NULL) {
727 		goto failed;
728 	}
729 
730 	return 0;
731 
732 failed:
733 	if (new_pg_map) {
734 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
735 	}
736 
737 	return -1;
738 }
739 
740 int
741 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
742 				   int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
743 {
744 	uint16_t i;
745 	int rc;
746 
747 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
748 	for (i = 0; i < num_maps; i++) {
749 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
750 						  ig_tag_list[i]);
751 		if (rc != 0) {
752 			SPDK_ERRLOG("could not add map to target\n");
753 			goto invalid;
754 		}
755 	}
756 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
757 	return 0;
758 
759 invalid:
760 	for (; i > 0; --i) {
761 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
762 						ig_tag_list[i - 1]);
763 	}
764 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
765 	return -1;
766 }
767 
768 int
769 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
770 				      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
771 {
772 	uint16_t i;
773 	int rc;
774 
775 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
776 	for (i = 0; i < num_maps; i++) {
777 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
778 						     ig_tag_list[i]);
779 		if (rc != 0) {
780 			SPDK_ERRLOG("could not delete map from target\n");
781 			goto invalid;
782 		}
783 	}
784 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
785 	return 0;
786 
787 invalid:
788 	for (; i > 0; --i) {
789 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
790 						  ig_tag_list[i - 1]);
791 		if (rc != 0) {
792 			iscsi_tgt_node_delete_all_pg_maps(target);
793 			break;
794 		}
795 	}
796 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
797 	return -1;
798 }
799 
800 static int
801 check_iscsi_name(const char *name)
802 {
803 	const unsigned char *up = (const unsigned char *) name;
804 	size_t n;
805 
806 	/* valid iSCSI name no larger than 223 bytes */
807 	if (strlen(name) > MAX_TARGET_NAME) {
808 		return -1;
809 	}
810 
811 	/* valid iSCSI name? */
812 	for (n = 0; up[n] != 0; n++) {
813 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
814 			return -1;
815 		}
816 		if (up[n] == 0x2fU) {
817 			return -1;
818 		}
819 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
820 			return -1;
821 		}
822 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
823 			return -1;
824 		}
825 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
826 			return -1;
827 		}
828 		if (isspace(up[n])) {
829 			return -1;
830 		}
831 	}
832 	/* valid format? */
833 	if (strncasecmp(name, "iqn.", 4) == 0) {
834 		/* iqn.YYYY-MM.reversed.domain.name */
835 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
836 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
837 		    || !isdigit(up[10]) || up[11] != '.') {
838 			SPDK_ERRLOG("invalid iqn format. "
839 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
840 			return -1;
841 		}
842 	} else if (strncasecmp(name, "eui.", 4) == 0) {
843 		/* EUI-64 -> 16bytes */
844 		/* XXX */
845 	} else if (strncasecmp(name, "naa.", 4) == 0) {
846 		/* 64bit -> 16bytes, 128bit -> 32bytes */
847 		/* XXX */
848 	}
849 	/* OK */
850 	return 0;
851 }
852 
853 bool
854 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
855 {
856 	if (group < 0) {
857 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
858 		return false;
859 	}
860 	if ((!disable && !require && !mutual) ||	/* Auto */
861 	    (disable && !require && !mutual) ||	/* None */
862 	    (!disable && require && !mutual) ||	/* CHAP */
863 	    (!disable && require && mutual)) {	/* CHAP Mutual */
864 		return true;
865 	}
866 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
867 		    disable, require, mutual);
868 	return false;
869 }
870 
871 _spdk_iscsi_tgt_node *
872 spdk_iscsi_tgt_node_construct(int target_index,
873 			      const char *name, const char *alias,
874 			      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
875 			      const char *bdev_name_list[], int *lun_id_list, int num_luns,
876 			      int queue_depth,
877 			      bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
878 			      bool header_digest, bool data_digest)
879 {
880 	char				fullname[MAX_TMPBUF];
881 	struct spdk_iscsi_tgt_node	*target;
882 	int				rc;
883 
884 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
885 					  mutual_chap, chap_group)) {
886 		return NULL;
887 	}
888 
889 	if (num_maps == 0) {
890 		SPDK_ERRLOG("num_maps = 0\n");
891 		return NULL;
892 	}
893 
894 	if (name == NULL) {
895 		SPDK_ERRLOG("TargetName not found\n");
896 		return NULL;
897 	}
898 
899 	if (strncasecmp(name, "iqn.", 4) != 0
900 	    && strncasecmp(name, "eui.", 4) != 0
901 	    && strncasecmp(name, "naa.", 4) != 0) {
902 		snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name);
903 	} else {
904 		snprintf(fullname, sizeof(fullname), "%s", name);
905 	}
906 
907 	if (check_iscsi_name(fullname) != 0) {
908 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
909 			    name);
910 		return NULL;
911 	}
912 
913 	target = malloc(sizeof(*target));
914 	if (!target) {
915 		SPDK_ERRLOG("could not allocate target\n");
916 		return NULL;
917 	}
918 
919 	memset(target, 0, sizeof(*target));
920 
921 	rc = pthread_mutex_init(&target->mutex, NULL);
922 	if (rc != 0) {
923 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
924 		iscsi_tgt_node_destruct(target);
925 		return NULL;
926 	}
927 
928 	target->num = target_index;
929 
930 	target->name = strdup(fullname);
931 	if (!target->name) {
932 		SPDK_ERRLOG("Could not allocate TargetName\n");
933 		iscsi_tgt_node_destruct(target);
934 		return NULL;
935 	}
936 
937 	if (alias == NULL) {
938 		target->alias = NULL;
939 	} else {
940 		target->alias = strdup(alias);
941 		if (!target->alias) {
942 			SPDK_ERRLOG("Could not allocate TargetAlias\n");
943 			iscsi_tgt_node_destruct(target);
944 			return NULL;
945 		}
946 	}
947 
948 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
949 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
950 	if (!target->dev) {
951 		SPDK_ERRLOG("Could not construct SCSI device\n");
952 		iscsi_tgt_node_destruct(target);
953 		return NULL;
954 	}
955 
956 	TAILQ_INIT(&target->pg_map_head);
957 	rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps);
958 	if (rc != 0) {
959 		SPDK_ERRLOG("could not add map to target\n");
960 		iscsi_tgt_node_destruct(target);
961 		return NULL;
962 	}
963 
964 	target->disable_chap = disable_chap;
965 	target->require_chap = require_chap;
966 	target->mutual_chap = mutual_chap;
967 	target->chap_group = chap_group;
968 	target->header_digest = header_digest;
969 	target->data_digest = data_digest;
970 
971 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
972 		target->queue_depth = queue_depth;
973 	} else {
974 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
975 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
976 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
977 	}
978 
979 	rc = iscsi_tgt_node_register(target);
980 	if (rc != 0) {
981 		SPDK_ERRLOG("register target is failed\n");
982 		iscsi_tgt_node_destruct(target);
983 		return NULL;
984 	}
985 
986 	return target;
987 }
988 
989 static int
990 iscsi_parse_tgt_node(struct spdk_conf_section *sp)
991 {
992 	char buf[MAX_TMPBUF];
993 	struct spdk_iscsi_tgt_node *target;
994 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
995 	int num_target_maps;
996 	const char *alias, *pg_tag, *ig_tag;
997 	const char *ag_tag;
998 	const char *val, *name;
999 	int target_num, chap_group, pg_tag_i, ig_tag_i;
1000 	bool header_digest, data_digest;
1001 	bool disable_chap, require_chap, mutual_chap;
1002 	int i;
1003 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
1004 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
1005 	int num_luns, queue_depth;
1006 
1007 	target_num = spdk_conf_section_get_num(sp);
1008 
1009 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
1010 
1011 	data_digest = false;
1012 	header_digest = false;
1013 
1014 	name = spdk_conf_section_get_val(sp, "TargetName");
1015 
1016 	if (name == NULL) {
1017 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1018 		return -1;
1019 	}
1020 
1021 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1022 
1023 	/* Setup initiator and portal group mapping */
1024 	val = spdk_conf_section_get_val(sp, "Mapping");
1025 	if (val == NULL) {
1026 		/* no map */
1027 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1028 		return -1;
1029 	}
1030 
1031 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1032 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1033 		if (val == NULL) {
1034 			break;
1035 		}
1036 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1037 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1038 		if (pg_tag == NULL || ig_tag == NULL) {
1039 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1040 			return -1;
1041 		}
1042 		if (strncasecmp(pg_tag, "PortalGroup",
1043 				strlen("PortalGroup")) != 0
1044 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1045 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1046 			return -1;
1047 		}
1048 		if (strncasecmp(ig_tag, "InitiatorGroup",
1049 				strlen("InitiatorGroup")) != 0
1050 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1051 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1052 			return -1;
1053 		}
1054 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1055 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1056 			return -1;
1057 		}
1058 		pg_tag_list[i] = pg_tag_i;
1059 		ig_tag_list[i] = ig_tag_i;
1060 	}
1061 
1062 	num_target_maps = i;
1063 
1064 	/* Setup AuthMethod */
1065 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1066 	disable_chap = false;
1067 	require_chap = false;
1068 	mutual_chap = false;
1069 	if (val != NULL) {
1070 		for (i = 0; ; i++) {
1071 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1072 			if (val == NULL) {
1073 				break;
1074 			}
1075 			if (strcasecmp(val, "CHAP") == 0) {
1076 				require_chap = true;
1077 			} else if (strcasecmp(val, "Mutual") == 0) {
1078 				mutual_chap = true;
1079 			} else if (strcasecmp(val, "Auto") == 0) {
1080 				disable_chap = false;
1081 				require_chap = false;
1082 				mutual_chap = false;
1083 			} else if (strcasecmp(val, "None") == 0) {
1084 				disable_chap = true;
1085 				require_chap = false;
1086 				mutual_chap = false;
1087 			} else {
1088 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1089 				return -1;
1090 			}
1091 		}
1092 		if (mutual_chap && !require_chap) {
1093 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1094 			return -1;
1095 		}
1096 	}
1097 	if (disable_chap) {
1098 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1099 	} else if (!require_chap) {
1100 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1101 	} else {
1102 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1103 			      mutual_chap ? "Mutual" : "");
1104 	}
1105 
1106 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1107 	if (val == NULL) {
1108 		chap_group = 0;
1109 	} else {
1110 		ag_tag = val;
1111 		if (strcasecmp(ag_tag, "None") == 0) {
1112 			chap_group = 0;
1113 		} else {
1114 			if (strncasecmp(ag_tag, "AuthGroup",
1115 					strlen("AuthGroup")) != 0
1116 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1117 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1118 				return -1;
1119 			}
1120 			if (chap_group == 0) {
1121 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1122 				return -1;
1123 			}
1124 		}
1125 	}
1126 	if (chap_group == 0) {
1127 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1128 	} else {
1129 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1130 	}
1131 
1132 	val = spdk_conf_section_get_val(sp, "UseDigest");
1133 	if (val != NULL) {
1134 		for (i = 0; ; i++) {
1135 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1136 			if (val == NULL) {
1137 				break;
1138 			}
1139 			if (strcasecmp(val, "Header") == 0) {
1140 				header_digest = true;
1141 			} else if (strcasecmp(val, "Data") == 0) {
1142 				data_digest = true;
1143 			} else if (strcasecmp(val, "Auto") == 0) {
1144 				header_digest = false;
1145 				data_digest = false;
1146 			} else {
1147 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1148 				return -1;
1149 			}
1150 		}
1151 	}
1152 	if (!header_digest && !data_digest) {
1153 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1154 	} else {
1155 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1156 			      header_digest ? "Header" : "",
1157 			      data_digest ? "Data" : "");
1158 	}
1159 
1160 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1161 	if (val == NULL) {
1162 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1163 	} else {
1164 		queue_depth = (int) strtol(val, NULL, 10);
1165 	}
1166 
1167 	num_luns = 0;
1168 
1169 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1170 		snprintf(buf, sizeof(buf), "LUN%d", i);
1171 		val = spdk_conf_section_get_val(sp, buf);
1172 		if (val == NULL) {
1173 			continue;
1174 		}
1175 
1176 		bdev_name_list[num_luns] = val;
1177 		lun_id_list[num_luns] = i;
1178 		num_luns++;
1179 	}
1180 
1181 	if (num_luns == 0) {
1182 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1183 		return -1;
1184 	}
1185 
1186 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1187 					       pg_tag_list, ig_tag_list, num_target_maps,
1188 					       bdev_name_list, lun_id_list, num_luns, queue_depth,
1189 					       disable_chap, require_chap, mutual_chap, chap_group,
1190 					       header_digest, data_digest);
1191 
1192 	if (target == NULL) {
1193 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1194 		return -1;
1195 	}
1196 
1197 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1198 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1199 
1200 		if (lun) {
1201 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1202 				     spdk_scsi_dev_get_id(target->dev),
1203 				     spdk_scsi_lun_get_id(lun),
1204 				     spdk_scsi_lun_get_bdev_name(lun));
1205 		}
1206 	}
1207 
1208 	return 0;
1209 }
1210 
1211 int spdk_iscsi_parse_tgt_nodes(void)
1212 {
1213 	struct spdk_conf_section *sp;
1214 	int rc;
1215 
1216 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n");
1217 
1218 	sp = spdk_conf_first_section(NULL);
1219 	while (sp != NULL) {
1220 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1221 			int tag = spdk_conf_section_get_num(sp);
1222 
1223 			if (tag > SPDK_TN_TAG_MAX) {
1224 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1225 				return -1;
1226 			}
1227 			rc = iscsi_parse_tgt_node(sp);
1228 			if (rc < 0) {
1229 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1230 				return -1;
1231 			}
1232 		}
1233 		sp = spdk_conf_next_section(sp);
1234 	}
1235 	return 0;
1236 }
1237 
1238 void
1239 spdk_iscsi_shutdown_tgt_nodes(void)
1240 {
1241 	struct spdk_iscsi_tgt_node *target;
1242 
1243 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1244 	while (!TAILQ_EMPTY(&g_spdk_iscsi.target_head)) {
1245 		target = TAILQ_FIRST(&g_spdk_iscsi.target_head);
1246 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1247 
1248 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1249 
1250 		iscsi_tgt_node_destruct(target);
1251 
1252 		pthread_mutex_lock(&g_spdk_iscsi.mutex);
1253 	}
1254 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1255 }
1256 
1257 int
1258 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name)
1259 {
1260 	struct spdk_iscsi_tgt_node *target;
1261 
1262 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1263 	target = spdk_iscsi_find_tgt_node(target_name);
1264 	if (target != NULL) {
1265 		iscsi_tgt_node_unregister(target);
1266 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1267 
1268 		iscsi_tgt_node_destruct(target);
1269 
1270 		return 0;
1271 	}
1272 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1273 
1274 	return -ENOENT;
1275 }
1276 
1277 bool
1278 spdk_iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1279 {
1280 	return target->destructed;
1281 }
1282 
1283 int
1284 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1285 				 struct spdk_iscsi_tgt_node *target)
1286 {
1287 	int i;
1288 	struct spdk_iscsi_task *task;
1289 
1290 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1291 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1292 
1293 		if (!lun) {
1294 			continue;
1295 		}
1296 
1297 		/* we create a fake management task per LUN to cleanup */
1298 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1299 		if (!task) {
1300 			SPDK_ERRLOG("Unable to acquire task\n");
1301 			return -1;
1302 		}
1303 
1304 		task->scsi.target_port = conn->target_port;
1305 		task->scsi.initiator_port = conn->initiator_port;
1306 		task->scsi.lun = lun;
1307 
1308 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1309 	}
1310 
1311 	return 0;
1312 }
1313 
1314 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1315 				    struct spdk_iscsi_init_grp *initiator_group)
1316 {
1317 	struct spdk_iscsi_tgt_node *target;
1318 
1319 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1320 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1321 		if (portal_group) {
1322 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1323 		}
1324 		if (initiator_group) {
1325 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1326 		}
1327 	}
1328 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1329 }
1330 
1331 int
1332 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1333 			    const char *bdev_name, int lun_id)
1334 {
1335 	struct spdk_scsi_dev *dev;
1336 	int rc;
1337 
1338 	if (target->num_active_conns > 0) {
1339 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1340 			    target->num_active_conns);
1341 		return -1;
1342 	}
1343 
1344 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1345 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1346 		return -1;
1347 	}
1348 
1349 	dev = target->dev;
1350 	if (dev == NULL) {
1351 		SPDK_ERRLOG("SCSI device is not found\n");
1352 		return -1;
1353 	}
1354 
1355 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1356 	if (rc != 0) {
1357 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1358 		return -1;
1359 	}
1360 
1361 	return 0;
1362 }
1363 
1364 int
1365 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1366 				    bool disable_chap, bool require_chap,
1367 				    bool mutual_chap, int32_t chap_group)
1368 {
1369 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
1370 					  mutual_chap, chap_group)) {
1371 		return -EINVAL;
1372 	}
1373 
1374 	pthread_mutex_lock(&target->mutex);
1375 	target->disable_chap = disable_chap;
1376 	target->require_chap = require_chap;
1377 	target->mutual_chap = mutual_chap;
1378 	target->chap_group = chap_group;
1379 	pthread_mutex_unlock(&target->mutex);
1380 
1381 	return 0;
1382 }
1383 
1384 static const char *target_nodes_section = \
1385 		"\n"
1386 		"# Users should change the TargetNode section(s) below to match the\n"
1387 		"#  desired iSCSI target node configuration.\n"
1388 		"# TargetName, Mapping, LUN0 are minimum required\n";
1389 
1390 #define TARGET_NODE_TMPL \
1391 "[TargetNode%d]\n" \
1392 "  Comment \"Target%d\"\n" \
1393 "  TargetName %s\n" \
1394 "  TargetAlias \"%s\"\n"
1395 
1396 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1397 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1398 
1399 #define TARGET_NODE_AUTH_TMPL \
1400 "  AuthMethod %s\n" \
1401 "  AuthGroup %s\n" \
1402 "  UseDigest %s\n"
1403 
1404 #define TARGET_NODE_QD_TMPL \
1405 "  QueueDepth %d\n\n"
1406 
1407 #define TARGET_NODE_LUN_TMPL \
1408 "  LUN%d %s\n"
1409 
1410 void
1411 spdk_iscsi_tgt_nodes_config_text(FILE *fp)
1412 {
1413 	int l = 0;
1414 	struct spdk_scsi_dev *dev = NULL;
1415 	struct spdk_iscsi_tgt_node *target = NULL;
1416 	struct spdk_iscsi_pg_map *pg_map;
1417 	struct spdk_iscsi_ig_map *ig_map;
1418 
1419 	/* Create target nodes section */
1420 	fprintf(fp, "%s", target_nodes_section);
1421 
1422 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1423 		int idx;
1424 		const char *authmethod = "None";
1425 		char authgroup[32] = "None";
1426 		const char *usedigest = "Auto";
1427 
1428 		dev = target->dev;
1429 		if (NULL == dev) { continue; }
1430 
1431 		idx = target->num;
1432 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1433 
1434 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1435 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1436 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1437 					pg_map->pg->tag,
1438 					ig_map->ig->tag);
1439 			}
1440 		}
1441 
1442 		if (target->disable_chap) {
1443 			authmethod = "None";
1444 		} else if (!target->require_chap) {
1445 			authmethod = "Auto";
1446 		} else if (target->mutual_chap) {
1447 			authmethod = "CHAP Mutual";
1448 		} else {
1449 			authmethod = "CHAP";
1450 		}
1451 
1452 		if (target->chap_group > 0) {
1453 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1454 		}
1455 
1456 		if (target->header_digest) {
1457 			usedigest = "Header";
1458 		} else if (target->data_digest) {
1459 			usedigest = "Data";
1460 		}
1461 
1462 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1463 			authmethod, authgroup, usedigest);
1464 
1465 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1466 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1467 
1468 			if (!lun) {
1469 				continue;
1470 			}
1471 
1472 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1473 				spdk_scsi_lun_get_id(lun),
1474 				spdk_scsi_lun_get_bdev_name(lun));
1475 		}
1476 
1477 		fprintf(fp, TARGET_NODE_QD_TMPL,
1478 			target->queue_depth);
1479 	}
1480 }
1481 
1482 static void
1483 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1484 			 struct spdk_json_write_ctx *w)
1485 {
1486 	struct spdk_iscsi_pg_map *pg_map;
1487 	struct spdk_iscsi_ig_map *ig_map;
1488 	int i;
1489 
1490 	spdk_json_write_object_begin(w);
1491 
1492 	spdk_json_write_named_string(w, "name", target->name);
1493 
1494 	if (target->alias) {
1495 		spdk_json_write_named_string(w, "alias_name", target->alias);
1496 	}
1497 
1498 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1499 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1500 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1501 			spdk_json_write_object_begin(w);
1502 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1503 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1504 			spdk_json_write_object_end(w);
1505 		}
1506 	}
1507 	spdk_json_write_array_end(w);
1508 
1509 	spdk_json_write_named_array_begin(w, "luns");
1510 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1511 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1512 
1513 		if (lun) {
1514 			spdk_json_write_object_begin(w);
1515 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1516 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1517 			spdk_json_write_object_end(w);
1518 		}
1519 	}
1520 	spdk_json_write_array_end(w);
1521 
1522 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1523 
1524 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1525 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1526 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1527 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1528 
1529 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1530 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1531 
1532 	spdk_json_write_object_end(w);
1533 }
1534 
1535 static void
1536 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1537 			   struct spdk_json_write_ctx *w)
1538 {
1539 	spdk_json_write_object_begin(w);
1540 
1541 	spdk_json_write_named_string(w, "method", "construct_target_node");
1542 
1543 	spdk_json_write_name(w, "params");
1544 	iscsi_tgt_node_info_json(target, w);
1545 
1546 	spdk_json_write_object_end(w);
1547 }
1548 
1549 void
1550 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1551 {
1552 	struct spdk_iscsi_tgt_node *target;
1553 
1554 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1555 		iscsi_tgt_node_info_json(target, w);
1556 	}
1557 }
1558 
1559 void
1560 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1561 {
1562 	struct spdk_iscsi_tgt_node *target;
1563 
1564 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1565 		iscsi_tgt_node_config_json(target, w);
1566 	}
1567 }
1568