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 <linux/sched/signal.h>
239 #include <net/sock.h>
243 static const char *l1oip_revision = "2.00";
245 static int l1oip_cnt;
246 static spinlock_t l1oip_lock;
247 static struct list_head l1oip_ilist;
250 static u_int type[MAX_CARDS];
251 static u_int codec[MAX_CARDS];
252 static u_int ip[MAX_CARDS * 4];
253 static u_int port[MAX_CARDS];
254 static u_int remoteport[MAX_CARDS];
255 static u_int ondemand[MAX_CARDS];
256 static u_int limit[MAX_CARDS];
257 static u_int id[MAX_CARDS];
261 MODULE_AUTHOR("Andreas Eversberg");
262 MODULE_LICENSE("GPL");
263 module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
264 module_param_array(codec, uint, NULL, S_IRUGO | S_IWUSR);
265 module_param_array(ip, uint, NULL, S_IRUGO | S_IWUSR);
266 module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
267 module_param_array(remoteport, uint, NULL, S_IRUGO | S_IWUSR);
268 module_param_array(ondemand, uint, NULL, S_IRUGO | S_IWUSR);
269 module_param_array(limit, uint, NULL, S_IRUGO | S_IWUSR);
270 module_param_array(id, uint, NULL, S_IRUGO | S_IWUSR);
271 module_param(ulaw, uint, S_IRUGO | S_IWUSR);
272 module_param(debug, uint, S_IRUGO | S_IWUSR);
275 * send a frame via socket, if open and restart timer
278 l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
279 u16 timebase, u8 *buf, int len)
282 u8 frame[MAX_DFRAME_LEN_L1 + 32];
283 struct socket *socket = NULL;
285 if (debug & DEBUG_L1OIP_MSG)
286 printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
292 if (time_before(hc->keep_tl.expires, jiffies + 5 * HZ) && !hc->shutdown)
293 mod_timer(&hc->keep_tl, jiffies + L1OIP_KEEPALIVE * HZ);
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++ = 0x00 + channel; /* m-flag, channel */
320 *p++ = timebase >> 8; /* time base */
323 if (buf && len) { /* add data to frame */
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);
336 /* check for socket in safe condition */
337 spin_lock(&hc->socket_lock);
339 spin_unlock(&hc->socket_lock);
345 spin_unlock(&hc->socket_lock);
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 /* give socket back */
354 hc->socket = socket; /* no locking required */
361 * receive channel data from socket
364 l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
367 struct sk_buff *nskb;
368 struct bchannel *bch;
369 struct dchannel *dch;
374 if (debug & DEBUG_L1OIP_MSG)
375 printk(KERN_DEBUG "%s: received empty keepalive data, "
376 "ignoring\n", __func__);
380 if (debug & DEBUG_L1OIP_MSG)
381 printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
384 if (channel < 1 || channel > 127) {
385 printk(KERN_WARNING "%s: packet error - channel %d out of "
386 "range\n", __func__, channel);
389 dch = hc->chan[channel].dch;
390 bch = hc->chan[channel].bch;
392 printk(KERN_WARNING "%s: packet error - channel %d not in "
393 "stack\n", __func__, channel);
397 /* prepare message */
398 nskb = mI_alloc_skb((remotecodec == 3) ? (len << 1) : len, GFP_ATOMIC);
400 printk(KERN_ERR "%s: No mem for skb.\n", __func__);
403 p = skb_put(nskb, (remotecodec == 3) ? (len << 1) : len);
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);
414 /* send message up */
415 if (dch && len >= 2) {
420 /* expand 16 bit sequence number to 32 bit sequence number */
421 rx_counter = hc->chan[channel].rx_counter;
422 if (((s16)(timebase - rx_counter)) >= 0) {
423 /* time has changed forward */
424 if (timebase >= (rx_counter & 0xffff))
426 (rx_counter & 0xffff0000) | timebase;
428 rx_counter = ((rx_counter & 0xffff0000) + 0x10000)
431 /* time has changed backwards */
432 if (timebase < (rx_counter & 0xffff))
434 (rx_counter & 0xffff0000) | timebase;
436 rx_counter = ((rx_counter & 0xffff0000) - 0x10000)
439 hc->chan[channel].rx_counter = rx_counter;
442 if (hc->chan[channel].disorder_flag) {
443 swap(hc->chan[channel].disorder_skb, nskb);
444 swap(hc->chan[channel].disorder_cnt, rx_counter);
446 hc->chan[channel].disorder_flag ^= 1;
449 queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
455 * parse frame and extract channel data
458 l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
465 int len_start = len; /* initial frame length */
466 struct dchannel *dch = hc->chan[hc->d_idx].dch;
468 if (debug & DEBUG_L1OIP_MSG)
469 printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
473 if (len < 1 + 1 + 2) {
474 printk(KERN_WARNING "%s: packet error - length %d below "
475 "4 bytes\n", __func__, len);
480 if (((*buf) >> 6) != L1OIP_VERSION) {
481 printk(KERN_WARNING "%s: packet error - unknown version %d\n",
482 __func__, buf[0]>>6);
487 if (((*buf) & 0x20) && !hc->pri) {
488 printk(KERN_WARNING "%s: packet error - received E1 packet "
489 "on S0 interface\n", __func__);
492 if (!((*buf) & 0x20) && hc->pri) {
493 printk(KERN_WARNING "%s: packet error - received S0 packet "
494 "on E1 interface\n", __func__);
499 packet_id = (*buf >> 4) & 1;
502 remotecodec = (*buf) & 0x0f;
503 if (remotecodec > 3) {
504 printk(KERN_WARNING "%s: packet error - remotecodec %d "
505 "unsupported\n", __func__, remotecodec);
511 /* check packet_id */
514 printk(KERN_WARNING "%s: packet error - packet has id "
515 "0x%x, but we have not\n", __func__, packet_id);
519 printk(KERN_WARNING "%s: packet error - packet too "
520 "short for ID value\n", __func__);
523 packet_id = (*buf++) << 24;
524 packet_id += (*buf++) << 16;
525 packet_id += (*buf++) << 8;
526 packet_id += (*buf++);
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);
537 printk(KERN_WARNING "%s: packet error - packet has no "
538 "ID, but we have\n", __func__);
545 printk(KERN_WARNING "%s: packet error - packet too short, "
546 "channel expected at position %d.\n",
547 __func__, len-len_start + 1);
551 /* get channel and multiframe flag */
552 channel = *buf & 0x7f;
557 /* check length on multiframe */
560 printk(KERN_WARNING "%s: packet error - packet too "
561 "short, length expected at position %d.\n",
562 __func__, len_start - len - 1);
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);
576 if (len == mlen + 3) {
577 printk(KERN_WARNING "%s: packet error - length %d at "
578 "position %d will not allow additional "
580 __func__, mlen, len_start-len + 1);
584 mlen = len - 2; /* single frame, subtract timebase */
587 printk(KERN_WARNING "%s: packet error - packet too short, time "
588 "base expected at position %d.\n",
589 __func__, len-len_start + 1);
594 timebase = (*buf++) << 8;
595 timebase |= (*buf++);
598 /* if inactive, we send up a PH_ACTIVATE and activate */
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,
608 /* distribute packet */
609 l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
618 if ((time_before(hc->timeout_tl.expires, jiffies + 5 * HZ) ||
622 mod_timer(&hc->timeout_tl, jiffies + L1OIP_TIMEOUT * HZ);
623 } else /* only adjust timer */
624 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
626 /* if ip or source port changes */
627 if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
628 || (hc->sin_remote.sin_port != sin->sin_port)) {
629 if (debug & DEBUG_L1OIP_SOCKET)
630 printk(KERN_DEBUG "%s: remote address changes from "
631 "0x%08x to 0x%08x (port %d to %d)\n", __func__,
632 ntohl(hc->sin_remote.sin_addr.s_addr),
633 ntohl(sin->sin_addr.s_addr),
634 ntohs(hc->sin_remote.sin_port),
635 ntohs(sin->sin_port));
636 hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
637 hc->sin_remote.sin_port = sin->sin_port;
646 l1oip_socket_thread(void *data)
648 struct l1oip *hc = (struct l1oip *)data;
650 struct sockaddr_in sin_rx;
652 struct msghdr msg = {.msg_name = &sin_rx,
653 .msg_namelen = sizeof(sin_rx)};
654 unsigned char *recvbuf;
655 size_t recvbuf_size = 1500;
657 struct socket *socket = NULL;
658 DECLARE_COMPLETION_ONSTACK(wait);
660 /* allocate buffer memory */
661 recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
663 printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
668 iov.iov_base = recvbuf;
669 iov.iov_len = recvbuf_size;
672 allow_signal(SIGTERM);
675 if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
676 printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
681 /* set incoming address */
682 hc->sin_local.sin_family = AF_INET;
683 hc->sin_local.sin_addr.s_addr = INADDR_ANY;
684 hc->sin_local.sin_port = htons((unsigned short)hc->localport);
686 /* set outgoing address */
687 hc->sin_remote.sin_family = AF_INET;
688 hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip);
689 hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport);
691 /* bind to incoming port */
692 if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
693 sizeof(hc->sin_local))) {
694 printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
695 __func__, hc->localport);
701 if (socket->sk == NULL) {
702 printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
707 /* build send message */
708 hc->sendmsg.msg_name = &hc->sin_remote;
709 hc->sendmsg.msg_namelen = sizeof(hc->sin_remote);
710 hc->sendmsg.msg_control = NULL;
711 hc->sendmsg.msg_controllen = 0;
713 /* give away socket */
714 spin_lock(&hc->socket_lock);
716 spin_unlock(&hc->socket_lock);
719 if (debug & DEBUG_L1OIP_SOCKET)
720 printk(KERN_DEBUG "%s: socket created and open\n",
722 while (!signal_pending(current)) {
723 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1,
725 recvlen = sock_recvmsg(socket, &msg, 0);
727 l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
729 if (debug & DEBUG_L1OIP_SOCKET)
731 "%s: broken pipe on socket\n", __func__);
735 /* get socket back, check first if in use, maybe by send function */
736 spin_lock(&hc->socket_lock);
737 /* if hc->socket is NULL, it is in use until it is given back */
738 while (!hc->socket) {
739 spin_unlock(&hc->socket_lock);
740 schedule_timeout(HZ / 10);
741 spin_lock(&hc->socket_lock);
744 spin_unlock(&hc->socket_lock);
746 if (debug & DEBUG_L1OIP_SOCKET)
747 printk(KERN_DEBUG "%s: socket thread terminating\n",
756 sock_release(socket);
758 /* if we got killed, signal completion */
759 complete(&hc->socket_complete);
760 hc->socket_thread = NULL; /* show termination of thread */
762 if (debug & DEBUG_L1OIP_SOCKET)
763 printk(KERN_DEBUG "%s: socket thread terminated\n",
769 l1oip_socket_close(struct l1oip *hc)
771 struct dchannel *dch = hc->chan[hc->d_idx].dch;
774 if (hc->socket_thread) {
775 if (debug & DEBUG_L1OIP_SOCKET)
776 printk(KERN_DEBUG "%s: socket thread exists, "
777 "killing...\n", __func__);
778 send_sig(SIGTERM, hc->socket_thread, 0);
779 wait_for_completion(&hc->socket_complete);
782 /* if active, we send up a PH_DEACTIVATE and deactivate */
783 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
784 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
785 printk(KERN_DEBUG "%s: interface become deactivated "
786 "due to timeout\n", __func__);
787 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
788 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
794 l1oip_socket_open(struct l1oip *hc)
796 /* in case of reopen, we need to close first */
797 l1oip_socket_close(hc);
799 init_completion(&hc->socket_complete);
801 /* create receive process */
802 hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
804 if (IS_ERR(hc->socket_thread)) {
805 int err = PTR_ERR(hc->socket_thread);
806 printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
808 hc->socket_thread = NULL;
809 sock_release(hc->socket);
812 if (debug & DEBUG_L1OIP_SOCKET)
813 printk(KERN_DEBUG "%s: socket thread created\n", __func__);
820 l1oip_send_bh(struct work_struct *work)
822 struct l1oip *hc = container_of(work, struct l1oip, workq);
824 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
825 printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
826 "frame on dchannel\n", __func__);
828 /* send an empty l1oip frame at D-channel */
829 l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
837 l1oip_keepalive(struct timer_list *t)
839 struct l1oip *hc = from_timer(hc, t, keep_tl);
841 schedule_work(&hc->workq);
845 l1oip_timeout(struct timer_list *t)
847 struct l1oip *hc = from_timer(hc, t,
849 struct dchannel *dch = hc->chan[hc->d_idx].dch;
851 if (debug & DEBUG_L1OIP_MSG)
852 printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
853 "down.\n", __func__);
855 hc->timeout_on = 0; /* state that timer must be initialized next time */
857 /* if timeout, we send up a PH_DEACTIVATE and deactivate */
858 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
859 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
860 printk(KERN_DEBUG "%s: interface become deactivated "
861 "due to timeout\n", __func__);
862 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
863 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
867 /* if we have ondemand set, we remove ip address */
869 if (debug & DEBUG_L1OIP_MSG)
870 printk(KERN_DEBUG "%s: on demand causes ip address to "
871 "be removed\n", __func__);
872 hc->sin_remote.sin_addr.s_addr = 0;
881 handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
883 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
884 struct dchannel *dch = container_of(dev, struct dchannel, dev);
885 struct l1oip *hc = dch->hw;
886 struct mISDNhead *hh = mISDN_HEAD_P(skb);
894 printk(KERN_WARNING "%s: skb too small\n",
898 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
899 printk(KERN_WARNING "%s: skb too large\n",
908 * This is technically bounded by L1OIP_MAX_PERFRAME but
909 * MAX_DFRAME_LEN_L1 < L1OIP_MAX_PERFRAME
911 ll = (l < MAX_DFRAME_LEN_L1) ? l : MAX_DFRAME_LEN_L1;
912 l1oip_socket_send(hc, 0, dch->slot, 0,
913 hc->chan[dch->slot].tx_counter++, p, ll);
918 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
920 case PH_ACTIVATE_REQ:
921 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
922 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
923 , __func__, dch->slot, hc->b_num + 1);
925 if (test_bit(FLG_ACTIVE, &dch->Flags))
926 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
928 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
930 case PH_DEACTIVATE_REQ:
931 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
932 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
933 "(1..%d)\n", __func__, dch->slot,
936 if (test_bit(FLG_ACTIVE, &dch->Flags))
937 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
939 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
948 channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
951 struct l1oip *hc = dch->hw;
954 case MISDN_CTRL_GETOP:
955 cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
956 | MISDN_CTRL_GETPEER;
958 case MISDN_CTRL_SETPEER:
959 hc->remoteip = (u32)cq->p1;
960 hc->remoteport = cq->p2 & 0xffff;
961 hc->localport = cq->p2 >> 16;
963 hc->remoteport = hc->localport;
964 if (debug & DEBUG_L1OIP_SOCKET)
965 printk(KERN_DEBUG "%s: got new ip address from user "
966 "space.\n", __func__);
967 l1oip_socket_open(hc);
969 case MISDN_CTRL_UNSETPEER:
970 if (debug & DEBUG_L1OIP_SOCKET)
971 printk(KERN_DEBUG "%s: removing ip address.\n",
974 l1oip_socket_open(hc);
976 case MISDN_CTRL_GETPEER:
977 if (debug & DEBUG_L1OIP_SOCKET)
978 printk(KERN_DEBUG "%s: getting ip address.\n",
980 cq->p1 = hc->remoteip;
981 cq->p2 = hc->remoteport | (hc->localport << 16);
984 printk(KERN_WARNING "%s: unknown Op %x\n",
993 open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
995 if (debug & DEBUG_HW_OPEN)
996 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
997 dch->dev.id, __builtin_return_address(0));
998 if (rq->protocol == ISDN_P_NONE)
1000 if ((dch->dev.D.protocol != ISDN_P_NONE) &&
1001 (dch->dev.D.protocol != rq->protocol)) {
1002 if (debug & DEBUG_HW_OPEN)
1003 printk(KERN_WARNING "%s: change protocol %x to %x\n",
1004 __func__, dch->dev.D.protocol, rq->protocol);
1006 if (dch->dev.D.protocol != rq->protocol)
1007 dch->dev.D.protocol = rq->protocol;
1009 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1010 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
1011 0, NULL, GFP_KERNEL);
1013 rq->ch = &dch->dev.D;
1014 if (!try_module_get(THIS_MODULE))
1015 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1020 open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1022 struct bchannel *bch;
1025 if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
1027 if (rq->protocol == ISDN_P_NONE)
1029 ch = rq->adr.channel; /* BRI: 1=B1 2=B2 PRI: 1..15,17.. */
1030 bch = hc->chan[ch].bch;
1032 printk(KERN_ERR "%s:internal error ch %d has no bch\n",
1036 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1037 return -EBUSY; /* b-channel can be only open once */
1038 bch->ch.protocol = rq->protocol;
1040 if (!try_module_get(THIS_MODULE))
1041 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1046 l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1048 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1049 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1050 struct l1oip *hc = dch->hw;
1051 struct channel_req *rq;
1054 if (dch->debug & DEBUG_HW)
1055 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1056 __func__, cmd, arg);
1060 switch (rq->protocol) {
1067 err = open_dchannel(hc, dch, rq);
1075 err = open_dchannel(hc, dch, rq);
1078 err = open_bchannel(hc, dch, rq);
1082 if (debug & DEBUG_HW_OPEN)
1083 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
1084 __func__, dch->dev.id,
1085 __builtin_return_address(0));
1086 module_put(THIS_MODULE);
1088 case CONTROL_CHANNEL:
1089 err = channel_dctrl(dch, arg);
1092 if (dch->debug & DEBUG_HW)
1093 printk(KERN_DEBUG "%s: unknown command %x\n",
1101 handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1103 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1104 struct l1oip *hc = bch->hw;
1106 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1112 if (skb->len <= 0) {
1113 printk(KERN_WARNING "%s: skb too small\n",
1117 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1118 printk(KERN_WARNING "%s: skb too large\n",
1122 /* check for AIS / ulaw-silence */
1124 if (!memchr_inv(skb->data, 0xff, l)) {
1125 if (debug & DEBUG_L1OIP_MSG)
1126 printk(KERN_DEBUG "%s: got AIS, not sending, "
1127 "but counting\n", __func__);
1128 hc->chan[bch->slot].tx_counter += l;
1130 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1133 /* check for silence */
1135 if (!memchr_inv(skb->data, 0x2a, l)) {
1136 if (debug & DEBUG_L1OIP_MSG)
1137 printk(KERN_DEBUG "%s: got silence, not sending"
1138 ", but counting\n", __func__);
1139 hc->chan[bch->slot].tx_counter += l;
1141 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1150 * This is technically bounded by L1OIP_MAX_PERFRAME but
1151 * MAX_DFRAME_LEN_L1 < L1OIP_MAX_PERFRAME
1153 ll = (l < MAX_DFRAME_LEN_L1) ? l : MAX_DFRAME_LEN_L1;
1154 l1oip_socket_send(hc, hc->codec, bch->slot, 0,
1155 hc->chan[bch->slot].tx_counter, p, ll);
1156 hc->chan[bch->slot].tx_counter += ll;
1161 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1163 case PH_ACTIVATE_REQ:
1164 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1165 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
1166 , __func__, bch->slot, hc->b_num + 1);
1167 hc->chan[bch->slot].codecstate = 0;
1168 test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1170 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1172 case PH_DEACTIVATE_REQ:
1173 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1174 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
1175 "(1..%d)\n", __func__, bch->slot,
1177 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1179 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1188 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1191 struct dsp_features *features =
1192 (struct dsp_features *)(*((u_long *)&cq->p1));
1195 case MISDN_CTRL_GETOP:
1196 cq->op = MISDN_CTRL_HW_FEATURES_OP;
1198 case MISDN_CTRL_HW_FEATURES: /* fill features structure */
1199 if (debug & DEBUG_L1OIP_MSG)
1200 printk(KERN_DEBUG "%s: HW_FEATURE request\n",
1202 /* create confirm */
1203 features->unclocked = 1;
1204 features->unordered = 1;
1207 printk(KERN_WARNING "%s: unknown Op %x\n",
1216 l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1218 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1221 if (bch->debug & DEBUG_HW)
1222 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1223 __func__, cmd, arg);
1226 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1227 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1228 ch->protocol = ISDN_P_NONE;
1230 module_put(THIS_MODULE);
1233 case CONTROL_CHANNEL:
1234 err = channel_bctrl(bch, arg);
1237 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1245 * cleanup module and stack
1248 release_card(struct l1oip *hc)
1252 hc->shutdown = true;
1254 del_timer_sync(&hc->keep_tl);
1255 del_timer_sync(&hc->timeout_tl);
1257 cancel_work_sync(&hc->workq);
1259 if (hc->socket_thread)
1260 l1oip_socket_close(hc);
1262 if (hc->registered && hc->chan[hc->d_idx].dch)
1263 mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1264 for (ch = 0; ch < 128; ch++) {
1265 if (hc->chan[ch].dch) {
1266 mISDN_freedchannel(hc->chan[ch].dch);
1267 kfree(hc->chan[ch].dch);
1269 if (hc->chan[ch].bch) {
1270 mISDN_freebchannel(hc->chan[ch].bch);
1271 kfree(hc->chan[ch].bch);
1272 #ifdef REORDER_DEBUG
1273 if (hc->chan[ch].disorder_skb)
1274 dev_kfree_skb(hc->chan[ch].disorder_skb);
1279 spin_lock(&l1oip_lock);
1280 list_del(&hc->list);
1281 spin_unlock(&l1oip_lock);
1289 struct l1oip *hc, *next;
1291 list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1299 * module and stack init
1302 init_card(struct l1oip *hc, int pri, int bundle)
1304 struct dchannel *dch;
1305 struct bchannel *bch;
1309 spin_lock_init(&hc->socket_lock);
1310 hc->idx = l1oip_cnt;
1312 hc->d_idx = pri ? 16 : 3;
1313 hc->b_num = pri ? 30 : 2;
1314 hc->bundle = bundle;
1316 sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1318 sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1320 switch (codec[l1oip_cnt]) {
1327 printk(KERN_ERR "Codec(%d) not supported.\n",
1331 hc->codec = codec[l1oip_cnt];
1332 if (debug & DEBUG_L1OIP_INIT)
1333 printk(KERN_DEBUG "%s: using codec %d\n",
1334 __func__, hc->codec);
1336 if (id[l1oip_cnt] == 0) {
1337 printk(KERN_WARNING "Warning: No 'id' value given or "
1338 "0, this is highly unsecure. Please use 32 "
1339 "bit random number 0x...\n");
1341 hc->id = id[l1oip_cnt];
1342 if (debug & DEBUG_L1OIP_INIT)
1343 printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1345 hc->ondemand = ondemand[l1oip_cnt];
1346 if (hc->ondemand && !hc->id) {
1347 printk(KERN_ERR "%s: ondemand option only allowed in "
1348 "conjunction with non 0 ID\n", __func__);
1352 if (limit[l1oip_cnt])
1353 hc->b_num = limit[l1oip_cnt];
1354 if (!pri && hc->b_num > 2) {
1355 printk(KERN_ERR "Maximum limit for BRI interface is 2 "
1359 if (pri && hc->b_num > 126) {
1360 printk(KERN_ERR "Maximum limit for PRI interface is 126 "
1364 if (pri && hc->b_num > 30) {
1365 printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
1367 printk(KERN_WARNING "Your selection of %d channels must be "
1368 "supported by application.\n", hc->limit);
1371 hc->remoteip = ip[l1oip_cnt << 2] << 24
1372 | ip[(l1oip_cnt << 2) + 1] << 16
1373 | ip[(l1oip_cnt << 2) + 2] << 8
1374 | ip[(l1oip_cnt << 2) + 3];
1375 hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT + l1oip_cnt);
1376 if (remoteport[l1oip_cnt])
1377 hc->remoteport = remoteport[l1oip_cnt];
1379 hc->remoteport = hc->localport;
1380 if (debug & DEBUG_L1OIP_INIT)
1381 printk(KERN_DEBUG "%s: using local port %d remote ip "
1382 "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1383 hc->localport, hc->remoteip >> 24,
1384 (hc->remoteip >> 16) & 0xff,
1385 (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1386 hc->remoteport, hc->ondemand);
1388 dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1392 mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1395 dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1397 dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1398 dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1399 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1400 dch->dev.D.send = handle_dmsg;
1401 dch->dev.D.ctrl = l1oip_dctrl;
1402 dch->dev.nrbchan = hc->b_num;
1403 dch->slot = hc->d_idx;
1404 hc->chan[hc->d_idx].dch = dch;
1406 for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1409 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1411 printk(KERN_ERR "%s: no memory for bchannel\n",
1418 mISDN_initbchannel(bch, MAX_DATA_MEM, 0);
1420 bch->ch.send = handle_bmsg;
1421 bch->ch.ctrl = l1oip_bctrl;
1422 bch->ch.nr = i + ch;
1423 list_add(&bch->ch.list, &dch->dev.bchannels);
1424 hc->chan[i + ch].bch = bch;
1425 set_channelmap(bch->nr, dch->dev.channelmap);
1427 /* TODO: create a parent device for this driver */
1428 ret = mISDN_register_device(&dch->dev, NULL, hc->name);
1433 if (debug & DEBUG_L1OIP_INIT)
1434 printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
1435 __func__, l1oip_cnt + 1);
1436 ret = l1oip_socket_open(hc);
1440 timer_setup(&hc->keep_tl, l1oip_keepalive, 0);
1441 hc->keep_tl.expires = jiffies + 2 * HZ; /* two seconds first time */
1442 add_timer(&hc->keep_tl);
1444 timer_setup(&hc->timeout_tl, l1oip_timeout, 0);
1445 hc->timeout_on = 0; /* state that we have timer off */
1457 printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
1460 INIT_LIST_HEAD(&l1oip_ilist);
1461 spin_lock_init(&l1oip_lock);
1463 if (l1oip_4bit_alloc(ulaw))
1467 while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
1468 switch (type[l1oip_cnt] & 0xff) {
1486 printk(KERN_ERR "Card type(%d) not supported.\n",
1487 type[l1oip_cnt] & 0xff);
1492 if (debug & DEBUG_L1OIP_INIT)
1493 printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
1494 __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1495 bundle ? "bundled IP packet for all B-channels" :
1496 "separate IP packets for every B-channel");
1498 hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1500 printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1504 INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1506 spin_lock(&l1oip_lock);
1507 list_add_tail(&hc->list, &l1oip_ilist);
1508 spin_unlock(&l1oip_lock);
1510 ret = init_card(hc, pri, bundle);
1518 printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1522 module_init(l1oip_init);
1523 module_exit(l1oip_cleanup);