blob: 09012d0130e4142a0a6ac0b114268e8421d436f7 [file] [log] [blame]
Harald Weltef1033cc2012-11-08 16:14:37 +01001/* OpenBSC SMPP 3.4 interface, SMSC-side implementation */
2
Harald Welte3f786002013-03-13 15:29:27 +01003/* (C) 2012-2013 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>
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 Welte3f786002013-03-13 15:29:27 +010037#include <osmocom/gsm/protocol/smpp34_osmocom.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010038
39#include <openbsc/gsm_subscriber.h>
40#include <openbsc/debug.h>
41#include <openbsc/db.h>
42#include <openbsc/gsm_04_11.h>
Harald Welted4bdee72012-11-08 19:44:08 +010043#include <openbsc/gsm_data.h>
44#include <openbsc/signal.h>
Harald Welte99e273d2013-07-30 23:39:18 +080045#include <openbsc/transaction.h>
46#include <openbsc/gsm_subscriber.h>
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +020047#include <openbsc/chan_alloc.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020048#include <openbsc/vlr.h>
Harald Weltef1033cc2012-11-08 16:14:37 +010049
50#include "smpp_smsc.h"
51
Harald Welte2483f1b2016-06-19 18:06:02 +020052/*! \brief find vlr_subscr for a given SMPP NPI/TON/Address */
53static struct vlr_subscr *subscr_by_dst(struct gsm_network *net,
54 uint8_t npi, uint8_t ton,
55 const char *addr)
Harald Weltef1033cc2012-11-08 16:14:37 +010056{
Harald Welte2483f1b2016-06-19 18:06:02 +020057 struct vlr_subscr *vsub = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +010058
59 switch (npi) {
60 case NPI_Land_Mobile_E212:
Harald Welte2483f1b2016-06-19 18:06:02 +020061 vsub = vlr_subscr_find_by_imsi(net->vlr, addr);
Harald Weltef1033cc2012-11-08 16:14:37 +010062 break;
63 case NPI_ISDN_E163_E164:
64 case NPI_Private:
Harald Welte2483f1b2016-06-19 18:06:02 +020065 vsub = vlr_subscr_find_by_msisdn(net->vlr, addr);
Harald Weltef1033cc2012-11-08 16:14:37 +010066 break;
67 default:
68 LOGP(DSMPP, LOGL_NOTICE, "Unsupported NPI: %u\n", npi);
69 break;
70 }
71
Harald Welte2483f1b2016-06-19 18:06:02 +020072 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
73 return vsub;
Harald Weltef1033cc2012-11-08 16:14:37 +010074}
75
Harald Weltee94db492012-11-08 20:11:05 +010076/*! \brief find a TLV with given tag in list of libsmpp34 TLVs */
Harald Weltee07b6a72012-11-23 19:02:37 +010077static struct tlv_t *find_tlv(struct tlv_t *head, uint16_t tag)
Harald Weltef1033cc2012-11-08 16:14:37 +010078{
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 Weltee94db492012-11-08 20:11:05 +010088/*! \brief convert from submit_sm_t to gsm_sms */
Harald Weltef1033cc2012-11-08 16:14:37 +010089static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net,
90 const struct submit_sm_t *submit)
91{
Harald Welte2483f1b2016-06-19 18:06:02 +020092 struct vlr_subscr *dest;
Harald Weltef1033cc2012-11-08 16:14:37 +010093 struct gsm_sms *sms;
94 struct tlv_t *t;
95 const uint8_t *sms_msg;
96 unsigned int sms_msg_len;
Harald Welte27d5e652013-05-28 20:37:07 +020097 int mode;
Harald Weltef1033cc2012-11-08 16:14:37 +010098
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 Welte0d0c9ec2012-11-24 11:13:19 +0100103 LOGP(DLSMS, LOGL_NOTICE, "SMPP SUBMIT-SM for unknown subscriber: "
Harald Weltef1033cc2012-11-08 16:14:37 +0100104 "%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 Freyther94f83e12014-07-19 19:02:46 +0200112 /* ERROR: we cannot have both! */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100113 LOGP(DLSMS, LOGL_ERROR, "SMPP Cannot have payload in "
Harald Welte9122c132012-11-09 19:43:50 +0100114 "TLV _and_ in the header\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200115 vlr_subscr_put(dest);
Harald Weltef1033cc2012-11-08 16:14:37 +0100116 return ESME_ROPTPARNOTALLWD;
117 }
118 sms_msg = t->value.octet;
119 sms_msg_len = t->length;
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100120 } else if (submit->sm_length > 0 && submit->sm_length < 255) {
Harald Weltef1033cc2012-11-08 16:14:37 +0100121 sms_msg = submit->short_message;
122 sms_msg_len = submit->sm_length;
123 } else {
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100124 LOGP(DLSMS, LOGL_ERROR,
125 "SMPP neither message payload nor valid sm_length.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200126 vlr_subscr_put(dest);
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100127 return ESME_RINVPARLEN;
Harald Weltef1033cc2012-11-08 16:14:37 +0100128 }
129
130 sms = sms_alloc();
Harald Welted4bdee72012-11-08 19:44:08 +0100131 sms->source = SMS_SOURCE_SMPP;
132 sms->smpp.sequence_nr = submit->sequence_number;
Pablo Neira Ayuso791bdf72017-08-07 14:01:18 +0100133 sms->status_rep_req = submit->registered_delivery;
Harald Weltec0de14d2012-11-23 23:35:01 +0100134
135 /* fill in the destination address */
Harald Welte1a2993a2012-11-09 19:48:48 +0100136 sms->receiver = dest;
Harald Weltec0de14d2012-11-23 23:35:01 +0100137 sms->dst.ton = submit->dest_addr_ton;
138 sms->dst.npi = submit->dest_addr_npi;
Harald Welte2483f1b2016-06-19 18:06:02 +0200139 osmo_strlcpy(sms->dst.addr, dest->msisdn, sizeof(sms->dst.addr));
Harald Weltec0de14d2012-11-23 23:35:01 +0100140
141 /* fill in the source address */
Harald Weltec0de14d2012-11-23 23:35:01 +0100142 sms->src.ton = submit->source_addr_ton;
143 sms->src.npi = submit->source_addr_npi;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100144 osmo_strlcpy(sms->src.addr, (char *)submit->source_addr,
145 sizeof(sms->src.addr));
Harald Weltef1033cc2012-11-08 16:14:37 +0100146
Pablo Neira Ayusobd71d322017-08-07 14:01:40 +0100147 /* This is a Delivery Acknowledgment. */
148 if (submit->esm_class == 0x08)
149 sms->is_report = true;
150
Harald Weltef1033cc2012-11-08 16:14:37 +0100151 if (submit->esm_class & 0x40)
152 sms->ud_hdr_ind = 1;
153
154 if (submit->esm_class & 0x80) {
155 sms->reply_path_req = 1;
156#warning Implement reply path
157 }
158
Harald Welte9ad03622012-11-08 22:15:04 +0100159 if (submit->data_coding == 0x00 || /* SMSC default */
Harald Welte27d5e652013-05-28 20:37:07 +0200160 submit->data_coding == 0x01) { /* GSM default alphabet */
161 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
162 mode = MODE_7BIT;
163 } else if ((submit->data_coding & 0xFC) == 0xF0) { /* 03.38 DCS default */
164 /* pass DCS 1:1 through from SMPP to GSM */
165 sms->data_coding_scheme = submit->data_coding;
166 mode = MODE_7BIT;
167 } else if (submit->data_coding == 0x02 ||
168 submit->data_coding == 0x04) {
169 /* 8-bit binary */
170 sms->data_coding_scheme = GSM338_DCS_1111_8BIT_DATA;
171 mode = MODE_8BIT;
172 } else if ((submit->data_coding & 0xFC) == 0xF4) { /* 03.38 DCS 8bit */
173 /* pass DCS 1:1 through from SMPP to GSM */
174 sms->data_coding_scheme = submit->data_coding;
175 mode = MODE_8BIT;
Harald Welte7e40be32014-02-21 13:21:03 +0100176 } else if (submit->data_coding == 0x08) {
Harald Welte27d5e652013-05-28 20:37:07 +0200177 /* UCS-2 */
178 sms->data_coding_scheme = (2 << 2);
179 mode = MODE_8BIT;
180 } else {
181 sms_free(sms);
182 LOGP(DLSMS, LOGL_ERROR, "SMPP Unknown Data Coding 0x%02x\n",
183 submit->data_coding);
184 return ESME_RUNKNOWNERR;
185 }
186
Harald Weltec75ed6d2013-05-28 20:58:02 +0200187 if (mode == MODE_7BIT) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100188 uint8_t ud_len = 0, padbits = 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100189 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
Harald Welte9122c132012-11-09 19:43:50 +0100190 if (sms->ud_hdr_ind) {
191 ud_len = *sms_msg + 1;
192 printf("copying %u bytes user data...\n", ud_len);
193 memcpy(sms->user_data, sms_msg,
194 OSMO_MIN(ud_len, sizeof(sms->user_data)));
195 sms_msg += ud_len;
196 sms_msg_len -= ud_len;
Harald Welteb8a1f962012-11-24 01:37:39 +0100197 padbits = 7 - (ud_len % 7);
Harald Welte9122c132012-11-09 19:43:50 +0100198 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100199 gsm_septets2octets(sms->user_data+ud_len, sms_msg,
200 sms_msg_len, padbits);
201 sms->user_data_len = (ud_len*8 + padbits)/7 + sms_msg_len;/* SEPTETS */
202 /* FIXME: sms->text */
Harald Welte27d5e652013-05-28 20:37:07 +0200203 } else {
Harald Weltef1033cc2012-11-08 16:14:37 +0100204 memcpy(sms->user_data, sms_msg, sms_msg_len);
205 sms->user_data_len = sms_msg_len;
Harald Weltef1033cc2012-11-08 16:14:37 +0100206 }
207
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100208 t = find_tlv(submit->tlv, TLVID_user_message_reference);
209 if (t)
210 sms->msg_ref = ntohs(t->value.val16);
211
Harald Weltef1033cc2012-11-08 16:14:37 +0100212 *psms = sms;
213 return ESME_ROK;
214}
215
Harald Weltee94db492012-11-08 20:11:05 +0100216/*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100217int handle_smpp_submit(struct osmo_esme *esme, struct submit_sm_t *submit,
218 struct submit_sm_resp_t *submit_r)
219{
220 struct gsm_sms *sms;
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100221 struct gsm_network *net = esme->smsc->priv;
222 struct sms_signal_data sig;
Harald Weltef1033cc2012-11-08 16:14:37 +0100223 int rc = -1;
224
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100225 rc = submit_to_sms(&sms, net, submit);
Harald Weltef1033cc2012-11-08 16:14:37 +0100226 if (rc != ESME_ROK) {
227 submit_r->command_status = rc;
228 return 0;
229 }
Harald Weltee94db492012-11-08 20:11:05 +0100230 smpp_esme_get(esme);
Harald Welted4bdee72012-11-08 19:44:08 +0100231 sms->smpp.esme = esme;
Harald Welte9ad03622012-11-08 22:15:04 +0100232 sms->protocol_id = submit->protocol_id;
Harald Weltef1033cc2012-11-08 16:14:37 +0100233
234 switch (submit->esm_class & 3) {
235 case 0: /* default */
236 case 1: /* datagram */
237 case 3: /* store-and-forward */
238 rc = db_sms_store(sms);
Holger Hans Peter Freyther6a85c152013-01-20 17:21:31 +0100239 sms_free(sms);
240 sms = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +0100241 if (rc < 0) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100242 LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to "
Harald Weltef1033cc2012-11-08 16:14:37 +0100243 "store SMS in database\n");
Harald Weltef1033cc2012-11-08 16:14:37 +0100244 submit_r->command_status = ESME_RSYSERR;
245 return 0;
246 }
247 strcpy((char *)submit_r->message_id, "msg_id_not_implemented");
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100248 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Stored in DB\n");
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100249
250 memset(&sig, 0, sizeof(sig));
251 osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig);
Harald Weltef1033cc2012-11-08 16:14:37 +0100252 rc = 0;
253 break;
254 case 2: /* forward (i.e. transaction) mode */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100255 LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in "
Harald Welte9122c132012-11-09 19:43:50 +0100256 "real time (Transaction/Forward mode)\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100257 sms->smpp.transaction_mode = 1;
258 gsm411_send_sms_subscr(sms->receiver, sms);
Harald Weltef1033cc2012-11-08 16:14:37 +0100259 rc = 1; /* don't send any response yet */
260 break;
261 }
262 return rc;
263}
264
Harald Welte2483f1b2016-06-19 18:06:02 +0200265static void alert_all_esme(struct smsc *smsc, struct vlr_subscr *vsub,
Harald Welte045f4022013-07-30 23:45:01 +0800266 uint8_t smpp_avail_status)
267{
268 struct osmo_esme *esme;
269
270 llist_for_each_entry(esme, &smsc->esme_list, list) {
271 /* we currently send an alert notification to each ESME that is
272 * connected, and do not require a (non-existant) delivery
273 * pending flag to be set before, FIXME: make this VTY
274 * configurable */
275 if (esme->acl && esme->acl->deliver_src_imsi) {
276 smpp_tx_alert(esme, TON_Subscriber_Number,
277 NPI_Land_Mobile_E212,
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 vsub->imsi, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800279 } else {
280 smpp_tx_alert(esme, TON_Network_Specific,
281 NPI_ISDN_E163_E164,
Harald Welte2483f1b2016-06-19 18:06:02 +0200282 vsub->msisdn, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800283 }
284 }
285}
286
287
Harald Weltee94db492012-11-08 20:11:05 +0100288/*! \brief signal handler for status of attempted SMS deliveries */
Harald Welted4bdee72012-11-08 19:44:08 +0100289static int smpp_sms_cb(unsigned int subsys, unsigned int signal,
290 void *handler_data, void *signal_data)
291{
Harald Welted4bdee72012-11-08 19:44:08 +0100292 struct sms_signal_data *sig_sms = signal_data;
293 struct gsm_sms *sms = sig_sms->sms;
Harald Welte99e273d2013-07-30 23:39:18 +0800294 struct smsc *smsc = handler_data;
Harald Welted4bdee72012-11-08 19:44:08 +0100295 int rc = 0;
296
297 if (!sms)
298 return 0;
299
300 if (sms->source != SMS_SOURCE_SMPP)
301 return 0;
302
303 switch (signal) {
Harald Welte1aeb2af2013-07-30 22:30:24 +0800304 case S_SMS_MEM_EXCEEDED:
305 /* fall-through: There is no ESME_Rxxx result code to
306 * indicate a MEMORY EXCEEDED in transaction mode back
307 * to the ESME */
Harald Welted4bdee72012-11-08 19:44:08 +0100308 case S_SMS_UNKNOWN_ERROR:
309 if (sms->smpp.transaction_mode) {
310 /* Send back the SUBMIT-SM response with apropriate error */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100311 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100312 rc = smpp_tx_submit_r(sms->smpp.esme,
313 sms->smpp.sequence_nr,
314 ESME_RDELIVERYFAILURE,
315 sms->smpp.msg_id);
316 }
317 break;
318 case S_SMS_DELIVERED:
319 /* SMS layer tells us the delivery has been completed */
320 if (sms->smpp.transaction_mode) {
321 /* Send back the SUBMIT-SM response */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100322 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100323 rc = smpp_tx_submit_r(sms->smpp.esme,
324 sms->smpp.sequence_nr,
325 ESME_ROK, sms->smpp.msg_id);
326 }
327 break;
Harald Welte99e273d2013-07-30 23:39:18 +0800328 case S_SMS_SMMA:
Harald Welte2483f1b2016-06-19 18:06:02 +0200329 if (!sig_sms->trans || !sig_sms->trans->vsub) {
Harald Welte99e273d2013-07-30 23:39:18 +0800330 /* SMMA without a subscriber? strange... */
331 LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n");
332 break;
333 }
334
Harald Welte99e273d2013-07-30 23:39:18 +0800335 /* There's no real 1:1 match for SMMA in SMPP. However,
336 * an ALERT NOTIFICATION seems to be the most logical
337 * choice */
Harald Welte2483f1b2016-06-19 18:06:02 +0200338 alert_all_esme(smsc, sig_sms->trans->vsub, 0);
Harald Welte99e273d2013-07-30 23:39:18 +0800339 break;
Harald Welted4bdee72012-11-08 19:44:08 +0100340 }
341
342 return rc;
343}
344
Harald Welte8a1b0562012-11-09 12:51:44 +0100345/*! \brief signal handler for subscriber related signals */
346static int smpp_subscr_cb(unsigned int subsys, unsigned int signal,
347 void *handler_data, void *signal_data)
348{
Harald Welte2483f1b2016-06-19 18:06:02 +0200349 struct vlr_subscr *vsub = signal_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100350 struct smsc *smsc = handler_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100351 uint8_t smpp_avail_status;
352
353 /* determine the smpp_avail_status depending on attach/detach */
354 switch (signal) {
355 case S_SUBSCR_ATTACHED:
356 smpp_avail_status = 0;
357 break;
358 case S_SUBSCR_DETACHED:
359 smpp_avail_status = 2;
360 break;
361 default:
362 return 0;
363 }
364
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 alert_all_esme(smsc, vsub, smpp_avail_status);
Harald Welte8a1b0562012-11-09 12:51:44 +0100366
367 return 0;
368}
369
Harald Welteb8a1f962012-11-24 01:37:39 +0100370/* GSM 03.38 6.2.1 Character expanding (no decode!) */
371static int gsm_7bit_expand(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind)
372{
373 int i = 0;
374 int shift = 0;
375 uint8_t c;
376
377 /* skip the user data header */
378 if (ud_hdr_ind) {
379 /* get user data header length + 1 (for the 'user data header length'-field) */
380 shift = ((user_data[0] + 1) * 8) / 7;
381 if ((((user_data[0] + 1) * 8) % 7) != 0)
382 shift++;
383 septet_l = septet_l - shift;
384 }
385
386 for (i = 0; i < septet_l; i++) {
387 c =
388 ((user_data[((i + shift) * 7 + 7) >> 3] <<
389 (7 - (((i + shift) * 7 + 7) & 7))) |
390 (user_data[((i + shift) * 7) >> 3] >>
391 (((i + shift) * 7) & 7))) & 0x7f;
392
393 *(text++) = c;
394 }
395
396 *text = '\0';
397
398 return i;
399}
400
401
Harald Welte3f786002013-03-13 15:29:27 +0100402/* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */
403void append_tlv(tlv_t **req_tlv, uint16_t tag,
404 const uint8_t *data, uint16_t len)
405{
406 tlv_t tlv;
407
408 memset(&tlv, 0, sizeof(tlv));
409 tlv.tag = tag;
410 tlv.length = len;
411 memcpy(tlv.value.octet, data, tlv.length);
412 build_tlv(req_tlv, &tlv);
413}
414void append_tlv_u8(tlv_t **req_tlv, uint16_t tag, uint8_t val)
415{
416 tlv_t tlv;
417
418 memset(&tlv, 0, sizeof(tlv));
419 tlv.tag = tag;
420 tlv.length = 1;
421 tlv.value.val08 = val;
422 build_tlv(req_tlv, &tlv);
423}
424void append_tlv_u16(tlv_t **req_tlv, uint16_t tag, uint16_t val)
425{
426 tlv_t tlv;
427
428 memset(&tlv, 0, sizeof(tlv));
429 tlv.tag = tag;
430 tlv.length = 2;
431 tlv.value.val16 = htons(val);
432 build_tlv(req_tlv, &tlv);
433}
434
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200435#if BEFORE_MSCSPLIT
436/* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Harald Welte3f786002013-03-13 15:29:27 +0100437/* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */
438static void append_osmo_tlvs(tlv_t **req_tlv, const struct gsm_lchan *lchan)
439{
440 int idx = calc_initial_idx(ARRAY_SIZE(lchan->meas_rep),
441 lchan->meas_rep_idx, 1);
442 const struct gsm_meas_rep *mr = &lchan->meas_rep[idx];
443 const struct gsm_meas_rep_unidir *ul_meas = &mr->ul;
444 const struct gsm_meas_rep_unidir *dl_meas = &mr->dl;
445
446 /* Osmocom vendor-specific SMPP34 extensions */
447 append_tlv_u16(req_tlv, TLVID_osmo_arfcn, lchan->ts->trx->arfcn);
448 if (mr->flags & MEAS_REP_F_MS_L1) {
449 uint8_t ms_dbm;
450 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_l1.ta);
451 ms_dbm = ms_pwr_dbm(lchan->ts->trx->bts->band, mr->ms_l1.pwr);
452 append_tlv_u8(req_tlv, TLVID_osmo_ms_l1_txpwr, ms_dbm);
Max11e4e412017-04-20 13:07:58 +0200453 } else if (mr->flags & MEAS_REP_F_MS_TO) /* Save Timing Offset field = MS Timing Offset + 63 */
454 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_timing_offset + 63);
Harald Welte3f786002013-03-13 15:29:27 +0100455
456 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_ul,
457 rxlev2dbm(ul_meas->full.rx_lev));
458 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_ul, ul_meas->full.rx_qual);
459
460 if (mr->flags & MEAS_REP_F_DL_VALID) {
461 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_dl,
462 rxlev2dbm(dl_meas->full.rx_lev));
463 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_dl,
464 dl_meas->full.rx_qual);
465 }
466
Harald Welte2483f1b2016-06-19 18:06:02 +0200467 if (lchan->conn && lchan->conn->vsub) {
468 struct vlr_subscr *vsub = lchan->conn->vsub;
469 size_t imei_len = strlen(vsub->imei);
Harald Welte3f786002013-03-13 15:29:27 +0100470 if (imei_len)
471 append_tlv(req_tlv, TLVID_osmo_imei,
Harald Welte2483f1b2016-06-19 18:06:02 +0200472 (uint8_t *)vsub->imei, imei_len+1);
Harald Welte3f786002013-03-13 15:29:27 +0100473 }
474}
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200475#endif
Harald Welte3f786002013-03-13 15:29:27 +0100476
Keith320960c2017-05-13 23:38:52 +0200477struct {
478 uint32_t smpp_status_code;
479 uint8_t gsm411_cause;
480} smpp_to_gsm411_err_array[] = {
481
482 /* Seems like most phones don't care about the failure cause,
483 * although some will display a different notification for
484 * GSM411_RP_CAUSE_MO_NUM_UNASSIGNED
485 * Some provoke a display of "Try again later"
486 * while others a more definitive "Message sending failed"
487 */
488
489 { ESME_RSYSERR, GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER },
490 { ESME_RINVDSTADR, GSM411_RP_CAUSE_MO_NUM_UNASSIGNED },
491 { ESME_RMSGQFUL, GSM411_RP_CAUSE_MO_CONGESTION },
492 { ESME_RINVSRCADR, GSM411_RP_CAUSE_MO_SMS_REJECTED },
493 { ESME_RINVMSGID, GSM411_RP_CAUSE_INV_TRANS_REF }
494};
495
496static int smpp_to_gsm411_err(uint32_t smpp_status_code, int *gsm411_cause)
497{
498 int i;
499 for (i = 0; i < ARRAY_SIZE(smpp_to_gsm411_err_array); i++) {
500 if (smpp_to_gsm411_err_array[i].smpp_status_code != smpp_status_code)
501 continue;
502 *gsm411_cause = smpp_to_gsm411_err_array[i].gsm411_cause;
503 return 0;
504 }
505 return -1;
506}
507
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200508static void smpp_cmd_free(struct osmo_smpp_cmd *cmd)
509{
510 osmo_timer_del(&cmd->response_timer);
511 llist_del(&cmd->list);
Harald Welte2483f1b2016-06-19 18:06:02 +0200512 vlr_subscr_put(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200513 talloc_free(cmd);
514}
515
516void smpp_cmd_flush_pending(struct osmo_esme *esme)
517{
518 struct osmo_smpp_cmd *cmd, *next;
519
520 llist_for_each_entry_safe(cmd, next, &esme->smpp_cmd_list, list)
521 smpp_cmd_free(cmd);
522}
523
524void smpp_cmd_ack(struct osmo_smpp_cmd *cmd)
525{
526 struct gsm_subscriber_connection *conn;
527 struct gsm_trans *trans;
528
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100529 if (cmd->is_report)
530 goto out;
531
Harald Welte2483f1b2016-06-19 18:06:02 +0200532 conn = connection_for_subscr(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200533 if (!conn) {
534 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n");
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100535 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200536 }
537
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100538 trans = trans_find_by_id(conn, GSM48_PDISC_SMS, cmd->gsm411_trans_id);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200539 if (!trans) {
540 LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n",
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100541 cmd->gsm411_trans_id);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100542 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200543 }
544
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100545 gsm411_send_rp_ack(trans, cmd->gsm411_msg_ref);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100546out:
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200547 smpp_cmd_free(cmd);
548}
549
Keith320960c2017-05-13 23:38:52 +0200550void smpp_cmd_err(struct osmo_smpp_cmd *cmd, uint32_t status)
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200551{
552 struct gsm_subscriber_connection *conn;
553 struct gsm_trans *trans;
Keith320960c2017-05-13 23:38:52 +0200554 int gsm411_cause;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200555
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100556 if (cmd->is_report)
557 goto out;
558
Harald Welte2483f1b2016-06-19 18:06:02 +0200559 conn = connection_for_subscr(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200560 if (!conn) {
561 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n");
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100562 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200563 }
564
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100565 trans = trans_find_by_id(conn, GSM48_PDISC_SMS, cmd->gsm411_trans_id);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200566 if (!trans) {
567 LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n",
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100568 cmd->gsm411_trans_id);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100569 goto out;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200570 }
571
Keith320960c2017-05-13 23:38:52 +0200572 if (smpp_to_gsm411_err(status, &gsm411_cause) < 0)
573 gsm411_cause = GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
574
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100575 gsm411_send_rp_error(trans, cmd->gsm411_msg_ref, gsm411_cause);
Pablo Neira Ayusof00d8102017-08-07 14:01:01 +0100576out:
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200577 smpp_cmd_free(cmd);
578}
579
580static void smpp_deliver_sm_cb(void *data)
581{
Keith320960c2017-05-13 23:38:52 +0200582 smpp_cmd_err(data, ESME_RSYSERR);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200583}
584
585static int smpp_cmd_enqueue(struct osmo_esme *esme,
Harald Welte2483f1b2016-06-19 18:06:02 +0200586 struct vlr_subscr *vsub, struct gsm_sms *sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100587 uint32_t sequence_number)
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200588{
589 struct osmo_smpp_cmd *cmd;
590
591 cmd = talloc_zero(esme, struct osmo_smpp_cmd);
592 if (!cmd)
593 return -1;
594
595 cmd->sequence_nr = sequence_number;
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100596 cmd->is_report = sms->is_report;
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100597 cmd->gsm411_msg_ref = sms->gsm411.msg_ref;
598 cmd->gsm411_trans_id = sms->gsm411.transaction_id;
Harald Welte2483f1b2016-06-19 18:06:02 +0200599 cmd->vsub = vlr_subscr_get(vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200600
601 /* FIXME: No predefined value for this response_timer as specified by
602 * SMPP 3.4 specs, section 7.2. Make this configurable? Don't forget
603 * lchan keeps busy until we get a reply to this SMPP command. Too high
604 * value may exhaust resources.
605 */
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200606 osmo_timer_setup(&cmd->response_timer, smpp_deliver_sm_cb, cmd);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200607 osmo_timer_schedule(&cmd->response_timer, 5, 0);
608 llist_add_tail(&cmd->list, &esme->smpp_cmd_list);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200609
610 return 0;
611}
612
613struct osmo_smpp_cmd *smpp_cmd_find_by_seqnum(struct osmo_esme *esme,
614 uint32_t sequence_nr)
615{
616 struct osmo_smpp_cmd *cmd;
617
618 llist_for_each_entry(cmd, &esme->smpp_cmd_list, list) {
619 if (cmd->sequence_nr == sequence_nr)
620 return cmd;
621 }
622 return NULL;
623}
624
Pablo Neira Ayuso6ceac7c2017-08-07 14:01:15 +0100625/* See SMPP 3.4, Section 5.2.17. registered_delivery (1 byte field). */
626#define SMPP34_NO_DELIVERY_RECEIPT 0x0
627#define SMPP34_DELIVERY_RECEIPT_REQ 0x1
628
Harald Welte3f786002013-03-13 15:29:27 +0100629static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100630 struct gsm_subscriber_connection *conn)
Harald Weltee07b6a72012-11-23 19:02:37 +0100631{
632 struct deliver_sm_t deliver;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200633 int mode, ret;
Harald Weltee07b6a72012-11-23 19:02:37 +0100634 uint8_t dcs;
635
636 memset(&deliver, 0, sizeof(deliver));
637 deliver.command_length = 0;
638 deliver.command_id = DELIVER_SM;
639 deliver.command_status = ESME_ROK;
640
641 strcpy((char *)deliver.service_type, "CMT");
642 if (esme->acl && esme->acl->deliver_src_imsi) {
643 deliver.source_addr_ton = TON_Subscriber_Number;
644 deliver.source_addr_npi = NPI_Land_Mobile_E212;
645 snprintf((char *)deliver.source_addr,
646 sizeof(deliver.source_addr), "%s",
Harald Welte2483f1b2016-06-19 18:06:02 +0200647 conn->vsub->imsi);
Harald Weltee07b6a72012-11-23 19:02:37 +0100648 } else {
649 deliver.source_addr_ton = TON_Network_Specific;
650 deliver.source_addr_npi = NPI_ISDN_E163_E164;
651 snprintf((char *)deliver.source_addr,
652 sizeof(deliver.source_addr), "%s",
Harald Welte2483f1b2016-06-19 18:06:02 +0200653 conn->vsub->msisdn);
Harald Weltee07b6a72012-11-23 19:02:37 +0100654 }
655
Harald Weltec0de14d2012-11-23 23:35:01 +0100656 deliver.dest_addr_ton = sms->dst.ton;
657 deliver.dest_addr_npi = sms->dst.npi;
658 memcpy(deliver.destination_addr, sms->dst.addr,
Harald Weltee07b6a72012-11-23 19:02:37 +0100659 sizeof(deliver.destination_addr));
660
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100661 /* Short message contains a delivery receipt? Sect. 5.2.12. */
662 if (sms->is_report)
663 deliver.esm_class = 0x04;
664 else
665 deliver.esm_class = 1; /* datagram mode */
666
Harald Weltee07b6a72012-11-23 19:02:37 +0100667 if (sms->ud_hdr_ind)
668 deliver.esm_class |= 0x40;
669 if (sms->reply_path_req)
670 deliver.esm_class |= 0x80;
671
672 deliver.protocol_id = sms->protocol_id;
673 deliver.priority_flag = 0;
Pablo Neira Ayuso6ceac7c2017-08-07 14:01:15 +0100674 if (sms->status_rep_req)
675 deliver.registered_delivery = SMPP34_DELIVERY_RECEIPT_REQ;
676 else
677 deliver.registered_delivery = SMPP34_NO_DELIVERY_RECEIPT;
Harald Weltee07b6a72012-11-23 19:02:37 +0100678
Harald Weltec75ed6d2013-05-28 20:58:02 +0200679 /* Figure out SMPP DCS from TP-DCS */
Harald Weltee07b6a72012-11-23 19:02:37 +0100680 dcs = sms->data_coding_scheme;
Holger Hans Peter Freythera7328a52013-07-13 17:09:56 +0200681 if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200682 return -1;
Harald Weltec75ed6d2013-05-28 20:58:02 +0200683
684 /* Transparently pass on DCS via SMPP if requested */
Holger Hans Peter Freyther921b2272013-07-14 08:54:07 +0200685 if (esme->acl && esme->acl->dcs_transparent)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200686 deliver.data_coding = dcs;
687
688 if (mode == MODE_7BIT) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100689 uint8_t *dst = deliver.short_message;
Harald Weltee07b6a72012-11-23 19:02:37 +0100690
691 /* SMPP has this strange notion of putting 7bit SMS in
692 * an octet-aligned mode */
Harald Weltee07b6a72012-11-23 19:02:37 +0100693 if (sms->ud_hdr_ind) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100694 /* length (bytes) of UDH inside UD */
695 uint8_t udh_len = sms->user_data[0] + 1;
696
697 /* copy over the UDH */
698 memcpy(dst, sms->user_data, udh_len);
699 dst += udh_len;
700 deliver.sm_length = udh_len;
Harald Weltee07b6a72012-11-23 19:02:37 +0100701 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100702 /* add decoded text */
703 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 +0200704 } else {
Harald Weltee07b6a72012-11-23 19:02:37 +0100705 deliver.sm_length = sms->user_data_len;
706 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
Harald Weltee07b6a72012-11-23 19:02:37 +0100707 }
708
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200709#if BEFORE_MSCSPLIT
710 /* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Holger Hans Peter Freyther019851a2015-02-08 09:21:04 +0100711 if (esme->acl && esme->acl->osmocom_ext && conn->lchan)
Harald Welte3f786002013-03-13 15:29:27 +0100712 append_osmo_tlvs(&deliver.tlv, conn->lchan);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200713#endif
Harald Welte3f786002013-03-13 15:29:27 +0100714
Pablo Neira Ayusoadae8592017-08-07 14:01:30 +0100715 append_tlv_u16(&deliver.tlv, TLVID_user_message_reference,
716 sms->msg_ref);
717
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200718 ret = smpp_tx_deliver(esme, &deliver);
719 if (ret < 0)
720 return ret;
721
Harald Welte2483f1b2016-06-19 18:06:02 +0200722 return smpp_cmd_enqueue(esme, conn->vsub, sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100723 deliver.sequence_number);
Harald Weltee07b6a72012-11-23 19:02:37 +0100724}
725
Harald Welte338e3b32012-11-20 22:22:04 +0100726static struct smsc *g_smsc;
727
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200728int smpp_route_smpp_first(struct gsm_sms *sms, struct gsm_subscriber_connection *conn)
729{
730 return g_smsc->smpp_first;
731}
732
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200733int smpp_try_deliver(struct gsm_sms *sms,
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100734 struct gsm_subscriber_connection *conn)
Harald Weltee07b6a72012-11-23 19:02:37 +0100735{
736 struct osmo_esme *esme;
737 struct osmo_smpp_addr dst;
Benoit Bolseed34ed572017-07-05 11:46:55 +0200738 int rc;
Harald Weltee07b6a72012-11-23 19:02:37 +0100739
740 memset(&dst, 0, sizeof(dst));
Harald Weltec0de14d2012-11-23 23:35:01 +0100741 dst.ton = sms->dst.ton;
742 dst.npi = sms->dst.npi;
743 memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr));
Harald Weltee07b6a72012-11-23 19:02:37 +0100744
Benoit Bolseed34ed572017-07-05 11:46:55 +0200745 rc = smpp_route(g_smsc, &dst, &esme);
746 if (!rc)
Pablo Neira Ayusofdc99662017-08-07 14:01:10 +0100747 rc = deliver_to_esme(esme, sms, conn);
Harald Weltee07b6a72012-11-23 19:02:37 +0100748
Benoit Bolseed34ed572017-07-05 11:46:55 +0200749 return rc;
Harald Weltee07b6a72012-11-23 19:02:37 +0100750}
751
Harald Welte338e3b32012-11-20 22:22:04 +0100752struct smsc *smsc_from_vty(struct vty *v)
753{
754 /* FIXME: this is ugly */
755 return g_smsc;
756}
757
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100758/*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */
759int smpp_openbsc_alloc_init(void *ctx)
Harald Weltef1033cc2012-11-08 16:14:37 +0100760{
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100761 g_smsc = smpp_smsc_alloc_init(ctx);
762 if (!g_smsc) {
763 LOGP(DSMPP, LOGL_FATAL, "Cannot allocate smsc struct\n");
764 return -1;
765 }
766 return smpp_vty_init();
767}
768
769/*! \brief Launch the OpenBSC SMPP interface with the parameters set from VTY.
770 */
771int smpp_openbsc_start(struct gsm_network *net)
772{
Harald Weltef1033cc2012-11-08 16:14:37 +0100773 int rc;
Harald Welte76afa162013-03-13 15:05:26 +0100774 g_smsc->priv = net;
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100775
776 /* If a VTY configuration has taken place, the values have been stored
777 * in the smsc struct. Otherwise, use the defaults (NULL -> any, 0 ->
778 * default SMPP port, see smpp_smsc_bind()). */
779 rc = smpp_smsc_start(g_smsc, g_smsc->bind_addr, g_smsc->listen_port);
780 if (rc < 0)
781 return rc;
782
783 rc = osmo_signal_register_handler(SS_SMS, smpp_sms_cb, g_smsc);
784 if (rc < 0)
785 return rc;
786 rc = osmo_signal_register_handler(SS_SUBSCR, smpp_subscr_cb, g_smsc);
787 if (rc < 0)
788 return rc;
789
790 return 0;
Harald Welte76afa162013-03-13 15:05:26 +0100791}
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100792