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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*-
* Copyright (c) 2012 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Edward Tomasz Napierala under sponsorship
* from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef CTLD_H
#define CTLD_H
#include <sys/queue.h>
#ifdef ICL_KERNEL_PROXY
#include <sys/types.h>
#endif
#include <sys/socket.h>
#include <stdbool.h>
#include <libutil.h>
#include <openssl/md5.h>
#define DEFAULT_CONFIG_PATH "/etc/ctl.conf"
#define DEFAULT_PIDFILE "/var/run/ctld.pid"
#define DEFAULT_BLOCKSIZE 512
#define MAX_LUNS 1024
#define MAX_NAME_LEN 223
#define MAX_DATA_SEGMENT_LENGTH (128 * 1024)
#define MAX_BURST_LENGTH 16776192
#define SOCKBUF_SIZE 1048576
struct auth {
TAILQ_ENTRY(auth) a_next;
struct auth_group *a_auth_group;
char *a_user;
char *a_secret;
char *a_mutual_user;
char *a_mutual_secret;
};
struct auth_name {
TAILQ_ENTRY(auth_name) an_next;
struct auth_group *an_auth_group;
char *an_initator_name;
};
struct auth_portal {
TAILQ_ENTRY(auth_portal) ap_next;
struct auth_group *ap_auth_group;
char *ap_initator_portal;
struct sockaddr_storage ap_sa;
int ap_mask;
};
#define AG_TYPE_UNKNOWN 0
#define AG_TYPE_DENY 1
#define AG_TYPE_NO_AUTHENTICATION 2
#define AG_TYPE_CHAP 3
#define AG_TYPE_CHAP_MUTUAL 4
struct auth_group {
TAILQ_ENTRY(auth_group) ag_next;
struct conf *ag_conf;
char *ag_name;
struct target *ag_target;
int ag_type;
TAILQ_HEAD(, auth) ag_auths;
TAILQ_HEAD(, auth_name) ag_names;
TAILQ_HEAD(, auth_portal) ag_portals;
};
struct portal {
TAILQ_ENTRY(portal) p_next;
struct portal_group *p_portal_group;
bool p_iser;
char *p_listen;
struct addrinfo *p_ai;
#ifdef ICL_KERNEL_PROXY
int p_id;
#endif
TAILQ_HEAD(, target) p_targets;
int p_socket;
};
#define PG_FILTER_UNKNOWN 0
#define PG_FILTER_NONE 1
#define PG_FILTER_PORTAL 2
#define PG_FILTER_PORTAL_NAME 3
#define PG_FILTER_PORTAL_NAME_AUTH 4
struct portal_group {
TAILQ_ENTRY(portal_group) pg_next;
struct conf *pg_conf;
char *pg_name;
struct auth_group *pg_discovery_auth_group;
int pg_discovery_filter;
bool pg_unassigned;
TAILQ_HEAD(, portal) pg_portals;
char *pg_redirection;
uint16_t pg_tag;
};
struct lun_option {
TAILQ_ENTRY(lun_option) lo_next;
struct lun *lo_lun;
char *lo_name;
char *lo_value;
};
struct lun {
TAILQ_ENTRY(lun) l_next;
struct conf *l_conf;
TAILQ_HEAD(, lun_option) l_options;
char *l_name;
char *l_backend;
int l_blocksize;
char *l_device_id;
char *l_path;
char *l_scsiname;
char *l_serial;
int64_t l_size;
int l_ctl_lun;
};
struct target {
TAILQ_ENTRY(target) t_next;
struct conf *t_conf;
struct lun *t_luns[MAX_LUNS];
struct auth_group *t_auth_group;
struct portal_group *t_portal_group;
char *t_name;
char *t_alias;
char *t_redirection;
uint32_t t_ctl_port;
};
struct isns {
TAILQ_ENTRY(isns) i_next;
struct conf *i_conf;
char *i_addr;
struct addrinfo *i_ai;
};
struct conf {
char *conf_pidfile_path;
TAILQ_HEAD(, lun) conf_luns;
TAILQ_HEAD(, target) conf_targets;
TAILQ_HEAD(, auth_group) conf_auth_groups;
TAILQ_HEAD(, portal_group) conf_portal_groups;
TAILQ_HEAD(, isns) conf_isns;
int conf_isns_period;
int conf_isns_timeout;
int conf_debug;
int conf_timeout;
int conf_maxproc;
uint16_t conf_last_portal_group_tag;
#ifdef ICL_KERNEL_PROXY
int conf_portal_id;
#endif
struct pidfh *conf_pidfh;
bool conf_default_pg_defined;
bool conf_default_ag_defined;
bool conf_kernel_port_on;
};
#define CONN_SESSION_TYPE_NONE 0
#define CONN_SESSION_TYPE_DISCOVERY 1
#define CONN_SESSION_TYPE_NORMAL 2
#define CONN_DIGEST_NONE 0
#define CONN_DIGEST_CRC32C 1
struct connection {
struct portal *conn_portal;
struct target *conn_target;
int conn_socket;
int conn_session_type;
char *conn_initiator_name;
char *conn_initiator_addr;
char *conn_initiator_alias;
uint8_t conn_initiator_isid[6];
struct sockaddr_storage conn_initiator_sa;
uint32_t conn_cmdsn;
uint32_t conn_statsn;
size_t conn_max_data_segment_length;
size_t conn_max_burst_length;
int conn_immediate_data;
int conn_header_digest;
int conn_data_digest;
const char *conn_user;
struct chap *conn_chap;
};
struct pdu {
struct connection *pdu_connection;
struct iscsi_bhs *pdu_bhs;
char *pdu_data;
size_t pdu_data_len;
};
#define KEYS_MAX 1024
struct keys {
char *keys_names[KEYS_MAX];
char *keys_values[KEYS_MAX];
char *keys_data;
size_t keys_data_len;
};
#define CHAP_CHALLENGE_LEN 1024
struct chap {
unsigned char chap_id;
char chap_challenge[CHAP_CHALLENGE_LEN];
char chap_response[MD5_DIGEST_LENGTH];
};
struct rchap {
char *rchap_secret;
unsigned char rchap_id;
void *rchap_challenge;
size_t rchap_challenge_len;
};
struct chap *chap_new(void);
char *chap_get_id(const struct chap *chap);
char *chap_get_challenge(const struct chap *chap);
int chap_receive(struct chap *chap, const char *response);
int chap_authenticate(struct chap *chap,
const char *secret);
void chap_delete(struct chap *chap);
struct rchap *rchap_new(const char *secret);
int rchap_receive(struct rchap *rchap,
const char *id, const char *challenge);
char *rchap_get_response(struct rchap *rchap);
void rchap_delete(struct rchap *rchap);
struct conf *conf_new(void);
struct conf *conf_new_from_file(const char *path);
struct conf *conf_new_from_kernel(void);
void conf_delete(struct conf *conf);
int conf_verify(struct conf *conf);
struct auth_group *auth_group_new(struct conf *conf, const char *name);
void auth_group_delete(struct auth_group *ag);
struct auth_group *auth_group_find(const struct conf *conf,
const char *name);
int auth_group_set_type(struct auth_group *ag,
const char *type);
const struct auth *auth_new_chap(struct auth_group *ag,
const char *user, const char *secret);
const struct auth *auth_new_chap_mutual(struct auth_group *ag,
const char *user, const char *secret,
const char *user2, const char *secret2);
const struct auth *auth_find(const struct auth_group *ag,
const char *user);
const struct auth_name *auth_name_new(struct auth_group *ag,
const char *initiator_name);
bool auth_name_defined(const struct auth_group *ag);
const struct auth_name *auth_name_find(const struct auth_group *ag,
const char *initiator_name);
int auth_name_check(const struct auth_group *ag,
const char *initiator_name);
const struct auth_portal *auth_portal_new(struct auth_group *ag,
const char *initiator_portal);
bool auth_portal_defined(const struct auth_group *ag);
const struct auth_portal *auth_portal_find(const struct auth_group *ag,
const struct sockaddr_storage *sa);
int auth_portal_check(const struct auth_group *ag,
const struct sockaddr_storage *sa);
struct portal_group *portal_group_new(struct conf *conf, const char *name);
void portal_group_delete(struct portal_group *pg);
struct portal_group *portal_group_find(const struct conf *conf,
const char *name);
int portal_group_add_listen(struct portal_group *pg,
const char *listen, bool iser);
int portal_group_set_filter(struct portal_group *pg,
const char *filter);
int portal_group_set_redirection(struct portal_group *pg,
const char *addr);
int isns_new(struct conf *conf, const char *addr);
void isns_delete(struct isns *is);
void isns_register(struct isns *isns, struct isns *oldisns);
void isns_check(struct isns *isns);
void isns_deregister(struct isns *isns);
struct target *target_new(struct conf *conf, const char *name);
void target_delete(struct target *target);
struct target *target_find(struct conf *conf,
const char *name);
int target_set_redirection(struct target *target,
const char *addr);
void target_set_ctl_port(struct target *target,
uint32_t value);
struct lun *lun_new(struct conf *conf, const char *name);
void lun_delete(struct lun *lun);
struct lun *lun_find(const struct conf *conf, const char *name);
void lun_set_backend(struct lun *lun, const char *value);
void lun_set_blocksize(struct lun *lun, size_t value);
void lun_set_device_id(struct lun *lun, const char *value);
void lun_set_path(struct lun *lun, const char *value);
void lun_set_scsiname(struct lun *lun, const char *value);
void lun_set_serial(struct lun *lun, const char *value);
void lun_set_size(struct lun *lun, size_t value);
void lun_set_ctl_lun(struct lun *lun, uint32_t value);
struct lun_option *lun_option_new(struct lun *lun,
const char *name, const char *value);
void lun_option_delete(struct lun_option *clo);
struct lun_option *lun_option_find(const struct lun *lun,
const char *name);
void lun_option_set(struct lun_option *clo,
const char *value);
void kernel_init(void);
int kernel_lun_add(struct lun *lun);
int kernel_lun_resize(struct lun *lun);
int kernel_lun_remove(struct lun *lun);
void kernel_handoff(struct connection *conn);
int kernel_port_add(struct target *targ);
int kernel_port_update(struct target *targ);
int kernel_port_remove(struct target *targ);
void kernel_capsicate(void);
#ifdef ICL_KERNEL_PROXY
void kernel_listen(struct addrinfo *ai, bool iser,
int portal_id);
void kernel_accept(int *connection_id, int *portal_id,
struct sockaddr *client_sa,
socklen_t *client_salen);
void kernel_send(struct pdu *pdu);
void kernel_receive(struct pdu *pdu);
#endif
struct keys *keys_new(void);
void keys_delete(struct keys *keys);
void keys_load(struct keys *keys, const struct pdu *pdu);
void keys_save(struct keys *keys, struct pdu *pdu);
const char *keys_find(struct keys *keys, const char *name);
int keys_find_int(struct keys *keys, const char *name);
void keys_add(struct keys *keys,
const char *name, const char *value);
void keys_add_int(struct keys *keys,
const char *name, int value);
struct pdu *pdu_new(struct connection *conn);
struct pdu *pdu_new_response(struct pdu *request);
void pdu_delete(struct pdu *pdu);
void pdu_receive(struct pdu *request);
void pdu_send(struct pdu *response);
void login(struct connection *conn);
void discovery(struct connection *conn);
void log_init(int level);
void log_set_peer_name(const char *name);
void log_set_peer_addr(const char *addr);
void log_err(int, const char *, ...)
__dead2 __printflike(2, 3);
void log_errx(int, const char *, ...)
__dead2 __printflike(2, 3);
void log_warn(const char *, ...) __printflike(1, 2);
void log_warnx(const char *, ...) __printflike(1, 2);
void log_debugx(const char *, ...) __printflike(1, 2);
char *checked_strdup(const char *);
bool valid_iscsi_name(const char *name);
void set_timeout(int timeout, int fatal);
bool timed_out(void);
#endif /* !CTLD_H */
|