Documentation
¶
Index ¶
- Constants
- Variables
- func Generate(h Header, payload []byte) []byte
- func Hexdump(b []byte) string
- func UpdateDstAddr(q *Parsed, dst netip.Addr)
- func UpdateSrcAddr(q *Parsed, src netip.Addr)
- type CaptureMeta
- type Header
- type HeaderChecksummer
- type ICMP4Code
- type ICMP4Header
- type ICMP4Type
- type ICMP6Code
- type ICMP6Header
- type ICMP6Type
- type IP4Header
- type IP6Header
- type Parsed
- func (q *Parsed) Buffer() []byte
- func (q *Parsed) Decode(b []byte)
- func (q *Parsed) EchoIDSeq() uint32
- func (q *Parsed) ICMP4Header() ICMP4Header
- func (q *Parsed) ICMP6Header() ICMP6Header
- func (q *Parsed) IP4Header() IP4Header
- func (q *Parsed) IP6Header() IP6Header
- func (q *Parsed) IsEchoRequest() bool
- func (q *Parsed) IsEchoResponse() bool
- func (q *Parsed) IsError() bool
- func (q *Parsed) IsTCPSyn() bool
- func (q *Parsed) Payload() []byte
- func (p *Parsed) String() string
- func (p *Parsed) Transport() []byte
- func (q *Parsed) UDP4Header() UDP4Header
- type ParsedPool
- type Proto
- func (p Proto) String() stringdeprecated
- type TCPFlag
- type UDP4Header
- type UDP6Header
- type Version
Constants ¶
const ( Version4 = 4 Version6 = 6 )
Valid Version values.
const IP4HeaderLength = 20
IP4HeaderLength is the length of an IPv4 header with no IP options.
const IP4SrcAddrOffset = 12
const IP6HeaderLength = 40
IP6HeaderLength is the length of an IPv6 header with no IP options.
const IP6SrcAddrOffset = 9
const MinTCPHeaderSize = 20
Variables ¶
var ( // PreferredNames is the set of protocol names that re produced by // MarshalText, and are the preferred representation. PreferredNames = map[Proto]string{ 51: "ah", DCCP: "dccp", 8: "egp", 50: "esp", 47: "gre", ICMPv4: "icmp", IGMP: "igmp", 9: "igp", 4: "ipv4", ICMPv6: "ipv6-icmp", SCTP: "sctp", TCP: "tcp", UDP: "udp", } // AcceptedNames is the set of protocol names that are accepted by // UnmarshalText. AcceptedNames = map[string]Proto{ "ah": 51, "dccp": DCCP, "egp": 8, "esp": 50, "gre": 47, "icmp": ICMPv4, "icmpv4": ICMPv4, "icmpv6": ICMPv6, "igmp": IGMP, "igp": 9, "ip-in-ip": 4, "ipv4": 4, "ipv6-icmp": ICMPv6, "sctp": SCTP, "tcp": TCP, "tsmp": TSMP, "udp": UDP, } )
Prefer names from https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml unless otherwise noted.
var Pool = ParsedPool{New: func() any { return new(Parsed) }}
Pool holds a pool of Parsed structs for use in filtering.
Functions ¶
func Generate ¶
Generate generates a new packet with the given Header and payload. This function allocates memory, see Header.Marshal for an allocation-free option.
func UpdateDstAddr ¶
UpdateDstAddr updates the destination address in the packet buffer (e.g. during DNAT). It also updates the checksum. Currently (2022-12-10) only TCP/UDP/ICMP is supported. It panics if provided with an address in a different family to the parsed packet.
func UpdateSrcAddr ¶
UpdateSrcAddr updates the source address in the packet buffer (e.g. during SNAT). It also updates the checksum. Currently (2023-09-22) only TCP/UDP/ICMP is supported. It panics if provided with an address in a different family to the parsed packet.
Types ¶
type CaptureMeta ¶
type CaptureMeta struct { DidSNAT bool // SNAT was performed & the address was updated. OriginalSrc netip.AddrPort // The source address before SNAT was performed. DidDNAT bool // DNAT was performed & the address was updated. OriginalDst netip.AddrPort // The destination address before DNAT was performed. }
CaptureMeta contains metadata that is used when debugging.
type Header ¶
type Header interface { // Len returns the length of the marshaled packet. Len() int // Marshal serializes the header into buf, which must be at // least Len() bytes long. Implementations of Marshal assume // that bytes after the first Len() are payload bytes for the // purpose of computing length and checksum fields. Marshal // implementations must not allocate memory. Marshal(buf []byte) error }
Header is a packet header capable of marshaling itself into a byte buffer.
type HeaderChecksummer ¶
type HeaderChecksummer interface { Header // WriteCheck writes the correct checksum into buf, which should // be be the already-marshalled header and payload. WriteChecksum(buf []byte) }
HeaderChecksummer is implemented by Header implementations that need to do a checksum over their payloads.
type ICMP4Code ¶
type ICMP4Code uint8
ICMP4Code is an ICMPv4 code, as specified in https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
const (
ICMP4NoCode ICMP4Code = 0
)
type ICMP4Header ¶
ICMP4Header is an IPv4+ICMPv4 header.
func (ICMP4Header) Marshal ¶
func (h ICMP4Header) Marshal(buf []byte) error
Marshal implements Header.
func (*ICMP4Header) ToResponse ¶
func (h *ICMP4Header) ToResponse()
ToResponse implements Header. TODO: it doesn't implement it correctly, instead it statically generates an ICMP Echo Reply packet.
type ICMP4Type ¶
type ICMP4Type uint8
ICMP4Type is an ICMPv4 type, as specified in https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
type ICMP6Code ¶
type ICMP6Code uint8
ICMP6Code is an ICMPv6 code, as specified in https://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xhtml
const (
ICMP6NoCode ICMP6Code = 0
)
type ICMP6Header ¶
ICMP6Header is an IPv4+ICMPv4 header.
func (ICMP6Header) Marshal ¶
func (h ICMP6Header) Marshal(buf []byte) error
Marshal implements Header.
func (*ICMP6Header) ToResponse ¶
func (h *ICMP6Header) ToResponse()
ToResponse implements Header. TODO: it doesn't implement it correctly, instead it statically generates an ICMP Echo Reply packet.
func (ICMP6Header) WriteChecksum ¶
func (h ICMP6Header) WriteChecksum(p []byte)
WriteChecksum implements HeaderChecksummer, writing just the checksum bytes into the otherwise fully marshaled ICMP6 packet p (which should include the IPv6 header, ICMPv6 header, and payload).
type ICMP6Type ¶
type ICMP6Type uint8
ICMP6Type is an ICMPv6 type, as specified in https://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xhtml
type IP6Header ¶
type IP6Header struct { IPProto Proto IPID uint32 // only lower 20 bits used Src netip.Addr Dst netip.Addr }
IP6Header represents an IPv6 packet header.
type Parsed ¶
type Parsed struct { // IPVersion is the IP protocol version of the packet (4 or // 6), or 0 if the packet doesn't look like IPv4 or IPv6. IPVersion uint8 // IPProto is the IP subprotocol (UDP, TCP, etc.). Valid iff IPVersion != 0. IPProto Proto // Src is the source address. Family matches IPVersion. Port is // valid iff IPProto == TCP || IPProto == UDP || IPProto == SCTP. Src netip.AddrPort // Dst is the destination address. Family matches IPVersion. Port is // valid iff IPProto == TCP || IPProto == UDP || IPProto == SCTP. Dst netip.AddrPort // TCPFlags is the packet's TCP flag bits. Valid iff IPProto == TCP. TCPFlags TCPFlag // CaptureMeta contains metadata that is used when debugging. CaptureMeta CaptureMeta // contains filtered or unexported fields }
Parsed is a minimal decoding of a packet suitable for use in filters.
func (*Parsed) Buffer ¶
Buffer returns the entire packet buffer. This is a read-only view; that is, q retains the ownership of the buffer.
func (*Parsed) Decode ¶
Decode extracts data from the packet in b into q. It performs extremely simple packet decoding for basic IPv4 and IPv6 packet types. It extracts only the subprotocol id, IP addresses, and (if any) ports, and shouldn't need any memory allocation.
func (*Parsed) EchoIDSeq ¶
EchoIDSeq extracts the identifier/sequence bytes from an ICMP Echo response, and returns them as a uint32, used to lookup internally routed ICMP echo responses. This function is intentionally lightweight as it is called on every incoming ICMP packet.
func (*Parsed) ICMP4Header ¶
func (q *Parsed) ICMP4Header() ICMP4Header
func (*Parsed) ICMP6Header ¶
func (q *Parsed) ICMP6Header() ICMP6Header
func (*Parsed) IsEchoRequest ¶
IsEchoRequest reports whether q is an ICMP Echo Request.
func (*Parsed) IsEchoResponse ¶
IsEchoResponse reports whether q is an IPv4 ICMP Echo Response.
func (*Parsed) IsTCPSyn ¶
IsTCPSyn reports whether q is a TCP SYN packet, without ACK set. (i.e. the first packet in a new connection)
func (*Parsed) Payload ¶
Payload returns the payload of the IP subprotocol section. This is a read-only view; that is, q retains the ownership of the buffer.
func (*Parsed) Transport ¶
Transport returns the transport header and payload (IP subprotocol, such as TCP or UDP). This is a read-only view; that is, p retains the ownership of the buffer.
func (*Parsed) UDP4Header ¶
func (q *Parsed) UDP4Header() UDP4Header
type ParsedPool ¶
func (*ParsedPool) Get ¶
func (p *ParsedPool) Get() *Parsed
func (*ParsedPool) Put ¶
func (p *ParsedPool) Put(parsed *Parsed)
type Proto ¶
type Proto uint8
Proto is an IP subprotocol as defined by the IANA protocol numbers list (https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml), or the special values Unknown or Fragment.
const ( // Unknown represents an unknown or unsupported protocol; it's // deliberately the zero value. Strictly speaking the zero // value is IPv6 hop-by-hop extensions, but we don't support // those, so this is still technically correct. UnknownProto Proto = 0x00 // Values from the IANA registry. ICMPv4 Proto = 0x01 IGMP Proto = 0x02 ICMPv6 Proto = 0x3a TCP Proto = 0x06 UDP Proto = 0x11 DCCP Proto = 0x21 GRE Proto = 0x2f SCTP Proto = 0x84 // TSMP is the Tailscale Message Protocol (our ICMP-ish // thing), an IP protocol used only between Tailscale nodes // (still encrypted by WireGuard) that communicates why things // failed, etc. // // Proto number 99 is reserved for "any private encryption // scheme". We never accept these from the host OS stack nor // send them to the host network stack. It's only used between // nodes. TSMP Proto = 99 // Fragment represents any non-first IP fragment, for which we // don't have the sub-protocol header (and therefore can't // figure out what the sub-protocol is). // // 0xFF is reserved in the IANA registry, so we steal it for // internal use. Fragment Proto = 0xFF )
type UDP4Header ¶
UDP4Header is an IPv4+UDP header.
func (UDP4Header) Marshal ¶
func (h UDP4Header) Marshal(buf []byte) error
Marshal implements Header.
type UDP6Header ¶
UDP6Header is an IPv6+UDP header.
func (UDP6Header) Marshal ¶
func (h UDP6Header) Marshal(buf []byte) error
Marshal implements Header.