blob: 7ec1712e745e8429067b85c32ace42c2560d8688 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Directing individual GSUP messages to their respective handlers. */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#include <errno.h>
25
26#include <osmocom/gsupclient/gsup_client.h>
27
28#include <osmocom/msc/debug.h>
29#include <osmocom/msc/gsup_client_mux.h>
30
31static enum osmo_gsup_message_class gsup_client_mux_classify(struct gsup_client_mux *gcm,
32 const struct osmo_gsup_message *gsup_msg)
33{
34 if (gsup_msg->message_class)
35 return gsup_msg->message_class;
36
37 LOGP(DLGSUP, LOGL_DEBUG, "No explicit GSUP Message Class, trying to guess from message type %s\n",
38 osmo_gsup_message_type_name(gsup_msg->message_type));
39
40 switch (gsup_msg->message_type) {
41 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
42 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
43 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
44 return OSMO_GSUP_MESSAGE_CLASS_USSD;
45
46 /* GSM 04.11 code implementing MO SMS */
47 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
48 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
49 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
50 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
51 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
52 return OSMO_GSUP_MESSAGE_CLASS_SMS;
53
54 default:
55 return OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
56 }
57}
58
59/* Non-static for unit tests */
60int gsup_client_mux_rx(struct osmo_gsup_client *gsup_client, struct msgb *msg)
61{
62 struct gsup_client_mux *gcm = gsup_client->data;
63 struct osmo_gsup_message gsup;
64 enum osmo_gsup_message_class message_class;
65 int rc;
66
67 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
68 if (rc < 0) {
69 LOGP(DLGSUP, LOGL_ERROR, "Failed to decode GSUP message: '%s' (%d) [ %s]\n",
70 get_value_string(gsm48_gmm_cause_names, -rc), -rc, osmo_hexdump(msg->data, msg->len));
71 goto msgb_free_and_return;
72 }
73
74 if (!gsup.imsi[0]) {
75 LOGP(DLGSUP, LOGL_ERROR, "Failed to decode GSUP message: missing IMSI\n");
76 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type))
77 gsup_client_mux_tx_error_reply(gcm, &gsup, GMM_CAUSE_INV_MAND_INFO);
78 rc = -GMM_CAUSE_INV_MAND_INFO;
79 goto msgb_free_and_return;
80 }
81
82 message_class = gsup_client_mux_classify(gcm, &gsup);
83
84 if (message_class <= OSMO_GSUP_MESSAGE_CLASS_UNSET || message_class >= ARRAY_SIZE(gcm->rx_cb)) {
85 LOGP(DLGSUP, LOGL_ERROR, "Failed to classify GSUP message target\n");
86 rc = -EINVAL;
87 goto msgb_free_and_return;
88 }
89
90 if (!gcm->rx_cb[message_class].func) {
91 LOGP(DLGSUP, LOGL_ERROR, "No receiver set up for GSUP Message Class %s\n", osmo_gsup_message_class_name(message_class));
92 rc = -ENOTSUP;
93 goto msgb_free_and_return;
94 }
95
96 rc = gcm->rx_cb[message_class].func(gcm, gcm->rx_cb[message_class].data, &gsup);
97
98msgb_free_and_return:
99 msgb_free(msg);
100 return rc;
101}
102
103/* Make it clear that struct gsup_client_mux should be talloc allocated, so that it can be used as talloc parent. */
104struct gsup_client_mux *gsup_client_mux_alloc(void *talloc_ctx)
105{
106 return talloc_zero(talloc_ctx, struct gsup_client_mux);
107}
108
109/* Start a GSUP client to serve this gsup_client_mux. */
110int gsup_client_mux_start(struct gsup_client_mux *gcm, const char *gsup_server_addr_str, uint16_t gsup_server_port,
111 struct ipaccess_unit *ipa_dev)
112{
113 gcm->gsup_client = osmo_gsup_client_create2(gcm, ipa_dev,
114 gsup_server_addr_str,
115 gsup_server_port,
116 &gsup_client_mux_rx, NULL);
117 if (!gcm->gsup_client)
118 return -ENOMEM;
119 gcm->gsup_client->data = gcm;
120 return 0;
121}
122
123int gsup_client_mux_tx(struct gsup_client_mux *gcm, const struct osmo_gsup_message *gsup_msg)
124{
125 struct msgb *msg;
126 int rc;
127
128 if (!gcm || !gcm->gsup_client) {
129 LOGP(DLGSUP, LOGL_ERROR, "GSUP link is down, cannot send GSUP message\n");
130 return -ENOTSUP;
131 }
132
133 msg = osmo_gsup_client_msgb_alloc();
134 rc = osmo_gsup_encode(msg, gsup_msg);
135 if (rc < 0) {
136 LOGP(DLGSUP, LOGL_ERROR, "Failed to encode GSUP message: '%s'\n", strerror(-rc));
137 return rc;
138 }
139
140 return osmo_gsup_client_send(gcm->gsup_client, msg);
141}
142
143/* Transmit GSUP error in response to original message */
144void gsup_client_mux_tx_error_reply(struct gsup_client_mux *gcm, const struct osmo_gsup_message *gsup_orig,
145 enum gsm48_gmm_cause cause)
146{
147 struct osmo_gsup_message gsup_reply;
148
149 /* No need to answer if we couldn't parse an ERROR message type, only REQUESTs need an error reply. */
150 if (!OSMO_GSUP_IS_MSGT_REQUEST(gsup_orig->message_type))
151 return;
152
153 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
154
155 gsup_reply = (struct osmo_gsup_message){
156 .cause = cause,
157 .message_type = OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type),
158 };
159
160 if (osmo_gsup_client_enc_send(gcm->gsup_client, &gsup_reply))
161 LOGP(DLGSUP, LOGL_ERROR, "Failed to send Error reply (imsi=%s)\n",
162 osmo_quote_str(gsup_orig->imsi, -1));
163}