blob: 24a465373956b6d49bda14953d34541dcbb56bbb [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;
Harald Weltec0de14d2012-11-23 23:35:01 +0100133
134 /* fill in the destination address */
Harald Welte1a2993a2012-11-09 19:48:48 +0100135 sms->receiver = dest;
Harald Weltec0de14d2012-11-23 23:35:01 +0100136 sms->dst.ton = submit->dest_addr_ton;
137 sms->dst.npi = submit->dest_addr_npi;
Harald Welte2483f1b2016-06-19 18:06:02 +0200138 osmo_strlcpy(sms->dst.addr, dest->msisdn, sizeof(sms->dst.addr));
Harald Weltec0de14d2012-11-23 23:35:01 +0100139
140 /* fill in the source address */
Harald Weltec0de14d2012-11-23 23:35:01 +0100141 sms->src.ton = submit->source_addr_ton;
142 sms->src.npi = submit->source_addr_npi;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100143 osmo_strlcpy(sms->src.addr, (char *)submit->source_addr,
144 sizeof(sms->src.addr));
Harald Weltef1033cc2012-11-08 16:14:37 +0100145
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 Welte9ad03622012-11-08 22:15:04 +0100154 if (submit->data_coding == 0x00 || /* SMSC default */
Harald Welte27d5e652013-05-28 20:37:07 +0200155 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 Welte7e40be32014-02-21 13:21:03 +0100171 } else if (submit->data_coding == 0x08) {
Harald Welte27d5e652013-05-28 20:37:07 +0200172 /* 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 Weltec75ed6d2013-05-28 20:58:02 +0200182 if (mode == MODE_7BIT) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100183 uint8_t ud_len = 0, padbits = 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100184 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
Harald Welte9122c132012-11-09 19:43:50 +0100185 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 Welteb8a1f962012-11-24 01:37:39 +0100192 padbits = 7 - (ud_len % 7);
Harald Welte9122c132012-11-09 19:43:50 +0100193 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100194 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 Welte27d5e652013-05-28 20:37:07 +0200198 } else {
Harald Weltef1033cc2012-11-08 16:14:37 +0100199 memcpy(sms->user_data, sms_msg, sms_msg_len);
200 sms->user_data_len = sms_msg_len;
Harald Weltef1033cc2012-11-08 16:14:37 +0100201 }
202
203 *psms = sms;
204 return ESME_ROK;
205}
206
Harald Weltee94db492012-11-08 20:11:05 +0100207/*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100208int 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 Freytherb5a4edd2013-01-20 17:43:50 +0100212 struct gsm_network *net = esme->smsc->priv;
213 struct sms_signal_data sig;
Harald Weltef1033cc2012-11-08 16:14:37 +0100214 int rc = -1;
215
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100216 rc = submit_to_sms(&sms, net, submit);
Harald Weltef1033cc2012-11-08 16:14:37 +0100217 if (rc != ESME_ROK) {
218 submit_r->command_status = rc;
219 return 0;
220 }
Harald Weltee94db492012-11-08 20:11:05 +0100221 smpp_esme_get(esme);
Harald Welted4bdee72012-11-08 19:44:08 +0100222 sms->smpp.esme = esme;
Harald Welte9ad03622012-11-08 22:15:04 +0100223 sms->protocol_id = submit->protocol_id;
Harald Weltef1033cc2012-11-08 16:14:37 +0100224
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 Freyther6a85c152013-01-20 17:21:31 +0100230 sms_free(sms);
231 sms = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +0100232 if (rc < 0) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100233 LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to "
Harald Weltef1033cc2012-11-08 16:14:37 +0100234 "store SMS in database\n");
Harald Weltef1033cc2012-11-08 16:14:37 +0100235 submit_r->command_status = ESME_RSYSERR;
236 return 0;
237 }
238 strcpy((char *)submit_r->message_id, "msg_id_not_implemented");
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100239 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Stored in DB\n");
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100240
241 memset(&sig, 0, sizeof(sig));
242 osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig);
Harald Weltef1033cc2012-11-08 16:14:37 +0100243 rc = 0;
244 break;
245 case 2: /* forward (i.e. transaction) mode */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100246 LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in "
Harald Welte9122c132012-11-09 19:43:50 +0100247 "real time (Transaction/Forward mode)\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100248 sms->smpp.transaction_mode = 1;
249 gsm411_send_sms_subscr(sms->receiver, sms);
Harald Weltef1033cc2012-11-08 16:14:37 +0100250 rc = 1; /* don't send any response yet */
251 break;
252 }
253 return rc;
254}
255
Harald Welte2483f1b2016-06-19 18:06:02 +0200256static void alert_all_esme(struct smsc *smsc, struct vlr_subscr *vsub,
Harald Welte045f4022013-07-30 23:45:01 +0800257 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 Welte2483f1b2016-06-19 18:06:02 +0200269 vsub->imsi, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800270 } else {
271 smpp_tx_alert(esme, TON_Network_Specific,
272 NPI_ISDN_E163_E164,
Harald Welte2483f1b2016-06-19 18:06:02 +0200273 vsub->msisdn, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800274 }
275 }
276}
277
278
Harald Weltee94db492012-11-08 20:11:05 +0100279/*! \brief signal handler for status of attempted SMS deliveries */
Harald Welted4bdee72012-11-08 19:44:08 +0100280static int smpp_sms_cb(unsigned int subsys, unsigned int signal,
281 void *handler_data, void *signal_data)
282{
Harald Welted4bdee72012-11-08 19:44:08 +0100283 struct sms_signal_data *sig_sms = signal_data;
284 struct gsm_sms *sms = sig_sms->sms;
Harald Welte99e273d2013-07-30 23:39:18 +0800285 struct smsc *smsc = handler_data;
Harald Welted4bdee72012-11-08 19:44:08 +0100286 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 Welte1aeb2af2013-07-30 22:30:24 +0800295 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 Welted4bdee72012-11-08 19:44:08 +0100299 case S_SMS_UNKNOWN_ERROR:
300 if (sms->smpp.transaction_mode) {
301 /* Send back the SUBMIT-SM response with apropriate error */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100302 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100303 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 Welte0d0c9ec2012-11-24 11:13:19 +0100313 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100314 rc = smpp_tx_submit_r(sms->smpp.esme,
315 sms->smpp.sequence_nr,
316 ESME_ROK, sms->smpp.msg_id);
317 }
318 break;
Harald Welte99e273d2013-07-30 23:39:18 +0800319 case S_SMS_SMMA:
Harald Welte2483f1b2016-06-19 18:06:02 +0200320 if (!sig_sms->trans || !sig_sms->trans->vsub) {
Harald Welte99e273d2013-07-30 23:39:18 +0800321 /* SMMA without a subscriber? strange... */
322 LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n");
323 break;
324 }
325
Harald Welte99e273d2013-07-30 23:39:18 +0800326 /* 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 Welte2483f1b2016-06-19 18:06:02 +0200329 alert_all_esme(smsc, sig_sms->trans->vsub, 0);
Harald Welte99e273d2013-07-30 23:39:18 +0800330 break;
Harald Welted4bdee72012-11-08 19:44:08 +0100331 }
332
333 return rc;
334}
335
Harald Welte8a1b0562012-11-09 12:51:44 +0100336/*! \brief signal handler for subscriber related signals */
337static int smpp_subscr_cb(unsigned int subsys, unsigned int signal,
338 void *handler_data, void *signal_data)
339{
Harald Welte2483f1b2016-06-19 18:06:02 +0200340 struct vlr_subscr *vsub = signal_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100341 struct smsc *smsc = handler_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100342 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 Welte2483f1b2016-06-19 18:06:02 +0200356 alert_all_esme(smsc, vsub, smpp_avail_status);
Harald Welte8a1b0562012-11-09 12:51:44 +0100357
358 return 0;
359}
360
Harald Welteb8a1f962012-11-24 01:37:39 +0100361/* GSM 03.38 6.2.1 Character expanding (no decode!) */
362static 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 Welte3f786002013-03-13 15:29:27 +0100393/* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */
394void 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}
405void 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}
415void 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 Hofmeyre2f24d52017-05-08 15:12:20 +0200426#if BEFORE_MSCSPLIT
427/* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Harald Welte3f786002013-03-13 15:29:27 +0100428/* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */
429static 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);
Max11e4e412017-04-20 13:07:58 +0200444 } 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 Welte3f786002013-03-13 15:29:27 +0100446
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 Welte2483f1b2016-06-19 18:06:02 +0200458 if (lchan->conn && lchan->conn->vsub) {
459 struct vlr_subscr *vsub = lchan->conn->vsub;
460 size_t imei_len = strlen(vsub->imei);
Harald Welte3f786002013-03-13 15:29:27 +0100461 if (imei_len)
462 append_tlv(req_tlv, TLVID_osmo_imei,
Harald Welte2483f1b2016-06-19 18:06:02 +0200463 (uint8_t *)vsub->imei, imei_len+1);
Harald Welte3f786002013-03-13 15:29:27 +0100464 }
465}
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200466#endif
Harald Welte3f786002013-03-13 15:29:27 +0100467
Keith320960c2017-05-13 23:38:52 +0200468struct {
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
487static 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 Ayuso93ffbd02017-05-04 18:44:22 +0200499static void smpp_cmd_free(struct osmo_smpp_cmd *cmd)
500{
501 osmo_timer_del(&cmd->response_timer);
502 llist_del(&cmd->list);
Harald Welte2483f1b2016-06-19 18:06:02 +0200503 vlr_subscr_put(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200504 sms_free(cmd->sms);
505 talloc_free(cmd);
506}
507
508void 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
516void smpp_cmd_ack(struct osmo_smpp_cmd *cmd)
517{
518 struct gsm_subscriber_connection *conn;
519 struct gsm_trans *trans;
520
Harald Welte2483f1b2016-06-19 18:06:02 +0200521 conn = connection_for_subscr(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200522 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
Keith320960c2017-05-13 23:38:52 +0200539void smpp_cmd_err(struct osmo_smpp_cmd *cmd, uint32_t status)
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200540{
541 struct gsm_subscriber_connection *conn;
542 struct gsm_trans *trans;
Keith320960c2017-05-13 23:38:52 +0200543 int gsm411_cause;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200544
Harald Welte2483f1b2016-06-19 18:06:02 +0200545 conn = connection_for_subscr(cmd->vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200546 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
Keith320960c2017-05-13 23:38:52 +0200559 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 Ayuso93ffbd02017-05-04 18:44:22 +0200564 smpp_cmd_free(cmd);
565}
566
567static void smpp_deliver_sm_cb(void *data)
568{
Keith320960c2017-05-13 23:38:52 +0200569 smpp_cmd_err(data, ESME_RSYSERR);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200570}
571
572static int smpp_cmd_enqueue(struct osmo_esme *esme,
Harald Welte2483f1b2016-06-19 18:06:02 +0200573 struct vlr_subscr *vsub, struct gsm_sms *sms,
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200574 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 Welte2483f1b2016-06-19 18:06:02 +0200584 cmd->vsub = vlr_subscr_get(vsub);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200585
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 Ayuso51215762017-05-08 20:57:52 +0200591 osmo_timer_setup(&cmd->response_timer, smpp_deliver_sm_cb, cmd);
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200592 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
599struct 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 Welte3f786002013-03-13 15:29:27 +0100611static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms,
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200612 struct gsm_subscriber_connection *conn,
613 bool *deferred)
Harald Weltee07b6a72012-11-23 19:02:37 +0100614{
615 struct deliver_sm_t deliver;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200616 int mode, ret;
Harald Weltee07b6a72012-11-23 19:02:37 +0100617 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 Welte2483f1b2016-06-19 18:06:02 +0200630 conn->vsub->imsi);
Harald Weltee07b6a72012-11-23 19:02:37 +0100631 } 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 Welte2483f1b2016-06-19 18:06:02 +0200636 conn->vsub->msisdn);
Harald Weltee07b6a72012-11-23 19:02:37 +0100637 }
638
Harald Weltec0de14d2012-11-23 23:35:01 +0100639 deliver.dest_addr_ton = sms->dst.ton;
640 deliver.dest_addr_npi = sms->dst.npi;
641 memcpy(deliver.destination_addr, sms->dst.addr,
Harald Weltee07b6a72012-11-23 19:02:37 +0100642 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 Weltec75ed6d2013-05-28 20:58:02 +0200654 /* Figure out SMPP DCS from TP-DCS */
Harald Weltee07b6a72012-11-23 19:02:37 +0100655 dcs = sms->data_coding_scheme;
Holger Hans Peter Freythera7328a52013-07-13 17:09:56 +0200656 if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200657 return -1;
Harald Weltec75ed6d2013-05-28 20:58:02 +0200658
659 /* Transparently pass on DCS via SMPP if requested */
Holger Hans Peter Freyther921b2272013-07-14 08:54:07 +0200660 if (esme->acl && esme->acl->dcs_transparent)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200661 deliver.data_coding = dcs;
662
663 if (mode == MODE_7BIT) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100664 uint8_t *dst = deliver.short_message;
Harald Weltee07b6a72012-11-23 19:02:37 +0100665
666 /* SMPP has this strange notion of putting 7bit SMS in
667 * an octet-aligned mode */
Harald Weltee07b6a72012-11-23 19:02:37 +0100668 if (sms->ud_hdr_ind) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100669 /* 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 Weltee07b6a72012-11-23 19:02:37 +0100676 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100677 /* add decoded text */
678 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 +0200679 } else {
Harald Weltee07b6a72012-11-23 19:02:37 +0100680 deliver.sm_length = sms->user_data_len;
681 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
Harald Weltee07b6a72012-11-23 19:02:37 +0100682 deliver.sm_length = sms->user_data_len;
683 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
684 }
685
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200686#if BEFORE_MSCSPLIT
687 /* We currently have no lchan information. Re-add after A-interface, see OS#2390. */
Holger Hans Peter Freyther019851a2015-02-08 09:21:04 +0100688 if (esme->acl && esme->acl->osmocom_ext && conn->lchan)
Harald Welte3f786002013-03-13 15:29:27 +0100689 append_osmo_tlvs(&deliver.tlv, conn->lchan);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200690#endif
Harald Welte3f786002013-03-13 15:29:27 +0100691
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200692 ret = smpp_tx_deliver(esme, &deliver);
693 if (ret < 0)
694 return ret;
695
Harald Welte2483f1b2016-06-19 18:06:02 +0200696 return smpp_cmd_enqueue(esme, conn->vsub, sms,
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200697 deliver.sequence_number, deferred);
Harald Weltee07b6a72012-11-23 19:02:37 +0100698}
699
Harald Welte338e3b32012-11-20 22:22:04 +0100700static struct smsc *g_smsc;
701
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200702int smpp_route_smpp_first(struct gsm_sms *sms, struct gsm_subscriber_connection *conn)
703{
704 return g_smsc->smpp_first;
705}
706
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200707int smpp_try_deliver(struct gsm_sms *sms,
708 struct gsm_subscriber_connection *conn, bool *deferred)
Harald Weltee07b6a72012-11-23 19:02:37 +0100709{
710 struct osmo_esme *esme;
711 struct osmo_smpp_addr dst;
712
713 memset(&dst, 0, sizeof(dst));
Harald Weltec0de14d2012-11-23 23:35:01 +0100714 dst.ton = sms->dst.ton;
715 dst.npi = sms->dst.npi;
716 memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr));
Harald Weltee07b6a72012-11-23 19:02:37 +0100717
718 esme = smpp_route(g_smsc, &dst);
719 if (!esme)
Pablo Neira Ayuso638ad062017-05-03 18:59:05 +0200720 return GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
Harald Weltee07b6a72012-11-23 19:02:37 +0100721
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200722 return deliver_to_esme(esme, sms, conn, deferred);
Harald Weltee07b6a72012-11-23 19:02:37 +0100723}
724
Harald Welte338e3b32012-11-20 22:22:04 +0100725struct smsc *smsc_from_vty(struct vty *v)
726{
727 /* FIXME: this is ugly */
728 return g_smsc;
729}
730
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100731/*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */
732int smpp_openbsc_alloc_init(void *ctx)
Harald Weltef1033cc2012-11-08 16:14:37 +0100733{
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100734 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 */
744int smpp_openbsc_start(struct gsm_network *net)
745{
Harald Weltef1033cc2012-11-08 16:14:37 +0100746 int rc;
Harald Welte76afa162013-03-13 15:05:26 +0100747 g_smsc->priv = net;
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100748
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 Welte76afa162013-03-13 15:05:26 +0100764}
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100765