-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
310 lines (287 loc) · 9.81 KB
/
conf.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"fmt"
"net"
"net/netip"
"strconv"
"strings"
"time"
"github.com/hujun-open/etherconn"
"github.com/hujun-open/shouchan"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv6"
)
func init() {
shouchan.Register[dhcpv4.Option](d4OptionToStr, d4OptionFromStr)
shouchan.Register[dhcpv6.OptionGeneric](d6OptionToStr, d6OptionFromStr)
shouchan.Register[dhcpv6.MessageType](d6MsgTypeToStr, d6MsgTypeFromStr)
}
type testSetup struct {
Ifname string `alias:"i" usage:"interface name"`
NumOfClients uint `alias:"n" usage:"number of clients"`
StartMAC net.HardwareAddr `alias:"mac" usage:"starting MAC address"`
MacStep uint `usage:"amount of increase between two consecutive MAC address"`
StartVLANs etherconn.VLANs `alias:"vlan" usage:"starting VLAN ID, Dot1Q or QinQ"`
VLANEType uint `usage:"EthernetType for the vlan tag" base:"16"`
VLANStep uint `usage:"amount of increase between two consecutive VLAN ID"`
ExcludedVLANs []uint16 `usage:"a list of excluded VLAN IDs"`
Interval time.Duration `usage:"interval between setup of sessions"`
CustomV4Option dhcpv4.Option `usage:"custom DHCPv4 option, code:value format"`
CustomV6Option dhcpv6.OptionGeneric `usage:"custom DHCPv6 option, code:value format"`
v4Options []dhcpv4.Option
v6Options dhcpv6.Options //non-relay specific options
Debug bool `alias:"d" usage:"enable debug output"`
SaveLease bool `usage:"save the lease if true"`
ApplyLease bool `usage:"apply assigned address on the interface if true"`
Retry uint `usage:"number of setup retry"`
Timeout time.Duration `usage:"setup timout"`
GiAddr netip.Addr `usage:"Gi address for DHCPv4, simulating relay agent"`
SourceV4Addr netip.Addr `usage:"source address for DHCPv4" alias:"srcv4"`
SourceV6Port uint16 `usage:"source port for egress DHCPv6 message" alias:"srcv6port"`
SourceV4Port uint16 `usage:"source port for egress DHCPv4 message" alias:"srcv4port"`
//following are template str, $ID will be replaced by client id
RID string `usage:"BBF remote-id"`
CID string `usage:"BBF circuit-id"`
ClntID string `usage:"client-id"`
VendorClass string `usage:"vendor class"`
EnableV4 bool `alias:"v4" usage:"do DHCPv4 if true"`
//v6 specific
EnableV6 bool `alias:"v6" usage:"do DHCPv6 if true"`
SourceV6Addr netip.Addr `usage:"source address for DHCPv6" alias:"srcv6"`
StackDelay time.Duration `usage:"delay between setup v4 and v6, postive value means setup v4 first, negative means v6 first"`
V6MsgType dhcpv6.MessageType `usage:"DHCPv6 exchange type, solict|relay|auto"`
NeedNA bool `usage:"request DHCPv6 IANA if true"`
NeedPD bool `usage:"request DHCPv6 IAPD if true"`
pktRelay etherconn.PacketRelay
Driver etherconn.RelayType `usage:"etherconn forward engine"`
Flapping *FlappingConf `usage:"enable flapping"`
SendRSFirst bool `usage:"send Router Solict first if true"`
Profiling bool `usage:"enable profiling, dev use only"`
LeaseFile string
Action actionType `usage:"dora | release | renew | rebind"`
saveV4Chan chan *v4LeaseWithID
saveV6Chan chan *v6LeaseWithID
}
func newDefaultConf() *testSetup {
return &testSetup{
Action: actionDORA,
NumOfClients: 1,
StartMAC: []byte{},
MacStep: 1,
VLANEType: etherconn.DefaultVLANEtype,
VLANStep: 1,
Interval: time.Second,
GiAddr: netip.MustParseAddr("0.0.0.0"),
SourceV4Addr: netip.MustParseAddr("0.0.0.0"),
SourceV6Addr: netip.MustParseAddr("::"),
Retry: 1,
Timeout: 5 * time.Second,
EnableV4: true,
EnableV6: false,
SourceV6Port: dhcpv6.DefaultClientPort,
SourceV4Port: dhcpv4.ClientPort,
NeedNA: true,
V6MsgType: dhcpv6.MessageTypeNone,
Driver: etherconn.RelayTypeAFP,
LeaseFile: "dhcplt.lease",
Flapping: &FlappingConf{
FlapNum: 0,
MinInterval: defaultMinFlapInt,
MaxInterval: defualtMaxFlapInt,
StayDownDur: 10 * time.Second,
},
}
}
func (setup *testSetup) excluded(vids []uint16) bool {
for _, vid := range vids {
for _, extv := range setup.ExcludedVLANs {
if extv == vid {
return true
}
}
}
return false
}
const saveChanDepth = 8
func (setup *testSetup) init() error {
if setup.Ifname == "" {
return fmt.Errorf("interface name can't be empty")
}
if setup.NumOfClients <= 0 {
return fmt.Errorf("number of clients can't be zero")
}
iff, err := net.InterfaceByName(setup.Ifname)
if err != nil {
return fmt.Errorf("can't find interface %v,%w", setup.Ifname, err)
}
if len(setup.StartMAC) == 0 {
setup.StartMAC = iff.HardwareAddr
}
if !setup.EnableV4 && !setup.EnableV6 {
return fmt.Errorf("both DHCPv4 and DHCPv6 are disabled")
}
if setup.SourceV4Port == 0 {
return fmt.Errorf("source v4 port can't be zero")
}
if setup.SourceV6Port == 0 {
return fmt.Errorf("source v6 port can't be zero")
}
if setup.EnableV4 {
if !setup.SourceV4Addr.IsUnspecified() {
if !setup.SourceV4Addr.Is4() || !setup.SourceV4Addr.IsGlobalUnicast() {
return fmt.Errorf("source v4 address must be an IPv4 unicast addr")
}
}
if setup.GiAddr.IsValid() {
if !setup.GiAddr.IsUnspecified() {
if !setup.GiAddr.Is4() || !setup.GiAddr.IsGlobalUnicast() {
return fmt.Errorf("gi address must be an IPv4 unicast addr")
}
}
} else {
setup.GiAddr = netip.MustParseAddr("0.0.0.0")
}
if setup.GiAddr.IsUnspecified() != setup.SourceV4Addr.IsUnspecified() {
fmt.Printf("warning: giaddr should be specified along with srcv4 address")
}
}
if setup.EnableV6 {
if !setup.SourceV6Addr.IsUnspecified() {
if !setup.SourceV6Addr.Is6() || !setup.SourceV6Addr.IsGlobalUnicast() {
return fmt.Errorf("source v6 address must be an IPv4 unicast addr")
}
}
}
if setup.NumOfClients == 0 {
return fmt.Errorf("number of client is 0")
}
for _, v := range setup.StartVLANs {
v.EtherType = uint16(setup.VLANEType)
}
setup.ExcludedVLANs = []uint16{}
for _, n := range setup.ExcludedVLANs {
if n > 4096 {
return fmt.Errorf("%v is not valid vlan number", n)
}
setup.ExcludedVLANs = append(setup.ExcludedVLANs, n)
}
if setup.VendorClass != "" {
setup.v4Options = append(setup.v4Options, dhcpv4.OptClassIdentifier(setup.VendorClass))
setup.v6Options.Add(&dhcpv6.OptVendorClass{
EnterpriseNumber: BBFEnterpriseNumber,
Data: [][]byte{[]byte(setup.VendorClass)},
})
}
if setup.CustomV4Option.Code != nil {
setup.v4Options = append(setup.v4Options, setup.CustomV4Option)
}
if setup.CustomV6Option.OptionCode != 0 {
setup.v6Options = append(setup.v6Options, &setup.CustomV6Option)
}
if setup.V6MsgType == dhcpv6.MessageTypeNone {
if setup.RID != "" || setup.CID != "" {
setup.V6MsgType = dhcpv6.MessageTypeRelayForward
} else {
setup.V6MsgType = dhcpv6.MessageTypeSolicit
}
}
setup.pktRelay, err = createPktRelay(setup)
if err != nil {
return err
}
if setup.Flapping.FlapNum > int(setup.NumOfClients) {
return fmt.Errorf("flapping number %d can't be bigger than client number %d", setup.Flapping.FlapNum, setup.NumOfClients)
}
if setup.Flapping.MinInterval > setup.Flapping.MaxInterval {
return fmt.Errorf("minimal flapping interval %v is bigger than max value %v", setup.Flapping.MinInterval, setup.Flapping.MaxInterval)
}
if setup.SaveLease || setup.Action == actionRelease {
if setup.EnableV4 {
setup.saveV4Chan = make(chan *v4LeaseWithID, saveChanDepth)
}
if setup.EnableV6 {
setup.saveV6Chan = make(chan *v6LeaseWithID, saveChanDepth)
}
}
return nil
}
func parseD4CustomOptionStr(coptStr string) (dhcpv4.Option, error) {
strList := strings.SplitN(coptStr, ":", 2)
if len(strList) < 2 {
return dhcpv4.Option{}, fmt.Errorf("invalid custom option %v", coptStr)
}
var oid int
var err error
if oid, err = strconv.Atoi(strList[0]); err != nil {
return dhcpv4.Option{}, fmt.Errorf("%v is not a number", strList[0])
}
return dhcpv4.Option{
Code: dhcpv4.GenericOptionCode(oid),
Value: dhcpv4.OptionGeneric{
Data: []byte(strList[1]),
},
}, nil
}
func parseD6CustomOptionStr(coptStr string) (dhcpv6.Option, error) {
strList := strings.SplitN(coptStr, ":", 2)
if len(strList) < 2 {
return nil, fmt.Errorf("invalid custom option %v", coptStr)
}
var oid int
var err error
if oid, err = strconv.Atoi(strList[0]); err != nil {
return nil, fmt.Errorf("%v is not a number", strList[0])
}
return &dhcpv6.OptionGeneric{
OptionCode: dhcpv6.OptionCode(oid),
OptionData: []byte(strList[1]),
}, nil
}
func d4OptionFromStr(text string) (any, error) {
if text == "" {
return dhcpv4.Option{
Code: nil,
}, nil
}
return parseD4CustomOptionStr(text)
}
func d4OptionToStr(in any) (string, error) {
v := in.(dhcpv4.Option)
if v.Code == nil {
return "", nil
}
return fmt.Sprintf("%d:%v", v.Code.Code(), v.Value.String()), nil
}
func d6OptionFromStr(text string) (any, error) {
if text == "" {
return dhcpv6.OptionGeneric{
OptionCode: 0,
}, nil
}
return parseD6CustomOptionStr(text)
}
func d6OptionToStr(in any) (string, error) {
v := in.(dhcpv6.OptionGeneric)
if v.OptionCode == 0 {
return "", nil
}
return fmt.Sprintf("%d:%v", v.OptionCode, string(v.OptionData)), nil
}
func d6MsgTypeFromStr(text string) (any, error) {
switch strings.ToLower(text) {
case "solicit":
return dhcpv6.MessageTypeSolicit, nil
case "relay":
return dhcpv6.MessageTypeRelayForward, nil
case "auto":
return dhcpv6.MessageTypeNone, nil
}
return nil, fmt.Errorf("unsupported DHCPv6 type: %v", text)
}
func d6MsgTypeToStr(in any) (string, error) {
v := in.(dhcpv6.MessageType)
if v == dhcpv6.MessageTypeNone {
return "auto", nil
}
return strings.ToLower(v.String()), nil
}