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