Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / layers / icmp6.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style license
5 // that can be found in the LICENSE file in the root of the source
6 // tree.
7
8 package layers
9
10 import (
11         "encoding/binary"
12         "errors"
13         "fmt"
14         "reflect"
15
16         "github.com/google/gopacket"
17 )
18
19 const (
20         // The following are from RFC 4443
21         ICMPv6TypeDestinationUnreachable = 1
22         ICMPv6TypePacketTooBig           = 2
23         ICMPv6TypeTimeExceeded           = 3
24         ICMPv6TypeParameterProblem       = 4
25         ICMPv6TypeEchoRequest            = 128
26         ICMPv6TypeEchoReply              = 129
27         // The following are from RFC 4861
28         ICMPv6TypeRouterSolicitation    = 133
29         ICMPv6TypeRouterAdvertisement   = 134
30         ICMPv6TypeNeighborSolicitation  = 135
31         ICMPv6TypeNeighborAdvertisement = 136
32         ICMPv6TypeRedirect              = 137
33 )
34
35 const (
36         // DestinationUnreachable
37         ICMPv6CodeNoRouteToDst           = 0
38         ICMPv6CodeAdminProhibited        = 1
39         ICMPv6CodeBeyondScopeOfSrc       = 2
40         ICMPv6CodeAddressUnreachable     = 3
41         ICMPv6CodePortUnreachable        = 4
42         ICMPv6CodeSrcAddressFailedPolicy = 5
43         ICMPv6CodeRejectRouteToDst       = 6
44
45         // TimeExceeded
46         ICMPv6CodeHopLimitExceeded               = 0
47         ICMPv6CodeFragmentReassemblyTimeExceeded = 1
48
49         // ParameterProblem
50         ICMPv6CodeErroneousHeaderField   = 0
51         ICMPv6CodeUnrecognizedNextHeader = 1
52         ICMPv6CodeUnrecognizedIPv6Option = 2
53 )
54
55 type icmpv6TypeCodeInfoStruct struct {
56         typeStr string
57         codeStr *map[uint8]string
58 }
59
60 var (
61         icmpv6TypeCodeInfo = map[uint8]icmpv6TypeCodeInfoStruct{
62                 ICMPv6TypeDestinationUnreachable: icmpv6TypeCodeInfoStruct{
63                         "DestinationUnreachable", &map[uint8]string{
64                                 ICMPv6CodeNoRouteToDst:           "NoRouteToDst",
65                                 ICMPv6CodeAdminProhibited:        "AdminProhibited",
66                                 ICMPv6CodeBeyondScopeOfSrc:       "BeyondScopeOfSrc",
67                                 ICMPv6CodeAddressUnreachable:     "AddressUnreachable",
68                                 ICMPv6CodePortUnreachable:        "PortUnreachable",
69                                 ICMPv6CodeSrcAddressFailedPolicy: "SrcAddressFailedPolicy",
70                                 ICMPv6CodeRejectRouteToDst:       "RejectRouteToDst",
71                         },
72                 },
73                 ICMPv6TypePacketTooBig: icmpv6TypeCodeInfoStruct{
74                         "PacketTooBig", nil,
75                 },
76                 ICMPv6TypeTimeExceeded: icmpv6TypeCodeInfoStruct{
77                         "TimeExceeded", &map[uint8]string{
78                                 ICMPv6CodeHopLimitExceeded:               "HopLimitExceeded",
79                                 ICMPv6CodeFragmentReassemblyTimeExceeded: "FragmentReassemblyTimeExceeded",
80                         },
81                 },
82                 ICMPv6TypeParameterProblem: icmpv6TypeCodeInfoStruct{
83                         "ParameterProblem", &map[uint8]string{
84                                 ICMPv6CodeErroneousHeaderField:   "ErroneousHeaderField",
85                                 ICMPv6CodeUnrecognizedNextHeader: "UnrecognizedNextHeader",
86                                 ICMPv6CodeUnrecognizedIPv6Option: "UnrecognizedIPv6Option",
87                         },
88                 },
89                 ICMPv6TypeEchoRequest: icmpv6TypeCodeInfoStruct{
90                         "EchoRequest", nil,
91                 },
92                 ICMPv6TypeEchoReply: icmpv6TypeCodeInfoStruct{
93                         "EchoReply", nil,
94                 },
95                 ICMPv6TypeRouterSolicitation: icmpv6TypeCodeInfoStruct{
96                         "RouterSolicitation", nil,
97                 },
98                 ICMPv6TypeRouterAdvertisement: icmpv6TypeCodeInfoStruct{
99                         "RouterAdvertisement", nil,
100                 },
101                 ICMPv6TypeNeighborSolicitation: icmpv6TypeCodeInfoStruct{
102                         "NeighborSolicitation", nil,
103                 },
104                 ICMPv6TypeNeighborAdvertisement: icmpv6TypeCodeInfoStruct{
105                         "NeighborAdvertisement", nil,
106                 },
107                 ICMPv6TypeRedirect: icmpv6TypeCodeInfoStruct{
108                         "Redirect", nil,
109                 },
110         }
111 )
112
113 type ICMPv6TypeCode uint16
114
115 // Type returns the ICMPv6 type field.
116 func (a ICMPv6TypeCode) Type() uint8 {
117         return uint8(a >> 8)
118 }
119
120 // Code returns the ICMPv6 code field.
121 func (a ICMPv6TypeCode) Code() uint8 {
122         return uint8(a)
123 }
124
125 func (a ICMPv6TypeCode) String() string {
126         t, c := a.Type(), a.Code()
127         strInfo, ok := icmpv6TypeCodeInfo[t]
128         if !ok {
129                 // Unknown ICMPv6 type field
130                 return fmt.Sprintf("%d(%d)", t, c)
131         }
132         typeStr := strInfo.typeStr
133         if strInfo.codeStr == nil && c == 0 {
134                 // The ICMPv6 type does not make use of the code field
135                 return fmt.Sprintf("%s", strInfo.typeStr)
136         }
137         if strInfo.codeStr == nil && c != 0 {
138                 // The ICMPv6 type does not make use of the code field, but it is present anyway
139                 return fmt.Sprintf("%s(Code: %d)", typeStr, c)
140         }
141         codeStr, ok := (*strInfo.codeStr)[c]
142         if !ok {
143                 // We don't know this ICMPv6 code; print the numerical value
144                 return fmt.Sprintf("%s(Code: %d)", typeStr, c)
145         }
146         return fmt.Sprintf("%s(%s)", typeStr, codeStr)
147 }
148
149 func (a ICMPv6TypeCode) GoString() string {
150         t := reflect.TypeOf(a)
151         return fmt.Sprintf("%s(%d, %d)", t.String(), a.Type(), a.Code())
152 }
153
154 // SerializeTo writes the ICMPv6TypeCode value to the 'bytes' buffer.
155 func (a ICMPv6TypeCode) SerializeTo(bytes []byte) {
156         binary.BigEndian.PutUint16(bytes, uint16(a))
157 }
158
159 // CreateICMPv6TypeCode is a convenience function to create an ICMPv6TypeCode
160 // gopacket type from the ICMPv6 type and code values.
161 func CreateICMPv6TypeCode(typ uint8, code uint8) ICMPv6TypeCode {
162         return ICMPv6TypeCode(binary.BigEndian.Uint16([]byte{typ, code}))
163 }
164
165 // ICMPv6 is the layer for IPv6 ICMP packet data
166 type ICMPv6 struct {
167         BaseLayer
168         TypeCode  ICMPv6TypeCode
169         Checksum  uint16
170         TypeBytes []byte
171         tcpipchecksum
172 }
173
174 // LayerType returns LayerTypeICMPv6.
175 func (i *ICMPv6) LayerType() gopacket.LayerType { return LayerTypeICMPv6 }
176
177 // DecodeFromBytes decodes the given bytes into this layer.
178 func (i *ICMPv6) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
179         if len(data) < 8 {
180                 df.SetTruncated()
181                 return errors.New("ICMP layer less then 8 bytes for ICMPv6 packet")
182         }
183         i.TypeCode = CreateICMPv6TypeCode(data[0], data[1])
184         i.Checksum = binary.BigEndian.Uint16(data[2:4])
185         i.TypeBytes = data[4:8]
186         i.BaseLayer = BaseLayer{data[:8], data[8:]}
187         return nil
188 }
189
190 // SerializeTo writes the serialized form of this layer into the
191 // SerializationBuffer, implementing gopacket.SerializableLayer.
192 // See the docs for gopacket.SerializableLayer for more info.
193 func (i *ICMPv6) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
194         if i.TypeBytes == nil {
195                 i.TypeBytes = lotsOfZeros[:4]
196         } else if len(i.TypeBytes) != 4 {
197                 return fmt.Errorf("invalid type bytes for ICMPv6 packet: %v", i.TypeBytes)
198         }
199         bytes, err := b.PrependBytes(8)
200         if err != nil {
201                 return err
202         }
203         i.TypeCode.SerializeTo(bytes)
204         copy(bytes[4:8], i.TypeBytes)
205         if opts.ComputeChecksums {
206                 bytes[2] = 0
207                 bytes[3] = 0
208                 csum, err := i.computeChecksum(b.Bytes(), IPProtocolICMPv6)
209                 if err != nil {
210                         return err
211                 }
212                 i.Checksum = csum
213         }
214         binary.BigEndian.PutUint16(bytes[2:], i.Checksum)
215         return nil
216 }
217
218 // CanDecode returns the set of layer types that this DecodingLayer can decode.
219 func (i *ICMPv6) CanDecode() gopacket.LayerClass {
220         return LayerTypeICMPv6
221 }
222
223 // NextLayerType returns the layer type contained by this DecodingLayer.
224 func (i *ICMPv6) NextLayerType() gopacket.LayerType {
225         return gopacket.LayerTypePayload
226 }
227
228 func decodeICMPv6(data []byte, p gopacket.PacketBuilder) error {
229         i := &ICMPv6{}
230         return decodingLayerDecoder(i, data, p)
231 }