61#include <sys/socket.h>
62#include <netinet/in.h>
64#include <netinet/in_systm.h>
65#include <netinet/ip.h>
281#define SCHED_MULTITHREADED
285#define DEBUG_SCHED_MULTITHREAD
289static int nochecksums = 0;
292#define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
293#define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
295#define DEFAULT_THREAD_COUNT 10
296#define DEFAULT_MAX_THREAD_COUNT 100
297#define DEFAULT_RETRY_TIME 1000
298#define MEMORY_SIZE 100
299#define DEFAULT_DROP 3
303#define MIN_REUSE_TIME 60
310static const char tdesc[] =
"Inter Asterisk eXchange Driver (Ver 2)";
315#define MAX_TRUNK_MTU 1240
320#define DEFAULT_CONTEXT "default"
340#define MAX_TRUNKDATA 640 * 200
373#define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
375#define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
377 ~AST_FORMAT_SLIN16 & \
378 ~AST_FORMAT_SIREN7 & \
379 ~AST_FORMAT_SIREN14 & \
385#define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
387 ~AST_FORMAT_G726_AAL2 & \
390#define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
394#define DEFAULT_MAXMS 2000
395#define DEFAULT_FREQ_OK 60 * 1000
396#define DEFAULT_FREQ_NOTOK 10 * 1000
407#define AUTH_METHOD_NAMES_BUFSIZE 19
422 pos += sprintf(pos,
"|RSA");
425 pos += sprintf(pos,
"|MD5");
428 pos += sprintf(pos,
"|plaintext");
440#define IAX_CALLENCRYPTED(pvt) \
441 (ast_test_flag64(pvt, IAX_ENCRYPTED) && ast_test_flag64(pvt, IAX_KEYPOPULATED))
443#define IAX_DEBUGDIGEST(msg, key) do { \
445 char digest[33] = ""; \
450 for (idx = 0; idx < 16; idx++) \
451 sprintf(digest + (idx << 1), "%02hhx", (unsigned char) key[idx]); \
453 ast_log(LOG_NOTICE, msg " IAX_COMMAND_RTKEY to rotate key to '%s'\n", digest); \
467static int test_late = 0;
468static int test_resync = 0;
469static int test_jit = 0;
470static int test_jitpct = 0;
498#define IAX_HASCALLERID (uint64_t)(1LLU << 0)
499#define IAX_DELME (uint64_t)(1LLU << 1)
500#define IAX_TEMPONLY (uint64_t)(1LLU << 2)
501#define IAX_TRUNK (uint64_t)(1LLU << 3)
502#define IAX_NOTRANSFER (uint64_t)(1LLU << 4)
503#define IAX_USEJITTERBUF (uint64_t)(1LLU << 5)
504#define IAX_DYNAMIC (uint64_t)(1LLU << 6)
505#define IAX_SENDANI (uint64_t)(1LLU << 7)
506#define IAX_RTSAVE_SYSNAME (uint64_t)(1LLU << 8)
507#define IAX_ALREADYGONE (uint64_t)(1LLU << 9)
508#define IAX_PROVISION (uint64_t)(1LLU << 10)
509#define IAX_QUELCH (uint64_t)(1LLU << 11)
510#define IAX_ENCRYPTED (uint64_t)(1LLU << 12)
511#define IAX_KEYPOPULATED (uint64_t)(1LLU << 13)
512#define IAX_CODEC_USER_FIRST (uint64_t)(1LLU << 14)
513#define IAX_CODEC_NOPREFS (uint64_t)(1LLU << 15)
514#define IAX_CODEC_NOCAP (uint64_t)(1LLU << 16)
515#define IAX_RTCACHEFRIENDS (uint64_t)(1LLU << 17)
516#define IAX_RTUPDATE (uint64_t)(1LLU << 18)
517#define IAX_RTAUTOCLEAR (uint64_t)(1LLU << 19)
518#define IAX_RTIGNOREREGEXPIRE (uint64_t)(1LLU << 21)
519#define IAX_TRUNKTIMESTAMPS (uint64_t)(1LLU << 22)
520#define IAX_TRANSFERMEDIA (uint64_t)(1LLU << 23)
521#define IAX_MAXAUTHREQ (uint64_t)(1LLU << 24)
522#define IAX_DELAYPBXSTART (uint64_t)(1LLU << 25)
523#define IAX_ALLOWFWDOWNLOAD (uint64_t)(1LLU << 26)
524#define IAX_IMMEDIATE (uint64_t)(1LLU << 27)
525#define IAX_SENDCONNECTEDLINE (uint64_t)(1LLU << 28)
526#define IAX_RECVCONNECTEDLINE (uint64_t)(1LLU << 29)
527#define IAX_FORCE_ENCRYPT (uint64_t)(1LLU << 30)
528#define IAX_SHRINKCALLERID (uint64_t)(1LLU << 31)
638#define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
704#define MIN_RETRY_TIME 100
705#define MAX_RETRY_TIME 10000
707#define MAX_JITTER_BUFFER 50
708#define MIN_JITTER_BUFFER 10
710#define DEFAULT_TRUNKDATA 640 * 10
712#define MAX_TIMESTAMP_SKEW 160
715#define TS_GAP_FOR_JB_RESYNC 5000
718#define MARK_IAX_SUBCLASS_TX 0x8000
950#define PTR_TO_CALLNO_ENTRY(a) ((uint16_t)(unsigned long)(a))
951#define CALLNO_ENTRY_TO_PTR(a) ((void *)(unsigned long)(a))
953#define CALLNO_ENTRY_SET_VALIDATED(a) ((a) |= 0x8000)
954#define CALLNO_ENTRY_IS_VALIDATED(a) ((a) & 0x8000)
955#define CALLNO_ENTRY_GET_CALLNO(a) ((a) & 0x7FFF)
996#define MAX_PEER_BUCKETS 17
998#define MAX_PEER_BUCKETS 563
1002#define MAX_USER_BUCKETS MAX_PEER_BUCKETS
1112#ifdef SCHED_MULTITHREADED
1116#ifdef DEBUG_SCHED_MULTITHREAD
1222#define TRUNK_CALL_START (IAX_MAX_CALLS / 2)
1261 va_start(
args, fmt);
1273 va_start(
args, fmt);
1285 va_start(
args, fmt);
1318static int send_command_locked(
unsigned short callno,
char,
int,
unsigned int,
const unsigned char *,
int,
int);
1343 .description =
tdesc,
1384 if (!
iaxs[callno] || !
iaxs[callno]->owner) {
1415 if (subtype == -1) {
1552 ast_verb(1,
"IAX, got a network change message, renewing all IAX registrations.\n");
1570 .
type =
"IAX2_VARIABLE",
1698#ifdef SCHED_MULTITHREADED
1702 static time_t lasterror;
1707 thread->schedfunc = func;
1708 thread->scheddata = data;
1710#ifdef DEBUG_SCHED_MULTITHREAD
1717 if (t != lasterror) {
1719 ast_debug(1,
"Out of idle IAX2 threads for scheduling! (%s)\n", funcname);
1724#define schedule_action(func, data) __schedule_action(func, data, __PRETTY_FUNCTION__)
1752 ast_debug(3,
"I wanted to lock callno %d, but it is dead or going to die.\n",
callno);
1768 ast_debug(3,
"Hangup initiated on call %d, aborting __send_ping\n",
callno);
1789#ifdef SCHED_MULTITHREADED
1816 if (!strcasecmp(s,
"aes128"))
1832 ast_debug(3,
"Hangup initiated on call %d, aborting __send_lagrq\n",
callno);
1853#ifdef SCHED_MULTITHREADED
1869 if (subclass & (1LL << x)) {
1901 uint64_t pref_bitfield;
1904 if (!pref_bitfield) {
1920 return found_format;
1923 ast_debug(4,
"Could not find preferred codec - Returning zero codec.\n");
2012 const char *
name = obj;
2022 struct iax2_peer *peer = obj, *peer2 = arg;
2023 const char *
name = arg;
2035 const char *
name = obj;
2046 const char *
name = arg;
2063 if (!peer && realtime) {
2188 ast_debug(1,
"Really destroying %d now...\n", callno);
2304 tmp->keyrotateid = -1;
2327 tmp->hold_signaling = 1;
2338 memcpy(
new, fr,
sizeof(*
new));
2449 ast_log(
LOG_ERROR,
"This should not be called without a transfer call number.\n");
2459 ast_log(
LOG_ERROR,
"This should not be called without a transfer call number.\n");
2468 ast_log(
LOG_ERROR,
"This should not be called without a peer call number.\n");
2478 ast_log(
LOG_ERROR,
"This should not be called without a peer call number.\n");
2518 struct peercnt *peercnt1 = obj, *peercnt2 = arg;
2547 const char *find =
S_OR(
name,
"guest");
2630 ast_debug(1,
"Reset limits for peercnts table\n");
2796 for (; v; v = v->
next) {
2804 ast_log(
LOG_ERROR,
"Call number limit for %s could not be added, Invalid address range\n.", v->
name);
2806 }
else if ((sscanf(v->
value,
"%d", &limit) != 1) || (limit < 0)) {
2892 e->
command =
"iax2 show callnumber usage";
2894 "Usage: iax2 show callnumber usage [IP address]\n"
2895 " Shows current IP addresses which are consuming iax2 call numbers\n";
2900 if (
a->argc < 4 ||
a->argc > 5)
2904 ast_cli(
a->fd,
"%-45s %-12s %-12s\n",
"Address",
"Callno Usage",
"Callno Limit");
2913 ast_cli(
a->fd,
"%-45s %-12s %-12s\n",
"Address",
"Callno Usage",
"Callno Limit");
2930 ast_cli(
a->fd,
"\nNon-CallToken Validation Callno Limit: %d\n"
2931 "Non-CallToken Validated Callno Used: %d\n",
2935 ast_cli(
a->fd,
"Total Available Callno: %zu\n"
2936 "Regular Callno Available: %zu\n"
2937 "Trunk Callno Available: %zu\n",
2938 pool_avail + trunk_pool_avail,
2941 }
else if (
a->argc == 5 && !found) {
2942 ast_cli(
a->fd,
"No call number table entries for %s found\n",
a->argv[4] );
2989 "NON-CallToken callnumber limit is reached. Current: %d Max: %d\n",
3043 "Attempted to decrement total non calltoken validated "
3044 "callnumbers below zero. Callno is: %d\n",
3161 int validated = (
new >
NEW_ALLOW) ? 1 : 0;
3170 .transfercallno =
callno,
3172 .frames_received = check_dcallno,
3178 if (return_locked) {
3187 memset(&tmp_pvt.
addr, 0,
sizeof(tmp_pvt.
addr));
3190 if (return_locked) {
3208 if (!res || !return_locked) {
3248 ast_debug(1,
"Creating new call structure %d\n", x);
3408 struct sockaddr_in *sin;
3411 struct sock_extended_err e;
3416 m.msg_controllen =
sizeof(e);
3418 res = recvmsg(
netsocket, &m, MSG_ERRQUEUE);
3422 if (m.msg_controllen) {
3423 sin = (
struct sockaddr_in *)SO_EE_OFFENDER(&e);
3512 if ((pvt =
iaxs[callno])) {
3529 ast_debug(3,
"Avoiding IAX destroy deadlock\n");
3615 }
else if (f->
final) {
3619 ast_log(
LOG_WARNING,
"Max retries exceeded to host %s on %s (type = %u, subclass = %d, ts=%u, seqno=%d)\n",
3633 if (
iaxs[callno] &&
iaxs[callno]->owner)
3636 if (
iaxs[callno]->reg) {
3637 memset(&
iaxs[callno]->reg->us, 0,
sizeof(
iaxs[callno]->
reg->
us));
3673 }
else if (callno) {
3680#ifdef SCHED_MULTITHREADED
3691 static const char *
const choices[] = {
"all",
NULL };
3696 e->
command =
"iax2 prune realtime";
3698 "Usage: iax2 prune realtime [<peername>|all]\n"
3699 " Prunes object(s) from the cache\n";
3712 if (!strcmp(
a->argv[3],
"all")) {
3715 ast_cli(
a->fd,
"Cache flushed successfully.\n");
3725 ast_cli(
a->fd,
"Peer %s was removed from the cache.\n",
a->argv[3]);
3727 ast_cli(
a->fd,
"Peer %s is not eligible for this operation.\n",
a->argv[3]);
3734 ast_cli(
a->fd,
"User %s was removed from the cache.\n",
a->argv[3]);
3736 ast_cli(
a->fd,
"User %s is not eligible for this operation.\n",
a->argv[3]);
3742 ast_cli(
a->fd,
"%s was not found in the cache.\n",
a->argv[3]);
3752 e->
command =
"iax2 test losspct";
3754 "Usage: iax2 test losspct <percentage>\n"
3755 " For testing, throws away <percentage> percent of incoming packets\n";
3773 e->
command =
"iax2 test late";
3775 "Usage: iax2 test late <ms>\n"
3776 " For testing, count the next frame as <ms> ms late\n";
3785 test_late = atoi(
a->argv[3]);
3794 e->
command =
"iax2 test resync";
3796 "Usage: iax2 test resync <ms>\n"
3797 " For testing, adjust all future frames by <ms> ms\n";
3806 test_resync = atoi(
a->argv[3]);
3815 e->
command =
"iax2 test jitter";
3817 "Usage: iax2 test jitter <ms> <pct>\n"
3818 " For testing, simulate maximum jitter of +/- <ms> on <pct>\n"
3819 " percentage of packets. If <pct> is not specified, adds\n"
3820 " jitter to all packets.\n";
3826 if (
a->argc < 4 ||
a->argc > 5)
3829 test_jit = atoi(
a->argv[3]);
3831 test_jitpct = atoi(
a->argv[4]);
3846 snprintf(
status, statuslen,
"LAGGED (%d ms)", peer->
lastms);
3848 }
else if (peer->
lastms) {
3849 snprintf(
status, statuslen,
"OK (%d ms)", peer->
lastms);
3869 int load_realtime = 0;
3873 e->
command =
"iax2 show peer";
3875 "Usage: iax2 show peer <name>\n"
3876 " Display details on specific IAX peer\n";
3887 load_realtime = (
a->argc == 5 && !strcmp(
a->argv[4],
"load")) ? 1 : 0;
3891 char *str_addr, *str_defaddr;
3892 char *str_port, *str_defport;
3915 ast_cli(
a->fd,
" Addr->IP : %s Port %s\n", str_addr ? str_addr :
"(Unspecified)", str_port);
3916 ast_cli(
a->fd,
" Defaddr->IP : %s Port %s\n", str_defaddr, str_defport);
3921 strcpy(cbuf,
"Error");
3923 ast_cli(
a->fd,
" Codec Order : %s\n", cbuf);
3931 ast_cli(
a->fd,
"Peer %s not found.\n",
a->argv[3]);
3943 int wordlen = strlen(
word);
3948 if (!strncasecmp(peer->
name,
word, wordlen) && ++which >
state
3964 int cnt = 0, dead = 0,
final = 0, i = 0;
3968 e->
command =
"iax2 show stats";
3970 "Usage: iax2 show stats\n"
3971 " Display statistics on IAX channel driver.\n";
3992 ast_cli(
a->fd,
" IAX Statistics\n");
3993 ast_cli(
a->fd,
"---------------------\n");
3997 ast_cli(
a->fd,
"Packets in transmit queue: %d dead, %d final, %d total\n\n", dead,
final, cnt);
4015 "Usage: iax2 set mtu <value>\n"
4016 " Set the system-wide IAX IP mtu to <value> bytes net or\n"
4017 " zero to disable. Disabling means that the operating system\n"
4018 " must handle fragmentation of UDP packets when the IAX2 trunk\n"
4019 " packet exceeds the UDP payload size. This is substantially\n"
4020 " below the IP mtu. Try 1240 on ethernets. Must be 172 or\n"
4021 " greater for G.711 samples.\n";
4029 if (strncasecmp(
a->argv[3],
"default", strlen(
a->argv[3])) == 0)
4032 mtuv = atoi(
a->argv[3]);
4039 if (mtuv < 172 || mtuv > 4000) {
4040 ast_cli(
a->fd,
"Trunk MTU must be between 172 and 4000\n");
4057 e->
command =
"iax2 show cache";
4059 "Usage: iax2 show cache\n"
4060 " Display currently cached IAX Dialplan results.\n";
4068 ast_cli(
a->fd,
"%-20.20s %-12.12s %-9.9s %-8.8s %s\n",
"Peer/Context",
"Exten",
"Exp.",
"Wait.",
"Flags");
4071 s = dp->
expiry.tv_sec - now.tv_sec;
4074 strncat(
tmp,
"EXISTS|",
sizeof(
tmp) - strlen(
tmp) - 1);
4076 strncat(
tmp,
"NONEXISTENT|",
sizeof(
tmp) - strlen(
tmp) - 1);
4078 strncat(
tmp,
"CANEXIST|",
sizeof(
tmp) - strlen(
tmp) - 1);
4080 strncat(
tmp,
"PENDING|",
sizeof(
tmp) - strlen(
tmp) - 1);
4082 strncat(
tmp,
"TIMEOUT|",
sizeof(
tmp) - strlen(
tmp) - 1);
4084 strncat(
tmp,
"TRANSMITTED|",
sizeof(
tmp) - strlen(
tmp) - 1);
4086 strncat(
tmp,
"MATCHMORE|",
sizeof(
tmp) - strlen(
tmp) - 1);
4088 strncat(
tmp,
"UNKNOWN|",
sizeof(
tmp) - strlen(
tmp) - 1);
4091 tmp[strlen(
tmp) - 1] =
'\0';
4107 ast_cli(
a->fd,
"%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->
exten, s, y,
tmp);
4109 ast_cli(
a->fd,
"%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->
exten,
"(expired)", y,
tmp);
4125 const int lower_mask = (1 << ts_shift) - 1;
4126 const int upper_mask = ~lower_mask;
4129 if ( (fr->
ts & upper_mask) == last_upper ) {
4131 const int threshold = (ts_shift == 15) ? 25000 : 50000;
4138 fr->
ts = (last_upper + (1 << ts_shift)) | (fr->
ts & lower_mask);
4140 ast_debug(1,
"schedule_delivery: pushed forward timestamp\n");
4146 fr->
ts = (last_upper - (1 << ts_shift)) | (fr->
ts & lower_mask);
4148 ast_debug(1,
"schedule_delivery: pushed back timestamp\n");