isdn_divert.c 23 KB

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