blob: 1a2b6d8e0754db054215047b25ca19495733efae [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{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200139 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT;
140 mgcp_msg->audio_ip = info->addr;
141 mgcp_msg->audio_port = info->port;
Philipp Maiera5e0cf02018-08-29 14:33:01 +0200142 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100143}
144
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100145static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
146{
147 enum mgcp_connection_mode conn_mode = peer->conn_mode;
148 if (conn_mode != MGCP_CONN_NONE)
149 mgcp_msg->conn_mode = conn_mode;
150}
151
Philipp Maier8bda7a72018-01-17 14:32:23 +0100152static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
153{
154 struct mgcp_msg mgcp_msg;
155
156 mgcp_msg = (struct mgcp_msg) {
157 .verb = MGCP_VERB_MDCX,
158 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
159 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
160 .call_id = mgcp_ctx->conn_peer_remote.call_id,
161 .conn_id = mgcp_ctx->conn_id,
162 .conn_mode = MGCP_CONN_RECV_SEND,
163 .audio_ip = mgcp_ctx->conn_peer_local.addr,
164 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200165 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200166 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100167 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
168 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100169 };
170 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200171 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200172 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100173 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100174
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100175 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
176
Pau Espin Pedrolca538fc2019-05-10 16:49:59 +0200177 if (mgcp_ctx->conn_peer_local.x_osmo_osmux_use) {
178 mgcp_msg.x_osmo_osmux_cid = mgcp_ctx->conn_peer_local.x_osmo_osmux_cid;
179 mgcp_msg.presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
180 }
181
Philipp Maier8bda7a72018-01-17 14:32:23 +0100182 /* Note: We take the endpoint and the call_id from the remote
183 * connection info, because we can be confident that the
184 * information there is valid. For the local info, we explicitly
185 * allow endpoint and call_id to be optional */
186 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
187}
188
189struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
190{
191 struct mgcp_msg mgcp_msg;
192
193 mgcp_msg = (struct mgcp_msg) {
194 .verb = MGCP_VERB_DLCX,
195 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
196 .call_id = mgcp_ctx->conn_peer_remote.call_id,
197 .conn_id = mgcp_ctx->conn_id,
198 };
199 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
200
201 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
202}
203
204static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
205
206static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
207{
208 struct mgcp_ctx *mgcp_ctx = data;
209 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200210 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100211 struct msgb *msg;
212 int rc;
213
214 OSMO_ASSERT(mgcp_ctx);
215 mgcp = mgcp_ctx->mgcp;
216 OSMO_ASSERT(mgcp);
217
218 switch (event) {
219 case EV_CRCX:
220 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
221 mgcp_ctx->conn_peer_local.endpoint);
222
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200223 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100224 if (mgcp_ctx->conn_peer_local.port)
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200225 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100226 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
227
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200228 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100229 OSMO_ASSERT(msg);
230
231 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
232 mgcp_ctx->mgw_trans_pending = true;
233 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
234 if (rc < 0) {
235 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
236 return;
237 }
238
239 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
240 break;
241 default:
242 OSMO_ASSERT(false);
243 break;
244 }
245}
246
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200247/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
248 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
249const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
250{
251 struct mgcp_ctx *mgcp_ctx = fi->priv;
252 return mgcp_ctx->conn_id;
253}
254
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200255/* Get the mgcp_client that is used with this mgcp_client_fsm instance */
256struct mgcp_client *mgcp_conn_get_client(struct osmo_fsm_inst *fi)
257{
258 struct mgcp_ctx *mgcp_ctx;
259
260 if (!fi)
261 return NULL;
262
263 mgcp_ctx = fi->priv;
264 return mgcp_ctx->mgcp;
265}
266
Philipp Maier8bda7a72018-01-17 14:32:23 +0100267static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
268{
269 struct osmo_fsm_inst *fi = priv;
270 struct mgcp_ctx *mgcp_ctx;
271 int rc;
272
273 OSMO_ASSERT(fi);
274 mgcp_ctx = fi->priv;
275 OSMO_ASSERT(mgcp_ctx);
276
277 mgcp_ctx->mgw_trans_pending = false;
278
279 if (r->head.response_code != 200) {
280 LOGPFSML(fi, LOGL_ERROR,
281 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
282 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
283 return;
284 }
285
286 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
287 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
288
289 rc = mgcp_response_parse_params(r);
290 if (rc) {
291 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
292 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
293 return;
294 }
295 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 +0200296 if (r->head.x_osmo_osmux_use) {
297 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
298 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
299 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
300 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100301
302 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
303 mgcp_ctx->conn_peer_remote.port = r->audio_port;
304
305 if (strlen(r->head.endpoint) > 0) {
306 /* If we get an endpoint identifier back from the MGW, take it */
307 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
308 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
309 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
310 /* If we do not get an endpoint identifier back and the
311 * identifier we used to create the connection is not a
312 * wildcarded one, we take the local endpoint identifier
313 * instead */
314 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
315 sizeof(mgcp_ctx->conn_peer_local.endpoint));
316 } else {
317 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
318 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
319 return;
320 }
321
322 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
323
324 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
325}
326
327static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
328{
329 struct mgcp_ctx *mgcp_ctx = data;
330 OSMO_ASSERT(mgcp_ctx);
331
332 switch (event) {
333 case EV_CRCX_RESP:
334 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
335 if (mgcp_ctx->terminate) {
336 /* Trigger immediate DLCX if DLCX was requested while the FSM was
337 * busy with the previous operation */
338 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
339 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
340 } else
341 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
342 break;
343 default:
344 OSMO_ASSERT(false);
345 break;
346 }
347}
348
349static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
350static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
351
352static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
353{
354 struct mgcp_ctx *mgcp_ctx = data;
355 struct msgb *msg;
356 struct mgcp_client *mgcp;
357 uint32_t new_state;
358 int rc;
359
360 OSMO_ASSERT(mgcp_ctx);
361 mgcp = mgcp_ctx->mgcp;
362 OSMO_ASSERT(mgcp);
363
364 switch (event) {
365 case EV_MDCX:
366 msg = make_mdcx_msg(mgcp_ctx);
367 OSMO_ASSERT(msg);
368 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
369 new_state = ST_MDCX_RESP;
370 break;
371 case EV_DLCX:
372 msg = make_dlcx_msg(mgcp_ctx);
373 OSMO_ASSERT(msg);
374 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
375 new_state = ST_DLCX_RESP;
376 break;
377 default:
378 OSMO_ASSERT(false);
379 break;
380 }
381
382 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
383 mgcp_ctx->mgw_trans_pending = true;
384
385 if (rc < 0) {
386 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
387 return;
388 }
389
390 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
391}
392
393static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
394{
395 struct osmo_fsm_inst *fi = priv;
396 struct mgcp_ctx *mgcp_ctx;
397 int rc;
398
399 OSMO_ASSERT(fi);
400 mgcp_ctx = fi->priv;
401 OSMO_ASSERT(mgcp_ctx);
402
403 mgcp_ctx->mgw_trans_pending = false;
404
405 if (r->head.response_code != 200) {
406 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
407 r->head.comment);
408 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
409 return;
410 }
411
412 rc = mgcp_response_parse_params(r);
413 if (rc) {
414 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
415 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
416 return;
417 }
418 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
419
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200420 if (r->head.x_osmo_osmux_use) {
421 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
422 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
423 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
424 }
425
Philipp Maier8bda7a72018-01-17 14:32:23 +0100426 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
427 mgcp_ctx->conn_peer_remote.port = r->audio_port;
428
429 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
430}
431
432static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
433{
434 struct mgcp_ctx *mgcp_ctx = data;
435 OSMO_ASSERT(mgcp_ctx);
436
437 switch (event) {
438 case EV_MDCX_RESP:
439 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
440 if (mgcp_ctx->terminate) {
441 /* Trigger immediate DLCX if DLCX was requested while the FSM was
442 * busy with the previous operation */
443 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
444 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
445 } else
446 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
447 break;
448 default:
449 OSMO_ASSERT(false);
450 break;
451 }
452}
453
454static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
455{
456 struct osmo_fsm_inst *fi = priv;
457 struct mgcp_ctx *mgcp_ctx;
458
459 OSMO_ASSERT(fi);
460 mgcp_ctx = fi->priv;
461 OSMO_ASSERT(mgcp_ctx);
462
463 mgcp_ctx->mgw_trans_pending = false;
464
465 if (r->head.response_code != 250) {
466 LOGPFSML(fi, LOGL_ERROR,
467 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
468 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
469 return;
470 }
471
472 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
473}
474
475static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
476{
477 struct mgcp_ctx *mgcp_ctx = data;
478 OSMO_ASSERT(mgcp_ctx);
479
480 switch (event) {
481 case EV_DLCX_RESP:
482 /* Rub out the connection identifier, since the connection
483 * is no longer present and we will use the connection id
484 * to know in error cases if the connection is still present
485 * or not */
486 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
487
488 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
489 break;
490 default:
491 OSMO_ASSERT(false);
492 break;
493 }
494}
495
496static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
497{
498 struct mgcp_ctx *mgcp_ctx = fi->priv;
499 struct mgcp_client *mgcp;
500
501 OSMO_ASSERT(mgcp_ctx);
502 mgcp = mgcp_ctx->mgcp;
503 OSMO_ASSERT(mgcp);
504
505 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
506 /* Note: We were unable to communicate with the MGW,
507 * unfortunately there is no meaningful action we can take
508 * now other than giving up. */
509 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
510 } else {
511 /* Note: Ther must not be any unsolicited timers
512 * in this FSM. If so, we have serious problem. */
513 OSMO_ASSERT(false);
514 }
515
516 return 0;
517}
518
519static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
520{
521 struct mgcp_ctx *mgcp_ctx = fi->priv;
522 struct mgcp_client *mgcp;
523 struct msgb *msg;
524
525 OSMO_ASSERT(mgcp_ctx);
526 mgcp = mgcp_ctx->mgcp;
527 OSMO_ASSERT(mgcp);
528
529 /* If there is still a transaction pending, cancel it now. */
530 if (mgcp_ctx->mgw_trans_pending)
531 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
532
533 /* Should the FSM be terminated while there are still open connections
534 * on the MGW, we send an unconditional DLCX to terminate the
535 * connection. This is not the normal case. The user should always use
Neels Hofmeyr73716632021-05-01 02:32:29 +0000536 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit.
537 * If in ST_DLCX_RESP, a DLCX was already sent and we did not get a
538 * response. No point in sending another one. */
539 if (fi->state != ST_DLCX_RESP && strlen(mgcp_ctx->conn_id)) {
Neels Hofmeyr427cede2021-05-01 02:32:50 +0000540 LOGPFSML(fi, LOGL_INFO, "Conn cleanup, sending DLCX for %s %s\n", mgcp_ctx->conn_peer_remote.endpoint,
541 mgcp_ctx->conn_id);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100542 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr8c69e292019-09-19 03:05:00 +0200543 if (!msg)
544 LOGPFSML(fi, LOGL_ERROR, "MGW/DLCX: Error composing DLCX message\n");
545 else
546 mgcp_client_tx(mgcp, msg, NULL, NULL);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100547 }
548
549 talloc_free(mgcp_ctx);
550}
551
552static struct osmo_fsm_state fsm_mgcp_client_states[] = {
553
554 /* Initial CRCX state. This state is immediately entered and executed
555 * when the FSM is started. The rationale is that we first have to
556 * create a connectin before we can execute other operations on that
557 * connection. */
558 [ST_CRCX] = {
559 .in_event_mask = S(EV_CRCX),
560 .out_state_mask = S(ST_CRCX_RESP),
561 .name = OSMO_STRINGIFY(ST_CRCX),
562 .action = fsm_crcx_cb,
563 },
564
565 /* Wait for the response to a CRCX operation, check and process the
566 * results, change to ST_READY afterwards. */
567 [ST_CRCX_RESP] = {
568 .in_event_mask = S(EV_CRCX_RESP),
569 .out_state_mask = S(ST_READY),
570 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
571 .action = fsm_crcx_resp_cb,
572 },
573
574 /* In this idle state we wait for further operations (e.g. MDCX) that
575 * can be executed by the user using the API. There is no timeout in
576 * this state. The connection lives on until the user decides to
577 * terminate it (DLCX). */
578 [ST_READY] = {
579 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
580 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
581 .name = OSMO_STRINGIFY(ST_READY),
582 .action = fsm_ready_cb,
583 },
584
585 /* Wait for the response of a MDCX operation, check and process the
586 * results, change to ST_READY afterwards. */
587 [ST_MDCX_RESP] = {
588 .in_event_mask = S(EV_MDCX_RESP),
589 .out_state_mask = S(ST_READY),
590 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
591 .action = fsm_mdcx_resp_cb,
592 },
593
594 /* Wait for the response of a DLCX operation and terminate the FSM
595 * normally. */
596 [ST_DLCX_RESP] = {
597 .in_event_mask = S(EV_DLCX_RESP),
598 .out_state_mask = 0,
599 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
600 .action = fsm_dlcx_resp_cb,
601 },
602};
603
604static struct osmo_fsm fsm_mgcp_client = {
605 .name = "MGCP_CONN",
606 .states = fsm_mgcp_client_states,
607 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
608 .timer_cb = fsm_timeout_cb,
609 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100610 .event_names = fsm_mgcp_client_evt_names,
Neels Hofmeyra8c684b2021-07-15 01:37:00 +0200611 .log_subsys = DLMGCP,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100612};
613
614/*! allocate FSM, and create a new connection on the MGW.
615 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200616 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100617 * \param[in] parent_term_evt Event to be sent to parent when terminating.
618 * \param[in] parent_evt Event to be sent to parent when operation is done.
619 * \param[in] conn_peer Connection parameters (ip, port...).
620 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
621struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
622 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
623{
624 struct mgcp_ctx *mgcp_ctx;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100625 struct osmo_fsm_inst *fi;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200626 struct in6_addr ip_test;
627
Philipp Maier8bda7a72018-01-17 14:32:23 +0100628
629 OSMO_ASSERT(parent_fi);
630 OSMO_ASSERT(mgcp);
631 OSMO_ASSERT(conn_peer);
632
Philipp Maier704c4f02018-06-07 18:51:31 +0200633 /* Check if IP/Port information in conn info makes sense */
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200634 if (conn_peer->port && inet_pton(osmo_ip_str_type(conn_peer->addr),
635 conn_peer->addr, &ip_test) != 1)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100636 return NULL;
637
Philipp Maier8bda7a72018-01-17 14:32:23 +0100638 /* Allocate and configure a new fsm instance */
639 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
640 OSMO_ASSERT(fi);
641 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
642 OSMO_ASSERT(mgcp_ctx);
643 mgcp_ctx->mgcp = mgcp;
644 mgcp_ctx->parent_evt = parent_evt;
645
646 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
647 fi->priv = mgcp_ctx;
648
649 /* start state machine */
650 OSMO_ASSERT(fi->state == ST_CRCX);
651 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
652
653 return fi;
654}
655
656/*! modify an existing connection on the MGW.
657 * \param[in] fi FSM instance.
658 * \param[in] parent_evt Event to be sent to parent when operation is done.
659 * \param[in] conn_peer New connection information (ip, port...).
660 * \returns 0 on success, -EINVAL on error. */
661int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
662{
663 OSMO_ASSERT(fi);
664 struct mgcp_ctx *mgcp_ctx = fi->priv;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200665 struct in6_addr ip_test;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100666
667 OSMO_ASSERT(mgcp_ctx);
668 OSMO_ASSERT(conn_peer);
669
670 /* The user must not issue an MDCX before the CRCX has completed,
671 * if this happens, it means that the parent FSM has overhead the
672 * parent_evt (mandatory!) and executed the MDCX without even
673 * waiting for the results. Another reason could be that the
674 * parent FSM got messed up */
675 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
676
677 /* If the user tries to issue an MDCX while an DLCX operation is
678 * pending, there must be a serious problem with the paren FSM.
679 * Eeither the parent_term_evt (mandatory!) has been overheard,
680 * or the parant FSM got messed so badly that it still assumes
681 * a live connection although it as killed it. */
682 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
683
684 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200685 if (conn_peer->port == 0) {
686 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100687 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200688 }
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200689 if (inet_pton(osmo_ip_str_type(conn_peer->addr), conn_peer->addr, &ip_test) != 1) {
690 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address %s\n", conn_peer->addr);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100691 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200692 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100693
694 /*! The user may supply an endpoint identifier in conn_peer. The
695 * identifier is then checked. This check is optional. Later steps do
696 * not depend on the endpoint identifier supplied here because it is
697 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200698 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
699 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
700 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100701 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200702 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100703
704 /*! Note: The call-id is implicitly known from the previous CRCX and
705 * will not be checked even when it is set in conn_peer. */
706
707 mgcp_ctx->parent_evt = parent_evt;
708 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
709 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
710 return 0;
711}
712
713/*! delete existing connection on the MGW, destroy FSM afterwards.
714 * \param[in] fi FSM instance. */
715void mgcp_conn_delete(struct osmo_fsm_inst *fi)
716{
717 OSMO_ASSERT(fi);
718 struct mgcp_ctx *mgcp_ctx = fi->priv;
719
720 OSMO_ASSERT(mgcp_ctx);
721
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200722 if (fi->proc.terminating)
723 return;
724
Neels Hofmeyr9de30e72021-07-15 02:21:14 +0200725 /* Unlink FSM from parent, set the struct mgcp_client as new talloc ctx. */
726 osmo_fsm_inst_unlink_parent(fi, mgcp_ctx->mgcp);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100727
728 /* An error situation where the parent FSM must be killed immediately
729 * may lead into a situation where the DLCX can not be executed right
730 * at that moment because the FSM is still busy with another operation.
731 * In those cases we postpone the DLCX so that the FSM and the
732 * connections on the MGW get cleaned up gracefully. */
733 if (fi->state != ST_READY) {
734 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
735 mgcp_ctx->terminate = true;
736 return;
737 }
738 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
739}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100740
741const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
742{
743 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
744 * this buffer is too small. */
745 static char buf[1024];
746
747 if (!info)
748 return "NULL";
749
750 if (info->endpoint[0]
751 && info->addr[0])
752 snprintf(buf, sizeof(buf), "%s:%s:%u",
753 info->endpoint, info->addr, info->port);
754 else if (info->endpoint[0])
755 snprintf(buf, sizeof(buf), "%s", info->endpoint);
756 else if (info->addr[0])
757 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
758 else
759 return "empty";
760 return buf;
761}
Harald Welte6a25a612019-12-01 15:37:47 +0100762
763static __attribute__((constructor)) void osmo_mgcp_client_fsm_init()
764{
765 OSMO_ASSERT(osmo_fsm_register(&fsm_mgcp_client) == 0);
766}