-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethernetinterface.go
346 lines (309 loc) · 12.4 KB
/
ethernetinterface.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"context"
"encoding/json"
"reflect"
"github.com/jacobweinstock/gophish/common"
)
// DHCPv6OperatingMode is the IPv6 DHCP mode.
type DHCPv6OperatingMode string
const (
// StatefulDHCPv6OperatingMode shall operate in stateful mode on this
// interface. DHCPv6 stateful mode is used to configure addresses, and
// when it is enabled, stateless mode is also implicitly enabled.
StatefulDHCPv6OperatingMode DHCPv6OperatingMode = "Stateful"
// StatelessDHCPv6OperatingMode shall operate in stateless mode on this
// interface. DHCPv6 stateless mode allows configuring the interface
// using DHCP options but does not configure addresses. It is always
// enabled by default whenever DHCPv6 Stateful mode is also enabled.
StatelessDHCPv6OperatingMode DHCPv6OperatingMode = "Stateless"
// DisabledDHCPv6OperatingMode shall be disabled for this interface.
DisabledDHCPv6OperatingMode DHCPv6OperatingMode = "Disabled"
)
// EthernetDeviceType is the device type.
type EthernetDeviceType string
const (
// PhysicalEthernetDeviceType shall indicate a physical traditional
// network interface.
PhysicalEthernetDeviceType EthernetDeviceType = "Physical"
// VirtualEthernetDeviceType shall indicate a network device function has
// multiple VLANs and is representing one of them as a virtual Ethernet
// interface. The NetworkDeviceFunction property within Links shall
// contain the locator for the parent network device function.
VirtualEthernetDeviceType EthernetDeviceType = "Virtual"
)
// LinkStatus is the interface link status.
type LinkStatus string
const (
// LinkUpLinkStatus The link is available for communication on this
// interface.
LinkUpLinkStatus LinkStatus = "LinkUp"
// NoLinkLinkStatus There is no link or connection detected on this
// interface.
NoLinkLinkStatus LinkStatus = "NoLink"
// LinkDownLinkStatus There is no link on this interface, but the
// interface is connected.
LinkDownLinkStatus LinkStatus = "LinkDown"
)
// DHCPv4Configuration describes the configuration of DHCP v4.
type DHCPv4Configuration struct {
// DHCPEnabled shall indicate whether DHCP v4 is enabled for this
// EthernetInterface.
DHCPEnabled bool
// UseDNSServers shall indicate whether the interface will use
// DHCPv4-supplied DNS servers.
UseDNSServers bool
// UseDomainName shall indicate whether the interface will use a
// DHCPv4-supplied domain name.
UseDomainName bool
// UseGateway shall indicate whether the interface will use a
// DHCPv4-supplied gateway.
UseGateway bool
// UseNTPServers shall indicate whether the interface will use
// DHCPv4-supplied NTP servers.
UseNTPServers bool
// UseStaticRoutes shall indicate whether the interface will use a
// DHCPv4-supplied static routes.
UseStaticRoutes bool
}
// DHCPv6Configuration describes the configuration of DHCP v6.
type DHCPv6Configuration struct {
// OperatingMode is used to configure addresses, and when it is enabled,
// stateless mode is also implicitly enabled.
OperatingMode DHCPv6OperatingMode
// UseDNSServers shall indicate whether the interface will use
// DHCPv6-supplied DNS servers.
UseDNSServers bool
// UseDomainName shall indicate whether the interface will use a domain name
// supplied through DHCPv6 stateless mode.
UseDomainName bool
// UseNTPServers shall indicate whether the interface will use
// DHCPv6-supplied NTP servers.
UseNTPServers bool
// UseRapidCommit shall indicate whether the interface will use DHCPv6 rapid
// commit mode for stateful mode address assignments.
UseRapidCommit bool
}
// EthernetInterface is used to represent NIC resources.
type EthernetInterface struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// AutoNeg shall be true if auto negotiation of speed and duplex is enabled
// on this interface and false if it is disabled.
AutoNeg bool
// DHCPv4 shall contain the configuration of DHCP v4.
DHCPv4 DHCPv4Configuration
// DHCPv6 shall contain the configuration of DHCP v6.
DHCPv6 DHCPv6Configuration
// Description provides a description of this resource.
Description string
// EthernetInterfaceType shall contain the type of interface.
EthernetInterfaceType EthernetDeviceType
// FQDN shall be the fully qualified domain name for this interface.
FQDN string
// FullDuplex shall represent the duplex status of the Ethernet connection
// on this interface.
FullDuplex bool
// HostName shall be host name for this interface.
HostName string
// IPv4Addresses is used to represent the IPv4 connection characteristics
// for this interface. It is recommended that this property be regarded as
// read-only, with configuration of static addresses performed by
// updating the values within IPv4StaticAddresses. Services may reject
// updates to this array for this reason.
IPv4Addresses []IPv4Address
// IPv4StaticAddresses is used to represent all IPv4 static addresses
// assigned (but not necessarily in use) to this interface. Addresses in
// use by this interface shall also appear in the IPv4Addresses property.
IPv4StaticAddresses []IPv4Address
// IPv6AddressPolicyTable is used to represent the Address Selection
// Policy Table as defined in RFC 6724.
IPv6AddressPolicyTable []IPv6AddressPolicyEntry
// IPv6Addresses is used to represent the IPv6 connection characteristics
// for this interface.
IPv6Addresses []IPv6Address
// IPv6DefaultGateway shall be the current
// IPv6 default gateway address that is in use on this interface.
IPv6DefaultGateway string
// IPv6StaticAddresses is used to represent the IPv6 static connection
// characteristics for this interface.
IPv6StaticAddresses []IPv6StaticAddress
// IPv6StaticDefaultGateways is The values in this array shall represent
// the IPv6 static default gateway addresses for this interface.
IPv6StaticDefaultGateways []IPv6GatewayStaticAddress
// InterfaceEnabled shall be a boolean
// indicating whether this interface is enabled.
InterfaceEnabled bool
// LinkStatus shall be the link status of this interface (port).
LinkStatus LinkStatus
// MACAddress shall be the effective
// current MAC Address of this interface. If an assignable MAC address is
// not supported, this is a read only alias of the PermanentMACAddress.
MACAddress string
// MTUSize shall be the size in bytes of largest Protocol Data Unit (PDU)
// that can be passed in an Ethernet (MAC) frame on this interface.
MTUSize int
// MaxIPv6StaticAddresses shall indicate the number of array items supported
// by IPv6StaticAddresses.
MaxIPv6StaticAddresses int
// NameServers used on this interface.
NameServers []string
// Oem object used on this interface.
Oem interface{}
// PermanentMACAddress shall be the Permanent MAC Address of this interface
// (port). This value is typically programmed during the manufacturing time.
// This address is not assignable.
PermanentMACAddress string
// SpeedMbps shall be the link speed of the interface in Mbps.
SpeedMbps int
// StatelessAddressAutoConfig is This object shall contain the IPv4 and
// IPv6 Stateless Address Automatic Configuration (SLAAC) properties for
// this interface.
StatelessAddressAutoConfig StatelessAddressAutoConfiguration
// StaticNameServers is used when DHCP provisioning is not in enabled for
// name server configuration. As an implementation option they may also
// be used in addition to DHCP provided addresses, or in cases where the
// DHCP server provides no DNS assignments.
StaticNameServers []string
// Status shall contain any status or health properties
// of the resource.
Status common.Status
// UefiDevicePath shall be the UEFI device path to the device which
// implements this interface (port).
UefiDevicePath string
// VLAN shall be the VLAN for this interface. If this interface supports
// more than one VLAN, the VLAN property shall not be present and the VLANS
// collection link shall be present instead.
VLAN VLAN
// VLANs is a collection of VLANs and is only used if the interface supports
// more than one VLANs.
vlans string
// Chassis shall be a reference to a resource of type Chassis that represent
// the physical container associated with this Ethernet Interface.
chassis string
// Endpoints shall be a reference to the resources that this ethernet
// interface is associated with and shall reference a resource of type
// Endpoint.
endpoints []string
// EndpointsCount is the number of endpoints.
EndpointsCount int
// HostInterface is used by a host to communicate with a Manager.
hostInterface string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a EthernetInterface object from the raw JSON.
func (ethernetinterface *EthernetInterface) UnmarshalJSON(b []byte) error {
type temp EthernetInterface
type links struct {
Chassis common.Link
Endpoints common.Links
EndpointsCount int `json:"Endpoints@odata.count"`
HostInterface common.Link
}
var t struct {
temp
Links links
VLANs common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*ethernetinterface = EthernetInterface(t.temp)
// Extract the links to other entities for later
ethernetinterface.chassis = string(t.Links.Chassis)
ethernetinterface.endpoints = t.Links.Endpoints.ToStrings()
ethernetinterface.EndpointsCount = t.Links.EndpointsCount
ethernetinterface.hostInterface = string(t.Links.HostInterface)
ethernetinterface.vlans = string(t.VLANs)
// This is a read/write object, so we need to save the raw object data for later
ethernetinterface.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (ethernetinterface *EthernetInterface) Update(ctx context.Context) error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(EthernetInterface)
original.UnmarshalJSON(ethernetinterface.rawData)
readWriteFields := []string{
"AutoNeg",
"FQDN",
"FullDuplex",
"HostName",
"InterfaceEnabled",
"MACAddress",
"MTUSize",
"SpeedMbps",
"StaticNameServers",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(ethernetinterface).Elem()
return ethernetinterface.Entity.Update(ctx, originalElement, currentElement, readWriteFields)
}
// GetEthernetInterface will get a EthernetInterface instance from the service.
func GetEthernetInterface(ctx context.Context, c common.Client, uri string) (*EthernetInterface, error) {
resp, err := c.Get(ctx, uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var ethernetinterface EthernetInterface
err = json.NewDecoder(resp.Body).Decode(ðernetinterface)
if err != nil {
return nil, err
}
ethernetinterface.SetClient(c)
return ðernetinterface, nil
}
// ListReferencedEthernetInterfaces gets the collection of EthernetInterface from
// a provided reference.
func ListReferencedEthernetInterfaces(ctx context.Context, c common.Client, link string) ([]*EthernetInterface, error) {
var result []*EthernetInterface
if link == "" {
return result, nil
}
links, err := common.GetCollection(ctx, c, link)
if err != nil {
return result, err
}
for _, ethernetinterfaceLink := range links.ItemLinks {
ethernetinterface, err := GetEthernetInterface(ctx, c, ethernetinterfaceLink)
if err != nil {
return result, err
}
result = append(result, ethernetinterface)
}
return result, nil
}
// IPv6AddressPolicyEntry describes and entry in the Address Selection Policy
// Table as defined in RFC 6724.
type IPv6AddressPolicyEntry struct {
// Label shall contain the IPv6 Label value for this table entry as defined
// in RFC 6724 section 2.1.
Label int
// Precedence shall contain the IPv6 Precedence value for this table entry
// as defined in RFC 6724 section 2.1.
Precedence int
// Prefix shall contain the IPv6 Address Prefix for this table entry as
// defined in RFC 6724 section 2.1.
Prefix string
}
// StatelessAddressAutoConfiguration describes the IPv4 and IPv6 Stateless
// Address Automatic Configuration (SLAAC) for this interface.
type StatelessAddressAutoConfiguration struct {
// IPv4AutoConfigEnabled shall indicate whether IPv4 Stateless Address
// Auto-Configuration (SLAAC) is enabled for this interface.
IPv4AutoConfigEnabled bool
// IPv6AutoConfigEnabled shall indicate whether IPv6 Stateless Address
// Auto-Configuration (SLAAC) is enabled for this interface.
IPv6AutoConfigEnabled bool
}
// TODO: Add vlans