-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_openbsd.go
213 lines (184 loc) · 5.63 KB
/
interface_openbsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// {{{ Copyright (c) Paul R. Tagliamonte <paul@k3xec.com>, 2022
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
//go:build openbsd
package tap
import (
"fmt"
"net"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
const (
// IFDESCRSIZE is the max size of the Description field on OpenBSD.
// Replace this once syscall or x/sys/unix grows IFDESCRSIZE.
IFDESCRSIZE int = 64
)
type ifreqDescription struct {
Name [syscall.IFNAMSIZ]byte
Description *[IFDESCRSIZE]byte
}
// SetDescription will set the network interface description to the provided
// string.
func (i Interface) SetDescription(descr string) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
d := [IFDESCRSIZE]byte{}
copy(d[:], descr)
req := ifreqDescription{
Name: i.name,
Description: &d,
}
if err := ioctl(syscall.SIOCSIFDESCR, file.Fd(), uintptr(unsafe.Pointer(&req))); err != nil {
return err
}
return nil
}
type ifreqFlags struct {
Name [syscall.IFNAMSIZ]byte
Flags uint16
}
// SetUp will set the link state to up or down.
func (i Interface) SetUp(updown bool) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
req := ifreqFlags{Name: i.name}
if err := ioctl(syscall.SIOCGIFFLAGS, file.Fd(), uintptr(unsafe.Pointer(&req))); err != nil {
return err
}
if updown {
// Bring the iface up; so let's set the bit and let the syscall
// roll. This is a noop if the interface is already up.
req.Flags |= syscall.IFF_UP
} else {
// Bring the interface down; let's create a bitmask of the flags
// without the IFF_UP flag, and send that back in. This is a
// noop if the interface is already down.
req.Flags &= (^uint16(syscall.IFF_UP))
}
if err := ioctl(syscall.SIOCSIFFLAGS, file.Fd(), uintptr(unsafe.Pointer(&req))); err != nil {
return err
}
return nil
}
type ifreqMTU struct {
Name [syscall.IFNAMSIZ]byte
MTU int32
}
// SetMTU will set the MTU for the created TAP interface.
func (i Interface) SetMTU(sz uint) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
req := ifreqMTU{Name: i.name, MTU: int32(sz)}
return ioctl(syscall.SIOCSIFMTU, file.Fd(), uintptr(unsafe.Pointer(&req)))
}
type ifreqLLADDR struct {
Name [syscall.IFNAMSIZ]byte
Addr unix.RawSockaddr
}
// SetHardwareAddr will set the MAC Address (LLADDR) of the underlying
// TAP interface.
func (i Interface) SetHardwareAddr(addr net.HardwareAddr) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
req := ifreqLLADDR{Name: i.name}
// Awkwardly, this is not a RawSockaddrDatalink, this is a RawSockaddr
// with a MAC in the payload. Soooo, cool cool. Let's just do that
// here.
req.Addr = unix.RawSockaddr{
Len: 6,
Family: unix.AF_LINK,
}
// Doubly annoying is a RawSockaddr is a []int8 (wat) not a []uint8,
// so let's cast this away since this is... wrong, and the kernel
// will understand this memory as a []byte, not as a []int. This makes
// debugging and interface code a lot harder, but it's possible
// if we escape the typesystem.
//
// This happens (I think) because the OpenBSD struct is a [14]char,
// which is a [14]int8.
//
// struct sockaddr {
// u_int8_t sa_len;
// sa_family_t sa_family;
// char sa_data[14];
// };
//
buf := (*[14]byte)(unsafe.Pointer(&(req.Addr.Data[0])))
copy(buf[:6], addr)
return ioctl(syscall.SIOCSIFLLADDR, file.Fd(), uintptr(unsafe.Pointer(&req)))
}
type ifreqGroup struct {
Name [syscall.IFNAMSIZ]byte
GroupLen uint64
Group [syscall.IFNAMSIZ]byte
}
func newGroupReq(name [syscall.IFNAMSIZ]byte, group string) (*ifreqGroup, error) {
if group == "" {
return nil, fmt.Errorf("tap: interface group is empty")
}
// TODO: check if the group ends with a digit (not allowed)
if len(group) >= syscall.IFNAMSIZ {
return nil, fmt.Errorf("tap: interface group name is too long")
}
req := ifreqGroup{Name: name}
copy(req.Group[:], []byte(group))
return &req, nil
}
// AddGroup will add the provided group name to the underlying TAP
// interface.
func (i Interface) AddGroup(group string) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
req, err := newGroupReq(i.name, group)
if err != nil {
return err
}
return ioctl(syscall.SIOCAIFGROUP, file.Fd(), uintptr(unsafe.Pointer(req)))
}
// RemoveGroup will remove the group name from the TAP interface.
func (i Interface) RemoveGroup(group string) error {
file, err := afInet()
if err != nil {
return err
}
defer file.Close()
req, err := newGroupReq(i.name, group)
if err != nil {
return err
}
return ioctl(syscall.SIOCDIFGROUP, file.Fd(), uintptr(unsafe.Pointer(req)))
}
// vim: foldmethod=marker