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 around 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 useful 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 <linux/slab.h>
237 #include <net/sock.h>
241 static const char *l1oip_revision
= "2.00";
243 static int l1oip_cnt
;
244 static spinlock_t l1oip_lock
;
245 static struct list_head l1oip_ilist
;
248 static u_int type
[MAX_CARDS
];
249 static u_int codec
[MAX_CARDS
];
250 static u_int ip
[MAX_CARDS
*4];
251 static u_int port
[MAX_CARDS
];
252 static u_int remoteport
[MAX_CARDS
];
253 static u_int ondemand
[MAX_CARDS
];
254 static u_int limit
[MAX_CARDS
];
255 static u_int id
[MAX_CARDS
];
259 MODULE_AUTHOR("Andreas Eversberg");
260 MODULE_LICENSE("GPL");
261 module_param_array(type
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
262 module_param_array(codec
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
263 module_param_array(ip
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
264 module_param_array(port
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
265 module_param_array(remoteport
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
266 module_param_array(ondemand
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
267 module_param_array(limit
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
268 module_param_array(id
, uint
, NULL
, S_IRUGO
| S_IWUSR
);
269 module_param(ulaw
, uint
, S_IRUGO
| S_IWUSR
);
270 module_param(debug
, uint
, S_IRUGO
| S_IWUSR
);
273 * send a frame via socket, if open and restart timer
276 l1oip_socket_send(struct l1oip
*hc
, u8 localcodec
, u8 channel
, u32 chanmask
,
277 u16 timebase
, u8
*buf
, int len
)
282 struct socket
*socket
= NULL
;
284 if (debug
& DEBUG_L1OIP_MSG
)
285 printk(KERN_DEBUG
"%s: sending data to socket (len = %d)\n",
291 if ((int)(hc
->keep_tl
.expires
-jiffies
) < 5*HZ
) {
292 del_timer(&hc
->keep_tl
);
293 hc
->keep_tl
.expires
= jiffies
+ L1OIP_KEEPALIVE
*HZ
;
294 add_timer(&hc
->keep_tl
);
296 hc
->keep_tl
.expires
= jiffies
+ L1OIP_KEEPALIVE
*HZ
;
298 if (debug
& DEBUG_L1OIP_MSG
)
299 printk(KERN_DEBUG
"%s: resetting timer\n", __func__
);
301 /* drop if we have no remote ip or port */
302 if (!hc
->sin_remote
.sin_addr
.s_addr
|| !hc
->sin_remote
.sin_port
) {
303 if (debug
& DEBUG_L1OIP_MSG
)
304 printk(KERN_DEBUG
"%s: dropping frame, because remote "
305 "IP is not set.\n", __func__
);
310 *p
++ = (L1OIP_VERSION
<<6) /* version and coding */
311 | (hc
->pri
? 0x20 : 0x00) /* type */
312 | (hc
->id
? 0x10 : 0x00) /* id */
315 *p
++ = hc
->id
>>24; /* id */
320 *p
++ = (multi
== 1) ? 0x80 : 0x00 + channel
; /* m-flag, channel */
322 *p
++ = len
; /* length */
323 *p
++ = timebase
>>8; /* time base */
326 if (buf
&& len
) { /* add data to frame */
327 if (localcodec
== 1 && ulaw
)
328 l1oip_ulaw_to_alaw(buf
, len
, p
);
329 else if (localcodec
== 2 && !ulaw
)
330 l1oip_alaw_to_ulaw(buf
, len
, p
);
331 else if (localcodec
== 3)
332 len
= l1oip_law_to_4bit(buf
, len
, p
,
333 &hc
->chan
[channel
].codecstate
);
339 /* check for socket in safe condition */
340 spin_lock(&hc
->socket_lock
);
342 spin_unlock(&hc
->socket_lock
);
348 spin_unlock(&hc
->socket_lock
);
350 if (debug
& DEBUG_L1OIP_MSG
)
351 printk(KERN_DEBUG
"%s: sending packet to socket (len "
352 "= %d)\n", __func__
, len
);
353 hc
->sendiov
.iov_base
= frame
;
354 hc
->sendiov
.iov_len
= len
;
355 len
= kernel_sendmsg(socket
, &hc
->sendmsg
, &hc
->sendiov
, 1, len
);
356 /* give socket back */
357 hc
->socket
= socket
; /* no locking required */
364 * receive channel data from socket
367 l1oip_socket_recv(struct l1oip
*hc
, u8 remotecodec
, u8 channel
, u16 timebase
,
370 struct sk_buff
*nskb
;
371 struct bchannel
*bch
;
372 struct dchannel
*dch
;
377 if (debug
& DEBUG_L1OIP_MSG
)
378 printk(KERN_DEBUG
"%s: received empty keepalive data, "
379 "ignoring\n", __func__
);
383 if (debug
& DEBUG_L1OIP_MSG
)
384 printk(KERN_DEBUG
"%s: received data, sending to mISDN (%d)\n",
387 if (channel
< 1 || channel
> 127) {
388 printk(KERN_WARNING
"%s: packet error - channel %d out of "
389 "range\n", __func__
, channel
);
392 dch
= hc
->chan
[channel
].dch
;
393 bch
= hc
->chan
[channel
].bch
;
395 printk(KERN_WARNING
"%s: packet error - channel %d not in "
396 "stack\n", __func__
, channel
);
400 /* prepare message */
401 nskb
= mI_alloc_skb((remotecodec
== 3) ? (len
<<1) : len
, GFP_ATOMIC
);
403 printk(KERN_ERR
"%s: No mem for skb.\n", __func__
);
406 p
= skb_put(nskb
, (remotecodec
== 3) ? (len
<<1) : len
);
408 if (remotecodec
== 1 && ulaw
)
409 l1oip_alaw_to_ulaw(buf
, len
, p
);
410 else if (remotecodec
== 2 && !ulaw
)
411 l1oip_ulaw_to_alaw(buf
, len
, p
);
412 else if (remotecodec
== 3)
413 len
= l1oip_4bit_to_law(buf
, len
, p
);
417 /* send message up */
418 if (dch
&& len
>= 2) {
423 /* expand 16 bit sequence number to 32 bit sequence number */
424 rx_counter
= hc
->chan
[channel
].rx_counter
;
425 if (((s16
)(timebase
- rx_counter
)) >= 0) {
426 /* time has changed forward */
427 if (timebase
>= (rx_counter
& 0xffff))
429 (rx_counter
& 0xffff0000) | timebase
;
431 rx_counter
= ((rx_counter
& 0xffff0000)+0x10000)
434 /* time has changed backwards */
435 if (timebase
< (rx_counter
& 0xffff))
437 (rx_counter
& 0xffff0000) | timebase
;
439 rx_counter
= ((rx_counter
& 0xffff0000)-0x10000)
442 hc
->chan
[channel
].rx_counter
= rx_counter
;
445 if (hc
->chan
[channel
].disorder_flag
) {
448 skb
= hc
->chan
[channel
].disorder_skb
;
449 hc
->chan
[channel
].disorder_skb
= nskb
;
451 cnt
= hc
->chan
[channel
].disorder_cnt
;
452 hc
->chan
[channel
].disorder_cnt
= rx_counter
;
455 hc
->chan
[channel
].disorder_flag
^= 1;
458 queue_ch_frame(&bch
->ch
, PH_DATA_IND
, rx_counter
, nskb
);
464 * parse frame and extract channel data
467 l1oip_socket_parse(struct l1oip
*hc
, struct sockaddr_in
*sin
, u8
*buf
, int len
)
474 int len_start
= len
; /* initial frame length */
475 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
477 if (debug
& DEBUG_L1OIP_MSG
)
478 printk(KERN_DEBUG
"%s: received frame, parsing... (%d)\n",
483 printk(KERN_WARNING
"%s: packet error - length %d below "
484 "4 bytes\n", __func__
, len
);
489 if (((*buf
)>>6) != L1OIP_VERSION
) {
490 printk(KERN_WARNING
"%s: packet error - unknown version %d\n",
491 __func__
, buf
[0]>>6);
496 if (((*buf
)&0x20) && !hc
->pri
) {
497 printk(KERN_WARNING
"%s: packet error - received E1 packet "
498 "on S0 interface\n", __func__
);
501 if (!((*buf
)&0x20) && hc
->pri
) {
502 printk(KERN_WARNING
"%s: packet error - received S0 packet "
503 "on E1 interface\n", __func__
);
508 packet_id
= (*buf
>>4)&1;
511 remotecodec
= (*buf
) & 0x0f;
512 if (remotecodec
> 3) {
513 printk(KERN_WARNING
"%s: packet error - remotecodec %d "
514 "unsupported\n", __func__
, remotecodec
);
520 /* check packet_id */
523 printk(KERN_WARNING
"%s: packet error - packet has id "
524 "0x%x, but we have not\n", __func__
, packet_id
);
528 printk(KERN_WARNING
"%s: packet error - packet too "
529 "short for ID value\n", __func__
);
532 packet_id
= (*buf
++) << 24;
533 packet_id
+= (*buf
++) << 16;
534 packet_id
+= (*buf
++) << 8;
535 packet_id
+= (*buf
++);
538 if (packet_id
!= hc
->id
) {
539 printk(KERN_WARNING
"%s: packet error - ID mismatch, "
540 "got 0x%x, we 0x%x\n",
541 __func__
, packet_id
, hc
->id
);
546 printk(KERN_WARNING
"%s: packet error - packet has no "
547 "ID, but we have\n", __func__
);
554 printk(KERN_WARNING
"%s: packet error - packet too short, "
555 "channel expected at position %d.\n",
556 __func__
, len
-len_start
+1);
560 /* get channel and multiframe flag */
566 /* check length on multiframe */
569 printk(KERN_WARNING
"%s: packet error - packet too "
570 "short, length expected at position %d.\n",
571 __func__
, len_start
-len
-1);
580 printk(KERN_WARNING
"%s: packet error - length %d at "
581 "position %d exceeds total length %d.\n",
582 __func__
, mlen
, len_start
-len
-1, len_start
);
586 printk(KERN_WARNING
"%s: packet error - length %d at "
587 "position %d will not allow additional "
589 __func__
, mlen
, len_start
-len
+1);
593 mlen
= len
-2; /* single frame, subtract timebase */
596 printk(KERN_WARNING
"%s: packet error - packet too short, time "
597 "base expected at position %d.\n",
598 __func__
, len
-len_start
+1);
603 timebase
= (*buf
++) << 8;
604 timebase
|= (*buf
++);
607 /* if inactive, we send up a PH_ACTIVATE and activate */
608 if (!test_bit(FLG_ACTIVE
, &dch
->Flags
)) {
609 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
610 printk(KERN_DEBUG
"%s: interface become active due to "
611 "received packet\n", __func__
);
612 test_and_set_bit(FLG_ACTIVE
, &dch
->Flags
);
613 _queue_data(&dch
->dev
.D
, PH_ACTIVATE_IND
, MISDN_ID_ANY
, 0,
617 /* distribute packet */
618 l1oip_socket_recv(hc
, remotecodec
, channel
, timebase
, buf
, mlen
);
627 if ((int)(hc
->timeout_tl
.expires
-jiffies
) < 5*HZ
|| !hc
->timeout_on
) {
629 del_timer(&hc
->timeout_tl
);
630 hc
->timeout_tl
.expires
= jiffies
+ L1OIP_TIMEOUT
*HZ
;
631 add_timer(&hc
->timeout_tl
);
632 } else /* only adjust timer */
633 hc
->timeout_tl
.expires
= jiffies
+ L1OIP_TIMEOUT
*HZ
;
635 /* if ip or source port changes */
636 if ((hc
->sin_remote
.sin_addr
.s_addr
!= sin
->sin_addr
.s_addr
)
637 || (hc
->sin_remote
.sin_port
!= sin
->sin_port
)) {
638 if (debug
& DEBUG_L1OIP_SOCKET
)
639 printk(KERN_DEBUG
"%s: remote address changes from "
640 "0x%08x to 0x%08x (port %d to %d)\n", __func__
,
641 ntohl(hc
->sin_remote
.sin_addr
.s_addr
),
642 ntohl(sin
->sin_addr
.s_addr
),
643 ntohs(hc
->sin_remote
.sin_port
),
644 ntohs(sin
->sin_port
));
645 hc
->sin_remote
.sin_addr
.s_addr
= sin
->sin_addr
.s_addr
;
646 hc
->sin_remote
.sin_port
= sin
->sin_port
;
655 l1oip_socket_thread(void *data
)
657 struct l1oip
*hc
= (struct l1oip
*)data
;
660 struct sockaddr_in sin_rx
;
661 unsigned char *recvbuf
;
662 size_t recvbuf_size
= 1500;
664 struct socket
*socket
= NULL
;
665 DECLARE_COMPLETION_ONSTACK(wait
);
667 /* allocate buffer memory */
668 recvbuf
= kmalloc(recvbuf_size
, GFP_KERNEL
);
670 printk(KERN_ERR
"%s: Failed to alloc recvbuf.\n", __func__
);
676 allow_signal(SIGTERM
);
679 if (sock_create(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &socket
)) {
680 printk(KERN_ERR
"%s: Failed to create socket.\n", __func__
);
685 /* set incoming address */
686 hc
->sin_local
.sin_family
= AF_INET
;
687 hc
->sin_local
.sin_addr
.s_addr
= INADDR_ANY
;
688 hc
->sin_local
.sin_port
= htons((unsigned short)hc
->localport
);
690 /* set outgoing address */
691 hc
->sin_remote
.sin_family
= AF_INET
;
692 hc
->sin_remote
.sin_addr
.s_addr
= htonl(hc
->remoteip
);
693 hc
->sin_remote
.sin_port
= htons((unsigned short)hc
->remoteport
);
695 /* bind to incomming port */
696 if (socket
->ops
->bind(socket
, (struct sockaddr
*)&hc
->sin_local
,
697 sizeof(hc
->sin_local
))) {
698 printk(KERN_ERR
"%s: Failed to bind socket to port %d.\n",
699 __func__
, hc
->localport
);
705 if (socket
->sk
== NULL
) {
706 printk(KERN_ERR
"%s: socket->sk == NULL\n", __func__
);
711 /* build receive message */
712 msg
.msg_name
= &sin_rx
;
713 msg
.msg_namelen
= sizeof(sin_rx
);
714 msg
.msg_control
= NULL
;
715 msg
.msg_controllen
= 0;
717 /* build send message */
718 hc
->sendmsg
.msg_name
= &hc
->sin_remote
;
719 hc
->sendmsg
.msg_namelen
= sizeof(hc
->sin_remote
);
720 hc
->sendmsg
.msg_control
= NULL
;
721 hc
->sendmsg
.msg_controllen
= 0;
723 /* give away socket */
724 spin_lock(&hc
->socket_lock
);
726 spin_unlock(&hc
->socket_lock
);
729 if (debug
& DEBUG_L1OIP_SOCKET
)
730 printk(KERN_DEBUG
"%s: socket created and open\n",
732 while (!signal_pending(current
)) {
735 .iov_len
= recvbuf_size
,
737 recvlen
= kernel_recvmsg(socket
, &msg
, &iov
, 1,
740 l1oip_socket_parse(hc
, &sin_rx
, recvbuf
, recvlen
);
742 if (debug
& DEBUG_L1OIP_SOCKET
)
744 "%s: broken pipe on socket\n", __func__
);
748 /* get socket back, check first if in use, maybe by send function */
749 spin_lock(&hc
->socket_lock
);
750 /* if hc->socket is NULL, it is in use until it is given back */
751 while (!hc
->socket
) {
752 spin_unlock(&hc
->socket_lock
);
753 schedule_timeout(HZ
/10);
754 spin_lock(&hc
->socket_lock
);
757 spin_unlock(&hc
->socket_lock
);
759 if (debug
& DEBUG_L1OIP_SOCKET
)
760 printk(KERN_DEBUG
"%s: socket thread terminating\n",
769 sock_release(socket
);
771 /* if we got killed, signal completion */
772 complete(&hc
->socket_complete
);
773 hc
->socket_thread
= NULL
; /* show termination of thread */
775 if (debug
& DEBUG_L1OIP_SOCKET
)
776 printk(KERN_DEBUG
"%s: socket thread terminated\n",
782 l1oip_socket_close(struct l1oip
*hc
)
784 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
787 if (hc
->socket_thread
) {
788 if (debug
& DEBUG_L1OIP_SOCKET
)
789 printk(KERN_DEBUG
"%s: socket thread exists, "
790 "killing...\n", __func__
);
791 send_sig(SIGTERM
, hc
->socket_thread
, 0);
792 wait_for_completion(&hc
->socket_complete
);
795 /* if active, we send up a PH_DEACTIVATE and deactivate */
796 if (test_bit(FLG_ACTIVE
, &dch
->Flags
)) {
797 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
798 printk(KERN_DEBUG
"%s: interface become deactivated "
799 "due to timeout\n", __func__
);
800 test_and_clear_bit(FLG_ACTIVE
, &dch
->Flags
);
801 _queue_data(&dch
->dev
.D
, PH_DEACTIVATE_IND
, MISDN_ID_ANY
, 0,
807 l1oip_socket_open(struct l1oip
*hc
)
809 /* in case of reopen, we need to close first */
810 l1oip_socket_close(hc
);
812 init_completion(&hc
->socket_complete
);
814 /* create receive process */
815 hc
->socket_thread
= kthread_run(l1oip_socket_thread
, hc
, "l1oip_%s",
817 if (IS_ERR(hc
->socket_thread
)) {
818 int err
= PTR_ERR(hc
->socket_thread
);
819 printk(KERN_ERR
"%s: Failed (%d) to create socket process.\n",
821 hc
->socket_thread
= NULL
;
822 sock_release(hc
->socket
);
825 if (debug
& DEBUG_L1OIP_SOCKET
)
826 printk(KERN_DEBUG
"%s: socket thread created\n", __func__
);
833 l1oip_send_bh(struct work_struct
*work
)
835 struct l1oip
*hc
= container_of(work
, struct l1oip
, workq
);
837 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
838 printk(KERN_DEBUG
"%s: keepalive timer expired, sending empty "
839 "frame on dchannel\n", __func__
);
841 /* send an empty l1oip frame at D-channel */
842 l1oip_socket_send(hc
, 0, hc
->d_idx
, 0, 0, NULL
, 0);
850 l1oip_keepalive(void *data
)
852 struct l1oip
*hc
= (struct l1oip
*)data
;
854 schedule_work(&hc
->workq
);
858 l1oip_timeout(void *data
)
860 struct l1oip
*hc
= (struct l1oip
*)data
;
861 struct dchannel
*dch
= hc
->chan
[hc
->d_idx
].dch
;
863 if (debug
& DEBUG_L1OIP_MSG
)
864 printk(KERN_DEBUG
"%s: timeout timer expired, turn layer one "
865 "down.\n", __func__
);
867 hc
->timeout_on
= 0; /* state that timer must be initialized next time */
869 /* if timeout, we send up a PH_DEACTIVATE and deactivate */
870 if (test_bit(FLG_ACTIVE
, &dch
->Flags
)) {
871 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
872 printk(KERN_DEBUG
"%s: interface become deactivated "
873 "due to timeout\n", __func__
);
874 test_and_clear_bit(FLG_ACTIVE
, &dch
->Flags
);
875 _queue_data(&dch
->dev
.D
, PH_DEACTIVATE_IND
, MISDN_ID_ANY
, 0,
879 /* if we have ondemand set, we remove ip address */
881 if (debug
& DEBUG_L1OIP_MSG
)
882 printk(KERN_DEBUG
"%s: on demand causes ip address to "
883 "be removed\n", __func__
);
884 hc
->sin_remote
.sin_addr
.s_addr
= 0;
893 handle_dmsg(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
895 struct mISDNdevice
*dev
= container_of(ch
, struct mISDNdevice
, D
);
896 struct dchannel
*dch
= container_of(dev
, struct dchannel
, dev
);
897 struct l1oip
*hc
= dch
->hw
;
898 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
906 printk(KERN_WARNING
"%s: skb too small\n",
910 if (skb
->len
> MAX_DFRAME_LEN_L1
|| skb
->len
> L1OIP_MAX_LEN
) {
911 printk(KERN_WARNING
"%s: skb too large\n",
919 ll
= (l
< L1OIP_MAX_PERFRAME
) ? l
: L1OIP_MAX_PERFRAME
;
920 l1oip_socket_send(hc
, 0, dch
->slot
, 0,
921 hc
->chan
[dch
->slot
].tx_counter
++, p
, ll
);
926 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
928 case PH_ACTIVATE_REQ
:
929 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
930 printk(KERN_DEBUG
"%s: PH_ACTIVATE channel %d (1..%d)\n"
931 , __func__
, dch
->slot
, hc
->b_num
+1);
933 if (test_bit(FLG_ACTIVE
, &dch
->Flags
))
934 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
936 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
938 case PH_DEACTIVATE_REQ
:
939 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
940 printk(KERN_DEBUG
"%s: PH_DEACTIVATE channel %d "
941 "(1..%d)\n", __func__
, dch
->slot
,
944 if (test_bit(FLG_ACTIVE
, &dch
->Flags
))
945 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
947 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
956 channel_dctrl(struct dchannel
*dch
, struct mISDN_ctrl_req
*cq
)
959 struct l1oip
*hc
= dch
->hw
;
962 case MISDN_CTRL_GETOP
:
963 cq
->op
= MISDN_CTRL_SETPEER
| MISDN_CTRL_UNSETPEER
964 | MISDN_CTRL_GETPEER
;
966 case MISDN_CTRL_SETPEER
:
967 hc
->remoteip
= (u32
)cq
->p1
;
968 hc
->remoteport
= cq
->p2
& 0xffff;
969 hc
->localport
= cq
->p2
>> 16;
971 hc
->remoteport
= hc
->localport
;
972 if (debug
& DEBUG_L1OIP_SOCKET
)
973 printk(KERN_DEBUG
"%s: got new ip address from user "
974 "space.\n", __func__
);
975 l1oip_socket_open(hc
);
977 case MISDN_CTRL_UNSETPEER
:
978 if (debug
& DEBUG_L1OIP_SOCKET
)
979 printk(KERN_DEBUG
"%s: removing ip address.\n",
982 l1oip_socket_open(hc
);
984 case MISDN_CTRL_GETPEER
:
985 if (debug
& DEBUG_L1OIP_SOCKET
)
986 printk(KERN_DEBUG
"%s: getting ip address.\n",
988 cq
->p1
= hc
->remoteip
;
989 cq
->p2
= hc
->remoteport
| (hc
->localport
<< 16);
992 printk(KERN_WARNING
"%s: unknown Op %x\n",
1001 open_dchannel(struct l1oip
*hc
, struct dchannel
*dch
, struct channel_req
*rq
)
1003 if (debug
& DEBUG_HW_OPEN
)
1004 printk(KERN_DEBUG
"%s: dev(%d) open from %p\n", __func__
,
1005 dch
->dev
.id
, __builtin_return_address(0));
1006 if (rq
->protocol
== ISDN_P_NONE
)
1008 if ((dch
->dev
.D
.protocol
!= ISDN_P_NONE
) &&
1009 (dch
->dev
.D
.protocol
!= rq
->protocol
)) {
1010 if (debug
& DEBUG_HW_OPEN
)
1011 printk(KERN_WARNING
"%s: change protocol %x to %x\n",
1012 __func__
, dch
->dev
.D
.protocol
, rq
->protocol
);
1014 if (dch
->dev
.D
.protocol
!= rq
->protocol
)
1015 dch
->dev
.D
.protocol
= rq
->protocol
;
1017 if (test_bit(FLG_ACTIVE
, &dch
->Flags
)) {
1018 _queue_data(&dch
->dev
.D
, PH_ACTIVATE_IND
, MISDN_ID_ANY
,
1019 0, NULL
, GFP_KERNEL
);
1021 rq
->ch
= &dch
->dev
.D
;
1022 if (!try_module_get(THIS_MODULE
))
1023 printk(KERN_WARNING
"%s:cannot get module\n", __func__
);
1028 open_bchannel(struct l1oip
*hc
, struct dchannel
*dch
, struct channel_req
*rq
)
1030 struct bchannel
*bch
;
1033 if (!test_channelmap(rq
->adr
.channel
, dch
->dev
.channelmap
))
1035 if (rq
->protocol
== ISDN_P_NONE
)
1037 ch
= rq
->adr
.channel
; /* BRI: 1=B1 2=B2 PRI: 1..15,17.. */
1038 bch
= hc
->chan
[ch
].bch
;
1040 printk(KERN_ERR
"%s:internal error ch %d has no bch\n",
1044 if (test_and_set_bit(FLG_OPEN
, &bch
->Flags
))
1045 return -EBUSY
; /* b-channel can be only open once */
1046 bch
->ch
.protocol
= rq
->protocol
;
1048 if (!try_module_get(THIS_MODULE
))
1049 printk(KERN_WARNING
"%s:cannot get module\n", __func__
);
1054 l1oip_dctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
1056 struct mISDNdevice
*dev
= container_of(ch
, struct mISDNdevice
, D
);
1057 struct dchannel
*dch
= container_of(dev
, struct dchannel
, dev
);
1058 struct l1oip
*hc
= dch
->hw
;
1059 struct channel_req
*rq
;
1062 if (dch
->debug
& DEBUG_HW
)
1063 printk(KERN_DEBUG
"%s: cmd:%x %p\n",
1064 __func__
, cmd
, arg
);
1068 switch (rq
->protocol
) {
1075 err
= open_dchannel(hc
, dch
, rq
);
1083 err
= open_dchannel(hc
, dch
, rq
);
1086 err
= open_bchannel(hc
, dch
, rq
);
1090 if (debug
& DEBUG_HW_OPEN
)
1091 printk(KERN_DEBUG
"%s: dev(%d) close from %p\n",
1092 __func__
, dch
->dev
.id
,
1093 __builtin_return_address(0));
1094 module_put(THIS_MODULE
);
1096 case CONTROL_CHANNEL
:
1097 err
= channel_dctrl(dch
, arg
);
1100 if (dch
->debug
& DEBUG_HW
)
1101 printk(KERN_DEBUG
"%s: unknown command %x\n",
1109 handle_bmsg(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
1111 struct bchannel
*bch
= container_of(ch
, struct bchannel
, ch
);
1112 struct l1oip
*hc
= bch
->hw
;
1114 struct mISDNhead
*hh
= mISDN_HEAD_P(skb
);
1120 if (skb
->len
<= 0) {
1121 printk(KERN_WARNING
"%s: skb too small\n",
1125 if (skb
->len
> MAX_DFRAME_LEN_L1
|| skb
->len
> L1OIP_MAX_LEN
) {
1126 printk(KERN_WARNING
"%s: skb too large\n",
1130 /* check for AIS / ulaw-silence */
1133 for (i
= 0; i
< l
; i
++) {
1138 if (debug
& DEBUG_L1OIP_MSG
)
1139 printk(KERN_DEBUG
"%s: got AIS, not sending, "
1140 "but counting\n", __func__
);
1141 hc
->chan
[bch
->slot
].tx_counter
+= l
;
1143 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
1146 /* check for silence */
1149 for (i
= 0; i
< l
; i
++) {
1154 if (debug
& DEBUG_L1OIP_MSG
)
1155 printk(KERN_DEBUG
"%s: got silence, not sending"
1156 ", but counting\n", __func__
);
1157 hc
->chan
[bch
->slot
].tx_counter
+= l
;
1159 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
1167 ll
= (l
< L1OIP_MAX_PERFRAME
) ? l
: L1OIP_MAX_PERFRAME
;
1168 l1oip_socket_send(hc
, hc
->codec
, bch
->slot
, 0,
1169 hc
->chan
[bch
->slot
].tx_counter
, p
, ll
);
1170 hc
->chan
[bch
->slot
].tx_counter
+= ll
;
1175 queue_ch_frame(ch
, PH_DATA_CNF
, hh
->id
, skb
);
1177 case PH_ACTIVATE_REQ
:
1178 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
1179 printk(KERN_DEBUG
"%s: PH_ACTIVATE channel %d (1..%d)\n"
1180 , __func__
, bch
->slot
, hc
->b_num
+1);
1181 hc
->chan
[bch
->slot
].codecstate
= 0;
1182 test_and_set_bit(FLG_ACTIVE
, &bch
->Flags
);
1184 queue_ch_frame(ch
, PH_ACTIVATE_IND
, hh
->id
, skb
);
1186 case PH_DEACTIVATE_REQ
:
1187 if (debug
& (DEBUG_L1OIP_MSG
|DEBUG_L1OIP_SOCKET
))
1188 printk(KERN_DEBUG
"%s: PH_DEACTIVATE channel %d "
1189 "(1..%d)\n", __func__
, bch
->slot
,
1191 test_and_clear_bit(FLG_ACTIVE
, &bch
->Flags
);
1193 queue_ch_frame(ch
, PH_DEACTIVATE_IND
, hh
->id
, skb
);
1202 channel_bctrl(struct bchannel
*bch
, struct mISDN_ctrl_req
*cq
)
1205 struct dsp_features
*features
=
1206 (struct dsp_features
*)(*((u_long
*)&cq
->p1
));
1209 case MISDN_CTRL_GETOP
:
1210 cq
->op
= MISDN_CTRL_HW_FEATURES_OP
;
1212 case MISDN_CTRL_HW_FEATURES
: /* fill features structure */
1213 if (debug
& DEBUG_L1OIP_MSG
)
1214 printk(KERN_DEBUG
"%s: HW_FEATURE request\n",
1216 /* create confirm */
1217 features
->unclocked
= 1;
1218 features
->unordered
= 1;
1221 printk(KERN_WARNING
"%s: unknown Op %x\n",
1230 l1oip_bctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
1232 struct bchannel
*bch
= container_of(ch
, struct bchannel
, ch
);
1235 if (bch
->debug
& DEBUG_HW
)
1236 printk(KERN_DEBUG
"%s: cmd:%x %p\n",
1237 __func__
, cmd
, arg
);
1240 test_and_clear_bit(FLG_OPEN
, &bch
->Flags
);
1241 test_and_clear_bit(FLG_ACTIVE
, &bch
->Flags
);
1242 ch
->protocol
= ISDN_P_NONE
;
1244 module_put(THIS_MODULE
);
1247 case CONTROL_CHANNEL
:
1248 err
= channel_bctrl(bch
, arg
);
1251 printk(KERN_WARNING
"%s: unknown prim(%x)\n",
1259 * cleanup module and stack
1262 release_card(struct l1oip
*hc
)
1266 if (timer_pending(&hc
->keep_tl
))
1267 del_timer(&hc
->keep_tl
);
1269 if (timer_pending(&hc
->timeout_tl
))
1270 del_timer(&hc
->timeout_tl
);
1272 cancel_work_sync(&hc
->workq
);
1274 if (hc
->socket_thread
)
1275 l1oip_socket_close(hc
);
1277 if (hc
->registered
&& hc
->chan
[hc
->d_idx
].dch
)
1278 mISDN_unregister_device(&hc
->chan
[hc
->d_idx
].dch
->dev
);
1279 for (ch
= 0; ch
< 128; ch
++) {
1280 if (hc
->chan
[ch
].dch
) {
1281 mISDN_freedchannel(hc
->chan
[ch
].dch
);
1282 kfree(hc
->chan
[ch
].dch
);
1284 if (hc
->chan
[ch
].bch
) {
1285 mISDN_freebchannel(hc
->chan
[ch
].bch
);
1286 kfree(hc
->chan
[ch
].bch
);
1287 #ifdef REORDER_DEBUG
1288 if (hc
->chan
[ch
].disorder_skb
)
1289 dev_kfree_skb(hc
->chan
[ch
].disorder_skb
);
1294 spin_lock(&l1oip_lock
);
1295 list_del(&hc
->list
);
1296 spin_unlock(&l1oip_lock
);
1304 struct l1oip
*hc
, *next
;
1306 list_for_each_entry_safe(hc
, next
, &l1oip_ilist
, list
)
1314 * module and stack init
1317 init_card(struct l1oip
*hc
, int pri
, int bundle
)
1319 struct dchannel
*dch
;
1320 struct bchannel
*bch
;
1324 spin_lock_init(&hc
->socket_lock
);
1325 hc
->idx
= l1oip_cnt
;
1327 hc
->d_idx
= pri
? 16 : 3;
1328 hc
->b_num
= pri
? 30 : 2;
1329 hc
->bundle
= bundle
;
1331 sprintf(hc
->name
, "l1oip-e1.%d", l1oip_cnt
+ 1);
1333 sprintf(hc
->name
, "l1oip-s0.%d", l1oip_cnt
+ 1);
1335 switch (codec
[l1oip_cnt
]) {
1342 printk(KERN_ERR
"Codec(%d) not supported.\n",
1346 hc
->codec
= codec
[l1oip_cnt
];
1347 if (debug
& DEBUG_L1OIP_INIT
)
1348 printk(KERN_DEBUG
"%s: using codec %d\n",
1349 __func__
, hc
->codec
);
1351 if (id
[l1oip_cnt
] == 0) {
1352 printk(KERN_WARNING
"Warning: No 'id' value given or "
1353 "0, this is highly unsecure. Please use 32 "
1354 "bit randmom number 0x...\n");
1356 hc
->id
= id
[l1oip_cnt
];
1357 if (debug
& DEBUG_L1OIP_INIT
)
1358 printk(KERN_DEBUG
"%s: using id 0x%x\n", __func__
, hc
->id
);
1360 hc
->ondemand
= ondemand
[l1oip_cnt
];
1361 if (hc
->ondemand
&& !hc
->id
) {
1362 printk(KERN_ERR
"%s: ondemand option only allowed in "
1363 "conjunction with non 0 ID\n", __func__
);
1367 if (limit
[l1oip_cnt
])
1368 hc
->b_num
= limit
[l1oip_cnt
];
1369 if (!pri
&& hc
->b_num
> 2) {
1370 printk(KERN_ERR
"Maximum limit for BRI interface is 2 "
1374 if (pri
&& hc
->b_num
> 126) {
1375 printk(KERN_ERR
"Maximum limit for PRI interface is 126 "
1379 if (pri
&& hc
->b_num
> 30) {
1380 printk(KERN_WARNING
"Maximum limit for BRI interface is 30 "
1382 printk(KERN_WARNING
"Your selection of %d channels must be "
1383 "supported by application.\n", hc
->limit
);
1386 hc
->remoteip
= ip
[l1oip_cnt
<<2] << 24
1387 | ip
[(l1oip_cnt
<<2)+1] << 16
1388 | ip
[(l1oip_cnt
<<2)+2] << 8
1389 | ip
[(l1oip_cnt
<<2)+3];
1390 hc
->localport
= port
[l1oip_cnt
]?:(L1OIP_DEFAULTPORT
+l1oip_cnt
);
1391 if (remoteport
[l1oip_cnt
])
1392 hc
->remoteport
= remoteport
[l1oip_cnt
];
1394 hc
->remoteport
= hc
->localport
;
1395 if (debug
& DEBUG_L1OIP_INIT
)
1396 printk(KERN_DEBUG
"%s: using local port %d remote ip "
1397 "%d.%d.%d.%d port %d ondemand %d\n", __func__
,
1398 hc
->localport
, hc
->remoteip
>> 24,
1399 (hc
->remoteip
>> 16) & 0xff,
1400 (hc
->remoteip
>> 8) & 0xff, hc
->remoteip
& 0xff,
1401 hc
->remoteport
, hc
->ondemand
);
1403 dch
= kzalloc(sizeof(struct dchannel
), GFP_KERNEL
);
1407 mISDN_initdchannel(dch
, MAX_DFRAME_LEN_L1
, NULL
);
1410 dch
->dev
.Dprotocols
= (1 << ISDN_P_TE_E1
) | (1 << ISDN_P_NT_E1
);
1412 dch
->dev
.Dprotocols
= (1 << ISDN_P_TE_S0
) | (1 << ISDN_P_NT_S0
);
1413 dch
->dev
.Bprotocols
= (1 << (ISDN_P_B_RAW
& ISDN_P_B_MASK
)) |
1414 (1 << (ISDN_P_B_HDLC
& ISDN_P_B_MASK
));
1415 dch
->dev
.D
.send
= handle_dmsg
;
1416 dch
->dev
.D
.ctrl
= l1oip_dctrl
;
1417 dch
->dev
.nrbchan
= hc
->b_num
;
1418 dch
->slot
= hc
->d_idx
;
1419 hc
->chan
[hc
->d_idx
].dch
= dch
;
1421 for (ch
= 0; ch
< dch
->dev
.nrbchan
; ch
++) {
1424 bch
= kzalloc(sizeof(struct bchannel
), GFP_KERNEL
);
1426 printk(KERN_ERR
"%s: no memory for bchannel\n",
1433 mISDN_initbchannel(bch
, MAX_DATA_MEM
);
1435 bch
->ch
.send
= handle_bmsg
;
1436 bch
->ch
.ctrl
= l1oip_bctrl
;
1437 bch
->ch
.nr
= i
+ ch
;
1438 list_add(&bch
->ch
.list
, &dch
->dev
.bchannels
);
1439 hc
->chan
[i
+ ch
].bch
= bch
;
1440 set_channelmap(bch
->nr
, dch
->dev
.channelmap
);
1442 /* TODO: create a parent device for this driver */
1443 ret
= mISDN_register_device(&dch
->dev
, NULL
, hc
->name
);
1448 if (debug
& DEBUG_L1OIP_INIT
)
1449 printk(KERN_DEBUG
"%s: Setting up network card(%d)\n",
1450 __func__
, l1oip_cnt
+ 1);
1451 ret
= l1oip_socket_open(hc
);
1455 hc
->keep_tl
.function
= (void *)l1oip_keepalive
;
1456 hc
->keep_tl
.data
= (ulong
)hc
;
1457 init_timer(&hc
->keep_tl
);
1458 hc
->keep_tl
.expires
= jiffies
+ 2*HZ
; /* two seconds first time */
1459 add_timer(&hc
->keep_tl
);
1461 hc
->timeout_tl
.function
= (void *)l1oip_timeout
;
1462 hc
->timeout_tl
.data
= (ulong
)hc
;
1463 init_timer(&hc
->timeout_tl
);
1464 hc
->timeout_on
= 0; /* state that we have timer off */
1476 printk(KERN_INFO
"mISDN: Layer-1-over-IP driver Rev. %s\n",
1479 INIT_LIST_HEAD(&l1oip_ilist
);
1480 spin_lock_init(&l1oip_lock
);
1482 if (l1oip_4bit_alloc(ulaw
))
1486 while (l1oip_cnt
< MAX_CARDS
&& type
[l1oip_cnt
]) {
1487 switch (type
[l1oip_cnt
] & 0xff) {
1505 printk(KERN_ERR
"Card type(%d) not supported.\n",
1506 type
[l1oip_cnt
] & 0xff);
1511 if (debug
& DEBUG_L1OIP_INIT
)
1512 printk(KERN_DEBUG
"%s: interface %d is %s with %s.\n",
1513 __func__
, l1oip_cnt
, pri
? "PRI" : "BRI",
1514 bundle
? "bundled IP packet for all B-channels" :
1515 "separate IP packets for every B-channel");
1517 hc
= kzalloc(sizeof(struct l1oip
), GFP_ATOMIC
);
1519 printk(KERN_ERR
"No kmem for L1-over-IP driver.\n");
1523 INIT_WORK(&hc
->workq
, (void *)l1oip_send_bh
);
1525 spin_lock(&l1oip_lock
);
1526 list_add_tail(&hc
->list
, &l1oip_ilist
);
1527 spin_unlock(&l1oip_lock
);
1529 ret
= init_card(hc
, pri
, bundle
);
1537 printk(KERN_INFO
"%d virtual devices registered\n", l1oip_cnt
);
1541 module_init(l1oip_init
);
1542 module_exit(l1oip_cleanup
);