* l1oip.c low level driver for tunneling layer 1 over IP * * NOTE: It is not compatible with TDMoIP nor "ISDN over IP". * * Author Andreas Eversberg (jolly@eversberg.eu)
*/
/* module parameters: * type: Value 1 = BRI Value 2 = PRI Value 3 = BRI (multi channel frame, not supported yet) Value 4 = PRI (multi channel frame, not supported yet) A multi channel frame reduces overhead to a single frame for all b-channels, but increases delay. (NOTE: Multi channel frames are not implemented yet.)
* codec: Value 0 = transparent (default) Value 1 = transfer ALAW Value 2 = transfer ULAW Value 3 = transfer generic 4 bit compression.
* ulaw: 0 = we use a-Law (default) 1 = we use u-Law
* limit: limitation of B-channels to control bandwidth (1...126) BRI: 1 or 2 PRI: 1-30, 31-126 (126, because dchannel ist not counted here) Also limited ressources are used for stack, resulting in less channels. It is possible to have more channels than 30 in PRI mode, this must be supported by the application.
* ip: byte representation of remote ip address (127.0.0.1 -> 127,0,0,1) If not given or four 0, no remote address is set. For multiple interfaces, concat ip addresses. (127,0,0,1,127,0,0,1)
* port: port number (local interface) If not given or 0, port 931 is used for fist instance, 932 for next... For multiple interfaces, different ports must be given.
* remoteport: port number (remote interface) If not given or 0, remote port equals local port For multiple interfaces on equal sites, different ports must be given.
* ondemand: 0 = fixed (always transmit packets, even when remote side timed out) 1 = on demand (only transmit packets, when remote side is detected) the default is 0 NOTE: ID must also be set for on demand.
* id: optional value to identify frames. This value must be equal on both peers and should be random. If omitted or 0, no ID is transmitted.
* debug: NOTE: only one debug value must be given for all cards enable debugging (see l1oip.h for debug options)
Special mISDN controls:
op = MISDN_CTRL_SETPEER* p1 = bytes 0-3 : remote IP address in network order (left element first) p2 = bytes 1-2 : remote port in network order (high byte first) optional: p2 = bytes 3-4 : local port in network order (high byte first)
op = MISDN_CTRL_UNSETPEER*
* Use l1oipctrl for comfortable setting or removing ip address. (Layer 1 Over IP CTRL)
L1oIP-Protocol --------------
Frame Header:
7 6 5 4 3 2 1 0 +---------------+ |Ver|T|I|Coding | +---------------+ | ID byte 3 * | +---------------+ | ID byte 2 * | +---------------+ | ID byte 1 * | +---------------+ | ID byte 0 * | +---------------+ |M| Channel | +---------------+ | Length * | +---------------+ | Time Base MSB | +---------------+ | Time Base LSB | +---------------+ | Data.... |
...
| | +---------------+ |M| Channel | +---------------+ | Length * | +---------------+ | Time Base MSB | +---------------+ | Time Base LSB | +---------------+ | Data.... |
...
* Only included in some cases.
- Ver = Version If version is missmatch, the frame must be ignored.
- T = Type of interface Must be 0 for S0 or 1 for E1.
- I = Id present If bit is set, four ID bytes are included in frame.
- ID = Connection ID Additional ID to prevent Denial of Service attacs. Also it prevents hijacking connections with dynamic IP. The ID should be random and must not be 0.
- Coding = Type of codec Must be 0 for no transcoding. Also for D-channel and other HDLC frames. 1 and 2 are reserved for explicitly use of a-LAW or u-LAW codec. 3 is used for generic table compressor.
- M = More channels to come. If this flag is 1, the following byte contains the length of the channel data. After the data block, the next channel will be defined. The flag for the last channel block (or if only one channel is transmitted), must be 0 and no length is given.
- Channel = Channel number 0 reserved 1-3 channel data for S0 (3 is D-channel) 1-31 channel data for E1 (16 is D-channel) 32-127 channel data for extended E1 (16 is D-channel)
- The length is used if the M-flag is 1. It is used to find the next channel inside frame. NOTE: A value of 0 equals 256 bytes of data. -> For larger data blocks, a single frame must be used. -> For larger streams, a single frame or multiple blocks with same channel ID must be used.
- Time Base = Timestamp of first sample in frame The "Time Base" is used to rearange packets and to detect packet loss. The 16 bits are sent in network order (MSB first) and count 1/8000 th of a second. This causes a wrap around each 8,192 seconds. There is no requirement for the initial "Time Base", but 0 should be used for the first packet. In case of HDLC data, this timestamp counts the packet or byte number.
Two Timers:
After initialisation, a timer of 15 seconds is started. Whenever a packet is transmitted, the timer is reset to 15 seconds again. If the timer expires, an empty packet is transmitted. This keep the connection alive.
When a valid packet is received, a timer 65 seconds is started. The interface become ACTIVE. If the timer expires, the interface becomes INACTIVE.
Dynamic IP handling:
To allow dynamic IP, the ID must be non 0. In this case, any packet with the correct port number and ID will be accepted. If the remote side changes its IP the new IP is used for all transmitted packets until it changes again.
On Demand:
If the ondemand parameter is given, the remote IP is set to 0 on timeout. This will stop keepalive traffic to remote. If the remote is online again, traffic will continue to the remote address. This is useful for road warriors. This feature only works with ID set, otherwhise it is highly unsecure.
Socket and Thread -----------------
The complete socket opening and closing is done by a thread. When the thread opened a socket, the hc->socket descriptor is set. Whenever a packet shall be sent to the socket, the hc->socket must be checked whether not NULL. To prevent change in socket descriptor, the hc->socket_lock must be used. To change the socket, a recall of l1oip_socket_open() will safely kill the socket process and create a new one.
if (debug & DEBUG_L1OIP_MSG)
printk(KERN_DEBUG "%s: resetting timer\n", __func__);
/* drop if we have no remote ip or port */ if (!hc->sin_remote.sin_addr.s_addr || !hc->sin_remote.sin_port) { if (debug & DEBUG_L1OIP_MSG)
printk(KERN_DEBUG "%s: dropping frame, because remote " "IP is not set.\n", __func__); return len;
}
/* assemble frame */
*p++ = (L1OIP_VERSION << 6) /* version and coding */
| (hc->pri ? 0x20 : 0x00) /* type */
| (hc->id ? 0x10 : 0x00) /* id */
| localcodec; if (hc->id) {
*p++ = hc->id >> 24; /* id */
*p++ = hc->id >> 16;
*p++ = hc->id >> 8;
*p++ = hc->id;
}
*p++ = 0x00 + channel; /* m-flag, channel */
*p++ = timebase >> 8; /* time base */
*p++ = timebase;
if (buf && len) { /* add data to frame */ if (localcodec == 1 && ulaw)
l1oip_ulaw_to_alaw(buf, len, p); elseif (localcodec == 2 && !ulaw)
l1oip_alaw_to_ulaw(buf, len, p); elseif (localcodec == 3)
len = l1oip_law_to_4bit(buf, len, p,
&hc->chan[channel].codecstate); else
memcpy(p, buf, len);
}
len += p - frame;
/* check for socket in safe condition */
spin_lock(&hc->socket_lock); if (!hc->socket) {
spin_unlock(&hc->socket_lock); return 0;
} /* seize socket */
socket = hc->socket;
hc->socket = NULL;
spin_unlock(&hc->socket_lock); /* send packet */ if (debug & DEBUG_L1OIP_MSG)
printk(KERN_DEBUG "%s: sending packet to socket (len " "= %d)\n", __func__, len);
hc->sendiov.iov_base = frame;
hc->sendiov.iov_len = len;
len = kernel_sendmsg(socket, &hc->sendmsg, &hc->sendiov, 1, len); /* give socket back */
hc->socket = socket; /* no locking required */
/* check packet_id */ if (packet_id) { if (!hc->id) {
printk(KERN_WARNING "%s: packet error - packet has id " "0x%x, but we have not\n", __func__, packet_id); return;
} if (len < 4) {
printk(KERN_WARNING "%s: packet error - packet too " "short for ID value\n", __func__); return;
}
packet_id = (*buf++) << 24;
packet_id += (*buf++) << 16;
packet_id += (*buf++) << 8;
packet_id += (*buf++);
len -= 4;
if (packet_id != hc->id) {
printk(KERN_WARNING "%s: packet error - ID mismatch, " "got 0x%x, we 0x%x\n",
__func__, packet_id, hc->id); return;
}
} else { if (hc->id) {
printk(KERN_WARNING "%s: packet error - packet has no " "ID, but we have\n", __func__); return;
}
}
multiframe: if (len < 1) {
printk(KERN_WARNING "%s: packet error - packet too short, " "channel expected at position %d.\n",
__func__, len-len_start + 1); return;
}
/* get channel and multiframe flag */
channel = *buf & 0x7f;
m = *buf >> 7;
buf++;
len--;
/* check length on multiframe */ if (m) { if (len < 1) {
printk(KERN_WARNING "%s: packet error - packet too " "short, length expected at position %d.\n",
__func__, len_start - len - 1); return;
}
mlen = *buf++;
len--; if (mlen == 0)
mlen = 256; if (len < mlen + 3) {
printk(KERN_WARNING "%s: packet error - length %d at " "position %d exceeds total length %d.\n",
__func__, mlen, len_start-len - 1, len_start); return;
} if (len == mlen + 3) {
printk(KERN_WARNING "%s: packet error - length %d at " "position %d will not allow additional " "packet.\n",
__func__, mlen, len_start-len + 1); return;
}
} else
mlen = len - 2; /* single frame, subtract timebase */
if (len < 2) {
printk(KERN_WARNING "%s: packet error - packet too short, time " "base expected at position %d.\n",
__func__, len-len_start + 1); return;
}
/* get time base */
timebase = (*buf++) << 8;
timebase |= (*buf++);
len -= 2;
/* if inactive, we send up a PH_ACTIVATE and activate */ if (!test_bit(FLG_ACTIVE, &dch->Flags)) { if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
printk(KERN_DEBUG "%s: interface become active due to " "received packet\n", __func__);
test_and_set_bit(FLG_ACTIVE, &dch->Flags);
_queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
NULL, GFP_ATOMIC);
}
/* bind to incoming port */ if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local, sizeof(hc->sin_local))) {
printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
__func__, hc->localport);
ret = -EINVAL; goto fail;
}
/* check sk */ if (socket->sk == NULL) {
printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
ret = -EIO; goto fail;
}
/* give away socket */
spin_lock(&hc->socket_lock);
hc->socket = socket;
spin_unlock(&hc->socket_lock);
/* read loop */ if (debug & DEBUG_L1OIP_SOCKET)
printk(KERN_DEBUG "%s: socket created and open\n",
__func__); while (!signal_pending(current)) {
iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, recvbuf_size);
recvlen = sock_recvmsg(socket, &msg, 0); if (recvlen > 0) {
l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
} else { if (debug & DEBUG_L1OIP_SOCKET)
printk(KERN_WARNING "%s: broken pipe on socket\n", __func__);
}
}
/* get socket back, check first if in use, maybe by send function */
spin_lock(&hc->socket_lock); /* if hc->socket is NULL, it is in use until it is given back */ while (!hc->socket) {
spin_unlock(&hc->socket_lock);
schedule_timeout(HZ / 10);
spin_lock(&hc->socket_lock);
}
hc->socket = NULL;
spin_unlock(&hc->socket_lock);
if (debug & DEBUG_L1OIP_SOCKET)
printk(KERN_DEBUG "%s: socket thread terminating\n",
__func__);
fail: /* free recvbuf */
kfree(recvbuf);
/* close socket */ if (socket)
sock_release(socket);
/* if we got killed, signal completion */
complete(&hc->socket_complete);
hc->socket_thread = NULL; /* show termination of thread */
/* if active, we send up a PH_DEACTIVATE and deactivate */ if (test_bit(FLG_ACTIVE, &dch->Flags)) { if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
printk(KERN_DEBUG "%s: interface become deactivated " "due to timeout\n", __func__);
test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
_queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
NULL, GFP_ATOMIC);
}
}
staticint
l1oip_socket_open(struct l1oip *hc)
{ /* in case of reopen, we need to close first */
l1oip_socket_close(hc);
init_completion(&hc->socket_complete);
/* create receive process */
hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
hc->name); if (IS_ERR(hc->socket_thread)) { int err = PTR_ERR(hc->socket_thread);
printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
__func__, err);
hc->socket_thread = NULL;
sock_release(hc->socket); return err;
} if (debug & DEBUG_L1OIP_SOCKET)
printk(KERN_DEBUG "%s: socket thread created\n", __func__);
if (debug & DEBUG_L1OIP_MSG)
printk(KERN_DEBUG "%s: timeout timer expired, turn layer one " "down.\n", __func__);
hc->timeout_on = 0; /* state that timer must be initialized next time */
/* if timeout, we send up a PH_DEACTIVATE and deactivate */ if (test_bit(FLG_ACTIVE, &dch->Flags)) { if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
printk(KERN_DEBUG "%s: interface become deactivated " "due to timeout\n", __func__);
test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
_queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
NULL, GFP_ATOMIC);
}
/* if we have ondemand set, we remove ip address */ if (hc->ondemand) { if (debug & DEBUG_L1OIP_MSG)
printk(KERN_DEBUG "%s: on demand causes ip address to " "be removed\n", __func__);
hc->sin_remote.sin_addr.s_addr = 0;
}
}
/* * module and stack init
*/ staticint
init_card(struct l1oip *hc, int pri, int bundle)
{ struct dchannel *dch; struct bchannel *bch; int ret; int i, ch;
switch (codec[l1oip_cnt]) { case 0: /* as is */ case 1: /* alaw */ case 2: /* ulaw */ case 3: /* 4bit */ break; default:
printk(KERN_ERR "Codec(%d) not supported.\n",
codec[l1oip_cnt]); return -EINVAL;
}
hc->codec = codec[l1oip_cnt]; if (debug & DEBUG_L1OIP_INIT)
printk(KERN_DEBUG "%s: using codec %d\n",
__func__, hc->codec);
if (id[l1oip_cnt] == 0) {
printk(KERN_WARNING "Warning: No 'id' value given or " "0, this is highly unsecure. Please use 32 " "bit random number 0x...\n");
}
hc->id = id[l1oip_cnt]; if (debug & DEBUG_L1OIP_INIT)
printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
hc->ondemand = ondemand[l1oip_cnt]; if (hc->ondemand && !hc->id) {
printk(KERN_ERR "%s: ondemand option only allowed in " "conjunction with non 0 ID\n", __func__); return -EINVAL;
}
if (limit[l1oip_cnt])
hc->b_num = limit[l1oip_cnt]; if (!pri && hc->b_num > 2) {
printk(KERN_ERR "Maximum limit for BRI interface is 2 " "channels.\n"); return -EINVAL;
} if (pri && hc->b_num > 126) {
printk(KERN_ERR "Maximum limit for PRI interface is 126 " "channels.\n"); return -EINVAL;
} if (pri && hc->b_num > 30) {
printk(KERN_WARNING "Maximum limit for BRI interface is 30 " "channels.\n");
printk(KERN_WARNING "Your selection of %d channels must be " "supported by application.\n", hc->limit);
}
l1oip_cnt = 0; while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) { switch (type[l1oip_cnt] & 0xff) { case 1:
pri = 0;
bundle = 0; break; case 2:
pri = 1;
bundle = 0; break; case 3:
pri = 0;
bundle = 1; break; case 4:
pri = 1;
bundle = 1; break; default:
printk(KERN_ERR "Card type(%d) not supported.\n",
type[l1oip_cnt] & 0xff);
l1oip_cleanup(); return -EINVAL;
}
if (debug & DEBUG_L1OIP_INIT)
printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
__func__, l1oip_cnt, pri ? "PRI" : "BRI",
bundle ? "bundled IP packet for all B-channels" : "separate IP packets for every B-channel");
hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC); if (!hc) {
printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
l1oip_cleanup(); return -ENOMEM;
}
INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.