blob: 218527cfb8b3039fec956e084ba013b3e9f782e8 [file] [log] [blame]
Philipp Maier8bda7a72018-01-17 14:32:23 +01001/* (C) 2018 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 General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/mgcp_client/mgcp_client.h>
22#include <osmocom/mgcp_client/mgcp_client_fsm.h>
23#include <osmocom/core/utils.h>
24#include <osmocom/core/fsm.h>
25#include <osmocom/core/byteswap.h>
26#include <arpa/inet.h>
27#include <osmocom/core/logging.h>
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +020028#include <osmocom/core/sockaddr_str.h>
Philipp Maier8bda7a72018-01-17 14:32:23 +010029
30/* Context information, this is attached to the priv pointer of the FSM and
31 * is also handed back when dispatcheing events to the parent FSM. This is
32 * purly intened and not meant to be accessible for the API user */
33struct mgcp_ctx {
34 /* MGCP client instance that is used to interact with the MGW */
35 struct mgcp_client *mgcp;
36
37 /* The ID of the last pending transaction. This is used internally
38 * to cancel the transaction in case of an error */
39 mgcp_trans_id_t mgw_pending_trans;
40
41 /* Flag to mark that there is a pending transaction */
42 bool mgw_trans_pending;
43
44 /* Connection ID which has been assigned by he MGW */
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020045 char conn_id[MGCP_CONN_ID_MAXLEN];
Philipp Maier8bda7a72018-01-17 14:32:23 +010046
47 /* Local RTP connection info, the MGW will send outgoing traffic to the
48 * ip/port specified here. The Address does not have to be choosen right
49 * on the creation of a connection. It can always be modified later by
50 * the user. */
51 struct mgcp_conn_peer conn_peer_local;
52
53 /* Remote RTP connection info, the ip/port specified here is the address
54 * where the MGW expects the RTP data to be sent. This address is
55 * defined by soly by the MGW and can not be influenced by the user. */
56 struct mgcp_conn_peer conn_peer_remote;
57
58 /* The terminate flag is a way to handle cornercase sitations that
59 * might occur when the user runs into an error situation and sends
60 * a DLCX command while the FSM is waiting for a response. In this
61 * case the DLCX command is not executed immediately. Instead the
62 * terminate flag is set. When the response to from the previous
63 * operation is received, we know that there is a DLCX event is
64 * pending. The FSM then generates the EV_DLCX by itsself before
65 * it enters ST_READY to cause the immediate execution of the
66 * DLCX procedure. (If normal operations are executed too fast,
67 * the API functions will return an error. In general, the user
68 * should synchronize using the callback events) */
69 bool terminate;
70
71 /* Event that is sent when the current operation is completed (except
72 * for DLCX, there the specified parent_term_evt is sent instead) */
73 uint32_t parent_evt;
74};
75
76#define S(x) (1 << (x))
77
78#define MGCP_MGW_TIMEOUT 4 /* in seconds */
79#define MGCP_MGW_TIMEOUT_TIMER_NR 1
80
Philipp Maier01f03952018-02-26 14:33:25 +010081enum fsm_mgcp_client_states {
Philipp Maier8bda7a72018-01-17 14:32:23 +010082 ST_CRCX,
83 ST_CRCX_RESP,
84 ST_READY,
85 ST_MDCX_RESP,
86 ST_DLCX_RESP,
87};
88
Philipp Maier01f03952018-02-26 14:33:25 +010089enum fsm_mgcp_client_evt {
Philipp Maier8bda7a72018-01-17 14:32:23 +010090 EV_CRCX,
91 EV_CRCX_RESP,
92 EV_MDCX,
93 EV_MDCX_RESP,
94 EV_DLCX,
95 EV_DLCX_RESP,
96};
97
Philipp Maierd2e3a522018-02-26 14:29:01 +010098static const struct value_string fsm_mgcp_client_evt_names[] = {
99 OSMO_VALUE_STRING(EV_CRCX),
100 OSMO_VALUE_STRING(EV_CRCX_RESP),
101 OSMO_VALUE_STRING(EV_MDCX),
102 OSMO_VALUE_STRING(EV_MDCX_RESP),
103 OSMO_VALUE_STRING(EV_DLCX),
104 OSMO_VALUE_STRING(EV_DLCX_RESP),
105 {0, NULL}
106};
107
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200108static void make_crcx_msg(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100109{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200110 *mgcp_msg = (struct mgcp_msg) {
Philipp Maier8bda7a72018-01-17 14:32:23 +0100111 .verb = MGCP_VERB_CRCX,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200112 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
113 | MGCP_MSG_PRESENCE_CONN_MODE),
114 .call_id = info->call_id,
Philipp Maier54eb0e12018-05-29 09:49:52 +0200115 .conn_mode = MGCP_CONN_RECV_ONLY,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200116 .ptime = info->ptime,
117 .codecs_len = info->codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100118 .ptmap_len = info->ptmap_len,
119 .param_present = info->param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100120 };
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200121 osmo_strlcpy(mgcp_msg->endpoint, info->endpoint, MGCP_ENDPOINT_MAXLEN);
122 memcpy(mgcp_msg->codecs, info->codecs, sizeof(mgcp_msg->codecs));
123 memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100124 memcpy(&mgcp_msg->param, &info->param, sizeof(mgcp_msg->param));
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200125
126 if (info->x_osmo_ign) {
127 mgcp_msg->x_osmo_ign = info->x_osmo_ign;
128 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN;
129 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +0200130
131 if (info->x_osmo_osmux_use) {
132 mgcp_msg->x_osmo_osmux_cid = info->x_osmo_osmux_cid;
133 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
134 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100135}
136
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200137static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100138{
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100139 bool ip_is_set = info->addr[0] != '\0' &&
140 strncmp(info->addr, "::", sizeof(info->addr)) != 0 &&
141 strncmp(info->addr, "0.0.0.0", sizeof(info->addr)) != 0;
142 if (ip_is_set) {
143 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP;
144 mgcp_msg->audio_ip = info->addr;
145 }
146 if (info->port) {
147 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_PORT;
148 mgcp_msg->audio_port = info->port;
149 }
150 if (ip_is_set && info->port)
151 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100152}
153
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100154static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
155{
156 enum mgcp_connection_mode conn_mode = peer->conn_mode;
157 if (conn_mode != MGCP_CONN_NONE)
158 mgcp_msg->conn_mode = conn_mode;
159}
160
Philipp Maier8bda7a72018-01-17 14:32:23 +0100161static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
162{
163 struct mgcp_msg mgcp_msg;
164
165 mgcp_msg = (struct mgcp_msg) {
166 .verb = MGCP_VERB_MDCX,
167 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
168 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
169 .call_id = mgcp_ctx->conn_peer_remote.call_id,
170 .conn_id = mgcp_ctx->conn_id,
171 .conn_mode = MGCP_CONN_RECV_SEND,
172 .audio_ip = mgcp_ctx->conn_peer_local.addr,
173 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200174 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200175 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100176 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
177 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100178 };
179 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200180 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200181 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100182 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100183
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100184 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
185
Pau Espin Pedrolca538fc2019-05-10 16:49:59 +0200186 if (mgcp_ctx->conn_peer_local.x_osmo_osmux_use) {
187 mgcp_msg.x_osmo_osmux_cid = mgcp_ctx->conn_peer_local.x_osmo_osmux_cid;
188 mgcp_msg.presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
189 }
190
Philipp Maier8bda7a72018-01-17 14:32:23 +0100191 /* Note: We take the endpoint and the call_id from the remote
192 * connection info, because we can be confident that the
193 * information there is valid. For the local info, we explicitly
194 * allow endpoint and call_id to be optional */
195 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
196}
197
198struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
199{
200 struct mgcp_msg mgcp_msg;
201
202 mgcp_msg = (struct mgcp_msg) {
203 .verb = MGCP_VERB_DLCX,
204 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
205 .call_id = mgcp_ctx->conn_peer_remote.call_id,
206 .conn_id = mgcp_ctx->conn_id,
207 };
208 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
209
210 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
211}
212
213static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
214
215static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
216{
217 struct mgcp_ctx *mgcp_ctx = data;
218 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200219 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100220 struct msgb *msg;
221 int rc;
222
223 OSMO_ASSERT(mgcp_ctx);
224 mgcp = mgcp_ctx->mgcp;
225 OSMO_ASSERT(mgcp);
226
227 switch (event) {
228 case EV_CRCX:
229 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
230 mgcp_ctx->conn_peer_local.endpoint);
231
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200232 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100233 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100234 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
235
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200236 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100237 OSMO_ASSERT(msg);
238
239 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
240 mgcp_ctx->mgw_trans_pending = true;
241 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
242 if (rc < 0) {
243 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
244 return;
245 }
246
247 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
248 break;
249 default:
250 OSMO_ASSERT(false);
251 break;
252 }
253}
254
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200255/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
256 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
257const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
258{
259 struct mgcp_ctx *mgcp_ctx = fi->priv;
260 return mgcp_ctx->conn_id;
261}
262
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200263/* Get the mgcp_client that is used with this mgcp_client_fsm instance */
264struct mgcp_client *mgcp_conn_get_client(struct osmo_fsm_inst *fi)
265{
266 struct mgcp_ctx *mgcp_ctx;
267
268 if (!fi)
269 return NULL;
270
271 mgcp_ctx = fi->priv;
272 return mgcp_ctx->mgcp;
273}
274
Philipp Maier8bda7a72018-01-17 14:32:23 +0100275static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
276{
277 struct osmo_fsm_inst *fi = priv;
278 struct mgcp_ctx *mgcp_ctx;
279 int rc;
280
281 OSMO_ASSERT(fi);
282 mgcp_ctx = fi->priv;
283 OSMO_ASSERT(mgcp_ctx);
284
285 mgcp_ctx->mgw_trans_pending = false;
286
287 if (r->head.response_code != 200) {
288 LOGPFSML(fi, LOGL_ERROR,
289 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
290 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
291 return;
292 }
293
294 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
295 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
296
297 rc = mgcp_response_parse_params(r);
298 if (rc) {
299 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
300 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
301 return;
302 }
303 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200304 if (r->head.x_osmo_osmux_use) {
305 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
306 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
307 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
308 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100309
310 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
311 mgcp_ctx->conn_peer_remote.port = r->audio_port;
312
313 if (strlen(r->head.endpoint) > 0) {
314 /* If we get an endpoint identifier back from the MGW, take it */
315 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
316 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
317 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
318 /* If we do not get an endpoint identifier back and the
319 * identifier we used to create the connection is not a
320 * wildcarded one, we take the local endpoint identifier
321 * instead */
322 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
323 sizeof(mgcp_ctx->conn_peer_local.endpoint));
324 } else {
325 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
326 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
327 return;
328 }
329
330 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
331
332 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
333}
334
335static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
336{
337 struct mgcp_ctx *mgcp_ctx = data;
338 OSMO_ASSERT(mgcp_ctx);
339
340 switch (event) {
341 case EV_CRCX_RESP:
342 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
343 if (mgcp_ctx->terminate) {
344 /* Trigger immediate DLCX if DLCX was requested while the FSM was
345 * busy with the previous operation */
346 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
347 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
348 } else
349 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
350 break;
351 default:
352 OSMO_ASSERT(false);
353 break;
354 }
355}
356
357static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
358static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
359
360static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
361{
362 struct mgcp_ctx *mgcp_ctx = data;
363 struct msgb *msg;
364 struct mgcp_client *mgcp;
365 uint32_t new_state;
366 int rc;
367
368 OSMO_ASSERT(mgcp_ctx);
369 mgcp = mgcp_ctx->mgcp;
370 OSMO_ASSERT(mgcp);
371
372 switch (event) {
373 case EV_MDCX:
374 msg = make_mdcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100375 if (!msg) {
376 /* make_mdcx_msg() should already have logged the error */
377 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
378 return;
379 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100380 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
381 new_state = ST_MDCX_RESP;
382 break;
383 case EV_DLCX:
384 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100385 if (!msg) {
386 /* make_dlcx_msg() should already have logged the error */
387 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
388 return;
389 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100390 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
391 new_state = ST_DLCX_RESP;
392 break;
393 default:
394 OSMO_ASSERT(false);
395 break;
396 }
397
398 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
399 mgcp_ctx->mgw_trans_pending = true;
400
401 if (rc < 0) {
402 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
403 return;
404 }
405
406 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
407}
408
409static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
410{
411 struct osmo_fsm_inst *fi = priv;
412 struct mgcp_ctx *mgcp_ctx;
413 int rc;
414
415 OSMO_ASSERT(fi);
416 mgcp_ctx = fi->priv;
417 OSMO_ASSERT(mgcp_ctx);
418
419 mgcp_ctx->mgw_trans_pending = false;
420
421 if (r->head.response_code != 200) {
422 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
423 r->head.comment);
424 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
425 return;
426 }
427
428 rc = mgcp_response_parse_params(r);
429 if (rc) {
430 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
431 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
432 return;
433 }
434 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
435
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200436 if (r->head.x_osmo_osmux_use) {
437 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
438 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
439 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
440 }
441
Philipp Maier8bda7a72018-01-17 14:32:23 +0100442 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
443 mgcp_ctx->conn_peer_remote.port = r->audio_port;
444
445 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
446}
447
448static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
449{
450 struct mgcp_ctx *mgcp_ctx = data;
451 OSMO_ASSERT(mgcp_ctx);
452
453 switch (event) {
454 case EV_MDCX_RESP:
455 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
456 if (mgcp_ctx->terminate) {
457 /* Trigger immediate DLCX if DLCX was requested while the FSM was
458 * busy with the previous operation */
459 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
460 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
461 } else
462 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
463 break;
464 default:
465 OSMO_ASSERT(false);
466 break;
467 }
468}
469
470static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
471{
472 struct osmo_fsm_inst *fi = priv;
473 struct mgcp_ctx *mgcp_ctx;
474
475 OSMO_ASSERT(fi);
476 mgcp_ctx = fi->priv;
477 OSMO_ASSERT(mgcp_ctx);
478
479 mgcp_ctx->mgw_trans_pending = false;
480
481 if (r->head.response_code != 250) {
482 LOGPFSML(fi, LOGL_ERROR,
483 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
484 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
485 return;
486 }
487
488 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
489}
490
491static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
492{
493 struct mgcp_ctx *mgcp_ctx = data;
494 OSMO_ASSERT(mgcp_ctx);
495
496 switch (event) {
497 case EV_DLCX_RESP:
498 /* Rub out the connection identifier, since the connection
499 * is no longer present and we will use the connection id
500 * to know in error cases if the connection is still present
501 * or not */
502 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
503
504 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
505 break;
506 default:
507 OSMO_ASSERT(false);
508 break;
509 }
510}
511
512static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
513{
514 struct mgcp_ctx *mgcp_ctx = fi->priv;
515 struct mgcp_client *mgcp;
516
517 OSMO_ASSERT(mgcp_ctx);
518 mgcp = mgcp_ctx->mgcp;
519 OSMO_ASSERT(mgcp);
520
521 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
522 /* Note: We were unable to communicate with the MGW,
523 * unfortunately there is no meaningful action we can take
524 * now other than giving up. */
525 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
526 } else {
527 /* Note: Ther must not be any unsolicited timers
528 * in this FSM. If so, we have serious problem. */
529 OSMO_ASSERT(false);
530 }
531
532 return 0;
533}
534
535static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
536{
537 struct mgcp_ctx *mgcp_ctx = fi->priv;
538 struct mgcp_client *mgcp;
539 struct msgb *msg;
540
541 OSMO_ASSERT(mgcp_ctx);
542 mgcp = mgcp_ctx->mgcp;
543 OSMO_ASSERT(mgcp);
544
545 /* If there is still a transaction pending, cancel it now. */
546 if (mgcp_ctx->mgw_trans_pending)
547 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
548
549 /* Should the FSM be terminated while there are still open connections
550 * on the MGW, we send an unconditional DLCX to terminate the
551 * connection. This is not the normal case. The user should always use
Neels Hofmeyr73716632021-05-01 02:32:29 +0000552 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit.
553 * If in ST_DLCX_RESP, a DLCX was already sent and we did not get a
554 * response. No point in sending another one. */
555 if (fi->state != ST_DLCX_RESP && strlen(mgcp_ctx->conn_id)) {
Neels Hofmeyr427cede2021-05-01 02:32:50 +0000556 LOGPFSML(fi, LOGL_INFO, "Conn cleanup, sending DLCX for %s %s\n", mgcp_ctx->conn_peer_remote.endpoint,
557 mgcp_ctx->conn_id);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100558 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr8c69e292019-09-19 03:05:00 +0200559 if (!msg)
560 LOGPFSML(fi, LOGL_ERROR, "MGW/DLCX: Error composing DLCX message\n");
561 else
562 mgcp_client_tx(mgcp, msg, NULL, NULL);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100563 }
564
565 talloc_free(mgcp_ctx);
566}
567
568static struct osmo_fsm_state fsm_mgcp_client_states[] = {
569
570 /* Initial CRCX state. This state is immediately entered and executed
571 * when the FSM is started. The rationale is that we first have to
572 * create a connectin before we can execute other operations on that
573 * connection. */
574 [ST_CRCX] = {
575 .in_event_mask = S(EV_CRCX),
576 .out_state_mask = S(ST_CRCX_RESP),
577 .name = OSMO_STRINGIFY(ST_CRCX),
578 .action = fsm_crcx_cb,
579 },
580
581 /* Wait for the response to a CRCX operation, check and process the
582 * results, change to ST_READY afterwards. */
583 [ST_CRCX_RESP] = {
584 .in_event_mask = S(EV_CRCX_RESP),
585 .out_state_mask = S(ST_READY),
586 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
587 .action = fsm_crcx_resp_cb,
588 },
589
590 /* In this idle state we wait for further operations (e.g. MDCX) that
591 * can be executed by the user using the API. There is no timeout in
592 * this state. The connection lives on until the user decides to
593 * terminate it (DLCX). */
594 [ST_READY] = {
595 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
596 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
597 .name = OSMO_STRINGIFY(ST_READY),
598 .action = fsm_ready_cb,
599 },
600
601 /* Wait for the response of a MDCX operation, check and process the
602 * results, change to ST_READY afterwards. */
603 [ST_MDCX_RESP] = {
604 .in_event_mask = S(EV_MDCX_RESP),
605 .out_state_mask = S(ST_READY),
606 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
607 .action = fsm_mdcx_resp_cb,
608 },
609
610 /* Wait for the response of a DLCX operation and terminate the FSM
611 * normally. */
612 [ST_DLCX_RESP] = {
613 .in_event_mask = S(EV_DLCX_RESP),
614 .out_state_mask = 0,
615 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
616 .action = fsm_dlcx_resp_cb,
617 },
618};
619
620static struct osmo_fsm fsm_mgcp_client = {
621 .name = "MGCP_CONN",
622 .states = fsm_mgcp_client_states,
623 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
624 .timer_cb = fsm_timeout_cb,
625 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100626 .event_names = fsm_mgcp_client_evt_names,
Neels Hofmeyra8c684b2021-07-15 01:37:00 +0200627 .log_subsys = DLMGCP,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100628};
629
630/*! allocate FSM, and create a new connection on the MGW.
631 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200632 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100633 * \param[in] parent_term_evt Event to be sent to parent when terminating.
634 * \param[in] parent_evt Event to be sent to parent when operation is done.
635 * \param[in] conn_peer Connection parameters (ip, port...).
636 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
637struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
638 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
639{
640 struct mgcp_ctx *mgcp_ctx;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100641 struct osmo_fsm_inst *fi;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200642 struct in6_addr ip_test;
643
Philipp Maier8bda7a72018-01-17 14:32:23 +0100644
645 OSMO_ASSERT(parent_fi);
646 OSMO_ASSERT(mgcp);
647 OSMO_ASSERT(conn_peer);
648
Philipp Maier704c4f02018-06-07 18:51:31 +0200649 /* Check if IP/Port information in conn info makes sense */
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200650 if (conn_peer->port && inet_pton(osmo_ip_str_type(conn_peer->addr),
651 conn_peer->addr, &ip_test) != 1)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100652 return NULL;
653
Philipp Maier8bda7a72018-01-17 14:32:23 +0100654 /* Allocate and configure a new fsm instance */
655 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
656 OSMO_ASSERT(fi);
657 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
658 OSMO_ASSERT(mgcp_ctx);
659 mgcp_ctx->mgcp = mgcp;
660 mgcp_ctx->parent_evt = parent_evt;
661
662 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
663 fi->priv = mgcp_ctx;
664
665 /* start state machine */
666 OSMO_ASSERT(fi->state == ST_CRCX);
667 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
668
669 return fi;
670}
671
672/*! modify an existing connection on the MGW.
673 * \param[in] fi FSM instance.
674 * \param[in] parent_evt Event to be sent to parent when operation is done.
675 * \param[in] conn_peer New connection information (ip, port...).
676 * \returns 0 on success, -EINVAL on error. */
677int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
678{
679 OSMO_ASSERT(fi);
680 struct mgcp_ctx *mgcp_ctx = fi->priv;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200681 struct in6_addr ip_test;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100682
683 OSMO_ASSERT(mgcp_ctx);
684 OSMO_ASSERT(conn_peer);
685
686 /* The user must not issue an MDCX before the CRCX has completed,
687 * if this happens, it means that the parent FSM has overhead the
688 * parent_evt (mandatory!) and executed the MDCX without even
689 * waiting for the results. Another reason could be that the
690 * parent FSM got messed up */
691 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
692
693 /* If the user tries to issue an MDCX while an DLCX operation is
694 * pending, there must be a serious problem with the paren FSM.
695 * Eeither the parent_term_evt (mandatory!) has been overheard,
696 * or the parant FSM got messed so badly that it still assumes
697 * a live connection although it as killed it. */
698 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
699
700 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200701 if (conn_peer->port == 0) {
702 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100703 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200704 }
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200705 if (inet_pton(osmo_ip_str_type(conn_peer->addr), conn_peer->addr, &ip_test) != 1) {
706 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address %s\n", conn_peer->addr);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100707 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200708 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100709
710 /*! The user may supply an endpoint identifier in conn_peer. The
711 * identifier is then checked. This check is optional. Later steps do
712 * not depend on the endpoint identifier supplied here because it is
713 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200714 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
715 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
716 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100717 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200718 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100719
720 /*! Note: The call-id is implicitly known from the previous CRCX and
721 * will not be checked even when it is set in conn_peer. */
722
723 mgcp_ctx->parent_evt = parent_evt;
724 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
725 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
726 return 0;
727}
728
729/*! delete existing connection on the MGW, destroy FSM afterwards.
730 * \param[in] fi FSM instance. */
731void mgcp_conn_delete(struct osmo_fsm_inst *fi)
732{
733 OSMO_ASSERT(fi);
734 struct mgcp_ctx *mgcp_ctx = fi->priv;
735
736 OSMO_ASSERT(mgcp_ctx);
737
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200738 if (fi->proc.terminating)
739 return;
740
Neels Hofmeyr9de30e72021-07-15 02:21:14 +0200741 /* Unlink FSM from parent, set the struct mgcp_client as new talloc ctx. */
742 osmo_fsm_inst_unlink_parent(fi, mgcp_ctx->mgcp);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100743
744 /* An error situation where the parent FSM must be killed immediately
745 * may lead into a situation where the DLCX can not be executed right
746 * at that moment because the FSM is still busy with another operation.
747 * In those cases we postpone the DLCX so that the FSM and the
748 * connections on the MGW get cleaned up gracefully. */
749 if (fi->state != ST_READY) {
750 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
751 mgcp_ctx->terminate = true;
752 return;
753 }
754 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
755}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100756
757const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
758{
759 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
760 * this buffer is too small. */
761 static char buf[1024];
762
763 if (!info)
764 return "NULL";
765
766 if (info->endpoint[0]
767 && info->addr[0])
768 snprintf(buf, sizeof(buf), "%s:%s:%u",
769 info->endpoint, info->addr, info->port);
770 else if (info->endpoint[0])
771 snprintf(buf, sizeof(buf), "%s", info->endpoint);
772 else if (info->addr[0])
773 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
774 else
775 return "empty";
776 return buf;
777}
Harald Welte6a25a612019-12-01 15:37:47 +0100778
Harald Welte9befdeb2022-11-03 11:41:05 +0100779static __attribute__((constructor)) void osmo_mgcp_client_fsm_init(void)
Harald Welte6a25a612019-12-01 15:37:47 +0100780{
781 OSMO_ASSERT(osmo_fsm_register(&fsm_mgcp_client) == 0);
782}