]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/ctld/ctld.h
MFC r270179:
[FreeBSD/stable/10.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
47 #define MAX_NAME_LEN                    223
48 #define MAX_DATA_SEGMENT_LENGTH         (128 * 1024)
49 #define MAX_BURST_LENGTH                16776192
50
51 struct auth {
52         TAILQ_ENTRY(auth)               a_next;
53         struct auth_group               *a_auth_group;
54         char                            *a_user;
55         char                            *a_secret;
56         char                            *a_mutual_user;
57         char                            *a_mutual_secret;
58 };
59
60 struct auth_name {
61         TAILQ_ENTRY(auth_name)          an_next;
62         struct auth_group               *an_auth_group;
63         char                            *an_initator_name;
64 };
65
66 struct auth_portal {
67         TAILQ_ENTRY(auth_portal)        ap_next;
68         struct auth_group               *ap_auth_group;
69         char                            *ap_initator_portal;
70         struct sockaddr_storage         ap_sa;
71         int                             ap_mask;
72 };
73
74 #define AG_TYPE_UNKNOWN                 0
75 #define AG_TYPE_DENY                    1
76 #define AG_TYPE_NO_AUTHENTICATION       2
77 #define AG_TYPE_CHAP                    3
78 #define AG_TYPE_CHAP_MUTUAL             4
79
80 struct auth_group {
81         TAILQ_ENTRY(auth_group)         ag_next;
82         struct conf                     *ag_conf;
83         char                            *ag_name;
84         struct target                   *ag_target;
85         int                             ag_type;
86         TAILQ_HEAD(, auth)              ag_auths;
87         TAILQ_HEAD(, auth_name)         ag_names;
88         TAILQ_HEAD(, auth_portal)       ag_portals;
89 };
90
91 struct portal {
92         TAILQ_ENTRY(portal)             p_next;
93         struct portal_group             *p_portal_group;
94         bool                            p_iser;
95         char                            *p_listen;
96         struct addrinfo                 *p_ai;
97 #ifdef ICL_KERNEL_PROXY
98         int                             p_id;
99 #endif
100
101         TAILQ_HEAD(, target)            p_targets;
102         int                             p_socket;
103 };
104
105 struct portal_group {
106         TAILQ_ENTRY(portal_group)       pg_next;
107         struct conf                     *pg_conf;
108         char                            *pg_name;
109         struct auth_group               *pg_discovery_auth_group;
110         bool                            pg_unassigned;
111         TAILQ_HEAD(, portal)            pg_portals;
112
113         uint16_t                        pg_tag;
114 };
115
116 struct lun_option {
117         TAILQ_ENTRY(lun_option)         lo_next;
118         struct lun                      *lo_lun;
119         char                            *lo_name;
120         char                            *lo_value;
121 };
122
123 struct lun {
124         TAILQ_ENTRY(lun)                l_next;
125         TAILQ_HEAD(, lun_option)        l_options;
126         struct target                   *l_target;
127         int                             l_lun;
128         char                            *l_backend;
129         int                             l_blocksize;
130         char                            *l_device_id;
131         char                            *l_path;
132         char                            *l_serial;
133         int64_t                         l_size;
134
135         int                             l_ctl_lun;
136 };
137
138 struct target {
139         TAILQ_ENTRY(target)             t_next;
140         TAILQ_HEAD(, lun)               t_luns;
141         struct conf                     *t_conf;
142         struct auth_group               *t_auth_group;
143         struct portal_group             *t_portal_group;
144         char                            *t_name;
145         char                            *t_alias;
146 };
147
148 struct conf {
149         char                            *conf_pidfile_path;
150         TAILQ_HEAD(, target)            conf_targets;
151         TAILQ_HEAD(, auth_group)        conf_auth_groups;
152         TAILQ_HEAD(, portal_group)      conf_portal_groups;
153         int                             conf_debug;
154         int                             conf_timeout;
155         int                             conf_maxproc;
156
157         uint16_t                        conf_last_portal_group_tag;
158 #ifdef ICL_KERNEL_PROXY
159         int                             conf_portal_id;
160 #endif
161         struct pidfh                    *conf_pidfh;
162
163         bool                            conf_default_pg_defined;
164         bool                            conf_default_ag_defined;
165         bool                            conf_kernel_port_on;
166 };
167
168 #define CONN_SESSION_TYPE_NONE          0
169 #define CONN_SESSION_TYPE_DISCOVERY     1
170 #define CONN_SESSION_TYPE_NORMAL        2
171
172 #define CONN_DIGEST_NONE                0
173 #define CONN_DIGEST_CRC32C              1
174
175 struct connection {
176         struct portal           *conn_portal;
177         struct target           *conn_target;
178         int                     conn_socket;
179         int                     conn_session_type;
180         char                    *conn_initiator_name;
181         char                    *conn_initiator_addr;
182         char                    *conn_initiator_alias;
183         uint8_t                 conn_initiator_isid[6];
184         struct sockaddr_storage conn_initiator_sa;
185         uint32_t                conn_cmdsn;
186         uint32_t                conn_statsn;
187         size_t                  conn_max_data_segment_length;
188         size_t                  conn_max_burst_length;
189         int                     conn_immediate_data;
190         int                     conn_header_digest;
191         int                     conn_data_digest;
192 };
193
194 struct pdu {
195         struct connection       *pdu_connection;
196         struct iscsi_bhs        *pdu_bhs;
197         char                    *pdu_data;
198         size_t                  pdu_data_len;
199 };
200
201 #define KEYS_MAX        1024
202
203 struct keys {
204         char            *keys_names[KEYS_MAX];
205         char            *keys_values[KEYS_MAX];
206         char            *keys_data;
207         size_t          keys_data_len;
208 };
209
210 struct conf             *conf_new(void);
211 struct conf             *conf_new_from_file(const char *path);
212 struct conf             *conf_new_from_kernel(void);
213 void                    conf_delete(struct conf *conf);
214 int                     conf_verify(struct conf *conf);
215
216 struct auth_group       *auth_group_new(struct conf *conf, const char *name);
217 void                    auth_group_delete(struct auth_group *ag);
218 struct auth_group       *auth_group_find(const struct conf *conf,
219                             const char *name);
220 int                     auth_group_set_type_str(struct auth_group *ag,
221                             const char *type);
222
223 const struct auth       *auth_new_chap(struct auth_group *ag,
224                             const char *user, const char *secret);
225 const struct auth       *auth_new_chap_mutual(struct auth_group *ag,
226                             const char *user, const char *secret,
227                             const char *user2, const char *secret2);
228 const struct auth       *auth_find(const struct auth_group *ag,
229                             const char *user);
230
231 const struct auth_name  *auth_name_new(struct auth_group *ag,
232                             const char *initiator_name);
233 bool                    auth_name_defined(const struct auth_group *ag);
234 const struct auth_name  *auth_name_find(const struct auth_group *ag,
235                             const char *initiator_name);
236
237 const struct auth_portal        *auth_portal_new(struct auth_group *ag,
238                                     const char *initiator_portal);
239 bool                    auth_portal_defined(const struct auth_group *ag);
240 const struct auth_portal        *auth_portal_find(const struct auth_group *ag,
241                                     const struct sockaddr_storage *sa);
242
243 struct portal_group     *portal_group_new(struct conf *conf, const char *name);
244 void                    portal_group_delete(struct portal_group *pg);
245 struct portal_group     *portal_group_find(const struct conf *conf,
246                             const char *name);
247 int                     portal_group_add_listen(struct portal_group *pg,
248                             const char *listen, bool iser);
249
250 struct target           *target_new(struct conf *conf, const char *name);
251 void                    target_delete(struct target *target);
252 struct target           *target_find(struct conf *conf,
253                             const char *name);
254
255 struct lun              *lun_new(struct target *target, int lun_id);
256 void                    lun_delete(struct lun *lun);
257 struct lun              *lun_find(const struct target *target, int lun_id);
258 void                    lun_set_backend(struct lun *lun, const char *value);
259 void                    lun_set_blocksize(struct lun *lun, size_t value);
260 void                    lun_set_device_id(struct lun *lun, const char *value);
261 void                    lun_set_path(struct lun *lun, const char *value);
262 void                    lun_set_serial(struct lun *lun, const char *value);
263 void                    lun_set_size(struct lun *lun, size_t value);
264 void                    lun_set_ctl_lun(struct lun *lun, uint32_t value);
265
266 struct lun_option       *lun_option_new(struct lun *lun,
267                             const char *name, const char *value);
268 void                    lun_option_delete(struct lun_option *clo);
269 struct lun_option       *lun_option_find(const struct lun *lun,
270                             const char *name);
271 void                    lun_option_set(struct lun_option *clo,
272                             const char *value);
273
274 void                    kernel_init(void);
275 int                     kernel_lun_add(struct lun *lun);
276 int                     kernel_lun_resize(struct lun *lun);
277 int                     kernel_lun_remove(struct lun *lun);
278 void                    kernel_handoff(struct connection *conn);
279 int                     kernel_port_add(struct target *targ);
280 int                     kernel_port_remove(struct target *targ);
281 void                    kernel_capsicate(void);
282
283 #ifdef ICL_KERNEL_PROXY
284 void                    kernel_listen(struct addrinfo *ai, bool iser,
285                             int portal_id);
286 void                    kernel_accept(int *connection_id, int *portal_id,
287                             struct sockaddr *client_sa,
288                             socklen_t *client_salen);
289 void                    kernel_send(struct pdu *pdu);
290 void                    kernel_receive(struct pdu *pdu);
291 #endif
292
293 struct keys             *keys_new(void);
294 void                    keys_delete(struct keys *keys);
295 void                    keys_load(struct keys *keys, const struct pdu *pdu);
296 void                    keys_save(struct keys *keys, struct pdu *pdu);
297 const char              *keys_find(struct keys *keys, const char *name);
298 int                     keys_find_int(struct keys *keys, const char *name);
299 void                    keys_add(struct keys *keys,
300                             const char *name, const char *value);
301 void                    keys_add_int(struct keys *keys,
302                             const char *name, int value);
303
304 struct pdu              *pdu_new(struct connection *conn);
305 struct pdu              *pdu_new_response(struct pdu *request);
306 void                    pdu_delete(struct pdu *pdu);
307 void                    pdu_receive(struct pdu *request);
308 void                    pdu_send(struct pdu *response);
309
310 void                    login(struct connection *conn);
311
312 void                    discovery(struct connection *conn);
313
314 void                    log_init(int level);
315 void                    log_set_peer_name(const char *name);
316 void                    log_set_peer_addr(const char *addr);
317 void                    log_err(int, const char *, ...)
318                             __dead2 __printflike(2, 3);
319 void                    log_errx(int, const char *, ...)
320                             __dead2 __printflike(2, 3);
321 void                    log_warn(const char *, ...) __printflike(1, 2);
322 void                    log_warnx(const char *, ...) __printflike(1, 2);
323 void                    log_debugx(const char *, ...) __printflike(1, 2);
324
325 char                    *checked_strdup(const char *);
326 bool                    valid_iscsi_name(const char *name);
327 bool                    timed_out(void);
328
329 #endif /* !CTLD_H */