core: Don't allow free to mean ast_free (and malloc, etc..).
[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/callerid.h"
45 #include "asterisk/transcap.h"
46 #include "asterisk/stasis_channels.h"
47
48 #include "sig_ss7.h"
49 #if !defined(LIBSS7_ABI_COMPATIBILITY)
50 #error "Upgrade your libss7"
51 #elif LIBSS7_ABI_COMPATIBILITY != 2
52 #error "Your installed libss7 is not compatible"
53 #endif
54
55 /* ------------------------------------------------------------------- */
56
57 static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
58 {
59         switch (level) {
60         case SIG_SS7_CALL_LEVEL_IDLE:
61                 return "Idle";
62         case SIG_SS7_CALL_LEVEL_ALLOCATED:
63                 return "Allocated";
64         case SIG_SS7_CALL_LEVEL_CONTINUITY:
65                 return "Continuity";
66         case SIG_SS7_CALL_LEVEL_SETUP:
67                 return "Setup";
68         case SIG_SS7_CALL_LEVEL_PROCEEDING:
69                 return "Proceeding";
70         case SIG_SS7_CALL_LEVEL_ALERTING:
71                 return "Alerting";
72         case SIG_SS7_CALL_LEVEL_CONNECT:
73                 return "Connect";
74         }
75         return "Unknown";
76 }
77
78 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
79 {
80         if (sig_ss7_callbacks.unlock_private) {
81                 sig_ss7_callbacks.unlock_private(p->chan_pvt);
82         }
83 }
84
85 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
86 {
87         if (sig_ss7_callbacks.lock_private) {
88                 sig_ss7_callbacks.lock_private(p->chan_pvt);
89         }
90 }
91
92 static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
93 {
94         if (sig_ss7_callbacks.deadlock_avoidance_private) {
95                 sig_ss7_callbacks.deadlock_avoidance_private(p->chan_pvt);
96         } else {
97                 /* Fallback to the old way if callback not present. */
98                 sig_ss7_unlock_private(p);
99                 sched_yield();
100                 sig_ss7_lock_private(p);
101         }
102 }
103
104 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
105 {
106         p->inalarm = in_alarm;
107         if (sig_ss7_callbacks.set_alarm) {
108                 sig_ss7_callbacks.set_alarm(p->chan_pvt, in_alarm);
109         }
110 }
111
112 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
113 {
114         if (sig_ss7_callbacks.set_dialing) {
115                 sig_ss7_callbacks.set_dialing(p->chan_pvt, is_dialing);
116         }
117 }
118
119 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
120 {
121         if (sig_ss7_callbacks.set_digital) {
122                 sig_ss7_callbacks.set_digital(p->chan_pvt, is_digital);
123         }
124 }
125
126 static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
127 {
128         p->outgoing = is_outgoing;
129         if (sig_ss7_callbacks.set_outgoing) {
130                 sig_ss7_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
131         }
132 }
133
134 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
135 {
136         p->inservice = is_inservice;
137         if (sig_ss7_callbacks.set_inservice) {
138                 sig_ss7_callbacks.set_inservice(p->chan_pvt, is_inservice);
139         }
140 }
141
142 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked, int type)
143 {
144         if (is_blocked) {
145                 p->locallyblocked |= type;
146         } else {
147                 p->locallyblocked &= ~type;
148         }
149
150         if (sig_ss7_callbacks.set_locallyblocked) {
151                 sig_ss7_callbacks.set_locallyblocked(p->chan_pvt, p->locallyblocked);
152         }
153 }
154
155 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked, int type)
156 {
157         if (is_blocked) {
158                 p->remotelyblocked |= type;
159         } else {
160                 p->remotelyblocked &= ~type;
161         }
162
163         if (sig_ss7_callbacks.set_remotelyblocked) {
164                 sig_ss7_callbacks.set_remotelyblocked(p->chan_pvt, p->remotelyblocked);
165         }
166 }
167
168 /*!
169  * \internal
170  * \brief Open the SS7 channel media path.
171  * \since 1.8.12
172  *
173  * \param p Channel private control structure.
174  *
175  * \return Nothing
176  */
177 static void sig_ss7_open_media(struct sig_ss7_chan *p)
178 {
179         if (sig_ss7_callbacks.open_media) {
180                 sig_ss7_callbacks.open_media(p->chan_pvt);
181         }
182 }
183
184 /*!
185  * \internal
186  * \brief Set the caller id information in the parent module.
187  * \since 1.8
188  *
189  * \param p sig_ss7 channel structure.
190  *
191  * \return Nothing
192  */
193 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
194 {
195         struct ast_party_caller caller;
196
197         if (sig_ss7_callbacks.set_callerid) {
198                 ast_party_caller_init(&caller);
199
200                 caller.id.name.str = p->cid_name;
201                 caller.id.name.presentation = p->callingpres;
202                 caller.id.name.valid = 1;
203
204                 caller.id.number.str = p->cid_num;
205                 caller.id.number.plan = p->cid_ton;
206                 caller.id.number.presentation = p->callingpres;
207                 caller.id.number.valid = 1;
208
209                 if (!ast_strlen_zero(p->cid_subaddr)) {
210                         caller.id.subaddress.valid = 1;
211                         //caller.id.subaddress.type = 0;/* nsap */
212                         //caller.id.subaddress.odd_even_indicator = 0;
213                         caller.id.subaddress.str = p->cid_subaddr;
214                 }
215
216                 caller.ani.number.str = p->cid_ani;
217                 //caller.ani.number.plan = p->xxx;
218                 //caller.ani.number.presentation = p->xxx;
219                 caller.ani.number.valid = 1;
220
221                 caller.ani2 = p->cid_ani2;
222                 sig_ss7_callbacks.set_callerid(p->chan_pvt, &caller);
223         }
224 }
225
226 /*!
227  * \internal
228  * \brief Set the Dialed Number Identifier.
229  * \since 1.8
230  *
231  * \param p sig_ss7 channel structure.
232  * \param dnid Dialed Number Identifier string.
233  *
234  * \return Nothing
235  */
236 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
237 {
238         if (sig_ss7_callbacks.set_dnid) {
239                 sig_ss7_callbacks.set_dnid(p->chan_pvt, dnid);
240         }
241 }
242
243 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
244 {
245         int res;
246
247         if (sig_ss7_callbacks.play_tone) {
248                 res = sig_ss7_callbacks.play_tone(p->chan_pvt, tone);
249         } else {
250                 res = -1;
251         }
252         return res;
253 }
254
255 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
256 {
257         if (sig_ss7_callbacks.set_echocanceller) {
258                 return sig_ss7_callbacks.set_echocanceller(p->chan_pvt, enable);
259         }
260         return -1;
261 }
262
263 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
264 {
265         if (p->loopedback != enable) {
266                 p->loopedback = enable;
267                 if (sig_ss7_callbacks.set_loopback) {
268                         sig_ss7_callbacks.set_loopback(p->chan_pvt, enable);
269                 }
270         }
271 }
272
273 static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state,
274         int ulaw, int transfercapability, char *exten,
275         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
276 {
277         struct ast_channel *ast;
278
279         if (sig_ss7_callbacks.new_ast_channel) {
280                 ast = sig_ss7_callbacks.new_ast_channel(p->chan_pvt, state, ulaw, exten,
281                         assignedids, requestor);
282         } else {
283                 return NULL;
284         }
285         if (!ast) {
286                 return NULL;
287         }
288
289         if (!p->owner) {
290                 p->owner = ast;
291         }
292
293         if (p->outgoing) {
294                 p->do_hangup = SS7_HANGUP_FREE_CALL;
295         } else {
296                 p->do_hangup = SS7_HANGUP_SEND_REL;
297         }
298
299         ast_channel_transfercapability_set(ast, transfercapability);
300         pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
301                 ast_transfercapability2str(transfercapability));
302         if (transfercapability & AST_TRANS_CAP_DIGITAL) {
303                 sig_ss7_set_digital(p, 1);
304         }
305
306         return ast;
307 }
308
309 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
310 {
311         if (sig_ss7_callbacks.handle_link_exception) {
312                 sig_ss7_callbacks.handle_link_exception(linkset, which);
313         }
314 }
315
316 static struct sig_ss7_linkset *sig_ss7_find_linkset(struct ss7 *ss7)
317 {
318         if (sig_ss7_callbacks.find_linkset) {
319                 return sig_ss7_callbacks.find_linkset(ss7);
320         }
321         return NULL;
322 }
323
324 /*!
325  * \internal
326  * \brief Determine if a private channel structure is available.
327  *
328  * \param pvt Channel to determine if available.
329  *
330  * \return TRUE if the channel is available.
331  */
332 static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
333 {
334         if (pvt->inservice && !pvt->inalarm && !pvt->owner && !pvt->ss7call
335                 && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
336                 && !pvt->locallyblocked && !pvt->remotelyblocked) {
337                 return 1;
338         }
339         return 0;
340 }
341
342 /*!
343  * \internal
344  * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
345  * \since 1.8
346  *
347  * \param ss7 SS7 linkset control structure.
348  * \param chanpos Channel position in the span.
349  *
350  * \note Assumes the ss7->lock is already obtained.
351  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
352  *
353  * \return Nothing
354  */
355 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
356 {
357         for (;;) {
358                 if (!ss7->pvts[chanpos]->owner) {
359                         /* There is no owner lock to get. */
360                         break;
361                 }
362                 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
363                         /* We got the lock */
364                         break;
365                 }
366
367                 /* Avoid deadlock */
368                 sig_ss7_unlock_private(ss7->pvts[chanpos]);
369                 DEADLOCK_AVOIDANCE(&ss7->lock);
370                 sig_ss7_lock_private(ss7->pvts[chanpos]);
371         }
372 }
373
374 /*!
375  * \internal
376  * \brief Queue the given frame onto the owner channel.
377  * \since 1.8
378  *
379  * \param ss7 SS7 linkset control structure.
380  * \param chanpos Channel position in the span.
381  * \param frame Frame to queue onto the owner channel.
382  *
383  * \note Assumes the ss7->lock is already obtained.
384  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
385  *
386  * \return Nothing
387  */
388 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
389 {
390         sig_ss7_lock_owner(ss7, chanpos);
391         if (ss7->pvts[chanpos]->owner) {
392                 ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
393                 ast_channel_unlock(ss7->pvts[chanpos]->owner);
394         }
395 }
396
397 /*!
398  * \internal
399  * \brief Queue a control frame of the specified subclass onto the owner channel.
400  * \since 1.8
401  *
402  * \param ss7 SS7 linkset control structure.
403  * \param chanpos Channel position in the span.
404  * \param subclass Control frame subclass to queue onto the owner channel.
405  *
406  * \note Assumes the ss7->lock is already obtained.
407  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
408  *
409  * \return Nothing
410  */
411 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
412 {
413         struct ast_frame f = {AST_FRAME_CONTROL, };
414         struct sig_ss7_chan *p = ss7->pvts[chanpos];
415
416         if (sig_ss7_callbacks.queue_control) {
417                 sig_ss7_callbacks.queue_control(p->chan_pvt, subclass);
418         }
419
420         f.subclass.integer = subclass;
421         sig_ss7_queue_frame(ss7, chanpos, &f);
422 }
423
424 /*!
425  * \internal
426  * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
427  * \since 11.0
428  *
429  * \param owner Owner channel of the pvt.
430  * \param cause String describing the cause to be placed into the frame.
431  *
432  * \note Assumes the linkset->lock is already obtained.
433  * \note Assumes the sig_ss7_lock_private(linkset->pvts[chanpos]) is already obtained.
434  * \note Assumes linkset->pvts[chanpos]->owner is non-NULL and its lock is already obtained.
435  *
436  * \return Nothing
437  */
438 static void ss7_queue_pvt_cause_data(struct ast_channel *owner, const char *cause, int ast_cause)
439 {
440         struct ast_control_pvt_cause_code *cause_code;
441         int datalen = sizeof(*cause_code) + strlen(cause);
442
443         cause_code = ast_alloca(datalen);
444         memset(cause_code, 0, datalen);
445         cause_code->ast_cause = ast_cause;
446         ast_copy_string(cause_code->chan_name, ast_channel_name(owner), AST_CHANNEL_NAME);
447         ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
448         ast_queue_control_data(owner, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
449         ast_channel_hangupcause_hash_set(owner, cause_code, datalen);
450 }
451
452
453 /*!
454  * \brief Find the channel position by CIC/DPC.
455  *
456  * \param linkset SS7 linkset control structure.
457  * \param cic Circuit Identification Code
458  * \param dpc Destination Point Code
459  *
460  * \retval chanpos on success.
461  * \retval -1 on error.
462  */
463 int sig_ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
464 {
465         int i;
466         int winner = -1;
467         for (i = 0; i < linkset->numchans; i++) {
468                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
469                         winner = i;
470                         break;
471                 }
472         }
473         return winner;
474 }
475
476 /*!
477  * \internal
478  * \brief Find the channel position by CIC/DPC and gripe if not found.
479  *
480  * \param linkset SS7 linkset control structure.
481  * \param cic Circuit Identification Code
482  * \param dpc Destination Point Code
483  * \param msg_name Message type name that failed.
484  *
485  * \retval chanpos on success.
486  * \retval -1 on error.
487  */
488 static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
489 {
490         int chanpos;
491
492         chanpos = sig_ss7_find_cic(linkset, cic, dpc);
493         if (chanpos < 0) {
494                 ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested on unconfigured CIC/DPC %d/%d.\n",
495                         linkset->span, msg_name, cic, dpc);
496                 return -1;
497         }
498         return chanpos;
499 }
500
501 static struct sig_ss7_chan *ss7_find_pvt(struct ss7 *ss7, int cic, unsigned int dpc)
502 {
503         int chanpos;
504         struct sig_ss7_linkset *winner;
505
506         winner = sig_ss7_find_linkset(ss7);
507         if (winner && (chanpos = sig_ss7_find_cic(winner, cic, dpc)) > -1) {
508                 return winner->pvts[chanpos];
509         }
510         return NULL;
511 }
512
513 int sig_ss7_cb_hangup(struct ss7 *ss7, int cic, unsigned int dpc, int cause, int do_hangup)
514 {
515         struct sig_ss7_chan *p;
516         int res;
517
518         if (!(p = ss7_find_pvt(ss7, cic, dpc))) {
519                 return SS7_CIC_NOT_EXISTS;
520         }
521
522         sig_ss7_lock_private(p);
523         if (p->owner) {
524                 ast_channel_hangupcause_set(p->owner, cause);
525                 ast_channel_softhangup_internal_flag_add(p->owner, AST_SOFTHANGUP_DEV);
526                 p->do_hangup = do_hangup;
527                 res = SS7_CIC_USED;
528         } else {
529                 res = SS7_CIC_IDLE;
530         }
531         sig_ss7_unlock_private(p);
532
533         return res;
534 }
535
536 void sig_ss7_cb_call_null(struct ss7 *ss7, struct isup_call *call, int lock)
537 {
538         int i;
539         struct sig_ss7_linkset *winner;
540
541         winner = sig_ss7_find_linkset(ss7);
542         if (!winner) {
543                 return;
544         }
545         for (i = 0; i < winner->numchans; i++) {
546                 if (winner->pvts[i] && (winner->pvts[i]->ss7call == call)) {
547                         if (lock) {
548                                 sig_ss7_lock_private(winner->pvts[i]);
549                         }
550                         winner->pvts[i]->ss7call = NULL;
551                         if (winner->pvts[i]->owner) {
552                                 ast_channel_hangupcause_set(winner->pvts[i]->owner, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
553                                 ast_channel_softhangup_internal_flag_add(winner->pvts[i]->owner, AST_SOFTHANGUP_DEV);
554                         }
555                         if (lock) {
556                                 sig_ss7_unlock_private(winner->pvts[i]);
557                         }
558                         ast_log(LOG_WARNING, "libss7 asked set ss7 call to NULL on CIC %d DPC %d\n", winner->pvts[i]->cic, winner->pvts[i]->dpc);
559                 }
560         }
561 }
562
563 void sig_ss7_cb_notinservice(struct ss7 *ss7, int cic, unsigned int dpc)
564 {
565         struct sig_ss7_chan *p;
566
567         if (!(p = ss7_find_pvt(ss7, cic, dpc))) {
568                 return;
569         }
570
571         sig_ss7_lock_private(p);
572         sig_ss7_set_inservice(p, 0);
573         sig_ss7_unlock_private(p);
574 }
575
576 /*!
577  * \internal
578  * \brief Check if CICs in a range belong to the linkset for a given DPC.
579  * \since 11.0
580  *
581  * \param linkset SS7 linkset control structure.
582  * \param startcic Circuit Identification Code to start from
583  * \param endcic Circuit Identification Code to search up-to
584  * \param dpc Destination Point Code
585  * \param state Array containing the status of the search
586  *
587  * \retval Nothing.
588  */
589 static void ss7_check_range(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char *state)
590 {
591         int cic;
592
593         for (cic = startcic; cic <= endcic; cic++) {
594                 if (state[cic - startcic] && sig_ss7_find_cic(linkset, cic, dpc) == -1) {
595                         state[cic - startcic] = 0;
596                 }
597         }
598 }
599
600 static int ss7_match_range(struct sig_ss7_chan *pvt, int startcic, int endcic, unsigned int dpc)
601 {
602         if (pvt && pvt->dpc == dpc && pvt->cic >= startcic && pvt->cic <= endcic) {
603                 return 1;
604         }
605
606         return 0;
607 }
608
609 /*!
610  * \internal
611  * \brief Check if a range is defined for the given DPC.
612  * \since 11.0
613  *
614  * \param linkset SS7 linkset control structure.
615  * \param startcic Start CIC of the range to clear.
616  * \param endcic End CIC of the range to clear.
617  * \param dpc Destination Point Code.
618  *
619  * \note Assumes the linkset->lock is already obtained.
620  *
621  * \return TRUE if all CICs in the range are present
622  */
623 int sig_ss7_find_cic_range(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
624 {
625         int i, found = 0;
626
627         for (i = 0; i < linkset->numchans; i++) {
628                 if (ss7_match_range(linkset->pvts[i], startcic, endcic, dpc)) {
629                         found++;
630                 }
631         }
632
633         if (found == endcic - startcic + 1) {
634                 return  1;
635         }
636
637         return 0;
638 }
639
640 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, ss7_event *e)
641 {
642         unsigned char status[32];
643         struct sig_ss7_chan *p = NULL;
644         int i;
645         int offset;
646         int chanpos;
647
648         memset(status, 0, sizeof(status));
649         for (i = 0; i < linkset->numchans; i++) {
650                 if (ss7_match_range(linkset->pvts[i], e->cqm.startcic, e->cqm.endcic, e->cqm.opc)) {
651                         p = linkset->pvts[i];
652                         sig_ss7_lock_private(p);
653                         offset = p->cic - e->cqm.startcic;
654                         status[offset] = 0;
655                         if (p->locallyblocked) {
656                                 status[offset] |= (1 << 0) | (1 << 4);
657                         }
658                         if (p->remotelyblocked) {
659                                 status[offset] |= (1 << 1) | (1 << 5);
660                         }
661                         if (p->ss7call) {
662                                 if (p->outgoing) {
663                                         status[offset] |= (1 << 3);
664                                 } else {
665                                         status[offset] |= (1 << 2);
666                                 }
667                         } else {
668                                 status[offset] |= 0x3 << 2;
669                         }
670                         sig_ss7_unlock_private(p);
671                 }
672         }
673
674         if (p) {
675                 isup_cqr(linkset->ss7, e->cqm.startcic, e->cqm.endcic, e->cqm.opc, status);
676         } else {
677                 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
678         }
679
680         chanpos = sig_ss7_find_cic(linkset, e->cqm.startcic, e->cqm.opc);
681         if (chanpos < 0) {
682                 isup_free_call(linkset->ss7, e->cqm.call);
683                 return;
684         }
685         p = linkset->pvts[chanpos];
686         sig_ss7_lock_private(p);
687         p->ss7call = e->cqm.call;
688         if (!p->owner) {
689                 p->ss7call = isup_free_call_if_clear(linkset->ss7, e->cqm.call);
690         }
691         sig_ss7_unlock_private(p);
692 }
693
694 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
695 {
696         int i;
697
698         for (i = 0; i < linkset->numchans; i++) {
699                 if (ss7_match_range(linkset->pvts[i], startcic, endcic, dpc)) {
700                         sig_ss7_lock_private(linkset->pvts[i]);
701                         sig_ss7_lock_owner(linkset, i);
702                         if (linkset->pvts[i]->owner) {
703                                 ast_softhangup_nolock(linkset->pvts[i]->owner, AST_SOFTHANGUP_DEV);
704                                 ast_channel_unlock(linkset->pvts[i]->owner);
705                         }
706                         sig_ss7_unlock_private(linkset->pvts[i]);
707                 }
708         }
709 }
710
711 /*!
712  * \param linkset SS7 linkset control structure.
713  * \param startcic Start CIC of the range to clear.
714  * \param endcic End CIC of the range to clear.
715  * \param dpc Destination Point Code.
716  * \param state Affected CICs from the operation. NULL for all CICs in the range.
717  * \param block Operation to perform. TRUE to block.
718  * \param remotely Direction of the blocking. TRUE to block/unblock remotely.
719  * \param type Blocking type - hardware or maintenance.
720  *
721  * \note Assumes the linkset->lock is already obtained.
722  * \note Must be called without sig_ss7_lock_private() obtained.
723  *
724  * \return Nothing.
725  */
726 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block, int remotely, int type)
727 {
728         int i;
729
730         for (i = 0; i < linkset->numchans; i++) {
731                 if (ss7_match_range(linkset->pvts[i], startcic, endcic, dpc)) {
732                         sig_ss7_lock_private(linkset->pvts[i]);
733                         if (state) {
734                                 if (state[linkset->pvts[i]->cic - startcic]) {
735
736                                         if (remotely) {
737                                                 sig_ss7_set_remotelyblocked(linkset->pvts[i], block, type);
738                                         } else {
739                                                 sig_ss7_set_locallyblocked(linkset->pvts[i], block, type);
740                                         }
741
742                                         sig_ss7_lock_owner(linkset, i);
743                                         if (linkset->pvts[i]->owner) {
744                                                 if (ast_channel_state(linkset->pvts[i]->owner) == AST_STATE_DIALING
745                                                         && linkset->pvts[i]->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
746                                                         ast_channel_hangupcause_set(linkset->pvts[i]->owner, SS7_CAUSE_TRY_AGAIN);
747                                                 }
748                                                 ast_channel_unlock(linkset->pvts[i]->owner);
749                                         }
750                                 }
751                         } else {
752                                 if (remotely) {
753                                         sig_ss7_set_remotelyblocked(linkset->pvts[i], block, type);
754                                 } else {
755                                         sig_ss7_set_locallyblocked(linkset->pvts[i], block, type);
756                                 }
757                         }
758                         sig_ss7_unlock_private(linkset->pvts[i]);
759                 }
760         }
761 }
762
763 /*!
764  * \param linkset SS7 linkset control structure.
765  * \param startcic Start CIC of the range to set in service.
766  * \param endcic End CIC of the range to set in service.
767  * \param dpc Destination Point Code.
768  *
769  * \note Must be called without sig_ss7_lock_private() obtained.
770  *
771  * \return Nothing.
772  */
773 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
774 {
775         int i;
776
777         for (i = 0; i < linkset->numchans; i++) {
778                 if (ss7_match_range(linkset->pvts[i], startcic, endcic, dpc)) {
779                         sig_ss7_lock_private(linkset->pvts[i]);
780                         sig_ss7_set_inservice(linkset->pvts[i], 1);
781                         sig_ss7_unlock_private(linkset->pvts[i]);
782                 }
783         }
784 }
785
786 static int ss7_find_alloc_call(struct sig_ss7_chan *p)
787 {
788         if (!p) {
789                 return 0;
790         }
791
792         if (!p->ss7call) {
793                 p->ss7call = isup_new_call(p->ss7->ss7, p->cic, p->dpc, 0);
794                 if (!p->ss7call) {
795                         return 0;
796                 }
797         }
798         return 1;
799 }
800
801 /*
802  * XXX This routine is not tolerant of holes in the pvts[] array.
803  * XXX This routine assumes the cic's in the pvts[] array are sorted.
804  *
805  * Probably the easiest way to deal with the invalid assumptions
806  * is to have a local pvts[] array and sort it by dpc and cic.
807  * Then the existing algorithm could work.
808  */
809 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
810 {
811         int i, startcic, endcic, dpc;
812         struct sig_ss7_chan *p;
813
814         if (linkset->numchans <= 0) {
815                 return;
816         }
817
818         startcic = linkset->pvts[0]->cic;
819         p = linkset->pvts[0];
820         /* DB: CIC's DPC fix */
821         dpc = linkset->pvts[0]->dpc;
822
823         for (i = 0; i < linkset->numchans; i++) {
824                 if (linkset->pvts[i+1]
825                         && linkset->pvts[i+1]->dpc == dpc
826                         && linkset->pvts[i+1]->cic - linkset->pvts[i]->cic == 1
827                         && linkset->pvts[i]->cic - startcic < (linkset->type == SS7_ANSI ? 24 : 31)) {
828                         continue;
829                 } else {
830                         endcic = linkset->pvts[i]->cic;
831                         ast_verb(1, "Resetting CICs %d to %d\n", startcic, endcic);
832
833                         sig_ss7_lock_private(p);
834                         if (!ss7_find_alloc_call(p)) {
835                                 ast_log(LOG_ERROR, "Unable to allocate new ss7call\n");
836                         } else if (!(endcic - startcic)) {      /* GRS range can not be 0 - use RSC instead */
837                                 isup_rsc(linkset->ss7, p->ss7call);
838                         } else {
839                                 isup_grs(linkset->ss7, p->ss7call, endcic);
840                         }
841                         sig_ss7_unlock_private(p);
842
843                         /* DB: CIC's DPC fix */
844                         if (linkset->pvts[i+1]) {
845                                 startcic = linkset->pvts[i+1]->cic;
846                                 dpc = linkset->pvts[i+1]->dpc;
847                                 p = linkset->pvts[i+1];
848                         }
849                 }
850         }
851 }
852
853 /*!
854  * \internal
855  * \brief Complete the RSC procedure started earlier
856  * \since 11.0
857  *
858  * \param p Signaling private structure pointer.
859  *
860  * \note Assumes the ss7->lock is already obtained.
861  * \note Assumes sig_ss7_lock_private(p) is already obtained.
862  *
863  * \return Nothing.
864  */
865 static void ss7_do_rsc(struct sig_ss7_chan *p)
866 {
867         if (!p || !p->ss7call) {
868                 return;
869         }
870
871         isup_rsc(p->ss7->ss7, p->ss7call);
872
873         if (p->locallyblocked & SS7_BLOCKED_MAINTENANCE) {
874                 isup_blo(p->ss7->ss7, p->ss7call);
875         } else {
876                 sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
877         }
878 }
879
880 /*!
881  * \internal
882  * \brief Start RSC procedure on a specific link
883  * \since 11.0
884  *
885  * \param ss7 SS7 linkset control structure.
886  * \param which Channel position in the span.
887  *
888  * \note Assumes the ss7->lock is already obtained.
889  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
890  *
891  * \return TRUE on success
892  */
893 static int ss7_start_rsc(struct sig_ss7_linkset *linkset, int which)
894 {
895         if (!linkset->pvts[which]) {
896                 return 0;
897         }
898
899         if (!ss7_find_alloc_call(linkset->pvts[which])) {
900                 return 0;
901         }
902
903         sig_ss7_set_remotelyblocked(linkset->pvts[which], 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
904         sig_ss7_set_inservice(linkset->pvts[which], 0);
905         sig_ss7_loopback(linkset->pvts[which], 0);
906
907         sig_ss7_lock_owner(linkset, which);
908         if (linkset->pvts[which]->owner) {
909                 ast_channel_hangupcause_set(linkset->pvts[which]->owner, AST_CAUSE_NORMAL_CLEARING);
910                 ast_softhangup_nolock(linkset->pvts[which]->owner, AST_SOFTHANGUP_DEV);
911                 ast_channel_unlock(linkset->pvts[which]->owner);
912                 linkset->pvts[which]->do_hangup = SS7_HANGUP_SEND_RSC;
913         } else {
914                 ss7_do_rsc(linkset->pvts[which]);
915         }
916
917         return 1;
918 }
919
920 /*!
921  * \internal
922  * \brief Determine if a private channel structure is available.
923  * \since 11.0
924  *
925  * \param linkset SS7 linkset control structure.
926  * \param startcic Start CIC of the range to clear.
927  * \param endcic End CIC of the range to clear.
928  * \param dpc Destination Point Code.
929  * \param do_hangup What we have to do to clear the call.
930  *
931  * \note Assumes the linkset->lock is already obtained.
932  * \note Must be called without sig_ss7_lock_private() obtained.
933  *
934  * \return Nothing.
935  */
936 static void ss7_clear_channels(struct sig_ss7_linkset *linkset, int startcic, int endcic, int dpc, int do_hangup)
937 {
938         int i;
939
940         for (i = 0; i < linkset->numchans; i++) {
941                 if (ss7_match_range(linkset->pvts[i], startcic, endcic, dpc)) {
942                         sig_ss7_lock_private(linkset->pvts[i]);
943                         sig_ss7_set_inservice(linkset->pvts[i], 0);
944                         sig_ss7_lock_owner(linkset, i);
945                         if (linkset->pvts[i]->owner) {
946                                 ast_channel_hangupcause_set(linkset->pvts[i]->owner,
947                                                                                         AST_CAUSE_NORMAL_CLEARING);
948                                 ast_softhangup_nolock(linkset->pvts[i]->owner, AST_SOFTHANGUP_DEV);
949                                 ast_channel_unlock(linkset->pvts[i]->owner);
950                                 linkset->pvts[i]->do_hangup = (linkset->pvts[i]->cic != startcic) ?
951                                                                                         do_hangup : SS7_HANGUP_DO_NOTHING;
952                         } else if (linkset->pvts[i] && linkset->pvts[i]->cic != startcic) {
953                                 isup_free_call(linkset->pvts[i]->ss7->ss7, linkset->pvts[i]->ss7call);
954                                 linkset->pvts[i]->ss7call = NULL;
955                         }
956                         sig_ss7_unlock_private(linkset->pvts[i]);
957                 }
958         }
959 }
960
961 /*!
962  * \internal
963  *
964  * \param p Signaling private structure pointer.
965  * \param linkset SS7 linkset control structure.
966  *
967  * \note Assumes the linkset->lock is already obtained.
968  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
969  *
970  * \return Nothing.
971  */
972 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
973 {
974         struct ss7 *ss7 = linkset->ss7;
975         int law;
976         struct ast_channel *c;
977         char tmp[256];
978         char *strp;
979         struct ast_callid *callid = NULL;
980         int callid_created = ast_callid_threadstorage_auto(&callid);
981
982         if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
983                 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
984                 isup_acm(ss7, p->ss7call);
985         } else {
986                 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
987         }
988
989         /* Companding law is determined by SS7 signaling type. */
990         if (linkset->type == SS7_ITU) {
991                 law = SIG_SS7_ALAW;
992         } else {
993                 law = SIG_SS7_ULAW;
994         }
995
996         isup_set_echocontrol(p->ss7call, (linkset->flags & LINKSET_FLAG_DEFAULTECHOCONTROL) ? 1 : 0);
997
998         /*
999          * Release the SS7 lock while we create the channel so other
1000          * threads can send messages.  We must also release the private
1001          * lock to prevent deadlock while creating the channel.
1002          */
1003         ast_mutex_unlock(&linkset->lock);
1004         sig_ss7_unlock_private(p);
1005         c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL, NULL);
1006         if (!c) {
1007                 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
1008                 ast_mutex_lock(&linkset->lock);
1009                 sig_ss7_lock_private(p);
1010                 isup_rel(linkset->ss7, p->ss7call, AST_CAUSE_SWITCH_CONGESTION);
1011                 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1012                 ast_callid_threadstorage_auto_clean(callid, callid_created);
1013                 return;
1014         }
1015
1016         /* Hold the channel and private lock while we setup the channel. */
1017         ast_channel_lock(c);
1018         sig_ss7_lock_private(p);
1019
1020         ast_channel_stage_snapshot(c);
1021
1022         /*
1023          * It is reasonably safe to set the following
1024          * channel variables while the channel private
1025          * structure is locked.  The PBX has not been
1026          * started yet and it is unlikely that any other task
1027          * will do anything with the channel we have just
1028          * created.
1029          *
1030          * We only reference these variables in the context of the ss7_linkset function
1031          * when receiving either and IAM or a COT message.
1032          */
1033         if (!ast_strlen_zero(p->charge_number)) {
1034                 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
1035                 /* Clear this after we set it */
1036                 p->charge_number[0] = 0;
1037         }
1038         if (!ast_strlen_zero(p->gen_add_number)) {
1039                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
1040                 /* Clear this after we set it */
1041                 p->gen_add_number[0] = 0;
1042         }
1043         if (!ast_strlen_zero(p->jip_number)) {
1044                 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
1045                 /* Clear this after we set it */
1046                 p->jip_number[0] = 0;
1047         }
1048         if (!ast_strlen_zero(p->gen_dig_number)) {
1049                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
1050                 /* Clear this after we set it */
1051                 p->gen_dig_number[0] = 0;
1052         }
1053
1054         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
1055         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
1056         /* Clear this after we set it */
1057         p->gen_dig_type = 0;
1058
1059         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
1060         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
1061         /* Clear this after we set it */
1062         p->gen_dig_scheme = 0;
1063
1064         if (!ast_strlen_zero(p->lspi_ident)) {
1065                 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
1066                 /* Clear this after we set it */
1067                 p->lspi_ident[0] = 0;
1068         }
1069
1070         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
1071         pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
1072         /* Clear this after we set it */
1073         p->call_ref_ident = 0;
1074
1075         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
1076         pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
1077         /* Clear this after we set it */
1078         p->call_ref_pc = 0;
1079
1080         snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
1081         pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
1082         /* Clear this after we set it */
1083         p->calling_party_cat = 0;
1084
1085         if (p->redirect_counter) {
1086                 struct ast_party_redirecting redirecting;
1087
1088                 switch (p->redirect_info_ind) {
1089                 case 0:
1090                         strp = "NO_REDIRECTION";
1091                         break;
1092                 case 1:
1093                         strp = "CALL_REROUTED_PRES_ALLOWED";
1094                         break;
1095                 case 2:
1096                         strp = "CALL_REROUTED_INFO_RESTRICTED";
1097                         break;
1098                 case 3:
1099                         strp = "CALL_DIVERTED_PRES_ALLOWED";
1100                         break;
1101                 case 4:
1102                         strp = "CALL_DIVERTED_INFO_RESTRICTED";
1103                         break;
1104                 case 5:
1105                         strp = "CALL_REROUTED_PRES_RESTRICTED";
1106                         break;
1107                 case 6:
1108                         strp = "CALL_DIVERTED_PRES_RESTRICTED";
1109                         break;
1110                 case 7:
1111                         strp = "SPARE";
1112                         break;
1113                 default:
1114                         strp = "NO_REDIRECTION";
1115                         break;
1116                 }
1117                 pbx_builtin_setvar_helper(c, "SS7_REDIRECT_INFO_IND", strp);
1118                 /* Clear this after we set it */
1119                 p->redirect_info_ind = 0;
1120
1121                 ast_party_redirecting_init(&redirecting);
1122
1123                 if (p->redirect_info_counter) {
1124                         redirecting.count = p->redirect_info_counter;
1125                         if (p->redirect_info_counter != p->redirect_counter) {
1126                                 if (p->redirect_info_counter < p->redirect_counter) {
1127                                         redirecting.count = p->redirect_counter;
1128                                 }
1129                                 ast_log(LOG_WARNING, "Redirect counters differ: %u while info says %u - using %u\n",
1130                                         p->redirect_counter, p->redirect_info_counter, redirecting.count);
1131                         }
1132                         /* Clear this after we set it */
1133                         p->redirect_info_counter = 0;
1134                         p->redirect_counter = 0;
1135                 }
1136
1137                 if (p->redirect_counter) {
1138                         redirecting.count = p->redirect_counter;
1139                         /* Clear this after we set it */
1140                         p->redirect_counter = 0;
1141                 }
1142
1143                 switch (p->redirect_info_orig_reas) {
1144                 case SS7_REDIRECTING_REASON_UNKNOWN:
1145                         redirecting.orig_reason.code = AST_REDIRECTING_REASON_UNKNOWN;
1146                         break;
1147                 case SS7_REDIRECTING_REASON_USER_BUSY:
1148                         redirecting.orig_reason.code = AST_REDIRECTING_REASON_USER_BUSY;
1149                         break;
1150                 case SS7_REDIRECTING_REASON_NO_ANSWER:
1151                         redirecting.orig_reason.code = AST_REDIRECTING_REASON_NO_ANSWER;
1152                         break;
1153                 case SS7_REDIRECTING_REASON_UNCONDITIONAL:
1154                         redirecting.orig_reason.code = AST_REDIRECTING_REASON_UNCONDITIONAL;
1155                         break;
1156                 default:
1157                         redirecting.orig_reason.code = AST_REDIRECTING_REASON_UNKNOWN;
1158                         break;
1159                 }
1160
1161                 switch (p->redirect_info_reas) {
1162                 case SS7_REDIRECTING_REASON_UNKNOWN:
1163                         redirecting.reason.code = AST_REDIRECTING_REASON_UNKNOWN;
1164                         break;
1165                 case SS7_REDIRECTING_REASON_USER_BUSY:
1166                         redirecting.reason.code = AST_REDIRECTING_REASON_USER_BUSY;
1167                         if (!p->redirect_info_orig_reas && redirecting.count == 1) {
1168                                 redirecting.orig_reason.code = AST_REDIRECTING_REASON_USER_BUSY;
1169                         }
1170                         break;
1171                 case SS7_REDIRECTING_REASON_NO_ANSWER:
1172                         redirecting.reason.code = AST_REDIRECTING_REASON_NO_ANSWER;
1173                         if (!p->redirect_info_orig_reas && redirecting.count == 1) {
1174                                 redirecting.orig_reason.code = AST_REDIRECTING_REASON_NO_ANSWER;
1175                         }
1176                         break;
1177                 case SS7_REDIRECTING_REASON_UNCONDITIONAL:
1178                         redirecting.reason.code = AST_REDIRECTING_REASON_UNCONDITIONAL;
1179                         if (!p->redirect_info_orig_reas && redirecting.count == 1) {
1180                                 redirecting.orig_reason.code = AST_REDIRECTING_REASON_UNCONDITIONAL;
1181                         }
1182                         break;
1183                 case SS7_REDIRECTING_REASON_DEFLECTION_DURING_ALERTING:
1184                 case SS7_REDIRECTING_REASON_DEFLECTION_IMMEDIATE_RESPONSE:
1185                         redirecting.reason.code = AST_REDIRECTING_REASON_DEFLECTION;
1186                         break;
1187                 case SS7_REDIRECTING_REASON_UNAVAILABLE:
1188                         redirecting.reason.code = AST_REDIRECTING_REASON_UNAVAILABLE;
1189                         break;
1190                 default:
1191                         redirecting.reason.code = AST_REDIRECTING_REASON_UNKNOWN;
1192                         break;
1193                 }
1194                 /* Clear this after we set it */
1195                 p->redirect_info_orig_reas = 0;
1196                 p->redirect_info_reas = 0;
1197
1198                 if (!ast_strlen_zero(p->redirecting_num)) {
1199                         redirecting.from.number.str = ast_strdup(p->redirecting_num);
1200                         redirecting.from.number.presentation = p->redirecting_presentation;
1201                         redirecting.from.number.valid = 1;
1202                         /* Clear this after we set it */
1203                         p->redirecting_num[0] = 0;
1204                 }
1205
1206                 if (!ast_strlen_zero(p->generic_name)) {
1207                         redirecting.from.name.str = ast_strdup(p->generic_name);
1208                         redirecting.from.name.presentation = p->redirecting_presentation;
1209                         redirecting.from.name.valid = 1;
1210                         /* Clear this after we set it */
1211                         p->generic_name[0] = 0;
1212                 }
1213
1214                 if (!ast_strlen_zero(p->orig_called_num)) {
1215                         redirecting.orig.number.str = ast_strdup(p->orig_called_num);
1216                         redirecting.orig.number.presentation = p->orig_called_presentation;
1217                         redirecting.orig.number.valid = 1;
1218                         /* Clear this after we set it */
1219                         p->orig_called_num[0] = 0;
1220                 } else if (redirecting.count == 1) {
1221                         ast_party_id_copy(&redirecting.orig, &redirecting.from);
1222                 }
1223
1224                 ast_channel_update_redirecting(c, &redirecting, NULL);
1225                 ast_party_redirecting_free(&redirecting);
1226         }
1227
1228         if (p->cug_indicator != ISUP_CUG_NON) {
1229                 sprintf(tmp, "%d", p->cug_interlock_code);
1230                 pbx_builtin_setvar_helper(c, "SS7_CUG_INTERLOCK_CODE", tmp);
1231
1232                 switch (p->cug_indicator) {
1233                 case ISUP_CUG_NON:
1234                         strp = "NON_CUG";
1235                         break;
1236                 case ISUP_CUG_OUTGOING_ALLOWED:
1237                         strp = "OUTGOING_ALLOWED";
1238                         break;
1239                 case ISUP_CUG_OUTGOING_NOT_ALLOWED:
1240                         strp = "OUTGOING_NOT_ALLOWED";
1241                         break;
1242                 default:
1243                         strp = "SPARE";
1244                         break;
1245                 }
1246                 pbx_builtin_setvar_helper(c, "SS7_CUG_INDICATOR", strp);
1247
1248                 if (!ast_strlen_zero(p->cug_interlock_ni)) {
1249                         pbx_builtin_setvar_helper(c, "SS7_CUG_INTERLOCK_NI", p->cug_interlock_ni);
1250                 }
1251
1252                 p->cug_indicator = ISUP_CUG_NON;
1253         }
1254
1255         ast_channel_stage_snapshot_done(c);
1256
1257         sig_ss7_unlock_private(p);
1258         ast_channel_unlock(c);
1259
1260         if (ast_pbx_start(c)) {
1261                 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", ast_channel_name(c), p->cic);
1262                 ast_hangup(c);
1263         } else {
1264                 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
1265         }
1266
1267         /* Must return with linkset and private lock. */
1268         ast_mutex_lock(&linkset->lock);
1269         sig_ss7_lock_private(p);
1270         ast_callid_threadstorage_auto_clean(callid, callid_created);
1271 }
1272
1273 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
1274 {
1275         if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
1276                 if (size) {
1277                         *buf = '\0';
1278                 }
1279                 return;
1280         }
1281         switch (nai) {
1282         case SS7_NAI_INTERNATIONAL:
1283                 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
1284                 break;
1285         case SS7_NAI_NATIONAL:
1286                 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
1287                 break;
1288         case SS7_NAI_SUBSCRIBER:
1289                 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
1290                 break;
1291         case SS7_NAI_UNKNOWN:
1292                 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
1293                 break;
1294         case SS7_NAI_NETWORKROUTED:
1295                 snprintf(buf, size, "%s%s", ss7->networkroutedprefix, number);
1296                 break;
1297         default:
1298                 snprintf(buf, size, "%s", number);
1299                 break;
1300         }
1301 }
1302
1303 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
1304 {
1305         return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
1306 }
1307
1308 /*!
1309  * \internal
1310  * \brief Set callid threadstorage for the ss7_linkset thread to that of an existing channel
1311  *
1312  * \param linkset ss7 span control structure.
1313  * \param chanpos channel position in the span
1314  *
1315  * \note Assumes the ss7->lock is already obtained.
1316  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
1317  *
1318  * \return a reference to the callid bound to the channel which has also
1319  *         been bound to threadstorage if it exists. If this returns non-NULL,
1320  *         the callid must be unreffed and the threadstorage should be unbound
1321  *         before the while loop wraps in ss7_linkset.
1322  */
1323 static struct ast_callid *func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
1324 {
1325         struct ast_callid *callid = NULL;
1326         sig_ss7_lock_owner(linkset, chanpos);
1327         if (linkset->pvts[chanpos]->owner) {
1328                 callid = ast_channel_callid(linkset->pvts[chanpos]->owner);
1329                 ast_channel_unlock(linkset->pvts[chanpos]->owner);
1330                 if (callid) {
1331                         ast_callid_threadassoc_add(callid);
1332                 }
1333         }
1334
1335         return callid;
1336 }
1337
1338 /*!
1339  * \internal
1340  * \brief Proceed with the call based on the extension matching status
1341  * is matching in the dialplan.
1342  * \since 11.0
1343  *
1344  * \param linkset ss7 span control structure.
1345  * \param p Signaling private structure pointer.
1346  * \param e Event causing the match.
1347  *
1348  * \note Assumes the linkset->lock is already obtained.
1349  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
1350  *
1351  * \return Nothing.
1352  */
1353 static void ss7_match_extension(struct sig_ss7_linkset *linkset, struct sig_ss7_chan *p, ss7_event *e)
1354 {
1355         ast_verb(3, "SS7 exten: %s complete: %d\n", p->exten, p->called_complete);
1356
1357         if (!p->called_complete
1358                 && linkset->type == SS7_ITU /* ANSI does not support overlap dialing. */
1359                 && ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)
1360                 && !isup_start_digittimeout(linkset->ss7, p->ss7call)) {
1361                 /* Wait for more digits. */
1362                 return;
1363         }
1364         if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1365                 /* DNID is complete */
1366                 p->called_complete = 1;
1367                 sig_ss7_set_dnid(p, p->exten);
1368
1369                 /* If COT successful start call! */
1370                 if ((e->e == ISUP_EVENT_IAM)
1371                         ? !(e->iam.cot_check_required || e->iam.cot_performed_on_previous_cic)
1372                         : (!(e->sam.cot_check_required || e->sam.cot_performed_on_previous_cic) || e->sam.cot_check_passed)) {
1373                         ss7_start_call(p, linkset);
1374                 }
1375                 return;
1376         }
1377
1378         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1379         isup_rel(linkset->ss7, (e->e == ISUP_EVENT_IAM) ? e->iam.call : e->sam.call, AST_CAUSE_UNALLOCATED);
1380 }
1381
1382 /* This is a thread per linkset that handles all received events from libss7. */
1383 void *ss7_linkset(void *data)
1384 {
1385         int res, i;
1386         struct timeval *next = NULL, tv;
1387         struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
1388         struct ss7 *ss7 = linkset->ss7;
1389         ss7_event *e = NULL;
1390         struct sig_ss7_chan *p;
1391         struct pollfd pollers[SIG_SS7_NUM_DCHANS];
1392         unsigned char mb_state[255];
1393         int nextms;
1394
1395 #define SS7_MAX_POLL    60000   /* Maximum poll time in ms. */
1396
1397         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1398
1399         ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
1400         ast_mutex_lock(&linkset->lock);
1401         ss7_start(ss7);
1402         ast_mutex_unlock(&linkset->lock);
1403
1404         for (;;) {
1405                 ast_mutex_lock(&linkset->lock);
1406                 if ((next = ss7_schedule_next(ss7))) {
1407                         tv = ast_tvnow();
1408                         tv.tv_sec = next->tv_sec - tv.tv_sec;
1409                         tv.tv_usec = next->tv_usec - tv.tv_usec;
1410                         if (tv.tv_usec < 0) {
1411                                 tv.tv_usec += 1000000;
1412                                 tv.tv_sec -= 1;
1413                         }
1414                         if (tv.tv_sec < 0) {
1415                                 tv.tv_sec = 0;
1416                                 tv.tv_usec = 0;
1417                         }
1418                         nextms = tv.tv_sec * 1000;
1419                         nextms += tv.tv_usec / 1000;
1420                         if (SS7_MAX_POLL < nextms) {
1421                                 nextms = SS7_MAX_POLL;
1422                         }
1423                 } else {
1424                         nextms = SS7_MAX_POLL;
1425                 }
1426
1427                 for (i = 0; i < linkset->numsigchans; i++) {
1428                         pollers[i].fd = linkset->fds[i];
1429                         pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
1430                         pollers[i].revents = 0;
1431                 }
1432                 ast_mutex_unlock(&linkset->lock);
1433
1434                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1435                 pthread_testcancel();
1436                 res = poll(pollers, linkset->numsigchans, nextms);
1437                 pthread_testcancel();
1438                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1439
1440                 ast_mutex_lock(&linkset->lock);
1441                 if ((res < 0) && (errno != EINTR)) {
1442                         ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
1443                 } else if (!res) {
1444                         ss7_schedule_run(ss7);
1445                 }
1446
1447                 for (i = 0; i < linkset->numsigchans; i++) {
1448                         if (pollers[i].revents & POLLPRI) {
1449                                 sig_ss7_handle_link_exception(linkset, i);
1450                         }
1451                         if (pollers[i].revents & POLLIN) {
1452                                 res = ss7_read(ss7, pollers[i].fd);
1453                         }
1454                         if (pollers[i].revents & POLLOUT) {
1455                                 res = ss7_write(ss7, pollers[i].fd);
1456                                 if (res < 0) {
1457                                         ast_debug(1, "Error in write %s\n", strerror(errno));
1458                                 }
1459                         }
1460                 }
1461
1462                 while ((e = ss7_check_event(ss7))) {
1463                         struct ast_callid *callid = NULL;
1464                         int chanpos = -1;
1465                         char cause_str[30];
1466
1467                         if (linkset->debug) {
1468                                 ast_verbose("Linkset %d: Processing event: %s\n",
1469                                         linkset->span, ss7_event2str(e->e));
1470                         }
1471
1472                         switch (e->e) {
1473                         case SS7_EVENT_UP:
1474                                 if (linkset->state != LINKSET_STATE_UP) {
1475                                         ast_verb(1, "--- SS7 Up ---\n");
1476                                         ss7_reset_linkset(linkset);
1477                                 }
1478                                 linkset->state = LINKSET_STATE_UP;
1479                                 break;
1480                         case SS7_EVENT_DOWN:
1481                                 ast_verb(1, "--- SS7 Down ---\n");
1482                                 linkset->state = LINKSET_STATE_DOWN;
1483                                 for (i = 0; i < linkset->numchans; i++) {
1484                                         p = linkset->pvts[i];
1485                                         if (p) {
1486                                                 sig_ss7_set_inservice(p, 0);
1487                                                 if (linkset->flags & LINKSET_FLAG_INITIALHWBLO) {
1488                                                         sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_HARDWARE);
1489                                                 }
1490                                         }
1491                                 }
1492                                 break;
1493                         case MTP2_LINK_UP:
1494                                 ast_verb(1, "MTP2 link up (SLC %d)\n", e->gen.data);
1495                                 break;
1496                         case MTP2_LINK_DOWN:
1497                                 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
1498                                 break;
1499                         case ISUP_EVENT_CPG:
1500                                 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
1501                                 if (chanpos < 0) {
1502                                         isup_free_call(ss7, e->cpg.call);
1503                                         break;
1504                                 }
1505                                 p = linkset->pvts[chanpos];
1506                                 sig_ss7_lock_private(p);
1507                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1508
1509                                 switch (e->cpg.event) {
1510                                 case CPG_EVENT_ALERTING:
1511                                         if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
1512                                                 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1513                                         }
1514                                         sig_ss7_lock_owner(linkset, chanpos);
1515                                         if (p->owner) {
1516                                                 ast_setstate(p->owner, AST_STATE_RINGING);
1517                                                 if (!ast_strlen_zero(e->cpg.connected_num)) {
1518                                                         struct ast_party_connected_line ast_connected;
1519                                                         char connected_num[AST_MAX_EXTENSION];
1520
1521                                                         ast_party_connected_line_init(&ast_connected);
1522                                                         ast_connected.id.number.presentation =
1523                                                                 ss7_pres_scr2cid_pres(e->cpg.connected_presentation_ind,
1524                                                                 e->cpg.connected_screening_ind);
1525                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
1526                                                                 linkset, e->cpg.connected_num, e->cpg.connected_nai);
1527                                                         ast_connected.id.number.str = ast_strdup(connected_num);
1528                                                         ast_connected.id.number.valid = 1;
1529                                                         ast_channel_queue_connected_line_update(p->owner, &ast_connected, NULL);
1530                                                         ast_party_connected_line_free(&ast_connected);
1531                                                 }
1532                                                 ast_channel_unlock(p->owner);
1533                                         }
1534                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
1535                                         break;
1536                                 case CPG_EVENT_PROGRESS:
1537                                 case CPG_EVENT_INBANDINFO:
1538                                         {
1539                                                 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
1540                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
1541                                                 p->progress = 1;
1542                                                 sig_ss7_set_dialing(p, 0);
1543                                                 sig_ss7_open_media(p);
1544                                         }
1545                                         break;
1546                                 default:
1547                                         ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
1548                                         break;
1549                                 }
1550
1551                                 sig_ss7_unlock_private(p);
1552                                 break;
1553                         case ISUP_EVENT_RSC:
1554                                 ast_verb(1, "Resetting CIC %d\n", e->rsc.cic);
1555                                 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
1556                                 if (chanpos < 0) {
1557                                         isup_free_call(ss7, e->rsc.call);
1558                                         break;
1559                                 }
1560                                 p = linkset->pvts[chanpos];
1561                                 sig_ss7_lock_private(p);
1562                                 p->ss7call = e->rsc.call;
1563                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1564                                 sig_ss7_set_inservice(p, 1);
1565                                 sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1566
1567                                 if (p->locallyblocked & SS7_BLOCKED_MAINTENANCE) {
1568                                         isup_blo(ss7, e->rsc.call);
1569                                 } else if (p->locallyblocked & SS7_BLOCKED_HARDWARE) {
1570                                         sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_HARDWARE);
1571                                 }
1572
1573                                 isup_set_call_dpc(e->rsc.call, p->dpc);
1574                                 sig_ss7_lock_owner(linkset, chanpos);
1575                                 if (p->owner) {
1576                                         p->do_hangup = SS7_HANGUP_SEND_RLC;
1577                                         if (!(e->rsc.got_sent_msg & ISUP_SENT_IAM)) {
1578                                                 /* Q.784 6.2.3 */
1579                                                 ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CLEARING);
1580                                         } else {
1581                                                 ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
1582                                         }
1583
1584                                         ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_RSC", AST_CAUSE_INTERWORKING);
1585
1586                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1587                                         ast_channel_unlock(p->owner);
1588                                 } else {
1589                                         isup_rlc(ss7, e->rsc.call);
1590                                         p->ss7call = isup_free_call_if_clear(ss7, e->rsc.call);
1591                                 }
1592                                 /* End the loopback if we have one */
1593                                 sig_ss7_loopback(p, 0);
1594
1595                                 sig_ss7_unlock_private(p);
1596                                 break;
1597                         case ISUP_EVENT_GRS:
1598                                 if (!sig_ss7_find_cic_range(linkset, e->grs.startcic, e->grs.endcic,
1599                                         e->grs.opc)) {
1600                                         ast_log(LOG_WARNING, "GRS on unconfigured range CIC %d - %d PC %d\n",
1601                                                 e->grs.startcic, e->grs.endcic, e->grs.opc);
1602                                         chanpos = sig_ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
1603                                         if (chanpos < 0) {
1604                                                 isup_free_call(ss7, e->grs.call);
1605                                                 break;
1606                                         }
1607                                         p = linkset->pvts[chanpos];
1608                                         sig_ss7_lock_private(p);
1609                                         p->ss7call = isup_free_call_if_clear(ss7, e->grs.call);
1610                                         sig_ss7_unlock_private(p);
1611                                         break;
1612                                 }
1613
1614                                 /* Leave startcic last to collect all cics mb_state */
1615                                 for (i = e->grs.endcic - e->grs.startcic; 0 <= i; --i) {
1616                                         /*
1617                                          * We are guaranteed to find chanpos because
1618                                          * sig_ss7_find_cic_range() includes it.
1619                                          */
1620                                         chanpos = sig_ss7_find_cic(linkset, e->grs.startcic + i, e->grs.opc);
1621                                         p = linkset->pvts[chanpos];
1622                                         sig_ss7_lock_private(p);
1623
1624                                         if (p->locallyblocked & SS7_BLOCKED_MAINTENANCE) {
1625                                                 mb_state[i] = 1;
1626                                         } else {
1627                                                 mb_state[i] = 0;
1628                                                 sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_HARDWARE);
1629                                         }
1630
1631                                         sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1632
1633                                         if (!i) {
1634                                                 p->ss7call = e->grs.call;
1635                                                 isup_gra(ss7, p->ss7call, e->grs.endcic, mb_state);
1636                                         }
1637
1638                                         sig_ss7_lock_owner(linkset, chanpos);
1639                                         if (p->owner) {
1640                                                 ast_channel_softhangup_internal_flag_add(p->owner, AST_SOFTHANGUP_DEV);
1641                                                 if (ast_channel_state(p->owner) == AST_STATE_DIALING
1642                                                         && linkset->pvts[i]->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
1643                                                         ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
1644                                                 } else {
1645                                                         ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CLEARING);
1646                                                 }
1647                                                 p->do_hangup = SS7_HANGUP_FREE_CALL;
1648                                                 ast_channel_unlock(p->owner);
1649                                         } else if (!i) {
1650                                                 p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1651                                         } else if (p->ss7call) {
1652                                                 /* clear any other session */
1653                                                 isup_free_call(ss7, p->ss7call);
1654                                                 p->ss7call = NULL;
1655                                         }
1656                                         sig_ss7_set_inservice(p, 1);
1657                                         sig_ss7_unlock_private(p);
1658                                 }
1659                                 break;
1660                         case ISUP_EVENT_CQM:
1661                                 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n",
1662                                                         e->cqm.startcic, e->cqm.endcic);
1663                                 ss7_handle_cqm(linkset, e);
1664                                 break;
1665                         case ISUP_EVENT_GRA:
1666                                 if (!sig_ss7_find_cic_range(linkset, e->gra.startcic,
1667                                                         e->gra.endcic, e->gra.opc)) {   /* Never will be true */
1668                                         ast_log(LOG_WARNING, "GRA on unconfigured range CIC %d - %d PC %d\n",
1669                                                         e->gra.startcic, e->gra.endcic, e->gra.opc);
1670                                         isup_free_call(ss7, e->gra.call);
1671                                         break;
1672                                 }
1673                                 ast_verb(1, "Got reset acknowledgement from CIC %d to %d DPC: %d\n",
1674                                         e->gra.startcic, e->gra.endcic, e->gra.opc);
1675                                 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc,
1676                                         e->gra.status, 1, 1, SS7_BLOCKED_MAINTENANCE);
1677                                 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
1678
1679                                 chanpos = sig_ss7_find_cic(linkset, e->gra.startcic, e->gra.opc);
1680                                 if (chanpos < 0) {
1681                                         isup_free_call(ss7, e->gra.call);
1682                                         break;
1683                                 }
1684
1685                                 p = linkset->pvts[chanpos];
1686                                 sig_ss7_lock_private(p);
1687
1688                                 /* we may send a CBD with GRS! */
1689                                 p->ss7call = isup_free_call_if_clear(ss7, e->gra.call);
1690                                 sig_ss7_unlock_private(p);
1691                                 break;
1692                         case ISUP_EVENT_SAM:
1693                                 chanpos = ss7_find_cic_gripe(linkset, e->sam.cic, e->sam.opc, "SAM");
1694                                 if (chanpos < 0) {
1695                                         isup_free_call(ss7, e->sam.call);
1696                                         break;
1697                                 }
1698                                 p = linkset->pvts[chanpos];
1699                                 sig_ss7_lock_private(p);
1700                                 sig_ss7_lock_owner(linkset, chanpos);
1701                                 if (p->owner) {
1702                                         ast_log(LOG_WARNING, "SAM on CIC %d PC %d already have call\n", e->sam.cic, e->sam.opc);
1703                                         ast_channel_unlock(p->owner);
1704                                         sig_ss7_unlock_private(p);
1705                                         break;
1706                                 }
1707                                 p->called_complete = 0;
1708                                 if (!ast_strlen_zero(e->sam.called_party_num)) {
1709                                         char *st;
1710                                         strncat(p->exten, e->sam.called_party_num, sizeof(p->exten) - strlen(p->exten) - 1);
1711                                         st = strchr(p->exten, '#');
1712                                         if (st) {
1713                                                 *st = '\0';
1714                                                 p->called_complete = 1;
1715                                         }
1716                                         ss7_match_extension(linkset, p, e);
1717                                 }
1718                                 sig_ss7_unlock_private(p);
1719                                 break;
1720                         case ISUP_EVENT_IAM:
1721                                 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);
1722                                 chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
1723                                 if (chanpos < 0) {
1724                                         isup_free_call(ss7, e->iam.call);
1725                                         break;
1726                                 }
1727                                 p = linkset->pvts[chanpos];
1728                                 sig_ss7_lock_private(p);
1729                                 /*
1730                                  * The channel should be idle and not have an owner at this point since we
1731                                  * are in the process of creating an owner for it.
1732                                  */
1733                                 ast_assert(!p->owner && p->call_level == SIG_SS7_CALL_LEVEL_IDLE);
1734
1735                                 if (p->remotelyblocked) {
1736                                         ast_log(LOG_NOTICE, "Got IAM on remotely blocked CIC %d DPC %d remove blocking\n", e->iam.cic, e->iam.opc);
1737                                         sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1738                                         sig_ss7_set_inservice(p, 1);
1739                                 }
1740
1741                                 if (!sig_ss7_is_chan_available(p)) {
1742                                         /* Circuit is likely blocked or in alarm. */
1743                                         isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1744                                         if (p->locallyblocked) {
1745                                                 isup_clear_callflags(ss7, e->iam.call, ISUP_GOT_IAM);
1746                                                 p->ss7call = isup_free_call_if_clear(ss7, e->iam.call);
1747                                                 ast_log(LOG_WARNING, "Got IAM on locally blocked CIC %d DPC %d, ignore\n", e->iam.cic, e->iam.opc);
1748                                         }
1749                                         sig_ss7_unlock_private(p);
1750                                         break;
1751                                 }
1752
1753                                 /* Mark channel as in use so no outgoing call will steal it. */
1754                                 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1755                                 p->ss7call = e->iam.call;
1756
1757                                 isup_set_call_dpc(p->ss7call, p->dpc);
1758
1759                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
1760                                         ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
1761                                         p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1762                                 } else {
1763                                         p->cid_num[0] = 0;
1764                                         if (e->iam.presentation_ind) {
1765                                                 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1766                                         }
1767                                 }
1768
1769                                 p->called_complete = 0;
1770                                 if (p->immediate) {
1771                                         p->exten[0] = 's';
1772                                         p->exten[1] = '\0';
1773                                 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
1774                                         char *st;
1775                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
1776                                         st = strchr(p->exten, '#');
1777                                         if (st) {
1778                                                 *st = '\0';
1779                                                 p->called_complete = 1;
1780                                         }
1781                                 } else {
1782                                         p->exten[0] = '\0';
1783                                 }
1784
1785                                 p->cid_ani[0] = '\0';
1786                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name))) {
1787                                         ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
1788                                 } else {
1789                                         p->cid_name[0] = '\0';
1790                                 }
1791
1792                                 p->cid_ani2 = e->iam.oli_ani2;
1793                                 p->cid_ton = 0;
1794                                 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
1795                                 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
1796                                 p->gen_add_type = e->iam.gen_add_type;
1797                                 p->gen_add_nai = e->iam.gen_add_nai;
1798                                 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
1799                                 p->gen_add_num_plan = e->iam.gen_add_num_plan;
1800                                 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
1801                                 p->gen_dig_type = e->iam.gen_dig_type;
1802                                 p->gen_dig_scheme = e->iam.gen_dig_scheme;
1803                                 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
1804                                 if (!ast_strlen_zero(e->iam.orig_called_num)) {
1805                                         ss7_apply_plan_to_number(p->orig_called_num, sizeof(p->orig_called_num), linkset, e->iam.orig_called_num, e->iam.orig_called_nai);
1806                                         p->orig_called_presentation = ss7_pres_scr2cid_pres(e->iam.orig_called_pres_ind, e->iam.orig_called_screening_ind);
1807                                 }
1808                                 if (!ast_strlen_zero(e->iam.redirecting_num)) {
1809                                         ss7_apply_plan_to_number(p->redirecting_num, sizeof(p->redirecting_num), linkset, e->iam.redirecting_num, e->iam.redirecting_num_nai);
1810                                         p->redirecting_presentation = ss7_pres_scr2cid_pres(e->iam.redirecting_num_presentation_ind, e->iam.redirecting_num_screening_ind);
1811                                 }
1812                                 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
1813                                 p->calling_party_cat = e->iam.calling_party_cat;
1814                                 p->redirect_counter = e->iam.redirect_counter;
1815                                 p->redirect_info = e->iam.redirect_info;
1816                                 p->redirect_info_ind = e->iam.redirect_info_ind;
1817                                 p->redirect_info_orig_reas = e->iam.redirect_info_orig_reas;
1818                                 p->redirect_info_counter = e->iam.redirect_info_counter;
1819                                 if (p->redirect_info_counter && !p->redirect_counter) {
1820                                         p->redirect_counter = p->redirect_info_counter;
1821                                 }
1822                                 p->redirect_info_reas = e->iam.redirect_info_reas;
1823                                 p->cug_indicator = e->iam.cug_indicator;
1824                                 p->cug_interlock_code = e->iam.cug_interlock_code;
1825                                 ast_copy_string(p->cug_interlock_ni, e->iam.cug_interlock_ni, sizeof(p->cug_interlock_ni));
1826
1827                                 if (e->iam.cot_check_required) {
1828                                         sig_ss7_loopback(p, 1);
1829                                 }
1830
1831                                 p->echocontrol_ind = e->iam.echocontrol_ind;
1832                                 sig_ss7_set_caller_id(p);
1833                                 ss7_match_extension(linkset, p, e);
1834                                 sig_ss7_unlock_private(p);
1835
1836                                 if (e->iam.cot_performed_on_previous_cic) {
1837                                         chanpos = sig_ss7_find_cic(linkset, (e->iam.cic - 1), e->iam.opc);
1838                                         if (chanpos < 0) {
1839                                                 /* some stupid switch do this */
1840                                                 ast_verb(1, "COT request on previous nonexistent CIC %d in IAM PC %d\n", (e->iam.cic - 1), e->iam.opc);
1841                                                 break;
1842                                         }
1843                                         ast_verb(1, "COT request on previous CIC %d in IAM PC %d\n", (e->iam.cic - 1), e->iam.opc);
1844                                         p = linkset->pvts[chanpos];
1845                                         sig_ss7_lock_private(p);
1846                                         if (sig_ss7_is_chan_available(p)) {
1847                                                 sig_ss7_set_inservice(p, 0);    /* to prevent to use this circuit */
1848                                                 sig_ss7_loopback(p, 1);
1849                                         } /* If already have a call don't loop */
1850                                         sig_ss7_unlock_private(p);
1851                                 }
1852                                 break;
1853                         case ISUP_EVENT_DIGITTIMEOUT:
1854                                 chanpos = ss7_find_cic_gripe(linkset, e->digittimeout.cic, e->digittimeout.opc, "DIGITTIMEOUT");
1855                                 if (chanpos < 0) {
1856                                         isup_free_call(ss7, e->digittimeout.call);
1857                                         break;
1858                                 }
1859                                 p = linkset->pvts[chanpos];
1860                                 sig_ss7_lock_private(p);
1861                                 ast_debug(1, "Digittimeout on CIC: %d PC: %d\n", e->digittimeout.cic, e->digittimeout.opc);
1862                                 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1863                                         /* DNID is complete */
1864                                         p->called_complete = 1;
1865                                         sig_ss7_set_dnid(p, p->exten);
1866
1867                                         /* If COT successful start call! */
1868                                         if (!(e->digittimeout.cot_check_required || e->digittimeout.cot_performed_on_previous_cic) || e->digittimeout.cot_check_passed) {
1869                                                 ss7_start_call(p, linkset);
1870                                         }
1871                                 } else {
1872                                         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1873                                         isup_rel(linkset->ss7, e->digittimeout.call, AST_CAUSE_UNALLOCATED);
1874                                 }
1875                                 sig_ss7_unlock_private(p);
1876                                 break;
1877                         case ISUP_EVENT_COT:
1878                                 if (e->cot.cot_performed_on_previous_cic) {
1879                                         chanpos = sig_ss7_find_cic(linkset, (e->cot.cic - 1), e->cot.opc);
1880                                         /* some stupid switches do this!!! */
1881                                         if (-1 < chanpos) {
1882                                                 p = linkset->pvts[chanpos];
1883                                                 sig_ss7_lock_private(p);
1884                                                 sig_ss7_set_inservice(p, 1);
1885                                                 sig_ss7_loopback(p, 0);
1886                                                 sig_ss7_unlock_private(p);;
1887                                                 ast_verb(1, "Loop turned off on CIC: %d PC: %d\n",  (e->cot.cic - 1), e->cot.opc);
1888                                         }
1889                                 }
1890
1891                                 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
1892                                 if (chanpos < 0) {
1893                                         isup_free_call(ss7, e->cot.call);
1894                                         break;
1895                                 }
1896                                 p = linkset->pvts[chanpos];
1897
1898                                 sig_ss7_lock_private(p);
1899                                 p->ss7call = e->cot.call;
1900
1901                                 if (p->loopedback) {
1902                                         sig_ss7_loopback(p, 0);
1903                                         ast_verb(1, "Loop turned off on CIC: %d PC: %d\n",  e->cot.cic, e->cot.opc);
1904                                 }
1905
1906                                 /* Don't start call if we didn't get IAM or COT failed! */
1907                                 if ((e->cot.got_sent_msg & ISUP_GOT_IAM) && e->cot.passed && p->called_complete) {
1908                                         ss7_start_call(p, linkset);
1909                                 }
1910
1911                                 p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1912                                 sig_ss7_unlock_private(p);
1913                                 break;
1914                         case ISUP_EVENT_CCR:
1915                                 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
1916                                 chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
1917                                 if (chanpos < 0) {
1918                                         isup_free_call(ss7, e->ccr.call);
1919                                         break;
1920                                 }
1921
1922                                 p = linkset->pvts[chanpos];
1923
1924                                 sig_ss7_lock_private(p);
1925                                 p->ss7call = e->ccr.call;
1926                                 sig_ss7_loopback(p, 1);
1927                                 if (linkset->type == SS7_ANSI) {
1928                                         isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
1929                                 }
1930                                 sig_ss7_unlock_private(p);
1931                                 break;
1932                         case ISUP_EVENT_CVT:
1933                                 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
1934                                 chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
1935                                 if (chanpos < 0) {
1936                                         isup_free_call(ss7, e->cvt.call);
1937                                         break;
1938                                 }
1939
1940                                 p = linkset->pvts[chanpos];
1941
1942                                 sig_ss7_lock_private(p);
1943                                 p->ss7call = e->cvt.call;
1944                                 sig_ss7_loopback(p, 1);
1945                                 if (!p->owner) {
1946                                         p->ss7call = isup_free_call_if_clear(ss7, e->cvt.call);
1947                                 }
1948                                 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
1949                                 sig_ss7_unlock_private(p);
1950                                 break;
1951                         case ISUP_EVENT_REL:
1952                                 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
1953                                 if (chanpos < 0) {
1954                                         isup_free_call(ss7, e->rel.call);
1955                                         break;
1956                                 }
1957                                 p = linkset->pvts[chanpos];
1958                                 sig_ss7_lock_private(p);
1959                                 p->ss7call = e->rel.call;
1960                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1961                                 sig_ss7_lock_owner(linkset, chanpos);
1962                                 if (p->owner) {
1963                                         snprintf(cause_str, sizeof(cause_str), "SS7 ISUP_EVENT_REL (%d)", e->rel.cause);
1964                                         ss7_queue_pvt_cause_data(p->owner, cause_str, e->rel.cause);
1965
1966                                         ast_channel_hangupcause_set(p->owner, e->rel.cause);
1967                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1968                                         p->do_hangup = SS7_HANGUP_SEND_RLC;
1969                                         ast_channel_unlock(p->owner);
1970                                 } else {
1971                                         ast_verb(1, "REL on CIC %d DPC %d without owner!\n", p->cic, p->dpc);
1972                                         isup_rlc(ss7, p->ss7call);
1973                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1974                                 }
1975
1976                                 /* End the loopback if we have one */
1977                                 sig_ss7_loopback(p, 0);
1978
1979                                 /* the rel is not complete here!!! */
1980                                 sig_ss7_unlock_private(p);
1981                                 break;
1982                         case ISUP_EVENT_ACM:
1983                                 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
1984                                 if (chanpos < 0) {
1985                                         isup_free_call(ss7, e->acm.call);
1986                                         break;
1987                                 }
1988
1989                                 p = linkset->pvts[chanpos];
1990
1991                                 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
1992
1993                                 if (e->acm.call_ref_ident > 0) {
1994                                         p->rlt = 1; /* Setting it but not using it here*/
1995                                 }
1996
1997                                 sig_ss7_lock_private(p);
1998                                 p->ss7call = e->acm.call;
1999                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2000                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
2001                                 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
2002                                         p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
2003                                 }
2004                                 sig_ss7_set_dialing(p, 0);
2005                                 /* Send alerting if subscriber is free */
2006                                 if (e->acm.called_party_status_ind == 1) {
2007                                         if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
2008                                                 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
2009                                         }
2010                                         sig_ss7_lock_owner(linkset, chanpos);
2011                                         if (p->owner) {
2012                                                 ast_setstate(p->owner, AST_STATE_RINGING);
2013                                                 ast_channel_unlock(p->owner);
2014                                         }
2015                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
2016                                 }
2017                                 p->echocontrol_ind = e->acm.echocontrol_ind;
2018                                 sig_ss7_unlock_private(p);
2019                                 break;
2020                         case ISUP_EVENT_CGB:
2021                                 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
2022                                 if (chanpos < 0) {
2023                                         isup_free_call(ss7, e->cgb.call);
2024                                         break;
2025                                 }
2026                                 p = linkset->pvts[chanpos];
2027                                 ss7_check_range(linkset, e->cgb.startcic, e->cgb.endcic,
2028                                         e->cgb.opc, e->cgb.status);
2029                                 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic,
2030                                         e->cgb.opc, e->cgb.status, 1, 1,
2031                                         (e->cgb.type) ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2032
2033                                 sig_ss7_lock_private(p);
2034                                 p->ss7call = e->cgb.call;
2035
2036                                 isup_cgba(linkset->ss7, p->ss7call, e->cgb.endcic, e->cgb.status);
2037                                 if (!p->owner) {
2038                                         p->ss7call = isup_free_call_if_clear(ss7, e->cgb.call);
2039                                 }
2040                                 sig_ss7_unlock_private(p);
2041                                 break;
2042                         case ISUP_EVENT_CGU:
2043                                 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
2044                                 if (chanpos < 0) {
2045                                         isup_free_call(ss7, e->cgu.call);
2046                                         break;
2047                                 }
2048                                 p = linkset->pvts[chanpos];
2049                                 ss7_check_range(linkset, e->cgu.startcic, e->cgu.endcic,
2050                                         e->cgu.opc, e->cgu.status);
2051                                 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic,
2052                                         e->cgu.opc, e->cgu.status, 0, 1,
2053                                         e->cgu.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2054
2055                                 sig_ss7_lock_private(p);
2056                                 p->ss7call = e->cgu.call;
2057
2058                                 isup_cgua(linkset->ss7, p->ss7call, e->cgu.endcic, e->cgu.status);
2059                                 if (!p->owner) {
2060                                         p->ss7call = isup_free_call_if_clear(ss7, e->cgu.call);
2061                                 }
2062                                 sig_ss7_unlock_private(p);
2063                                 break;
2064                         case ISUP_EVENT_UCIC:
2065                                 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
2066                                 if (chanpos < 0) {
2067                                         isup_free_call(ss7, e->ucic.call);
2068                                         break;
2069                                 }
2070                                 p = linkset->pvts[chanpos];
2071                                 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
2072                                 sig_ss7_lock_private(p);
2073                                 sig_ss7_lock_owner(linkset, chanpos);
2074                                 if (p->owner) {
2075                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2076                                         ast_channel_unlock(p->owner);
2077                                 }
2078                                 sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2079                                 sig_ss7_set_inservice(p, 0);
2080                                 p->ss7call = NULL;
2081                                 isup_free_call(ss7, e->ucic.call);
2082                                 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
2083                                 break;
2084                         case ISUP_EVENT_BLO:
2085                                 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
2086                                 if (chanpos < 0) {
2087                                         isup_free_call(ss7, e->blo.call);
2088                                         break;
2089                                 }
2090                                 p = linkset->pvts[chanpos];
2091                                 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
2092                                 sig_ss7_lock_private(p);
2093                                 p->ss7call = e->blo.call;
2094                                 sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2095                                 isup_bla(linkset->ss7, e->blo.call);
2096                                 sig_ss7_lock_owner(linkset, chanpos);
2097                                 if (!p->owner) {
2098                                         p->ss7call = isup_free_call_if_clear(ss7, e->blo.call);
2099                                 } else {
2100                                         if (e->blo.got_sent_msg & ISUP_SENT_IAM) {
2101                                                 /* Q.784 6.2.2 */
2102                                                 ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
2103                                         }
2104                                         ast_channel_unlock(p->owner);
2105                                 }
2106                                 sig_ss7_unlock_private(p);
2107                                 break;
2108                         case ISUP_EVENT_BLA:
2109                                 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
2110                                 if (chanpos < 0) {
2111                                         isup_free_call(ss7, e->bla.call);
2112                                         break;
2113                                 }
2114                                 ast_debug(1, "Locally blocking CIC %d\n", e->bla.cic);
2115                                 p = linkset->pvts[chanpos];
2116                                 sig_ss7_lock_private(p);
2117                                 p->ss7call = e->bla.call;
2118                                 sig_ss7_set_locallyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2119                                 if (!p->owner) {
2120                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2121                                 }
2122                                 sig_ss7_unlock_private(p);
2123                                 break;
2124                         case ISUP_EVENT_UBL:
2125                                 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
2126                                 if (chanpos < 0) {
2127                                         isup_free_call(ss7, e->ubl.call);
2128                                         break;
2129                                 }
2130                                 p = linkset->pvts[chanpos];
2131                                 ast_debug(1, "Remotely unblocking CIC %d PC %d\n", e->ubl.cic, e->ubl.opc);
2132                                 sig_ss7_lock_private(p);
2133                                 p->ss7call = e->ubl.call;
2134                                 sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE);
2135                                 isup_uba(linkset->ss7, e->ubl.call);
2136                                 if (!p->owner) {
2137                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2138                                 }
2139                                 sig_ss7_unlock_private(p);
2140                                 break;
2141                         case ISUP_EVENT_UBA:
2142                                 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
2143                                 if (chanpos < 0) {
2144                                         isup_free_call(ss7, e->uba.call);
2145                                         break;
2146                                 }
2147                                 p = linkset->pvts[chanpos];
2148                                 ast_debug(1, "Locally unblocking CIC %d PC %d\n", e->uba.cic, e->uba.opc);
2149                                 sig_ss7_lock_private(p);
2150                                 p->ss7call = e->uba.call;
2151                                 sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_MAINTENANCE);
2152                                 if (!p->owner) {
2153                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2154                                 }
2155                                 sig_ss7_unlock_private(p);
2156                                 break;
2157                         case ISUP_EVENT_CON:
2158                         case ISUP_EVENT_ANM:
2159                                 if (e->e == ISUP_EVENT_CON) {
2160                                         chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
2161                                         if (chanpos < 0) {
2162                                                 isup_free_call(ss7, e->con.call);
2163                                                 break;
2164                                         }
2165                                 } else {
2166                                         chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
2167                                         if (chanpos < 0) {
2168                                                 isup_free_call(ss7, e->anm.call);
2169                                                 break;
2170                                         }
2171                                 }
2172
2173                                 p = linkset->pvts[chanpos];
2174                                 sig_ss7_lock_private(p);
2175                                 p->ss7call = (e->e == ISUP_EVENT_ANM) ?  e->anm.call : e->con.call;
2176                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2177                                 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
2178                                         p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
2179                                 }
2180
2181                                 if (!ast_strlen_zero((e->e == ISUP_EVENT_ANM)
2182                                         ? e->anm.connected_num : e->con.connected_num)) {
2183                                         sig_ss7_lock_owner(linkset, chanpos);
2184                                         if (p->owner) {
2185                                                 struct ast_party_connected_line ast_connected;
2186                                                 char connected_num[AST_MAX_EXTENSION];
2187
2188                                                 ast_party_connected_line_init(&ast_connected);
2189                                                 if (e->e == ISUP_EVENT_ANM) {
2190                                                         ast_connected.id.number.presentation = ss7_pres_scr2cid_pres(
2191                                                                 e->anm.connected_presentation_ind,
2192                                                                 e->anm.connected_screening_ind);
2193                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
2194                                                                 linkset, e->anm.connected_num, e->anm.connected_nai);
2195                                                         ast_connected.id.number.str = ast_strdup(connected_num);
2196                                                 } else {
2197                                                         ast_connected.id.number.presentation = ss7_pres_scr2cid_pres(
2198                                                                 e->con.connected_presentation_ind,
2199                                                                 e->con.connected_screening_ind);
2200                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
2201                                                                 linkset, e->con.connected_num, e->con.connected_nai);
2202                                                         ast_connected.id.number.str = ast_strdup(connected_num);
2203                                                 }
2204                                                 ast_connected.id.number.valid = 1;
2205                                                 ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
2206                                                 ast_channel_queue_connected_line_update(p->owner, &ast_connected, NULL);
2207                                                 ast_party_connected_line_free(&ast_connected);
2208                                                 ast_channel_unlock(p->owner);
2209                                         }
2210                                 }
2211
2212                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
2213                                 sig_ss7_set_dialing(p, 0);
2214                                 sig_ss7_open_media(p);
2215                                 if (((e->e == ISUP_EVENT_ANM) ? !e->anm.echocontrol_ind  :
2216                                                 !e->con.echocontrol_ind) || !(linkset->flags & LINKSET_FLAG_USEECHOCONTROL)) {
2217                                         sig_ss7_set_echocanceller(p, 1);
2218                                 }
2219                                 sig_ss7_unlock_private(p);
2220                                 break;
2221                         case ISUP_EVENT_RLC:
2222                                 chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
2223                                 if (chanpos < 0) {
2224                                         isup_free_call(ss7, e->rlc.call);
2225                                         break;
2226                                 }
2227
2228                                 p = linkset->pvts[chanpos];
2229                                 sig_ss7_lock_private(p);
2230                                 p->ss7call = e->rlc.call;
2231                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2232                                 if (e->rlc.got_sent_msg & (ISUP_SENT_RSC | ISUP_SENT_REL)) {
2233                                         sig_ss7_loopback(p, 0);
2234                                         if (e->rlc.got_sent_msg & ISUP_SENT_RSC) {
2235                                                 sig_ss7_set_inservice(p, 1);
2236                                         }
2237                                 }
2238                                 sig_ss7_lock_owner(linkset, chanpos);
2239                                 if (!p->owner) {
2240                                         p->ss7call = isup_free_call_if_clear(ss7, e->rlc.call);
2241                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
2242                                 } else {
2243                                         p->do_hangup = SS7_HANGUP_DO_NOTHING;
2244                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2245                                         ast_channel_unlock(p->owner);
2246                                 }
2247                                 sig_ss7_unlock_private(p);
2248                                 break;
2249                         case ISUP_EVENT_FAA:
2250                                 /*!
2251                                  * \todo The handling of the SS7 FAA message is not good and I
2252                                  * don't know enough to handle it correctly.
2253                                  */
2254                                 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
2255                                 if (chanpos < 0) {
2256                                         isup_free_call(ss7, e->faa.call);
2257                                         break;
2258                                 }
2259
2260                                 /* XXX FAR and FAA used for something dealing with transfers? */
2261                                 p = linkset->pvts[chanpos];
2262                                 sig_ss7_lock_private(p);
2263                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2264                                 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
2265                                 p->ss7call = isup_free_call_if_clear(ss7, e->faa.call);
2266                                 sig_ss7_unlock_private(p);
2267                                 break;
2268                         case ISUP_EVENT_CGBA:
2269                                 chanpos = ss7_find_cic_gripe(linkset, e->cgba.startcic, e->cgba.opc, "CGBA");
2270                                 if (chanpos < 0) {      /* Never will be true */
2271                                         isup_free_call(ss7, e->cgba.call);
2272                                         break;
2273                                 }
2274
2275                                 ss7_block_cics(linkset, e->cgba.startcic, e->cgba.endcic,
2276                                         e->cgba.opc, e->cgba.status, 1, 0,
2277                                         e->cgba.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2278
2279                                 p = linkset->pvts[chanpos];
2280                                 sig_ss7_lock_private(p);
2281                                 p->ss7call = e->cgba.call;
2282
2283                                 if (!p->owner) {
2284                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2285                                 }
2286                                 sig_ss7_unlock_private(p);
2287                                 break;
2288                         case ISUP_EVENT_CGUA:
2289                                 chanpos = ss7_find_cic_gripe(linkset, e->cgua.startcic, e->cgua.opc, "CGUA");
2290                                 if (chanpos < 0) { /* Never will be true */
2291                                         isup_free_call(ss7, e->cgua.call);
2292                                         break;
2293                                 }
2294
2295                                 ss7_block_cics(linkset, e->cgua.startcic, e->cgua.endcic,
2296                                         e->cgua.opc, e->cgua.status, 0, 0,
2297                                         e->cgba.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2298
2299                                 p = linkset->pvts[chanpos];
2300                                 sig_ss7_lock_private(p);
2301                                 p->ss7call = e->cgua.call;
2302
2303                                 if (!p->owner) {
2304                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2305                                 }
2306                                 sig_ss7_unlock_private(p);
2307                                 break;
2308                         case ISUP_EVENT_SUS:
2309                                 chanpos = ss7_find_cic_gripe(linkset, e->sus.cic, e->sus.opc, "SUS");
2310                                 if (chanpos < 0) {
2311                                         isup_free_call(ss7, e->sus.call);
2312                                         break;
2313                                 }
2314
2315                                 p = linkset->pvts[chanpos];
2316                                 sig_ss7_lock_private(p);
2317                                 p->ss7call = e->sus.call;
2318                                 if (!p->owner) {
2319                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2320                                 }
2321                                 sig_ss7_unlock_private(p);
2322                                 break;
2323                         case ISUP_EVENT_RES:
2324                                 chanpos = ss7_find_cic_gripe(linkset, e->res.cic, e->res.opc, "RES");
2325                                 if (chanpos < 0) {
2326                                         isup_free_call(ss7, e->res.call);
2327                                         break;
2328                                 }
2329
2330                                 p = linkset->pvts[chanpos];
2331                                 sig_ss7_lock_private(p);
2332                                 p->ss7call = e->res.call;
2333                                 if (!p->owner) {
2334                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2335                                 }
2336                                 sig_ss7_unlock_private(p);
2337                                 break;
2338                         default:
2339                                 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
2340                                 break;
2341                         }
2342
2343                         /* Call ID stuff needs to be cleaned up here */
2344                         if (callid) {
2345                                 callid = ast_callid_unref(callid);
2346                                 ast_callid_threadassoc_remove();
2347                         }
2348                 }
2349                 ast_mutex_unlock(&linkset->lock);
2350         }
2351
2352         return 0;
2353 }
2354
2355 static void ss7_rel(struct sig_ss7_linkset *ss7)
2356 {
2357         /* Release the lock first */
2358         ast_mutex_unlock(&ss7->lock);
2359
2360         /* Then break the poll to send our messages */
2361         if (ss7->master != AST_PTHREADT_NULL) {
2362                 pthread_kill(ss7->master, SIGURG);
2363         }
2364 }
2365
2366 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
2367 {
2368         /* Grab the lock first */
2369         while (ast_mutex_trylock(&ss7->lock)) {
2370                 /* Avoid deadlock */
2371                 sig_ss7_deadlock_avoidance_private(pvt);
2372         }
2373 }
2374
2375 /*!
2376  * \brief Reset a specific CIC.
2377  * \since 11.0
2378  *
2379  * \param linkset linkset control structure.
2380  * \param cic Circuit Identification Code
2381  * \param dpc Destination Point Code
2382  *
2383  * \return TRUE on success
2384  */
2385 int sig_ss7_reset_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
2386 {
2387         int i;
2388
2389         ast_mutex_lock(&linkset->lock);
2390         for (i = 0; i < linkset->numchans; i++) {
2391                 if (linkset->pvts[i] && linkset->pvts[i]->cic == cic && linkset->pvts[i]->dpc == dpc) {
2392                         int res;
2393
2394                         sig_ss7_lock_private(linkset->pvts[i]);
2395                         sig_ss7_set_locallyblocked(linkset->pvts[i], 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2396                         res = ss7_start_rsc(linkset, i);
2397                         sig_ss7_unlock_private(linkset->pvts[i]);
2398                         ss7_rel(linkset);       /* Also breaks the poll to send our messages */
2399                         return res;
2400                 }
2401         }
2402         ss7_rel(linkset);
2403
2404         return 0;
2405 }
2406
2407 /*!
2408  * \brief Block or Unblock a specific CIC.
2409  * \since 11.0
2410  *
2411  * \param linkset linkset control structure.
2412  * \param do_block Action to perform. Block if TRUE.
2413  * \param which On which CIC to perform the operation.
2414  *
2415  * \return 0 on success
2416  */
2417 int sig_ss7_cic_blocking(struct sig_ss7_linkset *linkset, int do_block, int which)
2418 {
2419         ast_mutex_lock(&linkset->lock);
2420         sig_ss7_lock_private(linkset->pvts[which]);
2421         if (!ss7_find_alloc_call(linkset->pvts[which])) {
2422                 sig_ss7_unlock_private(linkset->pvts[which]);
2423                 ss7_rel(linkset);
2424                 return -1;
2425         }
2426
2427         if (do_block) {
2428                 isup_blo(linkset->ss7, linkset->pvts[which]->ss7call);
2429         } else {
2430                 isup_ubl(linkset->ss7, linkset->pvts[which]->ss7call);
2431         }
2432
2433         sig_ss7_unlock_private(linkset->pvts[which]);
2434         ss7_rel(linkset);       /* Also breaks the poll to send our messages */
2435
2436         return 0;
2437 }
2438
2439 /*!
2440  * \brief Block or Unblock a range of CICs.
2441  * \since 11.0
2442  *
2443  * \param linkset linkset control structure.
2444  * \param do_block Action to perform. Block if TRUE.
2445  * \param chanpos Channel position to start from.
2446  * \param endcic Circuit Identification Code of the end of the range.
2447  * \param state Array of CIC blocking status.
2448  * \param type Type of the blocking - maintenance or hardware
2449  *
2450  * \note Assumes the linkset->lock is already obtained.
2451  *
2452  * \return 0 on success
2453  */
2454 int sig_ss7_group_blocking(struct sig_ss7_linkset *linkset, int do_block, int chanpos, int endcic, unsigned char state[], int type)
2455 {
2456         sig_ss7_lock_private(linkset->pvts[chanpos]);
2457         if (!ss7_find_alloc_call(linkset->pvts[chanpos])) {
2458                 sig_ss7_unlock_private(linkset->pvts[chanpos]);
2459                 return -1;
2460         }
2461
2462         if (do_block) {
2463                 isup_cgb(linkset->ss7, linkset->pvts[chanpos]->ss7call, endcic, state, type);
2464         } else {
2465                 isup_cgu(linkset->ss7, linkset->pvts[chanpos]->ss7call, endcic, state, type);
2466         }
2467
2468         sig_ss7_unlock_private(linkset->pvts[chanpos]);
2469         return 0;
2470 }
2471
2472 /*!
2473  * \brief Reset a group of CICs.
2474  * \since 11.0
2475  *
2476  * \param linkset linkset control structure.
2477  * \param cic Circuit Identification Code
2478  * \param dpc Destination Point Code
2479  * \param range Range of the CICs to reset
2480  *
2481  * \note Assumes the linkset->lock is already obtained.
2482  *
2483  * \return 0 on success
2484  */
2485 int sig_ss7_reset_group(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, int range)
2486 {
2487         int i;
2488
2489         for (i = 0; i < linkset->numchans; i++) {
2490                 if (linkset->pvts[i] && linkset->pvts[i]->cic == cic && linkset->pvts[i]->dpc == dpc) {
2491                         ss7_clear_channels(linkset, cic, cic + range, dpc, SS7_HANGUP_FREE_CALL);
2492                         ss7_block_cics(linkset, cic, cic + range, dpc, NULL, 0, 1,
2493                                 SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2494                         ss7_block_cics(linkset, cic, cic + range, dpc, NULL, 0, 0,
2495                                 SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2496
2497                         sig_ss7_lock_private(linkset->pvts[i]);
2498                         if (!ss7_find_alloc_call(linkset->pvts[i])) {
2499                                 sig_ss7_unlock_private(linkset->pvts[i]);
2500                                 return -1;
2501                         }
2502                         isup_grs(linkset->ss7, linkset->pvts[i]->ss7call, linkset->pvts[i]->cic + range);
2503                         sig_ss7_unlock_private(linkset->pvts[i]);
2504                         break;
2505                 }
2506         }
2507         return 0;
2508 }
2509
2510 void sig_ss7_free_isup_call(struct sig_ss7_linkset *linkset, int channel)
2511 {
2512         sig_ss7_lock_private(linkset->pvts[channel]);
2513         if (linkset->pvts[channel]->ss7call) {
2514                 isup_free_call(linkset->ss7, linkset->pvts[channel]->ss7call);
2515                 linkset->pvts[channel]->ss7call = NULL;
2516         }
2517         sig_ss7_unlock_private(linkset->pvts[channel]);
2518 }
2519
2520 static int ss7_parse_prefix(struct sig_ss7_chan *p, const char *number, char *nai)
2521 {
2522         int strip = 0;
2523
2524         if (strncmp(number, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2525                 strip = strlen(p->ss7->internationalprefix);
2526                 *nai = SS7_NAI_INTERNATIONAL;
2527         } else if (strncmp(number, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2528                 strip = strlen(p->ss7->nationalprefix);
2529                 *nai = SS7_NAI_NATIONAL;
2530         } else if (strncmp(number, p->ss7->networkroutedprefix, strlen(p->ss7->networkroutedprefix)) == 0) {
2531                 strip = strlen(p->ss7->networkroutedprefix);
2532                 *nai = SS7_NAI_NETWORKROUTED;
2533         } else if (strncmp(number, p->ss7->unknownprefix, strlen(p->ss7->unknownprefix)) == 0) {
2534                 strip = strlen(p->ss7->unknownprefix);
2535                 *nai = SS7_NAI_UNKNOWN;
2536         } else if (strncmp(number, p->ss7->subscriberprefix, strlen(p->ss7->subscriberprefix)) == 0) {
2537                 strip = strlen(p->ss7->subscriberprefix);
2538                 *nai = SS7_NAI_SUBSCRIBER;
2539         } else {
2540                 *nai = SS7_NAI_SUBSCRIBER;
2541         }
2542
2543         return strip;
2544 }
2545
2546 /*!
2547  * \brief Notify the SS7 layer that the link is in alarm.
2548  * \since 1.8
2549  *
2550  * \param linkset Controlling linkset for the channel.
2551  * \param which Link index of the signaling channel.
2552  *
2553  * \return Nothing
2554  */
2555 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
2556 {
2557         linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
2558         linkset->linkstate[which] &= ~LINKSTATE_UP;
2559         ss7_link_alarm(linkset->ss7, linkset->fds[which]);
2560 }
2561
2562 /*!
2563  * \brief Notify the SS7 layer that the link is no longer in alarm.
2564  * \since 1.8
2565  *
2566  * \param linkset Controlling linkset for the channel.
2567  * \param which Link index of the signaling channel.
2568  *
2569  * \return Nothing
2570  */
2571 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
2572 {
2573         linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
2574         linkset->linkstate[which] |= LINKSTATE_STARTING;
2575         ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
2576 }
2577
2578 /*!
2579  * \brief Setup and add a SS7 link channel.
2580  * \since 1.8
2581  *
2582  * \param linkset Controlling linkset for the channel.
2583  * \param which Link index of the signaling channel.
2584  * \param ss7type Switch type of the linkset
2585  * \param transport Signaling transport of channel.
2586  * \param inalarm Non-zero if the channel is in alarm.
2587  * \param networkindicator User configuration parameter.
2588  * \param pointcode User configuration parameter.
2589  * \param adjpointcode User configuration parameter.
2590  *
2591  * \retval 0 on success.
2592  * \retval -1 on error.
2593  */
2594 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode, int cur_slc)
2595 {
2596         if (!linkset->ss7) {
2597                 linkset->type = ss7type;
2598                 linkset->ss7 = ss7_new(ss7type);
2599                 if (!linkset->ss7) {
2600                         ast_log(LOG_ERROR, "Can't create new SS7!\n");
2601                         return -1;
2602                 }
2603         }
2604
2605         ss7_set_network_ind(linkset->ss7, networkindicator);
2606         ss7_set_pc(linkset->ss7, pointcode);
2607
2608         if (ss7_add_link(linkset->ss7, transport, linkset->fds[which], cur_slc, adjpointcode)) {
2609                 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
2610         }
2611
2612         if (inalarm) {
2613                 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
2614                 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
2615         } else {
2616                 linkset->linkstate[which] = LINKSTATE_DOWN;
2617                 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
2618         }
2619
2620         return 0;
2621 }
2622
2623 /*!
2624  * \brief Determine if the specified channel is available for an outgoing call.
2625  * \since 1.8
2626  *
2627  * \param p Signaling private structure pointer.
2628  *
2629  * \retval TRUE if the channel is available.
2630  */
2631 int sig_ss7_available(struct sig_ss7_chan *p)
2632 {
2633         int available;
2634
2635         if (!p->ss7) {
2636                 /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
2637                 return 0;
2638         }
2639
2640         /* Only have to deal with the linkset lock. */
2641         ast_mutex_lock(&p->ss7->lock);
2642         available = sig_ss7_is_chan_available(p);
2643         if (available) {
2644                 p->ss7call = isup_new_call(p->ss7->ss7, p->cic, p->dpc, 1);
2645                 if (!p->ss7call) {
2646                         ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2647                         available = 0;
2648                 } else {
2649                         p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
2650                 }
2651         }
2652         ast_mutex_unlock(&p->ss7->lock);
2653
2654         return available;
2655 }
2656
2657 static unsigned char cid_pres2ss7pres(int cid_pres)
2658 {
2659          return (cid_pres >> 5) & 0x03;
2660 }
2661
2662 static unsigned char cid_pres2ss7screen(int cid_pres)
2663 {
2664         return cid_pres & 0x03;
2665 }
2666
2667 static void ss7_connected_line_update(struct sig_ss7_chan *p, struct ast_party_connected_line *connected)
2668 {
2669         int connected_strip = 0;
2670         char connected_nai;
2671         unsigned char connected_pres;
2672         unsigned char connected_screen;
2673         const char *connected_num;
2674
2675         if (!connected->id.number.valid) {
2676                 return;
2677         }
2678
2679         connected_num = S_OR(connected->id.number.str, "");
2680         if (p->ss7->called_nai ==  SS7_NAI_DYNAMIC) {
2681                 connected_strip = ss7_parse_prefix(p, connected_num, &connected_nai);
2682         } else {
2683                 connected_nai = p->ss7->called_nai;
2684         }
2685
2686         connected_pres = cid_pres2ss7pres(connected->id.number.presentation);
2687         connected_screen = cid_pres2ss7screen(connected->id.number.presentation);
2688
2689         isup_set_connected(p->ss7call, connected_num + connected_strip, connected_nai, connected_pres, connected_screen);
2690 }
2691
2692 static unsigned char ss7_redirect_reason(struct sig_ss7_chan *p, struct ast_party_redirecting *redirecting, int orig)
2693 {
2694         int reason = (orig) ? redirecting->orig_reason.code : redirecting->reason.code;
2695
2696         switch (reason) {
2697         case AST_REDIRECTING_REASON_USER_BUSY:
2698                 return SS7_REDIRECTING_REASON_USER_BUSY;
2699         case AST_REDIRECTING_REASON_NO_ANSWER:
2700                 return SS7_REDIRECTING_REASON_NO_ANSWER;
2701         case AST_REDIRECTING_REASON_UNCONDITIONAL:
2702                 return SS7_REDIRECTING_REASON_UNCONDITIONAL;
2703         }
2704
2705         if (orig || reason == AST_REDIRECTING_REASON_UNKNOWN) {
2706                 return SS7_REDIRECTING_REASON_UNKNOWN;
2707         }
2708
2709         if (reason == AST_REDIRECTING_REASON_UNAVAILABLE) {
2710                 return SS7_REDIRECTING_REASON_UNAVAILABLE;
2711         }
2712
2713         if (reason == AST_REDIRECTING_REASON_DEFLECTION) {
2714                 if (p->call_level > SIG_SS7_CALL_LEVEL_PROCEEDING) {
2715                         return SS7_REDIRECTING_REASON_DEFLECTION_DURING_ALERTING;
2716                 }
2717                 return SS7_REDIRECTING_REASON_DEFLECTION_IMMEDIATE_RESPONSE;
2718         }
2719
2720         return SS7_REDIRECTING_REASON_UNKNOWN;
2721 }
2722
2723 static unsigned char ss7_redirect_info_ind(struct ast_channel *ast)
2724 {
2725         const char *redirect_info_ind;
2726         struct ast_party_redirecting *redirecting = ast_channel_redirecting(ast);
2727
2728         redirect_info_ind = pbx_builtin_getvar_helper(ast, "SS7_REDIRECT_INFO_IND");
2729         if (!ast_strlen_zero(redirect_info_ind)) {
2730                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_PRES_ALLOWED")) {
2731                         return SS7_INDICATION_REROUTED_PRES_ALLOWED;
2732                 }
2733                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_INFO_RESTRICTED")) {
2734                         return SS7_INDICATION_REROUTED_INFO_RESTRICTED;
2735                 }
2736                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_PRES_ALLOWED")) {
2737                         return SS7_INDICATION_DIVERTED_PRES_ALLOWED;
2738                 }
2739                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_INFO_RESTRICTED")) {
2740                         return SS7_INDICATION_DIVERTED_INFO_RESTRICTED;
2741                 }
2742                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_PRES_RESTRICTED")) {
2743                         return SS7_INDICATION_REROUTED_PRES_RESTRICTED;
2744                 }
2745                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_PRES_RESTRICTED")) {
2746                         return SS7_INDICATION_DIVERTED_PRES_RESTRICTED;
2747                 }
2748                 if (!strcasecmp(redirect_info_ind, "SPARE")) {
2749                         return SS7_INDICATION_SPARE;
2750                 }
2751                 return SS7_INDICATION_NO_REDIRECTION;
2752         }
2753
2754         if (redirecting->reason.code == AST_REDIRECTING_REASON_DEFLECTION) {
2755                 if ((redirecting->to.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2756                         if ((redirecting->orig.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2757                                 return SS7_INDICATION_DIVERTED_PRES_ALLOWED;
2758                         }
2759                         return SS7_INDICATION_DIVERTED_PRES_RESTRICTED;
2760                 }
2761                 return SS7_INDICATION_DIVERTED_INFO_RESTRICTED;
2762         }
2763
2764         if ((redirecting->to.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2765                 if ((redirecting->orig.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2766                         return SS7_INDICATION_REROUTED_PRES_ALLOWED;
2767                 }
2768                 return SS7_INDICATION_REROUTED_PRES_RESTRICTED;
2769         }
2770         return SS7_INDICATION_REROUTED_INFO_RESTRICTED;
2771 }
2772
2773 static void ss7_redirecting_update(struct sig_ss7_chan *p, struct ast_channel *ast)
2774 {
2775         int num_nai_strip = 0;
2776         struct ast_party_redirecting *redirecting = ast_channel_redirecting(ast);
2777
2778         if (!redirecting->count) {
2779                 return;
2780         }
2781
2782         isup_set_redirect_counter(p->ss7call, redirecting->count);
2783
2784         if (redirecting->orig.number.valid) {
2785                 char ss7_orig_called_nai = p->ss7->called_nai;
2786                 const char *ss7_orig_called_num = S_OR(redirecting->orig.number.str, "");
2787
2788                 if (ss7_orig_called_nai == SS7_NAI_DYNAMIC) {
2789                         num_nai_strip = ss7_parse_prefix(p, ss7_orig_called_num, &ss7_orig_called_nai);
2790                 } else {
2791                         num_nai_strip = 0;
2792                 }
2793                 isup_set_orig_called_num(p->ss7call, ss7_orig_called_num + num_nai_strip,
2794                         ss7_orig_called_nai,
2795                         cid_pres2ss7pres(redirecting->orig.number.presentation),
2796                         cid_pres2ss7screen(redirecting->orig.number.presentation));
2797         }
2798
2799         if (redirecting->from.number.valid) {
2800                 char ss7_redirecting_num_nai = p->ss7->calling_nai;
2801                 const char *redirecting_number = S_OR(redirecting->from.number.str, "");
2802
2803                 if (ss7_redirecting_num_nai == SS7_NAI_DYNAMIC) {
2804                         num_nai_strip = ss7_parse_prefix(p, redirecting_number, &ss7_redirecting_num_nai);
2805                 } else {
2806                         num_nai_strip = 0;
2807                 }
2808
2809                 isup_set_redirecting_number(p->ss7call, redirecting_number + num_nai_strip,
2810                         ss7_redirecting_num_nai,
2811                         cid_pres2ss7pres(redirecting->from.number.presentation),
2812                         cid_pres2ss7screen(redirecting->from.number.presentation));
2813         }
2814
2815         isup_set_redirection_info(p->ss7call, ss7_redirect_info_ind(ast),
2816                 ss7_redirect_reason(p, ast_channel_redirecting(ast), 1),
2817                 redirecting->count, ss7_redirect_reason(p, ast_channel_redirecting(ast), 0));
2818 }
2819
2820 /*!
2821  * \brief Dial out using the specified SS7 channel.
2822  * \since 1.8
2823  *
2824  * \param p Signaling private structure pointer.
2825  * \param ast Asterisk channel structure pointer.
2826  * \param rdest Dialstring.
2827  *
2828  * \retval 0 on success.
2829  * \retval -1 on error.
2830  */
2831 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
2832 {
2833         char ss7_called_nai;
2834         int called_nai_strip;
2835         char ss7_calling_nai;
2836         int calling_nai_strip;
2837         const char *col_req = NULL;
2838         const char *ss7_cug_indicator_str;
2839         const char *ss7_cug_interlock_ni;
2840         const char *ss7_cug_interlock_code;
2841         const char *ss7_interworking_indicator;
2842         const char *ss7_forward_indicator_pmbits;
2843         unsigned char ss7_cug_indicator;
2844         const char *charge_str = NULL;
2845         const char *gen_address = NULL;
2846         const char *gen_digits = NULL;
2847         const char *gen_dig_type = NULL;
2848         const char *gen_dig_scheme = NULL;
2849         const char *gen_name = NULL;
2850         const char *jip_digits = NULL;
2851         const char *lspi_ident = NULL;
2852         const char *rlt_flag = NULL;
2853         const char *call_ref_id = NULL;
2854         const char *call_ref_pc = NULL;
2855         const char *send_far = NULL;
2856         const char *tmr = NULL;
2857         char *c;
2858         char *l;
2859         char dest[256];
2860
2861         ast_copy_string(dest, rdest, sizeof(dest));
2862
2863         c = strchr(dest, '/');
2864         if (c) {
2865                 c++;
2866         } else {
2867                 c = "";
2868         }
2869         if (strlen(c) < p->stripmsd) {
2870                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2871                 return -1;
2872         }
2873
2874         if (!p->hidecallerid) {
2875                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
2876         } else {
2877                 l = NULL;
2878         }
2879