blob: 8c2c11887a28eb820691e1ac321fed4bbff7b60f [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/* mgcp_utils - common functions to setup an MGCP connection
2 */
3/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte220f4872018-02-04 09:04:16 +01007 * 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
Neels Hofmeyre9920f22017-07-10 15:07:22 +02009 * (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
Harald Welte220f4872018-02-04 09:04:16 +010014 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020015 *
Harald Welte220f4872018-02-04 09:04:16 +010016 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/core/linuxlist.h>
22#include <osmocom/core/select.h>
23#include <osmocom/core/write_queue.h>
24#include <osmocom/core/msgb.h>
25#include <osmocom/core/logging.h>
Philipp Maier1dc6be62017-10-05 18:25:37 +020026#include <osmocom/core/byteswap.h>
Harald Welte8890dfa2017-11-17 15:09:30 +010027#include <osmocom/core/socket.h>
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +020028#include <osmocom/core/sockaddr_str.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020029
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020030#include <osmocom/mgcp_client/mgcp_client.h>
31#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020032
Philipp Maier92a73cd2020-11-26 22:21:11 +010033#include <osmocom/abis/e1_input.h>
34
Neels Hofmeyre9920f22017-07-10 15:07:22 +020035#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <errno.h>
39#include <unistd.h>
40#include <string.h>
Pau Espin Pedrol166077e2019-06-26 12:21:38 +020041#include <ctype.h>
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +020042#include <stdlib.h>
43#include <limits.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020044
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +020045#ifndef OSMUX_CID_MAX
46#define OSMUX_CID_MAX 255 /* FIXME: use OSMUX_CID_MAX from libosmo-netif? */
47#endif
48
Philipp Maierdf9192e2021-09-03 17:50:51 +020049#define LOGPMGW(mgcp, level, fmt, args...) \
50LOGP(DLMGCP, level, "MGW(%s) " fmt, mgcp_client_name(mgcp), ## args)
51
Philipp Maier704c4f02018-06-07 18:51:31 +020052/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010053const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020054 { CODEC_PCMU_8000_1, "PCMU/8000/1" },
55 { CODEC_GSM_8000_1, "GSM/8000/1" },
56 { CODEC_PCMA_8000_1, "PCMA/8000/1" },
57 { CODEC_G729_8000_1, "G729/8000/1" },
58 { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" },
59 { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" },
60 { CODEC_AMR_8000_1, "AMR/8000/1" },
61 { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" },
62 { 0, NULL },
63};
64
65/* Get encoding name from a full codec string e,g.
66 * ("CODEC/8000/2" => returns "CODEC") */
67static char *extract_codec_name(const char *str)
68{
69 static char buf[64];
70 unsigned int i;
71
72 if (!str)
73 return NULL;
74
Philipp Maier704c4f02018-06-07 18:51:31 +020075 osmo_strlcpy(buf, str, sizeof(buf));
76
77 for (i = 0; i < strlen(buf); i++) {
78 if (buf[i] == '/')
79 buf[i] = '\0';
80 }
81
82 return buf;
83}
84
85/*! Map a string to a codec.
86 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
87 * \returns codec that corresponds to the given string representation. */
88enum mgcp_codecs map_str_to_codec(const char *str)
89{
90 unsigned int i;
91 char *codec_name;
92 char str_buf[64];
93
94 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
95
Neels Hofmeyr47642f22019-02-27 05:56:53 +010096 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
97 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020098 if (!codec_name)
99 continue;
100 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +0100101 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +0200102 }
103
104 return -1;
105}
106
107/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200108static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200109{
110 /* Check if there are mappings that leave the IANA assigned dynamic
111 * payload type range. Under normal conditions such mappings should
112 * not occur */
113
114 /* Its ok to have a 1:1 mapping in the statically defined
115 * range, this won't hurt */
116 if (ptmap->codec == ptmap->pt)
117 return 0;
118
119 if (ptmap->codec < 96 || ptmap->codec > 127)
120 goto error;
121 if (ptmap->pt < 96 || ptmap->pt > 127)
122 goto error;
123
124 return 0;
125error:
126 LOGP(DLMGCP, LOGL_ERROR,
127 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
128 ptmap->codec, ptmap->pt);
129 return -1;
130}
131
132/*! Map a codec to a payload type.
133 * \ptmap[in] payload pointer to payload type map with specified payload types.
134 * \ptmap[in] ptmap_len length of the payload type map.
135 * \ptmap[in] codec the codec for which the payload type should be looked up.
136 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200137unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200138 enum mgcp_codecs codec)
139{
140 unsigned int i;
141
142 /*! Note: If the payload type map is empty or the codec is not found
143 * in the map, then a 1:1 mapping is performed. If the codec falls
144 * into the statically defined range or if the mapping table isself
145 * tries to map to the statically defined range, then the mapping
146 * is also ignored and a 1:1 mapping is performed instead. */
147
148 /* we may return the codec directly since enum mgcp_codecs directly
149 * corresponds to the statićally assigned payload types */
150 if (codec < 96 || codec > 127)
151 return codec;
152
153 for (i = 0; i < ptmap_len; i++) {
154 /* Skip illegal map entries */
155 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
156 return ptmap->pt;
157 ptmap++;
158 }
159
160 /* If nothing is found, do not perform any mapping */
161 return codec;
162}
163
164/*! Map a payload type to a codec.
165 * \ptmap[in] payload pointer to payload type map with specified payload types.
166 * \ptmap[in] ptmap_len length of the payload type map.
167 * \ptmap[in] payload type for which the codec should be looked up.
168 * \returns codec that corresponds to the specified payload type */
169enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
170 unsigned int pt)
171{
172 unsigned int i;
173
174 /*! Note: If the payload type map is empty or the payload type is not
175 * found in the map, then a 1:1 mapping is performed. If the payload
176 * type falls into the statically defined range or if the mapping
177 * table isself tries to map to the statically defined range, then
178 * the mapping is also ignored and a 1:1 mapping is performed
179 * instead. */
180
181 /* See also note in map_codec_to_pt() */
182 if (pt < 96 || pt > 127)
183 return pt;
184
185 for (i = 0; i < ptmap_len; i++) {
186 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
187 return ptmap->codec;
188 ptmap++;
189 }
190
191 /* If nothing is found, do not perform any mapping */
192 return pt;
193}
194
Philipp Maierc824fe42021-08-03 15:00:02 +0200195/*! Initialize MGCP client configuration struct with default values.
Philipp Maier275ac972018-01-20 00:58:16 +0100196 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200197void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200198{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200199 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
200 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200201 .local_addr = NULL,
202 .local_port = -1,
203 .remote_addr = NULL,
204 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200205 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200206
207 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200208}
209
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200210static void mgcp_client_handle_response(struct mgcp_client *mgcp,
211 struct mgcp_response_pending *pending,
212 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200213{
214 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200215 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200216 return;
217 }
218 if (pending->response_cb)
219 pending->response_cb(response, pending->priv);
220 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200221 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200222 talloc_free(pending);
223}
224
225static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
226{
227 int comment_pos;
228 char *end;
229
230 if (mgcp_msg_terminate_nul(msg))
231 goto response_parse_failure;
232
233 r->body = (char *)msg->data;
234
Harald Welte9bf7c532017-11-17 14:14:31 +0100235 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200236 &r->head.response_code, &r->head.trans_id,
237 &comment_pos) != 2)
238 goto response_parse_failure;
239
Philipp Maierabe8c892018-01-20 00:15:12 +0100240 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200241 end = strchr(r->head.comment, '\r');
242 if (!end)
243 goto response_parse_failure;
244 /* Mark the end of the comment */
245 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200246 return 0;
247
248response_parse_failure:
249 LOGP(DLMGCP, LOGL_ERROR,
250 "Failed to parse MGCP response header\n");
251 return -EINVAL;
252}
253
254/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
255static bool mgcp_line_is_valid(const char *line)
256{
257 const size_t line_len = strlen(line);
258 if (line[0] == '\0')
259 return true;
260
261 if (line_len < 2
262 || line[1] != '=') {
263 LOGP(DLMGCP, LOGL_ERROR,
264 "Wrong MGCP option format: '%s'\n",
265 line);
266 return false;
267 }
268
269 return true;
270}
271
Philipp Maier704c4f02018-06-07 18:51:31 +0200272/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
273static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274{
Philipp Maier704c4f02018-06-07 18:51:31 +0200275 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200276 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200277 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200278 unsigned int count = 0;
279 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200280
Philipp Maier704c4f02018-06-07 18:51:31 +0200281 /* Extract port information */
282 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
283 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100284 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200285 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100286
Philipp Maier704c4f02018-06-07 18:51:31 +0200287 /* Extract payload types */
288 line = strstr(line, "RTP/AVP ");
289 if (!line)
290 goto exit;
291
292 pt_str = strtok(line, " ");
293 while (1) {
294 /* Do not allow excessive payload types */
295 if (count > ARRAY_SIZE(r->codecs))
296 goto response_parse_failure_pt;
297
298 pt_str = strtok(NULL, " ");
299 if (!pt_str)
300 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200301 errno = 0;
302 pt = strtoul(pt_str, &pt_end, 0);
303 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
304 pt_str == pt_end)
305 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200306
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200307 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
308 goto response_parse_failure_pt;
309
Philipp Maier704c4f02018-06-07 18:51:31 +0200310 /* Do not allow duplicate payload types */
311 for (i = 0; i < count; i++)
312 if (r->codecs[i] == pt)
313 goto response_parse_failure_pt;
314
315 /* Note: The payload type we store may not necessarly match
316 * the codec types we have defined in enum mgcp_codecs. To
317 * ensure that the end result only contains codec types which
318 * match enum mgcp_codecs, we will go through afterwards and
319 * remap the affected entries with the inrofmation we learn
320 * from rtpmap */
321 r->codecs[count] = pt;
322 count++;
323 }
324
325 r->codecs_len = count;
326
327exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200328 return 0;
329
Philipp Maier704c4f02018-06-07 18:51:31 +0200330response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200331 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200332 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200333 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200334
335response_parse_failure_pt:
336 LOGP(DLMGCP, LOGL_ERROR,
337 "Failed to parse SDP parameter payload types (%s)\n", line);
338 return -EINVAL;
339}
340
341/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
342static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
343{
344 unsigned int pt;
345 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200346 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200347
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200348#define A_PTIME "a=ptime:"
349#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200350
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200351 if (osmo_str_startswith(line, A_PTIME)) {
352 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
353 LOGP(DLMGCP, LOGL_ERROR,
354 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
355 return -EINVAL;
356 }
357 } else if (osmo_str_startswith(line, A_RTPMAP)) {
358 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
359 LOGP(DLMGCP, LOGL_ERROR,
360 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
361 return -EINVAL;
362 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200363 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
364 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
365 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200366 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200367 codec = map_str_to_codec(codec_resp);
368 r->ptmap[r->ptmap_len].pt = pt;
369 r->ptmap[r->ptmap_len].codec = codec;
370 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200371 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200372
Philipp Maier704c4f02018-06-07 18:51:31 +0200373 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200374}
375
376/* Parse a line like "c=IN IP4 10.11.12.13" */
377static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
378{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200379 struct in6_addr ip_test;
380 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200381
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200382 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200383 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200384 line += 7;
385 if (*line == '6')
386 is_ipv6 = true;
387 else if (*line == '4')
388 is_ipv6 = false;
389 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200390 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200391 line++;
392 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200393 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200394 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200395
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200396 /* Extract and check IP-Address */
397 if (is_ipv6) {
398 /* 45 = INET6_ADDRSTRLEN -1 */
399 if (sscanf(line, "%45s", r->audio_ip) != 1)
400 goto response_parse_failure;
401 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
402 goto response_parse_failure;
403 } else {
404 /* 15 = INET_ADDRSTRLEN -1 */
405 if (sscanf(line, "%15s", r->audio_ip) != 1)
406 goto response_parse_failure;
407 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
408 goto response_parse_failure;
409 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200410 return 0;
411
412response_parse_failure:
413 LOGP(DLMGCP, LOGL_ERROR,
414 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200415 return -EINVAL;
416}
417
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200418/*! Extract OSMUX CID from an MGCP parameter line (string).
419 * \param[in] line single parameter line from the MGCP message
420 * \returns OSMUX CID, -1 wildcard, -2 on error
421 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
422 */
423static int mgcp_parse_osmux_cid(const char *line)
424{
425 int osmux_cid;
426
427
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200428 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200429 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
430 return -1;
431 }
432
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200433 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200434 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
435 line);
436 return -2;
437 }
438
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200439 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
440 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
441 osmux_cid, OSMUX_CID_MAX);
442 return -2;
443 }
444 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
445
446 return osmux_cid;
447}
448
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100449/* A new section is marked by a double line break, check a few more
450 * patterns as there may be variants */
451static char *mgcp_find_section_end(char *string)
452{
453 char *rc;
454
455 rc = strstr(string, "\n\n");
456 if (rc)
457 return rc;
458
459 rc = strstr(string, "\n\r\n\r");
460 if (rc)
461 return rc;
462
463 rc = strstr(string, "\r\n\r\n");
464 if (rc)
465 return rc;
466
467 return NULL;
468}
469
Philipp Maier275ac972018-01-20 00:58:16 +0100470/*! Parse body (SDP) parameters of the MGCP response
471 * \param[in,out] r Response data
472 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200473int mgcp_response_parse_params(struct mgcp_response *r)
474{
475 char *line;
476 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100477 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100478 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200479 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200480
Philipp Maiere9d645b2018-01-19 23:54:08 +0100481 /* Since this functions performs a destructive parsing, we create a
482 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100483 OSMO_ASSERT(r->body);
484 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100485 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100486
Philipp Maiere9d645b2018-01-19 23:54:08 +0100487 /* Find beginning of the parameter (SDP) section */
488 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100489 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200490 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
491 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100492 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200493 }
494
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100495 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
496 * skips any \r and \n characters for free, so we don't need to skip the marker. */
497
Philipp Maiere9d645b2018-01-19 23:54:08 +0100498 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200499 if (!mgcp_line_is_valid(line))
500 return -EINVAL;
501
502 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200503 case 'a':
504 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
505 if (rc)
506 goto exit;
507 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200508 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200509 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200510 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100511 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200512 break;
513 case 'c':
514 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200515 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100516 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200517 break;
518 default:
519 /* skip unhandled parameters */
520 break;
521 }
522 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100523
Philipp Maier704c4f02018-06-07 18:51:31 +0200524 /* See also note in mgcp_parse_audio_port_pt() */
525 for (i = 0; i < r->codecs_len; i++)
526 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
527
Philipp Maiere9d645b2018-01-19 23:54:08 +0100528 rc = 0;
529exit:
530 talloc_free(data);
531 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200532}
533
Philipp Maier55295f72018-01-15 14:00:28 +0100534/* Parse a line like "X: something" */
535static int mgcp_parse_head_param(char *result, unsigned int result_len,
536 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100537{
Philipp Maier55295f72018-01-15 14:00:28 +0100538 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200539 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100540
541 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100542 if (strlen(line) < 4)
543 goto response_parse_failure;
544
Philipp Maier55295f72018-01-15 14:00:28 +0100545 /* Check if the label matches */
546 snprintf(label_string, sizeof(label_string), "%c: ", label);
547 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100548 goto response_parse_failure;
549
Philipp Maier55295f72018-01-15 14:00:28 +0100550 /* Copy payload part of the string to destinations (the label string
551 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200552 rc = osmo_strlcpy(result, line + 3, result_len);
553 if (rc >= result_len) {
554 LOGP(DLMGCP, LOGL_ERROR,
555 "Failed to parse MGCP response (parameter label: %c):"
556 " the received conn ID is too long: %zu, maximum is %u characters\n",
557 label, rc, result_len - 1);
558 return -ENOSPC;
559 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100560 return 0;
561
562response_parse_failure:
563 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100564 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100565 return -EINVAL;
566}
567
568/* Parse MGCP parameters of the response */
569static int parse_head_params(struct mgcp_response *r)
570{
571 char *line;
572 int rc = 0;
573 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100574 char *data;
575 char *data_ptr;
576 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100577
Philipp Maier55295f72018-01-15 14:00:28 +0100578 /* Since this functions performs a destructive parsing, we create a
579 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100580 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100581 OSMO_ASSERT(data);
582 data_ptr = data;
583 osmo_strlcpy(data, r->body, strlen(r->body));
584
585 /* If there is an SDP body attached, prevent for_each_non_empty_line()
586 * into running in there, we are not yet interested in the parameters
587 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200588 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100589 if (data_end)
590 *data_end = '\0';
591
Philipp Maier55295f72018-01-15 14:00:28 +0100592 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200593 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100594 case 'Z':
595 rc = mgcp_parse_head_param(r->head.endpoint,
596 sizeof(r->head.endpoint),
597 'Z', line);
598 if (rc)
599 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100600
601 /* A specific endpoint identifier returned by the MGW
602 * must not contain any wildcard characters */
603 if (strstr(r->head.endpoint, "*") != NULL) {
604 rc = -EINVAL;
605 goto exit;
606 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100607
608 /* A specific endpoint identifier returned by the MGW
609 * must contain an @ character */
610 if (strstr(r->head.endpoint, "@") == NULL) {
611 rc = -EINVAL;
612 goto exit;
613 }
Philipp Maier55295f72018-01-15 14:00:28 +0100614 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100615 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100616 rc = mgcp_parse_head_param(r->head.conn_id,
617 sizeof(r->head.conn_id),
618 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100619 if (rc)
620 goto exit;
621 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200622 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200623 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200624 rc = mgcp_parse_osmux_cid(line);
625 if (rc < 0) {
626 /* -1: we don't want wildcards in response. -2: error */
627 rc = -EINVAL;
628 goto exit;
629 }
630 r->head.x_osmo_osmux_use = true;
631 r->head.x_osmo_osmux_cid = (uint8_t) rc;
632 rc = 0;
633 break;
634 }
635 /* Ignore unknown X-headers */
636 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100637 default:
638 /* skip unhandled parameters */
639 break;
640 }
641 }
642exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100643 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100644 return rc;
645}
646
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200647static struct mgcp_response_pending *mgcp_client_response_pending_get(
648 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100649 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200650{
651 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200652 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100653 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200654 llist_del(&pending->entry);
655 return pending;
656 }
657 }
658 return NULL;
659}
660
661/* Feed an MGCP message into the receive processing.
662 * Parse the head and call any callback registered for the transaction id found
663 * in the MGCP message. This is normally called directly from the internal
664 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
665 * function is published mainly to be able to feed data from the test suite.
666 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200667int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200668{
Philipp Maier1fc69992018-01-31 15:32:55 +0100669 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200670 struct mgcp_response_pending *pending;
671 int rc;
672
Philipp Maier1fc69992018-01-31 15:32:55 +0100673 r = talloc_zero(mgcp, struct mgcp_response);
674 OSMO_ASSERT(r);
675
676 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200677 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200678 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100679 rc = 1;
680 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100681 }
682
Philipp Maierdf9192e2021-09-03 17:50:51 +0200683 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200684 r->head.response_code, r->head.trans_id, r->head.comment);
685
Philipp Maier1fc69992018-01-31 15:32:55 +0100686 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100687 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200688 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100689 rc = 1;
690 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200691 }
692
Philipp Maier1fc69992018-01-31 15:32:55 +0100693 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200694 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200695 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
696 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100697 rc = -ENOENT;
698 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200699 }
700
Philipp Maier1fc69992018-01-31 15:32:55 +0100701 mgcp_client_handle_response(mgcp, pending, r);
702 rc = 0;
703
704error:
705 talloc_free(r);
706 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200707}
708
709static int mgcp_do_read(struct osmo_fd *fd)
710{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200711 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200712 struct msgb *msg;
713 int ret;
714
715 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
716 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200717 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200718 return -1;
719 }
720
721 ret = read(fd->fd, msg->data, 4096 - 128);
722 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200723 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
724 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100725
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200726 msgb_free(msg);
727 return -1;
728 } else if (ret > 4096 - 128) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200729 LOGPMGW(mgcp, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 msgb_free(msg);
731 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100732 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200733
734 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200735 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200736 talloc_free(msg);
737 return ret;
738}
739
740static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
741{
742 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200743 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200744
Philipp Maierdf9192e2021-09-03 17:50:51 +0200745 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
746 osmo_sock_get_name2(fd->fd), msg->len,
747 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200748
749 ret = write(fd->fd, msg->data, msg->len);
750 if (ret != msg->len)
Philipp Maierdf9192e2021-09-03 17:50:51 +0200751 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
752 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
753 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754 return ret;
755}
756
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200757struct mgcp_client *mgcp_client_init(void *ctx,
758 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200759{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200760 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200761 struct reset_ep *reset_ep;
762 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200763
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200764 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200765 if (!mgcp)
766 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200767
768 INIT_LLIST_HEAD(&mgcp->responses_pending);
769 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
770
771 mgcp->next_trans_id = 1;
772
773 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200774 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200776 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200777
778 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200779 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200780 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200781 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100783 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
784 sizeof(mgcp->actual.endpoint_domain_name))
785 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200786 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
787 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100788 talloc_free(mgcp);
789 return NULL;
790 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200791 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100792
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200793 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
794 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
795 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
796 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
797 }
798
Philipp Maierdf9192e2021-09-03 17:50:51 +0200799 if (conf->description)
800 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
801
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200802 return mgcp;
803}
804
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200805static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier6a26c162018-08-01 16:37:06 +0200806{
807 int rc;
808 struct osmo_wqueue *wq;
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200809 unsigned int i;
Philipp Maier6a26c162018-08-01 16:37:06 +0200810
811 wq = &mgcp->wq;
812
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200813 for (i = 0; i < retry_n_ports + 1; i++) {
Philipp Maier6a26c162018-08-01 16:37:06 +0200814
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200815 /* Initialize socket with the currently configured port number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200816 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200817 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
818 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
819 if (rc > 0)
820 return rc;
821
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200822 /* If there is a different port than the default port configured then we assume that the user has
823 * chosen that port conciously and we will not try to resolve this by silently choosing a different
824 * port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200825 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200826 return -EINVAL;
827
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200828 if (i == retry_n_ports) {
829 /* Last try failed */
Philipp Maierdf9192e2021-09-03 17:50:51 +0200830 LOGPMGW(mgcp, LOGL_NOTICE, "Failed to bind to %s:%d -- check configuration!\n",
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200831 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);
832 if (retry_n_ports == 0)
833 return -EINVAL;
834 } else {
835 /* Choose a new port number to try next */
Philipp Maierdf9192e2021-09-03 17:50:51 +0200836 LOGPMGW(mgcp, LOGL_NOTICE,
837 "Failed to bind to %s:%d, retrying with port %d -- check configuration!\n",
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200838 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
839 mgcp->actual.local_port + 1);
840 mgcp->actual.local_port++;
841 }
Philipp Maier6a26c162018-08-01 16:37:06 +0200842 }
843
Philipp Maierdf9192e2021-09-03 17:50:51 +0200844 LOGPMGW(mgcp, LOGL_FATAL, "Failed to find a port to bind on %u times -- check configuration!\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200845 return -EINVAL;
846}
847
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200848/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
849static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
850
851/* Format DLCX message (fire and forget) and send it off to the MGW */
852static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
853{
854 struct msgb *msgb_dlcx;
855 struct mgcp_msg mgcp_msg_dlcx = {
856 .verb = MGCP_VERB_DLCX,
857 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
858 };
859 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
860 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
861 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
862}
863
864static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
865{
866 static char endpoint[MGCP_ENDPOINT_MAXLEN];
867 int rc;
868
869 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
870 if (rc > sizeof(endpoint) - 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200871 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
872 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200873 return NULL;
874 }
875 if (rc < 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200876 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200877 return NULL;
878 }
879 return endpoint;
880}
881
882/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100883 * \param[in,out] mgcp MGCP client descriptor.
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200884 * \param[in] retry_n_ports number of consecutive local ports that should be used to retry on failure.
Philipp Maier275ac972018-01-20 00:58:16 +0100885 * \returns 0 on success, -EINVAL on error. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200886int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200887{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200888 struct osmo_wqueue *wq;
889 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200890 struct reset_ep *reset_ep;
891 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200892
893 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200894 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200895 return -EINVAL;
896 }
897
898 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200899 osmo_wqueue_init(wq, 1024);
900 wq->read_cb = mgcp_do_read;
901 wq->write_cb = mgcp_do_write;
902
903 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200904
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200905 rc = init_socket(mgcp, retry_n_ports);
Harald Welte8890dfa2017-11-17 15:09:30 +0100906 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200907 LOGPMGW(mgcp, LOGL_FATAL,
908 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200909 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
910 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
911 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200912 goto error_close_fd;
913 }
914
Philipp Maierdf9192e2021-09-03 17:50:51 +0200915 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200916
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200917 /* If configured, send a DLCX message to the endpoints that are configured to
918 * be reset on startup. Usually this is a wildcarded endpoint. */
919 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
920 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200921 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200922 _mgcp_client_send_dlcx(mgcp, epname);
923 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200924 return 0;
925error_close_fd:
926 close(wq->bfd.fd);
927 wq->bfd.fd = -1;
928 return rc;
929}
930
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200931/*! Initialize client connection (opens socket)
932 * \param[in,out] mgcp MGCP client descriptor.
933 * \returns 0 on success, -EINVAL on error. */
934int mgcp_client_connect(struct mgcp_client *mgcp)
935{
936 return mgcp_client_connect2(mgcp, 99);
937}
938
939/*! Terminate client connection
940 * \param[in,out] mgcp MGCP client descriptor.
941 * \returns 0 on success, -EINVAL on error. */
942void mgcp_client_disconnect(struct mgcp_client *mgcp)
943{
944 struct osmo_wqueue *wq;
945
946 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200947 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200948 return;
949 }
950
951 wq = &mgcp->wq;
952 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200953 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200954 close(wq->bfd.fd);
955 wq->bfd.fd = -1;
956 if (osmo_fd_is_registered(&wq->bfd))
957 osmo_fd_unregister(&wq->bfd);
958}
959
Philipp Maier275ac972018-01-20 00:58:16 +0100960/*! Get the IP-Aaddress of the associated MGW as string.
961 * \param[in] mgcp MGCP client descriptor.
962 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200963const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200964{
965 return mgcp->actual.remote_addr;
966}
967
Philipp Maier275ac972018-01-20 00:58:16 +0100968/*! Get the IP-Port of the associated MGW.
969 * \param[in] mgcp MGCP client descriptor.
970 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200971uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200972{
973 return mgcp->actual.remote_port;
974}
975
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200976/*! Get the IP-Address of the associated MGW as its numeric representation.
977 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100978 * \param[in] mgcp MGCP client descriptor.
979 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200980uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200981{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200982 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200983}
984
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100985/* To compose endpoint names, usually for CRCX, use this as domain name.
986 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
987const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
988{
989 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
990}
991
Philipp Maiera466f572020-06-25 16:11:04 +0200992/*! Compose endpoint name for a wildcarded request to the virtual trunk
993 * \param[in] mgcp MGCP client descriptor.
994 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200995const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
996{
997 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
998}
999
Philipp Maier48635912020-06-25 20:16:22 +02001000/*! Compose endpoint name for an E1 endpoint.
1001 * \param[in] ctx talloc context.
1002 * \param[in] mgcp MGCP client descriptor.
1003 * \param[in] trunk_id id number of the E1 trunk (1-64).
1004 * \param[in] ts timeslot on the E1 trunk (1-31).
1005 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
1006 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
1007 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
1008const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
1009 uint8_t rate, uint8_t offset)
1010{
1011 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
1012 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
1013 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
1014
1015 uint8_t i;
1016 bool rate_offs_valid = false;
1017 char *epname;
1018
1019 epname =
1020 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
1021 mgcp_client_endpoint_domain(mgcp));
1022 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001023 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +02001024 return NULL;
1025 }
1026
1027 /* Check if the supplied rate/offset pair resembles a valid combination */
1028 for (i = 0; i < sizeof(valid_rates); i++) {
1029 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1030 rate_offs_valid = true;
1031 }
1032 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001033 LOGPMGW(mgcp, LOGL_ERROR,
1034 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
1035 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +02001036 talloc_free(epname);
1037 return NULL;
1038 }
1039
1040 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1041 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001042 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001043 LOGPMGW(mgcp, LOGL_ERROR,
1044 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1045 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001046 talloc_free(epname);
1047 return NULL;
1048 }
1049
1050 return epname;
1051}
1052
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001053struct mgcp_response_pending * mgcp_client_pending_add(
1054 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001055 mgcp_trans_id_t trans_id,
1056 mgcp_response_cb_t response_cb,
1057 void *priv)
1058{
1059 struct mgcp_response_pending *pending;
1060
1061 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001062 if (!pending)
1063 return NULL;
1064
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001065 pending->trans_id = trans_id;
1066 pending->response_cb = response_cb;
1067 pending->priv = priv;
1068 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1069
1070 return pending;
1071}
1072
Philipp Maierdf9192e2021-09-03 17:50:51 +02001073/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001074 * response_cb. NOTE: the response_cb still needs to call
1075 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1076 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001077 * the response_cb is invoked.
1078 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1079 * mgcp_client_cancel() needs to be called for this transaction.
1080 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001081int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1082 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001083{
Harald Welte563ffc52020-06-17 21:54:13 +07001084 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001085 mgcp_trans_id_t trans_id;
1086 int rc;
1087
1088 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1089 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001090 LOGPMGW(mgcp, LOGL_ERROR,
1091 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001092 talloc_free(msg);
1093 return -EINVAL;
1094 }
1095
Harald Welte563ffc52020-06-17 21:54:13 +07001096 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1097 if (response_cb != NULL) {
1098 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1099 if (!pending) {
1100 talloc_free(msg);
1101 return -ENOMEM;
1102 }
Harald Welte827da2c2020-07-15 10:57:08 +02001103 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001104
1105 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001106 LOGPMGW(mgcp, LOGL_ERROR,
1107 "Cannot send, MGCP message too large: %u\n",
1108 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001109 msgb_free(msg);
1110 rc = -EINVAL;
1111 goto mgcp_tx_error;
1112 }
1113
1114 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1115 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001116 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001117 msgb_free(msg);
1118 goto mgcp_tx_error;
1119 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001120 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1121 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001122 return 0;
1123
1124mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001125 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001126 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001127 /* Dequeue pending response, it's going to be free()d */
1128 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001129 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001130 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001131 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001132}
1133
Philipp Maier275ac972018-01-20 00:58:16 +01001134/*! Cancel a pending transaction.
1135 * \param[in] mgcp MGCP client descriptor.
1136 * \param[in,out] trans_id Transaction id.
1137 * \returns 0 on success, -ENOENT on error.
1138 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001139 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1140 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1141 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001142 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001143int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1144{
1145 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1146 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001147 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001148 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001149 return -ENOENT;
1150 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001151 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001152 talloc_free(pending);
1153 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001154 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1155 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1156 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1157 * before we had a chance to send out the message and it is still going to be sent, we will just
1158 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1159 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001160 */
1161}
1162
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001163static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001164{
1165 /* avoid zero trans_id to distinguish from unset trans_id */
1166 if (!mgcp->next_trans_id)
1167 mgcp->next_trans_id ++;
1168 return mgcp->next_trans_id ++;
1169}
1170
Philipp Maier1dc6be62017-10-05 18:25:37 +02001171#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1172 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001173 MGCP_MSG_PRESENCE_CONN_MODE)
1174#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001175 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001176 MGCP_MSG_PRESENCE_CONN_ID)
1177#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1178#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1179#define MGCP_RSIP_MANDATORY 0 /* none */
1180
Philipp Maier704c4f02018-06-07 18:51:31 +02001181/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1182static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1183{
1184 unsigned int i;
1185 int rc = 0;
1186 const char *codec;
1187 unsigned int pt;
1188
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001189 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001190
1191 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001192 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001193
1194 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001195 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001196 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1197 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001198 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001199
Philipp Maier704c4f02018-06-07 18:51:31 +02001200 /* Note: Use codec descriptors from enum mgcp_codecs
1201 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001202 if (!codec) {
1203 msgb_free(msg);
1204 return -EINVAL;
1205 }
1206
1207 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001208 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001209 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001210 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001211 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001212 }
1213
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001214 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001215
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001216 if (rc != 0) {
1217 LOGP(DLMGCP, LOGL_ERROR,
1218 "message buffer to small, can not generate MGCP message (LCO)\n");
1219 msgb_free(msg);
1220 return -ENOBUFS;
1221 }
1222 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001223}
1224
1225/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1226static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1227{
1228 unsigned int i;
1229 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001230 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001231 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001232 const char *codec;
1233 unsigned int pt;
1234
1235 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001236 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001237
1238 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001239 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001240
1241 /* Determine local IP-Address */
1242 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001243 LOGPMGW(mgcp, LOGL_ERROR,
1244 "Could not determine local IP-Address!\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001245 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001246 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001247 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001248 local_ip_family = osmo_ip_str_type(local_ip);
1249 if (local_ip_family == AF_UNSPEC) {
1250 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001251 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001252 }
1253 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1254 if (audio_ip_family == AF_UNSPEC) {
1255 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001256 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001257 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001258
1259 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001260 rc |= msgb_printf(msg, "o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id,
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001261 local_ip_family == AF_INET6 ? '6' : '4',
1262 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001263
1264 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001265 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001266
1267 /* Add RTP address and port */
1268 if (mgcp_msg->audio_port == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001269 LOGPMGW(mgcp, LOGL_ERROR,
1270 "Invalid port number, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001271 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001272 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001273 }
1274 if (strlen(mgcp_msg->audio_ip) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001275 LOGPMGW(mgcp, LOGL_ERROR,
1276 "Empty ip address, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001277 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001278 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001279 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001280 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001281 audio_ip_family == AF_INET6 ? '6' : '4',
1282 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001283
1284 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001285 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001286
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001287 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001288 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1289 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001290 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001291
1292 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001293 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001294
Philipp Maier228e5912019-03-05 13:56:59 +01001295 /* Add optional codec parameters (fmtp) */
1296 if (mgcp_msg->param_present) {
1297 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1298 /* The following is only applicable for AMR */
1299 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1300 continue;
1301 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1302 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001303 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001304 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001305 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001306 }
1307 }
1308
Philipp Maier704c4f02018-06-07 18:51:31 +02001309 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1310 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001311
Philipp Maier704c4f02018-06-07 18:51:31 +02001312 /* Note: Only dynamic payload type from the range 96-127
1313 * require to be explained further via rtpmap. All others
1314 * are implcitly definedby the number in m=audio */
1315 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001316 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001317
1318 /* Note: Use codec descriptors from enum mgcp_codecs
1319 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001320 if (!codec) {
1321 msgb_free(msg);
1322 return -EINVAL;
1323 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001324
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001325 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001326 }
1327 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001328
Philipp Maier704c4f02018-06-07 18:51:31 +02001329 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001330 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001331
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001332 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001333 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message (SDP)\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001334 msgb_free(msg);
1335 return -ENOBUFS;
1336 }
1337
1338 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001339}
1340
Philipp Maier275ac972018-01-20 00:58:16 +01001341/*! Generate an MGCP message
1342 * \param[in] mgcp MGCP client descriptor.
1343 * \param[in] mgcp_msg Message description
1344 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001345struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1346{
1347 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1348 uint32_t mandatory_mask;
1349 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1350 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001351 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001352 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001353
1354 msg->l2h = msg->data;
1355 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1356
1357 /* Add command verb */
1358 switch (mgcp_msg->verb) {
1359 case MGCP_VERB_CRCX:
1360 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001361 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001362 break;
1363 case MGCP_VERB_MDCX:
1364 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001365 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001366 break;
1367 case MGCP_VERB_DLCX:
1368 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001369 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001370 break;
1371 case MGCP_VERB_AUEP:
1372 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001373 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001374 break;
1375 case MGCP_VERB_RSIP:
1376 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001377 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001378 break;
1379 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001380 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001381 msgb_free(msg);
1382 return NULL;
1383 }
1384
1385 /* Check if mandatory fields are missing */
1386 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001387 LOGPMGW(mgcp, LOGL_ERROR,
1388 "One or more missing mandatory fields, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001389 msgb_free(msg);
1390 return NULL;
1391 }
1392
1393 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001394 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1395 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001396 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001397 msgb_free(msg);
1398 return NULL;
1399 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001400
1401 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001402 LOGPMGW(mgcp, LOGL_ERROR,
1403 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1404 mgcp_msg->endpoint);
Philipp Maier3aa81572018-01-31 14:13:45 +01001405 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001406 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001407 }
1408
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001409 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001410 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001411
1412 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001413 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001414
1415 /* Add call id */
1416 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001417 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001418
1419 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001420 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1421 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001422 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001423 msgb_free(msg);
1424 return NULL;
1425 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001426 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001427 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001428
Philipp Maier704c4f02018-06-07 18:51:31 +02001429 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1430 * if we do not know this information yet, we fall back to LCO */
1431 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1432 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1433 use_sdp = true;
1434
1435 /* Add local connection options (LCO) */
1436 if (!use_sdp
1437 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001438 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1439 if (add_lco(msg, mgcp_msg) < 0)
1440 return NULL;
1441 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001442
1443 /* Add mode */
1444 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001445 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001446 msgb_printf(msg, "M: %s\r\n",
1447 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1448
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001449 /* Add X-Osmo-IGN */
1450 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1451 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001452 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001453 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1454 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1455
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001456 /* Add X-Osmo-Osmux */
1457 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001458 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001459 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1460 mgcp_msg->x_osmo_osmux_cid);
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001461 msgb_free(msg);
1462 return NULL;
1463 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001464 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001465 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001466 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1467 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1468 }
1469
1470
Philipp Maier704c4f02018-06-07 18:51:31 +02001471 /* Add session description protocol (SDP) */
1472 if (use_sdp
1473 && (mgcp_msg->verb == MGCP_VERB_CRCX
1474 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001475 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001476 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001477 }
1478
1479 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001480 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001481 msgb_free(msg);
1482 msg = NULL;
1483 }
1484
1485 return msg;
1486}
1487
Philipp Maier275ac972018-01-20 00:58:16 +01001488/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1489 * \param[in] msg message buffer
1490 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001491mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1492{
1493 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1494}
1495
Philipp Maierfa495d62021-09-02 10:08:56 +02001496/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001497 * \param[in] mgcp MGCP client descriptor.
1498 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001499struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001500{
1501 return &mgcp->actual;
1502}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001503
1504const struct value_string mgcp_client_connection_mode_strs[] = {
1505 { MGCP_CONN_NONE, "none" },
1506 { MGCP_CONN_RECV_SEND, "sendrecv" },
1507 { MGCP_CONN_SEND_ONLY, "sendonly" },
1508 { MGCP_CONN_RECV_ONLY, "recvonly" },
1509 { MGCP_CONN_LOOPBACK, "loopback" },
1510 { 0, NULL }
1511};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001512
1513/*! Get MGCP client instance name (VTY).
1514 * \param[in] mgcp MGCP client descriptor.
1515 * \returns MGCP client name.
1516 *
1517 * The user can only modify the name of an MGCP client instance when it is
1518 * part of a pool. For single MGCP client instances and MGCP client instance
1519 * where no description is set via the VTY, the MGW domain name will be used
1520 * as name. */
1521const char *mgcp_client_name(const struct mgcp_client *mgcp)
1522{
1523 if (!mgcp)
1524 return "(null)";
1525
1526 if (mgcp->actual.description)
1527 return mgcp->actual.description;
1528 else
1529 return mgcp_client_endpoint_domain(mgcp);
1530}