xref: /openbmc/linux/drivers/isdn/mISDN/l1oip_core.c (revision 1b39eacd)
1 /*
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:
27  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.)
34 
35  * codec:
36  Value 0 = transparent (default)
37  Value 1 = transfer ALAW
38  Value 2 = transfer ULAW
39  Value 3 = transfer generic 4 bit compression.
40 
41  * ulaw:
42  0 = we use a-Law (default)
43  1 = we use u-Law
44 
45  * limit:
46  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.
52 
53  * ip:
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)
57 
58  * port:
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.
62 
63  * remoteport:
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.
67 
68  * ondemand:
69  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.
73 
74  * id:
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.
77 
78  * debug:
79  NOTE: only one debug value must be given for all cards
80  enable debugging (see l1oip.h for debug options)
81 
82 
83  Special mISDN controls:
84 
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.
94  (Layer 1 Over IP CTRL)
95 
96 
97  L1oIP-Protocol
98  --------------
99 
100  Frame Header:
101 
102  7 6 5 4 3 2 1 0
103  +---------------+
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....	|
123 
124  ...
125 
126  |               |
127  +---------------+
128  |M|   Channel   |
129  +---------------+
130  |    Length *   |
131  +---------------+
132  | Time Base MSB |
133  +---------------+
134  | Time Base LSB |
135  +---------------+
136  | Data....	|
137 
138  ...
139 
140 
141  * Only included in some cases.
142 
143  - Ver = Version
144  If version is missmatch, the frame must be ignored.
145 
146  - T = Type of interface
147  Must be 0 for S0 or 1 for E1.
148 
149  - I = Id present
150  If bit is set, four ID bytes are included in frame.
151 
152  - 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.
155 
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.
160 
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.
165 
166  - 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)
171 
172  - 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.
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
177  must be used.
178 
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.
185 
186 
187  Two Timers:
188 
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.
192 
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.
195 
196 
197  Dynamic IP handling:
198 
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.
202 
203 
204  On Demand:
205 
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.
210 
211 
212  Socket and Thread
213  -----------------
214 
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.
221 
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>
236 #include <linux/slab.h>
237 #include <linux/sched/signal.h>
238 
239 #include <net/sock.h>
240 #include "core.h"
241 #include "l1oip.h"
242 
243 static const char *l1oip_revision = "2.00";
244 
245 static int l1oip_cnt;
246 static spinlock_t l1oip_lock;
247 static struct list_head l1oip_ilist;
248 
249 #define MAX_CARDS	16
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];
258 static int debug;
259 static int ulaw;
260 
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);
273 
274 /*
275  * send a frame via socket, if open and restart timer
276  */
277 static int
278 l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
279 		  u16 timebase, u8 *buf, int len)
280 {
281 	u8 *p;
282 	u8 frame[len + 32];
283 	struct socket *socket = NULL;
284 
285 	if (debug & DEBUG_L1OIP_MSG)
286 		printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
287 		       __func__, len);
288 
289 	p = frame;
290 
291 	/* restart timer */
292 	if (time_before(hc->keep_tl.expires, jiffies + 5 * HZ))
293 		mod_timer(&hc->keep_tl, jiffies + L1OIP_KEEPALIVE * HZ);
294 	else
295 		hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE * HZ;
296 
297 	if (debug & DEBUG_L1OIP_MSG)
298 		printk(KERN_DEBUG "%s: resetting timer\n", __func__);
299 
300 	/* 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__);
305 		return len;
306 	}
307 
308 	/* assemble frame */
309 	*p++ = (L1OIP_VERSION << 6) /* version and coding */
310 		| (hc->pri ? 0x20 : 0x00) /* type */
311 		| (hc->id ? 0x10 : 0x00) /* id */
312 		| localcodec;
313 	if (hc->id) {
314 		*p++ = hc->id >> 24; /* id */
315 		*p++ = hc->id >> 16;
316 		*p++ = hc->id >> 8;
317 		*p++ = hc->id;
318 	}
319 	*p++ =  0x00 + channel; /* m-flag, channel */
320 	*p++ = timebase >> 8; /* time base */
321 	*p++ = timebase;
322 
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);
331 		else
332 			memcpy(p, buf, len);
333 	}
334 	len += p - frame;
335 
336 	/* check for socket in safe condition */
337 	spin_lock(&hc->socket_lock);
338 	if (!hc->socket) {
339 		spin_unlock(&hc->socket_lock);
340 		return 0;
341 	}
342 	/* seize socket */
343 	socket = hc->socket;
344 	hc->socket = NULL;
345 	spin_unlock(&hc->socket_lock);
346 	/* send packet */
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 */
355 
356 	return len;
357 }
358 
359 
360 /*
361  * receive channel data from socket
362  */
363 static void
364 l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
365 		  u8 *buf, int len)
366 {
367 	struct sk_buff *nskb;
368 	struct bchannel *bch;
369 	struct dchannel *dch;
370 	u8 *p;
371 	u32 rx_counter;
372 
373 	if (len == 0) {
374 		if (debug & DEBUG_L1OIP_MSG)
375 			printk(KERN_DEBUG "%s: received empty keepalive data, "
376 			       "ignoring\n", __func__);
377 		return;
378 	}
379 
380 	if (debug & DEBUG_L1OIP_MSG)
381 		printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
382 		       __func__, len);
383 
384 	if (channel < 1 || channel > 127) {
385 		printk(KERN_WARNING "%s: packet error - channel %d out of "
386 		       "range\n", __func__, channel);
387 		return;
388 	}
389 	dch = hc->chan[channel].dch;
390 	bch = hc->chan[channel].bch;
391 	if (!dch && !bch) {
392 		printk(KERN_WARNING "%s: packet error - channel %d not in "
393 		       "stack\n", __func__, channel);
394 		return;
395 	}
396 
397 	/* prepare message */
398 	nskb = mI_alloc_skb((remotecodec == 3) ? (len << 1) : len, GFP_ATOMIC);
399 	if (!nskb) {
400 		printk(KERN_ERR "%s: No mem for skb.\n", __func__);
401 		return;
402 	}
403 	p = skb_put(nskb, (remotecodec == 3) ? (len << 1) : len);
404 
405 	if (remotecodec == 1 && ulaw)
406 		l1oip_alaw_to_ulaw(buf, len, p);
407 	else if (remotecodec == 2 && !ulaw)
408 		l1oip_ulaw_to_alaw(buf, len, p);
409 	else if (remotecodec == 3)
410 		len = l1oip_4bit_to_law(buf, len, p);
411 	else
412 		memcpy(p, buf, len);
413 
414 	/* send message up */
415 	if (dch && len >= 2) {
416 		dch->rx_skb = nskb;
417 		recv_Dchannel(dch);
418 	}
419 	if (bch) {
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))
425 				rx_counter =
426 					(rx_counter & 0xffff0000) | timebase;
427 			else
428 				rx_counter = ((rx_counter & 0xffff0000) + 0x10000)
429 					| timebase;
430 		} else {
431 			/* time has changed backwards */
432 			if (timebase < (rx_counter & 0xffff))
433 				rx_counter =
434 					(rx_counter & 0xffff0000) | timebase;
435 			else
436 				rx_counter = ((rx_counter & 0xffff0000) - 0x10000)
437 					| timebase;
438 		}
439 		hc->chan[channel].rx_counter = rx_counter;
440 
441 #ifdef REORDER_DEBUG
442 		if (hc->chan[channel].disorder_flag) {
443 			swap(hc->chan[channel].disorder_skb, nskb);
444 			swap(hc->chan[channel].disorder_cnt, rx_counter);
445 		}
446 		hc->chan[channel].disorder_flag ^= 1;
447 		if (nskb)
448 #endif
449 			queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
450 	}
451 }
452 
453 
454 /*
455  * parse frame and extract channel data
456  */
457 static void
458 l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
459 {
460 	u32			packet_id;
461 	u8			channel;
462 	u8			remotecodec;
463 	u16			timebase;
464 	int			m, mlen;
465 	int			len_start = len; /* initial frame length */
466 	struct dchannel		*dch = hc->chan[hc->d_idx].dch;
467 
468 	if (debug & DEBUG_L1OIP_MSG)
469 		printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
470 		       __func__, len);
471 
472 	/* check length */
473 	if (len < 1 + 1 + 2) {
474 		printk(KERN_WARNING "%s: packet error - length %d below "
475 		       "4 bytes\n", __func__, len);
476 		return;
477 	}
478 
479 	/* check version */
480 	if (((*buf) >> 6) != L1OIP_VERSION) {
481 		printk(KERN_WARNING "%s: packet error - unknown version %d\n",
482 		       __func__, buf[0]>>6);
483 		return;
484 	}
485 
486 	/* check type */
487 	if (((*buf) & 0x20) && !hc->pri) {
488 		printk(KERN_WARNING "%s: packet error - received E1 packet "
489 		       "on S0 interface\n", __func__);
490 		return;
491 	}
492 	if (!((*buf) & 0x20) && hc->pri) {
493 		printk(KERN_WARNING "%s: packet error - received S0 packet "
494 		       "on E1 interface\n", __func__);
495 		return;
496 	}
497 
498 	/* get id flag */
499 	packet_id = (*buf >> 4) & 1;
500 
501 	/* check coding */
502 	remotecodec = (*buf) & 0x0f;
503 	if (remotecodec > 3) {
504 		printk(KERN_WARNING "%s: packet error - remotecodec %d "
505 		       "unsupported\n", __func__, remotecodec);
506 		return;
507 	}
508 	buf++;
509 	len--;
510 
511 	/* check packet_id */
512 	if (packet_id) {
513 		if (!hc->id) {
514 			printk(KERN_WARNING "%s: packet error - packet has id "
515 			       "0x%x, but we have not\n", __func__, packet_id);
516 			return;
517 		}
518 		if (len < 4) {
519 			printk(KERN_WARNING "%s: packet error - packet too "
520 			       "short for ID value\n", __func__);
521 			return;
522 		}
523 		packet_id = (*buf++) << 24;
524 		packet_id += (*buf++) << 16;
525 		packet_id += (*buf++) << 8;
526 		packet_id += (*buf++);
527 		len -= 4;
528 
529 		if (packet_id != hc->id) {
530 			printk(KERN_WARNING "%s: packet error - ID mismatch, "
531 			       "got 0x%x, we 0x%x\n",
532 			       __func__, packet_id, hc->id);
533 			return;
534 		}
535 	} else {
536 		if (hc->id) {
537 			printk(KERN_WARNING "%s: packet error - packet has no "
538 			       "ID, but we have\n", __func__);
539 			return;
540 		}
541 	}
542 
543 multiframe:
544 	if (len < 1) {
545 		printk(KERN_WARNING "%s: packet error - packet too short, "
546 		       "channel expected at position %d.\n",
547 		       __func__, len-len_start + 1);
548 		return;
549 	}
550 
551 	/* get channel and multiframe flag */
552 	channel = *buf & 0x7f;
553 	m = *buf >> 7;
554 	buf++;
555 	len--;
556 
557 	/* check length on multiframe */
558 	if (m) {
559 		if (len < 1) {
560 			printk(KERN_WARNING "%s: packet error - packet too "
561 			       "short, length expected at position %d.\n",
562 			       __func__, len_start - len - 1);
563 			return;
564 		}
565 
566 		mlen = *buf++;
567 		len--;
568 		if (mlen == 0)
569 			mlen = 256;
570 		if (len < mlen + 3) {
571 			printk(KERN_WARNING "%s: packet error - length %d at "
572 			       "position %d exceeds total length %d.\n",
573 			       __func__, mlen, len_start-len - 1, len_start);
574 			return;
575 		}
576 		if (len == mlen + 3) {
577 			printk(KERN_WARNING "%s: packet error - length %d at "
578 			       "position %d will not allow additional "
579 			       "packet.\n",
580 			       __func__, mlen, len_start-len + 1);
581 			return;
582 		}
583 	} else
584 		mlen = len - 2; /* single frame, subtract timebase */
585 
586 	if (len < 2) {
587 		printk(KERN_WARNING "%s: packet error - packet too short, time "
588 		       "base expected at position %d.\n",
589 		       __func__, len-len_start + 1);
590 		return;
591 	}
592 
593 	/* get time base */
594 	timebase = (*buf++) << 8;
595 	timebase |= (*buf++);
596 	len -= 2;
597 
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,
605 			    NULL, GFP_ATOMIC);
606 	}
607 
608 	/* distribute packet */
609 	l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
610 	buf += mlen;
611 	len -= mlen;
612 
613 	/* multiframe */
614 	if (m)
615 		goto multiframe;
616 
617 	/* restart timer */
618 	if (time_before(hc->timeout_tl.expires, jiffies + 5 * HZ) || !hc->timeout_on) {
619 		hc->timeout_on = 1;
620 		mod_timer(&hc->timeout_tl, jiffies + L1OIP_TIMEOUT * HZ);
621 	} else /* only adjust timer */
622 		hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
623 
624 	/* if ip or source port changes */
625 	if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
626 	    || (hc->sin_remote.sin_port != sin->sin_port)) {
627 		if (debug & DEBUG_L1OIP_SOCKET)
628 			printk(KERN_DEBUG "%s: remote address changes from "
629 			       "0x%08x to 0x%08x (port %d to %d)\n", __func__,
630 			       ntohl(hc->sin_remote.sin_addr.s_addr),
631 			       ntohl(sin->sin_addr.s_addr),
632 			       ntohs(hc->sin_remote.sin_port),
633 			       ntohs(sin->sin_port));
634 		hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
635 		hc->sin_remote.sin_port = sin->sin_port;
636 	}
637 }
638 
639 
640 /*
641  * socket stuff
642  */
643 static int
644 l1oip_socket_thread(void *data)
645 {
646 	struct l1oip *hc = (struct l1oip *)data;
647 	int ret = 0;
648 	struct msghdr msg;
649 	struct sockaddr_in sin_rx;
650 	unsigned char *recvbuf;
651 	size_t recvbuf_size = 1500;
652 	int recvlen;
653 	struct socket *socket = NULL;
654 	DECLARE_COMPLETION_ONSTACK(wait);
655 
656 	/* allocate buffer memory */
657 	recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
658 	if (!recvbuf) {
659 		printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
660 		ret = -ENOMEM;
661 		goto fail;
662 	}
663 
664 	/* make daemon */
665 	allow_signal(SIGTERM);
666 
667 	/* create socket */
668 	if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
669 		printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
670 		ret = -EIO;
671 		goto fail;
672 	}
673 
674 	/* set incoming address */
675 	hc->sin_local.sin_family = AF_INET;
676 	hc->sin_local.sin_addr.s_addr = INADDR_ANY;
677 	hc->sin_local.sin_port = htons((unsigned short)hc->localport);
678 
679 	/* set outgoing address */
680 	hc->sin_remote.sin_family = AF_INET;
681 	hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip);
682 	hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport);
683 
684 	/* bind to incoming port */
685 	if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
686 			      sizeof(hc->sin_local))) {
687 		printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
688 		       __func__, hc->localport);
689 		ret = -EINVAL;
690 		goto fail;
691 	}
692 
693 	/* check sk */
694 	if (socket->sk == NULL) {
695 		printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
696 		ret = -EIO;
697 		goto fail;
698 	}
699 
700 	/* build receive message */
701 	msg.msg_name = &sin_rx;
702 	msg.msg_namelen = sizeof(sin_rx);
703 	msg.msg_control = NULL;
704 	msg.msg_controllen = 0;
705 
706 	/* build send message */
707 	hc->sendmsg.msg_name = &hc->sin_remote;
708 	hc->sendmsg.msg_namelen = sizeof(hc->sin_remote);
709 	hc->sendmsg.msg_control = NULL;
710 	hc->sendmsg.msg_controllen = 0;
711 
712 	/* give away socket */
713 	spin_lock(&hc->socket_lock);
714 	hc->socket = socket;
715 	spin_unlock(&hc->socket_lock);
716 
717 	/* read loop */
718 	if (debug & DEBUG_L1OIP_SOCKET)
719 		printk(KERN_DEBUG "%s: socket created and open\n",
720 		       __func__);
721 	while (!signal_pending(current)) {
722 		struct kvec iov = {
723 			.iov_base = recvbuf,
724 			.iov_len = recvbuf_size,
725 		};
726 		recvlen = kernel_recvmsg(socket, &msg, &iov, 1,
727 					 recvbuf_size, 0);
728 		if (recvlen > 0) {
729 			l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
730 		} else {
731 			if (debug & DEBUG_L1OIP_SOCKET)
732 				printk(KERN_WARNING
733 				       "%s: broken pipe on socket\n", __func__);
734 		}
735 	}
736 
737 	/* get socket back, check first if in use, maybe by send function */
738 	spin_lock(&hc->socket_lock);
739 	/* if hc->socket is NULL, it is in use until it is given back */
740 	while (!hc->socket) {
741 		spin_unlock(&hc->socket_lock);
742 		schedule_timeout(HZ / 10);
743 		spin_lock(&hc->socket_lock);
744 	}
745 	hc->socket = NULL;
746 	spin_unlock(&hc->socket_lock);
747 
748 	if (debug & DEBUG_L1OIP_SOCKET)
749 		printk(KERN_DEBUG "%s: socket thread terminating\n",
750 		       __func__);
751 
752 fail:
753 	/* free recvbuf */
754 	kfree(recvbuf);
755 
756 	/* close socket */
757 	if (socket)
758 		sock_release(socket);
759 
760 	/* if we got killed, signal completion */
761 	complete(&hc->socket_complete);
762 	hc->socket_thread = NULL; /* show termination of thread */
763 
764 	if (debug & DEBUG_L1OIP_SOCKET)
765 		printk(KERN_DEBUG "%s: socket thread terminated\n",
766 		       __func__);
767 	return ret;
768 }
769 
770 static void
771 l1oip_socket_close(struct l1oip *hc)
772 {
773 	struct dchannel *dch = hc->chan[hc->d_idx].dch;
774 
775 	/* kill thread */
776 	if (hc->socket_thread) {
777 		if (debug & DEBUG_L1OIP_SOCKET)
778 			printk(KERN_DEBUG "%s: socket thread exists, "
779 			       "killing...\n", __func__);
780 		send_sig(SIGTERM, hc->socket_thread, 0);
781 		wait_for_completion(&hc->socket_complete);
782 	}
783 
784 	/* if active, we send up a PH_DEACTIVATE and deactivate */
785 	if (test_bit(FLG_ACTIVE, &dch->Flags)) {
786 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
787 			printk(KERN_DEBUG "%s: interface become deactivated "
788 			       "due to timeout\n", __func__);
789 		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
790 		_queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
791 			    NULL, GFP_ATOMIC);
792 	}
793 }
794 
795 static int
796 l1oip_socket_open(struct l1oip *hc)
797 {
798 	/* in case of reopen, we need to close first */
799 	l1oip_socket_close(hc);
800 
801 	init_completion(&hc->socket_complete);
802 
803 	/* create receive process */
804 	hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
805 					hc->name);
806 	if (IS_ERR(hc->socket_thread)) {
807 		int err = PTR_ERR(hc->socket_thread);
808 		printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
809 		       __func__, err);
810 		hc->socket_thread = NULL;
811 		sock_release(hc->socket);
812 		return err;
813 	}
814 	if (debug & DEBUG_L1OIP_SOCKET)
815 		printk(KERN_DEBUG "%s: socket thread created\n", __func__);
816 
817 	return 0;
818 }
819 
820 
821 static void
822 l1oip_send_bh(struct work_struct *work)
823 {
824 	struct l1oip *hc = container_of(work, struct l1oip, workq);
825 
826 	if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
827 		printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
828 		       "frame on dchannel\n", __func__);
829 
830 	/* send an empty l1oip frame at D-channel */
831 	l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
832 }
833 
834 
835 /*
836  * timer stuff
837  */
838 static void
839 l1oip_keepalive(struct timer_list *t)
840 {
841 	struct l1oip *hc = from_timer(hc, t, keep_tl);
842 
843 	schedule_work(&hc->workq);
844 }
845 
846 static void
847 l1oip_timeout(struct timer_list *t)
848 {
849 	struct l1oip			*hc = from_timer(hc, t,
850 								  timeout_tl);
851 	struct dchannel		*dch = hc->chan[hc->d_idx].dch;
852 
853 	if (debug & DEBUG_L1OIP_MSG)
854 		printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
855 		       "down.\n", __func__);
856 
857 	hc->timeout_on = 0; /* state that timer must be initialized next time */
858 
859 	/* if timeout, we send up a PH_DEACTIVATE and deactivate */
860 	if (test_bit(FLG_ACTIVE, &dch->Flags)) {
861 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
862 			printk(KERN_DEBUG "%s: interface become deactivated "
863 			       "due to timeout\n", __func__);
864 		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
865 		_queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
866 			    NULL, GFP_ATOMIC);
867 	}
868 
869 	/* if we have ondemand set, we remove ip address */
870 	if (hc->ondemand) {
871 		if (debug & DEBUG_L1OIP_MSG)
872 			printk(KERN_DEBUG "%s: on demand causes ip address to "
873 			       "be removed\n", __func__);
874 		hc->sin_remote.sin_addr.s_addr = 0;
875 	}
876 }
877 
878 
879 /*
880  * message handling
881  */
882 static int
883 handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
884 {
885 	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
886 	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
887 	struct l1oip			*hc = dch->hw;
888 	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
889 	int			ret = -EINVAL;
890 	int			l, ll;
891 	unsigned char		*p;
892 
893 	switch (hh->prim) {
894 	case PH_DATA_REQ:
895 		if (skb->len < 1) {
896 			printk(KERN_WARNING "%s: skb too small\n",
897 			       __func__);
898 			break;
899 		}
900 		if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
901 			printk(KERN_WARNING "%s: skb too large\n",
902 			       __func__);
903 			break;
904 		}
905 		/* send frame */
906 		p = skb->data;
907 		l = skb->len;
908 		while (l) {
909 			ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
910 			l1oip_socket_send(hc, 0, dch->slot, 0,
911 					  hc->chan[dch->slot].tx_counter++, p, ll);
912 			p += ll;
913 			l -= ll;
914 		}
915 		skb_trim(skb, 0);
916 		queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
917 		return 0;
918 	case PH_ACTIVATE_REQ:
919 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
920 			printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
921 			       , __func__, dch->slot, hc->b_num + 1);
922 		skb_trim(skb, 0);
923 		if (test_bit(FLG_ACTIVE, &dch->Flags))
924 			queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
925 		else
926 			queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
927 		return 0;
928 	case PH_DEACTIVATE_REQ:
929 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
930 			printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
931 			       "(1..%d)\n", __func__, dch->slot,
932 			       hc->b_num + 1);
933 		skb_trim(skb, 0);
934 		if (test_bit(FLG_ACTIVE, &dch->Flags))
935 			queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
936 		else
937 			queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
938 		return 0;
939 	}
940 	if (!ret)
941 		dev_kfree_skb(skb);
942 	return ret;
943 }
944 
945 static int
946 channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
947 {
948 	int	ret = 0;
949 	struct l1oip	*hc = dch->hw;
950 
951 	switch (cq->op) {
952 	case MISDN_CTRL_GETOP:
953 		cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
954 			| MISDN_CTRL_GETPEER;
955 		break;
956 	case MISDN_CTRL_SETPEER:
957 		hc->remoteip = (u32)cq->p1;
958 		hc->remoteport = cq->p2 & 0xffff;
959 		hc->localport = cq->p2 >> 16;
960 		if (!hc->remoteport)
961 			hc->remoteport = hc->localport;
962 		if (debug & DEBUG_L1OIP_SOCKET)
963 			printk(KERN_DEBUG "%s: got new ip address from user "
964 			       "space.\n", __func__);
965 		l1oip_socket_open(hc);
966 		break;
967 	case MISDN_CTRL_UNSETPEER:
968 		if (debug & DEBUG_L1OIP_SOCKET)
969 			printk(KERN_DEBUG "%s: removing ip address.\n",
970 			       __func__);
971 		hc->remoteip = 0;
972 		l1oip_socket_open(hc);
973 		break;
974 	case MISDN_CTRL_GETPEER:
975 		if (debug & DEBUG_L1OIP_SOCKET)
976 			printk(KERN_DEBUG "%s: getting ip address.\n",
977 			       __func__);
978 		cq->p1 = hc->remoteip;
979 		cq->p2 = hc->remoteport | (hc->localport << 16);
980 		break;
981 	default:
982 		printk(KERN_WARNING "%s: unknown Op %x\n",
983 		       __func__, cq->op);
984 		ret = -EINVAL;
985 		break;
986 	}
987 	return ret;
988 }
989 
990 static int
991 open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
992 {
993 	if (debug & DEBUG_HW_OPEN)
994 		printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
995 		       dch->dev.id, __builtin_return_address(0));
996 	if (rq->protocol == ISDN_P_NONE)
997 		return -EINVAL;
998 	if ((dch->dev.D.protocol != ISDN_P_NONE) &&
999 	    (dch->dev.D.protocol != rq->protocol)) {
1000 		if (debug & DEBUG_HW_OPEN)
1001 			printk(KERN_WARNING "%s: change protocol %x to %x\n",
1002 			       __func__, dch->dev.D.protocol, rq->protocol);
1003 	}
1004 	if (dch->dev.D.protocol != rq->protocol)
1005 		dch->dev.D.protocol = rq->protocol;
1006 
1007 	if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1008 		_queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
1009 			    0, NULL, GFP_KERNEL);
1010 	}
1011 	rq->ch = &dch->dev.D;
1012 	if (!try_module_get(THIS_MODULE))
1013 		printk(KERN_WARNING "%s:cannot get module\n", __func__);
1014 	return 0;
1015 }
1016 
1017 static int
1018 open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1019 {
1020 	struct bchannel	*bch;
1021 	int		ch;
1022 
1023 	if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
1024 		return -EINVAL;
1025 	if (rq->protocol == ISDN_P_NONE)
1026 		return -EINVAL;
1027 	ch = rq->adr.channel; /* BRI: 1=B1 2=B2  PRI: 1..15,17.. */
1028 	bch = hc->chan[ch].bch;
1029 	if (!bch) {
1030 		printk(KERN_ERR "%s:internal error ch %d has no bch\n",
1031 		       __func__, ch);
1032 		return -EINVAL;
1033 	}
1034 	if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1035 		return -EBUSY; /* b-channel can be only open once */
1036 	bch->ch.protocol = rq->protocol;
1037 	rq->ch = &bch->ch;
1038 	if (!try_module_get(THIS_MODULE))
1039 		printk(KERN_WARNING "%s:cannot get module\n", __func__);
1040 	return 0;
1041 }
1042 
1043 static int
1044 l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1045 {
1046 	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
1047 	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
1048 	struct l1oip			*hc = dch->hw;
1049 	struct channel_req	*rq;
1050 	int			err = 0;
1051 
1052 	if (dch->debug & DEBUG_HW)
1053 		printk(KERN_DEBUG "%s: cmd:%x %p\n",
1054 		       __func__, cmd, arg);
1055 	switch (cmd) {
1056 	case OPEN_CHANNEL:
1057 		rq = arg;
1058 		switch (rq->protocol) {
1059 		case ISDN_P_TE_S0:
1060 		case ISDN_P_NT_S0:
1061 			if (hc->pri) {
1062 				err = -EINVAL;
1063 				break;
1064 			}
1065 			err = open_dchannel(hc, dch, rq);
1066 			break;
1067 		case ISDN_P_TE_E1:
1068 		case ISDN_P_NT_E1:
1069 			if (!hc->pri) {
1070 				err = -EINVAL;
1071 				break;
1072 			}
1073 			err = open_dchannel(hc, dch, rq);
1074 			break;
1075 		default:
1076 			err = open_bchannel(hc, dch, rq);
1077 		}
1078 		break;
1079 	case CLOSE_CHANNEL:
1080 		if (debug & DEBUG_HW_OPEN)
1081 			printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
1082 			       __func__, dch->dev.id,
1083 			       __builtin_return_address(0));
1084 		module_put(THIS_MODULE);
1085 		break;
1086 	case CONTROL_CHANNEL:
1087 		err = channel_dctrl(dch, arg);
1088 		break;
1089 	default:
1090 		if (dch->debug & DEBUG_HW)
1091 			printk(KERN_DEBUG "%s: unknown command %x\n",
1092 			       __func__, cmd);
1093 		err = -EINVAL;
1094 	}
1095 	return err;
1096 }
1097 
1098 static int
1099 handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1100 {
1101 	struct bchannel		*bch = container_of(ch, struct bchannel, ch);
1102 	struct l1oip			*hc = bch->hw;
1103 	int			ret = -EINVAL;
1104 	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
1105 	int			l, ll;
1106 	unsigned char		*p;
1107 
1108 	switch (hh->prim) {
1109 	case PH_DATA_REQ:
1110 		if (skb->len <= 0) {
1111 			printk(KERN_WARNING "%s: skb too small\n",
1112 			       __func__);
1113 			break;
1114 		}
1115 		if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1116 			printk(KERN_WARNING "%s: skb too large\n",
1117 			       __func__);
1118 			break;
1119 		}
1120 		/* check for AIS / ulaw-silence */
1121 		l = skb->len;
1122 		if (!memchr_inv(skb->data, 0xff, l)) {
1123 			if (debug & DEBUG_L1OIP_MSG)
1124 				printk(KERN_DEBUG "%s: got AIS, not sending, "
1125 				       "but counting\n", __func__);
1126 			hc->chan[bch->slot].tx_counter += l;
1127 			skb_trim(skb, 0);
1128 			queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1129 			return 0;
1130 		}
1131 		/* check for silence */
1132 		l = skb->len;
1133 		if (!memchr_inv(skb->data, 0x2a, l)) {
1134 			if (debug & DEBUG_L1OIP_MSG)
1135 				printk(KERN_DEBUG "%s: got silence, not sending"
1136 				       ", but counting\n", __func__);
1137 			hc->chan[bch->slot].tx_counter += l;
1138 			skb_trim(skb, 0);
1139 			queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1140 			return 0;
1141 		}
1142 
1143 		/* send frame */
1144 		p = skb->data;
1145 		l = skb->len;
1146 		while (l) {
1147 			ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
1148 			l1oip_socket_send(hc, hc->codec, bch->slot, 0,
1149 					  hc->chan[bch->slot].tx_counter, p, ll);
1150 			hc->chan[bch->slot].tx_counter += ll;
1151 			p += ll;
1152 			l -= ll;
1153 		}
1154 		skb_trim(skb, 0);
1155 		queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1156 		return 0;
1157 	case PH_ACTIVATE_REQ:
1158 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1159 			printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
1160 			       , __func__, bch->slot, hc->b_num + 1);
1161 		hc->chan[bch->slot].codecstate = 0;
1162 		test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1163 		skb_trim(skb, 0);
1164 		queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1165 		return 0;
1166 	case PH_DEACTIVATE_REQ:
1167 		if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1168 			printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
1169 			       "(1..%d)\n", __func__, bch->slot,
1170 			       hc->b_num + 1);
1171 		test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1172 		skb_trim(skb, 0);
1173 		queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1174 		return 0;
1175 	}
1176 	if (!ret)
1177 		dev_kfree_skb(skb);
1178 	return ret;
1179 }
1180 
1181 static int
1182 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1183 {
1184 	int			ret = 0;
1185 	struct dsp_features	*features =
1186 		(struct dsp_features *)(*((u_long *)&cq->p1));
1187 
1188 	switch (cq->op) {
1189 	case MISDN_CTRL_GETOP:
1190 		cq->op = MISDN_CTRL_HW_FEATURES_OP;
1191 		break;
1192 	case MISDN_CTRL_HW_FEATURES: /* fill features structure */
1193 		if (debug & DEBUG_L1OIP_MSG)
1194 			printk(KERN_DEBUG "%s: HW_FEATURE request\n",
1195 			       __func__);
1196 		/* create confirm */
1197 		features->unclocked = 1;
1198 		features->unordered = 1;
1199 		break;
1200 	default:
1201 		printk(KERN_WARNING "%s: unknown Op %x\n",
1202 		       __func__, cq->op);
1203 		ret = -EINVAL;
1204 		break;
1205 	}
1206 	return ret;
1207 }
1208 
1209 static int
1210 l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1211 {
1212 	struct bchannel	*bch = container_of(ch, struct bchannel, ch);
1213 	int		err = -EINVAL;
1214 
1215 	if (bch->debug & DEBUG_HW)
1216 		printk(KERN_DEBUG "%s: cmd:%x %p\n",
1217 		       __func__, cmd, arg);
1218 	switch (cmd) {
1219 	case CLOSE_CHANNEL:
1220 		test_and_clear_bit(FLG_OPEN, &bch->Flags);
1221 		test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1222 		ch->protocol = ISDN_P_NONE;
1223 		ch->peer = NULL;
1224 		module_put(THIS_MODULE);
1225 		err = 0;
1226 		break;
1227 	case CONTROL_CHANNEL:
1228 		err = channel_bctrl(bch, arg);
1229 		break;
1230 	default:
1231 		printk(KERN_WARNING "%s: unknown prim(%x)\n",
1232 		       __func__, cmd);
1233 	}
1234 	return err;
1235 }
1236 
1237 
1238 /*
1239  * cleanup module and stack
1240  */
1241 static void
1242 release_card(struct l1oip *hc)
1243 {
1244 	int	ch;
1245 
1246 	if (timer_pending(&hc->keep_tl))
1247 		del_timer(&hc->keep_tl);
1248 
1249 	if (timer_pending(&hc->timeout_tl))
1250 		del_timer(&hc->timeout_tl);
1251 
1252 	cancel_work_sync(&hc->workq);
1253 
1254 	if (hc->socket_thread)
1255 		l1oip_socket_close(hc);
1256 
1257 	if (hc->registered && hc->chan[hc->d_idx].dch)
1258 		mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1259 	for (ch = 0; ch < 128; ch++) {
1260 		if (hc->chan[ch].dch) {
1261 			mISDN_freedchannel(hc->chan[ch].dch);
1262 			kfree(hc->chan[ch].dch);
1263 		}
1264 		if (hc->chan[ch].bch) {
1265 			mISDN_freebchannel(hc->chan[ch].bch);
1266 			kfree(hc->chan[ch].bch);
1267 #ifdef REORDER_DEBUG
1268 			if (hc->chan[ch].disorder_skb)
1269 				dev_kfree_skb(hc->chan[ch].disorder_skb);
1270 #endif
1271 		}
1272 	}
1273 
1274 	spin_lock(&l1oip_lock);
1275 	list_del(&hc->list);
1276 	spin_unlock(&l1oip_lock);
1277 
1278 	kfree(hc);
1279 }
1280 
1281 static void
1282 l1oip_cleanup(void)
1283 {
1284 	struct l1oip *hc, *next;
1285 
1286 	list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1287 		release_card(hc);
1288 
1289 	l1oip_4bit_free();
1290 }
1291 
1292 
1293 /*
1294  * module and stack init
1295  */
1296 static int
1297 init_card(struct l1oip *hc, int pri, int bundle)
1298 {
1299 	struct dchannel	*dch;
1300 	struct bchannel	*bch;
1301 	int		ret;
1302 	int		i, ch;
1303 
1304 	spin_lock_init(&hc->socket_lock);
1305 	hc->idx = l1oip_cnt;
1306 	hc->pri = pri;
1307 	hc->d_idx = pri ? 16 : 3;
1308 	hc->b_num = pri ? 30 : 2;
1309 	hc->bundle = bundle;
1310 	if (hc->pri)
1311 		sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1312 	else
1313 		sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1314 
1315 	switch (codec[l1oip_cnt]) {
1316 	case 0: /* as is */
1317 	case 1: /* alaw */
1318 	case 2: /* ulaw */
1319 	case 3: /* 4bit */
1320 		break;
1321 	default:
1322 		printk(KERN_ERR "Codec(%d) not supported.\n",
1323 		       codec[l1oip_cnt]);
1324 		return -EINVAL;
1325 	}
1326 	hc->codec = codec[l1oip_cnt];
1327 	if (debug & DEBUG_L1OIP_INIT)
1328 		printk(KERN_DEBUG "%s: using codec %d\n",
1329 		       __func__, hc->codec);
1330 
1331 	if (id[l1oip_cnt] == 0) {
1332 		printk(KERN_WARNING "Warning: No 'id' value given or "
1333 		       "0, this is highly unsecure. Please use 32 "
1334 		       "bit random number 0x...\n");
1335 	}
1336 	hc->id = id[l1oip_cnt];
1337 	if (debug & DEBUG_L1OIP_INIT)
1338 		printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1339 
1340 	hc->ondemand = ondemand[l1oip_cnt];
1341 	if (hc->ondemand && !hc->id) {
1342 		printk(KERN_ERR "%s: ondemand option only allowed in "
1343 		       "conjunction with non 0 ID\n", __func__);
1344 		return -EINVAL;
1345 	}
1346 
1347 	if (limit[l1oip_cnt])
1348 		hc->b_num = limit[l1oip_cnt];
1349 	if (!pri && hc->b_num > 2) {
1350 		printk(KERN_ERR "Maximum limit for BRI interface is 2 "
1351 		       "channels.\n");
1352 		return -EINVAL;
1353 	}
1354 	if (pri && hc->b_num > 126) {
1355 		printk(KERN_ERR "Maximum limit for PRI interface is 126 "
1356 		       "channels.\n");
1357 		return -EINVAL;
1358 	}
1359 	if (pri && hc->b_num > 30) {
1360 		printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
1361 		       "channels.\n");
1362 		printk(KERN_WARNING "Your selection of %d channels must be "
1363 		       "supported by application.\n", hc->limit);
1364 	}
1365 
1366 	hc->remoteip = ip[l1oip_cnt << 2] << 24
1367 		| ip[(l1oip_cnt << 2) + 1] << 16
1368 		| ip[(l1oip_cnt << 2) + 2] << 8
1369 		| ip[(l1oip_cnt << 2) + 3];
1370 	hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT + l1oip_cnt);
1371 	if (remoteport[l1oip_cnt])
1372 		hc->remoteport = remoteport[l1oip_cnt];
1373 	else
1374 		hc->remoteport = hc->localport;
1375 	if (debug & DEBUG_L1OIP_INIT)
1376 		printk(KERN_DEBUG "%s: using local port %d remote ip "
1377 		       "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1378 		       hc->localport, hc->remoteip >> 24,
1379 		       (hc->remoteip >> 16) & 0xff,
1380 		       (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1381 		       hc->remoteport, hc->ondemand);
1382 
1383 	dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1384 	if (!dch)
1385 		return -ENOMEM;
1386 	dch->debug = debug;
1387 	mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1388 	dch->hw = hc;
1389 	if (pri)
1390 		dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1391 	else
1392 		dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1393 	dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1394 		(1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1395 	dch->dev.D.send = handle_dmsg;
1396 	dch->dev.D.ctrl = l1oip_dctrl;
1397 	dch->dev.nrbchan = hc->b_num;
1398 	dch->slot = hc->d_idx;
1399 	hc->chan[hc->d_idx].dch = dch;
1400 	i = 1;
1401 	for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1402 		if (ch == 15)
1403 			i++;
1404 		bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1405 		if (!bch) {
1406 			printk(KERN_ERR "%s: no memory for bchannel\n",
1407 			       __func__);
1408 			return -ENOMEM;
1409 		}
1410 		bch->nr = i + ch;
1411 		bch->slot = i + ch;
1412 		bch->debug = debug;
1413 		mISDN_initbchannel(bch, MAX_DATA_MEM, 0);
1414 		bch->hw = hc;
1415 		bch->ch.send = handle_bmsg;
1416 		bch->ch.ctrl = l1oip_bctrl;
1417 		bch->ch.nr = i + ch;
1418 		list_add(&bch->ch.list, &dch->dev.bchannels);
1419 		hc->chan[i + ch].bch = bch;
1420 		set_channelmap(bch->nr, dch->dev.channelmap);
1421 	}
1422 	/* TODO: create a parent device for this driver */
1423 	ret = mISDN_register_device(&dch->dev, NULL, hc->name);
1424 	if (ret)
1425 		return ret;
1426 	hc->registered = 1;
1427 
1428 	if (debug & DEBUG_L1OIP_INIT)
1429 		printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
1430 		       __func__, l1oip_cnt + 1);
1431 	ret = l1oip_socket_open(hc);
1432 	if (ret)
1433 		return ret;
1434 
1435 	timer_setup(&hc->keep_tl, l1oip_keepalive, 0);
1436 	hc->keep_tl.expires = jiffies + 2 * HZ; /* two seconds first time */
1437 	add_timer(&hc->keep_tl);
1438 
1439 	timer_setup(&hc->timeout_tl, l1oip_timeout, 0);
1440 	hc->timeout_on = 0; /* state that we have timer off */
1441 
1442 	return 0;
1443 }
1444 
1445 static int __init
1446 l1oip_init(void)
1447 {
1448 	int		pri, bundle;
1449 	struct l1oip		*hc;
1450 	int		ret;
1451 
1452 	printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
1453 	       l1oip_revision);
1454 
1455 	INIT_LIST_HEAD(&l1oip_ilist);
1456 	spin_lock_init(&l1oip_lock);
1457 
1458 	if (l1oip_4bit_alloc(ulaw))
1459 		return -ENOMEM;
1460 
1461 	l1oip_cnt = 0;
1462 	while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
1463 		switch (type[l1oip_cnt] & 0xff) {
1464 		case 1:
1465 			pri = 0;
1466 			bundle = 0;
1467 			break;
1468 		case 2:
1469 			pri = 1;
1470 			bundle = 0;
1471 			break;
1472 		case 3:
1473 			pri = 0;
1474 			bundle = 1;
1475 			break;
1476 		case 4:
1477 			pri = 1;
1478 			bundle = 1;
1479 			break;
1480 		default:
1481 			printk(KERN_ERR "Card type(%d) not supported.\n",
1482 			       type[l1oip_cnt] & 0xff);
1483 			l1oip_cleanup();
1484 			return -EINVAL;
1485 		}
1486 
1487 		if (debug & DEBUG_L1OIP_INIT)
1488 			printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
1489 			       __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1490 			       bundle ? "bundled IP packet for all B-channels" :
1491 			       "separate IP packets for every B-channel");
1492 
1493 		hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1494 		if (!hc) {
1495 			printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1496 			l1oip_cleanup();
1497 			return -ENOMEM;
1498 		}
1499 		INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1500 
1501 		spin_lock(&l1oip_lock);
1502 		list_add_tail(&hc->list, &l1oip_ilist);
1503 		spin_unlock(&l1oip_lock);
1504 
1505 		ret = init_card(hc, pri, bundle);
1506 		if (ret) {
1507 			l1oip_cleanup();
1508 			return ret;
1509 		}
1510 
1511 		l1oip_cnt++;
1512 	}
1513 	printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1514 	return 0;
1515 }
1516 
1517 module_init(l1oip_init);
1518 module_exit(l1oip_cleanup);
1519