1 /*******************************************************************************
2  * This file contains iSCSI Target Portal Group related functions.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20 
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23 #include <target/target_core_configfs.h>
24 
25 #include "iscsi_target_core.h"
26 #include "iscsi_target_erl0.h"
27 #include "iscsi_target_login.h"
28 #include "iscsi_target_nodeattrib.h"
29 #include "iscsi_target_tpg.h"
30 #include "iscsi_target_util.h"
31 #include "iscsi_target.h"
32 #include "iscsi_target_parameters.h"
33 
34 #include <target/iscsi/iscsi_transport.h>
35 
36 struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
37 {
38 	struct iscsi_portal_group *tpg;
39 
40 	tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
41 	if (!tpg) {
42 		pr_err("Unable to allocate struct iscsi_portal_group\n");
43 		return NULL;
44 	}
45 
46 	tpg->tpgt = tpgt;
47 	tpg->tpg_state = TPG_STATE_FREE;
48 	tpg->tpg_tiqn = tiqn;
49 	INIT_LIST_HEAD(&tpg->tpg_gnp_list);
50 	INIT_LIST_HEAD(&tpg->tpg_list);
51 	mutex_init(&tpg->tpg_access_lock);
52 	mutex_init(&tpg->np_login_lock);
53 	spin_lock_init(&tpg->tpg_state_lock);
54 	spin_lock_init(&tpg->tpg_np_lock);
55 
56 	return tpg;
57 }
58 
59 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
60 
61 int iscsit_load_discovery_tpg(void)
62 {
63 	struct iscsi_param *param;
64 	struct iscsi_portal_group *tpg;
65 	int ret;
66 
67 	tpg = iscsit_alloc_portal_group(NULL, 1);
68 	if (!tpg) {
69 		pr_err("Unable to allocate struct iscsi_portal_group\n");
70 		return -1;
71 	}
72 
73 	ret = core_tpg_register(
74 			&lio_target_fabric_configfs->tf_ops,
75 			NULL, &tpg->tpg_se_tpg, tpg,
76 			TRANSPORT_TPG_TYPE_DISCOVERY);
77 	if (ret < 0) {
78 		kfree(tpg);
79 		return -1;
80 	}
81 
82 	tpg->sid = 1; /* First Assigned LIO Session ID */
83 	iscsit_set_default_tpg_attribs(tpg);
84 
85 	if (iscsi_create_default_params(&tpg->param_list) < 0)
86 		goto out;
87 	/*
88 	 * By default we disable authentication for discovery sessions,
89 	 * this can be changed with:
90 	 *
91 	 * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
92 	 */
93 	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
94 	if (!param)
95 		goto out;
96 
97 	if (iscsi_update_param_value(param, "CHAP,None") < 0)
98 		goto out;
99 
100 	tpg->tpg_attrib.authentication = 0;
101 
102 	spin_lock(&tpg->tpg_state_lock);
103 	tpg->tpg_state  = TPG_STATE_ACTIVE;
104 	spin_unlock(&tpg->tpg_state_lock);
105 
106 	iscsit_global->discovery_tpg = tpg;
107 	pr_debug("CORE[0] - Allocated Discovery TPG\n");
108 
109 	return 0;
110 out:
111 	if (tpg->sid == 1)
112 		core_tpg_deregister(&tpg->tpg_se_tpg);
113 	kfree(tpg);
114 	return -1;
115 }
116 
117 void iscsit_release_discovery_tpg(void)
118 {
119 	struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
120 
121 	if (!tpg)
122 		return;
123 
124 	core_tpg_deregister(&tpg->tpg_se_tpg);
125 
126 	kfree(tpg);
127 	iscsit_global->discovery_tpg = NULL;
128 }
129 
130 struct iscsi_portal_group *iscsit_get_tpg_from_np(
131 	struct iscsi_tiqn *tiqn,
132 	struct iscsi_np *np)
133 {
134 	struct iscsi_portal_group *tpg = NULL;
135 	struct iscsi_tpg_np *tpg_np;
136 
137 	spin_lock(&tiqn->tiqn_tpg_lock);
138 	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
139 
140 		spin_lock(&tpg->tpg_state_lock);
141 		if (tpg->tpg_state == TPG_STATE_FREE) {
142 			spin_unlock(&tpg->tpg_state_lock);
143 			continue;
144 		}
145 		spin_unlock(&tpg->tpg_state_lock);
146 
147 		spin_lock(&tpg->tpg_np_lock);
148 		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
149 			if (tpg_np->tpg_np == np) {
150 				spin_unlock(&tpg->tpg_np_lock);
151 				spin_unlock(&tiqn->tiqn_tpg_lock);
152 				return tpg;
153 			}
154 		}
155 		spin_unlock(&tpg->tpg_np_lock);
156 	}
157 	spin_unlock(&tiqn->tiqn_tpg_lock);
158 
159 	return NULL;
160 }
161 
162 int iscsit_get_tpg(
163 	struct iscsi_portal_group *tpg)
164 {
165 	int ret;
166 
167 	ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
168 	return ((ret != 0) || signal_pending(current)) ? -1 : 0;
169 }
170 
171 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
172 {
173 	mutex_unlock(&tpg->tpg_access_lock);
174 }
175 
176 static void iscsit_clear_tpg_np_login_thread(
177 	struct iscsi_tpg_np *tpg_np,
178 	struct iscsi_portal_group *tpg)
179 {
180 	if (!tpg_np->tpg_np) {
181 		pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
182 		return;
183 	}
184 
185 	iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg);
186 }
187 
188 void iscsit_clear_tpg_np_login_threads(
189 	struct iscsi_portal_group *tpg)
190 {
191 	struct iscsi_tpg_np *tpg_np;
192 
193 	spin_lock(&tpg->tpg_np_lock);
194 	list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
195 		if (!tpg_np->tpg_np) {
196 			pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
197 			continue;
198 		}
199 		spin_unlock(&tpg->tpg_np_lock);
200 		iscsit_clear_tpg_np_login_thread(tpg_np, tpg);
201 		spin_lock(&tpg->tpg_np_lock);
202 	}
203 	spin_unlock(&tpg->tpg_np_lock);
204 }
205 
206 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
207 {
208 	iscsi_print_params(tpg->param_list);
209 }
210 
211 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
212 {
213 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
214 
215 	a->authentication = TA_AUTHENTICATION;
216 	a->login_timeout = TA_LOGIN_TIMEOUT;
217 	a->netif_timeout = TA_NETIF_TIMEOUT;
218 	a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
219 	a->generate_node_acls = TA_GENERATE_NODE_ACLS;
220 	a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
221 	a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
222 	a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
223 }
224 
225 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
226 {
227 	if (tpg->tpg_state != TPG_STATE_FREE) {
228 		pr_err("Unable to add iSCSI Target Portal Group: %d"
229 			" while not in TPG_STATE_FREE state.\n", tpg->tpgt);
230 		return -EEXIST;
231 	}
232 	iscsit_set_default_tpg_attribs(tpg);
233 
234 	if (iscsi_create_default_params(&tpg->param_list) < 0)
235 		goto err_out;
236 
237 	ISCSI_TPG_ATTRIB(tpg)->tpg = tpg;
238 
239 	spin_lock(&tpg->tpg_state_lock);
240 	tpg->tpg_state	= TPG_STATE_INACTIVE;
241 	spin_unlock(&tpg->tpg_state_lock);
242 
243 	spin_lock(&tiqn->tiqn_tpg_lock);
244 	list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
245 	tiqn->tiqn_ntpgs++;
246 	pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
247 			tiqn->tiqn, tpg->tpgt);
248 	spin_unlock(&tiqn->tiqn_tpg_lock);
249 
250 	return 0;
251 err_out:
252 	if (tpg->param_list) {
253 		iscsi_release_param_list(tpg->param_list);
254 		tpg->param_list = NULL;
255 	}
256 	kfree(tpg);
257 	return -ENOMEM;
258 }
259 
260 int iscsit_tpg_del_portal_group(
261 	struct iscsi_tiqn *tiqn,
262 	struct iscsi_portal_group *tpg,
263 	int force)
264 {
265 	u8 old_state = tpg->tpg_state;
266 
267 	spin_lock(&tpg->tpg_state_lock);
268 	tpg->tpg_state = TPG_STATE_INACTIVE;
269 	spin_unlock(&tpg->tpg_state_lock);
270 
271 	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
272 		pr_err("Unable to delete iSCSI Target Portal Group:"
273 			" %hu while active sessions exist, and force=0\n",
274 			tpg->tpgt);
275 		tpg->tpg_state = old_state;
276 		return -EPERM;
277 	}
278 
279 	core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
280 
281 	if (tpg->param_list) {
282 		iscsi_release_param_list(tpg->param_list);
283 		tpg->param_list = NULL;
284 	}
285 
286 	core_tpg_deregister(&tpg->tpg_se_tpg);
287 
288 	spin_lock(&tpg->tpg_state_lock);
289 	tpg->tpg_state = TPG_STATE_FREE;
290 	spin_unlock(&tpg->tpg_state_lock);
291 
292 	spin_lock(&tiqn->tiqn_tpg_lock);
293 	tiqn->tiqn_ntpgs--;
294 	list_del(&tpg->tpg_list);
295 	spin_unlock(&tiqn->tiqn_tpg_lock);
296 
297 	pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
298 			tiqn->tiqn, tpg->tpgt);
299 
300 	kfree(tpg);
301 	return 0;
302 }
303 
304 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
305 {
306 	struct iscsi_param *param;
307 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
308 	int ret;
309 
310 	spin_lock(&tpg->tpg_state_lock);
311 	if (tpg->tpg_state == TPG_STATE_ACTIVE) {
312 		pr_err("iSCSI target portal group: %hu is already"
313 			" active, ignoring request.\n", tpg->tpgt);
314 		spin_unlock(&tpg->tpg_state_lock);
315 		return -EINVAL;
316 	}
317 	/*
318 	 * Make sure that AuthMethod does not contain None as an option
319 	 * unless explictly disabled.  Set the default to CHAP if authentication
320 	 * is enforced (as per default), and remove the NONE option.
321 	 */
322 	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
323 	if (!param) {
324 		spin_unlock(&tpg->tpg_state_lock);
325 		return -EINVAL;
326 	}
327 
328 	if (ISCSI_TPG_ATTRIB(tpg)->authentication) {
329 		if (!strcmp(param->value, NONE)) {
330 			ret = iscsi_update_param_value(param, CHAP);
331 			if (ret)
332 				goto err;
333 		}
334 
335 		ret = iscsit_ta_authentication(tpg, 1);
336 		if (ret < 0)
337 			goto err;
338 	}
339 
340 	tpg->tpg_state = TPG_STATE_ACTIVE;
341 	spin_unlock(&tpg->tpg_state_lock);
342 
343 	spin_lock(&tiqn->tiqn_tpg_lock);
344 	tiqn->tiqn_active_tpgs++;
345 	pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
346 			tpg->tpgt);
347 	spin_unlock(&tiqn->tiqn_tpg_lock);
348 
349 	return 0;
350 
351 err:
352 	spin_unlock(&tpg->tpg_state_lock);
353 	return ret;
354 }
355 
356 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
357 {
358 	struct iscsi_tiqn *tiqn;
359 	u8 old_state = tpg->tpg_state;
360 
361 	spin_lock(&tpg->tpg_state_lock);
362 	if (tpg->tpg_state == TPG_STATE_INACTIVE) {
363 		pr_err("iSCSI Target Portal Group: %hu is already"
364 			" inactive, ignoring request.\n", tpg->tpgt);
365 		spin_unlock(&tpg->tpg_state_lock);
366 		return -EINVAL;
367 	}
368 	tpg->tpg_state = TPG_STATE_INACTIVE;
369 	spin_unlock(&tpg->tpg_state_lock);
370 
371 	iscsit_clear_tpg_np_login_threads(tpg);
372 
373 	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
374 		spin_lock(&tpg->tpg_state_lock);
375 		tpg->tpg_state = old_state;
376 		spin_unlock(&tpg->tpg_state_lock);
377 		pr_err("Unable to disable iSCSI Target Portal Group:"
378 			" %hu while active sessions exist, and force=0\n",
379 			tpg->tpgt);
380 		return -EPERM;
381 	}
382 
383 	tiqn = tpg->tpg_tiqn;
384 	if (!tiqn || (tpg == iscsit_global->discovery_tpg))
385 		return 0;
386 
387 	spin_lock(&tiqn->tiqn_tpg_lock);
388 	tiqn->tiqn_active_tpgs--;
389 	pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
390 			tpg->tpgt);
391 	spin_unlock(&tiqn->tiqn_tpg_lock);
392 
393 	return 0;
394 }
395 
396 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
397 	struct iscsi_session *sess)
398 {
399 	struct se_session *se_sess = sess->se_sess;
400 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
401 	struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
402 					se_node_acl);
403 
404 	return &acl->node_attrib;
405 }
406 
407 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
408 	struct iscsi_tpg_np *tpg_np,
409 	int network_transport)
410 {
411 	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
412 
413 	spin_lock(&tpg_np->tpg_np_parent_lock);
414 	list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
415 			&tpg_np->tpg_np_parent_list, tpg_np_child_list) {
416 		if (tpg_np_child->tpg_np->np_network_transport ==
417 				network_transport) {
418 			spin_unlock(&tpg_np->tpg_np_parent_lock);
419 			return tpg_np_child;
420 		}
421 	}
422 	spin_unlock(&tpg_np->tpg_np_parent_lock);
423 
424 	return NULL;
425 }
426 
427 static bool iscsit_tpg_check_network_portal(
428 	struct iscsi_tiqn *tiqn,
429 	struct __kernel_sockaddr_storage *sockaddr,
430 	int network_transport)
431 {
432 	struct iscsi_portal_group *tpg;
433 	struct iscsi_tpg_np *tpg_np;
434 	struct iscsi_np *np;
435 	bool match = false;
436 
437 	spin_lock(&tiqn->tiqn_tpg_lock);
438 	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
439 
440 		spin_lock(&tpg->tpg_np_lock);
441 		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
442 			np = tpg_np->tpg_np;
443 
444 			match = iscsit_check_np_match(sockaddr, np,
445 						network_transport);
446 			if (match == true)
447 				break;
448 		}
449 		spin_unlock(&tpg->tpg_np_lock);
450 	}
451 	spin_unlock(&tiqn->tiqn_tpg_lock);
452 
453 	return match;
454 }
455 
456 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
457 	struct iscsi_portal_group *tpg,
458 	struct __kernel_sockaddr_storage *sockaddr,
459 	char *ip_str,
460 	struct iscsi_tpg_np *tpg_np_parent,
461 	int network_transport)
462 {
463 	struct iscsi_np *np;
464 	struct iscsi_tpg_np *tpg_np;
465 
466 	if (!tpg_np_parent) {
467 		if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
468 				network_transport) == true) {
469 			pr_err("Network Portal: %s already exists on a"
470 				" different TPG on %s\n", ip_str,
471 				tpg->tpg_tiqn->tiqn);
472 			return ERR_PTR(-EEXIST);
473 		}
474 	}
475 
476 	tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
477 	if (!tpg_np) {
478 		pr_err("Unable to allocate memory for"
479 				" struct iscsi_tpg_np.\n");
480 		return ERR_PTR(-ENOMEM);
481 	}
482 
483 	np = iscsit_add_np(sockaddr, ip_str, network_transport);
484 	if (IS_ERR(np)) {
485 		kfree(tpg_np);
486 		return ERR_CAST(np);
487 	}
488 
489 	INIT_LIST_HEAD(&tpg_np->tpg_np_list);
490 	INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
491 	INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
492 	spin_lock_init(&tpg_np->tpg_np_parent_lock);
493 	tpg_np->tpg_np		= np;
494 	tpg_np->tpg		= tpg;
495 
496 	spin_lock(&tpg->tpg_np_lock);
497 	list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
498 	tpg->num_tpg_nps++;
499 	if (tpg->tpg_tiqn)
500 		tpg->tpg_tiqn->tiqn_num_tpg_nps++;
501 	spin_unlock(&tpg->tpg_np_lock);
502 
503 	if (tpg_np_parent) {
504 		tpg_np->tpg_np_parent = tpg_np_parent;
505 		spin_lock(&tpg_np_parent->tpg_np_parent_lock);
506 		list_add_tail(&tpg_np->tpg_np_child_list,
507 			&tpg_np_parent->tpg_np_parent_list);
508 		spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
509 	}
510 
511 	pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
512 		tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
513 		np->np_transport->name);
514 
515 	return tpg_np;
516 }
517 
518 static int iscsit_tpg_release_np(
519 	struct iscsi_tpg_np *tpg_np,
520 	struct iscsi_portal_group *tpg,
521 	struct iscsi_np *np)
522 {
523 	iscsit_clear_tpg_np_login_thread(tpg_np, tpg);
524 
525 	pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
526 		tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
527 		np->np_transport->name);
528 
529 	tpg_np->tpg_np = NULL;
530 	tpg_np->tpg = NULL;
531 	kfree(tpg_np);
532 	/*
533 	 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
534 	 */
535 	return iscsit_del_np(np);
536 }
537 
538 int iscsit_tpg_del_network_portal(
539 	struct iscsi_portal_group *tpg,
540 	struct iscsi_tpg_np *tpg_np)
541 {
542 	struct iscsi_np *np;
543 	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
544 	int ret = 0;
545 
546 	np = tpg_np->tpg_np;
547 	if (!np) {
548 		pr_err("Unable to locate struct iscsi_np from"
549 				" struct iscsi_tpg_np\n");
550 		return -EINVAL;
551 	}
552 
553 	if (!tpg_np->tpg_np_parent) {
554 		/*
555 		 * We are the parent tpg network portal.  Release all of the
556 		 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
557 		 * list first.
558 		 */
559 		list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
560 				&tpg_np->tpg_np_parent_list,
561 				tpg_np_child_list) {
562 			ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
563 			if (ret < 0)
564 				pr_err("iscsit_tpg_del_network_portal()"
565 					" failed: %d\n", ret);
566 		}
567 	} else {
568 		/*
569 		 * We are not the parent ISCSI_TCP tpg network portal.  Release
570 		 * our own network portals from the child list.
571 		 */
572 		spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
573 		list_del(&tpg_np->tpg_np_child_list);
574 		spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
575 	}
576 
577 	spin_lock(&tpg->tpg_np_lock);
578 	list_del(&tpg_np->tpg_np_list);
579 	tpg->num_tpg_nps--;
580 	if (tpg->tpg_tiqn)
581 		tpg->tpg_tiqn->tiqn_num_tpg_nps--;
582 	spin_unlock(&tpg->tpg_np_lock);
583 
584 	return iscsit_tpg_release_np(tpg_np, tpg, np);
585 }
586 
587 int iscsit_tpg_set_initiator_node_queue_depth(
588 	struct iscsi_portal_group *tpg,
589 	unsigned char *initiatorname,
590 	u32 queue_depth,
591 	int force)
592 {
593 	return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
594 		initiatorname, queue_depth, force);
595 }
596 
597 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
598 {
599 	unsigned char buf1[256], buf2[256], *none = NULL;
600 	int len;
601 	struct iscsi_param *param;
602 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
603 
604 	if ((authentication != 1) && (authentication != 0)) {
605 		pr_err("Illegal value for authentication parameter:"
606 			" %u, ignoring request.\n", authentication);
607 		return -EINVAL;
608 	}
609 
610 	memset(buf1, 0, sizeof(buf1));
611 	memset(buf2, 0, sizeof(buf2));
612 
613 	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
614 	if (!param)
615 		return -EINVAL;
616 
617 	if (authentication) {
618 		snprintf(buf1, sizeof(buf1), "%s", param->value);
619 		none = strstr(buf1, NONE);
620 		if (!none)
621 			goto out;
622 		if (!strncmp(none + 4, ",", 1)) {
623 			if (!strcmp(buf1, none))
624 				sprintf(buf2, "%s", none+5);
625 			else {
626 				none--;
627 				*none = '\0';
628 				len = sprintf(buf2, "%s", buf1);
629 				none += 5;
630 				sprintf(buf2 + len, "%s", none);
631 			}
632 		} else {
633 			none--;
634 			*none = '\0';
635 			sprintf(buf2, "%s", buf1);
636 		}
637 		if (iscsi_update_param_value(param, buf2) < 0)
638 			return -EINVAL;
639 	} else {
640 		snprintf(buf1, sizeof(buf1), "%s", param->value);
641 		none = strstr(buf1, NONE);
642 		if (none)
643 			goto out;
644 		strncat(buf1, ",", strlen(","));
645 		strncat(buf1, NONE, strlen(NONE));
646 		if (iscsi_update_param_value(param, buf1) < 0)
647 			return -EINVAL;
648 	}
649 
650 out:
651 	a->authentication = authentication;
652 	pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
653 		a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
654 
655 	return 0;
656 }
657 
658 int iscsit_ta_login_timeout(
659 	struct iscsi_portal_group *tpg,
660 	u32 login_timeout)
661 {
662 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
663 
664 	if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
665 		pr_err("Requested Login Timeout %u larger than maximum"
666 			" %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
667 		return -EINVAL;
668 	} else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
669 		pr_err("Requested Logout Timeout %u smaller than"
670 			" minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
671 		return -EINVAL;
672 	}
673 
674 	a->login_timeout = login_timeout;
675 	pr_debug("Set Logout Timeout to %u for Target Portal Group"
676 		" %hu\n", a->login_timeout, tpg->tpgt);
677 
678 	return 0;
679 }
680 
681 int iscsit_ta_netif_timeout(
682 	struct iscsi_portal_group *tpg,
683 	u32 netif_timeout)
684 {
685 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
686 
687 	if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
688 		pr_err("Requested Network Interface Timeout %u larger"
689 			" than maximum %u\n", netif_timeout,
690 				TA_NETIF_TIMEOUT_MAX);
691 		return -EINVAL;
692 	} else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
693 		pr_err("Requested Network Interface Timeout %u smaller"
694 			" than minimum %u\n", netif_timeout,
695 				TA_NETIF_TIMEOUT_MIN);
696 		return -EINVAL;
697 	}
698 
699 	a->netif_timeout = netif_timeout;
700 	pr_debug("Set Network Interface Timeout to %u for"
701 		" Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
702 
703 	return 0;
704 }
705 
706 int iscsit_ta_generate_node_acls(
707 	struct iscsi_portal_group *tpg,
708 	u32 flag)
709 {
710 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
711 
712 	if ((flag != 0) && (flag != 1)) {
713 		pr_err("Illegal value %d\n", flag);
714 		return -EINVAL;
715 	}
716 
717 	a->generate_node_acls = flag;
718 	pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
719 		tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
720 
721 	if (flag == 1 && a->cache_dynamic_acls == 0) {
722 		pr_debug("Explicitly setting cache_dynamic_acls=1 when "
723 			"generate_node_acls=1\n");
724 		a->cache_dynamic_acls = 1;
725 	}
726 
727 	return 0;
728 }
729 
730 int iscsit_ta_default_cmdsn_depth(
731 	struct iscsi_portal_group *tpg,
732 	u32 tcq_depth)
733 {
734 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
735 
736 	if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
737 		pr_err("Requested Default Queue Depth: %u larger"
738 			" than maximum %u\n", tcq_depth,
739 				TA_DEFAULT_CMDSN_DEPTH_MAX);
740 		return -EINVAL;
741 	} else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
742 		pr_err("Requested Default Queue Depth: %u smaller"
743 			" than minimum %u\n", tcq_depth,
744 				TA_DEFAULT_CMDSN_DEPTH_MIN);
745 		return -EINVAL;
746 	}
747 
748 	a->default_cmdsn_depth = tcq_depth;
749 	pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
750 		tpg->tpgt, a->default_cmdsn_depth);
751 
752 	return 0;
753 }
754 
755 int iscsit_ta_cache_dynamic_acls(
756 	struct iscsi_portal_group *tpg,
757 	u32 flag)
758 {
759 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
760 
761 	if ((flag != 0) && (flag != 1)) {
762 		pr_err("Illegal value %d\n", flag);
763 		return -EINVAL;
764 	}
765 
766 	if (a->generate_node_acls == 1 && flag == 0) {
767 		pr_debug("Skipping cache_dynamic_acls=0 when"
768 			" generate_node_acls=1\n");
769 		return 0;
770 	}
771 
772 	a->cache_dynamic_acls = flag;
773 	pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
774 		" ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
775 		"Enabled" : "Disabled");
776 
777 	return 0;
778 }
779 
780 int iscsit_ta_demo_mode_write_protect(
781 	struct iscsi_portal_group *tpg,
782 	u32 flag)
783 {
784 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
785 
786 	if ((flag != 0) && (flag != 1)) {
787 		pr_err("Illegal value %d\n", flag);
788 		return -EINVAL;
789 	}
790 
791 	a->demo_mode_write_protect = flag;
792 	pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
793 		tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
794 
795 	return 0;
796 }
797 
798 int iscsit_ta_prod_mode_write_protect(
799 	struct iscsi_portal_group *tpg,
800 	u32 flag)
801 {
802 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
803 
804 	if ((flag != 0) && (flag != 1)) {
805 		pr_err("Illegal value %d\n", flag);
806 		return -EINVAL;
807 	}
808 
809 	a->prod_mode_write_protect = flag;
810 	pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
811 		" %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
812 		"ON" : "OFF");
813 
814 	return 0;
815 }
816