]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - usr.sbin/ctld/ctld.h
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / usr.sbin / ctld / ctld.h
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #ifndef CTLD_H
33 #define CTLD_H
34
35 #include <sys/queue.h>
36 #ifdef ICL_KERNEL_PROXY
37 #include <sys/types.h>
38 #endif
39 #include <sys/socket.h>
40 #include <stdbool.h>
41 #include <libutil.h>
42
43 #define DEFAULT_CONFIG_PATH             "/etc/ctl.conf"
44 #define DEFAULT_PIDFILE                 "/var/run/ctld.pid"
45 #define DEFAULT_BLOCKSIZE               512
46 #define DEFAULT_CD_BLOCKSIZE            2048
47
48 #define MAX_LUNS                        1024
49 #define MAX_NAME_LEN                    223
50 #define MAX_DATA_SEGMENT_LENGTH         (128 * 1024)
51 #define MAX_BURST_LENGTH                16776192
52 #define SOCKBUF_SIZE                    1048576
53
54 struct auth {
55         TAILQ_ENTRY(auth)               a_next;
56         struct auth_group               *a_auth_group;
57         char                            *a_user;
58         char                            *a_secret;
59         char                            *a_mutual_user;
60         char                            *a_mutual_secret;
61 };
62
63 struct auth_name {
64         TAILQ_ENTRY(auth_name)          an_next;
65         struct auth_group               *an_auth_group;
66         char                            *an_initator_name;
67 };
68
69 struct auth_portal {
70         TAILQ_ENTRY(auth_portal)        ap_next;
71         struct auth_group               *ap_auth_group;
72         char                            *ap_initator_portal;
73         struct sockaddr_storage         ap_sa;
74         int                             ap_mask;
75 };
76
77 #define AG_TYPE_UNKNOWN                 0
78 #define AG_TYPE_DENY                    1
79 #define AG_TYPE_NO_AUTHENTICATION       2
80 #define AG_TYPE_CHAP                    3
81 #define AG_TYPE_CHAP_MUTUAL             4
82
83 struct auth_group {
84         TAILQ_ENTRY(auth_group)         ag_next;
85         struct conf                     *ag_conf;
86         char                            *ag_name;
87         struct target                   *ag_target;
88         int                             ag_type;
89         TAILQ_HEAD(, auth)              ag_auths;
90         TAILQ_HEAD(, auth_name)         ag_names;
91         TAILQ_HEAD(, auth_portal)       ag_portals;
92 };
93
94 struct portal {
95         TAILQ_ENTRY(portal)             p_next;
96         struct portal_group             *p_portal_group;
97         bool                            p_iser;
98         char                            *p_listen;
99         struct addrinfo                 *p_ai;
100 #ifdef ICL_KERNEL_PROXY
101         int                             p_id;
102 #endif
103
104         TAILQ_HEAD(, target)            p_targets;
105         int                             p_socket;
106 };
107
108 TAILQ_HEAD(options, option);
109
110 #define PG_FILTER_UNKNOWN               0
111 #define PG_FILTER_NONE                  1
112 #define PG_FILTER_PORTAL                2
113 #define PG_FILTER_PORTAL_NAME           3
114 #define PG_FILTER_PORTAL_NAME_AUTH      4
115
116 struct portal_group {
117         TAILQ_ENTRY(portal_group)       pg_next;
118         struct conf                     *pg_conf;
119         struct options                  pg_options;
120         char                            *pg_name;
121         struct auth_group               *pg_discovery_auth_group;
122         int                             pg_discovery_filter;
123         int                             pg_foreign;
124         bool                            pg_unassigned;
125         TAILQ_HEAD(, portal)            pg_portals;
126         TAILQ_HEAD(, port)              pg_ports;
127         char                            *pg_redirection;
128
129         uint16_t                        pg_tag;
130 };
131
132 struct pport {
133         TAILQ_ENTRY(pport)              pp_next;
134         TAILQ_HEAD(, port)              pp_ports;
135         struct conf                     *pp_conf;
136         char                            *pp_name;
137
138         uint32_t                        pp_ctl_port;
139 };
140
141 struct port {
142         TAILQ_ENTRY(port)               p_next;
143         TAILQ_ENTRY(port)               p_pgs;
144         TAILQ_ENTRY(port)               p_pps;
145         TAILQ_ENTRY(port)               p_ts;
146         struct conf                     *p_conf;
147         char                            *p_name;
148         struct auth_group               *p_auth_group;
149         struct portal_group             *p_portal_group;
150         struct pport                    *p_pport;
151         struct target                   *p_target;
152         int                             p_foreign;
153
154         uint32_t                        p_ctl_port;
155 };
156
157 struct option {
158         TAILQ_ENTRY(option)             o_next;
159         char                            *o_name;
160         char                            *o_value;
161 };
162
163 struct lun {
164         TAILQ_ENTRY(lun)                l_next;
165         struct conf                     *l_conf;
166         struct options                  l_options;
167         char                            *l_name;
168         char                            *l_backend;
169         uint8_t                         l_device_type;
170         int                             l_blocksize;
171         char                            *l_device_id;
172         char                            *l_path;
173         char                            *l_scsiname;
174         char                            *l_serial;
175         int64_t                         l_size;
176
177         int                             l_ctl_lun;
178 };
179
180 struct target {
181         TAILQ_ENTRY(target)             t_next;
182         struct conf                     *t_conf;
183         struct lun                      *t_luns[MAX_LUNS];
184         struct auth_group               *t_auth_group;
185         TAILQ_HEAD(, port)              t_ports;
186         char                            *t_name;
187         char                            *t_alias;
188         char                            *t_redirection;
189 };
190
191 struct isns {
192         TAILQ_ENTRY(isns)               i_next;
193         struct conf                     *i_conf;
194         char                            *i_addr;
195         struct addrinfo                 *i_ai;
196 };
197
198 struct conf {
199         char                            *conf_pidfile_path;
200         TAILQ_HEAD(, lun)               conf_luns;
201         TAILQ_HEAD(, target)            conf_targets;
202         TAILQ_HEAD(, auth_group)        conf_auth_groups;
203         TAILQ_HEAD(, port)              conf_ports;
204         TAILQ_HEAD(, portal_group)      conf_portal_groups;
205         TAILQ_HEAD(, pport)             conf_pports;
206         TAILQ_HEAD(, isns)              conf_isns;
207         int                             conf_isns_period;
208         int                             conf_isns_timeout;
209         int                             conf_debug;
210         int                             conf_timeout;
211         int                             conf_maxproc;
212
213 #ifdef ICL_KERNEL_PROXY
214         int                             conf_portal_id;
215 #endif
216         struct pidfh                    *conf_pidfh;
217
218         bool                            conf_default_pg_defined;
219         bool                            conf_default_ag_defined;
220         bool                            conf_kernel_port_on;
221 };
222
223 #define CONN_SESSION_TYPE_NONE          0
224 #define CONN_SESSION_TYPE_DISCOVERY     1
225 #define CONN_SESSION_TYPE_NORMAL        2
226
227 #define CONN_DIGEST_NONE                0
228 #define CONN_DIGEST_CRC32C              1
229
230 struct connection {
231         struct portal           *conn_portal;
232         struct port             *conn_port;
233         struct target           *conn_target;
234         int                     conn_socket;
235         int                     conn_session_type;
236         char                    *conn_initiator_name;
237         char                    *conn_initiator_addr;
238         char                    *conn_initiator_alias;
239         uint8_t                 conn_initiator_isid[6];
240         struct sockaddr_storage conn_initiator_sa;
241         uint32_t                conn_cmdsn;
242         uint32_t                conn_statsn;
243         size_t                  conn_max_data_segment_length;
244         size_t                  conn_max_burst_length;
245         int                     conn_immediate_data;
246         int                     conn_header_digest;
247         int                     conn_data_digest;
248         const char              *conn_user;
249         struct chap             *conn_chap;
250 };
251
252 struct pdu {
253         struct connection       *pdu_connection;
254         struct iscsi_bhs        *pdu_bhs;
255         char                    *pdu_data;
256         size_t                  pdu_data_len;
257 };
258
259 #define KEYS_MAX        1024
260
261 struct keys {
262         char            *keys_names[KEYS_MAX];
263         char            *keys_values[KEYS_MAX];
264         char            *keys_data;
265         size_t          keys_data_len;
266 };
267
268 #define CHAP_CHALLENGE_LEN      1024
269 #define CHAP_DIGEST_LEN         16 /* Equal to MD5 digest size. */
270
271 struct chap {
272         unsigned char   chap_id;
273         char            chap_challenge[CHAP_CHALLENGE_LEN];
274         char            chap_response[CHAP_DIGEST_LEN];
275 };
276
277 struct rchap {
278         char            *rchap_secret;
279         unsigned char   rchap_id;
280         void            *rchap_challenge;
281         size_t          rchap_challenge_len;
282 };
283
284 struct chap             *chap_new(void);
285 char                    *chap_get_id(const struct chap *chap);
286 char                    *chap_get_challenge(const struct chap *chap);
287 int                     chap_receive(struct chap *chap, const char *response);
288 int                     chap_authenticate(struct chap *chap,
289                             const char *secret);
290 void                    chap_delete(struct chap *chap);
291
292 struct rchap            *rchap_new(const char *secret);
293 int                     rchap_receive(struct rchap *rchap,
294                             const char *id, const char *challenge);
295 char                    *rchap_get_response(struct rchap *rchap);
296 void                    rchap_delete(struct rchap *rchap);
297
298 struct conf             *conf_new(void);
299 struct conf             *conf_new_from_file(const char *path, struct conf *old);
300 struct conf             *conf_new_from_kernel(void);
301 void                    conf_delete(struct conf *conf);
302 int                     conf_verify(struct conf *conf);
303
304 struct auth_group       *auth_group_new(struct conf *conf, const char *name);
305 void                    auth_group_delete(struct auth_group *ag);
306 struct auth_group       *auth_group_find(const struct conf *conf,
307                             const char *name);
308 int                     auth_group_set_type(struct auth_group *ag,
309                             const char *type);
310
311 const struct auth       *auth_new_chap(struct auth_group *ag,
312                             const char *user, const char *secret);
313 const struct auth       *auth_new_chap_mutual(struct auth_group *ag,
314                             const char *user, const char *secret,
315                             const char *user2, const char *secret2);
316 const struct auth       *auth_find(const struct auth_group *ag,
317                             const char *user);
318
319 const struct auth_name  *auth_name_new(struct auth_group *ag,
320                             const char *initiator_name);
321 bool                    auth_name_defined(const struct auth_group *ag);
322 const struct auth_name  *auth_name_find(const struct auth_group *ag,
323                             const char *initiator_name);
324 int                     auth_name_check(const struct auth_group *ag,
325                             const char *initiator_name);
326
327 const struct auth_portal        *auth_portal_new(struct auth_group *ag,
328                                     const char *initiator_portal);
329 bool                    auth_portal_defined(const struct auth_group *ag);
330 const struct auth_portal        *auth_portal_find(const struct auth_group *ag,
331                                     const struct sockaddr_storage *sa);
332 int                             auth_portal_check(const struct auth_group *ag,
333                                     const struct sockaddr_storage *sa);
334
335 struct portal_group     *portal_group_new(struct conf *conf, const char *name);
336 void                    portal_group_delete(struct portal_group *pg);
337 struct portal_group     *portal_group_find(const struct conf *conf,
338                             const char *name);
339 int                     portal_group_add_listen(struct portal_group *pg,
340                             const char *listen, bool iser);
341 int                     portal_group_set_filter(struct portal_group *pg,
342                             const char *filter);
343 int                     portal_group_set_redirection(struct portal_group *pg,
344                             const char *addr);
345
346 int                     isns_new(struct conf *conf, const char *addr);
347 void                    isns_delete(struct isns *is);
348 void                    isns_register(struct isns *isns, struct isns *oldisns);
349 void                    isns_check(struct isns *isns);
350 void                    isns_deregister(struct isns *isns);
351
352 struct pport            *pport_new(struct conf *conf, const char *name,
353                             uint32_t ctl_port);
354 struct pport            *pport_find(const struct conf *conf, const char *name);
355 struct pport            *pport_copy(struct pport *pport, struct conf *conf);
356 void                    pport_delete(struct pport *pport);
357
358 struct port             *port_new(struct conf *conf, struct target *target,
359                             struct portal_group *pg);
360 struct port             *port_new_pp(struct conf *conf, struct target *target,
361                             struct pport *pp);
362 struct port             *port_find(const struct conf *conf, const char *name);
363 struct port             *port_find_in_pg(const struct portal_group *pg,
364                             const char *target);
365 void                    port_delete(struct port *port);
366
367 struct target           *target_new(struct conf *conf, const char *name);
368 void                    target_delete(struct target *target);
369 struct target           *target_find(struct conf *conf,
370                             const char *name);
371 int                     target_set_redirection(struct target *target,
372                             const char *addr);
373
374 struct lun              *lun_new(struct conf *conf, const char *name);
375 void                    lun_delete(struct lun *lun);
376 struct lun              *lun_find(const struct conf *conf, const char *name);
377 void                    lun_set_backend(struct lun *lun, const char *value);
378 void                    lun_set_device_type(struct lun *lun, uint8_t value);
379 void                    lun_set_blocksize(struct lun *lun, size_t value);
380 void                    lun_set_device_id(struct lun *lun, const char *value);
381 void                    lun_set_path(struct lun *lun, const char *value);
382 void                    lun_set_scsiname(struct lun *lun, const char *value);
383 void                    lun_set_serial(struct lun *lun, const char *value);
384 void                    lun_set_size(struct lun *lun, size_t value);
385 void                    lun_set_ctl_lun(struct lun *lun, uint32_t value);
386
387 struct option           *option_new(struct options *os,
388                             const char *name, const char *value);
389 void                    option_delete(struct options *os, struct option *co);
390 struct option           *option_find(const struct options *os, const char *name);
391 void                    option_set(struct option *o, const char *value);
392
393 void                    kernel_init(void);
394 int                     kernel_lun_add(struct lun *lun);
395 int                     kernel_lun_modify(struct lun *lun);
396 int                     kernel_lun_remove(struct lun *lun);
397 void                    kernel_handoff(struct connection *conn);
398 int                     kernel_port_add(struct port *port);
399 int                     kernel_port_update(struct port *port, struct port *old);
400 int                     kernel_port_remove(struct port *port);
401 void                    kernel_capsicate(void);
402
403 #ifdef ICL_KERNEL_PROXY
404 void                    kernel_listen(struct addrinfo *ai, bool iser,
405                             int portal_id);
406 void                    kernel_accept(int *connection_id, int *portal_id,
407                             struct sockaddr *client_sa,
408                             socklen_t *client_salen);
409 void                    kernel_send(struct pdu *pdu);
410 void                    kernel_receive(struct pdu *pdu);
411 #endif
412
413 struct keys             *keys_new(void);
414 void                    keys_delete(struct keys *keys);
415 void                    keys_load(struct keys *keys, const struct pdu *pdu);
416 void                    keys_save(struct keys *keys, struct pdu *pdu);
417 const char              *keys_find(struct keys *keys, const char *name);
418 void                    keys_add(struct keys *keys,
419                             const char *name, const char *value);
420 void                    keys_add_int(struct keys *keys,
421                             const char *name, int value);
422
423 struct pdu              *pdu_new(struct connection *conn);
424 struct pdu              *pdu_new_response(struct pdu *request);
425 void                    pdu_delete(struct pdu *pdu);
426 void                    pdu_receive(struct pdu *request);
427 void                    pdu_send(struct pdu *response);
428
429 void                    login(struct connection *conn);
430
431 void                    discovery(struct connection *conn);
432
433 void                    log_init(int level);
434 void                    log_set_peer_name(const char *name);
435 void                    log_set_peer_addr(const char *addr);
436 void                    log_err(int, const char *, ...)
437                             __dead2 __printflike(2, 3);
438 void                    log_errx(int, const char *, ...)
439                             __dead2 __printflike(2, 3);
440 void                    log_warn(const char *, ...) __printflike(1, 2);
441 void                    log_warnx(const char *, ...) __printflike(1, 2);
442 void                    log_debugx(const char *, ...) __printflike(1, 2);
443
444 char                    *checked_strdup(const char *);
445 bool                    valid_iscsi_name(const char *name);
446 void                    set_timeout(int timeout, int fatal);
447 bool                    timed_out(void);
448
449 #endif /* !CTLD_H */