tunframe

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tunframe 实现零特权、纯内存的 OpenSSH tun@openssh.com L3 线缆帧状态机与 ControlMaster 复用单包帧桥接 (ADR-019 §2.3 A1 / A1-mux)。

本包自动归类为 LayerCoreSSHNetOther,严禁导入 os / net / syscall / x/sys/* / x/crypto/ssh, 由 core/internal/sshnet/forward 与 core/internal/sshnet/mux 共同复用,消除 L3 拆帧状态机重复实现:

  • L3 线缆帧格式:[4B big-endian OpenBSD AF (AF_INET=2 / AF_INET6=24)][完整裸 IPv4/IPv6 包],总长上限 32768B;
  • Mux 单包帧格式:[4B big-endian frameLen][4B big-endian OpenBSD AF][完整裸 IPv4/IPv6 包],长度区间 [4, 32768];
  • RX 拆帧状态机仅在帧边界 0 字节处返回 io.EOF,任何非法 AF、版本、IHL、包长、IPv6 jumbogram、超限或帧内截断 一律立即 fail-closed 返回包装 ports.ErrCorruptData 的结构化错误(绝不包装 io.EOF);
  • BridgeMux 停机时恒先取消内部作用域上下文再关闭双端连接,彻底杜绝正常关闭将另一侧帧中阻塞读取误判为 ErrCorruptData。

Index

Constants

View Source
const (
	OpenSSHAFInet       uint32 = 2
	OpenSSHAFInet6      uint32 = 24
	MinWirePayloadBytes        = 4
	MaxWirePayloadBytes        = 32768
	MaxIPPacketBytes           = MaxWirePayloadBytes - MinWirePayloadBytes
)

OpenSSH tun@openssh.com L3 线缆协议常量(ADR-019 §2.3 A1): 远端线缆帧严格使用 4 字节大端 OpenBSD 地址族号(AF_INET=2, AF_INET6=24), 单帧总负载(4B AF + 完整裸 IPv4/IPv6 包)受 32768 字节硬上限约束。

Variables

This section is empty.

Functions

func BridgeMux

func BridgeMux(ctx context.Context, muxConn, remoteTUN io.ReadWriteCloser) error

BridgeMux 在服务端(mux/server_dispatch.go)将客户端长度前缀 TUN 帧流与上游 OpenTUN 字节流双向逐包桥接:

  • 客户端 -> 上游:逐帧解包并通过 ValidateWireFrame 校验 L3 [4B AF][IP] 后整包写入 remoteTUN;
  • 上游 -> 客户端:以 bufio + ReadWireFrame 严格按 OpenSSH L3 状态机切分完整单包后加长度前缀写回 muxConn, 彻底消除 1–3B 尾分片死锁、跨包粘合、半包切片与静默丢弃;
  • 任一侧帧失步/畸形、EOF 或 ctx 取消时,恒先取消内部上下文再同步关闭双端连接,杜绝正常关停触发误报 ErrCorruptData。

func EncodeWireFrame

func EncodeWireFrame(dst, pkt []byte) ([]byte, string, bool)

EncodeWireFrame 将单个合法裸 IP 包编码为 [4B big-endian OpenBSD AF][IP pkt] 线缆帧; 若 dst 容量足够则原地复用以减少堆分配。

func ReadMuxFrame

func ReadMuxFrame(r io.Reader, scratch []byte) ([]byte, error)

ReadMuxFrame 从流式复用连接读取单个长度前缀 TUN 包帧并校验帧长位于 [MinWirePayloadBytes, MaxWirePayloadBytes] 区间; 仅在帧边界读取到 0 字节时返回 io.EOF,帧头或帧体截断一律 fail-closed 返回包装 ports.ErrCorruptData 的错误(绝不包装 io.EOF)。 优先复用调用方传入的 scratch 缓冲区以减少高频包转发堆分配。

func ReadWireFrame

func ReadWireFrame(r io.Reader, frameBuf []byte) ([]byte, error)

ReadWireFrame 按 OpenSSH tun@openssh.com L3 状态机从字节流读取并校验下一条完整线缆帧, 返回指向 frameBuf 内的完整 [4B big-endian OpenBSD AF][IP pkt] 切片(零额外拷贝;frameBuf 长度须 >= MaxWirePayloadBytes)。 仅当在帧边界读取到 0 字节时返回 io.EOF;任何非法 AF、IP 版本/IHL/长度错误、IPv6 jumbogram、超限或帧内截断 均立即 fail-closed 返回包装 ports.ErrCorruptData 的错误(绝不包装 io.EOF)。

func ValidateLocalIPPacket

func ValidateLocalIPPacket(pkt []byte) (af uint32, reason string, ok bool)

ValidateLocalIPPacket 严格校验单个完整裸 IPv4/IPv6 包并求得对应 OpenBSD 地址族号。 校验规则:

  • IPv4(ver==4):len>=20、ihl>=5、totalLen>=ihl*4 且 totalLen==len(pkt);
  • IPv6(ver==6):len>=40、严禁 payloadLen==0(拒绝 jumbogram)且 40+payloadLen==len(pkt);
  • 总帧长 4+len(pkt) <= MaxWirePayloadBytes (32768)。

func ValidateWireFrame

func ValidateWireFrame(frame []byte) error

ValidateWireFrame 严格校验已组装的单条 [4B big-endian OpenBSD AF][IP pkt] 线缆帧: 必须同时满足总长位于 [4, 32768]、AF 为 2 或 24、内层裸 IP 包合法且与 AF 精确一致, 否则立即 fail-closed 返回包装 ports.ErrCorruptData 的错误。

func WriteFull

func WriteFull(w io.Writer, data []byte) error

WriteFull 优先在单次 Write 中写全整个切片;仅当底层流返回非零短写且无错时继续补写剩余字节, 遇 0 字节或越界短写则立即 fail-closed 返回 io.ErrShortWrite。

func WriteMuxFrame

func WriteMuxFrame(w io.Writer, pkt []byte) error

WriteMuxFrame 按 [4B big-endian frameLen][4B big-endian OpenBSD AF][IP pkt] 线缆格式原子写出单个复用 TUN 包帧, 严格校验帧长位于 [MinWirePayloadBytes, MaxWirePayloadBytes]([4, 32768])区间,防止空包、截断头或超限包破坏流式复用帧边界。

Types

type FramedConn

type FramedConn struct {
	// contains filtered or unexported fields
}

FramedConn 在客户端将 XOpsCOpenTUN 的长度前缀流封装为满足 [4B AF][IP pkt] 单包边界契约的 io.ReadWriteCloser; 每次 Write 原子写入单帧,每次 Read 在目标缓冲足够时精确交付单个完整包(绝不跨包粘合), 当调用方以分段 io.ReadFull 消费单包头/体时亦安全缓冲余量;零值恒失败关闭。

func NewFramedConn

func NewFramedConn(conn io.ReadWriteCloser) (*FramedConn, error)

NewFramedConn 构造基于已协商 XOpsCOpenTUN 底层流的单包帧隧道连接。

func (*FramedConn) Close

func (c *FramedConn) Close() error

Close 幂等关闭底层复用 TUN 连接。

func (*FramedConn) Read

func (c *FramedConn) Read(dst []byte) (int, error)

Read 读取单个 [4B AF][IP pkt] 包帧:当 dst 足以容纳整包时精确返回单包不跨帧粘合, 当 dst 小于单包时保留包尾余量供后续分段读取。

func (*FramedConn) Write

func (c *FramedConn) Write(p []byte) (int, error)

Write 将单个 [4B AF][IP pkt] 包作为独立长度前缀帧原子写入复用连接。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL