Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 1 | /* 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 | * |
| 29 | * Rationales: |
| 30 | * |
| 31 | * - osmo_gsup_req makes it easy to handle GSUP requests asynchronously. Before this, a GSUP message struct would be |
| 32 | * valid only within a read callback function, and would not survive asynchronous handling, because the struct often |
| 33 | * points directly into the received msgb. An osmo_gsup_req takes ownership of the msgb and ensures that the data |
| 34 | * remains valid, so that it can easily be queued for later handling. |
| 35 | * - osmo_gsup_req unifies the composition of response messages to ensure that all IEs that identify it to belong to |
| 36 | * the initial request are preserved / derived, like the source_name, destination_name, session_id, etc (see |
| 37 | * osmo_gsup_make_response() for details). |
| 38 | * - Deallocation of an osmo_gsup_req is implicit upon sending a response. The idea is that msgb memory leaks are a |
| 39 | * recurring source of bugs. By enforcing a request-response relation with implicit deallocation, osmo_gsup_req aims |
| 40 | * to help avoid most such memory leaks implicitly. |
| 41 | * |
| 42 | * The typical GSUP message sequence is: |
| 43 | * -> rx request, |
| 44 | * <- tx response. |
| 45 | * |
| 46 | * With osmo_gsup_req we can easily expand to: |
| 47 | * -> rx request, |
| 48 | * ... wait asynchronously, |
| 49 | * <- tx response. |
| 50 | * |
| 51 | * Only few GSUP conversations go beyond a 1:1 request-response match. But some have a session (e.g. USSD) or more |
| 52 | * negotiation may happen before the initial request is completed (e.g. Update Location with interleaved Insert |
| 53 | * Subscriber Data), so osmo_gsup_req also allows passing non-final responses. |
| 54 | * The final_response flag allows for: |
| 55 | * -> rx request, |
| 56 | * ... wait async, |
| 57 | * <- tx intermediate message to same peer (final_response = false, req remains open), |
| 58 | * ... wait async, |
| 59 | * -> rx intermediate response, |
| 60 | * ... wait async, |
| 61 | * <- tx final response (final_response = true, req is deallocated). |
| 62 | * |
| 63 | * This function takes ownership of the msgb, which will, on success, be owned by the returned osmo_gsup_req instance |
| 64 | * until osmo_gsup_req_free(). If a decoding error occurs, send an error response immediately, and return NULL. |
| 65 | * |
| 66 | * The original CNI entity that sent the message is found in req->source_name. If the message was passed on by an |
| 67 | * intermediate CNI peer, then req->via_proxy is set to the immediate peer, and it is the responsibility of the caller |
| 68 | * to add req->source_name to the GSUP routes that are serviced by req->via_proxy (usually not relevant for clients with |
| 69 | * a single GSUP conn). |
| 70 | * Examples: |
| 71 | * |
| 72 | * "msc" ---> here |
| 73 | * source_name = "msc" |
| 74 | * via_proxy = <empty> |
| 75 | * |
| 76 | * "msc" ---> "proxy-HLR" ---> here (e.g. home HLR) |
| 77 | * source_name = "msc" |
| 78 | * via_proxy = "proxy-HLR" |
| 79 | * |
| 80 | * "msc" ---> "proxy-HLR" ---> "home-HLR" ---> here (e.g. EUSE) |
| 81 | * source_name = "msc" |
| 82 | * via_proxy = "home-HLR" |
| 83 | * |
| 84 | * An osmo_gsup_req must be concluded (and deallocated) by calling one of the osmo_gsup_req_respond* functions. |
| 85 | * |
| 86 | * Note: osmo_gsup_req API makes use of OTC_SELECT to allocate volatile buffers for logging. Use of |
| 87 | * osmo_select_main_ctx() is mandatory when using osmo_gsup_req. |
| 88 | * |
| 89 | * \param[in] ctx Talloc context for allocation of the new request. |
| 90 | * \param[in] from_peer The IPA unit name of the immediate GSUP peer from which this msgb was received. |
| 91 | * \param[in] msg The message buffer containing the received GSUP message, where msgb_l2() shall point to the GSUP |
| 92 | * message start. The caller no longer owns the msgb when it is passed to this function: on error, the |
| 93 | * msgb is freed immediately, and on success, the msgb is owned by the returned osmo_gsup_req. |
| 94 | * \param[in] send_response_cb User specific method to send a GSUP response message, invoked upon |
| 95 | * osmo_gsup_req_respond*() functions. Typically this invokes encoding and transmitting the |
| 96 | * GSUP message over a network socket. See for example gsup_server_send_req_response(). |
| 97 | * \param[inout] cb_data Context data to be used freely by the caller. |
| 98 | * \param[inout] add_to_list List to which to append this request, or NULL for no list. |
| 99 | * \return a newly allocated osmo_gsup_req, or NULL on error. If NULL is returned, an error response has already been |
| 100 | * dispatched to the send_response_cb. |
| 101 | */ |
| 102 | struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg, |
| 103 | osmo_gsup_req_send_response_t send_response_cb, void *cb_data, |
| 104 | struct llist_head *add_to_list) |
| 105 | { |
| 106 | static unsigned int next_req_nr = 1; |
| 107 | struct osmo_gsup_req *req; |
| 108 | int rc; |
| 109 | |
| 110 | if (!msgb_l2(msg) || !msgb_l2len(msg)) { |
| 111 | LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: missing or empty L2 data\n", |
| 112 | osmo_ipa_name_to_str(from_peer)); |
| 113 | msgb_free(msg); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | req = talloc_zero(ctx, struct osmo_gsup_req); |
| 118 | OSMO_ASSERT(req); |
| 119 | /* Note: req->gsup is declared const, so that the incoming message cannot be modified by handlers. */ |
| 120 | req->nr = next_req_nr++; |
| 121 | req->msg = msg; |
| 122 | req->send_response_cb = send_response_cb; |
| 123 | req->cb_data = cb_data; |
| 124 | if (from_peer) |
| 125 | req->source_name = *from_peer; |
| 126 | rc = osmo_gsup_decode(msgb_l2(req->msg), msgb_l2len(req->msg), (struct osmo_gsup_message*)&req->gsup); |
| 127 | if (rc < 0) { |
| 128 | LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_ipa_name_to_str(from_peer), rc); |
| 129 | osmo_gsup_req_free(req); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | LOG_GSUP_REQ(req, LOGL_DEBUG, "new request: {%s}\n", osmo_gsup_message_to_str_c(OTC_SELECT, &req->gsup)); |
| 134 | |
| 135 | if (req->gsup.source_name_len) { |
| 136 | if (osmo_ipa_name_set(&req->source_name, req->gsup.source_name, req->gsup.source_name_len)) { |
| 137 | LOGP(DLGSUP, LOGL_ERROR, |
| 138 | "Rx GSUP from %s: failed to decode source_name, message is not routable\n", |
| 139 | osmo_ipa_name_to_str(from_peer)); |
| 140 | osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | /* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source. |
| 145 | */ |
| 146 | if (osmo_ipa_name_cmp(&req->source_name, from_peer)) |
| 147 | req->via_proxy = *from_peer; |
| 148 | } |
| 149 | |
| 150 | if (!osmo_imsi_str_valid(req->gsup.imsi)) { |
| 151 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "invalid IMSI: %s", |
| 152 | osmo_quote_str(req->gsup.imsi, -1)); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | if (add_to_list) |
| 157 | llist_add_tail(&req->entry, add_to_list); |
| 158 | return req; |
| 159 | } |
| 160 | |
| 161 | /*! Free an osmo_gsup_req and its msgb -- this is usually implicit in osmo_gsup_req_resond_*(), it should not be |
| 162 | * necessary to call this directly. */ |
| 163 | void osmo_gsup_req_free(struct osmo_gsup_req *req) |
| 164 | { |
| 165 | LOG_GSUP_REQ(req, LOGL_DEBUG, "free\n"); |
| 166 | if (req->msg) |
| 167 | msgb_free(req->msg); |
| 168 | if (req->entry.prev) |
| 169 | llist_del(&req->entry); |
| 170 | talloc_free(req); |
| 171 | } |
| 172 | |
| 173 | /*! Send a response to a GSUP request. |
| 174 | * |
| 175 | * Ensure that the response message contains all GSUP IEs that identify it as a response for the request req, by calling |
| 176 | * osmo_gsup_make_response(). |
| 177 | * |
| 178 | * The final complete response message is passed to req->send_response_cb() to take care of the transmission. |
| 179 | * |
| 180 | * \param req Request as previously initialized by osmo_gsup_req_new(). |
| 181 | * \param response Buffer to compose the response, possibly with some pre-configured IEs. |
| 182 | * Any missing IEs are added via osmo_gsup_make_response(). |
| 183 | * Must not be NULL. Does not need to remain valid memory beyond the function call, |
| 184 | * i.e. this can just be a local variable in the calling function. |
| 185 | * \param error True when the response message indicates an error response (error message type). |
| 186 | * \param final_response True when the request is concluded by this response, which deallocates the req. |
| 187 | * False when the request should remain open after this response. |
| 188 | * For most plain request->response GSUP messages, this should be True. |
| 189 | * \param file Source file for logging as in __FILE__, added by osmo_gsup_req_respond() macro. |
| 190 | * \param line Source line for logging as in __LINE__, added by osmo_gsup_req_respond() macro. |
| 191 | */ |
| 192 | int _osmo_gsup_req_respond(struct osmo_gsup_req *req, struct osmo_gsup_message *response, |
| 193 | bool error, bool final_response, const char *file, int line) |
| 194 | { |
| 195 | int rc; |
| 196 | |
| 197 | rc = osmo_gsup_make_response(response, &req->gsup, error, final_response); |
| 198 | if (rc) { |
| 199 | LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "Invalid response (rc=%d): {%s}\n", |
| 200 | rc, osmo_gsup_message_to_str_c(OTC_SELECT, response)); |
| 201 | rc = -EINVAL; |
| 202 | goto exit_cleanup; |
| 203 | } |
| 204 | |
| 205 | if (!req->send_response_cb) { |
| 206 | LOG_GSUP_REQ_SRC(req, LOGL_ERROR, file, line, "No send_response_cb set, cannot send: {%s}\n", |
| 207 | osmo_gsup_message_to_str_c(OTC_SELECT, response)); |
| 208 | rc = -EINVAL; |
| 209 | goto exit_cleanup; |
| 210 | } |
| 211 | |
| 212 | LOG_GSUP_REQ_SRC(req, LOGL_DEBUG, file, line, "Tx response: {%s}\n", |
| 213 | osmo_gsup_message_to_str_c(OTC_SELECT, response)); |
| 214 | req->send_response_cb(req, response); |
| 215 | |
| 216 | exit_cleanup: |
| 217 | if (final_response) |
| 218 | osmo_gsup_req_free(req); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | /*! Shorthand for _osmo_gsup_req_respond() with no additional IEs and a fixed message type. |
| 223 | * Set the message type in a local osmo_gsup_message and feed it to _osmo_gsup_req_respond(). |
| 224 | * That will ensure to add all IEs that identify it as a response to req. |
| 225 | * |
| 226 | * \param req Request as previously initialized by osmo_gsup_req_new(). |
| 227 | * \param message_type The GSUP message type discriminator to respond with. |
| 228 | * \param final_response True when the request is concluded by this response, which deallocates the req. |
| 229 | * False when the request should remain open after this response. |
| 230 | * For most plain request->response GSUP messages, this should be True. |
| 231 | * \param file Source file for logging as in __FILE__, added by osmo_gsup_req_respond_msgt() macro. |
| 232 | * \param line Source line for logging as in __LINE__, added by osmo_gsup_req_respond_msgt() macro. |
| 233 | */ |
| 234 | int _osmo_gsup_req_respond_msgt(struct osmo_gsup_req *req, enum osmo_gsup_message_type message_type, |
| 235 | bool final_response, const char *file, int line) |
| 236 | { |
| 237 | struct osmo_gsup_message response = { |
| 238 | .message_type = message_type, |
| 239 | }; |
| 240 | return _osmo_gsup_req_respond(req, &response, OSMO_GSUP_IS_MSGT_ERROR(message_type), final_response, |
| 241 | file, line); |
| 242 | } |
| 243 | |
| 244 | /*! Shorthand for _osmo_gsup_req_respond() with an error cause IEs and using the req's matched error message type. |
| 245 | * Set the error cause in a local osmo_gsup_message and feed it to _osmo_gsup_req_respond(). |
| 246 | * That will ensure to add all IEs that identify it as a response to req. |
| 247 | * |
| 248 | * Responding with an error always implies a final response: req is implicitly deallocated. |
| 249 | * |
| 250 | * \param req Request as previously initialized by osmo_gsup_req_new(). |
| 251 | * \param cause The error cause to include in a OSMO_GSUP_CAUSE_IE. |
| 252 | * \param file Source file for logging as in __FILE__, added by osmo_gsup_req_respond_err() macro. |
| 253 | * \param line Source line for logging as in __LINE__, added by osmo_gsup_req_respond_err() macro. |
| 254 | */ |
| 255 | void _osmo_gsup_req_respond_err(struct osmo_gsup_req *req, enum gsm48_gmm_cause cause, |
| 256 | const char *file, int line) |
| 257 | { |
| 258 | struct osmo_gsup_message response = { |
| 259 | .cause = cause, |
| 260 | }; |
| 261 | |
| 262 | /* No need to answer if we couldn't parse an ERROR message type, only REQUESTs need an error reply. */ |
| 263 | if (!OSMO_GSUP_IS_MSGT_REQUEST(req->gsup.message_type)) { |
| 264 | osmo_gsup_req_free(req); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | osmo_gsup_req_respond(req, &response, true, true); |
| 269 | } |
| 270 | |
| 271 | /*! This function is implicitly called by the osmo_gsup_req API, if at all possible rather use osmo_gsup_req_respond(). |
| 272 | * This function is non-static mostly to allow unit testing. |
| 273 | * |
| 274 | * Set fields, if still unset, that need to be copied from a received message over to its response message, to ensure |
| 275 | * the response can be routed back to the requesting peer even via GSUP proxies. |
| 276 | * |
| 277 | * Note: after calling this function, fields in the reply may reference the same memory as rx and are not deep-copied, |
| 278 | * as is the usual way we are handling decoded GSUP messages. |
| 279 | * |
| 280 | * These fields are set in the reply message, iff they are still unset: |
| 281 | * - Set reply->message_type to the rx's matching RESULT code (or ERROR code if error == true). |
| 282 | * - IMSI, |
| 283 | * - Set reply->destination_name to rx->source_name (for proxy routing), |
| 284 | * - sm_rp_mr (for SMS), |
| 285 | * - session_id (for SS/USSD), |
| 286 | * - if rx->session_state is not NONE, set tx->session_state depending on the final_response argument: |
| 287 | * If false, set to OSMO_GSUP_SESSION_STATE_CONTINUE, else OSMO_GSUP_SESSION_STATE_END. |
| 288 | * |
| 289 | * If values in reply are already set, they will not be overwritten. The return code is an optional way of finding out |
| 290 | * whether all values that were already set in 'reply' are indeed matching the 'rx' values that would have been set. |
| 291 | * |
| 292 | * \param[in] rx Received GSUP message that is being replied to. |
| 293 | * \param[inout] reply The message that should be the response to rx, either empty or with some values already set up. |
| 294 | * \return 0 if the resulting message is a valid response for rx, nonzero otherwise. A nonzero rc has no effect on the |
| 295 | * values set in the reply message: all unset fields are first updated, and then the rc is determined. |
| 296 | * The rc is intended to merely warn if the reply message already contained data that is incompatible with rx, |
| 297 | * e.g. a mismatching IMSI. |
| 298 | */ |
| 299 | int osmo_gsup_make_response(struct osmo_gsup_message *reply, |
| 300 | const struct osmo_gsup_message *rx, bool error, bool final_response) |
| 301 | { |
| 302 | int rc = 0; |
| 303 | |
| 304 | if (!reply->message_type) { |
| 305 | if (error) |
| 306 | reply->message_type = OSMO_GSUP_TO_MSGT_ERROR(rx->message_type); |
| 307 | else |
| 308 | reply->message_type = OSMO_GSUP_TO_MSGT_RESULT(rx->message_type); |
| 309 | } |
| 310 | |
| 311 | if (*reply->imsi == '\0') |
| 312 | OSMO_STRLCPY_ARRAY(reply->imsi, rx->imsi); |
| 313 | |
| 314 | if (reply->message_class == OSMO_GSUP_MESSAGE_CLASS_UNSET) |
| 315 | reply->message_class = rx->message_class; |
| 316 | |
| 317 | if (!reply->destination_name || !reply->destination_name_len) { |
| 318 | reply->destination_name = rx->source_name; |
| 319 | reply->destination_name_len = rx->source_name_len; |
| 320 | } |
| 321 | |
| 322 | /* RP-Message-Reference is mandatory for SM Service */ |
| 323 | if (!reply->sm_rp_mr) |
| 324 | reply->sm_rp_mr = rx->sm_rp_mr; |
| 325 | |
| 326 | /* For SS/USSD, it's important to keep both session state and ID IEs */ |
| 327 | if (!reply->session_id) |
| 328 | reply->session_id = rx->session_id; |
| 329 | if (rx->session_state != OSMO_GSUP_SESSION_STATE_NONE |
| 330 | && reply->session_state == OSMO_GSUP_SESSION_STATE_NONE) { |
| 331 | if (final_response || rx->session_state == OSMO_GSUP_SESSION_STATE_END) |
| 332 | reply->session_state = OSMO_GSUP_SESSION_STATE_END; |
| 333 | else |
| 334 | reply->session_state = OSMO_GSUP_SESSION_STATE_CONTINUE; |
| 335 | } |
| 336 | |
| 337 | if (strcmp(reply->imsi, rx->imsi)) |
| 338 | rc |= 1 << 0; |
| 339 | if (reply->message_class != rx->message_class) |
| 340 | rc |= 1 << 1; |
| 341 | if (rx->sm_rp_mr && (!reply->sm_rp_mr || *rx->sm_rp_mr != *reply->sm_rp_mr)) |
| 342 | rc |= 1 << 2; |
| 343 | if (reply->session_id != rx->session_id) |
| 344 | rc |= 1 << 3; |
| 345 | return rc; |
| 346 | } |
| 347 | |
| 348 | /*! Print the most important value of a GSUP message to a string buffer in human readable form. |
| 349 | * \param[out] buf The buffer to write to. |
| 350 | * \param[out] buflen sizeof(buf). |
| 351 | * \param[in] msg GSUP message to print. |
| 352 | */ |
| 353 | size_t osmo_gsup_message_to_str_buf(char *buf, size_t buflen, const struct osmo_gsup_message *msg) |
| 354 | { |
| 355 | struct osmo_strbuf sb = { .buf = buf, .len = buflen }; |
| 356 | if (!msg) { |
| 357 | OSMO_STRBUF_PRINTF(sb, "NULL"); |
| 358 | return sb.chars_needed; |
| 359 | } |
| 360 | |
| 361 | if (msg->message_class) |
| 362 | OSMO_STRBUF_PRINTF(sb, "%s ", osmo_gsup_message_class_name(msg->message_class)); |
| 363 | |
| 364 | OSMO_STRBUF_PRINTF(sb, "%s:", osmo_gsup_message_type_name(msg->message_type)); |
| 365 | |
| 366 | OSMO_STRBUF_PRINTF(sb, " imsi="); |
| 367 | OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, msg->imsi, strnlen(msg->imsi, sizeof(msg->imsi))); |
| 368 | |
| 369 | if (msg->cause) |
| 370 | OSMO_STRBUF_PRINTF(sb, " cause=%s", get_value_string(gsm48_gmm_cause_names, msg->cause)); |
| 371 | |
| 372 | switch (msg->cn_domain) { |
| 373 | case OSMO_GSUP_CN_DOMAIN_CS: |
| 374 | OSMO_STRBUF_PRINTF(sb, " cn_domain=CS"); |
| 375 | break; |
| 376 | case OSMO_GSUP_CN_DOMAIN_PS: |
| 377 | OSMO_STRBUF_PRINTF(sb, " cn_domain=PS"); |
| 378 | break; |
| 379 | default: |
| 380 | if (msg->cn_domain) |
| 381 | OSMO_STRBUF_PRINTF(sb, " cn_domain=?(%d)", msg->cn_domain); |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (msg->source_name_len) { |
| 386 | OSMO_STRBUF_PRINTF(sb, " source_name="); |
| 387 | OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->source_name, msg->source_name_len); |
| 388 | } |
| 389 | |
| 390 | if (msg->destination_name_len) { |
| 391 | OSMO_STRBUF_PRINTF(sb, " destination_name="); |
| 392 | OSMO_STRBUF_APPEND(sb, osmo_quote_cstr_buf, (char*)msg->destination_name, msg->destination_name_len); |
| 393 | } |
| 394 | |
| 395 | if (msg->session_id) |
| 396 | OSMO_STRBUF_PRINTF(sb, " session_id=%" PRIu32, msg->session_id); |
| 397 | if (msg->session_state) |
| 398 | OSMO_STRBUF_PRINTF(sb, " session_state=%s", osmo_gsup_session_state_name(msg->session_state)); |
| 399 | |
| 400 | if (msg->sm_rp_mr) |
| 401 | OSMO_STRBUF_PRINTF(sb, " sm_rp_mr=%" PRIu8, *msg->sm_rp_mr); |
| 402 | |
| 403 | return sb.chars_needed; |
| 404 | } |
| 405 | |
| 406 | /*! Same as osmo_gsup_message_to_str_buf() but returns a talloc allocated string. */ |
| 407 | char *osmo_gsup_message_to_str_c(void *ctx, const struct osmo_gsup_message *msg) |
| 408 | { |
| 409 | OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_gsup_message_to_str_buf, msg) |
| 410 | } |