-
Notifications
You must be signed in to change notification settings - Fork 14
/
alg.go
1106 lines (1005 loc) · 29.9 KB
/
alg.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
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2022 RethinkDNS and its authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package dnsx
import (
"encoding/binary"
"errors"
"hash/fnv"
"net/netip"
"strconv"
"strings"
"sync"
"time"
x "github.com/celzero/firestack/intra/backend"
"github.com/celzero/firestack/intra/log"
"github.com/celzero/firestack/intra/settings"
"github.com/celzero/firestack/intra/xdns"
"github.com/miekg/dns"
)
const (
timeout = 15 * time.Second
ttl2m = 2 * time.Minute // 2m ttl for alg/nat ip
algttl = 15 // 15s ttl for alg dns
key4 = ":a"
key6 = ":aaaa"
notransport = "NoTransport"
maxiter = 100 // max number alg/nat evict iterations
)
type iptype int
const (
typalg iptype = iota
typreal
typsecondary
)
var (
// 100.64.x.x
rfc6598 = []uint8{100, 64, 0, 1}
rfc8215a = []uint16{0x64, 0xff9b, 0x1, 0xda19, 0x100, 0x0, 0x0, 0x0}
errNoTransportAlg = errors.New("no alg transport")
errNotAvailableAlg = errors.New("no valid alg ips")
errCannotRegisterAlg = errors.New("cannot register alg ip")
errCannotSubstAlg = errors.New("cannot substitute alg ip")
)
func isAlgErr(err error) bool {
return (err == errCannotRegisterAlg || err == errNotAvailableAlg || err == errCannotSubstAlg)
}
type Gateway interface {
// given an alg or real ip, retrieves assoc real ips as csv, if any
X(algip netip.Addr) (realipcsv string)
// given an alg or real ip, retrieves assoc dns names as csv, if any
PTR(algip netip.Addr, force bool) (domaincsv string)
// given domain, retrieve assoc alg ips as csv, if any
RESOLV(domain string) (algipcsv string)
// given an alg or real ip, retrieve assoc blocklists as csv, if any
RDNSBL(algip netip.Addr) (blocklistcsv string)
// translate overwrites ip answers to alg ip answers
translate(yes bool)
// Query using t1 as primary transport and t2 as secondary and preset as pre-determined ip answers
q(t1 Transport, t2 Transport, preset []*netip.Addr, network string, q *dns.Msg, s *x.DNSSummary) (*dns.Msg, error)
// clear obj state
stop()
}
type secans struct {
ips []*netip.Addr
summary *x.DNSSummary
}
type ans struct {
algip *netip.Addr // generated answer
realips []*netip.Addr // all ip answers
secondaryips []*netip.Addr // all ip answers from secondary
domain []string // all domain names in an answer (incl qname)
qname string // the query domain name
blocklists string // csv blocklists containing qname per active config at the time
ttl time.Time
}
type ansMulti struct {
algip []*netip.Addr // generated answers
realip []*netip.Addr // all ip answers
secondaryips []*netip.Addr // all ip answers from secondary
domain []string // all domain names in an answer (incl qname)
qname string // the query domain name
blocklists string // csv blocklists containing qname per active config at the time
ttl time.Time
}
// TODO: Keep a context here so that queries can be canceled.
type dnsgateway struct {
sync.RWMutex // locks alg, nat, octets, hexes
mod bool // modify realip to algip
alg map[string]*ans // domain+type -> ans
nat map[netip.Addr]*ans // algip -> ans
ptr map[netip.Addr]*ans // realip -> ans
rdns RdnsResolver // local and remote rdns blocks
dns64 NatPt // dns64/nat64
octets []uint8 // ip4 octets, 100.x.y.z
hexes []uint16 // ip6 hex, 64:ff9b:1:da19:0100.x.y.z
chash bool // use consistent hashing to generae alg ips
}
var _ Gateway = (*dnsgateway)(nil)
// NewDNSGateway returns a DNS ALG, ready for use.
func NewDNSGateway(outer RdnsResolver, dns64 NatPt) (t *dnsgateway) {
alg := make(map[string]*ans)
nat := make(map[netip.Addr]*ans)
ptr := make(map[netip.Addr]*ans)
t = &dnsgateway{
alg: alg,
nat: nat,
ptr: ptr,
rdns: outer,
dns64: dns64,
octets: rfc6598,
hexes: rfc8215a,
chash: true,
}
log.I("alg: setup done")
return
}
func (t *dnsgateway) translate(yes bool) {
log.I("alg: translate? %t", yes)
t.mod = yes
}
// Implements Gateway
func (t *dnsgateway) stop() {
t.Lock()
defer t.Unlock()
clear(t.alg)
clear(t.nat)
t.octets = rfc6598
t.hexes = rfc8215a
}
func (t *dnsgateway) querySecondary(t2 Transport, network string, msg *dns.Msg, out chan<- secans, in <-chan *dns.Msg) {
var r *dns.Msg
var err error
result := secans{
ips: []*netip.Addr{},
summary: new(x.DNSSummary),
}
go func() {
time.Sleep(timeout)
out <- result
}()
defer func() {
// race against the timeout
out <- result
}()
// check if the question is blocked
if msg == nil || !xdns.HasAnyQuestion(msg) {
return // not a valid dns message
} else if ok := xdns.HasAQuadAQuestion(msg) || xdns.HasHTTPQuestion(msg) || xdns.HasSVCBQuestion(msg); !ok {
return // not a dns question we care about
} else if ans1, blocklists, err2 := t.rdns.blockQ( /*maybe nil*/ t2, nil, msg); err2 == nil {
// if err !is nil, then the question is blocked
if ans1 != nil && len(ans1.Answer) > 0 {
result.ips = append(result.ips, xdns.AAnswer(ans1)...)
result.ips = append(result.ips, xdns.AAAAAnswer(ans1)...)
} // noop: for HTTP/SVCB, the answer is always empty
result.summary.Blocklists = blocklists
result.summary.Status = Complete
return
}
// no secondary transport; check if there's already an answer to work with
if t2 == nil {
ticker := time.NewTicker(timeout)
select {
case r = <-in:
ticker.Stop()
break
case <-ticker.C:
ticker.Stop()
return
}
} else {
// query secondary to get answer for q
r, err = Req(t2, network, msg, result.summary)
}
if err != nil {
log.D("alg: skip; sec transport %s err? %v", idstr(t2), err)
return
}
// check if answer r is blocked; r is either from t2 or from <-in
if r == nil || !xdns.HasAnyAnswer(r) {
// not a valid dns answer
return
} else if a, blocklistnames := t.rdns.blockA( /*may be nil*/ t2, nil, msg, r, result.summary.Blocklists); a != nil {
// if "a" is not nil, then the r is blocked
if len(blocklistnames) > 0 {
result.summary.Blocklists = blocklistnames
}
// blocked answer has A, AAAA, or HTTPS/SVCB records
// see: xdns.RefusedResponseFromMessage
if len(a.Answer) > 0 {
result.ips = append(result.ips, xdns.AAnswer(a)...)
result.ips = append(result.ips, xdns.AAAAAnswer(a)...)
} // noop: for HTTPS/SVCB, the answer section is empty
return
} else {
if len(blocklistnames) > 0 {
result.summary.Blocklists = blocklistnames
}
if xdns.AQuadAUnspecified(r) {
result.summary.UpstreamBlocks = true
} else {
a4 := xdns.AAAAAnswer(r)
a6 := xdns.AAnswer(r)
ip4hints := xdns.IPHints(r, dns.SVCB_IPV4HINT)
ip6hints := xdns.IPHints(r, dns.SVCB_IPV6HINT)
result.ips = append(result.ips, a4...)
result.ips = append(result.ips, a6...)
result.ips = append(result.ips, ip4hints...)
result.ips = append(result.ips, ip6hints...)
// TODO: result.targets?
}
return
}
}
// Implements Gateway
func (t *dnsgateway) q(t1, t2 Transport, preset []*netip.Addr, network string, q *dns.Msg, summary *x.DNSSummary) (*dns.Msg, error) {
var ansin *dns.Msg // answer got from transports
var err error
if t1 == nil {
return nil, errNoTransportAlg
}
usepreset := len(preset) > 0
// presets override both t1 and t2:
// discard t2 as with preset we don't care about additional ips and blocklists;
// t1 is not discarded entirely as it is needed to subst ips in https/svcb responses
if usepreset {
t2 = nil
}
mod := t.mod // allow alg?
secch := make(chan secans, 1)
resch := make(chan *dns.Msg, 1)
innersummary := new(x.DNSSummary)
// todo: use context?
// t2 may be nil
go t.querySecondary(t2, network, q, secch, resch)
if usepreset {
ansin, err = synthesizeOrQuery(preset, t1, q, network, innersummary)
} else {
ansin, err = Req(t1, network, q, innersummary)
}
resch <- ansin // ansin may be nil; but that's ok
// override relevant values in summary
fillSummary(innersummary, summary)
if err != nil {
if ansin == nil {
log.I("alg: abort; r: 0, qerr %v", err)
return nil, err
}
log.D("alg: err but r ok; ans: %d, qerr %v", xdns.Len(ansin), err)
}
if ansin == nil { // may be nil on errors
return nil, err
}
qname, _ := xdns.NormalizeQName(xdns.QName(ansin))
summary.QName = qname
summary.QType = qtype(ansin)
ans64 := t.dns64.D64(network, ansin, t1) // ans64 may be nil if no D64 or error
if ans64 != nil {
log.D("alg: dns64; s/ans(%d)/ans64(%d)", xdns.Len(ansin), xdns.Len(ans64))
withDNS64Summary(ans64, summary)
ansin = ans64
} // else: no dns64, or error; continue with ansin
hasq := xdns.HasAAAAQuestion(ansin) || xdns.HasAQuestion(ansin) || xdns.HasSVCBQuestion(ansin) || xdns.HasHTTPQuestion(ansin)
hasans := xdns.HasAnyAnswer(ansin)
rgood := xdns.HasRcodeSuccess(ansin)
ans0000 := xdns.AQuadAUnspecified(ans64)
if ans0000 {
summary.UpstreamBlocks = true
}
if !hasq || !hasans || !rgood || ans0000 {
log.D("alg: skip; query(n:%s / a:%d) hasq(%t) hasans(%t) rgood(%t), ans0000(%t)", qname, xdns.Len(ansin), hasq, hasans, rgood, ans0000)
return ansin, nil
}
a6 := xdns.AAAAAnswer(ansin)
a4 := xdns.AAnswer(ansin)
ip4hints := xdns.IPHints(ansin, dns.SVCB_IPV4HINT)
ip6hints := xdns.IPHints(ansin, dns.SVCB_IPV6HINT)
// TODO: generate one alg ip per target, synth one rec per target
targets := xdns.Targets(ansin)
realip := make([]*netip.Addr, 0)
algips := make([]*netip.Addr, 0)
// fetch secondary ips before lock
secres := <-secch
// inform kt of secondary blocklists, if any
summary.Blocklists = secres.summary.Blocklists
summary.UpstreamBlocks = secres.summary.UpstreamBlocks || summary.UpstreamBlocks
defer func() {
if isAlgErr(err) && !mod {
log.D("alg: no mod; supress err %v", err)
// ignore alg errors if no modification is desired
err = nil
}
}()
t.Lock()
defer t.Unlock()
algip4hints := make([]*netip.Addr, 0, len(ip4hints))
algip6hints := make([]*netip.Addr, 0, len(ip6hints))
algip4s := make([]*netip.Addr, 0, len(a4))
algip6s := make([]*netip.Addr, 0, len(a6))
for i, ip4 := range ip4hints {
realip = append(realip, ip4)
// 0th algip is reserved for A records
algip, ipok := t.take4Locked(qname, i+1)
if !ipok {
return ansin, errNotAvailableAlg
}
algip4hints = append(algip4hints, algip)
}
for i, ip6 := range ip6hints {
realip = append(realip, ip6)
// 0th algip is reserved for AAAA records
algip, ipok := t.take6Locked(qname, i+1)
if !ipok {
return ansin, errNotAvailableAlg
}
algip6hints = append(algip6hints, algip)
}
if len(a6) > 0 {
realip = append(realip, a6...)
// choose the first alg ip6; may've been generated by ip6hints
algip, ipok := t.take6Locked(qname, 0)
if !ipok {
return ansin, errNotAvailableAlg
}
algip6s = append(algip6s, algip)
}
if len(a4) > 0 {
realip = append(realip, a4...)
// choose the first alg ip4; may've been generated by ip4hints
algip, ipok := t.take4Locked(qname, 0)
if !ipok {
return ansin, errNotAvailableAlg
}
algip4s = append(algip4s, algip)
}
substok4 := false
substok6 := false
// substituions needn't happen when no alg ips to begin with
mustsubst := false
ansout := ansin.Copy()
// TODO: substitute ips in additional section
if len(algip4hints) > 0 {
substok4 = xdns.SubstSVCBRecordIPs( /*out*/ ansout, dns.SVCB_IPV4HINT, algip4hints, algttl) || substok4
mustsubst = true
}
if len(algip6hints) > 0 {
substok6 = xdns.SubstSVCBRecordIPs( /*out*/ ansout, dns.SVCB_IPV6HINT, algip6hints, algttl) || substok6
mustsubst = true
}
if len(algip4s) > 0 {
substok4 = xdns.SubstARecords( /*out*/ ansout, algip4s, algttl) || substok4
mustsubst = true
}
if len(algip6s) > 0 {
substok6 = xdns.SubstAAAARecords( /*out*/ ansout, algip6s, algttl) || substok6
mustsubst = true
}
log.D("alg: %s a6(a %d / h %d / s %t) : a4(a %d / h %d / s %t)", qname, len(a6), len(ip6hints), substok6, len(a4), len(ip4hints), substok4)
if !substok4 && !substok6 {
if mustsubst {
err = errCannotSubstAlg
} else {
err = nil
}
log.D("alg: skip; err(%v); ips subst %s", err, qname)
return ansin, err // ansin is nil if no alg ips
}
typ := typalg
if !mod {
typ = typreal
}
// get existing secondary ips for qname, from previous alg/nat
previp4s, previp6s := t.resolvLocked(qname, typ)
if len(previp4s) > 0 {
realip = appendUniq(realip, previp4s, key4)
log.D("alg: subst; for %s with prev ip4s (alg: %t) %s", qname, typ == typalg, previp4s)
}
if len(previp6s) > 0 {
realip = appendUniq(realip, previp6s, key6)
log.D("alg: subst; for %s with prev ip6s (alg? %t) %s", qname, typ == typalg, previp6s)
}
prevsec4s, prevsec6s := t.resolvLocked(qname, typsecondary)
if len(prevsec4s) > 0 {
secres.ips = appendUniq(secres.ips, prevsec4s, key4)
log.D("alg: subst; for %s with prev sec ip4s (alg: %t) %s", qname, typ == typalg, prevsec4s)
}
if len(prevsec6s) > 0 {
secres.ips = appendUniq(secres.ips, prevsec6s, key6)
log.D("alg: subst; for %s with prev sec ip6s (alg? %t) %s", qname, typ == typalg, prevsec6s)
}
// TODO: just like w/ previps, get existing targets, blocklists for qname and merge w/ new ones
algips = append(algips, algip4s...)
algips = append(algips, algip6s...)
algips = append(algips, algip4hints...)
algips = append(algips, algip6hints...)
x := &ansMulti{
algip: algips,
realip: realip,
// may be empty on timeout errors, or
// or same as realips if t2 is nil
secondaryips: secres.ips,
domain: targets, // may be nil
qname: qname,
blocklists: secres.summary.Blocklists,
// qname->realip valid for next ttl seconds
ttl: time.Now().Add(ttl2m),
}
log.D("alg: ok; domains %s ips %s => subst %s; mod? %t", targets, realip, algips, mod)
if t.registerMultiLocked(qname, x) {
// if mod is set, send modified answer
if mod {
withAlgSummaryIfNeeded(algips, summary)
return ansout, nil
} else {
return ansin, nil
}
} else {
return ansin, errCannotRegisterAlg
}
}
func netip2csv(ips []*netip.Addr) (csv string) {
for i, ip := range ips {
if i > 0 {
csv += ","
}
csv += ip.String()
}
return strings.TrimSuffix(csv, ",")
}
func withDNS64Summary(ans64 *dns.Msg, s *x.DNSSummary) {
s.RCode = xdns.Rcode(ans64)
s.RData = xdns.GetInterestingRData(ans64)
s.RTtl = xdns.RTtl(ans64)
if settings.Debug {
prefix := PrefixFor(AlgDNS64)
s.Server = prefix + s.Server
}
}
func withAlgSummaryIfNeeded(algips []*netip.Addr, s *x.DNSSummary) {
if settings.Debug {
// convert algips to ipcsv
ipcsv := netip2csv(algips)
if len(s.RData) > 0 {
s.RData = s.RData + "," + ipcsv
} else {
s.RData = ipcsv
}
prefix := PrefixFor(Alg)
if len(s.Server) > 0 {
s.Server = prefix + s.Server
} else {
s.Server = prefix + notransport
}
}
}
func (am *ansMulti) ansViewLocked(i int) *ans {
return &ans{
algip: am.algip[i],
realips: am.realip,
secondaryips: am.secondaryips,
domain: am.domain,
qname: am.qname,
blocklists: am.blocklists,
ttl: am.ttl,
}
}
func (t *dnsgateway) registerMultiLocked(q string, am *ansMulti) bool {
for i := range am.algip {
if ok := t.registerNatLocked(q, i, am.ansViewLocked(i)); !ok {
return false
}
}
for i := range am.realip {
// index is always 0 since algip is inconsequential for ptr
if ok := t.registerPtrLocked(i, am.ansViewLocked(0)); !ok {
return false
}
}
return true
}
// register mapping from qname -> algip+realip (alg) and algip -> qname+realip (nat)
func (t *dnsgateway) registerNatLocked(q string, idx int, x *ans) bool {
ip := x.algip
var k string
if ip.Is4() {
k = q + key4 + strconv.Itoa(idx)
} else if ip.Is6() {
k = q + key6 + strconv.Itoa(idx)
} else {
return false
}
t.alg[k] = x
t.nat[*ip] = x
return true
}
// register mapping from realip -> algip+qname (ptr)
func (t *dnsgateway) registerPtrLocked(idx int, x *ans) bool {
ip := x.realips[idx]
t.ptr[*ip] = x // x contains qname and the algip
return true
}
func (t *dnsgateway) take4Locked(q string, idx int) (*netip.Addr, bool) {
k := q + key4 + strconv.Itoa(idx)
if ans, ok := t.alg[k]; ok {
ip := ans.algip
if ip.Is4() {
ans.ttl = time.Now().Add(ttl2m)
return ip, true
} else {
// shouldn't happen; if it does, rm erroneous entry
delete(t.alg, k)
delete(t.nat, *ip)
}
}
if t.chash {
for i := 0; i < maxiter; i++ {
genip := gen4Locked(k, i)
if !genip.IsGlobalUnicast() {
continue
}
if _, taken := t.nat[genip]; !taken {
return &genip, genip.IsValid()
}
}
log.W("alg: gen: no more IP4s (%v)", q)
return nil, false
}
gen := true
// 100.x.y.z: 4m+ ip4s
if z := t.octets[3]; z < 254 {
t.octets[3] += 1 // z
} else if y := t.octets[2]; y < 254 {
t.octets[2] += 1 // y
t.octets[3] = 1 // z
} else if x := t.octets[1]; x < 128 {
t.octets[1] += 1 // x
t.octets[2] = 0 // y
t.octets[3] = 1 // z
} else {
i := 0
for kx, ent := range t.alg {
if i > maxiter {
break
}
if d := time.Since(ent.ttl); d > 0 {
log.I("alg: reuse stale alg %s for %s", kx, k)
delete(t.alg, kx)
delete(t.nat, *ent.algip)
return ent.algip, true
}
i += 1
}
gen = false
}
if gen {
// 100.x.y.z: big endian is network-order, which netip expects
b4 := [4]byte{t.octets[0], t.octets[1], t.octets[2], t.octets[3]}
genip := netip.AddrFrom4(b4).Unmap()
return &genip, genip.IsValid()
} else {
log.W("alg: no more IP4s (%v)", t.octets)
}
return nil, false
}
func gen4Locked(k string, hop int) netip.Addr {
s := strconv.Itoa(hop) + k
v22 := hash22(s)
// 100.64.y.z/15 2m+ ip4s
b4 := [4]byte{
rfc6598[0], // 100
rfc6598[1] + uint8(v22>>16), // 64 + int(6bits)
uint8((v22 >> 8) & 0xff), // extract next 8 bits
uint8(v22 & 0xff), // extract last 8 bits
}
// why unmap? github.com/golang/go/issues/53607
return netip.AddrFrom4(b4).Unmap()
}
func (t *dnsgateway) take6Locked(q string, idx int) (*netip.Addr, bool) {
k := q + key6 + strconv.Itoa(idx)
if ans, ok := t.alg[k]; ok {
ip := ans.algip
if ip.Is6() {
ans.ttl = time.Now().Add(ttl2m)
return ip, true
} else {
// shouldn't happen; if it does, rm erroneous entry
delete(t.alg, k)
delete(t.nat, *ip)
}
}
if t.chash {
for i := 0; i < maxiter; i++ {
genip := gen6Locked(k, i)
if _, taken := t.nat[genip]; !taken {
return &genip, genip.IsValid()
}
}
log.W("alg: gen: no more IP6s (%v)", q)
return nil, false
}
gen := true
// 64:ff9b:1:da19:0100.x.y.z: 281 trillion ip6s
if z := t.hexes[7]; z < 65534 {
t.hexes[7] += 1 // z
} else if y := t.hexes[6]; y < 65534 {
t.hexes[6] += 1 // y
t.hexes[7] = 1 // z
} else if x := t.hexes[5]; x < 65534 {
t.hexes[5] += 1 // x
t.hexes[6] = 0 // y
t.hexes[7] = 1 // z
} else {
// possible that we run out of 200 trillion ips...?
gen = false
}
if gen {
// 64:ff9b:1:da19:0100.x.y.z: big endian is network-order, which netip expects
b16 := [16]byte{}
for i, hx := range t.hexes {
i = i * 2
binary.BigEndian.PutUint16(b16[i:i+2], hx)
}
genip := netip.AddrFrom16(b16)
return &genip, genip.IsValid()
} else {
log.W("alg: no more IP6s (%x)", t.hexes)
}
return nil, false
}
func gen6Locked(k string, hop int) netip.Addr {
s := strconv.Itoa(hop) + k
v48 := hash48(s)
// 64:ff9b:1:da19:0100.x.y.z: 281 trillion ip6s
a16 := [8]uint16{
rfc8215a[0], // 64
rfc8215a[1], // ff9b
rfc8215a[2], // 1
rfc8215a[3], // da19
rfc8215a[4], // 0100
uint16((v48 >> 32) & 0xffff), // extract the top 16 bits
uint16((v48 >> 16) & 0xffff), // extract the mid 16 bits
uint16(v48 & 0xffff), // extract the last 16 bits
}
b16 := [16]byte{}
for i, hx := range a16 {
i = i * 2
binary.BigEndian.PutUint16(b16[i:i+2], hx)
}
return netip.AddrFrom16(b16)
}
func (t *dnsgateway) X(algip netip.Addr) (ips string) {
t.RLock()
defer t.RUnlock()
rip := t.xLocked(algip, !t.mod)
if len(rip) > 0 {
var s []string
for _, r := range rip {
if r != nil && r.IsValid() {
s = append(s, r.String())
}
}
ips = strings.Join(s, ",")
} // else: algip isn't really an alg ip, nothing to do
return ips
}
func (t *dnsgateway) PTR(algip netip.Addr, force bool) (domains string) {
t.RLock()
defer t.RUnlock()
d := t.ptrLocked(algip, (!t.mod || force))
if len(d) > 0 {
domains = strings.Join(d, ",")
} // else: algip isn't really an alg ip, nothing to do
return domains
}
func (t *dnsgateway) RESOLV(domain string) (ipcsv string) {
t.RLock()
defer t.RUnlock()
typ := typalg
if !t.mod {
typ = typreal
}
ip4s, ip6s := t.resolvLocked(domain, typ)
ips := append(ip4s, ip6s...)
if len(ips) > 0 {
var s []string
for _, ip := range ips {
if ip != nil && ip.IsValid() {
s = append(s, ip.String())
}
}
ipcsv = strings.Join(s, ",")
}
return
}
func (t *dnsgateway) RDNSBL(algip netip.Addr) (blocklists string) {
t.RLock()
defer t.RUnlock()
return t.rdnsblLocked(algip, !t.mod)
}
func (t *dnsgateway) xLocked(algip netip.Addr, useptr bool) []*netip.Addr {
var realips []*netip.Addr
// alg ips are always unmappped; see take4Locked
unmapped := algip.Unmap()
if ans, ok := t.nat[unmapped]; ok {
realips = append(ans.realips, ans.secondaryips...)
} else if ans, ok := t.ptr[unmapped]; useptr && ok {
// translate from realip only if not in mod mode
realips = append(ans.realips, ans.secondaryips...)
}
var unnated []*netip.Addr
if len(realips) == 0 {
unnated = t.maybeUndoNat64(&unmapped)
} else {
unnated = t.maybeUndoNat64(realips...)
}
log.D("alg: dns64: algip(%v) -> realips(%v) -> unnated(%v)", unmapped, realips, unnated)
if len(unnated) > 0 {
return unnated
}
return realips
}
func (t *dnsgateway) maybeUndoNat64(realips ...*netip.Addr) (unnat []*netip.Addr) {
for _, nip := range realips {
unmapped := nip.Unmap()
if !unmapped.Is6() {
continue
}
// the actual ID of the DNS64 for this whoever responded with "realips" for some unknown
// DNS query is not available. But, we needn't worry about UN-NAT64'ing other resolvers
// except the one we "force" onto the clients (aka dnsx.Local464Resolver).
// whether the active network has ipv4 connectivity is checked by dialers.filter()
ipx4 := t.dns64.X64(Local464Resolver, unmapped) // ipx4 may be zero addr
if !ipok(ipx4) { // no nat?
log.D("alg: dns64: maybeUndoNat64: No local nat64 to ip4(%v) for ip6(%v); ip not ok", ipx4, nip)
continue
}
log.D("alg: dns64: maybeUndoNat64: nat64 to ip4(%v) from ip6(%v)", ipx4, nip)
unmapped4 := ipx4.Unmap()
unnat = append(unnat, &unmapped4)
}
return
}
func (t *dnsgateway) ptrLocked(algip netip.Addr, useptr bool) (domains []string) {
// alg ips are always unmappped; see take4Locked
unmapped := algip.Unmap()
if ans, ok := t.nat[unmapped]; ok {
domains = ans.domain
} else if ans, ok := t.ptr[unmapped]; useptr && ok {
// translate from realip only if not in mod mode
domains = ans.domain
}
return
}
func (t *dnsgateway) resolvLocked(domain string, typ iptype) (ip4s []*netip.Addr, ip6s []*netip.Addr) {
partkey4 := domain + key4
partkey6 := domain + key6
ip4s = make([]*netip.Addr, 0)
ip6s = make([]*netip.Addr, 0)
staleips := make([]*netip.Addr, 0)
switch typ {
case typalg:
for i := 0; i < maxiter; i++ {
k4 := partkey4 + strconv.Itoa(i)
if ans, ok := t.alg[k4]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip4s = append(ip4s, ans.algip)
} else {
staleips = append(staleips, ans.algip)
}
} else {
break
}
}
for i := 0; i < maxiter; i++ {
k6 := partkey6 + strconv.Itoa(i)
if ans, ok := t.alg[k6]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip6s = append(ip6s, ans.algip)
} else {
staleips = append(staleips, ans.algip)
}
} else {
break
}
}
log.V("alg: resolv: %s -> alg ip4 %d, ip6 %d; stale %v", domain, len(ip4s), len(ip6s), staleips)
case typreal:
// all "ans" records have all realips; pick the first one
k4 := partkey4 + "0"
if ans, ok := t.alg[k4]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip4s = append(ip4s, ans.realips...)
} else {
staleips = append(staleips, ans.realips...)
}
}
k6 := partkey6 + "0"
if ans, ok := t.alg[k6]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip6s = append(ip6s, ans.realips...)
} else {
staleips = append(staleips, ans.realips...)
}
}
log.V("alg: resolv: %s -> real ip4 %d, ip6 %d; stale %v", domain, len(ip4s), len(ip6s), staleips)
case typsecondary:
// all "ans" records have all secondaryips; pick the first one
k4 := partkey4 + "0"
if ans, ok := t.alg[k4]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip4s = append(ip4s, ans.secondaryips...)
} else {
staleips = append(staleips, ans.secondaryips...)
}
}
k6 := partkey6 + "0"
if ans, ok := t.alg[k6]; ok {
if time.Until(ans.ttl) > 0 { // not stale
ip6s = append(ip6s, ans.secondaryips...)
} else {
staleips = append(staleips, ans.secondaryips...)
}
}
log.V("alg: resolv: %s -> secondary ip4 %d, ip6 %d; stale %v", domain, len(ip4s), len(ip6s), staleips)
}
return
}
func (t *dnsgateway) rdnsblLocked(algip netip.Addr, useptr bool) (bcsv string) {
// alg ips are always unmappped; see take4Locked
unmapped := algip.Unmap()
if ans, ok := t.nat[unmapped]; ok {
bcsv = ans.blocklists
} else if ans, ok := t.ptr[unmapped]; useptr && ok {
// translate from realip only if not in mod mode
bcsv = ans.blocklists
}
return
}
// xor fold fnv to 18 bits: www.isthe.com/chongo/tech/comp/fnv
func hash22(s string) uint32 {
h := fnv.New64a()
_, _ = h.Write([]byte(s))
v64 := h.Sum64()
return (uint32(v64>>22) ^ uint32(v64)) & 0x3FFFFF // 22 bits
}
// xor fold fnv to 48 bits: www.isthe.com/chongo/tech/comp/fnv
func hash48(s string) uint64 {
h := fnv.New64a()
_, _ = h.Write([]byte(s))
v64 := h.Sum64()
return (uint64(v64>>48) ^ uint64(v64)) & 0xFFFFFFFFFFFF // 48 bits
}
func synthesizeOrQuery(pre []*netip.Addr, tr Transport, msg *dns.Msg, network string, smm *x.DNSSummary) (*dns.Msg, error) {
// synthesize a response with the given ips
if len(pre) == 0 {
return Req(tr, network, msg, smm)
}
if msg == nil || !xdns.HasAnyQuestion(msg) {
return nil, errNoQuestion
}
qname := xdns.QName(msg)
qtyp := uint16(qtype(msg))
is4 := xdns.IsAQType(qtyp)
is6 := !is4 && xdns.IsAAAAQType(qtyp)
isHTTPS := (!is4 && !is6) && xdns.IsHTTPSQType(qtyp)
isSVCB := (!is4 && !is6) && xdns.IsSVCBQType(qtyp)
if is4 || is6 {
// if no ips are of the same family as the question xdns.AQuadAForQuery returns error
ans, err := xdns.AQuadAForQuery(msg, unptr(pre)...)
if err != nil { // errors on invalid msg, question, or mismatched ips
return Req(tr, network, msg, smm)
}
withPresetSummary(smm)
smm.RCode = xdns.Rcode(ans)
smm.RData = xdns.GetInterestingRData(ans)
smm.RTtl = xdns.RTtl(ans)
log.D("alg: synthesize: q(4? %t / 6? %t) rdata(%s)", qname, is4, is6, smm.RData)
return ans, nil // no error
} else if isHTTPS || isSVCB {
ans, err := Req(tr, network, msg, smm)
if err != nil {
return ans, err
} else if ans == nil { // empty answer is ok
return nil, errNoAnswer
}
var ok4, ok6 bool
ttl := int(xdns.AnsTTL)
ip4s, ip6s := splitIPFamilies(pre)
if len(ip4s) > 0 {
ok4 = xdns.SubstSVCBRecordIPs( /*out*/ ans, dns.SVCB_IPV4HINT, ip4s, ttl)
}
if len(ip6s) > 0 {
ok6 = xdns.SubstSVCBRecordIPs( /*out*/ ans, dns.SVCB_IPV6HINT, ip6s, algttl)
}
withPresetSummary(smm)
smm.RCode = xdns.Rcode(ans)
smm.RData = xdns.GetInterestingRData(ans)
smm.RTtl = xdns.RTtl(ans)
log.D("alg: synthesize: q: %s; (HTTPS? %t); subst4(%t), subst6(%t); rdata(%s)", qname, isHTTPS, ok4, ok6, smm.RData)
return ans, nil // no error
} else {
return Req(tr, network, msg, smm)
}
}
// Req sends q to transport t and returns the answer, if any;
// errors are unset if answer is not servfail or empty;
// smm, the in/out parameter, is dns summary as got from t.