1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin freebsd netbsd openbsd
7 // Berkeley packet filter for BSD variants
15 func BpfStmt(code, k int) *BpfInsn {
16 return &BpfInsn{Code: uint16(code), K: uint32(k)}
19 func BpfJump(code, k, jt, jf int) *BpfInsn {
20 return &BpfInsn{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
23 func BpfBuflen(fd int) (int, error) {
25 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
32 func SetBpfBuflen(fd, l int) (int, error) {
33 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSBLEN, uintptr(unsafe.Pointer(&l)))
40 func BpfDatalink(fd int) (int, error) {
42 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
49 func SetBpfDatalink(fd, t int) (int, error) {
50 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSDLT, uintptr(unsafe.Pointer(&t)))
57 func SetBpfPromisc(fd, m int) error {
58 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
65 func FlushBpf(fd int) error {
66 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
78 func BpfInterface(fd int, name string) (string, error) {
80 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
87 func SetBpfInterface(fd int, name string) error {
89 copy(iv.name[:], []byte(name))
90 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
97 func BpfTimeout(fd int) (*Timeval, error) {
99 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
101 return nil, Errno(err)
106 func SetBpfTimeout(fd int, tv *Timeval) error {
107 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
114 func BpfStats(fd int) (*BpfStat, error) {
116 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
118 return nil, Errno(err)
123 func SetBpfImmediate(fd, m int) error {
124 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
131 func SetBpf(fd int, i []BpfInsn) error {
133 p.Len = uint32(len(i))
134 p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
135 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
142 func CheckBpfVersion(fd int) error {
144 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
148 if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
154 func BpfHeadercmpl(fd int) (int, error) {
156 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
163 func SetBpfHeadercmpl(fd, f int) error {
164 _, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))