-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_qemu.go
150 lines (131 loc) · 3.49 KB
/
relay_qemu.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
package tpmproxy
import (
"errors"
"log"
"net"
"os"
"github.com/opencontainers/runc/libcontainer/utils"
)
// QemuCtrlRelayer is a relayer for the UNIXIO control channel of QEMU.
// It listens on a UNIX domain socket for incoming connections from QEMU.
// Upon receiving a connection, it creates a pair of forwarders for the
// control channel and the server channel, and starts exchanging messages
// between the two channels.
// The relayer can be configured to terminate upon closing of either channel.
// The relayer can also be configured with an interceptor to intercept and
// modify messages.
type QemuCtrlRelayer struct {
CtrlSockFile string
ForwarderFactory ForwarderFactory
CtrlForwarderFactory ForwarderFactory
TerminateOnClose bool
Interceptor Interceptor
Terminate chan interface{}
}
func NewQemuCtrlRelayer(ctrlSockFile string,
forwarderFactory ForwarderFactory, ctrlForwarderFactory ForwarderFactory,
terminateOnClose bool, interceptor Interceptor) *QemuCtrlRelayer {
return &QemuCtrlRelayer{
CtrlSockFile: ctrlSockFile,
ForwarderFactory: forwarderFactory,
CtrlForwarderFactory: ctrlForwarderFactory,
TerminateOnClose: terminateOnClose,
Interceptor: interceptor,
Terminate: make(chan interface{}),
}
}
func (r *QemuCtrlRelayer) HandleConnLoop(qemuCtrlConn net.Conn) error {
qemuCtrlUnixConn, ok := qemuCtrlConn.(*net.UnixConn)
if !ok {
return errors.New("not a unix conn")
}
qemuCtrlUnixFile, err := qemuCtrlUnixConn.File()
if err != nil {
return err
}
qemuServerFd, err := utils.RecvFd(qemuCtrlUnixFile)
if err != nil {
return err
}
if _, err := qemuCtrlConn.Write([]byte{0x00, 0x00, 0x00, 0x00}); err != nil {
return err
}
fwd, err := r.ForwarderFactory.NewForwarder()
if err != nil {
return err
}
ctrlFwd, err := r.CtrlForwarderFactory.NewForwarder()
if err != nil {
return err
}
go func(qemuServerFd *os.File, fwd Forwarder) {
defer fwd.Close()
defer qemuServerFd.Close()
var handlerFactory RequestResponseHandlerFactory
if r.Interceptor != nil {
handlerFactory = &TpmRequestResponseHandlerFactory{
Interceptor: r.Interceptor,
}
} else {
handlerFactory = &NopRequestResponseHandlerFactory{}
}
ex := &Exchanger{
Src: qemuServerFd,
Dst: fwd,
HandlerFactory: handlerFactory,
}
if err := ex.Exchange(); err != nil {
log.Printf("server exchange error: %v\n", err)
}
if r.TerminateOnClose {
r.Terminate <- nil
}
}(qemuServerFd, fwd)
go func(qemuCtrlConn net.Conn, ctrlFwd Forwarder) {
defer ctrlFwd.Close()
defer qemuCtrlConn.Close()
ex := &Exchanger{
Src: qemuCtrlConn,
Dst: ctrlFwd,
HandlerFactory: &NopRequestResponseHandlerFactory{},
}
if err := ex.Exchange(); err != nil {
log.Printf("ctrl exchange error: %v\n", err)
}
if r.TerminateOnClose {
r.Terminate <- nil
}
}(qemuCtrlConn, ctrlFwd)
return nil
}
func (r *QemuCtrlRelayer) Relay() error {
os.Remove(r.CtrlSockFile)
listener, err := net.Listen("unix", r.CtrlSockFile)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
if err := os.Chmod(r.CtrlSockFile, 0666); err != nil {
return err
}
newConns := make(chan net.Conn)
go func() {
for {
newConn, err := listener.Accept()
if err != nil {
newConns <- nil
r.Terminate <- nil
return
}
newConns <- newConn
}
}()
for {
select {
case conn := <-newConns:
r.HandleConnLoop(conn)
case <-r.Terminate:
return nil
}
}
}