aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/nvmf/nvmf_tcp.h
blob: 78d6cf17a977cdefe78a1661502581ca0827a99d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2022-2024 Chelsio Communications, Inc.
 * Written by: John Baldwin <jhb@FreeBSD.org>
 */

#ifndef __NVMF_TCP_H__
#define	__NVMF_TCP_H__

#ifndef _KERNEL
#define	__assert_unreachable	__unreachable
#define	MPASS			assert
#endif

#define	NVME_SGL_TYPE_ICD						\
	NVME_SGL_TYPE(NVME_SGL_TYPE_DATA_BLOCK, NVME_SGL_SUBTYPE_OFFSET)

#define	NVME_SGL_TYPE_COMMAND_BUFFER					\
	NVME_SGL_TYPE(NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK,		\
	    NVME_SGL_SUBTYPE_TRANSPORT)

/*
 * Validate common fields in a received PDU header.  If an error is
 * detected that requires an immediate disconnect, ECONNRESET is
 * returned.  If an error is detected that should be reported, EBADMSG
 * is returned and *fes and *fei are set to the values to be used in a
 * termination request PDU.  If no error is detected, 0 is returned
 * and *data_lenp is set to the length of any included data.
 *
 * Section number references refer to NVM Express over Fabrics
 * Revision 1.1 dated October 22, 2019.
 */
static __inline int
nvmf_tcp_validate_pdu_header(const struct nvme_tcp_common_pdu_hdr *ch,
    bool controller, bool header_digests, bool data_digests, uint8_t rxpda,
    uint32_t *data_lenp, uint16_t *fes, uint32_t *fei)
{
	uint32_t data_len, plen;
	u_int expected_hlen, full_hlen;
	uint8_t digest_flags, valid_flags;

	plen = le32toh(ch->plen);

	/*
	 * Errors must be reported for the lowest incorrect field
	 * first, so validate fields in order.
	 */

	/* Validate pdu_type. */

	/* Controllers only receive PDUs with a PDU direction of 0. */
	if (controller != ((ch->pdu_type & 0x01) == 0)) {
		printf("NVMe/TCP: Invalid PDU type %u\n", ch->pdu_type);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, pdu_type);
		return (EBADMSG);
	}

	switch (ch->pdu_type) {
	case NVME_TCP_PDU_TYPE_IC_REQ:
	case NVME_TCP_PDU_TYPE_IC_RESP:
		/* Shouldn't get these for an established connection. */
		printf("NVMe/TCP: Received Initialize Connection PDU\n");
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, pdu_type);
		return (EBADMSG);
	case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
	case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
		/*
		 * 7.4.7 Termination requests with invalid PDU lengths
		 * result in an immediate connection termination
		 * without reporting an error.
		 */
		if (plen < sizeof(struct nvme_tcp_term_req_hdr) ||
		    plen > NVME_TCP_TERM_REQ_PDU_MAX_SIZE) {
			printf("NVMe/TCP: Received invalid termination request\n");
			return (ECONNRESET);
		}
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
	case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
	case NVME_TCP_PDU_TYPE_H2C_DATA:
	case NVME_TCP_PDU_TYPE_C2H_DATA:
	case NVME_TCP_PDU_TYPE_R2T:
		break;
	default:
		printf("NVMe/TCP: Invalid PDU type %u\n", ch->pdu_type);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, pdu_type);
		return (EBADMSG);
	}

	/* Validate flags. */
	switch (ch->pdu_type) {
	default:
		__assert_unreachable();
		break;
	case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
	case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
		valid_flags = 0;
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
		valid_flags = NVME_TCP_CH_FLAGS_HDGSTF |
		    NVME_TCP_CH_FLAGS_DDGSTF;
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
	case NVME_TCP_PDU_TYPE_R2T:
		valid_flags = NVME_TCP_CH_FLAGS_HDGSTF;
		break;
	case NVME_TCP_PDU_TYPE_H2C_DATA:
		valid_flags = NVME_TCP_CH_FLAGS_HDGSTF |
		    NVME_TCP_CH_FLAGS_DDGSTF | NVME_TCP_H2C_DATA_FLAGS_LAST_PDU;
		break;
	case NVME_TCP_PDU_TYPE_C2H_DATA:
		valid_flags = NVME_TCP_CH_FLAGS_HDGSTF |
		    NVME_TCP_CH_FLAGS_DDGSTF | NVME_TCP_C2H_DATA_FLAGS_LAST_PDU |
		    NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
		break;
	}
	if ((ch->flags & ~valid_flags) != 0) {
		printf("NVMe/TCP: Invalid PDU header flags %#x\n", ch->flags);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, flags);
		return (EBADMSG);
	}

	/* Verify that digests are present iff enabled. */
	digest_flags = 0;
	if (header_digests)
		digest_flags |= NVME_TCP_CH_FLAGS_HDGSTF;
	if (data_digests)
		digest_flags |= NVME_TCP_CH_FLAGS_DDGSTF;
	if ((digest_flags & valid_flags) !=
	    (ch->flags & (NVME_TCP_CH_FLAGS_HDGSTF |
	    NVME_TCP_CH_FLAGS_DDGSTF))) {
		printf("NVMe/TCP: Invalid PDU header flags %#x\n", ch->flags);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, flags);
		return (EBADMSG);
	}

	/* 7.4.5.2: SUCCESS in C2H requires LAST_PDU */
	if (ch->pdu_type == NVME_TCP_PDU_TYPE_C2H_DATA &&
	    (ch->flags & (NVME_TCP_C2H_DATA_FLAGS_LAST_PDU |
	    NVME_TCP_C2H_DATA_FLAGS_SUCCESS)) ==
	    NVME_TCP_C2H_DATA_FLAGS_SUCCESS) {
		printf("NVMe/TCP: Invalid PDU header flags %#x\n", ch->flags);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, flags);
		return (EBADMSG);
	}

	/* Validate hlen. */
	switch (ch->pdu_type) {
	default:
		__assert_unreachable();
		break;
	case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
	case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
		expected_hlen = sizeof(struct nvme_tcp_term_req_hdr);
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
		expected_hlen = sizeof(struct nvme_tcp_cmd);
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
		expected_hlen = sizeof(struct nvme_tcp_rsp);
		break;
	case NVME_TCP_PDU_TYPE_H2C_DATA:
		expected_hlen = sizeof(struct nvme_tcp_h2c_data_hdr);
		break;
	case NVME_TCP_PDU_TYPE_C2H_DATA:
		expected_hlen = sizeof(struct nvme_tcp_c2h_data_hdr);
		break;
	case NVME_TCP_PDU_TYPE_R2T:
		expected_hlen = sizeof(struct nvme_tcp_r2t_hdr);
		break;
	}
	if (ch->hlen != expected_hlen) {
		printf("NVMe/TCP: Invalid PDU header length %u\n", ch->hlen);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, hlen);
		return (EBADMSG);
	}

	/* Validate pdo. */
	full_hlen = ch->hlen;
	if ((ch->flags & NVME_TCP_CH_FLAGS_HDGSTF) != 0)
		full_hlen += sizeof(uint32_t);
	switch (ch->pdu_type) {
	default:
		__assert_unreachable();
		break;
	case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
	case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
	case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
	case NVME_TCP_PDU_TYPE_R2T:
		if (ch->pdo != 0) {
			printf("NVMe/TCP: Invalid PDU data offset %u\n",
			    ch->pdo);
			*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
			*fei = offsetof(struct nvme_tcp_common_pdu_hdr, pdo);
			return (EBADMSG);
		}
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
	case NVME_TCP_PDU_TYPE_H2C_DATA:
	case NVME_TCP_PDU_TYPE_C2H_DATA:
		/* Permit PDO of 0 if there is no data. */
		if (full_hlen == plen && ch->pdo == 0)
			break;

		if (ch->pdo < full_hlen || ch->pdo > plen ||
		    ch->pdo % rxpda != 0) {
			printf("NVMe/TCP: Invalid PDU data offset %u\n",
			    ch->pdo);
			*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
			*fei = offsetof(struct nvme_tcp_common_pdu_hdr, pdo);
			return (EBADMSG);
		}
		break;
	}

	/* Validate plen. */
	if (plen < ch->hlen) {
		printf("NVMe/TCP: Invalid PDU length %u\n", plen);
		*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
		*fei = offsetof(struct nvme_tcp_common_pdu_hdr, plen);
		return (EBADMSG);
	}

	if (plen == full_hlen)
		data_len = 0;
	else
		data_len = plen - ch->pdo;
	switch (ch->pdu_type) {
	default:
		__assert_unreachable();
		break;
	case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
	case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
		/* Checked above. */
		MPASS(plen <= NVME_TCP_TERM_REQ_PDU_MAX_SIZE);
		break;
	case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
	case NVME_TCP_PDU_TYPE_H2C_DATA:
	case NVME_TCP_PDU_TYPE_C2H_DATA:
		if ((ch->flags & NVME_TCP_CH_FLAGS_DDGSTF) != 0 &&
		    data_len <= sizeof(uint32_t)) {
			printf("NVMe/TCP: PDU %u too short for digest\n",
			    ch->pdu_type);
			*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
			*fei = offsetof(struct nvme_tcp_common_pdu_hdr, plen);
			return (EBADMSG);
		}
		break;
	case NVME_TCP_PDU_TYPE_R2T:
	case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
		if (data_len != 0) {
			printf("NVMe/TCP: PDU %u with data length %u\n",
			    ch->pdu_type, data_len);
			*fes = NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
			*fei = offsetof(struct nvme_tcp_common_pdu_hdr, plen);
			return (EBADMSG);
		}
		break;
	}

	if ((ch->flags & NVME_TCP_CH_FLAGS_DDGSTF) != 0)
		data_len -= sizeof(uint32_t);

	*data_lenp = data_len;
	return (0);
}

#endif /* !__NVMF_TCP_H__ */