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