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