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
|
/*********************************************************************************************************
* Software License Agreement (BSD License) *
* Author: Sebastien Decugis <sdecugis@freediameter.net> *
* *
* Copyright (c) 2020, WIDE Project and NICT *
* All rights reserved. *
* *
* Redistribution and use of this software in source and binary forms, with or without modification, are *
* permitted provided that the following conditions are met: *
* *
* * Redistributions of source code must retain the above *
* copyright notice, this list of conditions and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above *
* copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other *
* materials provided with the distribution. *
* *
* * Neither the name of the WIDE Project or NICT nor the *
* names of its contributors may be used to endorse or *
* promote products derived from this software without *
* specific prior written permission of WIDE Project and *
* NICT. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*********************************************************************************************************/
#include "fdcore-internal.h"
/* TODO: change the behavior to handle properly forced ordering at beginning & end of OPEN state */
/* This file contains code used by a peer state machine to initiate a connection to remote peer */
struct next_conn {
struct fd_list chain;
int proto; /* Protocol of the next attempt */
union {
sSS ss; /* The address, only for TCP */
sSA4 sin;
sSA6 sin6;
};
uint16_t port; /* The port, for SCTP (included in ss for TCP) */
int dotls; /* Handshake TLS after connection ? */
};
static __inline__ void failed_connection_attempt(struct fd_peer * peer)
{
/* Simply remove the first item in the list if not empty */
if (! FD_IS_LIST_EMPTY(&peer->p_connparams) ) {
struct fd_list * li = peer->p_connparams.next;
fd_list_unlink(li);
free(li);
}
}
static void empty_connection_list(struct fd_peer * peer)
{
/* Remove all items */
while (!FD_IS_LIST_EMPTY(&peer->p_connparams)) {
failed_connection_attempt(peer);
}
}
static int prepare_connection_list(struct fd_peer * peer)
{
struct fd_list * li, *last_prio;
struct next_conn * new;
uint16_t port_no; /* network order */
int dotls_immediate;
int count = 0;
TRACE_ENTRY("%p", peer);
/* Resolve peer address(es) if needed */
if (FD_IS_LIST_EMPTY(&peer->p_hdr.info.pi_endpoints)) {
struct addrinfo hints, *ai, *aip;
int ret;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG;
ret = getaddrinfo(peer->p_hdr.info.pi_diamid, NULL, &hints, &ai);
if (ret) {
TRACE_DEBUG(INFO, "Unable to resolve address for peer '%s' (%s), aborting", peer->p_hdr.info.pi_diamid, gai_strerror(ret));
if (ret != EAI_AGAIN)
fd_psm_terminate( peer, NULL );
return 0;
}
for (aip = ai; aip != NULL; aip = aip->ai_next) {
CHECK_FCT( fd_ep_add_merge( &peer->p_hdr.info.pi_endpoints, aip->ai_addr, aip->ai_addrlen, EP_FL_DISC ) );
}
freeaddrinfo(ai);
}
/* Remove addresses from unwanted family */
if (peer->p_hdr.info.config.pic_flags.pro3) {
CHECK_FCT( fd_ep_filter_family(
&peer->p_hdr.info.pi_endpoints,
(peer->p_hdr.info.config.pic_flags.pro3 == PI_P3_IP) ?
AF_INET
: AF_INET6));
}
if (fd_g_config->cnf_flags.no_ip4) {
CHECK_FCT( fd_ep_filter_family(
&peer->p_hdr.info.pi_endpoints,
AF_INET6));
}
if (fd_g_config->cnf_flags.no_ip6) {
CHECK_FCT( fd_ep_filter_family(
&peer->p_hdr.info.pi_endpoints,
AF_INET));
}
/* We don't use the alternate addresses that were sent by the remote peer */
CHECK_FCT( fd_ep_clearflags(&peer->p_hdr.info.pi_endpoints, EP_FL_ADV) );
/* Now check we have at least one address to attempt */
if (FD_IS_LIST_EMPTY(&peer->p_hdr.info.pi_endpoints)) {
TRACE_DEBUG(INFO, "No address %savailable to connect to peer '%s', aborting",
peer->p_hdr.info.config.pic_flags.pro3 ? "in the configured family " : "", peer->p_hdr.info.pi_diamid);
fd_psm_terminate( peer, NULL );
return 0;
}
/* Check if we are able to communicate with this peer */
if (fd_g_config->cnf_sec_data.tls_disabled && ( peer->p_hdr.info.config.pic_flags.sec != PI_SEC_NONE)) {
LOG_E("Peer '%s' not configured for No_TLS and TLS is locally disabled; giving up connection attempts",
peer->p_hdr.info.pi_diamid);
fd_psm_terminate( peer, NULL );
return 0;
}
/* Cleanup any previous list */
empty_connection_list(peer);
/* Prepare the parameters */
if ((peer->p_hdr.info.config.pic_flags.sec != PI_SEC_DEFAULT) || (fd_g_config->cnf_flags.tls_alg)) {
dotls_immediate = 0;
port_no = htons(peer->p_hdr.info.config.pic_port ?: DIAMETER_PORT);
} else {
dotls_immediate = 1;
port_no = htons(peer->p_hdr.info.config.pic_port ?: DIAMETER_SECURE_PORT);
}
last_prio = &peer->p_connparams;
/* Create TCP parameters unless specified otherwise */
if ((!fd_g_config->cnf_flags.no_tcp) && (peer->p_hdr.info.config.pic_flags.pro4 != PI_P4_SCTP)) {
for (li = peer->p_hdr.info.pi_endpoints.next; li != &peer->p_hdr.info.pi_endpoints; li = li->next) {
struct fd_endpoint * ep = (struct fd_endpoint *)li;
CHECK_MALLOC( new = malloc(sizeof(struct next_conn)) );
memset(new, 0, sizeof(struct next_conn));
fd_list_init(&new->chain, new);
new->proto = IPPROTO_TCP;
memcpy( &new->ss, &ep->ss, sizeof(sSS) );
switch (new->ss.ss_family) {
case AF_INET:
new->sin.sin_port = port_no;
break;
case AF_INET6:
new->sin6.sin6_port = port_no;
break;
default:
free(new);
continue; /* Move to the next endpoint */
}
new->dotls = dotls_immediate;
/* Add the new entry to the appropriate position (conf and disc go first) */
if (ep->flags & (EP_FL_CONF | EP_FL_DISC)) {
fd_list_insert_after(last_prio, &new->chain);
last_prio = &new->chain;
} else {
fd_list_insert_before(&peer->p_connparams, &new->chain);
}
count++;
}
}
/* Now, add the SCTP entry, if not disabled */
#ifndef DISABLE_SCTP
if ((!fd_g_config->cnf_flags.no_sctp) && (peer->p_hdr.info.config.pic_flags.pro4 != PI_P4_TCP)) {
struct next_conn * new;
CHECK_MALLOC( new = malloc(sizeof(struct next_conn)) );
memset(new, 0, sizeof(struct next_conn));
fd_list_init(&new->chain, new);
new->proto = IPPROTO_SCTP;
new->port = ntohs(port_no); /* back to host byte order... */
new->dotls = dotls_immediate;
/* Add the new entry to the appropriate position (depending on preferences) */
if ((fd_g_config->cnf_flags.pr_tcp) || (peer->p_hdr.info.config.pic_flags.alg == PI_ALGPREF_TCP)) {
fd_list_insert_after(last_prio, &new->chain);
} else {
fd_list_insert_after(&peer->p_connparams, &new->chain); /* very first position */
}
count++;
}
#endif /* DISABLE_SCTP */
LOG_D("Prepared %d sets of connection parameters to peer %s", count, peer->p_hdr.info.pi_diamid);
return 0;
}
/* The thread that attempts the connection */
static void * connect_thr(void * arg)
{
struct fd_peer * peer = arg;
struct cnxctx * cnx = NULL;
struct next_conn * nc = NULL;
int rebuilt = 0;
int fatal_error=0;
TRACE_ENTRY("%p", arg);
CHECK_PARAMS_DO( CHECK_PEER(peer), return NULL );
/* Set the thread name */
{
char buf[48];
snprintf(buf, sizeof(buf), "ConnTo:%s", peer->p_hdr.info.pi_diamid);
fd_log_threadname ( buf );
}
do {
/* Rebuild the list if needed, if it is empty -- but at most once */
if (FD_IS_LIST_EMPTY(&peer->p_connparams)) {
if (! rebuilt) {
CHECK_FCT_DO( fatal_error = prepare_connection_list(peer), goto out );
rebuilt ++;
}
if (FD_IS_LIST_EMPTY(&peer->p_connparams)) {
/* We encountered an error or we have looped over all the addresses of the peer. */
fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, peer, "All connection attempts failed, will retry later", NULL);
CHECK_FCT_DO( fatal_error = fd_event_send(peer->p_events, FDEVP_CNX_FAILED, 0, NULL), goto out );
return NULL;
}
}
/* Attempt connection to the first entry */
nc = (struct next_conn *)(peer->p_connparams.next);
switch (nc->proto) {
case IPPROTO_TCP:
/* TODO: use no_bind and first of cnf_endpoints of nc->ss.sa_family ? */
cnx = fd_cnx_cli_connect_tcp((sSA *)&nc->ss, sSAlen(&nc->ss));
break;
#ifndef DISABLE_SCTP
case IPPROTO_SCTP:
cnx = fd_cnx_cli_connect_sctp((peer->p_hdr.info.config.pic_flags.pro3 == PI_P3_IP) ? 1 : fd_g_config->cnf_flags.no_ip6,
nc->port, &peer->p_hdr.info.pi_endpoints,
fd_g_config->cnf_flags.no_bind ? NULL : &fd_g_config->cnf_endpoints);
break;
#endif /* DISABLE_SCTP */
}
if (cnx)
break;
/* Pop these parameters and continue */
failed_connection_attempt(peer);
pthread_testcancel();
} while (!cnx); /* and until cancellation or all addresses attempted without success */
/* Now, we have an established connection in cnx */
pthread_cleanup_push((void *)fd_cnx_destroy, cnx);
/* Set the hostname in the connection, so that handshake verifies the remote identity */
fd_cnx_sethostname(cnx,peer->p_hdr.info.pi_diamid);
/* Handshake if needed (secure port) */
if (nc->dotls) {
CHECK_FCT_DO( fd_cnx_handshake(cnx, GNUTLS_CLIENT,
ALGO_HANDSHAKE_3436,
peer->p_hdr.info.config.pic_priority, NULL),
{
/* Handshake failed ... */
fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, peer, "TLS Handshake failed", NULL);
fd_cnx_destroy(cnx);
empty_connection_list(peer);
fd_ep_filter(&peer->p_hdr.info.pi_endpoints, EP_FL_CONF);
goto out_pop;
} );
LOG_A("%s: TLS handshake successful.", peer->p_hdr.info.pi_diamid);
} else {
/* Prepare to receive the next message */
CHECK_FCT_DO( fatal_error = fd_cnx_start_clear(cnx, 0), goto out_pop );
}
/* Upon success, generate FDEVP_CNX_ESTABLISHED */
CHECK_FCT_DO( fatal_error = fd_event_send(peer->p_events, FDEVP_CNX_ESTABLISHED, 0, cnx), );
out_pop:
;
pthread_cleanup_pop(0);
out:
if (fatal_error) {
/* Cleanup the connection */
if (cnx)
fd_cnx_destroy(cnx);
/* Generate a termination event */
CHECK_FCT_DO(fd_core_shutdown(), );
}
return NULL;
}
/* Initiate a connection attempt to a remote peer */
int fd_p_cnx_init(struct fd_peer * peer)
{
TRACE_ENTRY("%p", peer);
/* Start the connect thread */
CHECK_FCT( pthread_create(&peer->p_ini_thr, NULL, connect_thr, peer) );
return 0;
}
/* Cancel a connection attempt */
void fd_p_cnx_abort(struct fd_peer * peer, int cleanup_all)
{
TRACE_ENTRY("%p %d", peer, cleanup_all);
CHECK_PARAMS_DO( CHECK_PEER(peer), return );
if (peer->p_ini_thr != (pthread_t)NULL) {
CHECK_FCT_DO( fd_thr_term(&peer->p_ini_thr), /* continue */);
failed_connection_attempt(peer);
}
if (cleanup_all) {
empty_connection_list(peer);
}
}
|