blob: 5c0107245497e5a2f2a0d02829ecdb5ee9add379 [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;
124 struct ran_conn *conn;
125 const char *msg_name;
126 bool msg_is_err;
127
128 /* Obtain required pointers */
129 vlr = vsub->vlr;
130 net = (struct gsm_network *) vlr->user_ctx;
131
132 /* Associate logging messages with this subscriber */
133 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
134
135 /* Determine the message type and name */
136 msg_is_err = OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type);
137 switch (gsup_msg->message_type) {
138 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
139 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
140 msg_name = "MO-forwardSM";
141 break;
142 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
143 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
144 msg_name = "MO-ReadyForSM";
145 break;
146 default:
147 /* Shall not happen */
148 OSMO_ASSERT(0);
149 }
150
151 LOGP(DLSMS, LOGL_DEBUG, "RX %s-%s\n", msg_name,
152 msg_is_err ? "Err" : "Res");
153
154 /* Make sure that 'SMS over GSUP' is expected */
155 if (!net->sms_over_gsup) {
156 /* TODO: notify sender about that? */
157 LOGP(DLSMS, LOGL_NOTICE, "Unexpected MO SMS over GSUP, "
158 "ignoring message...\n");
159 return -EIO;
160 }
161
162 /* Verify GSUP message */
163 if (!gsup_msg->sm_rp_mr)
164 goto msg_error;
165 if (msg_is_err && !gsup_msg->sm_rp_cause)
166 goto msg_error;
167
168 /* Attempt to find a DTAP-connection */
169 conn = connection_for_subscr(vsub);
170 if (!conn) {
171 /* FIXME: should we establish it then? */
172 LOGP(DLSMS, LOGL_NOTICE, "No connection found for %s, "
173 "ignoring %s-%s message...\n", vlr_subscr_name(vsub),
174 msg_name, msg_is_err ? "Err" : "Res");
175 return -EIO; /* TODO: notify sender about that? */
176 }
177
178 /* Attempt to find DTAP-transaction */
179 trans = trans_find_by_sm_rp_mr(conn, *(gsup_msg->sm_rp_mr));
180 if (!trans) {
181 LOGP(DLSMS, LOGL_NOTICE, "No transaction found for %s, "
182 "ignoring %s-%s message...\n", vlr_subscr_name(vsub),
183 msg_name, msg_is_err ? "Err" : "Res");
184 return -EIO; /* TODO: notify sender about that? */
185 }
186
187 /* Send either RP-ERROR, or RP-ACK */
188 if (msg_is_err) {
189 /* TODO: handle optional SM-RP-UI payload (requires API change) */
190 gsm411_send_rp_error(trans, *(gsup_msg->sm_rp_mr),
191 *(gsup_msg->sm_rp_cause));
192 } else {
193 gsm411_send_rp_ack(trans, *(gsup_msg->sm_rp_mr));
194 }
195
196 return 0;
197
198msg_error:
199 /* TODO: notify sender about that? */
200 LOGP(DLSMS, LOGL_NOTICE, "RX malformed %s-%s\n",
201 msg_name, msg_is_err ? "Err" : "Res");
202 return -EINVAL;
203}