blob: db50f788855db4f41de7f35e3aee4abe4da87253 [file] [log] [blame]
Karsten Keil3712b422008-07-27 02:02:10 +02001/*
2
3 * l1oip.c low level driver for tunneling layer 1 over IP
4 *
5 * NOTE: It is not compatible with TDMoIP nor "ISDN over IP".
6 *
7 * Author Andreas Eversberg (jolly@eversberg.eu)
8 *
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)
12 * any later version.
13 *
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.
18 *
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.
22 *
23 */
24
25/* module parameters:
26 * type:
Joe Perches475be4d2012-02-19 19:52:38 -080027 Value 1 = BRI
28 Value 2 = PRI
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.)
Karsten Keil3712b422008-07-27 02:02:10 +020034
35 * codec:
Joe Perches475be4d2012-02-19 19:52:38 -080036 Value 0 = transparent (default)
37 Value 1 = transfer ALAW
38 Value 2 = transfer ULAW
39 Value 3 = transfer generic 4 bit compression.
Karsten Keil3712b422008-07-27 02:02:10 +020040
41 * ulaw:
Joe Perches475be4d2012-02-19 19:52:38 -080042 0 = we use a-Law (default)
43 1 = we use u-Law
Karsten Keil3712b422008-07-27 02:02:10 +020044
45 * limit:
Joe Perches475be4d2012-02-19 19:52:38 -080046 limitation of B-channels to control bandwidth (1...126)
47 BRI: 1 or 2
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.
Karsten Keil3712b422008-07-27 02:02:10 +020052
53 * ip:
Joe Perches475be4d2012-02-19 19:52:38 -080054 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)
Karsten Keil3712b422008-07-27 02:02:10 +020057
58 * port:
Joe Perches475be4d2012-02-19 19:52:38 -080059 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.
Karsten Keil3712b422008-07-27 02:02:10 +020062
63 * remoteport:
Joe Perches475be4d2012-02-19 19:52:38 -080064 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.
Karsten Keil3712b422008-07-27 02:02:10 +020067
68 * ondemand:
Joe Perches475be4d2012-02-19 19:52:38 -080069 0 = fixed (always transmit packets, even when remote side timed out)
70 1 = on demand (only transmit packets, when remote side is detected)
71 the default is 0
72 NOTE: ID must also be set for on demand.
Karsten Keil3712b422008-07-27 02:02:10 +020073
74 * id:
Joe Perches475be4d2012-02-19 19:52:38 -080075 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.
Karsten Keil3712b422008-07-27 02:02:10 +020077
78 * debug:
Joe Perches475be4d2012-02-19 19:52:38 -080079 NOTE: only one debug value must be given for all cards
80 enable debugging (see l1oip.h for debug options)
Karsten Keil3712b422008-07-27 02:02:10 +020081
82
Joe Perches475be4d2012-02-19 19:52:38 -080083 Special mISDN controls:
Karsten Keil3712b422008-07-27 02:02:10 +020084
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)
88 optional:
89 p2 = bytes 3-4 : local port in network order (high byte first)
90
91 op = MISDN_CTRL_UNSETPEER*
92
93 * Use l1oipctrl for comfortable setting or removing ip address.
Joe Perches475be4d2012-02-19 19:52:38 -080094 (Layer 1 Over IP CTRL)
Karsten Keil3712b422008-07-27 02:02:10 +020095
96
Joe Perches475be4d2012-02-19 19:52:38 -080097 L1oIP-Protocol
98 --------------
Karsten Keil3712b422008-07-27 02:02:10 +020099
Joe Perches475be4d2012-02-19 19:52:38 -0800100 Frame Header:
Karsten Keil3712b422008-07-27 02:02:10 +0200101
102 7 6 5 4 3 2 1 0
Joe Perches475be4d2012-02-19 19:52:38 -0800103 +---------------+
104 |Ver|T|I|Coding |
105 +---------------+
106 | ID byte 3 * |
107 +---------------+
108 | ID byte 2 * |
109 +---------------+
110 | ID byte 1 * |
111 +---------------+
112 | ID byte 0 * |
113 +---------------+
114 |M| Channel |
115 +---------------+
116 | Length * |
117 +---------------+
118 | Time Base MSB |
119 +---------------+
120 | Time Base LSB |
121 +---------------+
122 | Data.... |
Karsten Keil3712b422008-07-27 02:02:10 +0200123
Joe Perches475be4d2012-02-19 19:52:38 -0800124 ...
Karsten Keil3712b422008-07-27 02:02:10 +0200125
Joe Perches475be4d2012-02-19 19:52:38 -0800126 | |
127 +---------------+
128 |M| Channel |
129 +---------------+
130 | Length * |
131 +---------------+
132 | Time Base MSB |
133 +---------------+
134 | Time Base LSB |
135 +---------------+
136 | Data.... |
Karsten Keil3712b422008-07-27 02:02:10 +0200137
Joe Perches475be4d2012-02-19 19:52:38 -0800138 ...
Karsten Keil3712b422008-07-27 02:02:10 +0200139
140
Joe Perches475be4d2012-02-19 19:52:38 -0800141 * Only included in some cases.
Karsten Keil3712b422008-07-27 02:02:10 +0200142
Joe Perches475be4d2012-02-19 19:52:38 -0800143 - Ver = Version
144 If version is missmatch, the frame must be ignored.
Karsten Keil3712b422008-07-27 02:02:10 +0200145
Joe Perches475be4d2012-02-19 19:52:38 -0800146 - T = Type of interface
147 Must be 0 for S0 or 1 for E1.
Karsten Keil3712b422008-07-27 02:02:10 +0200148
Joe Perches475be4d2012-02-19 19:52:38 -0800149 - I = Id present
150 If bit is set, four ID bytes are included in frame.
Karsten Keil3712b422008-07-27 02:02:10 +0200151
Joe Perches475be4d2012-02-19 19:52:38 -0800152 - ID = Connection ID
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.
Karsten Keil3712b422008-07-27 02:02:10 +0200155
Joe Perches475be4d2012-02-19 19:52:38 -0800156 - Coding = Type of codec
157 Must be 0 for no transcoding. Also for D-channel and other HDLC frames.
Karsten Keil3712b422008-07-27 02:02:10 +0200158 1 and 2 are reserved for explicitly use of a-LAW or u-LAW codec.
159 3 is used for generic table compressor.
160
Joe Perches475be4d2012-02-19 19:52:38 -0800161 - 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200165
Joe Perches475be4d2012-02-19 19:52:38 -0800166 - Channel = Channel number
167 0 reserved
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)
Karsten Keil3712b422008-07-27 02:02:10 +0200171
Joe Perches475be4d2012-02-19 19:52:38 -0800172 - The length is used if the M-flag is 1. It is used to find the next channel
173 inside frame.
174 NOTE: A value of 0 equals 256 bytes of data.
Karsten Keil3712b422008-07-27 02:02:10 +0200175 -> For larger data blocks, a single frame must be used.
176 -> For larger streams, a single frame or multiple blocks with same channel ID
Joe Perches475be4d2012-02-19 19:52:38 -0800177 must be used.
Karsten Keil3712b422008-07-27 02:02:10 +0200178
Joe Perches475be4d2012-02-19 19:52:38 -0800179 - 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200185
186
Joe Perches475be4d2012-02-19 19:52:38 -0800187 Two Timers:
Karsten Keil3712b422008-07-27 02:02:10 +0200188
Joe Perches475be4d2012-02-19 19:52:38 -0800189 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200192
Joe Perches475be4d2012-02-19 19:52:38 -0800193 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200195
196
Joe Perches475be4d2012-02-19 19:52:38 -0800197 Dynamic IP handling:
Karsten Keil3712b422008-07-27 02:02:10 +0200198
Joe Perches475be4d2012-02-19 19:52:38 -0800199 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200202
203
Joe Perches475be4d2012-02-19 19:52:38 -0800204 On Demand:
Karsten Keil3712b422008-07-27 02:02:10 +0200205
Joe Perches475be4d2012-02-19 19:52:38 -0800206 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200210
211
Joe Perches475be4d2012-02-19 19:52:38 -0800212 Socket and Thread
213 -----------------
Karsten Keil3712b422008-07-27 02:02:10 +0200214
Joe Perches475be4d2012-02-19 19:52:38 -0800215 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.
Karsten Keil3712b422008-07-27 02:02:10 +0200221
222*/
223
224#define L1OIP_VERSION 0 /* 0...3 */
225
226#include <linux/module.h>
227#include <linux/delay.h>
228#include <linux/mISDNif.h>
229#include <linux/mISDNhw.h>
230#include <linux/mISDNdsp.h>
231#include <linux/init.h>
232#include <linux/in.h>
233#include <linux/inet.h>
234#include <linux/workqueue.h>
235#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900236#include <linux/slab.h>
Karsten Keil3712b422008-07-27 02:02:10 +0200237#include <net/sock.h>
238#include "core.h"
239#include "l1oip.h"
240
241static const char *l1oip_revision = "2.00";
242
243static int l1oip_cnt;
244static spinlock_t l1oip_lock;
245static struct list_head l1oip_ilist;
246
247#define MAX_CARDS 16
248static u_int type[MAX_CARDS];
249static u_int codec[MAX_CARDS];
Joe Perches475be4d2012-02-19 19:52:38 -0800250static u_int ip[MAX_CARDS * 4];
Karsten Keil3712b422008-07-27 02:02:10 +0200251static u_int port[MAX_CARDS];
252static u_int remoteport[MAX_CARDS];
253static u_int ondemand[MAX_CARDS];
254static u_int limit[MAX_CARDS];
255static u_int id[MAX_CARDS];
256static int debug;
257static int ulaw;
258
259MODULE_AUTHOR("Andreas Eversberg");
260MODULE_LICENSE("GPL");
261module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
262module_param_array(codec, uint, NULL, S_IRUGO | S_IWUSR);
263module_param_array(ip, uint, NULL, S_IRUGO | S_IWUSR);
264module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
265module_param_array(remoteport, uint, NULL, S_IRUGO | S_IWUSR);
266module_param_array(ondemand, uint, NULL, S_IRUGO | S_IWUSR);
267module_param_array(limit, uint, NULL, S_IRUGO | S_IWUSR);
268module_param_array(id, uint, NULL, S_IRUGO | S_IWUSR);
269module_param(ulaw, uint, S_IRUGO | S_IWUSR);
270module_param(debug, uint, S_IRUGO | S_IWUSR);
271
272/*
273 * send a frame via socket, if open and restart timer
274 */
275static int
276l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
Joe Perches475be4d2012-02-19 19:52:38 -0800277 u16 timebase, u8 *buf, int len)
Karsten Keil3712b422008-07-27 02:02:10 +0200278{
279 u8 *p;
280 int multi = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800281 u8 frame[len + 32];
Karsten Keil3712b422008-07-27 02:02:10 +0200282 struct socket *socket = NULL;
Karsten Keil3712b422008-07-27 02:02:10 +0200283
284 if (debug & DEBUG_L1OIP_MSG)
285 printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800286 __func__, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200287
288 p = frame;
289
290 /* restart timer */
Joe Perches475be4d2012-02-19 19:52:38 -0800291 if ((int)(hc->keep_tl.expires-jiffies) < 5 * HZ) {
Karsten Keil3712b422008-07-27 02:02:10 +0200292 del_timer(&hc->keep_tl);
Joe Perches475be4d2012-02-19 19:52:38 -0800293 hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE * HZ;
Karsten Keil3712b422008-07-27 02:02:10 +0200294 add_timer(&hc->keep_tl);
295 } else
Joe Perches475be4d2012-02-19 19:52:38 -0800296 hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE * HZ;
Karsten Keil3712b422008-07-27 02:02:10 +0200297
298 if (debug & DEBUG_L1OIP_MSG)
299 printk(KERN_DEBUG "%s: resetting timer\n", __func__);
300
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 "
Joe Perches475be4d2012-02-19 19:52:38 -0800305 "IP is not set.\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200306 return len;
307 }
308
309 /* assemble frame */
Joe Perches475be4d2012-02-19 19:52:38 -0800310 *p++ = (L1OIP_VERSION << 6) /* version and coding */
311 | (hc->pri ? 0x20 : 0x00) /* type */
312 | (hc->id ? 0x10 : 0x00) /* id */
313 | localcodec;
Karsten Keil3712b422008-07-27 02:02:10 +0200314 if (hc->id) {
Joe Perches475be4d2012-02-19 19:52:38 -0800315 *p++ = hc->id >> 24; /* id */
316 *p++ = hc->id >> 16;
317 *p++ = hc->id >> 8;
Karsten Keil3712b422008-07-27 02:02:10 +0200318 *p++ = hc->id;
319 }
Karsten Keileac74af2009-05-22 11:04:56 +0000320 *p++ = (multi == 1) ? 0x80 : 0x00 + channel; /* m-flag, channel */
Karsten Keil3712b422008-07-27 02:02:10 +0200321 if (multi == 1)
322 *p++ = len; /* length */
Joe Perches475be4d2012-02-19 19:52:38 -0800323 *p++ = timebase >> 8; /* time base */
Karsten Keil3712b422008-07-27 02:02:10 +0200324 *p++ = timebase;
325
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800333 &hc->chan[channel].codecstate);
Karsten Keil3712b422008-07-27 02:02:10 +0200334 else
335 memcpy(p, buf, len);
336 }
337 len += p - frame;
338
339 /* check for socket in safe condition */
340 spin_lock(&hc->socket_lock);
341 if (!hc->socket) {
342 spin_unlock(&hc->socket_lock);
343 return 0;
344 }
345 /* seize socket */
346 socket = hc->socket;
347 hc->socket = NULL;
348 spin_unlock(&hc->socket_lock);
349 /* send packet */
350 if (debug & DEBUG_L1OIP_MSG)
351 printk(KERN_DEBUG "%s: sending packet to socket (len "
Joe Perches475be4d2012-02-19 19:52:38 -0800352 "= %d)\n", __func__, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200353 hc->sendiov.iov_base = frame;
354 hc->sendiov.iov_len = len;
Arnaldo Carvalho de Melo8c90e112009-05-22 11:04:57 +0000355 len = kernel_sendmsg(socket, &hc->sendmsg, &hc->sendiov, 1, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200356 /* give socket back */
357 hc->socket = socket; /* no locking required */
358
359 return len;
360}
361
362
363/*
364 * receive channel data from socket
365 */
366static void
367l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
Joe Perches475be4d2012-02-19 19:52:38 -0800368 u8 *buf, int len)
Karsten Keil3712b422008-07-27 02:02:10 +0200369{
370 struct sk_buff *nskb;
371 struct bchannel *bch;
372 struct dchannel *dch;
373 u8 *p;
374 u32 rx_counter;
375
376 if (len == 0) {
377 if (debug & DEBUG_L1OIP_MSG)
378 printk(KERN_DEBUG "%s: received empty keepalive data, "
Joe Perches475be4d2012-02-19 19:52:38 -0800379 "ignoring\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200380 return;
381 }
382
383 if (debug & DEBUG_L1OIP_MSG)
384 printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800385 __func__, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200386
387 if (channel < 1 || channel > 127) {
388 printk(KERN_WARNING "%s: packet error - channel %d out of "
Joe Perches475be4d2012-02-19 19:52:38 -0800389 "range\n", __func__, channel);
Karsten Keil3712b422008-07-27 02:02:10 +0200390 return;
391 }
392 dch = hc->chan[channel].dch;
393 bch = hc->chan[channel].bch;
394 if (!dch && !bch) {
395 printk(KERN_WARNING "%s: packet error - channel %d not in "
Joe Perches475be4d2012-02-19 19:52:38 -0800396 "stack\n", __func__, channel);
Karsten Keil3712b422008-07-27 02:02:10 +0200397 return;
398 }
399
400 /* prepare message */
Joe Perches475be4d2012-02-19 19:52:38 -0800401 nskb = mI_alloc_skb((remotecodec == 3) ? (len << 1) : len, GFP_ATOMIC);
Karsten Keil3712b422008-07-27 02:02:10 +0200402 if (!nskb) {
403 printk(KERN_ERR "%s: No mem for skb.\n", __func__);
404 return;
405 }
Joe Perches475be4d2012-02-19 19:52:38 -0800406 p = skb_put(nskb, (remotecodec == 3) ? (len << 1) : len);
Karsten Keil3712b422008-07-27 02:02:10 +0200407
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);
414 else
415 memcpy(p, buf, len);
416
417 /* send message up */
418 if (dch && len >= 2) {
419 dch->rx_skb = nskb;
420 recv_Dchannel(dch);
421 }
422 if (bch) {
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))
428 rx_counter =
429 (rx_counter & 0xffff0000) | timebase;
430 else
Joe Perches475be4d2012-02-19 19:52:38 -0800431 rx_counter = ((rx_counter & 0xffff0000) + 0x10000)
Karsten Keil3712b422008-07-27 02:02:10 +0200432 | timebase;
433 } else {
434 /* time has changed backwards */
435 if (timebase < (rx_counter & 0xffff))
436 rx_counter =
437 (rx_counter & 0xffff0000) | timebase;
438 else
Joe Perches475be4d2012-02-19 19:52:38 -0800439 rx_counter = ((rx_counter & 0xffff0000) - 0x10000)
Karsten Keil3712b422008-07-27 02:02:10 +0200440 | timebase;
441 }
442 hc->chan[channel].rx_counter = rx_counter;
443
444#ifdef REORDER_DEBUG
445 if (hc->chan[channel].disorder_flag) {
446 struct sk_buff *skb;
447 int cnt;
448 skb = hc->chan[channel].disorder_skb;
449 hc->chan[channel].disorder_skb = nskb;
450 nskb = skb;
451 cnt = hc->chan[channel].disorder_cnt;
452 hc->chan[channel].disorder_cnt = rx_counter;
453 rx_counter = cnt;
454 }
455 hc->chan[channel].disorder_flag ^= 1;
456 if (nskb)
457#endif
Karsten Keileac74af2009-05-22 11:04:56 +0000458 queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
Karsten Keil3712b422008-07-27 02:02:10 +0200459 }
460}
461
462
463/*
464 * parse frame and extract channel data
465 */
466static void
467l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
468{
Hannes Eder863a76e2008-12-12 21:17:38 -0800469 u32 packet_id;
Karsten Keil3712b422008-07-27 02:02:10 +0200470 u8 channel;
471 u8 remotecodec;
472 u16 timebase;
473 int m, mlen;
474 int len_start = len; /* initial frame length */
475 struct dchannel *dch = hc->chan[hc->d_idx].dch;
476
477 if (debug & DEBUG_L1OIP_MSG)
478 printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800479 __func__, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200480
Daniel Mack1537a362010-01-29 15:57:49 +0800481 /* check length */
Joe Perches475be4d2012-02-19 19:52:38 -0800482 if (len < 1 + 1 + 2) {
Karsten Keil3712b422008-07-27 02:02:10 +0200483 printk(KERN_WARNING "%s: packet error - length %d below "
Joe Perches475be4d2012-02-19 19:52:38 -0800484 "4 bytes\n", __func__, len);
Karsten Keil3712b422008-07-27 02:02:10 +0200485 return;
486 }
487
488 /* check version */
Joe Perches475be4d2012-02-19 19:52:38 -0800489 if (((*buf) >> 6) != L1OIP_VERSION) {
Karsten Keil3712b422008-07-27 02:02:10 +0200490 printk(KERN_WARNING "%s: packet error - unknown version %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800491 __func__, buf[0]>>6);
Karsten Keil3712b422008-07-27 02:02:10 +0200492 return;
493 }
494
495 /* check type */
Joe Perches475be4d2012-02-19 19:52:38 -0800496 if (((*buf) & 0x20) && !hc->pri) {
Karsten Keil3712b422008-07-27 02:02:10 +0200497 printk(KERN_WARNING "%s: packet error - received E1 packet "
Joe Perches475be4d2012-02-19 19:52:38 -0800498 "on S0 interface\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200499 return;
500 }
Joe Perches475be4d2012-02-19 19:52:38 -0800501 if (!((*buf) & 0x20) && hc->pri) {
Karsten Keil3712b422008-07-27 02:02:10 +0200502 printk(KERN_WARNING "%s: packet error - received S0 packet "
Joe Perches475be4d2012-02-19 19:52:38 -0800503 "on E1 interface\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200504 return;
505 }
506
507 /* get id flag */
Joe Perches475be4d2012-02-19 19:52:38 -0800508 packet_id = (*buf >> 4) & 1;
Karsten Keil3712b422008-07-27 02:02:10 +0200509
510 /* check coding */
511 remotecodec = (*buf) & 0x0f;
512 if (remotecodec > 3) {
513 printk(KERN_WARNING "%s: packet error - remotecodec %d "
Joe Perches475be4d2012-02-19 19:52:38 -0800514 "unsupported\n", __func__, remotecodec);
Karsten Keil3712b422008-07-27 02:02:10 +0200515 return;
516 }
517 buf++;
518 len--;
519
Hannes Eder863a76e2008-12-12 21:17:38 -0800520 /* check packet_id */
521 if (packet_id) {
Karsten Keil3712b422008-07-27 02:02:10 +0200522 if (!hc->id) {
523 printk(KERN_WARNING "%s: packet error - packet has id "
Joe Perches475be4d2012-02-19 19:52:38 -0800524 "0x%x, but we have not\n", __func__, packet_id);
Karsten Keil3712b422008-07-27 02:02:10 +0200525 return;
526 }
527 if (len < 4) {
528 printk(KERN_WARNING "%s: packet error - packet too "
Joe Perches475be4d2012-02-19 19:52:38 -0800529 "short for ID value\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200530 return;
531 }
Hannes Eder863a76e2008-12-12 21:17:38 -0800532 packet_id = (*buf++) << 24;
533 packet_id += (*buf++) << 16;
534 packet_id += (*buf++) << 8;
535 packet_id += (*buf++);
Karsten Keil3712b422008-07-27 02:02:10 +0200536 len -= 4;
537
Hannes Eder863a76e2008-12-12 21:17:38 -0800538 if (packet_id != hc->id) {
Karsten Keil3712b422008-07-27 02:02:10 +0200539 printk(KERN_WARNING "%s: packet error - ID mismatch, "
Joe Perches475be4d2012-02-19 19:52:38 -0800540 "got 0x%x, we 0x%x\n",
541 __func__, packet_id, hc->id);
Karsten Keil3712b422008-07-27 02:02:10 +0200542 return;
543 }
544 } else {
545 if (hc->id) {
546 printk(KERN_WARNING "%s: packet error - packet has no "
Joe Perches475be4d2012-02-19 19:52:38 -0800547 "ID, but we have\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200548 return;
549 }
550 }
551
552multiframe:
553 if (len < 1) {
554 printk(KERN_WARNING "%s: packet error - packet too short, "
Joe Perches475be4d2012-02-19 19:52:38 -0800555 "channel expected at position %d.\n",
556 __func__, len-len_start + 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200557 return;
558 }
559
560 /* get channel and multiframe flag */
Joe Perches475be4d2012-02-19 19:52:38 -0800561 channel = *buf & 0x7f;
Karsten Keil3712b422008-07-27 02:02:10 +0200562 m = *buf >> 7;
563 buf++;
564 len--;
565
566 /* check length on multiframe */
567 if (m) {
568 if (len < 1) {
569 printk(KERN_WARNING "%s: packet error - packet too "
Joe Perches475be4d2012-02-19 19:52:38 -0800570 "short, length expected at position %d.\n",
571 __func__, len_start - len - 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200572 return;
573 }
574
575 mlen = *buf++;
576 len--;
577 if (mlen == 0)
578 mlen = 256;
Joe Perches475be4d2012-02-19 19:52:38 -0800579 if (len < mlen + 3) {
Karsten Keil3712b422008-07-27 02:02:10 +0200580 printk(KERN_WARNING "%s: packet error - length %d at "
Joe Perches475be4d2012-02-19 19:52:38 -0800581 "position %d exceeds total length %d.\n",
582 __func__, mlen, len_start-len - 1, len_start);
Karsten Keil3712b422008-07-27 02:02:10 +0200583 return;
584 }
Joe Perches475be4d2012-02-19 19:52:38 -0800585 if (len == mlen + 3) {
Karsten Keil3712b422008-07-27 02:02:10 +0200586 printk(KERN_WARNING "%s: packet error - length %d at "
Joe Perches475be4d2012-02-19 19:52:38 -0800587 "position %d will not allow additional "
588 "packet.\n",
589 __func__, mlen, len_start-len + 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200590 return;
591 }
592 } else
Joe Perches475be4d2012-02-19 19:52:38 -0800593 mlen = len - 2; /* single frame, subtract timebase */
Karsten Keil3712b422008-07-27 02:02:10 +0200594
595 if (len < 2) {
596 printk(KERN_WARNING "%s: packet error - packet too short, time "
Joe Perches475be4d2012-02-19 19:52:38 -0800597 "base expected at position %d.\n",
598 __func__, len-len_start + 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200599 return;
600 }
601
602 /* get time base */
603 timebase = (*buf++) << 8;
604 timebase |= (*buf++);
605 len -= 2;
606
607 /* if inactive, we send up a PH_ACTIVATE and activate */
608 if (!test_bit(FLG_ACTIVE, &dch->Flags)) {
Joe Perches475be4d2012-02-19 19:52:38 -0800609 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +0200610 printk(KERN_DEBUG "%s: interface become active due to "
Joe Perches475be4d2012-02-19 19:52:38 -0800611 "received packet\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200612 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
613 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800614 NULL, GFP_ATOMIC);
Karsten Keil3712b422008-07-27 02:02:10 +0200615 }
616
617 /* distribute packet */
618 l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
619 buf += mlen;
620 len -= mlen;
621
622 /* multiframe */
623 if (m)
624 goto multiframe;
625
626 /* restart timer */
Joe Perches475be4d2012-02-19 19:52:38 -0800627 if ((int)(hc->timeout_tl.expires-jiffies) < 5 * HZ || !hc->timeout_on) {
Karsten Keil3712b422008-07-27 02:02:10 +0200628 hc->timeout_on = 1;
629 del_timer(&hc->timeout_tl);
Joe Perches475be4d2012-02-19 19:52:38 -0800630 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
Karsten Keil3712b422008-07-27 02:02:10 +0200631 add_timer(&hc->timeout_tl);
632 } else /* only adjust timer */
Joe Perches475be4d2012-02-19 19:52:38 -0800633 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
Karsten Keil3712b422008-07-27 02:02:10 +0200634
635 /* if ip or source port changes */
636 if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
Joe Perches475be4d2012-02-19 19:52:38 -0800637 || (hc->sin_remote.sin_port != sin->sin_port)) {
Karsten Keil3712b422008-07-27 02:02:10 +0200638 if (debug & DEBUG_L1OIP_SOCKET)
639 printk(KERN_DEBUG "%s: remote address changes from "
Joe Perches475be4d2012-02-19 19:52:38 -0800640 "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));
Karsten Keil3712b422008-07-27 02:02:10 +0200645 hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
646 hc->sin_remote.sin_port = sin->sin_port;
647 }
648}
649
650
651/*
652 * socket stuff
653 */
654static int
655l1oip_socket_thread(void *data)
656{
657 struct l1oip *hc = (struct l1oip *)data;
658 int ret = 0;
659 struct msghdr msg;
Karsten Keil3712b422008-07-27 02:02:10 +0200660 struct sockaddr_in sin_rx;
Frank Seidel3dc40392009-05-22 11:04:51 +0000661 unsigned char *recvbuf;
662 size_t recvbuf_size = 1500;
Karsten Keil3712b422008-07-27 02:02:10 +0200663 int recvlen;
664 struct socket *socket = NULL;
Yong Zhang01a1e7e2009-12-18 20:30:11 -0800665 DECLARE_COMPLETION_ONSTACK(wait);
Karsten Keil3712b422008-07-27 02:02:10 +0200666
Frank Seidel3dc40392009-05-22 11:04:51 +0000667 /* allocate buffer memory */
668 recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
669 if (!recvbuf) {
670 printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
671 ret = -ENOMEM;
672 goto fail;
673 }
674
Karsten Keil3712b422008-07-27 02:02:10 +0200675 /* make daemon */
676 allow_signal(SIGTERM);
677
678 /* create socket */
679 if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
680 printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
Frank Seidel3dc40392009-05-22 11:04:51 +0000681 ret = -EIO;
682 goto fail;
Karsten Keil3712b422008-07-27 02:02:10 +0200683 }
684
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);
689
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);
694
695 /* bind to incomming port */
696 if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
Joe Perches475be4d2012-02-19 19:52:38 -0800697 sizeof(hc->sin_local))) {
Karsten Keil3712b422008-07-27 02:02:10 +0200698 printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800699 __func__, hc->localport);
Karsten Keil3712b422008-07-27 02:02:10 +0200700 ret = -EINVAL;
701 goto fail;
702 }
703
704 /* check sk */
705 if (socket->sk == NULL) {
706 printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
707 ret = -EIO;
708 goto fail;
709 }
710
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;
Karsten Keil3712b422008-07-27 02:02:10 +0200716
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;
Karsten Keil3712b422008-07-27 02:02:10 +0200722
723 /* give away socket */
724 spin_lock(&hc->socket_lock);
725 hc->socket = socket;
726 spin_unlock(&hc->socket_lock);
727
728 /* read loop */
729 if (debug & DEBUG_L1OIP_SOCKET)
730 printk(KERN_DEBUG "%s: socket created and open\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800731 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200732 while (!signal_pending(current)) {
Arnaldo Carvalho de Melo8c90e112009-05-22 11:04:57 +0000733 struct kvec iov = {
734 .iov_base = recvbuf,
Andreas Eversbergb564afc2009-07-27 07:24:04 +0000735 .iov_len = recvbuf_size,
Arnaldo Carvalho de Melo8c90e112009-05-22 11:04:57 +0000736 };
737 recvlen = kernel_recvmsg(socket, &msg, &iov, 1,
Andreas Eversbergb564afc2009-07-27 07:24:04 +0000738 recvbuf_size, 0);
Karsten Keil3712b422008-07-27 02:02:10 +0200739 if (recvlen > 0) {
740 l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
741 } else {
742 if (debug & DEBUG_L1OIP_SOCKET)
Karsten Keileac74af2009-05-22 11:04:56 +0000743 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800744 "%s: broken pipe on socket\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200745 }
746 }
747
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);
Joe Perches475be4d2012-02-19 19:52:38 -0800753 schedule_timeout(HZ / 10);
Karsten Keil3712b422008-07-27 02:02:10 +0200754 spin_lock(&hc->socket_lock);
755 }
756 hc->socket = NULL;
757 spin_unlock(&hc->socket_lock);
758
759 if (debug & DEBUG_L1OIP_SOCKET)
760 printk(KERN_DEBUG "%s: socket thread terminating\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800761 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200762
763fail:
Frank Seidel3dc40392009-05-22 11:04:51 +0000764 /* free recvbuf */
765 kfree(recvbuf);
766
Karsten Keil3712b422008-07-27 02:02:10 +0200767 /* close socket */
768 if (socket)
769 sock_release(socket);
770
771 /* if we got killed, signal completion */
772 complete(&hc->socket_complete);
773 hc->socket_thread = NULL; /* show termination of thread */
774
775 if (debug & DEBUG_L1OIP_SOCKET)
776 printk(KERN_DEBUG "%s: socket thread terminated\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800777 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200778 return ret;
779}
780
781static void
782l1oip_socket_close(struct l1oip *hc)
783{
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200784 struct dchannel *dch = hc->chan[hc->d_idx].dch;
785
Karsten Keil3712b422008-07-27 02:02:10 +0200786 /* kill thread */
787 if (hc->socket_thread) {
788 if (debug & DEBUG_L1OIP_SOCKET)
789 printk(KERN_DEBUG "%s: socket thread exists, "
Joe Perches475be4d2012-02-19 19:52:38 -0800790 "killing...\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200791 send_sig(SIGTERM, hc->socket_thread, 0);
792 wait_for_completion(&hc->socket_complete);
793 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200794
795 /* if active, we send up a PH_DEACTIVATE and deactivate */
796 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
Joe Perches475be4d2012-02-19 19:52:38 -0800797 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200798 printk(KERN_DEBUG "%s: interface become deactivated "
Joe Perches475be4d2012-02-19 19:52:38 -0800799 "due to timeout\n", __func__);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200800 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
801 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800802 NULL, GFP_ATOMIC);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200803 }
Karsten Keil3712b422008-07-27 02:02:10 +0200804}
805
806static int
807l1oip_socket_open(struct l1oip *hc)
808{
809 /* in case of reopen, we need to close first */
810 l1oip_socket_close(hc);
811
812 init_completion(&hc->socket_complete);
813
814 /* create receive process */
815 hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
Joe Perches475be4d2012-02-19 19:52:38 -0800816 hc->name);
Karsten Keil3712b422008-07-27 02:02:10 +0200817 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800820 __func__, err);
Karsten Keil3712b422008-07-27 02:02:10 +0200821 hc->socket_thread = NULL;
822 sock_release(hc->socket);
823 return err;
824 }
825 if (debug & DEBUG_L1OIP_SOCKET)
826 printk(KERN_DEBUG "%s: socket thread created\n", __func__);
827
828 return 0;
829}
830
831
832static void
833l1oip_send_bh(struct work_struct *work)
834{
835 struct l1oip *hc = container_of(work, struct l1oip, workq);
836
Joe Perches475be4d2012-02-19 19:52:38 -0800837 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +0200838 printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
Joe Perches475be4d2012-02-19 19:52:38 -0800839 "frame on dchannel\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200840
841 /* send an empty l1oip frame at D-channel */
842 l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
843}
844
845
846/*
847 * timer stuff
848 */
849static void
850l1oip_keepalive(void *data)
851{
852 struct l1oip *hc = (struct l1oip *)data;
853
854 schedule_work(&hc->workq);
855}
856
857static void
858l1oip_timeout(void *data)
859{
860 struct l1oip *hc = (struct l1oip *)data;
861 struct dchannel *dch = hc->chan[hc->d_idx].dch;
862
863 if (debug & DEBUG_L1OIP_MSG)
864 printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
Joe Perches475be4d2012-02-19 19:52:38 -0800865 "down.\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200866
867 hc->timeout_on = 0; /* state that timer must be initialized next time */
868
869 /* if timeout, we send up a PH_DEACTIVATE and deactivate */
870 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
Joe Perches475be4d2012-02-19 19:52:38 -0800871 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +0200872 printk(KERN_DEBUG "%s: interface become deactivated "
Joe Perches475be4d2012-02-19 19:52:38 -0800873 "due to timeout\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200874 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
875 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800876 NULL, GFP_ATOMIC);
Karsten Keil3712b422008-07-27 02:02:10 +0200877 }
878
879 /* if we have ondemand set, we remove ip address */
880 if (hc->ondemand) {
881 if (debug & DEBUG_L1OIP_MSG)
882 printk(KERN_DEBUG "%s: on demand causes ip address to "
Joe Perches475be4d2012-02-19 19:52:38 -0800883 "be removed\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200884 hc->sin_remote.sin_addr.s_addr = 0;
885 }
886}
887
888
889/*
890 * message handling
891 */
892static int
893handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
894{
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);
899 int ret = -EINVAL;
900 int l, ll;
901 unsigned char *p;
902
903 switch (hh->prim) {
904 case PH_DATA_REQ:
905 if (skb->len < 1) {
906 printk(KERN_WARNING "%s: skb too small\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800907 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200908 break;
909 }
910 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
911 printk(KERN_WARNING "%s: skb too large\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800912 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200913 break;
914 }
915 /* send frame */
916 p = skb->data;
917 l = skb->len;
918 while (l) {
Karsten Keileac74af2009-05-22 11:04:56 +0000919 ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
Karsten Keil3712b422008-07-27 02:02:10 +0200920 l1oip_socket_send(hc, 0, dch->slot, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800921 hc->chan[dch->slot].tx_counter++, p, ll);
Karsten Keil3712b422008-07-27 02:02:10 +0200922 p += ll;
923 l -= ll;
924 }
925 skb_trim(skb, 0);
926 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
927 return 0;
928 case PH_ACTIVATE_REQ:
Joe Perches475be4d2012-02-19 19:52:38 -0800929 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +0200930 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
Joe Perches475be4d2012-02-19 19:52:38 -0800931 , __func__, dch->slot, hc->b_num + 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200932 skb_trim(skb, 0);
933 if (test_bit(FLG_ACTIVE, &dch->Flags))
934 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
935 else
936 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
937 return 0;
938 case PH_DEACTIVATE_REQ:
Joe Perches475be4d2012-02-19 19:52:38 -0800939 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +0200940 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
Joe Perches475be4d2012-02-19 19:52:38 -0800941 "(1..%d)\n", __func__, dch->slot,
942 hc->b_num + 1);
Karsten Keil3712b422008-07-27 02:02:10 +0200943 skb_trim(skb, 0);
944 if (test_bit(FLG_ACTIVE, &dch->Flags))
945 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
946 else
947 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
948 return 0;
949 }
950 if (!ret)
951 dev_kfree_skb(skb);
952 return ret;
953}
954
955static int
956channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
957{
958 int ret = 0;
959 struct l1oip *hc = dch->hw;
960
961 switch (cq->op) {
962 case MISDN_CTRL_GETOP:
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200963 cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
964 | MISDN_CTRL_GETPEER;
Karsten Keil3712b422008-07-27 02:02:10 +0200965 break;
966 case MISDN_CTRL_SETPEER:
967 hc->remoteip = (u32)cq->p1;
968 hc->remoteport = cq->p2 & 0xffff;
969 hc->localport = cq->p2 >> 16;
970 if (!hc->remoteport)
971 hc->remoteport = hc->localport;
972 if (debug & DEBUG_L1OIP_SOCKET)
973 printk(KERN_DEBUG "%s: got new ip address from user "
Joe Perches475be4d2012-02-19 19:52:38 -0800974 "space.\n", __func__);
Julia Lawallf7df0b82010-08-05 10:29:19 +0000975 l1oip_socket_open(hc);
Karsten Keil3712b422008-07-27 02:02:10 +0200976 break;
977 case MISDN_CTRL_UNSETPEER:
978 if (debug & DEBUG_L1OIP_SOCKET)
979 printk(KERN_DEBUG "%s: removing ip address.\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800980 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +0200981 hc->remoteip = 0;
982 l1oip_socket_open(hc);
983 break;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200984 case MISDN_CTRL_GETPEER:
985 if (debug & DEBUG_L1OIP_SOCKET)
986 printk(KERN_DEBUG "%s: getting ip address.\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800987 __func__);
Andreas Eversberg1b36c782008-09-20 13:43:28 +0200988 cq->p1 = hc->remoteip;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200989 cq->p2 = hc->remoteport | (hc->localport << 16);
990 break;
Karsten Keil3712b422008-07-27 02:02:10 +0200991 default:
992 printk(KERN_WARNING "%s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800993 __func__, cq->op);
Karsten Keil3712b422008-07-27 02:02:10 +0200994 ret = -EINVAL;
995 break;
996 }
997 return ret;
998}
999
1000static int
1001open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1002{
1003 if (debug & DEBUG_HW_OPEN)
1004 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001005 dch->dev.id, __builtin_return_address(0));
Karsten Keil3712b422008-07-27 02:02:10 +02001006 if (rq->protocol == ISDN_P_NONE)
1007 return -EINVAL;
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",
Joe Perches475be4d2012-02-19 19:52:38 -08001012 __func__, dch->dev.D.protocol, rq->protocol);
Karsten Keil3712b422008-07-27 02:02:10 +02001013 }
1014 if (dch->dev.D.protocol != rq->protocol)
1015 dch->dev.D.protocol = rq->protocol;
1016
1017 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1018 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
Joe Perches475be4d2012-02-19 19:52:38 -08001019 0, NULL, GFP_KERNEL);
Karsten Keil3712b422008-07-27 02:02:10 +02001020 }
1021 rq->ch = &dch->dev.D;
1022 if (!try_module_get(THIS_MODULE))
1023 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1024 return 0;
1025}
1026
1027static int
1028open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1029{
1030 struct bchannel *bch;
1031 int ch;
1032
Karsten Keilff4cc1d2008-07-30 18:26:58 +02001033 if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
Karsten Keil3712b422008-07-27 02:02:10 +02001034 return -EINVAL;
1035 if (rq->protocol == ISDN_P_NONE)
1036 return -EINVAL;
1037 ch = rq->adr.channel; /* BRI: 1=B1 2=B2 PRI: 1..15,17.. */
1038 bch = hc->chan[ch].bch;
1039 if (!bch) {
1040 printk(KERN_ERR "%s:internal error ch %d has no bch\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001041 __func__, ch);
Karsten Keil3712b422008-07-27 02:02:10 +02001042 return -EINVAL;
1043 }
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;
1047 rq->ch = &bch->ch;
1048 if (!try_module_get(THIS_MODULE))
1049 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1050 return 0;
1051}
1052
1053static int
1054l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1055{
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;
1060 int err = 0;
1061
1062 if (dch->debug & DEBUG_HW)
1063 printk(KERN_DEBUG "%s: cmd:%x %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001064 __func__, cmd, arg);
Karsten Keil3712b422008-07-27 02:02:10 +02001065 switch (cmd) {
1066 case OPEN_CHANNEL:
1067 rq = arg;
1068 switch (rq->protocol) {
1069 case ISDN_P_TE_S0:
1070 case ISDN_P_NT_S0:
1071 if (hc->pri) {
1072 err = -EINVAL;
1073 break;
1074 }
1075 err = open_dchannel(hc, dch, rq);
1076 break;
1077 case ISDN_P_TE_E1:
1078 case ISDN_P_NT_E1:
1079 if (!hc->pri) {
1080 err = -EINVAL;
1081 break;
1082 }
1083 err = open_dchannel(hc, dch, rq);
1084 break;
1085 default:
1086 err = open_bchannel(hc, dch, rq);
1087 }
1088 break;
1089 case CLOSE_CHANNEL:
1090 if (debug & DEBUG_HW_OPEN)
1091 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001092 __func__, dch->dev.id,
1093 __builtin_return_address(0));
Karsten Keil3712b422008-07-27 02:02:10 +02001094 module_put(THIS_MODULE);
1095 break;
1096 case CONTROL_CHANNEL:
1097 err = channel_dctrl(dch, arg);
1098 break;
1099 default:
1100 if (dch->debug & DEBUG_HW)
1101 printk(KERN_DEBUG "%s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001102 __func__, cmd);
Karsten Keil3712b422008-07-27 02:02:10 +02001103 err = -EINVAL;
1104 }
1105 return err;
1106}
1107
1108static int
1109handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1110{
1111 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1112 struct l1oip *hc = bch->hw;
1113 int ret = -EINVAL;
1114 struct mISDNhead *hh = mISDN_HEAD_P(skb);
Akinobu Mitab5d58432012-01-27 04:24:55 +00001115 int l, ll;
Karsten Keil3712b422008-07-27 02:02:10 +02001116 unsigned char *p;
1117
1118 switch (hh->prim) {
1119 case PH_DATA_REQ:
1120 if (skb->len <= 0) {
1121 printk(KERN_WARNING "%s: skb too small\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001122 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001123 break;
1124 }
1125 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1126 printk(KERN_WARNING "%s: skb too large\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001127 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001128 break;
1129 }
1130 /* check for AIS / ulaw-silence */
Karsten Keil3712b422008-07-27 02:02:10 +02001131 l = skb->len;
Akinobu Mitab5d58432012-01-27 04:24:55 +00001132 if (!memchr_inv(skb->data, 0xff, l)) {
Karsten Keil3712b422008-07-27 02:02:10 +02001133 if (debug & DEBUG_L1OIP_MSG)
1134 printk(KERN_DEBUG "%s: got AIS, not sending, "
Joe Perches475be4d2012-02-19 19:52:38 -08001135 "but counting\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001136 hc->chan[bch->slot].tx_counter += l;
1137 skb_trim(skb, 0);
1138 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1139 return 0;
1140 }
1141 /* check for silence */
Karsten Keil3712b422008-07-27 02:02:10 +02001142 l = skb->len;
Akinobu Mitab5d58432012-01-27 04:24:55 +00001143 if (!memchr_inv(skb->data, 0x2a, l)) {
Karsten Keil3712b422008-07-27 02:02:10 +02001144 if (debug & DEBUG_L1OIP_MSG)
1145 printk(KERN_DEBUG "%s: got silence, not sending"
Joe Perches475be4d2012-02-19 19:52:38 -08001146 ", but counting\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001147 hc->chan[bch->slot].tx_counter += l;
1148 skb_trim(skb, 0);
1149 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1150 return 0;
1151 }
1152
1153 /* send frame */
1154 p = skb->data;
1155 l = skb->len;
1156 while (l) {
Karsten Keileac74af2009-05-22 11:04:56 +00001157 ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
Karsten Keil3712b422008-07-27 02:02:10 +02001158 l1oip_socket_send(hc, hc->codec, bch->slot, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08001159 hc->chan[bch->slot].tx_counter, p, ll);
Karsten Keil3712b422008-07-27 02:02:10 +02001160 hc->chan[bch->slot].tx_counter += ll;
1161 p += ll;
1162 l -= ll;
1163 }
1164 skb_trim(skb, 0);
1165 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1166 return 0;
1167 case PH_ACTIVATE_REQ:
Joe Perches475be4d2012-02-19 19:52:38 -08001168 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +02001169 printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
Joe Perches475be4d2012-02-19 19:52:38 -08001170 , __func__, bch->slot, hc->b_num + 1);
Karsten Keil3712b422008-07-27 02:02:10 +02001171 hc->chan[bch->slot].codecstate = 0;
1172 test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1173 skb_trim(skb, 0);
1174 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1175 return 0;
1176 case PH_DEACTIVATE_REQ:
Joe Perches475be4d2012-02-19 19:52:38 -08001177 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
Karsten Keil3712b422008-07-27 02:02:10 +02001178 printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
Joe Perches475be4d2012-02-19 19:52:38 -08001179 "(1..%d)\n", __func__, bch->slot,
1180 hc->b_num + 1);
Karsten Keil3712b422008-07-27 02:02:10 +02001181 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1182 skb_trim(skb, 0);
1183 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1184 return 0;
1185 }
1186 if (!ret)
1187 dev_kfree_skb(skb);
1188 return ret;
1189}
1190
1191static int
1192channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1193{
1194 int ret = 0;
1195 struct dsp_features *features =
1196 (struct dsp_features *)(*((u_long *)&cq->p1));
1197
1198 switch (cq->op) {
1199 case MISDN_CTRL_GETOP:
1200 cq->op = MISDN_CTRL_HW_FEATURES_OP;
1201 break;
1202 case MISDN_CTRL_HW_FEATURES: /* fill features structure */
1203 if (debug & DEBUG_L1OIP_MSG)
1204 printk(KERN_DEBUG "%s: HW_FEATURE request\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001205 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001206 /* create confirm */
1207 features->unclocked = 1;
1208 features->unordered = 1;
1209 break;
1210 default:
1211 printk(KERN_WARNING "%s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001212 __func__, cq->op);
Karsten Keil3712b422008-07-27 02:02:10 +02001213 ret = -EINVAL;
1214 break;
1215 }
1216 return ret;
1217}
1218
1219static int
1220l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1221{
1222 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1223 int err = -EINVAL;
1224
1225 if (bch->debug & DEBUG_HW)
1226 printk(KERN_DEBUG "%s: cmd:%x %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001227 __func__, cmd, arg);
Karsten Keil3712b422008-07-27 02:02:10 +02001228 switch (cmd) {
1229 case CLOSE_CHANNEL:
1230 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1231 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1232 ch->protocol = ISDN_P_NONE;
1233 ch->peer = NULL;
1234 module_put(THIS_MODULE);
1235 err = 0;
1236 break;
1237 case CONTROL_CHANNEL:
1238 err = channel_bctrl(bch, arg);
1239 break;
1240 default:
1241 printk(KERN_WARNING "%s: unknown prim(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001242 __func__, cmd);
Karsten Keil3712b422008-07-27 02:02:10 +02001243 }
1244 return err;
1245}
1246
1247
1248/*
1249 * cleanup module and stack
1250 */
1251static void
1252release_card(struct l1oip *hc)
1253{
1254 int ch;
1255
1256 if (timer_pending(&hc->keep_tl))
1257 del_timer(&hc->keep_tl);
1258
1259 if (timer_pending(&hc->timeout_tl))
1260 del_timer(&hc->timeout_tl);
1261
Tejun Heo0d26aa72010-12-24 15:59:07 +01001262 cancel_work_sync(&hc->workq);
1263
Karsten Keil3712b422008-07-27 02:02:10 +02001264 if (hc->socket_thread)
1265 l1oip_socket_close(hc);
1266
1267 if (hc->registered && hc->chan[hc->d_idx].dch)
1268 mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1269 for (ch = 0; ch < 128; ch++) {
1270 if (hc->chan[ch].dch) {
1271 mISDN_freedchannel(hc->chan[ch].dch);
1272 kfree(hc->chan[ch].dch);
1273 }
1274 if (hc->chan[ch].bch) {
1275 mISDN_freebchannel(hc->chan[ch].bch);
1276 kfree(hc->chan[ch].bch);
1277#ifdef REORDER_DEBUG
1278 if (hc->chan[ch].disorder_skb)
1279 dev_kfree_skb(hc->chan[ch].disorder_skb);
1280#endif
1281 }
1282 }
1283
1284 spin_lock(&l1oip_lock);
1285 list_del(&hc->list);
1286 spin_unlock(&l1oip_lock);
1287
1288 kfree(hc);
1289}
1290
1291static void
1292l1oip_cleanup(void)
1293{
1294 struct l1oip *hc, *next;
1295
1296 list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1297 release_card(hc);
1298
1299 l1oip_4bit_free();
1300}
1301
1302
1303/*
1304 * module and stack init
1305 */
1306static int
1307init_card(struct l1oip *hc, int pri, int bundle)
1308{
1309 struct dchannel *dch;
1310 struct bchannel *bch;
1311 int ret;
1312 int i, ch;
1313
1314 spin_lock_init(&hc->socket_lock);
1315 hc->idx = l1oip_cnt;
1316 hc->pri = pri;
Karsten Keileac74af2009-05-22 11:04:56 +00001317 hc->d_idx = pri ? 16 : 3;
1318 hc->b_num = pri ? 30 : 2;
Karsten Keil3712b422008-07-27 02:02:10 +02001319 hc->bundle = bundle;
1320 if (hc->pri)
1321 sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1322 else
1323 sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1324
1325 switch (codec[l1oip_cnt]) {
1326 case 0: /* as is */
1327 case 1: /* alaw */
1328 case 2: /* ulaw */
1329 case 3: /* 4bit */
1330 break;
1331 default:
1332 printk(KERN_ERR "Codec(%d) not supported.\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001333 codec[l1oip_cnt]);
Karsten Keil3712b422008-07-27 02:02:10 +02001334 return -EINVAL;
1335 }
1336 hc->codec = codec[l1oip_cnt];
1337 if (debug & DEBUG_L1OIP_INIT)
1338 printk(KERN_DEBUG "%s: using codec %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001339 __func__, hc->codec);
Karsten Keil3712b422008-07-27 02:02:10 +02001340
1341 if (id[l1oip_cnt] == 0) {
1342 printk(KERN_WARNING "Warning: No 'id' value given or "
Joe Perches475be4d2012-02-19 19:52:38 -08001343 "0, this is highly unsecure. Please use 32 "
1344 "bit randmom number 0x...\n");
Karsten Keil3712b422008-07-27 02:02:10 +02001345 }
1346 hc->id = id[l1oip_cnt];
1347 if (debug & DEBUG_L1OIP_INIT)
1348 printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1349
1350 hc->ondemand = ondemand[l1oip_cnt];
1351 if (hc->ondemand && !hc->id) {
1352 printk(KERN_ERR "%s: ondemand option only allowed in "
Joe Perches475be4d2012-02-19 19:52:38 -08001353 "conjunction with non 0 ID\n", __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001354 return -EINVAL;
1355 }
1356
1357 if (limit[l1oip_cnt])
1358 hc->b_num = limit[l1oip_cnt];
1359 if (!pri && hc->b_num > 2) {
1360 printk(KERN_ERR "Maximum limit for BRI interface is 2 "
Joe Perches475be4d2012-02-19 19:52:38 -08001361 "channels.\n");
Karsten Keil3712b422008-07-27 02:02:10 +02001362 return -EINVAL;
1363 }
1364 if (pri && hc->b_num > 126) {
1365 printk(KERN_ERR "Maximum limit for PRI interface is 126 "
Joe Perches475be4d2012-02-19 19:52:38 -08001366 "channels.\n");
Karsten Keil3712b422008-07-27 02:02:10 +02001367 return -EINVAL;
1368 }
1369 if (pri && hc->b_num > 30) {
1370 printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
Joe Perches475be4d2012-02-19 19:52:38 -08001371 "channels.\n");
Karsten Keil3712b422008-07-27 02:02:10 +02001372 printk(KERN_WARNING "Your selection of %d channels must be "
Joe Perches475be4d2012-02-19 19:52:38 -08001373 "supported by application.\n", hc->limit);
Karsten Keil3712b422008-07-27 02:02:10 +02001374 }
1375
Joe Perches475be4d2012-02-19 19:52:38 -08001376 hc->remoteip = ip[l1oip_cnt << 2] << 24
1377 | ip[(l1oip_cnt << 2) + 1] << 16
1378 | ip[(l1oip_cnt << 2) + 2] << 8
1379 | ip[(l1oip_cnt << 2) + 3];
1380 hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT + l1oip_cnt);
Karsten Keil3712b422008-07-27 02:02:10 +02001381 if (remoteport[l1oip_cnt])
1382 hc->remoteport = remoteport[l1oip_cnt];
1383 else
1384 hc->remoteport = hc->localport;
1385 if (debug & DEBUG_L1OIP_INIT)
1386 printk(KERN_DEBUG "%s: using local port %d remote ip "
Joe Perches475be4d2012-02-19 19:52:38 -08001387 "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1388 hc->localport, hc->remoteip >> 24,
1389 (hc->remoteip >> 16) & 0xff,
1390 (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1391 hc->remoteport, hc->ondemand);
Karsten Keil3712b422008-07-27 02:02:10 +02001392
1393 dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1394 if (!dch)
1395 return -ENOMEM;
1396 dch->debug = debug;
1397 mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1398 dch->hw = hc;
1399 if (pri)
1400 dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1401 else
1402 dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1403 dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08001404 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil3712b422008-07-27 02:02:10 +02001405 dch->dev.D.send = handle_dmsg;
1406 dch->dev.D.ctrl = l1oip_dctrl;
1407 dch->dev.nrbchan = hc->b_num;
1408 dch->slot = hc->d_idx;
1409 hc->chan[hc->d_idx].dch = dch;
1410 i = 1;
1411 for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1412 if (ch == 15)
1413 i++;
1414 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1415 if (!bch) {
1416 printk(KERN_ERR "%s: no memory for bchannel\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001417 __func__);
Karsten Keil3712b422008-07-27 02:02:10 +02001418 return -ENOMEM;
1419 }
1420 bch->nr = i + ch;
1421 bch->slot = i + ch;
1422 bch->debug = debug;
Karsten Keil034005a2012-05-15 23:51:06 +00001423 mISDN_initbchannel(bch, MAX_DATA_MEM, 0);
Karsten Keil3712b422008-07-27 02:02:10 +02001424 bch->hw = hc;
1425 bch->ch.send = handle_bmsg;
1426 bch->ch.ctrl = l1oip_bctrl;
1427 bch->ch.nr = i + ch;
1428 list_add(&bch->ch.list, &dch->dev.bchannels);
1429 hc->chan[i + ch].bch = bch;
Karsten Keilff4cc1d2008-07-30 18:26:58 +02001430 set_channelmap(bch->nr, dch->dev.channelmap);
Karsten Keil3712b422008-07-27 02:02:10 +02001431 }
Matthias Urlichsb36b6542008-08-16 00:09:24 +02001432 /* TODO: create a parent device for this driver */
1433 ret = mISDN_register_device(&dch->dev, NULL, hc->name);
Karsten Keil3712b422008-07-27 02:02:10 +02001434 if (ret)
1435 return ret;
1436 hc->registered = 1;
1437
1438 if (debug & DEBUG_L1OIP_INIT)
1439 printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001440 __func__, l1oip_cnt + 1);
Karsten Keil3712b422008-07-27 02:02:10 +02001441 ret = l1oip_socket_open(hc);
1442 if (ret)
1443 return ret;
1444
1445 hc->keep_tl.function = (void *)l1oip_keepalive;
1446 hc->keep_tl.data = (ulong)hc;
1447 init_timer(&hc->keep_tl);
Joe Perches475be4d2012-02-19 19:52:38 -08001448 hc->keep_tl.expires = jiffies + 2 * HZ; /* two seconds first time */
Karsten Keil3712b422008-07-27 02:02:10 +02001449 add_timer(&hc->keep_tl);
1450
1451 hc->timeout_tl.function = (void *)l1oip_timeout;
1452 hc->timeout_tl.data = (ulong)hc;
1453 init_timer(&hc->timeout_tl);
1454 hc->timeout_on = 0; /* state that we have timer off */
1455
1456 return 0;
1457}
1458
1459static int __init
1460l1oip_init(void)
1461{
1462 int pri, bundle;
1463 struct l1oip *hc;
1464 int ret;
1465
1466 printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001467 l1oip_revision);
Karsten Keil3712b422008-07-27 02:02:10 +02001468
1469 INIT_LIST_HEAD(&l1oip_ilist);
1470 spin_lock_init(&l1oip_lock);
1471
1472 if (l1oip_4bit_alloc(ulaw))
1473 return -ENOMEM;
1474
1475 l1oip_cnt = 0;
roel kluin9bfdac92009-07-31 03:43:59 +00001476 while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
Karsten Keil3712b422008-07-27 02:02:10 +02001477 switch (type[l1oip_cnt] & 0xff) {
1478 case 1:
1479 pri = 0;
1480 bundle = 0;
1481 break;
1482 case 2:
1483 pri = 1;
1484 bundle = 0;
1485 break;
1486 case 3:
1487 pri = 0;
1488 bundle = 1;
1489 break;
1490 case 4:
1491 pri = 1;
1492 bundle = 1;
1493 break;
1494 default:
1495 printk(KERN_ERR "Card type(%d) not supported.\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001496 type[l1oip_cnt] & 0xff);
Karsten Keil3712b422008-07-27 02:02:10 +02001497 l1oip_cleanup();
1498 return -EINVAL;
1499 }
1500
1501 if (debug & DEBUG_L1OIP_INIT)
1502 printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001503 __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1504 bundle ? "bundled IP packet for all B-channels" :
1505 "separate IP packets for every B-channel");
Karsten Keil3712b422008-07-27 02:02:10 +02001506
1507 hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1508 if (!hc) {
1509 printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1510 l1oip_cleanup();
1511 return -ENOMEM;
1512 }
1513 INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1514
1515 spin_lock(&l1oip_lock);
1516 list_add_tail(&hc->list, &l1oip_ilist);
1517 spin_unlock(&l1oip_lock);
1518
1519 ret = init_card(hc, pri, bundle);
1520 if (ret) {
1521 l1oip_cleanup();
1522 return ret;
1523 }
1524
1525 l1oip_cnt++;
1526 }
1527 printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1528 return 0;
1529}
1530
1531module_init(l1oip_init);
1532module_exit(l1oip_cleanup);