xref: /freebsd/lib/libiscsiutil/libiscsiutil.h (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
163783933SJohn Baldwin /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
363783933SJohn Baldwin  *
463783933SJohn Baldwin  * Copyright (c) 2012 The FreeBSD Foundation
563783933SJohn Baldwin  *
663783933SJohn Baldwin  * This software was developed by Edward Tomasz Napierala under sponsorship
763783933SJohn Baldwin  * from the FreeBSD Foundation.
863783933SJohn Baldwin  *
963783933SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
1063783933SJohn Baldwin  * modification, are permitted provided that the following conditions
1163783933SJohn Baldwin  * are met:
1263783933SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
1363783933SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
1463783933SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
1563783933SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
1663783933SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1763783933SJohn Baldwin  *
1863783933SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1963783933SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2063783933SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2163783933SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2263783933SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2363783933SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2463783933SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2563783933SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2663783933SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2763783933SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2863783933SJohn Baldwin  * SUCH DAMAGE.
2963783933SJohn Baldwin  */
3063783933SJohn Baldwin 
3163783933SJohn Baldwin #ifndef __LIBISCSIUTIL_H__
3263783933SJohn Baldwin #define	__LIBISCSIUTIL_H__
3363783933SJohn Baldwin 
3463783933SJohn Baldwin #include <sys/types.h>
3563783933SJohn Baldwin #include <stdbool.h>
3663783933SJohn Baldwin 
3763783933SJohn Baldwin struct connection_ops;
3863783933SJohn Baldwin 
3963783933SJohn Baldwin #define	CONN_DIGEST_NONE		0
4063783933SJohn Baldwin #define	CONN_DIGEST_CRC32C		1
4163783933SJohn Baldwin 
4263783933SJohn Baldwin struct connection {
4363783933SJohn Baldwin 	const struct connection_ops *conn_ops;
4463783933SJohn Baldwin 	int		conn_socket;
4563783933SJohn Baldwin 	uint8_t		conn_isid[6];
4663783933SJohn Baldwin 	uint16_t	conn_tsih;
4763783933SJohn Baldwin 	uint32_t	conn_cmdsn;
4863783933SJohn Baldwin 	uint32_t	conn_statsn;
4963783933SJohn Baldwin 	int		conn_header_digest;
5063783933SJohn Baldwin 	int		conn_data_digest;
5163783933SJohn Baldwin 	bool		conn_immediate_data;
5263783933SJohn Baldwin 	bool		conn_use_proxy;
5363783933SJohn Baldwin 	int		conn_max_recv_data_segment_length;
5463783933SJohn Baldwin 	int		conn_max_send_data_segment_length;
5563783933SJohn Baldwin 	int		conn_max_burst_length;
5663783933SJohn Baldwin 	int		conn_first_burst_length;
57bd6bb493SRichard Scheffenegger 	int		conn_ping_timeout;
58bd6bb493SRichard Scheffenegger 	int		conn_login_timeout;
5963783933SJohn Baldwin };
6063783933SJohn Baldwin 
6163783933SJohn Baldwin struct pdu {
6263783933SJohn Baldwin 	struct connection *pdu_connection;
6363783933SJohn Baldwin 	struct iscsi_bhs *pdu_bhs;
6463783933SJohn Baldwin 	char		*pdu_data;
6563783933SJohn Baldwin 	size_t		pdu_data_len;
6663783933SJohn Baldwin };
6763783933SJohn Baldwin 
6863783933SJohn Baldwin struct connection_ops {
6963783933SJohn Baldwin 	bool		(*timed_out)(void);
7063783933SJohn Baldwin 	void		(*pdu_receive_proxy)(struct pdu *);
7163783933SJohn Baldwin 	void		(*pdu_send_proxy)(struct pdu *);
7263783933SJohn Baldwin 	void		(*fail)(const struct connection *, const char *);
7363783933SJohn Baldwin };
7463783933SJohn Baldwin 
7563783933SJohn Baldwin #define	KEYS_MAX		1024
7663783933SJohn Baldwin 
7763783933SJohn Baldwin struct keys {
7863783933SJohn Baldwin 	char		*keys_names[KEYS_MAX];
7963783933SJohn Baldwin 	char		*keys_values[KEYS_MAX];
8063783933SJohn Baldwin };
8163783933SJohn Baldwin 
8263783933SJohn Baldwin #define	CHAP_CHALLENGE_LEN	1024
8363783933SJohn Baldwin #define	CHAP_DIGEST_LEN		16 /* Equal to MD5 digest size. */
8463783933SJohn Baldwin 
8563783933SJohn Baldwin struct chap {
8663783933SJohn Baldwin 	unsigned char	chap_id;
8763783933SJohn Baldwin 	char		chap_challenge[CHAP_CHALLENGE_LEN];
8863783933SJohn Baldwin 	char		chap_response[CHAP_DIGEST_LEN];
8963783933SJohn Baldwin };
9063783933SJohn Baldwin 
9163783933SJohn Baldwin struct rchap {
9263783933SJohn Baldwin 	char		*rchap_secret;
9363783933SJohn Baldwin 	unsigned char	rchap_id;
9463783933SJohn Baldwin 	void		*rchap_challenge;
9563783933SJohn Baldwin 	size_t		rchap_challenge_len;
9663783933SJohn Baldwin };
9763783933SJohn Baldwin 
9863783933SJohn Baldwin struct chap		*chap_new(void);
9963783933SJohn Baldwin char			*chap_get_id(const struct chap *chap);
10063783933SJohn Baldwin char			*chap_get_challenge(const struct chap *chap);
10163783933SJohn Baldwin int			chap_receive(struct chap *chap, const char *response);
10263783933SJohn Baldwin int			chap_authenticate(struct chap *chap,
10363783933SJohn Baldwin 			    const char *secret);
10463783933SJohn Baldwin void			chap_delete(struct chap *chap);
10563783933SJohn Baldwin 
10663783933SJohn Baldwin struct rchap		*rchap_new(const char *secret);
10763783933SJohn Baldwin int			rchap_receive(struct rchap *rchap,
10863783933SJohn Baldwin 			    const char *id, const char *challenge);
10963783933SJohn Baldwin char			*rchap_get_response(struct rchap *rchap);
11063783933SJohn Baldwin void			rchap_delete(struct rchap *rchap);
11163783933SJohn Baldwin 
11263783933SJohn Baldwin struct keys		*keys_new(void);
11363783933SJohn Baldwin void			keys_delete(struct keys *key);
11425700db3SJohn Baldwin void			keys_load(struct keys *keys, const char *data,
11525700db3SJohn Baldwin 			    size_t len);
11625700db3SJohn Baldwin void			keys_save(struct keys *keys, char **datap,
11725700db3SJohn Baldwin 			    size_t *lenp);
11863783933SJohn Baldwin const char		*keys_find(struct keys *keys, const char *name);
11963783933SJohn Baldwin void			keys_add(struct keys *keys,
12063783933SJohn Baldwin 			    const char *name, const char *value);
12163783933SJohn Baldwin void			keys_add_int(struct keys *keys,
12263783933SJohn Baldwin 			    const char *name, int value);
12363783933SJohn Baldwin 
12425700db3SJohn Baldwin static __inline void
keys_load_pdu(struct keys * keys,const struct pdu * pdu)12525700db3SJohn Baldwin keys_load_pdu(struct keys *keys, const struct pdu *pdu)
12625700db3SJohn Baldwin {
12725700db3SJohn Baldwin 	keys_load(keys, pdu->pdu_data, pdu->pdu_data_len);
12825700db3SJohn Baldwin }
12925700db3SJohn Baldwin 
13025700db3SJohn Baldwin static __inline void
keys_save_pdu(struct keys * keys,struct pdu * pdu)13125700db3SJohn Baldwin keys_save_pdu(struct keys *keys, struct pdu *pdu)
13225700db3SJohn Baldwin {
13325700db3SJohn Baldwin 	keys_save(keys, &pdu->pdu_data, &pdu->pdu_data_len);
13425700db3SJohn Baldwin }
13525700db3SJohn Baldwin 
13663783933SJohn Baldwin struct pdu		*pdu_new(struct connection *ic);
13763783933SJohn Baldwin struct pdu		*pdu_new_response(struct pdu *request);
13863783933SJohn Baldwin int			pdu_ahs_length(const struct pdu *pdu);
13963783933SJohn Baldwin int			pdu_data_segment_length(const struct pdu *pdu);
14063783933SJohn Baldwin void			pdu_set_data_segment_length(struct pdu *pdu,
14163783933SJohn Baldwin 			    uint32_t len);
14263783933SJohn Baldwin void			pdu_receive(struct pdu *request);
14363783933SJohn Baldwin void			pdu_send(struct pdu *response);
14463783933SJohn Baldwin void			pdu_delete(struct pdu *ip);
14563783933SJohn Baldwin 
146b4068979SJohn Baldwin void			text_send_request(struct connection *conn,
147b4068979SJohn Baldwin 			    struct keys *request_keys);
148b4068979SJohn Baldwin struct keys *		text_read_response(struct connection *conn);
149b4068979SJohn Baldwin struct keys *		text_read_request(struct connection *conn,
150b4068979SJohn Baldwin 			    struct pdu **requestp);
151b4068979SJohn Baldwin void			text_send_response(struct pdu *request,
152b4068979SJohn Baldwin 			    struct keys *response_keys);
153b4068979SJohn Baldwin 
15463783933SJohn Baldwin void			connection_init(struct connection *conn,
15563783933SJohn Baldwin 			    const struct connection_ops *ops, bool use_proxy);
15663783933SJohn Baldwin 
15763783933SJohn Baldwin void			log_init(int level);
15863783933SJohn Baldwin void			log_set_peer_name(const char *name);
15963783933SJohn Baldwin void			log_set_peer_addr(const char *addr);
16063783933SJohn Baldwin void			log_err(int, const char *, ...)
16163783933SJohn Baldwin 			    __dead2 __printflike(2, 3);
16263783933SJohn Baldwin void			log_errx(int, const char *, ...)
16363783933SJohn Baldwin 			    __dead2 __printflike(2, 3);
16463783933SJohn Baldwin void			log_warn(const char *, ...) __printflike(1, 2);
16563783933SJohn Baldwin void			log_warnx(const char *, ...) __printflike(1, 2);
16663783933SJohn Baldwin void			log_debugx(const char *, ...) __printflike(1, 2);
16763783933SJohn Baldwin 
16863783933SJohn Baldwin char			*checked_strdup(const char *);
16963783933SJohn Baldwin 
17063783933SJohn Baldwin #endif /* !__LIBISCSIUTIL_H__ */
171