xref: /spdk/lib/iscsi/tgt_node.c (revision 552e21cce6cccbf833ed9109827e08337377d7ce)
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 spdk_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 spdk_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 spdk_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 (spdk_iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
177 			return true;
178 		}
179 	} else {
180 		/* IPv4 */
181 		if (spdk_iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
182 			return true;
183 		}
184 	}
185 	return false;
186 }
187 
188 static bool
189 spdk_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 (spdk_iscsi_netmask_allow_addr(imask->mask, addr)) {
198 			return true;
199 		}
200 	}
201 	return false;
202 }
203 
204 static int
205 spdk_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 spdk_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 = spdk_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 = spdk_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 (spdk_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 spdk_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 = spdk_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 = spdk_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 spdk_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 spdk_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 spdk_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 spdk_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 (spdk_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 _spdk_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 spdk_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 = spdk_iscsi_pg_map_find_ig_map(pg_map, ig);
494 	if (ig_map == NULL) {
495 		return -ENOENT;
496 	}
497 
498 	_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
499 	return 0;
500 }
501 
502 static void
503 spdk_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 		_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
509 	}
510 }
511 
512 static struct spdk_iscsi_pg_map *
513 spdk_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 spdk_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 (spdk_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 _spdk_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 spdk_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 = spdk_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 	spdk_iscsi_pg_map_delete_all_ig_maps(pg_map);
598 	_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
599 	return 0;
600 }
601 
602 static void
603 spdk_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 		spdk_iscsi_pg_map_delete_ig_map(pg_map, ig);
610 		if (pg_map->num_ig_maps == 0) {
611 			_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
612 		}
613 	}
614 }
615 
616 static void
617 spdk_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 		spdk_iscsi_pg_map_delete_all_ig_maps(pg_map);
623 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
624 	}
625 }
626 
627 static void
628 spdk_iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target)
629 {
630 	if (target == NULL) {
631 		return;
632 	}
633 
634 	free(target->name);
635 	free(target->alias);
636 	spdk_iscsi_tgt_node_delete_all_pg_maps(target);
637 	spdk_scsi_dev_destruct(target->dev);
638 
639 	pthread_mutex_destroy(&target->mutex);
640 	free(target);
641 }
642 
643 static int
644 spdk_iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
645 				     int pg_tag, int ig_tag)
646 {
647 	struct spdk_iscsi_portal_grp	*pg;
648 	struct spdk_iscsi_init_grp	*ig;
649 	struct spdk_iscsi_pg_map	*pg_map;
650 	struct spdk_iscsi_ig_map	*ig_map;
651 
652 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
653 	if (pg == NULL) {
654 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
655 		return -ENOENT;
656 	}
657 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
658 	if (ig == NULL) {
659 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
660 		return -ENOENT;
661 	}
662 
663 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
664 	if (pg_map == NULL) {
665 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
666 		return -ENOENT;
667 	}
668 	ig_map = spdk_iscsi_pg_map_find_ig_map(pg_map, ig);
669 	if (ig_map == NULL) {
670 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
671 		return -ENOENT;
672 	}
673 
674 	_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
675 	if (pg_map->num_ig_maps == 0) {
676 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
677 	}
678 
679 	return 0;
680 }
681 
682 static int
683 spdk_iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
684 				  int pg_tag, int ig_tag)
685 {
686 	struct spdk_iscsi_portal_grp	*pg;
687 	struct spdk_iscsi_pg_map	*pg_map;
688 	struct spdk_iscsi_init_grp	*ig;
689 	struct spdk_iscsi_ig_map	*ig_map;
690 	bool				new_pg_map = false;
691 
692 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
693 	if (pg == NULL) {
694 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
695 		return -ENOENT;
696 	}
697 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
698 	if (ig == NULL) {
699 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
700 		return -ENOENT;
701 	}
702 
703 	/* get existing pg_map or create new pg_map and add it to target */
704 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
705 	if (pg_map == NULL) {
706 		pg_map = spdk_iscsi_tgt_node_add_pg_map(target, pg);
707 		if (pg_map == NULL) {
708 			goto failed;
709 		}
710 		new_pg_map = true;
711 	}
712 
713 	/* create new ig_map and add it to pg_map */
714 	ig_map = spdk_iscsi_pg_map_add_ig_map(pg_map, ig);
715 	if (ig_map == NULL) {
716 		goto failed;
717 	}
718 
719 	return 0;
720 
721 failed:
722 	if (new_pg_map) {
723 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
724 	}
725 
726 	return -1;
727 }
728 
729 int
730 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
731 				   int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
732 {
733 	uint16_t i;
734 	int rc;
735 
736 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
737 	for (i = 0; i < num_maps; i++) {
738 		rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
739 						       ig_tag_list[i]);
740 		if (rc != 0) {
741 			SPDK_ERRLOG("could not add map to target\n");
742 			goto invalid;
743 		}
744 	}
745 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
746 	return 0;
747 
748 invalid:
749 	for (; i > 0; --i) {
750 		spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
751 						     ig_tag_list[i - 1]);
752 	}
753 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
754 	return -1;
755 }
756 
757 int
758 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
759 				      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
760 {
761 	uint16_t i;
762 	int rc;
763 
764 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
765 	for (i = 0; i < num_maps; i++) {
766 		rc = spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
767 				ig_tag_list[i]);
768 		if (rc != 0) {
769 			SPDK_ERRLOG("could not delete map from target\n");
770 			goto invalid;
771 		}
772 	}
773 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
774 	return 0;
775 
776 invalid:
777 	for (; i > 0; --i) {
778 		rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
779 						       ig_tag_list[i - 1]);
780 		if (rc != 0) {
781 			spdk_iscsi_tgt_node_delete_all_pg_maps(target);
782 			break;
783 		}
784 	}
785 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
786 	return -1;
787 }
788 
789 static int
790 spdk_check_iscsi_name(const char *name)
791 {
792 	const unsigned char *up = (const unsigned char *) name;
793 	size_t n;
794 
795 	/* valid iSCSI name no larger than 223 bytes */
796 	if (strlen(name) > MAX_TARGET_NAME) {
797 		return -1;
798 	}
799 
800 	/* valid iSCSI name? */
801 	for (n = 0; up[n] != 0; n++) {
802 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
803 			return -1;
804 		}
805 		if (up[n] == 0x2fU) {
806 			return -1;
807 		}
808 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
809 			return -1;
810 		}
811 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
812 			return -1;
813 		}
814 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
815 			return -1;
816 		}
817 		if (isspace(up[n])) {
818 			return -1;
819 		}
820 	}
821 	/* valid format? */
822 	if (strncasecmp(name, "iqn.", 4) == 0) {
823 		/* iqn.YYYY-MM.reversed.domain.name */
824 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
825 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
826 		    || !isdigit(up[10]) || up[11] != '.') {
827 			SPDK_ERRLOG("invalid iqn format. "
828 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
829 			return -1;
830 		}
831 	} else if (strncasecmp(name, "eui.", 4) == 0) {
832 		/* EUI-64 -> 16bytes */
833 		/* XXX */
834 	} else if (strncasecmp(name, "naa.", 4) == 0) {
835 		/* 64bit -> 16bytes, 128bit -> 32bytes */
836 		/* XXX */
837 	}
838 	/* OK */
839 	return 0;
840 }
841 
842 bool
843 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
844 {
845 	if (group < 0) {
846 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
847 		return false;
848 	}
849 	if ((!disable && !require && !mutual) ||	/* Auto */
850 	    (disable && !require && !mutual) ||	/* None */
851 	    (!disable && require && !mutual) ||	/* CHAP */
852 	    (!disable && require && mutual)) {	/* CHAP Mutual */
853 		return true;
854 	}
855 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
856 		    disable, require, mutual);
857 	return false;
858 }
859 
860 _spdk_iscsi_tgt_node *
861 spdk_iscsi_tgt_node_construct(int target_index,
862 			      const char *name, const char *alias,
863 			      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
864 			      const char *bdev_name_list[], int *lun_id_list, int num_luns,
865 			      int queue_depth,
866 			      bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
867 			      bool header_digest, bool data_digest)
868 {
869 	char				fullname[MAX_TMPBUF];
870 	struct spdk_iscsi_tgt_node	*target;
871 	int				rc;
872 
873 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
874 					  mutual_chap, chap_group)) {
875 		return NULL;
876 	}
877 
878 	if (num_maps == 0) {
879 		SPDK_ERRLOG("num_maps = 0\n");
880 		return NULL;
881 	}
882 
883 	if (name == NULL) {
884 		SPDK_ERRLOG("TargetName not found\n");
885 		return NULL;
886 	}
887 
888 	if (strncasecmp(name, "iqn.", 4) != 0
889 	    && strncasecmp(name, "eui.", 4) != 0
890 	    && strncasecmp(name, "naa.", 4) != 0) {
891 		snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name);
892 	} else {
893 		snprintf(fullname, sizeof(fullname), "%s", name);
894 	}
895 
896 	if (spdk_check_iscsi_name(fullname) != 0) {
897 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
898 			    name);
899 		return NULL;
900 	}
901 
902 	target = malloc(sizeof(*target));
903 	if (!target) {
904 		SPDK_ERRLOG("could not allocate target\n");
905 		return NULL;
906 	}
907 
908 	memset(target, 0, sizeof(*target));
909 
910 	rc = pthread_mutex_init(&target->mutex, NULL);
911 	if (rc != 0) {
912 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
913 		spdk_iscsi_tgt_node_destruct(target);
914 		return NULL;
915 	}
916 
917 	target->num = target_index;
918 
919 	target->name = strdup(fullname);
920 	if (!target->name) {
921 		SPDK_ERRLOG("Could not allocate TargetName\n");
922 		spdk_iscsi_tgt_node_destruct(target);
923 		return NULL;
924 	}
925 
926 	if (alias == NULL) {
927 		target->alias = NULL;
928 	} else {
929 		target->alias = strdup(alias);
930 		if (!target->alias) {
931 			SPDK_ERRLOG("Could not allocate TargetAlias\n");
932 			spdk_iscsi_tgt_node_destruct(target);
933 			return NULL;
934 		}
935 	}
936 
937 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
938 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
939 	if (!target->dev) {
940 		SPDK_ERRLOG("Could not construct SCSI device\n");
941 		spdk_iscsi_tgt_node_destruct(target);
942 		return NULL;
943 	}
944 
945 	TAILQ_INIT(&target->pg_map_head);
946 	rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps);
947 	if (rc != 0) {
948 		SPDK_ERRLOG("could not add map to target\n");
949 		spdk_iscsi_tgt_node_destruct(target);
950 		return NULL;
951 	}
952 
953 	target->disable_chap = disable_chap;
954 	target->require_chap = require_chap;
955 	target->mutual_chap = mutual_chap;
956 	target->chap_group = chap_group;
957 	target->header_digest = header_digest;
958 	target->data_digest = data_digest;
959 
960 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
961 		target->queue_depth = queue_depth;
962 	} else {
963 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
964 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
965 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
966 	}
967 
968 	rc = spdk_iscsi_tgt_node_register(target);
969 	if (rc != 0) {
970 		SPDK_ERRLOG("register target is failed\n");
971 		spdk_iscsi_tgt_node_destruct(target);
972 		return NULL;
973 	}
974 
975 	return target;
976 }
977 
978 static int
979 spdk_iscsi_parse_tgt_node(struct spdk_conf_section *sp)
980 {
981 	char buf[MAX_TMPBUF];
982 	struct spdk_iscsi_tgt_node *target;
983 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
984 	int num_target_maps;
985 	const char *alias, *pg_tag, *ig_tag;
986 	const char *ag_tag;
987 	const char *val, *name;
988 	int target_num, chap_group, pg_tag_i, ig_tag_i;
989 	bool header_digest, data_digest;
990 	bool disable_chap, require_chap, mutual_chap;
991 	int i;
992 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
993 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
994 	int num_luns, queue_depth;
995 
996 	target_num = spdk_conf_section_get_num(sp);
997 
998 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
999 
1000 	data_digest = false;
1001 	header_digest = false;
1002 
1003 	name = spdk_conf_section_get_val(sp, "TargetName");
1004 
1005 	if (name == NULL) {
1006 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1007 		return -1;
1008 	}
1009 
1010 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1011 
1012 	/* Setup initiator and portal group mapping */
1013 	val = spdk_conf_section_get_val(sp, "Mapping");
1014 	if (val == NULL) {
1015 		/* no map */
1016 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1017 		return -1;
1018 	}
1019 
1020 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1021 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1022 		if (val == NULL) {
1023 			break;
1024 		}
1025 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1026 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1027 		if (pg_tag == NULL || ig_tag == NULL) {
1028 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1029 			return -1;
1030 		}
1031 		if (strncasecmp(pg_tag, "PortalGroup",
1032 				strlen("PortalGroup")) != 0
1033 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1034 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1035 			return -1;
1036 		}
1037 		if (strncasecmp(ig_tag, "InitiatorGroup",
1038 				strlen("InitiatorGroup")) != 0
1039 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1040 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1041 			return -1;
1042 		}
1043 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1044 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1045 			return -1;
1046 		}
1047 		pg_tag_list[i] = pg_tag_i;
1048 		ig_tag_list[i] = ig_tag_i;
1049 	}
1050 
1051 	num_target_maps = i;
1052 
1053 	/* Setup AuthMethod */
1054 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1055 	disable_chap = false;
1056 	require_chap = false;
1057 	mutual_chap = false;
1058 	if (val != NULL) {
1059 		for (i = 0; ; i++) {
1060 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1061 			if (val == NULL) {
1062 				break;
1063 			}
1064 			if (strcasecmp(val, "CHAP") == 0) {
1065 				require_chap = true;
1066 			} else if (strcasecmp(val, "Mutual") == 0) {
1067 				mutual_chap = true;
1068 			} else if (strcasecmp(val, "Auto") == 0) {
1069 				disable_chap = false;
1070 				require_chap = false;
1071 				mutual_chap = false;
1072 			} else if (strcasecmp(val, "None") == 0) {
1073 				disable_chap = true;
1074 				require_chap = false;
1075 				mutual_chap = false;
1076 			} else {
1077 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1078 				return -1;
1079 			}
1080 		}
1081 		if (mutual_chap && !require_chap) {
1082 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1083 			return -1;
1084 		}
1085 	}
1086 	if (disable_chap) {
1087 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1088 	} else if (!require_chap) {
1089 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1090 	} else {
1091 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1092 			      mutual_chap ? "Mutual" : "");
1093 	}
1094 
1095 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1096 	if (val == NULL) {
1097 		chap_group = 0;
1098 	} else {
1099 		ag_tag = val;
1100 		if (strcasecmp(ag_tag, "None") == 0) {
1101 			chap_group = 0;
1102 		} else {
1103 			if (strncasecmp(ag_tag, "AuthGroup",
1104 					strlen("AuthGroup")) != 0
1105 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1106 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1107 				return -1;
1108 			}
1109 			if (chap_group == 0) {
1110 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1111 				return -1;
1112 			}
1113 		}
1114 	}
1115 	if (chap_group == 0) {
1116 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1117 	} else {
1118 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1119 	}
1120 
1121 	val = spdk_conf_section_get_val(sp, "UseDigest");
1122 	if (val != NULL) {
1123 		for (i = 0; ; i++) {
1124 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1125 			if (val == NULL) {
1126 				break;
1127 			}
1128 			if (strcasecmp(val, "Header") == 0) {
1129 				header_digest = true;
1130 			} else if (strcasecmp(val, "Data") == 0) {
1131 				data_digest = true;
1132 			} else if (strcasecmp(val, "Auto") == 0) {
1133 				header_digest = false;
1134 				data_digest = false;
1135 			} else {
1136 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1137 				return -1;
1138 			}
1139 		}
1140 	}
1141 	if (!header_digest && !data_digest) {
1142 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1143 	} else {
1144 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1145 			      header_digest ? "Header" : "",
1146 			      data_digest ? "Data" : "");
1147 	}
1148 
1149 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1150 	if (val == NULL) {
1151 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1152 	} else {
1153 		queue_depth = (int) strtol(val, NULL, 10);
1154 	}
1155 
1156 	num_luns = 0;
1157 
1158 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1159 		snprintf(buf, sizeof(buf), "LUN%d", i);
1160 		val = spdk_conf_section_get_val(sp, buf);
1161 		if (val == NULL) {
1162 			continue;
1163 		}
1164 
1165 		bdev_name_list[num_luns] = val;
1166 		lun_id_list[num_luns] = i;
1167 		num_luns++;
1168 	}
1169 
1170 	if (num_luns == 0) {
1171 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1172 		return -1;
1173 	}
1174 
1175 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1176 					       pg_tag_list, ig_tag_list, num_target_maps,
1177 					       bdev_name_list, lun_id_list, num_luns, queue_depth,
1178 					       disable_chap, require_chap, mutual_chap, chap_group,
1179 					       header_digest, data_digest);
1180 
1181 	if (target == NULL) {
1182 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1183 		return -1;
1184 	}
1185 
1186 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1187 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1188 
1189 		if (lun) {
1190 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1191 				     spdk_scsi_dev_get_id(target->dev),
1192 				     spdk_scsi_lun_get_id(lun),
1193 				     spdk_scsi_lun_get_bdev_name(lun));
1194 		}
1195 	}
1196 
1197 	return 0;
1198 }
1199 
1200 int spdk_iscsi_parse_tgt_nodes(void)
1201 {
1202 	struct spdk_conf_section *sp;
1203 	int rc;
1204 
1205 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n");
1206 
1207 	sp = spdk_conf_first_section(NULL);
1208 	while (sp != NULL) {
1209 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1210 			int tag = spdk_conf_section_get_num(sp);
1211 
1212 			if (tag > SPDK_TN_TAG_MAX) {
1213 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1214 				return -1;
1215 			}
1216 			rc = spdk_iscsi_parse_tgt_node(sp);
1217 			if (rc < 0) {
1218 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1219 				return -1;
1220 			}
1221 		}
1222 		sp = spdk_conf_next_section(sp);
1223 	}
1224 	return 0;
1225 }
1226 
1227 void
1228 spdk_iscsi_shutdown_tgt_nodes(void)
1229 {
1230 	struct spdk_iscsi_tgt_node *target, *tmp;
1231 
1232 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1233 	TAILQ_FOREACH_SAFE(target, &g_spdk_iscsi.target_head, tailq, tmp) {
1234 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1235 		spdk_iscsi_tgt_node_destruct(target);
1236 	}
1237 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1238 }
1239 
1240 int
1241 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name)
1242 {
1243 	struct spdk_iscsi_tgt_node *target;
1244 
1245 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1246 	target = spdk_iscsi_find_tgt_node(target_name);
1247 	if (target != NULL) {
1248 		spdk_iscsi_tgt_node_unregister(target);
1249 		spdk_iscsi_tgt_node_destruct(target);
1250 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1251 
1252 		return 0;
1253 	}
1254 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1255 
1256 	return -ENOENT;
1257 }
1258 
1259 int
1260 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1261 				 struct spdk_iscsi_tgt_node *target)
1262 {
1263 	int i;
1264 	struct spdk_iscsi_task *task;
1265 
1266 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1267 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1268 
1269 		if (!lun) {
1270 			continue;
1271 		}
1272 
1273 		/* we create a fake management task per LUN to cleanup */
1274 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1275 		if (!task) {
1276 			SPDK_ERRLOG("Unable to acquire task\n");
1277 			return -1;
1278 		}
1279 
1280 		task->scsi.target_port = conn->target_port;
1281 		task->scsi.initiator_port = conn->initiator_port;
1282 		task->scsi.lun = lun;
1283 
1284 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1285 	}
1286 
1287 	return 0;
1288 }
1289 
1290 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1291 				    struct spdk_iscsi_init_grp *initiator_group)
1292 {
1293 	struct spdk_iscsi_tgt_node *target;
1294 
1295 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1296 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1297 		if (portal_group) {
1298 			spdk_iscsi_tgt_node_delete_pg_map(target, portal_group);
1299 		}
1300 		if (initiator_group) {
1301 			spdk_iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1302 		}
1303 	}
1304 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1305 }
1306 
1307 int
1308 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1309 			    const char *bdev_name, int lun_id)
1310 {
1311 	struct spdk_scsi_dev *dev;
1312 	int rc;
1313 
1314 	if (target->num_active_conns > 0) {
1315 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1316 			    target->num_active_conns);
1317 		return -1;
1318 	}
1319 
1320 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1321 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1322 		return -1;
1323 	}
1324 
1325 	dev = target->dev;
1326 	if (dev == NULL) {
1327 		SPDK_ERRLOG("SCSI device is not found\n");
1328 		return -1;
1329 	}
1330 
1331 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1332 	if (rc != 0) {
1333 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1334 		return -1;
1335 	}
1336 
1337 	return 0;
1338 }
1339 
1340 int
1341 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1342 				    bool disable_chap, bool require_chap,
1343 				    bool mutual_chap, int32_t chap_group)
1344 {
1345 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
1346 					  mutual_chap, chap_group)) {
1347 		return -EINVAL;
1348 	}
1349 
1350 	pthread_mutex_lock(&target->mutex);
1351 	target->disable_chap = disable_chap;
1352 	target->require_chap = require_chap;
1353 	target->mutual_chap = mutual_chap;
1354 	target->chap_group = chap_group;
1355 	pthread_mutex_unlock(&target->mutex);
1356 
1357 	return 0;
1358 }
1359 
1360 static const char *target_nodes_section = \
1361 		"\n"
1362 		"# Users should change the TargetNode section(s) below to match the\n"
1363 		"#  desired iSCSI target node configuration.\n"
1364 		"# TargetName, Mapping, LUN0 are minimum required\n";
1365 
1366 #define TARGET_NODE_TMPL \
1367 "[TargetNode%d]\n" \
1368 "  Comment \"Target%d\"\n" \
1369 "  TargetName %s\n" \
1370 "  TargetAlias \"%s\"\n"
1371 
1372 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1373 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1374 
1375 #define TARGET_NODE_AUTH_TMPL \
1376 "  AuthMethod %s\n" \
1377 "  AuthGroup %s\n" \
1378 "  UseDigest %s\n"
1379 
1380 #define TARGET_NODE_QD_TMPL \
1381 "  QueueDepth %d\n\n"
1382 
1383 #define TARGET_NODE_LUN_TMPL \
1384 "  LUN%d %s\n"
1385 
1386 void
1387 spdk_iscsi_tgt_nodes_config_text(FILE *fp)
1388 {
1389 	int l = 0;
1390 	struct spdk_scsi_dev *dev = NULL;
1391 	struct spdk_iscsi_tgt_node *target = NULL;
1392 	struct spdk_iscsi_pg_map *pg_map;
1393 	struct spdk_iscsi_ig_map *ig_map;
1394 
1395 	/* Create target nodes section */
1396 	fprintf(fp, "%s", target_nodes_section);
1397 
1398 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1399 		int idx;
1400 		const char *authmethod = "None";
1401 		char authgroup[32] = "None";
1402 		const char *usedigest = "Auto";
1403 
1404 		dev = target->dev;
1405 		if (NULL == dev) { continue; }
1406 
1407 		idx = target->num;
1408 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1409 
1410 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1411 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1412 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1413 					pg_map->pg->tag,
1414 					ig_map->ig->tag);
1415 			}
1416 		}
1417 
1418 		if (target->disable_chap) {
1419 			authmethod = "None";
1420 		} else if (!target->require_chap) {
1421 			authmethod = "Auto";
1422 		} else if (target->mutual_chap) {
1423 			authmethod = "CHAP Mutual";
1424 		} else {
1425 			authmethod = "CHAP";
1426 		}
1427 
1428 		if (target->chap_group > 0) {
1429 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1430 		}
1431 
1432 		if (target->header_digest) {
1433 			usedigest = "Header";
1434 		} else if (target->data_digest) {
1435 			usedigest = "Data";
1436 		}
1437 
1438 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1439 			authmethod, authgroup, usedigest);
1440 
1441 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1442 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1443 
1444 			if (!lun) {
1445 				continue;
1446 			}
1447 
1448 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1449 				spdk_scsi_lun_get_id(lun),
1450 				spdk_scsi_lun_get_bdev_name(lun));
1451 		}
1452 
1453 		fprintf(fp, TARGET_NODE_QD_TMPL,
1454 			target->queue_depth);
1455 	}
1456 }
1457 
1458 static void
1459 spdk_iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1460 			      struct spdk_json_write_ctx *w)
1461 {
1462 	struct spdk_iscsi_pg_map *pg_map;
1463 	struct spdk_iscsi_ig_map *ig_map;
1464 	int i;
1465 
1466 	spdk_json_write_object_begin(w);
1467 
1468 	spdk_json_write_named_string(w, "name", target->name);
1469 
1470 	if (target->alias) {
1471 		spdk_json_write_named_string(w, "alias_name", target->alias);
1472 	}
1473 
1474 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1475 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1476 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1477 			spdk_json_write_object_begin(w);
1478 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1479 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1480 			spdk_json_write_object_end(w);
1481 		}
1482 	}
1483 	spdk_json_write_array_end(w);
1484 
1485 	spdk_json_write_named_array_begin(w, "luns");
1486 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1487 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1488 
1489 		if (lun) {
1490 			spdk_json_write_object_begin(w);
1491 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1492 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1493 			spdk_json_write_object_end(w);
1494 		}
1495 	}
1496 	spdk_json_write_array_end(w);
1497 
1498 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1499 
1500 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1501 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1502 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1503 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1504 
1505 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1506 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1507 
1508 	spdk_json_write_object_end(w);
1509 }
1510 
1511 static void
1512 spdk_iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1513 				struct spdk_json_write_ctx *w)
1514 {
1515 	spdk_json_write_object_begin(w);
1516 
1517 	spdk_json_write_named_string(w, "method", "construct_target_node");
1518 
1519 	spdk_json_write_name(w, "params");
1520 	spdk_iscsi_tgt_node_info_json(target, w);
1521 
1522 	spdk_json_write_object_end(w);
1523 }
1524 
1525 void
1526 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1527 {
1528 	struct spdk_iscsi_tgt_node *target;
1529 
1530 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1531 		spdk_iscsi_tgt_node_info_json(target, w);
1532 	}
1533 }
1534 
1535 void
1536 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1537 {
1538 	struct spdk_iscsi_tgt_node *target;
1539 
1540 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1541 		spdk_iscsi_tgt_node_config_json(target, w);
1542 	}
1543 }
1544