2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-05-25 15:57:04 +00:00
|
|
|
|
|
|
|
"tailscale.com/types/strbuilder"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RFC1858: prevent overlapping fragment attacks.
|
2020-05-25 15:57:04 +00:00
|
|
|
const minFrag = 60 + 20 // max IPv4 header + basic TCP header
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
const (
|
2020-05-25 15:57:04 +00:00
|
|
|
TCPSyn = 0x02
|
|
|
|
TCPAck = 0x10
|
|
|
|
TCPSynAck = TCPSyn | TCPAck
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
var (
|
|
|
|
get16 = binary.BigEndian.Uint16
|
|
|
|
get32 = binary.BigEndian.Uint32
|
|
|
|
|
|
|
|
put16 = binary.BigEndian.PutUint16
|
|
|
|
put32 = binary.BigEndian.PutUint32
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParsedPacket is a minimal decoding of a packet suitable for use in filters.
|
2020-07-28 23:02:15 +00:00
|
|
|
//
|
|
|
|
// In general, it only supports IPv4. The IPv6 parsing is very minimal.
|
2020-06-04 22:42:44 +00:00
|
|
|
type ParsedPacket struct {
|
|
|
|
// b is the byte buffer that this decodes.
|
|
|
|
b []byte
|
|
|
|
// subofs is the offset of IP subprotocol.
|
|
|
|
subofs int
|
|
|
|
// dataofs is the offset of IP subprotocol payload.
|
|
|
|
dataofs int
|
|
|
|
// length is the total length of the packet.
|
|
|
|
// This is not the same as len(b) because b can have trailing zeros.
|
|
|
|
length int
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-07-28 23:02:15 +00:00
|
|
|
IPVersion uint8 // 4, 6, or 0
|
|
|
|
IPProto IPProto // IP subprotocol (UDP, TCP, etc); the NextHeader field for IPv6
|
|
|
|
SrcIP IP // IP source address (not used for IPv6)
|
|
|
|
DstIP IP // IP destination address (not used for IPv6)
|
|
|
|
SrcPort uint16 // TCP/UDP source port
|
|
|
|
DstPort uint16 // TCP/UDP destination port
|
|
|
|
TCPFlags uint8 // TCP flags (SYN, ACK, etc)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:02:15 +00:00
|
|
|
// NextHeader
|
|
|
|
type NextHeader uint8
|
|
|
|
|
|
|
|
func (p *ParsedPacket) String() string {
|
|
|
|
if p.IPVersion == 6 {
|
2020-07-30 17:57:30 +00:00
|
|
|
return fmt.Sprintf("IPv6{Proto=%d}", p.IPProto)
|
2020-07-28 23:02:15 +00:00
|
|
|
}
|
|
|
|
switch p.IPProto {
|
2020-06-04 22:42:44 +00:00
|
|
|
case Unknown:
|
|
|
|
return "Unknown{???}"
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-25 15:57:04 +00:00
|
|
|
sb := strbuilder.Get()
|
2020-07-28 23:02:15 +00:00
|
|
|
sb.WriteString(p.IPProto.String())
|
2020-05-25 15:57:04 +00:00
|
|
|
sb.WriteByte('{')
|
2020-07-28 23:02:15 +00:00
|
|
|
writeIPPort(sb, p.SrcIP, p.SrcPort)
|
2020-05-25 15:57:04 +00:00
|
|
|
sb.WriteString(" > ")
|
2020-07-28 23:02:15 +00:00
|
|
|
writeIPPort(sb, p.DstIP, p.DstPort)
|
2020-05-25 15:57:04 +00:00
|
|
|
sb.WriteByte('}')
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeIPPort(sb *strbuilder.Builder, ip IP, port uint16) {
|
|
|
|
sb.WriteUint(uint64(byte(ip >> 24)))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(byte(ip >> 16)))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(byte(ip >> 8)))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(byte(ip)))
|
|
|
|
sb.WriteByte(':')
|
|
|
|
sb.WriteUint(uint64(port))
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// based on https://tools.ietf.org/html/rfc1071
|
|
|
|
func ipChecksum(b []byte) uint16 {
|
|
|
|
var ac uint32
|
|
|
|
i := 0
|
|
|
|
n := len(b)
|
|
|
|
for n >= 2 {
|
2020-06-04 22:42:44 +00:00
|
|
|
ac += uint32(get16(b[i : i+2]))
|
2020-02-05 22:16:58 +00:00
|
|
|
n -= 2
|
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
if n == 1 {
|
|
|
|
ac += uint32(b[i]) << 8
|
|
|
|
}
|
|
|
|
for (ac >> 16) > 0 {
|
|
|
|
ac = (ac >> 16) + (ac & 0xffff)
|
|
|
|
}
|
|
|
|
return uint16(^ac)
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
// Decode extracts data from the packet in b into q.
|
|
|
|
// It performs extremely simple packet decoding for basic IPv4 packet types.
|
2020-02-05 22:16:58 +00:00
|
|
|
// It extracts only the subprotocol id, IP addresses, and (if any) ports,
|
|
|
|
// and shouldn't need any memory allocation.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) Decode(b []byte) {
|
2020-06-08 22:19:26 +00:00
|
|
|
q.b = b
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
if len(b) < ipHeaderLength {
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPVersion = 0
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-04 22:42:44 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// Check that it's IPv4.
|
|
|
|
// TODO(apenwarr): consider IPv6 support
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPVersion = (b[0] & 0xF0) >> 4
|
|
|
|
switch q.IPVersion {
|
2020-06-04 22:42:44 +00:00
|
|
|
case 4:
|
|
|
|
q.IPProto = IPProto(b[9])
|
|
|
|
case 6:
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPProto = IPProto(b[6]) // "Next Header" field
|
2020-06-04 22:42:44 +00:00
|
|
|
return
|
|
|
|
default:
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPVersion = 0
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
q.length = int(get16(b[2:4]))
|
|
|
|
if len(b) < q.length {
|
2020-02-05 22:16:58 +00:00
|
|
|
// Packet was cut off before full IPv4 length.
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's valid IPv4, then the IP addresses are valid
|
2020-06-04 22:42:44 +00:00
|
|
|
q.SrcIP = IP(get32(b[12:16]))
|
|
|
|
q.DstIP = IP(get32(b[16:20]))
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
q.subofs = int((b[0] & 0x0F) << 2)
|
2020-02-05 22:16:58 +00:00
|
|
|
sub := b[q.subofs:]
|
|
|
|
|
|
|
|
// We don't care much about IP fragmentation, except insofar as it's
|
|
|
|
// used for firewall bypass attacks. The trick is make the first
|
|
|
|
// fragment of a TCP or UDP packet so short that it doesn't fit
|
|
|
|
// the TCP or UDP header, so we can't read the port, in hope that
|
|
|
|
// it'll sneak past. Then subsequent fragments fill it in, but we're
|
|
|
|
// missing the first part of the header, so we can't read that either.
|
|
|
|
//
|
|
|
|
// A "perfectly correct" implementation would have to reassemble
|
|
|
|
// fragments before deciding what to do. But the truth is there's
|
|
|
|
// zero reason to send such a short first fragment, so we can treat
|
2020-06-04 22:42:44 +00:00
|
|
|
// it as Unknown. We can also treat any subsequent fragment that starts
|
|
|
|
// at such a low offset as Unknown.
|
|
|
|
fragFlags := get16(b[6:8])
|
2020-02-05 22:16:58 +00:00
|
|
|
moreFrags := (fragFlags & 0x20) != 0
|
|
|
|
fragOfs := fragFlags & 0x1FFF
|
|
|
|
if fragOfs == 0 {
|
|
|
|
// This is the first fragment
|
2020-05-25 15:57:04 +00:00
|
|
|
if moreFrags && len(sub) < minFrag {
|
2020-02-05 22:16:58 +00:00
|
|
|
// Suspiciously short first fragment, dump it.
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// otherwise, this is either non-fragmented (the usual case)
|
|
|
|
// or a big enough initial fragment that we can read the
|
|
|
|
// whole subprotocol header.
|
2020-06-04 22:42:44 +00:00
|
|
|
switch q.IPProto {
|
|
|
|
case ICMP:
|
|
|
|
if len(sub) < icmpHeaderLength {
|
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
q.SrcPort = 0
|
|
|
|
q.DstPort = 0
|
2020-06-04 22:42:44 +00:00
|
|
|
q.dataofs = q.subofs + icmpHeaderLength
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
2020-06-04 22:42:44 +00:00
|
|
|
case TCP:
|
|
|
|
if len(sub) < tcpHeaderLength {
|
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-04 22:42:44 +00:00
|
|
|
q.SrcPort = get16(sub[0:2])
|
|
|
|
q.DstPort = get16(sub[2:4])
|
2020-02-05 22:16:58 +00:00
|
|
|
q.TCPFlags = sub[13] & 0x3F
|
2020-06-04 22:42:44 +00:00
|
|
|
headerLength := (sub[12] & 0xF0) >> 2
|
|
|
|
q.dataofs = q.subofs + int(headerLength)
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
2020-06-04 22:42:44 +00:00
|
|
|
case UDP:
|
|
|
|
if len(sub) < udpHeaderLength {
|
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-04 22:42:44 +00:00
|
|
|
q.SrcPort = get16(sub[0:2])
|
|
|
|
q.DstPort = get16(sub[2:4])
|
|
|
|
q.dataofs = q.subofs + udpHeaderLength
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
default:
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a fragment other than the first one.
|
2020-05-25 15:57:04 +00:00
|
|
|
if fragOfs < minFrag {
|
2020-02-05 22:16:58 +00:00
|
|
|
// First frag was suspiciously short, so we can't
|
|
|
|
// trust the followup either.
|
2020-06-04 22:42:44 +00:00
|
|
|
q.IPProto = Unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// otherwise, we have to permit the fragment to slide through.
|
|
|
|
// Second and later fragments don't have sub-headers.
|
|
|
|
// Ideally, we would drop fragments that we can't identify,
|
|
|
|
// but that would require statefulness. Anyway, receivers'
|
|
|
|
// kernels know to drop fragments where the initial fragment
|
|
|
|
// doesn't arrive.
|
|
|
|
q.IPProto = Fragment
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) IPHeader() IPHeader {
|
|
|
|
ipid := get16(q.b[4:6])
|
|
|
|
return IPHeader{
|
|
|
|
IPID: ipid,
|
|
|
|
IPProto: q.IPProto,
|
|
|
|
SrcIP: q.SrcIP,
|
|
|
|
DstIP: q.DstIP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *ParsedPacket) ICMPHeader() ICMPHeader {
|
|
|
|
return ICMPHeader{
|
|
|
|
IPHeader: q.IPHeader(),
|
|
|
|
Type: ICMPType(q.b[q.subofs+0]),
|
|
|
|
Code: ICMPCode(q.b[q.subofs+1]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *ParsedPacket) UDPHeader() UDPHeader {
|
|
|
|
return UDPHeader{
|
|
|
|
IPHeader: q.IPHeader(),
|
|
|
|
SrcPort: q.SrcPort,
|
|
|
|
DstPort: q.DstPort,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:19:26 +00:00
|
|
|
// Buffer returns the entire packet buffer.
|
2020-06-09 17:09:43 +00:00
|
|
|
// This is a read-only view; that is, q retains the ownership of the buffer.
|
2020-06-08 22:19:26 +00:00
|
|
|
func (q *ParsedPacket) Buffer() []byte {
|
|
|
|
return q.b
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
// Sub returns the IP subprotocol section.
|
2020-06-09 17:09:43 +00:00
|
|
|
// This is a read-only view; that is, q retains the ownership of the buffer.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) Sub(begin, n int) []byte {
|
2020-02-05 22:16:58 +00:00
|
|
|
return q.b[q.subofs+begin : q.subofs+begin+n]
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
// Payload returns the payload of the IP subprotocol section.
|
2020-06-09 17:09:43 +00:00
|
|
|
// This is a read-only view; that is, q retains the ownership of the buffer.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) Payload() []byte {
|
|
|
|
return q.b[q.dataofs:q.length]
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:57:04 +00:00
|
|
|
// Trim trims the buffer to its IPv4 length.
|
2020-02-05 22:16:58 +00:00
|
|
|
// Sometimes packets arrive from an interface with extra bytes on the end.
|
|
|
|
// This removes them.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) Trim() []byte {
|
|
|
|
return q.b[:q.length]
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
// IsTCPSyn reports whether q is a TCP SYN packet
|
|
|
|
// (i.e. the first packet in a new connection).
|
|
|
|
func (q *ParsedPacket) IsTCPSyn() bool {
|
2020-05-25 15:57:04 +00:00
|
|
|
return (q.TCPFlags & TCPSynAck) == TCPSyn
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 15:57:04 +00:00
|
|
|
// IsError reports whether q is an IPv4 ICMP "Error" packet.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) IsError() bool {
|
2020-04-29 07:53:32 +00:00
|
|
|
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
|
2020-06-04 22:42:44 +00:00
|
|
|
switch ICMPType(q.b[q.subofs]) {
|
2020-05-25 15:57:04 +00:00
|
|
|
case ICMPUnreachable, ICMPTimeExceeded:
|
2020-04-29 07:53:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:57:04 +00:00
|
|
|
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Request.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) IsEchoRequest() bool {
|
2020-02-05 22:16:58 +00:00
|
|
|
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
|
2020-06-04 22:42:44 +00:00
|
|
|
return ICMPType(q.b[q.subofs]) == ICMPEchoRequest &&
|
|
|
|
ICMPCode(q.b[q.subofs+1]) == ICMPNoCode
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:57:04 +00:00
|
|
|
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Response.
|
2020-06-04 22:42:44 +00:00
|
|
|
func (q *ParsedPacket) IsEchoResponse() bool {
|
2020-04-29 07:53:32 +00:00
|
|
|
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
|
2020-06-04 22:42:44 +00:00
|
|
|
return ICMPType(q.b[q.subofs]) == ICMPEchoReply &&
|
|
|
|
ICMPCode(q.b[q.subofs+1]) == ICMPNoCode
|
2020-04-29 07:53:32 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func Hexdump(b []byte) string {
|
|
|
|
out := new(strings.Builder)
|
|
|
|
for i := 0; i < len(b); i += 16 {
|
|
|
|
if i > 0 {
|
|
|
|
fmt.Fprintf(out, "\n")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " %04x ", i)
|
|
|
|
j := 0
|
|
|
|
for ; j < 16 && i+j < len(b); j++ {
|
|
|
|
if j == 8 {
|
|
|
|
fmt.Fprintf(out, " ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "%02x ", b[i+j])
|
|
|
|
}
|
|
|
|
for ; j < 16; j++ {
|
|
|
|
if j == 8 {
|
|
|
|
fmt.Fprintf(out, " ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, " ")
|
|
|
|
for j = 0; j < 16 && i+j < len(b); j++ {
|
|
|
|
if b[i+j] >= 32 && b[i+j] < 128 {
|
|
|
|
fmt.Fprintf(out, "%c", b[i+j])
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, ".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|