-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpull_options.go
More file actions
889 lines (853 loc) · 26.9 KB
/
Copy pathpull_options.go
File metadata and controls
889 lines (853 loc) · 26.9 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
package openvpn
import (
"math"
"net/netip"
"slices"
"strconv"
"strings"
"time"
E "github.com/sagernet/sing/common/exceptions"
)
const (
pushRequestPayload = "PUSH_REQUEST"
legacyPullRequestPayload = "PULL_REQUEST"
pushReplyPayloadPrefix = "PUSH_REPLY"
pushUpdatePayloadPrefix = "PUSH_UPDATE"
)
type pushedOptionParseError struct {
Name string
Value string
Err error
}
type pushedExcludedRoute struct {
Name string
Value string
Route TunnelRoute
}
type pushedPingTimeoutAction uint8
const (
pushedPingTimeoutNone pushedPingTimeoutAction = iota
pushedPingTimeoutRestart
pushedPingTimeoutExit
)
type wirePushedOptions struct {
Topology string
TunMTU uint32
Ifconfig string
IfconfigIPv6 string
RouteGateway string
Route []string
RouteIPv6 []string
DNS []string
DHCPOptions []string
BlockIPv6 bool
BlockOutsideDNS bool
RedirectGateway bool
RedirectGatewayFlags []string
RedirectPrivate bool
RouteMetric int
RouteMetricSet bool
PingInterval time.Duration
PingIntervalEnabled bool
PingRestart time.Duration
PingRestartEnabled bool
AuthToken string
AuthTokenUser string
PeerID *uint32
SelectedCipher string
SelectedAuth string
ProtocolFlags []string
KeyDerivation string
ExplicitExitNotify uint32
ExplicitExitNotifySet bool
CompressionDirectives []compressionDirective
InactiveTimeout time.Duration
InactiveMinimumBytes uint64
InactiveTimeoutSet bool
SessionTimeout time.Duration
SessionTimeoutSet bool
PingExit time.Duration
PingExitSet bool
PingTimeoutAction pushedPingTimeoutAction
PingTimerRemote bool
}
type pushOptionsKind int
const (
pushOptionsKindReply pushOptionsKind = iota
pushOptionsKindUpdate
)
const peerIDMaxValue = (1 << 24) - 1
// Upstream process_incoming_push_msg (push.c) hands the payload that follows
// "PUSH_REPLY," to apply_push_options (options.c), which cuts it into option
// lines with buf_parse(buf, ','): the comma is a raw separator and carries no
// escape of any kind.
func splitPushReplyPayloadLines(payload []byte) (string, []string, bool) {
payloadValue := normalizeControlPayload(payload)
if payloadValue == "" {
return "", nil, false
}
payloadLines := strings.Split(payloadValue, ",")
commandName := strings.TrimSpace(payloadLines[0])
if !strings.EqualFold(commandName, pushReplyPayloadPrefix) &&
!strings.EqualFold(commandName, pushUpdatePayloadPrefix) {
return "", nil, false
}
return commandName, payloadLines[1:], true
}
func appendPushReplyPayloadSegment(accumulatedLines []string, payload []byte) ([]string, int, bool) {
commandName, payloadLines, decoded := splitPushReplyPayloadLines(payload)
if !decoded {
return accumulatedLines, 0, false
}
if len(accumulatedLines) == 0 {
accumulatedLines = append(accumulatedLines, commandName)
}
continuation := 0
for _, optionLine := range payloadLines {
parameters, parsed := parsePushedOptionLine(optionLine)
if parsed && len(parameters) >= 2 && strings.EqualFold(parameters[0], "push-continuation") {
continuationValue, err := strconv.Atoi(parameters[1])
if err == nil && continuationValue >= 0 && continuationValue <= 2 {
continuation = continuationValue
}
}
accumulatedLines = append(accumulatedLines, optionLine)
}
return accumulatedLines, continuation, true
}
func decodePushReplyPayloadWithFilters(payload []byte, remoteHost netip.Addr, filters []PullFilter) (pushedOptions, int, bool) {
commandName, payloadLines, decoded := splitPushReplyPayloadLines(payload)
if !decoded {
return pushedOptions{}, 0, false
}
options, continuation := decodePushReplyOptionLines(commandName, payloadLines, remoteHost, filters)
return options, continuation, true
}
func decodePushReplyOptionLines(commandName string, optionLines []string, remoteHost netip.Addr, filters []PullFilter) (pushedOptions, int) {
kind := pushOptionsKindReply
if strings.EqualFold(commandName, pushUpdatePayloadPrefix) {
kind = pushOptionsKindUpdate
}
var wireOptions wirePushedOptions
var continuation int
var pullFilterRejection string
for _, payloadLine := range optionLines {
optionLine := strings.TrimLeft(payloadLine, " \t\r\n\v\f")
allowed, rejected := applyPullFilters(filters, optionLine)
if rejected {
pullFilterRejection = optionLine
break
}
if !allowed {
continue
}
parameters, parsed := parsePushedOptionLine(payloadLine)
if !parsed {
continue
}
optionName := parameters[0]
optionValue := strings.Join(parameters[1:], " ")
switch strings.ToLower(optionName) {
case "topology":
if optionValue != "" {
wireOptions.Topology = optionValue
}
case "tun-mtu":
tunMTUValue := parsePushedPositiveInteger(optionValue)
if tunMTUValue > 0 {
wireOptions.TunMTU = uint32(tunMTUValue)
}
case "ifconfig":
if optionValue != "" {
wireOptions.Ifconfig = optionValue
}
case "ifconfig-ipv6":
if optionValue != "" {
wireOptions.IfconfigIPv6 = optionValue
}
case "route":
if optionValue != "" {
wireOptions.Route = append(wireOptions.Route, optionValue)
}
case "route-gateway":
if optionValue != "" {
wireOptions.RouteGateway = optionValue
}
case "route-ipv6":
if optionValue != "" {
wireOptions.RouteIPv6 = append(wireOptions.RouteIPv6, optionValue)
}
case "dns":
if optionValue != "" {
wireOptions.DNS = append(wireOptions.DNS, optionValue)
}
case "dhcp-option":
if optionValue != "" {
wireOptions.DHCPOptions = append(wireOptions.DHCPOptions, optionValue)
}
case "block-ipv6":
wireOptions.BlockIPv6 = true
case "block-outside-dns":
wireOptions.BlockOutsideDNS = true
case "redirect-gateway":
wireOptions.RedirectGateway = true
if optionValue != "" {
wireOptions.RedirectGatewayFlags = strings.Fields(optionValue)
}
case "redirect-private":
wireOptions.RedirectPrivate = true
if optionValue != "" {
wireOptions.RedirectGatewayFlags = strings.Fields(optionValue)
}
case "route-metric":
if optionValue != "" {
wireOptions.RouteMetric = int(parsePushedPositiveInteger(optionValue))
wireOptions.RouteMetricSet = true
}
case "ping":
if optionValue != "" {
wireOptions.PingInterval = parsePushedSecondCount(optionValue)
wireOptions.PingIntervalEnabled = true
}
case "ping-restart":
if optionValue != "" {
wireOptions.PingRestart = parsePushedSecondCount(optionValue)
wireOptions.PingRestartEnabled = true
wireOptions.PingTimeoutAction = pushedPingTimeoutRestart
}
case "auth-token":
wireOptions.AuthToken = optionValue
case "auth-token-user":
wireOptions.AuthTokenUser = optionValue
case "peer-id":
peerIDValue, err := strconv.ParseUint(strings.TrimSpace(optionValue), 10, 32)
if err == nil && peerIDValue <= peerIDMaxValue {
peerIDCopy := uint32(peerIDValue)
wireOptions.PeerID = &peerIDCopy
}
case "cipher":
if optionValue != "" {
wireOptions.SelectedCipher = optionValue
}
case "auth":
if optionValue != "" {
wireOptions.SelectedAuth = optionValue
}
case "protocol-flags":
if optionValue != "" {
wireOptions.ProtocolFlags = strings.Fields(optionValue)
}
case "key-derivation":
if optionValue != "" {
wireOptions.KeyDerivation = strings.ToLower(strings.TrimSpace(optionValue))
}
case "explicit-exit-notify":
if optionValue == "" {
wireOptions.ExplicitExitNotify = 1
} else {
wireOptions.ExplicitExitNotify = uint32(parsePushedPositiveInteger(optionValue))
}
wireOptions.ExplicitExitNotifySet = true
case compressionDirectiveCompress, compressionDirectiveLZO:
wireOptions.CompressionDirectives = append(wireOptions.CompressionDirectives, compressionDirective{
Name: strings.ToLower(optionName),
Value: strings.TrimSpace(optionValue),
})
case "inactive":
inactiveFields := strings.Fields(optionValue)
if len(inactiveFields) >= 1 {
wireOptions.InactiveTimeout = parsePushedSecondCount(inactiveFields[0])
wireOptions.InactiveTimeoutSet = true
if len(inactiveFields) >= 2 {
// Upstream parse_inactive (options.c) clamps negative
// minimum-bytes to 0.
minimumBytes, minimumBytesErr := strconv.ParseInt(inactiveFields[1], 10, 64)
if minimumBytesErr == nil && minimumBytes > 0 {
wireOptions.InactiveMinimumBytes = uint64(minimumBytes)
}
}
}
case "session-timeout":
if optionValue != "" {
wireOptions.SessionTimeout = parsePushedSecondCount(optionValue)
wireOptions.SessionTimeoutSet = true
}
case "ping-exit":
if optionValue != "" {
wireOptions.PingExit = parsePushedSecondCount(optionValue)
wireOptions.PingExitSet = true
wireOptions.PingTimeoutAction = pushedPingTimeoutExit
}
case "ping-timer-rem":
wireOptions.PingTimerRemote = true
case "push-continuation":
continuationValue, err := strconv.Atoi(optionValue)
if err == nil && continuationValue >= 0 && continuationValue <= 2 {
continuation = continuationValue
}
}
}
options := pushedOptionsFromWire(wireOptions, remoteHost)
options.kind = kind
options.pullFilterRejection = pullFilterRejection
return options, continuation
}
// Upstream reads these pushed values with positive_atoi (options.c): atoi()
// with a negative result replaced by zero. atoi() is (int)strtol(), so it skips
// leading whitespace, stops at the first character the number does not cover,
// saturates at LONG_MIN/LONG_MAX and keeps only the low 32 bits of what strtol
// returned.
func parsePushedPositiveInteger(value string) int32 {
text := strings.TrimLeft(value, " \t\n\v\f\r")
numberEnd := 0
if numberEnd < len(text) && (text[numberEnd] == '+' || text[numberEnd] == '-') {
numberEnd++
}
digitStart := numberEnd
for numberEnd < len(text) && text[numberEnd] >= '0' && text[numberEnd] <= '9' {
numberEnd++
}
if numberEnd == digitStart {
return 0
}
parsed, err := strconv.ParseInt(text[:numberEnd], 10, 64)
if err != nil {
parsed = math.MaxInt64
if text[0] == '-' {
parsed = math.MinInt64
}
}
truncated := int32(parsed)
if truncated < 0 {
return 0
}
return truncated
}
func parsePushedSecondCount(value string) time.Duration {
return time.Duration(parsePushedPositiveInteger(value)) * time.Second
}
func applyPullFilters(filters []PullFilter, optionLine string) (bool, bool) {
for _, filter := range filters {
if !strings.HasPrefix(optionLine, filter.Text) {
continue
}
switch filter.Action {
case "accept":
return true, false
case "ignore":
return false, false
case "reject":
return false, true
}
}
return true, false
}
func pushedOptionsFromWire(wireOptions wirePushedOptions, remoteHost netip.Addr) pushedOptions {
options := pushedOptions{
Topology: wireOptions.Topology,
TunMTU: wireOptions.TunMTU,
DHCPOptions: slices.Clone(wireOptions.DHCPOptions),
BlockIPv6: wireOptions.BlockIPv6,
BlockOutsideDNS: wireOptions.BlockOutsideDNS,
RedirectGateway: wireOptions.RedirectGateway,
RedirectGatewayFlags: slices.Clone(wireOptions.RedirectGatewayFlags),
RedirectPrivate: wireOptions.RedirectPrivate,
RouteMetric: wireOptions.RouteMetric,
RouteMetricSet: wireOptions.RouteMetricSet,
PingInterval: wireOptions.PingInterval,
PingIntervalEnabled: wireOptions.PingIntervalEnabled,
PingRestart: wireOptions.PingRestart,
PingRestartEnabled: wireOptions.PingRestartEnabled,
AuthToken: wireOptions.AuthToken,
AuthTokenUser: wireOptions.AuthTokenUser,
PeerID: wireOptions.PeerID,
SelectedCipher: wireOptions.SelectedCipher,
SelectedAuth: wireOptions.SelectedAuth,
ProtocolFlags: slices.Clone(wireOptions.ProtocolFlags),
KeyDerivation: wireOptions.KeyDerivation,
ExplicitExitNotify: wireOptions.ExplicitExitNotify,
ExplicitExitNotifySet: wireOptions.ExplicitExitNotifySet,
CompressionDirectives: slices.Clone(wireOptions.CompressionDirectives),
InactiveTimeout: wireOptions.InactiveTimeout,
InactiveMinimumBytes: wireOptions.InactiveMinimumBytes,
InactiveTimeoutSet: wireOptions.InactiveTimeoutSet,
SessionTimeout: wireOptions.SessionTimeout,
SessionTimeoutSet: wireOptions.SessionTimeoutSet,
PingExit: wireOptions.PingExit,
PingExitSet: wireOptions.PingExitSet,
PingTimerRemote: wireOptions.PingTimerRemote,
}
switch wireOptions.PingTimeoutAction {
case pushedPingTimeoutRestart:
options.PingExit = 0
options.PingExitSet = false
case pushedPingTimeoutExit:
options.PingRestart = 0
options.PingRestartEnabled = false
}
if wireOptions.Ifconfig != "" {
options.addWireIfconfig(wireOptions.Ifconfig, wireOptions.Topology)
}
if wireOptions.IfconfigIPv6 != "" {
options.addWireIfconfigIPv6(wireOptions.IfconfigIPv6)
}
options.setWireRouteGateway(wireOptions.RouteGateway)
for _, value := range wireOptions.Route {
options.addWireRoute(value, remoteHost)
}
for _, value := range wireOptions.RouteIPv6 {
options.addWireRouteIPv6(value, remoteHost)
}
for _, value := range wireOptions.DNS {
options.addWireDNSOptionV2(value)
}
options.DNSServers = slices.DeleteFunc(options.DNSServers, func(server TunnelDNSServer) bool {
if len(server.Addresses) > 0 {
return false
}
options.addParseError("dns", "server "+strconv.Itoa(server.Priority), E.New("dns server has no address assigned"))
return true
})
options.modernDNS = len(options.DNSServers) > 0
for _, value := range wireOptions.DHCPOptions {
if !options.modernDNS {
options.addWireDHCPOptionDNS(value)
}
}
if options.modernDNS {
options.DHCPOptions = filterOpenVPNNonDNSDHCPOptions(options.DHCPOptions)
}
return options
}
func (options *pushedOptions) addWireIfconfig(value string, topology string) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
prefix, err := parseIfconfigPrefix(raw, topology)
if err != nil {
options.addParseError("ifconfig", raw, err)
return
}
options.LocalAddress = append(options.LocalAddress, pushedLocalAddress{
Prefix: prefix,
Peer: parseIfconfigVPNGateway([]string{raw}, topology),
Raw: raw,
})
}
func (options *pushedOptions) addWireIfconfigIPv6(value string) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
prefix, peer, err := parseIfconfigIPv6(raw)
if err != nil {
options.addParseError("ifconfig-ipv6", raw, err)
return
}
options.LocalAddress = append(options.LocalAddress, pushedLocalAddress{
Prefix: prefix,
Peer: peer,
Raw: raw,
})
}
func (options *pushedOptions) setWireRouteGateway(value string) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
options.RouteGatewayRaw = raw
fields := strings.Fields(raw)
if len(fields) == 0 {
return
}
if strings.EqualFold(fields[0], "vpn_gateway") {
options.RouteGatewayVPN = true
return
}
routeGateway, err := netip.ParseAddr(fields[0])
if err != nil {
options.addParseError("route-gateway", raw, errInvalidPushedRouteGateway)
return
}
options.RouteGateway = routeGateway
}
func (options *pushedOptions) addWireRoute(value string, remoteHost netip.Addr) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
prefix, gateway, metric, excluded, err := parsePushedRoute(raw, remoteHost)
if err != nil {
options.addParseError("route", raw, err)
return
}
if excluded {
options.addExcludedRoute("route", raw, TunnelRoute{Prefix: prefix, Metric: metric})
return
}
options.Routes = append(options.Routes, pushedRoute{
Route: TunnelRoute{
Prefix: prefix,
Gateway: gateway,
Metric: metric,
},
Raw: raw,
})
}
func (options *pushedOptions) addWireRouteIPv6(value string, remoteHost netip.Addr) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
prefix, gateway, metric, excluded, err := parsePushedRouteIPv6(raw, remoteHost)
if err != nil {
options.addParseError("route-ipv6", raw, err)
return
}
if excluded {
options.addExcludedRoute("route-ipv6", raw, TunnelRoute{Prefix: prefix, Metric: metric})
return
}
options.Routes = append(options.Routes, pushedRoute{
Route: TunnelRoute{
Prefix: prefix,
Gateway: gateway,
Metric: metric,
},
Raw: raw,
})
}
func (options *pushedOptions) addWireDNSOptionV2(value string) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
fields := strings.Fields(raw)
if len(fields) == 0 {
return
}
if strings.EqualFold(fields[0], "search-domains") {
if len(fields) < 2 {
options.addParseError("dns", raw, E.New("dns search-domains requires domain value"))
return
}
for _, domain := range fields[1:] {
if !validateTunnelDNSDomain(domain) {
options.addParseError("dns", raw, E.New("dns search domain contains invalid characters: ", domain))
continue
}
options.SearchDomains = appendUniqueStringValues(options.SearchDomains, []string{domain})
options.modernSearchDomains = appendUniqueStringValues(options.modernSearchDomains, []string{domain})
}
return
}
if !strings.EqualFold(fields[0], "server") {
return
}
if len(fields) < 3 {
options.addParseError("dns", raw, E.New("invalid dns server option: ", raw))
return
}
priority, err := strconv.Atoi(fields[1])
if err != nil {
options.addParseError("dns", raw, E.Cause(err, "parse dns server priority: ", fields[1]))
return
}
if priority < 0 || priority > 127 {
options.addParseError("dns", raw, E.New("pushed dns server priority must be between 0 and 127"))
return
}
server := options.tunnelDNSServer(priority)
switch strings.ToLower(fields[2]) {
case "address":
if len(fields) < 4 {
options.addParseError("dns", raw, E.New("dns server address requires address value"))
return
}
for _, addressValue := range fields[3:] {
if len(server.Addresses) >= maxTunnelDNSServerAddresses {
options.addParseError("dns", raw, E.New("dns server address maximum exceeded: ", addressValue))
return
}
address, parseErr := parseTunnelDNSAddress(addressValue)
if parseErr != nil {
options.addParseError("dns", raw, E.Cause(parseErr, "parse dns server address: ", addressValue))
continue
}
server.Addresses = append(server.Addresses, address)
modernAddress := pushedAddress{Address: address.Addr(), Raw: raw, OptionName: "dns"}
options.DNS = append(options.DNS, modernAddress)
options.modernDNSAddresses = append(options.modernDNSAddresses, modernAddress)
}
case "resolve-domains":
if len(fields) < 4 {
options.addParseError("dns", raw, E.New("dns server resolve-domains requires domain value"))
return
}
for _, domain := range fields[3:] {
if !validateTunnelDNSDomain(domain) {
options.addParseError("dns", raw, E.New("dns resolve domain contains invalid characters: ", domain))
continue
}
server.ResolveDomains = appendUniqueStringValues(server.ResolveDomains, []string{domain})
}
case "dnssec":
if len(fields) != 4 {
options.addParseError("dns", raw, E.New("dns server dnssec requires one value"))
return
}
dnssec := strings.ToLower(fields[3])
switch dnssec {
case "yes", "optional", "no":
server.DNSSEC = dnssec
default:
options.addParseError("dns", raw, E.New("invalid dnssec mode: ", fields[3]))
}
case "transport":
if len(fields) != 4 {
options.addParseError("dns", raw, E.New("dns server transport requires one value"))
return
}
switch fields[3] {
case "plain":
server.Transport = "plain"
case "DoT":
server.Transport = "dot"
case "DoH":
server.Transport = "doh"
default:
options.addParseError("dns", raw, E.New("invalid dns transport: ", fields[3]))
}
case "sni":
if len(fields) != 4 {
options.addParseError("dns", raw, E.New("dns server sni requires one value"))
return
}
if !validateTunnelDNSDomain(fields[3]) {
options.addParseError("dns", raw, E.New("dns server sni contains invalid characters: ", fields[3]))
return
}
server.SNI = fields[3]
default:
options.addParseError("dns", raw, E.New("unsupported dns server option: ", fields[2]))
}
}
func (options *pushedOptions) tunnelDNSServer(priority int) *TunnelDNSServer {
for i := range options.DNSServers {
if options.DNSServers[i].Priority == priority {
return &options.DNSServers[i]
}
}
options.DNSServers = append(options.DNSServers, TunnelDNSServer{Priority: priority})
return &options.DNSServers[len(options.DNSServers)-1]
}
func parseTunnelDNSAddress(value string) (netip.AddrPort, error) {
address, err := netip.ParseAddr(value)
if err == nil {
return netip.AddrPortFrom(address, 0), nil
}
addressPort, addressPortErr := netip.ParseAddrPort(value)
if addressPortErr != nil {
return netip.AddrPort{}, addressPortErr
}
if addressPort.Port() == 0 {
return netip.AddrPort{}, E.New("dns server port must not be zero")
}
return addressPort, nil
}
func (options *pushedOptions) addWireDHCPOptionDNS(value string) {
raw := strings.TrimSpace(value)
if raw == "" {
return
}
fields := strings.Fields(raw)
if len(fields) == 0 {
return
}
optionName := strings.ToUpper(fields[0])
if optionName == "DOMAIN" || optionName == "ADAPTER_DOMAIN_SUFFIX" || optionName == "DOMAIN-SEARCH" {
if len(fields) < 2 {
options.addParseError("dhcp-option", raw, E.New("dhcp-option ", optionName, " requires domain value"))
return
}
for _, domain := range fields[1:] {
if !validateTunnelDNSDomain(domain) {
options.addParseError("dhcp-option", raw, E.New("dhcp-option ", optionName, " contains invalid domain: ", domain))
continue
}
options.SearchDomains = appendUniqueStringValues(options.SearchDomains, []string{domain})
}
return
}
if optionName == "DOMAIN-ROUTE" {
if len(fields) < 2 {
options.addParseError("dhcp-option", raw, E.New("dhcp-option DOMAIN-ROUTE requires domain value"))
return
}
for _, domain := range fields[1:] {
if !validateTunnelDNSDomain(domain) {
options.addParseError("dhcp-option", raw, E.New("dhcp-option DOMAIN-ROUTE contains invalid domain: ", domain))
continue
}
options.DNSRoutes = appendUniqueStringValues(options.DNSRoutes, []string{domain})
}
return
}
if optionName != "DNS" && optionName != "DNS6" {
return
}
if len(fields) < 2 {
options.addParseError("dhcp-option", raw, E.New("dhcp-option ", optionName, " requires address value"))
return
}
for _, addressValue := range fields[1:] {
address, err := netip.ParseAddr(addressValue)
if err != nil {
options.addParseError("dhcp-option", raw, E.Cause(err, "parse dhcp-option ", optionName, " address: ", addressValue))
continue
}
if optionName == "DNS" && !address.Is4() {
options.addParseError("dhcp-option", raw, E.New("dhcp-option DNS expected IPv4 address: ", addressValue))
continue
}
if optionName == "DNS6" && !address.Is6() {
options.addParseError("dhcp-option", raw, E.New("dhcp-option DNS6 expected IPv6 address: ", addressValue))
continue
}
options.DNS = append(options.DNS, pushedAddress{Address: address, Raw: raw, OptionName: "dhcp-option"})
}
}
func filterOpenVPNNonDNSDHCPOptions(values []string) []string {
return slices.DeleteFunc(slices.Clone(values), func(value string) bool {
fields := strings.Fields(value)
if len(fields) == 0 {
return false
}
switch strings.ToUpper(fields[0]) {
case "DNS", "DNS6", "DOMAIN", "ADAPTER_DOMAIN_SUFFIX", "DOMAIN-SEARCH", "DOMAIN-ROUTE":
return true
default:
return false
}
})
}
func (options *pushedOptions) addParseError(name string, value string, err error) {
options.parseErrors = append(options.parseErrors, pushedOptionParseError{
Name: name,
Value: value,
Err: err,
})
}
func (options *pushedOptions) addExcludedRoute(name string, value string, route TunnelRoute) {
options.excludedRoutes = append(options.excludedRoutes, pushedExcludedRoute{
Name: name,
Value: value,
Route: route,
})
}
func normalizeControlPayload(payload []byte) string {
trimmedPayload := strings.Trim(string(payload), "\x00")
return strings.TrimSpace(trimmedPayload)
}
const (
pushedOptionLineStateInitial = iota
pushedOptionLineStateQuoted
pushedOptionLineStateUnquoted
pushedOptionLineStateDone
pushedOptionLineStateSingleQuoted
)
// MAX_PARMS (options.h); apply_push_options calls parse_line with
// SIZE(p) - 1 parameter slots.
const pushedOptionLineMaxParameters = 16
// Upstream space() (options.c) counts the terminating NUL as whitespace, which
// is what closes the last unquoted parameter of a line.
func isPushedOptionLineSpace(character byte) bool {
switch character {
case 0, ' ', '\t', '\n', '\v', '\f', '\r':
return true
default:
return false
}
}
// parse_line (options.c) splits one option line into parameters, and it is the
// only place upstream processes an escape: a backslash quotes the next
// character, but only '\\', '"' and whitespace may follow it — anything else
// makes parse_line report "Bad backslash ('\') usage" and return zero
// parameters, which drops the whole option line. A line that ends inside a
// parameter (trailing backslash, unbalanced quote) is dropped the same way with
// "Residual parse state". Single quotes suppress backslash processing entirely,
// and ';' or '#' at the start of a parameter comments out the rest of the line.
func parsePushedOptionLine(line string) ([]string, bool) {
parameters := make([]string, 0, 4)
var parameter strings.Builder
state := pushedOptionLineStateInitial
backslash := false
parameterLoop:
for index := 0; index <= len(line); index++ {
var character byte
if index < len(line) {
character = line[index]
}
if !backslash && character == '\\' && state != pushedOptionLineStateSingleQuoted {
backslash = true
continue
}
if backslash && character != '\\' && character != '"' && !isPushedOptionLineSpace(character) {
return nil, false
}
var stored byte
switch state {
case pushedOptionLineStateInitial:
if isPushedOptionLineSpace(character) {
break
}
if character == ';' || character == '#' {
break parameterLoop
}
switch {
case !backslash && character == '"':
state = pushedOptionLineStateQuoted
case !backslash && character == '\'':
state = pushedOptionLineStateSingleQuoted
default:
stored = character
state = pushedOptionLineStateUnquoted
}
case pushedOptionLineStateUnquoted:
if !backslash && isPushedOptionLineSpace(character) {
state = pushedOptionLineStateDone
} else {
stored = character
}
case pushedOptionLineStateQuoted:
if !backslash && character == '"' {
state = pushedOptionLineStateDone
} else {
stored = character
}
case pushedOptionLineStateSingleQuoted:
if character == '\'' {
state = pushedOptionLineStateDone
} else {
stored = character
}
}
if state == pushedOptionLineStateDone {
parameters = append(parameters, parameter.String())
parameter.Reset()
state = pushedOptionLineStateInitial
if len(parameters) >= pushedOptionLineMaxParameters {
break parameterLoop
}
}
backslash = false
if stored != 0 {
parameter.WriteByte(stored)
}
}
if state != pushedOptionLineStateInitial || len(parameters) == 0 {
return nil, false
}
return parameters, true
}