1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224#define L1OIP_VERSION 0
225
226#include <linux/module.h>
227#include <linux/delay.h>
228#include <linux/mISDNif.h>
229#include <linux/mISDNhw.h>
230#include <linux/mISDNdsp.h>
231#include <linux/init.h>
232#include <linux/in.h>
233#include <linux/inet.h>
234#include <linux/workqueue.h>
235#include <linux/kthread.h>
236#include <net/sock.h>
237#include "core.h"
238#include "l1oip.h"
239
240static const char *l1oip_revision = "2.00";
241
242static int l1oip_cnt;
243static spinlock_t l1oip_lock;
244static struct list_head l1oip_ilist;
245
246#define MAX_CARDS 16
247static u_int type[MAX_CARDS];
248static u_int codec[MAX_CARDS];
249static u_int ip[MAX_CARDS*4];
250static u_int port[MAX_CARDS];
251static u_int remoteport[MAX_CARDS];
252static u_int ondemand[MAX_CARDS];
253static u_int limit[MAX_CARDS];
254static u_int id[MAX_CARDS];
255static int debug;
256static int ulaw;
257
258MODULE_AUTHOR("Andreas Eversberg");
259MODULE_LICENSE("GPL");
260module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
261module_param_array(codec, uint, NULL, S_IRUGO | S_IWUSR);
262module_param_array(ip, uint, NULL, S_IRUGO | S_IWUSR);
263module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
264module_param_array(remoteport, uint, NULL, S_IRUGO | S_IWUSR);
265module_param_array(ondemand, uint, NULL, S_IRUGO | S_IWUSR);
266module_param_array(limit, uint, NULL, S_IRUGO | S_IWUSR);
267module_param_array(id, uint, NULL, S_IRUGO | S_IWUSR);
268module_param(ulaw, uint, S_IRUGO | S_IWUSR);
269module_param(debug, uint, S_IRUGO | S_IWUSR);
270
271
272
273
274static int
275l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
276 u16 timebase, u8 *buf, int len)
277{
278 u8 *p;
279 int multi = 0;
280 u8 frame[len+32];
281 struct socket *socket = NULL;
282
283 if (debug & DEBUG_L1OIP_MSG)
284 printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
285 __func__, len);
286
287 p = frame;
288
289
290 if ((int)(hc->keep_tl.expires-jiffies) < 5*HZ) {
291 del_timer(&hc->keep_tl);
292 hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE*HZ;
293 add_timer(&hc->keep_tl);
294 } else
295 hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE*HZ;
296
297 if (debug & DEBUG_L1OIP_MSG)
298 printk(KERN_DEBUG "%s: resetting timer\n", __func__);
299
300
301 if (!hc->sin_remote.sin_addr.s_addr || !hc->sin_remote.sin_port) {
302 if (debug & DEBUG_L1OIP_MSG)
303 printk(KERN_DEBUG "%s: dropping frame, because remote "
304 "IP is not set.\n", __func__);
305 return len;
306 }
307
308
309 *p++ = (L1OIP_VERSION<<6)
310 | (hc->pri ? 0x20 : 0x00)
311 | (hc->id ? 0x10 : 0x00)
312 | localcodec;
313 if (hc->id) {
314 *p++ = hc->id>>24;
315 *p++ = hc->id>>16;
316 *p++ = hc->id>>8;
317 *p++ = hc->id;
318 }
319 *p++ = (multi == 1) ? 0x80 : 0x00 + channel;
320 if (multi == 1)
321 *p++ = len;
322 *p++ = timebase>>8;
323 *p++ = timebase;
324
325 if (buf && len) {
326 if (localcodec == 1 && ulaw)
327 l1oip_ulaw_to_alaw(buf, len, p);
328 else if (localcodec == 2 && !ulaw)
329 l1oip_alaw_to_ulaw(buf, len, p);
330 else if (localcodec == 3)
331 len = l1oip_law_to_4bit(buf, len, p,
332 &hc->chan[channel].codecstate);
333 else
334 memcpy(p, buf, len);
335 }
336 len += p - frame;
337
338
339 spin_lock(&hc->socket_lock);
340 if (!hc->socket) {
341 spin_unlock(&hc->socket_lock);
342 return 0;
343 }
344
345 socket = hc->socket;
346 hc->socket = NULL;
347 spin_unlock(&hc->socket_lock);
348
349 if (debug & DEBUG_L1OIP_MSG)
350 printk(KERN_DEBUG "%s: sending packet to socket (len "
351 "= %d)\n", __func__, len);
352 hc->sendiov.iov_base = frame;
353 hc->sendiov.iov_len = len;
354 len = kernel_sendmsg(socket, &hc->sendmsg, &hc->sendiov, 1, len);
355
356 hc->socket = socket;
357
358 return len;
359}
360
361
362
363
364
365static void
366l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
367 u8 *buf, int len)
368{
369 struct sk_buff *nskb;
370 struct bchannel *bch;
371 struct dchannel *dch;
372 u8 *p;
373 u32 rx_counter;
374
375 if (len == 0) {
376 if (debug & DEBUG_L1OIP_MSG)
377 printk(KERN_DEBUG "%s: received empty keepalive data, "
378 "ignoring\n", __func__);
379 return;
380 }
381
382 if (debug & DEBUG_L1OIP_MSG)
383 printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
384 __func__, len);
385
386 if (channel < 1 || channel > 127) {
387 printk(KERN_WARNING "%s: packet error - channel %d out of "
388 "range\n", __func__, channel);
389 return;
390 }
391 dch = hc->chan[channel].dch;
392 bch = hc->chan[channel].bch;
393 if (!dch && !bch) {
394 printk(KERN_WARNING "%s: packet error - channel %d not in "
395 "stack\n", __func__, channel);
396 return;
397 }
398
399
400 nskb = mI_alloc_skb((remotecodec == 3) ? (len<<1) : len, GFP_ATOMIC);
401 if (!nskb) {
402 printk(KERN_ERR "%s: No mem for skb.\n", __func__);
403 return;
404 }
405 p = skb_put(nskb, (remotecodec == 3) ? (len<<1) : len);
406
407 if (remotecodec == 1 && ulaw)
408 l1oip_alaw_to_ulaw(buf, len, p);
409 else if (remotecodec == 2 && !ulaw)
410 l1oip_ulaw_to_alaw(buf, len, p);
411 else if (remotecodec == 3)
412 len = l1oip_4bit_to_law(buf, len, p);
413 else
414 memcpy(p, buf, len);
415
416
417 if (dch && len >= 2) {
418 dch->rx_skb = nskb;
419 recv_Dchannel(dch);
420 }
421 if (bch) {
422
423 rx_counter = hc->chan[channel].rx_counter;
424 if (((s16)(timebase - rx_counter)) >= 0) {
425
426 if (timebase >= (rx_counter & 0xffff))
427 rx_counter =
428 (rx_counter & 0xffff0000) | timebase;
429 else
430 rx_counter = ((rx_counter & 0xffff0000)+0x10000)
431 | timebase;
432 } else {
433
434 if (timebase < (rx_counter & 0xffff))
435 rx_counter =
436 (rx_counter & 0xffff0000) | timebase;
437 else
438 rx_counter = ((rx_counter & 0xffff0000)-0x10000)
439 | timebase;
440 }
441 hc->chan[channel].rx_counter = rx_counter;
442
443#ifdef REORDER_DEBUG
444 if (hc->chan[channel].disorder_flag) {
445 struct sk_buff *skb;
446 int cnt;
447 skb = hc->chan[channel].disorder_skb;
448 hc->chan[channel].disorder_skb = nskb;
449 nskb = skb;
450 cnt = hc->chan[channel].disorder_cnt;
451 hc->chan[channel].disorder_cnt = rx_counter;
452 rx_counter = cnt;
453 }
454 hc->chan[channel].disorder_flag ^= 1;
455 if (nskb)
456#endif
457 queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
458 }
459}
460
461
462
463
464
465static void
466l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
467{
468 u32 packet_id;
469 u8 channel;
470 u8 remotecodec;
471 u16 timebase;
472 int m, mlen;
473 int len_start = len;
474 struct dchannel *dch = hc->chan[hc->d_idx].dch;
475
476 if (debug & DEBUG_L1OIP_MSG)
477 printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
478 __func__, len);
479
480
481 if (len < 1+1+2) {
482 printk(KERN_WARNING "%s: packet error - length %d below "
483 "4 bytes\n", __func__, len);
484 return;
485 }
486
487
488 if (((*buf)>>6) != L1OIP_VERSION) {
489 printk(KERN_WARNING "%s: packet error - unknown version %d\n",
490 __func__, buf[0]>>6);
491 return;
492 }
493
494
495 if (((*buf)&0x20) && !hc->pri) {
496 printk(KERN_WARNING "%s: packet error - received E1 packet "
497 "on S0 interface\n", __func__);
498 return;
499 }
500 if (!((*buf)&0x20) && hc->pri) {
501 printk(KERN_WARNING "%s: packet error - received S0 packet "
502 "on E1 interface\n", __func__);
503 return;
504 }
505
506
507 packet_id = (*buf>>4)&1;
508
509
510 remotecodec = (*buf) & 0x0f;
511 if (remotecodec > 3) {
512 printk(KERN_WARNING "%s: packet error - remotecodec %d "
513 "unsupported\n", __func__, remotecodec);
514 return;
515 }
516 buf++;
517 len--;
518
519
520 if (packet_id) {
521 if (!hc->id) {
522 printk(KERN_WARNING "%s: packet error - packet has id "
523 "0x%x, but we have not\n", __func__, packet_id);
524 return;
525 }
526 if (len < 4) {
527 printk(KERN_WARNING "%s: packet error - packet too "
528 "short for ID value\n", __func__);
529 return;
530 }
531 packet_id = (*buf++) << 24;
532 packet_id += (*buf++) << 16;
533 packet_id += (*buf++) << 8;
534 packet_id += (*buf++);
535 len -= 4;
536
537 if (packet_id != hc->id) {
538 printk(KERN_WARNING "%s: packet error - ID mismatch, "
539 "got 0x%x, we 0x%x\n",
540 __func__, packet_id, hc->id);
541 return;
542 }
543 } else {
544 if (hc->id) {
545 printk(KERN_WARNING "%s: packet error - packet has no "
546 "ID, but we have\n", __func__);
547 return;
548 }
549 }
550
551multiframe:
552 if (len < 1) {
553 printk(KERN_WARNING "%s: packet error - packet too short, "
554 "channel expected at position %d.\n",
555 __func__, len-len_start+1);
556 return;
557 }
558
559
560 channel = *buf&0x7f;
561 m = *buf >> 7;
562 buf++;
563 len--;
564
565
566 if (m) {
567 if (len < 1) {
568 printk(KERN_WARNING "%s: packet error - packet too "
569 "short, length expected at position %d.\n",
570 __func__, len_start-len-1);
571 return;
572 }
573
574 mlen = *buf++;
575 len--;
576 if (mlen == 0)
577 mlen = 256;
578 if (len < mlen+3) {
579 printk(KERN_WARNING "%s: packet error - length %d at "
580 "position %d exceeds total length %d.\n",
581 __func__, mlen, len_start-len-1, len_start);
582 return;
583 }
584 if (len == mlen+3) {
585 printk(KERN_WARNING "%s: packet error - length %d at "
586 "position %d will not allow additional "
587 "packet.\n",
588 __func__, mlen, len_start-len+1);
589 return;
590 }
591 } else
592 mlen = len-2;
593
594 if (len < 2) {
595 printk(KERN_WARNING "%s: packet error - packet too short, time "
596 "base expected at position %d.\n",
597 __func__, len-len_start+1);
598 return;
599 }
600
601
602 timebase = (*buf++) << 8;
603 timebase |= (*buf++);
604 len -= 2;
605
606
607 if (!test_bit(FLG_ACTIVE, &dch->Flags)) {
608 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
609 printk(KERN_DEBUG "%s: interface become active due to "
610 "received packet\n", __func__);
611 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
612 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
613 NULL, GFP_ATOMIC);
614 }
615
616
617 l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
618 buf += mlen;
619 len -= mlen;
620
621
622 if (m)
623 goto multiframe;
624
625
626 if ((int)(hc->timeout_tl.expires-jiffies) < 5*HZ || !hc->timeout_on) {
627 hc->timeout_on = 1;
628 del_timer(&hc->timeout_tl);
629 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT*HZ;
630 add_timer(&hc->timeout_tl);
631 } else
632 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT*HZ;
633
634
635 if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
636 || (hc->sin_remote.sin_port != sin->sin_port)) {
637 if (debug & DEBUG_L1OIP_SOCKET)
638 printk(KERN_DEBUG "%s: remote address changes from "
639 "0x%08x to 0x%08x (port %d to %d)\n", __func__,
640 ntohl(hc->sin_remote.sin_addr.s_addr),
641 ntohl(sin->sin_addr.s_addr),
642 ntohs(hc->sin_remote.sin_port),
643 ntohs(sin->sin_port));
644 hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
645 hc->sin_remote.sin_port = sin->sin_port;
646 }
647}
648
649
650
651
652
653static int
654l1oip_socket_thread(void *data)
655{
656 struct l1oip *hc = (struct l1oip *)data;
657 int ret = 0;
658 struct msghdr msg;
659 struct sockaddr_in sin_rx;
660 unsigned char *recvbuf;
661 size_t recvbuf_size = 1500;
662 int recvlen;
663 struct socket *socket = NULL;
664 DECLARE_COMPLETION(wait);
665
666
667 recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
668 if (!recvbuf) {
669 printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
670 ret = -ENOMEM;
671 goto fail;
672 }
673
674
675 allow_signal(SIGTERM);
676
677
678 if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
679 printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
680 ret = -EIO;
681 goto fail;
682 }
683
684
685 hc->sin_local.sin_family = AF_INET;
686 hc->sin_local.sin_addr.s_addr = INADDR_ANY;
687 hc->sin_local.sin_port = htons((unsigned short)hc->localport);
688
689
690 hc->sin_remote.sin_family = AF_INET;
691 hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip);
692 hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport);
693
694
695 if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
696 sizeof(hc->sin_local))) {
697 printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
698 __func__, hc->localport);
699 ret = -EINVAL;
700 goto fail;
701 }
702
703
704 if (socket->sk == NULL) {
705 printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
706 ret = -EIO;
707 goto fail;
708 }
709
710
711 msg.msg_name = &sin_rx;
712 msg.msg_namelen = sizeof(sin_rx);
713 msg.msg_control = NULL;
714 msg.msg_controllen = 0;
715
716
717 hc->sendmsg.msg_name = &hc->sin_remote;
718 hc->sendmsg.msg_namelen = sizeof(hc->sin_remote);
719 hc->sendmsg.msg_control = NULL;
720 hc->sendmsg.msg_controllen = 0;
721
722
723 spin_lock(&hc->socket_lock);
724 hc->socket = socket;
725 spin_unlock(&hc->socket_lock);
726
727
728 if (debug & DEBUG_L1OIP_SOCKET)
729 printk(KERN_DEBUG "%s: socket created and open\n",
730 __func__);
731 while (!signal_pending(current)) {
732 struct kvec iov = {
733 .iov_base = recvbuf,
734 .iov_len = recvbuf_size,
735 };
736 recvlen = kernel_recvmsg(socket, &msg, &iov, 1,
737 recvbuf_size, 0);
738 if (recvlen > 0) {
739 l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
740 } else {
741 if (debug & DEBUG_L1OIP_SOCKET)
742 printk(KERN_WARNING
743 "%s: broken pipe on socket\n", __func__);
744 }
745 }
746
747
748 spin_lock(&hc->socket_lock);
749
750 while (!hc->socket) {
751 spin_unlock(&hc->socket_lock);
752 schedule_timeout(HZ/10);
753 spin_lock(&hc->socket_lock);
754 }
755 hc->socket = NULL;
756 spin_unlock(&hc->socket_lock);
757
758 if (debug & DEBUG_L1OIP_SOCKET)
759 printk(KERN_DEBUG "%s: socket thread terminating\n",
760 __func__);
761
762fail:
763
764 kfree(recvbuf);
765
766
767 if (socket)
768 sock_release(socket);
769
770
771 complete(&hc->socket_complete);
772 hc->socket_thread = NULL;
773
774 if (debug & DEBUG_L1OIP_SOCKET)
775 printk(KERN_DEBUG "%s: socket thread terminated\n",
776 __func__);
777 return ret;
778}
779
780static void
781l1oip_socket_close(struct l1oip *hc)
782{
783 struct dchannel *dch = hc->chan[hc->d_idx].dch;
784
785
786 if (hc->socket_thread) {
787 if (debug & DEBUG_L1OIP_SOCKET)
788 printk(KERN_DEBUG "%s: socket thread exists, "
789 "killing...\n", __func__);
790 send_sig(SIGTERM, hc->socket_thread, 0);
791 wait_for_completion(&hc->socket_complete);
792 }
793
794
795 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
796 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
797 printk(KERN_DEBUG "%s: interface become deactivated "
798 "due to timeout\n", __func__);
799 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
800 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
801 NULL, GFP_ATOMIC);
802 }
803}
804
805static int
806l1oip_socket_open(struct l1oip *hc)
807{
808
809 l1oip_socket_close(hc);
810
811 init_completion(&hc->socket_complete);
812
813
814 hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
815 hc->name);
816 if (IS_ERR(hc->socket_thread)) {
817 int err = PTR_ERR(hc->socket_thread);
818 printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
819 __func__, err);
820 hc->socket_thread = NULL;
821 sock_release(hc->socket);
822 return err;
823 }
824 if (debug & DEBUG_L1OIP_SOCKET)
825 printk(KERN_DEBUG "%s: socket thread created\n", __func__);
826
827 return 0;
828}
829
830
831static void
832l1oip_send_bh(struct work_struct *work)
833{
834 struct l1oip *hc = container_of(work, struct l1oip, workq);
835
836 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
837 printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
838 "frame on dchannel\n", __func__);
839
840
841 l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
842}
843
844
845
846
847
848static void
849l1oip_keepalive(void *data)
850{
851 struct l1oip *hc = (struct l1oip *)data;
852
853 schedule_work(&hc->workq);
854}
855
856static void
857l1oip_timeout(void *data)
858{
859 struct l1oip *hc = (struct l1oip *)data;
860 struct dchannel *dch = hc->chan[hc->d_idx].dch;
861
862 if (debug & DEBUG_L1OIP_MSG)
863 printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
864 "down.\n", __func__);
865
866 hc->timeout_on = 0;
867
868
869 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
870 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
871 printk(KERN_DEBUG "%s: interface become deactivated "
872 "due to timeout\n", __func__);
873 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
874 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
875 NULL, GFP_ATOMIC);
876 }
877
878
879 if (hc->ondemand) {
880 if (debug & DEBUG_L1OIP_MSG)
881 printk(KERN_DEBUG "%s: on demand causes ip address to "
882 "be removed\n", __func__);
883 hc->sin_remote.sin_addr.s_addr = 0;
884 }
885}
886
887
888
889
890
891static int
892handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
893{
894 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
895 struct dchannel *dch = container_of(dev, struct dchannel, dev);
896 struct l1oip *hc = dch->hw;
897 struct mISDNhead *hh = mISDN_HEAD_P(skb);
898 int ret = -EINVAL;
899 int l, ll;
900 unsigned char *p;
901
902 switch (hh->prim) {
903 case PH_DATA_REQ:
904 if (skb->len < 1) {
905 printk(KERN_WARNING "%s: skb too small\n",
906 __func__);
907 break;
908 }
909 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
910 printk(KERN_WARNING "%s: skb too large\n",
911 __func__);
912 break;
913 }
914
915 p = skb->data;
916 l = skb->len;
917 while (l) {
918 ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
919 l1oip_socket_send(hc, 0, dch->slot, 0,
920 hc->chan[dch->slot].tx_counter++, p, ll);
921 p += ll;
922 l -= ll;
923 }
924 skb_trim(skb, 0);
925 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
926 return 0;
927 case PH_ACTIVATE_REQ:
928 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
929 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
930 , __func__, dch->slot, hc->b_num+1);
931 skb_trim(skb, 0);
932 if (test_bit(FLG_ACTIVE, &dch->Flags))
933 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
934 else
935 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
936 return 0;
937 case PH_DEACTIVATE_REQ:
938 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
939 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
940 "(1..%d)\n", __func__, dch->slot,
941 hc->b_num+1);
942 skb_trim(skb, 0);
943 if (test_bit(FLG_ACTIVE, &dch->Flags))
944 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
945 else
946 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
947 return 0;
948 }
949 if (!ret)
950 dev_kfree_skb(skb);
951 return ret;
952}
953
954static int
955channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
956{
957 int ret = 0;
958 struct l1oip *hc = dch->hw;
959
960 switch (cq->op) {
961 case MISDN_CTRL_GETOP:
962 cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
963 | MISDN_CTRL_GETPEER;
964 break;
965 case MISDN_CTRL_SETPEER:
966 hc->remoteip = (u32)cq->p1;
967 hc->remoteport = cq->p2 & 0xffff;
968 hc->localport = cq->p2 >> 16;
969 if (!hc->remoteport)
970 hc->remoteport = hc->localport;
971 if (debug & DEBUG_L1OIP_SOCKET)
972 printk(KERN_DEBUG "%s: got new ip address from user "
973 "space.\n", __func__);
974 l1oip_socket_open(hc);
975 break;
976 case MISDN_CTRL_UNSETPEER:
977 if (debug & DEBUG_L1OIP_SOCKET)
978 printk(KERN_DEBUG "%s: removing ip address.\n",
979 __func__);
980 hc->remoteip = 0;
981 l1oip_socket_open(hc);
982 break;
983 case MISDN_CTRL_GETPEER:
984 if (debug & DEBUG_L1OIP_SOCKET)
985 printk(KERN_DEBUG "%s: getting ip address.\n",
986 __func__);
987 cq->p1 = hc->remoteip;
988 cq->p2 = hc->remoteport | (hc->localport << 16);
989 break;
990 default:
991 printk(KERN_WARNING "%s: unknown Op %x\n",
992 __func__, cq->op);
993 ret = -EINVAL;
994 break;
995 }
996 return ret;
997}
998
999static int
1000open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1001{
1002 if (debug & DEBUG_HW_OPEN)
1003 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
1004 dch->dev.id, __builtin_return_address(0));
1005 if (rq->protocol == ISDN_P_NONE)
1006 return -EINVAL;
1007 if ((dch->dev.D.protocol != ISDN_P_NONE) &&
1008 (dch->dev.D.protocol != rq->protocol)) {
1009 if (debug & DEBUG_HW_OPEN)
1010 printk(KERN_WARNING "%s: change protocol %x to %x\n",
1011 __func__, dch->dev.D.protocol, rq->protocol);
1012 }
1013 if (dch->dev.D.protocol != rq->protocol)
1014 dch->dev.D.protocol = rq->protocol;
1015
1016 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1017 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
1018 0, NULL, GFP_KERNEL);
1019 }
1020 rq->ch = &dch->dev.D;
1021 if (!try_module_get(THIS_MODULE))
1022 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1023 return 0;
1024}
1025
1026static int
1027open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1028{
1029 struct bchannel *bch;
1030 int ch;
1031
1032 if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
1033 return -EINVAL;
1034 if (rq->protocol == ISDN_P_NONE)
1035 return -EINVAL;
1036 ch = rq->adr.channel;
1037 bch = hc->chan[ch].bch;
1038 if (!bch) {
1039 printk(KERN_ERR "%s:internal error ch %d has no bch\n",
1040 __func__, ch);
1041 return -EINVAL;
1042 }
1043 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1044 return -EBUSY;
1045 bch->ch.protocol = rq->protocol;
1046 rq->ch = &bch->ch;
1047 if (!try_module_get(THIS_MODULE))
1048 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1049 return 0;
1050}
1051
1052static int
1053l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1054{
1055 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1056 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1057 struct l1oip *hc = dch->hw;
1058 struct channel_req *rq;
1059 int err = 0;
1060
1061 if (dch->debug & DEBUG_HW)
1062 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1063 __func__, cmd, arg);
1064 switch (cmd) {
1065 case OPEN_CHANNEL:
1066 rq = arg;
1067 switch (rq->protocol) {
1068 case ISDN_P_TE_S0:
1069 case ISDN_P_NT_S0:
1070 if (hc->pri) {
1071 err = -EINVAL;
1072 break;
1073 }
1074 err = open_dchannel(hc, dch, rq);
1075 break;
1076 case ISDN_P_TE_E1:
1077 case ISDN_P_NT_E1:
1078 if (!hc->pri) {
1079 err = -EINVAL;
1080 break;
1081 }
1082 err = open_dchannel(hc, dch, rq);
1083 break;
1084 default:
1085 err = open_bchannel(hc, dch, rq);
1086 }
1087 break;
1088 case CLOSE_CHANNEL:
1089 if (debug & DEBUG_HW_OPEN)
1090 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
1091 __func__, dch->dev.id,
1092 __builtin_return_address(0));
1093 module_put(THIS_MODULE);
1094 break;
1095 case CONTROL_CHANNEL:
1096 err = channel_dctrl(dch, arg);
1097 break;
1098 default:
1099 if (dch->debug & DEBUG_HW)
1100 printk(KERN_DEBUG "%s: unknown command %x\n",
1101 __func__, cmd);
1102 err = -EINVAL;
1103 }
1104 return err;
1105}
1106
1107static int
1108handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1109{
1110 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1111 struct l1oip *hc = bch->hw;
1112 int ret = -EINVAL;
1113 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1114 int l, ll, i;
1115 unsigned char *p;
1116
1117 switch (hh->prim) {
1118 case PH_DATA_REQ:
1119 if (skb->len <= 0) {
1120 printk(KERN_WARNING "%s: skb too small\n",
1121 __func__);
1122 break;
1123 }
1124 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1125 printk(KERN_WARNING "%s: skb too large\n",
1126 __func__);
1127 break;
1128 }
1129
1130 p = skb->data;
1131 l = skb->len;
1132 for (i = 0; i < l; i++) {
1133 if (*p++ != 0xff)
1134 break;
1135 }
1136 if (i == l) {
1137 if (debug & DEBUG_L1OIP_MSG)
1138 printk(KERN_DEBUG "%s: got AIS, not sending, "
1139 "but counting\n", __func__);
1140 hc->chan[bch->slot].tx_counter += l;
1141 skb_trim(skb, 0);
1142 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1143 return 0;
1144 }
1145
1146 p = skb->data;
1147 l = skb->len;
1148 for (i = 0; i < l; i++) {
1149 if (*p++ != 0x2a)
1150 break;
1151 }
1152 if (i == l) {
1153 if (debug & DEBUG_L1OIP_MSG)
1154 printk(KERN_DEBUG "%s: got silence, not sending"
1155 ", but counting\n", __func__);
1156 hc->chan[bch->slot].tx_counter += l;
1157 skb_trim(skb, 0);
1158 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1159 return 0;
1160 }
1161
1162
1163 p = skb->data;
1164 l = skb->len;
1165 while (l) {
1166 ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
1167 l1oip_socket_send(hc, hc->codec, bch->slot, 0,
1168 hc->chan[bch->slot].tx_counter, p, ll);
1169 hc->chan[bch->slot].tx_counter += ll;
1170 p += ll;
1171 l -= ll;
1172 }
1173 skb_trim(skb, 0);
1174 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1175 return 0;
1176 case PH_ACTIVATE_REQ:
1177 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
1178 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
1179 , __func__, bch->slot, hc->b_num+1);
1180 hc->chan[bch->slot].codecstate = 0;
1181 test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1182 skb_trim(skb, 0);
1183 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1184 return 0;
1185 case PH_DEACTIVATE_REQ:
1186 if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET))
1187 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
1188 "(1..%d)\n", __func__, bch->slot,
1189 hc->b_num+1);
1190 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1191 skb_trim(skb, 0);
1192 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1193 return 0;
1194 }
1195 if (!ret)
1196 dev_kfree_skb(skb);
1197 return ret;
1198}
1199
1200static int
1201channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1202{
1203 int ret = 0;
1204 struct dsp_features *features =
1205 (struct dsp_features *)(*((u_long *)&cq->p1));
1206
1207 switch (cq->op) {
1208 case MISDN_CTRL_GETOP:
1209 cq->op = MISDN_CTRL_HW_FEATURES_OP;
1210 break;
1211 case MISDN_CTRL_HW_FEATURES:
1212 if (debug & DEBUG_L1OIP_MSG)
1213 printk(KERN_DEBUG "%s: HW_FEATURE request\n",
1214 __func__);
1215
1216 features->unclocked = 1;
1217 features->unordered = 1;
1218 break;
1219 default:
1220 printk(KERN_WARNING "%s: unknown Op %x\n",
1221 __func__, cq->op);
1222 ret = -EINVAL;
1223 break;
1224 }
1225 return ret;
1226}
1227
1228static int
1229l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1230{
1231 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1232 int err = -EINVAL;
1233
1234 if (bch->debug & DEBUG_HW)
1235 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1236 __func__, cmd, arg);
1237 switch (cmd) {
1238 case CLOSE_CHANNEL:
1239 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1240 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1241 ch->protocol = ISDN_P_NONE;
1242 ch->peer = NULL;
1243 module_put(THIS_MODULE);
1244 err = 0;
1245 break;
1246 case CONTROL_CHANNEL:
1247 err = channel_bctrl(bch, arg);
1248 break;
1249 default:
1250 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1251 __func__, cmd);
1252 }
1253 return err;
1254}
1255
1256
1257
1258
1259
1260static void
1261release_card(struct l1oip *hc)
1262{
1263 int ch;
1264
1265 if (timer_pending(&hc->keep_tl))
1266 del_timer(&hc->keep_tl);
1267
1268 if (timer_pending(&hc->timeout_tl))
1269 del_timer(&hc->timeout_tl);
1270
1271 if (hc->socket_thread)
1272 l1oip_socket_close(hc);
1273
1274 if (hc->registered && hc->chan[hc->d_idx].dch)
1275 mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1276 for (ch = 0; ch < 128; ch++) {
1277 if (hc->chan[ch].dch) {
1278 mISDN_freedchannel(hc->chan[ch].dch);
1279 kfree(hc->chan[ch].dch);
1280 }
1281 if (hc->chan[ch].bch) {
1282 mISDN_freebchannel(hc->chan[ch].bch);
1283 kfree(hc->chan[ch].bch);
1284#ifdef REORDER_DEBUG
1285 if (hc->chan[ch].disorder_skb)
1286 dev_kfree_skb(hc->chan[ch].disorder_skb);
1287#endif
1288 }
1289 }
1290
1291 spin_lock(&l1oip_lock);
1292 list_del(&hc->list);
1293 spin_unlock(&l1oip_lock);
1294
1295 kfree(hc);
1296}
1297
1298static void
1299l1oip_cleanup(void)
1300{
1301 struct l1oip *hc, *next;
1302
1303 list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1304 release_card(hc);
1305
1306 l1oip_4bit_free();
1307}
1308
1309
1310
1311
1312
1313static int
1314init_card(struct l1oip *hc, int pri, int bundle)
1315{
1316 struct dchannel *dch;
1317 struct bchannel *bch;
1318 int ret;
1319 int i, ch;
1320
1321 spin_lock_init(&hc->socket_lock);
1322 hc->idx = l1oip_cnt;
1323 hc->pri = pri;
1324 hc->d_idx = pri ? 16 : 3;
1325 hc->b_num = pri ? 30 : 2;
1326 hc->bundle = bundle;
1327 if (hc->pri)
1328 sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1329 else
1330 sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1331
1332 switch (codec[l1oip_cnt]) {
1333 case 0:
1334 case 1:
1335 case 2:
1336 case 3:
1337 break;
1338 default:
1339 printk(KERN_ERR "Codec(%d) not supported.\n",
1340 codec[l1oip_cnt]);
1341 return -EINVAL;
1342 }
1343 hc->codec = codec[l1oip_cnt];
1344 if (debug & DEBUG_L1OIP_INIT)
1345 printk(KERN_DEBUG "%s: using codec %d\n",
1346 __func__, hc->codec);
1347
1348 if (id[l1oip_cnt] == 0) {
1349 printk(KERN_WARNING "Warning: No 'id' value given or "
1350 "0, this is highly unsecure. Please use 32 "
1351 "bit randmom number 0x...\n");
1352 }
1353 hc->id = id[l1oip_cnt];
1354 if (debug & DEBUG_L1OIP_INIT)
1355 printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1356
1357 hc->ondemand = ondemand[l1oip_cnt];
1358 if (hc->ondemand && !hc->id) {
1359 printk(KERN_ERR "%s: ondemand option only allowed in "
1360 "conjunction with non 0 ID\n", __func__);
1361 return -EINVAL;
1362 }
1363
1364 if (limit[l1oip_cnt])
1365 hc->b_num = limit[l1oip_cnt];
1366 if (!pri && hc->b_num > 2) {
1367 printk(KERN_ERR "Maximum limit for BRI interface is 2 "
1368 "channels.\n");
1369 return -EINVAL;
1370 }
1371 if (pri && hc->b_num > 126) {
1372 printk(KERN_ERR "Maximum limit for PRI interface is 126 "
1373 "channels.\n");
1374 return -EINVAL;
1375 }
1376 if (pri && hc->b_num > 30) {
1377 printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
1378 "channels.\n");
1379 printk(KERN_WARNING "Your selection of %d channels must be "
1380 "supported by application.\n", hc->limit);
1381 }
1382
1383 hc->remoteip = ip[l1oip_cnt<<2] << 24
1384 | ip[(l1oip_cnt<<2)+1] << 16
1385 | ip[(l1oip_cnt<<2)+2] << 8
1386 | ip[(l1oip_cnt<<2)+3];
1387 hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT+l1oip_cnt);
1388 if (remoteport[l1oip_cnt])
1389 hc->remoteport = remoteport[l1oip_cnt];
1390 else
1391 hc->remoteport = hc->localport;
1392 if (debug & DEBUG_L1OIP_INIT)
1393 printk(KERN_DEBUG "%s: using local port %d remote ip "
1394 "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1395 hc->localport, hc->remoteip >> 24,
1396 (hc->remoteip >> 16) & 0xff,
1397 (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1398 hc->remoteport, hc->ondemand);
1399
1400 dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1401 if (!dch)
1402 return -ENOMEM;
1403 dch->debug = debug;
1404 mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1405 dch->hw = hc;
1406 if (pri)
1407 dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1408 else
1409 dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1410 dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1411 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1412 dch->dev.D.send = handle_dmsg;
1413 dch->dev.D.ctrl = l1oip_dctrl;
1414 dch->dev.nrbchan = hc->b_num;
1415 dch->slot = hc->d_idx;
1416 hc->chan[hc->d_idx].dch = dch;
1417 i = 1;
1418 for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1419 if (ch == 15)
1420 i++;
1421 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1422 if (!bch) {
1423 printk(KERN_ERR "%s: no memory for bchannel\n",
1424 __func__);
1425 return -ENOMEM;
1426 }
1427 bch->nr = i + ch;
1428 bch->slot = i + ch;
1429 bch->debug = debug;
1430 mISDN_initbchannel(bch, MAX_DATA_MEM);
1431 bch->hw = hc;
1432 bch->ch.send = handle_bmsg;
1433 bch->ch.ctrl = l1oip_bctrl;
1434 bch->ch.nr = i + ch;
1435 list_add(&bch->ch.list, &dch->dev.bchannels);
1436 hc->chan[i + ch].bch = bch;
1437 set_channelmap(bch->nr, dch->dev.channelmap);
1438 }
1439
1440 ret = mISDN_register_device(&dch->dev, NULL, hc->name);
1441 if (ret)
1442 return ret;
1443 hc->registered = 1;
1444
1445 if (debug & DEBUG_L1OIP_INIT)
1446 printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
1447 __func__, l1oip_cnt + 1);
1448 ret = l1oip_socket_open(hc);
1449 if (ret)
1450 return ret;
1451
1452 hc->keep_tl.function = (void *)l1oip_keepalive;
1453 hc->keep_tl.data = (ulong)hc;
1454 init_timer(&hc->keep_tl);
1455 hc->keep_tl.expires = jiffies + 2*HZ;
1456 add_timer(&hc->keep_tl);
1457
1458 hc->timeout_tl.function = (void *)l1oip_timeout;
1459 hc->timeout_tl.data = (ulong)hc;
1460 init_timer(&hc->timeout_tl);
1461 hc->timeout_on = 0;
1462
1463 return 0;
1464}
1465
1466static int __init
1467l1oip_init(void)
1468{
1469 int pri, bundle;
1470 struct l1oip *hc;
1471 int ret;
1472
1473 printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
1474 l1oip_revision);
1475
1476 INIT_LIST_HEAD(&l1oip_ilist);
1477 spin_lock_init(&l1oip_lock);
1478
1479 if (l1oip_4bit_alloc(ulaw))
1480 return -ENOMEM;
1481
1482 l1oip_cnt = 0;
1483 while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
1484 switch (type[l1oip_cnt] & 0xff) {
1485 case 1:
1486 pri = 0;
1487 bundle = 0;
1488 break;
1489 case 2:
1490 pri = 1;
1491 bundle = 0;
1492 break;
1493 case 3:
1494 pri = 0;
1495 bundle = 1;
1496 break;
1497 case 4:
1498 pri = 1;
1499 bundle = 1;
1500 break;
1501 default:
1502 printk(KERN_ERR "Card type(%d) not supported.\n",
1503 type[l1oip_cnt] & 0xff);
1504 l1oip_cleanup();
1505 return -EINVAL;
1506 }
1507
1508 if (debug & DEBUG_L1OIP_INIT)
1509 printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
1510 __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1511 bundle ? "bundled IP packet for all B-channels" :
1512 "seperate IP packets for every B-channel");
1513
1514 hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1515 if (!hc) {
1516 printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1517 l1oip_cleanup();
1518 return -ENOMEM;
1519 }
1520 INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1521
1522 spin_lock(&l1oip_lock);
1523 list_add_tail(&hc->list, &l1oip_ilist);
1524 spin_unlock(&l1oip_lock);
1525
1526 ret = init_card(hc, pri, bundle);
1527 if (ret) {
1528 l1oip_cleanup();
1529 return ret;
1530 }
1531
1532 l1oip_cnt++;
1533 }
1534 printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1535 return 0;
1536}
1537
1538module_init(l1oip_init);
1539module_exit(l1oip_cleanup);
1540
1541