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> |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 47 | #include <openbsc/chan_alloc.h> |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 48 | #include <openbsc/vlr.h> |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 49 | |
| 50 | #include "smpp_smsc.h" |
| 51 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 52 | /*! \brief find vlr_subscr for a given SMPP NPI/TON/Address */ |
| 53 | static struct vlr_subscr *subscr_by_dst(struct gsm_network *net, |
| 54 | uint8_t npi, uint8_t ton, |
| 55 | const char *addr) |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 56 | { |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 57 | struct vlr_subscr *vsub = NULL; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 58 | |
| 59 | switch (npi) { |
| 60 | case NPI_Land_Mobile_E212: |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 61 | vsub = vlr_subscr_find_by_imsi(net->vlr, addr); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 62 | break; |
| 63 | case NPI_ISDN_E163_E164: |
| 64 | case NPI_Private: |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 65 | vsub = vlr_subscr_find_by_msisdn(net->vlr, addr); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 66 | break; |
| 67 | default: |
| 68 | LOGP(DSMPP, LOGL_NOTICE, "Unsupported NPI: %u\n", npi); |
| 69 | break; |
| 70 | } |
| 71 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 72 | log_set_context(LOG_CTX_VLR_SUBSCR, vsub); |
| 73 | return vsub; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 76 | /*! \brief find a TLV with given tag in list of libsmpp34 TLVs */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 77 | 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] | 78 | { |
| 79 | struct tlv_t *t; |
| 80 | |
| 81 | for (t = head; t != NULL; t = t->next) { |
| 82 | if (t->tag == tag) |
| 83 | return t; |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 88 | /*! \brief convert from submit_sm_t to gsm_sms */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 89 | static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net, |
| 90 | const struct submit_sm_t *submit) |
| 91 | { |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 92 | struct vlr_subscr *dest; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 93 | struct gsm_sms *sms; |
| 94 | struct tlv_t *t; |
| 95 | const uint8_t *sms_msg; |
| 96 | unsigned int sms_msg_len; |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 97 | int mode; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 98 | |
| 99 | dest = subscr_by_dst(net, submit->dest_addr_npi, |
| 100 | submit->dest_addr_ton, |
| 101 | (const char *)submit->destination_addr); |
| 102 | if (!dest) { |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 103 | LOGP(DLSMS, LOGL_NOTICE, "SMPP SUBMIT-SM for unknown subscriber: " |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 104 | "%s (NPI=%u)\n", submit->destination_addr, |
| 105 | submit->dest_addr_npi); |
| 106 | return ESME_RINVDSTADR; |
| 107 | } |
| 108 | |
| 109 | t = find_tlv(submit->tlv, TLVID_message_payload); |
| 110 | if (t) { |
| 111 | if (submit->sm_length) { |
Holger Hans Peter Freyther | 94f83e1 | 2014-07-19 19:02:46 +0200 | [diff] [blame] | 112 | /* ERROR: we cannot have both! */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 113 | LOGP(DLSMS, LOGL_ERROR, "SMPP Cannot have payload in " |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 114 | "TLV _and_ in the header\n"); |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 115 | vlr_subscr_put(dest); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 116 | return ESME_ROPTPARNOTALLWD; |
| 117 | } |
| 118 | sms_msg = t->value.octet; |
| 119 | sms_msg_len = t->length; |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 120 | } else if (submit->sm_length > 0 && submit->sm_length < 255) { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 121 | sms_msg = submit->short_message; |
| 122 | sms_msg_len = submit->sm_length; |
| 123 | } else { |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 124 | LOGP(DLSMS, LOGL_ERROR, |
| 125 | "SMPP neither message payload nor valid sm_length.\n"); |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 126 | vlr_subscr_put(dest); |
Holger Hans Peter Freyther | a0735ec | 2015-02-08 09:53:44 +0100 | [diff] [blame] | 127 | return ESME_RINVPARLEN; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | sms = sms_alloc(); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 131 | sms->source = SMS_SOURCE_SMPP; |
| 132 | sms->smpp.sequence_nr = submit->sequence_number; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 133 | |
| 134 | /* fill in the destination address */ |
Harald Welte | 1a2993a | 2012-11-09 19:48:48 +0100 | [diff] [blame] | 135 | sms->receiver = dest; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 136 | sms->dst.ton = submit->dest_addr_ton; |
| 137 | sms->dst.npi = submit->dest_addr_npi; |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 138 | osmo_strlcpy(sms->dst.addr, dest->msisdn, sizeof(sms->dst.addr)); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 139 | |
| 140 | /* fill in the source address */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 141 | sms->src.ton = submit->source_addr_ton; |
| 142 | sms->src.npi = submit->source_addr_npi; |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 143 | osmo_strlcpy(sms->src.addr, (char *)submit->source_addr, |
| 144 | sizeof(sms->src.addr)); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 145 | |
| 146 | if (submit->esm_class & 0x40) |
| 147 | sms->ud_hdr_ind = 1; |
| 148 | |
| 149 | if (submit->esm_class & 0x80) { |
| 150 | sms->reply_path_req = 1; |
| 151 | #warning Implement reply path |
| 152 | } |
| 153 | |
Harald Welte | 9ad0362 | 2012-11-08 22:15:04 +0100 | [diff] [blame] | 154 | if (submit->data_coding == 0x00 || /* SMSC default */ |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 155 | submit->data_coding == 0x01) { /* GSM default alphabet */ |
| 156 | sms->data_coding_scheme = GSM338_DCS_1111_7BIT; |
| 157 | mode = MODE_7BIT; |
| 158 | } else if ((submit->data_coding & 0xFC) == 0xF0) { /* 03.38 DCS default */ |
| 159 | /* pass DCS 1:1 through from SMPP to GSM */ |
| 160 | sms->data_coding_scheme = submit->data_coding; |
| 161 | mode = MODE_7BIT; |
| 162 | } else if (submit->data_coding == 0x02 || |
| 163 | submit->data_coding == 0x04) { |
| 164 | /* 8-bit binary */ |
| 165 | sms->data_coding_scheme = GSM338_DCS_1111_8BIT_DATA; |
| 166 | mode = MODE_8BIT; |
| 167 | } else if ((submit->data_coding & 0xFC) == 0xF4) { /* 03.38 DCS 8bit */ |
| 168 | /* pass DCS 1:1 through from SMPP to GSM */ |
| 169 | sms->data_coding_scheme = submit->data_coding; |
| 170 | mode = MODE_8BIT; |
Harald Welte | 7e40be3 | 2014-02-21 13:21:03 +0100 | [diff] [blame] | 171 | } else if (submit->data_coding == 0x08) { |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 172 | /* UCS-2 */ |
| 173 | sms->data_coding_scheme = (2 << 2); |
| 174 | mode = MODE_8BIT; |
| 175 | } else { |
| 176 | sms_free(sms); |
| 177 | LOGP(DLSMS, LOGL_ERROR, "SMPP Unknown Data Coding 0x%02x\n", |
| 178 | submit->data_coding); |
| 179 | return ESME_RUNKNOWNERR; |
| 180 | } |
| 181 | |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 182 | if (mode == MODE_7BIT) { |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 183 | uint8_t ud_len = 0, padbits = 0; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 184 | sms->data_coding_scheme = GSM338_DCS_1111_7BIT; |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 185 | if (sms->ud_hdr_ind) { |
| 186 | ud_len = *sms_msg + 1; |
| 187 | printf("copying %u bytes user data...\n", ud_len); |
| 188 | memcpy(sms->user_data, sms_msg, |
| 189 | OSMO_MIN(ud_len, sizeof(sms->user_data))); |
| 190 | sms_msg += ud_len; |
| 191 | sms_msg_len -= ud_len; |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 192 | padbits = 7 - (ud_len % 7); |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 193 | } |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 194 | gsm_septets2octets(sms->user_data+ud_len, sms_msg, |
| 195 | sms_msg_len, padbits); |
| 196 | sms->user_data_len = (ud_len*8 + padbits)/7 + sms_msg_len;/* SEPTETS */ |
| 197 | /* FIXME: sms->text */ |
Harald Welte | 27d5e65 | 2013-05-28 20:37:07 +0200 | [diff] [blame] | 198 | } else { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 199 | memcpy(sms->user_data, sms_msg, sms_msg_len); |
| 200 | sms->user_data_len = sms_msg_len; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | *psms = sms; |
| 204 | return ESME_ROK; |
| 205 | } |
| 206 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 207 | /*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 208 | int handle_smpp_submit(struct osmo_esme *esme, struct submit_sm_t *submit, |
| 209 | struct submit_sm_resp_t *submit_r) |
| 210 | { |
| 211 | struct gsm_sms *sms; |
Holger Hans Peter Freyther | b5a4edd | 2013-01-20 17:43:50 +0100 | [diff] [blame] | 212 | struct gsm_network *net = esme->smsc->priv; |
| 213 | struct sms_signal_data sig; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 214 | int rc = -1; |
| 215 | |
Holger Hans Peter Freyther | b5a4edd | 2013-01-20 17:43:50 +0100 | [diff] [blame] | 216 | rc = submit_to_sms(&sms, net, submit); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 217 | if (rc != ESME_ROK) { |
| 218 | submit_r->command_status = rc; |
| 219 | return 0; |
| 220 | } |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 221 | smpp_esme_get(esme); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 222 | sms->smpp.esme = esme; |
Harald Welte | 9ad0362 | 2012-11-08 22:15:04 +0100 | [diff] [blame] | 223 | sms->protocol_id = submit->protocol_id; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 224 | |
| 225 | switch (submit->esm_class & 3) { |
| 226 | case 0: /* default */ |
| 227 | case 1: /* datagram */ |
| 228 | case 3: /* store-and-forward */ |
| 229 | rc = db_sms_store(sms); |
Holger Hans Peter Freyther | 6a85c15 | 2013-01-20 17:21:31 +0100 | [diff] [blame] | 230 | sms_free(sms); |
| 231 | sms = NULL; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 232 | if (rc < 0) { |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 233 | LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to " |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 234 | "store SMS in database\n"); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 235 | submit_r->command_status = ESME_RSYSERR; |
| 236 | return 0; |
| 237 | } |
| 238 | strcpy((char *)submit_r->message_id, "msg_id_not_implemented"); |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 239 | 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] | 240 | |
| 241 | memset(&sig, 0, sizeof(sig)); |
| 242 | osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 243 | rc = 0; |
| 244 | break; |
| 245 | case 2: /* forward (i.e. transaction) mode */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 246 | LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in " |
Harald Welte | 9122c13 | 2012-11-09 19:43:50 +0100 | [diff] [blame] | 247 | "real time (Transaction/Forward mode)\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 248 | sms->smpp.transaction_mode = 1; |
| 249 | gsm411_send_sms_subscr(sms->receiver, sms); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 250 | rc = 1; /* don't send any response yet */ |
| 251 | break; |
| 252 | } |
| 253 | return rc; |
| 254 | } |
| 255 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 256 | static void alert_all_esme(struct smsc *smsc, struct vlr_subscr *vsub, |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 257 | uint8_t smpp_avail_status) |
| 258 | { |
| 259 | struct osmo_esme *esme; |
| 260 | |
| 261 | llist_for_each_entry(esme, &smsc->esme_list, list) { |
| 262 | /* we currently send an alert notification to each ESME that is |
| 263 | * connected, and do not require a (non-existant) delivery |
| 264 | * pending flag to be set before, FIXME: make this VTY |
| 265 | * configurable */ |
| 266 | if (esme->acl && esme->acl->deliver_src_imsi) { |
| 267 | smpp_tx_alert(esme, TON_Subscriber_Number, |
| 268 | NPI_Land_Mobile_E212, |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 269 | vsub->imsi, smpp_avail_status); |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 270 | } else { |
| 271 | smpp_tx_alert(esme, TON_Network_Specific, |
| 272 | NPI_ISDN_E163_E164, |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 273 | vsub->msisdn, smpp_avail_status); |
Harald Welte | 045f402 | 2013-07-30 23:45:01 +0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 279 | /*! \brief signal handler for status of attempted SMS deliveries */ |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 280 | static int smpp_sms_cb(unsigned int subsys, unsigned int signal, |
| 281 | void *handler_data, void *signal_data) |
| 282 | { |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 283 | struct sms_signal_data *sig_sms = signal_data; |
| 284 | struct gsm_sms *sms = sig_sms->sms; |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 285 | struct smsc *smsc = handler_data; |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 286 | int rc = 0; |
| 287 | |
| 288 | if (!sms) |
| 289 | return 0; |
| 290 | |
| 291 | if (sms->source != SMS_SOURCE_SMPP) |
| 292 | return 0; |
| 293 | |
| 294 | switch (signal) { |
Harald Welte | 1aeb2af | 2013-07-30 22:30:24 +0800 | [diff] [blame] | 295 | case S_SMS_MEM_EXCEEDED: |
| 296 | /* fall-through: There is no ESME_Rxxx result code to |
| 297 | * indicate a MEMORY EXCEEDED in transaction mode back |
| 298 | * to the ESME */ |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 299 | case S_SMS_UNKNOWN_ERROR: |
| 300 | if (sms->smpp.transaction_mode) { |
| 301 | /* Send back the SUBMIT-SM response with apropriate error */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 302 | LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 303 | rc = smpp_tx_submit_r(sms->smpp.esme, |
| 304 | sms->smpp.sequence_nr, |
| 305 | ESME_RDELIVERYFAILURE, |
| 306 | sms->smpp.msg_id); |
| 307 | } |
| 308 | break; |
| 309 | case S_SMS_DELIVERED: |
| 310 | /* SMS layer tells us the delivery has been completed */ |
| 311 | if (sms->smpp.transaction_mode) { |
| 312 | /* Send back the SUBMIT-SM response */ |
Harald Welte | 0d0c9ec | 2012-11-24 11:13:19 +0100 | [diff] [blame] | 313 | LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n"); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 314 | rc = smpp_tx_submit_r(sms->smpp.esme, |
| 315 | sms->smpp.sequence_nr, |
| 316 | ESME_ROK, sms->smpp.msg_id); |
| 317 | } |
| 318 | break; |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 319 | case S_SMS_SMMA: |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 320 | if (!sig_sms->trans || !sig_sms->trans->vsub) { |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 321 | /* SMMA without a subscriber? strange... */ |
| 322 | LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n"); |
| 323 | break; |
| 324 | } |
| 325 | |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 326 | /* There's no real 1:1 match for SMMA in SMPP. However, |
| 327 | * an ALERT NOTIFICATION seems to be the most logical |
| 328 | * choice */ |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 329 | alert_all_esme(smsc, sig_sms->trans->vsub, 0); |
Harald Welte | 99e273d | 2013-07-30 23:39:18 +0800 | [diff] [blame] | 330 | break; |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return rc; |
| 334 | } |
| 335 | |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 336 | /*! \brief signal handler for subscriber related signals */ |
| 337 | static int smpp_subscr_cb(unsigned int subsys, unsigned int signal, |
| 338 | void *handler_data, void *signal_data) |
| 339 | { |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 340 | struct vlr_subscr *vsub = signal_data; |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 341 | struct smsc *smsc = handler_data; |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 342 | uint8_t smpp_avail_status; |
| 343 | |
| 344 | /* determine the smpp_avail_status depending on attach/detach */ |
| 345 | switch (signal) { |
| 346 | case S_SUBSCR_ATTACHED: |
| 347 | smpp_avail_status = 0; |
| 348 | break; |
| 349 | case S_SUBSCR_DETACHED: |
| 350 | smpp_avail_status = 2; |
| 351 | break; |
| 352 | default: |
| 353 | return 0; |
| 354 | } |
| 355 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 356 | alert_all_esme(smsc, vsub, smpp_avail_status); |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 361 | /* GSM 03.38 6.2.1 Character expanding (no decode!) */ |
| 362 | static int gsm_7bit_expand(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind) |
| 363 | { |
| 364 | int i = 0; |
| 365 | int shift = 0; |
| 366 | uint8_t c; |
| 367 | |
| 368 | /* skip the user data header */ |
| 369 | if (ud_hdr_ind) { |
| 370 | /* get user data header length + 1 (for the 'user data header length'-field) */ |
| 371 | shift = ((user_data[0] + 1) * 8) / 7; |
| 372 | if ((((user_data[0] + 1) * 8) % 7) != 0) |
| 373 | shift++; |
| 374 | septet_l = septet_l - shift; |
| 375 | } |
| 376 | |
| 377 | for (i = 0; i < septet_l; i++) { |
| 378 | c = |
| 379 | ((user_data[((i + shift) * 7 + 7) >> 3] << |
| 380 | (7 - (((i + shift) * 7 + 7) & 7))) | |
| 381 | (user_data[((i + shift) * 7) >> 3] >> |
| 382 | (((i + shift) * 7) & 7))) & 0x7f; |
| 383 | |
| 384 | *(text++) = c; |
| 385 | } |
| 386 | |
| 387 | *text = '\0'; |
| 388 | |
| 389 | return i; |
| 390 | } |
| 391 | |
| 392 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 393 | /* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */ |
| 394 | void append_tlv(tlv_t **req_tlv, uint16_t tag, |
| 395 | const uint8_t *data, uint16_t len) |
| 396 | { |
| 397 | tlv_t tlv; |
| 398 | |
| 399 | memset(&tlv, 0, sizeof(tlv)); |
| 400 | tlv.tag = tag; |
| 401 | tlv.length = len; |
| 402 | memcpy(tlv.value.octet, data, tlv.length); |
| 403 | build_tlv(req_tlv, &tlv); |
| 404 | } |
| 405 | void append_tlv_u8(tlv_t **req_tlv, uint16_t tag, uint8_t val) |
| 406 | { |
| 407 | tlv_t tlv; |
| 408 | |
| 409 | memset(&tlv, 0, sizeof(tlv)); |
| 410 | tlv.tag = tag; |
| 411 | tlv.length = 1; |
| 412 | tlv.value.val08 = val; |
| 413 | build_tlv(req_tlv, &tlv); |
| 414 | } |
| 415 | void append_tlv_u16(tlv_t **req_tlv, uint16_t tag, uint16_t val) |
| 416 | { |
| 417 | tlv_t tlv; |
| 418 | |
| 419 | memset(&tlv, 0, sizeof(tlv)); |
| 420 | tlv.tag = tag; |
| 421 | tlv.length = 2; |
| 422 | tlv.value.val16 = htons(val); |
| 423 | build_tlv(req_tlv, &tlv); |
| 424 | } |
| 425 | |
Neels Hofmeyr | ce67be1 | 2017-05-08 15:12:20 +0200 | [diff] [blame^] | 426 | #if BEFORE_MSCSPLIT |
| 427 | /* We currently have no lchan information. Re-add after A-interface, see OS#2390. */ |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 428 | /* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */ |
| 429 | static void append_osmo_tlvs(tlv_t **req_tlv, const struct gsm_lchan *lchan) |
| 430 | { |
| 431 | int idx = calc_initial_idx(ARRAY_SIZE(lchan->meas_rep), |
| 432 | lchan->meas_rep_idx, 1); |
| 433 | const struct gsm_meas_rep *mr = &lchan->meas_rep[idx]; |
| 434 | const struct gsm_meas_rep_unidir *ul_meas = &mr->ul; |
| 435 | const struct gsm_meas_rep_unidir *dl_meas = &mr->dl; |
| 436 | |
| 437 | /* Osmocom vendor-specific SMPP34 extensions */ |
| 438 | append_tlv_u16(req_tlv, TLVID_osmo_arfcn, lchan->ts->trx->arfcn); |
| 439 | if (mr->flags & MEAS_REP_F_MS_L1) { |
| 440 | uint8_t ms_dbm; |
| 441 | append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_l1.ta); |
| 442 | ms_dbm = ms_pwr_dbm(lchan->ts->trx->bts->band, mr->ms_l1.pwr); |
| 443 | append_tlv_u8(req_tlv, TLVID_osmo_ms_l1_txpwr, ms_dbm); |
Max | 11e4e41 | 2017-04-20 13:07:58 +0200 | [diff] [blame] | 444 | } else if (mr->flags & MEAS_REP_F_MS_TO) /* Save Timing Offset field = MS Timing Offset + 63 */ |
| 445 | 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] | 446 | |
| 447 | append_tlv_u16(req_tlv, TLVID_osmo_rxlev_ul, |
| 448 | rxlev2dbm(ul_meas->full.rx_lev)); |
| 449 | append_tlv_u8(req_tlv, TLVID_osmo_rxqual_ul, ul_meas->full.rx_qual); |
| 450 | |
| 451 | if (mr->flags & MEAS_REP_F_DL_VALID) { |
| 452 | append_tlv_u16(req_tlv, TLVID_osmo_rxlev_dl, |
| 453 | rxlev2dbm(dl_meas->full.rx_lev)); |
| 454 | append_tlv_u8(req_tlv, TLVID_osmo_rxqual_dl, |
| 455 | dl_meas->full.rx_qual); |
| 456 | } |
| 457 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 458 | if (lchan->conn && lchan->conn->vsub) { |
| 459 | struct vlr_subscr *vsub = lchan->conn->vsub; |
| 460 | size_t imei_len = strlen(vsub->imei); |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 461 | if (imei_len) |
| 462 | append_tlv(req_tlv, TLVID_osmo_imei, |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 463 | (uint8_t *)vsub->imei, imei_len+1); |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 464 | } |
| 465 | } |
Neels Hofmeyr | ce67be1 | 2017-05-08 15:12:20 +0200 | [diff] [blame^] | 466 | #endif |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 467 | |
Keith | 320960c | 2017-05-13 23:38:52 +0200 | [diff] [blame] | 468 | struct { |
| 469 | uint32_t smpp_status_code; |
| 470 | uint8_t gsm411_cause; |
| 471 | } smpp_to_gsm411_err_array[] = { |
| 472 | |
| 473 | /* Seems like most phones don't care about the failure cause, |
| 474 | * although some will display a different notification for |
| 475 | * GSM411_RP_CAUSE_MO_NUM_UNASSIGNED |
| 476 | * Some provoke a display of "Try again later" |
| 477 | * while others a more definitive "Message sending failed" |
| 478 | */ |
| 479 | |
| 480 | { ESME_RSYSERR, GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER }, |
| 481 | { ESME_RINVDSTADR, GSM411_RP_CAUSE_MO_NUM_UNASSIGNED }, |
| 482 | { ESME_RMSGQFUL, GSM411_RP_CAUSE_MO_CONGESTION }, |
| 483 | { ESME_RINVSRCADR, GSM411_RP_CAUSE_MO_SMS_REJECTED }, |
| 484 | { ESME_RINVMSGID, GSM411_RP_CAUSE_INV_TRANS_REF } |
| 485 | }; |
| 486 | |
| 487 | static int smpp_to_gsm411_err(uint32_t smpp_status_code, int *gsm411_cause) |
| 488 | { |
| 489 | int i; |
| 490 | for (i = 0; i < ARRAY_SIZE(smpp_to_gsm411_err_array); i++) { |
| 491 | if (smpp_to_gsm411_err_array[i].smpp_status_code != smpp_status_code) |
| 492 | continue; |
| 493 | *gsm411_cause = smpp_to_gsm411_err_array[i].gsm411_cause; |
| 494 | return 0; |
| 495 | } |
| 496 | return -1; |
| 497 | } |
| 498 | |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 499 | static void smpp_cmd_free(struct osmo_smpp_cmd *cmd) |
| 500 | { |
| 501 | osmo_timer_del(&cmd->response_timer); |
| 502 | llist_del(&cmd->list); |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 503 | vlr_subscr_put(cmd->vsub); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 504 | sms_free(cmd->sms); |
| 505 | talloc_free(cmd); |
| 506 | } |
| 507 | |
| 508 | void smpp_cmd_flush_pending(struct osmo_esme *esme) |
| 509 | { |
| 510 | struct osmo_smpp_cmd *cmd, *next; |
| 511 | |
| 512 | llist_for_each_entry_safe(cmd, next, &esme->smpp_cmd_list, list) |
| 513 | smpp_cmd_free(cmd); |
| 514 | } |
| 515 | |
| 516 | void smpp_cmd_ack(struct osmo_smpp_cmd *cmd) |
| 517 | { |
| 518 | struct gsm_subscriber_connection *conn; |
| 519 | struct gsm_trans *trans; |
| 520 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 521 | conn = connection_for_subscr(cmd->vsub); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 522 | if (!conn) { |
| 523 | LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | trans = trans_find_by_id(conn, GSM48_PDISC_SMS, |
| 528 | cmd->sms->gsm411.transaction_id); |
| 529 | if (!trans) { |
| 530 | LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n", |
| 531 | cmd->sms->gsm411.transaction_id); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | gsm411_send_rp_ack(trans, cmd->sms->gsm411.msg_ref); |
| 536 | smpp_cmd_free(cmd); |
| 537 | } |
| 538 | |
Keith | 320960c | 2017-05-13 23:38:52 +0200 | [diff] [blame] | 539 | void smpp_cmd_err(struct osmo_smpp_cmd *cmd, uint32_t status) |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 540 | { |
| 541 | struct gsm_subscriber_connection *conn; |
| 542 | struct gsm_trans *trans; |
Keith | 320960c | 2017-05-13 23:38:52 +0200 | [diff] [blame] | 543 | int gsm411_cause; |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 544 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 545 | conn = connection_for_subscr(cmd->vsub); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 546 | if (!conn) { |
| 547 | LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n"); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | trans = trans_find_by_id(conn, GSM48_PDISC_SMS, |
| 552 | cmd->sms->gsm411.transaction_id); |
| 553 | if (!trans) { |
| 554 | LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n", |
| 555 | cmd->sms->gsm411.transaction_id); |
| 556 | return; |
| 557 | } |
| 558 | |
Keith | 320960c | 2017-05-13 23:38:52 +0200 | [diff] [blame] | 559 | if (smpp_to_gsm411_err(status, &gsm411_cause) < 0) |
| 560 | gsm411_cause = GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
| 561 | |
| 562 | gsm411_send_rp_error(trans, cmd->sms->gsm411.msg_ref, gsm411_cause); |
| 563 | |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 564 | smpp_cmd_free(cmd); |
| 565 | } |
| 566 | |
| 567 | static void smpp_deliver_sm_cb(void *data) |
| 568 | { |
Keith | 320960c | 2017-05-13 23:38:52 +0200 | [diff] [blame] | 569 | smpp_cmd_err(data, ESME_RSYSERR); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static int smpp_cmd_enqueue(struct osmo_esme *esme, |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 573 | struct vlr_subscr *vsub, struct gsm_sms *sms, |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 574 | uint32_t sequence_number, bool *deferred) |
| 575 | { |
| 576 | struct osmo_smpp_cmd *cmd; |
| 577 | |
| 578 | cmd = talloc_zero(esme, struct osmo_smpp_cmd); |
| 579 | if (!cmd) |
| 580 | return -1; |
| 581 | |
| 582 | cmd->sequence_nr = sequence_number; |
| 583 | cmd->sms = sms; |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 584 | cmd->vsub = vlr_subscr_get(vsub); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 585 | |
| 586 | /* FIXME: No predefined value for this response_timer as specified by |
| 587 | * SMPP 3.4 specs, section 7.2. Make this configurable? Don't forget |
| 588 | * lchan keeps busy until we get a reply to this SMPP command. Too high |
| 589 | * value may exhaust resources. |
| 590 | */ |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 591 | osmo_timer_setup(&cmd->response_timer, smpp_deliver_sm_cb, cmd); |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 592 | osmo_timer_schedule(&cmd->response_timer, 5, 0); |
| 593 | llist_add_tail(&cmd->list, &esme->smpp_cmd_list); |
| 594 | *deferred = true; |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | struct osmo_smpp_cmd *smpp_cmd_find_by_seqnum(struct osmo_esme *esme, |
| 600 | uint32_t sequence_nr) |
| 601 | { |
| 602 | struct osmo_smpp_cmd *cmd; |
| 603 | |
| 604 | llist_for_each_entry(cmd, &esme->smpp_cmd_list, list) { |
| 605 | if (cmd->sequence_nr == sequence_nr) |
| 606 | return cmd; |
| 607 | } |
| 608 | return NULL; |
| 609 | } |
| 610 | |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 611 | static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms, |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 612 | struct gsm_subscriber_connection *conn, |
| 613 | bool *deferred) |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 614 | { |
| 615 | struct deliver_sm_t deliver; |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 616 | int mode, ret; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 617 | uint8_t dcs; |
| 618 | |
| 619 | memset(&deliver, 0, sizeof(deliver)); |
| 620 | deliver.command_length = 0; |
| 621 | deliver.command_id = DELIVER_SM; |
| 622 | deliver.command_status = ESME_ROK; |
| 623 | |
| 624 | strcpy((char *)deliver.service_type, "CMT"); |
| 625 | if (esme->acl && esme->acl->deliver_src_imsi) { |
| 626 | deliver.source_addr_ton = TON_Subscriber_Number; |
| 627 | deliver.source_addr_npi = NPI_Land_Mobile_E212; |
| 628 | snprintf((char *)deliver.source_addr, |
| 629 | sizeof(deliver.source_addr), "%s", |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 630 | conn->vsub->imsi); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 631 | } else { |
| 632 | deliver.source_addr_ton = TON_Network_Specific; |
| 633 | deliver.source_addr_npi = NPI_ISDN_E163_E164; |
| 634 | snprintf((char *)deliver.source_addr, |
| 635 | sizeof(deliver.source_addr), "%s", |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 636 | conn->vsub->msisdn); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 639 | deliver.dest_addr_ton = sms->dst.ton; |
| 640 | deliver.dest_addr_npi = sms->dst.npi; |
| 641 | memcpy(deliver.destination_addr, sms->dst.addr, |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 642 | sizeof(deliver.destination_addr)); |
| 643 | |
| 644 | deliver.esm_class = 1; /* datagram mode */ |
| 645 | if (sms->ud_hdr_ind) |
| 646 | deliver.esm_class |= 0x40; |
| 647 | if (sms->reply_path_req) |
| 648 | deliver.esm_class |= 0x80; |
| 649 | |
| 650 | deliver.protocol_id = sms->protocol_id; |
| 651 | deliver.priority_flag = 0; |
| 652 | deliver.registered_delivery = 0; |
| 653 | |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 654 | /* Figure out SMPP DCS from TP-DCS */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 655 | dcs = sms->data_coding_scheme; |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 656 | if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1) |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 657 | return -1; |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 658 | |
| 659 | /* Transparently pass on DCS via SMPP if requested */ |
Holger Hans Peter Freyther | 921b227 | 2013-07-14 08:54:07 +0200 | [diff] [blame] | 660 | if (esme->acl && esme->acl->dcs_transparent) |
Harald Welte | c75ed6d | 2013-05-28 20:58:02 +0200 | [diff] [blame] | 661 | deliver.data_coding = dcs; |
| 662 | |
| 663 | if (mode == MODE_7BIT) { |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 664 | uint8_t *dst = deliver.short_message; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 665 | |
| 666 | /* SMPP has this strange notion of putting 7bit SMS in |
| 667 | * an octet-aligned mode */ |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 668 | if (sms->ud_hdr_ind) { |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 669 | /* length (bytes) of UDH inside UD */ |
| 670 | uint8_t udh_len = sms->user_data[0] + 1; |
| 671 | |
| 672 | /* copy over the UDH */ |
| 673 | memcpy(dst, sms->user_data, udh_len); |
| 674 | dst += udh_len; |
| 675 | deliver.sm_length = udh_len; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 676 | } |
Harald Welte | b8a1f96 | 2012-11-24 01:37:39 +0100 | [diff] [blame] | 677 | /* add decoded text */ |
| 678 | 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] | 679 | } else { |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 680 | deliver.sm_length = sms->user_data_len; |
| 681 | memcpy(deliver.short_message, sms->user_data, deliver.sm_length); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 682 | deliver.sm_length = sms->user_data_len; |
| 683 | memcpy(deliver.short_message, sms->user_data, deliver.sm_length); |
| 684 | } |
| 685 | |
Neels Hofmeyr | ce67be1 | 2017-05-08 15:12:20 +0200 | [diff] [blame^] | 686 | #if BEFORE_MSCSPLIT |
| 687 | /* We currently have no lchan information. Re-add after A-interface, see OS#2390. */ |
Holger Hans Peter Freyther | 019851a | 2015-02-08 09:21:04 +0100 | [diff] [blame] | 688 | if (esme->acl && esme->acl->osmocom_ext && conn->lchan) |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 689 | append_osmo_tlvs(&deliver.tlv, conn->lchan); |
Neels Hofmeyr | ce67be1 | 2017-05-08 15:12:20 +0200 | [diff] [blame^] | 690 | #endif |
Harald Welte | 3f78600 | 2013-03-13 15:29:27 +0100 | [diff] [blame] | 691 | |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 692 | ret = smpp_tx_deliver(esme, &deliver); |
| 693 | if (ret < 0) |
| 694 | return ret; |
| 695 | |
Harald Welte | 9066b74 | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 696 | return smpp_cmd_enqueue(esme, conn->vsub, sms, |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 697 | deliver.sequence_number, deferred); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 700 | static struct smsc *g_smsc; |
| 701 | |
Holger Hans Peter Freyther | 42cf2e0 | 2015-07-06 16:41:30 +0200 | [diff] [blame] | 702 | int smpp_route_smpp_first(struct gsm_sms *sms, struct gsm_subscriber_connection *conn) |
| 703 | { |
| 704 | return g_smsc->smpp_first; |
| 705 | } |
| 706 | |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 707 | int smpp_try_deliver(struct gsm_sms *sms, |
| 708 | struct gsm_subscriber_connection *conn, bool *deferred) |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 709 | { |
| 710 | struct osmo_esme *esme; |
| 711 | struct osmo_smpp_addr dst; |
| 712 | |
| 713 | memset(&dst, 0, sizeof(dst)); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 714 | dst.ton = sms->dst.ton; |
| 715 | dst.npi = sms->dst.npi; |
| 716 | memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr)); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 717 | |
| 718 | esme = smpp_route(g_smsc, &dst); |
| 719 | if (!esme) |
Pablo Neira Ayuso | 638ad06 | 2017-05-03 18:59:05 +0200 | [diff] [blame] | 720 | return GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 721 | |
Pablo Neira Ayuso | 93ffbd0 | 2017-05-04 18:44:22 +0200 | [diff] [blame] | 722 | return deliver_to_esme(esme, sms, conn, deferred); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 725 | struct smsc *smsc_from_vty(struct vty *v) |
| 726 | { |
| 727 | /* FIXME: this is ugly */ |
| 728 | return g_smsc; |
| 729 | } |
| 730 | |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 731 | /*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */ |
| 732 | int smpp_openbsc_alloc_init(void *ctx) |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 733 | { |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 734 | g_smsc = smpp_smsc_alloc_init(ctx); |
| 735 | if (!g_smsc) { |
| 736 | LOGP(DSMPP, LOGL_FATAL, "Cannot allocate smsc struct\n"); |
| 737 | return -1; |
| 738 | } |
| 739 | return smpp_vty_init(); |
| 740 | } |
| 741 | |
| 742 | /*! \brief Launch the OpenBSC SMPP interface with the parameters set from VTY. |
| 743 | */ |
| 744 | int smpp_openbsc_start(struct gsm_network *net) |
| 745 | { |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 746 | int rc; |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 747 | g_smsc->priv = net; |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 748 | |
| 749 | /* If a VTY configuration has taken place, the values have been stored |
| 750 | * in the smsc struct. Otherwise, use the defaults (NULL -> any, 0 -> |
| 751 | * default SMPP port, see smpp_smsc_bind()). */ |
| 752 | rc = smpp_smsc_start(g_smsc, g_smsc->bind_addr, g_smsc->listen_port); |
| 753 | if (rc < 0) |
| 754 | return rc; |
| 755 | |
| 756 | rc = osmo_signal_register_handler(SS_SMS, smpp_sms_cb, g_smsc); |
| 757 | if (rc < 0) |
| 758 | return rc; |
| 759 | rc = osmo_signal_register_handler(SS_SUBSCR, smpp_subscr_cb, g_smsc); |
| 760 | if (rc < 0) |
| 761 | return rc; |
| 762 | |
| 763 | return 0; |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 764 | } |
Neels Hofmeyr | 1b0e554 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 765 | |