Added bridgin support for GSM and SIP
[lcr.git] / mISDN.cpp
index dc6be83..6392a4c 100644 (file)
--- a/mISDN.cpp
+++ b/mISDN.cpp
@@ -11,9 +11,6 @@
 
 #include "main.h"
 #include "myisdn.h"
-
-#include <mISDN/mISDNcompat.h>
-int __af_isdn = MISDN_AF_ISDN;
 #include <mISDN/q931.h>
 
 #undef offsetof
@@ -40,7 +37,9 @@ struct mISDNport *mISDNport_first;
 unsigned char mISDN_rand[256];
 int mISDN_rand_count = 0;
 
+#ifdef OLD_MT_ASSIGN
 unsigned int mt_assign_pid = ~0;
+#endif
 
 int mISDNsocket = -1;
 static int upqueue_pipe[2];
@@ -50,9 +49,21 @@ int upqueue_avail = 0;
 static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i);
 static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i);
 
+static int my_mISDNlib_debug(const char *file, int line, const char *func, int level, const char *fmt, va_list va)
+{
+        int ret = 0;
+
+        if (debug_fp > 0)
+               ret = vfprintf(debug_fp, fmt, va);
+       return ret;
+}
+
+static struct mi_ext_fn_s myfn;
+
 int mISDN_initialize(void)
 {
        char filename[256];
+       int ver;
 
        /* try to open raw socket to check kernel */
        mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
@@ -62,7 +73,10 @@ int mISDN_initialize(void)
        }
 
        /* init mlayer3 */
-       init_layer3(4); // buffer of 4
+       // set debug printout function
+       myfn.prt_debug = my_mISDNlib_debug;
+
+       ver = init_layer3(4, &myfn); // buffer of 4
 
        /* open debug, if enabled and not only stack debugging */
        if (options.deb) {
@@ -70,11 +84,10 @@ int mISDN_initialize(void)
                debug_fp = fopen(filename, "a");
        }
 
-       if (options.deb & DEBUG_STACK) {
-               SPRINT(filename, "%s/debug_mISDN.log", LOG_DIR);
-               mISDN_debug_init(0xfffffeff, filename, filename, filename);
-       } else
-               mISDN_debug_init(0, NULL, NULL, NULL);
+       if (options.deb & DEBUG_STACK)
+               mISDN_set_debug_level(0xfffffeff);
+       else
+               mISDN_set_debug_level(0);
 
        if (pipe(upqueue_pipe) < 0)
                FATAL("Failed to open pipe\n");
@@ -89,8 +102,6 @@ void mISDN_deinitialize(void)
 {
        cleanup_layer3();
 
-       mISDN_debug_close();
-
        if (debug_fp)
                fclose(debug_fp);
        debug_fp = NULL;
@@ -130,7 +141,6 @@ PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_setti
        p_m_echo = 0;
        p_m_tone = 0;
        p_m_rxoff = 0;
-       p_m_joindata = 0;
        p_m_inband_send_on = 0;
        p_m_inband_receive_on = 0;
        p_m_dtmf = !mISDNport->ifport->nodtmf;
@@ -656,7 +666,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        case B_STATE_IDLE:
                        if (p_m_remote_ref) {
                                /* export bchannel */
-                               message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
+                               message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type, 0);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "assign");
                                add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -704,7 +714,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        /* in case, the bchannel is exported right after seize_bchannel */
                        /* export bchannel */
                        /* p_m_remote_id is set, when this event happens. */
-                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
+                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type, 0);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "assign");
                        add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -759,7 +769,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
 
                        case B_STATE_REMOTE:
                        /* bchannel is exported, so we re-import */
-                       message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0);
+                       message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0, 0);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "remove");
                        add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -810,7 +820,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                 * OR bchannel is not used anymore
                                 * OR bchannel has been exported to an obsolete ref,
                                 * so reimport, to later export to new remote */
-                               message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0);
+                               message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0, 0);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "remove");
                                add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -846,7 +856,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
 
                        case B_STATE_REMOTE:
                        /* bchannel is exported, so we re-import */
-                       message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0);
+                       message_bchannel_to_remote(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0, 0);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "remove");
                        add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -877,7 +887,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        if (b_port) {
                                /* bchannel is now deactivate, but is requied by Port class, so we reactivate / export */
                                if (p_m_remote_ref) {
-                                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
+                                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type, 0);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
                                        add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -909,7 +919,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        if (b_port) {
                                /* bchannel is now imported, but is requied by Port class, so we reactivate / export */
                                if (p_m_remote_ref) {
-                                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
+                                       message_bchannel_to_remote(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type, 0);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
                                        add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
@@ -1113,7 +1123,7 @@ void message_bchannel_from_remote(class JoinRemote *joinremote, int type, unsign
                chan_trace_header(mISDNport, isdnport, "MESSAGE_BCHANNEL (from remote application)", DIRECTION_NONE);
                add_trace("type", NULL, "export request");
                end_trace();
-               isdnport->p_m_remote_ref = joinremote->j_serial;
+               isdnport->p_m_remote_ref = joinremote->j_remote_ref;
                isdnport->p_m_remote_id = joinremote->j_remote_id;
                if (mISDNport && i>=0) {
                        bchannel_event(mISDNport, i, B_EVENT_EXPORTREQUEST);
@@ -1327,7 +1337,7 @@ void PmISDN::load_tx(void)
        }
 
        if (p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on || p_m_load) {
-               schedule_timer(&p_m_loadtimer, 0, ISDN_TRANSMIT*125);
+               schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
        }
 }
 
@@ -1353,8 +1363,6 @@ static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i)
 void PmISDN::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
 {
        unsigned int cont = *((unsigned int *)data);
-       unsigned char *data_temp;
-       unsigned int length_temp;
        struct lcr_msg *message;
        unsigned char *p;
        int l;
@@ -1367,7 +1375,11 @@ void PmISDN::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len
                if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL) {
                        chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
                        add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
+                       if (!p_m_dtmf)
+                               add_trace("info", NULL, "DTMF is disabled");
                        end_trace();
+                       if (!p_m_dtmf)
+                               return;
                        message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
                        message->param.dtmf = cont & DTMF_TONE_MASK;
                        PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION  DTMF digit '%c'\n", p_name, message->param.dtmf);
@@ -1473,23 +1485,8 @@ void PmISDN::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len
                cryptman_listen_bch(data, len);
        }
 
-       p = data;
-
-       /* send data to epoint */
-       if (p_m_joindata && ACTIVE_EPOINT(p_epointlist)) { /* only if we have an epoint object */
-               length_temp = len;
-               data_temp = p;
-               while(length_temp) {
-                       message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
-                       message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
-                       memcpy(message->param.data.data, data_temp, message->param.data.len);
-                       message_put(message);
-                       if (length_temp <= sizeof(message->param.data.data))
-                               break;
-                       data_temp += sizeof(message->param.data.data);
-                       length_temp -= sizeof(message->param.data.data);
-               }
-       }
+       /* send to remote, if bridged */
+       bridge_tx(data, len);
 }
 
 
@@ -1657,15 +1654,6 @@ void PmISDN::message_mISDNsignal(unsigned int epoint_id, int message_id, union p
                set_conf(oldconf, newconf);
                break;
 
-               case mISDNSIGNAL_JOINDATA:
-               if (p_m_joindata != param->mISDNsignal.joindata) {
-                       p_m_joindata = param->mISDNsignal.joindata;
-                       PDEBUG(DEBUG_BCHANNEL, "we change to joindata=%d.\n", p_m_joindata);
-                       update_rxoff();
-               } else
-                       PDEBUG(DEBUG_BCHANNEL, "we already have joindata=%d.\n", p_m_joindata);
-               break;
-               
                case mISDNSIGNAL_DELAY:
                if (p_m_delay != param->mISDNsignal.delay) {
                        p_m_delay = param->mISDNsignal.delay;
@@ -1752,32 +1740,31 @@ void PmISDN::message_crypt(unsigned int epoint_id, int message_id, union paramet
  */
 int PmISDN::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
 {
-       if (Port::message_epoint(epoint_id, message_id, param))
-               return(1);
+       if (Port::message_epoint(epoint_id, message_id, param)) {
+               if (message_id == MESSAGE_BRIDGE)
+                       update_rxoff();
+               return 1;
+       }
 
        switch(message_id) {
-               case MESSAGE_DATA: /* tx-data from upper layer */
-               txfromup(param->data.data, param->data.len);
-               return(1);
-
                case MESSAGE_mISDNSIGNAL: /* user command */
                PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
                message_mISDNsignal(epoint_id, message_id, param);
-               return(1);
+               return 1;
 
                case MESSAGE_CRYPT: /* crypt control command */
                PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
                message_crypt(epoint_id, message_id, param);
-               return(1);
+               return 1;
        }
 
-       return(0);
+       return 0;
 }
 
 void PmISDN::update_rxoff(void)
 {
        /* call bridges in user space OR crypto OR recording */
-       if (p_m_joindata || p_m_crypt_msg_loops || p_m_crypt_listen || p_record || p_m_inband_receive_on) {
+       if (p_bridge || p_m_crypt_msg_loops || p_m_crypt_listen || p_record || p_m_inband_receive_on) {
                /* rx IS required */
                if (p_m_rxoff) {
                        /* turn on RX */
@@ -1841,7 +1828,7 @@ static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, i
        mISDNport = mISDNport_first;
        while(mISDNport) {
                /* handle queued up-messages (d-channel) */
-               if (!mISDNport->gsm) {
+               if (!mISDNport->isloopback) {
                        while ((mb = mdequeue(&mISDNport->upqueue))) {
                                l3m = &mb->l3;
                                switch(l3m->type) {
@@ -1917,7 +1904,7 @@ static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, i
                                                if (!mISDNport->ntmode || mISDNport->ptp)
                                                        mISDNport->l2link = 0;
                                        }
-                                       if (!mISDNport->gsm && (!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
+                                       if (!mISDNport->isloopback && (!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
                                                if (!mISDNport->l2establish.active && mISDNport->l2hold) {
                                                        PDEBUG(DEBUG_ISDN, "set timer and establish.\n");
                                                        schedule_timer(&mISDNport->l2establish, 5, 0);
@@ -1944,8 +1931,8 @@ static int l2establish_timeout(struct lcr_timer *timer, void *instance, int i)
 {
        struct mISDNport *mISDNport = (struct mISDNport *)instance;
 
-       if (!mISDNport->gsm && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
-//             PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
+       if (!mISDNport->isloopback && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
+               PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
                mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
                schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
        }
@@ -2014,7 +2001,6 @@ static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance,
 static int b_timer_timeout(struct lcr_timer *timer, void *instance, int i)
 {
        struct mISDNport *mISDNport = (struct mISDNport *)instance;
-puts("fires");
 
        bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
 
@@ -2032,6 +2018,7 @@ int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3
        struct mISDNport *mISDNport = (struct mISDNport *)ml3->priv;
        struct mbuffer *mb;
 
+#ifdef OLD_MT_ASSIGN
        /* special MT_ASSIGN handling:
         *
         * if we request a PID from mlayer, we always do it while lcr is locked.
@@ -2049,6 +2036,7 @@ int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3
                mt_assign_pid = pid;
                return(0);
        }
+#endif
        /* queue message, create, if required */
        if (!l3m) {
                l3m = alloc_l3_msg();
@@ -2104,7 +2092,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        int force_nt = ifport->nt;
        int l1hold = ifport->l1hold;
        int l2hold = ifport->l2hold;
-       int gsm = ifport->gsm;
+       int loop = 0;
        int ss5 = ifport->ss5;
        int i, cnt;
        int pri, bri, pots;
@@ -2113,6 +2101,16 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        struct mISDN_devinfo devinfo;
        unsigned int protocol, prop;
 
+       loop = 0;
+//printf("%s == %s\n", ifport->portname, options.loopback_int);
+       if (!strcmp(ifport->portname, options.loopback_lcr))
+               loop = 1;
+
+       if (loop) {
+               if (mISDNloop_open())
+                       return NULL;
+       }
+
        /* check port counts */
        ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
        if (ret < 0) {
@@ -2127,8 +2125,8 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        if (port < 0) {
                port = mISDN_getportbyname(mISDNsocket, cnt, ifport->portname);
                if (port < 0) {
-                       if (gsm)
-                               PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", ifport->portname);
+                       if (loop)
+                               PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface?.\n", ifport->portname);
                        else
                                PERROR_RUNTIME("Port name '%s' not found, use 'misdn_info' tool to list all existing ports.\n", ifport->portname);
                        return(NULL);
@@ -2244,15 +2242,15 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
                mISDNportp = &((*mISDNportp)->next);
        mISDNport = (struct mISDNport *)MALLOC(sizeof(struct mISDNport));
        add_timer(&mISDNport->l2establish, l2establish_timeout, mISDNport, 0);
-       if (gsm | ss5) {
-               /* gsm/ss5 link is always active */
+       if (loop | ss5) {
+               /* loop/ss5 link is always active */
                mISDNport->l1link = 1;
                mISDNport->l2link = 1;
        } else {
                mISDNport->l1link = -1;
                mISDNport->l2link = -1;
        }
-       mISDNport->gsm = gsm;
+       mISDNport->isloopback = loop;
        pmemuse++;
        *mISDNportp = mISDNport;
 
@@ -2264,7 +2262,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        if (ss5) {
                /* try to keep interface enabled */
                l1hold = 1;
-               l2hold = 1;
+               l2hold = 0;
        }
        /* set l2hold */
        switch (l2hold) {
@@ -2294,24 +2292,24 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        if (l2hold) // supports layer 2 hold
               prop |= (1 << MISDN_FLG_L2_HOLD);
        /* open layer 3 and init upqueue */
-       if (gsm) {
+       if (loop) {
                unsigned long on = 1;
                struct sockaddr_mISDN addr;
 
                if (devinfo.nrbchan < 8) {
-                       PERROR_RUNTIME("GSM port %d must have at least 8 b-channels.\n", port);
-                       mISDNport_close(mISDNport);
-                       return(NULL);
+                       printf("loop port %d has a low number of bchannels. (only %d) remember that all interfaces that requires a loopback could run out of channels\n", port, devinfo.nrbchan);
+//                     mISDNport_close(mISDNport);
+//                     return(NULL);
                }
 
-               if ((mISDNport->lcr_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_NT_S0)) < 0) {
-                       PERROR_RUNTIME("GSM port %d failed to open socket.\n", port);
+               if ((mISDNport->lcr_sock = socket(PF_ISDN, SOCK_DGRAM, (bri) ? ISDN_P_TE_S0 : ISDN_P_TE_E1)) < 0) {
+                       PERROR_RUNTIME("loop port %d failed to open socket.\n", port);
                        mISDNport_close(mISDNport);
                        return(NULL);
                }
                /* set nonblocking io */
                if (ioctl(mISDNport->lcr_sock, FIONBIO, &on) < 0) {
-                       PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", port);
+                       PERROR_RUNTIME("loop port %d failed to set socket into nonblocking io.\n", port);
                        mISDNport_close(mISDNport);
                        return(NULL);
                }
@@ -2321,7 +2319,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
                addr.dev = port;
                addr.channel = 0;
                if (bind(mISDNport->lcr_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-                       PERROR_RUNTIME("GSM port %d failed to bind socket. (errno %d)\n", port, errno);
+                       PERROR_RUNTIME("loop port %d failed to bind socket. (errno %d)\n", port, errno);
                        mISDNport_close(mISDNport);
                        return(NULL);
                }
@@ -2365,7 +2363,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        }
 
        /* if ptp, pull up the link */
-       if (!mISDNport->gsm && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
+       if (!mISDNport->isloopback && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
                mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
                l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_REQ, DIRECTION_OUT);
                add_trace("tei", NULL, "%d", 0);
@@ -2478,17 +2476,17 @@ void mISDNport_close(struct mISDNport *mISDNport)
        del_timer(&mISDNport->l2establish);
 
        /* close layer 3, if open */
-       if (!mISDNport->gsm && mISDNport->ml3) {
+       if (!mISDNport->isloopback && mISDNport->ml3) {
                close_layer3(mISDNport->ml3);
        }
 
        /* close gsm socket, if open */
-       if (mISDNport->gsm && mISDNport->lcr_sock > -1) {
+       if (mISDNport->isloopback && mISDNport->lcr_sock > -1) {
                close(mISDNport->lcr_sock);
        }
 
        /* purge upqueue */
-       if (!mISDNport->gsm)
+       if (!mISDNport->isloopback)
                mqueue_purge(&mISDNport->upqueue);
 
        /* remove from list */
@@ -2514,22 +2512,22 @@ void mISDNport_close(struct mISDNport *mISDNport)
 /*
  * enque data from upper buffer
  */
-void PmISDN::txfromup(unsigned char *data, int length)
+int PmISDN::bridge_rx(unsigned char *data, int length)
 {
        unsigned char buf[MISDN_HEADER_LEN+((length>ISDN_LOAD)?length:ISDN_LOAD)];
        struct mISDNhead *hh = (struct mISDNhead *)buf;
        int ret;
 
        if (p_m_b_index < 0)
-               return;
+               return -EIO;
        if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_ACTIVE)
-               return;
+               return -EINVAL;
 
        /* check if high priority tones exist
         * ignore data in this case
         */
        if (p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on)
-               return;
+               return -EBUSY;
 
        /* preload procedure
         * if transmit buffer in DSP module is empty,
@@ -2543,14 +2541,14 @@ void PmISDN::txfromup(unsigned char *data, int length)
                if (ret <= 0)
                        PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
                p_m_load += ISDN_LOAD;
-               schedule_timer(&p_m_loadtimer, 0, ISDN_TRANSMIT*125);
+               schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
        }
 
        /* drop if load would exceed ISDN_MAXLOAD
         * this keeps the delay not too high
         */
        if (p_m_load+length > ISDN_MAXLOAD)
-               return;
+               return -EINVAL;
 
        /* make and send frame */
        hh->prim = PH_DATA_REQ;
@@ -2560,6 +2558,8 @@ void PmISDN::txfromup(unsigned char *data, int length)
        if (ret <= 0)
                PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
        p_m_load += length;
+
+       return 0;
 }
 
 int PmISDN::inband_send(unsigned char *buffer, int len)