1171568Sscottl/*-
2211095Sdes * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
3171568Sscottl * All rights reserved.
4171568Sscottl *
5171568Sscottl * Redistribution and use in source and binary forms, with or without
6171568Sscottl * modification, are permitted provided that the following conditions
7171568Sscottl * are met:
8171568Sscottl * 1. Redistributions of source code must retain the above copyright
9171568Sscottl *    notice, this list of conditions and the following disclaimer.
10171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11171568Sscottl *    notice, this list of conditions and the following disclaimer in the
12171568Sscottl *    documentation and/or other materials provided with the distribution.
13171568Sscottl *
14171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171568Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171568Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171568Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171568Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171568Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171568Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171568Sscottl * SUCH DAMAGE.
25171568Sscottl *
26171568Sscottl * $FreeBSD$
27171568Sscottl */
28171568Sscottl/*
29171568Sscottl | $Id: iscontrol.h,v 2.3 2007/04/27 08:36:49 danny Exp danny $
30171568Sscottl */
31171568Sscottl#ifdef DEBUG
32171568Sscottlint vflag;
33171568Sscottl
34171568Sscottl# define debug(level, fmt, args...)	do {if (level <= vflag) printf("%s: " fmt "\n", __func__ , ##args);} while(0)
35171568Sscottl# define debug_called(level)		do {if (level <= vflag) printf("%s: called\n", __func__);} while(0)
36171568Sscottl#else
37171568Sscottl# define debug(level, fmt, args...)
38171568Sscottl# define debug_called(level)
39171568Sscottl#endif // DEBUG
40171568Sscottl#define xdebug(fmt, args...)	printf("%s: " fmt "\n", __func__ , ##args)
41171568Sscottl
42171568Sscottl#define BIT(n)	(1 <<(n))
43171568Sscottl
44171568Sscottl#define MAXREDIRECTS	2
45171568Sscottl
46171568Sscottltypedef int auth_t(void *sess);
47171568Sscottl
48185289Sscottltypedef struct {
49185289Sscottl     char      *address;
50185289Sscottl     int       port;
51185289Sscottl     int       pgt;
52185289Sscottl} target_t;
53185289Sscottl
54171568Sscottltypedef struct isess {
55171568Sscottl     int	flags;
56171568Sscottl#define SESS_CONNECTED		BIT(0)
57171568Sscottl#define SESS_DISCONNECT		BIT(1)
58171568Sscottl#define SESS_LOGGEDIN		BIT(2)
59171568Sscottl#define SESS_RECONNECT		BIT(3)
60171568Sscottl#define SESS_REDIRECT		BIT(4)
61171568Sscottl
62171568Sscottl#define SESS_NEGODONE		BIT(10)	// XXX: kludge
63171568Sscottl
64171568Sscottl#define SESS_FULLFEATURE	BIT(29)
65171568Sscottl#define SESS_INITIALLOGIN1	BIT(30)
66171568Sscottl#define SESS_INITIALLOGIN	BIT(31)
67171568Sscottl
68171568Sscottl
69171568Sscottl     isc_opt_t	*op;		// operational values
70185289Sscottl     target_t  target;         // the Original target address
71171568Sscottl     int	fd;		// the session fd
72171568Sscottl     int	soc;		// the socket
73171568Sscottl     iscsi_cam_t	cam;
74171568Sscottl     struct cam_device	*camdev;
75171568Sscottl
76171568Sscottl     time_t	open_time;
77171568Sscottl     int	redirect_cnt;
78171568Sscottl     time_t	redirect_time;
79171568Sscottl     int	reconnect_cnt;
80171568Sscottl     int	reconnect_cnt1;
81171568Sscottl     time_t	reconnect_time;
82171568Sscottl     char	isid[6+1];
83171568Sscottl     int	csg;		// current stage
84171568Sscottl     int	nsg;		// next stage
85171568Sscottl     // Phases/Stages
86171568Sscottl#define	SN_PHASE	0	// Security Negotiation
87171568Sscottl#define LON_PHASE	1	// Login Operational Negotiation
88171568Sscottl#define FF_PHASE	3	// FuLL-Feature
89171568Sscottl     uint	tsih;
90171568Sscottl     sn_t	sn;
91171568Sscottl} isess_t;
92171568Sscottl
93171568Sscottltypedef struct token {
94171568Sscottl     char	*name;
95171568Sscottl     int	val;
96171568Sscottl} token_t;
97171568Sscottl
98171568Sscottltypedef enum {
99171568Sscottl     NONE	= 0,
100171568Sscottl     KRB5,
101171568Sscottl     SPKM1,
102171568Sscottl     SPKM2,
103171568Sscottl     SRP,
104171568Sscottl     CHAP
105171568Sscottl} authm_t;
106171568Sscottl
107171568Sscottlextern token_t AuthMethods[];
108171568Sscottlextern token_t DigestMethods[];
109171568Sscottl
110171568Sscottltypedef enum {
111171568Sscottl     SET,
112171568Sscottl     GET
113171568Sscottl} oper_t;
114171568Sscottl
115171568Sscottltypedef enum {
116171568Sscottl     U_PR,	// private
117171568Sscottl     U_IO,	// Initialize Only -- during login
118171568Sscottl     U_LO,	// Leading Only -- when TSIH is zero
119171568Sscottl     U_FFPO,	// Full Feature Phase Only
120171568Sscottl     U_ALL	// in any phase
121171568Sscottl} usage_t;
122171568Sscottl
123171568Sscottltypedef enum {
124171568Sscottl     S_PR,
125171568Sscottl     S_CO,	// Connect only
126171568Sscottl     S_SW	// Session Wide
127171568Sscottl} scope_t;
128171568Sscottl
129171568Sscottltypedef void keyfun_t(isess_t *, oper_t);
130171568Sscottl
131171568Sscottltypedef struct {
132171568Sscottl     usage_t	usage;
133171568Sscottl     scope_t	scope;
134171568Sscottl     char	*name;
135171568Sscottl     int	tokenID;
136171568Sscottl} textkey_t;
137171568Sscottl
138171568Sscottltypedef int handler_t(isess_t *sess, pdu_t *pp);
139171568Sscottl
140171568Sscottlint	authenticateLogin(isess_t *sess);
141171568Sscottlint	fsm(isc_opt_t *op);
142171568Sscottlint	sendPDU(isess_t *sess, pdu_t *pp, handler_t *hdlr);
143171568Sscottlint	addText(pdu_t *pp, char *fmt, ...);
144171568Sscottlvoid	freePDU(pdu_t *pp);
145171568Sscottlint	xmitpdu(isess_t *sess, pdu_t *pp);
146171568Sscottlint	recvpdu(isess_t *sess, pdu_t *pp);
147171568Sscottl
148171568Sscottlint	lookup(token_t *tbl, char *m);
149171568Sscottl
150171568Sscottlint	vflag;
151171568Sscottlchar	*iscsidev;
152171568Sscottl
153171568Sscottlvoid	parseArgs(int nargs, char **args, isc_opt_t *op);
154171568Sscottlvoid	parseConfig(FILE *fd, char *key, isc_opt_t *op);
155171568Sscottl
156171568Sscottlchar	*chapDigest(char *ap, char id, char *cp, char *chapSecret);
157211095Sdeschar	*genChapChallenge(char *encoding, uint len);
158171568Sscottl
159171568Sscottlint	str2bin(char *str, char **rsp);
160171568Sscottlchar	*bin2str(char *fmt, unsigned char *md, int blen);
161171568Sscottl
162171568Sscottlint	negotiateOPV(isess_t *sess);
163171568Sscottlint	setOptions(isess_t *sess, int flag);
164171568Sscottl
165171568Sscottlint	loginPhase(isess_t *sess);
166