blob: 7a47e9386add16332690ccbafa554ffc9413aab2 [file] [log] [blame]
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +07001/*
Vadim Yanitskiy6e722822019-12-19 17:22:22 +09002 * (C) 2018-2019 by Vadim Yanitskiy <axilirator@gmail.com>
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +07003 *
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <stdio.h>
22#include <errno.h>
23
24#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/utils.h>
26#include <osmocom/core/msgb.h>
27
28#include <osmocom/gsupclient/gsup_client.h>
29#include <osmocom/msc/gsm_subscriber.h>
30#include <osmocom/msc/transaction.h>
31#include <osmocom/msc/msc_common.h>
32#include <osmocom/msc/debug.h>
33#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010034#include <osmocom/msc/msub.h>
35#include <osmocom/msc/gsup_client_mux.h>
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070036
37/* Common helper for preparing to be encoded GSUP message */
38static void gsup_sm_msg_init(struct osmo_gsup_message *gsup_msg,
39 enum osmo_gsup_message_type msg_type, const char *imsi,
40 uint8_t *sm_rp_mr)
41{
42 /* Init a mew GSUP message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010043 *gsup_msg = (struct osmo_gsup_message){
44 .message_type = msg_type,
45 .sm_rp_mr = sm_rp_mr,
46 .message_class = OSMO_GSUP_MESSAGE_CLASS_SMS,
47 };
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070048
49 /* Fill in subscriber's IMSI */
50 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, imsi);
51}
52
53int gsm411_gsup_mo_fwd_sm_req(struct gsm_trans *trans, struct msgb *msg,
54 uint8_t sm_rp_mr, uint8_t *sm_rp_da, uint8_t sm_rp_da_len)
55{
Vadim Yanitskiy61f47802019-12-19 22:08:14 +090056 uint8_t bcd_buf[GSM48_MI_SIZE];
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070057 struct osmo_gsup_message gsup_msg;
58 size_t bcd_len;
59
60 /* Associate logging messages with this subscriber */
61 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
62
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010063 LOG_TRANS(trans, LOGL_DEBUG, "TX GSUP MO-forwardSM-Req\n");
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070064
65 /* Assign SM-RP-MR to transaction state */
66 trans->sms.sm_rp_mr = sm_rp_mr;
67
Vadim Yanitskiy6e722822019-12-19 17:22:22 +090068 /* Encode subscriber's MSISDN as LHV (with room for ToN/NPI header) */
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070069 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf),
Vadim Yanitskiy6e722822019-12-19 17:22:22 +090070 1, trans->vsub->msisdn);
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070071 if (bcd_len <= 0 || bcd_len > sizeof(bcd_buf)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010072 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode subscriber's MSISDN\n");
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070073 return -EINVAL;
74 }
75
Vadim Yanitskiy6e722822019-12-19 17:22:22 +090076 /* NOTE: assuming default ToN/NPI values as we don't have this info */
77 bcd_buf[1] = 0x01 /* NPI: ISDN/Telephony Numbering (ITU-T Rec. E.164 / ITU-T Rec. E.163) */
78 | (0x01 << 4) /* ToN: International Number */
79 | (0x01 << 7); /* No Extension */
80
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070081 /* Initialize a new GSUP message */
82 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST,
83 trans->vsub->imsi, &sm_rp_mr);
84
Vadim Yanitskiy6e722822019-12-19 17:22:22 +090085 /* According to 12.2.3, the MSISDN from VLR is inserted here.
86 * NOTE: redundant BCD length octet is not included. */
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070087 gsup_msg.sm_rp_oa_type = OSMO_GSUP_SMS_SM_RP_ODA_MSISDN;
Vadim Yanitskiy6e722822019-12-19 17:22:22 +090088 gsup_msg.sm_rp_oa_len = bcd_len - 1;
89 gsup_msg.sm_rp_oa = bcd_buf + 1;
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +070090
91 /* SM-RP-DA should (already) contain SMSC address */
92 gsup_msg.sm_rp_da_type = OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR;
93 gsup_msg.sm_rp_da_len = sm_rp_da_len;
94 gsup_msg.sm_rp_da = sm_rp_da;
95
96 /* SM-RP-UI (TPDU) is pointed by msgb->l4h */
97 gsup_msg.sm_rp_ui_len = msgb_l4len(msg);
98 gsup_msg.sm_rp_ui = (uint8_t *) msgb_sms(msg);
99
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100100 return gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700101}
102
103int gsm411_gsup_mo_ready_for_sm_req(struct gsm_trans *trans, uint8_t sm_rp_mr)
104{
105 struct osmo_gsup_message gsup_msg;
106
107 /* Associate logging messages with this subscriber */
108 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
109
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100110 LOG_TRANS(trans, LOGL_DEBUG, "TX GSUP READY-FOR-SM Req\n");
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700111
112 /* Assign SM-RP-MR to transaction state */
113 trans->sms.sm_rp_mr = sm_rp_mr;
114
115 /* Initialize a new GSUP message */
116 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST,
117 trans->vsub->imsi, &sm_rp_mr);
118
119 /* Indicate SMMA as the Alert Reason */
120 gsup_msg.sm_alert_rsn = OSMO_GSUP_SMS_SM_ALERT_RSN_MEM_AVAIL;
121
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100122 return gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700123}
124
125/* Triggers either RP-ACK or RP-ERROR on response from SMSC */
Vadim Yanitskiy805eca22019-06-15 17:30:23 +0700126static int gsm411_gsup_mo_handler(struct gsm_network *net, struct vlr_subscr *vsub,
127 const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700128{
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700129 struct gsm_trans *trans;
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700130 const char *msg_name;
131 bool msg_is_err;
132
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700133 /* Associate logging messages with this subscriber */
134 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
135
136 /* Determine the message type and name */
137 msg_is_err = OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type);
138 switch (gsup_msg->message_type) {
139 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
140 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
141 msg_name = "MO-forwardSM";
142 break;
143 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
144 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
145 msg_name = "MO-ReadyForSM";
146 break;
147 default:
148 /* Shall not happen */
149 OSMO_ASSERT(0);
150 }
151
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700152 /* Verify GSUP message */
153 if (!gsup_msg->sm_rp_mr)
154 goto msg_error;
155 if (msg_is_err && !gsup_msg->sm_rp_cause)
156 goto msg_error;
157
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700158 /* Attempt to find DTAP-transaction */
Vadim Yanitskiy36c44b22019-01-23 21:22:27 +0700159 trans = trans_find_by_sm_rp_mr(net, vsub, *(gsup_msg->sm_rp_mr));
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700160 if (!trans) {
161 LOGP(DLSMS, LOGL_NOTICE, "No transaction found for %s, "
162 "ignoring %s-%s message...\n", vlr_subscr_name(vsub),
163 msg_name, msg_is_err ? "Err" : "Res");
Vadim Yanitskiya361cab2020-04-20 10:50:14 +0700164 gsup_client_mux_tx_error_reply(net->gcm, gsup_msg, GMM_CAUSE_NO_PDP_ACTIVATED);
165 return -EIO;
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700166 }
167
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100168 LOG_TRANS(trans, LOGL_DEBUG, "RX %s-%s\n", msg_name, msg_is_err ? "Err" : "Res");
169
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700170 /* Send either RP-ERROR, or RP-ACK */
171 if (msg_is_err) {
172 /* TODO: handle optional SM-RP-UI payload (requires API change) */
173 gsm411_send_rp_error(trans, *(gsup_msg->sm_rp_mr),
174 *(gsup_msg->sm_rp_cause));
175 } else {
176 gsm411_send_rp_ack(trans, *(gsup_msg->sm_rp_mr));
177 }
178
179 return 0;
180
181msg_error:
Vadim Yanitskiy4547cf12020-04-20 10:42:28 +0700182 LOGP(DLSMS, LOGL_NOTICE, "RX malformed %s-%s\n", msg_name, msg_is_err ? "Err" : "Res");
183 gsup_client_mux_tx_error_reply(net->gcm, gsup_msg, GMM_CAUSE_INV_MAND_INFO);
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700184 return -EINVAL;
185}
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700186
187int gsm411_gsup_mt_fwd_sm_res(struct gsm_trans *trans, uint8_t sm_rp_mr)
188{
189 struct osmo_gsup_message gsup_msg;
190
191 /* Associate logging messages with this subscriber */
192 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
193
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100194 LOG_TRANS(trans, LOGL_DEBUG, "TX MT-forwardSM-Res\n");
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700195
196 /* Initialize a new GSUP message */
197 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT,
198 trans->vsub->imsi, &sm_rp_mr);
199
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100200 return gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700201}
202
203int gsm411_gsup_mt_fwd_sm_err(struct gsm_trans *trans,
204 uint8_t sm_rp_mr, uint8_t cause)
205{
206 struct osmo_gsup_message gsup_msg;
207
208 /* Associate logging messages with this subscriber */
209 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
210
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100211 LOG_TRANS(trans, LOGL_DEBUG, "TX MT-forwardSM-Err\n");
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700212
213 /* Initialize a new GSUP message */
214 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
215 trans->vsub->imsi, &sm_rp_mr);
216
217 /* SM-RP-Cause value */
218 gsup_msg.sm_rp_cause = &cause;
219
220 /* TODO: include optional SM-RP-UI field if present */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100221 return gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700222}
223
224/* Handles MT SMS (and triggers Paging Request if required) */
Vadim Yanitskiy805eca22019-06-15 17:30:23 +0700225static int gsm411_gsup_mt_handler(struct gsm_network *net, struct vlr_subscr *vsub,
226 const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700227{
Vadim Yanitskiy643270f2019-05-12 05:38:41 +0700228 bool sm_rp_mmts_ind;
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700229 int rc;
230
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700231 /* Associate logging messages with this subscriber */
232 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
233
234 LOGP(DLSMS, LOGL_DEBUG, "RX MT-forwardSM-Req\n");
235
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700236 /**
237 * Verify GSUP message
238 *
239 * FIXME: SM-RP-MR is not known yet (to be assigned by MSC)
240 * NOTE: SM-RP-DA is out of our interest
241 */
242 if (!gsup_msg->sm_rp_mr)
243 goto msg_error;
244 if (!gsup_msg->sm_rp_ui)
245 goto msg_error;
246
247 /* SM-RP-OA shall contain SMSC address */
248 if (gsup_msg->sm_rp_oa_type != OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR)
249 goto msg_error;
250
Vadim Yanitskiy643270f2019-05-12 05:38:41 +0700251 /* MMS (More Messages to Send) IE is optional */
252 if (gsup_msg->sm_rp_mms)
253 sm_rp_mmts_ind = *gsup_msg->sm_rp_mms > 0;
254 else
255 sm_rp_mmts_ind = false;
256
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700257 /* Send RP-DATA */
258 rc = gsm411_send_rp_data(net, vsub,
259 gsup_msg->sm_rp_oa_len, gsup_msg->sm_rp_oa,
Vadim Yanitskiy643270f2019-05-12 05:38:41 +0700260 gsup_msg->sm_rp_ui_len, gsup_msg->sm_rp_ui,
261 sm_rp_mmts_ind);
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700262 if (rc) {
263 LOGP(DLSMS, LOGL_NOTICE, "Failed to send MT SMS, "
264 "ignoring MT-forwardSM-Req message...\n");
Vadim Yanitskiy3c8fc132020-04-20 10:49:14 +0700265 gsup_client_mux_tx_error_reply(net->gcm, gsup_msg, GMM_CAUSE_NET_FAIL);
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700266 return rc;
267 }
268
269 return 0;
270
271msg_error:
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700272 LOGP(DLSMS, LOGL_NOTICE, "RX malformed MT-forwardSM-Req\n");
Vadim Yanitskiy4547cf12020-04-20 10:42:28 +0700273 gsup_client_mux_tx_error_reply(net->gcm, gsup_msg, GMM_CAUSE_INV_MAND_INFO);
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700274 return -EINVAL;
275}
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100276
277int gsm411_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup_msg)
278{
Vadim Yanitskiy805eca22019-06-15 17:30:23 +0700279 struct gsm_network *net = (struct gsm_network *) data;
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700280 struct vlr_subscr *vsub;
281 int rc;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100282
Vadim Yanitskiy531d3a42020-04-20 10:29:19 +0700283 /* Make sure that 'SMS over GSUP' is expected */
284 if (!net->sms_over_gsup) {
Vadim Yanitskiy531d3a42020-04-20 10:29:19 +0700285 LOGP(DLSMS, LOGL_NOTICE, "Unexpected MO/MT SMS over GSUP "
286 "(sms-over-gsup is not enabled), ignoring message...\n");
Vadim Yanitskiyb6ec0992020-04-20 10:33:36 +0700287 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_GPRS_NOTALLOWED);
Vadim Yanitskiy531d3a42020-04-20 10:29:19 +0700288 return -EIO;
289 }
290
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700291 vsub = vlr_subscr_find_by_imsi(net->vlr, gsup_msg->imsi, __func__);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100292 if (!vsub) {
Vadim Yanitskiy3d603032019-06-15 00:54:30 +0700293 LOGP(DLSMS, LOGL_ERROR, "Rx %s for unknown subscriber, rejecting\n",
294 osmo_gsup_message_type_name(gsup_msg->message_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100295 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
296 return -GMM_CAUSE_IMSI_UNKNOWN;
297 }
298
299 switch (gsup_msg->message_type) {
300 /* GSM 04.11 code implementing MO SMS */
301 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
302 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
303 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
304 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
305 DEBUGP(DMSC, "Routed to GSM 04.11 MO handler\n");
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700306 rc = gsm411_gsup_mo_handler(net, vsub, gsup_msg);
307 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308
309 /* GSM 04.11 code implementing MT SMS */
310 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
311 DEBUGP(DMSC, "Routed to GSM 04.11 MT handler\n");
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700312 rc = gsm411_gsup_mt_handler(net, vsub, gsup_msg);
313 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100314
315 default:
316 LOGP(DMM, LOGL_ERROR, "No handler found for %s, dropping message...\n",
317 osmo_gsup_message_type_name(gsup_msg->message_type));
Vadim Yanitskiyb1e46b12020-04-20 10:38:14 +0700318 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700319 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100320 }
Vadim Yanitskiy59e0c6b2019-06-19 19:48:37 +0700321
322 vlr_subscr_put(vsub, __func__);
323 return rc;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100324}