2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-06-14 09:28:36 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-08-23 09:51:25 -06:00
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-06-14 09:28:36 -06:00
|
|
|
// Berkeley packet filter for BSD variants
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BpfStmt(code, k int) *BpfInsn {
|
|
|
|
return &BpfInsn{Code: uint16(code), K: uint32(k)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BpfJump(code, k, jt, jf int) *BpfInsn {
|
|
|
|
return &BpfInsn{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfBuflen(fd int) (int, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var l int
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
|
|
|
|
if err != 0 {
|
|
|
|
return 0, Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return l, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
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)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return l, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfDatalink(fd int) (int, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var t int
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
|
|
|
|
if err != 0 {
|
|
|
|
return 0, Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return t, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
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)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return t, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpfPromisc(fd, m int) error {
|
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func FlushBpf(fd int) error {
|
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type ivalue struct {
|
|
|
|
name [IFNAMSIZ]byte
|
|
|
|
value int16
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfInterface(fd int, name string) (string, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var iv ivalue
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
|
|
|
|
if err != 0 {
|
|
|
|
return "", Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return name, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpfInterface(fd int, name string) error {
|
2011-06-14 09:28:36 -06:00
|
|
|
var iv ivalue
|
|
|
|
copy(iv.name[:], []byte(name))
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfTimeout(fd int) (*Timeval, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var tv Timeval
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
|
|
|
|
if err != 0 {
|
|
|
|
return nil, Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return &tv, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpfTimeout(fd int, tv *Timeval) error {
|
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfStats(fd int) (*BpfStat, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var s BpfStat
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
|
|
|
|
if err != 0 {
|
|
|
|
return nil, Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return &s, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpfImmediate(fd, m int) error {
|
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpf(fd int, i []BpfInsn) error {
|
2011-06-14 09:28:36 -06:00
|
|
|
var p BpfProgram
|
|
|
|
p.Len = uint32(len(i))
|
|
|
|
p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func CheckBpfVersion(fd int) error {
|
2011-06-14 09:28:36 -06:00
|
|
|
var v BpfVersion
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
|
|
|
|
return EINVAL
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func BpfHeadercmpl(fd int) (int, error) {
|
2011-06-14 09:28:36 -06:00
|
|
|
var f int
|
2011-11-13 20:44:52 -07:00
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
|
|
|
|
if err != 0 {
|
|
|
|
return 0, Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return f, nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func SetBpfHeadercmpl(fd, f int) error {
|
|
|
|
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))
|
|
|
|
if err != 0 {
|
|
|
|
return Errno(err)
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil
|
2011-06-14 09:28:36 -06:00
|
|
|
}
|