sig_ss7.c 101 KB

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