3 * l1oip.c low level driver for tunneling layer 1 over IP
5 * NOTE: It is not compatible with TDMoIP nor "ISDN over IP".
7 * Author Andreas Eversberg (jolly@eversberg.eu)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 Value 3 = BRI (multi channel frame, not supported yet)
30 Value 4 = PRI (multi channel frame, not supported yet)
31 A multi channel frame reduces overhead to a single frame for all
32 b-channels, but increases delay.
33 (NOTE: Multi channel frames are not implemented yet.)
36 Value 0 = transparent (default)
37 Value 1 = transfer ALAW
38 Value 2 = transfer ULAW
39 Value 3 = transfer generic 4 bit compression.
42 0 = we use a-Law (default)
46 limitation of B-channels to control bandwidth (1...126)
48 PRI: 1-30, 31-126 (126, because dchannel ist not counted here)
49 Also limited ressources are used for stack, resulting in less channels.
50 It is possible to have more channels than 30 in PRI mode, this must
51 be supported by the application.
54 byte representation of remote ip address (127.0.0.1 -> 127,0,0,1)
55 If not given or four 0, no remote address is set.
56 For multiple interfaces, concat ip addresses. (127,0,0,1,127,0,0,1)
59 port number (local interface)
60 If not given or 0, port 931 is used for fist instance, 932 for next...
61 For multiple interfaces, different ports must be given.
64 port number (remote interface)
65 If not given or 0, remote port equals local port
66 For multiple interfaces on equal sites, different ports must be given.
69 0 = fixed (always transmit packets, even when remote side timed out)
70 1 = on demand (only transmit packets, when remote side is detected)
72 NOTE: ID must also be set for on demand.
75 optional value to identify frames. This value must be equal on both
76 peers and should be random. If omitted or 0, no ID is transmitted.
79 NOTE: only one debug value must be given for all cards
80 enable debugging (see l1oip.h for debug options)
83 Special mISDN controls:
85 op = MISDN_CTRL_SETPEER*
86 p1 = bytes 0-3 : remote IP address in network order (left element first)
87 p2 = bytes 1-2 : remote port in network order (high byte first)
89 p2 = bytes 3-4 : local port in network order (high byte first)
91 op = MISDN_CTRL_UNSETPEER*
93 * Use l1oipctrl for comfortable setting or removing ip address.
94 (Layer 1 Over IP CTRL)
141 * Only included in some cases.
144 If version is missmatch, the frame must be ignored.
146 - T = Type of interface
147 Must be 0 for S0 or 1 for E1.
150 If bit is set, four ID bytes are included in frame.
153 Additional ID to prevent Denial of Service attacs. Also it prevents hijacking
154 connections with dynamic IP. The ID should be random and must not be 0.
156 - Coding = Type of codec
157 Must be 0 for no transcoding. Also for D-channel and other HDLC frames.
158 1 and 2 are reserved for explicitly use of a-LAW or u-LAW codec.
159 3 is used for generic table compressor.
161 - M = More channels to come. If this flag is 1, the following byte contains
162 the length of the channel data. After the data block, the next channel will
163 be defined. The flag for the last channel block (or if only one channel is
164 transmitted), must be 0 and no length is given.
166 - Channel = Channel number
168 1-3 channel data for S0 (3 is D-channel)
169 1-31 channel data for E1 (16 is D-channel)
170 32-127 channel data for extended E1 (16 is D-channel)
172 - The length is used if the M-flag is 1. It is used to find the next channel
174 NOTE: A value of 0 equals 256 bytes of data.
175 -> For larger data blocks, a single frame must be used.
176 -> For larger streams, a single frame or multiple blocks with same channel ID
179 - Time Base = Timestamp of first sample in frame
180 The "Time Base" is used to rearange packets and to detect packet loss.
181 The 16 bits are sent in network order (MSB first) and count 1/8000 th of a
182 second. This causes a wrap arround each 8,192 seconds. There is no requirement
183 for the initial "Time Base", but 0 should be used for the first packet.
184 In case of HDLC data, this timestamp counts the packet or byte number.
189 After initialisation, a timer of 15 seconds is started. Whenever a packet is
190 transmitted, the timer is reset to 15 seconds again. If the timer expires, an
191 empty packet is transmitted. This keep the connection alive.
193 When a valid packet is received, a timer 65 seconds is started. The interface
194 become ACTIVE. If the timer expires, the interface becomes INACTIVE.
199 To allow dynamic IP, the ID must be non 0. In this case, any packet with the
200 correct port number and ID will be accepted. If the remote side changes its IP
201 the new IP is used for all transmitted packets until it changes again.
206 If the ondemand parameter is given, the remote IP is set to 0 on timeout.
207 This will stop keepalive traffic to remote. If the remote is online again,
208 traffic will continue to the remote address. This is usefull for road warriors.
209 This feature only works with ID set, otherwhise it is highly unsecure.
215 The complete socket opening and closing is done by a thread.
216 When the thread opened a socket, the hc->socket descriptor is set. Whenever a
217 packet shall be sent to the socket, the hc->socket must be checked wheter not
218 NULL. To prevent change in socket descriptor, the hc->socket_lock must be used.
219 To change the socket, a recall of l1oip_socket_open() will safely kill the
220 socket process and create a new one.
224 #define L1OIP_VERSION 0 /* 0...3 */
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>
240 static const char *l1oip_revision
= "2.00";
242 static int l1oip_cnt
;
243 static spinlock_t l1oip_lock
;
244 static struct list_head l1oip_ilist
;
247 static u_int type
[MAX_CARDS
];
248 static u_int codec
[MAX_CARDS
];
249 static u_int ip
[MAX_CARDS
*4];
250 static u_int port
[MAX_CARDS
];
251 static u_int remoteport
[MAX_CARDS
];
252 static u_int ondemand
[MAX_CARDS
];
253 static u_int limit
[MAX_CARDS
];
254 static u_int id
[MAX_CARDS
];
258 MODULE_AUTHOR("Andreas Eversberg");
259 MODULE_LICENSE("GPL");
260 module_param_array(type
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
261 module_param_array(codec
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
262 module_param_array(ip
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
263 module_param_array(port
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
264 module_param_array(remoteport
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
265 module_param_array(ondemand
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
266 module_param_array(limit
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
267 module_param_array(id
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
268 module_param(ulaw
, uint
, S_IRUGO
| S_IWUSR
);
269 module_param(debug
, uint
, S_IRUGO
| S_IWUSR
);
272 * send a frame via socket, if open and restart timer
275 l1oip_socket_send(struct l1oip
*hc
, u8 localcodec
, u8 channel
, u32 chanmask
,
276 u16 timebase
, u8
*buf
, int len
)
281 struct socket
*socket
= NULL
;
283 if (debug
& DEBUG_L1OIP_MSG
)
284 printk(KERN_DEBUG
"%s: sending data to socket (len = %d)\n",
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
);
295 hc
->keep_tl
.expires
= jiffies
+ L1OIP_KEEPALIVE
*HZ
;
297 if (debug
& DEBUG_L1OIP_MSG
)
298 printk(KERN_DEBUG
"%s: resetting timer\n", __func__
);
300 /* drop if we have no remote ip or port */
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__
);
309 *p
++ = (L1OIP_VERSION
<<6) /* version and coding */
310 | (hc
->pri
? 0x20 : 0x00) /* type */
311 | (hc
->id
? 0x10 : 0x00) /* id */
314 *p
++ = hc
->id
>>24; /* id */
319 *p
++ = (multi
== 1) ? 0x80 : 0x00 + channel
; /* m-flag, channel */
321 *p
++ = len
; /* length */
322 *p
++ = timebase
>>8; /* time base */
325 if (buf
&& len
) { /* add data to frame */
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
);
338 /* check for socket in safe condition */
339 spin_lock(&hc
->socket_lock
);
341 spin_unlock(&hc
->socket_lock
);
347 spin_unlock(&hc
->socket_lock
);
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 /* give socket back */
356 hc
->socket
= socket
; /* no locking required */
363 * receive channel data from socket
366 l1oip_socket_recv(struct l1oip
*hc
, u8 remotecodec
, u8 channel
, u16 timebase
,
369 struct sk_buff
*nskb
;
370 struct bchannel
*bch
;
371 struct dchannel
*dch
;
376 if (debug
& DEBUG_L1OIP_MSG
)
377 printk(KERN_DEBUG
"%s: received empty keepalive data, "
378 "ignoring\n", __func__
);
382 if (debug
& DEBUG_L1OIP_MSG
)
383 printk(KERN_DEBUG
"%s: received data, sending to mISDN (%d)\n",
386 if (channel
< 1 || channel
> 127) {
387 printk(KERN_WARNING
"%s: packet error - channel %d out of "
388 "range\n", __func__
, channel
);
391 dch
= hc
->chan
[channel
].dch
;
392 bch
= hc
->chan
[channel
].bch
;
394 printk(KERN_WARNING
"%s: packet error - channel %d not in "
395 "stack\n", __func__
, channel
);
399 /* prepare message */
400 nskb
= mI_alloc_skb((remotecodec
== 3) ? (len
<<1) : len
, GFP_ATOMIC
);
402 printk(KERN_ERR
"%s: No mem for skb.\n", __func__
);
405 p
= skb_put(nskb
, (remotecodec
== 3) ? (len
<<1) : len
);
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
);
416 /* send message up */
417 if (dch
&& len
>= 2) {
422 /* expand 16 bit sequence number to 32 bit sequence number */
423 rx_counter
= hc
->chan
[channel
].rx_counter
;
424 if (((s16
)(timebase
- rx_counter
)) >= 0) {
425 /* time has changed forward */
426 if (timebase
>= (rx_counter
& 0xffff))
428 (rx_counter
& 0xffff0000) | timebase
;
430 rx_counter
= ((rx_counter
& 0xffff0000)+0x10000)
433 /* time has changed backwards */
434 if (timebase
< (rx_counter
& 0xffff))
436 (rx_counter
& 0xffff0000) | timebase
;
438 rx_counter
= ((rx_counter
& 0xffff0000)-0x10000)
441 hc
->chan
[channel
].rx_counter
= rx_counter
;
444 if (hc
->chan
[channel
].disorder_flag
) {
447 skb
= hc
->chan
[channel
].disorder_skb
;
448 hc
->chan
[channel
].disorder_skb
= nskb
;
450 cnt
= hc
->chan
[channel
].disorder_cnt
;
451 hc
->chan
[channel
].disorder_cnt
= rx_counter
;
454 hc
->chan
[channel
].disorder_flag
^= 1;
457 queue_ch_frame(&bch
->ch
, PH_DATA_IND
, rx_counter
, nskb
);
463 * parse frame and extract channel data
466 l1oip_socket_parse(struct l1oip
*hc
, struct sockaddr_in
*sin
, u8
*buf
, int len
)
473 int len_start
= len
; /* initial frame length */
474 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
476 if (debug
& DEBUG_L1OIP_MSG
)
477 printk(KERN_DEBUG
"%s: received frame, parsing... (%d)\n",
482 printk(KERN_WARNING
"%s: packet error - length %d below "
483 "4 bytes\n", __func__
, len
);
488 if (((*buf
)>>6) != L1OIP_VERSION
) {
489 printk(KERN_WARNING
"%s: packet error - unknown version %d\n",
490 __func__
, buf
[0]>>6);
495 if (((*buf
)&0x20) && !hc
->pri
) {
496 printk(KERN_WARNING
"%s: packet error - received E1 packet "
497 "on S0 interface\n", __func__
);
500 if (!((*buf
)&0x20) && hc
->pri
) {
501 printk(KERN_WARNING
"%s: packet error - received S0 packet "
502 "on E1 interface\n", __func__
);
507 packet_id
= (*buf
>>4)&1;
510 remotecodec
= (*buf
) & 0x0f;
511 if (remotecodec
> 3) {
512 printk(KERN_WARNING
"%s: packet error - remotecodec %d "
513 "unsupported\n", __func__
, remotecodec
);
519 /* check packet_id */
522 printk(KERN_WARNING
"%s: packet error - packet has id "
523 "0x%x, but we have not\n", __func__
, packet_id
);
527 printk(KERN_WARNING
"%s: packet error - packet too "
528 "short for ID value\n", __func__
);
531 packet_id
= (*buf
++) << 24;
532 packet_id
+= (*buf
++) << 16;
533 packet_id
+= (*buf
++) << 8;
534 packet_id
+= (*buf
++);
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
);
545 printk(KERN_WARNING
"%s: packet error - packet has no "
546 "ID, but we have\n", __func__
);
553 printk(KERN_WARNING
"%s: packet error - packet too short, "
554 "channel expected at position %d.\n",
555 __func__
, len
-len_start
+1);
559 /* get channel and multiframe flag */
565 /* check length on multiframe */
568 printk(KERN_WARNING
"%s: packet error - packet too "
569 "short, length expected at position %d.\n",
570 __func__
, len_start
-len
-1);
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
);
585 printk(KERN_WARNING
"%s: packet error - length %d at "
586 "position %d will not allow additional "
588 __func__
, mlen
, len_start
-len
+1);
592 mlen
= len
-2; /* single frame, substract timebase */
595 printk(KERN_WARNING
"%s: packet error - packet too short, time "
596 "base expected at position %d.\n",
597 __func__
, len
-len_start
+1);
602 timebase
= (*buf
++) << 8;
603 timebase
|= (*buf
++);
606 /* if inactive, we send up a PH_ACTIVATE and activate */
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,
616 /* distribute packet */
617 l1oip_socket_recv(hc
, remotecodec
, channel
, timebase
, buf
, mlen
);
626 if ((int)(hc
->timeout_tl
.expires
-jiffies
) < 5*HZ
|| !hc
->timeout_on
) {
628 del_timer(&hc
->timeout_tl
);
629 hc
->timeout_tl
.expires
= jiffies
+ L1OIP_TIMEOUT
*HZ
;
630 add_timer(&hc
->timeout_tl
);
631 } else /* only adjust timer */
632 hc
->timeout_tl
.expires
= jiffies
+ L1OIP_TIMEOUT
*HZ
;
634 /* if ip or source port changes */
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
;
654 l1oip_socket_thread(void *data
)
656 struct l1oip
*hc
= (struct l1oip
*)data
;
659 struct sockaddr_in sin_rx
;
660 unsigned char *recvbuf
;
661 size_t recvbuf_size
= 1500;
663 struct socket
*socket
= NULL
;
664 DECLARE_COMPLETION(wait
);
666 /* allocate buffer memory */
667 recvbuf
= kmalloc(recvbuf_size
, GFP_KERNEL
);
669 printk(KERN_ERR
"%s: Failed to alloc recvbuf.\n", __func__
);
675 allow_signal(SIGTERM
);
678 if (sock_create(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &socket
)) {
679 printk(KERN_ERR
"%s: Failed to create socket.\n", __func__
);
684 /* set incoming address */
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
);
689 /* set outgoing address */
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
);
694 /* bind to incomming port */
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
);
704 if (socket
->sk
== NULL
) {
705 printk(KERN_ERR
"%s: socket->sk == NULL\n", __func__
);
710 /* build receive message */
711 msg
.msg_name
= &sin_rx
;
712 msg
.msg_namelen
= sizeof(sin_rx
);
713 msg
.msg_control
= NULL
;
714 msg
.msg_controllen
= 0;
716 /* build send message */
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;
722 /* give away socket */
723 spin_lock(&hc
->socket_lock
);
725 spin_unlock(&hc
->socket_lock
);
728 if (debug
& DEBUG_L1OIP_SOCKET
)
729 printk(KERN_DEBUG
"%s: socket created and open\n",
731 while (!signal_pending(current
)) {
734 .iov_len
= recvbuf_size
,
736 recvlen
= kernel_recvmsg(socket
, &msg
, &iov
, 1,
739 l1oip_socket_parse(hc
, &sin_rx
, recvbuf
, recvlen
);
741 if (debug
& DEBUG_L1OIP_SOCKET
)
743 "%s: broken pipe on socket\n", __func__
);
747 /* get socket back, check first if in use, maybe by send function */
748 spin_lock(&hc
->socket_lock
);
749 /* if hc->socket is NULL, it is in use until it is given back */
750 while (!hc
->socket
) {
751 spin_unlock(&hc
->socket_lock
);
752 schedule_timeout(HZ
/10);
753 spin_lock(&hc
->socket_lock
);
756 spin_unlock(&hc
->socket_lock
);
758 if (debug
& DEBUG_L1OIP_SOCKET
)
759 printk(KERN_DEBUG
"%s: socket thread terminating\n",
768 sock_release(socket
);
770 /* if we got killed, signal completion */
771 complete(&hc
->socket_complete
);
772 hc
->socket_thread
= NULL
; /* show termination of thread */
774 if (debug
& DEBUG_L1OIP_SOCKET
)
775 printk(KERN_DEBUG
"%s: socket thread terminated\n",
781 l1oip_socket_close(struct l1oip
*hc
)
783 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
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
);
794 /* if active, we send up a PH_DEACTIVATE and deactivate */
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,
806 l1oip_socket_open(struct l1oip
*hc
)
808 /* in case of reopen, we need to close first */
809 l1oip_socket_close(hc
);
811 init_completion(&hc
->socket_complete
);
813 /* create receive process */
814 hc
->socket_thread
= kthread_run(l1oip_socket_thread
, hc
, "l1oip_%s",
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",
820 hc
->socket_thread
= NULL
;
821 sock_release(hc
->socket
);
824 if (debug
& DEBUG_L1OIP_SOCKET
)
825 printk(KERN_DEBUG
"%s: socket thread created\n", __func__
);
832 l1oip_send_bh(struct work_struct
*work
)
834 struct l1oip
*hc
= container_of(work
, struct l1oip
, workq
);
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__
);
840 /* send an empty l1oip frame at D-channel */
841 l1oip_socket_send(hc
, 0, hc
->d_idx
, 0, 0, NULL
, 0);
849 l1oip_keepalive(void *data
)
851 struct l1oip
*hc
= (struct l1oip
*)data
;
853 schedule_work(&hc
->workq
);
857 l1oip_timeout(void *data
)
859 struct l1oip
*hc
= (struct l1oip
*)data
;
860 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
862 if (debug
& DEBUG_L1OIP_MSG
)
863 printk(KERN_DEBUG
"%s: timeout timer expired, turn layer one "
864 "down.\n", __func__
);
866 hc
->timeout_on
= 0; /* state that timer must be initialized next time */
868 /* if timeout, we send up a PH_DEACTIVATE and deactivate */
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,
878 /* if we have ondemand set, we remove ip address */
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;
892 handle_dmsg(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
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
);
905 printk(KERN_WARNING
"%s: skb too small\n",
909 if (skb
->len
> MAX_DFRAME_LEN_L1
|| skb
->len
> L1OIP_MAX_LEN
) {
910 printk(KERN_WARNING
"%s: skb too large\n",
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
);
925 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
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);
932 if (test_bit(FLG_ACTIVE
, &dch
->Flags
))
933 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
935 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
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
,
943 if (test_bit(FLG_ACTIVE
, &dch
->Flags
))
944 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
946 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
955 channel_dctrl(struct dchannel
*dch
, struct mISDN_ctrl_req
*cq
)
958 struct l1oip
*hc
= dch
->hw
;
961 case MISDN_CTRL_GETOP
:
962 cq
->op
= MISDN_CTRL_SETPEER
| MISDN_CTRL_UNSETPEER
963 | MISDN_CTRL_GETPEER
;
965 case MISDN_CTRL_SETPEER
:
966 hc
->remoteip
= (u32
)cq
->p1
;
967 hc
->remoteport
= cq
->p2
& 0xffff;
968 hc
->localport
= cq
->p2
>> 16;
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
);
976 case MISDN_CTRL_UNSETPEER
:
977 if (debug
& DEBUG_L1OIP_SOCKET
)
978 printk(KERN_DEBUG
"%s: removing ip address.\n",
981 l1oip_socket_open(hc
);
983 case MISDN_CTRL_GETPEER
:
984 if (debug
& DEBUG_L1OIP_SOCKET
)
985 printk(KERN_DEBUG
"%s: getting ip address.\n",
987 cq
->p1
= hc
->remoteip
;
988 cq
->p2
= hc
->remoteport
| (hc
->localport
<< 16);
991 printk(KERN_WARNING
"%s: unknown Op %x\n",
1000 open_dchannel(struct l1oip
*hc
, struct dchannel
*dch
, struct channel_req
*rq
)
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
)
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
);
1013 if (dch
->dev
.D
.protocol
!= rq
->protocol
)
1014 dch
->dev
.D
.protocol
= rq
->protocol
;
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
);
1020 rq
->ch
= &dch
->dev
.D
;
1021 if (!try_module_get(THIS_MODULE
))
1022 printk(KERN_WARNING
"%s:cannot get module\n", __func__
);
1027 open_bchannel(struct l1oip
*hc
, struct dchannel
*dch
, struct channel_req
*rq
)
1029 struct bchannel
*bch
;
1032 if (!test_channelmap(rq
->adr
.channel
, dch
->dev
.channelmap
))
1034 if (rq
->protocol
== ISDN_P_NONE
)
1036 ch
= rq
->adr
.channel
; /* BRI: 1=B1 2=B2 PRI: 1..15,17.. */
1037 bch
= hc
->chan
[ch
].bch
;
1039 printk(KERN_ERR
"%s:internal error ch %d has no bch\n",
1043 if (test_and_set_bit(FLG_OPEN
, &bch
->Flags
))
1044 return -EBUSY
; /* b-channel can be only open once */
1045 bch
->ch
.protocol
= rq
->protocol
;
1047 if (!try_module_get(THIS_MODULE
))
1048 printk(KERN_WARNING
"%s:cannot get module\n", __func__
);
1053 l1oip_dctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
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
;
1061 if (dch
->debug
& DEBUG_HW
)
1062 printk(KERN_DEBUG
"%s: cmd:%x %p\n",
1063 __func__
, cmd
, arg
);
1067 switch (rq
->protocol
) {
1074 err
= open_dchannel(hc
, dch
, rq
);
1082 err
= open_dchannel(hc
, dch
, rq
);
1085 err
= open_bchannel(hc
, dch
, rq
);
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
);
1095 case CONTROL_CHANNEL
:
1096 err
= channel_dctrl(dch
, arg
);
1099 if (dch
->debug
& DEBUG_HW
)
1100 printk(KERN_DEBUG
"%s: unknown command %x\n",
1108 handle_bmsg(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
1110 struct bchannel
*bch
= container_of(ch
, struct bchannel
, ch
);
1111 struct l1oip
*hc
= bch
->hw
;
1113 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
1119 if (skb
->len
<= 0) {
1120 printk(KERN_WARNING
"%s: skb too small\n",
1124 if (skb
->len
> MAX_DFRAME_LEN_L1
|| skb
->len
> L1OIP_MAX_LEN
) {
1125 printk(KERN_WARNING
"%s: skb too large\n",
1129 /* check for AIS / ulaw-silence */
1132 for (i
= 0; i
< l
; i
++) {
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
;
1142 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
1145 /* check for silence */
1148 for (i
= 0; i
< l
; i
++) {
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
;
1158 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
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
;
1174 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
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
);
1183 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
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
,
1190 test_and_clear_bit(FLG_ACTIVE
, &bch
->Flags
);
1192 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
1201 channel_bctrl(struct bchannel
*bch
, struct mISDN_ctrl_req
*cq
)
1204 struct dsp_features
*features
=
1205 (struct dsp_features
*)(*((u_long
*)&cq
->p1
));
1208 case MISDN_CTRL_GETOP
:
1209 cq
->op
= MISDN_CTRL_HW_FEATURES_OP
;
1211 case MISDN_CTRL_HW_FEATURES
: /* fill features structure */
1212 if (debug
& DEBUG_L1OIP_MSG
)
1213 printk(KERN_DEBUG
"%s: HW_FEATURE request\n",
1215 /* create confirm */
1216 features
->unclocked
= 1;
1217 features
->unordered
= 1;
1220 printk(KERN_WARNING
"%s: unknown Op %x\n",
1229 l1oip_bctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
1231 struct bchannel
*bch
= container_of(ch
, struct bchannel
, ch
);
1234 if (bch
->debug
& DEBUG_HW
)
1235 printk(KERN_DEBUG
"%s: cmd:%x %p\n",
1236 __func__
, cmd
, arg
);
1239 test_and_clear_bit(FLG_OPEN
, &bch
->Flags
);
1240 test_and_clear_bit(FLG_ACTIVE
, &bch
->Flags
);
1241 ch
->protocol
= ISDN_P_NONE
;
1243 module_put(THIS_MODULE
);
1246 case CONTROL_CHANNEL
:
1247 err
= channel_bctrl(bch
, arg
);
1250 printk(KERN_WARNING
"%s: unknown prim(%x)\n",
1258 * cleanup module and stack
1261 release_card(struct l1oip
*hc
)
1265 if (timer_pending(&hc
->keep_tl
))
1266 del_timer(&hc
->keep_tl
);
1268 if (timer_pending(&hc
->timeout_tl
))
1269 del_timer(&hc
->timeout_tl
);
1271 if (hc
->socket_thread
)
1272 l1oip_socket_close(hc
);
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
);
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
);
1291 spin_lock(&l1oip_lock
);
1292 list_del(&hc
->list
);
1293 spin_unlock(&l1oip_lock
);
1301 struct l1oip
*hc
, *next
;
1303 list_for_each_entry_safe(hc
, next
, &l1oip_ilist
, list
)
1311 * module and stack init
1314 init_card(struct l1oip
*hc
, int pri
, int bundle
)
1316 struct dchannel
*dch
;
1317 struct bchannel
*bch
;
1321 spin_lock_init(&hc
->socket_lock
);
1322 hc
->idx
= l1oip_cnt
;
1324 hc
->d_idx
= pri
? 16 : 3;
1325 hc
->b_num
= pri
? 30 : 2;
1326 hc
->bundle
= bundle
;
1328 sprintf(hc
->name
, "l1oip-e1.%d", l1oip_cnt
+ 1);
1330 sprintf(hc
->name
, "l1oip-s0.%d", l1oip_cnt
+ 1);
1332 switch (codec
[l1oip_cnt
]) {
1339 printk(KERN_ERR
"Codec(%d) not supported.\n",
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
);
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");
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
);
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__
);
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 "
1371 if (pri
&& hc
->b_num
> 126) {
1372 printk(KERN_ERR
"Maximum limit for PRI interface is 126 "
1376 if (pri
&& hc
->b_num
> 30) {
1377 printk(KERN_WARNING
"Maximum limit for BRI interface is 30 "
1379 printk(KERN_WARNING
"Your selection of %d channels must be "
1380 "supported by application.\n", hc
->limit
);
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
];
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
);
1400 dch
= kzalloc(sizeof(struct dchannel
), GFP_KERNEL
);
1404 mISDN_initdchannel(dch
, MAX_DFRAME_LEN_L1
, NULL
);
1407 dch
->dev
.Dprotocols
= (1 << ISDN_P_TE_E1
) | (1 << ISDN_P_NT_E1
);
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
;
1418 for (ch
= 0; ch
< dch
->dev
.nrbchan
; ch
++) {
1421 bch
= kzalloc(sizeof(struct bchannel
), GFP_KERNEL
);
1423 printk(KERN_ERR
"%s: no memory for bchannel\n",
1430 mISDN_initbchannel(bch
, MAX_DATA_MEM
);
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
);
1439 /* TODO: create a parent device for this driver */
1440 ret
= mISDN_register_device(&dch
->dev
, NULL
, hc
->name
);
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
);
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
; /* two seconds first time */
1456 add_timer(&hc
->keep_tl
);
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; /* state that we have timer off */
1473 printk(KERN_INFO
"mISDN: Layer-1-over-IP driver Rev. %s\n",
1476 INIT_LIST_HEAD(&l1oip_ilist
);
1477 spin_lock_init(&l1oip_lock
);
1479 if (l1oip_4bit_alloc(ulaw
))
1483 while (l1oip_cnt
< MAX_CARDS
&& type
[l1oip_cnt
]) {
1484 switch (type
[l1oip_cnt
] & 0xff) {
1502 printk(KERN_ERR
"Card type(%d) not supported.\n",
1503 type
[l1oip_cnt
] & 0xff);
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");
1514 hc
= kzalloc(sizeof(struct l1oip
), GFP_ATOMIC
);
1516 printk(KERN_ERR
"No kmem for L1-over-IP driver.\n");
1520 INIT_WORK(&hc
->workq
, (void *)l1oip_send_bh
);
1522 spin_lock(&l1oip_lock
);
1523 list_add_tail(&hc
->list
, &l1oip_ilist
);
1524 spin_unlock(&l1oip_lock
);
1526 ret
= init_card(hc
, pri
, bundle
);
1534 printk(KERN_INFO
"%d virtual devices registered\n", l1oip_cnt
);
1538 module_init(l1oip_init
);
1539 module_exit(l1oip_cleanup
);