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