Files
Reims-Go/internal/packetbridge/frame_test.go
T

276 lines
9.7 KiB
Go

package packetbridge
import (
"bytes"
"encoding/binary"
"net"
"testing"
)
func mustMAC(t *testing.T, s string) net.HardwareAddr {
t.Helper()
m, err := net.ParseMAC(s)
if err != nil {
t.Fatal(err)
}
return m
}
func buildARPFrame(dstMAC, srcMAC, sha []byte, spa []byte, tha []byte, tpa []byte, op uint16) []byte {
f := make([]byte, 42)
copy(f[0:6], dstMAC)
copy(f[6:12], srcMAC)
binary.BigEndian.PutUint16(f[12:14], 0x0806)
binary.BigEndian.PutUint16(f[14:16], 1) // Ethernet
binary.BigEndian.PutUint16(f[16:18], 0x0800) // IPv4
f[18], f[19] = 6, 4
binary.BigEndian.PutUint16(f[20:22], op)
copy(f[22:28], sha)
copy(f[28:32], spa)
copy(f[32:38], tha)
copy(f[38:42], tpa)
return f
}
func buildDHCPFrame(dstMAC, srcMAC []byte, srcPort, dstPort uint16, chaddr []byte, yiaddr []byte, flags uint16) []byte {
bootp := make([]byte, 248)
if srcPort == 68 {
bootp[0] = 1
} else {
bootp[0] = 2
}
bootp[1], bootp[2] = 1, 6
binary.BigEndian.PutUint16(bootp[10:12], flags)
if len(yiaddr) == 4 {
copy(bootp[16:20], yiaddr)
}
copy(bootp[28:34], chaddr)
binary.BigEndian.PutUint32(bootp[236:240], 0x63825363)
udpLen := 8 + len(bootp)
udp := make([]byte, udpLen)
binary.BigEndian.PutUint16(udp[0:2], srcPort)
binary.BigEndian.PutUint16(udp[2:4], dstPort)
binary.BigEndian.PutUint16(udp[4:6], uint16(udpLen))
binary.BigEndian.PutUint16(udp[6:8], 0xabcd)
copy(udp[8:], bootp)
ipLen := 20 + len(udp)
ip := make([]byte, ipLen)
ip[0] = 0x45
binary.BigEndian.PutUint16(ip[2:4], uint16(ipLen))
ip[8], ip[9] = 64, 17
copy(ip[12:16], []byte{192, 168, 1, 50})
copy(ip[16:20], []byte{255, 255, 255, 255})
copy(ip[20:], udp)
f := make([]byte, 14+len(ip))
copy(f[0:6], dstMAC)
copy(f[6:12], srcMAC)
binary.BigEndian.PutUint16(f[12:14], 0x0800)
copy(f[14:], ip)
return f
}
func buildNDPFrame(dstMAC, srcMAC []byte, srcIP, dstIP, targetIP net.IP, icmpType, optType byte, optMAC []byte) []byte {
icmpLen := 24
if len(optMAC) == 6 {
icmpLen += 8
}
icmp := make([]byte, icmpLen)
icmp[0] = icmpType
copy(icmp[8:24], targetIP.To16())
if len(optMAC) == 6 {
icmp[24] = optType
icmp[25] = 1
copy(icmp[26:32], optMAC)
}
f := make([]byte, 54+icmpLen)
copy(f[0:6], dstMAC)
copy(f[6:12], srcMAC)
binary.BigEndian.PutUint16(f[12:14], 0x86dd)
f[14] = 0x60
binary.BigEndian.PutUint16(f[18:20], uint16(icmpLen))
f[20] = 58
f[21] = 255
copy(f[22:38], srcIP.To16())
copy(f[38:54], dstIP.To16())
copy(f[54:], icmp)
// Compute initial valid ICMPv6 checksum
pseudo := append([]byte(nil), f[22:54]...)
pseudo = append(pseudo, 0, 0, byte(icmpLen>>8), byte(icmpLen), 0, 0, 0, 58)
pseudo = append(pseudo, f[54:]...)
binary.BigEndian.PutUint16(f[56:58], frameSum(pseudo))
return f
}
func TestTranslatorWiredL2PassThrough(t *testing.T) {
guest := mustMAC(t, "52:54:00:11:22:33")
host := mustMAC(t, "02:00:00:00:00:f0")
peer := mustMAC(t, "bc:24:11:50:e3:da")
bcast := mustMAC(t, "ff:ff:ff:ff:ff:ff")
tr := newTranslator(guest, host, false)
// Outbound DHCP preserves guest MAC and does NOT force broadcast flag.
dhcp := buildDHCPFrame(bcast, guest, 68, 67, guest, nil, 0x0000)
out, ok := tr.outbound(dhcp)
if !ok {
t.Fatal("wired outbound DHCP dropped")
}
if !bytes.Equal(out[6:12], guest) {
t.Fatalf("wired outbound rewrote src MAC: got %x want %x", out[6:12], guest)
}
if binary.BigEndian.Uint16(out[14+20+8+10:14+20+8+12]) != 0x0000 {
t.Fatal("wired outbound unexpectedly forced DHCP broadcast flag")
}
// Outbound ARP preserves guest SHA.
arp := buildARPFrame(bcast, guest, guest, []byte{172, 18, 1, 207}, make([]byte, 6), []byte{172, 18, 1, 1}, 1)
outARP, ok := tr.outbound(arp)
if !ok || !bytes.Equal(outARP[22:28], guest) {
t.Fatalf("wired outbound ARP altered SHA or dropped: ok=%v", ok)
}
// Inbound unicast addressed to guest's real MAC passes untouched; other unicast is dropped.
inGuest := buildARPFrame(guest, peer, peer, []byte{172, 18, 1, 1}, guest, []byte{172, 18, 1, 207}, 2)
gotIn, ok := tr.inbound(inGuest)
if !ok || !bytes.Equal(gotIn[:6], guest) {
t.Fatalf("wired inbound unicast to guest MAC failed: ok=%v", ok)
}
inOther := buildARPFrame(host, peer, peer, []byte{172, 18, 1, 1}, host, []byte{172, 18, 1, 186}, 2)
if _, ok := tr.inbound(inOther); ok {
t.Fatal("wired inbound leaked host unicast frame to guest")
}
// Inbound loopback of guest's own TX is dropped.
if _, ok := tr.inbound(arp); ok {
t.Fatal("wired inbound looped back guest's own TX frame")
}
// Inbound IPv6 solicited-node multicast passes through.
nsDst := []byte{0x33, 0x33, 0xff, 0x04, 0x83, 0x7c}
inNS := buildNDPFrame(nsDst, peer, net.ParseIP("fe80::1"), net.ParseIP("ff02::1:ff04:837c"), net.ParseIP("2409::837c"), 135, 1, peer)
if _, ok := tr.inbound(inNS); !ok {
t.Fatal("wired inbound dropped IPv6 solicited-node multicast NS")
}
}
func TestTranslatorWLAN_ARP_DHCP_Multicast_And_NDPTargetLearn(t *testing.T) {
guest := mustMAC(t, "02:00:00:00:00:01")
host := mustMAC(t, "3c:f0:11:0b:0f:0e")
peer := mustMAC(t, "bc:24:11:50:e3:da")
bcast := mustMAC(t, "ff:ff:ff:ff:ff:ff")
tr := newTranslator(guest, host, true)
// 1. Outbound DHCP sets broadcast flag & rewrites src MAC to host, keeping chaddr=guest.
dhcpReq := buildDHCPFrame(bcast, guest, 68, 67, guest, nil, 0x0000)
outDHCP, ok := tr.outbound(dhcpReq)
if !ok {
t.Fatal("WLAN outbound DHCP dropped")
}
if !bytes.Equal(outDHCP[6:12], host) {
t.Fatalf("WLAN outbound src MAC = %x want %x", outDHCP[6:12], host)
}
if outDHCP[14+20+8+10]&0x80 == 0 {
t.Fatal("WLAN outbound did not set DHCP broadcast flag")
}
if outDHCP[14+20+6] != 0 || outDHCP[14+20+7] != 0 {
t.Fatal("WLAN outbound did not zero UDP checksum after modifying DHCP")
}
// 2. Inbound DHCP reply matching guest chaddr learns yiaddr; mismatched chaddr is dropped.
dhcpReply := buildDHCPFrame(bcast, peer, 67, 68, guest, []byte{172, 18, 1, 207}, 0x8000)
if _, ok := tr.inbound(dhcpReply); !ok || !tr.owns([]byte{172, 18, 1, 207}) {
t.Fatal("WLAN inbound DHCP reply did not deliver/learn guest IP")
}
otherDHCP := buildDHCPFrame(bcast, peer, 67, 68, peer, []byte{172, 18, 1, 99}, 0x8000)
if _, ok := tr.inbound(otherDHCP); ok {
t.Fatal("WLAN inbound delivered another host's DHCP reply")
}
// 3. Inbound unicast ARP reply for learned IP rewrites dst MAC and THA from host -> guest.
arpReply := buildARPFrame(host, peer, peer, []byte{172, 18, 1, 1}, host, []byte{172, 18, 1, 207}, 2)
gotARP, ok := tr.inbound(arpReply)
if !ok {
t.Fatal("WLAN inbound ARP reply dropped")
}
if !bytes.Equal(gotARP[:6], guest) || !bytes.Equal(gotARP[32:38], guest) {
t.Fatalf("WLAN inbound ARP did not restore guest dst/THA: dst=%x tha=%x", gotARP[:6], gotARP[32:38])
}
// 4. AP-unicasted IPv4 multicast frame restores 01:00:5e:xx:yy:zz multicast MAC.
mcastIPv4 := make([]byte, 34)
copy(mcastIPv4[0:6], host) // AP converted multicast dst to host unicast
copy(mcastIPv4[6:12], peer)
binary.BigEndian.PutUint16(mcastIPv4[12:14], 0x0800)
mcastIPv4[14] = 0x45
copy(mcastIPv4[26:30], []byte{172, 18, 1, 1})
copy(mcastIPv4[30:34], []byte{224, 0, 0, 251}) // mDNS
gotMcast, ok := tr.inbound(mcastIPv4)
if !ok || !bytes.Equal(gotMcast[:6], []byte{0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb}) {
t.Fatalf("WLAN inbound did not restore IPv4 multicast MAC: ok=%v dst=%x", ok, gotMcast[:6])
}
// 5. Real-VM bug regression: macOS SLAAC DAD NS (src=::) and NA (src=fe80::..., target=2409:...:837c)
// MUST learn the NDP Target Address AND rewrite NDP link-layer option with a valid ICMPv6 checksum!
securedGUA := net.ParseIP("2409:8a55:1476:c7e1:f2:74f0:ba04:837c")
linkLocal := net.ParseIP("fe80::c38:4e7c:d13f:f8e5")
peerGUA := net.ParseIP("2409:8a55:1476:c7e1:e111:46ea:dfe8:7a8d")
// 5a. Outbound NA with src=fe80::..., target=securedGUA, Target Link-Layer Option=guest
na := buildNDPFrame(peer, guest, linkLocal, peerGUA, securedGUA, 136, 2, guest)
outNA, ok := tr.outbound(na)
if !ok {
t.Fatal("WLAN outbound NA dropped")
}
if !bytes.Equal(outNA[54+26:54+32], host) {
t.Fatalf("rewriteNDP did not rewrite TLLA from guest to host: got %x", outNA[54+26:54+32])
}
// Verify ICMPv6 checksum over rewritten NA equals 0 (valid!)
icmpLen := int(binary.BigEndian.Uint16(outNA[18:20]))
pseudo := append([]byte(nil), outNA[22:54]...)
pseudo = append(pseudo, 0, 0, byte(icmpLen>>8), byte(icmpLen), 0, 0, 0, 58)
pseudo = append(pseudo, outNA[54:54+icmpLen]...)
if sum := frameSum(pseudo); sum != 0 {
t.Fatalf("rewritten NDP checksum invalid: residual=%#x", sum)
}
if !tr.owns(securedGUA.To16()) {
t.Fatal("WLAN outbound NA failed to learn NDP target secured GUA")
}
// 5b. Unicast IPv6 packet inbound to securedGUA MUST now be delivered to guest!
inV6 := make([]byte, 64)
copy(inV6[0:6], host)
copy(inV6[6:12], peer)
binary.BigEndian.PutUint16(inV6[12:14], 0x86dd)
inV6[14] = 0x60
binary.BigEndian.PutUint16(inV6[18:20], 10)
inV6[20] = 58
copy(inV6[22:38], peerGUA.To16())
copy(inV6[38:54], securedGUA.To16())
gotV6, ok := tr.inbound(inV6)
if !ok || !bytes.Equal(gotV6[:6], guest) {
t.Fatalf("WLAN inbound IPv6 unicast to secured GUA failed after NA: ok=%v", ok)
}
// 5c. Ordinary outbound NS (non-zero src) MUST NOT learn the remote peer's target IP!
tr2 := newTranslator(guest, host, true)
ordinaryNS := buildNDPFrame([]byte{0x33, 0x33, 0xff, 0xe8, 0x7a, 0x8d}, guest, linkLocal, net.ParseIP("ff02::1:ffe8:7a8d"), peerGUA, 135, 1, guest)
if _, ok := tr2.outbound(ordinaryNS); !ok {
t.Fatal("ordinary NS dropped")
}
if tr2.owns(peerGUA.To16()) {
t.Fatal("ordinary NS erroneously learned remote peer IP as guest's own IP")
}
// DAD NS (src == ::) MUST learn the target IP!
dadNS := buildNDPFrame([]byte{0x33, 0x33, 0xff, 0x04, 0x83, 0x7c}, guest, net.IPv6unspecified, net.ParseIP("ff02::1:ff04:837c"), securedGUA, 135, 0, nil)
if _, ok := tr2.outbound(dadNS); !ok {
t.Fatal("DAD NS dropped")
}
if !tr2.owns(securedGUA.To16()) {
t.Fatal("DAD NS failed to learn guest IPv6 address")
}
}