blob: c68d8b396a4d88eeb8b372aa7beda5cd9afc4807 [file] [log] [blame]
Neels Hofmeyr538d2c52019-01-28 03:51:35 +01001/* FSM to manage multiple connections of an MGW endpoint
2 *
3 * (C) 2018-2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr <neels@hofmeyr.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26
27#include <osmocom/core/fsm.h>
28#include <osmocom/core/byteswap.h>
29#include <osmocom/core/tdef.h>
Pau Espin Pedrol729bf3e2020-08-28 19:28:07 +020030#include <osmocom/core/sockaddr_str.h>
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010031
32#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
33
34#define LOG_CI(ci, level, fmt, args...) do { \
35 if (!ci || !ci->ep) \
36 LOGP(DLGLOBAL, level, "(unknown MGW endpoint) " fmt, ## args); \
37 else \
38 LOG_MGCPC_EP(ci->ep, level, "CI[%d] %s%s%s: " fmt, \
39 (int)(ci - ci->ep->ci), \
40 ci->label ? : "-", \
41 ci->mgcp_ci_str[0] ? " CI=" : "", \
42 ci->mgcp_ci_str[0] ? ci->mgcp_ci_str : "", \
43 ## args); \
44 } while(0)
45
46#define LOG_CI_VERB(ci, level, fmt, args...) do { \
47 if (ci->verb_info.addr[0]) \
48 LOG_CI(ci, level, "%s %s:%u: " fmt, \
49 osmo_mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port, \
50 ## args); \
51 else \
52 LOG_CI(ci, level, "%s: " fmt, \
53 osmo_mgcp_verb_name(ci->verb), \
54 ## args); \
55 } while(0)
56
57enum osmo_mgcpc_ep_fsm_state {
58 OSMO_MGCPC_EP_ST_UNUSED = 0,
59 OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE,
60 OSMO_MGCPC_EP_ST_IN_USE,
61};
62
63enum osmo_mgcpc_ep_fsm_event {
64 _OSMO_MGCPC_EP_EV_LAST = 0,
65 /* and MGW response events are allocated dynamically */
66};
67
68#define FIRST_CI_EVENT (_OSMO_MGCPC_EP_EV_LAST + (_OSMO_MGCPC_EP_EV_LAST & 1)) /* rounded up to even nr */
69#define USABLE_CI ((32 - FIRST_CI_EVENT)/2)
70#define EV_TO_CI_IDX(event) ((event - FIRST_CI_EVENT) / 2)
71
72#define CI_EV_SUCCESS(ci) (FIRST_CI_EVENT + (((ci) - ci->ep->ci) * 2))
73#define CI_EV_FAILURE(ci) (CI_EV_SUCCESS(ci) + 1)
74
75static struct osmo_fsm osmo_mgcpc_ep_fsm;
76
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010077struct fsm_notify {
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +010078 struct llist_head entry;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010079 struct osmo_fsm_inst *fi;
80 uint32_t success;
81 uint32_t failure;
82 void *data;
83};
84
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010085/*! One connection on an endpoint, corresponding to a connection identifier (CI) as returned by the MGW.
86 * An endpoint has a fixed number of slots of these, which may or may not be in use.
87 */
88struct osmo_mgcpc_ep_ci {
89 struct osmo_mgcpc_ep *ep;
90
91 bool occupied;
92 char label[64];
93 struct osmo_fsm_inst *mgcp_client_fi;
94
95 bool pending;
96 bool sent;
97 enum mgcp_verb verb;
98 struct mgcp_conn_peer verb_info;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010099 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100100
101 bool got_port_info;
102 struct mgcp_conn_peer rtp_info;
103 char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
104};
105
106/*! An MGW endpoint with N connections, like "rtpbridge/23@mgw". */
107struct osmo_mgcpc_ep {
108 /*! MGCP client connection to the MGW. */
109 struct mgcp_client *mgcp_client;
110 struct osmo_fsm_inst *fi;
111
112 /*! Endpoint string; at first this might be a wildcard, and upon the first CRCX OK response, this will reflect
113 * the endpoint name returned by the MGW. */
114 char endpoint[MGCP_ENDPOINT_MAXLEN];
115
116 /*! Timeout definitions used for this endpoint, see osmo_mgcpc_ep_fsm_timeouts. */
117 const struct osmo_tdef *T_defs;
118
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200119 /*! True as soon as the first CRCX OK is received. The endpoint name may be determined by the first CRCX
120 * response, so to dispatch any other messages, the FSM instance *must* wait for the first CRCX OK to arrive
121 * first. Once the endpoint name is pinpointed, any amount of operations may be dispatched concurrently. */
122 bool first_crcx_complete;
123
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100124 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
125 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
126 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100127
128 /*! Internal use: if a function keeps an fsm_notify for later dispatch while already clearing or re-using the
129 * ci[], the fsm_notify should be kept here to also get canceled by osmo_mgcpc_ep_cancel_notify(). */
130 struct llist_head background_notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100131};
132
133const struct value_string osmo_mgcp_verb_names[] = {
134 { MGCP_VERB_CRCX, "CRCX" },
135 { MGCP_VERB_MDCX, "MDCX" },
136 { MGCP_VERB_DLCX, "DLCX" },
137 { MGCP_VERB_AUEP, "AUEP" },
138 { MGCP_VERB_RSIP, "RSIP" },
139 {}
140};
141
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200142static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
143 int *waiting_for_response);
144
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100145static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
146{
147 if (!ci)
148 return NULL;
149 if (!ci->ep)
150 return NULL;
151 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
152 return NULL;
153 return ci;
154}
155
156static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
157{
158 int idx;
159 if (event < FIRST_CI_EVENT)
160 return NULL;
161 idx = EV_TO_CI_IDX(event);
162 if (idx >= sizeof(ep->ci))
163 return NULL;
164 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
165}
166
167const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
168{
169 if (!ep)
170 return "NULL";
171 if (ep->endpoint[0])
172 return ep->endpoint;
173 return osmo_fsm_inst_name(ep->fi);
174}
175
176const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
177{
178 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
179 * this buffer is too small. */
180 static char buf[1024];
181
182 if (!info)
183 return "NULL";
184
185 if (info->endpoint[0]
186 && info->addr[0])
187 snprintf(buf, sizeof(buf), "%s:%s:%u",
188 info->endpoint, info->addr, info->port);
189 else if (info->endpoint[0])
190 snprintf(buf, sizeof(buf), "%s", info->endpoint);
191 else if (info->addr[0])
192 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
193 else
194 return "empty";
195 return buf;
196}
197
198const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
199{
200 const struct mgcp_conn_peer *rtp_info;
201
202 if (!ci)
203 return "NULL";
204
205 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
206
207 if (rtp_info)
208 return mgcp_conn_peer_name(rtp_info);
209 return osmo_mgcpc_ep_name(ci->ep);
210}
211
212const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
213{
214 if (!ci || !ci->mgcp_ci_str[0])
215 return NULL;
216 return ci->mgcp_ci_str;
217}
218
219static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
220
221static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
222
223static void fill_event_names()
224{
225 int i;
226 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
227 if (i < _OSMO_MGCPC_EP_EV_LAST)
228 continue;
229 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
230 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
231 continue;
232 }
233 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
234 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
235 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
236 }
237}
238
239/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
240 * default MGCP port in osmo-mgw). */
241static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
242{
243 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
244 fill_event_names();
245}
246
247struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
248{
249 OSMO_ASSERT(fi);
250 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
251 OSMO_ASSERT(fi->priv);
252 return fi->priv;
253}
254
255/*! Allocate an osmo_mgcpc_ep FSM.
256 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
257 * osmo_mgcpc_ep_ci_request() is invoked.
258 *
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200259 * IMPORTANT: To avoid use-after-free problems, using this FSM requires use of deferred FSM deallocation using
260 * osmo_fsm_set_dealloc_ctx(), e.g. using osmo_select_main_ctx(OTC_SELECT) with osmo_select_main_ctx() as main loop.
261 *
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100262 * A typical sequence of events would be:
263 *
264 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
265 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
266 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
267 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
268 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
269 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
270 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
271 * ...
272 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
273 * ...
274 * osmo_mgcpc_ep_clear(ep);
275 * ep = NULL;
276 *
277 * \param parent Parent FSM.
278 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
279 * \param mgcp_client Connection to the MGW.
280 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
281 * \param fsm_id FSM instance ID.
282 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
283 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
284 */
285struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
286 struct mgcp_client *mgcp_client,
287 const struct osmo_tdef *T_defs,
288 const char *fsm_id,
289 const char *endpoint_str_fmt, ...)
290{
291 va_list ap;
292 struct osmo_fsm_inst *fi;
293 struct osmo_mgcpc_ep *ep;
294 int rc;
295
296 if (!mgcp_client)
297 return NULL;
298
299 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
300 OSMO_ASSERT(fi);
301
302 osmo_fsm_inst_update_id(fi, fsm_id);
303
304 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
305 OSMO_ASSERT(ep);
306
307 *ep = (struct osmo_mgcpc_ep){
308 .mgcp_client = mgcp_client,
309 .fi = fi,
310 .T_defs = T_defs,
311 };
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100312 INIT_LLIST_HEAD(&ep->background_notify);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100313 fi->priv = ep;
314
315 va_start(ap, endpoint_str_fmt);
316 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
317 va_end(ap);
318
319 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
320 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
321 ep->endpoint);
322 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
323 return NULL;
324 }
325
326 return ep;
327}
328
329/*! Add a connection to an endpoint.
330 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
331 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
332 * \param ep Parent endpoint instance.
333 * \param label_fmt Label for logging.
334 */
335struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
336 const char *label_fmt, ...)
337{
338 va_list ap;
339 int i;
340 struct osmo_mgcpc_ep_ci *ci;
341
342 for (i = 0; i < USABLE_CI; i++) {
343 ci = &ep->ci[i];
344
345 if (ci->occupied || ci->mgcp_client_fi)
346 continue;
347
348 *ci = (struct osmo_mgcpc_ep_ci){
349 .ep = ep,
350 .occupied = true,
351 };
352 if (label_fmt) {
353 va_start(ap, label_fmt);
354 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
355 va_end(ap);
356 }
357 return ci;
358 }
359
360 LOG_MGCPC_EP(ep, LOGL_ERROR,
361 "Cannot allocate another endpoint, all "
362 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
363
364 return NULL;
365}
366
367static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
368
369static void on_failure(struct osmo_mgcpc_ep_ci *ci)
370{
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200371 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100372 struct fsm_notify notify;
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200373 int i;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100374
375 if (!ci->occupied)
376 return;
377
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100378 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn right
379 * away. So we must be ready with a cleared *ci. Store the notify separately and clear before dispatching. */
380 notify = ci->notify;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100381 /* Register the planned notification in ep->background_notify so we also catch any osmo_mgcpc_ep_cancel_notify()
382 * that might be triggered between clearing the ci and actually dispatching the event. */
383 llist_add(&notify.entry, &ep->background_notify);
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100384
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100385 *ci = (struct osmo_mgcpc_ep_ci){
386 .ep = ci->ep,
387 };
388
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200389 /* An MGCP failure typically means the endpoint becomes unusable, cancel all pending request (except DLCX).
390 * Particularly, if two CRCX were scheduled and the first fails, we must no longer dispatch the second CRCX. */
391 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
392 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
393 if (other_ci == ci)
394 continue;
395 if (!other_ci->occupied)
396 continue;
397 if (!other_ci->pending)
398 continue;
399 if (other_ci->sent)
400 continue;
401 if (other_ci->verb == MGCP_VERB_DLCX)
402 continue;
403 /* Just clear the pending request, don't fire more events than below. */
404 other_ci->pending = false;
405 }
406
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100407 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
408 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100409 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ep->fi)) {
410 /* The ep has deallocated, no need to llist_del(&notify.entry) here. */
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100411 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100412 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100413
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100414 if (notify.fi)
415 osmo_fsm_inst_dispatch(notify.fi, notify.failure, notify.data);
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100416
417 llist_del(&notify.entry);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100418}
419
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200420static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
421{
422 struct osmo_mgcpc_ep *ep = ci->ep;
423 int rc;
424 int i;
425
426 if (!strcmp(ep->endpoint, new_endpoint_name)) {
427 /* Same endpoint name, nothing to do. */
428 return 0;
429 }
430
431 /* The endpoint name should only change on the very first CRCX response. */
432 if (ep->first_crcx_complete) {
433 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
434 " This is endpoint %s, instead received %s\n",
435 ep->endpoint, new_endpoint_name);
436 on_failure(ci);
437 return -EINVAL;
438 }
439
440 /* This is the first CRCX response, update endpoint name. */
441 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
442 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
443 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
444 osmo_mgcpc_ep_ci_dlcx(ci);
445 on_failure(ci);
446 return -ENOSPC;
447 }
448
449 /* Make sure already pending requests use this updated endpoint name. */
450 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
451 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
452 if (!other_ci->occupied)
453 continue;
454 if (!other_ci->pending)
455 continue;
456 if (other_ci->sent)
457 continue;
458 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
459 }
460 return 0;
461}
462
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100463static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
464{
465 struct mgcp_conn_peer *rtp_info;
466
467 if (!ci->occupied)
468 return;
469
470 ci->pending = false;
471
472 switch (ci->verb) {
473 case MGCP_VERB_CRCX:
474 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
475 * name here. Also, we receive the MGW's RTP port information. */
476 rtp_info = data;
477 OSMO_ASSERT(rtp_info);
478 ci->got_port_info = true;
479 ci->rtp_info = *rtp_info;
480 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
481 sizeof(ci->mgcp_ci_str));
482 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200483 /* On errors, this instance might already be deallocated. Make sure to not access anything after
484 * error. */
485 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100486 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100487 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200488 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100489 break;
490
491 default:
492 break;
493 }
494
495 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
496 osmo_mgcp_verb_name(ci->verb),
497 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100498 ci->notify.fi ? "" : " (not sending a notification)");
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100499
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100500 if (ci->notify.fi)
501 osmo_fsm_inst_dispatch(ci->notify.fi, ci->notify.success, ci->notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100502
503 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
504}
505
506/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
507const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
508{
509 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
510 if (!ci)
511 return NULL;
512 if (!ci->got_port_info)
513 return NULL;
514 return &ci->rtp_info;
515}
516
517/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
518bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
519{
520 const struct mgcp_conn_peer *rtp_info;
Pau Espin Pedrol729bf3e2020-08-28 19:28:07 +0200521 int family;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100522 struct sockaddr_in *sin;
Pau Espin Pedrol729bf3e2020-08-28 19:28:07 +0200523 struct sockaddr_in6 *sin6;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100524
525 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
526 if (!rtp_info)
527 return false;
528
Pau Espin Pedrol729bf3e2020-08-28 19:28:07 +0200529 family = osmo_ip_str_type(rtp_info->addr);
530 switch (family) {
531 case AF_INET:
532 sin = (struct sockaddr_in *)dest;
533 sin->sin_family = AF_INET;
534 sin->sin_port = osmo_ntohs(rtp_info->port);
535 if (inet_pton(AF_INET, rtp_info->addr, &sin->sin_addr) != 1)
536 return false;
537 break;
538 case AF_INET6:
539 sin6 = (struct sockaddr_in6 *)dest;
540 sin6->sin6_family = AF_INET6;
541 sin6->sin6_port = osmo_ntohs(rtp_info->port);
542 if (inet_pton(AF_INET6, rtp_info->addr, &sin6->sin6_addr) != 1)
543 return false;
544 break;
545 default:
546 return false;
547 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100548 return true;
549}
550
Pau Espin Pedrol30907dc2019-05-06 11:54:17 +0200551bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid)
552{
553 const struct mgcp_conn_peer *rtp_info;
554
555 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
556 if (!rtp_info)
557 return false;
558
559 if (!rtp_info->x_osmo_osmux_use)
560 return false;
561
562 *cid = rtp_info->x_osmo_osmux_cid;
563 return true;
564}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100565
566static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
567 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
568};
569
570/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
571 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
572 * Assumes local variable fi exists. */
573#define osmo_mgcpc_ep_fsm_state_chg(state) \
574 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
575 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
576
577/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100578 *
579 * If the 'notify' instance deallocates before it received a notification of event_success or event_failure,
580 * osmo_mgcpc_ep_ci_cancel_notify() or osmo_mgcpc_ep_cancel_notify() must be called. It is not harmful to cancel
581 * notification after an event has been received.
582 *
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100583 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
584 * \param verb MGCP operation to dispatch.
585 * \param verb_info Parameters for the MGCP operation.
586 * \param notify Peer FSM instance to notify of completed/failed operation.
587 * \param event_success Which event to dispatch to 'notify' upon OK response.
588 * \param event_failure Which event to dispatch to 'notify' upon failure response.
589 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
590 */
591void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
592 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
593 struct osmo_fsm_inst *notify,
594 uint32_t event_success, uint32_t event_failure,
595 void *notify_data)
596{
597 struct osmo_mgcpc_ep *ep;
598 struct osmo_fsm_inst *fi;
599 struct osmo_mgcpc_ep_ci cleared_ci;
600 ci = osmo_mgcpc_ep_check_ci(ci);
601
602 if (!ci) {
603 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
604 goto dispatch_error;
605 }
606 if (!verb_info && verb != MGCP_VERB_DLCX) {
607 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
608 osmo_mgcp_verb_name(verb));
609 goto dispatch_error;
610 }
611 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
612 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
613 osmo_mgcp_verb_name(verb));
614 goto dispatch_error;
615 }
616
617 ep = ci->ep;
618 fi = ep->fi;
619
620 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
621 * the char[] directly, dance through cleared_ci and copy back. */
622 cleared_ci = (struct osmo_mgcpc_ep_ci){
623 .ep = ep,
624 .mgcp_client_fi = ci->mgcp_client_fi,
625 .got_port_info = ci->got_port_info,
626 .rtp_info = ci->rtp_info,
627
628 .occupied = true,
629 /* .pending = true follows below */
630 .verb = verb,
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100631 .notify = {
632 .fi = notify,
633 .success = event_success,
634 .failure = event_failure,
635 .data = notify_data,
636 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100637 };
638 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
639 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
640 *ci = cleared_ci;
641
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100642 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify.fi));
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100643
644 if (verb_info)
645 ci->verb_info = *verb_info;
646
647 if (ep->endpoint[0]) {
648 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
649 LOG_CI(ci, LOGL_ERROR,
650 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
651 " Using the proper endpoint instead.\n",
652 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
653 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
654 }
655
656 switch (ci->verb) {
657 case MGCP_VERB_CRCX:
658 if (ci->mgcp_client_fi) {
659 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
660 on_failure(ci);
661 return;
662 }
663 break;
664
665 case MGCP_VERB_MDCX:
666 if (!ci->mgcp_client_fi) {
667 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
668 osmo_mgcp_verb_name(ci->verb));
669 on_failure(ci);
670 return;
671 }
672 break;
673
674 case MGCP_VERB_DLCX:
675 if (!ci->mgcp_client_fi) {
676 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
677 return;
678 }
679 break;
680
681 default:
682 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
683 on_failure(ci);
684 return;
685 }
686
687 ci->pending = true;
688
689 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
690
691 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
692 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
693
694 return;
695dispatch_error:
696 if (notify)
697 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
698}
699
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100700/*! No longer notify for any state changes for any conns of this endpoint.
701 * Useful if the notify instance passed to osmo_mgcpc_ep_ci_request() is about to deallocate.
702 * \param ep The endpoint FSM instance.
703 * \param notify Which target to cancel notification for, if NULL cancel all notifications. */
704void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify)
705{
706 struct fsm_notify *n;
707 int i;
708 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
709 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
710 if (!notify || ci->notify.fi == notify)
711 ci->notify.fi = NULL;
712 }
713 llist_for_each_entry(n, &ep->background_notify, entry) {
714 if (!notify || n->fi == notify)
715 n->fi = NULL;
716 }
717
718}
719
Neels Hofmeyr923d60b2019-10-29 17:40:08 +0100720/* Return the osmo_mgcpc_ep that this conn belongs to. */
721struct osmo_mgcpc_ep *osmo_mgcpc_ep_ci_ep(struct osmo_mgcpc_ep_ci *conn)
722{
723 if (!conn)
724 return NULL;
725 return conn->ep;
726}
727
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100728static int send_verb(struct osmo_mgcpc_ep_ci *ci)
729{
730 int rc;
731 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100732 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100733
734 if (!ci->occupied || !ci->pending || ci->sent)
735 return 0;
736
737 switch (ci->verb) {
738
739 case MGCP_VERB_CRCX:
740 OSMO_ASSERT(!ci->mgcp_client_fi);
741 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
742 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
743 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
744 &ci->verb_info);
745 ci->sent = true;
746 if (!ci->mgcp_client_fi){
747 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
748 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200749 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100750 }
751 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
752 break;
753
754 case MGCP_VERB_MDCX:
755 OSMO_ASSERT(ci->mgcp_client_fi);
756 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
757 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
758 ci->sent = true;
759 if (rc) {
760 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
761 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200762 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100763 }
764 break;
765
766 case MGCP_VERB_DLCX:
767 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
768 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
769 /* The way this is designed, we actually need to forget all about the ci right away. */
770 mgcp_conn_delete(ci->mgcp_client_fi);
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100771 notify = ci->notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100772 *ci = (struct osmo_mgcpc_ep_ci){
773 .ep = ep,
774 };
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100775 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn
776 * right away. So we must be ready with a cleared *ci. */
777 if (notify.fi)
778 osmo_fsm_inst_dispatch(notify.fi, notify.success, notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100779 break;
780
781 default:
782 OSMO_ASSERT(false);
783 }
784
785 return 1;
786}
787
788/*! DLCX all connections, terminate the endpoint FSM and free. */
789void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
790{
791 if (!ep)
792 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100793 osmo_mgcpc_ep_cancel_notify(ep, NULL);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100794 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
795}
796
797static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
798 int *waiting_for_response)
799{
800 int i;
801
802 if (occupied)
803 *occupied = 0;
804
805 if (pending_not_sent)
806 *pending_not_sent = 0;
807
808 if (waiting_for_response)
809 *waiting_for_response = 0;
810
811 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
812 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
813 if (ci->occupied) {
814 if (occupied)
815 (*occupied)++;
816 } else
817 continue;
818
819 if (ci->pending)
820 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
821 ci->sent ? "waiting for response" : "waiting to be sent");
822 else
823 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
824
825 if (ci->pending && ci->sent)
826 if (waiting_for_response)
827 (*waiting_for_response)++;
828 if (ci->pending && !ci->sent)
829 if (pending_not_sent)
830 (*pending_not_sent)++;
831 }
832}
833
834static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
835{
836 int waiting_for_response;
837 int occupied;
838 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
839
840 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
841 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
842
843 if (!occupied) {
844 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
845 * terminating. */
846 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
847 return false;
848 }
849
850 if (!waiting_for_response) {
851 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
852 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
853 }
854
855 return true;
856}
857
858static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
859{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200860 static int re_enter = 0;
861 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100862 int count = 0;
863 int i;
864 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
865
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200866 re_enter++;
867 OSMO_ASSERT(re_enter < 10);
868
869 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
870 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
871 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
872 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
873 * sent (which needs to be a CRCX). */
874
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100875 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200876 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
877
878 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
879 if (!ep->first_crcx_complete) {
880 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
881 continue;
882 }
883
884 rc = send_verb(&ep->ci[i]);
885 /* Need to be careful not to access the instance after failure. Event chains may already have
886 * deallocated this memory. */
887 if (rc < 0)
888 return;
889 if (!rc)
890 continue;
891 count++;
892 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
893 if (!ep->first_crcx_complete)
894 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100895 }
896
897 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200898 if (ep->first_crcx_complete)
899 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
900 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100901}
902
903static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
904{
905 struct osmo_mgcpc_ep_ci *ci;
906 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
907 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
908 if (ci) {
909 if (event == CI_EV_SUCCESS(ci))
910 on_success(ci, data);
911 else
912 on_failure(ci);
913 }
914}
915
916static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
917{
918 int pending_not_sent;
919 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
920
921 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
922 if (pending_not_sent)
923 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
924}
925
926#define S(x) (1 << (x))
927
928static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
929 [OSMO_MGCPC_EP_ST_UNUSED] = {
930 .name = "UNUSED",
931 .in_event_mask = 0,
932 .out_state_mask = 0
933 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
934 ,
935 },
936 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
937 .name = "WAIT_MGW_RESPONSE",
938 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
939 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
940 .in_event_mask = 0xffffffff,
941 .out_state_mask = 0
942 | S(OSMO_MGCPC_EP_ST_IN_USE)
943 ,
944 },
945 [OSMO_MGCPC_EP_ST_IN_USE] = {
946 .name = "IN_USE",
947 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
948 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
949 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
950 .out_state_mask = 0
951 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
952 ,
953 },
954};
955
956static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
957{
958 int i;
959 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
960
961 switch (fi->T) {
962 default:
963 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
964 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
965 if (!ci->occupied)
966 continue;
967 if (!(ci->pending && ci->sent))
968 continue;
969 on_failure(ci);
970 }
971 return 0;
972 }
973
974 return 0;
975}
976
977static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200978 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100979 .states = osmo_mgcpc_ep_fsm_states,
980 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
981 .log_subsys = DLMGCP,
982 .event_names = osmo_mgcpc_ep_fsm_event_names,
983 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
984 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
985};