forked from vmware-archive/goipmi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lan.go
386 lines (318 loc) · 7.04 KB
/
lan.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
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 (
"crypto/md5"
"crypto/rand"
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"time"
)
type lan struct {
*Connection
ipmiSession
rqSeq uint8
conn net.Conn
active bool
authcode [16]uint8
username [16]uint8
priv uint8
lun uint8
timeout time.Duration
}
func newLanTransport(c *Connection) transport {
l := &lan{Connection: c}
copy(l.username[:], c.Username[:])
copy(l.authcode[:], c.Password[:])
return l
}
func (l *lan) dial() (net.Conn, error) {
// TODO: support more than just udp4
addr := net.JoinHostPort(l.Hostname, strconv.Itoa(l.Port))
return net.Dial("udp4", addr)
}
func (l *lan) open() error {
conn, err := l.dial()
if err != nil {
return err
}
l.conn = conn
// TODO: options
l.priv = PrivLevelAdmin
l.timeout = time.Second * 5
l.lun = 0
return l.openSession()
}
func (l *lan) close() error {
if l.active {
err := l.closeSession()
if err != nil {
log.Printf("error closing session: %s", err)
}
l.active = false
}
if l.conn != nil {
_ = l.conn.Close()
l.conn = nil
}
return nil
}
func (l *lan) send(req *Request, res Response) error {
err := l.sendPacket(l.message(req))
if err != nil {
return err
}
m, err := l.recvMessage()
if err != nil {
return err
}
return m.Response(res)
}
func (*lan) Console() error {
fmt.Println("Console not supported. Press Enter to continue.")
r := make([]byte, 1)
_, err := os.Stdin.Read(r)
return err
}
func (l *lan) sendPacket(buf []byte) error {
_, err := l.conn.Write(buf)
return err
}
func (l *lan) recvPacket() ([]byte, error) {
buf := make([]byte, ipmiBufSize)
err := l.conn.SetReadDeadline(time.Now().Add(l.timeout))
if err != nil {
return nil, err
}
n, err := l.conn.Read(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
func (l *lan) recvMessage() (*Message, error) {
buf, err := l.recvPacket()
if err != nil {
return nil, err
}
header, err := rmcpHeaderFromBytes(buf)
if err != nil {
return nil, err
}
if header.Class != rmcpClassIPMI {
return nil, header.unsupportedClass()
}
return messageFromBytes(buf)
}
func (l *lan) nextSequence() uint32 {
if l.Sequence != 0 {
l.Sequence++
}
return l.Sequence
}
func (l *lan) nextRqSeq() uint8 {
l.rqSeq++
return l.rqSeq << 2
}
func (l *lan) message(r *Request) []byte {
m := &Message{
rmcpHeader: &rmcpHeader{
Version: rmcpVersion1,
Class: rmcpClassIPMI,
RMCPSequenceNumber: 0xff,
},
ipmiSession: &ipmiSession{
Sequence: l.nextSequence(),
SessionID: l.SessionID,
},
ipmiHeader: &ipmiHeader{
RsAddr: 0x20, // bmcSlaveAddr
NetFnRsLUN: uint8(r.NetworkFunction)<<2 | l.lun&3,
Command: r.Command,
RqAddr: 0x81, // remoteSWID
RqSeq: l.nextRqSeq(),
},
}
if l.active && l.AuthType != 0 {
copy(m.AuthCode[:], l.authcode[:])
m.AuthType = l.AuthType
}
msg := m.toBytes(r.Data)
if l.active && l.AuthType == AuthTypeMD5 {
hlen := rmcpHeaderSize + ipmiSessionSize
// offset is location of ipmiHeader.RsAddr
offset := hlen + len(m.AuthCode) + 1
// rewrite m.AuthCode field
md5 := l.authMD5(msg[offset:])
copy(msg[hlen:], md5)
}
return msg
}
// per section 22.17.1
func (l *lan) authMD5(data []uint8) []uint8 {
h := md5.New()
binaryWrite(h, l.authcode)
binaryWrite(h, l.SessionID)
binaryWrite(h, data)
binaryWrite(h, l.Sequence)
binaryWrite(h, l.authcode)
return h.Sum(nil)
}
func (l *lan) openSession() error {
if err := l.ping(); err != nil {
return err
}
if err := l.getAuthCapabilities(); err != nil {
return err
}
res, err := l.getSessionChallenge()
if err != nil {
return err
}
if err := l.activateSession(res); err != nil {
return err
}
return l.setSessionPriv()
}
func (l *lan) ping() error {
msg := &asfMessage{
rmcpHeader: &rmcpHeader{
Version: rmcpVersion1,
Class: rmcpClassASF,
RMCPSequenceNumber: 0xff,
},
asfHeader: &asfHeader{
IANAEnterpriseNumber: asfIANA,
MessageType: asfMessageTypePing,
},
}
if err := l.sendPacket(msg.toBytes(nil)); err != nil {
return err
}
buf, err := l.recvPacket()
if err != nil {
return err
}
m, err := asfMessageFromBytes(buf)
if err != nil {
return err
}
if m.MessageType != asfMessageTypePong {
return m.unsupportedMessageType()
}
pong := &asfPong{}
if err := m.response(pong); err != nil {
return err
}
if !pong.valid() {
return errors.New("IPMI not supported")
}
return nil
}
func (l *lan) getAuthCapabilities() error {
req := &Request{
NetworkFunctionApp,
CommandGetAuthCapabilities,
AuthCapabilitiesRequest{
ChannelNumber: 0x0e, // lanChannelE
PrivLevel: l.priv,
},
}
res := &AuthCapabilitiesResponse{}
if err := l.send(req, res); err != nil {
return err
}
for _, t := range []uint8{AuthTypeMD5, AuthTypePassword, AuthTypeNone} {
if (res.AuthTypeSupport & (1 << t)) != 0 {
l.AuthType = t
break
}
log.Printf("BMC did not offer a supported AuthType")
return CompletionCode(0xd4)
}
return nil
}
func (l *lan) getSessionChallenge() (*SessionChallengeResponse, error) {
req := &Request{
NetworkFunctionApp,
CommandGetSessionChallenge,
SessionChallengeRequest{
AuthType: l.AuthType,
Username: l.username,
},
}
res := &SessionChallengeResponse{}
if err := l.send(req, res); err != nil {
return nil, err
}
l.SessionID = res.TemporarySessionID
return res, nil
}
func (l *lan) inSeq() [4]uint8 {
seq := [4]uint8{}
if _, err := rand.Read(seq[:]); err != nil {
panic(err)
}
return seq
}
func (l *lan) activateSession(sc *SessionChallengeResponse) error {
req := &Request{
NetworkFunctionApp,
CommandActivateSession,
ActivateSessionRequest{
AuthType: l.AuthType,
PrivLevel: l.priv,
AuthCode: sc.Challenge,
InSeq: l.inSeq(),
},
}
res := &ActivateSessionResponse{}
l.active = true
if err := l.send(req, res); err != nil {
l.active = false
return err
}
l.SessionID = res.SessionID
l.AuthType = res.AuthType
l.Sequence = res.InboundSeq
return nil
}
func (l *lan) setSessionPriv() error {
req := &Request{
NetworkFunctionApp,
CommandSetSessionPrivilegeLevel,
SessionPrivilegeLevelRequest{
PrivLevel: l.priv,
},
}
res := &SessionPrivilegeLevelResponse{}
if err := l.send(req, res); err != nil {
return err
}
l.priv = res.NewPrivilegeLevel
return nil
}
func (l *lan) closeSession() error {
req := &Request{
NetworkFunctionApp,
CommandCloseSession,
CloseSessionRequest{
SessionID: l.SessionID,
},
}
return l.send(req, &CloseSessionResponse{})
}