From dd93bf46ed0c3c75c1e330db2197703c0b6f8cda Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 19 May 2011 01:40:43 +0200 Subject: logging: fix corrupted output Harald reported a problem in the logging: http://lists.osmocom.org/pipermail/openbsc/2011-May/002896.html Reverting 81e9636454294ae10ef9bc8bf149dd0248afce76 seems to fix the problem. However, that workaround looks ugly. Holger gives us another clue on what was wrong: http://lists.osmocom.org/pipermail/openbsc/2011-May/002905.html While digging in the manpage, I found this: "The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() are equivalent to the functions printf(), fprintf(), sprintf(), snprintf(), respectively, except that they are called with a va_list instead of a variable number of arguments. These functions do not call the va_end macro. Consequently, the value of ap is undefined after the call. The application should call va_end(ap) itself afterwards." --- src/logging.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/logging.c b/src/logging.c index 0911010a..3c9dc03f 100644 --- a/src/logging.c +++ b/src/logging.c @@ -198,6 +198,7 @@ static void _logp(unsigned int subsys, int level, char *file, int line, llist_for_each_entry(tar, &osmo_log_target_list, entry) { struct log_category *category; int output = 0; + va_list bp; category = &tar->categories[subsys]; /* subsystem is not supposed to be logged */ @@ -224,7 +225,12 @@ static void _logp(unsigned int subsys, int level, char *file, int line, if (!output) continue; + /* According to the manpage, vsnprintf leaves the value of ap + * in undefined state. Since _output uses vsnprintf and it may + * be called several times, we have to pass a copy of ap. */ + va_copy(bp, ap); _output(tar, subsys, level, file, line, cont, format, ap); + va_end(bp); } } -- cgit v1.2.3 From 0849c9af2a5637da5ce24118e2f4b9ab9da9c6d9 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 9 Jun 2011 15:04:30 +0200 Subject: socket: add OSMO_SOCK_F_[CONNECT|BIND|NON_BLOCK] flags This extends the socket infrastructure in libosmocore to allow to create non-blocking sockets. Basically, it replaces the connect0_bind1 parameter by one flags parameter. --- include/osmocom/core/socket.h | 11 ++++++++--- src/gsmtap_util.c | 6 ++++-- src/socket.c | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h index b2601c76..612b12c8 100644 --- a/include/osmocom/core/socket.h +++ b/include/osmocom/core/socket.h @@ -5,14 +5,19 @@ struct sockaddr; +/* flags for osmo_sock_init. */ +#define OSMO_SOCK_F_CONNECT (1 << 0) +#define OSMO_SOCK_F_BIND (1 << 1) +#define OSMO_SOCK_F_NONBLOCK (1 << 2) + int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, - const char *host, uint16_t port, int connect0_bind1); + const char *host, uint16_t port, unsigned int flags); int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, - const char *host, uint16_t port, int connect0_bind1); + const char *host, uint16_t port, unsigned int flags); int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, - uint8_t proto, int connect0_bind1); + uint8_t proto, unsigned int flags); /* determine if the given address is a local address */ int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen); diff --git a/src/gsmtap_util.c b/src/gsmtap_util.c index 95458657..c8c26c62 100644 --- a/src/gsmtap_util.c +++ b/src/gsmtap_util.c @@ -124,7 +124,8 @@ int gsmtap_source_init_fd(const char *host, uint16_t port) if (host == NULL) host = "localhost"; - return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, 0); + return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, + OSMO_SOCK_F_CONNECT); } int gsmtap_source_add_sink_fd(int gsmtap_fd) @@ -138,7 +139,8 @@ int gsmtap_source_add_sink_fd(int gsmtap_fd) return rc; if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) { - rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, IPPROTO_UDP, 1); + rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, + IPPROTO_UDP, OSMO_SOCK_F_BIND); if (rc >= 0) return rc; } diff --git a/src/socket.c b/src/socket.c index 0be98b93..f1fcccd5 100644 --- a/src/socket.c +++ b/src/socket.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -18,12 +19,16 @@ #include int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, - const char *host, uint16_t port, int connect0_bind1) + const char *host, uint16_t port, unsigned int flags) { struct addrinfo hints, *result, *rp; int sfd, rc, on = 1; char portbuf[16]; + if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == + (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) + return -EINVAL; + sprintf(portbuf, "%u", port); memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; @@ -31,7 +36,7 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, hints.ai_flags = 0; hints.ai_protocol = proto; - if (connect0_bind1) + if (flags & OSMO_SOCK_F_BIND) hints.ai_flags |= AI_PASSIVE; rc = getaddrinfo(host, portbuf, &hints, &result); @@ -44,10 +49,24 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; - if (connect0_bind1 == 0) { - if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) + if (flags & OSMO_SOCK_F_NONBLOCK) { + if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { + perror("cannot set this socket unblocking"); + close(sfd); + return -EINVAL; + } + } + if (flags & OSMO_SOCK_F_CONNECT) { + rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); + if (rc != -1 || (rc == -1 && errno == EINPROGRESS)) break; } else { + rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, + &on, sizeof(on)); + if (rc < 0) { + perror("cannot setsockopt socket"); + break; + } if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) break; } @@ -63,7 +82,7 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); /* Make sure to call 'listen' on a bound, connection-oriented sock */ - if (connect0_bind1 == 1) { + if (flags & OSMO_SOCK_F_BIND) { switch (type) { case SOCK_STREAM: case SOCK_SEQPACKET: @@ -75,11 +94,11 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, } int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, - const char *host, uint16_t port, int connect0_bind1) + const char *host, uint16_t port, unsigned int flags) { int sfd, rc; - sfd = osmo_sock_init(family, type, proto, host, port, connect0_bind1); + sfd = osmo_sock_init(family, type, proto, host, port, flags); if (sfd < 0) return sfd; @@ -96,7 +115,7 @@ int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, } int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, - uint8_t proto, int connect0_bind1) + uint8_t proto, unsigned int flags) { char host[NI_MAXHOST]; uint16_t port; @@ -127,8 +146,7 @@ int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, return s; } - return osmo_sock_init(ss->sa_family, type, proto, host, - port, connect0_bind1); + return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); } static int sockaddr_equal(const struct sockaddr *a, -- cgit v1.2.3 From 9b21e88367eea8cdf200cf06fae99dec06e712a7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 23 Jun 2011 14:14:20 +0200 Subject: add new 'prim.h' header file for primitives --- include/osmocom/core/Makefile.am | 2 +- include/osmocom/core/prim.h | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 include/osmocom/core/prim.h diff --git a/include/osmocom/core/Makefile.am b/include/osmocom/core/Makefile.am index 3c30362c..1ef37693 100644 --- a/include/osmocom/core/Makefile.am +++ b/include/osmocom/core/Makefile.am @@ -1,6 +1,6 @@ osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h bits.h \ bitvec.h statistics.h utils.h socket.h \ - gsmtap.h write_queue.h \ + gsmtap.h write_queue.h prim.h \ logging.h rate_ctr.h gsmtap_util.h \ plugin.h crc16.h panic.h process.h msgfile.h \ backtrace.h conv.h application.h diff --git a/include/osmocom/core/prim.h b/include/osmocom/core/prim.h new file mode 100644 index 00000000..5494f3c3 --- /dev/null +++ b/include/osmocom/core/prim.h @@ -0,0 +1,20 @@ +#ifndef OSMO_PRIMITIVE_H +#define OSMO_PRIMITIVE_H + +#include + +enum osmo_prim_operation { + PRIM_OP_REQUEST, + PRIM_OP_RESPONSE, + PRIM_OP_INDICATION, + PRIM_OP_CONFIRM, +}; + +struct osmo_prim_hdr { + unsigned int sap; + unsigned int primitive; + enum osmo_prim_operation operation; + struct msgb *msg; /* message containing associated data */ +}; + +#endif -- cgit v1.2.3 From 5e924a31a0b6c73f297a61a1e4cb8b40b6f2946c Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 23 Jun 2011 15:04:47 +0200 Subject: introduce GSM primitive definitions The idea here is to use the osmocom core primitive code ot abstract out primitives for inter-layer comunication in GSM. --- include/osmocom/core/prim.h | 18 ++++++++++++++++++ include/osmocom/gsm/Makefile.am | 2 +- include/osmocom/gsm/prim.h | 13 +++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 include/osmocom/gsm/prim.h diff --git a/include/osmocom/core/prim.h b/include/osmocom/core/prim.h index 5494f3c3..e892c62c 100644 --- a/include/osmocom/core/prim.h +++ b/include/osmocom/core/prim.h @@ -2,6 +2,7 @@ #define OSMO_PRIMITIVE_H #include +#include enum osmo_prim_operation { PRIM_OP_REQUEST, @@ -10,6 +11,11 @@ enum osmo_prim_operation { PRIM_OP_CONFIRM, }; +#define _SAP_GSM_SHIFT 24 + +#define _SAP_GSM_BASE (0x01 << _SAP_GSM_SHIFT) +#define _SAP_TETRA_BASE (0x02 << _SAP_GSM_SHIFT) + struct osmo_prim_hdr { unsigned int sap; unsigned int primitive; @@ -17,4 +23,16 @@ struct osmo_prim_hdr { struct msgb *msg; /* message containing associated data */ }; +static inline void +osmo_prim_init(struct osmo_prim_hdr *oph, unsigned int sap, + unsigned int primitive, enum osmo_prim_operation operation, + struct msgb *msg) +{ + oph->sap = sap; + oph->primitive = primitive; + oph->operation = operation; + oph->msg = msg; +} + +typedef int (*osmo_prim_cb)(struct osmo_prim_hdr *oph, void *ctx); #endif diff --git a/include/osmocom/gsm/Makefile.am b/include/osmocom/gsm/Makefile.am index 547933ec..aa7b1a9a 100644 --- a/include/osmocom/gsm/Makefile.am +++ b/include/osmocom/gsm/Makefile.am @@ -1,6 +1,6 @@ osmogsm_HEADERS = a5.h comp128.h gsm0808.h gsm48_ie.h mncc.h rxlev_stat.h \ gsm0480.h gsm48.h gsm_utils.h rsl.h tlv.h abis_nm.h \ - sysinfo.h + sysinfo.h prim.h SUBDIRS = protocol diff --git a/include/osmocom/gsm/prim.h b/include/osmocom/gsm/prim.h new file mode 100644 index 00000000..95cbb120 --- /dev/null +++ b/include/osmocom/gsm/prim.h @@ -0,0 +1,13 @@ +#ifndef OSMO_GSM_PRIM_H +#define OSMO_GSM_PRIM_H + +#include + +/* enumeration of GSM related SAPs */ +enum osmo_gsm_sap { + SAP_GSM_PH = _SAP_GSM_BASE, + SAP_GSM_DL, + SAP_GSM_MDL, +}; + +#endif -- cgit v1.2.3 From cf7e5dad45141325b26a8d9de2441ac65466d09d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 23 Jun 2011 20:43:13 +0200 Subject: add value_string definitions for RSL message types --- include/osmocom/gsm/rsl.h | 1 + src/gsm/rsl.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/include/osmocom/gsm/rsl.h b/include/osmocom/gsm/rsl.h index 7e46330f..2eb045f2 100644 --- a/include/osmocom/gsm/rsl.h +++ b/include/osmocom/gsm/rsl.h @@ -23,6 +23,7 @@ const char *rsl_chan_nr_str(uint8_t chan_nr); const char *rsl_err_name(uint8_t err); const char *rsl_rlm_cause_name(uint8_t err); +const char *rsl_msg_name(uint8_t err); /* Section 3.3.2.3 TS 05.02. I think this looks like a table */ int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf); diff --git a/src/gsm/rsl.c b/src/gsm/rsl.c index 12ac0cfb..c497ba95 100644 --- a/src/gsm/rsl.c +++ b/src/gsm/rsl.c @@ -250,6 +250,82 @@ const char *rsl_err_name(uint8_t err) return get_value_string(rsl_err_vals, err); } +/* Names for Radio Link Layer Management */ +static const struct value_string rsl_msgt_names[] = { + { RSL_MT_DATA_REQ, "DATA_REQ" }, + { RSL_MT_DATA_IND, "DATA_IND" }, + { RSL_MT_ERROR_IND, "ERROR_IND" }, + { RSL_MT_EST_REQ, "EST_REQ" }, + { RSL_MT_EST_CONF, "EST_CONF" }, + { RSL_MT_EST_IND, "EST_IND" }, + { RSL_MT_REL_REQ, "REL_REQ" }, + { RSL_MT_REL_CONF, "REL_CONF" }, + { RSL_MT_REL_IND, "REL_IND" }, + { RSL_MT_UNIT_DATA_REQ, "UNIT_DATA_REQ" }, + { RSL_MT_UNIT_DATA_IND, "UNIT_DATA_IND" }, + { RSL_MT_SUSP_REQ, "SUSP_REQ" }, + { RSL_MT_SUSP_CONF, "SUSP_CONF" }, + { RSL_MT_RES_REQ, "RES_REQ" }, + { RSL_MT_RECON_REQ, "RECON_REQ" }, + + { RSL_MT_BCCH_INFO, "BCCH_INFO" }, + { RSL_MT_CCCH_LOAD_IND, "CCCH_LOAD_IND" }, + { RSL_MT_CHAN_RQD, "CHAN_RQD" }, + { RSL_MT_DELETE_IND, "DELETE_IND" }, + { RSL_MT_PAGING_CMD, "PAGING_CMD" }, + { RSL_MT_IMMEDIATE_ASSIGN_CMD, "IMM_ASS_CMD" }, + { RSL_MT_SMS_BC_REQ, "SMS_BC_REQ" }, + { RSL_MT_CHAN_CONF, "CHAN_CONF" }, + + { RSL_MT_RF_RES_IND, "RF_RES_IND" }, + { RSL_MT_SACCH_FILL, "SACCH_FILL" }, + { RSL_MT_OVERLOAD, "OVERLOAD" }, + { RSL_MT_ERROR_REPORT, "ERROR_REPORT" }, + { RSL_MT_SMS_BC_CMD, "SMS_BC_CMD" }, + { RSL_MT_CBCH_LOAD_IND, "CBCH_LOAD_IND" }, + { RSL_MT_NOT_CMD, "NOTIFY_CMD" }, + + { RSL_MT_CHAN_ACTIV, "CHAN_ACTIV" }, + { RSL_MT_CHAN_ACTIV_ACK, "CHAN_ACTIV_ACK" }, + { RSL_MT_CHAN_ACTIV_NACK, "CHAN_ACTIV_NACK" }, + { RSL_MT_CONN_FAIL, "CONN_FAIL" }, + { RSL_MT_DEACTIVATE_SACCH, "DEACTIVATE_SACCH" }, + { RSL_MT_ENCR_CMD, "ENCR_CMD" }, + { RSL_MT_HANDO_DET, "HANDOVER_DETECT" }, + { RSL_MT_MEAS_RES, "MEAS_RES" }, + { RSL_MT_MODE_MODIFY_REQ, "MODE_MODIFY_REQ" }, + { RSL_MT_MODE_MODIFY_ACK, "MODE_MODIFY_ACK" }, + { RSL_MT_MODE_MODIFY_NACK, "MODE_MODIFY_NACK" }, + { RSL_MT_PHY_CONTEXT_REQ, "PHY_CONTEXT_REQ" }, + { RSL_MT_PHY_CONTEXT_CONF, "PHY_CONTEXT_CONF" }, + { RSL_MT_RF_CHAN_REL, "RF_CHAN_REL" }, + { RSL_MT_MS_POWER_CONTROL, "MS_POWER_CONTROL" }, + { RSL_MT_BS_POWER_CONTROL, "BS_POWER_CONTROL" }, + { RSL_MT_PREPROC_CONFIG, "PREPROC_CONFIG" }, + { RSL_MT_PREPROC_MEAS_RES, "PREPROC_MEAS_RES" }, + { RSL_MT_RF_CHAN_REL_ACK, "RF_CHAN_REL_ACK" }, + { RSL_MT_SACCH_INFO_MODIFY, "SACCH_INFO_MODIFY" }, + { RSL_MT_TALKER_DET, "TALKER_DETECT" }, + { RSL_MT_LISTENER_DET, "LISTENER_DETECT" }, + { RSL_MT_REMOTE_CODEC_CONF_REP, "REM_CODEC_CONF_REP" }, + { RSL_MT_RTD_REP, "RTD_REQ" }, + { RSL_MT_PRE_HANDO_NOTIF, "HANDO_NOTIF" }, + { RSL_MT_MR_CODEC_MOD_REQ, "CODEC_MOD_REQ" }, + { RSL_MT_MR_CODEC_MOD_ACK, "CODEC_MOD_ACK" }, + { RSL_MT_MR_CODEC_MOD_NACK, "CODEC_MOD_NACK" }, + { RSL_MT_MR_CODEC_MOD_PER, "CODEC_MODE_PER" }, + { RSL_MT_TFO_REP, "TFO_REP" }, + { RSL_MT_TFO_MOD_REQ, "TFO_MOD_REQ" }, + { RSL_MT_LOCATION_INFO, "LOCATION_INFO" }, + { 0, NULL } +}; + + +const char *rsl_msg_name(uint8_t msg_type) +{ + return get_value_string(rsl_msgt_names, msg_type); +} + static const struct value_string rsl_rlm_cause_strs[] = { { RLL_CAUSE_T200_EXPIRED, "Timer T200 expired (N200+1) times" }, { RLL_CAUSE_REEST_REQ, "Re-establishment request" }, -- cgit v1.2.3 From 3d73227b61fb1610a0e4bf317321a298702f76e4 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 25 Jun 2011 21:39:01 +0200 Subject: RSL: Fix typo --- include/osmocom/gsm/protocol/gsm_08_58.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/osmocom/gsm/protocol/gsm_08_58.h b/include/osmocom/gsm/protocol/gsm_08_58.h index 74a4083b..9b641b34 100644 --- a/include/osmocom/gsm/protocol/gsm_08_58.h +++ b/include/osmocom/gsm/protocol/gsm_08_58.h @@ -426,7 +426,7 @@ struct rsl_ie_chan_ident { #define RSL_SYSTEM_INFO_5bis 0x0d #define RSL_SYSTEM_INFO_5ter 0x0e #define RSL_SYSTEM_INFO_10 0x0f -#define REL_EXT_MEAS_ORDER 0x47 +#define RSL_EXT_MEAS_ORDER 0x47 #define RSL_MEAS_INFO 0x48 #define RSL_SYSTEM_INFO_13 0x28 #define RSL_SYSTEM_INFO_2quater 0x29 -- cgit v1.2.3 From 620f7ab35034f78f894319e027a151568c331eea Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 25 Jun 2011 21:39:18 +0200 Subject: sysinfo: add EXTENDED MEASUREMENT ORDER / MEASUREMENT INFO to SI types those two are used in the RSL SACCH FILLING messages and delivered like SI5 / SI6. --- include/osmocom/gsm/sysinfo.h | 2 ++ src/gsm/sysinfo.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/osmocom/gsm/sysinfo.h b/include/osmocom/gsm/sysinfo.h index 6c81134e..b808d6f9 100644 --- a/include/osmocom/gsm/sysinfo.h +++ b/include/osmocom/gsm/sysinfo.h @@ -27,6 +27,8 @@ enum osmo_sysinfo_type { SYSINFO_TYPE_2quater, SYSINFO_TYPE_5bis, SYSINFO_TYPE_5ter, + SYSINFO_TYPE_EMO, + SYSINFO_TYPE_MEAS_INFO, /* FIXME all the various bis and ter */ _MAX_SYSINFO_TYPE }; diff --git a/src/gsm/sysinfo.c b/src/gsm/sysinfo.c index 9df18877..1408f6bf 100644 --- a/src/gsm/sysinfo.c +++ b/src/gsm/sysinfo.c @@ -68,6 +68,8 @@ static const uint8_t sitype2rsl[_MAX_SYSINFO_TYPE] = { [SYSINFO_TYPE_2quater] = RSL_SYSTEM_INFO_2quater, [SYSINFO_TYPE_5bis] = RSL_SYSTEM_INFO_5bis, [SYSINFO_TYPE_5ter] = RSL_SYSTEM_INFO_5ter, + [SYSINFO_TYPE_EMO] = RSL_EXT_MEAS_ORDER, + [SYSINFO_TYPE_MEAS_INFO]= RSL_MEAS_INFO, }; static const uint8_t rsl2sitype[256] = { @@ -92,6 +94,8 @@ static const uint8_t rsl2sitype[256] = { [RSL_SYSTEM_INFO_2quater] = SYSINFO_TYPE_2quater, [RSL_SYSTEM_INFO_5bis] = SYSINFO_TYPE_5bis, [RSL_SYSTEM_INFO_5ter] = SYSINFO_TYPE_5ter, + [RSL_EXT_MEAS_ORDER] = SYSINFO_TYPE_EMO, + [RSL_MEAS_INFO] = SYSINFO_TYPE_MEAS_INFO, }; const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE] = { @@ -116,6 +120,8 @@ const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE] = { { SYSINFO_TYPE_2quater, "2quater" }, { SYSINFO_TYPE_5bis, "5bis" }, { SYSINFO_TYPE_5ter, "5ter" }, + { SYSINFO_TYPE_EMO, "EMO" }, + { SYSINFO_TYPE_MEAS_INFO, "MI" }, { 0, NULL } }; -- cgit v1.2.3 From 2a68c7c8cd2eca65c58c0898d0e1716e9d4c3ecd Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sun, 26 Jun 2011 11:41:48 +0200 Subject: gsm/utils: Adding conversion of "mobile power class" to dBm Written-by: Andreas Eversberg Signed-off-by: Sylvain Munaut --- include/osmocom/gsm/gsm_utils.h | 2 ++ src/gsm/gsm_utils.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/osmocom/gsm/gsm_utils.h b/include/osmocom/gsm/gsm_utils.h index a0ef3c4f..405dfe32 100644 --- a/include/osmocom/gsm/gsm_utils.h +++ b/include/osmocom/gsm/gsm_utils.h @@ -59,6 +59,8 @@ enum gsm_band gsm_band_parse(const char *mhz); int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length); int gsm_7bit_encode(uint8_t *result, const char *data); +unsigned int ms_class_gmsk_dbm(enum gsm_band band, int class); + int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm); int ms_pwr_dbm(enum gsm_band band, uint8_t lvl); diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c index 377705d9..5da713c2 100644 --- a/src/gsm/gsm_utils.c +++ b/src/gsm/gsm_utils.c @@ -171,6 +171,47 @@ int gsm_7bit_encode(uint8_t *result, const char *data) return z; } +/* convert power class to dBm according to GSM TS 05.05 */ +unsigned int ms_class_gmsk_dbm(enum gsm_band band, int class) +{ + switch (band) { + case GSM_BAND_450: + case GSM_BAND_480: + case GSM_BAND_750: + case GSM_BAND_900: + case GSM_BAND_810: + case GSM_BAND_850: + if (class == 1) + return 43; /* 20W */ + if (class == 2) + return 39; /* 8W */ + if (class == 3) + return 37; /* 5W */ + if (class == 4) + return 33; /* 2W */ + if (class == 5) + return 29; /* 0.8W */ + break; + case GSM_BAND_1800: + if (class == 1) + return 30; /* 1W */ + if (class == 2) + return 24; /* 0.25W */ + if (class == 3) + return 36; /* 4W */ + break; + case GSM_BAND_1900: + if (class == 1) + return 30; /* 1W */ + if (class == 2) + return 24; /* 0.25W */ + if (class == 3) + return 33; /* 2W */ + break; + } + return -EINVAL; +} + /* determine power control level for given dBm value, as indicated * by the tables in chapter 4.1.1 of GSM TS 05.05 */ int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm) -- cgit v1.2.3 From 32e1f239b3b8f04c8f26c80ea87c978040c122bf Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Jun 2011 13:07:18 +0200 Subject: merge process.[ch] with application.[ch] --- include/osmocom/core/application.h | 2 ++ include/osmocom/core/process.h | 8 ++--- src/Makefile.am | 2 +- src/application.c | 53 +++++++++++++++++++++++++++ src/process.c | 73 -------------------------------------- src/select.c | 9 +++++ 6 files changed, 67 insertions(+), 80 deletions(-) delete mode 100644 src/process.c diff --git a/include/osmocom/core/application.h b/include/osmocom/core/application.h index c1642ec4..5d098961 100644 --- a/include/osmocom/core/application.h +++ b/include/osmocom/core/application.h @@ -13,4 +13,6 @@ extern struct log_target *osmo_stderr_target; void osmo_init_ignore_signals(void); int osmo_init_logging(const struct log_info *); +int osmo_daemonize(void); + #endif diff --git a/include/osmocom/core/process.h b/include/osmocom/core/process.h index 2d663828..1dde0219 100644 --- a/include/osmocom/core/process.h +++ b/include/osmocom/core/process.h @@ -1,6 +1,2 @@ -#ifndef _OSMO_PROCESS_H -#define _OSMO_PROCESS_H - -int osmo_daemonize(void); - -#endif +#warning "Update from osmocom/core/process.h to osmocom/core/application.h" +#include diff --git a/src/Makefile.am b/src/Makefile.am index 1ae3cff8..bbe6286d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,7 @@ libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c bits.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c rate_ctr.c \ gsmtap_util.c crc16.c panic.c backtrace.c \ - process.c conv.c application.c + conv.c application.c if ENABLE_PLUGIN libosmocore_la_SOURCES += plugin.c diff --git a/src/application.c b/src/application.c index 96b4204e..5f8f4471 100644 --- a/src/application.c +++ b/src/application.c @@ -1,5 +1,6 @@ /* Utility functions to setup applications */ /* + * (C) 2010 by Harald Welte * (C) 2011 by Holger Hans Peter Freyther * * All Rights Reserved @@ -24,6 +25,11 @@ #include #include +#include +#include +#include +#include +#include struct log_target *osmo_stderr_target; @@ -47,3 +53,50 @@ int osmo_init_logging(const struct log_info *log_info) log_set_all_filter(osmo_stderr_target, 1); return 0; } + +int osmo_daemonize(void) +{ + int rc; + pid_t pid, sid; + + /* Check if parent PID == init, in which case we are already a daemon */ + if (getppid() == 1) + return -EEXIST; + + /* Fork from the parent process */ + pid = fork(); + if (pid < 0) { + /* some error happened */ + return pid; + } + + if (pid > 0) { + /* if we have received a positive PID, then we are the parent + * and can exit */ + exit(0); + } + + /* FIXME: do we really want this? */ + umask(0); + + /* Create a new session and set process group ID */ + sid = setsid(); + if (sid < 0) + return sid; + + /* Change to the /tmp directory, which prevents the CWD from being locked + * and unable to remove it */ + rc = chdir("/tmp"); + if (rc < 0) + return rc; + + /* Redirect stdio to /dev/null */ +/* since C89/C99 says stderr is a macro, we can safely do this! */ +#ifdef stderr + freopen("/dev/null", "r", stdin); + freopen("/dev/null", "w", stdout); + freopen("/dev/null", "w", stderr); +#endif + + return 0; +} diff --git a/src/process.c b/src/process.c deleted file mode 100644 index 7f4f12cb..00000000 --- a/src/process.c +++ /dev/null @@ -1,73 +0,0 @@ -/* Process handling support code */ - -/* (C) 2010 by Harald Welte - * All Rights Reserved - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#include -#include -#include -#include -#include - -int osmo_daemonize(void) -{ - int rc; - pid_t pid, sid; - - /* Check if parent PID == init, in which case we are already a daemon */ - if (getppid() == 1) - return -EEXIST; - - /* Fork from the parent process */ - pid = fork(); - if (pid < 0) { - /* some error happened */ - return pid; - } - - if (pid > 0) { - /* if we have received a positive PID, then we are the parent - * and can exit */ - exit(0); - } - - /* FIXME: do we really want this? */ - umask(0); - - /* Create a new session and set process group ID */ - sid = setsid(); - if (sid < 0) - return sid; - - /* Change to the /tmp directory, which prevents the CWD from being locked - * and unable to remove it */ - rc = chdir("/tmp"); - if (rc < 0) - return rc; - - /* Redirect stdio to /dev/null */ -/* since C89/C99 says stderr is a macro, we can safely do this! */ -#ifdef stderr - freopen("/dev/null", "r", stdin); - freopen("/dev/null", "w", stdout); - freopen("/dev/null", "w", stderr); -#endif - - return 0; -} diff --git a/src/select.c b/src/select.c index 4ea95362..4b002ae5 100644 --- a/src/select.c +++ b/src/select.c @@ -47,6 +47,15 @@ int osmo_fd_register(struct osmo_fd *fd) if (flags < 0) return flags; + /* set close-on-exec flag */ + flags = fcntl(fd->fd, F_GETFD); + if (flags < 0) + return flags; + flags |= FD_CLOEXEC; + flags = fcntl(fd->fd, F_SETFD, flags); + if (flags < 0) + return flags; + /* Register FD */ if (fd->fd > maxfd) maxfd = fd->fd; -- cgit v1.2.3 From e9b744e581c13f8bfbf65f0cb78eb555ffa5e591 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Jun 2011 14:19:54 +0200 Subject: add MAX() macro as OSMO_MAX --- include/osmocom/core/utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 0f1ea3bb..01b0ab99 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -2,6 +2,7 @@ #define OSMOCORE_UTIL_H #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b) #include -- cgit v1.2.3 From 2aee7b14ca17995fc311eb2d7d99c23f734adad6 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Jun 2011 14:20:04 +0200 Subject: add gsm48_number_of_paging_subchannels() function (from openbsc's rsl_number_of_paging_subchannels) --- include/osmocom/gsm/gsm48.h | 2 ++ src/gsm/gsm48.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/osmocom/gsm/gsm48.h b/include/osmocom/gsm/gsm48.h index 1e9403bc..16a625aa 100644 --- a/include/osmocom/gsm/gsm48.h +++ b/include/osmocom/gsm/gsm48.h @@ -33,4 +33,6 @@ int gsm48_mi_to_string(char *string, const int str_len, void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf); int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid); +int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc); + #endif diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c index d0345892..4645af4d 100644 --- a/src/gsm/gsm48.c +++ b/src/gsm/gsm48.c @@ -33,6 +33,7 @@ #include #include +#include const struct tlv_definition gsm48_att_tlvdef = { .def = { @@ -413,3 +414,15 @@ int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) return 6; } + +/* From Table 10.5.33 of GSM 04.08 */ +int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc) +{ + if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) { + return OSMO_MAX(1, (3 - chan_desc->bs_ag_blks_res)) + * (chan_desc->bs_pa_mfrms + 2); + } else { + return (9 - chan_desc->bs_ag_blks_res) + * (chan_desc->bs_pa_mfrms + 2); + } +} -- cgit v1.2.3 From 94df39e905b9de424ecb40e9c54ddae0e7b6d4e3 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Jun 2011 14:33:57 +0200 Subject: add some utility functions for paging related calculation (TS 05.02) --- include/osmocom/gsm/Makefile.am | 2 +- include/osmocom/gsm/gsm0502.h | 35 +++++++++++++++++++++++++++++++++++ src/gsm/gsm48.c | 14 +++++++------- 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 include/osmocom/gsm/gsm0502.h diff --git a/include/osmocom/gsm/Makefile.am b/include/osmocom/gsm/Makefile.am index aa7b1a9a..ce3ac9cb 100644 --- a/include/osmocom/gsm/Makefile.am +++ b/include/osmocom/gsm/Makefile.am @@ -1,6 +1,6 @@ osmogsm_HEADERS = a5.h comp128.h gsm0808.h gsm48_ie.h mncc.h rxlev_stat.h \ gsm0480.h gsm48.h gsm_utils.h rsl.h tlv.h abis_nm.h \ - sysinfo.h prim.h + sysinfo.h prim.h gsm0502.h SUBDIRS = protocol diff --git a/include/osmocom/gsm/gsm0502.h b/include/osmocom/gsm/gsm0502.h new file mode 100644 index 00000000..de92c636 --- /dev/null +++ b/include/osmocom/gsm/gsm0502.h @@ -0,0 +1,35 @@ +#ifndef OSMOCOM_GSM_0502_H +#define OSMOCOM_GSM_0502_H + +#include + +#include +#include + +/* Table 5 Clause 7 TS 05.02 */ +static inline unsigned int +gsm0502_get_n_pag_blocks(struct gsm48_control_channel_descr *chan_desc) +{ + if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) + return 3 - chan_desc->bs_ag_blks_res; + else + return 9 - chan_desc->bs_ag_blks_res; +} + +/* Chapter 6.5.2 of TS 05.02 */ +static inline unsigned int +gsm0502_get_ccch_group(uint64_t imsi, unsigned int bs_cc_chans, + unsigned int n_pag_blocks) +{ + return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks; +} + +/* Chapter 6.5.2 of TS 05.02 */ +static inline unsigned int +gsm0502_get_paging_group(uint64_t imsi, unsigned int bs_cc_chans, + int n_pag_blocks) +{ + return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks; +} + +#endif diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c index 4645af4d..44baec60 100644 --- a/src/gsm/gsm48.c +++ b/src/gsm/gsm48.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -418,11 +419,10 @@ int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) /* From Table 10.5.33 of GSM 04.08 */ int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc) { - if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) { - return OSMO_MAX(1, (3 - chan_desc->bs_ag_blks_res)) - * (chan_desc->bs_pa_mfrms + 2); - } else { - return (9 - chan_desc->bs_ag_blks_res) - * (chan_desc->bs_pa_mfrms + 2); - } + unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc); + + if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) + return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2); + else + return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2); } -- cgit v1.2.3 From ea19c978160af32e4fee8001f5308518bcf4fd4c Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Jun 2011 14:47:16 +0200 Subject: import gsm0502_calc_paging_group() from openbsc --- include/osmocom/gsm/gsm0502.h | 3 +++ include/osmocom/gsm/rsl.h | 1 + src/gsm/Makefile.am | 2 +- src/gsm/gsm0502.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/gsm/gsm0502.c diff --git a/include/osmocom/gsm/gsm0502.h b/include/osmocom/gsm/gsm0502.h index de92c636..46b629e4 100644 --- a/include/osmocom/gsm/gsm0502.h +++ b/include/osmocom/gsm/gsm0502.h @@ -32,4 +32,7 @@ gsm0502_get_paging_group(uint64_t imsi, unsigned int bs_cc_chans, return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks; } +unsigned int +gsm0502_calc_paging_group(struct gsm48_control_channel_descr *chan_desc, uint64_t imsi); + #endif diff --git a/include/osmocom/gsm/rsl.h b/include/osmocom/gsm/rsl.h index 2eb045f2..44ded1b1 100644 --- a/include/osmocom/gsm/rsl.h +++ b/include/osmocom/gsm/rsl.h @@ -3,6 +3,7 @@ #include #include +#include #include void rsl_init_rll_hdr(struct abis_rsl_rll_hdr *dh, uint8_t msg_type); diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am index 55b772f1..74893270 100644 --- a/src/gsm/Makefile.am +++ b/src/gsm/Makefile.am @@ -9,6 +9,6 @@ lib_LTLIBRARIES = libosmogsm.la libosmogsm_la_SOURCES = a5.c rxlev_stat.c tlv_parser.c comp128.c gsm_utils.c \ rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \ - gprs_cipher_core.c gsm0480.c abis_nm.c + gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c libosmogsm_la_LDFLAGS = -version-info $(LIBVERSION) libosmogsm_la_LIBADD = $(top_builddir)/src/libosmocore.la diff --git a/src/gsm/gsm0502.c b/src/gsm/gsm0502.c new file mode 100644 index 00000000..df1d8e9e --- /dev/null +++ b/src/gsm/gsm0502.c @@ -0,0 +1,43 @@ +/* Paging helper code */ + +/* (C) 2009 by Holger Hans Peter Freyther + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include +#include +#include +#include + +unsigned int +gsm0502_calc_paging_group(struct gsm48_control_channel_descr *chan_desc, uint64_t imsi) +{ + int ccch_conf; + int bs_cc_chans; + int blocks; + unsigned int group; + + ccch_conf = chan_desc->ccch_conf; + bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf); + /* code word + 2, as 2 channels equals 0x0 */ + blocks = gsm48_number_of_paging_subchannels(chan_desc); + group = gsm0502_get_paging_group(imsi, bs_cc_chans, blocks); + + return group; +} -- cgit v1.2.3 From b43bc048eb4c2c0855d4d7c4ad6b0b3c14e50eb2 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 27 Jun 2011 10:29:17 +0200 Subject: logging: introduce library-internal logging categories We do this by using a trick: library-internal log categories use negative subsystem numbers, which are converted into positive array indexes at the time of logging. library-internal log categories need to be knwo at compile-time, while application-specified categories now are of unlimited number, as they are dynamically allocated. --- include/osmocom/core/logging.h | 17 ++++--- src/application.c | 2 +- src/logging.c | 112 +++++++++++++++++++++++++++++++++-------- 3 files changed, 104 insertions(+), 27 deletions(-) diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index db029402..b477a668 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -5,7 +5,6 @@ #include #include -#define LOG_MAX_CATEGORY 32 #define LOG_MAX_CTX 8 #define LOG_MAX_FILTERS 8 @@ -20,7 +19,7 @@ #endif -void logp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6))); +void logp(int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6))); /* new logging interface */ #define LOGP(ss, level, fmt, args...) \ @@ -37,6 +36,10 @@ void logp(unsigned int subsys, char *file, int line, int cont, const char *forma #define LOG_FILTER_ALL 0x0001 +/* logging levels defined by the library itself */ +#define DLGLOBAL -1 +#define OSMO_NUM_DLIB 1 + struct log_category { uint8_t loglevel; uint8_t enabled; @@ -65,8 +68,9 @@ struct log_info { log_filter *filter_fn; /* per-category information */ - const struct log_info_cat *cat; + struct log_info_cat *cat; unsigned int num_cat; + unsigned int num_cat_user; }; enum log_target_type { @@ -82,7 +86,8 @@ struct log_target { int filter_map; void *filter_data[LOG_MAX_FILTERS+1]; - struct log_category categories[LOG_MAX_CATEGORY+1]; + struct log_category *categories; + uint8_t loglevel; int use_color:1; int print_timestamp:1; @@ -110,10 +115,10 @@ struct log_target { }; /* use the above macros */ -void logp2(unsigned int subsys, unsigned int level, char *file, +void logp2(int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 6, 7))); -void log_init(const struct log_info *cat); +int log_init(const struct log_info *inf, void *talloc_ctx); /* context management */ void log_reset_context(void); diff --git a/src/application.c b/src/application.c index 5f8f4471..b7e943d7 100644 --- a/src/application.c +++ b/src/application.c @@ -44,7 +44,7 @@ void osmo_init_ignore_signals(void) int osmo_init_logging(const struct log_info *log_info) { - log_init(log_info); + log_init(log_info, NULL); osmo_stderr_target = log_target_create_stderr(); if (!osmo_stderr_target) return -1; diff --git a/src/logging.c b/src/logging.c index 3c9dc03f..def13b3b 100644 --- a/src/logging.c +++ b/src/logging.c @@ -40,7 +40,7 @@ #include /* for LOGGING_STR. */ -const struct log_info *osmo_log_info; +struct log_info *osmo_log_info; static struct log_context log_context; static void *tall_log_ctx = NULL; @@ -58,6 +58,16 @@ static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = { { 0, NULL }, }; +#define INT2IDX(x) (-1*(x)-1) +static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = { + [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */ + .name = "DLGLOBAL", + .description = "Library-internal global log family", + .loglevel = LOGL_NOTICE, + .enabled = 1, + }, +}; + /* You have to keep this in sync with the structure loglevel_strs. */ const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = { "Log simply everything", @@ -69,6 +79,12 @@ const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = { NULL, }; +/* special magic for negative (library-internal) log subsystem numbers */ +static int subsys_lib2index(int subsys) +{ + return (subsys * -1) + (osmo_log_info->num_cat_user-1); +} + int log_parse_level(const char *lvl) { return get_string_value(loglevel_strs, lvl); @@ -84,6 +100,8 @@ int log_parse_category(const char *category) int i; for (i = 0; i < osmo_log_info->num_cat; ++i) { + if (osmo_log_info->cat[i].name == NULL) + continue; if (!strcasecmp(osmo_log_info->cat[i].name+1, category)) return i; } @@ -103,7 +121,7 @@ void log_parse_category_mask(struct log_target* target, const char *_mask) char *category_token = NULL; /* Disable everything to enable it afterwards */ - for (i = 0; i < ARRAY_SIZE(target->categories); ++i) + for (i = 0; i < osmo_log_info->num_cat; ++i) target->categories[i].enabled = 0; category_token = strtok(mask, ":"); @@ -112,6 +130,9 @@ void log_parse_category_mask(struct log_target* target, const char *_mask) char* colon = strstr(category_token, ","); int length = strlen(category_token); + if (!osmo_log_info->cat[i].name) + continue; + if (colon) length = colon - category_token; @@ -189,12 +210,17 @@ err: target->output(target, level, buf); } - -static void _logp(unsigned int subsys, int level, char *file, int line, +static void _logp(int subsys, int level, char *file, int line, int cont, const char *format, va_list ap) { struct log_target *tar; + if (subsys < 0) + subsys = subsys_lib2index(subsys); + + if (subsys > osmo_log_info->num_cat) + subsys = DLGLOBAL; + llist_for_each_entry(tar, &osmo_log_target_list, entry) { struct log_category *category; int output = 0; @@ -234,7 +260,7 @@ static void _logp(unsigned int subsys, int level, char *file, int line, } } -void logp(unsigned int subsys, char *file, int line, int cont, +void logp(int subsys, char *file, int line, int cont, const char *format, ...) { va_list ap; @@ -244,7 +270,7 @@ void logp(unsigned int subsys, char *file, int line, int cont, va_end(ap); } -void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) +void logp2(int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) { va_list ap; @@ -326,6 +352,14 @@ struct log_target *log_target_create(void) if (!target) return NULL; + target->categories = talloc_zero_array(target, + struct log_category, + osmo_log_info->num_cat); + if (!target->categories) { + talloc_free(target); + return NULL; + } + INIT_LLIST_HEAD(&target->entry); /* initialize the per-category enabled/loglevel from defaults */ @@ -441,8 +475,11 @@ const char *log_vty_command_string(const struct log_info *info) int size = strlen("logging level () ()") + 1; char *str; - for (i = 0; i < info->num_cat; i++) + for (i = 0; i < info->num_cat; i++) { + if (info->cat[i].name == NULL) + continue; size += strlen(info->cat[i].name) + 1; + } for (i = 0; i < LOGLEVEL_DEFS; i++) size += strlen(loglevel_strs[i].str) + 1; @@ -458,17 +495,19 @@ const char *log_vty_command_string(const struct log_info *info) OSMO_SNPRINTF_RET(ret, rem, offset, len); for (i = 0; i < info->num_cat; i++) { - int j, name_len = strlen(info->cat[i].name)+1; - char name[name_len]; + if (info->cat[i].name) { + int j, name_len = strlen(info->cat[i].name)+1; + char name[name_len]; - for (j = 0; j < name_len; j++) - name[j] = tolower(info->cat[i].name[j]); + for (j = 0; j < name_len; j++) + name[j] = tolower(info->cat[i].name[j]); - name[name_len-1] = '\0'; - ret = snprintf(str + offset, rem, "%s|", name+1); - if (ret < 0) - goto err; - OSMO_SNPRINTF_RET(ret, rem, offset, len); + name[name_len-1] = '\0'; + ret = snprintf(str + offset, rem, "%s|", name+1); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); + } } offset--; /* to remove the trailing | */ rem++; @@ -512,8 +551,11 @@ const char *log_vty_command_description(const struct log_info *info) strlen(LOGGING_STR "Set the log level for a specified category\n") + 1; - for (i = 0; i < info->num_cat; i++) + for (i = 0; i < info->num_cat; i++) { + if (info->cat[i].name == NULL) + continue; size += strlen(info->cat[i].description) + 1; + } for (i = 0; i < LOGLEVEL_DEFS; i++) size += strlen(loglevel_descriptions[i]) + 1; @@ -537,6 +579,8 @@ const char *log_vty_command_description(const struct log_info *info) OSMO_SNPRINTF_RET(ret, rem, offset, len); for (i = 0; i < info->num_cat; i++) { + if (info->cat[i].name == NULL) + continue; ret = snprintf(str + offset, rem, "%s\n", info->cat[i].description); if (ret < 0) @@ -555,8 +599,36 @@ err: return str; } -void log_init(const struct log_info *cat) +int log_init(const struct log_info *inf, void *ctx) { - tall_log_ctx = talloc_named_const(NULL, 1, "logging"); - osmo_log_info = cat; + int i; + + tall_log_ctx = talloc_named_const(ctx, 1, "logging"); + if (!tall_log_ctx) + return -ENOMEM; + + osmo_log_info = talloc_zero(tall_log_ctx, struct log_info); + if (!osmo_log_info) + return -ENOMEM; + + osmo_log_info->num_cat_user = inf->num_cat; + /* total number = number of user cat + library cat */ + osmo_log_info->num_cat = inf->num_cat + OSMO_NUM_DLIB; + + osmo_log_info->cat = talloc_zero_array(osmo_log_info, + struct log_info_cat, + osmo_log_info->num_cat); + if (!osmo_log_info->cat) { + talloc_free(osmo_log_info); + osmo_log_info = NULL; + return -ENOMEM; + } + + /* copy over the user part */ + for (i = 0; i < inf->num_cat; i++) { + memcpy(&osmo_log_info->cat[i], &inf->cat[i], + sizeof(struct log_info_cat)); + } + + /* copy over the library part */ } -- cgit v1.2.3 From 1f0b8c26f78a71b1dd948c464f15d0166e706792 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 27 Jun 2011 10:51:37 +0200 Subject: add LAPDm code from osmocom-bb into libosmocore --- include/osmocom/core/logging.h | 3 +- include/osmocom/gsm/Makefile.am | 2 +- include/osmocom/gsm/lapdm.h | 186 +++ src/gsm/Makefile.am | 3 +- src/gsm/lapdm.c | 2510 +++++++++++++++++++++++++++++++++++++++ src/logging.c | 6 + 6 files changed, 2707 insertions(+), 3 deletions(-) create mode 100644 include/osmocom/gsm/lapdm.h create mode 100644 src/gsm/lapdm.c diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index b477a668..4276b3af 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -38,7 +38,8 @@ void logp(int subsys, char *file, int line, int cont, const char *format, ...) _ /* logging levels defined by the library itself */ #define DLGLOBAL -1 -#define OSMO_NUM_DLIB 1 +#define DLLAPDM -2 +#define OSMO_NUM_DLIB 2 struct log_category { uint8_t loglevel; diff --git a/include/osmocom/gsm/Makefile.am b/include/osmocom/gsm/Makefile.am index ce3ac9cb..90f19bc5 100644 --- a/include/osmocom/gsm/Makefile.am +++ b/include/osmocom/gsm/Makefile.am @@ -1,6 +1,6 @@ osmogsm_HEADERS = a5.h comp128.h gsm0808.h gsm48_ie.h mncc.h rxlev_stat.h \ gsm0480.h gsm48.h gsm_utils.h rsl.h tlv.h abis_nm.h \ - sysinfo.h prim.h gsm0502.h + sysinfo.h prim.h gsm0502.h lapdm.h SUBDIRS = protocol diff --git a/include/osmocom/gsm/lapdm.h b/include/osmocom/gsm/lapdm.h new file mode 100644 index 00000000..2e78aeee --- /dev/null +++ b/include/osmocom/gsm/lapdm.h @@ -0,0 +1,186 @@ +#ifndef _OSMOCOM_LAPDM_H +#define _OSMOCOM_LAPDM_H + +#include + +#include +#include +#include + +/* primitive related sutff */ + +enum osmo_ph_prim { + PRIM_PH_DATA, /* PH-DATA */ + PRIM_PH_RACH, /* PH-RANDOM_ACCESS */ + PRIM_PH_CONN, /* PH-CONNECT */ + PRIM_PH_EMPTY_FRAME, /* PH-EMPTY_FRAME */ + PRIM_PH_RTS, /* PH-RTS */ +}; + +/* for PH-RANDOM_ACCESS.req */ +struct ph_rach_req_param { + uint8_t ra; + uint8_t ta; + uint8_t tx_power; + uint8_t is_combined_ccch; + uint16_t offset; +}; + +/* for PH-RANDOM_ACCESS.ind */ +struct ph_rach_ind_param { + uint8_t ra; + uint8_t acc_delay; + uint32_t fn; +}; + +/* for PH-[UNIT]DATA.{req,ind} */ +struct ph_data_param { + uint8_t link_id; + uint8_t chan_nr; +}; + +struct ph_conn_ind_param { + uint32_t fn; +}; + +struct osmo_phsap_prim { + struct osmo_prim_hdr oph; + union { + struct ph_data_param data; + struct ph_rach_req_param rach_req; + struct ph_rach_ind_param rach_ind; + struct ph_conn_ind_param conn_ind; + } u; +}; + +enum lapdm_mode { + LAPDM_MODE_MS, + LAPDM_MODE_BTS, +}; + +enum lapdm_state { + LAPDm_STATE_NULL = 0, + LAPDm_STATE_IDLE, + LAPDm_STATE_SABM_SENT, + LAPDm_STATE_MF_EST, + LAPDm_STATE_TIMER_RECOV, + LAPDm_STATE_DISC_SENT, +}; + +struct lapdm_entity; + +struct lapdm_msg_ctx { + struct lapdm_datalink *dl; + int lapdm_fmt; + uint8_t n201; + uint8_t chan_nr; + uint8_t link_id; + uint8_t addr; + uint8_t ctrl; + uint8_t ta_ind; + uint8_t tx_power_ind; +}; + +/* TS 04.06 / Section 3.5.2 */ +struct lapdm_datalink { + uint8_t V_send; /* seq nr of next I frame to be transmitted */ + uint8_t V_ack; /* last frame ACKed by peer */ + uint8_t N_send; /* ? set to V_send at Tx time*/ + uint8_t V_recv; /* seq nr of next I frame expected to be received */ + uint8_t N_recv; /* expected send seq nr of the next received I frame */ + uint32_t state; + int seq_err_cond; /* condition of sequence error */ + uint8_t own_busy, peer_busy; + struct osmo_timer_list t200; + uint8_t retrans_ctr; + struct llist_head send_queue; /* frames from L3 */ + struct msgb *send_buffer; /* current frame transmitting */ + int send_out; /* how much was sent from send_buffer */ + uint8_t tx_hist[8][200]; /* tx history buffer */ + int tx_length[8]; /* length in history buffer */ + struct llist_head tx_queue; /* frames to L1 */ + struct lapdm_msg_ctx mctx; /* context of established connection */ + struct msgb *rcv_buffer; /* buffer to assemble the received message */ + + struct lapdm_entity *entity; +}; + +enum lapdm_dl_sapi { + DL_SAPI0 = 0, + DL_SAPI3 = 1, + _NR_DL_SAPI +}; + +typedef int (*lapdm_cb_t)(struct msgb *msg, struct lapdm_entity *le, void *ctx); + +struct lapdm_cr_ent { + uint8_t cmd; + uint8_t resp; +}; + +#define LAPDM_ENT_F_EMPTY_FRAME 0x0001 +#define LAPDM_ENT_F_POLLING_ONLY 0x0002 + +/* register message handler for messages that are sent from L2->L3 */ +struct lapdm_entity { + struct lapdm_datalink datalink[_NR_DL_SAPI]; + int last_tx_dequeue; /* last entity that was dequeued */ + int tx_pending; /* currently a pending frame not confirmed by L1 */ + enum lapdm_mode mode; /* are we in BTS mode or MS mode */ + unsigned int flags; + + struct { + /* filled-in once we set the lapdm_mode above */ + struct lapdm_cr_ent loc2rem; + struct lapdm_cr_ent rem2loc; + } cr; + + void *l1_ctx; /* context for layer1 instance */ + void *l3_ctx; /* context for layer3 instance */ + + osmo_prim_cb l1_prim_cb; + lapdm_cb_t l3_cb; /* callback for sending stuff to L3 */ + + struct lapdm_channel *lapdm_ch; +}; + +/* the two lapdm_entities that form a GSM logical channel (ACCH + DCCH) */ +struct lapdm_channel { + struct llist_head list; + char *name; + struct lapdm_entity lapdm_acch; + struct lapdm_entity lapdm_dcch; +}; + +const char *get_rsl_name(int value); +extern const char *lapdm_state_names[]; + +/* initialize a LAPDm entity */ +void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode); +void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode); + +/* deinitialize a LAPDm entity */ +void lapdm_entity_exit(struct lapdm_entity *le); +void lapdm_channel_exit(struct lapdm_channel *lc); + +/* input into layer2 (from layer 1) */ +int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le); + +/* input into layer2 (from layer 3) */ +int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc); + +void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx); +void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx); + +int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode); +int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode); + +void lapdm_entity_reset(struct lapdm_entity *le); +void lapdm_channel_reset(struct lapdm_channel *lc); + +void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags); +void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags); + +int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp); + +#endif /* _OSMOCOM_LAPDM_H */ diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am index 74893270..88b2d9a7 100644 --- a/src/gsm/Makefile.am +++ b/src/gsm/Makefile.am @@ -9,6 +9,7 @@ lib_LTLIBRARIES = libosmogsm.la libosmogsm_la_SOURCES = a5.c rxlev_stat.c tlv_parser.c comp128.c gsm_utils.c \ rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \ - gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c + gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c \ + lapdm.c libosmogsm_la_LDFLAGS = -version-info $(LIBVERSION) libosmogsm_la_LIBADD = $(top_builddir)/src/libosmocore.la diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c new file mode 100644 index 00000000..b804a929 --- /dev/null +++ b/src/gsm/lapdm.c @@ -0,0 +1,2510 @@ +/* GSM LAPDm (TS 04.06) implementation */ + +/* (C) 2010-2011 by Harald Welte + * (C) 2010 by Andreas Eversberg + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/* Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue + * + * RX data is stored in the rcv_buffer (pointer). If the message is complete, it + * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is + * received while there is an incomplete rcv_buffer, it is appended to it. + * + * TX data is stored in the send_queue first. When transmitting a frame, + * the first message in the send_queue is moved to the send_buffer. There it + * resides until all fragments are acknowledged. Fragments to be sent by I + * frames are stored in the tx_hist buffer for resend, if required. Also the + * current fragment is copied into the tx_queue. There it resides until it is + * forwarded to layer 1. + * + * In case we have SAPI 0, we only have a window size of 1, so the unack- + * nowledged message resides always in the send_buffer. In case of a suspend, + * it can be written back to the first position of the send_queue. + * + * The layer 1 normally sends a PH-READY-TO-SEND. But because we use + * asynchronous transfer between layer 1 and layer 2 (serial link), we must + * send a frame before layer 1 reaches the right timeslot to send it. So we + * move the tx_queue to layer 1 when there is not already a pending frame, and + * wait until acknowledge after the frame has been sent. If we receive an + * acknowledge, we can send the next frame from the buffer, if any. + * + * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it + * will trigger next I frame, if possible. + * + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +/* TS 04.06 Figure 4 / Section 3.2 */ +#define LAPDm_LPD_NORMAL 0 +#define LAPDm_LPD_SMSCB 1 +#define LAPDm_SAPI_NORMAL 0 +#define LAPDm_SAPI_SMS 3 +#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1) + +#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7) +#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1) +#define LAPDm_ADDR_EA(addr) ((addr) & 0x1) + +/* TS 04.06 Table 3 / Section 3.4.3 */ +#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1)) +#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1) +#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3) + +#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0) +#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1) +#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3) + +#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3) +#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1) + +#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2) + +#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1) +#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5) + +/* TS 04.06 Table 4 / Section 3.8.1 */ +#define LAPDm_U_SABM 0x7 +#define LAPDm_U_DM 0x3 +#define LAPDm_U_UI 0x0 +#define LAPDm_U_DISC 0x8 +#define LAPDm_U_UA 0xC + +#define LAPDm_S_RR 0x0 +#define LAPDm_S_RNR 0x1 +#define LAPDm_S_REJ 0x2 + +#define LAPDm_LEN(len) ((len << 2) | 0x1) +#define LAPDm_MORE 0x2 + +/* TS 04.06 Section 5.8.3 */ +#define N201_AB_SACCH 18 +#define N201_AB_SDCCH 20 +#define N201_AB_FACCH 20 +#define N201_Bbis 23 +#define N201_Bter_SACCH 21 +#define N201_Bter_SDCCH 23 +#define N201_Bter_FACCH 23 +#define N201_B4 19 + +/* 5.8.2.1 N200 during establish and release */ +#define N200_EST_REL 5 +/* 5.8.2.1 N200 during timer recovery state */ +#define N200_TR_SACCH 5 +#define N200_TR_SDCCH 23 +#define N200_TR_FACCH_FR 34 +#define N200_TR_EFACCH_FR 48 +#define N200_TR_FACCH_HR 29 +/* FIXME: this depends on chan type */ +#define N200 N200_TR_SACCH + +#define CR_MS2BS_CMD 0 +#define CR_MS2BS_RESP 1 +#define CR_BS2MS_CMD 1 +#define CR_BS2MS_RESP 0 + +/* Set T200 to 1 Second (OpenBTS uses 900ms) */ +#define T200 1, 0 + +/* k value for each SAPI */ +static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1}; + +enum lapdm_format { + LAPDm_FMT_A, + LAPDm_FMT_B, + LAPDm_FMT_Bbis, + LAPDm_FMT_Bter, + LAPDm_FMT_B4, +}; + +static void lapdm_t200_cb(void *data); +static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line); + +/* UTILITY FUNCTIONS */ + +static inline uint8_t inc_mod8(uint8_t x) +{ + return (x + 1) & 7; +} + +static inline uint8_t add_mod8(uint8_t x, uint8_t y) +{ + return (x + y) & 7; +} + +static inline uint8_t sub_mod8(uint8_t x, uint8_t y) +{ + return (x - y) & 7; /* handle negative results correctly */ +} + +static void lapdm_dl_init(struct lapdm_datalink *dl, + struct lapdm_entity *entity) +{ + memset(dl, 0, sizeof(*dl)); + INIT_LLIST_HEAD(&dl->send_queue); + INIT_LLIST_HEAD(&dl->tx_queue); + dl->state = LAPDm_STATE_IDLE; + dl->t200.data = dl; + dl->t200.cb = &lapdm_t200_cb; + dl->entity = entity; +} + +void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(le->datalink); i++) + lapdm_dl_init(&le->datalink[i], le); + + lapdm_entity_set_mode(le, mode); +} + +void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode) +{ + lapdm_entity_init(&lc->lapdm_acch, mode); + lapdm_entity_init(&lc->lapdm_dcch, mode); +} + + +static void lapdm_dl_flush_send(struct lapdm_datalink *dl) +{ + struct msgb *msg; + + /* Flush send-queue */ + while ((msg = msgb_dequeue(&dl->send_queue))) + msgb_free(msg); + + /* Clear send-buffer */ + if (dl->send_buffer) { + msgb_free(dl->send_buffer); + dl->send_buffer = NULL; + } +} + +static void lapdm_dl_flush_tx(struct lapdm_datalink *dl) +{ + struct msgb *msg; + unsigned int i; + + while ((msg = msgb_dequeue(&dl->tx_queue))) + msgb_free(msg); + for (i = 0; i < 8; i++) + dl->tx_length[i] = 0; +} + +void lapdm_entity_exit(struct lapdm_entity *le) +{ + unsigned int i; + struct lapdm_datalink *dl; + + for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { + dl = &le->datalink[i]; + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); + if (dl->rcv_buffer) + msgb_free(dl->rcv_buffer); + } +} + +void lapdm_channel_exit(struct lapdm_channel *lc) +{ + lapdm_entity_exit(&lc->lapdm_acch); + lapdm_entity_exit(&lc->lapdm_dcch); +} + +static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state) +{ + LOGP(DLLAPDM, LOGL_INFO, "new state %s -> %s\n", + lapdm_state_names[dl->state], lapdm_state_names[state]); + + dl->state = state; +} + +static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) +{ + switch (sapi) { + case LAPDm_SAPI_NORMAL: + return &le->datalink[0]; + case LAPDm_SAPI_SMS: + return &le->datalink[1]; + default: + return NULL; + } +} + +/* remove the L2 header from a MSGB */ +static inline unsigned char *msgb_pull_l2h(struct msgb *msg) +{ + unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h); + msg->l2h = NULL; + return ret; +} + +/* Append padding (if required) */ +static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201) +{ + int pad_len = n201 - msgb_l2len(msg); + uint8_t *data; + + if (pad_len < 0) { + LOGP(DLLAPDM, LOGL_ERROR, + "cannot pad message that is already too big!\n"); + return; + } + + data = msgb_put(msg, pad_len); + memset(data, 0x2B, pad_len); +} + +/* input function that L2 calls when sending messages up to L3 */ +static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le) +{ + if (!le->l3_cb) { + msgb_free(msg); + return -EIO; + } + + /* call the layer2 message handler that is registered */ + return le->l3_cb(msg, le, le->l3_ctx); +} + +/* write a frame into the tx queue */ +static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, + uint8_t chan_nr, uint8_t link_id, uint8_t n201) +{ + struct lapdm_entity *le = dl->entity; + struct osmo_phsap_prim pp; + + /* if there is a pending message, queue it */ + if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) { + *msgb_push(msg, 1) = n201; + *msgb_push(msg, 1) = link_id; + *msgb_push(msg, 1) = chan_nr; + msgb_enqueue(&dl->tx_queue, msg); + return -EBUSY; + } + + osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA, + PRIM_OP_REQUEST, msg); + pp.u.data.chan_nr = chan_nr; + pp.u.data.link_id = link_id; + + /* send the frame now */ + le->tx_pending = 0; /* disabled flow control */ + lapdm_pad_msgb(msg, n201); + + return le->l1_prim_cb(&pp.oph, le->l1_ctx); +} + +static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le) +{ + struct lapdm_datalink *dl; + int last = le->last_tx_dequeue; + int i = last, n = ARRAY_SIZE(le->datalink); + struct msgb *msg = NULL; + + /* round-robin dequeue */ + do { + /* next */ + i = (i + 1) % n; + dl = &le->datalink[i]; + if ((msg = msgb_dequeue(&dl->tx_queue))) + break; + } while (i != last); + + if (msg) { + /* Set last dequeue position */ + le->last_tx_dequeue = i; + } + + return msg; +} + +/* dequeue a msg that's pending transmission via L1 and wrap it into + * a osmo_phsap_prim */ +int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp) +{ + struct msgb *msg; + uint8_t n201; + + msg = tx_dequeue_msgb(le); + if (!msg) + return -ENODEV; + + /* if we have a message, send PH-DATA.req */ + osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA, + PRIM_OP_REQUEST, msg); + + /* Pull chan_nr and link_id */ + pp->u.data.chan_nr = *msg->data; + msgb_pull(msg, 1); + pp->u.data.link_id = *msg->data; + msgb_pull(msg, 1); + n201 = *msg->data; + msgb_pull(msg, 1); + + /* Pad the frame, we can transmit now */ + lapdm_pad_msgb(msg, n201); + + return 0; +} + +/* get next frame from the tx queue. because the ms has multiple datalinks, + * each datalink's queue is read round-robin. + */ +static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le) +{ + struct osmo_phsap_prim pp; + + /* we may send again */ + le->tx_pending = 0; + + /* free confirm message */ + if (msg) + msgb_free(msg); + + if (lapdm_phsap_dequeue_prim(le, &pp) < 0) { + /* no message in all queues */ + + /* If user didn't request PH-EMPTY_FRAME.req, abort */ + if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME)) + return 0; + + /* otherwise, send PH-EMPTY_FRAME.req */ + osmo_prim_init(&pp.oph, SAP_GSM_PH, + PRIM_PH_EMPTY_FRAME, + PRIM_OP_REQUEST, NULL); + } else { + le->tx_pending = 1; + } + + return le->l1_prim_cb(&pp.oph, le->l1_ctx); +} + +/* Create RSLms various RSLms messages */ +static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx, + struct msgb *msg) +{ + /* Add the RSL + RLL header */ + rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1); + + /* send off the RSLms message to L3 */ + return rslms_sendmsg(msg, mctx->dl->entity); +} + +/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */ +static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg) +{ + uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg); + struct abis_rsl_rll_hdr *rllh; + + /* Add the RSL + RLL header */ + msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len); + msgb_push(msg, 2 + 2); + rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr, + mctx->link_id, 1); + rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg); + + rllh->data[0] = RSL_IE_TIMING_ADVANCE; + rllh->data[1] = mctx->ta_ind; + + rllh->data[2] = RSL_IE_MS_POWER; + rllh->data[3] = mctx->tx_power_ind; + + return rslms_sendmsg(msg, mctx->dl->entity); +} + +static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx) +{ + struct msgb *msg; + + msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1); + + /* send off the RSLms message to L3 */ + return rslms_sendmsg(msg, mctx->dl->entity); +} + +static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx) +{ + struct msgb *msg; + + LOGP(DLLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); + msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1); + msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr)); + msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause); + return rslms_sendmsg(msg, mctx->dl->entity); +} + +static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind) +{ + if (!(length_ind & 0x01)) { + /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION + * primitive with cause "frame not implemented" is sent to the + * mobile management entity. */ + LOGP(DLLAPDM, LOGL_NOTICE, + "we don't support multi-octet length\n"); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + return 0; +} + +static int lapdm_send_resend(struct lapdm_datalink *dl) +{ + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend"); + int length; + + /* Resend SABM/DISC from tx_hist */ + length = dl->tx_length[0]; + msg->l2h = msgb_put(msg, length); + memcpy(msg->l2h, dl->tx_hist[dl->V_send], length); + + return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id, + dl->mctx.n201); +} + +static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data) +{ + uint8_t sapi = mctx->link_id & 7; + uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA"); + struct lapdm_entity *le = mctx->dl->entity; + + msg->l2h = msgb_put(msg, 3 + len); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit); + msg->l2h[2] = LAPDm_LEN(len); + if (len) + memcpy(msg->l2h + 3, data, len); + + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + mctx->n201); +} + +static int lapdm_send_dm(struct lapdm_msg_ctx *mctx) +{ + uint8_t sapi = mctx->link_id & 7; + uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM"); + struct lapdm_entity *le = mctx->dl->entity; + + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit); + msg->l2h[2] = 0; + + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + mctx->n201); +} + +static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) +{ + uint8_t sapi = mctx->link_id & 7; + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR"); + struct lapdm_entity *le = mctx->dl->entity; + + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); + msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit); + msg->l2h[2] = LAPDm_LEN(0); + + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + mctx->n201); +} + +static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) +{ + uint8_t sapi = mctx->link_id & 7; + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR"); + struct lapdm_entity *le = mctx->dl->entity; + + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); + msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit); + msg->l2h[2] = LAPDm_LEN(0); + + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + mctx->n201); +} + +static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit) +{ + uint8_t sapi = mctx->link_id & 7; + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ"); + struct lapdm_entity *le = mctx->dl->entity; + + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); + msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit); + msg->l2h[2] = LAPDm_LEN(0); + + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + mctx->n201); +} + +/* Timer callback on T200 expiry */ +static void lapdm_t200_cb(void *data) +{ + struct lapdm_datalink *dl = data; + + LOGP(DLLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state); + + switch (dl->state) { + case LAPDm_STATE_SABM_SENT: + /* 5.4.1.3 */ + if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) { + /* send RELEASE INDICATION to L3 */ + send_rll_simple(RSL_MT_REL_IND, &dl->mctx); + /* send MDL ERROR INIDCATION to L3 */ + rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); + /* flush tx buffers */ + lapdm_dl_flush_tx(dl); + /* go back to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + /* NOTE: we must not change any other states or buffers + * and queues, since we may reconnect after handover + * failure. the buffered messages is replaced there */ + break; + } + /* retransmit SABM command */ + lapdm_send_resend(dl); + /* increment re-transmission counter */ + dl->retrans_ctr++; + /* restart T200 (PH-READY-TO-SEND) */ + osmo_timer_schedule(&dl->t200, T200); + break; + case LAPDm_STATE_DISC_SENT: + /* 5.4.4.3 */ + if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) { + /* send RELEASE INDICATION to L3 */ + send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); + /* send MDL ERROR INIDCATION to L3 */ + rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); + /* flush buffers */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); + /* go back to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + /* NOTE: we must not change any other states or buffers + * and queues, since we may reconnect after handover + * failure. the buffered messages is replaced there */ + break; + } + /* retransmit DISC command */ + lapdm_send_resend(dl); + /* increment re-transmission counter */ + dl->retrans_ctr++; + /* restart T200 (PH-READY-TO-SEND) */ + osmo_timer_schedule(&dl->t200, T200); + break; + case LAPDm_STATE_MF_EST: + /* 5.5.7 */ + dl->retrans_ctr = 0; + lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV); + /* fall through */ + case LAPDm_STATE_TIMER_RECOV: + dl->retrans_ctr++; + if (dl->retrans_ctr < N200) { + /* retransmit I frame (V_s-1) with P=1, if any */ + if (dl->tx_length[sub_mod8(dl->V_send, 1)]) { + struct msgb *msg; + int length; + + LOGP(DLLAPDM, LOGL_INFO, "retransmit last frame " + "V(S)=%d\n", sub_mod8(dl->V_send, 1)); + /* Create I frame (segment) from tx_hist */ + length = dl->tx_length[sub_mod8(dl->V_send, 1)]; + msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); + msg->l2h = msgb_put(msg, length); + memcpy(msg->l2h, + dl->tx_hist[sub_mod8(dl->V_send, 1)], + length); + msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, + sub_mod8(dl->V_send, 1), 1); /* P=1 */ + tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, + dl->mctx.link_id, dl->mctx.n201); + } else { + /* OR send appropriate supervision frame with P=1 */ + if (!dl->own_busy && !dl->seq_err_cond) { + lapdm_send_rr(&dl->mctx, 1); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + lapdm_send_rnr(&dl->mctx, 1); + } else { + LOGP(DLLAPDM, LOGL_INFO, "unhandled, " + "pls. fix\n"); + } + } + /* restart T200 (PH-READY-TO-SEND) */ + osmo_timer_schedule(&dl->t200, T200); + } else { + /* send MDL ERROR INIDCATION to L3 */ + rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); + } + break; + default: + LOGP(DLLAPDM, LOGL_INFO, "T200 expired in unexpected " + "dl->state %u\n", dl->state); + } +} + +/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */ +static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl); + int s = 0, rej = 0, t200_reset = 0; + int i; + + /* supervisory frame ? */ + if (LAPDm_CTRL_is_S(mctx->ctrl)) + s = 1; + /* REJ frame ? */ + if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ) + rej = 1; + + /* Flush all transmit buffers of acknowledged frames */ + for (i = dl->V_ack; i != nr; i = inc_mod8(i)) { + if (dl->tx_length[i]) { + dl->tx_length[i] = 0; + LOGP(DLLAPDM, LOGL_INFO, "ack frame %d\n", i); + } + } + + if (dl->state != LAPDm_STATE_TIMER_RECOV) { + /* When not in the timer recovery condition, the data + * link layer entity shall reset the timer T200 on + * receipt of a valid I frame with N(R) higher than V(A), + * or an REJ with an N(R) equal to V(A). */ + if ((!rej && nr != dl->V_ack) + || (rej && nr == dl->V_ack)) { + LOGP(DLLAPDM, LOGL_INFO, "reset t200\n"); + t200_reset = 1; + osmo_timer_del(&dl->t200); + /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */ + } + /* 5.7.4: N(R) sequence error + * N(R) is called valid, if and only if + * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. + */ + if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) { + LOGP(DLLAPDM, LOGL_NOTICE, "N(R) sequence error\n"); + rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx); + } + } + + /* V(A) shall be set to the value of N(R) */ + dl->V_ack = nr; + + /* If T200 has been reset by the receipt of an I, RR or RNR frame, + * and if there are outstanding I frames, restart T200 */ + if (t200_reset && !rej) { + if (dl->tx_length[dl->V_send - 1]) { + LOGP(DLLAPDM, LOGL_INFO, "start T200, due to unacked I " + "frame(s)\n"); + osmo_timer_schedule(&dl->t200, T200); + } + } +} + +/* L1 -> L2 */ + +/* Receive a LAPDm U (Unnumbered) message from L1 */ +static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + struct lapdm_entity *le = dl->entity; + uint8_t length; + int rc; + int rsl_msg; + + switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) { + case LAPDm_U_SABM: + rsl_msg = RSL_MT_EST_IND; + + LOGP(DLLAPDM, LOGL_INFO, "SABM received\n"); + /* 5.7.1 */ + dl->seq_err_cond = 0; + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { + LOGP(DLLAPDM, LOGL_NOTICE, "SABM response error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + + length = msg->l2h[2] >> 2; + /* G.4.5 If SABM is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { + LOGP(DLLAPDM, LOGL_NOTICE, "SABM too large error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); + return -EIO; + } + + /* Must be Format B */ + rc = check_length_ind(mctx, msg->l2h[2]); + if (rc < 0) { + msgb_free(msg); + return rc; + } + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* Set chan_nr and link_id for established connection */ + memset(&dl->mctx, 0, sizeof(dl->mctx)); + dl->mctx.dl = dl; + dl->mctx.chan_nr = mctx->chan_nr; + dl->mctx.link_id = mctx->link_id; + dl->mctx.n201 = mctx->n201; + break; + case LAPDm_STATE_MF_EST: + if (length == 0) { + rsl_msg = RSL_MT_EST_CONF; + break; + } + LOGP(DLLAPDM, LOGL_INFO, "SABM command, multiple " + "frame established state\n"); + /* check for contention resoultion */ + if (dl->tx_hist[0][2] >> 2) { + LOGP(DLLAPDM, LOGL_NOTICE, "SABM not allowed " + "during contention resolution\n"); + rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx); + } + msgb_free(msg); + return 0; + case LAPDm_STATE_DISC_SENT: + /* 5.4.6.2 send DM with F=P */ + lapdm_send_dm(mctx); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + msgb_free(msg); + return send_rll_simple(RSL_MT_REL_CONF, mctx); + default: + lapdm_send_ua(mctx, length, msg->l2h + 3); + msgb_free(msg); + return 0; + } + /* send UA response */ + lapdm_send_ua(mctx, length, msg->l2h + 3); + /* set Vs, Vr and Va to 0 */ + dl->V_send = dl->V_recv = dl->V_ack = 0; + /* clear tx_hist */ + dl->tx_length[0] = 0; + /* enter multiple-frame-established state */ + lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); + /* send notification to L3 */ + if (length == 0) { + /* 5.4.1.2 Normal establishment procedures */ + rc = send_rll_simple(rsl_msg, mctx); + msgb_free(msg); + } else { + /* 5.4.1.4 Contention resolution establishment */ + msg->l3h = msg->l2h + 3; + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(rsl_msg, mctx, msg); + } + break; + case LAPDm_U_DM: + LOGP(DLLAPDM, LOGL_INFO, "DM received\n"); + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { + LOGP(DLLAPDM, LOGL_NOTICE, "DM command error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.4.1.2 DM responses with the F bit set to "0" + * shall be ignored. + */ + msgb_free(msg); + return 0; + } + switch (dl->state) { + case LAPDm_STATE_SABM_SENT: + break; + case LAPDm_STATE_MF_EST: + if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) { + LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " + "response\n"); + rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx); + } else { + LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " + "response, multiple frame established " + "state\n"); + rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); + } + msgb_free(msg); + return 0; + case LAPDm_STATE_TIMER_RECOV: + /* DM is normal in case PF = 1 */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) { + LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " + "response, multiple frame established " + "state\n"); + rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); + msgb_free(msg); + return 0; + } + break; + case LAPDm_STATE_DISC_SENT: + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* go to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + rc = send_rll_simple(RSL_MT_REL_CONF, mctx); + msgb_free(msg); + return 0; + case LAPDm_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + default: + LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM response! " + "(discarding)\n"); + msgb_free(msg); + return 0; + } + /* reset T200 */ + osmo_timer_del(&dl->t200); + rc = send_rll_simple(RSL_MT_REL_IND, mctx); + msgb_free(msg); + break; + case LAPDm_U_UI: + LOGP(DLLAPDM, LOGL_INFO, "UI received\n"); + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { + LOGP(DLLAPDM, LOGL_NOTICE, "UI indicates response " + "error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + + length = msg->l2h[2] >> 2; + /* FIXME: G.4.5 If UI is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + + if (mctx->lapdm_fmt == LAPDm_FMT_B4) { + length = N201_B4; + msg->l3h = msg->l2h + 2; + } else { + rc = check_length_ind(mctx, msg->l2h[2]); + if (rc < 0) { + msgb_free(msg); + return rc; + } + length = msg->l2h[2] >> 2; + msg->l3h = msg->l2h + 3; + } + /* do some length checks */ + if (length == 0) { + /* 5.3.3 UI frames received with the length indicator + * set to "0" shall be ignored + */ + LOGP(DLLAPDM, LOGL_INFO, "length=0 (discarding)\n"); + msgb_free(msg); + return 0; + } + switch (LAPDm_ADDR_SAPI(mctx->addr)) { + case LAPDm_SAPI_NORMAL: + case LAPDm_SAPI_SMS: + break; + default: + /* 5.3.3 UI frames with invalid SAPI values shall be + * discarded + */ + LOGP(DLLAPDM, LOGL_INFO, "sapi=%u (discarding)\n", + LAPDm_ADDR_SAPI(mctx->addr)); + msgb_free(msg); + return 0; + } + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3_ui(mctx, msg); + break; + case LAPDm_U_DISC: + rsl_msg = RSL_MT_REL_IND; + + LOGP(DLLAPDM, LOGL_INFO, "DISC received\n"); + /* flush buffers */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); + /* 5.7.1 */ + dl->seq_err_cond = 0; + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { + LOGP(DLLAPDM, LOGL_NOTICE, "DISC response error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + length = msg->l2h[2] >> 2; + if (length > 0 || msg->l2h[2] & 0x02) { + /* G.4.4 If a DISC or DM frame is received with L>0 or + * with the M bit set to "1", an MDL-ERROR-INDICATION + * primitive with cause "U frame with incorrect + * parameters" is sent to the mobile management entity. + */ + LOGP(DLLAPDM, LOGL_NOTICE, + "U frame iwth incorrect parameters "); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); + return -EIO; + } + switch (dl->state) { + case LAPDm_STATE_IDLE: + LOGP(DLLAPDM, LOGL_INFO, "DISC in idle state\n"); + /* send DM with F=P */ + msgb_free(msg); + return lapdm_send_dm(mctx); + case LAPDm_STATE_SABM_SENT: + LOGP(DLLAPDM, LOGL_INFO, "DISC in SABM state\n"); + /* 5.4.6.2 send DM with F=P */ + lapdm_send_dm(mctx); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + msgb_free(msg); + return send_rll_simple(RSL_MT_REL_IND, mctx); + case LAPDm_STATE_MF_EST: + case LAPDm_STATE_TIMER_RECOV: + LOGP(DLLAPDM, LOGL_INFO, "DISC in est state\n"); + break; + case LAPDm_STATE_DISC_SENT: + LOGP(DLLAPDM, LOGL_INFO, "DISC in disc state\n"); + rsl_msg = RSL_MT_REL_CONF; + break; + default: + lapdm_send_ua(mctx, length, msg->l2h + 3); + msgb_free(msg); + return 0; + } + /* send UA response */ + lapdm_send_ua(mctx, length, msg->l2h + 3); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* enter idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + /* send notification to L3 */ + rc = send_rll_simple(rsl_msg, mctx); + msgb_free(msg); + break; + case LAPDm_U_UA: + LOGP(DLLAPDM, LOGL_INFO, "UA received\n"); + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { + LOGP(DLLAPDM, LOGL_NOTICE, "UA indicates command " + "error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + + length = msg->l2h[2] >> 2; + /* G.4.5 If UA is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { + LOGP(DLLAPDM, LOGL_NOTICE, "UA too large error\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); + return -EIO; + } + + if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.4.1.2 A UA response with the F bit set to "0" + * shall be ignored. + */ + LOGP(DLLAPDM, LOGL_INFO, "F=0 (discarding)\n"); + msgb_free(msg); + return 0; + } + switch (dl->state) { + case LAPDm_STATE_SABM_SENT: + break; + case LAPDm_STATE_MF_EST: + case LAPDm_STATE_TIMER_RECOV: + LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " + "(discarding)\n"); + rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx); + msgb_free(msg); + return 0; + case LAPDm_STATE_DISC_SENT: + LOGP(DLLAPDM, LOGL_INFO, "UA in disconnect state\n"); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* go to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + rc = send_rll_simple(RSL_MT_REL_CONF, mctx); + msgb_free(msg); + return 0; + case LAPDm_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + default: + LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " + "(discarding)\n"); + msgb_free(msg); + return 0; + } + LOGP(DLLAPDM, LOGL_INFO, "UA in SABM state\n"); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* compare UA with SABME if contention resolution is applied */ + if (dl->tx_hist[0][2] >> 2) { + rc = check_length_ind(mctx, msg->l2h[2]); + if (rc < 0) { + rc = send_rll_simple(RSL_MT_REL_IND, mctx); + msgb_free(msg); + /* go to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + return 0; + } + length = msg->l2h[2] >> 2; + if (length != (dl->tx_hist[0][2] >> 2) + || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3, + length)) { + LOGP(DLLAPDM, LOGL_INFO, "**** UA response " + "mismatches ****\n"); + rc = send_rll_simple(RSL_MT_REL_IND, mctx); + msgb_free(msg); + /* go to idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + return 0; + } + } + /* set Vs, Vr and Va to 0 */ + dl->V_send = dl->V_recv = dl->V_ack = 0; + /* clear tx_hist */ + dl->tx_length[0] = 0; + /* enter multiple-frame-established state */ + lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); + /* send outstanding frames, if any (resume / reconnect) */ + rslms_send_i(mctx, __LINE__); + /* send notification to L3 */ + rc = send_rll_simple(RSL_MT_EST_CONF, mctx); + msgb_free(msg); + break; + default: + /* G.3.1 */ + LOGP(DLLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + return rc; +} + +/* Receive a LAPDm S (Supervisory) message from L1 */ +static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + struct lapdm_entity *le = dl->entity; + uint8_t length; + + length = msg->l2h[2] >> 2; + if (length > 0 || msg->l2h[2] & 0x02) { + /* G.4.3 If a supervisory frame is received with L>0 or + * with the M bit set to "1", an MDL-ERROR-INDICATION + * primitive with cause "S frame with incorrect + * parameters" is sent to the mobile management entity. */ + LOGP(DLLAPDM, LOGL_NOTICE, + "S frame with incorrect parameters\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx); + return -EIO; + } + + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp + && LAPDm_CTRL_PF_BIT(mctx->ctrl) + && dl->state != LAPDm_STATE_TIMER_RECOV) { + /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */ + LOGP(DLLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n"); + rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx); + } + + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) + lapdm_send_dm(mctx); /* F=P */ + /* fall though */ + case LAPDm_STATE_SABM_SENT: + case LAPDm_STATE_DISC_SENT: + LOGP(DLLAPDM, LOGL_NOTICE, "S frame ignored in this state\n"); + msgb_free(msg); + return 0; + } + switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) { + case LAPDm_S_RR: + LOGP(DLLAPDM, LOGL_INFO, "RR received\n"); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapdm_acknowledge(mctx); + + /* 5.5.3.2 */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd + && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPDM, LOGL_NOTICE, "RR frame command " + "with polling bit set and we are not " + "busy, so we reply with RR frame\n"); + lapdm_send_rr(mctx, 1); + /* NOTE: In case of sequence error condition, + * the REJ frame has been transmitted when + * entering the condition, so it has not be + * done here + */ + } else if (dl->own_busy) { + LOGP(DLLAPDM, LOGL_NOTICE, "RR frame command " + "with polling bit set and we are busy, " + "so we reply with RR frame\n"); + lapdm_send_rnr(mctx, 1); + } + } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp + && LAPDm_CTRL_PF_BIT(mctx->ctrl) + && dl->state == LAPDm_STATE_TIMER_RECOV) { + LOGP(DLLAPDM, LOGL_INFO, "RR response with F==1, " + "and we are in timer recovery state, so " + "we leave that state\n"); + /* V(S) to the N(R) in the RR frame */ + dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* 5.5.7 Clear timer recovery condition */ + lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); + } + /* Send message, if possible due to acknowledged data */ + rslms_send_i(mctx, __LINE__); + + break; + case LAPDm_S_RNR: + LOGP(DLLAPDM, LOGL_INFO, "RNR received\n"); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapdm_acknowledge(mctx); + + /* 5.5.5 */ + /* Set peer receiver busy condition */ + dl->peer_busy = 1; + + if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { + if (!dl->own_busy) { + LOGP(DLLAPDM, LOGL_INFO, "RNR poll " + "command and we are not busy, " + "so we reply with RR final " + "response\n"); + /* Send RR with F=1 */ + lapdm_send_rr(mctx, 1); + } else { + LOGP(DLLAPDM, LOGL_INFO, "RNR poll " + "command and we are busy, so " + "we reply with RNR final " + "response\n"); + /* Send RNR with F=1 */ + lapdm_send_rnr(mctx, 1); + } + } else if (dl->state == LAPDm_STATE_TIMER_RECOV) { + LOGP(DLLAPDM, LOGL_INFO, "RNR poll response " + "and we in timer recovery state, so " + "we leave that state\n"); + /* 5.5.7 Clear timer recovery condition */ + lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); + /* V(S) to the N(R) in the RNR frame */ + dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl); + } + } else + LOGP(DLLAPDM, LOGL_INFO, "RNR not polling/final state " + "received\n"); + + /* Send message, if possible due to acknowledged data */ + rslms_send_i(mctx, __LINE__); + + break; + case LAPDm_S_REJ: + LOGP(DLLAPDM, LOGL_INFO, "REJ received\n"); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapdm_acknowledge(mctx); + + /* 5.5.4.1 */ + if (dl->state != LAPDm_STATE_TIMER_RECOV) { + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* 5.5.3.2 */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd + && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPDM, LOGL_INFO, "REJ poll " + "command not in timer recovery " + "state and not in own busy " + "condition received, so we " + "respond with RR final " + "response\n"); + lapdm_send_rr(mctx, 1); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + LOGP(DLLAPDM, LOGL_INFO, "REJ poll " + "command not in timer recovery " + "state and in own busy " + "condition received, so we " + "respond with RNR final " + "response\n"); + lapdm_send_rnr(mctx, 1); + } + } else + LOGP(DLLAPDM, LOGL_INFO, "REJ response or not " + "polling command not in timer recovery " + "state received\n"); + /* send MDL ERROR INIDCATION to L3 */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp + && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx); + } + + } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp + && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + LOGP(DLLAPDM, LOGL_INFO, "REJ poll response in timer " + "recovery state received\n"); + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* 5.5.7 Clear timer recovery condition */ + lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + } else { + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); + /* 5.5.3.2 */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd + && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPDM, LOGL_INFO, "REJ poll " + "command in timer recovery " + "state and not in own busy " + "condition received, so we " + "respond with RR final " + "response\n"); + lapdm_send_rr(mctx, 1); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + LOGP(DLLAPDM, LOGL_INFO, "REJ poll " + "command in timer recovery " + "state and in own busy " + "condition received, so we " + "respond with RNR final " + "response\n"); + lapdm_send_rnr(mctx, 1); + } + } else + LOGP(DLLAPDM, LOGL_INFO, "REJ response or not " + "polling command in timer recovery " + "state received\n"); + } + + /* FIXME: 5.5.4.2 2) */ + + /* Send message, if possible due to acknowledged data */ + rslms_send_i(mctx, __LINE__); + + break; + default: + /* G.3.1 */ + LOGP(DLLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + msgb_free(msg); + return 0; +} + +/* Receive a LAPDm I (Information) message from L1 */ +static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + struct lapdm_entity *le = dl->entity; + //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl); + uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl); + uint8_t length; + int rc; + + LOGP(DLLAPDM, LOGL_NOTICE, "I received\n"); + + /* G.2.2 Wrong value of the C/R bit */ + if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { + LOGP(DLLAPDM, LOGL_NOTICE, "I frame response not allowed\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + + length = msg->l2h[2] >> 2; + if (length == 0 || length + 3 > mctx->n201) { + /* G.4.2 If the length indicator of an I frame is set + * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION + * primitive with cause "I frame with incorrect length" + * is sent to the mobile management entity. */ + LOGP(DLLAPDM, LOGL_NOTICE, "I frame length not allowed\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx); + return -EIO; + } + + /* G.4.2 If the numerical value of L is Ll2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) { + LOGP(DLLAPDM, LOGL_NOTICE, "I frame with M bit too short\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx); + return -EIO; + } + + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) + lapdm_send_dm(mctx); /* F=P */ + /* fall though */ + case LAPDm_STATE_SABM_SENT: + case LAPDm_STATE_DISC_SENT: + LOGP(DLLAPDM, LOGL_NOTICE, "I frame ignored in this state\n"); + msgb_free(msg); + return 0; + } + + /* 5.7.1: N(s) sequence error */ + if (ns != dl->V_recv) { + LOGP(DLLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, " + "V(R)=%u\n", ns, dl->V_recv); + /* discard data */ + msgb_free(msg); + if (!dl->seq_err_cond) { + /* FIXME: help me understand what exactly todo here + dl->seq_err_cond = 1; + */ + lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl)); + } else { + } + return -EIO; + } + dl->seq_err_cond = 0; + + /* Increment receiver state */ + dl->V_recv = inc_mod8(dl->V_recv); + LOGP(DLLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv); + + /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */ + lapdm_acknowledge(mctx); /* V(A) is also set here */ + + /* Only if we are not in own receiver busy condition */ + if (!dl->own_busy) { + /* if the frame carries a complete segment */ + if (!(msg->l2h[2] & LAPDm_MORE) + && !dl->rcv_buffer) { + LOGP(DLLAPDM, LOGL_INFO, "message in single I frame\n"); + /* send a DATA INDICATION to L3 */ + msg->l3h = msg->l2h + 3; + msgb_pull_l2h(msg); + msg->len = length; + msg->tail = msg->data + length; + rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg); + } else { + /* create rcv_buffer */ + if (!dl->rcv_buffer) { + LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " + "frames (first message)\n"); + dl->rcv_buffer = msgb_alloc_headroom(200+56, 56, + "LAPDm RX"); + dl->rcv_buffer->l3h = dl->rcv_buffer->data; + } + /* concat. rcv_buffer */ + if (msgb_l3len(dl->rcv_buffer) + length > 200) { + LOGP(DLLAPDM, LOGL_NOTICE, "Received frame " + "overflow!\n"); + } else { + memcpy(msgb_put(dl->rcv_buffer, length), + msg->l2h + 3, length); + } + /* if the last segment was received */ + if (!(msg->l2h[2] & LAPDm_MORE)) { + LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " + "frames (last message)\n"); + rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, + dl->rcv_buffer); + dl->rcv_buffer = NULL; + } else + LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " + "frames (next message)\n"); + msgb_free(msg); + + } + } else + LOGP(DLLAPDM, LOGL_INFO, "I frame ignored during own receiver " + "busy condition\n"); + + /* Check for P bit */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.5.2.1 */ + /* check if we are not in own receiver busy */ + if (!dl->own_busy) { + LOGP(DLLAPDM, LOGL_INFO, "we are not busy, send RR\n"); + /* Send RR with F=1 */ + rc = lapdm_send_rr(mctx, 1); + } else { + LOGP(DLLAPDM, LOGL_INFO, "we are busy, send RNR\n"); + /* Send RNR with F=1 */ + rc = lapdm_send_rnr(mctx, 1); + } + } else { + /* 5.5.2.2 */ + /* check if we are not in own receiver busy */ + if (!dl->own_busy) { + /* NOTE: V(R) is already set above */ + rc = rslms_send_i(mctx, __LINE__); + if (rc) { + LOGP(DLLAPDM, LOGL_INFO, "we are not busy and " + "have no pending data, send RR\n"); + /* Send RR with F=0 */ + return lapdm_send_rr(mctx, 0); + } + /* all I or one RR is sent, we are done */ + return 0; + } else { + LOGP(DLLAPDM, LOGL_INFO, "we are busy, send RNR\n"); + /* Send RNR with F=0 */ + rc = lapdm_send_rnr(mctx, 0); + } + } + + /* Send message, if possible due to acknowledged data */ + rslms_send_i(mctx, __LINE__); + + return rc; +} + +/* Receive a LAPDm message from L1 */ +static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + int rc; + + /* G.2.3 EA bit set to "0" is not allowed in GSM */ + if (!LAPDm_ADDR_EA(mctx->addr)) { + LOGP(DLLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + return -EINVAL; + } + + if (LAPDm_CTRL_is_U(mctx->ctrl)) + rc = lapdm_rx_u(msg, mctx); + else if (LAPDm_CTRL_is_S(mctx->ctrl)) + rc = lapdm_rx_s(msg, mctx); + else if (LAPDm_CTRL_is_I(mctx->ctrl)) + rc = lapdm_rx_i(msg, mctx); + else { + LOGP(DLLAPDM, LOGL_NOTICE, "unknown LAPDm format\n"); + msgb_free(msg); + rc = -EINVAL; + } + return rc; +} + +/* input into layer2 (from layer 1) */ +static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id) +{ + uint8_t cbits = chan_nr >> 3; + uint8_t sapi = link_id & 7; + struct lapdm_msg_ctx mctx; + int rc = 0; + + /* when we reach here, we have a msgb with l2h pointing to the raw + * 23byte mac block. The l1h has already been purged. */ + + mctx.dl = datalink_for_sapi(le, sapi); + mctx.chan_nr = chan_nr; + mctx.link_id = link_id; + mctx.addr = mctx.ctrl = 0; + + /* G.2.1 No action schall be taken on frames containing an unallocated + * SAPI. + */ + if (!mctx.dl) { + LOGP(DLLAPDM, LOGL_NOTICE, "Received frame for unsupported " + "SAPI %d!\n", sapi); + return -EINVAL; + msgb_free(msg); + return -EIO; + } + + /* check for L1 chan_nr/link_id and determine LAPDm hdr format */ + if (cbits == 0x10 || cbits == 0x12) { + /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */ + mctx.lapdm_fmt = LAPDm_FMT_Bbis; + mctx.n201 = N201_Bbis; + } else { + if (mctx.link_id & 0x40) { + /* It was received from network on SACCH, thus + * lapdm_fmt must be B4 */ + mctx.lapdm_fmt = LAPDm_FMT_B4; + mctx.n201 = N201_B4; + LOGP(DLLAPDM, LOGL_INFO, "fmt=B4\n"); + /* SACCH frames have a two-byte L1 header that + * OsmocomBB L1 doesn't strip */ + mctx.tx_power_ind = msg->l2h[0] & 0x1f; + mctx.ta_ind = msg->l2h[1]; + msgb_pull(msg, 2); + msg->l2h += 2; + } else { + mctx.lapdm_fmt = LAPDm_FMT_B; + LOGP(DLLAPDM, LOGL_INFO, "fmt=B\n"); + mctx.n201 = 23; // FIXME: select correct size by chan. + } + } + + switch (mctx.lapdm_fmt) { + case LAPDm_FMT_A: + case LAPDm_FMT_B: + case LAPDm_FMT_B4: + mctx.addr = msg->l2h[0]; + if (!(mctx.addr & 0x01)) { + LOGP(DLLAPDM, LOGL_ERROR, "we don't support " + "multibyte addresses (discarding)\n"); + msgb_free(msg); + return -EINVAL; + } + mctx.ctrl = msg->l2h[1]; + /* obtain SAPI from address field */ + mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr); + rc = lapdm_ph_data_ind(msg, &mctx); + break; + case LAPDm_FMT_Bter: + /* FIXME */ + msgb_free(msg); + break; + case LAPDm_FMT_Bbis: + /* directly pass up to layer3 */ + LOGP(DLLAPDM, LOGL_INFO, "fmt=Bbis UI\n"); + msg->l3h = msg->l2h; + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg); + break; + default: + msgb_free(msg); + } + + return rc; +} + +/* input into layer2 (from layer 1) */ +static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay) +{ + struct abis_rsl_cchan_hdr *ch; + struct gsm48_req_ref req_ref; + struct gsm_time gt; + struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD"); + + msg->l2h = msgb_push(msg, sizeof(*ch)); + ch = (struct abis_rsl_cchan_hdr *)msg->l2h; + rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD); + ch->chan_nr = RSL_CHAN_RACH; + + /* generate a RSL CHANNEL REQUIRED message */ + gsm_fn2gsmtime(>, fn); + req_ref.ra = ra; + req_ref.t1 = gt.t1; /* FIXME: modulo? */ + req_ref.t2 = gt.t2; + req_ref.t3_low = gt.t3 & 7; + req_ref.t3_high = gt.t3 >> 3; + + msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref); + msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay); + + return rslms_sendmsg(msg, le); +} + +static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr); + +int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) +{ + struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph; + int rc = 0; + + if (oph->sap != SAP_GSM_PH) { + LOGP(DLLAPDM, LOGL_ERROR, "primitive for unknown SAP %u\n", + oph->sap); + return -ENODEV; + } + + switch (oph->primitive) { + case PRIM_PH_DATA: + if (oph->operation != PRIM_OP_INDICATION) { + LOGP(DLLAPDM, LOGL_ERROR, "PH_DATA is not INDICATION %u\n", + oph->operation); + return -ENODEV; + } + rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr, + pp->u.data.link_id); + break; + case PRIM_PH_RTS: + if (oph->operation != PRIM_OP_INDICATION) { + LOGP(DLLAPDM, LOGL_ERROR, "PH_RTS is not INDICATION %u\n", + oph->operation); + return -ENODEV; + } + rc = l2_ph_data_conf(oph->msg, le); + break; + case PRIM_PH_RACH: + switch (oph->operation) { + case PRIM_OP_INDICATION: + rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn, + pp->u.rach_ind.acc_delay); + break; + case PRIM_OP_CONFIRM: + rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn); + break; + default: + return -EIO; + } + break; + } + + return rc; +} + + +/* L3 -> L2 / RSLMS -> LAPDm */ + +/* L3 requests establishment of data link */ +static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct lapdm_entity *le = dl->entity; + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = rllh->link_id & 7; + struct tlv_parsed tv; + uint8_t length; + uint8_t n201 = 23; //FIXME + + /* Set chan_nr and link_id for established connection */ + memset(&dl->mctx, 0, sizeof(dl->mctx)); + dl->mctx.dl = dl; + dl->mctx.n201 = n201; + dl->mctx.chan_nr = chan_nr; + dl->mctx.link_id = link_id; + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); + /* contention resolution establishment procedure */ + if (sapi != 0) { + /* According to clause 6, the contention resolution + * procedure is only permitted with SAPI value 0 */ + LOGP(DLLAPDM, LOGL_ERROR, "SAPI != 0 but contention" + "resolution (discarding)\n"); + msgb_free(msg); + return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); + } + /* transmit a SABM command with the P bit set to "1". The SABM + * command shall contain the layer 3 message unit */ + length = TLVP_LEN(&tv, RSL_IE_L3_INFO); + LOGP(DLLAPDM, LOGL_INFO, "perform establishment with content " + "(SABM)\n"); + } else { + /* normal establishment procedure */ + length = 0; + LOGP(DLLAPDM, LOGL_INFO, "perform normal establishm. (SABM)\n"); + } + + /* check if the layer3 message length exceeds N201 */ + if (length + 3 > 21) { /* FIXME: do we know the channel N201? */ + LOGP(DLLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) " + "(discarding)\n", length + 3, 21); + msgb_free(msg); + return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); + } + + /* Flush send-queue */ + /* Clear send-buffer */ + lapdm_dl_flush_send(dl); + + /* Discard partly received L3 message */ + if (dl->rcv_buffer) { + msgb_free(dl->rcv_buffer); + dl->rcv_buffer = NULL; + } + + /* Remove RLL header from msgb */ + msgb_pull_l2h(msg); + + /* Push LAPDm header on msgb */ + msg->l2h = msgb_push(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); + msg->l2h[2] = LAPDm_LEN(length); + /* Transmit-buffer carries exactly one segment */ + memcpy(dl->tx_hist[0], msg->l2h, 3 + length); + dl->tx_length[0] = 3 + length; + /* set Vs to 0, because it is used as index when resending SABM */ + dl->V_send = 0; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT); + + /* Tramsmit and start T200 */ + osmo_timer_schedule(&dl->t200, T200); + return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); +} + +/* L3 requests transfer of unnumbered information */ +static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct lapdm_entity *le = dl->entity; + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = link_id & 7; + struct tlv_parsed tv; + int length; + uint8_t n201 = 23; //FIXME + uint8_t ta = 0, tx_power = 0; + + /* check if the layer3 message length exceeds N201 */ + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + + if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) { + ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE); + } + if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) { + tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER); + } + if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + LOGP(DLLAPDM, LOGL_ERROR, "unit data request without message " + "error\n"); + msgb_free(msg); + return -EINVAL; + } + msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); + length = TLVP_LEN(&tv, RSL_IE_L3_INFO); + /* check if the layer3 message length exceeds N201 */ + if (length + 5 > 23) { /* FIXME: do we know the channel N201? */ + LOGP(DLLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) " + "(discarding)\n", length + 5, 23); + msgb_free(msg); + return -EIO; + } + + LOGP(DLLAPDM, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n", + tx_power, ta); + + /* Remove RLL header from msgb */ + msgb_pull_l2h(msg); + + /* Push L1 + LAPDm header on msgb */ + msg->l2h = msgb_push(msg, 2 + 3); + msg->l2h[0] = tx_power; + msg->l2h[1] = ta; + msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0); + msg->l2h[4] = LAPDm_LEN(length); + // FIXME: short L2 header support + + /* Tramsmit */ + return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); +} + +/* L3 requests transfer of acknowledged information */ +static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + struct tlv_parsed tv; + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + LOGP(DLLAPDM, LOGL_ERROR, "data request without message " + "error\n"); + msgb_free(msg); + return -EINVAL; + } + msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); + + LOGP(DLLAPDM, LOGL_INFO, "writing message to send-queue\n"); + + /* Remove the RSL/RLL header */ + msgb_pull_l2h(msg); + + /* Write data into the send queue */ + msgb_enqueue(&dl->send_queue, msg); + + /* Send message, if possible */ + rslms_send_i(&dl->mctx, __LINE__); + return 0; +} + +/* Send next I frame from queued/buffered data */ +static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line) +{ + struct lapdm_datalink *dl = mctx->dl; + struct lapdm_entity *le = dl->entity; + uint8_t chan_nr = mctx->chan_nr; + uint8_t link_id = mctx->link_id; + uint8_t sapi = link_id & 7; + int k = k_sapi[sapi]; + struct msgb *msg; + int length, left; + int rc = - 1; /* we sent nothing */ + + LOGP(DLLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line); + + next_frame: + + if (dl->peer_busy) { + LOGP(DLLAPDM, LOGL_INFO, "peer busy, not sending\n"); + return rc; + } + + if (dl->state == LAPDm_STATE_TIMER_RECOV) { + LOGP(DLLAPDM, LOGL_INFO, "timer recovery, not sending\n"); + return rc; + } + + /* If the send state variable V(S) is equal to V(A) plus k + * (where k is the maximum number of outstanding I frames - see + * subclause 5.8.4), the data link layer entity shall not transmit any + * new I frames, but shall retransmit an I frame as a result + * of the error recovery procedures as described in subclauses 5.5.4 and + * 5.5.7. */ + if (dl->V_send == add_mod8(dl->V_ack, k)) { + LOGP(DLLAPDM, LOGL_INFO, "k frames outstanding, not sending " + "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send, + dl->V_ack); + return rc; + } + + /* if we have no tx_hist yet, we create it */ + if (!dl->tx_length[dl->V_send]) { + /* Get next message into send-buffer, if any */ + if (!dl->send_buffer) { + next_message: + dl->send_out = 0; + dl->send_buffer = msgb_dequeue(&dl->send_queue); + /* No more data to be sent */ + if (!dl->send_buffer) + return rc; + LOGP(DLLAPDM, LOGL_INFO, "get message from " + "send-queue\n"); + } + + /* How much is left in the send-buffer? */ + left = msgb_l3len(dl->send_buffer) - dl->send_out; + /* Segment, if data exceeds N201 */ + length = left; + if (length > mctx->n201 - 3) + length = mctx->n201 - 3; + LOGP(DLLAPDM, LOGL_INFO, "msg-len %d sent %d left %d N201 %d " + "length %d first byte %02x\n", + msgb_l3len(dl->send_buffer), dl->send_out, left, + mctx->n201, length, dl->send_buffer->l3h[0]); + /* If message in send-buffer is completely sent */ + if (left == 0) { + msgb_free(dl->send_buffer); + dl->send_buffer = NULL; + goto next_message; + } + + LOGP(DLLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n", + (left > length) ? "segment " : "", dl->V_send); + + /* Create I frame (segment) and transmit-buffer content */ + msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); + msg->l2h = msgb_put(msg, 3 + length); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); + msg->l2h[2] = LAPDm_LEN(length); + if (left > length) + msg->l2h[2] |= LAPDm_MORE; + memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out, + length); + memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length); + dl->tx_length[dl->V_send] = 3 + length; + /* Add length to track how much is already in the tx buffer */ + dl->send_out += length; + } else { + LOGP(DLLAPDM, LOGL_INFO, "resend I frame from tx buffer " + "V(S)=%d\n", dl->V_send); + + /* Create I frame (segment) from tx_hist */ + length = dl->tx_length[dl->V_send]; + msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); + msg->l2h = msgb_put(msg, length); + memcpy(msg->l2h, dl->tx_hist[dl->V_send], length); + msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); + } + + /* The value of the send state variable V(S) shall be incremented by 1 + * at the end of the transmission of the I frame */ + dl->V_send = inc_mod8(dl->V_send); + + /* If timer T200 is not running at the time right before transmitting a + * frame, when the PH-READY-TO-SEND primitive is received from the + * physical layer., it shall be set. */ + if (!osmo_timer_pending(&dl->t200)) + osmo_timer_schedule(&dl->t200, T200); + + tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201); + + rc = 0; /* we sent something */ + goto next_frame; +} + +/* L3 requests suspension of data link */ +static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t sapi = rllh->link_id & 7; + + if (sapi != 0) { + LOGP(DLLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n"); + msgb_free(msg); + return -EINVAL; + } + + LOGP(DLLAPDM, LOGL_INFO, "perform suspension\n"); + + /* put back the send-buffer to the send-queue (first position) */ + if (dl->send_buffer) { + LOGP(DLLAPDM, LOGL_INFO, "put frame in sendbuffer back to " + "queue\n"); + llist_add(&dl->send_buffer->list, &dl->send_queue); + dl->send_buffer = NULL; + } else + LOGP(DLLAPDM, LOGL_INFO, "no frame in sendbuffer\n"); + + /* Clear transmit buffer, but keep send buffer */ + lapdm_dl_flush_tx(dl); + + msgb_free(msg); + + return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx); +} + +/* L3 requests resume of data link */ +static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct lapdm_entity *le = dl->entity; + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = rllh->link_id & 7; + struct tlv_parsed tv; + uint8_t length; + uint8_t n201 = 23; //FIXME + + /* Set chan_nr and link_id for established connection */ + memset(&dl->mctx, 0, sizeof(dl->mctx)); + dl->mctx.dl = dl; + dl->mctx.n201 = n201; + dl->mctx.chan_nr = chan_nr; + dl->mctx.link_id = link_id; + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + LOGP(DLLAPDM, LOGL_ERROR, "resume without message error\n"); + msgb_free(msg); + return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); + } + length = TLVP_LEN(&tv, RSL_IE_L3_INFO); + + LOGP(DLLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", + length); + + /* Replace message in the send-buffer (reconnect) */ + if (dl->send_buffer) + msgb_free(dl->send_buffer); + dl->send_out = 0; + if (length) { + /* Remove the RSL/RLL header */ + msgb_pull_l2h(msg); + /* Write data into the send buffer, to be sent first */ + dl->send_buffer = msg; + } + + /* Discard partly received L3 message */ + if (dl->rcv_buffer) { + msgb_free(dl->rcv_buffer); + dl->rcv_buffer = NULL; + } + + /* Create new msgb (old one is now free) */ + msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM"); + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); + msg->l2h[2] = LAPDm_LEN(0); + /* Transmit-buffer carries exactly one segment */ + memcpy(dl->tx_hist[0], msg->l2h, 3); + dl->tx_length[0] = 3; + /* set Vs to 0, because it is used as index when resending SABM */ + dl->V_send = 0; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT); + + /* Tramsmit and start T200 */ + osmo_timer_schedule(&dl->t200, T200); + return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); +} + +/* L3 requests release of data link */ +static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct lapdm_entity *le = dl->entity; + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = rllh->link_id & 7; + uint8_t mode = 0; + + /* get release mode */ + if (rllh->data[0] == RSL_IE_RELEASE_MODE) + mode = rllh->data[1] & 1; + + /* local release */ + if (mode) { + LOGP(DLLAPDM, LOGL_INFO, "perform local release\n"); + msgb_free(msg); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* enter idle state */ + lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); + /* flush buffers */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); + /* send notification to L3 */ + return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); + } + + /* in case we are already disconnecting */ + if (dl->state == LAPDm_STATE_DISC_SENT) + return -EBUSY; + + LOGP(DLLAPDM, LOGL_INFO, "perform normal release (DISC)\n"); + + /* Pull rllh */ + msgb_pull(msg, msg->tail - msg->l2h); + + /* Push LAPDm header on msgb */ + msg->l2h = msgb_push(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1); + msg->l2h[2] = LAPDm_LEN(0); + /* Transmit-buffer carries exactly one segment */ + memcpy(dl->tx_hist[0], msg->l2h, 3); + dl->tx_length[0] = 3; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT); + + /* Tramsmit and start T200 */ + osmo_timer_schedule(&dl->t200, T200); + return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201); +} + +/* L3 requests release in idle state */ +static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl) +{ + msgb_free(msg); + + /* send notification to L3 */ + return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); +} + +/* L3 requests channel in idle state */ +static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg) +{ + struct abis_rsl_cchan_hdr *cch = msgb_l2(msg); + void *l1ctx = lc->lapdm_dcch.l1_ctx; + struct osmo_phsap_prim pp; + + osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH, + PRIM_OP_REQUEST, NULL); + + if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) { + LOGP(DLLAPDM, LOGL_ERROR, "Message too short for CHAN RQD!\n"); + return -EINVAL; + } + if (cch->data[0] != RSL_IE_REQ_REFERENCE) { + LOGP(DLLAPDM, LOGL_ERROR, "Missing REQ REFERENCE IE\n"); + return -EINVAL; + } + pp.u.rach_req.ra = cch->data[1]; + pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3]; + pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7; + + if (cch->data[4] != RSL_IE_ACCESS_DELAY) { + LOGP(DLLAPDM, LOGL_ERROR, "Missing ACCESS_DELAY IE\n"); + return -EINVAL; + } + /* TA = 0 - delay */ + pp.u.rach_req.ta = 0 - cch->data[5]; + + if (cch->data[6] != RSL_IE_MS_POWER) { + LOGP(DLLAPDM, LOGL_ERROR, "Missing MS POWER IE\n"); + return -EINVAL; + } + pp.u.rach_req.tx_power = cch->data[7]; + + msgb_free(msg); + + return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx); +} + +/* L1 confirms channel request */ +static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr) +{ + struct abis_rsl_cchan_hdr *ch; + struct gsm_time tm; + struct gsm48_req_ref *ref; + + gsm_fn2gsmtime(&tm, frame_nr); + + msgb_pull_l2h(msg); + msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref)); + ch = (struct abis_rsl_cchan_hdr *)msg->l2h; + rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF); + ch->chan_nr = RSL_CHAN_RACH; + ch->data[0] = RSL_IE_REQ_REFERENCE; + ref = (struct gsm48_req_ref *) (ch->data + 1); + ref->t1 = tm.t1; + ref->t2 = tm.t2; + ref->t3_low = tm.t3 & 0x7; + ref->t3_high = tm.t3 >> 3; + + return rslms_sendmsg(msg, le); +} + +const char *lapdm_state_names[] = { + "LAPDm_STATE_NULL", + "LAPDm_STATE_IDLE", + "LAPDm_STATE_SABM_SENT", + "LAPDm_STATE_MF_EST", + "LAPDm_STATE_TIMER_RECOV", + "LAPDm_STATE_DISC_SENT", +}; + +/* statefull handling for RSLms RLL messages from L3 */ +static struct l2downstate { + uint32_t states; + int type; + int (*rout) (struct msgb *msg, struct lapdm_datalink *dl); +} l2downstatelist[] = { + /* create and send UI command */ + {ALL_STATES, + RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req}, + + /* create and send SABM command */ + {SBIT(LAPDm_STATE_IDLE), + RSL_MT_EST_REQ, rslms_rx_rll_est_req}, + + /* create and send I command */ + {SBIT(LAPDm_STATE_MF_EST) | + SBIT(LAPDm_STATE_TIMER_RECOV), + RSL_MT_DATA_REQ, rslms_rx_rll_data_req}, + + /* suspend datalink */ + {SBIT(LAPDm_STATE_MF_EST) | + SBIT(LAPDm_STATE_TIMER_RECOV), + RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req}, + + /* create and send SABM command (resume) */ + {SBIT(LAPDm_STATE_MF_EST) | + SBIT(LAPDm_STATE_TIMER_RECOV), + RSL_MT_RES_REQ, rslms_rx_rll_res_req}, + + /* create and send SABM command (reconnect) */ + {SBIT(LAPDm_STATE_IDLE) | + SBIT(LAPDm_STATE_MF_EST) | + SBIT(LAPDm_STATE_TIMER_RECOV), + RSL_MT_RECON_REQ, rslms_rx_rll_res_req}, + + /* create and send DISC command */ + {SBIT(LAPDm_STATE_SABM_SENT) | + SBIT(LAPDm_STATE_MF_EST) | + SBIT(LAPDm_STATE_TIMER_RECOV) | + SBIT(LAPDm_STATE_DISC_SENT), + RSL_MT_REL_REQ, rslms_rx_rll_rel_req}, + + /* release in idle state */ + {SBIT(LAPDm_STATE_IDLE), + RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle}, +}; + +#define L2DOWNSLLEN \ + (sizeof(l2downstatelist) / sizeof(struct l2downstate)) + +/* incoming RSLms RLL message from L3 */ +static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + int msg_type = rllh->c.msg_type; + uint8_t sapi = rllh->link_id & 7; + struct lapdm_entity *le; + struct lapdm_datalink *dl; + int i, supported = 0; + int rc = 0; + + if (msgb_l2len(msg) < sizeof(*rllh)) { + LOGP(DLLAPDM, LOGL_ERROR, "Message too short for RLL hdr!\n"); + return -EINVAL; + } + + if (rllh->link_id & 0x40) + le = &lc->lapdm_acch; + else + le = &lc->lapdm_dcch; + + /* G.2.1 No action schall be taken on frames containing an unallocated + * SAPI. + */ + dl = datalink_for_sapi(le, sapi); + if (!dl) { + LOGP(DLLAPDM, LOGL_ERROR, "No instance for SAPI %d!\n", sapi); + return -EINVAL; + } + + LOGP(DLLAPDM, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n", + lc->name, rsl_msg_name(msg_type), lapdm_state_names[dl->state]); + + /* find function for current state and message */ + for (i = 0; i < L2DOWNSLLEN; i++) { + if (msg_type == l2downstatelist[i].type) + supported = 1; + if ((msg_type == l2downstatelist[i].type) + && ((1 << dl->state) & l2downstatelist[i].states)) + break; + } + if (!supported) { + LOGP(DLLAPDM, LOGL_NOTICE, "Message unsupported.\n"); + msgb_free(msg); + return 0; + } + if (i == L2DOWNSLLEN) { + LOGP(DLLAPDM, LOGL_NOTICE, "Message unhandled at this state.\n"); + msgb_free(msg); + return 0; + } + + rc = l2downstatelist[i].rout(msg, dl); + + return rc; +} + +/* incoming RSLms COMMON CHANNEL message from L3 */ +static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc) +{ + struct abis_rsl_cchan_hdr *cch = msgb_l2(msg); + int msg_type = cch->c.msg_type; + int rc = 0; + + if (msgb_l2len(msg) < sizeof(*cch)) { + LOGP(DLLAPDM, LOGL_ERROR, "Message too short for COM CHAN hdr!\n"); + return -EINVAL; + } + + switch (msg_type) { + case RSL_MT_CHAN_RQD: + /* create and send RACH request */ + rc = rslms_rx_chan_rqd(lc, msg); + break; + default: + LOGP(DLLAPDM, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n", + msg_type); + msgb_free(msg); + return 0; + } + + return rc; +} + +/* input into layer2 (from layer 3) */ +int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc) +{ + struct abis_rsl_common_hdr *rslh = msgb_l2(msg); + int rc = 0; + + if (msgb_l2len(msg) < sizeof(*rslh)) { + LOGP(DLLAPDM, LOGL_ERROR, "Message too short RSL hdr!\n"); + return -EINVAL; + } + + switch (rslh->msg_discr & 0xfe) { + case ABIS_RSL_MDISC_RLL: + rc = rslms_rx_rll(msg, lc); + break; + case ABIS_RSL_MDISC_COM_CHAN: + rc = rslms_rx_com_chan(msg, lc); + break; + default: + LOGP(DLLAPDM, LOGL_ERROR, "unknown RSLms message " + "discriminator 0x%02x", rslh->msg_discr); + msgb_free(msg); + return -EINVAL; + } + + return rc; +} + +int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode) +{ + switch (mode) { + case LAPDM_MODE_MS: + le->cr.loc2rem.cmd = CR_MS2BS_CMD; + le->cr.loc2rem.resp = CR_MS2BS_RESP; + le->cr.rem2loc.cmd = CR_BS2MS_CMD; + le->cr.rem2loc.resp = CR_BS2MS_RESP; + break; + case LAPDM_MODE_BTS: + le->cr.loc2rem.cmd = CR_BS2MS_CMD; + le->cr.loc2rem.resp = CR_BS2MS_RESP; + le->cr.rem2loc.cmd = CR_MS2BS_CMD; + le->cr.rem2loc.resp = CR_MS2BS_RESP; + break; + default: + return -EINVAL; + } + + le->mode = mode; + + return 0; +} + +int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode) +{ + int rc; + + rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode); + if (rc < 0) + return rc; + + return lapdm_entity_set_mode(&lc->lapdm_acch, mode); +} + +void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx) +{ + lc->lapdm_dcch.l1_prim_cb = cb; + lc->lapdm_acch.l1_prim_cb = cb; + lc->lapdm_dcch.l1_ctx = ctx; + lc->lapdm_acch.l1_ctx = ctx; +} + +void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx) +{ + lc->lapdm_dcch.l3_cb = cb; + lc->lapdm_acch.l3_cb = cb; + lc->lapdm_dcch.l3_ctx = ctx; + lc->lapdm_acch.l3_ctx = ctx; +} + +void lapdm_entity_reset(struct lapdm_entity *le) +{ + struct lapdm_datalink *dl; + int i; + + for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { + dl = &le->datalink[i]; + if (dl->state == LAPDm_STATE_IDLE) + continue; + LOGP(DLLAPDM, LOGL_INFO, "Resetting LAPDm instance\n"); + /* reset Timer T200 */ + osmo_timer_del(&dl->t200); + /* enter idle state */ + dl->state = LAPDm_STATE_IDLE; + /* flush buffer */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); + } +} + +void lapdm_channel_reset(struct lapdm_channel *lc) +{ + lapdm_entity_reset(&lc->lapdm_dcch); + lapdm_entity_reset(&lc->lapdm_acch); +} + +void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags) +{ + le->flags = flags; +} + +void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags) +{ + lapdm_entity_set_flags(&lc->lapdm_dcch, flags); + lapdm_entity_set_flags(&lc->lapdm_acch, flags); +} diff --git a/src/logging.c b/src/logging.c index def13b3b..8fd2e005 100644 --- a/src/logging.c +++ b/src/logging.c @@ -66,6 +66,12 @@ static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = { .loglevel = LOGL_NOTICE, .enabled = 1, }, + [INT2IDX(DLLAPDM)] = { /* -2 becomes 1 */ + .name = "DLLAPDM", + .description = "LAPDm in libosmogsm", + .loglevel = LOGL_NOTICE, + .enabled = 1, + }, }; /* You have to keep this in sync with the structure loglevel_strs. */ -- cgit v1.2.3 From 9fe1652212a84815e02d37f58d96192a1e6194d2 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 27 Jun 2011 14:00:03 +0200 Subject: logging: make sure to add the internal categories, as intended --- src/logging.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/logging.c b/src/logging.c index 8fd2e005..c05b523d 100644 --- a/src/logging.c +++ b/src/logging.c @@ -637,4 +637,10 @@ int log_init(const struct log_info *inf, void *ctx) } /* copy over the library part */ + for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { + memcpy(&osmo_log_info->cat[inf->num_cat+i], + &internal_cat[i], sizeof(struct log_info_cat)); + } + + return 0; } -- cgit v1.2.3 From ce9fec3e896571835ac5bfd2980d6836f2b29f0d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 27 Jun 2011 14:19:16 +0200 Subject: Fix the generation of the log_categories string + LAPDM ... I should do more testing :( Conflicts: src/logging.c --- src/logging.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/logging.c b/src/logging.c index c05b523d..730fc37c 100644 --- a/src/logging.c +++ b/src/logging.c @@ -475,8 +475,9 @@ int log_target_file_reopen(struct log_target *target) } /* This generates the logging command string for VTY. */ -const char *log_vty_command_string(const struct log_info *info) +const char *log_vty_command_string(const struct log_info *unused_info) { + struct log_info *info = osmo_log_info; int len = 0, offset = 0, ret, i, rem; int size = strlen("logging level () ()") + 1; char *str; @@ -549,8 +550,9 @@ err: } /* This generates the logging command description for VTY. */ -const char *log_vty_command_description(const struct log_info *info) +const char *log_vty_command_description(const struct log_info *unused_info) { + struct log_info *info = osmo_log_info; char *str; int i, ret, len = 0, offset = 0, rem; unsigned int size = @@ -619,7 +621,7 @@ int log_init(const struct log_info *inf, void *ctx) osmo_log_info->num_cat_user = inf->num_cat; /* total number = number of user cat + library cat */ - osmo_log_info->num_cat = inf->num_cat + OSMO_NUM_DLIB; + osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat); osmo_log_info->cat = talloc_zero_array(osmo_log_info, struct log_info_cat, @@ -638,7 +640,8 @@ int log_init(const struct log_info *inf, void *ctx) /* copy over the library part */ for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { - memcpy(&osmo_log_info->cat[inf->num_cat+i], + unsigned int cn = osmo_log_info->num_cat_user + i; + memcpy(&osmo_log_info->cat[cn], &internal_cat[i], sizeof(struct log_info_cat)); } -- cgit v1.2.3 From 6420774b3b2e1658fa7f3d788e81d2305d6f10c3 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 27 Jun 2011 23:32:14 +0200 Subject: LAPDm: When Rx DATA from L1, L1 does not know the SAPI We have to determine the SAPI ourselves inside the LAPDm header. --- src/gsm/lapdm.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c index b804a929..242544d3 100644 --- a/src/gsm/lapdm.c +++ b/src/gsm/lapdm.c @@ -1573,34 +1573,23 @@ static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx) static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id) { uint8_t cbits = chan_nr >> 3; - uint8_t sapi = link_id & 7; + uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */ struct lapdm_msg_ctx mctx; int rc = 0; /* when we reach here, we have a msgb with l2h pointing to the raw * 23byte mac block. The l1h has already been purged. */ - mctx.dl = datalink_for_sapi(le, sapi); mctx.chan_nr = chan_nr; mctx.link_id = link_id; mctx.addr = mctx.ctrl = 0; - /* G.2.1 No action schall be taken on frames containing an unallocated - * SAPI. - */ - if (!mctx.dl) { - LOGP(DLLAPDM, LOGL_NOTICE, "Received frame for unsupported " - "SAPI %d!\n", sapi); - return -EINVAL; - msgb_free(msg); - return -EIO; - } - /* check for L1 chan_nr/link_id and determine LAPDm hdr format */ if (cbits == 0x10 || cbits == 0x12) { /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */ mctx.lapdm_fmt = LAPDm_FMT_Bbis; mctx.n201 = N201_Bbis; + sapi = 0; } else { if (mctx.link_id & 0x40) { /* It was received from network on SACCH, thus @@ -1614,13 +1603,25 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha mctx.ta_ind = msg->l2h[1]; msgb_pull(msg, 2); msg->l2h += 2; + sapi = (msg->l2h[0] >> 2) & 7; } else { mctx.lapdm_fmt = LAPDm_FMT_B; LOGP(DLLAPDM, LOGL_INFO, "fmt=B\n"); mctx.n201 = 23; // FIXME: select correct size by chan. + sapi = (msg->l2h[0] >> 2) & 7; } } + mctx.dl = datalink_for_sapi(le, sapi); + /* G.2.1 No action on frames containing an unallocated SAPI. */ + if (!mctx.dl) { + LOGP(DLLAPDM, LOGL_NOTICE, "Received frame for unsupported " + "SAPI %d!\n", sapi); + return -EINVAL; + msgb_free(msg); + return -EIO; + } + switch (mctx.lapdm_fmt) { case LAPDm_FMT_A: case LAPDm_FMT_B: -- cgit v1.2.3 From 7ca604bcd3d635143236458dfeba97829d07b321 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 29 Jun 2011 12:13:51 +0200 Subject: LAPDm: Uplink SACCH frames use format B, not format B4 --- src/gsm/lapdm.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c index 242544d3..2679ca06 100644 --- a/src/gsm/lapdm.c +++ b/src/gsm/lapdm.c @@ -1592,11 +1592,18 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha sapi = 0; } else { if (mctx.link_id & 0x40) { - /* It was received from network on SACCH, thus - * lapdm_fmt must be B4 */ - mctx.lapdm_fmt = LAPDm_FMT_B4; - mctx.n201 = N201_B4; - LOGP(DLLAPDM, LOGL_INFO, "fmt=B4\n"); + /* It was received from network on SACCH */ + + /* If sent by BTS, lapdm_fmt must be B4 */ + if (le->mode == LAPDM_MODE_MS) { + mctx.lapdm_fmt = LAPDm_FMT_B4; + mctx.n201 = N201_B4; + LOGP(DLLAPDM, LOGL_INFO, "fmt=B4\n"); + } else { + mctx.lapdm_fmt = LAPDm_FMT_B; + mctx.n201 = N201_AB_SACCH; + LOGP(DLLAPDM, LOGL_INFO, "fmt=B\n"); + } /* SACCH frames have a two-byte L1 header that * OsmocomBB L1 doesn't strip */ mctx.tx_power_ind = msg->l2h[0] & 0x1f; -- cgit v1.2.3 From 7721a77eafcfffbfcc2d8027dd4134bb9a9cc2b9 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 29 Jun 2011 18:44:06 +0200 Subject: make sure abis_nm <-> osmocom pchan type conversion always works --- src/gsm/abis_nm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gsm/abis_nm.c b/src/gsm/abis_nm.c index a82194fd..b54657d6 100644 --- a/src/gsm/abis_nm.c +++ b/src/gsm/abis_nm.c @@ -399,6 +399,7 @@ void abis_nm_debugp_foh(int ss, struct abis_om_fom_hdr *foh) } static const enum abis_nm_chan_comb chcomb4pchan[] = { + [GSM_PCHAN_NONE] = 0xff, [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH, [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCHComb, [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull, @@ -406,6 +407,7 @@ static const enum abis_nm_chan_comb chcomb4pchan[] = { [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH, [GSM_PCHAN_PDCH] = NM_CHANC_IPAC_PDCH, [GSM_PCHAN_TCH_F_PDCH] = NM_CHANC_IPAC_TCHFull_PDCH, + [GSM_PCHAN_UNKNOWN] = 0xff, /* FIXME: bounds check */ }; -- cgit v1.2.3 From 9e1f0604b5d081197d1b2d5adbc8499074e6717e Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 29 Jun 2011 18:46:10 +0200 Subject: add msgb_set_talloc_ctx() to set the talloc context for msgb allocations --- include/osmocom/core/msgb.h | 2 ++ src/msgb.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 8665c2bf..58976f7f 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -193,5 +193,7 @@ static inline struct msgb *msgb_alloc_headroom(int size, int headroom, uint8_t *msgb_data(const struct msgb *msg); uint16_t msgb_length(const struct msgb *msg); +/* set the talloc context for msgb_alloc[_headroom] */ +void msgb_set_talloc_ctx(void *ctx); #endif /* _MSGB_H */ diff --git a/src/msgb.c b/src/msgb.c index d2c167aa..f9841ed1 100644 --- a/src/msgb.c +++ b/src/msgb.c @@ -98,3 +98,8 @@ uint16_t msgb_length(const struct msgb *msg) { return msg->len; } + +void msgb_set_talloc_ctx(void *ctx) +{ + tall_msgb_ctx = ctx; +} -- cgit v1.2.3 From 8264e09ca2f3bd93eba5eefa342267f303085629 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 29 Jun 2011 19:22:47 +0200 Subject: lapdm: make sure we flush all queues whenever entering IDLE state this fixes a memory leak where the final UA would always remain in memory after a LAPDm entity has been disconnected. --- src/gsm/lapdm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c index 2679ca06..e496461e 100644 --- a/src/gsm/lapdm.c +++ b/src/gsm/lapdm.c @@ -594,6 +594,7 @@ static void lapdm_t200_cb(void *data) rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); /* flush tx buffers */ lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); /* go back to idle state */ lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); /* NOTE: we must not change any other states or buffers @@ -893,6 +894,8 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) /* reset Timer T200 */ osmo_timer_del(&dl->t200); /* go to idle state */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); rc = send_rll_simple(RSL_MT_REL_CONF, mctx); msgb_free(msg); @@ -1024,6 +1027,8 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) /* reset Timer T200 */ osmo_timer_del(&dl->t200); /* enter idle state */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); /* send notification to L3 */ rc = send_rll_simple(rsl_msg, mctx); @@ -1074,6 +1079,8 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) /* reset Timer T200 */ osmo_timer_del(&dl->t200); /* go to idle state */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); rc = send_rll_simple(RSL_MT_REL_CONF, mctx); msgb_free(msg); @@ -1096,6 +1103,8 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) rc = send_rll_simple(RSL_MT_REL_IND, mctx); msgb_free(msg); /* go to idle state */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); return 0; } @@ -1108,6 +1117,8 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) rc = send_rll_simple(RSL_MT_REL_IND, mctx); msgb_free(msg); /* go to idle state */ + lapdm_dl_flush_tx(dl); + lapdm_dl_flush_send(dl); lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); return 0; } -- cgit v1.2.3