-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2session.go
333 lines (292 loc) · 10.7 KB
/
v2session.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
package bmc
import (
"context"
"encoding/hex"
"fmt"
"hash"
"time"
"github.com/gebn/bmc/pkg/ipmi"
"github.com/gebn/bmc/pkg/layerexts"
"github.com/cenkalti/backoff/v4"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/prometheus/client_golang/prometheus"
)
// V2Session represents an established IPMI v2.0/RMCP+ session with a BMC.
type V2Session struct {
v2ConnectionLayers
*v2ConnectionShared
// decode parses the layers in v2ConnectionShared, plus a confidentiality
// layer.
decode gopacket.DecodingLayerFunc
// LocalID is the remote console's session ID, used by the BMC to send us
// packets.
LocalID uint32
// RemoteID is the managed system's session ID, used by us to send the BMC
// packets.
RemoteID uint32
// AuthenticatedSequenceNumbers is the pair of sequence numbers for
// authenticated packets.
AuthenticatedSequenceNumbers sequenceNumbers
// UnauthenticatedSequenceNumbers is the pair of sequence numbers for
// unauthenticated packets, i.e. those without an auth code. We only send
// unauthenticated packets to the BMC if IntegrityAlgorithmNone was
// negotiated.
UnauthenticatedSequenceNumbers sequenceNumbers
// SIK is the session integrity key, whose creation is described in section
// 13.31 of the specs. It is the result of applying the negotiated
// authentication algorithm (which is usually, but may not be, an HMAC) to
// some random numbers, the remote console's requested maximum privilege
// level, and username. The SIK is then used to derive K_1 and K_2 (and
// possibly more, but not for any algorithms in the spec) which are the keys
// for the integrity algorithm and confidentiality algorithms respectively.
SIK []byte
// AuthenticationAlgorithm is the algorithm used to authenticate the user
// during establishment of the session. Given the session is already
// established, this will not be used further.
AuthenticationAlgorithm ipmi.AuthenticationAlgorithm
// IntegrityAlgorithm is the algorithm used to sign, or authenticate packets
// sent by the managed system and remote console. This library authenticates
// all packets it sends inside a session, provided IntegrityAlgorithmNone
// was not negotiated.
IntegrityAlgorithm ipmi.IntegrityAlgorithm
// ConfidentialityAlgorithm is the algorithm used to encrypt and decrypt
// packets sent by the managed system and remote console. This library
// encrypts all packets it sends inside a session, provided
// ConfidentialityAlgorithmNone was not negotiated.
ConfidentialityAlgorithm ipmi.ConfidentialityAlgorithm
// AdditionalKeyMaterialGenerator is the instance of the authentication
// algorithm used during session establishment, loaded with the session
// integrity key. It has no further use as far as the BMC is concerned by
// the time we have this struct, however we keep it around to allow
// providing K_n for information purposes.
AdditionalKeyMaterialGenerator
integrityAlgorithm hash.Hash
// confidentialityLayer is used to send packets (we encrypt all outgoing
// packets), and to decode incoming packets. It is created during session
// establishment, and loaded with the right key material. The session
// layer's ConfidentialityLayerType field is set to this layer's type, so it
// returns this as the NextLayerType() of encrypted packets. When sending a
// message, this layer's SerializeTo is called before adding the session
// wrapper.
confidentialityLayer layerexts.SerializableDecodingLayer
// timeout is the time allowed per attempt of a command. The context passed
// in by the user controls end-to-end.
timeout time.Duration
}
// String returns a summary of the session's attributes on one line.
func (s *V2Session) String() string {
return fmt.Sprintf("V2Session(Authentication: %v, Integrity: %v, Confidentiality: %v, LocalID: %v, RemoteID: %v, SIK: %v, K_1: %v, K_2: %v)",
s.AuthenticationAlgorithm, s.IntegrityAlgorithm, s.ConfidentialityAlgorithm,
s.LocalID, s.RemoteID,
hex.EncodeToString(s.SIK),
hex.EncodeToString(s.K(1)), hex.EncodeToString(s.K(2)))
}
func (s *V2Session) Version() string {
return "2.0"
}
func (s *V2Session) ID() uint32 {
return s.LocalID
}
func (s *V2Session) SendCommand(ctx context.Context, c ipmi.Command) (ipmi.CompletionCode, error) {
// this is effectively identical to session-less send, but the
// implementations of what we call are wildly different - prime for an
// interface
timer := prometheus.NewTimer(commandDuration)
defer timer.ObserveDuration()
commandAttempts.WithLabelValues(c.Name()).Inc()
if err := s.buildAndSend(ctx, c); err != nil {
commandFailures.WithLabelValues(c.Name()).Inc()
return 0, err
}
code := s.messageLayer.CompletionCode
if c.Response() != nil {
if err := c.Response().DecodeFromBytes(s.messageLayer.LayerPayload(),
gopacket.NilDecodeFeedback); err != nil {
commandFailures.WithLabelValues(c.Name()).Inc()
return code, err
}
}
return code, nil
}
func (s *V2Session) buildAndSend(ctx context.Context, c ipmi.Command) error {
s.rmcpLayer = layers.RMCP{
Version: layers.RMCPVersion1,
Sequence: 0xFF, // do not send us an ACK
Class: layers.RMCPClassIPMI,
}
s.v2SessionLayer = ipmi.V2Session{
Encrypted: true,
Authenticated: true,
ID: s.RemoteID,
PayloadDescriptor: ipmi.PayloadDescriptorIPMI,
IntegrityAlgorithm: s.integrityAlgorithm,
ConfidentialityLayerType: s.confidentialityLayer.LayerType(),
}
s.messageLayer = ipmi.Message{
Operation: *c.Operation(),
RemoteAddress: ipmi.SlaveAddressBMC.Address(),
RemoteLUN: c.RemoteLUN(),
LocalAddress: ipmi.SoftwareIDRemoteConsole1.Address(),
Sequence: 1, // used at the session level
}
firstAttempt := true
terminalErr := error(nil)
retryable := func() error {
if firstAttempt {
firstAttempt = false
} else {
commandRetries.Inc()
}
// TODO handle AuthenticationAlgorithmNone properly
// TODO handle ConfidentialityAlgorithmNone properly
s.AuthenticatedSequenceNumbers.Inbound++
s.v2SessionLayer.Sequence = s.AuthenticatedSequenceNumbers.Inbound
if err := gopacket.SerializeLayers(s.buffer, serializeOptions,
&s.rmcpLayer,
// session selector only used when decoding
&s.v2SessionLayer,
s.confidentialityLayer,
&s.messageLayer,
serializableLayerOrEmpty(c.Request())); err != nil {
// this is not a retryable error
terminalErr = err
return nil
}
requestCtx, cancel := context.WithTimeout(ctx, s.timeout)
response, err := s.transport.Send(requestCtx, s.buffer.Bytes())
cancel()
if err != nil {
// session is now in an unknown state - if we send another command,
// some BMCs can tear their send buffer. The BMC may also ignore us
// completely if it does not support the command.
terminalErr = err
return nil
}
if _, err := s.decode(response, &s.layers); err != nil {
return err
}
types := layerexts.DecodedTypes(s.layers)
if err := types.InnermostEquals(ipmi.LayerTypeMessage); err != nil {
return err
}
code := s.messageLayer.CompletionCode
// must increment here, otherwise we'll miss temporary codes at the
// higher levels
commandResponses.WithLabelValues(code.String()).Inc()
if code.IsTemporary() {
return errRetryableCode
}
return nil
}
s.backoff.Reset()
if err := backoff.Retry(retryable, backoff.WithContext(s.backoff, ctx)); err != nil {
return err
}
return terminalErr
}
func (s *V2Session) GetSystemGUID(ctx context.Context) ([16]byte, error) {
return getSystemGUID(ctx, s)
}
func (s *V2Session) GetChannelAuthenticationCapabilities(
ctx context.Context,
r *ipmi.GetChannelAuthenticationCapabilitiesReq,
) (*ipmi.GetChannelAuthenticationCapabilitiesRsp, error) {
return getChannelAuthenticationCapabilities(ctx, s, r)
}
func (s *V2Session) GetSessionInfo(ctx context.Context, r *ipmi.GetSessionInfoReq) (*ipmi.GetSessionInfoRsp, error) {
cmd := &ipmi.GetSessionInfoCmd{
Req: *r,
}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) GetDeviceID(ctx context.Context) (*ipmi.GetDeviceIDRsp, error) {
cmd := &ipmi.GetDeviceIDCmd{}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) GetChassisStatus(ctx context.Context) (*ipmi.GetChassisStatusRsp, error) {
cmd := &ipmi.GetChassisStatusCmd{}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) ChassisControl(ctx context.Context, c ipmi.ChassisControl) error {
cmd := &ipmi.ChassisControlCmd{
Req: ipmi.ChassisControlReq{
ChassisControl: c,
},
}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return err
}
return nil
}
func (s *V2Session) GetSDRRepositoryInfo(ctx context.Context) (*ipmi.GetSDRRepositoryInfoRsp, error) {
cmd := &ipmi.GetSDRRepositoryInfoCmd{}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) ReserveSDRRepository(ctx context.Context) (*ipmi.ReserveSDRRepositoryRsp, error) {
cmd := &ipmi.ReserveSDRRepositoryCmd{}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) GetSensorReading(ctx context.Context, sensor uint8) (*ipmi.GetSensorReadingRsp, error) {
cmd := &ipmi.GetSensorReadingCmd{
Req: ipmi.GetSensorReadingReq{
Number: sensor,
},
}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return nil, err
}
return &cmd.Rsp, nil
}
func (s *V2Session) GetSessionPrivilegeLevel(ctx context.Context) (ipmi.PrivilegeLevel, error) {
cmd := &ipmi.SetSessionPrivilegeLevelCmd{
Req: ipmi.SetSessionPrivilegeLevelReq{
// PrivilegeLevel omitted to retrieve current level
},
}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return 0, err
}
return cmd.Rsp.PrivilegeLevel, nil
}
func (s *V2Session) SetSessionPrivilegeLevel(ctx context.Context, level ipmi.PrivilegeLevel) (ipmi.PrivilegeLevel, error) {
cmd := &ipmi.SetSessionPrivilegeLevelCmd{
Req: ipmi.SetSessionPrivilegeLevelReq{
PrivilegeLevel: level,
},
}
if err := ValidateResponse(s.SendCommand(ctx, cmd)); err != nil {
return 0, err
}
return cmd.Rsp.PrivilegeLevel, nil
}
func (s *V2Session) closeSession(ctx context.Context) error {
// we decrement regardless of whether this command succeeds, as to not do so
// would be overly pessimistic - if it fails, there's nothing we can do;
// failures are better tracked as Close Session command errors
defer sessionsOpen.Dec()
cmd := &ipmi.CloseSessionCmd{
Req: ipmi.CloseSessionReq{
ID: s.RemoteID,
},
}
return ValidateResponse(s.SendCommand(ctx, cmd))
}
func (s *V2Session) Close(ctx context.Context) error {
return s.closeSession(ctx)
}