github.com/m10x/go/src@v0.0.0-20220112094212-ba61592315da/syscall/bpf_bsd.go (about)

     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.
     4  
     5  //go:build dragonfly || freebsd || netbsd || openbsd
     6  
     7  // Berkeley packet filter for BSD variants
     8  
     9  package syscall
    10  
    11  import (
    12  	"unsafe"
    13  )
    14  
    15  // Deprecated: Use golang.org/x/net/bpf instead.
    16  func BpfStmt(code, k int) *BpfInsn {
    17  	return &BpfInsn{Code: uint16(code), K: uint32(k)}
    18  }
    19  
    20  // Deprecated: Use golang.org/x/net/bpf instead.
    21  func BpfJump(code, k, jt, jf int) *BpfInsn {
    22  	return &BpfInsn{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
    23  }
    24  
    25  // Deprecated: Use golang.org/x/net/bpf instead.
    26  func BpfBuflen(fd int) (int, error) {
    27  	var l int
    28  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
    29  	if err != 0 {
    30  		return 0, Errno(err)
    31  	}
    32  	return l, nil
    33  }
    34  
    35  // Deprecated: Use golang.org/x/net/bpf instead.
    36  func SetBpfBuflen(fd, l int) (int, error) {
    37  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSBLEN, uintptr(unsafe.Pointer(&l)))
    38  	if err != 0 {
    39  		return 0, Errno(err)
    40  	}
    41  	return l, nil
    42  }
    43  
    44  // Deprecated: Use golang.org/x/net/bpf instead.
    45  func BpfDatalink(fd int) (int, error) {
    46  	var t int
    47  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
    48  	if err != 0 {
    49  		return 0, Errno(err)
    50  	}
    51  	return t, nil
    52  }
    53  
    54  // Deprecated: Use golang.org/x/net/bpf instead.
    55  func SetBpfDatalink(fd, t int) (int, error) {
    56  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSDLT, uintptr(unsafe.Pointer(&t)))
    57  	if err != 0 {
    58  		return 0, Errno(err)
    59  	}
    60  	return t, nil
    61  }
    62  
    63  // Deprecated: Use golang.org/x/net/bpf instead.
    64  func SetBpfPromisc(fd, m int) error {
    65  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
    66  	if err != 0 {
    67  		return Errno(err)
    68  	}
    69  	return nil
    70  }
    71  
    72  // Deprecated: Use golang.org/x/net/bpf instead.
    73  func FlushBpf(fd int) error {
    74  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
    75  	if err != 0 {
    76  		return Errno(err)
    77  	}
    78  	return nil
    79  }
    80  
    81  type ivalue struct {
    82  	name  [IFNAMSIZ]byte
    83  	value int16
    84  }
    85  
    86  // Deprecated: Use golang.org/x/net/bpf instead.
    87  func BpfInterface(fd int, name string) (string, error) {
    88  	var iv ivalue
    89  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
    90  	if err != 0 {
    91  		return "", Errno(err)
    92  	}
    93  	return name, nil
    94  }
    95  
    96  // Deprecated: Use golang.org/x/net/bpf instead.
    97  func SetBpfInterface(fd int, name string) error {
    98  	var iv ivalue
    99  	copy(iv.name[:], []byte(name))
   100  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
   101  	if err != 0 {
   102  		return Errno(err)
   103  	}
   104  	return nil
   105  }
   106  
   107  // Deprecated: Use golang.org/x/net/bpf instead.
   108  func BpfTimeout(fd int) (*Timeval, error) {
   109  	var tv Timeval
   110  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
   111  	if err != 0 {
   112  		return nil, Errno(err)
   113  	}
   114  	return &tv, nil
   115  }
   116  
   117  // Deprecated: Use golang.org/x/net/bpf instead.
   118  func SetBpfTimeout(fd int, tv *Timeval) error {
   119  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
   120  	if err != 0 {
   121  		return Errno(err)
   122  	}
   123  	return nil
   124  }
   125  
   126  // Deprecated: Use golang.org/x/net/bpf instead.
   127  func BpfStats(fd int) (*BpfStat, error) {
   128  	var s BpfStat
   129  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
   130  	if err != 0 {
   131  		return nil, Errno(err)
   132  	}
   133  	return &s, nil
   134  }
   135  
   136  // Deprecated: Use golang.org/x/net/bpf instead.
   137  func SetBpfImmediate(fd, m int) error {
   138  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
   139  	if err != 0 {
   140  		return Errno(err)
   141  	}
   142  	return nil
   143  }
   144  
   145  // Deprecated: Use golang.org/x/net/bpf instead.
   146  func SetBpf(fd int, i []BpfInsn) error {
   147  	var p BpfProgram
   148  	p.Len = uint32(len(i))
   149  	p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
   150  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
   151  	if err != 0 {
   152  		return Errno(err)
   153  	}
   154  	return nil
   155  }
   156  
   157  // Deprecated: Use golang.org/x/net/bpf instead.
   158  func CheckBpfVersion(fd int) error {
   159  	var v BpfVersion
   160  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
   161  	if err != 0 {
   162  		return Errno(err)
   163  	}
   164  	if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
   165  		return EINVAL
   166  	}
   167  	return nil
   168  }
   169  
   170  // Deprecated: Use golang.org/x/net/bpf instead.
   171  func BpfHeadercmpl(fd int) (int, error) {
   172  	var f int
   173  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
   174  	if err != 0 {
   175  		return 0, Errno(err)
   176  	}
   177  	return f, nil
   178  }
   179  
   180  // Deprecated: Use golang.org/x/net/bpf instead.
   181  func SetBpfHeadercmpl(fd, f int) error {
   182  	_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))
   183  	if err != 0 {
   184  		return Errno(err)
   185  	}
   186  	return nil
   187  }