forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns.go
More file actions
287 lines (271 loc) · 8.73 KB
/
dns.go
File metadata and controls
287 lines (271 loc) · 8.73 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
// Copyright (c) 2022 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package dpcmanager
import (
"fmt"
"net"
"time"
"github.com/eriknordmark/ipinfo"
"github.com/lf-edge/eve/pkg/pillar/devicenetwork"
"github.com/lf-edge/eve/pkg/pillar/types"
)
type geoService struct{}
// GetGeolocationInfo tries to obtain geolocation information corresponding
// to the given IP address.
func (g *geoService) GetGeolocationInfo(ipAddr net.IP) (*ipinfo.IPInfo, error) {
// geolocation with short timeout
opt := ipinfo.Options{
Timeout: 5 * time.Second,
SourceIp: ipAddr,
}
info, err := ipinfo.MyIPWithOptions(opt)
if err != nil {
return nil, err
}
return info, nil
}
func (m *DpcManager) updateDNS() {
defer m.publishDNS()
dpc := m.currentDPC()
if dpc == nil {
m.deviceNetStatus = types.DeviceNetworkStatus{}
return
}
m.deviceNetStatus.DPCKey = dpc.Key
m.deviceNetStatus.Version = dpc.Version
m.deviceNetStatus.State = dpc.State
m.deviceNetStatus.Testing = m.dpcVerify.inProgress
m.deviceNetStatus.CurrentIndex = m.dpcList.CurrentIndex
m.deviceNetStatus.RadioSilence = m.radioSilence
oldPorts := m.deviceNetStatus.Ports
m.deviceNetStatus.Ports = make([]types.NetworkPortStatus, len(dpc.Ports))
for ix, port := range dpc.Ports {
m.deviceNetStatus.Ports[ix].IfName = port.IfName
m.deviceNetStatus.Ports[ix].Phylabel = port.Phylabel
m.deviceNetStatus.Ports[ix].Logicallabel = port.Logicallabel
m.deviceNetStatus.Ports[ix].Alias = port.Alias
m.deviceNetStatus.Ports[ix].IsMgmt = port.IsMgmt
m.deviceNetStatus.Ports[ix].IsL3Port = port.IsL3Port
m.deviceNetStatus.Ports[ix].Cost = port.Cost
m.deviceNetStatus.Ports[ix].ProxyConfig = port.ProxyConfig
m.deviceNetStatus.Ports[ix].WirelessCfg = port.WirelessCfg
// Set fields from the config...
m.deviceNetStatus.Ports[ix].Dhcp = port.Dhcp
m.deviceNetStatus.Ports[ix].Type = port.Type
_, subnet, _ := net.ParseCIDR(port.AddrSubnet)
if subnet != nil {
m.deviceNetStatus.Ports[ix].Subnet = *subnet
}
// Start with any statically assigned values; update below
m.deviceNetStatus.Ports[ix].DomainName = port.DomainName
m.deviceNetStatus.Ports[ix].DNSServers = port.DnsServers
m.deviceNetStatus.Ports[ix].NtpServer = port.NtpServer
m.deviceNetStatus.Ports[ix].TestResults = port.TestResults
// Do not try to get state data for interface which is in PCIback.
ioBundle := m.adapters.LookupIoBundleIfName(port.IfName)
if ioBundle != nil && ioBundle.IsPCIBack {
err := fmt.Errorf("port %s is in PCIBack - ignored", port.IfName)
m.Log.Warnf("updateDNS: %v", err)
m.deviceNetStatus.Ports[ix].RecordFailure(err.Error())
continue
}
// Get interface state data from the network stack.
ifindex, exists, err := m.NetworkMonitor.GetInterfaceIndex(port.IfName)
if !exists || err != nil {
err = fmt.Errorf("port %s does not exist - ignored", port.IfName)
m.Log.Warnf("updateDNS: %v", err)
m.deviceNetStatus.Ports[ix].RecordFailure(err.Error())
continue
}
var isUp bool
ifAttrs, err := m.NetworkMonitor.GetInterfaceAttrs(ifindex)
if err != nil {
m.Log.Warnf(
"updateDNS: failed to get attrs for interface %s with index %d: %v",
port.IfName, ifindex, err)
} else {
isUp = ifAttrs.AdminUp
}
m.deviceNetStatus.Ports[ix].Up = isUp
ipAddrs, macAddr, err := m.NetworkMonitor.GetInterfaceAddrs(ifindex)
if err != nil {
m.Log.Warnf(
"updateDNS: failed to get IP addresses for interface %s with index %d: %v",
port.IfName, ifindex, err)
ipAddrs = nil
}
m.deviceNetStatus.Ports[ix].MacAddr = macAddr.String()
m.deviceNetStatus.Ports[ix].AddrInfoList = make([]types.AddrInfo, len(ipAddrs))
if len(ipAddrs) == 0 {
m.Log.Functionf("updateDNS: interface %s has NO IP addresses", port.IfName)
}
for i, addr := range ipAddrs {
m.deviceNetStatus.Ports[ix].AddrInfoList[i].Addr = addr.IP
}
// Get DNS etc info from dhcpcd. Updates DomainName and DnsServers.
err = m.getDHCPInfo(&m.deviceNetStatus.Ports[ix])
if err != nil && dpc.State != types.DPCStateAsyncWait {
m.Log.Error(err)
}
err = m.getDNSInfo(&m.deviceNetStatus.Ports[ix])
if err != nil && dpc.State != types.DPCStateAsyncWait {
m.Log.Error(err)
}
// Get used default routers aka gateways from kernel
gws, err := m.NetworkMonitor.GetInterfaceDefaultGWs(ifindex)
if err != nil {
m.Log.Warnf(
"updateDNS: failed to get default GWs for interface %s with index %d: %v",
port.IfName, ifindex, err)
gws = nil
}
m.deviceNetStatus.Ports[ix].DefaultRouters = gws
// Attempt to get a wpad.dat file if so configured
// Result is updating the Pacfile
// We always redo this since we don't know what has changed
// from the previous DeviceNetworkStatus.
err = devicenetwork.CheckAndGetNetworkProxy(
m.Log, &m.deviceNetStatus.Ports[ix], m.ZedcloudMetrics)
if err != nil {
err = fmt.Errorf("updateDNS: CheckAndGetNetworkProxy failed for %s: %v",
port.IfName, err)
// XXX where can we return this failure?
// Already have TestResults set from above
m.Log.Error(err)
}
// If this is a cellular network connectivity, add status information
// obtained from the wwan service.
if port.WirelessCfg.WType == types.WirelessTypeCellular {
wwanNetStatus, found := m.wwanStatus.LookupNetworkStatus(port.Logicallabel)
if found {
m.deviceNetStatus.Ports[ix].WirelessStatus = types.WirelessStatus{
WType: types.WirelessTypeCellular,
Cellular: wwanNetStatus,
}
}
}
}
// Preserve geo info for existing interface and IP address
for portIdx := range m.deviceNetStatus.Ports {
port := &m.deviceNetStatus.Ports[portIdx]
for addrIdx := range port.AddrInfoList {
// Need pointer since we are going to modify
ai := &port.AddrInfoList[addrIdx]
oai := lookupPortStatusAddr(oldPorts, port.IfName, ai.Addr)
if oai == nil {
continue
}
ai.Geo = oai.Geo
ai.LastGeoTimestamp = oai.LastGeoTimestamp
}
}
}
func (m *DpcManager) publishDNS() {
err := m.PubDeviceNetworkStatus.Publish("global", m.deviceNetStatus)
if err != nil {
m.Log.Errorf("failed to publish DNS: %v", err)
}
}
func (m *DpcManager) updateGeo() {
var change bool
for idx := range m.deviceNetStatus.Ports {
port := &m.deviceNetStatus.Ports[idx]
if m.deviceNetStatus.Version >= types.DPCIsMgmt && !port.IsMgmt {
continue
}
for i := range port.AddrInfoList {
// Need pointer since we are going to modify.
ai := &port.AddrInfoList[i]
if ai.Addr.IsLinkLocalUnicast() {
continue
}
numDNSServers := types.CountDNSServers(m.deviceNetStatus, port.IfName)
if numDNSServers == 0 {
continue
}
timePassed := time.Since(ai.LastGeoTimestamp)
if timePassed < m.geoRedoInterval {
continue
}
info, err := m.GeoService.GetGeolocationInfo(ai.Addr)
if err != nil {
// Ignore error
m.Log.Functionf("updateGeo: GetGeolocationInfo failed %s\n", err)
continue
}
// Note that if the global IP is unchanged we don't update anything.
if info == nil || info.IP == ai.Geo.IP {
continue
}
m.Log.Functionf("updateGeo: MyIPInfo changed from %v to %v\n",
ai.Geo, *info)
ai.Geo = *info
ai.LastGeoTimestamp = time.Now()
change = true
}
}
if change {
m.publishDNS()
}
}
func (m *DpcManager) getDHCPInfo(port *types.NetworkPortStatus) error {
if port.Dhcp != types.DT_CLIENT {
return nil
}
if port.WirelessStatus.WType == types.WirelessTypeCellular {
return nil
}
ifIndex, exists, err := m.NetworkMonitor.GetInterfaceIndex(port.IfName)
if !exists {
return nil
}
if err != nil {
return fmt.Errorf("getDHCPInfo: failed to get index for interface %s: %v",
port.IfName, err)
}
dhcpInfo, err := m.NetworkMonitor.GetInterfaceDHCPInfo(ifIndex)
if err != nil {
return fmt.Errorf("getDHCPInfo: failed to get DHCP info for interface %s: %v",
port.IfName, err)
}
if dhcpInfo.Subnet != nil {
port.Subnet = *dhcpInfo.Subnet
}
port.NtpServers = dhcpInfo.NtpServers
return nil
}
func (m *DpcManager) getDNSInfo(port *types.NetworkPortStatus) error {
ifIndex, exists, err := m.NetworkMonitor.GetInterfaceIndex(port.IfName)
if !exists {
return nil
}
if err != nil {
return fmt.Errorf("getDNSInfo: failed to get index for interface %s: %v",
port.IfName, err)
}
dnsInfo, err := m.NetworkMonitor.GetInterfaceDNSInfo(ifIndex)
if err != nil {
return fmt.Errorf("getDNSInfo: failed to get DNS info for interface %s: %v",
port.IfName, err)
}
port.DNSServers = dnsInfo.DNSServers
// XXX just pick first since have one DomainName slot
if len(dnsInfo.Domains) > 0 {
port.DomainName = dnsInfo.Domains[0]
}
return nil
}
func lookupPortStatusAddr(ports []types.NetworkPortStatus,
ifname string, addr net.IP) *types.AddrInfo {
for _, port := range ports {
if port.IfName != ifname {
continue
}
for _, ai := range port.AddrInfoList {
if ai.Addr.Equal(addr) {
return &ai
}
}
}
return nil
}