Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afpacket: Fix support for 32-bit x86 arch #3

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: CI

on:
push:
branches: [ beats ]
pull_request:
branches: [ beats ]

jobs:
test:
strategy:
matrix:
go-version: [1.16.x, 1.17.x]
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Install Linux packages
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -qq libpcap-dev

- name: Checkout code
uses: actions/checkout@v2

- name: Test
run: |
go test ./bytediff
go test ./defrag/lcmdefrag
go test ./dumpcommand
go test ./ip4defrag
go test ./layers
go test ./macs
go test ./pcap
go test ./pcap/gopacket_benchmark
go test ./pcapgo
go test ./reassembly
go test ./tcpassembly
go test ./tcpassembly/tcpreader
# go test ./pfring

- name: Test Race
run: |
go test -race ./bytediff
go test -race ./defrag/lcmdefrag
go test -race ./dumpcommand
go test -race ./ip4defrag
go test -race ./layers
go test -race ./macs
go test -race ./pcap
go test -race ./pcap/gopacket_benchmark
go test -race ./pcapgo
go test -race ./reassembly
go test -race ./tcpassembly
go test -race ./tcpassembly/tcpreader
# go test -race ./pfring

- name: Test Linux
if: matrix.os == 'ubuntu-latest'
run: |
go test ./afpacket
sudo $(which go) test ./routing

- name: Test Linux Race
if: matrix.os == 'ubuntu-latest'
run: |
go test -race ./afpacket
sudo $(which go) test -race ./routing

- name: Test MacOS
if: matrix.os == 'macos-latest'
run: |
go test ./bsdbpf

- name: Test MacOS Race
if: matrix.os == 'macos-latest'
run: |
go test -race ./bsdbpf
12 changes: 4 additions & 8 deletions afpacket/afpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,18 @@ func (h *TPacket) Stats() (Stats, error) {
func (h *TPacket) InitSocketStats() error {
if h.tpVersion == TPacketVersion3 {
socklen := unsafe.Sizeof(h.socketStatsV3)
slt := C.socklen_t(socklen)
var ssv3 SocketStatsV3

err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ssv3), uintptr(unsafe.Pointer(&slt)))
err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ssv3), &socklen)
if err != nil {
return err
}
h.socketStatsV3 = SocketStatsV3{}
} else {
socklen := unsafe.Sizeof(h.socketStats)
slt := C.socklen_t(socklen)
var ss SocketStats

err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ss), uintptr(unsafe.Pointer(&slt)))
err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ss), &socklen)
if err != nil {
return err
}
Expand All @@ -366,10 +364,9 @@ func (h *TPacket) SocketStats() (SocketStats, SocketStatsV3, error) {
// We need to save the counters since asking for the stats will clear them
if h.tpVersion == TPacketVersion3 {
socklen := unsafe.Sizeof(h.socketStatsV3)
slt := C.socklen_t(socklen)
var ssv3 SocketStatsV3

err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ssv3), uintptr(unsafe.Pointer(&slt)))
err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ssv3), &socklen)
if err != nil {
return SocketStats{}, SocketStatsV3{}, err
}
Expand All @@ -380,10 +377,9 @@ func (h *TPacket) SocketStats() (SocketStats, SocketStatsV3, error) {
return h.socketStats, h.socketStatsV3, nil
}
socklen := unsafe.Sizeof(h.socketStats)
slt := C.socklen_t(socklen)
var ss SocketStats

err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ss), uintptr(unsafe.Pointer(&slt)))
err := getsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_STATISTICS, unsafe.Pointer(&ss), &socklen)
if err != nil {
return SocketStats{}, SocketStatsV3{}, err
}
Expand Down
46 changes: 20 additions & 26 deletions afpacket/sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,38 @@
package afpacket

import (
"errors"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

const maxOptSize = 8192

var errSockoptTooBig = errors.New("socket option too big")

// setsockopt provides access to the setsockopt syscall.
func setsockopt(fd, level, name int, val unsafe.Pointer, vallen uintptr) error {
_, _, errno := unix.Syscall6(
unix.SYS_SETSOCKOPT,
uintptr(fd),
uintptr(level),
uintptr(name),
uintptr(val),
vallen,
0,
)
if errno != 0 {
return error(errno)
if vallen > maxOptSize {
return errSockoptTooBig
}

return nil
slice := (*[maxOptSize]byte)(val)[:]
return syscall.SetsockoptString(fd, level, name, string(slice[:vallen]))
}

// getsockopt provides access to the getsockopt syscall.
func getsockopt(fd, level, name int, val unsafe.Pointer, vallen uintptr) error {
_, _, errno := unix.Syscall6(
unix.SYS_GETSOCKOPT,
uintptr(fd),
uintptr(level),
uintptr(name),
uintptr(val),
vallen,
0,
)
if errno != 0 {
return error(errno)
func getsockopt(fd, level, name int, val unsafe.Pointer, vallen *uintptr) error {
s, err := unix.GetsockoptString(fd, level, name)
if err != nil {
return err
}

rcvLen := uintptr(len(s))
if rcvLen > *vallen {
return errSockoptTooBig
}
copy((*[maxOptSize]byte)(val)[:rcvLen], s)
*vallen = rcvLen
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pcap/pcapnggo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"io/ioutil"
"reflect"
"runtime"
"testing"
"time"

Expand All @@ -19,6 +20,10 @@ import (
)

func TestPCAPGoNgWrite(t *testing.T) {
if runtime.GOOS == "darwin" {
t.Skip("Reading PCAPNG files fails on MacOS")
}

f, err := ioutil.TempFile("", "pcapnggo")
if err != nil {
t.Fatal(err)
Expand Down