blob: df834ebe69ff2ca5b16efd356c2e8c4e763efda3 [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 */
47struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg,
48 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",
57 osmo_ipa_name_to_str(from_peer));
58 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) {
73 LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_ipa_name_to_str(from_peer), rc);
74 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) {
81 if (osmo_ipa_name_set(&req->source_name, req->gsup.source_name, req->gsup.source_name_len)) {
82 LOGP(DLGSUP, LOGL_ERROR,
83 "Rx GSUP from %s: failed to decode source_name, message is not routable\n",
84 osmo_ipa_name_to_str(from_peer));
85 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
86 return NULL;
87 }
88
89 /* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source.
90 */
91 if (osmo_ipa_name_cmp(&req->source_name, from_peer))
92 req->via_proxy = *from_peer;
93 }
94
95 if (!osmo_imsi_str_valid(req->gsup.imsi)) {
96 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "invalid IMSI: %s",
97 osmo_quote_str(req->gsup.imsi, -1));
98 return NULL;
99 }
100
101 if (add_to_list)
102 llist_add_tail(&req->entry, add_to_list);
103 return req;
104}
105
106void osmo_gsup_req_free(struct osmo_gsup_req *req)
107{
108 LOG_GSUP_REQ(req, LOGL_DEBUG, "free\n");
109 if (req->msg)
110 msgb_free(req->msg);
111 if (req->entry.prev)
112 llist_del(&req->entry);
113 talloc_free(req);
114}
115
116int _osmo_gsup_req_respond(struct osmo_gsup_req *req, struct osmo_gsup_message *response,
117 bool error, bool final_response, const char *file, int line)
118{
119 int rc;
120
121 rc = osmo_gsup_make_response(response, &req->gsup, error, final_response);
122 if (rc) {
123 LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "Invalid response (rc=%d): {%s}\n",
124 rc, osmo_gsup_message_to_str_c(OTC_SELECT, response));
125 rc = -EINVAL;
126 goto exit_cleanup;
127 }
128
129 if (!req->send_response_cb) {
130 LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "No send_response_cb set, cannot send: {%s}\n",
131 osmo_gsup_message_to_str_c(OTC_SELECT, response));
132 rc = -EINVAL;
133 goto exit_cleanup;
134 }
135
136 LOG_GSUP_REQ_SRC(req, LOGL_DEBUG, file, line, "Tx response: {%s}\n",
137 osmo_gsup_message_to_str_c(OTC_SELECT, response));
138 req->send_response_cb(req, response);
139
140exit_cleanup:
141 if (final_response)
142 osmo_gsup_req_free(req);
143 return rc;
144}
145
146int _osmo_gsup_req_respond_msgt(struct osmo_gsup_req *req, enum osmo_gsup_message_type message_type,
147 bool final_response, const char *file, int line)
148{
149 struct osmo_gsup_message response = {
150 .message_type = message_type,
151 };
152 return _osmo_gsup_req_respond(req, &response, OSMO_GSUP_IS_MSGT_ERROR(message_type), final_response,
153 file, line);
154}
155
156void _osmo_gsup_req_respond_err(struct osmo_gsup_req *req, enum gsm48_gmm_cause cause,
157 const char *file, int line)
158{
159 struct osmo_gsup_message response = {
160 .cause = cause,
161 };
162
163 /* No need to answer if we couldn't parse an ERROR message type, only REQUESTs need an error reply. */
164 if (!OSMO_GSUP_IS_MSGT_REQUEST(req->gsup.message_type)) {
165 osmo_gsup_req_free(req);
166 return;
167 }
168
169 osmo_gsup_req_respond(req, &response, true, true);
170}
171
172/*! This function is implicitly called by the osmo_gsup_req API, if at all possible rather use osmo_gsup_req_respond().
173 * This function is non-static mostly to allow unit testing.
174 *
175 * Set fields, if still unset, that need to be copied from a received message over to its response message, to ensure
176 * the response can be routed back to the requesting peer even via GSUP proxies.
177 *
178 * Note: after calling this function, fields in the reply may reference the same memory as rx and are not deep-copied,
179 * as is the usual way we are handling decoded GSUP messages.
180 *
181 * These fields are set in the reply message, iff they are still unset:
182 * - Set reply->message_type to the rx's matching RESULT code (or ERROR code if error == true).
183 * - IMSI,
184 * - Set reply->destination_name to rx->source_name (for proxy routing),
185 * - sm_rp_mr (for SMS),
186 * - session_id (for SS/USSD),
187 * - if rx->session_state is not NONE, set tx->session_state depending on the final_response argument:
188 * If false, set to OSMO_GSUP_SESSION_STATE_CONTINUE, else OSMO_GSUP_SESSION_STATE_END.
189 *
190 * If values in reply are already set, they will not be overwritten. The return code is an optional way of finding out
191 * whether all values that were already set in 'reply' are indeed matching the 'rx' values that would have been set.
192 *
193 * \param[in] rx Received GSUP message that is being replied to.
194 * \param[inout] reply The message that should be the response to rx, either empty or with some values already set up.
195 * \return 0 if the resulting message is a valid response for rx, nonzero otherwise. A nonzero rc has no effect on the
196 * values set in the reply message: all unset fields are first updated, and then the rc is determined.
197 * The rc is intended to merely warn if the reply message already contained data that is incompatible with rx,
198 * e.g. a mismatching IMSI.
199 */
200int osmo_gsup_make_response(struct osmo_gsup_message *reply,
201 const struct osmo_gsup_message *rx, bool error, bool final_response)
202{
203 int rc = 0;
204
205 if (!reply->message_type) {
206 if (error)
207 reply->message_type = OSMO_GSUP_TO_MSGT_ERROR(rx->message_type);
208 else
209 reply->message_type = OSMO_GSUP_TO_MSGT_RESULT(rx->message_type);
210 }
211
212 if (*reply->imsi == '\0')
213 OSMO_STRLCPY_ARRAY(reply->imsi, rx->imsi);
214
215 if (reply->message_class == OSMO_GSUP_MESSAGE_CLASS_UNSET)
216 reply->message_class = rx->message_class;
217
218 if (!reply->destination_name || !reply->destination_name_len) {
219 reply->destination_name = rx->source_name;
220 reply->destination_name_len = rx->source_name_len;
221 }
222
223 /* RP-Message-Reference is mandatory for SM Service */
224 if (!reply->sm_rp_mr)
225 reply->sm_rp_mr = rx->sm_rp_mr;
226
227 /* For SS/USSD, it's important to keep both session state and ID IEs */
228 if (!reply->session_id)
229 reply->session_id = rx->session_id;
230 if (rx->session_state != OSMO_GSUP_SESSION_STATE_NONE
231 && reply->session_state == OSMO_GSUP_SESSION_STATE_NONE) {
232 if (final_response || rx->session_state == OSMO_GSUP_SESSION_STATE_END)
233 reply->session_state = OSMO_GSUP_SESSION_STATE_END;
234 else
235 reply->session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
236 }
237
238 if (strcmp(reply->imsi, rx->imsi))
239 rc |= 1 << 0;
240 if (reply->message_class != rx->message_class)
241 rc |= 1 << 1;
242 if (rx->sm_rp_mr && (!reply->sm_rp_mr || *rx->sm_rp_mr != *reply->sm_rp_mr))
243 rc |= 1 << 2;
244 if (reply->session_id != rx->session_id)
245 rc |= 1 << 3;
246 return rc;
247}
248
249/*! Print the most important value of a GSUP message to a string buffer in human readable form.
250 * \param[out] buf The buffer to write to.
251 * \param[out] buflen sizeof(buf).
252 * \param[in] msg GSUP message to print.
253 */
254size_t osmo_gsup_message_to_str_buf(char *buf, size_t buflen, const struct osmo_gsup_message *msg)
255{
256 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
257 if (!msg) {
258 OSMO_STRBUF_PRINTF(sb, "NULL");
259 return sb.chars_needed;
260 }
261
262 if (msg->message_class)
263 OSMO_STRBUF_PRINTF(sb, "%s ", osmo_gsup_message_class_name(msg->message_class));
264
265 OSMO_STRBUF_PRINTF(sb, "%s:", osmo_gsup_message_type_name(msg->message_type));
266
267 OSMO_STRBUF_PRINTF(sb, " imsi=");
268 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, msg->imsi, strnlen(msg->imsi, sizeof(msg->imsi)));
269
270 if (msg->cause)
271 OSMO_STRBUF_PRINTF(sb, " cause=%s", get_value_string(gsm48_gmm_cause_names, msg->cause));
272
273 switch (msg->cn_domain) {
274 case OSMO_GSUP_CN_DOMAIN_CS:
275 OSMO_STRBUF_PRINTF(sb, " cn_domain=CS");
276 break;
277 case OSMO_GSUP_CN_DOMAIN_PS:
278 OSMO_STRBUF_PRINTF(sb, " cn_domain=PS");
279 break;
280 default:
281 if (msg->cn_domain)
282 OSMO_STRBUF_PRINTF(sb, " cn_domain=?(%d)", msg->cn_domain);
283 break;
284 }
285
286 if (msg->source_name_len) {
287 OSMO_STRBUF_PRINTF(sb, " source_name=");
288 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->source_name, msg->source_name_len);
289 }
290
291 if (msg->destination_name_len) {
292 OSMO_STRBUF_PRINTF(sb, " destination_name=");
293 OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->destination_name, msg->destination_name_len);
294 }
295
296 if (msg->session_id)
297 OSMO_STRBUF_PRINTF(sb, " session_id=%" PRIu32, msg->session_id);
298 if (msg->session_state)
299 OSMO_STRBUF_PRINTF(sb, " session_state=%s", osmo_gsup_session_state_name(msg->session_state));
300
301 if (msg->sm_rp_mr)
302 OSMO_STRBUF_PRINTF(sb, " sm_rp_mr=%" PRIu8, *msg->sm_rp_mr);
303
304 return sb.chars_needed;
305}
306
307/*! Same as osmo_gsup_message_to_str_buf() but returns a talloc allocated string. */
308char *osmo_gsup_message_to_str_c(void *ctx, const struct osmo_gsup_message *msg)
309{
310 OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_gsup_message_to_str_buf, msg)
311}