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