blob: e638d1e6449012312ef26f68ef7f04c8e06bb25f [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>
Neels Hofmeyr18717f52023-12-07 02:32:00 +010022#include <osmocom/mgcp_client/mgcp_client_internal.h>
Philipp Maier8bda7a72018-01-17 14:32:23 +010023#include <osmocom/mgcp_client/mgcp_client_fsm.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/fsm.h>
26#include <osmocom/core/byteswap.h>
27#include <arpa/inet.h>
28#include <osmocom/core/logging.h>
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +020029#include <osmocom/core/sockaddr_str.h>
Philipp Maier8bda7a72018-01-17 14:32:23 +010030
31/* Context information, this is attached to the priv pointer of the FSM and
32 * is also handed back when dispatcheing events to the parent FSM. This is
33 * purly intened and not meant to be accessible for the API user */
34struct mgcp_ctx {
35 /* MGCP client instance that is used to interact with the MGW */
36 struct mgcp_client *mgcp;
37
38 /* The ID of the last pending transaction. This is used internally
39 * to cancel the transaction in case of an error */
40 mgcp_trans_id_t mgw_pending_trans;
41
42 /* Flag to mark that there is a pending transaction */
43 bool mgw_trans_pending;
44
45 /* Connection ID which has been assigned by he MGW */
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020046 char conn_id[MGCP_CONN_ID_MAXLEN];
Philipp Maier8bda7a72018-01-17 14:32:23 +010047
48 /* Local RTP connection info, the MGW will send outgoing traffic to the
49 * ip/port specified here. The Address does not have to be choosen right
50 * on the creation of a connection. It can always be modified later by
51 * the user. */
52 struct mgcp_conn_peer conn_peer_local;
53
54 /* Remote RTP connection info, the ip/port specified here is the address
55 * where the MGW expects the RTP data to be sent. This address is
56 * defined by soly by the MGW and can not be influenced by the user. */
57 struct mgcp_conn_peer conn_peer_remote;
58
59 /* The terminate flag is a way to handle cornercase sitations that
60 * might occur when the user runs into an error situation and sends
61 * a DLCX command while the FSM is waiting for a response. In this
62 * case the DLCX command is not executed immediately. Instead the
63 * terminate flag is set. When the response to from the previous
64 * operation is received, we know that there is a DLCX event is
65 * pending. The FSM then generates the EV_DLCX by itsself before
66 * it enters ST_READY to cause the immediate execution of the
67 * DLCX procedure. (If normal operations are executed too fast,
68 * the API functions will return an error. In general, the user
69 * should synchronize using the callback events) */
70 bool terminate;
71
72 /* Event that is sent when the current operation is completed (except
73 * for DLCX, there the specified parent_term_evt is sent instead) */
74 uint32_t parent_evt;
75};
76
77#define S(x) (1 << (x))
78
79#define MGCP_MGW_TIMEOUT 4 /* in seconds */
80#define MGCP_MGW_TIMEOUT_TIMER_NR 1
81
Philipp Maier01f03952018-02-26 14:33:25 +010082enum fsm_mgcp_client_states {
Philipp Maier8bda7a72018-01-17 14:32:23 +010083 ST_CRCX,
84 ST_CRCX_RESP,
85 ST_READY,
86 ST_MDCX_RESP,
87 ST_DLCX_RESP,
88};
89
Philipp Maier01f03952018-02-26 14:33:25 +010090enum fsm_mgcp_client_evt {
Philipp Maier8bda7a72018-01-17 14:32:23 +010091 EV_CRCX,
92 EV_CRCX_RESP,
93 EV_MDCX,
94 EV_MDCX_RESP,
95 EV_DLCX,
96 EV_DLCX_RESP,
97};
98
Philipp Maierd2e3a522018-02-26 14:29:01 +010099static const struct value_string fsm_mgcp_client_evt_names[] = {
100 OSMO_VALUE_STRING(EV_CRCX),
101 OSMO_VALUE_STRING(EV_CRCX_RESP),
102 OSMO_VALUE_STRING(EV_MDCX),
103 OSMO_VALUE_STRING(EV_MDCX_RESP),
104 OSMO_VALUE_STRING(EV_DLCX),
105 OSMO_VALUE_STRING(EV_DLCX_RESP),
106 {0, NULL}
107};
108
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200109static void make_crcx_msg(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100110{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200111 *mgcp_msg = (struct mgcp_msg) {
Philipp Maier8bda7a72018-01-17 14:32:23 +0100112 .verb = MGCP_VERB_CRCX,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200113 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
114 | MGCP_MSG_PRESENCE_CONN_MODE),
115 .call_id = info->call_id,
Philipp Maier54eb0e12018-05-29 09:49:52 +0200116 .conn_mode = MGCP_CONN_RECV_ONLY,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200117 .ptime = info->ptime,
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);
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200122 memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100123 memcpy(&mgcp_msg->param, &info->param, sizeof(mgcp_msg->param));
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200124
125 if (info->x_osmo_ign) {
126 mgcp_msg->x_osmo_ign = info->x_osmo_ign;
127 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN;
128 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +0200129
130 if (info->x_osmo_osmux_use) {
131 mgcp_msg->x_osmo_osmux_cid = info->x_osmo_osmux_cid;
132 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
133 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100134}
135
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200136static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100137{
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100138 bool ip_is_set = info->addr[0] != '\0' &&
139 strncmp(info->addr, "::", sizeof(info->addr)) != 0 &&
140 strncmp(info->addr, "0.0.0.0", sizeof(info->addr)) != 0;
141 if (ip_is_set) {
142 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP;
143 mgcp_msg->audio_ip = info->addr;
144 }
145 if (info->port) {
146 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_PORT;
147 mgcp_msg->audio_port = info->port;
148 }
149 if (ip_is_set && info->port)
150 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100151}
152
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100153static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
154{
155 enum mgcp_connection_mode conn_mode = peer->conn_mode;
156 if (conn_mode != MGCP_CONN_NONE)
157 mgcp_msg->conn_mode = conn_mode;
158}
159
Philipp Maier8bda7a72018-01-17 14:32:23 +0100160static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
161{
162 struct mgcp_msg mgcp_msg;
163
164 mgcp_msg = (struct mgcp_msg) {
165 .verb = MGCP_VERB_MDCX,
166 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
167 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
168 .call_id = mgcp_ctx->conn_peer_remote.call_id,
169 .conn_id = mgcp_ctx->conn_id,
170 .conn_mode = MGCP_CONN_RECV_SEND,
171 .audio_ip = mgcp_ctx->conn_peer_local.addr,
172 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200173 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier228e5912019-03-05 13:56:59 +0100174 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
175 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100176 };
177 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier544448a2018-07-26 11:37:44 +0200178 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100179 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100180
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100181 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
182
Pau Espin Pedrolca538fc2019-05-10 16:49:59 +0200183 if (mgcp_ctx->conn_peer_local.x_osmo_osmux_use) {
184 mgcp_msg.x_osmo_osmux_cid = mgcp_ctx->conn_peer_local.x_osmo_osmux_cid;
185 mgcp_msg.presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
186 }
187
Philipp Maier8bda7a72018-01-17 14:32:23 +0100188 /* Note: We take the endpoint and the call_id from the remote
189 * connection info, because we can be confident that the
190 * information there is valid. For the local info, we explicitly
191 * allow endpoint and call_id to be optional */
192 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
193}
194
195struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
196{
197 struct mgcp_msg mgcp_msg;
198
199 mgcp_msg = (struct mgcp_msg) {
200 .verb = MGCP_VERB_DLCX,
201 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
202 .call_id = mgcp_ctx->conn_peer_remote.call_id,
203 .conn_id = mgcp_ctx->conn_id,
204 };
205 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
206
207 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
208}
209
210static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
211
212static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
213{
214 struct mgcp_ctx *mgcp_ctx = data;
215 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200216 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100217 struct msgb *msg;
218 int rc;
219
220 OSMO_ASSERT(mgcp_ctx);
221 mgcp = mgcp_ctx->mgcp;
222 OSMO_ASSERT(mgcp);
223
224 switch (event) {
225 case EV_CRCX:
226 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
227 mgcp_ctx->conn_peer_local.endpoint);
228
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200229 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100230 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100231 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
232
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200233 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100234 OSMO_ASSERT(msg);
235
236 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
237 mgcp_ctx->mgw_trans_pending = true;
238 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
239 if (rc < 0) {
240 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
241 return;
242 }
243
244 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
245 break;
246 default:
247 OSMO_ASSERT(false);
248 break;
249 }
250}
251
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200252/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
253 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
254const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
255{
256 struct mgcp_ctx *mgcp_ctx = fi->priv;
257 return mgcp_ctx->conn_id;
258}
259
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200260/* Get the mgcp_client that is used with this mgcp_client_fsm instance */
261struct mgcp_client *mgcp_conn_get_client(struct osmo_fsm_inst *fi)
262{
263 struct mgcp_ctx *mgcp_ctx;
264
265 if (!fi)
266 return NULL;
267
268 mgcp_ctx = fi->priv;
269 return mgcp_ctx->mgcp;
270}
271
Philipp Maier8bda7a72018-01-17 14:32:23 +0100272static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
273{
274 struct osmo_fsm_inst *fi = priv;
275 struct mgcp_ctx *mgcp_ctx;
276 int rc;
277
278 OSMO_ASSERT(fi);
279 mgcp_ctx = fi->priv;
280 OSMO_ASSERT(mgcp_ctx);
281
282 mgcp_ctx->mgw_trans_pending = false;
283
284 if (r->head.response_code != 200) {
285 LOGPFSML(fi, LOGL_ERROR,
286 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
287 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
288 return;
289 }
290
291 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
292 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
293
294 rc = mgcp_response_parse_params(r);
295 if (rc) {
296 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
297 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
298 return;
299 }
300 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 +0200301 if (r->head.x_osmo_osmux_use) {
302 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
303 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
304 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
305 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100306
307 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
308 mgcp_ctx->conn_peer_remote.port = r->audio_port;
309
310 if (strlen(r->head.endpoint) > 0) {
311 /* If we get an endpoint identifier back from the MGW, take it */
312 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
313 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
314 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
315 /* If we do not get an endpoint identifier back and the
316 * identifier we used to create the connection is not a
317 * wildcarded one, we take the local endpoint identifier
318 * instead */
319 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
320 sizeof(mgcp_ctx->conn_peer_local.endpoint));
321 } else {
322 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
323 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
324 return;
325 }
326
327 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
328
329 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
330}
331
332static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
333{
334 struct mgcp_ctx *mgcp_ctx = data;
335 OSMO_ASSERT(mgcp_ctx);
336
337 switch (event) {
338 case EV_CRCX_RESP:
339 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
340 if (mgcp_ctx->terminate) {
341 /* Trigger immediate DLCX if DLCX was requested while the FSM was
342 * busy with the previous operation */
343 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
344 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
345 } else
346 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
347 break;
348 default:
349 OSMO_ASSERT(false);
350 break;
351 }
352}
353
354static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
355static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
356
357static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
358{
359 struct mgcp_ctx *mgcp_ctx = data;
360 struct msgb *msg;
361 struct mgcp_client *mgcp;
362 uint32_t new_state;
363 int rc;
364
365 OSMO_ASSERT(mgcp_ctx);
366 mgcp = mgcp_ctx->mgcp;
367 OSMO_ASSERT(mgcp);
368
369 switch (event) {
370 case EV_MDCX:
371 msg = make_mdcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100372 if (!msg) {
373 /* make_mdcx_msg() should already have logged the error */
374 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
375 return;
376 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100377 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
378 new_state = ST_MDCX_RESP;
379 break;
380 case EV_DLCX:
381 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100382 if (!msg) {
383 /* make_dlcx_msg() should already have logged the error */
384 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
385 return;
386 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100387 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
388 new_state = ST_DLCX_RESP;
389 break;
390 default:
391 OSMO_ASSERT(false);
392 break;
393 }
394
395 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
396 mgcp_ctx->mgw_trans_pending = true;
397
398 if (rc < 0) {
399 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
400 return;
401 }
402
403 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
404}
405
406static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
407{
408 struct osmo_fsm_inst *fi = priv;
409 struct mgcp_ctx *mgcp_ctx;
410 int rc;
411
412 OSMO_ASSERT(fi);
413 mgcp_ctx = fi->priv;
414 OSMO_ASSERT(mgcp_ctx);
415
416 mgcp_ctx->mgw_trans_pending = false;
417
418 if (r->head.response_code != 200) {
419 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
420 r->head.comment);
421 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
422 return;
423 }
424
425 rc = mgcp_response_parse_params(r);
426 if (rc) {
427 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
428 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
429 return;
430 }
431 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
432
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200433 if (r->head.x_osmo_osmux_use) {
434 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
435 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
436 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
437 }
438
Philipp Maier8bda7a72018-01-17 14:32:23 +0100439 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
440 mgcp_ctx->conn_peer_remote.port = r->audio_port;
441
442 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
443}
444
445static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
446{
447 struct mgcp_ctx *mgcp_ctx = data;
448 OSMO_ASSERT(mgcp_ctx);
449
450 switch (event) {
451 case EV_MDCX_RESP:
452 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
453 if (mgcp_ctx->terminate) {
454 /* Trigger immediate DLCX if DLCX was requested while the FSM was
455 * busy with the previous operation */
456 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
457 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
458 } else
459 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
460 break;
461 default:
462 OSMO_ASSERT(false);
463 break;
464 }
465}
466
467static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
468{
469 struct osmo_fsm_inst *fi = priv;
470 struct mgcp_ctx *mgcp_ctx;
471
472 OSMO_ASSERT(fi);
473 mgcp_ctx = fi->priv;
474 OSMO_ASSERT(mgcp_ctx);
475
476 mgcp_ctx->mgw_trans_pending = false;
477
478 if (r->head.response_code != 250) {
479 LOGPFSML(fi, LOGL_ERROR,
480 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
481 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
482 return;
483 }
484
485 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
486}
487
488static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
489{
490 struct mgcp_ctx *mgcp_ctx = data;
491 OSMO_ASSERT(mgcp_ctx);
492
493 switch (event) {
494 case EV_DLCX_RESP:
495 /* Rub out the connection identifier, since the connection
496 * is no longer present and we will use the connection id
497 * to know in error cases if the connection is still present
498 * or not */
499 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
500
501 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
502 break;
503 default:
504 OSMO_ASSERT(false);
505 break;
506 }
507}
508
509static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
510{
511 struct mgcp_ctx *mgcp_ctx = fi->priv;
512 struct mgcp_client *mgcp;
513
514 OSMO_ASSERT(mgcp_ctx);
515 mgcp = mgcp_ctx->mgcp;
516 OSMO_ASSERT(mgcp);
517
518 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
519 /* Note: We were unable to communicate with the MGW,
520 * unfortunately there is no meaningful action we can take
521 * now other than giving up. */
522 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
523 } else {
524 /* Note: Ther must not be any unsolicited timers
525 * in this FSM. If so, we have serious problem. */
526 OSMO_ASSERT(false);
527 }
528
529 return 0;
530}
531
532static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
533{
534 struct mgcp_ctx *mgcp_ctx = fi->priv;
535 struct mgcp_client *mgcp;
536 struct msgb *msg;
537
538 OSMO_ASSERT(mgcp_ctx);
539 mgcp = mgcp_ctx->mgcp;
540 OSMO_ASSERT(mgcp);
541
542 /* If there is still a transaction pending, cancel it now. */
543 if (mgcp_ctx->mgw_trans_pending)
544 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
545
546 /* Should the FSM be terminated while there are still open connections
547 * on the MGW, we send an unconditional DLCX to terminate the
548 * connection. This is not the normal case. The user should always use
Neels Hofmeyr73716632021-05-01 02:32:29 +0000549 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit.
550 * If in ST_DLCX_RESP, a DLCX was already sent and we did not get a
551 * response. No point in sending another one. */
552 if (fi->state != ST_DLCX_RESP && strlen(mgcp_ctx->conn_id)) {
Neels Hofmeyr427cede2021-05-01 02:32:50 +0000553 LOGPFSML(fi, LOGL_INFO, "Conn cleanup, sending DLCX for %s %s\n", mgcp_ctx->conn_peer_remote.endpoint,
554 mgcp_ctx->conn_id);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100555 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr8c69e292019-09-19 03:05:00 +0200556 if (!msg)
557 LOGPFSML(fi, LOGL_ERROR, "MGW/DLCX: Error composing DLCX message\n");
558 else
559 mgcp_client_tx(mgcp, msg, NULL, NULL);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100560 }
561
562 talloc_free(mgcp_ctx);
563}
564
565static struct osmo_fsm_state fsm_mgcp_client_states[] = {
566
567 /* Initial CRCX state. This state is immediately entered and executed
568 * when the FSM is started. The rationale is that we first have to
569 * create a connectin before we can execute other operations on that
570 * connection. */
571 [ST_CRCX] = {
572 .in_event_mask = S(EV_CRCX),
573 .out_state_mask = S(ST_CRCX_RESP),
574 .name = OSMO_STRINGIFY(ST_CRCX),
575 .action = fsm_crcx_cb,
576 },
577
578 /* Wait for the response to a CRCX operation, check and process the
579 * results, change to ST_READY afterwards. */
580 [ST_CRCX_RESP] = {
581 .in_event_mask = S(EV_CRCX_RESP),
582 .out_state_mask = S(ST_READY),
583 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
584 .action = fsm_crcx_resp_cb,
585 },
586
587 /* In this idle state we wait for further operations (e.g. MDCX) that
588 * can be executed by the user using the API. There is no timeout in
589 * this state. The connection lives on until the user decides to
590 * terminate it (DLCX). */
591 [ST_READY] = {
592 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
593 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
594 .name = OSMO_STRINGIFY(ST_READY),
595 .action = fsm_ready_cb,
596 },
597
598 /* Wait for the response of a MDCX operation, check and process the
599 * results, change to ST_READY afterwards. */
600 [ST_MDCX_RESP] = {
601 .in_event_mask = S(EV_MDCX_RESP),
602 .out_state_mask = S(ST_READY),
603 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
604 .action = fsm_mdcx_resp_cb,
605 },
606
607 /* Wait for the response of a DLCX operation and terminate the FSM
608 * normally. */
609 [ST_DLCX_RESP] = {
610 .in_event_mask = S(EV_DLCX_RESP),
611 .out_state_mask = 0,
612 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
613 .action = fsm_dlcx_resp_cb,
614 },
615};
616
617static struct osmo_fsm fsm_mgcp_client = {
618 .name = "MGCP_CONN",
619 .states = fsm_mgcp_client_states,
620 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
621 .timer_cb = fsm_timeout_cb,
622 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100623 .event_names = fsm_mgcp_client_evt_names,
Neels Hofmeyra8c684b2021-07-15 01:37:00 +0200624 .log_subsys = DLMGCP,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100625};
626
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100627/* Provide backwards compat for deprecated conn_peer->codecs[]: when the caller passes in an mgcp_conn_peer instance
628 * that has codecs[] set, apply it to ptmap[] instead. */
629static void mgcp_conn_peer_compat(struct mgcp_conn_peer *conn_peer)
630{
631 struct ptmap ptmap[MGCP_MAX_CODECS];
632 unsigned int ptmap_len;
633
634 if (!conn_peer->codecs_len)
635 return;
636
637 /* Before dropping codecs[], codecs[] would indicate the order in which the codecs should appear in SDP. ptmap[]
638 * would indicate payload type numbers when not using a default payload type number (may omit entries).
639 * Now, ptmap[] just indicates both at the same time; codecs[] should be empty, and ptmap[] lists all codecs.
640 * So if any codecs[] are present, recreate ptmap[] in the order of codecs[]. */
641
642 ptmap_len = 0;
643 for (int i = 0; i < conn_peer->codecs_len; i++) {
644 enum mgcp_codecs codec = conn_peer->codecs[i];
645 struct ptmap *found = NULL;
646
647 /* Look up whether a specific pt was indicated for this codec */
648 for (int p = 0; p < conn_peer->ptmap_len; p++) {
649 if (conn_peer->ptmap[p].codec != codec)
650 continue;
651 found = &conn_peer->ptmap[p];
652 break;
653 }
654 if (found) {
655 ptmap[ptmap_len] = *found;
656 } else {
657 ptmap[ptmap_len] = (struct ptmap){
658 .codec = codec,
659 /* some enum mgcp_codecs correspond to their standard PT nr, so for compat: */
660 .pt = codec,
661 };
662 }
663 ptmap_len++;
664 }
665
666 /* Are there any entries in the old ptmap that were omitted by codecs[]? */
667 for (int p = 0; p < conn_peer->ptmap_len; p++) {
668 bool exists = false;
669 for (int i = 0; i < ptmap_len; i++) {
670 if (ptmap_cmp(&ptmap[i], &conn_peer->ptmap[p]))
671 continue;
672 exists = true;
673 break;
674 }
675
676 if (exists)
677 continue;
678
679 if (ptmap_len >= ARRAY_SIZE(ptmap))
680 break;
681
682 /* Not present yet, add it to the end */
683 ptmap[ptmap_len] = conn_peer->ptmap[p];
684 ptmap_len++;
685 }
686
687 /* Use the new ptmap[], and clear out legacy codecs[]. */
688 memcpy(conn_peer->ptmap, ptmap, sizeof(conn_peer->ptmap));
689 conn_peer->ptmap_len = ptmap_len;
690 conn_peer->codecs_len = 0;
691}
692
Philipp Maier8bda7a72018-01-17 14:32:23 +0100693/*! allocate FSM, and create a new connection on the MGW.
694 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200695 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100696 * \param[in] parent_term_evt Event to be sent to parent when terminating.
697 * \param[in] parent_evt Event to be sent to parent when operation is done.
698 * \param[in] conn_peer Connection parameters (ip, port...).
699 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
700struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
701 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
702{
703 struct mgcp_ctx *mgcp_ctx;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100704 struct osmo_fsm_inst *fi;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200705 struct in6_addr ip_test;
706
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100707 mgcp_conn_peer_compat(conn_peer);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100708
709 OSMO_ASSERT(parent_fi);
710 OSMO_ASSERT(mgcp);
711 OSMO_ASSERT(conn_peer);
712
Philipp Maier704c4f02018-06-07 18:51:31 +0200713 /* Check if IP/Port information in conn info makes sense */
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200714 if (conn_peer->port && inet_pton(osmo_ip_str_type(conn_peer->addr),
715 conn_peer->addr, &ip_test) != 1)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100716 return NULL;
717
Philipp Maier8bda7a72018-01-17 14:32:23 +0100718 /* Allocate and configure a new fsm instance */
719 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
720 OSMO_ASSERT(fi);
721 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
722 OSMO_ASSERT(mgcp_ctx);
723 mgcp_ctx->mgcp = mgcp;
724 mgcp_ctx->parent_evt = parent_evt;
725
726 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
727 fi->priv = mgcp_ctx;
728
729 /* start state machine */
730 OSMO_ASSERT(fi->state == ST_CRCX);
731 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
732
733 return fi;
734}
735
736/*! modify an existing connection on the MGW.
737 * \param[in] fi FSM instance.
738 * \param[in] parent_evt Event to be sent to parent when operation is done.
739 * \param[in] conn_peer New connection information (ip, port...).
740 * \returns 0 on success, -EINVAL on error. */
741int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
742{
743 OSMO_ASSERT(fi);
744 struct mgcp_ctx *mgcp_ctx = fi->priv;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200745 struct in6_addr ip_test;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100746
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100747 mgcp_conn_peer_compat(conn_peer);
748
Philipp Maier8bda7a72018-01-17 14:32:23 +0100749 OSMO_ASSERT(mgcp_ctx);
750 OSMO_ASSERT(conn_peer);
751
752 /* The user must not issue an MDCX before the CRCX has completed,
753 * if this happens, it means that the parent FSM has overhead the
754 * parent_evt (mandatory!) and executed the MDCX without even
755 * waiting for the results. Another reason could be that the
756 * parent FSM got messed up */
757 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
758
759 /* If the user tries to issue an MDCX while an DLCX operation is
760 * pending, there must be a serious problem with the paren FSM.
761 * Eeither the parent_term_evt (mandatory!) has been overheard,
762 * or the parant FSM got messed so badly that it still assumes
763 * a live connection although it as killed it. */
764 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
765
766 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200767 if (conn_peer->port == 0) {
768 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100769 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200770 }
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200771 if (inet_pton(osmo_ip_str_type(conn_peer->addr), conn_peer->addr, &ip_test) != 1) {
772 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address %s\n", conn_peer->addr);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100773 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200774 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100775
776 /*! The user may supply an endpoint identifier in conn_peer. The
777 * identifier is then checked. This check is optional. Later steps do
778 * not depend on the endpoint identifier supplied here because it is
779 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200780 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
781 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
782 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100783 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200784 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100785
786 /*! Note: The call-id is implicitly known from the previous CRCX and
787 * will not be checked even when it is set in conn_peer. */
788
789 mgcp_ctx->parent_evt = parent_evt;
790 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
791 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
792 return 0;
793}
794
795/*! delete existing connection on the MGW, destroy FSM afterwards.
796 * \param[in] fi FSM instance. */
797void mgcp_conn_delete(struct osmo_fsm_inst *fi)
798{
799 OSMO_ASSERT(fi);
800 struct mgcp_ctx *mgcp_ctx = fi->priv;
801
802 OSMO_ASSERT(mgcp_ctx);
803
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200804 if (fi->proc.terminating)
805 return;
806
Neels Hofmeyr9de30e72021-07-15 02:21:14 +0200807 /* Unlink FSM from parent, set the struct mgcp_client as new talloc ctx. */
808 osmo_fsm_inst_unlink_parent(fi, mgcp_ctx->mgcp);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100809
810 /* An error situation where the parent FSM must be killed immediately
811 * may lead into a situation where the DLCX can not be executed right
812 * at that moment because the FSM is still busy with another operation.
813 * In those cases we postpone the DLCX so that the FSM and the
814 * connections on the MGW get cleaned up gracefully. */
815 if (fi->state != ST_READY) {
816 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
817 mgcp_ctx->terminate = true;
818 return;
819 }
820 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
821}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100822
823const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
824{
825 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
826 * this buffer is too small. */
827 static char buf[1024];
828
829 if (!info)
830 return "NULL";
831
832 if (info->endpoint[0]
833 && info->addr[0])
834 snprintf(buf, sizeof(buf), "%s:%s:%u",
835 info->endpoint, info->addr, info->port);
836 else if (info->endpoint[0])
837 snprintf(buf, sizeof(buf), "%s", info->endpoint);
838 else if (info->addr[0])
839 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
840 else
841 return "empty";
842 return buf;
843}
Harald Welte6a25a612019-12-01 15:37:47 +0100844
Harald Welte9befdeb2022-11-03 11:41:05 +0100845static __attribute__((constructor)) void osmo_mgcp_client_fsm_init(void)
Harald Welte6a25a612019-12-01 15:37:47 +0100846{
847 OSMO_ASSERT(osmo_fsm_register(&fsm_mgcp_client) == 0);
848}