2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010 Digium, Inc.
6 * Richard Mudgett <rmudgett@digium.com>
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.
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.
21 * \brief SS7 signaling module.
23 * \author Matthew Fredrickson <creslin@digium.com>
24 * \author Richard Mudgett <rmudgett@digium.com>
27 * \arg \ref AstCREDITS
31 <support_level>core</support_level>
40 #include "asterisk/pbx.h"
41 #include "asterisk/causes.h"
42 #include "asterisk/musiconhold.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/transcap.h"
47 #if defined(LIBSS7_ABI_COMPATIBILITY)
48 #error "Your installed libss7 is not compatible"
51 /* ------------------------------------------------------------------- */
53 static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
56 case SIG_SS7_CALL_LEVEL_IDLE:
58 case SIG_SS7_CALL_LEVEL_ALLOCATED:
60 case SIG_SS7_CALL_LEVEL_CONTINUITY:
62 case SIG_SS7_CALL_LEVEL_SETUP:
64 case SIG_SS7_CALL_LEVEL_PROCEEDING:
66 case SIG_SS7_CALL_LEVEL_ALERTING:
68 case SIG_SS7_CALL_LEVEL_CONNECT:
70 case SIG_SS7_CALL_LEVEL_GLARE:
76 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
78 if (sig_ss7_callbacks.unlock_private) {
79 sig_ss7_callbacks.unlock_private(p->chan_pvt);
83 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
85 if (sig_ss7_callbacks.lock_private) {
86 sig_ss7_callbacks.lock_private(p->chan_pvt);
90 static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
92 if (sig_ss7_callbacks.deadlock_avoidance_private) {
93 sig_ss7_callbacks.deadlock_avoidance_private(p->chan_pvt);
95 /* Fallback to the old way if callback not present. */
96 sig_ss7_unlock_private(p);
98 sig_ss7_lock_private(p);
102 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
104 p->inalarm = in_alarm;
105 if (sig_ss7_callbacks.set_alarm) {
106 sig_ss7_callbacks.set_alarm(p->chan_pvt, in_alarm);
110 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
112 if (sig_ss7_callbacks.set_dialing) {
113 sig_ss7_callbacks.set_dialing(p->chan_pvt, is_dialing);
117 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
119 if (sig_ss7_callbacks.set_digital) {
120 sig_ss7_callbacks.set_digital(p->chan_pvt, is_digital);
124 static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
126 p->outgoing = is_outgoing;
127 if (sig_ss7_callbacks.set_outgoing) {
128 sig_ss7_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
132 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
134 if (sig_ss7_callbacks.set_inservice) {
135 sig_ss7_callbacks.set_inservice(p->chan_pvt, is_inservice);
139 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
141 p->locallyblocked = is_blocked;
142 if (sig_ss7_callbacks.set_locallyblocked) {
143 sig_ss7_callbacks.set_locallyblocked(p->chan_pvt, is_blocked);
147 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
149 p->remotelyblocked = is_blocked;
150 if (sig_ss7_callbacks.set_remotelyblocked) {
151 sig_ss7_callbacks.set_remotelyblocked(p->chan_pvt, is_blocked);
157 * \brief Open the SS7 channel media path.
160 * \param p Channel private control structure.
164 static void sig_ss7_open_media(struct sig_ss7_chan *p)
166 if (sig_ss7_callbacks.open_media) {
167 sig_ss7_callbacks.open_media(p->chan_pvt);
173 * \brief Set the caller id information in the parent module.
176 * \param p sig_ss7 channel structure.
180 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
182 struct ast_party_caller caller;
184 if (sig_ss7_callbacks.set_callerid) {
185 ast_party_caller_init(&caller);
187 caller.id.name.str = p->cid_name;
188 caller.id.name.presentation = p->callingpres;
189 caller.id.name.valid = 1;
191 caller.id.number.str = p->cid_num;
192 caller.id.number.plan = p->cid_ton;
193 caller.id.number.presentation = p->callingpres;
194 caller.id.number.valid = 1;
196 if (!ast_strlen_zero(p->cid_subaddr)) {
197 caller.id.subaddress.valid = 1;
198 //caller.id.subaddress.type = 0;/* nsap */
199 //caller.id.subaddress.odd_even_indicator = 0;
200 caller.id.subaddress.str = p->cid_subaddr;
203 caller.ani.number.str = p->cid_ani;
204 //caller.ani.number.plan = p->xxx;
205 //caller.ani.number.presentation = p->xxx;
206 caller.ani.number.valid = 1;
208 caller.ani2 = p->cid_ani2;
209 sig_ss7_callbacks.set_callerid(p->chan_pvt, &caller);
215 * \brief Set the Dialed Number Identifier.
218 * \param p sig_ss7 channel structure.
219 * \param dnid Dialed Number Identifier string.
223 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
225 if (sig_ss7_callbacks.set_dnid) {
226 sig_ss7_callbacks.set_dnid(p->chan_pvt, dnid);
230 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
234 if (sig_ss7_callbacks.play_tone) {
235 res = sig_ss7_callbacks.play_tone(p->chan_pvt, tone);
242 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
244 if (sig_ss7_callbacks.set_echocanceller) {
245 return sig_ss7_callbacks.set_echocanceller(p->chan_pvt, enable);
250 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
252 if (p->loopedback != enable) {
253 p->loopedback = enable;
254 if (sig_ss7_callbacks.set_loopback) {
255 sig_ss7_callbacks.set_loopback(p->chan_pvt, enable);
260 static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
262 struct ast_channel *ast;
264 if (sig_ss7_callbacks.new_ast_channel) {
265 ast = sig_ss7_callbacks.new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
276 p->alreadyhungup = 0;
277 ast_channel_transfercapability_set(ast, transfercapability);
278 pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
279 ast_transfercapability2str(transfercapability));
280 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
281 sig_ss7_set_digital(p, 1);
287 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
289 if (sig_ss7_callbacks.handle_link_exception) {
290 sig_ss7_callbacks.handle_link_exception(linkset, which);
296 * \brief Determine if a private channel structure is available.
298 * \param pvt Channel to determine if available.
300 * \return TRUE if the channel is available.
302 static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
304 if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
305 && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
306 && !pvt->locallyblocked && !pvt->remotelyblocked) {
314 * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
317 * \param ss7 SS7 linkset control structure.
318 * \param chanpos Channel position in the span.
320 * \note Assumes the ss7->lock is already obtained.
321 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
325 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
328 if (!ss7->pvts[chanpos]->owner) {
329 /* There is no owner lock to get. */
332 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
333 /* We got the lock */
338 sig_ss7_unlock_private(ss7->pvts[chanpos]);
339 DEADLOCK_AVOIDANCE(&ss7->lock);
340 sig_ss7_lock_private(ss7->pvts[chanpos]);
346 * \brief Queue the given frame onto the owner channel.
349 * \param ss7 SS7 linkset control structure.
350 * \param chanpos Channel position in the span.
351 * \param frame Frame to queue onto the owner channel.
353 * \note Assumes the ss7->lock is already obtained.
354 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
358 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
360 sig_ss7_lock_owner(ss7, chanpos);
361 if (ss7->pvts[chanpos]->owner) {
362 ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
363 ast_channel_unlock(ss7->pvts[chanpos]->owner);
369 * \brief Queue a control frame of the specified subclass onto the owner channel.
372 * \param ss7 SS7 linkset control structure.
373 * \param chanpos Channel position in the span.
374 * \param subclass Control frame subclass to queue onto the owner channel.
376 * \note Assumes the ss7->lock is already obtained.
377 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
381 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
383 struct ast_frame f = {AST_FRAME_CONTROL, };
384 struct sig_ss7_chan *p = ss7->pvts[chanpos];
386 if (sig_ss7_callbacks.queue_control) {
387 sig_ss7_callbacks.queue_control(p->chan_pvt, subclass);
390 f.subclass.integer = subclass;
391 sig_ss7_queue_frame(ss7, chanpos, &f);
396 * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
399 * \param owner Owner channel of the pvt.
400 * \param cause String describing the cause to be placed into the frame.
402 * \note Assumes the linkset->lock is already obtained.
403 * \note Assumes the sig_ss7_lock_private(linkset->pvts[chanpos]) is already obtained.
404 * \note Assumes linkset->pvts[chanpos]->owner is non-NULL and its lock is already obtained.
408 static void ss7_queue_pvt_cause_data(struct ast_channel *owner, const char *cause, int ast_cause)
410 struct ast_control_pvt_cause_code *cause_code;
411 int datalen = sizeof(*cause_code) + strlen(cause);
413 cause_code = ast_alloca(datalen);
414 memset(cause_code, 0, datalen);
415 cause_code->ast_cause = ast_cause;
416 ast_copy_string(cause_code->chan_name, ast_channel_name(owner), AST_CHANNEL_NAME);
417 ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
418 ast_queue_control_data(owner, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
419 ast_channel_hangupcause_hash_set(owner, cause_code, datalen);
425 * \brief Find the channel position by CIC/DPC.
427 * \param linkset SS7 linkset control structure.
428 * \param cic Circuit Identification Code
429 * \param dpc Destination Point Code
431 * \retval chanpos on success.
432 * \retval -1 on error.
434 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
438 for (i = 0; i < linkset->numchans; i++) {
439 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
449 * \brief Find the channel position by CIC/DPC and gripe if not found.
451 * \param linkset SS7 linkset control structure.
452 * \param cic Circuit Identification Code
453 * \param dpc Destination Point Code
454 * \param msg_name Message type name that failed.
456 * \retval chanpos on success.
457 * \retval -1 on error.
459 static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
463 chanpos = ss7_find_cic(linkset, cic, dpc);
465 ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested unconfigured CIC/DPC %d/%d.\n",
466 linkset->span, msg_name, cic, dpc);
472 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
474 unsigned char status[32];
475 struct sig_ss7_chan *p = NULL;
478 for (i = 0; i < linkset->numchans; i++) {
479 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
480 p = linkset->pvts[i];
481 offset = p->cic - startcic;
483 if (p->locallyblocked)
484 status[offset] |= (1 << 0) | (1 << 4);
485 if (p->remotelyblocked)
486 status[offset] |= (1 << 1) | (1 << 5);
489 status[offset] |= (1 << 3);
491 status[offset] |= (1 << 2);
493 status[offset] |= 0x3 << 2;
498 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
500 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
504 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
508 for (i = 0; i < linkset->numchans; i++) {
509 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
510 sig_ss7_lock_private(linkset->pvts[i]);
511 sig_ss7_lock_owner(linkset, i);
512 if (linkset->pvts[i]->owner) {
513 ast_softhangup_nolock(linkset->pvts[i]->owner, AST_SOFTHANGUP_DEV);
514 ast_channel_unlock(linkset->pvts[i]->owner);
516 sig_ss7_unlock_private(linkset->pvts[i]);
521 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
525 /* XXX the use of state here seems questionable about matching up with the linkset channels */
526 for (i = 0; i < linkset->numchans; i++) {
527 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
530 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
532 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
537 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
541 for (i = 0; i < linkset->numchans; i++) {
542 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
543 sig_ss7_set_inservice(linkset->pvts[i], 1);
547 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
549 int i, startcic = -1, endcic, dpc;
551 if (linkset->numchans <= 0)
554 startcic = linkset->pvts[0]->cic;
555 /* DB: CIC's DPC fix */
556 dpc = linkset->pvts[0]->dpc;
558 for (i = 0; i < linkset->numchans; i++) {
559 if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
562 endcic = linkset->pvts[i]->cic;
563 ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
564 isup_grs(linkset->ss7, startcic, endcic, dpc);
566 /* DB: CIC's DPC fix */
567 if (linkset->pvts[i+1]) {
568 startcic = linkset->pvts[i+1]->cic;
569 dpc = linkset->pvts[i+1]->dpc;
575 /* This function is assumed to be called with the private channel lock and linkset lock held */
576 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
578 struct ss7 *ss7 = linkset->ss7;
580 struct ast_channel *c;
582 struct ast_callid *callid = NULL;
583 int callid_created = ast_callid_threadstorage_auto(&callid);
585 if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
586 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
587 isup_acm(ss7, p->ss7call);
589 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
592 /* Companding law is determined by SS7 signaling type. */
593 if (linkset->type == SS7_ITU) {
600 * Release the SS7 lock while we create the channel so other
601 * threads can send messages. We must also release the private
602 * lock to prevent deadlock while creating the channel.
604 ast_mutex_unlock(&linkset->lock);
605 sig_ss7_unlock_private(p);
606 c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
608 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
609 ast_mutex_lock(&linkset->lock);
610 sig_ss7_lock_private(p);
611 isup_rel(linkset->ss7, p->ss7call, AST_CAUSE_SWITCH_CONGESTION);
612 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
613 p->alreadyhungup = 1;
614 ast_callid_threadstorage_auto_clean(callid, callid_created);
618 /* Hold the channel and private lock while we setup the channel. */
620 sig_ss7_lock_private(p);
622 sig_ss7_set_echocanceller(p, 1);
625 * It is reasonably safe to set the following
626 * channel variables while the channel private
627 * structure is locked. The PBX has not been
628 * started yet and it is unlikely that any other task
629 * will do anything with the channel we have just
632 * We only reference these variables in the context of the ss7_linkset function
633 * when receiving either and IAM or a COT message.
635 if (!ast_strlen_zero(p->charge_number)) {
636 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
637 /* Clear this after we set it */
638 p->charge_number[0] = 0;
640 if (!ast_strlen_zero(p->gen_add_number)) {
641 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
642 /* Clear this after we set it */
643 p->gen_add_number[0] = 0;
645 if (!ast_strlen_zero(p->jip_number)) {
646 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
647 /* Clear this after we set it */
648 p->jip_number[0] = 0;
650 if (!ast_strlen_zero(p->gen_dig_number)) {
651 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
652 /* Clear this after we set it */
653 p->gen_dig_number[0] = 0;
655 if (!ast_strlen_zero(p->orig_called_num)) {
656 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
657 /* Clear this after we set it */
658 p->orig_called_num[0] = 0;
661 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
662 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
663 /* Clear this after we set it */
666 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
667 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
668 /* Clear this after we set it */
669 p->gen_dig_scheme = 0;
671 if (!ast_strlen_zero(p->lspi_ident)) {
672 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
673 /* Clear this after we set it */
674 p->lspi_ident[0] = 0;
677 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
678 pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
679 /* Clear this after we set it */
680 p->call_ref_ident = 0;
682 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
683 pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
684 /* Clear this after we set it */
687 snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
688 pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
689 /* Clear this after we set it */
690 p->calling_party_cat = 0;
692 if (!ast_strlen_zero(p->redirecting_num)) {
693 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
694 /* Clear this after we set it */
695 p->redirecting_num[0] = 0;
697 if (!ast_strlen_zero(p->generic_name)) {
698 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
699 /* Clear this after we set it */
700 p->generic_name[0] = 0;
703 sig_ss7_unlock_private(p);
704 ast_channel_unlock(c);
706 if (ast_pbx_start(c)) {
707 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", ast_channel_name(c), p->cic);
710 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
713 /* Must return with linkset and private lock. */
714 ast_mutex_lock(&linkset->lock);
715 sig_ss7_lock_private(p);
716 ast_callid_threadstorage_auto_clean(callid, callid_created);
719 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
721 if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
728 case SS7_NAI_INTERNATIONAL:
729 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
731 case SS7_NAI_NATIONAL:
732 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
734 case SS7_NAI_SUBSCRIBER:
735 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
737 case SS7_NAI_UNKNOWN:
738 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
741 snprintf(buf, size, "%s", number);
746 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
748 return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
753 * \brief Set callid threadstorage for the ss7_linkset thread to that of an existing channel
755 * \param linkset ss7 span control structure.
756 * \param chanpos channel position in the span
758 * \note Assumes the ss7->lock is already obtained.
759 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
761 * \return a reference to the callid bound to the channel which has also
762 * been bound to threadstorage if it exists. If this returns non-NULL,
763 * the callid must be unreffed and the threadstorage should be unbound
764 * before the while loop wraps in ss7_linkset.
766 static struct ast_callid *func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
768 struct ast_callid *callid = NULL;
769 sig_ss7_lock_owner(linkset, chanpos);
770 if (linkset->pvts[chanpos]->owner) {
771 callid = ast_channel_callid(linkset->pvts[chanpos]->owner);
772 ast_channel_unlock(linkset->pvts[chanpos]->owner);
774 ast_callid_threadassoc_add(callid);
781 /* This is a thread per linkset that handles all received events from libss7. */
782 void *ss7_linkset(void *data)
785 struct timeval *next = NULL, tv;
786 struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
787 struct ss7 *ss7 = linkset->ss7;
789 struct sig_ss7_chan *p;
790 struct pollfd pollers[SIG_SS7_NUM_DCHANS];
793 #define SS7_MAX_POLL 60000 /* Maximum poll time in ms. */
795 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
797 ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
798 ast_mutex_lock(&linkset->lock);
800 ast_mutex_unlock(&linkset->lock);
803 ast_mutex_lock(&linkset->lock);
804 if ((next = ss7_schedule_next(ss7))) {
806 tv.tv_sec = next->tv_sec - tv.tv_sec;
807 tv.tv_usec = next->tv_usec - tv.tv_usec;
808 if (tv.tv_usec < 0) {
809 tv.tv_usec += 1000000;
816 nextms = tv.tv_sec * 1000;
817 nextms += tv.tv_usec / 1000;
818 if (SS7_MAX_POLL < nextms) {
819 nextms = SS7_MAX_POLL;
822 nextms = SS7_MAX_POLL;
825 for (i = 0; i < linkset->numsigchans; i++) {
826 pollers[i].fd = linkset->fds[i];
827 pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
828 pollers[i].revents = 0;
830 ast_mutex_unlock(&linkset->lock);
832 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
833 pthread_testcancel();
834 res = poll(pollers, linkset->numsigchans, nextms);
835 pthread_testcancel();
836 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
838 if ((res < 0) && (errno != EINTR)) {
839 ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
841 ast_mutex_lock(&linkset->lock);
842 ss7_schedule_run(ss7);
843 ast_mutex_unlock(&linkset->lock);
847 ast_mutex_lock(&linkset->lock);
848 for (i = 0; i < linkset->numsigchans; i++) {
849 if (pollers[i].revents & POLLPRI) {
850 sig_ss7_handle_link_exception(linkset, i);
852 if (pollers[i].revents & POLLIN) {
853 res = ss7_read(ss7, pollers[i].fd);
855 if (pollers[i].revents & POLLOUT) {
856 res = ss7_write(ss7, pollers[i].fd);
858 ast_debug(1, "Error in write %s\n", strerror(errno));
863 while ((e = ss7_check_event(ss7))) {
864 struct ast_callid *callid = NULL;
868 if (linkset->debug) {
869 ast_verbose("Linkset %d: Processing event: %s\n",
870 linkset->span, ss7_event2str(e->e));
875 if (linkset->state != LINKSET_STATE_UP) {
876 ast_verbose("--- SS7 Up ---\n");
877 ss7_reset_linkset(linkset);
879 linkset->state = LINKSET_STATE_UP;
882 ast_verbose("--- SS7 Down ---\n");
883 linkset->state = LINKSET_STATE_DOWN;
884 for (i = 0; i < linkset->numchans; i++) {
885 p = linkset->pvts[i];
887 sig_ss7_set_alarm(p, 1);
892 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
895 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
898 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
902 p = linkset->pvts[chanpos];
903 sig_ss7_lock_private(p);
904 callid = func_ss7_linkset_callid(linkset, chanpos);
906 switch (e->cpg.event) {
907 case CPG_EVENT_ALERTING:
908 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
909 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
911 sig_ss7_lock_owner(linkset, chanpos);
913 ast_setstate(p->owner, AST_STATE_RINGING);
914 ast_channel_unlock(p->owner);
916 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
918 case CPG_EVENT_PROGRESS:
919 case CPG_EVENT_INBANDINFO:
921 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
922 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
924 sig_ss7_set_dialing(p, 0);
925 sig_ss7_open_media(p);
929 ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
933 sig_ss7_unlock_private(p);
936 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
937 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
941 p = linkset->pvts[chanpos];
942 sig_ss7_lock_private(p);
943 callid = func_ss7_linkset_callid(linkset, chanpos);
944 sig_ss7_set_inservice(p, 1);
945 sig_ss7_set_remotelyblocked(p, 0);
946 isup_set_call_dpc(e->rsc.call, p->dpc);
947 sig_ss7_lock_owner(linkset, chanpos);
950 ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_RSC", AST_CAUSE_INTERWORKING);
951 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
952 ast_channel_unlock(p->owner);
954 sig_ss7_unlock_private(p);
955 isup_rlc(ss7, e->rsc.call);
958 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
959 chanpos = ss7_find_cic_gripe(linkset, e->grs.startcic, e->grs.opc, "GRS");
963 p = linkset->pvts[chanpos];
964 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
965 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
966 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
969 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
970 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
973 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
974 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
975 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
978 ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
979 chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
981 isup_rel(ss7, e->iam.call, -1);
984 p = linkset->pvts[chanpos];
985 sig_ss7_lock_private(p);
986 sig_ss7_lock_owner(linkset, chanpos);
987 if (p->call_level != SIG_SS7_CALL_LEVEL_IDLE) {
989 * Detected glare/dual-seizure
991 * Always abort both calls since we can't implement the dual
992 * seizure procedures due to our channel assignment architecture
993 * and the fact that we cannot tell libss7 to discard its call
994 * structure to ignore the incoming IAM.
997 "Linkset %d: SS7 IAM glare on CIC/DPC %d/%d. Dropping both calls.\n",
998 linkset->span, e->iam.cic, e->iam.opc);
999 if (p->call_level == SIG_SS7_CALL_LEVEL_ALLOCATED) {
1001 * We have not sent our IAM yet and we never will at this point.
1003 p->alreadyhungup = 1;
1004 isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1006 p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
1008 ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_IAM (glare)", AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1009 ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1010 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1011 ast_channel_unlock(p->owner);
1013 sig_ss7_unlock_private(p);
1017 * The channel should not have an owner at this point since we
1018 * are in the process of creating an owner for it.
1020 ast_assert(!p->owner);
1022 if (!sig_ss7_is_chan_available(p)) {
1023 /* Circuit is likely blocked or in alarm. */
1024 isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1025 sig_ss7_unlock_private(p);
1029 /* Mark channel as in use so no outgoing call will steal it. */
1030 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1031 p->ss7call = e->iam.call;
1033 isup_set_call_dpc(p->ss7call, p->dpc);
1035 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
1036 ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
1037 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1042 if (!ast_strlen_zero(e->iam.called_party_num)) {
1043 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
1044 e->iam.called_party_num, e->iam.called_nai);
1048 sig_ss7_set_dnid(p, p->exten);
1053 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
1055 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
1056 st = strchr(p->exten, '#');
1064 p->cid_ani[0] = '\0';
1065 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
1066 ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
1068 p->cid_name[0] = '\0';
1070 p->cid_ani2 = e->iam.oli_ani2;
1072 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
1073 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
1074 p->gen_add_type = e->iam.gen_add_type;
1075 p->gen_add_nai = e->iam.gen_add_nai;
1076 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
1077 p->gen_add_num_plan = e->iam.gen_add_num_plan;
1078 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
1079 p->gen_dig_type = e->iam.gen_dig_type;
1080 p->gen_dig_scheme = e->iam.gen_dig_scheme;
1081 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
1082 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
1083 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
1084 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
1085 p->calling_party_cat = e->iam.calling_party_cat;
1087 sig_ss7_set_caller_id(p);
1089 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1090 if (e->iam.cot_check_required) {
1091 p->call_level = SIG_SS7_CALL_LEVEL_CONTINUITY;
1092 sig_ss7_loopback(p, 1);
1094 ss7_start_call(p, linkset);
1097 ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1098 p->alreadyhungup = 1;
1099 isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
1101 sig_ss7_unlock_private(p);
1103 case ISUP_EVENT_COT:
1104 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
1106 isup_rel(ss7, e->cot.call, -1);
1109 p = linkset->pvts[chanpos];
1111 sig_ss7_lock_private(p);
1112 if (p->loopedback) {
1113 sig_ss7_loopback(p, 0);
1114 ss7_start_call(p, linkset);
1116 sig_ss7_unlock_private(p);
1118 case ISUP_EVENT_CCR:
1119 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
1120 chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
1125 p = linkset->pvts[chanpos];
1127 sig_ss7_lock_private(p);
1128 sig_ss7_loopback(p, 1);
1129 sig_ss7_unlock_private(p);
1131 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
1133 case ISUP_EVENT_CVT:
1134 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
1135 chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
1140 p = linkset->pvts[chanpos];
1142 sig_ss7_lock_private(p);
1143 sig_ss7_loopback(p, 1);
1144 sig_ss7_unlock_private(p);
1146 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
1148 case ISUP_EVENT_REL:
1149 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
1151 /* Continue hanging up the call anyway. */
1152 isup_rlc(ss7, e->rel.call);
1155 p = linkset->pvts[chanpos];
1156 sig_ss7_lock_private(p);
1157 callid = func_ss7_linkset_callid(linkset, chanpos);
1158 sig_ss7_lock_owner(linkset, chanpos);
1160 snprintf(cause_str, sizeof(cause_str), "SS7 ISUP_EVENT_REL (%d)", e->rel.cause);
1161 ss7_queue_pvt_cause_data(p->owner, cause_str, e->rel.cause);
1163 ast_channel_hangupcause_set(p->owner, e->rel.cause);
1164 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1165 ast_channel_unlock(p->owner);
1168 /* End the loopback if we have one */
1169 sig_ss7_loopback(p, 0);
1171 isup_rlc(ss7, e->rel.call);
1174 sig_ss7_unlock_private(p);
1176 case ISUP_EVENT_ACM:
1177 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
1179 isup_rel(ss7, e->acm.call, -1);
1183 p = linkset->pvts[chanpos];
1185 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
1187 if (e->acm.call_ref_ident > 0) {
1188 p->rlt = 1; /* Setting it but not using it here*/
1191 sig_ss7_lock_private(p);
1192 callid = func_ss7_linkset_callid(linkset, chanpos);
1193 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
1194 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
1195 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
1197 sig_ss7_set_dialing(p, 0);
1198 /* Send alerting if subscriber is free */
1199 if (e->acm.called_party_status_ind == 1) {
1200 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
1201 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1203 sig_ss7_lock_owner(linkset, chanpos);
1205 ast_setstate(p->owner, AST_STATE_RINGING);
1206 ast_channel_unlock(p->owner);
1208 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
1210 sig_ss7_unlock_private(p);
1213 case ISUP_EVENT_CGB:
1214 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
1218 p = linkset->pvts[chanpos];
1219 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
1220 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
1222 case ISUP_EVENT_CGU:
1223 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
1227 p = linkset->pvts[chanpos];
1228 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
1229 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
1231 case ISUP_EVENT_UCIC:
1232 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
1236 p = linkset->pvts[chanpos];
1237 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
1238 sig_ss7_lock_private(p);
1239 sig_ss7_set_remotelyblocked(p, 1);
1240 sig_ss7_set_inservice(p, 0);
1241 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
1243 case ISUP_EVENT_BLO:
1244 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
1248 p = linkset->pvts[chanpos];
1249 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
1250 sig_ss7_lock_private(p);
1251 sig_ss7_set_remotelyblocked(p, 1);
1252 sig_ss7_unlock_private(p);
1253 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
1255 case ISUP_EVENT_BLA:
1256 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
1260 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
1261 p = linkset->pvts[chanpos];
1262 sig_ss7_lock_private(p);
1263 sig_ss7_set_locallyblocked(p, 1);
1264 sig_ss7_unlock_private(p);
1266 case ISUP_EVENT_UBL:
1267 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
1271 p = linkset->pvts[chanpos];
1272 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
1273 sig_ss7_lock_private(p);
1274 sig_ss7_set_remotelyblocked(p, 0);
1275 sig_ss7_unlock_private(p);
1276 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
1278 case ISUP_EVENT_UBA:
1279 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
1283 p = linkset->pvts[chanpos];
1284 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
1285 sig_ss7_lock_private(p);
1286 sig_ss7_set_locallyblocked(p, 0);
1287 sig_ss7_unlock_private(p);
1289 case ISUP_EVENT_CON:
1290 case ISUP_EVENT_ANM:
1291 if (e->e == ISUP_EVENT_CON) {
1292 chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
1294 isup_rel(ss7, e->con.call, -1);
1298 chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
1300 isup_rel(ss7, e->anm.call, -1);
1306 p = linkset->pvts[chanpos];
1307 sig_ss7_lock_private(p);
1308 callid = func_ss7_linkset_callid(linkset, chanpos);
1309 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1310 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1312 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
1313 sig_ss7_set_dialing(p, 0);
1314 sig_ss7_open_media(p);
1315 sig_ss7_set_echocanceller(p, 1);
1316 sig_ss7_unlock_private(p);
1319 case ISUP_EVENT_RLC:
1320 /* XXX Call ptr should be passed up from libss7! */
1321 chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
1326 p = linkset->pvts[chanpos];
1327 sig_ss7_lock_private(p);
1328 callid = func_ss7_linkset_callid(linkset, chanpos);
1329 if (p->alreadyhungup) {
1331 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1335 sig_ss7_unlock_private(p);
1338 case ISUP_EVENT_FAA:
1340 * \todo The handling of the SS7 FAA message is not good and I
1341 * don't know enough to handle it correctly.
1343 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
1345 isup_rel(linkset->ss7, e->faa.call, -1);
1349 /* XXX FAR and FAA used for something dealing with transfers? */
1350 p = linkset->pvts[chanpos];
1351 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
1352 sig_ss7_lock_private(p);
1353 callid = func_ss7_linkset_callid(linkset, chanpos);
1354 if (p->alreadyhungup){
1356 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1358 /* XXX We seem to be leaking the isup call structure here. */
1360 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
1362 sig_ss7_unlock_private(p);
1366 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
1370 /* Call ID stuff needs to be cleaned up here */
1372 callid = ast_callid_unref(callid);
1373 ast_callid_threadassoc_remove();
1376 ast_mutex_unlock(&linkset->lock);
1382 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
1384 ast_mutex_unlock(&ss7->lock);
1387 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
1389 /* Grab the lock first */
1390 while (ast_mutex_trylock(&ss7->lock)) {
1391 /* Avoid deadlock */
1392 sig_ss7_deadlock_avoidance_private(pvt);
1394 /* Then break the poll */
1395 if (ss7->master != AST_PTHREADT_NULL) {
1396 pthread_kill(ss7->master, SIGURG);
1401 * \brief Notify the SS7 layer that the link is in alarm.
1404 * \param linkset Controlling linkset for the channel.
1405 * \param which Link index of the signaling channel.
1409 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
1411 linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
1412 linkset->linkstate[which] &= ~LINKSTATE_UP;
1413 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1417 * \brief Notify the SS7 layer that the link is no longer in alarm.
1420 * \param linkset Controlling linkset for the channel.
1421 * \param which Link index of the signaling channel.
1425 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
1427 linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
1428 linkset->linkstate[which] |= LINKSTATE_STARTING;
1429 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1433 * \brief Setup and add a SS7 link channel.
1436 * \param linkset Controlling linkset for the channel.
1437 * \param which Link index of the signaling channel.
1438 * \param ss7type Switch type of the linkset
1439 * \param transport Signaling transport of channel.
1440 * \param inalarm Non-zero if the channel is in alarm.
1441 * \param networkindicator User configuration parameter.
1442 * \param pointcode User configuration parameter.
1443 * \param adjpointcode User configuration parameter.
1445 * \retval 0 on success.
1446 * \retval -1 on error.
1448 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
1450 if (!linkset->ss7) {
1451 linkset->type = ss7type;
1452 linkset->ss7 = ss7_new(ss7type);
1453 if (!linkset->ss7) {
1454 ast_log(LOG_ERROR, "Can't create new SS7!\n");
1459 ss7_set_network_ind(linkset->ss7, networkindicator);
1460 ss7_set_pc(linkset->ss7, pointcode);
1462 if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
1463 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
1467 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
1468 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1470 linkset->linkstate[which] = LINKSTATE_DOWN;
1471 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1474 ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
1480 * \brief Determine if the specified channel is available for an outgoing call.
1483 * \param p Signaling private structure pointer.
1485 * \retval TRUE if the channel is available.
1487 int sig_ss7_available(struct sig_ss7_chan *p)
1492 /* Something is wrong here. A SS7 channel without the ss7 pointer? */
1496 /* Only have to deal with the linkset lock. */
1497 ast_mutex_lock(&p->ss7->lock);
1498 available = sig_ss7_is_chan_available(p);
1500 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1502 ast_mutex_unlock(&p->ss7->lock);
1507 static unsigned char cid_pres2ss7pres(int cid_pres)
1509 return (cid_pres >> 5) & 0x03;
1512 static unsigned char cid_pres2ss7screen(int cid_pres)
1514 return cid_pres & 0x03;
1518 * \brief Dial out using the specified SS7 channel.
1521 * \param p Signaling private structure pointer.
1522 * \param ast Asterisk channel structure pointer.
1523 * \param rdest Dialstring.
1525 * \retval 0 on success.
1526 * \retval -1 on error.
1528 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
1530 char ss7_called_nai;
1531 int called_nai_strip;
1532 char ss7_calling_nai;
1533 int calling_nai_strip;
1534 const char *charge_str = NULL;
1535 const char *gen_address = NULL;
1536 const char *gen_digits = NULL;
1537 const char *gen_dig_type = NULL;
1538 const char *gen_dig_scheme = NULL;
1539 const char *gen_name = NULL;
1540 const char *jip_digits = NULL;
1541 const char *lspi_ident = NULL;
1542 const char *rlt_flag = NULL;
1543 const char *call_ref_id = NULL;
1544 const char *call_ref_pc = NULL;
1545 const char *send_far = NULL;
1550 ast_copy_string(dest, rdest, sizeof(dest));
1552 c = strchr(dest, '/');
1558 if (strlen(c) < p->stripmsd) {
1559 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1563 if (!p->hidecallerid) {
1564 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1569 ss7_grab(p, p->ss7);
1571 if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
1572 /* Call collision before sending IAM. Abort call. */
1577 p->ss7call = isup_new_call(p->ss7->ss7);
1580 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
1584 called_nai_strip = 0;
1585 ss7_called_nai = p->ss7->called_nai;
1586 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
1587 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1588 called_nai_strip = strlen(p->ss7->internationalprefix);
1589 ss7_called_nai = SS7_NAI_INTERNATIONAL;
1590 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1591 called_nai_strip = strlen(p->ss7->nationalprefix);
1592 ss7_called_nai = SS7_NAI_NATIONAL;
1594 ss7_called_nai = SS7_NAI_SUBSCRIBER;
1597 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
1599 calling_nai_strip = 0;
1600 ss7_calling_nai = p->ss7->calling_nai;
1601 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
1602 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1603 calling_nai_strip = strlen(p->ss7->internationalprefix);
1604 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
1605 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1606 calling_nai_strip = strlen(p->ss7->nationalprefix);
1607 ss7_calling_nai = SS7_NAI_NATIONAL;
1609 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
1612 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
1613 p->use_callingpres ? cid_pres2ss7pres(ast_channel_connected(ast)->id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
1614 p->use_callingpres ? cid_pres2ss7screen(ast_channel_connected(ast)->id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
1616 isup_set_oli(p->ss7call, ast_channel_connected(ast)->ani2);
1617 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
1619 /* Set the charge number if it is set */
1620 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
1622 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
1624 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
1626 isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type); /* need to add some types here for NAI,PRES,TYPE */
1628 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
1629 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
1630 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
1632 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
1634 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
1636 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
1638 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
1640 isup_set_jip_digits(p->ss7call, jip_digits);
1642 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
1644 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
1646 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
1647 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
1648 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
1651 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
1652 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
1653 if (call_ref_id && call_ref_pc) {
1654 isup_set_callref(p->ss7call, atoi(call_ref_id),
1655 call_ref_pc ? atoi(call_ref_pc) : 0);
1658 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
1659 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
1660 (isup_far(p->ss7->ss7, p->ss7call));
1662 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
1663 isup_iam(p->ss7->ss7, p->ss7call);
1664 sig_ss7_set_dialing(p, 1);
1665 ast_setstate(ast, AST_STATE_DIALING);
1671 * \brief SS7 hangup channel.
1674 * \param p Signaling private structure pointer.
1675 * \param ast Asterisk channel structure pointer.
1677 * \retval 0 on success.
1678 * \retval -1 on error.
1680 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
1684 if (!ast_channel_tech_pvt(ast)) {
1685 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1690 sig_ss7_set_dialing(p, 0);
1691 sig_ss7_set_outgoing(p, 0);
1695 /* Perform low level hangup if no owner left */
1696 ss7_grab(p, p->ss7);
1697 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1699 if (!p->alreadyhungup) {
1700 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
1701 int icause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : -1;
1705 icause = atoi(cause);
1708 isup_rel(p->ss7->ss7, p->ss7call, icause);
1709 p->alreadyhungup = 1;
1718 * \brief SS7 answer channel.
1721 * \param p Signaling private structure pointer.
1722 * \param ast Asterisk channel structure pointer.
1724 * \retval 0 on success.
1725 * \retval -1 on error.
1727 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
1731 ss7_grab(p, p->ss7);
1732 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1733 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1735 sig_ss7_open_media(p);
1736 res = isup_anm(p->ss7->ss7, p->ss7call);
1742 * \brief Fix up a channel: If a channel is consumed, this is called. Basically update any ->owner links.
1745 * \param oldchan Old channel pointer to replace.
1746 * \param newchan New channel pointer to set.
1747 * \param pchan Signaling private structure pointer.
1751 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
1753 if (pchan->owner == oldchan) {
1754 pchan->owner = newchan;
1759 * \brief SS7 answer channel.
1762 * \param p Signaling private structure pointer.
1763 * \param chan Asterisk channel structure pointer.
1764 * \param condition AST control frame subtype.
1765 * \param data AST control frame payload contents.
1766 * \param datalen Length of payload contents.
1768 * \retval 0 on success.
1769 * \retval -1 on error or indication condition not handled.
1771 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
1775 switch (condition) {
1776 case AST_CONTROL_BUSY:
1777 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1778 ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
1779 ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1783 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
1785 case AST_CONTROL_RINGING:
1786 ss7_grab(p, p->ss7);
1787 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1788 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1789 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
1793 /* No need to send CPG if call will be RELEASE */
1795 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
1800 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
1802 if (ast_channel_state(chan) != AST_STATE_UP && ast_channel_state(chan) != AST_STATE_RING) {
1803 ast_setstate(chan, AST_STATE_RINGING);
1806 case AST_CONTROL_PROCEEDING:
1807 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",ast_channel_name(chan));
1808 ss7_grab(p, p->ss7);
1809 /* This IF sends the FAR for an answered ALEG call */
1810 if (ast_channel_state(chan) == AST_STATE_UP && (p->rlt != 1)){
1811 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
1816 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
1817 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
1818 isup_acm(p->ss7->ss7, p->ss7call);
1821 /* don't continue in ast_indicate */
1824 case AST_CONTROL_PROGRESS:
1825 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",ast_channel_name(chan));
1826 ss7_grab(p, p->ss7);
1827 if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1828 p->progress = 1;/* No need to send inband-information progress again. */
1829 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
1832 /* enable echo canceler here on SS7 calls */
1833 sig_ss7_set_echocanceller(p, 1);
1837 /* don't continue in ast_indicate */
1840 case AST_CONTROL_INCOMPLETE:
1841 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1842 ast_channel_hangupcause_set(chan, AST_CAUSE_INVALID_NUMBER_FORMAT);
1843 ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1847 /* Wait for DTMF digits to complete the dialed number. */
1850 case AST_CONTROL_CONGESTION:
1851 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1852 ast_channel_hangupcause_set(chan, AST_CAUSE_CONGESTION);
1853 ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1857 res = sig_ss7_play_tone(p, SIG_SS7_TONE_CONGESTION);
1859 case AST_CONTROL_HOLD:
1860 ast_moh_start(chan, data, p->mohinterpret);
1862 case AST_CONTROL_UNHOLD:
1865 case AST_CONTROL_SRCUPDATE:
1869 res = sig_ss7_play_tone(p, -1);
1876 * \brief SS7 channel request.
1879 * \param p Signaling private structure pointer.
1880 * \param law Companding law preferred
1881 * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
1882 * \param transfercapability
1884 * \retval ast_channel on success.
1885 * \retval NULL on error.
1887 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
1889 struct ast_channel *ast;
1891 /* Companding law is determined by SS7 signaling type. */
1892 if (p->ss7->type == SS7_ITU) {
1898 sig_ss7_set_outgoing(p, 1);
1899 ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1901 sig_ss7_set_outgoing(p, 0);
1903 /* Release the allocated channel. Only have to deal with the linkset lock. */
1904 ast_mutex_lock(&p->ss7->lock);
1905 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1906 ast_mutex_unlock(&p->ss7->lock);
1912 * \brief Delete the sig_ss7 private channel structure.
1915 * \param doomed sig_ss7 private channel structure to delete.
1919 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
1924 #define SIG_SS7_SC_HEADER "%-4s %4s %-4s %-3s %-3s %-10s %-4s %s\n"
1925 #define SIG_SS7_SC_LINE "%4d %4d %-4s %-3s %-3s %-10s %-4s %s"
1926 void sig_ss7_cli_show_channels_header(int fd)
1928 ast_cli(fd, SIG_SS7_SC_HEADER, "link", "", "Chan", "Lcl", "Rem", "Call", "SS7", "Channel");
1929 ast_cli(fd, SIG_SS7_SC_HEADER, "set", "Chan", "Idle", "Blk", "Blk", "Level", "Call", "Name");
1932 void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
1936 struct sig_ss7_chan *pvt;
1938 ast_mutex_lock(&linkset->lock);
1939 for (idx = 0; idx < linkset->numchans; ++idx) {
1940 if (!linkset->pvts[idx]) {
1943 pvt = linkset->pvts[idx];
1944 sig_ss7_lock_private(pvt);
1945 sig_ss7_lock_owner(linkset, idx);
1947 snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
1950 sig_ss7_is_chan_available(pvt) ? "Yes" : "No",
1951 pvt->locallyblocked ? "Yes" : "No",
1952 pvt->remotelyblocked ? "Yes" : "No",
1953 sig_ss7_call_level2str(pvt->call_level),
1954 pvt->ss7call ? "Yes" : "No",
1955 pvt->owner ? ast_channel_name(pvt->owner) : "");
1958 ast_channel_unlock(pvt->owner);
1960 sig_ss7_unlock_private(pvt);
1962 ast_mutex_unlock(&linkset->lock);
1963 ast_cli(fd, "%s\n", line);
1964 ast_mutex_lock(&linkset->lock);
1966 ast_mutex_unlock(&linkset->lock);
1970 * \brief Create a new sig_ss7 private channel structure.
1973 * \param pvt_data Upper layer private data structure.
1974 * \param ss7 Controlling linkset for the channel.
1976 * \retval sig_ss7_chan on success.
1977 * \retval NULL on error.
1979 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_linkset *ss7)
1981 struct sig_ss7_chan *pvt;
1983 pvt = ast_calloc(1, sizeof(*pvt));
1988 pvt->chan_pvt = pvt_data;
1995 * \brief Initialize the SS7 linkset control.
1998 * \param ss7 SS7 linkset control structure.
2002 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
2006 memset(ss7, 0, sizeof(*ss7));
2008 ast_mutex_init(&ss7->lock);
2010 ss7->master = AST_PTHREADT_NULL;
2011 for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
2016 /* ------------------------------------------------------------------- */
2018 #endif /* defined(HAVE_SS7) */