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
|
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#ifndef ZT_NETCONETHERNETTAP_HPP
#define ZT_NETCONETHERNETTAP_HPP
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <utility>
#include <stdexcept>
#include <stdint.h>
#include "../node/Constants.hpp"
#include "../node/MulticastGroup.hpp"
#include "../node/Mutex.hpp"
#include "../node/InetAddress.hpp"
#include "../osdep/Thread.hpp"
#include "../osdep/Phy.hpp"
#include "netif/etharp.h"
#include "RPC.h"
struct tcp_pcb;
struct socket_st;
struct listen_st;
struct bind_st;
struct connect_st;
struct getsockname_st;
struct accept_st;
#define APPLICATION_POLL_FREQ 50
#define ZT_LWIP_TCP_TIMER_INTERVAL 5
#define STATUS_TMR_INTERVAL 1000 // How often we check connection statuses (in ms)
#define DEFAULT_BUF_SZ 1024 * 1024 * 2
namespace ZeroTier {
class NetconEthernetTap;
class LWIPStack;
/*
* TCP connection administered by service
*/
struct TcpConnection
{
bool listening;
int pid, txsz, rxsz;
PhySocket *rpcSock, *sock;
struct tcp_pcb *pcb;
struct sockaddr_storage *addr;
unsigned char txbuf[DEFAULT_BUF_SZ];
unsigned char rxbuf[DEFAULT_BUF_SZ];
};
/*
* A helper for passing a reference to _phy to LWIP callbacks as a "state"
*/
struct Larg
{
NetconEthernetTap *tap;
TcpConnection *conn;
Larg(NetconEthernetTap *_tap, TcpConnection *conn) : tap(_tap), conn(conn) {}
};
/*
* Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
*/
class NetconEthernetTap
{
friend class Phy<NetconEthernetTap *>;
public:
NetconEthernetTap(
const char *homePath,
const MAC &mac,
unsigned int mtu,
unsigned int metric,
uint64_t nwid,
const char *friendlyName,
void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
void *arg);
~NetconEthernetTap();
void setEnabled(bool en);
bool enabled() const;
bool addIp(const InetAddress &ip);
bool removeIp(const InetAddress &ip);
std::vector<InetAddress> ips() const;
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
std::string deviceName() const;
void setFriendlyName(const char *friendlyName);
void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
void threadMain()
throw();
LWIPStack *lwipstack;
uint64_t _nwid;
void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
void *_arg;
private:
// LWIP callbacks
// NOTE: these are called from within LWIP, meaning that lwipstack->_lock is ALREADY
// locked in this case!
/*
* Callback from LWIP for when a connection has been accepted and the PCB has been
* put into an ACCEPT state.
*
* A socketpair is created, one end is kept and wrapped into a PhySocket object
* for use in the main ZT I/O loop, and one end is sent to the client. The client
* is then required to tell the service what new file descriptor it has allocated
* for this connection. After the mapping is complete, the accepted socket can be
* used.
*
* @param associated service state object
* @param newly allocated PCB
* @param error code
* @return ERR_OK if everything is ok, -1 otherwise
*
* i := should be implemented in intercept lib
* I := is implemented in intercept lib
* X := is implemented in service
* ? := required treatment Unknown
* - := Not needed
*
* [ ] EAGAIN or EWOULDBLOCK - The socket is marked nonblocking and no connections are present
* to be accepted. POSIX.1-2001 allows either error to be returned for
* this case, and does not require these constants to have the same value,
* so a portable application should check for both possibilities.
* [I] EBADF - The descriptor is invalid.
* [I] ECONNABORTED - A connection has been aborted.
* [i] EFAULT - The addr argument is not in a writable part of the user address space.
* [-] EINTR - The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
* [I] EINVAL - Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
* [I] EINVAL - (accept4()) invalid value in flags.
* [I] EMFILE - The per-process limit of open file descriptors has been reached.
* [ ] ENFILE - The system limit on the total number of open files has been reached.
* [ ] ENOBUFS, ENOMEM - Not enough free memory. This often means that the memory allocation is
* limited by the socket buffer limits, not by the system memory.
* [I] ENOTSOCK - The descriptor references a file, not a socket.
* [I] EOPNOTSUPP - The referenced socket is not of type SOCK_STREAM.
* [ ] EPROTO - Protocol error.
*
*/
static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
/*
* Callback from LWIP for when data is available to be read from the network.
*
* Data is in the form of a linked list of struct pbufs, it is then recombined and
* send to the client over the associated unix socket.
*
* @param associated service state object
* @param allocated PCB
* @param chain of pbufs
* @param error code
* @return ERR_OK if everything is ok, -1 otherwise
*
*/
static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
/*
* Callback from LWIP when an internal error is associtated with the given (arg)
*
* Since the PCB related to this error might no longer exist, only its perviously
* associated (arg) is provided to us.
*
* @param associated service state object
* @param error code
*
*/
static void nc_err(void *arg, err_t err);
/*
* Callback from LWIP to do whatever work we might need to do.
*
* @param associated service state object
* @param PCB we're polling on
* @return ERR_OK if everything is ok, -1 otherwise
*
*/
static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
/*
* Callback from LWIP to signal that 'len' bytes have successfully been sent.
* As a result, we should put our socket back into a notify-on-readability state
* since there is now room on the PCB buffer to write to.
*
* NOTE: This could be used to track the amount of data sent by a connection.
*
* @param associated service state object
* @param relevant PCB
* @param length of data sent
* @return ERR_OK if everything is ok, -1 otherwise
*
*/
static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
/*
* Callback from LWIP which sends a return value to the client to signal that
* a connection was established for this PCB
*
* @param associated service state object
* @param relevant PCB
* @param error code
* @return ERR_OK if everything is ok, -1 otherwise
*
*/
static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
//static void nc_close(struct tcp_pcb *PCB);
//static err_t nc_send(struct tcp_pcb *PCB);
/*
* Handles an RPC to bind an LWIP PCB to a given address and port
*
* @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC
*
i := should be implemented in intercept lib
I := is implemented in intercept lib
X := is implemented in service
? := required treatment Unknown
- := Not needed
[ ] EACCES - The address is protected, and the user is not the superuser.
[X] EADDRINUSE - The given address is already in use.
[I] EBADF - sockfd is not a valid descriptor.
[X] EINVAL - The socket is already bound to an address.
[I] ENOTSOCK - sockfd is a descriptor for a file, not a socket.
[X] ENOMEM - Insufficient kernel memory was available.
- The following errors are specific to UNIX domain (AF_UNIX) sockets:
[-] EACCES - Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
[-] EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local.
[-] EFAULT - addr points outside the user's accessible address space.
[-] EINVAL - The addrlen is wrong, or the socket was not in the AF_UNIX family.
[-] ELOOP - Too many symbolic links were encountered in resolving addr.
[-] ENAMETOOLONG - s addr is too long.
[-] ENOENT - The file does not exist.
[-] ENOTDIR - A component of the path prefix is not a directory.
[-] EROFS - The socket inode would reside on a read-only file system.
*/
void handleBind(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct bind_st *bind_rpc);
/*
* Handles an RPC to put an LWIP PCB into LISTEN mode
*
* @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC
*
i := should be implemented in intercept lib
I := is implemented in intercept lib
X := is implemented in service
? := required treatment Unknown
- := Not needed
[?] EADDRINUSE - Another socket is already listening on the same port.
[IX] EBADF - The argument sockfd is not a valid descriptor.
[I] ENOTSOCK - The argument sockfd is not a socket.
[I] EOPNOTSUPP - The socket is not of a type that supports the listen() operation.
*/
void handleListen(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct listen_st *listen_rpc);
/*
* Handles an RPC to create a socket (LWIP PCB and associated socketpair)
*
* A socketpair is created, one end is kept and wrapped into a PhySocket object
* for use in the main ZT I/O loop, and one end is sent to the client. The client
* is then required to tell the service what new file descriptor it has allocated
* for this connection. After the mapping is complete, the socket can be used.
*
* @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC
*
i := should be implemented in intercept lib
I := is implemented in intercept lib
X := is implemented in service
? := required treatment Unknown
- := Not needed
[-] EACCES - Permission to create a socket of the specified type and/or protocol is denied.
[I] EAFNOSUPPORT - The implementation does not support the specified address family.
[I] EINVAL - Unknown protocol, or protocol family not available.
[I] EINVAL - Invalid flags in type.
[I] EMFILE - Process file table overflow.
[?] ENFILE - The system limit on the total number of open files has been reached.
[X] ENOBUFS or ENOMEM - Insufficient memory is available. The socket cannot be created until sufficient resources are freed.
[?] EPROTONOSUPPORT - The protocol type or the specified protocol is not supported within this domain.
*/
TcpConnection * handleSocket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc);
/*
* Handles an RPC to connect to a given address and port
*
* @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC
--- Error handling in this method will only catch problems which are immedately
apprent. Some errors will need to be caught in the nc_connected(0 callback
i := should be implemented in intercept lib
I := is implemented in intercept lib
X := is implemented in service
? := required treatment Unknown
- := Not needed
[-] EACCES - For UNIX domain sockets, which are identified by pathname: Write permission is denied ...
[?] EACCES, EPERM - The user tried to connect to a broadcast address without having the socket broadcast flag enabled ...
[X] EADDRINUSE - Local address is already in use.
[I] EAFNOSUPPORT - The passed address didn't have the correct address family in its sa_family field.
[X] EAGAIN - No more free local ports or insufficient entries in the routing cache.
[ ] EALREADY - The socket is nonblocking and a previous connection attempt has not yet been completed.
[IX] EBADF - The file descriptor is not a valid index in the descriptor table.
[ ] ECONNREFUSED - No-one listening on the remote address.
[i] EFAULT - The socket structure address is outside the user's address space.
[ ] EINPROGRESS - The socket is nonblocking and the connection cannot be completed immediately.
[-] EINTR - The system call was interrupted by a signal that was caught.
[X] EISCONN - The socket is already connected.
[X] ENETUNREACH - Network is unreachable.
[I] ENOTSOCK - The file descriptor is not associated with a socket.
[X] ETIMEDOUT - Timeout while attempting connection.
[X] EINVAL - Invalid argument, SVr4, generally makes sense to set this
*/
void handleConnect(PhySocket *sock, PhySocket *rpcsock, TcpConnection *conn, struct connect_st* connect_rpc);
/*
* Return the address that the socket is bound to
*/
void handleGetsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
/*
* Writes data from the application's socket to the LWIP connection
*/
void handleWrite(TcpConnection *conn);
/*
* Sends a return value to the intercepted application
*/
int sendReturnValue(PhySocket *sock, int retval, int _errno);
int sendReturnValue(int fd, int retval, int _errno);
/*
* Unpacks the buffer from an RPC command
*/
void unloadRPC(void *data, pid_t &pid, pid_t &tid,
int &rpc_count, char (timestamp[RPC_TIMESTAMP_SZ]), char (magic[sizeof(uint64_t)]), char &cmd, void* &payload);
// Unused -- no UDP or TCP from this thread/Phy<>
void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
void phyOnTcpClose(PhySocket *sock,void **uptr);
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
void phyOnTcpWritable(PhySocket *sock,void **uptr);
/*
* Signals us to close the TcpConnection associated with this PhySocket
*/
void phyOnUnixClose(PhySocket *sock,void **uptr);
/*
* Notifies us that there is data to be read from an application's socket
*/
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
/*
* Notifies us that we can write to an application's socket
*/
void phyOnUnixWritable(PhySocket *sock,void **uptr);
/*
* Returns a pointer to a TcpConnection associated with a given PhySocket
*/
TcpConnection *getConnection(PhySocket *sock);
/*
* Safely adds a new TcpConnection to _TcpConnections
*/
TcpConnection *addConnection(TcpConnection *conn);
/*
* Safely removes a TcpConnection from _TcpConnections
*/
void removeConnection(TcpConnection *conn);
/*
* Closes a TcpConnection, associated LWIP PCB strcuture,
* PhySocket, and underlying file descriptor
*/
void closeConnection(PhySocket *sock);
ip_addr_t convert_ip(struct sockaddr_in * addr)
{
ip_addr_t conn_addr;
struct sockaddr_in *ipv4 = addr;
short a = ip4_addr1(&(ipv4->sin_addr));
short b = ip4_addr2(&(ipv4->sin_addr));
short c = ip4_addr3(&(ipv4->sin_addr));
short d = ip4_addr4(&(ipv4->sin_addr));
IP4_ADDR(&conn_addr, a,b,c,d);
return conn_addr;
}
Phy<NetconEthernetTap *> _phy;
PhySocket *_unixListenSocket;
std::vector<TcpConnection*> _TcpConnections;
std::map<uint64_t, std::pair<PhySocket*, void*> > jobmap;
pid_t rpcCounter;
netif interface;
MAC _mac;
Thread _thread;
std::string _homePath;
std::string _dev; // path to Unix domain socket
std::vector<MulticastGroup> _multicastGroups;
Mutex _multicastGroups_m;
std::vector<InetAddress> _ips;
Mutex _ips_m, _tcpconns_m, _rx_buf_m;
unsigned int _mtu;
volatile bool _enabled;
volatile bool _run;
};
} // namespace ZeroTier
#endif
|