isdn_divert.c 26 KB

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