add packet capture method

master v0.1.0
Johannes Kimmel 8 months ago
parent f45a5665cf
commit 98bc2c21ba
  1. 90
      capture/capture.go
  2. 1
      go.mod
  3. 5
      go.sum
  4. 69
      main.go
  5. 37
      packet/packet.go

@ -0,0 +1,90 @@
package capture
import (
"fmt"
"git.freifunk-franken.de/jkimmel/abbel/packet"
"github.com/google/gopacket"
"github.com/google/gopacket/afpacket"
"github.com/google/gopacket/layers"
"golang.org/x/net/bpf"
"inet.af/netaddr"
)
var (
// tcpdump -y RAW -dd ip6 and udp dst port 6696
babelBPFRAW = []bpf.RawInstruction{
{0x30, 0, 0, 0x00000000},
{0x54, 0, 0, 0x000000f0},
{0x15, 0, 5, 0x00000060},
{0x30, 0, 0, 0x00000006},
{0x15, 0, 3, 0x00000011},
{0x28, 0, 0, 0x0000002a},
{0x15, 0, 1, 0x00001a28},
{0x6, 0, 0, 0x00040000},
{0x6, 0, 0, 0x00000000},
}
)
type Handle struct {
tp *afpacket.TPacket
}
func FromInterface(iface string) (Handle, error) {
var h Handle
var err error
if iface != "any" {
h.tp, err = afpacket.NewTPacket(
afpacket.SocketDgram,
afpacket.OptInterface(iface),
)
} else {
h.tp, err = afpacket.NewTPacket(
afpacket.SocketDgram,
)
}
if err != nil {
fmt.Println("here")
return Handle{}, err
}
err = h.tp.SetBPF(babelBPFRAW)
if err != nil {
return Handle{}, err
}
return h, nil
}
func (h Handle) Close() error {
h.tp.Close()
return nil
}
func (h Handle) ReadFrom(b []byte) (body []byte, src netaddr.IP, ifindex int, err error) {
data, ci, err := h.tp.ZeroCopyReadPacketData()
if err != nil {
return nil, netaddr.IP{}, 0, err
}
pckt := gopacket.NewPacket(data, layers.LayerTypeIPv6, gopacket.NoCopy)
ip6, ok := pckt.NetworkLayer().(*layers.IPv6)
if !ok {
return nil, netaddr.IP{}, 0, fmt.Errorf("Expected IPv6 layer, got %v", pckt.NetworkLayer())
}
src, ok = netaddr.FromStdIP(ip6.SrcIP)
if !ok {
return nil, netaddr.IP{}, 0, fmt.Errorf("Error parsing packet src address %q", ip6.SrcIP)
}
payload := pckt.ApplicationLayer().Payload()
copy(b, payload)
b, err = packet.Validate(b[:len(payload)])
if err != nil {
return nil, netaddr.IP{}, 0, err
}
return b, src, ci.InterfaceIndex, nil
}

@ -3,6 +3,7 @@ module git.freifunk-franken.de/jkimmel/abbel
go 1.0
require (
github.com/google/gopacket v1.1.19
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6
)

@ -1,4 +1,6 @@
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 h1:UXLjNohABv4S58tHmeuIZDO6e3mHpW2Dx33gaNt03LE=
go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
@ -7,6 +9,8 @@ go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@ -26,6 +30,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -4,14 +4,17 @@ import (
"fmt"
"log"
"git.freifunk-franken.de/jkimmel/abbel/capture"
"git.freifunk-franken.de/jkimmel/abbel/packet"
"git.freifunk-franken.de/jkimmel/abbel/tlv"
"inet.af/netaddr"
)
type options struct {
Group string
Port uint16
Ifs []string
Group string
Port uint16
Ifs []string
UseCapture bool
}
func parseOpts() (options, error) {
@ -19,16 +22,39 @@ func parseOpts() (options, error) {
var err error
opt = options{
Group: "ff02:0:0:0:0:0:1:6",
Port: 6696,
Ifs: []string{"babel"},
Group: "ff02:0:0:0:0:0:1:6",
Port: 6696,
Ifs: []string{"babel"},
UseCapture: false,
}
return opt, err
}
type updatesKey struct {
prefix netaddr.IPPrefix
routerID tlv.RouterID
nexthop netaddr.IP
}
var (
updates = map[updatesKey]tlv.Update{}
)
type BabelPacketConn interface {
ReadFrom(b []byte) (body []byte, src netaddr.IP, ifindex int, err error)
Close() error
}
func run(opt options) error {
conn, err := packet.Listen(opt.Group, opt.Port, opt.Ifs...)
var err error
var conn BabelPacketConn
if opt.UseCapture {
conn, err = capture.FromInterface(opt.Ifs[0])
} else {
conn, err = packet.Listen(opt.Group, opt.Port, opt.Ifs...)
}
if err != nil {
return err
}
@ -37,36 +63,39 @@ func run(opt options) error {
buf := [4096]byte{}
var s tlv.PacketDecoder
for {
b, src, rcm, err := conn.ReadFrom(buf[:])
b, src, ifindex, err := conn.ReadFrom(buf[:])
if err != nil {
fmt.Println("Skipping packet:", err)
continue
}
fmt.Print("\x1B[1m")
fmt.Printf("Packet size %4d %s", len(b), rcm)
fmt.Printf("Packet size %4d from %s", len(b), src)
fmt.Println("\x1B[0m")
s.Reset(b, src, rcm.IfIndex)
s.Reset(b, src, ifindex)
for s.Scan() {
switch t := s.TLV().(type) {
case tlv.NextHop:
fmt.Printf("%12s %s\n", t.T(), t.Address)
//fmt.Printf("%12s %s\n", t.T(), t.Address)
case tlv.Hello:
fmt.Printf("% 12s %+v\n", t.T(), t)
case tlv.Update:
if err != nil {
fmt.Println(t, t.Prefix, err)
key := updatesKey{t.Prefix, t.RouterID, t.NextHop}
tmetric := t.Metric
diff := int(tmetric) - int(updates[key].Metric)
updates[key] = t
if diff == 0 && tmetric != 0xFFFF {
break
}
if t.Metric == 0xFFFF {
fmt.Print("\x1B[31m")
} else {
if t.Metric == 0xFFFF {
fmt.Print("\x1B[31m")
} else {
fmt.Print("\x1B[36m")
}
fmt.Printf("% 12s %-43s%s\x1B[0m\n", t.T(), t.Prefix, t.FormatHeader())
fmt.Print("\x1B[36m")
}
fmt.Printf("% 12s %-43s%+d %s\x1B[0m\n", t.T(), t.Prefix, diff, t.FormatHeader())
case tlv.RouterID:
fmt.Printf("%12s %s\n", t.T(), t)
//fmt.Printf("%12s %s\n", t.T(), t)
case tlv.RouteRequest:
fmt.Printf("%12s %s\n", t.T(), t.Prefix)
case tlv.Raw:

@ -63,16 +63,11 @@ func (c Conn) JoinGroup(ifname string, addr string) error {
return c.v6pc.JoinGroup(ifi, &net.UDPAddr{IP: ip.IPAddr().IP})
}
func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, rcm *ipv6.ControlMessage, err error) {
var n int
n, rcm, _, err = c.v6pc.ReadFrom(b)
if err != nil {
return nil, netaddr.IP{}, nil, err
}
func Validate(b []byte) ([]byte, error) {
n := len(b)
b = b[:n]
if len(b) < BabelPacketHeaderSize {
return nil, netaddr.IP{}, nil, fmt.Errorf("Packet too short: %d", len(b))
if n < BabelPacketHeaderSize {
return nil, fmt.Errorf("Packet too short: %d", len(b))
}
magic := b[0]
@ -81,22 +76,36 @@ func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, rcm *ipv6.Control
b = b[4:]
if magic != BabelMagic {
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid magic number %d", magic)
return nil, fmt.Errorf("Invalid magic number %d", magic)
}
if version != BabelVersion {
return nil, netaddr.IP{}, nil, fmt.Errorf("Unsupported version number %d", version)
return nil, fmt.Errorf("Unsupported version number %d", version)
}
if int(length) > len(b) {
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid length for packet of size %d: %d", n, length)
return nil, fmt.Errorf("Invalid length for packet of size %d: %d", n, length)
}
return b, nil
}
func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, ifindex int, err error) {
n, rcm, _, err := c.v6pc.ReadFrom(b)
if err != nil {
return nil, netaddr.IP{}, 0, err
}
b, err = Validate(b[:n])
if err != nil {
return nil, netaddr.IP{}, 0, err
}
var ok bool
src, ok = netaddr.FromStdIPRaw(rcm.Src)
if !ok {
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid src address %q", rcm.Src)
return nil, netaddr.IP{}, 0, fmt.Errorf("Invalid src address %q", rcm.Src)
}
return b, src, rcm, err
return b, src, rcm.IfIndex, err
}

Loading…
Cancel
Save