Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2016, 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 | #ifndef _LIBFDCORE_H |
| 37 | #define _LIBFDCORE_H |
| 38 | |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | #include <freeDiameter/libfdproto.h> |
| 45 | #include <gnutls/gnutls.h> |
| 46 | #include <gnutls/x509.h> |
| 47 | |
| 48 | /* GNUTLS version */ |
| 49 | #ifndef GNUTLS_VERSION |
| 50 | #define GNUTLS_VERSION LIBGNUTLS_VERSION |
| 51 | #endif /* GNUTLS_VERSION */ |
| 52 | |
| 53 | /* GNUTLS calls debug level */ |
| 54 | #ifndef GNUTLS_DBG_LEVEL |
| 55 | #define GNUTLS_DBG_LEVEL ANNOYING |
| 56 | #endif /* GNUTLS_DBG_LEVEL */ |
| 57 | |
| 58 | |
| 59 | /* Check the return value of a GNUTLS function, log and propagate */ |
| 60 | #define CHECK_GNUTLS_GEN( faillevel, __call__, __fallback__ ) { \ |
| 61 | CHECK_PRELUDE(__call__); \ |
| 62 | if (__ret__ < 0) { \ |
| 63 | LOG(faillevel, "TLS ERROR: in '%s' :\t%s", #__call__ , gnutls_strerror(__ret__)); \ |
| 64 | __fallback__; \ |
| 65 | } \ |
| 66 | } |
| 67 | |
| 68 | /* we use this macro to help debugging gnutls usage issues -- just change the content to display what you need */ |
| 69 | #define GNUTLS_TRACE( __call__) { \ |
| 70 | TRACE_CALL("Check: %s", #__call__ ); \ |
| 71 | (__call__); \ |
| 72 | } |
| 73 | |
| 74 | |
| 75 | #ifndef EXCLUDE_DEPRECATED |
| 76 | /* Macro for transition, replace with CHECK_GNUTLS_GEN */ |
| 77 | #define CHECK_GNUTLS_DO( __call__, __fallback__ ) \ |
| 78 | CHECK_GNUTLS_GEN( FD_LOG_ERROR, __call__, __fallback__ ) |
| 79 | |
| 80 | #endif /* EXCLUDE_DEPRECATED */ |
| 81 | |
| 82 | |
| 83 | /*============================================================*/ |
| 84 | /* INITIALIZATION */ |
| 85 | /*============================================================*/ |
| 86 | |
| 87 | |
| 88 | /* Initialize the libfdcore internals. This also initializes libfdproto */ |
| 89 | int fd_core_initialize(void); |
| 90 | |
| 91 | /* A string describing the version of the library */ |
| 92 | extern const char fd_core_version[]; |
| 93 | |
| 94 | /* Parse the freeDiameter.conf configuration file, load the extensions */ |
| 95 | int fd_core_parseconf(const char * conffile); |
| 96 | |
| 97 | /* Start the server & client threads */ |
| 98 | int fd_core_start(void); |
| 99 | |
| 100 | /* Block until the framework has completed its initialization -- useful for extensions */ |
| 101 | int fd_core_waitstartcomplete(void); |
| 102 | |
| 103 | /* Initialize shutdown of the framework */ |
| 104 | int fd_core_shutdown(void); |
| 105 | |
| 106 | /* Wait for the shutdown to be complete -- this should always be called after fd_core_shutdown */ |
| 107 | int fd_core_wait_shutdown_complete(void); |
| 108 | |
| 109 | |
| 110 | /*============================================================*/ |
| 111 | /* CONFIG */ |
| 112 | /*============================================================*/ |
| 113 | |
| 114 | /* Structure to hold the configuration of the freeDiameter daemon */ |
| 115 | #define EYEC_CONFIG 0xC011F16 |
| 116 | struct fd_config { |
| 117 | int cnf_eyec; /* Eye catcher: EYEC_CONFIG */ |
| 118 | |
| 119 | const char *cnf_file; /* Configuration file to parse, default is DEFAULT_CONF_FILE */ |
| 120 | |
| 121 | DiamId_t cnf_diamid; /* Diameter Identity of the local peer (FQDN -- ASCII) */ |
| 122 | size_t cnf_diamid_len;/* cached length of the previous string */ |
| 123 | DiamId_t cnf_diamrlm; /* Diameter realm of the local peer, default to realm part of cnf_diamid */ |
| 124 | size_t cnf_diamrlm_len;/* length of the previous string */ |
| 125 | |
| 126 | unsigned int cnf_timer_tc; /* The value in seconds of the default Tc timer */ |
| 127 | unsigned int cnf_timer_tw; /* The value in seconds of the default Tw timer */ |
| 128 | |
| 129 | uint16_t cnf_port; /* the local port for legacy Diameter (default: 3868) in host byte order */ |
| 130 | uint16_t cnf_port_tls; /* the local port for Diameter/TLS (default: 5868) in host byte order */ |
| 131 | uint16_t cnf_port_3436; /* Open an additional server port to listen to old TLS/SCTP clients (RFC3436, freeDiameter versions < 1.2.0) */ |
| 132 | uint16_t cnf_sctp_str; /* default max number of streams for SCTP associations (def: 30) */ |
| 133 | struct fd_list cnf_endpoints; /* the local endpoints to bind the server to. list of struct fd_endpoint. default is empty (bind all). After servers are started, this is the actual list of endpoints including port information. */ |
| 134 | int cnf_thr_srv; /* Number of threads per servers handling the connection state machines */ |
| 135 | struct fd_list cnf_apps; /* Applications locally supported (except relay, see flags). Use fd_disp_app_support to add one. list of struct fd_app. */ |
| 136 | uint16_t cnf_dispthr; /* Number of dispatch threads to create */ |
| 137 | struct { |
| 138 | unsigned no_fwd : 1; /* the peer does not relay messages (0xffffff app id) */ |
| 139 | unsigned no_ip4 : 1; /* disable IP */ |
| 140 | unsigned no_ip6 : 1; /* disable IPv6 */ |
| 141 | unsigned no_tcp : 1; /* disable use of TCP */ |
| 142 | unsigned no_sctp: 1; /* disable the use of SCTP */ |
| 143 | unsigned pr_tcp : 1; /* prefer TCP over SCTP */ |
| 144 | unsigned tls_alg: 1; /* TLS algorithm for initiated cnx. 0: separate port. 1: inband-security (old) */ |
| 145 | } cnf_flags; |
| 146 | |
| 147 | struct { |
| 148 | int tls_disabled; |
| 149 | |
| 150 | /* Credentials parameters (backup) */ |
| 151 | char * cert_file; |
| 152 | char * key_file; |
| 153 | |
| 154 | char * ca_file; |
| 155 | int ca_file_nr; |
| 156 | char * crl_file; |
| 157 | |
| 158 | char * prio_string; |
| 159 | unsigned int dh_bits; |
| 160 | char * dh_file; |
| 161 | |
| 162 | /* GNUTLS parameters */ |
| 163 | gnutls_priority_t prio_cache; |
| 164 | gnutls_dh_params_t dh_cache; |
| 165 | |
| 166 | /* GNUTLS server credential(s) */ |
| 167 | gnutls_certificate_credentials_t credentials; /* contains local cert + trust anchors */ |
| 168 | #ifdef GNUTLS_VERSION_300 |
| 169 | gnutls_x509_trust_list_t trustlist; /* the logic to check local certificate has changed */ |
| 170 | #endif /* GNUTLS_VERSION_300 */ |
| 171 | |
| 172 | } cnf_sec_data; |
| 173 | |
| 174 | uint32_t cnf_orstateid; /* The value to use in Origin-State-Id, default to random value */ |
| 175 | struct dictionary *cnf_dict; /* pointer to the global dictionary */ |
| 176 | struct fifo *cnf_main_ev; /* events for the daemon's main (struct fd_event items) */ |
| 177 | }; |
| 178 | extern struct fd_config *fd_g_config; /* The pointer to access the global configuration, initalized in main */ |
| 179 | |
| 180 | |
| 181 | |
| 182 | /*============================================================*/ |
| 183 | /* PEERS */ |
| 184 | /*============================================================*/ |
| 185 | |
| 186 | /* States of a peer */ |
| 187 | enum peer_state { |
| 188 | /* Stable states */ |
| 189 | STATE_NEW = 0, /* The peer has been just been created, PSM thread not started yet */ |
| 190 | STATE_OPEN, /* Connexion established */ |
| 191 | |
| 192 | /* Peer state machine */ |
| 193 | STATE_CLOSED, /* No connection established, will re-attempt after TcTimer. */ |
| 194 | STATE_CLOSING, /* the connection is being shutdown (DPR/DPA in progress) */ |
| 195 | STATE_WAITCNXACK, /* Attempting to establish transport-level connection */ |
| 196 | STATE_WAITCNXACK_ELEC, /* Received a CER from this same peer on an incoming connection (other peer object), while we were waiting for cnx ack */ |
| 197 | STATE_WAITCEA, /* Connection established, CER sent, waiting for CEA */ |
| 198 | /* STATE_WAITRETURNS_ELEC, */ /* This state is not stable and therefore deprecated: |
| 199 | We have sent a CER on our initiated connection, and received a CER from the remote peer on another connection. Election. |
| 200 | If we win the election, we must disconnect the initiated connection and send a CEA on the other => we go to OPEN state. |
| 201 | If we lose, we disconnect the other connection (receiver) and fallback to WAITCEA state. */ |
| 202 | STATE_OPEN_HANDSHAKE, /* TLS Handshake and validation are in progress in open state -- we use it only for debug purpose, it is never displayed */ |
| 203 | |
| 204 | /* Failover state machine */ |
| 205 | STATE_SUSPECT, /* A DWR was sent and not answered within TwTime. Failover in progress. */ |
| 206 | STATE_REOPEN, /* Connection has been re-established, waiting for 3 DWR/DWA exchanges before putting back to service */ |
| 207 | |
| 208 | /* Ordering issues with multistream & state machine. -- see top of p_psm.c for explanation */ |
| 209 | STATE_OPEN_NEW, /* after CEA is sent, until a new message is received. Force ordering in this state */ |
| 210 | STATE_CLOSING_GRACE, /* after DPA is sent or received, give a short delay for messages in the pipe to be received. */ |
| 211 | |
| 212 | /* Error state */ |
| 213 | STATE_ZOMBIE /* The PSM thread is not running anymore; it must be re-started or peer should be deleted. */ |
| 214 | #define STATE_MAX STATE_ZOMBIE |
| 215 | }; |
| 216 | /* The following macro is called in freeDiameter/p_psm.c */ |
| 217 | #define DECLARE_STATE_STR() \ |
| 218 | const char *peer_state_str[] = { \ |
| 219 | "STATE_NEW" \ |
| 220 | , "STATE_OPEN" \ |
| 221 | , "STATE_CLOSED" \ |
| 222 | , "STATE_CLOSING" \ |
| 223 | , "STATE_WAITCNXACK" \ |
| 224 | , "STATE_WAITCNXACK_ELEC" \ |
| 225 | , "STATE_WAITCEA" \ |
| 226 | , "STATE_OPEN_HANDSHAKE" \ |
| 227 | , "STATE_SUSPECT" \ |
| 228 | , "STATE_REOPEN" \ |
| 229 | , "STATE_OPEN_NEW" \ |
| 230 | , "STATE_CLOSING_GRACE" \ |
| 231 | , "STATE_ZOMBIE" \ |
| 232 | }; |
| 233 | extern const char *peer_state_str[]; |
| 234 | #define STATE_STR(state) \ |
| 235 | (((unsigned)(state)) <= STATE_MAX ? peer_state_str[((unsigned)(state)) ] : "<Invalid>") |
| 236 | |
| 237 | /* Constants for the peer_info structure below */ |
| 238 | #define PI_P3_DEFAULT 0 /* Use any available protocol */ |
| 239 | #define PI_P3_IP 1 /* Use only IP to connect to this peer */ |
| 240 | #define PI_P3_IPv6 2 /* resp, IPv6 */ |
| 241 | |
| 242 | #define PI_P4_DEFAULT 0 /* Attempt any available protocol */ |
| 243 | #define PI_P4_TCP 1 /* Only use TCP */ |
| 244 | #define PI_P4_SCTP 2 /* Only use SCTP */ |
| 245 | |
| 246 | #define PI_ALGPREF_SCTP 0 /* SCTP is attempted first (default) */ |
| 247 | #define PI_ALGPREF_TCP 1 /* TCP is attempted first */ |
| 248 | |
| 249 | #define PI_SEC_DEFAULT 0 /* New TLS security (handshake after connection, protecting also CER/CEA) */ |
| 250 | #define PI_SEC_NONE 1 /* Transparent security with this peer (IPsec) */ |
| 251 | #define PI_SEC_TLS_OLD 2 /* Old TLS security (use Inband-Security-Id AVP during CER/CEA) */ |
| 252 | /* Set sec = 3 to authorize use of (Inband-Security-Id == NONE) with this peer, sec = 2 only authorizing TLS */ |
| 253 | |
| 254 | #define PI_SCTPSEC_DEF 0 /* Use DTLS over SCTP to connect to this peer (default) */ |
| 255 | #define PI_SCTPSEC_3436 1 /* Use TLS over SCTP to connect to this peer (RFC3436) */ |
| 256 | |
| 257 | #define PI_EXP_NONE 0 /* the peer entry does not expire */ |
| 258 | #define PI_EXP_INACTIVE 1 /* the peer entry expires (i.e. is deleted) after pi_lft seconds without activity */ |
| 259 | |
| 260 | #define PI_PRST_NONE 0 /* the peer entry is deleted after disconnection / error */ |
| 261 | #define PI_PRST_ALWAYS 1 /* the peer entry is persistant (will be kept as ZOMBIE in case of error) */ |
| 262 | |
| 263 | /* Information about a remote peer */ |
| 264 | struct peer_info { |
| 265 | |
| 266 | DiamId_t pi_diamid; /* (supposedly) UTF-8, \0 terminated. The Diameter Identity of the remote peer. */ |
| 267 | size_t pi_diamidlen; /* cached length of pi_diamid */ |
| 268 | |
| 269 | struct { |
| 270 | struct { |
| 271 | unsigned pro3 :2; /* PI_P3_* */ |
| 272 | unsigned pro4 :2; /* PI_P4_* */ |
| 273 | unsigned alg :1; /* PI_ALGPREF_* */ |
| 274 | unsigned sec :2; /* PI_SEC_* */ |
| 275 | unsigned sctpsec :1; /* PI_SCTPSEC_* */ |
| 276 | unsigned exp :1; /* PI_EXP_* */ |
| 277 | unsigned persist :1; /* PI_PRST_* */ |
| 278 | |
| 279 | } pic_flags; /* Flags influencing the connection to the remote peer */ |
| 280 | |
| 281 | DiamId_t pic_realm; /* If configured, the daemon will check the received realm in CER/CEA matches this. */ |
| 282 | uint16_t pic_port; /* port to connect to. 0: default. */ |
| 283 | |
| 284 | uint32_t pic_lft; /* lifetime of this peer when inactive (see pic_flags.exp definition) */ |
| 285 | int pic_tctimer; /* use this value for TcTimer instead of global, if != 0 */ |
| 286 | int pic_twtimer; /* use this value for TwTimer instead of global, if != 0 */ |
| 287 | |
| 288 | char * pic_priority; /* Priority string for GnuTLS if we don't use the default */ |
| 289 | |
| 290 | } config; /* Configured data (static for this peer entry) */ |
| 291 | |
| 292 | struct { |
| 293 | |
| 294 | /* enum peer_state pir_state; */ |
| 295 | /* Since 1.1.0, read the state with fd_peer_getstate(peer). */ |
| 296 | |
| 297 | DiamId_t pir_realm; /* The received realm in CER/CEA. */ |
| 298 | size_t pir_realmlen; /* length of the realm */ |
| 299 | |
| 300 | uint32_t pir_vendorid; /* Content of the Vendor-Id AVP, or 0 by default */ |
| 301 | uint32_t pir_orstate; /* Origin-State-Id value */ |
| 302 | os0_t pir_prodname; /* copy of Product-Name AVP (\0 terminated) */ |
| 303 | uint32_t pir_firmrev; /* Content of the Firmware-Revision AVP */ |
| 304 | int pir_relay; /* The remote peer advertized the relay application */ |
| 305 | struct fd_list pir_apps; /* applications advertised by the remote peer, except relay (pi_flags.relay) */ |
| 306 | int pir_isi; /* Inband-Security-Id advertised (PI_SEC_* bits) */ |
| 307 | |
| 308 | uint32_t pir_lastDC; /* The last Disconnect-Cause value received */ |
| 309 | |
| 310 | int pir_proto; /* The L4 protocol currently used with the peer (IPPROTO_TCP or IPPROTO_SCTP) */ |
| 311 | const gnutls_datum_t *pir_cert_list; /* The (valid) credentials that the peer has presented, or NULL if TLS is not used */ |
| 312 | /* This is inspired from http://www.gnu.org/software/gnutls/manual/gnutls.html#ex_003ax509_002dinfo |
| 313 | see there for example of using this data */ |
| 314 | unsigned int pir_cert_list_size; /* Number of certificates in the list */ |
| 315 | |
| 316 | } runtime; /* Data populated after connection, may change between 2 connections -- not used by fd_peer_add */ |
| 317 | |
| 318 | struct fd_list pi_endpoints; /* Endpoint(s) of the remote peer (configured, discovered, or advertized). list of struct fd_endpoint. DNS resolved if empty. */ |
| 319 | }; |
| 320 | |
| 321 | |
| 322 | struct peer_hdr { |
| 323 | struct fd_list chain; /* Link into the list of all the peers, ordered by their Diameter Id (fd_os_cmp) */ |
| 324 | struct peer_info info; /* The public data */ |
| 325 | |
| 326 | /* This header is followed by more data in the private peer structure definition */ |
| 327 | }; |
| 328 | |
| 329 | /* the global list of peers. |
| 330 | Since we are not expecting so many connections, we don't use a hash, but it might be changed. |
| 331 | The list items are peer_hdr structures (actually, fd_peer, but the cast is OK) */ |
| 332 | extern struct fd_list fd_g_peers; |
| 333 | extern pthread_rwlock_t fd_g_peers_rw; /* protect the list */ |
| 334 | |
| 335 | /* |
| 336 | * FUNCTION: fd_peer_add |
| 337 | * |
| 338 | * PARAMETERS: |
| 339 | * info : Information to create the peer. |
| 340 | * orig_dbg : A string indicating the origin of the peer information, for debug (ex: conf, redirect, ...) |
| 341 | * cb : optional, a callback to call (once) when the peer connection is established or failed |
| 342 | * cb_data : opaque data to pass to the callback. |
| 343 | * |
| 344 | * DESCRIPTION: |
| 345 | * Add a peer to the list of peers to which the daemon must maintain a connexion. |
| 346 | * |
| 347 | * The content of info parameter is copied, except for the list of endpoints if |
| 348 | * not empty, which is simply moved into the created object. It means that the list |
| 349 | * items must have been malloc'd, so that they can be freed. |
| 350 | * |
| 351 | * If cb is not null, the callback is called when the connection is in OPEN state or |
| 352 | * when an error has occurred. The callback should use the pi_state information to |
| 353 | * determine which one it is. If the first parameter of the called callback is NULL, it |
| 354 | * means that the peer is being destroyed before attempt success / failure. |
| 355 | * cb is called to allow freeing cb_data in * this case. |
| 356 | * |
| 357 | * The orig_dbg string is only useful for easing debug, and can be left to NULL. |
| 358 | * |
| 359 | * RETURN VALUE: |
| 360 | * 0 : The peer is added. |
| 361 | * EINVAL : A parameter is invalid. |
| 362 | * EEXIST : A peer with the same Diameter-Id is already in the list. |
| 363 | * (other standard errors may be returned, too, with their standard meaning. Example: |
| 364 | * ENOMEM : Memory allocation for the new object element failed.) |
| 365 | */ |
| 366 | int fd_peer_add ( struct peer_info * info, const char * orig_dbg, void (*cb)(struct peer_info *, void *), void * cb_data ); |
| 367 | |
| 368 | /* |
| 369 | * FUNCTION: fd_peer_getbyid |
| 370 | * |
| 371 | * PARAMETERS: |
| 372 | * diamid : an UTF8 string describing the diameter Id of the peer to seek |
| 373 | * diamidlen : length of the diamid |
| 374 | * igncase : perform an almost-case-insensitive search? (slower) |
| 375 | * peer : The peer is stored here if it exists. |
| 376 | * |
| 377 | * DESCRIPTION: |
| 378 | * Search a peer by its Diameter-Id. |
| 379 | * |
| 380 | * RETURN VALUE: |
| 381 | * 0 : *peer has been updated (to NULL if the peer is not found). |
| 382 | * !0 : An error occurred. |
| 383 | */ |
| 384 | int fd_peer_getbyid( DiamId_t diamid, size_t diamidlen, int igncase, struct peer_hdr ** peer ); |
| 385 | |
| 386 | /* |
| 387 | * FUNCTION: fd_peer_get_state |
| 388 | * |
| 389 | * PARAMETERS: |
| 390 | * peer : The peer which state to read |
| 391 | * |
| 392 | * DESCRIPTION: |
| 393 | * Returns the current state of the peer. |
| 394 | * |
| 395 | * RETURN VALUE: |
| 396 | * -1 : peer is invalid |
| 397 | * >=0 : the state of the peer at the time of reading. |
| 398 | */ |
| 399 | int fd_peer_get_state(struct peer_hdr *peer); |
| 400 | |
| 401 | /* |
| 402 | * FUNCTION: fd_peer_cnx_proto_info |
| 403 | * |
| 404 | * PARAMETERS: |
| 405 | * peer : The peer which information to be read |
| 406 | * buf : Where to store the protocol information |
| 407 | * len : available space in bug |
| 408 | * |
| 409 | * DESCRIPTION: |
| 410 | * Creates a string describing the current connection to this peer, e.g.: "TCP,TLS,soc#3". |
| 411 | * |
| 412 | * RETURN VALUE: |
| 413 | * 0 : buffer was written |
| 414 | * >=0 : error code. |
| 415 | */ |
| 416 | int fd_peer_cnx_proto_info(struct peer_hdr *peer, char * buf, size_t len); |
| 417 | |
| 418 | /* |
| 419 | * FUNCTION: fd_peer_get_load_pending |
| 420 | * |
| 421 | * PARAMETERS: |
| 422 | * peer : The peer which load to read |
| 423 | * to_receive : (out) number of requests sent to this peer without matching answer yet. |
| 424 | * to_send : (out) number of requests received from this peer and not yet answered. |
| 425 | * |
| 426 | * DESCRIPTION: |
| 427 | * Returns the current number of requests sent to this peer |
| 428 | * that have not been answered yet. This is an empirical indication |
| 429 | * of the workload of this peer. |
| 430 | * |
| 431 | * RETURN VALUE: |
| 432 | * 0 : The load parameter has been updated. (it should have a positive value always) |
| 433 | * !0 : An error occurred |
| 434 | */ |
| 435 | int fd_peer_get_load_pending(struct peer_hdr *peer, long * to_receive, long * to_send); |
| 436 | |
| 437 | /* |
| 438 | * FUNCTION: fd_peer_validate_register |
| 439 | * |
| 440 | * PARAMETERS: |
| 441 | * peer_validate : Callback as defined below. |
| 442 | * |
| 443 | * DESCRIPTION: |
| 444 | * Add a callback to authorize / reject incoming peer connections. |
| 445 | * All registered callbacks are called until a callback sets auth = -1 or auth = 1. |
| 446 | * If no callback returns a clear decision, the default behavior is applied (reject unknown connections) |
| 447 | * The callbacks are called in FILO order of their registration. |
| 448 | * |
| 449 | * RETURN VALUE: |
| 450 | * 0 : The callback is added. |
| 451 | * !0 : An error occurred. |
| 452 | */ |
| 453 | int fd_peer_validate_register ( int (*peer_validate)(struct peer_info * /* info */, int * /* auth */, int (**cb2)(struct peer_info *)) ); |
| 454 | /* |
| 455 | * CALLBACK: peer_validate |
| 456 | * |
| 457 | * PARAMETERS: |
| 458 | * info : Structure containing information about the peer attempting the connection. |
| 459 | * auth : Store there the result if the peer is accepted (1), rejected (-1), or unknown (0). |
| 460 | * cb2 : If != NULL and in case of PI_SEC_TLS_OLD, another callback to call after handshake (if auth = 1). |
| 461 | * |
| 462 | * DESCRIPTION: |
| 463 | * This callback is called when a new connection is being established from an unknown peer, |
| 464 | * after the CER is received. An extension must register such callback with peer_validate_register. |
| 465 | * |
| 466 | * The callback can learn if the peer has sent Inband-Security-Id AVPs in runtime.pir_isi fields. |
| 467 | * It can also learn if a handshake has already been performed in runtime.pir_cert_list field. |
| 468 | * The callback must set the value of config.pic_flags.sec appropriately to allow a connection without TLS. |
| 469 | * |
| 470 | * If the old TLS mechanism is used, |
| 471 | * the extension may also need to check the credentials provided during the TLS |
| 472 | * exchange (remote certificate). For this purpose, it may set the address of a new callback |
| 473 | * to be called once the handshake is completed. This new callback receives the information |
| 474 | * structure as parameter (with pir_cert_list set) and returns 0 if the credentials are correct, |
| 475 | * or an error code otherwise. If the error code is received, the connection is closed and the |
| 476 | * peer is destroyed. |
| 477 | * Note that freeDiameter already achieves some usual checks. The callback may be used to enforce |
| 478 | * additional restrictions. |
| 479 | * |
| 480 | * RETURN VALUE: |
| 481 | * 0 : The authorization decision has been written in the location pointed by auth. |
| 482 | * !0 : An error occurred. |
| 483 | */ |
| 484 | |
| 485 | |
| 486 | |
| 487 | /*============================================================*/ |
| 488 | /* MESSAGES */ |
| 489 | /*============================================================*/ |
| 490 | |
| 491 | /* |
| 492 | * FUNCTION: fd_msg_send, fd_msg_send_timeout |
| 493 | * |
| 494 | * PARAMETERS: |
| 495 | * pmsg : Location of the message to be sent on the network (set to NULL on function return to avoid double deletion). |
| 496 | * anscb : A callback to be called when corresponding answer is received, when sending a request (not used with answers) |
| 497 | * anscb_data : opaque data to be passed back to the anscb (or expirecb) when it is called. |
| 498 | * expirecb : (only for fd_msg_send_timeout) If the request did not get an answer before timeout, this callback is called. |
| 499 | * timeout : (only for fd_msg_send_timeout) sets the absolute time until when to wait for an answer. Past this time, |
| 500 | * the expirecb is called with the request and the answer will be discarded if received later. |
| 501 | * |
| 502 | * DESCRIPTION: |
| 503 | * Sends a message on the network. (actually simply queues it in a global queue, to be picked by a daemon's thread) |
| 504 | * For requests, the end-to-end id must be set (see fd_msg_get_eteid / MSGFL_ALLOC_ETEID). |
| 505 | * For answers, the message must be created with function fd_msg_new_answer_from_req. |
| 506 | * |
| 507 | * The routing module will handle sending to the correct peer, usually based on the Destination-Realm / Destination-Host AVP. |
| 508 | * |
| 509 | * If the msg is a request, there are two ways of receiving the answer: |
| 510 | * - either having registered a callback in the dispatch module (see fd_disp_register) |
| 511 | * - or provide a anscb callback here. If such callback is provided, it is called before the dispatch callbacks. |
| 512 | * The prototype for this anscb callback function is: |
| 513 | * void anscb(void * data, struct msg ** answer) |
| 514 | * where: |
| 515 | * data : opaque data that was registered along with the callback. |
| 516 | * answer : location of the pointer to the answer. |
| 517 | * note1: on function return, if *answer is not NULL, the message is passed to the dispatch module for regular callbacks. |
| 518 | * otherwise, the callback must take care of freeing the message (fd_msg_free). |
| 519 | * note2: the opaque data is not freed by the daemon in any case, extensions should ensure clean handling in fd_ext_fini. |
| 520 | * |
| 521 | * If no callback is registered to handle an answer, the message is discarded and an error is logged. |
| 522 | * |
| 523 | * fd_msg_send_timeout is similar to fd_msg_send, except that it takes two additional arguments "expirecb" and "timeout". |
| 524 | * If the message parameter is an answer, there is no difference with fd_msg_send. |
| 525 | * Otherwise, if the corresponding answer (or error) is received before the timeout date elapses, everything occurs as with fd_msg_send. |
| 526 | * Otherwise, the request is removed from the queue (meaning the matching answer will be discarded upon reception) and passed to the expirecb |
| 527 | * function. Upon return, if the *msg parameter is not NULL, it is freed (not passed to other callbacks). |
| 528 | * expirecb is called in a dedicated thread. |
| 529 | * |
| 530 | * The prototype for the expirecb callback function is: |
| 531 | * void expirecb(void * data, struct peer_hdr * sentto, struct msg ** request) |
| 532 | * where: |
| 533 | * data : opaque data that was registered along with the callback. |
| 534 | * sentto : pointer to the peer to which the message was sent and no answer received within timeout. |
| 535 | * request: location of the pointer to the request that was not answered. |
| 536 | * |
| 537 | * RETURN VALUE: |
| 538 | * 0 : The message has been queued for sending (sending may fail asynchronously). |
| 539 | * EINVAL : A parameter is invalid (ex: anscb provided but message is not a request). |
| 540 | * ... |
| 541 | */ |
| 542 | int fd_msg_send ( struct msg ** pmsg, void (*anscb)(void *, struct msg **), void * data ); |
| 543 | int fd_msg_send_timeout ( struct msg ** pmsg, void (*anscb)(void *, struct msg **), void * data, void (*expirecb)(void *, DiamId_t, size_t, struct msg **), const struct timespec *timeout ); |
| 544 | |
| 545 | /* |
| 546 | * FUNCTION: fd_msg_rescode_set |
| 547 | * |
| 548 | * PARAMETERS: |
| 549 | * msg : A msg object -- it must be an answer. |
| 550 | * rescode : The name of the returned error code (ex: "DIAMETER_INVALID_AVP") |
| 551 | * errormsg : (optional) human-readable error message to put in Error-Message AVP |
| 552 | * optavp : (optional) If provided, the content will be put inside a Failed-AVP |
| 553 | * type_id : 0 => nothing; 1 => adds Origin-Host and Origin-Realm with local info. 2=> adds Error-Reporting-Host. |
| 554 | * |
| 555 | * DESCRIPTION: |
| 556 | * This function adds a Result-Code AVP to a message, and optionally |
| 557 | * - sets the 'E' error flag in the header, |
| 558 | * - adds Error-Message, Error-Reporting-Host and Failed-AVP AVPs. |
| 559 | * |
| 560 | * RETURN VALUE: |
| 561 | * 0 : Operation complete. |
| 562 | * !0 : an error occurred. |
| 563 | */ |
| 564 | int fd_msg_rescode_set( struct msg * msg, char * rescode, char * errormsg, struct avp * optavp, int type_id ); |
| 565 | |
| 566 | /* Add Origin-Host, Origin-Realm, (if osi) Origin-State-Id AVPS at the end of the message */ |
| 567 | int fd_msg_add_origin ( struct msg * msg, int osi ); |
| 568 | |
| 569 | /* Generate a new Session-Id and add it at the beginning of the message (opt is added at the end of the sid if provided) */ |
| 570 | int fd_msg_new_session( struct msg * msg, os0_t opt, size_t optlen ); |
| 571 | |
| 572 | |
| 573 | /* Parse a message against our dictionary, |
| 574 | return 0 in case of success. |
| 575 | log parsing error & return error code in case of failure in parsing. |
| 576 | In addition, if the error code is EBADMSG (the message does not follow our dictionary) |
| 577 | if *msg was a request, *msg is NULL and *error contains the error message ready to send back on return |
| 578 | if *msg was an answer, *msg is untouched and *error==*msg if *msg was an error message, *error is null otherwise */ |
| 579 | int fd_msg_parse_or_error( struct msg ** msg, struct msg **error ); |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | /*============================================================*/ |
| 585 | /* DISPATCH */ |
| 586 | /*============================================================*/ |
| 587 | |
| 588 | /* |
| 589 | * FUNCTION: fd_disp_app_support |
| 590 | * |
| 591 | * PARAMETERS: |
| 592 | * app : The dictionary object corresponding to the Application. |
| 593 | * vendor : (Optional) the dictionary object of a Vendor to claim support in Vendor-Specific-Application-Id |
| 594 | * auth : Support auth app part. |
| 595 | * acct : Support acct app part. |
| 596 | * |
| 597 | * DESCRIPTION: |
| 598 | * Registers an application to be advertized in CER/CEA exchanges. |
| 599 | * Messages with an application-id matching a registered value are passed to the dispatch module, |
| 600 | * while other messages are simply relayed or an error is returned (if local node does not relay) |
| 601 | * |
| 602 | * RETURN VALUE: |
| 603 | * 0 : The application support is registered. |
| 604 | * EINVAL : A parameter is invalid. |
| 605 | */ |
| 606 | int fd_disp_app_support ( struct dict_object * app, struct dict_object * vendor, int auth, int acct ); |
| 607 | |
| 608 | /* Note: if we want to support capabilities updates, we'll have to add possibility to remove an app as well... */ |
| 609 | |
| 610 | |
| 611 | /*============================================================*/ |
| 612 | /* ROUTING */ |
| 613 | /*============================================================*/ |
| 614 | |
| 615 | /* This file contains the definitions of types and functions involved in the routing decisions in freeDiameter, |
| 616 | * and that can be called by extensions. |
| 617 | * |
| 618 | * Three different type of messages must be distinguished: |
| 619 | * - Messages received, and the peer is final recipient (IN messages) |
| 620 | * - Messages received, and the peer is not final recipient (FWD messages) |
| 621 | * - Message is locally generated (OUT messages) |
| 622 | * |
| 623 | * There are three global message queues (in queues.c) and also peers-specific queues (in struct fd_peer). |
| 624 | * |
| 625 | * (*) IN messages processing details: |
| 626 | * - the message is received from the remote peer, a FDEVP_CNX_MSG_RECV event is generated for the peer. |
| 627 | * - the PSM thread parses the buffer, does some verifications, handles non routable messages (fd_msg_is_routable) |
| 628 | * - routable messages are queued in the fd_g_incoming global queue. |
| 629 | * - a thread (routing-in) picks the message and takes the decision if it is handled locally or forwarded, |
| 630 | * based on local capabilities (registered by extensions with fd_disp_app_support). |
| 631 | * - If the message is handled locally, it is queued in fd_g_local. |
| 632 | * - Another thread (dispatch.c) will handle this message and pass it to registered callbacks (see fd_disp_register in libfreeDiameter.h). |
| 633 | * |
| 634 | * (*) FWD messages details: |
| 635 | * - The process is the same as for IN messages, until the routing-in threads makes its decision that the message is not handled locally. |
| 636 | * - If the local peer does not relay message, an error DIAMETER_APPLICATION_UNSUPPORTED is returned. |
| 637 | * - All callbacks registered with fd_rt_fwd_register are called for the message (see below). |
| 638 | * - these callbacks will typically do proxying work. Note that adding the route-record is handled by the daemon. |
| 639 | * - Once all callbacks have been called, the message is queued in the global fd_g_outgoing queue. |
| 640 | * - The remaining processing is the same as for OUT messages, as described below. |
| 641 | * |
| 642 | * (*) OUT messages details: |
| 643 | * - The message are picked from fd_g_outgoing (they are queued there as result of forwarding process or call to fd_msg_send.) |
| 644 | * - The (routing-out) thread builds a list of possible destinations for the message, as follow: |
| 645 | * - create a list of all known peers in the "OPEN" state. |
| 646 | * - remove from that list all peers that are in a Route-Record AVP of the message, to avoid routing loops. |
| 647 | * - remove also all peers that have previously replied an error message for this message. |
| 648 | * - If the list is empty, create an error UNABLE_TO_DELIVER (note: should we trig dynamic discovery here???) and reply. |
| 649 | * - Otherwise, call all callbacks registered by function fd_rt_out_register, with the list of peers and the message. |
| 650 | * - Order the resulting list of peers by score (see below), and sent the message to the peer with highest (positive) score. |
| 651 | * - in case the peer is no longer in the "OPEN" state, send the message to the second peer in the list. |
| 652 | * - if no peer is in OPEN state anymore, restart the process of creating the list. |
| 653 | * - Once a peer has been selected, the message is queued into that peer's outgoing queue. |
| 654 | * |
| 655 | * The following functions allow an extension to register or remove a callback as described above. |
| 656 | */ |
| 657 | |
| 658 | /********** Forwarding callbacks: for Proxy operations ***********/ |
| 659 | |
| 660 | /* Handle to registered callback */ |
| 661 | struct fd_rt_fwd_hdl; |
| 662 | |
| 663 | /* Message direction for the callback */ |
| 664 | enum fd_rt_fwd_dir { |
| 665 | RT_FWD_REQ = 1, /* The callback will be called on forwarded requests only */ |
| 666 | RT_FWD_ALL = 2, /* The callback will be called on all forwarded messages (requests and answers )*/ |
| 667 | RT_FWD_ANS = 3 /* The callback will be called on answers and errors only */ |
| 668 | }; |
| 669 | |
| 670 | /* |
| 671 | * FUNCTION: fd_rt_fwd_register |
| 672 | * |
| 673 | * PARAMETERS: |
| 674 | * rt_fwd_cb : The callback function to register (see prototype below). |
| 675 | * cbdata : Pointer to pass to the callback when it is called. The data is opaque to the daemon. |
| 676 | * dir : One of the RT_FWD_* directions defined above. |
| 677 | * handler : On success, a handler to the registered callback is stored here. |
| 678 | * This handler will be used to unregister the cb. |
| 679 | * |
| 680 | * DESCRIPTION: |
| 681 | * Register a new callback for forwarded messages. See explanations above. |
| 682 | * Note that there is no guaranteed order for the callbacks calls. |
| 683 | * |
| 684 | * RETURN VALUE: |
| 685 | * 0 : The callback is registered. |
| 686 | * EINVAL : A parameter is invalid. |
| 687 | * ENOMEM : Not enough memory to complete the operation |
| 688 | */ |
| 689 | int fd_rt_fwd_register ( int (*rt_fwd_cb)(void * cbdata, struct msg ** msg), void * cbdata, enum fd_rt_fwd_dir dir, struct fd_rt_fwd_hdl ** handler ); |
| 690 | /* |
| 691 | * CALLBACK: rt_fwd_cb |
| 692 | * |
| 693 | * PARAMETERS: |
| 694 | * data : pointer to some data that was passed when the callback was registered (optional). |
| 695 | * msg : The message that is being forwarded. |
| 696 | * |
| 697 | * DESCRIPTION: |
| 698 | * This callback is called when a message is forwarded to another peer. It may for example add a Proxy-Info AVP. |
| 699 | * The callback may also choose to handle the message in a more complex form. In that case, it must set *msg = NULL |
| 700 | * and handle it differently. In such case, the forwarding thread will stop processing this message. |
| 701 | * |
| 702 | * RETURN VALUE: |
| 703 | * 0 : Operation complete. |
| 704 | * !0 : An error occurred -- will result in daemon's termination. |
| 705 | */ |
| 706 | |
| 707 | /* |
| 708 | * FUNCTION: fd_rt_fwd_unregister |
| 709 | * |
| 710 | * PARAMETERS: |
| 711 | * handler : The handler of the callback that must be unregistered. |
| 712 | * cbdata : Will receive the data registered with the callback, that can be freed if needed. |
| 713 | * |
| 714 | * DESCRIPTION: |
| 715 | * Removes a callback from the list of registered callbacks. |
| 716 | * |
| 717 | * RETURN VALUE: |
| 718 | * 0 : The callback is unregistered. |
| 719 | * EINVAL : A parameter is invalid. |
| 720 | */ |
| 721 | int fd_rt_fwd_unregister ( struct fd_rt_fwd_hdl * handler, void ** cbdata ); |
| 722 | |
| 723 | |
| 724 | /********** Out callbacks: for next hop routing decision operations ***********/ |
| 725 | |
| 726 | /* Handle to registered callback */ |
| 727 | struct fd_rt_out_hdl; |
| 728 | |
| 729 | enum fd_rt_out_score { |
| 730 | FD_SCORE_NO_DELIVERY = -70, /* We should not send this message to this candidate */ |
| 731 | FD_SCORE_SENT_REDIRECT = -60, /* If this peer previously sent a Redirect indication that applies to this message */ |
| 732 | FD_SCORE_INI = -2, /* All candidates are initialized with this value */ |
| 733 | FD_SCORE_LOAD_BALANCE = 1, /* Use this to differentiate between several peers with the same score */ |
| 734 | FD_SCORE_DEFAULT = 5, /* The peer is a default route for all messages */ |
| 735 | FD_SCORE_DEFAULT_REALM = 10, /* The peer is a default route for this realm */ |
| 736 | FD_SCORE_REALM = 15, /* The peer belongs to Destination-Realm of the message */ |
| 737 | FD_SCORE_REDIR_HOST = 25, /* If there is a redirect rule with ALL_HOST for these message and peer */ |
| 738 | FD_SCORE_REDIR_APP = 30, /* If there is a redirect rule with ALL_APPLICATION for these message and peer */ |
| 739 | FD_SCORE_REDIR_REALM = 35, /* If there is a redirect rule with ALL_REALM for these message and peer */ |
| 740 | FD_SCORE_REDIR_REALM_APP = 40, /* If there is a redirect rule with REALM_AND_APPLICATION for these message and peer */ |
| 741 | FD_SCORE_REDIR_USER = 45, /* If there is a redirect rule with ALL_USER for these message and peer */ |
| 742 | FD_SCORE_REDIR_SESSION = 50, /* If there is a redirect rule with ALL_SESSION for these message and peer */ |
| 743 | FD_SCORE_REDIR_ONCE = 55, /* If there is a redirect rule with DONT_CACHE for these message and peer */ |
| 744 | FD_SCORE_FINALDEST = 100 /* If the peer is the final recipient of the message (i.e. matching Destination-Host), it receives a big score. */ |
| 745 | }; |
| 746 | |
| 747 | /* |
| 748 | * FUNCTION: fd_rt_out_register |
| 749 | * |
| 750 | * PARAMETERS: |
| 751 | * rt_out_cb : The callback function to register (see prototype below). |
| 752 | * cbdata : Pointer to pass to the callback when it is called. The data is opaque to the daemon. |
| 753 | * priority : Order for calling this callback. The callbacks are called in reverse priority order (higher priority = called sooner). |
| 754 | * handler : On success, a handler to the registered callback is stored here. |
| 755 | * This handler will be used to unregister the cb. |
| 756 | * |
| 757 | * DESCRIPTION: |
| 758 | * Register a new callback to handle OUT routing decisions. See explanations above. |
| 759 | * |
| 760 | * RETURN VALUE: |
| 761 | * 0 : The callback is registered. |
| 762 | * EINVAL : A parameter is invalid. |
| 763 | * ENOMEM : Not enough memory to complete the operation |
| 764 | */ |
| 765 | int fd_rt_out_register ( int (*rt_out_cb)(void * cbdata, struct msg ** pmsg, struct fd_list * candidates), void * cbdata, int priority, struct fd_rt_out_hdl ** handler ); |
| 766 | /* |
| 767 | * CALLBACK: rt_out_cb |
| 768 | * |
| 769 | * PARAMETERS: |
| 770 | * cbdata : pointer to some data that was registered with the callback. |
| 771 | * pmsg : pointer to the message that must be sent. upon return if *msg is NULL, the processing stops and the message is not sent. |
| 772 | * list : The list of peers to which the message may be sent to, as returned by fd_rtd_candidate_extract |
| 773 | * |
| 774 | * DESCRIPTION: |
| 775 | * This callback must attribute a score (preferably from FD_SCORE_*) to each candidate peer in the list. |
| 776 | * Once all registered callbacks have been called, the message is sent to the candidate with the highest score. |
| 777 | * Note that each callback must *add* its locally-attributed score to the candidate current "score" parameter, not replace it! |
| 778 | * Note also that this callback must be re-entrant since it may be called by several threads at the same time |
| 779 | * (for different messages) |
| 780 | * |
| 781 | * RETURN VALUE: |
| 782 | * 0 : Operation complete. |
| 783 | * !0 : An error occurred. |
| 784 | */ |
| 785 | |
| 786 | /* |
| 787 | * FUNCTION: fd_rt_out_unregister |
| 788 | * |
| 789 | * PARAMETERS: |
| 790 | * handler : The handler of the callback that must be unregistered. |
| 791 | * cbdata : Will receive the data registered with the callback, that can be freed if needed. |
| 792 | * |
| 793 | * DESCRIPTION: |
| 794 | * Removes a callback from the list of registered callbacks. |
| 795 | * |
| 796 | * RETURN VALUE: |
| 797 | * 0 : The callback is unregistered. |
| 798 | * EINVAL : A parameter is invalid. |
| 799 | */ |
| 800 | int fd_rt_out_unregister ( struct fd_rt_out_hdl * handler, void ** cbdata ); |
| 801 | |
| 802 | |
| 803 | /*============================================================*/ |
| 804 | /* EVENTS */ |
| 805 | /*============================================================*/ |
| 806 | |
| 807 | struct fd_event { |
| 808 | int code; /* codespace depends on the queue */ |
| 809 | size_t size; |
| 810 | void *data; |
| 811 | }; |
| 812 | |
| 813 | /* Daemon's codespace: 1000->1999 (1500->1999 defined in fdcore-internal.h) */ |
| 814 | enum { |
| 815 | FDEV_TERMINATE_INT= 1000 /* request to terminate. DO NOT USE. Use fd_core_shutdown() instead. */ |
| 816 | ,FDEV_TRIGGER /* Trigger available for extensions. size is sizeof(int), data is int * */ |
| 817 | }; |
| 818 | |
| 819 | int fd_event_send(struct fifo *queue, int code, size_t datasz, void * data); |
| 820 | int fd_event_get(struct fifo *queue, int * code, size_t * datasz, void ** data); |
| 821 | int fd_event_timedget(struct fifo *queue, struct timespec * timeout, int timeoutcode, int * code, size_t * datasz, void ** data); |
| 822 | void fd_event_destroy(struct fifo **queue, void (*free_cb)(void * data)); |
| 823 | const char * fd_ev_str(int event); |
| 824 | |
| 825 | /* for extensions */ |
| 826 | int fd_event_trig_regcb(int trigger_val, const char * module, void (*cb)(void)); |
| 827 | |
| 828 | #ifndef SWIG |
| 829 | DECLARE_FD_DUMP_PROTOTYPE(fd_event_trig_dump); |
| 830 | |
| 831 | /* The "old" FD_EV_DUMP_* events are replaced with direct calls to the following dump functions */ |
| 832 | DECLARE_FD_DUMP_PROTOTYPE(fd_conf_dump); |
| 833 | |
| 834 | DECLARE_FD_DUMP_PROTOTYPE(fd_ext_dump); |
| 835 | #else /* SWIG */ |
| 836 | DECLARE_FD_DUMP_PROTOTYPE_simple(fd_event_trig_dump); |
| 837 | DECLARE_FD_DUMP_PROTOTYPE_simple(fd_conf_dump); |
| 838 | DECLARE_FD_DUMP_PROTOTYPE_simple(fd_ext_dump); |
| 839 | #endif /* SWIG */ |
| 840 | DECLARE_FD_DUMP_PROTOTYPE(fd_servers_dump, int details); |
| 841 | DECLARE_FD_DUMP_PROTOTYPE(fd_peer_dump_list, int details); |
| 842 | DECLARE_FD_DUMP_PROTOTYPE(fd_peer_dump, struct peer_hdr * p, int details); |
| 843 | |
| 844 | /*============================================================*/ |
| 845 | /* ENDPOINTS */ |
| 846 | /*============================================================*/ |
| 847 | |
| 848 | struct fd_endpoint { |
| 849 | struct fd_list chain; /* link in cnf_endpoints list */ |
| 850 | |
| 851 | union { |
| 852 | sSS ss; /* the socket information. List is always ordered by ss value (memcmp) -- see fd_ep_add_merge */ |
| 853 | sSA4 sin; |
| 854 | sSA6 sin6; |
| 855 | sSA sa; |
| 856 | } |
| 857 | #ifdef SWIG /* nested anonymous unions are not supported yet */ |
| 858 | s |
| 859 | #endif /* SWIG */ |
| 860 | ; |
| 861 | |
| 862 | #define EP_FL_CONF (1 << 0) /* This endpoint is statically configured in a configuration file */ |
| 863 | #define EP_FL_DISC (1 << 1) /* This endpoint was resolved from the Diameter Identity or other DNS query */ |
| 864 | #define EP_FL_ADV (1 << 2) /* This endpoint was advertized in Diameter CER/CEA exchange */ |
| 865 | #define EP_FL_LL (1 << 3) /* Lower layer mechanism provided this endpoint */ |
| 866 | #define EP_FL_PRIMARY (1 << 4) /* This endpoint is primary in a multihomed SCTP association */ |
| 867 | #define EP_ACCEPTALL (1 << 15) /* This flag allows bypassing the address filter in fd_ep_add_merge. */ |
| 868 | uint32_t flags; /* Additional information about the endpoint */ |
| 869 | |
| 870 | /* To add: a validity timestamp for DNS records ? How do we retrieve this lifetime from DNS ? */ |
| 871 | }; |
| 872 | |
| 873 | int fd_ep_add_merge( struct fd_list * list, sSA * sa, socklen_t sl, uint32_t flags ); |
| 874 | int fd_ep_filter( struct fd_list * list, uint32_t flags ); |
| 875 | int fd_ep_filter_family( struct fd_list * list, int af ); |
| 876 | int fd_ep_clearflags( struct fd_list * list, uint32_t flags ); |
| 877 | DECLARE_FD_DUMP_PROTOTYPE(fd_ep_dump_one, int preamble, struct fd_endpoint * ep ); |
| 878 | DECLARE_FD_DUMP_PROTOTYPE(fd_ep_dump, int preamble, int indent, struct fd_list * eps ); |
| 879 | |
| 880 | |
| 881 | /*============================================================*/ |
| 882 | /* APPLICATIONS IDs */ |
| 883 | /*============================================================*/ |
| 884 | |
| 885 | struct fd_app { |
| 886 | struct fd_list chain; /* link in cnf_apps list. List ordered by appid. */ |
| 887 | struct { |
| 888 | unsigned auth : 1; |
| 889 | unsigned acct : 1; |
| 890 | } flags; |
| 891 | vendor_id_t vndid; /* if not 0, Vendor-Specific-App-Id AVP will be used */ |
| 892 | application_id_t appid; /* The identifier of the application */ |
| 893 | }; |
| 894 | |
| 895 | int fd_app_merge(struct fd_list * list, application_id_t aid, vendor_id_t vid, int auth, int acct); |
| 896 | int fd_app_check(struct fd_list * list, application_id_t aid, struct fd_app **detail); |
| 897 | int fd_app_check_common(struct fd_list * list1, struct fd_list * list2, int * common_found); |
| 898 | int fd_app_empty(struct fd_list * list); |
| 899 | |
| 900 | |
| 901 | |
| 902 | /*============================================================*/ |
| 903 | /* MONITORING */ |
| 904 | /*============================================================*/ |
| 905 | |
| 906 | /* These functions allow an extension to collect state information about the |
| 907 | * framework, as well as being hooked at some key checkpoints in the processing |
| 908 | * for logging or statistics purpose. |
| 909 | */ |
| 910 | |
| 911 | |
| 912 | /* CALLBACK: fd_hook_cb |
| 913 | * |
| 914 | * PARAMETERS: |
| 915 | * type : The type of hook that triggered this call, in case same cb is registered for several hooks. |
| 916 | * msg : If relevant, the pointer to the message triggering the call. NULL otherwise. |
| 917 | * peer : If relevant, the pointer to the peer associated with the call. NULL otherwise. |
| 918 | * other : For some callbacks, the remaining information is passed in this parameter. See each hook detail. |
| 919 | * permsgdata : Structure associated with a given message, across several hooks. |
| 920 | * A different structure is associated with requests and corresponding answers. |
| 921 | * See fd_hook_data_hdl below for details. |
| 922 | * If no fd_hook_data_hdl is registered with this callback, this parameter is always NULL |
| 923 | * regdata : Data pointer stored at registration, opaque for the framework. |
| 924 | * |
| 925 | * DESCRIPTION: |
| 926 | * When such callback is registered with fd_hook_register function, it will be called on matching events with |
| 927 | * the parameters as described in the list of fd_hook_type below. One can use this mechanism for e.g.: |
| 928 | * - log completely the messages for safety / backup |
| 929 | * - create statistics information on the throughput |
| 930 | * - ... |
| 931 | * |
| 932 | * IMPORTANT: the callback MUST NOT change the memory pointed by the different parameters (peer, message, ...) |
| 933 | * |
| 934 | * RETURN VALUE: |
| 935 | * none. |
| 936 | */ |
| 937 | |
| 938 | /* The available hooks in the framework */ |
| 939 | enum fd_hook_type { |
| 940 | |
| 941 | HOOK_DATA_RECEIVED = 0, |
| 942 | /* Hook called as soon as a message has been received from the network, after TLS & boundary processing. |
| 943 | - {msg} is NULL. |
| 944 | - {peer} is NULL. |
| 945 | - {other} is a pointer to a struct fd_cnx_rcvdata containing the received buffer. |
| 946 | - {permsgdata} points to either a new empty structure allocated for this message (cf. fd_hook_data_hdl), or NULL if no hdl is registered. |
| 947 | */ |
| 948 | |
| 949 | HOOK_MESSAGE_RECEIVED, |
| 950 | /* Hook called when a message has been received and the structure has been parsed successfully (list of AVPs). |
| 951 | - {msg} points to the parsed message. At this time, the objects have not been dictionary resolved. If you |
| 952 | try to call fd_msg_parse_dict, it will slow down the operation of a relay agent. |
| 953 | - {peer} is set if the message is received from a peer's connection, and NULL if the message is from a new client |
| 954 | connected and not yet identified |
| 955 | - {other} is NULL, or a char * identifying the connection when {peer} is null. |
| 956 | - {permsgdata} points to either a new empty structure allocated for this message or the one passed to HOOK_DATA_RECEIVED if used. |
| 957 | */ |
| 958 | |
| 959 | HOOK_MESSAGE_LOCAL, |
| 960 | /* Hook called when a request message has been created locally by an extension and is being sent. |
| 961 | - {msg} points to the message. |
| 962 | - {peer} is NULL |
| 963 | - {other} is NULL |
| 964 | - {permsgdata} points to a new empty structure allocated for this request (cf. fd_hook_data_hdl) |
| 965 | */ |
| 966 | |
| 967 | HOOK_MESSAGE_SENDING, |
| 968 | /* Hook called when a message is about to be sent to a peer. The message pointer cannot be modified here, but the content of the message |
| 969 | could still be changed (for example add or remove some AVP. This is the last chance. |
| 970 | - {msg} points to the message. Some objects may not have been dictionary resolved. If you |
| 971 | try to call fd_msg_parse_dict, it will slow down the operation of the instance. |
| 972 | - {peer} is the one the message is being sent to. |
| 973 | - {other} is NULL. |
| 974 | - {permsgdata} points to existing structure if any, or a new structure otherwise. |
| 975 | */ |
| 976 | |
| 977 | HOOK_MESSAGE_SENT, |
| 978 | /* Hook called when a message has been sent to a peer. The message might be freed as soon as the hook function returns, |
| 979 | so it is not safe to store the pointer for asynchronous processing. |
| 980 | - {msg} points to the sent message. Again, the objects may not have been dictionary resolved. If you |
| 981 | try to call fd_msg_parse_dict, it will slow down the operation of a relay agent. |
| 982 | - {peer} is set if the message is sent to a peer's connection, and NULL if the message is sent to a new client |
| 983 | connected and not yet identified, or being rejected |
| 984 | - {other} is NULL. |
| 985 | - {permsgdata} points to existing structure if any, or a new structure otherwise. |
| 986 | */ |
| 987 | |
| 988 | HOOK_MESSAGE_FAILOVER, |
| 989 | /* Hook called when a message that was sent to a peer is being requeued, because e.g. the connection was torn down. |
| 990 | In that case the message will go again through the routing process. |
| 991 | - {msg} points to the corresponding request message (the answer is discarded). Again, the objects may not have been dictionary resolved. If you |
| 992 | try to call fd_msg_parse_dict, it might slow down the operation of a relay agent, although this hook is not on the normal execution path. |
| 993 | - {peer} is the peer this message was previously sent to. |
| 994 | - {other} is NULL. |
| 995 | - {permsgdata} points to existing structure if any, or a new structure otherwise. |
| 996 | */ |
| 997 | |
| 998 | HOOK_MESSAGE_PARSING_ERROR, |
| 999 | /* Hook called when a message being processed cannot be parsed successfully. |
| 1000 | - {msg} points to the message if buffer was parsed successfully, or NULL otherwise. You should not call fd_msg_parse_dict on this in any case. |
| 1001 | - {peer} is NULL or the peer that received the message. If NULL and the message is not NULL, you can still retrieve the source from the message itself. |
| 1002 | - {other} is a char * pointer to the error message (human-readable) if {msg} is not NULL, a pointer to struct fd_cnx_rcvdata containing the received buffer otherwise. |
| 1003 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1004 | */ |
| 1005 | |
| 1006 | HOOK_MESSAGE_ROUTING_ERROR, |
| 1007 | /* Hook called when a message being processed by the routing thread meets an error such as no remaining available peer for sending, based on routing callbacks decisions (maybe after retries). |
| 1008 | - {msg} points to the message. Again, the objects may not have been dictionary resolved. If you |
| 1009 | try to call fd_msg_parse_dict, it might slow down the operation of a relay agent, although this hook is not on the normal execution path. |
| 1010 | - {peer} is NULL. |
| 1011 | - {other} is a char * pointer to the error message (human-readable). |
| 1012 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1013 | */ |
| 1014 | |
| 1015 | HOOK_MESSAGE_ROUTING_FORWARD, |
| 1016 | /* Hook called when a received message is deemed to be not handled locally by the routing_dispatch process. |
| 1017 | The decision of knowing which peer it will be sent to is not made yet (or if an error will be returned). |
| 1018 | The hook is trigged before the callbacks registered with fd_rt_fwd_register are called. |
| 1019 | - {msg} points to the message. Again, the objects may not have been dictionary resolved. |
| 1020 | If you try to call fd_msg_parse_dict, it will slow down the operation of a relay agent. |
| 1021 | - {peer} is NULL. |
| 1022 | - {other} is NULL. |
| 1023 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1024 | */ |
| 1025 | |
| 1026 | HOOK_MESSAGE_ROUTING_LOCAL, |
| 1027 | /* Hook called when a received message is handled locally by the routing_dispatch process (i.e., not forwarded). |
| 1028 | The hook is trigged before the callbacks registered with fd_disp_register are called. |
| 1029 | - {msg} points to the message. Here, the message has been already parsed completely & successfully. |
| 1030 | - {peer} is NULL. |
| 1031 | - {other} is NULL. |
| 1032 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1033 | */ |
| 1034 | |
| 1035 | HOOK_MESSAGE_DROPPED, |
| 1036 | /* Hook called when a message is being discarded by the framework because of some error condition (normal or abnormal). |
| 1037 | It is probably a good idea to log this for analysis / backup. |
| 1038 | - {msg} points to the message, which will be freed as soon as the hook returns. |
| 1039 | - {peer} may be NULL or a peer related to the event. |
| 1040 | - {other} is a char * pointer to the error message (human-readable). |
| 1041 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1042 | */ |
| 1043 | |
| 1044 | HOOK_PEER_CONNECT_FAILED, |
| 1045 | /* Hook called when a connection attempt to/from a remote peer has failed. This hook is also called when the peer was in OPEN state and the connection is broken. |
| 1046 | - {msg} may be NULL (lower layer error, e.g. connection timeout) or points to a message showing the error (either invalid incoming message, or the CEA message sent or received with an error code). |
| 1047 | - {peer} may be NULL for incoming requests from unknown peers being rejected, otherwise it points to the peer structure associated with the attempt. |
| 1048 | - {other} is a char * pointer to the error message (human-readable). |
| 1049 | - {permsgdata} is always NULL for this hook. |
| 1050 | */ |
| 1051 | |
| 1052 | HOOK_PEER_CONNECT_SUCCESS, |
| 1053 | /* Hook called when a connection attempt to/from a remote peer has succeeded (the peer moves to OPEN_HANDSHAKE or OPEN state). |
| 1054 | In case of deprecated TLS handshake after the CER/CEA exchange, this hook can still be followed by HOOK_PEER_CONNECT_FAILED if TLS handshake fails. |
| 1055 | - {msg} points to the CEA message sent or received (with a success code) -- in case it is sent, you can always get access to the matching CER. |
| 1056 | - {peer} points to the peer structure. |
| 1057 | - {other} is NULL. |
| 1058 | - {permsgdata} is always NULL for this hook. |
| 1059 | */ |
| 1060 | |
| 1061 | HOOK_MESSAGE_PARSING_ERROR2, |
| 1062 | /* Hook called after an error message has been generated due to a dictionary parsing error. |
| 1063 | - {msg} points to the error message that has been generated. |
| 1064 | - {peer} is NULL. You can still retrieve the source from the message itself. |
| 1065 | - {other} is NULL |
| 1066 | - {permsgdata} points to existing structure associated with this message (or new structure if no previous hook was registered). |
| 1067 | Use this hook if you need to populate more data in the error being returned, from the error message. |
| 1068 | (e.g. some AVP need to be added to replies even if error case. |
| 1069 | */ |
| 1070 | #define HOOK_LAST HOOK_MESSAGE_PARSING_ERROR2 |
| 1071 | }; |
| 1072 | |
| 1073 | |
| 1074 | /* Type of the {permsgdata} pointer. It is up to each extension to define its own structure. This is opaque for the framework. */ |
| 1075 | struct fd_hook_permsgdata; |
| 1076 | |
| 1077 | /* A handle that will be associated with the extension, and with the permsgdata structures. */ |
| 1078 | struct fd_hook_data_hdl; |
| 1079 | |
| 1080 | /* The following structure is what is passed to the HOOK_DATA_RECEIVED hook */ |
| 1081 | struct fd_cnx_rcvdata { |
| 1082 | size_t length; |
| 1083 | uint8_t * buffer; /* internal note: the buffer is padded with a struct fd_msg_pmdl, not accounted for in length */ |
| 1084 | }; |
| 1085 | |
| 1086 | /* Function to register a new fd_hook_data_hdl. Should be called by your extension init function. |
| 1087 | * The arguments are the functions called to initialize a new fd_hook_permsgdata and to free this structure when the corresponding message is being freed. |
| 1088 | */ |
| 1089 | /* |
| 1090 | * FUNCTION: fd_hook_data_register |
| 1091 | * |
| 1092 | * PARAMETERS: |
| 1093 | * permsgdata_size : the size of the fd_hook_permsgdata structure. |
| 1094 | * permsgdata_init_cb : function called to initialize a new fd_hook_permsgdata structure, when a hook will be called for a message that does not have such structure yet. |
| 1095 | * The memory is already allocated and blanked, so you can pass NULL if no further handling is required. |
| 1096 | * permsgdata_fini_cb : function called when a message is being disposed. It should free the resources associated with the fd_hook_permsgdata. |
| 1097 | * You can pass NULL if no special handling is required. The memory of the permsgdata structure itself will be freed by the framework. |
| 1098 | * new_handle : On success, a handler to the registered callback is stored here. |
| 1099 | * This handler will be used to unregister the cb. |
| 1100 | * |
| 1101 | * DESCRIPTION: |
| 1102 | * Register a new fd_hook_data_hdl. This handle is used during hooks registration (see below) in order to associate data with the messages, to allow keeping tracking of the message easily. |
| 1103 | * Note that these handlers are statically allocated and cannot be unregistered. FD_HOOK_HANDLE_LIMIT handlers can be registered at maximum (recompile libfdproto if you change this value) |
| 1104 | * |
| 1105 | * RETURN VALUE: |
| 1106 | * 0 : The callback is registered. |
| 1107 | * EINVAL : A parameter is invalid. |
| 1108 | * ENOSPC : Too many handles already registered. You may need to increase the limit in the code. |
| 1109 | */ |
| 1110 | int fd_hook_data_register( |
| 1111 | size_t permsgdata_size, |
| 1112 | void (*permsgdata_init_cb) (struct fd_hook_permsgdata *), |
| 1113 | void (*permsgdata_fini_cb) (struct fd_hook_permsgdata *), |
| 1114 | struct fd_hook_data_hdl **new_handle |
| 1115 | ); |
| 1116 | |
| 1117 | /* A handler associated with a registered hook callback (for cleanup) */ |
| 1118 | struct fd_hook_hdl; |
| 1119 | |
| 1120 | /* Helper for building a mask of hooks for registration */ |
| 1121 | #define HOOK_MASK(hooklist...) fd_hook_mask_helper(0, ## hooklist, -1) |
| 1122 | |
| 1123 | /* |
| 1124 | * FUNCTION: fd_hook_register |
| 1125 | * |
| 1126 | * PARAMETERS: |
| 1127 | * type_mask : A bitmask of fd_hook_type bits for which this cb is registered, e.g. HOOK_MASK( HOOK_MESSAGE_RECEIVED, HOOK_MESSAGE_SENT ) |
| 1128 | * fd_hook_cb : The callback function to register (see prototype above). |
| 1129 | * regdata : Pointer to pass to the callback when it is called. The data is opaque to the daemon. |
| 1130 | * data_hdl : If permsgdata is requested for the hooks, a handler registered with fd_hook_data_register. NULL otherwise. |
| 1131 | * handler : On success, a handler to the registered callback is stored here. |
| 1132 | * This handler can be used to unregister the cb. |
| 1133 | * |
| 1134 | * DESCRIPTION: |
| 1135 | * Register a new hook in the framework. See explanations above. |
| 1136 | * |
| 1137 | * RETURN VALUE: |
| 1138 | * 0 : The callback is registered. |
| 1139 | * EEXIST : Another callback is already registered for this type of hook (HOOK_DATA_RECEIVED). |
| 1140 | * EINVAL : A parameter is invalid. |
| 1141 | * ENOMEM : Not enough memory to complete the operation |
| 1142 | */ |
| 1143 | int fd_hook_register ( uint32_t type_mask, |
| 1144 | void (*fd_hook_cb)(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata), |
| 1145 | void *regdata, |
| 1146 | struct fd_hook_data_hdl *data_hdl, |
| 1147 | struct fd_hook_hdl ** handler ); |
| 1148 | |
| 1149 | /* Remove a hook registration */ |
| 1150 | int fd_hook_unregister( struct fd_hook_hdl * handler ); |
| 1151 | |
| 1152 | |
| 1153 | /* Use the following function to retrieve any pmd structure associated with a request matching the current answer. Returns NULL in case of error / no such structure */ |
| 1154 | struct fd_hook_permsgdata * fd_hook_get_request_pmd(struct fd_hook_data_hdl *data_hdl, struct msg * answer); |
| 1155 | |
| 1156 | /* The following is used by HOOK_MASK macro */ |
| 1157 | uint32_t fd_hook_mask_helper(int dummy, ...); |
| 1158 | |
| 1159 | /*============================================================*/ |
| 1160 | |
| 1161 | /* |
| 1162 | * The following allows an extension to retrieve stat information on the different fifo queues involved in the freeDiameter framework. |
| 1163 | * There are three global queues, plus per-peer queues. |
| 1164 | * This information can be used to build SNMP-like data for example, or quickly get a status of the framework to find the loaded path of execution / bottlenecks. |
| 1165 | */ |
| 1166 | enum fd_stat_type { |
| 1167 | /* For the following, no peer is associated with the stat */ |
| 1168 | STAT_G_LOCAL= 1, /* Get statistics for the global queue of messages processed by local extensions */ |
| 1169 | STAT_G_INCOMING, /* Get statistics for the global queue of received messages to be processed by routing_in thread */ |
| 1170 | STAT_G_OUTGOING, /* Get statistics for the global queue of messages to be processed by routing_out thread */ |
| 1171 | |
| 1172 | /* For the following, the peer must be provided */ |
| 1173 | STAT_P_PSM, /* Peer state machine queue (events to be processed for this peer, including received messages) */ |
| 1174 | STAT_P_TOSEND, /* Queue of messages for sending to this peer */ |
| 1175 | }; |
| 1176 | |
| 1177 | /* |
| 1178 | * FUNCTION: fd_stat_getstats |
| 1179 | * |
| 1180 | * PARAMETERS: |
| 1181 | * stat : Which queue is being queried |
| 1182 | * peer : (depending on the stat parameter) which peer is being queried |
| 1183 | * current_count : (out) The number of items in the queue currently |
| 1184 | * limit_count : (out) The max number of items the queue accepts before becoming blocking -- 0 means no max. |
| 1185 | * highest_count : (out) The highest count the queue has reached since startup |
| 1186 | * total_count : (out) Total number of items that this queue has processed (always growing, use deltas for monitoring) |
| 1187 | * total : (out) Cumulated time all items spent in this queue, including blocking time (always growing, use deltas for monitoring) |
| 1188 | * blocking : (out) Cumulated time threads trying to post new items were blocked (queue full). |
| 1189 | * last : (out) For the last element retrieved from the queue, how long it took between posting (including blocking) and poping |
| 1190 | * |
| 1191 | * DESCRIPTION: |
| 1192 | * Get statistics information about a given queue. |
| 1193 | * Any of the (out) parameters can be NULL if not requested. |
| 1194 | * |
| 1195 | * RETURN VALUE: |
| 1196 | * 0 : The callback is registered. |
| 1197 | * EINVAL : A parameter is invalid. |
| 1198 | */ |
| 1199 | int fd_stat_getstats(enum fd_stat_type stat, struct peer_hdr * peer, |
| 1200 | int * current_count, int * limit_count, int * highest_count, long long * total_count, |
| 1201 | struct timespec * total, struct timespec * blocking, struct timespec * last); |
| 1202 | |
| 1203 | /*============================================================*/ |
| 1204 | /* EOF */ |
| 1205 | /*============================================================*/ |
| 1206 | |
| 1207 | #ifdef __cplusplus |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
| 1211 | #endif /* _LIBFDCORE_H */ |