-
Notifications
You must be signed in to change notification settings - Fork 51
/
dns.go
196 lines (163 loc) · 5.12 KB
/
dns.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
// +build linux
package dnsproxy
import (
"context"
"net"
"strconv"
"sync"
"syscall"
"time"
"github.com/miekg/dns"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/pkg/flowtracking"
"go.aporeto.io/trireme-lib/controller/pkg/ipsetmanager"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cache"
"go.uber.org/zap"
)
// Proxy struct represents the object for dns proxy
type Proxy struct {
puFromID cache.DataStore
conntrack flowtracking.FlowClient
collector collector.EventCollector
contextIDToServer map[string]*dns.Server
chreports chan dnsReport
updateIPsets ipsetmanager.ACLManager
sync.RWMutex
}
type serveDNS struct {
contextID string
*Proxy
}
const (
dnsRequestTimeout = 2 * time.Second
proxyMarkInt = 0x40 //Duplicated from supervisor/iptablesctrl refer to it
)
func socketOptions(_, _ string, c syscall.RawConn) error {
var opErr error
err := c.Control(func(fd uintptr) {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, proxyMarkInt); err != nil {
zap.L().Error("Failed to mark connection", zap.Error(err))
}
})
if err != nil {
return err
}
return opErr
}
func listenUDP(network, addr string) (net.PacketConn, error) {
var lc net.ListenConfig
lc.Control = socketOptions
return lc.ListenPacket(context.Background(), network, addr)
}
func forwardDNSReq(r *dns.Msg, ip net.IP, port uint16) (*dns.Msg, []string, error) {
var ips []string
c := new(dns.Client)
c.Dialer = &net.Dialer{
Control: func(_, _ string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, proxyMarkInt); err != nil {
zap.L().Error("Failed to assing mark to socket", zap.Error(err))
}
})
},
Timeout: dnsRequestTimeout,
}
in, _, err := c.Exchange(r, net.JoinHostPort(ip.String(), strconv.Itoa(int(port))))
if err != nil {
return nil, nil, err
}
for _, ans := range in.Answer {
if ans.Header().Rrtype == dns.TypeA {
t, _ := ans.(*dns.A)
ips = append(ips, t.A.String())
}
if ans.Header().Rrtype == dns.TypeAAAA {
t, _ := ans.(*dns.AAAA)
ips = append(ips, t.AAAA.String())
}
}
return in, ips, nil
}
func (s *serveDNS) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
var err error
lAddr := w.LocalAddr().(*net.UDPAddr)
rAddr := w.RemoteAddr().(*net.UDPAddr)
var puCtx *pucontext.PUContext
defer func() {
if puCtx != nil {
s.reportDNSLookup(r.Question[0].Name, puCtx, rAddr.IP, "")
}
}()
origIP, origPort, _, err := s.conntrack.GetOriginalDest(net.ParseIP("127.0.0.1"), rAddr.IP, uint16(lAddr.Port), uint16(rAddr.Port), 17)
if err != nil {
zap.L().Error("Failed to find flow for the redirected dns traffic", zap.Error(err))
return
}
data, err := s.puFromID.Get(s.contextID)
if err != nil {
zap.L().Error("context not found for the PU with ID", zap.String("contextID", s.contextID))
return
}
dnsReply, ips, err := forwardDNSReq(r, origIP, origPort)
if err != nil {
zap.L().Debug("Forwarded dns request returned error", zap.Error(err))
return
}
puCtx = data.(*pucontext.PUContext)
ps, err1 := puCtx.GetPolicyFromFQDN(r.Question[0].Name)
if err1 == nil {
for _, p := range ps {
s.updateIPsets.UpdateIPsets(ips, p.Policy.ServiceID)
if err1 := puCtx.UpdateApplicationACLs(policy.IPRuleList{{Addresses: ips,
Ports: p.Ports,
Protocols: p.Protocols,
Policy: p.Policy,
}}); err1 != nil {
zap.L().Error("Adding IP rule returned error", zap.Error(err1))
}
}
}
if err = w.WriteMsg(dnsReply); err != nil {
zap.L().Error("Writing dns response back to the client returned error", zap.Error(err))
}
}
// StartDNSServer starts the dns server on the port provided for contextID
func (p *Proxy) StartDNSServer(contextID, port string) error {
netPacketConn, err := listenUDP("udp", "127.0.0.1:"+port)
if err != nil {
return err
}
var server *dns.Server
storeInMap := func() {
p.Lock()
defer p.Unlock()
p.contextIDToServer[contextID] = server
}
server = &dns.Server{NotifyStartedFunc: storeInMap, PacketConn: netPacketConn, Handler: &serveDNS{contextID, p}}
go func() {
if err := server.ActivateAndServe(); err != nil {
zap.L().Error("Could not start DNS proxy server", zap.Error(err))
}
}()
return nil
}
// ShutdownDNS shuts down the dns server for contextID
func (p *Proxy) ShutdownDNS(contextID string) {
p.Lock()
defer p.Unlock()
if s, ok := p.contextIDToServer[contextID]; ok {
if err := s.Shutdown(); err != nil {
zap.L().Error("shutdown of dns server returned error", zap.String("contextID", contextID), zap.Error(err))
}
delete(p.contextIDToServer, contextID)
}
}
// New creates an instance of the dns proxy
func New(puFromID cache.DataStore, conntrack flowtracking.FlowClient, c collector.EventCollector, aclmanager ipsetmanager.ACLManager) *Proxy {
ch := make(chan dnsReport)
p := &Proxy{chreports: ch, puFromID: puFromID, collector: c, conntrack: conntrack, contextIDToServer: map[string]*dns.Server{}, updateIPsets: aclmanager}
go p.reportDNSRequests(ch)
return p
}