blob: 2505ff281db96034ff859ce561f5756e258b9c77 [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 Maier8bda7a72018-01-17 14:32:23 +0100255static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
256{
257 struct osmo_fsm_inst *fi = priv;
258 struct mgcp_ctx *mgcp_ctx;
259 int rc;
260
261 OSMO_ASSERT(fi);
262 mgcp_ctx = fi->priv;
263 OSMO_ASSERT(mgcp_ctx);
264
265 mgcp_ctx->mgw_trans_pending = false;
266
267 if (r->head.response_code != 200) {
268 LOGPFSML(fi, LOGL_ERROR,
269 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
270 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
271 return;
272 }
273
274 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
275 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
276
277 rc = mgcp_response_parse_params(r);
278 if (rc) {
279 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
280 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
281 return;
282 }
283 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 +0200284 if (r->head.x_osmo_osmux_use) {
285 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
286 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
287 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
288 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100289
290 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
291 mgcp_ctx->conn_peer_remote.port = r->audio_port;
292
293 if (strlen(r->head.endpoint) > 0) {
294 /* If we get an endpoint identifier back from the MGW, take it */
295 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
296 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
297 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
298 /* If we do not get an endpoint identifier back and the
299 * identifier we used to create the connection is not a
300 * wildcarded one, we take the local endpoint identifier
301 * instead */
302 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
303 sizeof(mgcp_ctx->conn_peer_local.endpoint));
304 } else {
305 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
306 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
307 return;
308 }
309
310 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
311
312 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
313}
314
315static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
316{
317 struct mgcp_ctx *mgcp_ctx = data;
318 OSMO_ASSERT(mgcp_ctx);
319
320 switch (event) {
321 case EV_CRCX_RESP:
322 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
323 if (mgcp_ctx->terminate) {
324 /* Trigger immediate DLCX if DLCX was requested while the FSM was
325 * busy with the previous operation */
326 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
327 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
328 } else
329 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
330 break;
331 default:
332 OSMO_ASSERT(false);
333 break;
334 }
335}
336
337static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
338static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
339
340static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
341{
342 struct mgcp_ctx *mgcp_ctx = data;
343 struct msgb *msg;
344 struct mgcp_client *mgcp;
345 uint32_t new_state;
346 int rc;
347
348 OSMO_ASSERT(mgcp_ctx);
349 mgcp = mgcp_ctx->mgcp;
350 OSMO_ASSERT(mgcp);
351
352 switch (event) {
353 case EV_MDCX:
354 msg = make_mdcx_msg(mgcp_ctx);
355 OSMO_ASSERT(msg);
356 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
357 new_state = ST_MDCX_RESP;
358 break;
359 case EV_DLCX:
360 msg = make_dlcx_msg(mgcp_ctx);
361 OSMO_ASSERT(msg);
362 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
363 new_state = ST_DLCX_RESP;
364 break;
365 default:
366 OSMO_ASSERT(false);
367 break;
368 }
369
370 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
371 mgcp_ctx->mgw_trans_pending = true;
372
373 if (rc < 0) {
374 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
375 return;
376 }
377
378 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
379}
380
381static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
382{
383 struct osmo_fsm_inst *fi = priv;
384 struct mgcp_ctx *mgcp_ctx;
385 int rc;
386
387 OSMO_ASSERT(fi);
388 mgcp_ctx = fi->priv;
389 OSMO_ASSERT(mgcp_ctx);
390
391 mgcp_ctx->mgw_trans_pending = false;
392
393 if (r->head.response_code != 200) {
394 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
395 r->head.comment);
396 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
397 return;
398 }
399
400 rc = mgcp_response_parse_params(r);
401 if (rc) {
402 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
403 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
404 return;
405 }
406 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
407
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200408 if (r->head.x_osmo_osmux_use) {
409 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
410 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
411 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
412 }
413
Philipp Maier8bda7a72018-01-17 14:32:23 +0100414 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
415 mgcp_ctx->conn_peer_remote.port = r->audio_port;
416
417 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
418}
419
420static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
421{
422 struct mgcp_ctx *mgcp_ctx = data;
423 OSMO_ASSERT(mgcp_ctx);
424
425 switch (event) {
426 case EV_MDCX_RESP:
427 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
428 if (mgcp_ctx->terminate) {
429 /* Trigger immediate DLCX if DLCX was requested while the FSM was
430 * busy with the previous operation */
431 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
432 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
433 } else
434 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
435 break;
436 default:
437 OSMO_ASSERT(false);
438 break;
439 }
440}
441
442static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
443{
444 struct osmo_fsm_inst *fi = priv;
445 struct mgcp_ctx *mgcp_ctx;
446
447 OSMO_ASSERT(fi);
448 mgcp_ctx = fi->priv;
449 OSMO_ASSERT(mgcp_ctx);
450
451 mgcp_ctx->mgw_trans_pending = false;
452
453 if (r->head.response_code != 250) {
454 LOGPFSML(fi, LOGL_ERROR,
455 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
456 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
457 return;
458 }
459
460 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
461}
462
463static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
464{
465 struct mgcp_ctx *mgcp_ctx = data;
466 OSMO_ASSERT(mgcp_ctx);
467
468 switch (event) {
469 case EV_DLCX_RESP:
470 /* Rub out the connection identifier, since the connection
471 * is no longer present and we will use the connection id
472 * to know in error cases if the connection is still present
473 * or not */
474 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
475
476 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
477 break;
478 default:
479 OSMO_ASSERT(false);
480 break;
481 }
482}
483
484static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
485{
486 struct mgcp_ctx *mgcp_ctx = fi->priv;
487 struct mgcp_client *mgcp;
488
489 OSMO_ASSERT(mgcp_ctx);
490 mgcp = mgcp_ctx->mgcp;
491 OSMO_ASSERT(mgcp);
492
493 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
494 /* Note: We were unable to communicate with the MGW,
495 * unfortunately there is no meaningful action we can take
496 * now other than giving up. */
497 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
498 } else {
499 /* Note: Ther must not be any unsolicited timers
500 * in this FSM. If so, we have serious problem. */
501 OSMO_ASSERT(false);
502 }
503
504 return 0;
505}
506
507static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
508{
509 struct mgcp_ctx *mgcp_ctx = fi->priv;
510 struct mgcp_client *mgcp;
511 struct msgb *msg;
512
513 OSMO_ASSERT(mgcp_ctx);
514 mgcp = mgcp_ctx->mgcp;
515 OSMO_ASSERT(mgcp);
516
517 /* If there is still a transaction pending, cancel it now. */
518 if (mgcp_ctx->mgw_trans_pending)
519 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
520
521 /* Should the FSM be terminated while there are still open connections
522 * on the MGW, we send an unconditional DLCX to terminate the
523 * connection. This is not the normal case. The user should always use
524 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit */
525 if (strlen(mgcp_ctx->conn_id)) {
526 LOGPFSML(fi, LOGL_ERROR,
Harald Welted4e6aa42018-06-02 18:07:10 +0200527 "MGW/DLCX: abrupt FSM termination with connections still present, sending unconditional DLCX...\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100528 msg = make_dlcx_msg(mgcp_ctx);
Neels Hofmeyr8c69e292019-09-19 03:05:00 +0200529 if (!msg)
530 LOGPFSML(fi, LOGL_ERROR, "MGW/DLCX: Error composing DLCX message\n");
531 else
532 mgcp_client_tx(mgcp, msg, NULL, NULL);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100533 }
534
535 talloc_free(mgcp_ctx);
536}
537
538static struct osmo_fsm_state fsm_mgcp_client_states[] = {
539
540 /* Initial CRCX state. This state is immediately entered and executed
541 * when the FSM is started. The rationale is that we first have to
542 * create a connectin before we can execute other operations on that
543 * connection. */
544 [ST_CRCX] = {
545 .in_event_mask = S(EV_CRCX),
546 .out_state_mask = S(ST_CRCX_RESP),
547 .name = OSMO_STRINGIFY(ST_CRCX),
548 .action = fsm_crcx_cb,
549 },
550
551 /* Wait for the response to a CRCX operation, check and process the
552 * results, change to ST_READY afterwards. */
553 [ST_CRCX_RESP] = {
554 .in_event_mask = S(EV_CRCX_RESP),
555 .out_state_mask = S(ST_READY),
556 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
557 .action = fsm_crcx_resp_cb,
558 },
559
560 /* In this idle state we wait for further operations (e.g. MDCX) that
561 * can be executed by the user using the API. There is no timeout in
562 * this state. The connection lives on until the user decides to
563 * terminate it (DLCX). */
564 [ST_READY] = {
565 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
566 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
567 .name = OSMO_STRINGIFY(ST_READY),
568 .action = fsm_ready_cb,
569 },
570
571 /* Wait for the response of a MDCX operation, check and process the
572 * results, change to ST_READY afterwards. */
573 [ST_MDCX_RESP] = {
574 .in_event_mask = S(EV_MDCX_RESP),
575 .out_state_mask = S(ST_READY),
576 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
577 .action = fsm_mdcx_resp_cb,
578 },
579
580 /* Wait for the response of a DLCX operation and terminate the FSM
581 * normally. */
582 [ST_DLCX_RESP] = {
583 .in_event_mask = S(EV_DLCX_RESP),
584 .out_state_mask = 0,
585 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
586 .action = fsm_dlcx_resp_cb,
587 },
588};
589
590static struct osmo_fsm fsm_mgcp_client = {
591 .name = "MGCP_CONN",
592 .states = fsm_mgcp_client_states,
593 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
594 .timer_cb = fsm_timeout_cb,
595 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100596 .event_names = fsm_mgcp_client_evt_names,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100597};
598
599/*! allocate FSM, and create a new connection on the MGW.
600 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200601 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100602 * \param[in] parent_term_evt Event to be sent to parent when terminating.
603 * \param[in] parent_evt Event to be sent to parent when operation is done.
604 * \param[in] conn_peer Connection parameters (ip, port...).
605 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
606struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
607 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
608{
609 struct mgcp_ctx *mgcp_ctx;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100610 struct osmo_fsm_inst *fi;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200611 struct in6_addr ip_test;
612
Philipp Maier8bda7a72018-01-17 14:32:23 +0100613
614 OSMO_ASSERT(parent_fi);
615 OSMO_ASSERT(mgcp);
616 OSMO_ASSERT(conn_peer);
617
Philipp Maier704c4f02018-06-07 18:51:31 +0200618 /* Check if IP/Port information in conn info makes sense */
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200619 if (conn_peer->port && inet_pton(osmo_ip_str_type(conn_peer->addr),
620 conn_peer->addr, &ip_test) != 1)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100621 return NULL;
622
Philipp Maier8bda7a72018-01-17 14:32:23 +0100623 /* Allocate and configure a new fsm instance */
624 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
625 OSMO_ASSERT(fi);
626 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
627 OSMO_ASSERT(mgcp_ctx);
628 mgcp_ctx->mgcp = mgcp;
629 mgcp_ctx->parent_evt = parent_evt;
630
631 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
632 fi->priv = mgcp_ctx;
633
634 /* start state machine */
635 OSMO_ASSERT(fi->state == ST_CRCX);
636 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
637
638 return fi;
639}
640
641/*! modify an existing connection on the MGW.
642 * \param[in] fi FSM instance.
643 * \param[in] parent_evt Event to be sent to parent when operation is done.
644 * \param[in] conn_peer New connection information (ip, port...).
645 * \returns 0 on success, -EINVAL on error. */
646int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
647{
648 OSMO_ASSERT(fi);
649 struct mgcp_ctx *mgcp_ctx = fi->priv;
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200650 struct in6_addr ip_test;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100651
652 OSMO_ASSERT(mgcp_ctx);
653 OSMO_ASSERT(conn_peer);
654
655 /* The user must not issue an MDCX before the CRCX has completed,
656 * if this happens, it means that the parent FSM has overhead the
657 * parent_evt (mandatory!) and executed the MDCX without even
658 * waiting for the results. Another reason could be that the
659 * parent FSM got messed up */
660 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
661
662 /* If the user tries to issue an MDCX while an DLCX operation is
663 * pending, there must be a serious problem with the paren FSM.
664 * Eeither the parent_term_evt (mandatory!) has been overheard,
665 * or the parant FSM got messed so badly that it still assumes
666 * a live connection although it as killed it. */
667 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
668
669 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200670 if (conn_peer->port == 0) {
671 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100672 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200673 }
Pau Espin Pedrolc4ef4a22020-09-02 17:00:12 +0200674 if (inet_pton(osmo_ip_str_type(conn_peer->addr), conn_peer->addr, &ip_test) != 1) {
675 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address %s\n", conn_peer->addr);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100676 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200677 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100678
679 /*! The user may supply an endpoint identifier in conn_peer. The
680 * identifier is then checked. This check is optional. Later steps do
681 * not depend on the endpoint identifier supplied here because it is
682 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200683 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
684 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
685 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100686 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200687 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100688
689 /*! Note: The call-id is implicitly known from the previous CRCX and
690 * will not be checked even when it is set in conn_peer. */
691
692 mgcp_ctx->parent_evt = parent_evt;
693 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
694 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
695 return 0;
696}
697
698/*! delete existing connection on the MGW, destroy FSM afterwards.
699 * \param[in] fi FSM instance. */
700void mgcp_conn_delete(struct osmo_fsm_inst *fi)
701{
702 OSMO_ASSERT(fi);
703 struct mgcp_ctx *mgcp_ctx = fi->priv;
704
705 OSMO_ASSERT(mgcp_ctx);
706
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200707 if (fi->proc.terminating)
708 return;
709
Philipp Maier8bda7a72018-01-17 14:32:23 +0100710 /* Unlink FSM from parent */
711 osmo_fsm_inst_unlink_parent(fi, NULL);
712
713 /* An error situation where the parent FSM must be killed immediately
714 * may lead into a situation where the DLCX can not be executed right
715 * at that moment because the FSM is still busy with another operation.
716 * In those cases we postpone the DLCX so that the FSM and the
717 * connections on the MGW get cleaned up gracefully. */
718 if (fi->state != ST_READY) {
719 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
720 mgcp_ctx->terminate = true;
721 return;
722 }
723 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
724}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100725
726const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
727{
728 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
729 * this buffer is too small. */
730 static char buf[1024];
731
732 if (!info)
733 return "NULL";
734
735 if (info->endpoint[0]
736 && info->addr[0])
737 snprintf(buf, sizeof(buf), "%s:%s:%u",
738 info->endpoint, info->addr, info->port);
739 else if (info->endpoint[0])
740 snprintf(buf, sizeof(buf), "%s", info->endpoint);
741 else if (info->addr[0])
742 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
743 else
744 return "empty";
745 return buf;
746}
Harald Welte6a25a612019-12-01 15:37:47 +0100747
748static __attribute__((constructor)) void osmo_mgcp_client_fsm_init()
749{
750 OSMO_ASSERT(osmo_fsm_register(&fsm_mgcp_client) == 0);
751}