blob: 7fac852039731e08374f5931f160207fa4fdfab7 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* This file contains the definitions for internal use in the freeDiameter core library */
37
38#ifndef _FDCORE_INTERNAL_H
39#define _FDCORE_INTERNAL_H
40
41#include <freeDiameter/freeDiameter-host.h>
42#include <freeDiameter/libfdcore.h>
43
44#ifdef DISABLE_SCTP
45#undef IPPROTO_SCTP
46#define IPPROTO_SCTP (2 = 4) /* some compilation error to spot the references */
47#endif /* DISABLE_SCTP */
48
49#ifndef HAVE_AI_ADDRCONFIG
50#define AI_ADDRCONFIG 0 /* ignore this flag at the moment */
51#endif /* HAVE_AI_ADDRCONFIG */
52
53/* Timeout for establishing a connection */
54#ifndef CNX_TIMEOUT
55#define CNX_TIMEOUT 10 /* in seconds */
56#endif /* CNX_TIMEOUT */
57
58/* Timeout for receiving a CER after incoming connection is established */
59#ifndef INCNX_TIMEOUT
60#define INCNX_TIMEOUT 20 /* in seconds */
61#endif /* INCNX_TIMEOUT */
62
63/* Timeout for receiving a CEA after CER is sent */
64#ifndef CEA_TIMEOUT
65#define CEA_TIMEOUT 10 /* in seconds */
66#endif /* CEA_TIMEOUT */
67
68/* The timeout value to wait for answer to a DPR */
69#ifndef DPR_TIMEOUT
70#define DPR_TIMEOUT 15 /* in seconds */
71#endif /* DPR_TIMEOUT */
72
73/* Delay where the connection is maintained opened to allow exchanging remaining pending answers after DPR/DPA */
74#ifndef GRACE_TIMEOUT
75#define GRACE_TIMEOUT 1 /* in seconds */
76#endif /* GRACE_TIMEOUT */
77
78/* The Vendor-Id to advertise in CER/CEA */
79#ifndef MY_VENDOR_ID
80#define MY_VENDOR_ID 0 /* Reserved value to tell it must be ignored */
81#endif /* MY_VENDOR_ID */
82
83
84
85/* Configuration */
86int fd_conf_init();
87int fd_conf_deinit();
88int fd_conf_parse();
89int fddparse(struct fd_config * conf); /* yacc generated */
90int fd_conf_stream_to_gnutls_datum(FILE * pemfile, gnutls_datum_t *out);
91
92
93/* Extensions */
94int fd_ext_add( char * filename, char * conffile );
95int fd_ext_load();
96int fd_ext_term(void);
97
98/* Messages */
99int fd_msg_init(void);
100extern struct dict_object * fd_dict_avp_OSI; /* Origin-State-Id */
101extern struct dict_object * fd_dict_cmd_CER; /* Capabilities-Exchange-Request */
102extern struct dict_object * fd_dict_cmd_DWR; /* Device-Watchdog-Request */
103extern struct dict_object * fd_dict_avp_DC; /* Disconnect-Cause */
104extern struct dict_object * fd_dict_cmd_DPR; /* Disconnect-Peer-Request */
105
106/* Global message queues */
107extern struct fifo * fd_g_incoming; /* all messages received from other peers, except local messages (CER, ...) */
108extern struct fifo * fd_g_outgoing; /* messages to be sent to other peers on the network following routing procedure */
109extern struct fifo * fd_g_local; /* messages to be handled to local extensions */
110/* Message queues */
111int fd_queues_init(void);
112int fd_queues_fini(struct fifo ** queue);
113
114/* Trigged events */
115int fd_event_trig_call_cb(int trigger_val);
116int fd_event_trig_fini(void);
117
118/* Create all the dictionary objects defined in the Diameter base RFC. */
119int fd_dict_base_protocol(struct dictionary * dict);
120
121/* Routing */
122int fd_rtdisp_init(void);
123int fd_rtdisp_cleanstop(void);
124int fd_rtdisp_fini(void);
125int fd_rtdisp_cleanup(void);
126
127/* Sentinel for the sent requests list */
128struct sr_list {
129 struct fd_list srs; /* requests ordered by hop-by-hop id */
130 struct fd_list exp; /* requests that have a timeout set, ordered by timeout */
131 long cnt; /* number of requests in the srs list */
132 long cnt_lost; /* number of requests that have not been answered in time.
133 It is decremented when an unexpected answer is received, so this may not be accurate. */
134 pthread_mutex_t mtx; /* mutex to protect these lists */
135 pthread_cond_t cnd; /* cond var used by the thread that handles timeouts */
136 pthread_t thr; /* the thread that handles timeouts (expirecb called in separate forked threads) */
137};
138
139/* Peers */
140struct fd_peer { /* The "real" definition of the peer structure */
141
142 /* The public data */
143 struct peer_hdr p_hdr;
144
145 /* Eye catcher, EYEC_PEER */
146 int p_eyec;
147 #define EYEC_PEER 0x373C9336
148
149 /* Origin of this peer object, for debug */
150 char *p_dbgorig;
151
152 /* State of the peer, and its lock */
153 enum peer_state p_state;
154 pthread_mutex_t p_state_mtx;
155
156 /* Chaining in peers sublists */
157 struct fd_list p_actives; /* list of peers in the STATE_OPEN state -- used by routing */
158 struct fd_list p_expiry; /* list of expiring peers, ordered by their timeout value */
159 struct timespec p_exp_timer; /* Timestamp where the peer will expire; updated each time activity is seen on the peer (except DW) */
160
161 /* Some flags influencing the peer state machine */
162 struct {
163 unsigned pf_responder : 1; /* The peer has been created to handle incoming connection */
164 unsigned pf_delete : 1; /* Destroy the peer when the connection is terminated */
165 unsigned pf_localterm : 1; /* If the latest DPR/DPA was initiated from this side */
166
167 unsigned pf_dw_pending : 1; /* A DWR message was sent and not answered yet */
168
169 unsigned pf_cnx_pb : 1; /* The peer was disconnected because of watchdogs; must exchange 3 watchdogs before putting back to normal */
170 unsigned pf_reopen_cnt : 2; /* remaining DW to be exchanged after re-established connection */
171
172 } p_flags;
173
174 /* The events queue, peer state machine thread, timer for states timeouts */
175 struct fifo *p_events; /* The mutex of this FIFO list protects also the state and timer information */
176 pthread_t p_psm;
177 struct timespec p_psm_timer;
178
179 /* Outgoing message queue, and thread managing sending the messages */
180 struct fifo *p_tosend;
181 pthread_t p_outthr;
182
183 /* The next hop-by-hop id value for the link, only read & modified by p_outthr */
184 uint32_t p_hbh;
185
186 /* Sent requests (for fallback), list of struct sentreq ordered by hbh */
187 struct sr_list p_sr;
188 struct fifo *p_tofailover;
189
190 /* Pending received requests not yet answered (count only) */
191 long p_reqin_count; /* We use p_state_mtx to protect this value */
192
193 /* Data for transitional states before the peer is in OPEN state */
194 struct {
195 struct cnxctx * p_receiver; /* Only used in case of election */
196 struct msg * p_cer; /* Only used in case of election */
197
198 pthread_t p_ini_thr; /* Initiator thread for establishing a connection */
199 struct fd_list p_connparams; /* The list of connection attempts, see p_cnx.c */
200 };
201
202 /* connection context: socket and related information */
203 struct cnxctx *p_cnxctx;
204
205 /* Callback for peer validation after the handshake */
206 int (*p_cb2)(struct peer_info *);
207
208 /* Callback on initial connection success / failure after the peer was added */
209 void (*p_cb)(struct peer_info *, void *);
210 void *p_cb_data;
211
212};
213#define CHECK_PEER( _p ) \
214 (((_p) != NULL) && (((struct fd_peer *)(_p))->p_eyec == EYEC_PEER))
215
216#define fd_peer_getstate(peer) fd_peer_get_state((struct peer_hdr *)(peer))
217
218
219/* Events codespace for struct fd_peer->p_events */
220enum {
221 /* request to terminate this peer : disconnect, requeue all messages */
222 FDEVP_TERMINATE = 1500
223
224 /* A connection object has received a message. (data contains the buffer + padding + struct fd_msg_pmdl) */
225 ,FDEVP_CNX_MSG_RECV
226
227 /* A connection object has encountered an error (disconnected). */
228 ,FDEVP_CNX_ERROR
229
230 /* Endpoints of a connection have been changed (multihomed SCTP). */
231 ,FDEVP_CNX_EP_CHANGE
232
233 /* The connection is being shutdown (SCTP notification). */
234 ,FDEVP_CNX_SHUTDOWN
235
236 /* A new connection (with a CER) has been received */
237 ,FDEVP_CNX_INCOMING
238
239 /* A new connection has been established to the remote peer (event data is the cnxctx object) */
240 ,FDEVP_CNX_ESTABLISHED
241
242 /* A connection attempt (initiator side) has failed */
243 ,FDEVP_CNX_FAILED
244
245 /* The PSM state is expired */
246 ,FDEVP_PSM_TIMEOUT
247
248};
249#define CHECK_PEVENT( _e ) \
250 (((int)(_e) >= FDEVP_TERMINATE) && ((int)(_e) <= FDEVP_PSM_TIMEOUT))
251/* The following macro is actually called in p_psm.c -- another solution would be to declare it static inline */
252#define DECLARE_PEV_STR() \
253const char * fd_pev_str(int event) \
254{ \
255 switch (event) { \
256 case_str(FDEVP_TERMINATE); \
257 case_str(FDEVP_CNX_MSG_RECV); \
258 case_str(FDEVP_CNX_ERROR); \
259 case_str(FDEVP_CNX_EP_CHANGE); \
260 case_str(FDEVP_CNX_INCOMING); \
261 case_str(FDEVP_CNX_ESTABLISHED); \
262 case_str(FDEVP_CNX_FAILED); \
263 case_str(FDEVP_PSM_TIMEOUT); \
264 } \
265 TRACE_DEBUG(FULL, "Unknown event : %d", event); \
266 return "Unknown event"; \
267}
268const char * fd_pev_str(int event);
269
270/* The data structure for FDEVP_CNX_INCOMING event */
271struct cnx_incoming {
272 struct msg * cer; /* the CER message received on this connection */
273 struct cnxctx * cnx; /* The connection context */
274 int validate; /* The peer is new, it must be validated (by an extension) or error CEA to be sent */
275};
276
277/* Functions */
278int fd_peer_fini();
279int fd_peer_alloc(struct fd_peer ** ptr);
280int fd_peer_free(struct fd_peer ** ptr);
281int fd_peer_handle_newCER( struct msg ** cer, struct cnxctx ** cnx );
282/* fd_peer_add declared in freeDiameter.h */
283int fd_peer_validate( struct fd_peer * peer );
284void fd_peer_failover_msg(struct fd_peer * peer);
285
286/* Peer expiry */
287int fd_p_expi_init(void);
288int fd_p_expi_fini(void);
289int fd_p_expi_update(struct fd_peer * peer );
290
291/* Peer state machine */
292int fd_psm_start();
293int fd_psm_begin(struct fd_peer * peer );
294int fd_psm_terminate(struct fd_peer * peer, char * reason );
295void fd_psm_abord(struct fd_peer * peer );
296void fd_psm_next_timeout(struct fd_peer * peer, int add_random, int delay);
297int fd_psm_change_state(struct fd_peer * peer, int new_state);
298void fd_psm_cleanup(struct fd_peer * peer, int terminate);
299
300/* Peer out */
301int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer, int update_reqin_cnt);
302int fd_out_start(struct fd_peer * peer);
303int fd_out_stop(struct fd_peer * peer);
304
305/* Initiating connections */
306int fd_p_cnx_init(struct fd_peer * peer);
307void fd_p_cnx_abort(struct fd_peer * peer, int cleanup_all);
308
309/* Peer sent requests cache */
310int fd_p_sr_store(struct sr_list * srlist, struct msg **req, uint32_t *hbhloc, uint32_t hbh_restore);
311int fd_p_sr_fetch(struct sr_list * srlist, uint32_t hbh, struct msg **req);
312int fd_p_sr_start(struct sr_list * srlist);
313int fd_p_sr_stop(struct sr_list * srlist);
314void fd_p_sr_failover(struct sr_list * srlist);
315
316/* Local Link messages (CER/CEA, DWR/DWA, DPR/DPA) */
317int fd_p_ce_msgrcv(struct msg ** msg, int req, struct fd_peer * peer);
318int fd_p_ce_handle_newCER(struct msg ** msg, struct fd_peer * peer, struct cnxctx ** cnx, int valid);
319int fd_p_ce_handle_newcnx(struct fd_peer * peer, struct cnxctx * initiator);
320int fd_p_ce_process_receiver(struct fd_peer * peer);
321void fd_p_ce_clear_cnx(struct fd_peer * peer, struct cnxctx ** cnx_kept);
322int fd_p_dw_handle(struct msg ** msg, int req, struct fd_peer * peer);
323int fd_p_dw_timeout(struct fd_peer * peer);
324int fd_p_dw_reopen(struct fd_peer * peer);
325int fd_p_dp_handle(struct msg ** msg, int req, struct fd_peer * peer);
326int fd_p_dp_initiate(struct fd_peer * peer, char * reason);
327int fd_p_dp_newdelay(struct fd_peer * peer);
328
329/* Active peers -- routing process should only ever take the read lock, the write lock is managed by PSMs */
330extern struct fd_list fd_g_activ_peers;
331extern pthread_rwlock_t fd_g_activ_peers_rw; /* protect the list */
332
333
334/* Server sockets */
335int fd_servers_start();
336int fd_servers_stop();
337
338/* Connection contexts -- there are also definitions in cnxctx.h for the relevant files */
339struct cnxctx * fd_cnx_serv_tcp(uint16_t port, int family, struct fd_endpoint * ep);
340struct cnxctx * fd_cnx_serv_sctp(uint16_t port, struct fd_list * ep_list);
341int fd_cnx_serv_listen(struct cnxctx * conn);
342struct cnxctx * fd_cnx_serv_accept(struct cnxctx * serv);
343struct cnxctx * fd_cnx_cli_connect_tcp(sSA * sa, socklen_t addrlen);
344struct cnxctx * fd_cnx_cli_connect_sctp(int no_ip6, uint16_t port, struct fd_list * list);
345int fd_cnx_start_clear(struct cnxctx * conn, int loop);
346void fd_cnx_sethostname(struct cnxctx * conn, DiamId_t hn);
347int fd_cnx_proto_info(struct cnxctx * conn, char * buf, size_t len);
348#define ALGO_HANDSHAKE_DEFAULT 0 /* TLS for TCP, DTLS for SCTP */
349#define ALGO_HANDSHAKE_3436 1 /* For TLS for SCTP also */
350int fd_cnx_handshake(struct cnxctx * conn, int mode, int algo, char * priority, void * alt_creds);
351char * fd_cnx_getid(struct cnxctx * conn);
352int fd_cnx_getproto(struct cnxctx * conn);
353int fd_cnx_getTLS(struct cnxctx * conn);
354int fd_cnx_is_unordered_delivery_supported(struct cnxctx * conn);
355int fd_cnx_unordered_delivery(struct cnxctx * conn, int is_allowed);
356int fd_cnx_getcred(struct cnxctx * conn, const gnutls_datum_t **cert_list, unsigned int *cert_list_size);
357int fd_cnx_get_local_eps(struct fd_list * list);
358int fd_cnx_getremoteeps(struct cnxctx * conn, struct fd_list * eps);
359char * fd_cnx_getremoteid(struct cnxctx * conn);
360int fd_cnx_receive(struct cnxctx * conn, struct timespec * timeout, unsigned char **buf, size_t * len);
361int fd_cnx_recv_setaltfifo(struct cnxctx * conn, struct fifo * alt_fifo); /* send FDEVP_CNX_MSG_RECV event to the fifo list */
362int fd_cnx_send(struct cnxctx * conn, unsigned char * buf, size_t len);
363void fd_cnx_destroy(struct cnxctx * conn);
364#ifdef GNUTLS_VERSION_300
365int fd_tls_verify_credentials_2(gnutls_session_t session);
366#endif /* GNUTLS_VERSION_300 */
367
368/* Internal calls of the hook mechanism */
369void fd_hook_call(enum fd_hook_type type, struct msg * msg, struct fd_peer * peer, void * other, struct fd_msg_pmdl * pmdl);
370void fd_hook_associate(struct msg * msg, struct fd_msg_pmdl * pmdl);
371int fd_hooks_init(void);
372size_t fd_msg_pmdl_sizewithoverhead(size_t datalen);
373struct fd_msg_pmdl * fd_msg_pmdl_get_inbuf(uint8_t * buf, size_t datalen);
374
375#endif /* _FDCORE_INTERNAL_H */