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>
13 #include <linux/timer.h>
14 #include <linux/jiffies.h>
16 #include "isdn_divert.h"
18 /**********************************/
19 /* structure keeping calling info */
20 /**********************************/
22 { isdn_ctrl ics; /* delivered setup + driver parameters */
23 ulong divert_id; /* Id delivered to user */
24 unsigned char akt_state; /* actual state */
25 char deflect_dest[35]; /* deflection destination */
26 struct timer_list timer; /* timer control structure */
27 char info[90]; /* device info output */
28 struct call_struc *next; /* pointer to next entry */
29 struct call_struc *prev;
33 /********************************************/
34 /* structure keeping deflection table entry */
35 /********************************************/
37 { struct deflect_struc *next,*prev;
38 divert_rule rule; /* used rule */
42 /*****************************************/
43 /* variables for main diversion services */
44 /*****************************************/
45 /* diversion/deflection processes */
46 static struct call_struc *divert_head = NULL; /* head of remembered entrys */
47 static ulong next_id = 1; /* next info id */
48 static struct deflect_struc *table_head = NULL;
49 static struct deflect_struc *table_tail = NULL;
50 static unsigned char extern_wait_max = 4; /* maximum wait in s for external process */
52 DEFINE_SPINLOCK(divert_lock);
54 /***************************/
55 /* timer callback function */
56 /***************************/
57 static void deflect_timer_expire(ulong arg)
60 struct call_struc *cs = (struct call_struc *) arg;
62 spin_lock_irqsave(&divert_lock, flags);
63 del_timer(&cs->timer); /* delete active timer */
64 spin_unlock_irqrestore(&divert_lock, flags);
67 { case DEFLECT_PROCEED:
68 cs->ics.command = ISDN_CMD_HANGUP; /* cancel action */
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);
78 cs->ics.command = ISDN_CMD_REDIR; /* protocol */
79 strcpy(cs->ics.parm.setup.phone,cs->deflect_dest);
80 strcpy(cs->ics.parm.setup.eazmsn,"Testtext delayed");
81 divert_if.ll_cmd(&cs->ics);
82 spin_lock_irqsave(&divert_lock, flags);
83 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
84 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
85 add_timer(&cs->timer);
86 spin_unlock_irqrestore(&divert_lock, flags);
91 spin_lock_irqsave(&divert_lock, flags);
93 cs->prev->next = cs->next; /* forward link */
95 divert_head = cs->next;
97 cs->next->prev = cs->prev; /* back link */
98 spin_unlock_irqrestore(&divert_lock, flags);
103 } /* deflect_timer_func */
106 /*****************************************/
107 /* handle call forwarding de/activations */
108 /* 0 = deact, 1 = act, 2 = interrogate */
109 /*****************************************/
110 int cf_command(int drvid, int mode,
111 u_char proc, char *msn,
112 u_char service, char *fwd_nr, ulong *procid)
113 { unsigned long flags;
116 char *p,*ielenp,tmp[60];
117 struct call_struc *cs;
119 if (strchr(msn,'.')) return(-EINVAL); /* subaddress not allowed in msn */
120 if ((proc & 0x7F) > 2) return(-EINVAL);
123 *p++ = 0x30; /* enumeration */
124 ielenp = p++; /* remember total length position */
125 *p++ = 0xa; /* proc tag */
126 *p++ = 1; /* length */
127 *p++ = proc & 0x7F; /* procedure to de/activate/interrogate */
128 *p++ = 0xa; /* service tag */
129 *p++ = 1; /* length */
130 *p++ = service; /* service to handle */
133 { if (!*fwd_nr) return(-EINVAL); /* destination missing */
134 if (strchr(fwd_nr,'.')) return(-EINVAL); /* subaddress not allowed */
135 fwd_len = strlen(fwd_nr);
136 *p++ = 0x30; /* number enumeration */
137 *p++ = fwd_len + 2; /* complete forward to len */
138 *p++ = 0x80; /* fwd to nr */
139 *p++ = fwd_len; /* length of number */
140 strcpy(p,fwd_nr); /* copy number */
141 p += fwd_len; /* pointer beyond fwd */
144 msnlen = strlen(msn);
145 *p++ = 0x80; /* msn number */
147 { *p++ = msnlen; /* length */
153 *ielenp = p - ielenp - 1; /* set total IE length */
155 /* allocate mem for information struct */
156 if (!(cs = kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
157 return(-ENOMEM); /* no memory */
158 init_timer(&cs->timer);
160 cs->timer.function = deflect_timer_expire;
161 cs->timer.data = (ulong) cs; /* pointer to own structure */
162 cs->ics.driver = drvid;
163 cs->ics.command = ISDN_CMD_PROT_IO; /* protocol specific io */
164 cs->ics.arg = DSS1_CMD_INVOKE; /* invoke supplementary service */
165 cs->ics.parm.dss1_io.proc = (mode == 1) ? 7: (mode == 2) ? 11:8; /* operation */
166 cs->ics.parm.dss1_io.timeout = 4000; /* from ETS 300 207-1 */
167 cs->ics.parm.dss1_io.datalen = p - tmp; /* total len */
168 cs->ics.parm.dss1_io.data = tmp; /* start of buffer */
170 spin_lock_irqsave(&divert_lock, flags);
171 cs->ics.parm.dss1_io.ll_id = next_id++; /* id for callback */
172 spin_unlock_irqrestore(&divert_lock, flags);
173 *procid = cs->ics.parm.dss1_io.ll_id;
175 sprintf(cs->info,"%d 0x%lx %s%s 0 %s %02x %d%s%s\n",
176 (!mode ) ? DIVERT_DEACTIVATE : (mode == 1) ? DIVERT_ACTIVATE : DIVERT_REPORT,
177 cs->ics.parm.dss1_io.ll_id,
178 (mode != 2) ? "" : "0 ",
179 divert_if.drv_to_name(cs->ics.driver),
183 (mode != 1) ? "" : " 0 ",
184 (mode != 1) ? "" : fwd_nr);
186 retval = divert_if.ll_cmd(&cs->ics); /* excute command */
190 spin_lock_irqsave(&divert_lock, flags);
191 cs->next = divert_head;
193 spin_unlock_irqrestore(&divert_lock, flags);
201 /****************************************/
202 /* handle a external deflection command */
203 /****************************************/
204 int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
205 { struct call_struc *cs;
210 if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */
211 cs = divert_head; /* start of parameter list */
213 { if (cs->divert_id == callid) break; /* found */
216 if (!cs) return(-EINVAL); /* invalid callid */
218 ic.driver = cs->ics.driver;
219 ic.arg = cs->ics.arg;
221 if (cs->akt_state == DEFLECT_AUTODEL) return(i); /* no valid call */
223 { case 0: /* hangup */
224 del_timer(&cs->timer);
225 ic.command = ISDN_CMD_HANGUP;
226 i = divert_if.ll_cmd(&ic);
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);
235 if (cs->akt_state == DEFLECT_ALERT) return(0);
236 cmd &= 0x7F; /* never wait */
237 del_timer(&cs->timer);
238 ic.command = ISDN_CMD_ALERT;
239 if ((i = divert_if.ll_cmd(&ic)))
241 spin_lock_irqsave(&divert_lock, flags);
242 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
243 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
244 add_timer(&cs->timer);
245 spin_unlock_irqrestore(&divert_lock, flags);
248 cs->akt_state = DEFLECT_ALERT;
252 del_timer(&cs->timer);
253 strcpy(cs->ics.parm.setup.phone, to_nr);
254 strcpy(cs->ics.parm.setup.eazmsn, "Testtext manual");
255 ic.command = ISDN_CMD_REDIR;
256 if ((i = divert_if.ll_cmd(&ic)))
258 spin_lock_irqsave(&divert_lock, flags);
259 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
260 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
261 add_timer(&cs->timer);
262 spin_unlock_irqrestore(&divert_lock, flags);
265 cs->akt_state = DEFLECT_ALERT;
270 } /* deflect_extern_action */
272 /********************************/
273 /* insert a new rule before idx */
274 /********************************/
275 int insertrule(int idx, divert_rule *newrule)
276 { struct deflect_struc *ds,*ds1=NULL;
279 if (!(ds = kmalloc(sizeof(struct deflect_struc),
281 return(-ENOMEM); /* no memory */
283 ds->rule = *newrule; /* set rule */
285 spin_lock_irqsave(&divert_lock, flags);
289 while ((ds1) && (idx > 0))
297 { ds->prev = table_tail; /* previous entry */
298 ds->next = NULL; /* end of chain */
300 ds->prev->next = ds; /* last forward */
302 table_head = ds; /* is first entry */
303 table_tail = ds; /* end of queue */
306 { ds->next = ds1; /* next entry */
307 ds->prev = ds1->prev; /* prev entry */
308 ds1->prev = ds; /* backward chain old element */
310 table_head = ds; /* first element */
313 spin_unlock_irqrestore(&divert_lock, flags);
317 /***********************************/
318 /* delete the rule at position idx */
319 /***********************************/
320 int deleterule(int idx)
321 { struct deflect_struc *ds,*ds1;
325 { spin_lock_irqsave(&divert_lock, flags);
329 spin_unlock_irqrestore(&divert_lock, flags);
338 spin_lock_irqsave(&divert_lock, flags);
341 while ((ds) && (idx > 0))
348 spin_unlock_irqrestore(&divert_lock, flags);
353 ds->next->prev = ds->prev; /* backward chain */
355 table_tail = ds->prev; /* end of chain */
358 ds->prev->next = ds->next; /* forward chain */
360 table_head = ds->next; /* start of chain */
362 spin_unlock_irqrestore(&divert_lock, flags);
367 /*******************************************/
368 /* get a pointer to a specific rule number */
369 /*******************************************/
370 divert_rule *getruleptr(int idx)
371 { struct deflect_struc *ds = table_head;
373 if (idx < 0) return(NULL);
374 while ((ds) && (idx >= 0))
384 /*************************************************/
385 /* called from common module on an incoming call */
386 /*************************************************/
387 static int isdn_divert_icall(isdn_ctrl *ic)
390 struct call_struc *cs = NULL;
391 struct deflect_struc *dv;
395 /* first check the internal deflection table */
396 for (dv = table_head; dv ; dv = dv->next )
398 if (((dv->rule.callopt == 1) && (ic->command == ISDN_STAT_ICALLW)) ||
399 ((dv->rule.callopt == 2) && (ic->command == ISDN_STAT_ICALL)))
400 continue; /* call option check */
401 if (!(dv->rule.drvid & (1L << ic->driver)))
402 continue; /* driver not matching */
403 if ((dv->rule.si1) && (dv->rule.si1 != ic->parm.setup.si1))
404 continue; /* si1 not matching */
405 if ((dv->rule.si2) && (dv->rule.si2 != ic->parm.setup.si2))
406 continue; /* si2 not matching */
409 p1 = ic->parm.setup.eazmsn;
412 { /* complete compare */
414 { accept = 1; /* call accepted */
418 break; /* not accepted */
421 } /* complete compare */
422 if (!accept) continue; /* not accepted */
424 if ((strcmp(dv->rule.caller,"0")) || (ic->parm.setup.phone[0]))
425 { p = dv->rule.caller;
426 p1 = ic->parm.setup.phone;
429 { /* complete compare */
431 { accept = 1; /* call accepted */
435 break; /* not accepted */
438 } /* complete compare */
439 if (!accept) continue; /* not accepted */
442 switch (dv->rule.action)
443 { case DEFLECT_IGNORE:
448 case DEFLECT_PROCEED:
451 if (dv->rule.action == DEFLECT_PROCEED)
452 if ((!if_used) || ((!extern_wait_max) && (!dv->rule.waittime)))
453 return(0); /* no external deflection needed */
454 if (!(cs = kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))
455 return(0); /* no memory */
456 init_timer(&cs->timer);
458 cs->timer.function = deflect_timer_expire;
459 cs->timer.data = (ulong) cs; /* pointer to own structure */
461 cs->ics = *ic; /* copy incoming data */
462 if (!cs->ics.parm.setup.phone[0]) strcpy(cs->ics.parm.setup.phone,"0");
463 if (!cs->ics.parm.setup.eazmsn[0]) strcpy(cs->ics.parm.setup.eazmsn,"0");
464 cs->ics.parm.setup.screen = dv->rule.screen;
465 if (dv->rule.waittime)
466 cs->timer.expires = jiffies + (HZ * dv->rule.waittime);
468 if (dv->rule.action == DEFLECT_PROCEED)
469 cs->timer.expires = jiffies + (HZ * extern_wait_max);
471 cs->timer.expires = 0;
472 cs->akt_state = dv->rule.action;
473 spin_lock_irqsave(&divert_lock, flags);
474 cs->divert_id = next_id++; /* new sequence number */
475 spin_unlock_irqrestore(&divert_lock, flags);
477 if (cs->akt_state == DEFLECT_ALERT)
478 { strcpy(cs->deflect_dest,dv->rule.to_nr);
479 if (!cs->timer.expires)
480 { strcpy(ic->parm.setup.eazmsn,"Testtext direct");
481 ic->parm.setup.screen = dv->rule.screen;
482 strcpy(ic->parm.setup.phone,dv->rule.to_nr);
483 cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
484 cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
488 retval = 1; /* alerting */
491 { cs->deflect_dest[0] = '\0';
492 retval = 4; /* only proceed */
494 sprintf(cs->info,"%d 0x%lx %s %s %s %s 0x%x 0x%x %d %d %s\n",
497 divert_if.drv_to_name(cs->ics.driver),
498 (ic->command == ISDN_STAT_ICALLW) ? "1":"0",
499 cs->ics.parm.setup.phone,
500 cs->ics.parm.setup.eazmsn,
501 cs->ics.parm.setup.si1,
502 cs->ics.parm.setup.si2,
503 cs->ics.parm.setup.screen,
506 if ((dv->rule.action == DEFLECT_REPORT) ||
507 (dv->rule.action == DEFLECT_REJECT))
508 { put_info_buffer(cs->info);
509 kfree(cs); /* remove */
510 return((dv->rule.action == DEFLECT_REPORT) ? 0:2); /* nothing to do */
515 return(0); /* ignore call */
517 } /* switch action */
523 spin_lock_irqsave(&divert_lock, flags);
524 cs->next = divert_head;
526 if (cs->timer.expires) add_timer(&cs->timer);
527 spin_unlock_irqrestore(&divert_lock, flags);
529 put_info_buffer(cs->info);
534 } /* isdn_divert_icall */
537 void deleteprocs(void)
538 { struct call_struc *cs, *cs1;
541 spin_lock_irqsave(&divert_lock, flags);
545 { del_timer(&cs->timer);
550 spin_unlock_irqrestore(&divert_lock, flags);
553 /****************************************************/
554 /* put a address including address type into buffer */
555 /****************************************************/
556 static int put_address(char *st, u_char *p, int len)
558 u_char adr_typ = 0; /* network standard */
560 if (len < 2) return(retval);
562 { retval = *(++p) + 2; /* total length */
563 if (retval > len) return(0); /* too short */
564 len = retval - 2; /* remaining length */
565 if (len < 3) return(0);
566 if ((*(++p) != 0x0A) || (*(++p) != 1)) return(0);
570 if (len < 2) return(0);
571 if (*p++ != 0x12) return(0);
572 if (*p > len) return(0); /* check number length */
577 { retval = *(++p) + 2; /* total length */
578 if (retval > len) return(0);
583 return(0); /* invalid address information */
585 sprintf(st,"%d ",adr_typ);
596 /*************************************/
597 /* report a successful interrogation */
598 /*************************************/
599 static int interrogate_success(isdn_ctrl *ic, struct call_struc *cs)
600 { char *src = ic->parm.dss1_io.data;
601 int restlen = ic->parm.dss1_io.datalen;
604 char st[90], *p, *stp;
606 if (restlen < 2) return(-100); /* frame too short */
607 if (*src++ != 0x30) return(-101);
608 if ((n = *src++) > 0x81) return(-102); /* invalid length field */
609 restlen -= 2; /* remaining bytes */
611 { if (restlen < 2) return(-103);
612 if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-104);
619 if (n > restlen) return(-105);
623 if (n > restlen) return(-106);
625 restlen = n; /* standard format */
626 if (restlen < 3) return(-107); /* no procedure */
627 if ((*src++ != 2) || (*src++ != 1) || (*src++ != 0x0B)) return(-108);
629 if (restlen < 2) return(-109); /* list missing */
632 if ((n = *src++) > 0x81) return(-110); /* invalid length field */
633 restlen -= 2; /* remaining bytes */
635 { if (restlen < 2) return(-111);
636 if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-112);
643 if (n > restlen) return(-113);
647 if (n > restlen) return(-114);
649 restlen = n; /* standard format */
650 } /* result list header */
654 sprintf(stp,"%d 0x%lx %d %s ",DIVERT_REPORT, ic->parm.dss1_io.ll_id,
655 cnt++,divert_if.drv_to_name(ic->driver));
657 if (*src++ != 0x30) return(-115); /* invalid enum */
660 if (n > restlen) return(-116); /* enum length wrong */
662 p = src; /* one entry */
664 if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
668 if (n < 6) continue; /* no service and proc */
669 if ((*p++ != 0x0A) || (*p++ != 1)) continue;
670 sprintf(stp," 0x%02x ",(*p++) & 0xFF);
672 if ((*p++ != 0x0A) || (*p++ != 1)) continue;
673 sprintf(stp,"%d ",(*p++) & 0xFF);
677 { if (*p++ != 0x30) continue;
678 if (*p > (n-2)) continue;
680 if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
685 } /* while restlen */
686 if (restlen) return(-117);
688 } /* interrogate_success */
690 /*********************************************/
691 /* callback for protocol specific extensions */
692 /*********************************************/
693 static int prot_stat_callback(isdn_ctrl *ic)
694 { struct call_struc *cs, *cs1;
698 cs = divert_head; /* start of list */
701 { if (ic->driver == cs->ics.driver)
702 { switch (cs->ics.arg)
703 { case DSS1_CMD_INVOKE:
704 if ((cs->ics.parm.dss1_io.ll_id == ic->parm.dss1_io.ll_id) &&
705 (cs->ics.parm.dss1_io.hl_id == ic->parm.dss1_io.hl_id))
707 { case DSS1_STAT_INVOKE_ERR:
708 sprintf(cs->info,"128 0x%lx 0x%x\n",
709 ic->parm.dss1_io.ll_id,
710 ic->parm.dss1_io.timeout);
711 put_info_buffer(cs->info);
714 case DSS1_STAT_INVOKE_RES:
715 switch (cs->ics.parm.dss1_io.proc)
718 put_info_buffer(cs->info);
722 i = interrogate_success(ic,cs);
724 sprintf(cs->info,"%d 0x%lx %d\n",DIVERT_REPORT,
725 ic->parm.dss1_io.ll_id,i);
726 put_info_buffer(cs->info);
730 printk(KERN_WARNING "dss1_divert: unknown proc %d\n",cs->ics.parm.dss1_io.proc);
738 printk(KERN_WARNING "dss1_divert unknown invoke answer %lx\n",ic->arg);
741 cs1 = cs; /* remember structure */
743 continue; /* abort search */
747 case DSS1_CMD_INVOKE_ABORT:
748 printk(KERN_WARNING "dss1_divert unhandled invoke abort\n");
752 printk(KERN_WARNING "dss1_divert unknown cmd 0x%lx\n",cs->ics.arg);
754 } /* switch ics.arg */
760 { printk(KERN_WARNING "dss1_divert unhandled process\n");
764 if (cs1->ics.driver == -1)
766 spin_lock_irqsave(&divert_lock, flags);
767 del_timer(&cs1->timer);
769 cs1->prev->next = cs1->next; /* forward link */
771 divert_head = cs1->next;
773 cs1->next->prev = cs1->prev; /* back link */
774 spin_unlock_irqrestore(&divert_lock, flags);
779 } /* prot_stat_callback */
782 /***************************/
783 /* status callback from HL */
784 /***************************/
785 static int isdn_divert_stat_callback(isdn_ctrl *ic)
786 { struct call_struc *cs, *cs1;
791 cs = divert_head; /* start of list */
793 { if ((ic->driver == cs->ics.driver) && (ic->arg == cs->ics.arg))
794 { switch (ic->command)
795 { case ISDN_STAT_DHUP:
796 sprintf(cs->info,"129 0x%lx\n",cs->divert_id);
797 del_timer(&cs->timer);
801 case ISDN_STAT_CAUSE:
802 sprintf(cs->info,"130 0x%lx %s\n",cs->divert_id,ic->parm.num);
805 case ISDN_STAT_REDIR:
806 sprintf(cs->info,"131 0x%lx\n",cs->divert_id);
807 del_timer(&cs->timer);
812 sprintf(cs->info,"999 0x%lx 0x%x\n",cs->divert_id,(int)(ic->command));
815 put_info_buffer(cs->info);
820 if (cs1->ics.driver == -1)
822 spin_lock_irqsave(&divert_lock, flags);
824 cs1->prev->next = cs1->next; /* forward link */
826 divert_head = cs1->next;
828 cs1->next->prev = cs1->prev; /* back link */
829 spin_unlock_irqrestore(&divert_lock, flags);
833 return(retval); /* not found */
834 } /* isdn_divert_stat_callback */
837 /********************/
838 /* callback from ll */
839 /********************/
840 int ll_callback(isdn_ctrl *ic)
843 { case ISDN_STAT_ICALL:
844 case ISDN_STAT_ICALLW:
845 return(isdn_divert_icall(ic));
849 if ((ic->arg & 0xFF) == ISDN_PTYPE_EURO)
850 { if (ic->arg != DSS1_STAT_INVOKE_BRD)
851 return(prot_stat_callback(ic));
853 return(0); /* DSS1 invoke broadcast */
856 return(-1); /* protocol not euro */
859 return(isdn_divert_stat_callback(ic));