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