blob: eb092ae0b20aa9c1741b74e52f2d1c89c97f1838 [file] [log] [blame]
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +07001/*
2 * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com>
3 *
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>
34
35/* Common helper for preparing to be encoded GSUP message */
36static void gsup_sm_msg_init(struct osmo_gsup_message *gsup_msg,
37 enum osmo_gsup_message_type msg_type, const char *imsi,
38 uint8_t *sm_rp_mr)
39{
40 /* Init a mew GSUP message */
41 memset(gsup_msg, 0x00, sizeof(*gsup_msg));
42 gsup_msg->message_type = msg_type;
43
44 /* SM-RP-MR (Message Reference) */
45 gsup_msg->sm_rp_mr = sm_rp_mr;
46
47 /* Fill in subscriber's IMSI */
48 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, imsi);
49}
50
51int gsm411_gsup_mo_fwd_sm_req(struct gsm_trans *trans, struct msgb *msg,
52 uint8_t sm_rp_mr, uint8_t *sm_rp_da, uint8_t sm_rp_da_len)
53{
54 uint8_t bcd_buf[GSM48_MI_SIZE] = { 0 };
55 struct osmo_gsup_message gsup_msg;
56 size_t bcd_len;
57
58 /* Associate logging messages with this subscriber */
59 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
60
61 LOGP(DLSMS, LOGL_DEBUG, "TX GSUP MO-forwardSM-Req\n");
62
63 /* Assign SM-RP-MR to transaction state */
64 trans->sms.sm_rp_mr = sm_rp_mr;
65
66 /* Encode subscriber's MSISDN */
67 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf),
68 0, trans->vsub->msisdn);
69 if (bcd_len <= 0 || bcd_len > sizeof(bcd_buf)) {
70 LOGP(DLSMS, LOGL_ERROR, "Failed to encode subscriber's MSISDN\n");
71 return -EINVAL;
72 }
73
74 /* Initialize a new GSUP message */
75 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST,
76 trans->vsub->imsi, &sm_rp_mr);
77
78 /* According to 12.2.3, the MSISDN from VLR is inserted here */
79 gsup_msg.sm_rp_oa_type = OSMO_GSUP_SMS_SM_RP_ODA_MSISDN;
80 gsup_msg.sm_rp_oa_len = bcd_len;
81 gsup_msg.sm_rp_oa = bcd_buf;
82
83 /* SM-RP-DA should (already) contain SMSC address */
84 gsup_msg.sm_rp_da_type = OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR;
85 gsup_msg.sm_rp_da_len = sm_rp_da_len;
86 gsup_msg.sm_rp_da = sm_rp_da;
87
88 /* SM-RP-UI (TPDU) is pointed by msgb->l4h */
89 gsup_msg.sm_rp_ui_len = msgb_l4len(msg);
90 gsup_msg.sm_rp_ui = (uint8_t *) msgb_sms(msg);
91
92 return osmo_gsup_client_enc_send(trans->net->vlr->gsup_client, &gsup_msg);
93}
94
95int gsm411_gsup_mo_ready_for_sm_req(struct gsm_trans *trans, uint8_t sm_rp_mr)
96{
97 struct osmo_gsup_message gsup_msg;
98
99 /* Associate logging messages with this subscriber */
100 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
101
102 LOGP(DLSMS, LOGL_DEBUG, "TX GSUP READY-FOR-SM Req\n");
103
104 /* Assign SM-RP-MR to transaction state */
105 trans->sms.sm_rp_mr = sm_rp_mr;
106
107 /* Initialize a new GSUP message */
108 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST,
109 trans->vsub->imsi, &sm_rp_mr);
110
111 /* Indicate SMMA as the Alert Reason */
112 gsup_msg.sm_alert_rsn = OSMO_GSUP_SMS_SM_ALERT_RSN_MEM_AVAIL;
113
114 return osmo_gsup_client_enc_send(trans->net->vlr->gsup_client, &gsup_msg);
115}
116
117/* Triggers either RP-ACK or RP-ERROR on response from SMSC */
118int gsm411_gsup_mo_handler(struct vlr_subscr *vsub,
119 struct osmo_gsup_message *gsup_msg)
120{
121 struct vlr_instance *vlr;
122 struct gsm_network *net;
123 struct gsm_trans *trans;
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700124 const char *msg_name;
125 bool msg_is_err;
126
127 /* Obtain required pointers */
128 vlr = vsub->vlr;
129 net = (struct gsm_network *) vlr->user_ctx;
130
131 /* Associate logging messages with this subscriber */
132 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
133
134 /* Determine the message type and name */
135 msg_is_err = OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type);
136 switch (gsup_msg->message_type) {
137 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
138 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
139 msg_name = "MO-forwardSM";
140 break;
141 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
142 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
143 msg_name = "MO-ReadyForSM";
144 break;
145 default:
146 /* Shall not happen */
147 OSMO_ASSERT(0);
148 }
149
150 LOGP(DLSMS, LOGL_DEBUG, "RX %s-%s\n", msg_name,
151 msg_is_err ? "Err" : "Res");
152
153 /* Make sure that 'SMS over GSUP' is expected */
154 if (!net->sms_over_gsup) {
155 /* TODO: notify sender about that? */
156 LOGP(DLSMS, LOGL_NOTICE, "Unexpected MO SMS over GSUP, "
157 "ignoring message...\n");
158 return -EIO;
159 }
160
161 /* Verify GSUP message */
162 if (!gsup_msg->sm_rp_mr)
163 goto msg_error;
164 if (msg_is_err && !gsup_msg->sm_rp_cause)
165 goto msg_error;
166
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700167 /* Attempt to find DTAP-transaction */
Vadim Yanitskiy36c44b22019-01-23 21:22:27 +0700168 trans = trans_find_by_sm_rp_mr(net, vsub, *(gsup_msg->sm_rp_mr));
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +0700169 if (!trans) {
170 LOGP(DLSMS, LOGL_NOTICE, "No transaction found for %s, "
171 "ignoring %s-%s message...\n", vlr_subscr_name(vsub),
172 msg_name, msg_is_err ? "Err" : "Res");
173 return -EIO; /* TODO: notify sender about that? */
174 }
175
176 /* Send either RP-ERROR, or RP-ACK */
177 if (msg_is_err) {
178 /* TODO: handle optional SM-RP-UI payload (requires API change) */
179 gsm411_send_rp_error(trans, *(gsup_msg->sm_rp_mr),
180 *(gsup_msg->sm_rp_cause));
181 } else {
182 gsm411_send_rp_ack(trans, *(gsup_msg->sm_rp_mr));
183 }
184
185 return 0;
186
187msg_error:
188 /* TODO: notify sender about that? */
189 LOGP(DLSMS, LOGL_NOTICE, "RX malformed %s-%s\n",
190 msg_name, msg_is_err ? "Err" : "Res");
191 return -EINVAL;
192}
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +0700193
194int gsm411_gsup_mt_fwd_sm_res(struct gsm_trans *trans, uint8_t sm_rp_mr)
195{
196 struct osmo_gsup_message gsup_msg;
197
198 /* Associate logging messages with this subscriber */
199 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
200
201 LOGP(DLSMS, LOGL_DEBUG, "TX MT-forwardSM-Res\n");
202
203 /* Initialize a new GSUP message */
204 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT,
205 trans->vsub->imsi, &sm_rp_mr);
206
207 return osmo_gsup_client_enc_send(trans->net->vlr->gsup_client, &gsup_msg);
208}
209
210int gsm411_gsup_mt_fwd_sm_err(struct gsm_trans *trans,
211 uint8_t sm_rp_mr, uint8_t cause)
212{
213 struct osmo_gsup_message gsup_msg;
214
215 /* Associate logging messages with this subscriber */
216 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
217
218 LOGP(DLSMS, LOGL_DEBUG, "TX MT-forwardSM-Err\n");
219
220 /* Initialize a new GSUP message */
221 gsup_sm_msg_init(&gsup_msg, OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
222 trans->vsub->imsi, &sm_rp_mr);
223
224 /* SM-RP-Cause value */
225 gsup_msg.sm_rp_cause = &cause;
226
227 /* TODO: include optional SM-RP-UI field if present */
228 return osmo_gsup_client_enc_send(trans->net->vlr->gsup_client, &gsup_msg);
229}
230
231/* Handles MT SMS (and triggers Paging Request if required) */
232int gsm411_gsup_mt_handler(struct vlr_subscr *vsub,
233 struct osmo_gsup_message *gsup_msg)
234{
235 struct vlr_instance *vlr;
236 struct gsm_network *net;
237 int rc;
238
239 /* Obtain required pointers */
240 vlr = vsub->vlr;
241 net = (struct gsm_network *) vlr->user_ctx;
242
243 /* Associate logging messages with this subscriber */
244 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
245
246 LOGP(DLSMS, LOGL_DEBUG, "RX MT-forwardSM-Req\n");
247
248 /* Make sure that 'SMS over GSUP' is expected */
249 if (!net->sms_over_gsup) {
250 LOGP(DLSMS, LOGL_NOTICE, "Unexpected MT SMS over GSUP, "
251 "ignoring message...\n");
252 /* TODO: notify sender about that? */
253 return -EIO;
254 }
255
256 /**
257 * Verify GSUP message
258 *
259 * FIXME: SM-RP-MR is not known yet (to be assigned by MSC)
260 * NOTE: SM-RP-DA is out of our interest
261 */
262 if (!gsup_msg->sm_rp_mr)
263 goto msg_error;
264 if (!gsup_msg->sm_rp_ui)
265 goto msg_error;
266
267 /* SM-RP-OA shall contain SMSC address */
268 if (gsup_msg->sm_rp_oa_type != OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR)
269 goto msg_error;
270
271 /* Send RP-DATA */
272 rc = gsm411_send_rp_data(net, vsub,
273 gsup_msg->sm_rp_oa_len, gsup_msg->sm_rp_oa,
274 gsup_msg->sm_rp_ui_len, gsup_msg->sm_rp_ui);
275 if (rc) {
276 LOGP(DLSMS, LOGL_NOTICE, "Failed to send MT SMS, "
277 "ignoring MT-forwardSM-Req message...\n");
278 /* TODO: notify sender about that? */
279 return rc;
280 }
281
282 return 0;
283
284msg_error:
285 /* TODO: notify sender about that? */
286 LOGP(DLSMS, LOGL_NOTICE, "RX malformed MT-forwardSM-Req\n");
287 return -EINVAL;
288}