forked from medallia/goipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.go
280 lines (231 loc) · 6.31 KB
/
simulator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ipmi
import (
"bytes"
"hash/adler32"
"log"
"net"
"sync"
)
const authTypeSupport = (1 << AuthTypeNone) | (1 << AuthTypeMD5) | (1 << AuthTypePassword)
// Handler function
type Handler func(*Message) Response
// Simulator for IPMI
type Simulator struct {
wg sync.WaitGroup
addr net.UDPAddr
conn *net.UDPConn
handlers map[NetworkFunction]map[Command]Handler
ids map[uint32]string
bopts [BootParamInitMbox + 1][]uint8
}
// NewSimulator constructs a Simulator with the given addr
func NewSimulator(addr net.UDPAddr) *Simulator {
s := &Simulator{
addr: addr,
ids: map[uint32]string{},
handlers: map[NetworkFunction]map[Command]Handler{},
}
// Built-in handlers for session management
s.handlers[NetworkFunctionApp] = map[Command]Handler{
CommandGetDeviceID: s.deviceID,
CommandGetAuthCapabilities: s.authCapabilities,
CommandGetSessionChallenge: s.sessionChallenge,
CommandActivateSession: s.sessionActivate,
CommandSetSessionPrivilegeLevel: s.sessionPrivilege,
CommandCloseSession: s.sessionClose,
}
// Built-in handlers for chassis commands
s.handlers[NetworkFunctionChassis] = map[Command]Handler{
CommandChassisStatus: s.chassisStatus,
CommandGetSystemBootOptions: s.getSystemBootOptions,
CommandSetSystemBootOptions: s.setSystemBootOptions,
}
return s
}
// SetHandler sets the command handler for the given netfn and command
func (s *Simulator) SetHandler(netfn NetworkFunction, command Command, handler Handler) {
s.handlers[netfn][command] = handler
}
// NewConnection to this Simulator instance
func (s *Simulator) NewConnection() *Connection {
addr := s.LocalAddr()
return &Connection{
Hostname: addr.IP.String(),
Port: addr.Port,
Interface: "lan",
}
}
// LocalAddr returns the address the server is bound to.
func (s *Simulator) LocalAddr() *net.UDPAddr {
if s.conn != nil {
return s.conn.LocalAddr().(*net.UDPAddr)
}
return nil
}
// Run the Simulator.
func (s *Simulator) Run() error {
var err error
s.conn, err = net.ListenUDP("udp4", &s.addr)
if err != nil {
return err
}
s.wg.Add(1)
go func() {
_ = s.serve()
s.wg.Done()
}()
return nil
}
// Stop the Simulator.
func (s *Simulator) Stop() {
_ = s.conn.Close()
s.wg.Wait()
}
func (s *Simulator) chassisStatus(*Message) Response {
return &ChassisStatusResponse{
CompletionCode: CommandCompleted,
PowerState: SystemPower,
}
}
func (s *Simulator) getSystemBootOptions(m *Message) Response {
r := &SystemBootOptionsRequest{}
if err := m.Request(r); err != nil {
return err
}
return &SystemBootOptionsResponse{
CompletionCode: CommandCompleted,
Version: 0x01,
Param: r.Param,
Data: s.bopts[r.Param],
}
}
func (s *Simulator) setSystemBootOptions(m *Message) Response {
r := &SetSystemBootOptionsRequest{}
if err := m.Request(r); err != nil {
return err
}
s.bopts[r.Param] = r.Data
return &SetSystemBootOptionsResponse{}
}
func (s *Simulator) deviceID(*Message) Response {
return &DeviceIDResponse{
CompletionCode: CommandCompleted,
IPMIVersion: 0x51, // 1.5
}
}
func (s *Simulator) authCapabilities(*Message) Response {
return &AuthCapabilitiesResponse{
CompletionCode: CommandCompleted,
ChannelNumber: 0x01,
AuthTypeSupport: authTypeSupport,
}
}
func (s *Simulator) sessionChallenge(m *Message) Response {
// Convert username to a uint32 and use as the SessionID.
// The SessionID will be propagated such that all requests
// for this session include the ID, which can be used to
// dispatch requests.
username := bytes.TrimRight(m.Data[1:], "\000")
hash := adler32.New()
_, err := hash.Write(username)
if err != nil {
panic(err)
}
id := hash.Sum32()
s.ids[id] = string(username)
return &SessionChallengeResponse{
CompletionCode: CommandCompleted,
TemporarySessionID: id,
}
}
func (s *Simulator) sessionActivate(m *Message) Response {
return &ActivateSessionResponse{
CompletionCode: CommandCompleted,
AuthType: m.AuthType,
SessionID: m.SessionID,
InboundSeq: m.Sequence,
MaxPriv: PrivLevelAdmin,
}
}
func (s *Simulator) sessionPrivilege(m *Message) Response {
return &SessionPrivilegeLevelResponse{
CompletionCode: CommandCompleted,
NewPrivilegeLevel: m.Data[0],
}
}
func (s *Simulator) sessionClose(*Message) Response {
return CommandCompleted
}
func (s *Simulator) ipmiCommand(m *Message) []byte {
response := Response(ErrInvalidCommand)
if commands, ok := s.handlers[m.NetFn()]; ok {
if handler, ok := commands[m.Command]; ok {
m.RequestID = s.ids[m.SessionID]
response = handler(m)
}
}
return m.toBytes(response)
}
func (s *Simulator) asfCommand(m *asfMessage) []byte {
if m.MessageType != asfMessageTypePing {
log.Print(m.unsupportedMessageType())
return []byte{} // TODO: general ASF error code?
}
m.MessageType = asfMessageTypePong
response := asfPong{
IANAEnterpriseNumber: asfIANA,
SupportedEntities: 0x81, // IPMI
}
return m.toBytes(&response)
}
func (s *Simulator) serve() error {
buf := make([]byte, ipmiBufSize)
for {
var response []byte
var err error
n, addr, err := s.conn.ReadFrom(buf)
if err != nil {
return err // conn closed
}
header, err := rmcpHeaderFromBytes(buf)
if err != nil {
log.Print(err)
continue
}
switch header.Class {
case rmcpClassASF:
m, err := asfMessageFromBytes(buf)
if err != nil {
log.Print(err)
continue
}
response = s.asfCommand(m)
case rmcpClassIPMI:
m, err := messageFromBytes(buf[:n])
if err != nil {
log.Print(err)
continue
}
response = s.ipmiCommand(m)
default:
log.Print(header.unsupportedClass())
continue
}
_, err = s.conn.WriteTo(response, addr)
if err != nil {
return err // conn closed
}
}
}