Files
Reims-Go/internal/packetbridge/host_offload.go
root 403e2aeac0 fix(network): secure Npcap channels and enable bidirectional host access
Require AF_UNIX for bridge and management channels and reject legacy TCP restore paths before opening resources. Add per-adapter host delivery, Npcap receive injection, and checksum and segmentation handling for Ethernet and WLAN IPv4/IPv6 traffic.

Validation: full CI, targeted packetbridge/app tests, two live macOS VMs over Ethernet and WLAN, and Windows Firewall allow/block/default-deny checks with SMB 3.02 negotiation. Authenticated SMB file access and physical-display suspend/resume remain unverified.
2026-12-02 16:28:46 +08:00

247 lines
6.1 KiB
Go

package packetbridge
import (
"encoding/binary"
"sync/atomic"
)
// IPv6 的 65535 字节负载还需要网络头和以太网头,捕获上限不能只按 64 KiB 计算。
const maxHostCaptureSize = 14 + 40 + 65535
type hostIPPacket struct {
version byte
protocol byte
header int
end int
nextOffset int
fragmented bool
}
func parseHostIP(ip []byte) (p hostIPPacket, ok bool) {
if len(ip) < 20 {
return p, false
}
p.version = ip[0] >> 4
switch p.version {
case 4:
p.header, p.protocol = int(ip[0]&15)*4, ip[9]
p.end = int(binary.BigEndian.Uint16(ip[2:4]))
if p.end == 0 && p.protocol == 6 {
// LSO 报文的总长度可能留给网卡填写。
p.end = len(ip)
}
p.fragmented = binary.BigEndian.Uint16(ip[6:8])&0x3fff != 0
return p, p.header >= 20 && p.end >= p.header && p.end <= len(ip)
case 6:
if len(ip) < 40 {
return p, false
}
p.header, p.protocol, p.nextOffset = 40, ip[6], 6
p.end = 40 + int(binary.BigEndian.Uint16(ip[4:6]))
if p.end == 40 && p.protocol == 6 {
p.end = len(ip)
}
if p.end > len(ip) {
return p, false
}
for count := 0; count < 8; count++ {
switch p.protocol {
case 0, 43, 60:
if p.header+8 > p.end {
return p, false
}
n := (int(ip[p.header+1]) + 1) * 8
if p.header+n > p.end {
return p, false
}
p.nextOffset, p.protocol, p.header = p.header, ip[p.header], p.header+n
case 44:
p.fragmented = true
return p, p.header+8 <= p.end
default:
return p, p.header <= p.end
}
}
}
return p, false
}
// normalizeHostFrames 补齐宿主发送卸载尚未计算的校验和,并在软件中完成 TCP 大包分段。
// 不关闭网卡卸载,也不修改宿主网络设置。
func normalizeHostFrames(frame []byte, mtu int) [][]byte {
if !validFrameType(frame) {
return nil
}
if binary.BigEndian.Uint16(frame[12:14]) == 0x0806 {
if !validARP(frame) {
return nil
}
return [][]byte{frame}
}
p, ok := parseHostIP(frame[14:])
if !ok {
return nil
}
frame = append([]byte(nil), frame[:14+p.end]...)
if mtu < 68 {
mtu = 1500
}
if p.fragmented {
if p.end > mtu {
return nil
}
fixHostIPChecksum(frame[14:], p)
return [][]byte{frame}
}
transport := frame[14+p.header:]
if p.protocol == 6 {
if len(transport) < 20 {
return nil
}
header := int(transport[12]>>4) * 4
if header < 20 || header > len(transport) {
return nil
}
if p.end > mtu {
payloadSize := mtu - p.header - header
if payloadSize <= 0 || transport[13]&0x26 != 0 {
return nil
}
seq := binary.BigEndian.Uint32(transport[4:8])
payload := transport[header:]
var packets [][]byte
for offset := 0; offset < len(payload); offset += payloadSize {
end := min(offset+payloadSize, len(payload))
f := append([]byte(nil), frame[:14+p.header+header]...)
f = append(f, payload[offset:end]...)
seg := p
seg.end = len(f) - 14
if p.version == 4 {
binary.BigEndian.PutUint16(f[18:20], binary.BigEndian.Uint16(frame[18:20])+uint16(len(packets)))
}
tcp := f[14+p.header:]
binary.BigEndian.PutUint32(tcp[4:8], seq+uint32(offset))
if end < len(payload) {
tcp[13] &^= 0x09 // FIN 和 PSH 只保留在最后一段。
}
if offset != 0 {
tcp[13] &^= 0x80 // CWR 只保留在第一段。
}
if !fixHostTransport(f[14:], seg) {
return nil
}
packets = append(packets, f)
}
return packets
}
}
if !fixHostTransport(frame[14:], p) {
return nil
}
if p.end <= mtu {
return [][]byte{frame}
}
return fragmentHostPacket(frame, p, mtu)
}
func fixHostIPChecksum(ip []byte, p hostIPPacket) {
if p.version == 4 {
binary.BigEndian.PutUint16(ip[2:4], uint16(p.end))
ip[10], ip[11] = 0, 0
binary.BigEndian.PutUint16(ip[10:12], frameSum(ip[:p.header]))
} else {
binary.BigEndian.PutUint16(ip[4:6], uint16(p.end-40))
}
}
func fixHostTransport(ip []byte, p hostIPPacket) bool {
data := ip[p.header:p.end]
var checksum, minimum int
switch p.protocol {
case 6:
checksum, minimum = 16, 20
case 17:
checksum, minimum = 6, 8
case 1, 58:
checksum, minimum = 2, 4
default:
fixHostIPChecksum(ip, p)
return true
}
if len(data) < minimum {
return false
}
if p.protocol == 17 {
binary.BigEndian.PutUint16(data[4:6], uint16(len(data)))
}
data[checksum], data[checksum+1] = 0, 0
var pseudo []byte
if p.version == 4 && p.protocol != 1 {
pseudo = append(pseudo, ip[12:20]...)
pseudo = append(pseudo, 0, p.protocol, byte(len(data)>>8), byte(len(data)))
} else if p.version == 6 {
pseudo = append(pseudo, ip[8:40]...)
pseudo = append(pseudo, 0, 0, byte(len(data)>>8), byte(len(data)), 0, 0, 0, p.protocol)
}
pseudo = append(pseudo, data...)
sum := frameSum(pseudo)
if p.protocol == 17 && sum == 0 {
sum = 0xffff
}
binary.BigEndian.PutUint16(data[checksum:checksum+2], sum)
fixHostIPChecksum(ip, p)
return true
}
var hostFragmentID atomic.Uint32
func fragmentHostPacket(frame []byte, p hostIPPacket, mtu int) [][]byte {
header, extra := p.header, 0
if p.version == 4 {
if binary.BigEndian.Uint16(frame[20:22])&0x4000 != 0 {
return nil
}
// 带 IP 选项的分片涉及选项复制语义,不生成含错误选项的分片。
if header != 20 {
return nil
}
} else {
extra = 8
}
size := (mtu - header - extra) &^ 7
if size <= 0 {
return nil
}
payload := frame[14+header:]
id := hostFragmentID.Add(1)
var packets [][]byte
for offset := 0; offset < len(payload); offset += size {
end := min(offset+size, len(payload))
f := append([]byte(nil), frame[:14+header]...)
if p.version == 6 {
f[14+p.nextOffset] = 44
fragment := make([]byte, 8)
fragment[0] = p.protocol
flags := uint16(offset)
if end < len(payload) {
flags |= 1
}
binary.BigEndian.PutUint16(fragment[2:4], flags)
binary.BigEndian.PutUint32(fragment[4:8], id)
f = append(f, fragment...)
} else {
flags := uint16(offset / 8)
if end < len(payload) {
flags |= 0x2000
}
binary.BigEndian.PutUint16(f[20:22], flags)
}
f = append(f, payload[offset:end]...)
seg := p
seg.end = len(f) - 14
fixHostIPChecksum(f[14:], seg)
packets = append(packets, f)
}
return packets
}