|
5 | 5 | "net/netip" |
6 | 6 | "testing" |
7 | 7 |
|
| 8 | + "github.com/google/gopacket" |
8 | 9 | "github.com/google/gopacket/layers" |
9 | 10 | "github.com/stretchr/testify/require" |
10 | 11 | "golang.org/x/net/icmp" |
@@ -101,4 +102,96 @@ func assertTTLExceedPacket(t *testing.T, pk *ICMP) { |
101 | 102 | require.Len(t, rawTTLExceedPacket.Data, headerLen+icmpHeaderLen+len(rawPacket.Data)) |
102 | 103 | require.True(t, bytes.Equal(rawPacket.Data, rawTTLExceedPacket.Data[headerLen+icmpHeaderLen:])) |
103 | 104 | } |
| 105 | + |
| 106 | + decoder := NewICMPDecoder() |
| 107 | + decodedPacket, err := decoder.Decode(rawTTLExceedPacket) |
| 108 | + require.NoError(t, err) |
| 109 | + assertICMPChecksum(t, decodedPacket) |
| 110 | +} |
| 111 | + |
| 112 | +func assertICMPChecksum(t *testing.T, icmpPacket *ICMP) { |
| 113 | + buf := gopacket.NewSerializeBuffer() |
| 114 | + if icmpPacket.Protocol == layers.IPProtocolICMPv4 { |
| 115 | + icmpv4 := layers.ICMPv4{ |
| 116 | + TypeCode: layers.CreateICMPv4TypeCode(uint8(icmpPacket.Type.(ipv4.ICMPType)), uint8(icmpPacket.Code)), |
| 117 | + } |
| 118 | + switch body := icmpPacket.Body.(type) { |
| 119 | + case *icmp.Echo: |
| 120 | + icmpv4.Id = uint16(body.ID) |
| 121 | + icmpv4.Seq = uint16(body.Seq) |
| 122 | + payload := gopacket.Payload(body.Data) |
| 123 | + require.NoError(t, payload.SerializeTo(buf, serializeOpts)) |
| 124 | + default: |
| 125 | + require.NoError(t, serializeICMPAsPayload(icmpPacket.Message, buf)) |
| 126 | + } |
| 127 | + // SerializeTo sets the checksum in icmpv4 |
| 128 | + require.NoError(t, icmpv4.SerializeTo(buf, serializeOpts)) |
| 129 | + require.Equal(t, icmpv4.Checksum, uint16(icmpPacket.Checksum)) |
| 130 | + } else { |
| 131 | + switch body := icmpPacket.Body.(type) { |
| 132 | + case *icmp.Echo: |
| 133 | + payload := gopacket.Payload(body.Data) |
| 134 | + require.NoError(t, payload.SerializeTo(buf, serializeOpts)) |
| 135 | + echo := layers.ICMPv6Echo{ |
| 136 | + Identifier: uint16(body.ID), |
| 137 | + SeqNumber: uint16(body.Seq), |
| 138 | + } |
| 139 | + require.NoError(t, echo.SerializeTo(buf, serializeOpts)) |
| 140 | + default: |
| 141 | + require.NoError(t, serializeICMPAsPayload(icmpPacket.Message, buf)) |
| 142 | + } |
| 143 | + |
| 144 | + icmpv6 := layers.ICMPv6{ |
| 145 | + TypeCode: layers.CreateICMPv6TypeCode(uint8(icmpPacket.Type.(ipv6.ICMPType)), uint8(icmpPacket.Code)), |
| 146 | + } |
| 147 | + ipLayer := layers.IPv6{ |
| 148 | + Version: 6, |
| 149 | + SrcIP: icmpPacket.Src.AsSlice(), |
| 150 | + DstIP: icmpPacket.Dst.AsSlice(), |
| 151 | + NextHeader: icmpPacket.Protocol, |
| 152 | + HopLimit: icmpPacket.TTL, |
| 153 | + } |
| 154 | + require.NoError(t, icmpv6.SetNetworkLayerForChecksum(&ipLayer)) |
| 155 | + |
| 156 | + // SerializeTo sets the checksum in icmpv4 |
| 157 | + require.NoError(t, icmpv6.SerializeTo(buf, serializeOpts)) |
| 158 | + require.Equal(t, icmpv6.Checksum, uint16(icmpPacket.Checksum)) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func serializeICMPAsPayload(message *icmp.Message, buf gopacket.SerializeBuffer) error { |
| 163 | + serializedBody, err := message.Body.Marshal(message.Type.Protocol()) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + payload := gopacket.Payload(serializedBody) |
| 168 | + return payload.SerializeTo(buf, serializeOpts) |
| 169 | +} |
| 170 | + |
| 171 | +func TestChecksum(t *testing.T) { |
| 172 | + data := []byte{0x63, 0x2c, 0x49, 0xd6, 0x00, 0x0d, 0xc1, 0xda} |
| 173 | + pk := ICMP{ |
| 174 | + IP: &IP{ |
| 175 | + Src: netip.MustParseAddr("2606:4700:110:89c1:c63a:861:e08c:b049"), |
| 176 | + Dst: netip.MustParseAddr("fde8:b693:d420:109b::2"), |
| 177 | + Protocol: layers.IPProtocolICMPv6, |
| 178 | + TTL: 3, |
| 179 | + }, |
| 180 | + Message: &icmp.Message{ |
| 181 | + Type: ipv6.ICMPTypeEchoRequest, |
| 182 | + Code: 0, |
| 183 | + Body: &icmp.Echo{ |
| 184 | + ID: 0x20a7, |
| 185 | + Seq: 8, |
| 186 | + Data: data, |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | + encoder := NewEncoder() |
| 191 | + encoded, err := encoder.Encode(&pk) |
| 192 | + require.NoError(t, err) |
| 193 | + |
| 194 | + decoder := NewICMPDecoder() |
| 195 | + decoded, err := decoder.Decode(encoded) |
| 196 | + require.Equal(t, 0xff96, decoded.Checksum) |
104 | 197 | } |
0 commit comments