-
Notifications
You must be signed in to change notification settings - Fork 6
/
ndp.go
203 lines (178 loc) · 5.31 KB
/
ndp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"crypto/rand"
"errors"
"fmt"
"io"
"math"
"net"
"net/netip"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"golang.org/x/net/bpf"
)
// tcpdump -dd 'icmp6 && ip6[40]==135'
var bpfFilter = []bpf.RawInstruction{
{0x28, 0, 0, 0x0000000c},
{0x15, 0, 8, 0x000086dd},
{0x30, 0, 0, 0x00000014},
{0x15, 3, 0, 0x0000003a},
{0x15, 0, 5, 0x0000002c},
{0x30, 0, 0, 0x00000036},
{0x15, 0, 3, 0x0000003a},
{0x30, 0, 0, 0x00000036},
{0x15, 0, 1, 0x00000087},
{0x6, 0, 0, 0x00040000},
{0x6, 0, 0, 0x00000000},
}
var packetSerializeOpts = gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
var solicitTypeCode = layers.CreateICMPv6TypeCode(layers.ICMPv6TypeNeighborSolicitation, 0)
var advertTypeCode = layers.CreateICMPv6TypeCode(layers.ICMPv6TypeNeighborAdvertisement, 0)
// Gratuitous creates a gratuitous ICMPv6 neighbor solicitation packet.
func Gratuitous(w gopacket.SerializeBuffer, hi HostInfo, targetIP netip.Addr) error {
ip16 := targetIP.As16()
eth := layers.Ethernet{
SrcMAC: hi.HostMAC,
DstMAC: net.HardwareAddr{0x33, 0x33, 0xFF, ip16[13], ip16[14], ip16[15]},
EthernetType: layers.EthernetTypeIPv6,
}
dstIP := net.IP{0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0xFF, ip16[13], ip16[14], ip16[15]}
ip6 := layers.IPv6{
Version: 6,
SrcIP: make(net.IP, net.IPv6len),
DstIP: dstIP,
NextHeader: layers.IPProtocolICMPv6,
HopLimit: math.MaxUint8,
}
icmp6 := layers.ICMPv6{
TypeCode: solicitTypeCode,
}
icmp6.SetNetworkLayerForChecksum(&ip6)
nonce := make([]byte, 6)
rand.Read(nonce)
solicit := layers.ICMPv6NeighborSolicitation{
TargetAddress: targetIP.AsSlice(),
Options: layers.ICMPv6Options{
{
Type: 0x0E,
Data: nonce,
},
},
}
return gopacket.SerializeLayers(w, packetSerializeOpts, ð, &ip6, &icmp6, &solicit)
}
// Solicit creates an ICMPv6 neighbor solicitation packet.
func Solicit(w gopacket.SerializeBuffer, hi HostInfo, sourceIP netip.Addr) error {
eth := layers.Ethernet{
SrcMAC: hi.HostMAC,
DstMAC: net.HardwareAddr{0x33, 0x33, 0xFF, 0x00, 0x00, 0x01},
EthernetType: layers.EthernetTypeIPv6,
}
dstIP := net.IP{0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x01}
ip6 := layers.IPv6{
Version: 6,
SrcIP: sourceIP.AsSlice(),
DstIP: dstIP,
NextHeader: layers.IPProtocolICMPv6,
HopLimit: math.MaxUint8,
}
icmp6 := layers.ICMPv6{
TypeCode: solicitTypeCode,
}
icmp6.SetNetworkLayerForChecksum(&ip6)
nonce := make([]byte, 6)
rand.Read(nonce)
solicit := layers.ICMPv6NeighborSolicitation{
TargetAddress: hi.GatewayIP.AsSlice(),
Options: layers.ICMPv6Options{
{
Type: layers.ICMPv6OptSourceAddress,
Data: []byte(hi.HostMAC),
},
},
}
return gopacket.SerializeLayers(w, packetSerializeOpts, ð, &ip6, &icmp6, &solicit)
}
// NeighSolicitation contains information from an ICMPv6 neighbor solicitation packet.
type NeighSolicitation struct {
RouterMAC [6]byte
RouterIP netip.Addr
DestIP netip.Addr
TargetIP netip.Addr
}
func (ns NeighSolicitation) String() string {
if ns.DestIP.IsMulticast() {
return fmt.Sprintf("who-has %s tell %s", ns.TargetIP, ns.RouterIP)
}
return fmt.Sprintf("is-alive %s tell %s", ns.TargetIP, ns.RouterIP)
}
// Respond creates an ICMPv6 neighbor advertisement packet.
func (ns NeighSolicitation) Respond(w gopacket.SerializeBuffer, hi HostInfo) error {
eth := layers.Ethernet{
SrcMAC: hi.HostMAC,
DstMAC: net.HardwareAddr(ns.RouterMAC[:]),
EthernetType: layers.EthernetTypeIPv6,
}
ip6 := layers.IPv6{
Version: 6,
SrcIP: ns.TargetIP.AsSlice(),
DstIP: ns.RouterIP.AsSlice(),
NextHeader: layers.IPProtocolICMPv6,
HopLimit: math.MaxUint8,
}
icmp6 := layers.ICMPv6{
TypeCode: advertTypeCode,
}
icmp6.SetNetworkLayerForChecksum(&ip6)
var advertFlags uint8 = 0x80 | 0x40 // router, solicited
if ns.DestIP.IsMulticast() {
advertFlags |= 0x20 // override
}
advert := layers.ICMPv6NeighborAdvertisement{
Flags: advertFlags,
TargetAddress: ns.TargetIP.AsSlice(),
Options: layers.ICMPv6Options{
{
Type: layers.ICMPv6OptTargetAddress,
Data: []byte(hi.HostMAC),
},
},
}
return gopacket.SerializeLayers(w, packetSerializeOpts, ð, &ip6, &icmp6, &advert)
}
// CaptureNeighSolicitation captures ICMPv6 neighbor solicitation packets.
func CaptureNeighSolicitation(src gopacket.ZeroCopyPacketDataSource) <-chan NeighSolicitation {
ch := make(chan NeighSolicitation)
go func() {
var eth layers.Ethernet
var ip6 layers.IPv6
var icmp6 layers.ICMPv6
var solicit layers.ICMPv6NeighborSolicitation
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, ð, &ip6, &icmp6, &solicit)
decoded := []gopacket.LayerType{}
for {
pkt, _, e := src.ZeroCopyReadPacketData()
if errors.Is(e, io.EOF) {
close(ch)
return
}
if e := parser.DecodeLayers(pkt, &decoded); e != nil {
continue
}
if len(decoded) == 4 && decoded[3] == layers.LayerTypeICMPv6NeighborSolicitation {
ns := NeighSolicitation{}
copy(ns.RouterMAC[:], eth.SrcMAC)
ns.RouterIP, _ = netip.AddrFromSlice(ip6.SrcIP)
ns.DestIP, _ = netip.AddrFromSlice(ip6.DstIP)
ns.TargetIP, _ = netip.AddrFromSlice(solicit.TargetAddress)
ch <- ns
}
}
}()
return ch
}