This source file includes following definitions.
- sas_get_pr_transport_id
- fc_get_pr_transport_id
- sbp_get_pr_transport_id
- srp_get_pr_transport_id
- iscsi_get_pr_transport_id
- iscsi_get_pr_transport_id_len
- iscsi_parse_pr_out_transport_id
- target_get_pr_transport_id_len
- target_get_pr_transport_id
- target_parse_pr_out_transport_id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <linux/spinlock.h>
23 #include <linux/export.h>
24 #include <asm/unaligned.h>
25
26 #include <scsi/scsi_proto.h>
27
28 #include <target/target_core_base.h>
29 #include <target/target_core_fabric.h>
30
31 #include "target_core_internal.h"
32 #include "target_core_pr.h"
33
34
35 static int sas_get_pr_transport_id(
36 struct se_node_acl *nacl,
37 int *format_code,
38 unsigned char *buf)
39 {
40 int ret;
41
42
43 ret = hex2bin(&buf[4], &nacl->initiatorname[4], 8);
44 if (ret) {
45 pr_debug("%s: invalid hex string\n", __func__);
46 return ret;
47 }
48
49 return 24;
50 }
51
52 static int fc_get_pr_transport_id(
53 struct se_node_acl *se_nacl,
54 int *format_code,
55 unsigned char *buf)
56 {
57 unsigned char *ptr;
58 int i, ret;
59 u32 off = 8;
60
61
62
63
64
65 ptr = &se_nacl->initiatorname[0];
66 for (i = 0; i < 23; ) {
67 if (!strncmp(&ptr[i], ":", 1)) {
68 i++;
69 continue;
70 }
71 ret = hex2bin(&buf[off++], &ptr[i], 1);
72 if (ret < 0) {
73 pr_debug("%s: invalid hex string\n", __func__);
74 return ret;
75 }
76 i += 2;
77 }
78
79
80
81 return 24;
82 }
83
84 static int sbp_get_pr_transport_id(
85 struct se_node_acl *nacl,
86 int *format_code,
87 unsigned char *buf)
88 {
89 int ret;
90
91 ret = hex2bin(&buf[8], nacl->initiatorname, 8);
92 if (ret) {
93 pr_debug("%s: invalid hex string\n", __func__);
94 return ret;
95 }
96
97 return 24;
98 }
99
100 static int srp_get_pr_transport_id(
101 struct se_node_acl *nacl,
102 int *format_code,
103 unsigned char *buf)
104 {
105 const char *p;
106 unsigned len, count, leading_zero_bytes;
107 int rc;
108
109 p = nacl->initiatorname;
110 if (strncasecmp(p, "0x", 2) == 0)
111 p += 2;
112 len = strlen(p);
113 if (len % 2)
114 return -EINVAL;
115
116 count = min(len / 2, 16U);
117 leading_zero_bytes = 16 - count;
118 memset(buf + 8, 0, leading_zero_bytes);
119 rc = hex2bin(buf + 8 + leading_zero_bytes, p, count);
120 if (rc < 0) {
121 pr_debug("hex2bin failed for %s: %d\n", p, rc);
122 return rc;
123 }
124
125 return 24;
126 }
127
128 static int iscsi_get_pr_transport_id(
129 struct se_node_acl *se_nacl,
130 struct t10_pr_registration *pr_reg,
131 int *format_code,
132 unsigned char *buf)
133 {
134 u32 off = 4, padding = 0;
135 u16 len = 0;
136
137 spin_lock_irq(&se_nacl->nacl_sess_lock);
138
139
140
141
142
143
144
145
146
147
148
149 len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
150
151
152
153 len++;
154
155
156
157
158
159
160
161
162 if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
163
164
165
166
167 buf[0] |= 0x40;
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 buf[off+len] = 0x2c; off++;
183 buf[off+len] = 0x69; off++;
184 buf[off+len] = 0x2c; off++;
185 buf[off+len] = 0x30; off++;
186 buf[off+len] = 0x78; off++;
187 len += 5;
188 buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
189 buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
190 buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
191 buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
192 buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
193 buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
194 buf[off+len] = '\0'; off++;
195 len += 7;
196 }
197 spin_unlock_irq(&se_nacl->nacl_sess_lock);
198
199
200
201
202
203 padding = ((-len) & 3);
204 if (padding != 0)
205 len += padding;
206
207 put_unaligned_be16(len, &buf[2]);
208
209
210
211
212 len += 4;
213
214 return len;
215 }
216
217 static int iscsi_get_pr_transport_id_len(
218 struct se_node_acl *se_nacl,
219 struct t10_pr_registration *pr_reg,
220 int *format_code)
221 {
222 u32 len = 0, padding = 0;
223
224 spin_lock_irq(&se_nacl->nacl_sess_lock);
225 len = strlen(se_nacl->initiatorname);
226
227
228
229 len++;
230
231
232
233
234
235
236
237 if (pr_reg->isid_present_at_reg) {
238 len += 5;
239 len += 7;
240 *format_code = 1;
241 } else
242 *format_code = 0;
243 spin_unlock_irq(&se_nacl->nacl_sess_lock);
244
245
246
247
248
249 padding = ((-len) & 3);
250 if (padding != 0)
251 len += padding;
252
253
254
255
256 len += 4;
257
258 return len;
259 }
260
261 static char *iscsi_parse_pr_out_transport_id(
262 struct se_portal_group *se_tpg,
263 char *buf,
264 u32 *out_tid_len,
265 char **port_nexus_ptr)
266 {
267 char *p;
268 u32 tid_len, padding;
269 int i;
270 u16 add_len;
271 u8 format_code = (buf[0] & 0xc0);
272
273
274
275
276
277
278
279
280
281
282
283
284
285 if ((format_code != 0x00) && (format_code != 0x40)) {
286 pr_err("Illegal format code: 0x%02x for iSCSI"
287 " Initiator Transport ID\n", format_code);
288 return NULL;
289 }
290
291
292
293
294 if (out_tid_len) {
295
296 add_len = get_unaligned_be16(&buf[2]);
297
298 tid_len = strlen(&buf[4]);
299 tid_len += 4;
300 tid_len += 1;
301 padding = ((-tid_len) & 3);
302 if (padding != 0)
303 tid_len += padding;
304
305 if ((add_len + 4) != tid_len) {
306 pr_debug("LIO-Target Extracted add_len: %hu "
307 "does not match calculated tid_len: %u,"
308 " using tid_len instead\n", add_len+4, tid_len);
309 *out_tid_len = tid_len;
310 } else
311 *out_tid_len = (add_len + 4);
312 }
313
314
315
316
317
318 if (format_code == 0x40) {
319 p = strstr(&buf[4], ",i,0x");
320 if (!p) {
321 pr_err("Unable to locate \",i,0x\" separator"
322 " for Initiator port identifier: %s\n",
323 &buf[4]);
324 return NULL;
325 }
326 *p = '\0';
327 p += 5;
328
329 *port_nexus_ptr = p;
330
331
332
333
334
335
336 for (i = 0; i < 12; i++) {
337 if (isdigit(*p)) {
338 p++;
339 continue;
340 }
341 *p = tolower(*p);
342 p++;
343 }
344 }
345
346 return &buf[4];
347 }
348
349 int target_get_pr_transport_id_len(struct se_node_acl *nacl,
350 struct t10_pr_registration *pr_reg, int *format_code)
351 {
352 switch (nacl->se_tpg->proto_id) {
353 case SCSI_PROTOCOL_FCP:
354 case SCSI_PROTOCOL_SBP:
355 case SCSI_PROTOCOL_SRP:
356 case SCSI_PROTOCOL_SAS:
357 break;
358 case SCSI_PROTOCOL_ISCSI:
359 return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code);
360 default:
361 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
362 return -EINVAL;
363 }
364
365
366
367
368 *format_code = 0;
369 return 24;
370 }
371
372 int target_get_pr_transport_id(struct se_node_acl *nacl,
373 struct t10_pr_registration *pr_reg, int *format_code,
374 unsigned char *buf)
375 {
376 switch (nacl->se_tpg->proto_id) {
377 case SCSI_PROTOCOL_SAS:
378 return sas_get_pr_transport_id(nacl, format_code, buf);
379 case SCSI_PROTOCOL_SBP:
380 return sbp_get_pr_transport_id(nacl, format_code, buf);
381 case SCSI_PROTOCOL_SRP:
382 return srp_get_pr_transport_id(nacl, format_code, buf);
383 case SCSI_PROTOCOL_FCP:
384 return fc_get_pr_transport_id(nacl, format_code, buf);
385 case SCSI_PROTOCOL_ISCSI:
386 return iscsi_get_pr_transport_id(nacl, pr_reg, format_code,
387 buf);
388 default:
389 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
390 return -EINVAL;
391 }
392 }
393
394 const char *target_parse_pr_out_transport_id(struct se_portal_group *tpg,
395 char *buf, u32 *out_tid_len, char **port_nexus_ptr)
396 {
397 u32 offset;
398
399 switch (tpg->proto_id) {
400 case SCSI_PROTOCOL_SAS:
401
402
403
404
405 offset = 4;
406 break;
407 case SCSI_PROTOCOL_SBP:
408 case SCSI_PROTOCOL_SRP:
409 case SCSI_PROTOCOL_FCP:
410 offset = 8;
411 break;
412 case SCSI_PROTOCOL_ISCSI:
413 return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len,
414 port_nexus_ptr);
415 default:
416 pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id);
417 return NULL;
418 }
419
420 *port_nexus_ptr = NULL;
421 *out_tid_len = 24;
422 return buf + offset;
423 }