blob: 9a0dc1667c4ae72560c03adb9e5ea41920f9aa19 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Directing individual GSUP messages to their respective handlers. */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01004 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010019 */
20#include <errno.h>
21
22#include <osmocom/gsupclient/gsup_client.h>
23
24#include <osmocom/msc/debug.h>
25#include <osmocom/msc/gsup_client_mux.h>
26
27static enum osmo_gsup_message_class gsup_client_mux_classify(struct gsup_client_mux *gcm,
28 const struct osmo_gsup_message *gsup_msg)
29{
30 if (gsup_msg->message_class)
31 return gsup_msg->message_class;
32
33 LOGP(DLGSUP, LOGL_DEBUG, "No explicit GSUP Message Class, trying to guess from message type %s\n",
34 osmo_gsup_message_type_name(gsup_msg->message_type));
35
36 switch (gsup_msg->message_type) {
37 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
38 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
39 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
40 return OSMO_GSUP_MESSAGE_CLASS_USSD;
41
42 /* GSM 04.11 code implementing MO SMS */
43 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
44 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
45 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
46 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
47 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
48 return OSMO_GSUP_MESSAGE_CLASS_SMS;
49
50 default:
51 return OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
52 }
53}
54
55/* Non-static for unit tests */
56int gsup_client_mux_rx(struct osmo_gsup_client *gsup_client, struct msgb *msg)
57{
58 struct gsup_client_mux *gcm = gsup_client->data;
59 struct osmo_gsup_message gsup;
60 enum osmo_gsup_message_class message_class;
61 int rc;
62
63 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
64 if (rc < 0) {
65 LOGP(DLGSUP, LOGL_ERROR, "Failed to decode GSUP message: '%s' (%d) [ %s]\n",
66 get_value_string(gsm48_gmm_cause_names, -rc), -rc, osmo_hexdump(msg->data, msg->len));
67 goto msgb_free_and_return;
68 }
69
70 if (!gsup.imsi[0]) {
71 LOGP(DLGSUP, LOGL_ERROR, "Failed to decode GSUP message: missing IMSI\n");
72 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type))
73 gsup_client_mux_tx_error_reply(gcm, &gsup, GMM_CAUSE_INV_MAND_INFO);
74 rc = -GMM_CAUSE_INV_MAND_INFO;
75 goto msgb_free_and_return;
76 }
77
78 message_class = gsup_client_mux_classify(gcm, &gsup);
79
80 if (message_class <= OSMO_GSUP_MESSAGE_CLASS_UNSET || message_class >= ARRAY_SIZE(gcm->rx_cb)) {
81 LOGP(DLGSUP, LOGL_ERROR, "Failed to classify GSUP message target\n");
82 rc = -EINVAL;
83 goto msgb_free_and_return;
84 }
85
86 if (!gcm->rx_cb[message_class].func) {
87 LOGP(DLGSUP, LOGL_ERROR, "No receiver set up for GSUP Message Class %s\n", osmo_gsup_message_class_name(message_class));
88 rc = -ENOTSUP;
89 goto msgb_free_and_return;
90 }
91
92 rc = gcm->rx_cb[message_class].func(gcm, gcm->rx_cb[message_class].data, &gsup);
93
94msgb_free_and_return:
95 msgb_free(msg);
96 return rc;
97}
98
99/* Make it clear that struct gsup_client_mux should be talloc allocated, so that it can be used as talloc parent. */
100struct gsup_client_mux *gsup_client_mux_alloc(void *talloc_ctx)
101{
102 return talloc_zero(talloc_ctx, struct gsup_client_mux);
103}
104
105/* Start a GSUP client to serve this gsup_client_mux. */
106int gsup_client_mux_start(struct gsup_client_mux *gcm, const char *gsup_server_addr_str, uint16_t gsup_server_port,
107 struct ipaccess_unit *ipa_dev)
108{
109 gcm->gsup_client = osmo_gsup_client_create2(gcm, ipa_dev,
110 gsup_server_addr_str,
111 gsup_server_port,
112 &gsup_client_mux_rx, NULL);
113 if (!gcm->gsup_client)
114 return -ENOMEM;
115 gcm->gsup_client->data = gcm;
116 return 0;
117}
118
119int gsup_client_mux_tx(struct gsup_client_mux *gcm, const struct osmo_gsup_message *gsup_msg)
120{
121 struct msgb *msg;
122 int rc;
123
124 if (!gcm || !gcm->gsup_client) {
125 LOGP(DLGSUP, LOGL_ERROR, "GSUP link is down, cannot send GSUP message\n");
126 return -ENOTSUP;
127 }
128
129 msg = osmo_gsup_client_msgb_alloc();
130 rc = osmo_gsup_encode(msg, gsup_msg);
131 if (rc < 0) {
132 LOGP(DLGSUP, LOGL_ERROR, "Failed to encode GSUP message: '%s'\n", strerror(-rc));
133 return rc;
134 }
135
136 return osmo_gsup_client_send(gcm->gsup_client, msg);
137}
138
139/* Transmit GSUP error in response to original message */
140void gsup_client_mux_tx_error_reply(struct gsup_client_mux *gcm, const struct osmo_gsup_message *gsup_orig,
141 enum gsm48_gmm_cause cause)
142{
143 struct osmo_gsup_message gsup_reply;
144
145 /* No need to answer if we couldn't parse an ERROR message type, only REQUESTs need an error reply. */
146 if (!OSMO_GSUP_IS_MSGT_REQUEST(gsup_orig->message_type))
147 return;
148
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100149 gsup_reply = (struct osmo_gsup_message){
150 .cause = cause,
151 .message_type = OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type),
Vadim Yanitskiyc33d94b2019-06-15 01:31:50 +0700152 .message_class = gsup_orig->message_class,
Mychaela N. Falconia8de4ea62023-08-26 02:23:13 +0000153 .destination_name = gsup_orig->source_name,
154 .destination_name_len = gsup_orig->source_name_len,
Vadim Yanitskiy20edc972019-06-15 03:10:33 +0700155
156 /* RP-Message-Reference is mandatory for SM Service */
157 .sm_rp_mr = gsup_orig->sm_rp_mr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100158 };
159
Vadim Yanitskiy4d0066c2019-06-15 01:08:16 +0700160 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
161
Vadim Yanitskiy463005e2019-06-15 01:28:35 +0700162 /* For SS/USSD, it's important to keep both session state and ID IEs */
163 if (gsup_orig->session_state != OSMO_GSUP_SESSION_STATE_NONE) {
164 gsup_reply.session_state = OSMO_GSUP_SESSION_STATE_END;
165 gsup_reply.session_id = gsup_orig->session_id;
166 }
167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100168 if (osmo_gsup_client_enc_send(gcm->gsup_client, &gsup_reply))
169 LOGP(DLGSUP, LOGL_ERROR, "Failed to send Error reply (imsi=%s)\n",
170 osmo_quote_str(gsup_orig->imsi, -1));
171}