diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gsm/Makefile.am | 2 | ||||
-rw-r--r-- | src/gsm/gsup.c | 75 | ||||
-rw-r--r-- | src/gsm/gsup_sms.c | 256 | ||||
-rw-r--r-- | src/gsm/libosmogsm.map | 5 |
4 files changed, 336 insertions, 2 deletions
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am index ccb38add..3d2c5606 100644 --- a/src/gsm/Makefile.am +++ b/src/gsm/Makefile.am @@ -29,7 +29,7 @@ libgsmint_la_SOURCES = a5.c rxlev_stat.c tlv_parser.c comp128.c comp128v23.c \ auth_milenage.c milenage/aes-encblock.c gea.c \ milenage/aes-internal.c milenage/aes-internal-enc.c \ milenage/milenage.c gan.c ipa.c gsm0341.c apn.c \ - gsup.c gprs_gea.c gsm0503_conv.c oap.c gsm0808_utils.c \ + gsup.c gsup_sms.c gprs_gea.c gsm0503_conv.c oap.c gsm0808_utils.c \ gsm23003.c mncc.c bts_features.c oap_client.c \ gsm29118.c libgsmint_la_LDFLAGS = -no-undefined diff --git a/src/gsm/gsup.c b/src/gsm/gsup.c index 18b35806..e4339971 100644 --- a/src/gsm/gsup.c +++ b/src/gsm/gsup.c @@ -67,6 +67,14 @@ const struct value_string osmo_gsup_message_type_names[] = { OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PROC_SS_ERROR), OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PROC_SS_RESULT), + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST), + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR), + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT), + + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST), + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR), + OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT), + { 0, NULL } }; @@ -434,6 +442,35 @@ int osmo_gsup_decode(const uint8_t *const_data, size_t data_len, gsup_msg->ss_info_len = value_len; break; + case OSMO_GSUP_SM_RP_MR_IE: + gsup_msg->sm_rp_mr = value; + break; + + case OSMO_GSUP_SM_RP_DA_IE: + rc = osmo_gsup_sms_decode_sm_rp_da(gsup_msg, value, value_len); + if (rc) + return rc; + break; + + case OSMO_GSUP_SM_RP_OA_IE: + rc = osmo_gsup_sms_decode_sm_rp_oa(gsup_msg, value, value_len); + if (rc) + return rc; + break; + + case OSMO_GSUP_SM_RP_UI_IE: + gsup_msg->sm_rp_ui = value; + gsup_msg->sm_rp_ui_len = value_len; + break; + + case OSMO_GSUP_SM_RP_MMS_IE: + gsup_msg->sm_rp_mms = value; + break; + + case OSMO_GSUP_SM_RP_CAUSE_IE: + gsup_msg->sm_rp_cause = value; + break; + default: LOGP(DLGSUP, LOGL_NOTICE, "GSUP IE type %d unknown\n", iei); @@ -529,7 +566,7 @@ static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei, int osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg) { uint8_t u8; - int idx; + int idx, rc; uint8_t bcd_buf[GSM48_MI_SIZE] = {0}; size_t bcd_len; @@ -626,6 +663,42 @@ int osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg) gsup_msg->ss_info_len, gsup_msg->ss_info); } + if (gsup_msg->sm_rp_mr) { + msgb_tlv_put(msg, OSMO_GSUP_SM_RP_MR_IE, + sizeof(*gsup_msg->sm_rp_mr), gsup_msg->sm_rp_mr); + } + + if (gsup_msg->sm_rp_da_type) { + rc = osmo_gsup_sms_encode_sm_rp_da(msg, gsup_msg); + if (rc) { + LOGP(DLGSUP, LOGL_ERROR, "Failed to encode SM-RP-DA IE\n"); + return -EINVAL; + } + } + + if (gsup_msg->sm_rp_oa_type) { + rc = osmo_gsup_sms_encode_sm_rp_oa(msg, gsup_msg); + if (rc) { + LOGP(DLGSUP, LOGL_ERROR, "Failed to encode SM-RP-OA IE\n"); + return -EINVAL; + } + } + + if (gsup_msg->sm_rp_ui) { + msgb_tlv_put(msg, OSMO_GSUP_SM_RP_UI_IE, + gsup_msg->sm_rp_ui_len, gsup_msg->sm_rp_ui); + } + + if (gsup_msg->sm_rp_mms) { + msgb_tlv_put(msg, OSMO_GSUP_SM_RP_MMS_IE, + sizeof(*gsup_msg->sm_rp_mms), gsup_msg->sm_rp_mms); + } + + if (gsup_msg->sm_rp_cause) { + msgb_tlv_put(msg, OSMO_GSUP_SM_RP_CAUSE_IE, + sizeof(*gsup_msg->sm_rp_cause), gsup_msg->sm_rp_cause); + } + return 0; } diff --git a/src/gsm/gsup_sms.c b/src/gsm/gsup_sms.c new file mode 100644 index 00000000..d49cf20a --- /dev/null +++ b/src/gsm/gsup_sms.c @@ -0,0 +1,256 @@ +/* + * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com> + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#include <stdint.h> +#include <string.h> +#include <errno.h> + +#include <osmocom/core/logging.h> +#include <osmocom/core/msgb.h> + +#include <osmocom/gsm/gsup.h> +#include <osmocom/gsm/tlv.h> + +/*! \addtogroup gsup + * @{ + * \file gsup_sms.c + * SMS (Short Message Service) extensions for Osmocom GSUP. + */ + +/*! Encode SM-RP-DA IE (see 7.6.8.1), Destination Address. + * \param[out] msg target message buffer (caller-allocated) + * \param[in] gsup_msg abstract GSUP message structure + * \returns 0 in case of success, negative in case of error + */ +int osmo_gsup_sms_encode_sm_rp_da(struct msgb *msg, + const struct osmo_gsup_message *gsup_msg) +{ + uint8_t *id_enc; + + switch (gsup_msg->sm_rp_da_type) { + case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: + case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: + case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: + /* Prevent NULL-pointer (or empty) dereference */ + if (gsup_msg->sm_rp_da == NULL || gsup_msg->sm_rp_da_len == 0) { + LOGP(DLGSUP, LOGL_ERROR, "Empty?!? SM-RP-DA ID " + "(type=0x%02x)!\n", gsup_msg->sm_rp_da_type); + return -EINVAL; + } + break; + + /* Special case for noSM-RP-DA */ + case OSMO_GSUP_SMS_SM_RP_ODA_NULL: + break; + + case OSMO_GSUP_SMS_SM_RP_ODA_NONE: + default: + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID " + "(type=0x%02x)!\n", gsup_msg->sm_rp_da_type); + return -EINVAL; + } + + /* SM-RP-DA tag | len | ... */ + msgb_tv_put(msg, OSMO_GSUP_SM_RP_DA_IE, gsup_msg->sm_rp_da_len + 1); + msgb_v_put(msg, gsup_msg->sm_rp_da_type); /* ... | id_type */ + + if (gsup_msg->sm_rp_da_type == OSMO_GSUP_SMS_SM_RP_ODA_NULL) + return 0; + + /* ... | id_enc */ + id_enc = msgb_put(msg, gsup_msg->sm_rp_da_len); + memcpy(id_enc, gsup_msg->sm_rp_da, gsup_msg->sm_rp_da_len); + + return 0; +} + +/*! Decode SM-RP-DA IE (see 7.6.8.1), Destination Address. + * \param[out] gsup_msg abstract GSUP message structure + * \param[in] data pointer to the raw IE payload + * \param[in] data_len length of IE pointed by \ref data + * \returns 0 in case of success, negative in case of error + */ +int osmo_gsup_sms_decode_sm_rp_da(struct osmo_gsup_message *gsup_msg, + uint8_t *data, size_t data_len) +{ + uint8_t *ptr = data; + uint8_t id_type; + + /* There should be at least id_type */ + if (data_len < 1) { + LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-DA IE " + "(missing identity type)\n"); + return -EINVAL; + } + + /* ... | id_type | id_enc (optional) */ + id_type = *ptr++; + data_len--; + + /* Parse ID type */ + switch (id_type) { + case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: + case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: + case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: + if (!data_len) { + /* ID shall not be empty (if its type != NULL) */ + LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-DA IE " + "(missing encoded identity)\n"); + return -EINVAL; + } + + gsup_msg->sm_rp_da_type = id_type; + gsup_msg->sm_rp_da_len = data_len; + gsup_msg->sm_rp_da = ptr; + break; + + /* Special case for noSM-RP-DA */ + case OSMO_GSUP_SMS_SM_RP_ODA_NULL: + if (data_len != 0) { + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID, " + "(id_len != 0) for noSM-RP-DA!\n"); + return -EINVAL; + } + + gsup_msg->sm_rp_da_type = id_type; + gsup_msg->sm_rp_da_len = 0; + gsup_msg->sm_rp_da = NULL; + break; + + case OSMO_GSUP_SMS_SM_RP_ODA_NONE: + default: + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID " + "(type=0x%02x)!\n", id_type); + return -EINVAL; + } + + return 0; +} + +/*! Encode SM-RP-OA IE (see 7.6.8.2), Originating Address. + * \param[out] msg target message buffer (caller-allocated) + * \param[in] gsup_msg abstract GSUP message structure + * \returns 0 in case of success, negative in case of error + */ +int osmo_gsup_sms_encode_sm_rp_oa(struct msgb *msg, + const struct osmo_gsup_message *gsup_msg) +{ + uint8_t *id_enc; + + switch (gsup_msg->sm_rp_oa_type) { + case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: + case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: + /* Prevent NULL-pointer (or empty) dereference */ + if (gsup_msg->sm_rp_oa == NULL || gsup_msg->sm_rp_oa_len == 0) { + LOGP(DLGSUP, LOGL_ERROR, "Empty?!? SM-RP-OA ID " + "(type=0x%02x)!\n", gsup_msg->sm_rp_oa_type); + return -EINVAL; + } + break; + + /* Special case for noSM-RP-OA */ + case OSMO_GSUP_SMS_SM_RP_ODA_NULL: + break; + + case OSMO_GSUP_SMS_SM_RP_ODA_NONE: + default: + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID " + "(type=0x%02x)!\n", gsup_msg->sm_rp_oa_type); + return -EINVAL; + } + + /* SM-RP-OA tag | len | ... */ + msgb_tv_put(msg, OSMO_GSUP_SM_RP_OA_IE, gsup_msg->sm_rp_oa_len + 1); + msgb_v_put(msg, gsup_msg->sm_rp_oa_type); /* ... | id_type */ + + if (gsup_msg->sm_rp_oa_type == OSMO_GSUP_SMS_SM_RP_ODA_NULL) + return 0; + + /* ... | id_enc */ + id_enc = msgb_put(msg, gsup_msg->sm_rp_oa_len); + memcpy(id_enc, gsup_msg->sm_rp_oa, gsup_msg->sm_rp_oa_len); + + return 0; +} + +/*! Decode SM-RP-OA IE (see 7.6.8.2), Originating Address. + * \param[out] gsup_msg abstract GSUP message structure + * \param[in] data pointer to the raw IE payload + * \param[in] data_len length of IE pointed by \ref data + * \returns 0 in case of success, negative in case of error + */ +int osmo_gsup_sms_decode_sm_rp_oa(struct osmo_gsup_message *gsup_msg, + uint8_t *data, size_t data_len) +{ + uint8_t *ptr = data; + uint8_t id_type; + + /* There should be at least id_type */ + if (data_len < 1) { + LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-OA IE " + "(missing identity type)\n"); + return -EINVAL; + } + + /* ... | id_type | id_enc (optional) */ + id_type = *ptr++; + data_len--; + + /* Parse ID type */ + switch (id_type) { + case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: + case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: + case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: + if (!data_len) { + /* ID shall not be empty (if its type != NULL) */ + LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-OA IE " + "(missing encoded identity)\n"); + return -EINVAL; + } + + gsup_msg->sm_rp_oa_type = id_type; + gsup_msg->sm_rp_oa_len = data_len; + gsup_msg->sm_rp_oa = ptr; + break; + + /* Special case for noSM-RP-DA */ + case OSMO_GSUP_SMS_SM_RP_ODA_NULL: + if (data_len != 0) { + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID, " + "(id_len != 0) for noSM-RP-DA!\n"); + return -EINVAL; + } + + gsup_msg->sm_rp_oa_type = id_type; + gsup_msg->sm_rp_oa_len = 0; + gsup_msg->sm_rp_oa = NULL; + break; + + case OSMO_GSUP_SMS_SM_RP_ODA_NONE: + default: + LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID " + "(type=0x%02x)!\n", id_type); + return -EINVAL; + } + + return 0; +} + +/*! @} */ diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map index 959d1827..90c21954 100644 --- a/src/gsm/libosmogsm.map +++ b/src/gsm/libosmogsm.map @@ -523,6 +523,11 @@ osmo_gsup_message_type_names; osmo_gsup_session_state_names; osmo_gsup_get_err_msg_type; +osmo_gsup_sms_encode_sm_rp_da; +osmo_gsup_sms_decode_sm_rp_da; +osmo_gsup_sms_encode_sm_rp_oa; +osmo_gsup_sms_decode_sm_rp_oa; + osmo_oap_encode; osmo_oap_decode; |