sig_ss7.c 101 KB

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