Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 1 | /* OpenBSC SMPP 3.4 interface, SMSC-side implementation */ |
| 2 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 3 | /* (C) 2012-2013 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 4 | * |
Harald Welte | 995ff35 | 2013-07-13 16:35:32 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 6 | * |
Harald Welte | 995ff35 | 2013-07-13 16:35:32 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 11 | * |
Harald Welte | 995ff35 | 2013-07-13 16:35:32 +0200 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <stdint.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <smpp34.h> |
| 29 | #include <smpp34_structs.h> |
| 30 | #include <smpp34_params.h> |
| 31 | |
| 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/logging.h> |
| 35 | #include <osmocom/core/talloc.h> |
| 36 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 37 | #include <osmocom/gsm/protocol/smpp34_osmocom.h> |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 38 | |
| 39 | #include <openbsc/gsm_subscriber.h> |
| 40 | #include <openbsc/debug.h> |
| 41 | #include <openbsc/db.h> |
| 42 | #include <openbsc/gsm_04_11.h> |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 43 | #include <openbsc/gsm_data.h> |
| 44 | #include <openbsc/signal.h> |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 45 | #include <openbsc/transaction.h> |
| 46 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 47 | |
| 48 | #include "smpp_smsc.h" |
| 49 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 50 | /*! \brief find gsm_subscriber for a given SMPP NPI/TON/Address */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 51 | static struct gsm_subscriber *subscr_by_dst(struct gsm_network *net, |
| 52 | uint8_t npi, uint8_t ton, const char *addr) |
| 53 | { |
| 54 | struct gsm_subscriber *subscr = NULL; |
| 55 | |
| 56 | switch (npi) { |
| 57 | case NPI_Land_Mobile_E212: |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 58 | subscr = subscr_get_by_imsi(net->subscr_group, addr); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 59 | break; |
| 60 | case NPI_ISDN_E163_E164: |
| 61 | case NPI_Private: |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 62 | subscr = subscr_get_by_extension(net->subscr_group, addr); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 63 | break; |
| 64 | default: |
| 65 | LOGP(DSMPP, LOGL_NOTICE, "Unsupported NPI: %u\n", npi); |
| 66 | break; |
| 67 | } |
| 68 | |
Holger Hans Peter Freyther | 095bd36 | 2013-12-29 20:30:02 +0100 | [diff] [blame] | 69 | /* tag the context in case we know it */ |
Neels Hofmeyr | 89a8e72 | 2017-02-23 18:00:51 +0100 | [diff] [blame] | 70 | log_set_context(LOG_CTX_VLR_SUBSCR, subscr); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 71 | return subscr; |
| 72 | } |
| 73 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 74 | /*! \brief find a TLV with given tag in list of libsmpp34 TLVs */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 75 | static struct tlv_t *find_tlv(struct tlv_t *head, uint16_t tag) |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 76 | { |
| 77 | struct tlv_t *t; |
| 78 | |
| 79 | for (t = head; t != NULL; t = t->next) { |
| 80 | if (t->tag == tag) |
| 81 | return t; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 86 | /*! \brief convert from submit_sm_t to gsm_sms */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 87 | static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net, |
| 88 | const struct submit_sm_t *submit) |
| 89 | { |
| 90 | struct gsm_subscriber *dest; |
| 91 | struct gsm_sms *sms; |
| 92 | struct tlv_t *t; |
| 93 | const uint8_t *sms_msg; |
| 94 | unsigned int sms_msg_len; |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 95 | int mode; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 96 | |
| 97 | dest = subscr_by_dst(net, submit->dest_addr_npi, |
| 98 | submit->dest_addr_ton, |
| 99 | (const char *)submit->destination_addr); |
| 100 | if (!dest) { |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 101 | LOGP(DLSMS, LOGL_NOTICE, "SMPP SUBMIT-SM for unknown subscriber: " |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 102 | "%s (NPI=%u)\n", submit->destination_addr, |
| 103 | submit->dest_addr_npi); |
| 104 | return ESME_RINVDSTADR; |
| 105 | } |
| 106 | |
| 107 | t = find_tlv(submit->tlv, TLVID_message_payload); |
| 108 | if (t) { |
| 109 | if (submit->sm_length) { |
Holger Hans Peter Freyther | 94f83e1 | 2014-07-19 19:02:46 +0200 | [diff] [blame] | 110 | /* ERROR: we cannot have both! */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 111 | LOGP(DLSMS, LOGL_ERROR, "SMPP Cannot have payload in " |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 112 | "TLV _and_ in the header\n"); |
Holger Hans Peter Freyther | 5425e5e | 2015-08-04 12:22:56 +0200 | [diff] [blame] | 113 | subscr_put(dest); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 114 | return ESME_ROPTPARNOTALLWD; |
| 115 | } |
| 116 | sms_msg = t->value.octet; |
| 117 | sms_msg_len = t->length; |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 118 | } else if (submit->sm_length > 0 && submit->sm_length < 255) { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 119 | sms_msg = submit->short_message; |
| 120 | sms_msg_len = submit->sm_length; |
| 121 | } else { |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 122 | LOGP(DLSMS, LOGL_ERROR, |
| 123 | "SMPP neither message payload nor valid sm_length.\n"); |
Holger Hans Peter Freyther | 5425e5e | 2015-08-04 12:22:56 +0200 | [diff] [blame] | 124 | subscr_put(dest); |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 125 | return ESME_RINVPARLEN; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | sms = sms_alloc(); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 129 | sms->source = SMS_SOURCE_SMPP; |
| 130 | sms->smpp.sequence_nr = submit->sequence_number; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 131 | |
| 132 | /* fill in the destination address */ |
Harald Welte | 1a2993a | 2012-11-09 19:48:48 +0100 | [diff] [blame] | 133 | sms->receiver = dest; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 134 | sms->dst.ton = submit->dest_addr_ton; |
| 135 | sms->dst.npi = submit->dest_addr_npi; |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 136 | osmo_strlcpy(sms->dst.addr, dest->extension, sizeof(sms->dst.addr)); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 137 | |
| 138 | /* fill in the source address */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 139 | sms->src.ton = submit->source_addr_ton; |
| 140 | sms->src.npi = submit->source_addr_npi; |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 141 | osmo_strlcpy(sms->src.addr, (char *)submit->source_addr, |
| 142 | sizeof(sms->src.addr)); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 143 | |
| 144 | if (submit->esm_class & 0x40) |
| 145 | sms->ud_hdr_ind = 1; |
| 146 | |
| 147 | if (submit->esm_class & 0x80) { |
| 148 | sms->reply_path_req = 1; |
| 149 | #warning Implement reply path |
| 150 | } |
| 151 | |
Harald Welte | 9ad0362 | 2012-11-08 22:15:04 +0100 | [diff] [blame] | 152 | if (submit->data_coding == 0x00 || /* SMSC default */ |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 153 | submit->data_coding == 0x01) { /* GSM default alphabet */ |
| 154 | sms->data_coding_scheme = GSM338_DCS_1111_7BIT; |
| 155 | mode = MODE_7BIT; |
| 156 | } else if ((submit->data_coding & 0xFC) == 0xF0) { /* 03.38 DCS default */ |
| 157 | /* pass DCS 1:1 through from SMPP to GSM */ |
| 158 | sms->data_coding_scheme = submit->data_coding; |
| 159 | mode = MODE_7BIT; |
| 160 | } else if (submit->data_coding == 0x02 || |
| 161 | submit->data_coding == 0x04) { |
| 162 | /* 8-bit binary */ |
| 163 | sms->data_coding_scheme = GSM338_DCS_1111_8BIT_DATA; |
| 164 | mode = MODE_8BIT; |
| 165 | } else if ((submit->data_coding & 0xFC) == 0xF4) { /* 03.38 DCS 8bit */ |
| 166 | /* pass DCS 1:1 through from SMPP to GSM */ |
| 167 | sms->data_coding_scheme = submit->data_coding; |
| 168 | mode = MODE_8BIT; |
Harald Welte | 7e40be3 | 2014-02-21 13:21:03 +0100 | [diff] [blame] | 169 | } else if (submit->data_coding == 0x08) { |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 170 | /* UCS-2 */ |
| 171 | sms->data_coding_scheme = (2 << 2); |
| 172 | mode = MODE_8BIT; |
| 173 | } else { |
| 174 | sms_free(sms); |
| 175 | LOGP(DLSMS, LOGL_ERROR, "SMPP Unknown Data Coding 0x%02x\n", |
| 176 | submit->data_coding); |
| 177 | return ESME_RUNKNOWNERR; |
| 178 | } |
| 179 | |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 180 | if (mode == MODE_7BIT) { |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 181 | uint8_t ud_len = 0, padbits = 0; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 182 | sms->data_coding_scheme = GSM338_DCS_1111_7BIT; |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 183 | if (sms->ud_hdr_ind) { |
| 184 | ud_len = *sms_msg + 1; |
| 185 | printf("copying %u bytes user data...\n", ud_len); |
| 186 | memcpy(sms->user_data, sms_msg, |
| 187 | OSMO_MIN(ud_len, sizeof(sms->user_data))); |
| 188 | sms_msg += ud_len; |
| 189 | sms_msg_len -= ud_len; |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 190 | padbits = 7 - (ud_len % 7); |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 191 | } |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 192 | gsm_septets2octets(sms->user_data+ud_len, sms_msg, |
| 193 | sms_msg_len, padbits); |
| 194 | sms->user_data_len = (ud_len*8 + padbits)/7 + sms_msg_len;/* SEPTETS */ |
| 195 | /* FIXME: sms->text */ |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 196 | } else { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 197 | memcpy(sms->user_data, sms_msg, sms_msg_len); |
| 198 | sms->user_data_len = sms_msg_len; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | *psms = sms; |
| 202 | return ESME_ROK; |
| 203 | } |
| 204 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 205 | /*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 206 | int handle_smpp_submit(struct osmo_esme *esme, struct submit_sm_t *submit, |
| 207 | struct submit_sm_resp_t *submit_r) |
| 208 | { |
| 209 | struct gsm_sms *sms; |
Holger Hans Peter Freyther | b5a4edd | 2013-01-20 17:43:50 +0100 | [diff] [blame] | 210 | struct gsm_network *net = esme->smsc->priv; |
| 211 | struct sms_signal_data sig; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 212 | int rc = -1; |
| 213 | |
Holger Hans Peter Freyther | b5a4edd | 2013-01-20 17:43:50 +0100 | [diff] [blame] | 214 | rc = submit_to_sms(&sms, net, submit); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 215 | if (rc != ESME_ROK) { |
| 216 | submit_r->command_status = rc; |
| 217 | return 0; |
| 218 | } |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 219 | smpp_esme_get(esme); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 220 | sms->smpp.esme = esme; |
Harald Welte | 9ad0362 | 2012-11-08 22:15:04 +0100 | [diff] [blame] | 221 | sms->protocol_id = submit->protocol_id; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 222 | |
| 223 | switch (submit->esm_class & 3) { |
| 224 | case 0: /* default */ |
| 225 | case 1: /* datagram */ |
| 226 | case 3: /* store-and-forward */ |
| 227 | rc = db_sms_store(sms); |
Holger Hans Peter Freyther | 6a85c15 | 2013-01-20 17:21:31 +0100 | [diff] [blame] | 228 | sms_free(sms); |
| 229 | sms = NULL; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 230 | if (rc < 0) { |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 231 | LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to " |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 232 | "store SMS in database\n"); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 233 | submit_r->command_status = ESME_RSYSERR; |
| 234 | return 0; |
| 235 | } |
| 236 | strcpy((char *)submit_r->message_id, "msg_id_not_implemented"); |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 237 | LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Stored in DB\n"); |
Holger Hans Peter Freyther | b5a4edd | 2013-01-20 17:43:50 +0100 | [diff] [blame] | 238 | |
| 239 | memset(&sig, 0, sizeof(sig)); |
| 240 | osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 241 | rc = 0; |
| 242 | break; |
| 243 | case 2: /* forward (i.e. transaction) mode */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 244 | LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in " |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 245 | "real time (Transaction/Forward mode)\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 246 | sms->smpp.transaction_mode = 1; |
| 247 | gsm411_send_sms_subscr(sms->receiver, sms); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 248 | rc = 1; /* don't send any response yet */ |
| 249 | break; |
| 250 | } |
| 251 | return rc; |
| 252 | } |
| 253 | |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 254 | static void alert_all_esme(struct smsc *smsc, struct gsm_subscriber *subscr, |
| 255 | uint8_t smpp_avail_status) |
| 256 | { |
| 257 | struct osmo_esme *esme; |
| 258 | |
| 259 | llist_for_each_entry(esme, &smsc->esme_list, list) { |
| 260 | /* we currently send an alert notification to each ESME that is |
| 261 | * connected, and do not require a (non-existant) delivery |
| 262 | * pending flag to be set before, FIXME: make this VTY |
| 263 | * configurable */ |
| 264 | if (esme->acl && esme->acl->deliver_src_imsi) { |
| 265 | smpp_tx_alert(esme, TON_Subscriber_Number, |
| 266 | NPI_Land_Mobile_E212, |
Keith | 80abe52 | 2016-12-22 19:18:18 +0100 | [diff] [blame] | 267 | subscr->imsi, smpp_avail_status); |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 268 | } else { |
| 269 | smpp_tx_alert(esme, TON_Network_Specific, |
| 270 | NPI_ISDN_E163_E164, |
Keith | 80abe52 | 2016-12-22 19:18:18 +0100 | [diff] [blame] | 271 | subscr->extension, smpp_avail_status); |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 277 | /*! \brief signal handler for status of attempted SMS deliveries */ |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 278 | static int smpp_sms_cb(unsigned int subsys, unsigned int signal, |
| 279 | void *handler_data, void *signal_data) |
| 280 | { |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 281 | struct sms_signal_data *sig_sms = signal_data; |
| 282 | struct gsm_sms *sms = sig_sms->sms; |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 283 | struct smsc *smsc = handler_data; |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 284 | int rc = 0; |
| 285 | |
| 286 | if (!sms) |
| 287 | return 0; |
| 288 | |
| 289 | if (sms->source != SMS_SOURCE_SMPP) |
| 290 | return 0; |
| 291 | |
| 292 | switch (signal) { |
Harald Welte | 1aeb2af | 2013-07-30 22:30:24 +0800 | [diff] [blame] | 293 | case S_SMS_MEM_EXCEEDED: |
| 294 | /* fall-through: There is no ESME_Rxxx result code to |
| 295 | * indicate a MEMORY EXCEEDED in transaction mode back |
| 296 | * to the ESME */ |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 297 | case S_SMS_UNKNOWN_ERROR: |
| 298 | if (sms->smpp.transaction_mode) { |
| 299 | /* Send back the SUBMIT-SM response with apropriate error */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 300 | LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 301 | rc = smpp_tx_submit_r(sms->smpp.esme, |
| 302 | sms->smpp.sequence_nr, |
| 303 | ESME_RDELIVERYFAILURE, |
| 304 | sms->smpp.msg_id); |
| 305 | } |
| 306 | break; |
| 307 | case S_SMS_DELIVERED: |
| 308 | /* SMS layer tells us the delivery has been completed */ |
| 309 | if (sms->smpp.transaction_mode) { |
| 310 | /* Send back the SUBMIT-SM response */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 311 | LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 312 | rc = smpp_tx_submit_r(sms->smpp.esme, |
| 313 | sms->smpp.sequence_nr, |
| 314 | ESME_ROK, sms->smpp.msg_id); |
| 315 | } |
| 316 | break; |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 317 | case S_SMS_SMMA: |
| 318 | if (!sig_sms->trans || !sig_sms->trans->subscr) { |
| 319 | /* SMMA without a subscriber? strange... */ |
| 320 | LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n"); |
| 321 | break; |
| 322 | } |
| 323 | |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 324 | /* There's no real 1:1 match for SMMA in SMPP. However, |
| 325 | * an ALERT NOTIFICATION seems to be the most logical |
| 326 | * choice */ |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 327 | alert_all_esme(smsc, sig_sms->trans->subscr, 0); |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 328 | break; |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return rc; |
| 332 | } |
| 333 | |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 334 | /*! \brief signal handler for subscriber related signals */ |
| 335 | static int smpp_subscr_cb(unsigned int subsys, unsigned int signal, |
| 336 | void *handler_data, void *signal_data) |
| 337 | { |
| 338 | struct gsm_subscriber *subscr = signal_data; |
| 339 | struct smsc *smsc = handler_data; |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 340 | uint8_t smpp_avail_status; |
| 341 | |
| 342 | /* determine the smpp_avail_status depending on attach/detach */ |
| 343 | switch (signal) { |
| 344 | case S_SUBSCR_ATTACHED: |
| 345 | smpp_avail_status = 0; |
| 346 | break; |
| 347 | case S_SUBSCR_DETACHED: |
| 348 | smpp_avail_status = 2; |
| 349 | break; |
| 350 | default: |
| 351 | return 0; |
| 352 | } |
| 353 | |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 354 | alert_all_esme(smsc, subscr, smpp_avail_status); |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 359 | /* GSM 03.38 6.2.1 Character expanding (no decode!) */ |
| 360 | static int gsm_7bit_expand(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind) |
| 361 | { |
| 362 | int i = 0; |
| 363 | int shift = 0; |
| 364 | uint8_t c; |
| 365 | |
| 366 | /* skip the user data header */ |
| 367 | if (ud_hdr_ind) { |
| 368 | /* get user data header length + 1 (for the 'user data header length'-field) */ |
| 369 | shift = ((user_data[0] + 1) * 8) / 7; |
| 370 | if ((((user_data[0] + 1) * 8) % 7) != 0) |
| 371 | shift++; |
| 372 | septet_l = septet_l - shift; |
| 373 | } |
| 374 | |
| 375 | for (i = 0; i < septet_l; i++) { |
| 376 | c = |
| 377 | ((user_data[((i + shift) * 7 + 7) >> 3] << |
| 378 | (7 - (((i + shift) * 7 + 7) & 7))) | |
| 379 | (user_data[((i + shift) * 7) >> 3] >> |
| 380 | (((i + shift) * 7) & 7))) & 0x7f; |
| 381 | |
| 382 | *(text++) = c; |
| 383 | } |
| 384 | |
| 385 | *text = '\0'; |
| 386 | |
| 387 | return i; |
| 388 | } |
| 389 | |
| 390 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 391 | /* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */ |
| 392 | void append_tlv(tlv_t **req_tlv, uint16_t tag, |
| 393 | const uint8_t *data, uint16_t len) |
| 394 | { |
| 395 | tlv_t tlv; |
| 396 | |
| 397 | memset(&tlv, 0, sizeof(tlv)); |
| 398 | tlv.tag = tag; |
| 399 | tlv.length = len; |
| 400 | memcpy(tlv.value.octet, data, tlv.length); |
| 401 | build_tlv(req_tlv, &tlv); |
| 402 | } |
| 403 | void append_tlv_u8(tlv_t **req_tlv, uint16_t tag, uint8_t val) |
| 404 | { |
| 405 | tlv_t tlv; |
| 406 | |
| 407 | memset(&tlv, 0, sizeof(tlv)); |
| 408 | tlv.tag = tag; |
| 409 | tlv.length = 1; |
| 410 | tlv.value.val08 = val; |
| 411 | build_tlv(req_tlv, &tlv); |
| 412 | } |
| 413 | void append_tlv_u16(tlv_t **req_tlv, uint16_t tag, uint16_t val) |
| 414 | { |
| 415 | tlv_t tlv; |
| 416 | |
| 417 | memset(&tlv, 0, sizeof(tlv)); |
| 418 | tlv.tag = tag; |
| 419 | tlv.length = 2; |
| 420 | tlv.value.val16 = htons(val); |
| 421 | build_tlv(req_tlv, &tlv); |
| 422 | } |
| 423 | |
| 424 | /* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */ |
| 425 | static void append_osmo_tlvs(tlv_t **req_tlv, const struct gsm_lchan *lchan) |
| 426 | { |
| 427 | int idx = calc_initial_idx(ARRAY_SIZE(lchan->meas_rep), |
| 428 | lchan->meas_rep_idx, 1); |
| 429 | const struct gsm_meas_rep *mr = &lchan->meas_rep[idx]; |
| 430 | const struct gsm_meas_rep_unidir *ul_meas = &mr->ul; |
| 431 | const struct gsm_meas_rep_unidir *dl_meas = &mr->dl; |
| 432 | |
| 433 | /* Osmocom vendor-specific SMPP34 extensions */ |
| 434 | append_tlv_u16(req_tlv, TLVID_osmo_arfcn, lchan->ts->trx->arfcn); |
| 435 | if (mr->flags & MEAS_REP_F_MS_L1) { |
| 436 | uint8_t ms_dbm; |
| 437 | append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_l1.ta); |
| 438 | ms_dbm = ms_pwr_dbm(lchan->ts->trx->bts->band, mr->ms_l1.pwr); |
| 439 | append_tlv_u8(req_tlv, TLVID_osmo_ms_l1_txpwr, ms_dbm); |
Max | 11e4e41 | 2017-04-20 13:07:58 +0200 | [diff] [blame] | 440 | } else if (mr->flags & MEAS_REP_F_MS_TO) /* Save Timing Offset field = MS Timing Offset + 63 */ |
| 441 | append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_timing_offset + 63); |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 442 | |
| 443 | append_tlv_u16(req_tlv, TLVID_osmo_rxlev_ul, |
| 444 | rxlev2dbm(ul_meas->full.rx_lev)); |
| 445 | append_tlv_u8(req_tlv, TLVID_osmo_rxqual_ul, ul_meas->full.rx_qual); |
| 446 | |
| 447 | if (mr->flags & MEAS_REP_F_DL_VALID) { |
| 448 | append_tlv_u16(req_tlv, TLVID_osmo_rxlev_dl, |
| 449 | rxlev2dbm(dl_meas->full.rx_lev)); |
| 450 | append_tlv_u8(req_tlv, TLVID_osmo_rxqual_dl, |
| 451 | dl_meas->full.rx_qual); |
| 452 | } |
| 453 | |
| 454 | if (lchan->conn && lchan->conn->subscr) { |
| 455 | struct gsm_subscriber *subscr = lchan->conn->subscr; |
| 456 | size_t imei_len = strlen(subscr->equipment.imei); |
| 457 | if (imei_len) |
| 458 | append_tlv(req_tlv, TLVID_osmo_imei, |
| 459 | (uint8_t *)subscr->equipment.imei, imei_len+1); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms, |
| 464 | struct gsm_subscriber_connection *conn) |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 465 | { |
| 466 | struct deliver_sm_t deliver; |
| 467 | uint8_t dcs; |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 468 | int mode; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 469 | |
| 470 | memset(&deliver, 0, sizeof(deliver)); |
| 471 | deliver.command_length = 0; |
| 472 | deliver.command_id = DELIVER_SM; |
| 473 | deliver.command_status = ESME_ROK; |
| 474 | |
| 475 | strcpy((char *)deliver.service_type, "CMT"); |
| 476 | if (esme->acl && esme->acl->deliver_src_imsi) { |
| 477 | deliver.source_addr_ton = TON_Subscriber_Number; |
| 478 | deliver.source_addr_npi = NPI_Land_Mobile_E212; |
| 479 | snprintf((char *)deliver.source_addr, |
| 480 | sizeof(deliver.source_addr), "%s", |
Holger Hans Peter Freyther | ca3c256 | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 481 | conn->subscr->imsi); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 482 | } else { |
| 483 | deliver.source_addr_ton = TON_Network_Specific; |
| 484 | deliver.source_addr_npi = NPI_ISDN_E163_E164; |
| 485 | snprintf((char *)deliver.source_addr, |
| 486 | sizeof(deliver.source_addr), "%s", |
Holger Hans Peter Freyther | ca3c256 | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 487 | conn->subscr->extension); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 490 | deliver.dest_addr_ton = sms->dst.ton; |
| 491 | deliver.dest_addr_npi = sms->dst.npi; |
| 492 | memcpy(deliver.destination_addr, sms->dst.addr, |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 493 | sizeof(deliver.destination_addr)); |
| 494 | |
| 495 | deliver.esm_class = 1; /* datagram mode */ |
| 496 | if (sms->ud_hdr_ind) |
| 497 | deliver.esm_class |= 0x40; |
| 498 | if (sms->reply_path_req) |
| 499 | deliver.esm_class |= 0x80; |
| 500 | |
| 501 | deliver.protocol_id = sms->protocol_id; |
| 502 | deliver.priority_flag = 0; |
| 503 | deliver.registered_delivery = 0; |
| 504 | |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 505 | /* Figure out SMPP DCS from TP-DCS */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 506 | dcs = sms->data_coding_scheme; |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 507 | if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1) |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 508 | return -1; |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 509 | |
| 510 | /* Transparently pass on DCS via SMPP if requested */ |
Holger Hans Peter Freyther | 921b227 | 2013-07-14 08:54:07 +0200 | [diff] [blame] | 511 | if (esme->acl && esme->acl->dcs_transparent) |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 512 | deliver.data_coding = dcs; |
| 513 | |
| 514 | if (mode == MODE_7BIT) { |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 515 | uint8_t *dst = deliver.short_message; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 516 | |
| 517 | /* SMPP has this strange notion of putting 7bit SMS in |
| 518 | * an octet-aligned mode */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 519 | if (sms->ud_hdr_ind) { |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 520 | /* length (bytes) of UDH inside UD */ |
| 521 | uint8_t udh_len = sms->user_data[0] + 1; |
| 522 | |
| 523 | /* copy over the UDH */ |
| 524 | memcpy(dst, sms->user_data, udh_len); |
| 525 | dst += udh_len; |
| 526 | deliver.sm_length = udh_len; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 527 | } |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 528 | /* add decoded text */ |
| 529 | deliver.sm_length += gsm_7bit_expand((char *)dst, sms->user_data, sms->user_data_len, sms->ud_hdr_ind); |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 530 | } else { |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 531 | deliver.sm_length = sms->user_data_len; |
| 532 | memcpy(deliver.short_message, sms->user_data, deliver.sm_length); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 533 | deliver.sm_length = sms->user_data_len; |
| 534 | memcpy(deliver.short_message, sms->user_data, deliver.sm_length); |
| 535 | } |
| 536 | |
Holger Hans Peter Freyther | 019851a | 2015-02-08 09:21:04 +0100 | [diff] [blame] | 537 | if (esme->acl && esme->acl->osmocom_ext && conn->lchan) |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 538 | append_osmo_tlvs(&deliver.tlv, conn->lchan); |
| 539 | |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 540 | return smpp_tx_deliver(esme, &deliver); |
| 541 | } |
| 542 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 543 | static struct smsc *g_smsc; |
| 544 | |
Holger Hans Peter Freyther | 42cf2e0 | 2015-07-06 16:41:30 +0200 | [diff] [blame] | 545 | int smpp_route_smpp_first(struct gsm_sms *sms, struct gsm_subscriber_connection *conn) |
| 546 | { |
| 547 | return g_smsc->smpp_first; |
| 548 | } |
| 549 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 550 | int smpp_try_deliver(struct gsm_sms *sms, struct gsm_subscriber_connection *conn) |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 551 | { |
| 552 | struct osmo_esme *esme; |
| 553 | struct osmo_smpp_addr dst; |
| 554 | |
| 555 | memset(&dst, 0, sizeof(dst)); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 556 | dst.ton = sms->dst.ton; |
| 557 | dst.npi = sms->dst.npi; |
| 558 | memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr)); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 559 | |
| 560 | esme = smpp_route(g_smsc, &dst); |
| 561 | if (!esme) |
Pablo Neira Ayuso | 638ad06 | 2017-05-03 18:59:05 +0200 | [diff] [blame^] | 562 | return GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 563 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 564 | return deliver_to_esme(esme, sms, conn); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 567 | struct smsc *smsc_from_vty(struct vty *v) |
| 568 | { |
| 569 | /* FIXME: this is ugly */ |
| 570 | return g_smsc; |
| 571 | } |
| 572 | |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 573 | /*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */ |
| 574 | int smpp_openbsc_alloc_init(void *ctx) |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 575 | { |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 576 | g_smsc = smpp_smsc_alloc_init(ctx); |
| 577 | if (!g_smsc) { |
| 578 | LOGP(DSMPP, LOGL_FATAL, "Cannot allocate smsc struct\n"); |
| 579 | return -1; |
| 580 | } |
| 581 | return smpp_vty_init(); |
| 582 | } |
| 583 | |
| 584 | /*! \brief Launch the OpenBSC SMPP interface with the parameters set from VTY. |
| 585 | */ |
| 586 | int smpp_openbsc_start(struct gsm_network *net) |
| 587 | { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 588 | int rc; |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 589 | g_smsc->priv = net; |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 590 | |
| 591 | /* If a VTY configuration has taken place, the values have been stored |
| 592 | * in the smsc struct. Otherwise, use the defaults (NULL -> any, 0 -> |
| 593 | * default SMPP port, see smpp_smsc_bind()). */ |
| 594 | rc = smpp_smsc_start(g_smsc, g_smsc->bind_addr, g_smsc->listen_port); |
| 595 | if (rc < 0) |
| 596 | return rc; |
| 597 | |
| 598 | rc = osmo_signal_register_handler(SS_SMS, smpp_sms_cb, g_smsc); |
| 599 | if (rc < 0) |
| 600 | return rc; |
| 601 | rc = osmo_signal_register_handler(SS_SUBSCR, smpp_subscr_cb, g_smsc); |
| 602 | if (rc < 0) |
| 603 | return rc; |
| 604 | |
| 605 | return 0; |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 606 | } |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 607 | |