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