blob: 4ce6c27d95fd70c20b6b003b957cfeb775259b71 [file] [log] [blame]
Harald Weltef1033cc2012-11-08 16:14:37 +01001/* OpenBSC SMPP 3.4 interface, SMSC-side implementation */
2
Harald Weltee6f11602022-05-17 18:25:14 +02003/* (C) 2012-2022 by Harald Welte <laforge@gnumonks.org>
Harald Weltef1033cc2012-11-08 16:14:37 +01004 *
Harald Welte995ff352013-07-13 16:35:32 +02005 * All Rights Reserved
Harald Weltef1033cc2012-11-08 16:14:37 +01006 *
Harald Welte995ff352013-07-13 16:35:32 +02007 * 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 Weltef1033cc2012-11-08 16:14:37 +010011 *
Harald Welte995ff352013-07-13 16:35:32 +020012 * 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 Weltef1033cc2012-11-08 16:14:37 +010019 */
20
21
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdint.h>
26#include <errno.h>
Harald Weltee6f11602022-05-17 18:25:14 +020027#include <time.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010028
29#include <smpp34.h>
30#include <smpp34_structs.h>
31#include <smpp34_params.h>
Harald Weltee39f6cd2019-04-10 00:33:46 +020032#include <smpp34_heap.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010033
34#include <osmocom/core/utils.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/talloc.h>
38#include <osmocom/gsm/protocol/gsm_04_11.h>
Harald Welte3f786002013-03-13 15:29:27 +010039#include <osmocom/gsm/protocol/smpp34_osmocom.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010040
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/gsm_subscriber.h>
42#include <osmocom/msc/debug.h>
43#include <osmocom/msc/db.h>
44#include <osmocom/msc/gsm_04_11.h>
45#include <osmocom/msc/gsm_data.h>
46#include <osmocom/msc/signal.h>
47#include <osmocom/msc/transaction.h>
48#include <osmocom/msc/gsm_subscriber.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010050#include <osmocom/msc/msc_a.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010051
52#include "smpp_smsc.h"
53
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010054#define VSUB_USE_SMPP "SMPP"
55#define VSUB_USE_SMPP_CMD "SMPP-cmd"
56
Harald Weltee39f6cd2019-04-10 00:33:46 +020057/* talloc integration for libsmpp34 */
58
59static struct smsc *g_smsc;
60
61static void *smpp34_talloc_malloc(size_t sz)
62{
63 return talloc_size(g_smsc, sz);
64}
65
66static void *smpp34_talloc_realloc(void *ptr, size_t sz)
67{
68 return talloc_realloc_size(g_smsc, ptr, sz);
69}
70
71static void smpp34_talloc_free(void *ptr)
72{
73 talloc_free(ptr);
74}
75
76static const struct smpp34_memory_functions smpp34_talloc = {
77 .malloc_fun = smpp34_talloc_malloc,
78 .realloc_fun = smpp34_talloc_realloc,
79 .free_fun = smpp34_talloc_free,
80};
81
Harald Welte2483f1b2016-06-19 18:06:02 +020082/*! \brief find vlr_subscr for a given SMPP NPI/TON/Address */
83static struct vlr_subscr *subscr_by_dst(struct gsm_network *net,
84 uint8_t npi, uint8_t ton,
85 const char *addr)
Harald Weltef1033cc2012-11-08 16:14:37 +010086{
Harald Welte2483f1b2016-06-19 18:06:02 +020087 struct vlr_subscr *vsub = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +010088
89 switch (npi) {
90 case NPI_Land_Mobile_E212:
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010091 vsub = vlr_subscr_find_by_imsi(net->vlr, addr, VSUB_USE_SMPP);
Harald Weltef1033cc2012-11-08 16:14:37 +010092 break;
93 case NPI_ISDN_E163_E164:
94 case NPI_Private:
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010095 vsub = vlr_subscr_find_by_msisdn(net->vlr, addr, VSUB_USE_SMPP);
Harald Weltef1033cc2012-11-08 16:14:37 +010096 break;
97 default:
98 LOGP(DSMPP, LOGL_NOTICE, "Unsupported NPI: %u\n", npi);
99 break;
100 }
101
Harald Welte2483f1b2016-06-19 18:06:02 +0200102 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
103 return vsub;
Harald Weltef1033cc2012-11-08 16:14:37 +0100104}
105
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200106static int smpp34_submit_tlv_msg_payload(const struct tlv_t *t,
107 const struct submit_sm_t *submit,
108 const uint8_t **sms_msg,
109 unsigned int *sms_msg_len)
Harald Weltef1033cc2012-11-08 16:14:37 +0100110{
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200111 if (submit->sm_length) {
112 LOGP(DLSMS, LOGL_ERROR,
113 "SMPP cannot have payload in TLV _and_ in the header\n");
114 return -1;
Harald Weltef1033cc2012-11-08 16:14:37 +0100115 }
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200116 *sms_msg = t->value.octet;
117 *sms_msg_len = t->length;
118
119 return 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100120}
121
Harald Weltee94db492012-11-08 20:11:05 +0100122/*! \brief convert from submit_sm_t to gsm_sms */
Harald Weltef1033cc2012-11-08 16:14:37 +0100123static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net,
124 const struct submit_sm_t *submit)
125{
Harald Weltee6f11602022-05-17 18:25:14 +0200126 time_t t_now = time(NULL);
127 time_t t_validity_absolute;
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200128 const uint8_t *sms_msg = NULL;
Pau Espin Pedrol7e16e722017-08-14 21:18:41 +0200129 unsigned int sms_msg_len = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +0200130 struct vlr_subscr *dest;
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200131 uint16_t msg_ref = 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100132 struct gsm_sms *sms;
133 struct tlv_t *t;
Harald Welte27d5e652013-05-28 20:37:07 +0200134 int mode;
Stefan Sperling6d289812018-01-30 12:15:40 +0100135 int can_store_sms = ((submit->esm_class & SMPP34_MSG_MODE_MASK) != 2); /* != forward mode */
Harald Weltef1033cc2012-11-08 16:14:37 +0100136
137 dest = subscr_by_dst(net, submit->dest_addr_npi,
138 submit->dest_addr_ton,
139 (const char *)submit->destination_addr);
Stefan Sperling6d289812018-01-30 12:15:40 +0100140 if (!dest && !can_store_sms) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100141 LOGP(DLSMS, LOGL_NOTICE, "SMPP SUBMIT-SM for unknown subscriber: "
Harald Weltef1033cc2012-11-08 16:14:37 +0100142 "%s (NPI=%u)\n", submit->destination_addr,
143 submit->dest_addr_npi);
144 return ESME_RINVDSTADR;
145 }
146
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200147 smpp34_tlv_for_each(t, submit->tlv) {
148 switch (t->tag) {
149 case TLVID_message_payload:
150 if (smpp34_submit_tlv_msg_payload(t, submit, &sms_msg,
151 &sms_msg_len) < 0) {
Stefan Sperling6d289812018-01-30 12:15:40 +0100152 if (dest)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100153 vlr_subscr_put(dest, VSUB_USE_SMPP);
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200154 return ESME_ROPTPARNOTALLWD;
155 }
156 break;
157 case TLVID_user_message_reference:
Pau Espin Pedrold5c43392017-08-18 12:26:23 +0200158 msg_ref = t->value.val16;
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200159 break;
160 default:
161 break;
Harald Weltef1033cc2012-11-08 16:14:37 +0100162 }
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200163 }
164
165 if (!sms_msg) {
166 if (submit->sm_length > 0 && submit->sm_length < 255) {
167 sms_msg = submit->short_message;
168 sms_msg_len = submit->sm_length;
169 } else {
170 LOGP(DLSMS, LOGL_ERROR,
171 "SMPP neither message payload nor valid sm_length.\n");
Neels Hofmeyr07140022019-04-15 13:46:44 +0200172 if (dest)
173 vlr_subscr_put(dest, VSUB_USE_SMPP);
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200174 return ESME_RINVPARLEN;
175 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100176 }
177
178 sms = sms_alloc();
Harald Welted4bdee72012-11-08 19:44:08 +0100179 sms->source = SMS_SOURCE_SMPP;
180 sms->smpp.sequence_nr = submit->sequence_number;
Pablo Neira Ayuso791bdf72017-08-07 14:01:18 +0100181 sms->status_rep_req = submit->registered_delivery;
Pablo Neira Ayuso9a3ed852017-08-11 12:02:12 +0200182 sms->msg_ref = msg_ref;
Harald Weltec0de14d2012-11-23 23:35:01 +0100183
184 /* fill in the destination address */
Harald Welte1a2993a2012-11-09 19:48:48 +0100185 sms->receiver = dest;
Neels Hofmeyr07140022019-04-15 13:46:44 +0200186 if (dest) {
187 /* Replace use count from above subscr_by_dst (VSUB_USE_SMPP) by the sms->receiver use count
188 * (VSUB_USE_SMS_RECEIVER) */
189 vlr_subscr_get(sms->receiver, VSUB_USE_SMS_RECEIVER);
190 vlr_subscr_put(dest, VSUB_USE_SMPP);
191 }
Harald Weltec0de14d2012-11-23 23:35:01 +0100192 sms->dst.ton = submit->dest_addr_ton;
193 sms->dst.npi = submit->dest_addr_npi;
Stefan Sperling6d289812018-01-30 12:15:40 +0100194 if (dest)
Max98f74672018-02-05 12:57:06 +0100195 OSMO_STRLCPY_ARRAY(sms->dst.addr, dest->msisdn);
Stefan Sperling6d289812018-01-30 12:15:40 +0100196 else
Max98f74672018-02-05 12:57:06 +0100197 OSMO_STRLCPY_ARRAY(sms->dst.addr,
198 (const char *)submit->destination_addr);
Harald Weltec0de14d2012-11-23 23:35:01 +0100199
200 /* fill in the source address */
Harald Weltec0de14d2012-11-23 23:35:01 +0100201 sms->src.ton = submit->source_addr_ton;
202 sms->src.npi = submit->source_addr_npi;
Max98f74672018-02-05 12:57:06 +0100203 OSMO_STRLCPY_ARRAY(sms->src.addr, (char *)submit->source_addr);
Harald Weltef1033cc2012-11-08 16:14:37 +0100204
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200205 if (submit->esm_class == SMPP34_DELIVERY_ACK)
Pablo Neira Ayusobd71d322017-08-07 14:01:40 +0100206 sms->is_report = true;
207
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200208 if (submit->esm_class & SMPP34_UDHI_IND)
Harald Weltef1033cc2012-11-08 16:14:37 +0100209 sms->ud_hdr_ind = 1;
210
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200211 if (submit->esm_class & SMPP34_REPLY_PATH) {
Harald Weltef1033cc2012-11-08 16:14:37 +0100212 sms->reply_path_req = 1;
213#warning Implement reply path
214 }
215
Harald Welte9ad03622012-11-08 22:15:04 +0100216 if (submit->data_coding == 0x00 || /* SMSC default */
Harald Welte27d5e652013-05-28 20:37:07 +0200217 submit->data_coding == 0x01) { /* GSM default alphabet */
218 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
219 mode = MODE_7BIT;
220 } else if ((submit->data_coding & 0xFC) == 0xF0) { /* 03.38 DCS default */
221 /* pass DCS 1:1 through from SMPP to GSM */
222 sms->data_coding_scheme = submit->data_coding;
223 mode = MODE_7BIT;
224 } else if (submit->data_coding == 0x02 ||
225 submit->data_coding == 0x04) {
226 /* 8-bit binary */
227 sms->data_coding_scheme = GSM338_DCS_1111_8BIT_DATA;
228 mode = MODE_8BIT;
229 } else if ((submit->data_coding & 0xFC) == 0xF4) { /* 03.38 DCS 8bit */
230 /* pass DCS 1:1 through from SMPP to GSM */
231 sms->data_coding_scheme = submit->data_coding;
232 mode = MODE_8BIT;
Harald Welte7e40be32014-02-21 13:21:03 +0100233 } else if (submit->data_coding == 0x08) {
Harald Welte27d5e652013-05-28 20:37:07 +0200234 /* UCS-2 */
235 sms->data_coding_scheme = (2 << 2);
236 mode = MODE_8BIT;
237 } else {
238 sms_free(sms);
239 LOGP(DLSMS, LOGL_ERROR, "SMPP Unknown Data Coding 0x%02x\n",
240 submit->data_coding);
241 return ESME_RUNKNOWNERR;
242 }
243
Harald Weltec75ed6d2013-05-28 20:58:02 +0200244 if (mode == MODE_7BIT) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100245 uint8_t ud_len = 0, padbits = 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100246 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
Harald Welte9122c132012-11-09 19:43:50 +0100247 if (sms->ud_hdr_ind) {
248 ud_len = *sms_msg + 1;
249 printf("copying %u bytes user data...\n", ud_len);
250 memcpy(sms->user_data, sms_msg,
251 OSMO_MIN(ud_len, sizeof(sms->user_data)));
252 sms_msg += ud_len;
253 sms_msg_len -= ud_len;
Harald Welteb8a1f962012-11-24 01:37:39 +0100254 padbits = 7 - (ud_len % 7);
Harald Welte9122c132012-11-09 19:43:50 +0100255 }
Vadim Yanitskiy566ce112021-02-05 20:08:00 +0100256 gsm_septet_pack(sms->user_data+ud_len, sms_msg, sms_msg_len, padbits);
Harald Welteb8a1f962012-11-24 01:37:39 +0100257 sms->user_data_len = (ud_len*8 + padbits)/7 + sms_msg_len;/* SEPTETS */
258 /* FIXME: sms->text */
Harald Welte27d5e652013-05-28 20:37:07 +0200259 } else {
Harald Weltef1033cc2012-11-08 16:14:37 +0100260 memcpy(sms->user_data, sms_msg, sms_msg_len);
261 sms->user_data_len = sms_msg_len;
Harald Weltef1033cc2012-11-08 16:14:37 +0100262 }
263
Harald Weltee6f11602022-05-17 18:25:14 +0200264 t_validity_absolute = smpp_parse_time_format((const char *) submit->validity_period, &t_now);
265 if (!t_validity_absolute)
266 sms->validity_minutes = 7 * 24 * 60; /* default: 7 days */
267 else
268 sms->validity_minutes = (t_validity_absolute - t_now) / 60;
269
Harald Weltef1033cc2012-11-08 16:14:37 +0100270 *psms = sms;
271 return ESME_ROK;
272}
273
Harald Weltee94db492012-11-08 20:11:05 +0100274/*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100275int handle_smpp_submit(struct osmo_esme *esme, struct submit_sm_t *submit,
276 struct submit_sm_resp_t *submit_r)
277{
278 struct gsm_sms *sms;
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100279 struct gsm_network *net = esme->smsc->priv;
280 struct sms_signal_data sig;
Harald Weltef1033cc2012-11-08 16:14:37 +0100281 int rc = -1;
282
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100283 rc = submit_to_sms(&sms, net, submit);
Harald Weltef1033cc2012-11-08 16:14:37 +0100284 if (rc != ESME_ROK) {
285 submit_r->command_status = rc;
286 return 0;
287 }
Harald Weltee94db492012-11-08 20:11:05 +0100288 smpp_esme_get(esme);
Harald Welted4bdee72012-11-08 19:44:08 +0100289 sms->smpp.esme = esme;
Harald Welte9ad03622012-11-08 22:15:04 +0100290 sms->protocol_id = submit->protocol_id;
Harald Weltef1033cc2012-11-08 16:14:37 +0100291
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200292 switch (submit->esm_class & SMPP34_MSG_MODE_MASK) {
Harald Weltef1033cc2012-11-08 16:14:37 +0100293 case 0: /* default */
294 case 1: /* datagram */
295 case 3: /* store-and-forward */
296 rc = db_sms_store(sms);
Holger Hans Peter Freyther6a85c152013-01-20 17:21:31 +0100297 sms_free(sms);
298 sms = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +0100299 if (rc < 0) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100300 LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to "
Harald Weltef1033cc2012-11-08 16:14:37 +0100301 "store SMS in database\n");
Harald Weltef1033cc2012-11-08 16:14:37 +0100302 submit_r->command_status = ESME_RSYSERR;
303 return 0;
304 }
305 strcpy((char *)submit_r->message_id, "msg_id_not_implemented");
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100306 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Stored in DB\n");
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100307
308 memset(&sig, 0, sizeof(sig));
309 osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig);
Harald Weltef1033cc2012-11-08 16:14:37 +0100310 rc = 0;
311 break;
312 case 2: /* forward (i.e. transaction) mode */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100313 LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in "
Harald Welte9122c132012-11-09 19:43:50 +0100314 "real time (Transaction/Forward mode)\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100315 sms->smpp.transaction_mode = 1;
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700316 gsm411_send_sms(net, sms->receiver, sms);
Harald Weltef1033cc2012-11-08 16:14:37 +0100317 rc = 1; /* don't send any response yet */
318 break;
319 }
320 return rc;
321}
322
Harald Welte2483f1b2016-06-19 18:06:02 +0200323static void alert_all_esme(struct smsc *smsc, struct vlr_subscr *vsub,
Harald Welte045f4022013-07-30 23:45:01 +0800324 uint8_t smpp_avail_status)
325{
326 struct osmo_esme *esme;
327
328 llist_for_each_entry(esme, &smsc->esme_list, list) {
329 /* we currently send an alert notification to each ESME that is
Martin Hauke3f07dac2019-11-14 17:49:08 +0100330 * connected, and do not require a (non-existent) delivery
Keithc6d219c2019-01-17 01:01:59 +0100331 * pending flag to be set before. */
Keitha3a88212019-01-17 01:07:53 +0100332 if (!esme->bind_flags) {
333 LOGP(DSMPP, LOGL_DEBUG,
334 "ESME is not (yet) bound, skipping alert\n");
335 continue;
336 }
Alexander Couzens4aeb4ec2019-08-23 23:30:12 +0200337 if (esme->acl && !esme->acl->alert_notifications) {
Keithc6d219c2019-01-17 01:01:59 +0100338 LOGP(DSMPP, LOGL_DEBUG,
339 "[%s] is not set to receive Alert Notifications\n",
340 esme->system_id);
341 continue;
342 }
Harald Welte045f4022013-07-30 23:45:01 +0800343 if (esme->acl && esme->acl->deliver_src_imsi) {
344 smpp_tx_alert(esme, TON_Subscriber_Number,
345 NPI_Land_Mobile_E212,
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 vsub->imsi, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800347 } else {
348 smpp_tx_alert(esme, TON_Network_Specific,
349 NPI_ISDN_E163_E164,
Harald Welte2483f1b2016-06-19 18:06:02 +0200350 vsub->msisdn, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800351 }
352 }
353}
354
355
Harald Weltee94db492012-11-08 20:11:05 +0100356/*! \brief signal handler for status of attempted SMS deliveries */
Harald Welted4bdee72012-11-08 19:44:08 +0100357static int smpp_sms_cb(unsigned int subsys, unsigned int signal,
358 void *handler_data, void *signal_data)
359{
Harald Welted4bdee72012-11-08 19:44:08 +0100360 struct sms_signal_data *sig_sms = signal_data;
361 struct gsm_sms *sms = sig_sms->sms;
Harald Welte99e273d2013-07-30 23:39:18 +0800362 struct smsc *smsc = handler_data;
Harald Welted4bdee72012-11-08 19:44:08 +0100363 int rc = 0;
364
365 if (!sms)
366 return 0;
367
368 if (sms->source != SMS_SOURCE_SMPP)
369 return 0;
370
371 switch (signal) {
Harald Welte1aeb2af2013-07-30 22:30:24 +0800372 case S_SMS_MEM_EXCEEDED:
373 /* fall-through: There is no ESME_Rxxx result code to
374 * indicate a MEMORY EXCEEDED in transaction mode back
375 * to the ESME */
Harald Welted4bdee72012-11-08 19:44:08 +0100376 case S_SMS_UNKNOWN_ERROR:
377 if (sms->smpp.transaction_mode) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100378 /* Send back the SUBMIT-SM response with appropriate error */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100379 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100380 rc = smpp_tx_submit_r(sms->smpp.esme,
381 sms->smpp.sequence_nr,
382 ESME_RDELIVERYFAILURE,
383 sms->smpp.msg_id);
384 }
385 break;
386 case S_SMS_DELIVERED:
387 /* SMS layer tells us the delivery has been completed */
388 if (sms->smpp.transaction_mode) {
389 /* Send back the SUBMIT-SM response */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100390 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100391 rc = smpp_tx_submit_r(sms->smpp.esme,
392 sms->smpp.sequence_nr,
393 ESME_ROK, sms->smpp.msg_id);
394 }
395 break;
Harald Welte99e273d2013-07-30 23:39:18 +0800396 case S_SMS_SMMA:
Harald Welte2483f1b2016-06-19 18:06:02 +0200397 if (!sig_sms->trans || !sig_sms->trans->vsub) {
Harald Welte99e273d2013-07-30 23:39:18 +0800398 /* SMMA without a subscriber? strange... */
399 LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n");
400 break;
401 }
402
Harald Welte99e273d2013-07-30 23:39:18 +0800403 /* There's no real 1:1 match for SMMA in SMPP. However,
404 * an ALERT NOTIFICATION seems to be the most logical
405 * choice */
Harald Welte2483f1b2016-06-19 18:06:02 +0200406 alert_all_esme(smsc, sig_sms->trans->vsub, 0);
Harald Welte99e273d2013-07-30 23:39:18 +0800407 break;
Harald Welted4bdee72012-11-08 19:44:08 +0100408 }
409
410 return rc;
411}
412
Harald Welte8a1b0562012-11-09 12:51:44 +0100413/*! \brief signal handler for subscriber related signals */
414static int smpp_subscr_cb(unsigned int subsys, unsigned int signal,
415 void *handler_data, void *signal_data)
416{
Harald Welte2483f1b2016-06-19 18:06:02 +0200417 struct vlr_subscr *vsub = signal_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100418 struct smsc *smsc = handler_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100419 uint8_t smpp_avail_status;
420
421 /* determine the smpp_avail_status depending on attach/detach */
422 switch (signal) {
423 case S_SUBSCR_ATTACHED:
424 smpp_avail_status = 0;
425 break;
426 case S_SUBSCR_DETACHED:
427 smpp_avail_status = 2;
428 break;
429 default:
430 return 0;
431 }
432
Harald Welte2483f1b2016-06-19 18:06:02 +0200433 alert_all_esme(smsc, vsub, smpp_avail_status);
Harald Welte8a1b0562012-11-09 12:51:44 +0100434
435 return 0;
436}
437
Harald Welteb8a1f962012-11-24 01:37:39 +0100438/* GSM 03.38 6.2.1 Character expanding (no decode!) */
439static int gsm_7bit_expand(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind)
440{
441 int i = 0;
442 int shift = 0;
443 uint8_t c;
444
445 /* skip the user data header */
446 if (ud_hdr_ind) {
447 /* get user data header length + 1 (for the 'user data header length'-field) */
448 shift = ((user_data[0] + 1) * 8) / 7;
449 if ((((user_data[0] + 1) * 8) % 7) != 0)
450 shift++;
451 septet_l = septet_l - shift;
452 }
453
454 for (i = 0; i < septet_l; i++) {
455 c =
456 ((user_data[((i + shift) * 7 + 7) >> 3] <<
457 (7 - (((i + shift) * 7 + 7) & 7))) |
458 (user_data[((i + shift) * 7) >> 3] >>
459 (((i + shift) * 7) & 7))) & 0x7f;
460
461 *(text++) = c;
462 }
463
464 *text = '\0';
465
466 return i;
467}
468
469
Harald Welte3f786002013-03-13 15:29:27 +0100470/* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */
471void append_tlv(tlv_t **req_tlv, uint16_t tag,
472 const uint8_t *data, uint16_t len)
473{
474 tlv_t tlv;
475
476 memset(&tlv, 0, sizeof(tlv));
477 tlv.tag = tag;
478 tlv.length = len;
479 memcpy(tlv.value.octet, data, tlv.length);
480 build_tlv(req_tlv, &tlv);
481}
482void append_tlv_u8(tlv_t **req_tlv, uint16_t tag, uint8_t val)
483{
484 tlv_t tlv;
485
486 memset(&tlv, 0, sizeof(tlv));
487 tlv.tag = tag;
488 tlv.length = 1;
489 tlv.value.val08 = val;
490 build_tlv(req_tlv, &tlv);
491}
492void append_tlv_u16(tlv_t **req_tlv, uint16_t tag, uint16_t val)
493{
494 tlv_t tlv;
495
496 memset(&tlv, 0, sizeof(tlv));
497 tlv.tag = tag;
498 tlv.length = 2;
Pau Espin Pedrold5c43392017-08-18 12:26:23 +0200499 tlv.value.val16 = val;
Harald Welte3f786002013-03-13 15:29:27 +0100500 build_tlv(req_tlv, &tlv);
501}
502
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200503#if BEFORE_MSCSPLIT
504/* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Harald Welte3f786002013-03-13 15:29:27 +0100505/* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */
506static void append_osmo_tlvs(tlv_t **req_tlv, const struct gsm_lchan *lchan)
507{
508 int idx = calc_initial_idx(ARRAY_SIZE(lchan->meas_rep),
509 lchan->meas_rep_idx, 1);
510 const struct gsm_meas_rep *mr = &lchan->meas_rep[idx];
511 const struct gsm_meas_rep_unidir *ul_meas = &mr->ul;
512 const struct gsm_meas_rep_unidir *dl_meas = &mr->dl;
513
514 /* Osmocom vendor-specific SMPP34 extensions */
515 append_tlv_u16(req_tlv, TLVID_osmo_arfcn, lchan->ts->trx->arfcn);
516 if (mr->flags & MEAS_REP_F_MS_L1) {
517 uint8_t ms_dbm;
518 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_l1.ta);
519 ms_dbm = ms_pwr_dbm(lchan->ts->trx->bts->band, mr->ms_l1.pwr);
520 append_tlv_u8(req_tlv, TLVID_osmo_ms_l1_txpwr, ms_dbm);
Max11e4e412017-04-20 13:07:58 +0200521 } else if (mr->flags & MEAS_REP_F_MS_TO) /* Save Timing Offset field = MS Timing Offset + 63 */
522 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_timing_offset + 63);
Harald Welte3f786002013-03-13 15:29:27 +0100523
524 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_ul,
525 rxlev2dbm(ul_meas->full.rx_lev));
526 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_ul, ul_meas->full.rx_qual);
527
528 if (mr->flags & MEAS_REP_F_DL_VALID) {
529 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_dl,
530 rxlev2dbm(dl_meas->full.rx_lev));
531 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_dl,
532 dl_meas->full.rx_qual);
533 }
534
Harald Welte2483f1b2016-06-19 18:06:02 +0200535 if (lchan->conn && lchan->conn->vsub) {
536 struct vlr_subscr *vsub = lchan->conn->vsub;
537 size_t imei_len = strlen(vsub->imei);
Harald Welte3f786002013-03-13 15:29:27 +0100538 if (imei_len)
539 append_tlv(req_tlv, TLVID_osmo_imei,
Harald Welte2483f1b2016-06-19 18:06:02 +0200540 (uint8_t *)vsub->imei, imei_len+1);
Harald Welte3f786002013-03-13 15:29:27 +0100541 }
542}
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200543#endif
Harald Welte3f786002013-03-13 15:29:27 +0100544
Keith320960c2017-05-13 23:38:52 +0200545struct {
546 uint32_t smpp_status_code;
547 uint8_t gsm411_cause;
548} smpp_to_gsm411_err_array[] = {
549
550 /* Seems like most phones don't care about the failure cause,
551 * although some will display a different notification for
552 * GSM411_RP_CAUSE_MO_NUM_UNASSIGNED
553 * Some provoke a display of "Try again later"
554 * while others a more definitive "Message sending failed"
555 */
556
557 { ESME_RSYSERR, GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER },
558 { ESME_RINVDSTADR, GSM411_RP_CAUSE_MO_NUM_UNASSIGNED },
559 { ESME_RMSGQFUL, GSM411_RP_CAUSE_MO_CONGESTION },
560 { ESME_RINVSRCADR, GSM411_RP_CAUSE_MO_SMS_REJECTED },
561 { ESME_RINVMSGID, GSM411_RP_CAUSE_INV_TRANS_REF }
562};
563
564static int smpp_to_gsm411_err(uint32_t smpp_status_code, int *gsm411_cause)
565{
566 int i;
567 for (i = 0; i < ARRAY_SIZE(smpp_to_gsm411_err_array); i++) {
568 if (smpp_to_gsm411_err_array[i].smpp_status_code != smpp_status_code)
569 continue;
570 *gsm411_cause = smpp_to_gsm411_err_array[i].gsm411_cause;
571 return 0;
572 }
573 return -1;
574}
575
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200576static void smpp_cmd_free(struct osmo_smpp_cmd *cmd)
577{
578 osmo_timer_del(&cmd->response_timer);
579 llist_del(&cmd->list);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100580 vlr_subscr_put(cmd->vsub, VSUB_USE_SMPP_CMD);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200581 talloc_free(cmd);
582}
583
584void smpp_cmd_flush_pending(struct osmo_esme *esme)
585{
586 struct osmo_smpp_cmd *cmd, *next;
587
588 llist_for_each_entry_safe(cmd, next, &esme->smpp_cmd_list, list)
589 smpp_cmd_free(cmd);
590}
591
592void smpp_cmd_ack(struct osmo_smpp_cmd *cmd)
593{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100594 struct msc_a *msc_a;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200595 struct gsm_trans *trans;
596
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100597 if (cmd->is_report)
598 goto out;
599
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100600 msc_a = msc_a_for_vsub(cmd->vsub, true);
601 if (!msc_a) {
602 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber %s\n", vlr_subscr_name(cmd->vsub));
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100603 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200604 }
605
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100606 trans = trans_find_by_id(msc_a, TRANS_SMS, cmd->gsm411_trans_id);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200607 if (!trans) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100608 LOG_MSC_A_CAT(msc_a, DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n", cmd->gsm411_trans_id);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100609 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200610 }
611
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100612 gsm411_send_rp_ack(trans, cmd->gsm411_msg_ref);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100613out:
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200614 smpp_cmd_free(cmd);
615}
616
Keith320960c2017-05-13 23:38:52 +0200617void smpp_cmd_err(struct osmo_smpp_cmd *cmd, uint32_t status)
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200618{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100619 struct msc_a *msc_a;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200620 struct gsm_trans *trans;
Keith320960c2017-05-13 23:38:52 +0200621 int gsm411_cause;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200622
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100623 if (cmd->is_report)
624 goto out;
625
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100626 msc_a = msc_a_for_vsub(cmd->vsub, true);
627 if (!msc_a) {
628 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber %s\n", vlr_subscr_name(cmd->vsub));
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100629 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200630 }
631
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100632 trans = trans_find_by_id(msc_a, TRANS_SMS, cmd->gsm411_trans_id);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200633 if (!trans) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100634 LOG_MSC_A_CAT(msc_a, DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n",
635 cmd->gsm411_trans_id);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100636 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200637 }
638
Keith320960c2017-05-13 23:38:52 +0200639 if (smpp_to_gsm411_err(status, &gsm411_cause) < 0)
640 gsm411_cause = GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
641
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100642 gsm411_send_rp_error(trans, cmd->gsm411_msg_ref, gsm411_cause);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100643out:
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200644 smpp_cmd_free(cmd);
645}
646
647static void smpp_deliver_sm_cb(void *data)
648{
Keith320960c2017-05-13 23:38:52 +0200649 smpp_cmd_err(data, ESME_RSYSERR);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200650}
651
652static int smpp_cmd_enqueue(struct osmo_esme *esme,
Harald Welte2483f1b2016-06-19 18:06:02 +0200653 struct vlr_subscr *vsub, struct gsm_sms *sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100654 uint32_t sequence_number)
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200655{
656 struct osmo_smpp_cmd *cmd;
657
658 cmd = talloc_zero(esme, struct osmo_smpp_cmd);
659 if (!cmd)
660 return -1;
661
662 cmd->sequence_nr = sequence_number;
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100663 cmd->is_report = sms->is_report;
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100664 cmd->gsm411_msg_ref = sms->gsm411.msg_ref;
665 cmd->gsm411_trans_id = sms->gsm411.transaction_id;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100666 vlr_subscr_get(vsub, VSUB_USE_SMPP_CMD);
667 cmd->vsub = vsub;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200668
669 /* FIXME: No predefined value for this response_timer as specified by
670 * SMPP 3.4 specs, section 7.2. Make this configurable? Don't forget
671 * lchan keeps busy until we get a reply to this SMPP command. Too high
672 * value may exhaust resources.
673 */
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200674 osmo_timer_setup(&cmd->response_timer, smpp_deliver_sm_cb, cmd);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200675 osmo_timer_schedule(&cmd->response_timer, 5, 0);
676 llist_add_tail(&cmd->list, &esme->smpp_cmd_list);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200677
678 return 0;
679}
680
681struct osmo_smpp_cmd *smpp_cmd_find_by_seqnum(struct osmo_esme *esme,
682 uint32_t sequence_nr)
683{
684 struct osmo_smpp_cmd *cmd;
685
686 llist_for_each_entry(cmd, &esme->smpp_cmd_list, list) {
687 if (cmd->sequence_nr == sequence_nr)
688 return cmd;
689 }
690 return NULL;
691}
692
Harald Welte3f786002013-03-13 15:29:27 +0100693static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100694 struct msc_a *msc_a)
Harald Weltee07b6a72012-11-23 19:02:37 +0100695{
696 struct deliver_sm_t deliver;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200697 int mode, ret;
Harald Weltee07b6a72012-11-23 19:02:37 +0100698 uint8_t dcs;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100699 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Weltee07b6a72012-11-23 19:02:37 +0100700
701 memset(&deliver, 0, sizeof(deliver));
702 deliver.command_length = 0;
703 deliver.command_id = DELIVER_SM;
704 deliver.command_status = ESME_ROK;
705
706 strcpy((char *)deliver.service_type, "CMT");
707 if (esme->acl && esme->acl->deliver_src_imsi) {
708 deliver.source_addr_ton = TON_Subscriber_Number;
709 deliver.source_addr_npi = NPI_Land_Mobile_E212;
710 snprintf((char *)deliver.source_addr,
711 sizeof(deliver.source_addr), "%s",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100712 vsub->imsi);
Harald Weltee07b6a72012-11-23 19:02:37 +0100713 } else {
714 deliver.source_addr_ton = TON_Network_Specific;
715 deliver.source_addr_npi = NPI_ISDN_E163_E164;
716 snprintf((char *)deliver.source_addr,
717 sizeof(deliver.source_addr), "%s",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100718 vsub->msisdn);
Harald Weltee07b6a72012-11-23 19:02:37 +0100719 }
720
Harald Weltec0de14d2012-11-23 23:35:01 +0100721 deliver.dest_addr_ton = sms->dst.ton;
722 deliver.dest_addr_npi = sms->dst.npi;
723 memcpy(deliver.destination_addr, sms->dst.addr,
Harald Weltee07b6a72012-11-23 19:02:37 +0100724 sizeof(deliver.destination_addr));
725
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100726 if (sms->is_report)
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200727 deliver.esm_class = SMPP34_DELIVERY_RECEIPT;
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100728 else
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200729 deliver.esm_class = SMPP34_DATAGRAM_MODE;
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100730
Harald Weltee07b6a72012-11-23 19:02:37 +0100731 if (sms->ud_hdr_ind)
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200732 deliver.esm_class |= SMPP34_UDHI_IND;
Harald Weltee07b6a72012-11-23 19:02:37 +0100733 if (sms->reply_path_req)
Pablo Neira Ayusod7e14da2017-08-11 13:10:48 +0200734 deliver.esm_class |= SMPP34_REPLY_PATH;
Harald Weltee07b6a72012-11-23 19:02:37 +0100735
736 deliver.protocol_id = sms->protocol_id;
737 deliver.priority_flag = 0;
Pablo Neira Ayuso6ceac7c2017-08-07 14:01:15 +0100738 if (sms->status_rep_req)
Pablo Neira Ayuso4f03b472017-08-11 14:36:01 +0200739 deliver.registered_delivery = SMPP34_DELIVERY_RECEIPT_ON;
Harald Weltee07b6a72012-11-23 19:02:37 +0100740
Harald Weltec75ed6d2013-05-28 20:58:02 +0200741 /* Figure out SMPP DCS from TP-DCS */
Harald Weltee07b6a72012-11-23 19:02:37 +0100742 dcs = sms->data_coding_scheme;
Holger Hans Peter Freythera7328a52013-07-13 17:09:56 +0200743 if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200744 return -1;
Harald Weltec75ed6d2013-05-28 20:58:02 +0200745
746 /* Transparently pass on DCS via SMPP if requested */
Holger Hans Peter Freyther921b2272013-07-14 08:54:07 +0200747 if (esme->acl && esme->acl->dcs_transparent)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200748 deliver.data_coding = dcs;
749
750 if (mode == MODE_7BIT) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100751 uint8_t *dst = deliver.short_message;
Harald Weltee07b6a72012-11-23 19:02:37 +0100752
753 /* SMPP has this strange notion of putting 7bit SMS in
754 * an octet-aligned mode */
Harald Weltee07b6a72012-11-23 19:02:37 +0100755 if (sms->ud_hdr_ind) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100756 /* length (bytes) of UDH inside UD */
757 uint8_t udh_len = sms->user_data[0] + 1;
758
759 /* copy over the UDH */
760 memcpy(dst, sms->user_data, udh_len);
761 dst += udh_len;
762 deliver.sm_length = udh_len;
Harald Weltee07b6a72012-11-23 19:02:37 +0100763 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100764 /* add decoded text */
765 deliver.sm_length += gsm_7bit_expand((char *)dst, sms->user_data, sms->user_data_len, sms->ud_hdr_ind);
Harald Weltec75ed6d2013-05-28 20:58:02 +0200766 } else {
Harald Weltee07b6a72012-11-23 19:02:37 +0100767 deliver.sm_length = sms->user_data_len;
768 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
Harald Weltee07b6a72012-11-23 19:02:37 +0100769 }
770
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200771#if BEFORE_MSCSPLIT
772 /* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Holger Hans Peter Freyther019851a2015-02-08 09:21:04 +0100773 if (esme->acl && esme->acl->osmocom_ext && conn->lchan)
Harald Welte3f786002013-03-13 15:29:27 +0100774 append_osmo_tlvs(&deliver.tlv, conn->lchan);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200775#endif
Harald Welte3f786002013-03-13 15:29:27 +0100776
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100777 append_tlv_u16(&deliver.tlv, TLVID_user_message_reference,
778 sms->msg_ref);
779
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200780 ret = smpp_tx_deliver(esme, &deliver);
781 if (ret < 0)
782 return ret;
783
Pablo Neira Ayuso83616a82020-01-15 10:00:24 +0100784 OSMO_ASSERT(!sms->smpp.esme);
785 smpp_esme_get(esme);
786 sms->smpp.esme = esme;
787
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100788 return smpp_cmd_enqueue(esme, vsub, sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100789 deliver.sequence_number);
Harald Weltee07b6a72012-11-23 19:02:37 +0100790}
791
Harald Welte338e3b32012-11-20 22:22:04 +0100792static struct smsc *g_smsc;
793
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100794bool smpp_route_smpp_first()
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200795{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100796 return (bool)(g_smsc->smpp_first);
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200797}
798
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100799int smpp_try_deliver(struct gsm_sms *sms, struct msc_a *msc_a)
Harald Weltee07b6a72012-11-23 19:02:37 +0100800{
801 struct osmo_esme *esme;
802 struct osmo_smpp_addr dst;
Benoit Bolseed34ed572017-07-05 11:46:55 +0200803 int rc;
Harald Weltee07b6a72012-11-23 19:02:37 +0100804
805 memset(&dst, 0, sizeof(dst));
Harald Weltec0de14d2012-11-23 23:35:01 +0100806 dst.ton = sms->dst.ton;
807 dst.npi = sms->dst.npi;
808 memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr));
Harald Weltee07b6a72012-11-23 19:02:37 +0100809
Benoit Bolseed34ed572017-07-05 11:46:55 +0200810 rc = smpp_route(g_smsc, &dst, &esme);
811 if (!rc)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100812 rc = deliver_to_esme(esme, sms, msc_a);
Harald Weltee07b6a72012-11-23 19:02:37 +0100813
Benoit Bolseed34ed572017-07-05 11:46:55 +0200814 return rc;
Harald Weltee07b6a72012-11-23 19:02:37 +0100815}
816
Harald Welte338e3b32012-11-20 22:22:04 +0100817struct smsc *smsc_from_vty(struct vty *v)
818{
819 /* FIXME: this is ugly */
820 return g_smsc;
821}
822
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100823/*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */
824int smpp_openbsc_alloc_init(void *ctx)
Harald Weltef1033cc2012-11-08 16:14:37 +0100825{
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100826 g_smsc = smpp_smsc_alloc_init(ctx);
827 if (!g_smsc) {
828 LOGP(DSMPP, LOGL_FATAL, "Cannot allocate smsc struct\n");
829 return -1;
830 }
Harald Weltee39f6cd2019-04-10 00:33:46 +0200831 smpp34_set_memory_functions(&smpp34_talloc);
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100832 return smpp_vty_init();
833}
834
835/*! \brief Launch the OpenBSC SMPP interface with the parameters set from VTY.
836 */
837int smpp_openbsc_start(struct gsm_network *net)
838{
Harald Weltef1033cc2012-11-08 16:14:37 +0100839 int rc;
Harald Welte76afa162013-03-13 15:05:26 +0100840 g_smsc->priv = net;
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100841
842 /* If a VTY configuration has taken place, the values have been stored
843 * in the smsc struct. Otherwise, use the defaults (NULL -> any, 0 ->
844 * default SMPP port, see smpp_smsc_bind()). */
845 rc = smpp_smsc_start(g_smsc, g_smsc->bind_addr, g_smsc->listen_port);
846 if (rc < 0)
847 return rc;
848
849 rc = osmo_signal_register_handler(SS_SMS, smpp_sms_cb, g_smsc);
850 if (rc < 0)
851 return rc;
852 rc = osmo_signal_register_handler(SS_SUBSCR, smpp_subscr_cb, g_smsc);
853 if (rc < 0)
854 return rc;
855
856 return 0;
Harald Welte76afa162013-03-13 15:05:26 +0100857}
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100858