Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* The MSC-T role implementation variant that forwards requests to/from a remote MSC. */ |
| 2 | /* |
Vadim Yanitskiy | 999a593 | 2023-05-18 17:22:26 +0700 | [diff] [blame] | 3 | * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: AGPL-3.0+ |
| 7 | * |
| 8 | * Author: Neels Hofmeyr |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/core/fsm.h> |
| 25 | |
| 26 | #include <osmocom/msc/debug.h> |
| 27 | #include <osmocom/msc/gsm_data.h> |
| 28 | #include <osmocom/msc/msc_t_remote.h> |
| 29 | #include <osmocom/msc/msc_roles.h> |
| 30 | #include <osmocom/msc/msub.h> |
| 31 | #include <osmocom/msc/msc_t.h> |
| 32 | #include <osmocom/msc/e_link.h> |
| 33 | |
| 34 | static struct osmo_fsm msc_t_remote_fsm; |
| 35 | |
| 36 | static struct msc_t *msc_t_remote_priv(struct osmo_fsm_inst *fi) |
| 37 | { |
| 38 | OSMO_ASSERT(fi); |
| 39 | OSMO_ASSERT(fi->fsm == &msc_t_remote_fsm); |
| 40 | OSMO_ASSERT(fi->priv); |
| 41 | return fi->priv; |
| 42 | } |
| 43 | |
| 44 | /* The idea is that this msc_t role is event-compatible to the "real" msc_t.c FSM, but instead of acting on the events |
| 45 | * directly, it forwards the events to a remote MSC-T role, via E-over-GSUP. |
| 46 | * |
| 47 | * [MSC-A-----------------] [MSC-B-----------------] |
| 48 | * msc_a --> msc_t_remote ----GSUP---> msc_a_remote --> msc_t ---BSSMAP--> [BSS] |
| 49 | * you are here^ |
| 50 | */ |
| 51 | static int msc_t_remote_msg_down_to_remote_msc(struct msc_t *msc_t, |
| 52 | enum osmo_gsup_message_type message_type, |
| 53 | struct an_apdu *an_apdu) |
| 54 | { |
| 55 | struct osmo_gsup_message m; |
| 56 | struct e_link *e = msc_t->c.remote_to; |
| 57 | |
| 58 | if (!e) { |
| 59 | LOG_MSC_T_REMOTE(msc_t, LOGL_ERROR, "No E link to remote MSC, cannot send AN-APDU\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | if (e_prep_gsup_msg(e, &m)) { |
| 64 | LOG_MSC_T_REMOTE(msc_t, LOGL_ERROR, "Error composing E-interface GSUP message\n"); |
| 65 | return -1; |
| 66 | } |
| 67 | m.message_type = message_type; |
| 68 | if (an_apdu) { |
| 69 | if (gsup_msg_assign_an_apdu(&m, an_apdu)) { |
| 70 | LOG_MSC_T_REMOTE(msc_t, LOGL_ERROR, "Error composing E-interface GSUP message\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return e_tx(e, &m); |
| 76 | } |
| 77 | |
| 78 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 79 | * msc_a <-- msc_t_remote <---GSUP---- msc_a_remote <-- msc_t <--BSSMAP--- [BSS] |
| 80 | * you are here^ |
| 81 | */ |
| 82 | static int msc_t_remote_rx_gsup(struct msc_t *msc_t, const struct osmo_gsup_message *gsup_msg) |
| 83 | { |
| 84 | uint32_t event; |
| 85 | struct an_apdu an_apdu; |
| 86 | int rc; |
| 87 | |
| 88 | switch (gsup_msg->message_type) { |
| 89 | case OSMO_GSUP_MSGT_E_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 90 | event = MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST; |
| 91 | break; |
| 92 | |
| 93 | case OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_ERROR: |
| 94 | case OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_RESULT: |
| 95 | event = MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE; |
| 96 | break; |
| 97 | |
| 98 | case OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_REQUEST: |
| 99 | event = MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST; |
| 100 | break; |
| 101 | |
| 102 | case OSMO_GSUP_MSGT_E_CLOSE: |
| 103 | case OSMO_GSUP_MSGT_E_ABORT: |
| 104 | case OSMO_GSUP_MSGT_E_ROUTING_ERROR: |
| 105 | msc_t_clear(msc_t); |
| 106 | return 0; |
| 107 | |
| 108 | default: |
| 109 | LOG_MSC_T_REMOTE(msc_t, LOGL_ERROR, "Unhandled GSUP message type: %s\n", |
| 110 | osmo_gsup_message_type_name(gsup_msg->message_type)); |
| 111 | return -1; |
| 112 | }; |
| 113 | |
| 114 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 115 | * msc_a <-- msc_t_remote <---GSUP---- msc_a_remote <-- msc_t <--BSSMAP--- [BSS] |
| 116 | * ^you are here |
| 117 | */ |
| 118 | gsup_msg_to_an_apdu(&an_apdu, gsup_msg); |
| 119 | rc = msub_role_dispatch(msc_t->c.msub, MSC_ROLE_A, event, &an_apdu); |
| 120 | if (an_apdu.msg) |
| 121 | msgb_free(an_apdu.msg); |
| 122 | return rc; |
| 123 | } |
| 124 | |
| 125 | static void msc_t_remote_fsm_ready(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 126 | { |
| 127 | struct msc_t *msc_t = msc_t_remote_priv(fi); |
| 128 | struct an_apdu *an_apdu; |
| 129 | |
| 130 | switch (event) { |
| 131 | |
| 132 | case MSC_REMOTE_EV_RX_GSUP: |
| 133 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 134 | * msc_a <-- msc_t_remote <---GSUP---- msc_a_remote <-- msc_t <--BSSMAP--- [BSS] |
| 135 | * you are here^ |
| 136 | */ |
| 137 | msc_t_remote_rx_gsup(msc_t, (const struct osmo_gsup_message*)data); |
| 138 | return; |
| 139 | |
| 140 | case MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST: |
| 141 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 142 | * msc_a --> msc_t_remote ----GSUP---> going to create an msc_t if the request succeeds |
| 143 | * ^you are here |
| 144 | */ |
| 145 | an_apdu = data; |
| 146 | msc_t_remote_msg_down_to_remote_msc(msc_t, OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_REQUEST, an_apdu); |
| 147 | return; |
| 148 | |
| 149 | case MSC_T_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST: |
| 150 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 151 | * msc_a --> msc_t_remote ----GSUP---> msc_a_remote --> msc_t ---BSSMAP--> [BSS] |
| 152 | * ^you are here |
| 153 | */ |
| 154 | an_apdu = data; |
| 155 | msc_t_remote_msg_down_to_remote_msc(msc_t, OSMO_GSUP_MSGT_E_FORWARD_ACCESS_SIGNALLING_REQUEST, an_apdu); |
| 156 | return; |
| 157 | |
| 158 | default: |
| 159 | OSMO_ASSERT(false); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void msc_t_remote_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 164 | { |
| 165 | struct msc_t *msc_t = msc_t_remote_priv(fi); |
| 166 | if (msc_t->c.remote_to) |
| 167 | msc_t_remote_msg_down_to_remote_msc(msc_t, OSMO_GSUP_MSGT_E_CLOSE, NULL); |
| 168 | } |
| 169 | |
| 170 | #define S(x) (1 << (x)) |
| 171 | |
| 172 | static const struct osmo_fsm_state msc_t_remote_fsm_states[] = { |
| 173 | /* An FSM instance always starts in state 0. Define one just to be able to state_chg out of it. Root reason is |
| 174 | * that we're using MSC_T_ST_* enum values from msc_t.c, but don't need the first |
| 175 | * MSC_T_ST_PENDING_FIRST_CO_INITIAL_MSG. */ |
| 176 | [0] = { |
| 177 | .name = "0", |
| 178 | .out_state_mask = 0 |
| 179 | | S(MSC_T_ST_WAIT_HO_COMPLETE) |
| 180 | , |
| 181 | }, |
| 182 | [MSC_T_ST_WAIT_HO_COMPLETE] = { |
| 183 | .name = "WAIT_HO_COMPLETE", |
| 184 | .action = msc_t_remote_fsm_ready, |
| 185 | .in_event_mask = 0 |
| 186 | | S(MSC_REMOTE_EV_RX_GSUP) |
| 187 | | S(MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST) |
| 188 | | S(MSC_T_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST) |
| 189 | , |
| 190 | }, |
| 191 | }; |
| 192 | |
| 193 | static struct osmo_fsm msc_t_remote_fsm = { |
| 194 | .name = "msc_t_remote", |
| 195 | .states = msc_t_remote_fsm_states, |
| 196 | .num_states = ARRAY_SIZE(msc_t_remote_fsm_states), |
| 197 | .log_subsys = DMSC, |
| 198 | .event_names = msc_t_fsm_event_names, |
| 199 | .cleanup = msc_t_remote_fsm_cleanup, |
| 200 | }; |
| 201 | |
| 202 | static __attribute__((constructor)) void msc_t_remote_fsm_init(void) |
| 203 | { |
| 204 | OSMO_ASSERT(osmo_fsm_register(&msc_t_remote_fsm) == 0); |
| 205 | } |
| 206 | |
| 207 | struct msc_t *msc_t_remote_alloc(struct msub *msub, struct ran_infra *ran, |
| 208 | const uint8_t *remote_msc_name, size_t remote_msc_name_len) |
| 209 | { |
| 210 | struct msc_t *msc_t; |
| 211 | |
| 212 | msub_role_alloc(msub, MSC_ROLE_T, &msc_t_remote_fsm, struct msc_t, ran); |
| 213 | msc_t = msub_msc_t(msub); |
| 214 | if (!msc_t) |
| 215 | return NULL; |
| 216 | |
| 217 | msc_t->c.remote_to = e_link_alloc(msub_net(msub)->gcm, msc_t->c.fi, remote_msc_name, remote_msc_name_len); |
| 218 | if (!msc_t->c.remote_to) { |
| 219 | LOG_MSC_T_REMOTE(msc_t, LOGL_ERROR, "Failed to set up E link over GSUP to remote MSC\n"); |
| 220 | msc_t_clear(msc_t); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | osmo_fsm_inst_state_chg(msc_t->c.fi, MSC_T_ST_WAIT_HO_COMPLETE, 0, 0); |
| 225 | return msc_t; |
| 226 | } |