-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayer_arp_test.go
246 lines (226 loc) · 8.68 KB
/
layer_arp_test.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package packet
import (
"bytes"
"net"
"net/netip"
"syscall"
"testing"
)
func newEtherPacket(hType uint16, srcMAC net.HardwareAddr, dstMAC net.HardwareAddr) Ether {
buf := make([]byte, EthMaxSize) // allocate in the stack
p := EncodeEther(buf, hType, srcMAC, dstMAC)
return p
}
func newARPPacket(op uint16, srcAddr Addr, dstAddr Addr) ARP {
b := make([]byte, ARPLen)
p := EncodeARP(b, op, srcAddr, dstAddr)
if p == nil {
panic("invalid arp packet")
}
return p
}
func TestMarshalUnmarshall(t *testing.T) {
// marshall
buf := make([]byte, EthMaxSize) // allocate in the stack
ether := EncodeEther(buf, syscall.ETH_P_ARP, mac1, mac2)
arpFrame := EncodeARP(ether.Payload(), ARPOperationRequest, addr1, addr2)
if arpFrame == nil {
t.Errorf("error in marshall binary: %v", arpFrame)
}
if len(ether) != 14 {
t.Errorf("invalid ether len=%d", len(ether))
}
if len(arpFrame) != 28 {
t.Errorf("invalid arp len=%d", len(arpFrame))
}
// unmarschall
ether.SetPayload(arpFrame)
n := len(ether)
ether = Ether(ether[:n])
arpFrame = ARP(ether.Payload())
if err := ether.IsValid(); err != nil {
t.Errorf("invalid ether=%s", ether)
}
if err := arpFrame.IsValid(); err != nil {
t.Errorf("invalid arp=%s", err)
}
}
func TestMarshalBinary(t *testing.T) {
tests := []struct {
name string
wantErr bool
proto uint16
operation uint16
srcMAC net.HardwareAddr
srcIP netip.Addr
dstMAC net.HardwareAddr
dstIP netip.Addr
}{
{name: "reply", wantErr: false, proto: syscall.ETH_P_ARP, operation: ARPOperationReply, srcMAC: mac1, srcIP: ip1, dstMAC: mac2, dstIP: ip2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := newARPPacket(tt.operation, Addr{MAC: tt.srcMAC, IP: tt.srcIP}, Addr{MAC: tt.dstMAC, IP: tt.dstIP})
p := ARP(buf)
if err := p.IsValid(); err != nil {
t.Errorf("%s: invalid arp err=%s", tt.name, err)
}
if p.Operation() != tt.operation {
t.Errorf("%s: invalid operation=%d want=%d", tt.name, p.Operation(), tt.operation)
}
if !bytes.Equal(p.SrcMAC(), tt.srcMAC) || !bytes.Equal(p.DstMAC(), tt.dstMAC) {
t.Errorf("%s: invalid srcMAC=%s wantSrcMAC=%s dstMAC=%s wantDstMAC=%s", tt.name, p.SrcMAC(), tt.srcMAC, p.DstMAC(), tt.dstMAC)
}
if p.SrcIP() != tt.srcIP || p.DstIP() != tt.dstIP {
t.Errorf("%s: invalid srcIP=%s wantSrcIP=%s dstIP=%s wantDstIP=%s", tt.name, p.SrcIP(), tt.srcIP, p.DstIP(), tt.dstIP)
}
})
}
}
func Test_Handler_ARPRequests(t *testing.T) {
session, _ := testSession()
// Debug = true
tests := []struct {
name string
ether Ether
arp ARP
wantErr error
wantLen int
wantIPs int
wantCountResponse int
}{
{name: "whois1",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr2, Addr{MAC: EthernetBroadcast, IP: ip3}),
wantErr: nil, wantLen: 3, wantIPs: 1, wantCountResponse: 0},
{name: "whois1-dup2",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr2, Addr{MAC: EthernetBroadcast, IP: ip3}),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "whois1-dup3",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr2, Addr{MAC: EthernetBroadcast, IP: ip3}),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "whois2",
ether: newEtherPacket(syscall.ETH_P_ARP, mac3, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr3, Addr{MAC: EthernetBroadcast, IP: routerIP4}),
wantErr: nil, wantLen: 4, wantIPs: 1},
{name: "announce-ip4",
ether: newEtherPacket(syscall.ETH_P_ARP, mac4, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr4, Addr{MAC: EthernetBroadcast, IP: ip4}),
wantErr: nil, wantLen: 5, wantIPs: 1},
{name: "host-whois-ip3", // host mac - ignore entry
ether: newEtherPacket(syscall.ETH_P_ARP, hostMAC, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, hostAddr, Addr{MAC: EthernetBroadcast, IP: ip3}),
wantErr: nil, wantLen: 5, wantIPs: 0},
{name: "router-whois-ip3", // router mac - ignore entry
ether: newEtherPacket(syscall.ETH_P_ARP, routerMAC, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, routerAddr, Addr{MAC: EthernetBroadcast, IP: ip3}),
wantErr: nil, wantLen: 5, wantIPs: 0},
{name: "probe", // probe does not add host but will send a probe reject if IP is not our DHCP IP
ether: newEtherPacket(syscall.ETH_P_ARP, mac5, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, Addr{MAC: mac5, IP: IPv4zero}, Addr{MAC: EthernetZero, IP: ip5}),
wantErr: nil, wantLen: 5, wantIPs: 0, wantCountResponse: 1},
{name: "localink", // local link IP does not add host
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, Addr{MAC: mac2, IP: localIP}, Addr{MAC: EthernetBroadcast, IP: localIP2}),
wantErr: nil, wantLen: 5, wantIPs: 0, wantCountResponse: 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ether, err := tt.ether.AppendPayload(tt.arp)
if err != nil {
panic(err)
}
// TODO: this test should be moved to Parse
// if bytes.Equal(SrcMAC(ether), session.NICInfo.HostAddr4.MAC) {
// return
// }
// fmt.Println("frame ether: ", ether, "frame arp: ", ARP(ether.Payload()), "srcarp: ", tt.arp)
result, err := session.Parse(ether)
if err != tt.wantErr {
t.Errorf("Test_Requests:%s error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
if result.Host != nil {
result.Host.MACEntry.Row.Lock() // test deadlock
defer result.Host.MACEntry.Row.Unlock()
}
if len(session.HostTable.Table) != tt.wantLen {
session.PrintTable()
t.Errorf("Test_Requests:%s table len = %v, wantLen %v", tt.name, len(session.HostTable.Table), tt.wantLen)
}
})
}
}
func Test_Handler_ServeReplies(t *testing.T) {
session, _ := testSession()
// Debug = true
tests := []struct {
name string
ether Ether
arp ARP
wantErr error
wantLen int
wantIPs int
}{
{name: "replyHost",
ether: newEtherPacket(syscall.ETH_P_ARP, hostMAC, mac2),
arp: newARPPacket(ARPOperationReply, hostAddr, addr2),
wantErr: nil, wantLen: 2, wantIPs: 0},
{name: "replyRouter",
ether: newEtherPacket(syscall.ETH_P_ARP, routerMAC, EthernetBroadcast),
arp: newARPPacket(ARPOperationReply, routerAddr, addr2),
wantErr: nil, wantLen: 2, wantIPs: 0},
{name: "replyLocalLink",
ether: newEtherPacket(syscall.ETH_P_ARP, routerMAC, EthernetBroadcast),
arp: newARPPacket(ARPOperationReply, Addr{MAC: mac2, IP: localIP}, Addr{MAC: EthernetZero, IP: localIP}),
wantErr: nil, wantLen: 2, wantIPs: 0},
{name: "replyMAC2",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, routerMAC),
arp: newARPPacket(ARPOperationReply, addr2, routerAddr),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "replyMAC2-dup",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, mac1),
arp: newARPPacket(ARPOperationReply, addr2, routerAddr),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "requestMAC2",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, hostMAC),
arp: newARPPacket(ARPOperationRequest, addr2, hostAddr),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "replyMAC2-dup2",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, hostMAC),
arp: newARPPacket(ARPOperationReply, addr2, hostAddr),
wantErr: nil, wantLen: 3, wantIPs: 1},
{name: "replyMAC2-newip",
ether: newEtherPacket(syscall.ETH_P_ARP, mac2, hostMAC),
arp: newARPPacket(ARPOperationReply, Addr{MAC: mac2, IP: ip3}, hostAddr),
wantErr: nil, wantLen: 4, wantIPs: 2},
{name: "requestMAC3-newip",
ether: newEtherPacket(syscall.ETH_P_ARP, mac3, EthernetBroadcast),
arp: newARPPacket(ARPOperationRequest, addr3, hostAddr),
wantErr: nil, wantLen: 4, wantIPs: 1},
{name: "replyMAC3",
ether: newEtherPacket(syscall.ETH_P_ARP, mac3, hostMAC),
arp: newARPPacket(ARPOperationReply, Addr{MAC: mac3, IP: ip4}, hostAddr),
wantErr: nil, wantLen: 5, wantIPs: 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ether, err := tt.ether.AppendPayload(tt.arp)
if err != nil {
panic(err)
}
result, err := session.Parse(ether)
if err != tt.wantErr {
t.Errorf("Test_Requests:%s error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
if result.Host != nil {
result.Host.MACEntry.Row.Lock() // test deadlock
defer result.Host.MACEntry.Row.Unlock()
}
if len(session.HostTable.Table) != tt.wantLen {
t.Errorf("Test_Requests:%s table len = %v, wantLen %v", tt.name, len(session.HostTable.Table), tt.wantLen)
}
})
}
}