Skip to content

Commit

Permalink
syscall: provide and use ioctlPtr for all BSD platforms
Browse files Browse the repository at this point in the history
Provide ioctlPtr for all BSD platforms, then use this for BPF.
This reduces darwin specific code, as well as avoiding the use of
an indirect system call on OpenBSD.

Updates #63900

Change-Id: I81f3e74a3149150abe972f106903310e3cf26929
Reviewed-on: https://go-review.googlesource.com/c/go/+/540019
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Joel Sing <joel@sing.id.au>
Reviewed-by: Josh Rickmar <jrick@zettaport.com>
  • Loading branch information
4a6f656c committed Nov 7, 2023
1 parent fd59c87 commit 1d9040b
Show file tree
Hide file tree
Showing 31 changed files with 461 additions and 387 deletions.
100 changes: 50 additions & 50 deletions src/syscall/bpf_bsd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build dragonfly || freebsd || netbsd || openbsd
//go:build darwin || dragonfly || freebsd || netbsd || openbsd

// Berkeley packet filter for BSD variants

Expand All @@ -25,55 +25,55 @@ func BpfJump(code, k, jt, jf int) *BpfInsn {
// Deprecated: Use golang.org/x/net/bpf instead.
func BpfBuflen(fd int) (int, error) {
var l int
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
if err != 0 {
return 0, Errno(err)
err := ioctlPtr(fd, BIOCGBLEN, unsafe.Pointer(&l))
if err != nil {
return 0, err
}
return l, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfBuflen(fd, l int) (int, error) {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSBLEN, uintptr(unsafe.Pointer(&l)))
if err != 0 {
return 0, Errno(err)
err := ioctlPtr(fd, BIOCSBLEN, unsafe.Pointer(&l))
if err != nil {
return 0, err
}
return l, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func BpfDatalink(fd int) (int, error) {
var t int
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
if err != 0 {
return 0, Errno(err)
err := ioctlPtr(fd, BIOCGDLT, unsafe.Pointer(&t))
if err != nil {
return 0, err
}
return t, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfDatalink(fd, t int) (int, error) {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSDLT, uintptr(unsafe.Pointer(&t)))
if err != 0 {
return 0, Errno(err)
err := ioctlPtr(fd, BIOCSDLT, unsafe.Pointer(&t))
if err != nil {
return 0, err
}
return t, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfPromisc(fd, m int) error {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCPROMISC, unsafe.Pointer(&m))
if err != nil {
return err
}
return nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func FlushBpf(fd int) error {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCFLUSH, nil)
if err != nil {
return err
}
return nil
}
Expand All @@ -86,9 +86,9 @@ type ivalue struct {
// Deprecated: Use golang.org/x/net/bpf instead.
func BpfInterface(fd int, name string) (string, error) {
var iv ivalue
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
if err != 0 {
return "", Errno(err)
err := ioctlPtr(fd, BIOCGETIF, unsafe.Pointer(&iv))
if err != nil {
return "", err
}
return name, nil
}
Expand All @@ -97,47 +97,47 @@ func BpfInterface(fd int, name string) (string, error) {
func SetBpfInterface(fd int, name string) error {
var iv ivalue
copy(iv.name[:], []byte(name))
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCSETIF, unsafe.Pointer(&iv))
if err != nil {
return err
}
return nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func BpfTimeout(fd int) (*Timeval, error) {
var tv Timeval
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
if err != 0 {
return nil, Errno(err)
err := ioctlPtr(fd, BIOCGRTIMEOUT, unsafe.Pointer(&tv))
if err != nil {
return nil, err
}
return &tv, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfTimeout(fd int, tv *Timeval) error {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCSRTIMEOUT, unsafe.Pointer(tv))
if err != nil {
return err
}
return nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func BpfStats(fd int) (*BpfStat, error) {
var s BpfStat
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
if err != 0 {
return nil, Errno(err)
err := ioctlPtr(fd, BIOCGSTATS, unsafe.Pointer(&s))
if err != nil {
return nil, err
}
return &s, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfImmediate(fd, m int) error {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCIMMEDIATE, unsafe.Pointer(&m))
if err != nil {
return err
}
return nil
}
Expand All @@ -147,19 +147,19 @@ func SetBpf(fd int, i []BpfInsn) error {
var p BpfProgram
p.Len = uint32(len(i))
p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCSETF, unsafe.Pointer(&p))
if err != nil {
return err
}
return nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func CheckBpfVersion(fd int) error {
var v BpfVersion
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCVERSION, unsafe.Pointer(&v))
if err != nil {
return err
}
if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
return EINVAL
Expand All @@ -170,18 +170,18 @@ func CheckBpfVersion(fd int) error {
// Deprecated: Use golang.org/x/net/bpf instead.
func BpfHeadercmpl(fd int) (int, error) {
var f int
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
if err != 0 {
return 0, Errno(err)
err := ioctlPtr(fd, BIOCGHDRCMPLT, unsafe.Pointer(&f))
if err != nil {
return 0, err
}
return f, nil
}

// Deprecated: Use golang.org/x/net/bpf instead.
func SetBpfHeadercmpl(fd, f int) error {
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))
if err != 0 {
return Errno(err)
err := ioctlPtr(fd, BIOCSHDRCMPLT, unsafe.Pointer(&f))
if err != nil {
return err
}
return nil
}
185 changes: 0 additions & 185 deletions src/syscall/bpf_darwin.go

This file was deleted.

Loading

0 comments on commit 1d9040b

Please sign in to comment.