forked from canonical/lxd
/
proxy.go
576 lines (478 loc) · 15.7 KB
/
proxy.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
package device
import (
"bufio"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
liblxc "gopkg.in/lxc/go-lxc.v2"
"github.com/lxc/lxd/lxd/apparmor"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
"github.com/lxc/lxd/lxd/device/nictype"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/util"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logger"
"github.com/lxc/lxd/shared/subprocess"
"github.com/lxc/lxd/shared/validate"
)
type proxy struct {
deviceCommon
}
type proxyProcInfo struct {
listenPid string
listenPidFd string
connectPid string
connectPidFd string
connectAddr string
listenAddr string
listenAddrGID string
listenAddrUID string
listenAddrMode string
securityUID string
securityGID string
proxyProtocol string
inheritFds []*os.File
}
// validateConfig checks the supplied config for correctness.
func (d *proxy) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container, instancetype.VM) {
return ErrUnsupportedDevType
}
validateAddr := func(input string) error {
_, err := ProxyParseAddr(input)
return err
}
// Supported bind types are: "host" or "instance" (or "guest" or "container", legacy options equivalent to "instance").
// If an empty value is supplied the default behavior is to assume "host" bind mode.
validateBind := func(input string) error {
if !shared.StringInSlice(d.config["bind"], []string{"", "host", "instance", "guest", "container"}) {
return fmt.Errorf("Invalid binding side given. Must be \"host\" or \"instance\"")
}
return nil
}
rules := map[string]func(string) error{
"listen": validateAddr,
"connect": validateAddr,
"bind": validateBind,
"mode": unixValidOctalFileMode,
"nat": validate.Optional(validate.IsBool),
"gid": unixValidUserID,
"uid": unixValidUserID,
"security.uid": unixValidUserID,
"security.gid": unixValidUserID,
"proxy_protocol": validate.Optional(validate.IsBool),
}
err := d.config.Validate(rules)
if err != nil {
return err
}
if instConf.Type() == instancetype.VM && !shared.IsTrue(d.config["nat"]) {
return fmt.Errorf("Only NAT mode is supported for proxies on VM instances")
}
listenAddr, err := ProxyParseAddr(d.config["listen"])
if err != nil {
return err
}
connectAddr, err := ProxyParseAddr(d.config["connect"])
if err != nil {
return err
}
if len(connectAddr.Addr) > len(listenAddr.Addr) {
// Cannot support single port -> multiple port
return fmt.Errorf("Cannot map a single port to multiple ports")
}
if shared.IsTrue(d.config["proxy_protocol"]) && (!strings.HasPrefix(d.config["connect"], "tcp") || shared.IsTrue(d.config["nat"])) {
return fmt.Errorf("The PROXY header can only be sent to tcp servers in non-nat mode")
}
if (!strings.HasPrefix(d.config["listen"], "unix:") || strings.HasPrefix(d.config["listen"], "unix:@")) &&
(d.config["uid"] != "" || d.config["gid"] != "" || d.config["mode"] != "") {
return fmt.Errorf("Only proxy devices for non-abstract unix sockets can carry uid, gid, or mode properties")
}
if shared.IsTrue(d.config["nat"]) {
if d.config["bind"] != "" && d.config["bind"] != "host" {
return fmt.Errorf("Only host-bound proxies can use NAT")
}
// Support TCP <-> TCP and UDP <-> UDP
if listenAddr.ConnType == "unix" || connectAddr.ConnType == "unix" || listenAddr.ConnType != connectAddr.ConnType {
return fmt.Errorf("Proxying %s <-> %s is not supported when using NAT", listenAddr.ConnType, connectAddr.ConnType)
}
var ipVersion uint // Records which IP version we are using, as these cannot be mixed in NAT mode.
for _, listenAddrStr := range listenAddr.Addr {
ipStr, _, err := net.SplitHostPort(listenAddrStr)
if err != nil {
return err
}
ip := net.ParseIP(ipStr)
if ip.Equal(net.IPv4zero) || ip.Equal(net.IPv6zero) {
return fmt.Errorf("Cannot listen on wildcard address %q when in nat mode", ip)
}
// Record the listen IP version if not record already.
if ipVersion == 0 {
if ip.To4() == nil {
ipVersion = 6
} else {
ipVersion = 4
}
}
}
// Check each connect address against the listen IP version and check they match.
for _, connectAddrStr := range connectAddr.Addr {
ipStr, _, err := net.SplitHostPort(connectAddrStr)
if err != nil {
return err
}
ipTo4 := net.ParseIP(ipStr).To4()
if ipTo4 == nil && ipVersion != 6 || ipTo4 != nil && ipVersion != 4 {
return fmt.Errorf("Cannot mix IP versions between listen and connect in nat mode")
}
}
}
return nil
}
// validateEnvironment checks the runtime environment for correctness.
func (d *proxy) validateEnvironment() error {
if d.name == "" {
return fmt.Errorf("Device name cannot be empty")
}
return nil
}
// Start is run when the device is added to the instance.
func (d *proxy) Start() (*deviceConfig.RunConfig, error) {
err := d.validateEnvironment()
if err != nil {
return nil, err
}
// Proxy devices have to be setup once the instance is running.
runConf := deviceConfig.RunConfig{}
runConf.PostHooks = []func() error{
func() error {
if shared.IsTrue(d.config["nat"]) {
return d.setupNAT()
}
proxyValues, err := d.setupProxyProcInfo()
if err != nil {
return err
}
devFileName := fmt.Sprintf("proxy.%s", d.name)
pidPath := filepath.Join(d.inst.DevicesPath(), devFileName)
logFileName := fmt.Sprintf("proxy.%s.log", d.name)
logPath := filepath.Join(d.inst.LogPath(), logFileName)
// Load the apparmor profile
err = apparmor.ForkproxyLoad(d.state, d.inst, d)
if err != nil {
return err
}
// Spawn the daemon using subprocess
command := d.state.OS.ExecPath
forkproxyargs := []string{"forkproxy",
"--",
proxyValues.listenPid,
proxyValues.listenPidFd,
proxyValues.listenAddr,
proxyValues.connectPid,
proxyValues.connectPidFd,
proxyValues.connectAddr,
proxyValues.listenAddrGID,
proxyValues.listenAddrUID,
proxyValues.listenAddrMode,
proxyValues.securityGID,
proxyValues.securityUID,
proxyValues.proxyProtocol,
}
p, err := subprocess.NewProcess(command, forkproxyargs, logPath, logPath)
if err != nil {
return fmt.Errorf("Failed to create subprocess: %s", err)
}
p.SetApparmor(apparmor.ForkproxyProfileName(d.inst, d))
err = p.StartWithFiles(proxyValues.inheritFds)
if err != nil {
return fmt.Errorf("Failed to run: %s %s: %v", command, strings.Join(forkproxyargs, " "), err)
}
for _, file := range proxyValues.inheritFds {
file.Close()
}
err = p.Save(pidPath)
if err != nil {
// Kill Process if started, but could not save the file
err2 := p.Stop()
if err != nil {
return fmt.Errorf("Could not kill subprocess while handling saving error: %s: %s", err, err2)
}
return fmt.Errorf("Failed to save subprocess details: %s", err)
}
// Poll log file a few times until we see "Started" to indicate successful start.
for i := 0; i < 10; i++ {
started, err := d.checkProcStarted(logPath)
if err != nil {
p.Stop()
return fmt.Errorf("Error occurred when starting proxy device: %s", err)
}
if started {
return nil
}
time.Sleep(time.Second)
}
p.Stop()
return fmt.Errorf("Error occurred when starting proxy device, please look in %s", logPath)
},
}
return &runConf, nil
}
// checkProcStarted checks for the "Started" line in the log file. Returns true if found, false
// if not, and error if any other error occurs.
func (d *proxy) checkProcStarted(logPath string) (bool, error) {
file, err := os.Open(logPath)
if err != nil {
return false, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "Status: Started" {
return true, nil
}
if strings.HasPrefix(line, "Error:") {
return false, fmt.Errorf("%s", line)
}
}
err = scanner.Err()
if err != nil {
return false, err
}
return false, nil
}
// Stop is run when the device is removed from the instance.
func (d *proxy) Stop() (*deviceConfig.RunConfig, error) {
// Remove possible iptables entries
err := d.state.Firewall.InstanceClearProxyNAT(d.inst.Project(), d.inst.Name(), d.name)
if err != nil {
logger.Errorf("Failed to remove proxy NAT filters: %v", err)
}
devFileName := fmt.Sprintf("proxy.%s", d.name)
devPath := filepath.Join(d.inst.DevicesPath(), devFileName)
if !shared.PathExists(devPath) {
// There's no proxy process if NAT is enabled
return nil, nil
}
err = d.killProxyProc(devPath)
if err != nil {
return nil, err
}
// Unload apparmor profile.
err = apparmor.ForkproxyUnload(d.state, d.inst, d)
if err != nil {
return nil, err
}
return nil, nil
}
func (d *proxy) setupNAT() error {
listenAddr, err := ProxyParseAddr(d.config["listen"])
if err != nil {
return err
}
connectAddr, err := ProxyParseAddr(d.config["connect"])
if err != nil {
return err
}
connectHost, _, err := net.SplitHostPort(connectAddr.Addr[0])
if err != nil {
return err
}
ipFamily := "ipv4"
if strings.Contains(connectHost, ":") {
ipFamily = "ipv6"
}
var connectIP net.IP
var hostName string
for devName, devConfig := range d.inst.ExpandedDevices() {
if devConfig["type"] != "nic" {
continue
}
nicType, err := nictype.NICType(d.state, d.inst.Project(), devConfig)
if err != nil {
return err
}
if nicType != "bridged" {
continue
}
// Ensure the connect IP matches one of the NIC's static IPs otherwise we could mess with other
// instance's network traffic. If the wildcard address is supplied as the connect host then the
// first bridged NIC which has a static IP address defined is selected as the connect host IP.
if ipFamily == "ipv4" && devConfig["ipv4.address"] != "" {
if connectHost == devConfig["ipv4.address"] || connectHost == "0.0.0.0" {
connectIP = net.ParseIP(devConfig["ipv4.address"])
}
} else if ipFamily == "ipv6" && devConfig["ipv6.address"] != "" {
if connectHost == devConfig["ipv6.address"] || connectHost == "::" {
connectIP = net.ParseIP(devConfig["ipv6.address"])
}
}
if connectIP != nil {
// Get host_name of device so we can enable hairpin mode on bridge port.
hostName = d.inst.ExpandedConfig()[fmt.Sprintf("volatile.%s.host_name", devName)]
break // Found a match, stop searching.
}
}
if connectIP == nil {
return fmt.Errorf("Proxy connect IP cannot be used with any of the instance NICs static IPs")
}
// Override the host part of the connectAddr.Addr to the chosen connect IP.
for i, addr := range connectAddr.Addr {
_, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if ipFamily == "ipv4" {
connectAddr.Addr[i] = fmt.Sprintf("%s:%s", connectIP.String(), port)
} else if ipFamily == "ipv6" {
// IPv6 addresses need to be enclosed in square brackets.
connectAddr.Addr[i] = fmt.Sprintf("[%s]:%s", connectIP.String(), port)
}
}
err = d.checkBridgeNetfilterEnabled(ipFamily)
if err != nil {
logger.Warnf("Proxy bridge netfilter not enabled: %v. Instances using the bridge will not be able to connect to the proxy's listen IP", err)
} else {
if hostName == "" {
return fmt.Errorf("Proxy cannot find bridge port host_name to enable hairpin mode")
}
// br_netfilter is enabled, so we need to enable hairpin mode on instance's bridge port otherwise
// the instances on the bridge will not be able to connect to the proxy device's listn IP and the
// NAT rule added by the firewall below to allow instance <-> instance traffic will also not work.
_, err = shared.RunCommand("bridge", "link", "set", "dev", hostName, "hairpin", "on")
if err != nil {
return errors.Wrapf(err, "Error enabling hairpin mode on bridge port %q", hostName)
}
}
err = d.state.Firewall.InstanceSetupProxyNAT(d.inst.Project(), d.inst.Name(), d.name, listenAddr, connectAddr)
if err != nil {
return err
}
return nil
}
// checkBridgeNetfilterEnabled checks whether the bridge netfilter feature is loaded and enabled.
// If it is not an error is returned. This is needed in order for instances connected to a bridge to access the
// proxy's listen IP on the LXD host, as otherwise the packets from the bridge do not go through the netfilter
// NAT SNAT/MASQUERADE rules.
func (d *proxy) checkBridgeNetfilterEnabled(ipFamily string) error {
sysctlName := "iptables"
if ipFamily == "ipv6" {
sysctlName = "ip6tables"
}
sysctlPath := fmt.Sprintf("net/bridge/bridge-nf-call-%s", sysctlName)
sysctlVal, err := util.SysctlGet(sysctlPath)
if err != nil {
return errors.Wrap(err, "br_netfilter not loaded")
}
sysctlVal = strings.TrimSpace(sysctlVal)
if sysctlVal != "1" {
return fmt.Errorf("br_netfilter sysctl net.bridge.bridge-nf-call-%s=%s", sysctlName, sysctlVal)
}
return nil
}
func (d *proxy) rewriteHostAddr(addr string) string {
fields := strings.SplitN(addr, ":", 2)
proto := fields[0]
addr = fields[1]
if proto == "unix" && !strings.HasPrefix(addr, "@") {
// Unix non-abstract sockets need to be addressed to the host
// filesystem, not be scoped inside the LXD snap.
addr = shared.HostPath(addr)
}
return fmt.Sprintf("%s:%s", proto, addr)
}
func (d *proxy) setupProxyProcInfo() (*proxyProcInfo, error) {
cname := project.Instance(d.inst.Project(), d.inst.Name())
cc, err := liblxc.NewContainer(cname, d.state.OS.LxcPath)
if err != nil {
return nil, err
}
defer cc.Release()
containerPid := strconv.Itoa(cc.InitPid())
lxdPid := strconv.Itoa(os.Getpid())
containerPidFd := -1
lxdPidFd := -1
var inheritFd []*os.File
if d.state.OS.PidFds {
cPidFd, err := cc.InitPidFd()
if err == nil {
dPidFd, err := shared.PidFdOpen(os.Getpid(), 0)
if err == nil {
inheritFd = []*os.File{cPidFd, dPidFd}
containerPidFd = 3
lxdPidFd = 4
}
}
}
var listenPid, listenPidFd, connectPid, connectPidFd string
connectAddr := d.config["connect"]
listenAddr := d.config["listen"]
switch d.config["bind"] {
case "host", "":
listenPid = lxdPid
listenPidFd = fmt.Sprintf("%d", lxdPidFd)
connectPid = containerPid
connectPidFd = fmt.Sprintf("%d", containerPidFd)
listenAddr = d.rewriteHostAddr(listenAddr)
case "instance", "guest", "container":
listenPid = containerPid
listenPidFd = fmt.Sprintf("%d", containerPidFd)
connectPid = lxdPid
connectPidFd = fmt.Sprintf("%d", lxdPidFd)
connectAddr = d.rewriteHostAddr(connectAddr)
default:
return nil, fmt.Errorf("Invalid binding side given. Must be \"host\" or \"instance\"")
}
listenAddrMode := "0644"
if d.config["mode"] != "" {
listenAddrMode = d.config["mode"]
}
p := &proxyProcInfo{
listenPid: listenPid,
listenPidFd: listenPidFd,
connectPid: connectPid,
connectPidFd: connectPidFd,
connectAddr: connectAddr,
listenAddr: listenAddr,
listenAddrGID: d.config["gid"],
listenAddrUID: d.config["uid"],
listenAddrMode: listenAddrMode,
securityGID: d.config["security.gid"],
securityUID: d.config["security.uid"],
proxyProtocol: d.config["proxy_protocol"],
inheritFds: inheritFd,
}
return p, nil
}
func (d *proxy) killProxyProc(pidPath string) error {
// If the pid file doesn't exist, there is no process to kill.
if !shared.PathExists(pidPath) {
return nil
}
p, err := subprocess.ImportProcess(pidPath)
if err != nil {
return fmt.Errorf("Could not read pid file: %s", err)
}
err = p.Stop()
if err != nil && err != subprocess.ErrNotRunning {
return fmt.Errorf("Unable to kill forkproxy: %s", err)
}
os.Remove(pidPath)
return nil
}
func (d *proxy) Remove() error {
// Delete apparmor profile.
err := apparmor.ForkproxyDelete(d.state, d.inst, d)
if err != nil {
return err
}
return nil
}