1 /* $Id: isdn_divert.c,v 1.6.6.3 2001/09/23 22:24:36 kai Exp $
3 * DSS1 main diversion supplementary handling for i4l.
5 * Copyright 1999 by Werner Cornelius (werner@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/proc_fs.h>
14 #include "isdn_divert.h"
16 /**********************************/
17 /* structure keeping calling info */
18 /**********************************/
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;
31 /********************************************/
32 /* structure keeping deflection table entry */
33 /********************************************/
35 { struct deflect_struc *next,*prev;
36 divert_rule rule; /* used rule */
40 /*****************************************/
41 /* variables for main diversion services */
42 /*****************************************/
43 /* diversion/deflection processes */
44 static struct call_struc *divert_head = NULL; /* head of remembered entrys */
45 static ulong next_id = 1; /* next info id */
46 static struct deflect_struc *table_head = NULL;
47 static struct deflect_struc *table_tail = NULL;
48 static unsigned char extern_wait_max = 4; /* maximum wait in s for external process */
50 DEFINE_SPINLOCK(divert_lock);
52 /***************************/
53 /* timer callback function */
54 /***************************/
55 static void deflect_timer_expire(ulong arg)
58 struct call_struc *cs = (struct call_struc *) arg;
60 spin_lock_irqsave(&divert_lock, flags);
61 del_timer(&cs->timer); /* delete active timer */
62 spin_unlock_irqrestore(&divert_lock, flags);
65 { case DEFLECT_PROCEED:
66 cs->ics.command = ISDN_CMD_HANGUP; /* cancel action */
67 divert_if.ll_cmd(&cs->ics);
68 spin_lock_irqsave(&divert_lock, flags);
69 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
70 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
71 add_timer(&cs->timer);
72 spin_unlock_irqrestore(&divert_lock, flags);
76 cs->ics.command = ISDN_CMD_REDIR; /* protocol */
77 strcpy(cs->ics.parm.setup.phone,cs->deflect_dest);
78 strcpy(cs->ics.parm.setup.eazmsn,"Testtext delayed");
79 divert_if.ll_cmd(&cs->ics);
80 spin_lock_irqsave(&divert_lock, flags);
81 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
82 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
83 add_timer(&cs->timer);
84 spin_unlock_irqrestore(&divert_lock, flags);
89 spin_lock_irqsave(&divert_lock, flags);
91 cs->prev->next = cs->next; /* forward link */
93 divert_head = cs->next;
95 cs->next->prev = cs->prev; /* back link */
96 spin_unlock_irqrestore(&divert_lock, flags);
101 } /* deflect_timer_func */
104 /*****************************************/
105 /* handle call forwarding de/activations */
106 /* 0 = deact, 1 = act, 2 = interrogate */
107 /*****************************************/
108 int cf_command(int drvid, int mode,
109 u_char proc, char *msn,
110 u_char service, char *fwd_nr, ulong *procid)
111 { unsigned long flags;
114 char *p,*ielenp,tmp[60];
115 struct call_struc *cs;
117 if (strchr(msn,'.')) return(-EINVAL); /* subaddress not allowed in msn */
118 if ((proc & 0x7F) > 2) return(-EINVAL);
121 *p++ = 0x30; /* enumeration */
122 ielenp = p++; /* remember total length position */
123 *p++ = 0xa; /* proc tag */
124 *p++ = 1; /* length */
125 *p++ = proc & 0x7F; /* procedure to de/activate/interrogate */
126 *p++ = 0xa; /* service tag */
127 *p++ = 1; /* length */
128 *p++ = service; /* service to handle */
131 { if (!*fwd_nr) return(-EINVAL); /* destination missing */
132 if (strchr(fwd_nr,'.')) return(-EINVAL); /* subaddress not allowed */
133 fwd_len = strlen(fwd_nr);
134 *p++ = 0x30; /* number enumeration */
135 *p++ = fwd_len + 2; /* complete forward to len */
136 *p++ = 0x80; /* fwd to nr */
137 *p++ = fwd_len; /* length of number */
138 strcpy(p,fwd_nr); /* copy number */
139 p += fwd_len; /* pointer beyond fwd */
142 msnlen = strlen(msn);
143 *p++ = 0x80; /* msn number */
145 { *p++ = msnlen; /* length */
151 *ielenp = p - ielenp - 1; /* set total IE length */
153 /* allocate mem for information struct */
154 if (!(cs = (struct call_struc *) kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
155 return(-ENOMEM); /* no memory */
156 init_timer(&cs->timer);
158 cs->timer.function = deflect_timer_expire;
159 cs->timer.data = (ulong) cs; /* pointer to own structure */
160 cs->ics.driver = drvid;
161 cs->ics.command = ISDN_CMD_PROT_IO; /* protocol specific io */
162 cs->ics.arg = DSS1_CMD_INVOKE; /* invoke supplementary service */
163 cs->ics.parm.dss1_io.proc = (mode == 1) ? 7: (mode == 2) ? 11:8; /* operation */
164 cs->ics.parm.dss1_io.timeout = 4000; /* from ETS 300 207-1 */
165 cs->ics.parm.dss1_io.datalen = p - tmp; /* total len */
166 cs->ics.parm.dss1_io.data = tmp; /* start of buffer */
168 spin_lock_irqsave(&divert_lock, flags);
169 cs->ics.parm.dss1_io.ll_id = next_id++; /* id for callback */
170 spin_unlock_irqrestore(&divert_lock, flags);
171 *procid = cs->ics.parm.dss1_io.ll_id;
173 sprintf(cs->info,"%d 0x%lx %s%s 0 %s %02x %d%s%s\n",
174 (!mode ) ? DIVERT_DEACTIVATE : (mode == 1) ? DIVERT_ACTIVATE : DIVERT_REPORT,
175 cs->ics.parm.dss1_io.ll_id,
176 (mode != 2) ? "" : "0 ",
177 divert_if.drv_to_name(cs->ics.driver),
181 (mode != 1) ? "" : " 0 ",
182 (mode != 1) ? "" : fwd_nr);
184 retval = divert_if.ll_cmd(&cs->ics); /* excute command */
188 spin_lock_irqsave(&divert_lock, flags);
189 cs->next = divert_head;
191 spin_unlock_irqrestore(&divert_lock, flags);
199 /****************************************/
200 /* handle a external deflection command */
201 /****************************************/
202 int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
203 { struct call_struc *cs;
208 if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */
209 cs = divert_head; /* start of parameter list */
211 { if (cs->divert_id == callid) break; /* found */
214 if (!cs) return(-EINVAL); /* invalid callid */
216 ic.driver = cs->ics.driver;
217 ic.arg = cs->ics.arg;
219 if (cs->akt_state == DEFLECT_AUTODEL) return(i); /* no valid call */
221 { case 0: /* hangup */
222 del_timer(&cs->timer);
223 ic.command = ISDN_CMD_HANGUP;
224 i = divert_if.ll_cmd(&ic);
225 spin_lock_irqsave(&divert_lock, flags);
226 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
227 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
228 add_timer(&cs->timer);
229 spin_unlock_irqrestore(&divert_lock, flags);
233 if (cs->akt_state == DEFLECT_ALERT) return(0);
234 cmd &= 0x7F; /* never wait */
235 del_timer(&cs->timer);
236 ic.command = ISDN_CMD_ALERT;
237 if ((i = divert_if.ll_cmd(&ic)))
239 spin_lock_irqsave(&divert_lock, flags);
240 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
241 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
242 add_timer(&cs->timer);
243 spin_unlock_irqrestore(&divert_lock, flags);
246 cs->akt_state = DEFLECT_ALERT;
250 del_timer(&cs->timer);
251 strcpy(cs->ics.parm.setup.phone, to_nr);
252 strcpy(cs->ics.parm.setup.eazmsn, "Testtext manual");
253 ic.command = ISDN_CMD_REDIR;
254 if ((i = divert_if.ll_cmd(&ic)))
256 spin_lock_irqsave(&divert_lock, flags);
257 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
258 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
259 add_timer(&cs->timer);
260 spin_unlock_irqrestore(&divert_lock, flags);
263 cs->akt_state = DEFLECT_ALERT;
268 } /* deflect_extern_action */
270 /********************************/
271 /* insert a new rule before idx */
272 /********************************/
273 int insertrule(int idx, divert_rule *newrule)
274 { struct deflect_struc *ds,*ds1=NULL;
277 if (!(ds = (struct deflect_struc *) kmalloc(sizeof(struct deflect_struc),
279 return(-ENOMEM); /* no memory */
281 ds->rule = *newrule; /* set rule */
283 spin_lock_irqsave(&divert_lock, flags);
287 while ((ds1) && (idx > 0))
295 { ds->prev = table_tail; /* previous entry */
296 ds->next = NULL; /* end of chain */
298 ds->prev->next = ds; /* last forward */
300 table_head = ds; /* is first entry */
301 table_tail = ds; /* end of queue */
304 { ds->next = ds1; /* next entry */
305 ds->prev = ds1->prev; /* prev entry */
306 ds1->prev = ds; /* backward chain old element */
308 table_head = ds; /* first element */
311 spin_unlock_irqrestore(&divert_lock, flags);
315 /***********************************/
316 /* delete the rule at position idx */
317 /***********************************/
318 int deleterule(int idx)
319 { struct deflect_struc *ds,*ds1;
323 { spin_lock_irqsave(&divert_lock, flags);
327 spin_unlock_irqrestore(&divert_lock, flags);
336 spin_lock_irqsave(&divert_lock, flags);
339 while ((ds) && (idx > 0))
346 spin_unlock_irqrestore(&divert_lock, flags);
351 ds->next->prev = ds->prev; /* backward chain */
353 table_tail = ds->prev; /* end of chain */
356 ds->prev->next = ds->next; /* forward chain */
358 table_head = ds->next; /* start of chain */
360 spin_unlock_irqrestore(&divert_lock, flags);
365 /*******************************************/
366 /* get a pointer to a specific rule number */
367 /*******************************************/
368 divert_rule *getruleptr(int idx)
369 { struct deflect_struc *ds = table_head;
371 if (idx < 0) return(NULL);
372 while ((ds) && (idx >= 0))
382 /*************************************************/
383 /* called from common module on an incoming call */
384 /*************************************************/
385 static int isdn_divert_icall(isdn_ctrl *ic)
388 struct call_struc *cs = NULL;
389 struct deflect_struc *dv;
393 /* first check the internal deflection table */
394 for (dv = table_head; dv ; dv = dv->next )
396 if (((dv->rule.callopt == 1) && (ic->command == ISDN_STAT_ICALLW)) ||
397 ((dv->rule.callopt == 2) && (ic->command == ISDN_STAT_ICALL)))
398 continue; /* call option check */
399 if (!(dv->rule.drvid & (1L << ic->driver)))
400 continue; /* driver not matching */
401 if ((dv->rule.si1) && (dv->rule.si1 != ic->parm.setup.si1))
402 continue; /* si1 not matching */
403 if ((dv->rule.si2) && (dv->rule.si2 != ic->parm.setup.si2))
404 continue; /* si2 not matching */
407 p1 = ic->parm.setup.eazmsn;
410 { /* complete compare */
412 { accept = 1; /* call accepted */
416 break; /* not accepted */
419 } /* complete compare */
420 if (!accept) continue; /* not accepted */
422 if ((strcmp(dv->rule.caller,"0")) || (ic->parm.setup.phone[0]))
423 { p = dv->rule.caller;
424 p1 = ic->parm.setup.phone;
427 { /* complete compare */
429 { accept = 1; /* call accepted */
433 break; /* not accepted */
436 } /* complete compare */
437 if (!accept) continue; /* not accepted */
440 switch (dv->rule.action)
441 { case DEFLECT_IGNORE:
446 case DEFLECT_PROCEED:
449 if (dv->rule.action == DEFLECT_PROCEED)
450 if ((!if_used) || ((!extern_wait_max) && (!dv->rule.waittime)))
451 return(0); /* no external deflection needed */
452 if (!(cs = (struct call_struc *) kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
453 return(0); /* no memory */
454 init_timer(&cs->timer);
456 cs->timer.function = deflect_timer_expire;
457 cs->timer.data = (ulong) cs; /* pointer to own structure */
459 cs->ics = *ic; /* copy incoming data */
460 if (!cs->ics.parm.setup.phone[0]) strcpy(cs->ics.parm.setup.phone,"0");
461 if (!cs->ics.parm.setup.eazmsn[0]) strcpy(cs->ics.parm.setup.eazmsn,"0");
462 cs->ics.parm.setup.screen = dv->rule.screen;
463 if (dv->rule.waittime)
464 cs->timer.expires = jiffies + (HZ * dv->rule.waittime);
466 if (dv->rule.action == DEFLECT_PROCEED)
467 cs->timer.expires = jiffies + (HZ * extern_wait_max);
469 cs->timer.expires = 0;
470 cs->akt_state = dv->rule.action;
471 spin_lock_irqsave(&divert_lock, flags);
472 cs->divert_id = next_id++; /* new sequence number */
473 spin_unlock_irqrestore(&divert_lock, flags);
475 if (cs->akt_state == DEFLECT_ALERT)
476 { strcpy(cs->deflect_dest,dv->rule.to_nr);
477 if (!cs->timer.expires)
478 { strcpy(ic->parm.setup.eazmsn,"Testtext direct");
479 ic->parm.setup.screen = dv->rule.screen;
480 strcpy(ic->parm.setup.phone,dv->rule.to_nr);
481 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
482 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
486 retval = 1; /* alerting */
489 { cs->deflect_dest[0] = '\0';
490 retval = 4; /* only proceed */
492 sprintf(cs->info,"%d 0x%lx %s %s %s %s 0x%x 0x%x %d %d %s\n",
495 divert_if.drv_to_name(cs->ics.driver),
496 (ic->command == ISDN_STAT_ICALLW) ? "1":"0",
497 cs->ics.parm.setup.phone,
498 cs->ics.parm.setup.eazmsn,
499 cs->ics.parm.setup.si1,
500 cs->ics.parm.setup.si2,
501 cs->ics.parm.setup.screen,
504 if ((dv->rule.action == DEFLECT_REPORT) ||
505 (dv->rule.action == DEFLECT_REJECT))
506 { put_info_buffer(cs->info);
507 kfree(cs); /* remove */
508 return((dv->rule.action == DEFLECT_REPORT) ? 0:2); /* nothing to do */
513 return(0); /* ignore call */
515 } /* switch action */
521 spin_lock_irqsave(&divert_lock, flags);
522 cs->next = divert_head;
524 if (cs->timer.expires) add_timer(&cs->timer);
525 spin_unlock_irqrestore(&divert_lock, flags);
527 put_info_buffer(cs->info);
532 } /* isdn_divert_icall */
535 void deleteprocs(void)
536 { struct call_struc *cs, *cs1;
539 spin_lock_irqsave(&divert_lock, flags);
543 { del_timer(&cs->timer);
548 spin_unlock_irqrestore(&divert_lock, flags);
551 /****************************************************/
552 /* put a address including address type into buffer */
553 /****************************************************/
554 static int put_address(char *st, u_char *p, int len)
556 u_char adr_typ = 0; /* network standard */
558 if (len < 2) return(retval);
560 { retval = *(++p) + 2; /* total length */
561 if (retval > len) return(0); /* too short */
562 len = retval - 2; /* remaining length */
563 if (len < 3) return(0);
564 if ((*(++p) != 0x0A) || (*(++p) != 1)) return(0);
568 if (len < 2) return(0);
569 if (*p++ != 0x12) return(0);
570 if (*p > len) return(0); /* check number length */
575 { retval = *(++p) + 2; /* total length */
576 if (retval > len) return(0);
581 return(0); /* invalid address information */
583 sprintf(st,"%d ",adr_typ);
594 /*************************************/
595 /* report a successful interrogation */
596 /*************************************/
597 static int interrogate_success(isdn_ctrl *ic, struct call_struc *cs)
598 { char *src = ic->parm.dss1_io.data;
599 int restlen = ic->parm.dss1_io.datalen;
602 char st[90], *p, *stp;
604 if (restlen < 2) return(-100); /* frame too short */
605 if (*src++ != 0x30) return(-101);
606 if ((n = *src++) > 0x81) return(-102); /* invalid length field */
607 restlen -= 2; /* remaining bytes */
609 { if (restlen < 2) return(-103);
610 if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-104);
617 if (n > restlen) return(-105);
621 if (n > restlen) return(-106);
623 restlen = n; /* standard format */
624 if (restlen < 3) return(-107); /* no procedure */
625 if ((*src++ != 2) || (*src++ != 1) || (*src++ != 0x0B)) return(-108);
627 if (restlen < 2) return(-109); /* list missing */
630 if ((n = *src++) > 0x81) return(-110); /* invalid length field */
631 restlen -= 2; /* remaining bytes */
633 { if (restlen < 2) return(-111);
634 if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-112);
641 if (n > restlen) return(-113);
645 if (n > restlen) return(-114);
647 restlen = n; /* standard format */
648 } /* result list header */
652 sprintf(stp,"%d 0x%lx %d %s ",DIVERT_REPORT, ic->parm.dss1_io.ll_id,
653 cnt++,divert_if.drv_to_name(ic->driver));
655 if (*src++ != 0x30) return(-115); /* invalid enum */
658 if (n > restlen) return(-116); /* enum length wrong */
660 p = src; /* one entry */
662 if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
666 if (n < 6) continue; /* no service and proc */
667 if ((*p++ != 0x0A) || (*p++ != 1)) continue;
668 sprintf(stp," 0x%02x ",(*p++) & 0xFF);
670 if ((*p++ != 0x0A) || (*p++ != 1)) continue;
671 sprintf(stp,"%d ",(*p++) & 0xFF);
675 { if (*p++ != 0x30) continue;
676 if (*p > (n-2)) continue;
678 if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
683 } /* while restlen */
684 if (restlen) return(-117);
686 } /* interrogate_success */
688 /*********************************************/
689 /* callback for protocol specific extensions */
690 /*********************************************/
691 static int prot_stat_callback(isdn_ctrl *ic)
692 { struct call_struc *cs, *cs1;
696 cs = divert_head; /* start of list */
699 { if (ic->driver == cs->ics.driver)
700 { switch (cs->ics.arg)
701 { case DSS1_CMD_INVOKE:
702 if ((cs->ics.parm.dss1_io.ll_id == ic->parm.dss1_io.ll_id) &&
703 (cs->ics.parm.dss1_io.hl_id == ic->parm.dss1_io.hl_id))
705 { case DSS1_STAT_INVOKE_ERR:
706 sprintf(cs->info,"128 0x%lx 0x%x\n",
707 ic->parm.dss1_io.ll_id,
708 ic->parm.dss1_io.timeout);
709 put_info_buffer(cs->info);
712 case DSS1_STAT_INVOKE_RES:
713 switch (cs->ics.parm.dss1_io.proc)
716 put_info_buffer(cs->info);
720 i = interrogate_success(ic,cs);
722 sprintf(cs->info,"%d 0x%lx %d\n",DIVERT_REPORT,
723 ic->parm.dss1_io.ll_id,i);
724 put_info_buffer(cs->info);
728 printk(KERN_WARNING "dss1_divert: unknown proc %d\n",cs->ics.parm.dss1_io.proc);
736 printk(KERN_WARNING "dss1_divert unknown invoke answer %lx\n",ic->arg);
739 cs1 = cs; /* remember structure */
741 continue; /* abort search */
745 case DSS1_CMD_INVOKE_ABORT:
746 printk(KERN_WARNING "dss1_divert unhandled invoke abort\n");
750 printk(KERN_WARNING "dss1_divert unknown cmd 0x%lx\n",cs->ics.arg);
752 } /* switch ics.arg */
758 { printk(KERN_WARNING "dss1_divert unhandled process\n");
762 if (cs1->ics.driver == -1)
764 spin_lock_irqsave(&divert_lock, flags);
765 del_timer(&cs1->timer);
767 cs1->prev->next = cs1->next; /* forward link */
769 divert_head = cs1->next;
771 cs1->next->prev = cs1->prev; /* back link */
772 spin_unlock_irqrestore(&divert_lock, flags);
777 } /* prot_stat_callback */
780 /***************************/
781 /* status callback from HL */
782 /***************************/
783 static int isdn_divert_stat_callback(isdn_ctrl *ic)
784 { struct call_struc *cs, *cs1;
789 cs = divert_head; /* start of list */
791 { if ((ic->driver == cs->ics.driver) && (ic->arg == cs->ics.arg))
792 { switch (ic->command)
793 { case ISDN_STAT_DHUP:
794 sprintf(cs->info,"129 0x%lx\n",cs->divert_id);
795 del_timer(&cs->timer);
799 case ISDN_STAT_CAUSE:
800 sprintf(cs->info,"130 0x%lx %s\n",cs->divert_id,ic->parm.num);
803 case ISDN_STAT_REDIR:
804 sprintf(cs->info,"131 0x%lx\n",cs->divert_id);
805 del_timer(&cs->timer);
810 sprintf(cs->info,"999 0x%lx 0x%x\n",cs->divert_id,(int)(ic->command));
813 put_info_buffer(cs->info);
818 if (cs1->ics.driver == -1)
820 spin_lock_irqsave(&divert_lock, flags);
822 cs1->prev->next = cs1->next; /* forward link */
824 divert_head = cs1->next;
826 cs1->next->prev = cs1->prev; /* back link */
827 spin_unlock_irqrestore(&divert_lock, flags);
831 return(retval); /* not found */
832 } /* isdn_divert_stat_callback */
835 /********************/
836 /* callback from ll */
837 /********************/
838 int ll_callback(isdn_ctrl *ic)
841 { case ISDN_STAT_ICALL:
842 case ISDN_STAT_ICALLW:
843 return(isdn_divert_icall(ic));
847 if ((ic->arg & 0xFF) == ISDN_PTYPE_EURO)
848 { if (ic->arg != DSS1_STAT_INVOKE_BRD)
849 return(prot_stat_callback(ic));
851 return(0); /* DSS1 invoke broadcast */
854 return(-1); /* protocol not euro */
857 return(isdn_divert_stat_callback(ic));