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