2019-10-22 14:25:35 +00:00
|
|
|
// Copyright 2018 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
2021-04-15 22:09:09 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/testutil"
|
2019-10-22 14:25:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsValidUnicastEthernetAddress(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
addr tcpip.LinkAddress
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Nil",
|
|
|
|
tcpip.LinkAddress([]byte(nil)),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Empty",
|
|
|
|
tcpip.LinkAddress(""),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidLength",
|
|
|
|
tcpip.LinkAddress("\x01\x02\x03"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Unspecified",
|
Add loopback interface as an ethernet-based device
...to match Linux behaviour.
We can see evidence of Linux representing loopback as an ethernet-based
device below:
```
# EUI-48 based MAC addresses.
$ ip link show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# tcpdump showing ethernet frames when sniffing loopback and logging the
# link-type as EN10MB (Ethernet).
$ sudo tcpdump -i lo -e -c 2 -n
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
03:09:05.002034 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 127.0.0.1.9557 > 127.0.0.1.36828: Flags [.], ack 3562800815, win 15342, options [nop,nop,TS val 843174495 ecr 843159493], length 0
03:09:05.002094 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 127.0.0.1.36828 > 127.0.0.1.9557: Flags [.], ack 1, win 6160, options [nop,nop,TS val 843174496 ecr 843159493], length 0
2 packets captured
116 packets received by filter
0 packets dropped by kernel
```
Wireshark shows a similar result as the tcpdump example above.
Linux's loopback setup: https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/drivers/net/loopback.c#L162
PiperOrigin-RevId: 391836719
2021-08-19 20:51:40 +00:00
|
|
|
UnspecifiedEthernetAddress,
|
2019-10-22 14:25:35 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Multicast",
|
|
|
|
tcpip.LinkAddress("\x01\x02\x03\x04\x05\x06"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Valid",
|
|
|
|
tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06"),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if got := IsValidUnicastEthernetAddress(test.addr); got != test.expected {
|
|
|
|
t.Fatalf("got IsValidUnicastEthernetAddress = %t, want = %t", got, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 21:24:48 +00:00
|
|
|
|
2020-10-09 19:07:02 +00:00
|
|
|
func TestIsMulticastEthernetAddress(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
addr tcpip.LinkAddress
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Nil",
|
|
|
|
tcpip.LinkAddress([]byte(nil)),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Empty",
|
|
|
|
tcpip.LinkAddress(""),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidLength",
|
|
|
|
tcpip.LinkAddress("\x01\x02\x03"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Unspecified",
|
Add loopback interface as an ethernet-based device
...to match Linux behaviour.
We can see evidence of Linux representing loopback as an ethernet-based
device below:
```
# EUI-48 based MAC addresses.
$ ip link show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# tcpdump showing ethernet frames when sniffing loopback and logging the
# link-type as EN10MB (Ethernet).
$ sudo tcpdump -i lo -e -c 2 -n
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
03:09:05.002034 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 127.0.0.1.9557 > 127.0.0.1.36828: Flags [.], ack 3562800815, win 15342, options [nop,nop,TS val 843174495 ecr 843159493], length 0
03:09:05.002094 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 127.0.0.1.36828 > 127.0.0.1.9557: Flags [.], ack 1, win 6160, options [nop,nop,TS val 843174496 ecr 843159493], length 0
2 packets captured
116 packets received by filter
0 packets dropped by kernel
```
Wireshark shows a similar result as the tcpdump example above.
Linux's loopback setup: https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/drivers/net/loopback.c#L162
PiperOrigin-RevId: 391836719
2021-08-19 20:51:40 +00:00
|
|
|
UnspecifiedEthernetAddress,
|
2020-10-09 19:07:02 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Multicast",
|
|
|
|
tcpip.LinkAddress("\x01\x02\x03\x04\x05\x06"),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Unicast",
|
|
|
|
tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if got := IsMulticastEthernetAddress(test.addr); got != test.expected {
|
|
|
|
t.Fatalf("got IsMulticastEthernetAddress = %t, want = %t", got, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:24:48 +00:00
|
|
|
func TestEthernetAddressFromMulticastIPv4Address(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
addr tcpip.Address
|
|
|
|
expectedLinkAddr tcpip.LinkAddress
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "IPv4 Multicast without 24th bit set",
|
|
|
|
addr: "\xe0\x7e\xdc\xba",
|
|
|
|
expectedLinkAddr: "\x01\x00\x5e\x7e\xdc\xba",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IPv4 Multicast with 24th bit set",
|
|
|
|
addr: "\xe0\xfe\xdc\xba",
|
|
|
|
expectedLinkAddr: "\x01\x00\x5e\x7e\xdc\xba",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if got := EthernetAddressFromMulticastIPv4Address(test.addr); got != test.expectedLinkAddr {
|
2020-03-26 00:15:05 +00:00
|
|
|
t.Fatalf("got EthernetAddressFromMulticastIPv4Address(%s) = %s, want = %s", test.addr, got, test.expectedLinkAddr)
|
2020-01-31 21:24:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthernetAddressFromMulticastIPv6Address(t *testing.T) {
|
2021-04-15 22:09:09 +00:00
|
|
|
addr := testutil.MustParse6("ff02:304:506:708:90a:b0c:d0e:f1a")
|
2020-01-31 21:24:48 +00:00
|
|
|
if got, want := EthernetAddressFromMulticastIPv6Address(addr), tcpip.LinkAddress("\x33\x33\x0d\x0e\x0f\x1a"); got != want {
|
|
|
|
t.Fatalf("got EthernetAddressFromMulticastIPv6Address(%s) = %s, want = %s", addr, got, want)
|
|
|
|
}
|
|
|
|
}
|