blob: 2f6d2d9e11d568e00e5188c9fb3ddb3f9b47b4a7 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Matteo Scandolof9d43412021-01-12 11:11:34 -08003 * Copyright 2020-present Open Networking Foundation
4
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07005 * 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
Matteo Scandolof9d43412021-01-12 11:11:34 -08008
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07009 * http://www.apache.org/licenses/LICENSE-2.0
Matteo Scandolof9d43412021-01-12 11:11:34 -080010
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070011 * 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.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070016 */
Matteo Scandolof9d43412021-01-12 11:11:34 -080017
18// Package omci provides a library of routines to create, manipulate, serialize, and
19// decode ITU-T G.988 OMCI messages/packets
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070020package omci
21
22import (
23 "encoding/binary"
24 "errors"
25 "fmt"
Girish Gowdra161d27a2021-05-05 12:01:44 -070026
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070027 "github.com/aead/cmac/aes"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070028 "github.com/google/gopacket"
29 "github.com/google/gopacket/layers"
Girish Gowdra161d27a2021-05-05 12:01:44 -070030 me "github.com/opencord/omci-lib-go/generated"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070031)
32
Matteo Scandolof9d43412021-01-12 11:11:34 -080033// DeviceIdent identifies the OMCI message format. Currently either baseline or extended.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070034type DeviceIdent byte
35
Matteo Scandolof9d43412021-01-12 11:11:34 -080036// LayerTypeOmci provide a gopacket LayerType for OMCI messages
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070037var (
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
Matteo Scandolof9d43412021-01-12 11:11:34 -080051 _ = 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
56 // ExtendedIdent messager are up to 1920 octets but may not be supported by all ONUs or OLTs.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070057 ExtendedIdent DeviceIdent = 0x0B
58)
59
Matteo Scandolof9d43412021-01-12 11:11:34 -080060var omciIK = []byte{0x18, 0x4b, 0x8a, 0xd4, 0xd1, 0xac, 0x4a, 0xf4,
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070061 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
Girish Gowdra161d27a2021-05-05 12:01:44 -070089// 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.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070091const MaxAttributeGetNextBaselineLength = MaxBaselineLength - 11 - 8
92
Girish Gowdra161d27a2021-05-05 12:01:44 -070093// 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
97// 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
Matteo Scandolof9d43412021-01-12 11:11:34 -0800105// MaxManagedEntityMibUploadNextExtendedLength is the maximum payload size for ME
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700106// 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
Girish Gowdra161d27a2021-05-05 12:01:44 -0700116// 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
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700120// 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
Girish Gowdra161d27a2021-05-05 12:01:44 -0700132 ResponseExpected bool // Significant for Download Section Request only
133 Payload []byte // TODO: Deprecated. Use layers.BaseLayer.Payload
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700134 Length uint16
135 MIC uint32
136}
137
138func (omci *OMCI) String() string {
139 //msgType := me.MsgType(byte(omci.MessageType) & me.MsgTypeMask)
140 //if me.IsAutonomousNotification(msgType) {
141 // return fmt.Sprintf("OMCI: Type: %v:", msgType)
142 //} else if byte(omci.MessageType)&me.AK == me.AK {
143 // return fmt.Sprintf("OMCI: Type: %v Response", msgType)
144 //}
145 return fmt.Sprintf("Type: %v, TID: %d (%#x), Ident: %v",
146 omci.MessageType, omci.TransactionID, omci.TransactionID, omci.DeviceIdentifier)
147}
148
149// LayerType returns LayerTypeOMCI
150func (omci *OMCI) LayerType() gopacket.LayerType {
151 return LayerTypeOMCI
152}
153
Matteo Scandolof9d43412021-01-12 11:11:34 -0800154// LayerContents returns the OMCI specific layer information
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700155func (omci *OMCI) LayerContents() []byte {
Matteo Scandolocedde462021-03-09 17:37:16 -0800156 b := make([]byte, 4)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700157 binary.BigEndian.PutUint16(b, omci.TransactionID)
158 b[2] = byte(omci.MessageType)
159 b[3] = byte(omci.DeviceIdentifier)
160 return b
161}
162
Matteo Scandolof9d43412021-01-12 11:11:34 -0800163// CanDecode returns the layers that this class can decode
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700164func (omci *OMCI) CanDecode() gopacket.LayerClass {
165 return LayerTypeOMCI
166}
167
168// NextLayerType returns the layer type contained by this DecodingLayer.
169func (omci *OMCI) NextLayerType() gopacket.LayerType {
170 return gopacket.LayerTypeZero
171}
172
173func decodeOMCI(data []byte, p gopacket.PacketBuilder) error {
174 // Allow baseline messages without Length & MIC, but no less
Girish Gowdra161d27a2021-05-05 12:01:44 -0700175 if len(data) < 10 {
176 p.SetTruncated()
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700177 return errors.New("frame header too small")
178 }
Girish Gowdra161d27a2021-05-05 12:01:44 -0700179 omci := &OMCI{}
180
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700181 switch DeviceIdent(data[3]) {
182 default:
Girish Gowdra161d27a2021-05-05 12:01:44 -0700183 return errors.New("unsupported message set/device identifier")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700184
185 case BaselineIdent:
Girish Gowdra161d27a2021-05-05 12:01:44 -0700186 if len(data) < MaxBaselineLength-8 {
187 p.SetTruncated()
188 return errors.New("frame too small")
189 }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700190 return omci.DecodeFromBytes(data, p)
191
192 case ExtendedIdent:
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700193 return omci.DecodeFromBytes(data, p)
194 }
195}
196
197func calculateMicAes128(data []byte) (uint32, error) {
198 // See if upstream or downstream
199 var downstreamCDir = [...]byte{0x01}
200 var upstreamCDir = [...]byte{0x02}
201
202 tid := binary.BigEndian.Uint16(data[0:2])
203 var sum []byte
204 var err error
205
206 if (data[2]&me.AK) == me.AK || tid == 0 {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800207 sum, err = aes.Sum(append(upstreamCDir[:], data[:44]...), omciIK, 4)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700208 } else {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800209 sum, err = aes.Sum(append(downstreamCDir[:], data[:44]...), omciIK, 4)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700210 }
211 if err != nil {
212 return 0, err
213 }
214 return binary.BigEndian.Uint32(sum), nil
215}
216
217/////////////////////////////////////////////////////////////////////////////
218// Baseline Message encode / decode
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700219
Matteo Scandolof9d43412021-01-12 11:11:34 -0800220// DecodeFromBytes will decode the OMCI layer of a packet/message
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700221func (omci *OMCI) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
222 if len(data) < 10 {
223 p.SetTruncated()
224 return errors.New("frame too small")
225 }
226 omci.TransactionID = binary.BigEndian.Uint16(data[0:])
227 omci.MessageType = MessageType(data[2])
228 omci.DeviceIdentifier = DeviceIdent(data[3])
Girish Gowdrae2683102021-03-05 08:24:26 -0800229 omci.ResponseExpected = byte(omci.MessageType)&me.AR == me.AR
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700230
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
Girish Gowdra161d27a2021-05-05 12:01:44 -0700238 var eomOffset int
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700239 if omci.DeviceIdentifier == BaselineIdent {
240 omci.Length = MaxBaselineLength - 8
241 payloadOffset = 8
242 micOffset = MaxBaselineLength - 4
Girish Gowdra161d27a2021-05-05 12:01:44 -0700243 eomOffset = MaxBaselineLength - 8
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700244
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
Girish Gowdra161d27a2021-05-05 12:01:44 -0700255 eomOffset = micOffset
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700256
Girish Gowdra161d27a2021-05-05 12:01:44 -0700257 if omci.Length > uint16(MaxExtendedLength-payloadOffset) {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700258 return me.NewProcessingError("extended frame exceeds maximum allowed")
259 }
Girish Gowdra161d27a2021-05-05 12:01:44 -0700260 if len(data) < micOffset {
261 p.SetTruncated()
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700262 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 }
Girish Gowdra161d27a2021-05-05 12:01:44 -0700275 omci.BaseLayer = layers.BaseLayer{data[:4], data[4:eomOffset]}
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700276 p.AddLayer(omci)
Girish Gowdra161d27a2021-05-05 12:01:44 -0700277 nextLayer, err := MsgTypeToNextLayer(omci.MessageType, omci.DeviceIdentifier == ExtendedIdent)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700278 if err != nil {
279 return err
280 }
281 return p.NextDecoder(nextLayer)
282}
283
284// SerializeTo writes the serialized form of this layer into the
285// SerializationBuffer, implementing gopacket.SerializableLayer.
286// See the docs for gopacket.SerializableLayer for more info.
287func (omci *OMCI) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700288 bytes, err := b.PrependBytes(4)
289 if err != nil {
290 return err
291 }
292 // OMCI layer error checks
293 isNotification := (int(omci.MessageType) & ^me.MsgTypeMask) == 0
294 if omci.TransactionID == 0 && !isNotification {
295 return errors.New("omci Transaction ID is zero for non-Notification type message")
296 }
297 if omci.DeviceIdentifier == 0 {
298 omci.DeviceIdentifier = BaselineIdent // Allow uninitialized device identifier
299 }
300 if omci.DeviceIdentifier == BaselineIdent {
301 if omci.Length == 0 {
302 omci.Length = MaxBaselineLength - 8 // Allow uninitialized length
303 } else if omci.Length != MaxBaselineLength-8 {
304 msg := fmt.Sprintf("invalid Baseline message length: %v", omci.Length)
305 return errors.New(msg)
306 }
307 } else if omci.DeviceIdentifier == ExtendedIdent {
Girish Gowdra161d27a2021-05-05 12:01:44 -0700308 omci.Length = uint16(len(b.Bytes()) - 10)
309
310 // Is length larger than maximum packet (less header and trailing MIC)
311 if omci.Length > MaxExtendedLength-10-4 {
312 msg := fmt.Sprintf("invalid Extended message length: %v", omci.Length)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700313 return errors.New(msg)
314 }
315 } else {
316 msg := fmt.Sprintf("invalid device identifier: %#x, Baseline or Extended expected",
317 omci.DeviceIdentifier)
318 return errors.New(msg)
319 }
320 binary.BigEndian.PutUint16(bytes, omci.TransactionID)
Girish Gowdrae2683102021-03-05 08:24:26 -0800321 // Download section request can optionally have the AR bit set or cleared. If user passes in this
322 // message type and sets download requested, fix up the message type for them.
323 if omci.MessageType == DownloadSectionRequestType && omci.ResponseExpected {
324 bytes[2] = byte(DownloadSectionRequestWithResponseType)
325 } else {
326 bytes[2] = byte(omci.MessageType)
327 }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700328 bytes[3] = byte(omci.DeviceIdentifier)
329 b.PushLayer(LayerTypeOMCI)
330
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700331 if omci.DeviceIdentifier == BaselineIdent {
Girish Gowdra161d27a2021-05-05 12:01:44 -0700332 bufLen := len(b.Bytes())
333 padSize := int(omci.Length) - bufLen + 4
334 if padSize < 0 {
335 msg := fmt.Sprintf("invalid OMCI Message Type length, exceeded allowed frame size by %d bytes",
336 -padSize)
337 return errors.New(msg)
338 }
339 padding, err := b.AppendBytes(padSize)
340 if err != nil {
341 return err
342 }
343 copy(padding, lotsOfZeros[:])
344
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700345 // For baseline, always provide the length
346 binary.BigEndian.PutUint32(b.Bytes()[MaxBaselineLength-8:], 40)
347 }
348 if opts.ComputeChecksums {
349 micBytes, err := b.AppendBytes(4)
350 if err != nil {
351 return err
352 }
353 omci.MIC, _ = calculateMicAes128(bytes[:MaxBaselineLength-4])
354 binary.BigEndian.PutUint32(micBytes, omci.MIC)
355 }
356 return nil
357}
358
359// hacky way to zero out memory... there must be a better way?
360var lotsOfZeros [MaxExtendedLength]byte // Extended OMCI messages may be up to 1980 bytes long, including headers