forked from bougou/go-ipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_get_lan_config_params.go
303 lines (253 loc) · 8.7 KB
/
cmd_get_lan_config_params.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
package ipmi
import (
"fmt"
"net"
)
// 23.2 Get LAN Configuration Parameters Command
type GetLanConfigParamsRequest struct {
ChannelNumber uint8
ParamSelector LanParamSelector
SetSelector uint8
BlockSelector uint8
}
type GetLanConfigParamsResponse struct {
ParameterVersion uint8
ConfigData []byte
}
func (req *GetLanConfigParamsRequest) Pack() []byte {
out := make([]byte, 4)
packUint8(req.ChannelNumber, out, 0)
packUint8(uint8(req.ParamSelector), out, 1)
packUint8(req.SetSelector, out, 2)
packUint8(req.BlockSelector, out, 3)
return out
}
func (req *GetLanConfigParamsRequest) Command() Command {
return CommandGetLanConfigParams
}
func (res *GetLanConfigParamsResponse) CompletionCodes() map[uint8]string {
return map[uint8]string{
0x80: "parameter not supported.",
}
}
func (res *GetLanConfigParamsResponse) Unpack(msg []byte) error {
if len(msg) < 1 {
return ErrUnpackedDataTooShort
}
res.ParameterVersion, _, _ = unpackUint8(msg, 0)
res.ConfigData, _, _ = unpackBytes(msg, 1, len(msg)-1)
return nil
}
func (res *GetLanConfigParamsResponse) Format() string {
return ""
}
func (c *Client) GetLanConfigParams(channelNumber uint8, paramSelector LanParamSelector) (response *GetLanConfigParamsResponse, err error) {
request := &GetLanConfigParamsRequest{
ChannelNumber: channelNumber,
ParamSelector: paramSelector,
}
response = &GetLanConfigParamsResponse{}
err = c.Exchange(request, response)
return
}
func (c *Client) GetLanConfig(channelNumber uint8) (*LanConfig, error) {
lanConfig := &LanConfig{}
for _, lanParam := range LanParams {
paramSelector := LanParamSelector(lanParam.Selector)
res, err := c.GetLanConfigParams(channelNumber, paramSelector)
if err != nil {
resErr, ok := err.(*ResponseError)
if !ok {
return nil, fmt.Errorf("not ResponseError")
}
cc := resErr.CompletionCode()
ccString := StrCC(res, uint8(cc))
switch uint8(cc) {
case 0x80,
uint8(CompletionCodeParameterOutOfRange),
uint8(CompletionCodeRequestDataFieldInvalid):
c.Debugf("paramSelector (%#02x) %s, cc: (%#02x) %s\n", uint8(paramSelector), paramSelector, uint8(cc), ccString)
continue
default:
// other completion codes are treated as error.
// including 0x00 which means cc is successful, but other part failed
return nil, fmt.Errorf("get lan config param (%s) failed, err: %s", paramSelector, err)
}
}
if err := parseLanConfig(lanConfig, paramSelector, res.ConfigData); err != nil {
return nil, fmt.Errorf("get lan config param (%s) failed, err: %s", paramSelector, err)
}
}
return lanConfig, nil
}
func parseLanConfig(lanConfig *LanConfig, paramSelector LanParamSelector, paramData []byte) error {
var lanParam LanParam
for _, v := range LanParams {
if v.Selector == paramSelector {
lanParam = v
break
}
}
if uint8(len(paramData)) < lanParam.DataSize {
return fmt.Errorf("the data for param (%s) is too short, input (%d), required (%d)", paramSelector, len(paramData), lanParam.DataSize)
}
switch lanParam.Selector {
case LanParam_SetInProgress:
lanConfig.SetInProgress = SetInProgress(paramData[0])
case LanParam_AuthTypeSupported:
b := paramData[0]
lanConfig.AuthTypeSupport = AuthTypeSupport{
OEM: isBit5Set(b),
Password: isBit4Set(b),
MD5: isBit2Set(b),
MD2: isBit1Set(b),
}
case LanParam_AuthTypeEnables:
lanConfig.AuthTypeEnables = AuthTypeEnables{
Callback: AuthTypeEnabled{
OEM: isBit5Set(paramData[0]),
Password: isBit4Set(paramData[0]),
MD5: isBit2Set(paramData[0]),
MD2: isBit1Set(paramData[0]),
},
User: AuthTypeEnabled{
OEM: isBit5Set(paramData[1]),
Password: isBit4Set(paramData[1]),
MD5: isBit2Set(paramData[1]),
MD2: isBit1Set(paramData[1]),
},
Operator: AuthTypeEnabled{
OEM: isBit5Set(paramData[2]),
Password: isBit4Set(paramData[2]),
MD5: isBit2Set(paramData[2]),
MD2: isBit1Set(paramData[2]),
},
Admin: AuthTypeEnabled{
OEM: isBit5Set(paramData[3]),
Password: isBit4Set(paramData[3]),
MD5: isBit2Set(paramData[3]),
MD2: isBit1Set(paramData[3]),
},
OEM: AuthTypeEnabled{
OEM: isBit5Set(paramData[4]),
Password: isBit4Set(paramData[4]),
MD5: isBit2Set(paramData[4]),
MD2: isBit1Set(paramData[4]),
},
}
case LanParam_IP:
lanConfig.IP = net.IPv4(paramData[0], paramData[1], paramData[2], paramData[3])
case LanParam_IPSource:
lanConfig.IPSource = IPAddressSource(paramData[0])
case LanParam_MAC:
lanConfig.MAC = net.HardwareAddr(paramData[0:6])
case LanParam_SubnetMask:
lanConfig.SubnetMask = net.IPv4(paramData[0], paramData[1], paramData[2], paramData[3])
case LanParam_IPv4HeaderParams:
lanConfig.IPHeaderParams = IPHeaderParams{
TTL: paramData[0],
Flags: (paramData[1] & 0xc0) >> 5,
Precedence: (paramData[2] & 0xc0) >> 5,
TOS: (paramData[2] & 0x1f) >> 1,
}
case LanParam_PrimaryRMCPPort:
lanConfig.PrimaryRMCPPort, _, _ = unpackUint16L(paramData[0:2], 0)
case LanParam_SecondaryRMCPPort:
lanConfig.SecondaryRMCPPort, _, _ = unpackUint16L(paramData[0:2], 0)
case LanParam_ARPControl:
lanConfig.ARPControl = ARPControl{
ARPResponseEnabled: isBit1Set(paramData[0]),
GratuitousARPEnabled: isBit0Set(paramData[0]),
}
case LanParam_GratuituousARPInterval:
// Gratuitous ARP interval in 500 millisecond increments. 0-based.
lanConfig.GratuitousARPIntervalMilliSec = int32(paramData[0]) * 500
case LanParam_DefaultGatewayIP:
lanConfig.DefaultGatewayIP = net.IP(paramData[0:4])
case LanParam_DefaultGatewayMAC:
lanConfig.DefaultGatewayMAC = net.HardwareAddr(paramData[0:6])
case LanParam_BackupGatewayIP:
lanConfig.BackupGatewayIP = net.IP(paramData[0:4])
case LanParam_BackupGatewayMAC:
lanConfig.BackupGatewayMAC = net.HardwareAddr(paramData[0:6])
case LanParam_CommunityString:
lanConfig.CommunityString = NewCommunityString(string(paramData))
case LanParam_AlertDestinationsNumber:
lanConfig.AlertDestinationsNumber = paramData[0]
case LanParam_AlertDestinationType:
lanConfig.AlertDestinationType = AlertDestinationType{
SetSelector: paramData[0],
AlertSupportAcknowledge: isBit7Set(paramData[1]),
DestinationType: paramData[1] & 0x07,
AlertAcknowledgeTimeout: paramData[2],
Retries: paramData[3] & 0x7f,
}
case LanParam_AlertDestinationAddress:
lanConfig.AlertDestinationAddress = AlertDestinationAddress{
SetSelector: paramData[0],
AddressFormat: (paramData[1] & 0xf0) >> 4,
IP4UseBackupGateway: isBit0Set(paramData[2]),
IP4IP: net.IP(paramData[3:7]),
IP4MAC: net.HardwareAddr(paramData[7:12]),
IP6IP: net.IP(paramData[2:18]),
}
case LanParam_VLANID:
lanConfig.VLANEnabled = isBit7Set(paramData[1])
lanConfig.VLANID = (uint16(paramData[1]&0x0f) << 8) | uint16(paramData[0])
case LanParam_VLANPriority:
lanConfig.VLANPriority = paramData[0] & 0x07
case LanParam_CipherSuiteEntrySupport:
lanConfig.RMCPCipherSuitesCount = paramData[0] & 0x1f
case LanParam_CipherSuiteEntries:
ids := []CipherSuiteID{}
var count uint8 = 0
for i, v := range paramData {
if i == 0 {
// first byte is Reserved
continue
}
if count+1 > lanConfig.RMCPCipherSuitesCount {
break
}
ids = append(ids, CipherSuiteID(v))
count += 1
}
lanConfig.RMCPCipherSuiteEntries = ids
case LanParam_CihperSuitePrivilegeLevels:
levels := []PrivilegeLevel{}
for i, v := range paramData {
if i == 0 {
// first byte is reserved
continue
}
levels = append(levels, PrivilegeLevel(v&0x0f))
levels = append(levels, PrivilegeLevel(v&0xf0>>4))
}
lanConfig.RMCPCipherSuitesMaxPrivLevel = levels
case LanParam_AlertDestinationVLAN:
lanConfig.AlertDestinationVLAN = AlertDestinationVLAN{
SetSelector: paramData[0],
AddressFormat: (paramData[1] & 0xf0) >> 4,
VLANID: (uint16(paramData[3]&0x0f) << 8) | uint16(paramData[2]),
CFI: isBit4Set(paramData[3]),
Priority: (paramData[3] & 0xe0) >> 5,
}
case LanParam_BadPassordThreshold:
resetInterval, _, _ := unpackUint16L(paramData, 2)
lockInterval, _, _ := unpackUint16L(paramData, 4)
lanConfig.BadPasswordThreshold = BadPasswordThreshold{
GenerateSessionAuditEvent: isBit0Set(paramData[0]),
Threshold: paramData[1],
AttemptCountResetIntevalSec: uint32(resetInterval) * 10,
UserLockoutIntervalSec: uint32(lockInterval) * 10,
}
case LanParam_IP6Support:
lanConfig.IP6Support = IP6Support{
SupportIP6AlertDestination: isBit2Set(paramData[0]),
CanUseBothIP4AndIP6: isBit1Set(paramData[0]),
CanUseIP6Only: isBit0Set(paramData[0]),
}
// Todo IP6 params parse
}
return nil
}