273 lines
7.0 KiB
Go
273 lines
7.0 KiB
Go
package packetbridge
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/binary"
|
||
"net"
|
||
"sync"
|
||
)
|
||
|
||
const maxLearnedAddresses = 256
|
||
|
||
type translator struct {
|
||
guest, host net.HardwareAddr
|
||
translateMAC bool
|
||
mu sync.Mutex
|
||
addresses map[string]bool
|
||
order []string
|
||
}
|
||
|
||
func newTranslator(guest, host net.HardwareAddr, translateMAC bool) *translator {
|
||
return &translator{
|
||
guest: append(net.HardwareAddr(nil), guest...),
|
||
host: append(net.HardwareAddr(nil), host...),
|
||
translateMAC: translateMAC,
|
||
addresses: map[string]bool{},
|
||
}
|
||
}
|
||
|
||
func (t *translator) learn(ip []byte) {
|
||
address := net.IP(ip)
|
||
if len(address) == 0 || address.IsUnspecified() || address.IsMulticast() {
|
||
return
|
||
}
|
||
key := address.String()
|
||
t.mu.Lock()
|
||
defer t.mu.Unlock()
|
||
if t.addresses[key] {
|
||
return
|
||
}
|
||
if len(t.order) >= maxLearnedAddresses {
|
||
evict := t.order[0]
|
||
t.order = t.order[1:]
|
||
delete(t.addresses, evict)
|
||
}
|
||
t.addresses[key] = true
|
||
t.order = append(t.order, key)
|
||
}
|
||
|
||
func (t *translator) owns(ip []byte) bool {
|
||
t.mu.Lock()
|
||
defer t.mu.Unlock()
|
||
return t.addresses[net.IP(ip).String()]
|
||
}
|
||
|
||
func frameSum(data []byte) uint16 {
|
||
var sum uint32
|
||
for len(data) >= 2 {
|
||
sum += uint32(binary.BigEndian.Uint16(data))
|
||
data = data[2:]
|
||
}
|
||
if len(data) > 0 {
|
||
sum += uint32(data[0]) << 8
|
||
}
|
||
for sum > 65535 {
|
||
sum = (sum & 65535) + (sum >> 16)
|
||
}
|
||
return ^uint16(sum)
|
||
}
|
||
|
||
func validFrameType(frame []byte) bool {
|
||
if len(frame) < 14 {
|
||
return false
|
||
}
|
||
switch binary.BigEndian.Uint16(frame[12:14]) {
|
||
case 0x0806:
|
||
return len(frame) >= 42 && frame[18] == 6 && frame[19] == 4
|
||
case 0x0800:
|
||
return len(frame) >= 34 && frame[14]>>4 == 4
|
||
case 0x86dd:
|
||
return len(frame) >= 54 && frame[14]>>4 == 6
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
// outbound 处理客体发往物理网卡的以太网帧:
|
||
// - 有线出口(translateMAC == false):保留客体真实 MAC 做标准二层桥接,不改 DHCP 标志或 ARP/NDP。
|
||
// - 无线出口(translateMAC == true):转换为宿主 MAC 以兼容普通 Wi-Fi AP,同时保留客体独立 IP 与 DHCP 身份。
|
||
func (t *translator) outbound(frame []byte) ([]byte, bool) {
|
||
if len(frame) < 14 || !bytes.Equal(frame[6:12], t.guest) {
|
||
return nil, false
|
||
}
|
||
if !t.translateMAC {
|
||
if !validFrameType(frame) {
|
||
return nil, false
|
||
}
|
||
return append([]byte(nil), frame...), true
|
||
}
|
||
f := append([]byte(nil), frame...)
|
||
copy(f[6:12], t.host)
|
||
switch binary.BigEndian.Uint16(f[12:14]) {
|
||
case 0x0806:
|
||
if len(f) < 42 || f[18] != 6 || f[19] != 4 {
|
||
return nil, false
|
||
}
|
||
copy(f[22:28], t.host)
|
||
t.learn(f[28:32])
|
||
case 0x0800:
|
||
if len(f) < 34 || f[14]>>4 != 4 {
|
||
return nil, false
|
||
}
|
||
t.learn(f[26:30])
|
||
start := 14 + int(f[14]&15)*4
|
||
if f[23] == 17 && start >= 34 && len(f) >= start+8+240 && binary.BigEndian.Uint16(f[start:start+2]) == 68 && binary.BigEndian.Uint16(f[start+2:start+4]) == 67 {
|
||
p := f[start+8:]
|
||
if binary.BigEndian.Uint32(p[236:240]) == 0x63825363 {
|
||
p[10] |= 0x80
|
||
f[start+6], f[start+7] = 0, 0
|
||
}
|
||
}
|
||
case 0x86dd:
|
||
if len(f) < 54 || f[14]>>4 != 6 {
|
||
return nil, false
|
||
}
|
||
t.learn(f[22:38])
|
||
// macOS SLAAC 的稳定地址在 DAD NS (src=::) 与 NA (src=fe80::...) 中仅出现在 NDP Target 字段,
|
||
// 必须一并学习,否则局域网主动发往客体稳定 IPv6 地址的入站单播会被误丢。
|
||
if target := ndpTargetAddress(f); target != nil {
|
||
t.learn(target)
|
||
}
|
||
rewriteNDP(f, t.guest, t.host)
|
||
default:
|
||
return nil, false
|
||
}
|
||
return f, true
|
||
}
|
||
|
||
func (t *translator) inbound(frame []byte) ([]byte, bool) {
|
||
if len(frame) < 14 {
|
||
return nil, false
|
||
}
|
||
if !t.translateMAC {
|
||
// 过滤本机刚发出的客体帧,防止混杂模式下 Npcap 回环重放。
|
||
if bytes.Equal(frame[6:12], t.guest) {
|
||
return nil, false
|
||
}
|
||
if !validFrameType(frame) {
|
||
return nil, false
|
||
}
|
||
if frame[0]&1 == 0 && !bytes.Equal(frame[:6], t.guest) {
|
||
return nil, false
|
||
}
|
||
return append([]byte(nil), frame...), true
|
||
}
|
||
multicast := frame[0]&1 != 0
|
||
var deliver bool
|
||
var multicastMAC net.HardwareAddr
|
||
switch binary.BigEndian.Uint16(frame[12:14]) {
|
||
case 0x0806:
|
||
if len(frame) < 42 {
|
||
return nil, false
|
||
}
|
||
deliver = multicast || t.owns(frame[38:42])
|
||
case 0x0800:
|
||
if len(frame) < 34 {
|
||
return nil, false
|
||
}
|
||
if net.IP(frame[30:34]).IsMulticast() {
|
||
multicastMAC = net.HardwareAddr{1, 0, 0x5e, frame[31] & 0x7f, frame[32], frame[33]}
|
||
}
|
||
deliver = multicast || multicastMAC != nil || t.owns(frame[30:34])
|
||
start := 14 + int(frame[14]&15)*4
|
||
if frame[23] == 17 && start >= 34 && len(frame) >= start+248 && binary.BigEndian.Uint16(frame[start:start+2]) == 67 && binary.BigEndian.Uint16(frame[start+2:start+4]) == 68 {
|
||
p := frame[start+8:]
|
||
if binary.BigEndian.Uint32(p[236:240]) == 0x63825363 {
|
||
if !bytes.Equal(p[28:34], t.guest) {
|
||
return nil, false
|
||
}
|
||
t.learn(p[16:20])
|
||
deliver = true
|
||
}
|
||
}
|
||
case 0x86dd:
|
||
if len(frame) < 54 {
|
||
return nil, false
|
||
}
|
||
if net.IP(frame[38:54]).IsMulticast() {
|
||
multicastMAC = append(net.HardwareAddr{0x33, 0x33}, frame[50:54]...)
|
||
}
|
||
deliver = multicast || multicastMAC != nil || t.owns(frame[38:54])
|
||
default:
|
||
return nil, false
|
||
}
|
||
if !deliver {
|
||
return nil, false
|
||
}
|
||
f := append([]byte(nil), frame...)
|
||
// 无线 AP 可能将 IP 组播封装为发往宿主的单播,交付客体前恢复组播地址。
|
||
if multicastMAC != nil {
|
||
copy(f[:6], multicastMAC)
|
||
} else if !multicast {
|
||
copy(f[:6], t.guest)
|
||
}
|
||
if binary.BigEndian.Uint16(f[12:14]) == 0x0806 && bytes.Equal(f[32:38], t.host) {
|
||
copy(f[32:38], t.guest)
|
||
}
|
||
return f, true
|
||
}
|
||
|
||
// ndpTargetAddress 提取客体发出的 DAD NS (type 135, src=::) 或 NA (type 136) 的 Target Address。
|
||
func ndpTargetAddress(frame []byte) []byte {
|
||
if len(frame) < 78 || frame[20] != 58 {
|
||
return nil
|
||
}
|
||
length := int(binary.BigEndian.Uint16(frame[18:20]))
|
||
if length < 24 || 54+length > len(frame) {
|
||
return nil
|
||
}
|
||
icmp := frame[54 : 54+length]
|
||
switch icmp[0] {
|
||
case 135:
|
||
// 仅在 DAD(src == ::)时将 Target 视为本机地址;普通 NS 的 Target 是对端地址,不可误学!
|
||
if net.IP(frame[22:38]).IsUnspecified() {
|
||
return icmp[8:24]
|
||
}
|
||
case 136:
|
||
// NA 的 Target Address 是客体声明自己拥有的地址。
|
||
return icmp[8:24]
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func rewriteNDP(frame []byte, from, to []byte) {
|
||
if len(frame) < 62 || frame[20] != 58 {
|
||
return
|
||
}
|
||
length := int(binary.BigEndian.Uint16(frame[18:20]))
|
||
if length < 8 || 54+length > len(frame) {
|
||
return
|
||
}
|
||
icmp := frame[54 : 54+length]
|
||
start := 0
|
||
switch icmp[0] {
|
||
case 133:
|
||
start = 8
|
||
case 134:
|
||
start = 16
|
||
case 135, 136:
|
||
start = 24
|
||
default:
|
||
return
|
||
}
|
||
changed := false
|
||
for p := start; p+2 <= len(icmp); {
|
||
n := int(icmp[p+1]) * 8
|
||
if n == 0 || p+n > len(icmp) {
|
||
return
|
||
}
|
||
if (icmp[p] == 1 || icmp[p] == 2) && n == 8 && bytes.Equal(icmp[p+2:p+8], from) {
|
||
copy(icmp[p+2:p+8], to)
|
||
changed = true
|
||
}
|
||
p += n
|
||
}
|
||
if changed {
|
||
icmp[2], icmp[3] = 0, 0
|
||
pseudo := append([]byte(nil), frame[22:54]...)
|
||
pseudo = append(pseudo, byte(length>>24), byte(length>>16), byte(length>>8), byte(length), 0, 0, 0, 58)
|
||
pseudo = append(pseudo, icmp...)
|
||
binary.BigEndian.PutUint16(icmp[2:4], frameSum(pseudo))
|
||
}
|
||
}
|