Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* The MSC-I role implementation variant that forwards requests to/from a remote MSC. */ |
| 2 | /* |
| 3 | * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de> |
| 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_i_remote.h> |
| 29 | #include <osmocom/msc/msc_roles.h> |
| 30 | #include <osmocom/msc/msub.h> |
| 31 | #include <osmocom/msc/msc_i.h> |
| 32 | #include <osmocom/msc/e_link.h> |
| 33 | |
| 34 | static struct osmo_fsm msc_i_remote_fsm; |
| 35 | |
| 36 | static struct msc_i *msc_i_remote_priv(struct osmo_fsm_inst *fi) |
| 37 | { |
| 38 | OSMO_ASSERT(fi); |
| 39 | OSMO_ASSERT(fi->fsm == &msc_i_remote_fsm); |
| 40 | OSMO_ASSERT(fi->priv); |
| 41 | return fi->priv; |
| 42 | } |
| 43 | |
| 44 | /* The idea is that this msc_i role is event-compatible to the "real" msc_i.c FSM, but instead of acting on the events |
| 45 | * directly, it forwards the events to a remote MSC-I role, via E-over-GSUP. |
| 46 | * |
| 47 | * [MSC-A-----------------] [MSC-B-----------------] |
| 48 | * msc_a --> msc_i_remote ----GSUP---> msc_a_remote --> msc_i ---BSSMAP--> [BSS] |
| 49 | * you are here^ |
| 50 | */ |
| 51 | static int msc_i_remote_msg_down_to_remote_msc(struct msc_i *msc_i, |
| 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_i->c.remote_to; |
| 57 | |
| 58 | if (!e) { |
| 59 | LOG_MSC_I_REMOTE(msc_i, 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_I_REMOTE(msc_i, 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_I_REMOTE(msc_i, 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_i_remote <---GSUP---- msc_a_remote <-- msc_i <--BSSMAP--- [BSS] |
| 80 | * you are here^ |
| 81 | */ |
| 82 | static int msc_i_remote_rx_gsup(struct msc_i *msc_i, const struct osmo_gsup_message *gsup_msg) |
| 83 | { |
| 84 | uint32_t event; |
| 85 | struct an_apdu an_apdu; |
| 86 | int rc; |
| 87 | |
| 88 | /* MSC_A_EV_FROM_I_COMPLETE_LAYER_3 will never occur with a remote MSC-I, since all Complete Layer 3 will happen |
| 89 | * between a local MSC-A and local MSC-I roles. Only after an inter-MSC Handover will there possibly exist a |
| 90 | * remote MSC-I, which is long after Complete Layer 3. */ |
| 91 | |
| 92 | switch (gsup_msg->message_type) { |
| 93 | case OSMO_GSUP_MSGT_E_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 94 | case OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_REQUEST: |
| 95 | event = MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST; |
| 96 | break; |
| 97 | |
| 98 | case OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_REQUEST: |
| 99 | event = MSC_A_EV_FROM_I_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_i_clear(msc_i); |
| 106 | return 0; |
| 107 | |
| 108 | default: |
| 109 | LOG_MSC_I_REMOTE(msc_i, 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_i_remote <---GSUP---- msc_a_remote <-- msc_i <--BSSMAP--- [BSS] |
| 116 | * ^you are here |
| 117 | */ |
| 118 | gsup_msg_to_an_apdu(&an_apdu, gsup_msg); |
| 119 | rc = msub_role_dispatch(msc_i->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_i_remote_fsm_ready(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 126 | { |
| 127 | struct msc_i *msc_i = msc_i_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_i_remote <---GSUP---- msc_a_remote <-- msc_i <--BSSMAP--- [BSS] |
| 135 | * you are here^ |
| 136 | */ |
| 137 | msc_i_remote_rx_gsup(msc_i, (const struct osmo_gsup_message*)data); |
| 138 | return; |
| 139 | |
| 140 | case MSC_I_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST: |
| 141 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 142 | * msc_a --> msc_i_remote ----GSUP---> msc_a_remote --> msc_i ---BSSMAP--> [BSS] |
| 143 | * ^you are here |
| 144 | */ |
| 145 | an_apdu = data; |
| 146 | msc_i_remote_msg_down_to_remote_msc(msc_i, OSMO_GSUP_MSGT_E_FORWARD_ACCESS_SIGNALLING_REQUEST, an_apdu); |
| 147 | return; |
| 148 | |
| 149 | case MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_RESULT: |
| 150 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 151 | * msc_a --> msc_i_remote ----GSUP---> msc_a_remote --> msc_i ---BSSMAP--> [BSS] |
| 152 | * ^you are here |
| 153 | */ |
| 154 | an_apdu = data; |
| 155 | msc_i_remote_msg_down_to_remote_msc(msc_i, OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_RESULT, an_apdu); |
| 156 | return; |
| 157 | |
| 158 | case MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_ERROR: |
| 159 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 160 | * msc_a --> msc_i_remote ----GSUP---> msc_a_remote --> msc_i ---BSSMAP--> [BSS] |
| 161 | * ^you are here |
| 162 | */ |
| 163 | an_apdu = data; |
| 164 | msc_i_remote_msg_down_to_remote_msc(msc_i, OSMO_GSUP_MSGT_E_PREPARE_SUBSEQUENT_HANDOVER_ERROR, an_apdu); |
| 165 | return; |
| 166 | |
| 167 | case MSC_I_EV_FROM_A_SEND_END_SIGNAL_RESPONSE: |
| 168 | /* [MSC-A-----------------] [MSC-B-----------------] |
| 169 | * msc_a --> msc_i_remote ----GSUP---> msc_a_remote --> msc_i ---BSSMAP--> [BSS] |
| 170 | * ^you are here |
| 171 | */ |
| 172 | an_apdu = data; |
| 173 | msc_i_remote_msg_down_to_remote_msc(msc_i, OSMO_GSUP_MSGT_E_SEND_END_SIGNAL_RESULT, an_apdu); |
| 174 | return; |
| 175 | |
| 176 | default: |
| 177 | OSMO_ASSERT(false); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void msc_i_remote_fsm_clearing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 182 | { |
| 183 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi); |
| 184 | } |
| 185 | |
| 186 | static void msc_i_remote_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 187 | { |
| 188 | struct msc_i *msc_i = msc_i_remote_priv(fi); |
| 189 | msc_i_remote_msg_down_to_remote_msc(msc_i, OSMO_GSUP_MSGT_E_CLOSE, NULL); |
| 190 | } |
| 191 | |
| 192 | #define S(x) (1 << (x)) |
| 193 | |
| 194 | static const struct osmo_fsm_state msc_i_remote_fsm_states[] = { |
| 195 | [MSC_I_ST_READY] = { |
| 196 | .name = "READY", |
| 197 | .action = msc_i_remote_fsm_ready, |
| 198 | .in_event_mask = 0 |
| 199 | | S(MSC_REMOTE_EV_RX_GSUP) |
| 200 | | S(MSC_I_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST) |
| 201 | | S(MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_RESULT) |
| 202 | | S(MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_ERROR) |
| 203 | , |
| 204 | .out_state_mask = 0 |
| 205 | | S(MSC_I_ST_CLEARING) |
| 206 | , |
| 207 | }, |
| 208 | [MSC_I_ST_CLEARING] = { |
| 209 | .name = "CLEARING", |
| 210 | .onenter = msc_i_remote_fsm_clearing_onenter, |
| 211 | }, |
| 212 | }; |
| 213 | |
| 214 | static struct osmo_fsm msc_i_remote_fsm = { |
| 215 | .name = "msc_i_remote", |
| 216 | .states = msc_i_remote_fsm_states, |
| 217 | .num_states = ARRAY_SIZE(msc_i_remote_fsm_states), |
| 218 | .log_subsys = DMSC, |
| 219 | .event_names = msc_i_fsm_event_names, |
| 220 | .cleanup = msc_i_remote_fsm_cleanup, |
| 221 | }; |
| 222 | |
| 223 | static __attribute__((constructor)) void msc_i_remote_fsm_init(void) |
| 224 | { |
| 225 | OSMO_ASSERT(osmo_fsm_register(&msc_i_remote_fsm) == 0); |
| 226 | } |
| 227 | |
| 228 | struct msc_i *msc_i_remote_alloc(struct msub *msub, struct ran_infra *ran, struct e_link *e) |
| 229 | { |
| 230 | struct msc_i *msc_i; |
| 231 | |
| 232 | msub_role_alloc(msub, MSC_ROLE_I, &msc_i_remote_fsm, struct msc_i, ran); |
| 233 | msc_i = msub_msc_i(msub); |
| 234 | if (!msc_i) |
| 235 | return NULL; |
| 236 | |
| 237 | e_link_assign(e, msc_i->c.fi); |
| 238 | if (!msc_i->c.remote_to) { |
| 239 | LOG_MSC_I_REMOTE(msc_i, LOGL_ERROR, "Failed to set up E link over GSUP to remote MSC\n"); |
| 240 | msc_i_clear(msc_i); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | return msc_i; |
| 245 | } |