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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package header_test
import (
"bytes"
"crypto/sha256"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
const (
linkAddr = tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06")
linkLocalAddr = tcpip.Address("\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")
uniqueLocalAddr1 = tcpip.Address("\xfc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")
uniqueLocalAddr2 = tcpip.Address("\xfd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02")
globalAddr = tcpip.Address("\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")
)
func TestEthernetAdddressToModifiedEUI64(t *testing.T) {
expectedIID := [header.IIDSize]byte{0, 2, 3, 255, 254, 4, 5, 6}
if diff := cmp.Diff(expectedIID, header.EthernetAddressToModifiedEUI64(linkAddr)); diff != "" {
t.Errorf("EthernetAddressToModifiedEUI64(%s) mismatch (-want +got):\n%s", linkAddr, diff)
}
var buf [header.IIDSize]byte
header.EthernetAdddressToModifiedEUI64IntoBuf(linkAddr, buf[:])
if diff := cmp.Diff(expectedIID, buf); diff != "" {
t.Errorf("EthernetAddressToModifiedEUI64IntoBuf(%s, _) mismatch (-want +got):\n%s", linkAddr, diff)
}
}
func TestLinkLocalAddr(t *testing.T) {
if got, want := header.LinkLocalAddr(linkAddr), tcpip.Address("\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x02\x03\xff\xfe\x04\x05\x06"); got != want {
t.Errorf("got LinkLocalAddr(%s) = %s, want = %s", linkAddr, got, want)
}
}
func TestAppendOpaqueInterfaceIdentifier(t *testing.T) {
var secretKeyBuf [header.OpaqueIIDSecretKeyMinBytes * 2]byte
if n, err := rand.Read(secretKeyBuf[:]); err != nil {
t.Fatalf("rand.Read(_): %s", err)
} else if want := header.OpaqueIIDSecretKeyMinBytes * 2; n != want {
t.Fatalf("expected rand.Read to read %d bytes, read %d bytes", want, n)
}
tests := []struct {
name string
prefix tcpip.Subnet
nicName string
dadCounter uint8
secretKey []byte
}{
{
name: "SecretKey of minimum size",
prefix: header.IPv6LinkLocalPrefix.Subnet(),
nicName: "eth0",
dadCounter: 0,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes],
},
{
name: "SecretKey of less than minimum size",
prefix: func() tcpip.Subnet {
addrWithPrefix := tcpip.AddressWithPrefix{
Address: "\x01\x02\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
PrefixLen: header.IIDOffsetInIPv6Address * 8,
}
return addrWithPrefix.Subnet()
}(),
nicName: "eth10",
dadCounter: 1,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes/2],
},
{
name: "SecretKey of more than minimum size",
prefix: func() tcpip.Subnet {
addrWithPrefix := tcpip.AddressWithPrefix{
Address: "\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
PrefixLen: header.IIDOffsetInIPv6Address * 8,
}
return addrWithPrefix.Subnet()
}(),
nicName: "eth11",
dadCounter: 2,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes*2],
},
{
name: "Nil SecretKey and empty nicName",
prefix: func() tcpip.Subnet {
addrWithPrefix := tcpip.AddressWithPrefix{
Address: "\x01\x02\x03\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
PrefixLen: header.IIDOffsetInIPv6Address * 8,
}
return addrWithPrefix.Subnet()
}(),
nicName: "",
dadCounter: 3,
secretKey: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h := sha256.New()
h.Write([]byte(test.prefix.ID()[:header.IIDOffsetInIPv6Address]))
h.Write([]byte(test.nicName))
h.Write([]byte{test.dadCounter})
if k := test.secretKey; k != nil {
h.Write(k)
}
var hashSum [sha256.Size]byte
h.Sum(hashSum[:0])
want := hashSum[:header.IIDSize]
// Passing a nil buffer should result in a new buffer returned with the
// IID.
if got := header.AppendOpaqueInterfaceIdentifier(nil, test.prefix, test.nicName, test.dadCounter, test.secretKey); !bytes.Equal(got, want) {
t.Errorf("got AppendOpaqueInterfaceIdentifier(nil, %s, %s, %d, %x) = %x, want = %x", test.prefix, test.nicName, test.dadCounter, test.secretKey, got, want)
}
// Passing a buffer with sufficient capacity for the IID should populate
// the buffer provided.
var iidBuf [header.IIDSize]byte
if got := header.AppendOpaqueInterfaceIdentifier(iidBuf[:0], test.prefix, test.nicName, test.dadCounter, test.secretKey); !bytes.Equal(got, want) {
t.Errorf("got AppendOpaqueInterfaceIdentifier(iidBuf[:0], %s, %s, %d, %x) = %x, want = %x", test.prefix, test.nicName, test.dadCounter, test.secretKey, got, want)
}
if got := iidBuf[:]; !bytes.Equal(got, want) {
t.Errorf("got iidBuf = %x, want = %x", got, want)
}
})
}
}
func TestLinkLocalAddrWithOpaqueIID(t *testing.T) {
var secretKeyBuf [header.OpaqueIIDSecretKeyMinBytes * 2]byte
if n, err := rand.Read(secretKeyBuf[:]); err != nil {
t.Fatalf("rand.Read(_): %s", err)
} else if want := header.OpaqueIIDSecretKeyMinBytes * 2; n != want {
t.Fatalf("expected rand.Read to read %d bytes, read %d bytes", want, n)
}
prefix := header.IPv6LinkLocalPrefix.Subnet()
tests := []struct {
name string
prefix tcpip.Subnet
nicName string
dadCounter uint8
secretKey []byte
}{
{
name: "SecretKey of minimum size",
nicName: "eth0",
dadCounter: 0,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes],
},
{
name: "SecretKey of less than minimum size",
nicName: "eth10",
dadCounter: 1,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes/2],
},
{
name: "SecretKey of more than minimum size",
nicName: "eth11",
dadCounter: 2,
secretKey: secretKeyBuf[:header.OpaqueIIDSecretKeyMinBytes*2],
},
{
name: "Nil SecretKey and empty nicName",
nicName: "",
dadCounter: 3,
secretKey: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
addrBytes := [header.IPv6AddressSize]byte{
0: 0xFE,
1: 0x80,
}
want := tcpip.Address(header.AppendOpaqueInterfaceIdentifier(
addrBytes[:header.IIDOffsetInIPv6Address],
prefix,
test.nicName,
test.dadCounter,
test.secretKey,
))
if got := header.LinkLocalAddrWithOpaqueIID(test.nicName, test.dadCounter, test.secretKey); got != want {
t.Errorf("got LinkLocalAddrWithOpaqueIID(%s, %d, %x) = %s, want = %s", test.nicName, test.dadCounter, test.secretKey, got, want)
}
})
}
}
func TestIsV6UniqueLocalAddress(t *testing.T) {
tests := []struct {
name string
addr tcpip.Address
expected bool
}{
{
name: "Valid Unique 1",
addr: uniqueLocalAddr1,
expected: true,
},
{
name: "Valid Unique 2",
addr: uniqueLocalAddr1,
expected: true,
},
{
name: "Link Local",
addr: linkLocalAddr,
expected: false,
},
{
name: "Global",
addr: globalAddr,
expected: false,
},
{
name: "IPv4",
addr: "\x01\x02\x03\x04",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := header.IsV6UniqueLocalAddress(test.addr); got != test.expected {
t.Errorf("got header.IsV6UniqueLocalAddress(%s) = %t, want = %t", test.addr, got, test.expected)
}
})
}
}
func TestScopeForIPv6Address(t *testing.T) {
tests := []struct {
name string
addr tcpip.Address
scope header.IPv6AddressScope
err *tcpip.Error
}{
{
name: "Unique Local",
addr: uniqueLocalAddr1,
scope: header.UniqueLocalScope,
err: nil,
},
{
name: "Link Local",
addr: linkLocalAddr,
scope: header.LinkLocalScope,
err: nil,
},
{
name: "Global",
addr: globalAddr,
scope: header.GlobalScope,
err: nil,
},
{
name: "IPv4",
addr: "\x01\x02\x03\x04",
scope: header.GlobalScope,
err: tcpip.ErrBadAddress,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := header.ScopeForIPv6Address(test.addr)
if err != test.err {
t.Errorf("got header.IsV6UniqueLocalAddress(%s) = (_, %v), want = (_, %v)", test.addr, err, test.err)
}
if got != test.scope {
t.Errorf("got header.IsV6UniqueLocalAddress(%s) = (%d, _), want = (%d, _)", test.addr, got, test.scope)
}
})
}
}
func TestSolicitedNodeAddr(t *testing.T) {
tests := []struct {
addr tcpip.Address
want tcpip.Address
}{
{
addr: "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\xa0",
want: "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\x0e\x0f\xa0",
},
{
addr: "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\xdd\x0e\x0f\xa0",
want: "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\x0e\x0f\xa0",
},
{
addr: "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\xdd\x01\x02\x03",
want: "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\x01\x02\x03",
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s", test.addr), func(t *testing.T) {
if got := header.SolicitedNodeAddr(test.addr); got != test.want {
t.Fatalf("got header.SolicitedNodeAddr(%s) = %s, want = %s", test.addr, got, test.want)
}
})
}
}
|