1 /*
2 * CAPI encoder/decoder for
3 * Portugal Telecom CAPI 2.0
4 *
5 * Copyright (C) 1996 Universidade de Lisboa
6 *
7 * Written by Pedro Roque Marques (pedro_m@yahoo.com)
8 *
9 * This software may be used and distributed according to the terms of
10 * the GNU General Public License, incorporated herein by reference.
11 *
12 * Not compatible with the AVM Gmbh. CAPI 2.0
13 *
14 */
15
16 /*
17 * Documentation:
18 * - "Common ISDN API - Perfil Portugu�s - Vers�o 2.1",
19 * Telecom Portugal, Fev 1992.
20 * - "Common ISDN API - Especifica��o de protocolos para
21 * acesso aos canais B", Inesc, Jan 1994.
22 */
23
24 /*
25 * TODO: better decoding of Information Elements
26 * for debug purposes mainly
27 * encode our number in CallerPN and ConnectedPN
28 */
29
30 #include <linux/sched.h>
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33
34 #include <linux/types.h>
35 #include <linux/slab.h>
36 #include <linux/mm.h>
37
38 #include <linux/tqueue.h>
39 #include <linux/skbuff.h>
40
41 #include <asm/io.h>
42 #include <asm/string.h>
43
44 #include <linux/isdnif.h>
45
46 #include "pcbit.h"
47 #include "edss1.h"
48 #include "capi.h"
49
50
51 /*
52 * Encoding of CAPI messages
53 *
54 */
55
capi_conn_req(const char * calledPN,struct sk_buff ** skb,int proto)56 int capi_conn_req(const char * calledPN, struct sk_buff **skb, int proto)
57 {
58 ushort len;
59
60 /*
61 * length
62 * AppInfoMask - 2
63 * BC0 - 3
64 * BC1 - 1
65 * Chan - 2
66 * Keypad - 1
67 * CPN - 1
68 * CPSA - 1
69 * CalledPN - 2 + strlen
70 * CalledPSA - 1
71 * rest... - 4
72 * ----------------
73 * Total 18 + strlen
74 */
75
76 len = 18 + strlen(calledPN);
77
78 if (proto == ISDN_PROTO_L2_TRANS)
79 len++;
80
81 if ((*skb = dev_alloc_skb(len)) == NULL) {
82
83 printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
84 return -1;
85 }
86
87 /* InfoElmMask */
88 *((ushort*) skb_put(*skb, 2)) = AppInfoMask;
89
90 if (proto == ISDN_PROTO_L2_TRANS)
91 {
92 /* Bearer Capability - Mandatory*/
93 *(skb_put(*skb, 1)) = 3; /* BC0.Length */
94 *(skb_put(*skb, 1)) = 0x80; /* Speech */
95 *(skb_put(*skb, 1)) = 0x10; /* Circuit Mode */
96 *(skb_put(*skb, 1)) = 0x23; /* A-law */
97 }
98 else
99 {
100 /* Bearer Capability - Mandatory*/
101 *(skb_put(*skb, 1)) = 2; /* BC0.Length */
102 *(skb_put(*skb, 1)) = 0x88; /* Digital Information */
103 *(skb_put(*skb, 1)) = 0x90; /* BC0.Octect4 */
104 }
105
106 /* Bearer Capability - Optional*/
107 *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
108
109 *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
110 *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
111
112 *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
113
114
115 *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
116 *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
117
118 /* Called Party Number */
119 *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
120 *(skb_put(*skb, 1)) = 0x81;
121 memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
122
123 /* '#' */
124
125 *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
126
127 /* LLC.Length = 0; */
128 /* HLC0.Length = 0; */
129 /* HLC1.Length = 0; */
130 /* UTUS.Length = 0; */
131 memset(skb_put(*skb, 4), 0, 4);
132
133 return len;
134 }
135
capi_conn_resp(struct pcbit_chan * chan,struct sk_buff ** skb)136 int capi_conn_resp(struct pcbit_chan* chan, struct sk_buff **skb)
137 {
138
139 if ((*skb = dev_alloc_skb(5)) == NULL) {
140
141 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
142 return -1;
143 }
144
145 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
146 *(skb_put(*skb, 1)) = 0x01; /* ACCEPT_CALL */
147 *(skb_put(*skb, 1)) = 0;
148 *(skb_put(*skb, 1)) = 0;
149
150 return 5;
151 }
152
capi_conn_active_req(struct pcbit_chan * chan,struct sk_buff ** skb)153 int capi_conn_active_req(struct pcbit_chan* chan, struct sk_buff **skb)
154 {
155 /*
156 * 8 bytes
157 */
158
159 if ((*skb = dev_alloc_skb(8)) == NULL) {
160
161 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
162 return -1;
163 }
164
165 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
166
167 #ifdef DEBUG
168 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
169 #endif
170
171 *(skb_put(*skb, 1)) = 0; /* BC.Length = 0; */
172 *(skb_put(*skb, 1)) = 0; /* ConnectedPN.Length = 0 */
173 *(skb_put(*skb, 1)) = 0; /* PSA.Length */
174 *(skb_put(*skb, 1)) = 0; /* LLC.Length = 0; */
175 *(skb_put(*skb, 1)) = 0; /* HLC.Length = 0; */
176 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
177
178 return 8;
179 }
180
capi_conn_active_resp(struct pcbit_chan * chan,struct sk_buff ** skb)181 int capi_conn_active_resp(struct pcbit_chan* chan, struct sk_buff **skb)
182 {
183 /*
184 * 2 bytes
185 */
186
187 if ((*skb = dev_alloc_skb(2)) == NULL) {
188
189 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
190 return -1;
191 }
192
193 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
194
195 return 2;
196 }
197
198
capi_select_proto_req(struct pcbit_chan * chan,struct sk_buff ** skb,int outgoing)199 int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
200 int outgoing)
201 {
202
203 /*
204 * 18 bytes
205 */
206
207 if ((*skb = dev_alloc_skb(18)) == NULL) {
208
209 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
210 return -1;
211 }
212
213 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
214
215 /* Layer2 protocol */
216
217 switch (chan->proto) {
218 case ISDN_PROTO_L2_X75I:
219 *(skb_put(*skb, 1)) = 0x05; /* LAPB */
220 break;
221 case ISDN_PROTO_L2_HDLC:
222 *(skb_put(*skb, 1)) = 0x02;
223 break;
224 case ISDN_PROTO_L2_TRANS:
225 /*
226 * Voice (a-law)
227 */
228 *(skb_put(*skb, 1)) = 0x06;
229 break;
230 default:
231 #ifdef DEBUG
232 printk(KERN_DEBUG "Transparent\n");
233 #endif
234 *(skb_put(*skb, 1)) = 0x03;
235 break;
236 }
237
238 *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
239 *(skb_put(*skb, 1)) = 0x00;
240
241 *((ushort *) skb_put(*skb, 2)) = MRU;
242
243
244 *(skb_put(*skb, 1)) = 0x08; /* Modulo */
245 *(skb_put(*skb, 1)) = 0x07; /* Max Window */
246
247 *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
248
249 /*
250 * 2 - layer3 MTU [10]
251 * - Modulo [12]
252 * - Window
253 * - layer1 proto [14]
254 * - bitrate
255 * - sub-channel [16]
256 * - layer1dataformat [17]
257 */
258
259 memset(skb_put(*skb, 8), 0, 8);
260
261 return 18;
262 }
263
264
capi_activate_transp_req(struct pcbit_chan * chan,struct sk_buff ** skb)265 int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
266 {
267
268 if ((*skb = dev_alloc_skb(7)) == NULL) {
269
270 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
271 return -1;
272 }
273
274 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
275
276
277 *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
278 *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
279
280 *((ushort *) skb_put(*skb, 2)) = MRU;
281
282 *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
283
284 return 7;
285 }
286
capi_tdata_req(struct pcbit_chan * chan,struct sk_buff * skb)287 int capi_tdata_req(struct pcbit_chan* chan, struct sk_buff *skb)
288 {
289 ushort data_len;
290
291
292 /*
293 * callref - 2
294 * layer2link - 1
295 * wBlockLength - 2
296 * data - 4
297 * sernum - 1
298 */
299
300 data_len = skb->len;
301
302 if(skb_headroom(skb) < 10)
303 {
304 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
305 }
306 else
307 {
308 skb_push(skb, 10);
309 }
310
311 *((u16 *) (skb->data)) = chan->callref;
312 skb->data[2] = chan->layer2link;
313 *((u16 *) (skb->data + 3)) = data_len;
314
315 chan->s_refnum = (chan->s_refnum + 1) % 8;
316 *((u32 *) (skb->data + 5)) = chan->s_refnum;
317
318 skb->data[9] = 0; /* HDLC frame number */
319
320 return 10;
321 }
322
capi_tdata_resp(struct pcbit_chan * chan,struct sk_buff ** skb)323 int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff ** skb)
324
325 {
326 if ((*skb = dev_alloc_skb(4)) == NULL) {
327
328 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
329 return -1;
330 }
331
332 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
333
334 *(skb_put(*skb, 1)) = chan->layer2link;
335 *(skb_put(*skb, 1)) = chan->r_refnum;
336
337 return (*skb)->len;
338 }
339
capi_disc_req(ushort callref,struct sk_buff ** skb,u_char cause)340 int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
341 {
342
343 if ((*skb = dev_alloc_skb(6)) == NULL) {
344
345 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
346 return -1;
347 }
348
349 *((ushort*) skb_put(*skb, 2) ) = callref;
350
351 *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
352 *(skb_put(*skb, 1)) = 0x80;
353 *(skb_put(*skb, 1)) = 0x80 | cause;
354
355 /*
356 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
357 */
358
359 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
360
361 return 6;
362 }
363
capi_disc_resp(struct pcbit_chan * chan,struct sk_buff ** skb)364 int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
365 {
366 if ((*skb = dev_alloc_skb(2)) == NULL) {
367
368 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
369 return -1;
370 }
371
372 *((ushort*) skb_put(*skb, 2)) = chan->callref;
373
374 return 2;
375 }
376
377
378 /*
379 * Decoding of CAPI messages
380 *
381 */
382
capi_decode_conn_ind(struct pcbit_chan * chan,struct sk_buff * skb,struct callb_data * info)383 int capi_decode_conn_ind(struct pcbit_chan * chan,
384 struct sk_buff *skb,
385 struct callb_data *info)
386 {
387 int CIlen, len;
388
389 /* Call Reference [CAPI] */
390 chan->callref = *((ushort*) skb->data);
391 skb_pull(skb, 2);
392
393 #ifdef DEBUG
394 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
395 #endif
396
397 /* Channel Identification */
398
399 /* Expect
400 Len = 1
401 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
402 */
403
404 CIlen = skb->data[0];
405 #ifdef DEBUG
406 if (CIlen == 1) {
407
408 if ( ((skb->data[1]) & 0xFC) == 0x48 )
409 printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
410 printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03);
411 }
412 else
413 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
414 #endif
415 skb_pull(skb, CIlen + 1);
416
417 /* Calling Party Number */
418 /* An "additional service" as far as Portugal Telecom is concerned */
419
420 len = skb->data[0];
421
422 if (len > 0) {
423 int count = 1;
424
425 #ifdef DEBUG
426 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
427 #endif
428 if ((skb->data[1] & 0x80) == 0)
429 count = 2;
430
431 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
432 return -1;
433
434 memcpy(info->data.setup.CallingPN, skb->data + count + 1,
435 len - count);
436 info->data.setup.CallingPN[len - count] = 0;
437
438 }
439 else {
440 info->data.setup.CallingPN = NULL;
441 printk(KERN_DEBUG "NULL CallingPN\n");
442 }
443
444 skb_pull(skb, len + 1);
445
446 /* Calling Party Subaddress */
447 skb_pull(skb, skb->data[0] + 1);
448
449 /* Called Party Number */
450
451 len = skb->data[0];
452
453 if (len > 0) {
454 int count = 1;
455
456 if ((skb->data[1] & 0x80) == 0)
457 count = 2;
458
459 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
460 return -1;
461
462 memcpy(info->data.setup.CalledPN, skb->data + count + 1,
463 len - count);
464 info->data.setup.CalledPN[len - count] = 0;
465
466 }
467 else {
468 info->data.setup.CalledPN = NULL;
469 printk(KERN_DEBUG "NULL CalledPN\n");
470 }
471
472 skb_pull(skb, len + 1);
473
474 /* Called Party Subaddress */
475 skb_pull(skb, skb->data[0] + 1);
476
477 /* LLC */
478 skb_pull(skb, skb->data[0] + 1);
479
480 /* HLC */
481 skb_pull(skb, skb->data[0] + 1);
482
483 /* U2U */
484 skb_pull(skb, skb->data[0] + 1);
485
486 return 0;
487 }
488
489 /*
490 * returns errcode
491 */
492
capi_decode_conn_conf(struct pcbit_chan * chan,struct sk_buff * skb,int * complete)493 int capi_decode_conn_conf(struct pcbit_chan * chan, struct sk_buff *skb,
494 int *complete)
495 {
496 int errcode;
497
498 chan->callref = *((ushort *) skb->data); /* Update CallReference */
499 skb_pull(skb, 2);
500
501 errcode = *((ushort *) skb->data); /* read errcode */
502 skb_pull(skb, 2);
503
504 *complete = *(skb->data);
505 skb_pull(skb, 1);
506
507 /* FIX ME */
508 /* This is actually a firmware bug */
509 if (!*complete)
510 {
511 printk(KERN_DEBUG "complete=%02x\n", *complete);
512 *complete = 1;
513 }
514
515
516 /* Optional Bearer Capability */
517 skb_pull(skb, *(skb->data) + 1);
518
519 /* Channel Identification */
520 skb_pull(skb, *(skb->data) + 1);
521
522 /* High Layer Compatibility follows */
523 skb_pull(skb, *(skb->data) + 1);
524
525 return errcode;
526 }
527
capi_decode_conn_actv_ind(struct pcbit_chan * chan,struct sk_buff * skb)528 int capi_decode_conn_actv_ind(struct pcbit_chan * chan, struct sk_buff *skb)
529 {
530 ushort len;
531 #ifdef DEBUG
532 char str[32];
533 #endif
534
535 /* Yet Another Bearer Capability */
536 skb_pull(skb, *(skb->data) + 1);
537
538
539 /* Connected Party Number */
540 len=*(skb->data);
541
542 #ifdef DEBUG
543 if (len > 1 && len < 31) {
544 memcpy(str, skb->data + 2, len - 1);
545 str[len] = 0;
546 printk(KERN_DEBUG "Connected Party Number: %s\n", str);
547 }
548 else
549 printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
550 #endif
551
552 skb_pull(skb, len + 1);
553
554 /* Connected Subaddress */
555 skb_pull(skb, *(skb->data) + 1);
556
557 /* Low Layer Capability */
558 skb_pull(skb, *(skb->data) + 1);
559
560 /* High Layer Capability */
561 skb_pull(skb, *(skb->data) + 1);
562
563 return 0;
564 }
565
capi_decode_conn_actv_conf(struct pcbit_chan * chan,struct sk_buff * skb)566 int capi_decode_conn_actv_conf(struct pcbit_chan * chan, struct sk_buff *skb)
567 {
568 ushort errcode;
569
570 errcode = *((ushort*) skb->data);
571 skb_pull(skb, 2);
572
573 /* Channel Identification
574 skb_pull(skb, skb->data[0] + 1);
575 */
576 return errcode;
577 }
578
579
capi_decode_sel_proto_conf(struct pcbit_chan * chan,struct sk_buff * skb)580 int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
581 {
582 ushort errcode;
583
584 chan->layer2link = *(skb->data);
585 skb_pull(skb, 1);
586
587 errcode = *((ushort*) skb->data);
588 skb_pull(skb, 2);
589
590 return errcode;
591 }
592
capi_decode_actv_trans_conf(struct pcbit_chan * chan,struct sk_buff * skb)593 int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
594 {
595 ushort errcode;
596
597 if (chan->layer2link != *(skb->data) )
598 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
599
600 skb_pull(skb, 1);
601
602 errcode = *((ushort*) skb->data);
603 skb_pull(skb, 2);
604
605 return errcode;
606 }
607
capi_decode_disc_ind(struct pcbit_chan * chan,struct sk_buff * skb)608 int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
609 {
610 ushort len;
611 #ifdef DEBUG
612 int i;
613 #endif
614 /* Cause */
615
616 len = *(skb->data);
617 skb_pull(skb, 1);
618
619 #ifdef DEBUG
620
621 for (i=0; i<len; i++)
622 printk(KERN_DEBUG "Cause Octect %d: %02x\n", i+3,
623 *(skb->data + i));
624 #endif
625
626 skb_pull(skb, len);
627
628 return 0;
629 }
630
capi_decode_disc_conf(struct pcbit_chan * chan,struct sk_buff * skb)631 int capi_decode_disc_conf(struct pcbit_chan *chan, struct sk_buff *skb)
632 {
633 ushort errcode;
634
635 errcode = *((ushort*) skb->data);
636 skb_pull(skb, 2);
637
638 return errcode;
639 }
640
641 #ifdef DEBUG
capi_decode_debug_188(u_char * hdr,ushort hdrlen)642 int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
643 {
644 char str[64];
645 int len;
646
647 len = hdr[0];
648
649 if (len < 64 && len == hdrlen - 1) {
650 memcpy(str, hdr + 1, hdrlen - 1);
651 str[hdrlen - 1] = 0;
652 printk("%s\n", str);
653 }
654 else
655 printk("debug message incorrect\n");
656
657 return 0;
658 }
659 #endif
660
661
662
663
664
665