BuildSystem: Enable Advanced Linux Sound Architecture (ALSA) in NetBSD.
[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         ast_callid callid = 0;
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 The callid bound to the channel which has also been bound to threadstorage
1319  *         if it exists. If this returns non-zero, the threadstorage should be unbound
1320  *         before the while loop wraps in ss7_linkset.
1321  */
1322 static ast_callid func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
1323 {
1324         ast_callid callid = 0;
1325         sig_ss7_lock_owner(linkset, chanpos);
1326         if (linkset->pvts[chanpos]->owner) {
1327                 callid = ast_channel_callid(linkset->pvts[chanpos]->owner);
1328                 ast_channel_unlock(linkset->pvts[chanpos]->owner);
1329                 if (callid) {
1330                         ast_callid_threadassoc_add(callid);
1331                 }
1332         }
1333
1334         return callid;
1335 }
1336
1337 /*!
1338  * \internal
1339  * \brief Proceed with the call based on the extension matching status
1340  * is matching in the dialplan.
1341  * \since 11.0
1342  *
1343  * \param linkset ss7 span control structure.
1344  * \param p Signaling private structure pointer.
1345  * \param e Event causing the match.
1346  *
1347  * \note Assumes the linkset->lock is already obtained.
1348  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
1349  *
1350  * \return Nothing.
1351  */
1352 static void ss7_match_extension(struct sig_ss7_linkset *linkset, struct sig_ss7_chan *p, ss7_event *e)
1353 {
1354         ast_verb(3, "SS7 exten: %s complete: %d\n", p->exten, p->called_complete);
1355
1356         if (!p->called_complete
1357                 && linkset->type == SS7_ITU /* ANSI does not support overlap dialing. */
1358                 && ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)
1359                 && !isup_start_digittimeout(linkset->ss7, p->ss7call)) {
1360                 /* Wait for more digits. */
1361                 return;
1362         }
1363         if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1364                 /* DNID is complete */
1365                 p->called_complete = 1;
1366                 sig_ss7_set_dnid(p, p->exten);
1367
1368                 /* If COT successful start call! */
1369                 if ((e->e == ISUP_EVENT_IAM)
1370                         ? !(e->iam.cot_check_required || e->iam.cot_performed_on_previous_cic)
1371                         : (!(e->sam.cot_check_required || e->sam.cot_performed_on_previous_cic) || e->sam.cot_check_passed)) {
1372                         ss7_start_call(p, linkset);
1373                 }
1374                 return;
1375         }
1376
1377         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1378         isup_rel(linkset->ss7, (e->e == ISUP_EVENT_IAM) ? e->iam.call : e->sam.call, AST_CAUSE_UNALLOCATED);
1379 }
1380
1381 /* This is a thread per linkset that handles all received events from libss7. */
1382 void *ss7_linkset(void *data)
1383 {
1384         int res, i;
1385         struct timeval *next = NULL, tv;
1386         struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
1387         struct ss7 *ss7 = linkset->ss7;
1388         ss7_event *e = NULL;
1389         struct sig_ss7_chan *p;
1390         struct pollfd pollers[SIG_SS7_NUM_DCHANS];
1391         unsigned char mb_state[255];
1392         int nextms;
1393
1394 #define SS7_MAX_POLL    60000   /* Maximum poll time in ms. */
1395
1396         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1397
1398         ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
1399         ast_mutex_lock(&linkset->lock);
1400         ss7_start(ss7);
1401         ast_mutex_unlock(&linkset->lock);
1402
1403         for (;;) {
1404                 ast_mutex_lock(&linkset->lock);
1405                 if ((next = ss7_schedule_next(ss7))) {
1406                         tv = ast_tvnow();
1407                         tv.tv_sec = next->tv_sec - tv.tv_sec;
1408                         tv.tv_usec = next->tv_usec - tv.tv_usec;
1409                         if (tv.tv_usec < 0) {
1410                                 tv.tv_usec += 1000000;
1411                                 tv.tv_sec -= 1;
1412                         }
1413                         if (tv.tv_sec < 0) {
1414                                 tv.tv_sec = 0;
1415                                 tv.tv_usec = 0;
1416                         }
1417                         nextms = tv.tv_sec * 1000;
1418                         nextms += tv.tv_usec / 1000;
1419                         if (SS7_MAX_POLL < nextms) {
1420                                 nextms = SS7_MAX_POLL;
1421                         }
1422                 } else {
1423                         nextms = SS7_MAX_POLL;
1424                 }
1425
1426                 for (i = 0; i < linkset->numsigchans; i++) {
1427                         pollers[i].fd = linkset->fds[i];
1428                         pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
1429                         pollers[i].revents = 0;
1430                 }
1431                 ast_mutex_unlock(&linkset->lock);
1432
1433                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1434                 pthread_testcancel();
1435                 res = poll(pollers, linkset->numsigchans, nextms);
1436                 pthread_testcancel();
1437                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1438
1439                 ast_mutex_lock(&linkset->lock);
1440                 if ((res < 0) && (errno != EINTR)) {
1441                         ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
1442                 } else if (!res) {
1443                         ss7_schedule_run(ss7);
1444                 }
1445
1446                 for (i = 0; i < linkset->numsigchans; i++) {
1447                         if (pollers[i].revents & POLLPRI) {
1448                                 sig_ss7_handle_link_exception(linkset, i);
1449                         }
1450                         if (pollers[i].revents & POLLIN) {
1451                                 res = ss7_read(ss7, pollers[i].fd);
1452                         }
1453                         if (pollers[i].revents & POLLOUT) {
1454                                 res = ss7_write(ss7, pollers[i].fd);
1455                                 if (res < 0) {
1456                                         ast_debug(1, "Error in write %s\n", strerror(errno));
1457                                 }
1458                         }
1459                 }
1460
1461                 while ((e = ss7_check_event(ss7))) {
1462                         ast_callid callid = 0;
1463                         int chanpos = -1;
1464                         char cause_str[30];
1465
1466                         if (linkset->debug) {
1467                                 ast_verbose("Linkset %d: Processing event: %s\n",
1468                                         linkset->span, ss7_event2str(e->e));
1469                         }
1470
1471                         switch (e->e) {
1472                         case SS7_EVENT_UP:
1473                                 if (linkset->state != LINKSET_STATE_UP) {
1474                                         ast_verb(1, "--- SS7 Up ---\n");
1475                                         ss7_reset_linkset(linkset);
1476                                 }
1477                                 linkset->state = LINKSET_STATE_UP;
1478                                 break;
1479                         case SS7_EVENT_DOWN:
1480                                 ast_verb(1, "--- SS7 Down ---\n");
1481                                 linkset->state = LINKSET_STATE_DOWN;
1482                                 for (i = 0; i < linkset->numchans; i++) {
1483                                         p = linkset->pvts[i];
1484                                         if (p) {
1485                                                 sig_ss7_set_inservice(p, 0);
1486                                                 if (linkset->flags & LINKSET_FLAG_INITIALHWBLO) {
1487                                                         sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_HARDWARE);
1488                                                 }
1489                                         }
1490                                 }
1491                                 break;
1492                         case MTP2_LINK_UP:
1493                                 ast_verb(1, "MTP2 link up (SLC %d)\n", e->gen.data);
1494                                 break;
1495                         case MTP2_LINK_DOWN:
1496                                 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
1497                                 break;
1498                         case ISUP_EVENT_CPG:
1499                                 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
1500                                 if (chanpos < 0) {
1501                                         isup_free_call(ss7, e->cpg.call);
1502                                         break;
1503                                 }
1504                                 p = linkset->pvts[chanpos];
1505                                 sig_ss7_lock_private(p);
1506                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1507
1508                                 switch (e->cpg.event) {
1509                                 case CPG_EVENT_ALERTING:
1510                                         if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
1511                                                 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1512                                         }
1513                                         sig_ss7_lock_owner(linkset, chanpos);
1514                                         if (p->owner) {
1515                                                 ast_setstate(p->owner, AST_STATE_RINGING);
1516                                                 if (!ast_strlen_zero(e->cpg.connected_num)) {
1517                                                         struct ast_party_connected_line ast_connected;
1518                                                         char connected_num[AST_MAX_EXTENSION];
1519
1520                                                         ast_party_connected_line_init(&ast_connected);
1521                                                         ast_connected.id.number.presentation =
1522                                                                 ss7_pres_scr2cid_pres(e->cpg.connected_presentation_ind,
1523                                                                 e->cpg.connected_screening_ind);
1524                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
1525                                                                 linkset, e->cpg.connected_num, e->cpg.connected_nai);
1526                                                         ast_connected.id.number.str = ast_strdup(connected_num);
1527                                                         ast_connected.id.number.valid = 1;
1528                                                         ast_channel_queue_connected_line_update(p->owner, &ast_connected, NULL);
1529                                                         ast_party_connected_line_free(&ast_connected);
1530                                                 }
1531                                                 ast_channel_unlock(p->owner);
1532                                         }
1533                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
1534                                         break;
1535                                 case CPG_EVENT_PROGRESS:
1536                                 case CPG_EVENT_INBANDINFO:
1537                                         {
1538                                                 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
1539                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
1540                                                 p->progress = 1;
1541                                                 sig_ss7_set_dialing(p, 0);
1542                                                 sig_ss7_open_media(p);
1543                                         }
1544                                         break;
1545                                 default:
1546                                         ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
1547                                         break;
1548                                 }
1549
1550                                 sig_ss7_unlock_private(p);
1551                                 break;
1552                         case ISUP_EVENT_RSC:
1553                                 ast_verb(1, "Resetting CIC %d\n", e->rsc.cic);
1554                                 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
1555                                 if (chanpos < 0) {
1556                                         isup_free_call(ss7, e->rsc.call);
1557                                         break;
1558                                 }
1559                                 p = linkset->pvts[chanpos];
1560                                 sig_ss7_lock_private(p);
1561                                 p->ss7call = e->rsc.call;
1562                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1563                                 sig_ss7_set_inservice(p, 1);
1564                                 sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1565
1566                                 if (p->locallyblocked & SS7_BLOCKED_MAINTENANCE) {
1567                                         isup_blo(ss7, e->rsc.call);
1568                                 } else if (p->locallyblocked & SS7_BLOCKED_HARDWARE) {
1569                                         sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_HARDWARE);
1570                                 }
1571
1572                                 isup_set_call_dpc(e->rsc.call, p->dpc);
1573                                 sig_ss7_lock_owner(linkset, chanpos);
1574                                 if (p->owner) {
1575                                         p->do_hangup = SS7_HANGUP_SEND_RLC;
1576                                         if (!(e->rsc.got_sent_msg & ISUP_SENT_IAM)) {
1577                                                 /* Q.784 6.2.3 */
1578                                                 ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CLEARING);
1579                                         } else {
1580                                                 ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
1581                                         }
1582
1583                                         ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_RSC", AST_CAUSE_INTERWORKING);
1584
1585                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1586                                         ast_channel_unlock(p->owner);
1587                                 } else {
1588                                         isup_rlc(ss7, e->rsc.call);
1589                                         p->ss7call = isup_free_call_if_clear(ss7, e->rsc.call);
1590                                 }
1591                                 /* End the loopback if we have one */
1592                                 sig_ss7_loopback(p, 0);
1593
1594                                 sig_ss7_unlock_private(p);
1595                                 break;
1596                         case ISUP_EVENT_GRS:
1597                                 if (!sig_ss7_find_cic_range(linkset, e->grs.startcic, e->grs.endcic,
1598                                         e->grs.opc)) {
1599                                         ast_log(LOG_WARNING, "GRS on unconfigured range CIC %d - %d PC %d\n",
1600                                                 e->grs.startcic, e->grs.endcic, e->grs.opc);
1601                                         chanpos = sig_ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
1602                                         if (chanpos < 0) {
1603                                                 isup_free_call(ss7, e->grs.call);
1604                                                 break;
1605                                         }
1606                                         p = linkset->pvts[chanpos];
1607                                         sig_ss7_lock_private(p);
1608                                         p->ss7call = isup_free_call_if_clear(ss7, e->grs.call);
1609                                         sig_ss7_unlock_private(p);
1610                                         break;
1611                                 }
1612
1613                                 /* Leave startcic last to collect all cics mb_state */
1614                                 for (i = e->grs.endcic - e->grs.startcic; 0 <= i; --i) {
1615                                         /*
1616                                          * We are guaranteed to find chanpos because
1617                                          * sig_ss7_find_cic_range() includes it.
1618                                          */
1619                                         chanpos = sig_ss7_find_cic(linkset, e->grs.startcic + i, e->grs.opc);
1620                                         p = linkset->pvts[chanpos];
1621                                         sig_ss7_lock_private(p);
1622
1623                                         if (p->locallyblocked & SS7_BLOCKED_MAINTENANCE) {
1624                                                 mb_state[i] = 1;
1625                                         } else {
1626                                                 mb_state[i] = 0;
1627                                                 sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_HARDWARE);
1628                                         }
1629
1630                                         sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1631
1632                                         if (!i) {
1633                                                 p->ss7call = e->grs.call;
1634                                                 isup_gra(ss7, p->ss7call, e->grs.endcic, mb_state);
1635                                         }
1636
1637                                         sig_ss7_lock_owner(linkset, chanpos);
1638                                         if (p->owner) {
1639                                                 ast_channel_softhangup_internal_flag_add(p->owner, AST_SOFTHANGUP_DEV);
1640                                                 if (ast_channel_state(p->owner) == AST_STATE_DIALING
1641                                                         && linkset->pvts[i]->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
1642                                                         ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
1643                                                 } else {
1644                                                         ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CLEARING);
1645                                                 }
1646                                                 p->do_hangup = SS7_HANGUP_FREE_CALL;
1647                                                 ast_channel_unlock(p->owner);
1648                                         } else if (!i) {
1649                                                 p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1650                                         } else if (p->ss7call) {
1651                                                 /* clear any other session */
1652                                                 isup_free_call(ss7, p->ss7call);
1653                                                 p->ss7call = NULL;
1654                                         }
1655                                         sig_ss7_set_inservice(p, 1);
1656                                         sig_ss7_unlock_private(p);
1657                                 }
1658                                 break;
1659                         case ISUP_EVENT_CQM:
1660                                 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n",
1661                                                         e->cqm.startcic, e->cqm.endcic);
1662                                 ss7_handle_cqm(linkset, e);
1663                                 break;
1664                         case ISUP_EVENT_GRA:
1665                                 if (!sig_ss7_find_cic_range(linkset, e->gra.startcic,
1666                                                         e->gra.endcic, e->gra.opc)) {   /* Never will be true */
1667                                         ast_log(LOG_WARNING, "GRA on unconfigured range CIC %d - %d PC %d\n",
1668                                                         e->gra.startcic, e->gra.endcic, e->gra.opc);
1669                                         isup_free_call(ss7, e->gra.call);
1670                                         break;
1671                                 }
1672                                 ast_verb(1, "Got reset acknowledgement from CIC %d to %d DPC: %d\n",
1673                                         e->gra.startcic, e->gra.endcic, e->gra.opc);
1674                                 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc,
1675                                         e->gra.status, 1, 1, SS7_BLOCKED_MAINTENANCE);
1676                                 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
1677
1678                                 chanpos = sig_ss7_find_cic(linkset, e->gra.startcic, e->gra.opc);
1679                                 if (chanpos < 0) {
1680                                         isup_free_call(ss7, e->gra.call);
1681                                         break;
1682                                 }
1683
1684                                 p = linkset->pvts[chanpos];
1685                                 sig_ss7_lock_private(p);
1686
1687                                 /* we may send a CBD with GRS! */
1688                                 p->ss7call = isup_free_call_if_clear(ss7, e->gra.call);
1689                                 sig_ss7_unlock_private(p);
1690                                 break;
1691                         case ISUP_EVENT_SAM:
1692                                 chanpos = ss7_find_cic_gripe(linkset, e->sam.cic, e->sam.opc, "SAM");
1693                                 if (chanpos < 0) {
1694                                         isup_free_call(ss7, e->sam.call);
1695                                         break;
1696                                 }
1697                                 p = linkset->pvts[chanpos];
1698                                 sig_ss7_lock_private(p);
1699                                 sig_ss7_lock_owner(linkset, chanpos);
1700                                 if (p->owner) {
1701                                         ast_log(LOG_WARNING, "SAM on CIC %d PC %d already have call\n", e->sam.cic, e->sam.opc);
1702                                         ast_channel_unlock(p->owner);
1703                                         sig_ss7_unlock_private(p);
1704                                         break;
1705                                 }
1706                                 p->called_complete = 0;
1707                                 if (!ast_strlen_zero(e->sam.called_party_num)) {
1708                                         char *st;
1709                                         strncat(p->exten, e->sam.called_party_num, sizeof(p->exten) - strlen(p->exten) - 1);
1710                                         st = strchr(p->exten, '#');
1711                                         if (st) {
1712                                                 *st = '\0';
1713                                                 p->called_complete = 1;
1714                                         }
1715                                         ss7_match_extension(linkset, p, e);
1716                                 }
1717                                 sig_ss7_unlock_private(p);
1718                                 break;
1719                         case ISUP_EVENT_IAM:
1720                                 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);
1721                                 chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
1722                                 if (chanpos < 0) {
1723                                         isup_free_call(ss7, e->iam.call);
1724                                         break;
1725                                 }
1726                                 p = linkset->pvts[chanpos];
1727                                 sig_ss7_lock_private(p);
1728                                 /*
1729                                  * The channel should be idle and not have an owner at this point since we
1730                                  * are in the process of creating an owner for it.
1731                                  */
1732                                 ast_assert(!p->owner && p->call_level == SIG_SS7_CALL_LEVEL_IDLE);
1733
1734                                 if (p->remotelyblocked) {
1735                                         ast_log(LOG_NOTICE, "Got IAM on remotely blocked CIC %d DPC %d remove blocking\n", e->iam.cic, e->iam.opc);
1736                                         sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
1737                                         sig_ss7_set_inservice(p, 1);
1738                                 }
1739
1740                                 if (!sig_ss7_is_chan_available(p)) {
1741                                         /* Circuit is likely blocked or in alarm. */
1742                                         isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1743                                         if (p->locallyblocked) {
1744                                                 isup_clear_callflags(ss7, e->iam.call, ISUP_GOT_IAM);
1745                                                 p->ss7call = isup_free_call_if_clear(ss7, e->iam.call);
1746                                                 ast_log(LOG_WARNING, "Got IAM on locally blocked CIC %d DPC %d, ignore\n", e->iam.cic, e->iam.opc);
1747                                         }
1748                                         sig_ss7_unlock_private(p);
1749                                         break;
1750                                 }
1751
1752                                 /* Mark channel as in use so no outgoing call will steal it. */
1753                                 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1754                                 p->ss7call = e->iam.call;
1755
1756                                 isup_set_call_dpc(p->ss7call, p->dpc);
1757
1758                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
1759                                         ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
1760                                         p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1761                                 } else {
1762                                         p->cid_num[0] = 0;
1763                                         if (e->iam.presentation_ind) {
1764                                                 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1765                                         }
1766                                 }
1767
1768                                 p->called_complete = 0;
1769                                 if (p->immediate) {
1770                                         p->exten[0] = 's';
1771                                         p->exten[1] = '\0';
1772                                 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
1773                                         char *st;
1774                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
1775                                         st = strchr(p->exten, '#');
1776                                         if (st) {
1777                                                 *st = '\0';
1778                                                 p->called_complete = 1;
1779                                         }
1780                                 } else {
1781                                         p->exten[0] = '\0';
1782                                 }
1783
1784                                 p->cid_ani[0] = '\0';
1785                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name))) {
1786                                         ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
1787                                 } else {
1788                                         p->cid_name[0] = '\0';
1789                                 }
1790
1791                                 p->cid_ani2 = e->iam.oli_ani2;
1792                                 p->cid_ton = 0;
1793                                 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
1794                                 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
1795                                 p->gen_add_type = e->iam.gen_add_type;
1796                                 p->gen_add_nai = e->iam.gen_add_nai;
1797                                 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
1798                                 p->gen_add_num_plan = e->iam.gen_add_num_plan;
1799                                 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
1800                                 p->gen_dig_type = e->iam.gen_dig_type;
1801                                 p->gen_dig_scheme = e->iam.gen_dig_scheme;
1802                                 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
1803                                 if (!ast_strlen_zero(e->iam.orig_called_num)) {
1804                                         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);
1805                                         p->orig_called_presentation = ss7_pres_scr2cid_pres(e->iam.orig_called_pres_ind, e->iam.orig_called_screening_ind);
1806                                 }
1807                                 if (!ast_strlen_zero(e->iam.redirecting_num)) {
1808                                         ss7_apply_plan_to_number(p->redirecting_num, sizeof(p->redirecting_num), linkset, e->iam.redirecting_num, e->iam.redirecting_num_nai);
1809                                         p->redirecting_presentation = ss7_pres_scr2cid_pres(e->iam.redirecting_num_presentation_ind, e->iam.redirecting_num_screening_ind);
1810                                 }
1811                                 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
1812                                 p->calling_party_cat = e->iam.calling_party_cat;
1813                                 p->redirect_counter = e->iam.redirect_counter;
1814                                 p->redirect_info = e->iam.redirect_info;
1815                                 p->redirect_info_ind = e->iam.redirect_info_ind;
1816                                 p->redirect_info_orig_reas = e->iam.redirect_info_orig_reas;
1817                                 p->redirect_info_counter = e->iam.redirect_info_counter;
1818                                 if (p->redirect_info_counter && !p->redirect_counter) {
1819                                         p->redirect_counter = p->redirect_info_counter;
1820                                 }
1821                                 p->redirect_info_reas = e->iam.redirect_info_reas;
1822                                 p->cug_indicator = e->iam.cug_indicator;
1823                                 p->cug_interlock_code = e->iam.cug_interlock_code;
1824                                 ast_copy_string(p->cug_interlock_ni, e->iam.cug_interlock_ni, sizeof(p->cug_interlock_ni));
1825
1826                                 if (e->iam.cot_check_required) {
1827                                         sig_ss7_loopback(p, 1);
1828                                 }
1829
1830                                 p->echocontrol_ind = e->iam.echocontrol_ind;
1831                                 sig_ss7_set_caller_id(p);
1832                                 ss7_match_extension(linkset, p, e);
1833                                 sig_ss7_unlock_private(p);
1834
1835                                 if (e->iam.cot_performed_on_previous_cic) {
1836                                         chanpos = sig_ss7_find_cic(linkset, (e->iam.cic - 1), e->iam.opc);
1837                                         if (chanpos < 0) {
1838                                                 /* some stupid switch do this */
1839                                                 ast_verb(1, "COT request on previous nonexistent CIC %d in IAM PC %d\n", (e->iam.cic - 1), e->iam.opc);
1840                                                 break;
1841                                         }
1842                                         ast_verb(1, "COT request on previous CIC %d in IAM PC %d\n", (e->iam.cic - 1), e->iam.opc);
1843                                         p = linkset->pvts[chanpos];
1844                                         sig_ss7_lock_private(p);
1845                                         if (sig_ss7_is_chan_available(p)) {
1846                                                 sig_ss7_set_inservice(p, 0);    /* to prevent to use this circuit */
1847                                                 sig_ss7_loopback(p, 1);
1848                                         } /* If already have a call don't loop */
1849                                         sig_ss7_unlock_private(p);
1850                                 }
1851                                 break;
1852                         case ISUP_EVENT_DIGITTIMEOUT:
1853                                 chanpos = ss7_find_cic_gripe(linkset, e->digittimeout.cic, e->digittimeout.opc, "DIGITTIMEOUT");
1854                                 if (chanpos < 0) {
1855                                         isup_free_call(ss7, e->digittimeout.call);
1856                                         break;
1857                                 }
1858                                 p = linkset->pvts[chanpos];
1859                                 sig_ss7_lock_private(p);
1860                                 ast_debug(1, "Digittimeout on CIC: %d PC: %d\n", e->digittimeout.cic, e->digittimeout.opc);
1861                                 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1862                                         /* DNID is complete */
1863                                         p->called_complete = 1;
1864                                         sig_ss7_set_dnid(p, p->exten);
1865
1866                                         /* If COT successful start call! */
1867                                         if (!(e->digittimeout.cot_check_required || e->digittimeout.cot_performed_on_previous_cic) || e->digittimeout.cot_check_passed) {
1868                                                 ss7_start_call(p, linkset);
1869                                         }
1870                                 } else {
1871                                         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1872                                         isup_rel(linkset->ss7, e->digittimeout.call, AST_CAUSE_UNALLOCATED);
1873                                 }
1874                                 sig_ss7_unlock_private(p);
1875                                 break;
1876                         case ISUP_EVENT_COT:
1877                                 if (e->cot.cot_performed_on_previous_cic) {
1878                                         chanpos = sig_ss7_find_cic(linkset, (e->cot.cic - 1), e->cot.opc);
1879                                         /* some stupid switches do this!!! */
1880                                         if (-1 < chanpos) {
1881                                                 p = linkset->pvts[chanpos];
1882                                                 sig_ss7_lock_private(p);
1883                                                 sig_ss7_set_inservice(p, 1);
1884                                                 sig_ss7_loopback(p, 0);
1885                                                 sig_ss7_unlock_private(p);;
1886                                                 ast_verb(1, "Loop turned off on CIC: %d PC: %d\n",  (e->cot.cic - 1), e->cot.opc);
1887                                         }
1888                                 }
1889
1890                                 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
1891                                 if (chanpos < 0) {
1892                                         isup_free_call(ss7, e->cot.call);
1893                                         break;
1894                                 }
1895                                 p = linkset->pvts[chanpos];
1896
1897                                 sig_ss7_lock_private(p);
1898                                 p->ss7call = e->cot.call;
1899
1900                                 if (p->loopedback) {
1901                                         sig_ss7_loopback(p, 0);
1902                                         ast_verb(1, "Loop turned off on CIC: %d PC: %d\n",  e->cot.cic, e->cot.opc);
1903                                 }
1904
1905                                 /* Don't start call if we didn't get IAM or COT failed! */
1906                                 if ((e->cot.got_sent_msg & ISUP_GOT_IAM) && e->cot.passed && p->called_complete) {
1907                                         ss7_start_call(p, linkset);
1908                                 }
1909
1910                                 p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1911                                 sig_ss7_unlock_private(p);
1912                                 break;
1913                         case ISUP_EVENT_CCR:
1914                                 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
1915                                 chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
1916                                 if (chanpos < 0) {
1917                                         isup_free_call(ss7, e->ccr.call);
1918                                         break;
1919                                 }
1920
1921                                 p = linkset->pvts[chanpos];
1922
1923                                 sig_ss7_lock_private(p);
1924                                 p->ss7call = e->ccr.call;
1925                                 sig_ss7_loopback(p, 1);
1926                                 if (linkset->type == SS7_ANSI) {
1927                                         isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
1928                                 }
1929                                 sig_ss7_unlock_private(p);
1930                                 break;
1931                         case ISUP_EVENT_CVT:
1932                                 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
1933                                 chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
1934                                 if (chanpos < 0) {
1935                                         isup_free_call(ss7, e->cvt.call);
1936                                         break;
1937                                 }
1938
1939                                 p = linkset->pvts[chanpos];
1940
1941                                 sig_ss7_lock_private(p);
1942                                 p->ss7call = e->cvt.call;
1943                                 sig_ss7_loopback(p, 1);
1944                                 if (!p->owner) {
1945                                         p->ss7call = isup_free_call_if_clear(ss7, e->cvt.call);
1946                                 }
1947                                 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
1948                                 sig_ss7_unlock_private(p);
1949                                 break;
1950                         case ISUP_EVENT_REL:
1951                                 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
1952                                 if (chanpos < 0) {
1953                                         isup_free_call(ss7, e->rel.call);
1954                                         break;
1955                                 }
1956                                 p = linkset->pvts[chanpos];
1957                                 sig_ss7_lock_private(p);
1958                                 p->ss7call = e->rel.call;
1959                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1960                                 sig_ss7_lock_owner(linkset, chanpos);
1961                                 if (p->owner) {
1962                                         snprintf(cause_str, sizeof(cause_str), "SS7 ISUP_EVENT_REL (%d)", e->rel.cause);
1963                                         ss7_queue_pvt_cause_data(p->owner, cause_str, e->rel.cause);
1964
1965                                         ast_channel_hangupcause_set(p->owner, e->rel.cause);
1966                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1967                                         p->do_hangup = SS7_HANGUP_SEND_RLC;
1968                                         ast_channel_unlock(p->owner);
1969                                 } else {
1970                                         ast_verb(1, "REL on CIC %d DPC %d without owner!\n", p->cic, p->dpc);
1971                                         isup_rlc(ss7, p->ss7call);
1972                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
1973                                 }
1974
1975                                 /* End the loopback if we have one */
1976                                 sig_ss7_loopback(p, 0);
1977
1978                                 /* the rel is not complete here!!! */
1979                                 sig_ss7_unlock_private(p);
1980                                 break;
1981                         case ISUP_EVENT_ACM:
1982                                 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
1983                                 if (chanpos < 0) {
1984                                         isup_free_call(ss7, e->acm.call);
1985                                         break;
1986                                 }
1987
1988                                 p = linkset->pvts[chanpos];
1989
1990                                 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
1991
1992                                 if (e->acm.call_ref_ident > 0) {
1993                                         p->rlt = 1; /* Setting it but not using it here*/
1994                                 }
1995
1996                                 sig_ss7_lock_private(p);
1997                                 p->ss7call = e->acm.call;
1998                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1999                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
2000                                 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
2001                                         p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
2002                                 }
2003                                 sig_ss7_set_dialing(p, 0);
2004                                 /* Send alerting if subscriber is free */
2005                                 if (e->acm.called_party_status_ind == 1) {
2006                                         if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
2007                                                 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
2008                                         }
2009                                         sig_ss7_lock_owner(linkset, chanpos);
2010                                         if (p->owner) {
2011                                                 ast_setstate(p->owner, AST_STATE_RINGING);
2012                                                 ast_channel_unlock(p->owner);
2013                                         }
2014                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
2015                                 }
2016                                 p->echocontrol_ind = e->acm.echocontrol_ind;
2017                                 sig_ss7_unlock_private(p);
2018                                 break;
2019                         case ISUP_EVENT_CGB:
2020                                 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
2021                                 if (chanpos < 0) {
2022                                         isup_free_call(ss7, e->cgb.call);
2023                                         break;
2024                                 }
2025                                 p = linkset->pvts[chanpos];
2026                                 ss7_check_range(linkset, e->cgb.startcic, e->cgb.endcic,
2027                                         e->cgb.opc, e->cgb.status);
2028                                 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic,
2029                                         e->cgb.opc, e->cgb.status, 1, 1,
2030                                         (e->cgb.type) ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2031
2032                                 sig_ss7_lock_private(p);
2033                                 p->ss7call = e->cgb.call;
2034
2035                                 isup_cgba(linkset->ss7, p->ss7call, e->cgb.endcic, e->cgb.status);
2036                                 if (!p->owner) {
2037                                         p->ss7call = isup_free_call_if_clear(ss7, e->cgb.call);
2038                                 }
2039                                 sig_ss7_unlock_private(p);
2040                                 break;
2041                         case ISUP_EVENT_CGU:
2042                                 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
2043                                 if (chanpos < 0) {
2044                                         isup_free_call(ss7, e->cgu.call);
2045                                         break;
2046                                 }
2047                                 p = linkset->pvts[chanpos];
2048                                 ss7_check_range(linkset, e->cgu.startcic, e->cgu.endcic,
2049                                         e->cgu.opc, e->cgu.status);
2050                                 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic,
2051                                         e->cgu.opc, e->cgu.status, 0, 1,
2052                                         e->cgu.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2053
2054                                 sig_ss7_lock_private(p);
2055                                 p->ss7call = e->cgu.call;
2056
2057                                 isup_cgua(linkset->ss7, p->ss7call, e->cgu.endcic, e->cgu.status);
2058                                 if (!p->owner) {
2059                                         p->ss7call = isup_free_call_if_clear(ss7, e->cgu.call);
2060                                 }
2061                                 sig_ss7_unlock_private(p);
2062                                 break;
2063                         case ISUP_EVENT_UCIC:
2064                                 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
2065                                 if (chanpos < 0) {
2066                                         isup_free_call(ss7, e->ucic.call);
2067                                         break;
2068                                 }
2069                                 p = linkset->pvts[chanpos];
2070                                 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
2071                                 sig_ss7_lock_private(p);
2072                                 sig_ss7_lock_owner(linkset, chanpos);
2073                                 if (p->owner) {
2074                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2075                                         ast_channel_unlock(p->owner);
2076                                 }
2077                                 sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2078                                 sig_ss7_set_inservice(p, 0);
2079                                 p->ss7call = NULL;
2080                                 isup_free_call(ss7, e->ucic.call);
2081                                 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
2082                                 break;
2083                         case ISUP_EVENT_BLO:
2084                                 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
2085                                 if (chanpos < 0) {
2086                                         isup_free_call(ss7, e->blo.call);
2087                                         break;
2088                                 }
2089                                 p = linkset->pvts[chanpos];
2090                                 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
2091                                 sig_ss7_lock_private(p);
2092                                 p->ss7call = e->blo.call;
2093                                 sig_ss7_set_remotelyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2094                                 isup_bla(linkset->ss7, e->blo.call);
2095                                 sig_ss7_lock_owner(linkset, chanpos);
2096                                 if (!p->owner) {
2097                                         p->ss7call = isup_free_call_if_clear(ss7, e->blo.call);
2098                                 } else {
2099                                         if (e->blo.got_sent_msg & ISUP_SENT_IAM) {
2100                                                 /* Q.784 6.2.2 */
2101                                                 ast_channel_hangupcause_set(p->owner, SS7_CAUSE_TRY_AGAIN);
2102                                         }
2103                                         ast_channel_unlock(p->owner);
2104                                 }
2105                                 sig_ss7_unlock_private(p);
2106                                 break;
2107                         case ISUP_EVENT_BLA:
2108                                 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
2109                                 if (chanpos < 0) {
2110                                         isup_free_call(ss7, e->bla.call);
2111                                         break;
2112                                 }
2113                                 ast_debug(1, "Locally blocking CIC %d\n", e->bla.cic);
2114                                 p = linkset->pvts[chanpos];
2115                                 sig_ss7_lock_private(p);
2116                                 p->ss7call = e->bla.call;
2117                                 sig_ss7_set_locallyblocked(p, 1, SS7_BLOCKED_MAINTENANCE);
2118                                 if (!p->owner) {
2119                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2120                                 }
2121                                 sig_ss7_unlock_private(p);
2122                                 break;
2123                         case ISUP_EVENT_UBL:
2124                                 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
2125                                 if (chanpos < 0) {
2126                                         isup_free_call(ss7, e->ubl.call);
2127                                         break;
2128                                 }
2129                                 p = linkset->pvts[chanpos];
2130                                 ast_debug(1, "Remotely unblocking CIC %d PC %d\n", e->ubl.cic, e->ubl.opc);
2131                                 sig_ss7_lock_private(p);
2132                                 p->ss7call = e->ubl.call;
2133                                 sig_ss7_set_remotelyblocked(p, 0, SS7_BLOCKED_MAINTENANCE);
2134                                 isup_uba(linkset->ss7, e->ubl.call);
2135                                 if (!p->owner) {
2136                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2137                                 }
2138                                 sig_ss7_unlock_private(p);
2139                                 break;
2140                         case ISUP_EVENT_UBA:
2141                                 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
2142                                 if (chanpos < 0) {
2143                                         isup_free_call(ss7, e->uba.call);
2144                                         break;
2145                                 }
2146                                 p = linkset->pvts[chanpos];
2147                                 ast_debug(1, "Locally unblocking CIC %d PC %d\n", e->uba.cic, e->uba.opc);
2148                                 sig_ss7_lock_private(p);
2149                                 p->ss7call = e->uba.call;
2150                                 sig_ss7_set_locallyblocked(p, 0, SS7_BLOCKED_MAINTENANCE);
2151                                 if (!p->owner) {
2152                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2153                                 }
2154                                 sig_ss7_unlock_private(p);
2155                                 break;
2156                         case ISUP_EVENT_CON:
2157                         case ISUP_EVENT_ANM:
2158                                 if (e->e == ISUP_EVENT_CON) {
2159                                         chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
2160                                         if (chanpos < 0) {
2161                                                 isup_free_call(ss7, e->con.call);
2162                                                 break;
2163                                         }
2164                                 } else {
2165                                         chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
2166                                         if (chanpos < 0) {
2167                                                 isup_free_call(ss7, e->anm.call);
2168                                                 break;
2169                                         }
2170                                 }
2171
2172                                 p = linkset->pvts[chanpos];
2173                                 sig_ss7_lock_private(p);
2174                                 p->ss7call = (e->e == ISUP_EVENT_ANM) ?  e->anm.call : e->con.call;
2175                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2176                                 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
2177                                         p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
2178                                 }
2179
2180                                 if (!ast_strlen_zero((e->e == ISUP_EVENT_ANM)
2181                                         ? e->anm.connected_num : e->con.connected_num)) {
2182                                         sig_ss7_lock_owner(linkset, chanpos);
2183                                         if (p->owner) {
2184                                                 struct ast_party_connected_line ast_connected;
2185                                                 char connected_num[AST_MAX_EXTENSION];
2186
2187                                                 ast_party_connected_line_init(&ast_connected);
2188                                                 if (e->e == ISUP_EVENT_ANM) {
2189                                                         ast_connected.id.number.presentation = ss7_pres_scr2cid_pres(
2190                                                                 e->anm.connected_presentation_ind,
2191                                                                 e->anm.connected_screening_ind);
2192                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
2193                                                                 linkset, e->anm.connected_num, e->anm.connected_nai);
2194                                                         ast_connected.id.number.str = ast_strdup(connected_num);
2195                                                 } else {
2196                                                         ast_connected.id.number.presentation = ss7_pres_scr2cid_pres(
2197                                                                 e->con.connected_presentation_ind,
2198                                                                 e->con.connected_screening_ind);
2199                                                         ss7_apply_plan_to_number(connected_num, sizeof(connected_num),
2200                                                                 linkset, e->con.connected_num, e->con.connected_nai);
2201                                                         ast_connected.id.number.str = ast_strdup(connected_num);
2202                                                 }
2203                                                 ast_connected.id.number.valid = 1;
2204                                                 ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
2205                                                 ast_channel_queue_connected_line_update(p->owner, &ast_connected, NULL);
2206                                                 ast_party_connected_line_free(&ast_connected);
2207                                                 ast_channel_unlock(p->owner);
2208                                         }
2209                                 }
2210
2211                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
2212                                 sig_ss7_set_dialing(p, 0);
2213                                 sig_ss7_open_media(p);
2214                                 if (((e->e == ISUP_EVENT_ANM) ? !e->anm.echocontrol_ind  :
2215                                                 !e->con.echocontrol_ind) || !(linkset->flags & LINKSET_FLAG_USEECHOCONTROL)) {
2216                                         sig_ss7_set_echocanceller(p, 1);
2217                                 }
2218                                 sig_ss7_unlock_private(p);
2219                                 break;
2220                         case ISUP_EVENT_RLC:
2221                                 chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
2222                                 if (chanpos < 0) {
2223                                         isup_free_call(ss7, e->rlc.call);
2224                                         break;
2225                                 }
2226
2227                                 p = linkset->pvts[chanpos];
2228                                 sig_ss7_lock_private(p);
2229                                 p->ss7call = e->rlc.call;
2230                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2231                                 if (e->rlc.got_sent_msg & (ISUP_SENT_RSC | ISUP_SENT_REL)) {
2232                                         sig_ss7_loopback(p, 0);
2233                                         if (e->rlc.got_sent_msg & ISUP_SENT_RSC) {
2234                                                 sig_ss7_set_inservice(p, 1);
2235                                         }
2236                                 }
2237                                 sig_ss7_lock_owner(linkset, chanpos);
2238                                 if (!p->owner) {
2239                                         p->ss7call = isup_free_call_if_clear(ss7, e->rlc.call);
2240                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
2241                                 } else {
2242                                         p->do_hangup = SS7_HANGUP_DO_NOTHING;
2243                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2244                                         ast_channel_unlock(p->owner);
2245                                 }
2246                                 sig_ss7_unlock_private(p);
2247                                 break;
2248                         case ISUP_EVENT_FAA:
2249                                 /*!
2250                                  * \todo The handling of the SS7 FAA message is not good and I
2251                                  * don't know enough to handle it correctly.
2252                                  */
2253                                 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
2254                                 if (chanpos < 0) {
2255                                         isup_free_call(ss7, e->faa.call);
2256                                         break;
2257                                 }
2258
2259                                 /* XXX FAR and FAA used for something dealing with transfers? */
2260                                 p = linkset->pvts[chanpos];
2261                                 sig_ss7_lock_private(p);
2262                                 callid = func_ss7_linkset_callid(linkset, chanpos);
2263                                 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
2264                                 p->ss7call = isup_free_call_if_clear(ss7, e->faa.call);
2265                                 sig_ss7_unlock_private(p);
2266                                 break;
2267                         case ISUP_EVENT_CGBA:
2268                                 chanpos = ss7_find_cic_gripe(linkset, e->cgba.startcic, e->cgba.opc, "CGBA");
2269                                 if (chanpos < 0) {      /* Never will be true */
2270                                         isup_free_call(ss7, e->cgba.call);
2271                                         break;
2272                                 }
2273
2274                                 ss7_block_cics(linkset, e->cgba.startcic, e->cgba.endcic,
2275                                         e->cgba.opc, e->cgba.status, 1, 0,
2276                                         e->cgba.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2277
2278                                 p = linkset->pvts[chanpos];
2279                                 sig_ss7_lock_private(p);
2280                                 p->ss7call = e->cgba.call;
2281
2282                                 if (!p->owner) {
2283                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2284                                 }
2285                                 sig_ss7_unlock_private(p);
2286                                 break;
2287                         case ISUP_EVENT_CGUA:
2288                                 chanpos = ss7_find_cic_gripe(linkset, e->cgua.startcic, e->cgua.opc, "CGUA");
2289                                 if (chanpos < 0) { /* Never will be true */
2290                                         isup_free_call(ss7, e->cgua.call);
2291                                         break;
2292                                 }
2293
2294                                 ss7_block_cics(linkset, e->cgua.startcic, e->cgua.endcic,
2295                                         e->cgua.opc, e->cgua.status, 0, 0,
2296                                         e->cgba.type ? SS7_BLOCKED_HARDWARE : SS7_BLOCKED_MAINTENANCE);
2297
2298                                 p = linkset->pvts[chanpos];
2299                                 sig_ss7_lock_private(p);
2300                                 p->ss7call = e->cgua.call;
2301
2302                                 if (!p->owner) {
2303                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2304                                 }
2305                                 sig_ss7_unlock_private(p);
2306                                 break;
2307                         case ISUP_EVENT_SUS:
2308                                 chanpos = ss7_find_cic_gripe(linkset, e->sus.cic, e->sus.opc, "SUS");
2309                                 if (chanpos < 0) {
2310                                         isup_free_call(ss7, e->sus.call);
2311                                         break;
2312                                 }
2313
2314                                 p = linkset->pvts[chanpos];
2315                                 sig_ss7_lock_private(p);
2316                                 p->ss7call = e->sus.call;
2317                                 if (!p->owner) {
2318                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2319                                 }
2320                                 sig_ss7_unlock_private(p);
2321                                 break;
2322                         case ISUP_EVENT_RES:
2323                                 chanpos = ss7_find_cic_gripe(linkset, e->res.cic, e->res.opc, "RES");
2324                                 if (chanpos < 0) {
2325                                         isup_free_call(ss7, e->res.call);
2326                                         break;
2327                                 }
2328
2329                                 p = linkset->pvts[chanpos];
2330                                 sig_ss7_lock_private(p);
2331                                 p->ss7call = e->res.call;
2332                                 if (!p->owner) {
2333                                         p->ss7call = isup_free_call_if_clear(ss7, p->ss7call);
2334                                 }
2335                                 sig_ss7_unlock_private(p);
2336                                 break;
2337                         default:
2338                                 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
2339                                 break;
2340                         }
2341
2342                         /* Call ID stuff needs to be cleaned up here */
2343                         if (callid) {
2344                                 ast_callid_threadassoc_remove();
2345                         }
2346                 }
2347                 ast_mutex_unlock(&linkset->lock);
2348         }
2349
2350         return 0;
2351 }
2352
2353 static void ss7_rel(struct sig_ss7_linkset *ss7)
2354 {
2355         /* Release the lock first */
2356         ast_mutex_unlock(&ss7->lock);
2357
2358         /* Then break the poll to send our messages */
2359         if (ss7->master != AST_PTHREADT_NULL) {
2360                 pthread_kill(ss7->master, SIGURG);
2361         }
2362 }
2363
2364 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
2365 {
2366         /* Grab the lock first */
2367         while (ast_mutex_trylock(&ss7->lock)) {
2368                 /* Avoid deadlock */
2369                 sig_ss7_deadlock_avoidance_private(pvt);
2370         }
2371 }
2372
2373 /*!
2374  * \brief Reset a specific CIC.
2375  * \since 11.0
2376  *
2377  * \param linkset linkset control structure.
2378  * \param cic Circuit Identification Code
2379  * \param dpc Destination Point Code
2380  *
2381  * \return TRUE on success
2382  */
2383 int sig_ss7_reset_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
2384 {
2385         int i;
2386
2387         ast_mutex_lock(&linkset->lock);
2388         for (i = 0; i < linkset->numchans; i++) {
2389                 if (linkset->pvts[i] && linkset->pvts[i]->cic == cic && linkset->pvts[i]->dpc == dpc) {
2390                         int res;
2391
2392                         sig_ss7_lock_private(linkset->pvts[i]);
2393                         sig_ss7_set_locallyblocked(linkset->pvts[i], 0, SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2394                         res = ss7_start_rsc(linkset, i);
2395                         sig_ss7_unlock_private(linkset->pvts[i]);
2396                         ss7_rel(linkset);       /* Also breaks the poll to send our messages */
2397                         return res;
2398                 }
2399         }
2400         ss7_rel(linkset);
2401
2402         return 0;
2403 }
2404
2405 /*!
2406  * \brief Block or Unblock a specific CIC.
2407  * \since 11.0
2408  *
2409  * \param linkset linkset control structure.
2410  * \param do_block Action to perform. Block if TRUE.
2411  * \param which On which CIC to perform the operation.
2412  *
2413  * \return 0 on success
2414  */
2415 int sig_ss7_cic_blocking(struct sig_ss7_linkset *linkset, int do_block, int which)
2416 {
2417         ast_mutex_lock(&linkset->lock);
2418         sig_ss7_lock_private(linkset->pvts[which]);
2419         if (!ss7_find_alloc_call(linkset->pvts[which])) {
2420                 sig_ss7_unlock_private(linkset->pvts[which]);
2421                 ss7_rel(linkset);
2422                 return -1;
2423         }
2424
2425         if (do_block) {
2426                 isup_blo(linkset->ss7, linkset->pvts[which]->ss7call);
2427         } else {
2428                 isup_ubl(linkset->ss7, linkset->pvts[which]->ss7call);
2429         }
2430
2431         sig_ss7_unlock_private(linkset->pvts[which]);
2432         ss7_rel(linkset);       /* Also breaks the poll to send our messages */
2433
2434         return 0;
2435 }
2436
2437 /*!
2438  * \brief Block or Unblock a range of CICs.
2439  * \since 11.0
2440  *
2441  * \param linkset linkset control structure.
2442  * \param do_block Action to perform. Block if TRUE.
2443  * \param chanpos Channel position to start from.
2444  * \param endcic Circuit Identification Code of the end of the range.
2445  * \param state Array of CIC blocking status.
2446  * \param type Type of the blocking - maintenance or hardware
2447  *
2448  * \note Assumes the linkset->lock is already obtained.
2449  *
2450  * \return 0 on success
2451  */
2452 int sig_ss7_group_blocking(struct sig_ss7_linkset *linkset, int do_block, int chanpos, int endcic, unsigned char state[], int type)
2453 {
2454         sig_ss7_lock_private(linkset->pvts[chanpos]);
2455         if (!ss7_find_alloc_call(linkset->pvts[chanpos])) {
2456                 sig_ss7_unlock_private(linkset->pvts[chanpos]);
2457                 return -1;
2458         }
2459
2460         if (do_block) {
2461                 isup_cgb(linkset->ss7, linkset->pvts[chanpos]->ss7call, endcic, state, type);
2462         } else {
2463                 isup_cgu(linkset->ss7, linkset->pvts[chanpos]->ss7call, endcic, state, type);
2464         }
2465
2466         sig_ss7_unlock_private(linkset->pvts[chanpos]);
2467         return 0;
2468 }
2469
2470 /*!
2471  * \brief Reset a group of CICs.
2472  * \since 11.0
2473  *
2474  * \param linkset linkset control structure.
2475  * \param cic Circuit Identification Code
2476  * \param dpc Destination Point Code
2477  * \param range Range of the CICs to reset
2478  *
2479  * \note Assumes the linkset->lock is already obtained.
2480  *
2481  * \return 0 on success
2482  */
2483 int sig_ss7_reset_group(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, int range)
2484 {
2485         int i;
2486
2487         for (i = 0; i < linkset->numchans; i++) {
2488                 if (linkset->pvts[i] && linkset->pvts[i]->cic == cic && linkset->pvts[i]->dpc == dpc) {
2489                         ss7_clear_channels(linkset, cic, cic + range, dpc, SS7_HANGUP_FREE_CALL);
2490                         ss7_block_cics(linkset, cic, cic + range, dpc, NULL, 0, 1,
2491                                 SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2492                         ss7_block_cics(linkset, cic, cic + range, dpc, NULL, 0, 0,
2493                                 SS7_BLOCKED_MAINTENANCE | SS7_BLOCKED_HARDWARE);
2494
2495                         sig_ss7_lock_private(linkset->pvts[i]);
2496                         if (!ss7_find_alloc_call(linkset->pvts[i])) {
2497                                 sig_ss7_unlock_private(linkset->pvts[i]);
2498                                 return -1;
2499                         }
2500                         isup_grs(linkset->ss7, linkset->pvts[i]->ss7call, linkset->pvts[i]->cic + range);
2501                         sig_ss7_unlock_private(linkset->pvts[i]);
2502                         break;
2503                 }
2504         }
2505         return 0;
2506 }
2507
2508 void sig_ss7_free_isup_call(struct sig_ss7_linkset *linkset, int channel)
2509 {
2510         sig_ss7_lock_private(linkset->pvts[channel]);
2511         if (linkset->pvts[channel]->ss7call) {
2512                 isup_free_call(linkset->ss7, linkset->pvts[channel]->ss7call);
2513                 linkset->pvts[channel]->ss7call = NULL;
2514         }
2515         sig_ss7_unlock_private(linkset->pvts[channel]);
2516 }
2517
2518 static int ss7_parse_prefix(struct sig_ss7_chan *p, const char *number, char *nai)
2519 {
2520         int strip = 0;
2521
2522         if (strncmp(number, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2523                 strip = strlen(p->ss7->internationalprefix);
2524                 *nai = SS7_NAI_INTERNATIONAL;
2525         } else if (strncmp(number, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2526                 strip = strlen(p->ss7->nationalprefix);
2527                 *nai = SS7_NAI_NATIONAL;
2528         } else if (strncmp(number, p->ss7->networkroutedprefix, strlen(p->ss7->networkroutedprefix)) == 0) {
2529                 strip = strlen(p->ss7->networkroutedprefix);
2530                 *nai = SS7_NAI_NETWORKROUTED;
2531         } else if (strncmp(number, p->ss7->unknownprefix, strlen(p->ss7->unknownprefix)) == 0) {
2532                 strip = strlen(p->ss7->unknownprefix);
2533                 *nai = SS7_NAI_UNKNOWN;
2534         } else if (strncmp(number, p->ss7->subscriberprefix, strlen(p->ss7->subscriberprefix)) == 0) {
2535                 strip = strlen(p->ss7->subscriberprefix);
2536                 *nai = SS7_NAI_SUBSCRIBER;
2537         } else {
2538                 *nai = SS7_NAI_SUBSCRIBER;
2539         }
2540
2541         return strip;
2542 }
2543
2544 /*!
2545  * \brief Notify the SS7 layer that the link is in alarm.
2546  * \since 1.8
2547  *
2548  * \param linkset Controlling linkset for the channel.
2549  * \param which Link index of the signaling channel.
2550  *
2551  * \return Nothing
2552  */
2553 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
2554 {
2555         linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
2556         linkset->linkstate[which] &= ~LINKSTATE_UP;
2557         ss7_link_alarm(linkset->ss7, linkset->fds[which]);
2558 }
2559
2560 /*!
2561  * \brief Notify the SS7 layer that the link is no longer in alarm.
2562  * \since 1.8
2563  *
2564  * \param linkset Controlling linkset for the channel.
2565  * \param which Link index of the signaling channel.
2566  *
2567  * \return Nothing
2568  */
2569 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
2570 {
2571         linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
2572         linkset->linkstate[which] |= LINKSTATE_STARTING;
2573         ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
2574 }
2575
2576 /*!
2577  * \brief Setup and add a SS7 link channel.
2578  * \since 1.8
2579  *
2580  * \param linkset Controlling linkset for the channel.
2581  * \param which Link index of the signaling channel.
2582  * \param ss7type Switch type of the linkset
2583  * \param transport Signaling transport of channel.
2584  * \param inalarm Non-zero if the channel is in alarm.
2585  * \param networkindicator User configuration parameter.
2586  * \param pointcode User configuration parameter.
2587  * \param adjpointcode User configuration parameter.
2588  *
2589  * \retval 0 on success.
2590  * \retval -1 on error.
2591  */
2592 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)
2593 {
2594         if (!linkset->ss7) {
2595                 linkset->type = ss7type;
2596                 linkset->ss7 = ss7_new(ss7type);
2597                 if (!linkset->ss7) {
2598                         ast_log(LOG_ERROR, "Can't create new SS7!\n");
2599                         return -1;
2600                 }
2601         }
2602
2603         ss7_set_network_ind(linkset->ss7, networkindicator);
2604         ss7_set_pc(linkset->ss7, pointcode);
2605
2606         if (ss7_add_link(linkset->ss7, transport, linkset->fds[which], cur_slc, adjpointcode)) {
2607                 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
2608         }
2609
2610         if (inalarm) {
2611                 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
2612                 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
2613         } else {
2614                 linkset->linkstate[which] = LINKSTATE_DOWN;
2615                 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
2616         }
2617
2618         return 0;
2619 }
2620
2621 /*!
2622  * \brief Determine if the specified channel is available for an outgoing call.
2623  * \since 1.8
2624  *
2625  * \param p Signaling private structure pointer.
2626  *
2627  * \retval TRUE if the channel is available.
2628  */
2629 int sig_ss7_available(struct sig_ss7_chan *p)
2630 {
2631         int available;
2632
2633         if (!p->ss7) {
2634                 /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
2635                 return 0;
2636         }
2637
2638         /* Only have to deal with the linkset lock. */
2639         ast_mutex_lock(&p->ss7->lock);
2640         available = sig_ss7_is_chan_available(p);
2641         if (available) {
2642                 p->ss7call = isup_new_call(p->ss7->ss7, p->cic, p->dpc, 1);
2643                 if (!p->ss7call) {
2644                         ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2645                         available = 0;
2646                 } else {
2647                         p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
2648                 }
2649         }
2650         ast_mutex_unlock(&p->ss7->lock);
2651
2652         return available;
2653 }
2654
2655 static unsigned char cid_pres2ss7pres(int cid_pres)
2656 {
2657          return (cid_pres >> 5) & 0x03;
2658 }
2659
2660 static unsigned char cid_pres2ss7screen(int cid_pres)
2661 {
2662         return cid_pres & 0x03;
2663 }
2664
2665 static void ss7_connected_line_update(struct sig_ss7_chan *p, struct ast_party_connected_line *connected)
2666 {
2667         int connected_strip = 0;
2668         char connected_nai;
2669         unsigned char connected_pres;
2670         unsigned char connected_screen;
2671         const char *connected_num;
2672
2673         if (!connected->id.number.valid) {
2674                 return;
2675         }
2676
2677         connected_num = S_OR(connected->id.number.str, "");
2678         if (p->ss7->called_nai ==  SS7_NAI_DYNAMIC) {
2679                 connected_strip = ss7_parse_prefix(p, connected_num, &connected_nai);
2680         } else {
2681                 connected_nai = p->ss7->called_nai;
2682         }
2683
2684         connected_pres = cid_pres2ss7pres(connected->id.number.presentation);
2685         connected_screen = cid_pres2ss7screen(connected->id.number.presentation);
2686
2687         isup_set_connected(p->ss7call, connected_num + connected_strip, connected_nai, connected_pres, connected_screen);
2688 }
2689
2690 static unsigned char ss7_redirect_reason(struct sig_ss7_chan *p, struct ast_party_redirecting *redirecting, int orig)
2691 {
2692         int reason = (orig) ? redirecting->orig_reason.code : redirecting->reason.code;
2693
2694         switch (reason) {
2695         case AST_REDIRECTING_REASON_USER_BUSY:
2696                 return SS7_REDIRECTING_REASON_USER_BUSY;
2697         case AST_REDIRECTING_REASON_NO_ANSWER:
2698                 return SS7_REDIRECTING_REASON_NO_ANSWER;
2699         case AST_REDIRECTING_REASON_UNCONDITIONAL:
2700                 return SS7_REDIRECTING_REASON_UNCONDITIONAL;
2701         }
2702
2703         if (orig || reason == AST_REDIRECTING_REASON_UNKNOWN) {
2704                 return SS7_REDIRECTING_REASON_UNKNOWN;
2705         }
2706
2707         if (reason == AST_REDIRECTING_REASON_UNAVAILABLE) {
2708                 return SS7_REDIRECTING_REASON_UNAVAILABLE;
2709         }
2710
2711         if (reason == AST_REDIRECTING_REASON_DEFLECTION) {
2712                 if (p->call_level > SIG_SS7_CALL_LEVEL_PROCEEDING) {
2713                         return SS7_REDIRECTING_REASON_DEFLECTION_DURING_ALERTING;
2714                 }
2715                 return SS7_REDIRECTING_REASON_DEFLECTION_IMMEDIATE_RESPONSE;
2716         }
2717
2718         return SS7_REDIRECTING_REASON_UNKNOWN;
2719 }
2720
2721 static unsigned char ss7_redirect_info_ind(struct ast_channel *ast)
2722 {
2723         const char *redirect_info_ind;
2724         struct ast_party_redirecting *redirecting = ast_channel_redirecting(ast);
2725
2726         redirect_info_ind = pbx_builtin_getvar_helper(ast, "SS7_REDIRECT_INFO_IND");
2727         if (!ast_strlen_zero(redirect_info_ind)) {
2728                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_PRES_ALLOWED")) {
2729                         return SS7_INDICATION_REROUTED_PRES_ALLOWED;
2730                 }
2731                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_INFO_RESTRICTED")) {
2732                         return SS7_INDICATION_REROUTED_INFO_RESTRICTED;
2733                 }
2734                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_PRES_ALLOWED")) {
2735                         return SS7_INDICATION_DIVERTED_PRES_ALLOWED;
2736                 }
2737                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_INFO_RESTRICTED")) {
2738                         return SS7_INDICATION_DIVERTED_INFO_RESTRICTED;
2739                 }
2740                 if (!strcasecmp(redirect_info_ind, "CALL_REROUTED_PRES_RESTRICTED")) {
2741                         return SS7_INDICATION_REROUTED_PRES_RESTRICTED;
2742                 }
2743                 if (!strcasecmp(redirect_info_ind, "CALL_DIVERTED_PRES_RESTRICTED")) {
2744                         return SS7_INDICATION_DIVERTED_PRES_RESTRICTED;
2745                 }
2746                 if (!strcasecmp(redirect_info_ind, "SPARE")) {
2747                         return SS7_INDICATION_SPARE;
2748                 }
2749                 return SS7_INDICATION_NO_REDIRECTION;
2750         }
2751
2752         if (redirecting->reason.code == AST_REDIRECTING_REASON_DEFLECTION) {
2753                 if ((redirecting->to.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2754                         if ((redirecting->orig.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2755                                 return SS7_INDICATION_DIVERTED_PRES_ALLOWED;
2756                         }
2757                         return SS7_INDICATION_DIVERTED_PRES_RESTRICTED;
2758                 }
2759                 return SS7_INDICATION_DIVERTED_INFO_RESTRICTED;
2760         }
2761
2762         if ((redirecting->to.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2763                 if ((redirecting->orig.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
2764                         return SS7_INDICATION_REROUTED_PRES_ALLOWED;
2765                 }
2766                 return SS7_INDICATION_REROUTED_PRES_RESTRICTED;
2767         }
2768         return SS7_INDICATION_REROUTED_INFO_RESTRICTED;
2769 }
2770
2771 static void ss7_redirecting_update(struct sig_ss7_chan *p, struct ast_channel *ast)
2772 {
2773         int num_nai_strip = 0;
2774         struct ast_party_redirecting *redirecting = ast_channel_redirecting(ast);
2775
2776         if (!redirecting->count) {
2777                 return;
2778         }
2779
2780         isup_set_redirect_counter(p->ss7call, redirecting->count);
2781
2782         if (redirecting->orig.number.valid) {
2783                 char ss7_orig_called_nai = p->ss7->called_nai;
2784                 const char *ss7_orig_called_num = S_OR(redirecting->orig.number.str, "");
2785
2786                 if (ss7_orig_called_nai == SS7_NAI_DYNAMIC) {
2787                         num_nai_strip = ss7_parse_prefix(p, ss7_orig_called_num, &ss7_orig_called_nai);
2788                 } else {
2789                         num_nai_strip = 0;
2790                 }
2791                 isup_set_orig_called_num(p->ss7call, ss7_orig_called_num + num_nai_strip,
2792                         ss7_orig_called_nai,
2793                         cid_pres2ss7pres(redirecting->orig.number.presentation),
2794                         cid_pres2ss7screen(redirecting->orig.number.presentation));
2795         }
2796
2797         if (redirecting->from.number.valid) {
2798                 char ss7_redirecting_num_nai = p->ss7->calling_nai;
2799                 const char *redirecting_number = S_OR(redirecting->from.number.str, "");
2800
2801                 if (ss7_redirecting_num_nai == SS7_NAI_DYNAMIC) {
2802                         num_nai_strip = ss7_parse_prefix(p, redirecting_number, &ss7_redirecting_num_nai);
2803                 } else {
2804                         num_nai_strip = 0;
2805                 }
2806
2807                 isup_set_redirecting_number(p->ss7call, redirecting_number + num_nai_strip,
2808                         ss7_redirecting_num_nai,
2809                         cid_pres2ss7pres(redirecting->from.number.presentation),
2810                         cid_pres2ss7screen(redirecting->from.number.presentation));
2811         }
2812
2813         isup_set_redirection_info(p->ss7call, ss7_redirect_info_ind(ast),
2814                 ss7_redirect_reason(p, ast_channel_redirecting(ast), 1),
2815                 redirecting->count, ss7_redirect_reason(p, ast_channel_redirecting(ast), 0));
2816 }
2817
2818 /*!
2819  * \brief Dial out using the specified SS7 channel.
2820  * \since 1.8
2821  *
2822  * \param p Signaling private structure pointer.
2823  * \param ast Asterisk channel structure pointer.
2824  * \param rdest Dialstring.
2825  *
2826  * \retval 0 on success.
2827  * \retval -1 on error.
2828  */
2829 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
2830 {
2831         char ss7_called_nai;
2832         int called_nai_strip;
2833         char ss7_calling_nai;
2834         int calling_nai_strip;
2835         const char *col_req = NULL;
2836         const char *ss7_cug_indicator_str;
2837         const char *ss7_cug_interlock_ni;
2838         const char *ss7_cug_interlock_code;
2839         const char *ss7_interworking_indicator;
2840         const char *ss7_forward_indicator_pmbits;
2841         unsigned char ss7_cug_indicator;
2842         const char *charge_str = NULL;
2843         const char *gen_address = NULL;
2844         const char *gen_digits = NULL;
2845         const char *gen_dig_type = NULL;
2846         const char *gen_dig_scheme = NULL;
2847         const char *gen_name = NULL;
2848         const char *jip_digits = NULL;
2849         const char *lspi_ident = NULL;
2850         const char *rlt_flag = NULL;
2851         const char *call_ref_id = NULL;
2852         const char *call_ref_pc = NULL;
2853         const char *send_far = NULL;
2854         const char *tmr = NULL;
2855         char *c;
2856         char *l;
2857         char dest[256];
2858
2859         ast_copy_string(dest, rdest, sizeof(dest));
2860
2861         c = strchr(dest, '/');
2862         if (c) {
2863                 c++;
2864         } else {
2865                 c = "";
2866         }
2867         if (strlen(c) < p->stripmsd) {
2868                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2869                 return -1;
2870         }
2871
2872         if (!p->hidecallerid) {
2873                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
2874         } else {
2875                 l = NULL;
2876         }
2877
2878         ss7_grab(p, p->ss7);
2879
2880         if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
2881</