1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 The FreeBSD Foundation
5 *
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33#ifndef ISCSID_H
34#define	ISCSID_H
35
36#include <stdbool.h>
37#include <stdint.h>
38
39#include <iscsi_ioctl.h>
40
41#define	DEFAULT_PIDFILE			"/var/run/iscsid.pid"
42
43#define	CONN_DIGEST_NONE		0
44#define	CONN_DIGEST_CRC32C		1
45
46#define	CONN_MUTUAL_CHALLENGE_LEN	1024
47#define	SOCKBUF_SIZE			1048576
48
49struct connection {
50	int			conn_iscsi_fd;
51	int			conn_socket;
52	unsigned int		conn_session_id;
53	struct iscsi_session_conf	conn_conf;
54	struct iscsi_session_limits	conn_limits;
55	char			conn_target_alias[ISCSI_ADDR_LEN];
56	uint8_t			conn_isid[6];
57	uint16_t		conn_tsih;
58	uint32_t		conn_statsn;
59	int			conn_protocol_level;
60	int			conn_header_digest;
61	int			conn_data_digest;
62	bool			conn_initial_r2t;
63	bool			conn_immediate_data;
64	int			conn_max_recv_data_segment_length;
65	int			conn_max_send_data_segment_length;
66	int			conn_max_burst_length;
67	int			conn_first_burst_length;
68	struct chap		*conn_mutual_chap;
69};
70
71struct pdu {
72	struct connection	*pdu_connection;
73	struct iscsi_bhs	*pdu_bhs;
74	char			*pdu_data;
75	size_t			pdu_data_len;
76};
77
78#define	KEYS_MAX		1024
79
80struct keys {
81	char			*keys_names[KEYS_MAX];
82	char			*keys_values[KEYS_MAX];
83	char			*keys_data;
84	size_t			keys_data_len;
85};
86
87#define	CHAP_CHALLENGE_LEN	1024
88#define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
89
90struct chap {
91	unsigned char	chap_id;
92	char		chap_challenge[CHAP_CHALLENGE_LEN];
93	char		chap_response[CHAP_DIGEST_LEN];
94};
95
96struct rchap {
97	char		*rchap_secret;
98	unsigned char	rchap_id;
99	void		*rchap_challenge;
100	size_t		rchap_challenge_len;
101};
102
103struct chap		*chap_new(void);
104char			*chap_get_id(const struct chap *chap);
105char			*chap_get_challenge(const struct chap *chap);
106int			chap_receive(struct chap *chap, const char *response);
107int			chap_authenticate(struct chap *chap,
108			    const char *secret);
109void			chap_delete(struct chap *chap);
110
111struct rchap		*rchap_new(const char *secret);
112int			rchap_receive(struct rchap *rchap,
113			    const char *id, const char *challenge);
114char			*rchap_get_response(struct rchap *rchap);
115void			rchap_delete(struct rchap *rchap);
116
117struct keys		*keys_new(void);
118void			keys_delete(struct keys *key);
119void			keys_load(struct keys *keys, const struct pdu *pdu);
120void			keys_save(struct keys *keys, struct pdu *pdu);
121const char		*keys_find(struct keys *keys, const char *name);
122void			keys_add(struct keys *keys,
123			    const char *name, const char *value);
124void			keys_add_int(struct keys *keys,
125			    const char *name, int value);
126
127struct pdu		*pdu_new(struct connection *ic);
128struct pdu		*pdu_new_response(struct pdu *request);
129void			pdu_receive(struct pdu *request);
130void			pdu_send(struct pdu *response);
131void			pdu_delete(struct pdu *ip);
132
133void			login(struct connection *ic);
134
135void			discovery(struct connection *ic);
136
137void			log_init(int level);
138void			log_set_peer_name(const char *name);
139void			log_set_peer_addr(const char *addr);
140void			log_err(int, const char *, ...)
141			    __dead2 __printflike(2, 3);
142void			log_errx(int, const char *, ...)
143			    __dead2 __printflike(2, 3);
144void			log_warn(const char *, ...) __printflike(1, 2);
145void			log_warnx(const char *, ...) __printflike(1, 2);
146void			log_debugx(const char *, ...) __printflike(1, 2);
147
148char			*checked_strdup(const char *);
149bool			timed_out(void);
150void			fail(const struct connection *, const char *);
151
152#endif /* !ISCSID_H */
153