1 // Copyright 2012 Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
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
16 "github.com/google/gopacket"
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
36 // DestinationUnreachable
37 ICMPv6CodeNoRouteToDst = 0
38 ICMPv6CodeAdminProhibited = 1
39 ICMPv6CodeBeyondScopeOfSrc = 2
40 ICMPv6CodeAddressUnreachable = 3
41 ICMPv6CodePortUnreachable = 4
42 ICMPv6CodeSrcAddressFailedPolicy = 5
43 ICMPv6CodeRejectRouteToDst = 6
46 ICMPv6CodeHopLimitExceeded = 0
47 ICMPv6CodeFragmentReassemblyTimeExceeded = 1
50 ICMPv6CodeErroneousHeaderField = 0
51 ICMPv6CodeUnrecognizedNextHeader = 1
52 ICMPv6CodeUnrecognizedIPv6Option = 2
55 type icmpv6TypeCodeInfoStruct struct {
57 codeStr *map[uint8]string
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",
73 ICMPv6TypePacketTooBig: icmpv6TypeCodeInfoStruct{
76 ICMPv6TypeTimeExceeded: icmpv6TypeCodeInfoStruct{
77 "TimeExceeded", &map[uint8]string{
78 ICMPv6CodeHopLimitExceeded: "HopLimitExceeded",
79 ICMPv6CodeFragmentReassemblyTimeExceeded: "FragmentReassemblyTimeExceeded",
82 ICMPv6TypeParameterProblem: icmpv6TypeCodeInfoStruct{
83 "ParameterProblem", &map[uint8]string{
84 ICMPv6CodeErroneousHeaderField: "ErroneousHeaderField",
85 ICMPv6CodeUnrecognizedNextHeader: "UnrecognizedNextHeader",
86 ICMPv6CodeUnrecognizedIPv6Option: "UnrecognizedIPv6Option",
89 ICMPv6TypeEchoRequest: icmpv6TypeCodeInfoStruct{
92 ICMPv6TypeEchoReply: icmpv6TypeCodeInfoStruct{
95 ICMPv6TypeRouterSolicitation: icmpv6TypeCodeInfoStruct{
96 "RouterSolicitation", nil,
98 ICMPv6TypeRouterAdvertisement: icmpv6TypeCodeInfoStruct{
99 "RouterAdvertisement", nil,
101 ICMPv6TypeNeighborSolicitation: icmpv6TypeCodeInfoStruct{
102 "NeighborSolicitation", nil,
104 ICMPv6TypeNeighborAdvertisement: icmpv6TypeCodeInfoStruct{
105 "NeighborAdvertisement", nil,
107 ICMPv6TypeRedirect: icmpv6TypeCodeInfoStruct{
113 type ICMPv6TypeCode uint16
115 // Type returns the ICMPv6 type field.
116 func (a ICMPv6TypeCode) Type() uint8 {
120 // Code returns the ICMPv6 code field.
121 func (a ICMPv6TypeCode) Code() uint8 {
125 func (a ICMPv6TypeCode) String() string {
126 t, c := a.Type(), a.Code()
127 strInfo, ok := icmpv6TypeCodeInfo[t]
129 // Unknown ICMPv6 type field
130 return fmt.Sprintf("%d(%d)", t, c)
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)
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)
141 codeStr, ok := (*strInfo.codeStr)[c]
143 // We don't know this ICMPv6 code; print the numerical value
144 return fmt.Sprintf("%s(Code: %d)", typeStr, c)
146 return fmt.Sprintf("%s(%s)", typeStr, codeStr)
149 func (a ICMPv6TypeCode) GoString() string {
150 t := reflect.TypeOf(a)
151 return fmt.Sprintf("%s(%d, %d)", t.String(), a.Type(), a.Code())
154 // SerializeTo writes the ICMPv6TypeCode value to the 'bytes' buffer.
155 func (a ICMPv6TypeCode) SerializeTo(bytes []byte) {
156 binary.BigEndian.PutUint16(bytes, uint16(a))
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}))
165 // ICMPv6 is the layer for IPv6 ICMP packet data
168 TypeCode ICMPv6TypeCode
174 // LayerType returns LayerTypeICMPv6.
175 func (i *ICMPv6) LayerType() gopacket.LayerType { return LayerTypeICMPv6 }
177 // DecodeFromBytes decodes the given bytes into this layer.
178 func (i *ICMPv6) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
181 return errors.New("ICMP layer less then 8 bytes for ICMPv6 packet")
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:]}
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)
199 bytes, err := b.PrependBytes(8)
203 i.TypeCode.SerializeTo(bytes)
204 copy(bytes[4:8], i.TypeBytes)
205 if opts.ComputeChecksums {
208 csum, err := i.computeChecksum(b.Bytes(), IPProtocolICMPv6)
214 binary.BigEndian.PutUint16(bytes[2:], i.Checksum)
218 // CanDecode returns the set of layer types that this DecodingLayer can decode.
219 func (i *ICMPv6) CanDecode() gopacket.LayerClass {
220 return LayerTypeICMPv6
223 // NextLayerType returns the layer type contained by this DecodingLayer.
224 func (i *ICMPv6) NextLayerType() gopacket.LayerType {
225 return gopacket.LayerTypePayload
228 func decodeICMPv6(data []byte, p gopacket.PacketBuilder) error {
230 return decodingLayerDecoder(i, data, p)