-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrivilegedOperations.cpp
197 lines (185 loc) · 5.36 KB
/
PrivilegedOperations.cpp
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
#include "PrivilegedOperations.hpp"
#include "Logger.hpp"
#include "LibcError.hpp"
#include "Utils.hpp"
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_tun.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <wait.h>
struct Packet
{
enum struct Type {CREATE_TUNNEL, ADD_ROUTE};
Type type;
union
{
struct
{
char name[IFNAMSIZ];
char local[32];
char remote[32];
} tunnel;
struct
{
char route[32];
char router[32];
} route;
};
};
static void send_fd(int sock, int fd)
{
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
struct cmsghdr *cmsg;
char buf[CMSG_SPACE(sizeof(fd))]; /* ancillary data buffer */
msg.msg_control = buf;
msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
/* Initialize the payload: */
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmsg->cmsg_len;
CHECK(sendmsg(sock, &msg, 0));
Logger::global()->debug("Send tunnel descriptor {}", fd);
}
static int recv_fd(int sock)
{
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
struct cmsghdr *cmsg;
int fd;
char buf[CMSG_SPACE(sizeof(fd))]; /* ancillary data buffer */
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmsg->cmsg_len;
CHECK(recvmsg(sock, &msg, 0));
fd=*reinterpret_cast<int*>(CMSG_DATA(cmsg));
Logger::global()->debug("Recv tunnel descriptor {}", fd);
return fd;
}
void PrivilegedOperations::work()
{
for(;;)
{
Packet p;
ssize_t n=recv(sock, &p, sizeof(p), 0);
Logger::global()->debug("Privileged worker received {} bytes", n);
if(n==0)
break;
assert(n==sizeof(p));
try
{
if(p.type==Packet::Type::CREATE_TUNNEL)
{
processCreateTunnel(p.tunnel.name, p.tunnel.local, p.tunnel.remote);
}
if(p.type==Packet::Type::ADD_ROUTE)
{
processAddRoute(p.route.route, p.route.router);
}
}
catch(const std::exception &e)
{
Logger::global()->error("{}", e.what());
}
}
exit(0);
}
static void fill(sockaddr &dst, const std::string& ip)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
in_addr_t x=inet_addr(ip.c_str());
memcpy(&addr.sin_addr, &x, sizeof(x));
memcpy(&dst, &addr, sizeof(addr));
}
void PrivilegedOperations::processCreateTunnel(const char *name, const char *local, const char *remote)
{
Logger::global()->info("Setting up tunnel {}: {} -> {}", name, local, remote);
int fd,helper;
CHECK(fd=open("/dev/net/tun",O_RDWR|O_CLOEXEC));
ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
sensibleCopy(ifr.ifr_name,name,IFNAMSIZ);
CHECK(ioctl(fd, TUNSETIFF, &ifr));
CHECK(fcntl(fd, F_SETFL, O_NONBLOCK));
CHECK(helper=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP));
fill(ifr.ifr_addr, local);
CHECK(ioctl(helper, SIOCSIFADDR, &ifr));
fill(ifr.ifr_addr, remote);
CHECK(ioctl(helper, SIOCSIFDSTADDR, &ifr));
CHECK(ioctl(helper, SIOCGIFFLAGS, &ifr));
ifr.ifr_flags|=IFF_UP|IFF_RUNNING;
CHECK(ioctl(helper, SIOCSIFFLAGS, &ifr));
close(helper);
send_fd(sock, fd);
close(fd);
}
void PrivilegedOperations::processAddRoute(const char *route, const char *router)
{
Logger::global()->info("Adding route {} via {}", route, router);
pid_t pid;
CHECK(pid=fork());
if(pid==0)
{
CHECK(execlp("ip","ip","r","a",route,"via",router,"metric","100", NULL));
exit(0);
}
WARN(waitpid(pid, NULL, 0));
}
void PrivilegedOperations::start()
{
int sockets[2];
CHECK(socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sockets));
pid_t pid;
CHECK(pid=fork());
if(pid==0)
{
this->sock=sockets[0];
close(sockets[1]);
work();
exit(0);
}
else
{
close(sockets[0]);
this->sock=sockets[1];
}
}
int PrivilegedOperations::createTunnel(const std::string& name, const std::string& local, const std::string& remote)
{
Packet p;
memset(&p,0,sizeof(p));
p.type=Packet::Type::CREATE_TUNNEL;
sensibleCopy(p.tunnel.name,name.c_str(),sizeof(p.tunnel.name));
sensibleCopy(p.tunnel.local, local.c_str(), sizeof(p.tunnel.local));
sensibleCopy(p.tunnel.remote, remote.c_str(), sizeof(p.tunnel.remote));
CHECK(send(sock, &p, sizeof(p), 0));
return recv_fd(sock);
}
void PrivilegedOperations::addRoute(const std::string &route, const std::string& router)
{
Packet p;
memset(&p,0,sizeof(p));
p.type=Packet::Type::ADD_ROUTE;
sensibleCopy(p.route.route,route.c_str(),sizeof(p.route.route));
sensibleCopy(p.route.router,router.c_str(),sizeof(p.route.router));
CHECK(send(sock, &p, sizeof(p), 0));
}