Vadim Yanitskiy | c262831 | 2018-09-25 23:03:13 +0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com> |
| 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #include <osmocom/core/logging.h> |
| 26 | #include <osmocom/core/msgb.h> |
| 27 | |
| 28 | #include <osmocom/gsm/gsup.h> |
| 29 | #include <osmocom/gsm/tlv.h> |
| 30 | |
| 31 | /*! \addtogroup gsup |
| 32 | * @{ |
| 33 | * \file gsup_sms.c |
| 34 | * SMS (Short Message Service) extensions for Osmocom GSUP. |
| 35 | */ |
| 36 | |
| 37 | /*! Encode SM-RP-DA IE (see 7.6.8.1), Destination Address. |
| 38 | * \param[out] msg target message buffer (caller-allocated) |
| 39 | * \param[in] gsup_msg abstract GSUP message structure |
| 40 | * \returns 0 in case of success, negative in case of error |
| 41 | */ |
| 42 | int osmo_gsup_sms_encode_sm_rp_da(struct msgb *msg, |
| 43 | const struct osmo_gsup_message *gsup_msg) |
| 44 | { |
| 45 | uint8_t *id_enc; |
| 46 | |
| 47 | switch (gsup_msg->sm_rp_da_type) { |
| 48 | case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: |
| 49 | case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: |
| 50 | case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: |
| 51 | /* Prevent NULL-pointer (or empty) dereference */ |
| 52 | if (gsup_msg->sm_rp_da == NULL || gsup_msg->sm_rp_da_len == 0) { |
| 53 | LOGP(DLGSUP, LOGL_ERROR, "Empty?!? SM-RP-DA ID " |
| 54 | "(type=0x%02x)!\n", gsup_msg->sm_rp_da_type); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | break; |
| 58 | |
| 59 | /* Special case for noSM-RP-DA */ |
| 60 | case OSMO_GSUP_SMS_SM_RP_ODA_NULL: |
| 61 | break; |
| 62 | |
| 63 | case OSMO_GSUP_SMS_SM_RP_ODA_NONE: |
| 64 | default: |
| 65 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID " |
| 66 | "(type=0x%02x)!\n", gsup_msg->sm_rp_da_type); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | /* SM-RP-DA tag | len | ... */ |
| 71 | msgb_tv_put(msg, OSMO_GSUP_SM_RP_DA_IE, gsup_msg->sm_rp_da_len + 1); |
| 72 | msgb_v_put(msg, gsup_msg->sm_rp_da_type); /* ... | id_type */ |
| 73 | |
| 74 | if (gsup_msg->sm_rp_da_type == OSMO_GSUP_SMS_SM_RP_ODA_NULL) |
| 75 | return 0; |
| 76 | |
| 77 | /* ... | id_enc */ |
| 78 | id_enc = msgb_put(msg, gsup_msg->sm_rp_da_len); |
| 79 | memcpy(id_enc, gsup_msg->sm_rp_da, gsup_msg->sm_rp_da_len); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /*! Decode SM-RP-DA IE (see 7.6.8.1), Destination Address. |
| 85 | * \param[out] gsup_msg abstract GSUP message structure |
| 86 | * \param[in] data pointer to the raw IE payload |
| 87 | * \param[in] data_len length of IE pointed by \ref data |
| 88 | * \returns 0 in case of success, negative in case of error |
| 89 | */ |
| 90 | int osmo_gsup_sms_decode_sm_rp_da(struct osmo_gsup_message *gsup_msg, |
| 91 | uint8_t *data, size_t data_len) |
| 92 | { |
| 93 | uint8_t *ptr = data; |
| 94 | uint8_t id_type; |
| 95 | |
| 96 | /* There should be at least id_type */ |
| 97 | if (data_len < 1) { |
| 98 | LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-DA IE " |
| 99 | "(missing identity type)\n"); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | /* ... | id_type | id_enc (optional) */ |
| 104 | id_type = *ptr++; |
| 105 | data_len--; |
| 106 | |
| 107 | /* Parse ID type */ |
| 108 | switch (id_type) { |
| 109 | case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: |
| 110 | case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: |
| 111 | case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: |
| 112 | if (!data_len) { |
| 113 | /* ID shall not be empty (if its type != NULL) */ |
| 114 | LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-DA IE " |
| 115 | "(missing encoded identity)\n"); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | gsup_msg->sm_rp_da_type = id_type; |
| 120 | gsup_msg->sm_rp_da_len = data_len; |
| 121 | gsup_msg->sm_rp_da = ptr; |
| 122 | break; |
| 123 | |
| 124 | /* Special case for noSM-RP-DA */ |
| 125 | case OSMO_GSUP_SMS_SM_RP_ODA_NULL: |
| 126 | if (data_len != 0) { |
| 127 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID, " |
| 128 | "(id_len != 0) for noSM-RP-DA!\n"); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | gsup_msg->sm_rp_da_type = id_type; |
| 133 | gsup_msg->sm_rp_da_len = 0; |
| 134 | gsup_msg->sm_rp_da = NULL; |
| 135 | break; |
| 136 | |
| 137 | case OSMO_GSUP_SMS_SM_RP_ODA_NONE: |
| 138 | default: |
| 139 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-DA ID " |
| 140 | "(type=0x%02x)!\n", id_type); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /*! Encode SM-RP-OA IE (see 7.6.8.2), Originating Address. |
| 148 | * \param[out] msg target message buffer (caller-allocated) |
| 149 | * \param[in] gsup_msg abstract GSUP message structure |
| 150 | * \returns 0 in case of success, negative in case of error |
| 151 | */ |
| 152 | int osmo_gsup_sms_encode_sm_rp_oa(struct msgb *msg, |
| 153 | const struct osmo_gsup_message *gsup_msg) |
| 154 | { |
| 155 | uint8_t *id_enc; |
| 156 | |
| 157 | switch (gsup_msg->sm_rp_oa_type) { |
| 158 | case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: |
| 159 | case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: |
| 160 | /* Prevent NULL-pointer (or empty) dereference */ |
| 161 | if (gsup_msg->sm_rp_oa == NULL || gsup_msg->sm_rp_oa_len == 0) { |
| 162 | LOGP(DLGSUP, LOGL_ERROR, "Empty?!? SM-RP-OA ID " |
| 163 | "(type=0x%02x)!\n", gsup_msg->sm_rp_oa_type); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | break; |
| 167 | |
| 168 | /* Special case for noSM-RP-OA */ |
| 169 | case OSMO_GSUP_SMS_SM_RP_ODA_NULL: |
| 170 | break; |
| 171 | |
| 172 | case OSMO_GSUP_SMS_SM_RP_ODA_NONE: |
| 173 | default: |
| 174 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID " |
| 175 | "(type=0x%02x)!\n", gsup_msg->sm_rp_oa_type); |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | /* SM-RP-OA tag | len | ... */ |
| 180 | msgb_tv_put(msg, OSMO_GSUP_SM_RP_OA_IE, gsup_msg->sm_rp_oa_len + 1); |
| 181 | msgb_v_put(msg, gsup_msg->sm_rp_oa_type); /* ... | id_type */ |
| 182 | |
| 183 | if (gsup_msg->sm_rp_oa_type == OSMO_GSUP_SMS_SM_RP_ODA_NULL) |
| 184 | return 0; |
| 185 | |
| 186 | /* ... | id_enc */ |
| 187 | id_enc = msgb_put(msg, gsup_msg->sm_rp_oa_len); |
| 188 | memcpy(id_enc, gsup_msg->sm_rp_oa, gsup_msg->sm_rp_oa_len); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /*! Decode SM-RP-OA IE (see 7.6.8.2), Originating Address. |
| 194 | * \param[out] gsup_msg abstract GSUP message structure |
| 195 | * \param[in] data pointer to the raw IE payload |
| 196 | * \param[in] data_len length of IE pointed by \ref data |
| 197 | * \returns 0 in case of success, negative in case of error |
| 198 | */ |
| 199 | int osmo_gsup_sms_decode_sm_rp_oa(struct osmo_gsup_message *gsup_msg, |
| 200 | uint8_t *data, size_t data_len) |
| 201 | { |
| 202 | uint8_t *ptr = data; |
| 203 | uint8_t id_type; |
| 204 | |
| 205 | /* There should be at least id_type */ |
| 206 | if (data_len < 1) { |
| 207 | LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-OA IE " |
| 208 | "(missing identity type)\n"); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
| 212 | /* ... | id_type | id_enc (optional) */ |
| 213 | id_type = *ptr++; |
| 214 | data_len--; |
| 215 | |
| 216 | /* Parse ID type */ |
| 217 | switch (id_type) { |
| 218 | case OSMO_GSUP_SMS_SM_RP_ODA_IMSI: |
| 219 | case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN: |
| 220 | case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR: |
| 221 | if (!data_len) { |
| 222 | /* ID shall not be empty (if its type != NULL) */ |
| 223 | LOGP(DLGSUP, LOGL_ERROR, "Corrupted SM-RP-OA IE " |
| 224 | "(missing encoded identity)\n"); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | gsup_msg->sm_rp_oa_type = id_type; |
| 229 | gsup_msg->sm_rp_oa_len = data_len; |
| 230 | gsup_msg->sm_rp_oa = ptr; |
| 231 | break; |
| 232 | |
| 233 | /* Special case for noSM-RP-DA */ |
| 234 | case OSMO_GSUP_SMS_SM_RP_ODA_NULL: |
| 235 | if (data_len != 0) { |
| 236 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID, " |
| 237 | "(id_len != 0) for noSM-RP-DA!\n"); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | gsup_msg->sm_rp_oa_type = id_type; |
| 242 | gsup_msg->sm_rp_oa_len = 0; |
| 243 | gsup_msg->sm_rp_oa = NULL; |
| 244 | break; |
| 245 | |
| 246 | case OSMO_GSUP_SMS_SM_RP_ODA_NONE: |
| 247 | default: |
| 248 | LOGP(DLGSUP, LOGL_ERROR, "Unexpected SM-RP-OA ID " |
| 249 | "(type=0x%02x)!\n", id_type); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /*! @} */ |