blob: f533d55611b7de9d6e66d7f86b487fb0ed119480 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* Message parser/generator utilities */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <limits.h>
26
27#include <osmocom/mgcp/mgcp_internal.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020028#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029#include <osmocom/mgcp/mgcp_msg.h>
30#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010031#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020032
33/*! Display an mgcp message on the log output.
34 * \param[in] message mgcp message string
35 * \param[in] len message mgcp message string length
36 * \param[in] preamble string to display in logtext in front of each line */
37void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble)
38{
39 unsigned char line[80];
40 unsigned char *ptr;
41 unsigned int consumed = 0;
42 unsigned int consumed_line = 0;
43 unsigned int line_count = 0;
44
45 if (!log_check_level(DLMGCP, LOGL_DEBUG))
46 return;
47
48 while (1) {
49 memset(line, 0, sizeof(line));
50 ptr = line;
51 consumed_line = 0;
52 do {
53 if (*message != '\n' && *message != '\r') {
54 *ptr = *message;
55 ptr++;
56 }
57 message++;
58 consumed++;
59 consumed_line++;
60 } while (*message != '\n' && consumed < len
61 && consumed_line < sizeof(line));
62
63 if (strlen((const char *)line)) {
64 LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
65 preamble, line_count, line);
66 line_count++;
67 }
68
69 if (consumed >= len)
70 return;
71 }
72}
73
74/*! Parse connection mode.
75 * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback)
76 * \param[in] endp pointer to endpoint (only used for log output)
77 * \param[out] associated connection to be modified accordingly
78 * \returns 0 on success, -1 on error */
79int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp,
80 struct mgcp_conn *conn)
81{
82 int ret = 0;
83
84 if (!mode) {
85 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010086 "endpoint:0x%x missing connection mode\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +020087 ENDPOINT_NUMBER(endp));
88 return -1;
89 }
90 if (!conn)
91 return -1;
92 if (!endp)
93 return -1;
94
95 if (strcmp(mode, "recvonly") == 0)
96 conn->mode = MGCP_CONN_RECV_ONLY;
97 else if (strcmp(mode, "sendrecv") == 0)
98 conn->mode = MGCP_CONN_RECV_SEND;
99 else if (strcmp(mode, "sendonly") == 0)
100 conn->mode = MGCP_CONN_SEND_ONLY;
101 else if (strcmp(mode, "loopback") == 0)
102 conn->mode = MGCP_CONN_LOOPBACK;
103 else {
104 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100105 "endpoint:0x%x unknown connection mode: '%s'\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 ENDPOINT_NUMBER(endp), mode);
107 ret = -1;
108 }
109
110 /* Special handling für RTP connections */
111 if (conn->type == MGCP_CONN_TYPE_RTP) {
112 conn->u.rtp.end.output_enabled =
113 conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
114 }
115
116 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100117 "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn));
119
120 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100121 "endpoint:0x%x connection mode '%s' %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122 ENDPOINT_NUMBER(endp), mode, conn->mode);
123
124 /* Special handling für RTP connections */
125 if (conn->type == MGCP_CONN_TYPE_RTP) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100126 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:0x%x output_enabled %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 ENDPOINT_NUMBER(endp), conn->u.rtp.end.output_enabled);
128 }
129
130 /* The VTY might change the connection mode at any time, so we have
131 * to hold a copy of the original connection mode */
132 conn->mode_orig = conn->mode;
133
134 return ret;
135}
136
137/* We have a null terminated string with the endpoint name here. We only
138 * support two kinds. Simple ones as seen on the BSC level and the ones
139 * seen on the trunk side. (helper function for find_endpoint()) */
140static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
141 const char *mgcp)
142{
143 char *rest = NULL;
144 struct mgcp_trunk_config *tcfg;
145 int trunk, endp;
146
147 trunk = strtoul(mgcp + 6, &rest, 10);
148 if (rest == NULL || rest[0] != '/' || trunk < 1) {
149 LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
150 return NULL;
151 }
152
153 endp = strtoul(rest + 1, &rest, 10);
154 if (rest == NULL || rest[0] != '@') {
155 LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
156 return NULL;
157 }
158
159 /* signalling is on timeslot 1 */
160 if (endp == 1)
161 return NULL;
162
163 tcfg = mgcp_trunk_num(cfg, trunk);
164 if (!tcfg) {
165 LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n",
166 trunk);
167 return NULL;
168 }
169
170 if (!tcfg->endpoints) {
171 LOGP(DLMGCP, LOGL_ERROR,
172 "Endpoints of trunk %d not allocated.\n", trunk);
173 return NULL;
174 }
175
176 if (endp < 1 || endp >= tcfg->number_endpoints) {
177 LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n",
178 mgcp);
179 return NULL;
180 }
181
182 return &tcfg->endpoints[endp];
183}
184
Philipp Maier55295f72018-01-15 14:00:28 +0100185/* Find an endpoint that is not in use. Do this by going through the endpoint
186 * array, check the callid. A callid nullpointer indicates that the endpoint
187 * is free */
188static struct mgcp_endpoint *find_free_endpoint(struct mgcp_endpoint *endpoints,
189 unsigned int number_endpoints)
190{
191 struct mgcp_endpoint *endp;
192 unsigned int i;
193
194 for (i = 0; i < number_endpoints; i++) {
195 if (endpoints[i].callid == NULL) {
196 endp = &endpoints[i];
197 LOGP(DLMGCP, LOGL_DEBUG,
198 "endpoint:0x%x found free endpoint\n",
199 ENDPOINT_NUMBER(endp));
200 endp->wildcarded_crcx = true;
201 return endp;
202 }
203 }
204
205 LOGP(DLMGCP, LOGL_ERROR, "Not able to find a free endpoint");
206 return NULL;
207}
208
Philipp Maier12943ea2018-01-17 15:40:25 +0100209/* Check if the domain name, which is supplied with the endpoint name
210 * matches the configuration. */
211static int check_domain_name(struct mgcp_config *cfg, const char *mgcp)
212{
213 char *domain_to_check;
214
215 domain_to_check = strstr(mgcp, "@");
216 if (!domain_to_check)
217 return -EINVAL;
218
219 if (strcmp(domain_to_check+1, cfg->domain) != 0)
220 return -EINVAL;
221
222 return 0;
223}
224
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225/* Search the endpoint pool for the endpoint that had been selected via the
226 * MGCP message (helper function for mgcp_analyze_header()) */
227static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg,
Philipp Maiera49e32a2018-02-01 18:18:50 +0100228 const char *mgcp,
229 int *cause)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230{
231 char *endptr = NULL;
232 unsigned int gw = INT_MAX;
Philipp Maier7f0966c2018-01-17 18:18:12 +0100233 const char *endpoint_number_str;
Philipp Maiera49e32a2018-02-01 18:18:50 +0100234 struct mgcp_endpoint *endp;
235
236 *cause = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200237
Philipp Maier7f0966c2018-01-17 18:18:12 +0100238 /* Check if the domainname in the request is correct */
Philipp Maier12943ea2018-01-17 15:40:25 +0100239 if (check_domain_name(cfg, mgcp)) {
240 LOGP(DLMGCP, LOGL_ERROR, "Wrong domain name '%s'\n", mgcp);
241 return NULL;
242 }
243
Philipp Maier7f0966c2018-01-17 18:18:12 +0100244 /* Check if the E1 trunk is requested */
Philipp Maiera49e32a2018-02-01 18:18:50 +0100245 if (strncmp(mgcp, "ds/e1", 5) == 0) {
246 endp = find_e1_endpoint(cfg, mgcp);
247 if (!endp)
248 *cause = -500;
249 return endp;
250 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251
Philipp Maier7f0966c2018-01-17 18:18:12 +0100252 /* Check if the virtual trunk is addressed (new, correct way with prefix) */
253 if (strncmp
254 (mgcp, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
255 strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK)) == 0) {
256 endpoint_number_str =
257 mgcp + strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
258 if (endpoint_number_str[0] == '*') {
Philipp Maiera49e32a2018-02-01 18:18:50 +0100259 endp = find_free_endpoint(cfg->trunk.endpoints,
Philipp Maier7f0966c2018-01-17 18:18:12 +0100260 cfg->trunk.number_endpoints);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100261 if (!endp)
262 *cause = -403;
263 return endp;
Philipp Maier7f0966c2018-01-17 18:18:12 +0100264 }
Philipp Maier7f0966c2018-01-17 18:18:12 +0100265 gw = strtoul(endpoint_number_str, &endptr, 16);
266 if (gw < cfg->trunk.number_endpoints && endptr[0] == '@')
267 return &cfg->trunk.endpoints[gw];
Philipp Maier55295f72018-01-15 14:00:28 +0100268 }
269
Philipp Maier7f0966c2018-01-17 18:18:12 +0100270 /* Deprecated method without prefix */
271 LOGP(DLMGCP, LOGL_NOTICE,
272 "Addressing virtual trunk without prefix (deprecated), please use %s: '%s'\n",
273 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, mgcp);
Harald Welte33eafe02017-12-28 03:15:27 +0100274 gw = strtoul(mgcp, &endptr, 16);
Philipp Maier03cc4842018-01-17 16:59:38 +0100275 if (gw < cfg->trunk.number_endpoints && endptr[0] == '@')
Philipp Maier87bd9be2017-08-22 16:35:41 +0200276 return &cfg->trunk.endpoints[gw];
277
278 LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100279 *cause = -500;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280 return NULL;
281}
282
283/*! Analyze and parse the the hader of an MGCP messeage string.
284 * \param[out] pdata caller provided memory to store the parsing results
285 * \param[in] data mgcp message string
286 * \returns when the status line was complete and transaction_id and
287 * endp out parameters are set, -1 on error */
288int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data)
289{
290 int i = 0;
291 char *elem, *save = NULL;
Philipp Maiera49e32a2018-02-01 18:18:50 +0100292 int cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293
294 /*! This function will parse the header part of the received
295 * MGCP message. The parsing results are stored in pdata.
296 * The function will also automatically search the pool with
297 * available endpoints in order to find an endpoint that matches
298 * the endpoint string in in the header */
299
300 OSMO_ASSERT(data);
301 pdata->trans = "000000";
302
303 for (elem = strtok_r(data, " ", &save); elem;
304 elem = strtok_r(NULL, " ", &save)) {
305 switch (i) {
306 case 0:
307 pdata->trans = elem;
308 break;
309 case 1:
Philipp Maiera49e32a2018-02-01 18:18:50 +0100310 pdata->endp = find_endpoint(pdata->cfg, elem, &cause);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200311 if (!pdata->endp) {
312 LOGP(DLMGCP, LOGL_ERROR,
313 "Unable to find Endpoint `%s'\n", elem);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100314 return cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315 }
316 break;
317 case 2:
318 if (strcmp("MGCP", elem)) {
319 LOGP(DLMGCP, LOGL_ERROR,
320 "MGCP header parsing error\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100321 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200322 }
323 break;
324 case 3:
325 if (strcmp("1.0", elem)) {
326 LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
327 "not supported\n", elem);
Harald Welteabbb6b92017-12-28 13:13:50 +0100328 return -528;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200329 }
330 break;
331 }
332 i++;
333 }
334
335 if (i != 4) {
336 LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
337 pdata->trans = "000000";
338 pdata->endp = NULL;
Harald Welteabbb6b92017-12-28 13:13:50 +0100339 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200340 }
341
342 return 0;
343}
344
345/*! Extract OSMUX CID from an MGCP parameter line (string).
346 * \param[in] line single parameter line from the MGCP message
347 * \returns OSMUX CID, -1 on error */
348int mgcp_parse_osmux_cid(const char *line)
349{
350 int osmux_cid;
351
352 if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1)
353 return -1;
354
355 if (osmux_cid > OSMUX_CID_MAX) {
356 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
357 osmux_cid, OSMUX_CID_MAX);
358 return -1;
359 }
360 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
361
362 return osmux_cid;
363}
364
365/*! Check MGCP parameter line (string) for plausibility.
366 * \param[in] endp pointer to endpoint (only used for log output)
367 * \param[in] line single parameter line from the MGCP message
368 * \returns 1 when line seems plausible, 0 on error */
369int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
370{
371 const size_t line_len = strlen(line);
372 if (line[0] != '\0' && line_len < 2) {
373 LOGP(DLMGCP, LOGL_ERROR,
374 "Wrong MGCP option format: '%s' on 0x%x\n",
375 line, ENDPOINT_NUMBER(endp));
376 return 0;
377 }
378
379 /* FIXME: A couple more checks wouldn't hurt... */
380
381 return 1;
382}
383
384/*! Check if the specified callid seems plausible.
385 * \param[in] endp pointer to endpoint
386 * \param{in] callid to verify
387 * \returns 1 when callid seems plausible, 0 on error */
388int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid)
389{
390 /*! This function compares the supplied callid with the called that is
391 * stored in the endpoint structure. */
392
393 if (!endp)
394 return -1;
395 if (!callid)
396 return -1;
397 if (!endp->callid)
398 return -1;
399
400 if (strcmp(endp->callid, callid) != 0) {
401 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100402 "endpoint:0x%x CallIDs does not match '%s' != '%s'\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403 ENDPOINT_NUMBER(endp), endp->callid, callid);
404 return -1;
405 }
406
407 return 0;
408}
409
410/*! Check if the specified connection id seems plausible.
411 * \param[in] endp pointer to endpoint
412 * \param{in] connection id to verify
413 * \returns 1 when connection id seems plausible, 0 on error */
Philipp Maier01d24a32017-11-21 17:26:09 +0100414int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415{
Philipp Maier01d24a32017-11-21 17:26:09 +0100416 /* Check for null identifiers */
417 if (!conn_id) {
418 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100419 "endpoint:0x%x invalid ConnectionIdentifier (missing)\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100420 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200421 return -1;
Philipp Maier01d24a32017-11-21 17:26:09 +0100422 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200423
Philipp Maier01d24a32017-11-21 17:26:09 +0100424 /* Check for empty connection identifiers */
425 if (strlen(conn_id) == 0) {
426 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100427 "endpoint:0x%x invalid ConnectionIdentifier (empty)\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100428 ENDPOINT_NUMBER(endp));
429 return -1;
430 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200431
Philipp Maier01d24a32017-11-21 17:26:09 +0100432 /* Check for over long connection identifiers */
433 if (strlen(conn_id) > MGCP_CONN_ID_LENGTH) {
434 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100435 "endpoint:0x%x invalid ConnectionIdentifier (too long) 0x%s\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100436 ENDPOINT_NUMBER(endp), conn_id);
437 return -1;
438 }
439
440 /* Check if connection exists */
441 if (mgcp_conn_get(endp, conn_id))
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 return 0;
443
444 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100445 "endpoint:0x%x no connection found under ConnectionIdentifier 0x%s\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100446 ENDPOINT_NUMBER(endp), conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200447
448 return -1;
449}
450
451/*! Extract individual lines from MCGP message.
452 * \param[in] str MGCP message string, consisting of multiple lines
453 * \param{in] saveptr pointer to next line in str
454 * \returns line, NULL when done */
455char *mgcp_strline(char *str, char **saveptr)
456{
457 char *result;
458
459 /*! The function must be called with *str set to the input string
460 * for the first line. After that saveptr will be initalized.
461 * all consecutive lines are extracted by calling the function
462 * with str set to NULL. When done, the function will return NULL
463 * to indicate that all lines have been parsed. */
464
465 if (str)
466 *saveptr = str;
467
468 result = *saveptr;
469
470 if (*saveptr != NULL) {
471 *saveptr = strpbrk(*saveptr, "\r\n");
472
473 if (*saveptr != NULL) {
474 char *eos = *saveptr;
475
476 if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n')
477 (*saveptr)++;
478 (*saveptr)++;
479 if ((*saveptr)[0] == '\0')
480 *saveptr = NULL;
481
482 *eos = '\0';
483 }
484 }
485
486 return result;
487}