blob: 43f0f50f766b8979ede56828a89bf725ac654c9f [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,
118 .codecs_len = info->codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100119 .ptmap_len = info->ptmap_len,
120 .param_present = info->param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100121 };
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200122 osmo_strlcpy(mgcp_msg->endpoint, info->endpoint, MGCP_ENDPOINT_MAXLEN);
123 memcpy(mgcp_msg->codecs, info->codecs, sizeof(mgcp_msg->codecs));
124 memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100125 memcpy(&mgcp_msg->param, &info->param, sizeof(mgcp_msg->param));
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200126
127 if (info->x_osmo_ign) {
128 mgcp_msg->x_osmo_ign = info->x_osmo_ign;
129 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN;
130 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +0200131
132 if (info->x_osmo_osmux_use) {
133 mgcp_msg->x_osmo_osmux_cid = info->x_osmo_osmux_cid;
134 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
135 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100136}
137
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200138static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100139{
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100140 bool ip_is_set = info->addr[0] != '\0' &&
141 strncmp(info->addr, "::", sizeof(info->addr)) != 0 &&
142 strncmp(info->addr, "0.0.0.0", sizeof(info->addr)) != 0;
143 if (ip_is_set) {
144 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP;
145 mgcp_msg->audio_ip = info->addr;
146 }
147 if (info->port) {
148 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_PORT;
149 mgcp_msg->audio_port = info->port;
150 }
151 if (ip_is_set && info->port)
152 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100153}
154
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100155static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
156{
157 enum mgcp_connection_mode conn_mode = peer->conn_mode;
158 if (conn_mode != MGCP_CONN_NONE)
159 mgcp_msg->conn_mode = conn_mode;
160}
161
Philipp Maier8bda7a72018-01-17 14:32:23 +0100162static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
163{
164 struct mgcp_msg mgcp_msg;
165
166 mgcp_msg = (struct mgcp_msg) {
167 .verb = MGCP_VERB_MDCX,
168 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
169 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
170 .call_id = mgcp_ctx->conn_peer_remote.call_id,
171 .conn_id = mgcp_ctx->conn_id,
172 .conn_mode = MGCP_CONN_RECV_SEND,
173 .audio_ip = mgcp_ctx->conn_peer_local.addr,
174 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200175 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200176 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100177 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
178 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100179 };
180 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200181 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200182 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100183 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100184
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100185 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
186
Pau Espin Pedrolca538fc2019-05-10 16:49:59 +0200187 if (mgcp_ctx->conn_peer_local.x_osmo_osmux_use) {
188 mgcp_msg.x_osmo_osmux_cid = mgcp_ctx->conn_peer_local.x_osmo_osmux_cid;
189 mgcp_msg.presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
190 }
191
Philipp Maier8bda7a72018-01-17 14:32:23 +0100192 /* Note: We take the endpoint and the call_id from the remote
193 * connection info, because we can be confident that the
194 * information there is valid. For the local info, we explicitly
195 * allow endpoint and call_id to be optional */
196 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
197}
198
199struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
200{
201 struct mgcp_msg mgcp_msg;
202
203 mgcp_msg = (struct mgcp_msg) {
204 .verb = MGCP_VERB_DLCX,
205 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
206 .call_id = mgcp_ctx->conn_peer_remote.call_id,
207 .conn_id = mgcp_ctx->conn_id,
208 };
209 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
210
211 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
212}
213
214static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
215
216static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
217{
218 struct mgcp_ctx *mgcp_ctx = data;
219 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200220 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100221 struct msgb *msg;
222 int rc;
223
224 OSMO_ASSERT(mgcp_ctx);
225 mgcp = mgcp_ctx->mgcp;
226 OSMO_ASSERT(mgcp);
227
228 switch (event) {
229 case EV_CRCX:
230 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
231 mgcp_ctx->conn_peer_local.endpoint);
232
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200233 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +0100234 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100235 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
236
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200237 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100238 OSMO_ASSERT(msg);
239
240 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
241 mgcp_ctx->mgw_trans_pending = true;
242 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
243 if (rc < 0) {
244 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
245 return;
246 }
247
248 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
249 break;
250 default:
251 OSMO_ASSERT(false);
252 break;
253 }
254}
255
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200256/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
257 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
258const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
259{
260 struct mgcp_ctx *mgcp_ctx = fi->priv;
261 return mgcp_ctx->conn_id;
262}
263
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200264/* Get the mgcp_client that is used with this mgcp_client_fsm instance */
265struct mgcp_client *mgcp_conn_get_client(struct osmo_fsm_inst *fi)
266{
267 struct mgcp_ctx *mgcp_ctx;
268
269 if (!fi)
270 return NULL;
271
272 mgcp_ctx = fi->priv;
273 return mgcp_ctx->mgcp;
274}
275
Philipp Maier8bda7a72018-01-17 14:32:23 +0100276static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
277{
278 struct osmo_fsm_inst *fi = priv;
279 struct mgcp_ctx *mgcp_ctx;
280 int rc;
281
282 OSMO_ASSERT(fi);
283 mgcp_ctx = fi->priv;
284 OSMO_ASSERT(mgcp_ctx);
285
286 mgcp_ctx->mgw_trans_pending = false;
287
288 if (r->head.response_code != 200) {
289 LOGPFSML(fi, LOGL_ERROR,
290 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
291 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
292 return;
293 }
294
295 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
296 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
297
298 rc = mgcp_response_parse_params(r);
299 if (rc) {
300 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
301 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
302 return;
303 }
304 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 +0200305 if (r->head.x_osmo_osmux_use) {
306 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
307 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
308 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
309 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100310
311 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
312 mgcp_ctx->conn_peer_remote.port = r->audio_port;
313
314 if (strlen(r->head.endpoint) > 0) {
315 /* If we get an endpoint identifier back from the MGW, take it */
316 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
317 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
318 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
319 /* If we do not get an endpoint identifier back and the
320 * identifier we used to create the connection is not a
321 * wildcarded one, we take the local endpoint identifier
322 * instead */
323 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
324 sizeof(mgcp_ctx->conn_peer_local.endpoint));
325 } else {
326 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
327 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
328 return;
329 }
330
331 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
332
333 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
334}
335
336static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
337{
338 struct mgcp_ctx *mgcp_ctx = data;
339 OSMO_ASSERT(mgcp_ctx);
340
341 switch (event) {
342 case EV_CRCX_RESP:
343 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
344 if (mgcp_ctx->terminate) {
345 /* Trigger immediate DLCX if DLCX was requested while the FSM was
346 * busy with the previous operation */
347 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
348 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
349 } else
350 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
351 break;
352 default:
353 OSMO_ASSERT(false);
354 break;
355 }
356}
357
358static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
359static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
360
361static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
362{
363 struct mgcp_ctx *mgcp_ctx = data;
364 struct msgb *msg;
365 struct mgcp_client *mgcp;
366 uint32_t new_state;
367 int rc;
368
369 OSMO_ASSERT(mgcp_ctx);
370 mgcp = mgcp_ctx->mgcp;
371 OSMO_ASSERT(mgcp);
372
373 switch (event) {
374 case EV_MDCX:
375 msg = make_mdcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100376 if (!msg) {
377 /* make_mdcx_msg() should already have logged the error */
378 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
379 return;
380 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100381 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
382 new_state = ST_MDCX_RESP;
383 break;
384 case EV_DLCX:
385 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr0127a062023-11-10 01:53:49 +0100386 if (!msg) {
387 /* make_dlcx_msg() should already have logged the error */
388 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
389 return;
390 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100391 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
392 new_state = ST_DLCX_RESP;
393 break;
394 default:
395 OSMO_ASSERT(false);
396 break;
397 }
398
399 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
400 mgcp_ctx->mgw_trans_pending = true;
401
402 if (rc < 0) {
403 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
404 return;
405 }
406
407 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
408}
409
410static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
411{
412 struct osmo_fsm_inst *fi = priv;
413 struct mgcp_ctx *mgcp_ctx;
414 int rc;
415
416 OSMO_ASSERT(fi);
417 mgcp_ctx = fi->priv;
418 OSMO_ASSERT(mgcp_ctx);
419
420 mgcp_ctx->mgw_trans_pending = false;
421
422 if (r->head.response_code != 200) {
423 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
424 r->head.comment);
425 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
426 return;
427 }
428
429 rc = mgcp_response_parse_params(r);
430 if (rc) {
431 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
432 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
433 return;
434 }
435 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
436
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200437 if (r->head.x_osmo_osmux_use) {
438 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
439 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
440 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
441 }
442
Philipp Maier8bda7a72018-01-17 14:32:23 +0100443 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
444 mgcp_ctx->conn_peer_remote.port = r->audio_port;
445
446 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
447}
448
449static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
450{
451 struct mgcp_ctx *mgcp_ctx = data;
452 OSMO_ASSERT(mgcp_ctx);
453
454 switch (event) {
455 case EV_MDCX_RESP:
456 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
457 if (mgcp_ctx->terminate) {
458 /* Trigger immediate DLCX if DLCX was requested while the FSM was
459 * busy with the previous operation */
460 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
461 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
462 } else
463 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
464 break;
465 default:
466 OSMO_ASSERT(false);
467 break;
468 }
469}
470
471static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
472{
473 struct osmo_fsm_inst *fi = priv;
474 struct mgcp_ctx *mgcp_ctx;
475
476 OSMO_ASSERT(fi);
477 mgcp_ctx = fi->priv;
478 OSMO_ASSERT(mgcp_ctx);
479
480 mgcp_ctx->mgw_trans_pending = false;
481
482 if (r->head.response_code != 250) {
483 LOGPFSML(fi, LOGL_ERROR,
484 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
485 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
486 return;
487 }
488
489 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
490}
491
492static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
493{
494 struct mgcp_ctx *mgcp_ctx = data;
495 OSMO_ASSERT(mgcp_ctx);
496
497 switch (event) {
498 case EV_DLCX_RESP:
499 /* Rub out the connection identifier, since the connection
500 * is no longer present and we will use the connection id
501 * to know in error cases if the connection is still present
502 * or not */
503 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
504
505 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
506 break;
507 default:
508 OSMO_ASSERT(false);
509 break;
510 }
511}
512
513static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
514{
515 struct mgcp_ctx *mgcp_ctx = fi->priv;
516 struct mgcp_client *mgcp;
517
518 OSMO_ASSERT(mgcp_ctx);
519 mgcp = mgcp_ctx->mgcp;
520 OSMO_ASSERT(mgcp);
521
522 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
523 /* Note: We were unable to communicate with the MGW,
524 * unfortunately there is no meaningful action we can take
525 * now other than giving up. */
526 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
527 } else {
528 /* Note: Ther must not be any unsolicited timers
529 * in this FSM. If so, we have serious problem. */
530 OSMO_ASSERT(false);
531 }
532
533 return 0;
534}
535
536static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
537{
538 struct mgcp_ctx *mgcp_ctx = fi->priv;
539 struct mgcp_client *mgcp;
540 struct msgb *msg;
541
542 OSMO_ASSERT(mgcp_ctx);
543 mgcp = mgcp_ctx->mgcp;
544 OSMO_ASSERT(mgcp);
545
546 /* If there is still a transaction pending, cancel it now. */
547 if (mgcp_ctx->mgw_trans_pending)
548 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
549
550 /* Should the FSM be terminated while there are still open connections
551 * on the MGW, we send an unconditional DLCX to terminate the
552 * connection. This is not the normal case. The user should always use
Neels Hofmeyr73716632021-05-01 02:32:29 +0000553 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit.
554 * If in ST_DLCX_RESP, a DLCX was already sent and we did not get a
555 * response. No point in sending another one. */
556 if (fi->state != ST_DLCX_RESP && strlen(mgcp_ctx->conn_id)) {
Neels Hofmeyr427cede2021-05-01 02:32:50 +0000557 LOGPFSML(fi, LOGL_INFO, "Conn cleanup, sending DLCX for %s %s\n", mgcp_ctx->conn_peer_remote.endpoint,
558 mgcp_ctx->conn_id);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100559 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr8c69e292019-09-19 03:05:00 +0200560 if (!msg)
561 LOGPFSML(fi, LOGL_ERROR, "MGW/DLCX: Error composing DLCX message\n");
562 else
563 mgcp_client_tx(mgcp, msg, NULL, NULL);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100564 }
565
566 talloc_free(mgcp_ctx);
567}
568
569static struct osmo_fsm_state fsm_mgcp_client_states[] = {
570
571 /* Initial CRCX state. This state is immediately entered and executed
572 * when the FSM is started. The rationale is that we first have to
573 * create a connectin before we can execute other operations on that
574 * connection. */
575 [ST_CRCX] = {
576 .in_event_mask = S(EV_CRCX),
577 .out_state_mask = S(ST_CRCX_RESP),
578 .name = OSMO_STRINGIFY(ST_CRCX),
579 .action = fsm_crcx_cb,
580 },
581
582 /* Wait for the response to a CRCX operation, check and process the
583 * results, change to ST_READY afterwards. */
584 [ST_CRCX_RESP] = {
585 .in_event_mask = S(EV_CRCX_RESP),
586 .out_state_mask = S(ST_READY),
587 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
588 .action = fsm_crcx_resp_cb,
589 },
590
591 /* In this idle state we wait for further operations (e.g. MDCX) that
592 * can be executed by the user using the API. There is no timeout in
593 * this state. The connection lives on until the user decides to
594 * terminate it (DLCX). */
595 [ST_READY] = {
596 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
597 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
598 .name = OSMO_STRINGIFY(ST_READY),
599 .action = fsm_ready_cb,
600 },
601
602 /* Wait for the response of a MDCX operation, check and process the
603 * results, change to ST_READY afterwards. */
604 [ST_MDCX_RESP] = {
605 .in_event_mask = S(EV_MDCX_RESP),
606 .out_state_mask = S(ST_READY),
607 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
608 .action = fsm_mdcx_resp_cb,
609 },
610
611 /* Wait for the response of a DLCX operation and terminate the FSM
612 * normally. */
613 [ST_DLCX_RESP] = {
614 .in_event_mask = S(EV_DLCX_RESP),
615 .out_state_mask = 0,
616 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
617 .action = fsm_dlcx_resp_cb,
618 },
619};
620
621static struct osmo_fsm fsm_mgcp_client = {
622 .name = "MGCP_CONN",
623 .states = fsm_mgcp_client_states,
624 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
625 .timer_cb = fsm_timeout_cb,
626 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100627 .event_names = fsm_mgcp_client_evt_names,
Neels Hofmeyra8c684b2021-07-15 01:37:00 +0200628 .log_subsys = DLMGCP,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100629};
630
631/*! allocate FSM, and create a new connection on the MGW.
632 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200633 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100634 * \param[in] parent_term_evt Event to be sent to parent when terminating.
635 * \param[in] parent_evt Event to be sent to parent when operation is done.
636 * \param[in] conn_peer Connection parameters (ip, port...).
637 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
638struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
639 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
640{
641 struct mgcp_ctx *mgcp_ctx;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100642 struct osmo_fsm_inst *fi;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200643 struct in6_addr ip_test;
644
Philipp Maier8bda7a72018-01-17 14:32:23 +0100645
646 OSMO_ASSERT(parent_fi);
647 OSMO_ASSERT(mgcp);
648 OSMO_ASSERT(conn_peer);
649
Philipp Maier704c4f02018-06-07 18:51:31 +0200650 /* Check if IP/Port information in conn info makes sense */
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200651 if (conn_peer->port && inet_pton(osmo_ip_str_type(conn_peer->addr),
652 conn_peer->addr, &ip_test) != 1)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100653 return NULL;
654
Philipp Maier8bda7a72018-01-17 14:32:23 +0100655 /* Allocate and configure a new fsm instance */
656 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
657 OSMO_ASSERT(fi);
658 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
659 OSMO_ASSERT(mgcp_ctx);
660 mgcp_ctx->mgcp = mgcp;
661 mgcp_ctx->parent_evt = parent_evt;
662
663 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
664 fi->priv = mgcp_ctx;
665
666 /* start state machine */
667 OSMO_ASSERT(fi->state == ST_CRCX);
668 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
669
670 return fi;
671}
672
673/*! modify an existing connection on the MGW.
674 * \param[in] fi FSM instance.
675 * \param[in] parent_evt Event to be sent to parent when operation is done.
676 * \param[in] conn_peer New connection information (ip, port...).
677 * \returns 0 on success, -EINVAL on error. */
678int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
679{
680 OSMO_ASSERT(fi);
681 struct mgcp_ctx *mgcp_ctx = fi->priv;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200682 struct in6_addr ip_test;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100683
684 OSMO_ASSERT(mgcp_ctx);
685 OSMO_ASSERT(conn_peer);
686
687 /* The user must not issue an MDCX before the CRCX has completed,
688 * if this happens, it means that the parent FSM has overhead the
689 * parent_evt (mandatory!) and executed the MDCX without even
690 * waiting for the results. Another reason could be that the
691 * parent FSM got messed up */
692 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
693
694 /* If the user tries to issue an MDCX while an DLCX operation is
695 * pending, there must be a serious problem with the paren FSM.
696 * Eeither the parent_term_evt (mandatory!) has been overheard,
697 * or the parant FSM got messed so badly that it still assumes
698 * a live connection although it as killed it. */
699 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
700
701 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200702 if (conn_peer->port == 0) {
703 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100704 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200705 }
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200706 if (inet_pton(osmo_ip_str_type(conn_peer->addr), conn_peer->addr, &ip_test) != 1) {
707 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address %s\n", conn_peer->addr);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100708 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200709 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100710
711 /*! The user may supply an endpoint identifier in conn_peer. The
712 * identifier is then checked. This check is optional. Later steps do
713 * not depend on the endpoint identifier supplied here because it is
714 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200715 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
716 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
717 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100718 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200719 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100720
721 /*! Note: The call-id is implicitly known from the previous CRCX and
722 * will not be checked even when it is set in conn_peer. */
723
724 mgcp_ctx->parent_evt = parent_evt;
725 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
726 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
727 return 0;
728}
729
730/*! delete existing connection on the MGW, destroy FSM afterwards.
731 * \param[in] fi FSM instance. */
732void mgcp_conn_delete(struct osmo_fsm_inst *fi)
733{
734 OSMO_ASSERT(fi);
735 struct mgcp_ctx *mgcp_ctx = fi->priv;
736
737 OSMO_ASSERT(mgcp_ctx);
738
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200739 if (fi->proc.terminating)
740 return;
741
Neels Hofmeyr9de30e72021-07-15 02:21:14 +0200742 /* Unlink FSM from parent, set the struct mgcp_client as new talloc ctx. */
743 osmo_fsm_inst_unlink_parent(fi, mgcp_ctx->mgcp);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100744
745 /* An error situation where the parent FSM must be killed immediately
746 * may lead into a situation where the DLCX can not be executed right
747 * at that moment because the FSM is still busy with another operation.
748 * In those cases we postpone the DLCX so that the FSM and the
749 * connections on the MGW get cleaned up gracefully. */
750 if (fi->state != ST_READY) {
751 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
752 mgcp_ctx->terminate = true;
753 return;
754 }
755 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
756}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100757
758const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
759{
760 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
761 * this buffer is too small. */
762 static char buf[1024];
763
764 if (!info)
765 return "NULL";
766
767 if (info->endpoint[0]
768 && info->addr[0])
769 snprintf(buf, sizeof(buf), "%s:%s:%u",
770 info->endpoint, info->addr, info->port);
771 else if (info->endpoint[0])
772 snprintf(buf, sizeof(buf), "%s", info->endpoint);
773 else if (info->addr[0])
774 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
775 else
776 return "empty";
777 return buf;
778}
Harald Welte6a25a612019-12-01 15:37:47 +0100779
Harald Welte9befdeb2022-11-03 11:41:05 +0100780static __attribute__((constructor)) void osmo_mgcp_client_fsm_init(void)
Harald Welte6a25a612019-12-01 15:37:47 +0100781{
782 OSMO_ASSERT(osmo_fsm_register(&fsm_mgcp_client) == 0);
783}