xref: /freebsd/usr.sbin/iscsid/iscsid.h (revision 5e386598a6d77973b93c073080f0cc574edda9e2)
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 ISCSID_H
33 #define	ISCSID_H
34 
35 #include <stdbool.h>
36 #include <stdint.h>
37 
38 #include <iscsi_ioctl.h>
39 
40 #define	DEFAULT_PIDFILE			"/var/run/iscsid.pid"
41 
42 #define	CONN_DIGEST_NONE		0
43 #define	CONN_DIGEST_CRC32C		1
44 
45 #define	CONN_MUTUAL_CHALLENGE_LEN	1024
46 #define	SOCKBUF_SIZE			1048576
47 
48 struct connection {
49 	int			conn_iscsi_fd;
50 	int			conn_socket;
51 	unsigned int		conn_session_id;
52 	struct iscsi_session_conf	conn_conf;
53 	struct iscsi_session_limits	conn_limits;
54 	char			conn_target_alias[ISCSI_ADDR_LEN];
55 	uint8_t			conn_isid[6];
56 	uint16_t		conn_tsih;
57 	uint32_t		conn_statsn;
58 	int			conn_header_digest;
59 	int			conn_data_digest;
60 	bool			conn_initial_r2t;
61 	bool			conn_immediate_data;
62 	int			conn_max_recv_data_segment_length;
63 	int			conn_max_send_data_segment_length;
64 	int			conn_max_burst_length;
65 	int			conn_first_burst_length;
66 	struct chap		*conn_mutual_chap;
67 };
68 
69 struct pdu {
70 	struct connection	*pdu_connection;
71 	struct iscsi_bhs	*pdu_bhs;
72 	char			*pdu_data;
73 	size_t			pdu_data_len;
74 };
75 
76 #define	KEYS_MAX		1024
77 
78 struct keys {
79 	char			*keys_names[KEYS_MAX];
80 	char			*keys_values[KEYS_MAX];
81 	char			*keys_data;
82 	size_t			keys_data_len;
83 };
84 
85 #define	CHAP_CHALLENGE_LEN	1024
86 #define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
87 
88 struct chap {
89 	unsigned char	chap_id;
90 	char		chap_challenge[CHAP_CHALLENGE_LEN];
91 	char		chap_response[CHAP_DIGEST_LEN];
92 };
93 
94 struct rchap {
95 	char		*rchap_secret;
96 	unsigned char	rchap_id;
97 	void		*rchap_challenge;
98 	size_t		rchap_challenge_len;
99 };
100 
101 struct chap		*chap_new(void);
102 char			*chap_get_id(const struct chap *chap);
103 char			*chap_get_challenge(const struct chap *chap);
104 int			chap_receive(struct chap *chap, const char *response);
105 int			chap_authenticate(struct chap *chap,
106 			    const char *secret);
107 void			chap_delete(struct chap *chap);
108 
109 struct rchap		*rchap_new(const char *secret);
110 int			rchap_receive(struct rchap *rchap,
111 			    const char *id, const char *challenge);
112 char			*rchap_get_response(struct rchap *rchap);
113 void			rchap_delete(struct rchap *rchap);
114 
115 struct keys		*keys_new(void);
116 void			keys_delete(struct keys *key);
117 void			keys_load(struct keys *keys, const struct pdu *pdu);
118 void			keys_save(struct keys *keys, struct pdu *pdu);
119 const char		*keys_find(struct keys *keys, const char *name);
120 void			keys_add(struct keys *keys,
121 			    const char *name, const char *value);
122 void			keys_add_int(struct keys *keys,
123 			    const char *name, int value);
124 
125 struct pdu		*pdu_new(struct connection *ic);
126 struct pdu		*pdu_new_response(struct pdu *request);
127 void			pdu_receive(struct pdu *request);
128 void			pdu_send(struct pdu *response);
129 void			pdu_delete(struct pdu *ip);
130 
131 void			login(struct connection *ic);
132 
133 void			discovery(struct connection *ic);
134 
135 void			log_init(int level);
136 void			log_set_peer_name(const char *name);
137 void			log_set_peer_addr(const char *addr);
138 void			log_err(int, const char *, ...)
139 			    __dead2 __printflike(2, 3);
140 void			log_errx(int, const char *, ...)
141 			    __dead2 __printflike(2, 3);
142 void			log_warn(const char *, ...) __printflike(1, 2);
143 void			log_warnx(const char *, ...) __printflike(1, 2);
144 void			log_debugx(const char *, ...) __printflike(1, 2);
145 
146 char			*checked_strdup(const char *);
147 bool			timed_out(void);
148 void			fail(const struct connection *, const char *);
149 
150 #endif /* !ISCSID_H */
151