1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistribution of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistribution in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind.
20  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
23  * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
25  * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
26  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
27  * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
28  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
29  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  */
32 
33 #ifndef IPMI_INTF_H
34 #define IPMI_INTF_H
35 
36 #include <ipmitool/ipmi.h>
37 #include <ipmitool/ipmi_oem.h>
38 #include <ipmitool/ipmi_constants.h>
39 
40 #include <sys/types.h>
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 
45 /*
46  * An enumeration that describes every possible session state for
47  * an IPMIv2 / RMCP+ session.
48  */
49 enum LANPLUS_SESSION_STATE {
50 	LANPLUS_STATE_PRESESSION = 0,
51 	LANPLUS_STATE_OPEN_SESSION_SENT,
52 	LANPLUS_STATE_OPEN_SESSION_RECEIEVED,
53 	LANPLUS_STATE_RAKP_1_SENT,
54 	LANPLUS_STATE_RAKP_2_RECEIVED,
55 	LANPLUS_STATE_RAKP_3_SENT,
56 	LANPLUS_STATE_ACTIVE,
57 	LANPLUS_STATE_CLOSE_SENT,
58 };
59 
60 
61 #define IPMI_AUTHCODE_BUFFER_SIZE 20
62 #define IPMI_SIK_BUFFER_SIZE      20
63 #define IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
64 
65 struct ipmi_session_params {
66 	char * hostname;
67 	uint8_t username[17];
68 	uint8_t authcode_set[IPMI_AUTHCODE_BUFFER_SIZE + 1];
69 	uint8_t authtype_set;
70 	uint8_t privlvl;
71 	uint8_t cipher_suite_id;
72 	char sol_escape_char;
73 	int password;
74 	int port;
75 	int retry;
76 	uint32_t timeout;
77 	uint8_t kg[IPMI_KG_BUFFER_SIZE];   /* BMC key */
78 	uint8_t lookupbit;
79 };
80 
81 #define IPMI_AUTHSTATUS_PER_MSG_DISABLED	0x10
82 #define IPMI_AUTHSTATUS_PER_USER_DISABLED	0x08
83 #define IPMI_AUTHSTATUS_NONNULL_USERS_ENABLED	0x04
84 #define IPMI_AUTHSTATUS_NULL_USERS_ENABLED	0x02
85 #define IPMI_AUTHSTATUS_ANONYMOUS_USERS_ENABLED	0x01
86 
87 struct ipmi_session {
88 	int active;
89 	uint32_t session_id;
90 	uint32_t in_seq;
91 	uint32_t out_seq;
92 
93 	uint8_t authcode[IPMI_AUTHCODE_BUFFER_SIZE + 1];
94 	uint8_t challenge[16];
95 	uint8_t authtype;
96 	uint8_t authstatus;
97 	uint8_t authextra;
98 	uint32_t timeout;
99 
100 	struct sockaddr_storage addr;
101 	socklen_t addrlen;
102 
103 	/*
104 	 * This struct holds state data specific to IPMI v2 / RMCP+ sessions
105 	 */
106 	struct {
107 		enum LANPLUS_SESSION_STATE session_state;
108 
109 		/* These are the algorithms agreed upon for the session */
110 		uint8_t requested_auth_alg;
111 		uint8_t requested_integrity_alg;
112 		uint8_t requested_crypt_alg;
113 		uint8_t auth_alg;
114 		uint8_t integrity_alg;
115 		uint8_t crypt_alg;
116 		uint8_t max_priv_level;
117 
118 		uint32_t console_id;
119 		uint32_t bmc_id;
120 
121 		/*
122 		 * Values required for RAKP mesages
123 		 */
124 
125 		/* Random number generated byt the console */
126 		uint8_t console_rand[16];
127 		/* Random number generated by the BMC */
128 		uint8_t bmc_rand[16];
129 
130 		uint8_t bmc_guid[16];
131 		uint8_t requested_role;   /* As sent in the RAKP 1 message */
132 		uint8_t rakp2_return_code;
133 
134 		uint8_t sik[IPMI_SIK_BUFFER_SIZE]; /* Session integrity key */
135 		uint8_t kg[IPMI_KG_BUFFER_SIZE];   /* BMC key */
136 		uint8_t k1[20];   /* Used for Integrity checking? */
137 		uint8_t k2[20];   /* First 16 bytes used for AES  */
138 	} v2_data;
139 
140 
141 	/*
142 	 * This data is specific to the Serial Over Lan session
143 	 */
144 	struct {
145 		uint16_t max_inbound_payload_size;
146 		uint16_t max_outbound_payload_size;
147 		uint16_t port;
148 		uint8_t sequence_number;
149 
150 		/*  This data describes the last SOL packet */
151 		uint8_t last_received_sequence_number;
152 		uint8_t last_received_byte_count;
153 		void (*sol_input_handler)(struct ipmi_rs * rsp);
154 	} sol_data;
155 };
156 
157 struct ipmi_cmd {
158 	int (*func)(struct ipmi_intf * intf, int argc, char ** argv);
159 	const char * name;
160 	const char * desc;
161 };
162 
163 struct ipmi_intf_support {
164 	const char * name;
165 	int supported;
166 };
167 
168 struct ipmi_intf {
169 	char name[16];
170 	char desc[128];
171 	char *devfile;
172 	int fd;
173 	int opened;
174 	int abort;
175 	int noanswer;
176 	int picmg_avail;
177 	int vita_avail;
178 	IPMI_OEM manufacturer_id;
179 
180 	struct ipmi_session_params ssn_params;
181 	struct ipmi_session * session;
182 	struct ipmi_oem_handle * oem;
183 	struct ipmi_cmd * cmdlist;
184 	uint8_t	target_ipmb_addr;
185 	uint32_t my_addr;
186 	uint32_t target_addr;
187 	uint8_t target_lun;
188 	uint8_t target_channel;
189 	uint32_t transit_addr;
190 	uint8_t transit_channel;
191 	uint16_t max_request_data_size;
192 	uint16_t max_response_data_size;
193 
194 	uint8_t devnum;
195 
196 	int (*setup)(struct ipmi_intf * intf);
197 	int (*open)(struct ipmi_intf * intf);
198 	void (*close)(struct ipmi_intf * intf);
199 	struct ipmi_rs *(*sendrecv)(struct ipmi_intf * intf, struct ipmi_rq * req);
200 	int (*sendrsp)(struct ipmi_intf * intf, struct ipmi_rs * rsp);
201 	struct ipmi_rs *(*recv_sol)(struct ipmi_intf * intf);
202 	struct ipmi_rs *(*send_sol)(struct ipmi_intf * intf, struct ipmi_v2_payload * payload);
203 	int (*keepalive)(struct ipmi_intf * intf);
204 	int (*set_my_addr)(struct ipmi_intf * intf, uint8_t addr);
205 	void (*set_max_request_data_size)(struct ipmi_intf * intf, uint16_t size);
206 	void (*set_max_response_data_size)(struct ipmi_intf * intf, uint16_t size);
207 };
208 
209 struct ipmi_intf * ipmi_intf_load(char * name);
210 void ipmi_intf_print(struct ipmi_intf_support * intflist);
211 
212 void ipmi_intf_session_set_hostname(struct ipmi_intf * intf, char * hostname);
213 void ipmi_intf_session_set_username(struct ipmi_intf * intf, char * username);
214 void ipmi_intf_session_set_password(struct ipmi_intf * intf, char * password);
215 void ipmi_intf_session_set_privlvl(struct ipmi_intf * intf, uint8_t privlvl);
216 void ipmi_intf_session_set_lookupbit(struct ipmi_intf * intf, uint8_t lookupbit);
217 void ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf, uint8_t cipher_suite_id);
218 void ipmi_intf_session_set_sol_escape_char(struct ipmi_intf * intf, char sol_escape_char);
219 void ipmi_intf_session_set_kgkey(struct ipmi_intf * intf, char * kgkey);
220 void ipmi_intf_session_set_port(struct ipmi_intf * intf, int port);
221 void ipmi_intf_session_set_authtype(struct ipmi_intf * intf, uint8_t authtype);
222 void ipmi_intf_session_set_timeout(struct ipmi_intf * intf, uint32_t timeout);
223 void ipmi_intf_session_set_retry(struct ipmi_intf * intf, int retry);
224 void ipmi_intf_session_cleanup(struct ipmi_intf *intf);
225 void ipmi_cleanup(struct ipmi_intf * intf);
226 
227 #if defined(IPMI_INTF_LAN) || defined (IPMI_INTF_LANPLUS)
228 int  ipmi_intf_socket_connect(struct ipmi_intf * intf);
229 #endif
230 #endif /* IPMI_INTF_H */
231