chan_dahdi: CLI "core stop gracefully" has needless delay for PRI and SS7.
[asterisk/asterisk.git] / channels / sig_ss7.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010 Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
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  * \brief SS7 signaling module.
22  *
23  * \author Matthew Fredrickson <creslin@digium.com>
24  * \author Richard Mudgett <rmudgett@digium.com>
25  *
26  * See Also:
27  * \arg \ref AstCREDITS
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #if defined(HAVE_SS7)
37
38 #include <signal.h>
39
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"
45
46 #include "sig_ss7.h"
47 #if defined(LIBSS7_ABI_COMPATIBILITY)
48 #error "Your installed libss7 is not compatible"
49 #endif
50
51 /* ------------------------------------------------------------------- */
52
53 static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
54 {
55         switch (level) {
56         case SIG_SS7_CALL_LEVEL_IDLE:
57                 return "Idle";
58         case SIG_SS7_CALL_LEVEL_ALLOCATED:
59                 return "Allocated";
60         case SIG_SS7_CALL_LEVEL_CONTINUITY:
61                 return "Continuity";
62         case SIG_SS7_CALL_LEVEL_SETUP:
63                 return "Setup";
64         case SIG_SS7_CALL_LEVEL_PROCEEDING:
65                 return "Proceeding";
66         case SIG_SS7_CALL_LEVEL_ALERTING:
67                 return "Alerting";
68         case SIG_SS7_CALL_LEVEL_CONNECT:
69                 return "Connect";
70         case SIG_SS7_CALL_LEVEL_GLARE:
71                 return "Glare";
72         }
73         return "Unknown";
74 }
75
76 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
77 {
78         if (sig_ss7_callbacks.unlock_private) {
79                 sig_ss7_callbacks.unlock_private(p->chan_pvt);
80         }
81 }
82
83 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
84 {
85         if (sig_ss7_callbacks.lock_private) {
86                 sig_ss7_callbacks.lock_private(p->chan_pvt);
87         }
88 }
89
90 static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
91 {
92         if (sig_ss7_callbacks.deadlock_avoidance_private) {
93                 sig_ss7_callbacks.deadlock_avoidance_private(p->chan_pvt);
94         } else {
95                 /* Fallback to the old way if callback not present. */
96                 sig_ss7_unlock_private(p);
97                 sched_yield();
98                 sig_ss7_lock_private(p);
99         }
100 }
101
102 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
103 {
104         p->inalarm = in_alarm;
105         if (sig_ss7_callbacks.set_alarm) {
106                 sig_ss7_callbacks.set_alarm(p->chan_pvt, in_alarm);
107         }
108 }
109
110 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
111 {
112         if (sig_ss7_callbacks.set_dialing) {
113                 sig_ss7_callbacks.set_dialing(p->chan_pvt, is_dialing);
114         }
115 }
116
117 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
118 {
119         if (sig_ss7_callbacks.set_digital) {
120                 sig_ss7_callbacks.set_digital(p->chan_pvt, is_digital);
121         }
122 }
123
124 static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
125 {
126         p->outgoing = is_outgoing;
127         if (sig_ss7_callbacks.set_outgoing) {
128                 sig_ss7_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
129         }
130 }
131
132 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
133 {
134         if (sig_ss7_callbacks.set_inservice) {
135                 sig_ss7_callbacks.set_inservice(p->chan_pvt, is_inservice);
136         }
137 }
138
139 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
140 {
141         p->locallyblocked = is_blocked;
142         if (sig_ss7_callbacks.set_locallyblocked) {
143                 sig_ss7_callbacks.set_locallyblocked(p->chan_pvt, is_blocked);
144         }
145 }
146
147 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
148 {
149         p->remotelyblocked = is_blocked;
150         if (sig_ss7_callbacks.set_remotelyblocked) {
151                 sig_ss7_callbacks.set_remotelyblocked(p->chan_pvt, is_blocked);
152         }
153 }
154
155 /*!
156  * \internal
157  * \brief Open the SS7 channel media path.
158  * \since 1.8.12
159  *
160  * \param p Channel private control structure.
161  *
162  * \return Nothing
163  */
164 static void sig_ss7_open_media(struct sig_ss7_chan *p)
165 {
166         if (sig_ss7_callbacks.open_media) {
167                 sig_ss7_callbacks.open_media(p->chan_pvt);
168         }
169 }
170
171 /*!
172  * \internal
173  * \brief Set the caller id information in the parent module.
174  * \since 1.8
175  *
176  * \param p sig_ss7 channel structure.
177  *
178  * \return Nothing
179  */
180 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
181 {
182         struct ast_party_caller caller;
183
184         if (sig_ss7_callbacks.set_callerid) {
185                 ast_party_caller_init(&caller);
186
187                 caller.id.name.str = p->cid_name;
188                 caller.id.name.presentation = p->callingpres;
189                 caller.id.name.valid = 1;
190
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;
195
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;
201                 }
202
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;
207
208                 caller.ani2 = p->cid_ani2;
209                 sig_ss7_callbacks.set_callerid(p->chan_pvt, &caller);
210         }
211 }
212
213 /*!
214  * \internal
215  * \brief Set the Dialed Number Identifier.
216  * \since 1.8
217  *
218  * \param p sig_ss7 channel structure.
219  * \param dnid Dialed Number Identifier string.
220  *
221  * \return Nothing
222  */
223 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
224 {
225         if (sig_ss7_callbacks.set_dnid) {
226                 sig_ss7_callbacks.set_dnid(p->chan_pvt, dnid);
227         }
228 }
229
230 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
231 {
232         int res;
233
234         if (sig_ss7_callbacks.play_tone) {
235                 res = sig_ss7_callbacks.play_tone(p->chan_pvt, tone);
236         } else {
237                 res = -1;
238         }
239         return res;
240 }
241
242 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
243 {
244         if (sig_ss7_callbacks.set_echocanceller) {
245                 return sig_ss7_callbacks.set_echocanceller(p->chan_pvt, enable);
246         }
247         return -1;
248 }
249
250 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
251 {
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);
256                 }
257         }
258 }
259
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)
261 {
262         struct ast_channel *ast;
263
264         if (sig_ss7_callbacks.new_ast_channel) {
265                 ast = sig_ss7_callbacks.new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
266         } else {
267                 return NULL;
268         }
269         if (!ast) {
270                 return NULL;
271         }
272
273         if (!p->owner) {
274                 p->owner = ast;
275         }
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);
282         }
283
284         return ast;
285 }
286
287 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
288 {
289         if (sig_ss7_callbacks.handle_link_exception) {
290                 sig_ss7_callbacks.handle_link_exception(linkset, which);
291         }
292 }
293
294 /*!
295  * \internal
296  * \brief Determine if a private channel structure is available.
297  *
298  * \param pvt Channel to determine if available.
299  *
300  * \return TRUE if the channel is available.
301  */
302 static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
303 {
304         if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
305                 && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
306                 && !pvt->locallyblocked && !pvt->remotelyblocked) {
307                 return 1;
308         }
309         return 0;
310 }
311
312 /*!
313  * \internal
314  * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
315  * \since 1.8
316  *
317  * \param ss7 SS7 linkset control structure.
318  * \param chanpos Channel position in the span.
319  *
320  * \note Assumes the ss7->lock is already obtained.
321  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
322  *
323  * \return Nothing
324  */
325 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
326 {
327         for (;;) {
328                 if (!ss7->pvts[chanpos]->owner) {
329                         /* There is no owner lock to get. */
330                         break;
331                 }
332                 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
333                         /* We got the lock */
334                         break;
335                 }
336
337                 /* Avoid deadlock */
338                 sig_ss7_unlock_private(ss7->pvts[chanpos]);
339                 DEADLOCK_AVOIDANCE(&ss7->lock);
340                 sig_ss7_lock_private(ss7->pvts[chanpos]);
341         }
342 }
343
344 /*!
345  * \internal
346  * \brief Queue the given frame onto the owner channel.
347  * \since 1.8
348  *
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.
352  *
353  * \note Assumes the ss7->lock is already obtained.
354  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
355  *
356  * \return Nothing
357  */
358 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
359 {
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);
364         }
365 }
366
367 /*!
368  * \internal
369  * \brief Queue a control frame of the specified subclass onto the owner channel.
370  * \since 1.8
371  *
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.
375  *
376  * \note Assumes the ss7->lock is already obtained.
377  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
378  *
379  * \return Nothing
380  */
381 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
382 {
383         struct ast_frame f = {AST_FRAME_CONTROL, };
384         struct sig_ss7_chan *p = ss7->pvts[chanpos];
385
386         if (sig_ss7_callbacks.queue_control) {
387                 sig_ss7_callbacks.queue_control(p->chan_pvt, subclass);
388         }
389
390         f.subclass.integer = subclass;
391         sig_ss7_queue_frame(ss7, chanpos, &f);
392 }
393
394 /*!
395  * \internal
396  * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
397  * \since 11
398  *
399  * \param owner Owner channel of the pvt.
400  * \param cause String describing the cause to be placed into the frame.
401  *
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.
405  *
406  * \return Nothing
407  */
408 static void ss7_queue_pvt_cause_data(struct ast_channel *owner, const char *cause, int ast_cause)
409 {
410         struct ast_control_pvt_cause_code *cause_code;
411         int datalen = sizeof(*cause_code) + strlen(cause);
412
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);
420 }
421
422
423 /*!
424  * \internal
425  * \brief Find the channel position by CIC/DPC.
426  *
427  * \param linkset SS7 linkset control structure.
428  * \param cic Circuit Identification Code
429  * \param dpc Destination Point Code
430  *
431  * \retval chanpos on success.
432  * \retval -1 on error.
433  */
434 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
435 {
436         int i;
437         int winner = -1;
438         for (i = 0; i < linkset->numchans; i++) {
439                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
440                         winner = i;
441                         break;
442                 }
443         }
444         return winner;
445 }
446
447 /*!
448  * \internal
449  * \brief Find the channel position by CIC/DPC and gripe if not found.
450  *
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.
455  *
456  * \retval chanpos on success.
457  * \retval -1 on error.
458  */
459 static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
460 {
461         int chanpos;
462
463         chanpos = ss7_find_cic(linkset, cic, dpc);
464         if (chanpos < 0) {
465                 ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested unconfigured CIC/DPC %d/%d.\n",
466                         linkset->span, msg_name, cic, dpc);
467                 return -1;
468         }
469         return chanpos;
470 }
471
472 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
473 {
474         unsigned char status[32];
475         struct sig_ss7_chan *p = NULL;
476         int i, offset;
477
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;
482                         status[offset] = 0;
483                         if (p->locallyblocked)
484                                 status[offset] |= (1 << 0) | (1 << 4);
485                         if (p->remotelyblocked)
486                                 status[offset] |= (1 << 1) | (1 << 5);
487                         if (p->ss7call) {
488                                 if (p->outgoing)
489                                         status[offset] |= (1 << 3);
490                                 else
491                                         status[offset] |= (1 << 2);
492                         } else
493                                 status[offset] |= 0x3 << 2;
494                 }
495         }
496
497         if (p)
498                 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
499         else
500                 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
501
502 }
503
504 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
505 {
506         int i;
507
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);
515                         }
516                         sig_ss7_unlock_private(linkset->pvts[i]);
517                 }
518         }
519 }
520
521 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
522 {
523         int i;
524
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)))) {
528                         if (state) {
529                                 if (state[i])
530                                         sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
531                         } else
532                                 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
533                 }
534         }
535 }
536
537 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
538 {
539         int i;
540
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);
544         }
545 }
546
547 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
548 {
549         int i, startcic = -1, endcic, dpc;
550
551         if (linkset->numchans <= 0)
552                 return;
553
554         startcic = linkset->pvts[0]->cic;
555         /* DB: CIC's DPC fix */
556         dpc = linkset->pvts[0]->dpc;
557
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)) {
560                         continue;
561                 } else {
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);
565
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;
570                         }
571                 }
572         }
573 }
574
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)
577 {
578         struct ss7 *ss7 = linkset->ss7;
579         int law;
580         struct ast_channel *c;
581         char tmp[256];
582         struct ast_callid *callid = NULL;
583         int callid_created = ast_callid_threadstorage_auto(&callid);
584
585         if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
586                 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
587                 isup_acm(ss7, p->ss7call);
588         } else {
589                 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
590         }
591
592         /* Companding law is determined by SS7 signaling type. */
593         if (linkset->type == SS7_ITU) {
594                 law = SIG_SS7_ALAW;
595         } else {
596                 law = SIG_SS7_ULAW;
597         }
598
599         /*
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.
603          */
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);
607         if (!c) {
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);
615                 return;
616         }
617
618         /* Hold the channel and private lock while we setup the channel. */
619         ast_channel_lock(c);
620         sig_ss7_lock_private(p);
621
622         sig_ss7_set_echocanceller(p, 1);
623
624         /*
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
630          * created.
631          *
632          * We only reference these variables in the context of the ss7_linkset function
633          * when receiving either and IAM or a COT message.
634          */
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;
639         }
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;
644         }
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;
649         }
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;
654         }
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;
659         }
660
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 */
664         p->gen_dig_type = 0;
665
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;
670
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;
675         }
676
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;
681
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 */
685         p->call_ref_pc = 0;
686
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;
691
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;
696         }
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;
701         }
702
703         sig_ss7_unlock_private(p);
704         ast_channel_unlock(c);
705
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);
708                 ast_hangup(c);
709         } else {
710                 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
711         }
712
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);
717 }
718
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)
720 {
721         if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
722                 if (size) {
723                         *buf = '\0';
724                 }
725                 return;
726         }
727         switch (nai) {
728         case SS7_NAI_INTERNATIONAL:
729                 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
730                 break;
731         case SS7_NAI_NATIONAL:
732                 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
733                 break;
734         case SS7_NAI_SUBSCRIBER:
735                 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
736                 break;
737         case SS7_NAI_UNKNOWN:
738                 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
739                 break;
740         default:
741                 snprintf(buf, size, "%s", number);
742                 break;
743         }
744 }
745
746 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
747 {
748         return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
749 }
750
751 /*!
752  * \internal
753  * \brief Set callid threadstorage for the ss7_linkset thread to that of an existing channel
754  *
755  * \param linkset ss7 span control structure.
756  * \param chanpos channel position in the span
757  *
758  * \note Assumes the ss7->lock is already obtained.
759  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
760  *
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.
765  */
766 static struct ast_callid *func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
767 {
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);
773                 if (callid) {
774                         ast_callid_threadassoc_add(callid);
775                 }
776         }
777
778         return callid;
779 }
780
781 /* This is a thread per linkset that handles all received events from libss7. */
782 void *ss7_linkset(void *data)
783 {
784         int res, i;
785         struct timeval *next = NULL, tv;
786         struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
787         struct ss7 *ss7 = linkset->ss7;
788         ss7_event *e = NULL;
789         struct sig_ss7_chan *p;
790         struct pollfd pollers[SIG_SS7_NUM_DCHANS];
791         int nextms;
792
793 #define SS7_MAX_POLL    60000   /* Maximum poll time in ms. */
794
795         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
796
797         ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
798         ast_mutex_lock(&linkset->lock);
799         ss7_start(ss7);
800         ast_mutex_unlock(&linkset->lock);
801
802         for (;;) {
803                 ast_mutex_lock(&linkset->lock);
804                 if ((next = ss7_schedule_next(ss7))) {
805                         tv = ast_tvnow();
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;
810                                 tv.tv_sec -= 1;
811                         }
812                         if (tv.tv_sec < 0) {
813                                 tv.tv_sec = 0;
814                                 tv.tv_usec = 0;
815                         }
816                         nextms = tv.tv_sec * 1000;
817                         nextms += tv.tv_usec / 1000;
818                         if (SS7_MAX_POLL < nextms) {
819                                 nextms = SS7_MAX_POLL;
820                         }
821                 } else {
822                         nextms = SS7_MAX_POLL;
823                 }
824
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;
829                 }
830                 ast_mutex_unlock(&linkset->lock);
831
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);
837
838                 if ((res < 0) && (errno != EINTR)) {
839                         ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
840                 } else if (!res) {
841                         ast_mutex_lock(&linkset->lock);
842                         ss7_schedule_run(ss7);
843                         ast_mutex_unlock(&linkset->lock);
844                         continue;
845                 }
846
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);
851                         }
852                         if (pollers[i].revents & POLLIN) {
853                                 res = ss7_read(ss7, pollers[i].fd);
854                         }
855                         if (pollers[i].revents & POLLOUT) {
856                                 res = ss7_write(ss7, pollers[i].fd);
857                                 if (res < 0) {
858                                         ast_debug(1, "Error in write %s\n", strerror(errno));
859                                 }
860                         }
861                 }
862
863                 while ((e = ss7_check_event(ss7))) {
864                         struct ast_callid *callid = NULL;
865                         int chanpos = -1;
866                         char cause_str[30];
867
868                         if (linkset->debug) {
869                                 ast_verbose("Linkset %d: Processing event: %s\n",
870                                         linkset->span, ss7_event2str(e->e));
871                         }
872
873                         switch (e->e) {
874                         case SS7_EVENT_UP:
875                                 if (linkset->state != LINKSET_STATE_UP) {
876                                         ast_verbose("--- SS7 Up ---\n");
877                                         ss7_reset_linkset(linkset);
878                                 }
879                                 linkset->state = LINKSET_STATE_UP;
880                                 break;
881                         case SS7_EVENT_DOWN:
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];
886                                         if (p) {
887                                                 sig_ss7_set_alarm(p, 1);
888                                         }
889                                 }
890                                 break;
891                         case MTP2_LINK_UP:
892                                 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
893                                 break;
894                         case MTP2_LINK_DOWN:
895                                 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
896                                 break;
897                         case ISUP_EVENT_CPG:
898                                 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
899                                 if (chanpos < 0) {
900                                         break;
901                                 }
902                                 p = linkset->pvts[chanpos];
903                                 sig_ss7_lock_private(p);
904                                 callid = func_ss7_linkset_callid(linkset, chanpos);
905
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;
910                                         }
911                                         sig_ss7_lock_owner(linkset, chanpos);
912                                         if (p->owner) {
913                                                 ast_setstate(p->owner, AST_STATE_RINGING);
914                                                 ast_channel_unlock(p->owner);
915                                         }
916                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
917                                         break;
918                                 case CPG_EVENT_PROGRESS:
919                                 case CPG_EVENT_INBANDINFO:
920                                         {
921                                                 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
922                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
923                                                 p->progress = 1;
924                                                 sig_ss7_set_dialing(p, 0);
925                                                 sig_ss7_open_media(p);
926                                         }
927                                         break;
928                                 default:
929                                         ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
930                                         break;
931                                 }
932
933                                 sig_ss7_unlock_private(p);
934                                 break;
935                         case ISUP_EVENT_RSC:
936                                 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
937                                 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
938                                 if (chanpos < 0) {
939                                         break;
940                                 }
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);
948                                 p->ss7call = NULL;
949                                 if (p->owner) {
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);
953                                 }
954                                 sig_ss7_unlock_private(p);
955                                 isup_rlc(ss7, e->rsc.call);
956                                 break;
957                         case ISUP_EVENT_GRS:
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");
960                                 if (chanpos < 0) {
961                                         break;
962                                 }
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);
967                                 break;
968                         case ISUP_EVENT_CQM:
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);
971                                 break;
972                         case ISUP_EVENT_GRA:
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);
976                                 break;
977                         case ISUP_EVENT_IAM:
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");
980                                 if (chanpos < 0) {
981                                         isup_rel(ss7, e->iam.call, -1);
982                                         break;
983                                 }
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) {
988                                         /*
989                                          * Detected glare/dual-seizure
990                                          *
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.
995                                          */
996                                         ast_debug(1,
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) {
1000                                                 /*
1001                                                  * We have not sent our IAM yet and we never will at this point.
1002                                                  */
1003                                                 p->alreadyhungup = 1;
1004                                                 isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1005                                         }
1006                                         p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
1007                                         if (p->owner) {
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);
1012                                         }
1013                                         sig_ss7_unlock_private(p);
1014                                         break;
1015                                 }
1016                                 /*
1017                                  * The channel should not have an owner at this point since we
1018                                  * are in the process of creating an owner for it.
1019                                  */
1020                                 ast_assert(!p->owner);
1021
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);
1026                                         break;
1027                                 }
1028
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;
1032
1033                                 isup_set_call_dpc(p->ss7call, p->dpc);
1034
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);
1038                                 } else
1039                                         p->cid_num[0] = 0;
1040
1041                                 /* Set DNID */
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);
1045                                 } else {
1046                                         p->exten[0] = '\0';
1047                                 }
1048                                 sig_ss7_set_dnid(p, p->exten);
1049
1050                                 if (p->immediate) {
1051                                         p->exten[0] = 's';
1052                                         p->exten[1] = '\0';
1053                                 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
1054                                         char *st;
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, '#');
1057                                         if (st) {
1058                                                 *st = '\0';
1059                                         }
1060                                 } else {
1061                                         p->exten[0] = '\0';
1062                                 }
1063
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));
1067                                 else
1068                                         p->cid_name[0] = '\0';
1069
1070                                 p->cid_ani2 = e->iam.oli_ani2;
1071                                 p->cid_ton = 0;
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;
1086
1087                                 sig_ss7_set_caller_id(p);
1088
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);
1093                                         } else {
1094                                                 ss7_start_call(p, linkset);
1095                                         }
1096                                 } else {
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);
1100                                 }
1101                                 sig_ss7_unlock_private(p);
1102                                 break;
1103                         case ISUP_EVENT_COT:
1104                                 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
1105                                 if (chanpos < 0) {
1106                                         isup_rel(ss7, e->cot.call, -1);
1107                                         break;
1108                                 }
1109                                 p = linkset->pvts[chanpos];
1110
1111                                 sig_ss7_lock_private(p);
1112                                 if (p->loopedback) {
1113                                         sig_ss7_loopback(p, 0);
1114                                         ss7_start_call(p, linkset);
1115                                 }
1116                                 sig_ss7_unlock_private(p);
1117                                 break;
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");
1121                                 if (chanpos < 0) {
1122                                         break;
1123                                 }
1124
1125                                 p = linkset->pvts[chanpos];
1126
1127                                 sig_ss7_lock_private(p);
1128                                 sig_ss7_loopback(p, 1);
1129                                 sig_ss7_unlock_private(p);
1130
1131                                 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
1132                                 break;
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");
1136                                 if (chanpos < 0) {
1137                                         break;
1138                                 }
1139
1140                                 p = linkset->pvts[chanpos];
1141
1142                                 sig_ss7_lock_private(p);
1143                                 sig_ss7_loopback(p, 1);
1144                                 sig_ss7_unlock_private(p);
1145
1146                                 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
1147                                 break;
1148                         case ISUP_EVENT_REL:
1149                                 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
1150                                 if (chanpos < 0) {
1151                                         /* Continue hanging up the call anyway. */
1152                                         isup_rlc(ss7, e->rel.call);
1153                                         break;
1154                                 }
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);
1159                                 if (p->owner) {
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);
1162
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);
1166                                 }
1167
1168                                 /* End the loopback if we have one */
1169                                 sig_ss7_loopback(p, 0);
1170
1171                                 isup_rlc(ss7, e->rel.call);
1172                                 p->ss7call = NULL;
1173
1174                                 sig_ss7_unlock_private(p);
1175                                 break;
1176                         case ISUP_EVENT_ACM:
1177                                 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
1178                                 if (chanpos < 0) {
1179                                         isup_rel(ss7, e->acm.call, -1);
1180                                         break;
1181                                 }
1182                                 {
1183                                         p = linkset->pvts[chanpos];
1184
1185                                         ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
1186
1187                                         if (e->acm.call_ref_ident > 0) {
1188                                                 p->rlt = 1; /* Setting it but not using it here*/
1189                                         }
1190
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;
1196                                         }
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;
1202                                                 }
1203                                                 sig_ss7_lock_owner(linkset, chanpos);
1204                                                 if (p->owner) {
1205                                                         ast_setstate(p->owner, AST_STATE_RINGING);
1206                                                         ast_channel_unlock(p->owner);
1207                                                 }
1208                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
1209                                         }
1210                                         sig_ss7_unlock_private(p);
1211                                 }
1212                                 break;
1213                         case ISUP_EVENT_CGB:
1214                                 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
1215                                 if (chanpos < 0) {
1216                                         break;
1217                                 }
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);
1221                                 break;
1222                         case ISUP_EVENT_CGU:
1223                                 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
1224                                 if (chanpos < 0) {
1225                                         break;
1226                                 }
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);
1230                                 break;
1231                         case ISUP_EVENT_UCIC:
1232                                 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
1233                                 if (chanpos < 0) {
1234                                         break;
1235                                 }
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 */
1242                                 break;
1243                         case ISUP_EVENT_BLO:
1244                                 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
1245                                 if (chanpos < 0) {
1246                                         break;
1247                                 }
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);
1254                                 break;
1255                         case ISUP_EVENT_BLA:
1256                                 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
1257                                 if (chanpos < 0) {
1258                                         break;
1259                                 }
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);
1265                                 break;
1266                         case ISUP_EVENT_UBL:
1267                                 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
1268                                 if (chanpos < 0) {
1269                                         break;
1270                                 }
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);
1277                                 break;
1278                         case ISUP_EVENT_UBA:
1279                                 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
1280                                 if (chanpos < 0) {
1281                                         break;
1282                                 }
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);
1288                                 break;
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");
1293                                         if (chanpos < 0) {
1294                                                 isup_rel(ss7, e->con.call, -1);
1295                                                 break;
1296                                         }
1297                                 } else {
1298                                         chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
1299                                         if (chanpos < 0) {
1300                                                 isup_rel(ss7, e->anm.call, -1);
1301                                                 break;
1302                                         }
1303                                 }
1304
1305                                 {
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;
1311                                         }
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);
1317                                 }
1318                                 break;
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");
1322                                 if (chanpos < 0) {
1323                                         break;
1324                                 }
1325                                 {
1326                                         p = linkset->pvts[chanpos];
1327                                         sig_ss7_lock_private(p);
1328                                         callid = func_ss7_linkset_callid(linkset, chanpos);
1329                                         if (p->alreadyhungup) {
1330                                                 if (!p->owner) {
1331                                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1332                                                 }
1333                                                 p->ss7call = NULL;
1334                                         }
1335                                         sig_ss7_unlock_private(p);
1336                                 }
1337                                 break;
1338                         case ISUP_EVENT_FAA:
1339                                 /*!
1340                                  * \todo The handling of the SS7 FAA message is not good and I
1341                                  * don't know enough to handle it correctly.
1342                                  */
1343                                 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
1344                                 if (chanpos < 0) {
1345                                         isup_rel(linkset->ss7, e->faa.call, -1);
1346                                         break;
1347                                 }
1348                                 {
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){
1355                                                 if (!p->owner) {
1356                                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1357                                                 }
1358                                                 /* XXX We seem to be leaking the isup call structure here. */
1359                                                 p->ss7call = NULL;
1360                                                 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR.  Ignoring.\n");
1361                                         }
1362                                         sig_ss7_unlock_private(p);
1363                                 }
1364                                 break;
1365                         default:
1366                                 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
1367                                 break;
1368                         }
1369
1370                         /* Call ID stuff needs to be cleaned up here */
1371                         if (callid) {
1372                                 callid = ast_callid_unref(callid);
1373                                 ast_callid_threadassoc_remove();
1374                         }
1375                 }
1376                 ast_mutex_unlock(&linkset->lock);
1377         }
1378
1379         return 0;
1380 }
1381
1382 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
1383 {
1384         ast_mutex_unlock(&ss7->lock);
1385 }
1386
1387 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
1388 {
1389         /* Grab the lock first */
1390         while (ast_mutex_trylock(&ss7->lock)) {
1391                 /* Avoid deadlock */
1392                 sig_ss7_deadlock_avoidance_private(pvt);
1393         }
1394         /* Then break the poll */
1395         if (ss7->master != AST_PTHREADT_NULL) {
1396                 pthread_kill(ss7->master, SIGURG);
1397         }
1398 }
1399
1400 /*!
1401  * \brief Notify the SS7 layer that the link is in alarm.
1402  * \since 1.8
1403  *
1404  * \param linkset Controlling linkset for the channel.
1405  * \param which Link index of the signaling channel.
1406  *
1407  * \return Nothing
1408  */
1409 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
1410 {
1411         linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
1412         linkset->linkstate[which] &= ~LINKSTATE_UP;
1413         ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1414 }
1415
1416 /*!
1417  * \brief Notify the SS7 layer that the link is no longer in alarm.
1418  * \since 1.8
1419  *
1420  * \param linkset Controlling linkset for the channel.
1421  * \param which Link index of the signaling channel.
1422  *
1423  * \return Nothing
1424  */
1425 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
1426 {
1427         linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
1428         linkset->linkstate[which] |= LINKSTATE_STARTING;
1429         ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1430 }
1431
1432 /*!
1433  * \brief Setup and add a SS7 link channel.
1434  * \since 1.8
1435  *
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.
1444  *
1445  * \retval 0 on success.
1446  * \retval -1 on error.
1447  */
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)
1449 {
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");
1455                         return -1;
1456                 }
1457         }
1458
1459         ss7_set_network_ind(linkset->ss7, networkindicator);
1460         ss7_set_pc(linkset->ss7, pointcode);
1461
1462         if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
1463                 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
1464         }
1465
1466         if (inalarm) {
1467                 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
1468                 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1469         } else {
1470                 linkset->linkstate[which] = LINKSTATE_DOWN;
1471                 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1472         }
1473
1474         ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
1475
1476         return 0;
1477 }
1478
1479 /*!
1480  * \brief Determine if the specified channel is available for an outgoing call.
1481  * \since 1.8
1482  *
1483  * \param p Signaling private structure pointer.
1484  *
1485  * \retval TRUE if the channel is available.
1486  */
1487 int sig_ss7_available(struct sig_ss7_chan *p)
1488 {
1489         int available;
1490
1491         if (!p->ss7) {
1492                 /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
1493                 return 0;
1494         }
1495
1496         /* Only have to deal with the linkset lock. */
1497         ast_mutex_lock(&p->ss7->lock);
1498         available = sig_ss7_is_chan_available(p);
1499         if (available) {
1500                 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1501         }
1502         ast_mutex_unlock(&p->ss7->lock);
1503
1504         return available;
1505 }
1506
1507 static unsigned char cid_pres2ss7pres(int cid_pres)
1508 {
1509          return (cid_pres >> 5) & 0x03;
1510 }
1511
1512 static unsigned char cid_pres2ss7screen(int cid_pres)
1513 {
1514         return cid_pres & 0x03;
1515 }
1516
1517 /*!
1518  * \brief Dial out using the specified SS7 channel.
1519  * \since 1.8
1520  *
1521  * \param p Signaling private structure pointer.
1522  * \param ast Asterisk channel structure pointer.
1523  * \param rdest Dialstring.
1524  *
1525  * \retval 0 on success.
1526  * \retval -1 on error.
1527  */
1528 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
1529 {
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;
1546         char *c;
1547         char *l;
1548         char dest[256];
1549
1550         ast_copy_string(dest, rdest, sizeof(dest));
1551
1552         c = strchr(dest, '/');
1553         if (c) {
1554                 c++;
1555         } else {
1556                 c = "";
1557         }
1558         if (strlen(c) < p->stripmsd) {
1559                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1560                 return -1;
1561         }
1562
1563         if (!p->hidecallerid) {
1564                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1565         } else {
1566                 l = NULL;
1567         }
1568
1569         ss7_grab(p, p->ss7);
1570
1571         if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
1572                 /* Call collision before sending IAM.  Abort call. */
1573                 ss7_rel(p->ss7);
1574                 return -1;
1575         }
1576
1577         p->ss7call = isup_new_call(p->ss7->ss7);
1578         if (!p->ss7call) {
1579                 ss7_rel(p->ss7);
1580                 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
1581                 return -1;
1582         }
1583
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;
1593                 } else {
1594                         ss7_called_nai = SS7_NAI_SUBSCRIBER;
1595                 }
1596         }
1597         isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
1598
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;
1608                 } else {
1609                         ss7_calling_nai = SS7_NAI_SUBSCRIBER;
1610                 }
1611         }
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);
1615
1616         isup_set_oli(p->ss7call, ast_channel_connected(ast)->ani2);
1617         isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
1618
1619         /* Set the charge number if it is set */
1620         charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
1621         if (charge_str)
1622                 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
1623
1624         gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
1625         if (gen_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 */
1627
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");
1631         if (gen_digits)
1632                 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
1633
1634         gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
1635         if (gen_name)
1636                 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
1637
1638         jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
1639         if (jip_digits)
1640                 isup_set_jip_digits(p->ss7call, jip_digits);
1641
1642         lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
1643         if (lspi_ident)
1644                 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
1645
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 */
1649         }
1650
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);
1656         }
1657
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));
1661
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);
1666         ss7_rel(p->ss7);
1667         return 0;
1668 }
1669
1670 /*!
1671  * \brief SS7 hangup channel.
1672  * \since 1.8
1673  *
1674  * \param p Signaling private structure pointer.
1675  * \param ast Asterisk channel structure pointer.
1676  *
1677  * \retval 0 on success.
1678  * \retval -1 on error.
1679  */
1680 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
1681 {
1682         int res = 0;
1683
1684         if (!ast_channel_tech_pvt(ast)) {
1685                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1686                 return 0;
1687         }
1688
1689         p->owner = NULL;
1690         sig_ss7_set_dialing(p, 0);
1691         sig_ss7_set_outgoing(p, 0);
1692         p->progress = 0;
1693         p->rlt = 0;
1694         p->exten[0] = '\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;
1698         if (p->ss7call) {
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;
1702
1703                         if (cause) {
1704                                 if (atoi(cause)) {
1705                                         icause = atoi(cause);
1706                                 }
1707                         }
1708                         isup_rel(p->ss7->ss7, p->ss7call, icause);
1709                         p->alreadyhungup = 1;
1710                 }
1711         }
1712         ss7_rel(p->ss7);
1713
1714         return res;
1715 }
1716
1717 /*!
1718  * \brief SS7 answer channel.
1719  * \since 1.8
1720  *
1721  * \param p Signaling private structure pointer.
1722  * \param ast Asterisk channel structure pointer.
1723  *
1724  * \retval 0 on success.
1725  * \retval -1 on error.
1726  */
1727 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
1728 {
1729         int res;
1730
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;
1734         }
1735         sig_ss7_open_media(p);
1736         res = isup_anm(p->ss7->ss7, p->ss7call);
1737         ss7_rel(p->ss7);
1738         return res;
1739 }
1740
1741 /*!
1742  * \brief Fix up a channel:  If a channel is consumed, this is called.  Basically update any ->owner links.
1743  * \since 1.8
1744  *
1745  * \param oldchan Old channel pointer to replace.
1746  * \param newchan New channel pointer to set.
1747  * \param pchan Signaling private structure pointer.
1748  *
1749  * \return Nothing
1750  */
1751 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
1752 {
1753         if (pchan->owner == oldchan) {
1754                 pchan->owner = newchan;
1755         }
1756 }
1757
1758 /*!
1759  * \brief SS7 answer channel.
1760  * \since 1.8
1761  *
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.
1767  *
1768  * \retval 0 on success.
1769  * \retval -1 on error or indication condition not handled.
1770  */
1771 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
1772 {
1773         int res = -1;
1774
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);
1780                         res = 0;
1781                         break;
1782                 }
1783                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
1784                 break;
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) {
1790                                 p->rlt = 1;
1791                         }
1792
1793                         /* No need to send CPG if call will be RELEASE */
1794                         if (p->rlt != 1) {
1795                                 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
1796                         }
1797                 }
1798                 ss7_rel(p->ss7);
1799
1800                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
1801
1802                 if (ast_channel_state(chan) != AST_STATE_UP && ast_channel_state(chan) != AST_STATE_RING) {
1803                         ast_setstate(chan, AST_STATE_RINGING);
1804                 }
1805                 break;
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) {
1812                                 p->rlt = 1;
1813                         }
1814                 }
1815
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);
1819                 }
1820                 ss7_rel(p->ss7);
1821                 /* don't continue in ast_indicate */
1822                 res = 0;
1823                 break;
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);
1830                         ss7_rel(p->ss7);
1831
1832                         /* enable echo canceler here on SS7 calls */
1833                         sig_ss7_set_echocanceller(p, 1);
1834                 } else {
1835                         ss7_rel(p->ss7);
1836                 }
1837                 /* don't continue in ast_indicate */
1838                 res = 0;
1839                 break;
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);
1844                         res = 0;
1845                         break;
1846                 }
1847                 /* Wait for DTMF digits to complete the dialed number. */
1848                 res = 0;
1849                 break;
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);
1854                         res = 0;
1855                         break;
1856                 }
1857                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_CONGESTION);
1858                 break;
1859         case AST_CONTROL_HOLD:
1860                 ast_moh_start(chan, data, p->mohinterpret);
1861                 break;
1862         case AST_CONTROL_UNHOLD:
1863                 ast_moh_stop(chan);
1864                 break;
1865         case AST_CONTROL_SRCUPDATE:
1866                 res = 0;
1867                 break;
1868         case -1:
1869                 res = sig_ss7_play_tone(p, -1);
1870                 break;
1871         }
1872         return res;
1873 }
1874
1875 /*!
1876  * \brief SS7 channel request.
1877  * \since 1.8
1878  *
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
1883  *
1884  * \retval ast_channel on success.
1885  * \retval NULL on error.
1886  */
1887 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
1888 {
1889         struct ast_channel *ast;
1890
1891         /* Companding law is determined by SS7 signaling type. */
1892         if (p->ss7->type == SS7_ITU) {
1893                 law = SIG_SS7_ALAW;
1894         } else {
1895                 law = SIG_SS7_ULAW;
1896         }
1897
1898         sig_ss7_set_outgoing(p, 1);
1899         ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1900         if (!ast) {
1901                 sig_ss7_set_outgoing(p, 0);
1902
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);
1907         }
1908         return ast;
1909 }
1910
1911 /*!
1912  * \brief Delete the sig_ss7 private channel structure.
1913  * \since 1.8
1914  *
1915  * \param doomed sig_ss7 private channel structure to delete.
1916  *
1917  * \return Nothing
1918  */
1919 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
1920 {
1921         ast_free(doomed);
1922 }
1923
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)
1927 {
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");
1930 }
1931
1932 void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
1933 {
1934         char line[256];
1935         int idx;
1936         struct sig_ss7_chan *pvt;
1937
1938         ast_mutex_lock(&linkset->lock);
1939         for (idx = 0; idx < linkset->numchans; ++idx) {
1940                 if (!linkset->pvts[idx]) {
1941                         continue;
1942                 }
1943                 pvt = linkset->pvts[idx];
1944                 sig_ss7_lock_private(pvt);
1945                 sig_ss7_lock_owner(linkset, idx);
1946
1947                 snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
1948                         linkset->span,
1949                         pvt->channel,
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) : "");
1956
1957                 if (pvt->owner) {
1958                         ast_channel_unlock(pvt->owner);
1959                 }
1960                 sig_ss7_unlock_private(pvt);
1961
1962                 ast_mutex_unlock(&linkset->lock);
1963                 ast_cli(fd, "%s\n", line);
1964                 ast_mutex_lock(&linkset->lock);
1965         }
1966         ast_mutex_unlock(&linkset->lock);
1967 }
1968
1969 /*!
1970  * \brief Create a new sig_ss7 private channel structure.
1971  * \since 1.8
1972  *
1973  * \param pvt_data Upper layer private data structure.
1974  * \param ss7 Controlling linkset for the channel.
1975  *
1976  * \retval sig_ss7_chan on success.
1977  * \retval NULL on error.
1978  */
1979 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_linkset *ss7)
1980 {
1981         struct sig_ss7_chan *pvt;
1982
1983         pvt = ast_calloc(1, sizeof(*pvt));
1984         if (!pvt) {
1985                 return pvt;
1986         }
1987
1988         pvt->chan_pvt = pvt_data;
1989         pvt->ss7 = ss7;
1990
1991         return pvt;
1992 }
1993
1994 /*!
1995  * \brief Initialize the SS7 linkset control.
1996  * \since 1.8
1997  *
1998  * \param ss7 SS7 linkset control structure.
1999  *
2000  * \return Nothing
2001  */
2002 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
2003 {
2004         int idx;
2005
2006         memset(ss7, 0, sizeof(*ss7));
2007
2008         ast_mutex_init(&ss7->lock);
2009
2010         ss7->master = AST_PTHREADT_NULL;
2011         for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
2012                 ss7->fds[idx] = -1;
2013         }
2014 }
2015
2016 /* ------------------------------------------------------------------- */
2017
2018 #endif  /* defined(HAVE_SS7) */
2019 /* end sig_ss7.c */