blob: 8f1a96c5c1029743bbf208e6f559310be7fa27e6 [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 Weltef1033cc2012-11-08 16:14:37 +010048
49#include "smpp_smsc.h"
50
Harald Weltee94db492012-11-08 20:11:05 +010051/*! \brief find gsm_subscriber for a given SMPP NPI/TON/Address */
Harald Weltef1033cc2012-11-08 16:14:37 +010052static struct gsm_subscriber *subscr_by_dst(struct gsm_network *net,
53 uint8_t npi, uint8_t ton, const char *addr)
54{
55 struct gsm_subscriber *subscr = NULL;
56
57 switch (npi) {
58 case NPI_Land_Mobile_E212:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010059 subscr = subscr_get_by_imsi(net->subscr_group, addr);
Harald Weltef1033cc2012-11-08 16:14:37 +010060 break;
61 case NPI_ISDN_E163_E164:
62 case NPI_Private:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010063 subscr = subscr_get_by_extension(net->subscr_group, addr);
Harald Weltef1033cc2012-11-08 16:14:37 +010064 break;
65 default:
66 LOGP(DSMPP, LOGL_NOTICE, "Unsupported NPI: %u\n", npi);
67 break;
68 }
69
Holger Hans Peter Freyther095bd362013-12-29 20:30:02 +010070 /* tag the context in case we know it */
Neels Hofmeyr89a8e722017-02-23 18:00:51 +010071 log_set_context(LOG_CTX_VLR_SUBSCR, subscr);
Harald Weltef1033cc2012-11-08 16:14:37 +010072 return subscr;
73}
74
Harald Weltee94db492012-11-08 20:11:05 +010075/*! \brief find a TLV with given tag in list of libsmpp34 TLVs */
Harald Weltee07b6a72012-11-23 19:02:37 +010076static struct tlv_t *find_tlv(struct tlv_t *head, uint16_t tag)
Harald Weltef1033cc2012-11-08 16:14:37 +010077{
78 struct tlv_t *t;
79
80 for (t = head; t != NULL; t = t->next) {
81 if (t->tag == tag)
82 return t;
83 }
84 return NULL;
85}
86
Harald Weltee94db492012-11-08 20:11:05 +010087/*! \brief convert from submit_sm_t to gsm_sms */
Harald Weltef1033cc2012-11-08 16:14:37 +010088static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net,
89 const struct submit_sm_t *submit)
90{
91 struct gsm_subscriber *dest;
92 struct gsm_sms *sms;
93 struct tlv_t *t;
94 const uint8_t *sms_msg;
95 unsigned int sms_msg_len;
Harald Welte27d5e652013-05-28 20:37:07 +020096 int mode;
Harald Weltef1033cc2012-11-08 16:14:37 +010097
98 dest = subscr_by_dst(net, submit->dest_addr_npi,
99 submit->dest_addr_ton,
100 (const char *)submit->destination_addr);
101 if (!dest) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100102 LOGP(DLSMS, LOGL_NOTICE, "SMPP SUBMIT-SM for unknown subscriber: "
Harald Weltef1033cc2012-11-08 16:14:37 +0100103 "%s (NPI=%u)\n", submit->destination_addr,
104 submit->dest_addr_npi);
105 return ESME_RINVDSTADR;
106 }
107
108 t = find_tlv(submit->tlv, TLVID_message_payload);
109 if (t) {
110 if (submit->sm_length) {
Holger Hans Peter Freyther94f83e12014-07-19 19:02:46 +0200111 /* ERROR: we cannot have both! */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100112 LOGP(DLSMS, LOGL_ERROR, "SMPP Cannot have payload in "
Harald Welte9122c132012-11-09 19:43:50 +0100113 "TLV _and_ in the header\n");
Holger Hans Peter Freyther5425e5e2015-08-04 12:22:56 +0200114 subscr_put(dest);
Harald Weltef1033cc2012-11-08 16:14:37 +0100115 return ESME_ROPTPARNOTALLWD;
116 }
117 sms_msg = t->value.octet;
118 sms_msg_len = t->length;
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100119 } else if (submit->sm_length > 0 && submit->sm_length < 255) {
Harald Weltef1033cc2012-11-08 16:14:37 +0100120 sms_msg = submit->short_message;
121 sms_msg_len = submit->sm_length;
122 } else {
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100123 LOGP(DLSMS, LOGL_ERROR,
124 "SMPP neither message payload nor valid sm_length.\n");
Holger Hans Peter Freyther5425e5e2015-08-04 12:22:56 +0200125 subscr_put(dest);
Holger Hans Peter Freythera0735ec2015-02-08 09:53:44 +0100126 return ESME_RINVPARLEN;
Harald Weltef1033cc2012-11-08 16:14:37 +0100127 }
128
129 sms = sms_alloc();
Harald Welted4bdee72012-11-08 19:44:08 +0100130 sms->source = SMS_SOURCE_SMPP;
131 sms->smpp.sequence_nr = submit->sequence_number;
Harald Weltec0de14d2012-11-23 23:35:01 +0100132
133 /* fill in the destination address */
Harald Welte1a2993a2012-11-09 19:48:48 +0100134 sms->receiver = dest;
Harald Weltec0de14d2012-11-23 23:35:01 +0100135 sms->dst.ton = submit->dest_addr_ton;
136 sms->dst.npi = submit->dest_addr_npi;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100137 osmo_strlcpy(sms->dst.addr, dest->extension, sizeof(sms->dst.addr));
Harald Weltec0de14d2012-11-23 23:35:01 +0100138
139 /* fill in the source address */
Harald Weltec0de14d2012-11-23 23:35:01 +0100140 sms->src.ton = submit->source_addr_ton;
141 sms->src.npi = submit->source_addr_npi;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100142 osmo_strlcpy(sms->src.addr, (char *)submit->source_addr,
143 sizeof(sms->src.addr));
Harald Weltef1033cc2012-11-08 16:14:37 +0100144
145 if (submit->esm_class & 0x40)
146 sms->ud_hdr_ind = 1;
147
148 if (submit->esm_class & 0x80) {
149 sms->reply_path_req = 1;
150#warning Implement reply path
151 }
152
Harald Welte9ad03622012-11-08 22:15:04 +0100153 if (submit->data_coding == 0x00 || /* SMSC default */
Harald Welte27d5e652013-05-28 20:37:07 +0200154 submit->data_coding == 0x01) { /* GSM default alphabet */
155 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
156 mode = MODE_7BIT;
157 } else if ((submit->data_coding & 0xFC) == 0xF0) { /* 03.38 DCS default */
158 /* pass DCS 1:1 through from SMPP to GSM */
159 sms->data_coding_scheme = submit->data_coding;
160 mode = MODE_7BIT;
161 } else if (submit->data_coding == 0x02 ||
162 submit->data_coding == 0x04) {
163 /* 8-bit binary */
164 sms->data_coding_scheme = GSM338_DCS_1111_8BIT_DATA;
165 mode = MODE_8BIT;
166 } else if ((submit->data_coding & 0xFC) == 0xF4) { /* 03.38 DCS 8bit */
167 /* pass DCS 1:1 through from SMPP to GSM */
168 sms->data_coding_scheme = submit->data_coding;
169 mode = MODE_8BIT;
Harald Welte7e40be32014-02-21 13:21:03 +0100170 } else if (submit->data_coding == 0x08) {
Harald Welte27d5e652013-05-28 20:37:07 +0200171 /* UCS-2 */
172 sms->data_coding_scheme = (2 << 2);
173 mode = MODE_8BIT;
174 } else {
175 sms_free(sms);
176 LOGP(DLSMS, LOGL_ERROR, "SMPP Unknown Data Coding 0x%02x\n",
177 submit->data_coding);
178 return ESME_RUNKNOWNERR;
179 }
180
Harald Weltec75ed6d2013-05-28 20:58:02 +0200181 if (mode == MODE_7BIT) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100182 uint8_t ud_len = 0, padbits = 0;
Harald Weltef1033cc2012-11-08 16:14:37 +0100183 sms->data_coding_scheme = GSM338_DCS_1111_7BIT;
Harald Welte9122c132012-11-09 19:43:50 +0100184 if (sms->ud_hdr_ind) {
185 ud_len = *sms_msg + 1;
186 printf("copying %u bytes user data...\n", ud_len);
187 memcpy(sms->user_data, sms_msg,
188 OSMO_MIN(ud_len, sizeof(sms->user_data)));
189 sms_msg += ud_len;
190 sms_msg_len -= ud_len;
Harald Welteb8a1f962012-11-24 01:37:39 +0100191 padbits = 7 - (ud_len % 7);
Harald Welte9122c132012-11-09 19:43:50 +0100192 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100193 gsm_septets2octets(sms->user_data+ud_len, sms_msg,
194 sms_msg_len, padbits);
195 sms->user_data_len = (ud_len*8 + padbits)/7 + sms_msg_len;/* SEPTETS */
196 /* FIXME: sms->text */
Harald Welte27d5e652013-05-28 20:37:07 +0200197 } else {
Harald Weltef1033cc2012-11-08 16:14:37 +0100198 memcpy(sms->user_data, sms_msg, sms_msg_len);
199 sms->user_data_len = sms_msg_len;
Harald Weltef1033cc2012-11-08 16:14:37 +0100200 }
201
202 *psms = sms;
203 return ESME_ROK;
204}
205
Harald Weltee94db492012-11-08 20:11:05 +0100206/*! \brief handle incoming libsmpp34 ssubmit_sm_t from remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100207int handle_smpp_submit(struct osmo_esme *esme, struct submit_sm_t *submit,
208 struct submit_sm_resp_t *submit_r)
209{
210 struct gsm_sms *sms;
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100211 struct gsm_network *net = esme->smsc->priv;
212 struct sms_signal_data sig;
Harald Weltef1033cc2012-11-08 16:14:37 +0100213 int rc = -1;
214
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100215 rc = submit_to_sms(&sms, net, submit);
Harald Weltef1033cc2012-11-08 16:14:37 +0100216 if (rc != ESME_ROK) {
217 submit_r->command_status = rc;
218 return 0;
219 }
Harald Weltee94db492012-11-08 20:11:05 +0100220 smpp_esme_get(esme);
Harald Welted4bdee72012-11-08 19:44:08 +0100221 sms->smpp.esme = esme;
Harald Welte9ad03622012-11-08 22:15:04 +0100222 sms->protocol_id = submit->protocol_id;
Harald Weltef1033cc2012-11-08 16:14:37 +0100223
224 switch (submit->esm_class & 3) {
225 case 0: /* default */
226 case 1: /* datagram */
227 case 3: /* store-and-forward */
228 rc = db_sms_store(sms);
Holger Hans Peter Freyther6a85c152013-01-20 17:21:31 +0100229 sms_free(sms);
230 sms = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +0100231 if (rc < 0) {
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100232 LOGP(DLSMS, LOGL_ERROR, "SMPP SUBMIT-SM: Unable to "
Harald Weltef1033cc2012-11-08 16:14:37 +0100233 "store SMS in database\n");
Harald Weltef1033cc2012-11-08 16:14:37 +0100234 submit_r->command_status = ESME_RSYSERR;
235 return 0;
236 }
237 strcpy((char *)submit_r->message_id, "msg_id_not_implemented");
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100238 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Stored in DB\n");
Holger Hans Peter Freytherb5a4edd2013-01-20 17:43:50 +0100239
240 memset(&sig, 0, sizeof(sig));
241 osmo_signal_dispatch(SS_SMS, S_SMS_SUBMITTED, &sig);
Harald Weltef1033cc2012-11-08 16:14:37 +0100242 rc = 0;
243 break;
244 case 2: /* forward (i.e. transaction) mode */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100245 LOGP(DLSMS, LOGL_DEBUG, "SMPP SUBMIT-SM: Forwarding in "
Harald Welte9122c132012-11-09 19:43:50 +0100246 "real time (Transaction/Forward mode)\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100247 sms->smpp.transaction_mode = 1;
248 gsm411_send_sms_subscr(sms->receiver, sms);
Harald Weltef1033cc2012-11-08 16:14:37 +0100249 rc = 1; /* don't send any response yet */
250 break;
251 }
252 return rc;
253}
254
Harald Welte045f4022013-07-30 23:45:01 +0800255static void alert_all_esme(struct smsc *smsc, struct gsm_subscriber *subscr,
256 uint8_t smpp_avail_status)
257{
258 struct osmo_esme *esme;
259
260 llist_for_each_entry(esme, &smsc->esme_list, list) {
261 /* we currently send an alert notification to each ESME that is
262 * connected, and do not require a (non-existant) delivery
263 * pending flag to be set before, FIXME: make this VTY
264 * configurable */
265 if (esme->acl && esme->acl->deliver_src_imsi) {
266 smpp_tx_alert(esme, TON_Subscriber_Number,
267 NPI_Land_Mobile_E212,
Keith80abe522016-12-22 19:18:18 +0100268 subscr->imsi, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800269 } else {
270 smpp_tx_alert(esme, TON_Network_Specific,
271 NPI_ISDN_E163_E164,
Keith80abe522016-12-22 19:18:18 +0100272 subscr->extension, smpp_avail_status);
Harald Welte045f4022013-07-30 23:45:01 +0800273 }
274 }
275}
276
277
Harald Weltee94db492012-11-08 20:11:05 +0100278/*! \brief signal handler for status of attempted SMS deliveries */
Harald Welted4bdee72012-11-08 19:44:08 +0100279static int smpp_sms_cb(unsigned int subsys, unsigned int signal,
280 void *handler_data, void *signal_data)
281{
Harald Welted4bdee72012-11-08 19:44:08 +0100282 struct sms_signal_data *sig_sms = signal_data;
283 struct gsm_sms *sms = sig_sms->sms;
Harald Welte99e273d2013-07-30 23:39:18 +0800284 struct smsc *smsc = handler_data;
Harald Welted4bdee72012-11-08 19:44:08 +0100285 int rc = 0;
286
287 if (!sms)
288 return 0;
289
290 if (sms->source != SMS_SOURCE_SMPP)
291 return 0;
292
293 switch (signal) {
Harald Welte1aeb2af2013-07-30 22:30:24 +0800294 case S_SMS_MEM_EXCEEDED:
295 /* fall-through: There is no ESME_Rxxx result code to
296 * indicate a MEMORY EXCEEDED in transaction mode back
297 * to the ESME */
Harald Welted4bdee72012-11-08 19:44:08 +0100298 case S_SMS_UNKNOWN_ERROR:
299 if (sms->smpp.transaction_mode) {
300 /* Send back the SUBMIT-SM response with apropriate error */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100301 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Error\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100302 rc = smpp_tx_submit_r(sms->smpp.esme,
303 sms->smpp.sequence_nr,
304 ESME_RDELIVERYFAILURE,
305 sms->smpp.msg_id);
306 }
307 break;
308 case S_SMS_DELIVERED:
309 /* SMS layer tells us the delivery has been completed */
310 if (sms->smpp.transaction_mode) {
311 /* Send back the SUBMIT-SM response */
Harald Welte0d0c9ec2012-11-24 11:13:19 +0100312 LOGP(DLSMS, LOGL_INFO, "SMPP SUBMIT-SM: Success\n");
Harald Welted4bdee72012-11-08 19:44:08 +0100313 rc = smpp_tx_submit_r(sms->smpp.esme,
314 sms->smpp.sequence_nr,
315 ESME_ROK, sms->smpp.msg_id);
316 }
317 break;
Harald Welte99e273d2013-07-30 23:39:18 +0800318 case S_SMS_SMMA:
319 if (!sig_sms->trans || !sig_sms->trans->subscr) {
320 /* SMMA without a subscriber? strange... */
321 LOGP(DLSMS, LOGL_NOTICE, "SMMA without subscriber?\n");
322 break;
323 }
324
Harald Welte99e273d2013-07-30 23:39:18 +0800325 /* There's no real 1:1 match for SMMA in SMPP. However,
326 * an ALERT NOTIFICATION seems to be the most logical
327 * choice */
Harald Welte045f4022013-07-30 23:45:01 +0800328 alert_all_esme(smsc, sig_sms->trans->subscr, 0);
Harald Welte99e273d2013-07-30 23:39:18 +0800329 break;
Harald Welted4bdee72012-11-08 19:44:08 +0100330 }
331
332 return rc;
333}
334
Harald Welte8a1b0562012-11-09 12:51:44 +0100335/*! \brief signal handler for subscriber related signals */
336static int smpp_subscr_cb(unsigned int subsys, unsigned int signal,
337 void *handler_data, void *signal_data)
338{
339 struct gsm_subscriber *subscr = signal_data;
340 struct smsc *smsc = handler_data;
Harald Welte8a1b0562012-11-09 12:51:44 +0100341 uint8_t smpp_avail_status;
342
343 /* determine the smpp_avail_status depending on attach/detach */
344 switch (signal) {
345 case S_SUBSCR_ATTACHED:
346 smpp_avail_status = 0;
347 break;
348 case S_SUBSCR_DETACHED:
349 smpp_avail_status = 2;
350 break;
351 default:
352 return 0;
353 }
354
Harald Welte045f4022013-07-30 23:45:01 +0800355 alert_all_esme(smsc, subscr, smpp_avail_status);
Harald Welte8a1b0562012-11-09 12:51:44 +0100356
357 return 0;
358}
359
Harald Welteb8a1f962012-11-24 01:37:39 +0100360/* GSM 03.38 6.2.1 Character expanding (no decode!) */
361static int gsm_7bit_expand(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind)
362{
363 int i = 0;
364 int shift = 0;
365 uint8_t c;
366
367 /* skip the user data header */
368 if (ud_hdr_ind) {
369 /* get user data header length + 1 (for the 'user data header length'-field) */
370 shift = ((user_data[0] + 1) * 8) / 7;
371 if ((((user_data[0] + 1) * 8) % 7) != 0)
372 shift++;
373 septet_l = septet_l - shift;
374 }
375
376 for (i = 0; i < septet_l; i++) {
377 c =
378 ((user_data[((i + shift) * 7 + 7) >> 3] <<
379 (7 - (((i + shift) * 7 + 7) & 7))) |
380 (user_data[((i + shift) * 7) >> 3] >>
381 (((i + shift) * 7) & 7))) & 0x7f;
382
383 *(text++) = c;
384 }
385
386 *text = '\0';
387
388 return i;
389}
390
391
Harald Welte3f786002013-03-13 15:29:27 +0100392/* FIXME: libsmpp34 helpers, they should be part of libsmpp34! */
393void append_tlv(tlv_t **req_tlv, uint16_t tag,
394 const uint8_t *data, uint16_t len)
395{
396 tlv_t tlv;
397
398 memset(&tlv, 0, sizeof(tlv));
399 tlv.tag = tag;
400 tlv.length = len;
401 memcpy(tlv.value.octet, data, tlv.length);
402 build_tlv(req_tlv, &tlv);
403}
404void append_tlv_u8(tlv_t **req_tlv, uint16_t tag, uint8_t val)
405{
406 tlv_t tlv;
407
408 memset(&tlv, 0, sizeof(tlv));
409 tlv.tag = tag;
410 tlv.length = 1;
411 tlv.value.val08 = val;
412 build_tlv(req_tlv, &tlv);
413}
414void append_tlv_u16(tlv_t **req_tlv, uint16_t tag, uint16_t val)
415{
416 tlv_t tlv;
417
418 memset(&tlv, 0, sizeof(tlv));
419 tlv.tag = tag;
420 tlv.length = 2;
421 tlv.value.val16 = htons(val);
422 build_tlv(req_tlv, &tlv);
423}
424
425/* Append the Osmocom vendor-specific additional TLVs to a SMPP msg */
426static void append_osmo_tlvs(tlv_t **req_tlv, const struct gsm_lchan *lchan)
427{
428 int idx = calc_initial_idx(ARRAY_SIZE(lchan->meas_rep),
429 lchan->meas_rep_idx, 1);
430 const struct gsm_meas_rep *mr = &lchan->meas_rep[idx];
431 const struct gsm_meas_rep_unidir *ul_meas = &mr->ul;
432 const struct gsm_meas_rep_unidir *dl_meas = &mr->dl;
433
434 /* Osmocom vendor-specific SMPP34 extensions */
435 append_tlv_u16(req_tlv, TLVID_osmo_arfcn, lchan->ts->trx->arfcn);
436 if (mr->flags & MEAS_REP_F_MS_L1) {
437 uint8_t ms_dbm;
438 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_l1.ta);
439 ms_dbm = ms_pwr_dbm(lchan->ts->trx->bts->band, mr->ms_l1.pwr);
440 append_tlv_u8(req_tlv, TLVID_osmo_ms_l1_txpwr, ms_dbm);
Max11e4e412017-04-20 13:07:58 +0200441 } else if (mr->flags & MEAS_REP_F_MS_TO) /* Save Timing Offset field = MS Timing Offset + 63 */
442 append_tlv_u8(req_tlv, TLVID_osmo_ta, mr->ms_timing_offset + 63);
Harald Welte3f786002013-03-13 15:29:27 +0100443
444 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_ul,
445 rxlev2dbm(ul_meas->full.rx_lev));
446 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_ul, ul_meas->full.rx_qual);
447
448 if (mr->flags & MEAS_REP_F_DL_VALID) {
449 append_tlv_u16(req_tlv, TLVID_osmo_rxlev_dl,
450 rxlev2dbm(dl_meas->full.rx_lev));
451 append_tlv_u8(req_tlv, TLVID_osmo_rxqual_dl,
452 dl_meas->full.rx_qual);
453 }
454
455 if (lchan->conn && lchan->conn->subscr) {
456 struct gsm_subscriber *subscr = lchan->conn->subscr;
457 size_t imei_len = strlen(subscr->equipment.imei);
458 if (imei_len)
459 append_tlv(req_tlv, TLVID_osmo_imei,
460 (uint8_t *)subscr->equipment.imei, imei_len+1);
461 }
462}
463
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200464static void smpp_cmd_free(struct osmo_smpp_cmd *cmd)
465{
466 osmo_timer_del(&cmd->response_timer);
467 llist_del(&cmd->list);
468 subscr_put(cmd->subscr);
469 sms_free(cmd->sms);
470 talloc_free(cmd);
471}
472
473void smpp_cmd_flush_pending(struct osmo_esme *esme)
474{
475 struct osmo_smpp_cmd *cmd, *next;
476
477 llist_for_each_entry_safe(cmd, next, &esme->smpp_cmd_list, list)
478 smpp_cmd_free(cmd);
479}
480
481void smpp_cmd_ack(struct osmo_smpp_cmd *cmd)
482{
483 struct gsm_subscriber_connection *conn;
484 struct gsm_trans *trans;
485
486 conn = connection_for_subscr(cmd->subscr);
487 if (!conn) {
488 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n");
489 return;
490 }
491
492 trans = trans_find_by_id(conn, GSM48_PDISC_SMS,
493 cmd->sms->gsm411.transaction_id);
494 if (!trans) {
495 LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n",
496 cmd->sms->gsm411.transaction_id);
497 return;
498 }
499
500 gsm411_send_rp_ack(trans, cmd->sms->gsm411.msg_ref);
501 smpp_cmd_free(cmd);
502}
503
504void smpp_cmd_err(struct osmo_smpp_cmd *cmd)
505{
506 struct gsm_subscriber_connection *conn;
507 struct gsm_trans *trans;
508
509 conn = connection_for_subscr(cmd->subscr);
510 if (!conn) {
511 LOGP(DSMPP, LOGL_ERROR, "No connection to subscriber anymore\n");
512 return;
513 }
514
515 trans = trans_find_by_id(conn, GSM48_PDISC_SMS,
516 cmd->sms->gsm411.transaction_id);
517 if (!trans) {
518 LOGP(DSMPP, LOGL_ERROR, "GSM transaction %u is gone\n",
519 cmd->sms->gsm411.transaction_id);
520 return;
521 }
522
523 gsm411_send_rp_error(trans, cmd->sms->gsm411.msg_ref,
524 GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER);
525 smpp_cmd_free(cmd);
526}
527
528static void smpp_deliver_sm_cb(void *data)
529{
530 smpp_cmd_err(data);
531}
532
533static int smpp_cmd_enqueue(struct osmo_esme *esme,
534 struct gsm_subscriber *subscr, struct gsm_sms *sms,
535 uint32_t sequence_number, bool *deferred)
536{
537 struct osmo_smpp_cmd *cmd;
538
539 cmd = talloc_zero(esme, struct osmo_smpp_cmd);
540 if (!cmd)
541 return -1;
542
543 cmd->sequence_nr = sequence_number;
544 cmd->sms = sms;
545 cmd->subscr = subscr_get(subscr);
546
547 /* FIXME: No predefined value for this response_timer as specified by
548 * SMPP 3.4 specs, section 7.2. Make this configurable? Don't forget
549 * lchan keeps busy until we get a reply to this SMPP command. Too high
550 * value may exhaust resources.
551 */
552 cmd->response_timer.cb = smpp_deliver_sm_cb;
553 cmd->response_timer.data = cmd;
554 osmo_timer_schedule(&cmd->response_timer, 5, 0);
555 llist_add_tail(&cmd->list, &esme->smpp_cmd_list);
556 *deferred = true;
557
558 return 0;
559}
560
561struct osmo_smpp_cmd *smpp_cmd_find_by_seqnum(struct osmo_esme *esme,
562 uint32_t sequence_nr)
563{
564 struct osmo_smpp_cmd *cmd;
565
566 llist_for_each_entry(cmd, &esme->smpp_cmd_list, list) {
567 if (cmd->sequence_nr == sequence_nr)
568 return cmd;
569 }
570 return NULL;
571}
572
Harald Welte3f786002013-03-13 15:29:27 +0100573static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms,
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200574 struct gsm_subscriber_connection *conn,
575 bool *deferred)
Harald Weltee07b6a72012-11-23 19:02:37 +0100576{
577 struct deliver_sm_t deliver;
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200578 int mode, ret;
Harald Weltee07b6a72012-11-23 19:02:37 +0100579 uint8_t dcs;
580
581 memset(&deliver, 0, sizeof(deliver));
582 deliver.command_length = 0;
583 deliver.command_id = DELIVER_SM;
584 deliver.command_status = ESME_ROK;
585
586 strcpy((char *)deliver.service_type, "CMT");
587 if (esme->acl && esme->acl->deliver_src_imsi) {
588 deliver.source_addr_ton = TON_Subscriber_Number;
589 deliver.source_addr_npi = NPI_Land_Mobile_E212;
590 snprintf((char *)deliver.source_addr,
591 sizeof(deliver.source_addr), "%s",
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200592 conn->subscr->imsi);
Harald Weltee07b6a72012-11-23 19:02:37 +0100593 } else {
594 deliver.source_addr_ton = TON_Network_Specific;
595 deliver.source_addr_npi = NPI_ISDN_E163_E164;
596 snprintf((char *)deliver.source_addr,
597 sizeof(deliver.source_addr), "%s",
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200598 conn->subscr->extension);
Harald Weltee07b6a72012-11-23 19:02:37 +0100599 }
600
Harald Weltec0de14d2012-11-23 23:35:01 +0100601 deliver.dest_addr_ton = sms->dst.ton;
602 deliver.dest_addr_npi = sms->dst.npi;
603 memcpy(deliver.destination_addr, sms->dst.addr,
Harald Weltee07b6a72012-11-23 19:02:37 +0100604 sizeof(deliver.destination_addr));
605
606 deliver.esm_class = 1; /* datagram mode */
607 if (sms->ud_hdr_ind)
608 deliver.esm_class |= 0x40;
609 if (sms->reply_path_req)
610 deliver.esm_class |= 0x80;
611
612 deliver.protocol_id = sms->protocol_id;
613 deliver.priority_flag = 0;
614 deliver.registered_delivery = 0;
615
Harald Weltec75ed6d2013-05-28 20:58:02 +0200616 /* Figure out SMPP DCS from TP-DCS */
Harald Weltee07b6a72012-11-23 19:02:37 +0100617 dcs = sms->data_coding_scheme;
Holger Hans Peter Freythera7328a52013-07-13 17:09:56 +0200618 if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200619 return -1;
Harald Weltec75ed6d2013-05-28 20:58:02 +0200620
621 /* Transparently pass on DCS via SMPP if requested */
Holger Hans Peter Freyther921b2272013-07-14 08:54:07 +0200622 if (esme->acl && esme->acl->dcs_transparent)
Harald Weltec75ed6d2013-05-28 20:58:02 +0200623 deliver.data_coding = dcs;
624
625 if (mode == MODE_7BIT) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100626 uint8_t *dst = deliver.short_message;
Harald Weltee07b6a72012-11-23 19:02:37 +0100627
628 /* SMPP has this strange notion of putting 7bit SMS in
629 * an octet-aligned mode */
Harald Weltee07b6a72012-11-23 19:02:37 +0100630 if (sms->ud_hdr_ind) {
Harald Welteb8a1f962012-11-24 01:37:39 +0100631 /* length (bytes) of UDH inside UD */
632 uint8_t udh_len = sms->user_data[0] + 1;
633
634 /* copy over the UDH */
635 memcpy(dst, sms->user_data, udh_len);
636 dst += udh_len;
637 deliver.sm_length = udh_len;
Harald Weltee07b6a72012-11-23 19:02:37 +0100638 }
Harald Welteb8a1f962012-11-24 01:37:39 +0100639 /* add decoded text */
640 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 +0200641 } else {
Harald Weltee07b6a72012-11-23 19:02:37 +0100642 deliver.sm_length = sms->user_data_len;
643 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
Harald Weltee07b6a72012-11-23 19:02:37 +0100644 deliver.sm_length = sms->user_data_len;
645 memcpy(deliver.short_message, sms->user_data, deliver.sm_length);
646 }
647
Holger Hans Peter Freyther019851a2015-02-08 09:21:04 +0100648 if (esme->acl && esme->acl->osmocom_ext && conn->lchan)
Harald Welte3f786002013-03-13 15:29:27 +0100649 append_osmo_tlvs(&deliver.tlv, conn->lchan);
650
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200651 ret = smpp_tx_deliver(esme, &deliver);
652 if (ret < 0)
653 return ret;
654
655 return smpp_cmd_enqueue(esme, conn->subscr, sms,
656 deliver.sequence_number, deferred);
Harald Weltee07b6a72012-11-23 19:02:37 +0100657}
658
Harald Welte338e3b32012-11-20 22:22:04 +0100659static struct smsc *g_smsc;
660
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200661int smpp_route_smpp_first(struct gsm_sms *sms, struct gsm_subscriber_connection *conn)
662{
663 return g_smsc->smpp_first;
664}
665
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200666int smpp_try_deliver(struct gsm_sms *sms,
667 struct gsm_subscriber_connection *conn, bool *deferred)
Harald Weltee07b6a72012-11-23 19:02:37 +0100668{
669 struct osmo_esme *esme;
670 struct osmo_smpp_addr dst;
671
672 memset(&dst, 0, sizeof(dst));
Harald Weltec0de14d2012-11-23 23:35:01 +0100673 dst.ton = sms->dst.ton;
674 dst.npi = sms->dst.npi;
675 memcpy(dst.addr, sms->dst.addr, sizeof(dst.addr));
Harald Weltee07b6a72012-11-23 19:02:37 +0100676
677 esme = smpp_route(g_smsc, &dst);
678 if (!esme)
Pablo Neira Ayuso638ad062017-05-03 18:59:05 +0200679 return GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
Harald Weltee07b6a72012-11-23 19:02:37 +0100680
Pablo Neira Ayuso93ffbd02017-05-04 18:44:22 +0200681 return deliver_to_esme(esme, sms, conn, deferred);
Harald Weltee07b6a72012-11-23 19:02:37 +0100682}
683
Harald Welte338e3b32012-11-20 22:22:04 +0100684struct smsc *smsc_from_vty(struct vty *v)
685{
686 /* FIXME: this is ugly */
687 return g_smsc;
688}
689
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100690/*! \brief Allocate the OpenBSC SMPP interface struct and init VTY. */
691int smpp_openbsc_alloc_init(void *ctx)
Harald Weltef1033cc2012-11-08 16:14:37 +0100692{
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100693 g_smsc = smpp_smsc_alloc_init(ctx);
694 if (!g_smsc) {
695 LOGP(DSMPP, LOGL_FATAL, "Cannot allocate smsc struct\n");
696 return -1;
697 }
698 return smpp_vty_init();
699}
700
701/*! \brief Launch the OpenBSC SMPP interface with the parameters set from VTY.
702 */
703int smpp_openbsc_start(struct gsm_network *net)
704{
Harald Weltef1033cc2012-11-08 16:14:37 +0100705 int rc;
Harald Welte76afa162013-03-13 15:05:26 +0100706 g_smsc->priv = net;
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100707
708 /* If a VTY configuration has taken place, the values have been stored
709 * in the smsc struct. Otherwise, use the defaults (NULL -> any, 0 ->
710 * default SMPP port, see smpp_smsc_bind()). */
711 rc = smpp_smsc_start(g_smsc, g_smsc->bind_addr, g_smsc->listen_port);
712 if (rc < 0)
713 return rc;
714
715 rc = osmo_signal_register_handler(SS_SMS, smpp_sms_cb, g_smsc);
716 if (rc < 0)
717 return rc;
718 rc = osmo_signal_register_handler(SS_SUBSCR, smpp_subscr_cb, g_smsc);
719 if (rc < 0)
720 return rc;
721
722 return 0;
Harald Welte76afa162013-03-13 15:05:26 +0100723}
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100724