blob: f7783828183bf1a28e84daa0509918c9b2678acd [file] [log] [blame]
Chip Boling6e27b352020-02-14 09:10:01 -06001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Andrea Campanella7167ebb2020-02-24 09:56:38 +01003 * Copyright 2020-present Open Networking Foundation
4
Chip Boling6e27b352020-02-14 09:10:01 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Andrea Campanella7167ebb2020-02-24 09:56:38 +01008
Chip Boling6e27b352020-02-14 09:10:01 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Andrea Campanella7167ebb2020-02-24 09:56:38 +010010
Chip Boling6e27b352020-02-14 09:10:01 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Chip Boling6e27b352020-02-14 09:10:01 -060016 */
17
18// Package omci provides a library of routines to create, manipulate, serialize, and
19// decode ITU-T G.988 OMCI messages/packets
20package omci
21
22import (
23 "encoding/binary"
24 "errors"
25 "fmt"
David K. Bainbridgeadf422d2021-04-09 16:06:41 +000026
Chip Boling6e27b352020-02-14 09:10:01 -060027 "github.com/aead/cmac/aes"
Chip Boling6e27b352020-02-14 09:10:01 -060028 "github.com/google/gopacket"
29 "github.com/google/gopacket/layers"
Andrea Campanellae0cd8232021-10-15 15:10:23 +020030 me "github.com/opencord/omci-lib-go/v2/generated"
Chip Boling6e27b352020-02-14 09:10:01 -060031)
32
33// DeviceIdent identifies the OMCI message format. Currently either baseline or extended.
34type DeviceIdent byte
35
Chip Boling610117d2021-09-09 11:24:34 -050036// LayerTypeOMCI provides a gopacket LayerType for OMCI messages
Chip Boling6e27b352020-02-14 09:10:01 -060037var (
38 LayerTypeOMCI gopacket.LayerType
39)
40
41func init() {
42 LayerTypeOMCI = gopacket.RegisterLayerType(1000,
43 gopacket.LayerTypeMetadata{
44 Name: "OMCI",
45 Decoder: gopacket.DecodeFunc(decodeOMCI),
46 })
47}
48
49const (
50 // Device Identifiers
51 _ = iota
52 // BaselineIdent message are composed of a fixed 40 octet packet + 8-octet trailer. All
53 // G-PON OLTs and ONUs support the baseline message set
54 BaselineIdent DeviceIdent = 0x0A
55
Chip Boling610117d2021-09-09 11:24:34 -050056 // ExtendedIdent messages are up to 1920 octets but may not be supported by all ONUs or OLTs.
Chip Boling6e27b352020-02-14 09:10:01 -060057 ExtendedIdent DeviceIdent = 0x0B
58)
59
60var omciIK = []byte{0x18, 0x4b, 0x8a, 0xd4, 0xd1, 0xac, 0x4a, 0xf4,
61 0xdd, 0x4b, 0x33, 0x9e, 0xcc, 0x0d, 0x33, 0x70}
62
63func (di DeviceIdent) String() string {
64 switch di {
65 default:
66 return "Unknown"
67
68 case BaselineIdent:
69 return "Baseline"
70
71 case ExtendedIdent:
72 return "Extended"
73 }
74}
75
76// MaxBaselineLength is the maximum number of octets allowed in an OMCI Baseline
77// message. Depending on the adapter, it may or may not include the
78const MaxBaselineLength = 48
79
80// MaxExtendedLength is the maximum number of octets allowed in an OMCI Extended
81// message (including header).
82const MaxExtendedLength = 1980
83
84// MaxAttributeMibUploadNextBaselineLength is the maximum payload size for attributes for
85// a Baseline MIB Upload Next message.29
86const MaxAttributeMibUploadNextBaselineLength = MaxBaselineLength - 14 - 8
87
88// MaxAttributeGetNextBaselineLength is the maximum payload size for attributes for
Chip Bolingd8637b02021-04-29 08:36:38 -050089// a Baseline MIB Get Next message for the baseline message set. This is just the
90// attribute portion of the message contents and does not include the Result Code & Attribute Mask.
Chip Boling6e27b352020-02-14 09:10:01 -060091const MaxAttributeGetNextBaselineLength = MaxBaselineLength - 11 - 8
92
Chip Boling00a30d22021-05-04 13:31:52 -050093// MaxDownloadSectionLength is the maximum payload size for section data of
94// a Download Section request message for the baseline message set.
95const MaxDownloadSectionLength = 31
96
Chip Bolingd8637b02021-04-29 08:36:38 -050097// MaxTestRequestLength is the maximum payload size for test request message
98// for the baseline message set.
99const MaxTestRequestLength = MaxBaselineLength - 8 - 8
100
101// MaxTestResultsLength is the maximum payload size for test results message
102// for the baseline message set.
103const MaxTestResultsLength = MaxBaselineLength - 8 - 8
104
Chip Boling6e27b352020-02-14 09:10:01 -0600105// MaxManagedEntityMibUploadNextExtendedLength is the maximum payload size for ME
106// entries for an Extended MIB Upload Next message. Extended messages differ from
107// the baseline as multiple MEs can be reported in a single frame, just not multiple
108// attributes.
109const MaxManagedEntityMibUploadNextExtendedLength = MaxExtendedLength - 10 - 4
110
111// MaxAttributeGetNextExtendedLength is the maximum payload size for attributes for
112// a Extended MIB Get Next message. This is just the attribute portion of the
113// message contents and does not include the Result Code & Attribute Mask.
114const MaxAttributeGetNextExtendedLength = MaxExtendedLength - 13 - 4
115
Chip Boling00a30d22021-05-04 13:31:52 -0500116// MaxDownloadSectionExtendedLength is the maximum payload size for section data of
117// a Download Section request message for the extended message set.
118const MaxDownloadSectionExtendedLength = MaxExtendedLength - 11 - 4
119
Chip Boling6e27b352020-02-14 09:10:01 -0600120// NullEntityID is often used as the Null/void Managed Entity ID for attributes
121// that are used to refer to other Managed Entities but are currently not provisioned.
122const NullEntityID = uint16(0xffff)
123
124// OMCI defines the common protocol. Extended will be added once
125// I can get basic working (and layered properly). See ITU-T G.988 11/2017 section
126// A.3 for more information
127type OMCI struct {
128 layers.BaseLayer
129 TransactionID uint16
130 MessageType MessageType
131 DeviceIdentifier DeviceIdent
David K. Bainbridgeadf422d2021-04-09 16:06:41 +0000132 ResponseExpected bool // Significant for Download Section Request only
133 Payload []byte // TODO: Deprecated. Use layers.BaseLayer.Payload
Chip Boling6e27b352020-02-14 09:10:01 -0600134 Length uint16
135 MIC uint32
136}
137
138func (omci *OMCI) String() string {
Chip Boling6e27b352020-02-14 09:10:01 -0600139 return fmt.Sprintf("Type: %v, TID: %d (%#x), Ident: %v",
140 omci.MessageType, omci.TransactionID, omci.TransactionID, omci.DeviceIdentifier)
141}
142
143// LayerType returns LayerTypeOMCI
144func (omci *OMCI) LayerType() gopacket.LayerType {
145 return LayerTypeOMCI
146}
147
Chip Boling610117d2021-09-09 11:24:34 -0500148// CanDecode returns the layers that this class can decode
149func (omci *OMCI) CanDecode() gopacket.LayerClass {
150 return LayerTypeOMCI
151}
152
153// NextLayerType returns the layer type contained by this DecodingLayer.
154func (omci *OMCI) NextLayerType() gopacket.LayerType {
155 if next, ok := nextLayerMapping[omci.MessageType]; ok {
156 return next
157 }
158 return gopacket.LayerTypePayload
159}
160
Chip Boling6e27b352020-02-14 09:10:01 -0600161// LayerContents returns the OMCI specific layer information
162func (omci *OMCI) LayerContents() []byte {
Chip Bolingd8543b22021-03-08 08:34:26 -0600163 b := make([]byte, 4)
Chip Boling6e27b352020-02-14 09:10:01 -0600164 binary.BigEndian.PutUint16(b, omci.TransactionID)
165 b[2] = byte(omci.MessageType)
166 b[3] = byte(omci.DeviceIdentifier)
167 return b
168}
169
Chip Boling6e27b352020-02-14 09:10:01 -0600170func decodeOMCI(data []byte, p gopacket.PacketBuilder) error {
171 // Allow baseline messages without Length & MIC, but no less
Chip Boling610117d2021-09-09 11:24:34 -0500172 if len(data) < 4 {
Chip Bolingd8637b02021-04-29 08:36:38 -0500173 p.SetTruncated()
Chip Boling6e27b352020-02-14 09:10:01 -0600174 return errors.New("frame header too small")
175 }
Chip Bolingd8637b02021-04-29 08:36:38 -0500176 omci := &OMCI{}
177
Chip Boling6e27b352020-02-14 09:10:01 -0600178 switch DeviceIdent(data[3]) {
179 default:
Chip Bolingd8637b02021-04-29 08:36:38 -0500180 return errors.New("unsupported message set/device identifier")
Chip Boling6e27b352020-02-14 09:10:01 -0600181
182 case BaselineIdent:
Chip Boling157c9b92021-04-21 09:58:36 -0500183 if len(data) < MaxBaselineLength-8 {
Chip Bolingd8637b02021-04-29 08:36:38 -0500184 p.SetTruncated()
Chip Boling610117d2021-09-09 11:24:34 -0500185 return fmt.Errorf("frame too small. OMCI baseline frame length %v, %v required",
186 len(data), MaxBaselineLength-8)
Chip Boling157c9b92021-04-21 09:58:36 -0500187 }
Chip Boling6e27b352020-02-14 09:10:01 -0600188 return omci.DecodeFromBytes(data, p)
189
190 case ExtendedIdent:
Chip Boling610117d2021-09-09 11:24:34 -0500191 if len(data) < 10 {
192 p.SetTruncated()
193 return fmt.Errorf("frame too small. OMCI minimal extended frame length %v, 10 required",
194 len(data))
195 }
Chip Boling6e27b352020-02-14 09:10:01 -0600196 return omci.DecodeFromBytes(data, p)
197 }
198}
199
200func calculateMicAes128(data []byte) (uint32, error) {
201 // See if upstream or downstream
202 var downstreamCDir = [...]byte{0x01}
203 var upstreamCDir = [...]byte{0x02}
204
205 tid := binary.BigEndian.Uint16(data[0:2])
206 var sum []byte
207 var err error
208
209 if (data[2]&me.AK) == me.AK || tid == 0 {
210 sum, err = aes.Sum(append(upstreamCDir[:], data[:44]...), omciIK, 4)
211 } else {
212 sum, err = aes.Sum(append(downstreamCDir[:], data[:44]...), omciIK, 4)
213 }
214 if err != nil {
215 return 0, err
216 }
217 return binary.BigEndian.Uint32(sum), nil
218}
219
220/////////////////////////////////////////////////////////////////////////////
221// Baseline Message encode / decode
222
223// DecodeFromBytes will decode the OMCI layer of a packet/message
224func (omci *OMCI) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
Chip Boling610117d2021-09-09 11:24:34 -0500225 // Minimal Baseline and Extended message set length has already been checked
Chip Boling6e27b352020-02-14 09:10:01 -0600226 omci.TransactionID = binary.BigEndian.Uint16(data[0:])
227 omci.MessageType = MessageType(data[2])
228 omci.DeviceIdentifier = DeviceIdent(data[3])
Chip Boling8c8018e2021-02-22 15:56:00 -0600229 omci.ResponseExpected = byte(omci.MessageType)&me.AR == me.AR
Chip Boling6e27b352020-02-14 09:10:01 -0600230
231 isNotification := (int(omci.MessageType) & ^me.MsgTypeMask) == 0
232 if omci.TransactionID == 0 && !isNotification {
233 return errors.New("omci Transaction ID is zero for non-Notification type message")
234 }
235 // Decode length
236 var payloadOffset int
237 var micOffset int
Chip Boling157c9b92021-04-21 09:58:36 -0500238 var eomOffset int
Chip Boling6e27b352020-02-14 09:10:01 -0600239 if omci.DeviceIdentifier == BaselineIdent {
240 omci.Length = MaxBaselineLength - 8
241 payloadOffset = 8
242 micOffset = MaxBaselineLength - 4
Chip Boling157c9b92021-04-21 09:58:36 -0500243 eomOffset = MaxBaselineLength - 8
Chip Boling6e27b352020-02-14 09:10:01 -0600244
245 if len(data) >= micOffset {
246 length := binary.BigEndian.Uint32(data[micOffset-4:])
247 if uint16(length) != omci.Length {
248 return me.NewProcessingError("invalid baseline message length")
249 }
250 }
251 } else {
252 payloadOffset = 10
253 omci.Length = binary.BigEndian.Uint16(data[8:10])
254 micOffset = int(omci.Length) + payloadOffset
Chip Boling157c9b92021-04-21 09:58:36 -0500255 eomOffset = micOffset
Chip Boling6e27b352020-02-14 09:10:01 -0600256
Chip Boling157c9b92021-04-21 09:58:36 -0500257 if omci.Length > uint16(MaxExtendedLength-payloadOffset) {
Chip Boling6e27b352020-02-14 09:10:01 -0600258 return me.NewProcessingError("extended frame exceeds maximum allowed")
259 }
Chip Boling157c9b92021-04-21 09:58:36 -0500260 if len(data) < micOffset {
261 p.SetTruncated()
Chip Boling6e27b352020-02-14 09:10:01 -0600262 return me.NewProcessingError("extended frame too small")
263 }
264 }
265 // Extract MIC if present in the data
266 if len(data) >= micOffset+4 {
267 omci.MIC = binary.BigEndian.Uint32(data[micOffset:])
268 actual, _ := calculateMicAes128(data[:micOffset])
269 if omci.MIC != actual {
270 _ = fmt.Sprintf("invalid MIC, expected %#x, got %#x",
271 omci.MIC, actual)
272 //return errors.New(msg)
273 }
274 }
Chip Boling610117d2021-09-09 11:24:34 -0500275 omci.BaseLayer = layers.BaseLayer{
276 Contents: data[:4],
277 Payload: data[4:eomOffset],
278 }
Chip Boling6e27b352020-02-14 09:10:01 -0600279 p.AddLayer(omci)
Chip Boling157c9b92021-04-21 09:58:36 -0500280 nextLayer, err := MsgTypeToNextLayer(omci.MessageType, omci.DeviceIdentifier == ExtendedIdent)
Chip Boling6e27b352020-02-14 09:10:01 -0600281 if err != nil {
282 return err
283 }
284 return p.NextDecoder(nextLayer)
285}
286
287// SerializeTo writes the serialized form of this layer into the
288// SerializationBuffer, implementing gopacket.SerializableLayer.
289// See the docs for gopacket.SerializableLayer for more info.
290func (omci *OMCI) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
Chip Boling6e27b352020-02-14 09:10:01 -0600291 bytes, err := b.PrependBytes(4)
292 if err != nil {
293 return err
294 }
295 // OMCI layer error checks
Chip Boling610117d2021-09-09 11:24:34 -0500296 // Self-initiated Test Results have a TID of 0, OLT-requested do not.
297 isNotification := omci.MessageType == AlarmNotificationType ||
298 omci.MessageType == AttributeValueChangeType
299
300 if omci.TransactionID == 0 && !isNotification && omci.MessageType != TestResultType {
Chip Boling6e27b352020-02-14 09:10:01 -0600301 return errors.New("omci Transaction ID is zero for non-Notification type message")
302 }
Chip Boling610117d2021-09-09 11:24:34 -0500303 if omci.TransactionID != 0 && isNotification {
304 return errors.New("omci Transaction ID is not zero for Notification type message")
305 }
Chip Boling6e27b352020-02-14 09:10:01 -0600306 if omci.DeviceIdentifier == 0 {
307 omci.DeviceIdentifier = BaselineIdent // Allow uninitialized device identifier
308 }
309 if omci.DeviceIdentifier == BaselineIdent {
310 if omci.Length == 0 {
311 omci.Length = MaxBaselineLength - 8 // Allow uninitialized length
312 } else if omci.Length != MaxBaselineLength-8 {
313 msg := fmt.Sprintf("invalid Baseline message length: %v", omci.Length)
314 return errors.New(msg)
315 }
316 } else if omci.DeviceIdentifier == ExtendedIdent {
Chip Boling157c9b92021-04-21 09:58:36 -0500317 omci.Length = uint16(len(b.Bytes()) - 10)
318
319 // Is length larger than maximum packet (less header and trailing MIC)
320 if omci.Length > MaxExtendedLength-10-4 {
321 msg := fmt.Sprintf("invalid Extended message length: %v", omci.Length)
Chip Boling6e27b352020-02-14 09:10:01 -0600322 return errors.New(msg)
323 }
324 } else {
325 msg := fmt.Sprintf("invalid device identifier: %#x, Baseline or Extended expected",
326 omci.DeviceIdentifier)
327 return errors.New(msg)
328 }
329 binary.BigEndian.PutUint16(bytes, omci.TransactionID)
Chip Boling8c8018e2021-02-22 15:56:00 -0600330 // Download section request can optionally have the AR bit set or cleared. If user passes in this
331 // message type and sets download requested, fix up the message type for them.
332 if omci.MessageType == DownloadSectionRequestType && omci.ResponseExpected {
333 bytes[2] = byte(DownloadSectionRequestWithResponseType)
334 } else {
335 bytes[2] = byte(omci.MessageType)
336 }
Chip Boling6e27b352020-02-14 09:10:01 -0600337 bytes[3] = byte(omci.DeviceIdentifier)
338 b.PushLayer(LayerTypeOMCI)
339
Chip Boling6e27b352020-02-14 09:10:01 -0600340 if omci.DeviceIdentifier == BaselineIdent {
Chip Boling157c9b92021-04-21 09:58:36 -0500341 bufLen := len(b.Bytes())
342 padSize := int(omci.Length) - bufLen + 4
343 if padSize < 0 {
344 msg := fmt.Sprintf("invalid OMCI Message Type length, exceeded allowed frame size by %d bytes",
345 -padSize)
346 return errors.New(msg)
347 }
348 padding, err := b.AppendBytes(padSize)
349 if err != nil {
350 return err
351 }
352 copy(padding, lotsOfZeros[:])
353
Chip Boling6e27b352020-02-14 09:10:01 -0600354 // For baseline, always provide the length
355 binary.BigEndian.PutUint32(b.Bytes()[MaxBaselineLength-8:], 40)
356 }
357 if opts.ComputeChecksums {
358 micBytes, err := b.AppendBytes(4)
359 if err != nil {
360 return err
361 }
362 omci.MIC, _ = calculateMicAes128(bytes[:MaxBaselineLength-4])
363 binary.BigEndian.PutUint32(micBytes, omci.MIC)
364 }
365 return nil
366}
367
368// hacky way to zero out memory... there must be a better way?
369var lotsOfZeros [MaxExtendedLength]byte // Extended OMCI messages may be up to 1980 bytes long, including headers