Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 8 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 10 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 | adf422d | 2021-04-09 16:06:41 +0000 | [diff] [blame] | 26 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 27 | "github.com/aead/cmac/aes" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 28 | "github.com/google/gopacket" |
| 29 | "github.com/google/gopacket/layers" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 30 | me "github.com/opencord/omci-lib-go/v2/generated" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | // DeviceIdent identifies the OMCI message format. Currently either baseline or extended. |
| 34 | type DeviceIdent byte |
| 35 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 36 | // LayerTypeOMCI provides a gopacket LayerType for OMCI messages |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 56 | // ExtendedIdent messages are up to 1920 octets but may not be supported by all ONUs or OLTs. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [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. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 91 | const MaxAttributeGetNextBaselineLength = MaxBaselineLength - 11 - 8 |
| 92 | |
Chip Boling | 00a30d2 | 2021-05-04 13:31:52 -0500 | [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 | |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [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 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 | |
Chip Boling | 00a30d2 | 2021-05-04 13:31:52 -0500 | [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 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 | adf422d | 2021-04-09 16:06:41 +0000 | [diff] [blame] | 132 | ResponseExpected bool // Significant for Download Section Request only |
| 133 | Payload []byte // TODO: Deprecated. Use layers.BaseLayer.Payload |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 134 | Length uint16 |
| 135 | MIC uint32 |
| 136 | } |
| 137 | |
| 138 | func (omci *OMCI) String() string { |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 | |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [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 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 161 | // LayerContents returns the OMCI specific layer information |
| 162 | func (omci *OMCI) LayerContents() []byte { |
Chip Boling | d8543b2 | 2021-03-08 08:34:26 -0600 | [diff] [blame] | 163 | b := make([]byte, 4) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [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 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 170 | func decodeOMCI(data []byte, p gopacket.PacketBuilder) error { |
| 171 | // Allow baseline messages without Length & MIC, but no less |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 172 | if len(data) < 4 { |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [diff] [blame] | 173 | p.SetTruncated() |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 174 | return errors.New("frame header too small") |
| 175 | } |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [diff] [blame] | 176 | omci := &OMCI{} |
| 177 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 178 | switch DeviceIdent(data[3]) { |
| 179 | default: |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [diff] [blame] | 180 | return errors.New("unsupported message set/device identifier") |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 181 | |
| 182 | case BaselineIdent: |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 183 | if len(data) < MaxBaselineLength-8 { |
Chip Boling | d8637b0 | 2021-04-29 08:36:38 -0500 | [diff] [blame] | 184 | p.SetTruncated() |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 185 | return fmt.Errorf("frame too small. OMCI baseline frame length %v, %v required", |
| 186 | len(data), MaxBaselineLength-8) |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 187 | } |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 188 | return omci.DecodeFromBytes(data, p) |
| 189 | |
| 190 | case ExtendedIdent: |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [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 | } |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 196 | return omci.DecodeFromBytes(data, p) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func 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 |
| 224 | func (omci *OMCI) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 225 | // Minimal Baseline and Extended message set length has already been checked |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 226 | omci.TransactionID = binary.BigEndian.Uint16(data[0:]) |
| 227 | omci.MessageType = MessageType(data[2]) |
| 228 | omci.DeviceIdentifier = DeviceIdent(data[3]) |
Chip Boling | 8c8018e | 2021-02-22 15:56:00 -0600 | [diff] [blame] | 229 | omci.ResponseExpected = byte(omci.MessageType)&me.AR == me.AR |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 230 | |
| 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 Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 238 | var eomOffset int |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 239 | if omci.DeviceIdentifier == BaselineIdent { |
| 240 | omci.Length = MaxBaselineLength - 8 |
| 241 | payloadOffset = 8 |
| 242 | micOffset = MaxBaselineLength - 4 |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 243 | eomOffset = MaxBaselineLength - 8 |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 244 | |
| 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 Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 255 | eomOffset = micOffset |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 256 | |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 257 | if omci.Length > uint16(MaxExtendedLength-payloadOffset) { |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 258 | return me.NewProcessingError("extended frame exceeds maximum allowed") |
| 259 | } |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 260 | if len(data) < micOffset { |
| 261 | p.SetTruncated() |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 262 | 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 Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 275 | omci.BaseLayer = layers.BaseLayer{ |
| 276 | Contents: data[:4], |
| 277 | Payload: data[4:eomOffset], |
| 278 | } |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 279 | p.AddLayer(omci) |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 280 | nextLayer, err := MsgTypeToNextLayer(omci.MessageType, omci.DeviceIdentifier == ExtendedIdent) |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 281 | 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. |
| 290 | func (omci *OMCI) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error { |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 291 | bytes, err := b.PrependBytes(4) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | // OMCI layer error checks |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 296 | // 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 Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 301 | return errors.New("omci Transaction ID is zero for non-Notification type message") |
| 302 | } |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 303 | if omci.TransactionID != 0 && isNotification { |
| 304 | return errors.New("omci Transaction ID is not zero for Notification type message") |
| 305 | } |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 306 | 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 Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 317 | 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 Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 322 | 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 Boling | 8c8018e | 2021-02-22 15:56:00 -0600 | [diff] [blame] | 330 | // 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 Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 337 | bytes[3] = byte(omci.DeviceIdentifier) |
| 338 | b.PushLayer(LayerTypeOMCI) |
| 339 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 340 | if omci.DeviceIdentifier == BaselineIdent { |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 341 | 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 Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 354 | // 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? |
| 369 | var lotsOfZeros [MaxExtendedLength]byte // Extended OMCI messages may be up to 1980 bytes long, including headers |