-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnatpt.go
144 lines (127 loc) · 3.48 KB
/
natpt.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
// 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 ipn
import (
"net"
"github.com/celzero/firestack/intra/log"
"github.com/celzero/firestack/intra/protect"
"github.com/celzero/firestack/intra/settings"
)
// app | interface | pt | who | internet?
// ---- | -------- | -------- | ---- | --------
// ip4 | ip4 | - | - | y
// ip4 | ip6 | 464xlat | os | y
// ---- | -------- | -------- | ---- | --------
// ip6 | ip6 | - | - | y
// ip6 | ip4 | nat64 | rdns | y
// ---- | -------- | -------- | ---- | --------
// ip4+6 | ip6 | 464xlat | os | y
// ip4+6 | ip4 | happyeye | app | y
// ---- | -------- | -------- | ---- | --------
// ip4+6 | ip4+6 | bind | rdns | y
// ip4+6 | ip6+4 | bind | rdns | y
// datatracker.ietf.org/doc/html/rfc8305#section-7
type natPt struct {
*nat64
*dns64
l3 string
tunmode *settings.TunMode
ip4s []net.IP
ip6s []net.IP
}
type NatPt interface {
protect.Protector
DNS64
NAT64
}
func NewNatPt(l3 string, tunmode *settings.TunMode) NatPt {
return &natPt{
nat64: newNat64(),
dns64: newDns64(),
l3: l3,
tunmode: tunmode,
ip4s: nil,
ip6s: nil,
}
}
func (pt *natPt) D64(id string, ans6 []byte, f Resolver) []byte {
if pt.can64() {
return pt.dns64.eval(id, pt.force64(), ans6, f)
}
return ans6
}
func (pt *natPt) can64() bool {
return settings.IP6 == pt.l3 || pt.force64()
}
func (pt *natPt) force64() bool {
return pt.tunmode.PtMode == settings.PtModeForce64
}
func (n *natPt) IsNat64(id string, ip []byte) bool {
prefixes := n.nat64PrefixForResolver(id)
_, ok := matchNat64(prefixes, ip)
return ok
}
func (n *natPt) X64(id string, ip6 []byte) []byte {
if len(ip6) != net.IPv6len {
log.Debugf("nat64: ip6(%v) len(%d) != 16", ip6, len(ip6))
return nil
}
prefixes := n.nat64PrefixForResolver(id)
if len(prefixes) <= 0 {
log.Debugf("nat64: no prefix64 found for resolver(%s)", ip6, id)
return nil
}
if x, ok := matchNat64(prefixes, ip6); ok {
return n.xAddr(x, ip6)
} else {
log.Debugf("nat64: no matching prefix64 for ip(%v) in id(%s/%d)", ip6, id, len(prefixes))
}
return nil
}
func (n *natPt) ResetNat64Prefix(ip6prefix string) bool {
var err error
var ipnet *net.IPNet
if _, ipnet, err = net.ParseCIDR(ip6prefix); err == nil {
n.dns64.register(UnderlayResolver) // wipe the slate clean
if err = n.dns64.addNat64Prefix(UnderlayResolver, ipnet); err == nil {
return true
}
}
log.Warnf("natpt: could not add underlay nat64 prefix: %s; err %v", ip6prefix, err)
return false
}
// Returns the first matching local-interface net.IP for the network
func (n *natPt) UIP(network string) []byte {
switch network {
case "tcp6":
fallthrough
case "udp6":
if len(n.ip6s) > 0 {
return n.ip6s[0]
}
return net.IPv6zero
default:
if len(n.ip4s) > 0 {
return n.ip4s[0]
}
return net.IPv4zero
}
}
func (n *natPt) nat64PrefixForResolver(id string) []*net.IPNet {
if ips, ok := n.ip64[id]; !ok {
return nil
} else {
return ips
}
}
func matchNat64(nets []*net.IPNet, ip net.IP) (*net.IPNet, bool) {
for _, p := range nets {
if p.Contains(ip) {
return p, true
}
}
return nil, false
}