Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Philipp Maier |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | |
| 23 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 24 | #include <osmocom/core/logging.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/fsm.h> |
| 28 | #include <osmocom/core/byteswap.h> |
| 29 | #include <osmocom/msc/msc_mgcp.h> |
| 30 | #include <osmocom/msc/debug.h> |
| 31 | #include <osmocom/msc/transaction.h> |
| 32 | #include <osmocom/msc/a_iface.h> |
| 33 | #include <osmocom/msc/msc_ifaces.h> |
| 34 | #include <osmocom/msc/gsm_04_08.h> |
| 35 | #include <osmocom/msc/iucs.h> |
| 36 | #include <osmocom/msc/vlr.h> |
| 37 | |
| 38 | #include "../../bscconfig.h" |
| 39 | |
| 40 | #define S(x) (1 << (x)) |
| 41 | |
| 42 | #define CONN_ID_RAN 1 |
| 43 | #define CONN_ID_CN 2 |
| 44 | |
| 45 | #define MGCP_MGW_TIMEOUT 4 /* in seconds */ |
| 46 | #define MGCP_MGW_TIMEOUT_TIMER_NR 1 |
Philipp Maier | 4c57377 | 2018-02-08 14:05:43 +0100 | [diff] [blame] | 47 | #define MGCP_RAN_TIMEOUT 120 /* in seconds */ |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 48 | #define MGCP_RAN_TIMEOUT_TIMER_NR 2 |
| 49 | #define MGCP_REL_TIMEOUT 60 /* in seconds */ |
| 50 | #define MGCP_REL_TIMEOUT_TIMER_NR 3 |
| 51 | #define MGCP_ASS_TIMEOUT 10 /* in seconds */ |
| 52 | #define MGCP_ASS_TIMEOUT_TIMER_NR 4 |
| 53 | |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 54 | #define ENDPOINT_ID "rtpbridge/*@mgw" |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 55 | |
| 56 | /* Some internal cause codes to indicate fault condition inside the FSM */ |
| 57 | enum msc_mgcp_cause_code { |
| 58 | MGCP_ERR_MGW_FAIL, |
| 59 | MGCP_ERR_MGW_INVAL_RESP, |
| 60 | MGCP_ERR_MGW_TX_FAIL, |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 61 | MGCP_ERR_MGW_TIMEOUT, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 62 | MGCP_ERR_UNEXP_TEARDOWN, |
| 63 | MGCP_ERR_UNSUPP_ADDR_FMT, |
| 64 | MGCP_ERR_RAN_TIMEOUT, |
| 65 | MGCP_ERR_ASS_TIMEOUT, |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 66 | MGCP_ERR_TOOLONG, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 67 | MGCP_ERR_ASSGMNT_FAIL |
| 68 | }; |
| 69 | |
| 70 | /* Human readable respresentation of the faul codes, will be displayed by |
| 71 | * handle_error() */ |
| 72 | static const struct value_string msc_mgcp_cause_codes_names[] = { |
| 73 | {MGCP_ERR_MGW_FAIL, "operation failed on MGW"}, |
| 74 | {MGCP_ERR_MGW_INVAL_RESP, "invalid / unparseable response from MGW"}, |
| 75 | {MGCP_ERR_MGW_TX_FAIL, "failed to transmit MGCP message to MGW"}, |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 76 | {MGCP_ERR_MGW_TIMEOUT, "request to MGW timed out"}, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 77 | {MGCP_ERR_UNEXP_TEARDOWN, "unexpected connection teardown"}, |
| 78 | {MGCP_ERR_UNSUPP_ADDR_FMT, "unsupported network address format used (RAN)"}, |
| 79 | {MGCP_ERR_RAN_TIMEOUT, "call could not be completed in time (RAN)"}, |
| 80 | {MGCP_ERR_ASS_TIMEOUT, "assignment could not be completed in time (RAN)"}, |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 81 | {MGCP_ERR_TOOLONG, "string value too long"}, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 82 | {MGCP_ERR_ASSGMNT_FAIL, "assignment failure (RAN)"}, |
| 83 | {0, NULL} |
| 84 | }; |
| 85 | |
| 86 | enum fsm_msc_mgcp_states { |
| 87 | ST_CRCX_RAN, |
| 88 | ST_CRCX_CN, |
| 89 | ST_CRCX_COMPL, |
| 90 | ST_MDCX_CN, |
| 91 | ST_MDCX_CN_COMPL, |
| 92 | ST_MDCX_RAN, |
| 93 | ST_MDCX_RAN_COMPL, |
| 94 | ST_CALL, |
| 95 | ST_HALT, |
| 96 | }; |
| 97 | |
| 98 | enum msc_mgcp_fsm_evt { |
| 99 | /* Initial event: start off the state machine */ |
| 100 | EV_INIT, |
| 101 | |
| 102 | /* External event: Notify that the Assignment is complete and we |
| 103 | * may now forward IP/Port of the remote call leg to the MGW */ |
| 104 | EV_ASSIGN, |
| 105 | |
| 106 | /* External event: Notify that the Call is complete and that the |
| 107 | * two half open connections on the MGW should now be connected */ |
| 108 | EV_CONNECT, |
| 109 | |
| 110 | /* External event: Notify that the call is over and the connections |
| 111 | * on the mgw shall be removed */ |
| 112 | EV_TEARDOWN, |
| 113 | |
| 114 | /* Internal event: An error occurred that requires a controlled |
| 115 | * teardown of the RTP connections */ |
| 116 | EV_TEARDOWN_ERROR, |
| 117 | |
| 118 | /* Internal event: The mgcp_gw has sent its CRCX response for |
| 119 | * the RAN side */ |
| 120 | EV_CRCX_RAN_RESP, |
| 121 | |
| 122 | /* Internal event: The mgcp_gw has sent its CRCX response for |
| 123 | * the CN side */ |
| 124 | EV_CRCX_CN_RESP, |
| 125 | |
| 126 | /* Internal event: The mgcp_gw has sent its MDCX response for |
| 127 | * the RAN side */ |
| 128 | EV_MDCX_RAN_RESP, |
| 129 | |
| 130 | /* Internal event: The mgcp_gw has sent its MDCX response for |
| 131 | * the CN side */ |
| 132 | EV_MDCX_CN_RESP, |
| 133 | |
| 134 | /* Internal event: The mgcp_gw has sent its DLCX response for |
| 135 | * the RAN and CN side */ |
| 136 | EV_DLCX_ALL_RESP, |
| 137 | }; |
| 138 | |
Philipp Maier | e4f9172 | 2018-02-26 15:20:49 +0100 | [diff] [blame] | 139 | static const struct value_string msc_mgcp_fsm_evt_names[] = { |
| 140 | OSMO_VALUE_STRING(EV_INIT), |
| 141 | OSMO_VALUE_STRING(EV_ASSIGN), |
| 142 | OSMO_VALUE_STRING(EV_CONNECT), |
| 143 | OSMO_VALUE_STRING(EV_TEARDOWN), |
| 144 | OSMO_VALUE_STRING(EV_TEARDOWN_ERROR), |
| 145 | OSMO_VALUE_STRING(EV_CRCX_RAN_RESP), |
| 146 | OSMO_VALUE_STRING(EV_CRCX_CN_RESP), |
Neels Hofmeyr | 5d66970 | 2018-12-18 22:03:46 +0100 | [diff] [blame] | 147 | OSMO_VALUE_STRING(EV_MDCX_RAN_RESP), |
| 148 | OSMO_VALUE_STRING(EV_MDCX_CN_RESP), |
Philipp Maier | e4f9172 | 2018-02-26 15:20:49 +0100 | [diff] [blame] | 149 | OSMO_VALUE_STRING(EV_DLCX_ALL_RESP), |
| 150 | {0, NULL} |
| 151 | }; |
| 152 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 153 | /* A general error handler function. On error we still have an interest to |
| 154 | * remove a half open connection (if possible). This function will execute |
| 155 | * a controlled jump to the DLCX phase. From there, the FSM will then just |
| 156 | * continue like the call were ended normally */ |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 157 | #define handle_error(mgcp_ctx, cause, dlcx) _handle_error(mgcp_ctx, cause, dlcx, __FILE__, __LINE__) |
| 158 | static void _handle_error(struct mgcp_ctx *mgcp_ctx, enum msc_mgcp_cause_code cause, bool dlcx, const char *file, |
| 159 | int line) |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 160 | { |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 161 | bool dlcx_possible = true; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 162 | struct osmo_fsm_inst *fi; |
Philipp Maier | 782ccec | 2018-10-08 17:12:49 +0200 | [diff] [blame] | 163 | struct gsm_mncc mncc; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 164 | |
| 165 | OSMO_ASSERT(mgcp_ctx); |
| 166 | fi = mgcp_ctx->fsm; |
| 167 | OSMO_ASSERT(fi); |
| 168 | |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 169 | /* Check if the endpoint identifier is a specific endpoint identifier, |
| 170 | * since in order to perform a DLCX we must know the specific |
| 171 | * identifier of the endpoint we want to release. If we do not have |
| 172 | * this information because of errornous communication we can not |
| 173 | * perform a DLCX. */ |
| 174 | if (strstr(mgcp_ctx->rtp_endpoint, "*")) |
| 175 | dlcx_possible = false; |
| 176 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 177 | LOGPFSMLSRC(mgcp_ctx->fsm, LOGL_ERROR, file, line, "%s -- graceful shutdown...\n", |
| 178 | get_value_string(msc_mgcp_cause_codes_names, cause)); |
| 179 | |
Stefan Sperling | 722f2b4 | 2018-09-28 14:26:35 +0200 | [diff] [blame] | 180 | /* Request the higher layers (gsm_04_08.c) to release the call. If the |
| 181 | * problem occured after msc_mgcp_call_release() was calls, remain |
| 182 | * silent because we already got informed and the higher layers might |
| 183 | * already freed their context information (trans). */ |
| 184 | if (!mgcp_ctx->free_ctx) { |
Philipp Maier | 782ccec | 2018-10-08 17:12:49 +0200 | [diff] [blame] | 185 | mncc = (struct gsm_mncc) { |
| 186 | .msg_type = MNCC_REL_REQ, |
| 187 | .callref = mgcp_ctx->trans->callref, |
| 188 | .cause = { |
| 189 | .location = GSM48_CAUSE_LOC_PRN_S_LU, |
| 190 | .coding = 0, /* FIXME */ |
| 191 | .value = GSM48_CC_CAUSE_RESOURCE_UNAVAIL |
| 192 | } |
| 193 | }; |
| 194 | |
Stefan Sperling | 722f2b4 | 2018-09-28 14:26:35 +0200 | [diff] [blame] | 195 | mncc_set_cause(&mncc, GSM48_CAUSE_LOC_TRANS_NET, |
| 196 | GSM48_CC_CAUSE_RESOURCE_UNAVAIL); |
| 197 | mncc_tx_to_cc(mgcp_ctx->trans->net, MNCC_REL_REQ, &mncc); |
| 198 | } |
| 199 | |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 200 | /* For the shutdown we have two options. Whenever it makes sense to |
| 201 | * send a DLCX to the MGW in order to be sure that the connection is |
| 202 | * properly cleaned up, the dlcx flag should be set. In other cases |
| 203 | * where a DLCX does not make sense (e.g. the MGW times out), halting |
| 204 | * directly is the better options. In those cases, the dlcx flag |
| 205 | * should not be set */ |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 206 | if (dlcx && dlcx_possible) { |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 207 | /* Fast-forward the FSM into call state. In this state the FSM |
| 208 | * expects either an EV_TEARDOWN or an EV_TEARDOWN_ERROR. When |
| 209 | * one of the two events is received a DLCX will be send to |
| 210 | * the MGW. After that. The FSM automatically halts but will |
| 211 | * still expect a call msc_mgcp_call_release() to be freed |
| 212 | * completely */ |
| 213 | osmo_fsm_inst_state_chg(fi, ST_CALL, 0, 0); |
| 214 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_TEARDOWN_ERROR, mgcp_ctx); |
| 215 | } else { |
| 216 | /* Halt the state machine immediately. The FSM will not be |
| 217 | * freed yet, we stil require the higher layers to call |
| 218 | * msc_mgcp_call_release() */ |
| 219 | osmo_fsm_inst_state_chg(fi, ST_HALT, 0, 0); |
| 220 | osmo_fsm_inst_dispatch(fi, EV_TEARDOWN_ERROR, mgcp_ctx); |
| 221 | } |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* Timer callback to shut down in case of connectivity problems */ |
| 225 | static int fsm_timeout_cb(struct osmo_fsm_inst *fi) |
| 226 | { |
| 227 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 228 | struct mgcp_client *mgcp; |
| 229 | |
| 230 | OSMO_ASSERT(mgcp_ctx); |
| 231 | mgcp = mgcp_ctx->mgcp; |
| 232 | OSMO_ASSERT(mgcp); |
| 233 | |
| 234 | if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) { |
| 235 | /* We were unable to communicate with the MGW, unfortunately |
| 236 | * there is no meaningful action we can take now other than |
| 237 | * giving up. */ |
| 238 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 239 | /* Cancel the transaction that timed out */ |
| 240 | mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans); |
| 241 | |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 242 | /* halt of the FSM */ |
| 243 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TIMEOUT, false); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 244 | } else if (fi->T == MGCP_RAN_TIMEOUT_TIMER_NR) { |
| 245 | /* If the logic that controls the RAN is unable to negotiate a |
| 246 | * connection, we presumably still have a working connection to |
| 247 | * the MGW, we will try to shut down gracefully. */ |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 248 | handle_error(mgcp_ctx, MGCP_ERR_RAN_TIMEOUT, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 249 | } else if (fi->T == MGCP_REL_TIMEOUT_TIMER_NR) { |
| 250 | /* Under normal conditions, the MSC logic should always command |
| 251 | * to release the call at some point. However, the release may |
| 252 | * be missing due to errors in the MSC logic and we may have |
| 253 | * reached ST_HALT because of cascading errors and timeouts. In |
| 254 | * this and only in this case we will allow ST_HALT to free all |
| 255 | * context information on its own authority. */ |
| 256 | mgcp_ctx->free_ctx = true; |
| 257 | |
| 258 | /* Initiate self destruction of the FSM */ |
| 259 | osmo_fsm_inst_state_chg(fi, ST_HALT, 0, 0); |
| 260 | osmo_fsm_inst_dispatch(fi, EV_TEARDOWN, mgcp_ctx); |
| 261 | } else if (fi->T == MGCP_ASS_TIMEOUT_TIMER_NR) { |
| 262 | /* There may be rare cases in which the MSC is unable to |
| 263 | * complete the call assignment */ |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 264 | handle_error(mgcp_ctx, MGCP_ERR_ASS_TIMEOUT, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 265 | } else { |
| 266 | /* Ther must not be any unsolicited timers in this FSM. If so, |
| 267 | * we have serious problem. */ |
| 268 | OSMO_ASSERT(false); |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static void mgw_crcx_ran_resp_cb(struct mgcp_response *r, void *priv); |
| 275 | |
| 276 | /* Callback for ST_CRCX_RAN: Send CRCX for RAN side to MGW */ |
| 277 | static void fsm_crcx_ran_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 278 | { |
| 279 | struct mgcp_ctx *mgcp_ctx = data; |
| 280 | struct mgcp_client *mgcp; |
| 281 | struct mgcp_msg mgcp_msg; |
| 282 | struct msgb *msg; |
| 283 | int rc; |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 284 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 285 | struct ran_conn *conn; |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 286 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 287 | OSMO_ASSERT(mgcp_ctx); |
| 288 | mgcp = mgcp_ctx->mgcp; |
| 289 | OSMO_ASSERT(mgcp); |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 290 | trans = mgcp_ctx->trans; |
| 291 | OSMO_ASSERT(trans); |
| 292 | conn = trans->conn; |
| 293 | OSMO_ASSERT(conn); |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 294 | |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 295 | /* NOTE: In case of error, we will not be able to perform any DLCX |
| 296 | * operation because until this point we do not have requested any |
| 297 | * endpoint yet. */ |
| 298 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 299 | LOGPFSML(fi, LOGL_DEBUG, |
Philipp Maier | d997fa1 | 2018-04-11 15:23:30 +0200 | [diff] [blame] | 300 | "CRCX/RAN: creating connection for the RAN side on MGW endpoint:%s...\n", mgcp_ctx->rtp_endpoint); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 301 | |
| 302 | /* Generate MGCP message string */ |
| 303 | mgcp_msg = (struct mgcp_msg) { |
| 304 | .verb = MGCP_VERB_CRCX, |
| 305 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_MODE), |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 306 | .call_id = mgcp_ctx->call_id, |
Philipp Maier | 65d8d0d | 2018-05-29 10:03:41 +0200 | [diff] [blame] | 307 | .conn_mode = MGCP_CONN_RECV_ONLY |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 308 | }; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 309 | if (osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->rtp_endpoint, sizeof(mgcp_msg.endpoint)) >= |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 310 | MGCP_ENDPOINT_MAXLEN) { |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 311 | handle_error(mgcp_ctx, MGCP_ERR_TOOLONG, false); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 312 | return; |
| 313 | } |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 314 | |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 315 | /* HACK: We put the connection in loopback mode at the beginnig to |
Neels Hofmeyr | a4efe3f | 2018-07-26 17:44:13 +0200 | [diff] [blame] | 316 | * trick the hNodeB into doing the IuUP negotiation with itself. |
| 317 | * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */ |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 318 | #ifdef BUILD_IU |
| 319 | if (conn->via_ran == RAN_UTRAN_IU) |
| 320 | mgcp_msg.conn_mode = MGCP_CONN_LOOPBACK; |
| 321 | #endif |
| 322 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 323 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 324 | OSMO_ASSERT(msg); |
| 325 | |
| 326 | /* Transmit MGCP message to MGW */ |
| 327 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 328 | rc = mgcp_client_tx(mgcp, msg, mgw_crcx_ran_resp_cb, mgcp_ctx); |
| 329 | if (rc < 0) { |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 330 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TX_FAIL, false); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
| 334 | osmo_fsm_inst_state_chg(fi, ST_CRCX_CN, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 335 | } |
| 336 | |
| 337 | /* Callback for MGCP-Client: handle response for RAN associated CRCX */ |
| 338 | static void mgw_crcx_ran_resp_cb(struct mgcp_response *r, void *priv) |
| 339 | { |
| 340 | struct mgcp_ctx *mgcp_ctx = priv; |
| 341 | int rc; |
| 342 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 343 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 344 | |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 345 | /* NOTE: In case of error, we will not be able to perform any DLCX |
| 346 | * operation because until we either get a parseable message that |
| 347 | * contains an error code (no endpoint is seized in those cases) |
| 348 | * or we get an unparseable message. In this case we can not be |
| 349 | * sure, but we also can not draw any assumptions from unparseable |
| 350 | * messages. */ |
| 351 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 352 | OSMO_ASSERT(mgcp_ctx); |
| 353 | trans = mgcp_ctx->trans; |
| 354 | OSMO_ASSERT(trans); |
| 355 | conn = trans->conn; |
| 356 | OSMO_ASSERT(conn); |
| 357 | |
| 358 | if (r->head.response_code != 200) { |
| 359 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, |
| 360 | "CRCX/RAN: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 361 | handle_error(mgcp_ctx, MGCP_ERR_MGW_FAIL, false); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 365 | /* memorize connection identifier and specific endpoint id */ |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 366 | osmo_strlcpy(mgcp_ctx->conn_id_ran, r->head.conn_id, sizeof(mgcp_ctx->conn_id_ran)); |
| 367 | LOGPFSML(mgcp_ctx->fsm, LOGL_DEBUG, "CRCX/RAN: MGW responded with CI: %s\n", mgcp_ctx->conn_id_ran); |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 368 | osmo_strlcpy(mgcp_ctx->rtp_endpoint, r->head.endpoint, sizeof(mgcp_ctx->rtp_endpoint)); |
| 369 | LOGPFSML(mgcp_ctx->fsm, LOGL_DEBUG, "CRCX/RAN: MGW assigned endpoint: %s\n", mgcp_ctx->rtp_endpoint); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 370 | |
| 371 | rc = mgcp_response_parse_params(r); |
| 372 | if (rc) { |
| 373 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, "CRCX/RAN: Cannot parse response\n"); |
Philipp Maier | 79beccd | 2018-04-11 17:36:45 +0200 | [diff] [blame] | 374 | handle_error(mgcp_ctx, MGCP_ERR_MGW_INVAL_RESP, false); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
Neels Hofmeyr | 4e0bd4b | 2018-12-18 22:04:40 +0100 | [diff] [blame] | 378 | LOGPFSML(mgcp_ctx->fsm, LOGL_DEBUG, "CRCX/RAN: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 379 | |
| 380 | conn->rtp.local_port_ran = r->audio_port; |
| 381 | osmo_strlcpy(conn->rtp.local_addr_ran, r->audio_ip, sizeof(conn->rtp.local_addr_ran)); |
| 382 | |
| 383 | /* Notify the FSM that we got the response. */ |
| 384 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_CRCX_RAN_RESP, mgcp_ctx); |
| 385 | } |
| 386 | |
| 387 | static void mgw_crcx_cn_resp_cb(struct mgcp_response *r, void *priv); |
| 388 | |
| 389 | /* Callback for ST_CRCX_CN: check MGW response and send CRCX for CN side to MGW */ |
| 390 | static void fsm_crcx_cn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 391 | { |
| 392 | struct mgcp_ctx *mgcp_ctx = data; |
| 393 | struct mgcp_client *mgcp; |
| 394 | struct mgcp_msg mgcp_msg; |
| 395 | struct msgb *msg; |
| 396 | int rc; |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 397 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 398 | struct ran_conn *conn; |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 399 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 400 | OSMO_ASSERT(mgcp_ctx); |
| 401 | mgcp = mgcp_ctx->mgcp; |
| 402 | OSMO_ASSERT(mgcp); |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 403 | trans = mgcp_ctx->trans; |
| 404 | OSMO_ASSERT(trans); |
| 405 | conn = trans->conn; |
| 406 | OSMO_ASSERT(conn); |
Philipp Maier | 5046db9 | 2018-05-29 12:02:38 +0200 | [diff] [blame] | 407 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 408 | switch (event) { |
| 409 | case EV_CRCX_RAN_RESP: |
| 410 | break; |
| 411 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 412 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | LOGPFSML(fi, LOGL_DEBUG, |
Philipp Maier | d997fa1 | 2018-04-11 15:23:30 +0200 | [diff] [blame] | 417 | "CRCX/CN creating connection for the CN side on MGW endpoint:%s...\n", mgcp_ctx->rtp_endpoint); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 418 | |
| 419 | /* Generate MGCP message string */ |
| 420 | mgcp_msg = (struct mgcp_msg) { |
| 421 | .verb = MGCP_VERB_CRCX, |
| 422 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_MODE), |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 423 | .call_id = mgcp_ctx->call_id, |
Philipp Maier | 65d8d0d | 2018-05-29 10:03:41 +0200 | [diff] [blame] | 424 | .conn_mode = MGCP_CONN_RECV_ONLY |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 425 | }; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 426 | if (osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->rtp_endpoint, sizeof(mgcp_msg.endpoint)) >= |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 427 | MGCP_ENDPOINT_MAXLEN) { |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 428 | handle_error(mgcp_ctx, MGCP_ERR_TOOLONG, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 429 | return; |
| 430 | } |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 431 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 432 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 433 | OSMO_ASSERT(msg); |
| 434 | |
| 435 | /* Transmit MGCP message to MGW */ |
| 436 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 437 | rc = mgcp_client_tx(mgcp, msg, mgw_crcx_cn_resp_cb, mgcp_ctx); |
| 438 | if (rc < 0) { |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 439 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TX_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | osmo_fsm_inst_state_chg(fi, ST_CRCX_COMPL, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 444 | } |
| 445 | |
| 446 | /* Callback for MGCP-Client: handle response for CN associated CRCX */ |
| 447 | static void mgw_crcx_cn_resp_cb(struct mgcp_response *r, void *priv) |
| 448 | { |
| 449 | struct mgcp_ctx *mgcp_ctx = priv; |
| 450 | int rc; |
| 451 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 452 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 453 | |
| 454 | OSMO_ASSERT(mgcp_ctx); |
| 455 | trans = mgcp_ctx->trans; |
| 456 | OSMO_ASSERT(trans); |
| 457 | conn = trans->conn; |
| 458 | OSMO_ASSERT(conn); |
| 459 | |
| 460 | if (r->head.response_code != 200) { |
| 461 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, |
| 462 | "CRCX/CN: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 463 | handle_error(mgcp_ctx, MGCP_ERR_MGW_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
| 467 | /* memorize connection identifier */ |
| 468 | osmo_strlcpy(mgcp_ctx->conn_id_cn, r->head.conn_id, sizeof(mgcp_ctx->conn_id_cn)); |
| 469 | LOGPFSML(mgcp_ctx->fsm, LOGL_DEBUG, "CRCX/CN: MGW responded with CI: %s\n", mgcp_ctx->conn_id_cn); |
| 470 | |
| 471 | rc = mgcp_response_parse_params(r); |
| 472 | if (rc) { |
| 473 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, "CRCX/CN: Cannot parse response\n"); |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 474 | handle_error(mgcp_ctx, MGCP_ERR_MGW_INVAL_RESP, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 475 | return; |
| 476 | } |
| 477 | |
| 478 | LOGPFSML(mgcp_ctx->fsm, LOGL_DEBUG, "CRCX/CN: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port); |
| 479 | |
| 480 | conn->rtp.local_port_cn = r->audio_port; |
| 481 | osmo_strlcpy(conn->rtp.local_addr_cn, r->audio_ip, sizeof(conn->rtp.local_addr_cn)); |
| 482 | |
| 483 | /* Notify the FSM that we got the response. */ |
| 484 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_CRCX_CN_RESP, mgcp_ctx); |
| 485 | } |
| 486 | |
| 487 | /* Callback for ST_CRCX_COMPL: check MGW response, start assignment */ |
| 488 | static void fsm_crcx_compl(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 489 | { |
| 490 | struct mgcp_ctx *mgcp_ctx = data; |
| 491 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 492 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 493 | |
| 494 | OSMO_ASSERT(mgcp_ctx); |
| 495 | trans = mgcp_ctx->trans; |
| 496 | OSMO_ASSERT(trans); |
| 497 | conn = trans->conn; |
| 498 | OSMO_ASSERT(conn); |
| 499 | |
| 500 | switch (event) { |
| 501 | case EV_CRCX_CN_RESP: |
| 502 | break; |
| 503 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 504 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | |
| 508 | /* Forward assignment request to A/RANAP */ |
| 509 | if (conn->via_ran == RAN_UTRAN_IU) { |
| 510 | #ifdef BUILD_IU |
| 511 | /* Assign a voice channel via RANAP on 3G */ |
| 512 | if (iu_rab_act_cs(trans)) |
| 513 | goto error; |
| 514 | #else |
| 515 | LOGPFSML(fi, LOGL_ERROR, "Cannot send Iu RAB Assignment: built without Iu support\n"); |
| 516 | goto error; |
| 517 | #endif |
| 518 | } else if (conn->via_ran == RAN_GERAN_A) { |
| 519 | /* Assign a voice channel via A on 2G */ |
| 520 | if (a_iface_tx_assignment(trans)) |
| 521 | goto error; |
| 522 | } else { |
| 523 | /* Unset or unimplemented new RAN type */ |
| 524 | LOGPFSML(fi, LOGL_ERROR, "Unknown RAN type: %d\n", conn->via_ran); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | /* Respond back to MNCC (if requested) */ |
| 529 | if (trans->tch_rtp_create) { |
| 530 | if (gsm48_tch_rtp_create(trans)) |
| 531 | goto error; |
| 532 | } |
| 533 | |
| 534 | /* Note: When we reach this point then the situation is basically that |
| 535 | * we have two sides connected, both are in loopback. The local ports |
| 536 | * of the side pointing towards the BSS should be already communicated |
Philipp Maier | 4c57377 | 2018-02-08 14:05:43 +0100 | [diff] [blame] | 537 | * and we are waiting now the other end to pick up. */ |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 538 | osmo_fsm_inst_state_chg(fi, ST_MDCX_CN, MGCP_RAN_TIMEOUT, MGCP_RAN_TIMEOUT_TIMER_NR); |
| 539 | return; |
| 540 | |
| 541 | error: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 542 | handle_error(mgcp_ctx, MGCP_ERR_ASSGMNT_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static void mgw_mdcx_cn_resp_cb(struct mgcp_response *r, void *priv); |
| 546 | |
| 547 | /* Callback for ST_MDCX_CN: send MDCX for RAN side to MGW */ |
| 548 | static void fsm_mdcx_cn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 549 | { |
| 550 | struct mgcp_ctx *mgcp_ctx = data; |
| 551 | struct mgcp_client *mgcp; |
| 552 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 553 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 554 | struct mgcp_msg mgcp_msg; |
| 555 | struct msgb *msg; |
| 556 | int rc; |
| 557 | |
| 558 | OSMO_ASSERT(mgcp_ctx); |
| 559 | mgcp = mgcp_ctx->mgcp; |
| 560 | OSMO_ASSERT(mgcp); |
| 561 | trans = mgcp_ctx->trans; |
| 562 | OSMO_ASSERT(trans); |
| 563 | conn = trans->conn; |
| 564 | OSMO_ASSERT(conn); |
| 565 | |
| 566 | switch (event) { |
| 567 | case EV_CONNECT: |
| 568 | break; |
| 569 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 570 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| 574 | LOGPFSML(fi, LOGL_DEBUG, |
Pau Espin Pedrol | 9fac985 | 2018-03-17 01:54:32 +0100 | [diff] [blame] | 575 | "MDCX/CN: completing connection for the CN side on MGW endpoint:%p, remote leg expects RTP input on address %s:%u\n", |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 576 | mgcp_ctx->rtp_endpoint, conn->rtp.remote_addr_cn, conn->rtp.remote_port_cn); |
| 577 | |
| 578 | /* Generate MGCP message string */ |
| 579 | mgcp_msg = (struct mgcp_msg) { |
| 580 | .verb = MGCP_VERB_MDCX, |
| 581 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID | |
| 582 | MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | |
| 583 | MGCP_MSG_PRESENCE_AUDIO_PORT), |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 584 | .call_id = mgcp_ctx->call_id, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 585 | .conn_id = mgcp_ctx->conn_id_cn, |
| 586 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 587 | .audio_ip = conn->rtp.remote_addr_cn, |
Philipp Maier | 8ad3dac | 2018-08-07 13:00:14 +0200 | [diff] [blame] | 588 | .audio_port = conn->rtp.remote_port_cn, |
| 589 | .codecs[0] = conn->rtp.codec_cn, |
| 590 | .codecs_len = 1 |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 591 | }; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 592 | if (osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->rtp_endpoint, sizeof(mgcp_msg.endpoint)) >= |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 593 | MGCP_ENDPOINT_MAXLEN) { |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 594 | handle_error(mgcp_ctx, MGCP_ERR_TOOLONG, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 595 | return; |
| 596 | } |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 597 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 598 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 599 | OSMO_ASSERT(msg); |
| 600 | |
| 601 | /* Transmit MGCP message to MGW */ |
| 602 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 603 | rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_cn_resp_cb, mgcp_ctx); |
| 604 | if (rc < 0) { |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 605 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TX_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | osmo_fsm_inst_state_chg(fi, ST_MDCX_CN_COMPL, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 610 | } |
| 611 | |
| 612 | /* Callback for MGCP-Client: handle response for CN associated CRCX */ |
| 613 | static void mgw_mdcx_cn_resp_cb(struct mgcp_response *r, void *priv) |
| 614 | { |
| 615 | struct mgcp_ctx *mgcp_ctx = priv; |
| 616 | |
| 617 | OSMO_ASSERT(mgcp_ctx); |
| 618 | |
| 619 | if (r->head.response_code != 200) { |
| 620 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, |
| 621 | "MDCX/CN: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 622 | handle_error(mgcp_ctx, MGCP_ERR_MGW_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
| 626 | /* Notify the FSM that we got the response. */ |
| 627 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_MDCX_CN_RESP, mgcp_ctx); |
| 628 | } |
| 629 | |
| 630 | /* Callback for ST_MDCX_CN_COMPL: wait for mgw response, move on with the MDCX |
| 631 | * for the RAN side if we already have valid IP/Port data for the RAN sided |
| 632 | * RTP stream. */ |
| 633 | static void fsm_mdcx_cn_compl_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 634 | { |
| 635 | struct mgcp_ctx *mgcp_ctx = data; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 636 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 637 | struct gsm_trans *trans; |
| 638 | |
| 639 | OSMO_ASSERT(mgcp_ctx); |
| 640 | trans = mgcp_ctx->trans; |
| 641 | OSMO_ASSERT(trans); |
| 642 | conn = trans->conn; |
| 643 | OSMO_ASSERT(conn); |
| 644 | |
| 645 | switch (event) { |
| 646 | case EV_MDCX_CN_RESP: |
| 647 | break; |
| 648 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 649 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | /* Enter MDCX phase, but we must be sure that the Assigmnet on the A or |
| 654 | * IuCS interface is complete (IP-Address and Port are valid) */ |
| 655 | osmo_fsm_inst_state_chg(fi, ST_MDCX_RAN, MGCP_ASS_TIMEOUT, MGCP_ASS_TIMEOUT_TIMER_NR); |
| 656 | |
| 657 | /* If we already have a valid remote port and IP-Address from the RAN side |
| 658 | * call leg, the assignment has been completed before we got here, so we |
| 659 | * may move on immediately */ |
| 660 | if (conn->rtp.remote_port_ran != 0 || strlen(conn->rtp.remote_addr_ran) > 0) |
| 661 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_ASSIGN, mgcp_ctx); |
| 662 | } |
| 663 | |
| 664 | static void mgw_mdcx_ran_resp_cb(struct mgcp_response *r, void *priv); |
| 665 | |
| 666 | /* Callback for ST_MDCX_RAN: wait for assignment completion, send MDCX for CN side to MGW */ |
| 667 | static void fsm_mdcx_ran_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 668 | { |
| 669 | struct mgcp_ctx *mgcp_ctx = data; |
| 670 | struct mgcp_client *mgcp; |
| 671 | struct gsm_trans *trans; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 672 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 673 | struct mgcp_msg mgcp_msg; |
| 674 | struct msgb *msg; |
| 675 | int rc; |
| 676 | |
| 677 | OSMO_ASSERT(mgcp_ctx); |
| 678 | mgcp = mgcp_ctx->mgcp; |
| 679 | OSMO_ASSERT(mgcp); |
| 680 | trans = mgcp_ctx->trans; |
| 681 | OSMO_ASSERT(trans); |
| 682 | conn = trans->conn; |
| 683 | OSMO_ASSERT(conn); |
| 684 | |
| 685 | switch (event) { |
| 686 | case EV_ASSIGN: |
| 687 | break; |
| 688 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 689 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | |
| 693 | LOGPFSML(fi, LOGL_DEBUG, |
Pau Espin Pedrol | 9fac985 | 2018-03-17 01:54:32 +0100 | [diff] [blame] | 694 | "MDCX/RAN: completing connection for the CN side on MGW endpoint:%p, RAN expects RTP input on address %s:%u\n", |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 695 | mgcp_ctx->rtp_endpoint, conn->rtp.remote_addr_ran, conn->rtp.remote_port_ran); |
| 696 | |
| 697 | /* Generate MGCP message string */ |
| 698 | mgcp_msg = (struct mgcp_msg) { |
| 699 | .verb = MGCP_VERB_MDCX, |
| 700 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID | |
| 701 | MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | |
| 702 | MGCP_MSG_PRESENCE_AUDIO_PORT), |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 703 | .call_id = mgcp_ctx->call_id, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 704 | .conn_id = mgcp_ctx->conn_id_ran, |
| 705 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 706 | .audio_ip = conn->rtp.remote_addr_ran, |
Philipp Maier | 8ad3dac | 2018-08-07 13:00:14 +0200 | [diff] [blame] | 707 | .audio_port = conn->rtp.remote_port_ran, |
| 708 | .codecs[0] = conn->rtp.codec_ran, |
| 709 | .codecs_len = 1 |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 710 | }; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 711 | if (osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->rtp_endpoint, sizeof(mgcp_msg.endpoint)) >= |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 712 | MGCP_ENDPOINT_MAXLEN) { |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 713 | handle_error(mgcp_ctx, MGCP_ERR_TOOLONG, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 714 | return; |
| 715 | } |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 716 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 717 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 718 | OSMO_ASSERT(msg); |
| 719 | |
| 720 | /* Transmit MGCP message to MGW */ |
| 721 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 722 | rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_ran_resp_cb, mgcp_ctx); |
| 723 | if (rc < 0) { |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 724 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TX_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
| 728 | osmo_fsm_inst_state_chg(fi, ST_MDCX_RAN_COMPL, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 729 | } |
| 730 | |
| 731 | /* Callback for MGCP-Client: handle response for CN associated CRCX */ |
| 732 | static void mgw_mdcx_ran_resp_cb(struct mgcp_response *r, void *priv) |
| 733 | { |
| 734 | struct mgcp_ctx *mgcp_ctx = priv; |
| 735 | |
| 736 | OSMO_ASSERT(mgcp_ctx); |
| 737 | |
| 738 | if (r->head.response_code != 200) { |
| 739 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, |
| 740 | "MDCX/RAN: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 741 | handle_error(mgcp_ctx, MGCP_ERR_MGW_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | |
| 745 | /* Notify the FSM that we got the response. */ |
| 746 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_MDCX_RAN_RESP, mgcp_ctx); |
| 747 | } |
| 748 | |
| 749 | /* Callback for ST_MDCX_RAN_COMPL: check MGW response */ |
| 750 | static void fsm_mdcx_ran_compl_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 751 | { |
| 752 | struct mgcp_ctx *mgcp_ctx = data; |
| 753 | OSMO_ASSERT(mgcp_ctx); |
| 754 | |
| 755 | switch (event) { |
| 756 | case EV_MDCX_RAN_RESP: |
| 757 | break; |
| 758 | default: |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 759 | handle_error(mgcp_ctx, MGCP_ERR_UNEXP_TEARDOWN, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
| 763 | LOGPFSML(fi, LOGL_DEBUG, "call active, waiting for teardown...\n"); |
| 764 | osmo_fsm_inst_state_chg(fi, ST_CALL, 0, 0); |
| 765 | } |
| 766 | |
| 767 | static void mgw_dlcx_all_resp_cb(struct mgcp_response *r, void *priv); |
| 768 | |
| 769 | /* Callback for ST_CALL: call is active, send DLCX for both sides on teardown */ |
| 770 | static void fsm_call_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 771 | { |
| 772 | |
| 773 | struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; |
| 774 | struct mgcp_client *mgcp; |
| 775 | struct mgcp_msg mgcp_msg; |
| 776 | struct msgb *msg; |
| 777 | int rc; |
| 778 | |
| 779 | OSMO_ASSERT(mgcp_ctx); |
| 780 | mgcp = mgcp_ctx->mgcp; |
| 781 | OSMO_ASSERT(mgcp); |
| 782 | |
| 783 | LOGPFSML(fi, LOGL_DEBUG, |
Philipp Maier | d997fa1 | 2018-04-11 15:23:30 +0200 | [diff] [blame] | 784 | "DLCX: removing connection for the RAN and CN side on MGW endpoint:%s...\n", mgcp_ctx->rtp_endpoint); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 785 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 786 | /* Generate MGCP message string */ |
| 787 | mgcp_msg = (struct mgcp_msg) { |
| 788 | .verb = MGCP_VERB_DLCX, |
| 789 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID), |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 790 | .call_id = mgcp_ctx->call_id |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 791 | }; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 792 | if (osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->rtp_endpoint, sizeof(mgcp_msg.endpoint)) >= |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 793 | MGCP_ENDPOINT_MAXLEN) { |
Philipp Maier | 3a77652 | 2018-02-22 12:00:00 +0100 | [diff] [blame] | 794 | handle_error(mgcp_ctx, MGCP_ERR_TOOLONG, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 795 | return; |
| 796 | } |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 797 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 798 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 799 | OSMO_ASSERT(msg); |
| 800 | |
| 801 | /* Transmit MGCP message to MGW */ |
| 802 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 803 | rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_all_resp_cb, mgcp_ctx); |
| 804 | if (rc < 0) { |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 805 | handle_error(mgcp_ctx, MGCP_ERR_MGW_TX_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 806 | return; |
| 807 | } |
| 808 | |
| 809 | osmo_fsm_inst_state_chg(fi, ST_HALT, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 810 | } |
| 811 | |
| 812 | /* Callback for MGCP-Client: handle response for CN associated CRCX */ |
| 813 | static void mgw_dlcx_all_resp_cb(struct mgcp_response *r, void *priv) |
| 814 | { |
| 815 | struct mgcp_ctx *mgcp_ctx = priv; |
| 816 | |
| 817 | OSMO_ASSERT(mgcp_ctx); |
| 818 | |
Harald Welte | 33d61e7 | 2018-02-10 10:43:38 +0100 | [diff] [blame] | 819 | /* DLCX is the only command where 250 is permitted as positive result */ |
| 820 | if (r->head.response_code != 200 && r->head.response_code != 250) { |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 821 | LOGPFSML(mgcp_ctx->fsm, LOGL_ERROR, |
| 822 | "DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
Philipp Maier | 4eef20b | 2018-03-13 13:07:45 +0100 | [diff] [blame] | 823 | handle_error(mgcp_ctx, MGCP_ERR_MGW_FAIL, true); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 824 | return; |
| 825 | } |
| 826 | |
| 827 | /* Notify the FSM that we got the response. */ |
| 828 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_DLCX_ALL_RESP, mgcp_ctx); |
| 829 | } |
| 830 | |
| 831 | /* Callback for ST_HALT: Terminate the state machine */ |
| 832 | static void fsm_halt_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 833 | { |
| 834 | struct mgcp_ctx *mgcp_ctx = data; |
Philipp Maier | addf63b | 2018-03-14 13:37:44 +0100 | [diff] [blame] | 835 | struct mgcp_client *mgcp; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 836 | |
| 837 | OSMO_ASSERT(mgcp_ctx); |
Philipp Maier | addf63b | 2018-03-14 13:37:44 +0100 | [diff] [blame] | 838 | mgcp = mgcp_ctx->mgcp; |
| 839 | OSMO_ASSERT(mgcp); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 840 | |
| 841 | /* NOTE: We must not free the context information now, we have to |
| 842 | * wait until msc_mgcp_call_release() is called. Then we are sure |
| 843 | * that the logic controlling us is fully aware that the context |
| 844 | * information is freed. If we would free early now the controlling |
| 845 | * logic might mistakenly think that the context info is still alive, |
| 846 | * so lets keep the context info until we are explicitly asked for |
| 847 | * throwing it away. */ |
| 848 | if (mgcp_ctx->free_ctx) { |
Philipp Maier | addf63b | 2018-03-14 13:37:44 +0100 | [diff] [blame] | 849 | /* Be sure that there is no pending MGW transaction */ |
| 850 | mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans); |
| 851 | |
| 852 | /* Free FSM and its context information */ |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 853 | osmo_fsm_inst_free(mgcp_ctx->fsm); |
| 854 | talloc_free(mgcp_ctx); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | osmo_fsm_inst_state_chg(fi, ST_HALT, MGCP_REL_TIMEOUT, MGCP_REL_TIMEOUT_TIMER_NR); |
| 859 | } |
| 860 | |
| 861 | static struct osmo_fsm_state fsm_msc_mgcp_states[] = { |
| 862 | |
| 863 | /* Startup state machine, send CRCX for RAN side. */ |
| 864 | [ST_CRCX_RAN] = { |
| 865 | .in_event_mask = S(EV_INIT), |
| 866 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_CRCX_CN), |
| 867 | .name = OSMO_STRINGIFY(ST_CRCX_RAN), |
| 868 | .action = fsm_crcx_ran_cb, |
| 869 | }, |
| 870 | /* When the response to the RAN CRCX is received, then proceed with |
| 871 | sending the CRCX for CN side */ |
| 872 | [ST_CRCX_CN] = { |
| 873 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_CRCX_RAN_RESP), |
| 874 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_CRCX_COMPL), |
| 875 | .name = OSMO_STRINGIFY(ST_CRCX_CN), |
| 876 | .action = fsm_crcx_cn_cb, |
| 877 | }, |
| 878 | /* Complete the CRCX phase by starting the assignment. Depending on the |
| 879 | * RAT (Radio Access Technology), this will either trigger an |
| 880 | * Assignment Request on the A-Interface or an RAB-Assignment on the |
| 881 | * IU-interface */ |
| 882 | [ST_CRCX_COMPL] = { |
| 883 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_CRCX_CN_RESP), |
| 884 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_MDCX_CN), |
| 885 | .name = OSMO_STRINGIFY(ST_CRCX_COMPL), |
| 886 | .action = fsm_crcx_compl, |
| 887 | }, |
| 888 | /* Wait for MSC to complete the assignment request, when complete, we |
| 889 | * will enter the MDCX phase by sending an MDCX for the CN side to the |
| 890 | * MGW */ |
| 891 | [ST_MDCX_CN] = { |
| 892 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_CONNECT), |
| 893 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_MDCX_CN_COMPL), |
| 894 | .name = OSMO_STRINGIFY(ST_MDCX_CN), |
| 895 | .action = fsm_mdcx_cn_cb, |
| 896 | }, |
| 897 | /* We arrive in this state when the MDCX phase for the CN side has |
| 898 | * completed we will check the IP/Port of the RAN connection. If this |
| 899 | * data is valid we may continue with the MDCX phase for the RAN side. |
| 900 | * If not we wait until the assinment completes on the A or on the IuCS |
| 901 | * interface. The completion of the assignment will fill in the port and |
| 902 | * IP-Address of the RAN side and way may continue then. */ |
| 903 | [ST_MDCX_CN_COMPL] = { |
| 904 | .in_event_mask = S(EV_TEARDOWN) | S(EV_MDCX_CN_RESP), |
| 905 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_MDCX_RAN), |
| 906 | .name = OSMO_STRINGIFY(ST_MDCX_CN_COMPL), |
| 907 | .action = fsm_mdcx_cn_compl_cb, |
| 908 | }, |
| 909 | /* When the response for the CN MDCX is received, send the MDCX for the |
| 910 | * RAN side to the MGW */ |
| 911 | [ST_MDCX_RAN] = { |
| 912 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_ASSIGN), |
| 913 | .out_state_mask = S(ST_HALT) | S(ST_CALL) | S(ST_MDCX_RAN_COMPL), |
| 914 | .name = OSMO_STRINGIFY(ST_MDCX_RAN), |
| 915 | .action = fsm_mdcx_ran_cb, |
| 916 | }, |
| 917 | /* The RAN side MDCX phase is complete when the response is received |
| 918 | * from the MGW. The call is then active, we change to ST_CALL and wait |
| 919 | * there until the call ends. */ |
| 920 | [ST_MDCX_RAN_COMPL] = { |
| 921 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_MDCX_RAN_RESP), |
| 922 | .out_state_mask = S(ST_HALT) | S(ST_CALL), |
| 923 | .name = OSMO_STRINGIFY(ST_MDCX_RAN_COMPL), |
| 924 | .action = fsm_mdcx_ran_compl_cb, |
| 925 | }, |
| 926 | /* We are now in the active call phase, wait until the call is done |
| 927 | * and send a DLCX then to remove all connections from the MGW */ |
| 928 | [ST_CALL] = { |
| 929 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR), |
| 930 | .out_state_mask = S(ST_HALT), |
| 931 | .name = OSMO_STRINGIFY(ST_CALL), |
| 932 | .action = fsm_call_cb, |
| 933 | }, |
| 934 | /* When the MGW confirms that the connections are terminated, then halt |
| 935 | * the state machine. */ |
| 936 | [ST_HALT] = { |
| 937 | .in_event_mask = S(EV_TEARDOWN) | S(EV_TEARDOWN_ERROR) | S(EV_DLCX_ALL_RESP), |
| 938 | .out_state_mask = S(ST_HALT), |
| 939 | .name = OSMO_STRINGIFY(ST_HALT), |
| 940 | .action = fsm_halt_cb, |
| 941 | }, |
| 942 | }; |
| 943 | |
| 944 | /* State machine definition */ |
| 945 | static struct osmo_fsm fsm_msc_mgcp = { |
| 946 | .name = "MGW", |
| 947 | .states = fsm_msc_mgcp_states, |
| 948 | .num_states = ARRAY_SIZE(fsm_msc_mgcp_states), |
| 949 | .log_subsys = DMGCP, |
| 950 | .timer_cb = fsm_timeout_cb, |
Philipp Maier | e4f9172 | 2018-02-26 15:20:49 +0100 | [diff] [blame] | 951 | .event_names = msc_mgcp_fsm_evt_names, |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 952 | }; |
| 953 | |
Neels Hofmeyr | a35712d | 2018-12-20 03:03:44 +0100 | [diff] [blame^] | 954 | /* Try to invoke call assignment and set trans->cc.assignment_started flag if invoked. |
Neels Hofmeyr | b16259f | 2018-12-20 02:57:56 +0100 | [diff] [blame] | 955 | * This is relevant for already ongoing calls -- scenario: |
| 956 | * - subscriber is in an active voice call, |
| 957 | * - another call is coming in. |
| 958 | * For the second call coming in, we must wait to establish RTP and assignment until the first call is CC-Disconnected. |
| 959 | */ |
| 960 | int msc_mgcp_try_call_assignment(struct gsm_trans *trans) |
| 961 | { |
| 962 | struct ran_conn *conn = trans->conn; |
Neels Hofmeyr | a35712d | 2018-12-20 03:03:44 +0100 | [diff] [blame^] | 963 | if (trans->cc.assignment_started) |
Neels Hofmeyr | b16259f | 2018-12-20 02:57:56 +0100 | [diff] [blame] | 964 | return 0; |
| 965 | LOGPFSMSL(conn->fi, DMGCP, LOGL_INFO, "Starting call assignment\n"); |
Neels Hofmeyr | a35712d | 2018-12-20 03:03:44 +0100 | [diff] [blame^] | 966 | trans->cc.assignment_started = true; |
Neels Hofmeyr | b16259f | 2018-12-20 02:57:56 +0100 | [diff] [blame] | 967 | return msc_mgcp_call_assignment(trans); |
| 968 | } |
| 969 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 970 | /* Notify that a new call begins. This will create a connection for the |
| 971 | * RAN and the CN on the MGW. |
| 972 | * Parameter: |
| 973 | * trans: transaction context. |
| 974 | * Returns -EINVAL on error, 0 on success. */ |
| 975 | int msc_mgcp_call_assignment(struct gsm_trans *trans) |
| 976 | { |
| 977 | struct mgcp_ctx *mgcp_ctx; |
| 978 | char name[32]; |
| 979 | static bool fsm_registered = false; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 980 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 981 | struct mgcp_client *mgcp; |
| 982 | |
| 983 | OSMO_ASSERT(trans); |
| 984 | |
| 985 | if (!trans->conn) { |
| 986 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid conn, call assignment failed\n", |
| 987 | vlr_subscr_name(trans->vsub)); |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
| 991 | conn = trans->conn; |
| 992 | mgcp = conn->network->mgw.client; |
| 993 | OSMO_ASSERT(mgcp); |
| 994 | |
| 995 | if (conn->rtp.mgcp_ctx) { |
| 996 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) double assignment detected, dropping...\n", |
| 997 | vlr_subscr_name(trans->vsub)); |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | #ifdef BUILD_IU |
| 1002 | /* FIXME: HACK. where to scope the RAB Id? At the conn / subscriber / ranap_ue_conn_ctx? */ |
| 1003 | static uint8_t next_iu_rab_id = 1; |
| 1004 | if (conn->via_ran == RAN_UTRAN_IU) |
| 1005 | conn->iu.rab_id = next_iu_rab_id++; |
| 1006 | #endif |
| 1007 | |
| 1008 | if (snprintf(name, sizeof(name), "MGW_%i", trans->transaction_id) >= sizeof(name)) |
| 1009 | return -EINVAL; |
| 1010 | |
| 1011 | /* Register the fsm description (if not already done) */ |
| 1012 | if (fsm_registered == false) { |
| 1013 | osmo_fsm_register(&fsm_msc_mgcp); |
| 1014 | fsm_registered = true; |
| 1015 | } |
| 1016 | |
| 1017 | /* Allocate and configure a new fsm instance */ |
| 1018 | mgcp_ctx = talloc_zero(NULL, struct mgcp_ctx); |
| 1019 | OSMO_ASSERT(mgcp_ctx); |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 1020 | if (osmo_strlcpy(mgcp_ctx->rtp_endpoint, ENDPOINT_ID, sizeof(mgcp_ctx->rtp_endpoint)) >= |
| 1021 | MGCP_ENDPOINT_MAXLEN) { |
| 1022 | talloc_free(mgcp_ctx); |
| 1023 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) endpoint identifier (%s) exceeds maximum length...\n", |
| 1024 | vlr_subscr_name(trans->vsub), ENDPOINT_ID); |
| 1025 | return -EINVAL; |
| 1026 | } |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1027 | mgcp_ctx->fsm = osmo_fsm_inst_alloc(&fsm_msc_mgcp, NULL, NULL, LOGL_DEBUG, name); |
| 1028 | OSMO_ASSERT(mgcp_ctx->fsm); |
| 1029 | mgcp_ctx->fsm->priv = mgcp_ctx; |
| 1030 | mgcp_ctx->mgcp = mgcp; |
| 1031 | mgcp_ctx->trans = trans; |
Philipp Maier | a2353c6 | 2018-02-08 14:15:59 +0100 | [diff] [blame] | 1032 | mgcp_ctx->call_id = trans->callref; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1033 | |
| 1034 | /* start state machine */ |
| 1035 | OSMO_ASSERT(mgcp_ctx->fsm->state == ST_CRCX_RAN); |
| 1036 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_INIT, mgcp_ctx); |
| 1037 | |
| 1038 | conn->rtp.mgcp_ctx = mgcp_ctx; |
| 1039 | |
| 1040 | LOGP(DMGCP, LOGL_DEBUG, "(subscriber:%s) call assignment initiated\n", |
| 1041 | vlr_subscr_name(conn->vsub)); |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1046 | /* Inform the FSM that the assignment (RAN connection) is now complete. |
| 1047 | * Parameter: |
| 1048 | * conn: RAN connection context. |
| 1049 | * port: port number of the remote leg. |
| 1050 | * addr: IP-address of the remote leg. |
| 1051 | * Returns -EINVAL on error, 0 on success. */ |
| 1052 | int msc_mgcp_ass_complete(struct ran_conn *conn, uint16_t port, char *addr) |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1053 | { |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1054 | struct mgcp_ctx *mgcp_ctx; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1055 | |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1056 | OSMO_ASSERT(conn); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1057 | |
| 1058 | if (port == 0) { |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1059 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid remote call leg port, assignment completion failed\n", |
| 1060 | vlr_subscr_name(conn->vsub)); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | if (!addr || strlen(addr) <= 0) { |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1064 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) missing remote call leg address, assignment completion failed\n", |
| 1065 | vlr_subscr_name(conn->vsub)); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1066 | return -EINVAL; |
| 1067 | } |
| 1068 | |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1069 | mgcp_ctx = conn->rtp.mgcp_ctx; |
| 1070 | if (!mgcp_ctx) { |
| 1071 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid mgcp context, assignment completion failed.\n", |
| 1072 | vlr_subscr_name(conn->vsub)); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
| 1076 | /* Memorize port and IP-Address of the remote RAN call leg. We need this |
| 1077 | * information at latest when we enter the MDCX phase for the RAN side. */ |
Neels Hofmeyr | 212c0c9 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1078 | conn->rtp.remote_port_ran = port; |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1079 | osmo_strlcpy(conn->rtp.remote_addr_ran, addr, sizeof(conn->rtp.remote_addr_ran)); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1080 | |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1081 | LOGP(DMGCP, LOGL_DEBUG, "(subscriber:%s) assignment completed, rtp %s:%d.\n", |
| 1082 | vlr_subscr_name(conn->vsub), conn->rtp.remote_addr_ran, port); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1083 | |
Neels Hofmeyr | 85cb253 | 2018-12-11 18:59:27 +0100 | [diff] [blame] | 1084 | /* Note: We only dispatch the event if we are really waiting for the |
| 1085 | * assignment, if we are not yet waiting, there is no need to loudly |
| 1086 | * broadcast an event that the all other states do not understand anyway */ |
| 1087 | if (mgcp_ctx->fsm->state == ST_MDCX_RAN) |
| 1088 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_ASSIGN, mgcp_ctx); |
| 1089 | |
| 1090 | return 0; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* Make the connection of a previously assigned call complete |
| 1094 | * Parameter: |
| 1095 | * trans: transaction context. |
| 1096 | * port: port number of the remote leg. |
| 1097 | * addr: IP-address of the remote leg. |
| 1098 | * Returns -EINVAL on error, 0 on success. */ |
| 1099 | int msc_mgcp_call_complete(struct gsm_trans *trans, uint16_t port, char *addr) |
| 1100 | { |
| 1101 | struct mgcp_ctx *mgcp_ctx; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 1102 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1103 | |
| 1104 | OSMO_ASSERT(trans); |
| 1105 | OSMO_ASSERT(addr); |
| 1106 | |
| 1107 | if (port == 0) { |
| 1108 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid remote call leg port, call completion failed\n", |
| 1109 | vlr_subscr_name(trans->vsub)); |
| 1110 | return -EINVAL; |
| 1111 | } |
| 1112 | if (!addr || strlen(addr) <= 0) { |
| 1113 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) missing remote call leg address, call completion failed\n", |
| 1114 | vlr_subscr_name(trans->vsub)); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | if (!trans->conn) { |
| 1118 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid conn, call completion failed\n", |
| 1119 | vlr_subscr_name(trans->vsub)); |
| 1120 | return -EINVAL; |
| 1121 | } |
| 1122 | if (!trans->conn->rtp.mgcp_ctx) { |
| 1123 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid mgcp context, call completion failed.\n", |
| 1124 | vlr_subscr_name(trans->vsub)); |
| 1125 | return -EINVAL; |
| 1126 | } |
| 1127 | if (!trans->conn->rtp.mgcp_ctx->fsm) { |
| 1128 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) no FSM, call completion failed\n", |
| 1129 | vlr_subscr_name(trans->vsub)); |
| 1130 | return -EINVAL; |
| 1131 | } |
| 1132 | |
| 1133 | mgcp_ctx = trans->conn->rtp.mgcp_ctx; |
| 1134 | |
| 1135 | /* The FSM should already have passed all CRCX phases and be ready to move |
| 1136 | * on with the MDCX phases. */ |
| 1137 | if (mgcp_ctx->fsm->state != ST_MDCX_CN) { |
| 1138 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid call state, call completion failed\n", |
| 1139 | vlr_subscr_name(trans->vsub)); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | |
| 1143 | conn = trans->conn; |
| 1144 | osmo_strlcpy(conn->rtp.remote_addr_cn, addr, sizeof(conn->rtp.remote_addr_cn)); |
| 1145 | conn->rtp.remote_port_cn = port; |
| 1146 | |
| 1147 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_CONNECT, mgcp_ctx); |
| 1148 | |
| 1149 | LOGP(DMGCP, LOGL_DEBUG, "(subscriber:%s) call completion initiated\n", |
| 1150 | vlr_subscr_name(conn->vsub)); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | /* Release ongoing call. |
| 1156 | * Parameter: |
| 1157 | * trans: connection context. |
| 1158 | * Returns -EINVAL on error, 0 on success. */ |
| 1159 | int msc_mgcp_call_release(struct gsm_trans *trans) |
| 1160 | { |
| 1161 | struct mgcp_ctx *mgcp_ctx; |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1162 | struct ran_conn *conn = trans->conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1163 | |
| 1164 | OSMO_ASSERT(trans); |
| 1165 | |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1166 | if (!conn) { |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1167 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid conn, call release failed\n", |
| 1168 | vlr_subscr_name(trans->vsub)); |
| 1169 | return -EINVAL; |
| 1170 | } |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1171 | mgcp_ctx = conn->rtp.mgcp_ctx; |
| 1172 | if (!mgcp_ctx) { |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1173 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) invalid mgcp context, call release failed.\n", |
| 1174 | vlr_subscr_name(trans->vsub)); |
| 1175 | return -EINVAL; |
| 1176 | } |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1177 | if (!mgcp_ctx->fsm) { |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1178 | LOGP(DMGCP, LOGL_ERROR, "(subscriber:%s) no FSM, call release failed\n", |
| 1179 | vlr_subscr_name(trans->vsub)); |
| 1180 | return -EINVAL; |
| 1181 | } |
| 1182 | |
Neels Hofmeyr | 3350bf9 | 2018-12-21 01:35:21 +0100 | [diff] [blame] | 1183 | if (mgcp_ctx->trans != trans) { |
| 1184 | LOGP(DMGCP, LOGL_DEBUG, "(ti %02x %s) call release for background CC transaction\n", |
| 1185 | trans->transaction_id, vlr_subscr_name(trans->vsub)); |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1189 | LOGP(DMGCP, LOGL_DEBUG, "(ti %02x %s) Call release: tearing down MGW endpoint\n", |
| 1190 | trans->transaction_id, vlr_subscr_name(trans->vsub)); |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1191 | |
| 1192 | /* Inform the FSM that as soon as it reaches ST_HALT it may free |
| 1193 | * all context information immediately */ |
| 1194 | mgcp_ctx->free_ctx = true; |
| 1195 | |
| 1196 | /* Initaite teardown, regardless of which state we are currently |
| 1197 | * in */ |
| 1198 | osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_TEARDOWN, mgcp_ctx); |
| 1199 | |
| 1200 | /* Prevent any further operation that is triggered from outside by |
| 1201 | * overwriting the context pointer with NULL. The FSM will now |
| 1202 | * take care for a graceful shutdown and when done it will free |
| 1203 | * all related context information */ |
Neels Hofmeyr | c43b966 | 2018-12-21 01:34:48 +0100 | [diff] [blame] | 1204 | conn->rtp.mgcp_ctx = NULL; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 1205 | |
| 1206 | return 0; |
| 1207 | } |