sig_ss7.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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/transcap.h"
  39. #include "asterisk/stasis_channels.h"
  40. #include "sig_ss7.h"
  41. #if defined(LIBSS7_ABI_COMPATIBILITY)
  42. #error "Your installed libss7 is not compatible"
  43. #endif
  44. /* ------------------------------------------------------------------- */
  45. static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
  46. {
  47. switch (level) {
  48. case SIG_SS7_CALL_LEVEL_IDLE:
  49. return "Idle";
  50. case SIG_SS7_CALL_LEVEL_ALLOCATED:
  51. return "Allocated";
  52. case SIG_SS7_CALL_LEVEL_CONTINUITY:
  53. return "Continuity";
  54. case SIG_SS7_CALL_LEVEL_SETUP:
  55. return "Setup";
  56. case SIG_SS7_CALL_LEVEL_PROCEEDING:
  57. return "Proceeding";
  58. case SIG_SS7_CALL_LEVEL_ALERTING:
  59. return "Alerting";
  60. case SIG_SS7_CALL_LEVEL_CONNECT:
  61. return "Connect";
  62. case SIG_SS7_CALL_LEVEL_GLARE:
  63. return "Glare";
  64. }
  65. return "Unknown";
  66. }
  67. static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
  68. {
  69. if (sig_ss7_callbacks.unlock_private) {
  70. sig_ss7_callbacks.unlock_private(p->chan_pvt);
  71. }
  72. }
  73. static void sig_ss7_lock_private(struct sig_ss7_chan *p)
  74. {
  75. if (sig_ss7_callbacks.lock_private) {
  76. sig_ss7_callbacks.lock_private(p->chan_pvt);
  77. }
  78. }
  79. static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
  80. {
  81. if (sig_ss7_callbacks.deadlock_avoidance_private) {
  82. sig_ss7_callbacks.deadlock_avoidance_private(p->chan_pvt);
  83. } else {
  84. /* Fallback to the old way if callback not present. */
  85. sig_ss7_unlock_private(p);
  86. sched_yield();
  87. sig_ss7_lock_private(p);
  88. }
  89. }
  90. void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
  91. {
  92. p->inalarm = in_alarm;
  93. if (sig_ss7_callbacks.set_alarm) {
  94. sig_ss7_callbacks.set_alarm(p->chan_pvt, in_alarm);
  95. }
  96. }
  97. static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
  98. {
  99. if (sig_ss7_callbacks.set_dialing) {
  100. sig_ss7_callbacks.set_dialing(p->chan_pvt, is_dialing);
  101. }
  102. }
  103. static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
  104. {
  105. if (sig_ss7_callbacks.set_digital) {
  106. sig_ss7_callbacks.set_digital(p->chan_pvt, is_digital);
  107. }
  108. }
  109. static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
  110. {
  111. p->outgoing = is_outgoing;
  112. if (sig_ss7_callbacks.set_outgoing) {
  113. sig_ss7_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
  114. }
  115. }
  116. static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
  117. {
  118. if (sig_ss7_callbacks.set_inservice) {
  119. sig_ss7_callbacks.set_inservice(p->chan_pvt, is_inservice);
  120. }
  121. }
  122. static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
  123. {
  124. p->locallyblocked = is_blocked;
  125. if (sig_ss7_callbacks.set_locallyblocked) {
  126. sig_ss7_callbacks.set_locallyblocked(p->chan_pvt, is_blocked);
  127. }
  128. }
  129. static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
  130. {
  131. p->remotelyblocked = is_blocked;
  132. if (sig_ss7_callbacks.set_remotelyblocked) {
  133. sig_ss7_callbacks.set_remotelyblocked(p->chan_pvt, is_blocked);
  134. }
  135. }
  136. /*!
  137. * \internal
  138. * \brief Open the SS7 channel media path.
  139. * \since 1.8.12
  140. *
  141. * \param p Channel private control structure.
  142. *
  143. * \return Nothing
  144. */
  145. static void sig_ss7_open_media(struct sig_ss7_chan *p)
  146. {
  147. if (sig_ss7_callbacks.open_media) {
  148. sig_ss7_callbacks.open_media(p->chan_pvt);
  149. }
  150. }
  151. /*!
  152. * \internal
  153. * \brief Set the caller id information in the parent module.
  154. * \since 1.8
  155. *
  156. * \param p sig_ss7 channel structure.
  157. *
  158. * \return Nothing
  159. */
  160. static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
  161. {
  162. struct ast_party_caller caller;
  163. if (sig_ss7_callbacks.set_callerid) {
  164. ast_party_caller_init(&caller);
  165. caller.id.name.str = p->cid_name;
  166. caller.id.name.presentation = p->callingpres;
  167. caller.id.name.valid = 1;
  168. caller.id.number.str = p->cid_num;
  169. caller.id.number.plan = p->cid_ton;
  170. caller.id.number.presentation = p->callingpres;
  171. caller.id.number.valid = 1;
  172. if (!ast_strlen_zero(p->cid_subaddr)) {
  173. caller.id.subaddress.valid = 1;
  174. //caller.id.subaddress.type = 0;/* nsap */
  175. //caller.id.subaddress.odd_even_indicator = 0;
  176. caller.id.subaddress.str = p->cid_subaddr;
  177. }
  178. caller.ani.number.str = p->cid_ani;
  179. //caller.ani.number.plan = p->xxx;
  180. //caller.ani.number.presentation = p->xxx;
  181. caller.ani.number.valid = 1;
  182. caller.ani2 = p->cid_ani2;
  183. sig_ss7_callbacks.set_callerid(p->chan_pvt, &caller);
  184. }
  185. }
  186. /*!
  187. * \internal
  188. * \brief Set the Dialed Number Identifier.
  189. * \since 1.8
  190. *
  191. * \param p sig_ss7 channel structure.
  192. * \param dnid Dialed Number Identifier string.
  193. *
  194. * \return Nothing
  195. */
  196. static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
  197. {
  198. if (sig_ss7_callbacks.set_dnid) {
  199. sig_ss7_callbacks.set_dnid(p->chan_pvt, dnid);
  200. }
  201. }
  202. static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
  203. {
  204. int res;
  205. if (sig_ss7_callbacks.play_tone) {
  206. res = sig_ss7_callbacks.play_tone(p->chan_pvt, tone);
  207. } else {
  208. res = -1;
  209. }
  210. return res;
  211. }
  212. static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
  213. {
  214. if (sig_ss7_callbacks.set_echocanceller) {
  215. return sig_ss7_callbacks.set_echocanceller(p->chan_pvt, enable);
  216. }
  217. return -1;
  218. }
  219. static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
  220. {
  221. if (p->loopedback != enable) {
  222. p->loopedback = enable;
  223. if (sig_ss7_callbacks.set_loopback) {
  224. sig_ss7_callbacks.set_loopback(p->chan_pvt, enable);
  225. }
  226. }
  227. }
  228. static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state,
  229. int ulaw, int transfercapability, char *exten,
  230. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
  231. {
  232. struct ast_channel *ast;
  233. if (sig_ss7_callbacks.new_ast_channel) {
  234. ast = sig_ss7_callbacks.new_ast_channel(p->chan_pvt, state, ulaw, exten,
  235. assignedids, requestor);
  236. } else {
  237. return NULL;
  238. }
  239. if (!ast) {
  240. return NULL;
  241. }
  242. if (!p->owner) {
  243. p->owner = ast;
  244. }
  245. p->alreadyhungup = 0;
  246. ast_channel_transfercapability_set(ast, transfercapability);
  247. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
  248. ast_transfercapability2str(transfercapability));
  249. if (transfercapability & AST_TRANS_CAP_DIGITAL) {
  250. sig_ss7_set_digital(p, 1);
  251. }
  252. return ast;
  253. }
  254. static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
  255. {
  256. if (sig_ss7_callbacks.handle_link_exception) {
  257. sig_ss7_callbacks.handle_link_exception(linkset, which);
  258. }
  259. }
  260. /*!
  261. * \internal
  262. * \brief Determine if a private channel structure is available.
  263. *
  264. * \param pvt Channel to determine if available.
  265. *
  266. * \return TRUE if the channel is available.
  267. */
  268. static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
  269. {
  270. if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
  271. && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
  272. && !pvt->locallyblocked && !pvt->remotelyblocked) {
  273. return 1;
  274. }
  275. return 0;
  276. }
  277. /*!
  278. * \internal
  279. * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
  280. * \since 1.8
  281. *
  282. * \param ss7 SS7 linkset control structure.
  283. * \param chanpos Channel position in the span.
  284. *
  285. * \note Assumes the ss7->lock is already obtained.
  286. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  287. *
  288. * \return Nothing
  289. */
  290. static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
  291. {
  292. for (;;) {
  293. if (!ss7->pvts[chanpos]->owner) {
  294. /* There is no owner lock to get. */
  295. break;
  296. }
  297. if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
  298. /* We got the lock */
  299. break;
  300. }
  301. /* Avoid deadlock */
  302. sig_ss7_unlock_private(ss7->pvts[chanpos]);
  303. DEADLOCK_AVOIDANCE(&ss7->lock);
  304. sig_ss7_lock_private(ss7->pvts[chanpos]);
  305. }
  306. }
  307. /*!
  308. * \internal
  309. * \brief Queue the given frame onto the owner channel.
  310. * \since 1.8
  311. *
  312. * \param ss7 SS7 linkset control structure.
  313. * \param chanpos Channel position in the span.
  314. * \param frame Frame to queue onto the owner channel.
  315. *
  316. * \note Assumes the ss7->lock is already obtained.
  317. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  318. *
  319. * \return Nothing
  320. */
  321. static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
  322. {
  323. sig_ss7_lock_owner(ss7, chanpos);
  324. if (ss7->pvts[chanpos]->owner) {
  325. ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
  326. ast_channel_unlock(ss7->pvts[chanpos]->owner);
  327. }
  328. }
  329. /*!
  330. * \internal
  331. * \brief Queue a control frame of the specified subclass onto the owner channel.
  332. * \since 1.8
  333. *
  334. * \param ss7 SS7 linkset control structure.
  335. * \param chanpos Channel position in the span.
  336. * \param subclass Control frame subclass to queue onto the owner channel.
  337. *
  338. * \note Assumes the ss7->lock is already obtained.
  339. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  340. *
  341. * \return Nothing
  342. */
  343. static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
  344. {
  345. struct ast_frame f = {AST_FRAME_CONTROL, };
  346. struct sig_ss7_chan *p = ss7->pvts[chanpos];
  347. if (sig_ss7_callbacks.queue_control) {
  348. sig_ss7_callbacks.queue_control(p->chan_pvt, subclass);
  349. }
  350. f.subclass.integer = subclass;
  351. sig_ss7_queue_frame(ss7, chanpos, &f);
  352. }
  353. /*!
  354. * \internal
  355. * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
  356. * \since 11
  357. *
  358. * \param owner Owner channel of the pvt.
  359. * \param cause String describing the cause to be placed into the frame.
  360. *
  361. * \note Assumes the linkset->lock is already obtained.
  362. * \note Assumes the sig_ss7_lock_private(linkset->pvts[chanpos]) is already obtained.
  363. * \note Assumes linkset->pvts[chanpos]->owner is non-NULL and its lock is already obtained.
  364. *
  365. * \return Nothing
  366. */
  367. static void ss7_queue_pvt_cause_data(struct ast_channel *owner, const char *cause, int ast_cause)
  368. {
  369. struct ast_control_pvt_cause_code *cause_code;
  370. int datalen = sizeof(*cause_code) + strlen(cause);
  371. cause_code = ast_alloca(datalen);
  372. memset(cause_code, 0, datalen);
  373. cause_code->ast_cause = ast_cause;
  374. ast_copy_string(cause_code->chan_name, ast_channel_name(owner), AST_CHANNEL_NAME);
  375. ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
  376. ast_queue_control_data(owner, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
  377. ast_channel_hangupcause_hash_set(owner, cause_code, datalen);
  378. }
  379. /*!
  380. * \internal
  381. * \brief Find the channel position by CIC/DPC.
  382. *
  383. * \param linkset SS7 linkset control structure.
  384. * \param cic Circuit Identification Code
  385. * \param dpc Destination Point Code
  386. *
  387. * \retval chanpos on success.
  388. * \retval -1 on error.
  389. */
  390. static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
  391. {
  392. int i;
  393. int winner = -1;
  394. for (i = 0; i < linkset->numchans; i++) {
  395. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
  396. winner = i;
  397. break;
  398. }
  399. }
  400. return winner;
  401. }
  402. /*!
  403. * \internal
  404. * \brief Find the channel position by CIC/DPC and gripe if not found.
  405. *
  406. * \param linkset SS7 linkset control structure.
  407. * \param cic Circuit Identification Code
  408. * \param dpc Destination Point Code
  409. * \param msg_name Message type name that failed.
  410. *
  411. * \retval chanpos on success.
  412. * \retval -1 on error.
  413. */
  414. static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
  415. {
  416. int chanpos;
  417. chanpos = ss7_find_cic(linkset, cic, dpc);
  418. if (chanpos < 0) {
  419. ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested unconfigured CIC/DPC %d/%d.\n",
  420. linkset->span, msg_name, cic, dpc);
  421. return -1;
  422. }
  423. return chanpos;
  424. }
  425. static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  426. {
  427. unsigned char status[32];
  428. struct sig_ss7_chan *p = NULL;
  429. int i, offset;
  430. for (i = 0; i < linkset->numchans; i++) {
  431. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  432. p = linkset->pvts[i];
  433. offset = p->cic - startcic;
  434. status[offset] = 0;
  435. if (p->locallyblocked)
  436. status[offset] |= (1 << 0) | (1 << 4);
  437. if (p->remotelyblocked)
  438. status[offset] |= (1 << 1) | (1 << 5);
  439. if (p->ss7call) {
  440. if (p->outgoing)
  441. status[offset] |= (1 << 3);
  442. else
  443. status[offset] |= (1 << 2);
  444. } else
  445. status[offset] |= 0x3 << 2;
  446. }
  447. }
  448. if (p)
  449. isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
  450. else
  451. ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
  452. }
  453. static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  454. {
  455. int i;
  456. for (i = 0; i < linkset->numchans; i++) {
  457. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  458. sig_ss7_lock_private(linkset->pvts[i]);
  459. sig_ss7_lock_owner(linkset, i);
  460. if (linkset->pvts[i]->owner) {
  461. ast_softhangup_nolock(linkset->pvts[i]->owner, AST_SOFTHANGUP_DEV);
  462. ast_channel_unlock(linkset->pvts[i]->owner);
  463. }
  464. sig_ss7_unlock_private(linkset->pvts[i]);
  465. }
  466. }
  467. }
  468. static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
  469. {
  470. int i;
  471. /* XXX the use of state here seems questionable about matching up with the linkset channels */
  472. for (i = 0; i < linkset->numchans; i++) {
  473. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  474. if (state) {
  475. if (state[i])
  476. sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
  477. } else
  478. sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
  479. }
  480. }
  481. }
  482. static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  483. {
  484. int i;
  485. for (i = 0; i < linkset->numchans; i++) {
  486. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
  487. sig_ss7_set_inservice(linkset->pvts[i], 1);
  488. }
  489. }
  490. static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
  491. {
  492. int i, startcic = -1, endcic, dpc;
  493. if (linkset->numchans <= 0)
  494. return;
  495. startcic = linkset->pvts[0]->cic;
  496. /* DB: CIC's DPC fix */
  497. dpc = linkset->pvts[0]->dpc;
  498. for (i = 0; i < linkset->numchans; i++) {
  499. if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
  500. continue;
  501. } else {
  502. endcic = linkset->pvts[i]->cic;
  503. ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
  504. isup_grs(linkset->ss7, startcic, endcic, dpc);
  505. /* DB: CIC's DPC fix */
  506. if (linkset->pvts[i+1]) {
  507. startcic = linkset->pvts[i+1]->cic;
  508. dpc = linkset->pvts[i+1]->dpc;
  509. }
  510. }
  511. }
  512. }
  513. /* This function is assumed to be called with the private channel lock and linkset lock held */
  514. static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
  515. {
  516. struct ss7 *ss7 = linkset->ss7;
  517. int law;
  518. struct ast_channel *c;
  519. char tmp[256];
  520. struct ast_callid *callid = NULL;
  521. int callid_created = ast_callid_threadstorage_auto(&callid);
  522. if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
  523. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  524. isup_acm(ss7, p->ss7call);
  525. } else {
  526. p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
  527. }
  528. /* Companding law is determined by SS7 signaling type. */
  529. if (linkset->type == SS7_ITU) {
  530. law = SIG_SS7_ALAW;
  531. } else {
  532. law = SIG_SS7_ULAW;
  533. }
  534. /*
  535. * Release the SS7 lock while we create the channel so other
  536. * threads can send messages. We must also release the private
  537. * lock to prevent deadlock while creating the channel.
  538. */
  539. ast_mutex_unlock(&linkset->lock);
  540. sig_ss7_unlock_private(p);
  541. c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL, NULL);
  542. if (!c) {
  543. ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
  544. ast_mutex_lock(&linkset->lock);
  545. sig_ss7_lock_private(p);
  546. isup_rel(linkset->ss7, p->ss7call, AST_CAUSE_SWITCH_CONGESTION);
  547. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  548. p->alreadyhungup = 1;
  549. ast_callid_threadstorage_auto_clean(callid, callid_created);
  550. return;
  551. }
  552. /* Hold the channel and private lock while we setup the channel. */
  553. ast_channel_lock(c);
  554. sig_ss7_lock_private(p);
  555. sig_ss7_set_echocanceller(p, 1);
  556. ast_channel_stage_snapshot(c);
  557. /*
  558. * It is reasonably safe to set the following
  559. * channel variables while the channel private
  560. * structure is locked. The PBX has not been
  561. * started yet and it is unlikely that any other task
  562. * will do anything with the channel we have just
  563. * created.
  564. *
  565. * We only reference these variables in the context of the ss7_linkset function
  566. * when receiving either and IAM or a COT message.
  567. */
  568. if (!ast_strlen_zero(p->charge_number)) {
  569. pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
  570. /* Clear this after we set it */
  571. p->charge_number[0] = 0;
  572. }
  573. if (!ast_strlen_zero(p->gen_add_number)) {
  574. pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
  575. /* Clear this after we set it */
  576. p->gen_add_number[0] = 0;
  577. }
  578. if (!ast_strlen_zero(p->jip_number)) {
  579. pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
  580. /* Clear this after we set it */
  581. p->jip_number[0] = 0;
  582. }
  583. if (!ast_strlen_zero(p->gen_dig_number)) {
  584. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
  585. /* Clear this after we set it */
  586. p->gen_dig_number[0] = 0;
  587. }
  588. if (!ast_strlen_zero(p->orig_called_num)) {
  589. pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
  590. /* Clear this after we set it */
  591. p->orig_called_num[0] = 0;
  592. }
  593. snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
  594. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
  595. /* Clear this after we set it */
  596. p->gen_dig_type = 0;
  597. snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
  598. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
  599. /* Clear this after we set it */
  600. p->gen_dig_scheme = 0;
  601. if (!ast_strlen_zero(p->lspi_ident)) {
  602. pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
  603. /* Clear this after we set it */
  604. p->lspi_ident[0] = 0;
  605. }
  606. snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
  607. pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
  608. /* Clear this after we set it */
  609. p->call_ref_ident = 0;
  610. snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
  611. pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
  612. /* Clear this after we set it */
  613. p->call_ref_pc = 0;
  614. snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
  615. pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
  616. /* Clear this after we set it */
  617. p->calling_party_cat = 0;
  618. if (!ast_strlen_zero(p->redirecting_num)) {
  619. pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
  620. /* Clear this after we set it */
  621. p->redirecting_num[0] = 0;
  622. }
  623. if (!ast_strlen_zero(p->generic_name)) {
  624. pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
  625. /* Clear this after we set it */
  626. p->generic_name[0] = 0;
  627. }
  628. ast_channel_stage_snapshot_done(c);
  629. sig_ss7_unlock_private(p);
  630. ast_channel_unlock(c);
  631. if (ast_pbx_start(c)) {
  632. ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", ast_channel_name(c), p->cic);
  633. ast_hangup(c);
  634. } else {
  635. ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
  636. }
  637. /* Must return with linkset and private lock. */
  638. ast_mutex_lock(&linkset->lock);
  639. sig_ss7_lock_private(p);
  640. ast_callid_threadstorage_auto_clean(callid, callid_created);
  641. }
  642. static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
  643. {
  644. if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
  645. if (size) {
  646. *buf = '\0';
  647. }
  648. return;
  649. }
  650. switch (nai) {
  651. case SS7_NAI_INTERNATIONAL:
  652. snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
  653. break;
  654. case SS7_NAI_NATIONAL:
  655. snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
  656. break;
  657. case SS7_NAI_SUBSCRIBER:
  658. snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
  659. break;
  660. case SS7_NAI_UNKNOWN:
  661. snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
  662. break;
  663. default:
  664. snprintf(buf, size, "%s", number);
  665. break;
  666. }
  667. }
  668. static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
  669. {
  670. return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
  671. }
  672. /*!
  673. * \internal
  674. * \brief Set callid threadstorage for the ss7_linkset thread to that of an existing channel
  675. *
  676. * \param linkset ss7 span control structure.
  677. * \param chanpos channel position in the span
  678. *
  679. * \note Assumes the ss7->lock is already obtained.
  680. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  681. *
  682. * \return a reference to the callid bound to the channel which has also
  683. * been bound to threadstorage if it exists. If this returns non-NULL,
  684. * the callid must be unreffed and the threadstorage should be unbound
  685. * before the while loop wraps in ss7_linkset.
  686. */
  687. static struct ast_callid *func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
  688. {
  689. struct ast_callid *callid = NULL;
  690. sig_ss7_lock_owner(linkset, chanpos);
  691. if (linkset->pvts[chanpos]->owner) {
  692. callid = ast_channel_callid(linkset->pvts[chanpos]->owner);
  693. ast_channel_unlock(linkset->pvts[chanpos]->owner);
  694. if (callid) {
  695. ast_callid_threadassoc_add(callid);
  696. }
  697. }
  698. return callid;
  699. }
  700. /* This is a thread per linkset that handles all received events from libss7. */
  701. void *ss7_linkset(void *data)
  702. {
  703. int res, i;
  704. struct timeval *next = NULL, tv;
  705. struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
  706. struct ss7 *ss7 = linkset->ss7;
  707. ss7_event *e = NULL;
  708. struct sig_ss7_chan *p;
  709. struct pollfd pollers[SIG_SS7_NUM_DCHANS];
  710. int nextms;
  711. #define SS7_MAX_POLL 60000 /* Maximum poll time in ms. */
  712. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  713. ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
  714. ast_mutex_lock(&linkset->lock);
  715. ss7_start(ss7);
  716. ast_mutex_unlock(&linkset->lock);
  717. for (;;) {
  718. ast_mutex_lock(&linkset->lock);
  719. if ((next = ss7_schedule_next(ss7))) {
  720. tv = ast_tvnow();
  721. tv.tv_sec = next->tv_sec - tv.tv_sec;
  722. tv.tv_usec = next->tv_usec - tv.tv_usec;
  723. if (tv.tv_usec < 0) {
  724. tv.tv_usec += 1000000;
  725. tv.tv_sec -= 1;
  726. }
  727. if (tv.tv_sec < 0) {
  728. tv.tv_sec = 0;
  729. tv.tv_usec = 0;
  730. }
  731. nextms = tv.tv_sec * 1000;
  732. nextms += tv.tv_usec / 1000;
  733. if (SS7_MAX_POLL < nextms) {
  734. nextms = SS7_MAX_POLL;
  735. }
  736. } else {
  737. nextms = SS7_MAX_POLL;
  738. }
  739. for (i = 0; i < linkset->numsigchans; i++) {
  740. pollers[i].fd = linkset->fds[i];
  741. pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
  742. pollers[i].revents = 0;
  743. }
  744. ast_mutex_unlock(&linkset->lock);
  745. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  746. pthread_testcancel();
  747. res = poll(pollers, linkset->numsigchans, nextms);
  748. pthread_testcancel();
  749. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  750. if ((res < 0) && (errno != EINTR)) {
  751. ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
  752. } else if (!res) {
  753. ast_mutex_lock(&linkset->lock);
  754. ss7_schedule_run(ss7);
  755. ast_mutex_unlock(&linkset->lock);
  756. continue;
  757. }
  758. ast_mutex_lock(&linkset->lock);
  759. for (i = 0; i < linkset->numsigchans; i++) {
  760. if (pollers[i].revents & POLLPRI) {
  761. sig_ss7_handle_link_exception(linkset, i);
  762. }
  763. if (pollers[i].revents & POLLIN) {
  764. res = ss7_read(ss7, pollers[i].fd);
  765. }
  766. if (pollers[i].revents & POLLOUT) {
  767. res = ss7_write(ss7, pollers[i].fd);
  768. if (res < 0) {
  769. ast_debug(1, "Error in write %s\n", strerror(errno));
  770. }
  771. }
  772. }
  773. while ((e = ss7_check_event(ss7))) {
  774. struct ast_callid *callid = NULL;
  775. int chanpos = -1;
  776. char cause_str[30];
  777. if (linkset->debug) {
  778. ast_verbose("Linkset %d: Processing event: %s\n",
  779. linkset->span, ss7_event2str(e->e));
  780. }
  781. switch (e->e) {
  782. case SS7_EVENT_UP:
  783. if (linkset->state != LINKSET_STATE_UP) {
  784. ast_verbose("--- SS7 Up ---\n");
  785. ss7_reset_linkset(linkset);
  786. }
  787. linkset->state = LINKSET_STATE_UP;
  788. break;
  789. case SS7_EVENT_DOWN:
  790. ast_verbose("--- SS7 Down ---\n");
  791. linkset->state = LINKSET_STATE_DOWN;
  792. for (i = 0; i < linkset->numchans; i++) {
  793. p = linkset->pvts[i];
  794. if (p) {
  795. sig_ss7_set_alarm(p, 1);
  796. }
  797. }
  798. break;
  799. case MTP2_LINK_UP:
  800. ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
  801. break;
  802. case MTP2_LINK_DOWN:
  803. ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
  804. break;
  805. case ISUP_EVENT_CPG:
  806. chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
  807. if (chanpos < 0) {
  808. break;
  809. }
  810. p = linkset->pvts[chanpos];
  811. sig_ss7_lock_private(p);
  812. callid = func_ss7_linkset_callid(linkset, chanpos);
  813. switch (e->cpg.event) {
  814. case CPG_EVENT_ALERTING:
  815. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
  816. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  817. }
  818. sig_ss7_lock_owner(linkset, chanpos);
  819. if (p->owner) {
  820. ast_setstate(p->owner, AST_STATE_RINGING);
  821. ast_channel_unlock(p->owner);
  822. }
  823. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
  824. break;
  825. case CPG_EVENT_PROGRESS:
  826. case CPG_EVENT_INBANDINFO:
  827. {
  828. ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
  829. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
  830. p->progress = 1;
  831. sig_ss7_set_dialing(p, 0);
  832. sig_ss7_open_media(p);
  833. }
  834. break;
  835. default:
  836. ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
  837. break;
  838. }
  839. sig_ss7_unlock_private(p);
  840. break;
  841. case ISUP_EVENT_RSC:
  842. ast_verbose("Resetting CIC %d\n", e->rsc.cic);
  843. chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
  844. if (chanpos < 0) {
  845. break;
  846. }
  847. p = linkset->pvts[chanpos];
  848. sig_ss7_lock_private(p);
  849. callid = func_ss7_linkset_callid(linkset, chanpos);
  850. sig_ss7_set_inservice(p, 1);
  851. sig_ss7_set_remotelyblocked(p, 0);
  852. isup_set_call_dpc(e->rsc.call, p->dpc);
  853. sig_ss7_lock_owner(linkset, chanpos);
  854. p->ss7call = NULL;
  855. if (p->owner) {
  856. ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_RSC", AST_CAUSE_INTERWORKING);
  857. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  858. ast_channel_unlock(p->owner);
  859. }
  860. sig_ss7_unlock_private(p);
  861. isup_rlc(ss7, e->rsc.call);
  862. break;
  863. case ISUP_EVENT_GRS:
  864. ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
  865. chanpos = ss7_find_cic_gripe(linkset, e->grs.startcic, e->grs.opc, "GRS");
  866. if (chanpos < 0) {
  867. break;
  868. }
  869. p = linkset->pvts[chanpos];
  870. isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
  871. ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
  872. ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
  873. break;
  874. case ISUP_EVENT_CQM:
  875. ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
  876. ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
  877. break;
  878. case ISUP_EVENT_GRA:
  879. ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
  880. ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
  881. ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
  882. break;
  883. case ISUP_EVENT_IAM:
  884. 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);
  885. chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
  886. if (chanpos < 0) {
  887. isup_rel(ss7, e->iam.call, -1);
  888. break;
  889. }
  890. p = linkset->pvts[chanpos];
  891. sig_ss7_lock_private(p);
  892. sig_ss7_lock_owner(linkset, chanpos);
  893. if (p->call_level != SIG_SS7_CALL_LEVEL_IDLE) {
  894. /*
  895. * Detected glare/dual-seizure
  896. *
  897. * Always abort both calls since we can't implement the dual
  898. * seizure procedures due to our channel assignment architecture
  899. * and the fact that we cannot tell libss7 to discard its call
  900. * structure to ignore the incoming IAM.
  901. */
  902. ast_debug(1,
  903. "Linkset %d: SS7 IAM glare on CIC/DPC %d/%d. Dropping both calls.\n",
  904. linkset->span, e->iam.cic, e->iam.opc);
  905. if (p->call_level == SIG_SS7_CALL_LEVEL_ALLOCATED) {
  906. /*
  907. * We have not sent our IAM yet and we never will at this point.
  908. */
  909. p->alreadyhungup = 1;
  910. isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  911. }
  912. p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
  913. if (p->owner) {
  914. ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_IAM (glare)", AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  915. ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  916. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  917. ast_channel_unlock(p->owner);
  918. }
  919. sig_ss7_unlock_private(p);
  920. break;
  921. }
  922. /*
  923. * The channel should not have an owner at this point since we
  924. * are in the process of creating an owner for it.
  925. */
  926. ast_assert(!p->owner);
  927. if (!sig_ss7_is_chan_available(p)) {
  928. /* Circuit is likely blocked or in alarm. */
  929. isup_rel(ss7, e->iam.call, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  930. sig_ss7_unlock_private(p);
  931. break;
  932. }
  933. /* Mark channel as in use so no outgoing call will steal it. */
  934. p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
  935. p->ss7call = e->iam.call;
  936. isup_set_call_dpc(p->ss7call, p->dpc);
  937. if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
  938. ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
  939. p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
  940. } else
  941. p->cid_num[0] = 0;
  942. /* Set DNID */
  943. if (!ast_strlen_zero(e->iam.called_party_num)) {
  944. ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
  945. e->iam.called_party_num, e->iam.called_nai);
  946. } else {
  947. p->exten[0] = '\0';
  948. }
  949. sig_ss7_set_dnid(p, p->exten);
  950. if (p->immediate) {
  951. p->exten[0] = 's';
  952. p->exten[1] = '\0';
  953. } else if (!ast_strlen_zero(e->iam.called_party_num)) {
  954. char *st;
  955. ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
  956. st = strchr(p->exten, '#');
  957. if (st) {
  958. *st = '\0';
  959. }
  960. } else {
  961. p->exten[0] = '\0';
  962. }
  963. p->cid_ani[0] = '\0';
  964. if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
  965. ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
  966. else
  967. p->cid_name[0] = '\0';
  968. p->cid_ani2 = e->iam.oli_ani2;
  969. p->cid_ton = 0;
  970. ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
  971. ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
  972. p->gen_add_type = e->iam.gen_add_type;
  973. p->gen_add_nai = e->iam.gen_add_nai;
  974. p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
  975. p->gen_add_num_plan = e->iam.gen_add_num_plan;
  976. ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
  977. p->gen_dig_type = e->iam.gen_dig_type;
  978. p->gen_dig_scheme = e->iam.gen_dig_scheme;
  979. ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
  980. ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
  981. ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
  982. ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
  983. p->calling_party_cat = e->iam.calling_party_cat;
  984. sig_ss7_set_caller_id(p);
  985. if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
  986. if (e->iam.cot_check_required) {
  987. p->call_level = SIG_SS7_CALL_LEVEL_CONTINUITY;
  988. sig_ss7_loopback(p, 1);
  989. } else {
  990. ss7_start_call(p, linkset);
  991. }
  992. } else {
  993. ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
  994. p->alreadyhungup = 1;
  995. isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
  996. }
  997. sig_ss7_unlock_private(p);
  998. break;
  999. case ISUP_EVENT_COT:
  1000. chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
  1001. if (chanpos < 0) {
  1002. isup_rel(ss7, e->cot.call, -1);
  1003. break;
  1004. }
  1005. p = linkset->pvts[chanpos];
  1006. sig_ss7_lock_private(p);
  1007. if (p->loopedback) {
  1008. sig_ss7_loopback(p, 0);
  1009. ss7_start_call(p, linkset);
  1010. }
  1011. sig_ss7_unlock_private(p);
  1012. break;
  1013. case ISUP_EVENT_CCR:
  1014. ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
  1015. chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
  1016. if (chanpos < 0) {
  1017. break;
  1018. }
  1019. p = linkset->pvts[chanpos];
  1020. sig_ss7_lock_private(p);
  1021. sig_ss7_loopback(p, 1);
  1022. sig_ss7_unlock_private(p);
  1023. isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
  1024. break;
  1025. case ISUP_EVENT_CVT:
  1026. ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
  1027. chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
  1028. if (chanpos < 0) {
  1029. break;
  1030. }
  1031. p = linkset->pvts[chanpos];
  1032. sig_ss7_lock_private(p);
  1033. sig_ss7_loopback(p, 1);
  1034. sig_ss7_unlock_private(p);
  1035. isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
  1036. break;
  1037. case ISUP_EVENT_REL:
  1038. chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
  1039. if (chanpos < 0) {
  1040. /* Continue hanging up the call anyway. */
  1041. isup_rlc(ss7, e->rel.call);
  1042. break;
  1043. }
  1044. p = linkset->pvts[chanpos];
  1045. sig_ss7_lock_private(p);
  1046. callid = func_ss7_linkset_callid(linkset, chanpos);
  1047. sig_ss7_lock_owner(linkset, chanpos);
  1048. if (p->owner) {
  1049. snprintf(cause_str, sizeof(cause_str), "SS7 ISUP_EVENT_REL (%d)", e->rel.cause);
  1050. ss7_queue_pvt_cause_data(p->owner, cause_str, e->rel.cause);
  1051. ast_channel_hangupcause_set(p->owner, e->rel.cause);
  1052. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  1053. ast_channel_unlock(p->owner);
  1054. }
  1055. /* End the loopback if we have one */
  1056. sig_ss7_loopback(p, 0);
  1057. isup_rlc(ss7, e->rel.call);
  1058. p->ss7call = NULL;
  1059. sig_ss7_unlock_private(p);
  1060. break;
  1061. case ISUP_EVENT_ACM:
  1062. chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
  1063. if (chanpos < 0) {
  1064. isup_rel(ss7, e->acm.call, -1);
  1065. break;
  1066. }
  1067. {
  1068. p = linkset->pvts[chanpos];
  1069. ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
  1070. if (e->acm.call_ref_ident > 0) {
  1071. p->rlt = 1; /* Setting it but not using it here*/
  1072. }
  1073. sig_ss7_lock_private(p);
  1074. callid = func_ss7_linkset_callid(linkset, chanpos);
  1075. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
  1076. if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
  1077. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  1078. }
  1079. sig_ss7_set_dialing(p, 0);
  1080. /* Send alerting if subscriber is free */
  1081. if (e->acm.called_party_status_ind == 1) {
  1082. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
  1083. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  1084. }
  1085. sig_ss7_lock_owner(linkset, chanpos);
  1086. if (p->owner) {
  1087. ast_setstate(p->owner, AST_STATE_RINGING);
  1088. ast_channel_unlock(p->owner);
  1089. }
  1090. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
  1091. }
  1092. sig_ss7_unlock_private(p);
  1093. }
  1094. break;
  1095. case ISUP_EVENT_CGB:
  1096. chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
  1097. if (chanpos < 0) {
  1098. break;
  1099. }
  1100. p = linkset->pvts[chanpos];
  1101. ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
  1102. isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
  1103. break;
  1104. case ISUP_EVENT_CGU:
  1105. chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
  1106. if (chanpos < 0) {
  1107. break;
  1108. }
  1109. p = linkset->pvts[chanpos];
  1110. ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
  1111. isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
  1112. break;
  1113. case ISUP_EVENT_UCIC:
  1114. chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
  1115. if (chanpos < 0) {
  1116. break;
  1117. }
  1118. p = linkset->pvts[chanpos];
  1119. ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
  1120. sig_ss7_lock_private(p);
  1121. sig_ss7_set_remotelyblocked(p, 1);
  1122. sig_ss7_set_inservice(p, 0);
  1123. sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
  1124. break;
  1125. case ISUP_EVENT_BLO:
  1126. chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
  1127. if (chanpos < 0) {
  1128. break;
  1129. }
  1130. p = linkset->pvts[chanpos];
  1131. ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
  1132. sig_ss7_lock_private(p);
  1133. sig_ss7_set_remotelyblocked(p, 1);
  1134. sig_ss7_unlock_private(p);
  1135. isup_bla(linkset->ss7, e->blo.cic, p->dpc);
  1136. break;
  1137. case ISUP_EVENT_BLA:
  1138. chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
  1139. if (chanpos < 0) {
  1140. break;
  1141. }
  1142. ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
  1143. p = linkset->pvts[chanpos];
  1144. sig_ss7_lock_private(p);
  1145. sig_ss7_set_locallyblocked(p, 1);
  1146. sig_ss7_unlock_private(p);
  1147. break;
  1148. case ISUP_EVENT_UBL:
  1149. chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
  1150. if (chanpos < 0) {
  1151. break;
  1152. }
  1153. p = linkset->pvts[chanpos];
  1154. ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
  1155. sig_ss7_lock_private(p);
  1156. sig_ss7_set_remotelyblocked(p, 0);
  1157. sig_ss7_unlock_private(p);
  1158. isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
  1159. break;
  1160. case ISUP_EVENT_UBA:
  1161. chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
  1162. if (chanpos < 0) {
  1163. break;
  1164. }
  1165. p = linkset->pvts[chanpos];
  1166. ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
  1167. sig_ss7_lock_private(p);
  1168. sig_ss7_set_locallyblocked(p, 0);
  1169. sig_ss7_unlock_private(p);
  1170. break;
  1171. case ISUP_EVENT_CON:
  1172. case ISUP_EVENT_ANM:
  1173. if (e->e == ISUP_EVENT_CON) {
  1174. chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
  1175. if (chanpos < 0) {
  1176. isup_rel(ss7, e->con.call, -1);
  1177. break;
  1178. }
  1179. } else {
  1180. chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
  1181. if (chanpos < 0) {
  1182. isup_rel(ss7, e->anm.call, -1);
  1183. break;
  1184. }
  1185. }
  1186. {
  1187. p = linkset->pvts[chanpos];
  1188. sig_ss7_lock_private(p);
  1189. callid = func_ss7_linkset_callid(linkset, chanpos);
  1190. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1191. p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
  1192. }
  1193. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
  1194. sig_ss7_set_dialing(p, 0);
  1195. sig_ss7_open_media(p);
  1196. sig_ss7_set_echocanceller(p, 1);
  1197. sig_ss7_unlock_private(p);
  1198. }
  1199. break;
  1200. case ISUP_EVENT_RLC:
  1201. /* XXX Call ptr should be passed up from libss7! */
  1202. chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
  1203. if (chanpos < 0) {
  1204. break;
  1205. }
  1206. {
  1207. p = linkset->pvts[chanpos];
  1208. sig_ss7_lock_private(p);
  1209. callid = func_ss7_linkset_callid(linkset, chanpos);
  1210. if (p->alreadyhungup) {
  1211. if (!p->owner) {
  1212. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1213. }
  1214. p->ss7call = NULL;
  1215. }
  1216. sig_ss7_unlock_private(p);
  1217. }
  1218. break;
  1219. case ISUP_EVENT_FAA:
  1220. /*!
  1221. * \todo The handling of the SS7 FAA message is not good and I
  1222. * don't know enough to handle it correctly.
  1223. */
  1224. chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
  1225. if (chanpos < 0) {
  1226. isup_rel(linkset->ss7, e->faa.call, -1);
  1227. break;
  1228. }
  1229. {
  1230. /* XXX FAR and FAA used for something dealing with transfers? */
  1231. p = linkset->pvts[chanpos];
  1232. ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
  1233. sig_ss7_lock_private(p);
  1234. callid = func_ss7_linkset_callid(linkset, chanpos);
  1235. if (p->alreadyhungup){
  1236. if (!p->owner) {
  1237. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1238. }
  1239. /* XXX We seem to be leaking the isup call structure here. */
  1240. p->ss7call = NULL;
  1241. ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
  1242. }
  1243. sig_ss7_unlock_private(p);
  1244. }
  1245. break;
  1246. default:
  1247. ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
  1248. break;
  1249. }
  1250. /* Call ID stuff needs to be cleaned up here */
  1251. if (callid) {
  1252. callid = ast_callid_unref(callid);
  1253. ast_callid_threadassoc_remove();
  1254. }
  1255. }
  1256. ast_mutex_unlock(&linkset->lock);
  1257. }
  1258. return 0;
  1259. }
  1260. static inline void ss7_rel(struct sig_ss7_linkset *ss7)
  1261. {
  1262. ast_mutex_unlock(&ss7->lock);
  1263. }
  1264. static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
  1265. {
  1266. /* Grab the lock first */
  1267. while (ast_mutex_trylock(&ss7->lock)) {
  1268. /* Avoid deadlock */
  1269. sig_ss7_deadlock_avoidance_private(pvt);
  1270. }
  1271. /* Then break the poll */
  1272. if (ss7->master != AST_PTHREADT_NULL) {
  1273. pthread_kill(ss7->master, SIGURG);
  1274. }
  1275. }
  1276. /*!
  1277. * \brief Notify the SS7 layer that the link is in alarm.
  1278. * \since 1.8
  1279. *
  1280. * \param linkset Controlling linkset for the channel.
  1281. * \param which Link index of the signaling channel.
  1282. *
  1283. * \return Nothing
  1284. */
  1285. void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
  1286. {
  1287. linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
  1288. linkset->linkstate[which] &= ~LINKSTATE_UP;
  1289. ss7_link_alarm(linkset->ss7, linkset->fds[which]);
  1290. }
  1291. /*!
  1292. * \brief Notify the SS7 layer that the link is no longer in alarm.
  1293. * \since 1.8
  1294. *
  1295. * \param linkset Controlling linkset for the channel.
  1296. * \param which Link index of the signaling channel.
  1297. *
  1298. * \return Nothing
  1299. */
  1300. void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
  1301. {
  1302. linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
  1303. linkset->linkstate[which] |= LINKSTATE_STARTING;
  1304. ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
  1305. }
  1306. /*!
  1307. * \brief Setup and add a SS7 link channel.
  1308. * \since 1.8
  1309. *
  1310. * \param linkset Controlling linkset for the channel.
  1311. * \param which Link index of the signaling channel.
  1312. * \param ss7type Switch type of the linkset
  1313. * \param transport Signaling transport of channel.
  1314. * \param inalarm Non-zero if the channel is in alarm.
  1315. * \param networkindicator User configuration parameter.
  1316. * \param pointcode User configuration parameter.
  1317. * \param adjpointcode User configuration parameter.
  1318. *
  1319. * \retval 0 on success.
  1320. * \retval -1 on error.
  1321. */
  1322. int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
  1323. {
  1324. if (!linkset->ss7) {
  1325. linkset->type = ss7type;
  1326. linkset->ss7 = ss7_new(ss7type);
  1327. if (!linkset->ss7) {
  1328. ast_log(LOG_ERROR, "Can't create new SS7!\n");
  1329. return -1;
  1330. }
  1331. }
  1332. ss7_set_network_ind(linkset->ss7, networkindicator);
  1333. ss7_set_pc(linkset->ss7, pointcode);
  1334. if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
  1335. ast_log(LOG_WARNING, "Could not add SS7 link!\n");
  1336. }
  1337. if (inalarm) {
  1338. linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
  1339. ss7_link_alarm(linkset->ss7, linkset->fds[which]);
  1340. } else {
  1341. linkset->linkstate[which] = LINKSTATE_DOWN;
  1342. ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
  1343. }
  1344. ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
  1345. return 0;
  1346. }
  1347. /*!
  1348. * \brief Determine if the specified channel is available for an outgoing call.
  1349. * \since 1.8
  1350. *
  1351. * \param p Signaling private structure pointer.
  1352. *
  1353. * \retval TRUE if the channel is available.
  1354. */
  1355. int sig_ss7_available(struct sig_ss7_chan *p)
  1356. {
  1357. int available;
  1358. if (!p->ss7) {
  1359. /* Something is wrong here. A SS7 channel without the ss7 pointer? */
  1360. return 0;
  1361. }
  1362. /* Only have to deal with the linkset lock. */
  1363. ast_mutex_lock(&p->ss7->lock);
  1364. available = sig_ss7_is_chan_available(p);
  1365. if (available) {
  1366. p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
  1367. }
  1368. ast_mutex_unlock(&p->ss7->lock);
  1369. return available;
  1370. }
  1371. static unsigned char cid_pres2ss7pres(int cid_pres)
  1372. {
  1373. return (cid_pres >> 5) & 0x03;
  1374. }
  1375. static unsigned char cid_pres2ss7screen(int cid_pres)
  1376. {
  1377. return cid_pres & 0x03;
  1378. }
  1379. /*!
  1380. * \brief Dial out using the specified SS7 channel.
  1381. * \since 1.8
  1382. *
  1383. * \param p Signaling private structure pointer.
  1384. * \param ast Asterisk channel structure pointer.
  1385. * \param rdest Dialstring.
  1386. *
  1387. * \retval 0 on success.
  1388. * \retval -1 on error.
  1389. */
  1390. int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
  1391. {
  1392. char ss7_called_nai;
  1393. int called_nai_strip;
  1394. char ss7_calling_nai;
  1395. int calling_nai_strip;
  1396. const char *charge_str = NULL;
  1397. const char *gen_address = NULL;
  1398. const char *gen_digits = NULL;
  1399. const char *gen_dig_type = NULL;
  1400. const char *gen_dig_scheme = NULL;
  1401. const char *gen_name = NULL;
  1402. const char *jip_digits = NULL;
  1403. const char *lspi_ident = NULL;
  1404. const char *rlt_flag = NULL;
  1405. const char *call_ref_id = NULL;
  1406. const char *call_ref_pc = NULL;
  1407. const char *send_far = NULL;
  1408. char *c;
  1409. char *l;
  1410. char dest[256];
  1411. ast_copy_string(dest, rdest, sizeof(dest));
  1412. c = strchr(dest, '/');
  1413. if (c) {
  1414. c++;
  1415. } else {
  1416. c = "";
  1417. }
  1418. if (strlen(c) < p->stripmsd) {
  1419. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  1420. return -1;
  1421. }
  1422. if (!p->hidecallerid) {
  1423. l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
  1424. } else {
  1425. l = NULL;
  1426. }
  1427. ss7_grab(p, p->ss7);
  1428. if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
  1429. /* Call collision before sending IAM. Abort call. */
  1430. ss7_rel(p->ss7);
  1431. return -1;
  1432. }
  1433. p->ss7call = isup_new_call(p->ss7->ss7);
  1434. if (!p->ss7call) {
  1435. ss7_rel(p->ss7);
  1436. ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
  1437. return -1;
  1438. }
  1439. called_nai_strip = 0;
  1440. ss7_called_nai = p->ss7->called_nai;
  1441. if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
  1442. if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
  1443. called_nai_strip = strlen(p->ss7->internationalprefix);
  1444. ss7_called_nai = SS7_NAI_INTERNATIONAL;
  1445. } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
  1446. called_nai_strip = strlen(p->ss7->nationalprefix);
  1447. ss7_called_nai = SS7_NAI_NATIONAL;
  1448. } else {
  1449. ss7_called_nai = SS7_NAI_SUBSCRIBER;
  1450. }
  1451. }
  1452. isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
  1453. calling_nai_strip = 0;
  1454. ss7_calling_nai = p->ss7->calling_nai;
  1455. if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
  1456. if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
  1457. calling_nai_strip = strlen(p->ss7->internationalprefix);
  1458. ss7_calling_nai = SS7_NAI_INTERNATIONAL;
  1459. } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
  1460. calling_nai_strip = strlen(p->ss7->nationalprefix);
  1461. ss7_calling_nai = SS7_NAI_NATIONAL;
  1462. } else {
  1463. ss7_calling_nai = SS7_NAI_SUBSCRIBER;
  1464. }
  1465. }
  1466. isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
  1467. p->use_callingpres ? cid_pres2ss7pres(ast_channel_connected(ast)->id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
  1468. p->use_callingpres ? cid_pres2ss7screen(ast_channel_connected(ast)->id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
  1469. isup_set_oli(p->ss7call, ast_channel_connected(ast)->ani2);
  1470. isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
  1471. /* Set the charge number if it is set */
  1472. charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
  1473. if (charge_str)
  1474. isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
  1475. gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
  1476. if (gen_address)
  1477. 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 */
  1478. gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
  1479. gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
  1480. gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
  1481. if (gen_digits)
  1482. isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
  1483. gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
  1484. if (gen_name)
  1485. isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
  1486. jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
  1487. if (jip_digits)
  1488. isup_set_jip_digits(p->ss7call, jip_digits);
  1489. lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
  1490. if (lspi_ident)
  1491. isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
  1492. rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
  1493. if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
  1494. isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
  1495. }
  1496. call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
  1497. call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
  1498. if (call_ref_id && call_ref_pc) {
  1499. isup_set_callref(p->ss7call, atoi(call_ref_id),
  1500. call_ref_pc ? atoi(call_ref_pc) : 0);
  1501. }
  1502. send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
  1503. if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
  1504. (isup_far(p->ss7->ss7, p->ss7call));
  1505. p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
  1506. isup_iam(p->ss7->ss7, p->ss7call);
  1507. sig_ss7_set_dialing(p, 1);
  1508. ast_setstate(ast, AST_STATE_DIALING);
  1509. ss7_rel(p->ss7);
  1510. return 0;
  1511. }
  1512. /*!
  1513. * \brief SS7 hangup channel.
  1514. * \since 1.8
  1515. *
  1516. * \param p Signaling private structure pointer.
  1517. * \param ast Asterisk channel structure pointer.
  1518. *
  1519. * \retval 0 on success.
  1520. * \retval -1 on error.
  1521. */
  1522. int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
  1523. {
  1524. int res = 0;
  1525. if (!ast_channel_tech_pvt(ast)) {
  1526. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  1527. return 0;
  1528. }
  1529. p->owner = NULL;
  1530. sig_ss7_set_dialing(p, 0);
  1531. sig_ss7_set_outgoing(p, 0);
  1532. p->progress = 0;
  1533. p->rlt = 0;
  1534. p->exten[0] = '\0';
  1535. /* Perform low level hangup if no owner left */
  1536. ss7_grab(p, p->ss7);
  1537. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1538. if (p->ss7call) {
  1539. if (!p->alreadyhungup) {
  1540. const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
  1541. int icause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : -1;
  1542. if (cause) {
  1543. if (atoi(cause)) {
  1544. icause = atoi(cause);
  1545. }
  1546. }
  1547. isup_rel(p->ss7->ss7, p->ss7call, icause);
  1548. p->alreadyhungup = 1;
  1549. }
  1550. }
  1551. ss7_rel(p->ss7);
  1552. return res;
  1553. }
  1554. /*!
  1555. * \brief SS7 answer channel.
  1556. * \since 1.8
  1557. *
  1558. * \param p Signaling private structure pointer.
  1559. * \param ast Asterisk channel structure pointer.
  1560. *
  1561. * \retval 0 on success.
  1562. * \retval -1 on error.
  1563. */
  1564. int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
  1565. {
  1566. int res;
  1567. ss7_grab(p, p->ss7);
  1568. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1569. p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
  1570. }
  1571. sig_ss7_open_media(p);
  1572. res = isup_anm(p->ss7->ss7, p->ss7call);
  1573. ss7_rel(p->ss7);
  1574. return res;
  1575. }
  1576. /*!
  1577. * \brief Fix up a channel: If a channel is consumed, this is called. Basically update any ->owner links.
  1578. * \since 1.8
  1579. *
  1580. * \param oldchan Old channel pointer to replace.
  1581. * \param newchan New channel pointer to set.
  1582. * \param pchan Signaling private structure pointer.
  1583. *
  1584. * \return Nothing
  1585. */
  1586. void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
  1587. {
  1588. if (pchan->owner == oldchan) {
  1589. pchan->owner = newchan;
  1590. }
  1591. }
  1592. /*!
  1593. * \brief SS7 answer channel.
  1594. * \since 1.8
  1595. *
  1596. * \param p Signaling private structure pointer.
  1597. * \param chan Asterisk channel structure pointer.
  1598. * \param condition AST control frame subtype.
  1599. * \param data AST control frame payload contents.
  1600. * \param datalen Length of payload contents.
  1601. *
  1602. * \retval 0 on success.
  1603. * \retval -1 on error or indication condition not handled.
  1604. */
  1605. int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
  1606. {
  1607. int res = -1;
  1608. switch (condition) {
  1609. case AST_CONTROL_BUSY:
  1610. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1611. ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
  1612. ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
  1613. res = 0;
  1614. break;
  1615. }
  1616. res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
  1617. break;
  1618. case AST_CONTROL_RINGING:
  1619. ss7_grab(p, p->ss7);
  1620. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
  1621. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  1622. if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
  1623. p->rlt = 1;
  1624. }
  1625. /* No need to send CPG if call will be RELEASE */
  1626. if (p->rlt != 1) {
  1627. isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
  1628. }
  1629. }
  1630. ss7_rel(p->ss7);
  1631. res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
  1632. if (ast_channel_state(chan) != AST_STATE_UP && ast_channel_state(chan) != AST_STATE_RING) {
  1633. ast_setstate(chan, AST_STATE_RINGING);
  1634. }
  1635. break;
  1636. case AST_CONTROL_PROCEEDING:
  1637. ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",ast_channel_name(chan));
  1638. ss7_grab(p, p->ss7);
  1639. /* This IF sends the FAR for an answered ALEG call */
  1640. if (ast_channel_state(chan) == AST_STATE_UP && (p->rlt != 1)){
  1641. if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
  1642. p->rlt = 1;
  1643. }
  1644. }
  1645. if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
  1646. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  1647. isup_acm(p->ss7->ss7, p->ss7call);
  1648. }
  1649. ss7_rel(p->ss7);
  1650. /* don't continue in ast_indicate */
  1651. res = 0;
  1652. break;
  1653. case AST_CONTROL_PROGRESS:
  1654. ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",ast_channel_name(chan));
  1655. ss7_grab(p, p->ss7);
  1656. if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
  1657. p->progress = 1;/* No need to send inband-information progress again. */
  1658. isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
  1659. ss7_rel(p->ss7);
  1660. /* enable echo canceler here on SS7 calls */
  1661. sig_ss7_set_echocanceller(p, 1);
  1662. } else {
  1663. ss7_rel(p->ss7);
  1664. }
  1665. /* don't continue in ast_indicate */
  1666. res = 0;
  1667. break;
  1668. case AST_CONTROL_INCOMPLETE:
  1669. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1670. ast_channel_hangupcause_set(chan, AST_CAUSE_INVALID_NUMBER_FORMAT);
  1671. ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
  1672. res = 0;
  1673. break;
  1674. }
  1675. /* Wait for DTMF digits to complete the dialed number. */
  1676. res = 0;
  1677. break;
  1678. case AST_CONTROL_CONGESTION:
  1679. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1680. ast_channel_hangupcause_set(chan, AST_CAUSE_CONGESTION);
  1681. ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
  1682. res = 0;
  1683. break;
  1684. }
  1685. res = sig_ss7_play_tone(p, SIG_SS7_TONE_CONGESTION);
  1686. break;
  1687. case AST_CONTROL_HOLD:
  1688. ast_moh_start(chan, data, p->mohinterpret);
  1689. break;
  1690. case AST_CONTROL_UNHOLD:
  1691. ast_moh_stop(chan);
  1692. break;
  1693. case AST_CONTROL_SRCUPDATE:
  1694. res = 0;
  1695. break;
  1696. case -1:
  1697. res = sig_ss7_play_tone(p, -1);
  1698. break;
  1699. }
  1700. return res;
  1701. }
  1702. /*!
  1703. * \brief SS7 channel request.
  1704. * \since 1.8
  1705. *
  1706. * \param p Signaling private structure pointer.
  1707. * \param law Companding law preferred
  1708. * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
  1709. * \param transfercapability
  1710. *
  1711. * \retval ast_channel on success.
  1712. * \retval NULL on error.
  1713. */
  1714. struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law,
  1715. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,
  1716. int transfercapability)
  1717. {
  1718. struct ast_channel *ast;
  1719. /* Companding law is determined by SS7 signaling type. */
  1720. if (p->ss7->type == SS7_ITU) {
  1721. law = SIG_SS7_ALAW;
  1722. } else {
  1723. law = SIG_SS7_ULAW;
  1724. }
  1725. sig_ss7_set_outgoing(p, 1);
  1726. ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability,
  1727. p->exten, assignedids, requestor);
  1728. if (!ast) {
  1729. sig_ss7_set_outgoing(p, 0);
  1730. /* Release the allocated channel. Only have to deal with the linkset lock. */
  1731. ast_mutex_lock(&p->ss7->lock);
  1732. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1733. ast_mutex_unlock(&p->ss7->lock);
  1734. }
  1735. return ast;
  1736. }
  1737. /*!
  1738. * \brief Delete the sig_ss7 private channel structure.
  1739. * \since 1.8
  1740. *
  1741. * \param doomed sig_ss7 private channel structure to delete.
  1742. *
  1743. * \return Nothing
  1744. */
  1745. void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
  1746. {
  1747. ast_free(doomed);
  1748. }
  1749. #define SIG_SS7_SC_HEADER "%-4s %4s %-4s %-3s %-3s %-10s %-4s %s\n"
  1750. #define SIG_SS7_SC_LINE "%4d %4d %-4s %-3s %-3s %-10s %-4s %s"
  1751. void sig_ss7_cli_show_channels_header(int fd)
  1752. {
  1753. ast_cli(fd, SIG_SS7_SC_HEADER, "link", "", "Chan", "Lcl", "Rem", "Call", "SS7", "Channel");
  1754. ast_cli(fd, SIG_SS7_SC_HEADER, "set", "Chan", "Idle", "Blk", "Blk", "Level", "Call", "Name");
  1755. }
  1756. void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
  1757. {
  1758. char line[256];
  1759. int idx;
  1760. struct sig_ss7_chan *pvt;
  1761. ast_mutex_lock(&linkset->lock);
  1762. for (idx = 0; idx < linkset->numchans; ++idx) {
  1763. if (!linkset->pvts[idx]) {
  1764. continue;
  1765. }
  1766. pvt = linkset->pvts[idx];
  1767. sig_ss7_lock_private(pvt);
  1768. sig_ss7_lock_owner(linkset, idx);
  1769. snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
  1770. linkset->span,
  1771. pvt->channel,
  1772. sig_ss7_is_chan_available(pvt) ? "Yes" : "No",
  1773. pvt->locallyblocked ? "Yes" : "No",
  1774. pvt->remotelyblocked ? "Yes" : "No",
  1775. sig_ss7_call_level2str(pvt->call_level),
  1776. pvt->ss7call ? "Yes" : "No",
  1777. pvt->owner ? ast_channel_name(pvt->owner) : "");
  1778. if (pvt->owner) {
  1779. ast_channel_unlock(pvt->owner);
  1780. }
  1781. sig_ss7_unlock_private(pvt);
  1782. ast_mutex_unlock(&linkset->lock);
  1783. ast_cli(fd, "%s\n", line);
  1784. ast_mutex_lock(&linkset->lock);
  1785. }
  1786. ast_mutex_unlock(&linkset->lock);
  1787. }
  1788. /*!
  1789. * \brief Create a new sig_ss7 private channel structure.
  1790. * \since 1.8
  1791. *
  1792. * \param pvt_data Upper layer private data structure.
  1793. * \param ss7 Controlling linkset for the channel.
  1794. *
  1795. * \retval sig_ss7_chan on success.
  1796. * \retval NULL on error.
  1797. */
  1798. struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_linkset *ss7)
  1799. {
  1800. struct sig_ss7_chan *pvt;
  1801. pvt = ast_calloc(1, sizeof(*pvt));
  1802. if (!pvt) {
  1803. return pvt;
  1804. }
  1805. pvt->chan_pvt = pvt_data;
  1806. pvt->ss7 = ss7;
  1807. return pvt;
  1808. }
  1809. /*!
  1810. * \brief Initialize the SS7 linkset control.
  1811. * \since 1.8
  1812. *
  1813. * \param ss7 SS7 linkset control structure.
  1814. *
  1815. * \return Nothing
  1816. */
  1817. void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
  1818. {
  1819. int idx;
  1820. memset(ss7, 0, sizeof(*ss7));
  1821. ast_mutex_init(&ss7->lock);
  1822. ss7->master = AST_PTHREADT_NULL;
  1823. for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
  1824. ss7->fds[idx] = -1;
  1825. }
  1826. }
  1827. /* ------------------------------------------------------------------- */
  1828. #endif /* defined(HAVE_SS7) */
  1829. /* end sig_ss7.c */