Coda Distributed File System
codatunnel.private.h
Go to the documentation of this file.
1/* BLURB lgpl
2
3 Coda File System
4 Release 8
5
6 Copyright (c) 2017-2021 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU Library General Public Licence Version 2, as
11shown in the file LICENSE. The technical and financial contributors to
12Coda are listed in the file CREDITS.
13
14 Additional copyrights
15
16#*/
17
18#ifndef _CODATUNNEL_PRIVATE_H_
19#define _CODATUNNEL_PRIVATE_H_
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <assert.h>
24#include <sys/time.h>
25#include <uv.h>
26#include <gnutls/gnutls.h>
27
28int mapthread(uv_thread_t);
29
30#if 0
31#define DEBUG(...) \
32 do { \
33 struct timeval tt; \
34 gettimeofday(&tt, 0); \
35 int myid = mapthread(uv_thread_self()); \
36 fprintf(stderr, "[%d] %ld:%ld %s:%d ", myid, tt.tv_sec, tt.tv_usec, \
37 __FUNCTION__, __LINE__); \
38 fprintf(stderr, __VA_ARGS__); \
39 fflush(stderr); \
40 } while (0)
41#else
42#define DEBUG(...)
43#endif
44
45#define ERROR(...) \
46 do { \
47 fprintf(stderr, "%s:%d ", __FUNCTION__, __LINE__); \
48 fprintf(stderr, __VA_ARGS__); \
49 fflush(stderr); \
50 } while (0)
51
52/* the actual tunnel daemon (defined in codatunneld.c) */
53void codatunneld(int codatunnel_sockfd, const char *tcp_bindaddr,
54 const char *udp_bindaddr, const char *bind_service,
55 int onlytcp, const char *sslcertdir) __attribute__((noreturn));
56
57/* Format of encapsulated UDP packets sent on Unix domain connections
58 (i.e., between Venus and codatunneld, and between codasrv and
59 codatunneld.) All fields are in the clear. This header is followed
60 by `msglen` encrypted bytes of the RPC2 packet that is sent or
61 received. On the network, this header is NOT sent in UDP packets; but
62 it IS sent in TCP-tunneled packets
63*/
64typedef struct codatunnel_packet {
65 char magic[8];
66 uint32_t is_retry; /* 1 if this is a resend, 0 otherwise */
68 is_init0; /* 1 if this packet identifies peer hostname, 0 otherwise */
69 uint32_t msglen; /* actual number of bytes in the packet */
70 uint32_t addrlen; /* verbatim from sendto() or recvfrom () */
71 struct sockaddr_storage addr; /* verbatim from sendto() or recvfrom () */
73
74#define MAXRECEIVE (4500 + sizeof(ctp_t))
75/* WARNING: gross hack! Above field is space for received packet
76 that may be assembled piecemeal from multiple TCP reads; the
77 figure of 4500 is RPC2_MAXPACKETSIZE; there are many cleverer
78 ways of allocating this space, but the simplicity of this
79 approach is fine for initial implementation; get it working
80 first, and then fix later
81*/
82
83/* Transitions always: FREE --> ALLOCATED --> (optionally)TCPATTEMPTING -->
84 * TLSHANDSHAKE --> TCPACTIVE --> TCPCLOSING --> FREE */
86{
87 FREE = 0, /* this entry is not allocated */
88 ALLOCATED = 1, /* entry allocated, but TCP is not active; UDP works */
89 TCPATTEMPTING = 2, /* entry allocated, tcp connect is being attempted;
90 UDP works */
91 TLSHANDSHAKE = 3, /* tcp connection is good; TLS handshake in progress */
92 TCPACTIVE = 4, /* entry allocated, its tcphandle is good, and TLS
93 handshake successful */
94 TLSERROR = 5, /* an error occurred in TLS worker threads */
95 TCPCLOSING = 6, /* now closing, and waiting to become FREE */
96};
97
98const char *tcpstatename(enum deststate);
99
100typedef struct {
101 uv_buf_t b; /* b.len is max size of buffer, not useful bytes */
102 int numbytes; /* number of useful bytes pointed to by b->base */
103} enq_data_t;
104
105typedef struct remotedest {
108 const char *fqdn; /* passed by INIT0 packet on client, NULL on server */
109
110 enum deststate state; /* All destinations are assumed to be capable of
111 becoming TCPACTIVE; Setting TCPACTIVE should be a
112 commit point: all fields below should have been
113 set before that happens, to avoid race conditions */
114 char certvalidation_failed; /* when certificate validation fails we
115 suppress UDP, but will retry TLS connections
116 for INIT0 packets*/
117
118 uv_tcp_t *tcphandle; /* only valid if state is TCPACTIVE or TLSHANDSHAKE */
119
120 gnutls_session_t my_tls_session;
121
122 char *decrypted_record; /* pointer to malloced array of size MAXRECEIVE;
123 filled by gnutls_record_recv() by reassembly from
124 calls to eat_uvbytes(); must be preserved across
125 successive calls to gnutls_record_recv() for
126 reassembly to work properly */
127
128 /* Space to buffer packets from recv_tcp_cb() en route to
129 gnutls_record_recv(). enq_uvbuf() appends packets to this list.
130 eat_uvbytes() peels off bytes in these packets. We use a simple
131 array, because we don't expect this queue to get very long. Most
132 common case will be an exact match: one input packet waiting,
133 that is completely consumed in one call. If the future proves
134 otherwise, change to a linked list structure instead of array. */
135
136#define UVBUFLIMIT 10 /* drop packets beyond this limit */
137 enq_data_t enqarray[UVBUFLIMIT]; /* array of structures */
138 int uvcount; /* how many elements in use in above array */
139 uv_mutex_t uvcount_mutex; /* protects uvcount */
140 uv_cond_t uvcount_nonzero; /* signaled when uvcount goes above zero */
141 int uvoffset; /* array index of next unused byte in ((enqarray[0].b)->base[] */
142
143 /* Mutexes below ensure that only one gnutls_recv_record() and
144 one gnutls_send_record() can be in progress at a time; this is needed because
145 gnutls serializes data records on the TCP stream; currently, this appears to be
146 a 5-byte header that indicates a length, followed by that many bytes; however,
147 this may change in the future to be something more complex; use of a mutex eliminates
148 dependence on the exact serialization format; it is essential that
149 all the pieces of a serialized record appear consecutively in the TCP stream;
150 interleaving in an multi-threaded environment could be disastrous */
152 uv_mutex_t tls_send_mutex;
154
155 uv_async_t wakeup; /* wakeup for outgoing packets or dest_t teardown */
157 uv_mutex_t outbound_mutex;
159
160void outbound_worker_cb(uv_async_t *async);
161
162/* Stuff for destination management */
164dest_t *getdest(const struct sockaddr_storage *, socklen_t);
166 const char *peername);
167void free_dest(dest_t *d);
168
169/* Procedures to add and remove buffered data from a dest.
170 These operate in producer-consumer manner.
171 recv_tcp_cb() calls enq_uvbuf() as producer.
172 gnutls_record_recv() calls eat_uvbytes() as consumer.
173 During TLS handshake, eat_uvbytes() is also called as consumer.
174*/
175void enq_element(dest_t *, const uv_buf_t *, int);
176ssize_t eat_uvbytes(gnutls_transport_ptr_t, void *, size_t);
177int poll_uvbytes(gnutls_transport_ptr_t gtp, unsigned int ms);
178
180
181/* Helper/debugging functions */
182void hexdump(char *desc, void *addr, int len);
184
185#endif /* _CODATUNNEL_PRIVATE_H_ */
unsigned int uint32_t
Definition: coda.h:105
void printsockaddr(const struct sockaddr_storage *, socklen_t)
dest_t * getdest(const struct sockaddr_storage *, socklen_t)
Definition: remotedest.c:115
const char * tcpstatename(enum deststate)
Definition: remotedest.c:349
void codatunneld(int codatunnel_sockfd, const char *tcp_bindaddr, const char *udp_bindaddr, const char *bind_service, int onlytcp, const char *sslcertdir) __attribute__((noreturn))
Definition: codatunneld.c:1260
void drain_outbound_queues(dest_t *d)
Definition: codatunneld.c:199
void outbound_worker_cb(uv_async_t *async)
Definition: codatunneld.c:550
void enq_element(dest_t *, const uv_buf_t *, int)
Definition: remotedest.c:213
struct remotedest dest_t
void hexdump(char *desc, void *addr, int len)
Definition: codatunneld.c:1413
void free_dest(dest_t *d)
Definition: remotedest.c:178
void initdestarray()
int mapthread(uv_thread_t)
Definition: remotedest.c:327
dest_t * createdest(const struct sockaddr_storage *, socklen_t, const char *peername)
Definition: remotedest.c:132
ssize_t eat_uvbytes(gnutls_transport_ptr_t, void *, size_t)
Definition: remotedest.c:268
deststate
Definition: codatunnel.private.h:86
@ TLSHANDSHAKE
Definition: codatunnel.private.h:91
@ TCPATTEMPTING
Definition: codatunnel.private.h:89
@ TLSERROR
Definition: codatunnel.private.h:94
@ TCPACTIVE
Definition: codatunnel.private.h:92
@ ALLOCATED
Definition: codatunnel.private.h:88
@ FREE
Definition: codatunnel.private.h:87
@ TCPCLOSING
Definition: codatunnel.private.h:95
struct codatunnel_packet ctp_t
int poll_uvbytes(gnutls_transport_ptr_t gtp, unsigned int ms)
Definition: remotedest.c:245
#define UVBUFLIMIT
Definition: codatunnel.private.h:136
int socklen_t
Definition: mariner.cc:73
desc
Definition: gensrvstats.py:254
Definition: codatunnel.private.h:64
struct sockaddr_storage addr
Definition: codatunnel.private.h:71
char magic[8]
Definition: codatunnel.private.h:65
uint32_t is_init0
Definition: codatunnel.private.h:68
uint32_t msglen
Definition: codatunnel.private.h:69
uint32_t addrlen
Definition: codatunnel.private.h:70
uint32_t is_retry
Definition: codatunnel.private.h:66
Definition: codatunnel.private.h:100
uv_buf_t b
Definition: codatunnel.private.h:101
int numbytes
Definition: codatunnel.private.h:102
Definition: codatunneld.c:95
Definition: codatunnel.private.h:105
void * tls_send_queue
Definition: codatunnel.private.h:153
uv_mutex_t uvcount_mutex
Definition: codatunnel.private.h:139
uv_cond_t uvcount_nonzero
Definition: codatunnel.private.h:140
uv_tcp_t * tcphandle
Definition: codatunnel.private.h:118
struct minicb_tcp_req * outbound_queue
Definition: codatunnel.private.h:156
char certvalidation_failed
Definition: codatunnel.private.h:114
char * decrypted_record
Definition: codatunnel.private.h:122
uv_async_t wakeup
Definition: codatunnel.private.h:155
socklen_t destlen
Definition: codatunnel.private.h:107
int uvcount
Definition: codatunnel.private.h:138
uv_mutex_t outbound_mutex
Definition: codatunnel.private.h:157
enum deststate state
Definition: codatunnel.private.h:110
struct sockaddr_storage destaddr
Definition: codatunnel.private.h:106
const char * fqdn
Definition: codatunnel.private.h:108
uv_mutex_t tls_receive_record_mutex
Definition: codatunnel.private.h:151
enq_data_t enqarray[UVBUFLIMIT]
Definition: codatunnel.private.h:137
gnutls_session_t my_tls_session
Definition: codatunnel.private.h:120
int uvoffset
Definition: codatunnel.private.h:141
uv_mutex_t tls_send_mutex
Definition: codatunnel.private.h:152
Definition: rpc2.private.h:63
char d
Definition: tdb.c:54
const char * sslcertdir
Definition: venus.cc:93