isdn_divert.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* $Id: isdn_divert.c,v 1.6.6.3 2001/09/23 22:24:36 kai Exp $
  2. *
  3. * DSS1 main diversion supplementary handling for i4l.
  4. *
  5. * Copyright 1999 by Werner Cornelius (werner@isdn4linux.de)
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/proc_fs.h>
  12. #include <linux/timer.h>
  13. #include <linux/jiffies.h>
  14. #include "isdn_divert.h"
  15. /**********************************/
  16. /* structure keeping calling info */
  17. /**********************************/
  18. struct call_struc
  19. { isdn_ctrl ics; /* delivered setup + driver parameters */
  20. ulong divert_id; /* Id delivered to user */
  21. unsigned char akt_state; /* actual state */
  22. char deflect_dest[35]; /* deflection destination */
  23. struct timer_list timer; /* timer control structure */
  24. char info[90]; /* device info output */
  25. struct call_struc *next; /* pointer to next entry */
  26. struct call_struc *prev;
  27. };
  28. /********************************************/
  29. /* structure keeping deflection table entry */
  30. /********************************************/
  31. struct deflect_struc
  32. { struct deflect_struc *next,*prev;
  33. divert_rule rule; /* used rule */
  34. };
  35. /*****************************************/
  36. /* variables for main diversion services */
  37. /*****************************************/
  38. /* diversion/deflection processes */
  39. static struct call_struc *divert_head = NULL; /* head of remembered entrys */
  40. static ulong next_id = 1; /* next info id */
  41. static struct deflect_struc *table_head = NULL;
  42. static struct deflect_struc *table_tail = NULL;
  43. static unsigned char extern_wait_max = 4; /* maximum wait in s for external process */
  44. DEFINE_SPINLOCK(divert_lock);
  45. /***************************/
  46. /* timer callback function */
  47. /***************************/
  48. static void deflect_timer_expire(ulong arg)
  49. {
  50. unsigned long flags;
  51. struct call_struc *cs = (struct call_struc *) arg;
  52. spin_lock_irqsave(&divert_lock, flags);
  53. del_timer(&cs->timer); /* delete active timer */
  54. spin_unlock_irqrestore(&divert_lock, flags);
  55. switch(cs->akt_state)
  56. { case DEFLECT_PROCEED:
  57. cs->ics.command = ISDN_CMD_HANGUP; /* cancel action */
  58. divert_if.ll_cmd(&cs->ics);
  59. spin_lock_irqsave(&divert_lock, flags);
  60. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  61. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  62. add_timer(&cs->timer);
  63. spin_unlock_irqrestore(&divert_lock, flags);
  64. break;
  65. case DEFLECT_ALERT:
  66. cs->ics.command = ISDN_CMD_REDIR; /* protocol */
  67. strcpy(cs->ics.parm.setup.phone,cs->deflect_dest);
  68. strcpy(cs->ics.parm.setup.eazmsn,"Testtext delayed");
  69. divert_if.ll_cmd(&cs->ics);
  70. spin_lock_irqsave(&divert_lock, flags);
  71. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  72. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  73. add_timer(&cs->timer);
  74. spin_unlock_irqrestore(&divert_lock, flags);
  75. break;
  76. case DEFLECT_AUTODEL:
  77. default:
  78. spin_lock_irqsave(&divert_lock, flags);
  79. if (cs->prev)
  80. cs->prev->next = cs->next; /* forward link */
  81. else
  82. divert_head = cs->next;
  83. if (cs->next)
  84. cs->next->prev = cs->prev; /* back link */
  85. spin_unlock_irqrestore(&divert_lock, flags);
  86. kfree(cs);
  87. return;
  88. } /* switch */
  89. } /* deflect_timer_func */
  90. /*****************************************/
  91. /* handle call forwarding de/activations */
  92. /* 0 = deact, 1 = act, 2 = interrogate */
  93. /*****************************************/
  94. int cf_command(int drvid, int mode,
  95. u_char proc, char *msn,
  96. u_char service, char *fwd_nr, ulong *procid)
  97. { unsigned long flags;
  98. int retval,msnlen;
  99. int fwd_len;
  100. char *p,*ielenp,tmp[60];
  101. struct call_struc *cs;
  102. if (strchr(msn,'.')) return(-EINVAL); /* subaddress not allowed in msn */
  103. if ((proc & 0x7F) > 2) return(-EINVAL);
  104. proc &= 3;
  105. p = tmp;
  106. *p++ = 0x30; /* enumeration */
  107. ielenp = p++; /* remember total length position */
  108. *p++ = 0xa; /* proc tag */
  109. *p++ = 1; /* length */
  110. *p++ = proc & 0x7F; /* procedure to de/activate/interrogate */
  111. *p++ = 0xa; /* service tag */
  112. *p++ = 1; /* length */
  113. *p++ = service; /* service to handle */
  114. if (mode == 1)
  115. { if (!*fwd_nr) return(-EINVAL); /* destination missing */
  116. if (strchr(fwd_nr,'.')) return(-EINVAL); /* subaddress not allowed */
  117. fwd_len = strlen(fwd_nr);
  118. *p++ = 0x30; /* number enumeration */
  119. *p++ = fwd_len + 2; /* complete forward to len */
  120. *p++ = 0x80; /* fwd to nr */
  121. *p++ = fwd_len; /* length of number */
  122. strcpy(p,fwd_nr); /* copy number */
  123. p += fwd_len; /* pointer beyond fwd */
  124. } /* activate */
  125. msnlen = strlen(msn);
  126. *p++ = 0x80; /* msn number */
  127. if (msnlen > 1)
  128. { *p++ = msnlen; /* length */
  129. strcpy(p,msn);
  130. p += msnlen;
  131. }
  132. else *p++ = 0;
  133. *ielenp = p - ielenp - 1; /* set total IE length */
  134. /* allocate mem for information struct */
  135. if (!(cs = kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
  136. return(-ENOMEM); /* no memory */
  137. init_timer(&cs->timer);
  138. cs->info[0] = '\0';
  139. cs->timer.function = deflect_timer_expire;
  140. cs->timer.data = (ulong) cs; /* pointer to own structure */
  141. cs->ics.driver = drvid;
  142. cs->ics.command = ISDN_CMD_PROT_IO; /* protocol specific io */
  143. cs->ics.arg = DSS1_CMD_INVOKE; /* invoke supplementary service */
  144. cs->ics.parm.dss1_io.proc = (mode == 1) ? 7: (mode == 2) ? 11:8; /* operation */
  145. cs->ics.parm.dss1_io.timeout = 4000; /* from ETS 300 207-1 */
  146. cs->ics.parm.dss1_io.datalen = p - tmp; /* total len */
  147. cs->ics.parm.dss1_io.data = tmp; /* start of buffer */
  148. spin_lock_irqsave(&divert_lock, flags);
  149. cs->ics.parm.dss1_io.ll_id = next_id++; /* id for callback */
  150. spin_unlock_irqrestore(&divert_lock, flags);
  151. *procid = cs->ics.parm.dss1_io.ll_id;
  152. sprintf(cs->info,"%d 0x%lx %s%s 0 %s %02x %d%s%s\n",
  153. (!mode ) ? DIVERT_DEACTIVATE : (mode == 1) ? DIVERT_ACTIVATE : DIVERT_REPORT,
  154. cs->ics.parm.dss1_io.ll_id,
  155. (mode != 2) ? "" : "0 ",
  156. divert_if.drv_to_name(cs->ics.driver),
  157. msn,
  158. service & 0xFF,
  159. proc,
  160. (mode != 1) ? "" : " 0 ",
  161. (mode != 1) ? "" : fwd_nr);
  162. retval = divert_if.ll_cmd(&cs->ics); /* excute command */
  163. if (!retval)
  164. { cs->prev = NULL;
  165. spin_lock_irqsave(&divert_lock, flags);
  166. cs->next = divert_head;
  167. divert_head = cs;
  168. spin_unlock_irqrestore(&divert_lock, flags);
  169. }
  170. else
  171. kfree(cs);
  172. return(retval);
  173. } /* cf_command */
  174. /****************************************/
  175. /* handle a external deflection command */
  176. /****************************************/
  177. int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
  178. { struct call_struc *cs;
  179. isdn_ctrl ic;
  180. unsigned long flags;
  181. int i;
  182. if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */
  183. cs = divert_head; /* start of parameter list */
  184. while (cs)
  185. { if (cs->divert_id == callid) break; /* found */
  186. cs = cs->next;
  187. } /* search entry */
  188. if (!cs) return(-EINVAL); /* invalid callid */
  189. ic.driver = cs->ics.driver;
  190. ic.arg = cs->ics.arg;
  191. i = -EINVAL;
  192. if (cs->akt_state == DEFLECT_AUTODEL) return(i); /* no valid call */
  193. switch (cmd & 0x7F)
  194. { case 0: /* hangup */
  195. del_timer(&cs->timer);
  196. ic.command = ISDN_CMD_HANGUP;
  197. i = divert_if.ll_cmd(&ic);
  198. spin_lock_irqsave(&divert_lock, flags);
  199. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  200. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  201. add_timer(&cs->timer);
  202. spin_unlock_irqrestore(&divert_lock, flags);
  203. break;
  204. case 1: /* alert */
  205. if (cs->akt_state == DEFLECT_ALERT) return(0);
  206. cmd &= 0x7F; /* never wait */
  207. del_timer(&cs->timer);
  208. ic.command = ISDN_CMD_ALERT;
  209. if ((i = divert_if.ll_cmd(&ic)))
  210. {
  211. spin_lock_irqsave(&divert_lock, flags);
  212. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  213. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  214. add_timer(&cs->timer);
  215. spin_unlock_irqrestore(&divert_lock, flags);
  216. }
  217. else
  218. cs->akt_state = DEFLECT_ALERT;
  219. break;
  220. case 2: /* redir */
  221. del_timer(&cs->timer);
  222. strcpy(cs->ics.parm.setup.phone, to_nr);
  223. strcpy(cs->ics.parm.setup.eazmsn, "Testtext manual");
  224. ic.command = ISDN_CMD_REDIR;
  225. if ((i = divert_if.ll_cmd(&ic)))
  226. {
  227. spin_lock_irqsave(&divert_lock, flags);
  228. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  229. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  230. add_timer(&cs->timer);
  231. spin_unlock_irqrestore(&divert_lock, flags);
  232. }
  233. else
  234. cs->akt_state = DEFLECT_ALERT;
  235. break;
  236. } /* switch */
  237. return(i);
  238. } /* deflect_extern_action */
  239. /********************************/
  240. /* insert a new rule before idx */
  241. /********************************/
  242. int insertrule(int idx, divert_rule *newrule)
  243. { struct deflect_struc *ds,*ds1=NULL;
  244. unsigned long flags;
  245. if (!(ds = kmalloc(sizeof(struct deflect_struc),
  246. GFP_KERNEL)))
  247. return(-ENOMEM); /* no memory */
  248. ds->rule = *newrule; /* set rule */
  249. spin_lock_irqsave(&divert_lock, flags);
  250. if (idx >= 0)
  251. { ds1 = table_head;
  252. while ((ds1) && (idx > 0))
  253. { idx--;
  254. ds1 = ds1->next;
  255. }
  256. if (!ds1) idx = -1;
  257. }
  258. if (idx < 0)
  259. { ds->prev = table_tail; /* previous entry */
  260. ds->next = NULL; /* end of chain */
  261. if (ds->prev)
  262. ds->prev->next = ds; /* last forward */
  263. else
  264. table_head = ds; /* is first entry */
  265. table_tail = ds; /* end of queue */
  266. }
  267. else
  268. { ds->next = ds1; /* next entry */
  269. ds->prev = ds1->prev; /* prev entry */
  270. ds1->prev = ds; /* backward chain old element */
  271. if (!ds->prev)
  272. table_head = ds; /* first element */
  273. }
  274. spin_unlock_irqrestore(&divert_lock, flags);
  275. return(0);
  276. } /* insertrule */
  277. /***********************************/
  278. /* delete the rule at position idx */
  279. /***********************************/
  280. int deleterule(int idx)
  281. { struct deflect_struc *ds,*ds1;
  282. unsigned long flags;
  283. if (idx < 0)
  284. { spin_lock_irqsave(&divert_lock, flags);
  285. ds = table_head;
  286. table_head = NULL;
  287. table_tail = NULL;
  288. spin_unlock_irqrestore(&divert_lock, flags);
  289. while (ds)
  290. { ds1 = ds;
  291. ds = ds->next;
  292. kfree(ds1);
  293. }
  294. return(0);
  295. }
  296. spin_lock_irqsave(&divert_lock, flags);
  297. ds = table_head;
  298. while ((ds) && (idx > 0))
  299. { idx--;
  300. ds = ds->next;
  301. }
  302. if (!ds)
  303. {
  304. spin_unlock_irqrestore(&divert_lock, flags);
  305. return(-EINVAL);
  306. }
  307. if (ds->next)
  308. ds->next->prev = ds->prev; /* backward chain */
  309. else
  310. table_tail = ds->prev; /* end of chain */
  311. if (ds->prev)
  312. ds->prev->next = ds->next; /* forward chain */
  313. else
  314. table_head = ds->next; /* start of chain */
  315. spin_unlock_irqrestore(&divert_lock, flags);
  316. kfree(ds);
  317. return(0);
  318. } /* deleterule */
  319. /*******************************************/
  320. /* get a pointer to a specific rule number */
  321. /*******************************************/
  322. divert_rule *getruleptr(int idx)
  323. { struct deflect_struc *ds = table_head;
  324. if (idx < 0) return(NULL);
  325. while ((ds) && (idx >= 0))
  326. { if (!(idx--))
  327. { return(&ds->rule);
  328. break;
  329. }
  330. ds = ds->next;
  331. }
  332. return(NULL);
  333. } /* getruleptr */
  334. /*************************************************/
  335. /* called from common module on an incoming call */
  336. /*************************************************/
  337. static int isdn_divert_icall(isdn_ctrl *ic)
  338. { int retval = 0;
  339. unsigned long flags;
  340. struct call_struc *cs = NULL;
  341. struct deflect_struc *dv;
  342. char *p,*p1;
  343. u_char accept;
  344. /* first check the internal deflection table */
  345. for (dv = table_head; dv ; dv = dv->next )
  346. { /* scan table */
  347. if (((dv->rule.callopt == 1) && (ic->command == ISDN_STAT_ICALLW)) ||
  348. ((dv->rule.callopt == 2) && (ic->command == ISDN_STAT_ICALL)))
  349. continue; /* call option check */
  350. if (!(dv->rule.drvid & (1L << ic->driver)))
  351. continue; /* driver not matching */
  352. if ((dv->rule.si1) && (dv->rule.si1 != ic->parm.setup.si1))
  353. continue; /* si1 not matching */
  354. if ((dv->rule.si2) && (dv->rule.si2 != ic->parm.setup.si2))
  355. continue; /* si2 not matching */
  356. p = dv->rule.my_msn;
  357. p1 = ic->parm.setup.eazmsn;
  358. accept = 0;
  359. while (*p)
  360. { /* complete compare */
  361. if (*p == '-')
  362. { accept = 1; /* call accepted */
  363. break;
  364. }
  365. if (*p++ != *p1++)
  366. break; /* not accepted */
  367. if ((!*p) && (!*p1))
  368. accept = 1;
  369. } /* complete compare */
  370. if (!accept) continue; /* not accepted */
  371. if ((strcmp(dv->rule.caller,"0")) || (ic->parm.setup.phone[0]))
  372. { p = dv->rule.caller;
  373. p1 = ic->parm.setup.phone;
  374. accept = 0;
  375. while (*p)
  376. { /* complete compare */
  377. if (*p == '-')
  378. { accept = 1; /* call accepted */
  379. break;
  380. }
  381. if (*p++ != *p1++)
  382. break; /* not accepted */
  383. if ((!*p) && (!*p1))
  384. accept = 1;
  385. } /* complete compare */
  386. if (!accept) continue; /* not accepted */
  387. }
  388. switch (dv->rule.action)
  389. { case DEFLECT_IGNORE:
  390. return(0);
  391. break;
  392. case DEFLECT_ALERT:
  393. case DEFLECT_PROCEED:
  394. case DEFLECT_REPORT:
  395. case DEFLECT_REJECT:
  396. if (dv->rule.action == DEFLECT_PROCEED)
  397. if ((!if_used) || ((!extern_wait_max) && (!dv->rule.waittime)))
  398. return(0); /* no external deflection needed */
  399. if (!(cs = kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
  400. return(0); /* no memory */
  401. init_timer(&cs->timer);
  402. cs->info[0] = '\0';
  403. cs->timer.function = deflect_timer_expire;
  404. cs->timer.data = (ulong) cs; /* pointer to own structure */
  405. cs->ics = *ic; /* copy incoming data */
  406. if (!cs->ics.parm.setup.phone[0]) strcpy(cs->ics.parm.setup.phone,"0");
  407. if (!cs->ics.parm.setup.eazmsn[0]) strcpy(cs->ics.parm.setup.eazmsn,"0");
  408. cs->ics.parm.setup.screen = dv->rule.screen;
  409. if (dv->rule.waittime)
  410. cs->timer.expires = jiffies + (HZ * dv->rule.waittime);
  411. else
  412. if (dv->rule.action == DEFLECT_PROCEED)
  413. cs->timer.expires = jiffies + (HZ * extern_wait_max);
  414. else
  415. cs->timer.expires = 0;
  416. cs->akt_state = dv->rule.action;
  417. spin_lock_irqsave(&divert_lock, flags);
  418. cs->divert_id = next_id++; /* new sequence number */
  419. spin_unlock_irqrestore(&divert_lock, flags);
  420. cs->prev = NULL;
  421. if (cs->akt_state == DEFLECT_ALERT)
  422. { strcpy(cs->deflect_dest,dv->rule.to_nr);
  423. if (!cs->timer.expires)
  424. { strcpy(ic->parm.setup.eazmsn,"Testtext direct");
  425. ic->parm.setup.screen = dv->rule.screen;
  426. strcpy(ic->parm.setup.phone,dv->rule.to_nr);
  427. cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
  428. cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
  429. retval = 5;
  430. }
  431. else
  432. retval = 1; /* alerting */
  433. }
  434. else
  435. { cs->deflect_dest[0] = '\0';
  436. retval = 4; /* only proceed */
  437. }
  438. sprintf(cs->info,"%d 0x%lx %s %s %s %s 0x%x 0x%x %d %d %s\n",
  439. cs->akt_state,
  440. cs->divert_id,
  441. divert_if.drv_to_name(cs->ics.driver),
  442. (ic->command == ISDN_STAT_ICALLW) ? "1":"0",
  443. cs->ics.parm.setup.phone,
  444. cs->ics.parm.setup.eazmsn,
  445. cs->ics.parm.setup.si1,
  446. cs->ics.parm.setup.si2,
  447. cs->ics.parm.setup.screen,
  448. dv->rule.waittime,
  449. cs->deflect_dest);
  450. if ((dv->rule.action == DEFLECT_REPORT) ||
  451. (dv->rule.action == DEFLECT_REJECT))
  452. { put_info_buffer(cs->info);
  453. kfree(cs); /* remove */
  454. return((dv->rule.action == DEFLECT_REPORT) ? 0:2); /* nothing to do */
  455. }
  456. break;
  457. default:
  458. return(0); /* ignore call */
  459. break;
  460. } /* switch action */
  461. break;
  462. } /* scan_table */
  463. if (cs)
  464. { cs->prev = NULL;
  465. spin_lock_irqsave(&divert_lock, flags);
  466. cs->next = divert_head;
  467. divert_head = cs;
  468. if (cs->timer.expires) add_timer(&cs->timer);
  469. spin_unlock_irqrestore(&divert_lock, flags);
  470. put_info_buffer(cs->info);
  471. return(retval);
  472. }
  473. else
  474. return(0);
  475. } /* isdn_divert_icall */
  476. void deleteprocs(void)
  477. { struct call_struc *cs, *cs1;
  478. unsigned long flags;
  479. spin_lock_irqsave(&divert_lock, flags);
  480. cs = divert_head;
  481. divert_head = NULL;
  482. while (cs)
  483. { del_timer(&cs->timer);
  484. cs1 = cs;
  485. cs = cs->next;
  486. kfree(cs1);
  487. }
  488. spin_unlock_irqrestore(&divert_lock, flags);
  489. } /* deleteprocs */
  490. /****************************************************/
  491. /* put a address including address type into buffer */
  492. /****************************************************/
  493. static int put_address(char *st, u_char *p, int len)
  494. { u_char retval = 0;
  495. u_char adr_typ = 0; /* network standard */
  496. if (len < 2) return(retval);
  497. if (*p == 0xA1)
  498. { retval = *(++p) + 2; /* total length */
  499. if (retval > len) return(0); /* too short */
  500. len = retval - 2; /* remaining length */
  501. if (len < 3) return(0);
  502. if ((*(++p) != 0x0A) || (*(++p) != 1)) return(0);
  503. adr_typ = *(++p);
  504. len -= 3;
  505. p++;
  506. if (len < 2) return(0);
  507. if (*p++ != 0x12) return(0);
  508. if (*p > len) return(0); /* check number length */
  509. len = *p++;
  510. }
  511. else
  512. if (*p == 0x80)
  513. { retval = *(++p) + 2; /* total length */
  514. if (retval > len) return(0);
  515. len = retval - 2;
  516. p++;
  517. }
  518. else
  519. return(0); /* invalid address information */
  520. sprintf(st,"%d ",adr_typ);
  521. st += strlen(st);
  522. if (!len)
  523. *st++ = '-';
  524. else
  525. while (len--)
  526. *st++ = *p++;
  527. *st = '\0';
  528. return(retval);
  529. } /* put_address */
  530. /*************************************/
  531. /* report a successful interrogation */
  532. /*************************************/
  533. static int interrogate_success(isdn_ctrl *ic, struct call_struc *cs)
  534. { char *src = ic->parm.dss1_io.data;
  535. int restlen = ic->parm.dss1_io.datalen;
  536. int cnt = 1;
  537. u_char n,n1;
  538. char st[90], *p, *stp;
  539. if (restlen < 2) return(-100); /* frame too short */
  540. if (*src++ != 0x30) return(-101);
  541. if ((n = *src++) > 0x81) return(-102); /* invalid length field */
  542. restlen -= 2; /* remaining bytes */
  543. if (n == 0x80)
  544. { if (restlen < 2) return(-103);
  545. if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-104);
  546. restlen -= 2;
  547. }
  548. else
  549. if ( n == 0x81)
  550. { n = *src++;
  551. restlen--;
  552. if (n > restlen) return(-105);
  553. restlen = n;
  554. }
  555. else
  556. if (n > restlen) return(-106);
  557. else
  558. restlen = n; /* standard format */
  559. if (restlen < 3) return(-107); /* no procedure */
  560. if ((*src++ != 2) || (*src++ != 1) || (*src++ != 0x0B)) return(-108);
  561. restlen -= 3;
  562. if (restlen < 2) return(-109); /* list missing */
  563. if (*src == 0x31)
  564. { src++;
  565. if ((n = *src++) > 0x81) return(-110); /* invalid length field */
  566. restlen -= 2; /* remaining bytes */
  567. if (n == 0x80)
  568. { if (restlen < 2) return(-111);
  569. if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-112);
  570. restlen -= 2;
  571. }
  572. else
  573. if ( n == 0x81)
  574. { n = *src++;
  575. restlen--;
  576. if (n > restlen) return(-113);
  577. restlen = n;
  578. }
  579. else
  580. if (n > restlen) return(-114);
  581. else
  582. restlen = n; /* standard format */
  583. } /* result list header */
  584. while (restlen >= 2)
  585. { stp = st;
  586. sprintf(stp,"%d 0x%lx %d %s ",DIVERT_REPORT, ic->parm.dss1_io.ll_id,
  587. cnt++,divert_if.drv_to_name(ic->driver));
  588. stp += strlen(stp);
  589. if (*src++ != 0x30) return(-115); /* invalid enum */
  590. n = *src++;
  591. restlen -= 2;
  592. if (n > restlen) return(-116); /* enum length wrong */
  593. restlen -= n;
  594. p = src; /* one entry */
  595. src += n;
  596. if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
  597. stp += strlen(stp);
  598. p += n1;
  599. n -= n1;
  600. if (n < 6) continue; /* no service and proc */
  601. if ((*p++ != 0x0A) || (*p++ != 1)) continue;
  602. sprintf(stp," 0x%02x ",(*p++) & 0xFF);
  603. stp += strlen(stp);
  604. if ((*p++ != 0x0A) || (*p++ != 1)) continue;
  605. sprintf(stp,"%d ",(*p++) & 0xFF);
  606. stp += strlen(stp);
  607. n -= 6;
  608. if (n > 2)
  609. { if (*p++ != 0x30) continue;
  610. if (*p > (n-2)) continue;
  611. n = *p++;
  612. if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
  613. stp += strlen(stp);
  614. }
  615. sprintf(stp,"\n");
  616. put_info_buffer(st);
  617. } /* while restlen */
  618. if (restlen) return(-117);
  619. return(0);
  620. } /* interrogate_success */
  621. /*********************************************/
  622. /* callback for protocol specific extensions */
  623. /*********************************************/
  624. static int prot_stat_callback(isdn_ctrl *ic)
  625. { struct call_struc *cs, *cs1;
  626. int i;
  627. unsigned long flags;
  628. cs = divert_head; /* start of list */
  629. cs1 = NULL;
  630. while (cs)
  631. { if (ic->driver == cs->ics.driver)
  632. { switch (cs->ics.arg)
  633. { case DSS1_CMD_INVOKE:
  634. if ((cs->ics.parm.dss1_io.ll_id == ic->parm.dss1_io.ll_id) &&
  635. (cs->ics.parm.dss1_io.hl_id == ic->parm.dss1_io.hl_id))
  636. { switch (ic->arg)
  637. { case DSS1_STAT_INVOKE_ERR:
  638. sprintf(cs->info,"128 0x%lx 0x%x\n",
  639. ic->parm.dss1_io.ll_id,
  640. ic->parm.dss1_io.timeout);
  641. put_info_buffer(cs->info);
  642. break;
  643. case DSS1_STAT_INVOKE_RES:
  644. switch (cs->ics.parm.dss1_io.proc)
  645. { case 7:
  646. case 8:
  647. put_info_buffer(cs->info);
  648. break;
  649. case 11:
  650. i = interrogate_success(ic,cs);
  651. if (i)
  652. sprintf(cs->info,"%d 0x%lx %d\n",DIVERT_REPORT,
  653. ic->parm.dss1_io.ll_id,i);
  654. put_info_buffer(cs->info);
  655. break;
  656. default:
  657. printk(KERN_WARNING "dss1_divert: unknown proc %d\n",cs->ics.parm.dss1_io.proc);
  658. break;
  659. }
  660. break;
  661. default:
  662. printk(KERN_WARNING "dss1_divert unknown invoke answer %lx\n",ic->arg);
  663. break;
  664. }
  665. cs1 = cs; /* remember structure */
  666. cs = NULL;
  667. continue; /* abort search */
  668. } /* id found */
  669. break;
  670. case DSS1_CMD_INVOKE_ABORT:
  671. printk(KERN_WARNING "dss1_divert unhandled invoke abort\n");
  672. break;
  673. default:
  674. printk(KERN_WARNING "dss1_divert unknown cmd 0x%lx\n",cs->ics.arg);
  675. break;
  676. } /* switch ics.arg */
  677. cs = cs->next;
  678. } /* driver ok */
  679. }
  680. if (!cs1)
  681. { printk(KERN_WARNING "dss1_divert unhandled process\n");
  682. return(0);
  683. }
  684. if (cs1->ics.driver == -1)
  685. {
  686. spin_lock_irqsave(&divert_lock, flags);
  687. del_timer(&cs1->timer);
  688. if (cs1->prev)
  689. cs1->prev->next = cs1->next; /* forward link */
  690. else
  691. divert_head = cs1->next;
  692. if (cs1->next)
  693. cs1->next->prev = cs1->prev; /* back link */
  694. spin_unlock_irqrestore(&divert_lock, flags);
  695. kfree(cs1);
  696. }
  697. return(0);
  698. } /* prot_stat_callback */
  699. /***************************/
  700. /* status callback from HL */
  701. /***************************/
  702. static int isdn_divert_stat_callback(isdn_ctrl *ic)
  703. { struct call_struc *cs, *cs1;
  704. unsigned long flags;
  705. int retval;
  706. retval = -1;
  707. cs = divert_head; /* start of list */
  708. while (cs)
  709. { if ((ic->driver == cs->ics.driver) && (ic->arg == cs->ics.arg))
  710. { switch (ic->command)
  711. { case ISDN_STAT_DHUP:
  712. sprintf(cs->info,"129 0x%lx\n",cs->divert_id);
  713. del_timer(&cs->timer);
  714. cs->ics.driver = -1;
  715. break;
  716. case ISDN_STAT_CAUSE:
  717. sprintf(cs->info,"130 0x%lx %s\n",cs->divert_id,ic->parm.num);
  718. break;
  719. case ISDN_STAT_REDIR:
  720. sprintf(cs->info,"131 0x%lx\n",cs->divert_id);
  721. del_timer(&cs->timer);
  722. cs->ics.driver = -1;
  723. break;
  724. default:
  725. sprintf(cs->info,"999 0x%lx 0x%x\n",cs->divert_id,(int)(ic->command));
  726. break;
  727. }
  728. put_info_buffer(cs->info);
  729. retval = 0;
  730. }
  731. cs1 = cs;
  732. cs = cs->next;
  733. if (cs1->ics.driver == -1)
  734. {
  735. spin_lock_irqsave(&divert_lock, flags);
  736. if (cs1->prev)
  737. cs1->prev->next = cs1->next; /* forward link */
  738. else
  739. divert_head = cs1->next;
  740. if (cs1->next)
  741. cs1->next->prev = cs1->prev; /* back link */
  742. spin_unlock_irqrestore(&divert_lock, flags);
  743. kfree(cs1);
  744. }
  745. }
  746. return(retval); /* not found */
  747. } /* isdn_divert_stat_callback */
  748. /********************/
  749. /* callback from ll */
  750. /********************/
  751. int ll_callback(isdn_ctrl *ic)
  752. {
  753. switch (ic->command)
  754. { case ISDN_STAT_ICALL:
  755. case ISDN_STAT_ICALLW:
  756. return(isdn_divert_icall(ic));
  757. break;
  758. case ISDN_STAT_PROT:
  759. if ((ic->arg & 0xFF) == ISDN_PTYPE_EURO)
  760. { if (ic->arg != DSS1_STAT_INVOKE_BRD)
  761. return(prot_stat_callback(ic));
  762. else
  763. return(0); /* DSS1 invoke broadcast */
  764. }
  765. else
  766. return(-1); /* protocol not euro */
  767. default:
  768. return(isdn_divert_stat_callback(ic));
  769. }
  770. } /* ll_callback */