Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003 
0004  * l1oip.c  low level driver for tunneling layer 1 over IP
0005  *
0006  * NOTE: It is not compatible with TDMoIP nor "ISDN over IP".
0007  *
0008  * Author   Andreas Eversberg (jolly@eversberg.eu)
0009  */
0010 
0011 /* module parameters:
0012  * type:
0013  Value 1    = BRI
0014  Value 2    = PRI
0015  Value 3 = BRI (multi channel frame, not supported yet)
0016  Value 4 = PRI (multi channel frame, not supported yet)
0017  A multi channel frame reduces overhead to a single frame for all
0018  b-channels, but increases delay.
0019  (NOTE: Multi channel frames are not implemented yet.)
0020 
0021  * codec:
0022  Value 0 = transparent (default)
0023  Value 1 = transfer ALAW
0024  Value 2 = transfer ULAW
0025  Value 3 = transfer generic 4 bit compression.
0026 
0027  * ulaw:
0028  0 = we use a-Law (default)
0029  1 = we use u-Law
0030 
0031  * limit:
0032  limitation of B-channels to control bandwidth (1...126)
0033  BRI: 1 or 2
0034  PRI: 1-30, 31-126 (126, because dchannel ist not counted here)
0035  Also limited ressources are used for stack, resulting in less channels.
0036  It is possible to have more channels than 30 in PRI mode, this must
0037  be supported by the application.
0038 
0039  * ip:
0040  byte representation of remote ip address (127.0.0.1 -> 127,0,0,1)
0041  If not given or four 0, no remote address is set.
0042  For multiple interfaces, concat ip addresses. (127,0,0,1,127,0,0,1)
0043 
0044  * port:
0045  port number (local interface)
0046  If not given or 0, port 931 is used for fist instance, 932 for next...
0047  For multiple interfaces, different ports must be given.
0048 
0049  * remoteport:
0050  port number (remote interface)
0051  If not given or 0, remote port equals local port
0052  For multiple interfaces on equal sites, different ports must be given.
0053 
0054  * ondemand:
0055  0 = fixed (always transmit packets, even when remote side timed out)
0056  1 = on demand (only transmit packets, when remote side is detected)
0057  the default is 0
0058  NOTE: ID must also be set for on demand.
0059 
0060  * id:
0061  optional value to identify frames. This value must be equal on both
0062  peers and should be random. If omitted or 0, no ID is transmitted.
0063 
0064  * debug:
0065  NOTE: only one debug value must be given for all cards
0066  enable debugging (see l1oip.h for debug options)
0067 
0068 
0069  Special mISDN controls:
0070 
0071  op = MISDN_CTRL_SETPEER*
0072  p1 = bytes 0-3 : remote IP address in network order (left element first)
0073  p2 = bytes 1-2 : remote port in network order (high byte first)
0074  optional:
0075  p2 = bytes 3-4 : local port in network order (high byte first)
0076 
0077  op = MISDN_CTRL_UNSETPEER*
0078 
0079  * Use l1oipctrl for comfortable setting or removing ip address.
0080  (Layer 1 Over IP CTRL)
0081 
0082 
0083  L1oIP-Protocol
0084  --------------
0085 
0086  Frame Header:
0087 
0088  7 6 5 4 3 2 1 0
0089  +---------------+
0090  |Ver|T|I|Coding |
0091  +---------------+
0092  |  ID byte 3 *  |
0093  +---------------+
0094  |  ID byte 2 *  |
0095  +---------------+
0096  |  ID byte 1 *  |
0097  +---------------+
0098  |  ID byte 0 *  |
0099  +---------------+
0100  |M|   Channel   |
0101  +---------------+
0102  |    Length *   |
0103  +---------------+
0104  | Time Base MSB |
0105  +---------------+
0106  | Time Base LSB |
0107  +---------------+
0108  | Data.... |
0109 
0110  ...
0111 
0112  |               |
0113  +---------------+
0114  |M|   Channel   |
0115  +---------------+
0116  |    Length *   |
0117  +---------------+
0118  | Time Base MSB |
0119  +---------------+
0120  | Time Base LSB |
0121  +---------------+
0122  | Data.... |
0123 
0124  ...
0125 
0126 
0127  * Only included in some cases.
0128 
0129  - Ver = Version
0130  If version is missmatch, the frame must be ignored.
0131 
0132  - T = Type of interface
0133  Must be 0 for S0 or 1 for E1.
0134 
0135  - I = Id present
0136  If bit is set, four ID bytes are included in frame.
0137 
0138  - ID = Connection ID
0139  Additional ID to prevent Denial of Service attacs. Also it prevents hijacking
0140  connections with dynamic IP. The ID should be random and must not be 0.
0141 
0142  - Coding = Type of codec
0143  Must be 0 for no transcoding. Also for D-channel and other HDLC frames.
0144  1 and 2 are reserved for explicitly use of a-LAW or u-LAW codec.
0145  3 is used for generic table compressor.
0146 
0147  - M = More channels to come. If this flag is 1, the following byte contains
0148  the length of the channel data. After the data block, the next channel will
0149  be defined. The flag for the last channel block (or if only one channel is
0150  transmitted), must be 0 and no length is given.
0151 
0152  - Channel = Channel number
0153  0 reserved
0154  1-3 channel data for S0 (3 is D-channel)
0155  1-31 channel data for E1 (16 is D-channel)
0156  32-127 channel data for extended E1 (16 is D-channel)
0157 
0158  - The length is used if the M-flag is 1. It is used to find the next channel
0159  inside frame.
0160  NOTE: A value of 0 equals 256 bytes of data.
0161  -> For larger data blocks, a single frame must be used.
0162  -> For larger streams, a single frame or multiple blocks with same channel ID
0163  must be used.
0164 
0165  - Time Base = Timestamp of first sample in frame
0166  The "Time Base" is used to rearange packets and to detect packet loss.
0167  The 16 bits are sent in network order (MSB first) and count 1/8000 th of a
0168  second. This causes a wrap around each 8,192 seconds. There is no requirement
0169  for the initial "Time Base", but 0 should be used for the first packet.
0170  In case of HDLC data, this timestamp counts the packet or byte number.
0171 
0172 
0173  Two Timers:
0174 
0175  After initialisation, a timer of 15 seconds is started. Whenever a packet is
0176  transmitted, the timer is reset to 15 seconds again. If the timer expires, an
0177  empty packet is transmitted. This keep the connection alive.
0178 
0179  When a valid packet is received, a timer 65 seconds is started. The interface
0180  become ACTIVE. If the timer expires, the interface becomes INACTIVE.
0181 
0182 
0183  Dynamic IP handling:
0184 
0185  To allow dynamic IP, the ID must be non 0. In this case, any packet with the
0186  correct port number and ID will be accepted. If the remote side changes its IP
0187  the new IP is used for all transmitted packets until it changes again.
0188 
0189 
0190  On Demand:
0191 
0192  If the ondemand parameter is given, the remote IP is set to 0 on timeout.
0193  This will stop keepalive traffic to remote. If the remote is online again,
0194  traffic will continue to the remote address. This is useful for road warriors.
0195  This feature only works with ID set, otherwhise it is highly unsecure.
0196 
0197 
0198  Socket and Thread
0199  -----------------
0200 
0201  The complete socket opening and closing is done by a thread.
0202  When the thread opened a socket, the hc->socket descriptor is set. Whenever a
0203  packet shall be sent to the socket, the hc->socket must be checked whether not
0204  NULL. To prevent change in socket descriptor, the hc->socket_lock must be used.
0205  To change the socket, a recall of l1oip_socket_open() will safely kill the
0206  socket process and create a new one.
0207 
0208 */
0209 
0210 #define L1OIP_VERSION   0   /* 0...3 */
0211 
0212 #include <linux/module.h>
0213 #include <linux/delay.h>
0214 #include <linux/mISDNif.h>
0215 #include <linux/mISDNhw.h>
0216 #include <linux/mISDNdsp.h>
0217 #include <linux/init.h>
0218 #include <linux/in.h>
0219 #include <linux/inet.h>
0220 #include <linux/workqueue.h>
0221 #include <linux/kthread.h>
0222 #include <linux/slab.h>
0223 #include <linux/sched/signal.h>
0224 
0225 #include <net/sock.h>
0226 #include "core.h"
0227 #include "l1oip.h"
0228 
0229 static const char *l1oip_revision = "2.00";
0230 
0231 static int l1oip_cnt;
0232 static DEFINE_SPINLOCK(l1oip_lock);
0233 static LIST_HEAD(l1oip_ilist);
0234 
0235 #define MAX_CARDS   16
0236 static u_int type[MAX_CARDS];
0237 static u_int codec[MAX_CARDS];
0238 static u_int ip[MAX_CARDS * 4];
0239 static u_int port[MAX_CARDS];
0240 static u_int remoteport[MAX_CARDS];
0241 static u_int ondemand[MAX_CARDS];
0242 static u_int limit[MAX_CARDS];
0243 static u_int id[MAX_CARDS];
0244 static int debug;
0245 static int ulaw;
0246 
0247 MODULE_AUTHOR("Andreas Eversberg");
0248 MODULE_LICENSE("GPL");
0249 module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
0250 module_param_array(codec, uint, NULL, S_IRUGO | S_IWUSR);
0251 module_param_array(ip, uint, NULL, S_IRUGO | S_IWUSR);
0252 module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
0253 module_param_array(remoteport, uint, NULL, S_IRUGO | S_IWUSR);
0254 module_param_array(ondemand, uint, NULL, S_IRUGO | S_IWUSR);
0255 module_param_array(limit, uint, NULL, S_IRUGO | S_IWUSR);
0256 module_param_array(id, uint, NULL, S_IRUGO | S_IWUSR);
0257 module_param(ulaw, uint, S_IRUGO | S_IWUSR);
0258 module_param(debug, uint, S_IRUGO | S_IWUSR);
0259 
0260 /*
0261  * send a frame via socket, if open and restart timer
0262  */
0263 static int
0264 l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
0265           u16 timebase, u8 *buf, int len)
0266 {
0267     u8 *p;
0268     u8 frame[MAX_DFRAME_LEN_L1 + 32];
0269     struct socket *socket = NULL;
0270 
0271     if (debug & DEBUG_L1OIP_MSG)
0272         printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
0273                __func__, len);
0274 
0275     p = frame;
0276 
0277     /* restart timer */
0278     if (time_before(hc->keep_tl.expires, jiffies + 5 * HZ))
0279         mod_timer(&hc->keep_tl, jiffies + L1OIP_KEEPALIVE * HZ);
0280     else
0281         hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE * HZ;
0282 
0283     if (debug & DEBUG_L1OIP_MSG)
0284         printk(KERN_DEBUG "%s: resetting timer\n", __func__);
0285 
0286     /* drop if we have no remote ip or port */
0287     if (!hc->sin_remote.sin_addr.s_addr || !hc->sin_remote.sin_port) {
0288         if (debug & DEBUG_L1OIP_MSG)
0289             printk(KERN_DEBUG "%s: dropping frame, because remote "
0290                    "IP is not set.\n", __func__);
0291         return len;
0292     }
0293 
0294     /* assemble frame */
0295     *p++ = (L1OIP_VERSION << 6) /* version and coding */
0296         | (hc->pri ? 0x20 : 0x00) /* type */
0297         | (hc->id ? 0x10 : 0x00) /* id */
0298         | localcodec;
0299     if (hc->id) {
0300         *p++ = hc->id >> 24; /* id */
0301         *p++ = hc->id >> 16;
0302         *p++ = hc->id >> 8;
0303         *p++ = hc->id;
0304     }
0305     *p++ =  0x00 + channel; /* m-flag, channel */
0306     *p++ = timebase >> 8; /* time base */
0307     *p++ = timebase;
0308 
0309     if (buf && len) { /* add data to frame */
0310         if (localcodec == 1 && ulaw)
0311             l1oip_ulaw_to_alaw(buf, len, p);
0312         else if (localcodec == 2 && !ulaw)
0313             l1oip_alaw_to_ulaw(buf, len, p);
0314         else if (localcodec == 3)
0315             len = l1oip_law_to_4bit(buf, len, p,
0316                         &hc->chan[channel].codecstate);
0317         else
0318             memcpy(p, buf, len);
0319     }
0320     len += p - frame;
0321 
0322     /* check for socket in safe condition */
0323     spin_lock(&hc->socket_lock);
0324     if (!hc->socket) {
0325         spin_unlock(&hc->socket_lock);
0326         return 0;
0327     }
0328     /* seize socket */
0329     socket = hc->socket;
0330     hc->socket = NULL;
0331     spin_unlock(&hc->socket_lock);
0332     /* send packet */
0333     if (debug & DEBUG_L1OIP_MSG)
0334         printk(KERN_DEBUG "%s: sending packet to socket (len "
0335                "= %d)\n", __func__, len);
0336     hc->sendiov.iov_base = frame;
0337     hc->sendiov.iov_len  = len;
0338     len = kernel_sendmsg(socket, &hc->sendmsg, &hc->sendiov, 1, len);
0339     /* give socket back */
0340     hc->socket = socket; /* no locking required */
0341 
0342     return len;
0343 }
0344 
0345 
0346 /*
0347  * receive channel data from socket
0348  */
0349 static void
0350 l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
0351           u8 *buf, int len)
0352 {
0353     struct sk_buff *nskb;
0354     struct bchannel *bch;
0355     struct dchannel *dch;
0356     u8 *p;
0357     u32 rx_counter;
0358 
0359     if (len == 0) {
0360         if (debug & DEBUG_L1OIP_MSG)
0361             printk(KERN_DEBUG "%s: received empty keepalive data, "
0362                    "ignoring\n", __func__);
0363         return;
0364     }
0365 
0366     if (debug & DEBUG_L1OIP_MSG)
0367         printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
0368                __func__, len);
0369 
0370     if (channel < 1 || channel > 127) {
0371         printk(KERN_WARNING "%s: packet error - channel %d out of "
0372                "range\n", __func__, channel);
0373         return;
0374     }
0375     dch = hc->chan[channel].dch;
0376     bch = hc->chan[channel].bch;
0377     if (!dch && !bch) {
0378         printk(KERN_WARNING "%s: packet error - channel %d not in "
0379                "stack\n", __func__, channel);
0380         return;
0381     }
0382 
0383     /* prepare message */
0384     nskb = mI_alloc_skb((remotecodec == 3) ? (len << 1) : len, GFP_ATOMIC);
0385     if (!nskb) {
0386         printk(KERN_ERR "%s: No mem for skb.\n", __func__);
0387         return;
0388     }
0389     p = skb_put(nskb, (remotecodec == 3) ? (len << 1) : len);
0390 
0391     if (remotecodec == 1 && ulaw)
0392         l1oip_alaw_to_ulaw(buf, len, p);
0393     else if (remotecodec == 2 && !ulaw)
0394         l1oip_ulaw_to_alaw(buf, len, p);
0395     else if (remotecodec == 3)
0396         len = l1oip_4bit_to_law(buf, len, p);
0397     else
0398         memcpy(p, buf, len);
0399 
0400     /* send message up */
0401     if (dch && len >= 2) {
0402         dch->rx_skb = nskb;
0403         recv_Dchannel(dch);
0404     }
0405     if (bch) {
0406         /* expand 16 bit sequence number to 32 bit sequence number */
0407         rx_counter = hc->chan[channel].rx_counter;
0408         if (((s16)(timebase - rx_counter)) >= 0) {
0409             /* time has changed forward */
0410             if (timebase >= (rx_counter & 0xffff))
0411                 rx_counter =
0412                     (rx_counter & 0xffff0000) | timebase;
0413             else
0414                 rx_counter = ((rx_counter & 0xffff0000) + 0x10000)
0415                     | timebase;
0416         } else {
0417             /* time has changed backwards */
0418             if (timebase < (rx_counter & 0xffff))
0419                 rx_counter =
0420                     (rx_counter & 0xffff0000) | timebase;
0421             else
0422                 rx_counter = ((rx_counter & 0xffff0000) - 0x10000)
0423                     | timebase;
0424         }
0425         hc->chan[channel].rx_counter = rx_counter;
0426 
0427 #ifdef REORDER_DEBUG
0428         if (hc->chan[channel].disorder_flag) {
0429             swap(hc->chan[channel].disorder_skb, nskb);
0430             swap(hc->chan[channel].disorder_cnt, rx_counter);
0431         }
0432         hc->chan[channel].disorder_flag ^= 1;
0433         if (nskb)
0434 #endif
0435             queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
0436     }
0437 }
0438 
0439 
0440 /*
0441  * parse frame and extract channel data
0442  */
0443 static void
0444 l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
0445 {
0446     u32         packet_id;
0447     u8          channel;
0448     u8          remotecodec;
0449     u16         timebase;
0450     int         m, mlen;
0451     int         len_start = len; /* initial frame length */
0452     struct dchannel     *dch = hc->chan[hc->d_idx].dch;
0453 
0454     if (debug & DEBUG_L1OIP_MSG)
0455         printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
0456                __func__, len);
0457 
0458     /* check length */
0459     if (len < 1 + 1 + 2) {
0460         printk(KERN_WARNING "%s: packet error - length %d below "
0461                "4 bytes\n", __func__, len);
0462         return;
0463     }
0464 
0465     /* check version */
0466     if (((*buf) >> 6) != L1OIP_VERSION) {
0467         printk(KERN_WARNING "%s: packet error - unknown version %d\n",
0468                __func__, buf[0]>>6);
0469         return;
0470     }
0471 
0472     /* check type */
0473     if (((*buf) & 0x20) && !hc->pri) {
0474         printk(KERN_WARNING "%s: packet error - received E1 packet "
0475                "on S0 interface\n", __func__);
0476         return;
0477     }
0478     if (!((*buf) & 0x20) && hc->pri) {
0479         printk(KERN_WARNING "%s: packet error - received S0 packet "
0480                "on E1 interface\n", __func__);
0481         return;
0482     }
0483 
0484     /* get id flag */
0485     packet_id = (*buf >> 4) & 1;
0486 
0487     /* check coding */
0488     remotecodec = (*buf) & 0x0f;
0489     if (remotecodec > 3) {
0490         printk(KERN_WARNING "%s: packet error - remotecodec %d "
0491                "unsupported\n", __func__, remotecodec);
0492         return;
0493     }
0494     buf++;
0495     len--;
0496 
0497     /* check packet_id */
0498     if (packet_id) {
0499         if (!hc->id) {
0500             printk(KERN_WARNING "%s: packet error - packet has id "
0501                    "0x%x, but we have not\n", __func__, packet_id);
0502             return;
0503         }
0504         if (len < 4) {
0505             printk(KERN_WARNING "%s: packet error - packet too "
0506                    "short for ID value\n", __func__);
0507             return;
0508         }
0509         packet_id = (*buf++) << 24;
0510         packet_id += (*buf++) << 16;
0511         packet_id += (*buf++) << 8;
0512         packet_id += (*buf++);
0513         len -= 4;
0514 
0515         if (packet_id != hc->id) {
0516             printk(KERN_WARNING "%s: packet error - ID mismatch, "
0517                    "got 0x%x, we 0x%x\n",
0518                    __func__, packet_id, hc->id);
0519             return;
0520         }
0521     } else {
0522         if (hc->id) {
0523             printk(KERN_WARNING "%s: packet error - packet has no "
0524                    "ID, but we have\n", __func__);
0525             return;
0526         }
0527     }
0528 
0529 multiframe:
0530     if (len < 1) {
0531         printk(KERN_WARNING "%s: packet error - packet too short, "
0532                "channel expected at position %d.\n",
0533                __func__, len-len_start + 1);
0534         return;
0535     }
0536 
0537     /* get channel and multiframe flag */
0538     channel = *buf & 0x7f;
0539     m = *buf >> 7;
0540     buf++;
0541     len--;
0542 
0543     /* check length on multiframe */
0544     if (m) {
0545         if (len < 1) {
0546             printk(KERN_WARNING "%s: packet error - packet too "
0547                    "short, length expected at position %d.\n",
0548                    __func__, len_start - len - 1);
0549             return;
0550         }
0551 
0552         mlen = *buf++;
0553         len--;
0554         if (mlen == 0)
0555             mlen = 256;
0556         if (len < mlen + 3) {
0557             printk(KERN_WARNING "%s: packet error - length %d at "
0558                    "position %d exceeds total length %d.\n",
0559                    __func__, mlen, len_start-len - 1, len_start);
0560             return;
0561         }
0562         if (len == mlen + 3) {
0563             printk(KERN_WARNING "%s: packet error - length %d at "
0564                    "position %d will not allow additional "
0565                    "packet.\n",
0566                    __func__, mlen, len_start-len + 1);
0567             return;
0568         }
0569     } else
0570         mlen = len - 2; /* single frame, subtract timebase */
0571 
0572     if (len < 2) {
0573         printk(KERN_WARNING "%s: packet error - packet too short, time "
0574                "base expected at position %d.\n",
0575                __func__, len-len_start + 1);
0576         return;
0577     }
0578 
0579     /* get time base */
0580     timebase = (*buf++) << 8;
0581     timebase |= (*buf++);
0582     len -= 2;
0583 
0584     /* if inactive, we send up a PH_ACTIVATE and activate */
0585     if (!test_bit(FLG_ACTIVE, &dch->Flags)) {
0586         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0587             printk(KERN_DEBUG "%s: interface become active due to "
0588                    "received packet\n", __func__);
0589         test_and_set_bit(FLG_ACTIVE, &dch->Flags);
0590         _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
0591                 NULL, GFP_ATOMIC);
0592     }
0593 
0594     /* distribute packet */
0595     l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
0596     buf += mlen;
0597     len -= mlen;
0598 
0599     /* multiframe */
0600     if (m)
0601         goto multiframe;
0602 
0603     /* restart timer */
0604     if (time_before(hc->timeout_tl.expires, jiffies + 5 * HZ) || !hc->timeout_on) {
0605         hc->timeout_on = 1;
0606         mod_timer(&hc->timeout_tl, jiffies + L1OIP_TIMEOUT * HZ);
0607     } else /* only adjust timer */
0608         hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
0609 
0610     /* if ip or source port changes */
0611     if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
0612         || (hc->sin_remote.sin_port != sin->sin_port)) {
0613         if (debug & DEBUG_L1OIP_SOCKET)
0614             printk(KERN_DEBUG "%s: remote address changes from "
0615                    "0x%08x to 0x%08x (port %d to %d)\n", __func__,
0616                    ntohl(hc->sin_remote.sin_addr.s_addr),
0617                    ntohl(sin->sin_addr.s_addr),
0618                    ntohs(hc->sin_remote.sin_port),
0619                    ntohs(sin->sin_port));
0620         hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
0621         hc->sin_remote.sin_port = sin->sin_port;
0622     }
0623 }
0624 
0625 
0626 /*
0627  * socket stuff
0628  */
0629 static int
0630 l1oip_socket_thread(void *data)
0631 {
0632     struct l1oip *hc = (struct l1oip *)data;
0633     int ret = 0;
0634     struct sockaddr_in sin_rx;
0635     struct kvec iov;
0636     struct msghdr msg = {.msg_name = &sin_rx,
0637                  .msg_namelen = sizeof(sin_rx)};
0638     unsigned char *recvbuf;
0639     size_t recvbuf_size = 1500;
0640     int recvlen;
0641     struct socket *socket = NULL;
0642     DECLARE_COMPLETION_ONSTACK(wait);
0643 
0644     /* allocate buffer memory */
0645     recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
0646     if (!recvbuf) {
0647         printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
0648         ret = -ENOMEM;
0649         goto fail;
0650     }
0651 
0652     iov.iov_base = recvbuf;
0653     iov.iov_len = recvbuf_size;
0654 
0655     /* make daemon */
0656     allow_signal(SIGTERM);
0657 
0658     /* create socket */
0659     if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
0660         printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
0661         ret = -EIO;
0662         goto fail;
0663     }
0664 
0665     /* set incoming address */
0666     hc->sin_local.sin_family = AF_INET;
0667     hc->sin_local.sin_addr.s_addr = INADDR_ANY;
0668     hc->sin_local.sin_port = htons((unsigned short)hc->localport);
0669 
0670     /* set outgoing address */
0671     hc->sin_remote.sin_family = AF_INET;
0672     hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip);
0673     hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport);
0674 
0675     /* bind to incoming port */
0676     if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
0677                   sizeof(hc->sin_local))) {
0678         printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
0679                __func__, hc->localport);
0680         ret = -EINVAL;
0681         goto fail;
0682     }
0683 
0684     /* check sk */
0685     if (socket->sk == NULL) {
0686         printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
0687         ret = -EIO;
0688         goto fail;
0689     }
0690 
0691     /* build send message */
0692     hc->sendmsg.msg_name = &hc->sin_remote;
0693     hc->sendmsg.msg_namelen = sizeof(hc->sin_remote);
0694     hc->sendmsg.msg_control = NULL;
0695     hc->sendmsg.msg_controllen = 0;
0696 
0697     /* give away socket */
0698     spin_lock(&hc->socket_lock);
0699     hc->socket = socket;
0700     spin_unlock(&hc->socket_lock);
0701 
0702     /* read loop */
0703     if (debug & DEBUG_L1OIP_SOCKET)
0704         printk(KERN_DEBUG "%s: socket created and open\n",
0705                __func__);
0706     while (!signal_pending(current)) {
0707         iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, recvbuf_size);
0708         recvlen = sock_recvmsg(socket, &msg, 0);
0709         if (recvlen > 0) {
0710             l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
0711         } else {
0712             if (debug & DEBUG_L1OIP_SOCKET)
0713                 printk(KERN_WARNING
0714                        "%s: broken pipe on socket\n", __func__);
0715         }
0716     }
0717 
0718     /* get socket back, check first if in use, maybe by send function */
0719     spin_lock(&hc->socket_lock);
0720     /* if hc->socket is NULL, it is in use until it is given back */
0721     while (!hc->socket) {
0722         spin_unlock(&hc->socket_lock);
0723         schedule_timeout(HZ / 10);
0724         spin_lock(&hc->socket_lock);
0725     }
0726     hc->socket = NULL;
0727     spin_unlock(&hc->socket_lock);
0728 
0729     if (debug & DEBUG_L1OIP_SOCKET)
0730         printk(KERN_DEBUG "%s: socket thread terminating\n",
0731                __func__);
0732 
0733 fail:
0734     /* free recvbuf */
0735     kfree(recvbuf);
0736 
0737     /* close socket */
0738     if (socket)
0739         sock_release(socket);
0740 
0741     /* if we got killed, signal completion */
0742     complete(&hc->socket_complete);
0743     hc->socket_thread = NULL; /* show termination of thread */
0744 
0745     if (debug & DEBUG_L1OIP_SOCKET)
0746         printk(KERN_DEBUG "%s: socket thread terminated\n",
0747                __func__);
0748     return ret;
0749 }
0750 
0751 static void
0752 l1oip_socket_close(struct l1oip *hc)
0753 {
0754     struct dchannel *dch = hc->chan[hc->d_idx].dch;
0755 
0756     /* kill thread */
0757     if (hc->socket_thread) {
0758         if (debug & DEBUG_L1OIP_SOCKET)
0759             printk(KERN_DEBUG "%s: socket thread exists, "
0760                    "killing...\n", __func__);
0761         send_sig(SIGTERM, hc->socket_thread, 0);
0762         wait_for_completion(&hc->socket_complete);
0763     }
0764 
0765     /* if active, we send up a PH_DEACTIVATE and deactivate */
0766     if (test_bit(FLG_ACTIVE, &dch->Flags)) {
0767         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0768             printk(KERN_DEBUG "%s: interface become deactivated "
0769                    "due to timeout\n", __func__);
0770         test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
0771         _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
0772                 NULL, GFP_ATOMIC);
0773     }
0774 }
0775 
0776 static int
0777 l1oip_socket_open(struct l1oip *hc)
0778 {
0779     /* in case of reopen, we need to close first */
0780     l1oip_socket_close(hc);
0781 
0782     init_completion(&hc->socket_complete);
0783 
0784     /* create receive process */
0785     hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
0786                     hc->name);
0787     if (IS_ERR(hc->socket_thread)) {
0788         int err = PTR_ERR(hc->socket_thread);
0789         printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
0790                __func__, err);
0791         hc->socket_thread = NULL;
0792         sock_release(hc->socket);
0793         return err;
0794     }
0795     if (debug & DEBUG_L1OIP_SOCKET)
0796         printk(KERN_DEBUG "%s: socket thread created\n", __func__);
0797 
0798     return 0;
0799 }
0800 
0801 
0802 static void
0803 l1oip_send_bh(struct work_struct *work)
0804 {
0805     struct l1oip *hc = container_of(work, struct l1oip, workq);
0806 
0807     if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0808         printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
0809                "frame on dchannel\n", __func__);
0810 
0811     /* send an empty l1oip frame at D-channel */
0812     l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
0813 }
0814 
0815 
0816 /*
0817  * timer stuff
0818  */
0819 static void
0820 l1oip_keepalive(struct timer_list *t)
0821 {
0822     struct l1oip *hc = from_timer(hc, t, keep_tl);
0823 
0824     schedule_work(&hc->workq);
0825 }
0826 
0827 static void
0828 l1oip_timeout(struct timer_list *t)
0829 {
0830     struct l1oip            *hc = from_timer(hc, t,
0831                                   timeout_tl);
0832     struct dchannel     *dch = hc->chan[hc->d_idx].dch;
0833 
0834     if (debug & DEBUG_L1OIP_MSG)
0835         printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
0836                "down.\n", __func__);
0837 
0838     hc->timeout_on = 0; /* state that timer must be initialized next time */
0839 
0840     /* if timeout, we send up a PH_DEACTIVATE and deactivate */
0841     if (test_bit(FLG_ACTIVE, &dch->Flags)) {
0842         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0843             printk(KERN_DEBUG "%s: interface become deactivated "
0844                    "due to timeout\n", __func__);
0845         test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
0846         _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
0847                 NULL, GFP_ATOMIC);
0848     }
0849 
0850     /* if we have ondemand set, we remove ip address */
0851     if (hc->ondemand) {
0852         if (debug & DEBUG_L1OIP_MSG)
0853             printk(KERN_DEBUG "%s: on demand causes ip address to "
0854                    "be removed\n", __func__);
0855         hc->sin_remote.sin_addr.s_addr = 0;
0856     }
0857 }
0858 
0859 
0860 /*
0861  * message handling
0862  */
0863 static int
0864 handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
0865 {
0866     struct mISDNdevice  *dev = container_of(ch, struct mISDNdevice, D);
0867     struct dchannel     *dch = container_of(dev, struct dchannel, dev);
0868     struct l1oip            *hc = dch->hw;
0869     struct mISDNhead    *hh = mISDN_HEAD_P(skb);
0870     int         ret = -EINVAL;
0871     int         l, ll;
0872     unsigned char       *p;
0873 
0874     switch (hh->prim) {
0875     case PH_DATA_REQ:
0876         if (skb->len < 1) {
0877             printk(KERN_WARNING "%s: skb too small\n",
0878                    __func__);
0879             break;
0880         }
0881         if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
0882             printk(KERN_WARNING "%s: skb too large\n",
0883                    __func__);
0884             break;
0885         }
0886         /* send frame */
0887         p = skb->data;
0888         l = skb->len;
0889         while (l) {
0890             /*
0891              * This is technically bounded by L1OIP_MAX_PERFRAME but
0892              * MAX_DFRAME_LEN_L1 < L1OIP_MAX_PERFRAME
0893              */
0894             ll = (l < MAX_DFRAME_LEN_L1) ? l : MAX_DFRAME_LEN_L1;
0895             l1oip_socket_send(hc, 0, dch->slot, 0,
0896                       hc->chan[dch->slot].tx_counter++, p, ll);
0897             p += ll;
0898             l -= ll;
0899         }
0900         skb_trim(skb, 0);
0901         queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
0902         return 0;
0903     case PH_ACTIVATE_REQ:
0904         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0905             printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
0906                    , __func__, dch->slot, hc->b_num + 1);
0907         skb_trim(skb, 0);
0908         if (test_bit(FLG_ACTIVE, &dch->Flags))
0909             queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
0910         else
0911             queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
0912         return 0;
0913     case PH_DEACTIVATE_REQ:
0914         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
0915             printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
0916                    "(1..%d)\n", __func__, dch->slot,
0917                    hc->b_num + 1);
0918         skb_trim(skb, 0);
0919         if (test_bit(FLG_ACTIVE, &dch->Flags))
0920             queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
0921         else
0922             queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
0923         return 0;
0924     }
0925     if (!ret)
0926         dev_kfree_skb(skb);
0927     return ret;
0928 }
0929 
0930 static int
0931 channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
0932 {
0933     int ret = 0;
0934     struct l1oip    *hc = dch->hw;
0935 
0936     switch (cq->op) {
0937     case MISDN_CTRL_GETOP:
0938         cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
0939             | MISDN_CTRL_GETPEER;
0940         break;
0941     case MISDN_CTRL_SETPEER:
0942         hc->remoteip = (u32)cq->p1;
0943         hc->remoteport = cq->p2 & 0xffff;
0944         hc->localport = cq->p2 >> 16;
0945         if (!hc->remoteport)
0946             hc->remoteport = hc->localport;
0947         if (debug & DEBUG_L1OIP_SOCKET)
0948             printk(KERN_DEBUG "%s: got new ip address from user "
0949                    "space.\n", __func__);
0950         l1oip_socket_open(hc);
0951         break;
0952     case MISDN_CTRL_UNSETPEER:
0953         if (debug & DEBUG_L1OIP_SOCKET)
0954             printk(KERN_DEBUG "%s: removing ip address.\n",
0955                    __func__);
0956         hc->remoteip = 0;
0957         l1oip_socket_open(hc);
0958         break;
0959     case MISDN_CTRL_GETPEER:
0960         if (debug & DEBUG_L1OIP_SOCKET)
0961             printk(KERN_DEBUG "%s: getting ip address.\n",
0962                    __func__);
0963         cq->p1 = hc->remoteip;
0964         cq->p2 = hc->remoteport | (hc->localport << 16);
0965         break;
0966     default:
0967         printk(KERN_WARNING "%s: unknown Op %x\n",
0968                __func__, cq->op);
0969         ret = -EINVAL;
0970         break;
0971     }
0972     return ret;
0973 }
0974 
0975 static int
0976 open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
0977 {
0978     if (debug & DEBUG_HW_OPEN)
0979         printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
0980                dch->dev.id, __builtin_return_address(0));
0981     if (rq->protocol == ISDN_P_NONE)
0982         return -EINVAL;
0983     if ((dch->dev.D.protocol != ISDN_P_NONE) &&
0984         (dch->dev.D.protocol != rq->protocol)) {
0985         if (debug & DEBUG_HW_OPEN)
0986             printk(KERN_WARNING "%s: change protocol %x to %x\n",
0987                    __func__, dch->dev.D.protocol, rq->protocol);
0988     }
0989     if (dch->dev.D.protocol != rq->protocol)
0990         dch->dev.D.protocol = rq->protocol;
0991 
0992     if (test_bit(FLG_ACTIVE, &dch->Flags)) {
0993         _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
0994                 0, NULL, GFP_KERNEL);
0995     }
0996     rq->ch = &dch->dev.D;
0997     if (!try_module_get(THIS_MODULE))
0998         printk(KERN_WARNING "%s:cannot get module\n", __func__);
0999     return 0;
1000 }
1001 
1002 static int
1003 open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1004 {
1005     struct bchannel *bch;
1006     int     ch;
1007 
1008     if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
1009         return -EINVAL;
1010     if (rq->protocol == ISDN_P_NONE)
1011         return -EINVAL;
1012     ch = rq->adr.channel; /* BRI: 1=B1 2=B2  PRI: 1..15,17.. */
1013     bch = hc->chan[ch].bch;
1014     if (!bch) {
1015         printk(KERN_ERR "%s:internal error ch %d has no bch\n",
1016                __func__, ch);
1017         return -EINVAL;
1018     }
1019     if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1020         return -EBUSY; /* b-channel can be only open once */
1021     bch->ch.protocol = rq->protocol;
1022     rq->ch = &bch->ch;
1023     if (!try_module_get(THIS_MODULE))
1024         printk(KERN_WARNING "%s:cannot get module\n", __func__);
1025     return 0;
1026 }
1027 
1028 static int
1029 l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1030 {
1031     struct mISDNdevice  *dev = container_of(ch, struct mISDNdevice, D);
1032     struct dchannel     *dch = container_of(dev, struct dchannel, dev);
1033     struct l1oip            *hc = dch->hw;
1034     struct channel_req  *rq;
1035     int         err = 0;
1036 
1037     if (dch->debug & DEBUG_HW)
1038         printk(KERN_DEBUG "%s: cmd:%x %p\n",
1039                __func__, cmd, arg);
1040     switch (cmd) {
1041     case OPEN_CHANNEL:
1042         rq = arg;
1043         switch (rq->protocol) {
1044         case ISDN_P_TE_S0:
1045         case ISDN_P_NT_S0:
1046             if (hc->pri) {
1047                 err = -EINVAL;
1048                 break;
1049             }
1050             err = open_dchannel(hc, dch, rq);
1051             break;
1052         case ISDN_P_TE_E1:
1053         case ISDN_P_NT_E1:
1054             if (!hc->pri) {
1055                 err = -EINVAL;
1056                 break;
1057             }
1058             err = open_dchannel(hc, dch, rq);
1059             break;
1060         default:
1061             err = open_bchannel(hc, dch, rq);
1062         }
1063         break;
1064     case CLOSE_CHANNEL:
1065         if (debug & DEBUG_HW_OPEN)
1066             printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
1067                    __func__, dch->dev.id,
1068                    __builtin_return_address(0));
1069         module_put(THIS_MODULE);
1070         break;
1071     case CONTROL_CHANNEL:
1072         err = channel_dctrl(dch, arg);
1073         break;
1074     default:
1075         if (dch->debug & DEBUG_HW)
1076             printk(KERN_DEBUG "%s: unknown command %x\n",
1077                    __func__, cmd);
1078         err = -EINVAL;
1079     }
1080     return err;
1081 }
1082 
1083 static int
1084 handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1085 {
1086     struct bchannel     *bch = container_of(ch, struct bchannel, ch);
1087     struct l1oip            *hc = bch->hw;
1088     int         ret = -EINVAL;
1089     struct mISDNhead    *hh = mISDN_HEAD_P(skb);
1090     int         l, ll;
1091     unsigned char       *p;
1092 
1093     switch (hh->prim) {
1094     case PH_DATA_REQ:
1095         if (skb->len <= 0) {
1096             printk(KERN_WARNING "%s: skb too small\n",
1097                    __func__);
1098             break;
1099         }
1100         if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1101             printk(KERN_WARNING "%s: skb too large\n",
1102                    __func__);
1103             break;
1104         }
1105         /* check for AIS / ulaw-silence */
1106         l = skb->len;
1107         if (!memchr_inv(skb->data, 0xff, l)) {
1108             if (debug & DEBUG_L1OIP_MSG)
1109                 printk(KERN_DEBUG "%s: got AIS, not sending, "
1110                        "but counting\n", __func__);
1111             hc->chan[bch->slot].tx_counter += l;
1112             skb_trim(skb, 0);
1113             queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1114             return 0;
1115         }
1116         /* check for silence */
1117         l = skb->len;
1118         if (!memchr_inv(skb->data, 0x2a, l)) {
1119             if (debug & DEBUG_L1OIP_MSG)
1120                 printk(KERN_DEBUG "%s: got silence, not sending"
1121                        ", but counting\n", __func__);
1122             hc->chan[bch->slot].tx_counter += l;
1123             skb_trim(skb, 0);
1124             queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1125             return 0;
1126         }
1127 
1128         /* send frame */
1129         p = skb->data;
1130         l = skb->len;
1131         while (l) {
1132             /*
1133              * This is technically bounded by L1OIP_MAX_PERFRAME but
1134              * MAX_DFRAME_LEN_L1 < L1OIP_MAX_PERFRAME
1135              */
1136             ll = (l < MAX_DFRAME_LEN_L1) ? l : MAX_DFRAME_LEN_L1;
1137             l1oip_socket_send(hc, hc->codec, bch->slot, 0,
1138                       hc->chan[bch->slot].tx_counter, p, ll);
1139             hc->chan[bch->slot].tx_counter += ll;
1140             p += ll;
1141             l -= ll;
1142         }
1143         skb_trim(skb, 0);
1144         queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1145         return 0;
1146     case PH_ACTIVATE_REQ:
1147         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1148             printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
1149                    , __func__, bch->slot, hc->b_num + 1);
1150         hc->chan[bch->slot].codecstate = 0;
1151         test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1152         skb_trim(skb, 0);
1153         queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1154         return 0;
1155     case PH_DEACTIVATE_REQ:
1156         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1157             printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
1158                    "(1..%d)\n", __func__, bch->slot,
1159                    hc->b_num + 1);
1160         test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1161         skb_trim(skb, 0);
1162         queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1163         return 0;
1164     }
1165     if (!ret)
1166         dev_kfree_skb(skb);
1167     return ret;
1168 }
1169 
1170 static int
1171 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1172 {
1173     int         ret = 0;
1174     struct dsp_features *features =
1175         (struct dsp_features *)(*((u_long *)&cq->p1));
1176 
1177     switch (cq->op) {
1178     case MISDN_CTRL_GETOP:
1179         cq->op = MISDN_CTRL_HW_FEATURES_OP;
1180         break;
1181     case MISDN_CTRL_HW_FEATURES: /* fill features structure */
1182         if (debug & DEBUG_L1OIP_MSG)
1183             printk(KERN_DEBUG "%s: HW_FEATURE request\n",
1184                    __func__);
1185         /* create confirm */
1186         features->unclocked = 1;
1187         features->unordered = 1;
1188         break;
1189     default:
1190         printk(KERN_WARNING "%s: unknown Op %x\n",
1191                __func__, cq->op);
1192         ret = -EINVAL;
1193         break;
1194     }
1195     return ret;
1196 }
1197 
1198 static int
1199 l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1200 {
1201     struct bchannel *bch = container_of(ch, struct bchannel, ch);
1202     int     err = -EINVAL;
1203 
1204     if (bch->debug & DEBUG_HW)
1205         printk(KERN_DEBUG "%s: cmd:%x %p\n",
1206                __func__, cmd, arg);
1207     switch (cmd) {
1208     case CLOSE_CHANNEL:
1209         test_and_clear_bit(FLG_OPEN, &bch->Flags);
1210         test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1211         ch->protocol = ISDN_P_NONE;
1212         ch->peer = NULL;
1213         module_put(THIS_MODULE);
1214         err = 0;
1215         break;
1216     case CONTROL_CHANNEL:
1217         err = channel_bctrl(bch, arg);
1218         break;
1219     default:
1220         printk(KERN_WARNING "%s: unknown prim(%x)\n",
1221                __func__, cmd);
1222     }
1223     return err;
1224 }
1225 
1226 
1227 /*
1228  * cleanup module and stack
1229  */
1230 static void
1231 release_card(struct l1oip *hc)
1232 {
1233     int ch;
1234 
1235     if (timer_pending(&hc->keep_tl))
1236         del_timer(&hc->keep_tl);
1237 
1238     if (timer_pending(&hc->timeout_tl))
1239         del_timer(&hc->timeout_tl);
1240 
1241     cancel_work_sync(&hc->workq);
1242 
1243     if (hc->socket_thread)
1244         l1oip_socket_close(hc);
1245 
1246     if (hc->registered && hc->chan[hc->d_idx].dch)
1247         mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1248     for (ch = 0; ch < 128; ch++) {
1249         if (hc->chan[ch].dch) {
1250             mISDN_freedchannel(hc->chan[ch].dch);
1251             kfree(hc->chan[ch].dch);
1252         }
1253         if (hc->chan[ch].bch) {
1254             mISDN_freebchannel(hc->chan[ch].bch);
1255             kfree(hc->chan[ch].bch);
1256 #ifdef REORDER_DEBUG
1257             dev_kfree_skb(hc->chan[ch].disorder_skb);
1258 #endif
1259         }
1260     }
1261 
1262     spin_lock(&l1oip_lock);
1263     list_del(&hc->list);
1264     spin_unlock(&l1oip_lock);
1265 
1266     kfree(hc);
1267 }
1268 
1269 static void
1270 l1oip_cleanup(void)
1271 {
1272     struct l1oip *hc, *next;
1273 
1274     list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1275         release_card(hc);
1276 
1277     l1oip_4bit_free();
1278 }
1279 
1280 
1281 /*
1282  * module and stack init
1283  */
1284 static int
1285 init_card(struct l1oip *hc, int pri, int bundle)
1286 {
1287     struct dchannel *dch;
1288     struct bchannel *bch;
1289     int     ret;
1290     int     i, ch;
1291 
1292     spin_lock_init(&hc->socket_lock);
1293     hc->idx = l1oip_cnt;
1294     hc->pri = pri;
1295     hc->d_idx = pri ? 16 : 3;
1296     hc->b_num = pri ? 30 : 2;
1297     hc->bundle = bundle;
1298     if (hc->pri)
1299         sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1300     else
1301         sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1302 
1303     switch (codec[l1oip_cnt]) {
1304     case 0: /* as is */
1305     case 1: /* alaw */
1306     case 2: /* ulaw */
1307     case 3: /* 4bit */
1308         break;
1309     default:
1310         printk(KERN_ERR "Codec(%d) not supported.\n",
1311                codec[l1oip_cnt]);
1312         return -EINVAL;
1313     }
1314     hc->codec = codec[l1oip_cnt];
1315     if (debug & DEBUG_L1OIP_INIT)
1316         printk(KERN_DEBUG "%s: using codec %d\n",
1317                __func__, hc->codec);
1318 
1319     if (id[l1oip_cnt] == 0) {
1320         printk(KERN_WARNING "Warning: No 'id' value given or "
1321                "0, this is highly unsecure. Please use 32 "
1322                "bit random number 0x...\n");
1323     }
1324     hc->id = id[l1oip_cnt];
1325     if (debug & DEBUG_L1OIP_INIT)
1326         printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1327 
1328     hc->ondemand = ondemand[l1oip_cnt];
1329     if (hc->ondemand && !hc->id) {
1330         printk(KERN_ERR "%s: ondemand option only allowed in "
1331                "conjunction with non 0 ID\n", __func__);
1332         return -EINVAL;
1333     }
1334 
1335     if (limit[l1oip_cnt])
1336         hc->b_num = limit[l1oip_cnt];
1337     if (!pri && hc->b_num > 2) {
1338         printk(KERN_ERR "Maximum limit for BRI interface is 2 "
1339                "channels.\n");
1340         return -EINVAL;
1341     }
1342     if (pri && hc->b_num > 126) {
1343         printk(KERN_ERR "Maximum limit for PRI interface is 126 "
1344                "channels.\n");
1345         return -EINVAL;
1346     }
1347     if (pri && hc->b_num > 30) {
1348         printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
1349                "channels.\n");
1350         printk(KERN_WARNING "Your selection of %d channels must be "
1351                "supported by application.\n", hc->limit);
1352     }
1353 
1354     hc->remoteip = ip[l1oip_cnt << 2] << 24
1355         | ip[(l1oip_cnt << 2) + 1] << 16
1356         | ip[(l1oip_cnt << 2) + 2] << 8
1357         | ip[(l1oip_cnt << 2) + 3];
1358     hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT + l1oip_cnt);
1359     if (remoteport[l1oip_cnt])
1360         hc->remoteport = remoteport[l1oip_cnt];
1361     else
1362         hc->remoteport = hc->localport;
1363     if (debug & DEBUG_L1OIP_INIT)
1364         printk(KERN_DEBUG "%s: using local port %d remote ip "
1365                "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1366                hc->localport, hc->remoteip >> 24,
1367                (hc->remoteip >> 16) & 0xff,
1368                (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1369                hc->remoteport, hc->ondemand);
1370 
1371     dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1372     if (!dch)
1373         return -ENOMEM;
1374     dch->debug = debug;
1375     mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1376     dch->hw = hc;
1377     if (pri)
1378         dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1379     else
1380         dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1381     dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1382         (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1383     dch->dev.D.send = handle_dmsg;
1384     dch->dev.D.ctrl = l1oip_dctrl;
1385     dch->dev.nrbchan = hc->b_num;
1386     dch->slot = hc->d_idx;
1387     hc->chan[hc->d_idx].dch = dch;
1388     i = 1;
1389     for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1390         if (ch == 15)
1391             i++;
1392         bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1393         if (!bch) {
1394             printk(KERN_ERR "%s: no memory for bchannel\n",
1395                    __func__);
1396             return -ENOMEM;
1397         }
1398         bch->nr = i + ch;
1399         bch->slot = i + ch;
1400         bch->debug = debug;
1401         mISDN_initbchannel(bch, MAX_DATA_MEM, 0);
1402         bch->hw = hc;
1403         bch->ch.send = handle_bmsg;
1404         bch->ch.ctrl = l1oip_bctrl;
1405         bch->ch.nr = i + ch;
1406         list_add(&bch->ch.list, &dch->dev.bchannels);
1407         hc->chan[i + ch].bch = bch;
1408         set_channelmap(bch->nr, dch->dev.channelmap);
1409     }
1410     /* TODO: create a parent device for this driver */
1411     ret = mISDN_register_device(&dch->dev, NULL, hc->name);
1412     if (ret)
1413         return ret;
1414     hc->registered = 1;
1415 
1416     if (debug & DEBUG_L1OIP_INIT)
1417         printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
1418                __func__, l1oip_cnt + 1);
1419     ret = l1oip_socket_open(hc);
1420     if (ret)
1421         return ret;
1422 
1423     timer_setup(&hc->keep_tl, l1oip_keepalive, 0);
1424     hc->keep_tl.expires = jiffies + 2 * HZ; /* two seconds first time */
1425     add_timer(&hc->keep_tl);
1426 
1427     timer_setup(&hc->timeout_tl, l1oip_timeout, 0);
1428     hc->timeout_on = 0; /* state that we have timer off */
1429 
1430     return 0;
1431 }
1432 
1433 static int __init
1434 l1oip_init(void)
1435 {
1436     int     pri, bundle;
1437     struct l1oip        *hc;
1438     int     ret;
1439 
1440     printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
1441            l1oip_revision);
1442 
1443     if (l1oip_4bit_alloc(ulaw))
1444         return -ENOMEM;
1445 
1446     l1oip_cnt = 0;
1447     while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
1448         switch (type[l1oip_cnt] & 0xff) {
1449         case 1:
1450             pri = 0;
1451             bundle = 0;
1452             break;
1453         case 2:
1454             pri = 1;
1455             bundle = 0;
1456             break;
1457         case 3:
1458             pri = 0;
1459             bundle = 1;
1460             break;
1461         case 4:
1462             pri = 1;
1463             bundle = 1;
1464             break;
1465         default:
1466             printk(KERN_ERR "Card type(%d) not supported.\n",
1467                    type[l1oip_cnt] & 0xff);
1468             l1oip_cleanup();
1469             return -EINVAL;
1470         }
1471 
1472         if (debug & DEBUG_L1OIP_INIT)
1473             printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
1474                    __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1475                    bundle ? "bundled IP packet for all B-channels" :
1476                    "separate IP packets for every B-channel");
1477 
1478         hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1479         if (!hc) {
1480             printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1481             l1oip_cleanup();
1482             return -ENOMEM;
1483         }
1484         INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1485 
1486         spin_lock(&l1oip_lock);
1487         list_add_tail(&hc->list, &l1oip_ilist);
1488         spin_unlock(&l1oip_lock);
1489 
1490         ret = init_card(hc, pri, bundle);
1491         if (ret) {
1492             l1oip_cleanup();
1493             return ret;
1494         }
1495 
1496         l1oip_cnt++;
1497     }
1498     printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1499     return 0;
1500 }
1501 
1502 module_init(l1oip_init);
1503 module_exit(l1oip_cleanup);