Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
| 5 | * 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 |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * 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. |
| 16 | */ |
| 17 | |
| 18 | // Package omci provides a library of routines to create, manipulate, serialize, and |
| 19 | // decode ITU-T G.988 OMCI messages/packets |
| 20 | package omci |
| 21 | |
| 22 | import ( |
| 23 | "encoding/binary" |
| 24 | "errors" |
| 25 | "fmt" |
David K. Bainbridge | d80007b | 2021-04-12 12:22:29 +0000 | [diff] [blame] | 26 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 27 | "github.com/aead/cmac/aes" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 28 | "github.com/google/gopacket" |
| 29 | "github.com/google/gopacket/layers" |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 30 | me "github.com/opencord/omci-lib-go/v2/generated" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | // DeviceIdent identifies the OMCI message format. Currently either baseline or extended. |
| 34 | type DeviceIdent byte |
| 35 | |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 36 | // LayerTypeOMCI provides a gopacket LayerType for OMCI messages |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 37 | var ( |
| 38 | LayerTypeOMCI gopacket.LayerType |
| 39 | ) |
| 40 | |
| 41 | func init() { |
| 42 | LayerTypeOMCI = gopacket.RegisterLayerType(1000, |
| 43 | gopacket.LayerTypeMetadata{ |
| 44 | Name: "OMCI", |
| 45 | Decoder: gopacket.DecodeFunc(decodeOMCI), |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | const ( |
| 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 | |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 56 | // ExtendedIdent messages are up to 1920 octets but may not be supported by all ONUs or OLTs. |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 57 | ExtendedIdent DeviceIdent = 0x0B |
| 58 | ) |
| 59 | |
| 60 | var omciIK = []byte{0x18, 0x4b, 0x8a, 0xd4, 0xd1, 0xac, 0x4a, 0xf4, |
| 61 | 0xdd, 0x4b, 0x33, 0x9e, 0xcc, 0x0d, 0x33, 0x70} |
| 62 | |
| 63 | func (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 |
| 78 | const MaxBaselineLength = 48 |
| 79 | |
| 80 | // MaxExtendedLength is the maximum number of octets allowed in an OMCI Extended |
| 81 | // message (including header). |
| 82 | const MaxExtendedLength = 1980 |
| 83 | |
| 84 | // MaxAttributeMibUploadNextBaselineLength is the maximum payload size for attributes for |
| 85 | // a Baseline MIB Upload Next message.29 |
| 86 | const MaxAttributeMibUploadNextBaselineLength = MaxBaselineLength - 14 - 8 |
| 87 | |
| 88 | // MaxAttributeGetNextBaselineLength is the maximum payload size for attributes for |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 89 | // 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. |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 91 | const MaxAttributeGetNextBaselineLength = MaxBaselineLength - 11 - 8 |
| 92 | |
Himani Chawla | 43f95ff | 2021-06-03 00:24:12 +0530 | [diff] [blame] | 93 | // MaxDownloadSectionLength is the maximum payload size for section data of |
| 94 | // a Download Section request message for the baseline message set. |
| 95 | const MaxDownloadSectionLength = 31 |
| 96 | |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 97 | // MaxTestRequestLength is the maximum payload size for test request message |
| 98 | // for the baseline message set. |
| 99 | const MaxTestRequestLength = MaxBaselineLength - 8 - 8 |
| 100 | |
| 101 | // MaxTestResultsLength is the maximum payload size for test results message |
| 102 | // for the baseline message set. |
| 103 | const MaxTestResultsLength = MaxBaselineLength - 8 - 8 |
| 104 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 105 | // 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. |
| 109 | const 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. |
| 114 | const MaxAttributeGetNextExtendedLength = MaxExtendedLength - 13 - 4 |
| 115 | |
Himani Chawla | 43f95ff | 2021-06-03 00:24:12 +0530 | [diff] [blame] | 116 | // MaxDownloadSectionExtendedLength is the maximum payload size for section data of |
| 117 | // a Download Section request message for the extended message set. |
| 118 | const MaxDownloadSectionExtendedLength = MaxExtendedLength - 11 - 4 |
| 119 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 120 | // 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. |
| 122 | const 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 |
| 127 | type OMCI struct { |
| 128 | layers.BaseLayer |
| 129 | TransactionID uint16 |
| 130 | MessageType MessageType |
| 131 | DeviceIdentifier DeviceIdent |
David K. Bainbridge | d80007b | 2021-04-12 12:22:29 +0000 | [diff] [blame] | 132 | ResponseExpected bool // Significant for Download Section Request only |
| 133 | Payload []byte // TODO: Deprecated. Use layers.BaseLayer.Payload |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 134 | Length uint16 |
| 135 | MIC uint32 |
| 136 | } |
| 137 | |
| 138 | func (omci *OMCI) String() string { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 139 | 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 |
| 144 | func (omci *OMCI) LayerType() gopacket.LayerType { |
| 145 | return LayerTypeOMCI |
| 146 | } |
| 147 | |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 148 | // CanDecode returns the layers that this class can decode |
| 149 | func (omci *OMCI) CanDecode() gopacket.LayerClass { |
| 150 | return LayerTypeOMCI |
| 151 | } |
| 152 | |
| 153 | // NextLayerType returns the layer type contained by this DecodingLayer. |
| 154 | func (omci *OMCI) NextLayerType() gopacket.LayerType { |
| 155 | if next, ok := nextLayerMapping[omci.MessageType]; ok { |
| 156 | return next |
| 157 | } |
| 158 | return gopacket.LayerTypePayload |
| 159 | } |
| 160 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 161 | // LayerContents returns the OMCI specific layer information |
| 162 | func (omci *OMCI) LayerContents() []byte { |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 163 | b := make([]byte, 4) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 164 | binary.BigEndian.PutUint16(b, omci.TransactionID) |
| 165 | b[2] = byte(omci.MessageType) |
| 166 | b[3] = byte(omci.DeviceIdentifier) |
| 167 | return b |
| 168 | } |
| 169 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 170 | func decodeOMCI(data []byte, p gopacket.PacketBuilder) error { |
| 171 | // Allow baseline messages without Length & MIC, but no less |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 172 | if len(data) < 4 { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 173 | p.SetTruncated() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 174 | return errors.New("frame header too small") |
| 175 | } |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 176 | omci := &OMCI{} |
| 177 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 178 | switch DeviceIdent(data[3]) { |
| 179 | default: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 180 | return errors.New("unsupported message set/device identifier") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 181 | |
| 182 | case BaselineIdent: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 183 | if len(data) < MaxBaselineLength-8 { |
| 184 | p.SetTruncated() |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 185 | return fmt.Errorf("frame too small. OMCI baseline frame length %v, %v required", |
| 186 | len(data), MaxBaselineLength-8) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 187 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 188 | return omci.DecodeFromBytes(data, p) |
| 189 | |
| 190 | case ExtendedIdent: |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 191 | 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 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 196 | return omci.DecodeFromBytes(data, p) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func calculateMicAes128(data []byte) (uint32, error) { |
Holger Hildebrandt | ea6a611 | 2023-03-16 13:42:08 +0000 | [diff] [blame^] | 201 | // See if upstream or downstream. OMCI header should have been validated before this call |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 202 | var downstreamCDir = [...]byte{0x01} |
| 203 | var upstreamCDir = [...]byte{0x02} |
| 204 | |
| 205 | tid := binary.BigEndian.Uint16(data[0:2]) |
Holger Hildebrandt | ea6a611 | 2023-03-16 13:42:08 +0000 | [diff] [blame^] | 206 | |
| 207 | var length = 44 |
| 208 | if DeviceIdent(data[3]) == ExtendedIdent { |
| 209 | length = 10 + int(binary.BigEndian.Uint16(data[8:10])) |
| 210 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 211 | var sum []byte |
| 212 | var err error |
| 213 | |
| 214 | if (data[2]&me.AK) == me.AK || tid == 0 { |
Holger Hildebrandt | ea6a611 | 2023-03-16 13:42:08 +0000 | [diff] [blame^] | 215 | sum, err = aes.Sum(append(upstreamCDir[:], data[:length]...), omciIK, 4) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 216 | } else { |
Holger Hildebrandt | ea6a611 | 2023-03-16 13:42:08 +0000 | [diff] [blame^] | 217 | sum, err = aes.Sum(append(downstreamCDir[:], data[:length]...), omciIK, 4) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 218 | } |
| 219 | if err != nil { |
| 220 | return 0, err |
| 221 | } |
| 222 | return binary.BigEndian.Uint32(sum), nil |
| 223 | } |
| 224 | |
| 225 | ///////////////////////////////////////////////////////////////////////////// |
| 226 | // Baseline Message encode / decode |
| 227 | |
| 228 | // DecodeFromBytes will decode the OMCI layer of a packet/message |
| 229 | func (omci *OMCI) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 230 | // Minimal Baseline and Extended message set length has already been checked |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 231 | omci.TransactionID = binary.BigEndian.Uint16(data[0:]) |
| 232 | omci.MessageType = MessageType(data[2]) |
| 233 | omci.DeviceIdentifier = DeviceIdent(data[3]) |
Girish Gowdra | 8a7b456 | 2021-02-23 16:27:42 -0800 | [diff] [blame] | 234 | omci.ResponseExpected = byte(omci.MessageType)&me.AR == me.AR |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 235 | |
| 236 | isNotification := (int(omci.MessageType) & ^me.MsgTypeMask) == 0 |
| 237 | if omci.TransactionID == 0 && !isNotification { |
| 238 | return errors.New("omci Transaction ID is zero for non-Notification type message") |
| 239 | } |
| 240 | // Decode length |
| 241 | var payloadOffset int |
| 242 | var micOffset int |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 243 | var eomOffset int |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 244 | if omci.DeviceIdentifier == BaselineIdent { |
| 245 | omci.Length = MaxBaselineLength - 8 |
| 246 | payloadOffset = 8 |
| 247 | micOffset = MaxBaselineLength - 4 |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 248 | eomOffset = MaxBaselineLength - 8 |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 249 | |
| 250 | if len(data) >= micOffset { |
| 251 | length := binary.BigEndian.Uint32(data[micOffset-4:]) |
| 252 | if uint16(length) != omci.Length { |
| 253 | return me.NewProcessingError("invalid baseline message length") |
| 254 | } |
| 255 | } |
| 256 | } else { |
| 257 | payloadOffset = 10 |
| 258 | omci.Length = binary.BigEndian.Uint16(data[8:10]) |
| 259 | micOffset = int(omci.Length) + payloadOffset |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 260 | eomOffset = micOffset |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 261 | |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 262 | if omci.Length > uint16(MaxExtendedLength-payloadOffset) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 263 | return me.NewProcessingError("extended frame exceeds maximum allowed") |
| 264 | } |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 265 | if len(data) < micOffset { |
| 266 | p.SetTruncated() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 267 | return me.NewProcessingError("extended frame too small") |
| 268 | } |
| 269 | } |
| 270 | // Extract MIC if present in the data |
| 271 | if len(data) >= micOffset+4 { |
| 272 | omci.MIC = binary.BigEndian.Uint32(data[micOffset:]) |
| 273 | actual, _ := calculateMicAes128(data[:micOffset]) |
| 274 | if omci.MIC != actual { |
| 275 | _ = fmt.Sprintf("invalid MIC, expected %#x, got %#x", |
| 276 | omci.MIC, actual) |
| 277 | //return errors.New(msg) |
| 278 | } |
| 279 | } |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 280 | omci.BaseLayer = layers.BaseLayer{ |
| 281 | Contents: data[:4], |
| 282 | Payload: data[4:eomOffset], |
| 283 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 284 | p.AddLayer(omci) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 285 | nextLayer, err := MsgTypeToNextLayer(omci.MessageType, omci.DeviceIdentifier == ExtendedIdent) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | return p.NextDecoder(nextLayer) |
| 290 | } |
| 291 | |
| 292 | // SerializeTo writes the serialized form of this layer into the |
| 293 | // SerializationBuffer, implementing gopacket.SerializableLayer. |
| 294 | // See the docs for gopacket.SerializableLayer for more info. |
| 295 | func (omci *OMCI) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 296 | bytes, err := b.PrependBytes(4) |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | // OMCI layer error checks |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 301 | // Self-initiated Test Results have a TID of 0, OLT-requested do not. |
| 302 | isNotification := omci.MessageType == AlarmNotificationType || |
| 303 | omci.MessageType == AttributeValueChangeType |
| 304 | |
| 305 | if omci.TransactionID == 0 && !isNotification && omci.MessageType != TestResultType { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 306 | return errors.New("omci Transaction ID is zero for non-Notification type message") |
| 307 | } |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 308 | if omci.TransactionID != 0 && isNotification { |
| 309 | return errors.New("omci Transaction ID is not zero for Notification type message") |
| 310 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 311 | if omci.DeviceIdentifier == 0 { |
| 312 | omci.DeviceIdentifier = BaselineIdent // Allow uninitialized device identifier |
| 313 | } |
| 314 | if omci.DeviceIdentifier == BaselineIdent { |
| 315 | if omci.Length == 0 { |
| 316 | omci.Length = MaxBaselineLength - 8 // Allow uninitialized length |
| 317 | } else if omci.Length != MaxBaselineLength-8 { |
| 318 | msg := fmt.Sprintf("invalid Baseline message length: %v", omci.Length) |
| 319 | return errors.New(msg) |
| 320 | } |
| 321 | } else if omci.DeviceIdentifier == ExtendedIdent { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 322 | omci.Length = uint16(len(b.Bytes()) - 10) |
| 323 | |
| 324 | // Is length larger than maximum packet (less header and trailing MIC) |
| 325 | if omci.Length > MaxExtendedLength-10-4 { |
| 326 | msg := fmt.Sprintf("invalid Extended message length: %v", omci.Length) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 327 | return errors.New(msg) |
| 328 | } |
| 329 | } else { |
| 330 | msg := fmt.Sprintf("invalid device identifier: %#x, Baseline or Extended expected", |
| 331 | omci.DeviceIdentifier) |
| 332 | return errors.New(msg) |
| 333 | } |
| 334 | binary.BigEndian.PutUint16(bytes, omci.TransactionID) |
Girish Gowdra | 8a7b456 | 2021-02-23 16:27:42 -0800 | [diff] [blame] | 335 | // Download section request can optionally have the AR bit set or cleared. If user passes in this |
| 336 | // message type and sets download requested, fix up the message type for them. |
| 337 | if omci.MessageType == DownloadSectionRequestType && omci.ResponseExpected { |
| 338 | bytes[2] = byte(DownloadSectionRequestWithResponseType) |
| 339 | } else { |
| 340 | bytes[2] = byte(omci.MessageType) |
| 341 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 342 | bytes[3] = byte(omci.DeviceIdentifier) |
| 343 | b.PushLayer(LayerTypeOMCI) |
| 344 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 345 | if omci.DeviceIdentifier == BaselineIdent { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 346 | bufLen := len(b.Bytes()) |
| 347 | padSize := int(omci.Length) - bufLen + 4 |
| 348 | if padSize < 0 { |
| 349 | msg := fmt.Sprintf("invalid OMCI Message Type length, exceeded allowed frame size by %d bytes", |
| 350 | -padSize) |
| 351 | return errors.New(msg) |
| 352 | } |
| 353 | padding, err := b.AppendBytes(padSize) |
| 354 | if err != nil { |
| 355 | return err |
| 356 | } |
| 357 | copy(padding, lotsOfZeros[:]) |
| 358 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 359 | // For baseline, always provide the length |
| 360 | binary.BigEndian.PutUint32(b.Bytes()[MaxBaselineLength-8:], 40) |
| 361 | } |
| 362 | if opts.ComputeChecksums { |
| 363 | micBytes, err := b.AppendBytes(4) |
| 364 | if err != nil { |
| 365 | return err |
| 366 | } |
| 367 | omci.MIC, _ = calculateMicAes128(bytes[:MaxBaselineLength-4]) |
| 368 | binary.BigEndian.PutUint32(micBytes, omci.MIC) |
| 369 | } |
| 370 | return nil |
| 371 | } |
| 372 | |
| 373 | // hacky way to zero out memory... there must be a better way? |
| 374 | var lotsOfZeros [MaxExtendedLength]byte // Extended OMCI messages may be up to 1980 bytes long, including headers |