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"
|
2020-12-20 00:43:25 +00:00
|
|
|
"net"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
2020-05-25 15:57:04 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
"inet.af/netaddr"
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2020-05-25 15:57:04 +00:00
|
|
|
"tailscale.com/types/strbuilder"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2021-03-21 04:45:47 +00:00
|
|
|
const unknown = ipproto.Unknown
|
2021-03-20 04:05:51 +00:00
|
|
|
|
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
|
|
|
|
2021-01-12 04:17:13 +00:00
|
|
|
type TCPFlag uint8
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
const (
|
2021-01-12 04:17:13 +00:00
|
|
|
TCPFin TCPFlag = 0x01
|
|
|
|
TCPSyn TCPFlag = 0x02
|
|
|
|
TCPRst TCPFlag = 0x04
|
|
|
|
TCPPsh TCPFlag = 0x08
|
|
|
|
TCPAck TCPFlag = 0x10
|
|
|
|
TCPSynAck TCPFlag = TCPSyn | TCPAck
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
// Parsed is a minimal decoding of a packet suitable for use in filters.
|
|
|
|
type Parsed struct {
|
2020-06-04 22:42:44 +00:00
|
|
|
// 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-11-10 09:00:35 +00:00
|
|
|
// 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.
|
2021-03-20 04:05:51 +00:00
|
|
|
IPProto ipproto.Proto
|
2020-12-20 00:43:25 +00:00
|
|
|
// SrcIP4 is the source address. Family matches IPVersion. Port is
|
|
|
|
// valid iff IPProto == TCP || IPProto == UDP.
|
|
|
|
Src netaddr.IPPort
|
|
|
|
// DstIP4 is the destination address. Family matches IPVersion.
|
|
|
|
Dst netaddr.IPPort
|
2020-11-10 09:00:35 +00:00
|
|
|
// TCPFlags is the packet's TCP flag bigs. Valid iff IPProto == TCP.
|
2021-01-12 04:17:13 +00:00
|
|
|
TCPFlags TCPFlag
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
func (p *Parsed) String() string {
|
2020-12-20 00:43:25 +00:00
|
|
|
if p.IPVersion != 4 && p.IPVersion != 6 {
|
2020-06-04 22:42:44 +00:00
|
|
|
return "Unknown{???}"
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-25 15:57:04 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
sb := strbuilder.Get()
|
|
|
|
sb.WriteString(p.IPProto.String())
|
|
|
|
sb.WriteByte('{')
|
|
|
|
writeIPPort(sb, p.Src)
|
|
|
|
sb.WriteString(" > ")
|
|
|
|
writeIPPort(sb, p.Dst)
|
|
|
|
sb.WriteByte('}')
|
|
|
|
return sb.String()
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
// writeIPPort writes ipp.String() into sb, with fewer allocations.
|
|
|
|
//
|
|
|
|
// TODO: make netaddr more efficient in this area, and retire this func.
|
|
|
|
func writeIPPort(sb *strbuilder.Builder, ipp netaddr.IPPort) {
|
|
|
|
if ipp.IP.Is4() {
|
|
|
|
raw := ipp.IP.As4()
|
|
|
|
sb.WriteUint(uint64(raw[0]))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(raw[1]))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(raw[2]))
|
|
|
|
sb.WriteByte('.')
|
|
|
|
sb.WriteUint(uint64(raw[3]))
|
|
|
|
sb.WriteByte(':')
|
|
|
|
} else {
|
|
|
|
sb.WriteByte('[')
|
|
|
|
sb.WriteString(ipp.IP.String()) // TODO: faster?
|
|
|
|
sb.WriteString("]:")
|
|
|
|
}
|
|
|
|
sb.WriteUint(uint64(ipp.Port))
|
2020-11-10 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
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-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) Decode(b []byte) {
|
2020-06-08 22:19:26 +00:00
|
|
|
q.b = b
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(b) < 1 {
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPVersion = 0
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-04 22:42:44 +00:00
|
|
|
|
2020-11-14 02:20:03 +00:00
|
|
|
q.IPVersion = b[0] >> 4
|
2020-07-28 23:02:15 +00:00
|
|
|
switch q.IPVersion {
|
2020-06-04 22:42:44 +00:00
|
|
|
case 4:
|
2020-11-10 09:00:35 +00:00
|
|
|
q.decode4(b)
|
2020-06-04 22:42:44 +00:00
|
|
|
case 6:
|
2020-11-10 09:00:35 +00:00
|
|
|
q.decode6(b)
|
2020-06-04 22:42:44 +00:00
|
|
|
default:
|
2020-07-28 23:02:15 +00:00
|
|
|
q.IPVersion = 0
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 00:10:30 +00:00
|
|
|
// StuffForTesting makes Parsed contain a len-bytes buffer. Used in
|
|
|
|
// tests to build up a synthetic parse result with a non-zero buffer.
|
|
|
|
func (q *Parsed) StuffForTesting(len int) {
|
|
|
|
q.b = make([]byte, len)
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
func (q *Parsed) decode4(b []byte) {
|
|
|
|
if len(b) < ip4HeaderLength {
|
|
|
|
q.IPVersion = 0
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
// Check that it's IPv4.
|
2021-03-20 04:05:51 +00:00
|
|
|
q.IPProto = ipproto.Proto(b[9])
|
2020-11-11 03:09:47 +00:00
|
|
|
q.length = int(binary.BigEndian.Uint16(b[2:4]))
|
2020-06-04 22:42:44 +00:00
|
|
|
if len(b) < q.length {
|
2020-02-05 22:16:58 +00:00
|
|
|
// Packet was cut off before full IPv4 length.
|
2021-03-21 04:45:47 +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-12-20 00:43:25 +00:00
|
|
|
q.Src.IP = netaddr.IPv4(b[12], b[13], b[14], b[15])
|
|
|
|
q.Dst.IP = netaddr.IPv4(b[16], b[17], b[18], b[19])
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
q.subofs = int((b[0] & 0x0F) << 2)
|
2020-11-11 03:10:11 +00:00
|
|
|
if q.subofs > q.length {
|
|
|
|
// next-proto starts beyond end of packet.
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-11 03:10:11 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
sub := b[q.subofs:]
|
2020-11-14 02:14:10 +00:00
|
|
|
sub = sub[:len(sub):len(sub)] // help the compiler do bounds check elimination
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-11-11 03:09:47 +00:00
|
|
|
fragFlags := binary.BigEndian.Uint16(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.
|
2021-03-21 04:45:47 +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 {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv4:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(sub) < icmp4HeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = 0
|
|
|
|
q.Dst.Port = 0
|
2020-11-10 09:00:35 +00:00
|
|
|
q.dataofs = q.subofs + icmp4HeaderLength
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.IGMP:
|
2020-11-11 07:23:17 +00:00
|
|
|
// Keep IPProto, but don't parse anything else
|
|
|
|
// out.
|
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TCP:
|
2020-06-04 22:42:44 +00:00
|
|
|
if len(sub) < tcpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
2021-01-12 04:17:13 +00:00
|
|
|
q.TCPFlags = TCPFlag(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
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.UDP:
|
2020-06-04 22:42:44 +00:00
|
|
|
if len(sub) < udpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
2020-06-04 22:42:44 +00:00
|
|
|
q.dataofs = q.subofs + udpHeaderLength
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.SCTP:
|
2021-03-20 04:05:51 +00:00
|
|
|
if len(sub) < sctpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2021-03-20 04:05:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TSMP:
|
2021-01-12 20:03:41 +00:00
|
|
|
// Inter-tailscale messages.
|
|
|
|
q.dataofs = q.subofs
|
|
|
|
return
|
2020-02-05 22:16:58 +00:00
|
|
|
default:
|
2021-03-21 04:45:47 +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.
|
2021-03-21 04:45:47 +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.
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = ipproto.Fragment
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
func (q *Parsed) decode6(b []byte) {
|
|
|
|
if len(b) < ip6HeaderLength {
|
|
|
|
q.IPVersion = 0
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
q.IPProto = ipproto.Proto(b[6])
|
2020-11-11 03:09:47 +00:00
|
|
|
q.length = int(binary.BigEndian.Uint16(b[4:6])) + ip6HeaderLength
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(b) < q.length {
|
|
|
|
// Packet was cut off before the full IPv6 length.
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
// okay to ignore `ok` here, because IPs pulled from packets are
|
|
|
|
// always well-formed stdlib IPs.
|
|
|
|
q.Src.IP, _ = netaddr.FromStdIP(net.IP(b[8:24]))
|
|
|
|
q.Dst.IP, _ = netaddr.FromStdIP(net.IP(b[24:40]))
|
2020-11-10 09:00:35 +00:00
|
|
|
|
|
|
|
// We don't support any IPv6 extension headers. Don't try to
|
|
|
|
// be clever. Therefore, the IP subprotocol always starts at
|
|
|
|
// byte 40.
|
|
|
|
//
|
|
|
|
// Note that this means we don't support fragmentation in
|
|
|
|
// IPv6. This is fine, because IPv6 strongly mandates that you
|
|
|
|
// should not fragment, which makes fragmentation on the open
|
|
|
|
// internet extremely uncommon.
|
|
|
|
//
|
|
|
|
// This also means we don't support IPSec headers (AH/ESP), or
|
|
|
|
// IPv6 jumbo frames. Those will get marked Unknown and
|
|
|
|
// dropped.
|
|
|
|
q.subofs = 40
|
|
|
|
sub := b[q.subofs:]
|
2020-11-14 02:14:10 +00:00
|
|
|
sub = sub[:len(sub):len(sub)] // help the compiler do bounds check elimination
|
2020-11-10 09:00:35 +00:00
|
|
|
|
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv6:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(sub) < icmp6HeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = 0
|
|
|
|
q.Dst.Port = 0
|
2020-11-10 09:00:35 +00:00
|
|
|
q.dataofs = q.subofs + icmp6HeaderLength
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TCP:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(sub) < tcpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
2021-01-12 04:17:13 +00:00
|
|
|
q.TCPFlags = TCPFlag(sub[13]) & 0x3F
|
2020-11-10 09:00:35 +00:00
|
|
|
headerLength := (sub[12] & 0xF0) >> 2
|
|
|
|
q.dataofs = q.subofs + int(headerLength)
|
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.UDP:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(sub) < udpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
2020-11-10 09:00:35 +00:00
|
|
|
q.dataofs = q.subofs + udpHeaderLength
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.SCTP:
|
2021-03-20 04:05:51 +00:00
|
|
|
if len(sub) < sctpHeaderLength {
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2021-03-20 04:05:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
q.Src.Port = binary.BigEndian.Uint16(sub[0:2])
|
|
|
|
q.Dst.Port = binary.BigEndian.Uint16(sub[2:4])
|
|
|
|
return
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TSMP:
|
2021-01-12 20:03:41 +00:00
|
|
|
// Inter-tailscale messages.
|
|
|
|
q.dataofs = q.subofs
|
|
|
|
return
|
2020-11-10 09:00:35 +00:00
|
|
|
default:
|
2021-03-21 04:45:47 +00:00
|
|
|
q.IPProto = unknown
|
2020-11-10 09:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Parsed) IP4Header() IP4Header {
|
|
|
|
if q.IPVersion != 4 {
|
|
|
|
panic("IP4Header called on non-IPv4 Parsed")
|
|
|
|
}
|
2020-11-11 03:09:47 +00:00
|
|
|
ipid := binary.BigEndian.Uint16(q.b[4:6])
|
2020-11-09 23:34:03 +00:00
|
|
|
return IP4Header{
|
2020-06-04 22:42:44 +00:00
|
|
|
IPID: ipid,
|
|
|
|
IPProto: q.IPProto,
|
2020-12-20 00:43:25 +00:00
|
|
|
Src: q.Src.IP,
|
|
|
|
Dst: q.Dst.IP,
|
2020-06-04 22:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
func (q *Parsed) ICMP4Header() ICMP4Header {
|
|
|
|
if q.IPVersion != 4 {
|
|
|
|
panic("IP4Header called on non-IPv4 Parsed")
|
|
|
|
}
|
2020-11-09 23:34:03 +00:00
|
|
|
return ICMP4Header{
|
2020-11-10 09:00:35 +00:00
|
|
|
IP4Header: q.IP4Header(),
|
2020-11-09 23:34:03 +00:00
|
|
|
Type: ICMP4Type(q.b[q.subofs+0]),
|
|
|
|
Code: ICMP4Code(q.b[q.subofs+1]),
|
2020-06-04 22:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
func (q *Parsed) UDP4Header() UDP4Header {
|
|
|
|
if q.IPVersion != 4 {
|
|
|
|
panic("IP4Header called on non-IPv4 Parsed")
|
|
|
|
}
|
2020-11-09 23:34:03 +00:00
|
|
|
return UDP4Header{
|
2020-11-10 09:00:35 +00:00
|
|
|
IP4Header: q.IP4Header(),
|
2020-12-20 00:43:25 +00:00
|
|
|
SrcPort: q.Src.Port,
|
|
|
|
DstPort: q.Dst.Port,
|
2020-06-04 22:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) Buffer() []byte {
|
2020-06-08 22:19:26 +00:00
|
|
|
return q.b
|
|
|
|
}
|
|
|
|
|
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-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) Payload() []byte {
|
2020-06-04 22:42:44 +00:00
|
|
|
return q.b[q.dataofs:q.length]
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTCPSyn reports whether q is a TCP SYN packet
|
|
|
|
// (i.e. the first packet in a new connection).
|
2020-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) IsTCPSyn() bool {
|
2020-05-25 15:57:04 +00:00
|
|
|
return (q.TCPFlags & TCPSynAck) == TCPSyn
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
// IsError reports whether q is an ICMP "Error" packet.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) IsError() bool {
|
2020-11-10 09:00:35 +00:00
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv4:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(q.b) < q.subofs+8 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t := ICMP4Type(q.b[q.subofs])
|
|
|
|
return t == ICMP4Unreachable || t == ICMP4TimeExceeded
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv6:
|
2020-11-10 09:00:35 +00:00
|
|
|
if len(q.b) < q.subofs+8 {
|
|
|
|
return false
|
2020-04-29 07:53:32 +00:00
|
|
|
}
|
2020-11-10 09:00:35 +00:00
|
|
|
t := ICMP6Type(q.b[q.subofs])
|
|
|
|
return t == ICMP6Unreachable || t == ICMP6TimeExceeded
|
|
|
|
default:
|
|
|
|
return false
|
2020-04-29 07:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:00:35 +00:00
|
|
|
// IsEchoRequest reports whether q is an ICMP Echo Request.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) IsEchoRequest() bool {
|
2020-11-10 09:00:35 +00:00
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv4:
|
2020-11-10 09:00:35 +00:00
|
|
|
return len(q.b) >= q.subofs+8 && ICMP4Type(q.b[q.subofs]) == ICMP4EchoRequest && ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv6:
|
2020-11-10 09:00:35 +00:00
|
|
|
return len(q.b) >= q.subofs+8 && ICMP6Type(q.b[q.subofs]) == ICMP6EchoRequest && ICMP6Code(q.b[q.subofs+1]) == ICMP6NoCode
|
|
|
|
default:
|
|
|
|
return false
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:57:04 +00:00
|
|
|
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Response.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (q *Parsed) IsEchoResponse() bool {
|
2020-11-10 09:00:35 +00:00
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv4:
|
2020-11-10 09:00:35 +00:00
|
|
|
return len(q.b) >= q.subofs+8 && ICMP4Type(q.b[q.subofs]) == ICMP4EchoReply && ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv6:
|
2020-11-10 09:00:35 +00:00
|
|
|
return len(q.b) >= q.subofs+8 && ICMP6Type(q.b[q.subofs]) == ICMP6EchoReply && ICMP6Code(q.b[q.subofs+1]) == ICMP6NoCode
|
|
|
|
default:
|
|
|
|
return false
|
2020-04-29 07:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|