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