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