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