]>
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 */ | |
eac74af9 KK |
311 | | (hc->pri ? 0x20 : 0x00) /* type */ |
312 | | (hc->id ? 0x10 : 0x00) /* id */ | |
3712b42d KK |
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 | } | |
eac74af9 | 320 | *p++ = (multi == 1) ? 0x80 : 0x00 + channel; /* m-flag, channel */ |
3712b42d KK |
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 */ | |
eac74af9 | 404 | nskb = mI_alloc_skb((remotecodec == 3) ? (len<<1) : len, GFP_ATOMIC); |
3712b42d KK |
405 | if (!nskb) { |
406 | printk(KERN_ERR "%s: No mem for skb.\n", __func__); | |
407 | return; | |
408 | } | |
eac74af9 | 409 | p = skb_put(nskb, (remotecodec == 3) ? (len<<1) : len); |
3712b42d KK |
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 | |
eac74af9 | 461 | queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb); |
3712b42d KK |
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; | |
3dc40393 FS |
666 | unsigned char *recvbuf; |
667 | size_t recvbuf_size = 1500; | |
3712b42d KK |
668 | int recvlen; |
669 | struct socket *socket = NULL; | |
670 | DECLARE_COMPLETION(wait); | |
671 | ||
3dc40393 FS |
672 | /* allocate buffer memory */ |
673 | recvbuf = kmalloc(recvbuf_size, GFP_KERNEL); | |
674 | if (!recvbuf) { | |
675 | printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__); | |
676 | ret = -ENOMEM; | |
677 | goto fail; | |
678 | } | |
679 | ||
3712b42d KK |
680 | /* make daemon */ |
681 | allow_signal(SIGTERM); | |
682 | ||
683 | /* create socket */ | |
684 | if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) { | |
685 | printk(KERN_ERR "%s: Failed to create socket.\n", __func__); | |
3dc40393 FS |
686 | ret = -EIO; |
687 | goto fail; | |
3712b42d KK |
688 | } |
689 | ||
690 | /* set incoming address */ | |
691 | hc->sin_local.sin_family = AF_INET; | |
692 | hc->sin_local.sin_addr.s_addr = INADDR_ANY; | |
693 | hc->sin_local.sin_port = htons((unsigned short)hc->localport); | |
694 | ||
695 | /* set outgoing address */ | |
696 | hc->sin_remote.sin_family = AF_INET; | |
697 | hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip); | |
698 | hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport); | |
699 | ||
700 | /* bind to incomming port */ | |
701 | if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local, | |
702 | sizeof(hc->sin_local))) { | |
703 | printk(KERN_ERR "%s: Failed to bind socket to port %d.\n", | |
704 | __func__, hc->localport); | |
705 | ret = -EINVAL; | |
706 | goto fail; | |
707 | } | |
708 | ||
709 | /* check sk */ | |
710 | if (socket->sk == NULL) { | |
711 | printk(KERN_ERR "%s: socket->sk == NULL\n", __func__); | |
712 | ret = -EIO; | |
713 | goto fail; | |
714 | } | |
715 | ||
716 | /* build receive message */ | |
717 | msg.msg_name = &sin_rx; | |
718 | msg.msg_namelen = sizeof(sin_rx); | |
719 | msg.msg_control = NULL; | |
720 | msg.msg_controllen = 0; | |
721 | msg.msg_iov = &iov; | |
722 | msg.msg_iovlen = 1; | |
723 | ||
724 | /* build send message */ | |
725 | hc->sendmsg.msg_name = &hc->sin_remote; | |
726 | hc->sendmsg.msg_namelen = sizeof(hc->sin_remote); | |
727 | hc->sendmsg.msg_control = NULL; | |
728 | hc->sendmsg.msg_controllen = 0; | |
729 | hc->sendmsg.msg_iov = &hc->sendiov; | |
730 | hc->sendmsg.msg_iovlen = 1; | |
731 | ||
732 | /* give away socket */ | |
733 | spin_lock(&hc->socket_lock); | |
734 | hc->socket = socket; | |
735 | spin_unlock(&hc->socket_lock); | |
736 | ||
737 | /* read loop */ | |
738 | if (debug & DEBUG_L1OIP_SOCKET) | |
739 | printk(KERN_DEBUG "%s: socket created and open\n", | |
740 | __func__); | |
741 | while (!signal_pending(current)) { | |
742 | iov.iov_base = recvbuf; | |
3dc40393 | 743 | iov.iov_len = recvbuf_size; |
3712b42d KK |
744 | oldfs = get_fs(); |
745 | set_fs(KERNEL_DS); | |
3dc40393 | 746 | recvlen = sock_recvmsg(socket, &msg, recvbuf_size, 0); |
3712b42d KK |
747 | set_fs(oldfs); |
748 | if (recvlen > 0) { | |
749 | l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen); | |
750 | } else { | |
751 | if (debug & DEBUG_L1OIP_SOCKET) | |
eac74af9 KK |
752 | printk(KERN_WARNING |
753 | "%s: broken pipe on socket\n", __func__); | |
3712b42d KK |
754 | } |
755 | } | |
756 | ||
757 | /* get socket back, check first if in use, maybe by send function */ | |
758 | spin_lock(&hc->socket_lock); | |
759 | /* if hc->socket is NULL, it is in use until it is given back */ | |
760 | while (!hc->socket) { | |
761 | spin_unlock(&hc->socket_lock); | |
762 | schedule_timeout(HZ/10); | |
763 | spin_lock(&hc->socket_lock); | |
764 | } | |
765 | hc->socket = NULL; | |
766 | spin_unlock(&hc->socket_lock); | |
767 | ||
768 | if (debug & DEBUG_L1OIP_SOCKET) | |
769 | printk(KERN_DEBUG "%s: socket thread terminating\n", | |
770 | __func__); | |
771 | ||
772 | fail: | |
3dc40393 FS |
773 | /* free recvbuf */ |
774 | kfree(recvbuf); | |
775 | ||
3712b42d KK |
776 | /* close socket */ |
777 | if (socket) | |
778 | sock_release(socket); | |
779 | ||
780 | /* if we got killed, signal completion */ | |
781 | complete(&hc->socket_complete); | |
782 | hc->socket_thread = NULL; /* show termination of thread */ | |
783 | ||
784 | if (debug & DEBUG_L1OIP_SOCKET) | |
785 | printk(KERN_DEBUG "%s: socket thread terminated\n", | |
786 | __func__); | |
787 | return ret; | |
788 | } | |
789 | ||
790 | static void | |
791 | l1oip_socket_close(struct l1oip *hc) | |
792 | { | |
1b4d3312 AE |
793 | struct dchannel *dch = hc->chan[hc->d_idx].dch; |
794 | ||
3712b42d KK |
795 | /* kill thread */ |
796 | if (hc->socket_thread) { | |
797 | if (debug & DEBUG_L1OIP_SOCKET) | |
798 | printk(KERN_DEBUG "%s: socket thread exists, " | |
799 | "killing...\n", __func__); | |
800 | send_sig(SIGTERM, hc->socket_thread, 0); | |
801 | wait_for_completion(&hc->socket_complete); | |
802 | } | |
1b4d3312 AE |
803 | |
804 | /* if active, we send up a PH_DEACTIVATE and deactivate */ | |
805 | if (test_bit(FLG_ACTIVE, &dch->Flags)) { | |
806 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
807 | printk(KERN_DEBUG "%s: interface become deactivated " | |
808 | "due to timeout\n", __func__); | |
809 | test_and_clear_bit(FLG_ACTIVE, &dch->Flags); | |
810 | _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0, | |
811 | NULL, GFP_ATOMIC); | |
812 | } | |
3712b42d KK |
813 | } |
814 | ||
815 | static int | |
816 | l1oip_socket_open(struct l1oip *hc) | |
817 | { | |
818 | /* in case of reopen, we need to close first */ | |
819 | l1oip_socket_close(hc); | |
820 | ||
821 | init_completion(&hc->socket_complete); | |
822 | ||
823 | /* create receive process */ | |
824 | hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s", | |
825 | hc->name); | |
826 | if (IS_ERR(hc->socket_thread)) { | |
827 | int err = PTR_ERR(hc->socket_thread); | |
828 | printk(KERN_ERR "%s: Failed (%d) to create socket process.\n", | |
829 | __func__, err); | |
830 | hc->socket_thread = NULL; | |
831 | sock_release(hc->socket); | |
832 | return err; | |
833 | } | |
834 | if (debug & DEBUG_L1OIP_SOCKET) | |
835 | printk(KERN_DEBUG "%s: socket thread created\n", __func__); | |
836 | ||
837 | return 0; | |
838 | } | |
839 | ||
840 | ||
841 | static void | |
842 | l1oip_send_bh(struct work_struct *work) | |
843 | { | |
844 | struct l1oip *hc = container_of(work, struct l1oip, workq); | |
845 | ||
846 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
847 | printk(KERN_DEBUG "%s: keepalive timer expired, sending empty " | |
848 | "frame on dchannel\n", __func__); | |
849 | ||
850 | /* send an empty l1oip frame at D-channel */ | |
851 | l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0); | |
852 | } | |
853 | ||
854 | ||
855 | /* | |
856 | * timer stuff | |
857 | */ | |
858 | static void | |
859 | l1oip_keepalive(void *data) | |
860 | { | |
861 | struct l1oip *hc = (struct l1oip *)data; | |
862 | ||
863 | schedule_work(&hc->workq); | |
864 | } | |
865 | ||
866 | static void | |
867 | l1oip_timeout(void *data) | |
868 | { | |
869 | struct l1oip *hc = (struct l1oip *)data; | |
870 | struct dchannel *dch = hc->chan[hc->d_idx].dch; | |
871 | ||
872 | if (debug & DEBUG_L1OIP_MSG) | |
873 | printk(KERN_DEBUG "%s: timeout timer expired, turn layer one " | |
874 | "down.\n", __func__); | |
875 | ||
876 | hc->timeout_on = 0; /* state that timer must be initialized next time */ | |
877 | ||
878 | /* if timeout, we send up a PH_DEACTIVATE and deactivate */ | |
879 | if (test_bit(FLG_ACTIVE, &dch->Flags)) { | |
880 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
881 | printk(KERN_DEBUG "%s: interface become deactivated " | |
882 | "due to timeout\n", __func__); | |
883 | test_and_clear_bit(FLG_ACTIVE, &dch->Flags); | |
884 | _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0, | |
885 | NULL, GFP_ATOMIC); | |
886 | } | |
887 | ||
888 | /* if we have ondemand set, we remove ip address */ | |
889 | if (hc->ondemand) { | |
890 | if (debug & DEBUG_L1OIP_MSG) | |
891 | printk(KERN_DEBUG "%s: on demand causes ip address to " | |
892 | "be removed\n", __func__); | |
893 | hc->sin_remote.sin_addr.s_addr = 0; | |
894 | } | |
895 | } | |
896 | ||
897 | ||
898 | /* | |
899 | * message handling | |
900 | */ | |
901 | static int | |
902 | handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb) | |
903 | { | |
904 | struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D); | |
905 | struct dchannel *dch = container_of(dev, struct dchannel, dev); | |
906 | struct l1oip *hc = dch->hw; | |
907 | struct mISDNhead *hh = mISDN_HEAD_P(skb); | |
908 | int ret = -EINVAL; | |
909 | int l, ll; | |
910 | unsigned char *p; | |
911 | ||
912 | switch (hh->prim) { | |
913 | case PH_DATA_REQ: | |
914 | if (skb->len < 1) { | |
915 | printk(KERN_WARNING "%s: skb too small\n", | |
916 | __func__); | |
917 | break; | |
918 | } | |
919 | if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) { | |
920 | printk(KERN_WARNING "%s: skb too large\n", | |
921 | __func__); | |
922 | break; | |
923 | } | |
924 | /* send frame */ | |
925 | p = skb->data; | |
926 | l = skb->len; | |
927 | while (l) { | |
eac74af9 | 928 | ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME; |
3712b42d KK |
929 | l1oip_socket_send(hc, 0, dch->slot, 0, |
930 | hc->chan[dch->slot].tx_counter++, p, ll); | |
931 | p += ll; | |
932 | l -= ll; | |
933 | } | |
934 | skb_trim(skb, 0); | |
935 | queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb); | |
936 | return 0; | |
937 | case PH_ACTIVATE_REQ: | |
938 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
939 | printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n" | |
940 | , __func__, dch->slot, hc->b_num+1); | |
941 | skb_trim(skb, 0); | |
942 | if (test_bit(FLG_ACTIVE, &dch->Flags)) | |
943 | queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb); | |
944 | else | |
945 | queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb); | |
946 | return 0; | |
947 | case PH_DEACTIVATE_REQ: | |
948 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
949 | printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d " | |
950 | "(1..%d)\n", __func__, dch->slot, | |
951 | hc->b_num+1); | |
952 | skb_trim(skb, 0); | |
953 | if (test_bit(FLG_ACTIVE, &dch->Flags)) | |
954 | queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb); | |
955 | else | |
956 | queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb); | |
957 | return 0; | |
958 | } | |
959 | if (!ret) | |
960 | dev_kfree_skb(skb); | |
961 | return ret; | |
962 | } | |
963 | ||
964 | static int | |
965 | channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq) | |
966 | { | |
967 | int ret = 0; | |
968 | struct l1oip *hc = dch->hw; | |
969 | ||
970 | switch (cq->op) { | |
971 | case MISDN_CTRL_GETOP: | |
1b4d3312 AE |
972 | cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER |
973 | | MISDN_CTRL_GETPEER; | |
3712b42d KK |
974 | break; |
975 | case MISDN_CTRL_SETPEER: | |
976 | hc->remoteip = (u32)cq->p1; | |
977 | hc->remoteport = cq->p2 & 0xffff; | |
978 | hc->localport = cq->p2 >> 16; | |
979 | if (!hc->remoteport) | |
980 | hc->remoteport = hc->localport; | |
981 | if (debug & DEBUG_L1OIP_SOCKET) | |
982 | printk(KERN_DEBUG "%s: got new ip address from user " | |
983 | "space.\n", __func__); | |
984 | l1oip_socket_open(hc); | |
985 | break; | |
986 | case MISDN_CTRL_UNSETPEER: | |
987 | if (debug & DEBUG_L1OIP_SOCKET) | |
988 | printk(KERN_DEBUG "%s: removing ip address.\n", | |
989 | __func__); | |
990 | hc->remoteip = 0; | |
991 | l1oip_socket_open(hc); | |
992 | break; | |
1b4d3312 AE |
993 | case MISDN_CTRL_GETPEER: |
994 | if (debug & DEBUG_L1OIP_SOCKET) | |
995 | printk(KERN_DEBUG "%s: getting ip address.\n", | |
996 | __func__); | |
1b36c78f | 997 | cq->p1 = hc->remoteip; |
1b4d3312 AE |
998 | cq->p2 = hc->remoteport | (hc->localport << 16); |
999 | break; | |
3712b42d KK |
1000 | default: |
1001 | printk(KERN_WARNING "%s: unknown Op %x\n", | |
1002 | __func__, cq->op); | |
1003 | ret = -EINVAL; | |
1004 | break; | |
1005 | } | |
1006 | return ret; | |
1007 | } | |
1008 | ||
1009 | static int | |
1010 | open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq) | |
1011 | { | |
1012 | if (debug & DEBUG_HW_OPEN) | |
1013 | printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__, | |
1014 | dch->dev.id, __builtin_return_address(0)); | |
1015 | if (rq->protocol == ISDN_P_NONE) | |
1016 | return -EINVAL; | |
1017 | if ((dch->dev.D.protocol != ISDN_P_NONE) && | |
1018 | (dch->dev.D.protocol != rq->protocol)) { | |
1019 | if (debug & DEBUG_HW_OPEN) | |
1020 | printk(KERN_WARNING "%s: change protocol %x to %x\n", | |
1021 | __func__, dch->dev.D.protocol, rq->protocol); | |
1022 | } | |
1023 | if (dch->dev.D.protocol != rq->protocol) | |
1024 | dch->dev.D.protocol = rq->protocol; | |
1025 | ||
1026 | if (test_bit(FLG_ACTIVE, &dch->Flags)) { | |
1027 | _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, | |
1028 | 0, NULL, GFP_KERNEL); | |
1029 | } | |
1030 | rq->ch = &dch->dev.D; | |
1031 | if (!try_module_get(THIS_MODULE)) | |
1032 | printk(KERN_WARNING "%s:cannot get module\n", __func__); | |
1033 | return 0; | |
1034 | } | |
1035 | ||
1036 | static int | |
1037 | open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq) | |
1038 | { | |
1039 | struct bchannel *bch; | |
1040 | int ch; | |
1041 | ||
ff4cc1de | 1042 | if (!test_channelmap(rq->adr.channel, dch->dev.channelmap)) |
3712b42d KK |
1043 | return -EINVAL; |
1044 | if (rq->protocol == ISDN_P_NONE) | |
1045 | return -EINVAL; | |
1046 | ch = rq->adr.channel; /* BRI: 1=B1 2=B2 PRI: 1..15,17.. */ | |
1047 | bch = hc->chan[ch].bch; | |
1048 | if (!bch) { | |
1049 | printk(KERN_ERR "%s:internal error ch %d has no bch\n", | |
1050 | __func__, ch); | |
1051 | return -EINVAL; | |
1052 | } | |
1053 | if (test_and_set_bit(FLG_OPEN, &bch->Flags)) | |
1054 | return -EBUSY; /* b-channel can be only open once */ | |
1055 | bch->ch.protocol = rq->protocol; | |
1056 | rq->ch = &bch->ch; | |
1057 | if (!try_module_get(THIS_MODULE)) | |
1058 | printk(KERN_WARNING "%s:cannot get module\n", __func__); | |
1059 | return 0; | |
1060 | } | |
1061 | ||
1062 | static int | |
1063 | l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg) | |
1064 | { | |
1065 | struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D); | |
1066 | struct dchannel *dch = container_of(dev, struct dchannel, dev); | |
1067 | struct l1oip *hc = dch->hw; | |
1068 | struct channel_req *rq; | |
1069 | int err = 0; | |
1070 | ||
1071 | if (dch->debug & DEBUG_HW) | |
1072 | printk(KERN_DEBUG "%s: cmd:%x %p\n", | |
1073 | __func__, cmd, arg); | |
1074 | switch (cmd) { | |
1075 | case OPEN_CHANNEL: | |
1076 | rq = arg; | |
1077 | switch (rq->protocol) { | |
1078 | case ISDN_P_TE_S0: | |
1079 | case ISDN_P_NT_S0: | |
1080 | if (hc->pri) { | |
1081 | err = -EINVAL; | |
1082 | break; | |
1083 | } | |
1084 | err = open_dchannel(hc, dch, rq); | |
1085 | break; | |
1086 | case ISDN_P_TE_E1: | |
1087 | case ISDN_P_NT_E1: | |
1088 | if (!hc->pri) { | |
1089 | err = -EINVAL; | |
1090 | break; | |
1091 | } | |
1092 | err = open_dchannel(hc, dch, rq); | |
1093 | break; | |
1094 | default: | |
1095 | err = open_bchannel(hc, dch, rq); | |
1096 | } | |
1097 | break; | |
1098 | case CLOSE_CHANNEL: | |
1099 | if (debug & DEBUG_HW_OPEN) | |
1100 | printk(KERN_DEBUG "%s: dev(%d) close from %p\n", | |
1101 | __func__, dch->dev.id, | |
1102 | __builtin_return_address(0)); | |
1103 | module_put(THIS_MODULE); | |
1104 | break; | |
1105 | case CONTROL_CHANNEL: | |
1106 | err = channel_dctrl(dch, arg); | |
1107 | break; | |
1108 | default: | |
1109 | if (dch->debug & DEBUG_HW) | |
1110 | printk(KERN_DEBUG "%s: unknown command %x\n", | |
1111 | __func__, cmd); | |
1112 | err = -EINVAL; | |
1113 | } | |
1114 | return err; | |
1115 | } | |
1116 | ||
1117 | static int | |
1118 | handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb) | |
1119 | { | |
1120 | struct bchannel *bch = container_of(ch, struct bchannel, ch); | |
1121 | struct l1oip *hc = bch->hw; | |
1122 | int ret = -EINVAL; | |
1123 | struct mISDNhead *hh = mISDN_HEAD_P(skb); | |
1124 | int l, ll, i; | |
1125 | unsigned char *p; | |
1126 | ||
1127 | switch (hh->prim) { | |
1128 | case PH_DATA_REQ: | |
1129 | if (skb->len <= 0) { | |
1130 | printk(KERN_WARNING "%s: skb too small\n", | |
1131 | __func__); | |
1132 | break; | |
1133 | } | |
1134 | if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) { | |
1135 | printk(KERN_WARNING "%s: skb too large\n", | |
1136 | __func__); | |
1137 | break; | |
1138 | } | |
1139 | /* check for AIS / ulaw-silence */ | |
1140 | p = skb->data; | |
1141 | l = skb->len; | |
1142 | for (i = 0; i < l; i++) { | |
1143 | if (*p++ != 0xff) | |
1144 | break; | |
1145 | } | |
1146 | if (i == l) { | |
1147 | if (debug & DEBUG_L1OIP_MSG) | |
1148 | printk(KERN_DEBUG "%s: got AIS, not sending, " | |
1149 | "but counting\n", __func__); | |
1150 | hc->chan[bch->slot].tx_counter += l; | |
1151 | skb_trim(skb, 0); | |
1152 | queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb); | |
1153 | return 0; | |
1154 | } | |
1155 | /* check for silence */ | |
1156 | p = skb->data; | |
1157 | l = skb->len; | |
1158 | for (i = 0; i < l; i++) { | |
1159 | if (*p++ != 0x2a) | |
1160 | break; | |
1161 | } | |
1162 | if (i == l) { | |
1163 | if (debug & DEBUG_L1OIP_MSG) | |
1164 | printk(KERN_DEBUG "%s: got silence, not sending" | |
1165 | ", but counting\n", __func__); | |
1166 | hc->chan[bch->slot].tx_counter += l; | |
1167 | skb_trim(skb, 0); | |
1168 | queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb); | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | /* send frame */ | |
1173 | p = skb->data; | |
1174 | l = skb->len; | |
1175 | while (l) { | |
eac74af9 | 1176 | ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME; |
3712b42d KK |
1177 | l1oip_socket_send(hc, hc->codec, bch->slot, 0, |
1178 | hc->chan[bch->slot].tx_counter, p, ll); | |
1179 | hc->chan[bch->slot].tx_counter += ll; | |
1180 | p += ll; | |
1181 | l -= ll; | |
1182 | } | |
1183 | skb_trim(skb, 0); | |
1184 | queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb); | |
1185 | return 0; | |
1186 | case PH_ACTIVATE_REQ: | |
1187 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
1188 | printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n" | |
1189 | , __func__, bch->slot, hc->b_num+1); | |
1190 | hc->chan[bch->slot].codecstate = 0; | |
1191 | test_and_set_bit(FLG_ACTIVE, &bch->Flags); | |
1192 | skb_trim(skb, 0); | |
1193 | queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb); | |
1194 | return 0; | |
1195 | case PH_DEACTIVATE_REQ: | |
1196 | if (debug & (DEBUG_L1OIP_MSG|DEBUG_L1OIP_SOCKET)) | |
1197 | printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d " | |
1198 | "(1..%d)\n", __func__, bch->slot, | |
1199 | hc->b_num+1); | |
1200 | test_and_clear_bit(FLG_ACTIVE, &bch->Flags); | |
1201 | skb_trim(skb, 0); | |
1202 | queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb); | |
1203 | return 0; | |
1204 | } | |
1205 | if (!ret) | |
1206 | dev_kfree_skb(skb); | |
1207 | return ret; | |
1208 | } | |
1209 | ||
1210 | static int | |
1211 | channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq) | |
1212 | { | |
1213 | int ret = 0; | |
1214 | struct dsp_features *features = | |
1215 | (struct dsp_features *)(*((u_long *)&cq->p1)); | |
1216 | ||
1217 | switch (cq->op) { | |
1218 | case MISDN_CTRL_GETOP: | |
1219 | cq->op = MISDN_CTRL_HW_FEATURES_OP; | |
1220 | break; | |
1221 | case MISDN_CTRL_HW_FEATURES: /* fill features structure */ | |
1222 | if (debug & DEBUG_L1OIP_MSG) | |
1223 | printk(KERN_DEBUG "%s: HW_FEATURE request\n", | |
1224 | __func__); | |
1225 | /* create confirm */ | |
1226 | features->unclocked = 1; | |
1227 | features->unordered = 1; | |
1228 | break; | |
1229 | default: | |
1230 | printk(KERN_WARNING "%s: unknown Op %x\n", | |
1231 | __func__, cq->op); | |
1232 | ret = -EINVAL; | |
1233 | break; | |
1234 | } | |
1235 | return ret; | |
1236 | } | |
1237 | ||
1238 | static int | |
1239 | l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg) | |
1240 | { | |
1241 | struct bchannel *bch = container_of(ch, struct bchannel, ch); | |
1242 | int err = -EINVAL; | |
1243 | ||
1244 | if (bch->debug & DEBUG_HW) | |
1245 | printk(KERN_DEBUG "%s: cmd:%x %p\n", | |
1246 | __func__, cmd, arg); | |
1247 | switch (cmd) { | |
1248 | case CLOSE_CHANNEL: | |
1249 | test_and_clear_bit(FLG_OPEN, &bch->Flags); | |
1250 | test_and_clear_bit(FLG_ACTIVE, &bch->Flags); | |
1251 | ch->protocol = ISDN_P_NONE; | |
1252 | ch->peer = NULL; | |
1253 | module_put(THIS_MODULE); | |
1254 | err = 0; | |
1255 | break; | |
1256 | case CONTROL_CHANNEL: | |
1257 | err = channel_bctrl(bch, arg); | |
1258 | break; | |
1259 | default: | |
1260 | printk(KERN_WARNING "%s: unknown prim(%x)\n", | |
1261 | __func__, cmd); | |
1262 | } | |
1263 | return err; | |
1264 | } | |
1265 | ||
1266 | ||
1267 | /* | |
1268 | * cleanup module and stack | |
1269 | */ | |
1270 | static void | |
1271 | release_card(struct l1oip *hc) | |
1272 | { | |
1273 | int ch; | |
1274 | ||
1275 | if (timer_pending(&hc->keep_tl)) | |
1276 | del_timer(&hc->keep_tl); | |
1277 | ||
1278 | if (timer_pending(&hc->timeout_tl)) | |
1279 | del_timer(&hc->timeout_tl); | |
1280 | ||
1281 | if (hc->socket_thread) | |
1282 | l1oip_socket_close(hc); | |
1283 | ||
1284 | if (hc->registered && hc->chan[hc->d_idx].dch) | |
1285 | mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev); | |
1286 | for (ch = 0; ch < 128; ch++) { | |
1287 | if (hc->chan[ch].dch) { | |
1288 | mISDN_freedchannel(hc->chan[ch].dch); | |
1289 | kfree(hc->chan[ch].dch); | |
1290 | } | |
1291 | if (hc->chan[ch].bch) { | |
1292 | mISDN_freebchannel(hc->chan[ch].bch); | |
1293 | kfree(hc->chan[ch].bch); | |
1294 | #ifdef REORDER_DEBUG | |
1295 | if (hc->chan[ch].disorder_skb) | |
1296 | dev_kfree_skb(hc->chan[ch].disorder_skb); | |
1297 | #endif | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | spin_lock(&l1oip_lock); | |
1302 | list_del(&hc->list); | |
1303 | spin_unlock(&l1oip_lock); | |
1304 | ||
1305 | kfree(hc); | |
1306 | } | |
1307 | ||
1308 | static void | |
1309 | l1oip_cleanup(void) | |
1310 | { | |
1311 | struct l1oip *hc, *next; | |
1312 | ||
1313 | list_for_each_entry_safe(hc, next, &l1oip_ilist, list) | |
1314 | release_card(hc); | |
1315 | ||
1316 | l1oip_4bit_free(); | |
1317 | } | |
1318 | ||
1319 | ||
1320 | /* | |
1321 | * module and stack init | |
1322 | */ | |
1323 | static int | |
1324 | init_card(struct l1oip *hc, int pri, int bundle) | |
1325 | { | |
1326 | struct dchannel *dch; | |
1327 | struct bchannel *bch; | |
1328 | int ret; | |
1329 | int i, ch; | |
1330 | ||
1331 | spin_lock_init(&hc->socket_lock); | |
1332 | hc->idx = l1oip_cnt; | |
1333 | hc->pri = pri; | |
eac74af9 KK |
1334 | hc->d_idx = pri ? 16 : 3; |
1335 | hc->b_num = pri ? 30 : 2; | |
3712b42d KK |
1336 | hc->bundle = bundle; |
1337 | if (hc->pri) | |
1338 | sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1); | |
1339 | else | |
1340 | sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1); | |
1341 | ||
1342 | switch (codec[l1oip_cnt]) { | |
1343 | case 0: /* as is */ | |
1344 | case 1: /* alaw */ | |
1345 | case 2: /* ulaw */ | |
1346 | case 3: /* 4bit */ | |
1347 | break; | |
1348 | default: | |
1349 | printk(KERN_ERR "Codec(%d) not supported.\n", | |
1350 | codec[l1oip_cnt]); | |
1351 | return -EINVAL; | |
1352 | } | |
1353 | hc->codec = codec[l1oip_cnt]; | |
1354 | if (debug & DEBUG_L1OIP_INIT) | |
1355 | printk(KERN_DEBUG "%s: using codec %d\n", | |
1356 | __func__, hc->codec); | |
1357 | ||
1358 | if (id[l1oip_cnt] == 0) { | |
1359 | printk(KERN_WARNING "Warning: No 'id' value given or " | |
1360 | "0, this is highly unsecure. Please use 32 " | |
1361 | "bit randmom number 0x...\n"); | |
1362 | } | |
1363 | hc->id = id[l1oip_cnt]; | |
1364 | if (debug & DEBUG_L1OIP_INIT) | |
1365 | printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id); | |
1366 | ||
1367 | hc->ondemand = ondemand[l1oip_cnt]; | |
1368 | if (hc->ondemand && !hc->id) { | |
1369 | printk(KERN_ERR "%s: ondemand option only allowed in " | |
1370 | "conjunction with non 0 ID\n", __func__); | |
1371 | return -EINVAL; | |
1372 | } | |
1373 | ||
1374 | if (limit[l1oip_cnt]) | |
1375 | hc->b_num = limit[l1oip_cnt]; | |
1376 | if (!pri && hc->b_num > 2) { | |
1377 | printk(KERN_ERR "Maximum limit for BRI interface is 2 " | |
1378 | "channels.\n"); | |
1379 | return -EINVAL; | |
1380 | } | |
1381 | if (pri && hc->b_num > 126) { | |
1382 | printk(KERN_ERR "Maximum limit for PRI interface is 126 " | |
1383 | "channels.\n"); | |
1384 | return -EINVAL; | |
1385 | } | |
1386 | if (pri && hc->b_num > 30) { | |
1387 | printk(KERN_WARNING "Maximum limit for BRI interface is 30 " | |
1388 | "channels.\n"); | |
1389 | printk(KERN_WARNING "Your selection of %d channels must be " | |
1390 | "supported by application.\n", hc->limit); | |
1391 | } | |
1392 | ||
1393 | hc->remoteip = ip[l1oip_cnt<<2] << 24 | |
1394 | | ip[(l1oip_cnt<<2)+1] << 16 | |
1395 | | ip[(l1oip_cnt<<2)+2] << 8 | |
1396 | | ip[(l1oip_cnt<<2)+3]; | |
1397 | hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT+l1oip_cnt); | |
1398 | if (remoteport[l1oip_cnt]) | |
1399 | hc->remoteport = remoteport[l1oip_cnt]; | |
1400 | else | |
1401 | hc->remoteport = hc->localport; | |
1402 | if (debug & DEBUG_L1OIP_INIT) | |
1403 | printk(KERN_DEBUG "%s: using local port %d remote ip " | |
1404 | "%d.%d.%d.%d port %d ondemand %d\n", __func__, | |
1405 | hc->localport, hc->remoteip >> 24, | |
1406 | (hc->remoteip >> 16) & 0xff, | |
1407 | (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff, | |
1408 | hc->remoteport, hc->ondemand); | |
1409 | ||
1410 | dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL); | |
1411 | if (!dch) | |
1412 | return -ENOMEM; | |
1413 | dch->debug = debug; | |
1414 | mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL); | |
1415 | dch->hw = hc; | |
1416 | if (pri) | |
1417 | dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1); | |
1418 | else | |
1419 | dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0); | |
1420 | dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) | | |
1421 | (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK)); | |
1422 | dch->dev.D.send = handle_dmsg; | |
1423 | dch->dev.D.ctrl = l1oip_dctrl; | |
1424 | dch->dev.nrbchan = hc->b_num; | |
1425 | dch->slot = hc->d_idx; | |
1426 | hc->chan[hc->d_idx].dch = dch; | |
1427 | i = 1; | |
1428 | for (ch = 0; ch < dch->dev.nrbchan; ch++) { | |
1429 | if (ch == 15) | |
1430 | i++; | |
1431 | bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL); | |
1432 | if (!bch) { | |
1433 | printk(KERN_ERR "%s: no memory for bchannel\n", | |
1434 | __func__); | |
1435 | return -ENOMEM; | |
1436 | } | |
1437 | bch->nr = i + ch; | |
1438 | bch->slot = i + ch; | |
1439 | bch->debug = debug; | |
1440 | mISDN_initbchannel(bch, MAX_DATA_MEM); | |
1441 | bch->hw = hc; | |
1442 | bch->ch.send = handle_bmsg; | |
1443 | bch->ch.ctrl = l1oip_bctrl; | |
1444 | bch->ch.nr = i + ch; | |
1445 | list_add(&bch->ch.list, &dch->dev.bchannels); | |
1446 | hc->chan[i + ch].bch = bch; | |
ff4cc1de | 1447 | set_channelmap(bch->nr, dch->dev.channelmap); |
3712b42d | 1448 | } |
b36b654a MU |
1449 | /* TODO: create a parent device for this driver */ |
1450 | ret = mISDN_register_device(&dch->dev, NULL, hc->name); | |
3712b42d KK |
1451 | if (ret) |
1452 | return ret; | |
1453 | hc->registered = 1; | |
1454 | ||
1455 | if (debug & DEBUG_L1OIP_INIT) | |
1456 | printk(KERN_DEBUG "%s: Setting up network card(%d)\n", | |
1457 | __func__, l1oip_cnt + 1); | |
1458 | ret = l1oip_socket_open(hc); | |
1459 | if (ret) | |
1460 | return ret; | |
1461 | ||
1462 | hc->keep_tl.function = (void *)l1oip_keepalive; | |
1463 | hc->keep_tl.data = (ulong)hc; | |
1464 | init_timer(&hc->keep_tl); | |
1465 | hc->keep_tl.expires = jiffies + 2*HZ; /* two seconds first time */ | |
1466 | add_timer(&hc->keep_tl); | |
1467 | ||
1468 | hc->timeout_tl.function = (void *)l1oip_timeout; | |
1469 | hc->timeout_tl.data = (ulong)hc; | |
1470 | init_timer(&hc->timeout_tl); | |
1471 | hc->timeout_on = 0; /* state that we have timer off */ | |
1472 | ||
1473 | return 0; | |
1474 | } | |
1475 | ||
1476 | static int __init | |
1477 | l1oip_init(void) | |
1478 | { | |
1479 | int pri, bundle; | |
1480 | struct l1oip *hc; | |
1481 | int ret; | |
1482 | ||
1483 | printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n", | |
1484 | l1oip_revision); | |
1485 | ||
1486 | INIT_LIST_HEAD(&l1oip_ilist); | |
1487 | spin_lock_init(&l1oip_lock); | |
1488 | ||
1489 | if (l1oip_4bit_alloc(ulaw)) | |
1490 | return -ENOMEM; | |
1491 | ||
1492 | l1oip_cnt = 0; | |
1493 | while (type[l1oip_cnt] && l1oip_cnt < MAX_CARDS) { | |
1494 | switch (type[l1oip_cnt] & 0xff) { | |
1495 | case 1: | |
1496 | pri = 0; | |
1497 | bundle = 0; | |
1498 | break; | |
1499 | case 2: | |
1500 | pri = 1; | |
1501 | bundle = 0; | |
1502 | break; | |
1503 | case 3: | |
1504 | pri = 0; | |
1505 | bundle = 1; | |
1506 | break; | |
1507 | case 4: | |
1508 | pri = 1; | |
1509 | bundle = 1; | |
1510 | break; | |
1511 | default: | |
1512 | printk(KERN_ERR "Card type(%d) not supported.\n", | |
1513 | type[l1oip_cnt] & 0xff); | |
1514 | l1oip_cleanup(); | |
1515 | return -EINVAL; | |
1516 | } | |
1517 | ||
1518 | if (debug & DEBUG_L1OIP_INIT) | |
1519 | printk(KERN_DEBUG "%s: interface %d is %s with %s.\n", | |
eac74af9 KK |
1520 | __func__, l1oip_cnt, pri ? "PRI" : "BRI", |
1521 | bundle ? "bundled IP packet for all B-channels" : | |
1522 | "seperate IP packets for every B-channel"); | |
3712b42d KK |
1523 | |
1524 | hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC); | |
1525 | if (!hc) { | |
1526 | printk(KERN_ERR "No kmem for L1-over-IP driver.\n"); | |
1527 | l1oip_cleanup(); | |
1528 | return -ENOMEM; | |
1529 | } | |
1530 | INIT_WORK(&hc->workq, (void *)l1oip_send_bh); | |
1531 | ||
1532 | spin_lock(&l1oip_lock); | |
1533 | list_add_tail(&hc->list, &l1oip_ilist); | |
1534 | spin_unlock(&l1oip_lock); | |
1535 | ||
1536 | ret = init_card(hc, pri, bundle); | |
1537 | if (ret) { | |
1538 | l1oip_cleanup(); | |
1539 | return ret; | |
1540 | } | |
1541 | ||
1542 | l1oip_cnt++; | |
1543 | } | |
1544 | printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt); | |
1545 | return 0; | |
1546 | } | |
1547 | ||
1548 | module_init(l1oip_init); | |
1549 | module_exit(l1oip_cleanup); | |
1550 |