v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / drivers / isdn / hisax / l3ni1.c
1 // $Id: l3ni1.c,v 2.5.6.2 2001/02/16 16:43:28 kai Exp $
2 //
3 //-----------------------------------------------------------------------------
4 //
5 // NI1 D-channel protocol
6 //
7 // Authors:
8 // Matt Henderson & Guy Ellis - Traverse Tecnologies Pty Ltd
9 // www.traverse.com.au
10 //
11 // 2000.6.6 Initial implementation of routines for US NI1 
12 // Layer 3 protocol based on the EURO/DSS1 D-channel protocol 
13 // driver written by Karsten Keil et al.  
14 // NI-1 Hall of Fame - Thanks to.... 
15 // Ragnar Paulson - for some handy code fragments
16 // Will Scales - beta tester extraordinaire
17 // Brett Whittacre - beta tester and remote devel system in Vegas
18 //
19 // This file is (c) under GNU General Public License
20 //
21 //-----------------------------------------------------------------------------
22 #define __NO_VERSION__
23 #include "hisax.h"
24 #include "isdnl3.h"
25 #include "l3ni1.h"
26 #include <linux/ctype.h>
27
28 extern char *HiSax_getrev(const char *revision);
29 const char *ni1_revision = "$Revision: 2.5.6.2 $";
30
31 #define EXT_BEARER_CAPS 1
32
33 #define MsgHead(ptr, cref, mty) \
34         *ptr++ = 0x8; \
35         if (cref == -1) { \
36                 *ptr++ = 0x0; \
37         } else { \
38                 *ptr++ = 0x1; \
39                 *ptr++ = cref^0x80; \
40         } \
41         *ptr++ = mty
42
43
44 /**********************************************/
45 /* get a new invoke id for remote operations. */
46 /* Only a return value != 0 is valid          */
47 /**********************************************/
48 static unsigned char new_invoke_id(struct PStack *p)
49 {
50         unsigned char retval;
51         long flags;
52         int i;
53   
54         i = 32; /* maximum search depth */
55
56         save_flags(flags);
57         cli();
58
59         retval = p->prot.ni1.last_invoke_id + 1; /* try new id */
60         while ((i) && (p->prot.ni1.invoke_used[retval >> 3] == 0xFF)) {
61                 p->prot.ni1.last_invoke_id = (retval & 0xF8) + 8;
62                 i--;
63         }  
64         if (i) {
65                 while (p->prot.ni1.invoke_used[retval >> 3] & (1 << (retval & 7)))
66                 retval++; 
67         } else
68                 retval = 0;
69         p->prot.ni1.last_invoke_id = retval;
70         p->prot.ni1.invoke_used[retval >> 3] |= (1 << (retval & 7));
71         restore_flags(flags);
72
73         return(retval);  
74 } /* new_invoke_id */
75
76 /*************************/
77 /* free a used invoke id */
78 /*************************/
79 static void free_invoke_id(struct PStack *p, unsigned char id)
80 { long flags;
81
82   if (!id) return; /* 0 = invalid value */
83
84   save_flags(flags);
85   cli();
86   p->prot.ni1.invoke_used[id >> 3] &= ~(1 << (id & 7));
87   restore_flags(flags);
88 } /* free_invoke_id */  
89
90
91 /**********************************************************/
92 /* create a new l3 process and fill in ni1 specific data */
93 /**********************************************************/
94 static struct l3_process
95 *ni1_new_l3_process(struct PStack *st, int cr)
96 {  struct l3_process *proc;
97
98    if (!(proc = new_l3_process(st, cr))) 
99      return(NULL);
100
101    proc->prot.ni1.invoke_id = 0;
102    proc->prot.ni1.remote_operation = 0;
103    proc->prot.ni1.uus1_data[0] = '\0';
104    
105    return(proc);
106 } /* ni1_new_l3_process */
107
108 /************************************************/
109 /* free a l3 process and all ni1 specific data */
110 /************************************************/ 
111 static void
112 ni1_release_l3_process(struct l3_process *p)
113 {
114    free_invoke_id(p->st,p->prot.ni1.invoke_id);
115    release_l3_process(p);
116 } /* ni1_release_l3_process */
117  
118 /********************************************************/
119 /* search a process with invoke id id and dummy callref */
120 /********************************************************/
121 static struct l3_process *
122 l3ni1_search_dummy_proc(struct PStack *st, int id)
123 { struct l3_process *pc = st->l3.proc; /* start of processes */
124
125   if (!id) return(NULL);
126
127   while (pc)
128    { if ((pc->callref == -1) && (pc->prot.ni1.invoke_id == id))
129        return(pc);
130      pc = pc->next;
131    } 
132   return(NULL);
133 } /* l3ni1_search_dummy_proc */
134
135 /*******************************************************************/
136 /* called when a facility message with a dummy callref is received */
137 /* and a return result is delivered. id specifies the invoke id.   */
138 /*******************************************************************/ 
139 static void 
140 l3ni1_dummy_return_result(struct PStack *st, int id, u_char *p, u_char nlen)
141 { isdn_ctrl ic;
142   struct IsdnCardState *cs;
143   struct l3_process *pc = NULL; 
144
145   if ((pc = l3ni1_search_dummy_proc(st, id)))
146    { L3DelTimer(&pc->timer); /* remove timer */
147
148      cs = pc->st->l1.hardware;
149      ic.driver = cs->myid;
150      ic.command = ISDN_STAT_PROT;
151      ic.arg = NI1_STAT_INVOKE_RES;
152      ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
153      ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
154      ic.parm.ni1_io.proc = pc->prot.ni1.proc;
155      ic.parm.ni1_io.timeout= 0;
156      ic.parm.ni1_io.datalen = nlen;
157      ic.parm.ni1_io.data = p;
158      free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
159      pc->prot.ni1.invoke_id = 0; /* reset id */
160
161      cs->iif.statcallb(&ic);
162      ni1_release_l3_process(pc); 
163    }
164   else
165    l3_debug(st, "dummy return result id=0x%x result len=%d",id,nlen);
166 } /* l3ni1_dummy_return_result */
167
168 /*******************************************************************/
169 /* called when a facility message with a dummy callref is received */
170 /* and a return error is delivered. id specifies the invoke id.    */
171 /*******************************************************************/ 
172 static void 
173 l3ni1_dummy_error_return(struct PStack *st, int id, ulong error)
174 { isdn_ctrl ic;
175   struct IsdnCardState *cs;
176   struct l3_process *pc = NULL; 
177
178   if ((pc = l3ni1_search_dummy_proc(st, id)))
179    { L3DelTimer(&pc->timer); /* remove timer */
180
181      cs = pc->st->l1.hardware;
182      ic.driver = cs->myid;
183      ic.command = ISDN_STAT_PROT;
184      ic.arg = NI1_STAT_INVOKE_ERR;
185      ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
186      ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
187      ic.parm.ni1_io.proc = pc->prot.ni1.proc;
188      ic.parm.ni1_io.timeout= error;
189      ic.parm.ni1_io.datalen = 0;
190      ic.parm.ni1_io.data = NULL;
191      free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
192      pc->prot.ni1.invoke_id = 0; /* reset id */
193
194      cs->iif.statcallb(&ic);
195      ni1_release_l3_process(pc); 
196    }
197   else
198    l3_debug(st, "dummy return error id=0x%x error=0x%lx",id,error);
199 } /* l3ni1_error_return */
200
201 /*******************************************************************/
202 /* called when a facility message with a dummy callref is received */
203 /* and a invoke is delivered. id specifies the invoke id.          */
204 /*******************************************************************/ 
205 static void 
206 l3ni1_dummy_invoke(struct PStack *st, int cr, int id, 
207                     int ident, u_char *p, u_char nlen)
208 { isdn_ctrl ic;
209   struct IsdnCardState *cs;
210
211   l3_debug(st, "dummy invoke %s id=0x%x ident=0x%x datalen=%d",
212                (cr == -1) ? "local" : "broadcast",id,ident,nlen);
213   if (cr >= -1) return; /* ignore local data */
214
215   cs = st->l1.hardware;
216   ic.driver = cs->myid;
217   ic.command = ISDN_STAT_PROT;
218   ic.arg = NI1_STAT_INVOKE_BRD;
219   ic.parm.ni1_io.hl_id = id;
220   ic.parm.ni1_io.ll_id = 0;
221   ic.parm.ni1_io.proc = ident;
222   ic.parm.ni1_io.timeout= 0;
223   ic.parm.ni1_io.datalen = nlen;
224   ic.parm.ni1_io.data = p;
225
226   cs->iif.statcallb(&ic);
227 } /* l3ni1_dummy_invoke */
228
229 static void
230 l3ni1_parse_facility(struct PStack *st, struct l3_process *pc,
231                       int cr, u_char * p)
232 {
233         int qd_len = 0;
234         unsigned char nlen = 0, ilen, cp_tag;
235         int ident, id;
236         ulong err_ret;
237
238         if (pc) 
239                 st = pc->st; /* valid Stack */
240         else
241                 if ((!st) || (cr >= 0)) return; /* neither pc nor st specified */
242
243         p++;
244         qd_len = *p++;
245         if (qd_len == 0) {
246                 l3_debug(st, "qd_len == 0");
247                 return;
248         }
249         if ((*p & 0x1F) != 0x11) {      /* Service discriminator, supplementary service */
250                 l3_debug(st, "supplementary service != 0x11");
251                 return;
252         }
253         while (qd_len > 0 && !(*p & 0x80)) {    /* extension ? */
254                 p++;
255                 qd_len--;
256         }
257         if (qd_len < 2) {
258                 l3_debug(st, "qd_len < 2");
259                 return;
260         }
261         p++;
262         qd_len--;
263         if ((*p & 0xE0) != 0xA0) {      /* class and form */
264                 l3_debug(st, "class and form != 0xA0");
265                 return;
266         }
267        
268         cp_tag = *p & 0x1F; /* remember tag value */
269
270         p++;
271         qd_len--;
272         if (qd_len < 1) 
273           { l3_debug(st, "qd_len < 1");
274             return;
275           }
276         if (*p & 0x80) 
277           { /* length format indefinite or limited */
278             nlen = *p++ & 0x7F; /* number of len bytes or indefinite */
279             if ((qd_len-- < ((!nlen) ? 3 : (1 + nlen))) ||
280                 (nlen > 1))   
281              { l3_debug(st, "length format error or not implemented");
282                return;
283              }
284             if (nlen == 1)
285              { nlen = *p++; /* complete length */
286                qd_len--;
287              } 
288             else
289              { qd_len -= 2; /* trailing null bytes */
290                if ((*(p+qd_len)) || (*(p+qd_len+1)))
291                 { l3_debug(st,"length format indefinite error");
292                   return;
293                 }
294                nlen = qd_len;
295              }
296           }
297         else
298           { nlen = *p++;
299             qd_len--;
300           } 
301         if (qd_len < nlen) 
302           { l3_debug(st, "qd_len < nlen");
303             return;
304           }
305         qd_len -= nlen;
306
307         if (nlen < 2) 
308           { l3_debug(st, "nlen < 2");
309             return;
310           }
311         if (*p != 0x02) 
312           {  /* invoke identifier tag */
313              l3_debug(st, "invoke identifier tag !=0x02");
314              return;
315           }
316         p++;
317         nlen--;
318         if (*p & 0x80) 
319           { /* length format */
320             l3_debug(st, "invoke id length format 2");
321             return;
322           }
323         ilen = *p++;
324         nlen--;
325         if (ilen > nlen || ilen == 0) 
326           { l3_debug(st, "ilen > nlen || ilen == 0");
327             return;
328           }
329         nlen -= ilen;
330         id = 0;
331         while (ilen > 0) 
332           { id = (id << 8) | (*p++ & 0xFF);     /* invoke identifier */
333             ilen--;
334           }
335
336         switch (cp_tag) {       /* component tag */
337                 case 1: /* invoke */
338                                 if (nlen < 2) {
339                                         l3_debug(st, "nlen < 2 22");
340                                         return;
341                                 }
342                                 if (*p != 0x02) {       /* operation value */
343                                         l3_debug(st, "operation value !=0x02");
344                                         return;
345                                 }
346                                 p++;
347                                 nlen--;
348                                 ilen = *p++;
349                                 nlen--;
350                                 if (ilen > nlen || ilen == 0) {
351                                         l3_debug(st, "ilen > nlen || ilen == 0 22");
352                                         return;
353                                 }
354                                 nlen -= ilen;
355                                 ident = 0;
356                                 while (ilen > 0) {
357                                         ident = (ident << 8) | (*p++ & 0xFF);
358                                         ilen--;
359                                 }
360
361                                 if (!pc) 
362                                 {
363                                         l3ni1_dummy_invoke(st, cr, id, ident, p, nlen);
364                                         return;
365                                 } 
366                                 l3_debug(st, "invoke break");
367                                 break;
368                 case 2: /* return result */
369                          /* if no process available handle separately */ 
370                         if (!pc)
371                          { if (cr == -1) 
372                              l3ni1_dummy_return_result(st, id, p, nlen);
373                            return; 
374                          }   
375                         if ((pc->prot.ni1.invoke_id) && (pc->prot.ni1.invoke_id == id))
376                           { /* Diversion successful */
377                             free_invoke_id(st,pc->prot.ni1.invoke_id);
378                             pc->prot.ni1.remote_result = 0; /* success */     
379                             pc->prot.ni1.invoke_id = 0;
380                             pc->redir_result = pc->prot.ni1.remote_result; 
381                             st->l3.l3l4(st, CC_REDIR | INDICATION, pc);                                  } /* Diversion successful */
382                         else
383                           l3_debug(st,"return error unknown identifier");
384                         break;
385                 case 3: /* return error */
386                             err_ret = 0;
387                             if (nlen < 2) 
388                               { l3_debug(st, "return error nlen < 2");
389                                 return;
390                               }
391                             if (*p != 0x02) 
392                               { /* result tag */
393                                 l3_debug(st, "invoke error tag !=0x02");
394                                 return;
395                               }
396                             p++;
397                             nlen--;
398                             if (*p > 4) 
399                               { /* length format */
400                                 l3_debug(st, "invoke return errlen > 4 ");
401                                 return;
402                               }
403                             ilen = *p++;
404                             nlen--;
405                             if (ilen > nlen || ilen == 0) 
406                               { l3_debug(st, "error return ilen > nlen || ilen == 0");
407                                 return;
408                                }
409                             nlen -= ilen;
410                             while (ilen > 0) 
411                              { err_ret = (err_ret << 8) | (*p++ & 0xFF);        /* error value */
412                                ilen--;
413                              }
414                          /* if no process available handle separately */ 
415                         if (!pc)
416                          { if (cr == -1)
417                              l3ni1_dummy_error_return(st, id, err_ret);
418                            return; 
419                          }   
420                         if ((pc->prot.ni1.invoke_id) && (pc->prot.ni1.invoke_id == id))
421                           { /* Deflection error */
422                             free_invoke_id(st,pc->prot.ni1.invoke_id);
423                             pc->prot.ni1.remote_result = err_ret; /* result */
424                             pc->prot.ni1.invoke_id = 0; 
425                             pc->redir_result = pc->prot.ni1.remote_result; 
426                             st->l3.l3l4(st, CC_REDIR | INDICATION, pc);  
427                           } /* Deflection error */
428                         else
429                           l3_debug(st,"return result unknown identifier");
430                         break;
431                 default:
432                         l3_debug(st, "facility default break tag=0x%02x",cp_tag);
433                         break;
434         }
435 }
436
437 static void
438 l3ni1_message(struct l3_process *pc, u_char mt)
439 {
440         struct sk_buff *skb;
441         u_char *p;
442
443         if (!(skb = l3_alloc_skb(4)))
444                 return;
445         p = skb_put(skb, 4);
446         MsgHead(p, pc->callref, mt);
447         l3_msg(pc->st, DL_DATA | REQUEST, skb);
448 }
449
450 static void
451 l3ni1_message_plus_chid(struct l3_process *pc, u_char mt)
452 /* sends an l3 messages plus channel id -  added GE 05/09/00 */
453 {
454         struct sk_buff *skb;
455         u_char tmp[16];
456         u_char *p = tmp;
457         u_char chid;
458
459         chid = (u_char)(pc->para.bchannel & 0x03) | 0x88;
460         MsgHead(p, pc->callref, mt);
461         *p++ = IE_CHANNEL_ID;
462         *p++ = 0x01;
463         *p++ = chid;
464
465         if (!(skb = l3_alloc_skb(7)))
466                 return;
467         memcpy(skb_put(skb, 7), tmp, 7);
468         l3_msg(pc->st, DL_DATA | REQUEST, skb);
469 }
470
471 static void
472 l3ni1_message_cause(struct l3_process *pc, u_char mt, u_char cause)
473 {
474         struct sk_buff *skb;
475         u_char tmp[16];
476         u_char *p = tmp;
477         int l;
478
479         MsgHead(p, pc->callref, mt);
480         *p++ = IE_CAUSE;
481         *p++ = 0x2;
482         *p++ = 0x80;
483         *p++ = cause | 0x80;
484
485         l = p - tmp;
486         if (!(skb = l3_alloc_skb(l)))
487                 return;
488         memcpy(skb_put(skb, l), tmp, l);
489         l3_msg(pc->st, DL_DATA | REQUEST, skb);
490 }
491
492 static void
493 l3ni1_status_send(struct l3_process *pc, u_char pr, void *arg)
494 {
495         u_char tmp[16];
496         u_char *p = tmp;
497         int l;
498         struct sk_buff *skb;
499
500         MsgHead(p, pc->callref, MT_STATUS);
501
502         *p++ = IE_CAUSE;
503         *p++ = 0x2;
504         *p++ = 0x80;
505         *p++ = pc->para.cause | 0x80;
506
507         *p++ = IE_CALL_STATE;
508         *p++ = 0x1;
509         *p++ = pc->state & 0x3f;
510
511         l = p - tmp;
512         if (!(skb = l3_alloc_skb(l)))
513                 return;
514         memcpy(skb_put(skb, l), tmp, l);
515         l3_msg(pc->st, DL_DATA | REQUEST, skb);
516 }
517
518 static void
519 l3ni1_msg_without_setup(struct l3_process *pc, u_char pr, void *arg)
520 {
521         /* This routine is called if here was no SETUP made (checks in ni1up and in
522          * l3ni1_setup) and a RELEASE_COMPLETE have to be sent with an error code
523          * MT_STATUS_ENQUIRE in the NULL state is handled too
524          */
525         u_char tmp[16];
526         u_char *p = tmp;
527         int l;
528         struct sk_buff *skb;
529
530         switch (pc->para.cause) {
531                 case 81:        /* invalid callreference */
532                 case 88:        /* incomp destination */
533                 case 96:        /* mandory IE missing */
534                 case 100:       /* invalid IE contents */
535                 case 101:       /* incompatible Callstate */
536                         MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
537                         *p++ = IE_CAUSE;
538                         *p++ = 0x2;
539                         *p++ = 0x80;
540                         *p++ = pc->para.cause | 0x80;
541                         break;
542                 default:
543                         printk(KERN_ERR "HiSax l3ni1_msg_without_setup wrong cause %d\n",
544                                 pc->para.cause);
545                         return;
546         }
547         l = p - tmp;
548         if (!(skb = l3_alloc_skb(l)))
549                 return;
550         memcpy(skb_put(skb, l), tmp, l);
551         l3_msg(pc->st, DL_DATA | REQUEST, skb);
552         ni1_release_l3_process(pc);
553 }
554
555 static int ie_ALERTING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
556                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_HLC,
557                 IE_USER_USER, -1};
558 static int ie_CALL_PROCEEDING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
559                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_HLC, -1};
560 static int ie_CONNECT[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1, 
561                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_DATE, IE_SIGNAL,
562                 IE_CONNECT_PN, IE_CONNECT_SUB, IE_LLC, IE_HLC, IE_USER_USER, -1};
563 static int ie_CONNECT_ACKNOWLEDGE[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_SIGNAL, -1};
564 static int ie_DISCONNECT[] = {IE_CAUSE | IE_MANDATORY, IE_FACILITY,
565                 IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
566 static int ie_INFORMATION[] = {IE_COMPLETE, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL,
567                 IE_CALLED_PN, -1};
568 static int ie_NOTIFY[] = {IE_BEARER, IE_NOTIFY | IE_MANDATORY, IE_DISPLAY, -1};
569 static int ie_PROGRESS[] = {IE_BEARER, IE_CAUSE, IE_FACILITY, IE_PROGRESS |
570                 IE_MANDATORY, IE_DISPLAY, IE_HLC, IE_USER_USER, -1};
571 static int ie_RELEASE[] = {IE_CAUSE | IE_MANDATORY_1, IE_FACILITY, IE_DISPLAY,
572                 IE_SIGNAL, IE_USER_USER, -1};
573 /* a RELEASE_COMPLETE with errors don't require special actions 
574 static int ie_RELEASE_COMPLETE[] = {IE_CAUSE | IE_MANDATORY_1, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
575 */
576 static int ie_RESUME_ACKNOWLEDGE[] = {IE_CHANNEL_ID| IE_MANDATORY, IE_FACILITY,
577                 IE_DISPLAY, -1};
578 static int ie_RESUME_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
579 static int ie_SETUP[] = {IE_COMPLETE, IE_BEARER  | IE_MANDATORY,
580                 IE_CHANNEL_ID| IE_MANDATORY, IE_FACILITY, IE_PROGRESS,
581                 IE_NET_FAC, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL, IE_CALLING_PN,
582                 IE_CALLING_SUB, IE_CALLED_PN, IE_CALLED_SUB, IE_REDIR_NR,
583                 IE_LLC, IE_HLC, IE_USER_USER, -1};
584 static int ie_SETUP_ACKNOWLEDGE[] = {IE_CHANNEL_ID | IE_MANDATORY, IE_FACILITY,
585                 IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, -1};
586 static int ie_STATUS[] = {IE_CAUSE | IE_MANDATORY, IE_CALL_STATE |
587                 IE_MANDATORY, IE_DISPLAY, -1};
588 static int ie_STATUS_ENQUIRY[] = {IE_DISPLAY, -1};
589 static int ie_SUSPEND_ACKNOWLEDGE[] = {IE_DISPLAY, IE_FACILITY, -1};
590 static int ie_SUSPEND_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
591 /* not used 
592  * static int ie_CONGESTION_CONTROL[] = {IE_CONGESTION | IE_MANDATORY,
593  *              IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
594  * static int ie_USER_INFORMATION[] = {IE_MORE_DATA, IE_USER_USER | IE_MANDATORY, -1};
595  * static int ie_RESTART[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_RESTART_IND |
596  *              IE_MANDATORY, -1};
597  */
598 static int ie_FACILITY[] = {IE_FACILITY | IE_MANDATORY, IE_DISPLAY, -1};
599 static int comp_required[] = {1,2,3,5,6,7,9,10,11,14,15,-1};
600 static int l3_valid_states[] = {0,1,2,3,4,6,7,8,9,10,11,12,15,17,19,25,-1};
601
602 struct ie_len {
603         int ie;
604         int len;
605 };
606
607 static
608 struct ie_len max_ie_len[] = {
609         {IE_SEGMENT, 4},
610         {IE_BEARER, 12},
611         {IE_CAUSE, 32},
612         {IE_CALL_ID, 10},
613         {IE_CALL_STATE, 3},
614         {IE_CHANNEL_ID, 34},
615         {IE_FACILITY, 255},
616         {IE_PROGRESS, 4},
617         {IE_NET_FAC, 255},
618         {IE_NOTIFY, 3},
619         {IE_DISPLAY, 82},
620         {IE_DATE, 8},
621         {IE_KEYPAD, 34},
622         {IE_SIGNAL, 3},
623         {IE_INFORATE, 6},
624         {IE_E2E_TDELAY, 11},
625         {IE_TDELAY_SEL, 5},
626         {IE_PACK_BINPARA, 3},
627         {IE_PACK_WINSIZE, 4},
628         {IE_PACK_SIZE, 4},
629         {IE_CUG, 7},
630         {IE_REV_CHARGE, 3},
631         {IE_CALLING_PN, 24},
632         {IE_CALLING_SUB, 23},
633         {IE_CALLED_PN, 24},
634         {IE_CALLED_SUB, 23},
635         {IE_REDIR_NR, 255},
636         {IE_TRANS_SEL, 255},
637         {IE_RESTART_IND, 3},
638         {IE_LLC, 18},
639         {IE_HLC, 5},
640         {IE_USER_USER, 131},
641         {-1,0},
642 };
643
644 static int
645 getmax_ie_len(u_char ie) {
646         int i = 0;
647         while (max_ie_len[i].ie != -1) {
648                 if (max_ie_len[i].ie == ie)
649                         return(max_ie_len[i].len);
650                 i++;
651         }
652         return(255);
653 }
654
655 static int
656 ie_in_set(struct l3_process *pc, u_char ie, int *checklist) {
657         int ret = 1;
658
659         while (*checklist != -1) {
660                 if ((*checklist & 0xff) == ie) {
661                         if (ie & 0x80)
662                                 return(-ret);
663                         else
664                                 return(ret);
665                 }
666                 ret++;
667                 checklist++;
668         }
669         return(0);
670 }
671
672 static int
673 check_infoelements(struct l3_process *pc, struct sk_buff *skb, int *checklist)
674 {
675         int *cl = checklist;
676         u_char mt;
677         u_char *p, ie;
678         int l, newpos, oldpos;
679         int err_seq = 0, err_len = 0, err_compr = 0, err_ureg = 0;
680         u_char codeset = 0;
681         u_char old_codeset = 0;
682         u_char codelock = 1;
683         
684         p = skb->data;
685         /* skip cr */
686         p++;
687         l = (*p++) & 0xf;
688         p += l;
689         mt = *p++;
690         oldpos = 0;
691         while ((p - skb->data) < skb->len) {
692                 if ((*p & 0xf0) == 0x90) { /* shift codeset */
693                         old_codeset = codeset;
694                         codeset = *p & 7;
695                         if (*p & 0x08)
696                                 codelock = 0;
697                         else
698                                 codelock = 1;
699                         if (pc->debug & L3_DEB_CHECK)
700                                 l3_debug(pc->st, "check IE shift%scodeset %d->%d",
701                                         codelock ? " locking ": " ", old_codeset, codeset);
702                         p++;
703                         continue;
704                 }
705                 if (!codeset) { /* only codeset 0 */
706                         if ((newpos = ie_in_set(pc, *p, cl))) {
707                                 if (newpos > 0) {
708                                         if (newpos < oldpos)
709                                                 err_seq++;
710                                         else
711                                                 oldpos = newpos;
712                                 }
713                         } else {
714                                 if (ie_in_set(pc, *p, comp_required))
715                                         err_compr++;
716                                 else
717                                         err_ureg++;
718                         }
719                 }
720                 ie = *p++;
721                 if (ie & 0x80) {
722                         l = 1;
723                 } else {
724                         l = *p++;
725                         p += l;
726                         l += 2;
727                 }
728                 if (!codeset && (l > getmax_ie_len(ie)))
729                         err_len++;
730                 if (!codelock) {
731                         if (pc->debug & L3_DEB_CHECK)
732                                 l3_debug(pc->st, "check IE shift back codeset %d->%d",
733                                         codeset, old_codeset);
734                         codeset = old_codeset;
735                         codelock = 1;
736                 }
737         }
738         if (err_compr | err_ureg | err_len | err_seq) {
739                 if (pc->debug & L3_DEB_CHECK)
740                         l3_debug(pc->st, "check IE MT(%x) %d/%d/%d/%d",
741                                 mt, err_compr, err_ureg, err_len, err_seq);
742                 if (err_compr)
743                         return(ERR_IE_COMPREHENSION);
744                 if (err_ureg)
745                         return(ERR_IE_UNRECOGNIZED);
746                 if (err_len)
747                         return(ERR_IE_LENGTH);
748                 if (err_seq)
749                         return(ERR_IE_SEQUENCE);
750         } 
751         return(0);
752 }
753
754 /* verify if a message type exists and contain no IE error */
755 static int
756 l3ni1_check_messagetype_validity(struct l3_process *pc, int mt, void *arg)
757 {
758         switch (mt) {
759                 case MT_ALERTING:
760                 case MT_CALL_PROCEEDING:
761                 case MT_CONNECT:
762                 case MT_CONNECT_ACKNOWLEDGE:
763                 case MT_DISCONNECT:
764                 case MT_INFORMATION:
765                 case MT_FACILITY:
766                 case MT_NOTIFY:
767                 case MT_PROGRESS:
768                 case MT_RELEASE:
769                 case MT_RELEASE_COMPLETE:
770                 case MT_SETUP:
771                 case MT_SETUP_ACKNOWLEDGE:
772                 case MT_RESUME_ACKNOWLEDGE:
773                 case MT_RESUME_REJECT:
774                 case MT_SUSPEND_ACKNOWLEDGE:
775                 case MT_SUSPEND_REJECT:
776                 case MT_USER_INFORMATION:
777                 case MT_RESTART:
778                 case MT_RESTART_ACKNOWLEDGE:
779                 case MT_CONGESTION_CONTROL:
780                 case MT_STATUS:
781                 case MT_STATUS_ENQUIRY:
782                         if (pc->debug & L3_DEB_CHECK)
783                                 l3_debug(pc->st, "l3ni1_check_messagetype_validity mt(%x) OK", mt);
784                         break;
785                 case MT_RESUME: /* RESUME only in user->net */
786                 case MT_SUSPEND: /* SUSPEND only in user->net */
787                 default:
788                         if (pc->debug & (L3_DEB_CHECK | L3_DEB_WARN))
789                                 l3_debug(pc->st, "l3ni1_check_messagetype_validity mt(%x) fail", mt);
790                         pc->para.cause = 97;
791                         l3ni1_status_send(pc, 0, NULL);
792                         return(1);
793         }
794         return(0);
795 }
796
797 static void
798 l3ni1_std_ie_err(struct l3_process *pc, int ret) {
799
800         if (pc->debug & L3_DEB_CHECK)
801                 l3_debug(pc->st, "check_infoelements ret %d", ret);
802         switch(ret) {
803                 case 0: 
804                         break;
805                 case ERR_IE_COMPREHENSION:
806                         pc->para.cause = 96;
807                         l3ni1_status_send(pc, 0, NULL);
808                         break;
809                 case ERR_IE_UNRECOGNIZED:
810                         pc->para.cause = 99;
811                         l3ni1_status_send(pc, 0, NULL);
812                         break;
813                 case ERR_IE_LENGTH:
814                         pc->para.cause = 100;
815                         l3ni1_status_send(pc, 0, NULL);
816                         break;
817                 case ERR_IE_SEQUENCE:
818                 default:
819                         break;
820         }
821 }
822
823 static int
824 l3ni1_get_channel_id(struct l3_process *pc, struct sk_buff *skb) {
825         u_char *p;
826
827         p = skb->data;
828         if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
829                 p++;
830                 if (*p != 1) { /* len for BRI = 1 */
831                         if (pc->debug & L3_DEB_WARN)
832                                 l3_debug(pc->st, "wrong chid len %d", *p);
833                         return (-2);
834                 }
835                 p++;
836                 if (*p & 0x60) { /* only base rate interface */
837                         if (pc->debug & L3_DEB_WARN)
838                                 l3_debug(pc->st, "wrong chid %x", *p);
839                         return (-3);
840                 }
841                 return(*p & 0x3);
842         } else
843                 return(-1);
844 }
845
846 static int
847 l3ni1_get_cause(struct l3_process *pc, struct sk_buff *skb) {
848         u_char l, i=0;
849         u_char *p;
850
851         p = skb->data;
852         pc->para.cause = 31;
853         pc->para.loc = 0;
854         if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
855                 p++;
856                 l = *p++;
857                 if (l>30)
858                         return(1);
859                 if (l) {
860                         pc->para.loc = *p++;
861                         l--;
862                 } else {
863                         return(2);
864                 }
865                 if (l && !(pc->para.loc & 0x80)) {
866                         l--;
867                         p++; /* skip recommendation */
868                 }
869                 if (l) {
870                         pc->para.cause = *p++;
871                         l--;
872                         if (!(pc->para.cause & 0x80))
873                                 return(3);
874                 } else
875                         return(4);
876                 while (l && (i<6)) {
877                         pc->para.diag[i++] = *p++;
878                         l--;
879                 }
880         } else
881                 return(-1);
882         return(0);
883 }
884
885 static void
886 l3ni1_msg_with_uus(struct l3_process *pc, u_char cmd)
887 {
888         struct sk_buff *skb;
889         u_char tmp[16+40];
890         u_char *p = tmp;
891         int l;
892
893         MsgHead(p, pc->callref, cmd);
894
895         if (pc->prot.ni1.uus1_data[0])
896          { *p++ = IE_USER_USER; /* UUS info element */
897            *p++ = strlen(pc->prot.ni1.uus1_data) + 1;
898            *p++ = 0x04; /* IA5 chars */
899            strcpy(p,pc->prot.ni1.uus1_data);
900            p += strlen(pc->prot.ni1.uus1_data);
901            pc->prot.ni1.uus1_data[0] = '\0';   
902          } 
903
904         l = p - tmp;
905         if (!(skb = l3_alloc_skb(l)))
906                 return;
907         memcpy(skb_put(skb, l), tmp, l);
908         l3_msg(pc->st, DL_DATA | REQUEST, skb);
909 } /* l3ni1_msg_with_uus */
910
911 static void
912 l3ni1_release_req(struct l3_process *pc, u_char pr, void *arg)
913 {
914         StopAllL3Timer(pc);
915         newl3state(pc, 19);
916         if (!pc->prot.ni1.uus1_data[0]) 
917                 l3ni1_message(pc, MT_RELEASE);
918         else
919                 l3ni1_msg_with_uus(pc, MT_RELEASE);
920         L3AddTimer(&pc->timer, T308, CC_T308_1);
921 }
922
923 static void
924 l3ni1_release_cmpl(struct l3_process *pc, u_char pr, void *arg)
925 {
926         struct sk_buff *skb = arg;
927         int ret;
928
929         if ((ret = l3ni1_get_cause(pc, skb))>0) {
930                 if (pc->debug & L3_DEB_WARN)
931                         l3_debug(pc->st, "RELCMPL get_cause ret(%d)",ret);
932         } else if (ret < 0)
933                 pc->para.cause = NO_CAUSE;
934         StopAllL3Timer(pc);
935         newl3state(pc, 0);
936         pc->st->l3.l3l4(pc->st, CC_RELEASE | CONFIRM, pc);
937         ni1_release_l3_process(pc);
938 }
939
940 #if EXT_BEARER_CAPS
941
942 static u_char *
943 EncodeASyncParams(u_char * p, u_char si2)
944 {                               // 7c 06 88  90 21 42 00 bb
945
946         p[0] = 0;
947         p[1] = 0x40;            // Intermediate rate: 16 kbit/s jj 2000.02.19
948         p[2] = 0x80;
949         if (si2 & 32)           // 7 data bits
950
951                 p[2] += 16;
952         else                    // 8 data bits
953
954                 p[2] += 24;
955
956         if (si2 & 16)           // 2 stop bits
957
958                 p[2] += 96;
959         else                    // 1 stop bit
960
961                 p[2] += 32;
962
963         if (si2 & 8)            // even parity
964
965                 p[2] += 2;
966         else                    // no parity
967
968                 p[2] += 3;
969
970         switch (si2 & 0x07) {
971                 case 0:
972                         p[0] = 66;      // 1200 bit/s
973
974                         break;
975                 case 1:
976                         p[0] = 88;      // 1200/75 bit/s
977
978                         break;
979                 case 2:
980                         p[0] = 87;      // 75/1200 bit/s
981
982                         break;
983                 case 3:
984                         p[0] = 67;      // 2400 bit/s
985
986                         break;
987                 case 4:
988                         p[0] = 69;      // 4800 bit/s
989
990                         break;
991                 case 5:
992                         p[0] = 72;      // 9600 bit/s
993
994                         break;
995                 case 6:
996                         p[0] = 73;      // 14400 bit/s
997
998                         break;
999                 case 7:
1000                         p[0] = 75;      // 19200 bit/s
1001
1002                         break;
1003         }
1004         return p + 3;
1005 }
1006
1007 static u_char
1008 EncodeSyncParams(u_char si2, u_char ai)
1009 {
1010
1011         switch (si2) {
1012                 case 0:
1013                         return ai + 2;  // 1200 bit/s
1014
1015                 case 1:
1016                         return ai + 24;         // 1200/75 bit/s
1017
1018                 case 2:
1019                         return ai + 23;         // 75/1200 bit/s
1020
1021                 case 3:
1022                         return ai + 3;  // 2400 bit/s
1023
1024                 case 4:
1025                         return ai + 5;  // 4800 bit/s
1026
1027                 case 5:
1028                         return ai + 8;  // 9600 bit/s
1029
1030                 case 6:
1031                         return ai + 9;  // 14400 bit/s
1032
1033                 case 7:
1034                         return ai + 11;         // 19200 bit/s
1035
1036                 case 8:
1037                         return ai + 14;         // 48000 bit/s
1038
1039                 case 9:
1040                         return ai + 15;         // 56000 bit/s
1041
1042                 case 15:
1043                         return ai + 40;         // negotiate bit/s
1044
1045                 default:
1046                         break;
1047         }
1048         return ai;
1049 }
1050
1051
1052 static u_char
1053 DecodeASyncParams(u_char si2, u_char * p)
1054 {
1055         u_char info;
1056
1057         switch (p[5]) {
1058                 case 66:        // 1200 bit/s
1059
1060                         break;  // si2 don't change
1061
1062                 case 88:        // 1200/75 bit/s
1063
1064                         si2 += 1;
1065                         break;
1066                 case 87:        // 75/1200 bit/s
1067
1068                         si2 += 2;
1069                         break;
1070                 case 67:        // 2400 bit/s
1071
1072                         si2 += 3;
1073                         break;
1074                 case 69:        // 4800 bit/s
1075
1076                         si2 += 4;
1077                         break;
1078                 case 72:        // 9600 bit/s
1079
1080                         si2 += 5;
1081                         break;
1082                 case 73:        // 14400 bit/s
1083
1084                         si2 += 6;
1085                         break;
1086                 case 75:        // 19200 bit/s
1087
1088                         si2 += 7;
1089                         break;
1090         }
1091
1092         info = p[7] & 0x7f;
1093         if ((info & 16) && (!(info & 8)))       // 7 data bits
1094
1095                 si2 += 32;      // else 8 data bits
1096
1097         if ((info & 96) == 96)  // 2 stop bits
1098
1099                 si2 += 16;      // else 1 stop bit
1100
1101         if ((info & 2) && (!(info & 1)))        // even parity
1102
1103                 si2 += 8;       // else no parity
1104
1105         return si2;
1106 }
1107
1108
1109 static u_char
1110 DecodeSyncParams(u_char si2, u_char info)
1111 {
1112         info &= 0x7f;
1113         switch (info) {
1114                 case 40:        // bit/s negotiation failed  ai := 165 not 175!
1115
1116                         return si2 + 15;
1117                 case 15:        // 56000 bit/s failed, ai := 0 not 169 !
1118
1119                         return si2 + 9;
1120                 case 14:        // 48000 bit/s
1121
1122                         return si2 + 8;
1123                 case 11:        // 19200 bit/s
1124
1125                         return si2 + 7;
1126                 case 9: // 14400 bit/s
1127
1128                         return si2 + 6;
1129                 case 8: // 9600  bit/s
1130
1131                         return si2 + 5;
1132                 case 5: // 4800  bit/s
1133
1134                         return si2 + 4;
1135                 case 3: // 2400  bit/s
1136
1137                         return si2 + 3;
1138                 case 23:        // 75/1200 bit/s
1139
1140                         return si2 + 2;
1141                 case 24:        // 1200/75 bit/s
1142
1143                         return si2 + 1;
1144                 default:        // 1200 bit/s
1145
1146                         return si2;
1147         }
1148 }
1149
1150 static u_char
1151 DecodeSI2(struct sk_buff *skb)
1152 {
1153         u_char *p;              //, *pend=skb->data + skb->len;
1154
1155         if ((p = findie(skb->data, skb->len, 0x7c, 0))) {
1156                 switch (p[4] & 0x0f) {
1157                         case 0x01:
1158                                 if (p[1] == 0x04)       // sync. Bitratenadaption
1159
1160                                         return DecodeSyncParams(160, p[5]);     // V.110/X.30
1161
1162                                 else if (p[1] == 0x06)  // async. Bitratenadaption
1163
1164                                         return DecodeASyncParams(192, p);       // V.110/X.30
1165
1166                                 break;
1167                         case 0x08:      // if (p[5] == 0x02) // sync. Bitratenadaption
1168                                 if (p[1] > 3) 
1169                                         return DecodeSyncParams(176, p[5]);     // V.120
1170                                 break;
1171                 }
1172         }
1173         return 0;
1174 }
1175
1176 #endif
1177
1178
1179 static void
1180 l3ni1_setup_req(struct l3_process *pc, u_char pr,
1181                  void *arg)
1182 {
1183         struct sk_buff *skb;
1184         u_char tmp[128];
1185         u_char *p = tmp;
1186
1187         u_char *teln;
1188         u_char *sub;
1189         u_char *sp;
1190         int l;
1191
1192         MsgHead(p, pc->callref, MT_SETUP);
1193
1194         teln = pc->para.setup.phone;
1195
1196         *p++ = 0xa1;            /* complete indicator */
1197         /*
1198          * Set Bearer Capability, Map info from 1TR6-convention to NI1
1199          */
1200         switch (pc->para.setup.si1) {
1201         case 1:                   /* Telephony                                */
1202                 *p++ = IE_BEARER;
1203                 *p++ = 0x3;       /* Length                                   */
1204                 *p++ = 0x90;      /* 3.1khz Audio                             */
1205                 *p++ = 0x90;      /* Circuit-Mode 64kbps                      */
1206                 *p++ = 0xa2;      /* u-Law Audio                              */
1207                 break;
1208         case 5:                   /* Datatransmission 64k, BTX                */
1209         case 7:                   /* Datatransmission 64k                     */
1210         default:
1211                 *p++ = IE_BEARER;
1212                 *p++ = 0x2;       /* Length                                   */
1213                 *p++ = 0x88;      /* Coding Std. CCITT, unrestr. dig. Inform. */
1214                 *p++ = 0x90;      /* Circuit-Mode 64kbps                      */
1215                 break;
1216         }
1217
1218         sub = NULL;
1219         sp = teln;
1220         while (*sp) {
1221                 if ('.' == *sp) {
1222                         sub = sp;
1223                         *sp = 0;
1224                 } else
1225                         sp++;
1226         }
1227         
1228         *p++ = IE_KEYPAD;
1229         *p++ = strlen(teln);
1230         while (*teln)
1231                 *p++ = (*teln++) & 0x7F;
1232
1233         if (sub)
1234                 *sub++ = '.';
1235         
1236 #if EXT_BEARER_CAPS
1237         if ((pc->para.setup.si2 >= 160) && (pc->para.setup.si2 <= 175)) {       // sync. Bitratenadaption, V.110/X.30
1238
1239                 *p++ = IE_LLC;
1240                 *p++ = 0x04;
1241                 *p++ = 0x88;
1242                 *p++ = 0x90;
1243                 *p++ = 0x21;
1244                 *p++ = EncodeSyncParams(pc->para.setup.si2 - 160, 0x80);
1245         } else if ((pc->para.setup.si2 >= 176) && (pc->para.setup.si2 <= 191)) {        // sync. Bitratenadaption, V.120
1246
1247                 *p++ = IE_LLC;
1248                 *p++ = 0x05;
1249                 *p++ = 0x88;
1250                 *p++ = 0x90;
1251                 *p++ = 0x28;
1252                 *p++ = EncodeSyncParams(pc->para.setup.si2 - 176, 0);
1253                 *p++ = 0x82;
1254         } else if (pc->para.setup.si2 >= 192) {         // async. Bitratenadaption, V.110/X.30
1255
1256                 *p++ = IE_LLC;
1257                 *p++ = 0x06;
1258                 *p++ = 0x88;
1259                 *p++ = 0x90;
1260                 *p++ = 0x21;
1261                 p = EncodeASyncParams(p, pc->para.setup.si2 - 192);
1262         } else {
1263           switch (pc->para.setup.si1) {
1264                 case 1:                 /* Telephony                                */
1265                         *p++ = IE_LLC;
1266                         *p++ = 0x3;     /* Length                                   */
1267                         *p++ = 0x90;    /* Coding Std. CCITT, 3.1 kHz audio         */
1268                         *p++ = 0x90;    /* Circuit-Mode 64kbps                      */
1269                         *p++ = 0xa2;    /* u-Law Audio                              */
1270                         break;
1271                 case 5:                 /* Datatransmission 64k, BTX                */
1272                 case 7:                 /* Datatransmission 64k                     */
1273                 default:
1274                         *p++ = IE_LLC;
1275                         *p++ = 0x2;     /* Length                                   */
1276                         *p++ = 0x88;    /* Coding Std. CCITT, unrestr. dig. Inform. */
1277                         *p++ = 0x90;    /* Circuit-Mode 64kbps                      */
1278                         break;
1279           }
1280         }
1281 #endif
1282         l = p - tmp;
1283         if (!(skb = l3_alloc_skb(l)))
1284 {
1285                 return;
1286 }
1287         memcpy(skb_put(skb, l), tmp, l);
1288         L3DelTimer(&pc->timer);
1289         L3AddTimer(&pc->timer, T303, CC_T303);
1290         newl3state(pc, 1);
1291         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1292 }
1293
1294 static void
1295 l3ni1_call_proc(struct l3_process *pc, u_char pr, void *arg)
1296 {
1297         struct sk_buff *skb = arg;
1298         int id, ret;
1299
1300         if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
1301                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
1302                         if (pc->debug & L3_DEB_WARN)
1303                                 l3_debug(pc->st, "setup answer with wrong chid %x", id);
1304                         pc->para.cause = 100;
1305                         l3ni1_status_send(pc, pr, NULL);
1306                         return;
1307                 }
1308                 pc->para.bchannel = id;
1309         } else if (1 == pc->state) {
1310                 if (pc->debug & L3_DEB_WARN)
1311                         l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
1312                 if (id == -1)
1313                         pc->para.cause = 96;
1314                 else
1315                         pc->para.cause = 100;
1316                 l3ni1_status_send(pc, pr, NULL);
1317                 return;
1318         }
1319         /* Now we are on none mandatory IEs */
1320         ret = check_infoelements(pc, skb, ie_CALL_PROCEEDING);
1321         if (ERR_IE_COMPREHENSION == ret) {
1322                 l3ni1_std_ie_err(pc, ret);
1323                 return;
1324         }
1325         L3DelTimer(&pc->timer);
1326         newl3state(pc, 3);
1327         L3AddTimer(&pc->timer, T310, CC_T310);
1328         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
1329                 l3ni1_std_ie_err(pc, ret);
1330         pc->st->l3.l3l4(pc->st, CC_PROCEEDING | INDICATION, pc);
1331 }
1332
1333 static void
1334 l3ni1_setup_ack(struct l3_process *pc, u_char pr, void *arg)
1335 {
1336         struct sk_buff *skb = arg;
1337         int id, ret;
1338
1339         if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
1340                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
1341                         if (pc->debug & L3_DEB_WARN)
1342                                 l3_debug(pc->st, "setup answer with wrong chid %x", id);
1343                         pc->para.cause = 100;
1344                         l3ni1_status_send(pc, pr, NULL);
1345                         return;
1346                 }
1347                 pc->para.bchannel = id;
1348         } else {
1349                 if (pc->debug & L3_DEB_WARN)
1350                         l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
1351                 if (id == -1)
1352                         pc->para.cause = 96;
1353                 else
1354                         pc->para.cause = 100;
1355                 l3ni1_status_send(pc, pr, NULL);
1356                 return;
1357         }
1358         /* Now we are on none mandatory IEs */
1359         ret = check_infoelements(pc, skb, ie_SETUP_ACKNOWLEDGE);
1360         if (ERR_IE_COMPREHENSION == ret) {
1361                 l3ni1_std_ie_err(pc, ret);
1362                 return;
1363         }
1364         L3DelTimer(&pc->timer);
1365         newl3state(pc, 2);
1366         L3AddTimer(&pc->timer, T304, CC_T304);
1367         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
1368                 l3ni1_std_ie_err(pc, ret);
1369         pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
1370 }
1371
1372 static void
1373 l3ni1_disconnect(struct l3_process *pc, u_char pr, void *arg)
1374 {
1375         struct sk_buff *skb = arg;
1376         u_char *p;
1377         int ret;
1378         u_char cause = 0;
1379
1380         StopAllL3Timer(pc);
1381         if ((ret = l3ni1_get_cause(pc, skb))) {
1382                 if (pc->debug & L3_DEB_WARN)
1383                         l3_debug(pc->st, "DISC get_cause ret(%d)", ret);
1384                 if (ret < 0)
1385                         cause = 96;
1386                 else if (ret > 0)
1387                         cause = 100;
1388         } 
1389         if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
1390                 l3ni1_parse_facility(pc->st, pc, pc->callref, p);
1391         ret = check_infoelements(pc, skb, ie_DISCONNECT);
1392         if (ERR_IE_COMPREHENSION == ret)
1393                 cause = 96;
1394         else if ((!cause) && (ERR_IE_UNRECOGNIZED == ret))
1395                 cause = 99;
1396         ret = pc->state;
1397         newl3state(pc, 12);
1398         if (cause)
1399                 newl3state(pc, 19);
1400         if (11 != ret)
1401                 pc->st->l3.l3l4(pc->st, CC_DISCONNECT | INDICATION, pc);
1402         else if (!cause)
1403                    l3ni1_release_req(pc, pr, NULL);
1404         if (cause) {
1405                 l3ni1_message_cause(pc, MT_RELEASE, cause);
1406                 L3AddTimer(&pc->timer, T308, CC_T308_1);
1407         }
1408 }
1409
1410 static void
1411 l3ni1_connect(struct l3_process *pc, u_char pr, void *arg)
1412 {
1413         struct sk_buff *skb = arg;
1414         int ret;
1415
1416         ret = check_infoelements(pc, skb, ie_CONNECT);
1417         if (ERR_IE_COMPREHENSION == ret) {
1418                 l3ni1_std_ie_err(pc, ret);
1419                 return;
1420         }
1421         L3DelTimer(&pc->timer); /* T310 */
1422         newl3state(pc, 10);
1423         pc->para.chargeinfo = 0;
1424         /* here should inserted COLP handling KKe */
1425         if (ret)
1426                 l3ni1_std_ie_err(pc, ret);
1427         pc->st->l3.l3l4(pc->st, CC_SETUP | CONFIRM, pc);
1428 }
1429
1430 static void
1431 l3ni1_alerting(struct l3_process *pc, u_char pr, void *arg)
1432 {
1433         struct sk_buff *skb = arg;
1434         int ret;
1435
1436         ret = check_infoelements(pc, skb, ie_ALERTING);
1437         if (ERR_IE_COMPREHENSION == ret) {
1438                 l3ni1_std_ie_err(pc, ret);
1439                 return;
1440         }
1441         L3DelTimer(&pc->timer); /* T304 */
1442         newl3state(pc, 4);
1443         if (ret)
1444                 l3ni1_std_ie_err(pc, ret);
1445         pc->st->l3.l3l4(pc->st, CC_ALERTING | INDICATION, pc);
1446 }
1447
1448 static void
1449 l3ni1_setup(struct l3_process *pc, u_char pr, void *arg)
1450 {
1451         u_char *p;
1452         int bcfound = 0;
1453         char tmp[80];
1454         struct sk_buff *skb = arg;
1455         int id;
1456         int err = 0;
1457
1458         /*
1459          * Bearer Capabilities
1460          */
1461         p = skb->data;
1462         /* only the first occurence 'll be detected ! */
1463         if ((p = findie(p, skb->len, 0x04, 0))) {
1464                 if ((p[1] < 2) || (p[1] > 11))
1465                         err = 1;
1466                 else {
1467                         pc->para.setup.si2 = 0;
1468                         switch (p[2] & 0x7f) {
1469                                 case 0x00: /* Speech */
1470                                 case 0x10: /* 3.1 Khz audio */
1471                                         pc->para.setup.si1 = 1;
1472                                         break;
1473                                 case 0x08: /* Unrestricted digital information */
1474                                         pc->para.setup.si1 = 7;
1475 /* JIM, 05.11.97 I wanna set service indicator 2 */
1476 #if EXT_BEARER_CAPS
1477                                         pc->para.setup.si2 = DecodeSI2(skb);
1478 #endif
1479                                         break;
1480                                 case 0x09: /* Restricted digital information */
1481                                         pc->para.setup.si1 = 2;
1482                                         break;
1483                                 case 0x11:
1484                                         /* Unrestr. digital information  with 
1485                                          * tones/announcements ( or 7 kHz audio
1486                                          */
1487                                         pc->para.setup.si1 = 3;
1488                                         break;
1489                                 case 0x18: /* Video */
1490                                         pc->para.setup.si1 = 4;
1491                                         break;
1492                                 default:
1493                                         err = 2;
1494                                         break;
1495                         }
1496                         switch (p[3] & 0x7f) {
1497                                 case 0x40: /* packed mode */
1498                                         pc->para.setup.si1 = 8;
1499                                         break;
1500                                 case 0x10: /* 64 kbit */
1501                                 case 0x11: /* 2*64 kbit */
1502                                 case 0x13: /* 384 kbit */
1503                                 case 0x15: /* 1536 kbit */
1504                                 case 0x17: /* 1920 kbit */
1505                                         pc->para.moderate = p[3] & 0x7f;
1506                                         break;
1507                                 default:
1508                                         err = 3;
1509                                         break;
1510                         }
1511                 }
1512                 if (pc->debug & L3_DEB_SI)
1513                         l3_debug(pc->st, "SI=%d, AI=%d",
1514                                 pc->para.setup.si1, pc->para.setup.si2);
1515                 if (err) {
1516                         if (pc->debug & L3_DEB_WARN)
1517                                 l3_debug(pc->st, "setup with wrong bearer(l=%d:%x,%x)",
1518                                         p[1], p[2], p[3]);
1519                         pc->para.cause = 100;
1520                         l3ni1_msg_without_setup(pc, pr, NULL);
1521                         return;
1522                 }
1523         } else {
1524                 if (pc->debug & L3_DEB_WARN)
1525                         l3_debug(pc->st, "setup without bearer capabilities");
1526                 /* ETS 300-104 1.3.3 */
1527                 pc->para.cause = 96;
1528                 l3ni1_msg_without_setup(pc, pr, NULL);
1529                 return;
1530         }
1531         /*
1532          * Channel Identification
1533          */
1534         if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
1535                 if ((pc->para.bchannel = id)) {
1536                         if ((3 == id) && (0x10 == pc->para.moderate)) {
1537                                 if (pc->debug & L3_DEB_WARN)
1538                                         l3_debug(pc->st, "setup with wrong chid %x",
1539                                                 id);
1540                                 pc->para.cause = 100;
1541                                 l3ni1_msg_without_setup(pc, pr, NULL);
1542                                 return;
1543                         }
1544                         bcfound++;
1545                 } else 
1546                    { if (pc->debug & L3_DEB_WARN)
1547                          l3_debug(pc->st, "setup without bchannel, call waiting");
1548                      bcfound++;
1549                    } 
1550         } else {
1551                 if (pc->debug & L3_DEB_WARN)
1552                         l3_debug(pc->st, "setup with wrong chid ret %d", id);
1553                 if (id == -1)
1554                         pc->para.cause = 96;
1555                 else
1556                         pc->para.cause = 100;
1557                 l3ni1_msg_without_setup(pc, pr, NULL);
1558                 return;
1559         }
1560         /* Now we are on none mandatory IEs */
1561         err = check_infoelements(pc, skb, ie_SETUP);
1562         if (ERR_IE_COMPREHENSION == err) {
1563                 pc->para.cause = 96;
1564                 l3ni1_msg_without_setup(pc, pr, NULL);
1565                 return;
1566         }
1567         p = skb->data;
1568         if ((p = findie(p, skb->len, 0x70, 0)))
1569                 iecpy(pc->para.setup.eazmsn, p, 1);
1570         else
1571                 pc->para.setup.eazmsn[0] = 0;
1572
1573         p = skb->data;
1574         if ((p = findie(p, skb->len, 0x71, 0))) {
1575                 /* Called party subaddress */
1576                 if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
1577                         tmp[0] = '.';
1578                         iecpy(&tmp[1], p, 2);
1579                         strcat(pc->para.setup.eazmsn, tmp);
1580                 } else if (pc->debug & L3_DEB_WARN)
1581                         l3_debug(pc->st, "wrong called subaddress");
1582         }
1583         p = skb->data;
1584         if ((p = findie(p, skb->len, 0x6c, 0))) {
1585                 pc->para.setup.plan = p[2];
1586                 if (p[2] & 0x80) {
1587                         iecpy(pc->para.setup.phone, p, 1);
1588                         pc->para.setup.screen = 0;
1589                 } else {
1590                         iecpy(pc->para.setup.phone, p, 2);
1591                         pc->para.setup.screen = p[3];
1592                 }
1593         } else {
1594                 pc->para.setup.phone[0] = 0;
1595                 pc->para.setup.plan = 0;
1596                 pc->para.setup.screen = 0;
1597         }
1598         p = skb->data;
1599         if ((p = findie(p, skb->len, 0x6d, 0))) {
1600                 /* Calling party subaddress */
1601                 if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
1602                         tmp[0] = '.';
1603                         iecpy(&tmp[1], p, 2);
1604                         strcat(pc->para.setup.phone, tmp);
1605                 } else if (pc->debug & L3_DEB_WARN)
1606                         l3_debug(pc->st, "wrong calling subaddress");
1607         }
1608         newl3state(pc, 6);
1609         if (err) /* STATUS for none mandatory IE errors after actions are taken */
1610                 l3ni1_std_ie_err(pc, err);
1611         pc->st->l3.l3l4(pc->st, CC_SETUP | INDICATION, pc);
1612 }
1613
1614 static void
1615 l3ni1_reset(struct l3_process *pc, u_char pr, void *arg)
1616 {
1617         ni1_release_l3_process(pc);
1618 }
1619
1620 static void
1621 l3ni1_disconnect_req(struct l3_process *pc, u_char pr, void *arg)
1622 {
1623         struct sk_buff *skb;
1624         u_char tmp[16+40];
1625         u_char *p = tmp;
1626         int l;
1627         u_char cause = 16;
1628
1629         if (pc->para.cause != NO_CAUSE)
1630                 cause = pc->para.cause;
1631
1632         StopAllL3Timer(pc);
1633
1634         MsgHead(p, pc->callref, MT_DISCONNECT);
1635
1636         *p++ = IE_CAUSE;
1637         *p++ = 0x2;
1638         *p++ = 0x80;
1639         *p++ = cause | 0x80;
1640
1641         if (pc->prot.ni1.uus1_data[0])
1642          { *p++ = IE_USER_USER; /* UUS info element */
1643            *p++ = strlen(pc->prot.ni1.uus1_data) + 1;
1644            *p++ = 0x04; /* IA5 chars */
1645            strcpy(p,pc->prot.ni1.uus1_data);
1646            p += strlen(pc->prot.ni1.uus1_data);
1647            pc->prot.ni1.uus1_data[0] = '\0';   
1648          } 
1649
1650         l = p - tmp;
1651         if (!(skb = l3_alloc_skb(l)))
1652                 return;
1653         memcpy(skb_put(skb, l), tmp, l);
1654         newl3state(pc, 11);
1655         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1656         L3AddTimer(&pc->timer, T305, CC_T305);
1657 }
1658
1659 static void
1660 l3ni1_setup_rsp(struct l3_process *pc, u_char pr,
1661                  void *arg)
1662 {
1663         if (!pc->para.bchannel) 
1664          { if (pc->debug & L3_DEB_WARN)
1665                l3_debug(pc->st, "D-chan connect for waiting call");
1666            l3ni1_disconnect_req(pc, pr, arg);
1667            return;
1668          }
1669         newl3state(pc, 8);
1670         if (pc->debug & L3_DEB_WARN)
1671                 l3_debug(pc->st, "D-chan connect for waiting call");
1672         l3ni1_message_plus_chid(pc, MT_CONNECT); /* GE 05/09/00 */ 
1673         L3DelTimer(&pc->timer);
1674         L3AddTimer(&pc->timer, T313, CC_T313);
1675 }
1676
1677 static void
1678 l3ni1_connect_ack(struct l3_process *pc, u_char pr, void *arg)
1679 {
1680         struct sk_buff *skb = arg;
1681         int ret;
1682
1683         ret = check_infoelements(pc, skb, ie_CONNECT_ACKNOWLEDGE);
1684         if (ERR_IE_COMPREHENSION == ret) {
1685                 l3ni1_std_ie_err(pc, ret);
1686                 return;
1687         }
1688         newl3state(pc, 10);
1689         L3DelTimer(&pc->timer);
1690         if (ret)
1691                 l3ni1_std_ie_err(pc, ret);
1692         pc->st->l3.l3l4(pc->st, CC_SETUP_COMPL | INDICATION, pc);
1693 }
1694
1695 static void
1696 l3ni1_reject_req(struct l3_process *pc, u_char pr, void *arg)
1697 {
1698         struct sk_buff *skb;
1699         u_char tmp[16];
1700         u_char *p = tmp;
1701         int l;
1702         u_char cause = 21;
1703
1704         if (pc->para.cause != NO_CAUSE)
1705                 cause = pc->para.cause;
1706
1707         MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
1708
1709         *p++ = IE_CAUSE;
1710         *p++ = 0x2;
1711         *p++ = 0x80;
1712         *p++ = cause | 0x80;
1713
1714         l = p - tmp;
1715         if (!(skb = l3_alloc_skb(l)))
1716                 return;
1717         memcpy(skb_put(skb, l), tmp, l);
1718         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1719         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
1720         newl3state(pc, 0);
1721         ni1_release_l3_process(pc);
1722 }
1723
1724 static void
1725 l3ni1_release(struct l3_process *pc, u_char pr, void *arg)
1726 {
1727         struct sk_buff *skb = arg;
1728         u_char *p;
1729         int ret, cause=0;
1730
1731         StopAllL3Timer(pc);
1732         if ((ret = l3ni1_get_cause(pc, skb))>0) {
1733                 if (pc->debug & L3_DEB_WARN)
1734                         l3_debug(pc->st, "REL get_cause ret(%d)", ret);
1735         } else if (ret<0)
1736                 pc->para.cause = NO_CAUSE;
1737         if ((p = findie(skb->data, skb->len, IE_FACILITY, 0))) {
1738                 l3ni1_parse_facility(pc->st, pc, pc->callref, p);
1739         }
1740         if ((ret<0) && (pc->state != 11))
1741                 cause = 96;
1742         else if (ret>0)
1743                 cause = 100;
1744         ret = check_infoelements(pc, skb, ie_RELEASE);
1745         if (ERR_IE_COMPREHENSION == ret)
1746                 cause = 96;
1747         else if ((ERR_IE_UNRECOGNIZED == ret) && (!cause))
1748                 cause = 99;  
1749         if (cause)
1750                 l3ni1_message_cause(pc, MT_RELEASE_COMPLETE, cause);
1751         else
1752                 l3ni1_message(pc, MT_RELEASE_COMPLETE);
1753         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
1754         newl3state(pc, 0);
1755         ni1_release_l3_process(pc);
1756 }
1757
1758 static void
1759 l3ni1_alert_req(struct l3_process *pc, u_char pr,
1760                  void *arg)
1761 {
1762         newl3state(pc, 7);
1763         if (!pc->prot.ni1.uus1_data[0]) 
1764                 l3ni1_message(pc, MT_ALERTING);
1765         else
1766                 l3ni1_msg_with_uus(pc, MT_ALERTING); 
1767 }
1768
1769 static void
1770 l3ni1_proceed_req(struct l3_process *pc, u_char pr,
1771                    void *arg)
1772 {
1773         newl3state(pc, 9);
1774         l3ni1_message(pc, MT_CALL_PROCEEDING);
1775         pc->st->l3.l3l4(pc->st, CC_PROCEED_SEND | INDICATION, pc); 
1776 }
1777
1778 static void
1779 l3ni1_setup_ack_req(struct l3_process *pc, u_char pr,
1780                    void *arg)
1781 {
1782         newl3state(pc, 25);
1783         L3DelTimer(&pc->timer);
1784         L3AddTimer(&pc->timer, T302, CC_T302);
1785         l3ni1_message(pc, MT_SETUP_ACKNOWLEDGE);
1786 }
1787
1788 /********************************************/
1789 /* deliver a incoming display message to HL */
1790 /********************************************/
1791 static void
1792 l3ni1_deliver_display(struct l3_process *pc, int pr, u_char *infp)
1793 {       u_char len;
1794         isdn_ctrl ic; 
1795         struct IsdnCardState *cs;
1796         char *p; 
1797
1798         if (*infp++ != IE_DISPLAY) return;
1799         if ((len = *infp++) > 80) return; /* total length <= 82 */
1800         if (!pc->chan) return;
1801
1802         p = ic.parm.display; 
1803         while (len--)
1804           *p++ = *infp++;
1805         *p = '\0';
1806         ic.command = ISDN_STAT_DISPLAY;
1807         cs = pc->st->l1.hardware;
1808         ic.driver = cs->myid;
1809         ic.arg = pc->chan->chan; 
1810         cs->iif.statcallb(&ic);
1811 } /* l3ni1_deliver_display */
1812
1813
1814 static void
1815 l3ni1_progress(struct l3_process *pc, u_char pr, void *arg)
1816 {
1817         struct sk_buff *skb = arg;
1818         int err = 0;
1819         u_char *p;
1820
1821         if ((p = findie(skb->data, skb->len, IE_PROGRESS, 0))) {
1822                 if (p[1] != 2) {
1823                         err = 1;
1824                         pc->para.cause = 100;
1825                 } else if (!(p[2] & 0x70)) {
1826                         switch (p[2]) {
1827                                 case 0x80:
1828                                 case 0x81:
1829                                 case 0x82:
1830                                 case 0x84:
1831                                 case 0x85:
1832                                 case 0x87:
1833                                 case 0x8a:
1834                                         switch (p[3]) {
1835                                                 case 0x81:
1836                                                 case 0x82:
1837                                                 case 0x83:
1838                                                 case 0x84:
1839                                                 case 0x88:
1840                                                         break;
1841                                                 default:
1842                                                         err = 2;
1843                                                         pc->para.cause = 100;
1844                                                         break;
1845                                         }
1846                                         break;
1847                                 default:
1848                                         err = 3;
1849                                         pc->para.cause = 100;
1850                                         break;
1851                         }
1852                 }
1853         } else {
1854                 pc->para.cause = 96;
1855                 err = 4;
1856         }
1857         if (err) {      
1858                 if (pc->debug & L3_DEB_WARN)
1859                         l3_debug(pc->st, "progress error %d", err);
1860                 l3ni1_status_send(pc, pr, NULL);
1861                 return;
1862         }
1863         /* Now we are on none mandatory IEs */
1864         err = check_infoelements(pc, skb, ie_PROGRESS);
1865         if (err)
1866                 l3ni1_std_ie_err(pc, err);
1867         if (ERR_IE_COMPREHENSION != err)
1868                 pc->st->l3.l3l4(pc->st, CC_PROGRESS | INDICATION, pc);
1869 }
1870
1871 static void
1872 l3ni1_notify(struct l3_process *pc, u_char pr, void *arg)
1873 {
1874         struct sk_buff *skb = arg;
1875         int err = 0;
1876         u_char *p;
1877
1878         if ((p = findie(skb->data, skb->len, IE_NOTIFY, 0))) {
1879                 if (p[1] != 1) {
1880                         err = 1;
1881                         pc->para.cause = 100;
1882                 } else {
1883                         switch (p[2]) {
1884                                 case 0x80:
1885                                 case 0x81:
1886                                 case 0x82:
1887                                         break;
1888                                 default:
1889                                         pc->para.cause = 100;
1890                                         err = 2;
1891                                         break;
1892                         }
1893                 }
1894         } else {
1895                 pc->para.cause = 96;
1896                 err = 3;
1897         }
1898         if (err) {      
1899                 if (pc->debug & L3_DEB_WARN)
1900                         l3_debug(pc->st, "notify error %d", err);
1901                 l3ni1_status_send(pc, pr, NULL);
1902                 return;
1903         }
1904         /* Now we are on none mandatory IEs */
1905         err = check_infoelements(pc, skb, ie_NOTIFY);
1906         if (err)
1907                 l3ni1_std_ie_err(pc, err);
1908         if (ERR_IE_COMPREHENSION != err)
1909                 pc->st->l3.l3l4(pc->st, CC_NOTIFY | INDICATION, pc);
1910 }
1911
1912 static void
1913 l3ni1_status_enq(struct l3_process *pc, u_char pr, void *arg)
1914 {
1915         int ret;
1916         struct sk_buff *skb = arg;
1917
1918         ret = check_infoelements(pc, skb, ie_STATUS_ENQUIRY);
1919         l3ni1_std_ie_err(pc, ret);
1920         pc->para.cause = 30; /* response to STATUS_ENQUIRY */
1921         l3ni1_status_send(pc, pr, NULL);
1922 }
1923
1924 static void
1925 l3ni1_information(struct l3_process *pc, u_char pr, void *arg)
1926 {
1927         int ret;
1928         struct sk_buff *skb = arg;
1929         u_char *p;
1930         char tmp[32];
1931
1932         ret = check_infoelements(pc, skb, ie_INFORMATION);
1933         if (ret)
1934                 l3ni1_std_ie_err(pc, ret);
1935         if (pc->state == 25) { /* overlap receiving */
1936                 L3DelTimer(&pc->timer);
1937                 p = skb->data;
1938                 if ((p = findie(p, skb->len, 0x70, 0))) {
1939                         iecpy(tmp, p, 1);
1940                         strcat(pc->para.setup.eazmsn, tmp);
1941                         pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
1942                 }
1943                 L3AddTimer(&pc->timer, T302, CC_T302);
1944         }
1945 }
1946
1947 /******************************/
1948 /* handle deflection requests */
1949 /******************************/
1950 static void l3ni1_redir_req(struct l3_process *pc, u_char pr, void *arg)
1951 {
1952         struct sk_buff *skb;
1953         u_char tmp[128];
1954         u_char *p = tmp;
1955         u_char *subp;
1956         u_char len_phone = 0;
1957         u_char len_sub = 0;
1958         int l; 
1959
1960
1961         strcpy(pc->prot.ni1.uus1_data,pc->chan->setup.eazmsn); /* copy uus element if available */
1962         if (!pc->chan->setup.phone[0])
1963           { pc->para.cause = -1;
1964             l3ni1_disconnect_req(pc,pr,arg); /* disconnect immediately */
1965             return;
1966           } /* only uus */
1967  
1968         if (pc->prot.ni1.invoke_id) 
1969           free_invoke_id(pc->st,pc->prot.ni1.invoke_id);
1970  
1971         if (!(pc->prot.ni1.invoke_id = new_invoke_id(pc->st))) 
1972           return;
1973
1974         MsgHead(p, pc->callref, MT_FACILITY);
1975
1976         for (subp = pc->chan->setup.phone; (*subp) && (*subp != '.'); subp++) len_phone++; /* len of phone number */
1977         if (*subp++ == '.') len_sub = strlen(subp) + 2; /* length including info subaddress element */ 
1978
1979         *p++ = 0x1c;   /* Facility info element */
1980         *p++ = len_phone + len_sub + 2 + 2 + 8 + 3 + 3; /* length of element */
1981         *p++ = 0x91;  /* remote operations protocol */
1982         *p++ = 0xa1;  /* invoke component */
1983           
1984         *p++ = len_phone + len_sub + 2 + 2 + 8 + 3; /* length of data */
1985         *p++ = 0x02;  /* invoke id tag, integer */
1986         *p++ = 0x01;  /* length */
1987         *p++ = pc->prot.ni1.invoke_id;  /* invoke id */ 
1988         *p++ = 0x02;  /* operation value tag, integer */
1989         *p++ = 0x01;  /* length */
1990         *p++ = 0x0D;  /* Call Deflect */
1991           
1992         *p++ = 0x30;  /* sequence phone number */
1993         *p++ = len_phone + 2 + 2 + 3 + len_sub; /* length */
1994           
1995         *p++ = 0x30;  /* Deflected to UserNumber */
1996         *p++ = len_phone+2+len_sub; /* length */
1997         *p++ = 0x80; /* NumberDigits */
1998         *p++ = len_phone; /* length */
1999         for (l = 0; l < len_phone; l++)
2000          *p++ = pc->chan->setup.phone[l];
2001
2002         if (len_sub)
2003           { *p++ = 0x04; /* called party subaddress */
2004             *p++ = len_sub - 2;
2005             while (*subp) *p++ = *subp++;
2006           }
2007
2008         *p++ = 0x01; /* screening identifier */
2009         *p++ = 0x01;
2010         *p++ = pc->chan->setup.screen;
2011
2012         l = p - tmp;
2013         if (!(skb = l3_alloc_skb(l))) return;
2014         memcpy(skb_put(skb, l), tmp, l);
2015
2016         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2017 } /* l3ni1_redir_req */
2018
2019 /********************************************/
2020 /* handle deflection request in early state */
2021 /********************************************/
2022 static void l3ni1_redir_req_early(struct l3_process *pc, u_char pr, void *arg)
2023 {
2024   l3ni1_proceed_req(pc,pr,arg);
2025   l3ni1_redir_req(pc,pr,arg);
2026 } /* l3ni1_redir_req_early */
2027
2028 /***********************************************/
2029 /* handle special commands for this protocol.  */
2030 /* Examples are call independant services like */
2031 /* remote operations with dummy  callref.      */
2032 /***********************************************/
2033 static int l3ni1_cmd_global(struct PStack *st, isdn_ctrl *ic)
2034 { u_char id;
2035   u_char temp[265];
2036   u_char *p = temp;
2037   int i, l, proc_len; 
2038   struct sk_buff *skb;
2039   struct l3_process *pc = NULL;
2040
2041   switch (ic->arg)
2042    { case NI1_CMD_INVOKE:
2043        if (ic->parm.ni1_io.datalen < 0) return(-2); /* invalid parameter */ 
2044
2045        for (proc_len = 1, i = ic->parm.ni1_io.proc >> 8; i; i++) 
2046          i = i >> 8; /* add one byte */    
2047        l = ic->parm.ni1_io.datalen + proc_len + 8; /* length excluding ie header */
2048        if (l > 255) 
2049          return(-2); /* too long */
2050
2051        if (!(id = new_invoke_id(st))) 
2052          return(0); /* first get a invoke id -> return if no available */
2053        
2054        i = -1; 
2055        MsgHead(p, i, MT_FACILITY); /* build message head */
2056        *p++ = 0x1C; /* Facility IE */
2057        *p++ = l; /* length of ie */
2058        *p++ = 0x91; /* remote operations */
2059        *p++ = 0xA1; /* invoke */
2060        *p++ = l - 3; /* length of invoke */
2061        *p++ = 0x02; /* invoke id tag */
2062        *p++ = 0x01; /* length is 1 */
2063        *p++ = id; /* invoke id */
2064        *p++ = 0x02; /* operation */
2065        *p++ = proc_len; /* length of operation */
2066        
2067        for (i = proc_len; i; i--)
2068          *p++ = (ic->parm.ni1_io.proc >> (i-1)) & 0xFF;
2069        memcpy(p, ic->parm.ni1_io.data, ic->parm.ni1_io.datalen); /* copy data */
2070        l = (p - temp) + ic->parm.ni1_io.datalen; /* total length */         
2071
2072        if (ic->parm.ni1_io.timeout > 0)
2073         if (!(pc = ni1_new_l3_process(st, -1)))
2074           { free_invoke_id(st, id);
2075             return(-2);
2076           } 
2077        pc->prot.ni1.ll_id = ic->parm.ni1_io.ll_id; /* remember id */ 
2078        pc->prot.ni1.proc = ic->parm.ni1_io.proc; /* and procedure */
2079
2080        if (!(skb = l3_alloc_skb(l))) 
2081          { free_invoke_id(st, id);
2082            if (pc) ni1_release_l3_process(pc);
2083            return(-2);
2084          }
2085        memcpy(skb_put(skb, l), temp, l);
2086        
2087        if (pc)
2088         { pc->prot.ni1.invoke_id = id; /* remember id */
2089           L3AddTimer(&pc->timer, ic->parm.ni1_io.timeout, CC_TNI1_IO | REQUEST);
2090         }
2091        
2092        l3_msg(st, DL_DATA | REQUEST, skb);
2093        ic->parm.ni1_io.hl_id = id; /* return id */
2094        return(0);
2095
2096      case NI1_CMD_INVOKE_ABORT:
2097        if ((pc = l3ni1_search_dummy_proc(st, ic->parm.ni1_io.hl_id)))
2098         { L3DelTimer(&pc->timer); /* remove timer */
2099           ni1_release_l3_process(pc);
2100           return(0); 
2101         } 
2102        else
2103         { l3_debug(st, "l3ni1_cmd_global abort unknown id");
2104           return(-2);
2105         } 
2106        break;
2107     
2108      default: 
2109        l3_debug(st, "l3ni1_cmd_global unknown cmd 0x%lx", ic->arg);
2110        return(-1);  
2111    } /* switch ic-> arg */
2112   return(-1);
2113 } /* l3ni1_cmd_global */
2114
2115 static void 
2116 l3ni1_io_timer(struct l3_process *pc)
2117 { isdn_ctrl ic;
2118   struct IsdnCardState *cs = pc->st->l1.hardware;
2119
2120   L3DelTimer(&pc->timer); /* remove timer */
2121
2122   ic.driver = cs->myid;
2123   ic.command = ISDN_STAT_PROT;
2124   ic.arg = NI1_STAT_INVOKE_ERR;
2125   ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
2126   ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
2127   ic.parm.ni1_io.proc = pc->prot.ni1.proc;
2128   ic.parm.ni1_io.timeout= -1;
2129   ic.parm.ni1_io.datalen = 0;
2130   ic.parm.ni1_io.data = NULL;
2131   free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
2132   pc->prot.ni1.invoke_id = 0; /* reset id */
2133
2134   cs->iif.statcallb(&ic);
2135
2136   ni1_release_l3_process(pc); 
2137 } /* l3ni1_io_timer */
2138
2139 static void
2140 l3ni1_release_ind(struct l3_process *pc, u_char pr, void *arg)
2141 {
2142         u_char *p;
2143         struct sk_buff *skb = arg;
2144         int callState = 0;
2145         p = skb->data;
2146
2147         if ((p = findie(p, skb->len, IE_CALL_STATE, 0))) {
2148                 p++;
2149                 if (1 == *p++)
2150                         callState = *p;
2151         }
2152         if (callState == 0) {
2153                 /* ETS 300-104 7.6.1, 8.6.1, 10.6.1... and 16.1
2154                  * set down layer 3 without sending any message
2155                  */
2156                 pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2157                 newl3state(pc, 0);
2158                 ni1_release_l3_process(pc);
2159         } else {
2160                 pc->st->l3.l3l4(pc->st, CC_IGNORE | INDICATION, pc);
2161         }
2162 }
2163
2164 static void
2165 l3ni1_dummy(struct l3_process *pc, u_char pr, void *arg)
2166 {
2167 }
2168
2169 static void
2170 l3ni1_t302(struct l3_process *pc, u_char pr, void *arg)
2171 {
2172         L3DelTimer(&pc->timer);
2173         pc->para.loc = 0;
2174         pc->para.cause = 28; /* invalid number */
2175         l3ni1_disconnect_req(pc, pr, NULL);
2176         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2177 }
2178
2179 static void
2180 l3ni1_t303(struct l3_process *pc, u_char pr, void *arg)
2181 {
2182         if (pc->N303 > 0) {
2183                 pc->N303--;
2184                 L3DelTimer(&pc->timer);
2185                 l3ni1_setup_req(pc, pr, arg);
2186         } else {
2187                 L3DelTimer(&pc->timer);
2188                 l3ni1_message_cause(pc, MT_RELEASE_COMPLETE, 102);
2189                 pc->st->l3.l3l4(pc->st, CC_NOSETUP_RSP, pc);
2190                 ni1_release_l3_process(pc);
2191         }
2192 }
2193
2194 static void
2195 l3ni1_t304(struct l3_process *pc, u_char pr, void *arg)
2196 {
2197         L3DelTimer(&pc->timer);
2198         pc->para.loc = 0;
2199         pc->para.cause = 102;
2200         l3ni1_disconnect_req(pc, pr, NULL);
2201         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2202
2203 }
2204
2205 static void
2206 l3ni1_t305(struct l3_process *pc, u_char pr, void *arg)
2207 {
2208         u_char tmp[16];
2209         u_char *p = tmp;
2210         int l;
2211         struct sk_buff *skb;
2212         u_char cause = 16;
2213
2214         L3DelTimer(&pc->timer);
2215         if (pc->para.cause != NO_CAUSE)
2216                 cause = pc->para.cause;
2217
2218         MsgHead(p, pc->callref, MT_RELEASE);
2219
2220         *p++ = IE_CAUSE;
2221         *p++ = 0x2;
2222         *p++ = 0x80;
2223         *p++ = cause | 0x80;
2224
2225         l = p - tmp;
2226         if (!(skb = l3_alloc_skb(l)))
2227                 return;
2228         memcpy(skb_put(skb, l), tmp, l);
2229         newl3state(pc, 19);
2230         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2231         L3AddTimer(&pc->timer, T308, CC_T308_1);
2232 }
2233
2234 static void
2235 l3ni1_t310(struct l3_process *pc, u_char pr, void *arg)
2236 {
2237         L3DelTimer(&pc->timer);
2238         pc->para.loc = 0;
2239         pc->para.cause = 102;
2240         l3ni1_disconnect_req(pc, pr, NULL);
2241         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2242 }
2243
2244 static void
2245 l3ni1_t313(struct l3_process *pc, u_char pr, void *arg)
2246 {
2247         L3DelTimer(&pc->timer);
2248         pc->para.loc = 0;
2249         pc->para.cause = 102;
2250         l3ni1_disconnect_req(pc, pr, NULL);
2251         pc->st->l3.l3l4(pc->st, CC_CONNECT_ERR, pc);
2252 }
2253
2254 static void
2255 l3ni1_t308_1(struct l3_process *pc, u_char pr, void *arg)
2256 {
2257         newl3state(pc, 19);
2258         L3DelTimer(&pc->timer);
2259         l3ni1_message(pc, MT_RELEASE);
2260         L3AddTimer(&pc->timer, T308, CC_T308_2);
2261 }
2262
2263 static void
2264 l3ni1_t308_2(struct l3_process *pc, u_char pr, void *arg)
2265 {
2266         L3DelTimer(&pc->timer);
2267         pc->st->l3.l3l4(pc->st, CC_RELEASE_ERR, pc);
2268         ni1_release_l3_process(pc);
2269 }
2270
2271 static void
2272 l3ni1_t318(struct l3_process *pc, u_char pr, void *arg)
2273 {
2274         L3DelTimer(&pc->timer);
2275         pc->para.cause = 102;   /* Timer expiry */
2276         pc->para.loc = 0;       /* local */
2277         pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
2278         newl3state(pc, 19);
2279         l3ni1_message(pc, MT_RELEASE);
2280         L3AddTimer(&pc->timer, T308, CC_T308_1);
2281 }
2282
2283 static void
2284 l3ni1_t319(struct l3_process *pc, u_char pr, void *arg)
2285 {
2286         L3DelTimer(&pc->timer);
2287         pc->para.cause = 102;   /* Timer expiry */
2288         pc->para.loc = 0;       /* local */
2289         pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
2290         newl3state(pc, 10);
2291 }
2292
2293 static void
2294 l3ni1_restart(struct l3_process *pc, u_char pr, void *arg)
2295 {
2296         L3DelTimer(&pc->timer);
2297         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2298         ni1_release_l3_process(pc);
2299 }
2300
2301 static void
2302 l3ni1_status(struct l3_process *pc, u_char pr, void *arg)
2303 {
2304         u_char *p;
2305         struct sk_buff *skb = arg;
2306         int ret; 
2307         u_char cause = 0, callState = 0;
2308         
2309         if ((ret = l3ni1_get_cause(pc, skb))) {
2310                 if (pc->debug & L3_DEB_WARN)
2311                         l3_debug(pc->st, "STATUS get_cause ret(%d)",ret);
2312                 if (ret < 0)
2313                         cause = 96;
2314                 else if (ret > 0)
2315                         cause = 100;
2316         }
2317         if ((p = findie(skb->data, skb->len, IE_CALL_STATE, 0))) {
2318                 p++;
2319                 if (1 == *p++) {
2320                         callState = *p;
2321                         if (!ie_in_set(pc, *p, l3_valid_states))
2322                                 cause = 100;
2323                 } else
2324                         cause = 100;
2325         } else
2326                 cause = 96;
2327         if (!cause) { /*  no error before */
2328                 ret = check_infoelements(pc, skb, ie_STATUS);
2329                 if (ERR_IE_COMPREHENSION == ret)
2330                         cause = 96;
2331                 else if (ERR_IE_UNRECOGNIZED == ret)
2332                         cause = 99;
2333         }
2334         if (cause) {
2335                 u_char tmp;
2336                 
2337                 if (pc->debug & L3_DEB_WARN)
2338                         l3_debug(pc->st, "STATUS error(%d/%d)",ret,cause);
2339                 tmp = pc->para.cause;
2340                 pc->para.cause = cause;
2341                 l3ni1_status_send(pc, 0, NULL);
2342                 if (cause == 99)
2343                         pc->para.cause = tmp;
2344                 else
2345                         return;
2346         }
2347         cause = pc->para.cause;
2348         if (((cause & 0x7f) == 111) && (callState == 0)) {
2349                 /* ETS 300-104 7.6.1, 8.6.1, 10.6.1...
2350                  * if received MT_STATUS with cause == 111 and call
2351                  * state == 0, then we must set down layer 3
2352                  */
2353                 pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2354                 newl3state(pc, 0);
2355                 ni1_release_l3_process(pc);
2356         }
2357 }
2358
2359 static void
2360 l3ni1_facility(struct l3_process *pc, u_char pr, void *arg)
2361 {
2362         struct sk_buff *skb = arg;
2363         int ret;
2364         
2365         ret = check_infoelements(pc, skb, ie_FACILITY);
2366         l3ni1_std_ie_err(pc, ret);
2367           {
2368                 u_char *p;
2369                 if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
2370                         l3ni1_parse_facility(pc->st, pc, pc->callref, p);
2371         }
2372 }
2373
2374 static void
2375 l3ni1_suspend_req(struct l3_process *pc, u_char pr, void *arg)
2376 {
2377         struct sk_buff *skb;
2378         u_char tmp[32];
2379         u_char *p = tmp;
2380         u_char i, l;
2381         u_char *msg = pc->chan->setup.phone;
2382
2383         MsgHead(p, pc->callref, MT_SUSPEND);
2384         l = *msg++;
2385         if (l && (l <= 10)) {   /* Max length 10 octets */
2386                 *p++ = IE_CALL_ID;
2387                 *p++ = l;
2388                 for (i = 0; i < l; i++)
2389                         *p++ = *msg++;
2390         } else if (l) {
2391                 l3_debug(pc->st, "SUS wrong CALL_ID len %d", l);
2392                 return;
2393         }
2394         l = p - tmp;
2395         if (!(skb = l3_alloc_skb(l)))
2396                 return;
2397         memcpy(skb_put(skb, l), tmp, l);
2398         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2399         newl3state(pc, 15);
2400         L3AddTimer(&pc->timer, T319, CC_T319);
2401 }
2402
2403 static void
2404 l3ni1_suspend_ack(struct l3_process *pc, u_char pr, void *arg)
2405 {
2406         struct sk_buff *skb = arg;
2407         int ret;
2408
2409         L3DelTimer(&pc->timer);
2410         newl3state(pc, 0);
2411         pc->para.cause = NO_CAUSE;
2412         pc->st->l3.l3l4(pc->st, CC_SUSPEND | CONFIRM, pc);
2413         /* We don't handle suspend_ack for IE errors now */
2414         if ((ret = check_infoelements(pc, skb, ie_SUSPEND_ACKNOWLEDGE)))
2415                 if (pc->debug & L3_DEB_WARN)
2416                         l3_debug(pc->st, "SUSPACK check ie(%d)",ret);
2417         ni1_release_l3_process(pc);
2418 }
2419
2420 static void
2421 l3ni1_suspend_rej(struct l3_process *pc, u_char pr, void *arg)
2422 {
2423         struct sk_buff *skb = arg;
2424         int ret;
2425
2426         if ((ret = l3ni1_get_cause(pc, skb))) {
2427                 if (pc->debug & L3_DEB_WARN)
2428                         l3_debug(pc->st, "SUSP_REJ get_cause ret(%d)",ret);
2429                 if (ret < 0) 
2430                         pc->para.cause = 96;
2431                 else
2432                         pc->para.cause = 100;
2433                 l3ni1_status_send(pc, pr, NULL);
2434                 return;
2435         }
2436         ret = check_infoelements(pc, skb, ie_SUSPEND_REJECT);
2437         if (ERR_IE_COMPREHENSION == ret) {
2438                 l3ni1_std_ie_err(pc, ret);
2439                 return;
2440         }
2441         L3DelTimer(&pc->timer);
2442         pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
2443         newl3state(pc, 10);
2444         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2445                 l3ni1_std_ie_err(pc, ret);
2446 }
2447
2448 static void
2449 l3ni1_resume_req(struct l3_process *pc, u_char pr, void *arg)
2450 {
2451         struct sk_buff *skb;
2452         u_char tmp[32];
2453         u_char *p = tmp;
2454         u_char i, l;
2455         u_char *msg = pc->para.setup.phone;
2456
2457         MsgHead(p, pc->callref, MT_RESUME);
2458
2459         l = *msg++;
2460         if (l && (l <= 10)) {   /* Max length 10 octets */
2461                 *p++ = IE_CALL_ID;
2462                 *p++ = l;
2463                 for (i = 0; i < l; i++)
2464                         *p++ = *msg++;
2465         } else if (l) {
2466                 l3_debug(pc->st, "RES wrong CALL_ID len %d", l);
2467                 return;
2468         }
2469         l = p - tmp;
2470         if (!(skb = l3_alloc_skb(l)))
2471                 return;
2472         memcpy(skb_put(skb, l), tmp, l);
2473         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2474         newl3state(pc, 17);
2475         L3AddTimer(&pc->timer, T318, CC_T318);
2476 }
2477
2478 static void
2479 l3ni1_resume_ack(struct l3_process *pc, u_char pr, void *arg)
2480 {
2481         struct sk_buff *skb = arg;
2482         int id, ret;
2483
2484         if ((id = l3ni1_get_channel_id(pc, skb)) > 0) {
2485                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
2486                         if (pc->debug & L3_DEB_WARN)
2487                                 l3_debug(pc->st, "resume ack with wrong chid %x", id);
2488                         pc->para.cause = 100;
2489                         l3ni1_status_send(pc, pr, NULL);
2490                         return;
2491                 }
2492                 pc->para.bchannel = id;
2493         } else if (1 == pc->state) {
2494                 if (pc->debug & L3_DEB_WARN)
2495                         l3_debug(pc->st, "resume ack without chid (ret %d)", id);
2496                 pc->para.cause = 96;
2497                 l3ni1_status_send(pc, pr, NULL);
2498                 return;
2499         }
2500         ret = check_infoelements(pc, skb, ie_RESUME_ACKNOWLEDGE);
2501         if (ERR_IE_COMPREHENSION == ret) {
2502                 l3ni1_std_ie_err(pc, ret);
2503                 return;
2504         }
2505         L3DelTimer(&pc->timer);
2506         pc->st->l3.l3l4(pc->st, CC_RESUME | CONFIRM, pc);
2507         newl3state(pc, 10);
2508         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2509                 l3ni1_std_ie_err(pc, ret);
2510 }
2511
2512 static void
2513 l3ni1_resume_rej(struct l3_process *pc, u_char pr, void *arg)
2514 {
2515         struct sk_buff *skb = arg;
2516         int ret;
2517
2518         if ((ret = l3ni1_get_cause(pc, skb))) {
2519                 if (pc->debug & L3_DEB_WARN)
2520                         l3_debug(pc->st, "RES_REJ get_cause ret(%d)",ret);
2521                 if (ret < 0) 
2522                         pc->para.cause = 96;
2523                 else
2524                         pc->para.cause = 100;
2525                 l3ni1_status_send(pc, pr, NULL);
2526                 return;
2527         }
2528         ret = check_infoelements(pc, skb, ie_RESUME_REJECT);
2529         if (ERR_IE_COMPREHENSION == ret) {
2530                 l3ni1_std_ie_err(pc, ret);
2531                 return;
2532         }
2533         L3DelTimer(&pc->timer);
2534         pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
2535         newl3state(pc, 0);
2536         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2537                 l3ni1_std_ie_err(pc, ret);
2538         ni1_release_l3_process(pc);
2539 }
2540
2541 static void
2542 l3ni1_global_restart(struct l3_process *pc, u_char pr, void *arg)
2543 {
2544         u_char tmp[32];
2545         u_char *p;
2546         u_char ri, ch = 0, chan = 0;
2547         int l;
2548         struct sk_buff *skb = arg;
2549         struct l3_process *up;
2550
2551         newl3state(pc, 2);
2552         L3DelTimer(&pc->timer);
2553         p = skb->data;
2554         if ((p = findie(p, skb->len, IE_RESTART_IND, 0))) {
2555                 ri = p[2];
2556                 l3_debug(pc->st, "Restart %x", ri);
2557         } else {
2558                 l3_debug(pc->st, "Restart without restart IE");
2559                 ri = 0x86;
2560         }
2561         p = skb->data;
2562         if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
2563                 chan = p[2] & 3;
2564                 ch = p[2];
2565                 if (pc->st->l3.debug)
2566                         l3_debug(pc->st, "Restart for channel %d", chan);
2567         }
2568         newl3state(pc, 2);
2569         up = pc->st->l3.proc;
2570         while (up) {
2571                 if ((ri & 7) == 7)
2572                         up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
2573                 else if (up->para.bchannel == chan)
2574                         up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
2575                 
2576                 up = up->next;
2577         }
2578         p = tmp;
2579         MsgHead(p, pc->callref, MT_RESTART_ACKNOWLEDGE);
2580         if (chan) {
2581                 *p++ = IE_CHANNEL_ID;
2582                 *p++ = 1;
2583                 *p++ = ch | 0x80;
2584         }
2585         *p++ = 0x79;            /* RESTART Ind */
2586         *p++ = 1;
2587         *p++ = ri;
2588         l = p - tmp;
2589         if (!(skb = l3_alloc_skb(l)))
2590                 return;
2591         memcpy(skb_put(skb, l), tmp, l);
2592         newl3state(pc, 0);
2593         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2594 }
2595
2596 static void
2597 l3ni1_dl_reset(struct l3_process *pc, u_char pr, void *arg)
2598 {
2599         pc->para.cause = 0x29;          /* Temporary failure */
2600         pc->para.loc = 0;
2601         l3ni1_disconnect_req(pc, pr, NULL);
2602         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2603 }
2604
2605 static void
2606 l3ni1_dl_release(struct l3_process *pc, u_char pr, void *arg)
2607 {
2608         newl3state(pc, 0);
2609         pc->para.cause = 0x1b;          /* Destination out of order */
2610         pc->para.loc = 0;
2611         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2612         release_l3_process(pc);
2613 }
2614
2615 static void
2616 l3ni1_dl_reestablish(struct l3_process *pc, u_char pr, void *arg)
2617 {
2618         L3DelTimer(&pc->timer);
2619         L3AddTimer(&pc->timer, T309, CC_T309);
2620         l3_msg(pc->st, DL_ESTABLISH | REQUEST, NULL);
2621 }
2622  
2623 static void
2624 l3ni1_dl_reest_status(struct l3_process *pc, u_char pr, void *arg)
2625 {
2626         L3DelTimer(&pc->timer);
2627  
2628         pc->para.cause = 0x1F; /* normal, unspecified */
2629         l3ni1_status_send(pc, 0, NULL);
2630 }
2631
2632 static void l3ni1_SendSpid( struct l3_process *pc, u_char pr, struct sk_buff *skb, int iNewState )
2633 {
2634         u_char         * p;
2635         char           * pSPID;
2636         struct Channel * pChan = pc->st->lli.userdata;
2637         int              l;
2638
2639         if ( skb )
2640                 dev_kfree_skb( skb);
2641
2642         if ( !( pSPID = strchr( pChan->setup.eazmsn, ':' ) ) )
2643         {
2644                 printk( KERN_ERR "SPID not supplied in EAZMSN %s\n", pChan->setup.eazmsn );
2645                 newl3state( pc, 0 );
2646                 pc->st->l3.l3l2( pc->st, DL_RELEASE | REQUEST, NULL );
2647                 return;
2648         }
2649
2650         l = strlen( ++pSPID );
2651         if ( !( skb = l3_alloc_skb( 5+l ) ) )
2652         {
2653                 printk( KERN_ERR "HiSax can't get memory to send SPID\n" );
2654                 return;
2655         }
2656
2657         p = skb_put( skb, 5 );
2658         *p++ = PROTO_DIS_EURO;
2659         *p++ = 0;
2660         *p++ = MT_INFORMATION;
2661         *p++ = IE_SPID;
2662         *p++ = l;
2663
2664         memcpy( skb_put( skb, l ), pSPID, l );
2665
2666         newl3state( pc, iNewState );
2667
2668         L3DelTimer( &pc->timer );
2669         L3AddTimer( &pc->timer, TSPID, CC_TSPID );
2670
2671         pc->st->l3.l3l2( pc->st, DL_DATA | REQUEST, skb );
2672 }
2673
2674 static void l3ni1_spid_send( struct l3_process *pc, u_char pr, void *arg )
2675 {
2676         l3ni1_SendSpid( pc, pr, arg, 20 );
2677 }
2678
2679 void l3ni1_spid_epid( struct l3_process *pc, u_char pr, void *arg )
2680 {
2681         struct sk_buff *skb = arg;
2682
2683         if ( skb->data[ 1 ] == 0 )
2684                 if ( skb->data[ 3 ] == IE_ENDPOINT_ID )
2685                 {
2686                         L3DelTimer( &pc->timer );
2687                         newl3state( pc, 0 );
2688                         l3_msg( pc->st, DL_ESTABLISH | CONFIRM, NULL );
2689                 }
2690         dev_kfree_skb( skb);
2691 }
2692
2693 static void l3ni1_spid_tout( struct l3_process *pc, u_char pr, void *arg )
2694 {
2695         if ( pc->state < 22 )
2696                 l3ni1_SendSpid( pc, pr, arg, pc->state+1 );
2697         else