Asterisk - The Open Source Telephony Project  18.5.0
res_rtp_asterisk.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  *
22  * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
23  *
24  * \author Mark Spencer <[email protected]>
25  *
26  * \note RTP is defined in RFC 3550.
27  *
28  * \ingroup rtp_engines
29  */
30 
31 /*** MODULEINFO
32  <use type="external">openssl</use>
33  <use type="external">pjproject</use>
34  <support_level>core</support_level>
35  ***/
36 
37 #include "asterisk.h"
38 
39 #include <sys/time.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 
43 #ifdef HAVE_OPENSSL
44 #include <openssl/opensslconf.h>
45 #include <openssl/opensslv.h>
46 #if !defined(OPENSSL_NO_SRTP) && (OPENSSL_VERSION_NUMBER >= 0x10001000L)
47 #include <openssl/ssl.h>
48 #include <openssl/err.h>
49 #include <openssl/bio.h>
50 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L)
51 #include <openssl/bn.h>
52 #endif
53 #ifndef OPENSSL_NO_DH
54 #include <openssl/dh.h>
55 #endif
56 #endif
57 #endif
58 
59 #ifdef HAVE_PJPROJECT
60 #include <pjlib.h>
61 #include <pjlib-util.h>
62 #include <pjnath.h>
63 #include <ifaddrs.h>
64 #endif
65 
66 #include "asterisk/conversions.h"
67 #include "asterisk/options.h"
69 #include "asterisk/stun.h"
70 #include "asterisk/pbx.h"
71 #include "asterisk/frame.h"
72 #include "asterisk/format_cache.h"
73 #include "asterisk/channel.h"
74 #include "asterisk/acl.h"
75 #include "asterisk/config.h"
76 #include "asterisk/lock.h"
77 #include "asterisk/utils.h"
78 #include "asterisk/cli.h"
79 #include "asterisk/manager.h"
80 #include "asterisk/unaligned.h"
81 #include "asterisk/module.h"
82 #include "asterisk/rtp_engine.h"
83 #include "asterisk/smoother.h"
84 #include "asterisk/uuid.h"
85 #include "asterisk/test.h"
86 #include "asterisk/data_buffer.h"
87 #ifdef HAVE_PJPROJECT
88 #include "asterisk/res_pjproject.h"
90 #endif
91 
92 #define MAX_TIMESTAMP_SKEW 640
93 
94 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
95 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
96 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
97 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
98 
99 #define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
100 #define DEFAULT_RTP_END 31000 /*!< Default maximum port number to end allocating RTP ports at */
101 
102 #define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
103 #define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
104 
105 #define DEFAULT_TURN_PORT 3478
106 
107 #define TURN_STATE_WAIT_TIME 2000
108 
109 #define DEFAULT_RTP_SEND_BUFFER_SIZE 250 /*!< The initial size of the RTP send buffer */
110 #define MAXIMUM_RTP_SEND_BUFFER_SIZE (DEFAULT_RTP_SEND_BUFFER_SIZE + 200) /*!< Maximum RTP send buffer size */
111 #define DEFAULT_RTP_RECV_BUFFER_SIZE 20 /*!< The initial size of the RTP receiver buffer */
112 #define MAXIMUM_RTP_RECV_BUFFER_SIZE (DEFAULT_RTP_RECV_BUFFER_SIZE + 20) /*!< Maximum RTP receive buffer size */
113 #define OLD_PACKET_COUNT 1000 /*!< The number of previous packets that are considered old */
114 #define MISSING_SEQNOS_ADDED_TRIGGER 2 /*!< The number of immediate missing packets that will trigger an immediate NACK */
115 
116 #define SEQNO_CYCLE_OVER 65536 /*!< The number after the maximum allowed sequence number */
117 
118 /*! Full INTRA-frame Request / Fast Update Request (From RFC2032) */
119 #define RTCP_PT_FUR 192
120 /*! Sender Report (From RFC3550) */
121 #define RTCP_PT_SR AST_RTP_RTCP_SR
122 /*! Receiver Report (From RFC3550) */
123 #define RTCP_PT_RR AST_RTP_RTCP_RR
124 /*! Source Description (From RFC3550) */
125 #define RTCP_PT_SDES 202
126 /*! Goodbye (To remove SSRC's from tables) (From RFC3550) */
127 #define RTCP_PT_BYE 203
128 /*! Application defined (From RFC3550) */
129 #define RTCP_PT_APP 204
130 /* VP8: RTCP Feedback */
131 /*! Payload Specific Feed Back (From RFC4585 also RFC5104) */
132 #define RTCP_PT_PSFB AST_RTP_RTCP_PSFB
133 
134 #define RTP_MTU 1200
135 #define DTMF_SAMPLE_RATE_MS 8 /*!< DTMF samples per millisecond */
136 
137 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */
138 
139 #define ZFONE_PROFILE_ID 0x505a
140 
141 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
142 /*!
143  * \brief Calculate the min learning duration in ms.
144  *
145  * \details
146  * The min supported packet size represents 10 ms and we need to account
147  * for some jitter and fast clocks while learning. Some messed up devices
148  * have very bad jitter for a small packet sample size. Jitter can also
149  * be introduced by the network itself.
150  *
151  * So we'll allow packets to come in every 9ms on average for fast clocking
152  * with the last one coming in 5ms early for jitter.
153  */
154 #define CALC_LEARNING_MIN_DURATION(count) (((count) - 1) * 9 - 5)
155 #define DEFAULT_LEARNING_MIN_DURATION CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL)
156 
157 #define SRTP_MASTER_KEY_LEN 16
158 #define SRTP_MASTER_SALT_LEN 14
159 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
160 
161 #define RTP_DTLS_ESTABLISHED -37
162 
164  STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
165  STRICT_RTP_LEARN, /*! Accept next packet as source */
166  STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
167 };
168 
170  STRICT_RTP_NO = 0, /*! Don't adhere to any strict RTP rules */
171  STRICT_RTP_YES, /*! Strict RTP that restricts packets based on time and sequence number */
172  STRICT_RTP_SEQNO, /*! Strict RTP that restricts packets based on sequence number */
173 };
174 
175 /*!
176  * \brief Strict RTP learning timeout time in milliseconds
177  *
178  * \note Set to 5 seconds to allow reinvite chains for direct media
179  * to settle before media actually starts to arrive. There may be a
180  * reinvite collision involved on the other leg.
181  */
182 #define STRICT_RTP_LEARN_TIMEOUT 5000
183 
184 #define DEFAULT_STRICT_RTP STRICT_RTP_YES /*!< Enabled by default */
185 #define DEFAULT_SRTP_REPLAY_PROTECTION 1
186 #define DEFAULT_ICESUPPORT 1
187 #define DEFAULT_STUN_SOFTWARE_ATTRIBUTE 1
188 #define DEFAULT_DTLS_MTU 1200
189 
190 extern struct ast_srtp_res *res_srtp;
191 extern struct ast_srtp_policy_res *res_srtp_policy;
192 
194 
195 static int rtpstart = DEFAULT_RTP_START; /*!< First port for RTP sessions (set in rtp.conf) */
196 static int rtpend = DEFAULT_RTP_END; /*!< Last port for RTP sessions (set in rtp.conf) */
197 static int rtcpstats; /*!< Are we debugging RTCP? */
198 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
199 static struct ast_sockaddr rtpdebugaddr; /*!< Debug packets to/from this host */
200 static struct ast_sockaddr rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
201 static int rtpdebugport; /*!< Debug only RTP packets from IP or IP+Port if port is > 0 */
202 static int rtcpdebugport; /*!< Debug only RTCP packets from IP or IP+Port if port is > 0 */
203 #ifdef SO_NO_CHECK
204 static int nochecksums;
205 #endif
206 static int strictrtp = DEFAULT_STRICT_RTP; /*!< Only accept RTP frames from a defined source. If we receive an indication of a changing source, enter learning mode. */
207 static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; /*!< Number of sequential RTP frames needed from a single source during learning mode to accept new source. */
208 static int learning_min_duration = DEFAULT_LEARNING_MIN_DURATION; /*!< Lowest acceptable timeout between the first and the last sequential RTP frame. */
210 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
211 static int dtls_mtu = DEFAULT_DTLS_MTU;
212 #endif
213 #ifdef HAVE_PJPROJECT
216 static struct sockaddr_in stunaddr;
217 static pj_str_t turnaddr;
219 static pj_str_t turnusername;
220 static pj_str_t turnpassword;
222 static struct ast_sockaddr lo6 = { .len = 0 };
223 
224 /*! ACL for ICE addresses */
225 static struct ast_acl_list *ice_acl = NULL;
227 
228 /*! ACL for STUN requests */
229 static struct ast_acl_list *stun_acl = NULL;
231 
232 /*! \brief Pool factory used by pjlib to allocate memory. */
233 static pj_caching_pool cachingpool;
234 
235 /*! \brief Global memory pool for configuration and timers */
236 static pj_pool_t *pool;
237 
238 /*! \brief Global timer heap */
239 static pj_timer_heap_t *timer_heap;
240 
241 /*! \brief Thread executing the timer heap */
242 static pj_thread_t *timer_thread;
243 
244 /*! \brief Used to tell the timer thread to terminate */
245 static int timer_terminate;
246 
247 /*! \brief Structure which contains ioqueue thread information */
249  /*! \brief Pool used by the thread */
250  pj_pool_t *pool;
251  /*! \brief The thread handling the queue and timer heap */
252  pj_thread_t *thread;
253  /*! \brief Ioqueue which polls on sockets */
254  pj_ioqueue_t *ioqueue;
255  /*! \brief Timer heap for scheduled items */
256  pj_timer_heap_t *timerheap;
257  /*! \brief Termination request */
259  /*! \brief Current number of descriptors being waited on */
260  unsigned int count;
261  /*! \brief Linked list information */
263 };
264 
265 /*! \brief List of ioqueue threads */
267 
268 /*! \brief Structure which contains ICE host candidate mapping information */
270  struct ast_sockaddr local;
271  struct ast_sockaddr advertised;
272  unsigned int include_local;
274 };
275 
276 /*! \brief List of ICE host candidate mappings */
278 
279 static char *generate_random_string(char *buf, size_t size);
280 
281 #endif
282 
283 #define FLAG_3389_WARNING (1 << 0)
284 #define FLAG_NAT_ACTIVE (3 << 1)
285 #define FLAG_NAT_INACTIVE (0 << 1)
286 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
287 #define FLAG_NEED_MARKER_BIT (1 << 3)
288 #define FLAG_DTMF_COMPENSATE (1 << 4)
289 #define FLAG_REQ_LOCAL_BRIDGE_BIT (1 << 5)
290 
291 #define TRANSPORT_SOCKET_RTP 0
292 #define TRANSPORT_SOCKET_RTCP 1
293 #define TRANSPORT_TURN_RTP 2
294 #define TRANSPORT_TURN_RTCP 3
295 
296 /*! \brief RTP learning mode tracking information */
298  struct ast_sockaddr proposed_address; /*!< Proposed remote address for strict RTP */
299  struct timeval start; /*!< The time learning mode was started */
300  struct timeval received; /*!< The time of the first received packet */
301  int max_seq; /*!< The highest sequence number received */
302  int packets; /*!< The number of remaining packets before the source is accepted */
303  /*! Type of media stream carried by the RTP instance */
304  enum ast_media_type stream_type;
305 };
306 
307 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
308 struct dtls_details {
309  SSL *ssl; /*!< SSL session */
310  BIO *read_bio; /*!< Memory buffer for reading */
311  BIO *write_bio; /*!< Memory buffer for writing */
312  enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
313  enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
314  int timeout_timer; /*!< Scheduler id for timeout timer */
315 };
316 #endif
317 
318 #ifdef HAVE_PJPROJECT
319 /*! An ao2 wrapper protecting the PJPROJECT ice structure with ref counting. */
320 struct ice_wrap {
321  pj_ice_sess *real_ice; /*!< ICE session */
322 };
323 #endif
324 
325 /*! \brief Structure used for mapping an incoming SSRC to an RTP instance */
327  /*! \brief The received SSRC */
328  unsigned int ssrc;
329  /*! True if the SSRC is available. Otherwise, this is a placeholder mapping until the SSRC is set. */
330  unsigned int ssrc_valid;
331  /*! \brief The RTP instance this SSRC belongs to*/
333 };
334 
335 /*! \brief Packet statistics (used for transport-cc) */
337  /*! The transport specific sequence number */
338  unsigned int seqno;
339  /*! The time at which the packet was received */
340  struct timeval received;
341  /*! The delta between this packet and the previous */
342  int delta;
343 };
344 
345 /*! \brief Statistics information (used for transport-cc) */
347  /*! A vector of packet statistics */
348  AST_VECTOR(, struct rtp_transport_wide_cc_packet_statistics) packet_statistics; /*!< Packet statistics, used for transport-cc */
349  /*! The last sequence number received */
350  unsigned int last_seqno;
351  /*! The last extended sequence number */
352  unsigned int last_extended_seqno;
353  /*! How many feedback packets have gone out */
354  unsigned int feedback_count;
355  /*! How many cycles have occurred for the sequence numbers */
356  unsigned int cycles;
357  /*! Scheduler id for periodic feedback transmission */
358  int schedid;
359 };
360 
361 typedef struct {
362  unsigned int ts;
363  unsigned char is_set;
364 } optional_ts;
365 
366 /*! \brief RTP session description */
367 struct ast_rtp {
368  int s;
369  /*! \note The f.subclass.format holds a ref. */
370  struct ast_frame f;
371  unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
372  unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
373  unsigned int ssrc_orig; /*!< SSRC used before native bridge activated */
374  unsigned char ssrc_saved; /*!< indicates if ssrc_orig has a value */
375  char cname[AST_UUID_STR_LEN]; /*!< Our local CNAME */
376  unsigned int themssrc; /*!< Their SSRC */
377  unsigned int themssrc_valid; /*!< True if their SSRC is available. */
378  unsigned int lastts;
379  unsigned int lastividtimestamp;
380  unsigned int lastovidtimestamp;
381  unsigned int lastitexttimestamp;
382  unsigned int lastotexttimestamp;
383  int lastrxseqno; /*!< Last received sequence number, from the network */
384  int expectedrxseqno; /*!< Next expected sequence number, from the network */
385  AST_VECTOR(, int) missing_seqno; /*!< A vector of sequence numbers we never received */
386  int expectedseqno; /*!< Next expected sequence number, from the core */
387  unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
388  unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
389  unsigned int rxcount; /*!< How many packets have we received? */
390  unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
391  unsigned int txcount; /*!< How many packets have we sent? */
392  unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
393  unsigned int cycles; /*!< Shifted count of sequence number cycles */
394  double rxjitter; /*!< Interarrival jitter at the moment in seconds to be reported */
395  double rxtransit; /*!< Relative transit time for previous packet */
396  struct ast_format *lasttxformat;
397  struct ast_format *lastrxformat;
398 
399  /* DTMF Reception Variables */
400  char resp; /*!< The current digit being processed */
401  unsigned int last_seqno; /*!< The last known sequence number for any DTMF packet */
402  optional_ts last_end_timestamp; /*!< The last known timestamp received from an END packet */
403  unsigned int dtmf_duration; /*!< Total duration in samples since the digit start event */
404  unsigned int dtmf_timeout; /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
405  unsigned int dtmfsamples;
406  enum ast_rtp_dtmf_mode dtmfmode; /*!< The current DTMF mode of the RTP stream */
407  /* DTMF Transmission Variables */
408  unsigned int lastdigitts;
409  char sending_digit; /*!< boolean - are we sending digits */
410  char send_digit; /*!< digit we are sending */
411  int send_payload;
412  int send_duration;
413  unsigned int flags;
414  struct timeval rxcore;
415  struct timeval txcore;
416  double drxcore; /*!< The double representation of the first received packet */
417  struct timeval dtmfmute;
418  struct ast_smoother *smoother;
419  unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
420  struct ast_sched_context *sched;
421  struct ast_rtcp *rtcp;
422  unsigned int asymmetric_codec; /*!< Indicate if asymmetric send/receive codecs are allowed */
423 
424  struct ast_rtp_instance *bundled; /*!< The RTP instance we are bundled to */
425  int stream_num; /*!< Stream num for this RTP instance */
426  AST_VECTOR(, struct rtp_ssrc_mapping) ssrc_mapping; /*!< Mappings of SSRC to RTP instances */
427  struct ast_sockaddr bind_address; /*!< Requested bind address for the sockets */
428 
429  enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
430  struct ast_sockaddr strict_rtp_address; /*!< Remote address information for strict RTP purposes */
431 
432  /*
433  * Learning mode values based on pjmedia's probation mode. Many of these values are redundant to the above,
434  * but these are in place to keep learning mode sequence values sealed from their normal counterparts.
435  */
436  struct rtp_learning_info rtp_source_learn; /* Learning mode track for the expected RTP source */
437 
438  struct rtp_red *red;
439 
440  struct ast_data_buffer *send_buffer; /*!< Buffer for storing sent packets for retransmission */
441  struct ast_data_buffer *recv_buffer; /*!< Buffer for storing received packets for retransmission */
442 
443  struct rtp_transport_wide_cc_statistics transport_wide_cc; /*!< Transport-cc statistics information */
444 
445 #ifdef HAVE_PJPROJECT
446  ast_cond_t cond; /*!< ICE/TURN condition for signaling */
447 
448  struct ice_wrap *ice; /*!< ao2 wrapped ICE session */
449  enum ast_rtp_ice_role role; /*!< Our role in ICE negotiation */
450  pj_turn_sock *turn_rtp; /*!< RTP TURN relay */
451  pj_turn_sock *turn_rtcp; /*!< RTCP TURN relay */
452  pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
453  unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
454  unsigned int rtp_passthrough:1; /*!< Bit to indicate that TURN RTP should be passed through */
455  unsigned int rtcp_passthrough:1; /*!< Bit to indicate that TURN RTCP should be passed through */
456  unsigned int ice_port; /*!< Port that ICE was started with if it was previously started */
457  struct ast_sockaddr rtp_loop; /*!< Loopback address for forwarding RTP from TURN */
458  struct ast_sockaddr rtcp_loop; /*!< Loopback address for forwarding RTCP from TURN */
459 
460  struct ast_rtp_ioqueue_thread *ioqueue; /*!< The ioqueue thread handling us */
461 
462  char remote_ufrag[256]; /*!< The remote ICE username */
463  char remote_passwd[256]; /*!< The remote ICE password */
464 
465  char local_ufrag[256]; /*!< The local ICE username */
466  char local_passwd[256]; /*!< The local ICE password */
467 
468  struct ao2_container *ice_local_candidates; /*!< The local ICE candidates */
469  struct ao2_container *ice_active_remote_candidates; /*!< The remote ICE candidates */
470  struct ao2_container *ice_proposed_remote_candidates; /*!< Incoming remote ICE candidates for new session */
471  struct ast_sockaddr ice_original_rtp_addr; /*!< rtp address that ICE started on first session */
472  unsigned int ice_num_components; /*!< The number of ICE components */
473  unsigned int ice_media_started:1; /*!< ICE media has started, either on a valid pair or on ICE completion */
474 #endif
475 
476 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
477  SSL_CTX *ssl_ctx; /*!< SSL context */
478  enum ast_rtp_dtls_verify dtls_verify; /*!< What to verify */
479  enum ast_srtp_suite suite; /*!< SRTP crypto suite */
480  enum ast_rtp_dtls_hash local_hash; /*!< Local hash used for the fingerprint */
481  char local_fingerprint[160]; /*!< Fingerprint of our certificate */
482  enum ast_rtp_dtls_hash remote_hash; /*!< Remote hash used for the fingerprint */
483  unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
484  unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
485  int rekeyid; /*!< Scheduled item id for rekeying */
486  struct dtls_details dtls; /*!< DTLS state information */
487 #endif
488 };
489 
490 /*!
491  * \brief Structure defining an RTCP session.
492  *
493  * The concept "RTCP session" is not defined in RFC 3550, but since
494  * this structure is analogous to ast_rtp, which tracks a RTP session,
495  * it is logical to think of this as a RTCP session.
496  *
497  * RTCP packet is defined on page 9 of RFC 3550.
498  *
499  */
500 struct ast_rtcp {
502  int s; /*!< Socket */
503  struct ast_sockaddr us; /*!< Socket representation of the local endpoint. */
504  struct ast_sockaddr them; /*!< Socket representation of the remote endpoint. */
505  unsigned int soc; /*!< What they told us */
506  unsigned int spc; /*!< What they told us */
507  unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
508  struct timeval rxlsr; /*!< Time when we got their last SR */
509  struct timeval txlsr; /*!< Time when we sent or last SR*/
510  unsigned int expected_prior; /*!< no. packets in previous interval */
511  unsigned int received_prior; /*!< no. packets received in previous interval */
512  int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
513  unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
514  unsigned int sr_count; /*!< number of SRs we've sent */
515  unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
516  double accumulated_transit; /*!< accumulated a-dlsr-lsr */
517  double rtt; /*!< Last reported rtt */
518  unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
519  unsigned int reported_lost; /*!< Reported lost packets in their RR */
520 
521  double reported_maxjitter; /*!< Maximum reported interarrival jitter */
522  double reported_minjitter; /*!< Minimum reported interarrival jitter */
523  double reported_normdev_jitter; /*!< Mean of reported interarrival jitter */
524  double reported_stdev_jitter; /*!< Standard deviation of reported interarrival jitter */
525  unsigned int reported_jitter_count; /*!< Reported interarrival jitter count */
526 
527  double reported_maxlost; /*!< Maximum reported packets lost */
528  double reported_minlost; /*!< Minimum reported packets lost */
529  double reported_normdev_lost; /*!< Mean of reported packets lost */
530  double reported_stdev_lost; /*!< Standard deviation of reported packets lost */
531  unsigned int reported_lost_count; /*!< Reported packets lost count */
532 
533  double rxlost; /*!< Calculated number of lost packets since last report */
534  double maxrxlost; /*!< Maximum calculated lost number of packets between reports */
535  double minrxlost; /*!< Minimum calculated lost number of packets between reports */
536  double normdev_rxlost; /*!< Mean of calculated lost packets between reports */
537  double stdev_rxlost; /*!< Standard deviation of calculated lost packets between reports */
538  unsigned int rxlost_count; /*!< Calculated lost packets sample count */
539 
540  double maxrxjitter; /*!< Maximum of calculated interarrival jitter */
541  double minrxjitter; /*!< Minimum of calculated interarrival jitter */
542  double normdev_rxjitter; /*!< Mean of calculated interarrival jitter */
543  double stdev_rxjitter; /*!< Standard deviation of calculated interarrival jitter */
544  unsigned int rxjitter_count; /*!< Calculated interarrival jitter count */
545 
546  double maxrtt; /*!< Maximum of calculated round trip time */
547  double minrtt; /*!< Minimum of calculated round trip time */
548  double normdevrtt; /*!< Mean of calculated round trip time */
549  double stdevrtt; /*!< Standard deviation of calculated round trip time */
550  unsigned int rtt_count; /*!< Calculated round trip time count */
551 
552  /* VP8: sequence number for the RTCP FIR FCI */
553  int firseq;
554 
555 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
556  struct dtls_details dtls; /*!< DTLS state information */
557 #endif
558 
559  /* Cached local address string allows us to generate
560  * RTCP stasis messages without having to look up our
561  * own address every time
562  */
565  /* Buffer for frames created during RTCP interpretation */
566  unsigned char frame_buf[512 + AST_FRIENDLY_OFFSET];
567 };
568 
569 struct rtp_red {
570  struct ast_frame t140; /*!< Primary data */
571  struct ast_frame t140red; /*!< Redundant t140*/
572  unsigned char pt[AST_RED_MAX_GENERATION]; /*!< Payload types for redundancy data */
573  unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
574  unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
575  int num_gen; /*!< Number of generations */
576  int schedid; /*!< Timer id */
577  int ti; /*!< How long to buffer data before send */
578  unsigned char t140red_data[64000];
579  unsigned char buf_data[64000]; /*!< buffered primary data */
580  int hdrlen;
581  long int prev_ts;
582 };
583 
584 /*! \brief Structure for storing RTP packets for retransmission */
586  size_t size; /*!< The size of the payload */
587  unsigned char buf[0]; /*!< The payload data */
588 };
589 
591 
592 /* Forward Declarations */
593 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
594 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
595 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
596 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
597 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
598 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
599 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
600 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
601 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
602 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
603 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
604 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
605 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
606 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
607 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
608 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
609 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
610 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
611 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
612 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
613 static void ast_rtp_stop(struct ast_rtp_instance *instance);
614 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
615 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
616 static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance);
617 static const char *ast_rtp_get_cname(struct ast_rtp_instance *instance);
618 static void ast_rtp_set_remote_ssrc(struct ast_rtp_instance *instance, unsigned int ssrc);
619 static void ast_rtp_set_stream_num(struct ast_rtp_instance *instance, int stream_num);
620 static int ast_rtp_extension_enable(struct ast_rtp_instance *instance, enum ast_rtp_extension extension);
621 static int ast_rtp_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent);
622 
623 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
624 static int ast_rtp_activate(struct ast_rtp_instance *instance);
625 static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
626 static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
627 static int dtls_bio_write(BIO *bio, const char *buf, int len);
628 static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2);
629 static int dtls_bio_new(BIO *bio);
630 static int dtls_bio_free(BIO *bio);
631 
632 #ifndef HAVE_OPENSSL_BIO_METHOD
633 static BIO_METHOD dtls_bio_methods = {
634  .type = BIO_TYPE_BIO,
635  .name = "rtp write",
636  .bwrite = dtls_bio_write,
637  .ctrl = dtls_bio_ctrl,
638  .create = dtls_bio_new,
639  .destroy = dtls_bio_free,
640 };
641 #else
642 static BIO_METHOD *dtls_bio_methods;
643 #endif
644 #endif
645 
646 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp);
647 
648 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
649 static int dtls_bio_new(BIO *bio)
650 {
651 #ifdef HAVE_OPENSSL_BIO_METHOD
652  BIO_set_init(bio, 1);
653  BIO_set_data(bio, NULL);
654  BIO_set_shutdown(bio, 0);
655 #else
656  bio->init = 1;
657  bio->ptr = NULL;
658  bio->flags = 0;
659 #endif
660  return 1;
661 }
662 
663 static int dtls_bio_free(BIO *bio)
664 {
665  /* The pointer on the BIO is that of the RTP instance. It is not reference counted as the BIO
666  * lifetime is tied to the instance, and actions on the BIO are taken by the thread handling
667  * the RTP instance - not another thread.
668  */
669 #ifdef HAVE_OPENSSL_BIO_METHOD
670  BIO_set_data(bio, NULL);
671 #else
672  bio->ptr = NULL;
673 #endif
674  return 1;
675 }
676 
677 static int dtls_bio_write(BIO *bio, const char *buf, int len)
678 {
679 #ifdef HAVE_OPENSSL_BIO_METHOD
680  struct ast_rtp_instance *instance = BIO_get_data(bio);
681 #else
682  struct ast_rtp_instance *instance = bio->ptr;
683 #endif
684  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
685  int rtcp = 0;
686  struct ast_sockaddr remote_address = { {0, } };
687  int ice;
688  int bytes_sent;
689 
690  /* OpenSSL can't tolerate a packet not being sent, so we always state that
691  * we sent the packet. If it isn't then retransmission will occur.
692  */
693 
694  if (rtp->rtcp && rtp->rtcp->dtls.write_bio == bio) {
695  rtcp = 1;
696  ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
697  } else {
698  ast_rtp_instance_get_remote_address(instance, &remote_address);
699  }
700 
701  if (ast_sockaddr_isnull(&remote_address)) {
702  return len;
703  }
704 
705  bytes_sent = __rtp_sendto(instance, (char *)buf, len, 0, &remote_address, rtcp, &ice, 0);
706 
707  if (bytes_sent > 0 && ast_debug_dtls_packet_is_allowed) {
708  ast_debug(0, "(%p) DTLS - sent %s packet to %s%s (len %-6.6d)\n",
709  instance, rtcp ? "RTCP" : "RTP", ast_sockaddr_stringify(&remote_address),
710  ice ? " (via ICE)" : "", bytes_sent);
711  }
712 
713  return len;
714 }
715 
716 static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2)
717 {
718  switch (cmd) {
719  case BIO_CTRL_FLUSH:
720  return 1;
721  case BIO_CTRL_DGRAM_QUERY_MTU:
722  return dtls_mtu;
723  case BIO_CTRL_WPENDING:
724  case BIO_CTRL_PENDING:
725  return 0L;
726  default:
727  return 0;
728  }
729 }
730 
731 #endif
732 
733 #ifdef HAVE_PJPROJECT
734 /*! \brief Helper function which clears the ICE host candidate mapping */
736 {
737  struct ast_ice_host_candidate *candidate;
738 
742  ast_free(candidate);
743  }
746 }
747 
748 /*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
749 static void update_address_with_ice_candidate(pj_ice_sess *ice, enum ast_rtp_ice_component_type component,
750  struct ast_sockaddr *cand_address)
751 {
752  char address[PJ_INET6_ADDRSTRLEN];
753 
754  if (component < 1 || !ice->comp[component - 1].valid_check) {
755  return;
756  }
757 
758  ast_sockaddr_parse(cand_address,
759  pj_sockaddr_print(&ice->comp[component - 1].valid_check->rcand->addr, address,
760  sizeof(address), 0), 0);
761  ast_sockaddr_set_port(cand_address,
762  pj_sockaddr_get_port(&ice->comp[component - 1].valid_check->rcand->addr));
763 }
764 
765 /*! \brief Destructor for locally created ICE candidates */
766 static void ast_rtp_ice_candidate_destroy(void *obj)
767 {
768  struct ast_rtp_engine_ice_candidate *candidate = obj;
769 
770  if (candidate->foundation) {
771  ast_free(candidate->foundation);
772  }
773 
774  if (candidate->transport) {
775  ast_free(candidate->transport);
776  }
777 }
778 
779 /*! \pre instance is locked */
780 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
781 {
782  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
783  int ice_attrb_reset = 0;
784 
785  if (!ast_strlen_zero(ufrag)) {
786  if (!ast_strlen_zero(rtp->remote_ufrag) && strcmp(ufrag, rtp->remote_ufrag)) {
787  ice_attrb_reset = 1;
788  }
789  ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
790  }
791 
792  if (!ast_strlen_zero(password)) {
793  if (!ast_strlen_zero(rtp->remote_passwd) && strcmp(password, rtp->remote_passwd)) {
794  ice_attrb_reset = 1;
795  }
796  ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
797  }
798 
799  /* If the remote ufrag or passwd changed, local ufrag and passwd need to regenerate */
800  if (ice_attrb_reset) {
801  generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
802  generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
803  }
804 }
805 
806 static int ice_candidate_cmp(void *obj, void *arg, int flags)
807 {
808  struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
809 
810  if (strcmp(candidate1->foundation, candidate2->foundation) ||
811  candidate1->id != candidate2->id ||
812  candidate1->type != candidate2->type ||
813  ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) {
814  return 0;
815  }
816 
817  return CMP_MATCH | CMP_STOP;
818 }
819 
820 /*! \pre instance is locked */
821 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
822 {
823  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
824  struct ast_rtp_engine_ice_candidate *remote_candidate;
825 
826  /* ICE sessions only support UDP candidates */
827  if (strcasecmp(candidate->transport, "udp")) {
828  return;
829  }
830 
831  if (!rtp->ice_proposed_remote_candidates) {
832  rtp->ice_proposed_remote_candidates = ao2_container_alloc_list(
834  if (!rtp->ice_proposed_remote_candidates) {
835  return;
836  }
837  }
838 
839  /* If this is going to exceed the maximum number of ICE candidates don't even add it */
840  if (ao2_container_count(rtp->ice_proposed_remote_candidates) == PJ_ICE_MAX_CAND) {
841  return;
842  }
843 
844  if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
845  return;
846  }
847 
848  remote_candidate->foundation = ast_strdup(candidate->foundation);
849  remote_candidate->id = candidate->id;
850  remote_candidate->transport = ast_strdup(candidate->transport);
851  remote_candidate->priority = candidate->priority;
852  ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
853  ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
854  remote_candidate->type = candidate->type;
855 
856  ast_debug_ice(2, "(%p) ICE add remote candidate\n", instance);
857 
858  ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
859  ao2_ref(remote_candidate, -1);
860 }
861 
863 
864 /*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
865 static void pj_thread_register_check(void)
866 {
867  pj_thread_desc *desc;
868  pj_thread_t *thread;
869 
870  if (pj_thread_is_registered() == PJ_TRUE) {
871  return;
872  }
873 
874  desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
875  if (!desc) {
876  ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
877  return;
878  }
879  pj_bzero(*desc, sizeof(*desc));
880 
881  if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
882  ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
883  }
884  return;
885 }
886 
887 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
888  int port, int replace);
889 
890 /*! \pre instance is locked */
891 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
892 {
893  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
894  struct ice_wrap *ice;
895 
896  ice = rtp->ice;
897  rtp->ice = NULL;
898  if (ice) {
899  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
900  ao2_unlock(instance);
901  ao2_ref(ice, -1);
902  ao2_lock(instance);
903  ast_debug_ice(2, "(%p) ICE stopped\n", instance);
904  }
905 }
906 
907 /*!
908  * \brief ao2 ICE wrapper object destructor.
909  *
910  * \param vdoomed Object being destroyed.
911  *
912  * \note The associated struct ast_rtp_instance object must not
913  * be locked when unreffing the object. Otherwise we could
914  * deadlock trying to destroy the PJPROJECT ICE structure.
915  */
916 static void ice_wrap_dtor(void *vdoomed)
917 {
918  struct ice_wrap *ice = vdoomed;
919 
920  if (ice->real_ice) {
922 
923  pj_ice_sess_destroy(ice->real_ice);
924  }
925 }
926 
927 static void ast2pj_rtp_ice_role(enum ast_rtp_ice_role ast_role, enum pj_ice_sess_role *pj_role)
928 {
929  switch (ast_role) {
931  *pj_role = PJ_ICE_SESS_ROLE_CONTROLLED;
932  break;
934  *pj_role = PJ_ICE_SESS_ROLE_CONTROLLING;
935  break;
936  }
937 }
938 
939 static void pj2ast_rtp_ice_role(enum pj_ice_sess_role pj_role, enum ast_rtp_ice_role *ast_role)
940 {
941  switch (pj_role) {
942  case PJ_ICE_SESS_ROLE_CONTROLLED:
943  *ast_role = AST_RTP_ICE_ROLE_CONTROLLED;
944  return;
945  case PJ_ICE_SESS_ROLE_CONTROLLING:
946  *ast_role = AST_RTP_ICE_ROLE_CONTROLLING;
947  return;
948  case PJ_ICE_SESS_ROLE_UNKNOWN:
949  /* Don't change anything */
950  return;
951  default:
952  /* If we aren't explicitly handling something, it's a bug */
953  ast_assert(0);
954  return;
955  }
956 }
957 
958 /*! \pre instance is locked */
959 static int ice_reset_session(struct ast_rtp_instance *instance)
960 {
961  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
962  int res;
963 
964  ast_debug_ice(3, "(%p) ICE resetting\n", instance);
965  if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
966  ast_debug_ice(3, " (%p) ICE nevermind, not ready for a reset\n", instance);
967  return 0;
968  }
969 
970  ast_debug_ice(3, "(%p) ICE recreating ICE session %s (%d)\n",
971  instance, ast_sockaddr_stringify(&rtp->ice_original_rtp_addr), rtp->ice_port);
972  res = ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
973  if (!res) {
974  /* Use the current expected role for the ICE session */
975  enum pj_ice_sess_role role = PJ_ICE_SESS_ROLE_UNKNOWN;
976  ast2pj_rtp_ice_role(rtp->role, &role);
977  pj_ice_sess_change_role(rtp->ice->real_ice, role);
978  }
979 
980  /* If we only have one component now, and we previously set up TURN for RTCP,
981  * we need to destroy that TURN socket.
982  */
983  if (rtp->ice_num_components == 1 && rtp->turn_rtcp) {
984  struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
985  struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
986 
987  rtp->turn_state = PJ_TURN_STATE_NULL;
988 
989  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
990  ao2_unlock(instance);
991  pj_turn_sock_destroy(rtp->turn_rtcp);
992  ao2_lock(instance);
993  while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
994  ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
995  }
996  }
997 
998  rtp->ice_media_started = 0;
999 
1000  return res;
1001 }
1002 
1003 static int ice_candidates_compare(struct ao2_container *left, struct ao2_container *right)
1004 {
1005  struct ao2_iterator i;
1006  struct ast_rtp_engine_ice_candidate *right_candidate;
1007 
1008  if (ao2_container_count(left) != ao2_container_count(right)) {
1009  return -1;
1010  }
1011 
1012  i = ao2_iterator_init(right, 0);
1013  while ((right_candidate = ao2_iterator_next(&i))) {
1014  struct ast_rtp_engine_ice_candidate *left_candidate = ao2_find(left, right_candidate, OBJ_POINTER);
1015 
1016  if (!left_candidate) {
1017  ao2_ref(right_candidate, -1);
1019  return -1;
1020  }
1021 
1022  ao2_ref(left_candidate, -1);
1023  ao2_ref(right_candidate, -1);
1024  }
1026 
1027  return 0;
1028 }
1029 
1030 /*! \pre instance is locked */
1031 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
1032 {
1033  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1034  pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
1035  pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
1036  struct ao2_iterator i;
1037  struct ast_rtp_engine_ice_candidate *candidate;
1038  int cand_cnt = 0, has_rtp = 0, has_rtcp = 0;
1039 
1040  if (!rtp->ice || !rtp->ice_proposed_remote_candidates) {
1041  return;
1042  }
1043 
1044  /* Check for equivalence in the lists */
1045  if (rtp->ice_active_remote_candidates &&
1046  !ice_candidates_compare(rtp->ice_proposed_remote_candidates, rtp->ice_active_remote_candidates)) {
1047  ast_debug_ice(2, "(%p) ICE proposed equals active candidates\n", instance);
1048  ao2_cleanup(rtp->ice_proposed_remote_candidates);
1049  rtp->ice_proposed_remote_candidates = NULL;
1050  /* If this ICE session is being preserved then go back to the role it currently is */
1051  pj2ast_rtp_ice_role(rtp->ice->real_ice->role, &rtp->role);
1052  return;
1053  }
1054 
1055  /* Out with the old, in with the new */
1056  ao2_cleanup(rtp->ice_active_remote_candidates);
1057  rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates;
1058  rtp->ice_proposed_remote_candidates = NULL;
1059 
1060  ast_debug_ice(2, "(%p) ICE start\n", instance);
1061 
1062  /* Reset the ICE session. Is this going to work? */
1063  if (ice_reset_session(instance)) {
1064  ast_log(LOG_NOTICE, "(%p) ICE failed to create replacement session\n", instance);
1065  return;
1066  }
1067 
1069 
1070  i = ao2_iterator_init(rtp->ice_active_remote_candidates, 0);
1071 
1072  while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
1073  pj_str_t address;
1074 
1075  /* there needs to be at least one rtp and rtcp candidate in the list */
1076  has_rtp |= candidate->id == AST_RTP_ICE_COMPONENT_RTP;
1077  has_rtcp |= candidate->id == AST_RTP_ICE_COMPONENT_RTCP;
1078 
1079  pj_strdup2(rtp->ice->real_ice->pool, &candidates[cand_cnt].foundation,
1080  candidate->foundation);
1081  candidates[cand_cnt].comp_id = candidate->id;
1082  candidates[cand_cnt].prio = candidate->priority;
1083 
1084  pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
1085 
1086  if (!ast_sockaddr_isnull(&candidate->relay_address)) {
1087  pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
1088  }
1089 
1090  if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
1091  candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
1092  } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
1093  candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
1094  } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
1095  candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
1096  }
1097 
1098  if (candidate->id == AST_RTP_ICE_COMPONENT_RTP && rtp->turn_rtp) {
1099  ast_debug_ice(2, "(%p) ICE RTP candidate %s\n", instance, ast_sockaddr_stringify(&candidate->address));
1100  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1101  ao2_unlock(instance);
1102  pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
1103  ao2_lock(instance);
1104  } else if (candidate->id == AST_RTP_ICE_COMPONENT_RTCP && rtp->turn_rtcp) {
1105  ast_debug_ice(2, "(%p) ICE RTCP candidate %s\n", instance, ast_sockaddr_stringify(&candidate->address));
1106  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1107  ao2_unlock(instance);
1108  pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
1109  ao2_lock(instance);
1110  }
1111 
1112  cand_cnt++;
1113  ao2_ref(candidate, -1);
1114  }
1115 
1117 
1118  if (cand_cnt < ao2_container_count(rtp->ice_active_remote_candidates)) {
1119  ast_log(LOG_WARNING, "(%p) ICE lost %d candidates. Consider increasing PJ_ICE_MAX_CAND in PJSIP\n",
1120  instance, ao2_container_count(rtp->ice_active_remote_candidates) - cand_cnt);
1121  }
1122 
1123  if (!has_rtp) {
1124  ast_log(LOG_WARNING, "(%p) ICE no RTP candidates; skipping checklist\n", instance);
1125  }
1126 
1127  /* If we're only dealing with one ICE component, then we don't care about the lack of RTCP candidates */
1128  if (!has_rtcp && rtp->ice_num_components > 1) {
1129  ast_log(LOG_WARNING, "(%p) ICE no RTCP candidates; skipping checklist\n", instance);
1130  }
1131 
1132  if (rtp->ice && has_rtp && (has_rtcp || rtp->ice_num_components == 1)) {
1133  pj_status_t res;
1134  char reason[80];
1135  struct ice_wrap *ice;
1136 
1137  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1138  ice = rtp->ice;
1139  ao2_ref(ice, +1);
1140  ao2_unlock(instance);
1141  res = pj_ice_sess_create_check_list(ice->real_ice, &ufrag, &passwd, cand_cnt, &candidates[0]);
1142  if (res == PJ_SUCCESS) {
1143  ast_debug_ice(2, "(%p) ICE successfully created checklist\n", instance);
1144  ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: SUCCESS");
1145  pj_ice_sess_start_check(ice->real_ice);
1146  pj_timer_heap_poll(timer_heap, NULL);
1147  ao2_ref(ice, -1);
1148  ao2_lock(instance);
1149  rtp->strict_rtp_state = STRICT_RTP_OPEN;
1150  return;
1151  }
1152  ao2_ref(ice, -1);
1153  ao2_lock(instance);
1154 
1155  pj_strerror(res, reason, sizeof(reason));
1156  ast_log(LOG_WARNING, "(%p) ICE failed to create session check list: %s\n", instance, reason);
1157  }
1158 
1159  ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: FAILURE");
1160 
1161  /* even though create check list failed don't stop ice as
1162  it might still work */
1163  /* however we do need to reset remote candidates since
1164  this function may be re-entered */
1165  ao2_ref(rtp->ice_active_remote_candidates, -1);
1166  rtp->ice_active_remote_candidates = NULL;
1167  if (rtp->ice) {
1168  rtp->ice->real_ice->rcand_cnt = rtp->ice->real_ice->clist.count = 0;
1169  }
1170 }
1171 
1172 /*! \pre instance is locked */
1173 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
1174 {
1175  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1176 
1177  return rtp->local_ufrag;
1178 }
1179 
1180 /*! \pre instance is locked */
1181 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
1182 {
1183  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1184 
1185  return rtp->local_passwd;
1186 }
1187 
1188 /*! \pre instance is locked */
1190 {
1191  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1192 
1193  if (rtp->ice_local_candidates) {
1194  ao2_ref(rtp->ice_local_candidates, +1);
1195  }
1196 
1197  return rtp->ice_local_candidates;
1198 }
1199 
1200 /*! \pre instance is locked */
1201 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
1202 {
1203  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1204 
1205  if (!rtp->ice) {
1206  return;
1207  }
1208 
1210 
1211  pj_ice_sess_change_role(rtp->ice->real_ice, PJ_ICE_SESS_ROLE_CONTROLLING);
1212 }
1213 
1214 /*! \pre instance is locked */
1215 static void ast_rtp_ice_set_role(struct ast_rtp_instance *instance, enum ast_rtp_ice_role role)
1216 {
1217  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1218 
1219  if (!rtp->ice) {
1220  ast_debug_ice(3, "(%p) ICE set role failed; no ice instance\n", instance);
1221  return;
1222  }
1223 
1224  rtp->role = role;
1225 
1226  if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
1228  ast_debug_ice(2, "(%p) ICE set role to %s\n",
1229  instance, role == AST_RTP_ICE_ROLE_CONTROLLED ? "CONTROLLED" : "CONTROLLING");
1230  pj_ice_sess_change_role(rtp->ice->real_ice, role == AST_RTP_ICE_ROLE_CONTROLLED ?
1231  PJ_ICE_SESS_ROLE_CONTROLLED : PJ_ICE_SESS_ROLE_CONTROLLING);
1232  } else {
1233  ast_debug_ice(2, "(%p) ICE not setting role because state is %s\n",
1234  instance, rtp->ice->real_ice->is_nominating ? "nominating" : "complete");
1235  }
1236 }
1237 
1238 /*! \pre instance is locked */
1239 static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_rtp *rtp,
1240  unsigned comp_id, unsigned transport_id, pj_ice_cand_type type, pj_uint16_t local_pref,
1241  const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr,
1242  int addr_len)
1243 {
1244  pj_str_t foundation;
1245  struct ast_rtp_engine_ice_candidate *candidate, *existing;
1246  struct ice_wrap *ice;
1247  char address[PJ_INET6_ADDRSTRLEN];
1248  pj_status_t status;
1249 
1250  if (!rtp->ice) {
1251  return;
1252  }
1253 
1255 
1256  pj_ice_calc_foundation(rtp->ice->real_ice->pool, &foundation, type, addr);
1257 
1258  if (!rtp->ice_local_candidates) {
1259  rtp->ice_local_candidates = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1261  if (!rtp->ice_local_candidates) {
1262  return;
1263  }
1264  }
1265 
1266  if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
1267  return;
1268  }
1269 
1270  candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
1271  candidate->id = comp_id;
1272  candidate->transport = ast_strdup("UDP");
1273 
1274  ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
1275  ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
1276 
1277  if (rel_addr) {
1278  ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
1279  ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
1280  }
1281 
1282  if (type == PJ_ICE_CAND_TYPE_HOST) {
1284  } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
1286  } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
1288  }
1289 
1290  if ((existing = ao2_find(rtp->ice_local_candidates, candidate, OBJ_POINTER))) {
1291  ao2_ref(existing, -1);
1292  ao2_ref(candidate, -1);
1293  return;
1294  }
1295 
1296  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1297  ice = rtp->ice;
1298  ao2_ref(ice, +1);
1299  ao2_unlock(instance);
1300  status = pj_ice_sess_add_cand(ice->real_ice, comp_id, transport_id, type, local_pref,
1301  &foundation, addr, base_addr, rel_addr, addr_len, NULL);
1302  ao2_ref(ice, -1);
1303  ao2_lock(instance);
1304  if (!rtp->ice || status != PJ_SUCCESS) {
1305  ast_debug_ice(2, "(%p) ICE unable to add candidate: %s, %d\n", instance, ast_sockaddr_stringify(
1306  &candidate->address), candidate->priority);
1307  ao2_ref(candidate, -1);
1308  return;
1309  }
1310 
1311  /* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
1312  candidate->priority = rtp->ice->real_ice->lcand[rtp->ice->real_ice->lcand_cnt - 1].prio;
1313 
1314  ast_debug_ice(2, "(%p) ICE add candidate: %s, %d\n", instance, ast_sockaddr_stringify(
1315  &candidate->address), candidate->priority);
1316 
1317  ao2_link(rtp->ice_local_candidates, candidate);
1318  ao2_ref(candidate, -1);
1319 }
1320 
1321 /* PJPROJECT TURN callback */
1322 static void ast_rtp_on_turn_rx_rtp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1323 {
1324  struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1325  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1326  struct ice_wrap *ice;
1327  pj_status_t status;
1328 
1329  ao2_lock(instance);
1330  ice = ao2_bump(rtp->ice);
1331  ao2_unlock(instance);
1332 
1333  if (ice) {
1334  status = pj_ice_sess_on_rx_pkt(ice->real_ice, AST_RTP_ICE_COMPONENT_RTP,
1335  TRANSPORT_TURN_RTP, pkt, pkt_len, peer_addr, addr_len);
1336  ao2_ref(ice, -1);
1337  if (status != PJ_SUCCESS) {
1338  char buf[100];
1339 
1340  pj_strerror(status, buf, sizeof(buf));
1341  ast_log(LOG_WARNING, "(%p) ICE PJ Rx error status code: %d '%s'.\n",
1342  instance, (int)status, buf);
1343  return;
1344  }
1345  if (!rtp->rtp_passthrough) {
1346  return;
1347  }
1348  rtp->rtp_passthrough = 0;
1349  }
1350 
1351  ast_sendto(rtp->s, pkt, pkt_len, 0, &rtp->rtp_loop);
1352 }
1353 
1354 /* PJPROJECT TURN callback */
1355 static void ast_rtp_on_turn_rtp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1356 {
1357  struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1358  struct ast_rtp *rtp;
1359 
1360  /* If this is a leftover from an already notified RTP instance just ignore the state change */
1361  if (!instance) {
1362  return;
1363  }
1364 
1365  rtp = ast_rtp_instance_get_data(instance);
1366 
1367  ao2_lock(instance);
1368 
1369  /* We store the new state so the other thread can actually handle it */
1370  rtp->turn_state = new_state;
1371  ast_cond_signal(&rtp->cond);
1372 
1373  if (new_state == PJ_TURN_STATE_DESTROYING) {
1374  pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
1375  rtp->turn_rtp = NULL;
1376  }
1377 
1378  ao2_unlock(instance);
1379 }
1380 
1381 /* RTP TURN Socket interface declaration */
1382 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
1383  .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
1384  .on_state = ast_rtp_on_turn_rtp_state,
1385 };
1386 
1387 /* PJPROJECT TURN callback */
1388 static void ast_rtp_on_turn_rx_rtcp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1389 {
1390  struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1391  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1392  struct ice_wrap *ice;
1393  pj_status_t status;
1394 
1395  ao2_lock(instance);
1396  ice = ao2_bump(rtp->ice);
1397  ao2_unlock(instance);
1398 
1399  if (ice) {
1400  status = pj_ice_sess_on_rx_pkt(ice->real_ice, AST_RTP_ICE_COMPONENT_RTCP,
1401  TRANSPORT_TURN_RTCP, pkt, pkt_len, peer_addr, addr_len);
1402  ao2_ref(ice, -1);
1403  if (status != PJ_SUCCESS) {
1404  char buf[100];
1405 
1406  pj_strerror(status, buf, sizeof(buf));
1407  ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
1408  (int)status, buf);
1409  return;
1410  }
1411  if (!rtp->rtcp_passthrough) {
1412  return;
1413  }
1414  rtp->rtcp_passthrough = 0;
1415  }
1416 
1417  ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp_loop);
1418 }
1419 
1420 /* PJPROJECT TURN callback */
1421 static void ast_rtp_on_turn_rtcp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1422 {
1423  struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1424  struct ast_rtp *rtp;
1425 
1426  /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1427  if (!instance) {
1428  return;
1429  }
1430 
1431  rtp = ast_rtp_instance_get_data(instance);
1432 
1433  ao2_lock(instance);
1434 
1435  /* We store the new state so the other thread can actually handle it */
1436  rtp->turn_state = new_state;
1437  ast_cond_signal(&rtp->cond);
1438 
1439  if (new_state == PJ_TURN_STATE_DESTROYING) {
1440  pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
1441  rtp->turn_rtcp = NULL;
1442  }
1443 
1444  ao2_unlock(instance);
1445 }
1446 
1447 /* RTCP TURN Socket interface declaration */
1448 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
1449  .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
1450  .on_state = ast_rtp_on_turn_rtcp_state,
1451 };
1452 
1453 /*! \brief Worker thread for ioqueue and timerheap */
1454 static int ioqueue_worker_thread(void *data)
1455 {
1456  struct ast_rtp_ioqueue_thread *ioqueue = data;
1457 
1458  while (!ioqueue->terminate) {
1459  const pj_time_val delay = {0, 10};
1460 
1461  pj_ioqueue_poll(ioqueue->ioqueue, &delay);
1462 
1463  pj_timer_heap_poll(ioqueue->timerheap, NULL);
1464  }
1465 
1466  return 0;
1467 }
1468 
1469 /*! \brief Destroyer for ioqueue thread */
1471 {
1472  if (ioqueue->thread) {
1473  ioqueue->terminate = 1;
1474  pj_thread_join(ioqueue->thread);
1475  pj_thread_destroy(ioqueue->thread);
1476  }
1477 
1478  if (ioqueue->pool) {
1479  /* This mimics the behavior of pj_pool_safe_release
1480  * which was introduced in pjproject 2.6.
1481  */
1482  pj_pool_t *temp_pool = ioqueue->pool;
1483 
1484  ioqueue->pool = NULL;
1485  pj_pool_release(temp_pool);
1486  }
1487 
1488  ast_free(ioqueue);
1489 }
1490 
1491 /*! \brief Removal function for ioqueue thread, determines if it should be terminated and destroyed */
1493 {
1494  int destroy = 0;
1495 
1496  /* If nothing is using this ioqueue thread destroy it */
1498  if ((ioqueue->count - 2) == 0) {
1499  destroy = 1;
1500  AST_LIST_REMOVE(&ioqueues, ioqueue, next);
1501  }
1503 
1504  if (!destroy) {
1505  return;
1506  }
1507 
1508  rtp_ioqueue_thread_destroy(ioqueue);
1509 }
1510 
1511 /*! \brief Finder and allocator for an ioqueue thread */
1513 {
1515  pj_lock_t *lock;
1516 
1518 
1519  /* See if an ioqueue thread exists that can handle more */
1520  AST_LIST_TRAVERSE(&ioqueues, ioqueue, next) {
1521  if ((ioqueue->count + 2) < PJ_IOQUEUE_MAX_HANDLES) {
1522  break;
1523  }
1524  }
1525 
1526  /* If we found one bump it up and return it */
1527  if (ioqueue) {
1528  ioqueue->count += 2;
1529  goto end;
1530  }
1531 
1532  ioqueue = ast_calloc(1, sizeof(*ioqueue));
1533  if (!ioqueue) {
1534  goto end;
1535  }
1536 
1537  ioqueue->pool = pj_pool_create(&cachingpool.factory, "rtp", 512, 512, NULL);
1538 
1539  /* We use a timer on the ioqueue thread for TURN so that two threads aren't operating
1540  * on a session at the same time
1541  */
1542  if (pj_timer_heap_create(ioqueue->pool, 4, &ioqueue->timerheap) != PJ_SUCCESS) {
1543  goto fatal;
1544  }
1545 
1546  if (pj_lock_create_recursive_mutex(ioqueue->pool, "rtp%p", &lock) != PJ_SUCCESS) {
1547  goto fatal;
1548  }
1549 
1550  pj_timer_heap_set_lock(ioqueue->timerheap, lock, PJ_TRUE);
1551 
1552  if (pj_ioqueue_create(ioqueue->pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue->ioqueue) != PJ_SUCCESS) {
1553  goto fatal;
1554  }
1555 
1556  if (pj_thread_create(ioqueue->pool, "ice", &ioqueue_worker_thread, ioqueue, 0, 0, &ioqueue->thread) != PJ_SUCCESS) {
1557  goto fatal;
1558  }
1559 
1560  AST_LIST_INSERT_HEAD(&ioqueues, ioqueue, next);
1561 
1562  /* Since this is being returned to an active session the count always starts at 2 */
1563  ioqueue->count = 2;
1564 
1565  goto end;
1566 
1567 fatal:
1568  rtp_ioqueue_thread_destroy(ioqueue);
1569  ioqueue = NULL;
1570 
1571 end:
1573  return ioqueue;
1574 }
1575 
1576 /*! \pre instance is locked */
1577 static void ast_rtp_ice_turn_request(struct ast_rtp_instance *instance, enum ast_rtp_ice_component_type component,
1578  enum ast_transport transport, const char *server, unsigned int port, const char *username, const char *password)
1579 {
1580  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1581  pj_turn_sock **turn_sock;
1582  const pj_turn_sock_cb *turn_cb;
1583  pj_turn_tp_type conn_type;
1584  int conn_transport;
1585  pj_stun_auth_cred cred = { 0, };
1586  pj_str_t turn_addr;
1587  struct ast_sockaddr addr = { { 0, } };
1588  pj_stun_config stun_config;
1589  struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
1590  struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1591  pj_turn_session_info info;
1592  struct ast_sockaddr local, loop;
1593  pj_status_t status;
1594  pj_turn_sock_cfg turn_sock_cfg;
1595  struct ice_wrap *ice;
1596 
1597  ast_rtp_instance_get_local_address(instance, &local);
1598  if (ast_sockaddr_is_ipv4(&local)) {
1599  ast_sockaddr_parse(&loop, "127.0.0.1", PARSE_PORT_FORBID);
1600  } else {
1601  ast_sockaddr_parse(&loop, "::1", PARSE_PORT_FORBID);
1602  }
1603 
1604  /* Determine what component we are requesting a TURN session for */
1605  if (component == AST_RTP_ICE_COMPONENT_RTP) {
1606  turn_sock = &rtp->turn_rtp;
1607  turn_cb = &ast_rtp_turn_rtp_sock_cb;
1608  conn_transport = TRANSPORT_TURN_RTP;
1609  ast_sockaddr_set_port(&loop, ast_sockaddr_port(&local));
1610  } else if (component == AST_RTP_ICE_COMPONENT_RTCP) {
1611  turn_sock = &rtp->turn_rtcp;
1612  turn_cb = &ast_rtp_turn_rtcp_sock_cb;
1613  conn_transport = TRANSPORT_TURN_RTCP;
1614  ast_sockaddr_set_port(&loop, ast_sockaddr_port(&rtp->rtcp->us));
1615  } else {
1616  return;
1617  }
1618 
1619  if (transport == AST_TRANSPORT_UDP) {
1620  conn_type = PJ_TURN_TP_UDP;
1621  } else if (transport == AST_TRANSPORT_TCP) {
1622  conn_type = PJ_TURN_TP_TCP;
1623  } else {
1624  ast_assert(0);
1625  return;
1626  }
1627 
1628  ast_sockaddr_parse(&addr, server, PARSE_PORT_FORBID);
1629 
1630  if (*turn_sock) {
1631  rtp->turn_state = PJ_TURN_STATE_NULL;
1632 
1633  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1634  ao2_unlock(instance);
1635  pj_turn_sock_destroy(*turn_sock);
1636  ao2_lock(instance);
1637  while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
1638  ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
1639  }
1640  }
1641 
1642  if (component == AST_RTP_ICE_COMPONENT_RTP && !rtp->ioqueue) {
1643  /*
1644  * We cannot hold the instance lock because we could wait
1645  * for the ioqueue thread to die and we might deadlock as
1646  * a result.
1647  */
1648  ao2_unlock(instance);
1649  rtp->ioqueue = rtp_ioqueue_thread_get_or_create();
1650  ao2_lock(instance);
1651  if (!rtp->ioqueue) {
1652  return;
1653  }
1654  }
1655 
1656  pj_stun_config_init(&stun_config, &cachingpool.factory, 0, rtp->ioqueue->ioqueue, rtp->ioqueue->timerheap);
1657  if (!stun_software_attribute) {
1658  stun_config.software_name = pj_str(NULL);
1659  }
1660 
1661  /* Use ICE session group lock for TURN session to avoid deadlock */
1662  pj_turn_sock_cfg_default(&turn_sock_cfg);
1663  ice = rtp->ice;
1664  if (ice) {
1665  turn_sock_cfg.grp_lock = ice->real_ice->grp_lock;
1666  ao2_ref(ice, +1);
1667  }
1668 
1669  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
1670  ao2_unlock(instance);
1671  status = pj_turn_sock_create(&stun_config,
1672  ast_sockaddr_is_ipv4(&addr) ? pj_AF_INET() : pj_AF_INET6(), conn_type,
1673  turn_cb, &turn_sock_cfg, instance, turn_sock);
1674  ao2_cleanup(ice);
1675  if (status != PJ_SUCCESS) {
1676  ast_log(LOG_WARNING, "(%p) Could not create a TURN client socket\n", instance);
1677  ao2_lock(instance);
1678  return;
1679  }
1680 
1681  cred.type = PJ_STUN_AUTH_CRED_STATIC;
1682  pj_strset2(&cred.data.static_cred.username, (char*)username);
1683  cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1684  pj_strset2(&cred.data.static_cred.data, (char*)password);
1685 
1686  pj_turn_sock_alloc(*turn_sock, pj_cstr(&turn_addr, server), port, NULL, &cred, NULL);
1687 
1688  ast_debug_ice(2, "(%p) ICE request TURN %s %s candidate\n", instance,
1689  transport == AST_TRANSPORT_UDP ? "UDP" : "TCP",
1690  component == AST_RTP_ICE_COMPONENT_RTP ? "RTP" : "RTCP");
1691 
1692  ao2_lock(instance);
1693 
1694  /*
1695  * Because the TURN socket is asynchronous and we are synchronous we need to
1696  * wait until it is done
1697  */
1698  while (rtp->turn_state < PJ_TURN_STATE_READY) {
1699  ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
1700  }
1701 
1702  /* If a TURN session was allocated add it as a candidate */
1703  if (rtp->turn_state != PJ_TURN_STATE_READY) {
1704  return;
1705  }
1706 
1707  pj_turn_sock_get_info(*turn_sock, &info);
1708 
1709  ast_rtp_ice_add_cand(instance, rtp, component, conn_transport,
1710  PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
1711  &info.mapped_addr, pj_sockaddr_get_len(&info.relay_addr));
1712 
1713  if (component == AST_RTP_ICE_COMPONENT_RTP) {
1714  ast_sockaddr_copy(&rtp->rtp_loop, &loop);
1715  } else if (component == AST_RTP_ICE_COMPONENT_RTCP) {
1716  ast_sockaddr_copy(&rtp->rtcp_loop, &loop);
1717  }
1718 }
1719 
1720 static char *generate_random_string(char *buf, size_t size)
1721 {
1722  long val[4];
1723  int x;
1724 
1725  for (x=0; x<4; x++) {
1726  val[x] = ast_random();
1727  }
1728  snprintf(buf, size, "%08lx%08lx%08lx%08lx", (long unsigned)val[0], (long unsigned)val[1], (long unsigned)val[2], (long unsigned)val[3]);
1729 
1730  return buf;
1731 }
1732 
1733 /*! \pre instance is locked */
1734 static void ast_rtp_ice_change_components(struct ast_rtp_instance *instance, int num_components)
1735 {
1736  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1737 
1738  /* Don't do anything if ICE is unsupported or if we're not changing the
1739  * number of components
1740  */
1741  if (!icesupport || !rtp->ice || rtp->ice_num_components == num_components) {
1742  return;
1743  }
1744 
1745  ast_debug_ice(2, "(%p) ICE change number of components %u -> %u\n", instance,
1746  rtp->ice_num_components, num_components);
1747 
1748  rtp->ice_num_components = num_components;
1749  ice_reset_session(instance);
1750 }
1751 
1752 /* ICE RTP Engine interface declaration */
1755  .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
1756  .start = ast_rtp_ice_start,
1757  .stop = ast_rtp_ice_stop,
1758  .get_ufrag = ast_rtp_ice_get_ufrag,
1759  .get_password = ast_rtp_ice_get_password,
1760  .get_local_candidates = ast_rtp_ice_get_local_candidates,
1761  .ice_lite = ast_rtp_ice_lite,
1762  .set_role = ast_rtp_ice_set_role,
1763  .turn_request = ast_rtp_ice_turn_request,
1764  .change_components = ast_rtp_ice_change_components,
1765 };
1766 #endif
1767 
1768 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
1769 static int dtls_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
1770 {
1771  /* We don't want to actually verify the certificate so just accept what they have provided */
1772  return 1;
1773 }
1774 
1775 static int dtls_details_initialize(struct dtls_details *dtls, SSL_CTX *ssl_ctx,
1776  enum ast_rtp_dtls_setup setup, struct ast_rtp_instance *instance)
1777 {
1778  dtls->dtls_setup = setup;
1779 
1780  if (!(dtls->ssl = SSL_new(ssl_ctx))) {
1781  ast_log(LOG_ERROR, "Failed to allocate memory for SSL\n");
1782  goto error;
1783  }
1784 
1785  if (!(dtls->read_bio = BIO_new(BIO_s_mem()))) {
1786  ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic\n");
1787  goto error;
1788  }
1789  BIO_set_mem_eof_return(dtls->read_bio, -1);
1790 
1791 #ifdef HAVE_OPENSSL_BIO_METHOD
1792  if (!(dtls->write_bio = BIO_new(dtls_bio_methods))) {
1793  ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic\n");
1794  goto error;
1795  }
1796 
1797  BIO_set_data(dtls->write_bio, instance);
1798 #else
1799  if (!(dtls->write_bio = BIO_new(&dtls_bio_methods))) {
1800  ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic\n");
1801  goto error;
1802  }
1803  dtls->write_bio->ptr = instance;
1804 #endif
1805  SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio);
1806 
1807  if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
1808  SSL_set_accept_state(dtls->ssl);
1809  } else {
1810  SSL_set_connect_state(dtls->ssl);
1811  }
1812  dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
1813 
1814  return 0;
1815 
1816 error:
1817  if (dtls->read_bio) {
1818  BIO_free(dtls->read_bio);
1819  dtls->read_bio = NULL;
1820  }
1821 
1822  if (dtls->write_bio) {
1823  BIO_free(dtls->write_bio);
1824  dtls->write_bio = NULL;
1825  }
1826 
1827  if (dtls->ssl) {
1828  SSL_free(dtls->ssl);
1829  dtls->ssl = NULL;
1830  }
1831  return -1;
1832 }
1833 
1834 static int dtls_setup_rtcp(struct ast_rtp_instance *instance)
1835 {
1836  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1837 
1838  if (!rtp->ssl_ctx || !rtp->rtcp) {
1839  return 0;
1840  }
1841 
1842  ast_debug_dtls(3, "(%p) DTLS RTCP setup\n", instance);
1843  return dtls_details_initialize(&rtp->rtcp->dtls, rtp->ssl_ctx, rtp->dtls.dtls_setup, instance);
1844 }
1845 
1846 static const SSL_METHOD *get_dtls_method(void)
1847 {
1848 #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
1849  return DTLSv1_method();
1850 #else
1851  return DTLS_method();
1852 #endif
1853 }
1854 
1855 struct dtls_cert_info {
1856  EVP_PKEY *private_key;
1857  X509 *certificate;
1858 };
1859 
1860 static void configure_dhparams(const struct ast_rtp *rtp, const struct ast_rtp_dtls_cfg *dtls_cfg)
1861 {
1862 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
1863  EC_KEY *ecdh;
1864 #endif
1865 
1866 #ifndef OPENSSL_NO_DH
1867  if (!ast_strlen_zero(dtls_cfg->pvtfile)) {
1868  BIO *bio = BIO_new_file(dtls_cfg->pvtfile, "r");
1869  if (bio) {
1870  DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1871  if (dh) {
1872  if (SSL_CTX_set_tmp_dh(rtp->ssl_ctx, dh)) {
1873  long options = SSL_OP_CIPHER_SERVER_PREFERENCE |
1874  SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE;
1875  options = SSL_CTX_set_options(rtp->ssl_ctx, options);
1876  ast_verb(2, "DTLS DH initialized, PFS enabled\n");
1877  }
1878  DH_free(dh);
1879  }
1880  BIO_free(bio);
1881  }
1882  }
1883 #endif /* !OPENSSL_NO_DH */
1884 
1885 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
1886  /* enables AES-128 ciphers, to get AES-256 use NID_secp384r1 */
1887  ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1888  if (ecdh) {
1889  if (SSL_CTX_set_tmp_ecdh(rtp->ssl_ctx, ecdh)) {
1890  #ifndef SSL_CTRL_SET_ECDH_AUTO
1891  #define SSL_CTRL_SET_ECDH_AUTO 94
1892  #endif
1893  /* SSL_CTX_set_ecdh_auto(rtp->ssl_ctx, on); requires OpenSSL 1.0.2 which wraps: */
1894  if (SSL_CTX_ctrl(rtp->ssl_ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL)) {
1895  ast_verb(2, "DTLS ECDH initialized (automatic), faster PFS enabled\n");
1896  } else {
1897  ast_verb(2, "DTLS ECDH initialized (secp256r1), faster PFS enabled\n");
1898  }
1899  }
1900  EC_KEY_free(ecdh);
1901  }
1902 #endif /* !OPENSSL_NO_ECDH */
1903 }
1904 
1905 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L)
1906 
1907 static int create_ephemeral_ec_keypair(EVP_PKEY **keypair)
1908 {
1909  EC_KEY *eckey = NULL;
1910  EC_GROUP *group = NULL;
1911 
1912  group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
1913  if (!group) {
1914  goto error;
1915  }
1916 
1917  EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
1918  EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
1919 
1920  eckey = EC_KEY_new();
1921  if (!eckey) {
1922  goto error;
1923  }
1924 
1925  if (!EC_KEY_set_group(eckey, group)) {
1926  goto error;
1927  }
1928 
1929  if (!EC_KEY_generate_key(eckey)) {
1930  goto error;
1931  }
1932 
1933  *keypair = EVP_PKEY_new();
1934  if (!*keypair) {
1935  goto error;
1936  }
1937 
1938  EVP_PKEY_assign_EC_KEY(*keypair, eckey);
1939  EC_GROUP_free(group);
1940 
1941  return 0;
1942 
1943 error:
1944  EC_KEY_free(eckey);
1945  EC_GROUP_free(group);
1946 
1947  return -1;
1948 }
1949 
1950 /* From OpenSSL's x509 command */
1951 #define SERIAL_RAND_BITS 159
1952 
1953 static int create_ephemeral_certificate(EVP_PKEY *keypair, X509 **certificate)
1954 {
1955  X509 *cert = NULL;
1956  BIGNUM *serial = NULL;
1957  X509_NAME *name = NULL;
1958 
1959  cert = X509_new();
1960  if (!cert) {
1961  goto error;
1962  }
1963 
1964  if (!X509_set_version(cert, 2)) {
1965  goto error;
1966  }
1967 
1968  /* Set the public key */
1969  X509_set_pubkey(cert, keypair);
1970 
1971  /* Generate a random serial number */
1972  if (!(serial = BN_new())
1973  || !BN_rand(serial, SERIAL_RAND_BITS, -1, 0)
1974  || !BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert))) {
1975  goto error;
1976  }
1977 
1978  /*
1979  * Validity period - Current Chrome & Firefox make it 31 days starting
1980  * with yesterday at the current time, so we will do the same.
1981  */
1982 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1983  if (!X509_time_adj_ex(X509_get_notBefore(cert), -1, 0, NULL)
1984  || !X509_time_adj_ex(X509_get_notAfter(cert), 30, 0, NULL)) {
1985  goto error;
1986  }
1987 #else
1988  if (!X509_time_adj_ex(X509_getm_notBefore(cert), -1, 0, NULL)
1989  || !X509_time_adj_ex(X509_getm_notAfter(cert), 30, 0, NULL)) {
1990  goto error;
1991  }
1992 #endif
1993 
1994  /* Set the name and issuer */
1995  if (!(name = X509_get_subject_name(cert))
1996  || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
1997  (unsigned char *) "asterisk", -1, -1, 0)
1998  || !X509_set_issuer_name(cert, name)) {
1999  goto error;
2000  }
2001 
2002  /* Sign it */
2003  if (!X509_sign(cert, keypair, EVP_sha256())) {
2004  goto error;
2005  }
2006 
2007  *certificate = cert;
2008 
2009  return 0;
2010 
2011 error:
2012  BN_free(serial);
2013  X509_free(cert);
2014 
2015  return -1;
2016 }
2017 
2018 static int create_certificate_ephemeral(struct ast_rtp_instance *instance,
2019  const struct ast_rtp_dtls_cfg *dtls_cfg,
2020  struct dtls_cert_info *cert_info)
2021 {
2022  /* Make sure these are initialized */
2023  cert_info->private_key = NULL;
2024  cert_info->certificate = NULL;
2025 
2026  if (create_ephemeral_ec_keypair(&cert_info->private_key)) {
2027  ast_log(LOG_ERROR, "Failed to create ephemeral ECDSA keypair\n");
2028  goto error;
2029  }
2030 
2031  if (create_ephemeral_certificate(cert_info->private_key, &cert_info->certificate)) {
2032  ast_log(LOG_ERROR, "Failed to create ephemeral X509 certificate\n");
2033  goto error;
2034  }
2035 
2036  return 0;
2037 
2038  error:
2039  X509_free(cert_info->certificate);
2040  EVP_PKEY_free(cert_info->private_key);
2041 
2042  return -1;
2043 }
2044 
2045 #else
2046 
2047 static int create_certificate_ephemeral(struct ast_rtp_instance *instance,
2048  const struct ast_rtp_dtls_cfg *dtls_cfg,
2049  struct dtls_cert_info *cert_info)
2050 {
2051  ast_log(LOG_ERROR, "Your version of OpenSSL does not support ECDSA keys\n");
2052  return -1;
2053 }
2054 
2055 #endif /* !OPENSSL_NO_ECDH */
2056 
2057 static int create_certificate_from_file(struct ast_rtp_instance *instance,
2058  const struct ast_rtp_dtls_cfg *dtls_cfg,
2059  struct dtls_cert_info *cert_info)
2060 {
2061  FILE *fp;
2062  BIO *certbio = NULL;
2063  EVP_PKEY *private_key = NULL;
2064  X509 *cert = NULL;
2065  char *private_key_file = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
2066 
2067  fp = fopen(private_key_file, "r");
2068  if (!fp) {
2069  ast_log(LOG_ERROR, "Failed to read private key from file '%s': %s\n", private_key_file, strerror(errno));
2070  goto error;
2071  }
2072 
2073  if (!PEM_read_PrivateKey(fp, &private_key, NULL, NULL)) {
2074  ast_log(LOG_ERROR, "Failed to read private key from PEM file '%s'\n", private_key_file);
2075  fclose(fp);
2076  goto error;
2077  }
2078 
2079  if (fclose(fp)) {
2080  ast_log(LOG_ERROR, "Failed to close private key file '%s': %s\n", private_key_file, strerror(errno));
2081  goto error;
2082  }
2083 
2084  certbio = BIO_new(BIO_s_file());
2085  if (!certbio) {
2086  ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
2087  instance);
2088  goto error;
2089  }
2090 
2091  if (!BIO_read_filename(certbio, dtls_cfg->certfile)
2092  || !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
2093  ast_log(LOG_ERROR, "Failed to read certificate from file '%s'\n", dtls_cfg->certfile);
2094  goto error;
2095  }
2096 
2097  cert_info->private_key = private_key;
2098  cert_info->certificate = cert;
2099 
2100  BIO_free_all(certbio);
2101 
2102  return 0;
2103 
2104 error:
2105  X509_free(cert);
2106  BIO_free_all(certbio);
2107  EVP_PKEY_free(private_key);
2108 
2109  return -1;
2110 }
2111 
2112 static int load_dtls_certificate(struct ast_rtp_instance *instance,
2113  const struct ast_rtp_dtls_cfg *dtls_cfg,
2114  struct dtls_cert_info *cert_info)
2115 {
2116  if (dtls_cfg->ephemeral_cert) {
2117  return create_certificate_ephemeral(instance, dtls_cfg, cert_info);
2118  } else if (!ast_strlen_zero(dtls_cfg->certfile)) {
2119  return create_certificate_from_file(instance, dtls_cfg, cert_info);
2120  } else {
2121  return -1;
2122  }
2123 }
2124 
2125 /*! \pre instance is locked */
2126 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
2127 {
2128  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2129  struct dtls_cert_info cert_info = { 0 };
2130  int res;
2131 
2132  if (!dtls_cfg->enabled) {
2133  return 0;
2134  }
2135 
2136  ast_debug_dtls(3, "(%p) DTLS RTP setup\n", instance);
2137 
2139  ast_log(LOG_ERROR, "SRTP support module is not loaded or available. Try loading res_srtp.so.\n");
2140  return -1;
2141  }
2142 
2143  if (rtp->ssl_ctx) {
2144  return 0;
2145  }
2146 
2147  rtp->ssl_ctx = SSL_CTX_new(get_dtls_method());
2148  if (!rtp->ssl_ctx) {
2149  return -1;
2150  }
2151 
2152  SSL_CTX_set_read_ahead(rtp->ssl_ctx, 1);
2153 
2154  configure_dhparams(rtp, dtls_cfg);
2155 
2156  rtp->dtls_verify = dtls_cfg->verify;
2157 
2158  SSL_CTX_set_verify(rtp->ssl_ctx, (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) || (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ?
2159  SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, !(rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ?
2160  dtls_verify_callback : NULL);
2161 
2162  if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
2163  SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
2164  } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
2165  SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
2166  } else {
2167  ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
2168  return -1;
2169  }
2170 
2171  rtp->local_hash = dtls_cfg->hash;
2172 
2173  if (!load_dtls_certificate(instance, dtls_cfg, &cert_info)) {
2174  const EVP_MD *type;
2175  unsigned int size, i;
2176  unsigned char fingerprint[EVP_MAX_MD_SIZE];
2177  char *local_fingerprint = rtp->local_fingerprint;
2178 
2179  if (!SSL_CTX_use_certificate(rtp->ssl_ctx, cert_info.certificate)) {
2180  ast_log(LOG_ERROR, "Specified certificate for RTP instance '%p' could not be used\n",
2181  instance);
2182  return -1;
2183  }
2184 
2185  if (!SSL_CTX_use_PrivateKey(rtp->ssl_ctx, cert_info.private_key)
2186  || !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
2187  ast_log(LOG_ERROR, "Specified private key for RTP instance '%p' could not be used\n",
2188  instance);
2189  return -1;
2190  }
2191 
2192  if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA1) {
2193  type = EVP_sha1();
2194  } else if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA256) {
2195  type = EVP_sha256();
2196  } else {
2197  ast_log(LOG_ERROR, "Unsupported fingerprint hash type on RTP instance '%p'\n",
2198  instance);
2199  return -1;
2200  }
2201 
2202  if (!X509_digest(cert_info.certificate, type, fingerprint, &size) || !size) {
2203  ast_log(LOG_ERROR, "Could not produce fingerprint from certificate for RTP instance '%p'\n",
2204  instance);
2205  return -1;
2206  }
2207 
2208  for (i = 0; i < size; i++) {
2209  sprintf(local_fingerprint, "%02hhX:", fingerprint[i]);
2210  local_fingerprint += 3;
2211  }
2212 
2213  *(local_fingerprint - 1) = 0;
2214 
2215  EVP_PKEY_free(cert_info.private_key);
2216  X509_free(cert_info.certificate);
2217  }
2218 
2219  if (!ast_strlen_zero(dtls_cfg->cipher)) {
2220  if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
2221  ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
2222  dtls_cfg->cipher, instance);
2223  return -1;
2224  }
2225  }
2226 
2227  if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
2228  if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
2229  ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
2230  S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
2231  return -1;
2232  }
2233  }
2234 
2235  rtp->rekey = dtls_cfg->rekey;
2236  rtp->suite = dtls_cfg->suite;
2237 
2238  res = dtls_details_initialize(&rtp->dtls, rtp->ssl_ctx, dtls_cfg->default_setup, instance);
2239  if (!res) {
2240  dtls_setup_rtcp(instance);
2241  }
2242 
2243  return res;
2244 }
2245 
2246 /*! \pre instance is locked */
2247 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
2248 {
2249  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2250 
2251  return !rtp->ssl_ctx ? 0 : 1;
2252 }
2253 
2254 /*! \pre instance is locked */
2255 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
2256 {
2257  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2258  SSL *ssl = rtp->dtls.ssl;
2259 
2260  ast_debug_dtls(3, "(%p) DTLS stop\n", instance);
2261  ao2_unlock(instance);
2262  dtls_srtp_stop_timeout_timer(instance, rtp, 0);
2263  ao2_lock(instance);
2264 
2265  if (rtp->ssl_ctx) {
2266  SSL_CTX_free(rtp->ssl_ctx);
2267  rtp->ssl_ctx = NULL;
2268  }
2269 
2270  if (rtp->dtls.ssl) {
2271  SSL_free(rtp->dtls.ssl);
2272  rtp->dtls.ssl = NULL;
2273  }
2274 
2275  if (rtp->rtcp) {
2276  ao2_unlock(instance);
2277  dtls_srtp_stop_timeout_timer(instance, rtp, 1);
2278  ao2_lock(instance);
2279 
2280  if (rtp->rtcp->dtls.ssl) {
2281  if (rtp->rtcp->dtls.ssl != ssl) {
2282  SSL_free(rtp->rtcp->dtls.ssl);
2283  }
2284  rtp->rtcp->dtls.ssl = NULL;
2285  }
2286  }
2287 }
2288 
2289 /*! \pre instance is locked */
2290 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
2291 {
2292  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2293 
2294  if (SSL_is_init_finished(rtp->dtls.ssl)) {
2295  SSL_shutdown(rtp->dtls.ssl);
2296  rtp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW;
2297  }
2298 
2299  if (rtp->rtcp && SSL_is_init_finished(rtp->rtcp->dtls.ssl)) {
2300  SSL_shutdown(rtp->rtcp->dtls.ssl);
2301  rtp->rtcp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW;
2302  }
2303 }
2304 
2305 /*! \pre instance is locked */
2306 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
2307 {
2308  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2309 
2310  return rtp->dtls.connection;
2311 }
2312 
2313 /*! \pre instance is locked */
2314 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
2315 {
2316  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2317 
2318  return rtp->dtls.dtls_setup;
2319 }
2320 
2321 static void dtls_set_setup(enum ast_rtp_dtls_setup *dtls_setup, enum ast_rtp_dtls_setup setup, SSL *ssl)
2322 {
2323  enum ast_rtp_dtls_setup old = *dtls_setup;
2324 
2325  switch (setup) {
2327  *dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
2328  break;
2330  *dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
2331  break;
2333  /* We can't respond to an actpass setup with actpass ourselves... so respond with active, as we can initiate connections */
2334  if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
2335  *dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
2336  }
2337  break;
2339  *dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
2340  break;
2341  default:
2342  /* This should never occur... if it does exit early as we don't know what state things are in */
2343  return;
2344  }
2345 
2346  /* If the setup state did not change we go on as if nothing happened */
2347  if (old == *dtls_setup) {
2348  return;
2349  }
2350 
2351  /* If they don't want us to establish a connection wait until later */
2352  if (*dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
2353  return;
2354  }
2355 
2356  if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
2357  SSL_set_connect_state(ssl);
2358  } else if (*dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
2359  SSL_set_accept_state(ssl);
2360  } else {
2361  return;
2362  }
2363 }
2364 
2365 /*! \pre instance is locked */
2366 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
2367 {
2368  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2369 
2370  if (rtp->dtls.ssl) {
2371  dtls_set_setup(&rtp->dtls.dtls_setup, setup, rtp->dtls.ssl);
2372  }
2373 
2374  if (rtp->rtcp && rtp->rtcp->dtls.ssl) {
2375  dtls_set_setup(&rtp->rtcp->dtls.dtls_setup, setup, rtp->rtcp->dtls.ssl);
2376  }
2377 }
2378 
2379 /*! \pre instance is locked */
2380 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
2381 {
2382  char *tmp = ast_strdupa(fingerprint), *value;
2383  int pos = 0;
2384  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2385 
2386  if (hash != AST_RTP_DTLS_HASH_SHA1 && hash != AST_RTP_DTLS_HASH_SHA256) {
2387  return;
2388  }
2389 
2390  rtp->remote_hash = hash;
2391 
2392  while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
2393  sscanf(value, "%02hhx", &rtp->remote_fingerprint[pos++]);
2394  }
2395 }
2396 
2397 /*! \pre instance is locked */
2398 static enum ast_rtp_dtls_hash ast_rtp_dtls_get_fingerprint_hash(struct ast_rtp_instance *instance)
2399 {
2400  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2401 
2402  return rtp->local_hash;
2403 }
2404 
2405 /*! \pre instance is locked */
2406 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance)
2407 {
2408  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2409 
2410  return rtp->local_fingerprint;
2411 }
2412 
2413 /* DTLS RTP Engine interface declaration */
2414 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
2415  .set_configuration = ast_rtp_dtls_set_configuration,
2416  .active = ast_rtp_dtls_active,
2417  .stop = ast_rtp_dtls_stop,
2418  .reset = ast_rtp_dtls_reset,
2419  .get_connection = ast_rtp_dtls_get_connection,
2420  .get_setup = ast_rtp_dtls_get_setup,
2421  .set_setup = ast_rtp_dtls_set_setup,
2422  .set_fingerprint = ast_rtp_dtls_set_fingerprint,
2423  .get_fingerprint_hash = ast_rtp_dtls_get_fingerprint_hash,
2424  .get_fingerprint = ast_rtp_dtls_get_fingerprint,
2425 };
2426 
2427 #endif
2428 
2429 #ifdef TEST_FRAMEWORK
2430 static size_t get_recv_buffer_count(struct ast_rtp_instance *instance)
2431 {
2432  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2433 
2434  if (rtp && rtp->recv_buffer) {
2435  return ast_data_buffer_count(rtp->recv_buffer);
2436  }
2437 
2438  return 0;
2439 }
2440 
2441 static size_t get_recv_buffer_max(struct ast_rtp_instance *instance)
2442 {
2443  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2444 
2445  if (rtp && rtp->recv_buffer) {
2446  return ast_data_buffer_max(rtp->recv_buffer);
2447  }
2448 
2449  return 0;
2450 }
2451 
2452 static size_t get_send_buffer_count(struct ast_rtp_instance *instance)
2453 {
2454  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2455 
2456  if (rtp && rtp->send_buffer) {
2457  return ast_data_buffer_count(rtp->send_buffer);
2458  }
2459 
2460  return 0;
2461 }
2462 
2463 static void set_rtp_rtcp_schedid(struct ast_rtp_instance *instance, int id)
2464 {
2465  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2466 
2467  if (rtp && rtp->rtcp) {
2468  rtp->rtcp->schedid = id;
2469  }
2470 }
2471 
2473  .packets_to_drop = 0,
2474  .send_report = 0,
2475  .sdes_received = 0,
2476  .recv_buffer_count = get_recv_buffer_count,
2477  .recv_buffer_max = get_recv_buffer_max,
2478  .send_buffer_count = get_send_buffer_count,
2479  .set_schedid = set_rtp_rtcp_schedid,
2480 };
2481 #endif
2482 
2483 /* RTP Engine Declaration */
2485  .name = "asterisk",
2486  .new = ast_rtp_new,
2487  .destroy = ast_rtp_destroy,
2488  .dtmf_begin = ast_rtp_dtmf_begin,
2489  .dtmf_end = ast_rtp_dtmf_end,
2490  .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
2491  .dtmf_mode_set = ast_rtp_dtmf_mode_set,
2492  .dtmf_mode_get = ast_rtp_dtmf_mode_get,
2493  .update_source = ast_rtp_update_source,
2494  .change_source = ast_rtp_change_source,
2495  .write = ast_rtp_write,
2496  .read = ast_rtp_read,
2497  .prop_set = ast_rtp_prop_set,
2498  .fd = ast_rtp_fd,
2499  .remote_address_set = ast_rtp_remote_address_set,
2500  .red_init = rtp_red_init,
2501  .red_buffer = rtp_red_buffer,
2502  .local_bridge = ast_rtp_local_bridge,
2503  .get_stat = ast_rtp_get_stat,
2504  .dtmf_compatible = ast_rtp_dtmf_compatible,
2505  .stun_request = ast_rtp_stun_request,
2506  .stop = ast_rtp_stop,
2507  .qos = ast_rtp_qos_set,
2508  .sendcng = ast_rtp_sendcng,
2509 #ifdef HAVE_PJPROJECT
2510  .ice = &ast_rtp_ice,
2511 #endif
2512 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
2513  .dtls = &ast_rtp_dtls,
2514  .activate = ast_rtp_activate,
2515 #endif
2516  .ssrc_get = ast_rtp_get_ssrc,
2517  .cname_get = ast_rtp_get_cname,
2518  .set_remote_ssrc = ast_rtp_set_remote_ssrc,
2519  .set_stream_num = ast_rtp_set_stream_num,
2520  .extension_enable = ast_rtp_extension_enable,
2521  .bundle = ast_rtp_bundle,
2522 #ifdef TEST_FRAMEWORK
2523  .test = &ast_rtp_test,
2524 #endif
2525 };
2526 
2527 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
2528 /*! \pre instance is locked */
2529 static void dtls_perform_handshake(struct ast_rtp_instance *instance, struct dtls_details *dtls, int rtcp)
2530 {
2531  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2532 
2533  ast_debug_dtls(3, "(%p) DTLS perform handshake - ssl = %p, setup = %d\n",
2534  rtp, dtls->ssl, dtls->dtls_setup);
2535 
2536  /* If we are not acting as a client connecting to the remote side then
2537  * don't start the handshake as it will accomplish nothing and would conflict
2538  * with the handshake we receive from the remote side.
2539  */
2540  if (!dtls->ssl || (dtls->dtls_setup != AST_RTP_DTLS_SETUP_ACTIVE)) {
2541  return;
2542  }
2543 
2544  SSL_do_handshake(dtls->ssl);
2545 
2546  /*
2547  * A race condition is prevented between this function and __rtp_recvfrom()
2548  * because both functions have to get the instance lock before they can do
2549  * anything. Without holding the instance lock, this function could start
2550  * the SSL handshake above in one thread and the __rtp_recvfrom() function
2551  * called by the channel thread could read the response and stop the timeout
2552  * timer before we have a chance to even start it.
2553  */
2554  dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
2555 }
2556 #endif
2557 
2558 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
2559 static void dtls_perform_setup(struct dtls_details *dtls)
2560 {
2561  if (!dtls->ssl || !SSL_is_init_finished(dtls->ssl)) {
2562  return;
2563  }
2564 
2565  SSL_clear(dtls->ssl);
2566  if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
2567  SSL_set_accept_state(dtls->ssl);
2568  } else {
2569  SSL_set_connect_state(dtls->ssl);
2570  }
2571  dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
2572 
2573  ast_debug_dtls(3, "DTLS perform setup - connection reset\n");
2574 }
2575 #endif
2576 
2577 #ifdef HAVE_PJPROJECT
2578 static void rtp_learning_start(struct ast_rtp *rtp);
2579 
2580 /* Handles start of media during ICE negotiation or completion */
2581 static void ast_rtp_ice_start_media(pj_ice_sess *ice, pj_status_t status)
2582 {
2583  struct ast_rtp_instance *instance = ice->user_data;
2584  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2585 
2586  ao2_lock(instance);
2587 
2588  if (status == PJ_SUCCESS) {
2589  struct ast_sockaddr remote_address;
2590 
2591  ast_sockaddr_setnull(&remote_address);
2593  if (!ast_sockaddr_isnull(&remote_address)) {
2594  /* Symmetric RTP must be disabled for the remote address to not get overwritten */
2596 
2597  ast_rtp_instance_set_remote_address(instance, &remote_address);
2598  }
2599 
2600  if (rtp->rtcp) {
2602  }
2603  }
2604 
2605 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
2606  /* If we've already started media, no need to do all of this again */
2607  if (rtp->ice_media_started) {
2608  ao2_unlock(instance);
2609  return;
2610  }
2611 
2613  "(%p) ICE starting media - perform DTLS - (%p)\n", instance, rtp);
2614 
2615  /*
2616  * Seemingly no reason to call dtls_perform_setup here. Currently we'll do a full
2617  * protocol level renegotiation if things do change. And if bundled is being used
2618  * then ICE is reused when a stream is added.
2619  *
2620  * Note, if for some reason in the future dtls_perform_setup does need to done here
2621  * be aware that creates a race condition between the call here (on ice completion)
2622  * and potential DTLS handshaking when receiving RTP. What happens is the ssl object
2623  * can get cleared (SSL_clear) during that handshaking process (DTLS init). If that
2624  * happens then Asterisk won't complete DTLS initialization. RTP packets are still
2625  * sent/received but won't be encrypted/decrypted.
2626  */
2627  dtls_perform_handshake(instance, &rtp->dtls, 0);
2628 
2629  if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) {
2630  dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1);
2631  }
2632 #endif
2633 
2634  rtp->ice_media_started = 1;
2635 
2636  if (!strictrtp) {
2637  ao2_unlock(instance);
2638  return;
2639  }
2640 
2641  ast_verb(4, "%p -- Strict RTP learning after ICE completion\n", rtp);
2642  rtp_learning_start(rtp);
2643  ao2_unlock(instance);
2644 }
2645 
2646 #ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK
2647 /* PJPROJECT ICE optional callback */
2648 static void ast_rtp_on_valid_pair(pj_ice_sess *ice)
2649 {
2650  ast_debug_ice(2, "(%p) ICE valid pair, start media\n", ice->user_data);
2651  ast_rtp_ice_start_media(ice, PJ_SUCCESS);
2652 }
2653 #endif
2654 
2655 /* PJPROJECT ICE callback */
2656 static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t status)
2657 {
2658  ast_debug_ice(2, "(%p) ICE complete, start media\n", ice->user_data);
2659  ast_rtp_ice_start_media(ice, status);
2660 }
2661 
2662 /* PJPROJECT ICE callback */
2663 static void ast_rtp_on_ice_rx_data(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, void *pkt, pj_size_t size, const pj_sockaddr_t *src_addr, unsigned src_addr_len)
2664 {
2665  struct ast_rtp_instance *instance = ice->user_data;
2666  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2667 
2668  /* Instead of handling the packet here (which really doesn't work with our architecture) we set a bit to indicate that it should be handled after pj_ice_sess_on_rx_pkt
2669  * returns */
2670  if (transport_id == TRANSPORT_SOCKET_RTP || transport_id == TRANSPORT_SOCKET_RTCP) {
2671  rtp->passthrough = 1;
2672  } else if (transport_id == TRANSPORT_TURN_RTP) {
2673  rtp->rtp_passthrough = 1;
2674  } else if (transport_id == TRANSPORT_TURN_RTCP) {
2675  rtp->rtcp_passthrough = 1;
2676  }
2677 }
2678 
2679 /* PJPROJECT ICE callback */
2680 static pj_status_t ast_rtp_on_ice_tx_pkt(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, const void *pkt, pj_size_t size, const pj_sockaddr_t *dst_addr, unsigned dst_addr_len)
2681 {
2682  struct ast_rtp_instance *instance = ice->user_data;
2683  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2684  pj_status_t status = PJ_EINVALIDOP;
2685  pj_ssize_t _size = (pj_ssize_t)size;
2686 
2687  if (transport_id == TRANSPORT_SOCKET_RTP) {
2688  /* Traffic is destined to go right out the RTP socket we already have */
2689  status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
2690  /* sendto on a connectionless socket should send all the data, or none at all */
2691  ast_assert(_size == size || status != PJ_SUCCESS);
2692  } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
2693  /* Traffic is destined to go right out the RTCP socket we already have */
2694  if (rtp->rtcp) {
2695  status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
2696  /* sendto on a connectionless socket should send all the data, or none at all */
2697  ast_assert(_size == size || status != PJ_SUCCESS);
2698  } else {
2699  status = PJ_SUCCESS;
2700  }
2701  } else if (transport_id == TRANSPORT_TURN_RTP) {
2702  /* Traffic is going through the RTP TURN relay */
2703  if (rtp->turn_rtp) {
2704  status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
2705  }
2706  } else if (transport_id == TRANSPORT_TURN_RTCP) {
2707  /* Traffic is going through the RTCP TURN relay */
2708  if (rtp->turn_rtcp) {
2709  status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
2710  }
2711  }
2712 
2713  return status;
2714 }
2715 
2716 /* ICE Session interface declaration */
2717 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
2718 #ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK
2719  .on_valid_pair = ast_rtp_on_valid_pair,
2720 #endif
2721  .on_ice_complete = ast_rtp_on_ice_complete,
2722  .on_rx_data = ast_rtp_on_ice_rx_data,
2723  .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
2724 };
2725 
2726 /*! \brief Worker thread for timerheap */
2727 static int timer_worker_thread(void *data)
2728 {
2729  pj_ioqueue_t *ioqueue;
2730 
2731  if (pj_ioqueue_create(pool, 1, &ioqueue) != PJ_SUCCESS) {
2732  return -1;
2733  }
2734 
2735  while (!timer_terminate) {
2736  const pj_time_val delay = {0, 10};
2737 
2738  pj_timer_heap_poll(timer_heap, NULL);
2739  pj_ioqueue_poll(ioqueue, &delay);
2740  }
2741 
2742  return 0;
2743 }
2744 #endif
2745 
2746 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
2747 {
2749  return 0;
2750  }
2752  if (rtpdebugport) {
2753  return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
2754  } else {
2755  return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
2756  }
2757  }
2758 
2759  return 1;
2760 }
2761 
2762 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
2763 {
2765  return 0;
2766  }
2768  if (rtcpdebugport) {
2769  return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
2770  } else {
2771  return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
2772  }
2773  }
2774 
2775  return 1;
2776 }
2777 
2778 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
2779 /*! \pre instance is locked */
2780 static int dtls_srtp_handle_timeout(struct ast_rtp_instance *instance, int rtcp)
2781 {
2782  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2783  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2784  struct timeval dtls_timeout;
2785 
2786  ast_debug_dtls(3, "(%p) DTLS srtp - handle timeout - rtcp=%d\n", instance, rtcp);
2787  DTLSv1_handle_timeout(dtls->ssl);
2788 
2789  /* If a timeout can't be retrieved then this recurring scheduled item must stop */
2790  if (!DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
2791  dtls->timeout_timer = -1;
2792  return 0;
2793  }
2794 
2795  return dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
2796 }
2797 
2798 /* Scheduler callback */
2799 static int dtls_srtp_handle_rtp_timeout(const void *data)
2800 {
2801  struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
2802  int reschedule;
2803 
2804  ao2_lock(instance);
2805  reschedule = dtls_srtp_handle_timeout(instance, 0);
2806  ao2_unlock(instance);
2807  if (!reschedule) {
2808  ao2_ref(instance, -1);
2809  }
2810 
2811  return reschedule;
2812 }
2813 
2814 /* Scheduler callback */
2815 static int dtls_srtp_handle_rtcp_timeout(const void *data)
2816 {
2817  struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
2818  int reschedule;
2819 
2820  ao2_lock(instance);
2821  reschedule = dtls_srtp_handle_timeout(instance, 1);
2822  ao2_unlock(instance);
2823  if (!reschedule) {
2824  ao2_ref(instance, -1);
2825  }
2826 
2827  return reschedule;
2828 }
2829 
2830 static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
2831 {
2832  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2833  struct timeval dtls_timeout;
2834 
2835  if (DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
2836  int timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
2837 
2838  ast_assert(dtls->timeout_timer == -1);
2839 
2840  ao2_ref(instance, +1);
2841  if ((dtls->timeout_timer = ast_sched_add(rtp->sched, timeout,
2842  !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout, instance)) < 0) {
2843  ao2_ref(instance, -1);
2844  ast_log(LOG_WARNING, "Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n",
2845  !rtcp ? "RTP" : "RTCP", instance);
2846  } else {
2847  ast_debug_dtls(3, "(%p) DTLS srtp - scheduled timeout timer for '%d'\n", instance, timeout);
2848  }
2849  }
2850 }
2851 
2852 /*! \pre Must not be called with the instance locked. */
2853 static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
2854 {
2855  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2856 
2857  AST_SCHED_DEL_UNREF(rtp->sched, dtls->timeout_timer, ao2_ref(instance, -1));
2858  ast_debug_dtls(3, "(%p) DTLS srtp - stopped timeout timer'\n", instance);
2859 }
2860 
2861 /* Scheduler callback */
2862 static int dtls_srtp_renegotiate(const void *data)
2863 {
2864  struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
2865  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2866 
2867  ao2_lock(instance);
2868 
2869  ast_debug_dtls(3, "(%p) DTLS srtp - renegotiate'\n", instance);
2870  SSL_renegotiate(rtp->dtls.ssl);
2871  SSL_do_handshake(rtp->dtls.ssl);
2872 
2873  if (rtp->rtcp && rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) {
2874  SSL_renegotiate(rtp->rtcp->dtls.ssl);
2875  SSL_do_handshake(rtp->rtcp->dtls.ssl);
2876  }
2877 
2878  rtp->rekeyid = -1;
2879 
2880  ao2_unlock(instance);
2881  ao2_ref(instance, -1);
2882 
2883  return 0;
2884 }
2885 
2886 static int dtls_srtp_add_local_ssrc(struct ast_rtp *rtp, struct ast_rtp_instance *instance, int rtcp, unsigned int ssrc, int set_remote_policy)
2887 {
2888  unsigned char material[SRTP_MASTER_LEN * 2];
2889  unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
2890  struct ast_srtp_policy *local_policy, *remote_policy = NULL;
2891  int res = -1;
2892  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2893 
2894  ast_debug_dtls(3, "(%p) DTLS srtp - add local ssrc - rtcp=%d, set_remote_policy=%d'\n",
2895  instance, rtcp, set_remote_policy);
2896 
2897  /* Produce key information and set up SRTP */
2898  if (!SSL_export_keying_material(dtls->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
2899  ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
2900  instance);
2901  return -1;
2902  }
2903 
2904  /* Whether we are acting as a server or client determines where the keys/salts are */
2905  if (rtp->dtls.dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
2906  local_key = material;
2907  remote_key = local_key + SRTP_MASTER_KEY_LEN;
2908  local_salt = remote_key + SRTP_MASTER_KEY_LEN;
2909  remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
2910  } else {
2911  remote_key = material;
2912  local_key = remote_key + SRTP_MASTER_KEY_LEN;
2913  remote_salt = local_key + SRTP_MASTER_KEY_LEN;
2914  local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
2915  }
2916 
2917  if (!(local_policy = res_srtp_policy->alloc())) {
2918  return -1;
2919  }
2920 
2921  if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
2922  ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
2923  goto error;
2924  }
2925 
2926  if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
2927  ast_log(LOG_WARNING, "Could not set suite to '%u' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
2928  goto error;
2929  }
2930 
2931  res_srtp_policy->set_ssrc(local_policy, ssrc, 0);
2932 
2933  if (set_remote_policy) {
2934  if (!(remote_policy = res_srtp_policy->alloc())) {
2935  goto error;
2936  }
2937 
2938  if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
2939  ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
2940  goto error;
2941  }
2942 
2943  if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
2944  ast_log(LOG_WARNING, "Could not set suite to '%u' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
2945  goto error;
2946  }
2947 
2948  res_srtp_policy->set_ssrc(remote_policy, 0, 1);
2949  }
2950 
2951  if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy, rtcp)) {
2952  ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
2953  goto error;
2954  }
2955 
2956  res = 0;
2957 
2958 error:
2959  /* policy->destroy() called even on success to release local reference to these resources */
2960  res_srtp_policy->destroy(local_policy);
2961 
2962  if (remote_policy) {
2963  res_srtp_policy->destroy(remote_policy);
2964  }
2965 
2966  return res;
2967 }
2968 
2969 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_rtp_instance *instance, int rtcp)
2970 {
2971  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2972  int index;
2973 
2974  ast_debug_dtls(3, "(%p) DTLS setup SRTP rtp=%p'\n", instance, rtp);
2975 
2976  /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
2977  if (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) {
2978  X509 *certificate;
2979 
2980  if (!(certificate = SSL_get_peer_certificate(dtls->ssl))) {
2981  ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
2982  return -1;
2983  }
2984 
2985  /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
2986  if (rtp->remote_fingerprint[0]) {
2987  const EVP_MD *type;
2988  unsigned char fingerprint[EVP_MAX_MD_SIZE];
2989  unsigned int size;
2990 
2991  if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA1) {
2992  type = EVP_sha1();
2993  } else if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA256) {
2994  type = EVP_sha256();
2995  } else {
2996  ast_log(LOG_WARNING, "Unsupported fingerprint hash type on RTP instance '%p'\n", instance);
2997  return -1;
2998  }
2999 
3000  if (!X509_digest(certificate, type, fingerprint, &size) ||
3001  !size ||
3002  memcmp(fingerprint, rtp->remote_fingerprint, size)) {
3003  X509_free(certificate);
3004  ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
3005  instance);
3006  return -1;
3007  }
3008  }
3009 
3010  X509_free(certificate);
3011  }
3012 
3013  if (dtls_srtp_add_local_ssrc(rtp, instance, rtcp, ast_rtp_instance_get_ssrc(instance), 1)) {
3014  ast_log(LOG_ERROR, "Failed to add local source '%p'\n", rtp);
3015  return -1;
3016  }
3017 
3018  for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) {
3019  struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index);
3020 
3021  if (dtls_srtp_add_local_ssrc(rtp, instance, rtcp, ast_rtp_instance_get_ssrc(mapping->instance), 0)) {
3022  return -1;
3023  }
3024  }
3025 
3026  if (rtp->rekey) {
3027  ao2_ref(instance, +1);
3028  if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
3029  ao2_ref(instance, -1);
3030  return -1;
3031  }
3032  }
3033 
3034  return 0;
3035 }
3036 #endif
3037 
3038 /*! \brief Helper function to compare an elem in a vector by value */
3039 static int compare_by_value(int elem, int value)
3040 {
3041  return elem - value;
3042 }
3043 
3044 /*! \brief Helper function to find an elem in a vector by value */
3045 static int find_by_value(int elem, int value)
3046 {
3047  return elem == value;
3048 }
3049 
3050 static int rtcp_mux(struct ast_rtp *rtp, const unsigned char *packet)
3051 {
3052  uint8_t version;
3053  uint8_t pt;
3054  uint8_t m;
3055 
3056  if (!rtp->rtcp || rtp->rtcp->type != AST_RTP_INSTANCE_RTCP_MUX) {
3057  return 0;
3058  }
3059 
3060  version = (packet[0] & 0XC0) >> 6;
3061  if (version == 0) {
3062  /* version 0 indicates this is a STUN packet and shouldn't
3063  * be interpreted as a possible RTCP packet
3064  */
3065  return 0;
3066  }
3067 
3068  /* The second octet of a packet will be one of the following:
3069  * For RTP: The marker bit (1 bit) and the RTP payload type (7 bits)
3070  * For RTCP: The payload type (8)
3071  *
3072  * RTP has a forbidden range of payload types (64-95) since these
3073  * will conflict with RTCP payload numbers if the marker bit is set.
3074  */
3075  m = packet[1] & 0x80;
3076  pt = packet[1] & 0x7F;
3077  if (m && pt >= 64 && pt <= 95) {
3078  return 1;
3079  }
3080  return 0;
3081 }
3082 
3083 /*! \pre instance is locked */
3084 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
3085 {
3086  int len;
3087  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3088 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3089  char *in = buf;
3090 #endif
3091 #ifdef HAVE_PJPROJECT
3092  struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
3093 #endif
3094 #ifdef TEST_FRAMEWORK
3096 #endif
3097 
3098  if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
3099  return len;
3100  }
3101 
3102 #ifdef TEST_FRAMEWORK
3103  if (test && test->packets_to_drop > 0) {
3104  test->packets_to_drop--;
3105  return 0;
3106  }
3107 #endif
3108 
3109 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3110  /* If this is an SSL packet pass it to OpenSSL for processing. RFC section for first byte value:
3111  * https://tools.ietf.org/html/rfc5764#section-5.1.2 */
3112  if ((*in >= 20) && (*in <= 63)) {
3113  struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
3114  int res = 0;
3115 
3116  /* If no SSL session actually exists terminate things */
3117  if (!dtls->ssl) {
3118  ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
3119  instance);
3120  return -1;
3121  }
3122 
3123  ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - Got SSL packet '%d'\n", instance, rtp, *in);
3124 
3125  /*
3126  * A race condition is prevented between dtls_perform_handshake()
3127  * and this function because both functions have to get the
3128  * instance lock before they can do anything. The
3129  * dtls_perform_handshake() function needs to start the timer
3130  * before we stop it below.
3131  */
3132 
3133  /* Before we feed data into OpenSSL ensure that the timeout timer is either stopped or completed */
3134  ao2_unlock(instance);
3135  dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
3136  ao2_lock(instance);
3137 
3138  /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
3139  if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
3140  dtls->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
3141  SSL_set_accept_state(dtls->ssl);
3142  }
3143 
3144  BIO_write(dtls->read_bio, buf, len);
3145 
3146  len = SSL_read(dtls->ssl, buf, len);
3147 
3148  if ((len < 0) && (SSL_get_error(dtls->ssl, len) == SSL_ERROR_SSL)) {
3149  unsigned long error = ERR_get_error();
3150  ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n",
3151  instance, ERR_reason_error_string(error));
3152  return -1;
3153  }
3154 
3155  if (SSL_is_init_finished(dtls->ssl)) {
3156  /* Any further connections will be existing since this is now established */
3157  dtls->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
3158  /* Use the keying material to set up key/salt information */
3159  if ((res = dtls_srtp_setup(rtp, instance, rtcp))) {
3160  return res;
3161  }
3162  /* Notify that dtls has been established */
3163  res = RTP_DTLS_ESTABLISHED;
3164 
3165  ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - established'\n", instance, rtp);
3166  } else {
3167  /* Since we've sent additional traffic start the timeout timer for retransmission */
3168  dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
3169  }
3170 
3171  return res;
3172  }
3173 #endif
3174 
3175 #ifdef HAVE_PJPROJECT
3176  if (!ast_sockaddr_isnull(loop) && !ast_sockaddr_cmp(loop, sa)) {
3177  /* ICE traffic will have been handled in the TURN callback, so skip it but update the address
3178  * so it reflects the actual source and not the loopback
3179  */
3180  if (rtcp) {
3181  ast_sockaddr_copy(sa, &rtp->rtcp->them);
3182  } else {
3184  }
3185  } else if (rtp->ice) {
3186  pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
3187  pj_sockaddr address;
3188  pj_status_t status;
3189  struct ice_wrap *ice;
3190 
3192 
3193  pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
3194 
3195  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3196  ice = rtp->ice;
3197  ao2_ref(ice, +1);
3198  ao2_unlock(instance);
3199  status = pj_ice_sess_on_rx_pkt(ice->real_ice,
3201  rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
3202  pj_sockaddr_get_len(&address));
3203  ao2_ref(ice, -1);
3204  ao2_lock(instance);
3205  if (status != PJ_SUCCESS) {
3206  char err_buf[100];
3207 
3208  pj_strerror(status, err_buf, sizeof(err_buf));
3209  ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
3210  (int)status, err_buf);
3211  return -1;
3212  }
3213  if (!rtp->passthrough) {
3214  /* If a unidirectional ICE negotiation occurs then lock on to the source of the
3215  * ICE traffic and use it as the target. This will occur if the remote side only
3216  * wants to receive media but never send to us.
3217  */
3218  if (!rtp->ice_active_remote_candidates && !rtp->ice_proposed_remote_candidates) {
3219  if (rtcp) {
3220  ast_sockaddr_copy(&rtp->rtcp->them, sa);
3221  } else {
3223  }
3224  }
3225  return 0;
3226  }
3227  rtp->passthrough = 0;
3228  }
3229 #endif
3230 
3231  return len;
3232 }
3233 
3234 /*! \pre instance is locked */
3235 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
3236 {
3237  return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
3238 }
3239 
3240 /*! \pre instance is locked */
3241 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
3242 {
3243  return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
3244 }
3245 
3246 /*! \pre instance is locked */
3247 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp)
3248 {
3249  int len = size;
3250  void *temp = buf;
3251  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3252  struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance;
3253  struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(transport);
3254  struct ast_srtp *srtp = ast_rtp_instance_get_srtp(transport, rtcp);
3255  int res;
3256 
3257  *via_ice = 0;
3258 
3259  if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
3260  return -1;
3261  }
3262 
3263 #ifdef HAVE_PJPROJECT
3264  if (transport_rtp->ice) {
3266  pj_status_t status;
3267  struct ice_wrap *ice;
3268 
3269  /* If RTCP is sharing the same socket then use the same component */
3270  if (rtcp && rtp->rtcp->s == rtp->s) {
3271  component = AST_RTP_ICE_COMPONENT_RTP;
3272  }
3273 
3275 
3276  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3277  ice = transport_rtp->ice;
3278  ao2_ref(ice, +1);
3279  if (instance == transport) {
3280  ao2_unlock(instance);
3281  }
3282  status = pj_ice_sess_send_data(ice->real_ice, component, temp, len);
3283  ao2_ref(ice, -1);
3284  if (instance == transport) {
3285  ao2_lock(instance);
3286  }
3287  if (status == PJ_SUCCESS) {
3288  *via_ice = 1;
3289  return len;
3290  }
3291  }
3292 #endif
3293 
3294  res = ast_sendto(rtcp ? transport_rtp->rtcp->s : transport_rtp->s, temp, len, flags, sa);
3295  if (res > 0) {
3296  ast_rtp_instance_set_last_tx(instance, time(NULL));
3297  }
3298 
3299  return res;
3300 }
3301 
3302 /*! \pre instance is locked */
3303 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
3304 {
3305  return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
3306 }
3307 
3308 /*! \pre instance is locked */
3309 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
3310 {
3311  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3312  int hdrlen = 12;
3313  int res;
3314 
3315  if ((res = __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1)) > 0) {
3316  rtp->txcount++;
3317  rtp->txoctetcount += (res - hdrlen);
3318  }
3319 
3320  return res;
3321 }
3322 
3323 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
3324 {
3325  unsigned int interval;
3326  /*! \todo XXX Do a more reasonable calculation on this one
3327  * Look in RFC 3550 Section A.7 for an example*/
3328  interval = rtcpinterval;
3329  return interval;
3330 }
3331 
3332 static void calc_mean_and_standard_deviation(double new_sample, double *mean, double *std_dev, unsigned int *count)
3333 {
3334  double delta1;
3335  double delta2;
3336 
3337  /* First convert the standard deviation back into a sum of squares. */
3338  double last_sum_of_squares = (*std_dev) * (*std_dev) * (*count ?: 1);
3339 
3340  if (++(*count) == 0) {
3341  /* Avoid potential divide by zero on an overflow */
3342  *count = 1;
3343  }
3344 
3345  /*
3346  * Below is an implementation of Welford's online algorithm [1] for calculating
3347  * mean and variance in a single pass.
3348  *
3349  * [1] https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3350  */
3351 
3352  delta1 = new_sample - *mean;
3353  *mean += (delta1 / *count);
3354  delta2 = new_sample - *mean;
3355 
3356  /* Now calculate the new variance, and subsequent standard deviation */
3357  *std_dev = sqrt((last_sum_of_squares + (delta1 * delta2)) / *count);
3358 }
3359 
3360 static int create_new_socket(const char *type, int af)
3361 {
3362  int sock = ast_socket_nonblock(af, SOCK_DGRAM, 0);
3363 
3364  if (sock < 0) {
3365  ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
3366  return sock;
3367  }
3368 
3369 #ifdef SO_NO_CHECK
3370  if (nochecksums) {
3371  setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
3372  }
3373 #endif
3374 
3375  return sock;
3376 }
3377 
3378 /*!
3379  * \internal
3380  * \brief Initializes sequence values and probation for learning mode.
3381  * \note This is an adaptation of pjmedia's pjmedia_rtp_seq_init function.
3382  *
3383  * \param info The learning information to track
3384  * \param seq sequence number read from the rtp header to initialize the information with
3385  */
3386 static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq)
3387 {
3388  info->max_seq = seq;
3390  memset(&info->received, 0, sizeof(info->received));
3391 }
3392 
3393 /*!
3394  * \internal
3395  * \brief Updates sequence information for learning mode and determines if probation/learning mode should remain in effect.
3396  * \note This function was adapted from pjmedia's pjmedia_rtp_seq_update function.
3397  *
3398  * \param info Structure tracking the learning progress of some address
3399  * \param seq sequence number read from the rtp header
3400  * \retval 0 if probation mode should exit for this address
3401  * \retval non-zero if probation mode should continue
3402  */
3404 {
3405  if (seq == (uint16_t) (info->max_seq + 1)) {
3406  /* packet is in sequence */
3407  info->packets--;
3408  } else {
3409  /* Sequence discontinuity; reset */
3410  info->packets = learning_min_sequential - 1;
3411  info->received = ast_tvnow();
3412  }
3413 
3414  /* Only check time if strictrtp is set to yes. Otherwise, we only needed to check seqno */
3415  if (strictrtp == STRICT_RTP_YES) {
3416  switch (info->stream_type) {
3418  case AST_MEDIA_TYPE_AUDIO:
3419  /*
3420  * Protect against packet floods by checking that we
3421  * received the packet sequence in at least the minimum
3422  * allowed time.
3423  */
3424  if (ast_tvzero(info->received)) {
3425  info->received = ast_tvnow();
3426  } else if (!info->packets
3428  /* Packet flood; reset */
3429  info->packets = learning_min_sequential - 1;
3430  info->received = ast_tvnow();
3431  }
3432  break;
3433  case AST_MEDIA_TYPE_VIDEO:
3434  case AST_MEDIA_TYPE_IMAGE:
3435  case AST_MEDIA_TYPE_TEXT:
3436  case AST_MEDIA_TYPE_END:
3437  break;
3438  }
3439  }
3440 
3441  info->max_seq = seq;
3442 
3443  return info->packets;
3444 }
3445 
3446 /*!
3447  * \brief Start the strictrtp learning mode.
3448  *
3449  * \param rtp RTP session description
3450  *
3451  * \return Nothing
3452  */
3453 static void rtp_learning_start(struct ast_rtp *rtp)
3454 {
3455  rtp->strict_rtp_state = STRICT_RTP_LEARN;
3456  memset(&rtp->rtp_source_learn.proposed_address, 0,
3457  sizeof(rtp->rtp_source_learn.proposed_address));
3458  rtp->rtp_source_learn.start = ast_tvnow();
3459  rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t) rtp->lastrxseqno);
3460 }
3461 
3462 #ifdef HAVE_PJPROJECT
3463 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message);
3464 
3465 /*!
3466  * \internal
3467  * \brief Resets and ACL to empty state.
3468  *
3469  * \return Nothing
3470  */
3472 {
3473  ast_rwlock_wrlock(lock);
3474  *acl = ast_free_acl_list(*acl);
3475  ast_rwlock_unlock(lock);
3476 }
3477 
3478 /*!
3479  * \internal
3480  * \brief Checks an address against the ICE blacklist
3481  * \note If there is no ice_blacklist list, always returns 0
3482  *
3483  * \param address The address to consider
3484  * \retval 0 if address is not ICE blacklisted
3485  * \retval 1 if address is ICE blacklisted
3486  */
3488 {
3489  int result = 0;
3490 
3491  ast_rwlock_rdlock(&ice_acl_lock);
3492  result |= ast_apply_acl_nolog(ice_acl, address) == AST_SENSE_DENY;
3493  ast_rwlock_unlock(&ice_acl_lock);
3494 
3495  return result;
3496 }
3497 
3498 /*!
3499  * \internal
3500  * \brief Checks an address against the STUN blacklist
3501  * \since 13.16.0
3502  *
3503  * \note If there is no stun_blacklist list, always returns 0
3504  *
3505  * \param addr The address to consider
3506  *
3507  * \retval 0 if address is not STUN blacklisted
3508  * \retval 1 if address is STUN blacklisted
3509  */
3510 static int stun_address_is_blacklisted(const struct ast_sockaddr *addr)
3511 {
3512  int result = 0;
3513 
3514  ast_rwlock_rdlock(&stun_acl_lock);
3515  result |= ast_apply_acl_nolog(stun_acl, addr) == AST_SENSE_DENY;
3516  ast_rwlock_unlock(&stun_acl_lock);
3517 
3518  return result;
3519 }
3520 
3521 /*! \pre instance is locked */
3522 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
3523  int transport)
3524 {
3525  unsigned int count = 0;
3526  struct ifaddrs *ifa, *ia;
3527  struct ast_sockaddr tmp;
3528  pj_sockaddr pjtmp;
3529  struct ast_ice_host_candidate *candidate;
3530  int af_inet_ok = 0, af_inet6_ok = 0;
3531 
3532  if (ast_sockaddr_is_ipv4(addr)) {
3533  af_inet_ok = 1;
3534  } else if (ast_sockaddr_is_any(addr)) {
3535  af_inet_ok = af_inet6_ok = 1;
3536  } else {
3537  af_inet6_ok = 1;
3538  }
3539 
3540  if (getifaddrs(&ifa) < 0) {
3541  /* If we can't get addresses, we can't load ICE candidates */
3542  ast_log(LOG_ERROR, "(%p) ICE Error obtaining list of local addresses: %s\n",
3543  instance, strerror(errno));
3544  } else {
3545  ast_debug_ice(2, "(%p) ICE add system candidates\n", instance);
3546  /* Iterate through the list of addresses obtained from the system,
3547  * until we've iterated through all of them, or accepted
3548  * PJ_ICE_MAX_CAND candidates */
3549  for (ia = ifa; ia && count < PJ_ICE_MAX_CAND; ia = ia->ifa_next) {
3550  /* Interface is either not UP or doesn't have an address assigned,
3551  * eg, a ppp that just completed LCP but no IPCP yet */
3552  if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) {
3553  continue;
3554  }
3555 
3556  /* Filter out non-IPvX addresses, eg, link-layer */
3557  if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) {
3558  continue;
3559  }
3560 
3561  ast_sockaddr_from_sockaddr(&tmp, ia->ifa_addr);
3562 
3563  if (ia->ifa_addr->sa_family == AF_INET) {
3564  const struct sockaddr_in *sa_in = (struct sockaddr_in*)ia->ifa_addr;
3565  if (!af_inet_ok) {
3566  continue;
3567  }
3568 
3569  /* Skip 127.0.0.0/8 (loopback) */
3570  /* Don't use IFF_LOOPBACK check since one could assign usable
3571  * publics to the loopback */
3572  if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) {
3573  continue;
3574  }
3575 
3576  /* Skip 0.0.0.0/8 based on RFC1122, and from pjproject */
3577  if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) {
3578  continue;
3579  }
3580  } else { /* ia->ifa_addr->sa_family == AF_INET6 */
3581  if (!af_inet6_ok) {
3582  continue;
3583  }
3584 
3585  /* Filter ::1 */
3586  if (!ast_sockaddr_cmp_addr(&lo6, &tmp)) {
3587  continue;
3588  }
3589  }
3590 
3591  /* Pull in the host candidates from [ice_host_candidates] */
3593  AST_LIST_TRAVERSE(&host_candidates, candidate, next) {
3594  if (!ast_sockaddr_cmp(&candidate->local, &tmp)) {
3595  /* candidate->local matches actual assigned, so check if
3596  * advertised is blacklisted, if not, add it to the
3597  * advertised list. Not that it would make sense to remap
3598  * a local address to a blacklisted address, but honour it
3599  * anyway. */
3600  if (!rtp_address_is_ice_blacklisted(&candidate->advertised)) {
3601  ast_sockaddr_to_pj_sockaddr(&candidate->advertised, &pjtmp);
3602  pj_sockaddr_set_port(&pjtmp, port);
3603  ast_rtp_ice_add_cand(instance, rtp, component, transport,
3604  PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL,
3605  pj_sockaddr_get_len(&pjtmp));
3606  ++count;
3607  }
3608 
3609  if (!candidate->include_local) {
3610  /* We don't want to advertise the actual address */
3611  ast_sockaddr_setnull(&tmp);
3612  }
3613 
3614  break;
3615  }
3616  }
3618 
3619  /* we had an entry in [ice_host_candidates] that matched, and
3620  * didn't have include_local_address set. Alternatively, adding
3621  * that match resulted in us going to PJ_ICE_MAX_CAND */
3622  if (ast_sockaddr_isnull(&tmp) || count == PJ_ICE_MAX_CAND) {
3623  continue;
3624  }
3625 
3626  if (rtp_address_is_ice_blacklisted(&tmp)) {
3627  continue;
3628  }
3629 
3630  ast_sockaddr_to_pj_sockaddr(&tmp, &pjtmp);
3631  pj_sockaddr_set_port(&pjtmp, port);
3632  ast_rtp_ice_add_cand(instance, rtp, component, transport,
3633  PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL,
3634  pj_sockaddr_get_len(&pjtmp));
3635  ++count;
3636  }
3637  freeifaddrs(ifa);
3638  }
3639 
3640  /* If configured to use a STUN server to get our external mapped address do so */
3641  if (stunaddr.sin_addr.s_addr && !stun_address_is_blacklisted(addr) &&
3642  (ast_sockaddr_is_ipv4(addr) || ast_sockaddr_is_any(addr)) &&
3643  count < PJ_ICE_MAX_CAND) {
3644  struct sockaddr_in answer;
3645  int rsp;
3646 
3648  "(%p) ICE request STUN %s %s candidate\n", instance,
3649  transport == AST_TRANSPORT_UDP ? "UDP" : "TCP",
3650  component == AST_RTP_ICE_COMPONENT_RTP ? "RTP" : "RTCP");
3651 
3652  /*
3653  * The instance should not be locked because we can block
3654  * waiting for a STUN respone.
3655  */
3656  ao2_unlock(instance);
3657  rsp = ast_stun_request(component == AST_RTP_ICE_COMPONENT_RTCP
3658  ? rtp->rtcp->s : rtp->s, &stunaddr, NULL, &answer);
3659  ao2_lock(instance);
3660  if (!rsp) {
3661  struct ast_rtp_engine_ice_candidate *candidate;
3662  pj_sockaddr ext, base;
3663  pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
3664  int srflx = 1, baseset = 0;
3665  struct ao2_iterator i;
3666 
3667  pj_sockaddr_init(pj_AF_INET(), &ext, &mapped, ntohs(answer.sin_port));
3668 
3669  /*
3670  * If the returned address is the same as one of our host
3671  * candidates, don't send the srflx. At the same time,
3672  * we need to set the base address (raddr).
3673  */
3674  i = ao2_iterator_init(rtp->ice_local_candidates, 0);
3675  while (srflx && (candidate = ao2_iterator_next(&i))) {
3676  if (!baseset && ast_sockaddr_is_ipv4(&candidate->address)) {
3677  baseset = 1;
3678  ast_sockaddr_to_pj_sockaddr(&candidate->address, &base);
3679  }
3680 
3681  if (!pj_sockaddr_cmp(&candidate->address, &ext)) {
3682  srflx = 0;
3683  }
3684 
3685  ao2_ref(candidate, -1);
3686  }
3688 
3689  if (srflx && baseset) {
3690  pj_sockaddr_set_port(&base, port);
3691  ast_rtp_ice_add_cand(instance, rtp, component, transport,
3692  PJ_ICE_CAND_TYPE_SRFLX, 65535, &ext, &base, &base,
3693  pj_sockaddr_get_len(&ext));
3694  }
3695  }
3696  }
3697 
3698  /* If configured to use a TURN relay create a session and allocate */
3699  if (pj_strlen(&turnaddr)) {
3700  ast_rtp_ice_turn_request(instance, component, AST_TRANSPORT_TCP, pj_strbuf(&turnaddr), turnport,
3701  pj_strbuf(&turnusername), pj_strbuf(&turnpassword));
3702  }
3703 }
3704 #endif
3705 
3706 /*!
3707  * \internal
3708  * \brief Calculates the elapsed time from issue of the first tx packet in an
3709  * rtp session and a specified time
3710  *
3711  * \param rtp pointer to the rtp struct with the transmitted rtp packet
3712  * \param delivery time of delivery - if NULL or zero value, will be ast_tvnow()
3713  *
3714  * \return time elapsed in milliseconds
3715  */
3716 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
3717 {
3718  struct timeval t;
3719  long ms;
3720 
3721  if (ast_tvzero(rtp->txcore)) {
3722  rtp->txcore = ast_tvnow();
3723  rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
3724  }
3725 
3726  t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
3727  if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
3728  ms = 0;
3729  }
3730  rtp->txcore = t;
3731 
3732  return (unsigned int) ms;
3733 }
3734 
3735 #ifdef HAVE_PJPROJECT
3736 /*!
3737  * \internal
3738  * \brief Creates an ICE session. Can be used to replace a destroyed ICE session.
3739  *
3740  * \param instance RTP instance for which the ICE session is being replaced
3741  * \param addr ast_sockaddr to use for adding RTP candidates to the ICE session
3742  * \param port port to use for adding RTP candidates to the ICE session
3743  * \param replace 0 when creating a new session, 1 when replacing a destroyed session
3744  *
3745  * \pre instance is locked
3746  *
3747  * \retval 0 on success
3748  * \retval -1 on failure
3749  */
3750 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
3751  int port, int replace)
3752 {
3753  pj_stun_config stun_config;
3754  pj_str_t ufrag, passwd;
3755  pj_status_t status;
3756  struct ice_wrap *ice_old;
3757  struct ice_wrap *ice;
3758  pj_ice_sess *real_ice = NULL;
3759  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3760 
3761  ao2_cleanup(rtp->ice_local_candidates);
3762  rtp->ice_local_candidates = NULL;
3763 
3764  ast_debug_ice(2, "(%p) ICE create%s\n", instance, replace ? " and replace" : "");
3765 
3767  if (!ice) {
3768  ast_rtp_ice_stop(instance);
3769  return -1;
3770  }
3771 
3773 
3774  pj_stun_config_init(&stun_config, &cachingpool.factory, 0, NULL, timer_heap);
3775  if (!stun_software_attribute) {
3776  stun_config.software_name = pj_str(NULL);
3777  }
3778 
3779  ufrag = pj_str(rtp->local_ufrag);
3780  passwd = pj_str(rtp->local_passwd);
3781 
3782  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3783  ao2_unlock(instance);
3784  /* Create an ICE session for ICE negotiation */
3785  status = pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN,
3786  rtp->ice_num_components, &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &real_ice);
3787  ao2_lock(instance);
3788  if (status == PJ_SUCCESS) {
3789  /* Safely complete linking the ICE session into the instance */
3790  real_ice->user_data = instance;
3791  ice->real_ice = real_ice;
3792  ice_old = rtp->ice;
3793  rtp->ice = ice;
3794  if (ice_old) {
3795  ao2_unlock(instance);
3796  ao2_ref(ice_old, -1);
3797  ao2_lock(instance);
3798  }
3799 
3800  /* Add all of the available candidates to the ICE session */
3801  rtp_add_candidates_to_ice(instance, rtp, addr, port, AST_RTP_ICE_COMPONENT_RTP,
3803 
3804  /* Only add the RTCP candidates to ICE when replacing the session and if
3805  * the ICE session contains more than just an RTP component. New sessions
3806  * handle this in a separate part of the setup phase */
3807  if (replace && rtp->rtcp && rtp->ice_num_components > 1) {
3808  rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us,
3811  }
3812 
3813  return 0;
3814  }
3815 
3816  /*
3817  * It is safe to unref this while instance is locked here.
3818  * It was not initialized with a real_ice pointer.
3819  */
3820  ao2_ref(ice, -1);
3821 
3822  ast_rtp_ice_stop(instance);
3823  return -1;
3824 
3825 }
3826 #endif
3827 
3828 static int rtp_allocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
3829 {
3830  int x, startplace;
3831 
3832  rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_CLOSED : STRICT_RTP_OPEN);
3833 
3834  /* Create a new socket for us to listen on and use */
3835  if ((rtp->s =
3836  create_new_socket("RTP",
3837  ast_sockaddr_is_ipv4(&rtp->bind_address) ? AF_INET :
3838  ast_sockaddr_is_ipv6(&rtp->bind_address) ? AF_INET6 : -1)) < 0) {
3839  ast_log(LOG_WARNING, "Failed to create a new socket for RTP instance '%p'\n", instance);
3840  return -1;
3841  }
3842 
3843  /* Now actually find a free RTP port to use */
3844  x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
3845  x = x & ~1;
3846  startplace = x;
3847 
3848  for (;;) {
3849  ast_sockaddr_set_port(&rtp->bind_address, x);
3850  /* Try to bind, this will tell us whether the port is available or not */
3851  if (!ast_bind(rtp->s, &rtp->bind_address)) {
3852  ast_debug_rtp(1, "(%p) RTP allocated port %d\n", instance, x);
3853  ast_rtp_instance_set_local_address(instance, &rtp->bind_address);
3854  ast_test_suite_event_notify("RTP_PORT_ALLOCATED", "Port: %d", x);
3855  break;
3856  }
3857 
3858  x += 2;
3859  if (x > rtpend) {
3860  x = (rtpstart + 1) & ~1;
3861  }
3862 
3863  /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
3864  if (x == startplace || (errno != EADDRINUSE && errno != EACCES)) {
3865  ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
3866  close(rtp->s);
3867  rtp->s = -1;
3868  return -1;
3869  }
3870  }
3871 
3872 #ifdef HAVE_PJPROJECT
3873  /* Initialize synchronization aspects */
3874  ast_cond_init(&rtp->cond, NULL);
3875 
3876  generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
3877  generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
3878 
3879  /* Create an ICE session for ICE negotiation */
3880  if (icesupport) {
3881  rtp->ice_num_components = 2;
3882  ast_debug_ice(2, "(%p) ICE creating session %s (%d)\n", instance,
3883  ast_sockaddr_stringify(&rtp->bind_address), x);
3884  if (ice_create(instance, &rtp->bind_address, x, 0)) {
3885  ast_log(LOG_NOTICE, "(%p) ICE failed to create session\n", instance);
3886  } else {
3887  rtp->ice_port = x;
3888  ast_sockaddr_copy(&rtp->ice_original_rtp_addr, &rtp->bind_address);
3889  }
3890  }
3891 #endif
3892 
3893 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3894  rtp->rekeyid = -1;
3895  rtp->dtls.timeout_timer = -1;
3896 #endif
3897 
3898  return 0;
3899 }
3900 
3901 static void rtp_deallocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
3902 {
3903  int saved_rtp_s = rtp->s;
3904 #ifdef HAVE_PJPROJECT
3905  struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
3906  struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
3907 #endif
3908 
3909 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3910  ast_rtp_dtls_stop(instance);
3911 #endif
3912 
3913  /* Close our own socket so we no longer get packets */
3914  if (rtp->s > -1) {
3915  close(rtp->s);
3916  rtp->s = -1;
3917  }
3918 
3919  /* Destroy RTCP if it was being used */
3920  if (rtp->rtcp && rtp->rtcp->s > -1) {
3921  if (saved_rtp_s != rtp->rtcp->s) {
3922  close(rtp->rtcp->s);
3923  }
3924  rtp->rtcp->s = -1;
3925  }
3926 
3927 #ifdef HAVE_PJPROJECT
3929 
3930  /*
3931  * The instance lock is already held.
3932  *
3933  * Destroy the RTP TURN relay if being used
3934  */
3935  if (rtp->turn_rtp) {
3936  rtp->turn_state = PJ_TURN_STATE_NULL;
3937 
3938  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3939  ao2_unlock(instance);
3940  pj_turn_sock_destroy(rtp->turn_rtp);
3941  ao2_lock(instance);
3942  while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
3943  ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
3944  }
3945  rtp->turn_rtp = NULL;
3946  }
3947 
3948  /* Destroy the RTCP TURN relay if being used */
3949  if (rtp->turn_rtcp) {
3950  rtp->turn_state = PJ_TURN_STATE_NULL;
3951 
3952  /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3953  ao2_unlock(instance);
3954  pj_turn_sock_destroy(rtp->turn_rtcp);
3955  ao2_lock(instance);
3956  while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
3957  ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
3958  }
3959  rtp->turn_rtcp = NULL;
3960  }
3961 
3962  ast_debug_ice(2, "(%p) ICE RTP transport deallocating\n", instance);
3963  /* Destroy any ICE session */
3964  ast_rtp_ice_stop(instance);
3965 
3966  /* Destroy any candidates */
3967  if (rtp->ice_local_candidates) {
3968  ao2_ref(rtp->ice_local_candidates, -1);
3969  rtp->ice_local_candidates = NULL;
3970  }
3971 
3972  if (rtp->ice_active_remote_candidates) {
3973  ao2_ref(rtp->ice_active_remote_candidates, -1);
3974  rtp->ice_active_remote_candidates = NULL;
3975  }
3976 
3977  if (rtp->ioqueue) {
3978  /*
3979  * We cannot hold the instance lock because we could wait
3980  * for the ioqueue thread to die and we might deadlock as
3981  * a result.
3982  */
3983  ao2_unlock(instance);
3984  rtp_ioqueue_thread_remove(rtp->ioqueue);
3985  ao2_lock(instance);
3986  rtp->ioqueue = NULL;
3987  }
3988 #endif
3989 }
3990 
3991 /*! \pre instance is locked */
3992 static int ast_rtp_new(struct ast_rtp_instance *instance,
3993  struct ast_sched_context *sched, struct ast_sockaddr *addr,
3994  void *data)
3995 {
3996  struct ast_rtp *rtp = NULL;
3997 
3998  /* Create a new RTP structure to hold all of our data */
3999  if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
4000  return -1;
4001  }
4002 
4003  /* Set default parameters on the newly created RTP structure */
4004  rtp->ssrc = ast_random();
4005  ast_uuid_generate_str(rtp->cname, sizeof(rtp->cname));
4006  rtp->seqno = ast_random() & 0x7fff;
4007  rtp->expectedrxseqno = -1;
4008  rtp->expectedseqno = -1;
4009  rtp->sched = sched;
4010  ast_sockaddr_copy(&rtp->bind_address, addr);
4011 
4012  /* Transport creation operations can grab the RTP data from the instance, so set it */
4013  ast_rtp_instance_set_data(instance, rtp);
4014 
4015  if (rtp_allocate_transport(instance, rtp)) {
4016  return -1;
4017  }
4018 
4019  if (AST_VECTOR_INIT(&rtp->ssrc_mapping, 1)) {
4020  return -1;
4021  }
4022 
4023  if (AST_VECTOR_INIT(&rtp->transport_wide_cc.packet_statistics, 0)) {
4024  return -1;
4025  }
4026  rtp->transport_wide_cc.schedid = -1;
4027 
4029  rtp->lastrxformat = ao2_bump(ast_format_none);
4030  rtp->lasttxformat = ao2_bump(ast_format_none);
4031  rtp->stream_num = -1;
4032 
4033  return 0;
4034 }
4035 
4036 /*!
4037  * \brief SSRC mapping comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
4038  *
4039  * \param elem Element to compare against
4040  * \param value Value to compare with the vector element.
4041  *
4042  * \return 0 if element does not match.
4043  * \return Non-zero if element matches.
4044  */
4045 #define SSRC_MAPPING_ELEM_CMP(elem, value) ((elem).instance == (value))
4046 
4047 /*! \pre instance is locked */
4048 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
4049 {
4050  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4051 
4052  if (rtp->bundled) {
4053  struct ast_rtp *bundled_rtp;
4054 
4055  /* We can't hold our instance lock while removing ourselves from the parent */
4056  ao2_unlock(instance);
4057 
4058  ao2_lock(rtp->bundled);
4059  bundled_rtp = ast_rtp_instance_get_data(rtp->bundled);
4061  ao2_unlock(rtp->bundled);
4062 
4063  ao2_lock(instance);
4064  ao2_ref(rtp->bundled, -1);
4065  }
4066 
4067  rtp_deallocate_transport(instance, rtp);
4068 
4069  /* Destroy the smoother that was smoothing out audio if present */
4070  if (rtp->smoother) {
4071  ast_smoother_free(rtp->smoother);
4072  }
4073 
4074  /* Destroy RTCP if it was being used */
4075  if (rtp->rtcp) {
4076  /*
4077  * It is not possible for there to be an active RTCP scheduler
4078  * entry at this point since it holds a reference to the
4079  * RTP instance while it's active.
4080  */
4081  ast_free(rtp->rtcp->local_addr_str);
4082  ast_free(rtp->rtcp);
4083  }
4084 
4085  /* Destroy RED if it was being used */
4086  if (rtp->red) {
4087  ao2_unlock(instance);
4088  AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
4089  ao2_lock(instance);
4090  ast_free(rtp->red);
4091  rtp->red = NULL;
4092  }
4093 
4094  /* Destroy the send buffer if it was being used */
4095  if (rtp->send_buffer) {
4096  ast_data_buffer_free(rtp->send_buffer);
4097  }
4098 
4099  /* Destroy the recv buffer if it was being used */
4100  if (rtp->recv_buffer) {
4101  ast_data_buffer_free(rtp->recv_buffer);
4102  }
4103 
4104  AST_VECTOR_FREE(&rtp->transport_wide_cc.packet_statistics);
4105 
4106  ao2_cleanup(rtp->lasttxformat);
4107  ao2_cleanup(rtp->lastrxformat);
4108  ao2_cleanup(rtp->f.subclass.format);
4109  AST_VECTOR_FREE(&rtp->ssrc_mapping);
4110  AST_VECTOR_FREE(&rtp->missing_seqno);
4111 
4112  /* Finally destroy ourselves */
4113  ast_free(rtp);
4114 
4115  return 0;
4116 }
4117 
4118 /*! \pre instance is locked */
4119 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
4120 {
4121  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4122  rtp->dtmfmode = dtmf_mode;
4123  return 0;
4124 }
4125 
4126 /*! \pre instance is locked */
4128 {
4129  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4130  return rtp->dtmfmode;
4131 }
4132 
4133 /*! \pre instance is locked */
4134 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
4135 {
4136  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4137  struct ast_sockaddr remote_address = { {0,} };
4138  int hdrlen = 12, res = 0, i = 0, payload = 101;
4139  char data[256];
4140  unsigned int *rtpheader = (unsigned int*)data;
4141 
4142  ast_rtp_instance_get_remote_address(instance, &remote_address);
4143 
4144  /* If we have no remote address information bail out now */
4145  if (ast_sockaddr_isnull(&remote_address)) {
4146  return -1;
4147  }
4148 
4149  /* Convert given digit into what we want to transmit */
4150  if ((digit <= '9') && (digit >= '0')) {
4151  digit -= '0';
4152  } else if (digit == '*') {
4153  digit = 10;
4154  } else if (digit == '#') {
4155  digit = 11;
4156  } else if ((digit >= 'A') && (digit <= 'D')) {
4157  digit = digit - 'A' + 12;
4158  } else if ((digit >= 'a') && (digit <= 'd')) {
4159  digit = digit - 'a' + 12;
4160  } else {
4161  ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
4162  return -1;
4163  }
4164 
4165  /* Grab the payload that they expect the RFC2833 packet to be received in */
4167 
4168  rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
4169  rtp->send_duration = 160;
4170  rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
4171  rtp->lastdigitts = rtp->lastts + rtp->send_duration;
4172 
4173  /* Create the actual packet that we will be sending */
4174  rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
4175  rtpheader[1] = htonl(rtp->lastdigitts);
4176  rtpheader[2] = htonl(rtp->ssrc);
4177 
4178  /* Actually send the packet */
4179  for (i = 0; i < 2; i++) {
4180  int ice;
4181 
4182  rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4183  res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4184  if (res < 0) {
4185  ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4186  ast_sockaddr_stringify(&remote_address),
4187  strerror(errno));
4188  }
4189  if (rtp_debug_test_addr(&remote_address)) {
4190  ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4191  ast_sockaddr_stringify(&remote_address),
4192  ice ? " (via ICE)" : "",
4193  payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4194  }
4195  rtp->seqno++;
4196  rtp->send_duration += 160;
4197  rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
4198  }
4199 
4200  /* Record that we are in the process of sending a digit and information needed to continue doing so */
4201  rtp->sending_digit = 1;
4202  rtp->send_digit = digit;
4203  rtp->send_payload = payload;
4204 
4205  return 0;
4206 }
4207 
4208 /*! \pre instance is locked */
4209 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
4210 {
4211  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4212  struct ast_sockaddr remote_address = { {0,} };
4213  int hdrlen = 12, res = 0;
4214  char data[256];
4215  unsigned int *rtpheader = (unsigned int*)data;
4216  int ice;
4217 
4218  ast_rtp_instance_get_remote_address(instance, &remote_address);
4219 
4220  /* Make sure we know where the other side is so we can send them the packet */
4221  if (ast_sockaddr_isnull(&remote_address)) {
4222  return -1;
4223  }
4224 
4225  /* Actually create the packet we will be sending */
4226  rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4227  rtpheader[1] = htonl(rtp->lastdigitts);
4228  rtpheader[2] = htonl(rtp->ssrc);
4229  rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
4230 
4231  /* Boom, send it on out */
4232  res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4233  if (res < 0) {
4234  ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4235  ast_sockaddr_stringify(&remote_address),
4236  strerror(errno));
4237  }
4238 
4239  if (rtp_debug_test_addr(&remote_address)) {
4240  ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4241  ast_sockaddr_stringify(&remote_address),
4242  ice ? " (via ICE)" : "",
4243  rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4244  }
4245 
4246  /* And now we increment some values for the next time we swing by */
4247  rtp->seqno++;
4248  rtp->send_duration += 160;
4249  rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
4250 
4251  return 0;
4252 }
4253 
4254 /*! \pre instance is locked */
4255 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
4256 {
4257  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4258  struct ast_sockaddr remote_address = { {0,} };
4259  int hdrlen = 12, res = -1, i = 0;
4260  char data[256];
4261  unsigned int *rtpheader = (unsigned int*)data;
4262  unsigned int measured_samples;
4263 
4264  ast_rtp_instance_get_remote_address(instance, &remote_address);
4265 
4266  /* Make sure we know where the remote side is so we can send them the packet we construct */
4267  if (ast_sockaddr_isnull(&remote_address)) {
4268  goto cleanup;
4269  }
4270 
4271  /* Convert the given digit to the one we are going to send */
4272  if ((digit <= '9') && (digit >= '0')) {
4273  digit -= '0';
4274  } else if (digit == '*') {
4275  digit = 10;
4276  } else if (digit == '#') {
4277  digit = 11;
4278  } else if ((digit >= 'A') && (digit <= 'D')) {
4279  digit = digit - 'A' + 12;
4280  } else if ((digit >= 'a') && (digit <= 'd')) {
4281  digit = digit - 'a' + 12;
4282  } else {
4283  ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
4284  goto cleanup;
4285  }
4286 
4287  rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
4288 
4289  if (duration > 0 && (measured_samples = duration * ast_rtp_get_rate(rtp->f.subclass.format) / 1000) > rtp->send_duration) {
4290  ast_debug_rtp(2, "(%p) RTP adjusting final end duration from %d to %u\n",
4291  instance, rtp->send_duration, measured_samples);
4292  rtp->send_duration = measured_samples;
4293  }
4294 
4295  /* Construct the packet we are going to send */
4296  rtpheader[1] = htonl(rtp->lastdigitts);
4297  rtpheader[2] = htonl(rtp->ssrc);
4298  rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4299  rtpheader[3] |= htonl((1 << 23));
4300 
4301  /* Send it 3 times, that's the magical number */
4302  for (i = 0; i < 3; i++) {
4303  int ice;
4304 
4305  rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4306 
4307  res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4308 
4309  if (res < 0) {
4310  ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4311  ast_sockaddr_stringify(&remote_address),
4312  strerror(errno));
4313  }
4314 
4315  if (rtp_debug_test_addr(&remote_address)) {
4316  ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4317  ast_sockaddr_stringify(&remote_address),
4318  ice ? " (via ICE)" : "",
4319  rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4320  }
4321 
4322  rtp->seqno++;
4323  }
4324  res = 0;
4325 
4326  /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
4327  rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
4328 
4329  /* Reset the smoother as the delivery time stored in it is now out of date */
4330  if (rtp->smoother) {
4331  ast_smoother_free(rtp->smoother);
4332  rtp->smoother = NULL;
4333  }
4334 cleanup:
4335  rtp->sending_digit = 0;
4336  rtp->send_digit = 0;
4337 
4338  return res;
4339 }
4340 
4341 /*! \pre instance is locked */
4342 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
4343 {
4344  return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
4345 }
4346 
4347 /*! \pre instance is locked */
4348 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
4349 {
4350  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4351 
4352  /* We simply set this bit so that the next packet sent will have the marker bit turned on */
4354  ast_debug_rtp(3, "(%p) RTP setting the marker bit due to a source update\n", instance);
4355 
4356  return;
4357 }
4358 
4359 /*! \pre instance is locked */
4360 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
4361 {
4362  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4363  struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 0);
4364  struct ast_srtp *rtcp_srtp = ast_rtp_instance_get_srtp(instance, 1);
4365  unsigned int ssrc = ast_random();
4366 
4367  if (rtp->lastts) {
4368  /* We simply set this bit so that the next packet sent will have the marker bit turned on */
4370  }
4371 
4372  ast_debug_rtp(3, "(%p) RTP changing ssrc from %u to %u due to a source change\n",
4373  instance, rtp->ssrc, ssrc);
4374 
4375  if (srtp) {
4376  ast_debug_rtp(3, "(%p) RTP changing ssrc for SRTP from %u to %u\n",
4377  instance, rtp->ssrc, ssrc);
4378  res_srtp->change_source(srtp, rtp->ssrc, ssrc);
4379  if (rtcp_srtp != srtp) {
4380  res_srtp->change_source(rtcp_srtp, rtp->ssrc, ssrc);
4381  }
4382  }
4383 
4384  rtp->ssrc = ssrc;
4385 
4386  /* Since the source is changing, we don't know what sequence number to expect next */
4387  rtp->expectedrxseqno = -1;
4388 
4389  return;
4390 }
4391 
4392 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
4393 {
4394  unsigned int sec, usec, frac;
4395  sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
4396  usec = tv.tv_usec;
4397  /*
4398  * Convert usec to 0.32 bit fixed point without overflow.
4399  *
4400  * = usec * 2^32 / 10^6
4401  * = usec * 2^32 / (2^6 * 5^6)
4402  * = usec * 2^26 / 5^6
4403  *
4404  * The usec value needs 20 bits to represent 999999 usec. So
4405  * splitting the 2^26 to get the most precision using 32 bit
4406  * values gives:
4407  *
4408  * = ((usec * 2^12) / 5^6) * 2^14
4409  *
4410  * Splitting the division into two stages preserves all the
4411  * available significant bits of usec over doing the division
4412  * all at once.
4413  *
4414  * = ((((usec * 2^12) / 5^3) * 2^7) / 5^3) * 2^7
4415  */
4416  frac = ((((usec << 12) / 125) << 7) / 125) << 7;
4417  *msw = sec;
4418  *lsw = frac;
4419 }
4420 
4421 static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv)
4422 {
4423  tv->tv_sec = msw - 2208988800u;
4424  /* Reverse the sequence in timeval2ntp() */
4425  tv->tv_usec = ((((lsw >> 7) * 125) >> 7) * 125) >> 12;
4426 }
4427 
4429  unsigned int *lost_packets,
4430  int *fraction_lost)
4431 {
4432  unsigned int extended_seq_no;
4433  unsigned int expected_packets;
4434  unsigned int expected_interval;
4435  unsigned int received_interval;
4436  int lost_interval;
4437 
4438  /* Compute statistics */
4439  extended_seq_no = rtp->cycles + rtp->lastrxseqno;
4440  expected_packets = extended_seq_no - rtp->seedrxseqno + 1;
4441  if (rtp->rxcount > expected_packets) {
4442  expected_packets += rtp->rxcount - expected_packets;
4443  }
4444  *lost_packets = expected_packets - rtp->rxcount;
4445  expected_interval = expected_packets - rtp->rtcp->expected_prior;
4446  received_interval = rtp->rxcount - rtp->rtcp->received_prior;
4447  if (received_interval > expected_interval) {
4448  /* If we receive some late packets it is possible for the packets
4449  * we received in this interval to exceed the number we expected.
4450  * We update the expected so that the packet loss calculations
4451  * show that no packets are lost.
4452  */
4453  expected_interval = received_interval;
4454  }
4455  lost_interval = expected_interval - received_interval;
4456  if (expected_interval == 0 || lost_interval <= 0) {
4457  *fraction_lost = 0;
4458  } else {
4459  *fraction_lost = (lost_interval << 8) / expected_interval;
4460  }
4461 
4462  /* Update RTCP statistics */
4463  rtp->rtcp->received_prior = rtp->rxcount;
4464  rtp->rtcp->expected_prior = expected_packets;
4465 
4466  /*
4467  * While rxlost represents the number of packets lost since the last report was sent, for
4468  * the calculations below it should be thought of as a single sample. Thus min/max are the
4469  * lowest/highest sample value seen, and the mean is the average number of packets lost
4470  * between each report. As such rxlost_count only needs to be incremented per report.
4471  */
4472  if (lost_interval <= 0) {
4473  rtp->rtcp->rxlost = 0;
4474  } else {
4475  rtp->rtcp->rxlost = lost_interval;
4476  }
4477  if (rtp->rtcp->rxlost_count == 0) {
4478  rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4479  }
4480  if (lost_interval && lost_interval < rtp->rtcp->minrxlost) {
4481  rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4482  }
4483  if (lost_interval > rtp->rtcp->maxrxlost) {
4484  rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
4485  }
4486 
4487  calc_mean_and_standard_deviation(rtp->rtcp->rxlost, &rtp->rtcp->normdev_rxlost,
4488  &rtp->rtcp->stdev_rxlost, &rtp->rtcp->rxlost_count);
4489 }
4490 
4491 static int ast_rtcp_generate_report(struct ast_rtp_instance *instance, unsigned char *rtcpheader,
4492  struct ast_rtp_rtcp_report *rtcp_report, int *sr)
4493 {
4494  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4495  int len = 0;
4496  struct timeval now;
4497  unsigned int now_lsw;
4498  unsigned int now_msw;
4499  unsigned int lost_packets;
4500  int fraction_lost;
4501  struct timeval dlsr = { 0, };
4502  struct ast_rtp_rtcp_report_block *report_block = NULL;
4503 
4504  if (!rtp || !rtp->rtcp) {
4505  return 0;
4506  }
4507 
4508  if (ast_sockaddr_isnull(&rtp->rtcp->them)) { /* This'll stop rtcp for this rtp session */
4509  /* RTCP was stopped. */
4510  return 0;
4511  }
4512 
4513  if (!rtcp_report) {
4514  return 1;
4515  }
4516 
4517  *sr = rtp->txcount > rtp->rtcp->lastsrtxcount ? 1 : 0;
4518 
4519  /* Compute statistics */
4520  calculate_lost_packet_statistics(rtp, &lost_packets, &fraction_lost);
4521 
4522  gettimeofday(&now, NULL);
4523  rtcp_report->reception_report_count = rtp->themssrc_valid ? 1 : 0;
4524  rtcp_report->ssrc = rtp->ssrc;
4525  rtcp_report->type = *sr ? RTCP_PT_SR : RTCP_PT_RR;
4526  if (*sr) {
4527  rtcp_report->sender_information.ntp_timestamp = now;
4528  rtcp_report->sender_information.rtp_timestamp = rtp->lastts;
4529  rtcp_report->sender_information.packet_count = rtp->txcount;
4530  rtcp_report->sender_information.octet_count = rtp->txoctetcount;
4531  }
4532 
4533  if (rtp->themssrc_valid) {
4534  report_block = ast_calloc(1, sizeof(*report_block));
4535  if (!report_block) {
4536  return 1;
4537  }
4538 
4539  rtcp_report->report_block[0] = report_block;
4540  report_block->source_ssrc = rtp->themssrc;
4541  report_block->lost_count.fraction = (fraction_lost & 0xff);
4542  report_block->lost_count.packets = (lost_packets & 0xffffff);
4543  report_block->highest_seq_no = (rtp->cycles | (rtp->lastrxseqno & 0xffff));
4544  report_block->ia_jitter = (unsigned int)(rtp->rxjitter * ast_rtp_get_rate(rtp->f.subclass.format));
4545  report_block->lsr = rtp->rtcp->themrxlsr;
4546  /* If we haven't received an SR report, DLSR should be 0 */
4547  if (!ast_tvzero(rtp->rtcp->rxlsr)) {
4548  timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
4549  report_block->dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000;
4550  }
4551  }
4552  timeval2ntp(rtcp_report->sender_information.ntp_timestamp, &now_msw, &now_lsw);
4553  put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc)); /* Our SSRC */
4554  len += 8;
4555  if (*sr) {
4556  put_unaligned_uint32(rtcpheader + len, htonl(now_msw)); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970 */
4557  put_unaligned_uint32(rtcpheader + len + 4, htonl(now_lsw)); /* now, LSW */
4558  put_unaligned_uint32(rtcpheader + len + 8, htonl(rtcp_report->sender_information.rtp_timestamp));
4559  put_unaligned_uint32(rtcpheader + len + 12, htonl(rtcp_report->sender_information.packet_count));
4560  put_unaligned_uint32(rtcpheader + len + 16, htonl(rtcp_report->sender_information.octet_count));
4561  len += 20;
4562  }
4563  if (report_block) {
4564  put_unaligned_uint32(rtcpheader + len, htonl(report_block->source_ssrc)); /* Their SSRC */
4565  put_unaligned_uint32(rtcpheader + len + 4, htonl((report_block->lost_count.fraction << 24) | report_block->lost_count.packets));
4566  put_unaligned_uint32(rtcpheader + len + 8, htonl(report_block->highest_seq_no));
4567  put_unaligned_uint32(rtcpheader + len + 12, htonl(report_block->ia_jitter));
4568  put_unaligned_uint32(rtcpheader + len + 16, htonl(report_block->lsr));
4569  put_unaligned_uint32(rtcpheader + len + 20, htonl(report_block->dlsr));
4570  len += 24;
4571  }
4572 
4573  put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (rtcp_report->reception_report_count << 24)
4574  | ((*sr ? RTCP_PT_SR : RTCP_PT_RR) << 16) | ((len/4)-1)));
4575 
4576  return len;
4577 }
4578 
4580  struct ast_rtp_rtcp_report *rtcp_report, struct ast_sockaddr remote_address, int ice, int sr)
4581 {
4582  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4583  struct ast_rtp_rtcp_report_block *report_block = NULL;
4584  RAII_VAR(struct ast_json *, message_blob, NULL, ast_json_unref);
4585 
4586  if (!rtp || !rtp->rtcp) {
4587  return 0;
4588  }
4589 
4590  if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4591  return 0;
4592  }
4593 
4594  if (!rtcp_report) {
4595  return -1;
4596  }
4597 
4598  report_block = rtcp_report->report_block[0];
4599 
4600  if (sr) {
4601  rtp->rtcp->txlsr = rtcp_report->sender_information.ntp_timestamp;
4602  rtp->rtcp->sr_count++;
4603  rtp->rtcp->lastsrtxcount = rtp->txcount;
4604  } else {
4605  rtp->rtcp->rr_count++;
4606  }
4607 
4608  if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
4609  ast_verbose("* Sent RTCP %s to %s%s\n", sr ? "SR" : "RR",
4610  ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
4611  ast_verbose(" Our SSRC: %u\n", rtcp_report->ssrc);
4612  if (sr) {
4613  ast_verbose(" Sent(NTP): %u.%06u\n",
4614  (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
4615  (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec);
4616  ast_verbose(" Sent(RTP): %u\n", rtcp_report->sender_information.rtp_timestamp);
4617  ast_verbose(" Sent packets: %u\n", rtcp_report->sender_information.packet_count);
4618  ast_verbose(" Sent octets: %u\n", rtcp_report->sender_information.octet_count);
4619  }
4620  if (report_block) {
4621  ast_verbose(" Report block:\n");
4622  ast_verbose(" Their SSRC: %u\n", report_block->source_ssrc);
4623  ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction);
4624  ast_verbose(" Cumulative loss: %u\n", report_block->lost_count.packets);
4625  ast_verbose(" Highest seq no: %u\n", report_block->highest_seq_no);
4626  ast_verbose(" IA jitter: %.4f\n", (double)report_block->ia_jitter / ast_rtp_get_rate(rtp->f.subclass.format));
4627  ast_verbose(" Their last SR: %u\n", report_block->lsr);
4628  ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(report_block->dlsr / 65536.0));
4629  }
4630  }
4631 
4632  message_blob = ast_json_pack("{s: s, s: s}",
4633  "to", ast_sockaddr_stringify(&remote_address),
4634  "from", rtp->rtcp->local_addr_str);
4636  rtcp_report, message_blob);
4637 
4638  return 1;
4639 }
4640 
4641 static int ast_rtcp_generate_sdes(struct ast_rtp_instance *instance, unsigned char *rtcpheader,
4642  struct ast_rtp_rtcp_report *rtcp_report)
4643 {
4644  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4645  int len = 0;
4646  uint16_t sdes_packet_len_bytes;
4647  uint16_t sdes_packet_len_rounded;
4648 
4649  if (!rtp || !rtp->rtcp) {
4650  return 0;
4651  }
4652 
4653  if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4654  return 0;
4655  }
4656 
4657  if (!rtcp_report) {
4658  return -1;
4659  }
4660 
4661  sdes_packet_len_bytes =
4662  4 + /* RTCP Header */
4663  4 + /* SSRC */
4664  1 + /* Type (CNAME) */
4665  1 + /* Text Length */
4666  AST_UUID_STR_LEN /* Text and NULL terminator */
4667  ;
4668 
4669  /* Round to 32 bit boundary */
4670  sdes_packet_len_rounded = (sdes_packet_len_bytes + 3) & ~0x3;
4671 
4672  put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | ((sdes_packet_len_rounded / 4) - 1)));
4673  put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc));
4674  rtcpheader[8] = 0x01; /* CNAME */
4675  rtcpheader[9] = AST_UUID_STR_LEN - 1; /* Number of bytes of text */
4676  memcpy(rtcpheader + 10, rtp->cname, AST_UUID_STR_LEN);
4677  len += 10 + AST_UUID_STR_LEN;
4678 
4679  /* Padding - Note that we don't set the padded bit on the packet. From
4680  * RFC 3550 Section 6.5:
4681  *
4682  * No length octet follows the null item type octet, but additional null
4683  * octets MUST be included if needd to pad until the next 32-bit
4684  * boundary. Note that this padding is separate from that indicated by
4685  * the P bit in the RTCP header.
4686  *
4687  * These bytes will already be zeroed out during array initialization.
4688  */
4689  len += (sdes_packet_len_rounded - sdes_packet_len_bytes);
4690 
4691  return len;
4692 }
4693 
4694 /* Lock instance before calling this if it isn't already
4695  *
4696  * If successful, the overall packet length is returned
4697  * If not, then 0 is returned
4698  */
4699 static int ast_rtcp_generate_compound_prefix(struct ast_rtp_instance *instance, unsigned char *rtcpheader,
4700  struct ast_rtp_rtcp_report *report, int *sr)
4701 {
4702  int packet_len = 0;
4703  int res;
4704 
4705  /* Every RTCP packet needs to be sent out with a SR/RR and SDES prefixing it.
4706  * At the end of this function, rtcpheader should contain both of those packets,
4707  * and will return the length of the overall packet. This can be used to determine
4708  * where further packets can be inserted in the compound packet.
4709  */
4710  res = ast_rtcp_generate_report(instance, rtcpheader, report, sr);
4711 
4712  if (res == 0 || res == 1) {
4713  ast_debug_rtcp(1, "(%p) RTCP failed to generate %s report!\n", instance, sr ? "SR" : "RR");
4714  return 0;
4715  }
4716 
4717  packet_len += res;
4718 
4719  res = ast_rtcp_generate_sdes(instance, rtcpheader + packet_len, report);
4720 
4721  if (res == 0 || res == 1) {
4722  ast_debug_rtcp(1, "(%p) RTCP failed to generate SDES!\n", instance);
4723  return 0;
4724  }
4725 
4726  return packet_len + res;
4727 }
4728 
4729 static int ast_rtcp_generate_nack(struct ast_rtp_instance *instance, unsigned char *rtcpheader)
4730 {
4731  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4732  int packet_len;
4733  int blp_index = -1;
4734  int current_seqno;
4735  unsigned int fci = 0;
4736  size_t remaining_missing_seqno;
4737 
4738  if (!rtp || !rtp->rtcp) {
4739  return 0;
4740  }
4741 
4742  if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4743  return 0;
4744  }
4745 
4746  current_seqno = rtp->expectedrxseqno;
4747  remaining_missing_seqno = AST_VECTOR_SIZE(&rtp->missing_seqno);
4748  packet_len = 12; /* The header length is 12 (version line, packet source SSRC, media source SSRC) */
4749 
4750  /* If there are no missing sequence numbers then don't bother sending a NACK needlessly */
4751  if (!remaining_missing_seqno) {
4752  return 0;
4753  }
4754 
4755  /* This iterates through the possible forward sequence numbers seeing which ones we
4756  * have no packet for, adding it to the NACK until we are out of missing packets.
4757  */
4758  while (remaining_missing_seqno) {
4759  int *missing_seqno;
4760 
4761  /* On the first entry to this loop blp_index will be -1, so this will become 0
4762  * and the sequence number will be placed into the packet as the PID.
4763  */
4764  blp_index++;
4765 
4766  missing_seqno = AST_VECTOR_GET_CMP(&rtp->missing_seqno, current_seqno,
4767  find_by_value);
4768  if (missing_seqno) {
4769  /* We hit the max blp size, reset */
4770  if (blp_index >= 17) {
4771  put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));
4772  fci = 0;
4773  blp_index = 0;
4774  packet_len += 4;
4775  }
4776 
4777  if (blp_index == 0) {
4778  fci |= (current_seqno << 16);
4779  } else {
4780  fci |= (1 << (blp_index - 1));
4781  }
4782 
4783  /* Since we've used a missing sequence number, we're down one */
4784  remaining_missing_seqno--;
4785  }
4786 
4787  /* Handle cycling of the sequence number */
4788  current_seqno++;
4789  if (current_seqno == SEQNO_CYCLE_OVER) {
4790  current_seqno = 0;
4791  }
4792  }
4793 
4794  put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));
4795  packet_len += 4;
4796 
4797  /* Length MUST be 2+n, where n is the number of NACKs. Same as length in words minus 1 */
4798  put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (AST_RTP_RTCP_FMT_NACK << 24)
4799  | (AST_RTP_RTCP_RTPFB << 16) | ((packet_len / 4) - 1)));
4800  put_unaligned_uint32(rtcpheader + 4, htonl(rtp->ssrc));
4801  put_unaligned_uint32(rtcpheader + 8, htonl(rtp->themssrc));
4802 
4803  return packet_len;
4804 }
4805 
4806 /*!
4807  * \brief Write a RTCP packet to the far end
4808  *
4809  * \note Decide if we are going to send an SR (with Reception Block) or RR
4810  * RR is sent if we have not sent any rtp packets in the previous interval
4811  *
4812  * Scheduler callback
4813  */
4814 static int ast_rtcp_write(const void *data)
4815 {
4816  struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
4817  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4818  int res;
4819  int sr = 0;
4820  int packet_len = 0;
4821  int ice;
4822  struct ast_sockaddr remote_address = { { 0, } };
4823  unsigned char *rtcpheader;
4824  unsigned char bdata[AST_UUID_STR_LEN + 128] = ""; /* More than enough */
4825  RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
4827  ao2_cleanup);
4828 
4829  if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
4830  ao2_ref(instance, -1);
4831  return 0;
4832  }
4833 
4834  ao2_lock(instance);
4835  rtcpheader = bdata;
4836 
4837  res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
4838 
4839  if (res == 0 || res == 1) {
4840  goto cleanup;
4841  }
4842 
4843  packet_len += res;
4844 
4845  if (rtp->bundled) {
4846  ast_rtp_instance_get_remote_address(instance, &remote_address);
4847  } else {
4848  ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
4849  }
4850 
4851  res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
4852  if (res < 0) {
4853  ast_log(LOG_ERROR, "RTCP %s transmission error to %s, rtcp halted %s\n",
4854  sr ? "SR" : "RR",
4855  ast_sockaddr_stringify(&rtp->rtcp->them),
4856  strerror(errno));
4857  res = 0;
4858  } else {
4859  ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr);
4860  }
4861 
4862 cleanup:
4863  ao2_unlock(instance);
4864 
4865  if (!res) {
4866  /*
4867  * Not being rescheduled.
4868  */
4869  rtp->rtcp->schedid = -1;
4870  ao2_ref(instance, -1);
4871  }
4872 
4873  return res;
4874 }
4875 
4876 static void put_unaligned_time24(void *p, uint32_t time_msw, uint32_t time_lsw)
4877 {
4878  unsigned char *cp = p;
4879  uint32_t datum;
4880 
4881  /* Convert the time to 6.18 format */
4882  datum = (time_msw << 18) & 0x00fc0000;
4883  datum |= (time_lsw >> 14) & 0x0003ffff;
4884 
4885  cp[0] = datum >> 16;
4886  cp[1] = datum >> 8;
4887  cp[2] = datum;
4888 }
4889 
4890 /*! \pre instance is locked */
4891 static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
4892 {
4893  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4894  int pred, mark = 0;
4895  unsigned int ms = calc_txstamp(rtp, &frame->delivery);
4896  struct ast_sockaddr remote_address = { {0,} };
4897  int rate = ast_rtp_get_rate(frame->subclass.format) / 1000;
4898  unsigned int seqno;
4899 #ifdef TEST_FRAMEWORK
4901 #endif
4902 
4904  frame->samples /= 2;
4905  }
4906 
4907  if (rtp->sending_digit) {
4908  return 0;
4909  }
4910 
4911 #ifdef TEST_FRAMEWORK
4912  if (test && test->send_report) {
4913  test->send_report = 0;
4914  ast_rtcp_write(instance);
4915  return 0;
4916  }
4917 #endif
4918 
4919  if (frame->frametype == AST_FRAME_VOICE) {
4920  pred = rtp->lastts + frame->samples;
4921 
4922  /* Re-calculate last TS */
4923  rtp->lastts = rtp->lastts + ms * rate;
4924  if (ast_tvzero(frame->delivery)) {
4925  /* If this isn't an absolute delivery time, Check if it is close to our prediction,
4926  and if so, go with our prediction */
4927  if (abs((int)rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
4928  rtp->lastts = pred;
4929  } else {
4930  ast_debug_rtp(3, "(%p) RTP audio difference is %d, ms is %u\n",
4931  instance, abs((int)rtp->lastts - pred), ms);
4932  mark = 1;
4933  }
4934  }
4935  } else if (frame->frametype == AST_FRAME_VIDEO) {
4936  mark = frame->subclass.frame_ending;
4937  pred = rtp->lastovidtimestamp + frame->samples;
4938  /* Re-calculate last TS */
4939  rtp->lastts = rtp->lastts + ms * 90;
4940  /* If it's close to our prediction, go for it */
4941  if (ast_tvzero(frame->delivery)) {
4942  if (abs((int)rtp->lastts - pred) < 7200) {
4943  rtp->lastts = pred;
4944  rtp->lastovidtimestamp += frame->samples;
4945  } else {
4946  ast_debug_rtp(3, "(%p) RTP video difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n",
4947  instance, abs((int)rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
4948  rtp->lastovidtimestamp = rtp->lastts;
4949  }
4950  }
4951  } else {
4952  pred = rtp->lastotexttimestamp + frame->samples;
4953  /* Re-calculate last TS */
4954  rtp->lastts = rtp->lastts + ms;
4955  /* If it's close to our prediction, go for it */
4956  if (ast_tvzero(frame->delivery)) {
4957  if (abs((int)rtp->lastts - pred) < 7200) {
4958  rtp->lastts = pred;
4959  rtp->lastotexttimestamp += frame->samples;
4960  } else {
4961  ast_debug_rtp(3, "(%p) RTP other difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n",
4962  instance, abs((int)rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
4963  rtp->lastotexttimestamp = rtp->lastts;
4964  }
4965  }
4966  }
4967 
4968  /* If we have been explicitly told to set the marker bit then do so */
4969  if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
4970  mark = 1;
4972  }
4973 
4974  /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
4975  if (rtp->lastts > rtp->lastdigitts) {
4976  rtp->lastdigitts = rtp->lastts;
4977  }
4978 
4979  /* Assume that the sequence number we expect to use is what will be used until proven otherwise */
4980  seqno = rtp->seqno;
4981 
4982  /* If the frame contains sequence number information use it to influence our sequence number */
4984  if (rtp->expectedseqno != -1) {
4985  /* Determine where the frame from the core is in relation to where we expected */
4986  int difference = frame->seqno - rtp->expectedseqno;
4987 
4988  /* If there is a substantial difference then we've either got packets really out
4989  * of order, or the source is RTP and it has cycled. If this happens we resync
4990  * the sequence number adjustments to this frame. If we also have packet loss
4991  * things won't be reflected correctly but it will sort itself out after a bit.
4992  */
4993  if (abs(difference) > 100) {
4994  difference = 0;
4995  }
4996 
4997  /* Adjust the sequence number being used for this packet accordingly */
4998  seqno += difference;
4999 
5000  if (difference >= 0) {
5001  /* This frame is on time or in the future */
5002  rtp->expectedseqno = frame->seqno + 1;
5003  rtp->seqno += difference;
5004  }
5005  } else {
5006  /* This is the first frame with sequence number we've seen, so start keeping track */
5007  rtp->expectedseqno = frame->seqno + 1;
5008  }
5009  } else {
5010  rtp->expectedseqno = -1;
5011  }
5012 
5014  rtp->lastts = frame->ts * rate;
5015  }
5016 
5017  ast_rtp_instance_get_remote_address(instance, &remote_address);
5018 
5019  /* If we know the remote address construct a packet and send it out */
5020  if (!ast_sockaddr_isnull(&remote_address)) {
5021  int hdrlen = 12;
5022  int res;
5023  int ice;
5024  int ext = 0;
5025  int abs_send_time_id;
5026  int packet_len;
5027  unsigned char *rtpheader;
5028 
5029  /* If the abs-send-time extension has been negotiated determine how much space we need */
5031  if (abs_send_time_id != -1) {
5032  /* 4 bytes for the shared information, 1 byte for identifier, 3 bytes for abs-send-time */
5033  hdrlen += 8;
5034  ext = 1;
5035  }
5036 
5037  packet_len = frame->datalen + hdrlen;
5038  rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
5039 
5040  put_unaligned_uint32(rtpheader, htonl((2 << 30) | (ext << 28) | (codec << 16) | (seqno) | (mark << 23)));
5041  put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
5042  put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
5043 
5044  /* We assume right now that we will only ever have the abs-send-time extension in the packet
5045  * which simplifies things a bit.
5046  */
5047  if (abs_send_time_id != -1) {
5048  unsigned int now_msw;
5049  unsigned int now_lsw;
5050 
5051  /* This happens before being placed into the retransmission buffer so that when we
5052  * retransmit we only have to update the timestamp, not everything else.
5053  */
5054  put_unaligned_uint32(rtpheader + 12, htonl((0xBEDE << 16) | 1));
5055  rtpheader[16] = (abs_send_time_id << 4) | 2;
5056 
5057  timeval2ntp(ast_tvnow(), &now_msw, &now_lsw);
5058  put_unaligned_time24(rtpheader + 17, now_msw, now_lsw);
5059  }
5060 
5061  /* If retransmissions are enabled, we need to store this packet for future use */
5062  if (rtp->send_buffer) {
5063  struct ast_rtp_rtcp_nack_payload *payload;
5064 
5065  payload = ast_malloc(sizeof(*payload) + packet_len);
5066  if (payload) {
5067  payload->size = packet_len;
5068  memcpy(payload->buf, rtpheader, packet_len);
5069  if (ast_data_buffer_put(rtp->send_buffer, rtp->seqno, payload) == -1) {
5070  ast_free(payload);
5071  }
5072  }
5073  }
5074 
5075  res = rtp_sendto(instance, (void *)rtpheader, packet_len, 0, &remote_address, &ice);
5076  if (res < 0) {
5078  ast_debug_rtp(1, "(%p) RTP transmission error of packet %d to %s: %s\n",
5079  instance, rtp->seqno,
5080  ast_sockaddr_stringify(&remote_address),
5081  strerror(errno));
5083  /* Only give this error message once if we are not RTP debugging */
5085  ast_debug(0, "(%p) RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
5086  instance, ast_sockaddr_stringify(&remote_address));
5088  }
5089  } else {
5090  if (rtp->rtcp && rtp->rtcp->schedid < 0) {
5091  ast_debug_rtcp(1, "(%p) RTCP starting transmission\n", instance);
5092  ao2_ref(instance, +1);
5093  rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
5094  if (rtp->rtcp->schedid < 0) {
5095  ao2_ref(instance, -1);
5096  ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
5097  }
5098  }
5099  }
5100 
5101  if (rtp_debug_test_addr(&remote_address)) {
5102  ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
5103  ast_sockaddr_stringify(&remote_address),
5104  ice ? " (via ICE)" : "",
5105  codec, rtp->seqno, rtp->lastts, res - hdrlen);
5106  }
5107  }
5108 
5109  /* If the sequence number that has been used doesn't match what we expected then this is an out of
5110  * order late packet, so we don't need to increment as we haven't yet gotten the expected frame from
5111  * the core.
5112  */
5113  if (seqno == rtp->seqno) {
5114  rtp->seqno++;
5115  }
5116 
5117  return 0;
5118 }
5119 
5120 static struct ast_frame *red_t140_to_red(struct rtp_red *red)
5121 {
5122  unsigned char *data = red->t140red.data.ptr;
5123  int len = 0;
5124  int i;
5125 
5126  /* replace most aged generation */
5127  if (red->len[0]) {
5128  for (i = 1; i < red->num_gen+1; i++)
5129  len += red->len[i];
5130 
5131  memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
5132  }
5133 
5134  /* Store length of each generation and primary data length*/
5135  for (i = 0; i < red->num_gen; i++)
5136  red->len[i] = red->len[i+1];
5137  red->len[i] = red->t140.datalen;
5138 
5139  /* write each generation length in red header */
5140  len = red->hdrlen;
5141  for (i = 0; i < red->num_gen; i++) {
5142  len += data[i*4+3] = red->len[i];
5143  }
5144 
5145  /* add primary data to buffer */
5146  memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
5147  red->t140red.datalen = len + red->t140.datalen;
5148 
5149  /* no primary data and no generations to send */
5150  if (len == red->hdrlen && !red->t140.datalen) {
5151  return NULL;
5152  }
5153 
5154  /* reset t.140 buffer */
5155  red->t140.datalen = 0;
5156 
5157  return &red->t140red;
5158 }
5159 
5160 static void rtp_write_rtcp_fir(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *remote_address)
5161 {
5162  unsigned char *rtcpheader;
5163  unsigned char bdata[1024];
5164  int packet_len = 0;
5165  int fir_len = 20;
5166  int ice;
5167  int res;
5168  int sr;
5169  RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
5171  ao2_cleanup);
5172 
5173  if (!rtp || !rtp->rtcp) {
5174  return;
5175  }
5176 
5177  if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) {
5178  /*
5179  * RTCP was stopped.
5180  */
5181  return;
5182  }
5183 
5184  if (!rtp->themssrc_valid) {
5185  /* We don't know their SSRC value so we don't know who to update. */
5186  return;
5187  }
5188 
5189  /* Prepare RTCP FIR (PT=206, FMT=4) */
5190  rtp->rtcp->firseq++;
5191  if(rtp->rtcp->firseq == 256) {
5192  rtp->rtcp->firseq = 0;
5193  }
5194 
5195  rtcpheader = bdata;
5196 
5197  ao2_lock(instance);
5198  res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
5199 
5200  if (res == 0 || res == 1) {
5201  ao2_unlock(instance);
5202  return;
5203  }
5204 
5205  packet_len += res;
5206 
5207  put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((fir_len/4)-1)));
5208  put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc));
5209  put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(rtp->themssrc));
5210  put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(rtp->themssrc)); /* FCI: SSRC */
5211  put_unaligned_uint32(rtcpheader + packet_len + 16, htonl(rtp->rtcp->firseq << 24)); /* FCI: Sequence number */
5212  res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + fir_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5213  if (res < 0) {
5214  ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n", strerror(errno));
5215  } else {
5216  ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr);
5217  }
5218 
5219  ao2_unlock(instance);
5220 }
5221 
5222 static void rtp_write_rtcp_psfb(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_frame *frame, struct ast_sockaddr *remote_address)
5223 {
5224  struct ast_rtp_rtcp_feedback *feedback = frame->data.ptr;
5225  unsigned char *rtcpheader;
5226  unsigned char bdata[1024];
5227  int remb_len = 24;
5228  int ice;
5229  int res;
5230  int sr = 0;
5231  int packet_len = 0;
5232  RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
5234  ao2_cleanup);
5235 
5236  if (feedback->fmt != AST_RTP_RTCP_FMT_REMB) {
5237  ast_debug_rtcp(1, "(%p) RTCP provided feedback frame of format %d to write, but only REMB is supported\n",
5238  instance, feedback->fmt);
5239  return;
5240  }
5241 
5242  if (!rtp || !rtp->rtcp) {
5243  return;
5244  }
5245 
5246  /* If REMB support is not enabled don't send this RTCP packet */
5248  ast_debug_rtcp(1, "(%p) RTCP provided feedback REMB report to write, but REMB support not enabled\n",
5249  instance);
5250  return;
5251  }
5252 
5253  if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) {
5254  /*
5255  * RTCP was stopped.
5256  */
5257  return;
5258  }
5259 
5260  rtcpheader = bdata;
5261 
5262  ao2_lock(instance);
5263  res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
5264 
5265  if (res == 0 || res == 1) {
5266  ao2_unlock(instance);
5267  return;
5268  }
5269 
5270  packet_len += res;
5271 
5272  put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (AST_RTP_RTCP_FMT_REMB << 24) | (RTCP_PT_PSFB << 16) | ((remb_len/4)-1)));
5273  put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc));
5274  put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(0)); /* Per the draft, this should always be 0 */
5275  put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(('R' << 24) | ('E' << 16) | ('M' << 8) | ('B'))); /* Unique identifier 'R' 'E' 'M' 'B' */
5276  put_unaligned_uint32(rtcpheader + packet_len + 16, htonl((1 << 24) | (feedback->remb.br_exp << 18) | (feedback->remb.br_mantissa))); /* Number of SSRCs / BR Exp / BR Mantissa */
5277  put_unaligned_uint32(rtcpheader + packet_len + 20, htonl(rtp->ssrc)); /* The SSRC this feedback message applies to */
5278  res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + remb_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5279  if (res < 0) {
5280  ast_log(LOG_ERROR, "RTCP PSFB transmission error: %s\n", strerror(errno));
5281  } else {
5282  ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr);
5283  }
5284 
5285  ao2_unlock(instance);
5286 }
5287 
5288 /*! \pre instance is locked */
5289 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
5290 {
5291  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5292  struct ast_sockaddr remote_address = { {0,} };
5293  struct ast_format *format;
5294  int codec;
5295 
5296  ast_rtp_instance_get_remote_address(instance, &remote_address);
5297 
5298  /* If we don't actually know the remote address don't even bother doing anything */
5299  if (ast_sockaddr_isnull(&remote_address)) {
5300  ast_debug_rtp(1, "(%p) RTP no remote address on instance, so dropping frame\n", instance);
5301  return 0;
5302  }
5303 
5304  /* VP8: is this a request to send a RTCP FIR? */
5305  if (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
5306  rtp_write_rtcp_fir(instance, rtp, &remote_address);
5307  return 0;
5308  } else if (frame->frametype == AST_FRAME_RTCP) {
5309  if (frame->subclass.integer == AST_RTP_RTCP_PSFB) {
5310  rtp_write_rtcp_psfb(instance, rtp, frame, &remote_address);
5311  }
5312  return 0;
5313  }
5314 
5315  /* If there is no data length we can't very well send the packet */
5316  if (!frame->datalen) {
5317  ast_debug_rtp(1, "(%p) RTP received frame with no data for instance, so dropping frame\n", instance);
5318  return 0;
5319  }
5320 
5321  /* If the packet is not one our RTP stack supports bail out */
5322  if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
5323  ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
5324  return -1;
5325  }
5326 
5327  if (rtp->red) {
5328  /* return 0; */
5329  /* no primary data or generations to send */
5330  if ((frame = red_t140_to_red(rtp->red)) == NULL)
5331  return 0;
5332  }
5333 
5334  /* Grab the subclass and look up the payload we are going to use */
5336  1, frame->subclass.format, 0);
5337  if (codec < 0) {
5338  ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n",
5340  return -1;
5341  }
5342 
5343  /* Note that we do not increase the ref count here as this pointer
5344  * will not be held by any thing explicitly. The format variable is
5345  * merely a convenience reference to frame->subclass.format */
5346  format = frame->subclass.format;
5347  if (ast_format_cmp(rtp->lasttxformat, format) == AST_FORMAT_CMP_NOT_EQUAL) {
5348  /* Oh dear, if the format changed we will have to set up a new smoother */
5349  ast_debug_rtp(1, "(%p) RTP ooh, format changed from %s to %s\n",
5350  instance, ast_format_get_name(rtp->lasttxformat),
5352  ao2_replace(rtp->lasttxformat, format);
5353  if (rtp->smoother) {
5354  ast_smoother_free(rtp->smoother);
5355  rtp->smoother = NULL;
5356  }
5357  }
5358 
5359  /* If no smoother is present see if we have to set one up */
5360  if (!rtp->smoother && ast_format_can_be_smoothed(format)) {
5361  unsigned int smoother_flags = ast_format_get_smoother_flags(format);
5362  unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance));
5363 
5364  if (!framing_ms && (smoother_flags & AST_SMOOTHER_FLAG_FORCED)) {
5365  framing_ms = ast_format_get_default_ms(format);
5366  }
5367 
5368  if (framing_ms) {
5369  rtp->smoother = ast_smoother_new((framing_ms * ast_format_get_minimum_bytes(format)) / ast_format_get_minimum_ms(format));
5370  if (!rtp->smoother) {
5371  ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %u len: %u\n",
5372  ast_format_get_name(format), framing_ms, ast_format_get_minimum_bytes(format));
5373  return -1;
5374  }
5375  ast_smoother_set_flags(rtp->smoother, smoother_flags);
5376  }
5377  }
5378 
5379  /* Feed audio frames into the actual function that will create a frame and send it */
5380  if (rtp->smoother) {
5381  struct ast_frame *f;
5382 
5383  if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
5384  ast_smoother_feed_be(rtp->smoother, frame);
5385  } else {
5386  ast_smoother_feed(rtp->smoother, frame);
5387  }
5388 
5389  while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
5390  rtp_raw_write(instance, f, codec);
5391  }
5392  } else {
5393  int hdrlen = 12;
5394  struct ast_frame *f = NULL;
5395 
5396  if (frame->offset < hdrlen) {
5397  f = ast_frdup(frame);
5398  } else {
5399  f = frame;
5400  }
5401  if (f->data.ptr) {
5402  rtp_raw_write(instance, f, codec);
5403  }
5404  if (f != frame) {
5405  ast_frfree(f);
5406  }
5407 
5408  }
5409 
5410  return 0;
5411 }
5412 
5413 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
5414 {
5415  struct timeval now;
5416  struct timeval tmp;
5417  double transit;
5418  double current_time;
5419  double d;
5420  double dtv;
5421  double prog;
5422  int rate = ast_rtp_get_rate(rtp->f.subclass.format);
5423 
5424  if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
5425  gettimeofday(&rtp->rxcore, NULL);
5426  rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
5427  /* map timestamp to a real time */
5428  rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
5429  tmp = ast_samp2tv(timestamp, rate);
5430  rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
5431  /* Round to 0.1ms for nice, pretty timestamps */
5432  rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
5433  }
5434 
5435  gettimeofday(&now,NULL);
5436  /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
5437  tmp = ast_samp2tv(timestamp, rate);
5438  *tv = ast_tvadd(rtp->rxcore, tmp);
5439 
5440  prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
5441  dtv = (double)rtp->drxcore + (double)(prog);
5442  current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
5443  transit = current_time - dtv;
5444  d = transit - rtp->rxtransit;
5445  rtp->rxtransit = transit;
5446  if (d<0) {
5447  d=-d;
5448  }
5449  rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
5450  if (rtp->rtcp) {
5451  if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
5452  rtp->rtcp->maxrxjitter = rtp->rxjitter;
5453  if (rtp->rtcp->rxjitter_count == 1)
5454  rtp->rtcp->minrxjitter = rtp->rxjitter;
5455  if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
5456  rtp->rtcp->minrxjitter = rtp->rxjitter;
5457 
5458  calc_mean_and_standard_deviation(rtp->rxjitter, &rtp->rtcp->normdev_rxjitter,
5459  &rtp->rtcp->stdev_rxjitter, &rtp->rtcp->rxjitter_count);
5460  }
5461 }
5462 
5463 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
5464 {
5465  struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5466  struct ast_sockaddr remote_address = { {0,} };
5467 
5468  ast_rtp_instance_get_remote_address(instance, &remote_address);
5469 
5470  if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
5471  ast_debug_rtp(1, "(%p) RTP ignore potential DTMF echo from '%s'\n",
5472  instance, ast_sockaddr_stringify(&remote_address));
5473  rtp->resp = 0;
5474  rtp->dtmfsamples = 0;
5475  return &ast_null_frame;
5476  } else if (type == AST_FRAME_DTMF_BEGIN && rtp->resp == 'X') {
5477  ast_debug_rtp(1, "(%p) RTP ignore flash begin from '%s'\n",
5478  instance, ast_sockaddr_stringify(&remote_address));
5479  rtp->resp = 0;
5480  rtp->dtmfsamples = 0;
5481  return &ast_null_frame;
5482  }
5483 
5484  if (rtp->resp == 'X') {
5485  ast_debug_rtp(1, "(%p) RTP creating flash Frame at %s\n",
5486  instance, ast_sockaddr_stringify(&remote_address));
5487  rtp->f.frametype = AST_FRAME_CONTROL;
5489  } else {
5490  ast_debug_rtp(1, "(%p) RTP creating %s DTMF Frame: %d (%c), at %s\n",
5491  instance, type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
5492  rtp->resp, rtp->resp,
5493  ast_sockaddr_stringify(&remote_address));
5494  rtp->f.frametype = type;
5495  rtp->f.subclass.integer = rtp->resp;
5496  }
5497  rtp->f.datalen = 0;
5498  rtp->f.samples = 0;
5499  rtp->f.mallocd = 0;
5500  rtp->