blob: 7cefbdf96d88d0d290aafc738efc73492966084b [file] [log] [blame]
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +01001/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <errno.h>
20#include <inttypes.h>
21
22#include <osmocom/core/logging.h>
23#include <osmocom/gsm/gsm23003.h>
24
25#include <osmocom/gsupclient/gsup_req.h>
26
27/*! Create a new osmo_gsup_req record, decode GSUP and add to a provided list of requests.
28 * This function takes ownership of the msgb, which will, on success, be owned by the returned osmo_gsup_req instance
29 * until osmo_gsup_req_free(). If a decoding error occurs, send an error response immediately, and return NULL.
30 *
31 * When this function returns, the original sender is found in req->source_name. If this is not the immediate peer name,
32 * then req->via_proxy is set to the immediate peer, and it is the responsibility of the caller to add req->source_name
33 * to the GSUP routes that are serviced by req->via_proxy (usually not relevant for clients with a single GSUP conn).
34 *
35 * Note: osmo_gsup_req API makes use of OTC_SELECT to allocate volatile buffers for logging. Use of
36 * osmo_select_main_ctx() is mandatory when using osmo_gsup_req.
37 *
38 * \param[in] ctx Talloc context for allocation of the new request.
39 * \param[in] from_peer The IPA unit name of the immediate GSUP peer from which this msgb was received.
40 * \param[in] msg The GSUP message buffer.
41 * \param[in] send_response_cb User specific method to send a GSUP response message, invoked upon
42 * osmo_gsup_req_respond*() functions.
43 * \param[inout] cb_data Context data to be used freely by the caller.
44 * \param[inout] add_to_list List to which to append this request, or NULL for no list.
45 * \return a newly allocated osmo_gsup_req, or NULL on error.
46 */
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010047struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_gsup_peer_id *from_peer, struct msgb *msg,
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010048 osmo_gsup_req_send_response_t send_response_cb, void *cb_data,
49 struct llist_head *add_to_list)
50{
51 static unsigned int next_req_nr = 1;
52 struct osmo_gsup_req *req;
53 int rc;
54
55 if (!msgb_l2(msg) || !msgb_l2len(msg)) {
56 LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: missing or empty L2 data\n",
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010057 osmo_gsup_peer_id_to_str(from_peer));
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010058 msgb_free(msg);
59 return NULL;
60 }
61
62 req = talloc_zero(ctx, struct osmo_gsup_req);
63 OSMO_ASSERT(req);
64 /* Note: req->gsup is declared const, so that the incoming message cannot be modified by handlers. */
65 req->nr = next_req_nr++;
66 req->msg = msg;
67 req->send_response_cb = send_response_cb;
68 req->cb_data = cb_data;
69 if (from_peer)
70 req->source_name = *from_peer;
71 rc = osmo_gsup_decode(msgb_l2(req->msg), msgb_l2len(req->msg), (struct osmo_gsup_message*)&req->gsup);
72 if (rc < 0) {
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010073 LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_gsup_peer_id_to_str(from_peer), rc);
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010074 osmo_gsup_req_free(req);
75 return NULL;
76 }
77
78 LOG_GSUP_REQ(req, LOGL_DEBUG, "new request: {%s}\n", osmo_gsup_message_to_str_c(OTC_SELECT, &req->gsup));
79
80 if (req->gsup.source_name_len) {
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010081 if (osmo_gsup_peer_id_set(&req->source_name, OSMO_GSUP_PEER_ID_IPA_NAME,
82 req->gsup.source_name, req->gsup.source_name_len)) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010083 LOGP(DLGSUP, LOGL_ERROR,
84 "Rx GSUP from %s: failed to decode source_name, message is not routable\n",
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010085 osmo_gsup_peer_id_to_str(from_peer));
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010086 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
87 return NULL;
88 }
89
90 /* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source.
91 */
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010092 if (osmo_gsup_peer_id_cmp(&req->source_name, from_peer))
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010093 req->via_proxy = *from_peer;
94 }
95
96 if (!osmo_imsi_str_valid(req->gsup.imsi)) {
97 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "invalid IMSI: %s",
98 osmo_quote_str(req->gsup.imsi, -1));
99 return NULL;
100 }
101
102 if (add_to_list)
103 llist_add_tail(&req->entry, add_to_list);
104 return req;
105}
106
107void osmo_gsup_req_free(struct osmo_gsup_req *req)
108{
109 LOG_GSUP_REQ(req, LOGL_DEBUG, "free\n");
110 if (req->msg)
111 msgb_free(req->msg);
112 if (req->entry.prev)
113 llist_del(&req->entry);
114 talloc_free(req);
115}
116
117int _osmo_gsup_req_respond(struct osmo_gsup_req *req, struct osmo_gsup_message *response,
118 bool error, bool final_response, const char *file, int line)
119{
120 int rc;
121
122 rc = osmo_gsup_make_response(response, &req->gsup, error, final_response);
123 if (rc) {
124 LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "Invalid response (rc=%d): {%s}\n",
125 rc, osmo_gsup_message_to_str_c(OTC_SELECT, response));
126 rc = -EINVAL;
127 goto exit_cleanup;
128 }
129
130 if (!req->send_response_cb) {
131 LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "No send_response_cb set, cannot send: {%s}\n",
132 osmo_gsup_message_to_str_c(OTC_SELECT, response));
133 rc = -EINVAL;
134 goto exit_cleanup;
135 }
136
137 LOG_GSUP_REQ_SRC(req, LOGL_DEBUG, file, line, "Tx response: {%s}\n",
138 osmo_gsup_message_to_str_c(OTC_SELECT, response));
139 req->send_response_cb(req, response);
140
141exit_cleanup:
142 if (final_response)
143 osmo_gsup_req_free(req);
144 return rc;
145}
146
147int _osmo_gsup_req_respond_msgt(struct osmo_gsup_req *req, enum osmo_gsup_message_type message_type,
148 bool final_response, const char *file, int line)
149{
150 struct osmo_gsup_message response = {
151 .message_type = message_type,
152 };
153 return _osmo_gsup_req_respond(req, &response, OSMO_GSUP_IS_MSGT_ERROR(message_type), final_response,
154 file, line);
155}
156
157void _osmo_gsup_req_respond_err(struct osmo_gsup_req *req, enum gsm48_gmm_cause cause,
158 const char *file, int line)
159{
160 struct osmo_gsup_message response = {
161 .cause = cause,
162 };
163
164 /* No need to answer if we couldn't parse an ERROR message type, only REQUESTs need an error reply. */
165 if (!OSMO_GSUP_IS_MSGT_REQUEST(req->gsup.message_type)) {
166 osmo_gsup_req_free(req);
167 return;
168 }
169
170 osmo_gsup_req_respond(req, &response, true, true);
171}
172
173/*! This function is implicitly called by the osmo_gsup_req API, if at all possible rather use osmo_gsup_req_respond().
174 * This function is non-static mostly to allow unit testing.
175 *
176 * Set fields, if still unset, that need to be copied from a received message over to its response message, to ensure
177 * the response can be routed back to the requesting peer even via GSUP proxies.
178 *
179 * Note: after calling this function, fields in the reply may reference the same memory as rx and are not deep-copied,
180 * as is the usual way we are handling decoded GSUP messages.
181 *
182 * These fields are set in the reply message, iff they are still unset:
183 * - Set reply->message_type to the rx's matching RESULT code (or ERROR code if error == true).
184 * - IMSI,
185 * - Set reply->destination_name to rx->source_name (for proxy routing),
186 * - sm_rp_mr (for SMS),
187 * - session_id (for SS/USSD),
188 * - if rx->session_state is not NONE, set tx->session_state depending on the final_response argument:
189 * If false, set to OSMO_GSUP_SESSION_STATE_CONTINUE, else OSMO_GSUP_SESSION_STATE_END.
190 *
191 * If values in reply are already set, they will not be overwritten. The return code is an optional way of finding out
192 * whether all values that were already set in 'reply' are indeed matching the 'rx' values that would have been set.
193 *
194 * \param[in] rx Received GSUP message that is being replied to.
195 * \param[inout] reply The message that should be the response to rx, either empty or with some values already set up.
196 * \return 0 if the resulting message is a valid response for rx, nonzero otherwise. A nonzero rc has no effect on the
197 * values set in the reply message: all unset fields are first updated, and then the rc is determined.
198 * The rc is intended to merely warn if the reply message already contained data that is incompatible with rx,
199 * e.g. a mismatching IMSI.
200 */
201int osmo_gsup_make_response(struct osmo_gsup_message *reply,
202 const struct osmo_gsup_message *rx, bool error, bool final_response)
203{
204 int rc = 0;
205
206 if (!reply->message_type) {
207 if (error)
208 reply->message_type = OSMO_GSUP_TO_MSGT_ERROR(rx->message_type);
209 else
210 reply->message_type = OSMO_GSUP_TO_MSGT_RESULT(rx->message_type);
211 }
212
213 if (*reply->imsi == '\0')
214 OSMO_STRLCPY_ARRAY(reply->imsi, rx->imsi);
215
216 if (reply->message_class == OSMO_GSUP_MESSAGE_CLASS_UNSET)
217 reply->message_class = rx->message_class;
218
219 if (!reply->destination_name || !reply->destination_name_len) {
220 reply->destination_name = rx->source_name;
221 reply->destination_name_len = rx->source_name_len;
222 }
223
224 /* RP-Message-Reference is mandatory for SM Service */
225 if (!reply->sm_rp_mr)
226 reply->sm_rp_mr = rx->sm_rp_mr;
227
228 /* For SS/USSD, it's important to keep both session state and ID IEs */
229 if (!reply->session_id)
230 reply->session_id = rx->session_id;
231 if (rx->session_state != OSMO_GSUP_SESSION_STATE_NONE
232 && reply->session_state == OSMO_GSUP_SESSION_STATE_NONE) {
233 if (final_response || rx->session_state == OSMO_GSUP_SESSION_STATE_END)
234 reply->session_state = OSMO_GSUP_SESSION_STATE_END;
235 else
236 reply->session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
237 }
238
239 if (strcmp(reply->imsi, rx->imsi))
240 rc |= 1 << 0;
241 if (reply->message_class != rx->message_class)
242 rc |= 1 << 1;
243 if (rx->sm_rp_mr && (!reply->sm_rp_mr || *rx->sm_rp_mr != *reply->sm_rp_mr))
244 rc |= 1 << 2;
245 if (reply->session_id != rx->session_id)
246 rc |= 1 << 3;
247 return rc;
248}
249
250/*! Print the most important value of a GSUP message to a string buffer in human readable form.
251 * \param[out] buf The buffer to write to.
252 * \param[out] buflen sizeof(buf).
253 * \param[in] msg GSUP message to print.
254 */
255size_t osmo_gsup_message_to_str_buf(char *buf, size_t buflen, const struct osmo_gsup_message *msg)
256{
257 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
258 if (!msg) {
259 OSMO_STRBUF_PRINTF(sb, "NULL");
260 return sb.chars_needed;
261 }
262
263 if (msg->message_class)
264 OSMO_STRBUF_PRINTF(sb, "%s ", osmo_gsup_message_class_name(msg->message_class));
265
266 OSMO_STRBUF_PRINTF(sb, "%s:", osmo_gsup_message_type_name(msg->message_type));
267
268 OSMO_STRBUF_PRINTF(sb, " imsi=");
269 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, msg->imsi, strnlen(msg->imsi, sizeof(msg->imsi)));
270
271 if (msg->cause)
272 OSMO_STRBUF_PRINTF(sb, " cause=%s", get_value_string(gsm48_gmm_cause_names, msg->cause));
273
274 switch (msg->cn_domain) {
275 case OSMO_GSUP_CN_DOMAIN_CS:
276 OSMO_STRBUF_PRINTF(sb, " cn_domain=CS");
277 break;
278 case OSMO_GSUP_CN_DOMAIN_PS:
279 OSMO_STRBUF_PRINTF(sb, " cn_domain=PS");
280 break;
281 default:
282 if (msg->cn_domain)
283 OSMO_STRBUF_PRINTF(sb, " cn_domain=?(%d)", msg->cn_domain);
284 break;
285 }
286
287 if (msg->source_name_len) {
288 OSMO_STRBUF_PRINTF(sb, " source_name=");
289 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->source_name, msg->source_name_len);
290 }
291
292 if (msg->destination_name_len) {
293 OSMO_STRBUF_PRINTF(sb, " destination_name=");
294 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->destination_name, msg->destination_name_len);
295 }
296
297 if (msg->session_id)
298 OSMO_STRBUF_PRINTF(sb, " session_id=%" PRIu32, msg->session_id);
299 if (msg->session_state)
300 OSMO_STRBUF_PRINTF(sb, " session_state=%s", osmo_gsup_session_state_name(msg->session_state));
301
302 if (msg->sm_rp_mr)
303 OSMO_STRBUF_PRINTF(sb, " sm_rp_mr=%" PRIu8, *msg->sm_rp_mr);
304
305 return sb.chars_needed;
306}
307
308/*! Same as osmo_gsup_message_to_str_buf() but returns a talloc allocated string. */
309char *osmo_gsup_message_to_str_c(void *ctx, const struct osmo_gsup_message *msg)
310{
311 OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_gsup_message_to_str_buf, msg)
312}